Debian Codegraph @

src/Application.vala

raw
1 /*
2 * Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /**
19 * @class
20 * The Main Application
21 */
22 public class DesktopFolderApp : Gtk.Application {
23
24 /** File Monitor of desktop folder */
25 private FileMonitor monitor = null;
26
27 /** The volume monitor */
28 private GLib.VolumeMonitor volume_monitor = null;
29
30 /** schema settings */
31 private GLib.Settings settings = null;
32 private bool single_click = false;
33 private const string SHOW_DESKTOPPANEL_KEY = "desktop-panel";
34 private const string SHOW_DESKTOPICONS_KEY = "icons-on-desktop";
35 private const string SHOW_DESKTOPFOLDER_KEY = "show-desktopfolder";
36
37 private bool show_desktoppanel = false;
38 private bool show_desktopicons = false;
39
40 private bool desktop_visible = false;
41
42 /** List of folder owned by the application */
43 private DesktopFolder.DesktopManager desktop = null;
44 private List <DesktopFolder.FolderManager> folders = new List <DesktopFolder.FolderManager> ();
45 private List <DesktopFolder.NoteManager> notes = new List <DesktopFolder.NoteManager> ();
46 private List <DesktopFolder.PhotoManager> photos = new List <DesktopFolder.PhotoManager> ();
47 private int current_id = 0;
48
49 construct {
50 /* Needed by Glib.Application */
51 this.application_id = DesktopFolder.APP_ID; // Ensures an unique instance.
52 this.flags = ApplicationFlags.FLAGS_NONE;
53
54 /* Needed by Granite.Application */
55 /*
56 this.program_name = _(DesktopFolder.APP_TITLE);
57 this.exec_name = DesktopFolder.APP_NAME;
58 this.build_version = DesktopFolder.VERSION;
59 */
60 }
61
62 /**
63 * @constructor
64 */
65 public DesktopFolderApp () {
66 Object (application_id: "com.github.spheras.desktopfolder",
67 flags : ApplicationFlags.FLAGS_NONE);
68 }
69
70 /**
71 * @name get_fake_desktop
72 * @description return the fake desktop manager
73 * @return {DesktopFolder.DesktopManager} the fake desktop manager
74 */
75 public DesktopFolder.DesktopManager get_fake_desktop () {
76 return this.desktop;
77 }
78
79 /**
80 * @name activate
81 * @override
82 * @description activate life cycle
83 */
84 protected override void activate () {
85 base.activate ();
86 debug ("activate event");
87 this.hold ();
88 // we'll init the app in the activate event
89 init ();
90 }
91
92 /**
93 * @name startup
94 * @override
95 * @description startup life cycle
96 */
97 public override void startup () {
98 base.startup ();
99 debug ("startup event");
100 }
101
102 /**
103 * @name get_next_id
104 * @description return the next valid id for a window
105 * @return {string} the next valid id
106 */
107 public string get_next_id () {
108 this.current_id++;
109 return "id%d".printf (this.current_id);
110 }
111
112 /**
113 * @name init
114 * @description initialization of the application
115 */
116 private void init () {
117 // only one app at a time
118 if (get_windows ().length () > 0) {
119 get_windows ().data.present ();
120 return;
121 }
122
123 // define our settings schema
124 settings = new GLib.Settings ("com.github.spheras.desktopfolder");
125 try {
126 // loki -> GLib.File f_check_elementary = GLib.File.new_for_path ("/usr/share/glib-2.0/schemas/org.pantheon.files.gschema.xml");
127 GLib.File f_check_elementary = GLib.File.new_for_path ("/usr/share/glib-2.0/schemas/io.elementary.files.gschema.xml");
128 if (f_check_elementary.query_exists ()) {
129 // it seems we can't control an error reading settings!!
130 // loki -> GLib.Settings elementary_files_settings = new GLib.Settings ("org.pantheon.files.preferences");
131 GLib.Settings elementary_files_settings = new GLib.Settings ("io.elementary.files.preferences");
132 single_click = elementary_files_settings.get_boolean ("single-click");
133 }
134 } catch (Error error) {
135 // we don't have any files settings, using default config
136 }
137
138 // Connect to show-desktopfolder key
139 settings.changed[SHOW_DESKTOPFOLDER_KEY].connect (on_show_desktopfolder_changed);
140 on_show_desktopfolder_changed ();
141
142 // Connect to desktoppanel key
143
144 // This commented out line would allow the setting to be changed on the fly
145 // Commented out because it allows specific conditions where it will currently crash Mutter
146 // This application may also have an unknown bug causing the crash
147 //
148 // settings.changed[SHOW_DESKTOPPANEL_KEY].connect (on_show_desktoppanel_changed);
149
150 on_show_desktoppanel_changed ();
151
152 settings.changed[SHOW_DESKTOPICONS_KEY].connect (on_show_desktopicons_changed);
153 on_show_desktopicons_changed ();
154
155 create_shortcut ();
156
157 // we need the app folder (desktop folder)
158 var desktopFolder = File.new_for_path (DesktopFolderApp.get_app_folder ());
159 if (!desktopFolder.query_exists ()) {
160 DirUtils.create (DesktopFolderApp.get_app_folder (), 0755);
161 }
162
163 // initializing the clipboard manager
164 DesktopFolder.Clipboard.ClipboardManager.get_for_display ();
165
166 // providing css style
167 var provider = new Gtk.CssProvider ();
168 provider.load_from_resource ("com/github/spheras/desktopfolder/Application.css");
169 Gtk.StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), provider,
170 Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
171
172 // quit action
173 /*
174 var quit_action = new SimpleAction ("quit", null);
175 add_action (quit_action);
176 add_accelerator ("<Control>q", "app.quit", null);
177 quit_action.activate.connect (() => {
178 if (app_window != null) {
179 app_window.destroy ();
180 }
181 });
182 */
183
184 // we start creating the folders found at the desktop folder
185 this.sync_folders_and_notes ();
186 this.monitor_desktop_folder ();
187
188 // Listening to size change events
189 Gdk.Screen.get_default ().size_changed.connect (this.on_screen_size_changed);
190 Gdk.Screen.get_default ().composited_changed.connect (this.on_screen_size_changed);
191 Gdk.Screen.get_default ().monitors_changed.connect (this.on_screen_size_changed);
192
193 // Listening to workspace changes events
194 Wnck.Screen screen = Wnck.Screen.get_default ();
195 screen.active_workspace_changed.connect (this.on_workspace_change);
196
197 // Listening mount change events
198 this.volume_monitor = VolumeMonitor.get ();
199 volume_monitor.mount_changed.connect ((mount) => {
200 this.on_mount_changed ();
201 });
202 this.volume_monitor.volume_changed.connect ((volume) => {
203 this.on_mount_changed ();
204 });
205
206 Timeout.add (500, () => { // 1000
207 this.toggle_desktop_visibility ();
208 return false;
209 });
210 }
211
212 /** the uid reference for timeout workspace change */
213 private uint _workspace_change_ref1;
214 private uint _workspace_change_ref2;
215
216 /**
217 * @name on_workspace_change
218 * @param previous workspace
219 */
220 private void on_workspace_change (Wnck.Workspace ? previous) {
221 // removing the pending threads
222 try {
223 Source.remove (this._workspace_change_ref1);
224 } catch (Error err) {
225
226 }
227 try {
228 Source.remove (this._workspace_change_ref2);
229 } catch (Error err) {
230
231 }
232
233 this._workspace_change_ref1=Timeout.add (500, () => {
234 this.desktop.get_view ().visible=false;
235 foreach (var note in notes) {
236 note.get_view().visible=false;
237 }
238 foreach (var folder in folders) {
239 folder.get_view().visible=false;
240 }
241 foreach (var photo in photos) {
242 photo.get_view().visible=false;
243 }
244 return false;
245 });
246 this._workspace_change_ref2=Timeout.add (1000, () => {
247 this.desktop.get_view ().visible=true;
248 foreach (var note in notes) {
249 note.get_view().visible=true;
250 }
251 foreach (var folder in folders) {
252 folder.get_view().visible=true;
253 }
254 foreach (var photo in photos) {
255 photo.get_view().visible=true;
256 }
257 return false;
258 });
259
260
261
262 /*
263 Timeout.add (500, () => {
264 this.hide_everything ();
265
266 this.clear_folders ();
267 this.clear_notes ();
268 this.clear_photos ();
269
270 this.desktop_visible = false;
271 this.desktop = null;
272 return false;
273 });
274 Timeout.add (1000, () => {
275 this.desktop_visible = true;
276 this.desktop = new DesktopFolder.DesktopManager (this);
277
278 this.show_everything ();
279 // this.sync_folders_and_notes();
280 return false;
281 });
282 */
283
284 /*
285 Timeout.add (500, () => {
286 debug("heyyy1111: %s", (this.desktop.get_view ().visible?"true":"false"));
287 this.desktop.get_view ().visible=true;
288 debug("heyyy2222: %s", (this.desktop.get_view ().visible?"true":"false"));
289 this.desktop.get_view ().show();
290 return false;
291 });
292
293 Timeout.add (1000, () => {
294 debug("heyyy3333");
295 this.desktop.get_view ().show();
296 this.desktop.get_view ().visible=false;
297 this.desktop.get_view ().realize();
298 this.desktop.get_view ().show_now();
299 this.desktop.get_view ().show_all();
300 this.desktop.get_view ().present ();
301 this.desktop.get_view ().set_visible(true);
302 debug("heyyy4444");
303 return false;
304 });
305 */
306 /*
307 Timeout.add (500, () => {
308 this.hide_everything();
309 this.desktop = null;
310 this.create_fake_desktop ();
311 return false;
312 });
313 */
314 /*Timeout.add (1000, () => {
315 this.show_everything ();
316 return false;
317 });*/
318 }
319
320 /**
321 * @name on_show_desktopicons_changed
322 * @description detect when desktopicons key is toggled
323 */
324 private void on_show_desktopicons_changed () {
325 this.show_desktopicons = settings.get_boolean (SHOW_DESKTOPICONS_KEY);
326 // debug (@"called, show_desktoppanel: $(this.show_desktoppanel), show_desktopicons: $show_desktopicons");
327 if (this.show_desktoppanel) {
328 if (this.show_desktopicons) {
329 this.desktop.show_items ();
330 } else {
331 // debug ("calling hide_items");
332 this.desktop.hide_items ();
333 }
334 }
335 }
336
337 /**
338 * @name get_desktopicons_enabled
339 * @description detect when desktopicons key is toggled
340 */
341 public bool get_desktopicons_enabled () {
342 return show_desktopicons;
343 }
344
345 /**
346 * @name on_show_desktoppanel_changed
347 * @description detect when desktoppanel key is toggled
348 */
349 private void on_show_desktoppanel_changed () {
350 this.show_desktoppanel = settings.get_boolean (SHOW_DESKTOPPANEL_KEY);
351 debug (@"show_desktoppanel: $(this.show_desktoppanel)");
352 if (this.show_desktoppanel) {
353 this.create_fake_desktop ();
354 } else {
355 if (this.desktop != null) {
356 this.desktop.close ();
357 this.desktop = null;
358 }
359 }
360 }
361
362 /**
363 * @name on_show_desktopfolder_changed
364 * @description detect when desktopfolder key is toggled
365 */
366 public bool get_desktoppanel_enabled () {
367 return show_desktoppanel;
368 }
369
370 /**
371 * @name on_show_desktopfolder_changed
372 * @description detect when desktopfolder key is toggled
373 */
374 private void on_show_desktopfolder_changed () {
375 bool show_desktopfolder = settings.get_boolean (SHOW_DESKTOPFOLDER_KEY);
376 if (!show_desktopfolder) {
377 // requested to no longer show the desktop so let's gracefully quit
378 this.quit ();
379 }
380 }
381
382 /**
383 * @name on_mount_changed
384 * @description event to detect when the mount file system has been changed, probably we need to recheck files existence
385 */
386 public void on_mount_changed () {
387 debug ("MOUNT FILE SYSTEM CHANGED");
388 for (int i = 0; i < this.folders.length (); i++) {
389 this.folders.nth_data (i).on_mount_changed ();
390 }
391 if (this.desktop != null) {
392 this.desktop.on_mount_changed ();
393 }
394 }
395
396 /** resize event timeout id */
397 private uint resize_event_timeout = 0;
398
399
400 /**
401 * @name on_screen_size_changed
402 * @description detecting screen size changes
403 */
404
405 public void on_screen_size_changed () {
406 if (this.resize_event_timeout > 0) {
407 Source.remove (this.resize_event_timeout);
408 this.resize_event_timeout = 0;
409 }
410 // waiting 1 second to ensure the correct size information from monitors
411 this.resize_event_timeout = Timeout.add (1000, () => {
412 this.resize_event_timeout = 0;
413
414 debug ("SCREEN SIZE CHANGED!");
415 Gdk.Screen screen = Gdk.Screen.get_default ();
416 if (this.desktop != null) {
417 this.desktop.on_screen_size_changed (screen);
418 }
419 foreach (var folder in folders) {
420 folder.on_screen_size_changed (screen);
421 }
422 foreach (var note in notes) {
423 note.on_screen_size_changed (screen);
424 }
425 foreach (var photo in photos) {
426 photo.on_screen_size_changed (screen);
427 }
428
429 return false;
430 });
431 }
432
433 /** the desktop folder name */
434 public static string desktop_folder_name = "Desktop";
435
436 /**
437 * @name get_app_folder
438 * @description return the path where the app search folders to be created (the desktop folder)
439 * @return string the absolute path directory
440 */
441 public static string get_app_folder () {
442 return Environment.get_home_dir () + "/" + DesktopFolderApp.desktop_folder_name;
443 }
444
445 /**
446 * @name create_fake_desktop
447 * @description create the fake desktop
448 */
449 private void create_fake_desktop () {
450 this.desktop = new DesktopFolder.DesktopManager (this);
451 this.clear_all ();
452 }
453
454 /**
455 * @name sync_folders_and_notes
456 * @description create as many folder and note windows as the desktop folder and note founds
457 */
458 private void sync_folders_and_notes () {
459 try {
460 var base_path = DesktopFolderApp.get_app_folder ();
461 var directory = File.new_for_path (base_path);
462 var enumerator = directory.enumerate_children (FileAttribute.STANDARD_NAME, 0);
463
464 FileInfo file_info;
465 List <DesktopFolder.FolderManager> updated_folder_list = new List <DesktopFolder.FolderManager> ();
466 List <DesktopFolder.NoteManager> updated_note_list = new List <DesktopFolder.NoteManager> ();
467 List <DesktopFolder.PhotoManager> updated_photo_list = new List <DesktopFolder.PhotoManager> ();
468 int totalFolders = 0;
469 int totalNotes = 0;
470 int totalPhotos = 0;
471 while ((file_info = enumerator.next_file ()) != null) {
472 string name = file_info.get_name ();
473 File file = File.new_for_commandline_arg (base_path + "/" + name);
474 FileType type = file.query_file_type (FileQueryInfoFlags.NONE);
475
476 File nopanel = File.new_for_commandline_arg (base_path + "/" + name + "/" + DesktopFolder.PANEL_BLACKLIST_FILE);
477 File panel_flag = File.new_for_commandline_arg (base_path + "/" + name + "/" + DesktopFolder.FOLDER_SETTINGS_FILE);
478 if (type == FileType.DIRECTORY) {
479
480 // Is this folder already known about?
481 DesktopFolder.FolderManager fm = this.find_folder_by_name (name);
482
483 if (nopanel.query_exists () || !panel_flag.query_exists ()) {
484 if (fm != null) {
485 // This folder doesn't want to be a panel anymore
486 // (this check might be pointless however because it's already done in FolderManager's sync)
487 fm.close ();
488 }
489 // This folder doesn't want to be a panel, let's skip it
490 continue;
491 }
492
493 totalFolders++;
494
495 if (fm == null) {
496 // No, it's a new folder
497 fm = new DesktopFolder.FolderManager (this, name);
498 } else {
499 this.folders.remove (fm);
500 }
501 updated_folder_list.append (fm);
502 } else {
503 string basename = file.get_basename ();
504 int index = basename.last_index_of (".", 0);
505 if (index > 0) {
506 string ext = basename.substring (index + 1);
507 string file_name = basename.substring (0, index);
508 if (ext == DesktopFolder.OLD_NOTE_EXTENSION || ext == DesktopFolder.NEW_NOTE_EXTENSION) {
509 totalNotes++;
510
511 // Is this note already known about?
512 DesktopFolder.NoteManager nm = this.find_note_by_name (file_name);
513
514 if (nm == null) {
515 // No, it's a new note
516 nm = new DesktopFolder.NoteManager (this, basename.substring (0, index), file);
517 } else {
518 this.notes.remove (nm);
519 }
520 if (nm.is_valid ()) {
521 updated_note_list.append (nm);
522 }
523 } else if (ext == DesktopFolder.OLD_PHOTO_EXTENSION || ext == DesktopFolder.NEW_PHOTO_EXTENSION) {
524 totalPhotos++;
525
526 // Is this photo already known about?
527 DesktopFolder.PhotoManager pm = this.find_photo_by_name (file_name);
528
529 if (pm == null) {
530 // No, it's a new photo
531 pm = new DesktopFolder.PhotoManager (this, basename.substring (0, index), file);
532 } else {
533 this.photos.remove (pm);
534 }
535 if (pm.is_valid ()) {
536 updated_photo_list.append (pm);
537 }
538 }
539 }
540 // nothing
541 // we only deal with folders to be shown
542 }
543 }
544
545
546 // finally we close any other not existent folder
547 while (this.folders.length () > 0) {
548 DesktopFolder.FolderManager fm = this.folders.nth (0).data;
549 fm.close ();
550 this.folders.remove (fm);
551 }
552 this.folders = (owned) updated_folder_list;
553
554 // finally we close any other not existent note
555 while (this.notes.length () > 0) {
556 DesktopFolder.NoteManager nm = this.notes.nth (0).data;
557 nm.close ();
558 this.notes.remove (nm);
559 }
560 this.notes = (owned) updated_note_list;
561
562 // finally we close any other not existent photo
563 while (this.photos.length () > 0) {
564 DesktopFolder.PhotoManager pm = this.photos.nth (0).data;
565 pm.close ();
566 this.photos.remove (pm);
567 }
568 this.photos = (owned) updated_photo_list;
569
570 // by default, we create at least one folder if set by settings
571 if (totalFolders == 0 && totalPhotos == 0 && totalNotes == 0 && this.desktop == null) {
572 string first_panel_path = DesktopFolderApp.get_app_folder () + "/" + DesktopFolder.Lang.APP_FIRST_PANEL;
573 DirUtils.create (first_panel_path, 0755);
574 File first_settings_file = File.new_for_path (first_panel_path + "/.desktopfolder");
575 DesktopFolder.FolderSettings fs = new DesktopFolder.FolderSettings (DesktopFolder.Lang.APP_FIRST_PANEL);
576 fs.save_to_file (first_settings_file);
577
578 this.sync_folders_and_notes ();
579 }
580
581 } catch (Error e) {
582 // error! ??
583 stderr.printf ("Error: %s\n", e.message);
584 DesktopFolder.Util.show_error_dialog ("Error", e.message);
585 }
586 }
587
588 /**
589 * @name count_widgets
590 * @description return the amount of widgets existing
591 * @return {int} the total widgets currently shown
592 */
593 public uint count_widgets () {
594 return this.photos.length () + this.notes.length () + this.folders.length ();
595 }
596
597 /**
598 * @name find_folder_by_name
599 * @description find a foldermanager managed by its name
600 * @param string folder_name the name of the folder to find
601 * @return FolderManager the Folder found or null if none
602 */
603 private DesktopFolder.FolderManager ? find_folder_by_name (string folder_name) {
604 for (int i = 0; i < this.folders.length (); i++) {
605 DesktopFolder.FolderManager fm = this.folders.nth (i).data;
606 if (fm.get_folder_name () == folder_name) {
607 return fm;
608 }
609 }
610 return null;
611 }
612
613 /**
614 * @name find_note_by_name
615 * @description find a notemanager managed by its name
616 * @param string note_name the name of the note to find
617 * @return NoteManager the Note found or null if none
618 */
619 private DesktopFolder.NoteManager ? find_note_by_name (string note_name) {
620 for (int i = 0; i < this.notes.length (); i++) {
621 DesktopFolder.NoteManager nm = this.notes.nth (i).data;
622 if (nm.get_note_name () == note_name) {
623 return nm;
624 }
625 }
626 return null;
627 }
628
629 /**
630 * @name find_photo_by_name
631 * @description find a photomanager managed by its name
632 * @param string photo_name the name of the photo to find
633 * @return PhotoManager the Photo found or null if none
634 */
635 private DesktopFolder.PhotoManager ? find_photo_by_name (string photo_name) {
636 for (int i = 0; i < this.photos.length (); i++) {
637 DesktopFolder.PhotoManager nm = this.photos.nth (i).data;
638 if (nm.get_photo_name () == photo_name) {
639 return nm;
640 }
641 }
642 return null;
643 }
644
645 /**
646 * @name get_single_click
647 * @description get the single click system setting (fallback is false)
648 * @return bool whether single click is on or not
649 */
650 public bool get_single_click () {
651 return single_click;
652 }
653
654 /**
655 * @name exist_manager
656 * @description check if the folder_name is being monitored or not
657 * @return bool true->yes, it is being monitored
658 */
659 public bool exist_manager (string folder_name) {
660 for (int i = 0; i < this.folders.length (); i++) {
661 DesktopFolder.FolderManager fm = this.folders.nth (i).data;
662 if (fm.get_folder_name () == folder_name) {
663 return true;
664 }
665 }
666 return false;
667 }
668
669 /**
670 * @name monitor_desktop_folder
671 * @description monitor the desktop folder
672 */
673 private void monitor_desktop_folder () {
674 try {
675 if (this.monitor != null) {
676 // if we have an existing monitor, we cancel it before to monitor again
677 this.monitor.cancel ();
678 }
679 var basePath = DesktopFolderApp.get_app_folder ();
680 File directory = File.new_for_path (basePath);
681 this.monitor = directory.monitor_directory (FileMonitorFlags.SEND_MOVED, null);
682 this.monitor.rate_limit = 100;
683 debug ("Monitoring: %s\n", directory.get_path ());
684 this.monitor.changed.connect (this.desktop_changed);
685 } catch (Error e) {
686 stderr.printf ("Error: %s\n", e.message);
687 DesktopFolder.Util.show_error_dialog ("Error", e.message);
688 }
689 }
690
691 /**
692 * @name desktop_changed
693 * @description we received an event of the monitor that indicates a change
694 * @see changed signal of FileMonitor (https://valadoc.org/gio-2.0/GLib.FileMonitor.changed.html)
695 */
696 private void desktop_changed (GLib.File src, GLib.File ? dest, FileMonitorEvent event) {
697 // string src_path = "";
698 // if (src == null) {
699 // src_path = "null";
700 // } else {
701 // src_path = src.get_path ().to_string ();
702 // }
703 // string dest_path = "";
704 // if (dest == null) {
705 // dest_path = "null";
706 // } else {
707 // dest_path = dest.get_path ().to_string ();
708 // }
709 // debug ("src: " + src_path + " dest: " + dest_path + " event: " + event.to_string ());
710
711 // something changed at the desktop folder
712 bool flagNote = false;
713 bool flagPhoto = false;
714
715 string basename = src.get_basename ();
716 int index = basename.last_index_of (".", 0);
717 if (index > 0) {
718 string ext = basename.substring (index + 1);
719 if (event == FileMonitorEvent.CHANGES_DONE_HINT && (ext == DesktopFolder.OLD_NOTE_EXTENSION || ext == DesktopFolder.NEW_NOTE_EXTENSION)) {
720 flagNote = true;
721 } else if (event == FileMonitorEvent.CHANGES_DONE_HINT && (ext == DesktopFolder.OLD_PHOTO_EXTENSION || ext == DesktopFolder.NEW_PHOTO_EXTENSION)) {
722 flagPhoto = true;
723 }
724 }
725
726 // new content inside
727 var file_type = src.query_file_type (FileQueryInfoFlags.NONE);
728 if (flagNote || flagPhoto || file_type == FileType.DIRECTORY || !src.query_exists ()) {
729 // new directory or removed, we need to synchronize
730 // debug ("desktop changed, calling sync_folders_and_notes");
731 this.sync_folders_and_notes ();
732 }
733 }
734
735 /**
736 * @name get_desktop_visibility
737 * @description show or hide the desktop
738 */
739 public bool get_desktop_visibility () {
740 return this.desktop_visible;
741 }
742
743 /**
744 * @name toggle_desktop_visiblity
745 * @description show or hide the desktop
746 */
747 public void toggle_desktop_visibility () {
748 this.desktop_visible = !this.desktop_visible;
749 debug (@"desktop_visible is now $(this.desktop_visible)");
750 if (this.desktop_visible) {
751 this.show_everything ();
752 } else {
753 hide_everything ();
754 }
755 }
756
757 public void show_everything () {
758 this.desktop.show_view ();
759 foreach (var folder in folders) {
760 folder.show_view ();
761 }
762 foreach (var note in notes) {
763 note.show_view ();
764 }
765 foreach (var photo in photos) {
766 photo.show_view ();
767 }
768 }
769
770 public void hide_everything () {
771 this.desktop.hide_view ();
772 foreach (var folder in folders) {
773 folder.hide_view ();
774 }
775 foreach (var note in notes) {
776 note.hide_view ();
777 }
778 foreach (var photo in photos) {
779 photo.hide_view ();
780 }
781 }
782
783 /**
784 * @name clear_folders
785 * @description close all the folders launched
786 */
787 protected void clear_folders () {
788 foreach (var folder in folders) {
789 folder.close ();
790 }
791 this.folders = new List <DesktopFolder.FolderManager> ();
792 }
793
794 /**
795 * @name clear_notes
796 * @description close all the notes launched
797 */
798 protected void clear_notes () {
799 foreach (var note in notes) {
800 note.close ();
801 }
802 this.notes = new List <DesktopFolder.NoteManager> ();
803 }
804
805 /**
806 * @name clear_photos
807 * @description close all the photos launched
808 */
809 protected void clear_photos () {
810 foreach (var photo in photos) {
811 photo.close ();
812 }
813 this.photos = new List <DesktopFolder.PhotoManager> ();
814 }
815
816 /**
817 * @name clear_all
818 * @description close all the windows launched
819 */
820 protected void clear_all () {
821 this.clear_folders ();
822 this.clear_notes ();
823 this.clear_photos ();
824 this.sync_folders_and_notes ();
825 }
826
827 /**
828 * Main application
829 */
830 public static int main (string[] args) {
831 if (args.length > 1 && args[1].up () == DesktopFolder.PARAM_SHOW_DESKTOP.up ()) {
832 minimize_all (args);
833 return 0;
834 }
835 if (args.length > 1 && (args[1].up () == DesktopFolder.PARAM_SHOW_VERSION.up () || args[1].up () == "--" + DesktopFolder.PARAM_SHOW_VERSION.up ())) {
836 stdout.printf ("Desktop Folder. Version %s\n", DesktopFolder.VERSION);
837 return 0;
838 } else {
839 var app = new DesktopFolderApp ();
840 if (args.length > 1) {
841 DesktopFolderApp.desktop_folder_name = args[1];
842 }
843 return app.run ();
844 }
845 }
846
847 /**
848 * @name minimize_all
849 * @description minimize all windows
850 * @param args string[] the list of args to initialize Gdk
851 */
852 private static void minimize_all (string[] args) {
853 Gtk.init (ref args);
854
855 bool flagShowingDesktop = true;
856
857 // Help wanted: Need to check manually if we are showing desktop
858 // because screen.get_showing_desktop (); always return false
859 unowned Wnck.Screen screen = Wnck.Screen.get_default ();
860 while (Gtk.events_pending ()) {
861 Gtk.main_iteration ();
862 }
863 unowned List <Wnck.Window> windows = screen.get_windows ();
864 foreach (Wnck.Window w in windows) {
865 if (!w.is_minimized () && w.get_window_type () == Wnck.WindowType.NORMAL) {
866 flagShowingDesktop = false;
867 }
868 }
869
870
871 // unowned Wnck.Screen screen = Wnck.Screen.get_default ();
872 bool show = !flagShowingDesktop; // !screen.get_showing_desktop ();
873 screen.toggle_showing_desktop (show);
874
875 /*
876 string sshow="show: %s".printf(show?"true":"false");
877 // The MessageDialog
878 Gtk.MessageDialog msg = new Gtk.MessageDialog (null, Gtk.DialogFlags.MODAL, Gtk.MessageType.WARNING, Gtk.ButtonsType.OK_CANCEL, "show:"+sshow);
879 msg.response.connect ((response_id) => {
880 switch (response_id) {
881 case Gtk.ResponseType.OK:
882 stdout.puts ("Ok\n");
883 break;
884 case Gtk.ResponseType.CANCEL:
885 stdout.puts ("Cancel\n");
886 break;
887 case Gtk.ResponseType.DELETE_EVENT:
888 stdout.puts ("Delete\n");
889 break;
890 }
891 msg.destroy();
892 });
893 msg.show ();
894 Gtk.main ();
895 */
896
897 /**
898 "manual style"
899 Wnck.Screen screen = Wnck.Screen.get_default ();
900 while (Gtk.events_pending ()) {
901 Gtk.main_iteration ();
902 }
903 unowned List <Wnck.Window> windows = screen.get_windows ();
904 foreach (Wnck.Window w in windows) {
905 Wnck.Application window_app = w.get_application ();
906 string name = window_app.get_name ();
907 // debug("app name:%s",name);
908 if (name != DesktopFolder.APP_ID) {
909 w.minimize ();
910 }
911 }
912 */
913 }
914
915 /**
916 * @name create_shortcut
917 * @description create a short cut SUPER-D at the system shortcuts to minimize all windows
918 */
919 private static void create_shortcut () {
920 string path = "/usr/bin/"; // we expect to have the command at the path
921 Pantheon.Keyboard.Shortcuts.CustomShortcutSettings.init ();
922 var shortcut = new Pantheon.Keyboard.Shortcuts.Shortcut (100, Gdk.ModifierType.SUPER_MASK);
923 string command_conflict = "";
924 string relocatable_schema_conflict = "";
925 if (!Pantheon.Keyboard.Shortcuts.CustomShortcutSettings.shortcut_conflicts (shortcut, out command_conflict,
926 out relocatable_schema_conflict)) {
927
928 debug ("registering hotkey!");
929 var relocatable_schema = Pantheon.Keyboard.Shortcuts.CustomShortcutSettings.create_shortcut ();
930 Pantheon.Keyboard.Shortcuts.CustomShortcutSettings.edit_command ((string) relocatable_schema,
931 path + "com.github.spheras.desktopfolder " + DesktopFolder.PARAM_SHOW_DESKTOP);
932 Pantheon.Keyboard.Shortcuts.CustomShortcutSettings.edit_shortcut ((string) relocatable_schema,
933 shortcut.to_gsettings ());
934 }
935 }
936
937 }