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>
31 #ifdef HAVE_CRT_EXTERNS_H
32 #include <crt_externs.h>
35 #include "gcontenttypeprivate.h"
36 #include "gdesktopappinfo.h"
39 #include "gthemedicon.h"
40 #include "gfileicon.h"
41 #include <glib/gstdio.h>
43 #include "giomodule-priv.h"
48 * SECTION:gdesktopappinfo
49 * @title: GDesktopAppInfo
50 * @short_description: Application information from desktop files
51 * @include: gio/gdesktopappinfo.h
53 * #GDesktopAppInfo is an implementation of #GAppInfo based on
56 * Note that <filename><gio/gdesktopappinfo.h></filename> belongs to
57 * the UNIX-specific GIO interfaces, thus you have to use the
58 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
61 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
62 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
63 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
64 #define MIME_CACHE_GROUP "MIME Cache"
65 #define GENERIC_NAME_KEY "GenericName"
66 #define FULL_NAME_KEY "X-GNOME-FullName"
67 #define KEYWORDS_KEY "Keywords"
68 #define STARTUP_WM_CLASS_KEY "StartupWMClass"
75 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
76 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type,
78 gboolean include_fallback,
79 char **explicit_default);
80 static void mime_info_cache_reload (const char *dir);
81 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
87 * Information about an installed application from a desktop file.
89 struct _GDesktopAppInfo
91 GObject parent_instance;
110 char *startup_wm_class;
116 guint startup_notify : 1;
121 UPDATE_MIME_NONE = 1 << 0,
122 UPDATE_MIME_SET_DEFAULT = 1 << 1,
123 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
124 UPDATE_MIME_REMOVE = 1 << 3,
125 UPDATE_MIME_SET_LAST_USED = 1 << 4,
128 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
129 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
130 g_desktop_app_info_iface_init))
132 G_LOCK_DEFINE_STATIC (g_desktop_env);
133 static gchar *g_desktop_env = NULL;
136 search_path_init (gpointer data)
139 const char * const *data_dirs;
140 const char *user_data_dir;
143 data_dirs = g_get_system_data_dirs ();
144 length = g_strv_length ((char **) data_dirs);
146 args = g_new (char *, length + 2);
149 user_data_dir = g_get_user_data_dir ();
150 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
151 for (i = 0; i < length; i++)
152 args[j++] = g_build_filename (data_dirs[i],
153 "applications", NULL);
159 static const char * const *
160 get_applications_search_path (void)
162 static GOnce once_init = G_ONCE_INIT;
163 return g_once (&once_init, search_path_init, NULL);
167 g_desktop_app_info_finalize (GObject *object)
169 GDesktopAppInfo *info;
171 info = G_DESKTOP_APP_INFO (object);
173 g_free (info->desktop_id);
174 g_free (info->filename);
176 g_free (info->generic_name);
177 g_free (info->fullname);
178 g_free (info->comment);
179 g_free (info->icon_name);
181 g_object_unref (info->icon);
182 g_strfreev (info->keywords);
183 g_strfreev (info->only_show_in);
184 g_strfreev (info->not_show_in);
185 g_free (info->try_exec);
187 g_free (info->binary);
189 g_free (info->categories);
190 g_free (info->startup_wm_class);
191 g_strfreev (info->mime_types);
193 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
197 g_desktop_app_info_set_property(GObject *object,
202 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
207 self->filename = g_value_dup_string (value);
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217 g_desktop_app_info_get_property(GObject *object,
222 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
227 g_value_set_string (value, self->filename);
230 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
238 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
240 gobject_class->get_property = g_desktop_app_info_get_property;
241 gobject_class->set_property = g_desktop_app_info_set_property;
242 gobject_class->finalize = g_desktop_app_info_finalize;
245 * GDesktopAppInfo:filename:
247 * The origin filename of this #GDesktopAppInfo
249 g_object_class_install_property (gobject_class,
251 g_param_spec_string ("filename", "Filename", "",
253 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
257 g_desktop_app_info_init (GDesktopAppInfo *local)
262 binary_from_exec (const char *exec)
264 const char *p, *start;
270 while (*p != ' ' && *p != 0)
273 return g_strndup (start, p - start);
278 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
285 start_group = g_key_file_get_start_group (key_file);
286 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
288 g_free (start_group);
291 g_free (start_group);
293 type = g_key_file_get_string (key_file,
294 G_KEY_FILE_DESKTOP_GROUP,
295 G_KEY_FILE_DESKTOP_KEY_TYPE,
297 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
304 try_exec = g_key_file_get_string (key_file,
305 G_KEY_FILE_DESKTOP_GROUP,
306 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
308 if (try_exec && try_exec[0] != '\0')
311 t = g_find_program_in_path (try_exec);
320 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
321 info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
322 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
323 info->keywords = g_key_file_get_locale_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, KEYWORDS_KEY, NULL, NULL, NULL);
324 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
325 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
326 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
327 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);
328 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);
329 info->try_exec = try_exec;
330 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
331 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
332 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
333 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
334 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
335 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
336 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
337 info->startup_wm_class = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, STARTUP_WM_CLASS_KEY, NULL);
338 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);
343 if (g_path_is_absolute (info->icon_name))
347 file = g_file_new_for_path (info->icon_name);
348 info->icon = g_file_icon_new (file);
349 g_object_unref (file);
355 /* Work around a common mistake in desktop files */
356 if ((p = strrchr (info->icon_name, '.')) != NULL &&
357 (strcmp (p, ".png") == 0 ||
358 strcmp (p, ".xpm") == 0 ||
359 strcmp (p, ".svg") == 0))
362 info->icon = g_themed_icon_new (info->icon_name);
367 info->binary = binary_from_exec (info->exec);
369 if (info->path && info->path[0] == '\0')
379 g_desktop_app_info_load_file (GDesktopAppInfo *self)
382 gboolean retval = FALSE;
384 g_return_val_if_fail (self->filename != NULL, FALSE);
386 key_file = g_key_file_new ();
388 if (g_key_file_load_from_file (key_file,
393 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
396 g_key_file_free (key_file);
401 * g_desktop_app_info_new_from_keyfile:
402 * @key_file: an opened #GKeyFile
404 * Creates a new #GDesktopAppInfo.
406 * Returns: a new #GDesktopAppInfo or %NULL on error.
411 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
413 GDesktopAppInfo *info;
415 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
416 info->filename = NULL;
417 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
419 g_object_unref (info);
426 * g_desktop_app_info_new_from_filename:
427 * @filename: the path of a desktop file, in the GLib filename encoding
429 * Creates a new #GDesktopAppInfo.
431 * Returns: a new #GDesktopAppInfo or %NULL on error.
434 g_desktop_app_info_new_from_filename (const char *filename)
436 GDesktopAppInfo *info = NULL;
438 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
439 if (!g_desktop_app_info_load_file (info))
441 g_object_unref (info);
448 * g_desktop_app_info_new:
449 * @desktop_id: the desktop file id
451 * Creates a new #GDesktopAppInfo based on a desktop file id.
453 * A desktop file id is the basename of the desktop file, including the
454 * .desktop extension. GIO is looking for a desktop file with this name
455 * in the <filename>applications</filename> subdirectories of the XDG data
456 * directories (i.e. the directories specified in the
457 * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment
458 * variables). GIO also supports the prefix-to-subdirectory mapping that is
459 * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink>
460 * (i.e. a desktop id of kde-foo.desktop will match
461 * <filename>/usr/share/applications/kde/foo.desktop</filename>).
463 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
466 g_desktop_app_info_new (const char *desktop_id)
468 GDesktopAppInfo *appinfo;
469 const char * const *dirs;
473 dirs = get_applications_search_path ();
475 basename = g_strdup (desktop_id);
477 for (i = 0; dirs[i] != NULL; i++)
482 filename = g_build_filename (dirs[i], desktop_id, NULL);
483 appinfo = g_desktop_app_info_new_from_filename (filename);
489 while ((p = strchr (p, '-')) != NULL)
493 filename = g_build_filename (dirs[i], basename, NULL);
494 appinfo = g_desktop_app_info_new_from_filename (filename);
509 appinfo->desktop_id = g_strdup (desktop_id);
511 if (g_desktop_app_info_get_is_hidden (appinfo))
513 g_object_unref (appinfo);
521 g_desktop_app_info_dup (GAppInfo *appinfo)
523 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
524 GDesktopAppInfo *new_info;
526 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
528 new_info->filename = g_strdup (info->filename);
529 new_info->desktop_id = g_strdup (info->desktop_id);
531 new_info->name = g_strdup (info->name);
532 new_info->generic_name = g_strdup (info->generic_name);
533 new_info->fullname = g_strdup (info->fullname);
534 new_info->keywords = g_strdupv (info->keywords);
535 new_info->comment = g_strdup (info->comment);
536 new_info->nodisplay = info->nodisplay;
537 new_info->icon_name = g_strdup (info->icon_name);
539 new_info->icon = g_object_ref (info->icon);
540 new_info->only_show_in = g_strdupv (info->only_show_in);
541 new_info->not_show_in = g_strdupv (info->not_show_in);
542 new_info->try_exec = g_strdup (info->try_exec);
543 new_info->exec = g_strdup (info->exec);
544 new_info->binary = g_strdup (info->binary);
545 new_info->path = g_strdup (info->path);
546 new_info->hidden = info->hidden;
547 new_info->terminal = info->terminal;
548 new_info->startup_notify = info->startup_notify;
550 return G_APP_INFO (new_info);
554 g_desktop_app_info_equal (GAppInfo *appinfo1,
557 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
558 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
560 if (info1->desktop_id == NULL ||
561 info2->desktop_id == NULL)
562 return info1 == info2;
564 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
568 g_desktop_app_info_get_id (GAppInfo *appinfo)
570 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
572 return info->desktop_id;
576 g_desktop_app_info_get_name (GAppInfo *appinfo)
578 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
580 if (info->name == NULL)
586 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
588 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
590 if (info->fullname == NULL)
591 return g_desktop_app_info_get_name (appinfo);
592 return info->fullname;
596 * g_desktop_app_info_get_is_hidden:
597 * @info: a #GDesktopAppInfo.
599 * A desktop file is hidden if the Hidden key in it is
602 * Returns: %TRUE if hidden, %FALSE otherwise.
605 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
611 * g_desktop_app_info_get_filename:
612 * @info: a #GDesktopAppInfo
614 * When @info was created from a known filename, return it. In some
615 * situations such as the #GDesktopAppInfo returned from
616 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
618 * Returns: The full path to the file for @info, or %NULL if not known.
622 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
624 return info->filename;
628 g_desktop_app_info_get_description (GAppInfo *appinfo)
630 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
632 return info->comment;
636 g_desktop_app_info_get_executable (GAppInfo *appinfo)
638 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
644 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
646 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
652 g_desktop_app_info_get_icon (GAppInfo *appinfo)
654 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
660 * g_desktop_app_info_get_categories:
661 * @info: a #GDesktopAppInfo
663 * Gets the categories from the desktop file.
665 * Returns: The unparsed Categories key from the desktop file;
666 * i.e. no attempt is made to split it by ';' or validate it.
669 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
671 return info->categories;
675 * g_desktop_app_info_get_keywords:
676 * @info: a #GDesktopAppInfo
678 * Gets the keywords from the desktop file.
680 * Returns: (transfer none): The value of the Keywords key
685 g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
687 return (const char * const *)info->keywords;
691 * g_desktop_app_info_get_generic_name:
692 * @info: a #GDesktopAppInfo
694 * Gets the generic name from the destkop file.
696 * Returns: The value of the GenericName key
699 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
701 return info->generic_name;
705 * g_desktop_app_info_get_nodisplay:
706 * @info: a #GDesktopAppInfo
708 * Gets the value of the NoDisplay key, which helps determine if the
709 * application info should be shown in menus. See
710 * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
712 * Returns: The value of the NoDisplay key
717 g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
719 return info->nodisplay;
723 * g_desktop_app_info_get_show_in:
724 * @info: a #GDesktopAppInfo
725 * @desktop_env: a string specifying a desktop name
727 * Checks if the application info should be shown in menus that list available
728 * applications for a specific name of the desktop, based on the
729 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys.
731 * If @desktop_env is %NULL, then the name of the desktop set with
732 * g_desktop_app_info_set_desktop_env() is used.
734 * Note that g_app_info_should_show() for @info will include this check (with
735 * %NULL for @desktop_env) as well as additional checks.
737 * Returns: %TRUE if the @info should be shown in @desktop_env according to the
738 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys, %FALSE
744 g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
745 const gchar *desktop_env)
750 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
753 G_LOCK (g_desktop_env);
754 desktop_env = g_desktop_env;
755 G_UNLOCK (g_desktop_env);
758 if (info->only_show_in)
760 if (desktop_env == NULL)
764 for (i = 0; info->only_show_in[i] != NULL; i++)
766 if (strcmp (info->only_show_in[i], desktop_env) == 0)
776 if (info->not_show_in && desktop_env)
778 for (i = 0; info->not_show_in[i] != NULL; i++)
780 if (strcmp (info->not_show_in[i], desktop_env) == 0)
789 expand_macro_single (char macro, char *uri)
795 file = g_file_new_for_uri (uri);
796 path = g_file_get_path (file);
797 g_object_unref (file);
803 result = g_shell_quote (uri);
808 result = g_shell_quote (path);
814 name = g_path_get_dirname (path);
815 result = g_shell_quote (name);
823 name = g_path_get_basename (path);
824 result = g_shell_quote (name);
836 expand_macro (char macro,
838 GDesktopAppInfo *info,
841 GList *uris = *uri_list;
843 gboolean force_file_uri;
844 char force_file_uri_macro;
847 g_return_if_fail (exec != NULL);
849 /* On %u and %U, pass POSIX file path pointing to the URI via
850 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
851 * running or the URI doesn't have a POSIX file path via FUSE
852 * we'll just pass the URI.
854 force_file_uri_macro = macro;
855 force_file_uri = FALSE;
861 force_file_uri_macro = 'f';
862 force_file_uri = TRUE;
865 force_file_uri_macro = 'F';
866 force_file_uri = TRUE;
882 if (!force_file_uri ||
883 /* Pass URI if it contains an anchor */
884 strchr (uri, '#') != NULL)
886 expanded = expand_macro_single (macro, uri);
890 expanded = expand_macro_single (force_file_uri_macro, uri);
891 if (expanded == NULL)
892 expanded = expand_macro_single (macro, uri);
897 g_string_append (exec, expanded);
913 if (!force_file_uri ||
914 /* Pass URI if it contains an anchor */
915 strchr (uri, '#') != NULL)
917 expanded = expand_macro_single (macro, uri);
921 expanded = expand_macro_single (force_file_uri_macro, uri);
922 if (expanded == NULL)
923 expanded = expand_macro_single (macro, uri);
928 g_string_append (exec, expanded);
934 if (uris != NULL && expanded)
935 g_string_append_c (exec, ' ');
943 g_string_append (exec, "--icon ");
944 expanded = g_shell_quote (info->icon_name);
945 g_string_append (exec, expanded);
953 expanded = g_shell_quote (info->name);
954 g_string_append (exec, expanded);
962 expanded = g_shell_quote (info->filename);
963 g_string_append (exec, expanded);
968 case 'm': /* deprecated */
972 g_string_append_c (exec, '%');
980 expand_application_parameters (GDesktopAppInfo *info,
986 GList *uri_list = *uris;
987 const char *p = info->exec;
988 GString *expanded_exec;
991 if (info->exec == NULL)
993 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
994 _("Desktop file didn't specify Exec field"));
998 expanded_exec = g_string_new (NULL);
1002 if (p[0] == '%' && p[1] != '\0')
1004 expand_macro (p[1], expanded_exec, info, uris);
1008 g_string_append_c (expanded_exec, *p);
1013 /* No file substitutions */
1014 if (uri_list == *uris && uri_list != NULL)
1016 /* If there is no macro default to %f. This is also what KDE does */
1017 g_string_append_c (expanded_exec, ' ');
1018 expand_macro ('f', expanded_exec, info, uris);
1021 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
1022 g_string_free (expanded_exec, TRUE);
1027 prepend_terminal_to_vector (int *argc,
1034 char **term_argv = NULL;
1039 g_return_val_if_fail (argc != NULL, FALSE);
1040 g_return_val_if_fail (argv != NULL, FALSE);
1048 /* compute size if not given */
1051 for (i = 0; the_argv[i] != NULL; i++)
1057 term_argv = g_new0 (char *, 3);
1059 check = g_find_program_in_path ("gnome-terminal");
1062 term_argv[0] = check;
1063 /* Note that gnome-terminal takes -x and
1064 * as -e in gnome-terminal is broken we use that. */
1065 term_argv[1] = g_strdup ("-x");
1070 check = g_find_program_in_path ("nxterm");
1072 check = g_find_program_in_path ("color-xterm");
1074 check = g_find_program_in_path ("rxvt");
1076 check = g_find_program_in_path ("xterm");
1078 check = g_find_program_in_path ("dtterm");
1081 check = g_strdup ("xterm");
1082 g_warning ("couldn't find a terminal, falling back to xterm");
1084 term_argv[0] = check;
1085 term_argv[1] = g_strdup ("-e");
1088 real_argc = term_argc + *argc;
1089 real_argv = g_new (char *, real_argc + 1);
1091 for (i = 0; i < term_argc; i++)
1092 real_argv[i] = term_argv[i];
1094 for (j = 0; j < *argc; j++, i++)
1095 real_argv[i] = (char *)the_argv[j];
1097 real_argv[i] = NULL;
1103 /* we use g_free here as we sucked all the inner strings
1104 * out from it into real_argv */
1109 #endif /* G_OS_WIN32 */
1113 create_files_for_uris (GList *uris)
1120 for (iter = uris; iter; iter = iter->next)
1122 GFile *file = g_file_new_for_uri ((char *)iter->data);
1123 res = g_list_prepend (res, file);
1126 return g_list_reverse (res);
1131 GSpawnChildSetupFunc user_setup;
1132 gpointer user_setup_data;
1138 child_setup (gpointer user_data)
1140 ChildSetupData *data = user_data;
1142 if (data->pid_envvar)
1144 pid_t pid = getpid ();
1148 /* Write the pid into the space already reserved for it in the
1149 * environment array. We can't use sprintf because it might
1150 * malloc, so we do it by hand. It's simplest to write the pid
1151 * out backwards first, then copy it over.
1153 for (i = 0; pid; i++, pid /= 10)
1154 buf[i] = (pid % 10) + '0';
1155 for (i--; i >= 0; i--)
1156 *(data->pid_envvar++) = buf[i];
1157 *data->pid_envvar = '\0';
1160 if (data->user_setup)
1161 data->user_setup (data->user_setup_data);
1165 notify_desktop_launch (GDBusConnection *session_bus,
1166 GDesktopAppInfo *info,
1168 const char *display,
1173 GVariantBuilder uri_variant;
1174 GVariantBuilder extras_variant;
1176 const char *desktop_file_id;
1177 const char *gio_desktop_file;
1179 if (session_bus == NULL)
1182 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
1183 for (iter = uris; iter; iter = iter->next)
1184 g_variant_builder_add (&uri_variant, "s", iter->data);
1186 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
1187 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
1188 g_variant_builder_add (&extras_variant, "{sv}",
1192 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
1193 if (gio_desktop_file != NULL)
1194 g_variant_builder_add (&extras_variant, "{sv}",
1195 "origin-desktop-file",
1196 g_variant_new_bytestring (gio_desktop_file));
1197 if (g_get_prgname () != NULL)
1198 g_variant_builder_add (&extras_variant, "{sv}",
1200 g_variant_new_bytestring (g_get_prgname ()));
1201 g_variant_builder_add (&extras_variant, "{sv}",
1204 (gint64)getpid ()));
1207 desktop_file_id = info->filename;
1208 else if (info->desktop_id)
1209 desktop_file_id = info->desktop_id;
1211 desktop_file_id = "";
1213 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
1214 "org.gtk.gio.DesktopAppInfo",
1216 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
1217 g_variant_new_bytestring (desktop_file_id),
1218 display ? display : "",
1222 g_dbus_connection_send_message (session_bus,
1226 g_object_unref (msg);
1229 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
1232 _g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
1234 GAppLaunchContext *launch_context,
1235 GSpawnFlags spawn_flags,
1236 GSpawnChildSetupFunc user_setup,
1237 gpointer user_setup_data,
1238 GDesktopAppLaunchCallback pid_callback,
1239 gpointer pid_callback_data,
1242 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1243 GDBusConnection *session_bus;
1244 gboolean completed = FALSE;
1246 char **argv, **envp;
1248 ChildSetupData data;
1250 g_return_val_if_fail (appinfo != NULL, FALSE);
1254 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
1257 envp = g_app_launch_context_get_environment (launch_context);
1259 envp = g_get_environ ();
1264 GList *launched_uris;
1266 char *display, *sn_id;
1269 if (!expand_application_parameters (info, &uris,
1270 &argc, &argv, error))
1273 /* Get the subset of URIs we're launching with this process */
1274 launched_uris = NULL;
1275 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
1276 launched_uris = g_list_prepend (launched_uris, iter->data);
1277 launched_uris = g_list_reverse (launched_uris);
1279 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
1281 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1282 _("Unable to find terminal required for application"));
1286 data.user_setup = user_setup;
1287 data.user_setup_data = user_setup_data;
1291 envp = g_environ_setenv (envp,
1292 "GIO_LAUNCHED_DESKTOP_FILE",
1295 envp = g_environ_setenv (envp,
1296 "GIO_LAUNCHED_DESKTOP_FILE_PID",
1297 "XXXXXXXXXXXXXXXXXXXX", /* filled in child_setup */
1299 data.pid_envvar = (char *)g_environ_getenv (envp, "GIO_LAUNCHED_DESKTOP_FILE_PID");
1303 data.pid_envvar = NULL;
1310 GList *launched_files = create_files_for_uris (launched_uris);
1312 display = g_app_launch_context_get_display (launch_context,
1316 envp = g_environ_setenv (envp, "DISPLAY", display, TRUE);
1318 if (info->startup_notify)
1320 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
1323 envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
1326 g_list_free_full (launched_files, g_object_unref);
1329 if (!g_spawn_async (info->path,
1339 g_app_launch_context_launch_failed (launch_context, sn_id);
1343 g_list_free (launched_uris);
1348 if (pid_callback != NULL)
1349 pid_callback (info, pid, pid_callback_data);
1351 notify_desktop_launch (session_bus,
1360 g_list_free (launched_uris);
1365 while (uris != NULL);
1367 /* TODO - need to handle the process exiting immediately
1368 * after launching an app. See http://bugzilla.gnome.org/606960
1370 if (session_bus != NULL)
1372 /* This asynchronous flush holds a reference until it completes,
1373 * which ensures that the following unref won't immediately kill
1374 * the connection if we were the initial owner.
1376 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
1377 g_object_unref (session_bus);
1390 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
1392 GAppLaunchContext *launch_context,
1395 return _g_desktop_app_info_launch_uris_internal (appinfo, uris,
1397 _SPAWN_FLAGS_DEFAULT,
1398 NULL, NULL, NULL, NULL,
1403 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1405 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1407 return info->exec &&
1408 ((strstr (info->exec, "%u") != NULL) ||
1409 (strstr (info->exec, "%U") != NULL));
1413 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1415 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1417 return info->exec &&
1418 ((strstr (info->exec, "%f") != NULL) ||
1419 (strstr (info->exec, "%F") != NULL));
1423 g_desktop_app_info_launch (GAppInfo *appinfo,
1425 GAppLaunchContext *launch_context,
1435 uri = g_file_get_uri (files->data);
1436 uris = g_list_prepend (uris, uri);
1437 files = files->next;
1440 uris = g_list_reverse (uris);
1442 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1444 g_list_free_full (uris, g_free);
1450 * g_desktop_app_info_launch_uris_as_manager:
1451 * @appinfo: a #GDesktopAppInfo
1452 * @uris: (element-type utf8): List of URIs
1453 * @launch_context: a #GAppLaunchContext
1454 * @spawn_flags: #GSpawnFlags, used for each process
1455 * @user_setup: (scope call): a #GSpawnChildSetupFunc, used once for
1457 * @user_setup_data: (closure user_setup): User data for @user_setup
1458 * @pid_callback: (scope call): Callback for child processes
1459 * @pid_callback_data: (closure pid_callback): User data for @callback
1460 * @error: return location for a #GError, or %NULL
1462 * This function performs the equivalent of g_app_info_launch_uris(),
1463 * but is intended primarily for operating system components that
1464 * launch applications. Ordinary applications should use
1465 * g_app_info_launch_uris().
1467 * In contrast to g_app_info_launch_uris(), all processes created will
1468 * always be run directly as children as if by the UNIX fork()/exec()
1471 * This guarantee allows additional control over the exact environment
1472 * of the child processes, which is provided via a setup function
1473 * @user_setup, as well as the process identifier of each child process
1474 * via @pid_callback. See g_spawn_async() for more information about the
1475 * semantics of the @user_setup function.
1477 * Returns: %TRUE on successful launch, %FALSE otherwise.
1480 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
1482 GAppLaunchContext *launch_context,
1483 GSpawnFlags spawn_flags,
1484 GSpawnChildSetupFunc user_setup,
1485 gpointer user_setup_data,
1486 GDesktopAppLaunchCallback pid_callback,
1487 gpointer pid_callback_data,
1490 return _g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
1502 * g_desktop_app_info_set_desktop_env:
1503 * @desktop_env: a string specifying what desktop this is
1505 * Sets the name of the desktop that the application is running in.
1506 * This is used by g_app_info_should_show() and
1507 * g_desktop_app_info_get_show_in() to evaluate the
1508 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1509 * desktop entry fields.
1511 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1512 * Menu specification</ulink> recognizes the following:
1514 * <member>GNOME</member>
1515 * <member>KDE</member>
1516 * <member>ROX</member>
1517 * <member>XFCE</member>
1518 * <member>LXDE</member>
1519 * <member>Unity</member>
1520 * <member>Old</member>
1523 * Should be called only once; subsequent calls are ignored.
1526 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1528 G_LOCK (g_desktop_env);
1530 g_desktop_env = g_strdup (desktop_env);
1531 G_UNLOCK (g_desktop_env);
1535 g_desktop_app_info_should_show (GAppInfo *appinfo)
1537 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1539 if (info->nodisplay)
1542 return g_desktop_app_info_get_show_in (info, NULL);
1551 ensure_dir (DirType type,
1554 char *path, *display_name;
1557 if (type == APP_DIR)
1558 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1560 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1563 if (g_mkdir_with_parents (path, 0700) == 0)
1567 display_name = g_filename_display_name (path);
1568 if (type == APP_DIR)
1569 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1570 _("Can't create user application configuration folder %s: %s"),
1571 display_name, g_strerror (errsv));
1573 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1574 _("Can't create user MIME configuration folder %s: %s"),
1575 display_name, g_strerror (errsv));
1577 g_free (display_name);
1584 update_mimeapps_list (const char *desktop_id,
1585 const char *content_type,
1586 UpdateMimeFlags flags,
1589 char *dirname, *filename, *string;
1591 gboolean load_succeeded, res;
1592 char **old_list, **list;
1593 gsize length, data_size;
1596 char **content_types;
1598 /* Don't add both at start and end */
1599 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
1600 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
1602 dirname = ensure_dir (APP_DIR, error);
1606 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1609 key_file = g_key_file_new ();
1610 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1611 if (!load_succeeded ||
1612 (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) &&
1613 !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) &&
1614 !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP)))
1616 g_key_file_free (key_file);
1617 key_file = g_key_file_new ();
1622 content_types = g_new (char *, 2);
1623 content_types[0] = g_strdup (content_type);
1624 content_types[1] = NULL;
1628 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
1631 for (k = 0; content_types && content_types[k]; k++)
1633 /* set as default, if requested so */
1634 string = g_key_file_get_string (key_file,
1635 DEFAULT_APPLICATIONS_GROUP,
1639 if (g_strcmp0 (string, desktop_id) != 0 &&
1640 (flags & UPDATE_MIME_SET_DEFAULT))
1643 string = g_strdup (desktop_id);
1645 /* add in the non-default list too, if it's not already there */
1646 flags |= UPDATE_MIME_SET_NON_DEFAULT;
1649 if (string == NULL || desktop_id == NULL)
1650 g_key_file_remove_key (key_file,
1651 DEFAULT_APPLICATIONS_GROUP,
1655 g_key_file_set_string (key_file,
1656 DEFAULT_APPLICATIONS_GROUP,
1665 /* reuse the list from above */
1669 g_strfreev (content_types);
1670 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1673 for (k = 0; content_types && content_types[k]; k++)
1675 /* Add to the right place in the list */
1678 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1679 content_types[k], &length, NULL);
1681 list = g_new (char *, 1 + length + 1);
1685 /* if we're adding a last-used hint, just put the application in front of the list */
1686 if (flags & UPDATE_MIME_SET_LAST_USED)
1688 /* avoid adding this again as non-default later */
1689 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1690 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1692 list[i++] = g_strdup (desktop_id);
1697 for (j = 0; old_list[j] != NULL; j++)
1699 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1701 /* rewrite other entries if they're different from the new one */
1702 list[i++] = g_strdup (old_list[j]);
1704 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1706 /* we encountered an old entry which is equal to the one we're adding as non-default,
1707 * don't change its position in the list.
1709 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1710 list[i++] = g_strdup (old_list[j]);
1715 /* add it at the end of the list */
1716 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1717 list[i++] = g_strdup (desktop_id);
1721 g_strfreev (old_list);
1723 if (list[0] == NULL || desktop_id == NULL)
1724 g_key_file_remove_key (key_file,
1725 ADDED_ASSOCIATIONS_GROUP,
1729 g_key_file_set_string_list (key_file,
1730 ADDED_ASSOCIATIONS_GROUP,
1732 (const char * const *)list, i);
1739 /* reuse the list from above */
1743 g_strfreev (content_types);
1744 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1747 for (k = 0; content_types && content_types[k]; k++)
1749 /* Remove from removed associations group (unless remove) */
1752 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1753 content_types[k], &length, NULL);
1755 list = g_new (char *, 1 + length + 1);
1758 if (flags & UPDATE_MIME_REMOVE)
1759 list[i++] = g_strdup (desktop_id);
1762 for (j = 0; old_list[j] != NULL; j++)
1764 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1765 list[i++] = g_strdup (old_list[j]);
1770 g_strfreev (old_list);
1772 if (list[0] == NULL || desktop_id == NULL)
1773 g_key_file_remove_key (key_file,
1774 REMOVED_ASSOCIATIONS_GROUP,
1778 g_key_file_set_string_list (key_file,
1779 REMOVED_ASSOCIATIONS_GROUP,
1781 (const char * const *)list, i);
1786 g_strfreev (content_types);
1788 data = g_key_file_to_data (key_file, &data_size, error);
1789 g_key_file_free (key_file);
1791 res = g_file_set_contents (filename, data, data_size, error);
1793 mime_info_cache_reload (NULL);
1802 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
1803 const char *content_type,
1806 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1808 if (!g_desktop_app_info_ensure_saved (info, error))
1811 if (!info->desktop_id)
1813 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1814 _("Application information lacks an identifier"));
1818 /* both add support for the content type and set as last used */
1819 return update_mimeapps_list (info->desktop_id, content_type,
1820 UPDATE_MIME_SET_NON_DEFAULT |
1821 UPDATE_MIME_SET_LAST_USED,
1826 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1827 const char *content_type,
1830 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1832 if (!g_desktop_app_info_ensure_saved (info, error))
1835 if (!info->desktop_id)
1837 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1838 _("Application information lacks an identifier"));
1842 return update_mimeapps_list (info->desktop_id, content_type,
1843 UPDATE_MIME_SET_DEFAULT,
1848 update_program_done (GPid pid,
1852 /* Did the application exit correctly */
1853 if (WIFEXITED (status) &&
1854 WEXITSTATUS (status) == 0)
1856 /* Here we could clean out any caches in use */
1861 run_update_command (char *command,
1870 GError *error = NULL;
1873 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1875 if (g_spawn_async ("/", argv,
1877 G_SPAWN_SEARCH_PATH |
1878 G_SPAWN_STDOUT_TO_DEV_NULL |
1879 G_SPAWN_STDERR_TO_DEV_NULL |
1880 G_SPAWN_DO_NOT_REAP_CHILD,
1881 NULL, NULL, /* No setup function */
1884 g_child_watch_add (pid, update_program_done, NULL);
1887 /* If we get an error at this point, it's quite likely the user doesn't
1888 * have an installed copy of either 'update-mime-database' or
1889 * 'update-desktop-database'. I don't think we want to popup an error
1890 * dialog at this point, so we just do a g_warning to give the user a
1891 * chance of debugging it.
1893 g_warning ("%s", error->message);
1900 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1901 const char *extension,
1904 char *filename, *basename, *mimetype;
1908 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1911 dirname = ensure_dir (MIMETYPE_DIR, error);
1915 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1916 filename = g_build_filename (dirname, basename, NULL);
1920 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1922 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1927 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1928 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1929 " <mime-type type=\"%s\">\n"
1930 " <comment>%s document</comment>\n"
1931 " <glob pattern=\"*.%s\"/>\n"
1933 "</mime-info>\n", mimetype, extension, extension);
1935 g_file_set_contents (filename, contents, -1, NULL);
1938 run_update_command ("update-mime-database", "mime");
1942 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1952 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1953 const char *content_type,
1956 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1958 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1961 return update_mimeapps_list (info->desktop_id, content_type,
1962 UPDATE_MIME_SET_NON_DEFAULT,
1967 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1973 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1974 const char *content_type,
1977 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1979 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1982 return update_mimeapps_list (info->desktop_id, content_type,
1987 static const char **
1988 g_desktop_app_info_get_supported_types (GAppInfo *appinfo)
1990 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1992 return (const char**) info->mime_types;
1997 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
2003 char *data, *desktop_id;
2008 if (info->filename != NULL)
2011 /* This is only used for object created with
2012 * g_app_info_create_from_commandline. All other
2013 * object should have a filename
2016 dirname = ensure_dir (APP_DIR, error);
2020 key_file = g_key_file_new ();
2022 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2023 "Encoding", "UTF-8");
2024 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2025 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
2026 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2027 G_KEY_FILE_DESKTOP_KEY_TYPE,
2028 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
2030 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2031 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
2032 if (info->nodisplay)
2033 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2034 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2036 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2037 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
2039 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2040 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
2042 if (info->generic_name != NULL)
2043 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2044 GENERIC_NAME_KEY, info->generic_name);
2046 if (info->fullname != NULL)
2047 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2048 FULL_NAME_KEY, info->fullname);
2050 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2051 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
2053 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2054 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2056 data = g_key_file_to_data (key_file, &data_size, NULL);
2057 g_key_file_free (key_file);
2059 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
2060 filename = g_build_filename (dirname, desktop_id, NULL);
2061 g_free (desktop_id);
2064 fd = g_mkstemp (filename);
2069 display_name = g_filename_display_name (filename);
2070 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2071 _("Can't create user desktop file %s"), display_name);
2072 g_free (display_name);
2078 desktop_id = g_path_get_basename (filename);
2082 res = g_file_set_contents (filename, data, data_size, error);
2085 g_free (desktop_id);
2090 info->filename = filename;
2091 info->desktop_id = desktop_id;
2093 run_update_command ("update-desktop-database", "applications");
2099 g_desktop_app_info_can_delete (GAppInfo *appinfo)
2101 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2105 if (strstr (info->filename, "/userapp-"))
2106 return g_access (info->filename, W_OK) == 0;
2113 g_desktop_app_info_delete (GAppInfo *appinfo)
2115 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2119 if (g_remove (info->filename) == 0)
2121 update_mimeapps_list (info->desktop_id, NULL,
2125 g_free (info->filename);
2126 info->filename = NULL;
2127 g_free (info->desktop_id);
2128 info->desktop_id = NULL;
2138 * g_app_info_create_from_commandline:
2139 * @commandline: the commandline to use
2140 * @application_name: (allow-none): the application name, or %NULL to use @commandline
2141 * @flags: flags that can specify details of the created #GAppInfo
2142 * @error: a #GError location to store the error occurring, %NULL to ignore.
2144 * Creates a new #GAppInfo from the given information.
2146 * Note that for @commandline, the quoting rules of the Exec key of the
2147 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">freedesktop.org Desktop
2148 * Entry Specification</ulink> are applied. For example, if the @commandline contains
2149 * percent-encoded URIs, the percent-character must be doubled in order to prevent it from
2150 * being swallowed by Exec key unquoting. See the specification for exact quoting rules.
2152 * Returns: (transfer full): new #GAppInfo for given command.
2155 g_app_info_create_from_commandline (const char *commandline,
2156 const char *application_name,
2157 GAppInfoCreateFlags flags,
2162 GDesktopAppInfo *info;
2164 g_return_val_if_fail (commandline, NULL);
2166 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
2168 info->filename = NULL;
2169 info->desktop_id = NULL;
2171 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
2172 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
2173 info->hidden = FALSE;
2174 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
2175 info->exec = g_strconcat (commandline, " %u", NULL);
2177 info->exec = g_strconcat (commandline, " %f", NULL);
2178 info->nodisplay = TRUE;
2179 info->binary = binary_from_exec (info->exec);
2181 if (application_name)
2182 info->name = g_strdup (application_name);
2185 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
2186 split = g_strsplit (commandline, " ", 2);
2187 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
2189 info->name = basename;
2190 if (info->name == NULL)
2191 info->name = g_strdup ("custom");
2193 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
2195 return G_APP_INFO (info);
2199 g_desktop_app_info_iface_init (GAppInfoIface *iface)
2201 iface->dup = g_desktop_app_info_dup;
2202 iface->equal = g_desktop_app_info_equal;
2203 iface->get_id = g_desktop_app_info_get_id;
2204 iface->get_name = g_desktop_app_info_get_name;
2205 iface->get_description = g_desktop_app_info_get_description;
2206 iface->get_executable = g_desktop_app_info_get_executable;
2207 iface->get_icon = g_desktop_app_info_get_icon;
2208 iface->launch = g_desktop_app_info_launch;
2209 iface->supports_uris = g_desktop_app_info_supports_uris;
2210 iface->supports_files = g_desktop_app_info_supports_files;
2211 iface->launch_uris = g_desktop_app_info_launch_uris;
2212 iface->should_show = g_desktop_app_info_should_show;
2213 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
2214 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
2215 iface->add_supports_type = g_desktop_app_info_add_supports_type;
2216 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
2217 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
2218 iface->can_delete = g_desktop_app_info_can_delete;
2219 iface->do_delete = g_desktop_app_info_delete;
2220 iface->get_commandline = g_desktop_app_info_get_commandline;
2221 iface->get_display_name = g_desktop_app_info_get_display_name;
2222 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
2223 iface->get_supported_types = g_desktop_app_info_get_supported_types;
2227 app_info_in_list (GAppInfo *info,
2230 while (list != NULL)
2232 if (g_app_info_equal (info, list->data))
2240 * g_app_info_get_recommended_for_type:
2241 * @content_type: the content type to find a #GAppInfo for
2243 * Gets a list of recommended #GAppInfos for a given content type, i.e.
2244 * those applications which claim to support the given content type exactly,
2245 * and not by MIME type subclassing.
2246 * Note that the first application of the list is the last used one, i.e.
2247 * the last one for which g_app_info_set_as_last_used_for_type() has been
2250 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2251 * for given @content_type or %NULL on error.
2256 g_app_info_get_recommended_for_type (const gchar *content_type)
2258 GList *desktop_entries, *l;
2260 GDesktopAppInfo *info;
2262 g_return_val_if_fail (content_type != NULL, NULL);
2264 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
2267 for (l = desktop_entries; l != NULL; l = l->next)
2269 char *desktop_entry = l->data;
2271 info = g_desktop_app_info_new (desktop_entry);
2274 if (app_info_in_list (G_APP_INFO (info), infos))
2275 g_object_unref (info);
2277 infos = g_list_prepend (infos, info);
2279 g_free (desktop_entry);
2282 g_list_free (desktop_entries);
2284 return g_list_reverse (infos);
2288 * g_app_info_get_fallback_for_type:
2289 * @content_type: the content type to find a #GAppInfo for
2291 * Gets a list of fallback #GAppInfos for a given content type, i.e.
2292 * those applications which claim to support the given content type
2293 * by MIME type subclassing and not directly.
2295 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2296 * for given @content_type or %NULL on error.
2301 g_app_info_get_fallback_for_type (const gchar *content_type)
2303 GList *desktop_entries, *l;
2304 GList *infos, *recommended_infos;
2305 GDesktopAppInfo *info;
2307 g_return_val_if_fail (content_type != NULL, NULL);
2309 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
2310 recommended_infos = g_app_info_get_recommended_for_type (content_type);
2313 for (l = desktop_entries; l != NULL; l = l->next)
2315 char *desktop_entry = l->data;
2317 info = g_desktop_app_info_new (desktop_entry);
2320 if (app_info_in_list (G_APP_INFO (info), infos) ||
2321 app_info_in_list (G_APP_INFO (info), recommended_infos))
2322 g_object_unref (info);
2324 infos = g_list_prepend (infos, info);
2326 g_free (desktop_entry);
2329 g_list_free (desktop_entries);
2330 g_list_free_full (recommended_infos, g_object_unref);
2332 return g_list_reverse (infos);
2336 * g_app_info_get_all_for_type:
2337 * @content_type: the content type to find a #GAppInfo for
2339 * Gets a list of all #GAppInfos for a given content type,
2340 * including the recommended and fallback #GAppInfos. See
2341 * g_app_info_get_recommended_for_type() and
2342 * g_app_info_get_fallback_for_type().
2344 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2345 * for given @content_type or %NULL on error.
2348 g_app_info_get_all_for_type (const char *content_type)
2350 GList *desktop_entries, *l;
2352 char *user_default = NULL;
2353 GDesktopAppInfo *info;
2355 g_return_val_if_fail (content_type != NULL, NULL);
2357 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2360 /* put the user default in front of the list, for compatibility */
2361 if (user_default != NULL)
2363 info = g_desktop_app_info_new (user_default);
2366 infos = g_list_prepend (infos, info);
2369 g_free (user_default);
2371 for (l = desktop_entries; l != NULL; l = l->next)
2373 char *desktop_entry = l->data;
2375 info = g_desktop_app_info_new (desktop_entry);
2378 if (app_info_in_list (G_APP_INFO (info), infos))
2379 g_object_unref (info);
2381 infos = g_list_prepend (infos, info);
2383 g_free (desktop_entry);
2386 g_list_free (desktop_entries);
2388 return g_list_reverse (infos);
2392 * g_app_info_reset_type_associations:
2393 * @content_type: a content type
2395 * Removes all changes to the type associations done by
2396 * g_app_info_set_as_default_for_type(),
2397 * g_app_info_set_as_default_for_extension(),
2398 * g_app_info_add_supports_type() or
2399 * g_app_info_remove_supports_type().
2404 g_app_info_reset_type_associations (const char *content_type)
2406 update_mimeapps_list (NULL, content_type,
2412 * g_app_info_get_default_for_type:
2413 * @content_type: the content type to find a #GAppInfo for
2414 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
2417 * Gets the default #GAppInfo for a given content type.
2419 * Returns: (transfer full): #GAppInfo for given @content_type or
2423 g_app_info_get_default_for_type (const char *content_type,
2424 gboolean must_support_uris)
2426 GList *desktop_entries, *l;
2427 char *user_default = NULL;
2430 g_return_val_if_fail (content_type != NULL, NULL);
2432 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2436 if (user_default != NULL)
2438 info = (GAppInfo *) g_desktop_app_info_new (user_default);
2442 if (must_support_uris && !g_app_info_supports_uris (info))
2444 g_object_unref (info);
2450 g_free (user_default);
2454 g_list_free_full (desktop_entries, g_free);
2458 /* pick the first from the other list that matches our URI
2461 for (l = desktop_entries; l != NULL; l = l->next)
2463 char *desktop_entry = l->data;
2465 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
2468 if (must_support_uris && !g_app_info_supports_uris (info))
2470 g_object_unref (info);
2478 g_list_free_full (desktop_entries, g_free);
2484 * g_app_info_get_default_for_uri_scheme:
2485 * @uri_scheme: a string containing a URI scheme.
2487 * Gets the default application for handling URIs with
2488 * the given URI scheme. A URI scheme is the initial part
2489 * of the URI, up to but not including the ':', e.g. "http",
2492 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
2495 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
2498 char *content_type, *scheme_down;
2500 scheme_down = g_ascii_strdown (uri_scheme, -1);
2501 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
2502 g_free (scheme_down);
2503 app_info = g_app_info_get_default_for_type (content_type, FALSE);
2504 g_free (content_type);
2510 get_apps_from_dir (GHashTable *apps,
2511 const char *dirname,
2515 const char *basename;
2516 char *filename, *subprefix, *desktop_id;
2518 GDesktopAppInfo *appinfo;
2520 dir = g_dir_open (dirname, 0, NULL);
2523 while ((basename = g_dir_read_name (dir)) != NULL)
2525 filename = g_build_filename (dirname, basename, NULL);
2526 if (g_str_has_suffix (basename, ".desktop"))
2528 desktop_id = g_strconcat (prefix, basename, NULL);
2530 /* Use _extended so we catch NULLs too (hidden) */
2531 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
2533 appinfo = g_desktop_app_info_new_from_filename (filename);
2536 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
2538 g_object_unref (appinfo);
2543 if (appinfo || hidden)
2545 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
2549 /* Reuse instead of strdup here */
2550 appinfo->desktop_id = desktop_id;
2555 g_free (desktop_id);
2559 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
2561 subprefix = g_strconcat (prefix, basename, "-", NULL);
2562 get_apps_from_dir (apps, filename, subprefix);
2574 * g_app_info_get_all:
2576 * Gets a list of all of the applications currently registered
2579 * For desktop files, this includes applications that have
2580 * <literal>NoDisplay=true</literal> set or are excluded from
2581 * display by means of <literal>OnlyShowIn</literal> or
2582 * <literal>NotShowIn</literal>. See g_app_info_should_show().
2583 * The returned list does not include applications which have
2584 * the <literal>Hidden</literal> key set.
2586 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
2589 g_app_info_get_all (void)
2591 const char * const *dirs;
2593 GHashTableIter iter;
2598 dirs = get_applications_search_path ();
2600 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
2604 for (i = 0; dirs[i] != NULL; i++)
2605 get_apps_from_dir (apps, dirs[i], "");
2609 g_hash_table_iter_init (&iter, apps);
2610 while (g_hash_table_iter_next (&iter, NULL, &value))
2613 infos = g_list_prepend (infos, value);
2616 g_hash_table_destroy (apps);
2618 return g_list_reverse (infos);
2621 /* Cacheing of mimeinfo.cache and defaults.list files */
2625 GHashTable *mime_info_cache_map;
2626 GHashTable *defaults_list_map;
2627 GHashTable *mimeapps_list_added_map;
2628 GHashTable *mimeapps_list_removed_map;
2629 GHashTable *mimeapps_list_defaults_map;
2630 time_t mime_info_cache_timestamp;
2631 time_t defaults_list_timestamp;
2632 time_t mimeapps_list_timestamp;
2636 GList *dirs; /* mimeinfo.cache and defaults.list */
2637 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
2638 time_t last_stat_time;
2639 guint should_ping_mime_monitor : 1;
2642 static MimeInfoCache *mime_info_cache = NULL;
2643 G_LOCK_DEFINE_STATIC (mime_info_cache);
2645 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2646 const char *mime_type,
2647 char **new_desktop_file_ids);
2649 static MimeInfoCache * mime_info_cache_new (void);
2652 destroy_info_cache_value (gpointer key,
2656 g_list_free_full (value, g_free);
2660 destroy_info_cache_map (GHashTable *info_cache_map)
2662 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
2663 g_hash_table_destroy (info_cache_map);
2667 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2668 const char *cache_file,
2674 filename = g_build_filename (dir->path, cache_file, NULL);
2676 if (g_stat (filename, &buf) < 0)
2683 if (buf.st_mtime != *timestamp)
2689 /* Call with lock held */
2691 remove_all (gpointer key,
2700 mime_info_cache_blow_global_cache (void)
2702 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
2707 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2711 gchar *filename, **mime_types;
2718 if (dir->mime_info_cache_map != NULL &&
2719 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2720 &dir->mime_info_cache_timestamp))
2723 if (dir->mime_info_cache_map != NULL)
2724 destroy_info_cache_map (dir->mime_info_cache_map);
2726 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2727 (GDestroyNotify) g_free,
2730 key_file = g_key_file_new ();
2732 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2734 if (g_stat (filename, &buf) < 0)
2737 if (dir->mime_info_cache_timestamp > 0)
2738 mime_info_cache->should_ping_mime_monitor = TRUE;
2740 dir->mime_info_cache_timestamp = buf.st_mtime;
2742 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2747 if (load_error != NULL)
2750 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2753 if (load_error != NULL)
2756 for (i = 0; mime_types[i] != NULL; i++)
2758 gchar **desktop_file_ids;
2759 char *unaliased_type;
2760 desktop_file_ids = g_key_file_get_string_list (key_file,
2766 if (desktop_file_ids == NULL)
2769 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2770 mime_info_cache_dir_add_desktop_entries (dir,
2773 g_free (unaliased_type);
2775 g_strfreev (desktop_file_ids);
2778 g_strfreev (mime_types);
2779 g_key_file_free (key_file);
2784 g_key_file_free (key_file);
2786 if (mime_types != NULL)
2787 g_strfreev (mime_types);
2790 g_error_free (load_error);
2794 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2798 gchar *filename, **mime_types;
2799 char *unaliased_type;
2800 char **desktop_file_ids;
2807 if (dir->defaults_list_map != NULL &&
2808 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2809 &dir->defaults_list_timestamp))
2812 if (dir->defaults_list_map != NULL)
2813 g_hash_table_destroy (dir->defaults_list_map);
2814 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2815 g_free, (GDestroyNotify)g_strfreev);
2818 key_file = g_key_file_new ();
2820 filename = g_build_filename (dir->path, "defaults.list", NULL);
2821 if (g_stat (filename, &buf) < 0)
2824 if (dir->defaults_list_timestamp > 0)
2825 mime_info_cache->should_ping_mime_monitor = TRUE;
2827 dir->defaults_list_timestamp = buf.st_mtime;
2829 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2833 if (load_error != NULL)
2836 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2838 if (mime_types != NULL)
2840 for (i = 0; mime_types[i] != NULL; i++)
2842 desktop_file_ids = g_key_file_get_string_list (key_file,
2843 DEFAULT_APPLICATIONS_GROUP,
2847 if (desktop_file_ids == NULL)
2850 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2851 g_hash_table_replace (dir->defaults_list_map,
2856 g_strfreev (mime_types);
2859 g_key_file_free (key_file);
2864 g_key_file_free (key_file);
2866 if (mime_types != NULL)
2867 g_strfreev (mime_types);
2870 g_error_free (load_error);
2874 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2878 gchar *filename, **mime_types;
2879 char *unaliased_type;
2880 char **desktop_file_ids;
2888 if (dir->mimeapps_list_added_map != NULL &&
2889 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2890 &dir->mimeapps_list_timestamp))
2893 if (dir->mimeapps_list_added_map != NULL)
2894 g_hash_table_destroy (dir->mimeapps_list_added_map);
2895 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2896 g_free, (GDestroyNotify)g_strfreev);
2898 if (dir->mimeapps_list_removed_map != NULL)
2899 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2900 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2901 g_free, (GDestroyNotify)g_strfreev);
2903 if (dir->mimeapps_list_defaults_map != NULL)
2904 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2905 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2908 key_file = g_key_file_new ();
2910 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2911 if (g_stat (filename, &buf) < 0)
2914 if (dir->mimeapps_list_timestamp > 0)
2915 mime_info_cache->should_ping_mime_monitor = TRUE;
2917 dir->mimeapps_list_timestamp = buf.st_mtime;
2919 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2923 if (load_error != NULL)
2926 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2928 if (mime_types != NULL)
2930 for (i = 0; mime_types[i] != NULL; i++)
2932 desktop_file_ids = g_key_file_get_string_list (key_file,
2933 ADDED_ASSOCIATIONS_GROUP,
2937 if (desktop_file_ids == NULL)
2940 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2941 g_hash_table_replace (dir->mimeapps_list_added_map,
2946 g_strfreev (mime_types);
2949 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2951 if (mime_types != NULL)
2953 for (i = 0; mime_types[i] != NULL; i++)
2955 desktop_file_ids = g_key_file_get_string_list (key_file,
2956 REMOVED_ASSOCIATIONS_GROUP,
2960 if (desktop_file_ids == NULL)
2963 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2964 g_hash_table_replace (dir->mimeapps_list_removed_map,
2969 g_strfreev (mime_types);
2972 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2974 if (mime_types != NULL)
2976 for (i = 0; mime_types[i] != NULL; i++)
2978 desktop_id = g_key_file_get_string (key_file,
2979 DEFAULT_APPLICATIONS_GROUP,
2982 if (desktop_id == NULL)
2985 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2986 g_hash_table_replace (dir->mimeapps_list_defaults_map,
2991 g_strfreev (mime_types);
2994 g_key_file_free (key_file);
2999 g_key_file_free (key_file);
3001 if (mime_types != NULL)
3002 g_strfreev (mime_types);
3005 g_error_free (load_error);
3008 static MimeInfoCacheDir *
3009 mime_info_cache_dir_new (const char *path)
3011 MimeInfoCacheDir *dir;
3013 dir = g_new0 (MimeInfoCacheDir, 1);
3014 dir->path = g_strdup (path);
3020 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
3025 if (dir->mime_info_cache_map != NULL)
3027 destroy_info_cache_map (dir->mime_info_cache_map);
3028 dir->mime_info_cache_map = NULL;
3032 if (dir->defaults_list_map != NULL)
3034 g_hash_table_destroy (dir->defaults_list_map);
3035 dir->defaults_list_map = NULL;
3038 if (dir->mimeapps_list_added_map != NULL)
3040 g_hash_table_destroy (dir->mimeapps_list_added_map);
3041 dir->mimeapps_list_added_map = NULL;
3044 if (dir->mimeapps_list_removed_map != NULL)
3046 g_hash_table_destroy (dir->mimeapps_list_removed_map);
3047 dir->mimeapps_list_removed_map = NULL;
3050 if (dir->mimeapps_list_defaults_map != NULL)
3052 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
3053 dir->mimeapps_list_defaults_map = NULL;
3060 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
3061 const char *mime_type,
3062 char **new_desktop_file_ids)
3064 GList *desktop_file_ids;
3067 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
3070 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
3072 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
3073 desktop_file_ids = g_list_append (desktop_file_ids,
3074 g_strdup (new_desktop_file_ids[i]));
3077 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
3081 mime_info_cache_init_dir_lists (void)
3083 const char * const *dirs;
3086 mime_info_cache = mime_info_cache_new ();
3088 dirs = get_applications_search_path ();
3090 for (i = 0; dirs[i] != NULL; i++)
3092 MimeInfoCacheDir *dir;
3094 dir = mime_info_cache_dir_new (dirs[i]);
3098 mime_info_cache_dir_init (dir);
3099 mime_info_cache_dir_init_defaults_list (dir);
3100 mime_info_cache_dir_init_mimeapps_list (dir);
3102 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
3108 mime_info_cache_update_dir_lists (void)
3112 tmp = mime_info_cache->dirs;
3116 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
3118 /* No need to do this if we had file monitors... */
3119 mime_info_cache_blow_global_cache ();
3120 mime_info_cache_dir_init (dir);
3121 mime_info_cache_dir_init_defaults_list (dir);
3122 mime_info_cache_dir_init_mimeapps_list (dir);
3129 mime_info_cache_init (void)
3131 G_LOCK (mime_info_cache);
3132 if (mime_info_cache == NULL)
3133 mime_info_cache_init_dir_lists ();
3139 if (now >= mime_info_cache->last_stat_time + 10)
3141 mime_info_cache_update_dir_lists ();
3142 mime_info_cache->last_stat_time = now;
3146 if (mime_info_cache->should_ping_mime_monitor)
3148 /* g_idle_add (emit_mime_changed, NULL); */
3149 mime_info_cache->should_ping_mime_monitor = FALSE;
3152 G_UNLOCK (mime_info_cache);
3155 static MimeInfoCache *
3156 mime_info_cache_new (void)
3158 MimeInfoCache *cache;
3160 cache = g_new0 (MimeInfoCache, 1);
3162 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
3163 (GDestroyNotify) g_free,
3164 (GDestroyNotify) g_free);
3169 mime_info_cache_free (MimeInfoCache *cache)
3174 g_list_free_full (cache->dirs, (GDestroyNotify) mime_info_cache_dir_free);
3175 g_hash_table_destroy (cache->global_defaults_cache);
3180 * mime_info_cache_reload:
3181 * @dir: directory path which needs reloading.
3183 * Reload the mime information for the @dir.
3186 mime_info_cache_reload (const char *dir)
3188 /* FIXME: just reload the dir that needs reloading,
3189 * don't blow the whole cache
3191 if (mime_info_cache != NULL)
3193 G_LOCK (mime_info_cache);
3194 mime_info_cache_free (mime_info_cache);
3195 mime_info_cache = NULL;
3196 G_UNLOCK (mime_info_cache);
3201 append_desktop_entry (GList *list,
3202 const char *desktop_entry,
3203 GList *removed_entries)
3205 /* Add if not already in list, and valid */
3206 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
3207 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
3208 list = g_list_prepend (list, g_strdup (desktop_entry));
3214 * get_all_desktop_entries_for_mime_type:
3215 * @mime_type: a mime type.
3216 * @except: NULL or a strv list
3218 * Returns all the desktop ids for @mime_type. The desktop files
3219 * are listed in an order so that default applications are listed before
3220 * non-default ones, and handlers for inherited mimetypes are listed
3221 * after the base ones.
3223 * Optionally doesn't list the desktop ids given in the @except
3225 * Return value: a #GList containing the desktop ids which claim
3226 * to handle @mime_type.
3229 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
3230 const char **except,
3231 gboolean include_fallback,
3232 char **explicit_default)
3234 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
3235 MimeInfoCacheDir *dir;
3236 char *mime_type, *default_entry = NULL;
3237 char *old_default_entry = NULL;
3240 char **default_entries;
3241 char **removed_associations;
3246 mime_info_cache_init ();
3248 if (include_fallback)
3250 /* collect all ancestors */
3251 mime_types = _g_unix_content_type_get_parents (base_mime_type);
3252 array = g_ptr_array_new ();
3253 for (i = 0; mime_types[i]; i++)
3254 g_ptr_array_add (array, mime_types[i]);
3255 g_free (mime_types);
3256 for (i = 0; i < array->len; i++)
3258 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
3259 for (j = 0; anc[j]; j++)
3261 for (k = 0; k < array->len; k++)
3263 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
3266 if (k == array->len) /* not found */
3267 g_ptr_array_add (array, g_strdup (anc[j]));
3271 g_ptr_array_add (array, NULL);
3272 mime_types = (char **)g_ptr_array_free (array, FALSE);
3276 mime_types = g_malloc0 (2 * sizeof (gchar *));
3277 mime_types[0] = g_strdup (base_mime_type);
3278 mime_types[1] = NULL;
3281 G_LOCK (mime_info_cache);
3283 removed_entries = NULL;
3284 desktop_entries = NULL;
3286 for (i = 0; except != NULL && except[i] != NULL; i++)
3287 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
3289 for (i = 0; mime_types[i] != NULL; i++)
3291 mime_type = mime_types[i];
3293 /* Go through all apps listed in user and system dirs */
3294 for (dir_list = mime_info_cache->dirs;
3296 dir_list = dir_list->next)
3298 dir = dir_list->data;
3300 /* Pick the explicit default application if we got no result earlier
3301 * (ie, for more specific mime types)
3303 if (desktop_entries == NULL)
3305 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
3309 /* Save the default entry if it's the first one we encounter */
3310 if (default_entry == NULL)
3311 default_entry = g_strdup (entry);
3315 /* Then added associations from mimeapps.list */
3316 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
3317 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3318 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3320 /* Then removed associations from mimeapps.list */
3321 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
3322 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
3323 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
3325 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
3326 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
3327 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3329 if (default_entry == NULL && old_default_entry == NULL)
3330 old_default_entry = g_strdup (default_entries[j]);
3332 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3336 /* Go through all entries that support the mimetype */
3337 for (dir_list = mime_info_cache->dirs;
3339 dir_list = dir_list->next)
3341 dir = dir_list->data;
3343 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
3344 for (tmp = list; tmp != NULL; tmp = tmp->next)
3345 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
3349 G_UNLOCK (mime_info_cache);
3351 g_strfreev (mime_types);
3353 /* If we have no default from mimeapps.list, take it from
3354 * defaults.list intead.
3356 * If we do have a default from mimeapps.list, free any one that came
3357 * from defaults.list.
3359 if (default_entry == NULL)
3360 default_entry = old_default_entry;
3362 g_free (old_default_entry);
3364 if (explicit_default != NULL)
3365 *explicit_default = default_entry;
3367 g_free (default_entry);
3369 g_list_free_full (removed_entries, g_free);
3371 desktop_entries = g_list_reverse (desktop_entries);
3373 return desktop_entries;
3376 /* GDesktopAppInfoLookup interface: */
3378 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
3380 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
3381 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
3384 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
3389 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
3390 * @lookup: a #GDesktopAppInfoLookup
3391 * @uri_scheme: a string containing a URI scheme.
3393 * Gets the default application for launching applications
3394 * using this URI scheme for a particular GDesktopAppInfoLookup
3397 * The GDesktopAppInfoLookup interface and this function is used
3398 * to implement g_app_info_get_default_for_uri_scheme() backends
3399 * in a GIO module. There is no reason for applications to use it
3400 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
3402 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3404 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
3407 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
3408 const char *uri_scheme)
3410 GDesktopAppInfoLookupIface *iface;
3412 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
3414 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
3416 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
3419 G_GNUC_END_IGNORE_DEPRECATIONS
3422 * g_desktop_app_info_get_startup_wm_class:
3423 * @app_info: a #GDesktopAppInfo that supports startup notify
3425 * Retrieves the StartupWMClass field from @app_info. This represents the
3426 * WM_CLASS property of the main window of the application, if launched through
3429 * Returns: (transfer none): the startup WM class, or %NULL if none is set
3430 * in the desktop file.
3435 g_desktop_app_info_get_startup_wm_class (GDesktopAppInfo *app_info)
3437 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (app_info), NULL);
3439 return app_info->startup_wm_class;