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,
2186 envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
2189 g_list_free_full (launched_files, g_object_unref);
2192 if (!g_spawn_async (info->path,
2202 g_app_launch_context_launch_failed (launch_context, sn_id);
2206 g_list_free (launched_uris);
2211 if (pid_callback != NULL)
2212 pid_callback (info, pid, pid_callback_data);
2214 if (launch_context != NULL)
2216 GVariantBuilder builder;
2217 GVariant *platform_data;
2219 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
2220 g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (pid));
2222 g_variant_builder_add (&builder, "{sv}", "startup-notification-id", g_variant_new_string (sn_id));
2223 platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
2224 g_signal_emit_by_name (launch_context, "launched", info, platform_data);
2225 g_variant_unref (platform_data);
2228 notify_desktop_launch (session_bus,
2237 g_list_free (launched_uris);
2242 while (uris != NULL);
2254 object_path_from_appid (const gchar *app_id)
2259 n = strlen (app_id);
2260 path = g_malloc (n + 2);
2264 for (i = 0; i < n; i++)
2265 if (app_id[i] != '.')
2266 path[i + 1] = app_id[i];
2276 g_desktop_app_info_make_platform_data (GDesktopAppInfo *info,
2278 GAppLaunchContext *launch_context)
2280 GVariantBuilder builder;
2282 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
2286 GList *launched_files = create_files_for_uris (uris);
2288 if (info->startup_notify)
2292 sn_id = g_app_launch_context_get_startup_notify_id (launch_context, G_APP_INFO (info), launched_files);
2294 g_variant_builder_add (&builder, "{sv}", "desktop-startup-id", g_variant_new_take_string (sn_id));
2297 g_list_free_full (launched_files, g_object_unref);
2300 return g_variant_builder_end (&builder);
2304 g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo *info,
2305 GDBusConnection *session_bus,
2307 GAppLaunchContext *launch_context)
2309 GVariantBuilder builder;
2312 g_return_val_if_fail (info != NULL, FALSE);
2314 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
2320 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
2321 for (iter = uris; iter; iter = iter->next)
2322 g_variant_builder_add (&builder, "s", iter->data);
2323 g_variant_builder_close (&builder);
2326 g_variant_builder_add_value (&builder, g_desktop_app_info_make_platform_data (info, uris, launch_context));
2328 /* This is non-blocking API. Similar to launching via fork()/exec()
2329 * we don't wait around to see if the program crashed during startup.
2330 * This is what startup-notification's job is...
2332 object_path = object_path_from_appid (info->app_id);
2333 g_dbus_connection_call (session_bus, info->app_id, object_path, "org.freedesktop.Application",
2334 uris ? "Open" : "Activate", g_variant_builder_end (&builder),
2335 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
2336 g_free (object_path);
2342 g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
2344 GAppLaunchContext *launch_context,
2345 GSpawnFlags spawn_flags,
2346 GSpawnChildSetupFunc user_setup,
2347 gpointer user_setup_data,
2348 GDesktopAppLaunchCallback pid_callback,
2349 gpointer pid_callback_data,
2352 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2353 GDBusConnection *session_bus;
2354 gboolean success = TRUE;
2356 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
2358 if (session_bus && info->app_id)
2359 g_desktop_app_info_launch_uris_with_dbus (info, session_bus, uris, launch_context);
2361 success = g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec, uris, launch_context,
2362 spawn_flags, user_setup, user_setup_data,
2363 pid_callback, pid_callback_data, error);
2365 if (session_bus != NULL)
2367 /* This asynchronous flush holds a reference until it completes,
2368 * which ensures that the following unref won't immediately kill
2369 * the connection if we were the initial owner.
2371 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
2372 g_object_unref (session_bus);
2379 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
2381 GAppLaunchContext *launch_context,
2384 return g_desktop_app_info_launch_uris_internal (appinfo, uris,
2386 _SPAWN_FLAGS_DEFAULT,
2387 NULL, NULL, NULL, NULL,
2392 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
2394 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2396 return info->exec &&
2397 ((strstr (info->exec, "%u") != NULL) ||
2398 (strstr (info->exec, "%U") != NULL));
2402 g_desktop_app_info_supports_files (GAppInfo *appinfo)
2404 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2406 return info->exec &&
2407 ((strstr (info->exec, "%f") != NULL) ||
2408 (strstr (info->exec, "%F") != NULL));
2412 g_desktop_app_info_launch (GAppInfo *appinfo,
2414 GAppLaunchContext *launch_context,
2424 uri = g_file_get_uri (files->data);
2425 uris = g_list_prepend (uris, uri);
2426 files = files->next;
2429 uris = g_list_reverse (uris);
2431 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
2433 g_list_free_full (uris, g_free);
2439 * g_desktop_app_info_launch_uris_as_manager:
2440 * @appinfo: a #GDesktopAppInfo
2441 * @uris: (element-type utf8): List of URIs
2442 * @launch_context: (allow-none): a #GAppLaunchContext
2443 * @spawn_flags: #GSpawnFlags, used for each process
2444 * @user_setup: (scope call) (allow-none): a #GSpawnChildSetupFunc, used once
2446 * @user_setup_data: (closure user_setup) (allow-none): User data for @user_setup
2447 * @pid_callback: (scope call) (allow-none): Callback for child processes
2448 * @pid_callback_data: (closure pid_callback) (allow-none): User data for @callback
2449 * @error: return location for a #GError, or %NULL
2451 * This function performs the equivalent of g_app_info_launch_uris(),
2452 * but is intended primarily for operating system components that
2453 * launch applications. Ordinary applications should use
2454 * g_app_info_launch_uris().
2456 * If the application is launched via traditional UNIX fork()/exec()
2457 * then @spawn_flags, @user_setup and @user_setup_data are used for the
2458 * call to g_spawn_async(). Additionally, @pid_callback (with
2459 * @pid_callback_data) will be called to inform about the PID of the
2462 * If application launching occurs via some other mechanism (eg: D-Bus
2463 * activation) then @spawn_flags, @user_setup, @user_setup_data,
2464 * @pid_callback and @pid_callback_data are ignored.
2466 * Returns: %TRUE on successful launch, %FALSE otherwise.
2469 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
2471 GAppLaunchContext *launch_context,
2472 GSpawnFlags spawn_flags,
2473 GSpawnChildSetupFunc user_setup,
2474 gpointer user_setup_data,
2475 GDesktopAppLaunchCallback pid_callback,
2476 gpointer pid_callback_data,
2479 return g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
2490 /* OnlyShowIn API support {{{2 */
2493 * g_desktop_app_info_set_desktop_env:
2494 * @desktop_env: a string specifying what desktop this is
2496 * Sets the name of the desktop that the application is running in.
2497 * This is used by g_app_info_should_show() and
2498 * g_desktop_app_info_get_show_in() to evaluate the
2499 * `OnlyShowIn` and `NotShowIn`
2500 * desktop entry fields.
2503 * [Desktop Menu specification](http://standards.freedesktop.org/menu-spec/latest/)
2504 * recognizes the following:
2513 * Should be called only once; subsequent calls are ignored.
2516 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
2518 G_LOCK (g_desktop_env);
2520 g_desktop_env = g_strdup (desktop_env);
2521 G_UNLOCK (g_desktop_env);
2525 g_desktop_app_info_should_show (GAppInfo *appinfo)
2527 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2529 if (info->nodisplay)
2532 return g_desktop_app_info_get_show_in (info, NULL);
2535 /* mime types/default apps support {{{2 */
2543 ensure_dir (DirType type,
2546 char *path, *display_name;
2549 if (type == APP_DIR)
2550 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
2552 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
2555 if (g_mkdir_with_parents (path, 0700) == 0)
2559 display_name = g_filename_display_name (path);
2560 if (type == APP_DIR)
2561 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
2562 _("Can't create user application configuration folder %s: %s"),
2563 display_name, g_strerror (errsv));
2565 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
2566 _("Can't create user MIME configuration folder %s: %s"),
2567 display_name, g_strerror (errsv));
2569 g_free (display_name);
2576 update_mimeapps_list (const char *desktop_id,
2577 const char *content_type,
2578 UpdateMimeFlags flags,
2581 char *dirname, *filename, *string;
2583 gboolean load_succeeded, res;
2584 char **old_list, **list;
2585 gsize length, data_size;
2588 char **content_types;
2590 /* Don't add both at start and end */
2591 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
2592 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
2594 dirname = ensure_dir (APP_DIR, error);
2598 filename = g_build_filename (dirname, "mimeapps.list", NULL);
2601 key_file = g_key_file_new ();
2602 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
2603 if (!load_succeeded ||
2604 (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) &&
2605 !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) &&
2606 !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP)))
2608 g_key_file_free (key_file);
2609 key_file = g_key_file_new ();
2614 content_types = g_new (char *, 2);
2615 content_types[0] = g_strdup (content_type);
2616 content_types[1] = NULL;
2620 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
2623 for (k = 0; content_types && content_types[k]; k++)
2625 /* set as default, if requested so */
2626 string = g_key_file_get_string (key_file,
2627 DEFAULT_APPLICATIONS_GROUP,
2631 if (g_strcmp0 (string, desktop_id) != 0 &&
2632 (flags & UPDATE_MIME_SET_DEFAULT))
2635 string = g_strdup (desktop_id);
2637 /* add in the non-default list too, if it's not already there */
2638 flags |= UPDATE_MIME_SET_NON_DEFAULT;
2641 if (string == NULL || desktop_id == NULL)
2642 g_key_file_remove_key (key_file,
2643 DEFAULT_APPLICATIONS_GROUP,
2647 g_key_file_set_string (key_file,
2648 DEFAULT_APPLICATIONS_GROUP,
2657 /* reuse the list from above */
2661 g_strfreev (content_types);
2662 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
2665 for (k = 0; content_types && content_types[k]; k++)
2667 /* Add to the right place in the list */
2670 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
2671 content_types[k], &length, NULL);
2673 list = g_new (char *, 1 + length + 1);
2677 /* if we're adding a last-used hint, just put the application in front of the list */
2678 if (flags & UPDATE_MIME_SET_LAST_USED)
2680 /* avoid adding this again as non-default later */
2681 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
2682 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
2684 list[i++] = g_strdup (desktop_id);
2689 for (j = 0; old_list[j] != NULL; j++)
2691 if (g_strcmp0 (old_list[j], desktop_id) != 0)
2693 /* rewrite other entries if they're different from the new one */
2694 list[i++] = g_strdup (old_list[j]);
2696 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
2698 /* we encountered an old entry which is equal to the one we're adding as non-default,
2699 * don't change its position in the list.
2701 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
2702 list[i++] = g_strdup (old_list[j]);
2707 /* add it at the end of the list */
2708 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
2709 list[i++] = g_strdup (desktop_id);
2713 g_strfreev (old_list);
2715 if (list[0] == NULL || desktop_id == NULL)
2716 g_key_file_remove_key (key_file,
2717 ADDED_ASSOCIATIONS_GROUP,
2721 g_key_file_set_string_list (key_file,
2722 ADDED_ASSOCIATIONS_GROUP,
2724 (const char * const *)list, i);
2731 /* reuse the list from above */
2735 g_strfreev (content_types);
2736 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
2739 for (k = 0; content_types && content_types[k]; k++)
2741 /* Remove from removed associations group (unless remove) */
2744 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
2745 content_types[k], &length, NULL);
2747 list = g_new (char *, 1 + length + 1);
2750 if (flags & UPDATE_MIME_REMOVE)
2751 list[i++] = g_strdup (desktop_id);
2754 for (j = 0; old_list[j] != NULL; j++)
2756 if (g_strcmp0 (old_list[j], desktop_id) != 0)
2757 list[i++] = g_strdup (old_list[j]);
2762 g_strfreev (old_list);
2764 if (list[0] == NULL || desktop_id == NULL)
2765 g_key_file_remove_key (key_file,
2766 REMOVED_ASSOCIATIONS_GROUP,
2770 g_key_file_set_string_list (key_file,
2771 REMOVED_ASSOCIATIONS_GROUP,
2773 (const char * const *)list, i);
2778 g_strfreev (content_types);
2780 data = g_key_file_to_data (key_file, &data_size, error);
2781 g_key_file_free (key_file);
2783 res = g_file_set_contents (filename, data, data_size, error);
2785 mime_info_cache_reload (NULL);
2794 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
2795 const char *content_type,
2798 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2800 if (!g_desktop_app_info_ensure_saved (info, error))
2803 if (!info->desktop_id)
2805 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2806 _("Application information lacks an identifier"));
2810 /* both add support for the content type and set as last used */
2811 return update_mimeapps_list (info->desktop_id, content_type,
2812 UPDATE_MIME_SET_NON_DEFAULT |
2813 UPDATE_MIME_SET_LAST_USED,
2818 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
2819 const char *content_type,
2822 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2824 if (!g_desktop_app_info_ensure_saved (info, error))
2827 if (!info->desktop_id)
2829 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2830 _("Application information lacks an identifier"));
2834 return update_mimeapps_list (info->desktop_id, content_type,
2835 UPDATE_MIME_SET_DEFAULT,
2840 update_program_done (GPid pid,
2844 /* Did the application exit correctly */
2845 if (g_spawn_check_exit_status (status, NULL))
2847 /* Here we could clean out any caches in use */
2852 run_update_command (char *command,
2861 GError *error = NULL;
2864 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
2866 if (g_spawn_async ("/", argv,
2868 G_SPAWN_SEARCH_PATH |
2869 G_SPAWN_STDOUT_TO_DEV_NULL |
2870 G_SPAWN_STDERR_TO_DEV_NULL |
2871 G_SPAWN_DO_NOT_REAP_CHILD,
2872 NULL, NULL, /* No setup function */
2875 g_child_watch_add (pid, update_program_done, NULL);
2878 /* If we get an error at this point, it's quite likely the user doesn't
2879 * have an installed copy of either 'update-mime-database' or
2880 * 'update-desktop-database'. I don't think we want to popup an error
2881 * dialog at this point, so we just do a g_warning to give the user a
2882 * chance of debugging it.
2884 g_warning ("%s", error->message);
2891 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
2892 const char *extension,
2895 char *filename, *basename, *mimetype;
2899 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
2902 dirname = ensure_dir (MIMETYPE_DIR, error);
2906 basename = g_strdup_printf ("user-extension-%s.xml", extension);
2907 filename = g_build_filename (dirname, basename, NULL);
2911 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
2913 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
2918 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
2919 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
2920 " <mime-type type=\"%s\">\n"
2921 " <comment>%s document</comment>\n"
2922 " <glob pattern=\"*.%s\"/>\n"
2924 "</mime-info>\n", mimetype, extension, extension);
2926 g_file_set_contents (filename, contents, -1, NULL);
2929 run_update_command ("update-mime-database", "mime");
2933 res = g_desktop_app_info_set_as_default_for_type (appinfo,
2943 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
2944 const char *content_type,
2947 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2949 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
2952 return update_mimeapps_list (info->desktop_id, content_type,
2953 UPDATE_MIME_SET_NON_DEFAULT,
2958 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
2964 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
2965 const char *content_type,
2968 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2970 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
2973 return update_mimeapps_list (info->desktop_id, content_type,
2978 static const char **
2979 g_desktop_app_info_get_supported_types (GAppInfo *appinfo)
2981 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2983 return (const char**) info->mime_types;
2986 /* Saving and deleting {{{2 */
2989 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
2995 char *data, *desktop_id;
3000 if (info->filename != NULL)
3003 /* This is only used for object created with
3004 * g_app_info_create_from_commandline. All other
3005 * object should have a filename
3008 dirname = ensure_dir (APP_DIR, error);
3012 key_file = g_key_file_new ();
3014 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3015 "Encoding", "UTF-8");
3016 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3017 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
3018 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3019 G_KEY_FILE_DESKTOP_KEY_TYPE,
3020 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
3022 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3023 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
3024 if (info->nodisplay)
3025 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3026 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
3028 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3029 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
3031 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3032 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
3034 if (info->generic_name != NULL)
3035 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3036 GENERIC_NAME_KEY, info->generic_name);
3038 if (info->fullname != NULL)
3039 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3040 FULL_NAME_KEY, info->fullname);
3042 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3043 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
3045 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3046 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
3048 data = g_key_file_to_data (key_file, &data_size, NULL);
3049 g_key_file_free (key_file);
3051 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
3052 filename = g_build_filename (dirname, desktop_id, NULL);
3053 g_free (desktop_id);
3056 fd = g_mkstemp (filename);
3061 display_name = g_filename_display_name (filename);
3062 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
3063 _("Can't create user desktop file %s"), display_name);
3064 g_free (display_name);
3070 desktop_id = g_path_get_basename (filename);
3072 /* FIXME - actually handle error */
3073 (void) g_close (fd, NULL);
3075 res = g_file_set_contents (filename, data, data_size, error);
3079 g_free (desktop_id);
3084 info->filename = filename;
3085 info->desktop_id = desktop_id;
3087 run_update_command ("update-desktop-database", "applications");
3089 /* We just dropped a file in the user's desktop file directory. Save
3090 * the monitor the bother of having to notice it and invalidate
3093 * This means that calls directly following this will be able to see
3094 * the results immediately.
3096 desktop_file_dirs_invalidate_user ();
3102 g_desktop_app_info_can_delete (GAppInfo *appinfo)
3104 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3108 if (strstr (info->filename, "/userapp-"))
3109 return g_access (info->filename, W_OK) == 0;
3116 g_desktop_app_info_delete (GAppInfo *appinfo)
3118 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3122 if (g_remove (info->filename) == 0)
3124 update_mimeapps_list (info->desktop_id, NULL,
3128 g_free (info->filename);
3129 info->filename = NULL;
3130 g_free (info->desktop_id);
3131 info->desktop_id = NULL;
3140 /* Create for commandline {{{2 */
3142 * g_app_info_create_from_commandline:
3143 * @commandline: the commandline to use
3144 * @application_name: (allow-none): the application name, or %NULL to use @commandline
3145 * @flags: flags that can specify details of the created #GAppInfo
3146 * @error: a #GError location to store the error occurring, %NULL to ignore.
3148 * Creates a new #GAppInfo from the given information.
3150 * Note that for @commandline, the quoting rules of the Exec key of the
3151 * [freedesktop.org Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec)
3152 * are applied. For example, if the @commandline contains
3153 * percent-encoded URIs, the percent-character must be doubled in order to prevent it from
3154 * being swallowed by Exec key unquoting. See the specification for exact quoting rules.
3156 * Returns: (transfer full): new #GAppInfo for given command.
3159 g_app_info_create_from_commandline (const char *commandline,
3160 const char *application_name,
3161 GAppInfoCreateFlags flags,
3166 GDesktopAppInfo *info;
3168 g_return_val_if_fail (commandline, NULL);
3170 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
3172 info->filename = NULL;
3173 info->desktop_id = NULL;
3175 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
3176 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
3177 info->hidden = FALSE;
3178 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
3179 info->exec = g_strconcat (commandline, " %u", NULL);
3181 info->exec = g_strconcat (commandline, " %f", NULL);
3182 info->nodisplay = TRUE;
3183 info->binary = binary_from_exec (info->exec);
3185 if (application_name)
3186 info->name = g_strdup (application_name);
3189 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
3190 split = g_strsplit (commandline, " ", 2);
3191 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
3193 info->name = basename;
3194 if (info->name == NULL)
3195 info->name = g_strdup ("custom");
3197 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
3199 return G_APP_INFO (info);
3202 /* GAppInfo interface init */
3205 g_desktop_app_info_iface_init (GAppInfoIface *iface)
3207 iface->dup = g_desktop_app_info_dup;
3208 iface->equal = g_desktop_app_info_equal;
3209 iface->get_id = g_desktop_app_info_get_id;
3210 iface->get_name = g_desktop_app_info_get_name;
3211 iface->get_description = g_desktop_app_info_get_description;
3212 iface->get_executable = g_desktop_app_info_get_executable;
3213 iface->get_icon = g_desktop_app_info_get_icon;
3214 iface->launch = g_desktop_app_info_launch;
3215 iface->supports_uris = g_desktop_app_info_supports_uris;
3216 iface->supports_files = g_desktop_app_info_supports_files;
3217 iface->launch_uris = g_desktop_app_info_launch_uris;
3218 iface->should_show = g_desktop_app_info_should_show;
3219 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
3220 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
3221 iface->add_supports_type = g_desktop_app_info_add_supports_type;
3222 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
3223 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
3224 iface->can_delete = g_desktop_app_info_can_delete;
3225 iface->do_delete = g_desktop_app_info_delete;
3226 iface->get_commandline = g_desktop_app_info_get_commandline;
3227 iface->get_display_name = g_desktop_app_info_get_display_name;
3228 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
3229 iface->get_supported_types = g_desktop_app_info_get_supported_types;
3232 /* Recommended applications {{{2 */
3235 app_info_in_list (GAppInfo *info,
3238 while (list != NULL)
3240 if (g_app_info_equal (info, list->data))
3248 * g_app_info_get_recommended_for_type:
3249 * @content_type: the content type to find a #GAppInfo for
3251 * Gets a list of recommended #GAppInfos for a given content type, i.e.
3252 * those applications which claim to support the given content type exactly,
3253 * and not by MIME type subclassing.
3254 * Note that the first application of the list is the last used one, i.e.
3255 * the last one for which g_app_info_set_as_last_used_for_type() has been
3258 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
3259 * for given @content_type or %NULL on error.
3264 g_app_info_get_recommended_for_type (const gchar *content_type)
3266 GList *desktop_entries, *l;
3268 GDesktopAppInfo *info;
3270 g_return_val_if_fail (content_type != NULL, NULL);
3272 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
3275 for (l = desktop_entries; l != NULL; l = l->next)
3277 char *desktop_entry = l->data;
3279 info = g_desktop_app_info_new (desktop_entry);
3282 if (app_info_in_list (G_APP_INFO (info), infos))
3283 g_object_unref (info);
3285 infos = g_list_prepend (infos, info);
3287 g_free (desktop_entry);
3290 g_list_free (desktop_entries);
3292 return g_list_reverse (infos);
3296 * g_app_info_get_fallback_for_type:
3297 * @content_type: the content type to find a #GAppInfo for
3299 * Gets a list of fallback #GAppInfos for a given content type, i.e.
3300 * those applications which claim to support the given content type
3301 * by MIME type subclassing and not directly.
3303 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
3304 * for given @content_type or %NULL on error.
3309 g_app_info_get_fallback_for_type (const gchar *content_type)
3311 GList *desktop_entries, *l;
3312 GList *infos, *recommended_infos;
3313 GDesktopAppInfo *info;
3315 g_return_val_if_fail (content_type != NULL, NULL);
3317 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
3318 recommended_infos = g_app_info_get_recommended_for_type (content_type);
3321 for (l = desktop_entries; l != NULL; l = l->next)
3323 char *desktop_entry = l->data;
3325 info = g_desktop_app_info_new (desktop_entry);
3328 if (app_info_in_list (G_APP_INFO (info), infos) ||
3329 app_info_in_list (G_APP_INFO (info), recommended_infos))
3330 g_object_unref (info);
3332 infos = g_list_prepend (infos, info);
3334 g_free (desktop_entry);
3337 g_list_free (desktop_entries);
3338 g_list_free_full (recommended_infos, g_object_unref);
3340 return g_list_reverse (infos);
3344 * g_app_info_get_all_for_type:
3345 * @content_type: the content type to find a #GAppInfo for
3347 * Gets a list of all #GAppInfos for a given content type,
3348 * including the recommended and fallback #GAppInfos. See
3349 * g_app_info_get_recommended_for_type() and
3350 * g_app_info_get_fallback_for_type().
3352 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
3353 * for given @content_type or %NULL on error.
3356 g_app_info_get_all_for_type (const char *content_type)
3358 GList *desktop_entries, *l;
3360 char *user_default = NULL;
3361 GDesktopAppInfo *info;
3363 g_return_val_if_fail (content_type != NULL, NULL);
3365 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
3368 /* put the user default in front of the list, for compatibility */
3369 if (user_default != NULL)
3371 info = g_desktop_app_info_new (user_default);
3374 infos = g_list_prepend (infos, info);
3377 g_free (user_default);
3379 for (l = desktop_entries; l != NULL; l = l->next)
3381 char *desktop_entry = l->data;
3383 info = g_desktop_app_info_new (desktop_entry);
3386 if (app_info_in_list (G_APP_INFO (info), infos))
3387 g_object_unref (info);
3389 infos = g_list_prepend (infos, info);
3391 g_free (desktop_entry);
3394 g_list_free (desktop_entries);
3396 return g_list_reverse (infos);
3400 * g_app_info_reset_type_associations:
3401 * @content_type: a content type
3403 * Removes all changes to the type associations done by
3404 * g_app_info_set_as_default_for_type(),
3405 * g_app_info_set_as_default_for_extension(),
3406 * g_app_info_add_supports_type() or
3407 * g_app_info_remove_supports_type().
3412 g_app_info_reset_type_associations (const char *content_type)
3414 update_mimeapps_list (NULL, content_type,
3420 * g_app_info_get_default_for_type:
3421 * @content_type: the content type to find a #GAppInfo for
3422 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
3425 * Gets the default #GAppInfo for a given content type.
3427 * Returns: (transfer full): #GAppInfo for given @content_type or
3431 g_app_info_get_default_for_type (const char *content_type,
3432 gboolean must_support_uris)
3434 GList *desktop_entries, *l;
3435 char *user_default = NULL;
3438 g_return_val_if_fail (content_type != NULL, NULL);
3440 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
3444 if (user_default != NULL)
3446 info = (GAppInfo *) g_desktop_app_info_new (user_default);
3450 if (must_support_uris && !g_app_info_supports_uris (info))
3452 g_object_unref (info);
3458 g_free (user_default);
3462 g_list_free_full (desktop_entries, g_free);
3466 /* pick the first from the other list that matches our URI
3469 for (l = desktop_entries; l != NULL; l = l->next)
3471 char *desktop_entry = l->data;
3473 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
3476 if (must_support_uris && !g_app_info_supports_uris (info))
3478 g_object_unref (info);
3486 g_list_free_full (desktop_entries, g_free);
3492 * g_app_info_get_default_for_uri_scheme:
3493 * @uri_scheme: a string containing a URI scheme.
3495 * Gets the default application for handling URIs with
3496 * the given URI scheme. A URI scheme is the initial part
3497 * of the URI, up to but not including the ':', e.g. "http",
3500 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3503 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
3506 char *content_type, *scheme_down;
3508 scheme_down = g_ascii_strdown (uri_scheme, -1);
3509 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
3510 g_free (scheme_down);
3511 app_info = g_app_info_get_default_for_type (content_type, FALSE);
3512 g_free (content_type);
3517 /* "Get all" API {{{2 */
3520 * g_desktop_app_info_search:
3521 * @search_string: the search string to use
3523 * Searches desktop files for ones that match @search_string.
3525 * The return value is an array of strvs. Each strv contains a list of
3526 * applications that matched @search_string with an equal score. The
3527 * outer list is sorted by score so that the first strv contains the
3528 * best-matching applications, and so on.
3529 * The algorithm for determining matches is undefined and may change at
3532 * Returns: (array zero-terminated=1) (element-type GStrv) (transfer full): a
3533 * list of strvs. Free each item with g_strfreev() and free the outer
3534 * list with g_free().
3537 g_desktop_app_info_search (const gchar *search_string)
3539 gchar **search_tokens;
3540 gint last_category = -1;
3542 gint n_categories = 0;
3543 gint start_of_category;
3546 search_tokens = g_str_tokenize_and_fold (search_string, NULL, NULL);
3548 desktop_file_dirs_lock ();
3550 reset_total_search_results ();
3552 for (i = 0; i < n_desktop_file_dirs; i++)
3554 for (j = 0; search_tokens[j]; j++)
3556 desktop_file_dir_search (&desktop_file_dirs[i], search_tokens[j]);
3557 merge_token_results (j == 0);
3559 merge_directory_results ();
3562 sort_total_search_results ();
3564 /* Count the total number of unique categories */
3565 for (i = 0; i < static_total_results_size; i++)
3566 if (static_total_results[i].category != last_category)
3568 last_category = static_total_results[i].category;
3572 results = g_new (gchar **, n_categories + 1);
3574 /* Start loading into the results list */
3575 start_of_category = 0;
3576 for (i = 0; i < n_categories; i++)
3578 gint n_items_in_category = 0;
3582 this_category = static_total_results[start_of_category].category;
3584 while (start_of_category + n_items_in_category < static_total_results_size &&
3585 static_total_results[start_of_category + n_items_in_category].category == this_category)
3586 n_items_in_category++;
3588 results[i] = g_new (gchar *, n_items_in_category + 1);
3589 for (j = 0; j < n_items_in_category; j++)
3590 results[i][j] = g_strdup (static_total_results[start_of_category + j].app_name);
3591 results[i][j] = NULL;
3593 start_of_category += n_items_in_category;
3597 desktop_file_dirs_unlock ();
3599 g_strfreev (search_tokens);
3605 * g_app_info_get_all:
3607 * Gets a list of all of the applications currently registered
3610 * For desktop files, this includes applications that have
3611 * `NoDisplay=true` set or are excluded from display by means
3612 * of `OnlyShowIn` or `NotShowIn`. See g_app_info_should_show().
3613 * The returned list does not include applications which have
3614 * the `Hidden` key set.
3616 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfos.
3619 g_app_info_get_all (void)
3622 GHashTableIter iter;
3627 apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
3629 desktop_file_dirs_lock ();
3631 for (i = 0; i < n_desktop_file_dirs; i++)
3632 desktop_file_dir_get_all (&desktop_file_dirs[i], apps);
3634 desktop_file_dirs_unlock ();
3637 g_hash_table_iter_init (&iter, apps);
3638 while (g_hash_table_iter_next (&iter, NULL, &value))
3641 infos = g_list_prepend (infos, value);
3644 g_hash_table_destroy (apps);
3649 /* Caching of mimeinfo.cache and defaults.list files {{{2 */
3653 GHashTable *mime_info_cache_map;
3654 GHashTable *defaults_list_map;
3655 GHashTable *mimeapps_list_added_map;
3656 GHashTable *mimeapps_list_removed_map;
3657 GHashTable *mimeapps_list_defaults_map;
3658 time_t mime_info_cache_timestamp;
3659 time_t defaults_list_timestamp;
3660 time_t mimeapps_list_timestamp;
3664 GList *dirs; /* mimeinfo.cache and defaults.list */
3665 time_t last_stat_time;
3668 static MimeInfoCache *mime_info_cache = NULL;
3669 G_LOCK_DEFINE_STATIC (mime_info_cache);
3671 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
3672 const char *mime_type,
3673 char **new_desktop_file_ids);
3675 static MimeInfoCache * mime_info_cache_new (void);
3678 destroy_info_cache_value (gpointer key,
3682 g_list_free_full (value, g_free);
3686 destroy_info_cache_map (GHashTable *info_cache_map)
3688 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
3689 g_hash_table_destroy (info_cache_map);
3693 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
3694 const char *cache_file,
3700 filename = g_build_filename (dir->path, cache_file, NULL);
3702 if (g_stat (filename, &buf) < 0)
3709 if (buf.st_mtime != *timestamp)
3715 /* Call with lock held */
3717 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
3721 gchar *filename, **mime_types;
3728 if (dir->mime_info_cache_map != NULL &&
3729 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
3730 &dir->mime_info_cache_timestamp))
3733 if (dir->mime_info_cache_map != NULL)
3734 destroy_info_cache_map (dir->mime_info_cache_map);
3736 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3737 (GDestroyNotify) g_free,
3740 key_file = g_key_file_new ();
3742 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
3744 if (g_stat (filename, &buf) < 0)
3747 dir->mime_info_cache_timestamp = buf.st_mtime;
3749 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
3754 if (load_error != NULL)
3757 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
3760 if (load_error != NULL)
3763 for (i = 0; mime_types[i] != NULL; i++)
3765 gchar **desktop_file_ids;
3766 char *unaliased_type;
3767 desktop_file_ids = g_key_file_get_string_list (key_file,
3773 if (desktop_file_ids == NULL)
3776 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3777 mime_info_cache_dir_add_desktop_entries (dir,
3780 g_free (unaliased_type);
3782 g_strfreev (desktop_file_ids);
3785 g_strfreev (mime_types);
3786 g_key_file_free (key_file);
3791 g_key_file_free (key_file);
3793 if (mime_types != NULL)
3794 g_strfreev (mime_types);
3797 g_error_free (load_error);
3801 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
3805 gchar *filename, **mime_types;
3806 char *unaliased_type;
3807 char **desktop_file_ids;
3814 if (dir->defaults_list_map != NULL &&
3815 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
3816 &dir->defaults_list_timestamp))
3819 if (dir->defaults_list_map != NULL)
3820 g_hash_table_destroy (dir->defaults_list_map);
3821 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3822 g_free, (GDestroyNotify)g_strfreev);
3825 key_file = g_key_file_new ();
3827 filename = g_build_filename (dir->path, "defaults.list", NULL);
3828 if (g_stat (filename, &buf) < 0)
3831 dir->defaults_list_timestamp = buf.st_mtime;
3833 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
3837 if (load_error != NULL)
3840 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
3842 if (mime_types != NULL)
3844 for (i = 0; mime_types[i] != NULL; i++)
3846 desktop_file_ids = g_key_file_get_string_list (key_file,
3847 DEFAULT_APPLICATIONS_GROUP,
3851 if (desktop_file_ids == NULL)
3854 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3855 g_hash_table_replace (dir->defaults_list_map,
3860 g_strfreev (mime_types);
3863 g_key_file_free (key_file);
3868 g_key_file_free (key_file);
3870 if (mime_types != NULL)
3871 g_strfreev (mime_types);
3874 g_error_free (load_error);
3878 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
3882 gchar *filename, **mime_types;
3883 char *unaliased_type;
3884 char **desktop_file_ids;
3892 if (dir->mimeapps_list_added_map != NULL &&
3893 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
3894 &dir->mimeapps_list_timestamp))
3897 if (dir->mimeapps_list_added_map != NULL)
3898 g_hash_table_destroy (dir->mimeapps_list_added_map);
3899 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3900 g_free, (GDestroyNotify)g_strfreev);
3902 if (dir->mimeapps_list_removed_map != NULL)
3903 g_hash_table_destroy (dir->mimeapps_list_removed_map);
3904 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3905 g_free, (GDestroyNotify)g_strfreev);
3907 if (dir->mimeapps_list_defaults_map != NULL)
3908 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
3909 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3912 key_file = g_key_file_new ();
3914 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
3915 if (g_stat (filename, &buf) < 0)
3918 dir->mimeapps_list_timestamp = buf.st_mtime;
3920 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
3924 if (load_error != NULL)
3927 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
3929 if (mime_types != NULL)
3931 for (i = 0; mime_types[i] != NULL; i++)
3933 desktop_file_ids = g_key_file_get_string_list (key_file,
3934 ADDED_ASSOCIATIONS_GROUP,
3938 if (desktop_file_ids == NULL)
3941 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3942 g_hash_table_replace (dir->mimeapps_list_added_map,
3947 g_strfreev (mime_types);
3950 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
3952 if (mime_types != NULL)
3954 for (i = 0; mime_types[i] != NULL; i++)
3956 desktop_file_ids = g_key_file_get_string_list (key_file,
3957 REMOVED_ASSOCIATIONS_GROUP,
3961 if (desktop_file_ids == NULL)
3964 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3965 g_hash_table_replace (dir->mimeapps_list_removed_map,
3970 g_strfreev (mime_types);
3973 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
3975 if (mime_types != NULL)
3977 for (i = 0; mime_types[i] != NULL; i++)
3979 desktop_id = g_key_file_get_string (key_file,
3980 DEFAULT_APPLICATIONS_GROUP,
3983 if (desktop_id == NULL)
3986 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3987 g_hash_table_replace (dir->mimeapps_list_defaults_map,
3992 g_strfreev (mime_types);
3995 g_key_file_free (key_file);
4000 g_key_file_free (key_file);
4002 if (mime_types != NULL)
4003 g_strfreev (mime_types);
4006 g_error_free (load_error);
4009 static MimeInfoCacheDir *
4010 mime_info_cache_dir_new (const char *path)
4012 MimeInfoCacheDir *dir;
4014 dir = g_new0 (MimeInfoCacheDir, 1);
4015 dir->path = g_strdup (path);
4021 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
4026 if (dir->mime_info_cache_map != NULL)
4028 destroy_info_cache_map (dir->mime_info_cache_map);
4029 dir->mime_info_cache_map = NULL;
4033 if (dir->defaults_list_map != NULL)
4035 g_hash_table_destroy (dir->defaults_list_map);
4036 dir->defaults_list_map = NULL;
4039 if (dir->mimeapps_list_added_map != NULL)
4041 g_hash_table_destroy (dir->mimeapps_list_added_map);
4042 dir->mimeapps_list_added_map = NULL;
4045 if (dir->mimeapps_list_removed_map != NULL)
4047 g_hash_table_destroy (dir->mimeapps_list_removed_map);
4048 dir->mimeapps_list_removed_map = NULL;
4051 if (dir->mimeapps_list_defaults_map != NULL)
4053 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
4054 dir->mimeapps_list_defaults_map = NULL;
4061 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
4062 const char *mime_type,
4063 char **new_desktop_file_ids)
4065 GList *desktop_file_ids;
4068 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
4071 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
4073 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
4074 desktop_file_ids = g_list_append (desktop_file_ids,
4075 g_strdup (new_desktop_file_ids[i]));
4078 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
4082 mime_info_cache_init_dir_lists (void)
4086 mime_info_cache = mime_info_cache_new ();
4088 desktop_file_dirs_refresh ();
4090 for (i = 0; i < n_desktop_file_dirs; i++)
4092 MimeInfoCacheDir *dir;
4094 dir = mime_info_cache_dir_new (desktop_file_dirs[i].path);
4098 mime_info_cache_dir_init (dir);
4099 mime_info_cache_dir_init_defaults_list (dir);
4100 mime_info_cache_dir_init_mimeapps_list (dir);
4102 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
4108 mime_info_cache_update_dir_lists (void)
4112 tmp = mime_info_cache->dirs;
4116 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
4118 /* No need to do this if we had file monitors... */
4119 mime_info_cache_dir_init (dir);
4120 mime_info_cache_dir_init_defaults_list (dir);
4121 mime_info_cache_dir_init_mimeapps_list (dir);
4128 mime_info_cache_init (void)
4130 G_LOCK (mime_info_cache);
4131 if (mime_info_cache == NULL)
4132 mime_info_cache_init_dir_lists ();
4138 if (now >= mime_info_cache->last_stat_time + 10)
4140 mime_info_cache_update_dir_lists ();
4141 mime_info_cache->last_stat_time = now;
4145 G_UNLOCK (mime_info_cache);
4148 static MimeInfoCache *
4149 mime_info_cache_new (void)
4151 MimeInfoCache *cache;
4153 cache = g_new0 (MimeInfoCache, 1);
4159 mime_info_cache_free (MimeInfoCache *cache)
4164 g_list_free_full (cache->dirs, (GDestroyNotify) mime_info_cache_dir_free);
4169 * mime_info_cache_reload:
4170 * @dir: directory path which needs reloading.
4172 * Reload the mime information for the @dir.
4175 mime_info_cache_reload (const char *dir)
4177 /* FIXME: just reload the dir that needs reloading,
4178 * don't blow the whole cache
4180 if (mime_info_cache != NULL)
4182 G_LOCK (mime_info_cache);
4183 mime_info_cache_free (mime_info_cache);
4184 mime_info_cache = NULL;
4185 G_UNLOCK (mime_info_cache);
4190 append_desktop_entry (GList *list,
4191 const char *desktop_entry,
4192 GList *removed_entries)
4194 /* Add if not already in list, and valid */
4195 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
4196 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
4197 list = g_list_prepend (list, g_strdup (desktop_entry));
4203 * get_all_desktop_entries_for_mime_type:
4204 * @mime_type: a mime type.
4205 * @except: NULL or a strv list
4207 * Returns all the desktop ids for @mime_type. The desktop files
4208 * are listed in an order so that default applications are listed before
4209 * non-default ones, and handlers for inherited mimetypes are listed
4210 * after the base ones.
4212 * Optionally doesn't list the desktop ids given in the @except
4214 * Returns: a #GList containing the desktop ids which claim
4215 * to handle @mime_type.
4218 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
4219 const char **except,
4220 gboolean include_fallback,
4221 char **explicit_default)
4223 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
4224 MimeInfoCacheDir *dir;
4225 char *mime_type, *default_entry = NULL;
4226 char *old_default_entry = NULL;
4229 char **default_entries;
4230 char **removed_associations;
4231 gboolean already_found_handler;
4236 mime_info_cache_init ();
4238 if (include_fallback)
4240 /* collect all ancestors */
4241 mime_types = _g_unix_content_type_get_parents (base_mime_type);
4242 array = g_ptr_array_new ();
4243 for (i = 0; mime_types[i]; i++)
4244 g_ptr_array_add (array, mime_types[i]);
4245 g_free (mime_types);
4246 for (i = 0; i < array->len; i++)
4248 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
4249 for (j = 0; anc[j]; j++)
4251 for (k = 0; k < array->len; k++)
4253 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
4256 if (k == array->len) /* not found */
4257 g_ptr_array_add (array, g_strdup (anc[j]));
4261 g_ptr_array_add (array, NULL);
4262 mime_types = (char **)g_ptr_array_free (array, FALSE);
4266 mime_types = g_malloc0 (2 * sizeof (gchar *));
4267 mime_types[0] = g_strdup (base_mime_type);
4268 mime_types[1] = NULL;
4271 G_LOCK (mime_info_cache);
4273 removed_entries = NULL;
4274 desktop_entries = NULL;
4276 for (i = 0; except != NULL && except[i] != NULL; i++)
4277 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
4279 for (i = 0; mime_types[i] != NULL; i++)
4281 mime_type = mime_types[i];
4283 /* This is true if we already found a handler for a more specific
4284 mimetype. If its set we ignore any defaults for the less specific
4286 already_found_handler = (desktop_entries != NULL);
4288 /* Go through all apps listed in user and system dirs */
4289 for (dir_list = mime_info_cache->dirs;
4291 dir_list = dir_list->next)
4293 dir = dir_list->data;
4295 /* Pick the explicit default application if we got no result earlier
4296 * (ie, for more specific mime types)
4298 if (!already_found_handler)
4300 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
4304 /* Save the default entry if it's the first one we encounter */
4305 if (default_entry == NULL)
4306 default_entry = g_strdup (entry);
4310 /* Then added associations from mimeapps.list */
4311 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
4312 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
4313 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
4315 /* Then removed associations from mimeapps.list */
4316 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
4317 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
4318 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
4320 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
4321 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
4322 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
4324 if (default_entry == NULL && old_default_entry == NULL && !already_found_handler)
4325 old_default_entry = g_strdup (default_entries[j]);
4327 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
4331 /* Go through all entries that support the mimetype */
4332 for (dir_list = mime_info_cache->dirs;
4334 dir_list = dir_list->next)
4336 dir = dir_list->data;
4338 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
4339 for (tmp = list; tmp != NULL; tmp = tmp->next)
4340 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
4344 G_UNLOCK (mime_info_cache);
4346 g_strfreev (mime_types);
4348 /* If we have no default from mimeapps.list, take it from
4349 * defaults.list intead.
4351 * If we do have a default from mimeapps.list, free any one that came
4352 * from defaults.list.
4354 if (default_entry == NULL)
4355 default_entry = old_default_entry;
4357 g_free (old_default_entry);
4359 if (explicit_default != NULL)
4360 *explicit_default = default_entry;
4362 g_free (default_entry);
4364 g_list_free_full (removed_entries, g_free);
4366 desktop_entries = g_list_reverse (desktop_entries);
4368 return desktop_entries;
4371 /* GDesktopAppInfoLookup interface {{{2 */
4373 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
4375 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
4376 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
4379 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
4383 /* "Get for mime type" APIs {{{2 */
4386 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
4387 * @lookup: a #GDesktopAppInfoLookup
4388 * @uri_scheme: a string containing a URI scheme.
4390 * Gets the default application for launching applications
4391 * using this URI scheme for a particular GDesktopAppInfoLookup
4394 * The GDesktopAppInfoLookup interface and this function is used
4395 * to implement g_app_info_get_default_for_uri_scheme() backends
4396 * in a GIO module. There is no reason for applications to use it
4397 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
4399 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
4401 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
4404 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
4405 const char *uri_scheme)
4407 GDesktopAppInfoLookupIface *iface;
4409 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
4411 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
4413 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
4416 G_GNUC_END_IGNORE_DEPRECATIONS
4418 /* Misc getter APIs {{{2 */
4421 * g_desktop_app_info_get_startup_wm_class:
4422 * @info: a #GDesktopAppInfo that supports startup notify
4424 * Retrieves the StartupWMClass field from @info. This represents the
4425 * WM_CLASS property of the main window of the application, if launched
4428 * Returns: (transfer none): the startup WM class, or %NULL if none is set
4429 * in the desktop file.
4434 g_desktop_app_info_get_startup_wm_class (GDesktopAppInfo *info)
4436 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4438 return info->startup_wm_class;
4442 * g_desktop_app_info_get_string:
4443 * @info: a #GDesktopAppInfo
4444 * @key: the key to look up
4446 * Looks up a string value in the keyfile backing @info.
4448 * The @key is looked up in the "Desktop Entry" group.
4450 * Returns: a newly allocated string, or %NULL if the key
4456 g_desktop_app_info_get_string (GDesktopAppInfo *info,
4459 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4461 return g_key_file_get_string (info->keyfile,
4462 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4466 * g_desktop_app_info_get_boolean:
4467 * @info: a #GDesktopAppInfo
4468 * @key: the key to look up
4470 * Looks up a boolean value in the keyfile backing @info.
4472 * The @key is looked up in the "Desktop Entry" group.
4474 * Returns: the boolean value, or %FALSE if the key
4480 g_desktop_app_info_get_boolean (GDesktopAppInfo *info,
4483 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
4485 return g_key_file_get_boolean (info->keyfile,
4486 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4490 * g_desktop_app_info_has_key:
4491 * @info: a #GDesktopAppInfo
4492 * @key: the key to look up
4494 * Returns whether @key exists in the "Desktop Entry" group
4495 * of the keyfile backing @info.
4497 * Returns: %TRUE if the @key exists
4502 g_desktop_app_info_has_key (GDesktopAppInfo *info,
4505 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
4507 return g_key_file_has_key (info->keyfile,
4508 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4511 /* Desktop actions support {{{2 */
4514 * g_desktop_app_info_list_actions:
4515 * @info: a #GDesktopAppInfo
4517 * Returns the list of "additional application actions" supported on the
4518 * desktop file, as per the desktop file specification.
4520 * As per the specification, this is the list of actions that are
4521 * explicitly listed in the "Actions" key of the [Desktop Entry] group.
4523 * Returns: (array zero-terminated=1) (element-type utf8) (transfer none): a list of strings, always non-%NULL
4527 const gchar * const *
4528 g_desktop_app_info_list_actions (GDesktopAppInfo *info)
4530 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4532 return (const gchar **) info->actions;
4536 app_info_has_action (GDesktopAppInfo *info,
4537 const gchar *action_name)
4541 for (i = 0; info->actions[i]; i++)
4542 if (g_str_equal (info->actions[i], action_name))
4549 * g_desktop_app_info_get_action_name:
4550 * @info: a #GDesktopAppInfo
4551 * @action_name: the name of the action as from
4552 * g_desktop_app_info_list_actions()
4554 * Gets the user-visible display name of the "additional application
4555 * action" specified by @action_name.
4557 * This corresponds to the "Name" key within the keyfile group for the
4560 * Returns: (transfer full): the locale-specific action name
4565 g_desktop_app_info_get_action_name (GDesktopAppInfo *info,
4566 const gchar *action_name)
4571 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4572 g_return_val_if_fail (action_name != NULL, NULL);
4573 g_return_val_if_fail (app_info_has_action (info, action_name), NULL);
4575 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4576 result = g_key_file_get_locale_string (info->keyfile, group_name, "Name", NULL, NULL);
4577 g_free (group_name);
4579 /* The spec says that the Name field must be given.
4581 * If it's not, let's follow the behaviour of our get_name()
4582 * implementation above and never return %NULL.
4585 result = g_strdup (_("Unnamed"));
4591 * g_desktop_app_info_launch_action:
4592 * @info: a #GDesktopAppInfo
4593 * @action_name: the name of the action as from
4594 * g_desktop_app_info_list_actions()
4595 * @launch_context: (allow-none): a #GAppLaunchContext
4597 * Activates the named application action.
4599 * You may only call this function on action names that were
4600 * returned from g_desktop_app_info_list_actions().
4602 * Note that if the main entry of the desktop file indicates that the
4603 * application supports startup notification, and @launch_context is
4604 * non-%NULL, then startup notification will be used when activating the
4605 * action (and as such, invocation of the action on the receiving side
4606 * must signal the end of startup notification when it is completed).
4607 * This is the expected behaviour of applications declaring additional
4608 * actions, as per the desktop file specification.
4610 * As with g_app_info_launch() there is no way to detect failures that
4611 * occur while using this function.
4616 g_desktop_app_info_launch_action (GDesktopAppInfo *info,
4617 const gchar *action_name,
4618 GAppLaunchContext *launch_context)
4620 GDBusConnection *session_bus;
4622 g_return_if_fail (G_IS_DESKTOP_APP_INFO (info));
4623 g_return_if_fail (action_name != NULL);
4624 g_return_if_fail (app_info_has_action (info, action_name));
4626 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
4628 if (session_bus && info->app_id)
4632 object_path = object_path_from_appid (info->app_id);
4633 g_dbus_connection_call (session_bus, info->app_id, object_path,
4634 "org.freedesktop.Application", "ActivateAction",
4635 g_variant_new ("(sav@a{sv})", action_name, NULL,
4636 g_desktop_app_info_make_platform_data (info, NULL, launch_context)),
4637 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
4638 g_free (object_path);
4645 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4646 exec_line = g_key_file_get_string (info->keyfile, group_name, "Exec", NULL);
4647 g_free (group_name);
4650 g_desktop_app_info_launch_uris_with_spawn (info, session_bus, exec_line, NULL, launch_context,
4651 _SPAWN_FLAGS_DEFAULT, NULL, NULL, NULL, NULL, NULL);
4654 if (session_bus != NULL)
4656 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
4657 g_object_unref (session_bus);
4662 /* vim:set foldmethod=marker: */