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, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Author: Alexander Larsson <alexl@redhat.com>
22 * Ryan Lortie <desrt@desrt.ca>
33 #ifdef HAVE_CRT_EXTERNS_H
34 #include <crt_externs.h>
37 #include "gcontenttypeprivate.h"
38 #include "gdesktopappinfo.h"
40 #include "glib-unix.h"
44 #include "gthemedicon.h"
45 #include "gfileicon.h"
46 #include <glib/gstdio.h>
48 #include "giomodule-priv.h"
50 #include "glocaldirectorymonitor.h"
54 * SECTION:gdesktopappinfo
55 * @title: GDesktopAppInfo
56 * @short_description: Application information from desktop files
57 * @include: gio/gdesktopappinfo.h
59 * #GDesktopAppInfo is an implementation of #GAppInfo based on
62 * Note that <filename><gio/gdesktopappinfo.h></filename> belongs to
63 * the UNIX-specific GIO interfaces, thus you have to use the
64 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
67 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
68 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
69 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
70 #define MIME_CACHE_GROUP "MIME Cache"
71 #define GENERIC_NAME_KEY "GenericName"
72 #define FULL_NAME_KEY "X-GNOME-FullName"
73 #define KEYWORDS_KEY "Keywords"
74 #define STARTUP_WM_CLASS_KEY "StartupWMClass"
81 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
82 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type,
84 gboolean include_fallback,
85 char **explicit_default);
86 static void mime_info_cache_reload (const char *dir);
87 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
93 * Information about an installed application from a desktop file.
95 struct _GDesktopAppInfo
97 GObject parent_instance;
119 char *startup_wm_class;
126 guint startup_notify : 1;
131 UPDATE_MIME_NONE = 1 << 0,
132 UPDATE_MIME_SET_DEFAULT = 1 << 1,
133 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
134 UPDATE_MIME_REMOVE = 1 << 3,
135 UPDATE_MIME_SET_LAST_USED = 1 << 4,
138 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
139 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO, g_desktop_app_info_iface_init))
141 G_LOCK_DEFINE_STATIC (g_desktop_env);
142 static gchar *g_desktop_env = NULL;
144 /* DesktopFileDir implementation {{{1 */
149 GLocalDirectoryMonitor *monitor;
150 GHashTable *app_names;
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);
184 /* Internal utility functions {{{2 */
187 * desktop_file_dir_app_name_is_masked:
188 * @dir: a #DesktopFileDir
189 * @app_name: an application ID
191 * Checks if @app_name is masked for @dir.
193 * An application is masked if a similarly-named desktop file exists in
194 * a desktop file directory with higher precedence. Masked desktop
195 * files should be ignored.
198 desktop_file_dir_app_name_is_masked (DesktopFileDir *dir,
199 const gchar *app_name)
201 while (dir > desktop_file_dirs)
205 if (dir->app_names && g_hash_table_contains (dir->app_names, app_name))
213 * add_to_table_if_appropriate:
214 * @apps: a string to GDesktopAppInfo hash table
215 * @app_name: the name of the application
216 * @info: a #GDesktopAppInfo, or NULL
218 * If @info is non-%NULL and non-hidden, then add it to @apps, using
219 * @app_name as a key.
221 * If @info is non-%NULL then this function will consume the passed-in
225 add_to_table_if_appropriate (GHashTable *apps,
226 const gchar *app_name,
227 GDesktopAppInfo *info)
234 g_object_unref (info);
238 g_free (info->desktop_id);
239 info->desktop_id = g_strdup (app_name);
241 g_hash_table_insert (apps, g_strdup (info->desktop_id), info);
244 /* Support for unindexed DesktopFileDirs {{{2 */
246 get_apps_from_dir (GHashTable **apps,
250 const char *basename;
253 dir = g_dir_open (dirname, 0, NULL);
258 while ((basename = g_dir_read_name (dir)) != NULL)
262 filename = g_build_filename (dirname, basename, NULL);
264 if (g_str_has_suffix (basename, ".desktop"))
268 app_name = g_strconcat (prefix, basename, NULL);
271 *apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
273 g_hash_table_insert (*apps, app_name, g_strdup (filename));
275 else if (g_file_test (filename, G_FILE_TEST_IS_DIR))
279 subprefix = g_strconcat (prefix, basename, "-", NULL);
280 get_apps_from_dir (apps, filename, subprefix);
291 desktop_file_dir_unindexed_init (DesktopFileDir *dir)
293 get_apps_from_dir (&dir->app_names, dir->path, "");
296 static GDesktopAppInfo *
297 desktop_file_dir_unindexed_get_app (DesktopFileDir *dir,
298 const gchar *desktop_id)
300 const gchar *filename;
302 filename = g_hash_table_lookup (dir->app_names, desktop_id);
307 return g_desktop_app_info_new_from_filename (filename);
311 desktop_file_dir_unindexed_get_all (DesktopFileDir *dir,
318 if (dir->app_names == NULL)
321 g_hash_table_iter_init (&iter, dir->app_names);
322 while (g_hash_table_iter_next (&iter, &app_name, &filename))
324 if (desktop_file_dir_app_name_is_masked (dir, app_name))
327 add_to_table_if_appropriate (apps, app_name, g_desktop_app_info_new_from_filename (filename));
331 /* DesktopFileDir "API" {{{2 */
334 * desktop_file_dir_create:
335 * @array: the #GArray to add a new item to
336 * @data_dir: an XDG_DATA_DIR
338 * Creates a #DesktopFileDir for the corresponding @data_dir, adding it
342 desktop_file_dir_create (GArray *array,
343 const gchar *data_dir)
345 DesktopFileDir dir = { 0, };
347 dir.path = g_build_filename (data_dir, "applications", NULL);
349 g_array_append_val (array, dir);
353 * desktop_file_dir_reset:
354 * @dir: a #DesktopFileDir
356 * Cleans up @dir, releasing most resources that it was using.
359 desktop_file_dir_reset (DesktopFileDir *dir)
363 g_signal_handlers_disconnect_by_func (dir->monitor, desktop_file_dir_changed, dir);
364 g_object_unref (dir->monitor);
370 g_hash_table_unref (dir->app_names);
371 dir->app_names = NULL;
374 dir->is_setup = FALSE;
378 * desktop_file_dir_init:
379 * @dir: a #DesktopFileDir
381 * Does initial setup for @dir
383 * You should only call this if @dir is not already setup.
386 desktop_file_dir_init (DesktopFileDir *dir)
388 g_assert (!dir->is_setup);
390 g_assert (!dir->monitor);
391 dir->monitor = g_local_directory_monitor_new_in_worker (dir->path, G_FILE_MONITOR_NONE, NULL);
395 g_signal_connect (dir->monitor, "changed", G_CALLBACK (desktop_file_dir_changed), dir);
396 g_local_directory_monitor_start (dir->monitor);
399 desktop_file_dir_unindexed_init (dir);
401 dir->is_setup = TRUE;
405 * desktop_file_dir_get_app:
406 * @dir: a DesktopFileDir
407 * @desktop_id: the desktop ID to load
409 * Creates the #GDesktopAppInfo for the given @desktop_id if it exists
410 * within @dir, even if it is hidden.
412 * This function does not check if @desktop_id would be masked by a
413 * directory with higher precedence. The caller must do so.
415 static GDesktopAppInfo *
416 desktop_file_dir_get_app (DesktopFileDir *dir,
417 const gchar *desktop_id)
422 return desktop_file_dir_unindexed_get_app (dir, desktop_id);
426 * desktop_file_dir_get_all:
427 * @dir: a DesktopFileDir
428 * @apps: a #GHashTable<string, GDesktopAppInfo>
430 * Loads all desktop files in @dir and adds them to @apps, careful to
431 * ensure we don't add any files masked by a similarly-named file in a
432 * higher-precedence directory.
435 desktop_file_dir_get_all (DesktopFileDir *dir,
438 desktop_file_dir_unindexed_get_all (dir, apps);
441 /* Lock/unlock and global setup API {{{2 */
444 desktop_file_dirs_lock (void)
448 g_mutex_lock (&desktop_file_dir_lock);
450 if (desktop_file_dirs == NULL)
452 const char * const *data_dirs;
456 tmp = g_array_new (FALSE, FALSE, sizeof (DesktopFileDir));
458 /* Highest priority: the user's ~/.local/share/applications */
459 desktop_file_dir_create (tmp, g_get_user_data_dir ());
461 /* Following that, XDG_DATA_DIRS/applications, in order */
462 data_dirs = g_get_system_data_dirs ();
463 for (i = 0; data_dirs[i]; i++)
464 desktop_file_dir_create (tmp, data_dirs[i]);
466 desktop_file_dirs = (DesktopFileDir *) tmp->data;
467 n_desktop_file_dirs = tmp->len;
469 g_array_free (tmp, FALSE);
472 for (i = 0; i < n_desktop_file_dirs; i++)
473 if (!desktop_file_dirs[i].is_setup)
474 desktop_file_dir_init (&desktop_file_dirs[i]);
478 desktop_file_dirs_unlock (void)
480 g_mutex_unlock (&desktop_file_dir_lock);
484 desktop_file_dirs_refresh (void)
486 desktop_file_dirs_lock ();
487 desktop_file_dirs_unlock ();
491 desktop_file_dirs_invalidate_user (void)
493 g_mutex_lock (&desktop_file_dir_lock);
495 if (n_desktop_file_dirs)
496 desktop_file_dir_reset (&desktop_file_dirs[0]);
498 g_mutex_unlock (&desktop_file_dir_lock);
501 /* GDesktopAppInfo implementation {{{1 */
502 /* GObject implementation {{{2 */
504 g_desktop_app_info_finalize (GObject *object)
506 GDesktopAppInfo *info;
508 info = G_DESKTOP_APP_INFO (object);
510 g_free (info->desktop_id);
511 g_free (info->filename);
514 g_key_file_unref (info->keyfile);
517 g_free (info->generic_name);
518 g_free (info->fullname);
519 g_free (info->comment);
520 g_free (info->icon_name);
522 g_object_unref (info->icon);
523 g_strfreev (info->keywords);
524 g_strfreev (info->only_show_in);
525 g_strfreev (info->not_show_in);
526 g_free (info->try_exec);
528 g_free (info->binary);
530 g_free (info->categories);
531 g_free (info->startup_wm_class);
532 g_strfreev (info->mime_types);
533 g_free (info->app_id);
534 g_strfreev (info->actions);
536 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
540 g_desktop_app_info_set_property (GObject *object,
545 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
550 self->filename = g_value_dup_string (value);
554 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
560 g_desktop_app_info_get_property (GObject *object,
565 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
570 g_value_set_string (value, self->filename);
573 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
579 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
581 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
583 gobject_class->get_property = g_desktop_app_info_get_property;
584 gobject_class->set_property = g_desktop_app_info_set_property;
585 gobject_class->finalize = g_desktop_app_info_finalize;
588 * GDesktopAppInfo:filename:
590 * The origin filename of this #GDesktopAppInfo
592 g_object_class_install_property (gobject_class,
594 g_param_spec_string ("filename", "Filename", "", NULL,
595 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
599 g_desktop_app_info_init (GDesktopAppInfo *local)
603 /* Construction... {{{2 */
607 * @exec: an exec line
609 * Returns the first word in an exec line (ie: the binary name).
611 * If @exec is " progname --foo %F" then returns "progname".
614 binary_from_exec (const char *exec)
616 const char *p, *start;
622 while (*p != ' ' && *p != 0)
625 return g_strndup (start, p - start);
629 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
636 gboolean bus_activatable;
638 start_group = g_key_file_get_start_group (key_file);
639 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
641 g_free (start_group);
644 g_free (start_group);
646 type = g_key_file_get_string (key_file,
647 G_KEY_FILE_DESKTOP_GROUP,
648 G_KEY_FILE_DESKTOP_KEY_TYPE,
650 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
657 try_exec = g_key_file_get_string (key_file,
658 G_KEY_FILE_DESKTOP_GROUP,
659 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
661 if (try_exec && try_exec[0] != '\0')
664 t = g_find_program_in_path (try_exec);
673 exec = g_key_file_get_string (key_file,
674 G_KEY_FILE_DESKTOP_GROUP,
675 G_KEY_FILE_DESKTOP_KEY_EXEC,
677 if (exec && exec[0] != '\0')
681 if (!g_shell_parse_argv (exec, &argc, &argv, NULL))
690 t = g_find_program_in_path (argv[0]);
703 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
704 info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
705 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
706 info->keywords = g_key_file_get_locale_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, KEYWORDS_KEY, NULL, NULL, NULL);
707 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
708 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
709 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
710 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);
711 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);
712 info->try_exec = try_exec;
714 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
715 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
716 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
717 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
718 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
719 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
720 info->startup_wm_class = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, STARTUP_WM_CLASS_KEY, NULL);
721 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);
722 bus_activatable = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE, NULL);
723 info->actions = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ACTIONS, NULL, NULL);
725 /* Remove the special-case: no Actions= key just means 0 extra actions */
726 if (info->actions == NULL)
727 info->actions = g_new0 (gchar *, 0 + 1);
732 if (g_path_is_absolute (info->icon_name))
736 file = g_file_new_for_path (info->icon_name);
737 info->icon = g_file_icon_new (file);
738 g_object_unref (file);
744 /* Work around a common mistake in desktop files */
745 if ((p = strrchr (info->icon_name, '.')) != NULL &&
746 (strcmp (p, ".png") == 0 ||
747 strcmp (p, ".xpm") == 0 ||
748 strcmp (p, ".svg") == 0))
751 info->icon = g_themed_icon_new (info->icon_name);
756 info->binary = binary_from_exec (info->exec);
758 if (info->path && info->path[0] == '\0')
764 /* Can only be DBusActivatable if we know the filename, which means
765 * that this won't work for the load-from-keyfile case.
767 if (bus_activatable && info->filename)
772 basename = g_path_get_basename (info->filename);
773 last_dot = strrchr (basename, '.');
775 if (last_dot && g_str_equal (last_dot, ".desktop"))
779 if (g_dbus_is_interface_name (basename))
780 info->app_id = g_strdup (basename);
786 info->keyfile = g_key_file_ref (key_file);
792 g_desktop_app_info_load_file (GDesktopAppInfo *self)
795 gboolean retval = FALSE;
797 g_return_val_if_fail (self->filename != NULL, FALSE);
799 self->desktop_id = g_path_get_basename (self->filename);
801 key_file = g_key_file_new ();
803 if (g_key_file_load_from_file (key_file, self->filename, G_KEY_FILE_NONE, NULL))
804 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
806 g_key_file_unref (key_file);
811 * g_desktop_app_info_new_from_keyfile:
812 * @key_file: an opened #GKeyFile
814 * Creates a new #GDesktopAppInfo.
816 * Returns: a new #GDesktopAppInfo or %NULL on error.
821 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
823 GDesktopAppInfo *info;
825 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
826 info->filename = NULL;
827 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
829 g_object_unref (info);
836 * g_desktop_app_info_new_from_filename:
837 * @filename: the path of a desktop file, in the GLib filename encoding
839 * Creates a new #GDesktopAppInfo.
841 * Returns: a new #GDesktopAppInfo or %NULL on error.
844 g_desktop_app_info_new_from_filename (const char *filename)
846 GDesktopAppInfo *info = NULL;
848 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
849 if (!g_desktop_app_info_load_file (info))
851 g_object_unref (info);
858 * g_desktop_app_info_new:
859 * @desktop_id: the desktop file id
861 * Creates a new #GDesktopAppInfo based on a desktop file id.
863 * A desktop file id is the basename of the desktop file, including the
864 * .desktop extension. GIO is looking for a desktop file with this name
865 * in the <filename>applications</filename> subdirectories of the XDG data
866 * directories (i.e. the directories specified in the
867 * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment
868 * variables). GIO also supports the prefix-to-subdirectory mapping that is
869 * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink>
870 * (i.e. a desktop id of kde-foo.desktop will match
871 * <filename>/usr/share/applications/kde/foo.desktop</filename>).
873 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
876 g_desktop_app_info_new (const char *desktop_id)
878 GDesktopAppInfo *appinfo = NULL;
881 desktop_file_dirs_lock ();
883 for (i = 0; i < n_desktop_file_dirs; i++)
885 appinfo = desktop_file_dir_get_app (&desktop_file_dirs[i], desktop_id);
891 desktop_file_dirs_unlock ();
896 g_free (appinfo->desktop_id);
897 appinfo->desktop_id = g_strdup (desktop_id);
899 if (g_desktop_app_info_get_is_hidden (appinfo))
901 g_object_unref (appinfo);
909 g_desktop_app_info_dup (GAppInfo *appinfo)
911 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
912 GDesktopAppInfo *new_info;
914 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
916 new_info->filename = g_strdup (info->filename);
917 new_info->desktop_id = g_strdup (info->desktop_id);
920 new_info->keyfile = g_key_file_ref (info->keyfile);
922 new_info->name = g_strdup (info->name);
923 new_info->generic_name = g_strdup (info->generic_name);
924 new_info->fullname = g_strdup (info->fullname);
925 new_info->keywords = g_strdupv (info->keywords);
926 new_info->comment = g_strdup (info->comment);
927 new_info->nodisplay = info->nodisplay;
928 new_info->icon_name = g_strdup (info->icon_name);
930 new_info->icon = g_object_ref (info->icon);
931 new_info->only_show_in = g_strdupv (info->only_show_in);
932 new_info->not_show_in = g_strdupv (info->not_show_in);
933 new_info->try_exec = g_strdup (info->try_exec);
934 new_info->exec = g_strdup (info->exec);
935 new_info->binary = g_strdup (info->binary);
936 new_info->path = g_strdup (info->path);
937 new_info->app_id = g_strdup (info->app_id);
938 new_info->hidden = info->hidden;
939 new_info->terminal = info->terminal;
940 new_info->startup_notify = info->startup_notify;
942 return G_APP_INFO (new_info);
945 /* GAppInfo interface implementation functions {{{2 */
948 g_desktop_app_info_equal (GAppInfo *appinfo1,
951 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
952 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
954 if (info1->desktop_id == NULL ||
955 info2->desktop_id == NULL)
956 return info1 == info2;
958 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
962 g_desktop_app_info_get_id (GAppInfo *appinfo)
964 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
966 return info->desktop_id;
970 g_desktop_app_info_get_name (GAppInfo *appinfo)
972 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
974 if (info->name == NULL)
980 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
982 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
984 if (info->fullname == NULL)
985 return g_desktop_app_info_get_name (appinfo);
986 return info->fullname;
990 * g_desktop_app_info_get_is_hidden:
991 * @info: a #GDesktopAppInfo.
993 * A desktop file is hidden if the Hidden key in it is
996 * Returns: %TRUE if hidden, %FALSE otherwise.
999 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
1001 return info->hidden;
1005 * g_desktop_app_info_get_filename:
1006 * @info: a #GDesktopAppInfo
1008 * When @info was created from a known filename, return it. In some
1009 * situations such as the #GDesktopAppInfo returned from
1010 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
1012 * Returns: The full path to the file for @info, or %NULL if not known.
1016 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
1018 return info->filename;
1022 g_desktop_app_info_get_description (GAppInfo *appinfo)
1024 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1026 return info->comment;
1030 g_desktop_app_info_get_executable (GAppInfo *appinfo)
1032 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1034 return info->binary;
1038 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
1040 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1046 g_desktop_app_info_get_icon (GAppInfo *appinfo)
1048 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1054 * g_desktop_app_info_get_categories:
1055 * @info: a #GDesktopAppInfo
1057 * Gets the categories from the desktop file.
1059 * Returns: The unparsed Categories key from the desktop file;
1060 * i.e. no attempt is made to split it by ';' or validate it.
1063 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
1065 return info->categories;
1069 * g_desktop_app_info_get_keywords:
1070 * @info: a #GDesktopAppInfo
1072 * Gets the keywords from the desktop file.
1074 * Returns: (transfer none): The value of the Keywords key
1078 const char * const *
1079 g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
1081 return (const char * const *)info->keywords;
1085 * g_desktop_app_info_get_generic_name:
1086 * @info: a #GDesktopAppInfo
1088 * Gets the generic name from the destkop file.
1090 * Returns: The value of the GenericName key
1093 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
1095 return info->generic_name;
1099 * g_desktop_app_info_get_nodisplay:
1100 * @info: a #GDesktopAppInfo
1102 * Gets the value of the NoDisplay key, which helps determine if the
1103 * application info should be shown in menus. See
1104 * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
1106 * Returns: The value of the NoDisplay key
1111 g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
1113 return info->nodisplay;
1117 * g_desktop_app_info_get_show_in:
1118 * @info: a #GDesktopAppInfo
1119 * @desktop_env: a string specifying a desktop name
1121 * Checks if the application info should be shown in menus that list available
1122 * applications for a specific name of the desktop, based on the
1123 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys.
1125 * If @desktop_env is %NULL, then the name of the desktop set with
1126 * g_desktop_app_info_set_desktop_env() is used.
1128 * Note that g_app_info_should_show() for @info will include this check (with
1129 * %NULL for @desktop_env) as well as additional checks.
1131 * Returns: %TRUE if the @info should be shown in @desktop_env according to the
1132 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys, %FALSE
1138 g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
1139 const gchar *desktop_env)
1144 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
1147 G_LOCK (g_desktop_env);
1148 desktop_env = g_desktop_env;
1149 G_UNLOCK (g_desktop_env);
1152 if (info->only_show_in)
1154 if (desktop_env == NULL)
1158 for (i = 0; info->only_show_in[i] != NULL; i++)
1160 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1170 if (info->not_show_in && desktop_env)
1172 for (i = 0; info->not_show_in[i] != NULL; i++)
1174 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1182 /* Launching... {{{2 */
1185 expand_macro_single (char macro, char *uri)
1188 char *result = NULL;
1192 file = g_file_new_for_uri (uri);
1198 result = g_shell_quote (uri);
1202 path = g_file_get_path (file);
1204 result = g_shell_quote (path);
1208 path = g_file_get_path (file);
1211 name = g_path_get_dirname (path);
1212 result = g_shell_quote (name);
1218 path = g_file_get_path (file);
1221 name = g_path_get_basename (path);
1222 result = g_shell_quote (name);
1228 g_object_unref (file);
1235 expand_macro (char macro,
1237 GDesktopAppInfo *info,
1240 GList *uris = *uri_list;
1242 gboolean force_file_uri;
1243 char force_file_uri_macro;
1246 g_return_if_fail (exec != NULL);
1248 /* On %u and %U, pass POSIX file path pointing to the URI via
1249 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
1250 * running or the URI doesn't have a POSIX file path via FUSE
1251 * we'll just pass the URI.
1253 force_file_uri_macro = macro;
1254 force_file_uri = FALSE;
1260 force_file_uri_macro = 'f';
1261 force_file_uri = TRUE;
1264 force_file_uri_macro = 'F';
1265 force_file_uri = TRUE;
1281 if (!force_file_uri ||
1282 /* Pass URI if it contains an anchor */
1283 strchr (uri, '#') != NULL)
1285 expanded = expand_macro_single (macro, uri);
1289 expanded = expand_macro_single (force_file_uri_macro, uri);
1290 if (expanded == NULL)
1291 expanded = expand_macro_single (macro, uri);
1296 g_string_append (exec, expanded);
1312 if (!force_file_uri ||
1313 /* Pass URI if it contains an anchor */
1314 strchr (uri, '#') != NULL)
1316 expanded = expand_macro_single (macro, uri);
1320 expanded = expand_macro_single (force_file_uri_macro, uri);
1321 if (expanded == NULL)
1322 expanded = expand_macro_single (macro, uri);
1327 g_string_append (exec, expanded);
1333 if (uris != NULL && expanded)
1334 g_string_append_c (exec, ' ');
1340 if (info->icon_name)
1342 g_string_append (exec, "--icon ");
1343 expanded = g_shell_quote (info->icon_name);
1344 g_string_append (exec, expanded);
1352 expanded = g_shell_quote (info->name);
1353 g_string_append (exec, expanded);
1361 expanded = g_shell_quote (info->filename);
1362 g_string_append (exec, expanded);
1367 case 'm': /* deprecated */
1371 g_string_append_c (exec, '%');
1379 expand_application_parameters (GDesktopAppInfo *info,
1380 const gchar *exec_line,
1386 GList *uri_list = *uris;
1387 const char *p = exec_line;
1388 GString *expanded_exec;
1391 if (exec_line == NULL)
1393 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1394 _("Desktop file didn't specify Exec field"));
1398 expanded_exec = g_string_new (NULL);
1402 if (p[0] == '%' && p[1] != '\0')
1404 expand_macro (p[1], expanded_exec, info, uris);
1408 g_string_append_c (expanded_exec, *p);
1413 /* No file substitutions */
1414 if (uri_list == *uris && uri_list != NULL)
1416 /* If there is no macro default to %f. This is also what KDE does */
1417 g_string_append_c (expanded_exec, ' ');
1418 expand_macro ('f', expanded_exec, info, uris);
1421 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
1422 g_string_free (expanded_exec, TRUE);
1427 prepend_terminal_to_vector (int *argc,
1434 char **term_argv = NULL;
1439 g_return_val_if_fail (argc != NULL, FALSE);
1440 g_return_val_if_fail (argv != NULL, FALSE);
1448 /* compute size if not given */
1451 for (i = 0; the_argv[i] != NULL; i++)
1457 term_argv = g_new0 (char *, 3);
1459 check = g_find_program_in_path ("gnome-terminal");
1462 term_argv[0] = check;
1463 /* Note that gnome-terminal takes -x and
1464 * as -e in gnome-terminal is broken we use that. */
1465 term_argv[1] = g_strdup ("-x");
1470 check = g_find_program_in_path ("nxterm");
1472 check = g_find_program_in_path ("color-xterm");
1474 check = g_find_program_in_path ("rxvt");
1476 check = g_find_program_in_path ("xterm");
1478 check = g_find_program_in_path ("dtterm");
1481 check = g_strdup ("xterm");
1482 g_warning ("couldn't find a terminal, falling back to xterm");
1484 term_argv[0] = check;
1485 term_argv[1] = g_strdup ("-e");
1488 real_argc = term_argc + *argc;
1489 real_argv = g_new (char *, real_argc + 1);
1491 for (i = 0; i < term_argc; i++)
1492 real_argv[i] = term_argv[i];
1494 for (j = 0; j < *argc; j++, i++)
1495 real_argv[i] = (char *)the_argv[j];
1497 real_argv[i] = NULL;
1503 /* we use g_free here as we sucked all the inner strings
1504 * out from it into real_argv */
1509 #endif /* G_OS_WIN32 */
1513 create_files_for_uris (GList *uris)
1520 for (iter = uris; iter; iter = iter->next)
1522 GFile *file = g_file_new_for_uri ((char *)iter->data);
1523 res = g_list_prepend (res, file);
1526 return g_list_reverse (res);
1531 GSpawnChildSetupFunc user_setup;
1532 gpointer user_setup_data;
1538 child_setup (gpointer user_data)
1540 ChildSetupData *data = user_data;
1542 if (data->pid_envvar)
1544 pid_t pid = getpid ();
1548 /* Write the pid into the space already reserved for it in the
1549 * environment array. We can't use sprintf because it might
1550 * malloc, so we do it by hand. It's simplest to write the pid
1551 * out backwards first, then copy it over.
1553 for (i = 0; pid; i++, pid /= 10)
1554 buf[i] = (pid % 10) + '0';
1555 for (i--; i >= 0; i--)
1556 *(data->pid_envvar++) = buf[i];
1557 *data->pid_envvar = '\0';
1560 if (data->user_setup)
1561 data->user_setup (data->user_setup_data);
1565 notify_desktop_launch (GDBusConnection *session_bus,
1566 GDesktopAppInfo *info,
1568 const char *display,
1573 GVariantBuilder uri_variant;
1574 GVariantBuilder extras_variant;
1576 const char *desktop_file_id;
1577 const char *gio_desktop_file;
1579 if (session_bus == NULL)
1582 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
1583 for (iter = uris; iter; iter = iter->next)
1584 g_variant_builder_add (&uri_variant, "s", iter->data);
1586 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
1587 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
1588 g_variant_builder_add (&extras_variant, "{sv}",
1592 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
1593 if (gio_desktop_file != NULL)
1594 g_variant_builder_add (&extras_variant, "{sv}",
1595 "origin-desktop-file",
1596 g_variant_new_bytestring (gio_desktop_file));
1597 if (g_get_prgname () != NULL)
1598 g_variant_builder_add (&extras_variant, "{sv}",
1600 g_variant_new_bytestring (g_get_prgname ()));
1601 g_variant_builder_add (&extras_variant, "{sv}",
1604 (gint64)getpid ()));
1607 desktop_file_id = info->filename;
1608 else if (info->desktop_id)
1609 desktop_file_id = info->desktop_id;
1611 desktop_file_id = "";
1613 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
1614 "org.gtk.gio.DesktopAppInfo",
1616 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
1617 g_variant_new_bytestring (desktop_file_id),
1618 display ? display : "",
1622 g_dbus_connection_send_message (session_bus,
1626 g_object_unref (msg);
1629 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
1632 g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo *info,
1633 GDBusConnection *session_bus,
1634 const gchar *exec_line,
1636 GAppLaunchContext *launch_context,
1637 GSpawnFlags spawn_flags,
1638 GSpawnChildSetupFunc user_setup,
1639 gpointer user_setup_data,
1640 GDesktopAppLaunchCallback pid_callback,
1641 gpointer pid_callback_data,
1644 gboolean completed = FALSE;
1646 char **argv, **envp;
1648 ChildSetupData data;
1650 g_return_val_if_fail (info != NULL, FALSE);
1655 envp = g_app_launch_context_get_environment (launch_context);
1657 envp = g_get_environ ();
1662 GList *launched_uris;
1664 char *display, *sn_id;
1667 if (!expand_application_parameters (info, exec_line, &uris, &argc, &argv, error))
1670 /* Get the subset of URIs we're launching with this process */
1671 launched_uris = NULL;
1672 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
1673 launched_uris = g_list_prepend (launched_uris, iter->data);
1674 launched_uris = g_list_reverse (launched_uris);
1676 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
1678 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1679 _("Unable to find terminal required for application"));
1683 data.user_setup = user_setup;
1684 data.user_setup_data = user_setup_data;
1688 envp = g_environ_setenv (envp,
1689 "GIO_LAUNCHED_DESKTOP_FILE",
1692 envp = g_environ_setenv (envp,
1693 "GIO_LAUNCHED_DESKTOP_FILE_PID",
1694 "XXXXXXXXXXXXXXXXXXXX", /* filled in child_setup */
1696 data.pid_envvar = (char *)g_environ_getenv (envp, "GIO_LAUNCHED_DESKTOP_FILE_PID");
1700 data.pid_envvar = NULL;
1707 GList *launched_files = create_files_for_uris (launched_uris);
1709 display = g_app_launch_context_get_display (launch_context,
1713 envp = g_environ_setenv (envp, "DISPLAY", display, TRUE);
1715 if (info->startup_notify)
1717 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
1720 envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
1723 g_list_free_full (launched_files, g_object_unref);
1726 if (!g_spawn_async (info->path,
1736 g_app_launch_context_launch_failed (launch_context, sn_id);
1740 g_list_free (launched_uris);
1745 if (pid_callback != NULL)
1746 pid_callback (info, pid, pid_callback_data);
1748 if (launch_context != NULL)
1750 GVariantBuilder builder;
1751 GVariant *platform_data;
1753 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
1754 g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (pid));
1756 g_variant_builder_add (&builder, "{sv}", "startup-notification-id", g_variant_new_string (sn_id));
1757 platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
1758 g_signal_emit_by_name (launch_context, "launched", info, platform_data);
1759 g_variant_unref (platform_data);
1762 notify_desktop_launch (session_bus,
1771 g_list_free (launched_uris);
1776 while (uris != NULL);
1788 object_path_from_appid (const gchar *app_id)
1793 n = strlen (app_id);
1794 path = g_malloc (n + 2);
1798 for (i = 0; i < n; i++)
1799 if (app_id[i] != '.')
1800 path[i + 1] = app_id[i];
1810 g_desktop_app_info_make_platform_data (GDesktopAppInfo *info,
1812 GAppLaunchContext *launch_context)
1814 GVariantBuilder builder;
1816 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
1820 GList *launched_files = create_files_for_uris (uris);
1822 if (info->startup_notify)
1826 sn_id = g_app_launch_context_get_startup_notify_id (launch_context, G_APP_INFO (info), launched_files);
1827 g_variant_builder_add (&builder, "{sv}", "desktop-startup-id", g_variant_new_take_string (sn_id));
1830 g_list_free_full (launched_files, g_object_unref);
1833 return g_variant_builder_end (&builder);
1837 g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo *info,
1838 GDBusConnection *session_bus,
1840 GAppLaunchContext *launch_context)
1842 GVariantBuilder builder;
1845 g_return_val_if_fail (info != NULL, FALSE);
1847 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
1853 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
1854 for (iter = uris; iter; iter = iter->next)
1855 g_variant_builder_add (&builder, "s", iter->data);
1856 g_variant_builder_close (&builder);
1859 g_variant_builder_add_value (&builder, g_desktop_app_info_make_platform_data (info, uris, launch_context));
1861 /* This is non-blocking API. Similar to launching via fork()/exec()
1862 * we don't wait around to see if the program crashed during startup.
1863 * This is what startup-notification's job is...
1865 object_path = object_path_from_appid (info->app_id);
1866 g_dbus_connection_call (session_bus, info->app_id, object_path, "org.freedesktop.Application",
1867 uris ? "Open" : "Activate", g_variant_builder_end (&builder),
1868 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
1869 g_free (object_path);
1875 g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
1877 GAppLaunchContext *launch_context,
1878 GSpawnFlags spawn_flags,
1879 GSpawnChildSetupFunc user_setup,
1880 gpointer user_setup_data,
1881 GDesktopAppLaunchCallback pid_callback,
1882 gpointer pid_callback_data,
1885 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1886 GDBusConnection *session_bus;
1887 gboolean success = TRUE;
1889 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
1891 if (session_bus && info->app_id)
1892 g_desktop_app_info_launch_uris_with_dbus (info, session_bus, uris, launch_context);
1894 success = g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec, uris, launch_context,
1895 spawn_flags, user_setup, user_setup_data,
1896 pid_callback, pid_callback_data, error);
1898 if (session_bus != NULL)
1900 /* This asynchronous flush holds a reference until it completes,
1901 * which ensures that the following unref won't immediately kill
1902 * the connection if we were the initial owner.
1904 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
1905 g_object_unref (session_bus);
1912 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
1914 GAppLaunchContext *launch_context,
1917 return g_desktop_app_info_launch_uris_internal (appinfo, uris,
1919 _SPAWN_FLAGS_DEFAULT,
1920 NULL, NULL, NULL, NULL,
1925 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1927 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1929 return info->exec &&
1930 ((strstr (info->exec, "%u") != NULL) ||
1931 (strstr (info->exec, "%U") != NULL));
1935 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1937 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1939 return info->exec &&
1940 ((strstr (info->exec, "%f") != NULL) ||
1941 (strstr (info->exec, "%F") != NULL));
1945 g_desktop_app_info_launch (GAppInfo *appinfo,
1947 GAppLaunchContext *launch_context,
1957 uri = g_file_get_uri (files->data);
1958 uris = g_list_prepend (uris, uri);
1959 files = files->next;
1962 uris = g_list_reverse (uris);
1964 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1966 g_list_free_full (uris, g_free);
1972 * g_desktop_app_info_launch_uris_as_manager:
1973 * @appinfo: a #GDesktopAppInfo
1974 * @uris: (element-type utf8): List of URIs
1975 * @launch_context: a #GAppLaunchContext
1976 * @spawn_flags: #GSpawnFlags, used for each process
1977 * @user_setup: (scope call): a #GSpawnChildSetupFunc, used once for
1979 * @user_setup_data: (closure user_setup): User data for @user_setup
1980 * @pid_callback: (scope call): Callback for child processes
1981 * @pid_callback_data: (closure pid_callback): User data for @callback
1982 * @error: return location for a #GError, or %NULL
1984 * This function performs the equivalent of g_app_info_launch_uris(),
1985 * but is intended primarily for operating system components that
1986 * launch applications. Ordinary applications should use
1987 * g_app_info_launch_uris().
1989 * If the application is launched via traditional UNIX fork()/exec()
1990 * then @spawn_flags, @user_setup and @user_setup_data are used for the
1991 * call to g_spawn_async(). Additionally, @pid_callback (with
1992 * @pid_callback_data) will be called to inform about the PID of the
1995 * If application launching occurs via some other mechanism (eg: D-Bus
1996 * activation) then @spawn_flags, @user_setup, @user_setup_data,
1997 * @pid_callback and @pid_callback_data are ignored.
1999 * Returns: %TRUE on successful launch, %FALSE otherwise.
2002 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
2004 GAppLaunchContext *launch_context,
2005 GSpawnFlags spawn_flags,
2006 GSpawnChildSetupFunc user_setup,
2007 gpointer user_setup_data,
2008 GDesktopAppLaunchCallback pid_callback,
2009 gpointer pid_callback_data,
2012 return g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
2023 /* OnlyShowIn API support {{{2 */
2026 * g_desktop_app_info_set_desktop_env:
2027 * @desktop_env: a string specifying what desktop this is
2029 * Sets the name of the desktop that the application is running in.
2030 * This is used by g_app_info_should_show() and
2031 * g_desktop_app_info_get_show_in() to evaluate the
2032 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
2033 * desktop entry fields.
2035 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
2036 * Menu specification</ulink> recognizes the following:
2038 * <member>GNOME</member>
2039 * <member>KDE</member>
2040 * <member>ROX</member>
2041 * <member>XFCE</member>
2042 * <member>LXDE</member>
2043 * <member>Unity</member>
2044 * <member>Old</member>
2047 * Should be called only once; subsequent calls are ignored.
2050 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
2052 G_LOCK (g_desktop_env);
2054 g_desktop_env = g_strdup (desktop_env);
2055 G_UNLOCK (g_desktop_env);
2059 g_desktop_app_info_should_show (GAppInfo *appinfo)
2061 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2063 if (info->nodisplay)
2066 return g_desktop_app_info_get_show_in (info, NULL);
2069 /* mime types/default apps support {{{2 */
2077 ensure_dir (DirType type,
2080 char *path, *display_name;
2083 if (type == APP_DIR)
2084 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
2086 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
2089 if (g_mkdir_with_parents (path, 0700) == 0)
2093 display_name = g_filename_display_name (path);
2094 if (type == APP_DIR)
2095 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
2096 _("Can't create user application configuration folder %s: %s"),
2097 display_name, g_strerror (errsv));
2099 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
2100 _("Can't create user MIME configuration folder %s: %s"),
2101 display_name, g_strerror (errsv));
2103 g_free (display_name);
2110 update_mimeapps_list (const char *desktop_id,
2111 const char *content_type,
2112 UpdateMimeFlags flags,
2115 char *dirname, *filename, *string;
2117 gboolean load_succeeded, res;
2118 char **old_list, **list;
2119 gsize length, data_size;
2122 char **content_types;
2124 /* Don't add both at start and end */
2125 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
2126 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
2128 dirname = ensure_dir (APP_DIR, error);
2132 filename = g_build_filename (dirname, "mimeapps.list", NULL);
2135 key_file = g_key_file_new ();
2136 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
2137 if (!load_succeeded ||
2138 (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) &&
2139 !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) &&
2140 !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP)))
2142 g_key_file_free (key_file);
2143 key_file = g_key_file_new ();
2148 content_types = g_new (char *, 2);
2149 content_types[0] = g_strdup (content_type);
2150 content_types[1] = NULL;
2154 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
2157 for (k = 0; content_types && content_types[k]; k++)
2159 /* set as default, if requested so */
2160 string = g_key_file_get_string (key_file,
2161 DEFAULT_APPLICATIONS_GROUP,
2165 if (g_strcmp0 (string, desktop_id) != 0 &&
2166 (flags & UPDATE_MIME_SET_DEFAULT))
2169 string = g_strdup (desktop_id);
2171 /* add in the non-default list too, if it's not already there */
2172 flags |= UPDATE_MIME_SET_NON_DEFAULT;
2175 if (string == NULL || desktop_id == NULL)
2176 g_key_file_remove_key (key_file,
2177 DEFAULT_APPLICATIONS_GROUP,
2181 g_key_file_set_string (key_file,
2182 DEFAULT_APPLICATIONS_GROUP,
2191 /* reuse the list from above */
2195 g_strfreev (content_types);
2196 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
2199 for (k = 0; content_types && content_types[k]; k++)
2201 /* Add to the right place in the list */
2204 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
2205 content_types[k], &length, NULL);
2207 list = g_new (char *, 1 + length + 1);
2211 /* if we're adding a last-used hint, just put the application in front of the list */
2212 if (flags & UPDATE_MIME_SET_LAST_USED)
2214 /* avoid adding this again as non-default later */
2215 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
2216 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
2218 list[i++] = g_strdup (desktop_id);
2223 for (j = 0; old_list[j] != NULL; j++)
2225 if (g_strcmp0 (old_list[j], desktop_id) != 0)
2227 /* rewrite other entries if they're different from the new one */
2228 list[i++] = g_strdup (old_list[j]);
2230 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
2232 /* we encountered an old entry which is equal to the one we're adding as non-default,
2233 * don't change its position in the list.
2235 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
2236 list[i++] = g_strdup (old_list[j]);
2241 /* add it at the end of the list */
2242 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
2243 list[i++] = g_strdup (desktop_id);
2247 g_strfreev (old_list);
2249 if (list[0] == NULL || desktop_id == NULL)
2250 g_key_file_remove_key (key_file,
2251 ADDED_ASSOCIATIONS_GROUP,
2255 g_key_file_set_string_list (key_file,
2256 ADDED_ASSOCIATIONS_GROUP,
2258 (const char * const *)list, i);
2265 /* reuse the list from above */
2269 g_strfreev (content_types);
2270 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
2273 for (k = 0; content_types && content_types[k]; k++)
2275 /* Remove from removed associations group (unless remove) */
2278 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
2279 content_types[k], &length, NULL);
2281 list = g_new (char *, 1 + length + 1);
2284 if (flags & UPDATE_MIME_REMOVE)
2285 list[i++] = g_strdup (desktop_id);
2288 for (j = 0; old_list[j] != NULL; j++)
2290 if (g_strcmp0 (old_list[j], desktop_id) != 0)
2291 list[i++] = g_strdup (old_list[j]);
2296 g_strfreev (old_list);
2298 if (list[0] == NULL || desktop_id == NULL)
2299 g_key_file_remove_key (key_file,
2300 REMOVED_ASSOCIATIONS_GROUP,
2304 g_key_file_set_string_list (key_file,
2305 REMOVED_ASSOCIATIONS_GROUP,
2307 (const char * const *)list, i);
2312 g_strfreev (content_types);
2314 data = g_key_file_to_data (key_file, &data_size, error);
2315 g_key_file_free (key_file);
2317 res = g_file_set_contents (filename, data, data_size, error);
2319 mime_info_cache_reload (NULL);
2328 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
2329 const char *content_type,
2332 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2334 if (!g_desktop_app_info_ensure_saved (info, error))
2337 if (!info->desktop_id)
2339 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2340 _("Application information lacks an identifier"));
2344 /* both add support for the content type and set as last used */
2345 return update_mimeapps_list (info->desktop_id, content_type,
2346 UPDATE_MIME_SET_NON_DEFAULT |
2347 UPDATE_MIME_SET_LAST_USED,
2352 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
2353 const char *content_type,
2356 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2358 if (!g_desktop_app_info_ensure_saved (info, error))
2361 if (!info->desktop_id)
2363 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2364 _("Application information lacks an identifier"));
2368 return update_mimeapps_list (info->desktop_id, content_type,
2369 UPDATE_MIME_SET_DEFAULT,
2374 update_program_done (GPid pid,
2378 /* Did the application exit correctly */
2379 if (g_spawn_check_exit_status (status, NULL))
2381 /* Here we could clean out any caches in use */
2386 run_update_command (char *command,
2395 GError *error = NULL;
2398 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
2400 if (g_spawn_async ("/", argv,
2402 G_SPAWN_SEARCH_PATH |
2403 G_SPAWN_STDOUT_TO_DEV_NULL |
2404 G_SPAWN_STDERR_TO_DEV_NULL |
2405 G_SPAWN_DO_NOT_REAP_CHILD,
2406 NULL, NULL, /* No setup function */
2409 g_child_watch_add (pid, update_program_done, NULL);
2412 /* If we get an error at this point, it's quite likely the user doesn't
2413 * have an installed copy of either 'update-mime-database' or
2414 * 'update-desktop-database'. I don't think we want to popup an error
2415 * dialog at this point, so we just do a g_warning to give the user a
2416 * chance of debugging it.
2418 g_warning ("%s", error->message);
2425 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
2426 const char *extension,
2429 char *filename, *basename, *mimetype;
2433 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
2436 dirname = ensure_dir (MIMETYPE_DIR, error);
2440 basename = g_strdup_printf ("user-extension-%s.xml", extension);
2441 filename = g_build_filename (dirname, basename, NULL);
2445 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
2447 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
2452 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
2453 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
2454 " <mime-type type=\"%s\">\n"
2455 " <comment>%s document</comment>\n"
2456 " <glob pattern=\"*.%s\"/>\n"
2458 "</mime-info>\n", mimetype, extension, extension);
2460 g_file_set_contents (filename, contents, -1, NULL);
2463 run_update_command ("update-mime-database", "mime");
2467 res = g_desktop_app_info_set_as_default_for_type (appinfo,
2477 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
2478 const char *content_type,
2481 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2483 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
2486 return update_mimeapps_list (info->desktop_id, content_type,
2487 UPDATE_MIME_SET_NON_DEFAULT,
2492 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
2498 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
2499 const char *content_type,
2502 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2504 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
2507 return update_mimeapps_list (info->desktop_id, content_type,
2512 static const char **
2513 g_desktop_app_info_get_supported_types (GAppInfo *appinfo)
2515 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2517 return (const char**) info->mime_types;
2520 /* Saving and deleting {{{2 */
2523 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
2529 char *data, *desktop_id;
2534 if (info->filename != NULL)
2537 /* This is only used for object created with
2538 * g_app_info_create_from_commandline. All other
2539 * object should have a filename
2542 dirname = ensure_dir (APP_DIR, error);
2546 key_file = g_key_file_new ();
2548 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2549 "Encoding", "UTF-8");
2550 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2551 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
2552 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2553 G_KEY_FILE_DESKTOP_KEY_TYPE,
2554 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
2556 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2557 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
2558 if (info->nodisplay)
2559 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2560 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2562 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2563 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
2565 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2566 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
2568 if (info->generic_name != NULL)
2569 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2570 GENERIC_NAME_KEY, info->generic_name);
2572 if (info->fullname != NULL)
2573 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2574 FULL_NAME_KEY, info->fullname);
2576 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2577 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
2579 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2580 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2582 data = g_key_file_to_data (key_file, &data_size, NULL);
2583 g_key_file_free (key_file);
2585 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
2586 filename = g_build_filename (dirname, desktop_id, NULL);
2587 g_free (desktop_id);
2590 fd = g_mkstemp (filename);
2595 display_name = g_filename_display_name (filename);
2596 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2597 _("Can't create user desktop file %s"), display_name);
2598 g_free (display_name);
2604 desktop_id = g_path_get_basename (filename);
2606 /* FIXME - actually handle error */
2607 (void) g_close (fd, NULL);
2609 res = g_file_set_contents (filename, data, data_size, error);
2613 g_free (desktop_id);
2618 info->filename = filename;
2619 info->desktop_id = desktop_id;
2621 run_update_command ("update-desktop-database", "applications");
2623 /* We just dropped a file in the user's desktop file directory. Save
2624 * the monitor the bother of having to notice it and invalidate
2627 * This means that calls directly following this will be able to see
2628 * the results immediately.
2630 desktop_file_dirs_invalidate_user ();
2636 g_desktop_app_info_can_delete (GAppInfo *appinfo)
2638 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2642 if (strstr (info->filename, "/userapp-"))
2643 return g_access (info->filename, W_OK) == 0;
2650 g_desktop_app_info_delete (GAppInfo *appinfo)
2652 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2656 if (g_remove (info->filename) == 0)
2658 update_mimeapps_list (info->desktop_id, NULL,
2662 g_free (info->filename);
2663 info->filename = NULL;
2664 g_free (info->desktop_id);
2665 info->desktop_id = NULL;
2674 /* Create for commandline {{{2 */
2676 * g_app_info_create_from_commandline:
2677 * @commandline: the commandline to use
2678 * @application_name: (allow-none): the application name, or %NULL to use @commandline
2679 * @flags: flags that can specify details of the created #GAppInfo
2680 * @error: a #GError location to store the error occurring, %NULL to ignore.
2682 * Creates a new #GAppInfo from the given information.
2684 * Note that for @commandline, the quoting rules of the Exec key of the
2685 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">freedesktop.org Desktop
2686 * Entry Specification</ulink> are applied. For example, if the @commandline contains
2687 * percent-encoded URIs, the percent-character must be doubled in order to prevent it from
2688 * being swallowed by Exec key unquoting. See the specification for exact quoting rules.
2690 * Returns: (transfer full): new #GAppInfo for given command.
2693 g_app_info_create_from_commandline (const char *commandline,
2694 const char *application_name,
2695 GAppInfoCreateFlags flags,
2700 GDesktopAppInfo *info;
2702 g_return_val_if_fail (commandline, NULL);
2704 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
2706 info->filename = NULL;
2707 info->desktop_id = NULL;
2709 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
2710 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
2711 info->hidden = FALSE;
2712 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
2713 info->exec = g_strconcat (commandline, " %u", NULL);
2715 info->exec = g_strconcat (commandline, " %f", NULL);
2716 info->nodisplay = TRUE;
2717 info->binary = binary_from_exec (info->exec);
2719 if (application_name)
2720 info->name = g_strdup (application_name);
2723 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
2724 split = g_strsplit (commandline, " ", 2);
2725 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
2727 info->name = basename;
2728 if (info->name == NULL)
2729 info->name = g_strdup ("custom");
2731 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
2733 return G_APP_INFO (info);
2736 /* GAppInfo interface init */
2739 g_desktop_app_info_iface_init (GAppInfoIface *iface)
2741 iface->dup = g_desktop_app_info_dup;
2742 iface->equal = g_desktop_app_info_equal;
2743 iface->get_id = g_desktop_app_info_get_id;
2744 iface->get_name = g_desktop_app_info_get_name;
2745 iface->get_description = g_desktop_app_info_get_description;
2746 iface->get_executable = g_desktop_app_info_get_executable;
2747 iface->get_icon = g_desktop_app_info_get_icon;
2748 iface->launch = g_desktop_app_info_launch;
2749 iface->supports_uris = g_desktop_app_info_supports_uris;
2750 iface->supports_files = g_desktop_app_info_supports_files;
2751 iface->launch_uris = g_desktop_app_info_launch_uris;
2752 iface->should_show = g_desktop_app_info_should_show;
2753 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
2754 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
2755 iface->add_supports_type = g_desktop_app_info_add_supports_type;
2756 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
2757 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
2758 iface->can_delete = g_desktop_app_info_can_delete;
2759 iface->do_delete = g_desktop_app_info_delete;
2760 iface->get_commandline = g_desktop_app_info_get_commandline;
2761 iface->get_display_name = g_desktop_app_info_get_display_name;
2762 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
2763 iface->get_supported_types = g_desktop_app_info_get_supported_types;
2766 /* Recommended applications {{{2 */
2769 app_info_in_list (GAppInfo *info,
2772 while (list != NULL)
2774 if (g_app_info_equal (info, list->data))
2782 * g_app_info_get_recommended_for_type:
2783 * @content_type: the content type to find a #GAppInfo for
2785 * Gets a list of recommended #GAppInfos for a given content type, i.e.
2786 * those applications which claim to support the given content type exactly,
2787 * and not by MIME type subclassing.
2788 * Note that the first application of the list is the last used one, i.e.
2789 * the last one for which g_app_info_set_as_last_used_for_type() has been
2792 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2793 * for given @content_type or %NULL on error.
2798 g_app_info_get_recommended_for_type (const gchar *content_type)
2800 GList *desktop_entries, *l;
2802 GDesktopAppInfo *info;
2804 g_return_val_if_fail (content_type != NULL, NULL);
2806 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
2809 for (l = desktop_entries; l != NULL; l = l->next)
2811 char *desktop_entry = l->data;
2813 info = g_desktop_app_info_new (desktop_entry);
2816 if (app_info_in_list (G_APP_INFO (info), infos))
2817 g_object_unref (info);
2819 infos = g_list_prepend (infos, info);
2821 g_free (desktop_entry);
2824 g_list_free (desktop_entries);
2826 return g_list_reverse (infos);
2830 * g_app_info_get_fallback_for_type:
2831 * @content_type: the content type to find a #GAppInfo for
2833 * Gets a list of fallback #GAppInfos for a given content type, i.e.
2834 * those applications which claim to support the given content type
2835 * by MIME type subclassing and not directly.
2837 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2838 * for given @content_type or %NULL on error.
2843 g_app_info_get_fallback_for_type (const gchar *content_type)
2845 GList *desktop_entries, *l;
2846 GList *infos, *recommended_infos;
2847 GDesktopAppInfo *info;
2849 g_return_val_if_fail (content_type != NULL, NULL);
2851 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
2852 recommended_infos = g_app_info_get_recommended_for_type (content_type);
2855 for (l = desktop_entries; l != NULL; l = l->next)
2857 char *desktop_entry = l->data;
2859 info = g_desktop_app_info_new (desktop_entry);
2862 if (app_info_in_list (G_APP_INFO (info), infos) ||
2863 app_info_in_list (G_APP_INFO (info), recommended_infos))
2864 g_object_unref (info);
2866 infos = g_list_prepend (infos, info);
2868 g_free (desktop_entry);
2871 g_list_free (desktop_entries);
2872 g_list_free_full (recommended_infos, g_object_unref);
2874 return g_list_reverse (infos);
2878 * g_app_info_get_all_for_type:
2879 * @content_type: the content type to find a #GAppInfo for
2881 * Gets a list of all #GAppInfos for a given content type,
2882 * including the recommended and fallback #GAppInfos. See
2883 * g_app_info_get_recommended_for_type() and
2884 * g_app_info_get_fallback_for_type().
2886 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2887 * for given @content_type or %NULL on error.
2890 g_app_info_get_all_for_type (const char *content_type)
2892 GList *desktop_entries, *l;
2894 char *user_default = NULL;
2895 GDesktopAppInfo *info;
2897 g_return_val_if_fail (content_type != NULL, NULL);
2899 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2902 /* put the user default in front of the list, for compatibility */
2903 if (user_default != NULL)
2905 info = g_desktop_app_info_new (user_default);
2908 infos = g_list_prepend (infos, info);
2911 g_free (user_default);
2913 for (l = desktop_entries; l != NULL; l = l->next)
2915 char *desktop_entry = l->data;
2917 info = g_desktop_app_info_new (desktop_entry);
2920 if (app_info_in_list (G_APP_INFO (info), infos))
2921 g_object_unref (info);
2923 infos = g_list_prepend (infos, info);
2925 g_free (desktop_entry);
2928 g_list_free (desktop_entries);
2930 return g_list_reverse (infos);
2934 * g_app_info_reset_type_associations:
2935 * @content_type: a content type
2937 * Removes all changes to the type associations done by
2938 * g_app_info_set_as_default_for_type(),
2939 * g_app_info_set_as_default_for_extension(),
2940 * g_app_info_add_supports_type() or
2941 * g_app_info_remove_supports_type().
2946 g_app_info_reset_type_associations (const char *content_type)
2948 update_mimeapps_list (NULL, content_type,
2954 * g_app_info_get_default_for_type:
2955 * @content_type: the content type to find a #GAppInfo for
2956 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
2959 * Gets the default #GAppInfo for a given content type.
2961 * Returns: (transfer full): #GAppInfo for given @content_type or
2965 g_app_info_get_default_for_type (const char *content_type,
2966 gboolean must_support_uris)
2968 GList *desktop_entries, *l;
2969 char *user_default = NULL;
2972 g_return_val_if_fail (content_type != NULL, NULL);
2974 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2978 if (user_default != NULL)
2980 info = (GAppInfo *) g_desktop_app_info_new (user_default);
2984 if (must_support_uris && !g_app_info_supports_uris (info))
2986 g_object_unref (info);
2992 g_free (user_default);
2996 g_list_free_full (desktop_entries, g_free);
3000 /* pick the first from the other list that matches our URI
3003 for (l = desktop_entries; l != NULL; l = l->next)
3005 char *desktop_entry = l->data;
3007 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
3010 if (must_support_uris && !g_app_info_supports_uris (info))
3012 g_object_unref (info);
3020 g_list_free_full (desktop_entries, g_free);
3026 * g_app_info_get_default_for_uri_scheme:
3027 * @uri_scheme: a string containing a URI scheme.
3029 * Gets the default application for handling URIs with
3030 * the given URI scheme. A URI scheme is the initial part
3031 * of the URI, up to but not including the ':', e.g. "http",
3034 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3037 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
3040 char *content_type, *scheme_down;
3042 scheme_down = g_ascii_strdown (uri_scheme, -1);
3043 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
3044 g_free (scheme_down);
3045 app_info = g_app_info_get_default_for_type (content_type, FALSE);
3046 g_free (content_type);
3051 /* "Get all" API {{{2 */
3054 * g_app_info_get_all:
3056 * Gets a list of all of the applications currently registered
3059 * For desktop files, this includes applications that have
3060 * <literal>NoDisplay=true</literal> set or are excluded from
3061 * display by means of <literal>OnlyShowIn</literal> or
3062 * <literal>NotShowIn</literal>. See g_app_info_should_show().
3063 * The returned list does not include applications which have
3064 * the <literal>Hidden</literal> key set.
3066 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
3069 g_app_info_get_all (void)
3072 GHashTableIter iter;
3077 apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
3079 desktop_file_dirs_lock ();
3081 for (i = 0; i < n_desktop_file_dirs; i++)
3082 desktop_file_dir_get_all (&desktop_file_dirs[i], apps);
3084 desktop_file_dirs_unlock ();
3087 g_hash_table_iter_init (&iter, apps);
3088 while (g_hash_table_iter_next (&iter, NULL, &value))
3091 infos = g_list_prepend (infos, value);
3094 g_hash_table_destroy (apps);
3099 /* Caching of mimeinfo.cache and defaults.list files {{{2 */
3103 GHashTable *mime_info_cache_map;
3104 GHashTable *defaults_list_map;
3105 GHashTable *mimeapps_list_added_map;
3106 GHashTable *mimeapps_list_removed_map;
3107 GHashTable *mimeapps_list_defaults_map;
3108 time_t mime_info_cache_timestamp;
3109 time_t defaults_list_timestamp;
3110 time_t mimeapps_list_timestamp;
3114 GList *dirs; /* mimeinfo.cache and defaults.list */
3115 time_t last_stat_time;
3118 static MimeInfoCache *mime_info_cache = NULL;
3119 G_LOCK_DEFINE_STATIC (mime_info_cache);
3121 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
3122 const char *mime_type,
3123 char **new_desktop_file_ids);
3125 static MimeInfoCache * mime_info_cache_new (void);
3128 destroy_info_cache_value (gpointer key,
3132 g_list_free_full (value, g_free);
3136 destroy_info_cache_map (GHashTable *info_cache_map)
3138 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
3139 g_hash_table_destroy (info_cache_map);
3143 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
3144 const char *cache_file,
3150 filename = g_build_filename (dir->path, cache_file, NULL);
3152 if (g_stat (filename, &buf) < 0)
3159 if (buf.st_mtime != *timestamp)
3165 /* Call with lock held */
3167 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
3171 gchar *filename, **mime_types;
3178 if (dir->mime_info_cache_map != NULL &&
3179 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
3180 &dir->mime_info_cache_timestamp))
3183 if (dir->mime_info_cache_map != NULL)
3184 destroy_info_cache_map (dir->mime_info_cache_map);
3186 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3187 (GDestroyNotify) g_free,
3190 key_file = g_key_file_new ();
3192 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
3194 if (g_stat (filename, &buf) < 0)
3197 dir->mime_info_cache_timestamp = buf.st_mtime;
3199 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
3204 if (load_error != NULL)
3207 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
3210 if (load_error != NULL)
3213 for (i = 0; mime_types[i] != NULL; i++)
3215 gchar **desktop_file_ids;
3216 char *unaliased_type;
3217 desktop_file_ids = g_key_file_get_string_list (key_file,
3223 if (desktop_file_ids == NULL)
3226 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3227 mime_info_cache_dir_add_desktop_entries (dir,
3230 g_free (unaliased_type);
3232 g_strfreev (desktop_file_ids);
3235 g_strfreev (mime_types);
3236 g_key_file_free (key_file);
3241 g_key_file_free (key_file);
3243 if (mime_types != NULL)
3244 g_strfreev (mime_types);
3247 g_error_free (load_error);
3251 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
3255 gchar *filename, **mime_types;
3256 char *unaliased_type;
3257 char **desktop_file_ids;
3264 if (dir->defaults_list_map != NULL &&
3265 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
3266 &dir->defaults_list_timestamp))
3269 if (dir->defaults_list_map != NULL)
3270 g_hash_table_destroy (dir->defaults_list_map);
3271 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3272 g_free, (GDestroyNotify)g_strfreev);
3275 key_file = g_key_file_new ();
3277 filename = g_build_filename (dir->path, "defaults.list", NULL);
3278 if (g_stat (filename, &buf) < 0)
3281 dir->defaults_list_timestamp = buf.st_mtime;
3283 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
3287 if (load_error != NULL)
3290 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
3292 if (mime_types != NULL)
3294 for (i = 0; mime_types[i] != NULL; i++)
3296 desktop_file_ids = g_key_file_get_string_list (key_file,
3297 DEFAULT_APPLICATIONS_GROUP,
3301 if (desktop_file_ids == NULL)
3304 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3305 g_hash_table_replace (dir->defaults_list_map,
3310 g_strfreev (mime_types);
3313 g_key_file_free (key_file);
3318 g_key_file_free (key_file);
3320 if (mime_types != NULL)
3321 g_strfreev (mime_types);
3324 g_error_free (load_error);
3328 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
3332 gchar *filename, **mime_types;
3333 char *unaliased_type;
3334 char **desktop_file_ids;
3342 if (dir->mimeapps_list_added_map != NULL &&
3343 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
3344 &dir->mimeapps_list_timestamp))
3347 if (dir->mimeapps_list_added_map != NULL)
3348 g_hash_table_destroy (dir->mimeapps_list_added_map);
3349 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3350 g_free, (GDestroyNotify)g_strfreev);
3352 if (dir->mimeapps_list_removed_map != NULL)
3353 g_hash_table_destroy (dir->mimeapps_list_removed_map);
3354 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3355 g_free, (GDestroyNotify)g_strfreev);
3357 if (dir->mimeapps_list_defaults_map != NULL)
3358 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
3359 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3362 key_file = g_key_file_new ();
3364 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
3365 if (g_stat (filename, &buf) < 0)
3368 dir->mimeapps_list_timestamp = buf.st_mtime;
3370 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
3374 if (load_error != NULL)
3377 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
3379 if (mime_types != NULL)
3381 for (i = 0; mime_types[i] != NULL; i++)
3383 desktop_file_ids = g_key_file_get_string_list (key_file,
3384 ADDED_ASSOCIATIONS_GROUP,
3388 if (desktop_file_ids == NULL)
3391 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3392 g_hash_table_replace (dir->mimeapps_list_added_map,
3397 g_strfreev (mime_types);
3400 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
3402 if (mime_types != NULL)
3404 for (i = 0; mime_types[i] != NULL; i++)
3406 desktop_file_ids = g_key_file_get_string_list (key_file,
3407 REMOVED_ASSOCIATIONS_GROUP,
3411 if (desktop_file_ids == NULL)
3414 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3415 g_hash_table_replace (dir->mimeapps_list_removed_map,
3420 g_strfreev (mime_types);
3423 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
3425 if (mime_types != NULL)
3427 for (i = 0; mime_types[i] != NULL; i++)
3429 desktop_id = g_key_file_get_string (key_file,
3430 DEFAULT_APPLICATIONS_GROUP,
3433 if (desktop_id == NULL)
3436 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3437 g_hash_table_replace (dir->mimeapps_list_defaults_map,
3442 g_strfreev (mime_types);
3445 g_key_file_free (key_file);
3450 g_key_file_free (key_file);
3452 if (mime_types != NULL)
3453 g_strfreev (mime_types);
3456 g_error_free (load_error);
3459 static MimeInfoCacheDir *
3460 mime_info_cache_dir_new (const char *path)
3462 MimeInfoCacheDir *dir;
3464 dir = g_new0 (MimeInfoCacheDir, 1);
3465 dir->path = g_strdup (path);
3471 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
3476 if (dir->mime_info_cache_map != NULL)
3478 destroy_info_cache_map (dir->mime_info_cache_map);
3479 dir->mime_info_cache_map = NULL;
3483 if (dir->defaults_list_map != NULL)
3485 g_hash_table_destroy (dir->defaults_list_map);
3486 dir->defaults_list_map = NULL;
3489 if (dir->mimeapps_list_added_map != NULL)
3491 g_hash_table_destroy (dir->mimeapps_list_added_map);
3492 dir->mimeapps_list_added_map = NULL;
3495 if (dir->mimeapps_list_removed_map != NULL)
3497 g_hash_table_destroy (dir->mimeapps_list_removed_map);
3498 dir->mimeapps_list_removed_map = NULL;
3501 if (dir->mimeapps_list_defaults_map != NULL)
3503 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
3504 dir->mimeapps_list_defaults_map = NULL;
3511 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
3512 const char *mime_type,
3513 char **new_desktop_file_ids)
3515 GList *desktop_file_ids;
3518 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
3521 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
3523 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
3524 desktop_file_ids = g_list_append (desktop_file_ids,
3525 g_strdup (new_desktop_file_ids[i]));
3528 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
3532 mime_info_cache_init_dir_lists (void)
3536 mime_info_cache = mime_info_cache_new ();
3538 desktop_file_dirs_refresh ();
3540 for (i = 0; i < n_desktop_file_dirs; i++)
3542 MimeInfoCacheDir *dir;
3544 dir = mime_info_cache_dir_new (desktop_file_dirs[i].path);
3548 mime_info_cache_dir_init (dir);
3549 mime_info_cache_dir_init_defaults_list (dir);
3550 mime_info_cache_dir_init_mimeapps_list (dir);
3552 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
3558 mime_info_cache_update_dir_lists (void)
3562 tmp = mime_info_cache->dirs;
3566 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
3568 /* No need to do this if we had file monitors... */
3569 mime_info_cache_dir_init (dir);
3570 mime_info_cache_dir_init_defaults_list (dir);
3571 mime_info_cache_dir_init_mimeapps_list (dir);
3578 mime_info_cache_init (void)
3580 G_LOCK (mime_info_cache);
3581 if (mime_info_cache == NULL)
3582 mime_info_cache_init_dir_lists ();
3588 if (now >= mime_info_cache->last_stat_time + 10)
3590 mime_info_cache_update_dir_lists ();
3591 mime_info_cache->last_stat_time = now;
3595 G_UNLOCK (mime_info_cache);
3598 static MimeInfoCache *
3599 mime_info_cache_new (void)
3601 MimeInfoCache *cache;
3603 cache = g_new0 (MimeInfoCache, 1);
3609 mime_info_cache_free (MimeInfoCache *cache)
3614 g_list_free_full (cache->dirs, (GDestroyNotify) mime_info_cache_dir_free);
3619 * mime_info_cache_reload:
3620 * @dir: directory path which needs reloading.
3622 * Reload the mime information for the @dir.
3625 mime_info_cache_reload (const char *dir)
3627 /* FIXME: just reload the dir that needs reloading,
3628 * don't blow the whole cache
3630 if (mime_info_cache != NULL)
3632 G_LOCK (mime_info_cache);
3633 mime_info_cache_free (mime_info_cache);
3634 mime_info_cache = NULL;
3635 G_UNLOCK (mime_info_cache);
3640 append_desktop_entry (GList *list,
3641 const char *desktop_entry,
3642 GList *removed_entries)
3644 /* Add if not already in list, and valid */
3645 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
3646 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
3647 list = g_list_prepend (list, g_strdup (desktop_entry));
3653 * get_all_desktop_entries_for_mime_type:
3654 * @mime_type: a mime type.
3655 * @except: NULL or a strv list
3657 * Returns all the desktop ids for @mime_type. The desktop files
3658 * are listed in an order so that default applications are listed before
3659 * non-default ones, and handlers for inherited mimetypes are listed
3660 * after the base ones.
3662 * Optionally doesn't list the desktop ids given in the @except
3664 * Return value: a #GList containing the desktop ids which claim
3665 * to handle @mime_type.
3668 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
3669 const char **except,
3670 gboolean include_fallback,
3671 char **explicit_default)
3673 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
3674 MimeInfoCacheDir *dir;
3675 char *mime_type, *default_entry = NULL;
3676 char *old_default_entry = NULL;
3679 char **default_entries;
3680 char **removed_associations;
3681 gboolean already_found_handler;
3686 mime_info_cache_init ();
3688 if (include_fallback)
3690 /* collect all ancestors */
3691 mime_types = _g_unix_content_type_get_parents (base_mime_type);
3692 array = g_ptr_array_new ();
3693 for (i = 0; mime_types[i]; i++)
3694 g_ptr_array_add (array, mime_types[i]);
3695 g_free (mime_types);
3696 for (i = 0; i < array->len; i++)
3698 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
3699 for (j = 0; anc[j]; j++)
3701 for (k = 0; k < array->len; k++)
3703 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
3706 if (k == array->len) /* not found */
3707 g_ptr_array_add (array, g_strdup (anc[j]));
3711 g_ptr_array_add (array, NULL);
3712 mime_types = (char **)g_ptr_array_free (array, FALSE);
3716 mime_types = g_malloc0 (2 * sizeof (gchar *));
3717 mime_types[0] = g_strdup (base_mime_type);
3718 mime_types[1] = NULL;
3721 G_LOCK (mime_info_cache);
3723 removed_entries = NULL;
3724 desktop_entries = NULL;
3726 for (i = 0; except != NULL && except[i] != NULL; i++)
3727 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
3729 for (i = 0; mime_types[i] != NULL; i++)
3731 mime_type = mime_types[i];
3733 /* This is true if we already found a handler for a more specific
3734 mimetype. If its set we ignore any defaults for the less specific
3736 already_found_handler = (desktop_entries != NULL);
3738 /* Go through all apps listed in user and system dirs */
3739 for (dir_list = mime_info_cache->dirs;
3741 dir_list = dir_list->next)
3743 dir = dir_list->data;
3745 /* Pick the explicit default application if we got no result earlier
3746 * (ie, for more specific mime types)
3748 if (!already_found_handler)
3750 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
3754 /* Save the default entry if it's the first one we encounter */
3755 if (default_entry == NULL)
3756 default_entry = g_strdup (entry);
3760 /* Then added associations from mimeapps.list */
3761 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
3762 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3763 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3765 /* Then removed associations from mimeapps.list */
3766 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
3767 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
3768 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
3770 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
3771 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
3772 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3774 if (default_entry == NULL && old_default_entry == NULL && !already_found_handler)
3775 old_default_entry = g_strdup (default_entries[j]);
3777 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3781 /* Go through all entries that support the mimetype */
3782 for (dir_list = mime_info_cache->dirs;
3784 dir_list = dir_list->next)
3786 dir = dir_list->data;
3788 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
3789 for (tmp = list; tmp != NULL; tmp = tmp->next)
3790 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
3794 G_UNLOCK (mime_info_cache);
3796 g_strfreev (mime_types);
3798 /* If we have no default from mimeapps.list, take it from
3799 * defaults.list intead.
3801 * If we do have a default from mimeapps.list, free any one that came
3802 * from defaults.list.
3804 if (default_entry == NULL)
3805 default_entry = old_default_entry;
3807 g_free (old_default_entry);
3809 if (explicit_default != NULL)
3810 *explicit_default = default_entry;
3812 g_free (default_entry);
3814 g_list_free_full (removed_entries, g_free);
3816 desktop_entries = g_list_reverse (desktop_entries);
3818 return desktop_entries;
3821 /* GDesktopAppInfoLookup interface {{{2 */
3823 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
3825 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
3826 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
3829 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
3833 /* "Get for mime type" APIs {{{2 */
3836 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
3837 * @lookup: a #GDesktopAppInfoLookup
3838 * @uri_scheme: a string containing a URI scheme.
3840 * Gets the default application for launching applications
3841 * using this URI scheme for a particular GDesktopAppInfoLookup
3844 * The GDesktopAppInfoLookup interface and this function is used
3845 * to implement g_app_info_get_default_for_uri_scheme() backends
3846 * in a GIO module. There is no reason for applications to use it
3847 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
3849 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3851 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
3854 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
3855 const char *uri_scheme)
3857 GDesktopAppInfoLookupIface *iface;
3859 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
3861 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
3863 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
3866 G_GNUC_END_IGNORE_DEPRECATIONS
3868 /* Misc getter APIs {{{2 */
3871 * g_desktop_app_info_get_startup_wm_class:
3872 * @info: a #GDesktopAppInfo that supports startup notify
3874 * Retrieves the StartupWMClass field from @info. This represents the
3875 * WM_CLASS property of the main window of the application, if launched
3878 * Returns: (transfer none): the startup WM class, or %NULL if none is set
3879 * in the desktop file.
3884 g_desktop_app_info_get_startup_wm_class (GDesktopAppInfo *info)
3886 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
3888 return info->startup_wm_class;
3892 * g_desktop_app_info_get_string:
3893 * @info: a #GDesktopAppInfo
3894 * @key: the key to look up
3896 * Looks up a string value in the keyfile backing @info.
3898 * The @key is looked up in the "Desktop Entry" group.
3900 * Returns: a newly allocated string, or %NULL if the key
3906 g_desktop_app_info_get_string (GDesktopAppInfo *info,
3909 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
3911 return g_key_file_get_string (info->keyfile,
3912 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
3916 * g_desktop_app_info_get_boolean:
3917 * @info: a #GDesktopAppInfo
3918 * @key: the key to look up
3920 * Looks up a boolean value in the keyfile backing @info.
3922 * The @key is looked up in the "Desktop Entry" group.
3924 * Returns: the boolean value, or %FALSE if the key
3930 g_desktop_app_info_get_boolean (GDesktopAppInfo *info,
3933 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
3935 return g_key_file_get_boolean (info->keyfile,
3936 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
3940 * g_desktop_app_info_has_key:
3941 * @info: a #GDesktopAppInfo
3942 * @key: the key to look up
3944 * Returns whether @key exists in the "Desktop Entry" group
3945 * of the keyfile backing @info.
3947 * Returns: %TRUE if the @key exists
3952 g_desktop_app_info_has_key (GDesktopAppInfo *info,
3955 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
3957 return g_key_file_has_key (info->keyfile,
3958 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
3961 /* Desktop actions support {{{2 */
3964 * g_desktop_app_info_list_actions:
3965 * @info: a #GDesktopAppInfo
3967 * Returns the list of "additional application actions" supported on the
3968 * desktop file, as per the desktop file specification.
3970 * As per the specification, this is the list of actions that are
3971 * explicitly listed in the "Actions" key of the [Desktop Entry] group.
3973 * Returns: (array zero-terminated=1) (element-type utf8) (transfer none): a list of strings, always non-%NULL
3977 const gchar * const *
3978 g_desktop_app_info_list_actions (GDesktopAppInfo *info)
3980 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
3982 return (const gchar **) info->actions;
3986 app_info_has_action (GDesktopAppInfo *info,
3987 const gchar *action_name)
3991 for (i = 0; info->actions[i]; i++)
3992 if (g_str_equal (info->actions[i], action_name))
3999 * g_desktop_app_info_get_action_name:
4000 * @info: a #GDesktopAppInfo
4001 * @action_name: the name of the action as from
4002 * g_desktop_app_info_list_actions()
4004 * Gets the user-visible display name of the "additional application
4005 * action" specified by @action_name.
4007 * This corresponds to the "Name" key within the keyfile group for the
4010 * Returns: (transfer full): the locale-specific action name
4015 g_desktop_app_info_get_action_name (GDesktopAppInfo *info,
4016 const gchar *action_name)
4021 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4022 g_return_val_if_fail (action_name != NULL, NULL);
4023 g_return_val_if_fail (app_info_has_action (info, action_name), NULL);
4025 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4026 result = g_key_file_get_locale_string (info->keyfile, group_name, "Name", NULL, NULL);
4027 g_free (group_name);
4029 /* The spec says that the Name field must be given.
4031 * If it's not, let's follow the behaviour of our get_name()
4032 * implementation above and never return %NULL.
4035 result = g_strdup (_("Unnamed"));
4041 * g_desktop_app_info_launch_action:
4042 * @info: a #GDesktopAppInfo
4043 * @action_name: the name of the action as from
4044 * g_desktop_app_info_list_actions()
4045 * @launch_context: (allow-none): a #GAppLaunchContext
4047 * Activates the named application action.
4049 * You may only call this function on action names that were
4050 * returned from g_desktop_app_info_list_actions().
4052 * Note that if the main entry of the desktop file indicates that the
4053 * application supports startup notification, and @launch_context is
4054 * non-%NULL, then startup notification will be used when activating the
4055 * action (and as such, invocation of the action on the receiving side
4056 * must signal the end of startup notification when it is completed).
4057 * This is the expected behaviour of applications declaring additional
4058 * actions, as per the desktop file specification.
4060 * As with g_app_info_launch() there is no way to detect failures that
4061 * occur while using this function.
4066 g_desktop_app_info_launch_action (GDesktopAppInfo *info,
4067 const gchar *action_name,
4068 GAppLaunchContext *launch_context)
4070 GDBusConnection *session_bus;
4072 g_return_if_fail (G_IS_DESKTOP_APP_INFO (info));
4073 g_return_if_fail (action_name != NULL);
4074 g_return_if_fail (app_info_has_action (info, action_name));
4076 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
4078 if (session_bus && info->app_id)
4082 object_path = object_path_from_appid (info->app_id);
4083 g_dbus_connection_call (session_bus, info->app_id, object_path,
4084 "org.freedesktop.Application", "ActivateAction",
4085 g_variant_new ("(sav@a{sv})", action_name, NULL,
4086 g_desktop_app_info_make_platform_data (info, NULL, launch_context)),
4087 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
4088 g_free (object_path);
4095 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4096 exec_line = g_key_file_get_string (info->keyfile, group_name, "Exec", NULL);
4097 g_free (group_name);
4100 g_desktop_app_info_launch_uris_with_spawn (info, session_bus, exec_line, NULL, launch_context,
4101 _SPAWN_FLAGS_DEFAULT, NULL, NULL, NULL, NULL, NULL);
4104 if (session_bus != NULL)
4106 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
4107 g_object_unref (session_bus);
4112 /* vim:set foldmethod=marker: */