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 "gappinfoprivate.h"
51 #include "glocaldirectorymonitor.h"
55 * SECTION:gdesktopappinfo
56 * @title: GDesktopAppInfo
57 * @short_description: Application information from desktop files
58 * @include: gio/gdesktopappinfo.h
60 * #GDesktopAppInfo is an implementation of #GAppInfo based on
63 * Note that <filename><gio/gdesktopappinfo.h></filename> belongs to
64 * the UNIX-specific GIO interfaces, thus you have to use the
65 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
68 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
69 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
70 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
71 #define MIME_CACHE_GROUP "MIME Cache"
72 #define GENERIC_NAME_KEY "GenericName"
73 #define FULL_NAME_KEY "X-GNOME-FullName"
74 #define KEYWORDS_KEY "Keywords"
75 #define STARTUP_WM_CLASS_KEY "StartupWMClass"
82 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
83 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type,
85 gboolean include_fallback,
86 char **explicit_default);
87 static void mime_info_cache_reload (const char *dir);
88 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
94 * Information about an installed application from a desktop file.
96 struct _GDesktopAppInfo
98 GObject parent_instance;
120 char *startup_wm_class;
127 guint startup_notify : 1;
132 UPDATE_MIME_NONE = 1 << 0,
133 UPDATE_MIME_SET_DEFAULT = 1 << 1,
134 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
135 UPDATE_MIME_REMOVE = 1 << 3,
136 UPDATE_MIME_SET_LAST_USED = 1 << 4,
139 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
140 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO, g_desktop_app_info_iface_init))
142 G_LOCK_DEFINE_STATIC (g_desktop_env);
143 static gchar *g_desktop_env = NULL;
145 /* DesktopFileDir implementation {{{1 */
150 GLocalDirectoryMonitor *monitor;
151 GHashTable *app_names;
155 static DesktopFileDir *desktop_file_dirs;
156 static guint n_desktop_file_dirs;
157 static GMutex desktop_file_dir_lock;
159 /* Monitor 'changed' signal handler {{{2 */
160 static void desktop_file_dir_reset (DesktopFileDir *dir);
163 desktop_file_dir_changed (GFileMonitor *monitor,
166 GFileMonitorEvent event_type,
169 DesktopFileDir *dir = user_data;
171 /* We are not interested in receiving notifications forever just
172 * because someone asked about one desktop file once.
174 * After we receive the first notification, reset the dir, destroying
175 * the monitor. We will take this as a hint, next time that we are
176 * asked, that we need to check if everything is up to date.
178 g_mutex_lock (&desktop_file_dir_lock);
180 desktop_file_dir_reset (dir);
182 g_mutex_unlock (&desktop_file_dir_lock);
184 /* Notify anyone else who may be interested */
185 g_app_info_monitor_fire ();
188 /* Internal utility functions {{{2 */
191 * desktop_file_dir_app_name_is_masked:
192 * @dir: a #DesktopFileDir
193 * @app_name: an application ID
195 * Checks if @app_name is masked for @dir.
197 * An application is masked if a similarly-named desktop file exists in
198 * a desktop file directory with higher precedence. Masked desktop
199 * files should be ignored.
202 desktop_file_dir_app_name_is_masked (DesktopFileDir *dir,
203 const gchar *app_name)
205 while (dir > desktop_file_dirs)
209 if (dir->app_names && g_hash_table_contains (dir->app_names, app_name))
217 * add_to_table_if_appropriate:
218 * @apps: a string to GDesktopAppInfo hash table
219 * @app_name: the name of the application
220 * @info: a #GDesktopAppInfo, or NULL
222 * If @info is non-%NULL and non-hidden, then add it to @apps, using
223 * @app_name as a key.
225 * If @info is non-%NULL then this function will consume the passed-in
229 add_to_table_if_appropriate (GHashTable *apps,
230 const gchar *app_name,
231 GDesktopAppInfo *info)
238 g_object_unref (info);
242 g_free (info->desktop_id);
243 info->desktop_id = g_strdup (app_name);
245 g_hash_table_insert (apps, g_strdup (info->desktop_id), info);
248 /* Support for unindexed DesktopFileDirs {{{2 */
250 get_apps_from_dir (GHashTable **apps,
254 const char *basename;
257 dir = g_dir_open (dirname, 0, NULL);
262 while ((basename = g_dir_read_name (dir)) != NULL)
266 filename = g_build_filename (dirname, basename, NULL);
268 if (g_str_has_suffix (basename, ".desktop"))
272 app_name = g_strconcat (prefix, basename, NULL);
275 *apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
277 g_hash_table_insert (*apps, app_name, g_strdup (filename));
279 else if (g_file_test (filename, G_FILE_TEST_IS_DIR))
283 subprefix = g_strconcat (prefix, basename, "-", NULL);
284 get_apps_from_dir (apps, filename, subprefix);
295 desktop_file_dir_unindexed_init (DesktopFileDir *dir)
297 get_apps_from_dir (&dir->app_names, dir->path, "");
300 static GDesktopAppInfo *
301 desktop_file_dir_unindexed_get_app (DesktopFileDir *dir,
302 const gchar *desktop_id)
304 const gchar *filename;
306 filename = g_hash_table_lookup (dir->app_names, desktop_id);
311 return g_desktop_app_info_new_from_filename (filename);
315 desktop_file_dir_unindexed_get_all (DesktopFileDir *dir,
322 if (dir->app_names == NULL)
325 g_hash_table_iter_init (&iter, dir->app_names);
326 while (g_hash_table_iter_next (&iter, &app_name, &filename))
328 if (desktop_file_dir_app_name_is_masked (dir, app_name))
331 add_to_table_if_appropriate (apps, app_name, g_desktop_app_info_new_from_filename (filename));
335 /* DesktopFileDir "API" {{{2 */
338 * desktop_file_dir_create:
339 * @array: the #GArray to add a new item to
340 * @data_dir: an XDG_DATA_DIR
342 * Creates a #DesktopFileDir for the corresponding @data_dir, adding it
346 desktop_file_dir_create (GArray *array,
347 const gchar *data_dir)
349 DesktopFileDir dir = { 0, };
351 dir.path = g_build_filename (data_dir, "applications", NULL);
353 g_array_append_val (array, dir);
357 * desktop_file_dir_reset:
358 * @dir: a #DesktopFileDir
360 * Cleans up @dir, releasing most resources that it was using.
363 desktop_file_dir_reset (DesktopFileDir *dir)
367 g_signal_handlers_disconnect_by_func (dir->monitor, desktop_file_dir_changed, dir);
368 g_object_unref (dir->monitor);
374 g_hash_table_unref (dir->app_names);
375 dir->app_names = NULL;
378 dir->is_setup = FALSE;
382 * desktop_file_dir_init:
383 * @dir: a #DesktopFileDir
385 * Does initial setup for @dir
387 * You should only call this if @dir is not already setup.
390 desktop_file_dir_init (DesktopFileDir *dir)
392 g_assert (!dir->is_setup);
394 g_assert (!dir->monitor);
395 dir->monitor = g_local_directory_monitor_new_in_worker (dir->path, G_FILE_MONITOR_NONE, NULL);
399 g_signal_connect (dir->monitor, "changed", G_CALLBACK (desktop_file_dir_changed), dir);
400 g_local_directory_monitor_start (dir->monitor);
403 desktop_file_dir_unindexed_init (dir);
405 dir->is_setup = TRUE;
409 * desktop_file_dir_get_app:
410 * @dir: a DesktopFileDir
411 * @desktop_id: the desktop ID to load
413 * Creates the #GDesktopAppInfo for the given @desktop_id if it exists
414 * within @dir, even if it is hidden.
416 * This function does not check if @desktop_id would be masked by a
417 * directory with higher precedence. The caller must do so.
419 static GDesktopAppInfo *
420 desktop_file_dir_get_app (DesktopFileDir *dir,
421 const gchar *desktop_id)
426 return desktop_file_dir_unindexed_get_app (dir, desktop_id);
430 * desktop_file_dir_get_all:
431 * @dir: a DesktopFileDir
432 * @apps: a #GHashTable<string, GDesktopAppInfo>
434 * Loads all desktop files in @dir and adds them to @apps, careful to
435 * ensure we don't add any files masked by a similarly-named file in a
436 * higher-precedence directory.
439 desktop_file_dir_get_all (DesktopFileDir *dir,
442 desktop_file_dir_unindexed_get_all (dir, apps);
445 /* Lock/unlock and global setup API {{{2 */
448 desktop_file_dirs_lock (void)
452 g_mutex_lock (&desktop_file_dir_lock);
454 if (desktop_file_dirs == NULL)
456 const char * const *data_dirs;
460 tmp = g_array_new (FALSE, FALSE, sizeof (DesktopFileDir));
462 /* Highest priority: the user's ~/.local/share/applications */
463 desktop_file_dir_create (tmp, g_get_user_data_dir ());
465 /* Following that, XDG_DATA_DIRS/applications, in order */
466 data_dirs = g_get_system_data_dirs ();
467 for (i = 0; data_dirs[i]; i++)
468 desktop_file_dir_create (tmp, data_dirs[i]);
470 desktop_file_dirs = (DesktopFileDir *) tmp->data;
471 n_desktop_file_dirs = tmp->len;
473 g_array_free (tmp, FALSE);
476 for (i = 0; i < n_desktop_file_dirs; i++)
477 if (!desktop_file_dirs[i].is_setup)
478 desktop_file_dir_init (&desktop_file_dirs[i]);
482 desktop_file_dirs_unlock (void)
484 g_mutex_unlock (&desktop_file_dir_lock);
488 desktop_file_dirs_refresh (void)
490 desktop_file_dirs_lock ();
491 desktop_file_dirs_unlock ();
495 desktop_file_dirs_invalidate_user (void)
497 g_mutex_lock (&desktop_file_dir_lock);
499 if (n_desktop_file_dirs)
500 desktop_file_dir_reset (&desktop_file_dirs[0]);
502 g_mutex_unlock (&desktop_file_dir_lock);
505 /* GDesktopAppInfo implementation {{{1 */
506 /* GObject implementation {{{2 */
508 g_desktop_app_info_finalize (GObject *object)
510 GDesktopAppInfo *info;
512 info = G_DESKTOP_APP_INFO (object);
514 g_free (info->desktop_id);
515 g_free (info->filename);
518 g_key_file_unref (info->keyfile);
521 g_free (info->generic_name);
522 g_free (info->fullname);
523 g_free (info->comment);
524 g_free (info->icon_name);
526 g_object_unref (info->icon);
527 g_strfreev (info->keywords);
528 g_strfreev (info->only_show_in);
529 g_strfreev (info->not_show_in);
530 g_free (info->try_exec);
532 g_free (info->binary);
534 g_free (info->categories);
535 g_free (info->startup_wm_class);
536 g_strfreev (info->mime_types);
537 g_free (info->app_id);
538 g_strfreev (info->actions);
540 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
544 g_desktop_app_info_set_property (GObject *object,
549 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
554 self->filename = g_value_dup_string (value);
558 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
564 g_desktop_app_info_get_property (GObject *object,
569 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
574 g_value_set_string (value, self->filename);
577 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
583 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
585 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
587 gobject_class->get_property = g_desktop_app_info_get_property;
588 gobject_class->set_property = g_desktop_app_info_set_property;
589 gobject_class->finalize = g_desktop_app_info_finalize;
592 * GDesktopAppInfo:filename:
594 * The origin filename of this #GDesktopAppInfo
596 g_object_class_install_property (gobject_class,
598 g_param_spec_string ("filename", "Filename", "", NULL,
599 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
603 g_desktop_app_info_init (GDesktopAppInfo *local)
607 /* Construction... {{{2 */
611 * @exec: an exec line
613 * Returns the first word in an exec line (ie: the binary name).
615 * If @exec is " progname --foo %F" then returns "progname".
618 binary_from_exec (const char *exec)
620 const char *p, *start;
626 while (*p != ' ' && *p != 0)
629 return g_strndup (start, p - start);
633 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
640 gboolean bus_activatable;
642 start_group = g_key_file_get_start_group (key_file);
643 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
645 g_free (start_group);
648 g_free (start_group);
650 type = g_key_file_get_string (key_file,
651 G_KEY_FILE_DESKTOP_GROUP,
652 G_KEY_FILE_DESKTOP_KEY_TYPE,
654 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
661 try_exec = g_key_file_get_string (key_file,
662 G_KEY_FILE_DESKTOP_GROUP,
663 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
665 if (try_exec && try_exec[0] != '\0')
668 t = g_find_program_in_path (try_exec);
677 exec = g_key_file_get_string (key_file,
678 G_KEY_FILE_DESKTOP_GROUP,
679 G_KEY_FILE_DESKTOP_KEY_EXEC,
681 if (exec && exec[0] != '\0')
685 if (!g_shell_parse_argv (exec, &argc, &argv, NULL))
694 t = g_find_program_in_path (argv[0]);
707 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
708 info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
709 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
710 info->keywords = g_key_file_get_locale_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, KEYWORDS_KEY, NULL, NULL, NULL);
711 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
712 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
713 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
714 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);
715 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);
716 info->try_exec = try_exec;
718 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
719 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
720 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
721 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
722 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
723 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
724 info->startup_wm_class = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, STARTUP_WM_CLASS_KEY, NULL);
725 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);
726 bus_activatable = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE, NULL);
727 info->actions = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ACTIONS, NULL, NULL);
729 /* Remove the special-case: no Actions= key just means 0 extra actions */
730 if (info->actions == NULL)
731 info->actions = g_new0 (gchar *, 0 + 1);
736 if (g_path_is_absolute (info->icon_name))
740 file = g_file_new_for_path (info->icon_name);
741 info->icon = g_file_icon_new (file);
742 g_object_unref (file);
748 /* Work around a common mistake in desktop files */
749 if ((p = strrchr (info->icon_name, '.')) != NULL &&
750 (strcmp (p, ".png") == 0 ||
751 strcmp (p, ".xpm") == 0 ||
752 strcmp (p, ".svg") == 0))
755 info->icon = g_themed_icon_new (info->icon_name);
760 info->binary = binary_from_exec (info->exec);
762 if (info->path && info->path[0] == '\0')
768 /* Can only be DBusActivatable if we know the filename, which means
769 * that this won't work for the load-from-keyfile case.
771 if (bus_activatable && info->filename)
776 basename = g_path_get_basename (info->filename);
777 last_dot = strrchr (basename, '.');
779 if (last_dot && g_str_equal (last_dot, ".desktop"))
783 if (g_dbus_is_interface_name (basename))
784 info->app_id = g_strdup (basename);
790 info->keyfile = g_key_file_ref (key_file);
796 g_desktop_app_info_load_file (GDesktopAppInfo *self)
799 gboolean retval = FALSE;
801 g_return_val_if_fail (self->filename != NULL, FALSE);
803 self->desktop_id = g_path_get_basename (self->filename);
805 key_file = g_key_file_new ();
807 if (g_key_file_load_from_file (key_file, self->filename, G_KEY_FILE_NONE, NULL))
808 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
810 g_key_file_unref (key_file);
815 * g_desktop_app_info_new_from_keyfile:
816 * @key_file: an opened #GKeyFile
818 * Creates a new #GDesktopAppInfo.
820 * Returns: a new #GDesktopAppInfo or %NULL on error.
825 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
827 GDesktopAppInfo *info;
829 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
830 info->filename = NULL;
831 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
833 g_object_unref (info);
840 * g_desktop_app_info_new_from_filename:
841 * @filename: the path of a desktop file, in the GLib filename encoding
843 * Creates a new #GDesktopAppInfo.
845 * Returns: a new #GDesktopAppInfo or %NULL on error.
848 g_desktop_app_info_new_from_filename (const char *filename)
850 GDesktopAppInfo *info = NULL;
852 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
853 if (!g_desktop_app_info_load_file (info))
855 g_object_unref (info);
862 * g_desktop_app_info_new:
863 * @desktop_id: the desktop file id
865 * Creates a new #GDesktopAppInfo based on a desktop file id.
867 * A desktop file id is the basename of the desktop file, including the
868 * .desktop extension. GIO is looking for a desktop file with this name
869 * in the <filename>applications</filename> subdirectories of the XDG data
870 * directories (i.e. the directories specified in the
871 * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment
872 * variables). GIO also supports the prefix-to-subdirectory mapping that is
873 * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink>
874 * (i.e. a desktop id of kde-foo.desktop will match
875 * <filename>/usr/share/applications/kde/foo.desktop</filename>).
877 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
880 g_desktop_app_info_new (const char *desktop_id)
882 GDesktopAppInfo *appinfo = NULL;
885 desktop_file_dirs_lock ();
887 for (i = 0; i < n_desktop_file_dirs; i++)
889 appinfo = desktop_file_dir_get_app (&desktop_file_dirs[i], desktop_id);
895 desktop_file_dirs_unlock ();
900 g_free (appinfo->desktop_id);
901 appinfo->desktop_id = g_strdup (desktop_id);
903 if (g_desktop_app_info_get_is_hidden (appinfo))
905 g_object_unref (appinfo);
913 g_desktop_app_info_dup (GAppInfo *appinfo)
915 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
916 GDesktopAppInfo *new_info;
918 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
920 new_info->filename = g_strdup (info->filename);
921 new_info->desktop_id = g_strdup (info->desktop_id);
924 new_info->keyfile = g_key_file_ref (info->keyfile);
926 new_info->name = g_strdup (info->name);
927 new_info->generic_name = g_strdup (info->generic_name);
928 new_info->fullname = g_strdup (info->fullname);
929 new_info->keywords = g_strdupv (info->keywords);
930 new_info->comment = g_strdup (info->comment);
931 new_info->nodisplay = info->nodisplay;
932 new_info->icon_name = g_strdup (info->icon_name);
934 new_info->icon = g_object_ref (info->icon);
935 new_info->only_show_in = g_strdupv (info->only_show_in);
936 new_info->not_show_in = g_strdupv (info->not_show_in);
937 new_info->try_exec = g_strdup (info->try_exec);
938 new_info->exec = g_strdup (info->exec);
939 new_info->binary = g_strdup (info->binary);
940 new_info->path = g_strdup (info->path);
941 new_info->app_id = g_strdup (info->app_id);
942 new_info->hidden = info->hidden;
943 new_info->terminal = info->terminal;
944 new_info->startup_notify = info->startup_notify;
946 return G_APP_INFO (new_info);
949 /* GAppInfo interface implementation functions {{{2 */
952 g_desktop_app_info_equal (GAppInfo *appinfo1,
955 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
956 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
958 if (info1->desktop_id == NULL ||
959 info2->desktop_id == NULL)
960 return info1 == info2;
962 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
966 g_desktop_app_info_get_id (GAppInfo *appinfo)
968 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
970 return info->desktop_id;
974 g_desktop_app_info_get_name (GAppInfo *appinfo)
976 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
978 if (info->name == NULL)
984 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
986 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
988 if (info->fullname == NULL)
989 return g_desktop_app_info_get_name (appinfo);
990 return info->fullname;
994 * g_desktop_app_info_get_is_hidden:
995 * @info: a #GDesktopAppInfo.
997 * A desktop file is hidden if the Hidden key in it is
1000 * Returns: %TRUE if hidden, %FALSE otherwise.
1003 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
1005 return info->hidden;
1009 * g_desktop_app_info_get_filename:
1010 * @info: a #GDesktopAppInfo
1012 * When @info was created from a known filename, return it. In some
1013 * situations such as the #GDesktopAppInfo returned from
1014 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
1016 * Returns: The full path to the file for @info, or %NULL if not known.
1020 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
1022 return info->filename;
1026 g_desktop_app_info_get_description (GAppInfo *appinfo)
1028 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1030 return info->comment;
1034 g_desktop_app_info_get_executable (GAppInfo *appinfo)
1036 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1038 return info->binary;
1042 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
1044 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1050 g_desktop_app_info_get_icon (GAppInfo *appinfo)
1052 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1058 * g_desktop_app_info_get_categories:
1059 * @info: a #GDesktopAppInfo
1061 * Gets the categories from the desktop file.
1063 * Returns: The unparsed Categories key from the desktop file;
1064 * i.e. no attempt is made to split it by ';' or validate it.
1067 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
1069 return info->categories;
1073 * g_desktop_app_info_get_keywords:
1074 * @info: a #GDesktopAppInfo
1076 * Gets the keywords from the desktop file.
1078 * Returns: (transfer none): The value of the Keywords key
1082 const char * const *
1083 g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
1085 return (const char * const *)info->keywords;
1089 * g_desktop_app_info_get_generic_name:
1090 * @info: a #GDesktopAppInfo
1092 * Gets the generic name from the destkop file.
1094 * Returns: The value of the GenericName key
1097 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
1099 return info->generic_name;
1103 * g_desktop_app_info_get_nodisplay:
1104 * @info: a #GDesktopAppInfo
1106 * Gets the value of the NoDisplay key, which helps determine if the
1107 * application info should be shown in menus. See
1108 * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
1110 * Returns: The value of the NoDisplay key
1115 g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
1117 return info->nodisplay;
1121 * g_desktop_app_info_get_show_in:
1122 * @info: a #GDesktopAppInfo
1123 * @desktop_env: a string specifying a desktop name
1125 * Checks if the application info should be shown in menus that list available
1126 * applications for a specific name of the desktop, based on the
1127 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys.
1129 * If @desktop_env is %NULL, then the name of the desktop set with
1130 * g_desktop_app_info_set_desktop_env() is used.
1132 * Note that g_app_info_should_show() for @info will include this check (with
1133 * %NULL for @desktop_env) as well as additional checks.
1135 * Returns: %TRUE if the @info should be shown in @desktop_env according to the
1136 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys, %FALSE
1142 g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
1143 const gchar *desktop_env)
1148 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
1151 G_LOCK (g_desktop_env);
1152 desktop_env = g_desktop_env;
1153 G_UNLOCK (g_desktop_env);
1156 if (info->only_show_in)
1158 if (desktop_env == NULL)
1162 for (i = 0; info->only_show_in[i] != NULL; i++)
1164 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1174 if (info->not_show_in && desktop_env)
1176 for (i = 0; info->not_show_in[i] != NULL; i++)
1178 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1186 /* Launching... {{{2 */
1189 expand_macro_single (char macro, char *uri)
1192 char *result = NULL;
1196 file = g_file_new_for_uri (uri);
1202 result = g_shell_quote (uri);
1206 path = g_file_get_path (file);
1208 result = g_shell_quote (path);
1212 path = g_file_get_path (file);
1215 name = g_path_get_dirname (path);
1216 result = g_shell_quote (name);
1222 path = g_file_get_path (file);
1225 name = g_path_get_basename (path);
1226 result = g_shell_quote (name);
1232 g_object_unref (file);
1239 expand_macro (char macro,
1241 GDesktopAppInfo *info,
1244 GList *uris = *uri_list;
1246 gboolean force_file_uri;
1247 char force_file_uri_macro;
1250 g_return_if_fail (exec != NULL);
1252 /* On %u and %U, pass POSIX file path pointing to the URI via
1253 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
1254 * running or the URI doesn't have a POSIX file path via FUSE
1255 * we'll just pass the URI.
1257 force_file_uri_macro = macro;
1258 force_file_uri = FALSE;
1264 force_file_uri_macro = 'f';
1265 force_file_uri = TRUE;
1268 force_file_uri_macro = 'F';
1269 force_file_uri = TRUE;
1285 if (!force_file_uri ||
1286 /* Pass URI if it contains an anchor */
1287 strchr (uri, '#') != NULL)
1289 expanded = expand_macro_single (macro, uri);
1293 expanded = expand_macro_single (force_file_uri_macro, uri);
1294 if (expanded == NULL)
1295 expanded = expand_macro_single (macro, uri);
1300 g_string_append (exec, expanded);
1316 if (!force_file_uri ||
1317 /* Pass URI if it contains an anchor */
1318 strchr (uri, '#') != NULL)
1320 expanded = expand_macro_single (macro, uri);
1324 expanded = expand_macro_single (force_file_uri_macro, uri);
1325 if (expanded == NULL)
1326 expanded = expand_macro_single (macro, uri);
1331 g_string_append (exec, expanded);
1337 if (uris != NULL && expanded)
1338 g_string_append_c (exec, ' ');
1344 if (info->icon_name)
1346 g_string_append (exec, "--icon ");
1347 expanded = g_shell_quote (info->icon_name);
1348 g_string_append (exec, expanded);
1356 expanded = g_shell_quote (info->name);
1357 g_string_append (exec, expanded);
1365 expanded = g_shell_quote (info->filename);
1366 g_string_append (exec, expanded);
1371 case 'm': /* deprecated */
1375 g_string_append_c (exec, '%');
1383 expand_application_parameters (GDesktopAppInfo *info,
1384 const gchar *exec_line,
1390 GList *uri_list = *uris;
1391 const char *p = exec_line;
1392 GString *expanded_exec;
1395 if (exec_line == NULL)
1397 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1398 _("Desktop file didn't specify Exec field"));
1402 expanded_exec = g_string_new (NULL);
1406 if (p[0] == '%' && p[1] != '\0')
1408 expand_macro (p[1], expanded_exec, info, uris);
1412 g_string_append_c (expanded_exec, *p);
1417 /* No file substitutions */
1418 if (uri_list == *uris && uri_list != NULL)
1420 /* If there is no macro default to %f. This is also what KDE does */
1421 g_string_append_c (expanded_exec, ' ');
1422 expand_macro ('f', expanded_exec, info, uris);
1425 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
1426 g_string_free (expanded_exec, TRUE);
1431 prepend_terminal_to_vector (int *argc,
1438 char **term_argv = NULL;
1443 g_return_val_if_fail (argc != NULL, FALSE);
1444 g_return_val_if_fail (argv != NULL, FALSE);
1452 /* compute size if not given */
1455 for (i = 0; the_argv[i] != NULL; i++)
1461 term_argv = g_new0 (char *, 3);
1463 check = g_find_program_in_path ("gnome-terminal");
1466 term_argv[0] = check;
1467 /* Note that gnome-terminal takes -x and
1468 * as -e in gnome-terminal is broken we use that. */
1469 term_argv[1] = g_strdup ("-x");
1474 check = g_find_program_in_path ("nxterm");
1476 check = g_find_program_in_path ("color-xterm");
1478 check = g_find_program_in_path ("rxvt");
1480 check = g_find_program_in_path ("xterm");
1482 check = g_find_program_in_path ("dtterm");
1485 check = g_strdup ("xterm");
1486 g_warning ("couldn't find a terminal, falling back to xterm");
1488 term_argv[0] = check;
1489 term_argv[1] = g_strdup ("-e");
1492 real_argc = term_argc + *argc;
1493 real_argv = g_new (char *, real_argc + 1);
1495 for (i = 0; i < term_argc; i++)
1496 real_argv[i] = term_argv[i];
1498 for (j = 0; j < *argc; j++, i++)
1499 real_argv[i] = (char *)the_argv[j];
1501 real_argv[i] = NULL;
1507 /* we use g_free here as we sucked all the inner strings
1508 * out from it into real_argv */
1513 #endif /* G_OS_WIN32 */
1517 create_files_for_uris (GList *uris)
1524 for (iter = uris; iter; iter = iter->next)
1526 GFile *file = g_file_new_for_uri ((char *)iter->data);
1527 res = g_list_prepend (res, file);
1530 return g_list_reverse (res);
1535 GSpawnChildSetupFunc user_setup;
1536 gpointer user_setup_data;
1542 child_setup (gpointer user_data)
1544 ChildSetupData *data = user_data;
1546 if (data->pid_envvar)
1548 pid_t pid = getpid ();
1552 /* Write the pid into the space already reserved for it in the
1553 * environment array. We can't use sprintf because it might
1554 * malloc, so we do it by hand. It's simplest to write the pid
1555 * out backwards first, then copy it over.
1557 for (i = 0; pid; i++, pid /= 10)
1558 buf[i] = (pid % 10) + '0';
1559 for (i--; i >= 0; i--)
1560 *(data->pid_envvar++) = buf[i];
1561 *data->pid_envvar = '\0';
1564 if (data->user_setup)
1565 data->user_setup (data->user_setup_data);
1569 notify_desktop_launch (GDBusConnection *session_bus,
1570 GDesktopAppInfo *info,
1572 const char *display,
1577 GVariantBuilder uri_variant;
1578 GVariantBuilder extras_variant;
1580 const char *desktop_file_id;
1581 const char *gio_desktop_file;
1583 if (session_bus == NULL)
1586 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
1587 for (iter = uris; iter; iter = iter->next)
1588 g_variant_builder_add (&uri_variant, "s", iter->data);
1590 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
1591 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
1592 g_variant_builder_add (&extras_variant, "{sv}",
1596 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
1597 if (gio_desktop_file != NULL)
1598 g_variant_builder_add (&extras_variant, "{sv}",
1599 "origin-desktop-file",
1600 g_variant_new_bytestring (gio_desktop_file));
1601 if (g_get_prgname () != NULL)
1602 g_variant_builder_add (&extras_variant, "{sv}",
1604 g_variant_new_bytestring (g_get_prgname ()));
1605 g_variant_builder_add (&extras_variant, "{sv}",
1608 (gint64)getpid ()));
1611 desktop_file_id = info->filename;
1612 else if (info->desktop_id)
1613 desktop_file_id = info->desktop_id;
1615 desktop_file_id = "";
1617 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
1618 "org.gtk.gio.DesktopAppInfo",
1620 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
1621 g_variant_new_bytestring (desktop_file_id),
1622 display ? display : "",
1626 g_dbus_connection_send_message (session_bus,
1630 g_object_unref (msg);
1633 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
1636 g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo *info,
1637 GDBusConnection *session_bus,
1638 const gchar *exec_line,
1640 GAppLaunchContext *launch_context,
1641 GSpawnFlags spawn_flags,
1642 GSpawnChildSetupFunc user_setup,
1643 gpointer user_setup_data,
1644 GDesktopAppLaunchCallback pid_callback,
1645 gpointer pid_callback_data,
1648 gboolean completed = FALSE;
1650 char **argv, **envp;
1652 ChildSetupData data;
1654 g_return_val_if_fail (info != NULL, FALSE);
1659 envp = g_app_launch_context_get_environment (launch_context);
1661 envp = g_get_environ ();
1666 GList *launched_uris;
1668 char *display, *sn_id;
1671 if (!expand_application_parameters (info, exec_line, &uris, &argc, &argv, error))
1674 /* Get the subset of URIs we're launching with this process */
1675 launched_uris = NULL;
1676 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
1677 launched_uris = g_list_prepend (launched_uris, iter->data);
1678 launched_uris = g_list_reverse (launched_uris);
1680 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
1682 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1683 _("Unable to find terminal required for application"));
1687 data.user_setup = user_setup;
1688 data.user_setup_data = user_setup_data;
1692 envp = g_environ_setenv (envp,
1693 "GIO_LAUNCHED_DESKTOP_FILE",
1696 envp = g_environ_setenv (envp,
1697 "GIO_LAUNCHED_DESKTOP_FILE_PID",
1698 "XXXXXXXXXXXXXXXXXXXX", /* filled in child_setup */
1700 data.pid_envvar = (char *)g_environ_getenv (envp, "GIO_LAUNCHED_DESKTOP_FILE_PID");
1704 data.pid_envvar = NULL;
1711 GList *launched_files = create_files_for_uris (launched_uris);
1713 display = g_app_launch_context_get_display (launch_context,
1717 envp = g_environ_setenv (envp, "DISPLAY", display, TRUE);
1719 if (info->startup_notify)
1721 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
1724 envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
1727 g_list_free_full (launched_files, g_object_unref);
1730 if (!g_spawn_async (info->path,
1740 g_app_launch_context_launch_failed (launch_context, sn_id);
1744 g_list_free (launched_uris);
1749 if (pid_callback != NULL)
1750 pid_callback (info, pid, pid_callback_data);
1752 if (launch_context != NULL)
1754 GVariantBuilder builder;
1755 GVariant *platform_data;
1757 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
1758 g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (pid));
1760 g_variant_builder_add (&builder, "{sv}", "startup-notification-id", g_variant_new_string (sn_id));
1761 platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
1762 g_signal_emit_by_name (launch_context, "launched", info, platform_data);
1763 g_variant_unref (platform_data);
1766 notify_desktop_launch (session_bus,
1775 g_list_free (launched_uris);
1780 while (uris != NULL);
1792 object_path_from_appid (const gchar *app_id)
1797 n = strlen (app_id);
1798 path = g_malloc (n + 2);
1802 for (i = 0; i < n; i++)
1803 if (app_id[i] != '.')
1804 path[i + 1] = app_id[i];
1814 g_desktop_app_info_make_platform_data (GDesktopAppInfo *info,
1816 GAppLaunchContext *launch_context)
1818 GVariantBuilder builder;
1820 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
1824 GList *launched_files = create_files_for_uris (uris);
1826 if (info->startup_notify)
1830 sn_id = g_app_launch_context_get_startup_notify_id (launch_context, G_APP_INFO (info), launched_files);
1831 g_variant_builder_add (&builder, "{sv}", "desktop-startup-id", g_variant_new_take_string (sn_id));
1834 g_list_free_full (launched_files, g_object_unref);
1837 return g_variant_builder_end (&builder);
1841 g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo *info,
1842 GDBusConnection *session_bus,
1844 GAppLaunchContext *launch_context)
1846 GVariantBuilder builder;
1849 g_return_val_if_fail (info != NULL, FALSE);
1851 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
1857 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
1858 for (iter = uris; iter; iter = iter->next)
1859 g_variant_builder_add (&builder, "s", iter->data);
1860 g_variant_builder_close (&builder);
1863 g_variant_builder_add_value (&builder, g_desktop_app_info_make_platform_data (info, uris, launch_context));
1865 /* This is non-blocking API. Similar to launching via fork()/exec()
1866 * we don't wait around to see if the program crashed during startup.
1867 * This is what startup-notification's job is...
1869 object_path = object_path_from_appid (info->app_id);
1870 g_dbus_connection_call (session_bus, info->app_id, object_path, "org.freedesktop.Application",
1871 uris ? "Open" : "Activate", g_variant_builder_end (&builder),
1872 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
1873 g_free (object_path);
1879 g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
1881 GAppLaunchContext *launch_context,
1882 GSpawnFlags spawn_flags,
1883 GSpawnChildSetupFunc user_setup,
1884 gpointer user_setup_data,
1885 GDesktopAppLaunchCallback pid_callback,
1886 gpointer pid_callback_data,
1889 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1890 GDBusConnection *session_bus;
1891 gboolean success = TRUE;
1893 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
1895 if (session_bus && info->app_id)
1896 g_desktop_app_info_launch_uris_with_dbus (info, session_bus, uris, launch_context);
1898 success = g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec, uris, launch_context,
1899 spawn_flags, user_setup, user_setup_data,
1900 pid_callback, pid_callback_data, error);
1902 if (session_bus != NULL)
1904 /* This asynchronous flush holds a reference until it completes,
1905 * which ensures that the following unref won't immediately kill
1906 * the connection if we were the initial owner.
1908 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
1909 g_object_unref (session_bus);
1916 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
1918 GAppLaunchContext *launch_context,
1921 return g_desktop_app_info_launch_uris_internal (appinfo, uris,
1923 _SPAWN_FLAGS_DEFAULT,
1924 NULL, NULL, NULL, NULL,
1929 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1931 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1933 return info->exec &&
1934 ((strstr (info->exec, "%u") != NULL) ||
1935 (strstr (info->exec, "%U") != NULL));
1939 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1941 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1943 return info->exec &&
1944 ((strstr (info->exec, "%f") != NULL) ||
1945 (strstr (info->exec, "%F") != NULL));
1949 g_desktop_app_info_launch (GAppInfo *appinfo,
1951 GAppLaunchContext *launch_context,
1961 uri = g_file_get_uri (files->data);
1962 uris = g_list_prepend (uris, uri);
1963 files = files->next;
1966 uris = g_list_reverse (uris);
1968 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1970 g_list_free_full (uris, g_free);
1976 * g_desktop_app_info_launch_uris_as_manager:
1977 * @appinfo: a #GDesktopAppInfo
1978 * @uris: (element-type utf8): List of URIs
1979 * @launch_context: a #GAppLaunchContext
1980 * @spawn_flags: #GSpawnFlags, used for each process
1981 * @user_setup: (scope call): a #GSpawnChildSetupFunc, used once for
1983 * @user_setup_data: (closure user_setup): User data for @user_setup
1984 * @pid_callback: (scope call): Callback for child processes
1985 * @pid_callback_data: (closure pid_callback): User data for @callback
1986 * @error: return location for a #GError, or %NULL
1988 * This function performs the equivalent of g_app_info_launch_uris(),
1989 * but is intended primarily for operating system components that
1990 * launch applications. Ordinary applications should use
1991 * g_app_info_launch_uris().
1993 * If the application is launched via traditional UNIX fork()/exec()
1994 * then @spawn_flags, @user_setup and @user_setup_data are used for the
1995 * call to g_spawn_async(). Additionally, @pid_callback (with
1996 * @pid_callback_data) will be called to inform about the PID of the
1999 * If application launching occurs via some other mechanism (eg: D-Bus
2000 * activation) then @spawn_flags, @user_setup, @user_setup_data,
2001 * @pid_callback and @pid_callback_data are ignored.
2003 * Returns: %TRUE on successful launch, %FALSE otherwise.
2006 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
2008 GAppLaunchContext *launch_context,
2009 GSpawnFlags spawn_flags,
2010 GSpawnChildSetupFunc user_setup,
2011 gpointer user_setup_data,
2012 GDesktopAppLaunchCallback pid_callback,
2013 gpointer pid_callback_data,
2016 return g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
2027 /* OnlyShowIn API support {{{2 */
2030 * g_desktop_app_info_set_desktop_env:
2031 * @desktop_env: a string specifying what desktop this is
2033 * Sets the name of the desktop that the application is running in.
2034 * This is used by g_app_info_should_show() and
2035 * g_desktop_app_info_get_show_in() to evaluate the
2036 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
2037 * desktop entry fields.
2039 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
2040 * Menu specification</ulink> recognizes the following:
2042 * <member>GNOME</member>
2043 * <member>KDE</member>
2044 * <member>ROX</member>
2045 * <member>XFCE</member>
2046 * <member>LXDE</member>
2047 * <member>Unity</member>
2048 * <member>Old</member>
2051 * Should be called only once; subsequent calls are ignored.
2054 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
2056 G_LOCK (g_desktop_env);
2058 g_desktop_env = g_strdup (desktop_env);
2059 G_UNLOCK (g_desktop_env);
2063 g_desktop_app_info_should_show (GAppInfo *appinfo)
2065 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2067 if (info->nodisplay)
2070 return g_desktop_app_info_get_show_in (info, NULL);
2073 /* mime types/default apps support {{{2 */
2081 ensure_dir (DirType type,
2084 char *path, *display_name;
2087 if (type == APP_DIR)
2088 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
2090 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
2093 if (g_mkdir_with_parents (path, 0700) == 0)
2097 display_name = g_filename_display_name (path);
2098 if (type == APP_DIR)
2099 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
2100 _("Can't create user application configuration folder %s: %s"),
2101 display_name, g_strerror (errsv));
2103 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
2104 _("Can't create user MIME configuration folder %s: %s"),
2105 display_name, g_strerror (errsv));
2107 g_free (display_name);
2114 update_mimeapps_list (const char *desktop_id,
2115 const char *content_type,
2116 UpdateMimeFlags flags,
2119 char *dirname, *filename, *string;
2121 gboolean load_succeeded, res;
2122 char **old_list, **list;
2123 gsize length, data_size;
2126 char **content_types;
2128 /* Don't add both at start and end */
2129 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
2130 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
2132 dirname = ensure_dir (APP_DIR, error);
2136 filename = g_build_filename (dirname, "mimeapps.list", NULL);
2139 key_file = g_key_file_new ();
2140 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
2141 if (!load_succeeded ||
2142 (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) &&
2143 !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) &&
2144 !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP)))
2146 g_key_file_free (key_file);
2147 key_file = g_key_file_new ();
2152 content_types = g_new (char *, 2);
2153 content_types[0] = g_strdup (content_type);
2154 content_types[1] = NULL;
2158 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
2161 for (k = 0; content_types && content_types[k]; k++)
2163 /* set as default, if requested so */
2164 string = g_key_file_get_string (key_file,
2165 DEFAULT_APPLICATIONS_GROUP,
2169 if (g_strcmp0 (string, desktop_id) != 0 &&
2170 (flags & UPDATE_MIME_SET_DEFAULT))
2173 string = g_strdup (desktop_id);
2175 /* add in the non-default list too, if it's not already there */
2176 flags |= UPDATE_MIME_SET_NON_DEFAULT;
2179 if (string == NULL || desktop_id == NULL)
2180 g_key_file_remove_key (key_file,
2181 DEFAULT_APPLICATIONS_GROUP,
2185 g_key_file_set_string (key_file,
2186 DEFAULT_APPLICATIONS_GROUP,
2195 /* reuse the list from above */
2199 g_strfreev (content_types);
2200 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
2203 for (k = 0; content_types && content_types[k]; k++)
2205 /* Add to the right place in the list */
2208 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
2209 content_types[k], &length, NULL);
2211 list = g_new (char *, 1 + length + 1);
2215 /* if we're adding a last-used hint, just put the application in front of the list */
2216 if (flags & UPDATE_MIME_SET_LAST_USED)
2218 /* avoid adding this again as non-default later */
2219 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
2220 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
2222 list[i++] = g_strdup (desktop_id);
2227 for (j = 0; old_list[j] != NULL; j++)
2229 if (g_strcmp0 (old_list[j], desktop_id) != 0)
2231 /* rewrite other entries if they're different from the new one */
2232 list[i++] = g_strdup (old_list[j]);
2234 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
2236 /* we encountered an old entry which is equal to the one we're adding as non-default,
2237 * don't change its position in the list.
2239 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
2240 list[i++] = g_strdup (old_list[j]);
2245 /* add it at the end of the list */
2246 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
2247 list[i++] = g_strdup (desktop_id);
2251 g_strfreev (old_list);
2253 if (list[0] == NULL || desktop_id == NULL)
2254 g_key_file_remove_key (key_file,
2255 ADDED_ASSOCIATIONS_GROUP,
2259 g_key_file_set_string_list (key_file,
2260 ADDED_ASSOCIATIONS_GROUP,
2262 (const char * const *)list, i);
2269 /* reuse the list from above */
2273 g_strfreev (content_types);
2274 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
2277 for (k = 0; content_types && content_types[k]; k++)
2279 /* Remove from removed associations group (unless remove) */
2282 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
2283 content_types[k], &length, NULL);
2285 list = g_new (char *, 1 + length + 1);
2288 if (flags & UPDATE_MIME_REMOVE)
2289 list[i++] = g_strdup (desktop_id);
2292 for (j = 0; old_list[j] != NULL; j++)
2294 if (g_strcmp0 (old_list[j], desktop_id) != 0)
2295 list[i++] = g_strdup (old_list[j]);
2300 g_strfreev (old_list);
2302 if (list[0] == NULL || desktop_id == NULL)
2303 g_key_file_remove_key (key_file,
2304 REMOVED_ASSOCIATIONS_GROUP,
2308 g_key_file_set_string_list (key_file,
2309 REMOVED_ASSOCIATIONS_GROUP,
2311 (const char * const *)list, i);
2316 g_strfreev (content_types);
2318 data = g_key_file_to_data (key_file, &data_size, error);
2319 g_key_file_free (key_file);
2321 res = g_file_set_contents (filename, data, data_size, error);
2323 mime_info_cache_reload (NULL);
2332 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
2333 const char *content_type,
2336 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2338 if (!g_desktop_app_info_ensure_saved (info, error))
2341 if (!info->desktop_id)
2343 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2344 _("Application information lacks an identifier"));
2348 /* both add support for the content type and set as last used */
2349 return update_mimeapps_list (info->desktop_id, content_type,
2350 UPDATE_MIME_SET_NON_DEFAULT |
2351 UPDATE_MIME_SET_LAST_USED,
2356 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
2357 const char *content_type,
2360 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2362 if (!g_desktop_app_info_ensure_saved (info, error))
2365 if (!info->desktop_id)
2367 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2368 _("Application information lacks an identifier"));
2372 return update_mimeapps_list (info->desktop_id, content_type,
2373 UPDATE_MIME_SET_DEFAULT,
2378 update_program_done (GPid pid,
2382 /* Did the application exit correctly */
2383 if (g_spawn_check_exit_status (status, NULL))
2385 /* Here we could clean out any caches in use */
2390 run_update_command (char *command,
2399 GError *error = NULL;
2402 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
2404 if (g_spawn_async ("/", argv,
2406 G_SPAWN_SEARCH_PATH |
2407 G_SPAWN_STDOUT_TO_DEV_NULL |
2408 G_SPAWN_STDERR_TO_DEV_NULL |
2409 G_SPAWN_DO_NOT_REAP_CHILD,
2410 NULL, NULL, /* No setup function */
2413 g_child_watch_add (pid, update_program_done, NULL);
2416 /* If we get an error at this point, it's quite likely the user doesn't
2417 * have an installed copy of either 'update-mime-database' or
2418 * 'update-desktop-database'. I don't think we want to popup an error
2419 * dialog at this point, so we just do a g_warning to give the user a
2420 * chance of debugging it.
2422 g_warning ("%s", error->message);
2429 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
2430 const char *extension,
2433 char *filename, *basename, *mimetype;
2437 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
2440 dirname = ensure_dir (MIMETYPE_DIR, error);
2444 basename = g_strdup_printf ("user-extension-%s.xml", extension);
2445 filename = g_build_filename (dirname, basename, NULL);
2449 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
2451 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
2456 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
2457 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
2458 " <mime-type type=\"%s\">\n"
2459 " <comment>%s document</comment>\n"
2460 " <glob pattern=\"*.%s\"/>\n"
2462 "</mime-info>\n", mimetype, extension, extension);
2464 g_file_set_contents (filename, contents, -1, NULL);
2467 run_update_command ("update-mime-database", "mime");
2471 res = g_desktop_app_info_set_as_default_for_type (appinfo,
2481 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
2482 const char *content_type,
2485 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2487 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
2490 return update_mimeapps_list (info->desktop_id, content_type,
2491 UPDATE_MIME_SET_NON_DEFAULT,
2496 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
2502 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
2503 const char *content_type,
2506 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2508 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
2511 return update_mimeapps_list (info->desktop_id, content_type,
2516 static const char **
2517 g_desktop_app_info_get_supported_types (GAppInfo *appinfo)
2519 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2521 return (const char**) info->mime_types;
2524 /* Saving and deleting {{{2 */
2527 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
2533 char *data, *desktop_id;
2538 if (info->filename != NULL)
2541 /* This is only used for object created with
2542 * g_app_info_create_from_commandline. All other
2543 * object should have a filename
2546 dirname = ensure_dir (APP_DIR, error);
2550 key_file = g_key_file_new ();
2552 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2553 "Encoding", "UTF-8");
2554 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2555 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
2556 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2557 G_KEY_FILE_DESKTOP_KEY_TYPE,
2558 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
2560 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2561 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
2562 if (info->nodisplay)
2563 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2564 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2566 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2567 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
2569 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2570 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
2572 if (info->generic_name != NULL)
2573 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2574 GENERIC_NAME_KEY, info->generic_name);
2576 if (info->fullname != NULL)
2577 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2578 FULL_NAME_KEY, info->fullname);
2580 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2581 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
2583 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2584 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2586 data = g_key_file_to_data (key_file, &data_size, NULL);
2587 g_key_file_free (key_file);
2589 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
2590 filename = g_build_filename (dirname, desktop_id, NULL);
2591 g_free (desktop_id);
2594 fd = g_mkstemp (filename);
2599 display_name = g_filename_display_name (filename);
2600 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2601 _("Can't create user desktop file %s"), display_name);
2602 g_free (display_name);
2608 desktop_id = g_path_get_basename (filename);
2610 /* FIXME - actually handle error */
2611 (void) g_close (fd, NULL);
2613 res = g_file_set_contents (filename, data, data_size, error);
2617 g_free (desktop_id);
2622 info->filename = filename;
2623 info->desktop_id = desktop_id;
2625 run_update_command ("update-desktop-database", "applications");
2627 /* We just dropped a file in the user's desktop file directory. Save
2628 * the monitor the bother of having to notice it and invalidate
2631 * This means that calls directly following this will be able to see
2632 * the results immediately.
2634 desktop_file_dirs_invalidate_user ();
2640 g_desktop_app_info_can_delete (GAppInfo *appinfo)
2642 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2646 if (strstr (info->filename, "/userapp-"))
2647 return g_access (info->filename, W_OK) == 0;
2654 g_desktop_app_info_delete (GAppInfo *appinfo)
2656 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2660 if (g_remove (info->filename) == 0)
2662 update_mimeapps_list (info->desktop_id, NULL,
2666 g_free (info->filename);
2667 info->filename = NULL;
2668 g_free (info->desktop_id);
2669 info->desktop_id = NULL;
2678 /* Create for commandline {{{2 */
2680 * g_app_info_create_from_commandline:
2681 * @commandline: the commandline to use
2682 * @application_name: (allow-none): the application name, or %NULL to use @commandline
2683 * @flags: flags that can specify details of the created #GAppInfo
2684 * @error: a #GError location to store the error occurring, %NULL to ignore.
2686 * Creates a new #GAppInfo from the given information.
2688 * Note that for @commandline, the quoting rules of the Exec key of the
2689 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">freedesktop.org Desktop
2690 * Entry Specification</ulink> are applied. For example, if the @commandline contains
2691 * percent-encoded URIs, the percent-character must be doubled in order to prevent it from
2692 * being swallowed by Exec key unquoting. See the specification for exact quoting rules.
2694 * Returns: (transfer full): new #GAppInfo for given command.
2697 g_app_info_create_from_commandline (const char *commandline,
2698 const char *application_name,
2699 GAppInfoCreateFlags flags,
2704 GDesktopAppInfo *info;
2706 g_return_val_if_fail (commandline, NULL);
2708 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
2710 info->filename = NULL;
2711 info->desktop_id = NULL;
2713 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
2714 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
2715 info->hidden = FALSE;
2716 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
2717 info->exec = g_strconcat (commandline, " %u", NULL);
2719 info->exec = g_strconcat (commandline, " %f", NULL);
2720 info->nodisplay = TRUE;
2721 info->binary = binary_from_exec (info->exec);
2723 if (application_name)
2724 info->name = g_strdup (application_name);
2727 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
2728 split = g_strsplit (commandline, " ", 2);
2729 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
2731 info->name = basename;
2732 if (info->name == NULL)
2733 info->name = g_strdup ("custom");
2735 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
2737 return G_APP_INFO (info);
2740 /* GAppInfo interface init */
2743 g_desktop_app_info_iface_init (GAppInfoIface *iface)
2745 iface->dup = g_desktop_app_info_dup;
2746 iface->equal = g_desktop_app_info_equal;
2747 iface->get_id = g_desktop_app_info_get_id;
2748 iface->get_name = g_desktop_app_info_get_name;
2749 iface->get_description = g_desktop_app_info_get_description;
2750 iface->get_executable = g_desktop_app_info_get_executable;
2751 iface->get_icon = g_desktop_app_info_get_icon;
2752 iface->launch = g_desktop_app_info_launch;
2753 iface->supports_uris = g_desktop_app_info_supports_uris;
2754 iface->supports_files = g_desktop_app_info_supports_files;
2755 iface->launch_uris = g_desktop_app_info_launch_uris;
2756 iface->should_show = g_desktop_app_info_should_show;
2757 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
2758 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
2759 iface->add_supports_type = g_desktop_app_info_add_supports_type;
2760 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
2761 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
2762 iface->can_delete = g_desktop_app_info_can_delete;
2763 iface->do_delete = g_desktop_app_info_delete;
2764 iface->get_commandline = g_desktop_app_info_get_commandline;
2765 iface->get_display_name = g_desktop_app_info_get_display_name;
2766 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
2767 iface->get_supported_types = g_desktop_app_info_get_supported_types;
2770 /* Recommended applications {{{2 */
2773 app_info_in_list (GAppInfo *info,
2776 while (list != NULL)
2778 if (g_app_info_equal (info, list->data))
2786 * g_app_info_get_recommended_for_type:
2787 * @content_type: the content type to find a #GAppInfo for
2789 * Gets a list of recommended #GAppInfos for a given content type, i.e.
2790 * those applications which claim to support the given content type exactly,
2791 * and not by MIME type subclassing.
2792 * Note that the first application of the list is the last used one, i.e.
2793 * the last one for which g_app_info_set_as_last_used_for_type() has been
2796 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2797 * for given @content_type or %NULL on error.
2802 g_app_info_get_recommended_for_type (const gchar *content_type)
2804 GList *desktop_entries, *l;
2806 GDesktopAppInfo *info;
2808 g_return_val_if_fail (content_type != NULL, NULL);
2810 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
2813 for (l = desktop_entries; l != NULL; l = l->next)
2815 char *desktop_entry = l->data;
2817 info = g_desktop_app_info_new (desktop_entry);
2820 if (app_info_in_list (G_APP_INFO (info), infos))
2821 g_object_unref (info);
2823 infos = g_list_prepend (infos, info);
2825 g_free (desktop_entry);
2828 g_list_free (desktop_entries);
2830 return g_list_reverse (infos);
2834 * g_app_info_get_fallback_for_type:
2835 * @content_type: the content type to find a #GAppInfo for
2837 * Gets a list of fallback #GAppInfos for a given content type, i.e.
2838 * those applications which claim to support the given content type
2839 * by MIME type subclassing and not directly.
2841 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2842 * for given @content_type or %NULL on error.
2847 g_app_info_get_fallback_for_type (const gchar *content_type)
2849 GList *desktop_entries, *l;
2850 GList *infos, *recommended_infos;
2851 GDesktopAppInfo *info;
2853 g_return_val_if_fail (content_type != NULL, NULL);
2855 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
2856 recommended_infos = g_app_info_get_recommended_for_type (content_type);
2859 for (l = desktop_entries; l != NULL; l = l->next)
2861 char *desktop_entry = l->data;
2863 info = g_desktop_app_info_new (desktop_entry);
2866 if (app_info_in_list (G_APP_INFO (info), infos) ||
2867 app_info_in_list (G_APP_INFO (info), recommended_infos))
2868 g_object_unref (info);
2870 infos = g_list_prepend (infos, info);
2872 g_free (desktop_entry);
2875 g_list_free (desktop_entries);
2876 g_list_free_full (recommended_infos, g_object_unref);
2878 return g_list_reverse (infos);
2882 * g_app_info_get_all_for_type:
2883 * @content_type: the content type to find a #GAppInfo for
2885 * Gets a list of all #GAppInfos for a given content type,
2886 * including the recommended and fallback #GAppInfos. See
2887 * g_app_info_get_recommended_for_type() and
2888 * g_app_info_get_fallback_for_type().
2890 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2891 * for given @content_type or %NULL on error.
2894 g_app_info_get_all_for_type (const char *content_type)
2896 GList *desktop_entries, *l;
2898 char *user_default = NULL;
2899 GDesktopAppInfo *info;
2901 g_return_val_if_fail (content_type != NULL, NULL);
2903 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2906 /* put the user default in front of the list, for compatibility */
2907 if (user_default != NULL)
2909 info = g_desktop_app_info_new (user_default);
2912 infos = g_list_prepend (infos, info);
2915 g_free (user_default);
2917 for (l = desktop_entries; l != NULL; l = l->next)
2919 char *desktop_entry = l->data;
2921 info = g_desktop_app_info_new (desktop_entry);
2924 if (app_info_in_list (G_APP_INFO (info), infos))
2925 g_object_unref (info);
2927 infos = g_list_prepend (infos, info);
2929 g_free (desktop_entry);
2932 g_list_free (desktop_entries);
2934 return g_list_reverse (infos);
2938 * g_app_info_reset_type_associations:
2939 * @content_type: a content type
2941 * Removes all changes to the type associations done by
2942 * g_app_info_set_as_default_for_type(),
2943 * g_app_info_set_as_default_for_extension(),
2944 * g_app_info_add_supports_type() or
2945 * g_app_info_remove_supports_type().
2950 g_app_info_reset_type_associations (const char *content_type)
2952 update_mimeapps_list (NULL, content_type,
2958 * g_app_info_get_default_for_type:
2959 * @content_type: the content type to find a #GAppInfo for
2960 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
2963 * Gets the default #GAppInfo for a given content type.
2965 * Returns: (transfer full): #GAppInfo for given @content_type or
2969 g_app_info_get_default_for_type (const char *content_type,
2970 gboolean must_support_uris)
2972 GList *desktop_entries, *l;
2973 char *user_default = NULL;
2976 g_return_val_if_fail (content_type != NULL, NULL);
2978 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2982 if (user_default != NULL)
2984 info = (GAppInfo *) g_desktop_app_info_new (user_default);
2988 if (must_support_uris && !g_app_info_supports_uris (info))
2990 g_object_unref (info);
2996 g_free (user_default);
3000 g_list_free_full (desktop_entries, g_free);
3004 /* pick the first from the other list that matches our URI
3007 for (l = desktop_entries; l != NULL; l = l->next)
3009 char *desktop_entry = l->data;
3011 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
3014 if (must_support_uris && !g_app_info_supports_uris (info))
3016 g_object_unref (info);
3024 g_list_free_full (desktop_entries, g_free);
3030 * g_app_info_get_default_for_uri_scheme:
3031 * @uri_scheme: a string containing a URI scheme.
3033 * Gets the default application for handling URIs with
3034 * the given URI scheme. A URI scheme is the initial part
3035 * of the URI, up to but not including the ':', e.g. "http",
3038 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3041 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
3044 char *content_type, *scheme_down;
3046 scheme_down = g_ascii_strdown (uri_scheme, -1);
3047 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
3048 g_free (scheme_down);
3049 app_info = g_app_info_get_default_for_type (content_type, FALSE);
3050 g_free (content_type);
3055 /* "Get all" API {{{2 */
3058 * g_app_info_get_all:
3060 * Gets a list of all of the applications currently registered
3063 * For desktop files, this includes applications that have
3064 * <literal>NoDisplay=true</literal> set or are excluded from
3065 * display by means of <literal>OnlyShowIn</literal> or
3066 * <literal>NotShowIn</literal>. See g_app_info_should_show().
3067 * The returned list does not include applications which have
3068 * the <literal>Hidden</literal> key set.
3070 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
3073 g_app_info_get_all (void)
3076 GHashTableIter iter;
3081 apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
3083 desktop_file_dirs_lock ();
3085 for (i = 0; i < n_desktop_file_dirs; i++)
3086 desktop_file_dir_get_all (&desktop_file_dirs[i], apps);
3088 desktop_file_dirs_unlock ();
3091 g_hash_table_iter_init (&iter, apps);
3092 while (g_hash_table_iter_next (&iter, NULL, &value))
3095 infos = g_list_prepend (infos, value);
3098 g_hash_table_destroy (apps);
3103 /* Caching of mimeinfo.cache and defaults.list files {{{2 */
3107 GHashTable *mime_info_cache_map;
3108 GHashTable *defaults_list_map;
3109 GHashTable *mimeapps_list_added_map;
3110 GHashTable *mimeapps_list_removed_map;
3111 GHashTable *mimeapps_list_defaults_map;
3112 time_t mime_info_cache_timestamp;
3113 time_t defaults_list_timestamp;
3114 time_t mimeapps_list_timestamp;
3118 GList *dirs; /* mimeinfo.cache and defaults.list */
3119 time_t last_stat_time;
3122 static MimeInfoCache *mime_info_cache = NULL;
3123 G_LOCK_DEFINE_STATIC (mime_info_cache);
3125 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
3126 const char *mime_type,
3127 char **new_desktop_file_ids);
3129 static MimeInfoCache * mime_info_cache_new (void);
3132 destroy_info_cache_value (gpointer key,
3136 g_list_free_full (value, g_free);
3140 destroy_info_cache_map (GHashTable *info_cache_map)
3142 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
3143 g_hash_table_destroy (info_cache_map);
3147 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
3148 const char *cache_file,
3154 filename = g_build_filename (dir->path, cache_file, NULL);
3156 if (g_stat (filename, &buf) < 0)
3163 if (buf.st_mtime != *timestamp)
3169 /* Call with lock held */
3171 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
3175 gchar *filename, **mime_types;
3182 if (dir->mime_info_cache_map != NULL &&
3183 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
3184 &dir->mime_info_cache_timestamp))
3187 if (dir->mime_info_cache_map != NULL)
3188 destroy_info_cache_map (dir->mime_info_cache_map);
3190 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3191 (GDestroyNotify) g_free,
3194 key_file = g_key_file_new ();
3196 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
3198 if (g_stat (filename, &buf) < 0)
3201 dir->mime_info_cache_timestamp = buf.st_mtime;
3203 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
3208 if (load_error != NULL)
3211 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
3214 if (load_error != NULL)
3217 for (i = 0; mime_types[i] != NULL; i++)
3219 gchar **desktop_file_ids;
3220 char *unaliased_type;
3221 desktop_file_ids = g_key_file_get_string_list (key_file,
3227 if (desktop_file_ids == NULL)
3230 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3231 mime_info_cache_dir_add_desktop_entries (dir,
3234 g_free (unaliased_type);
3236 g_strfreev (desktop_file_ids);
3239 g_strfreev (mime_types);
3240 g_key_file_free (key_file);
3245 g_key_file_free (key_file);
3247 if (mime_types != NULL)
3248 g_strfreev (mime_types);
3251 g_error_free (load_error);
3255 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
3259 gchar *filename, **mime_types;
3260 char *unaliased_type;
3261 char **desktop_file_ids;
3268 if (dir->defaults_list_map != NULL &&
3269 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
3270 &dir->defaults_list_timestamp))
3273 if (dir->defaults_list_map != NULL)
3274 g_hash_table_destroy (dir->defaults_list_map);
3275 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3276 g_free, (GDestroyNotify)g_strfreev);
3279 key_file = g_key_file_new ();
3281 filename = g_build_filename (dir->path, "defaults.list", NULL);
3282 if (g_stat (filename, &buf) < 0)
3285 dir->defaults_list_timestamp = buf.st_mtime;
3287 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
3291 if (load_error != NULL)
3294 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
3296 if (mime_types != NULL)
3298 for (i = 0; mime_types[i] != NULL; i++)
3300 desktop_file_ids = g_key_file_get_string_list (key_file,
3301 DEFAULT_APPLICATIONS_GROUP,
3305 if (desktop_file_ids == NULL)
3308 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3309 g_hash_table_replace (dir->defaults_list_map,
3314 g_strfreev (mime_types);
3317 g_key_file_free (key_file);
3322 g_key_file_free (key_file);
3324 if (mime_types != NULL)
3325 g_strfreev (mime_types);
3328 g_error_free (load_error);
3332 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
3336 gchar *filename, **mime_types;
3337 char *unaliased_type;
3338 char **desktop_file_ids;
3346 if (dir->mimeapps_list_added_map != NULL &&
3347 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
3348 &dir->mimeapps_list_timestamp))
3351 if (dir->mimeapps_list_added_map != NULL)
3352 g_hash_table_destroy (dir->mimeapps_list_added_map);
3353 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3354 g_free, (GDestroyNotify)g_strfreev);
3356 if (dir->mimeapps_list_removed_map != NULL)
3357 g_hash_table_destroy (dir->mimeapps_list_removed_map);
3358 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3359 g_free, (GDestroyNotify)g_strfreev);
3361 if (dir->mimeapps_list_defaults_map != NULL)
3362 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
3363 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
3366 key_file = g_key_file_new ();
3368 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
3369 if (g_stat (filename, &buf) < 0)
3372 dir->mimeapps_list_timestamp = buf.st_mtime;
3374 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
3378 if (load_error != NULL)
3381 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
3383 if (mime_types != NULL)
3385 for (i = 0; mime_types[i] != NULL; i++)
3387 desktop_file_ids = g_key_file_get_string_list (key_file,
3388 ADDED_ASSOCIATIONS_GROUP,
3392 if (desktop_file_ids == NULL)
3395 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3396 g_hash_table_replace (dir->mimeapps_list_added_map,
3401 g_strfreev (mime_types);
3404 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
3406 if (mime_types != NULL)
3408 for (i = 0; mime_types[i] != NULL; i++)
3410 desktop_file_ids = g_key_file_get_string_list (key_file,
3411 REMOVED_ASSOCIATIONS_GROUP,
3415 if (desktop_file_ids == NULL)
3418 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3419 g_hash_table_replace (dir->mimeapps_list_removed_map,
3424 g_strfreev (mime_types);
3427 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
3429 if (mime_types != NULL)
3431 for (i = 0; mime_types[i] != NULL; i++)
3433 desktop_id = g_key_file_get_string (key_file,
3434 DEFAULT_APPLICATIONS_GROUP,
3437 if (desktop_id == NULL)
3440 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3441 g_hash_table_replace (dir->mimeapps_list_defaults_map,
3446 g_strfreev (mime_types);
3449 g_key_file_free (key_file);
3454 g_key_file_free (key_file);
3456 if (mime_types != NULL)
3457 g_strfreev (mime_types);
3460 g_error_free (load_error);
3463 static MimeInfoCacheDir *
3464 mime_info_cache_dir_new (const char *path)
3466 MimeInfoCacheDir *dir;
3468 dir = g_new0 (MimeInfoCacheDir, 1);
3469 dir->path = g_strdup (path);
3475 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
3480 if (dir->mime_info_cache_map != NULL)
3482 destroy_info_cache_map (dir->mime_info_cache_map);
3483 dir->mime_info_cache_map = NULL;
3487 if (dir->defaults_list_map != NULL)
3489 g_hash_table_destroy (dir->defaults_list_map);
3490 dir->defaults_list_map = NULL;
3493 if (dir->mimeapps_list_added_map != NULL)
3495 g_hash_table_destroy (dir->mimeapps_list_added_map);
3496 dir->mimeapps_list_added_map = NULL;
3499 if (dir->mimeapps_list_removed_map != NULL)
3501 g_hash_table_destroy (dir->mimeapps_list_removed_map);
3502 dir->mimeapps_list_removed_map = NULL;
3505 if (dir->mimeapps_list_defaults_map != NULL)
3507 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
3508 dir->mimeapps_list_defaults_map = NULL;
3515 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
3516 const char *mime_type,
3517 char **new_desktop_file_ids)
3519 GList *desktop_file_ids;
3522 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
3525 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
3527 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
3528 desktop_file_ids = g_list_append (desktop_file_ids,
3529 g_strdup (new_desktop_file_ids[i]));
3532 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
3536 mime_info_cache_init_dir_lists (void)
3540 mime_info_cache = mime_info_cache_new ();
3542 desktop_file_dirs_refresh ();
3544 for (i = 0; i < n_desktop_file_dirs; i++)
3546 MimeInfoCacheDir *dir;
3548 dir = mime_info_cache_dir_new (desktop_file_dirs[i].path);
3552 mime_info_cache_dir_init (dir);
3553 mime_info_cache_dir_init_defaults_list (dir);
3554 mime_info_cache_dir_init_mimeapps_list (dir);
3556 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
3562 mime_info_cache_update_dir_lists (void)
3566 tmp = mime_info_cache->dirs;
3570 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
3572 /* No need to do this if we had file monitors... */
3573 mime_info_cache_dir_init (dir);
3574 mime_info_cache_dir_init_defaults_list (dir);
3575 mime_info_cache_dir_init_mimeapps_list (dir);
3582 mime_info_cache_init (void)
3584 G_LOCK (mime_info_cache);
3585 if (mime_info_cache == NULL)
3586 mime_info_cache_init_dir_lists ();
3592 if (now >= mime_info_cache->last_stat_time + 10)
3594 mime_info_cache_update_dir_lists ();
3595 mime_info_cache->last_stat_time = now;
3599 G_UNLOCK (mime_info_cache);
3602 static MimeInfoCache *
3603 mime_info_cache_new (void)
3605 MimeInfoCache *cache;
3607 cache = g_new0 (MimeInfoCache, 1);
3613 mime_info_cache_free (MimeInfoCache *cache)
3618 g_list_free_full (cache->dirs, (GDestroyNotify) mime_info_cache_dir_free);
3623 * mime_info_cache_reload:
3624 * @dir: directory path which needs reloading.
3626 * Reload the mime information for the @dir.
3629 mime_info_cache_reload (const char *dir)
3631 /* FIXME: just reload the dir that needs reloading,
3632 * don't blow the whole cache
3634 if (mime_info_cache != NULL)
3636 G_LOCK (mime_info_cache);
3637 mime_info_cache_free (mime_info_cache);
3638 mime_info_cache = NULL;
3639 G_UNLOCK (mime_info_cache);
3644 append_desktop_entry (GList *list,
3645 const char *desktop_entry,
3646 GList *removed_entries)
3648 /* Add if not already in list, and valid */
3649 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
3650 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
3651 list = g_list_prepend (list, g_strdup (desktop_entry));
3657 * get_all_desktop_entries_for_mime_type:
3658 * @mime_type: a mime type.
3659 * @except: NULL or a strv list
3661 * Returns all the desktop ids for @mime_type. The desktop files
3662 * are listed in an order so that default applications are listed before
3663 * non-default ones, and handlers for inherited mimetypes are listed
3664 * after the base ones.
3666 * Optionally doesn't list the desktop ids given in the @except
3668 * Return value: a #GList containing the desktop ids which claim
3669 * to handle @mime_type.
3672 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
3673 const char **except,
3674 gboolean include_fallback,
3675 char **explicit_default)
3677 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
3678 MimeInfoCacheDir *dir;
3679 char *mime_type, *default_entry = NULL;
3680 char *old_default_entry = NULL;
3683 char **default_entries;
3684 char **removed_associations;
3685 gboolean already_found_handler;
3690 mime_info_cache_init ();
3692 if (include_fallback)
3694 /* collect all ancestors */
3695 mime_types = _g_unix_content_type_get_parents (base_mime_type);
3696 array = g_ptr_array_new ();
3697 for (i = 0; mime_types[i]; i++)
3698 g_ptr_array_add (array, mime_types[i]);
3699 g_free (mime_types);
3700 for (i = 0; i < array->len; i++)
3702 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
3703 for (j = 0; anc[j]; j++)
3705 for (k = 0; k < array->len; k++)
3707 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
3710 if (k == array->len) /* not found */
3711 g_ptr_array_add (array, g_strdup (anc[j]));
3715 g_ptr_array_add (array, NULL);
3716 mime_types = (char **)g_ptr_array_free (array, FALSE);
3720 mime_types = g_malloc0 (2 * sizeof (gchar *));
3721 mime_types[0] = g_strdup (base_mime_type);
3722 mime_types[1] = NULL;
3725 G_LOCK (mime_info_cache);
3727 removed_entries = NULL;
3728 desktop_entries = NULL;
3730 for (i = 0; except != NULL && except[i] != NULL; i++)
3731 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
3733 for (i = 0; mime_types[i] != NULL; i++)
3735 mime_type = mime_types[i];
3737 /* This is true if we already found a handler for a more specific
3738 mimetype. If its set we ignore any defaults for the less specific
3740 already_found_handler = (desktop_entries != NULL);
3742 /* Go through all apps listed in user and system dirs */
3743 for (dir_list = mime_info_cache->dirs;
3745 dir_list = dir_list->next)
3747 dir = dir_list->data;
3749 /* Pick the explicit default application if we got no result earlier
3750 * (ie, for more specific mime types)
3752 if (!already_found_handler)
3754 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
3758 /* Save the default entry if it's the first one we encounter */
3759 if (default_entry == NULL)
3760 default_entry = g_strdup (entry);
3764 /* Then added associations from mimeapps.list */
3765 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
3766 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3767 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3769 /* Then removed associations from mimeapps.list */
3770 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
3771 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
3772 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
3774 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
3775 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
3776 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3778 if (default_entry == NULL && old_default_entry == NULL && !already_found_handler)
3779 old_default_entry = g_strdup (default_entries[j]);
3781 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3785 /* Go through all entries that support the mimetype */
3786 for (dir_list = mime_info_cache->dirs;
3788 dir_list = dir_list->next)
3790 dir = dir_list->data;
3792 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
3793 for (tmp = list; tmp != NULL; tmp = tmp->next)
3794 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
3798 G_UNLOCK (mime_info_cache);
3800 g_strfreev (mime_types);
3802 /* If we have no default from mimeapps.list, take it from
3803 * defaults.list intead.
3805 * If we do have a default from mimeapps.list, free any one that came
3806 * from defaults.list.
3808 if (default_entry == NULL)
3809 default_entry = old_default_entry;
3811 g_free (old_default_entry);
3813 if (explicit_default != NULL)
3814 *explicit_default = default_entry;
3816 g_free (default_entry);
3818 g_list_free_full (removed_entries, g_free);
3820 desktop_entries = g_list_reverse (desktop_entries);
3822 return desktop_entries;
3825 /* GDesktopAppInfoLookup interface {{{2 */
3827 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
3829 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
3830 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
3833 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
3837 /* "Get for mime type" APIs {{{2 */
3840 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
3841 * @lookup: a #GDesktopAppInfoLookup
3842 * @uri_scheme: a string containing a URI scheme.
3844 * Gets the default application for launching applications
3845 * using this URI scheme for a particular GDesktopAppInfoLookup
3848 * The GDesktopAppInfoLookup interface and this function is used
3849 * to implement g_app_info_get_default_for_uri_scheme() backends
3850 * in a GIO module. There is no reason for applications to use it
3851 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
3853 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3855 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
3858 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
3859 const char *uri_scheme)
3861 GDesktopAppInfoLookupIface *iface;
3863 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
3865 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
3867 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
3870 G_GNUC_END_IGNORE_DEPRECATIONS
3872 /* Misc getter APIs {{{2 */
3875 * g_desktop_app_info_get_startup_wm_class:
3876 * @info: a #GDesktopAppInfo that supports startup notify
3878 * Retrieves the StartupWMClass field from @info. This represents the
3879 * WM_CLASS property of the main window of the application, if launched
3882 * Returns: (transfer none): the startup WM class, or %NULL if none is set
3883 * in the desktop file.
3888 g_desktop_app_info_get_startup_wm_class (GDesktopAppInfo *info)
3890 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
3892 return info->startup_wm_class;
3896 * g_desktop_app_info_get_string:
3897 * @info: a #GDesktopAppInfo
3898 * @key: the key to look up
3900 * Looks up a string value in the keyfile backing @info.
3902 * The @key is looked up in the "Desktop Entry" group.
3904 * Returns: a newly allocated string, or %NULL if the key
3910 g_desktop_app_info_get_string (GDesktopAppInfo *info,
3913 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
3915 return g_key_file_get_string (info->keyfile,
3916 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
3920 * g_desktop_app_info_get_boolean:
3921 * @info: a #GDesktopAppInfo
3922 * @key: the key to look up
3924 * Looks up a boolean value in the keyfile backing @info.
3926 * The @key is looked up in the "Desktop Entry" group.
3928 * Returns: the boolean value, or %FALSE if the key
3934 g_desktop_app_info_get_boolean (GDesktopAppInfo *info,
3937 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
3939 return g_key_file_get_boolean (info->keyfile,
3940 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
3944 * g_desktop_app_info_has_key:
3945 * @info: a #GDesktopAppInfo
3946 * @key: the key to look up
3948 * Returns whether @key exists in the "Desktop Entry" group
3949 * of the keyfile backing @info.
3951 * Returns: %TRUE if the @key exists
3956 g_desktop_app_info_has_key (GDesktopAppInfo *info,
3959 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
3961 return g_key_file_has_key (info->keyfile,
3962 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
3965 /* Desktop actions support {{{2 */
3968 * g_desktop_app_info_list_actions:
3969 * @info: a #GDesktopAppInfo
3971 * Returns the list of "additional application actions" supported on the
3972 * desktop file, as per the desktop file specification.
3974 * As per the specification, this is the list of actions that are
3975 * explicitly listed in the "Actions" key of the [Desktop Entry] group.
3977 * Returns: (array zero-terminated=1) (element-type utf8) (transfer none): a list of strings, always non-%NULL
3981 const gchar * const *
3982 g_desktop_app_info_list_actions (GDesktopAppInfo *info)
3984 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
3986 return (const gchar **) info->actions;
3990 app_info_has_action (GDesktopAppInfo *info,
3991 const gchar *action_name)
3995 for (i = 0; info->actions[i]; i++)
3996 if (g_str_equal (info->actions[i], action_name))
4003 * g_desktop_app_info_get_action_name:
4004 * @info: a #GDesktopAppInfo
4005 * @action_name: the name of the action as from
4006 * g_desktop_app_info_list_actions()
4008 * Gets the user-visible display name of the "additional application
4009 * action" specified by @action_name.
4011 * This corresponds to the "Name" key within the keyfile group for the
4014 * Returns: (transfer full): the locale-specific action name
4019 g_desktop_app_info_get_action_name (GDesktopAppInfo *info,
4020 const gchar *action_name)
4025 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4026 g_return_val_if_fail (action_name != NULL, NULL);
4027 g_return_val_if_fail (app_info_has_action (info, action_name), NULL);
4029 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4030 result = g_key_file_get_locale_string (info->keyfile, group_name, "Name", NULL, NULL);
4031 g_free (group_name);
4033 /* The spec says that the Name field must be given.
4035 * If it's not, let's follow the behaviour of our get_name()
4036 * implementation above and never return %NULL.
4039 result = g_strdup (_("Unnamed"));
4045 * g_desktop_app_info_launch_action:
4046 * @info: a #GDesktopAppInfo
4047 * @action_name: the name of the action as from
4048 * g_desktop_app_info_list_actions()
4049 * @launch_context: (allow-none): a #GAppLaunchContext
4051 * Activates the named application action.
4053 * You may only call this function on action names that were
4054 * returned from g_desktop_app_info_list_actions().
4056 * Note that if the main entry of the desktop file indicates that the
4057 * application supports startup notification, and @launch_context is
4058 * non-%NULL, then startup notification will be used when activating the
4059 * action (and as such, invocation of the action on the receiving side
4060 * must signal the end of startup notification when it is completed).
4061 * This is the expected behaviour of applications declaring additional
4062 * actions, as per the desktop file specification.
4064 * As with g_app_info_launch() there is no way to detect failures that
4065 * occur while using this function.
4070 g_desktop_app_info_launch_action (GDesktopAppInfo *info,
4071 const gchar *action_name,
4072 GAppLaunchContext *launch_context)
4074 GDBusConnection *session_bus;
4076 g_return_if_fail (G_IS_DESKTOP_APP_INFO (info));
4077 g_return_if_fail (action_name != NULL);
4078 g_return_if_fail (app_info_has_action (info, action_name));
4080 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
4082 if (session_bus && info->app_id)
4086 object_path = object_path_from_appid (info->app_id);
4087 g_dbus_connection_call (session_bus, info->app_id, object_path,
4088 "org.freedesktop.Application", "ActivateAction",
4089 g_variant_new ("(sav@a{sv})", action_name, NULL,
4090 g_desktop_app_info_make_platform_data (info, NULL, launch_context)),
4091 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
4092 g_free (object_path);
4099 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4100 exec_line = g_key_file_get_string (info->keyfile, group_name, "Exec", NULL);
4101 g_free (group_name);
4104 g_desktop_app_info_launch_uris_with_spawn (info, session_bus, exec_line, NULL, launch_context,
4105 _SPAWN_FLAGS_DEFAULT, NULL, NULL, NULL, NULL, NULL);
4108 if (session_bus != NULL)
4110 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
4111 g_object_unref (session_bus);
4116 /* vim:set foldmethod=marker: */