-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathAbstractDirectoryView.vala
More file actions
4019 lines (3363 loc) · 163 KB
/
AbstractDirectoryView.vala
File metadata and controls
4019 lines (3363 loc) · 163 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2015-2020 elementary, Inc. (https://elementary.io)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: Jeremy Wootten <jeremywootten@gmail.com>
*/
/* Implementations of AbstractDirectoryView are
* IconView
* ListView
* ColumnView
*/
namespace Files {
public abstract class AbstractDirectoryView : Gtk.Bin {
//TODO Reorder property declarations
protected enum ClickZone {
EXPANDER,
HELPER,
ICON,
NAME,
BLANK_PATH,
BLANK_NO_PATH,
INVALID
}
const int MAX_TEMPLATES = 2048;
const Gtk.TargetEntry [] DRAG_TARGETS = {
{"text/plain", Gtk.TargetFlags.SAME_APP, Files.TargetType.STRING},
{"text/plain", Gtk.TargetFlags.OTHER_APP, Files.TargetType.STRING},
{"text/uri-list", Gtk.TargetFlags.SAME_APP, Files.TargetType.TEXT_URI_LIST},
{"text/uri-list", Gtk.TargetFlags.OTHER_APP, Files.TargetType.TEXT_URI_LIST}
};
const Gtk.TargetEntry [] DROP_TARGETS = {
{"text/uri-list", Gtk.TargetFlags.SAME_APP, Files.TargetType.TEXT_URI_LIST},
{"text/uri-list", Gtk.TargetFlags.OTHER_APP, Files.TargetType.TEXT_URI_LIST},
{"XdndDirectSave0", Gtk.TargetFlags.OTHER_APP, Files.TargetType.XDND_DIRECT_SAVE0},
{"_NETSCAPE_URL", Gtk.TargetFlags.OTHER_APP, Files.TargetType.NETSCAPE_URL}
};
const Gdk.DragAction FILE_DRAG_ACTIONS = (Gdk.DragAction.COPY | Gdk.DragAction.MOVE | Gdk.DragAction.LINK);
/* Menu Handling */
const GLib.ActionEntry [] SELECTION_ENTRIES = {
{"open", on_selection_action_open_executable},
{"open-with-app", on_selection_action_open_with_app, "u"},
{"open-with-default", on_selection_action_open_with_default},
{"open-with-other-app", on_selection_action_open_with_other_app},
{"rename", on_selection_action_rename},
{"view-in-location", on_selection_action_view_in_location},
{"forget", on_selection_action_forget},
{"cut", on_selection_action_cut},
{"trash", on_selection_action_trash},
{"delete", on_selection_action_delete},
{"restore", on_selection_action_restore},
{"invert-selection", invert_selection}
};
const GLib.ActionEntry [] BACKGROUND_ENTRIES = {
{"new", on_background_action_new, "s"},
{"create-from", on_background_action_create_from, "s"},
{"sort-by", on_background_action_sort_by_changed, "s", "'name'"},
{"reverse", on_background_action_reverse_changed, null, "false"}
};
const GLib.ActionEntry [] COMMON_ENTRIES = {
{"copy", on_common_action_copy},
{"paste-into", on_common_action_paste_into}, // Paste into selected folder
{"paste", on_common_action_paste}, // Paste into background folder
{"open-in", on_common_action_open_in, "i"},
{"bookmark", on_common_action_bookmark},
{"properties", on_common_action_properties},
{"copy-link", on_common_action_copy_link},
{"select-all", toggle_select_all},
{"set-wallpaper", action_set_wallpaper}
};
GLib.SimpleActionGroup common_actions;
GLib.SimpleActionGroup selection_actions;
GLib.SimpleActionGroup background_actions;
private ZoomLevel _zoom_level = ZoomLevel.NORMAL;
public ZoomLevel zoom_level {
get {
return _zoom_level;
}
set {
if (value > maximum_zoom) {
_zoom_level = maximum_zoom;
} else if (value < minimum_zoom) {
_zoom_level = minimum_zoom;
} else {
_zoom_level = value;
}
on_zoom_level_changed (_zoom_level);
}
}
public int icon_size {
get {
return _zoom_level.to_icon_size ();
}
}
protected ZoomLevel minimum_zoom = ZoomLevel.SMALLEST;
protected ZoomLevel maximum_zoom = ZoomLevel.LARGEST;
/* Used only when acting as drag source */
double drag_x = 0;
double drag_y = 0;
protected GLib.List<Files.File> source_drag_file_list = null;
protected Gdk.Atom current_target_type = Gdk.Atom.NONE;
/* Used only when acting as drag destination */
uint drag_scroll_timer_id = 0;
uint drag_enter_timer_id = 0;
private bool destination_data_ready = false; /* whether the drop data was received already */
private bool drop_occurred = false; /* whether the data was dropped */
Files.File? drop_target_file = null;
private GLib.List<GLib.File> destination_drop_file_list = null; /* the list of URIs that are contained in the drop data */
Gdk.DragAction current_suggested_action = Gdk.DragAction.DEFAULT;
Gdk.DragAction current_actions = Gdk.DragAction.DEFAULT;
bool _drop_highlight;
bool drop_highlight {
get {
return _drop_highlight;
}
set {
if (value != _drop_highlight) {
if (value) {
Gtk.drag_highlight (this);
} else {
Gtk.drag_unhighlight (this);
}
}
_drop_highlight = value;
}
}
/* Used for blocking and unblocking DnD */
protected bool dnd_disabled = false;
/* Suppress native behavior when required */
private bool button_press_disabled = false;
private void* drag_data;
/* support for generating thumbnails */
int thumbnail_request = -1;
uint thumbnail_source_id = 0;
uint freeze_source_id = 0;
Thumbnailer thumbnailer = null;
/* Free space signal support */
uint add_remove_file_timeout_id = 0;
bool signal_free_space_change = false;
/* Rename support */
protected Files.TextRenderer? name_renderer = null;
public string original_name = "";
public string proposed_name = "";
/* Support for zoom by smooth scrolling */
private double total_delta_y = 0.0;
/* Support for keeping cursor position after delete */
private Gtk.TreePath deleted_path;
/* UI options for button press handling */
protected bool right_margin_unselects_all = false;
protected bool on_directory = false;
protected bool one_or_less = true;
protected bool should_activate = false;
protected bool should_deselect = false;
protected bool should_thumbnail = true;
public bool singleclick_select { get; set; }
protected bool should_select = false;
protected Gtk.TreePath? click_path = null;
protected uint click_zone = ClickZone.ICON;
protected uint previous_click_zone = ClickZone.ICON;
/* Cursors for different areas */
private Gdk.Cursor editable_cursor;
private Gdk.Cursor activatable_cursor;
private Gdk.Cursor selectable_cursor;
private GLib.List<GLib.AppInfo> open_with_apps;
/* Selected files are originally obtained with
gtk_tree_model_get(): this function increases the reference
count of the file object.*/
protected GLib.List<Files.File> selected_files = null;
private bool selected_files_invalid = true;
private GLib.AppInfo default_app;
private Gtk.TreePath? hover_path = null;
public bool renaming {get; protected set; default = false;}
private bool _is_frozen = false;
public bool is_frozen {
set {
if (is_frozen != value) {
_is_frozen = value;
if (value) {
action_set_enabled (selection_actions, "cut", false);
action_set_enabled (common_actions, "copy", false);
action_set_enabled (common_actions, "paste-into", false);
action_set_enabled (common_actions, "paste", false);
/* Fix problems when navigating away from directory with large number
* of selected files (e.g. OverlayBar critical errors)
*/
disconnect_tree_signals ();
clipboard.changed.disconnect (on_clipboard_changed);
} else {
clipboard.changed.connect (on_clipboard_changed);
connect_tree_signals ();
update_menu_actions ();
}
key_controller.propagation_phase = value ? Gtk.PropagationPhase.NONE : Gtk.PropagationPhase.BUBBLE;
}
}
get {
return _is_frozen;
}
}
public bool in_recent { get; private set; default = false; }
protected bool tree_frozen { get; set; default = false; }
private bool in_trash = false;
private bool in_network_root = false;
protected bool is_writable = false;
protected bool is_loading;
protected bool helpers_shown;
private bool all_selected = false;
private Gtk.Widget view;
protected Gtk.ScrolledWindow scrolled_window;
private Gtk.Label empty_label;
private Gtk.Overlay overlay;
private unowned ClipboardManager clipboard;
protected Files.ListModel model;
protected Files.IconRenderer icon_renderer;
protected unowned View.Slot slot; // Must be unowned else cyclic reference stops destruction
protected unowned View.Window? window {
get {
return slot.ctab.window;
}
}
protected static DndHandler dnd_handler = new DndHandler ();
protected unowned Gtk.RecentManager recent;
protected Gtk.EventControllerKey key_controller;
protected Gtk.GestureMultiPress button_controller;
protected Gtk.EventControllerScroll scroll_controller;
protected Gtk.EventControllerMotion motion_controller;
public signal void path_change_request (GLib.File location, Files.OpenFlag flag, bool new_root);
public signal void selection_changed (GLib.List<Files.File> gof_file);
private static Settings app_settings;
//TODO Rewrite in Object (), construct {} style
protected AbstractDirectoryView (View.Slot _slot) {
slot = _slot;
editable_cursor = new Gdk.Cursor.from_name (Gdk.Display.get_default (), "text");
activatable_cursor = new Gdk.Cursor.from_name (Gdk.Display.get_default (), "pointer");
selectable_cursor = new Gdk.Cursor.from_name (Gdk.Display.get_default (), "default");
scrolled_window = new Gtk.ScrolledWindow (null, null) {
kinetic_scrolling = true,
overlay_scrolling = true,
window_placement = TOP_LEFT,
shadow_type = NONE
};
empty_label = new Gtk.Label (slot.get_empty_message ()) {
halign = CENTER,
valign = CENTER,
hexpand = false,
vexpand = false,
wrap = true
};
empty_label.get_style_context ().add_class (Granite.STYLE_CLASS_H2_LABEL);
empty_label.no_show_all = true;
overlay = new Gtk.Overlay () {
hexpand = true,
vexpand = true,
child = scrolled_window
};
overlay.add_overlay (empty_label);
overlay.set_overlay_pass_through (empty_label, true);
overlay.add_events (Gdk.EventMask.ALL_EVENTS_MASK);
child = overlay;
var app = (Files.Application)(GLib.Application.get_default ());
clipboard = app.get_clipboard_manager ();
recent = app.get_recent_manager ();
app.set_accels_for_action ("common.select-all", {"<Ctrl>A"});
app.set_accels_for_action ("selection.invert-selection", {"<Shift><Ctrl>A"});
thumbnailer = Thumbnailer.get ();
thumbnailer.finished.connect ((req) => {
if (req == thumbnail_request) {
thumbnail_request = -1;
}
draw_when_idle ();
});
model = new Files.ListModel ();
/* Currently, "single-click rename" is disabled, matching existing UI
* Currently, "right margin unselects all" is disabled, matching existing UI
*/
set_up__menu_actions ();
set_up_directory_view ();
view = create_view ();
if (view != null) {
scrolled_window.child = view;
connect_drag_drop_signals (view);
view.realize.connect (() => {
schedule_thumbnail_color_tag_timeout ();
});
scroll_controller = new Gtk.EventControllerScroll (view, NONE) {
propagation_phase = CAPTURE
};
scroll_controller.scroll.connect (on_scroll_event);
enable_scroll (true);
key_controller = new Gtk.EventControllerKey (view) {
propagation_phase = BUBBLE
};
key_controller.key_pressed.connect (on_view_key_press_event);
// Workaround for scroll events getting consumed by scroll controller
// Only handle scroll events when a key is pressed (for zooming) or when frozen/renaming, otherwise
// they will be handled by the native widget
key_controller.key_pressed.connect (() => {
if (!is_frozen && !renaming) {
scroll_controller.flags = VERTICAL;
}
return false;
});
key_controller.key_released.connect (() => {
if (!is_frozen && !renaming) {
scroll_controller.flags = NONE;
}
});
// Hack required to suppress native behaviour when dragging
// multiple selected items with GestureMultiPress event controller
// Native behaviour deselects items except the one clicked on
view.button_press_event.connect (() => {
return button_press_disabled;
});
button_controller = new Gtk.GestureMultiPress (view) {
propagation_phase = TARGET, //Allow editable widget to receive button press event first
button = 0
};
button_controller.pressed.connect (on_view_button_press_event);
button_controller.released.connect (on_view_button_release_event);
motion_controller = new Gtk.EventControllerMotion (view) {
propagation_phase = CAPTURE
};
motion_controller.motion.connect (on_motion_notify_event);
motion_controller.leave.connect (on_leave_notify_event);
}
freeze_tree (); /* speed up loading of icon view. Thawed when directory loaded */
set_up_zoom_level ();
connect_directory_handlers (slot.directory);
}
static construct {
app_settings = new Settings ("io.elementary.files.preferences");
}
~AbstractDirectoryView () {
debug ("ADV destruct"); // Cannot reference slot here as it is already invalid
}
protected void set_up_name_renderer () {
name_renderer.editable = false;
name_renderer.edited.connect (on_name_edited);
name_renderer.editing_canceled.connect (on_name_editing_canceled);
name_renderer.editing_started.connect (on_name_editing_started);
}
private void set_up_directory_view () {
popup_menu.connect (on_popup_menu);
unrealize.connect (() => {
clipboard.changed.disconnect (on_clipboard_changed);
});
realize.connect (() => {
clipboard.changed.connect (on_clipboard_changed);
on_clipboard_changed ();
});
scrolled_window.get_vadjustment ().value_changed.connect_after (() => {
schedule_thumbnail_color_tag_timeout ();
});
notify["renaming"].connect (() => {
// Suppress ability to scroll with the scrollbar while renaming
// No obvious way to disable it so just hide it
var vscroll_bar = scrolled_window.get_vscrollbar ();
vscroll_bar.visible = !renaming;
});
var prefs = (Files.Preferences.get_default ());
prefs.notify["show-hidden-files"].connect (on_show_hidden_files_changed);
prefs.notify["date-format"].connect (on_dateformat_changed);
app_settings.bind ("singleclick-select", this, "singleclick_select", SettingsBindFlags.DEFAULT);
app_settings.changed["show-remote-thumbnails"].connect (on_show_thumbnails_changed);
app_settings.changed["show-local-thumbnails"].connect (on_show_thumbnails_changed);
app_settings.changed["sort-directories-first"].connect (on_sort_directories_first_changed);
model.set_should_sort_directories_first (app_settings.get_boolean ("sort-directories-first"));
model.row_deleted.connect (on_row_deleted);
/* Sort order of model is set after loading */
model.sort_column_changed.connect (on_sort_column_changed);
}
private void set_up__menu_actions () {
selection_actions = new GLib.SimpleActionGroup ();
selection_actions.add_action_entries (SELECTION_ENTRIES, this);
insert_action_group ("selection", selection_actions);
background_actions = new GLib.SimpleActionGroup ();
background_actions.add_action_entries (BACKGROUND_ENTRIES, this);
insert_action_group ("background", background_actions);
common_actions = new GLib.SimpleActionGroup ();
common_actions.add_action_entries (COMMON_ENTRIES, this);
insert_action_group ("common", common_actions);
}
public void zoom_in () {
zoom_level = zoom_level + 1;
}
public void zoom_out () {
if (zoom_level > 0) {
zoom_level = zoom_level - 1;
}
}
public void zoom_normal () {
var view_settings = get_view_settings ();
if (view_settings == null) {
zoom_level = ZoomLevel.NORMAL;
} else {
zoom_level = (ZoomLevel)view_settings.get_enum ("default-zoom-level"); // syncs to settings
}
}
private uint set_cursor_timeout_id = 0;
public void focus_first_for_empty_selection (bool select) {
if (selected_files == null) {
set_cursor_timeout_id = Idle.add_full (GLib.Priority.LOW, () => {
if (!tree_frozen) {
set_cursor_timeout_id = 0;
set_cursor (new Gtk.TreePath.from_indices (0), false, select, true);
return GLib.Source.REMOVE;
} else {
return GLib.Source.CONTINUE;
}
});
}
}
/* This function is only called by Slot in order to select a file item after loading has completed.
* If called before initial loading is complete then tree_frozen is true. Otherwise, e.g. when selecting search items
* tree_frozen is false.
*/
private ulong select_source_handler = 0;
public void select_glib_files_when_thawed (GLib.List<GLib.File> location_list, GLib.File? focus_location) {
var files_to_select_list = new Gee.LinkedList<Files.File> ();
location_list.@foreach ((loc) => {
files_to_select_list.add (Files.File.@get (loc));
});
GLib.File? focus_after_select = focus_location != null ? focus_location.dup () : null;
/* Because the Icon View disconnects the model while loading, we need to wait until
* the tree is thawed and the model reconnected before selecting the files.
* Using a timeout helps ensure that the files appear in the model before selecting. Using an Idle
* sometimes results in the pasted file not being selected because it is not found yet in the model. */
if (tree_frozen) {
select_source_handler = notify["tree-frozen"].connect (() => {
select_files_and_update_if_thawed (files_to_select_list, focus_after_select);
});
} else {
select_files_and_update_if_thawed (files_to_select_list, focus_after_select);
}
}
private void select_files_and_update_if_thawed (Gee.LinkedList<Files.File> files_to_select,
GLib.File? focus_file) {
if (tree_frozen) {
return;
}
// Ensure focus file not overridden later
if (set_cursor_timeout_id > 0) {
Source.remove (set_cursor_timeout_id);
set_cursor_timeout_id = 0;
}
if (select_source_handler > 0) {
disconnect (select_source_handler);
select_source_handler = 0;
}
disconnect_tree_signals (); /* Avoid unnecessary signal processing */
unselect_all ();
uint count = 0;
Gtk.TreeIter? iter;
foreach (Files.File f in files_to_select) {
/* Not all files selected in previous view (e.g. expanded tree view) may appear in this one. */
var path = model.get_path_for_first_file (f);
if (path != null) {
count++;
/* Cursor follows if matches focus location*/
select_path (path, focus_file != null && focus_file.equal (f.location));
}
}
if (count == 0) {
focus_first_for_empty_selection (false);
}
connect_tree_signals ();
on_view_selection_changed (); /* Mark selected_file list as invalid */
/* Update menu and selected file list now in case autoselected */
update_selected_files_and_menu ();
}
public unowned GLib.List<GLib.AppInfo> get_open_with_apps () {
return open_with_apps;
}
public GLib.AppInfo get_default_app () {
return default_app;
}
public new void grab_focus () {
if (view.get_realized ()) {
/* In Column View, maybe clicked on an inactive column */
if (!slot.is_active) {
set_active_slot ();
}
view.grab_focus ();
}
}
public unowned GLib.List<Files.File> get_selected_files () {
update_selected_files_and_menu ();
return selected_files;
}
/*** Protected Methods */
protected void set_active_slot (bool scroll = true) {
slot.active (scroll);
}
protected void load_location (GLib.File location) {
path_change_request (location, Files.OpenFlag.DEFAULT, false);
}
protected void load_root_location (GLib.File location) {
path_change_request (location, Files.OpenFlag.DEFAULT, true);
}
/** Operations on selections */
protected void activate_selected_items (Files.OpenFlag flag = Files.OpenFlag.DEFAULT,
GLib.List<Files.File> selection = get_selected_files ()) {
if (is_frozen || selection == null) {
return;
}
unowned Gdk.Screen screen = get_screen ();
if (selection.first ().next == null) { // Only one selected
activate_file (selection.data, screen, flag, true);
return;
}
if (!in_trash) {
/* launch each selected file individually ignoring selections greater than 10
* Do not launch with new instances of this app - open according to flag instead
*/
if (selection.nth_data (11) == null && // Less than 10 items
(default_app == null || app_is_this_app (default_app))) {
foreach (Files.File file in selection) {
/* Prevent too rapid activation of files - causes New Tab to crash for example */
if (file.is_folder ()) {
/* By default, multiple folders open in new tabs */
if (flag == Files.OpenFlag.DEFAULT) {
flag = Files.OpenFlag.NEW_TAB;
}
GLib.Idle.add (() => {
activate_file (file, screen, flag, false);
return GLib.Source.REMOVE;
});
} else {
GLib.Idle.add (() => {
open_file (file, screen, null);
return GLib.Source.REMOVE;
});
}
}
} else if (default_app != null) {
/* Because this is in another thread we need to copy the selection to ensure it remains valid */
var files_to_open = selection.copy_deep ((GLib.CopyFunc)(GLib.Object.ref));
GLib.Idle.add (() => {
open_files_with (default_app, files_to_open);
return GLib.Source.REMOVE;
});
}
} else {
warning ("Cannot open files in trash");
}
}
public void select_gof_file (Files.File file) {
var path = model.get_path_for_first_file (file);
set_cursor (path, false, true, false);
}
protected void select_and_scroll_to_gof_file (Files.File file) {
var path = model.get_path_for_first_file (file);
set_cursor (path, false, true, true);
}
protected void add_gof_file_to_selection (Files.File file) {
select_path (model.get_path_for_first_file (file)); /* Cursor does not follow */
}
/** Directory signal handlers. */
/* Signal could be from subdirectory as well as slot directory */
protected void connect_directory_handlers (Directory dir) {
dir.file_added.connect (on_directory_file_added);
dir.file_changed.connect (on_directory_file_changed);
dir.file_deleted.connect (on_directory_file_deleted);
dir.icon_changed.connect (on_directory_file_icon_changed);
connect_directory_loading_handlers (dir);
}
protected void connect_directory_loading_handlers (Directory dir) {
model.set_sorting_off ();
dir.file_loaded.connect (on_directory_file_loaded);
dir.done_loading.connect (on_directory_done_loading);
}
protected void disconnect_directory_loading_handlers (Directory dir) {
model.set_sorting_on ();
dir.file_loaded.disconnect (on_directory_file_loaded);
dir.done_loading.disconnect (on_directory_done_loading);
}
protected void disconnect_directory_handlers (Directory dir) {
/* If the directory is still loading the file_loaded signal handler
/* will not have been disconnected */
if (dir.is_loading ()) {
disconnect_directory_loading_handlers (dir);
}
dir.file_added.disconnect (on_directory_file_added);
dir.file_changed.disconnect (on_directory_file_changed);
dir.file_deleted.disconnect (on_directory_file_deleted);
dir.icon_changed.disconnect (on_directory_file_icon_changed);
dir.done_loading.disconnect (on_directory_done_loading);
}
public void change_directory (Directory old_dir, Directory new_dir) {
var style_context = get_style_context ();
if (style_context.has_class (Granite.STYLE_CLASS_H2_LABEL)) {
style_context.remove_class (Granite.STYLE_CLASS_H2_LABEL);
style_context.remove_class (Gtk.STYLE_CLASS_VIEW);
}
cancel ();
clear ();
disconnect_directory_handlers (old_dir);
connect_directory_handlers (new_dir);
}
public void prepare_reload (Directory dir) {
cancel ();
clear ();
connect_directory_loading_handlers (dir);
}
private void clear () {
/* after calling this (prior to reloading), the directory must be re-initialised so
* we reconnect the file_loaded and done_loading signals */
freeze_tree ();
block_model ();
model.clear ();
all_selected = false;
/* Prevent unexpected file activation after navigation with double-click in mixed mode */
on_directory = false;
unblock_model ();
}
protected void connect_drag_drop_signals (Gtk.Widget widget) {
/* Set up as drop site */
Gtk.drag_dest_set (widget, Gtk.DestDefaults.MOTION, DROP_TARGETS, Gdk.DragAction.ASK | FILE_DRAG_ACTIONS);
widget.drag_drop.connect (on_drag_drop);
widget.drag_data_received.connect (on_drag_data_received);
widget.drag_leave.connect (on_drag_leave);
widget.drag_motion.connect (on_drag_motion);
/* Set up as drag source */
Gtk.drag_source_set (
widget,
Gdk.ModifierType.BUTTON1_MASK | Gdk.ModifierType.CONTROL_MASK,
DRAG_TARGETS,
FILE_DRAG_ACTIONS
);
widget.drag_begin.connect (on_drag_begin);
widget.drag_data_get.connect (on_drag_data_get);
widget.drag_data_delete.connect (on_drag_data_delete);
widget.drag_end.connect (on_drag_end);
}
protected void cancel_thumbnailing () {
if (thumbnail_request >= 0) {
thumbnailer.dequeue (thumbnail_request);
thumbnail_request = -1;
}
cancel_timeout (ref thumbnail_source_id);
}
protected bool selection_only_contains_folders (GLib.List<Files.File> list) {
bool only_folders = true;
list.@foreach ((file) => {
if (!(file.is_folder () || file.is_root_network_folder ())) {
only_folders = false;
}
});
return only_folders;
}
protected GLib.List<Files.File>
get_selected_files_for_transfer (GLib.List<Files.File> selection = get_selected_files ()) {
return selection.copy_deep ((GLib.CopyFunc) GLib.Object.ref);
}
/*** Private methods */
/** File operations */
private void activate_file (Files.File _file, Gdk.Screen? screen, Files.OpenFlag flag, bool only_one_file) {
if (is_frozen) {
return;
}
Files.File file = _file;
if (in_recent) {
file = Files.File.get_by_uri (file.get_display_target_uri ());
}
default_app = MimeActions.get_default_application_for_file (file);
GLib.File location = file.get_target_location ();
if (screen == null) {
screen = get_screen ();
}
if (flag != Files.OpenFlag.APP && (file.is_folder () ||
file.get_ftype () == "inode/directory" ||
file.is_root_network_folder ())) {
switch (flag) {
case Files.OpenFlag.NEW_TAB:
case Files.OpenFlag.NEW_WINDOW:
path_change_request (location, flag, true);
break;
default:
if (only_one_file) {
load_location (location);
}
break;
}
} else if (!in_trash) {
if (only_one_file) {
if (file.is_executable ()) {
var content_type = file.get_ftype ();
if (GLib.ContentType.is_a (content_type, "text/plain")) {
open_file (file, screen, default_app);
} else {
try {
file.execute (null);
} catch (Error e) {
PF.Dialogs.show_warning_dialog (_("Cannot execute this file"), e.message, window);
}
}
} else {
open_file (file, screen, default_app);
}
}
} else {
PF.Dialogs.show_error_dialog (
///TRANSLATORS: '%s' is a quoted placehorder for the name of a file. It can be moved but not omitted
_("“%s” must be moved from Trash before opening").printf (file.basename),
_("Files inside Trash cannot be opened. To open this file, it must be moved elsewhere."),
window
);
}
}
/* Open all files through this */
private void open_file (Files.File file, Gdk.Screen? screen, GLib.AppInfo? app_info) {
if (can_open_file (file, true)) {
MimeActions.open_glib_file_request.begin (file.location, this, app_info);
}
}
/* Also used by build open menu */
private bool can_open_file (Files.File file, bool show_error_dialog = false) {
string err_msg1 = _("Cannot open this file");
string err_msg2 = "";
var content_type = file.get_ftype ();
if (content_type == null) {
bool result_uncertain = true;
content_type = ContentType.guess (file.basename, null, out result_uncertain);
debug ("Guessed content type to be %s from name - result_uncertain %s",
content_type,
result_uncertain.to_string ());
}
if (content_type == null) {
err_msg2 = _("Cannot identify file type to open");
} else if (!slot.directory.can_open_files) {
err_msg2 = "Cannot open files with this protocol (%s)".printf (slot.directory.scheme);
} else if (!slot.directory.can_stream_files &&
(content_type.contains ("video") || content_type.contains ("audio"))) {
err_msg2 = "Cannot stream from this protocol (%s)".printf (slot.directory.scheme);
}
bool success = err_msg2.length < 1;
if (!success && show_error_dialog) {
PF.Dialogs.show_warning_dialog (err_msg1, err_msg2, window);
}
return success;
}
private void trash_or_delete_files (GLib.List<Files.File> file_list,
bool delete_if_already_in_trash,
bool delete_immediately) {
GLib.List<GLib.File> locations = null;
if (in_recent) {
file_list.@foreach ((file) => {
locations.prepend (GLib.File.new_for_uri (file.get_display_target_uri ()));
});
} else {
file_list.@foreach ((file) => {
locations.prepend (file.location);
});
}
deleted_path = model.get_path_for_first_file (file_list.first ().data);
if (locations != null) {
locations.reverse ();
slot.directory.block_monitor ();
FileOperations.@delete.begin (
locations,
window as Gtk.Window,
!delete_immediately,
null,
(obj, res) => {
try {
FileOperations.@delete.end (res);
} catch (Error e) {
debug (e.message);
}
after_trash_or_delete ();
}
);
}
/* If in recent "folder" we need to refresh the view. */
if (in_recent) {
slot.reload ();
}
}
// Only called after initial loading finished, in response to files added due to internal or external
// file operations
private void add_file (Files.File file, Directory dir, bool is_internal = true) {
empty_label.visible = false;
model.insert_sorted (file, dir);
if (is_internal) { /* This true once view finished loading */
// Do not select until the model has resorted else wrong file is selected
ulong model_resorted = 0;
model_resorted = model.rows_reordered.connect (() => {
model.disconnect (model_resorted);
add_gof_file_to_selection (file);
});
}
}
private void handle_free_space_change () requires (window != null) {
/* Wait at least 250 mS after last space change before signalling to avoid unnecessary updates*/
if (add_remove_file_timeout_id == 0) {
signal_free_space_change = false;
add_remove_file_timeout_id = GLib.Timeout.add (250, () => {
if (signal_free_space_change) {
add_remove_file_timeout_id = 0;
window.free_space_change ();
return GLib.Source.REMOVE;
} else {
signal_free_space_change = true;
return GLib.Source.CONTINUE;
}
});
} else {
signal_free_space_change = false;
}
}
private void new_empty_file (string? parent_uri = null) {
if (parent_uri == null) {
parent_uri = slot.directory.file.uri;
}
/* Block the async directory file monitor to avoid generating unwanted "add-file" events */
slot.directory.block_monitor ();
FileOperations.new_file.begin (
this,
parent_uri,
null,
null,
0,
null,
(obj, res) => {