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
|
using Gtk;
using GLib;
using Gdk;
public class DesktopEntry : Object {
public string path { get; set; default = ""; }
public string desktop_id { get; set; default = ""; }
public string name { get; set; default = ""; }
public string comment { get; set; default = ""; }
public string exec_cmd { get; set; default = ""; }
public string icon_name { get; set; default = ""; }
public string categories { get; set; default = ""; }
public string source_dir { get; set; default = ""; }
public bool no_display { get; set; default = false; }
public bool hidden { get; set; default = false; }
public bool is_user_owned { get; set; default = false; }
}
private enum CategoryColumns {
ICON_NAME,
LABEL,
KEY
}
private enum EntryColumns {
ICON_PIXBUF,
NAME,
COMMENT,
PATH,
ENTRY
}
public class DesktopScanner : Object {
private const string DESKTOP_GROUP = "Desktop Entry";
private string[] scan_dirs;
private string[] user_delete_roots;
private GenericArray<DesktopEntry> last_scan_entries;
public DesktopScanner () {
string home_dir = Environment.get_home_dir ();
this.scan_dirs = {
Path.build_filename (home_dir, ".local", "share", "applications"),
"/usr/local/share/applications",
"/usr/share/applications",
Path.build_filename (home_dir, ".local", "share", "flatpak", "exports", "share", "applications"),
"/var/lib/flatpak/exports/share/applications",
"/var/lib/snapd/desktop/applications",
Path.build_filename (home_dir, "Desktop"),
Path.build_filename (home_dir, ".config", "autostart")
};
this.user_delete_roots = {
Path.build_filename (home_dir, ".local", "share", "applications"),
Path.build_filename (home_dir, "Desktop"),
Path.build_filename (home_dir, ".config", "autostart"),
Path.build_filename (home_dir, ".local", "share", "flatpak", "exports", "share", "applications")
};
this.last_scan_entries = new GenericArray<DesktopEntry> ();
}
public string[] get_scan_dirs () {
return this.scan_dirs;
}
public string[] get_delete_roots () {
return this.user_delete_roots;
}
public GenericArray<DesktopEntry> get_last_scan_entries () {
return this.last_scan_entries;
}
public GenericArray<DesktopEntry> scan () {
var results = new GenericArray<DesktopEntry> ();
var seen = new HashTable<string, bool> (str_hash, str_equal);
this.last_scan_entries = new GenericArray<DesktopEntry> ();
foreach (string dir in this.scan_dirs) {
scan_directory (dir, results, seen);
}
return results;
}
public string get_primary_category (DesktopEntry entry) {
if (entry.categories == null || entry.categories == "") {
return "Прочее";
}
string[] parts = entry.categories.split (";");
foreach (string raw_part in parts) {
string part = raw_part.strip ();
if (part == "") {
continue;
}
switch (part) {
case "AudioVideo":
return "Мультимедиа";
case "Development":
return "Разработка";
case "Game":
return "Игры";
case "Graphics":
return "Графика";
case "Network":
return "Интернет";
case "Office":
return "Офис";
case "Science":
case "Education":
return "Наука";
case "Settings":
return "Настройки";
case "System":
return "Система";
case "Utility":
return "Утилиты";
default:
break;
}
}
return "Прочее";
}
public string get_source_group (DesktopEntry entry) {
string home_dir = Environment.get_home_dir ();
string desktop_dir = Path.build_filename (home_dir, "Desktop");
string autostart_dir = Path.build_filename (home_dir, ".config", "autostart");
if (entry.source_dir == desktop_dir) {
return "Desktop";
}
if (entry.source_dir == autostart_dir) {
return "Autostart";
}
return "";
}
public bool is_path_user_owned (string path) {
string canonical = canonicalize_path (path);
if (canonical == "") {
canonical = path;
}
foreach (string root in this.user_delete_roots) {
string candidate = canonicalize_path (root);
if (candidate == "") {
candidate = root;
}
if (path_is_under (canonical, candidate)) {
return true;
}
}
return Posix.access (path, Posix.W_OK) == 0;
}
private void scan_directory (string dir, GenericArray<DesktopEntry> results, HashTable<string, bool> seen) {
if (!FileUtils.test (dir, FileTest.IS_DIR)) {
return;
}
try {
var directory = File.new_for_path (dir);
var enumerator = directory.enumerate_children (
FileAttribute.STANDARD_NAME + "," + FileAttribute.STANDARD_TYPE,
FileQueryInfoFlags.NONE,
null
);
FileInfo? info = null;
while ((info = enumerator.next_file (null)) != null) {
if (info.get_file_type () != FileType.REGULAR && info.get_file_type () != FileType.SYMBOLIC_LINK) {
continue;
}
string name = info.get_name ();
if (!name.has_suffix (".desktop")) {
continue;
}
string path = Path.build_filename (dir, name);
var entry = parse_desktop_file (path, dir);
if (entry == null) {
continue;
}
if (seen.contains (entry.desktop_id)) {
continue;
}
seen.insert (entry.desktop_id, true);
results.add (entry);
this.last_scan_entries.add (entry);
}
} catch (Error err) {
stderr.printf ("Warning: failed to scan %s: %s\n", dir, err.message);
}
}
private DesktopEntry? parse_desktop_file (string path, string source_dir) {
try {
var key_file = new KeyFile ();
key_file.load_from_file (path, KeyFileFlags.NONE);
string type_value = get_string_value (key_file, "Type");
if (type_value != "Application") {
return null;
}
string desktop_id = Path.get_basename (path);
if (desktop_id.has_suffix (".desktop")) {
desktop_id = desktop_id.substring (0, desktop_id.length - ".desktop".length);
}
var entry = new DesktopEntry ();
entry.path = path;
entry.desktop_id = desktop_id;
entry.name = get_locale_value (key_file, "Name");
if (entry.name == "") {
entry.name = desktop_id;
}
entry.comment = get_locale_value (key_file, "Comment");
entry.exec_cmd = get_string_value (key_file, "Exec");
entry.icon_name = get_string_value (key_file, "Icon");
entry.categories = get_string_value (key_file, "Categories");
entry.source_dir = source_dir;
entry.no_display = get_boolean_value (key_file, "NoDisplay");
entry.hidden = get_boolean_value (key_file, "Hidden");
entry.is_user_owned = is_path_user_owned (path);
return entry;
} catch (Error err) {
stderr.printf ("Warning: failed to parse %s: %s\n", path, err.message);
return null;
}
}
private string get_locale_value (KeyFile key_file, string key) {
try {
return key_file.get_locale_string (DESKTOP_GROUP, key, null);
} catch (Error err) {
return "";
}
}
private string get_string_value (KeyFile key_file, string key) {
try {
return key_file.get_string (DESKTOP_GROUP, key);
} catch (Error err) {
return "";
}
}
private bool get_boolean_value (KeyFile key_file, string key) {
try {
return key_file.get_boolean (DESKTOP_GROUP, key);
} catch (Error err) {
return false;
}
}
private string canonicalize_path (string path) {
string? canonical = Posix.realpath (path);
if (canonical == null) {
return "";
}
return canonical;
}
private bool path_is_under (string child_path, string parent_path) {
if (child_path == parent_path) {
return true;
}
string normalized_parent = parent_path;
if (!normalized_parent.has_suffix ("/")) {
normalized_parent += "/";
}
return child_path.has_prefix (normalized_parent);
}
}
public class DesktopItemManager : Gtk.Window {
private const string CATEGORY_ALL = "__all__";
private const string CATEGORY_USER = "__user__";
private const int ICON_SIZE = 24;
private DesktopScanner scanner;
private GenericArray<FileMonitor> monitors;
private GenericArray<DesktopEntry> entries;
private Gtk.SearchEntry search_entry;
private Gtk.ToggleButton hidden_toggle;
private Gtk.Button create_button;
private Gtk.TreeStore category_store;
private Gtk.TreeView category_view;
private Gtk.ListStore entry_store;
private Gtk.TreeModelFilter entry_filter;
private Gtk.TreeView entry_view;
private Gtk.Label name_label;
private Gtk.Label exec_label;
private Gtk.Label path_label;
private Gtk.Button launch_button;
private Gtk.Button edit_button;
private Gtk.Button delete_button;
private string selected_category = CATEGORY_ALL;
private uint rescan_source_id = 0;
public DesktopItemManager () {
Object (title: "Менеджер ярлыков");
this.scanner = new DesktopScanner ();
this.monitors = new GenericArray<FileMonitor> ();
this.entries = new GenericArray<DesktopEntry> ();
build_ui ();
populate_categories ();
rescan ();
setup_monitors ();
delete_event.connect (() => {
return false;
});
destroy.connect (() => {
cleanup_monitors ();
if (this.rescan_source_id != 0) {
Source.remove (this.rescan_source_id);
this.rescan_source_id = 0;
}
Gtk.main_quit ();
});
}
private void build_ui () {
set_default_size (1100, 720);
set_position (Gtk.WindowPosition.CENTER);
var header = new Gtk.HeaderBar ();
header.show_close_button = true;
header.title = "Менеджер ярлыков";
this.search_entry = new Gtk.SearchEntry ();
this.search_entry.placeholder_text = "Поиск...";
this.search_entry.hexpand = true;
this.search_entry.changed.connect (() => {
this.entry_filter.refilter ();
});
header.pack_start (this.search_entry);
this.hidden_toggle = new Gtk.ToggleButton.with_label ("Скрытые");
this.hidden_toggle.toggled.connect (() => {
this.entry_filter.refilter ();
});
header.pack_end (this.hidden_toggle);
this.create_button = new Gtk.Button.with_label ("+ Создать");
this.create_button.clicked.connect (() => {
on_create_clicked ();
});
header.pack_end (this.create_button);
set_titlebar (header);
var main_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
add (main_box);
var paned = new Gtk.Paned (Gtk.Orientation.HORIZONTAL);
main_box.pack_start (paned, true, true, 0);
build_category_panel (paned);
build_entry_panel (paned);
var detail_panel = build_detail_panel ();
main_box.pack_end (detail_panel, false, true, 0);
}
private void build_category_panel (Gtk.Paned paned) {
this.category_store = new Gtk.TreeStore (
3,
typeof (string),
typeof (string),
typeof (string)
);
this.category_view = new Gtk.TreeView.with_model (this.category_store);
this.category_view.headers_visible = false;
var icon_renderer = new Gtk.CellRendererPixbuf ();
var icon_column = new Gtk.TreeViewColumn.with_attributes (
"",
icon_renderer,
"icon-name",
CategoryColumns.ICON_NAME,
null
);
this.category_view.append_column (icon_column);
var text_renderer = new Gtk.CellRendererText ();
var text_column = new Gtk.TreeViewColumn.with_attributes (
"Категория",
text_renderer,
"text",
CategoryColumns.LABEL,
null
);
this.category_view.append_column (text_column);
var selection = this.category_view.get_selection ();
selection.set_mode (Gtk.SelectionMode.SINGLE);
selection.changed.connect (() => {
on_category_changed ();
});
var scrolled = new Gtk.ScrolledWindow (null, null);
scrolled.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
scrolled.set_min_content_width (250);
scrolled.add (this.category_view);
paned.pack1 (scrolled, false, false);
}
private void build_entry_panel (Gtk.Paned paned) {
this.entry_store = new Gtk.ListStore (
5,
typeof (Gdk.Pixbuf),
typeof (string),
typeof (string),
typeof (string),
typeof (DesktopEntry)
);
this.entry_store.set_sort_column_id (EntryColumns.NAME, Gtk.SortType.ASCENDING);
this.entry_filter = new Gtk.TreeModelFilter (this.entry_store, null);
this.entry_filter.set_visible_func ((model, iter) => {
return entry_visible (model, iter);
});
this.entry_view = new Gtk.TreeView.with_model (this.entry_filter);
this.entry_view.headers_visible = true;
this.entry_view.activate_on_single_click = false;
this.entry_view.row_activated.connect ((path, column) => {
on_launch_clicked ();
});
var icon_renderer = new Gtk.CellRendererPixbuf ();
var icon_column = new Gtk.TreeViewColumn.with_attributes (
"",
icon_renderer,
"pixbuf",
EntryColumns.ICON_PIXBUF,
null
);
icon_column.min_width = 34;
this.entry_view.append_column (icon_column);
var name_renderer = new Gtk.CellRendererText ();
var name_column = new Gtk.TreeViewColumn.with_attributes (
"Имя",
name_renderer,
"text",
EntryColumns.NAME,
null
);
name_column.expand = true;
this.entry_view.append_column (name_column);
var comment_renderer = new Gtk.CellRendererText ();
var comment_column = new Gtk.TreeViewColumn.with_attributes (
"Описание",
comment_renderer,
"text",
EntryColumns.COMMENT,
null
);
comment_column.expand = true;
this.entry_view.append_column (comment_column);
var selection = this.entry_view.get_selection ();
selection.set_mode (Gtk.SelectionMode.SINGLE);
selection.changed.connect (() => {
on_selection_changed ();
});
var scrolled = new Gtk.ScrolledWindow (null, null);
scrolled.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
scrolled.add (this.entry_view);
paned.pack2 (scrolled, true, false);
}
private Gtk.Widget build_detail_panel () {
var frame = new Gtk.Frame (null);
frame.shadow_type = Gtk.ShadowType.IN;
var outer_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 10);
outer_box.margin = 12;
frame.add (outer_box);
var grid = new Gtk.Grid ();
grid.column_spacing = 12;
grid.row_spacing = 6;
outer_box.pack_start (grid, false, false, 0);
var name_title = new Gtk.Label ("Имя:");
name_title.halign = Gtk.Align.START;
grid.attach (name_title, 0, 0, 1, 1);
this.name_label = new Gtk.Label ("—");
this.name_label.halign = Gtk.Align.START;
this.name_label.selectable = true;
this.name_label.ellipsize = Pango.EllipsizeMode.END;
grid.attach (this.name_label, 1, 0, 1, 1);
var exec_title = new Gtk.Label ("Exec:");
exec_title.halign = Gtk.Align.START;
grid.attach (exec_title, 0, 1, 1, 1);
this.exec_label = new Gtk.Label ("—");
this.exec_label.halign = Gtk.Align.START;
this.exec_label.selectable = true;
this.exec_label.ellipsize = Pango.EllipsizeMode.MIDDLE;
grid.attach (this.exec_label, 1, 1, 1, 1);
var path_title = new Gtk.Label ("Путь:");
path_title.halign = Gtk.Align.START;
grid.attach (path_title, 0, 2, 1, 1);
this.path_label = new Gtk.Label ("—");
this.path_label.halign = Gtk.Align.START;
this.path_label.selectable = true;
this.path_label.ellipsize = Pango.EllipsizeMode.MIDDLE;
grid.attach (this.path_label, 1, 2, 1, 1);
var button_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 8);
outer_box.pack_end (button_box, false, false, 0);
this.launch_button = new Gtk.Button.with_label ("▶ Запустить");
this.launch_button.sensitive = false;
this.launch_button.clicked.connect (() => {
on_launch_clicked ();
});
button_box.pack_start (this.launch_button, false, false, 0);
this.edit_button = new Gtk.Button.with_label ("✎ Изменить");
this.edit_button.sensitive = false;
this.edit_button.clicked.connect (() => {
on_edit_clicked ();
});
button_box.pack_start (this.edit_button, false, false, 0);
this.delete_button = new Gtk.Button.with_label ("🗑 Удалить");
this.delete_button.sensitive = false;
this.delete_button.clicked.connect (() => {
on_delete_clicked ();
});
button_box.pack_start (this.delete_button, false, false, 0);
return frame;
}
private void populate_categories () {
this.category_store.clear ();
add_category_row ("view-list-symbolic", "Все", CATEGORY_ALL);
add_category_row ("applications-multimedia", "Мультимедиа", "Мультимедиа");
add_category_row ("applications-development", "Разработка", "Разработка");
add_category_row ("applications-games", "Игры", "Игры");
add_category_row ("applications-graphics", "Графика", "Графика");
add_category_row ("applications-internet", "Интернет", "Интернет");
add_category_row ("x-office-document", "Офис", "Офис");
add_category_row ("applications-science", "Наука", "Наука");
add_category_row ("preferences-system", "Настройки", "Настройки");
add_category_row ("applications-system", "Система", "Система");
add_category_row ("applications-utilities", "Утилиты", "Утилиты");
add_category_row ("applications-other", "Прочее", "Прочее");
add_category_row ("media-playlist-repeat", "Autostart", "Autostart");
add_category_row ("user-desktop", "Desktop", "Desktop");
add_category_row ("user-home", "Пользовательские", CATEGORY_USER);
Gtk.TreeIter first_iter;
if (this.category_store.get_iter_first (out first_iter)) {
this.category_view.get_selection ().select_iter (first_iter);
}
}
private void add_category_row (string icon_name, string label, string key) {
Gtk.TreeIter iter;
this.category_store.append (out iter, null);
this.category_store.set (
iter,
CategoryColumns.ICON_NAME, icon_name,
CategoryColumns.LABEL, label,
CategoryColumns.KEY, key
);
}
private void populate_entries () {
this.entry_store.clear ();
var scan_entries = this.scanner.get_last_scan_entries ();
for (uint i = 0; i < scan_entries.length; i++) {
DesktopEntry entry = scan_entries[i];
Gdk.Pixbuf? icon = load_entry_icon (entry);
Gtk.TreeIter iter;
this.entry_store.append (out iter);
this.entry_store.set (
iter,
EntryColumns.ICON_PIXBUF, icon,
EntryColumns.NAME, entry.name,
EntryColumns.COMMENT, entry.comment,
EntryColumns.PATH, entry.path,
EntryColumns.ENTRY, entry
);
}
}
private Gdk.Pixbuf? load_entry_icon (DesktopEntry entry) {
string icon_name = entry.icon_name;
if (icon_name == null || icon_name.strip () == "") {
icon_name = "application-x-executable";
}
try {
if (Path.is_absolute (icon_name) && FileUtils.test (icon_name, FileTest.EXISTS)) {
return new Gdk.Pixbuf.from_file_at_scale (icon_name, ICON_SIZE, ICON_SIZE, true);
}
} catch (Error err) {
}
var theme = Gtk.IconTheme.get_default ();
try {
return theme.load_icon (icon_name, ICON_SIZE, 0);
} catch (Error err) {
try {
return theme.load_icon ("application-x-executable", ICON_SIZE, 0);
} catch (Error fallback_err) {
return null;
}
}
}
private void setup_monitors () {
cleanup_monitors ();
this.monitors = new GenericArray<FileMonitor> ();
foreach (string dir in this.scanner.get_scan_dirs ()) {
if (!FileUtils.test (dir, FileTest.IS_DIR)) {
continue;
}
try {
var monitor = File.new_for_path (dir).monitor_directory (FileMonitorFlags.NONE, null);
monitor.changed.connect ((file, other_file, event_type) => {
on_dir_changed ();
});
this.monitors.add (monitor);
} catch (Error err) {
stderr.printf ("Warning: failed to monitor %s: %s\n", dir, err.message);
}
}
}
private void cleanup_monitors () {
for (uint i = 0; i < this.monitors.length; i++) {
this.monitors[i].cancel ();
}
this.monitors = new GenericArray<FileMonitor> ();
}
private void on_dir_changed () {
if (this.rescan_source_id != 0) {
Source.remove (this.rescan_source_id);
this.rescan_source_id = 0;
}
this.rescan_source_id = Timeout.add (500, () => {
this.rescan_source_id = 0;
rescan ();
return false;
});
}
private void rescan () {
string previous_path = get_selected_path ();
this.entries = this.scanner.scan ();
populate_entries ();
this.entry_filter.refilter ();
if (previous_path != "" && select_entry_by_path (previous_path)) {
return;
}
clear_details ();
}
private bool entry_visible (Gtk.TreeModel model, Gtk.TreeIter iter) {
DesktopEntry? entry = null;
model.get (iter, EntryColumns.ENTRY, out entry);
if (entry == null) {
return false;
}
if (!this.hidden_toggle.active && (entry.no_display || entry.hidden)) {
return false;
}
string query = this.search_entry.text.strip ().down ();
if (query != "") {
string haystack = (entry.name + "\n" + entry.comment + "\n" + entry.exec_cmd).down ();
if (!haystack.contains (query)) {
return false;
}
}
if (this.selected_category == CATEGORY_ALL) {
return true;
}
if (this.selected_category == CATEGORY_USER) {
return entry.is_user_owned;
}
if (this.scanner.get_source_group (entry) == this.selected_category) {
return true;
}
return this.scanner.get_primary_category (entry) == this.selected_category;
}
private void on_category_changed () {
Gtk.TreeModel? model = null;
Gtk.TreeIter iter;
if (this.category_view.get_selection ().get_selected (out model, out iter)) {
string key = CATEGORY_ALL;
model.get (iter, CategoryColumns.KEY, out key);
this.selected_category = key;
} else {
this.selected_category = CATEGORY_ALL;
}
this.entry_filter.refilter ();
if (get_selected_entry () == null) {
clear_details ();
}
}
private void on_selection_changed () {
DesktopEntry? entry = get_selected_entry ();
if (entry == null) {
clear_details ();
return;
}
this.name_label.set_text (entry.name != "" ? entry.name : "—");
this.exec_label.set_text (entry.exec_cmd != "" ? entry.exec_cmd : "—");
this.path_label.set_text (entry.path != "" ? entry.path : "—");
this.launch_button.sensitive = true;
this.edit_button.sensitive = true;
this.delete_button.sensitive = can_delete_entry (entry);
}
private void clear_details () {
this.name_label.set_text ("—");
this.exec_label.set_text ("—");
this.path_label.set_text ("—");
this.launch_button.sensitive = false;
this.edit_button.sensitive = false;
this.delete_button.sensitive = false;
}
private DesktopEntry? get_selected_entry () {
Gtk.TreeModel? model = null;
Gtk.TreeIter iter;
if (!this.entry_view.get_selection ().get_selected (out model, out iter)) {
return null;
}
DesktopEntry? entry = null;
model.get (iter, EntryColumns.ENTRY, out entry);
return entry;
}
private string get_selected_path () {
DesktopEntry? entry = get_selected_entry ();
if (entry == null) {
return "";
}
return entry.path;
}
private bool select_entry_by_path (string path) {
Gtk.TreeIter iter;
if (!this.entry_filter.get_iter_first (out iter)) {
return false;
}
do {
string row_path = "";
this.entry_filter.get (iter, EntryColumns.PATH, out row_path);
if (row_path == path) {
var selection = this.entry_view.get_selection ();
selection.select_iter (iter);
Gtk.TreePath tree_path = this.entry_filter.get_path (iter);
this.entry_view.scroll_to_cell (tree_path, null, false, 0.0f, 0.0f);
return true;
}
} while (this.entry_filter.iter_next (ref iter));
return false;
}
private void on_launch_clicked () {
DesktopEntry? entry = get_selected_entry ();
if (entry == null) {
return;
}
try {
var info = new DesktopAppInfo.from_filename (entry.path);
if (info == null) {
show_error_dialog ("Не удалось открыть ярлык.", entry.path);
return;
}
info.launch (null, null);
} catch (Error err) {
show_error_dialog ("Не удалось запустить приложение.", err.message);
}
}
private void on_edit_clicked () {
DesktopEntry? entry = get_selected_entry ();
if (entry == null) {
return;
}
try {
string[] argv = { "gnome-desktop-item-edit", entry.path };
Pid child_pid;
Process.spawn_async (null, argv, null, SpawnFlags.SEARCH_PATH, null, out child_pid);
} catch (Error err) {
show_error_dialog ("Не удалось открыть редактор ярлыков.", err.message);
}
}
private void on_create_clicked () {
try {
string[] argv = { "gnome-desktop-item-edit" };
Pid child_pid;
Process.spawn_async (null, argv, null, SpawnFlags.SEARCH_PATH, null, out child_pid);
} catch (Error err) {
show_error_dialog ("Не удалось запустить создание ярлыка.", err.message);
}
}
private void on_delete_clicked () {
DesktopEntry? entry = get_selected_entry ();
if (entry == null) {
return;
}
if (!can_delete_entry (entry)) {
show_error_dialog ("Удаление недоступно.", "Разрешено удалять только пользовательские ярлыки из поддерживаемых каталогов.");
return;
}
var dialog = new Gtk.MessageDialog (
this,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.WARNING,
Gtk.ButtonsType.OK_CANCEL,
"Удалить %s?\nПуть: %s",
entry.name,
entry.path
);
int response = dialog.run ();
dialog.destroy ();
if (response != Gtk.ResponseType.OK) {
return;
}
if (FileUtils.remove (entry.path) != 0) {
show_error_dialog ("Не удалось удалить файл.", entry.path);
return;
}
rescan ();
}
private bool can_delete_entry (DesktopEntry entry) {
if (!entry.is_user_owned) {
return false;
}
string canonical = canonicalize_path (entry.path);
if (canonical == "") {
return false;
}
foreach (string root in this.scanner.get_delete_roots ()) {
string allowed_root = canonicalize_path (root);
if (allowed_root == "") {
allowed_root = root;
}
if (path_is_under (canonical, allowed_root)) {
return true;
}
}
return false;
}
private void show_error_dialog (string primary_text, string secondary_text) {
var dialog = new Gtk.MessageDialog (
this,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.ERROR,
Gtk.ButtonsType.CLOSE,
"%s",
primary_text
);
if (secondary_text != "") {
dialog.format_secondary_text ("%s", secondary_text);
}
dialog.run ();
dialog.destroy ();
}
private string canonicalize_path (string path) {
string? canonical = Posix.realpath (path);
if (canonical == null) {
return "";
}
return canonical;
}
private bool path_is_under (string child_path, string parent_path) {
if (child_path == parent_path) {
return true;
}
string normalized_parent = parent_path;
if (!normalized_parent.has_suffix ("/")) {
normalized_parent += "/";
}
return child_path.has_prefix (normalized_parent);
}
}
public static int main (string[] args) {
Gtk.init (ref args);
var window = new DesktopItemManager ();
window.show_all ();
Gtk.main ();
return 0;
}
|