1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright © 2007 Ryan Lortie
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Author: Alexander Larsson <alexl@redhat.com>
20 * Ryan Lortie <desrt@desrt.ca>
31 #ifdef HAVE_CRT_EXTERNS_H
32 #include <crt_externs.h>
35 #include "gcontenttypeprivate.h"
36 #include "gdesktopappinfo.h"
38 #include "glib-unix.h"
42 #include "gthemedicon.h"
43 #include "gfileicon.h"
44 #include <glib/gstdio.h>
46 #include "giomodule-priv.h"
48 #include "gappinfoprivate.h"
49 #include "glocaldirectorymonitor.h"
53 * SECTION:gdesktopappinfo
54 * @title: GDesktopAppInfo
55 * @short_description: Application information from desktop files
56 * @include: gio/gdesktopappinfo.h
58 * #GDesktopAppInfo is an implementation of #GAppInfo based on
61 * Note that `<gio/gdesktopappinfo.h>` belongs to the UNIX-specific
62 * GIO interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
66 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
67 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
68 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
69 #define MIME_CACHE_GROUP "MIME Cache"
70 #define GENERIC_NAME_KEY "GenericName"
71 #define FULL_NAME_KEY "X-GNOME-FullName"
72 #define KEYWORDS_KEY "Keywords"
73 #define STARTUP_WM_CLASS_KEY "StartupWMClass"
80 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
81 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
87 * Information about an installed application from a desktop file.
89 struct _GDesktopAppInfo
91 GObject parent_instance;
113 char *startup_wm_class;
120 guint startup_notify : 1;
125 UPDATE_MIME_NONE = 1 << 0,
126 UPDATE_MIME_SET_DEFAULT = 1 << 1,
127 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
128 UPDATE_MIME_REMOVE = 1 << 3,
129 UPDATE_MIME_SET_LAST_USED = 1 << 4,
132 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
133 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO, g_desktop_app_info_iface_init))
135 /* DesktopFileDir implementation {{{1 */
140 gchar *alternatively_watching;
143 GLocalDirectoryMonitor *monitor;
144 GHashTable *app_names;
145 GHashTable *mime_tweaks;
146 GHashTable *memory_index;
147 GHashTable *memory_implementations;
150 static DesktopFileDir *desktop_file_dirs;
151 static guint n_desktop_file_dirs;
152 static const guint desktop_file_dir_user_config_index = 0;
153 static guint desktop_file_dir_user_data_index;
154 static GMutex desktop_file_dir_lock;
156 /* Monitor 'changed' signal handler {{{2 */
157 static void desktop_file_dir_reset (DesktopFileDir *dir);
160 * desktop_file_dir_get_alternative_dir:
161 * @dir: a #DesktopFileDir
163 * Gets the "alternative" directory to monitor in case the path
166 * If the path exists this will return NULL, otherwise it will return a
167 * parent directory of the path.
169 * This is used to avoid inotify on a non-existent directory (which
170 * results in polling).
172 * See https://bugzilla.gnome.org/show_bug.cgi?id=522314 for more info.
175 desktop_file_dir_get_alternative_dir (DesktopFileDir *dir)
179 /* If the directory itself exists then we need no alternative. */
180 if (g_access (dir->path, R_OK | X_OK) == 0)
183 /* Otherwise, try the parent directories until we find one. */
184 parent = g_path_get_dirname (dir->path);
186 while (g_access (parent, R_OK | X_OK) != 0)
190 parent = g_path_get_dirname (tmp);
192 /* If somehow we get to '/' or '.' then just stop... */
193 if (g_str_equal (parent, tmp))
206 desktop_file_dir_changed (GFileMonitor *monitor,
209 GFileMonitorEvent event_type,
212 DesktopFileDir *dir = user_data;
213 gboolean do_nothing = FALSE;
215 /* We are not interested in receiving notifications forever just
216 * because someone asked about one desktop file once.
218 * After we receive the first notification, reset the dir, destroying
219 * the monitor. We will take this as a hint, next time that we are
220 * asked, that we need to check if everything is up to date.
222 * If this is a notification for a parent directory (because the
223 * desktop directory didn't exist) then we shouldn't fire the signal
224 * unless something actually changed.
226 g_mutex_lock (&desktop_file_dir_lock);
228 if (dir->alternatively_watching)
230 gchar *alternative_dir;
232 alternative_dir = desktop_file_dir_get_alternative_dir (dir);
233 do_nothing = alternative_dir && g_str_equal (dir->alternatively_watching, alternative_dir);
234 g_free (alternative_dir);
238 desktop_file_dir_reset (dir);
240 g_mutex_unlock (&desktop_file_dir_lock);
242 /* Notify anyone else who may be interested */
244 g_app_info_monitor_fire ();
247 /* Internal utility functions {{{2 */
250 * desktop_file_dir_app_name_is_masked:
251 * @dir: a #DesktopFileDir
252 * @app_name: an application ID
254 * Checks if @app_name is masked for @dir.
256 * An application is masked if a similarly-named desktop file exists in
257 * a desktop file directory with higher precedence. Masked desktop
258 * files should be ignored.
261 desktop_file_dir_app_name_is_masked (DesktopFileDir *dir,
262 const gchar *app_name)
264 while (dir > desktop_file_dirs)
268 if (dir->app_names && g_hash_table_contains (dir->app_names, app_name))
275 static const gchar * const *
276 get_lowercase_current_desktops (void)
278 static gchar **result;
280 if (g_once_init_enter (&result))
285 envvar = g_getenv ("XDG_CURRENT_DESKTOP");
291 tmp = g_strsplit (envvar, G_SEARCHPATH_SEPARATOR_S, 0);
293 for (i = 0; tmp[i]; i++)
294 for (j = 0; tmp[i][j]; j++)
295 tmp[i][j] = g_ascii_tolower (tmp[i][j]);
298 tmp = g_new0 (gchar *, 0 + 1);
300 g_once_init_leave (&result, tmp);
303 return (const gchar **) result;
306 static const gchar * const *
307 get_current_desktops (const gchar *value)
309 static gchar **result;
311 if (g_once_init_enter (&result))
316 value = g_getenv ("XDG_CURRENT_DESKTOP");
321 tmp = g_strsplit (value, ":", 0);
323 g_once_init_leave (&result, tmp);
326 return (const gchar **) result;
330 * add_to_table_if_appropriate:
331 * @apps: a string to GDesktopAppInfo hash table
332 * @app_name: the name of the application
333 * @info: a #GDesktopAppInfo, or NULL
335 * If @info is non-%NULL and non-hidden, then add it to @apps, using
336 * @app_name as a key.
338 * If @info is non-%NULL then this function will consume the passed-in
342 add_to_table_if_appropriate (GHashTable *apps,
343 const gchar *app_name,
344 GDesktopAppInfo *info)
351 g_object_unref (info);
355 g_free (info->desktop_id);
356 info->desktop_id = g_strdup (app_name);
358 g_hash_table_insert (apps, g_strdup (info->desktop_id), info);
365 DESKTOP_KEY_GenericName,
366 DESKTOP_KEY_Keywords,
368 DESKTOP_KEY_X_GNOME_FullName,
373 const gchar desktop_key_match_category[N_DESKTOP_KEYS] = {
374 /* Note: lower numbers are a better match.
376 * In case we want two keys to match at the same level, we can just
377 * use the same number for the two different keys.
379 [DESKTOP_KEY_Name] = 1,
380 [DESKTOP_KEY_Exec] = 2,
381 [DESKTOP_KEY_Keywords] = 3,
382 [DESKTOP_KEY_GenericName] = 4,
383 [DESKTOP_KEY_X_GNOME_FullName] = 5,
384 [DESKTOP_KEY_Comment] = 6
388 desktop_key_get_name (guint key_id)
392 case DESKTOP_KEY_Comment:
394 case DESKTOP_KEY_Exec:
396 case DESKTOP_KEY_GenericName:
397 return GENERIC_NAME_KEY;
398 case DESKTOP_KEY_Keywords:
400 case DESKTOP_KEY_Name:
402 case DESKTOP_KEY_X_GNOME_FullName:
403 return FULL_NAME_KEY;
405 g_assert_not_reached ();
409 /* Search global state {{{2
411 * We only ever search under a global lock, so we can use (and reuse)
412 * some global data to reduce allocations made while searching.
414 * In short, we keep around arrays of results that we expand as needed
415 * (and never shrink).
417 * static_token_results: this is where we append the results for each
418 * token within a given desktop directory, as we handle it (which is
419 * a union of all matches for this term)
421 * static_search_results: this is where we build the complete results
422 * for a single directory (which is an intersection of the matches
423 * found for each term)
425 * static_total_results: this is where we build the complete results
426 * across all directories (which is a union of the matches found in
429 * The app_names that enter these tables are always pointer-unique (in
430 * the sense that string equality is the same as pointer equality).
431 * This can be guaranteed for two reasons:
433 * - we mask appids so that a given appid will only ever appear within
434 * the highest-precedence directory that contains it. We never
435 * return search results from a lower-level directory if a desktop
436 * file exists in a higher-level one.
438 * - within a given directory, the string is unique because it's the
439 * key in the hashtable of all app_ids for that directory.
441 * We perform a merging of the results in merge_token_results(). This
442 * works by ordering the two lists and moving through each of them (at
443 * the same time) looking for common elements, rejecting uncommon ones.
444 * "Order" here need not mean any particular thing, as long as it is
445 * some order. Because of the uniqueness of our strings, we can use
446 * pointer order. That's what's going on in compare_results() below.
450 const gchar *app_name;
454 static struct search_result *static_token_results;
455 static gint static_token_results_size;
456 static gint static_token_results_allocated;
457 static struct search_result *static_search_results;
458 static gint static_search_results_size;
459 static gint static_search_results_allocated;
460 static struct search_result *static_total_results;
461 static gint static_total_results_size;
462 static gint static_total_results_allocated;
464 /* And some functions for performing nice operations against it */
466 compare_results (gconstpointer a,
469 const struct search_result *ra = a;
470 const struct search_result *rb = b;
472 if (ra->app_name < rb->app_name)
475 else if (ra->app_name > rb->app_name)
479 return ra->category - rb->category;
483 compare_categories (gconstpointer a,
486 const struct search_result *ra = a;
487 const struct search_result *rb = b;
489 return ra->category - rb->category;
493 add_token_result (const gchar *app_name,
496 if G_UNLIKELY (static_token_results_size == static_token_results_allocated)
498 static_token_results_allocated = MAX (16, static_token_results_allocated * 2);
499 static_token_results = g_renew (struct search_result, static_token_results, static_token_results_allocated);
502 static_token_results[static_token_results_size].app_name = app_name;
503 static_token_results[static_token_results_size].category = category;
504 static_token_results_size++;
508 merge_token_results (gboolean first)
510 qsort (static_token_results, static_token_results_size, sizeof (struct search_result), compare_results);
512 /* If this is the first token then we are basically merging a list with
513 * itself -- we only perform de-duplication.
515 * If this is not the first token then we are doing a real merge.
519 const gchar *last_name = NULL;
522 /* We must de-duplicate, but we do so by taking the best category
525 * The final list can be as large as the input here, so make sure
526 * we have enough room (even if it's too much room).
529 if G_UNLIKELY (static_search_results_allocated < static_token_results_size)
531 static_search_results_allocated = static_token_results_allocated;
532 static_search_results = g_renew (struct search_result,
533 static_search_results,
534 static_search_results_allocated);
537 for (i = 0; i < static_token_results_size; i++)
539 /* The list is sorted so that the best match for a given id
540 * will be at the front, so once we have copied an id, skip
541 * the rest of the entries for the same id.
543 if (static_token_results[i].app_name == last_name)
546 last_name = static_token_results[i].app_name;
548 static_search_results[static_search_results_size++] = static_token_results[i];
553 const gchar *last_name = NULL;
557 /* We only ever remove items from the results list, so no need to
558 * resize to ensure that we have enough room.
560 for (i = 0; i < static_token_results_size; i++)
562 if (static_token_results[i].app_name == last_name)
565 last_name = static_token_results[i].app_name;
567 /* Now we only want to have a result in static_search_results
568 * if we already have it there *and* we have it in
569 * static_token_results as well. The category will be the
572 * Skip past the results in static_search_results that are not
573 * going to be matches.
575 while (k < static_search_results_size &&
576 static_search_results[k].app_name < static_token_results[i].app_name)
579 if (k < static_search_results_size &&
580 static_search_results[k].app_name == static_token_results[i].app_name)
584 * Category should be the worse of the two (ie:
585 * numerically larger).
587 static_search_results[j].app_name = static_search_results[k].app_name;
588 static_search_results[j].category = MAX (static_search_results[k].category,
589 static_token_results[i].category);
594 static_search_results_size = j;
597 /* Clear it out for next time... */
598 static_token_results_size = 0;
602 reset_total_search_results (void)
604 static_total_results_size = 0;
608 sort_total_search_results (void)
610 qsort (static_total_results, static_total_results_size, sizeof (struct search_result), compare_categories);
614 merge_directory_results (void)
616 if G_UNLIKELY (static_total_results_size + static_search_results_size > static_total_results_allocated)
618 static_total_results_allocated = MAX (16, static_total_results_allocated);
619 while (static_total_results_allocated < static_total_results_size + static_search_results_size)
620 static_total_results_allocated *= 2;
621 static_total_results = g_renew (struct search_result, static_total_results, static_total_results_allocated);
624 memcpy (static_total_results + static_total_results_size,
625 static_search_results,
626 static_search_results_size * sizeof (struct search_result));
628 static_total_results_size += static_search_results_size;
630 /* Clear it out for next time... */
631 static_search_results_size = 0;
634 /* Support for unindexed DesktopFileDirs {{{2 */
636 get_apps_from_dir (GHashTable **apps,
640 const char *basename;
643 dir = g_dir_open (dirname, 0, NULL);
648 while ((basename = g_dir_read_name (dir)) != NULL)
652 filename = g_build_filename (dirname, basename, NULL);
654 if (g_str_has_suffix (basename, ".desktop"))
658 app_name = g_strconcat (prefix, basename, NULL);
661 *apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
663 g_hash_table_insert (*apps, app_name, g_strdup (filename));
665 else if (g_file_test (filename, G_FILE_TEST_IS_DIR))
669 subprefix = g_strconcat (prefix, basename, "-", NULL);
670 get_apps_from_dir (apps, filename, subprefix);
685 } UnindexedMimeTweaks;
688 free_mime_tweaks (gpointer data)
690 UnindexedMimeTweaks *tweaks = data;
692 g_strfreev (tweaks->additions);
693 g_strfreev (tweaks->removals);
694 g_strfreev (tweaks->defaults);
696 g_slice_free (UnindexedMimeTweaks, tweaks);
699 static UnindexedMimeTweaks *
700 desktop_file_dir_unindexed_get_tweaks (DesktopFileDir *dir,
701 const gchar *mime_type)
703 UnindexedMimeTweaks *tweaks;
704 gchar *unaliased_type;
706 unaliased_type = _g_unix_content_type_unalias (mime_type);
707 tweaks = g_hash_table_lookup (dir->mime_tweaks, unaliased_type);
711 tweaks = g_slice_new0 (UnindexedMimeTweaks);
712 g_hash_table_insert (dir->mime_tweaks, unaliased_type, tweaks);
715 g_free (unaliased_type);
720 /* consumes 'to_add' */
722 expand_strv (gchar ***strv_ptr,
724 gchar * const *blacklist)
726 guint strv_len, add_len;
737 strv_len = g_strv_length (strv);
738 add_len = g_strv_length (to_add);
739 strv = g_renew (gchar *, strv, strv_len + add_len + 1);
741 for (i = 0; to_add[i]; i++)
743 /* Don't add blacklisted strings */
745 for (j = 0; blacklist[j]; j++)
746 if (g_str_equal (to_add[i], blacklist[j]))
749 /* Don't add duplicates already in the list */
750 for (j = 0; j < strv_len; j++)
751 if (g_str_equal (to_add[i], strv[j]))
754 strv[strv_len++] = to_add[i];
761 strv[strv_len] = NULL;
768 desktop_file_dir_unindexed_read_mimeapps_list (DesktopFileDir *dir,
769 const gchar *filename,
770 const gchar *added_group,
771 gboolean tweaks_permitted)
773 UnindexedMimeTweaks *tweaks;
774 char **desktop_file_ids;
779 key_file = g_key_file_new ();
780 if (!g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL))
782 g_key_file_free (key_file);
786 mime_types = g_key_file_get_keys (key_file, added_group, NULL, NULL);
788 if G_UNLIKELY (mime_types != NULL && !tweaks_permitted)
790 g_warning ("%s contains a [%s] group, but it is not permitted here. Only the non-desktop-specific "
791 "mimeapps.list file may add or remove associations.", filename, added_group);
792 g_strfreev (mime_types);
796 if (mime_types != NULL)
798 for (i = 0; mime_types[i] != NULL; i++)
800 desktop_file_ids = g_key_file_get_string_list (key_file, added_group, mime_types[i], NULL, NULL);
802 if (desktop_file_ids)
804 tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
805 expand_strv (&tweaks->additions, desktop_file_ids, tweaks->removals);
809 g_strfreev (mime_types);
812 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
814 if G_UNLIKELY (mime_types != NULL && !tweaks_permitted)
816 g_warning ("%s contains a [%s] group, but it is not permitted here. Only the non-desktop-specific "
817 "mimeapps.list file may add or remove associations.", filename, REMOVED_ASSOCIATIONS_GROUP);
818 g_strfreev (mime_types);
822 if (mime_types != NULL)
824 for (i = 0; mime_types[i] != NULL; i++)
826 desktop_file_ids = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP, mime_types[i], NULL, NULL);
828 if (desktop_file_ids)
830 tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
831 expand_strv (&tweaks->removals, desktop_file_ids, tweaks->additions);
835 g_strfreev (mime_types);
838 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
840 if (mime_types != NULL)
842 for (i = 0; mime_types[i] != NULL; i++)
844 desktop_file_ids = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP, mime_types[i], NULL, NULL);
846 if (desktop_file_ids)
848 tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
849 expand_strv (&tweaks->defaults, desktop_file_ids, NULL);
853 g_strfreev (mime_types);
856 g_key_file_free (key_file);
860 desktop_file_dir_unindexed_read_mimeapps_lists (DesktopFileDir *dir)
862 const gchar * const *desktops;
866 dir->mime_tweaks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_mime_tweaks);
868 /* We process in order of precedence, using a blacklisting approach to
869 * avoid recording later instructions that conflict with ones we found
872 * We first start with the XDG_CURRENT_DESKTOP files, in precedence
875 desktops = get_lowercase_current_desktops ();
876 for (i = 0; desktops[i]; i++)
878 filename = g_strdup_printf ("%s/%s-mimeapps.list", dir->path, desktops[i]);
879 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, FALSE);
883 /* Next, the non-desktop-specific mimeapps.list */
884 filename = g_strdup_printf ("%s/mimeapps.list", dir->path);
885 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, TRUE);
888 /* The remaining files are only checked for in directories that might
889 * contain desktop files (ie: not the config dirs).
894 /* We have 'defaults.list' which was only ever understood by GLib. It
895 * exists widely, but it has never been part of any spec and it should
896 * be treated as deprecated. This will be removed in a future
899 filename = g_strdup_printf ("%s/defaults.list", dir->path);
900 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, FALSE);
903 /* Finally, the mimeinfo.cache, which is just a cached copy of what we
904 * would find in the MimeTypes= lines of all of the desktop files.
906 filename = g_strdup_printf ("%s/mimeinfo.cache", dir->path);
907 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, MIME_CACHE_GROUP, TRUE);
912 desktop_file_dir_unindexed_init (DesktopFileDir *dir)
915 get_apps_from_dir (&dir->app_names, dir->path, "");
917 desktop_file_dir_unindexed_read_mimeapps_lists (dir);
920 static GDesktopAppInfo *
921 desktop_file_dir_unindexed_get_app (DesktopFileDir *dir,
922 const gchar *desktop_id)
924 const gchar *filename;
926 filename = g_hash_table_lookup (dir->app_names, desktop_id);
931 return g_desktop_app_info_new_from_filename (filename);
935 desktop_file_dir_unindexed_get_all (DesktopFileDir *dir,
942 if (dir->app_names == NULL)
945 g_hash_table_iter_init (&iter, dir->app_names);
946 while (g_hash_table_iter_next (&iter, &app_name, &filename))
948 if (desktop_file_dir_app_name_is_masked (dir, app_name))
951 add_to_table_if_appropriate (apps, app_name, g_desktop_app_info_new_from_filename (filename));
955 typedef struct _MemoryIndexEntry MemoryIndexEntry;
956 typedef GHashTable MemoryIndex;
958 struct _MemoryIndexEntry
960 const gchar *app_name; /* pointer to the hashtable key */
962 MemoryIndexEntry *next;
966 memory_index_entry_free (gpointer data)
968 MemoryIndexEntry *mie = data;
972 MemoryIndexEntry *next = mie->next;
974 g_slice_free (MemoryIndexEntry, mie);
980 memory_index_add_token (MemoryIndex *mi,
983 const gchar *app_name)
985 MemoryIndexEntry *mie, *first;
987 mie = g_slice_new (MemoryIndexEntry);
988 mie->app_name = app_name;
989 mie->match_category = match_category;
991 first = g_hash_table_lookup (mi, token);
995 mie->next = first->next;
1001 g_hash_table_insert (mi, g_strdup (token), mie);
1006 memory_index_add_string (MemoryIndex *mi,
1007 const gchar *string,
1008 gint match_category,
1009 const gchar *app_name)
1011 gchar **tokens, **alternates;
1014 tokens = g_str_tokenize_and_fold (string, NULL, &alternates);
1016 for (i = 0; tokens[i]; i++)
1017 memory_index_add_token (mi, tokens[i], match_category, app_name);
1019 for (i = 0; alternates[i]; i++)
1020 memory_index_add_token (mi, alternates[i], match_category, app_name);
1022 g_strfreev (alternates);
1023 g_strfreev (tokens);
1026 static MemoryIndex *
1027 memory_index_new (void)
1029 return g_hash_table_new_full (g_str_hash, g_str_equal, g_free, memory_index_entry_free);
1033 desktop_file_dir_unindexed_setup_search (DesktopFileDir *dir)
1035 GHashTableIter iter;
1038 dir->memory_index = memory_index_new ();
1039 dir->memory_implementations = memory_index_new ();
1041 /* Nothing to search? */
1042 if (dir->app_names == NULL)
1045 g_hash_table_iter_init (&iter, dir->app_names);
1046 while (g_hash_table_iter_next (&iter, &app, &path))
1050 if (desktop_file_dir_app_name_is_masked (dir, app))
1053 key_file = g_key_file_new ();
1055 if (g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, NULL) &&
1056 !g_key_file_get_boolean (key_file, "Desktop Entry", "Hidden", NULL))
1058 /* Index the interesting keys... */
1062 for (i = 0; i < G_N_ELEMENTS (desktop_key_match_category); i++)
1067 if (!desktop_key_match_category[i])
1070 raw = g_key_file_get_locale_string (key_file, "Desktop Entry", desktop_key_get_name (i), NULL, NULL);
1073 if (i == DESKTOP_KEY_Exec && raw != NULL)
1075 /* Special handling: only match basename of first field */
1079 /* Remove extra arguments, if any */
1080 space = raw + strcspn (raw, " \t\n"); /* IFS */
1083 /* Skip the pathname, if any */
1084 if ((slash = strrchr (raw, '/')))
1089 memory_index_add_string (dir->memory_index, value, desktop_key_match_category[i], app);
1094 /* Make note of the Implements= line */
1095 implements = g_key_file_get_string_list (key_file, "Desktop Entry", "Implements", NULL, NULL);
1096 for (i = 0; implements && implements[i]; i++)
1097 memory_index_add_token (dir->memory_implementations, implements[i], 0, app);
1098 g_strfreev (implements);
1101 g_key_file_free (key_file);
1106 desktop_file_dir_unindexed_search (DesktopFileDir *dir,
1107 const gchar *search_token)
1109 GHashTableIter iter;
1110 gpointer key, value;
1112 if (!dir->memory_index)
1113 desktop_file_dir_unindexed_setup_search (dir);
1115 g_hash_table_iter_init (&iter, dir->memory_index);
1116 while (g_hash_table_iter_next (&iter, &key, &value))
1118 MemoryIndexEntry *mie = value;
1120 if (!g_str_has_prefix (key, search_token))
1125 add_token_result (mie->app_name, mie->match_category);
1132 array_contains (GPtrArray *array,
1137 for (i = 0; i < array->len; i++)
1138 if (g_str_equal (array->pdata[i], str))
1145 desktop_file_dir_unindexed_mime_lookup (DesktopFileDir *dir,
1146 const gchar *mime_type,
1148 GPtrArray *blacklist)
1150 UnindexedMimeTweaks *tweaks;
1153 tweaks = g_hash_table_lookup (dir->mime_tweaks, mime_type);
1158 if (tweaks->additions)
1160 for (i = 0; tweaks->additions[i]; i++)
1162 gchar *app_name = tweaks->additions[i];
1164 if (!desktop_file_dir_app_name_is_masked (dir, app_name) &&
1165 !array_contains (blacklist, app_name) && !array_contains (hits, app_name))
1166 g_ptr_array_add (hits, g_strdup (app_name));
1170 if (tweaks->removals)
1172 for (i = 0; tweaks->removals[i]; i++)
1174 gchar *app_name = tweaks->removals[i];
1176 if (!desktop_file_dir_app_name_is_masked (dir, app_name) &&
1177 !array_contains (blacklist, app_name) && !array_contains (hits, app_name))
1178 g_ptr_array_add (blacklist, app_name);
1184 desktop_file_dir_unindexed_default_lookup (DesktopFileDir *dir,
1185 const gchar *mime_type,
1188 UnindexedMimeTweaks *tweaks;
1191 tweaks = g_hash_table_lookup (dir->mime_tweaks, mime_type);
1193 if (!tweaks || !tweaks->defaults)
1196 for (i = 0; tweaks->defaults[i]; i++)
1198 gchar *app_name = tweaks->defaults[i];
1200 if (!array_contains (results, app_name))
1201 g_ptr_array_add (results, g_strdup (app_name));
1206 desktop_file_dir_unindexed_get_implementations (DesktopFileDir *dir,
1208 const gchar *interface)
1210 MemoryIndexEntry *mie;
1212 if (!dir->memory_index)
1213 desktop_file_dir_unindexed_setup_search (dir);
1215 for (mie = g_hash_table_lookup (dir->memory_implementations, interface); mie; mie = mie->next)
1216 *results = g_list_prepend (*results, g_strdup (mie->app_name));
1219 /* DesktopFileDir "API" {{{2 */
1222 * desktop_file_dir_create:
1223 * @array: the #GArray to add a new item to
1224 * @data_dir: an XDG_DATA_DIR
1226 * Creates a #DesktopFileDir for the corresponding @data_dir, adding it
1230 desktop_file_dir_create (GArray *array,
1231 const gchar *data_dir)
1233 DesktopFileDir dir = { 0, };
1235 dir.path = g_build_filename (data_dir, "applications", NULL);
1237 g_array_append_val (array, dir);
1241 * desktop_file_dir_create:
1242 * @array: the #GArray to add a new item to
1243 * @config_dir: an XDG_CONFIG_DIR
1245 * Just the same as desktop_file_dir_create() except that it does not
1246 * add the "applications" directory. It also marks the directory as
1247 * config-only, which prevents us from attempting to find desktop files
1251 desktop_file_dir_create_for_config (GArray *array,
1252 const gchar *config_dir)
1254 DesktopFileDir dir = { 0, };
1256 dir.path = g_strdup (config_dir);
1257 dir.is_config = TRUE;
1259 g_array_append_val (array, dir);
1263 * desktop_file_dir_reset:
1264 * @dir: a #DesktopFileDir
1266 * Cleans up @dir, releasing most resources that it was using.
1269 desktop_file_dir_reset (DesktopFileDir *dir)
1271 if (dir->alternatively_watching)
1273 g_free (dir->alternatively_watching);
1274 dir->alternatively_watching = NULL;
1279 g_signal_handlers_disconnect_by_func (dir->monitor, desktop_file_dir_changed, dir);
1280 g_object_unref (dir->monitor);
1281 dir->monitor = NULL;
1286 g_hash_table_unref (dir->app_names);
1287 dir->app_names = NULL;
1290 if (dir->memory_index)
1292 g_hash_table_unref (dir->memory_index);
1293 dir->memory_index = NULL;
1296 if (dir->mime_tweaks)
1298 g_hash_table_unref (dir->mime_tweaks);
1299 dir->mime_tweaks = NULL;
1302 if (dir->memory_implementations)
1304 g_hash_table_unref (dir->memory_implementations);
1305 dir->memory_implementations = NULL;
1308 dir->is_setup = FALSE;
1312 * desktop_file_dir_init:
1313 * @dir: a #DesktopFileDir
1315 * Does initial setup for @dir
1317 * You should only call this if @dir is not already setup.
1320 desktop_file_dir_init (DesktopFileDir *dir)
1322 const gchar *watch_dir;
1324 g_assert (!dir->is_setup);
1326 g_assert (!dir->alternatively_watching);
1327 g_assert (!dir->monitor);
1329 dir->alternatively_watching = desktop_file_dir_get_alternative_dir (dir);
1330 watch_dir = dir->alternatively_watching ? dir->alternatively_watching : dir->path;
1332 /* There is a very thin race here if the watch_dir has been _removed_
1333 * between when we checked for it and when we establish the watch.
1334 * Removes probably don't happen in usual operation, and even if it
1335 * does (and we catch the unlikely race), the only degradation is that
1336 * we will fall back to polling.
1338 dir->monitor = g_local_directory_monitor_new_in_worker (watch_dir, G_FILE_MONITOR_NONE, NULL);
1342 g_signal_connect (dir->monitor, "changed", G_CALLBACK (desktop_file_dir_changed), dir);
1343 g_local_directory_monitor_start (dir->monitor);
1346 desktop_file_dir_unindexed_init (dir);
1348 dir->is_setup = TRUE;
1352 * desktop_file_dir_get_app:
1353 * @dir: a DesktopFileDir
1354 * @desktop_id: the desktop ID to load
1356 * Creates the #GDesktopAppInfo for the given @desktop_id if it exists
1357 * within @dir, even if it is hidden.
1359 * This function does not check if @desktop_id would be masked by a
1360 * directory with higher precedence. The caller must do so.
1362 static GDesktopAppInfo *
1363 desktop_file_dir_get_app (DesktopFileDir *dir,
1364 const gchar *desktop_id)
1366 if (!dir->app_names)
1369 return desktop_file_dir_unindexed_get_app (dir, desktop_id);
1373 * desktop_file_dir_get_all:
1374 * @dir: a DesktopFileDir
1375 * @apps: a #GHashTable<string, GDesktopAppInfo>
1377 * Loads all desktop files in @dir and adds them to @apps, careful to
1378 * ensure we don't add any files masked by a similarly-named file in a
1379 * higher-precedence directory.
1382 desktop_file_dir_get_all (DesktopFileDir *dir,
1385 desktop_file_dir_unindexed_get_all (dir, apps);
1389 * desktop_file_dir_mime_lookup:
1390 * @dir: a #DesktopFileDir
1391 * @mime_type: the mime type to look up
1392 * @hits: the array to store the hits
1393 * @blacklist: the array to store the blacklist
1395 * Does a lookup of a mimetype against one desktop file directory,
1396 * recording any hits and blacklisting and "Removed" associations (so
1397 * later directories don't record them as hits).
1399 * The items added to @hits are duplicated, but the ones in @blacklist
1400 * are weak pointers. This facilitates simply freeing the blacklist
1401 * (which is only used for internal bookkeeping) but using the pdata of
1402 * @hits as the result of the operation.
1405 desktop_file_dir_mime_lookup (DesktopFileDir *dir,
1406 const gchar *mime_type,
1408 GPtrArray *blacklist)
1410 desktop_file_dir_unindexed_mime_lookup (dir, mime_type, hits, blacklist);
1414 * desktop_file_dir_default_lookup:
1415 * @dir: a #DesktopFileDir
1416 * @mime_type: the mime type to look up
1417 * @results: an array to store the results in
1419 * Collects the "default" applications for a given mime type from @dir.
1422 desktop_file_dir_default_lookup (DesktopFileDir *dir,
1423 const gchar *mime_type,
1426 desktop_file_dir_unindexed_default_lookup (dir, mime_type, results);
1430 * desktop_file_dir_search:
1431 * @dir: a #DesktopFileDir
1432 * @term: a normalised and casefolded search term
1434 * Finds the names of applications in @dir that match @term.
1437 desktop_file_dir_search (DesktopFileDir *dir,
1438 const gchar *search_token)
1440 desktop_file_dir_unindexed_search (dir, search_token);
1444 desktop_file_dir_get_implementations (DesktopFileDir *dir,
1446 const gchar *interface)
1448 desktop_file_dir_unindexed_get_implementations (dir, results, interface);
1451 /* Lock/unlock and global setup API {{{2 */
1454 desktop_file_dirs_lock (void)
1458 g_mutex_lock (&desktop_file_dir_lock);
1460 if (desktop_file_dirs == NULL)
1462 const char * const *dirs;
1466 tmp = g_array_new (FALSE, FALSE, sizeof (DesktopFileDir));
1468 /* First, the configs. Highest priority: the user's ~/.config */
1469 desktop_file_dir_create_for_config (tmp, g_get_user_config_dir ());
1471 /* Next, the system configs (/etc/xdg, and so on). */
1472 dirs = g_get_system_config_dirs ();
1473 for (i = 0; dirs[i]; i++)
1474 desktop_file_dir_create_for_config (tmp, dirs[i]);
1476 /* Now the data. Highest priority: the user's ~/.local/share/applications */
1477 desktop_file_dir_user_data_index = tmp->len;
1478 desktop_file_dir_create (tmp, g_get_user_data_dir ());
1480 /* Following that, XDG_DATA_DIRS/applications, in order */
1481 dirs = g_get_system_data_dirs ();
1482 for (i = 0; dirs[i]; i++)
1483 desktop_file_dir_create (tmp, dirs[i]);
1485 /* The list of directories will never change after this. */
1486 desktop_file_dirs = (DesktopFileDir *) tmp->data;
1487 n_desktop_file_dirs = tmp->len;
1489 g_array_free (tmp, FALSE);
1492 for (i = 0; i < n_desktop_file_dirs; i++)
1493 if (!desktop_file_dirs[i].is_setup)
1494 desktop_file_dir_init (&desktop_file_dirs[i]);
1498 desktop_file_dirs_unlock (void)
1500 g_mutex_unlock (&desktop_file_dir_lock);
1504 desktop_file_dirs_invalidate_user_config (void)
1506 g_mutex_lock (&desktop_file_dir_lock);
1508 if (n_desktop_file_dirs)
1509 desktop_file_dir_reset (&desktop_file_dirs[desktop_file_dir_user_config_index]);
1511 g_mutex_unlock (&desktop_file_dir_lock);
1515 desktop_file_dirs_invalidate_user_data (void)
1517 g_mutex_lock (&desktop_file_dir_lock);
1519 if (n_desktop_file_dirs)
1520 desktop_file_dir_reset (&desktop_file_dirs[desktop_file_dir_user_data_index]);
1522 g_mutex_unlock (&desktop_file_dir_lock);
1525 /* GDesktopAppInfo implementation {{{1 */
1526 /* GObject implementation {{{2 */
1528 g_desktop_app_info_finalize (GObject *object)
1530 GDesktopAppInfo *info;
1532 info = G_DESKTOP_APP_INFO (object);
1534 g_free (info->desktop_id);
1535 g_free (info->filename);
1538 g_key_file_unref (info->keyfile);
1540 g_free (info->name);
1541 g_free (info->generic_name);
1542 g_free (info->fullname);
1543 g_free (info->comment);
1544 g_free (info->icon_name);
1546 g_object_unref (info->icon);
1547 g_strfreev (info->keywords);
1548 g_strfreev (info->only_show_in);
1549 g_strfreev (info->not_show_in);
1550 g_free (info->try_exec);
1551 g_free (info->exec);
1552 g_free (info->binary);
1553 g_free (info->path);
1554 g_free (info->categories);
1555 g_free (info->startup_wm_class);
1556 g_strfreev (info->mime_types);
1557 g_free (info->app_id);
1558 g_strfreev (info->actions);
1560 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
1564 g_desktop_app_info_set_property (GObject *object,
1566 const GValue *value,
1569 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
1574 self->filename = g_value_dup_string (value);
1578 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1584 g_desktop_app_info_get_property (GObject *object,
1589 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
1594 g_value_set_string (value, self->filename);
1597 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1603 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
1605 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1607 gobject_class->get_property = g_desktop_app_info_get_property;
1608 gobject_class->set_property = g_desktop_app_info_set_property;
1609 gobject_class->finalize = g_desktop_app_info_finalize;
1612 * GDesktopAppInfo:filename:
1614 * The origin filename of this #GDesktopAppInfo
1616 g_object_class_install_property (gobject_class,
1618 g_param_spec_string ("filename", "Filename", "", NULL,
1619 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
1623 g_desktop_app_info_init (GDesktopAppInfo *local)
1627 /* Construction... {{{2 */
1631 * @exec: an exec line
1633 * Returns the first word in an exec line (ie: the binary name).
1635 * If @exec is " progname --foo %F" then returns "progname".
1638 binary_from_exec (const char *exec)
1640 const char *p, *start;
1646 while (*p != ' ' && *p != 0)
1649 return g_strndup (start, p - start);
1653 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
1660 gboolean bus_activatable;
1662 start_group = g_key_file_get_start_group (key_file);
1663 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
1665 g_free (start_group);
1668 g_free (start_group);
1670 type = g_key_file_get_string (key_file,
1671 G_KEY_FILE_DESKTOP_GROUP,
1672 G_KEY_FILE_DESKTOP_KEY_TYPE,
1674 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
1681 try_exec = g_key_file_get_string (key_file,
1682 G_KEY_FILE_DESKTOP_GROUP,
1683 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
1685 if (try_exec && try_exec[0] != '\0')
1688 t = g_find_program_in_path (try_exec);
1697 exec = g_key_file_get_string (key_file,
1698 G_KEY_FILE_DESKTOP_GROUP,
1699 G_KEY_FILE_DESKTOP_KEY_EXEC,
1701 if (exec && exec[0] != '\0')
1705 if (!g_shell_parse_argv (exec, &argc, &argv, NULL))
1714 t = g_find_program_in_path (argv[0]);
1727 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
1728 info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
1729 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
1730 info->keywords = g_key_file_get_locale_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, KEYWORDS_KEY, NULL, NULL, NULL);
1731 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
1732 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
1733 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
1734 info->only_show_in = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN, NULL, NULL);
1735 info->not_show_in = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN, NULL, NULL);
1736 info->try_exec = try_exec;
1738 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
1739 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
1740 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
1741 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
1742 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
1743 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
1744 info->startup_wm_class = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, STARTUP_WM_CLASS_KEY, NULL);
1745 info->mime_types = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL, NULL);
1746 bus_activatable = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE, NULL);
1747 info->actions = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ACTIONS, NULL, NULL);
1749 /* Remove the special-case: no Actions= key just means 0 extra actions */
1750 if (info->actions == NULL)
1751 info->actions = g_new0 (gchar *, 0 + 1);
1754 if (info->icon_name)
1756 if (g_path_is_absolute (info->icon_name))
1760 file = g_file_new_for_path (info->icon_name);
1761 info->icon = g_file_icon_new (file);
1762 g_object_unref (file);
1768 /* Work around a common mistake in desktop files */
1769 if ((p = strrchr (info->icon_name, '.')) != NULL &&
1770 (strcmp (p, ".png") == 0 ||
1771 strcmp (p, ".xpm") == 0 ||
1772 strcmp (p, ".svg") == 0))
1775 info->icon = g_themed_icon_new (info->icon_name);
1780 info->binary = binary_from_exec (info->exec);
1782 if (info->path && info->path[0] == '\0')
1784 g_free (info->path);
1788 /* Can only be DBusActivatable if we know the filename, which means
1789 * that this won't work for the load-from-keyfile case.
1791 if (bus_activatable && info->filename)
1796 basename = g_path_get_basename (info->filename);
1797 last_dot = strrchr (basename, '.');
1799 if (last_dot && g_str_equal (last_dot, ".desktop"))
1803 if (g_dbus_is_interface_name (basename))
1804 info->app_id = g_strdup (basename);
1810 info->keyfile = g_key_file_ref (key_file);
1816 g_desktop_app_info_load_file (GDesktopAppInfo *self)
1819 gboolean retval = FALSE;
1821 g_return_val_if_fail (self->filename != NULL, FALSE);
1823 self->desktop_id = g_path_get_basename (self->filename);
1825 key_file = g_key_file_new ();
1827 if (g_key_file_load_from_file (key_file, self->filename, G_KEY_FILE_NONE, NULL))
1828 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
1830 g_key_file_unref (key_file);
1835 * g_desktop_app_info_new_from_keyfile:
1836 * @key_file: an opened #GKeyFile
1838 * Creates a new #GDesktopAppInfo.
1840 * Returns: a new #GDesktopAppInfo or %NULL on error.
1845 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
1847 GDesktopAppInfo *info;
1849 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1850 info->filename = NULL;
1851 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
1853 g_object_unref (info);
1860 * g_desktop_app_info_new_from_filename:
1861 * @filename: the path of a desktop file, in the GLib filename encoding
1863 * Creates a new #GDesktopAppInfo.
1865 * Returns: a new #GDesktopAppInfo or %NULL on error.
1868 g_desktop_app_info_new_from_filename (const char *filename)
1870 GDesktopAppInfo *info = NULL;
1872 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
1873 if (!g_desktop_app_info_load_file (info))
1875 g_object_unref (info);
1882 * g_desktop_app_info_new:
1883 * @desktop_id: the desktop file id
1885 * Creates a new #GDesktopAppInfo based on a desktop file id.
1887 * A desktop file id is the basename of the desktop file, including the
1888 * .desktop extension. GIO is looking for a desktop file with this name
1889 * in the `applications` subdirectories of the XDG
1890 * data directories (i.e. the directories specified in the `XDG_DATA_HOME`
1891 * and `XDG_DATA_DIRS` environment variables). GIO also supports the
1892 * prefix-to-subdirectory mapping that is described in the
1893 * [Menu Spec](http://standards.freedesktop.org/menu-spec/latest/)
1894 * (i.e. a desktop id of kde-foo.desktop will match
1895 * `/usr/share/applications/kde/foo.desktop`).
1897 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
1900 g_desktop_app_info_new (const char *desktop_id)
1902 GDesktopAppInfo *appinfo = NULL;
1905 desktop_file_dirs_lock ();
1907 for (i = 0; i < n_desktop_file_dirs; i++)
1909 appinfo = desktop_file_dir_get_app (&desktop_file_dirs[i], desktop_id);
1915 desktop_file_dirs_unlock ();
1917 if (appinfo == NULL)
1920 g_free (appinfo->desktop_id);
1921 appinfo->desktop_id = g_strdup (desktop_id);
1923 if (g_desktop_app_info_get_is_hidden (appinfo))
1925 g_object_unref (appinfo);
1933 g_desktop_app_info_dup (GAppInfo *appinfo)
1935 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1936 GDesktopAppInfo *new_info;
1938 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1940 new_info->filename = g_strdup (info->filename);
1941 new_info->desktop_id = g_strdup (info->desktop_id);
1944 new_info->keyfile = g_key_file_ref (info->keyfile);
1946 new_info->name = g_strdup (info->name);
1947 new_info->generic_name = g_strdup (info->generic_name);
1948 new_info->fullname = g_strdup (info->fullname);
1949 new_info->keywords = g_strdupv (info->keywords);
1950 new_info->comment = g_strdup (info->comment);
1951 new_info->nodisplay = info->nodisplay;
1952 new_info->icon_name = g_strdup (info->icon_name);
1954 new_info->icon = g_object_ref (info->icon);
1955 new_info->only_show_in = g_strdupv (info->only_show_in);
1956 new_info->not_show_in = g_strdupv (info->not_show_in);
1957 new_info->try_exec = g_strdup (info->try_exec);
1958 new_info->exec = g_strdup (info->exec);
1959 new_info->binary = g_strdup (info->binary);
1960 new_info->path = g_strdup (info->path);
1961 new_info->app_id = g_strdup (info->app_id);
1962 new_info->hidden = info->hidden;
1963 new_info->terminal = info->terminal;
1964 new_info->startup_notify = info->startup_notify;
1966 return G_APP_INFO (new_info);
1969 /* GAppInfo interface implementation functions {{{2 */
1972 g_desktop_app_info_equal (GAppInfo *appinfo1,
1975 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
1976 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
1978 if (info1->desktop_id == NULL ||
1979 info2->desktop_id == NULL)
1980 return info1 == info2;
1982 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
1986 g_desktop_app_info_get_id (GAppInfo *appinfo)
1988 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1990 return info->desktop_id;
1994 g_desktop_app_info_get_name (GAppInfo *appinfo)
1996 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1998 if (info->name == NULL)
1999 return _("Unnamed");
2004 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
2006 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2008 if (info->fullname == NULL)
2009 return g_desktop_app_info_get_name (appinfo);
2010 return info->fullname;
2014 * g_desktop_app_info_get_is_hidden:
2015 * @info: a #GDesktopAppInfo.
2017 * A desktop file is hidden if the Hidden key in it is
2020 * Returns: %TRUE if hidden, %FALSE otherwise.
2023 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
2025 return info->hidden;
2029 * g_desktop_app_info_get_filename:
2030 * @info: a #GDesktopAppInfo
2032 * When @info was created from a known filename, return it. In some
2033 * situations such as the #GDesktopAppInfo returned from
2034 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
2036 * Returns: The full path to the file for @info, or %NULL if not known.
2040 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
2042 return info->filename;
2046 g_desktop_app_info_get_description (GAppInfo *appinfo)
2048 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2050 return info->comment;
2054 g_desktop_app_info_get_executable (GAppInfo *appinfo)
2056 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2058 return info->binary;
2062 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
2064 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2070 g_desktop_app_info_get_icon (GAppInfo *appinfo)
2072 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2078 * g_desktop_app_info_get_categories:
2079 * @info: a #GDesktopAppInfo
2081 * Gets the categories from the desktop file.
2083 * Returns: The unparsed Categories key from the desktop file;
2084 * i.e. no attempt is made to split it by ';' or validate it.
2087 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
2089 return info->categories;
2093 * g_desktop_app_info_get_keywords:
2094 * @info: a #GDesktopAppInfo
2096 * Gets the keywords from the desktop file.
2098 * Returns: (transfer none): The value of the Keywords key
2102 const char * const *
2103 g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
2105 return (const char * const *)info->keywords;
2109 * g_desktop_app_info_get_generic_name:
2110 * @info: a #GDesktopAppInfo
2112 * Gets the generic name from the destkop file.
2114 * Returns: The value of the GenericName key
2117 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
2119 return info->generic_name;
2123 * g_desktop_app_info_get_nodisplay:
2124 * @info: a #GDesktopAppInfo
2126 * Gets the value of the NoDisplay key, which helps determine if the
2127 * application info should be shown in menus. See
2128 * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
2130 * Returns: The value of the NoDisplay key
2135 g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
2137 return info->nodisplay;
2141 * g_desktop_app_info_get_show_in:
2142 * @info: a #GDesktopAppInfo
2143 * @desktop_env: (nullable): a string specifying a desktop name
2145 * Checks if the application info should be shown in menus that list available
2146 * applications for a specific name of the desktop, based on the
2147 * `OnlyShowIn` and `NotShowIn` keys.
2149 * @desktop_env should typically be given as %NULL, in which case the
2150 * `XDG_CURRENT_DESKTOP` environment variable is consulted. If you want
2151 * to override the default mechanism then you may specify @desktop_env,
2152 * but this is not recommended.
2154 * Note that g_app_info_should_show() for @info will include this check (with
2155 * %NULL for @desktop_env) as well as additional checks.
2157 * Returns: %TRUE if the @info should be shown in @desktop_env according to the
2158 * `OnlyShowIn` and `NotShowIn` keys, %FALSE
2164 g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
2165 const gchar *desktop_env)
2167 const gchar *specified_envs[] = { desktop_env, NULL };
2168 const gchar * const *envs;
2171 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
2174 envs = specified_envs;
2176 envs = get_current_desktops (NULL);
2178 for (i = 0; envs[i]; i++)
2182 if (info->only_show_in)
2183 for (j = 0; info->only_show_in[j]; j++)
2184 if (g_str_equal (info->only_show_in[j], envs[i]))
2187 if (info->not_show_in)
2188 for (j = 0; info->not_show_in[j]; j++)
2189 if (g_str_equal (info->not_show_in[j], envs[i]))
2193 return info->only_show_in == NULL;
2196 /* Launching... {{{2 */
2199 expand_macro_single (char macro, char *uri)
2202 char *result = NULL;
2206 file = g_file_new_for_uri (uri);
2212 result = g_shell_quote (uri);
2216 path = g_file_get_path (file);
2218 result = g_shell_quote (path);
2222 path = g_file_get_path (file);
2225 name = g_path_get_dirname (path);
2226 result = g_shell_quote (name);
2232 path = g_file_get_path (file);
2235 name = g_path_get_basename (path);
2236 result = g_shell_quote (name);
2242 g_object_unref (file);
2249 expand_macro (char macro,
2251 GDesktopAppInfo *info,
2254 GList *uris = *uri_list;
2256 gboolean force_file_uri;
2257 char force_file_uri_macro;
2260 g_return_if_fail (exec != NULL);
2262 /* On %u and %U, pass POSIX file path pointing to the URI via
2263 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
2264 * running or the URI doesn't have a POSIX file path via FUSE
2265 * we'll just pass the URI.
2267 force_file_uri_macro = macro;
2268 force_file_uri = FALSE;
2274 force_file_uri_macro = 'f';
2275 force_file_uri = TRUE;
2278 force_file_uri_macro = 'F';
2279 force_file_uri = TRUE;
2295 if (!force_file_uri ||
2296 /* Pass URI if it contains an anchor */
2297 strchr (uri, '#') != NULL)
2299 expanded = expand_macro_single (macro, uri);
2303 expanded = expand_macro_single (force_file_uri_macro, uri);
2304 if (expanded == NULL)
2305 expanded = expand_macro_single (macro, uri);
2310 g_string_append (exec, expanded);
2326 if (!force_file_uri ||
2327 /* Pass URI if it contains an anchor */
2328 strchr (uri, '#') != NULL)
2330 expanded = expand_macro_single (macro, uri);
2334 expanded = expand_macro_single (force_file_uri_macro, uri);
2335 if (expanded == NULL)
2336 expanded = expand_macro_single (macro, uri);
2341 g_string_append (exec, expanded);
2347 if (uris != NULL && expanded)
2348 g_string_append_c (exec, ' ');
2354 if (info->icon_name)
2356 g_string_append (exec, "--icon ");
2357 expanded = g_shell_quote (info->icon_name);
2358 g_string_append (exec, expanded);
2366 expanded = g_shell_quote (info->name);
2367 g_string_append (exec, expanded);
2375 expanded = g_shell_quote (info->filename);
2376 g_string_append (exec, expanded);
2381 case 'm': /* deprecated */
2385 g_string_append_c (exec, '%');
2393 expand_application_parameters (GDesktopAppInfo *info,
2394 const gchar *exec_line,
2400 GList *uri_list = *uris;
2401 const char *p = exec_line;
2402 GString *expanded_exec;
2405 if (exec_line == NULL)
2407 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2408 _("Desktop file didn't specify Exec field"));
2412 expanded_exec = g_string_new (NULL);
2416 if (p[0] == '%' && p[1] != '\0')
2418 expand_macro (p[1], expanded_exec, info, uris);
2422 g_string_append_c (expanded_exec, *p);
2427 /* No file substitutions */
2428 if (uri_list == *uris && uri_list != NULL)
2430 /* If there is no macro default to %f. This is also what KDE does */
2431 g_string_append_c (expanded_exec, ' ');
2432 expand_macro ('f', expanded_exec, info, uris);
2435 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
2436 g_string_free (expanded_exec, TRUE);
2441 prepend_terminal_to_vector (int *argc,
2448 char **term_argv = NULL;
2453 g_return_val_if_fail (argc != NULL, FALSE);
2454 g_return_val_if_fail (argv != NULL, FALSE);
2462 /* compute size if not given */
2465 for (i = 0; the_argv[i] != NULL; i++)
2471 term_argv = g_new0 (char *, 3);
2473 check = g_find_program_in_path ("gnome-terminal");
2476 term_argv[0] = check;
2477 /* Note that gnome-terminal takes -x and
2478 * as -e in gnome-terminal is broken we use that. */
2479 term_argv[1] = g_strdup ("-x");
2484 check = g_find_program_in_path ("nxterm");
2486 check = g_find_program_in_path ("color-xterm");
2488 check = g_find_program_in_path ("rxvt");
2490 check = g_find_program_in_path ("xterm");
2492 check = g_find_program_in_path ("dtterm");
2495 check = g_strdup ("xterm");
2496 g_warning ("couldn't find a terminal, falling back to xterm");
2498 term_argv[0] = check;
2499 term_argv[1] = g_strdup ("-e");
2502 real_argc = term_argc + *argc;
2503 real_argv = g_new (char *, real_argc + 1);
2505 for (i = 0; i < term_argc; i++)
2506 real_argv[i] = term_argv[i];
2508 for (j = 0; j < *argc; j++, i++)
2509 real_argv[i] = (char *)the_argv[j];
2511 real_argv[i] = NULL;
2517 /* we use g_free here as we sucked all the inner strings
2518 * out from it into real_argv */
2523 #endif /* G_OS_WIN32 */
2527 create_files_for_uris (GList *uris)
2534 for (iter = uris; iter; iter = iter->next)
2536 GFile *file = g_file_new_for_uri ((char *)iter->data);
2537 res = g_list_prepend (res, file);
2540 return g_list_reverse (res);
2545 GSpawnChildSetupFunc user_setup;
2546 gpointer user_setup_data;
2552 child_setup (gpointer user_data)
2554 ChildSetupData *data = user_data;
2556 if (data->pid_envvar)
2558 pid_t pid = getpid ();
2562 /* Write the pid into the space already reserved for it in the
2563 * environment array. We can't use sprintf because it might
2564 * malloc, so we do it by hand. It's simplest to write the pid
2565 * out backwards first, then copy it over.
2567 for (i = 0; pid; i++, pid /= 10)
2568 buf[i] = (pid % 10) + '0';
2569 for (i--; i >= 0; i--)
2570 *(data->pid_envvar++) = buf[i];
2571 *data->pid_envvar = '\0';
2574 if (data->user_setup)
2575 data->user_setup (data->user_setup_data);
2579 notify_desktop_launch (GDBusConnection *session_bus,
2580 GDesktopAppInfo *info,
2582 const char *display,
2587 GVariantBuilder uri_variant;
2588 GVariantBuilder extras_variant;
2590 const char *desktop_file_id;
2591 const char *gio_desktop_file;
2593 if (session_bus == NULL)
2596 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
2597 for (iter = uris; iter; iter = iter->next)
2598 g_variant_builder_add (&uri_variant, "s", iter->data);
2600 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
2601 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
2602 g_variant_builder_add (&extras_variant, "{sv}",
2606 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
2607 if (gio_desktop_file != NULL)
2608 g_variant_builder_add (&extras_variant, "{sv}",
2609 "origin-desktop-file",
2610 g_variant_new_bytestring (gio_desktop_file));
2611 if (g_get_prgname () != NULL)
2612 g_variant_builder_add (&extras_variant, "{sv}",
2614 g_variant_new_bytestring (g_get_prgname ()));
2615 g_variant_builder_add (&extras_variant, "{sv}",
2618 (gint64)getpid ()));
2621 desktop_file_id = info->filename;
2622 else if (info->desktop_id)
2623 desktop_file_id = info->desktop_id;
2625 desktop_file_id = "";
2627 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
2628 "org.gtk.gio.DesktopAppInfo",
2630 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
2631 g_variant_new_bytestring (desktop_file_id),
2632 display ? display : "",
2636 g_dbus_connection_send_message (session_bus,
2640 g_object_unref (msg);
2643 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
2646 g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo *info,
2647 GDBusConnection *session_bus,
2648 const gchar *exec_line,
2650 GAppLaunchContext *launch_context,
2651 GSpawnFlags spawn_flags,
2652 GSpawnChildSetupFunc user_setup,
2653 gpointer user_setup_data,
2654 GDesktopAppLaunchCallback pid_callback,
2655 gpointer pid_callback_data,
2658 gboolean completed = FALSE;
2660 char **argv, **envp;
2662 ChildSetupData data;
2664 g_return_val_if_fail (info != NULL, FALSE);
2669 envp = g_app_launch_context_get_environment (launch_context);
2671 envp = g_get_environ ();
2676 GList *launched_uris;
2678 char *display, *sn_id = NULL;
2681 if (!expand_application_parameters (info, exec_line, &uris, &argc, &argv, error))
2684 /* Get the subset of URIs we're launching with this process */
2685 launched_uris = NULL;
2686 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
2687 launched_uris = g_list_prepend (launched_uris, iter->data);
2688 launched_uris = g_list_reverse (launched_uris);
2690 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
2692 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2693 _("Unable to find terminal required for application"));
2697 data.user_setup = user_setup;
2698 data.user_setup_data = user_setup_data;
2702 envp = g_environ_setenv (envp,
2703 "GIO_LAUNCHED_DESKTOP_FILE",
2706 envp = g_environ_setenv (envp,
2707 "GIO_LAUNCHED_DESKTOP_FILE_PID",
2708 "XXXXXXXXXXXXXXXXXXXX", /* filled in child_setup */
2710 data.pid_envvar = (char *)g_environ_getenv (envp, "GIO_LAUNCHED_DESKTOP_FILE_PID");
2714 data.pid_envvar = NULL;
2721 GList *launched_files = create_files_for_uris (launched_uris);
2723 display = g_app_launch_context_get_display (launch_context,
2727 envp = g_environ_setenv (envp, "DISPLAY", display, TRUE);
2729 if (info->startup_notify)
2731 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
2735 envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
2738 g_list_free_full (launched_files, g_object_unref);
2741 if (!g_spawn_async (info->path,
2751 g_app_launch_context_launch_failed (launch_context, sn_id);
2755 g_list_free (launched_uris);
2760 if (pid_callback != NULL)
2761 pid_callback (info, pid, pid_callback_data);
2763 if (launch_context != NULL)
2765 GVariantBuilder builder;
2766 GVariant *platform_data;
2768 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
2769 g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (pid));
2771 g_variant_builder_add (&builder, "{sv}", "startup-notification-id", g_variant_new_string (sn_id));
2772 platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
2773 g_signal_emit_by_name (launch_context, "launched", info, platform_data);
2774 g_variant_unref (platform_data);
2777 notify_desktop_launch (session_bus,
2786 g_list_free (launched_uris);
2791 while (uris != NULL);
2803 object_path_from_appid (const gchar *app_id)
2808 n = strlen (app_id);
2809 path = g_malloc (n + 2);
2813 for (i = 0; i < n; i++)
2814 if (app_id[i] != '.')
2815 path[i + 1] = app_id[i];
2825 g_desktop_app_info_make_platform_data (GDesktopAppInfo *info,
2827 GAppLaunchContext *launch_context)
2829 GVariantBuilder builder;
2831 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
2835 GList *launched_files = create_files_for_uris (uris);
2837 if (info->startup_notify)
2841 sn_id = g_app_launch_context_get_startup_notify_id (launch_context, G_APP_INFO (info), launched_files);
2843 g_variant_builder_add (&builder, "{sv}", "desktop-startup-id", g_variant_new_take_string (sn_id));
2846 g_list_free_full (launched_files, g_object_unref);
2849 return g_variant_builder_end (&builder);
2853 g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo *info,
2854 GDBusConnection *session_bus,
2856 GAppLaunchContext *launch_context)
2858 GVariantBuilder builder;
2861 g_return_val_if_fail (info != NULL, FALSE);
2863 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
2869 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
2870 for (iter = uris; iter; iter = iter->next)
2871 g_variant_builder_add (&builder, "s", iter->data);
2872 g_variant_builder_close (&builder);
2875 g_variant_builder_add_value (&builder, g_desktop_app_info_make_platform_data (info, uris, launch_context));
2877 /* This is non-blocking API. Similar to launching via fork()/exec()
2878 * we don't wait around to see if the program crashed during startup.
2879 * This is what startup-notification's job is...
2881 object_path = object_path_from_appid (info->app_id);
2882 g_dbus_connection_call (session_bus, info->app_id, object_path, "org.freedesktop.Application",
2883 uris ? "Open" : "Activate", g_variant_builder_end (&builder),
2884 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
2885 g_free (object_path);
2891 g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
2893 GAppLaunchContext *launch_context,
2894 GSpawnFlags spawn_flags,
2895 GSpawnChildSetupFunc user_setup,
2896 gpointer user_setup_data,
2897 GDesktopAppLaunchCallback pid_callback,
2898 gpointer pid_callback_data,
2901 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2902 GDBusConnection *session_bus;
2903 gboolean success = TRUE;
2905 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
2907 if (session_bus && info->app_id)
2908 g_desktop_app_info_launch_uris_with_dbus (info, session_bus, uris, launch_context);
2910 success = g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec, uris, launch_context,
2911 spawn_flags, user_setup, user_setup_data,
2912 pid_callback, pid_callback_data, error);
2914 if (session_bus != NULL)
2916 /* This asynchronous flush holds a reference until it completes,
2917 * which ensures that the following unref won't immediately kill
2918 * the connection if we were the initial owner.
2920 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
2921 g_object_unref (session_bus);
2928 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
2930 GAppLaunchContext *launch_context,
2933 return g_desktop_app_info_launch_uris_internal (appinfo, uris,
2935 _SPAWN_FLAGS_DEFAULT,
2936 NULL, NULL, NULL, NULL,
2941 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
2943 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2945 return info->exec &&
2946 ((strstr (info->exec, "%u") != NULL) ||
2947 (strstr (info->exec, "%U") != NULL));
2951 g_desktop_app_info_supports_files (GAppInfo *appinfo)
2953 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2955 return info->exec &&
2956 ((strstr (info->exec, "%f") != NULL) ||
2957 (strstr (info->exec, "%F") != NULL));
2961 g_desktop_app_info_launch (GAppInfo *appinfo,
2963 GAppLaunchContext *launch_context,
2973 uri = g_file_get_uri (files->data);
2974 uris = g_list_prepend (uris, uri);
2975 files = files->next;
2978 uris = g_list_reverse (uris);
2980 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
2982 g_list_free_full (uris, g_free);
2988 * g_desktop_app_info_launch_uris_as_manager:
2989 * @appinfo: a #GDesktopAppInfo
2990 * @uris: (element-type utf8): List of URIs
2991 * @launch_context: (allow-none): a #GAppLaunchContext
2992 * @spawn_flags: #GSpawnFlags, used for each process
2993 * @user_setup: (scope call) (allow-none): a #GSpawnChildSetupFunc, used once
2995 * @user_setup_data: (closure user_setup) (allow-none): User data for @user_setup
2996 * @pid_callback: (scope call) (allow-none): Callback for child processes
2997 * @pid_callback_data: (closure pid_callback) (allow-none): User data for @callback
2998 * @error: return location for a #GError, or %NULL
3000 * This function performs the equivalent of g_app_info_launch_uris(),
3001 * but is intended primarily for operating system components that
3002 * launch applications. Ordinary applications should use
3003 * g_app_info_launch_uris().
3005 * If the application is launched via traditional UNIX fork()/exec()
3006 * then @spawn_flags, @user_setup and @user_setup_data are used for the
3007 * call to g_spawn_async(). Additionally, @pid_callback (with
3008 * @pid_callback_data) will be called to inform about the PID of the
3011 * If application launching occurs via some other mechanism (eg: D-Bus
3012 * activation) then @spawn_flags, @user_setup, @user_setup_data,
3013 * @pid_callback and @pid_callback_data are ignored.
3015 * Returns: %TRUE on successful launch, %FALSE otherwise.
3018 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
3020 GAppLaunchContext *launch_context,
3021 GSpawnFlags spawn_flags,
3022 GSpawnChildSetupFunc user_setup,
3023 gpointer user_setup_data,
3024 GDesktopAppLaunchCallback pid_callback,
3025 gpointer pid_callback_data,
3028 return g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
3039 /* OnlyShowIn API support {{{2 */
3042 * g_desktop_app_info_set_desktop_env:
3043 * @desktop_env: a string specifying what desktop this is
3045 * Sets the name of the desktop that the application is running in.
3046 * This is used by g_app_info_should_show() and
3047 * g_desktop_app_info_get_show_in() to evaluate the
3048 * `OnlyShowIn` and `NotShowIn`
3049 * desktop entry fields.
3051 * Should be called only once; subsequent calls are ignored.
3053 * Deprecated:2.42:do not use this API. Since 2.42 the value of the
3054 * `XDG_CURRENT_DESKTOP` environment variable will be used.
3057 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
3059 get_current_desktops (desktop_env);
3063 g_desktop_app_info_should_show (GAppInfo *appinfo)
3065 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3067 if (info->nodisplay)
3070 return g_desktop_app_info_get_show_in (info, NULL);
3073 /* mime types/default apps support {{{2 */
3082 ensure_dir (DirType type,
3085 char *path, *display_name;
3091 path = g_build_filename (g_get_user_config_dir (), NULL);
3095 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
3099 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
3103 g_assert_not_reached ();
3107 if (g_mkdir_with_parents (path, 0700) == 0)
3111 display_name = g_filename_display_name (path);
3112 if (type == APP_DIR)
3113 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
3114 _("Can't create user application configuration folder %s: %s"),
3115 display_name, g_strerror (errsv));
3117 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
3118 _("Can't create user MIME configuration folder %s: %s"),
3119 display_name, g_strerror (errsv));
3121 g_free (display_name);
3128 update_mimeapps_list (const char *desktop_id,
3129 const char *content_type,
3130 UpdateMimeFlags flags,
3133 char *dirname, *filename, *string;
3135 gboolean load_succeeded, res;
3136 char **old_list, **list;
3137 gsize length, data_size;
3140 char **content_types;
3142 /* Don't add both at start and end */
3143 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
3144 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
3146 dirname = ensure_dir (CONF_DIR, error);
3150 filename = g_build_filename (dirname, "mimeapps.list", NULL);
3153 key_file = g_key_file_new ();
3154 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
3155 if (!load_succeeded ||
3156 (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) &&
3157 !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) &&
3158 !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP)))
3160 g_key_file_free (key_file);
3161 key_file = g_key_file_new ();
3166 content_types = g_new (char *, 2);
3167 content_types[0] = g_strdup (content_type);
3168 content_types[1] = NULL;
3172 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
3175 for (k = 0; content_types && content_types[k]; k++)
3177 /* set as default, if requested so */
3178 string = g_key_file_get_string (key_file,
3179 DEFAULT_APPLICATIONS_GROUP,
3183 if (g_strcmp0 (string, desktop_id) != 0 &&
3184 (flags & UPDATE_MIME_SET_DEFAULT))
3187 string = g_strdup (desktop_id);
3189 /* add in the non-default list too, if it's not already there */
3190 flags |= UPDATE_MIME_SET_NON_DEFAULT;
3193 if (string == NULL || desktop_id == NULL)
3194 g_key_file_remove_key (key_file,
3195 DEFAULT_APPLICATIONS_GROUP,
3199 g_key_file_set_string (key_file,
3200 DEFAULT_APPLICATIONS_GROUP,
3209 /* reuse the list from above */
3213 g_strfreev (content_types);
3214 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
3217 for (k = 0; content_types && content_types[k]; k++)
3219 /* Add to the right place in the list */
3222 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
3223 content_types[k], &length, NULL);
3225 list = g_new (char *, 1 + length + 1);
3229 /* if we're adding a last-used hint, just put the application in front of the list */
3230 if (flags & UPDATE_MIME_SET_LAST_USED)
3232 /* avoid adding this again as non-default later */
3233 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
3234 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
3236 list[i++] = g_strdup (desktop_id);
3241 for (j = 0; old_list[j] != NULL; j++)
3243 if (g_strcmp0 (old_list[j], desktop_id) != 0)
3245 /* rewrite other entries if they're different from the new one */
3246 list[i++] = g_strdup (old_list[j]);
3248 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
3250 /* we encountered an old entry which is equal to the one we're adding as non-default,
3251 * don't change its position in the list.
3253 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
3254 list[i++] = g_strdup (old_list[j]);
3259 /* add it at the end of the list */
3260 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
3261 list[i++] = g_strdup (desktop_id);
3265 g_strfreev (old_list);
3267 if (list[0] == NULL || desktop_id == NULL)
3268 g_key_file_remove_key (key_file,
3269 ADDED_ASSOCIATIONS_GROUP,
3273 g_key_file_set_string_list (key_file,
3274 ADDED_ASSOCIATIONS_GROUP,
3276 (const char * const *)list, i);
3283 /* reuse the list from above */
3287 g_strfreev (content_types);
3288 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
3291 for (k = 0; content_types && content_types[k]; k++)
3293 /* Remove from removed associations group (unless remove) */
3296 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
3297 content_types[k], &length, NULL);
3299 list = g_new (char *, 1 + length + 1);
3302 if (flags & UPDATE_MIME_REMOVE)
3303 list[i++] = g_strdup (desktop_id);
3306 for (j = 0; old_list[j] != NULL; j++)
3308 if (g_strcmp0 (old_list[j], desktop_id) != 0)
3309 list[i++] = g_strdup (old_list[j]);
3314 g_strfreev (old_list);
3316 if (list[0] == NULL || desktop_id == NULL)
3317 g_key_file_remove_key (key_file,
3318 REMOVED_ASSOCIATIONS_GROUP,
3322 g_key_file_set_string_list (key_file,
3323 REMOVED_ASSOCIATIONS_GROUP,
3325 (const char * const *)list, i);
3330 g_strfreev (content_types);
3332 data = g_key_file_to_data (key_file, &data_size, error);
3333 g_key_file_free (key_file);
3335 res = g_file_set_contents (filename, data, data_size, error);
3337 desktop_file_dirs_invalidate_user_config ();
3346 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
3347 const char *content_type,
3350 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3352 if (!g_desktop_app_info_ensure_saved (info, error))
3355 if (!info->desktop_id)
3357 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
3358 _("Application information lacks an identifier"));
3362 /* both add support for the content type and set as last used */
3363 return update_mimeapps_list (info->desktop_id, content_type,
3364 UPDATE_MIME_SET_NON_DEFAULT |
3365 UPDATE_MIME_SET_LAST_USED,
3370 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
3371 const char *content_type,
3374 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3376 if (!g_desktop_app_info_ensure_saved (info, error))
3379 if (!info->desktop_id)
3381 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
3382 _("Application information lacks an identifier"));
3386 return update_mimeapps_list (info->desktop_id, content_type,
3387 UPDATE_MIME_SET_DEFAULT,
3392 update_program_done (GPid pid,
3396 /* Did the application exit correctly */
3397 if (g_spawn_check_exit_status (status, NULL))
3399 /* Here we could clean out any caches in use */
3404 run_update_command (char *command,
3413 GError *error = NULL;
3416 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
3418 if (g_spawn_async ("/", argv,
3420 G_SPAWN_SEARCH_PATH |
3421 G_SPAWN_STDOUT_TO_DEV_NULL |
3422 G_SPAWN_STDERR_TO_DEV_NULL |
3423 G_SPAWN_DO_NOT_REAP_CHILD,
3424 NULL, NULL, /* No setup function */
3427 g_child_watch_add (pid, update_program_done, NULL);
3430 /* If we get an error at this point, it's quite likely the user doesn't
3431 * have an installed copy of either 'update-mime-database' or
3432 * 'update-desktop-database'. I don't think we want to popup an error
3433 * dialog at this point, so we just do a g_warning to give the user a
3434 * chance of debugging it.
3436 g_warning ("%s", error->message);
3443 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
3444 const char *extension,
3447 char *filename, *basename, *mimetype;
3451 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
3454 dirname = ensure_dir (MIMETYPE_DIR, error);
3458 basename = g_strdup_printf ("user-extension-%s.xml", extension);
3459 filename = g_build_filename (dirname, basename, NULL);
3463 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
3465 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
3470 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3471 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
3472 " <mime-type type=\"%s\">\n"
3473 " <comment>%s document</comment>\n"
3474 " <glob pattern=\"*.%s\"/>\n"
3476 "</mime-info>\n", mimetype, extension, extension);
3478 g_file_set_contents (filename, contents, -1, NULL);
3481 run_update_command ("update-mime-database", "mime");
3485 res = g_desktop_app_info_set_as_default_for_type (appinfo,
3495 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
3496 const char *content_type,
3499 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3501 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
3504 return update_mimeapps_list (info->desktop_id, content_type,
3505 UPDATE_MIME_SET_NON_DEFAULT,
3510 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
3516 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
3517 const char *content_type,
3520 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3522 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
3525 return update_mimeapps_list (info->desktop_id, content_type,
3530 static const char **
3531 g_desktop_app_info_get_supported_types (GAppInfo *appinfo)
3533 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3535 return (const char**) info->mime_types;
3538 /* Saving and deleting {{{2 */
3541 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
3547 char *data, *desktop_id;
3552 if (info->filename != NULL)
3555 /* This is only used for object created with
3556 * g_app_info_create_from_commandline. All other
3557 * object should have a filename
3560 dirname = ensure_dir (APP_DIR, error);
3564 key_file = g_key_file_new ();
3566 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3567 "Encoding", "UTF-8");
3568 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3569 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
3570 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3571 G_KEY_FILE_DESKTOP_KEY_TYPE,
3572 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
3574 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3575 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
3576 if (info->nodisplay)
3577 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3578 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
3580 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3581 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
3583 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3584 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
3586 if (info->generic_name != NULL)
3587 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3588 GENERIC_NAME_KEY, info->generic_name);
3590 if (info->fullname != NULL)
3591 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3592 FULL_NAME_KEY, info->fullname);
3594 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3595 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
3597 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3598 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
3600 data = g_key_file_to_data (key_file, &data_size, NULL);
3601 g_key_file_free (key_file);
3603 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
3604 filename = g_build_filename (dirname, desktop_id, NULL);
3605 g_free (desktop_id);
3608 fd = g_mkstemp (filename);
3613 display_name = g_filename_display_name (filename);
3614 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
3615 _("Can't create user desktop file %s"), display_name);
3616 g_free (display_name);
3622 desktop_id = g_path_get_basename (filename);
3624 /* FIXME - actually handle error */
3625 (void) g_close (fd, NULL);
3627 res = g_file_set_contents (filename, data, data_size, error);
3631 g_free (desktop_id);
3636 info->filename = filename;
3637 info->desktop_id = desktop_id;
3639 run_update_command ("update-desktop-database", "applications");
3641 /* We just dropped a file in the user's desktop file directory. Save
3642 * the monitor the bother of having to notice it and invalidate
3645 * This means that calls directly following this will be able to see
3646 * the results immediately.
3648 desktop_file_dirs_invalidate_user_data ();
3654 g_desktop_app_info_can_delete (GAppInfo *appinfo)
3656 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3660 if (strstr (info->filename, "/userapp-"))
3661 return g_access (info->filename, W_OK) == 0;
3668 g_desktop_app_info_delete (GAppInfo *appinfo)
3670 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3674 if (g_remove (info->filename) == 0)
3676 update_mimeapps_list (info->desktop_id, NULL,
3680 g_free (info->filename);
3681 info->filename = NULL;
3682 g_free (info->desktop_id);
3683 info->desktop_id = NULL;
3692 /* Create for commandline {{{2 */
3694 * g_app_info_create_from_commandline:
3695 * @commandline: the commandline to use
3696 * @application_name: (allow-none): the application name, or %NULL to use @commandline
3697 * @flags: flags that can specify details of the created #GAppInfo
3698 * @error: a #GError location to store the error occurring, %NULL to ignore.
3700 * Creates a new #GAppInfo from the given information.
3702 * Note that for @commandline, the quoting rules of the Exec key of the
3703 * [freedesktop.org Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec)
3704 * are applied. For example, if the @commandline contains
3705 * percent-encoded URIs, the percent-character must be doubled in order to prevent it from
3706 * being swallowed by Exec key unquoting. See the specification for exact quoting rules.
3708 * Returns: (transfer full): new #GAppInfo for given command.
3711 g_app_info_create_from_commandline (const char *commandline,
3712 const char *application_name,
3713 GAppInfoCreateFlags flags,
3718 GDesktopAppInfo *info;
3720 g_return_val_if_fail (commandline, NULL);
3722 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
3724 info->filename = NULL;
3725 info->desktop_id = NULL;
3727 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
3728 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
3729 info->hidden = FALSE;
3730 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
3731 info->exec = g_strconcat (commandline, " %u", NULL);
3733 info->exec = g_strconcat (commandline, " %f", NULL);
3734 info->nodisplay = TRUE;
3735 info->binary = binary_from_exec (info->exec);
3737 if (application_name)
3738 info->name = g_strdup (application_name);
3741 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
3742 split = g_strsplit (commandline, " ", 2);
3743 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
3745 info->name = basename;
3746 if (info->name == NULL)
3747 info->name = g_strdup ("custom");
3749 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
3751 return G_APP_INFO (info);
3754 /* GAppInfo interface init */
3757 g_desktop_app_info_iface_init (GAppInfoIface *iface)
3759 iface->dup = g_desktop_app_info_dup;
3760 iface->equal = g_desktop_app_info_equal;
3761 iface->get_id = g_desktop_app_info_get_id;
3762 iface->get_name = g_desktop_app_info_get_name;
3763 iface->get_description = g_desktop_app_info_get_description;
3764 iface->get_executable = g_desktop_app_info_get_executable;
3765 iface->get_icon = g_desktop_app_info_get_icon;
3766 iface->launch = g_desktop_app_info_launch;
3767 iface->supports_uris = g_desktop_app_info_supports_uris;
3768 iface->supports_files = g_desktop_app_info_supports_files;
3769 iface->launch_uris = g_desktop_app_info_launch_uris;
3770 iface->should_show = g_desktop_app_info_should_show;
3771 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
3772 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
3773 iface->add_supports_type = g_desktop_app_info_add_supports_type;
3774 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
3775 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
3776 iface->can_delete = g_desktop_app_info_can_delete;
3777 iface->do_delete = g_desktop_app_info_delete;
3778 iface->get_commandline = g_desktop_app_info_get_commandline;
3779 iface->get_display_name = g_desktop_app_info_get_display_name;
3780 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
3781 iface->get_supported_types = g_desktop_app_info_get_supported_types;
3784 /* Recommended applications {{{2 */
3786 /* Converts content_type into a list of itself with all of its parent
3787 * types (if include_fallback is enabled) or just returns a single-item
3788 * list with the unaliased content type.
3791 get_list_of_mimetypes (const gchar *content_type,
3792 gboolean include_fallback)
3797 array = g_ptr_array_new ();
3798 unaliased = _g_unix_content_type_unalias (content_type);
3799 g_ptr_array_add (array, unaliased);
3801 if (include_fallback)
3805 /* Iterate the array as we grow it, until we have nothing more to add */
3806 for (i = 0; i < array->len; i++)
3808 gchar **parents = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
3811 for (j = 0; parents[j]; j++)
3812 /* Don't add duplicates */
3813 if (!array_contains (array, parents[j]))
3814 g_ptr_array_add (array, parents[j]);
3816 g_free (parents[j]);
3818 /* We already stole or freed each element. Free the container. */
3823 g_ptr_array_add (array, NULL);
3825 return (gchar **) g_ptr_array_free (array, FALSE);
3829 g_desktop_app_info_get_desktop_ids_for_content_type (const gchar *content_type,
3830 gboolean include_fallback)
3832 GPtrArray *hits, *blacklist;
3836 hits = g_ptr_array_new ();
3837 blacklist = g_ptr_array_new ();
3839 types = get_list_of_mimetypes (content_type, include_fallback);
3841 desktop_file_dirs_lock ();
3843 for (i = 0; types[i]; i++)
3844 for (j = 0; j < n_desktop_file_dirs; j++)
3845 desktop_file_dir_mime_lookup (&desktop_file_dirs[j], types[i], hits, blacklist);
3847 desktop_file_dirs_unlock ();
3849 g_ptr_array_add (hits, NULL);
3851 g_ptr_array_free (blacklist, TRUE);
3854 return (gchar **) g_ptr_array_free (hits, FALSE);
3858 g_desktop_app_info_get_defaults_for_content_type (const gchar *content_type)
3864 types = get_list_of_mimetypes (content_type, TRUE);
3865 results = g_ptr_array_new ();
3867 desktop_file_dirs_lock ();
3869 for (i = 0; types[i]; i++)
3870 for (j = 0; j < n_desktop_file_dirs; j++)
3871 desktop_file_dir_default_lookup (&desktop_file_dirs[j], types[i], results);
3873 desktop_file_dirs_unlock ();
3875 g_ptr_array_add (results, NULL);
3878 return (gchar **) g_ptr_array_free (results, FALSE);
3882 * g_app_info_get_recommended_for_type:
3883 * @content_type: the content type to find a #GAppInfo for
3885 * Gets a list of recommended #GAppInfos for a given content type, i.e.
3886 * those applications which claim to support the given content type exactly,
3887 * and not by MIME type subclassing.
3888 * Note that the first application of the list is the last used one, i.e.
3889 * the last one for which g_app_info_set_as_last_used_for_type() has been
3892 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
3893 * for given @content_type or %NULL on error.
3898 g_app_info_get_recommended_for_type (const gchar *content_type)
3900 gchar **desktop_ids;
3904 g_return_val_if_fail (content_type != NULL, NULL);
3906 desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, FALSE);
3909 for (i = 0; desktop_ids[i]; i++)
3911 GDesktopAppInfo *info;
3913 info = g_desktop_app_info_new (desktop_ids[i]);
3915 infos = g_list_prepend (infos, info);
3918 g_strfreev (desktop_ids);
3920 return g_list_reverse (infos);
3924 * g_app_info_get_fallback_for_type:
3925 * @content_type: the content type to find a #GAppInfo for
3927 * Gets a list of fallback #GAppInfos for a given content type, i.e.
3928 * those applications which claim to support the given content type
3929 * by MIME type subclassing and not directly.
3931 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
3932 * for given @content_type or %NULL on error.
3937 g_app_info_get_fallback_for_type (const gchar *content_type)
3939 gchar **recommended_ids;
3944 g_return_val_if_fail (content_type != NULL, NULL);
3946 recommended_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, FALSE);
3947 all_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE);
3950 for (i = 0; all_ids[i]; i++)
3952 GDesktopAppInfo *info;
3955 /* Don't return the ones on the recommended list */
3956 for (j = 0; recommended_ids[j]; j++)
3957 if (g_str_equal (all_ids[i], recommended_ids[j]))
3960 if (recommended_ids[j])
3963 info = g_desktop_app_info_new (all_ids[i]);
3966 infos = g_list_prepend (infos, info);
3969 g_strfreev (recommended_ids);
3970 g_strfreev (all_ids);
3972 return g_list_reverse (infos);
3976 * g_app_info_get_all_for_type:
3977 * @content_type: the content type to find a #GAppInfo for
3979 * Gets a list of all #GAppInfos for a given content type,
3980 * including the recommended and fallback #GAppInfos. See
3981 * g_app_info_get_recommended_for_type() and
3982 * g_app_info_get_fallback_for_type().
3984 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
3985 * for given @content_type or %NULL on error.
3988 g_app_info_get_all_for_type (const char *content_type)
3990 gchar **desktop_ids;
3994 g_return_val_if_fail (content_type != NULL, NULL);
3996 desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE);
3999 for (i = 0; desktop_ids[i]; i++)
4001 GDesktopAppInfo *info;
4003 info = g_desktop_app_info_new (desktop_ids[i]);
4005 infos = g_list_prepend (infos, info);
4008 g_strfreev (desktop_ids);
4010 return g_list_reverse (infos);
4014 * g_app_info_reset_type_associations:
4015 * @content_type: a content type
4017 * Removes all changes to the type associations done by
4018 * g_app_info_set_as_default_for_type(),
4019 * g_app_info_set_as_default_for_extension(),
4020 * g_app_info_add_supports_type() or
4021 * g_app_info_remove_supports_type().
4026 g_app_info_reset_type_associations (const char *content_type)
4028 update_mimeapps_list (NULL, content_type,
4034 * g_app_info_get_default_for_type:
4035 * @content_type: the content type to find a #GAppInfo for
4036 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
4039 * Gets the default #GAppInfo for a given content type.
4041 * Returns: (transfer full): #GAppInfo for given @content_type or
4045 g_app_info_get_default_for_type (const char *content_type,
4046 gboolean must_support_uris)
4048 gchar **desktop_ids;
4052 g_return_val_if_fail (content_type != NULL, NULL);
4054 desktop_ids = g_desktop_app_info_get_defaults_for_content_type (content_type);
4057 for (i = 0; desktop_ids[i]; i++)
4059 info = (GAppInfo *) g_desktop_app_info_new (desktop_ids[i]);
4063 if (!must_support_uris || g_app_info_supports_uris (info))
4066 g_object_unref (info);
4071 g_strfreev (desktop_ids);
4073 /* If we can't find a default app for this content type, pick one from
4074 * the list of all supported apps. This will be ordered by the user's
4075 * preference and by "recommended" apps first, so the first one we
4076 * find is probably the best fallback.
4080 desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE);
4082 for (i = 0; desktop_ids[i]; i++)
4084 info = (GAppInfo *) g_desktop_app_info_new (desktop_ids[i]);
4088 if (!must_support_uris || g_app_info_supports_uris (info))
4091 g_object_unref (info);
4096 g_strfreev (desktop_ids);
4103 * g_app_info_get_default_for_uri_scheme:
4104 * @uri_scheme: a string containing a URI scheme.
4106 * Gets the default application for handling URIs with
4107 * the given URI scheme. A URI scheme is the initial part
4108 * of the URI, up to but not including the ':', e.g. "http",
4111 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
4114 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
4117 char *content_type, *scheme_down;
4119 scheme_down = g_ascii_strdown (uri_scheme, -1);
4120 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
4121 g_free (scheme_down);
4122 app_info = g_app_info_get_default_for_type (content_type, FALSE);
4123 g_free (content_type);
4128 /* "Get all" API {{{2 */
4131 * g_desktop_app_info_get_implementations:
4132 * @interface: the name of the interface
4134 * Gets all applications that implement @interface.
4136 * An application implements an interface if that interface is listed in
4137 * the Implements= line of the desktop file of the application.
4139 * Returns: (element-type GDesktopAppInfo) (transfer full): a list of #GDesktopAppInfo
4145 g_desktop_app_info_get_implementations (const gchar *interface)
4147 GList *result = NULL;
4151 desktop_file_dirs_lock ();
4153 for (i = 0; i < n_desktop_file_dirs; i++)
4154 desktop_file_dir_get_implementations (&desktop_file_dirs[i], &result, interface);
4156 desktop_file_dirs_unlock ();
4161 gchar *name = (*ptr)->data;
4162 GDesktopAppInfo *app;
4164 app = g_desktop_app_info_new (name);
4170 ptr = &(*ptr)->next;
4173 *ptr = g_list_delete_link (*ptr, *ptr);
4180 * g_desktop_app_info_search:
4181 * @search_string: the search string to use
4183 * Searches desktop files for ones that match @search_string.
4185 * The return value is an array of strvs. Each strv contains a list of
4186 * applications that matched @search_string with an equal score. The
4187 * outer list is sorted by score so that the first strv contains the
4188 * best-matching applications, and so on.
4189 * The algorithm for determining matches is undefined and may change at
4192 * Returns: (array zero-terminated=1) (element-type GStrv) (transfer full): a
4193 * list of strvs. Free each item with g_strfreev() and free the outer
4194 * list with g_free().
4197 g_desktop_app_info_search (const gchar *search_string)
4199 gchar **search_tokens;
4200 gint last_category = -1;
4202 gint n_categories = 0;
4203 gint start_of_category;
4206 search_tokens = g_str_tokenize_and_fold (search_string, NULL, NULL);
4208 desktop_file_dirs_lock ();
4210 reset_total_search_results ();
4212 for (i = 0; i < n_desktop_file_dirs; i++)
4214 for (j = 0; search_tokens[j]; j++)
4216 desktop_file_dir_search (&desktop_file_dirs[i], search_tokens[j]);
4217 merge_token_results (j == 0);
4219 merge_directory_results ();
4222 sort_total_search_results ();
4224 /* Count the total number of unique categories */
4225 for (i = 0; i < static_total_results_size; i++)
4226 if (static_total_results[i].category != last_category)
4228 last_category = static_total_results[i].category;
4232 results = g_new (gchar **, n_categories + 1);
4234 /* Start loading into the results list */
4235 start_of_category = 0;
4236 for (i = 0; i < n_categories; i++)
4238 gint n_items_in_category = 0;
4242 this_category = static_total_results[start_of_category].category;
4244 while (start_of_category + n_items_in_category < static_total_results_size &&
4245 static_total_results[start_of_category + n_items_in_category].category == this_category)
4246 n_items_in_category++;
4248 results[i] = g_new (gchar *, n_items_in_category + 1);
4249 for (j = 0; j < n_items_in_category; j++)
4250 results[i][j] = g_strdup (static_total_results[start_of_category + j].app_name);
4251 results[i][j] = NULL;
4253 start_of_category += n_items_in_category;
4257 desktop_file_dirs_unlock ();
4259 g_strfreev (search_tokens);
4265 * g_app_info_get_all:
4267 * Gets a list of all of the applications currently registered
4270 * For desktop files, this includes applications that have
4271 * `NoDisplay=true` set or are excluded from display by means
4272 * of `OnlyShowIn` or `NotShowIn`. See g_app_info_should_show().
4273 * The returned list does not include applications which have
4274 * the `Hidden` key set.
4276 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfos.
4279 g_app_info_get_all (void)
4282 GHashTableIter iter;
4287 apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
4289 desktop_file_dirs_lock ();
4291 for (i = 0; i < n_desktop_file_dirs; i++)
4292 desktop_file_dir_get_all (&desktop_file_dirs[i], apps);
4294 desktop_file_dirs_unlock ();
4297 g_hash_table_iter_init (&iter, apps);
4298 while (g_hash_table_iter_next (&iter, NULL, &value))
4301 infos = g_list_prepend (infos, value);
4304 g_hash_table_destroy (apps);
4309 /* GDesktopAppInfoLookup interface {{{2 */
4311 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
4313 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
4314 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
4317 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
4321 /* "Get for mime type" APIs {{{2 */
4324 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
4325 * @lookup: a #GDesktopAppInfoLookup
4326 * @uri_scheme: a string containing a URI scheme.
4328 * Gets the default application for launching applications
4329 * using this URI scheme for a particular GDesktopAppInfoLookup
4332 * The GDesktopAppInfoLookup interface and this function is used
4333 * to implement g_app_info_get_default_for_uri_scheme() backends
4334 * in a GIO module. There is no reason for applications to use it
4335 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
4337 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
4339 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
4342 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
4343 const char *uri_scheme)
4345 GDesktopAppInfoLookupIface *iface;
4347 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
4349 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
4351 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
4354 G_GNUC_END_IGNORE_DEPRECATIONS
4356 /* Misc getter APIs {{{2 */
4359 * g_desktop_app_info_get_startup_wm_class:
4360 * @info: a #GDesktopAppInfo that supports startup notify
4362 * Retrieves the StartupWMClass field from @info. This represents the
4363 * WM_CLASS property of the main window of the application, if launched
4366 * Returns: (transfer none): the startup WM class, or %NULL if none is set
4367 * in the desktop file.
4372 g_desktop_app_info_get_startup_wm_class (GDesktopAppInfo *info)
4374 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4376 return info->startup_wm_class;
4380 * g_desktop_app_info_get_string:
4381 * @info: a #GDesktopAppInfo
4382 * @key: the key to look up
4384 * Looks up a string value in the keyfile backing @info.
4386 * The @key is looked up in the "Desktop Entry" group.
4388 * Returns: a newly allocated string, or %NULL if the key
4394 g_desktop_app_info_get_string (GDesktopAppInfo *info,
4397 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4399 return g_key_file_get_string (info->keyfile,
4400 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4404 * g_desktop_app_info_get_boolean:
4405 * @info: a #GDesktopAppInfo
4406 * @key: the key to look up
4408 * Looks up a boolean value in the keyfile backing @info.
4410 * The @key is looked up in the "Desktop Entry" group.
4412 * Returns: the boolean value, or %FALSE if the key
4418 g_desktop_app_info_get_boolean (GDesktopAppInfo *info,
4421 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
4423 return g_key_file_get_boolean (info->keyfile,
4424 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4428 * g_desktop_app_info_has_key:
4429 * @info: a #GDesktopAppInfo
4430 * @key: the key to look up
4432 * Returns whether @key exists in the "Desktop Entry" group
4433 * of the keyfile backing @info.
4435 * Returns: %TRUE if the @key exists
4440 g_desktop_app_info_has_key (GDesktopAppInfo *info,
4443 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
4445 return g_key_file_has_key (info->keyfile,
4446 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4449 /* Desktop actions support {{{2 */
4452 * g_desktop_app_info_list_actions:
4453 * @info: a #GDesktopAppInfo
4455 * Returns the list of "additional application actions" supported on the
4456 * desktop file, as per the desktop file specification.
4458 * As per the specification, this is the list of actions that are
4459 * explicitly listed in the "Actions" key of the [Desktop Entry] group.
4461 * Returns: (array zero-terminated=1) (element-type utf8) (transfer none): a list of strings, always non-%NULL
4465 const gchar * const *
4466 g_desktop_app_info_list_actions (GDesktopAppInfo *info)
4468 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4470 return (const gchar **) info->actions;
4474 app_info_has_action (GDesktopAppInfo *info,
4475 const gchar *action_name)
4479 for (i = 0; info->actions[i]; i++)
4480 if (g_str_equal (info->actions[i], action_name))
4487 * g_desktop_app_info_get_action_name:
4488 * @info: a #GDesktopAppInfo
4489 * @action_name: the name of the action as from
4490 * g_desktop_app_info_list_actions()
4492 * Gets the user-visible display name of the "additional application
4493 * action" specified by @action_name.
4495 * This corresponds to the "Name" key within the keyfile group for the
4498 * Returns: (transfer full): the locale-specific action name
4503 g_desktop_app_info_get_action_name (GDesktopAppInfo *info,
4504 const gchar *action_name)
4509 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4510 g_return_val_if_fail (action_name != NULL, NULL);
4511 g_return_val_if_fail (app_info_has_action (info, action_name), NULL);
4513 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4514 result = g_key_file_get_locale_string (info->keyfile, group_name, "Name", NULL, NULL);
4515 g_free (group_name);
4517 /* The spec says that the Name field must be given.
4519 * If it's not, let's follow the behaviour of our get_name()
4520 * implementation above and never return %NULL.
4523 result = g_strdup (_("Unnamed"));
4529 * g_desktop_app_info_launch_action:
4530 * @info: a #GDesktopAppInfo
4531 * @action_name: the name of the action as from
4532 * g_desktop_app_info_list_actions()
4533 * @launch_context: (allow-none): a #GAppLaunchContext
4535 * Activates the named application action.
4537 * You may only call this function on action names that were
4538 * returned from g_desktop_app_info_list_actions().
4540 * Note that if the main entry of the desktop file indicates that the
4541 * application supports startup notification, and @launch_context is
4542 * non-%NULL, then startup notification will be used when activating the
4543 * action (and as such, invocation of the action on the receiving side
4544 * must signal the end of startup notification when it is completed).
4545 * This is the expected behaviour of applications declaring additional
4546 * actions, as per the desktop file specification.
4548 * As with g_app_info_launch() there is no way to detect failures that
4549 * occur while using this function.
4554 g_desktop_app_info_launch_action (GDesktopAppInfo *info,
4555 const gchar *action_name,
4556 GAppLaunchContext *launch_context)
4558 GDBusConnection *session_bus;
4560 g_return_if_fail (G_IS_DESKTOP_APP_INFO (info));
4561 g_return_if_fail (action_name != NULL);
4562 g_return_if_fail (app_info_has_action (info, action_name));
4564 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
4566 if (session_bus && info->app_id)
4570 object_path = object_path_from_appid (info->app_id);
4571 g_dbus_connection_call (session_bus, info->app_id, object_path,
4572 "org.freedesktop.Application", "ActivateAction",
4573 g_variant_new ("(sav@a{sv})", action_name, NULL,
4574 g_desktop_app_info_make_platform_data (info, NULL, launch_context)),
4575 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
4576 g_free (object_path);
4583 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4584 exec_line = g_key_file_get_string (info->keyfile, group_name, "Exec", NULL);
4585 g_free (group_name);
4588 g_desktop_app_info_launch_uris_with_spawn (info, session_bus, exec_line, NULL, launch_context,
4589 _SPAWN_FLAGS_DEFAULT, NULL, NULL, NULL, NULL, NULL);
4592 if (session_bus != NULL)
4594 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
4595 g_object_unref (session_bus);
4600 /* vim:set foldmethod=marker: */