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>
30 #ifdef HAVE_CRT_EXTERNS_H
31 #include <crt_externs.h>
34 #include "gcontenttypeprivate.h"
35 #include "gdesktopappinfo.h"
38 #include "gthemedicon.h"
39 #include "gfileicon.h"
40 #include <glib/gstdio.h>
42 #include "giomodule-priv.h"
47 * SECTION:gdesktopappinfo
48 * @title: GDesktopAppInfo
49 * @short_description: Application information from desktop files
50 * @include: gio/gdesktopappinfo.h
52 * #GDesktopAppInfo is an implementation of #GAppInfo based on
55 * Note that <filename><gio/gdesktopappinfo.h></filename> belongs to
56 * the UNIX-specific GIO interfaces, thus you have to use the
57 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
60 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
61 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
62 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
63 #define MIME_CACHE_GROUP "MIME Cache"
64 #define GENERIC_NAME_KEY "GenericName"
65 #define FULL_NAME_KEY "X-GNOME-FullName"
66 #define KEYWORDS_KEY "Keywords"
67 #define STARTUP_WM_CLASS_KEY "StartupWMClass"
74 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
75 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type,
77 gboolean include_fallback,
78 char **explicit_default);
79 static void mime_info_cache_reload (const char *dir);
80 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
86 * Information about an installed application from a desktop file.
88 struct _GDesktopAppInfo
90 GObject parent_instance;
111 char *startup_wm_class;
117 guint startup_notify : 1;
122 UPDATE_MIME_NONE = 1 << 0,
123 UPDATE_MIME_SET_DEFAULT = 1 << 1,
124 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
125 UPDATE_MIME_REMOVE = 1 << 3,
126 UPDATE_MIME_SET_LAST_USED = 1 << 4,
129 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
130 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
131 g_desktop_app_info_iface_init))
133 G_LOCK_DEFINE_STATIC (g_desktop_env);
134 static gchar *g_desktop_env = NULL;
137 search_path_init (gpointer data)
140 const char * const *data_dirs;
141 const char *user_data_dir;
144 data_dirs = g_get_system_data_dirs ();
145 length = g_strv_length ((char **) data_dirs);
147 args = g_new (char *, length + 2);
150 user_data_dir = g_get_user_data_dir ();
151 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
152 for (i = 0; i < length; i++)
153 args[j++] = g_build_filename (data_dirs[i],
154 "applications", NULL);
160 static const char * const *
161 get_applications_search_path (void)
163 static GOnce once_init = G_ONCE_INIT;
164 return g_once (&once_init, search_path_init, NULL);
168 g_desktop_app_info_finalize (GObject *object)
170 GDesktopAppInfo *info;
172 info = G_DESKTOP_APP_INFO (object);
174 g_free (info->desktop_id);
175 g_free (info->filename);
178 g_key_file_unref (info->keyfile);
181 g_free (info->generic_name);
182 g_free (info->fullname);
183 g_free (info->comment);
184 g_free (info->icon_name);
186 g_object_unref (info->icon);
187 g_strfreev (info->keywords);
188 g_strfreev (info->only_show_in);
189 g_strfreev (info->not_show_in);
190 g_free (info->try_exec);
192 g_free (info->binary);
194 g_free (info->categories);
195 g_free (info->startup_wm_class);
196 g_strfreev (info->mime_types);
198 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
202 g_desktop_app_info_set_property(GObject *object,
207 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
212 self->filename = g_value_dup_string (value);
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222 g_desktop_app_info_get_property (GObject *object,
227 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
232 g_value_set_string (value, self->filename);
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
243 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
245 gobject_class->get_property = g_desktop_app_info_get_property;
246 gobject_class->set_property = g_desktop_app_info_set_property;
247 gobject_class->finalize = g_desktop_app_info_finalize;
250 * GDesktopAppInfo:filename:
252 * The origin filename of this #GDesktopAppInfo
254 g_object_class_install_property (gobject_class,
256 g_param_spec_string ("filename", "Filename", "",
258 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
262 g_desktop_app_info_init (GDesktopAppInfo *local)
267 binary_from_exec (const char *exec)
269 const char *p, *start;
275 while (*p != ' ' && *p != 0)
278 return g_strndup (start, p - start);
283 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
290 start_group = g_key_file_get_start_group (key_file);
291 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
293 g_free (start_group);
296 g_free (start_group);
298 type = g_key_file_get_string (key_file,
299 G_KEY_FILE_DESKTOP_GROUP,
300 G_KEY_FILE_DESKTOP_KEY_TYPE,
302 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
309 try_exec = g_key_file_get_string (key_file,
310 G_KEY_FILE_DESKTOP_GROUP,
311 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
313 if (try_exec && try_exec[0] != '\0')
316 t = g_find_program_in_path (try_exec);
325 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
326 info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
327 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
328 info->keywords = g_key_file_get_locale_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, KEYWORDS_KEY, NULL, NULL, NULL);
329 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
330 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
331 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
332 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);
333 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);
334 info->try_exec = try_exec;
335 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
336 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
337 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
338 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
339 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
340 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
341 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
342 info->startup_wm_class = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, STARTUP_WM_CLASS_KEY, NULL);
343 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);
348 if (g_path_is_absolute (info->icon_name))
352 file = g_file_new_for_path (info->icon_name);
353 info->icon = g_file_icon_new (file);
354 g_object_unref (file);
360 /* Work around a common mistake in desktop files */
361 if ((p = strrchr (info->icon_name, '.')) != NULL &&
362 (strcmp (p, ".png") == 0 ||
363 strcmp (p, ".xpm") == 0 ||
364 strcmp (p, ".svg") == 0))
367 info->icon = g_themed_icon_new (info->icon_name);
372 info->binary = binary_from_exec (info->exec);
374 if (info->path && info->path[0] == '\0')
380 info->keyfile = g_key_file_ref (key_file);
386 g_desktop_app_info_load_file (GDesktopAppInfo *self)
389 gboolean retval = FALSE;
391 g_return_val_if_fail (self->filename != NULL, FALSE);
393 key_file = g_key_file_new ();
395 if (g_key_file_load_from_file (key_file,
400 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
403 g_key_file_unref (key_file);
408 * g_desktop_app_info_new_from_keyfile:
409 * @key_file: an opened #GKeyFile
411 * Creates a new #GDesktopAppInfo.
413 * Returns: a new #GDesktopAppInfo or %NULL on error.
418 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
420 GDesktopAppInfo *info;
422 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
423 info->filename = NULL;
424 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
426 g_object_unref (info);
433 * g_desktop_app_info_new_from_filename:
434 * @filename: the path of a desktop file, in the GLib filename encoding
436 * Creates a new #GDesktopAppInfo.
438 * Returns: a new #GDesktopAppInfo or %NULL on error.
441 g_desktop_app_info_new_from_filename (const char *filename)
443 GDesktopAppInfo *info = NULL;
445 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
446 if (!g_desktop_app_info_load_file (info))
448 g_object_unref (info);
455 * g_desktop_app_info_new:
456 * @desktop_id: the desktop file id
458 * Creates a new #GDesktopAppInfo based on a desktop file id.
460 * A desktop file id is the basename of the desktop file, including the
461 * .desktop extension. GIO is looking for a desktop file with this name
462 * in the <filename>applications</filename> subdirectories of the XDG data
463 * directories (i.e. the directories specified in the
464 * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment
465 * variables). GIO also supports the prefix-to-subdirectory mapping that is
466 * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink>
467 * (i.e. a desktop id of kde-foo.desktop will match
468 * <filename>/usr/share/applications/kde/foo.desktop</filename>).
470 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
473 g_desktop_app_info_new (const char *desktop_id)
475 GDesktopAppInfo *appinfo;
476 const char * const *dirs;
480 dirs = get_applications_search_path ();
482 basename = g_strdup (desktop_id);
484 for (i = 0; dirs[i] != NULL; i++)
489 filename = g_build_filename (dirs[i], desktop_id, NULL);
490 appinfo = g_desktop_app_info_new_from_filename (filename);
496 while ((p = strchr (p, '-')) != NULL)
500 filename = g_build_filename (dirs[i], basename, NULL);
501 appinfo = g_desktop_app_info_new_from_filename (filename);
516 appinfo->desktop_id = g_strdup (desktop_id);
518 if (g_desktop_app_info_get_is_hidden (appinfo))
520 g_object_unref (appinfo);
528 g_desktop_app_info_dup (GAppInfo *appinfo)
530 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
531 GDesktopAppInfo *new_info;
533 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
535 new_info->filename = g_strdup (info->filename);
536 new_info->desktop_id = g_strdup (info->desktop_id);
539 new_info->keyfile = g_key_file_ref (info->keyfile);
541 new_info->name = g_strdup (info->name);
542 new_info->generic_name = g_strdup (info->generic_name);
543 new_info->fullname = g_strdup (info->fullname);
544 new_info->keywords = g_strdupv (info->keywords);
545 new_info->comment = g_strdup (info->comment);
546 new_info->nodisplay = info->nodisplay;
547 new_info->icon_name = g_strdup (info->icon_name);
549 new_info->icon = g_object_ref (info->icon);
550 new_info->only_show_in = g_strdupv (info->only_show_in);
551 new_info->not_show_in = g_strdupv (info->not_show_in);
552 new_info->try_exec = g_strdup (info->try_exec);
553 new_info->exec = g_strdup (info->exec);
554 new_info->binary = g_strdup (info->binary);
555 new_info->path = g_strdup (info->path);
556 new_info->hidden = info->hidden;
557 new_info->terminal = info->terminal;
558 new_info->startup_notify = info->startup_notify;
560 return G_APP_INFO (new_info);
564 g_desktop_app_info_equal (GAppInfo *appinfo1,
567 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
568 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
570 if (info1->desktop_id == NULL ||
571 info2->desktop_id == NULL)
572 return info1 == info2;
574 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
578 g_desktop_app_info_get_id (GAppInfo *appinfo)
580 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
582 return info->desktop_id;
586 g_desktop_app_info_get_name (GAppInfo *appinfo)
588 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
590 if (info->name == NULL)
596 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
598 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
600 if (info->fullname == NULL)
601 return g_desktop_app_info_get_name (appinfo);
602 return info->fullname;
606 * g_desktop_app_info_get_is_hidden:
607 * @info: a #GDesktopAppInfo.
609 * A desktop file is hidden if the Hidden key in it is
612 * Returns: %TRUE if hidden, %FALSE otherwise.
615 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
621 * g_desktop_app_info_get_filename:
622 * @info: a #GDesktopAppInfo
624 * When @info was created from a known filename, return it. In some
625 * situations such as the #GDesktopAppInfo returned from
626 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
628 * Returns: The full path to the file for @info, or %NULL if not known.
632 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
634 return info->filename;
638 g_desktop_app_info_get_description (GAppInfo *appinfo)
640 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
642 return info->comment;
646 g_desktop_app_info_get_executable (GAppInfo *appinfo)
648 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
654 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
656 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
662 g_desktop_app_info_get_icon (GAppInfo *appinfo)
664 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
670 * g_desktop_app_info_get_categories:
671 * @info: a #GDesktopAppInfo
673 * Gets the categories from the desktop file.
675 * Returns: The unparsed Categories key from the desktop file;
676 * i.e. no attempt is made to split it by ';' or validate it.
679 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
681 return info->categories;
685 * g_desktop_app_info_get_keywords:
686 * @info: a #GDesktopAppInfo
688 * Gets the keywords from the desktop file.
690 * Returns: (transfer none): The value of the Keywords key
695 g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
697 return (const char * const *)info->keywords;
701 * g_desktop_app_info_get_generic_name:
702 * @info: a #GDesktopAppInfo
704 * Gets the generic name from the destkop file.
706 * Returns: The value of the GenericName key
709 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
711 return info->generic_name;
715 * g_desktop_app_info_get_nodisplay:
716 * @info: a #GDesktopAppInfo
718 * Gets the value of the NoDisplay key, which helps determine if the
719 * application info should be shown in menus. See
720 * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
722 * Returns: The value of the NoDisplay key
727 g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
729 return info->nodisplay;
733 * g_desktop_app_info_get_show_in:
734 * @info: a #GDesktopAppInfo
735 * @desktop_env: a string specifying a desktop name
737 * Checks if the application info should be shown in menus that list available
738 * applications for a specific name of the desktop, based on the
739 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys.
741 * If @desktop_env is %NULL, then the name of the desktop set with
742 * g_desktop_app_info_set_desktop_env() is used.
744 * Note that g_app_info_should_show() for @info will include this check (with
745 * %NULL for @desktop_env) as well as additional checks.
747 * Returns: %TRUE if the @info should be shown in @desktop_env according to the
748 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys, %FALSE
754 g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
755 const gchar *desktop_env)
760 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
763 G_LOCK (g_desktop_env);
764 desktop_env = g_desktop_env;
765 G_UNLOCK (g_desktop_env);
768 if (info->only_show_in)
770 if (desktop_env == NULL)
774 for (i = 0; info->only_show_in[i] != NULL; i++)
776 if (strcmp (info->only_show_in[i], desktop_env) == 0)
786 if (info->not_show_in && desktop_env)
788 for (i = 0; info->not_show_in[i] != NULL; i++)
790 if (strcmp (info->not_show_in[i], desktop_env) == 0)
799 expand_macro_single (char macro, char *uri)
805 file = g_file_new_for_uri (uri);
806 path = g_file_get_path (file);
807 g_object_unref (file);
813 result = g_shell_quote (uri);
818 result = g_shell_quote (path);
824 name = g_path_get_dirname (path);
825 result = g_shell_quote (name);
833 name = g_path_get_basename (path);
834 result = g_shell_quote (name);
846 expand_macro (char macro,
848 GDesktopAppInfo *info,
851 GList *uris = *uri_list;
853 gboolean force_file_uri;
854 char force_file_uri_macro;
857 g_return_if_fail (exec != NULL);
859 /* On %u and %U, pass POSIX file path pointing to the URI via
860 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
861 * running or the URI doesn't have a POSIX file path via FUSE
862 * we'll just pass the URI.
864 force_file_uri_macro = macro;
865 force_file_uri = FALSE;
871 force_file_uri_macro = 'f';
872 force_file_uri = TRUE;
875 force_file_uri_macro = 'F';
876 force_file_uri = TRUE;
892 if (!force_file_uri ||
893 /* Pass URI if it contains an anchor */
894 strchr (uri, '#') != NULL)
896 expanded = expand_macro_single (macro, uri);
900 expanded = expand_macro_single (force_file_uri_macro, uri);
901 if (expanded == NULL)
902 expanded = expand_macro_single (macro, uri);
907 g_string_append (exec, expanded);
923 if (!force_file_uri ||
924 /* Pass URI if it contains an anchor */
925 strchr (uri, '#') != NULL)
927 expanded = expand_macro_single (macro, uri);
931 expanded = expand_macro_single (force_file_uri_macro, uri);
932 if (expanded == NULL)
933 expanded = expand_macro_single (macro, uri);
938 g_string_append (exec, expanded);
944 if (uris != NULL && expanded)
945 g_string_append_c (exec, ' ');
953 g_string_append (exec, "--icon ");
954 expanded = g_shell_quote (info->icon_name);
955 g_string_append (exec, expanded);
963 expanded = g_shell_quote (info->name);
964 g_string_append (exec, expanded);
972 expanded = g_shell_quote (info->filename);
973 g_string_append (exec, expanded);
978 case 'm': /* deprecated */
982 g_string_append_c (exec, '%');
990 expand_application_parameters (GDesktopAppInfo *info,
996 GList *uri_list = *uris;
997 const char *p = info->exec;
998 GString *expanded_exec;
1001 if (info->exec == NULL)
1003 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1004 _("Desktop file didn't specify Exec field"));
1008 expanded_exec = g_string_new (NULL);
1012 if (p[0] == '%' && p[1] != '\0')
1014 expand_macro (p[1], expanded_exec, info, uris);
1018 g_string_append_c (expanded_exec, *p);
1023 /* No file substitutions */
1024 if (uri_list == *uris && uri_list != NULL)
1026 /* If there is no macro default to %f. This is also what KDE does */
1027 g_string_append_c (expanded_exec, ' ');
1028 expand_macro ('f', expanded_exec, info, uris);
1031 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
1032 g_string_free (expanded_exec, TRUE);
1037 prepend_terminal_to_vector (int *argc,
1044 char **term_argv = NULL;
1049 g_return_val_if_fail (argc != NULL, FALSE);
1050 g_return_val_if_fail (argv != NULL, FALSE);
1058 /* compute size if not given */
1061 for (i = 0; the_argv[i] != NULL; i++)
1067 term_argv = g_new0 (char *, 3);
1069 check = g_find_program_in_path ("gnome-terminal");
1072 term_argv[0] = check;
1073 /* Note that gnome-terminal takes -x and
1074 * as -e in gnome-terminal is broken we use that. */
1075 term_argv[1] = g_strdup ("-x");
1080 check = g_find_program_in_path ("nxterm");
1082 check = g_find_program_in_path ("color-xterm");
1084 check = g_find_program_in_path ("rxvt");
1086 check = g_find_program_in_path ("xterm");
1088 check = g_find_program_in_path ("dtterm");
1091 check = g_strdup ("xterm");
1092 g_warning ("couldn't find a terminal, falling back to xterm");
1094 term_argv[0] = check;
1095 term_argv[1] = g_strdup ("-e");
1098 real_argc = term_argc + *argc;
1099 real_argv = g_new (char *, real_argc + 1);
1101 for (i = 0; i < term_argc; i++)
1102 real_argv[i] = term_argv[i];
1104 for (j = 0; j < *argc; j++, i++)
1105 real_argv[i] = (char *)the_argv[j];
1107 real_argv[i] = NULL;
1113 /* we use g_free here as we sucked all the inner strings
1114 * out from it into real_argv */
1119 #endif /* G_OS_WIN32 */
1123 create_files_for_uris (GList *uris)
1130 for (iter = uris; iter; iter = iter->next)
1132 GFile *file = g_file_new_for_uri ((char *)iter->data);
1133 res = g_list_prepend (res, file);
1136 return g_list_reverse (res);
1141 GSpawnChildSetupFunc user_setup;
1142 gpointer user_setup_data;
1148 child_setup (gpointer user_data)
1150 ChildSetupData *data = user_data;
1152 if (data->pid_envvar)
1154 pid_t pid = getpid ();
1158 /* Write the pid into the space already reserved for it in the
1159 * environment array. We can't use sprintf because it might
1160 * malloc, so we do it by hand. It's simplest to write the pid
1161 * out backwards first, then copy it over.
1163 for (i = 0; pid; i++, pid /= 10)
1164 buf[i] = (pid % 10) + '0';
1165 for (i--; i >= 0; i--)
1166 *(data->pid_envvar++) = buf[i];
1167 *data->pid_envvar = '\0';
1170 if (data->user_setup)
1171 data->user_setup (data->user_setup_data);
1175 notify_desktop_launch (GDBusConnection *session_bus,
1176 GDesktopAppInfo *info,
1178 const char *display,
1183 GVariantBuilder uri_variant;
1184 GVariantBuilder extras_variant;
1186 const char *desktop_file_id;
1187 const char *gio_desktop_file;
1189 if (session_bus == NULL)
1192 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
1193 for (iter = uris; iter; iter = iter->next)
1194 g_variant_builder_add (&uri_variant, "s", iter->data);
1196 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
1197 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
1198 g_variant_builder_add (&extras_variant, "{sv}",
1202 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
1203 if (gio_desktop_file != NULL)
1204 g_variant_builder_add (&extras_variant, "{sv}",
1205 "origin-desktop-file",
1206 g_variant_new_bytestring (gio_desktop_file));
1207 if (g_get_prgname () != NULL)
1208 g_variant_builder_add (&extras_variant, "{sv}",
1210 g_variant_new_bytestring (g_get_prgname ()));
1211 g_variant_builder_add (&extras_variant, "{sv}",
1214 (gint64)getpid ()));
1217 desktop_file_id = info->filename;
1218 else if (info->desktop_id)
1219 desktop_file_id = info->desktop_id;
1221 desktop_file_id = "";
1223 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
1224 "org.gtk.gio.DesktopAppInfo",
1226 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
1227 g_variant_new_bytestring (desktop_file_id),
1228 display ? display : "",
1232 g_dbus_connection_send_message (session_bus,
1236 g_object_unref (msg);
1239 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
1242 _g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
1244 GAppLaunchContext *launch_context,
1245 GSpawnFlags spawn_flags,
1246 GSpawnChildSetupFunc user_setup,
1247 gpointer user_setup_data,
1248 GDesktopAppLaunchCallback pid_callback,
1249 gpointer pid_callback_data,
1252 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1253 GDBusConnection *session_bus;
1254 gboolean completed = FALSE;
1256 char **argv, **envp;
1258 ChildSetupData data;
1260 g_return_val_if_fail (appinfo != NULL, FALSE);
1264 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
1267 envp = g_app_launch_context_get_environment (launch_context);
1269 envp = g_get_environ ();
1274 GList *launched_uris;
1276 char *display, *sn_id;
1279 if (!expand_application_parameters (info, &uris,
1280 &argc, &argv, error))
1283 /* Get the subset of URIs we're launching with this process */
1284 launched_uris = NULL;
1285 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
1286 launched_uris = g_list_prepend (launched_uris, iter->data);
1287 launched_uris = g_list_reverse (launched_uris);
1289 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
1291 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1292 _("Unable to find terminal required for application"));
1296 data.user_setup = user_setup;
1297 data.user_setup_data = user_setup_data;
1301 envp = g_environ_setenv (envp,
1302 "GIO_LAUNCHED_DESKTOP_FILE",
1305 envp = g_environ_setenv (envp,
1306 "GIO_LAUNCHED_DESKTOP_FILE_PID",
1307 "XXXXXXXXXXXXXXXXXXXX", /* filled in child_setup */
1309 data.pid_envvar = (char *)g_environ_getenv (envp, "GIO_LAUNCHED_DESKTOP_FILE_PID");
1313 data.pid_envvar = NULL;
1320 GList *launched_files = create_files_for_uris (launched_uris);
1322 display = g_app_launch_context_get_display (launch_context,
1326 envp = g_environ_setenv (envp, "DISPLAY", display, TRUE);
1328 if (info->startup_notify)
1330 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
1333 envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
1336 g_list_free_full (launched_files, g_object_unref);
1339 if (!g_spawn_async (info->path,
1349 g_app_launch_context_launch_failed (launch_context, sn_id);
1353 g_list_free (launched_uris);
1358 if (pid_callback != NULL)
1359 pid_callback (info, pid, pid_callback_data);
1361 if (launch_context != NULL)
1363 GVariantBuilder builder;
1364 GVariant *platform_data;
1366 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
1367 g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (pid));
1369 g_variant_builder_add (&builder, "{sv}", "startup-notification-id", g_variant_new_string (sn_id));
1370 platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
1371 g_signal_emit_by_name (launch_context, "launched", info, platform_data);
1372 g_variant_unref (platform_data);
1375 notify_desktop_launch (session_bus,
1384 g_list_free (launched_uris);
1389 while (uris != NULL);
1391 /* TODO - need to handle the process exiting immediately
1392 * after launching an app. See http://bugzilla.gnome.org/606960
1394 if (session_bus != NULL)
1396 /* This asynchronous flush holds a reference until it completes,
1397 * which ensures that the following unref won't immediately kill
1398 * the connection if we were the initial owner.
1400 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
1406 g_clear_object (&session_bus);
1414 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
1416 GAppLaunchContext *launch_context,
1419 return _g_desktop_app_info_launch_uris_internal (appinfo, uris,
1421 _SPAWN_FLAGS_DEFAULT,
1422 NULL, NULL, NULL, NULL,
1427 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1429 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1431 return info->exec &&
1432 ((strstr (info->exec, "%u") != NULL) ||
1433 (strstr (info->exec, "%U") != NULL));
1437 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1439 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1441 return info->exec &&
1442 ((strstr (info->exec, "%f") != NULL) ||
1443 (strstr (info->exec, "%F") != NULL));
1447 g_desktop_app_info_launch (GAppInfo *appinfo,
1449 GAppLaunchContext *launch_context,
1459 uri = g_file_get_uri (files->data);
1460 uris = g_list_prepend (uris, uri);
1461 files = files->next;
1464 uris = g_list_reverse (uris);
1466 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1468 g_list_free_full (uris, g_free);
1474 * g_desktop_app_info_launch_uris_as_manager:
1475 * @appinfo: a #GDesktopAppInfo
1476 * @uris: (element-type utf8): List of URIs
1477 * @launch_context: a #GAppLaunchContext
1478 * @spawn_flags: #GSpawnFlags, used for each process
1479 * @user_setup: (scope call): a #GSpawnChildSetupFunc, used once for
1481 * @user_setup_data: (closure user_setup): User data for @user_setup
1482 * @pid_callback: (scope call): Callback for child processes
1483 * @pid_callback_data: (closure pid_callback): User data for @callback
1484 * @error: return location for a #GError, or %NULL
1486 * This function performs the equivalent of g_app_info_launch_uris(),
1487 * but is intended primarily for operating system components that
1488 * launch applications. Ordinary applications should use
1489 * g_app_info_launch_uris().
1491 * In contrast to g_app_info_launch_uris(), all processes created will
1492 * always be run directly as children as if by the UNIX fork()/exec()
1495 * This guarantee allows additional control over the exact environment
1496 * of the child processes, which is provided via a setup function
1497 * @user_setup, as well as the process identifier of each child process
1498 * via @pid_callback. See g_spawn_async() for more information about the
1499 * semantics of the @user_setup function.
1501 * Returns: %TRUE on successful launch, %FALSE otherwise.
1504 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
1506 GAppLaunchContext *launch_context,
1507 GSpawnFlags spawn_flags,
1508 GSpawnChildSetupFunc user_setup,
1509 gpointer user_setup_data,
1510 GDesktopAppLaunchCallback pid_callback,
1511 gpointer pid_callback_data,
1514 return _g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
1526 * g_desktop_app_info_set_desktop_env:
1527 * @desktop_env: a string specifying what desktop this is
1529 * Sets the name of the desktop that the application is running in.
1530 * This is used by g_app_info_should_show() and
1531 * g_desktop_app_info_get_show_in() to evaluate the
1532 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1533 * desktop entry fields.
1535 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1536 * Menu specification</ulink> recognizes the following:
1538 * <member>GNOME</member>
1539 * <member>KDE</member>
1540 * <member>ROX</member>
1541 * <member>XFCE</member>
1542 * <member>LXDE</member>
1543 * <member>Unity</member>
1544 * <member>Old</member>
1547 * Should be called only once; subsequent calls are ignored.
1550 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1552 G_LOCK (g_desktop_env);
1554 g_desktop_env = g_strdup (desktop_env);
1555 G_UNLOCK (g_desktop_env);
1559 g_desktop_app_info_should_show (GAppInfo *appinfo)
1561 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1563 if (info->nodisplay)
1566 return g_desktop_app_info_get_show_in (info, NULL);
1575 ensure_dir (DirType type,
1578 char *path, *display_name;
1581 if (type == APP_DIR)
1582 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1584 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1587 if (g_mkdir_with_parents (path, 0700) == 0)
1591 display_name = g_filename_display_name (path);
1592 if (type == APP_DIR)
1593 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1594 _("Can't create user application configuration folder %s: %s"),
1595 display_name, g_strerror (errsv));
1597 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1598 _("Can't create user MIME configuration folder %s: %s"),
1599 display_name, g_strerror (errsv));
1601 g_free (display_name);
1608 update_mimeapps_list (const char *desktop_id,
1609 const char *content_type,
1610 UpdateMimeFlags flags,
1613 char *dirname, *filename, *string;
1615 gboolean load_succeeded, res;
1616 char **old_list, **list;
1617 gsize length, data_size;
1620 char **content_types;
1622 /* Don't add both at start and end */
1623 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
1624 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
1626 dirname = ensure_dir (APP_DIR, error);
1630 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1633 key_file = g_key_file_new ();
1634 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1635 if (!load_succeeded ||
1636 (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) &&
1637 !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) &&
1638 !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP)))
1640 g_key_file_free (key_file);
1641 key_file = g_key_file_new ();
1646 content_types = g_new (char *, 2);
1647 content_types[0] = g_strdup (content_type);
1648 content_types[1] = NULL;
1652 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
1655 for (k = 0; content_types && content_types[k]; k++)
1657 /* set as default, if requested so */
1658 string = g_key_file_get_string (key_file,
1659 DEFAULT_APPLICATIONS_GROUP,
1663 if (g_strcmp0 (string, desktop_id) != 0 &&
1664 (flags & UPDATE_MIME_SET_DEFAULT))
1667 string = g_strdup (desktop_id);
1669 /* add in the non-default list too, if it's not already there */
1670 flags |= UPDATE_MIME_SET_NON_DEFAULT;
1673 if (string == NULL || desktop_id == NULL)
1674 g_key_file_remove_key (key_file,
1675 DEFAULT_APPLICATIONS_GROUP,
1679 g_key_file_set_string (key_file,
1680 DEFAULT_APPLICATIONS_GROUP,
1689 /* reuse the list from above */
1693 g_strfreev (content_types);
1694 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1697 for (k = 0; content_types && content_types[k]; k++)
1699 /* Add to the right place in the list */
1702 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1703 content_types[k], &length, NULL);
1705 list = g_new (char *, 1 + length + 1);
1709 /* if we're adding a last-used hint, just put the application in front of the list */
1710 if (flags & UPDATE_MIME_SET_LAST_USED)
1712 /* avoid adding this again as non-default later */
1713 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1714 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1716 list[i++] = g_strdup (desktop_id);
1721 for (j = 0; old_list[j] != NULL; j++)
1723 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1725 /* rewrite other entries if they're different from the new one */
1726 list[i++] = g_strdup (old_list[j]);
1728 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1730 /* we encountered an old entry which is equal to the one we're adding as non-default,
1731 * don't change its position in the list.
1733 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1734 list[i++] = g_strdup (old_list[j]);
1739 /* add it at the end of the list */
1740 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1741 list[i++] = g_strdup (desktop_id);
1745 g_strfreev (old_list);
1747 if (list[0] == NULL || desktop_id == NULL)
1748 g_key_file_remove_key (key_file,
1749 ADDED_ASSOCIATIONS_GROUP,
1753 g_key_file_set_string_list (key_file,
1754 ADDED_ASSOCIATIONS_GROUP,
1756 (const char * const *)list, i);
1763 /* reuse the list from above */
1767 g_strfreev (content_types);
1768 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1771 for (k = 0; content_types && content_types[k]; k++)
1773 /* Remove from removed associations group (unless remove) */
1776 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1777 content_types[k], &length, NULL);
1779 list = g_new (char *, 1 + length + 1);
1782 if (flags & UPDATE_MIME_REMOVE)
1783 list[i++] = g_strdup (desktop_id);
1786 for (j = 0; old_list[j] != NULL; j++)
1788 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1789 list[i++] = g_strdup (old_list[j]);
1794 g_strfreev (old_list);
1796 if (list[0] == NULL || desktop_id == NULL)
1797 g_key_file_remove_key (key_file,
1798 REMOVED_ASSOCIATIONS_GROUP,
1802 g_key_file_set_string_list (key_file,
1803 REMOVED_ASSOCIATIONS_GROUP,
1805 (const char * const *)list, i);
1810 g_strfreev (content_types);
1812 data = g_key_file_to_data (key_file, &data_size, error);
1813 g_key_file_free (key_file);
1815 res = g_file_set_contents (filename, data, data_size, error);
1817 mime_info_cache_reload (NULL);
1826 g_desktop_app_info_set_as_last_used_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 /* both add support for the content type and set as last used */
1843 return update_mimeapps_list (info->desktop_id, content_type,
1844 UPDATE_MIME_SET_NON_DEFAULT |
1845 UPDATE_MIME_SET_LAST_USED,
1850 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1851 const char *content_type,
1854 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1856 if (!g_desktop_app_info_ensure_saved (info, error))
1859 if (!info->desktop_id)
1861 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1862 _("Application information lacks an identifier"));
1866 return update_mimeapps_list (info->desktop_id, content_type,
1867 UPDATE_MIME_SET_DEFAULT,
1872 update_program_done (GPid pid,
1876 /* Did the application exit correctly */
1877 if (g_spawn_check_exit_status (status, NULL))
1879 /* Here we could clean out any caches in use */
1884 run_update_command (char *command,
1893 GError *error = NULL;
1896 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1898 if (g_spawn_async ("/", argv,
1900 G_SPAWN_SEARCH_PATH |
1901 G_SPAWN_STDOUT_TO_DEV_NULL |
1902 G_SPAWN_STDERR_TO_DEV_NULL |
1903 G_SPAWN_DO_NOT_REAP_CHILD,
1904 NULL, NULL, /* No setup function */
1907 g_child_watch_add (pid, update_program_done, NULL);
1910 /* If we get an error at this point, it's quite likely the user doesn't
1911 * have an installed copy of either 'update-mime-database' or
1912 * 'update-desktop-database'. I don't think we want to popup an error
1913 * dialog at this point, so we just do a g_warning to give the user a
1914 * chance of debugging it.
1916 g_warning ("%s", error->message);
1923 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1924 const char *extension,
1927 char *filename, *basename, *mimetype;
1931 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1934 dirname = ensure_dir (MIMETYPE_DIR, error);
1938 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1939 filename = g_build_filename (dirname, basename, NULL);
1943 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1945 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1950 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1951 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1952 " <mime-type type=\"%s\">\n"
1953 " <comment>%s document</comment>\n"
1954 " <glob pattern=\"*.%s\"/>\n"
1956 "</mime-info>\n", mimetype, extension, extension);
1958 g_file_set_contents (filename, contents, -1, NULL);
1961 run_update_command ("update-mime-database", "mime");
1965 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1975 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1976 const char *content_type,
1979 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1981 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1984 return update_mimeapps_list (info->desktop_id, content_type,
1985 UPDATE_MIME_SET_NON_DEFAULT,
1990 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1996 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1997 const char *content_type,
2000 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2002 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
2005 return update_mimeapps_list (info->desktop_id, content_type,
2010 static const char **
2011 g_desktop_app_info_get_supported_types (GAppInfo *appinfo)
2013 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2015 return (const char**) info->mime_types;
2020 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
2026 char *data, *desktop_id;
2031 if (info->filename != NULL)
2034 /* This is only used for object created with
2035 * g_app_info_create_from_commandline. All other
2036 * object should have a filename
2039 dirname = ensure_dir (APP_DIR, error);
2043 key_file = g_key_file_new ();
2045 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2046 "Encoding", "UTF-8");
2047 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2048 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
2049 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2050 G_KEY_FILE_DESKTOP_KEY_TYPE,
2051 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
2053 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2054 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
2055 if (info->nodisplay)
2056 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2057 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2059 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2060 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
2062 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2063 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
2065 if (info->generic_name != NULL)
2066 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2067 GENERIC_NAME_KEY, info->generic_name);
2069 if (info->fullname != NULL)
2070 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2071 FULL_NAME_KEY, info->fullname);
2073 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2074 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
2076 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2077 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2079 data = g_key_file_to_data (key_file, &data_size, NULL);
2080 g_key_file_free (key_file);
2082 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
2083 filename = g_build_filename (dirname, desktop_id, NULL);
2084 g_free (desktop_id);
2087 fd = g_mkstemp (filename);
2092 display_name = g_filename_display_name (filename);
2093 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2094 _("Can't create user desktop file %s"), display_name);
2095 g_free (display_name);
2101 desktop_id = g_path_get_basename (filename);
2105 res = g_file_set_contents (filename, data, data_size, error);
2109 g_free (desktop_id);
2114 info->filename = filename;
2115 info->desktop_id = desktop_id;
2117 run_update_command ("update-desktop-database", "applications");
2123 g_desktop_app_info_can_delete (GAppInfo *appinfo)
2125 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2129 if (strstr (info->filename, "/userapp-"))
2130 return g_access (info->filename, W_OK) == 0;
2137 g_desktop_app_info_delete (GAppInfo *appinfo)
2139 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2143 if (g_remove (info->filename) == 0)
2145 update_mimeapps_list (info->desktop_id, NULL,
2149 g_free (info->filename);
2150 info->filename = NULL;
2151 g_free (info->desktop_id);
2152 info->desktop_id = NULL;
2162 * g_app_info_create_from_commandline:
2163 * @commandline: the commandline to use
2164 * @application_name: (allow-none): the application name, or %NULL to use @commandline
2165 * @flags: flags that can specify details of the created #GAppInfo
2166 * @error: a #GError location to store the error occurring, %NULL to ignore.
2168 * Creates a new #GAppInfo from the given information.
2170 * Note that for @commandline, the quoting rules of the Exec key of the
2171 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">freedesktop.org Desktop
2172 * Entry Specification</ulink> are applied. For example, if the @commandline contains
2173 * percent-encoded URIs, the percent-character must be doubled in order to prevent it from
2174 * being swallowed by Exec key unquoting. See the specification for exact quoting rules.
2176 * Returns: (transfer full): new #GAppInfo for given command.
2179 g_app_info_create_from_commandline (const char *commandline,
2180 const char *application_name,
2181 GAppInfoCreateFlags flags,
2186 GDesktopAppInfo *info;
2188 g_return_val_if_fail (commandline, NULL);
2190 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
2192 info->filename = NULL;
2193 info->desktop_id = NULL;
2195 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
2196 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
2197 info->hidden = FALSE;
2198 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
2199 info->exec = g_strconcat (commandline, " %u", NULL);
2201 info->exec = g_strconcat (commandline, " %f", NULL);
2202 info->nodisplay = TRUE;
2203 info->binary = binary_from_exec (info->exec);
2205 if (application_name)
2206 info->name = g_strdup (application_name);
2209 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
2210 split = g_strsplit (commandline, " ", 2);
2211 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
2213 info->name = basename;
2214 if (info->name == NULL)
2215 info->name = g_strdup ("custom");
2217 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
2219 return G_APP_INFO (info);
2223 g_desktop_app_info_iface_init (GAppInfoIface *iface)
2225 iface->dup = g_desktop_app_info_dup;
2226 iface->equal = g_desktop_app_info_equal;
2227 iface->get_id = g_desktop_app_info_get_id;
2228 iface->get_name = g_desktop_app_info_get_name;
2229 iface->get_description = g_desktop_app_info_get_description;
2230 iface->get_executable = g_desktop_app_info_get_executable;
2231 iface->get_icon = g_desktop_app_info_get_icon;
2232 iface->launch = g_desktop_app_info_launch;
2233 iface->supports_uris = g_desktop_app_info_supports_uris;
2234 iface->supports_files = g_desktop_app_info_supports_files;
2235 iface->launch_uris = g_desktop_app_info_launch_uris;
2236 iface->should_show = g_desktop_app_info_should_show;
2237 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
2238 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
2239 iface->add_supports_type = g_desktop_app_info_add_supports_type;
2240 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
2241 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
2242 iface->can_delete = g_desktop_app_info_can_delete;
2243 iface->do_delete = g_desktop_app_info_delete;
2244 iface->get_commandline = g_desktop_app_info_get_commandline;
2245 iface->get_display_name = g_desktop_app_info_get_display_name;
2246 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
2247 iface->get_supported_types = g_desktop_app_info_get_supported_types;
2251 app_info_in_list (GAppInfo *info,
2254 while (list != NULL)
2256 if (g_app_info_equal (info, list->data))
2264 * g_app_info_get_recommended_for_type:
2265 * @content_type: the content type to find a #GAppInfo for
2267 * Gets a list of recommended #GAppInfos for a given content type, i.e.
2268 * those applications which claim to support the given content type exactly,
2269 * and not by MIME type subclassing.
2270 * Note that the first application of the list is the last used one, i.e.
2271 * the last one for which g_app_info_set_as_last_used_for_type() has been
2274 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2275 * for given @content_type or %NULL on error.
2280 g_app_info_get_recommended_for_type (const gchar *content_type)
2282 GList *desktop_entries, *l;
2284 GDesktopAppInfo *info;
2286 g_return_val_if_fail (content_type != NULL, NULL);
2288 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
2291 for (l = desktop_entries; l != NULL; l = l->next)
2293 char *desktop_entry = l->data;
2295 info = g_desktop_app_info_new (desktop_entry);
2298 if (app_info_in_list (G_APP_INFO (info), infos))
2299 g_object_unref (info);
2301 infos = g_list_prepend (infos, info);
2303 g_free (desktop_entry);
2306 g_list_free (desktop_entries);
2308 return g_list_reverse (infos);
2312 * g_app_info_get_fallback_for_type:
2313 * @content_type: the content type to find a #GAppInfo for
2315 * Gets a list of fallback #GAppInfos for a given content type, i.e.
2316 * those applications which claim to support the given content type
2317 * by MIME type subclassing and not directly.
2319 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2320 * for given @content_type or %NULL on error.
2325 g_app_info_get_fallback_for_type (const gchar *content_type)
2327 GList *desktop_entries, *l;
2328 GList *infos, *recommended_infos;
2329 GDesktopAppInfo *info;
2331 g_return_val_if_fail (content_type != NULL, NULL);
2333 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
2334 recommended_infos = g_app_info_get_recommended_for_type (content_type);
2337 for (l = desktop_entries; l != NULL; l = l->next)
2339 char *desktop_entry = l->data;
2341 info = g_desktop_app_info_new (desktop_entry);
2344 if (app_info_in_list (G_APP_INFO (info), infos) ||
2345 app_info_in_list (G_APP_INFO (info), recommended_infos))
2346 g_object_unref (info);
2348 infos = g_list_prepend (infos, info);
2350 g_free (desktop_entry);
2353 g_list_free (desktop_entries);
2354 g_list_free_full (recommended_infos, g_object_unref);
2356 return g_list_reverse (infos);
2360 * g_app_info_get_all_for_type:
2361 * @content_type: the content type to find a #GAppInfo for
2363 * Gets a list of all #GAppInfos for a given content type,
2364 * including the recommended and fallback #GAppInfos. See
2365 * g_app_info_get_recommended_for_type() and
2366 * g_app_info_get_fallback_for_type().
2368 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2369 * for given @content_type or %NULL on error.
2372 g_app_info_get_all_for_type (const char *content_type)
2374 GList *desktop_entries, *l;
2376 char *user_default = NULL;
2377 GDesktopAppInfo *info;
2379 g_return_val_if_fail (content_type != NULL, NULL);
2381 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2384 /* put the user default in front of the list, for compatibility */
2385 if (user_default != NULL)
2387 info = g_desktop_app_info_new (user_default);
2390 infos = g_list_prepend (infos, info);
2393 g_free (user_default);
2395 for (l = desktop_entries; l != NULL; l = l->next)
2397 char *desktop_entry = l->data;
2399 info = g_desktop_app_info_new (desktop_entry);
2402 if (app_info_in_list (G_APP_INFO (info), infos))
2403 g_object_unref (info);
2405 infos = g_list_prepend (infos, info);
2407 g_free (desktop_entry);
2410 g_list_free (desktop_entries);
2412 return g_list_reverse (infos);
2416 * g_app_info_reset_type_associations:
2417 * @content_type: a content type
2419 * Removes all changes to the type associations done by
2420 * g_app_info_set_as_default_for_type(),
2421 * g_app_info_set_as_default_for_extension(),
2422 * g_app_info_add_supports_type() or
2423 * g_app_info_remove_supports_type().
2428 g_app_info_reset_type_associations (const char *content_type)
2430 update_mimeapps_list (NULL, content_type,
2436 * g_app_info_get_default_for_type:
2437 * @content_type: the content type to find a #GAppInfo for
2438 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
2441 * Gets the default #GAppInfo for a given content type.
2443 * Returns: (transfer full): #GAppInfo for given @content_type or
2447 g_app_info_get_default_for_type (const char *content_type,
2448 gboolean must_support_uris)
2450 GList *desktop_entries, *l;
2451 char *user_default = NULL;
2454 g_return_val_if_fail (content_type != NULL, NULL);
2456 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2460 if (user_default != NULL)
2462 info = (GAppInfo *) g_desktop_app_info_new (user_default);
2466 if (must_support_uris && !g_app_info_supports_uris (info))
2468 g_object_unref (info);
2474 g_free (user_default);
2478 g_list_free_full (desktop_entries, g_free);
2482 /* pick the first from the other list that matches our URI
2485 for (l = desktop_entries; l != NULL; l = l->next)
2487 char *desktop_entry = l->data;
2489 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
2492 if (must_support_uris && !g_app_info_supports_uris (info))
2494 g_object_unref (info);
2502 g_list_free_full (desktop_entries, g_free);
2508 * g_app_info_get_default_for_uri_scheme:
2509 * @uri_scheme: a string containing a URI scheme.
2511 * Gets the default application for handling URIs with
2512 * the given URI scheme. A URI scheme is the initial part
2513 * of the URI, up to but not including the ':', e.g. "http",
2516 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
2519 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
2522 char *content_type, *scheme_down;
2524 scheme_down = g_ascii_strdown (uri_scheme, -1);
2525 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
2526 g_free (scheme_down);
2527 app_info = g_app_info_get_default_for_type (content_type, FALSE);
2528 g_free (content_type);
2534 get_apps_from_dir (GHashTable *apps,
2535 const char *dirname,
2539 const char *basename;
2540 char *filename, *subprefix, *desktop_id;
2542 GDesktopAppInfo *appinfo;
2544 dir = g_dir_open (dirname, 0, NULL);
2547 while ((basename = g_dir_read_name (dir)) != NULL)
2549 filename = g_build_filename (dirname, basename, NULL);
2550 if (g_str_has_suffix (basename, ".desktop"))
2552 desktop_id = g_strconcat (prefix, basename, NULL);
2554 /* Use _extended so we catch NULLs too (hidden) */
2555 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
2557 appinfo = g_desktop_app_info_new_from_filename (filename);
2560 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
2562 g_object_unref (appinfo);
2567 if (appinfo || hidden)
2569 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
2573 /* Reuse instead of strdup here */
2574 appinfo->desktop_id = desktop_id;
2579 g_free (desktop_id);
2583 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
2585 subprefix = g_strconcat (prefix, basename, "-", NULL);
2586 get_apps_from_dir (apps, filename, subprefix);
2598 * g_app_info_get_all:
2600 * Gets a list of all of the applications currently registered
2603 * For desktop files, this includes applications that have
2604 * <literal>NoDisplay=true</literal> set or are excluded from
2605 * display by means of <literal>OnlyShowIn</literal> or
2606 * <literal>NotShowIn</literal>. See g_app_info_should_show().
2607 * The returned list does not include applications which have
2608 * the <literal>Hidden</literal> key set.
2610 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
2613 g_app_info_get_all (void)
2615 const char * const *dirs;
2617 GHashTableIter iter;
2622 dirs = get_applications_search_path ();
2624 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
2628 for (i = 0; dirs[i] != NULL; i++)
2629 get_apps_from_dir (apps, dirs[i], "");
2633 g_hash_table_iter_init (&iter, apps);
2634 while (g_hash_table_iter_next (&iter, NULL, &value))
2637 infos = g_list_prepend (infos, value);
2640 g_hash_table_destroy (apps);
2642 return g_list_reverse (infos);
2645 /* Cacheing of mimeinfo.cache and defaults.list files */
2649 GHashTable *mime_info_cache_map;
2650 GHashTable *defaults_list_map;
2651 GHashTable *mimeapps_list_added_map;
2652 GHashTable *mimeapps_list_removed_map;
2653 GHashTable *mimeapps_list_defaults_map;
2654 time_t mime_info_cache_timestamp;
2655 time_t defaults_list_timestamp;
2656 time_t mimeapps_list_timestamp;
2660 GList *dirs; /* mimeinfo.cache and defaults.list */
2661 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
2662 time_t last_stat_time;
2663 guint should_ping_mime_monitor : 1;
2666 static MimeInfoCache *mime_info_cache = NULL;
2667 G_LOCK_DEFINE_STATIC (mime_info_cache);
2669 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2670 const char *mime_type,
2671 char **new_desktop_file_ids);
2673 static MimeInfoCache * mime_info_cache_new (void);
2676 destroy_info_cache_value (gpointer key,
2680 g_list_free_full (value, g_free);
2684 destroy_info_cache_map (GHashTable *info_cache_map)
2686 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
2687 g_hash_table_destroy (info_cache_map);
2691 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2692 const char *cache_file,
2698 filename = g_build_filename (dir->path, cache_file, NULL);
2700 if (g_stat (filename, &buf) < 0)
2707 if (buf.st_mtime != *timestamp)
2713 /* Call with lock held */
2715 remove_all (gpointer key,
2724 mime_info_cache_blow_global_cache (void)
2726 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
2731 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2735 gchar *filename, **mime_types;
2742 if (dir->mime_info_cache_map != NULL &&
2743 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2744 &dir->mime_info_cache_timestamp))
2747 if (dir->mime_info_cache_map != NULL)
2748 destroy_info_cache_map (dir->mime_info_cache_map);
2750 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2751 (GDestroyNotify) g_free,
2754 key_file = g_key_file_new ();
2756 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2758 if (g_stat (filename, &buf) < 0)
2761 if (dir->mime_info_cache_timestamp > 0)
2762 mime_info_cache->should_ping_mime_monitor = TRUE;
2764 dir->mime_info_cache_timestamp = buf.st_mtime;
2766 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2771 if (load_error != NULL)
2774 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2777 if (load_error != NULL)
2780 for (i = 0; mime_types[i] != NULL; i++)
2782 gchar **desktop_file_ids;
2783 char *unaliased_type;
2784 desktop_file_ids = g_key_file_get_string_list (key_file,
2790 if (desktop_file_ids == NULL)
2793 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2794 mime_info_cache_dir_add_desktop_entries (dir,
2797 g_free (unaliased_type);
2799 g_strfreev (desktop_file_ids);
2802 g_strfreev (mime_types);
2803 g_key_file_free (key_file);
2808 g_key_file_free (key_file);
2810 if (mime_types != NULL)
2811 g_strfreev (mime_types);
2814 g_error_free (load_error);
2818 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2822 gchar *filename, **mime_types;
2823 char *unaliased_type;
2824 char **desktop_file_ids;
2831 if (dir->defaults_list_map != NULL &&
2832 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2833 &dir->defaults_list_timestamp))
2836 if (dir->defaults_list_map != NULL)
2837 g_hash_table_destroy (dir->defaults_list_map);
2838 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2839 g_free, (GDestroyNotify)g_strfreev);
2842 key_file = g_key_file_new ();
2844 filename = g_build_filename (dir->path, "defaults.list", NULL);
2845 if (g_stat (filename, &buf) < 0)
2848 if (dir->defaults_list_timestamp > 0)
2849 mime_info_cache->should_ping_mime_monitor = TRUE;
2851 dir->defaults_list_timestamp = buf.st_mtime;
2853 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2857 if (load_error != NULL)
2860 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2862 if (mime_types != NULL)
2864 for (i = 0; mime_types[i] != NULL; i++)
2866 desktop_file_ids = g_key_file_get_string_list (key_file,
2867 DEFAULT_APPLICATIONS_GROUP,
2871 if (desktop_file_ids == NULL)
2874 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2875 g_hash_table_replace (dir->defaults_list_map,
2880 g_strfreev (mime_types);
2883 g_key_file_free (key_file);
2888 g_key_file_free (key_file);
2890 if (mime_types != NULL)
2891 g_strfreev (mime_types);
2894 g_error_free (load_error);
2898 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2902 gchar *filename, **mime_types;
2903 char *unaliased_type;
2904 char **desktop_file_ids;
2912 if (dir->mimeapps_list_added_map != NULL &&
2913 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2914 &dir->mimeapps_list_timestamp))
2917 if (dir->mimeapps_list_added_map != NULL)
2918 g_hash_table_destroy (dir->mimeapps_list_added_map);
2919 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2920 g_free, (GDestroyNotify)g_strfreev);
2922 if (dir->mimeapps_list_removed_map != NULL)
2923 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2924 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2925 g_free, (GDestroyNotify)g_strfreev);
2927 if (dir->mimeapps_list_defaults_map != NULL)
2928 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2929 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2932 key_file = g_key_file_new ();
2934 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2935 if (g_stat (filename, &buf) < 0)
2938 if (dir->mimeapps_list_timestamp > 0)
2939 mime_info_cache->should_ping_mime_monitor = TRUE;
2941 dir->mimeapps_list_timestamp = buf.st_mtime;
2943 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2947 if (load_error != NULL)
2950 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2952 if (mime_types != NULL)
2954 for (i = 0; mime_types[i] != NULL; i++)
2956 desktop_file_ids = g_key_file_get_string_list (key_file,
2957 ADDED_ASSOCIATIONS_GROUP,
2961 if (desktop_file_ids == NULL)
2964 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2965 g_hash_table_replace (dir->mimeapps_list_added_map,
2970 g_strfreev (mime_types);
2973 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2975 if (mime_types != NULL)
2977 for (i = 0; mime_types[i] != NULL; i++)
2979 desktop_file_ids = g_key_file_get_string_list (key_file,
2980 REMOVED_ASSOCIATIONS_GROUP,
2984 if (desktop_file_ids == NULL)
2987 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2988 g_hash_table_replace (dir->mimeapps_list_removed_map,
2993 g_strfreev (mime_types);
2996 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2998 if (mime_types != NULL)
3000 for (i = 0; mime_types[i] != NULL; i++)
3002 desktop_id = g_key_file_get_string (key_file,
3003 DEFAULT_APPLICATIONS_GROUP,
3006 if (desktop_id == NULL)
3009 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3010 g_hash_table_replace (dir->mimeapps_list_defaults_map,
3015 g_strfreev (mime_types);
3018 g_key_file_free (key_file);
3023 g_key_file_free (key_file);
3025 if (mime_types != NULL)
3026 g_strfreev (mime_types);
3029 g_error_free (load_error);
3032 static MimeInfoCacheDir *
3033 mime_info_cache_dir_new (const char *path)
3035 MimeInfoCacheDir *dir;
3037 dir = g_new0 (MimeInfoCacheDir, 1);
3038 dir->path = g_strdup (path);
3044 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
3049 if (dir->mime_info_cache_map != NULL)
3051 destroy_info_cache_map (dir->mime_info_cache_map);
3052 dir->mime_info_cache_map = NULL;
3056 if (dir->defaults_list_map != NULL)
3058 g_hash_table_destroy (dir->defaults_list_map);
3059 dir->defaults_list_map = NULL;
3062 if (dir->mimeapps_list_added_map != NULL)
3064 g_hash_table_destroy (dir->mimeapps_list_added_map);
3065 dir->mimeapps_list_added_map = NULL;
3068 if (dir->mimeapps_list_removed_map != NULL)
3070 g_hash_table_destroy (dir->mimeapps_list_removed_map);
3071 dir->mimeapps_list_removed_map = NULL;
3074 if (dir->mimeapps_list_defaults_map != NULL)
3076 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
3077 dir->mimeapps_list_defaults_map = NULL;
3084 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
3085 const char *mime_type,
3086 char **new_desktop_file_ids)
3088 GList *desktop_file_ids;
3091 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
3094 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
3096 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
3097 desktop_file_ids = g_list_append (desktop_file_ids,
3098 g_strdup (new_desktop_file_ids[i]));
3101 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
3105 mime_info_cache_init_dir_lists (void)
3107 const char * const *dirs;
3110 mime_info_cache = mime_info_cache_new ();
3112 dirs = get_applications_search_path ();
3114 for (i = 0; dirs[i] != NULL; i++)
3116 MimeInfoCacheDir *dir;
3118 dir = mime_info_cache_dir_new (dirs[i]);
3122 mime_info_cache_dir_init (dir);
3123 mime_info_cache_dir_init_defaults_list (dir);
3124 mime_info_cache_dir_init_mimeapps_list (dir);
3126 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
3132 mime_info_cache_update_dir_lists (void)
3136 tmp = mime_info_cache->dirs;
3140 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
3142 /* No need to do this if we had file monitors... */
3143 mime_info_cache_blow_global_cache ();
3144 mime_info_cache_dir_init (dir);
3145 mime_info_cache_dir_init_defaults_list (dir);
3146 mime_info_cache_dir_init_mimeapps_list (dir);
3153 mime_info_cache_init (void)
3155 G_LOCK (mime_info_cache);
3156 if (mime_info_cache == NULL)
3157 mime_info_cache_init_dir_lists ();
3163 if (now >= mime_info_cache->last_stat_time + 10)
3165 mime_info_cache_update_dir_lists ();
3166 mime_info_cache->last_stat_time = now;
3170 if (mime_info_cache->should_ping_mime_monitor)
3172 /* g_idle_add (emit_mime_changed, NULL); */
3173 mime_info_cache->should_ping_mime_monitor = FALSE;
3176 G_UNLOCK (mime_info_cache);
3179 static MimeInfoCache *
3180 mime_info_cache_new (void)
3182 MimeInfoCache *cache;
3184 cache = g_new0 (MimeInfoCache, 1);
3186 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
3187 (GDestroyNotify) g_free,
3188 (GDestroyNotify) g_free);
3193 mime_info_cache_free (MimeInfoCache *cache)
3198 g_list_free_full (cache->dirs, (GDestroyNotify) mime_info_cache_dir_free);
3199 g_hash_table_destroy (cache->global_defaults_cache);
3204 * mime_info_cache_reload:
3205 * @dir: directory path which needs reloading.
3207 * Reload the mime information for the @dir.
3210 mime_info_cache_reload (const char *dir)
3212 /* FIXME: just reload the dir that needs reloading,
3213 * don't blow the whole cache
3215 if (mime_info_cache != NULL)
3217 G_LOCK (mime_info_cache);
3218 mime_info_cache_free (mime_info_cache);
3219 mime_info_cache = NULL;
3220 G_UNLOCK (mime_info_cache);
3225 append_desktop_entry (GList *list,
3226 const char *desktop_entry,
3227 GList *removed_entries)
3229 /* Add if not already in list, and valid */
3230 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
3231 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
3232 list = g_list_prepend (list, g_strdup (desktop_entry));
3238 * get_all_desktop_entries_for_mime_type:
3239 * @mime_type: a mime type.
3240 * @except: NULL or a strv list
3242 * Returns all the desktop ids for @mime_type. The desktop files
3243 * are listed in an order so that default applications are listed before
3244 * non-default ones, and handlers for inherited mimetypes are listed
3245 * after the base ones.
3247 * Optionally doesn't list the desktop ids given in the @except
3249 * Return value: a #GList containing the desktop ids which claim
3250 * to handle @mime_type.
3253 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
3254 const char **except,
3255 gboolean include_fallback,
3256 char **explicit_default)
3258 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
3259 MimeInfoCacheDir *dir;
3260 char *mime_type, *default_entry = NULL;
3261 char *old_default_entry = NULL;
3264 char **default_entries;
3265 char **removed_associations;
3266 gboolean already_found_handler;
3271 mime_info_cache_init ();
3273 if (include_fallback)
3275 /* collect all ancestors */
3276 mime_types = _g_unix_content_type_get_parents (base_mime_type);
3277 array = g_ptr_array_new ();
3278 for (i = 0; mime_types[i]; i++)
3279 g_ptr_array_add (array, mime_types[i]);
3280 g_free (mime_types);
3281 for (i = 0; i < array->len; i++)
3283 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
3284 for (j = 0; anc[j]; j++)
3286 for (k = 0; k < array->len; k++)
3288 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
3291 if (k == array->len) /* not found */
3292 g_ptr_array_add (array, g_strdup (anc[j]));
3296 g_ptr_array_add (array, NULL);
3297 mime_types = (char **)g_ptr_array_free (array, FALSE);
3301 mime_types = g_malloc0 (2 * sizeof (gchar *));
3302 mime_types[0] = g_strdup (base_mime_type);
3303 mime_types[1] = NULL;
3306 G_LOCK (mime_info_cache);
3308 removed_entries = NULL;
3309 desktop_entries = NULL;
3311 for (i = 0; except != NULL && except[i] != NULL; i++)
3312 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
3314 for (i = 0; mime_types[i] != NULL; i++)
3316 mime_type = mime_types[i];
3318 /* This is true if we already found a handler for a more specific
3319 mimetype. If its set we ignore any defaults for the less specific
3321 already_found_handler = (desktop_entries != NULL);
3323 /* Go through all apps listed in user and system dirs */
3324 for (dir_list = mime_info_cache->dirs;
3326 dir_list = dir_list->next)
3328 dir = dir_list->data;
3330 /* Pick the explicit default application if we got no result earlier
3331 * (ie, for more specific mime types)
3333 if (!already_found_handler)
3335 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
3339 /* Save the default entry if it's the first one we encounter */
3340 if (default_entry == NULL)
3341 default_entry = g_strdup (entry);
3345 /* Then added associations from mimeapps.list */
3346 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
3347 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3348 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3350 /* Then removed associations from mimeapps.list */
3351 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
3352 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
3353 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
3355 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
3356 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
3357 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3359 if (default_entry == NULL && old_default_entry == NULL && !already_found_handler)
3360 old_default_entry = g_strdup (default_entries[j]);
3362 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3366 /* Go through all entries that support the mimetype */
3367 for (dir_list = mime_info_cache->dirs;
3369 dir_list = dir_list->next)
3371 dir = dir_list->data;
3373 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
3374 for (tmp = list; tmp != NULL; tmp = tmp->next)
3375 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
3379 G_UNLOCK (mime_info_cache);
3381 g_strfreev (mime_types);
3383 /* If we have no default from mimeapps.list, take it from
3384 * defaults.list intead.
3386 * If we do have a default from mimeapps.list, free any one that came
3387 * from defaults.list.
3389 if (default_entry == NULL)
3390 default_entry = old_default_entry;
3392 g_free (old_default_entry);
3394 if (explicit_default != NULL)
3395 *explicit_default = default_entry;
3397 g_free (default_entry);
3399 g_list_free_full (removed_entries, g_free);
3401 desktop_entries = g_list_reverse (desktop_entries);
3403 return desktop_entries;
3406 /* GDesktopAppInfoLookup interface: */
3408 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
3410 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
3411 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
3414 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
3419 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
3420 * @lookup: a #GDesktopAppInfoLookup
3421 * @uri_scheme: a string containing a URI scheme.
3423 * Gets the default application for launching applications
3424 * using this URI scheme for a particular GDesktopAppInfoLookup
3427 * The GDesktopAppInfoLookup interface and this function is used
3428 * to implement g_app_info_get_default_for_uri_scheme() backends
3429 * in a GIO module. There is no reason for applications to use it
3430 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
3432 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3434 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
3437 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
3438 const char *uri_scheme)
3440 GDesktopAppInfoLookupIface *iface;
3442 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
3444 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
3446 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
3449 G_GNUC_END_IGNORE_DEPRECATIONS
3452 * g_desktop_app_info_get_startup_wm_class:
3453 * @info: a #GDesktopAppInfo that supports startup notify
3455 * Retrieves the StartupWMClass field from @info. This represents the
3456 * WM_CLASS property of the main window of the application, if launched
3459 * Returns: (transfer none): the startup WM class, or %NULL if none is set
3460 * in the desktop file.
3465 g_desktop_app_info_get_startup_wm_class (GDesktopAppInfo *info)
3467 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
3469 return info->startup_wm_class;
3473 * g_desktop_app_info_get_string:
3474 * @info: a #GDesktopAppInfo
3475 * @key: the key to look up
3477 * Looks up a string value in the keyfile backing @info.
3479 * The @key is looked up in the "Desktop Entry" group.
3481 * Returns: a newly allocated string, or %NULL if the key
3487 g_desktop_app_info_get_string (GDesktopAppInfo *info,
3490 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
3492 return g_key_file_get_string (info->keyfile,
3493 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
3497 * g_desktop_app_info_get_boolean:
3498 * @info: a #GDesktopAppInfo
3499 * @key: the key to look up
3501 * Looks up a boolean value in the keyfile backing @info.
3503 * The @key is looked up in the "Desktop Entry" group.
3505 * Returns: the boolean value, or %FALSE if the key
3511 g_desktop_app_info_get_boolean (GDesktopAppInfo *info,
3514 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
3516 return g_key_file_get_boolean (info->keyfile,
3517 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
3521 * g_desktop_app_info_has_key:
3522 * @info: a #GDesktopAppInfo
3523 * @key: the key to look up
3525 * Returns whether @key exists in the "Desktop Entry" group
3526 * of the keyfile backing @info.
3528 * Returns: %TRUE if the @key exists
3533 g_desktop_app_info_has_key (GDesktopAppInfo *info,
3536 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
3538 return g_key_file_has_key (info->keyfile,
3539 G_KEY_FILE_DESKTOP_GROUP, key, NULL);