1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright © 2007 Ryan Lortie
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Author: Alexander Larsson <alexl@redhat.com>
31 #ifdef HAVE_CRT_EXTERNS_H
32 #include <crt_externs.h>
35 #include "gcontenttypeprivate.h"
36 #include "gdesktopappinfo.h"
39 #include "gthemedicon.h"
40 #include "gfileicon.h"
41 #include <glib/gstdio.h>
43 #include "giomodule-priv.h"
48 * SECTION:gdesktopappinfo
49 * @title: GDesktopAppInfo
50 * @short_description: Application information from desktop files
51 * @include: gio/gdesktopappinfo.h
53 * #GDesktopAppInfo is an implementation of #GAppInfo based on
56 * Note that <filename><gio/gdesktopappinfo.h></filename> belongs to
57 * the UNIX-specific GIO interfaces, thus you have to use the
58 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
61 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
62 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
63 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
64 #define MIME_CACHE_GROUP "MIME Cache"
65 #define GENERIC_NAME_KEY "GenericName"
66 #define FULL_NAME_KEY "X-GNOME-FullName"
67 #define KEYWORDS_KEY "Keywords"
68 #define STARTUP_WM_CLASS_KEY "StartupWMClass"
75 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
76 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type,
78 gboolean include_fallback,
79 char **explicit_default);
80 static void mime_info_cache_reload (const char *dir);
81 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
87 * Information about an installed application from a desktop file.
89 struct _GDesktopAppInfo
91 GObject parent_instance;
110 char *startup_wm_class;
115 guint startup_notify : 1;
120 UPDATE_MIME_NONE = 1 << 0,
121 UPDATE_MIME_SET_DEFAULT = 1 << 1,
122 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
123 UPDATE_MIME_REMOVE = 1 << 3,
124 UPDATE_MIME_SET_LAST_USED = 1 << 4,
127 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
128 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
129 g_desktop_app_info_iface_init))
131 G_LOCK_DEFINE_STATIC (g_desktop_env);
132 static gchar *g_desktop_env = NULL;
135 search_path_init (gpointer data)
138 const char * const *data_dirs;
139 const char *user_data_dir;
142 data_dirs = g_get_system_data_dirs ();
143 length = g_strv_length ((char **) data_dirs);
145 args = g_new (char *, length + 2);
148 user_data_dir = g_get_user_data_dir ();
149 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
150 for (i = 0; i < length; i++)
151 args[j++] = g_build_filename (data_dirs[i],
152 "applications", NULL);
158 static const char * const *
159 get_applications_search_path (void)
161 static GOnce once_init = G_ONCE_INIT;
162 return g_once (&once_init, search_path_init, NULL);
166 g_desktop_app_info_finalize (GObject *object)
168 GDesktopAppInfo *info;
170 info = G_DESKTOP_APP_INFO (object);
172 g_free (info->desktop_id);
173 g_free (info->filename);
175 g_free (info->generic_name);
176 g_free (info->fullname);
177 g_free (info->comment);
178 g_free (info->icon_name);
180 g_object_unref (info->icon);
181 g_strfreev (info->keywords);
182 g_strfreev (info->only_show_in);
183 g_strfreev (info->not_show_in);
184 g_free (info->try_exec);
186 g_free (info->binary);
188 g_free (info->categories);
189 g_free (info->startup_wm_class);
191 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
195 g_desktop_app_info_set_property(GObject *object,
200 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
205 self->filename = g_value_dup_string (value);
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 g_desktop_app_info_get_property(GObject *object,
220 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
225 g_value_set_string (value, self->filename);
228 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
236 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
238 gobject_class->get_property = g_desktop_app_info_get_property;
239 gobject_class->set_property = g_desktop_app_info_set_property;
240 gobject_class->finalize = g_desktop_app_info_finalize;
243 * GDesktopAppInfo:filename:
245 * The origin filename of this #GDesktopAppInfo
247 g_object_class_install_property (gobject_class,
249 g_param_spec_string ("filename", "Filename", "",
251 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
255 g_desktop_app_info_init (GDesktopAppInfo *local)
260 binary_from_exec (const char *exec)
262 const char *p, *start;
268 while (*p != ' ' && *p != 0)
271 return g_strndup (start, p - start);
276 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
283 start_group = g_key_file_get_start_group (key_file);
284 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
286 g_free (start_group);
289 g_free (start_group);
291 type = g_key_file_get_string (key_file,
292 G_KEY_FILE_DESKTOP_GROUP,
293 G_KEY_FILE_DESKTOP_KEY_TYPE,
295 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
302 try_exec = g_key_file_get_string (key_file,
303 G_KEY_FILE_DESKTOP_GROUP,
304 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
306 if (try_exec && try_exec[0] != '\0')
309 t = g_find_program_in_path (try_exec);
318 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
319 info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
320 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
321 info->keywords = g_key_file_get_locale_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, KEYWORDS_KEY, NULL, NULL, NULL);
322 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
323 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
324 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
325 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);
326 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);
327 info->try_exec = try_exec;
328 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
329 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
330 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
331 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
332 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
333 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
334 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
335 info->startup_wm_class = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, STARTUP_WM_CLASS_KEY, NULL);
340 if (g_path_is_absolute (info->icon_name))
344 file = g_file_new_for_path (info->icon_name);
345 info->icon = g_file_icon_new (file);
346 g_object_unref (file);
352 /* Work around a common mistake in desktop files */
353 if ((p = strrchr (info->icon_name, '.')) != NULL &&
354 (strcmp (p, ".png") == 0 ||
355 strcmp (p, ".xpm") == 0 ||
356 strcmp (p, ".svg") == 0))
359 info->icon = g_themed_icon_new (info->icon_name);
364 info->binary = binary_from_exec (info->exec);
366 if (info->path && info->path[0] == '\0')
376 g_desktop_app_info_load_file (GDesktopAppInfo *self)
379 gboolean retval = FALSE;
381 g_return_val_if_fail (self->filename != NULL, FALSE);
383 key_file = g_key_file_new ();
385 if (g_key_file_load_from_file (key_file,
390 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
393 g_key_file_free (key_file);
398 * g_desktop_app_info_new_from_keyfile:
399 * @key_file: an opened #GKeyFile
401 * Creates a new #GDesktopAppInfo.
403 * Returns: a new #GDesktopAppInfo or %NULL on error.
408 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
410 GDesktopAppInfo *info;
412 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
413 info->filename = NULL;
414 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
416 g_object_unref (info);
423 * g_desktop_app_info_new_from_filename:
424 * @filename: the path of a desktop file, in the GLib filename encoding
426 * Creates a new #GDesktopAppInfo.
428 * Returns: a new #GDesktopAppInfo or %NULL on error.
431 g_desktop_app_info_new_from_filename (const char *filename)
433 GDesktopAppInfo *info = NULL;
435 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
436 if (!g_desktop_app_info_load_file (info))
438 g_object_unref (info);
445 * g_desktop_app_info_new:
446 * @desktop_id: the desktop file id
448 * Creates a new #GDesktopAppInfo based on a desktop file id.
450 * A desktop file id is the basename of the desktop file, including the
451 * .desktop extension. GIO is looking for a desktop file with this name
452 * in the <filename>applications</filename> subdirectories of the XDG data
453 * directories (i.e. the directories specified in the
454 * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment
455 * variables). GIO also supports the prefix-to-subdirectory mapping that is
456 * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink>
457 * (i.e. a desktop id of kde-foo.desktop will match
458 * <filename>/usr/share/applications/kde/foo.desktop</filename>).
460 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
463 g_desktop_app_info_new (const char *desktop_id)
465 GDesktopAppInfo *appinfo;
466 const char * const *dirs;
470 dirs = get_applications_search_path ();
472 basename = g_strdup (desktop_id);
474 for (i = 0; dirs[i] != NULL; i++)
479 filename = g_build_filename (dirs[i], desktop_id, NULL);
480 appinfo = g_desktop_app_info_new_from_filename (filename);
486 while ((p = strchr (p, '-')) != NULL)
490 filename = g_build_filename (dirs[i], basename, NULL);
491 appinfo = g_desktop_app_info_new_from_filename (filename);
506 appinfo->desktop_id = g_strdup (desktop_id);
508 if (g_desktop_app_info_get_is_hidden (appinfo))
510 g_object_unref (appinfo);
518 g_desktop_app_info_dup (GAppInfo *appinfo)
520 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
521 GDesktopAppInfo *new_info;
523 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
525 new_info->filename = g_strdup (info->filename);
526 new_info->desktop_id = g_strdup (info->desktop_id);
528 new_info->name = g_strdup (info->name);
529 new_info->generic_name = g_strdup (info->generic_name);
530 new_info->fullname = g_strdup (info->fullname);
531 new_info->keywords = g_strdupv (info->keywords);
532 new_info->comment = g_strdup (info->comment);
533 new_info->nodisplay = info->nodisplay;
534 new_info->icon_name = g_strdup (info->icon_name);
536 new_info->icon = g_object_ref (info->icon);
537 new_info->only_show_in = g_strdupv (info->only_show_in);
538 new_info->not_show_in = g_strdupv (info->not_show_in);
539 new_info->try_exec = g_strdup (info->try_exec);
540 new_info->exec = g_strdup (info->exec);
541 new_info->binary = g_strdup (info->binary);
542 new_info->path = g_strdup (info->path);
543 new_info->hidden = info->hidden;
544 new_info->terminal = info->terminal;
545 new_info->startup_notify = info->startup_notify;
547 return G_APP_INFO (new_info);
551 g_desktop_app_info_equal (GAppInfo *appinfo1,
554 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
555 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
557 if (info1->desktop_id == NULL ||
558 info2->desktop_id == NULL)
559 return info1 == info2;
561 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
565 g_desktop_app_info_get_id (GAppInfo *appinfo)
567 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
569 return info->desktop_id;
573 g_desktop_app_info_get_name (GAppInfo *appinfo)
575 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
577 if (info->name == NULL)
583 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
585 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
587 if (info->fullname == NULL)
588 return g_desktop_app_info_get_name (appinfo);
589 return info->fullname;
593 * g_desktop_app_info_get_is_hidden:
594 * @info: a #GDesktopAppInfo.
596 * A desktop file is hidden if the Hidden key in it is
599 * Returns: %TRUE if hidden, %FALSE otherwise.
602 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
608 * g_desktop_app_info_get_filename:
609 * @info: a #GDesktopAppInfo
611 * When @info was created from a known filename, return it. In some
612 * situations such as the #GDesktopAppInfo returned from
613 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
615 * Returns: The full path to the file for @info, or %NULL if not known.
619 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
621 return info->filename;
625 g_desktop_app_info_get_description (GAppInfo *appinfo)
627 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
629 return info->comment;
633 g_desktop_app_info_get_executable (GAppInfo *appinfo)
635 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
641 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
643 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
649 g_desktop_app_info_get_icon (GAppInfo *appinfo)
651 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
657 * g_desktop_app_info_get_categories:
658 * @info: a #GDesktopAppInfo
660 * Gets the categories from the desktop file.
662 * Returns: The unparsed Categories key from the desktop file;
663 * i.e. no attempt is made to split it by ';' or validate it.
666 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
668 return info->categories;
672 * g_desktop_app_info_get_keywords:
673 * @info: a #GDesktopAppInfo
675 * Gets the keywords from the desktop file.
677 * Returns: (transfer none): The value of the Keywords key
682 g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
684 return (const char * const *)info->keywords;
688 * g_desktop_app_info_get_generic_name:
689 * @info: a #GDesktopAppInfo
691 * Gets the generic name from the destkop file.
693 * Returns: The value of the GenericName key
696 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
698 return info->generic_name;
702 * g_desktop_app_info_get_nodisplay:
703 * @info: a #GDesktopAppInfo
705 * Gets the value of the NoDisplay key, which helps determine if the
706 * application info should be shown in menus. See
707 * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
709 * Returns: The value of the NoDisplay key
714 g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
716 return info->nodisplay;
720 * g_desktop_app_info_get_show_in:
721 * @info: a #GDesktopAppInfo
722 * @desktop_env: a string specifying a desktop name
724 * Checks if the application info should be shown in menus that list available
725 * applications for a specific name of the desktop, based on the
726 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys.
728 * If @desktop_env is %NULL, then the name of the desktop set with
729 * g_desktop_app_info_set_desktop_env() is used.
731 * Note that g_app_info_should_show() for @info will include this check (with
732 * %NULL for @desktop_env) as well as additional checks.
734 * Returns: %TRUE if the @info should be shown in @desktop_env according to the
735 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys, %FALSE
741 g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
742 const gchar *desktop_env)
747 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
750 G_LOCK (g_desktop_env);
751 desktop_env = g_desktop_env;
752 G_UNLOCK (g_desktop_env);
755 if (info->only_show_in)
757 if (desktop_env == NULL)
761 for (i = 0; info->only_show_in[i] != NULL; i++)
763 if (strcmp (info->only_show_in[i], desktop_env) == 0)
773 if (info->not_show_in && desktop_env)
775 for (i = 0; info->not_show_in[i] != NULL; i++)
777 if (strcmp (info->not_show_in[i], desktop_env) == 0)
786 expand_macro_single (char macro, char *uri)
792 file = g_file_new_for_uri (uri);
793 path = g_file_get_path (file);
794 g_object_unref (file);
800 result = g_shell_quote (uri);
805 result = g_shell_quote (path);
811 name = g_path_get_dirname (path);
812 result = g_shell_quote (name);
820 name = g_path_get_basename (path);
821 result = g_shell_quote (name);
833 expand_macro (char macro,
835 GDesktopAppInfo *info,
838 GList *uris = *uri_list;
840 gboolean force_file_uri;
841 char force_file_uri_macro;
844 g_return_if_fail (exec != NULL);
846 /* On %u and %U, pass POSIX file path pointing to the URI via
847 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
848 * running or the URI doesn't have a POSIX file path via FUSE
849 * we'll just pass the URI.
851 force_file_uri_macro = macro;
852 force_file_uri = FALSE;
858 force_file_uri_macro = 'f';
859 force_file_uri = TRUE;
862 force_file_uri_macro = 'F';
863 force_file_uri = TRUE;
879 if (!force_file_uri ||
880 /* Pass URI if it contains an anchor */
881 strchr (uri, '#') != NULL)
883 expanded = expand_macro_single (macro, uri);
887 expanded = expand_macro_single (force_file_uri_macro, uri);
888 if (expanded == NULL)
889 expanded = expand_macro_single (macro, uri);
894 g_string_append (exec, expanded);
910 if (!force_file_uri ||
911 /* Pass URI if it contains an anchor */
912 strchr (uri, '#') != NULL)
914 expanded = expand_macro_single (macro, uri);
918 expanded = expand_macro_single (force_file_uri_macro, uri);
919 if (expanded == NULL)
920 expanded = expand_macro_single (macro, uri);
925 g_string_append (exec, expanded);
931 if (uris != NULL && expanded)
932 g_string_append_c (exec, ' ');
940 g_string_append (exec, "--icon ");
941 expanded = g_shell_quote (info->icon_name);
942 g_string_append (exec, expanded);
950 expanded = g_shell_quote (info->name);
951 g_string_append (exec, expanded);
959 expanded = g_shell_quote (info->filename);
960 g_string_append (exec, expanded);
965 case 'm': /* deprecated */
969 g_string_append_c (exec, '%');
977 expand_application_parameters (GDesktopAppInfo *info,
983 GList *uri_list = *uris;
984 const char *p = info->exec;
985 GString *expanded_exec;
988 if (info->exec == NULL)
990 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
991 _("Desktop file didn't specify Exec field"));
995 expanded_exec = g_string_new (NULL);
999 if (p[0] == '%' && p[1] != '\0')
1001 expand_macro (p[1], expanded_exec, info, uris);
1005 g_string_append_c (expanded_exec, *p);
1010 /* No file substitutions */
1011 if (uri_list == *uris && uri_list != NULL)
1013 /* If there is no macro default to %f. This is also what KDE does */
1014 g_string_append_c (expanded_exec, ' ');
1015 expand_macro ('f', expanded_exec, info, uris);
1018 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
1019 g_string_free (expanded_exec, TRUE);
1024 prepend_terminal_to_vector (int *argc,
1031 char **term_argv = NULL;
1036 g_return_val_if_fail (argc != NULL, FALSE);
1037 g_return_val_if_fail (argv != NULL, FALSE);
1045 /* compute size if not given */
1048 for (i = 0; the_argv[i] != NULL; i++)
1054 term_argv = g_new0 (char *, 3);
1056 check = g_find_program_in_path ("gnome-terminal");
1059 term_argv[0] = check;
1060 /* Note that gnome-terminal takes -x and
1061 * as -e in gnome-terminal is broken we use that. */
1062 term_argv[1] = g_strdup ("-x");
1067 check = g_find_program_in_path ("nxterm");
1069 check = g_find_program_in_path ("color-xterm");
1071 check = g_find_program_in_path ("rxvt");
1073 check = g_find_program_in_path ("xterm");
1075 check = g_find_program_in_path ("dtterm");
1078 check = g_strdup ("xterm");
1079 g_warning ("couldn't find a terminal, falling back to xterm");
1081 term_argv[0] = check;
1082 term_argv[1] = g_strdup ("-e");
1085 real_argc = term_argc + *argc;
1086 real_argv = g_new (char *, real_argc + 1);
1088 for (i = 0; i < term_argc; i++)
1089 real_argv[i] = term_argv[i];
1091 for (j = 0; j < *argc; j++, i++)
1092 real_argv[i] = (char *)the_argv[j];
1094 real_argv[i] = NULL;
1100 /* we use g_free here as we sucked all the inner strings
1101 * out from it into real_argv */
1106 #endif /* G_OS_WIN32 */
1110 create_files_for_uris (GList *uris)
1117 for (iter = uris; iter; iter = iter->next)
1119 GFile *file = g_file_new_for_uri ((char *)iter->data);
1120 res = g_list_prepend (res, file);
1123 return g_list_reverse (res);
1128 GSpawnChildSetupFunc user_setup;
1129 gpointer user_setup_data;
1135 child_setup (gpointer user_data)
1137 ChildSetupData *data = user_data;
1139 if (data->pid_envvar)
1141 pid_t pid = getpid ();
1145 /* Write the pid into the space already reserved for it in the
1146 * environment array. We can't use sprintf because it might
1147 * malloc, so we do it by hand. It's simplest to write the pid
1148 * out backwards first, then copy it over.
1150 for (i = 0; pid; i++, pid /= 10)
1151 buf[i] = (pid % 10) + '0';
1152 for (i--; i >= 0; i--)
1153 *(data->pid_envvar++) = buf[i];
1154 *data->pid_envvar = '\0';
1157 if (data->user_setup)
1158 data->user_setup (data->user_setup_data);
1162 notify_desktop_launch (GDBusConnection *session_bus,
1163 GDesktopAppInfo *info,
1165 const char *display,
1170 GVariantBuilder uri_variant;
1171 GVariantBuilder extras_variant;
1173 const char *desktop_file_id;
1174 const char *gio_desktop_file;
1176 if (session_bus == NULL)
1179 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
1180 for (iter = uris; iter; iter = iter->next)
1181 g_variant_builder_add (&uri_variant, "s", iter->data);
1183 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
1184 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
1185 g_variant_builder_add (&extras_variant, "{sv}",
1189 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
1190 if (gio_desktop_file != NULL)
1191 g_variant_builder_add (&extras_variant, "{sv}",
1192 "origin-desktop-file",
1193 g_variant_new_bytestring (gio_desktop_file));
1194 if (g_get_prgname () != NULL)
1195 g_variant_builder_add (&extras_variant, "{sv}",
1197 g_variant_new_bytestring (g_get_prgname ()));
1198 g_variant_builder_add (&extras_variant, "{sv}",
1201 (gint64)getpid ()));
1204 desktop_file_id = info->filename;
1205 else if (info->desktop_id)
1206 desktop_file_id = info->desktop_id;
1208 desktop_file_id = "";
1210 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
1211 "org.gtk.gio.DesktopAppInfo",
1213 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
1214 g_variant_new_bytestring (desktop_file_id),
1215 display ? display : "",
1219 g_dbus_connection_send_message (session_bus,
1223 g_object_unref (msg);
1226 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
1229 _g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
1231 GAppLaunchContext *launch_context,
1232 GSpawnFlags spawn_flags,
1233 GSpawnChildSetupFunc user_setup,
1234 gpointer user_setup_data,
1235 GDesktopAppLaunchCallback pid_callback,
1236 gpointer pid_callback_data,
1239 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1240 GDBusConnection *session_bus;
1241 gboolean completed = FALSE;
1243 char **argv, **envp;
1245 ChildSetupData data;
1247 g_return_val_if_fail (appinfo != NULL, FALSE);
1251 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
1254 envp = g_app_launch_context_get_environment (launch_context);
1256 envp = g_get_environ ();
1261 GList *launched_uris;
1263 char *display, *sn_id;
1266 if (!expand_application_parameters (info, &uris,
1267 &argc, &argv, error))
1270 /* Get the subset of URIs we're launching with this process */
1271 launched_uris = NULL;
1272 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
1273 launched_uris = g_list_prepend (launched_uris, iter->data);
1274 launched_uris = g_list_reverse (launched_uris);
1276 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
1278 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1279 _("Unable to find terminal required for application"));
1283 data.user_setup = user_setup;
1284 data.user_setup_data = user_setup_data;
1288 envp = g_environ_setenv (envp,
1289 "GIO_LAUNCHED_DESKTOP_FILE",
1292 envp = g_environ_setenv (envp,
1293 "GIO_LAUNCHED_DESKTOP_FILE_PID",
1294 "XXXXXXXXXXXXXXXXXXXX", /* filled in child_setup */
1296 data.pid_envvar = (char *)g_environ_getenv (envp, "GIO_LAUNCHED_DESKTOP_FILE_PID");
1300 data.pid_envvar = NULL;
1307 GList *launched_files = create_files_for_uris (launched_uris);
1309 display = g_app_launch_context_get_display (launch_context,
1313 envp = g_environ_setenv (envp, "DISPLAY", display, TRUE);
1315 if (info->startup_notify)
1317 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
1320 envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
1323 g_list_free_full (launched_files, g_object_unref);
1326 if (!g_spawn_async (info->path,
1336 g_app_launch_context_launch_failed (launch_context, sn_id);
1340 g_list_free (launched_uris);
1345 if (pid_callback != NULL)
1346 pid_callback (info, pid, pid_callback_data);
1348 notify_desktop_launch (session_bus,
1357 g_list_free (launched_uris);
1362 while (uris != NULL);
1364 /* TODO - need to handle the process exiting immediately
1365 * after launching an app. See http://bugzilla.gnome.org/606960
1367 if (session_bus != NULL)
1369 /* This asynchronous flush holds a reference until it completes,
1370 * which ensures that the following unref won't immediately kill
1371 * the connection if we were the initial owner.
1373 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
1374 g_object_unref (session_bus);
1387 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
1389 GAppLaunchContext *launch_context,
1392 return _g_desktop_app_info_launch_uris_internal (appinfo, uris,
1394 _SPAWN_FLAGS_DEFAULT,
1395 NULL, NULL, NULL, NULL,
1400 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1402 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1404 return info->exec &&
1405 ((strstr (info->exec, "%u") != NULL) ||
1406 (strstr (info->exec, "%U") != NULL));
1410 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1412 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1414 return info->exec &&
1415 ((strstr (info->exec, "%f") != NULL) ||
1416 (strstr (info->exec, "%F") != NULL));
1420 g_desktop_app_info_launch (GAppInfo *appinfo,
1422 GAppLaunchContext *launch_context,
1432 uri = g_file_get_uri (files->data);
1433 uris = g_list_prepend (uris, uri);
1434 files = files->next;
1437 uris = g_list_reverse (uris);
1439 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1441 g_list_free_full (uris, g_free);
1447 * g_desktop_app_info_launch_uris_as_manager:
1448 * @appinfo: a #GDesktopAppInfo
1449 * @uris: (element-type utf8): List of URIs
1450 * @launch_context: a #GAppLaunchContext
1451 * @spawn_flags: #GSpawnFlags, used for each process
1452 * @user_setup: (scope call): a #GSpawnChildSetupFunc, used once for
1454 * @user_setup_data: (closure user_setup): User data for @user_setup
1455 * @pid_callback: (scope call): Callback for child processes
1456 * @pid_callback_data: (closure pid_callback): User data for @callback
1457 * @error: return location for a #GError, or %NULL
1459 * This function performs the equivalent of g_app_info_launch_uris(),
1460 * but is intended primarily for operating system components that
1461 * launch applications. Ordinary applications should use
1462 * g_app_info_launch_uris().
1464 * In contrast to g_app_info_launch_uris(), all processes created will
1465 * always be run directly as children as if by the UNIX fork()/exec()
1468 * This guarantee allows additional control over the exact environment
1469 * of the child processes, which is provided via a setup function
1470 * @user_setup, as well as the process identifier of each child process
1471 * via @pid_callback. See g_spawn_async() for more information about the
1472 * semantics of the @user_setup function.
1474 * Returns: %TRUE on successful launch, %FALSE otherwise.
1477 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
1479 GAppLaunchContext *launch_context,
1480 GSpawnFlags spawn_flags,
1481 GSpawnChildSetupFunc user_setup,
1482 gpointer user_setup_data,
1483 GDesktopAppLaunchCallback pid_callback,
1484 gpointer pid_callback_data,
1487 return _g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
1499 * g_desktop_app_info_set_desktop_env:
1500 * @desktop_env: a string specifying what desktop this is
1502 * Sets the name of the desktop that the application is running in.
1503 * This is used by g_app_info_should_show() and
1504 * g_desktop_app_info_get_show_in() to evaluate the
1505 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1506 * desktop entry fields.
1508 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1509 * Menu specification</ulink> recognizes the following:
1511 * <member>GNOME</member>
1512 * <member>KDE</member>
1513 * <member>ROX</member>
1514 * <member>XFCE</member>
1515 * <member>LXDE</member>
1516 * <member>Unity</member>
1517 * <member>Old</member>
1520 * Should be called only once; subsequent calls are ignored.
1523 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1525 G_LOCK (g_desktop_env);
1527 g_desktop_env = g_strdup (desktop_env);
1528 G_UNLOCK (g_desktop_env);
1532 g_desktop_app_info_should_show (GAppInfo *appinfo)
1534 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1536 if (info->nodisplay)
1539 return g_desktop_app_info_get_show_in (info, NULL);
1548 ensure_dir (DirType type,
1551 char *path, *display_name;
1554 if (type == APP_DIR)
1555 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1557 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1560 if (g_mkdir_with_parents (path, 0700) == 0)
1564 display_name = g_filename_display_name (path);
1565 if (type == APP_DIR)
1566 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1567 _("Can't create user application configuration folder %s: %s"),
1568 display_name, g_strerror (errsv));
1570 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1571 _("Can't create user MIME configuration folder %s: %s"),
1572 display_name, g_strerror (errsv));
1574 g_free (display_name);
1581 update_mimeapps_list (const char *desktop_id,
1582 const char *content_type,
1583 UpdateMimeFlags flags,
1586 char *dirname, *filename, *string;
1588 gboolean load_succeeded, res;
1589 char **old_list, **list;
1590 gsize length, data_size;
1593 char **content_types;
1595 /* Don't add both at start and end */
1596 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
1597 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
1599 dirname = ensure_dir (APP_DIR, error);
1603 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1606 key_file = g_key_file_new ();
1607 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1608 if (!load_succeeded ||
1609 (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) &&
1610 !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) &&
1611 !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP)))
1613 g_key_file_free (key_file);
1614 key_file = g_key_file_new ();
1619 content_types = g_new (char *, 2);
1620 content_types[0] = g_strdup (content_type);
1621 content_types[1] = NULL;
1625 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
1628 for (k = 0; content_types && content_types[k]; k++)
1630 /* set as default, if requested so */
1631 string = g_key_file_get_string (key_file,
1632 DEFAULT_APPLICATIONS_GROUP,
1636 if (g_strcmp0 (string, desktop_id) != 0 &&
1637 (flags & UPDATE_MIME_SET_DEFAULT))
1640 string = g_strdup (desktop_id);
1642 /* add in the non-default list too, if it's not already there */
1643 flags |= UPDATE_MIME_SET_NON_DEFAULT;
1646 if (string == NULL || desktop_id == NULL)
1647 g_key_file_remove_key (key_file,
1648 DEFAULT_APPLICATIONS_GROUP,
1652 g_key_file_set_string (key_file,
1653 DEFAULT_APPLICATIONS_GROUP,
1662 /* reuse the list from above */
1666 g_strfreev (content_types);
1667 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1670 for (k = 0; content_types && content_types[k]; k++)
1672 /* Add to the right place in the list */
1675 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1676 content_types[k], &length, NULL);
1678 list = g_new (char *, 1 + length + 1);
1682 /* if we're adding a last-used hint, just put the application in front of the list */
1683 if (flags & UPDATE_MIME_SET_LAST_USED)
1685 /* avoid adding this again as non-default later */
1686 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1687 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1689 list[i++] = g_strdup (desktop_id);
1694 for (j = 0; old_list[j] != NULL; j++)
1696 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1698 /* rewrite other entries if they're different from the new one */
1699 list[i++] = g_strdup (old_list[j]);
1701 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1703 /* we encountered an old entry which is equal to the one we're adding as non-default,
1704 * don't change its position in the list.
1706 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1707 list[i++] = g_strdup (old_list[j]);
1712 /* add it at the end of the list */
1713 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1714 list[i++] = g_strdup (desktop_id);
1718 g_strfreev (old_list);
1720 if (list[0] == NULL || desktop_id == NULL)
1721 g_key_file_remove_key (key_file,
1722 ADDED_ASSOCIATIONS_GROUP,
1726 g_key_file_set_string_list (key_file,
1727 ADDED_ASSOCIATIONS_GROUP,
1729 (const char * const *)list, i);
1736 /* reuse the list from above */
1740 g_strfreev (content_types);
1741 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1744 for (k = 0; content_types && content_types[k]; k++)
1746 /* Remove from removed associations group (unless remove) */
1749 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1750 content_types[k], &length, NULL);
1752 list = g_new (char *, 1 + length + 1);
1755 if (flags & UPDATE_MIME_REMOVE)
1756 list[i++] = g_strdup (desktop_id);
1759 for (j = 0; old_list[j] != NULL; j++)
1761 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1762 list[i++] = g_strdup (old_list[j]);
1767 g_strfreev (old_list);
1769 if (list[0] == NULL || desktop_id == NULL)
1770 g_key_file_remove_key (key_file,
1771 REMOVED_ASSOCIATIONS_GROUP,
1775 g_key_file_set_string_list (key_file,
1776 REMOVED_ASSOCIATIONS_GROUP,
1778 (const char * const *)list, i);
1783 g_strfreev (content_types);
1785 data = g_key_file_to_data (key_file, &data_size, error);
1786 g_key_file_free (key_file);
1788 res = g_file_set_contents (filename, data, data_size, error);
1790 mime_info_cache_reload (NULL);
1799 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
1800 const char *content_type,
1803 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1805 if (!g_desktop_app_info_ensure_saved (info, error))
1808 if (!info->desktop_id)
1810 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1811 _("Application information lacks an identifier"));
1815 /* both add support for the content type and set as last used */
1816 return update_mimeapps_list (info->desktop_id, content_type,
1817 UPDATE_MIME_SET_NON_DEFAULT |
1818 UPDATE_MIME_SET_LAST_USED,
1823 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1824 const char *content_type,
1827 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1829 if (!g_desktop_app_info_ensure_saved (info, error))
1832 if (!info->desktop_id)
1834 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1835 _("Application information lacks an identifier"));
1839 return update_mimeapps_list (info->desktop_id, content_type,
1840 UPDATE_MIME_SET_DEFAULT,
1845 update_program_done (GPid pid,
1849 /* Did the application exit correctly */
1850 if (WIFEXITED (status) &&
1851 WEXITSTATUS (status) == 0)
1853 /* Here we could clean out any caches in use */
1858 run_update_command (char *command,
1867 GError *error = NULL;
1870 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1872 if (g_spawn_async ("/", argv,
1874 G_SPAWN_SEARCH_PATH |
1875 G_SPAWN_STDOUT_TO_DEV_NULL |
1876 G_SPAWN_STDERR_TO_DEV_NULL |
1877 G_SPAWN_DO_NOT_REAP_CHILD,
1878 NULL, NULL, /* No setup function */
1881 g_child_watch_add (pid, update_program_done, NULL);
1884 /* If we get an error at this point, it's quite likely the user doesn't
1885 * have an installed copy of either 'update-mime-database' or
1886 * 'update-desktop-database'. I don't think we want to popup an error
1887 * dialog at this point, so we just do a g_warning to give the user a
1888 * chance of debugging it.
1890 g_warning ("%s", error->message);
1897 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1898 const char *extension,
1901 char *filename, *basename, *mimetype;
1905 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1908 dirname = ensure_dir (MIMETYPE_DIR, error);
1912 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1913 filename = g_build_filename (dirname, basename, NULL);
1917 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1919 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1924 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1925 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1926 " <mime-type type=\"%s\">\n"
1927 " <comment>%s document</comment>\n"
1928 " <glob pattern=\"*.%s\"/>\n"
1930 "</mime-info>\n", mimetype, extension, extension);
1932 g_file_set_contents (filename, contents, -1, NULL);
1935 run_update_command ("update-mime-database", "mime");
1939 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1949 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1950 const char *content_type,
1953 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1955 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1958 return update_mimeapps_list (info->desktop_id, content_type,
1959 UPDATE_MIME_SET_NON_DEFAULT,
1964 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1970 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1971 const char *content_type,
1974 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1976 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1979 return update_mimeapps_list (info->desktop_id, content_type,
1985 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1991 char *data, *desktop_id;
1996 if (info->filename != NULL)
1999 /* This is only used for object created with
2000 * g_app_info_create_from_commandline. All other
2001 * object should have a filename
2004 dirname = ensure_dir (APP_DIR, error);
2008 key_file = g_key_file_new ();
2010 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2011 "Encoding", "UTF-8");
2012 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2013 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
2014 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2015 G_KEY_FILE_DESKTOP_KEY_TYPE,
2016 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
2018 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2019 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
2020 if (info->nodisplay)
2021 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2022 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2024 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2025 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
2027 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2028 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
2030 if (info->generic_name != NULL)
2031 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2032 GENERIC_NAME_KEY, info->generic_name);
2034 if (info->fullname != NULL)
2035 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2036 FULL_NAME_KEY, info->fullname);
2038 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2039 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
2041 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2042 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2044 data = g_key_file_to_data (key_file, &data_size, NULL);
2045 g_key_file_free (key_file);
2047 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
2048 filename = g_build_filename (dirname, desktop_id, NULL);
2049 g_free (desktop_id);
2052 fd = g_mkstemp (filename);
2057 display_name = g_filename_display_name (filename);
2058 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2059 _("Can't create user desktop file %s"), display_name);
2060 g_free (display_name);
2066 desktop_id = g_path_get_basename (filename);
2070 res = g_file_set_contents (filename, data, data_size, error);
2073 g_free (desktop_id);
2078 info->filename = filename;
2079 info->desktop_id = desktop_id;
2081 run_update_command ("update-desktop-database", "applications");
2087 g_desktop_app_info_can_delete (GAppInfo *appinfo)
2089 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2093 if (strstr (info->filename, "/userapp-"))
2094 return g_access (info->filename, W_OK) == 0;
2101 g_desktop_app_info_delete (GAppInfo *appinfo)
2103 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2107 if (g_remove (info->filename) == 0)
2109 update_mimeapps_list (info->desktop_id, NULL,
2113 g_free (info->filename);
2114 info->filename = NULL;
2115 g_free (info->desktop_id);
2116 info->desktop_id = NULL;
2126 * g_app_info_create_from_commandline:
2127 * @commandline: the commandline to use
2128 * @application_name: (allow-none): the application name, or %NULL to use @commandline
2129 * @flags: flags that can specify details of the created #GAppInfo
2130 * @error: a #GError location to store the error occurring, %NULL to ignore.
2132 * Creates a new #GAppInfo from the given information.
2134 * Returns: (transfer full): new #GAppInfo for given command.
2137 g_app_info_create_from_commandline (const char *commandline,
2138 const char *application_name,
2139 GAppInfoCreateFlags flags,
2144 GDesktopAppInfo *info;
2146 g_return_val_if_fail (commandline, NULL);
2148 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
2150 info->filename = NULL;
2151 info->desktop_id = NULL;
2153 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
2154 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
2155 info->hidden = FALSE;
2156 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
2157 info->exec = g_strconcat (commandline, " %u", NULL);
2159 info->exec = g_strconcat (commandline, " %f", NULL);
2160 info->nodisplay = TRUE;
2161 info->binary = binary_from_exec (info->exec);
2163 if (application_name)
2164 info->name = g_strdup (application_name);
2167 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
2168 split = g_strsplit (commandline, " ", 2);
2169 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
2171 info->name = basename;
2172 if (info->name == NULL)
2173 info->name = g_strdup ("custom");
2175 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
2177 return G_APP_INFO (info);
2181 g_desktop_app_info_iface_init (GAppInfoIface *iface)
2183 iface->dup = g_desktop_app_info_dup;
2184 iface->equal = g_desktop_app_info_equal;
2185 iface->get_id = g_desktop_app_info_get_id;
2186 iface->get_name = g_desktop_app_info_get_name;
2187 iface->get_description = g_desktop_app_info_get_description;
2188 iface->get_executable = g_desktop_app_info_get_executable;
2189 iface->get_icon = g_desktop_app_info_get_icon;
2190 iface->launch = g_desktop_app_info_launch;
2191 iface->supports_uris = g_desktop_app_info_supports_uris;
2192 iface->supports_files = g_desktop_app_info_supports_files;
2193 iface->launch_uris = g_desktop_app_info_launch_uris;
2194 iface->should_show = g_desktop_app_info_should_show;
2195 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
2196 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
2197 iface->add_supports_type = g_desktop_app_info_add_supports_type;
2198 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
2199 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
2200 iface->can_delete = g_desktop_app_info_can_delete;
2201 iface->do_delete = g_desktop_app_info_delete;
2202 iface->get_commandline = g_desktop_app_info_get_commandline;
2203 iface->get_display_name = g_desktop_app_info_get_display_name;
2204 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
2208 app_info_in_list (GAppInfo *info,
2211 while (list != NULL)
2213 if (g_app_info_equal (info, list->data))
2221 * g_app_info_get_recommended_for_type:
2222 * @content_type: the content type to find a #GAppInfo for
2224 * Gets a list of recommended #GAppInfos for a given content type, i.e.
2225 * those applications which claim to support the given content type exactly,
2226 * and not by MIME type subclassing.
2227 * Note that the first application of the list is the last used one, i.e.
2228 * the last one for which g_app_info_set_as_last_used_for_type() has been
2231 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2232 * for given @content_type or %NULL on error.
2237 g_app_info_get_recommended_for_type (const gchar *content_type)
2239 GList *desktop_entries, *l;
2241 GDesktopAppInfo *info;
2243 g_return_val_if_fail (content_type != NULL, NULL);
2245 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
2248 for (l = desktop_entries; l != NULL; l = l->next)
2250 char *desktop_entry = l->data;
2252 info = g_desktop_app_info_new (desktop_entry);
2255 if (app_info_in_list (G_APP_INFO (info), infos))
2256 g_object_unref (info);
2258 infos = g_list_prepend (infos, info);
2260 g_free (desktop_entry);
2263 g_list_free (desktop_entries);
2265 return g_list_reverse (infos);
2269 * g_app_info_get_fallback_for_type:
2270 * @content_type: the content type to find a #GAppInfo for
2272 * Gets a list of fallback #GAppInfos for a given content type, i.e.
2273 * those applications which claim to support the given content type
2274 * by MIME type subclassing and not directly.
2276 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2277 * for given @content_type or %NULL on error.
2282 g_app_info_get_fallback_for_type (const gchar *content_type)
2284 GList *desktop_entries, *l;
2285 GList *infos, *recommended_infos;
2286 GDesktopAppInfo *info;
2288 g_return_val_if_fail (content_type != NULL, NULL);
2290 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
2291 recommended_infos = g_app_info_get_recommended_for_type (content_type);
2294 for (l = desktop_entries; l != NULL; l = l->next)
2296 char *desktop_entry = l->data;
2298 info = g_desktop_app_info_new (desktop_entry);
2301 if (app_info_in_list (G_APP_INFO (info), infos) ||
2302 app_info_in_list (G_APP_INFO (info), recommended_infos))
2303 g_object_unref (info);
2305 infos = g_list_prepend (infos, info);
2307 g_free (desktop_entry);
2310 g_list_free (desktop_entries);
2311 g_list_free_full (recommended_infos, g_object_unref);
2313 return g_list_reverse (infos);
2317 * g_app_info_get_all_for_type:
2318 * @content_type: the content type to find a #GAppInfo for
2320 * Gets a list of all #GAppInfos for a given content type,
2321 * including the recommended and fallback #GAppInfos. See
2322 * g_app_info_get_recommended_for_type() and
2323 * g_app_info_get_fallback_for_type().
2325 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2326 * for given @content_type or %NULL on error.
2329 g_app_info_get_all_for_type (const char *content_type)
2331 GList *desktop_entries, *l;
2333 char *user_default = NULL;
2334 GDesktopAppInfo *info;
2336 g_return_val_if_fail (content_type != NULL, NULL);
2338 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2341 /* put the user default in front of the list, for compatibility */
2342 if (user_default != NULL)
2344 info = g_desktop_app_info_new (user_default);
2347 infos = g_list_prepend (infos, info);
2350 g_free (user_default);
2352 for (l = desktop_entries; l != NULL; l = l->next)
2354 char *desktop_entry = l->data;
2356 info = g_desktop_app_info_new (desktop_entry);
2359 if (app_info_in_list (G_APP_INFO (info), infos))
2360 g_object_unref (info);
2362 infos = g_list_prepend (infos, info);
2364 g_free (desktop_entry);
2367 g_list_free (desktop_entries);
2369 return g_list_reverse (infos);
2373 * g_app_info_reset_type_associations:
2374 * @content_type: a content type
2376 * Removes all changes to the type associations done by
2377 * g_app_info_set_as_default_for_type(),
2378 * g_app_info_set_as_default_for_extension(),
2379 * g_app_info_add_supports_type() or
2380 * g_app_info_remove_supports_type().
2385 g_app_info_reset_type_associations (const char *content_type)
2387 update_mimeapps_list (NULL, content_type,
2393 * g_app_info_get_default_for_type:
2394 * @content_type: the content type to find a #GAppInfo for
2395 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
2398 * Gets the default #GAppInfo for a given content type.
2400 * Returns: (transfer full): #GAppInfo for given @content_type or
2404 g_app_info_get_default_for_type (const char *content_type,
2405 gboolean must_support_uris)
2407 GList *desktop_entries, *l;
2408 char *user_default = NULL;
2411 g_return_val_if_fail (content_type != NULL, NULL);
2413 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2417 if (user_default != NULL)
2419 info = (GAppInfo *) g_desktop_app_info_new (user_default);
2423 if (must_support_uris && !g_app_info_supports_uris (info))
2425 g_object_unref (info);
2431 g_free (user_default);
2435 g_list_free_full (desktop_entries, g_free);
2439 /* pick the first from the other list that matches our URI
2442 for (l = desktop_entries; l != NULL; l = l->next)
2444 char *desktop_entry = l->data;
2446 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
2449 if (must_support_uris && !g_app_info_supports_uris (info))
2451 g_object_unref (info);
2459 g_list_free_full (desktop_entries, g_free);
2465 * g_app_info_get_default_for_uri_scheme:
2466 * @uri_scheme: a string containing a URI scheme.
2468 * Gets the default application for handling URIs with
2469 * the given URI scheme. A URI scheme is the initial part
2470 * of the URI, up to but not including the ':', e.g. "http",
2473 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
2476 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
2479 char *content_type, *scheme_down;
2481 scheme_down = g_ascii_strdown (uri_scheme, -1);
2482 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
2483 g_free (scheme_down);
2484 app_info = g_app_info_get_default_for_type (content_type, FALSE);
2485 g_free (content_type);
2491 get_apps_from_dir (GHashTable *apps,
2492 const char *dirname,
2496 const char *basename;
2497 char *filename, *subprefix, *desktop_id;
2499 GDesktopAppInfo *appinfo;
2501 dir = g_dir_open (dirname, 0, NULL);
2504 while ((basename = g_dir_read_name (dir)) != NULL)
2506 filename = g_build_filename (dirname, basename, NULL);
2507 if (g_str_has_suffix (basename, ".desktop"))
2509 desktop_id = g_strconcat (prefix, basename, NULL);
2511 /* Use _extended so we catch NULLs too (hidden) */
2512 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
2514 appinfo = g_desktop_app_info_new_from_filename (filename);
2517 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
2519 g_object_unref (appinfo);
2524 if (appinfo || hidden)
2526 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
2530 /* Reuse instead of strdup here */
2531 appinfo->desktop_id = desktop_id;
2536 g_free (desktop_id);
2540 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
2542 subprefix = g_strconcat (prefix, basename, "-", NULL);
2543 get_apps_from_dir (apps, filename, subprefix);
2555 * g_app_info_get_all:
2557 * Gets a list of all of the applications currently registered
2560 * For desktop files, this includes applications that have
2561 * <literal>NoDisplay=true</literal> set or are excluded from
2562 * display by means of <literal>OnlyShowIn</literal> or
2563 * <literal>NotShowIn</literal>. See g_app_info_should_show().
2564 * The returned list does not include applications which have
2565 * the <literal>Hidden</literal> key set.
2567 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
2570 g_app_info_get_all (void)
2572 const char * const *dirs;
2574 GHashTableIter iter;
2579 dirs = get_applications_search_path ();
2581 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
2585 for (i = 0; dirs[i] != NULL; i++)
2586 get_apps_from_dir (apps, dirs[i], "");
2590 g_hash_table_iter_init (&iter, apps);
2591 while (g_hash_table_iter_next (&iter, NULL, &value))
2594 infos = g_list_prepend (infos, value);
2597 g_hash_table_destroy (apps);
2599 return g_list_reverse (infos);
2602 /* Cacheing of mimeinfo.cache and defaults.list files */
2606 GHashTable *mime_info_cache_map;
2607 GHashTable *defaults_list_map;
2608 GHashTable *mimeapps_list_added_map;
2609 GHashTable *mimeapps_list_removed_map;
2610 GHashTable *mimeapps_list_defaults_map;
2611 time_t mime_info_cache_timestamp;
2612 time_t defaults_list_timestamp;
2613 time_t mimeapps_list_timestamp;
2617 GList *dirs; /* mimeinfo.cache and defaults.list */
2618 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
2619 time_t last_stat_time;
2620 guint should_ping_mime_monitor : 1;
2623 static MimeInfoCache *mime_info_cache = NULL;
2624 G_LOCK_DEFINE_STATIC (mime_info_cache);
2626 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2627 const char *mime_type,
2628 char **new_desktop_file_ids);
2630 static MimeInfoCache * mime_info_cache_new (void);
2633 destroy_info_cache_value (gpointer key,
2637 g_list_free_full (value, g_free);
2641 destroy_info_cache_map (GHashTable *info_cache_map)
2643 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
2644 g_hash_table_destroy (info_cache_map);
2648 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2649 const char *cache_file,
2655 filename = g_build_filename (dir->path, cache_file, NULL);
2657 if (g_stat (filename, &buf) < 0)
2664 if (buf.st_mtime != *timestamp)
2670 /* Call with lock held */
2672 remove_all (gpointer key,
2681 mime_info_cache_blow_global_cache (void)
2683 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
2688 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2692 gchar *filename, **mime_types;
2699 if (dir->mime_info_cache_map != NULL &&
2700 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2701 &dir->mime_info_cache_timestamp))
2704 if (dir->mime_info_cache_map != NULL)
2705 destroy_info_cache_map (dir->mime_info_cache_map);
2707 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2708 (GDestroyNotify) g_free,
2711 key_file = g_key_file_new ();
2713 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2715 if (g_stat (filename, &buf) < 0)
2718 if (dir->mime_info_cache_timestamp > 0)
2719 mime_info_cache->should_ping_mime_monitor = TRUE;
2721 dir->mime_info_cache_timestamp = buf.st_mtime;
2723 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2728 if (load_error != NULL)
2731 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2734 if (load_error != NULL)
2737 for (i = 0; mime_types[i] != NULL; i++)
2739 gchar **desktop_file_ids;
2740 char *unaliased_type;
2741 desktop_file_ids = g_key_file_get_string_list (key_file,
2747 if (desktop_file_ids == NULL)
2750 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2751 mime_info_cache_dir_add_desktop_entries (dir,
2754 g_free (unaliased_type);
2756 g_strfreev (desktop_file_ids);
2759 g_strfreev (mime_types);
2760 g_key_file_free (key_file);
2765 g_key_file_free (key_file);
2767 if (mime_types != NULL)
2768 g_strfreev (mime_types);
2771 g_error_free (load_error);
2775 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2779 gchar *filename, **mime_types;
2780 char *unaliased_type;
2781 char **desktop_file_ids;
2788 if (dir->defaults_list_map != NULL &&
2789 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2790 &dir->defaults_list_timestamp))
2793 if (dir->defaults_list_map != NULL)
2794 g_hash_table_destroy (dir->defaults_list_map);
2795 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2796 g_free, (GDestroyNotify)g_strfreev);
2799 key_file = g_key_file_new ();
2801 filename = g_build_filename (dir->path, "defaults.list", NULL);
2802 if (g_stat (filename, &buf) < 0)
2805 if (dir->defaults_list_timestamp > 0)
2806 mime_info_cache->should_ping_mime_monitor = TRUE;
2808 dir->defaults_list_timestamp = buf.st_mtime;
2810 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2814 if (load_error != NULL)
2817 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2819 if (mime_types != NULL)
2821 for (i = 0; mime_types[i] != NULL; i++)
2823 desktop_file_ids = g_key_file_get_string_list (key_file,
2824 DEFAULT_APPLICATIONS_GROUP,
2828 if (desktop_file_ids == NULL)
2831 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2832 g_hash_table_replace (dir->defaults_list_map,
2837 g_strfreev (mime_types);
2840 g_key_file_free (key_file);
2845 g_key_file_free (key_file);
2847 if (mime_types != NULL)
2848 g_strfreev (mime_types);
2851 g_error_free (load_error);
2855 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2859 gchar *filename, **mime_types;
2860 char *unaliased_type;
2861 char **desktop_file_ids;
2869 if (dir->mimeapps_list_added_map != NULL &&
2870 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2871 &dir->mimeapps_list_timestamp))
2874 if (dir->mimeapps_list_added_map != NULL)
2875 g_hash_table_destroy (dir->mimeapps_list_added_map);
2876 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2877 g_free, (GDestroyNotify)g_strfreev);
2879 if (dir->mimeapps_list_removed_map != NULL)
2880 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2881 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2882 g_free, (GDestroyNotify)g_strfreev);
2884 if (dir->mimeapps_list_defaults_map != NULL)
2885 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2886 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2889 key_file = g_key_file_new ();
2891 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2892 if (g_stat (filename, &buf) < 0)
2895 if (dir->mimeapps_list_timestamp > 0)
2896 mime_info_cache->should_ping_mime_monitor = TRUE;
2898 dir->mimeapps_list_timestamp = buf.st_mtime;
2900 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2904 if (load_error != NULL)
2907 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2909 if (mime_types != NULL)
2911 for (i = 0; mime_types[i] != NULL; i++)
2913 desktop_file_ids = g_key_file_get_string_list (key_file,
2914 ADDED_ASSOCIATIONS_GROUP,
2918 if (desktop_file_ids == NULL)
2921 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2922 g_hash_table_replace (dir->mimeapps_list_added_map,
2927 g_strfreev (mime_types);
2930 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2932 if (mime_types != NULL)
2934 for (i = 0; mime_types[i] != NULL; i++)
2936 desktop_file_ids = g_key_file_get_string_list (key_file,
2937 REMOVED_ASSOCIATIONS_GROUP,
2941 if (desktop_file_ids == NULL)
2944 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2945 g_hash_table_replace (dir->mimeapps_list_removed_map,
2950 g_strfreev (mime_types);
2953 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2955 if (mime_types != NULL)
2957 for (i = 0; mime_types[i] != NULL; i++)
2959 desktop_id = g_key_file_get_string (key_file,
2960 DEFAULT_APPLICATIONS_GROUP,
2963 if (desktop_id == NULL)
2966 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2967 g_hash_table_replace (dir->mimeapps_list_defaults_map,
2972 g_strfreev (mime_types);
2975 g_key_file_free (key_file);
2980 g_key_file_free (key_file);
2982 if (mime_types != NULL)
2983 g_strfreev (mime_types);
2986 g_error_free (load_error);
2989 static MimeInfoCacheDir *
2990 mime_info_cache_dir_new (const char *path)
2992 MimeInfoCacheDir *dir;
2994 dir = g_new0 (MimeInfoCacheDir, 1);
2995 dir->path = g_strdup (path);
3001 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
3006 if (dir->mime_info_cache_map != NULL)
3008 destroy_info_cache_map (dir->mime_info_cache_map);
3009 dir->mime_info_cache_map = NULL;
3013 if (dir->defaults_list_map != NULL)
3015 g_hash_table_destroy (dir->defaults_list_map);
3016 dir->defaults_list_map = NULL;
3019 if (dir->mimeapps_list_added_map != NULL)
3021 g_hash_table_destroy (dir->mimeapps_list_added_map);
3022 dir->mimeapps_list_added_map = NULL;
3025 if (dir->mimeapps_list_removed_map != NULL)
3027 g_hash_table_destroy (dir->mimeapps_list_removed_map);
3028 dir->mimeapps_list_removed_map = NULL;
3031 if (dir->mimeapps_list_defaults_map != NULL)
3033 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
3034 dir->mimeapps_list_defaults_map = NULL;
3041 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
3042 const char *mime_type,
3043 char **new_desktop_file_ids)
3045 GList *desktop_file_ids;
3048 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
3051 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
3053 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
3054 desktop_file_ids = g_list_append (desktop_file_ids,
3055 g_strdup (new_desktop_file_ids[i]));
3058 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
3062 mime_info_cache_init_dir_lists (void)
3064 const char * const *dirs;
3067 mime_info_cache = mime_info_cache_new ();
3069 dirs = get_applications_search_path ();
3071 for (i = 0; dirs[i] != NULL; i++)
3073 MimeInfoCacheDir *dir;
3075 dir = mime_info_cache_dir_new (dirs[i]);
3079 mime_info_cache_dir_init (dir);
3080 mime_info_cache_dir_init_defaults_list (dir);
3081 mime_info_cache_dir_init_mimeapps_list (dir);
3083 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
3089 mime_info_cache_update_dir_lists (void)
3093 tmp = mime_info_cache->dirs;
3097 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
3099 /* No need to do this if we had file monitors... */
3100 mime_info_cache_blow_global_cache ();
3101 mime_info_cache_dir_init (dir);
3102 mime_info_cache_dir_init_defaults_list (dir);
3103 mime_info_cache_dir_init_mimeapps_list (dir);
3110 mime_info_cache_init (void)
3112 G_LOCK (mime_info_cache);
3113 if (mime_info_cache == NULL)
3114 mime_info_cache_init_dir_lists ();
3120 if (now >= mime_info_cache->last_stat_time + 10)
3122 mime_info_cache_update_dir_lists ();
3123 mime_info_cache->last_stat_time = now;
3127 if (mime_info_cache->should_ping_mime_monitor)
3129 /* g_idle_add (emit_mime_changed, NULL); */
3130 mime_info_cache->should_ping_mime_monitor = FALSE;
3133 G_UNLOCK (mime_info_cache);
3136 static MimeInfoCache *
3137 mime_info_cache_new (void)
3139 MimeInfoCache *cache;
3141 cache = g_new0 (MimeInfoCache, 1);
3143 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
3144 (GDestroyNotify) g_free,
3145 (GDestroyNotify) g_free);
3150 mime_info_cache_free (MimeInfoCache *cache)
3155 g_list_free_full (cache->dirs, (GDestroyNotify) mime_info_cache_dir_free);
3156 g_hash_table_destroy (cache->global_defaults_cache);
3161 * mime_info_cache_reload:
3162 * @dir: directory path which needs reloading.
3164 * Reload the mime information for the @dir.
3167 mime_info_cache_reload (const char *dir)
3169 /* FIXME: just reload the dir that needs reloading,
3170 * don't blow the whole cache
3172 if (mime_info_cache != NULL)
3174 G_LOCK (mime_info_cache);
3175 mime_info_cache_free (mime_info_cache);
3176 mime_info_cache = NULL;
3177 G_UNLOCK (mime_info_cache);
3182 append_desktop_entry (GList *list,
3183 const char *desktop_entry,
3184 GList *removed_entries)
3186 /* Add if not already in list, and valid */
3187 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
3188 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
3189 list = g_list_prepend (list, g_strdup (desktop_entry));
3195 * get_all_desktop_entries_for_mime_type:
3196 * @mime_type: a mime type.
3197 * @except: NULL or a strv list
3199 * Returns all the desktop ids for @mime_type. The desktop files
3200 * are listed in an order so that default applications are listed before
3201 * non-default ones, and handlers for inherited mimetypes are listed
3202 * after the base ones.
3204 * Optionally doesn't list the desktop ids given in the @except
3206 * Return value: a #GList containing the desktop ids which claim
3207 * to handle @mime_type.
3210 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
3211 const char **except,
3212 gboolean include_fallback,
3213 char **explicit_default)
3215 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
3216 MimeInfoCacheDir *dir;
3217 char *mime_type, *default_entry = NULL;
3218 char *old_default_entry = NULL;
3221 char **default_entries;
3222 char **removed_associations;
3227 mime_info_cache_init ();
3229 if (include_fallback)
3231 /* collect all ancestors */
3232 mime_types = _g_unix_content_type_get_parents (base_mime_type);
3233 array = g_ptr_array_new ();
3234 for (i = 0; mime_types[i]; i++)
3235 g_ptr_array_add (array, mime_types[i]);
3236 g_free (mime_types);
3237 for (i = 0; i < array->len; i++)
3239 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
3240 for (j = 0; anc[j]; j++)
3242 for (k = 0; k < array->len; k++)
3244 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
3247 if (k == array->len) /* not found */
3248 g_ptr_array_add (array, g_strdup (anc[j]));
3252 g_ptr_array_add (array, NULL);
3253 mime_types = (char **)g_ptr_array_free (array, FALSE);
3257 mime_types = g_malloc0 (2 * sizeof (gchar *));
3258 mime_types[0] = g_strdup (base_mime_type);
3259 mime_types[1] = NULL;
3262 G_LOCK (mime_info_cache);
3264 removed_entries = NULL;
3265 desktop_entries = NULL;
3267 for (i = 0; except != NULL && except[i] != NULL; i++)
3268 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
3270 for (i = 0; mime_types[i] != NULL; i++)
3272 mime_type = mime_types[i];
3274 /* Go through all apps listed in user and system dirs */
3275 for (dir_list = mime_info_cache->dirs;
3277 dir_list = dir_list->next)
3279 dir = dir_list->data;
3281 /* Pick the explicit default application if we got no result earlier
3282 * (ie, for more specific mime types)
3284 if (desktop_entries == NULL)
3286 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
3290 /* Save the default entry if it's the first one we encounter */
3291 if (default_entry == NULL)
3292 default_entry = g_strdup (entry);
3296 /* Then added associations from mimeapps.list */
3297 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
3298 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3299 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3301 /* Then removed associations from mimeapps.list */
3302 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
3303 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
3304 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
3306 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
3307 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
3308 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3310 if (default_entry == NULL && old_default_entry == NULL)
3311 old_default_entry = g_strdup (default_entries[j]);
3313 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3317 /* Go through all entries that support the mimetype */
3318 for (dir_list = mime_info_cache->dirs;
3320 dir_list = dir_list->next)
3322 dir = dir_list->data;
3324 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
3325 for (tmp = list; tmp != NULL; tmp = tmp->next)
3326 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
3330 G_UNLOCK (mime_info_cache);
3332 g_strfreev (mime_types);
3334 /* If we have no default from mimeapps.list, take it from
3335 * defaults.list intead.
3337 * If we do have a default from mimeapps.list, free any one that came
3338 * from defaults.list.
3340 if (default_entry == NULL)
3341 default_entry = old_default_entry;
3343 g_free (old_default_entry);
3345 if (explicit_default != NULL)
3346 *explicit_default = default_entry;
3348 g_free (default_entry);
3350 g_list_free_full (removed_entries, g_free);
3352 desktop_entries = g_list_reverse (desktop_entries);
3354 return desktop_entries;
3357 /* GDesktopAppInfoLookup interface: */
3359 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
3361 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
3362 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
3365 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
3370 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
3371 * @lookup: a #GDesktopAppInfoLookup
3372 * @uri_scheme: a string containing a URI scheme.
3374 * Gets the default application for launching applications
3375 * using this URI scheme for a particular GDesktopAppInfoLookup
3378 * The GDesktopAppInfoLookup interface and this function is used
3379 * to implement g_app_info_get_default_for_uri_scheme() backends
3380 * in a GIO module. There is no reason for applications to use it
3381 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
3383 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3385 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
3388 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
3389 const char *uri_scheme)
3391 GDesktopAppInfoLookupIface *iface;
3393 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
3395 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
3397 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
3400 G_GNUC_END_IGNORE_DEPRECATIONS
3403 * g_desktop_app_info_get_startup_wm_class:
3404 * @app_info: a #GDesktopAppInfo that supports startup notify
3406 * Retrieves the StartupWMClass field from @app_info. This represents the
3407 * WM_CLASS property of the main window of the application, if launched through
3410 * Returns: (transfer none): the startup WM class, or %NULL if none is set
3411 * in the desktop file.
3416 g_desktop_app_info_get_startup_wm_class (GDesktopAppInfo *app_info)
3418 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (app_info), NULL);
3420 return app_info->startup_wm_class;