-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlaunch-menu.c
More file actions
3404 lines (3016 loc) · 113 KB
/
Copy pathlaunch-menu.c
File metadata and controls
3404 lines (3016 loc) · 113 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
#define _POSIX_C_SOURCE 200809L
#define _GNU_SOURCE
#include <gtk/gtk.h>
#include <libxfce4panel/libxfce4panel.h>
#include <libxfce4util/libxfce4util.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <xfconf/xfconf.h>
#include <libxfce4ui/libxfce4ui.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
// #define WNCK_I_KNOW_THIS_IS_UNSTABLE
#include <libwnck/libwnck.h>
#define PLUGIN_ID "launch-menu"
#define PLUGIN_VERSION "1.0.0" // Release version
#define PLUGIN_WEBSITE "https://github.com/James-Gryphon/Launch-Menu"
#define PLUGIN_AUTHORS "James Gooch"
#define CONFIG_CHANNEL "xfce4-panel"
#define CONFIG_PROPERTY_BASE "/plugins/" PLUGIN_ID
#define SPATIAL_MENU_DIR ".config/Launch Menu Items"
#define RECENT_APPS_DIR "Recent Applications"
#define RECENT_DOCS_DIR "Recent Documents"
#define TIMESTAMP_FORMAT "%Y%m%d_%H%M%S"
/* CLASSIC LIBRARY DEFINES */
gchar *classlib_get_process_name_from_pid(pid_t pid);
const gchar *classlib_get_application_display_name(WnckApplication *app);
gboolean classlib_looks_like_window_title(const gchar *name);
const gchar *classlib_get_application_display_name(WnckApplication *app);
const gchar *classlib_ensure_valid_utf8(const gchar *input);
gboolean classlib_is_file_manager(WnckApplication *app);
gboolean classlib_should_blacklist_application(xmlNode *bookmark_node);
gchar *classlib_get_default_file_manager(void);
gboolean classlib_is_desktop_manager(const gchar *process_name);
typedef enum
{
CLASSLIB_LOCALE_TYPE_C, /* ASCII sorting */
CLASSLIB_LOCALE_TYPE_UTF8 /* Smart sorting that ignores special chars */
} ClassicLocaleType;
typedef enum
{
CLASSLIB_SORT_STYLE_CAJA, /* Locale-aware, ignores special chars in UTF-8 */
CLASSLIB_SORT_STYLE_THUNAR, /* Always uses natural sorting regardless of locale */
CLASSLIB_SORT_STYLE_UNKNOWN /* Fallback to Caja behavior */
} ClassicSortStyle;
gint classlib_file_manager_aware_compare(const gchar *a, const gchar *b, ClassicSortStyle sort_style, ClassicLocaleType locale_type);
gint classlib_get_special_char_priority(const gchar *str, ClassicSortStyle sort_style);
ClassicLocaleType classlib_detect_locale_type(void);
gint classlib_natural_compare_strings(const gchar *a, const gchar *b);
gchar *classlib_find_desktop_file(const gchar *app_name, WnckApplication *app);
gchar *classlib_search_desktop_directory(const gchar *dir_path, const gchar *app_name);
/* END CLASS LIBRARY DEFINES*/
typedef struct
{
XfcePanelPlugin *plugin;
GtkWidget *button;
GtkWidget *icon;
GtkWidget *label;
GtkWidget *menu;
WnckHandle *handle;
gchar *launch_menu_path;
gchar *icon_name;
gboolean hardinfo_available;
/* Configuration properties */
XfconfChannel *channel;
gchar *property_base;
gboolean recents_on;
gint recent_apps_count;
gint recent_docs_count;
gboolean classic_style_duplicates;
/* Enhanced functionality fields */
GFileMonitor *xbel_monitor;
WnckScreen *wnck_screen;
GHashTable *known_applications;
} LaunchMenuPlugin;
/* Structure to hold menu items with their display names for sorting */
typedef struct
{
gchar *filename; /* Original filename */
gchar *display_name; /* Pretty name for display and sorting */
gchar *full_path; /* Full path to the item */
} MenuItemInfo;
/* Structure for recent items management */
typedef struct
{
gchar *name;
gchar *path;
gchar *timestamp_filename;
time_t timestamp;
gboolean is_application;
} RecentItem;
typedef struct
{
gchar *path;
gchar *name;
time_t timestamp;
} XbelEntry;
/* Function declarations */
static void launch_menu_construct(XfcePanelPlugin *plugin);
static void launch_menu_free(XfcePanelPlugin *plugin);
static gboolean on_button_pressed(GtkWidget *widget, GdkEventButton *event,
LaunchMenuPlugin *launch_plugin);
static void create_simple_menu(LaunchMenuPlugin *launch_plugin);
static GtkWidget *build_submenu_from_directory(const gchar *dir_path, gint
depth, LaunchMenuPlugin *launch_plugin);
static void on_about_computer_clicked(LaunchMenuPlugin
*launch_plugin);
static void on_launch_item_clicked(GtkMenuItem *item, gpointer user_data);
static GtkWidget *create_menu_item_with_icon(const gchar *label, const gchar *item_path);
// static void add_recent_application(LaunchMenuPlugin *launch_plugin, const gchar *app_path);
// static void cleanup_recent_folder(const gchar *folder_path, gint max_items);
/* Enhanced functionality - Phase 2 & 3 additions */
static gboolean parse_desktop_file_for_menu(const gchar *desktop_path, gchar
**display_name, gchar **icon_name);
static gchar *get_item_display_name(const gchar *item_name, const gchar
*item_path);
static gint compare_menu_items(gconstpointer a, gconstpointer b);
static void menu_item_info_free(MenuItemInfo *info);
static gchar *ensure_valid_utf8(const gchar *input);
/* Phase 3 - XBEL Document Tracking */
static void parse_xbel_file(LaunchMenuPlugin *launch_plugin);
static void on_xbel_file_changed(GFileMonitor *monitor, GFile *file, GFile
*other_file, GFileMonitorEvent event_type, gpointer user_data);
static gboolean is_document_file(const gchar *path);
static gchar *get_timestamp_string(time_t timestamp);
static gchar *create_safe_filename(const gchar *original_name, time_t
timestamp);
static void enforce_item_limit_with_timestamps(const gchar *folder_path, guint
max_items);
static void create_clean_links(const gchar *folder_path, LaunchMenuPlugin
*launch_plugin);
static void update_clean_links(LaunchMenuPlugin *launch_plugin);
static void recent_item_free(RecentItem *item);
static gint compare_recent_items(gconstpointer a, gconstpointer b);
static gint compare_paths_for_processing(gconstpointer a, gconstpointer b);
/* Phase 4 - Application Monitoring via wnck */
static void setup_application_monitoring(LaunchMenuPlugin *launch_plugin);
static void cleanup_application_monitoring(LaunchMenuPlugin *launch_plugin);
static void on_application_opened(WnckScreen *screen, WnckApplication *app,
gpointer user_data);
static void populate_initial_applications(LaunchMenuPlugin *launch_plugin);
static void add_recent_application_from_wnck(LaunchMenuPlugin *launch_plugin,
WnckApplication *app);
// static gchar *search_desktop_dir_for_app(const gchar *dir_path, const gchar *app_name);
// static void create_simple_recent_application_entry(LaunchMenuPlugin *launch_plugin, const gchar *app_name, WnckApplication *app);
static void create_recent_application_entry_with_desktop(LaunchMenuPlugin
*launch_plugin, const gchar *display_name, const gchar *desktop_path);
/* Configuration functions */
static void launch_menu_configure_plugin(XfcePanelPlugin *panel,
LaunchMenuPlugin *plugin);
static void launch_menu_about(XfcePanelPlugin *panel);
static void launch_menu_load_settings(LaunchMenuPlugin *plugin);
static void launch_menu_save_settings(LaunchMenuPlugin *plugin);
static gchar *launch_menu_get_property_name(LaunchMenuPlugin *plugin, const
gchar *property);
/* Disable checkboxes and tracking functions */
static void initialize_tracking(LaunchMenuPlugin *plugin);
static void sensitivity_changer(GtkToggleButton *button, gpointer user_data);
static void kill_tracking(LaunchMenuPlugin *plugin);
/* OPEN CLASSIC LIBRARY*/
/* Application display finder tools for PIDs */
gchar *classlib_get_process_name_from_pid(pid_t pid)
{
gchar *cmdline_path = g_strdup_printf("/proc/%d/cmdline", pid);
gchar *cmdline = NULL;
gsize cmdline_length = 0;
gchar *result = NULL;
if (g_file_get_contents(cmdline_path, &cmdline, &cmdline_length, NULL))
{
if (cmdline_length > 0)
{
/* Extract program name (first null-terminated string) */
gchar *program_name = g_strdup(cmdline);
result = g_path_get_basename(program_name);
g_free(program_name);
}
g_free(cmdline);
}
g_free(cmdline_path);
return result ? result : g_strdup("unknown");
}
gboolean classlib_looks_like_window_title(const gchar *name)
{
return (strlen(name) > 20 || // Too long for app name
strstr(name, " — ") || // Contains document separator
strstr(name, " - ") || // Alternate separator
strchr(name, ':') || // Contains colons
strchr(name, '/')); // Contains paths
}
/* =============================================================================
* APPLICATION NAME RESOLUTION SYSTEM
* 6-tier resolution system extracted from both working projects
* ============================================================================= */
/**
* Validate that a string contains valid UTF-8 encoding.
* Returns a safe fallback if the input is invalid.
*/
const gchar *classlib_ensure_valid_utf8(const gchar *input)
{
if (!input)
{
return "Invalid App Name";
}
if (g_utf8_validate(input, -1, NULL))
{
return input; /* Input is valid UTF-8 */
}
else
{
return "Invalid App Name"; /* Safe fallback for invalid UTF-8 */
}
}
/**
* Get the proper display name for a WnckApplication using 6-tier resolution.
*
* This is the core function that both projects rely on for consistent
* application naming. Extracted from macos9-menu.c get_app_display_name().
*/
const gchar *classlib_get_application_display_name(WnckApplication *app)
{
if (!app)
{
return "Untitled Program";
}
const gchar *name = wnck_application_get_name(app);
/* Handle applications with no name (like Python apps) */
if (!name || strlen(name) == 0)
{
/* Try to get name from first window as fallback */
GList *windows = wnck_application_get_windows(app);
if (windows)
{
WnckWindow *first_window = WNCK_WINDOW(windows->data);
const gchar *window_name = wnck_window_get_name(first_window);
if (window_name && strlen(window_name) > 0)
{
/* Validate UTF-8 before returning */
return classlib_ensure_valid_utf8(window_name);
}
}
/* Last resort fallback */
return "Untitled Program";
}
/* Validate the application name before processing */
name = classlib_ensure_valid_utf8(name);
if (g_strcmp0(name, "Invalid App Name") == 0)
{
return name; /* Return the safe fallback */
}
/* =========================================================================
* TIER 0: Check if the name resembles a window title
* ========================================================================= */
/* TIER 0: PROCESS NAME FALLBACK - Handle window titles masquerading as app names */
if (classlib_looks_like_window_title(name))
{
pid_t pid = wnck_application_get_pid(app);
if (pid > 0)
{
gchar *process_name = classlib_get_process_name_from_pid(pid);
if (process_name && strlen(process_name) > 0 && g_strcmp0(process_name, "unknown") != 0)
{
/* Use process name instead and continue through existing tiers */
name = g_intern_string(process_name);
g_free(process_name);
}
else
{
g_free(process_name);
}
}
}
/* =========================================================================
* TIER 1: MANUAL MAPPING (Highest Priority)
* Specific preferences and edge cases that need exact control
* ========================================================================= */
/* Using case-insensitive comparison for reliability */
if (g_ascii_strcasecmp(name, "Org.mozilla.firefox") == 0)
{
return "Firefox";
}
else if (g_ascii_strcasecmp(name, "google-chrome") == 0)
{
return "Google Chrome";
}
else if (g_ascii_strcasecmp(name, "code") == 0) {
return "Visual Studio Code";
}
else if (g_ascii_strcasecmp(name, "vlc") == 0 || g_ascii_strcasecmp(name, "VLC media player") == 0)
{
return "VLC Media Player";
}
else if (g_ascii_strcasecmp(name, "xfce4-about") == 0)
{
return "About Xfce";
}
else if (g_ascii_strcasecmp(name, "xfce4-appfinder") == 0)
{
return "App Finder";
}
else if (g_str_has_prefix(name, "Soffice") || g_str_has_prefix(name, "soffice"))
{
return "LibreOffice";
}
else if (g_str_has_suffix(name, "- Audacious"))
{
return "Audacious";
}
/* =========================================================================
* TIER 2: XFCE SETTINGS PATTERN
* Handle Xfce4-*-settings applications with proper capitalization
* ========================================================================= */
if (g_str_has_prefix(name, "Xfce4-") && g_str_has_suffix(name, "-settings"))
{
/* Extract the middle part and capitalize it */
const gchar *start = name + 6; /* Skip "Xfce4-" */
const gchar *end = g_strrstr(name, "-settings");
if (end && end > start)
{
gsize len = end - start;
gchar *middle = g_strndup(start, len);
if (middle)
{
/* Capitalize first letter */
if (middle[0] >= 'a' && middle[0] <= 'z')
{
middle[0] = middle[0] - 'a' + 'A';
}
/* Apply Tier 5 logic to handle dashes in the middle part */
if (strchr(middle, '-') != NULL)
{
/* Replace dashes with spaces and capitalize each word */
for (int i = 0; middle[i]; i++)
{
if (middle[i] == '-')
{
middle[i] = ' ';
/* Capitalize letter after space (if exists and is lowercase) */
if (middle[i + 1] >= 'a' && middle[i + 1] <= 'z')
{
middle[i + 1] = middle[i + 1] - 'a' + 'A';
}
}
}
}
/* Use GLib intern string to avoid memory leaks */
const gchar *result = g_intern_string(middle);
g_free(middle); /* Free our temporary string */
return result; /* Return the interned version (managed by GLib) */
}
}
}
/* =========================================================================
* TIER 3: REVERSE DOMAIN PATTERN
* Handle org.*.* and Org.*.* applications
* ========================================================================= */
if (g_str_has_prefix(name, "org.") || g_str_has_prefix(name, "Org."))
{
/* Find the last dot to get the app name */
const gchar *last_dot = g_strrstr(name, ".");
if (last_dot)
{
const gchar *app_name = last_dot + 1; /* Skip the dot */
if (strlen(app_name) > 0)
{
/* Capitalize first letter only */
gchar *capitalized = g_strdup(app_name);
if (capitalized[0] >= 'a' && capitalized[0] <= 'z')
{
capitalized[0] = capitalized[0] - 'a' + 'A';
}
/* Apply Tier 5 logic if there are dashes */
if (strchr(capitalized, '-') != NULL)
{
for (int i = 0; capitalized[i]; i++) {
if (capitalized[i] == '-')
{
capitalized[i] = ' ';
/* Capitalize letter after space */
if (capitalized[i + 1] >= 'a' && capitalized[i + 1] <= 'z')
{
capitalized[i + 1] = capitalized[i + 1] - 'a' + 'A';
}
}
}
}
const gchar *result = g_intern_string(capitalized);
g_free(capitalized);
return result;
}
}
}
/* =========================================================================
* TIER 4: SIMPLE CAPITALIZATION
* Single lowercase words only - capitalize first letter
* ========================================================================= */
/* Check if it's a simple single word (no spaces, dots, dashes) */
if (!strchr(name, ' ') && !strchr(name, '.') && !strchr(name, '-'))
{
/* Check if it's all lowercase */
gboolean is_lowercase = TRUE;
for (const gchar *p = name; *p; p++)
{
if (*p >= 'A' && *p <= 'Z')
{
is_lowercase = FALSE;
break;
}
}
if (is_lowercase && strlen(name) > 0)
{
gchar *capitalized = g_strdup(name);
capitalized[0] = g_ascii_toupper(capitalized[0]);
const gchar *result = g_intern_string(capitalized);
g_free(capitalized);
return result;
}
}
/* =========================================================================
* TIER 5: DASH REPLACEMENT
* Any name with dashes - replace with spaces and capitalize each word
* ========================================================================= */
if (strchr(name, '-') != NULL)
{
gchar *processed = g_strdup(name);
/* Replace dashes with spaces and capitalize each word */
for (int i = 0; processed[i]; i++)
{
if (processed[i] == '-')
{
processed[i] = ' ';
/* Capitalize letter after space (if exists and is lowercase) */
if (processed[i + 1] >= 'a' && processed[i + 1] <= 'z')
{
processed[i + 1] = processed[i + 1] - 'a' + 'A';
}
}
}
/* Also capitalize the first letter if it's lowercase */
if (processed[0] >= 'a' && processed[0] <= 'z')
{
processed[0] = processed[0] - 'a' + 'A';
}
const gchar *result = g_intern_string(processed);
g_free(processed);
return result;
}
/* =========================================================================
* TIER 6: FALLBACK
* Return original name unchanged
* ========================================================================= */
return name;
}
/* =============================================================================
* FILE MANAGER DETECTION AND BLACKLISTING SYSTEM
* Extracted from switcher menu's file manager detection and spatial menu's blacklisting
* ============================================================================= */
/**
* Check if a process name corresponds to a desktop manager.
* Extracted from switcher menu's desktop manager detection logic.
*/
gboolean classlib_is_desktop_manager(const gchar *process_name)
{
if (!process_name)
{
return FALSE;
}
const gchar *desktop_managers[] =
{
"xfdesktop",
"caja", /* caja -n --force-desktop */
"nemo-desktop",
"nautilus-desktop",
"pcmanfm", /* pcmanfm --desktop */
NULL
};
for (int i = 0; desktop_managers[i]; i++)
{
if (g_ascii_strcasecmp(process_name, desktop_managers[i]) == 0)
{
return TRUE;
}
}
return FALSE;
}
/**
* Check if an application should be considered a file manager.
* Combines desktop manager detection and traditional file manager detection.
*/
gboolean classlib_is_file_manager(WnckApplication *app)
{
if (!app)
{
return FALSE;
}
const gchar *app_name = wnck_application_get_name(app);
if (!app_name)
{
return FALSE;
}
/* Check for known file managers */
const gchar *file_managers[] =
{
"caja",
"thunar",
"nemo",
"nautilus",
"pcmanfm",
"dolphin",
"konqueror",
NULL
};
for (int i = 0; file_managers[i]; i++)
{
if (g_ascii_strcasecmp(app_name, file_managers[i]) == 0)
{
return TRUE;
}
}
/* Check for desktop managers */
return classlib_is_desktop_manager(app_name);
}
/**
* Check if an application should be blacklisted from recent document tracking.
* Extracted from spatial menu's is_blacklisted_application() function.
*/
/* Check if application should be blacklisted from recent documents */
gboolean classlib_should_blacklist_application(xmlNode *bookmark_node)
{
/* Applications that primarily download/fetch files rather than edit documents */
const gchar *blacklisted_apps[] =
{
"Firefox",
"firefox",
"Mozilla Firefox",
"Chrome",
"Chromium",
"Google Chrome",
"chromium",
"wget",
"curl",
"Thunderbird",
"thunderbird",
"Transmission",
"qBittorrent",
"aria2c",
"yt-dlp",
"youtube-dl",
NULL
};
/* Look for application metadata in the bookmark */
for (xmlNode *child = bookmark_node->children; child; child = child->next)
{
if (child->type != XML_ELEMENT_NODE)
{
continue;
}
/* Check for info/metadata structure */
if (xmlStrcmp(child->name, (const xmlChar *)"info") == 0)
{
for (xmlNode *info_child = child->children; info_child; info_child = info_child->next)
{
if (info_child->type != XML_ELEMENT_NODE)
{
continue;
}
/* Look for metadata with applications */
if (xmlStrcmp(info_child->name, (const xmlChar *)"metadata") == 0)
{
for (xmlNode *meta_child = info_child->children; meta_child; meta_child = meta_child->next)
{
if (meta_child->type != XML_ELEMENT_NODE)
{
continue;
}
/* Look for bookmark:applications */
if (xmlStrcmp(meta_child->name, (const xmlChar *)"applications") == 0)
{
for (xmlNode *app_child = meta_child->children; app_child; app_child = app_child->next)
{
if (app_child->type != XML_ELEMENT_NODE)
{
continue;
}
/* Check bookmark:application elements */
if (xmlStrcmp(app_child->name, (const xmlChar *)"application") == 0)
{
xmlChar *app_name = xmlGetProp(app_child, (const xmlChar *)"name");
if (app_name)
{
/* Check against blacklist */
for (int i = 0; blacklisted_apps[i]; i++)
{
if (g_ascii_strcasecmp((const gchar *)app_name, blacklisted_apps[i]) == 0)
{
xmlFree(app_name);
return TRUE; /* Blacklisted */
}
}
xmlFree(app_name);
}
}
}
}
}
}
}
}
}
return FALSE; /* Not blacklisted */
}
/**
* Get the default file manager for the system using xdg-mime.
*/
gchar *classlib_get_default_file_manager(void)
{
gchar *output = NULL;
gchar *error = NULL;
gint exit_status;
/* Query the default application for inode/directory */
if (g_spawn_command_line_sync("xdg-mime query default inode/directory",
&output, &error, &exit_status, NULL))
{
if (exit_status == 0 && output && *output)
{
/* Strip newline and .desktop extension */
g_strstrip(output);
if (g_str_has_suffix(output, ".desktop"))
{
gchar *basename = g_path_get_basename(output);
gchar *name = g_strndup(basename, strlen(basename) - 8); /* Remove .desktop */
g_free(basename);
g_free(output);
return name;
}
return output;
}
}
g_free(output);
g_free(error);
/* Fallback detection */
const gchar *fallback_managers[] = {"caja", "thunar", "nemo", "nautilus", NULL};
for (int i = 0; fallback_managers[i]; i++)
{
gchar *path = g_find_program_in_path(fallback_managers[i]);
if (path)
{
g_free(path);
return g_strdup(fallback_managers[i]);
}
}
return NULL;
}
/* =============================================================================
* NATURAL SORTING SYSTEM
* File manager aware string comparison extracted from switcher menu
* ============================================================================= */
/**
* Detect the current system locale type for sorting purposes.
* Follows Linux locale hierarchy: LC_ALL -> LC_COLLATE -> LANG -> "C"
*/
ClassicLocaleType classlib_detect_locale_type(void)
{
const gchar *locale = NULL;
/* Follow the canonical hierarchy */
locale = getenv("LC_ALL");
if (!locale || !*locale)
{
locale = getenv("LC_COLLATE");
if (!locale || !*locale)
{
locale = getenv("LANG");
if (!locale || !*locale)
{
locale = "C"; /* Final fallback */
}
}
}
/* Check if it's C locale */
if (g_strcmp0(locale, "C") == 0 || g_strcmp0(locale, "POSIX") == 0)
{
return CLASSLIB_LOCALE_TYPE_C;
}
/* Everything else is treated as UTF-8 locale */
return CLASSLIB_LOCALE_TYPE_UTF8;
}
/**
* Get special character priority for different file managers.
* Extracted from switcher menu's get_special_char_priority().
*/
gint classlib_get_special_char_priority(const gchar *str, ClassicSortStyle sort_style)
{
if (!str || !*str) return 1; /* Default priority for empty strings */
{
switch (sort_style)
{
case CLASSLIB_SORT_STYLE_CAJA:
/* Caja: Both . and # files go to end */
if (str[0] == '.' || str[0] == '#')
{
return 1; /* Special files last */
}
else
{
return 0; /* Normal files first */
}
case CLASSLIB_SORT_STYLE_THUNAR:
/* Thunar: Only . files get special treatment (go to beginning) */
if (str[0] == '.')
{
return 0; /* Hidden files first */
}
else
{
return 1; /* Everything else (including #) second */
}
case CLASSLIB_SORT_STYLE_UNKNOWN:
default:
/* Default to Caja behavior */
if (str[0] == '.' || str[0] == '#')
{
return 1;
}
else
{
return 0;
}
}
}
}
/**
* File manager aware string comparison.
* Extracted from switcher menu's file_manager_aware_compare().
*/
gint classlib_file_manager_aware_compare(const gchar *a, const gchar *b, ClassicSortStyle sort_style, ClassicLocaleType locale_type)
{
if (!a && !b) return 0;
if (!a) return -1;
if (!b) return 1;
/* Phase 1: Special character priority (different for each file manager) */
gint priority_a = classlib_get_special_char_priority(a, sort_style);
gint priority_b = classlib_get_special_char_priority(b, sort_style);
if (priority_a != priority_b)
{
return priority_a - priority_b;
}
/* Phase 2: Locale-aware comparison */
if (sort_style == CLASSLIB_SORT_STYLE_CAJA && locale_type == CLASSLIB_LOCALE_TYPE_C)
{
/* Only Caja falls back to C locale sorting */
return strcmp(a, b);
}
else
{
/* Thunar always uses UTF-8, Caja uses UTF-8 in UTF-8 locales */
gchar *key_a = g_utf8_collate_key_for_filename(a, -1);
gchar *key_b = g_utf8_collate_key_for_filename(b, -1);
gint result = strcmp(key_a, key_b);
g_free(key_a);
g_free(key_b);
return result;
}
}
/* =============================================================================
* DESKTOP FILE SEARCH SYSTEM
* Extracted from spatial menu's desktop file search logic
* ============================================================================= */
/**
* Parse desktop file for display name and icon.
* Helper function for desktop file searching.
*/
static gboolean parse_desktop_file_for_search(const gchar *desktop_path, gchar **display_name, gchar **icon_name)
{
GKeyFile *key_file = g_key_file_new();
GError *error = NULL;
if (!g_key_file_load_from_file(key_file, desktop_path, G_KEY_FILE_NONE, &error))
{
g_key_file_free(key_file);
if (error) g_error_free(error);
return FALSE;
}
/* Get application name */
gchar *name = g_key_file_get_string(key_file, "Desktop Entry", "Name", NULL);
if (display_name)
{
*display_name = name;
}
else
{
g_free(name);
}
/* Get icon name */
gchar *icon = g_key_file_get_string(key_file, "Desktop Entry", "Icon", NULL);
if (icon_name)
{
*icon_name = icon;
}
else
{
g_free(icon);
}
g_key_file_free(key_file);
return TRUE;
}
/**
* Search a specific directory for desktop files matching an application name.
* Extracted from spatial menu's search_desktop_dir_for_app().
*/
gchar *classlib_search_desktop_directory(const gchar *dir_path, const gchar *app_name)
{
if (!dir_path || !app_name)
{
return NULL;
}
DIR *dir = opendir(dir_path);
if (!dir)
{
return NULL;
}
struct dirent *entry;
gchar *result = NULL;
while ((entry = readdir(dir)) != NULL)
{
if (!g_str_has_suffix(entry->d_name, ".desktop"))
{
continue;
}
gchar *desktop_path = g_build_filename(dir_path, entry->d_name, NULL);
gchar *display_name = NULL;
gchar *icon_name = NULL;
if (parse_desktop_file_for_search(desktop_path, &display_name, &icon_name))
{
if (display_name && g_ascii_strcasecmp(display_name, app_name) == 0)
{
result = g_strdup(desktop_path);
g_free(display_name);
g_free(icon_name);
g_free(desktop_path);
break;
}
g_free(display_name);
g_free(icon_name);
}
g_free(desktop_path);
}
closedir(dir);
return result;
}
/*
* Helper function to assist classlib_find_desktop_file in retrieving executables' desktop files.
*/
static gchar *search_desktop_by_executable(const gchar *dir_path, const gchar *exe_name)
{
DIR *dir = opendir(dir_path);
if (!dir) return NULL;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL)
{
if (!g_str_has_suffix(entry->d_name, ".desktop")) continue;
gchar *desktop_path = g_build_filename(dir_path, entry->d_name, NULL);
GKeyFile *keyfile = g_key_file_new();
if (g_key_file_load_from_file(keyfile, desktop_path, G_KEY_FILE_NONE, NULL))
{
gchar *exec = g_key_file_get_string(keyfile, "Desktop Entry", "Exec", NULL);
if (exec && strstr(exec, exe_name))
{
g_free(exec);
g_key_file_free(keyfile);
closedir(dir);
return desktop_path;
}
g_free(exec);
}
g_key_file_free(keyfile);
g_free(desktop_path);
}
closedir(dir);
return NULL;
}
/**
* Find desktop file for an application by name.
* Extracted from spatial menu's find_desktop_file_for_application().
*/
gchar *classlib_find_desktop_file(const gchar *app_name, WnckApplication *app)
{
if (!app_name) return NULL;
const gchar *desktop_dirs[] =
{
"/usr/share/applications",
"/usr/local/share/applications",
NULL
};
/* Try search variations */
gchar *search_names[4];
search_names[0] = g_strdup(app_name);
search_names[1] = g_ascii_strdown(app_name, -1);
search_names[2] = g_strdelimit(g_ascii_strdown(app_name, -1), " ", '-');