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"
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 <filename><gio/gdesktopappinfo.h></filename> belongs to
62 * the UNIX-specific GIO interfaces, thus you have to use the
63 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
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 */
150 static DesktopFileDir *desktop_file_dirs;
151 static guint n_desktop_file_dirs;
153 /* DesktopFileDir "API" {{{2 */
156 * desktop_file_dir_create:
157 * @array: the #GArray to add a new item to
158 * @data_dir: an XDG_DATA_DIR
160 * Creates a #DesktopFileDir for the corresponding @data_dir, adding it
164 desktop_file_dir_create (GArray *array,
165 const gchar *data_dir)
167 DesktopFileDir dir = { 0, };
169 dir.path = g_build_filename (data_dir, "applications", NULL);
171 g_array_append_val (array, dir);
174 /* Global setup API {{{2 */
177 desktop_file_dirs_refresh (void)
179 if (g_once_init_enter (&desktop_file_dirs))
181 const char * const *data_dirs;
185 tmp = g_array_new (FALSE, FALSE, sizeof (DesktopFileDir));
187 /* Highest priority: the user's ~/.local/share/applications */
188 desktop_file_dir_create (tmp, g_get_user_data_dir ());
190 /* Following that, XDG_DATA_DIRS/applications, in order */
191 data_dirs = g_get_system_data_dirs ();
192 for (i = 0; data_dirs[i]; i++)
193 desktop_file_dir_create (tmp, data_dirs[i]);
195 n_desktop_file_dirs = tmp->len;
197 g_once_init_leave (&desktop_file_dirs, (DesktopFileDir *) g_array_free (tmp, FALSE));
201 /* GDesktopAppInfo implementation {{{1 */
202 /* GObject implementation {{{2 */
204 g_desktop_app_info_finalize (GObject *object)
206 GDesktopAppInfo *info;
208 info = G_DESKTOP_APP_INFO (object);
210 g_free (info->desktop_id);
211 g_free (info->filename);
214 g_key_file_unref (info->keyfile);
217 g_free (info->generic_name);
218 g_free (info->fullname);
219 g_free (info->comment);
220 g_free (info->icon_name);
222 g_object_unref (info->icon);
223 g_strfreev (info->keywords);
224 g_strfreev (info->only_show_in);
225 g_strfreev (info->not_show_in);
226 g_free (info->try_exec);
228 g_free (info->binary);
230 g_free (info->categories);
231 g_free (info->startup_wm_class);
232 g_strfreev (info->mime_types);
233 g_free (info->app_id);
234 g_strfreev (info->actions);
236 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
240 g_desktop_app_info_set_property (GObject *object,
245 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
250 self->filename = g_value_dup_string (value);
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260 g_desktop_app_info_get_property (GObject *object,
265 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
270 g_value_set_string (value, self->filename);
273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
279 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
281 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
283 gobject_class->get_property = g_desktop_app_info_get_property;
284 gobject_class->set_property = g_desktop_app_info_set_property;
285 gobject_class->finalize = g_desktop_app_info_finalize;
288 * GDesktopAppInfo:filename:
290 * The origin filename of this #GDesktopAppInfo
292 g_object_class_install_property (gobject_class,
294 g_param_spec_string ("filename", "Filename", "", NULL,
295 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
299 g_desktop_app_info_init (GDesktopAppInfo *local)
303 /* Construction... {{{2 */
307 * @exec: an exec line
309 * Returns the first word in an exec line (ie: the binary name).
311 * If @exec is " progname --foo %F" then returns "progname".
314 binary_from_exec (const char *exec)
316 const char *p, *start;
322 while (*p != ' ' && *p != 0)
325 return g_strndup (start, p - start);
329 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
336 gboolean bus_activatable;
338 start_group = g_key_file_get_start_group (key_file);
339 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
341 g_free (start_group);
344 g_free (start_group);
346 type = g_key_file_get_string (key_file,
347 G_KEY_FILE_DESKTOP_GROUP,
348 G_KEY_FILE_DESKTOP_KEY_TYPE,
350 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
357 try_exec = g_key_file_get_string (key_file,
358 G_KEY_FILE_DESKTOP_GROUP,
359 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
361 if (try_exec && try_exec[0] != '\0')
364 t = g_find_program_in_path (try_exec);
373 exec = g_key_file_get_string (key_file,
374 G_KEY_FILE_DESKTOP_GROUP,
375 G_KEY_FILE_DESKTOP_KEY_EXEC,
377 if (exec && exec[0] != '\0')
381 if (!g_shell_parse_argv (exec, &argc, &argv, NULL))
390 t = g_find_program_in_path (argv[0]);
403 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
404 info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
405 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
406 info->keywords = g_key_file_get_locale_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, KEYWORDS_KEY, NULL, NULL, NULL);
407 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
408 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
409 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
410 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);
411 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);
412 info->try_exec = try_exec;
414 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
415 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
416 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
417 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
418 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
419 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
420 info->startup_wm_class = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, STARTUP_WM_CLASS_KEY, NULL);
421 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);
422 bus_activatable = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE, NULL);
423 info->actions = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ACTIONS, NULL, NULL);
425 /* Remove the special-case: no Actions= key just means 0 extra actions */
426 if (info->actions == NULL)
427 info->actions = g_new0 (gchar *, 0 + 1);
432 if (g_path_is_absolute (info->icon_name))
436 file = g_file_new_for_path (info->icon_name);
437 info->icon = g_file_icon_new (file);
438 g_object_unref (file);
444 /* Work around a common mistake in desktop files */
445 if ((p = strrchr (info->icon_name, '.')) != NULL &&
446 (strcmp (p, ".png") == 0 ||
447 strcmp (p, ".xpm") == 0 ||
448 strcmp (p, ".svg") == 0))
451 info->icon = g_themed_icon_new (info->icon_name);
456 info->binary = binary_from_exec (info->exec);
458 if (info->path && info->path[0] == '\0')
469 basename = g_path_get_basename (info->filename);
470 last_dot = strrchr (basename, '.');
472 if (last_dot && g_str_equal (last_dot, ".desktop"))
476 if (g_dbus_is_interface_name (basename))
477 info->app_id = g_strdup (basename);
483 info->keyfile = g_key_file_ref (key_file);
489 g_desktop_app_info_load_file (GDesktopAppInfo *self)
492 gboolean retval = FALSE;
494 g_return_val_if_fail (self->filename != NULL, FALSE);
496 self->desktop_id = g_path_get_basename (self->filename);
498 key_file = g_key_file_new ();
500 if (g_key_file_load_from_file (key_file, self->filename, G_KEY_FILE_NONE, NULL))
501 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
503 g_key_file_unref (key_file);
508 * g_desktop_app_info_new_from_keyfile:
509 * @key_file: an opened #GKeyFile
511 * Creates a new #GDesktopAppInfo.
513 * Returns: a new #GDesktopAppInfo or %NULL on error.
518 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
520 GDesktopAppInfo *info;
522 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
523 info->filename = NULL;
524 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
526 g_object_unref (info);
533 * g_desktop_app_info_new_from_filename:
534 * @filename: the path of a desktop file, in the GLib filename encoding
536 * Creates a new #GDesktopAppInfo.
538 * Returns: a new #GDesktopAppInfo or %NULL on error.
541 g_desktop_app_info_new_from_filename (const char *filename)
543 GDesktopAppInfo *info = NULL;
545 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
546 if (!g_desktop_app_info_load_file (info))
548 g_object_unref (info);
555 * g_desktop_app_info_new:
556 * @desktop_id: the desktop file id
558 * Creates a new #GDesktopAppInfo based on a desktop file id.
560 * A desktop file id is the basename of the desktop file, including the
561 * .desktop extension. GIO is looking for a desktop file with this name
562 * in the <filename>applications</filename> subdirectories of the XDG data
563 * directories (i.e. the directories specified in the
564 * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment
565 * variables). GIO also supports the prefix-to-subdirectory mapping that is
566 * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink>
567 * (i.e. a desktop id of kde-foo.desktop will match
568 * <filename>/usr/share/applications/kde/foo.desktop</filename>).
570 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
573 g_desktop_app_info_new (const char *desktop_id)
575 GDesktopAppInfo *appinfo;
579 desktop_file_dirs_refresh ();
581 basename = g_strdup (desktop_id);
583 for (i = 0; i < n_desktop_file_dirs; i++)
585 const gchar *path = desktop_file_dirs[i].path;
589 filename = g_build_filename (path, desktop_id, NULL);
590 appinfo = g_desktop_app_info_new_from_filename (filename);
596 while ((p = strchr (p, '-')) != NULL)
600 filename = g_build_filename (path, basename, NULL);
601 appinfo = g_desktop_app_info_new_from_filename (filename);
616 g_free (appinfo->desktop_id);
617 appinfo->desktop_id = g_strdup (desktop_id);
619 if (g_desktop_app_info_get_is_hidden (appinfo))
621 g_object_unref (appinfo);
629 g_desktop_app_info_dup (GAppInfo *appinfo)
631 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
632 GDesktopAppInfo *new_info;
634 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
636 new_info->filename = g_strdup (info->filename);
637 new_info->desktop_id = g_strdup (info->desktop_id);
640 new_info->keyfile = g_key_file_ref (info->keyfile);
642 new_info->name = g_strdup (info->name);
643 new_info->generic_name = g_strdup (info->generic_name);
644 new_info->fullname = g_strdup (info->fullname);
645 new_info->keywords = g_strdupv (info->keywords);
646 new_info->comment = g_strdup (info->comment);
647 new_info->nodisplay = info->nodisplay;
648 new_info->icon_name = g_strdup (info->icon_name);
650 new_info->icon = g_object_ref (info->icon);
651 new_info->only_show_in = g_strdupv (info->only_show_in);
652 new_info->not_show_in = g_strdupv (info->not_show_in);
653 new_info->try_exec = g_strdup (info->try_exec);
654 new_info->exec = g_strdup (info->exec);
655 new_info->binary = g_strdup (info->binary);
656 new_info->path = g_strdup (info->path);
657 new_info->app_id = g_strdup (info->app_id);
658 new_info->hidden = info->hidden;
659 new_info->terminal = info->terminal;
660 new_info->startup_notify = info->startup_notify;
662 return G_APP_INFO (new_info);
665 /* GAppInfo interface implementation functions {{{2 */
668 g_desktop_app_info_equal (GAppInfo *appinfo1,
671 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
672 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
674 if (info1->desktop_id == NULL ||
675 info2->desktop_id == NULL)
676 return info1 == info2;
678 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
682 g_desktop_app_info_get_id (GAppInfo *appinfo)
684 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
686 return info->desktop_id;
690 g_desktop_app_info_get_name (GAppInfo *appinfo)
692 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
694 if (info->name == NULL)
700 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
702 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
704 if (info->fullname == NULL)
705 return g_desktop_app_info_get_name (appinfo);
706 return info->fullname;
710 * g_desktop_app_info_get_is_hidden:
711 * @info: a #GDesktopAppInfo.
713 * A desktop file is hidden if the Hidden key in it is
716 * Returns: %TRUE if hidden, %FALSE otherwise.
719 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
725 * g_desktop_app_info_get_filename:
726 * @info: a #GDesktopAppInfo
728 * When @info was created from a known filename, return it. In some
729 * situations such as the #GDesktopAppInfo returned from
730 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
732 * Returns: The full path to the file for @info, or %NULL if not known.
736 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
738 return info->filename;
742 g_desktop_app_info_get_description (GAppInfo *appinfo)
744 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
746 return info->comment;
750 g_desktop_app_info_get_executable (GAppInfo *appinfo)
752 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
758 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
760 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
766 g_desktop_app_info_get_icon (GAppInfo *appinfo)
768 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
774 * g_desktop_app_info_get_categories:
775 * @info: a #GDesktopAppInfo
777 * Gets the categories from the desktop file.
779 * Returns: The unparsed Categories key from the desktop file;
780 * i.e. no attempt is made to split it by ';' or validate it.
783 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
785 return info->categories;
789 * g_desktop_app_info_get_keywords:
790 * @info: a #GDesktopAppInfo
792 * Gets the keywords from the desktop file.
794 * Returns: (transfer none): The value of the Keywords key
799 g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
801 return (const char * const *)info->keywords;
805 * g_desktop_app_info_get_generic_name:
806 * @info: a #GDesktopAppInfo
808 * Gets the generic name from the destkop file.
810 * Returns: The value of the GenericName key
813 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
815 return info->generic_name;
819 * g_desktop_app_info_get_nodisplay:
820 * @info: a #GDesktopAppInfo
822 * Gets the value of the NoDisplay key, which helps determine if the
823 * application info should be shown in menus. See
824 * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
826 * Returns: The value of the NoDisplay key
831 g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
833 return info->nodisplay;
837 * g_desktop_app_info_get_show_in:
838 * @info: a #GDesktopAppInfo
839 * @desktop_env: a string specifying a desktop name
841 * Checks if the application info should be shown in menus that list available
842 * applications for a specific name of the desktop, based on the
843 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys.
845 * If @desktop_env is %NULL, then the name of the desktop set with
846 * g_desktop_app_info_set_desktop_env() is used.
848 * Note that g_app_info_should_show() for @info will include this check (with
849 * %NULL for @desktop_env) as well as additional checks.
851 * Returns: %TRUE if the @info should be shown in @desktop_env according to the
852 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys, %FALSE
858 g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
859 const gchar *desktop_env)
864 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
867 G_LOCK (g_desktop_env);
868 desktop_env = g_desktop_env;
869 G_UNLOCK (g_desktop_env);
872 if (info->only_show_in)
874 if (desktop_env == NULL)
878 for (i = 0; info->only_show_in[i] != NULL; i++)
880 if (strcmp (info->only_show_in[i], desktop_env) == 0)
890 if (info->not_show_in && desktop_env)
892 for (i = 0; info->not_show_in[i] != NULL; i++)
894 if (strcmp (info->not_show_in[i], desktop_env) == 0)
902 /* Launching... {{{2 */
905 expand_macro_single (char macro, char *uri)
912 file = g_file_new_for_uri (uri);
918 result = g_shell_quote (uri);
922 path = g_file_get_path (file);
924 result = g_shell_quote (path);
928 path = g_file_get_path (file);
931 name = g_path_get_dirname (path);
932 result = g_shell_quote (name);
938 path = g_file_get_path (file);
941 name = g_path_get_basename (path);
942 result = g_shell_quote (name);
948 g_object_unref (file);
955 expand_macro (char macro,
957 GDesktopAppInfo *info,
960 GList *uris = *uri_list;
962 gboolean force_file_uri;
963 char force_file_uri_macro;
966 g_return_if_fail (exec != NULL);
968 /* On %u and %U, pass POSIX file path pointing to the URI via
969 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
970 * running or the URI doesn't have a POSIX file path via FUSE
971 * we'll just pass the URI.
973 force_file_uri_macro = macro;
974 force_file_uri = FALSE;
980 force_file_uri_macro = 'f';
981 force_file_uri = TRUE;
984 force_file_uri_macro = 'F';
985 force_file_uri = TRUE;
1001 if (!force_file_uri ||
1002 /* Pass URI if it contains an anchor */
1003 strchr (uri, '#') != NULL)
1005 expanded = expand_macro_single (macro, uri);
1009 expanded = expand_macro_single (force_file_uri_macro, uri);
1010 if (expanded == NULL)
1011 expanded = expand_macro_single (macro, uri);
1016 g_string_append (exec, expanded);
1032 if (!force_file_uri ||
1033 /* Pass URI if it contains an anchor */
1034 strchr (uri, '#') != NULL)
1036 expanded = expand_macro_single (macro, uri);
1040 expanded = expand_macro_single (force_file_uri_macro, uri);
1041 if (expanded == NULL)
1042 expanded = expand_macro_single (macro, uri);
1047 g_string_append (exec, expanded);
1053 if (uris != NULL && expanded)
1054 g_string_append_c (exec, ' ');
1060 if (info->icon_name)
1062 g_string_append (exec, "--icon ");
1063 expanded = g_shell_quote (info->icon_name);
1064 g_string_append (exec, expanded);
1072 expanded = g_shell_quote (info->name);
1073 g_string_append (exec, expanded);
1081 expanded = g_shell_quote (info->filename);
1082 g_string_append (exec, expanded);
1087 case 'm': /* deprecated */
1091 g_string_append_c (exec, '%');
1099 expand_application_parameters (GDesktopAppInfo *info,
1100 const gchar *exec_line,
1106 GList *uri_list = *uris;
1107 const char *p = exec_line;
1108 GString *expanded_exec;
1111 if (exec_line == NULL)
1113 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1114 _("Desktop file didn't specify Exec field"));
1118 expanded_exec = g_string_new (NULL);
1122 if (p[0] == '%' && p[1] != '\0')
1124 expand_macro (p[1], expanded_exec, info, uris);
1128 g_string_append_c (expanded_exec, *p);
1133 /* No file substitutions */
1134 if (uri_list == *uris && uri_list != NULL)
1136 /* If there is no macro default to %f. This is also what KDE does */
1137 g_string_append_c (expanded_exec, ' ');
1138 expand_macro ('f', expanded_exec, info, uris);
1141 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
1142 g_string_free (expanded_exec, TRUE);
1147 prepend_terminal_to_vector (int *argc,
1154 char **term_argv = NULL;
1159 g_return_val_if_fail (argc != NULL, FALSE);
1160 g_return_val_if_fail (argv != NULL, FALSE);
1168 /* compute size if not given */
1171 for (i = 0; the_argv[i] != NULL; i++)
1177 term_argv = g_new0 (char *, 3);
1179 check = g_find_program_in_path ("gnome-terminal");
1182 term_argv[0] = check;
1183 /* Note that gnome-terminal takes -x and
1184 * as -e in gnome-terminal is broken we use that. */
1185 term_argv[1] = g_strdup ("-x");
1190 check = g_find_program_in_path ("nxterm");
1192 check = g_find_program_in_path ("color-xterm");
1194 check = g_find_program_in_path ("rxvt");
1196 check = g_find_program_in_path ("xterm");
1198 check = g_find_program_in_path ("dtterm");
1201 check = g_strdup ("xterm");
1202 g_warning ("couldn't find a terminal, falling back to xterm");
1204 term_argv[0] = check;
1205 term_argv[1] = g_strdup ("-e");
1208 real_argc = term_argc + *argc;
1209 real_argv = g_new (char *, real_argc + 1);
1211 for (i = 0; i < term_argc; i++)
1212 real_argv[i] = term_argv[i];
1214 for (j = 0; j < *argc; j++, i++)
1215 real_argv[i] = (char *)the_argv[j];
1217 real_argv[i] = NULL;
1223 /* we use g_free here as we sucked all the inner strings
1224 * out from it into real_argv */
1229 #endif /* G_OS_WIN32 */
1233 create_files_for_uris (GList *uris)
1240 for (iter = uris; iter; iter = iter->next)
1242 GFile *file = g_file_new_for_uri ((char *)iter->data);
1243 res = g_list_prepend (res, file);
1246 return g_list_reverse (res);
1251 GSpawnChildSetupFunc user_setup;
1252 gpointer user_setup_data;
1258 child_setup (gpointer user_data)
1260 ChildSetupData *data = user_data;
1262 if (data->pid_envvar)
1264 pid_t pid = getpid ();
1268 /* Write the pid into the space already reserved for it in the
1269 * environment array. We can't use sprintf because it might
1270 * malloc, so we do it by hand. It's simplest to write the pid
1271 * out backwards first, then copy it over.
1273 for (i = 0; pid; i++, pid /= 10)
1274 buf[i] = (pid % 10) + '0';
1275 for (i--; i >= 0; i--)
1276 *(data->pid_envvar++) = buf[i];
1277 *data->pid_envvar = '\0';
1280 if (data->user_setup)
1281 data->user_setup (data->user_setup_data);
1285 notify_desktop_launch (GDBusConnection *session_bus,
1286 GDesktopAppInfo *info,
1288 const char *display,
1293 GVariantBuilder uri_variant;
1294 GVariantBuilder extras_variant;
1296 const char *desktop_file_id;
1297 const char *gio_desktop_file;
1299 if (session_bus == NULL)
1302 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
1303 for (iter = uris; iter; iter = iter->next)
1304 g_variant_builder_add (&uri_variant, "s", iter->data);
1306 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
1307 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
1308 g_variant_builder_add (&extras_variant, "{sv}",
1312 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
1313 if (gio_desktop_file != NULL)
1314 g_variant_builder_add (&extras_variant, "{sv}",
1315 "origin-desktop-file",
1316 g_variant_new_bytestring (gio_desktop_file));
1317 if (g_get_prgname () != NULL)
1318 g_variant_builder_add (&extras_variant, "{sv}",
1320 g_variant_new_bytestring (g_get_prgname ()));
1321 g_variant_builder_add (&extras_variant, "{sv}",
1324 (gint64)getpid ()));
1327 desktop_file_id = info->filename;
1328 else if (info->desktop_id)
1329 desktop_file_id = info->desktop_id;
1331 desktop_file_id = "";
1333 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
1334 "org.gtk.gio.DesktopAppInfo",
1336 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
1337 g_variant_new_bytestring (desktop_file_id),
1338 display ? display : "",
1342 g_dbus_connection_send_message (session_bus,
1346 g_object_unref (msg);
1349 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
1352 g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo *info,
1353 GDBusConnection *session_bus,
1354 const gchar *exec_line,
1356 GAppLaunchContext *launch_context,
1357 GSpawnFlags spawn_flags,
1358 GSpawnChildSetupFunc user_setup,
1359 gpointer user_setup_data,
1360 GDesktopAppLaunchCallback pid_callback,
1361 gpointer pid_callback_data,
1364 gboolean completed = FALSE;
1366 char **argv, **envp;
1368 ChildSetupData data;
1370 g_return_val_if_fail (info != NULL, FALSE);
1375 envp = g_app_launch_context_get_environment (launch_context);
1377 envp = g_get_environ ();
1382 GList *launched_uris;
1384 char *display, *sn_id;
1387 if (!expand_application_parameters (info, exec_line, &uris, &argc, &argv, error))
1390 /* Get the subset of URIs we're launching with this process */
1391 launched_uris = NULL;
1392 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
1393 launched_uris = g_list_prepend (launched_uris, iter->data);
1394 launched_uris = g_list_reverse (launched_uris);
1396 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
1398 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1399 _("Unable to find terminal required for application"));
1403 data.user_setup = user_setup;
1404 data.user_setup_data = user_setup_data;
1408 envp = g_environ_setenv (envp,
1409 "GIO_LAUNCHED_DESKTOP_FILE",
1412 envp = g_environ_setenv (envp,
1413 "GIO_LAUNCHED_DESKTOP_FILE_PID",
1414 "XXXXXXXXXXXXXXXXXXXX", /* filled in child_setup */
1416 data.pid_envvar = (char *)g_environ_getenv (envp, "GIO_LAUNCHED_DESKTOP_FILE_PID");
1420 data.pid_envvar = NULL;
1427 GList *launched_files = create_files_for_uris (launched_uris);
1429 display = g_app_launch_context_get_display (launch_context,
1433 envp = g_environ_setenv (envp, "DISPLAY", display, TRUE);
1435 if (info->startup_notify)
1437 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
1440 envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
1443 g_list_free_full (launched_files, g_object_unref);
1446 if (!g_spawn_async (info->path,
1456 g_app_launch_context_launch_failed (launch_context, sn_id);
1460 g_list_free (launched_uris);
1465 if (pid_callback != NULL)
1466 pid_callback (info, pid, pid_callback_data);
1468 if (launch_context != NULL)
1470 GVariantBuilder builder;
1471 GVariant *platform_data;
1473 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
1474 g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (pid));
1476 g_variant_builder_add (&builder, "{sv}", "startup-notification-id", g_variant_new_string (sn_id));
1477 platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
1478 g_signal_emit_by_name (launch_context, "launched", info, platform_data);
1479 g_variant_unref (platform_data);
1482 notify_desktop_launch (session_bus,
1491 g_list_free (launched_uris);
1496 while (uris != NULL);
1508 object_path_from_appid (const gchar *app_id)
1513 n = strlen (app_id);
1514 path = g_malloc (n + 2);
1518 for (i = 0; i < n; i++)
1519 if (app_id[i] != '.')
1520 path[i + 1] = app_id[i];
1530 g_desktop_app_info_make_platform_data (GDesktopAppInfo *info,
1532 GAppLaunchContext *launch_context)
1534 GVariantBuilder builder;
1536 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
1540 GList *launched_files = create_files_for_uris (uris);
1542 if (info->startup_notify)
1546 sn_id = g_app_launch_context_get_startup_notify_id (launch_context, G_APP_INFO (info), launched_files);
1547 g_variant_builder_add (&builder, "{sv}", "desktop-startup-id", g_variant_new_take_string (sn_id));
1550 g_list_free_full (launched_files, g_object_unref);
1553 return g_variant_builder_end (&builder);
1557 g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo *info,
1558 GDBusConnection *session_bus,
1560 GAppLaunchContext *launch_context)
1562 GVariantBuilder builder;
1565 g_return_val_if_fail (info != NULL, FALSE);
1567 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
1573 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
1574 for (iter = uris; iter; iter = iter->next)
1575 g_variant_builder_add (&builder, "s", iter->data);
1576 g_variant_builder_close (&builder);
1579 g_variant_builder_add_value (&builder, g_desktop_app_info_make_platform_data (info, uris, launch_context));
1581 /* This is non-blocking API. Similar to launching via fork()/exec()
1582 * we don't wait around to see if the program crashed during startup.
1583 * This is what startup-notification's job is...
1585 object_path = object_path_from_appid (info->app_id);
1586 g_dbus_connection_call (session_bus, info->app_id, object_path, "org.freedesktop.Application",
1587 uris ? "Open" : "Activate", g_variant_builder_end (&builder),
1588 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
1589 g_free (object_path);
1595 g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
1597 GAppLaunchContext *launch_context,
1598 GSpawnFlags spawn_flags,
1599 GSpawnChildSetupFunc user_setup,
1600 gpointer user_setup_data,
1601 GDesktopAppLaunchCallback pid_callback,
1602 gpointer pid_callback_data,
1605 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1606 GDBusConnection *session_bus;
1607 gboolean success = TRUE;
1609 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
1611 if (session_bus && info->app_id)
1612 g_desktop_app_info_launch_uris_with_dbus (info, session_bus, uris, launch_context);
1614 success = g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec, uris, launch_context,
1615 spawn_flags, user_setup, user_setup_data,
1616 pid_callback, pid_callback_data, error);
1618 if (session_bus != NULL)
1620 /* This asynchronous flush holds a reference until it completes,
1621 * which ensures that the following unref won't immediately kill
1622 * the connection if we were the initial owner.
1624 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
1625 g_object_unref (session_bus);
1632 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
1634 GAppLaunchContext *launch_context,
1637 return g_desktop_app_info_launch_uris_internal (appinfo, uris,
1639 _SPAWN_FLAGS_DEFAULT,
1640 NULL, NULL, NULL, NULL,
1645 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1647 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1649 return info->exec &&
1650 ((strstr (info->exec, "%u") != NULL) ||
1651 (strstr (info->exec, "%U") != NULL));
1655 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1657 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1659 return info->exec &&
1660 ((strstr (info->exec, "%f") != NULL) ||
1661 (strstr (info->exec, "%F") != NULL));
1665 g_desktop_app_info_launch (GAppInfo *appinfo,
1667 GAppLaunchContext *launch_context,
1677 uri = g_file_get_uri (files->data);
1678 uris = g_list_prepend (uris, uri);
1679 files = files->next;
1682 uris = g_list_reverse (uris);
1684 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1686 g_list_free_full (uris, g_free);
1692 * g_desktop_app_info_launch_uris_as_manager:
1693 * @appinfo: a #GDesktopAppInfo
1694 * @uris: (element-type utf8): List of URIs
1695 * @launch_context: a #GAppLaunchContext
1696 * @spawn_flags: #GSpawnFlags, used for each process
1697 * @user_setup: (scope call): a #GSpawnChildSetupFunc, used once for
1699 * @user_setup_data: (closure user_setup): User data for @user_setup
1700 * @pid_callback: (scope call): Callback for child processes
1701 * @pid_callback_data: (closure pid_callback): User data for @callback
1702 * @error: return location for a #GError, or %NULL
1704 * This function performs the equivalent of g_app_info_launch_uris(),
1705 * but is intended primarily for operating system components that
1706 * launch applications. Ordinary applications should use
1707 * g_app_info_launch_uris().
1709 * If the application is launched via traditional UNIX fork()/exec()
1710 * then @spawn_flags, @user_setup and @user_setup_data are used for the
1711 * call to g_spawn_async(). Additionally, @pid_callback (with
1712 * @pid_callback_data) will be called to inform about the PID of the
1715 * If application launching occurs via some other mechanism (eg: D-Bus
1716 * activation) then @spawn_flags, @user_setup, @user_setup_data,
1717 * @pid_callback and @pid_callback_data are ignored.
1719 * Returns: %TRUE on successful launch, %FALSE otherwise.
1722 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
1724 GAppLaunchContext *launch_context,
1725 GSpawnFlags spawn_flags,
1726 GSpawnChildSetupFunc user_setup,
1727 gpointer user_setup_data,
1728 GDesktopAppLaunchCallback pid_callback,
1729 gpointer pid_callback_data,
1732 return g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
1743 /* OnlyShowIn API support {{{2 */
1746 * g_desktop_app_info_set_desktop_env:
1747 * @desktop_env: a string specifying what desktop this is
1749 * Sets the name of the desktop that the application is running in.
1750 * This is used by g_app_info_should_show() and
1751 * g_desktop_app_info_get_show_in() to evaluate the
1752 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1753 * desktop entry fields.
1755 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1756 * Menu specification</ulink> recognizes the following:
1758 * <member>GNOME</member>
1759 * <member>KDE</member>
1760 * <member>ROX</member>
1761 * <member>XFCE</member>
1762 * <member>LXDE</member>
1763 * <member>Unity</member>
1764 * <member>Old</member>
1767 * Should be called only once; subsequent calls are ignored.
1770 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1772 G_LOCK (g_desktop_env);
1774 g_desktop_env = g_strdup (desktop_env);
1775 G_UNLOCK (g_desktop_env);
1779 g_desktop_app_info_should_show (GAppInfo *appinfo)
1781 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1783 if (info->nodisplay)
1786 return g_desktop_app_info_get_show_in (info, NULL);
1789 /* mime types/default apps support {{{2 */
1797 ensure_dir (DirType type,
1800 char *path, *display_name;
1803 if (type == APP_DIR)
1804 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1806 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1809 if (g_mkdir_with_parents (path, 0700) == 0)
1813 display_name = g_filename_display_name (path);
1814 if (type == APP_DIR)
1815 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1816 _("Can't create user application configuration folder %s: %s"),
1817 display_name, g_strerror (errsv));
1819 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1820 _("Can't create user MIME configuration folder %s: %s"),
1821 display_name, g_strerror (errsv));
1823 g_free (display_name);
1830 update_mimeapps_list (const char *desktop_id,
1831 const char *content_type,
1832 UpdateMimeFlags flags,
1835 char *dirname, *filename, *string;
1837 gboolean load_succeeded, res;
1838 char **old_list, **list;
1839 gsize length, data_size;
1842 char **content_types;
1844 /* Don't add both at start and end */
1845 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
1846 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
1848 dirname = ensure_dir (APP_DIR, error);
1852 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1855 key_file = g_key_file_new ();
1856 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1857 if (!load_succeeded ||
1858 (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) &&
1859 !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) &&
1860 !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP)))
1862 g_key_file_free (key_file);
1863 key_file = g_key_file_new ();
1868 content_types = g_new (char *, 2);
1869 content_types[0] = g_strdup (content_type);
1870 content_types[1] = NULL;
1874 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
1877 for (k = 0; content_types && content_types[k]; k++)
1879 /* set as default, if requested so */
1880 string = g_key_file_get_string (key_file,
1881 DEFAULT_APPLICATIONS_GROUP,
1885 if (g_strcmp0 (string, desktop_id) != 0 &&
1886 (flags & UPDATE_MIME_SET_DEFAULT))
1889 string = g_strdup (desktop_id);
1891 /* add in the non-default list too, if it's not already there */
1892 flags |= UPDATE_MIME_SET_NON_DEFAULT;
1895 if (string == NULL || desktop_id == NULL)
1896 g_key_file_remove_key (key_file,
1897 DEFAULT_APPLICATIONS_GROUP,
1901 g_key_file_set_string (key_file,
1902 DEFAULT_APPLICATIONS_GROUP,
1911 /* reuse the list from above */
1915 g_strfreev (content_types);
1916 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1919 for (k = 0; content_types && content_types[k]; k++)
1921 /* Add to the right place in the list */
1924 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1925 content_types[k], &length, NULL);
1927 list = g_new (char *, 1 + length + 1);
1931 /* if we're adding a last-used hint, just put the application in front of the list */
1932 if (flags & UPDATE_MIME_SET_LAST_USED)
1934 /* avoid adding this again as non-default later */
1935 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1936 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1938 list[i++] = g_strdup (desktop_id);
1943 for (j = 0; old_list[j] != NULL; j++)
1945 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1947 /* rewrite other entries if they're different from the new one */
1948 list[i++] = g_strdup (old_list[j]);
1950 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1952 /* we encountered an old entry which is equal to the one we're adding as non-default,
1953 * don't change its position in the list.
1955 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1956 list[i++] = g_strdup (old_list[j]);
1961 /* add it at the end of the list */
1962 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1963 list[i++] = g_strdup (desktop_id);
1967 g_strfreev (old_list);
1969 if (list[0] == NULL || desktop_id == NULL)
1970 g_key_file_remove_key (key_file,
1971 ADDED_ASSOCIATIONS_GROUP,
1975 g_key_file_set_string_list (key_file,
1976 ADDED_ASSOCIATIONS_GROUP,
1978 (const char * const *)list, i);
1985 /* reuse the list from above */
1989 g_strfreev (content_types);
1990 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1993 for (k = 0; content_types && content_types[k]; k++)
1995 /* Remove from removed associations group (unless remove) */
1998 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1999 content_types[k], &length, NULL);
2001 list = g_new (char *, 1 + length + 1);
2004 if (flags & UPDATE_MIME_REMOVE)
2005 list[i++] = g_strdup (desktop_id);
2008 for (j = 0; old_list[j] != NULL; j++)
2010 if (g_strcmp0 (old_list[j], desktop_id) != 0)
2011 list[i++] = g_strdup (old_list[j]);
2016 g_strfreev (old_list);
2018 if (list[0] == NULL || desktop_id == NULL)
2019 g_key_file_remove_key (key_file,
2020 REMOVED_ASSOCIATIONS_GROUP,
2024 g_key_file_set_string_list (key_file,
2025 REMOVED_ASSOCIATIONS_GROUP,
2027 (const char * const *)list, i);
2032 g_strfreev (content_types);
2034 data = g_key_file_to_data (key_file, &data_size, error);
2035 g_key_file_free (key_file);
2037 res = g_file_set_contents (filename, data, data_size, error);
2039 mime_info_cache_reload (NULL);
2048 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
2049 const char *content_type,
2052 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2054 if (!g_desktop_app_info_ensure_saved (info, error))
2057 if (!info->desktop_id)
2059 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2060 _("Application information lacks an identifier"));
2064 /* both add support for the content type and set as last used */
2065 return update_mimeapps_list (info->desktop_id, content_type,
2066 UPDATE_MIME_SET_NON_DEFAULT |
2067 UPDATE_MIME_SET_LAST_USED,
2072 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
2073 const char *content_type,
2076 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2078 if (!g_desktop_app_info_ensure_saved (info, error))
2081 if (!info->desktop_id)
2083 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2084 _("Application information lacks an identifier"));
2088 return update_mimeapps_list (info->desktop_id, content_type,
2089 UPDATE_MIME_SET_DEFAULT,
2094 update_program_done (GPid pid,
2098 /* Did the application exit correctly */
2099 if (g_spawn_check_exit_status (status, NULL))
2101 /* Here we could clean out any caches in use */
2106 run_update_command (char *command,
2115 GError *error = NULL;
2118 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
2120 if (g_spawn_async ("/", argv,
2122 G_SPAWN_SEARCH_PATH |
2123 G_SPAWN_STDOUT_TO_DEV_NULL |
2124 G_SPAWN_STDERR_TO_DEV_NULL |
2125 G_SPAWN_DO_NOT_REAP_CHILD,
2126 NULL, NULL, /* No setup function */
2129 g_child_watch_add (pid, update_program_done, NULL);
2132 /* If we get an error at this point, it's quite likely the user doesn't
2133 * have an installed copy of either 'update-mime-database' or
2134 * 'update-desktop-database'. I don't think we want to popup an error
2135 * dialog at this point, so we just do a g_warning to give the user a
2136 * chance of debugging it.
2138 g_warning ("%s", error->message);
2145 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
2146 const char *extension,
2149 char *filename, *basename, *mimetype;
2153 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
2156 dirname = ensure_dir (MIMETYPE_DIR, error);
2160 basename = g_strdup_printf ("user-extension-%s.xml", extension);
2161 filename = g_build_filename (dirname, basename, NULL);
2165 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
2167 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
2172 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
2173 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
2174 " <mime-type type=\"%s\">\n"
2175 " <comment>%s document</comment>\n"
2176 " <glob pattern=\"*.%s\"/>\n"
2178 "</mime-info>\n", mimetype, extension, extension);
2180 g_file_set_contents (filename, contents, -1, NULL);
2183 run_update_command ("update-mime-database", "mime");
2187 res = g_desktop_app_info_set_as_default_for_type (appinfo,
2197 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
2198 const char *content_type,
2201 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2203 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
2206 return update_mimeapps_list (info->desktop_id, content_type,
2207 UPDATE_MIME_SET_NON_DEFAULT,
2212 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
2218 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
2219 const char *content_type,
2222 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2224 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
2227 return update_mimeapps_list (info->desktop_id, content_type,
2232 static const char **
2233 g_desktop_app_info_get_supported_types (GAppInfo *appinfo)
2235 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2237 return (const char**) info->mime_types;
2240 /* Saving and deleting {{{2 */
2243 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
2249 char *data, *desktop_id;
2254 if (info->filename != NULL)
2257 /* This is only used for object created with
2258 * g_app_info_create_from_commandline. All other
2259 * object should have a filename
2262 dirname = ensure_dir (APP_DIR, error);
2266 key_file = g_key_file_new ();
2268 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2269 "Encoding", "UTF-8");
2270 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2271 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
2272 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2273 G_KEY_FILE_DESKTOP_KEY_TYPE,
2274 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
2276 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2277 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
2278 if (info->nodisplay)
2279 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2280 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2282 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2283 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
2285 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2286 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
2288 if (info->generic_name != NULL)
2289 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2290 GENERIC_NAME_KEY, info->generic_name);
2292 if (info->fullname != NULL)
2293 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2294 FULL_NAME_KEY, info->fullname);
2296 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2297 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
2299 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2300 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2302 data = g_key_file_to_data (key_file, &data_size, NULL);
2303 g_key_file_free (key_file);
2305 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
2306 filename = g_build_filename (dirname, desktop_id, NULL);
2307 g_free (desktop_id);
2310 fd = g_mkstemp (filename);
2315 display_name = g_filename_display_name (filename);
2316 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2317 _("Can't create user desktop file %s"), display_name);
2318 g_free (display_name);
2324 desktop_id = g_path_get_basename (filename);
2326 /* FIXME - actually handle error */
2327 (void) g_close (fd, NULL);
2329 res = g_file_set_contents (filename, data, data_size, error);
2333 g_free (desktop_id);
2338 info->filename = filename;
2339 info->desktop_id = desktop_id;
2341 run_update_command ("update-desktop-database", "applications");
2347 g_desktop_app_info_can_delete (GAppInfo *appinfo)
2349 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2353 if (strstr (info->filename, "/userapp-"))
2354 return g_access (info->filename, W_OK) == 0;
2361 g_desktop_app_info_delete (GAppInfo *appinfo)
2363 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2367 if (g_remove (info->filename) == 0)
2369 update_mimeapps_list (info->desktop_id, NULL,
2373 g_free (info->filename);
2374 info->filename = NULL;
2375 g_free (info->desktop_id);
2376 info->desktop_id = NULL;
2385 /* Create for commandline {{{2 */
2387 * g_app_info_create_from_commandline:
2388 * @commandline: the commandline to use
2389 * @application_name: (allow-none): the application name, or %NULL to use @commandline
2390 * @flags: flags that can specify details of the created #GAppInfo
2391 * @error: a #GError location to store the error occurring, %NULL to ignore.
2393 * Creates a new #GAppInfo from the given information.
2395 * Note that for @commandline, the quoting rules of the Exec key of the
2396 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">freedesktop.org Desktop
2397 * Entry Specification</ulink> are applied. For example, if the @commandline contains
2398 * percent-encoded URIs, the percent-character must be doubled in order to prevent it from
2399 * being swallowed by Exec key unquoting. See the specification for exact quoting rules.
2401 * Returns: (transfer full): new #GAppInfo for given command.
2404 g_app_info_create_from_commandline (const char *commandline,
2405 const char *application_name,
2406 GAppInfoCreateFlags flags,
2411 GDesktopAppInfo *info;
2413 g_return_val_if_fail (commandline, NULL);
2415 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
2417 info->filename = NULL;
2418 info->desktop_id = NULL;
2420 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
2421 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
2422 info->hidden = FALSE;
2423 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
2424 info->exec = g_strconcat (commandline, " %u", NULL);
2426 info->exec = g_strconcat (commandline, " %f", NULL);
2427 info->nodisplay = TRUE;
2428 info->binary = binary_from_exec (info->exec);
2430 if (application_name)
2431 info->name = g_strdup (application_name);
2434 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
2435 split = g_strsplit (commandline, " ", 2);
2436 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
2438 info->name = basename;
2439 if (info->name == NULL)
2440 info->name = g_strdup ("custom");
2442 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
2444 return G_APP_INFO (info);
2447 /* GAppInfo interface init */
2450 g_desktop_app_info_iface_init (GAppInfoIface *iface)
2452 iface->dup = g_desktop_app_info_dup;
2453 iface->equal = g_desktop_app_info_equal;
2454 iface->get_id = g_desktop_app_info_get_id;
2455 iface->get_name = g_desktop_app_info_get_name;
2456 iface->get_description = g_desktop_app_info_get_description;
2457 iface->get_executable = g_desktop_app_info_get_executable;
2458 iface->get_icon = g_desktop_app_info_get_icon;
2459 iface->launch = g_desktop_app_info_launch;
2460 iface->supports_uris = g_desktop_app_info_supports_uris;
2461 iface->supports_files = g_desktop_app_info_supports_files;
2462 iface->launch_uris = g_desktop_app_info_launch_uris;
2463 iface->should_show = g_desktop_app_info_should_show;
2464 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
2465 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
2466 iface->add_supports_type = g_desktop_app_info_add_supports_type;
2467 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
2468 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
2469 iface->can_delete = g_desktop_app_info_can_delete;
2470 iface->do_delete = g_desktop_app_info_delete;
2471 iface->get_commandline = g_desktop_app_info_get_commandline;
2472 iface->get_display_name = g_desktop_app_info_get_display_name;
2473 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
2474 iface->get_supported_types = g_desktop_app_info_get_supported_types;
2477 /* Recommended applications {{{2 */
2480 app_info_in_list (GAppInfo *info,
2483 while (list != NULL)
2485 if (g_app_info_equal (info, list->data))
2493 * g_app_info_get_recommended_for_type:
2494 * @content_type: the content type to find a #GAppInfo for
2496 * Gets a list of recommended #GAppInfos for a given content type, i.e.
2497 * those applications which claim to support the given content type exactly,
2498 * and not by MIME type subclassing.
2499 * Note that the first application of the list is the last used one, i.e.
2500 * the last one for which g_app_info_set_as_last_used_for_type() has been
2503 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2504 * for given @content_type or %NULL on error.
2509 g_app_info_get_recommended_for_type (const gchar *content_type)
2511 GList *desktop_entries, *l;
2513 GDesktopAppInfo *info;
2515 g_return_val_if_fail (content_type != NULL, NULL);
2517 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
2520 for (l = desktop_entries; l != NULL; l = l->next)
2522 char *desktop_entry = l->data;
2524 info = g_desktop_app_info_new (desktop_entry);
2527 if (app_info_in_list (G_APP_INFO (info), infos))
2528 g_object_unref (info);
2530 infos = g_list_prepend (infos, info);
2532 g_free (desktop_entry);
2535 g_list_free (desktop_entries);
2537 return g_list_reverse (infos);
2541 * g_app_info_get_fallback_for_type:
2542 * @content_type: the content type to find a #GAppInfo for
2544 * Gets a list of fallback #GAppInfos for a given content type, i.e.
2545 * those applications which claim to support the given content type
2546 * by MIME type subclassing and not directly.
2548 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2549 * for given @content_type or %NULL on error.
2554 g_app_info_get_fallback_for_type (const gchar *content_type)
2556 GList *desktop_entries, *l;
2557 GList *infos, *recommended_infos;
2558 GDesktopAppInfo *info;
2560 g_return_val_if_fail (content_type != NULL, NULL);
2562 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
2563 recommended_infos = g_app_info_get_recommended_for_type (content_type);
2566 for (l = desktop_entries; l != NULL; l = l->next)
2568 char *desktop_entry = l->data;
2570 info = g_desktop_app_info_new (desktop_entry);
2573 if (app_info_in_list (G_APP_INFO (info), infos) ||
2574 app_info_in_list (G_APP_INFO (info), recommended_infos))
2575 g_object_unref (info);
2577 infos = g_list_prepend (infos, info);
2579 g_free (desktop_entry);
2582 g_list_free (desktop_entries);
2583 g_list_free_full (recommended_infos, g_object_unref);
2585 return g_list_reverse (infos);
2589 * g_app_info_get_all_for_type:
2590 * @content_type: the content type to find a #GAppInfo for
2592 * Gets a list of all #GAppInfos for a given content type,
2593 * including the recommended and fallback #GAppInfos. See
2594 * g_app_info_get_recommended_for_type() and
2595 * g_app_info_get_fallback_for_type().
2597 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2598 * for given @content_type or %NULL on error.
2601 g_app_info_get_all_for_type (const char *content_type)
2603 GList *desktop_entries, *l;
2605 char *user_default = NULL;
2606 GDesktopAppInfo *info;
2608 g_return_val_if_fail (content_type != NULL, NULL);
2610 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2613 /* put the user default in front of the list, for compatibility */
2614 if (user_default != NULL)
2616 info = g_desktop_app_info_new (user_default);
2619 infos = g_list_prepend (infos, info);
2622 g_free (user_default);
2624 for (l = desktop_entries; l != NULL; l = l->next)
2626 char *desktop_entry = l->data;
2628 info = g_desktop_app_info_new (desktop_entry);
2631 if (app_info_in_list (G_APP_INFO (info), infos))
2632 g_object_unref (info);
2634 infos = g_list_prepend (infos, info);
2636 g_free (desktop_entry);
2639 g_list_free (desktop_entries);
2641 return g_list_reverse (infos);
2645 * g_app_info_reset_type_associations:
2646 * @content_type: a content type
2648 * Removes all changes to the type associations done by
2649 * g_app_info_set_as_default_for_type(),
2650 * g_app_info_set_as_default_for_extension(),
2651 * g_app_info_add_supports_type() or
2652 * g_app_info_remove_supports_type().
2657 g_app_info_reset_type_associations (const char *content_type)
2659 update_mimeapps_list (NULL, content_type,
2665 * g_app_info_get_default_for_type:
2666 * @content_type: the content type to find a #GAppInfo for
2667 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
2670 * Gets the default #GAppInfo for a given content type.
2672 * Returns: (transfer full): #GAppInfo for given @content_type or
2676 g_app_info_get_default_for_type (const char *content_type,
2677 gboolean must_support_uris)
2679 GList *desktop_entries, *l;
2680 char *user_default = NULL;
2683 g_return_val_if_fail (content_type != NULL, NULL);
2685 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2689 if (user_default != NULL)
2691 info = (GAppInfo *) g_desktop_app_info_new (user_default);
2695 if (must_support_uris && !g_app_info_supports_uris (info))
2697 g_object_unref (info);
2703 g_free (user_default);
2707 g_list_free_full (desktop_entries, g_free);
2711 /* pick the first from the other list that matches our URI
2714 for (l = desktop_entries; l != NULL; l = l->next)
2716 char *desktop_entry = l->data;
2718 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
2721 if (must_support_uris && !g_app_info_supports_uris (info))
2723 g_object_unref (info);
2731 g_list_free_full (desktop_entries, g_free);
2737 * g_app_info_get_default_for_uri_scheme:
2738 * @uri_scheme: a string containing a URI scheme.
2740 * Gets the default application for handling URIs with
2741 * the given URI scheme. A URI scheme is the initial part
2742 * of the URI, up to but not including the ':', e.g. "http",
2745 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
2748 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
2751 char *content_type, *scheme_down;
2753 scheme_down = g_ascii_strdown (uri_scheme, -1);
2754 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
2755 g_free (scheme_down);
2756 app_info = g_app_info_get_default_for_type (content_type, FALSE);
2757 g_free (content_type);
2763 get_apps_from_dir (GHashTable *apps,
2764 const char *dirname,
2768 const char *basename;
2769 char *filename, *subprefix, *desktop_id;
2771 GDesktopAppInfo *appinfo;
2773 dir = g_dir_open (dirname, 0, NULL);
2776 while ((basename = g_dir_read_name (dir)) != NULL)
2778 filename = g_build_filename (dirname, basename, NULL);
2779 if (g_str_has_suffix (basename, ".desktop"))
2781 desktop_id = g_strconcat (prefix, basename, NULL);
2783 /* Use _extended so we catch NULLs too (hidden) */
2784 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
2786 appinfo = g_desktop_app_info_new_from_filename (filename);
2789 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
2791 g_object_unref (appinfo);
2796 if (appinfo || hidden)
2798 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
2802 /* Reuse instead of strdup here */
2803 appinfo->desktop_id = desktop_id;
2808 g_free (desktop_id);
2812 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
2814 subprefix = g_strconcat (prefix, basename, "-", NULL);
2815 get_apps_from_dir (apps, filename, subprefix);
2825 /* "Get all" API {{{2 */
2828 * g_app_info_get_all:
2830 * Gets a list of all of the applications currently registered
2833 * For desktop files, this includes applications that have
2834 * <literal>NoDisplay=true</literal> set or are excluded from
2835 * display by means of <literal>OnlyShowIn</literal> or
2836 * <literal>NotShowIn</literal>. See g_app_info_should_show().
2837 * The returned list does not include applications which have
2838 * the <literal>Hidden</literal> key set.
2840 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
2843 g_app_info_get_all (void)
2846 GHashTableIter iter;
2851 desktop_file_dirs_refresh ();
2853 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
2857 for (i = 0; i < n_desktop_file_dirs; i++)
2858 get_apps_from_dir (apps, desktop_file_dirs[i].path, "");
2862 g_hash_table_iter_init (&iter, apps);
2863 while (g_hash_table_iter_next (&iter, NULL, &value))
2866 infos = g_list_prepend (infos, value);
2869 g_hash_table_destroy (apps);
2871 return g_list_reverse (infos);
2874 /* Caching of mimeinfo.cache and defaults.list files {{{2 */
2878 GHashTable *mime_info_cache_map;
2879 GHashTable *defaults_list_map;
2880 GHashTable *mimeapps_list_added_map;
2881 GHashTable *mimeapps_list_removed_map;
2882 GHashTable *mimeapps_list_defaults_map;
2883 time_t mime_info_cache_timestamp;
2884 time_t defaults_list_timestamp;
2885 time_t mimeapps_list_timestamp;
2889 GList *dirs; /* mimeinfo.cache and defaults.list */
2890 time_t last_stat_time;
2893 static MimeInfoCache *mime_info_cache = NULL;
2894 G_LOCK_DEFINE_STATIC (mime_info_cache);
2896 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2897 const char *mime_type,
2898 char **new_desktop_file_ids);
2900 static MimeInfoCache * mime_info_cache_new (void);
2903 destroy_info_cache_value (gpointer key,
2907 g_list_free_full (value, g_free);
2911 destroy_info_cache_map (GHashTable *info_cache_map)
2913 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
2914 g_hash_table_destroy (info_cache_map);
2918 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2919 const char *cache_file,
2925 filename = g_build_filename (dir->path, cache_file, NULL);
2927 if (g_stat (filename, &buf) < 0)
2934 if (buf.st_mtime != *timestamp)
2940 /* Call with lock held */
2942 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2946 gchar *filename, **mime_types;
2953 if (dir->mime_info_cache_map != NULL &&
2954 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2955 &dir->mime_info_cache_timestamp))
2958 if (dir->mime_info_cache_map != NULL)
2959 destroy_info_cache_map (dir->mime_info_cache_map);
2961 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2962 (GDestroyNotify) g_free,
2965 key_file = g_key_file_new ();
2967 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2969 if (g_stat (filename, &buf) < 0)
2972 dir->mime_info_cache_timestamp = buf.st_mtime;
2974 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2979 if (load_error != NULL)
2982 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2985 if (load_error != NULL)
2988 for (i = 0; mime_types[i] != NULL; i++)
2990 gchar **desktop_file_ids;
2991 char *unaliased_type;
2992 desktop_file_ids = g_key_file_get_string_list (key_file,
2998 if (desktop_file_ids == NULL)
3001 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3002 mime_info_cache_dir_add_desktop_entries (dir,
3005 g_free (unaliased_type);
3007 g_strfreev (desktop_file_ids);
3010 g_strfreev (mime_types);
3011 g_key_file_free (key_file);
3016 g_key_file_free (key_file);
3018 if (mime_types != NULL)
3019 g_strfreev (mime_types);
3022 g_error_free (load_error);
3026 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
3030 gchar *filename, **mime_types;
3031 char *unaliased_type;
3032 char **desktop_file_ids;
3039 if (dir->defaults_list_map != NULL &&
3040 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
3041 &dir->defaults_list_timestamp))
3044 if (dir->defaults_list_map != NULL)
3045 g_hash_table_destroy (dir->defaults_list_map);
3046 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3047 g_free, (GDestroyNotify)g_strfreev);
3050 key_file = g_key_file_new ();
3052 filename = g_build_filename (dir->path, "defaults.list", NULL);
3053 if (g_stat (filename, &buf) < 0)
3056 dir->defaults_list_timestamp = buf.st_mtime;
3058 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
3062 if (load_error != NULL)
3065 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
3067 if (mime_types != NULL)
3069 for (i = 0; mime_types[i] != NULL; i++)
3071 desktop_file_ids = g_key_file_get_string_list (key_file,
3072 DEFAULT_APPLICATIONS_GROUP,
3076 if (desktop_file_ids == NULL)
3079 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3080 g_hash_table_replace (dir->defaults_list_map,
3085 g_strfreev (mime_types);
3088 g_key_file_free (key_file);
3093 g_key_file_free (key_file);
3095 if (mime_types != NULL)
3096 g_strfreev (mime_types);
3099 g_error_free (load_error);
3103 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
3107 gchar *filename, **mime_types;
3108 char *unaliased_type;
3109 char **desktop_file_ids;
3117 if (dir->mimeapps_list_added_map != NULL &&
3118 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
3119 &dir->mimeapps_list_timestamp))
3122 if (dir->mimeapps_list_added_map != NULL)
3123 g_hash_table_destroy (dir->mimeapps_list_added_map);
3124 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3125 g_free, (GDestroyNotify)g_strfreev);
3127 if (dir->mimeapps_list_removed_map != NULL)
3128 g_hash_table_destroy (dir->mimeapps_list_removed_map);
3129 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3130 g_free, (GDestroyNotify)g_strfreev);
3132 if (dir->mimeapps_list_defaults_map != NULL)
3133 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
3134 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3137 key_file = g_key_file_new ();
3139 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
3140 if (g_stat (filename, &buf) < 0)
3143 dir->mimeapps_list_timestamp = buf.st_mtime;
3145 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
3149 if (load_error != NULL)
3152 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
3154 if (mime_types != NULL)
3156 for (i = 0; mime_types[i] != NULL; i++)
3158 desktop_file_ids = g_key_file_get_string_list (key_file,
3159 ADDED_ASSOCIATIONS_GROUP,
3163 if (desktop_file_ids == NULL)
3166 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3167 g_hash_table_replace (dir->mimeapps_list_added_map,
3172 g_strfreev (mime_types);
3175 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
3177 if (mime_types != NULL)
3179 for (i = 0; mime_types[i] != NULL; i++)
3181 desktop_file_ids = g_key_file_get_string_list (key_file,
3182 REMOVED_ASSOCIATIONS_GROUP,
3186 if (desktop_file_ids == NULL)
3189 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3190 g_hash_table_replace (dir->mimeapps_list_removed_map,
3195 g_strfreev (mime_types);
3198 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
3200 if (mime_types != NULL)
3202 for (i = 0; mime_types[i] != NULL; i++)
3204 desktop_id = g_key_file_get_string (key_file,
3205 DEFAULT_APPLICATIONS_GROUP,
3208 if (desktop_id == NULL)
3211 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3212 g_hash_table_replace (dir->mimeapps_list_defaults_map,
3217 g_strfreev (mime_types);
3220 g_key_file_free (key_file);
3225 g_key_file_free (key_file);
3227 if (mime_types != NULL)
3228 g_strfreev (mime_types);
3231 g_error_free (load_error);
3234 static MimeInfoCacheDir *
3235 mime_info_cache_dir_new (const char *path)
3237 MimeInfoCacheDir *dir;
3239 dir = g_new0 (MimeInfoCacheDir, 1);
3240 dir->path = g_strdup (path);
3246 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
3251 if (dir->mime_info_cache_map != NULL)
3253 destroy_info_cache_map (dir->mime_info_cache_map);
3254 dir->mime_info_cache_map = NULL;
3258 if (dir->defaults_list_map != NULL)
3260 g_hash_table_destroy (dir->defaults_list_map);
3261 dir->defaults_list_map = NULL;
3264 if (dir->mimeapps_list_added_map != NULL)
3266 g_hash_table_destroy (dir->mimeapps_list_added_map);
3267 dir->mimeapps_list_added_map = NULL;
3270 if (dir->mimeapps_list_removed_map != NULL)
3272 g_hash_table_destroy (dir->mimeapps_list_removed_map);
3273 dir->mimeapps_list_removed_map = NULL;
3276 if (dir->mimeapps_list_defaults_map != NULL)
3278 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
3279 dir->mimeapps_list_defaults_map = NULL;
3286 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
3287 const char *mime_type,
3288 char **new_desktop_file_ids)
3290 GList *desktop_file_ids;
3293 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
3296 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
3298 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
3299 desktop_file_ids = g_list_append (desktop_file_ids,
3300 g_strdup (new_desktop_file_ids[i]));
3303 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
3307 mime_info_cache_init_dir_lists (void)
3311 mime_info_cache = mime_info_cache_new ();
3313 desktop_file_dirs_refresh ();
3315 for (i = 0; i < n_desktop_file_dirs; i++)
3317 MimeInfoCacheDir *dir;
3319 dir = mime_info_cache_dir_new (desktop_file_dirs[i].path);
3323 mime_info_cache_dir_init (dir);
3324 mime_info_cache_dir_init_defaults_list (dir);
3325 mime_info_cache_dir_init_mimeapps_list (dir);
3327 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
3333 mime_info_cache_update_dir_lists (void)
3337 tmp = mime_info_cache->dirs;
3341 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
3343 /* No need to do this if we had file monitors... */
3344 mime_info_cache_dir_init (dir);
3345 mime_info_cache_dir_init_defaults_list (dir);
3346 mime_info_cache_dir_init_mimeapps_list (dir);
3353 mime_info_cache_init (void)
3355 G_LOCK (mime_info_cache);
3356 if (mime_info_cache == NULL)
3357 mime_info_cache_init_dir_lists ();
3363 if (now >= mime_info_cache->last_stat_time + 10)
3365 mime_info_cache_update_dir_lists ();
3366 mime_info_cache->last_stat_time = now;
3370 G_UNLOCK (mime_info_cache);
3373 static MimeInfoCache *
3374 mime_info_cache_new (void)
3376 MimeInfoCache *cache;
3378 cache = g_new0 (MimeInfoCache, 1);
3384 mime_info_cache_free (MimeInfoCache *cache)
3389 g_list_free_full (cache->dirs, (GDestroyNotify) mime_info_cache_dir_free);
3394 * mime_info_cache_reload:
3395 * @dir: directory path which needs reloading.
3397 * Reload the mime information for the @dir.
3400 mime_info_cache_reload (const char *dir)
3402 /* FIXME: just reload the dir that needs reloading,
3403 * don't blow the whole cache
3405 if (mime_info_cache != NULL)
3407 G_LOCK (mime_info_cache);
3408 mime_info_cache_free (mime_info_cache);
3409 mime_info_cache = NULL;
3410 G_UNLOCK (mime_info_cache);
3415 append_desktop_entry (GList *list,
3416 const char *desktop_entry,
3417 GList *removed_entries)
3419 /* Add if not already in list, and valid */
3420 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
3421 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
3422 list = g_list_prepend (list, g_strdup (desktop_entry));
3428 * get_all_desktop_entries_for_mime_type:
3429 * @mime_type: a mime type.
3430 * @except: NULL or a strv list
3432 * Returns all the desktop ids for @mime_type. The desktop files
3433 * are listed in an order so that default applications are listed before
3434 * non-default ones, and handlers for inherited mimetypes are listed
3435 * after the base ones.
3437 * Optionally doesn't list the desktop ids given in the @except
3439 * Return value: a #GList containing the desktop ids which claim
3440 * to handle @mime_type.
3443 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
3444 const char **except,
3445 gboolean include_fallback,
3446 char **explicit_default)
3448 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
3449 MimeInfoCacheDir *dir;
3450 char *mime_type, *default_entry = NULL;
3451 char *old_default_entry = NULL;
3454 char **default_entries;
3455 char **removed_associations;
3456 gboolean already_found_handler;
3461 mime_info_cache_init ();
3463 if (include_fallback)
3465 /* collect all ancestors */
3466 mime_types = _g_unix_content_type_get_parents (base_mime_type);
3467 array = g_ptr_array_new ();
3468 for (i = 0; mime_types[i]; i++)
3469 g_ptr_array_add (array, mime_types[i]);
3470 g_free (mime_types);
3471 for (i = 0; i < array->len; i++)
3473 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
3474 for (j = 0; anc[j]; j++)
3476 for (k = 0; k < array->len; k++)
3478 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
3481 if (k == array->len) /* not found */
3482 g_ptr_array_add (array, g_strdup (anc[j]));
3486 g_ptr_array_add (array, NULL);
3487 mime_types = (char **)g_ptr_array_free (array, FALSE);
3491 mime_types = g_malloc0 (2 * sizeof (gchar *));
3492 mime_types[0] = g_strdup (base_mime_type);
3493 mime_types[1] = NULL;
3496 G_LOCK (mime_info_cache);
3498 removed_entries = NULL;
3499 desktop_entries = NULL;
3501 for (i = 0; except != NULL && except[i] != NULL; i++)
3502 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
3504 for (i = 0; mime_types[i] != NULL; i++)
3506 mime_type = mime_types[i];
3508 /* This is true if we already found a handler for a more specific
3509 mimetype. If its set we ignore any defaults for the less specific
3511 already_found_handler = (desktop_entries != NULL);
3513 /* Go through all apps listed in user and system dirs */
3514 for (dir_list = mime_info_cache->dirs;
3516 dir_list = dir_list->next)
3518 dir = dir_list->data;
3520 /* Pick the explicit default application if we got no result earlier
3521 * (ie, for more specific mime types)
3523 if (!already_found_handler)
3525 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
3529 /* Save the default entry if it's the first one we encounter */
3530 if (default_entry == NULL)
3531 default_entry = g_strdup (entry);
3535 /* Then added associations from mimeapps.list */
3536 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
3537 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3538 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3540 /* Then removed associations from mimeapps.list */
3541 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
3542 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
3543 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
3545 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
3546 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
3547 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3549 if (default_entry == NULL && old_default_entry == NULL && !already_found_handler)
3550 old_default_entry = g_strdup (default_entries[j]);
3552 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3556 /* Go through all entries that support the mimetype */
3557 for (dir_list = mime_info_cache->dirs;
3559 dir_list = dir_list->next)
3561 dir = dir_list->data;
3563 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
3564 for (tmp = list; tmp != NULL; tmp = tmp->next)
3565 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
3569 G_UNLOCK (mime_info_cache);
3571 g_strfreev (mime_types);
3573 /* If we have no default from mimeapps.list, take it from
3574 * defaults.list intead.
3576 * If we do have a default from mimeapps.list, free any one that came
3577 * from defaults.list.
3579 if (default_entry == NULL)
3580 default_entry = old_default_entry;
3582 g_free (old_default_entry);
3584 if (explicit_default != NULL)
3585 *explicit_default = default_entry;
3587 g_free (default_entry);
3589 g_list_free_full (removed_entries, g_free);
3591 desktop_entries = g_list_reverse (desktop_entries);
3593 return desktop_entries;
3596 /* GDesktopAppInfoLookup interface {{{2 */
3598 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
3600 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
3601 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
3604 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
3608 /* "Get for mime type" APIs {{{2 */
3611 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
3612 * @lookup: a #GDesktopAppInfoLookup
3613 * @uri_scheme: a string containing a URI scheme.
3615 * Gets the default application for launching applications
3616 * using this URI scheme for a particular GDesktopAppInfoLookup
3619 * The GDesktopAppInfoLookup interface and this function is used
3620 * to implement g_app_info_get_default_for_uri_scheme() backends
3621 * in a GIO module. There is no reason for applications to use it
3622 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
3624 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3626 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
3629 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
3630 const char *uri_scheme)
3632 GDesktopAppInfoLookupIface *iface;
3634 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
3636 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
3638 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
3641 G_GNUC_END_IGNORE_DEPRECATIONS
3643 /* Misc getter APIs {{{2 */
3646 * g_desktop_app_info_get_startup_wm_class:
3647 * @info: a #GDesktopAppInfo that supports startup notify
3649 * Retrieves the StartupWMClass field from @info. This represents the
3650 * WM_CLASS property of the main window of the application, if launched
3653 * Returns: (transfer none): the startup WM class, or %NULL if none is set
3654 * in the desktop file.
3659 g_desktop_app_info_get_startup_wm_class (GDesktopAppInfo *info)
3661 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
3663 return info->startup_wm_class;
3667 * g_desktop_app_info_get_string:
3668 * @info: a #GDesktopAppInfo
3669 * @key: the key to look up
3671 * Looks up a string value in the keyfile backing @info.
3673 * The @key is looked up in the "Desktop Entry" group.
3675 * Returns: a newly allocated string, or %NULL if the key
3681 g_desktop_app_info_get_string (GDesktopAppInfo *info,
3684 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
3686 return g_key_file_get_string (info->keyfile,
3687 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
3691 * g_desktop_app_info_get_boolean:
3692 * @info: a #GDesktopAppInfo
3693 * @key: the key to look up
3695 * Looks up a boolean value in the keyfile backing @info.
3697 * The @key is looked up in the "Desktop Entry" group.
3699 * Returns: the boolean value, or %FALSE if the key
3705 g_desktop_app_info_get_boolean (GDesktopAppInfo *info,
3708 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
3710 return g_key_file_get_boolean (info->keyfile,
3711 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
3715 * g_desktop_app_info_has_key:
3716 * @info: a #GDesktopAppInfo
3717 * @key: the key to look up
3719 * Returns whether @key exists in the "Desktop Entry" group
3720 * of the keyfile backing @info.
3722 * Returns: %TRUE if the @key exists
3727 g_desktop_app_info_has_key (GDesktopAppInfo *info,
3730 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
3732 return g_key_file_has_key (info->keyfile,
3733 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
3736 /* Desktop actions support {{{2 */
3739 * g_desktop_app_info_list_actions:
3740 * @info: a #GDesktopAppInfo
3742 * Returns the list of "additional application actions" supported on the
3743 * desktop file, as per the desktop file specification.
3745 * As per the specification, this is the list of actions that are
3746 * explicitly listed in the "Actions" key of the [Desktop Entry] group.
3748 * Returns: (array zero-terminated=1) (element-type utf8) (transfer none): a list of strings, always non-%NULL
3752 const gchar * const *
3753 g_desktop_app_info_list_actions (GDesktopAppInfo *info)
3755 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
3757 return (const gchar **) info->actions;
3761 app_info_has_action (GDesktopAppInfo *info,
3762 const gchar *action_name)
3766 for (i = 0; info->actions[i]; i++)
3767 if (g_str_equal (info->actions[i], action_name))
3774 * g_desktop_app_info_get_action_name:
3775 * @info: a #GDesktopAppInfo
3776 * @action_name: the name of the action as from
3777 * g_desktop_app_info_list_actions()
3779 * Gets the user-visible display name of the "additional application
3780 * action" specified by @action_name.
3782 * This corresponds to the "Name" key within the keyfile group for the
3785 * Returns: (transfer full): the locale-specific action name
3790 g_desktop_app_info_get_action_name (GDesktopAppInfo *info,
3791 const gchar *action_name)
3796 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
3797 g_return_val_if_fail (action_name != NULL, NULL);
3798 g_return_val_if_fail (app_info_has_action (info, action_name), NULL);
3800 group_name = g_strdup_printf ("Desktop Action %s", action_name);
3801 result = g_key_file_get_locale_string (info->keyfile, group_name, "Name", NULL, NULL);
3802 g_free (group_name);
3804 /* The spec says that the Name field must be given.
3806 * If it's not, let's follow the behaviour of our get_name()
3807 * implementation above and never return %NULL.
3810 result = g_strdup (_("Unnamed"));
3816 * g_desktop_app_info_launch_action:
3817 * @info: a #GDesktopAppInfo
3818 * @action_name: the name of the action as from
3819 * g_desktop_app_info_list_actions()
3820 * @launch_context: (allow-none): a #GAppLaunchContext
3822 * Activates the named application action.
3824 * You may only call this function on action names that were
3825 * returned from g_desktop_app_info_list_actions().
3827 * Note that if the main entry of the desktop file indicates that the
3828 * application supports startup notification, and @launch_context is
3829 * non-%NULL, then startup notification will be used when activating the
3830 * action (and as such, invocation of the action on the receiving side
3831 * must signal the end of startup notification when it is completed).
3832 * This is the expected behaviour of applications declaring additional
3833 * actions, as per the desktop file specification.
3835 * As with g_app_info_launch() there is no way to detect failures that
3836 * occur while using this function.
3841 g_desktop_app_info_launch_action (GDesktopAppInfo *info,
3842 const gchar *action_name,
3843 GAppLaunchContext *launch_context)
3845 GDBusConnection *session_bus;
3847 g_return_if_fail (G_IS_DESKTOP_APP_INFO (info));
3848 g_return_if_fail (action_name != NULL);
3849 g_return_if_fail (app_info_has_action (info, action_name));
3851 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
3853 if (session_bus && info->app_id)
3857 object_path = object_path_from_appid (info->app_id);
3858 g_dbus_connection_call (session_bus, info->app_id, object_path,
3859 "org.freedesktop.Application", "ActivateAction",
3860 g_variant_new ("(sav@a{sv})", action_name, NULL,
3861 g_desktop_app_info_make_platform_data (info, NULL, launch_context)),
3862 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
3863 g_free (object_path);
3870 group_name = g_strdup_printf ("Desktop Action %s", action_name);
3871 exec_line = g_key_file_get_string (info->keyfile, group_name, "Exec", NULL);
3872 g_free (group_name);
3875 g_desktop_app_info_launch_uris_with_spawn (info, session_bus, exec_line, NULL, launch_context,
3876 _SPAWN_FLAGS_DEFAULT, NULL, NULL, NULL, NULL, NULL);
3879 if (session_bus != NULL)
3881 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
3882 g_object_unref (session_bus);
3887 /* vim:set foldmethod=marker: */