/
gdesktopappinfo.c
4713 lines (3962 loc) · 136 KB
/
gdesktopappinfo.c
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
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2006-2007 Red Hat, Inc.
* Copyright © 2007 Ryan Lortie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Alexander Larsson <alexl@redhat.com>
* Ryan Lortie <desrt@desrt.ca>
*/
/* Prelude {{{1 */
#include "config.h"
#include <errno.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_CRT_EXTERNS_H
#include <crt_externs.h>
#endif
#include "gcontenttypeprivate.h"
#include "gdesktopappinfo.h"
#ifdef G_OS_UNIX
#include "glib-unix.h"
#endif
#include "gfile.h"
#include "gioerror.h"
#include "gthemedicon.h"
#include "gfileicon.h"
#include <glib/gstdio.h>
#include "glibintl.h"
#include "giomodule-priv.h"
#include "gappinfo.h"
#include "gappinfoprivate.h"
#include "glocalfilemonitor.h"
#ifdef G_OS_UNIX
#include "gdocumentportal.h"
#endif
/**
* SECTION:gdesktopappinfo
* @title: GDesktopAppInfo
* @short_description: Application information from desktop files
* @include: gio/gdesktopappinfo.h
*
* #GDesktopAppInfo is an implementation of #GAppInfo based on
* desktop files.
*
* Note that `<gio/gdesktopappinfo.h>` belongs to the UNIX-specific
* GIO interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
* file when using it.
*/
#define DEFAULT_APPLICATIONS_GROUP "Default Applications"
#define ADDED_ASSOCIATIONS_GROUP "Added Associations"
#define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
#define MIME_CACHE_GROUP "MIME Cache"
#define GENERIC_NAME_KEY "GenericName"
#define FULL_NAME_KEY "X-GNOME-FullName"
#define KEYWORDS_KEY "Keywords"
#define STARTUP_WM_CLASS_KEY "StartupWMClass"
enum {
PROP_0,
PROP_FILENAME
};
static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
GError **error);
/**
* GDesktopAppInfo:
*
* Information about an installed application from a desktop file.
*/
struct _GDesktopAppInfo
{
GObject parent_instance;
char *desktop_id;
char *filename;
char *app_id;
GKeyFile *keyfile;
char *name;
char *generic_name;
char *fullname;
char *comment;
char *icon_name;
GIcon *icon;
char **keywords;
char **only_show_in;
char **not_show_in;
char *try_exec;
char *exec;
char *binary;
char *path;
char *categories;
char *startup_wm_class;
char **mime_types;
char **actions;
guint nodisplay : 1;
guint hidden : 1;
guint terminal : 1;
guint startup_notify : 1;
guint no_fuse : 1;
};
typedef enum {
UPDATE_MIME_NONE = 1 << 0,
UPDATE_MIME_SET_DEFAULT = 1 << 1,
UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
UPDATE_MIME_REMOVE = 1 << 3,
UPDATE_MIME_SET_LAST_USED = 1 << 4,
} UpdateMimeFlags;
G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO, g_desktop_app_info_iface_init))
/* DesktopFileDir implementation {{{1 */
typedef struct
{
gchar *path;
gchar *alternatively_watching;
gboolean is_config;
gboolean is_setup;
GFileMonitor *monitor;
GHashTable *app_names;
GHashTable *mime_tweaks;
GHashTable *memory_index;
GHashTable *memory_implementations;
} DesktopFileDir;
static DesktopFileDir *desktop_file_dirs;
static guint n_desktop_file_dirs;
static const guint desktop_file_dir_user_config_index = 0;
static guint desktop_file_dir_user_data_index;
static GMutex desktop_file_dir_lock;
static const gchar *gio_launch_desktop_path = NULL;
/* Monitor 'changed' signal handler {{{2 */
static void desktop_file_dir_reset (DesktopFileDir *dir);
/*< internal >
* desktop_file_dir_get_alternative_dir:
* @dir: a #DesktopFileDir
*
* Gets the "alternative" directory to monitor in case the path
* doesn't exist.
*
* If the path exists this will return NULL, otherwise it will return a
* parent directory of the path.
*
* This is used to avoid inotify on a non-existent directory (which
* results in polling).
*
* See https://bugzilla.gnome.org/show_bug.cgi?id=522314 for more info.
*/
static gchar *
desktop_file_dir_get_alternative_dir (DesktopFileDir *dir)
{
gchar *parent;
/* If the directory itself exists then we need no alternative. */
if (g_access (dir->path, R_OK | X_OK) == 0)
return NULL;
/* Otherwise, try the parent directories until we find one. */
parent = g_path_get_dirname (dir->path);
while (g_access (parent, R_OK | X_OK) != 0)
{
gchar *tmp = parent;
parent = g_path_get_dirname (tmp);
/* If somehow we get to '/' or '.' then just stop... */
if (g_str_equal (parent, tmp))
{
g_free (tmp);
break;
}
g_free (tmp);
}
return parent;
}
static void
desktop_file_dir_changed (GFileMonitor *monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event_type,
gpointer user_data)
{
DesktopFileDir *dir = user_data;
gboolean do_nothing = FALSE;
/* We are not interested in receiving notifications forever just
* because someone asked about one desktop file once.
*
* After we receive the first notification, reset the dir, destroying
* the monitor. We will take this as a hint, next time that we are
* asked, that we need to check if everything is up to date.
*
* If this is a notification for a parent directory (because the
* desktop directory didn't exist) then we shouldn't fire the signal
* unless something actually changed.
*/
g_mutex_lock (&desktop_file_dir_lock);
if (dir->alternatively_watching)
{
gchar *alternative_dir;
alternative_dir = desktop_file_dir_get_alternative_dir (dir);
do_nothing = alternative_dir && g_str_equal (dir->alternatively_watching, alternative_dir);
g_free (alternative_dir);
}
if (!do_nothing)
desktop_file_dir_reset (dir);
g_mutex_unlock (&desktop_file_dir_lock);
/* Notify anyone else who may be interested */
if (!do_nothing)
g_app_info_monitor_fire ();
}
/* Internal utility functions {{{2 */
/*< internal >
* desktop_file_dir_app_name_is_masked:
* @dir: a #DesktopFileDir
* @app_name: an application ID
*
* Checks if @app_name is masked for @dir.
*
* An application is masked if a similarly-named desktop file exists in
* a desktop file directory with higher precedence. Masked desktop
* files should be ignored.
*/
static gboolean
desktop_file_dir_app_name_is_masked (DesktopFileDir *dir,
const gchar *app_name)
{
while (dir > desktop_file_dirs)
{
dir--;
if (dir->app_names && g_hash_table_contains (dir->app_names, app_name))
return TRUE;
}
return FALSE;
}
static const gchar * const *
get_lowercase_current_desktops (void)
{
static gchar **result;
if (g_once_init_enter (&result))
{
const gchar *envvar;
gchar **tmp;
envvar = g_getenv ("XDG_CURRENT_DESKTOP");
if (envvar)
{
gint i, j;
tmp = g_strsplit (envvar, G_SEARCHPATH_SEPARATOR_S, 0);
for (i = 0; tmp[i]; i++)
for (j = 0; tmp[i][j]; j++)
tmp[i][j] = g_ascii_tolower (tmp[i][j]);
}
else
tmp = g_new0 (gchar *, 0 + 1);
g_once_init_leave (&result, tmp);
}
return (const gchar **) result;
}
static const gchar * const *
get_current_desktops (const gchar *value)
{
static gchar **result;
if (g_once_init_enter (&result))
{
gchar **tmp;
if (!value)
value = g_getenv ("XDG_CURRENT_DESKTOP");
if (!value)
value = "";
tmp = g_strsplit (value, ":", 0);
g_once_init_leave (&result, tmp);
}
return (const gchar **) result;
}
/*< internal >
* add_to_table_if_appropriate:
* @apps: a string to GDesktopAppInfo hash table
* @app_name: the name of the application
* @info: a #GDesktopAppInfo, or NULL
*
* If @info is non-%NULL and non-hidden, then add it to @apps, using
* @app_name as a key.
*
* If @info is non-%NULL then this function will consume the passed-in
* reference.
*/
static void
add_to_table_if_appropriate (GHashTable *apps,
const gchar *app_name,
GDesktopAppInfo *info)
{
if (!info)
return;
if (info->hidden)
{
g_object_unref (info);
return;
}
g_free (info->desktop_id);
info->desktop_id = g_strdup (app_name);
g_hash_table_insert (apps, g_strdup (info->desktop_id), info);
}
enum
{
DESKTOP_KEY_Comment,
DESKTOP_KEY_Exec,
DESKTOP_KEY_GenericName,
DESKTOP_KEY_Keywords,
DESKTOP_KEY_Name,
DESKTOP_KEY_X_GNOME_FullName,
N_DESKTOP_KEYS
};
const gchar desktop_key_match_category[N_DESKTOP_KEYS] = {
/* Note: lower numbers are a better match.
*
* In case we want two keys to match at the same level, we can just
* use the same number for the two different keys.
*/
[DESKTOP_KEY_Name] = 1,
[DESKTOP_KEY_Exec] = 2,
[DESKTOP_KEY_Keywords] = 3,
[DESKTOP_KEY_GenericName] = 4,
[DESKTOP_KEY_X_GNOME_FullName] = 5,
[DESKTOP_KEY_Comment] = 6
};
/* Common prefix commands to ignore from Exec= lines */
const char * const exec_key_match_blacklist[] = {
"bash",
"env",
"flatpak",
"gjs",
"pkexec",
"python",
"python2",
"python3",
"sh",
"wine",
"wine64",
NULL
};
static gchar *
desktop_key_get_name (guint key_id)
{
switch (key_id)
{
case DESKTOP_KEY_Comment:
return "Comment";
case DESKTOP_KEY_Exec:
return "Exec";
case DESKTOP_KEY_GenericName:
return GENERIC_NAME_KEY;
case DESKTOP_KEY_Keywords:
return KEYWORDS_KEY;
case DESKTOP_KEY_Name:
return "Name";
case DESKTOP_KEY_X_GNOME_FullName:
return FULL_NAME_KEY;
default:
g_assert_not_reached ();
}
}
/* Search global state {{{2
*
* We only ever search under a global lock, so we can use (and reuse)
* some global data to reduce allocations made while searching.
*
* In short, we keep around arrays of results that we expand as needed
* (and never shrink).
*
* static_token_results: this is where we append the results for each
* token within a given desktop directory, as we handle it (which is
* a union of all matches for this term)
*
* static_search_results: this is where we build the complete results
* for a single directory (which is an intersection of the matches
* found for each term)
*
* static_total_results: this is where we build the complete results
* across all directories (which is a union of the matches found in
* each directory)
*
* The app_names that enter these tables are always pointer-unique (in
* the sense that string equality is the same as pointer equality).
* This can be guaranteed for two reasons:
*
* - we mask appids so that a given appid will only ever appear within
* the highest-precedence directory that contains it. We never
* return search results from a lower-level directory if a desktop
* file exists in a higher-level one.
*
* - within a given directory, the string is unique because it's the
* key in the hashtable of all app_ids for that directory.
*
* We perform a merging of the results in merge_token_results(). This
* works by ordering the two lists and moving through each of them (at
* the same time) looking for common elements, rejecting uncommon ones.
* "Order" here need not mean any particular thing, as long as it is
* some order. Because of the uniqueness of our strings, we can use
* pointer order. That's what's going on in compare_results() below.
*/
struct search_result
{
const gchar *app_name;
gint category;
};
static struct search_result *static_token_results;
static gint static_token_results_size;
static gint static_token_results_allocated;
static struct search_result *static_search_results;
static gint static_search_results_size;
static gint static_search_results_allocated;
static struct search_result *static_total_results;
static gint static_total_results_size;
static gint static_total_results_allocated;
/* And some functions for performing nice operations against it */
static gint
compare_results (gconstpointer a,
gconstpointer b)
{
const struct search_result *ra = a;
const struct search_result *rb = b;
if (ra->app_name < rb->app_name)
return -1;
else if (ra->app_name > rb->app_name)
return 1;
else
return ra->category - rb->category;
}
static gint
compare_categories (gconstpointer a,
gconstpointer b)
{
const struct search_result *ra = a;
const struct search_result *rb = b;
return ra->category - rb->category;
}
static void
add_token_result (const gchar *app_name,
guint16 category)
{
if G_UNLIKELY (static_token_results_size == static_token_results_allocated)
{
static_token_results_allocated = MAX (16, static_token_results_allocated * 2);
static_token_results = g_renew (struct search_result, static_token_results, static_token_results_allocated);
}
static_token_results[static_token_results_size].app_name = app_name;
static_token_results[static_token_results_size].category = category;
static_token_results_size++;
}
static void
merge_token_results (gboolean first)
{
if (static_token_results_size != 0)
qsort (static_token_results, static_token_results_size, sizeof (struct search_result), compare_results);
/* If this is the first token then we are basically merging a list with
* itself -- we only perform de-duplication.
*
* If this is not the first token then we are doing a real merge.
*/
if (first)
{
const gchar *last_name = NULL;
gint i;
/* We must de-duplicate, but we do so by taking the best category
* in each case.
*
* The final list can be as large as the input here, so make sure
* we have enough room (even if it's too much room).
*/
if G_UNLIKELY (static_search_results_allocated < static_token_results_size)
{
static_search_results_allocated = static_token_results_allocated;
static_search_results = g_renew (struct search_result,
static_search_results,
static_search_results_allocated);
}
for (i = 0; i < static_token_results_size; i++)
{
/* The list is sorted so that the best match for a given id
* will be at the front, so once we have copied an id, skip
* the rest of the entries for the same id.
*/
if (static_token_results[i].app_name == last_name)
continue;
last_name = static_token_results[i].app_name;
static_search_results[static_search_results_size++] = static_token_results[i];
}
}
else
{
const gchar *last_name = NULL;
gint i, j = 0;
gint k = 0;
/* We only ever remove items from the results list, so no need to
* resize to ensure that we have enough room.
*/
for (i = 0; i < static_token_results_size; i++)
{
if (static_token_results[i].app_name == last_name)
continue;
last_name = static_token_results[i].app_name;
/* Now we only want to have a result in static_search_results
* if we already have it there *and* we have it in
* static_token_results as well. The category will be the
* lesser of the two.
*
* Skip past the results in static_search_results that are not
* going to be matches.
*/
while (k < static_search_results_size &&
static_search_results[k].app_name < static_token_results[i].app_name)
k++;
if (k < static_search_results_size &&
static_search_results[k].app_name == static_token_results[i].app_name)
{
/* We have a match.
*
* Category should be the worse of the two (ie:
* numerically larger).
*/
static_search_results[j].app_name = static_search_results[k].app_name;
static_search_results[j].category = MAX (static_search_results[k].category,
static_token_results[i].category);
j++;
}
}
static_search_results_size = j;
}
/* Clear it out for next time... */
static_token_results_size = 0;
}
static void
reset_total_search_results (void)
{
static_total_results_size = 0;
}
static void
sort_total_search_results (void)
{
if (static_total_results_size != 0)
qsort (static_total_results, static_total_results_size, sizeof (struct search_result), compare_categories);
}
static void
merge_directory_results (void)
{
if G_UNLIKELY (static_total_results_size + static_search_results_size > static_total_results_allocated)
{
static_total_results_allocated = MAX (16, static_total_results_allocated);
while (static_total_results_allocated < static_total_results_size + static_search_results_size)
static_total_results_allocated *= 2;
static_total_results = g_renew (struct search_result, static_total_results, static_total_results_allocated);
}
if (static_total_results + static_total_results_size != 0)
memcpy (static_total_results + static_total_results_size,
static_search_results,
static_search_results_size * sizeof (struct search_result));
static_total_results_size += static_search_results_size;
/* Clear it out for next time... */
static_search_results_size = 0;
}
/* Support for unindexed DesktopFileDirs {{{2 */
static void
get_apps_from_dir (GHashTable **apps,
const char *dirname,
const char *prefix)
{
const char *basename;
GDir *dir;
dir = g_dir_open (dirname, 0, NULL);
if (dir == NULL)
return;
while ((basename = g_dir_read_name (dir)) != NULL)
{
gchar *filename;
filename = g_build_filename (dirname, basename, NULL);
if (g_str_has_suffix (basename, ".desktop"))
{
gchar *app_name;
app_name = g_strconcat (prefix, basename, NULL);
if (*apps == NULL)
*apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_insert (*apps, app_name, g_strdup (filename));
}
else if (g_file_test (filename, G_FILE_TEST_IS_DIR))
{
gchar *subprefix;
subprefix = g_strconcat (prefix, basename, "-", NULL);
get_apps_from_dir (apps, filename, subprefix);
g_free (subprefix);
}
g_free (filename);
}
g_dir_close (dir);
}
typedef struct
{
gchar **additions;
gchar **removals;
gchar **defaults;
} UnindexedMimeTweaks;
static void
free_mime_tweaks (gpointer data)
{
UnindexedMimeTweaks *tweaks = data;
g_strfreev (tweaks->additions);
g_strfreev (tweaks->removals);
g_strfreev (tweaks->defaults);
g_slice_free (UnindexedMimeTweaks, tweaks);
}
static UnindexedMimeTweaks *
desktop_file_dir_unindexed_get_tweaks (DesktopFileDir *dir,
const gchar *mime_type)
{
UnindexedMimeTweaks *tweaks;
gchar *unaliased_type;
unaliased_type = _g_unix_content_type_unalias (mime_type);
tweaks = g_hash_table_lookup (dir->mime_tweaks, unaliased_type);
if (tweaks == NULL)
{
tweaks = g_slice_new0 (UnindexedMimeTweaks);
g_hash_table_insert (dir->mime_tweaks, unaliased_type, tweaks);
}
else
g_free (unaliased_type);
return tweaks;
}
/* consumes 'to_add' */
static void
expand_strv (gchar ***strv_ptr,
gchar **to_add,
gchar * const *blacklist)
{
guint strv_len, add_len;
gchar **strv;
guint i, j;
if (!*strv_ptr)
{
*strv_ptr = to_add;
return;
}
strv = *strv_ptr;
strv_len = g_strv_length (strv);
add_len = g_strv_length (to_add);
strv = g_renew (gchar *, strv, strv_len + add_len + 1);
for (i = 0; to_add[i]; i++)
{
/* Don't add blacklisted strings */
if (blacklist)
for (j = 0; blacklist[j]; j++)
if (g_str_equal (to_add[i], blacklist[j]))
goto no_add;
/* Don't add duplicates already in the list */
for (j = 0; j < strv_len; j++)
if (g_str_equal (to_add[i], strv[j]))
goto no_add;
strv[strv_len++] = to_add[i];
continue;
no_add:
g_free (to_add[i]);
}
strv[strv_len] = NULL;
*strv_ptr = strv;
g_free (to_add);
}
static void
desktop_file_dir_unindexed_read_mimeapps_list (DesktopFileDir *dir,
const gchar *filename,
const gchar *added_group,
gboolean tweaks_permitted)
{
UnindexedMimeTweaks *tweaks;
char **desktop_file_ids;
GKeyFile *key_file;
gchar **mime_types;
int i;
key_file = g_key_file_new ();
if (!g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL))
{
g_key_file_free (key_file);
return;
}
mime_types = g_key_file_get_keys (key_file, added_group, NULL, NULL);
if G_UNLIKELY (mime_types != NULL && !tweaks_permitted)
{
g_warning ("%s contains a [%s] group, but it is not permitted here. Only the non-desktop-specific "
"mimeapps.list file may add or remove associations.", filename, added_group);
g_strfreev (mime_types);
mime_types = NULL;
}
if (mime_types != NULL)
{
for (i = 0; mime_types[i] != NULL; i++)
{
desktop_file_ids = g_key_file_get_string_list (key_file, added_group, mime_types[i], NULL, NULL);
if (desktop_file_ids)
{
tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
expand_strv (&tweaks->additions, desktop_file_ids, tweaks->removals);
}
}
g_strfreev (mime_types);
}
mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
if G_UNLIKELY (mime_types != NULL && !tweaks_permitted)
{
g_warning ("%s contains a [%s] group, but it is not permitted here. Only the non-desktop-specific "
"mimeapps.list file may add or remove associations.", filename, REMOVED_ASSOCIATIONS_GROUP);
g_strfreev (mime_types);
mime_types = NULL;
}
if (mime_types != NULL)
{
for (i = 0; mime_types[i] != NULL; i++)
{
desktop_file_ids = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP, mime_types[i], NULL, NULL);
if (desktop_file_ids)
{
tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
expand_strv (&tweaks->removals, desktop_file_ids, tweaks->additions);
}
}
g_strfreev (mime_types);
}
mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
if (mime_types != NULL)
{
for (i = 0; mime_types[i] != NULL; i++)
{
desktop_file_ids = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP, mime_types[i], NULL, NULL);
if (desktop_file_ids)
{
tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
expand_strv (&tweaks->defaults, desktop_file_ids, NULL);
}
}
g_strfreev (mime_types);
}
g_key_file_free (key_file);
}
static void
desktop_file_dir_unindexed_read_mimeapps_lists (DesktopFileDir *dir)
{
const gchar * const *desktops;
gchar *filename;
gint i;
dir->mime_tweaks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_mime_tweaks);
/* We process in order of precedence, using a blacklisting approach to
* avoid recording later instructions that conflict with ones we found
* earlier.
*
* We first start with the XDG_CURRENT_DESKTOP files, in precedence
* order.
*/
desktops = get_lowercase_current_desktops ();
for (i = 0; desktops[i]; i++)
{
filename = g_strdup_printf ("%s/%s-mimeapps.list", dir->path, desktops[i]);
desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, FALSE);
g_free (filename);
}
/* Next, the non-desktop-specific mimeapps.list */
filename = g_strdup_printf ("%s/mimeapps.list", dir->path);
desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, TRUE);
g_free (filename);
/* The remaining files are only checked for in directories that might
* contain desktop files (ie: not the config dirs).
*/
if (dir->is_config)
return;
/* We have 'defaults.list' which was only ever understood by GLib. It
* exists widely, but it has never been part of any spec and it should
* be treated as deprecated. This will be removed in a future
* version.
*/
filename = g_strdup_printf ("%s/defaults.list", dir->path);
desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, FALSE);
g_free (filename);
/* Finally, the mimeinfo.cache, which is just a cached copy of what we
* would find in the MimeTypes= lines of all of the desktop files.
*/
filename = g_strdup_printf ("%s/mimeinfo.cache", dir->path);
desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, MIME_CACHE_GROUP, TRUE);
g_free (filename);
}
static void
desktop_file_dir_unindexed_init (DesktopFileDir *dir)
{
if (!dir->is_config)
get_apps_from_dir (&dir->app_names, dir->path, "");
desktop_file_dir_unindexed_read_mimeapps_lists (dir);
}
static GDesktopAppInfo *
desktop_file_dir_unindexed_get_app (DesktopFileDir *dir,
const gchar *desktop_id)
{
const gchar *filename;
filename = g_hash_table_lookup (dir->app_names, desktop_id);
if (!filename)
return NULL;
return g_desktop_app_info_new_from_filename (filename);
}
static void
desktop_file_dir_unindexed_get_all (DesktopFileDir *dir,
GHashTable *apps)
{
GHashTableIter iter;
gpointer app_name;
gpointer filename;
if (dir->app_names == NULL)
return;
g_hash_table_iter_init (&iter, dir->app_names);
while (g_hash_table_iter_next (&iter, &app_name, &filename))
{
if (desktop_file_dir_app_name_is_masked (dir, app_name))
continue;
add_to_table_if_appropriate (apps, app_name, g_desktop_app_info_new_from_filename (filename));
}
}
typedef struct _MemoryIndexEntry MemoryIndexEntry;
typedef GHashTable MemoryIndex;
struct _MemoryIndexEntry
{
const gchar *app_name; /* pointer to the hashtable key */
gint match_category;
MemoryIndexEntry *next;
};
static void
memory_index_entry_free (gpointer data)
{
MemoryIndexEntry *mie = data;
while (mie)
{
MemoryIndexEntry *next = mie->next;
g_slice_free (MemoryIndexEntry, mie);
mie = next;
}
}