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 #undef G_DISABLE_DEPRECATED
37 #include "gcontenttypeprivate.h"
38 #include "gdesktopappinfo.h"
41 #include "gthemedicon.h"
42 #include "gfileicon.h"
43 #include <glib/gstdio.h>
45 #include "giomodule-priv.h"
50 * SECTION:gdesktopappinfo
51 * @title: GDesktopAppInfo
52 * @short_description: Application information from desktop files
53 * @include: gio/gdesktopappinfo.h
55 * #GDesktopAppInfo is an implementation of #GAppInfo based on
58 * Note that <filename><gio/gdesktopappinfo.h></filename> belongs to
59 * the UNIX-specific GIO interfaces, thus you have to use the
60 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
63 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
64 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
65 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
66 #define MIME_CACHE_GROUP "MIME Cache"
67 #define GENERIC_NAME_KEY "GenericName"
68 #define FULL_NAME_KEY "X-GNOME-FullName"
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;
113 guint startup_notify : 1;
115 /* FIXME: what about StartupWMClass ? */
119 UPDATE_MIME_NONE = 1 << 0,
120 UPDATE_MIME_SET_DEFAULT = 1 << 1,
121 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
122 UPDATE_MIME_REMOVE = 1 << 3,
123 UPDATE_MIME_SET_LAST_USED = 1 << 4,
126 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
127 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
128 g_desktop_app_info_iface_init))
130 G_LOCK_DEFINE_STATIC (g_desktop_env);
131 static gchar *g_desktop_env = NULL;
134 search_path_init (gpointer data)
137 const char * const *data_dirs;
138 const char *user_data_dir;
141 data_dirs = g_get_system_data_dirs ();
142 length = g_strv_length ((char **) data_dirs);
144 args = g_new (char *, length + 2);
147 user_data_dir = g_get_user_data_dir ();
148 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
149 for (i = 0; i < length; i++)
150 args[j++] = g_build_filename (data_dirs[i],
151 "applications", NULL);
157 static const char * const *
158 get_applications_search_path (void)
160 static GOnce once_init = G_ONCE_INIT;
161 return g_once (&once_init, search_path_init, NULL);
165 g_desktop_app_info_finalize (GObject *object)
167 GDesktopAppInfo *info;
169 info = G_DESKTOP_APP_INFO (object);
171 g_free (info->desktop_id);
172 g_free (info->filename);
174 g_free (info->generic_name);
175 g_free (info->fullname);
176 g_free (info->comment);
177 g_free (info->icon_name);
179 g_object_unref (info->icon);
180 g_strfreev (info->only_show_in);
181 g_strfreev (info->not_show_in);
182 g_free (info->try_exec);
184 g_free (info->binary);
186 g_free (info->categories);
188 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
192 g_desktop_app_info_set_property(GObject *object,
197 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
202 self->filename = g_value_dup_string (value);
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212 g_desktop_app_info_get_property(GObject *object,
217 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
222 g_value_set_string (value, self->filename);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
233 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
235 gobject_class->get_property = g_desktop_app_info_get_property;
236 gobject_class->set_property = g_desktop_app_info_set_property;
237 gobject_class->finalize = g_desktop_app_info_finalize;
240 * GDesktopAppInfo:filename
242 * The origin filename of this #GDesktopAppInfo
244 g_object_class_install_property (gobject_class,
246 g_param_spec_string ("filename", "Filename", "",
248 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
252 g_desktop_app_info_init (GDesktopAppInfo *local)
257 binary_from_exec (const char *exec)
259 const char *p, *start;
265 while (*p != ' ' && *p != 0)
268 return g_strndup (start, p - start);
273 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
280 start_group = g_key_file_get_start_group (key_file);
281 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
283 g_free (start_group);
286 g_free (start_group);
288 type = g_key_file_get_string (key_file,
289 G_KEY_FILE_DESKTOP_GROUP,
290 G_KEY_FILE_DESKTOP_KEY_TYPE,
292 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
299 try_exec = g_key_file_get_string (key_file,
300 G_KEY_FILE_DESKTOP_GROUP,
301 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
303 if (try_exec && try_exec[0] != '\0')
306 t = g_find_program_in_path (try_exec);
315 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
316 info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
317 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
318 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
319 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
320 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
321 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);
322 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);
323 info->try_exec = try_exec;
324 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
325 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
326 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
327 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
328 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
329 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
330 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
335 if (g_path_is_absolute (info->icon_name))
339 file = g_file_new_for_path (info->icon_name);
340 info->icon = g_file_icon_new (file);
341 g_object_unref (file);
347 /* Work around a common mistake in desktop files */
348 if ((p = strrchr (info->icon_name, '.')) != NULL &&
349 (strcmp (p, ".png") == 0 ||
350 strcmp (p, ".xpm") == 0 ||
351 strcmp (p, ".svg") == 0))
354 info->icon = g_themed_icon_new (info->icon_name);
359 info->binary = binary_from_exec (info->exec);
361 if (info->path && info->path[0] == '\0')
371 g_desktop_app_info_load_file (GDesktopAppInfo *self)
374 gboolean retval = FALSE;
376 g_return_val_if_fail (self->filename != NULL, FALSE);
378 key_file = g_key_file_new ();
380 if (g_key_file_load_from_file (key_file,
385 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
388 g_key_file_free (key_file);
393 * g_desktop_app_info_new_from_keyfile:
394 * @key_file: an opened #GKeyFile
396 * Creates a new #GDesktopAppInfo.
398 * Returns: a new #GDesktopAppInfo or %NULL on error.
403 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
405 GDesktopAppInfo *info;
407 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
408 info->filename = NULL;
409 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
411 g_object_unref (info);
418 * g_desktop_app_info_new_from_filename:
419 * @filename: the path of a desktop file, in the GLib filename encoding
421 * Creates a new #GDesktopAppInfo.
423 * Returns: a new #GDesktopAppInfo or %NULL on error.
426 g_desktop_app_info_new_from_filename (const char *filename)
428 GDesktopAppInfo *info = NULL;
430 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
431 if (!g_desktop_app_info_load_file (info))
433 g_object_unref (info);
440 * g_desktop_app_info_new:
441 * @desktop_id: the desktop file id
443 * Creates a new #GDesktopAppInfo based on a desktop file id.
445 * A desktop file id is the basename of the desktop file, including the
446 * .desktop extension. GIO is looking for a desktop file with this name
447 * in the <filename>applications</filename> subdirectories of the XDG data
448 * directories (i.e. the directories specified in the
449 * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment
450 * variables). GIO also supports the prefix-to-subdirectory mapping that is
451 * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink>
452 * (i.e. a desktop id of kde-foo.desktop will match
453 * <filename>/usr/share/applications/kde/foo.desktop</filename>).
455 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
458 g_desktop_app_info_new (const char *desktop_id)
460 GDesktopAppInfo *appinfo;
461 const char * const *dirs;
465 dirs = get_applications_search_path ();
467 basename = g_strdup (desktop_id);
469 for (i = 0; dirs[i] != NULL; i++)
474 filename = g_build_filename (dirs[i], desktop_id, NULL);
475 appinfo = g_desktop_app_info_new_from_filename (filename);
481 while ((p = strchr (p, '-')) != NULL)
485 filename = g_build_filename (dirs[i], basename, NULL);
486 appinfo = g_desktop_app_info_new_from_filename (filename);
501 appinfo->desktop_id = g_strdup (desktop_id);
503 if (g_desktop_app_info_get_is_hidden (appinfo))
505 g_object_unref (appinfo);
513 g_desktop_app_info_dup (GAppInfo *appinfo)
515 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
516 GDesktopAppInfo *new_info;
518 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
520 new_info->filename = g_strdup (info->filename);
521 new_info->desktop_id = g_strdup (info->desktop_id);
523 new_info->name = g_strdup (info->name);
524 new_info->generic_name = g_strdup (info->generic_name);
525 new_info->fullname = g_strdup (info->fullname);
526 new_info->comment = g_strdup (info->comment);
527 new_info->nodisplay = info->nodisplay;
528 new_info->icon_name = g_strdup (info->icon_name);
530 new_info->icon = g_object_ref (info->icon);
531 new_info->only_show_in = g_strdupv (info->only_show_in);
532 new_info->not_show_in = g_strdupv (info->not_show_in);
533 new_info->try_exec = g_strdup (info->try_exec);
534 new_info->exec = g_strdup (info->exec);
535 new_info->binary = g_strdup (info->binary);
536 new_info->path = g_strdup (info->path);
537 new_info->hidden = info->hidden;
538 new_info->terminal = info->terminal;
539 new_info->startup_notify = info->startup_notify;
541 return G_APP_INFO (new_info);
545 g_desktop_app_info_equal (GAppInfo *appinfo1,
548 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
549 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
551 if (info1->desktop_id == NULL ||
552 info2->desktop_id == NULL)
553 return info1 == info2;
555 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
559 g_desktop_app_info_get_id (GAppInfo *appinfo)
561 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
563 return info->desktop_id;
567 g_desktop_app_info_get_name (GAppInfo *appinfo)
569 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
571 if (info->name == NULL)
577 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
579 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
581 if (info->fullname == NULL)
582 return g_desktop_app_info_get_name (appinfo);
583 return info->fullname;
587 * g_desktop_app_info_get_is_hidden:
588 * @info: a #GDesktopAppInfo.
590 * A desktop file is hidden if the Hidden key in it is
593 * Returns: %TRUE if hidden, %FALSE otherwise.
596 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
602 * g_desktop_app_info_get_filename:
603 * @info: a #GDesktopAppInfo
605 * When @info was created from a known filename, return it. In some
606 * situations such as the #GDesktopAppInfo returned from
607 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
609 * Returns: The full path to the file for @info, or %NULL if not known.
613 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
615 return info->filename;
619 g_desktop_app_info_get_description (GAppInfo *appinfo)
621 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
623 return info->comment;
627 g_desktop_app_info_get_executable (GAppInfo *appinfo)
629 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
635 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
637 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
643 g_desktop_app_info_get_icon (GAppInfo *appinfo)
645 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
651 * g_desktop_app_info_get_categories:
652 * @info: a #GDesktopAppInfo
654 * Gets the categories from the desktop file.
656 * Returns: The unparsed Categories key from the desktop file;
657 * i.e. no attempt is made to split it by ';' or validate it.
660 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
662 return info->categories;
666 * g_desktop_app_info_get_generic_name:
667 * @info: a #GDesktopAppInfo
669 * Gets the generic name from the destkop file.
671 * Returns: The value of the GenericName key
674 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
676 return info->generic_name;
680 * g_desktop_app_info_get_nodisplay:
681 * @info: a #GDesktopAppInfo
683 * Gets the value of the NoDisplay key, which helps determine if the
684 * application info should be shown in menus. See
685 * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
687 * Returns: The value of the NoDisplay key
692 g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
694 return info->nodisplay;
698 * g_desktop_app_info_get_show_in:
699 * @info: a #GDesktopAppInfo
700 * @desktop_env: a string specifying a desktop name
702 * Checks if the application info should be shown in menus that list available
703 * applications for a specific name of the desktop, based on the
704 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys.
706 * If @desktop_env is %NULL, then the name of the desktop set with
707 * g_desktop_app_info_set_desktop_env() is used.
709 * Note that g_app_info_should_show() for @info will include this check (with
710 * %NULL for @desktop_env) as well as additional checks.
712 * Returns: %TRUE if the @info should be shown in @desktop_env according to the
713 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys, %FALSE
719 g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
720 const gchar *desktop_env)
725 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
728 G_LOCK (g_desktop_env);
729 desktop_env = g_desktop_env;
730 G_UNLOCK (g_desktop_env);
733 if (info->only_show_in)
735 if (desktop_env == NULL)
739 for (i = 0; info->only_show_in[i] != NULL; i++)
741 if (strcmp (info->only_show_in[i], desktop_env) == 0)
751 if (info->not_show_in && desktop_env)
753 for (i = 0; info->not_show_in[i] != NULL; i++)
755 if (strcmp (info->not_show_in[i], desktop_env) == 0)
764 expand_macro_single (char macro, char *uri)
770 file = g_file_new_for_uri (uri);
771 path = g_file_get_path (file);
772 g_object_unref (file);
778 result = g_shell_quote (uri);
783 result = g_shell_quote (path);
789 name = g_path_get_dirname (path);
790 result = g_shell_quote (name);
798 name = g_path_get_basename (path);
799 result = g_shell_quote (name);
811 expand_macro (char macro,
813 GDesktopAppInfo *info,
816 GList *uris = *uri_list;
818 gboolean force_file_uri;
819 char force_file_uri_macro;
822 g_return_if_fail (exec != NULL);
824 /* On %u and %U, pass POSIX file path pointing to the URI via
825 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
826 * running or the URI doesn't have a POSIX file path via FUSE
827 * we'll just pass the URI.
829 force_file_uri_macro = macro;
830 force_file_uri = FALSE;
836 force_file_uri_macro = 'f';
837 force_file_uri = TRUE;
840 force_file_uri_macro = 'F';
841 force_file_uri = TRUE;
857 if (!force_file_uri ||
858 /* Pass URI if it contains an anchor */
859 strchr (uri, '#') != NULL)
861 expanded = expand_macro_single (macro, uri);
865 expanded = expand_macro_single (force_file_uri_macro, uri);
866 if (expanded == NULL)
867 expanded = expand_macro_single (macro, uri);
872 g_string_append (exec, expanded);
888 if (!force_file_uri ||
889 /* Pass URI if it contains an anchor */
890 strchr (uri, '#') != NULL)
892 expanded = expand_macro_single (macro, uri);
896 expanded = expand_macro_single (force_file_uri_macro, uri);
897 if (expanded == NULL)
898 expanded = expand_macro_single (macro, uri);
903 g_string_append (exec, expanded);
909 if (uris != NULL && expanded)
910 g_string_append_c (exec, ' ');
918 g_string_append (exec, "--icon ");
919 expanded = g_shell_quote (info->icon_name);
920 g_string_append (exec, expanded);
928 expanded = g_shell_quote (info->name);
929 g_string_append (exec, expanded);
937 expanded = g_shell_quote (info->filename);
938 g_string_append (exec, expanded);
943 case 'm': /* deprecated */
947 g_string_append_c (exec, '%');
955 expand_application_parameters (GDesktopAppInfo *info,
961 GList *uri_list = *uris;
962 const char *p = info->exec;
963 GString *expanded_exec;
966 if (info->exec == NULL)
968 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
969 _("Desktop file didn't specify Exec field"));
973 expanded_exec = g_string_new (NULL);
977 if (p[0] == '%' && p[1] != '\0')
979 expand_macro (p[1], expanded_exec, info, uris);
983 g_string_append_c (expanded_exec, *p);
988 /* No file substitutions */
989 if (uri_list == *uris && uri_list != NULL)
991 /* If there is no macro default to %f. This is also what KDE does */
992 g_string_append_c (expanded_exec, ' ');
993 expand_macro ('f', expanded_exec, info, uris);
996 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
997 g_string_free (expanded_exec, TRUE);
1002 prepend_terminal_to_vector (int *argc,
1009 char **term_argv = NULL;
1014 g_return_val_if_fail (argc != NULL, FALSE);
1015 g_return_val_if_fail (argv != NULL, FALSE);
1023 /* compute size if not given */
1026 for (i = 0; the_argv[i] != NULL; i++)
1032 term_argv = g_new0 (char *, 3);
1034 check = g_find_program_in_path ("gnome-terminal");
1037 term_argv[0] = check;
1038 /* Note that gnome-terminal takes -x and
1039 * as -e in gnome-terminal is broken we use that. */
1040 term_argv[1] = g_strdup ("-x");
1045 check = g_find_program_in_path ("nxterm");
1047 check = g_find_program_in_path ("color-xterm");
1049 check = g_find_program_in_path ("rxvt");
1051 check = g_find_program_in_path ("xterm");
1053 check = g_find_program_in_path ("dtterm");
1056 check = g_strdup ("xterm");
1057 g_warning ("couldn't find a terminal, falling back to xterm");
1059 term_argv[0] = check;
1060 term_argv[1] = g_strdup ("-e");
1063 real_argc = term_argc + *argc;
1064 real_argv = g_new (char *, real_argc + 1);
1066 for (i = 0; i < term_argc; i++)
1067 real_argv[i] = term_argv[i];
1069 for (j = 0; j < *argc; j++, i++)
1070 real_argv[i] = (char *)the_argv[j];
1072 real_argv[i] = NULL;
1078 /* we use g_free here as we sucked all the inner strings
1079 * out from it into real_argv */
1084 #endif /* G_OS_WIN32 */
1088 create_files_for_uris (GList *uris)
1095 for (iter = uris; iter; iter = iter->next)
1097 GFile *file = g_file_new_for_uri ((char *)iter->data);
1098 res = g_list_prepend (res, file);
1101 return g_list_reverse (res);
1106 GSpawnChildSetupFunc user_setup;
1107 gpointer user_setup_data;
1114 child_setup (gpointer user_data)
1116 ChildSetupData *data = user_data;
1119 g_setenv ("DISPLAY", data->display, TRUE);
1122 g_setenv ("DESKTOP_STARTUP_ID", data->sn_id, TRUE);
1124 if (data->desktop_file)
1128 g_setenv ("GIO_LAUNCHED_DESKTOP_FILE", data->desktop_file, TRUE);
1130 g_snprintf (pid, 20, "%ld", (long)getpid ());
1131 g_setenv ("GIO_LAUNCHED_DESKTOP_FILE_PID", pid, TRUE);
1134 if (data->user_setup)
1135 data->user_setup (data->user_setup_data);
1139 notify_desktop_launch (GDBusConnection *session_bus,
1140 GDesktopAppInfo *info,
1142 const char *display,
1147 GVariantBuilder uri_variant;
1148 GVariantBuilder extras_variant;
1150 const char *desktop_file_id;
1151 const char *gio_desktop_file;
1153 if (session_bus == NULL)
1156 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
1157 for (iter = uris; iter; iter = iter->next)
1158 g_variant_builder_add (&uri_variant, "s", iter->data);
1160 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
1161 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
1162 g_variant_builder_add (&extras_variant, "{sv}",
1166 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
1167 if (gio_desktop_file != NULL)
1168 g_variant_builder_add (&extras_variant, "{sv}",
1169 "origin-desktop-file",
1170 g_variant_new_bytestring (gio_desktop_file));
1171 if (g_get_prgname () != NULL)
1172 g_variant_builder_add (&extras_variant, "{sv}",
1174 g_variant_new_bytestring (g_get_prgname ()));
1175 g_variant_builder_add (&extras_variant, "{sv}",
1178 (gint64)getpid ()));
1181 desktop_file_id = info->filename;
1182 else if (info->desktop_id)
1183 desktop_file_id = info->desktop_id;
1185 desktop_file_id = "";
1187 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
1188 "org.gtk.gio.DesktopAppInfo",
1190 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
1191 g_variant_new_bytestring (desktop_file_id),
1192 display ? display : "",
1196 g_dbus_connection_send_message (session_bus,
1200 g_object_unref (msg);
1203 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
1206 _g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
1208 GAppLaunchContext *launch_context,
1209 GSpawnFlags spawn_flags,
1210 GSpawnChildSetupFunc user_setup,
1211 gpointer user_setup_data,
1212 GDesktopAppLaunchCallback pid_callback,
1213 gpointer pid_callback_data,
1216 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1217 GDBusConnection *session_bus;
1218 gboolean completed = FALSE;
1222 ChildSetupData data;
1224 g_return_val_if_fail (appinfo != NULL, FALSE);
1228 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
1233 GList *launched_uris;
1237 if (!expand_application_parameters (info, &uris,
1238 &argc, &argv, error))
1241 /* Get the subset of URIs we're launching with this process */
1242 launched_uris = NULL;
1243 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
1244 launched_uris = g_list_prepend (launched_uris, iter->data);
1245 launched_uris = g_list_reverse (launched_uris);
1247 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
1249 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1250 _("Unable to find terminal required for application"));
1254 data.user_setup = user_setup;
1255 data.user_setup_data = user_setup_data;
1256 data.display = NULL;
1258 data.desktop_file = info->filename;
1262 GList *launched_files = create_files_for_uris (launched_uris);
1264 data.display = g_app_launch_context_get_display (launch_context,
1268 if (info->startup_notify)
1269 data.sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
1272 g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
1273 g_list_free (launched_files);
1276 if (!g_spawn_async (info->path,
1286 g_app_launch_context_launch_failed (launch_context, data.sn_id);
1288 g_free (data.sn_id);
1289 g_free (data.display);
1290 g_list_free (launched_uris);
1295 if (pid_callback != NULL)
1296 pid_callback (info, pid, pid_callback_data);
1298 notify_desktop_launch (session_bus,
1305 g_free (data.sn_id);
1306 g_free (data.display);
1307 g_list_free (launched_uris);
1312 while (uris != NULL);
1314 /* TODO - need to handle the process exiting immediately
1315 * after launching an app. See http://bugzilla.gnome.org/606960
1317 if (session_bus != NULL)
1319 /* This asynchronous flush holds a reference until it completes,
1320 * which ensures that the following unref won't immediately kill
1321 * the connection if we were the initial owner.
1323 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
1324 g_object_unref (session_bus);
1336 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
1338 GAppLaunchContext *launch_context,
1341 return _g_desktop_app_info_launch_uris_internal (appinfo, uris,
1343 _SPAWN_FLAGS_DEFAULT,
1344 NULL, NULL, NULL, NULL,
1349 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1351 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1353 return info->exec &&
1354 ((strstr (info->exec, "%u") != NULL) ||
1355 (strstr (info->exec, "%U") != NULL));
1359 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1361 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1363 return info->exec &&
1364 ((strstr (info->exec, "%f") != NULL) ||
1365 (strstr (info->exec, "%F") != NULL));
1369 g_desktop_app_info_launch (GAppInfo *appinfo,
1371 GAppLaunchContext *launch_context,
1381 uri = g_file_get_uri (files->data);
1382 uris = g_list_prepend (uris, uri);
1383 files = files->next;
1386 uris = g_list_reverse (uris);
1388 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1390 g_list_foreach (uris, (GFunc)g_free, NULL);
1397 * g_desktop_app_info_launch_uris_as_manager:
1398 * @appinfo: a #GDesktopAppInfo
1399 * @uris: (element-type utf8): List of URIs
1400 * @launch_context: a #GAppLaunchContext
1401 * @spawn_flags: #GSpawnFlags, used for each process
1402 * @user_setup: (scope call): a #GSpawnChildSetupFunc, used once for
1404 * @user_setup_data: (closure user_setup): User data for @user_setup
1405 * @pid_callback: (scope call): Callback for child processes
1406 * @pid_callback_data: (closure pid_callback): User data for @callback
1407 * @error: return location for a #GError, or %NULL
1409 * This function performs the equivalent of g_app_info_launch_uris(),
1410 * but is intended primarily for operating system components that
1411 * launch applications. Ordinary applications should use
1412 * g_app_info_launch_uris().
1414 * In contrast to g_app_info_launch_uris(), all processes created will
1415 * always be run directly as children as if by the UNIX fork()/exec()
1418 * This guarantee allows additional control over the exact environment
1419 * of the child processes, which is provided via a setup function
1420 * @setup, as well as the process identifier of each child process via
1421 * @pid_callback. See g_spawn_async() for more information about the
1422 * semantics of the @setup function.
1424 * Returns: %TRUE on successful launch, %FALSE otherwise.
1427 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
1429 GAppLaunchContext *launch_context,
1430 GSpawnFlags spawn_flags,
1431 GSpawnChildSetupFunc user_setup,
1432 gpointer user_setup_data,
1433 GDesktopAppLaunchCallback pid_callback,
1434 gpointer pid_callback_data,
1437 return _g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
1449 * g_desktop_app_info_set_desktop_env:
1450 * @desktop_env: a string specifying what desktop this is
1452 * Sets the name of the desktop that the application is running in.
1453 * This is used by g_app_info_should_show() and
1454 * g_desktop_app_info_get_show_in() to evaluate the
1455 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1456 * desktop entry fields.
1458 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1459 * Menu specification</ulink> recognizes the following:
1461 * <member>GNOME</member>
1462 * <member>KDE</member>
1463 * <member>ROX</member>
1464 * <member>XFCE</member>
1465 * <member>LXDE</member>
1466 * <member>Unity</member>
1467 * <member>Old</member>
1470 * Should be called only once; subsequent calls are ignored.
1473 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1475 G_LOCK (g_desktop_env);
1477 g_desktop_env = g_strdup (desktop_env);
1478 G_UNLOCK (g_desktop_env);
1482 g_desktop_app_info_should_show (GAppInfo *appinfo)
1484 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1486 if (info->nodisplay)
1489 return g_desktop_app_info_get_show_in (info, NULL);
1498 ensure_dir (DirType type,
1501 char *path, *display_name;
1504 if (type == APP_DIR)
1505 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1507 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1510 if (g_mkdir_with_parents (path, 0700) == 0)
1514 display_name = g_filename_display_name (path);
1515 if (type == APP_DIR)
1516 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1517 _("Can't create user application configuration folder %s: %s"),
1518 display_name, g_strerror (errsv));
1520 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1521 _("Can't create user MIME configuration folder %s: %s"),
1522 display_name, g_strerror (errsv));
1524 g_free (display_name);
1531 update_mimeapps_list (const char *desktop_id,
1532 const char *content_type,
1533 UpdateMimeFlags flags,
1536 char *dirname, *filename, *string;
1538 gboolean load_succeeded, res, explicit_default;
1539 char **old_list, **list;
1541 gsize length, data_size;
1544 char **content_types;
1546 /* Don't add both at start and end */
1547 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
1548 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
1550 dirname = ensure_dir (APP_DIR, error);
1554 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1557 key_file = g_key_file_new ();
1558 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1559 if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1561 g_key_file_free (key_file);
1562 key_file = g_key_file_new ();
1567 content_types = g_new (char *, 2);
1568 content_types[0] = g_strdup (content_type);
1569 content_types[1] = NULL;
1573 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
1576 explicit_default = FALSE;
1578 for (k = 0; content_types && content_types[k]; k++)
1580 /* set as default, if requested so */
1581 string = g_key_file_get_string (key_file,
1582 DEFAULT_APPLICATIONS_GROUP,
1586 if (g_strcmp0 (string, desktop_id) != 0 &&
1587 (flags & UPDATE_MIME_SET_DEFAULT))
1590 string = g_strdup (desktop_id);
1592 /* add in the non-default list too, if it's not already there */
1593 flags |= UPDATE_MIME_SET_NON_DEFAULT;
1596 if (string == NULL || desktop_id == NULL)
1597 g_key_file_remove_key (key_file,
1598 DEFAULT_APPLICATIONS_GROUP,
1603 g_key_file_set_string (key_file,
1604 DEFAULT_APPLICATIONS_GROUP,
1608 explicit_default = TRUE;
1616 /* reuse the list from above */
1620 g_strfreev (content_types);
1621 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1624 for (k = 0; content_types && content_types[k]; k++)
1626 /* Add to the right place in the list */
1629 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1630 content_types[k], &length, NULL);
1632 list = g_new (char *, 1 + length + 1);
1636 /* if we're adding a last-used hint, just put the application in front of the list */
1637 if (flags & UPDATE_MIME_SET_LAST_USED)
1639 /* avoid adding this again as non-default later */
1640 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1641 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1643 list[i++] = g_strdup (desktop_id);
1648 for (j = 0; old_list[j] != NULL; j++)
1650 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1652 /* rewrite other entries if they're different from the new one */
1653 list[i++] = g_strdup (old_list[j]);
1655 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1657 /* we encountered an old entry which is equal to the one we're adding as non-default,
1658 * don't change its position in the list.
1660 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1661 list[i++] = g_strdup (old_list[j]);
1666 /* add it at the end of the list */
1667 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1668 list[i++] = g_strdup (desktop_id);
1672 g_strfreev (old_list);
1674 if (list[0] == NULL || desktop_id == NULL)
1675 g_key_file_remove_key (key_file,
1676 ADDED_ASSOCIATIONS_GROUP,
1681 g_key_file_set_string_list (key_file,
1682 ADDED_ASSOCIATIONS_GROUP,
1684 (const char * const *)list, i);
1686 /* if we had no explicit default set, we should add the system default to the
1687 * list, to avoid overriding it with applications from this list.
1689 if (!explicit_default)
1692 /* The system default can be in the list, don't exclude it */
1693 system_list = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, &user_default);
1696 if (user_default != NULL)
1698 /* There is an existing default set in a lower priority defaults file, re-use it */
1699 string = user_default;
1701 else if (system_list != NULL)
1703 string = system_list->data;
1707 g_key_file_set_string (key_file,
1708 DEFAULT_APPLICATIONS_GROUP,
1713 g_free (user_default);
1714 g_list_free_full (system_list, g_free);
1723 /* reuse the list from above */
1727 g_strfreev (content_types);
1728 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1731 for (k = 0; content_types && content_types[k]; k++)
1733 /* Remove from removed associations group (unless remove) */
1736 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1737 content_types[k], &length, NULL);
1739 list = g_new (char *, 1 + length + 1);
1742 if (flags & UPDATE_MIME_REMOVE)
1743 list[i++] = g_strdup (desktop_id);
1746 for (j = 0; old_list[j] != NULL; j++)
1748 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1749 list[i++] = g_strdup (old_list[j]);
1754 g_strfreev (old_list);
1756 if (list[0] == NULL || desktop_id == NULL)
1757 g_key_file_remove_key (key_file,
1758 REMOVED_ASSOCIATIONS_GROUP,
1762 g_key_file_set_string_list (key_file,
1763 REMOVED_ASSOCIATIONS_GROUP,
1765 (const char * const *)list, i);
1770 g_strfreev (content_types);
1772 data = g_key_file_to_data (key_file, &data_size, error);
1773 g_key_file_free (key_file);
1775 res = g_file_set_contents (filename, data, data_size, error);
1777 mime_info_cache_reload (NULL);
1786 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
1787 const char *content_type,
1790 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1792 if (!g_desktop_app_info_ensure_saved (info, error))
1795 if (!info->desktop_id)
1797 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1798 _("Application information lacks an identifier"));
1802 /* both add support for the content type and set as last used */
1803 return update_mimeapps_list (info->desktop_id, content_type,
1804 UPDATE_MIME_SET_NON_DEFAULT |
1805 UPDATE_MIME_SET_LAST_USED,
1810 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1811 const char *content_type,
1814 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1816 if (!g_desktop_app_info_ensure_saved (info, error))
1819 if (!info->desktop_id)
1821 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1822 _("Application information lacks an identifier"));
1826 return update_mimeapps_list (info->desktop_id, content_type,
1827 UPDATE_MIME_SET_DEFAULT,
1832 update_program_done (GPid pid,
1836 /* Did the application exit correctly */
1837 if (WIFEXITED (status) &&
1838 WEXITSTATUS (status) == 0)
1840 /* Here we could clean out any caches in use */
1845 run_update_command (char *command,
1854 GError *error = NULL;
1857 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1859 if (g_spawn_async ("/", argv,
1861 G_SPAWN_SEARCH_PATH |
1862 G_SPAWN_STDOUT_TO_DEV_NULL |
1863 G_SPAWN_STDERR_TO_DEV_NULL |
1864 G_SPAWN_DO_NOT_REAP_CHILD,
1865 NULL, NULL, /* No setup function */
1868 g_child_watch_add (pid, update_program_done, NULL);
1871 /* If we get an error at this point, it's quite likely the user doesn't
1872 * have an installed copy of either 'update-mime-database' or
1873 * 'update-desktop-database'. I don't think we want to popup an error
1874 * dialog at this point, so we just do a g_warning to give the user a
1875 * chance of debugging it.
1877 g_warning ("%s", error->message);
1884 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1885 const char *extension,
1888 char *filename, *basename, *mimetype;
1892 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1895 dirname = ensure_dir (MIMETYPE_DIR, error);
1899 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1900 filename = g_build_filename (dirname, basename, NULL);
1904 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1906 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1911 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1912 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1913 " <mime-type type=\"%s\">\n"
1914 " <comment>%s document</comment>\n"
1915 " <glob pattern=\"*.%s\"/>\n"
1917 "</mime-info>\n", mimetype, extension, extension);
1919 g_file_set_contents (filename, contents, -1, NULL);
1922 run_update_command ("update-mime-database", "mime");
1926 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1936 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1937 const char *content_type,
1940 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1942 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1945 return update_mimeapps_list (info->desktop_id, content_type,
1946 UPDATE_MIME_SET_NON_DEFAULT,
1951 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1957 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1958 const char *content_type,
1961 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1963 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1966 return update_mimeapps_list (info->desktop_id, content_type,
1972 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1978 char *data, *desktop_id;
1983 if (info->filename != NULL)
1986 /* This is only used for object created with
1987 * g_app_info_create_from_commandline. All other
1988 * object should have a filename
1991 dirname = ensure_dir (APP_DIR, error);
1995 key_file = g_key_file_new ();
1997 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1998 "Encoding", "UTF-8");
1999 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2000 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
2001 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2002 G_KEY_FILE_DESKTOP_KEY_TYPE,
2003 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
2005 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2006 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
2007 if (info->nodisplay)
2008 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2009 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2011 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2012 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
2014 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2015 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
2017 if (info->generic_name != NULL)
2018 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2019 GENERIC_NAME_KEY, info->generic_name);
2021 if (info->fullname != NULL)
2022 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2023 FULL_NAME_KEY, info->fullname);
2025 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2026 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
2028 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2029 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2031 data = g_key_file_to_data (key_file, &data_size, NULL);
2032 g_key_file_free (key_file);
2034 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
2035 filename = g_build_filename (dirname, desktop_id, NULL);
2036 g_free (desktop_id);
2039 fd = g_mkstemp (filename);
2044 display_name = g_filename_display_name (filename);
2045 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2046 _("Can't create user desktop file %s"), display_name);
2047 g_free (display_name);
2053 desktop_id = g_path_get_basename (filename);
2057 res = g_file_set_contents (filename, data, data_size, error);
2060 g_free (desktop_id);
2065 info->filename = filename;
2066 info->desktop_id = desktop_id;
2068 run_update_command ("update-desktop-database", "applications");
2074 g_desktop_app_info_can_delete (GAppInfo *appinfo)
2076 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2080 if (strstr (info->filename, "/userapp-"))
2081 return g_access (info->filename, W_OK) == 0;
2088 g_desktop_app_info_delete (GAppInfo *appinfo)
2090 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2094 if (g_remove (info->filename) == 0)
2096 update_mimeapps_list (info->desktop_id, NULL,
2100 g_free (info->filename);
2101 info->filename = NULL;
2102 g_free (info->desktop_id);
2103 info->desktop_id = NULL;
2113 * g_app_info_create_from_commandline:
2114 * @commandline: the commandline to use
2115 * @application_name: (allow-none): the application name, or %NULL to use @commandline
2116 * @flags: flags that can specify details of the created #GAppInfo
2117 * @error: a #GError location to store the error occurring, %NULL to ignore.
2119 * Creates a new #GAppInfo from the given information.
2121 * Returns: (transfer full): new #GAppInfo for given command.
2124 g_app_info_create_from_commandline (const char *commandline,
2125 const char *application_name,
2126 GAppInfoCreateFlags flags,
2131 GDesktopAppInfo *info;
2133 g_return_val_if_fail (commandline, NULL);
2135 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
2137 info->filename = NULL;
2138 info->desktop_id = NULL;
2140 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
2141 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
2142 info->hidden = FALSE;
2143 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
2144 info->exec = g_strconcat (commandline, " %u", NULL);
2146 info->exec = g_strconcat (commandline, " %f", NULL);
2147 info->nodisplay = TRUE;
2148 info->binary = binary_from_exec (info->exec);
2150 if (application_name)
2151 info->name = g_strdup (application_name);
2154 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
2155 split = g_strsplit (commandline, " ", 2);
2156 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
2158 info->name = basename;
2159 if (info->name == NULL)
2160 info->name = g_strdup ("custom");
2162 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
2164 return G_APP_INFO (info);
2168 g_desktop_app_info_iface_init (GAppInfoIface *iface)
2170 iface->dup = g_desktop_app_info_dup;
2171 iface->equal = g_desktop_app_info_equal;
2172 iface->get_id = g_desktop_app_info_get_id;
2173 iface->get_name = g_desktop_app_info_get_name;
2174 iface->get_description = g_desktop_app_info_get_description;
2175 iface->get_executable = g_desktop_app_info_get_executable;
2176 iface->get_icon = g_desktop_app_info_get_icon;
2177 iface->launch = g_desktop_app_info_launch;
2178 iface->supports_uris = g_desktop_app_info_supports_uris;
2179 iface->supports_files = g_desktop_app_info_supports_files;
2180 iface->launch_uris = g_desktop_app_info_launch_uris;
2181 iface->should_show = g_desktop_app_info_should_show;
2182 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
2183 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
2184 iface->add_supports_type = g_desktop_app_info_add_supports_type;
2185 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
2186 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
2187 iface->can_delete = g_desktop_app_info_can_delete;
2188 iface->do_delete = g_desktop_app_info_delete;
2189 iface->get_commandline = g_desktop_app_info_get_commandline;
2190 iface->get_display_name = g_desktop_app_info_get_display_name;
2191 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
2195 app_info_in_list (GAppInfo *info,
2198 while (list != NULL)
2200 if (g_app_info_equal (info, list->data))
2208 * g_app_info_get_recommended_for_type:
2209 * @content_type: the content type to find a #GAppInfo for
2211 * Gets a list of recommended #GAppInfos for a given content type, i.e.
2212 * those applications which claim to support the given content type exactly,
2213 * and not by MIME type subclassing.
2214 * Note that the first application of the list is the last used one, i.e.
2215 * the last one for which #g_app_info_set_as_last_used_for_type has been
2218 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2219 * for given @content_type or %NULL on error.
2224 g_app_info_get_recommended_for_type (const gchar *content_type)
2226 GList *desktop_entries, *l;
2228 GDesktopAppInfo *info;
2230 g_return_val_if_fail (content_type != NULL, NULL);
2232 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
2235 for (l = desktop_entries; l != NULL; l = l->next)
2237 char *desktop_entry = l->data;
2239 info = g_desktop_app_info_new (desktop_entry);
2242 if (app_info_in_list (G_APP_INFO (info), infos))
2243 g_object_unref (info);
2245 infos = g_list_prepend (infos, info);
2247 g_free (desktop_entry);
2250 g_list_free (desktop_entries);
2252 return g_list_reverse (infos);
2256 * g_app_info_get_fallback_for_type:
2257 * @content_type: the content type to find a #GAppInfo for
2259 * Gets a list of fallback #GAppInfos for a given content type, i.e.
2260 * those applications which claim to support the given content type
2261 * by MIME type subclassing and not directly.
2263 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2264 * for given @content_type or %NULL on error.
2269 g_app_info_get_fallback_for_type (const gchar *content_type)
2271 GList *desktop_entries, *l;
2272 GList *infos, *recommended_infos;
2273 GDesktopAppInfo *info;
2275 g_return_val_if_fail (content_type != NULL, NULL);
2277 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
2278 recommended_infos = g_app_info_get_recommended_for_type (content_type);
2281 for (l = desktop_entries; l != NULL; l = l->next)
2283 char *desktop_entry = l->data;
2285 info = g_desktop_app_info_new (desktop_entry);
2288 if (app_info_in_list (G_APP_INFO (info), infos) ||
2289 app_info_in_list (G_APP_INFO (info), recommended_infos))
2290 g_object_unref (info);
2292 infos = g_list_prepend (infos, info);
2294 g_free (desktop_entry);
2297 g_list_free (desktop_entries);
2298 g_list_free_full (recommended_infos, g_object_unref);
2300 return g_list_reverse (infos);
2304 * g_app_info_get_all_for_type:
2305 * @content_type: the content type to find a #GAppInfo for
2307 * Gets a list of all #GAppInfos for a given content type.
2309 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2310 * for given @content_type or %NULL on error.
2313 g_app_info_get_all_for_type (const char *content_type)
2315 GList *desktop_entries, *l;
2317 char *user_default = NULL;
2318 GDesktopAppInfo *info;
2320 g_return_val_if_fail (content_type != NULL, NULL);
2322 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2325 /* put the user default in front of the list, for compatibility */
2326 if (user_default != NULL)
2328 info = g_desktop_app_info_new (user_default);
2331 infos = g_list_prepend (infos, info);
2334 g_free (user_default);
2336 for (l = desktop_entries; l != NULL; l = l->next)
2338 char *desktop_entry = l->data;
2340 info = g_desktop_app_info_new (desktop_entry);
2343 if (app_info_in_list (G_APP_INFO (info), infos))
2344 g_object_unref (info);
2346 infos = g_list_prepend (infos, info);
2348 g_free (desktop_entry);
2351 g_list_free (desktop_entries);
2353 return g_list_reverse (infos);
2357 * g_app_info_reset_type_associations:
2358 * @content_type: a content type
2360 * Removes all changes to the type associations done by
2361 * g_app_info_set_as_default_for_type(),
2362 * g_app_info_set_as_default_for_extension(),
2363 * g_app_info_add_supports_type() or g_app_info_remove_supports_type().
2368 g_app_info_reset_type_associations (const char *content_type)
2370 update_mimeapps_list (NULL, content_type,
2376 * g_app_info_get_default_for_type:
2377 * @content_type: the content type to find a #GAppInfo for
2378 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
2381 * Gets the #GAppInfo that corresponds to a given content type.
2383 * Returns: (transfer full): #GAppInfo for given @content_type or
2387 g_app_info_get_default_for_type (const char *content_type,
2388 gboolean must_support_uris)
2390 GList *desktop_entries, *l;
2391 char *user_default = NULL;
2394 g_return_val_if_fail (content_type != NULL, NULL);
2396 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2400 if (user_default != NULL)
2402 info = (GAppInfo *) g_desktop_app_info_new (user_default);
2406 if (must_support_uris && !g_app_info_supports_uris (info))
2408 g_object_unref (info);
2414 g_free (user_default);
2418 g_list_free_full (desktop_entries, g_free);
2422 /* pick the first from the other list that matches our URI
2425 for (l = desktop_entries; l != NULL; l = l->next)
2427 char *desktop_entry = l->data;
2429 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
2432 if (must_support_uris && !g_app_info_supports_uris (info))
2434 g_object_unref (info);
2442 g_list_free_full (desktop_entries, g_free);
2448 * g_app_info_get_default_for_uri_scheme:
2449 * @uri_scheme: a string containing a URI scheme.
2451 * Gets the default application for launching applications
2452 * using this URI scheme. A URI scheme is the initial part
2453 * of the URI, up to but not including the ':', e.g. "http",
2456 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
2459 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
2462 char *content_type, *scheme_down;
2464 scheme_down = g_ascii_strdown (uri_scheme, -1);
2465 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
2466 g_free (scheme_down);
2467 app_info = g_app_info_get_default_for_type (content_type, FALSE);
2468 g_free (content_type);
2474 get_apps_from_dir (GHashTable *apps,
2475 const char *dirname,
2479 const char *basename;
2480 char *filename, *subprefix, *desktop_id;
2482 GDesktopAppInfo *appinfo;
2484 dir = g_dir_open (dirname, 0, NULL);
2487 while ((basename = g_dir_read_name (dir)) != NULL)
2489 filename = g_build_filename (dirname, basename, NULL);
2490 if (g_str_has_suffix (basename, ".desktop"))
2492 desktop_id = g_strconcat (prefix, basename, NULL);
2494 /* Use _extended so we catch NULLs too (hidden) */
2495 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
2497 appinfo = g_desktop_app_info_new_from_filename (filename);
2500 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
2502 g_object_unref (appinfo);
2507 if (appinfo || hidden)
2509 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
2513 /* Reuse instead of strdup here */
2514 appinfo->desktop_id = desktop_id;
2519 g_free (desktop_id);
2523 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
2525 subprefix = g_strconcat (prefix, basename, "-", NULL);
2526 get_apps_from_dir (apps, filename, subprefix);
2538 * g_app_info_get_all:
2540 * Gets a list of all of the applications currently registered
2543 * For desktop files, this includes applications that have
2544 * <literal>NoDisplay=true</literal> set or are excluded from
2545 * display by means of <literal>OnlyShowIn</literal> or
2546 * <literal>NotShowIn</literal>. See g_app_info_should_show().
2547 * The returned list does not include applications which have
2548 * the <literal>Hidden</literal> key set.
2550 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
2553 g_app_info_get_all (void)
2555 const char * const *dirs;
2557 GHashTableIter iter;
2562 dirs = get_applications_search_path ();
2564 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
2568 for (i = 0; dirs[i] != NULL; i++)
2569 get_apps_from_dir (apps, dirs[i], "");
2573 g_hash_table_iter_init (&iter, apps);
2574 while (g_hash_table_iter_next (&iter, NULL, &value))
2577 infos = g_list_prepend (infos, value);
2580 g_hash_table_destroy (apps);
2582 return g_list_reverse (infos);
2585 /* Cacheing of mimeinfo.cache and defaults.list files */
2589 GHashTable *mime_info_cache_map;
2590 GHashTable *defaults_list_map;
2591 GHashTable *mimeapps_list_added_map;
2592 GHashTable *mimeapps_list_removed_map;
2593 GHashTable *mimeapps_list_defaults_map;
2594 time_t mime_info_cache_timestamp;
2595 time_t defaults_list_timestamp;
2596 time_t mimeapps_list_timestamp;
2600 GList *dirs; /* mimeinfo.cache and defaults.list */
2601 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
2602 time_t last_stat_time;
2603 guint should_ping_mime_monitor : 1;
2606 static MimeInfoCache *mime_info_cache = NULL;
2607 G_LOCK_DEFINE_STATIC (mime_info_cache);
2609 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2610 const char *mime_type,
2611 char **new_desktop_file_ids);
2613 static MimeInfoCache * mime_info_cache_new (void);
2616 destroy_info_cache_value (gpointer key,
2620 g_list_foreach (value, (GFunc)g_free, NULL);
2621 g_list_free (value);
2625 destroy_info_cache_map (GHashTable *info_cache_map)
2627 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
2628 g_hash_table_destroy (info_cache_map);
2632 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2633 const char *cache_file,
2639 filename = g_build_filename (dir->path, cache_file, NULL);
2641 if (g_stat (filename, &buf) < 0)
2648 if (buf.st_mtime != *timestamp)
2654 /* Call with lock held */
2656 remove_all (gpointer key,
2665 mime_info_cache_blow_global_cache (void)
2667 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
2672 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2676 gchar *filename, **mime_types;
2683 if (dir->mime_info_cache_map != NULL &&
2684 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2685 &dir->mime_info_cache_timestamp))
2688 if (dir->mime_info_cache_map != NULL)
2689 destroy_info_cache_map (dir->mime_info_cache_map);
2691 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2692 (GDestroyNotify) g_free,
2695 key_file = g_key_file_new ();
2697 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2699 if (g_stat (filename, &buf) < 0)
2702 if (dir->mime_info_cache_timestamp > 0)
2703 mime_info_cache->should_ping_mime_monitor = TRUE;
2705 dir->mime_info_cache_timestamp = buf.st_mtime;
2707 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2712 if (load_error != NULL)
2715 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2718 if (load_error != NULL)
2721 for (i = 0; mime_types[i] != NULL; i++)
2723 gchar **desktop_file_ids;
2724 char *unaliased_type;
2725 desktop_file_ids = g_key_file_get_string_list (key_file,
2731 if (desktop_file_ids == NULL)
2734 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2735 mime_info_cache_dir_add_desktop_entries (dir,
2738 g_free (unaliased_type);
2740 g_strfreev (desktop_file_ids);
2743 g_strfreev (mime_types);
2744 g_key_file_free (key_file);
2749 g_key_file_free (key_file);
2751 if (mime_types != NULL)
2752 g_strfreev (mime_types);
2755 g_error_free (load_error);
2759 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2763 gchar *filename, **mime_types;
2764 char *unaliased_type;
2765 char **desktop_file_ids;
2772 if (dir->defaults_list_map != NULL &&
2773 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2774 &dir->defaults_list_timestamp))
2777 if (dir->defaults_list_map != NULL)
2778 g_hash_table_destroy (dir->defaults_list_map);
2779 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2780 g_free, (GDestroyNotify)g_strfreev);
2783 key_file = g_key_file_new ();
2785 filename = g_build_filename (dir->path, "defaults.list", NULL);
2786 if (g_stat (filename, &buf) < 0)
2789 if (dir->defaults_list_timestamp > 0)
2790 mime_info_cache->should_ping_mime_monitor = TRUE;
2792 dir->defaults_list_timestamp = buf.st_mtime;
2794 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2798 if (load_error != NULL)
2801 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2803 if (mime_types != NULL)
2805 for (i = 0; mime_types[i] != NULL; i++)
2807 desktop_file_ids = g_key_file_get_string_list (key_file,
2808 DEFAULT_APPLICATIONS_GROUP,
2812 if (desktop_file_ids == NULL)
2815 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2816 g_hash_table_replace (dir->defaults_list_map,
2821 g_strfreev (mime_types);
2824 g_key_file_free (key_file);
2829 g_key_file_free (key_file);
2831 if (mime_types != NULL)
2832 g_strfreev (mime_types);
2835 g_error_free (load_error);
2839 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2843 gchar *filename, **mime_types;
2844 char *unaliased_type;
2845 char **desktop_file_ids;
2853 if (dir->mimeapps_list_added_map != NULL &&
2854 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2855 &dir->mimeapps_list_timestamp))
2858 if (dir->mimeapps_list_added_map != NULL)
2859 g_hash_table_destroy (dir->mimeapps_list_added_map);
2860 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2861 g_free, (GDestroyNotify)g_strfreev);
2863 if (dir->mimeapps_list_removed_map != NULL)
2864 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2865 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2866 g_free, (GDestroyNotify)g_strfreev);
2868 if (dir->mimeapps_list_defaults_map != NULL)
2869 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2870 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2873 key_file = g_key_file_new ();
2875 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2876 if (g_stat (filename, &buf) < 0)
2879 if (dir->mimeapps_list_timestamp > 0)
2880 mime_info_cache->should_ping_mime_monitor = TRUE;
2882 dir->mimeapps_list_timestamp = buf.st_mtime;
2884 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2888 if (load_error != NULL)
2891 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2893 if (mime_types != NULL)
2895 for (i = 0; mime_types[i] != NULL; i++)
2897 desktop_file_ids = g_key_file_get_string_list (key_file,
2898 ADDED_ASSOCIATIONS_GROUP,
2902 if (desktop_file_ids == NULL)
2905 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2906 g_hash_table_replace (dir->mimeapps_list_added_map,
2911 g_strfreev (mime_types);
2914 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2916 if (mime_types != NULL)
2918 for (i = 0; mime_types[i] != NULL; i++)
2920 desktop_file_ids = g_key_file_get_string_list (key_file,
2921 REMOVED_ASSOCIATIONS_GROUP,
2925 if (desktop_file_ids == NULL)
2928 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2929 g_hash_table_replace (dir->mimeapps_list_removed_map,
2934 g_strfreev (mime_types);
2937 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2939 if (mime_types != NULL)
2941 for (i = 0; mime_types[i] != NULL; i++)
2943 desktop_id = g_key_file_get_string (key_file,
2944 DEFAULT_APPLICATIONS_GROUP,
2947 if (desktop_id == NULL)
2950 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2951 g_hash_table_replace (dir->mimeapps_list_defaults_map,
2956 g_strfreev (mime_types);
2959 g_key_file_free (key_file);
2964 g_key_file_free (key_file);
2966 if (mime_types != NULL)
2967 g_strfreev (mime_types);
2970 g_error_free (load_error);
2973 static MimeInfoCacheDir *
2974 mime_info_cache_dir_new (const char *path)
2976 MimeInfoCacheDir *dir;
2978 dir = g_new0 (MimeInfoCacheDir, 1);
2979 dir->path = g_strdup (path);
2985 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2990 if (dir->mime_info_cache_map != NULL)
2992 destroy_info_cache_map (dir->mime_info_cache_map);
2993 dir->mime_info_cache_map = NULL;
2997 if (dir->defaults_list_map != NULL)
2999 g_hash_table_destroy (dir->defaults_list_map);
3000 dir->defaults_list_map = NULL;
3003 if (dir->mimeapps_list_added_map != NULL)
3005 g_hash_table_destroy (dir->mimeapps_list_added_map);
3006 dir->mimeapps_list_added_map = NULL;
3009 if (dir->mimeapps_list_removed_map != NULL)
3011 g_hash_table_destroy (dir->mimeapps_list_removed_map);
3012 dir->mimeapps_list_removed_map = NULL;
3015 if (dir->mimeapps_list_defaults_map != NULL)
3017 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
3018 dir->mimeapps_list_defaults_map = NULL;
3025 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
3026 const char *mime_type,
3027 char **new_desktop_file_ids)
3029 GList *desktop_file_ids;
3032 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
3035 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
3037 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
3038 desktop_file_ids = g_list_append (desktop_file_ids,
3039 g_strdup (new_desktop_file_ids[i]));
3042 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
3046 mime_info_cache_init_dir_lists (void)
3048 const char * const *dirs;
3051 mime_info_cache = mime_info_cache_new ();
3053 dirs = get_applications_search_path ();
3055 for (i = 0; dirs[i] != NULL; i++)
3057 MimeInfoCacheDir *dir;
3059 dir = mime_info_cache_dir_new (dirs[i]);
3063 mime_info_cache_dir_init (dir);
3064 mime_info_cache_dir_init_defaults_list (dir);
3065 mime_info_cache_dir_init_mimeapps_list (dir);
3067 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
3073 mime_info_cache_update_dir_lists (void)
3077 tmp = mime_info_cache->dirs;
3081 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
3083 /* No need to do this if we had file monitors... */
3084 mime_info_cache_blow_global_cache ();
3085 mime_info_cache_dir_init (dir);
3086 mime_info_cache_dir_init_defaults_list (dir);
3087 mime_info_cache_dir_init_mimeapps_list (dir);
3094 mime_info_cache_init (void)
3096 G_LOCK (mime_info_cache);
3097 if (mime_info_cache == NULL)
3098 mime_info_cache_init_dir_lists ();
3104 if (now >= mime_info_cache->last_stat_time + 10)
3106 mime_info_cache_update_dir_lists ();
3107 mime_info_cache->last_stat_time = now;
3111 if (mime_info_cache->should_ping_mime_monitor)
3113 /* g_idle_add (emit_mime_changed, NULL); */
3114 mime_info_cache->should_ping_mime_monitor = FALSE;
3117 G_UNLOCK (mime_info_cache);
3120 static MimeInfoCache *
3121 mime_info_cache_new (void)
3123 MimeInfoCache *cache;
3125 cache = g_new0 (MimeInfoCache, 1);
3127 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
3128 (GDestroyNotify) g_free,
3129 (GDestroyNotify) g_free);
3134 mime_info_cache_free (MimeInfoCache *cache)
3139 g_list_foreach (cache->dirs,
3140 (GFunc) mime_info_cache_dir_free,
3142 g_list_free (cache->dirs);
3143 g_hash_table_destroy (cache->global_defaults_cache);
3148 * mime_info_cache_reload:
3149 * @dir: directory path which needs reloading.
3151 * Reload the mime information for the @dir.
3154 mime_info_cache_reload (const char *dir)
3156 /* FIXME: just reload the dir that needs reloading,
3157 * don't blow the whole cache
3159 if (mime_info_cache != NULL)
3161 G_LOCK (mime_info_cache);
3162 mime_info_cache_free (mime_info_cache);
3163 mime_info_cache = NULL;
3164 G_UNLOCK (mime_info_cache);
3169 append_desktop_entry (GList *list,
3170 const char *desktop_entry,
3171 GList *removed_entries)
3173 /* Add if not already in list, and valid */
3174 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
3175 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
3176 list = g_list_prepend (list, g_strdup (desktop_entry));
3182 * get_all_desktop_entries_for_mime_type:
3183 * @mime_type: a mime type.
3184 * @except: NULL or a strv list
3186 * Returns all the desktop ids for @mime_type. The desktop files
3187 * are listed in an order so that default applications are listed before
3188 * non-default ones, and handlers for inherited mimetypes are listed
3189 * after the base ones.
3191 * Optionally doesn't list the desktop ids given in the @except
3193 * Return value: a #GList containing the desktop ids which claim
3194 * to handle @mime_type.
3197 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
3198 const char **except,
3199 gboolean include_fallback,
3200 char **explicit_default)
3202 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
3203 MimeInfoCacheDir *dir;
3204 char *mime_type, *default_entry = NULL;
3207 char **default_entries;
3208 char **removed_associations;
3213 mime_info_cache_init ();
3215 if (include_fallback)
3217 /* collect all ancestors */
3218 mime_types = _g_unix_content_type_get_parents (base_mime_type);
3219 array = g_ptr_array_new ();
3220 for (i = 0; mime_types[i]; i++)
3221 g_ptr_array_add (array, mime_types[i]);
3222 g_free (mime_types);
3223 for (i = 0; i < array->len; i++)
3225 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
3226 for (j = 0; anc[j]; j++)
3228 for (k = 0; k < array->len; k++)
3230 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
3233 if (k == array->len) /* not found */
3234 g_ptr_array_add (array, g_strdup (anc[j]));
3238 g_ptr_array_add (array, NULL);
3239 mime_types = (char **)g_ptr_array_free (array, FALSE);
3243 mime_types = g_malloc0 (2 * sizeof (gchar *));
3244 mime_types[0] = g_strdup (base_mime_type);
3245 mime_types[1] = NULL;
3248 G_LOCK (mime_info_cache);
3250 removed_entries = NULL;
3251 desktop_entries = NULL;
3253 for (i = 0; except != NULL && except[i] != NULL; i++)
3254 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
3256 for (i = 0; mime_types[i] != NULL; i++)
3258 mime_type = mime_types[i];
3260 /* Go through all apps listed in user and system dirs */
3261 for (dir_list = mime_info_cache->dirs;
3263 dir_list = dir_list->next)
3265 dir = dir_list->data;
3267 /* Pick the explicit default application if we got no result earlier
3268 * (ie, for more specific mime types)
3270 if (desktop_entries == NULL)
3272 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
3276 /* Save the default entry if it's the first one we encounter */
3277 if (default_entry == NULL)
3278 default_entry = g_strdup (entry);
3282 /* Then added associations from mimeapps.list */
3283 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
3284 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3285 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3287 /* Then removed associations from mimeapps.list */
3288 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
3289 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
3290 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
3292 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
3293 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
3294 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3295 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3298 /* Go through all entries that support the mimetype */
3299 for (dir_list = mime_info_cache->dirs;
3301 dir_list = dir_list->next)
3303 dir = dir_list->data;
3305 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
3306 for (tmp = list; tmp != NULL; tmp = tmp->next)
3307 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
3311 G_UNLOCK (mime_info_cache);
3313 g_strfreev (mime_types);
3315 if (explicit_default != NULL)
3316 *explicit_default = default_entry;
3318 g_free (default_entry);
3320 g_list_foreach (removed_entries, (GFunc)g_free, NULL);
3321 g_list_free (removed_entries);
3323 desktop_entries = g_list_reverse (desktop_entries);
3325 return desktop_entries;
3328 /* GDesktopAppInfoLookup interface: */
3330 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
3331 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
3334 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
3339 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
3340 * @lookup: a #GDesktopAppInfoLookup
3341 * @uri_scheme: a string containing a URI scheme.
3343 * Gets the default application for launching applications
3344 * using this URI scheme for a particular GDesktopAppInfoLookup
3347 * The GDesktopAppInfoLookup interface and this function is used
3348 * to implement g_app_info_get_default_for_uri_scheme() backends
3349 * in a GIO module. There is no reason for applications to use it
3350 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
3352 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3354 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
3357 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
3358 const char *uri_scheme)
3360 GDesktopAppInfoLookupIface *iface;
3362 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
3364 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
3366 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);