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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
|
/*
* Copyright (c) 2011 Marlin Developers (http://launchpad.net/marlin)
* Copyright (c) 2015-2017 elementary LLC (http://launchpad.net/pantheon-files)
*
* 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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authored by: ammonkey <am.monkeyd@gmail.com>
*/
namespace Marlin.View {
public class PropertiesWindow : AbstractPropertiesDialog {
private Gtk.Entry perm_code;
private bool perm_code_should_update = true;
private Gtk.Label l_perm;
private PermissionButton perm_button_user;
private PermissionButton perm_button_group;
private PermissionButton perm_button_other;
private Gtk.ListStore store_users;
private Gtk.ListStore store_groups;
private Gtk.ListStore store_apps;
private uint count;
private GLib.List<GOF.File> files;
private GOF.File goffile;
public FM.AbstractDirectoryView view {get; private set;}
public Gtk.Entry entry {get; private set;}
private string original_name {
get {
return view.original_name;
}
set {
view.original_name = value;
}
}
private string proposed_name {
get {
return view.proposed_name;
}
set {
view.proposed_name = value;
}
}
private Mutex mutex;
private GLib.List<Marlin.DeepCount>? deep_count_directories = null;
private Gee.Set<string>? mimes;
private ValueLabel contains_value;
private ValueLabel resolution_value;
private ValueLabel size_value;
private ValueLabel type_value;
private KeyLabel contains_key_label;
private KeyLabel type_key_label;
private string ftype; /* common type */
private Gtk.Spinner spinner;
private int size_warning = 0;
private uint64 total_size = 0;
private uint timeout_perm = 0;
private GLib.Cancellable? cancellable;
private bool files_contain_a_directory;
private uint _uncounted_folders = 0;
private uint selected_folders = 0;
private uint selected_files = 0;
private signal void uncounted_folders_changed ();
private Gtk.Grid perm_grid;
private int owner_perm_code = 0;
private int group_perm_code = 0;
private int everyone_perm_code = 0;
private enum AppsColumn {
APP_INFO,
LABEL,
ICON
}
private Posix.mode_t[,] vfs_perms = {
{ Posix.S_IRUSR, Posix.S_IWUSR, Posix.S_IXUSR },
{ Posix.S_IRGRP, Posix.S_IWGRP, Posix.S_IXGRP },
{ Posix.S_IROTH, Posix.S_IWOTH, Posix.S_IXOTH }
};
private uint uncounted_folders {
get {
return _uncounted_folders;
}
set {
_uncounted_folders = value;
uncounted_folders_changed ();
}
}
private uint folder_count = 0; /* Count of folders current NOT including top level (selected) folders (to match OverlayBar)*/
private uint file_count; /* Count of files current including top level (selected) files other than folders */
public PropertiesWindow (GLib.List<GOF.File> _files, FM.AbstractDirectoryView _view, Gtk.Window parent) {
base (_("Properties"), parent);
if (_files == null) {
critical ("Properties Window constructor called with null file list");
return;
}
if (_view == null) {
critical ("Properties Window constructor called with null Directory View");
return;
}
view = _view;
/* Connect signal before creating any DeepCount directories */
this.destroy.connect (() => {
foreach (var dir in deep_count_directories)
dir.cancel ();
});
/* The properties window may outlive the passed-in file object
lifetimes. The objects must be referenced as a precaution.
GLib.List.copy() would not guarantee valid references: because it
does a shallow copy (copying the pointer values only) the objects'
memory may be freed even while this code is using it. */
foreach (GOF.File file in _files) {
/* prepend(G) is declared "owned G", so ref() will be called once
on the unowned foreach value. */
files.prepend (file);
}
count = files.length();
if (count < 1 ) {
critical ("Properties Window constructor called with empty file list");
return;
}
if (!(files.data is GOF.File)) {
critical ("Properties Window constructor called with invalid file data (1)");
return;
}
mimes = new Gee.HashSet<string> ();
foreach (var gof in files) {
if (!(gof is GOF.File)) {
critical ("Properties Window constructor called with invalid file data (2)");
return;
}
var ftype = gof.get_ftype ();
if (ftype != null) {
mimes.add (ftype);
}
if (gof.is_directory) {
files_contain_a_directory = true;
}
}
goffile = (GOF.File) files.data;
construct_info_panel (goffile);
cancellable = new GLib.Cancellable ();
update_selection_size (); /* Start counting first to get number of selected files and folders */
/* create some widgets first (may be hidden by update_selection_size ()) */
var file_pix = goffile.get_icon_pixbuf (48, false, GOF.FileIconFlags.NONE);
var file_icon = new Gtk.Image.from_pixbuf (file_pix);
overlay_emblems (file_icon, goffile.emblems_list);
/* Build header box */
if (count > 1 || (count == 1 && !goffile.is_writable ())) {
var label = new Gtk.Label (get_selected_label (selected_folders, selected_files));
label.halign = Gtk.Align.START;
header_title = label;
} else if (count == 1 && goffile.is_writable ()) {
entry = new Gtk.Entry ();
original_name = goffile.info.get_name ();
reset_entry_text ();
entry.activate.connect (() => {
rename_file (goffile, entry.get_text ());
});
entry.focus_out_event.connect (() => {
rename_file (goffile, entry.get_text ());
return false;
});
header_title = entry;
}
create_header_title ();
/* Permissions */
/* Don't show permissions for uri scheme trash and archives */
if (!(count == 1 && !goffile.location.is_native () && !goffile.is_remote_uri_scheme ())) {
construct_perm_panel ();
add_section (stack, _("Permissions"), PanelType.PERMISSIONS.to_string (), perm_grid);
if (!goffile.can_set_permissions ()) {
foreach (var widget in perm_grid.get_children ()) {
widget.set_sensitive (false);
}
}
}
show_all ();
}
private void update_size_value () {
size_value.label = format_size ((int64) total_size);
contains_value.label = get_contains_value (folder_count, file_count);
update_widgets_state ();
update_selection_usage (total_size);
if (size_warning > 0) {
var size_warning_image = new Gtk.Image.from_icon_name ("help-info-symbolic", Gtk.IconSize.MENU);
size_warning_image.halign = Gtk.Align.START;
size_warning_image.hexpand = true;
size_warning_image.tooltip_markup = "<b>" + _("Actual Size Could Be Larger") + "</b>" + "\n" + ngettext ("%i file could not be read due to permissions or other errors.", "%i files could not be read due to permissions or other errors.", (ulong) size_warning).printf (size_warning);
info_grid.attach_next_to (size_warning_image, size_value, Gtk.PositionType.RIGHT);
info_grid.show_all ();
}
}
private void update_selection_size () {
total_size = 0;
uncounted_folders = 0;
selected_folders = 0;
selected_files = 0;
folder_count = 0;
file_count = 0;
size_warning = 0;
deep_count_directories = null;
foreach (GOF.File gof in files) {
if (gof.is_root_network_folder ()) {
size_value.label = _("unknown");
continue;
}
if (gof.is_directory) {
mutex.lock ();
uncounted_folders++; /* this gets decremented by DeepCount*/
mutex.unlock ();
selected_folders++;
var d = new Marlin.DeepCount (gof.location); /* Starts counting on creation */
deep_count_directories.prepend (d);
d.finished.connect (() => {
mutex.lock ();
deep_count_directories.remove (d);
total_size += d.total_size;
size_warning = d.file_not_read;
if (file_count + uncounted_folders == size_warning)
size_value.label = _("unknown");
folder_count += d.dirs_count;
file_count += d.files_count;
uncounted_folders--; /* triggers signal which updates description when reaches zero */
mutex.unlock ();
});
} else {
selected_files++;
}
mutex.lock ();
total_size += PropertiesWindow.file_real_size (gof);
mutex.unlock ();
}
if (uncounted_folders > 0) {/* possible race condition - uncounted_folders could have been decremented? */
spinner.start ();
uncounted_folders_changed.connect (() => {
if (uncounted_folders == 0) {
spinner.hide ();
spinner.stop ();
update_size_value ();
}
});
} else {
update_size_value ();
}
}
private void rename_file (GOF.File file, string new_name) {
/* Only rename if name actually changed */
original_name = file.info.get_name ();
if (new_name != "") {
if (new_name != original_name) {
proposed_name = new_name;
view.set_file_display_name (file.location, new_name, after_rename);
}
} else {
reset_entry_text ();
}
}
private void after_rename (GLib.File original_file, GLib.File? new_location) {
if (new_location != null) {
reset_entry_text (new_location.get_basename ());
goffile = GOF.File.@get (new_location);
files.first ().data = goffile;
} else {
reset_entry_text (); //resets entry to old name
}
}
public void reset_entry_text (string? new_name = null) {
if (new_name != null) {
original_name = new_name;
}
entry.set_text (original_name);
}
private string? get_common_ftype () {
string? ftype = null;
if (files == null)
return null;
foreach (GOF.File gof in files) {
var gof_ftype = gof.get_ftype ();
if (ftype == null && gof != null) {
ftype = gof_ftype;
continue;
}
if (ftype != gof_ftype)
return null;
}
return ftype;
}
private bool got_common_location () {
File? loc = null;
foreach (GOF.File gof in files) {
if (loc == null && gof != null) {
if (gof.directory == null)
return false;
loc = gof.directory;
continue;
}
if (!loc.equal (gof.directory))
return false;
}
return true;
}
private GLib.File? get_parent_loc (string path) {
var loc = File.new_for_path (path);
return loc.get_parent ();
}
private string? get_common_trash_orig () {
File loc = null;
string path = null;
foreach (GOF.File gof in files) {
if (loc == null && gof != null) {
loc = get_parent_loc (gof.info.get_attribute_byte_string (FileAttribute.TRASH_ORIG_PATH));
continue;
}
if (gof != null && !loc.equal (get_parent_loc (gof.info.get_attribute_byte_string (FileAttribute.TRASH_ORIG_PATH))))
return null;
}
if (loc == null)
path = "/";
else
path = loc.get_parse_name();
return path;
}
private string filetype (GOF.File file) {
ftype = get_common_ftype ();
if (ftype != null) {
return ftype;
} else {
/* show list of mimetypes only if we got a default application in common */
if (view.get_default_app () != null && !goffile.is_directory) {
string str = null;
foreach (var mime in mimes) {
(str == null) ? str = mime : str = string.join (", ", str, mime);
}
return str;
}
}
return _("Unknown");
}
private string resolution (GOF.File file) {
/* get image size in pixels using an asynchronous method to stop the interface blocking on
* large images. */
if (file.width > 0) { /* resolution has already been determined */
return goffile.width.to_string () +" × " + goffile.height.to_string () + " px";
} else {
/* Async function will update info when resolution determined */
get_resolution.begin (file);
return _("Loading…");
}
}
private string location (GOF.File file) {
if (view.is_in_recent ()) {
string original_location = file.get_display_target_uri ().replace ("%20", " ");
string file_name = file.get_display_name ().replace ("%20", " ");
string location_folder = original_location.slice (0, -(file_name.length)).replace ("%20", " ");
string location_name = location_folder.slice (7, -1);
return "<a href=\"" + Markup.escape_text (location_folder) + "\">" + Markup.escape_text (location_name) + "</a>";
} else {
return "<a href=\"" + Markup.escape_text (file.directory.get_uri ()) + "\">" + Markup.escape_text (file.directory.get_parse_name ()) + "</a>";
}
}
private string original_location (GOF.File file) {
/* print orig location of trashed files */
if (file.info.get_attribute_byte_string (FileAttribute.TRASH_ORIG_PATH) != null) {
var trash_orig_loc = get_common_trash_orig ();
if (trash_orig_loc != null) {
return "<a href=\"" + get_parent_loc (file.info.get_attribute_byte_string (FileAttribute.TRASH_ORIG_PATH)).get_uri () + "\">" + trash_orig_loc + "</a>";
}
}
return _("Unknown");
}
private async void get_resolution (GOF.File goffile) {
GLib.FileInputStream? stream = null;
GLib.File file = goffile.location;
string resolution = _("Could not be determined");
try {
stream = yield file.read_async (0, cancellable);
if (stream == null) {
error ("Could not read image file's size data");
} else {
var pixbuf = yield new Gdk.Pixbuf.from_stream_async (stream, cancellable);
goffile.width = pixbuf.get_width ();
goffile.height = pixbuf.get_height ();
resolution = goffile.width.to_string () +" × " + goffile.height.to_string () + " px";
}
} catch (Error e) {
warning ("Error loading image resolution in PropertiesWindow: %s", e.message);
}
try {
stream.close ();
} catch (GLib.Error e) {
debug ("Error closing stream in get_resolution: %s", e.message);
}
resolution_value.label = resolution;
}
private void construct_info_panel (GOF.File file) {
/* Have to have these separate as size call is async */
var size_key_label = new KeyLabel (_("Size:"));
spinner = new Gtk.Spinner ();
spinner.halign = Gtk.Align.START;
size_value = new ValueLabel ("");
type_key_label = new KeyLabel (_("Type:"));
type_value = new ValueLabel ("");
contains_key_label = new KeyLabel (_("Contains:"));
contains_value = new ValueLabel ("");
/* Dialog may get displayed after these labels are hidden so we set no_show_all to true */
type_key_label.no_show_all = true;
type_value.no_show_all = true;
contains_key_label.no_show_all = true;
contains_value.no_show_all = true;
info_grid.attach (size_key_label, 0, 1, 1, 1);
info_grid.attach_next_to (spinner, size_key_label, Gtk.PositionType.RIGHT);
info_grid.attach_next_to (size_value, size_key_label, Gtk.PositionType.RIGHT);
info_grid.attach (type_key_label, 0, 2, 1, 1);
info_grid.attach_next_to (type_value, type_key_label, Gtk.PositionType.RIGHT, 3, 1);
info_grid.attach (contains_key_label, 0, 3, 1, 1);
info_grid.attach_next_to (contains_value, contains_key_label, Gtk.PositionType.RIGHT, 3, 1);
int n = 4;
if (count == 1) {
var time_created = PF.FileUtils.get_formatted_time_attribute_from_info (file.info, FileAttribute.TIME_CREATED);
if (time_created != "") {
var key_label = new KeyLabel (_("Created:"));
var value_label = new ValueLabel (time_created);
info_grid.attach (key_label, 0, n, 1, 1);
info_grid.attach_next_to (value_label, key_label, Gtk.PositionType.RIGHT, 3, 1);
n++;
}
var time_modified = PF.FileUtils.get_formatted_time_attribute_from_info (file.info, FileAttribute.TIME_MODIFIED);
if (time_modified != "") {
var key_label = new KeyLabel (_("Modified:"));
var value_label = new ValueLabel (time_modified);
info_grid.attach (key_label, 0, n, 1, 1);
info_grid.attach_next_to (value_label, key_label, Gtk.PositionType.RIGHT, 3, 1);
n++;
}
}
if (count == 1 && file.is_trashed ()) {
var deletion_date = PF.FileUtils.get_formatted_time_attribute_from_info (file.info, FileAttribute.TRASH_DELETION_DATE);
if (deletion_date != "") {
var key_label = new KeyLabel (_("Deleted:"));
var value_label = new ValueLabel (deletion_date);
info_grid.attach (key_label, 0, n, 1, 1);
info_grid.attach_next_to (value_label, key_label, Gtk.PositionType.RIGHT, 3, 1);
n++;
}
}
var ftype = filetype (file);
var mimetype_key = new KeyLabel (_("Mimetype:"));
var mimetype_value = new ValueLabel (ftype);
info_grid.attach (mimetype_key, 0, n, 1, 1);
info_grid.attach_next_to (mimetype_value, mimetype_key, Gtk.PositionType.RIGHT, 3, 1);
n++;
if (count == 1 && "image" in ftype) {
var resolution_key = new KeyLabel (_("Resolution:"));
resolution_value = new ValueLabel (resolution (file));
info_grid.attach (resolution_key, 0, n, 1, 1);
info_grid.attach_next_to (resolution_value, resolution_key, Gtk.PositionType.RIGHT, 3, 1);
n++;
}
if (got_common_location ()) {
var location_key = new KeyLabel (_("Location:"));
var location_value = new ValueLabel (location (file));
location_value.ellipsize = Pango.EllipsizeMode.MIDDLE;
location_value.max_width_chars = 32;
info_grid.attach (location_key, 0, n, 1, 1);
info_grid.attach_next_to (location_value, location_key, Gtk.PositionType.RIGHT, 3, 1);
n++;
}
if (count == 1 && file.info.get_is_symlink ()) {
var key_label = new KeyLabel (_("Target:"));
var value_label = new ValueLabel (file.info.get_symlink_target());
info_grid.attach (key_label, 0, n, 1, 1);
info_grid.attach_next_to (value_label, key_label, Gtk.PositionType.RIGHT, 3, 1);
n++;
}
if (file.is_trashed ()) {
var key_label = new KeyLabel (_("Original Location:"));
var value_label = new ValueLabel (original_location (file));
info_grid.attach (key_label, 0, n, 1, 1);
info_grid.attach_next_to (value_label, key_label, Gtk.PositionType.RIGHT, 3, 1);
n++;
}
/* Open with */
if (view.get_default_app () != null && !goffile.is_directory) {
Gtk.TreeIter iter;
AppInfo default_app = view.get_default_app ();
store_apps = new Gtk.ListStore (3, typeof (AppInfo), typeof (string), typeof (Icon));
unowned List<AppInfo> apps = view.get_open_with_apps ();
foreach (var app in apps) {
store_apps.append (out iter);
store_apps.set (iter,
AppsColumn.APP_INFO, app,
AppsColumn.LABEL, app.get_name (),
AppsColumn.ICON, ensure_icon (app));
}
store_apps.append (out iter);
store_apps.set (iter,
AppsColumn.LABEL, _("Other Application…"));
store_apps.prepend (out iter);
store_apps.set (iter,
AppsColumn.APP_INFO, default_app,
AppsColumn.LABEL, default_app.get_name (),
AppsColumn.ICON, ensure_icon (default_app));
var renderer = new Gtk.CellRendererText ();
var pix_renderer = new Gtk.CellRendererPixbuf ();
var combo = new Gtk.ComboBox.with_model ((Gtk.TreeModel) store_apps);
combo.active = 0;
combo.valign = Gtk.Align.CENTER;
combo.pack_start (pix_renderer, false);
combo.pack_start (renderer, true);
combo.add_attribute (renderer, "text", AppsColumn.LABEL);
combo.add_attribute (pix_renderer, "gicon", AppsColumn.ICON);
combo.changed.connect (combo_open_with_changed);
var key_label = new KeyLabel (_("Open with:"));
info_grid.attach (key_label, 0, n, 1, 1);
info_grid.attach_next_to (combo, key_label, Gtk.PositionType.RIGHT);
}
/* Device Usage */
if (should_show_device_usage ()) {
try {
var info = goffile.get_target_location ().query_filesystem_info ("filesystem::*");
create_storage_bar (info, n);
} catch (Error e) {
warning ("error: %s", e.message);
}
}
}
private bool should_show_device_usage () {
if (files_contain_a_directory)
return true;
if (count == 1) {
if (goffile.can_unmount ())
return true;
var rootfs_loc = File.new_for_uri ("file:///");
if (goffile.get_target_location ().equal (rootfs_loc))
return true;
}
return false;
}
private void update_perm_codes (Permissions.Type pt, int val, int mult) {
switch (pt) {
case Permissions.Type.USER:
owner_perm_code += mult*val;
break;
case Permissions.Type.GROUP:
group_perm_code += mult*val;
break;
case Permissions.Type.OTHER:
everyone_perm_code += mult*val;
break;
}
}
private void permission_button_toggle (Gtk.ToggleButton btn) {
unowned Permissions.Type pt = btn.get_data ("permissiontype");
unowned Permissions.Value permission_value = btn.get_data ("permissionvalue");
int mult = 1;
reset_and_cancel_perm_timeout ();
if (!btn.get_active ()) {
mult = -1;
}
switch (permission_value) {
case Permissions.Value.READ:
update_perm_codes (pt, 4, mult);
break;
case Permissions.Value.WRITE:
update_perm_codes (pt, 2, mult);
break;
case Permissions.Value.EXE:
update_perm_codes (pt, 1, mult);
break;
}
if (perm_code_should_update) {
perm_code.set_text ("%d%d%d".printf (owner_perm_code, group_perm_code, everyone_perm_code));
}
}
private PermissionButton create_perm_choice (Permissions.Type pt) {
var permission_button = new PermissionButton (pt);
permission_button.btn_read.toggled.connect (permission_button_toggle);
permission_button.btn_write.toggled.connect (permission_button_toggle);
permission_button.btn_exe.toggled.connect (permission_button_toggle);
return permission_button;
}
private uint32 get_perm_from_chmod_unit (uint32 vfs_perm, int nb,
int chmod, Permissions.Type pt) {
if (nb > 7 || nb < 0)
critical ("erroned chmod code %d %d", chmod, nb);
int[] chmod_types = { 4, 2, 1};
int i = 0;
for (; i<3; i++) {
int div = nb / chmod_types[i];
int modulo = nb % chmod_types[i];
if (div >= 1)
vfs_perm |= vfs_perms[pt,i];
nb = modulo;
}
return vfs_perm;
}
private uint32 chmod_to_vfs (int chmod) {
uint32 vfs_perm = 0;
/* user */
vfs_perm = get_perm_from_chmod_unit (vfs_perm, (int) chmod / 100,
chmod, Permissions.Type.USER);
/* group */
vfs_perm = get_perm_from_chmod_unit (vfs_perm, (int) (chmod / 10) % 10,
chmod, Permissions.Type.GROUP);
/* other */
vfs_perm = get_perm_from_chmod_unit (vfs_perm, (int) chmod % 10,
chmod, Permissions.Type.OTHER);
return vfs_perm;
}
private void update_perm_grid_toggle_states (uint32 permissions) {
perm_button_user.update_buttons (permissions);
perm_button_group.update_buttons (permissions);
perm_button_other.update_buttons (permissions);
}
private void reset_and_cancel_perm_timeout () {
if (cancellable != null) {
cancellable.cancel ();
cancellable.reset ();
}
if (timeout_perm != 0) {
Source.remove (timeout_perm);
timeout_perm = 0;
}
}
private async void file_set_attributes (GOF.File file, string attr,
uint32 val, Cancellable? _cancellable = null) {
FileInfo info = new FileInfo ();
/**TODO** use marlin jobs*/
try {
info.set_attribute_uint32 (attr, val);
yield file.location.set_attributes_async (info,
FileQueryInfoFlags.NONE,
Priority.DEFAULT,
_cancellable, null);
} catch (Error e) {
warning ("Could not set file attribute %s: %s", attr, e.message);
}
}
private void entry_changed () {
var str = perm_code.get_text ();
if (Permissions.is_chmod_code (str)) {
reset_and_cancel_perm_timeout ();
timeout_perm = Timeout.add (60, () => {
uint32 perm = chmod_to_vfs (int.parse (str));
perm_code_should_update = false;
update_perm_grid_toggle_states (perm);
perm_code_should_update = true;
int n = 0;
foreach (GOF.File gof in files) {
if (gof.can_set_permissions() && gof.permissions != perm) {
gof.permissions = perm;
/* update permission label once */
if (n < 1) {
l_perm.label = "<tt>%s</tt>".printf (goffile.get_permissions_as_string ());
}
/* real update permissions */
file_set_attributes.begin (gof, FileAttribute.UNIX_MODE, perm, cancellable);
n++;
} else {
warning ("can't change permission on %s", gof.uri);
}
/**TODO** add a list of permissions set errors in the property dialog.*/
}
timeout_perm = 0;
return false;
});
}
}
private void combo_owner_changed (Gtk.ComboBox combo) {
Gtk.TreeIter iter;
string user;
int uid;
if (!combo.get_active_iter(out iter))
return;
store_users.get (iter, 0, out user);
if (!goffile.can_set_owner ()) {
critical ("error can't set user");
return;
}
if (!Eel.get_user_id_from_user_name (user, out uid)
&& !Eel.get_id_from_digit_string (user, out uid)) {
critical ("user doesn t exit");
}
if (uid == goffile.uid)
return;
foreach (GOF.File gof in files)
file_set_attributes.begin (gof, FileAttribute.UNIX_UID, uid);
}
private void combo_group_changed (Gtk.ComboBox combo) {
Gtk.TreeIter iter;
string group;
int gid;
if (!combo.get_active_iter(out iter))
return;
store_groups.get (iter, 0, out group);
if (!goffile.can_set_group ()) {
critical ("error can't set group");
return;
}
/* match gid from name */
if (!Eel.get_group_id_from_group_name (group, out gid)
&& !Eel.get_id_from_digit_string (group, out gid)) {
critical ("group doesn t exit");
return;
}
if (gid == goffile.gid)
return;
foreach (GOF.File gof in files)
file_set_attributes.begin (gof, FileAttribute.UNIX_GID, gid);
}
private Gtk.Grid construct_perm_panel () {
var owner_user_label = new KeyLabel (_("Owner:"));
var owner_user_choice = create_owner_choice ();
var group_combo_label = new KeyLabel (_("Group:"));
group_combo_label.margin_bottom = 12;
var group_combo = create_group_choice ();
group_combo.margin_bottom = 12;
var owner_label = new KeyLabel (_("Owner:"));
perm_button_user = create_perm_choice (Permissions.Type.USER);
var group_label = new KeyLabel (_("Group:"));
perm_button_group = create_perm_choice (Permissions.Type.GROUP);
var other_label = new KeyLabel (_("Everyone:"));
perm_button_other = create_perm_choice (Permissions.Type.OTHER);
perm_code = new Gtk.Entry ();
perm_code.text = "000";
perm_code.max_length = perm_code.max_width_chars = perm_code.width_chars = 3;
l_perm = new Gtk.Label ("<tt>%s</tt>".printf (goffile.get_permissions_as_string ()));
l_perm.halign = Gtk.Align.START;
l_perm.use_markup = true;
perm_grid = new Gtk.Grid ();
perm_grid.column_spacing = 6;
perm_grid.row_spacing = 6;
perm_grid.halign = Gtk.Align.CENTER;
perm_grid.attach (owner_user_label, 0, 1, 1, 1);
perm_grid.attach (owner_user_choice, 1, 1, 2, 1);
perm_grid.attach (group_combo_label, 0, 2, 1, 1);
perm_grid.attach (group_combo, 1, 2, 2, 1);
perm_grid.attach (owner_label, 0, 3, 1, 1);
perm_grid.attach (perm_button_user, 1, 3, 2, 1);
perm_grid.attach (group_label, 0, 4, 1, 1);
perm_grid.attach (perm_button_group, 1, 4, 2, 1);
perm_grid.attach (other_label, 0, 5, 1, 1);
perm_grid.attach (perm_button_other, 1, 5, 2, 1);
perm_grid.attach (l_perm, 1, 6, 1, 1);
perm_grid.attach (perm_code, 2, 6, 1, 1);
update_perm_grid_toggle_states (goffile.permissions);
perm_code.changed.connect (entry_changed);
return perm_grid;
}
private bool selection_can_set_owner () {
foreach (GOF.File gof in files)
if (!gof.can_set_owner ())
return false;
return true;
}
private string? get_common_owner () {
int uid = -1;
if (files == null)
return null;
foreach (GOF.File gof in files){
if (uid == -1 && gof != null) {
uid = gof.uid;
continue;
}
if (gof != null && uid != gof.uid)
return null;
}
return goffile.info.get_attribute_string (FileAttribute.OWNER_USER);
}
private bool selection_can_set_group () {
foreach (GOF.File gof in files)
if (!gof.can_set_group ())
return false;
return true;
}
private string? get_common_group () {
int gid = -1;
if (files == null)
return null;
foreach (GOF.File gof in files) {
if (gid == -1 && gof != null) {
gid = gof.gid;
continue;
}
if (gof != null && gid != gof.gid)
return null;
}
return goffile.info.get_attribute_string (FileAttribute.OWNER_GROUP);
}
private Gtk.Widget create_owner_choice () {
Gtk.Widget choice;
choice = null;
if (selection_can_set_owner ()) {
GLib.List<string> users;
Gtk.TreeIter iter;
store_users = new Gtk.ListStore (1, typeof (string));
users = Eel.get_user_names();
int owner_index = -1;
int i = 0;
foreach (var user in users) {
if (user == goffile.owner) {
owner_index = i;
}
store_users.append(out iter);
store_users.set(iter, 0, user);
i++;
}
/* If ower is not known, we prepend it.
* It happens when the owner has no matching identifier in the password file.
*/
if (owner_index == -1) {
store_users.prepend (out iter);
store_users.set (iter, 0, goffile.owner);
}
var combo = new Gtk.ComboBox.with_model ((Gtk.TreeModel) store_users);
var renderer = new Gtk.CellRendererText ();
combo.pack_start (renderer, true);
combo.add_attribute (renderer, "text", 0);
if (owner_index == -1)
combo.set_active (0);
else
combo.set_active (owner_index);
combo.changed.connect (combo_owner_changed);
choice = (Gtk.Widget) combo;
} else {
string? common_owner = get_common_owner ();
if (common_owner == null)
common_owner = "--";
choice = (Gtk.Widget) new Gtk.Label (common_owner);
choice.set_halign (Gtk.Align.START);
}
choice.set_valign (Gtk.Align.CENTER);
return choice;
}
private Gtk.Widget create_group_choice () {
Gtk.Widget choice;
if (selection_can_set_group ()) {
GLib.List<string> groups;
Gtk.TreeIter iter;
store_groups = new Gtk.ListStore (1, typeof (string));
groups = goffile.get_settable_group_names ();
int group_index = -1;
int i = 0;
foreach (var group in groups) {
if (group == goffile.group) {
group_index = i;
}
store_groups.append (out iter);
store_groups.set (iter, 0, group);
i++;
}
/* If ower is not known, we prepend it.
* It happens when the owner has no matching identifier in the password file.
*/
if (group_index == -1) {
store_groups.prepend (out iter);
store_groups.set (iter, 0, goffile.owner);
}
var combo = new Gtk.ComboBox.with_model ((Gtk.TreeModel) store_groups);
var renderer = new Gtk.CellRendererText ();
combo.pack_start (renderer, true);
combo.add_attribute (renderer, "text", 0);
if (group_index == -1)
combo.set_active (0);
else
combo.set_active (group_index);
combo.changed.connect (combo_group_changed);
choice = (Gtk.Widget) combo;
} else {
string? common_group = get_common_group ();
if (common_group == null)
common_group = "--";
choice = (Gtk.Widget) new Gtk.Label (common_group);
choice.set_halign (Gtk.Align.START);
}
choice.set_valign (Gtk.Align.CENTER);
return choice;
}
private Icon ensure_icon (AppInfo app) {
Icon icon = app.get_icon ();
if (icon == null)
icon = new ThemedIcon ("application-x-executable");
return icon;
}
private void combo_open_with_changed (Gtk.ComboBox combo) {
Gtk.TreeIter iter;
string app_label;
AppInfo? app;
if (!combo.get_active_iter (out iter))
return;
store_apps.get (iter,
AppsColumn.LABEL, out app_label,
AppsColumn.APP_INFO, out app);
if (app == null) {
var app_chosen = Marlin.MimeActions.choose_app_for_glib_file (goffile.location, this);
if (app_chosen != null) {
store_apps.prepend (out iter);
store_apps.set (iter,
AppsColumn.APP_INFO, app_chosen,
AppsColumn.LABEL, app_chosen.get_name (),
AppsColumn.ICON, ensure_icon (app_chosen));
combo.set_active (0);
}
} else {
try {
foreach (var mime in mimes)
app.set_as_default_for_type (mime);
} catch (Error e) {
critical ("Couldn't set as default: %s", e.message);
}
}
}
public static uint64 file_real_size (GOF.File gof) {
if (!gof.is_connected)
return 0;
uint64 file_size = gof.size;
if (gof.location is GLib.File) {
try {
var info = gof.location.query_info (FileAttribute.STANDARD_ALLOCATED_SIZE, FileQueryInfoFlags.NONE);
uint64 allocated_size = info.get_attribute_uint64 (FileAttribute.STANDARD_ALLOCATED_SIZE);
/* Check for sparse file, allocated size will be smaller, for normal files allocated size
* includes overhead size so we don't use it for those here
*/
if (allocated_size > 0 && allocated_size < file_size && !gof.is_directory)
file_size = allocated_size;
} catch (Error err) {
debug ("%s", err.message);
gof.is_connected = false;
}
}
return file_size;
}
private string get_contains_value (uint folders, uint files) {
string folders_txt = "";
string files_txt = "";
if (folders > 0) {
folders_txt = (ngettext ("%u subfolder", "%u subfolders", folders)).printf (folders);
}
if (files > 0) {
files_txt = (ngettext ("%u file", "%u files", files)).printf (files);
}
if (folders > 0 && files > 0) {
///TRANSLATORS: folders, files
return _("%s, %s").printf (folders_txt, files_txt);
} else if (files > 0) {
return files_txt;
} else {
return folders_txt;
}
}
private string get_selected_label (uint folders, uint files) {
string folders_txt = "";
string files_txt = "";
if (folders > 0) {
folders_txt = (ngettext ("%u folder", "%u folders", folders)).printf (folders);
}
if (files > 0) {
files_txt = (ngettext ("%u file", "%u files", files)).printf (files);
}
if (files > 0 && folders > 0) {
string total_txt = (ngettext ("%u selected item", "%u selected items", folders + files)).printf (folders + files);
///TRANSLATORS: total (folders, files)
return _("%s (%s, %s)").printf (total_txt, folders_txt, files_txt);
} else if (files > 0) {
return files_txt;
} else {
return folders_txt;
}
}
/** Hide certain widgets under certain conditions **/
private void update_widgets_state () {
if (uncounted_folders == 0) {
spinner.hide ();
}
if (count > 1) {
type_key_label.hide ();
type_value.hide ();
} else {
if (ftype != null) {
type_value.label = goffile.formated_type;
}
}
if ((header_title is Gtk.Entry) && !view.is_in_recent ()) {
int start_offset= 0, end_offset = -1;
Marlin.get_rename_region (goffile.info.get_name (), out start_offset, out end_offset, goffile.is_folder ());
(header_title as Gtk.Entry).select_region (start_offset, end_offset);
}
/* Only show 'contains' label when only folders selected - otherwise could be ambiguous whether
* the "contained files" counted are only in the subfolders or not.*/
/* Only show 'contains' label when folders selected are not empty */
if (count > selected_folders || contains_value.label.length < 1) {
contains_key_label.hide ();
contains_value.hide ();
} else { /* Make sure it shows otherwise (may have been hidden by previous call)*/
contains_key_label.show ();
contains_value.show ();
}
}
}
}
|