Debian Codegraph @

src/logic/FolderManager.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 protected errordomain FolderManagerIOError {
19 FILE_EXISTS,
20 MOVE_ERROR
21 }
22
23 /**
24 * @class
25 * Desktop Folder Manager
26 */
27 public class DesktopFolder.FolderManager : Object, DragnDrop.DndView, FolderSettingsInfoProvider {
28 /** parent application */
29 private DesktopFolderApp application;
30 /** to know if the panel is moveable or not */
31 protected bool is_moveable = true;
32 /** the view of this logic */
33 protected FolderWindow view = null;
34 /** Folder Settings of this folder */
35 private FolderSettings settings = null;
36 /** File Monitor of this folder */
37 private FileMonitor monitor = null;
38 /** List of items of this folder */
39 public List <ItemManager> items = null;
40 /** name of the folder */
41 private string folder_name = null;
42 /** drag and drop behaviour for this folder */
43 private DragnDrop.DndBehaviour dnd_behaviour = null;
44 /** the arrangement for this folder manager */
45 private FolderArrangement arrangement = null;
46 // the list of selected items
47 private Gee.List <ItemView> selected_items = new Gee.ArrayList <ItemView>();
48 /** Folder Sync Thread */
49 private FolderSync.Thread sync_thread = null;
50 /** the id for the organize event timer */
51 private uint organize_event_timeout;
52
53
54 /**
55 * @constructor
56 * @param DesktopFolderApp application the application owner of this window
57 * @param string folder_name the name of the folder
58 */
59 public FolderManager (DesktopFolderApp application, string folder_name) {
60 this.folder_name = folder_name;
61
62 // Let's load the settings of the folder (if exist or a new one)
63 this.load_folder_settings ();
64
65 // First we create a Folder Window above the desktop
66 this.application = application;
67 this.create_view ();
68
69 this.try_to_order_at_top ();
70
71 // let's sync the files found at this folder
72 this.sync_files (0, 0);
73
74 // finally, we start monitoring the folder
75 this.monitor_folder ();
76
77 this.view.refresh ();
78
79 this.dnd_behaviour = new DragnDrop.DndBehaviour (this, false, true);
80 }
81
82 /**
83 * @overrided
84 */
85 public virtual int get_parent_default_arrangement_orientation_setting () {
86 return FolderSettings.ARRANGEMENT_ORIENTATION_HORIZONTAL;
87 }
88
89 /**
90 * @name create_view
91 * @description create the view associated with this manager
92 */
93 protected virtual void create_view () {
94 this.view = new DesktopFolder.FolderWindow (this);
95 this.application.add_window (this.view);
96 }
97
98 /**
99 * @name on_screen_size_changed
100 * @description detecting screen size changes
101 */
102 public virtual void on_screen_size_changed (Gdk.Screen screen) {
103 debug ("size changed for %s", this.folder_name);
104 this.settings.calculate_current_position ();
105 debug ("reloading settings");
106 this.view.reload_settings ();
107 }
108
109 /**
110 * @name on_arrange_change
111 * @description arrange type changed for the panel
112 */
113 public void on_arrange_change (int type) {
114 if (this.settings.arrangement_type != type) {
115 this.settings.arrangement_type = type;
116 this.settings.save ();
117 this.arrangement = FolderArrangement.factory (this.settings.arrangement_type);
118
119 if (this.arrangement.force_organization ()) {
120 this.organize_panel_items ();
121 }
122 }
123 }
124
125 /**
126 * @name get_item_by_filename
127 * @description get item by filename, or null if none
128 * @param string filename to get item for, null if none
129 */
130 public ItemManager ? get_item_by_filename (string name) {
131 foreach (ItemManager item in this.items) {
132 if (item.get_file_name () == name) {
133 return item;
134 }
135 }
136 return null;
137 }
138
139 public void select_items (Gdk.Rectangle sel_rectangle) {
140 this.view.unselect_all ();
141 this.selected_items.clear ();
142 // debug("sel_rectangle(%d,%d,%d,%d)",sel_rectangle.x,sel_rectangle.y,sel_rectangle.width,sel_rectangle.height);
143 foreach (ItemManager item in this.items) {
144 Gdk.Rectangle item_rect = item.get_view ().get_bounding_box ();
145 // debug("item_rect(%d,%d,%d,%d)",item_rect.x,item_rect.y,item_rect.width,item_rect.height);
146 bool intersect = item_rect.intersect (sel_rectangle, null);
147 if (intersect) {
148 // debug("sel_rectangle(%d,%d,%d,%d)",sel_rectangle.x,sel_rectangle.y,sel_rectangle.width,sel_rectangle.height);
149 // debug("item_rect(%d,%d,%d,%d)",item_rect.x,item_rect.y,item_rect.width,item_rect.height);
150 item.get_view ().select_add ();
151 }
152 }
153 }
154
155 /**
156 * @name set_selected_item
157 * @description set the selected item
158 * @param ItemView selected the new selected item
159 */
160 public void set_selected_item (ItemView ? selected) {
161 this.selected_items.clear ();
162 this.selected_items.add (selected);
163 }
164
165 /**
166 * @name add_selected_item
167 * @description add the item to the selected list of items
168 * @param {ItemView} the new selected item to add
169 */
170 public void add_selected_item (ItemView ? selected) {
171 this.selected_items.add (selected);
172 }
173
174 /**
175 * @name remove_selected_item
176 * @description remove a selected item from the list of selected items
177 * @param {ItemView} selected the item view which will be removed
178 */
179 public void remove_selected_item (ItemView selected) {
180 this.selected_items.remove (selected);
181 }
182
183 /**
184 * @name get_selected_item
185 * @description return the current selected item, or null if none
186 * @return ItemView the current selected item, null if none
187 */
188 public Gee.List <ItemView> get_selected_items () {
189 return this.selected_items;
190 }
191
192 /**
193 * @name are_items_selected
194 * @description check whether there are 1 or more items selected
195 * @return {bool} true->yes, there are
196 */
197 public bool are_items_selected () {
198 return this.selected_items.size > 0;
199 }
200
201 /**
202 * @name are_items_locked
203 * @description return whether the items are locked or not
204 * @return {bool} true->yes, the items are locked, false otherwise
205 */
206 public bool are_items_locked () {
207 return this.get_settings ().lockitems;
208 }
209
210 /**
211 * @name load_folder_settings
212 * @description load the settings file inside the folder (if exist), if not, it will create a new one.
213 * The settings file contains the basic info saved to create window and items componentes.. position, size, etc..
214 */
215 private void load_folder_settings () {
216 // let's search the folder settings file
217 var abs_path = this.get_absolute_path ();
218 // debug ("loading folder settings...%s", abs_path);
219 var settings_file = abs_path + "/.desktopfolder";
220 var file = File.new_for_path (settings_file);
221 if (!file.query_exists ()) {
222 // we don't have yet a folder settings file, let's create one
223 FolderSettings newone = new FolderSettings (this.folder_name);
224 newone.save_to_file (file);
225 this.settings = newone;
226 } else {
227 FolderSettings existent = FolderSettings.read_settings (file, this.get_folder_name (), this);
228 this.settings = existent;
229 }
230
231 this.settings.calculate_current_position ();
232
233 // creating the Manager
234 this.arrangement = FolderArrangement.factory (this.settings.arrangement_type);
235 }
236
237 /**
238 * @name can_move
239 * @description say if the panel can move or not
240 * @return {bool} true->yes, the panel can be moved
241 */
242 public bool can_move () {
243 return this.is_moveable && !this.settings.lockpanel;
244 }
245
246 /**
247 * @name monitor_folder
248 * @description monitor the folder owned by this manager looking for changes inside
249 */
250 private void monitor_folder () {
251 try {
252 if (this.monitor != null) {
253 // if we have an existing monitor, we cancel it before to monitor again
254 this.monitor.cancel ();
255 }
256 File directory = this.get_file ();
257 this.monitor = directory.monitor_directory (FileMonitorFlags.WATCH_MOVES, null);
258 this.monitor.rate_limit = 100;
259 debug ("Monitoring: %s", directory.get_path ());
260 this.monitor.changed.connect (this.directory_changed);
261 } catch (Error e) {
262 stderr.printf ("Error: %s\n", e.message);
263 Util.show_error_dialog ("Error", e.message);
264 }
265 }
266
267 /**
268 * @name on_sync_finished
269 * @description sync thread has been finished
270 */
271 public void on_sync_finished () {
272 this.view.refresh ();
273 }
274
275 /**
276 * @name directory_changed
277 * @description we received an event of the monitor that indicates a change
278 * @see changed signal of FileMonitor (https://valadoc.org/gio-2.0/GLib.FileMonitor.changed.html)
279 */
280 private void directory_changed (GLib.File src, GLib.File ? dest, FileMonitorEvent event) {
281 File file_myself = this.get_file ();
282 if (file_myself.get_path () == src.get_path ()) {
283 // its me! maybe I was removed :(
284 return;
285 }
286 string old_filename = src.get_basename ();
287 if (old_filename == DesktopFolder.FOLDER_SETTINGS_FILE) {
288 // we ignore the settings file changes
289 } else {
290 debug ("%s - Change Detected - %d", this.get_folder_name (), event);
291 if (event == FileMonitorEvent.RENAMED) {
292 // something has been renamed
293 string new_filename = dest.get_basename ();
294 this.settings.rename (old_filename, new_filename);
295 this.settings.save ();
296
297 // we need to rename the affected file/folder
298 for (int i = 0; i < this.items.length (); i++) {
299 ItemManager element = (ItemManager) this.items.nth_data (i);
300 if (element.get_file_name () == old_filename) {
301 element.rename (new_filename);
302 element.get_view ().rename (new_filename);
303 }
304 }
305
306 // finally we refresh the view
307 this.view.refresh ();
308 } else {
309 // somehthing changed.. created or removed
310 this.sync_files (0, 0);
311 }
312
313 }
314 }
315
316 /**
317 * @name get_folder_name
318 * @description return the folder name
319 * @return string the folder name
320 */
321 public string get_folder_name () {
322 return this.folder_name;
323 }
324
325 /**
326 * @name get_application
327 * @description return the desktop folder application
328 * @return DesktopFolderApp
329 */
330 public DesktopFolderApp get_application () {
331 return this.application;
332 }
333
334 /**
335 * @name get_arrangement
336 * @description return the current arrangement for the panel's items
337 * @return FolderArrangement the current arrangement class
338 */
339 public FolderArrangement get_arrangement () {
340 return this.arrangement;
341 }
342
343 /**
344 * @name skip_file
345 * @description to check if the folder manager should skip the file and not take into account
346 */
347 public virtual bool skip_file (File file) {
348 string basename = file.get_basename ();
349 if (basename.has_prefix (".")) {
350 return true;
351 }
352 if (basename.has_suffix ("~")) {
353 return true;
354 }
355 return false;
356 }
357
358 protected void try_to_order_at_top () {
359 this.view.set_keep_below (false);
360 this.view.set_keep_above (true);
361 this.view.present ();
362 this.view.set_keep_above (false);
363 this.view.set_keep_below (true);
364 }
365
366 /**
367 * @name stop_sync
368 * @description stop the syncing thread (if any)
369 */
370 public bool is_sync_running () {
371 if (this.sync_thread != null) {
372 return this.sync_thread.is_running ();
373 }
374 return false;
375 }
376
377 /**
378 * @name sync_files
379 * @description sync all the files contained at the folder this manager refers to
380 * @param x int the x position where any new item found should be positioned, <=0 if this algorithm must decide
381 * @param y int the y position where any new item found should be positioned, <=0 if this algorithm must decide
382 */
383 public void sync_files (int x, int y) {
384 if (this.sync_thread == null) {
385 this.sync_thread = new FolderSync.Thread (this);
386 }
387 this.load_folder_settings ();
388 this.sync_thread.sync_files (x, y);
389 }
390
391 /**
392 * @name organize_panel_items
393 * @description the panel try to organize all the items over the panel. This is asked manually by the user.
394 */
395 public void organize_panel_items () {
396 if (this.organize_event_timeout > 0) {
397 Source.remove (this.organize_event_timeout);
398 this.organize_event_timeout = 0;
399 }
400 this.organize_event_timeout = Timeout.add (500, () => {
401 this.organize_event_timeout = 0;
402
403 bool asc = !this.settings.sort_reverse;
404 FolderArrangement.organize_items (this.view, ref this.items, this.settings.sort_by_type, asc, this.is_vertical_arragement ());
405
406 return false;
407 });
408 }
409
410 /**
411 * @name get_item_at
412 * @description return the item at a certain area of the folder window
413 * @param {Gdk.Rectangle} rectangle the rectangle area to search
414 * @return {ItemManager} return the item at that area found, or null if none
415 */
416 public Gee.List <ItemManager> ? get_items_at (Gdk.Rectangle rectangle) {
417 Gee.List <ItemManager> result = new Gee.ArrayList <ItemManager>();
418 for (int i = 0; i < this.items.length (); i++) {
419 ItemManager im = this.items.nth_data (i);
420 ItemView iv = im.get_view ();
421 Gtk.Allocation allocation;
422 iv.get_allocation (out allocation);
423
424 int qwidth = DesktopFolder.ICON_DEFAULT_WIDTH / 2;
425 Gdk.Rectangle icon_rectangle = Gdk.Rectangle ();
426 icon_rectangle.x = allocation.x + qwidth;
427 icon_rectangle.y = allocation.y + qwidth;
428 icon_rectangle.width = DesktopFolder.ICON_DEFAULT_WIDTH - qwidth;
429 icon_rectangle.height = DesktopFolder.ICON_DEFAULT_WIDTH - qwidth;
430
431 if (rectangle.intersect (icon_rectangle, null)) {
432 result.add (im);
433 }
434 }
435
436 return result;
437 }
438
439 /**
440 * @name is_vertical_arragement
441 * @description check whether the arrangement is vertically
442 * @return {bool} true->vertical, false->horizontal
443 */
444 public bool is_vertical_arragement () {
445 return this.settings.arrangement_orientation == FolderSettings.ARRANGEMENT_ORIENTATION_VERTICAL;
446 }
447
448 /**
449 * @name quick_show_items
450 * @description shows the items
451 */
452 public void quick_show_items () {
453 // TODO Make this less messy
454 foreach (var item in items) {
455 item.get_view ().show_all ();
456 item.get_view ().get_style_context ().remove_class ("df_fadingwindow");
457 item.get_view ().get_style_context ().remove_class ("df_fadeout");
458 item.get_view ().get_style_context ().add_class ("df_fadein");
459 Timeout.add (20, () => {
460 item.get_view ().get_style_context ().add_class ("df_fadingwindow");
461 return false;
462 });
463 }
464 }
465
466 /**
467 * @name show_items
468 * @description shows the items
469 */
470 public virtual void show_items () {
471 foreach (var item in items) {
472 item.show_view ();
473 }
474 }
475
476 /**
477 * @name hide_items
478 * @description hides the items
479 */
480 public virtual void hide_items () {
481 foreach (var item in items) {
482 item.hide_view ();
483 }
484 }
485
486 /**
487 * @name show_view
488 * @description show the folder
489 */
490 public virtual void show_view () {
491 // setting opacity to stop the folder window flashing at startup
492 this.view.opacity = 1;
493 this.view.show_all ();
494 this.view.fade_in ();
495 this.show_items ();
496 }
497
498 /**
499 * @name hide_view
500 * @description hide the folder
501 */
502 public virtual void hide_view () {
503 this.view.fade_out ();
504 Timeout.add (160, () => {
505 // ditto
506 this.view.opacity = 0;
507 this.view.hide ();
508 return false;
509 });
510 }
511
512 /**
513 * @name clear_all
514 * @description clear all the items associated with this folder
515 */
516 public void clear_all () {
517 this.items = new List <ItemManager> ();
518 this.view.clear_all ();
519 }
520
521 /**
522 * @name open_terminal_here
523 * @description open terminal here, works in folder & on Desktop
524 */
525 public void open_terminal_here (string path) {
526 try {
527 Environment.set_current_dir (path);
528 Process.spawn_command_line_async ("x-terminal-emulator --working-directory \"" + path + "\"");
529 } catch (Error e) {
530 stderr.printf ("Error: %s\n", e.message);
531 Util.show_error_dialog ("Error", e.message);
532 }
533 }
534
535 /**
536 * @name create_new_folder_inside
537 * @description function to create inside the recent created folder whatever is needed
538 * @param {string} folder_path the folder which is being created
539 */
540 protected virtual void create_new_folder_inside (string folder_path) {
541 }
542
543 /**
544 * @name create_new_folder
545 * @description create a new folder inside this folder
546 * @param string name the name of the new folder
547 * @param int x the x position of the new folder
548 * @param int y the y position of the new folder
549 */
550 public string create_new_folder (int x, int y, string name = DesktopFolder.Lang.DESKTOPFOLDER_NEW_FOLDER_NAME) {
551 string path = this.get_absolute_path () + "/" + name;
552 string new_name = "";
553 File folder = File.new_for_path (path);
554
555 if (folder.query_exists ()) {
556 new_name = DesktopFolder.Util.make_next_duplicate_name (name, this.get_absolute_path ());
557 } else {
558 new_name = name;
559 }
560 // cancelling the current monitor
561 this.monitor.cancel ();
562
563 try {
564 string folder_path = this.get_absolute_path () + "/" + new_name;
565 DirUtils.create (folder_path, 0755);
566
567 this.create_new_folder_inside (folder_path);
568 // forcing the sync of the files as a new folder has been created
569 this.sync_files (x, y);
570 // monitoring again
571 this.monitor_folder ();
572 } catch (Error e) {
573 stderr.printf ("Error: %s\n", e.message);
574 Util.show_error_dialog ("Error", e.message);
575 }
576
577 return new_name;
578 }
579
580 /**
581 * @name create_new_text_file
582 * @description create a new text file inside this folder
583 * @param string name the name of the new text file
584 * @param int x the x position of the new file
585 * @param int y the y position of the new file
586 */
587 public string create_new_text_file (int x, int y, string name = DesktopFolder.Lang.DESKTOPFOLDER_NEW_TEXT_FILE_NAME) {
588 string path = this.get_absolute_path () + "/" + name;
589 string new_name = "";
590
591 File file = File.new_for_path (path);
592 if (file.query_exists ()) {
593 new_name = DesktopFolder.Util.make_next_duplicate_name (name, this.get_absolute_path ());
594 } else {
595 new_name = name;
596 }
597
598 // cancelling the current monitor
599 this.monitor.cancel ();
600
601 // we create the text file with a touch command
602 try {
603 file = File.new_for_path (this.get_absolute_path () + "/" + new_name);
604 file.create (FileCreateFlags.NONE);
605
606 // forcing the sync of the files as a new folder has been created
607 this.sync_files (x, y);
608 // monitoring again
609 this.monitor_folder ();
610 } catch (Error e) {
611 stderr.printf ("Error: %s\n", e.message);
612 Util.show_error_dialog ("Error", e.message);
613 }
614 return new_name;
615 }
616
617 /**
618 * @name create_new_link
619 * @description create a new link inside this folder
620 * @param string target the target of the new link file
621 * @param int x the x position of the new file
622 * @param int y the y position of the new file
623 */
624 public void create_new_link (string target, int x, int y) {
625 // cancelling the current monitor
626 this.monitor.cancel ();
627
628 // we create the text file with a touch command
629 try {
630 var file = File.new_for_path (target);
631 var name = file.get_basename ();
632 var command = "ln -s \"" + target + "\" \"" + this.get_absolute_path () + "/" + name + "\"";
633 // debug("command: %s"+command);
634 var appinfo = AppInfo.create_from_commandline (command, null, AppInfoCreateFlags.SUPPORTS_URIS);
635 appinfo.launch_uris (null, null);
636
637 // forcing the sync of the files as a new lynk has been created
638 this.sync_files (x, y);
639 // monitoring again
640 this.monitor_folder ();
641 } catch (Error e) {
642 stderr.printf ("Error: %s\n", e.message);
643 Util.show_error_dialog ("Error", e.message);
644 }
645 }
646
647 /**
648 * @name trash
649 * @description deleting myself!!
650 */
651 public void trash () {
652 try {
653 File file = File.new_for_path (this.get_absolute_path ());
654 file.trash ();
655 this.close ();
656 } catch (Error error) {
657 stderr.printf ("Error: %s\n", error.message);
658 Util.show_error_dialog ("Error", error.message);
659 }
660 }
661
662 /**
663 * @name close
664 * @description close the folder manager and its view
665 */
666 public void close () {
667 this.monitor.cancel ();
668 this.view.hide ();
669 this.application.remove_window (this.view);
670 this.view.close ();
671 }
672
673 /**
674 * @name rename
675 * @description Renaming the folder
676 * @param string name the new name
677 * @return bool true->everything is ok, false->something failed, rollback
678 */
679 public bool rename (string new_name) {
680 string sanitized_name = DesktopFolder.Util.sanitize_name (new_name);
681 if (!DesktopFolder.Util.check_name (sanitized_name)) {
682 DesktopFolder.Util.show_invalid_name_error_dialog (this.view, new_name);
683 return false;
684 }
685 var old_name = this.folder_name;
686 var old_path = this.get_absolute_path ();
687 this.folder_name = sanitized_name;
688 var new_path = this.get_absolute_path ();
689 var directory = File.new_for_path (new_path);
690 try {
691 if (directory.query_exists ()) {
692 DesktopFolder.Util.show_file_exists_error_dialog (this.view, sanitized_name, _("Panel"), null);
693 throw new FolderManagerIOError.FILE_EXISTS ("Folder already exists");
694 }
695 this.settings.name = this.folder_name;
696 this.settings.save ();
697 FileUtils.rename (old_path, new_path);
698 var new_directory = File.new_for_path (new_path);
699 if (new_directory.query_exists ()) {
700 // forcing to reload settings
701 this.load_folder_settings ();
702 this.sync_files (0, 0);
703 this.monitor_folder ();
704 return true;
705 } else {
706 throw new FolderManagerIOError.MOVE_ERROR ("Failed to rename folder");
707 }
708 } catch (Error error) {
709 warning (error.message);
710 // Revert changes
711 this.folder_name = old_name;
712 this.settings.name = this.folder_name;
713 this.settings.save ();
714 return false;
715 }
716 }
717
718 /**
719 * @name paste
720 * @description paste what ever is in the clipboard (file or folder) to this desktop folder
721 */
722 public void paste () {
723 Clipboard.ClipboardManager cm = Clipboard.ClipboardManager.get_for_display ();
724 if (cm.can_paste) {
725 File folder = this.get_file ();
726 cm.paste_files (folder, null, null);
727 }
728 }
729
730 /**
731 * @name set_new_shape
732 * @description set a new shape (position and size) of the view
733 */
734 public void set_new_shape (int x, int y, int width, int height) {
735 this.settings.x = x;
736 this.settings.y = y;
737 bool flag_size_change = (this.settings.w != width || this.settings.h != height);
738 this.settings.w = width;
739 this.settings.h = height;
740 this.settings.save ();
741
742 if (flag_size_change && this.get_arrangement ().force_organization ()) {
743 this.organize_panel_items ();
744 }
745
746 if (flag_size_change && this.sync_thread != null) {
747 this.sync_thread.on_resize ();
748 }
749 }
750
751 /**
752 * @name save_head_color
753 * @description save a head color to the settings file
754 * @param color string the color for the head to be saved
755 */
756 public void save_head_color (string color) {
757 this.settings.fgcolor = color;
758 this.settings.save ();
759 }
760
761 /**
762 * @name save_body_color
763 * @description save a body color to the settings file
764 * @param color string the color for the body to be saved
765 */
766 public void save_body_color (string color) {
767 this.settings.bgcolor = color;
768 this.settings.save ();
769 }
770
771 /**
772 * @name get_absolute_path
773 * @description return the absolute path for this folder
774 * @return the absolute path
775 */
776 public string get_absolute_path () {
777 return DesktopFolderApp.get_app_folder () + "/" + this.folder_name;
778 }
779
780 /**
781 * @name get_file
782 * @description return the Glib.File associated with this folder
783 * @return File the File object
784 */
785 private File get_file () {
786 var basePath = this.get_absolute_path ();
787 File directory = File.new_for_path (basePath);
788 return directory;
789 }
790
791 /**
792 * @name get_settings
793 * @description return the settings of this folder
794 * @return FolderSettings the settings of this folder
795 */
796 public FolderSettings get_settings () {
797 return this.settings;
798 }
799
800 /**
801 * @name reopen
802 * @description close the current view and reopen it again
803 */
804 public void reopen () {
805 this.get_settings ().save ();
806
807 // closing
808 this.application.remove_window (this.view);
809 this.close ();
810
811 // reopening
812 this.create_view ();
813
814 this.try_to_order_at_top ();
815
816
817 // let's sync the files found at this folder
818 this.sync_files (0, 0);
819
820 this.monitor_folder ();
821
822 this.view.show_all ();
823 }
824
825 /**
826 * @name get_view
827 * @description return the FolderWindow view of this manager
828 * @return FolderWindow
829 */
830 public FolderWindow get_view () {
831 return this.view;
832 }
833
834 /**
835 * @name on_active
836 * @description the folder is window is being active.
837 */
838 public void on_active () {
839 // lets recheck the file existence
840 this.on_mount_changed ();
841 }
842
843 /**
844 * @name on_mount_changed
845 * @description the mount filesystem has been changed
846 */
847 public void on_mount_changed () {
848 // lets recheck the file existence
849 for (int i = 0; i < this.items.length (); i++) {
850 ItemManager element = (ItemManager) this.items.nth_data (i);
851 element.recheck_existence ();
852 }
853 }
854
855 // ---------------------------------------------------------------------------------------
856 // ---------------------------DndView Implementation--------------------------------------
857 // ---------------------------------------------------------------------------------------
858
859 public void on_drag_motion () {
860 }
861
862 public void on_drag_leave () {
863 }
864
865 /**
866 * @overrided
867 */
868 public DragnDrop.DndView[] get_all_selected_views () {
869 Gee.List <ItemView> selected_views = this.get_selected_items ();
870 DragnDrop.DndView[] result = new DragnDrop.DndView[selected_views.size];
871 for (int i = 0; i < selected_views.size; i++) {
872 result[i] = (DragnDrop.DndView)selected_views.@get (i);
873 }
874 return result;
875 }
876
877 /**
878 * @name get_widget
879 * @description return the widget associated with this view
880 * @return Widget the widget
881 */
882 public Gtk.Widget get_widget () {
883 return this.view;
884 }
885
886 /**
887 * @name get_application_window
888 * @description return the application window of this view, needed for drag operations
889 * @return ApplicationWindow
890 */
891 public Gtk.ApplicationWindow get_application_window () {
892 return this.view;
893 }
894
895 /**
896 * @name get_file_at
897 * @name get the file at the position x, y
898 * @return File
899 */
900 public GLib.File get_file_at (int x, int y) {
901 return this.get_file ();
902 }
903
904 /**
905 * @name is_link
906 * @description check whether the item is a link to other folder or not
907 * @return {bool} true-> yes it is a link
908 */
909 public bool is_link () {
910 var file = this.get_file ();
911 var path = file.get_path ();
912 return FileUtils.test (path, FileTest.IS_SYMLINK);
913 }
914
915 /**
916 * @name is_writable
917 * @description indicates if the file linked by this view is writable or not
918 * @return bool
919 */
920 public bool is_writable () {
921 // TODO
922 return true;
923 }
924
925 /**
926 * @name is_folder
927 * @description check whether the view represents a folder or a file
928 * @return bool true->this view represents a folder
929 */
930 public bool is_folder () {
931 return true;
932 }
933
934 /**
935 * @name get_target_location
936 * @description return the target File that represents this view
937 * @return File the file target of this view
938 */
939 public GLib.File get_target_location () {
940 return this.get_file ();
941 }
942
943 /**
944 * @name is_recent_uri_scheme
945 * @description check whether the File is a recent uri scheme?
946 * @return bool
947 */
948 public bool is_recent_uri_scheme () {
949 return true;
950 }
951
952 /**
953 * @name get_display_target_uri
954 * @description return the target uri of this view
955 * @return string the target uri
956 */
957 public string get_display_target_uri () {
958 return DragnDrop.Util.get_display_target_uri (this.get_file ());
959 }
960
961 /**
962 * @overrided
963 */
964 public Gtk.Image get_image () {
965 return null as Gtk.Image;
966 }
967
968 public void on_drag_end () {
969 // nothing
970 }
971
972 // ---------------------------------------------------------------------------------------
973 // ---------------------------**********************--------------------------------------
974 // ---------------------------------------------------------------------------------------
975 }
976
977 public class DesktopFolder.FolderSync.Param {
978 public int x { get; set; }
979 public int y { get; set; }
980
981 public Param (int x, int y) {
982 this.x = x;
983 this.y = y;
984 }
985 }
986
987 public class DesktopFolder.FolderSync.PendingItem {
988 public string file_name { get; set; }
989 public File file { get; set; }
990 public bool calculate_position { get; set; default = false; }
991 public ItemSettings settings { get; set; }
992
993 public PendingItem (File file, string file_name, bool calculate_position, ItemSettings ? settings) {
994 this.file = file;
995 this.file_name = file_name;
996 this.calculate_position = calculate_position;
997 this.settings = settings;
998 }
999 }
1000
1001 public class DesktopFolder.FolderSync.Thread {
1002 /** the manager owner */
1003 private FolderManager manager;
1004 /** flag to know if the algorithm should stop and restart */
1005 private bool flag_restart = false;
1006 /** flag to know whether the sync algorithm is running or not */
1007 private bool flag_running = false;
1008
1009 /** list of pending items to add to the folder window */
1010 private Gee.List <PendingItem> pending_items_to_process = null;
1011 /** list of pending command params to process */
1012 private Gee.List <Param> pending_params_to_process = null;
1013 /** semaphore */
1014 private Mutex mutex = Mutex ();
1015 // the current grid of items, util to find gaps and position automatically new items
1016 FolderGrid <ItemSettings> grid = null;
1017
1018 /**
1019 * Constructor
1020 * @param {FolderManager} manager the manager owned of this thread
1021 */
1022 public Thread (FolderManager manager) {
1023 this.manager = manager;
1024 this.pending_params_to_process = new Gee.ArrayList <Param>();
1025 }
1026
1027 public bool is_running () {
1028 return this.flag_running;
1029 }
1030
1031 /**
1032 * @name add_pending
1033 * @descripition add a param to pending params to process
1034 */
1035 private void add_pending_param (Param param) {
1036 mutex.lock () ;
1037 this.pending_params_to_process.add (param);
1038 mutex.unlock ();
1039 }
1040
1041 /**
1042 * @name on_resize
1043 * @description the folder window was resize, this function notify of that event
1044 */
1045 public void on_resize () {
1046 mutex.lock () ;
1047 this.grid = null;
1048 mutex.unlock ();
1049 }
1050
1051 /**
1052 * @name sync_files
1053 * @param {int} x the x point where the next not managed item found should be positioned
1054 * @param {int} y the y point where the next not managed item found should be positioned
1055 */
1056 public void sync_files (int x, int y) {
1057 // we add the pending param to be processed, when a new item is found
1058 this.add_pending_param (new Param (x, y));
1059
1060 mutex.lock () ;
1061 if (this.flag_running) {
1062 // it is running, so, lets start
1063 this.flag_restart = true;
1064 } else {
1065 // the algorithm is not running, lets execute it in a new thread
1066 this.flag_running = true;
1067 try {
1068 this.manager.get_view ().show_loading ();
1069 new GLib.Thread <bool> .try ("sync_thread", this._sync_files);
1070 } catch (Error e) {
1071 stderr.printf ("Error: %s\n", e.message);
1072 Util.show_error_dialog ("Error", e.message);
1073 }
1074 }
1075 mutex.unlock ();
1076 }
1077
1078 /**
1079 * @name set_restart
1080 * @description set the flag restart to a value (concurrent safe)
1081 * @param {bool} value the value to set
1082 */
1083 private void set_restart (bool value) {
1084 mutex.lock () ;
1085 this.flag_restart = value;
1086 mutex.unlock ();
1087 }
1088
1089 /**
1090 * @name set_running
1091 * @description set the flag running to a value (concurrent safe)
1092 * @param {bool} value the value to set
1093 */
1094 private void set_running (bool value) {
1095 mutex.lock () ;
1096 this.flag_running = value;
1097 mutex.unlock ();
1098 }
1099
1100 /**
1101 * @name _sync_files
1102 * @description this is the sync algorithm processed in a different thread
1103 */
1104 private bool _sync_files () {
1105 this.grid = null;
1106 debug (">>>>>>>>>>> INIT _sync_files for Panel: %s", this.manager.get_folder_name ());
1107 this.set_running (true);
1108 this.set_restart (true);
1109 int restart_count = 0;
1110
1111 // main loop, in case we need to restart the algorithm (new sync commands)
1112 while (this.flag_restart) {
1113 restart_count++;
1114 debug (">>>>>>>>>>>>>>>>>>>>>>>RESTARTING _sync_files %d times for Panel %s", restart_count - 1, this.manager.get_folder_name ());
1115 this.set_restart (false);
1116
1117 // 1. setting some initial variables
1118 // --------------------------------------------------------------------------
1119 // list of current items that are showed in the window
1120 List <ItemManager> old_showed_items = new List <ItemManager>();
1121 this.manager.items.foreach ((entry) => {
1122 // debug("actualmente el manager tiene este entry: %s",entry.get_file_name());
1123 old_showed_items.append (entry);
1124 }) ;
1125 // list of new items to be viewed in the window
1126 List <ItemManager> new_viewed_items = new List <ItemManager>();
1127 // list of current items managed by the settings
1128 Gee.HashMap <string, ItemSettings> old_managed_items = this.manager.get_settings ().get_items_parsed ();
1129 // list of pending items to be processed (to create setting, manager and view)
1130 this.pending_items_to_process = new Gee.ArrayList <PendingItem>();
1131 string base_path = this.manager.get_absolute_path ();
1132 File directory = this.manager.get_file ();
1133 var file_enumerator = directory.enumerate_children (FileAttribute.STANDARD_NAME, 0);
1134 FileInfo file_info;
1135
1136 // 2. looping through all the files in the folder
1137 // --------------------------------------------------------------------------
1138 while ((file_info = file_enumerator.next_file ()) != null) {
1139 ////////////////////////////////////
1140 // >>>>>>>>> check control <<<<<<<<<
1141 if (this.flag_restart) /////////////
1142 break; /////////////////////////
1143 // >>>>>>>>> check control <<<<<<<<<
1144 ////////////////////////////////////
1145
1146 // lets get the file to process
1147 string file_name = file_info.get_name ();
1148 File file = File.new_for_commandline_arg (base_path + "/" + file_name);
1149 debug ("syncing file found:%s", base_path + "/" + file_name);
1150
1151 // checking the .nopanel flag
1152 if (file_name == ".nopanel") {
1153 // This folder doesn't want to be a panel anymore, destroy the panel
1154 debug (".nopanel found, destroying panel");
1155 this.manager.close ();
1156 this.set_running (false);
1157 this.set_restart (false);
1158 return false;
1159 }
1160
1161 // checking if we must skip the file
1162 if (this.manager.skip_file (file)) {
1163 // debug("skiping file %s", file_name);
1164 continue;
1165 }
1166
1167 // we try to get the settings for this item
1168 ItemSettings is = old_managed_items[file_name];
1169 if (is == null) {
1170 // debug("1---we don't have this file managed yet: %s",file_name);
1171 // we don't have this file managed yet
1172 this.pending_items_to_process.add (new PendingItem (file, file_name, true, null));
1173 } else {
1174 // lets check if the item already exists
1175 ItemManager old_item_manager = this.pop_item_from_list (file_name, ref old_showed_items);
1176 if (old_item_manager != null) {
1177 // debug("2---yes, this is an existing already managed file: %s",file_name);
1178 // yes, this is an existing already managed file, lets update
1179 old_item_manager.set_file (file);
1180 new_viewed_items.append (old_item_manager);
1181 } else {
1182 // debug("3---no, we need to add this file: %s",file_name);
1183 this.pending_items_to_process.add (new PendingItem (file, file_name, false, is));
1184 }
1185 }
1186 }
1187
1188 ////////////////////////////////////
1189 // >>>>>>>>> check control <<<<<<<<<
1190 if (this.flag_restart) /////////////
1191 continue; //////////////////////
1192 // >>>>>>>>> check control <<<<<<<<<
1193 ////////////////////////////////////
1194
1195
1196 // 2. Last part, there are new items that need to be positioned in a valid place
1197 // --------------------------------------------------------------------------
1198 // removing old entries, now no exist
1199 old_showed_items.foreach ((entry) => {
1200 GLib.Idle.add_full (GLib.Priority.LOW, () => {
1201 this.manager.get_view ().remove_item (entry.get_view ());
1202 return false;
1203 });
1204 }) ;
1205 this.manager.items = new List <ItemManager>();
1206 new_viewed_items.foreach ((entry) => {
1207 // GLib.Idle.add_full (GLib.Priority.LOW, () => {
1208 this.manager.items.append (entry);
1209 // return false;
1210 // });
1211 }) ;
1212
1213 if (this.pending_items_to_process.size > 0) {
1214 // there are pending items to be processed, it means, create the widget and so
1215 // Therefore, the pending actions need to be executed in the main gtk Thread
1216 GLib.Idle.add_full (GLib.Priority.LOW, () => {
1217 // max items to process in the gtk draw thread
1218 const int MAX = 10;
1219 int index = 0;
1220 while (index < MAX && this.pending_items_to_process.size > 0) {
1221 PendingItem pending = this.pending_items_to_process.remove_at (0);
1222 // debug ("drawing widget for file: %s", pending.file_name);
1223
1224 if (!pending.calculate_position) {
1225 // managing previously saved items in settings
1226 ItemManager item = new ItemManager (pending.file_name, pending.file, this.manager);
1227 this.manager.items.append (item);
1228 this.manager.get_view ().add_item (item.get_view (), pending.settings.x, pending.settings.y);
1229 } else {
1230 // new item to position
1231 // we need to create one empty setting
1232 ItemSettings is = new ItemSettings ();
1233 int x = 0;
1234 int y = 0;
1235
1236 // trying to get a position for the new items
1237 this.mutex.lock () ;
1238 if (this.pending_params_to_process.size > 0) {
1239 Param fsp = this.pending_params_to_process.remove_at (0);
1240 x = fsp.x;
1241 y = fsp.y;
1242 }
1243 if (x == 0 && y == 0) {
1244 // no desired position for the item, lets calculate a good position
1245 if (this.grid == null) {
1246 // building the structure to see current gaps
1247 this.grid = FolderGrid.build_grid_structure (this.manager.get_view (), this.manager.get_settings ().arrangement_padding);
1248 // grid.print ();
1249 }
1250 Gdk.Point pos = grid.get_next_gap (this.manager.get_view (), is, this.manager.get_settings ().arrangement_padding, this.manager.is_vertical_arragement ());
1251 is.x = pos.x;
1252 is.y = pos.y;
1253 } else {
1254 is.x = x;
1255 is.y = y;
1256 }
1257 this.mutex.unlock ();
1258
1259 is.name = pending.file_name;
1260 this.manager.get_settings ().add_item (is);
1261 ItemManager item = new ItemManager (pending.file_name, pending.file, this.manager);
1262 this.manager.items.append (item);
1263 this.manager.get_view ().add_item (item.get_view (), is.x, is.y);
1264 }
1265
1266 index++;
1267 }
1268
1269 // checking if we need to continue processing items in the idle thread
1270 if (this.flag_restart || this.pending_items_to_process.size == 0) {
1271 this.manager.get_settings ().save ();
1272 this.manager.get_view ().refresh ();
1273 if (this.manager.get_arrangement ().force_organization ()) {
1274 this.manager.organize_panel_items ();
1275 }
1276 this.manager.get_view ().hide_loading ();
1277 this.flag_running = false;
1278 this.manager.on_sync_finished ();
1279
1280 // debug ("finished drawing");
1281 debug (">>>>>>>>>>> END _sync_files for Panel: %s", this.manager.get_folder_name ());
1282 return false;
1283 } else {
1284 // this.manager.get_view ().refresh ();
1285 // debug ("lets continue drawing in the future");
1286 return true;
1287 }
1288 });
1289 } else {
1290 // nothing to process, the sync algorithm is finished
1291 this.manager.get_view ().hide_loading ();
1292 this.flag_running = false;
1293 this.manager.on_sync_finished ();
1294 debug (">>>>>>>>>>> END _sync_files for Panel: %s", this.manager.get_folder_name ());
1295 }
1296 }
1297
1298 return true;
1299 }
1300
1301 /**
1302 * @name pop_item_from_list
1303 * @description try to ind an item in a item list by the file get_name
1304 * @param string file_name the file name of the item
1305 * @param List<ItemManager> the list to search inside (reference)
1306 * @return ItemManager the itemmanager found, or null if none match
1307 */
1308 private ItemManager ? pop_item_from_list (string file_name, ref List <ItemManager> items) {
1309 foreach (var item in items) {
1310 if (item.get_file_name () == file_name) {
1311 items.remove (item);
1312 return item;
1313 }
1314 }
1315 return null;
1316 }
1317
1318 }