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 GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type,
83 gboolean include_fallback,
84 char **explicit_default);
85 static void mime_info_cache_reload (const char *dir);
86 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
92 * Information about an installed application from a desktop file.
94 struct _GDesktopAppInfo
96 GObject parent_instance;
118 char *startup_wm_class;
125 guint startup_notify : 1;
130 UPDATE_MIME_NONE = 1 << 0,
131 UPDATE_MIME_SET_DEFAULT = 1 << 1,
132 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
133 UPDATE_MIME_REMOVE = 1 << 3,
134 UPDATE_MIME_SET_LAST_USED = 1 << 4,
137 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
138 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO, g_desktop_app_info_iface_init))
140 G_LOCK_DEFINE_STATIC (g_desktop_env);
141 static gchar *g_desktop_env = NULL;
143 /* DesktopFileDir implementation {{{1 */
148 GLocalDirectoryMonitor *monitor;
149 GHashTable *app_names;
151 GHashTable *memory_index;
154 static DesktopFileDir *desktop_file_dirs;
155 static guint n_desktop_file_dirs;
156 static GMutex desktop_file_dir_lock;
158 /* Monitor 'changed' signal handler {{{2 */
159 static void desktop_file_dir_reset (DesktopFileDir *dir);
162 desktop_file_dir_changed (GFileMonitor *monitor,
165 GFileMonitorEvent event_type,
168 DesktopFileDir *dir = user_data;
170 /* We are not interested in receiving notifications forever just
171 * because someone asked about one desktop file once.
173 * After we receive the first notification, reset the dir, destroying
174 * the monitor. We will take this as a hint, next time that we are
175 * asked, that we need to check if everything is up to date.
177 g_mutex_lock (&desktop_file_dir_lock);
179 desktop_file_dir_reset (dir);
181 g_mutex_unlock (&desktop_file_dir_lock);
183 /* Notify anyone else who may be interested */
184 g_app_info_monitor_fire ();
187 /* Internal utility functions {{{2 */
190 * desktop_file_dir_app_name_is_masked:
191 * @dir: a #DesktopFileDir
192 * @app_name: an application ID
194 * Checks if @app_name is masked for @dir.
196 * An application is masked if a similarly-named desktop file exists in
197 * a desktop file directory with higher precedence. Masked desktop
198 * files should be ignored.
201 desktop_file_dir_app_name_is_masked (DesktopFileDir *dir,
202 const gchar *app_name)
204 while (dir > desktop_file_dirs)
208 if (dir->app_names && g_hash_table_contains (dir->app_names, app_name))
216 * add_to_table_if_appropriate:
217 * @apps: a string to GDesktopAppInfo hash table
218 * @app_name: the name of the application
219 * @info: a #GDesktopAppInfo, or NULL
221 * If @info is non-%NULL and non-hidden, then add it to @apps, using
222 * @app_name as a key.
224 * If @info is non-%NULL then this function will consume the passed-in
228 add_to_table_if_appropriate (GHashTable *apps,
229 const gchar *app_name,
230 GDesktopAppInfo *info)
237 g_object_unref (info);
241 g_free (info->desktop_id);
242 info->desktop_id = g_strdup (app_name);
244 g_hash_table_insert (apps, g_strdup (info->desktop_id), info);
251 DESKTOP_KEY_GenericName,
252 DESKTOP_KEY_Keywords,
254 DESKTOP_KEY_X_GNOME_FullName,
259 const gchar desktop_key_match_category[N_DESKTOP_KEYS] = {
260 /* Note: lower numbers are a better match.
262 * In case we want two keys to match at the same level, we can just
263 * use the same number for the two different keys.
265 [DESKTOP_KEY_Name] = 1,
266 [DESKTOP_KEY_Exec] = 2,
267 [DESKTOP_KEY_Keywords] = 3,
268 [DESKTOP_KEY_GenericName] = 4,
269 [DESKTOP_KEY_X_GNOME_FullName] = 5,
270 [DESKTOP_KEY_Comment] = 6
274 desktop_key_get_name (guint key_id)
278 case DESKTOP_KEY_Comment:
280 case DESKTOP_KEY_Exec:
282 case DESKTOP_KEY_GenericName:
283 return "GenericName";
284 case DESKTOP_KEY_Keywords:
286 case DESKTOP_KEY_Name:
288 case DESKTOP_KEY_X_GNOME_FullName:
289 return "X-GNOME-FullName";
291 g_assert_not_reached ();
295 /* Search global state {{{2
297 * We only ever search under a global lock, so we can use (and reuse)
298 * some global data to reduce allocations made while searching.
300 * In short, we keep around arrays of results that we expand as needed
301 * (and never shrink).
303 * static_token_results: this is where we append the results for each
304 * token within a given desktop directory, as we handle it (which is
305 * a union of all matches for this term)
307 * static_search_results: this is where we build the complete results
308 * for a single directory (which is an intersection of the matches
309 * found for each term)
311 * static_total_results: this is where we build the complete results
312 * across all directories (which is a union of the matches found in
315 * The app_names that enter these tables are always pointer-unique (in
316 * the sense that string equality is the same as pointer equality).
317 * This can be guaranteed for two reasons:
319 * - we mask appids so that a given appid will only ever appear within
320 * the highest-precedence directory that contains it. We never
321 * return search results from a lower-level directory if a desktop
322 * file exists in a higher-level one.
324 * - within a given directory, the string is unique because it's the
325 * key in the hashtable of all app_ids for that directory.
327 * We perform a merging of the results in merge_token_results(). This
328 * works by ordering the two lists and moving through each of them (at
329 * the same time) looking for common elements, rejecting uncommon ones.
330 * "Order" here need not mean any particular thing, as long as it is
331 * some order. Because of the uniqueness of our strings, we can use
332 * pointer order. That's what's going on in compare_results() below.
336 const gchar *app_name;
340 static struct search_result *static_token_results;
341 static gint static_token_results_size;
342 static gint static_token_results_allocated;
343 static struct search_result *static_search_results;
344 static gint static_search_results_size;
345 static gint static_search_results_allocated;
346 static struct search_result *static_total_results;
347 static gint static_total_results_size;
348 static gint static_total_results_allocated;
350 /* And some functions for performing nice operations against it */
352 compare_results (gconstpointer a,
355 const struct search_result *ra = a;
356 const struct search_result *rb = b;
358 if (ra->app_name < rb->app_name)
361 else if (ra->app_name > rb->app_name)
365 return ra->category - rb->category;
369 compare_categories (gconstpointer a,
372 const struct search_result *ra = a;
373 const struct search_result *rb = b;
375 return ra->category - rb->category;
379 add_token_result (const gchar *app_name,
382 if G_UNLIKELY (static_token_results_size == static_token_results_allocated)
384 static_token_results_allocated = MAX (16, static_token_results_allocated * 2);
385 static_token_results = g_renew (struct search_result, static_token_results, static_token_results_allocated);
388 static_token_results[static_token_results_size].app_name = app_name;
389 static_token_results[static_token_results_size].category = category;
390 static_token_results_size++;
394 merge_token_results (gboolean first)
396 qsort (static_token_results, static_token_results_size, sizeof (struct search_result), compare_results);
398 /* If this is the first token then we are basically merging a list with
399 * itself -- we only perform de-duplication.
401 * If this is not the first token then we are doing a real merge.
405 const gchar *last_name = NULL;
408 /* We must de-duplicate, but we do so by taking the best category
411 * The final list can be as large as the input here, so make sure
412 * we have enough room (even if it's too much room).
415 if G_UNLIKELY (static_search_results_allocated < static_token_results_size)
417 static_search_results_allocated = static_token_results_allocated;
418 static_search_results = g_renew (struct search_result,
419 static_search_results,
420 static_search_results_allocated);
423 for (i = 0; i < static_token_results_size; i++)
425 /* The list is sorted so that the best match for a given id
426 * will be at the front, so once we have copied an id, skip
427 * the rest of the entries for the same id.
429 if (static_token_results[i].app_name == last_name)
432 last_name = static_token_results[i].app_name;
434 static_search_results[static_search_results_size++] = static_token_results[i];
439 const gchar *last_name = NULL;
443 /* We only ever remove items from the results list, so no need to
444 * resize to ensure that we have enough room.
446 for (i = 0; i < static_token_results_size; i++)
448 if (static_token_results[i].app_name == last_name)
451 last_name = static_token_results[i].app_name;
453 /* Now we only want to have a result in static_search_results
454 * if we already have it there *and* we have it in
455 * static_token_results as well. The category will be the
458 * Skip past the results in static_search_results that are not
459 * going to be matches.
461 while (k < static_search_results_size &&
462 static_search_results[k].app_name < static_token_results[i].app_name)
465 if (k < static_search_results_size &&
466 static_search_results[k].app_name == static_token_results[i].app_name)
470 * Category should be the worse of the two (ie:
471 * numerically larger).
473 static_search_results[j].app_name = static_search_results[k].app_name;
474 static_search_results[j].category = MAX (static_search_results[k].category,
475 static_token_results[i].category);
480 static_search_results_size = j;
483 /* Clear it out for next time... */
484 static_token_results_size = 0;
488 reset_total_search_results (void)
490 static_total_results_size = 0;
494 sort_total_search_results (void)
496 qsort (static_total_results, static_total_results_size, sizeof (struct search_result), compare_categories);
500 merge_directory_results (void)
502 if G_UNLIKELY (static_total_results_size + static_search_results_size > static_total_results_allocated)
504 static_total_results_allocated = MAX (16, static_total_results_allocated);
505 while (static_total_results_allocated < static_total_results_size + static_search_results_size)
506 static_total_results_allocated *= 2;
507 static_total_results = g_renew (struct search_result, static_total_results, static_total_results_allocated);
510 memcpy (static_total_results + static_total_results_size,
511 static_search_results,
512 static_search_results_size * sizeof (struct search_result));
514 static_total_results_size += static_search_results_size;
516 /* Clear it out for next time... */
517 static_search_results_size = 0;
521 /* Support for unindexed DesktopFileDirs {{{2 */
523 get_apps_from_dir (GHashTable **apps,
527 const char *basename;
530 dir = g_dir_open (dirname, 0, NULL);
535 while ((basename = g_dir_read_name (dir)) != NULL)
539 filename = g_build_filename (dirname, basename, NULL);
541 if (g_str_has_suffix (basename, ".desktop"))
545 app_name = g_strconcat (prefix, basename, NULL);
548 *apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
550 g_hash_table_insert (*apps, app_name, g_strdup (filename));
552 else if (g_file_test (filename, G_FILE_TEST_IS_DIR))
556 subprefix = g_strconcat (prefix, basename, "-", NULL);
557 get_apps_from_dir (apps, filename, subprefix);
568 desktop_file_dir_unindexed_init (DesktopFileDir *dir)
570 get_apps_from_dir (&dir->app_names, dir->path, "");
573 static GDesktopAppInfo *
574 desktop_file_dir_unindexed_get_app (DesktopFileDir *dir,
575 const gchar *desktop_id)
577 const gchar *filename;
579 filename = g_hash_table_lookup (dir->app_names, desktop_id);
584 return g_desktop_app_info_new_from_filename (filename);
588 desktop_file_dir_unindexed_get_all (DesktopFileDir *dir,
595 if (dir->app_names == NULL)
598 g_hash_table_iter_init (&iter, dir->app_names);
599 while (g_hash_table_iter_next (&iter, &app_name, &filename))
601 if (desktop_file_dir_app_name_is_masked (dir, app_name))
604 add_to_table_if_appropriate (apps, app_name, g_desktop_app_info_new_from_filename (filename));
608 typedef struct _MemoryIndexEntry MemoryIndexEntry;
609 typedef GHashTable MemoryIndex;
611 struct _MemoryIndexEntry
613 const gchar *app_name; /* pointer to the hashtable key */
615 MemoryIndexEntry *next;
619 memory_index_entry_free (gpointer data)
621 MemoryIndexEntry *mie = data;
625 MemoryIndexEntry *next = mie->next;
627 g_slice_free (MemoryIndexEntry, mie);
633 memory_index_add_token (MemoryIndex *mi,
636 const gchar *app_name)
638 MemoryIndexEntry *mie, *first;
640 mie = g_slice_new (MemoryIndexEntry);
641 mie->app_name = app_name;
642 mie->match_category = match_category;
644 first = g_hash_table_lookup (mi, token);
648 mie->next = first->next;
654 g_hash_table_insert (mi, g_strdup (token), mie);
659 memory_index_add_string (MemoryIndex *mi,
662 const gchar *app_name)
664 gchar **tokens, **alternates;
667 tokens = g_str_tokenize_and_fold (string, NULL, &alternates);
669 for (i = 0; tokens[i]; i++)
670 memory_index_add_token (mi, tokens[i], match_category, app_name);
672 for (i = 0; alternates[i]; i++)
673 memory_index_add_token (mi, alternates[i], match_category, app_name);
675 g_strfreev (alternates);
680 memory_index_new (void)
682 return g_hash_table_new_full (g_str_hash, g_str_equal, g_free, memory_index_entry_free);
686 desktop_file_dir_unindexed_setup_search (DesktopFileDir *dir)
691 dir->memory_index = memory_index_new ();
693 /* Nothing to search? */
694 if (dir->app_names == NULL)
697 g_hash_table_iter_init (&iter, dir->app_names);
698 while (g_hash_table_iter_next (&iter, &app, &path))
702 if (desktop_file_dir_app_name_is_masked (dir, app))
705 key_file = g_key_file_new ();
707 if (g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, NULL) &&
708 !g_key_file_get_boolean (key_file, "Desktop Entry", "Hidden", NULL))
710 /* Index the interesting keys... */
713 for (i = 0; i < G_N_ELEMENTS (desktop_key_match_category); i++)
718 if (!desktop_key_match_category[i])
721 raw = g_key_file_get_locale_string (key_file, "Desktop Entry", desktop_key_get_name (i), NULL, NULL);
724 if (i == DESKTOP_KEY_Exec && raw != NULL)
726 /* Special handling: only match basename of first field */
730 /* Remove extra arguments, if any */
731 space = raw + strcspn (raw, " \t\n"); /* IFS */
734 /* Skip the pathname, if any */
735 if ((slash = strrchr (raw, '/')))
740 memory_index_add_string (dir->memory_index, value, desktop_key_match_category[i], app);
746 g_key_file_free (key_file);
751 desktop_file_dir_unindexed_search (DesktopFileDir *dir,
752 const gchar *search_token)
757 if (!dir->memory_index)
758 desktop_file_dir_unindexed_setup_search (dir);
760 g_hash_table_iter_init (&iter, dir->memory_index);
761 while (g_hash_table_iter_next (&iter, &key, &value))
763 MemoryIndexEntry *mie = value;
765 if (!g_str_has_prefix (key, search_token))
770 add_token_result (mie->app_name, mie->match_category);
776 /* DesktopFileDir "API" {{{2 */
779 * desktop_file_dir_create:
780 * @array: the #GArray to add a new item to
781 * @data_dir: an XDG_DATA_DIR
783 * Creates a #DesktopFileDir for the corresponding @data_dir, adding it
787 desktop_file_dir_create (GArray *array,
788 const gchar *data_dir)
790 DesktopFileDir dir = { 0, };
792 dir.path = g_build_filename (data_dir, "applications", NULL);
794 g_array_append_val (array, dir);
798 * desktop_file_dir_reset:
799 * @dir: a #DesktopFileDir
801 * Cleans up @dir, releasing most resources that it was using.
804 desktop_file_dir_reset (DesktopFileDir *dir)
808 g_signal_handlers_disconnect_by_func (dir->monitor, desktop_file_dir_changed, dir);
809 g_object_unref (dir->monitor);
815 g_hash_table_unref (dir->app_names);
816 dir->app_names = NULL;
819 if (dir->memory_index)
821 g_hash_table_unref (dir->memory_index);
822 dir->memory_index = NULL;
825 dir->is_setup = FALSE;
829 * desktop_file_dir_init:
830 * @dir: a #DesktopFileDir
832 * Does initial setup for @dir
834 * You should only call this if @dir is not already setup.
837 desktop_file_dir_init (DesktopFileDir *dir)
839 g_assert (!dir->is_setup);
841 g_assert (!dir->monitor);
842 dir->monitor = g_local_directory_monitor_new_in_worker (dir->path, G_FILE_MONITOR_NONE, NULL);
846 g_signal_connect (dir->monitor, "changed", G_CALLBACK (desktop_file_dir_changed), dir);
847 g_local_directory_monitor_start (dir->monitor);
850 desktop_file_dir_unindexed_init (dir);
852 dir->is_setup = TRUE;
856 * desktop_file_dir_get_app:
857 * @dir: a DesktopFileDir
858 * @desktop_id: the desktop ID to load
860 * Creates the #GDesktopAppInfo for the given @desktop_id if it exists
861 * within @dir, even if it is hidden.
863 * This function does not check if @desktop_id would be masked by a
864 * directory with higher precedence. The caller must do so.
866 static GDesktopAppInfo *
867 desktop_file_dir_get_app (DesktopFileDir *dir,
868 const gchar *desktop_id)
873 return desktop_file_dir_unindexed_get_app (dir, desktop_id);
877 * desktop_file_dir_get_all:
878 * @dir: a DesktopFileDir
879 * @apps: a #GHashTable<string, GDesktopAppInfo>
881 * Loads all desktop files in @dir and adds them to @apps, careful to
882 * ensure we don't add any files masked by a similarly-named file in a
883 * higher-precedence directory.
886 desktop_file_dir_get_all (DesktopFileDir *dir,
889 desktop_file_dir_unindexed_get_all (dir, apps);
893 * desktop_file_dir_search:
894 * @dir: a #DesktopFilEDir
895 * @term: a normalised and casefolded search term
897 * Finds the names of applications in @dir that match @term.
900 desktop_file_dir_search (DesktopFileDir *dir,
901 const gchar *search_token)
903 desktop_file_dir_unindexed_search (dir, search_token);
906 /* Lock/unlock and global setup API {{{2 */
909 desktop_file_dirs_lock (void)
913 g_mutex_lock (&desktop_file_dir_lock);
915 if (desktop_file_dirs == NULL)
917 const char * const *data_dirs;
921 tmp = g_array_new (FALSE, FALSE, sizeof (DesktopFileDir));
923 /* Highest priority: the user's ~/.local/share/applications */
924 desktop_file_dir_create (tmp, g_get_user_data_dir ());
926 /* Following that, XDG_DATA_DIRS/applications, in order */
927 data_dirs = g_get_system_data_dirs ();
928 for (i = 0; data_dirs[i]; i++)
929 desktop_file_dir_create (tmp, data_dirs[i]);
931 desktop_file_dirs = (DesktopFileDir *) tmp->data;
932 n_desktop_file_dirs = tmp->len;
934 g_array_free (tmp, FALSE);
937 for (i = 0; i < n_desktop_file_dirs; i++)
938 if (!desktop_file_dirs[i].is_setup)
939 desktop_file_dir_init (&desktop_file_dirs[i]);
943 desktop_file_dirs_unlock (void)
945 g_mutex_unlock (&desktop_file_dir_lock);
949 desktop_file_dirs_refresh (void)
951 desktop_file_dirs_lock ();
952 desktop_file_dirs_unlock ();
956 desktop_file_dirs_invalidate_user (void)
958 g_mutex_lock (&desktop_file_dir_lock);
960 if (n_desktop_file_dirs)
961 desktop_file_dir_reset (&desktop_file_dirs[0]);
963 g_mutex_unlock (&desktop_file_dir_lock);
966 /* GDesktopAppInfo implementation {{{1 */
967 /* GObject implementation {{{2 */
969 g_desktop_app_info_finalize (GObject *object)
971 GDesktopAppInfo *info;
973 info = G_DESKTOP_APP_INFO (object);
975 g_free (info->desktop_id);
976 g_free (info->filename);
979 g_key_file_unref (info->keyfile);
982 g_free (info->generic_name);
983 g_free (info->fullname);
984 g_free (info->comment);
985 g_free (info->icon_name);
987 g_object_unref (info->icon);
988 g_strfreev (info->keywords);
989 g_strfreev (info->only_show_in);
990 g_strfreev (info->not_show_in);
991 g_free (info->try_exec);
993 g_free (info->binary);
995 g_free (info->categories);
996 g_free (info->startup_wm_class);
997 g_strfreev (info->mime_types);
998 g_free (info->app_id);
999 g_strfreev (info->actions);
1001 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
1005 g_desktop_app_info_set_property (GObject *object,
1007 const GValue *value,
1010 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
1015 self->filename = g_value_dup_string (value);
1019 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1025 g_desktop_app_info_get_property (GObject *object,
1030 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
1035 g_value_set_string (value, self->filename);
1038 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1044 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
1046 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1048 gobject_class->get_property = g_desktop_app_info_get_property;
1049 gobject_class->set_property = g_desktop_app_info_set_property;
1050 gobject_class->finalize = g_desktop_app_info_finalize;
1053 * GDesktopAppInfo:filename:
1055 * The origin filename of this #GDesktopAppInfo
1057 g_object_class_install_property (gobject_class,
1059 g_param_spec_string ("filename", "Filename", "", NULL,
1060 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
1064 g_desktop_app_info_init (GDesktopAppInfo *local)
1068 /* Construction... {{{2 */
1072 * @exec: an exec line
1074 * Returns the first word in an exec line (ie: the binary name).
1076 * If @exec is " progname --foo %F" then returns "progname".
1079 binary_from_exec (const char *exec)
1081 const char *p, *start;
1087 while (*p != ' ' && *p != 0)
1090 return g_strndup (start, p - start);
1094 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
1101 gboolean bus_activatable;
1103 start_group = g_key_file_get_start_group (key_file);
1104 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
1106 g_free (start_group);
1109 g_free (start_group);
1111 type = g_key_file_get_string (key_file,
1112 G_KEY_FILE_DESKTOP_GROUP,
1113 G_KEY_FILE_DESKTOP_KEY_TYPE,
1115 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
1122 try_exec = g_key_file_get_string (key_file,
1123 G_KEY_FILE_DESKTOP_GROUP,
1124 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
1126 if (try_exec && try_exec[0] != '\0')
1129 t = g_find_program_in_path (try_exec);
1138 exec = g_key_file_get_string (key_file,
1139 G_KEY_FILE_DESKTOP_GROUP,
1140 G_KEY_FILE_DESKTOP_KEY_EXEC,
1142 if (exec && exec[0] != '\0')
1146 if (!g_shell_parse_argv (exec, &argc, &argv, NULL))
1155 t = g_find_program_in_path (argv[0]);
1168 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
1169 info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
1170 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
1171 info->keywords = g_key_file_get_locale_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, KEYWORDS_KEY, NULL, NULL, NULL);
1172 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
1173 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
1174 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
1175 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);
1176 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);
1177 info->try_exec = try_exec;
1179 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
1180 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
1181 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
1182 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
1183 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
1184 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
1185 info->startup_wm_class = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, STARTUP_WM_CLASS_KEY, NULL);
1186 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);
1187 bus_activatable = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE, NULL);
1188 info->actions = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ACTIONS, NULL, NULL);
1190 /* Remove the special-case: no Actions= key just means 0 extra actions */
1191 if (info->actions == NULL)
1192 info->actions = g_new0 (gchar *, 0 + 1);
1195 if (info->icon_name)
1197 if (g_path_is_absolute (info->icon_name))
1201 file = g_file_new_for_path (info->icon_name);
1202 info->icon = g_file_icon_new (file);
1203 g_object_unref (file);
1209 /* Work around a common mistake in desktop files */
1210 if ((p = strrchr (info->icon_name, '.')) != NULL &&
1211 (strcmp (p, ".png") == 0 ||
1212 strcmp (p, ".xpm") == 0 ||
1213 strcmp (p, ".svg") == 0))
1216 info->icon = g_themed_icon_new (info->icon_name);
1221 info->binary = binary_from_exec (info->exec);
1223 if (info->path && info->path[0] == '\0')
1225 g_free (info->path);
1229 /* Can only be DBusActivatable if we know the filename, which means
1230 * that this won't work for the load-from-keyfile case.
1232 if (bus_activatable && info->filename)
1237 basename = g_path_get_basename (info->filename);
1238 last_dot = strrchr (basename, '.');
1240 if (last_dot && g_str_equal (last_dot, ".desktop"))
1244 if (g_dbus_is_interface_name (basename))
1245 info->app_id = g_strdup (basename);
1251 info->keyfile = g_key_file_ref (key_file);
1257 g_desktop_app_info_load_file (GDesktopAppInfo *self)
1260 gboolean retval = FALSE;
1262 g_return_val_if_fail (self->filename != NULL, FALSE);
1264 self->desktop_id = g_path_get_basename (self->filename);
1266 key_file = g_key_file_new ();
1268 if (g_key_file_load_from_file (key_file, self->filename, G_KEY_FILE_NONE, NULL))
1269 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
1271 g_key_file_unref (key_file);
1276 * g_desktop_app_info_new_from_keyfile:
1277 * @key_file: an opened #GKeyFile
1279 * Creates a new #GDesktopAppInfo.
1281 * Returns: a new #GDesktopAppInfo or %NULL on error.
1286 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
1288 GDesktopAppInfo *info;
1290 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1291 info->filename = NULL;
1292 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
1294 g_object_unref (info);
1301 * g_desktop_app_info_new_from_filename:
1302 * @filename: the path of a desktop file, in the GLib filename encoding
1304 * Creates a new #GDesktopAppInfo.
1306 * Returns: a new #GDesktopAppInfo or %NULL on error.
1309 g_desktop_app_info_new_from_filename (const char *filename)
1311 GDesktopAppInfo *info = NULL;
1313 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
1314 if (!g_desktop_app_info_load_file (info))
1316 g_object_unref (info);
1323 * g_desktop_app_info_new:
1324 * @desktop_id: the desktop file id
1326 * Creates a new #GDesktopAppInfo based on a desktop file id.
1328 * A desktop file id is the basename of the desktop file, including the
1329 * .desktop extension. GIO is looking for a desktop file with this name
1330 * in the `applications` subdirectories of the XDG
1331 * data directories (i.e. the directories specified in the `XDG_DATA_HOME`
1332 * and `XDG_DATA_DIRS` environment variables). GIO also supports the
1333 * prefix-to-subdirectory mapping that is described in the
1334 * [Menu Spec](http://standards.freedesktop.org/menu-spec/latest/)
1335 * (i.e. a desktop id of kde-foo.desktop will match
1336 * `/usr/share/applications/kde/foo.desktop`).
1338 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
1341 g_desktop_app_info_new (const char *desktop_id)
1343 GDesktopAppInfo *appinfo = NULL;
1346 desktop_file_dirs_lock ();
1348 for (i = 0; i < n_desktop_file_dirs; i++)
1350 appinfo = desktop_file_dir_get_app (&desktop_file_dirs[i], desktop_id);
1356 desktop_file_dirs_unlock ();
1358 if (appinfo == NULL)
1361 g_free (appinfo->desktop_id);
1362 appinfo->desktop_id = g_strdup (desktop_id);
1364 if (g_desktop_app_info_get_is_hidden (appinfo))
1366 g_object_unref (appinfo);
1374 g_desktop_app_info_dup (GAppInfo *appinfo)
1376 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1377 GDesktopAppInfo *new_info;
1379 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1381 new_info->filename = g_strdup (info->filename);
1382 new_info->desktop_id = g_strdup (info->desktop_id);
1385 new_info->keyfile = g_key_file_ref (info->keyfile);
1387 new_info->name = g_strdup (info->name);
1388 new_info->generic_name = g_strdup (info->generic_name);
1389 new_info->fullname = g_strdup (info->fullname);
1390 new_info->keywords = g_strdupv (info->keywords);
1391 new_info->comment = g_strdup (info->comment);
1392 new_info->nodisplay = info->nodisplay;
1393 new_info->icon_name = g_strdup (info->icon_name);
1395 new_info->icon = g_object_ref (info->icon);
1396 new_info->only_show_in = g_strdupv (info->only_show_in);
1397 new_info->not_show_in = g_strdupv (info->not_show_in);
1398 new_info->try_exec = g_strdup (info->try_exec);
1399 new_info->exec = g_strdup (info->exec);
1400 new_info->binary = g_strdup (info->binary);
1401 new_info->path = g_strdup (info->path);
1402 new_info->app_id = g_strdup (info->app_id);
1403 new_info->hidden = info->hidden;
1404 new_info->terminal = info->terminal;
1405 new_info->startup_notify = info->startup_notify;
1407 return G_APP_INFO (new_info);
1410 /* GAppInfo interface implementation functions {{{2 */
1413 g_desktop_app_info_equal (GAppInfo *appinfo1,
1416 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
1417 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
1419 if (info1->desktop_id == NULL ||
1420 info2->desktop_id == NULL)
1421 return info1 == info2;
1423 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
1427 g_desktop_app_info_get_id (GAppInfo *appinfo)
1429 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1431 return info->desktop_id;
1435 g_desktop_app_info_get_name (GAppInfo *appinfo)
1437 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1439 if (info->name == NULL)
1440 return _("Unnamed");
1445 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
1447 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1449 if (info->fullname == NULL)
1450 return g_desktop_app_info_get_name (appinfo);
1451 return info->fullname;
1455 * g_desktop_app_info_get_is_hidden:
1456 * @info: a #GDesktopAppInfo.
1458 * A desktop file is hidden if the Hidden key in it is
1461 * Returns: %TRUE if hidden, %FALSE otherwise.
1464 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
1466 return info->hidden;
1470 * g_desktop_app_info_get_filename:
1471 * @info: a #GDesktopAppInfo
1473 * When @info was created from a known filename, return it. In some
1474 * situations such as the #GDesktopAppInfo returned from
1475 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
1477 * Returns: The full path to the file for @info, or %NULL if not known.
1481 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
1483 return info->filename;
1487 g_desktop_app_info_get_description (GAppInfo *appinfo)
1489 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1491 return info->comment;
1495 g_desktop_app_info_get_executable (GAppInfo *appinfo)
1497 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1499 return info->binary;
1503 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
1505 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1511 g_desktop_app_info_get_icon (GAppInfo *appinfo)
1513 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1519 * g_desktop_app_info_get_categories:
1520 * @info: a #GDesktopAppInfo
1522 * Gets the categories from the desktop file.
1524 * Returns: The unparsed Categories key from the desktop file;
1525 * i.e. no attempt is made to split it by ';' or validate it.
1528 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
1530 return info->categories;
1534 * g_desktop_app_info_get_keywords:
1535 * @info: a #GDesktopAppInfo
1537 * Gets the keywords from the desktop file.
1539 * Returns: (transfer none): The value of the Keywords key
1543 const char * const *
1544 g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
1546 return (const char * const *)info->keywords;
1550 * g_desktop_app_info_get_generic_name:
1551 * @info: a #GDesktopAppInfo
1553 * Gets the generic name from the destkop file.
1555 * Returns: The value of the GenericName key
1558 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
1560 return info->generic_name;
1564 * g_desktop_app_info_get_nodisplay:
1565 * @info: a #GDesktopAppInfo
1567 * Gets the value of the NoDisplay key, which helps determine if the
1568 * application info should be shown in menus. See
1569 * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
1571 * Returns: The value of the NoDisplay key
1576 g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
1578 return info->nodisplay;
1582 * g_desktop_app_info_get_show_in:
1583 * @info: a #GDesktopAppInfo
1584 * @desktop_env: a string specifying a desktop name
1586 * Checks if the application info should be shown in menus that list available
1587 * applications for a specific name of the desktop, based on the
1588 * `OnlyShowIn` and `NotShowIn` keys.
1590 * If @desktop_env is %NULL, then the name of the desktop set with
1591 * g_desktop_app_info_set_desktop_env() is used.
1593 * Note that g_app_info_should_show() for @info will include this check (with
1594 * %NULL for @desktop_env) as well as additional checks.
1596 * Returns: %TRUE if the @info should be shown in @desktop_env according to the
1597 * `OnlyShowIn` and `NotShowIn` keys, %FALSE
1603 g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
1604 const gchar *desktop_env)
1609 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
1612 G_LOCK (g_desktop_env);
1613 desktop_env = g_desktop_env;
1614 G_UNLOCK (g_desktop_env);
1617 if (info->only_show_in)
1619 if (desktop_env == NULL)
1623 for (i = 0; info->only_show_in[i] != NULL; i++)
1625 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1635 if (info->not_show_in && desktop_env)
1637 for (i = 0; info->not_show_in[i] != NULL; i++)
1639 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1647 /* Launching... {{{2 */
1650 expand_macro_single (char macro, char *uri)
1653 char *result = NULL;
1657 file = g_file_new_for_uri (uri);
1663 result = g_shell_quote (uri);
1667 path = g_file_get_path (file);
1669 result = g_shell_quote (path);
1673 path = g_file_get_path (file);
1676 name = g_path_get_dirname (path);
1677 result = g_shell_quote (name);
1683 path = g_file_get_path (file);
1686 name = g_path_get_basename (path);
1687 result = g_shell_quote (name);
1693 g_object_unref (file);
1700 expand_macro (char macro,
1702 GDesktopAppInfo *info,
1705 GList *uris = *uri_list;
1707 gboolean force_file_uri;
1708 char force_file_uri_macro;
1711 g_return_if_fail (exec != NULL);
1713 /* On %u and %U, pass POSIX file path pointing to the URI via
1714 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
1715 * running or the URI doesn't have a POSIX file path via FUSE
1716 * we'll just pass the URI.
1718 force_file_uri_macro = macro;
1719 force_file_uri = FALSE;
1725 force_file_uri_macro = 'f';
1726 force_file_uri = TRUE;
1729 force_file_uri_macro = 'F';
1730 force_file_uri = TRUE;
1746 if (!force_file_uri ||
1747 /* Pass URI if it contains an anchor */
1748 strchr (uri, '#') != NULL)
1750 expanded = expand_macro_single (macro, uri);
1754 expanded = expand_macro_single (force_file_uri_macro, uri);
1755 if (expanded == NULL)
1756 expanded = expand_macro_single (macro, uri);
1761 g_string_append (exec, expanded);
1777 if (!force_file_uri ||
1778 /* Pass URI if it contains an anchor */
1779 strchr (uri, '#') != NULL)
1781 expanded = expand_macro_single (macro, uri);
1785 expanded = expand_macro_single (force_file_uri_macro, uri);
1786 if (expanded == NULL)
1787 expanded = expand_macro_single (macro, uri);
1792 g_string_append (exec, expanded);
1798 if (uris != NULL && expanded)
1799 g_string_append_c (exec, ' ');
1805 if (info->icon_name)
1807 g_string_append (exec, "--icon ");
1808 expanded = g_shell_quote (info->icon_name);
1809 g_string_append (exec, expanded);
1817 expanded = g_shell_quote (info->name);
1818 g_string_append (exec, expanded);
1826 expanded = g_shell_quote (info->filename);
1827 g_string_append (exec, expanded);
1832 case 'm': /* deprecated */
1836 g_string_append_c (exec, '%');
1844 expand_application_parameters (GDesktopAppInfo *info,
1845 const gchar *exec_line,
1851 GList *uri_list = *uris;
1852 const char *p = exec_line;
1853 GString *expanded_exec;
1856 if (exec_line == NULL)
1858 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1859 _("Desktop file didn't specify Exec field"));
1863 expanded_exec = g_string_new (NULL);
1867 if (p[0] == '%' && p[1] != '\0')
1869 expand_macro (p[1], expanded_exec, info, uris);
1873 g_string_append_c (expanded_exec, *p);
1878 /* No file substitutions */
1879 if (uri_list == *uris && uri_list != NULL)
1881 /* If there is no macro default to %f. This is also what KDE does */
1882 g_string_append_c (expanded_exec, ' ');
1883 expand_macro ('f', expanded_exec, info, uris);
1886 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
1887 g_string_free (expanded_exec, TRUE);
1892 prepend_terminal_to_vector (int *argc,
1899 char **term_argv = NULL;
1904 g_return_val_if_fail (argc != NULL, FALSE);
1905 g_return_val_if_fail (argv != NULL, FALSE);
1913 /* compute size if not given */
1916 for (i = 0; the_argv[i] != NULL; i++)
1922 term_argv = g_new0 (char *, 3);
1924 check = g_find_program_in_path ("gnome-terminal");
1927 term_argv[0] = check;
1928 /* Note that gnome-terminal takes -x and
1929 * as -e in gnome-terminal is broken we use that. */
1930 term_argv[1] = g_strdup ("-x");
1935 check = g_find_program_in_path ("nxterm");
1937 check = g_find_program_in_path ("color-xterm");
1939 check = g_find_program_in_path ("rxvt");
1941 check = g_find_program_in_path ("xterm");
1943 check = g_find_program_in_path ("dtterm");
1946 check = g_strdup ("xterm");
1947 g_warning ("couldn't find a terminal, falling back to xterm");
1949 term_argv[0] = check;
1950 term_argv[1] = g_strdup ("-e");
1953 real_argc = term_argc + *argc;
1954 real_argv = g_new (char *, real_argc + 1);
1956 for (i = 0; i < term_argc; i++)
1957 real_argv[i] = term_argv[i];
1959 for (j = 0; j < *argc; j++, i++)
1960 real_argv[i] = (char *)the_argv[j];
1962 real_argv[i] = NULL;
1968 /* we use g_free here as we sucked all the inner strings
1969 * out from it into real_argv */
1974 #endif /* G_OS_WIN32 */
1978 create_files_for_uris (GList *uris)
1985 for (iter = uris; iter; iter = iter->next)
1987 GFile *file = g_file_new_for_uri ((char *)iter->data);
1988 res = g_list_prepend (res, file);
1991 return g_list_reverse (res);
1996 GSpawnChildSetupFunc user_setup;
1997 gpointer user_setup_data;
2003 child_setup (gpointer user_data)
2005 ChildSetupData *data = user_data;
2007 if (data->pid_envvar)
2009 pid_t pid = getpid ();
2013 /* Write the pid into the space already reserved for it in the
2014 * environment array. We can't use sprintf because it might
2015 * malloc, so we do it by hand. It's simplest to write the pid
2016 * out backwards first, then copy it over.
2018 for (i = 0; pid; i++, pid /= 10)
2019 buf[i] = (pid % 10) + '0';
2020 for (i--; i >= 0; i--)
2021 *(data->pid_envvar++) = buf[i];
2022 *data->pid_envvar = '\0';
2025 if (data->user_setup)
2026 data->user_setup (data->user_setup_data);
2030 notify_desktop_launch (GDBusConnection *session_bus,
2031 GDesktopAppInfo *info,
2033 const char *display,
2038 GVariantBuilder uri_variant;
2039 GVariantBuilder extras_variant;
2041 const char *desktop_file_id;
2042 const char *gio_desktop_file;
2044 if (session_bus == NULL)
2047 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
2048 for (iter = uris; iter; iter = iter->next)
2049 g_variant_builder_add (&uri_variant, "s", iter->data);
2051 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
2052 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
2053 g_variant_builder_add (&extras_variant, "{sv}",
2057 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
2058 if (gio_desktop_file != NULL)
2059 g_variant_builder_add (&extras_variant, "{sv}",
2060 "origin-desktop-file",
2061 g_variant_new_bytestring (gio_desktop_file));
2062 if (g_get_prgname () != NULL)
2063 g_variant_builder_add (&extras_variant, "{sv}",
2065 g_variant_new_bytestring (g_get_prgname ()));
2066 g_variant_builder_add (&extras_variant, "{sv}",
2069 (gint64)getpid ()));
2072 desktop_file_id = info->filename;
2073 else if (info->desktop_id)
2074 desktop_file_id = info->desktop_id;
2076 desktop_file_id = "";
2078 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
2079 "org.gtk.gio.DesktopAppInfo",
2081 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
2082 g_variant_new_bytestring (desktop_file_id),
2083 display ? display : "",
2087 g_dbus_connection_send_message (session_bus,
2091 g_object_unref (msg);
2094 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
2097 g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo *info,
2098 GDBusConnection *session_bus,
2099 const gchar *exec_line,
2101 GAppLaunchContext *launch_context,
2102 GSpawnFlags spawn_flags,
2103 GSpawnChildSetupFunc user_setup,
2104 gpointer user_setup_data,
2105 GDesktopAppLaunchCallback pid_callback,
2106 gpointer pid_callback_data,
2109 gboolean completed = FALSE;
2111 char **argv, **envp;
2113 ChildSetupData data;
2115 g_return_val_if_fail (info != NULL, FALSE);
2120 envp = g_app_launch_context_get_environment (launch_context);
2122 envp = g_get_environ ();
2127 GList *launched_uris;
2129 char *display, *sn_id = NULL;
2132 if (!expand_application_parameters (info, exec_line, &uris, &argc, &argv, error))
2135 /* Get the subset of URIs we're launching with this process */
2136 launched_uris = NULL;
2137 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
2138 launched_uris = g_list_prepend (launched_uris, iter->data);
2139 launched_uris = g_list_reverse (launched_uris);
2141 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
2143 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2144 _("Unable to find terminal required for application"));
2148 data.user_setup = user_setup;
2149 data.user_setup_data = user_setup_data;
2153 envp = g_environ_setenv (envp,
2154 "GIO_LAUNCHED_DESKTOP_FILE",
2157 envp = g_environ_setenv (envp,
2158 "GIO_LAUNCHED_DESKTOP_FILE_PID",
2159 "XXXXXXXXXXXXXXXXXXXX", /* filled in child_setup */
2161 data.pid_envvar = (char *)g_environ_getenv (envp, "GIO_LAUNCHED_DESKTOP_FILE_PID");
2165 data.pid_envvar = NULL;
2172 GList *launched_files = create_files_for_uris (launched_uris);
2174 display = g_app_launch_context_get_display (launch_context,
2178 envp = g_environ_setenv (envp, "DISPLAY", display, TRUE);
2180 if (info->startup_notify)
2182 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
2187 envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
2192 g_list_free_full (launched_files, g_object_unref);
2195 if (!g_spawn_async (info->path,
2205 g_app_launch_context_launch_failed (launch_context, sn_id);
2209 g_list_free (launched_uris);
2214 if (pid_callback != NULL)
2215 pid_callback (info, pid, pid_callback_data);
2217 if (launch_context != NULL)
2219 GVariantBuilder builder;
2220 GVariant *platform_data;
2222 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
2223 g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (pid));
2225 g_variant_builder_add (&builder, "{sv}", "startup-notification-id", g_variant_new_string (sn_id));
2226 platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
2227 g_signal_emit_by_name (launch_context, "launched", info, platform_data);
2228 g_variant_unref (platform_data);
2231 notify_desktop_launch (session_bus,
2240 g_list_free (launched_uris);
2245 while (uris != NULL);
2257 object_path_from_appid (const gchar *app_id)
2262 n = strlen (app_id);
2263 path = g_malloc (n + 2);
2267 for (i = 0; i < n; i++)
2268 if (app_id[i] != '.')
2269 path[i + 1] = app_id[i];
2279 g_desktop_app_info_make_platform_data (GDesktopAppInfo *info,
2281 GAppLaunchContext *launch_context)
2283 GVariantBuilder builder;
2285 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
2289 GList *launched_files = create_files_for_uris (uris);
2291 if (info->startup_notify)
2295 sn_id = g_app_launch_context_get_startup_notify_id (launch_context, G_APP_INFO (info), launched_files);
2297 g_variant_builder_add (&builder, "{sv}", "desktop-startup-id", g_variant_new_take_string (sn_id));
2300 g_list_free_full (launched_files, g_object_unref);
2303 return g_variant_builder_end (&builder);
2307 g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo *info,
2308 GDBusConnection *session_bus,
2310 GAppLaunchContext *launch_context)
2312 GVariantBuilder builder;
2315 g_return_val_if_fail (info != NULL, FALSE);
2317 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
2323 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
2324 for (iter = uris; iter; iter = iter->next)
2325 g_variant_builder_add (&builder, "s", iter->data);
2326 g_variant_builder_close (&builder);
2329 g_variant_builder_add_value (&builder, g_desktop_app_info_make_platform_data (info, uris, launch_context));
2331 /* This is non-blocking API. Similar to launching via fork()/exec()
2332 * we don't wait around to see if the program crashed during startup.
2333 * This is what startup-notification's job is...
2335 object_path = object_path_from_appid (info->app_id);
2336 g_dbus_connection_call (session_bus, info->app_id, object_path, "org.freedesktop.Application",
2337 uris ? "Open" : "Activate", g_variant_builder_end (&builder),
2338 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
2339 g_free (object_path);
2345 g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
2347 GAppLaunchContext *launch_context,
2348 GSpawnFlags spawn_flags,
2349 GSpawnChildSetupFunc user_setup,
2350 gpointer user_setup_data,
2351 GDesktopAppLaunchCallback pid_callback,
2352 gpointer pid_callback_data,
2355 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2356 GDBusConnection *session_bus;
2357 gboolean success = TRUE;
2359 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
2361 if (session_bus && info->app_id)
2362 g_desktop_app_info_launch_uris_with_dbus (info, session_bus, uris, launch_context);
2364 success = g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec, uris, launch_context,
2365 spawn_flags, user_setup, user_setup_data,
2366 pid_callback, pid_callback_data, error);
2368 if (session_bus != NULL)
2370 /* This asynchronous flush holds a reference until it completes,
2371 * which ensures that the following unref won't immediately kill
2372 * the connection if we were the initial owner.
2374 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
2375 g_object_unref (session_bus);
2382 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
2384 GAppLaunchContext *launch_context,
2387 return g_desktop_app_info_launch_uris_internal (appinfo, uris,
2389 _SPAWN_FLAGS_DEFAULT,
2390 NULL, NULL, NULL, NULL,
2395 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
2397 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2399 return info->exec &&
2400 ((strstr (info->exec, "%u") != NULL) ||
2401 (strstr (info->exec, "%U") != NULL));
2405 g_desktop_app_info_supports_files (GAppInfo *appinfo)
2407 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2409 return info->exec &&
2410 ((strstr (info->exec, "%f") != NULL) ||
2411 (strstr (info->exec, "%F") != NULL));
2415 g_desktop_app_info_launch (GAppInfo *appinfo,
2417 GAppLaunchContext *launch_context,
2427 uri = g_file_get_uri (files->data);
2428 uris = g_list_prepend (uris, uri);
2429 files = files->next;
2432 uris = g_list_reverse (uris);
2434 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
2436 g_list_free_full (uris, g_free);
2442 * g_desktop_app_info_launch_uris_as_manager:
2443 * @appinfo: a #GDesktopAppInfo
2444 * @uris: (element-type utf8): List of URIs
2445 * @launch_context: (allow-none): a #GAppLaunchContext
2446 * @spawn_flags: #GSpawnFlags, used for each process
2447 * @user_setup: (scope call) (allow-none): a #GSpawnChildSetupFunc, used once
2449 * @user_setup_data: (closure user_setup) (allow-none): User data for @user_setup
2450 * @pid_callback: (scope call) (allow-none): Callback for child processes
2451 * @pid_callback_data: (closure pid_callback) (allow-none): User data for @callback
2452 * @error: return location for a #GError, or %NULL
2454 * This function performs the equivalent of g_app_info_launch_uris(),
2455 * but is intended primarily for operating system components that
2456 * launch applications. Ordinary applications should use
2457 * g_app_info_launch_uris().
2459 * If the application is launched via traditional UNIX fork()/exec()
2460 * then @spawn_flags, @user_setup and @user_setup_data are used for the
2461 * call to g_spawn_async(). Additionally, @pid_callback (with
2462 * @pid_callback_data) will be called to inform about the PID of the
2465 * If application launching occurs via some other mechanism (eg: D-Bus
2466 * activation) then @spawn_flags, @user_setup, @user_setup_data,
2467 * @pid_callback and @pid_callback_data are ignored.
2469 * Returns: %TRUE on successful launch, %FALSE otherwise.
2472 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
2474 GAppLaunchContext *launch_context,
2475 GSpawnFlags spawn_flags,
2476 GSpawnChildSetupFunc user_setup,
2477 gpointer user_setup_data,
2478 GDesktopAppLaunchCallback pid_callback,
2479 gpointer pid_callback_data,
2482 return g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
2493 /* OnlyShowIn API support {{{2 */
2496 * g_desktop_app_info_set_desktop_env:
2497 * @desktop_env: a string specifying what desktop this is
2499 * Sets the name of the desktop that the application is running in.
2500 * This is used by g_app_info_should_show() and
2501 * g_desktop_app_info_get_show_in() to evaluate the
2502 * `OnlyShowIn` and `NotShowIn`
2503 * desktop entry fields.
2506 * [Desktop Menu specification](http://standards.freedesktop.org/menu-spec/latest/)
2507 * recognizes the following:
2516 * Should be called only once; subsequent calls are ignored.
2519 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
2521 G_LOCK (g_desktop_env);
2523 g_desktop_env = g_strdup (desktop_env);
2524 G_UNLOCK (g_desktop_env);
2528 g_desktop_app_info_should_show (GAppInfo *appinfo)
2530 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2532 if (info->nodisplay)
2535 return g_desktop_app_info_get_show_in (info, NULL);
2538 /* mime types/default apps support {{{2 */
2546 ensure_dir (DirType type,
2549 char *path, *display_name;
2552 if (type == APP_DIR)
2553 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
2555 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
2558 if (g_mkdir_with_parents (path, 0700) == 0)
2562 display_name = g_filename_display_name (path);
2563 if (type == APP_DIR)
2564 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
2565 _("Can't create user application configuration folder %s: %s"),
2566 display_name, g_strerror (errsv));
2568 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
2569 _("Can't create user MIME configuration folder %s: %s"),
2570 display_name, g_strerror (errsv));
2572 g_free (display_name);
2579 update_mimeapps_list (const char *desktop_id,
2580 const char *content_type,
2581 UpdateMimeFlags flags,
2584 char *dirname, *filename, *string;
2586 gboolean load_succeeded, res;
2587 char **old_list, **list;
2588 gsize length, data_size;
2591 char **content_types;
2593 /* Don't add both at start and end */
2594 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
2595 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
2597 dirname = ensure_dir (APP_DIR, error);
2601 filename = g_build_filename (dirname, "mimeapps.list", NULL);
2604 key_file = g_key_file_new ();
2605 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
2606 if (!load_succeeded ||
2607 (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) &&
2608 !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) &&
2609 !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP)))
2611 g_key_file_free (key_file);
2612 key_file = g_key_file_new ();
2617 content_types = g_new (char *, 2);
2618 content_types[0] = g_strdup (content_type);
2619 content_types[1] = NULL;
2623 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
2626 for (k = 0; content_types && content_types[k]; k++)
2628 /* set as default, if requested so */
2629 string = g_key_file_get_string (key_file,
2630 DEFAULT_APPLICATIONS_GROUP,
2634 if (g_strcmp0 (string, desktop_id) != 0 &&
2635 (flags & UPDATE_MIME_SET_DEFAULT))
2638 string = g_strdup (desktop_id);
2640 /* add in the non-default list too, if it's not already there */
2641 flags |= UPDATE_MIME_SET_NON_DEFAULT;
2644 if (string == NULL || desktop_id == NULL)
2645 g_key_file_remove_key (key_file,
2646 DEFAULT_APPLICATIONS_GROUP,
2650 g_key_file_set_string (key_file,
2651 DEFAULT_APPLICATIONS_GROUP,
2660 /* reuse the list from above */
2664 g_strfreev (content_types);
2665 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
2668 for (k = 0; content_types && content_types[k]; k++)
2670 /* Add to the right place in the list */
2673 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
2674 content_types[k], &length, NULL);
2676 list = g_new (char *, 1 + length + 1);
2680 /* if we're adding a last-used hint, just put the application in front of the list */
2681 if (flags & UPDATE_MIME_SET_LAST_USED)
2683 /* avoid adding this again as non-default later */
2684 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
2685 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
2687 list[i++] = g_strdup (desktop_id);
2692 for (j = 0; old_list[j] != NULL; j++)
2694 if (g_strcmp0 (old_list[j], desktop_id) != 0)
2696 /* rewrite other entries if they're different from the new one */
2697 list[i++] = g_strdup (old_list[j]);
2699 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
2701 /* we encountered an old entry which is equal to the one we're adding as non-default,
2702 * don't change its position in the list.
2704 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
2705 list[i++] = g_strdup (old_list[j]);
2710 /* add it at the end of the list */
2711 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
2712 list[i++] = g_strdup (desktop_id);
2716 g_strfreev (old_list);
2718 if (list[0] == NULL || desktop_id == NULL)
2719 g_key_file_remove_key (key_file,
2720 ADDED_ASSOCIATIONS_GROUP,
2724 g_key_file_set_string_list (key_file,
2725 ADDED_ASSOCIATIONS_GROUP,
2727 (const char * const *)list, i);
2734 /* reuse the list from above */
2738 g_strfreev (content_types);
2739 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
2742 for (k = 0; content_types && content_types[k]; k++)
2744 /* Remove from removed associations group (unless remove) */
2747 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
2748 content_types[k], &length, NULL);
2750 list = g_new (char *, 1 + length + 1);
2753 if (flags & UPDATE_MIME_REMOVE)
2754 list[i++] = g_strdup (desktop_id);
2757 for (j = 0; old_list[j] != NULL; j++)
2759 if (g_strcmp0 (old_list[j], desktop_id) != 0)
2760 list[i++] = g_strdup (old_list[j]);
2765 g_strfreev (old_list);
2767 if (list[0] == NULL || desktop_id == NULL)
2768 g_key_file_remove_key (key_file,
2769 REMOVED_ASSOCIATIONS_GROUP,
2773 g_key_file_set_string_list (key_file,
2774 REMOVED_ASSOCIATIONS_GROUP,
2776 (const char * const *)list, i);
2781 g_strfreev (content_types);
2783 data = g_key_file_to_data (key_file, &data_size, error);
2784 g_key_file_free (key_file);
2786 res = g_file_set_contents (filename, data, data_size, error);
2788 mime_info_cache_reload (NULL);
2797 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
2798 const char *content_type,
2801 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2803 if (!g_desktop_app_info_ensure_saved (info, error))
2806 if (!info->desktop_id)
2808 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2809 _("Application information lacks an identifier"));
2813 /* both add support for the content type and set as last used */
2814 return update_mimeapps_list (info->desktop_id, content_type,
2815 UPDATE_MIME_SET_NON_DEFAULT |
2816 UPDATE_MIME_SET_LAST_USED,
2821 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
2822 const char *content_type,
2825 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2827 if (!g_desktop_app_info_ensure_saved (info, error))
2830 if (!info->desktop_id)
2832 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2833 _("Application information lacks an identifier"));
2837 return update_mimeapps_list (info->desktop_id, content_type,
2838 UPDATE_MIME_SET_DEFAULT,
2843 update_program_done (GPid pid,
2847 /* Did the application exit correctly */
2848 if (g_spawn_check_exit_status (status, NULL))
2850 /* Here we could clean out any caches in use */
2855 run_update_command (char *command,
2864 GError *error = NULL;
2867 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
2869 if (g_spawn_async ("/", argv,
2871 G_SPAWN_SEARCH_PATH |
2872 G_SPAWN_STDOUT_TO_DEV_NULL |
2873 G_SPAWN_STDERR_TO_DEV_NULL |
2874 G_SPAWN_DO_NOT_REAP_CHILD,
2875 NULL, NULL, /* No setup function */
2878 g_child_watch_add (pid, update_program_done, NULL);
2881 /* If we get an error at this point, it's quite likely the user doesn't
2882 * have an installed copy of either 'update-mime-database' or
2883 * 'update-desktop-database'. I don't think we want to popup an error
2884 * dialog at this point, so we just do a g_warning to give the user a
2885 * chance of debugging it.
2887 g_warning ("%s", error->message);
2894 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
2895 const char *extension,
2898 char *filename, *basename, *mimetype;
2902 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
2905 dirname = ensure_dir (MIMETYPE_DIR, error);
2909 basename = g_strdup_printf ("user-extension-%s.xml", extension);
2910 filename = g_build_filename (dirname, basename, NULL);
2914 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
2916 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
2921 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
2922 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
2923 " <mime-type type=\"%s\">\n"
2924 " <comment>%s document</comment>\n"
2925 " <glob pattern=\"*.%s\"/>\n"
2927 "</mime-info>\n", mimetype, extension, extension);
2929 g_file_set_contents (filename, contents, -1, NULL);
2932 run_update_command ("update-mime-database", "mime");
2936 res = g_desktop_app_info_set_as_default_for_type (appinfo,
2946 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
2947 const char *content_type,
2950 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2952 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
2955 return update_mimeapps_list (info->desktop_id, content_type,
2956 UPDATE_MIME_SET_NON_DEFAULT,
2961 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
2967 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
2968 const char *content_type,
2971 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2973 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
2976 return update_mimeapps_list (info->desktop_id, content_type,
2981 static const char **
2982 g_desktop_app_info_get_supported_types (GAppInfo *appinfo)
2984 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2986 return (const char**) info->mime_types;
2989 /* Saving and deleting {{{2 */
2992 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
2998 char *data, *desktop_id;
3003 if (info->filename != NULL)
3006 /* This is only used for object created with
3007 * g_app_info_create_from_commandline. All other
3008 * object should have a filename
3011 dirname = ensure_dir (APP_DIR, error);
3015 key_file = g_key_file_new ();
3017 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3018 "Encoding", "UTF-8");
3019 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3020 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
3021 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3022 G_KEY_FILE_DESKTOP_KEY_TYPE,
3023 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
3025 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3026 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
3027 if (info->nodisplay)
3028 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3029 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
3031 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3032 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
3034 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3035 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
3037 if (info->generic_name != NULL)
3038 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3039 GENERIC_NAME_KEY, info->generic_name);
3041 if (info->fullname != NULL)
3042 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3043 FULL_NAME_KEY, info->fullname);
3045 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3046 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
3048 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3049 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
3051 data = g_key_file_to_data (key_file, &data_size, NULL);
3052 g_key_file_free (key_file);
3054 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
3055 filename = g_build_filename (dirname, desktop_id, NULL);
3056 g_free (desktop_id);
3059 fd = g_mkstemp (filename);
3064 display_name = g_filename_display_name (filename);
3065 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
3066 _("Can't create user desktop file %s"), display_name);
3067 g_free (display_name);
3073 desktop_id = g_path_get_basename (filename);
3075 /* FIXME - actually handle error */
3076 (void) g_close (fd, NULL);
3078 res = g_file_set_contents (filename, data, data_size, error);
3082 g_free (desktop_id);
3087 info->filename = filename;
3088 info->desktop_id = desktop_id;
3090 run_update_command ("update-desktop-database", "applications");
3092 /* We just dropped a file in the user's desktop file directory. Save
3093 * the monitor the bother of having to notice it and invalidate
3096 * This means that calls directly following this will be able to see
3097 * the results immediately.
3099 desktop_file_dirs_invalidate_user ();
3105 g_desktop_app_info_can_delete (GAppInfo *appinfo)
3107 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3111 if (strstr (info->filename, "/userapp-"))
3112 return g_access (info->filename, W_OK) == 0;
3119 g_desktop_app_info_delete (GAppInfo *appinfo)
3121 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3125 if (g_remove (info->filename) == 0)
3127 update_mimeapps_list (info->desktop_id, NULL,
3131 g_free (info->filename);
3132 info->filename = NULL;
3133 g_free (info->desktop_id);
3134 info->desktop_id = NULL;
3143 /* Create for commandline {{{2 */
3145 * g_app_info_create_from_commandline:
3146 * @commandline: the commandline to use
3147 * @application_name: (allow-none): the application name, or %NULL to use @commandline
3148 * @flags: flags that can specify details of the created #GAppInfo
3149 * @error: a #GError location to store the error occurring, %NULL to ignore.
3151 * Creates a new #GAppInfo from the given information.
3153 * Note that for @commandline, the quoting rules of the Exec key of the
3154 * [freedesktop.org Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec)
3155 * are applied. For example, if the @commandline contains
3156 * percent-encoded URIs, the percent-character must be doubled in order to prevent it from
3157 * being swallowed by Exec key unquoting. See the specification for exact quoting rules.
3159 * Returns: (transfer full): new #GAppInfo for given command.
3162 g_app_info_create_from_commandline (const char *commandline,
3163 const char *application_name,
3164 GAppInfoCreateFlags flags,
3169 GDesktopAppInfo *info;
3171 g_return_val_if_fail (commandline, NULL);
3173 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
3175 info->filename = NULL;
3176 info->desktop_id = NULL;
3178 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
3179 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
3180 info->hidden = FALSE;
3181 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
3182 info->exec = g_strconcat (commandline, " %u", NULL);
3184 info->exec = g_strconcat (commandline, " %f", NULL);
3185 info->nodisplay = TRUE;
3186 info->binary = binary_from_exec (info->exec);
3188 if (application_name)
3189 info->name = g_strdup (application_name);
3192 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
3193 split = g_strsplit (commandline, " ", 2);
3194 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
3196 info->name = basename;
3197 if (info->name == NULL)
3198 info->name = g_strdup ("custom");
3200 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
3202 return G_APP_INFO (info);
3205 /* GAppInfo interface init */
3208 g_desktop_app_info_iface_init (GAppInfoIface *iface)
3210 iface->dup = g_desktop_app_info_dup;
3211 iface->equal = g_desktop_app_info_equal;
3212 iface->get_id = g_desktop_app_info_get_id;
3213 iface->get_name = g_desktop_app_info_get_name;
3214 iface->get_description = g_desktop_app_info_get_description;
3215 iface->get_executable = g_desktop_app_info_get_executable;
3216 iface->get_icon = g_desktop_app_info_get_icon;
3217 iface->launch = g_desktop_app_info_launch;
3218 iface->supports_uris = g_desktop_app_info_supports_uris;
3219 iface->supports_files = g_desktop_app_info_supports_files;
3220 iface->launch_uris = g_desktop_app_info_launch_uris;
3221 iface->should_show = g_desktop_app_info_should_show;
3222 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
3223 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
3224 iface->add_supports_type = g_desktop_app_info_add_supports_type;
3225 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
3226 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
3227 iface->can_delete = g_desktop_app_info_can_delete;
3228 iface->do_delete = g_desktop_app_info_delete;
3229 iface->get_commandline = g_desktop_app_info_get_commandline;
3230 iface->get_display_name = g_desktop_app_info_get_display_name;
3231 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
3232 iface->get_supported_types = g_desktop_app_info_get_supported_types;
3235 /* Recommended applications {{{2 */
3238 app_info_in_list (GAppInfo *info,
3241 while (list != NULL)
3243 if (g_app_info_equal (info, list->data))
3251 * g_app_info_get_recommended_for_type:
3252 * @content_type: the content type to find a #GAppInfo for
3254 * Gets a list of recommended #GAppInfos for a given content type, i.e.
3255 * those applications which claim to support the given content type exactly,
3256 * and not by MIME type subclassing.
3257 * Note that the first application of the list is the last used one, i.e.
3258 * the last one for which g_app_info_set_as_last_used_for_type() has been
3261 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
3262 * for given @content_type or %NULL on error.
3267 g_app_info_get_recommended_for_type (const gchar *content_type)
3269 GList *desktop_entries, *l;
3271 GDesktopAppInfo *info;
3273 g_return_val_if_fail (content_type != NULL, NULL);
3275 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
3278 for (l = desktop_entries; l != NULL; l = l->next)
3280 char *desktop_entry = l->data;
3282 info = g_desktop_app_info_new (desktop_entry);
3285 if (app_info_in_list (G_APP_INFO (info), infos))
3286 g_object_unref (info);
3288 infos = g_list_prepend (infos, info);
3290 g_free (desktop_entry);
3293 g_list_free (desktop_entries);
3295 return g_list_reverse (infos);
3299 * g_app_info_get_fallback_for_type:
3300 * @content_type: the content type to find a #GAppInfo for
3302 * Gets a list of fallback #GAppInfos for a given content type, i.e.
3303 * those applications which claim to support the given content type
3304 * by MIME type subclassing and not directly.
3306 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
3307 * for given @content_type or %NULL on error.
3312 g_app_info_get_fallback_for_type (const gchar *content_type)
3314 GList *desktop_entries, *l;
3315 GList *infos, *recommended_infos;
3316 GDesktopAppInfo *info;
3318 g_return_val_if_fail (content_type != NULL, NULL);
3320 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
3321 recommended_infos = g_app_info_get_recommended_for_type (content_type);
3324 for (l = desktop_entries; l != NULL; l = l->next)
3326 char *desktop_entry = l->data;
3328 info = g_desktop_app_info_new (desktop_entry);
3331 if (app_info_in_list (G_APP_INFO (info), infos) ||
3332 app_info_in_list (G_APP_INFO (info), recommended_infos))
3333 g_object_unref (info);
3335 infos = g_list_prepend (infos, info);
3337 g_free (desktop_entry);
3340 g_list_free (desktop_entries);
3341 g_list_free_full (recommended_infos, g_object_unref);
3343 return g_list_reverse (infos);
3347 * g_app_info_get_all_for_type:
3348 * @content_type: the content type to find a #GAppInfo for
3350 * Gets a list of all #GAppInfos for a given content type,
3351 * including the recommended and fallback #GAppInfos. See
3352 * g_app_info_get_recommended_for_type() and
3353 * g_app_info_get_fallback_for_type().
3355 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
3356 * for given @content_type or %NULL on error.
3359 g_app_info_get_all_for_type (const char *content_type)
3361 GList *desktop_entries, *l;
3363 char *user_default = NULL;
3364 GDesktopAppInfo *info;
3366 g_return_val_if_fail (content_type != NULL, NULL);
3368 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
3371 /* put the user default in front of the list, for compatibility */
3372 if (user_default != NULL)
3374 info = g_desktop_app_info_new (user_default);
3377 infos = g_list_prepend (infos, info);
3380 g_free (user_default);
3382 for (l = desktop_entries; l != NULL; l = l->next)
3384 char *desktop_entry = l->data;
3386 info = g_desktop_app_info_new (desktop_entry);
3389 if (app_info_in_list (G_APP_INFO (info), infos))
3390 g_object_unref (info);
3392 infos = g_list_prepend (infos, info);
3394 g_free (desktop_entry);
3397 g_list_free (desktop_entries);
3399 return g_list_reverse (infos);
3403 * g_app_info_reset_type_associations:
3404 * @content_type: a content type
3406 * Removes all changes to the type associations done by
3407 * g_app_info_set_as_default_for_type(),
3408 * g_app_info_set_as_default_for_extension(),
3409 * g_app_info_add_supports_type() or
3410 * g_app_info_remove_supports_type().
3415 g_app_info_reset_type_associations (const char *content_type)
3417 update_mimeapps_list (NULL, content_type,
3423 * g_app_info_get_default_for_type:
3424 * @content_type: the content type to find a #GAppInfo for
3425 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
3428 * Gets the default #GAppInfo for a given content type.
3430 * Returns: (transfer full): #GAppInfo for given @content_type or
3434 g_app_info_get_default_for_type (const char *content_type,
3435 gboolean must_support_uris)
3437 GList *desktop_entries, *l;
3438 char *user_default = NULL;
3441 g_return_val_if_fail (content_type != NULL, NULL);
3443 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
3447 if (user_default != NULL)
3449 info = (GAppInfo *) g_desktop_app_info_new (user_default);
3453 if (must_support_uris && !g_app_info_supports_uris (info))
3455 g_object_unref (info);
3461 g_free (user_default);
3465 g_list_free_full (desktop_entries, g_free);
3469 /* pick the first from the other list that matches our URI
3472 for (l = desktop_entries; l != NULL; l = l->next)
3474 char *desktop_entry = l->data;
3476 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
3479 if (must_support_uris && !g_app_info_supports_uris (info))
3481 g_object_unref (info);
3489 g_list_free_full (desktop_entries, g_free);
3495 * g_app_info_get_default_for_uri_scheme:
3496 * @uri_scheme: a string containing a URI scheme.
3498 * Gets the default application for handling URIs with
3499 * the given URI scheme. A URI scheme is the initial part
3500 * of the URI, up to but not including the ':', e.g. "http",
3503 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3506 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
3509 char *content_type, *scheme_down;
3511 scheme_down = g_ascii_strdown (uri_scheme, -1);
3512 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
3513 g_free (scheme_down);
3514 app_info = g_app_info_get_default_for_type (content_type, FALSE);
3515 g_free (content_type);
3520 /* "Get all" API {{{2 */
3523 * g_desktop_app_info_search:
3524 * @search_string: the search string to use
3526 * Searches desktop files for ones that match @search_string.
3528 * The return value is an array of strvs. Each strv contains a list of
3529 * applications that matched @search_string with an equal score. The
3530 * outer list is sorted by score so that the first strv contains the
3531 * best-matching applications, and so on.
3532 * The algorithm for determining matches is undefined and may change at
3535 * Returns: (array zero-terminated=1) (element-type GStrv) (transfer full): a
3536 * list of strvs. Free each item with g_strfreev() and free the outer
3537 * list with g_free().
3540 g_desktop_app_info_search (const gchar *search_string)
3542 gchar **search_tokens;
3543 gint last_category = -1;
3545 gint n_categories = 0;
3546 gint start_of_category;
3549 search_tokens = g_str_tokenize_and_fold (search_string, NULL, NULL);
3551 desktop_file_dirs_lock ();
3553 reset_total_search_results ();
3555 for (i = 0; i < n_desktop_file_dirs; i++)
3557 for (j = 0; search_tokens[j]; j++)
3559 desktop_file_dir_search (&desktop_file_dirs[i], search_tokens[j]);
3560 merge_token_results (j == 0);
3562 merge_directory_results ();
3565 sort_total_search_results ();
3567 /* Count the total number of unique categories */
3568 for (i = 0; i < static_total_results_size; i++)
3569 if (static_total_results[i].category != last_category)
3571 last_category = static_total_results[i].category;
3575 results = g_new (gchar **, n_categories + 1);
3577 /* Start loading into the results list */
3578 start_of_category = 0;
3579 for (i = 0; i < n_categories; i++)
3581 gint n_items_in_category = 0;
3585 this_category = static_total_results[start_of_category].category;
3587 while (start_of_category + n_items_in_category < static_total_results_size &&
3588 static_total_results[start_of_category + n_items_in_category].category == this_category)
3589 n_items_in_category++;
3591 results[i] = g_new (gchar *, n_items_in_category + 1);
3592 for (j = 0; j < n_items_in_category; j++)
3593 results[i][j] = g_strdup (static_total_results[start_of_category + j].app_name);
3594 results[i][j] = NULL;
3596 start_of_category += n_items_in_category;
3600 desktop_file_dirs_unlock ();
3602 g_strfreev (search_tokens);
3608 * g_app_info_get_all:
3610 * Gets a list of all of the applications currently registered
3613 * For desktop files, this includes applications that have
3614 * `NoDisplay=true` set or are excluded from display by means
3615 * of `OnlyShowIn` or `NotShowIn`. See g_app_info_should_show().
3616 * The returned list does not include applications which have
3617 * the `Hidden` key set.
3619 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfos.
3622 g_app_info_get_all (void)
3625 GHashTableIter iter;
3630 apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
3632 desktop_file_dirs_lock ();
3634 for (i = 0; i < n_desktop_file_dirs; i++)
3635 desktop_file_dir_get_all (&desktop_file_dirs[i], apps);
3637 desktop_file_dirs_unlock ();
3640 g_hash_table_iter_init (&iter, apps);
3641 while (g_hash_table_iter_next (&iter, NULL, &value))
3644 infos = g_list_prepend (infos, value);
3647 g_hash_table_destroy (apps);
3652 /* Caching of mimeinfo.cache and defaults.list files {{{2 */
3656 GHashTable *mime_info_cache_map;
3657 GHashTable *defaults_list_map;
3658 GHashTable *mimeapps_list_added_map;
3659 GHashTable *mimeapps_list_removed_map;
3660 GHashTable *mimeapps_list_defaults_map;
3661 time_t mime_info_cache_timestamp;
3662 time_t defaults_list_timestamp;
3663 time_t mimeapps_list_timestamp;
3667 GList *dirs; /* mimeinfo.cache and defaults.list */
3668 time_t last_stat_time;
3671 static MimeInfoCache *mime_info_cache = NULL;
3672 G_LOCK_DEFINE_STATIC (mime_info_cache);
3674 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
3675 const char *mime_type,
3676 char **new_desktop_file_ids);
3678 static MimeInfoCache * mime_info_cache_new (void);
3681 destroy_info_cache_value (gpointer key,
3685 g_list_free_full (value, g_free);
3689 destroy_info_cache_map (GHashTable *info_cache_map)
3691 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
3692 g_hash_table_destroy (info_cache_map);
3696 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
3697 const char *cache_file,
3703 filename = g_build_filename (dir->path, cache_file, NULL);
3705 if (g_stat (filename, &buf) < 0)
3712 if (buf.st_mtime != *timestamp)
3718 /* Call with lock held */
3720 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
3724 gchar *filename, **mime_types;
3731 if (dir->mime_info_cache_map != NULL &&
3732 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
3733 &dir->mime_info_cache_timestamp))
3736 if (dir->mime_info_cache_map != NULL)
3737 destroy_info_cache_map (dir->mime_info_cache_map);
3739 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3740 (GDestroyNotify) g_free,
3743 key_file = g_key_file_new ();
3745 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
3747 if (g_stat (filename, &buf) < 0)
3750 dir->mime_info_cache_timestamp = buf.st_mtime;
3752 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
3757 if (load_error != NULL)
3760 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
3763 if (load_error != NULL)
3766 for (i = 0; mime_types[i] != NULL; i++)
3768 gchar **desktop_file_ids;
3769 char *unaliased_type;
3770 desktop_file_ids = g_key_file_get_string_list (key_file,
3776 if (desktop_file_ids == NULL)
3779 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3780 mime_info_cache_dir_add_desktop_entries (dir,
3783 g_free (unaliased_type);
3785 g_strfreev (desktop_file_ids);
3788 g_strfreev (mime_types);
3789 g_key_file_free (key_file);
3794 g_key_file_free (key_file);
3796 if (mime_types != NULL)
3797 g_strfreev (mime_types);
3800 g_error_free (load_error);
3804 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
3808 gchar *filename, **mime_types;
3809 char *unaliased_type;
3810 char **desktop_file_ids;
3817 if (dir->defaults_list_map != NULL &&
3818 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
3819 &dir->defaults_list_timestamp))
3822 if (dir->defaults_list_map != NULL)
3823 g_hash_table_destroy (dir->defaults_list_map);
3824 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3825 g_free, (GDestroyNotify)g_strfreev);
3828 key_file = g_key_file_new ();
3830 filename = g_build_filename (dir->path, "defaults.list", NULL);
3831 if (g_stat (filename, &buf) < 0)
3834 dir->defaults_list_timestamp = buf.st_mtime;
3836 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
3840 if (load_error != NULL)
3843 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
3845 if (mime_types != NULL)
3847 for (i = 0; mime_types[i] != NULL; i++)
3849 desktop_file_ids = g_key_file_get_string_list (key_file,
3850 DEFAULT_APPLICATIONS_GROUP,
3854 if (desktop_file_ids == NULL)
3857 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3858 g_hash_table_replace (dir->defaults_list_map,
3863 g_strfreev (mime_types);
3866 g_key_file_free (key_file);
3871 g_key_file_free (key_file);
3873 if (mime_types != NULL)
3874 g_strfreev (mime_types);
3877 g_error_free (load_error);
3881 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
3885 gchar *filename, **mime_types;
3886 char *unaliased_type;
3887 char **desktop_file_ids;
3895 if (dir->mimeapps_list_added_map != NULL &&
3896 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
3897 &dir->mimeapps_list_timestamp))
3900 if (dir->mimeapps_list_added_map != NULL)
3901 g_hash_table_destroy (dir->mimeapps_list_added_map);
3902 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3903 g_free, (GDestroyNotify)g_strfreev);
3905 if (dir->mimeapps_list_removed_map != NULL)
3906 g_hash_table_destroy (dir->mimeapps_list_removed_map);
3907 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3908 g_free, (GDestroyNotify)g_strfreev);
3910 if (dir->mimeapps_list_defaults_map != NULL)
3911 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
3912 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3915 key_file = g_key_file_new ();
3917 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
3918 if (g_stat (filename, &buf) < 0)
3921 dir->mimeapps_list_timestamp = buf.st_mtime;
3923 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
3927 if (load_error != NULL)
3930 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
3932 if (mime_types != NULL)
3934 for (i = 0; mime_types[i] != NULL; i++)
3936 desktop_file_ids = g_key_file_get_string_list (key_file,
3937 ADDED_ASSOCIATIONS_GROUP,
3941 if (desktop_file_ids == NULL)
3944 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3945 g_hash_table_replace (dir->mimeapps_list_added_map,
3950 g_strfreev (mime_types);
3953 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
3955 if (mime_types != NULL)
3957 for (i = 0; mime_types[i] != NULL; i++)
3959 desktop_file_ids = g_key_file_get_string_list (key_file,
3960 REMOVED_ASSOCIATIONS_GROUP,
3964 if (desktop_file_ids == NULL)
3967 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3968 g_hash_table_replace (dir->mimeapps_list_removed_map,
3973 g_strfreev (mime_types);
3976 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
3978 if (mime_types != NULL)
3980 for (i = 0; mime_types[i] != NULL; i++)
3982 desktop_id = g_key_file_get_string (key_file,
3983 DEFAULT_APPLICATIONS_GROUP,
3986 if (desktop_id == NULL)
3989 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3990 g_hash_table_replace (dir->mimeapps_list_defaults_map,
3995 g_strfreev (mime_types);
3998 g_key_file_free (key_file);
4003 g_key_file_free (key_file);
4005 if (mime_types != NULL)
4006 g_strfreev (mime_types);
4009 g_error_free (load_error);
4012 static MimeInfoCacheDir *
4013 mime_info_cache_dir_new (const char *path)
4015 MimeInfoCacheDir *dir;
4017 dir = g_new0 (MimeInfoCacheDir, 1);
4018 dir->path = g_strdup (path);
4024 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
4029 if (dir->mime_info_cache_map != NULL)
4031 destroy_info_cache_map (dir->mime_info_cache_map);
4032 dir->mime_info_cache_map = NULL;
4036 if (dir->defaults_list_map != NULL)
4038 g_hash_table_destroy (dir->defaults_list_map);
4039 dir->defaults_list_map = NULL;
4042 if (dir->mimeapps_list_added_map != NULL)
4044 g_hash_table_destroy (dir->mimeapps_list_added_map);
4045 dir->mimeapps_list_added_map = NULL;
4048 if (dir->mimeapps_list_removed_map != NULL)
4050 g_hash_table_destroy (dir->mimeapps_list_removed_map);
4051 dir->mimeapps_list_removed_map = NULL;
4054 if (dir->mimeapps_list_defaults_map != NULL)
4056 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
4057 dir->mimeapps_list_defaults_map = NULL;
4064 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
4065 const char *mime_type,
4066 char **new_desktop_file_ids)
4068 GList *desktop_file_ids;
4071 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
4074 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
4076 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
4077 desktop_file_ids = g_list_append (desktop_file_ids,
4078 g_strdup (new_desktop_file_ids[i]));
4081 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
4085 mime_info_cache_init_dir_lists (void)
4089 mime_info_cache = mime_info_cache_new ();
4091 desktop_file_dirs_refresh ();
4093 for (i = 0; i < n_desktop_file_dirs; i++)
4095 MimeInfoCacheDir *dir;
4097 dir = mime_info_cache_dir_new (desktop_file_dirs[i].path);
4101 mime_info_cache_dir_init (dir);
4102 mime_info_cache_dir_init_defaults_list (dir);
4103 mime_info_cache_dir_init_mimeapps_list (dir);
4105 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
4111 mime_info_cache_update_dir_lists (void)
4115 tmp = mime_info_cache->dirs;
4119 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
4121 /* No need to do this if we had file monitors... */
4122 mime_info_cache_dir_init (dir);
4123 mime_info_cache_dir_init_defaults_list (dir);
4124 mime_info_cache_dir_init_mimeapps_list (dir);
4131 mime_info_cache_init (void)
4133 G_LOCK (mime_info_cache);
4134 if (mime_info_cache == NULL)
4135 mime_info_cache_init_dir_lists ();
4141 if (now >= mime_info_cache->last_stat_time + 10)
4143 mime_info_cache_update_dir_lists ();
4144 mime_info_cache->last_stat_time = now;
4148 G_UNLOCK (mime_info_cache);
4151 static MimeInfoCache *
4152 mime_info_cache_new (void)
4154 MimeInfoCache *cache;
4156 cache = g_new0 (MimeInfoCache, 1);
4162 mime_info_cache_free (MimeInfoCache *cache)
4167 g_list_free_full (cache->dirs, (GDestroyNotify) mime_info_cache_dir_free);
4172 * mime_info_cache_reload:
4173 * @dir: directory path which needs reloading.
4175 * Reload the mime information for the @dir.
4178 mime_info_cache_reload (const char *dir)
4180 /* FIXME: just reload the dir that needs reloading,
4181 * don't blow the whole cache
4183 if (mime_info_cache != NULL)
4185 G_LOCK (mime_info_cache);
4186 mime_info_cache_free (mime_info_cache);
4187 mime_info_cache = NULL;
4188 G_UNLOCK (mime_info_cache);
4193 append_desktop_entry (GList *list,
4194 const char *desktop_entry,
4195 GList *removed_entries)
4197 /* Add if not already in list, and valid */
4198 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
4199 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
4200 list = g_list_prepend (list, g_strdup (desktop_entry));
4206 * get_all_desktop_entries_for_mime_type:
4207 * @mime_type: a mime type.
4208 * @except: NULL or a strv list
4210 * Returns all the desktop ids for @mime_type. The desktop files
4211 * are listed in an order so that default applications are listed before
4212 * non-default ones, and handlers for inherited mimetypes are listed
4213 * after the base ones.
4215 * Optionally doesn't list the desktop ids given in the @except
4217 * Returns: a #GList containing the desktop ids which claim
4218 * to handle @mime_type.
4221 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
4222 const char **except,
4223 gboolean include_fallback,
4224 char **explicit_default)
4226 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
4227 MimeInfoCacheDir *dir;
4228 char *mime_type, *default_entry = NULL;
4229 char *old_default_entry = NULL;
4232 char **default_entries;
4233 char **removed_associations;
4234 gboolean already_found_handler;
4239 mime_info_cache_init ();
4241 if (include_fallback)
4243 /* collect all ancestors */
4244 mime_types = _g_unix_content_type_get_parents (base_mime_type);
4245 array = g_ptr_array_new ();
4246 for (i = 0; mime_types[i]; i++)
4247 g_ptr_array_add (array, mime_types[i]);
4248 g_free (mime_types);
4249 for (i = 0; i < array->len; i++)
4251 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
4252 for (j = 0; anc[j]; j++)
4254 for (k = 0; k < array->len; k++)
4256 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
4259 if (k == array->len) /* not found */
4260 g_ptr_array_add (array, g_strdup (anc[j]));
4264 g_ptr_array_add (array, NULL);
4265 mime_types = (char **)g_ptr_array_free (array, FALSE);
4269 mime_types = g_malloc0 (2 * sizeof (gchar *));
4270 mime_types[0] = g_strdup (base_mime_type);
4271 mime_types[1] = NULL;
4274 G_LOCK (mime_info_cache);
4276 removed_entries = NULL;
4277 desktop_entries = NULL;
4279 for (i = 0; except != NULL && except[i] != NULL; i++)
4280 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
4282 for (i = 0; mime_types[i] != NULL; i++)
4284 mime_type = mime_types[i];
4286 /* This is true if we already found a handler for a more specific
4287 mimetype. If its set we ignore any defaults for the less specific
4289 already_found_handler = (desktop_entries != NULL);
4291 /* Go through all apps listed in user and system dirs */
4292 for (dir_list = mime_info_cache->dirs;
4294 dir_list = dir_list->next)
4296 dir = dir_list->data;
4298 /* Pick the explicit default application if we got no result earlier
4299 * (ie, for more specific mime types)
4301 if (!already_found_handler)
4303 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
4307 /* Save the default entry if it's the first one we encounter */
4308 if (default_entry == NULL)
4309 default_entry = g_strdup (entry);
4313 /* Then added associations from mimeapps.list */
4314 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
4315 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
4316 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
4318 /* Then removed associations from mimeapps.list */
4319 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
4320 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
4321 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
4323 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
4324 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
4325 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
4327 if (default_entry == NULL && old_default_entry == NULL && !already_found_handler)
4328 old_default_entry = g_strdup (default_entries[j]);
4330 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
4334 /* Go through all entries that support the mimetype */
4335 for (dir_list = mime_info_cache->dirs;
4337 dir_list = dir_list->next)
4339 dir = dir_list->data;
4341 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
4342 for (tmp = list; tmp != NULL; tmp = tmp->next)
4343 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
4347 G_UNLOCK (mime_info_cache);
4349 g_strfreev (mime_types);
4351 /* If we have no default from mimeapps.list, take it from
4352 * defaults.list intead.
4354 * If we do have a default from mimeapps.list, free any one that came
4355 * from defaults.list.
4357 if (default_entry == NULL)
4358 default_entry = old_default_entry;
4360 g_free (old_default_entry);
4362 if (explicit_default != NULL)
4363 *explicit_default = default_entry;
4365 g_free (default_entry);
4367 g_list_free_full (removed_entries, g_free);
4369 desktop_entries = g_list_reverse (desktop_entries);
4371 return desktop_entries;
4374 /* GDesktopAppInfoLookup interface {{{2 */
4376 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
4378 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
4379 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
4382 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
4386 /* "Get for mime type" APIs {{{2 */
4389 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
4390 * @lookup: a #GDesktopAppInfoLookup
4391 * @uri_scheme: a string containing a URI scheme.
4393 * Gets the default application for launching applications
4394 * using this URI scheme for a particular GDesktopAppInfoLookup
4397 * The GDesktopAppInfoLookup interface and this function is used
4398 * to implement g_app_info_get_default_for_uri_scheme() backends
4399 * in a GIO module. There is no reason for applications to use it
4400 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
4402 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
4404 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
4407 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
4408 const char *uri_scheme)
4410 GDesktopAppInfoLookupIface *iface;
4412 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
4414 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
4416 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
4419 G_GNUC_END_IGNORE_DEPRECATIONS
4421 /* Misc getter APIs {{{2 */
4424 * g_desktop_app_info_get_startup_wm_class:
4425 * @info: a #GDesktopAppInfo that supports startup notify
4427 * Retrieves the StartupWMClass field from @info. This represents the
4428 * WM_CLASS property of the main window of the application, if launched
4431 * Returns: (transfer none): the startup WM class, or %NULL if none is set
4432 * in the desktop file.
4437 g_desktop_app_info_get_startup_wm_class (GDesktopAppInfo *info)
4439 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4441 return info->startup_wm_class;
4445 * g_desktop_app_info_get_string:
4446 * @info: a #GDesktopAppInfo
4447 * @key: the key to look up
4449 * Looks up a string value in the keyfile backing @info.
4451 * The @key is looked up in the "Desktop Entry" group.
4453 * Returns: a newly allocated string, or %NULL if the key
4459 g_desktop_app_info_get_string (GDesktopAppInfo *info,
4462 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4464 return g_key_file_get_string (info->keyfile,
4465 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4469 * g_desktop_app_info_get_boolean:
4470 * @info: a #GDesktopAppInfo
4471 * @key: the key to look up
4473 * Looks up a boolean value in the keyfile backing @info.
4475 * The @key is looked up in the "Desktop Entry" group.
4477 * Returns: the boolean value, or %FALSE if the key
4483 g_desktop_app_info_get_boolean (GDesktopAppInfo *info,
4486 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
4488 return g_key_file_get_boolean (info->keyfile,
4489 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4493 * g_desktop_app_info_has_key:
4494 * @info: a #GDesktopAppInfo
4495 * @key: the key to look up
4497 * Returns whether @key exists in the "Desktop Entry" group
4498 * of the keyfile backing @info.
4500 * Returns: %TRUE if the @key exists
4505 g_desktop_app_info_has_key (GDesktopAppInfo *info,
4508 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
4510 return g_key_file_has_key (info->keyfile,
4511 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4514 /* Desktop actions support {{{2 */
4517 * g_desktop_app_info_list_actions:
4518 * @info: a #GDesktopAppInfo
4520 * Returns the list of "additional application actions" supported on the
4521 * desktop file, as per the desktop file specification.
4523 * As per the specification, this is the list of actions that are
4524 * explicitly listed in the "Actions" key of the [Desktop Entry] group.
4526 * Returns: (array zero-terminated=1) (element-type utf8) (transfer none): a list of strings, always non-%NULL
4530 const gchar * const *
4531 g_desktop_app_info_list_actions (GDesktopAppInfo *info)
4533 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4535 return (const gchar **) info->actions;
4539 app_info_has_action (GDesktopAppInfo *info,
4540 const gchar *action_name)
4544 for (i = 0; info->actions[i]; i++)
4545 if (g_str_equal (info->actions[i], action_name))
4552 * g_desktop_app_info_get_action_name:
4553 * @info: a #GDesktopAppInfo
4554 * @action_name: the name of the action as from
4555 * g_desktop_app_info_list_actions()
4557 * Gets the user-visible display name of the "additional application
4558 * action" specified by @action_name.
4560 * This corresponds to the "Name" key within the keyfile group for the
4563 * Returns: (transfer full): the locale-specific action name
4568 g_desktop_app_info_get_action_name (GDesktopAppInfo *info,
4569 const gchar *action_name)
4574 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4575 g_return_val_if_fail (action_name != NULL, NULL);
4576 g_return_val_if_fail (app_info_has_action (info, action_name), NULL);
4578 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4579 result = g_key_file_get_locale_string (info->keyfile, group_name, "Name", NULL, NULL);
4580 g_free (group_name);
4582 /* The spec says that the Name field must be given.
4584 * If it's not, let's follow the behaviour of our get_name()
4585 * implementation above and never return %NULL.
4588 result = g_strdup (_("Unnamed"));
4594 * g_desktop_app_info_launch_action:
4595 * @info: a #GDesktopAppInfo
4596 * @action_name: the name of the action as from
4597 * g_desktop_app_info_list_actions()
4598 * @launch_context: (allow-none): a #GAppLaunchContext
4600 * Activates the named application action.
4602 * You may only call this function on action names that were
4603 * returned from g_desktop_app_info_list_actions().
4605 * Note that if the main entry of the desktop file indicates that the
4606 * application supports startup notification, and @launch_context is
4607 * non-%NULL, then startup notification will be used when activating the
4608 * action (and as such, invocation of the action on the receiving side
4609 * must signal the end of startup notification when it is completed).
4610 * This is the expected behaviour of applications declaring additional
4611 * actions, as per the desktop file specification.
4613 * As with g_app_info_launch() there is no way to detect failures that
4614 * occur while using this function.
4619 g_desktop_app_info_launch_action (GDesktopAppInfo *info,
4620 const gchar *action_name,
4621 GAppLaunchContext *launch_context)
4623 GDBusConnection *session_bus;
4625 g_return_if_fail (G_IS_DESKTOP_APP_INFO (info));
4626 g_return_if_fail (action_name != NULL);
4627 g_return_if_fail (app_info_has_action (info, action_name));
4629 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
4631 if (session_bus && info->app_id)
4635 object_path = object_path_from_appid (info->app_id);
4636 g_dbus_connection_call (session_bus, info->app_id, object_path,
4637 "org.freedesktop.Application", "ActivateAction",
4638 g_variant_new ("(sav@a{sv})", action_name, NULL,
4639 g_desktop_app_info_make_platform_data (info, NULL, launch_context)),
4640 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
4641 g_free (object_path);
4648 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4649 exec_line = g_key_file_get_string (info->keyfile, group_name, "Exec", NULL);
4650 g_free (group_name);
4653 g_desktop_app_info_launch_uris_with_spawn (info, session_bus, exec_line, NULL, launch_context,
4654 _SPAWN_FLAGS_DEFAULT, NULL, NULL, NULL, NULL, NULL);
4657 if (session_bus != NULL)
4659 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
4660 g_object_unref (session_bus);
4665 /* vim:set foldmethod=marker: */