1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright © 2007 Ryan Lortie
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Author: Alexander Larsson <alexl@redhat.com>
30 #ifdef HAVE_CRT_EXTERNS_H
31 #include <crt_externs.h>
34 #include "gcontenttypeprivate.h"
35 #include "gdesktopappinfo.h"
37 #include "glib-unix.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"
69 #define KEYWORDS_KEY "Keywords"
70 #define STARTUP_WM_CLASS_KEY "StartupWMClass"
77 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
78 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type,
80 gboolean include_fallback,
81 char **explicit_default);
82 static void mime_info_cache_reload (const char *dir);
83 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
89 * Information about an installed application from a desktop file.
91 struct _GDesktopAppInfo
93 GObject parent_instance;
114 char *startup_wm_class;
120 guint startup_notify : 1;
125 UPDATE_MIME_NONE = 1 << 0,
126 UPDATE_MIME_SET_DEFAULT = 1 << 1,
127 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
128 UPDATE_MIME_REMOVE = 1 << 3,
129 UPDATE_MIME_SET_LAST_USED = 1 << 4,
132 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
133 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
134 g_desktop_app_info_iface_init))
136 G_LOCK_DEFINE_STATIC (g_desktop_env);
137 static gchar *g_desktop_env = NULL;
140 search_path_init (gpointer data)
143 const char * const *data_dirs;
144 const char *user_data_dir;
147 data_dirs = g_get_system_data_dirs ();
148 length = g_strv_length ((char **) data_dirs);
150 args = g_new (char *, length + 2);
153 user_data_dir = g_get_user_data_dir ();
154 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
155 for (i = 0; i < length; i++)
156 args[j++] = g_build_filename (data_dirs[i],
157 "applications", NULL);
163 static const char * const *
164 get_applications_search_path (void)
166 static GOnce once_init = G_ONCE_INIT;
167 return g_once (&once_init, search_path_init, NULL);
171 g_desktop_app_info_finalize (GObject *object)
173 GDesktopAppInfo *info;
175 info = G_DESKTOP_APP_INFO (object);
177 g_free (info->desktop_id);
178 g_free (info->filename);
181 g_key_file_unref (info->keyfile);
184 g_free (info->generic_name);
185 g_free (info->fullname);
186 g_free (info->comment);
187 g_free (info->icon_name);
189 g_object_unref (info->icon);
190 g_strfreev (info->keywords);
191 g_strfreev (info->only_show_in);
192 g_strfreev (info->not_show_in);
193 g_free (info->try_exec);
195 g_free (info->binary);
197 g_free (info->categories);
198 g_free (info->startup_wm_class);
199 g_strfreev (info->mime_types);
201 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
205 g_desktop_app_info_set_property(GObject *object,
210 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
215 self->filename = g_value_dup_string (value);
219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
225 g_desktop_app_info_get_property (GObject *object,
230 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
235 g_value_set_string (value, self->filename);
238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
244 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
246 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
248 gobject_class->get_property = g_desktop_app_info_get_property;
249 gobject_class->set_property = g_desktop_app_info_set_property;
250 gobject_class->finalize = g_desktop_app_info_finalize;
253 * GDesktopAppInfo:filename:
255 * The origin filename of this #GDesktopAppInfo
257 g_object_class_install_property (gobject_class,
259 g_param_spec_string ("filename", "Filename", "",
261 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
265 g_desktop_app_info_init (GDesktopAppInfo *local)
270 binary_from_exec (const char *exec)
272 const char *p, *start;
278 while (*p != ' ' && *p != 0)
281 return g_strndup (start, p - start);
286 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
294 start_group = g_key_file_get_start_group (key_file);
295 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
297 g_free (start_group);
300 g_free (start_group);
302 type = g_key_file_get_string (key_file,
303 G_KEY_FILE_DESKTOP_GROUP,
304 G_KEY_FILE_DESKTOP_KEY_TYPE,
306 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
313 try_exec = g_key_file_get_string (key_file,
314 G_KEY_FILE_DESKTOP_GROUP,
315 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
317 if (try_exec && try_exec[0] != '\0')
320 t = g_find_program_in_path (try_exec);
329 exec = g_key_file_get_string (key_file,
330 G_KEY_FILE_DESKTOP_GROUP,
331 G_KEY_FILE_DESKTOP_KEY_EXEC,
333 if (exec && exec[0] != '\0')
337 if (!g_shell_parse_argv (exec, &argc, &argv, NULL))
346 t = g_find_program_in_path (argv[0]);
359 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
360 info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
361 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
362 info->keywords = g_key_file_get_locale_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, KEYWORDS_KEY, NULL, NULL, NULL);
363 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
364 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
365 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
366 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);
367 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);
368 info->try_exec = try_exec;
370 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
371 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
372 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
373 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
374 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
375 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
376 info->startup_wm_class = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, STARTUP_WM_CLASS_KEY, NULL);
377 info->mime_types = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL, NULL);
382 if (g_path_is_absolute (info->icon_name))
386 file = g_file_new_for_path (info->icon_name);
387 info->icon = g_file_icon_new (file);
388 g_object_unref (file);
394 /* Work around a common mistake in desktop files */
395 if ((p = strrchr (info->icon_name, '.')) != NULL &&
396 (strcmp (p, ".png") == 0 ||
397 strcmp (p, ".xpm") == 0 ||
398 strcmp (p, ".svg") == 0))
401 info->icon = g_themed_icon_new (info->icon_name);
406 info->binary = binary_from_exec (info->exec);
408 if (info->path && info->path[0] == '\0')
414 info->keyfile = g_key_file_ref (key_file);
420 g_desktop_app_info_load_file (GDesktopAppInfo *self)
423 gboolean retval = FALSE;
425 g_return_val_if_fail (self->filename != NULL, FALSE);
427 key_file = g_key_file_new ();
429 if (g_key_file_load_from_file (key_file,
434 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
437 g_key_file_unref (key_file);
442 * g_desktop_app_info_new_from_keyfile:
443 * @key_file: an opened #GKeyFile
445 * Creates a new #GDesktopAppInfo.
447 * Returns: a new #GDesktopAppInfo or %NULL on error.
452 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
454 GDesktopAppInfo *info;
456 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
457 info->filename = NULL;
458 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
460 g_object_unref (info);
467 * g_desktop_app_info_new_from_filename:
468 * @filename: the path of a desktop file, in the GLib filename encoding
470 * Creates a new #GDesktopAppInfo.
472 * Returns: a new #GDesktopAppInfo or %NULL on error.
475 g_desktop_app_info_new_from_filename (const char *filename)
477 GDesktopAppInfo *info = NULL;
479 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
480 if (!g_desktop_app_info_load_file (info))
482 g_object_unref (info);
489 * g_desktop_app_info_new:
490 * @desktop_id: the desktop file id
492 * Creates a new #GDesktopAppInfo based on a desktop file id.
494 * A desktop file id is the basename of the desktop file, including the
495 * .desktop extension. GIO is looking for a desktop file with this name
496 * in the <filename>applications</filename> subdirectories of the XDG data
497 * directories (i.e. the directories specified in the
498 * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment
499 * variables). GIO also supports the prefix-to-subdirectory mapping that is
500 * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink>
501 * (i.e. a desktop id of kde-foo.desktop will match
502 * <filename>/usr/share/applications/kde/foo.desktop</filename>).
504 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
507 g_desktop_app_info_new (const char *desktop_id)
509 GDesktopAppInfo *appinfo;
510 const char * const *dirs;
514 dirs = get_applications_search_path ();
516 basename = g_strdup (desktop_id);
518 for (i = 0; dirs[i] != NULL; i++)
523 filename = g_build_filename (dirs[i], desktop_id, NULL);
524 appinfo = g_desktop_app_info_new_from_filename (filename);
530 while ((p = strchr (p, '-')) != NULL)
534 filename = g_build_filename (dirs[i], basename, NULL);
535 appinfo = g_desktop_app_info_new_from_filename (filename);
550 appinfo->desktop_id = g_strdup (desktop_id);
552 if (g_desktop_app_info_get_is_hidden (appinfo))
554 g_object_unref (appinfo);
562 g_desktop_app_info_dup (GAppInfo *appinfo)
564 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
565 GDesktopAppInfo *new_info;
567 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
569 new_info->filename = g_strdup (info->filename);
570 new_info->desktop_id = g_strdup (info->desktop_id);
573 new_info->keyfile = g_key_file_ref (info->keyfile);
575 new_info->name = g_strdup (info->name);
576 new_info->generic_name = g_strdup (info->generic_name);
577 new_info->fullname = g_strdup (info->fullname);
578 new_info->keywords = g_strdupv (info->keywords);
579 new_info->comment = g_strdup (info->comment);
580 new_info->nodisplay = info->nodisplay;
581 new_info->icon_name = g_strdup (info->icon_name);
583 new_info->icon = g_object_ref (info->icon);
584 new_info->only_show_in = g_strdupv (info->only_show_in);
585 new_info->not_show_in = g_strdupv (info->not_show_in);
586 new_info->try_exec = g_strdup (info->try_exec);
587 new_info->exec = g_strdup (info->exec);
588 new_info->binary = g_strdup (info->binary);
589 new_info->path = g_strdup (info->path);
590 new_info->hidden = info->hidden;
591 new_info->terminal = info->terminal;
592 new_info->startup_notify = info->startup_notify;
594 return G_APP_INFO (new_info);
598 g_desktop_app_info_equal (GAppInfo *appinfo1,
601 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
602 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
604 if (info1->desktop_id == NULL ||
605 info2->desktop_id == NULL)
606 return info1 == info2;
608 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
612 g_desktop_app_info_get_id (GAppInfo *appinfo)
614 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
616 return info->desktop_id;
620 g_desktop_app_info_get_name (GAppInfo *appinfo)
622 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
624 if (info->name == NULL)
630 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
632 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
634 if (info->fullname == NULL)
635 return g_desktop_app_info_get_name (appinfo);
636 return info->fullname;
640 * g_desktop_app_info_get_is_hidden:
641 * @info: a #GDesktopAppInfo.
643 * A desktop file is hidden if the Hidden key in it is
646 * Returns: %TRUE if hidden, %FALSE otherwise.
649 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
655 * g_desktop_app_info_get_filename:
656 * @info: a #GDesktopAppInfo
658 * When @info was created from a known filename, return it. In some
659 * situations such as the #GDesktopAppInfo returned from
660 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
662 * Returns: The full path to the file for @info, or %NULL if not known.
666 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
668 return info->filename;
672 g_desktop_app_info_get_description (GAppInfo *appinfo)
674 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
676 return info->comment;
680 g_desktop_app_info_get_executable (GAppInfo *appinfo)
682 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
688 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
690 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
696 g_desktop_app_info_get_icon (GAppInfo *appinfo)
698 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
704 * g_desktop_app_info_get_categories:
705 * @info: a #GDesktopAppInfo
707 * Gets the categories from the desktop file.
709 * Returns: The unparsed Categories key from the desktop file;
710 * i.e. no attempt is made to split it by ';' or validate it.
713 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
715 return info->categories;
719 * g_desktop_app_info_get_keywords:
720 * @info: a #GDesktopAppInfo
722 * Gets the keywords from the desktop file.
724 * Returns: (transfer none): The value of the Keywords key
729 g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
731 return (const char * const *)info->keywords;
735 * g_desktop_app_info_get_generic_name:
736 * @info: a #GDesktopAppInfo
738 * Gets the generic name from the destkop file.
740 * Returns: The value of the GenericName key
743 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
745 return info->generic_name;
749 * g_desktop_app_info_get_nodisplay:
750 * @info: a #GDesktopAppInfo
752 * Gets the value of the NoDisplay key, which helps determine if the
753 * application info should be shown in menus. See
754 * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
756 * Returns: The value of the NoDisplay key
761 g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
763 return info->nodisplay;
767 * g_desktop_app_info_get_show_in:
768 * @info: a #GDesktopAppInfo
769 * @desktop_env: a string specifying a desktop name
771 * Checks if the application info should be shown in menus that list available
772 * applications for a specific name of the desktop, based on the
773 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys.
775 * If @desktop_env is %NULL, then the name of the desktop set with
776 * g_desktop_app_info_set_desktop_env() is used.
778 * Note that g_app_info_should_show() for @info will include this check (with
779 * %NULL for @desktop_env) as well as additional checks.
781 * Returns: %TRUE if the @info should be shown in @desktop_env according to the
782 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys, %FALSE
788 g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
789 const gchar *desktop_env)
794 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
797 G_LOCK (g_desktop_env);
798 desktop_env = g_desktop_env;
799 G_UNLOCK (g_desktop_env);
802 if (info->only_show_in)
804 if (desktop_env == NULL)
808 for (i = 0; info->only_show_in[i] != NULL; i++)
810 if (strcmp (info->only_show_in[i], desktop_env) == 0)
820 if (info->not_show_in && desktop_env)
822 for (i = 0; info->not_show_in[i] != NULL; i++)
824 if (strcmp (info->not_show_in[i], desktop_env) == 0)
833 expand_macro_single (char macro, char *uri)
839 file = g_file_new_for_uri (uri);
840 path = g_file_get_path (file);
841 g_object_unref (file);
847 result = g_shell_quote (uri);
852 result = g_shell_quote (path);
858 name = g_path_get_dirname (path);
859 result = g_shell_quote (name);
867 name = g_path_get_basename (path);
868 result = g_shell_quote (name);
880 expand_macro (char macro,
882 GDesktopAppInfo *info,
885 GList *uris = *uri_list;
887 gboolean force_file_uri;
888 char force_file_uri_macro;
891 g_return_if_fail (exec != NULL);
893 /* On %u and %U, pass POSIX file path pointing to the URI via
894 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
895 * running or the URI doesn't have a POSIX file path via FUSE
896 * we'll just pass the URI.
898 force_file_uri_macro = macro;
899 force_file_uri = FALSE;
905 force_file_uri_macro = 'f';
906 force_file_uri = TRUE;
909 force_file_uri_macro = 'F';
910 force_file_uri = TRUE;
926 if (!force_file_uri ||
927 /* Pass URI if it contains an anchor */
928 strchr (uri, '#') != NULL)
930 expanded = expand_macro_single (macro, uri);
934 expanded = expand_macro_single (force_file_uri_macro, uri);
935 if (expanded == NULL)
936 expanded = expand_macro_single (macro, uri);
941 g_string_append (exec, expanded);
957 if (!force_file_uri ||
958 /* Pass URI if it contains an anchor */
959 strchr (uri, '#') != NULL)
961 expanded = expand_macro_single (macro, uri);
965 expanded = expand_macro_single (force_file_uri_macro, uri);
966 if (expanded == NULL)
967 expanded = expand_macro_single (macro, uri);
972 g_string_append (exec, expanded);
978 if (uris != NULL && expanded)
979 g_string_append_c (exec, ' ');
987 g_string_append (exec, "--icon ");
988 expanded = g_shell_quote (info->icon_name);
989 g_string_append (exec, expanded);
997 expanded = g_shell_quote (info->name);
998 g_string_append (exec, expanded);
1006 expanded = g_shell_quote (info->filename);
1007 g_string_append (exec, expanded);
1012 case 'm': /* deprecated */
1016 g_string_append_c (exec, '%');
1024 expand_application_parameters (GDesktopAppInfo *info,
1030 GList *uri_list = *uris;
1031 const char *p = info->exec;
1032 GString *expanded_exec;
1035 if (info->exec == NULL)
1037 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1038 _("Desktop file didn't specify Exec field"));
1042 expanded_exec = g_string_new (NULL);
1046 if (p[0] == '%' && p[1] != '\0')
1048 expand_macro (p[1], expanded_exec, info, uris);
1052 g_string_append_c (expanded_exec, *p);
1057 /* No file substitutions */
1058 if (uri_list == *uris && uri_list != NULL)
1060 /* If there is no macro default to %f. This is also what KDE does */
1061 g_string_append_c (expanded_exec, ' ');
1062 expand_macro ('f', expanded_exec, info, uris);
1065 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
1066 g_string_free (expanded_exec, TRUE);
1071 prepend_terminal_to_vector (int *argc,
1078 char **term_argv = NULL;
1083 g_return_val_if_fail (argc != NULL, FALSE);
1084 g_return_val_if_fail (argv != NULL, FALSE);
1092 /* compute size if not given */
1095 for (i = 0; the_argv[i] != NULL; i++)
1101 term_argv = g_new0 (char *, 3);
1103 check = g_find_program_in_path ("gnome-terminal");
1106 term_argv[0] = check;
1107 /* Note that gnome-terminal takes -x and
1108 * as -e in gnome-terminal is broken we use that. */
1109 term_argv[1] = g_strdup ("-x");
1114 check = g_find_program_in_path ("nxterm");
1116 check = g_find_program_in_path ("color-xterm");
1118 check = g_find_program_in_path ("rxvt");
1120 check = g_find_program_in_path ("xterm");
1122 check = g_find_program_in_path ("dtterm");
1125 check = g_strdup ("xterm");
1126 g_warning ("couldn't find a terminal, falling back to xterm");
1128 term_argv[0] = check;
1129 term_argv[1] = g_strdup ("-e");
1132 real_argc = term_argc + *argc;
1133 real_argv = g_new (char *, real_argc + 1);
1135 for (i = 0; i < term_argc; i++)
1136 real_argv[i] = term_argv[i];
1138 for (j = 0; j < *argc; j++, i++)
1139 real_argv[i] = (char *)the_argv[j];
1141 real_argv[i] = NULL;
1147 /* we use g_free here as we sucked all the inner strings
1148 * out from it into real_argv */
1153 #endif /* G_OS_WIN32 */
1157 create_files_for_uris (GList *uris)
1164 for (iter = uris; iter; iter = iter->next)
1166 GFile *file = g_file_new_for_uri ((char *)iter->data);
1167 res = g_list_prepend (res, file);
1170 return g_list_reverse (res);
1175 GSpawnChildSetupFunc user_setup;
1176 gpointer user_setup_data;
1182 child_setup (gpointer user_data)
1184 ChildSetupData *data = user_data;
1186 if (data->pid_envvar)
1188 pid_t pid = getpid ();
1192 /* Write the pid into the space already reserved for it in the
1193 * environment array. We can't use sprintf because it might
1194 * malloc, so we do it by hand. It's simplest to write the pid
1195 * out backwards first, then copy it over.
1197 for (i = 0; pid; i++, pid /= 10)
1198 buf[i] = (pid % 10) + '0';
1199 for (i--; i >= 0; i--)
1200 *(data->pid_envvar++) = buf[i];
1201 *data->pid_envvar = '\0';
1204 if (data->user_setup)
1205 data->user_setup (data->user_setup_data);
1209 notify_desktop_launch (GDBusConnection *session_bus,
1210 GDesktopAppInfo *info,
1212 const char *display,
1217 GVariantBuilder uri_variant;
1218 GVariantBuilder extras_variant;
1220 const char *desktop_file_id;
1221 const char *gio_desktop_file;
1223 if (session_bus == NULL)
1226 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
1227 for (iter = uris; iter; iter = iter->next)
1228 g_variant_builder_add (&uri_variant, "s", iter->data);
1230 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
1231 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
1232 g_variant_builder_add (&extras_variant, "{sv}",
1236 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
1237 if (gio_desktop_file != NULL)
1238 g_variant_builder_add (&extras_variant, "{sv}",
1239 "origin-desktop-file",
1240 g_variant_new_bytestring (gio_desktop_file));
1241 if (g_get_prgname () != NULL)
1242 g_variant_builder_add (&extras_variant, "{sv}",
1244 g_variant_new_bytestring (g_get_prgname ()));
1245 g_variant_builder_add (&extras_variant, "{sv}",
1248 (gint64)getpid ()));
1251 desktop_file_id = info->filename;
1252 else if (info->desktop_id)
1253 desktop_file_id = info->desktop_id;
1255 desktop_file_id = "";
1257 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
1258 "org.gtk.gio.DesktopAppInfo",
1260 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
1261 g_variant_new_bytestring (desktop_file_id),
1262 display ? display : "",
1266 g_dbus_connection_send_message (session_bus,
1270 g_object_unref (msg);
1273 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
1276 _g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
1278 GAppLaunchContext *launch_context,
1279 GSpawnFlags spawn_flags,
1280 GSpawnChildSetupFunc user_setup,
1281 gpointer user_setup_data,
1282 GDesktopAppLaunchCallback pid_callback,
1283 gpointer pid_callback_data,
1286 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1287 GDBusConnection *session_bus;
1288 gboolean completed = FALSE;
1290 char **argv, **envp;
1292 ChildSetupData data;
1294 g_return_val_if_fail (appinfo != NULL, FALSE);
1298 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
1301 envp = g_app_launch_context_get_environment (launch_context);
1303 envp = g_get_environ ();
1308 GList *launched_uris;
1310 char *display, *sn_id;
1313 if (!expand_application_parameters (info, &uris,
1314 &argc, &argv, error))
1317 /* Get the subset of URIs we're launching with this process */
1318 launched_uris = NULL;
1319 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
1320 launched_uris = g_list_prepend (launched_uris, iter->data);
1321 launched_uris = g_list_reverse (launched_uris);
1323 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
1325 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1326 _("Unable to find terminal required for application"));
1330 data.user_setup = user_setup;
1331 data.user_setup_data = user_setup_data;
1335 envp = g_environ_setenv (envp,
1336 "GIO_LAUNCHED_DESKTOP_FILE",
1339 envp = g_environ_setenv (envp,
1340 "GIO_LAUNCHED_DESKTOP_FILE_PID",
1341 "XXXXXXXXXXXXXXXXXXXX", /* filled in child_setup */
1343 data.pid_envvar = (char *)g_environ_getenv (envp, "GIO_LAUNCHED_DESKTOP_FILE_PID");
1347 data.pid_envvar = NULL;
1354 GList *launched_files = create_files_for_uris (launched_uris);
1356 display = g_app_launch_context_get_display (launch_context,
1360 envp = g_environ_setenv (envp, "DISPLAY", display, TRUE);
1362 if (info->startup_notify)
1364 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
1367 envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
1370 g_list_free_full (launched_files, g_object_unref);
1373 if (!g_spawn_async (info->path,
1383 g_app_launch_context_launch_failed (launch_context, sn_id);
1387 g_list_free (launched_uris);
1392 if (pid_callback != NULL)
1393 pid_callback (info, pid, pid_callback_data);
1395 if (launch_context != NULL)
1397 GVariantBuilder builder;
1398 GVariant *platform_data;
1400 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
1401 g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (pid));
1403 g_variant_builder_add (&builder, "{sv}", "startup-notification-id", g_variant_new_string (sn_id));
1404 platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
1405 g_signal_emit_by_name (launch_context, "launched", info, platform_data);
1406 g_variant_unref (platform_data);
1409 notify_desktop_launch (session_bus,
1418 g_list_free (launched_uris);
1423 while (uris != NULL);
1425 /* TODO - need to handle the process exiting immediately
1426 * after launching an app. See http://bugzilla.gnome.org/606960
1428 if (session_bus != NULL)
1430 /* This asynchronous flush holds a reference until it completes,
1431 * which ensures that the following unref won't immediately kill
1432 * the connection if we were the initial owner.
1434 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
1440 g_clear_object (&session_bus);
1448 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
1450 GAppLaunchContext *launch_context,
1453 return _g_desktop_app_info_launch_uris_internal (appinfo, uris,
1455 _SPAWN_FLAGS_DEFAULT,
1456 NULL, NULL, NULL, NULL,
1461 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1463 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1465 return info->exec &&
1466 ((strstr (info->exec, "%u") != NULL) ||
1467 (strstr (info->exec, "%U") != NULL));
1471 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1473 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1475 return info->exec &&
1476 ((strstr (info->exec, "%f") != NULL) ||
1477 (strstr (info->exec, "%F") != NULL));
1481 g_desktop_app_info_launch (GAppInfo *appinfo,
1483 GAppLaunchContext *launch_context,
1493 uri = g_file_get_uri (files->data);
1494 uris = g_list_prepend (uris, uri);
1495 files = files->next;
1498 uris = g_list_reverse (uris);
1500 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1502 g_list_free_full (uris, g_free);
1508 * g_desktop_app_info_launch_uris_as_manager:
1509 * @appinfo: a #GDesktopAppInfo
1510 * @uris: (element-type utf8): List of URIs
1511 * @launch_context: a #GAppLaunchContext
1512 * @spawn_flags: #GSpawnFlags, used for each process
1513 * @user_setup: (scope call): a #GSpawnChildSetupFunc, used once for
1515 * @user_setup_data: (closure user_setup): User data for @user_setup
1516 * @pid_callback: (scope call): Callback for child processes
1517 * @pid_callback_data: (closure pid_callback): User data for @callback
1518 * @error: return location for a #GError, or %NULL
1520 * This function performs the equivalent of g_app_info_launch_uris(),
1521 * but is intended primarily for operating system components that
1522 * launch applications. Ordinary applications should use
1523 * g_app_info_launch_uris().
1525 * In contrast to g_app_info_launch_uris(), all processes created will
1526 * always be run directly as children as if by the UNIX fork()/exec()
1529 * This guarantee allows additional control over the exact environment
1530 * of the child processes, which is provided via a setup function
1531 * @user_setup, as well as the process identifier of each child process
1532 * via @pid_callback. See g_spawn_async() for more information about the
1533 * semantics of the @user_setup function.
1535 * Returns: %TRUE on successful launch, %FALSE otherwise.
1538 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
1540 GAppLaunchContext *launch_context,
1541 GSpawnFlags spawn_flags,
1542 GSpawnChildSetupFunc user_setup,
1543 gpointer user_setup_data,
1544 GDesktopAppLaunchCallback pid_callback,
1545 gpointer pid_callback_data,
1548 return _g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
1560 * g_desktop_app_info_set_desktop_env:
1561 * @desktop_env: a string specifying what desktop this is
1563 * Sets the name of the desktop that the application is running in.
1564 * This is used by g_app_info_should_show() and
1565 * g_desktop_app_info_get_show_in() to evaluate the
1566 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1567 * desktop entry fields.
1569 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1570 * Menu specification</ulink> recognizes the following:
1572 * <member>GNOME</member>
1573 * <member>KDE</member>
1574 * <member>ROX</member>
1575 * <member>XFCE</member>
1576 * <member>LXDE</member>
1577 * <member>Unity</member>
1578 * <member>Old</member>
1581 * Should be called only once; subsequent calls are ignored.
1584 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1586 G_LOCK (g_desktop_env);
1588 g_desktop_env = g_strdup (desktop_env);
1589 G_UNLOCK (g_desktop_env);
1593 g_desktop_app_info_should_show (GAppInfo *appinfo)
1595 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1597 if (info->nodisplay)
1600 return g_desktop_app_info_get_show_in (info, NULL);
1609 ensure_dir (DirType type,
1612 char *path, *display_name;
1615 if (type == APP_DIR)
1616 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1618 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1621 if (g_mkdir_with_parents (path, 0700) == 0)
1625 display_name = g_filename_display_name (path);
1626 if (type == APP_DIR)
1627 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1628 _("Can't create user application configuration folder %s: %s"),
1629 display_name, g_strerror (errsv));
1631 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1632 _("Can't create user MIME configuration folder %s: %s"),
1633 display_name, g_strerror (errsv));
1635 g_free (display_name);
1642 update_mimeapps_list (const char *desktop_id,
1643 const char *content_type,
1644 UpdateMimeFlags flags,
1647 char *dirname, *filename, *string;
1649 gboolean load_succeeded, res;
1650 char **old_list, **list;
1651 gsize length, data_size;
1654 char **content_types;
1656 /* Don't add both at start and end */
1657 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
1658 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
1660 dirname = ensure_dir (APP_DIR, error);
1664 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1667 key_file = g_key_file_new ();
1668 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1669 if (!load_succeeded ||
1670 (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) &&
1671 !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) &&
1672 !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP)))
1674 g_key_file_free (key_file);
1675 key_file = g_key_file_new ();
1680 content_types = g_new (char *, 2);
1681 content_types[0] = g_strdup (content_type);
1682 content_types[1] = NULL;
1686 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
1689 for (k = 0; content_types && content_types[k]; k++)
1691 /* set as default, if requested so */
1692 string = g_key_file_get_string (key_file,
1693 DEFAULT_APPLICATIONS_GROUP,
1697 if (g_strcmp0 (string, desktop_id) != 0 &&
1698 (flags & UPDATE_MIME_SET_DEFAULT))
1701 string = g_strdup (desktop_id);
1703 /* add in the non-default list too, if it's not already there */
1704 flags |= UPDATE_MIME_SET_NON_DEFAULT;
1707 if (string == NULL || desktop_id == NULL)
1708 g_key_file_remove_key (key_file,
1709 DEFAULT_APPLICATIONS_GROUP,
1713 g_key_file_set_string (key_file,
1714 DEFAULT_APPLICATIONS_GROUP,
1723 /* reuse the list from above */
1727 g_strfreev (content_types);
1728 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1731 for (k = 0; content_types && content_types[k]; k++)
1733 /* Add to the right place in the list */
1736 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1737 content_types[k], &length, NULL);
1739 list = g_new (char *, 1 + length + 1);
1743 /* if we're adding a last-used hint, just put the application in front of the list */
1744 if (flags & UPDATE_MIME_SET_LAST_USED)
1746 /* avoid adding this again as non-default later */
1747 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1748 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1750 list[i++] = g_strdup (desktop_id);
1755 for (j = 0; old_list[j] != NULL; j++)
1757 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1759 /* rewrite other entries if they're different from the new one */
1760 list[i++] = g_strdup (old_list[j]);
1762 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1764 /* we encountered an old entry which is equal to the one we're adding as non-default,
1765 * don't change its position in the list.
1767 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1768 list[i++] = g_strdup (old_list[j]);
1773 /* add it at the end of the list */
1774 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1775 list[i++] = g_strdup (desktop_id);
1779 g_strfreev (old_list);
1781 if (list[0] == NULL || desktop_id == NULL)
1782 g_key_file_remove_key (key_file,
1783 ADDED_ASSOCIATIONS_GROUP,
1787 g_key_file_set_string_list (key_file,
1788 ADDED_ASSOCIATIONS_GROUP,
1790 (const char * const *)list, i);
1797 /* reuse the list from above */
1801 g_strfreev (content_types);
1802 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1805 for (k = 0; content_types && content_types[k]; k++)
1807 /* Remove from removed associations group (unless remove) */
1810 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1811 content_types[k], &length, NULL);
1813 list = g_new (char *, 1 + length + 1);
1816 if (flags & UPDATE_MIME_REMOVE)
1817 list[i++] = g_strdup (desktop_id);
1820 for (j = 0; old_list[j] != NULL; j++)
1822 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1823 list[i++] = g_strdup (old_list[j]);
1828 g_strfreev (old_list);
1830 if (list[0] == NULL || desktop_id == NULL)
1831 g_key_file_remove_key (key_file,
1832 REMOVED_ASSOCIATIONS_GROUP,
1836 g_key_file_set_string_list (key_file,
1837 REMOVED_ASSOCIATIONS_GROUP,
1839 (const char * const *)list, i);
1844 g_strfreev (content_types);
1846 data = g_key_file_to_data (key_file, &data_size, error);
1847 g_key_file_free (key_file);
1849 res = g_file_set_contents (filename, data, data_size, error);
1851 mime_info_cache_reload (NULL);
1860 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
1861 const char *content_type,
1864 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1866 if (!g_desktop_app_info_ensure_saved (info, error))
1869 if (!info->desktop_id)
1871 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1872 _("Application information lacks an identifier"));
1876 /* both add support for the content type and set as last used */
1877 return update_mimeapps_list (info->desktop_id, content_type,
1878 UPDATE_MIME_SET_NON_DEFAULT |
1879 UPDATE_MIME_SET_LAST_USED,
1884 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1885 const char *content_type,
1888 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1890 if (!g_desktop_app_info_ensure_saved (info, error))
1893 if (!info->desktop_id)
1895 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1896 _("Application information lacks an identifier"));
1900 return update_mimeapps_list (info->desktop_id, content_type,
1901 UPDATE_MIME_SET_DEFAULT,
1906 update_program_done (GPid pid,
1910 /* Did the application exit correctly */
1911 if (g_spawn_check_exit_status (status, NULL))
1913 /* Here we could clean out any caches in use */
1918 run_update_command (char *command,
1927 GError *error = NULL;
1930 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1932 if (g_spawn_async ("/", argv,
1934 G_SPAWN_SEARCH_PATH |
1935 G_SPAWN_STDOUT_TO_DEV_NULL |
1936 G_SPAWN_STDERR_TO_DEV_NULL |
1937 G_SPAWN_DO_NOT_REAP_CHILD,
1938 NULL, NULL, /* No setup function */
1941 g_child_watch_add (pid, update_program_done, NULL);
1944 /* If we get an error at this point, it's quite likely the user doesn't
1945 * have an installed copy of either 'update-mime-database' or
1946 * 'update-desktop-database'. I don't think we want to popup an error
1947 * dialog at this point, so we just do a g_warning to give the user a
1948 * chance of debugging it.
1950 g_warning ("%s", error->message);
1957 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1958 const char *extension,
1961 char *filename, *basename, *mimetype;
1965 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1968 dirname = ensure_dir (MIMETYPE_DIR, error);
1972 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1973 filename = g_build_filename (dirname, basename, NULL);
1977 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1979 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1984 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1985 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1986 " <mime-type type=\"%s\">\n"
1987 " <comment>%s document</comment>\n"
1988 " <glob pattern=\"*.%s\"/>\n"
1990 "</mime-info>\n", mimetype, extension, extension);
1992 g_file_set_contents (filename, contents, -1, NULL);
1995 run_update_command ("update-mime-database", "mime");
1999 res = g_desktop_app_info_set_as_default_for_type (appinfo,
2009 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
2010 const char *content_type,
2013 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2015 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
2018 return update_mimeapps_list (info->desktop_id, content_type,
2019 UPDATE_MIME_SET_NON_DEFAULT,
2024 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
2030 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
2031 const char *content_type,
2034 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2036 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
2039 return update_mimeapps_list (info->desktop_id, content_type,
2044 static const char **
2045 g_desktop_app_info_get_supported_types (GAppInfo *appinfo)
2047 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2049 return (const char**) info->mime_types;
2054 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
2060 char *data, *desktop_id;
2065 if (info->filename != NULL)
2068 /* This is only used for object created with
2069 * g_app_info_create_from_commandline. All other
2070 * object should have a filename
2073 dirname = ensure_dir (APP_DIR, error);
2077 key_file = g_key_file_new ();
2079 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2080 "Encoding", "UTF-8");
2081 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2082 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
2083 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2084 G_KEY_FILE_DESKTOP_KEY_TYPE,
2085 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
2087 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2088 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
2089 if (info->nodisplay)
2090 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2091 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2093 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2094 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
2096 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2097 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
2099 if (info->generic_name != NULL)
2100 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2101 GENERIC_NAME_KEY, info->generic_name);
2103 if (info->fullname != NULL)
2104 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2105 FULL_NAME_KEY, info->fullname);
2107 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2108 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
2110 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2111 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2113 data = g_key_file_to_data (key_file, &data_size, NULL);
2114 g_key_file_free (key_file);
2116 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
2117 filename = g_build_filename (dirname, desktop_id, NULL);
2118 g_free (desktop_id);
2121 fd = g_mkstemp (filename);
2126 display_name = g_filename_display_name (filename);
2127 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2128 _("Can't create user desktop file %s"), display_name);
2129 g_free (display_name);
2135 desktop_id = g_path_get_basename (filename);
2137 /* FIXME - actually handle error */
2138 (void) g_close (fd, NULL);
2140 res = g_file_set_contents (filename, data, data_size, error);
2144 g_free (desktop_id);
2149 info->filename = filename;
2150 info->desktop_id = desktop_id;
2152 run_update_command ("update-desktop-database", "applications");
2158 g_desktop_app_info_can_delete (GAppInfo *appinfo)
2160 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2164 if (strstr (info->filename, "/userapp-"))
2165 return g_access (info->filename, W_OK) == 0;
2172 g_desktop_app_info_delete (GAppInfo *appinfo)
2174 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2178 if (g_remove (info->filename) == 0)
2180 update_mimeapps_list (info->desktop_id, NULL,
2184 g_free (info->filename);
2185 info->filename = NULL;
2186 g_free (info->desktop_id);
2187 info->desktop_id = NULL;
2197 * g_app_info_create_from_commandline:
2198 * @commandline: the commandline to use
2199 * @application_name: (allow-none): the application name, or %NULL to use @commandline
2200 * @flags: flags that can specify details of the created #GAppInfo
2201 * @error: a #GError location to store the error occurring, %NULL to ignore.
2203 * Creates a new #GAppInfo from the given information.
2205 * Note that for @commandline, the quoting rules of the Exec key of the
2206 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">freedesktop.org Desktop
2207 * Entry Specification</ulink> are applied. For example, if the @commandline contains
2208 * percent-encoded URIs, the percent-character must be doubled in order to prevent it from
2209 * being swallowed by Exec key unquoting. See the specification for exact quoting rules.
2211 * Returns: (transfer full): new #GAppInfo for given command.
2214 g_app_info_create_from_commandline (const char *commandline,
2215 const char *application_name,
2216 GAppInfoCreateFlags flags,
2221 GDesktopAppInfo *info;
2223 g_return_val_if_fail (commandline, NULL);
2225 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
2227 info->filename = NULL;
2228 info->desktop_id = NULL;
2230 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
2231 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
2232 info->hidden = FALSE;
2233 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
2234 info->exec = g_strconcat (commandline, " %u", NULL);
2236 info->exec = g_strconcat (commandline, " %f", NULL);
2237 info->nodisplay = TRUE;
2238 info->binary = binary_from_exec (info->exec);
2240 if (application_name)
2241 info->name = g_strdup (application_name);
2244 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
2245 split = g_strsplit (commandline, " ", 2);
2246 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
2248 info->name = basename;
2249 if (info->name == NULL)
2250 info->name = g_strdup ("custom");
2252 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
2254 return G_APP_INFO (info);
2258 g_desktop_app_info_iface_init (GAppInfoIface *iface)
2260 iface->dup = g_desktop_app_info_dup;
2261 iface->equal = g_desktop_app_info_equal;
2262 iface->get_id = g_desktop_app_info_get_id;
2263 iface->get_name = g_desktop_app_info_get_name;
2264 iface->get_description = g_desktop_app_info_get_description;
2265 iface->get_executable = g_desktop_app_info_get_executable;
2266 iface->get_icon = g_desktop_app_info_get_icon;
2267 iface->launch = g_desktop_app_info_launch;
2268 iface->supports_uris = g_desktop_app_info_supports_uris;
2269 iface->supports_files = g_desktop_app_info_supports_files;
2270 iface->launch_uris = g_desktop_app_info_launch_uris;
2271 iface->should_show = g_desktop_app_info_should_show;
2272 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
2273 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
2274 iface->add_supports_type = g_desktop_app_info_add_supports_type;
2275 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
2276 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
2277 iface->can_delete = g_desktop_app_info_can_delete;
2278 iface->do_delete = g_desktop_app_info_delete;
2279 iface->get_commandline = g_desktop_app_info_get_commandline;
2280 iface->get_display_name = g_desktop_app_info_get_display_name;
2281 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
2282 iface->get_supported_types = g_desktop_app_info_get_supported_types;
2286 app_info_in_list (GAppInfo *info,
2289 while (list != NULL)
2291 if (g_app_info_equal (info, list->data))
2299 * g_app_info_get_recommended_for_type:
2300 * @content_type: the content type to find a #GAppInfo for
2302 * Gets a list of recommended #GAppInfos for a given content type, i.e.
2303 * those applications which claim to support the given content type exactly,
2304 * and not by MIME type subclassing.
2305 * Note that the first application of the list is the last used one, i.e.
2306 * the last one for which g_app_info_set_as_last_used_for_type() has been
2309 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2310 * for given @content_type or %NULL on error.
2315 g_app_info_get_recommended_for_type (const gchar *content_type)
2317 GList *desktop_entries, *l;
2319 GDesktopAppInfo *info;
2321 g_return_val_if_fail (content_type != NULL, NULL);
2323 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
2326 for (l = desktop_entries; l != NULL; l = l->next)
2328 char *desktop_entry = l->data;
2330 info = g_desktop_app_info_new (desktop_entry);
2333 if (app_info_in_list (G_APP_INFO (info), infos))
2334 g_object_unref (info);
2336 infos = g_list_prepend (infos, info);
2338 g_free (desktop_entry);
2341 g_list_free (desktop_entries);
2343 return g_list_reverse (infos);
2347 * g_app_info_get_fallback_for_type:
2348 * @content_type: the content type to find a #GAppInfo for
2350 * Gets a list of fallback #GAppInfos for a given content type, i.e.
2351 * those applications which claim to support the given content type
2352 * by MIME type subclassing and not directly.
2354 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2355 * for given @content_type or %NULL on error.
2360 g_app_info_get_fallback_for_type (const gchar *content_type)
2362 GList *desktop_entries, *l;
2363 GList *infos, *recommended_infos;
2364 GDesktopAppInfo *info;
2366 g_return_val_if_fail (content_type != NULL, NULL);
2368 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
2369 recommended_infos = g_app_info_get_recommended_for_type (content_type);
2372 for (l = desktop_entries; l != NULL; l = l->next)
2374 char *desktop_entry = l->data;
2376 info = g_desktop_app_info_new (desktop_entry);
2379 if (app_info_in_list (G_APP_INFO (info), infos) ||
2380 app_info_in_list (G_APP_INFO (info), recommended_infos))
2381 g_object_unref (info);
2383 infos = g_list_prepend (infos, info);
2385 g_free (desktop_entry);
2388 g_list_free (desktop_entries);
2389 g_list_free_full (recommended_infos, g_object_unref);
2391 return g_list_reverse (infos);
2395 * g_app_info_get_all_for_type:
2396 * @content_type: the content type to find a #GAppInfo for
2398 * Gets a list of all #GAppInfos for a given content type,
2399 * including the recommended and fallback #GAppInfos. See
2400 * g_app_info_get_recommended_for_type() and
2401 * g_app_info_get_fallback_for_type().
2403 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2404 * for given @content_type or %NULL on error.
2407 g_app_info_get_all_for_type (const char *content_type)
2409 GList *desktop_entries, *l;
2411 char *user_default = NULL;
2412 GDesktopAppInfo *info;
2414 g_return_val_if_fail (content_type != NULL, NULL);
2416 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2419 /* put the user default in front of the list, for compatibility */
2420 if (user_default != NULL)
2422 info = g_desktop_app_info_new (user_default);
2425 infos = g_list_prepend (infos, info);
2428 g_free (user_default);
2430 for (l = desktop_entries; l != NULL; l = l->next)
2432 char *desktop_entry = l->data;
2434 info = g_desktop_app_info_new (desktop_entry);
2437 if (app_info_in_list (G_APP_INFO (info), infos))
2438 g_object_unref (info);
2440 infos = g_list_prepend (infos, info);
2442 g_free (desktop_entry);
2445 g_list_free (desktop_entries);
2447 return g_list_reverse (infos);
2451 * g_app_info_reset_type_associations:
2452 * @content_type: a content type
2454 * Removes all changes to the type associations done by
2455 * g_app_info_set_as_default_for_type(),
2456 * g_app_info_set_as_default_for_extension(),
2457 * g_app_info_add_supports_type() or
2458 * g_app_info_remove_supports_type().
2463 g_app_info_reset_type_associations (const char *content_type)
2465 update_mimeapps_list (NULL, content_type,
2471 * g_app_info_get_default_for_type:
2472 * @content_type: the content type to find a #GAppInfo for
2473 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
2476 * Gets the default #GAppInfo for a given content type.
2478 * Returns: (transfer full): #GAppInfo for given @content_type or
2482 g_app_info_get_default_for_type (const char *content_type,
2483 gboolean must_support_uris)
2485 GList *desktop_entries, *l;
2486 char *user_default = NULL;
2489 g_return_val_if_fail (content_type != NULL, NULL);
2491 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2495 if (user_default != NULL)
2497 info = (GAppInfo *) g_desktop_app_info_new (user_default);
2501 if (must_support_uris && !g_app_info_supports_uris (info))
2503 g_object_unref (info);
2509 g_free (user_default);
2513 g_list_free_full (desktop_entries, g_free);
2517 /* pick the first from the other list that matches our URI
2520 for (l = desktop_entries; l != NULL; l = l->next)
2522 char *desktop_entry = l->data;
2524 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
2527 if (must_support_uris && !g_app_info_supports_uris (info))
2529 g_object_unref (info);
2537 g_list_free_full (desktop_entries, g_free);
2543 * g_app_info_get_default_for_uri_scheme:
2544 * @uri_scheme: a string containing a URI scheme.
2546 * Gets the default application for handling URIs with
2547 * the given URI scheme. A URI scheme is the initial part
2548 * of the URI, up to but not including the ':', e.g. "http",
2551 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
2554 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
2557 char *content_type, *scheme_down;
2559 scheme_down = g_ascii_strdown (uri_scheme, -1);
2560 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
2561 g_free (scheme_down);
2562 app_info = g_app_info_get_default_for_type (content_type, FALSE);
2563 g_free (content_type);
2569 get_apps_from_dir (GHashTable *apps,
2570 const char *dirname,
2574 const char *basename;
2575 char *filename, *subprefix, *desktop_id;
2577 GDesktopAppInfo *appinfo;
2579 dir = g_dir_open (dirname, 0, NULL);
2582 while ((basename = g_dir_read_name (dir)) != NULL)
2584 filename = g_build_filename (dirname, basename, NULL);
2585 if (g_str_has_suffix (basename, ".desktop"))
2587 desktop_id = g_strconcat (prefix, basename, NULL);
2589 /* Use _extended so we catch NULLs too (hidden) */
2590 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
2592 appinfo = g_desktop_app_info_new_from_filename (filename);
2595 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
2597 g_object_unref (appinfo);
2602 if (appinfo || hidden)
2604 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
2608 /* Reuse instead of strdup here */
2609 appinfo->desktop_id = desktop_id;
2614 g_free (desktop_id);
2618 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
2620 subprefix = g_strconcat (prefix, basename, "-", NULL);
2621 get_apps_from_dir (apps, filename, subprefix);
2633 * g_app_info_get_all:
2635 * Gets a list of all of the applications currently registered
2638 * For desktop files, this includes applications that have
2639 * <literal>NoDisplay=true</literal> set or are excluded from
2640 * display by means of <literal>OnlyShowIn</literal> or
2641 * <literal>NotShowIn</literal>. See g_app_info_should_show().
2642 * The returned list does not include applications which have
2643 * the <literal>Hidden</literal> key set.
2645 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
2648 g_app_info_get_all (void)
2650 const char * const *dirs;
2652 GHashTableIter iter;
2657 dirs = get_applications_search_path ();
2659 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
2663 for (i = 0; dirs[i] != NULL; i++)
2664 get_apps_from_dir (apps, dirs[i], "");
2668 g_hash_table_iter_init (&iter, apps);
2669 while (g_hash_table_iter_next (&iter, NULL, &value))
2672 infos = g_list_prepend (infos, value);
2675 g_hash_table_destroy (apps);
2677 return g_list_reverse (infos);
2680 /* Cacheing of mimeinfo.cache and defaults.list files */
2684 GHashTable *mime_info_cache_map;
2685 GHashTable *defaults_list_map;
2686 GHashTable *mimeapps_list_added_map;
2687 GHashTable *mimeapps_list_removed_map;
2688 GHashTable *mimeapps_list_defaults_map;
2689 time_t mime_info_cache_timestamp;
2690 time_t defaults_list_timestamp;
2691 time_t mimeapps_list_timestamp;
2695 GList *dirs; /* mimeinfo.cache and defaults.list */
2696 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
2697 time_t last_stat_time;
2698 guint should_ping_mime_monitor : 1;
2701 static MimeInfoCache *mime_info_cache = NULL;
2702 G_LOCK_DEFINE_STATIC (mime_info_cache);
2704 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2705 const char *mime_type,
2706 char **new_desktop_file_ids);
2708 static MimeInfoCache * mime_info_cache_new (void);
2711 destroy_info_cache_value (gpointer key,
2715 g_list_free_full (value, g_free);
2719 destroy_info_cache_map (GHashTable *info_cache_map)
2721 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
2722 g_hash_table_destroy (info_cache_map);
2726 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2727 const char *cache_file,
2733 filename = g_build_filename (dir->path, cache_file, NULL);
2735 if (g_stat (filename, &buf) < 0)
2742 if (buf.st_mtime != *timestamp)
2748 /* Call with lock held */
2750 remove_all (gpointer key,
2759 mime_info_cache_blow_global_cache (void)
2761 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
2766 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2770 gchar *filename, **mime_types;
2777 if (dir->mime_info_cache_map != NULL &&
2778 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2779 &dir->mime_info_cache_timestamp))
2782 if (dir->mime_info_cache_map != NULL)
2783 destroy_info_cache_map (dir->mime_info_cache_map);
2785 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2786 (GDestroyNotify) g_free,
2789 key_file = g_key_file_new ();
2791 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2793 if (g_stat (filename, &buf) < 0)
2796 if (dir->mime_info_cache_timestamp > 0)
2797 mime_info_cache->should_ping_mime_monitor = TRUE;
2799 dir->mime_info_cache_timestamp = buf.st_mtime;
2801 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2806 if (load_error != NULL)
2809 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2812 if (load_error != NULL)
2815 for (i = 0; mime_types[i] != NULL; i++)
2817 gchar **desktop_file_ids;
2818 char *unaliased_type;
2819 desktop_file_ids = g_key_file_get_string_list (key_file,
2825 if (desktop_file_ids == NULL)
2828 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2829 mime_info_cache_dir_add_desktop_entries (dir,
2832 g_free (unaliased_type);
2834 g_strfreev (desktop_file_ids);
2837 g_strfreev (mime_types);
2838 g_key_file_free (key_file);
2843 g_key_file_free (key_file);
2845 if (mime_types != NULL)
2846 g_strfreev (mime_types);
2849 g_error_free (load_error);
2853 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2857 gchar *filename, **mime_types;
2858 char *unaliased_type;
2859 char **desktop_file_ids;
2866 if (dir->defaults_list_map != NULL &&
2867 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2868 &dir->defaults_list_timestamp))
2871 if (dir->defaults_list_map != NULL)
2872 g_hash_table_destroy (dir->defaults_list_map);
2873 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2874 g_free, (GDestroyNotify)g_strfreev);
2877 key_file = g_key_file_new ();
2879 filename = g_build_filename (dir->path, "defaults.list", NULL);
2880 if (g_stat (filename, &buf) < 0)
2883 if (dir->defaults_list_timestamp > 0)
2884 mime_info_cache->should_ping_mime_monitor = TRUE;
2886 dir->defaults_list_timestamp = buf.st_mtime;
2888 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2892 if (load_error != NULL)
2895 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2897 if (mime_types != NULL)
2899 for (i = 0; mime_types[i] != NULL; i++)
2901 desktop_file_ids = g_key_file_get_string_list (key_file,
2902 DEFAULT_APPLICATIONS_GROUP,
2906 if (desktop_file_ids == NULL)
2909 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2910 g_hash_table_replace (dir->defaults_list_map,
2915 g_strfreev (mime_types);
2918 g_key_file_free (key_file);
2923 g_key_file_free (key_file);
2925 if (mime_types != NULL)
2926 g_strfreev (mime_types);
2929 g_error_free (load_error);
2933 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2937 gchar *filename, **mime_types;
2938 char *unaliased_type;
2939 char **desktop_file_ids;
2947 if (dir->mimeapps_list_added_map != NULL &&
2948 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2949 &dir->mimeapps_list_timestamp))
2952 if (dir->mimeapps_list_added_map != NULL)
2953 g_hash_table_destroy (dir->mimeapps_list_added_map);
2954 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2955 g_free, (GDestroyNotify)g_strfreev);
2957 if (dir->mimeapps_list_removed_map != NULL)
2958 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2959 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2960 g_free, (GDestroyNotify)g_strfreev);
2962 if (dir->mimeapps_list_defaults_map != NULL)
2963 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2964 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2967 key_file = g_key_file_new ();
2969 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2970 if (g_stat (filename, &buf) < 0)
2973 if (dir->mimeapps_list_timestamp > 0)
2974 mime_info_cache->should_ping_mime_monitor = TRUE;
2976 dir->mimeapps_list_timestamp = buf.st_mtime;
2978 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2982 if (load_error != NULL)
2985 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2987 if (mime_types != NULL)
2989 for (i = 0; mime_types[i] != NULL; i++)
2991 desktop_file_ids = g_key_file_get_string_list (key_file,
2992 ADDED_ASSOCIATIONS_GROUP,
2996 if (desktop_file_ids == NULL)
2999 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3000 g_hash_table_replace (dir->mimeapps_list_added_map,
3005 g_strfreev (mime_types);
3008 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
3010 if (mime_types != NULL)
3012 for (i = 0; mime_types[i] != NULL; i++)
3014 desktop_file_ids = g_key_file_get_string_list (key_file,
3015 REMOVED_ASSOCIATIONS_GROUP,
3019 if (desktop_file_ids == NULL)
3022 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3023 g_hash_table_replace (dir->mimeapps_list_removed_map,
3028 g_strfreev (mime_types);
3031 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
3033 if (mime_types != NULL)
3035 for (i = 0; mime_types[i] != NULL; i++)
3037 desktop_id = g_key_file_get_string (key_file,
3038 DEFAULT_APPLICATIONS_GROUP,
3041 if (desktop_id == NULL)
3044 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
3045 g_hash_table_replace (dir->mimeapps_list_defaults_map,
3050 g_strfreev (mime_types);
3053 g_key_file_free (key_file);
3058 g_key_file_free (key_file);
3060 if (mime_types != NULL)
3061 g_strfreev (mime_types);
3064 g_error_free (load_error);
3067 static MimeInfoCacheDir *
3068 mime_info_cache_dir_new (const char *path)
3070 MimeInfoCacheDir *dir;
3072 dir = g_new0 (MimeInfoCacheDir, 1);
3073 dir->path = g_strdup (path);
3079 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
3084 if (dir->mime_info_cache_map != NULL)
3086 destroy_info_cache_map (dir->mime_info_cache_map);
3087 dir->mime_info_cache_map = NULL;
3091 if (dir->defaults_list_map != NULL)
3093 g_hash_table_destroy (dir->defaults_list_map);
3094 dir->defaults_list_map = NULL;
3097 if (dir->mimeapps_list_added_map != NULL)
3099 g_hash_table_destroy (dir->mimeapps_list_added_map);
3100 dir->mimeapps_list_added_map = NULL;
3103 if (dir->mimeapps_list_removed_map != NULL)
3105 g_hash_table_destroy (dir->mimeapps_list_removed_map);
3106 dir->mimeapps_list_removed_map = NULL;
3109 if (dir->mimeapps_list_defaults_map != NULL)
3111 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
3112 dir->mimeapps_list_defaults_map = NULL;
3119 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
3120 const char *mime_type,
3121 char **new_desktop_file_ids)
3123 GList *desktop_file_ids;
3126 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
3129 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
3131 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
3132 desktop_file_ids = g_list_append (desktop_file_ids,
3133 g_strdup (new_desktop_file_ids[i]));
3136 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
3140 mime_info_cache_init_dir_lists (void)
3142 const char * const *dirs;
3145 mime_info_cache = mime_info_cache_new ();
3147 dirs = get_applications_search_path ();
3149 for (i = 0; dirs[i] != NULL; i++)
3151 MimeInfoCacheDir *dir;
3153 dir = mime_info_cache_dir_new (dirs[i]);
3157 mime_info_cache_dir_init (dir);
3158 mime_info_cache_dir_init_defaults_list (dir);
3159 mime_info_cache_dir_init_mimeapps_list (dir);
3161 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
3167 mime_info_cache_update_dir_lists (void)
3171 tmp = mime_info_cache->dirs;
3175 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
3177 /* No need to do this if we had file monitors... */
3178 mime_info_cache_blow_global_cache ();
3179 mime_info_cache_dir_init (dir);
3180 mime_info_cache_dir_init_defaults_list (dir);
3181 mime_info_cache_dir_init_mimeapps_list (dir);
3188 mime_info_cache_init (void)
3190 G_LOCK (mime_info_cache);
3191 if (mime_info_cache == NULL)
3192 mime_info_cache_init_dir_lists ();
3198 if (now >= mime_info_cache->last_stat_time + 10)
3200 mime_info_cache_update_dir_lists ();
3201 mime_info_cache->last_stat_time = now;
3205 if (mime_info_cache->should_ping_mime_monitor)
3207 /* g_idle_add (emit_mime_changed, NULL); */
3208 mime_info_cache->should_ping_mime_monitor = FALSE;
3211 G_UNLOCK (mime_info_cache);
3214 static MimeInfoCache *
3215 mime_info_cache_new (void)
3217 MimeInfoCache *cache;
3219 cache = g_new0 (MimeInfoCache, 1);
3221 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
3222 (GDestroyNotify) g_free,
3223 (GDestroyNotify) g_free);
3228 mime_info_cache_free (MimeInfoCache *cache)
3233 g_list_free_full (cache->dirs, (GDestroyNotify) mime_info_cache_dir_free);
3234 g_hash_table_destroy (cache->global_defaults_cache);
3239 * mime_info_cache_reload:
3240 * @dir: directory path which needs reloading.
3242 * Reload the mime information for the @dir.
3245 mime_info_cache_reload (const char *dir)
3247 /* FIXME: just reload the dir that needs reloading,
3248 * don't blow the whole cache
3250 if (mime_info_cache != NULL)
3252 G_LOCK (mime_info_cache);
3253 mime_info_cache_free (mime_info_cache);
3254 mime_info_cache = NULL;
3255 G_UNLOCK (mime_info_cache);
3260 append_desktop_entry (GList *list,
3261 const char *desktop_entry,
3262 GList *removed_entries)
3264 /* Add if not already in list, and valid */
3265 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
3266 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
3267 list = g_list_prepend (list, g_strdup (desktop_entry));
3273 * get_all_desktop_entries_for_mime_type:
3274 * @mime_type: a mime type.
3275 * @except: NULL or a strv list
3277 * Returns all the desktop ids for @mime_type. The desktop files
3278 * are listed in an order so that default applications are listed before
3279 * non-default ones, and handlers for inherited mimetypes are listed
3280 * after the base ones.
3282 * Optionally doesn't list the desktop ids given in the @except
3284 * Return value: a #GList containing the desktop ids which claim
3285 * to handle @mime_type.
3288 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
3289 const char **except,
3290 gboolean include_fallback,
3291 char **explicit_default)
3293 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
3294 MimeInfoCacheDir *dir;
3295 char *mime_type, *default_entry = NULL;
3296 char *old_default_entry = NULL;
3299 char **default_entries;
3300 char **removed_associations;
3301 gboolean already_found_handler;
3306 mime_info_cache_init ();
3308 if (include_fallback)
3310 /* collect all ancestors */
3311 mime_types = _g_unix_content_type_get_parents (base_mime_type);
3312 array = g_ptr_array_new ();
3313 for (i = 0; mime_types[i]; i++)
3314 g_ptr_array_add (array, mime_types[i]);
3315 g_free (mime_types);
3316 for (i = 0; i < array->len; i++)
3318 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
3319 for (j = 0; anc[j]; j++)
3321 for (k = 0; k < array->len; k++)
3323 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
3326 if (k == array->len) /* not found */
3327 g_ptr_array_add (array, g_strdup (anc[j]));
3331 g_ptr_array_add (array, NULL);
3332 mime_types = (char **)g_ptr_array_free (array, FALSE);
3336 mime_types = g_malloc0 (2 * sizeof (gchar *));
3337 mime_types[0] = g_strdup (base_mime_type);
3338 mime_types[1] = NULL;
3341 G_LOCK (mime_info_cache);
3343 removed_entries = NULL;
3344 desktop_entries = NULL;
3346 for (i = 0; except != NULL && except[i] != NULL; i++)
3347 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
3349 for (i = 0; mime_types[i] != NULL; i++)
3351 mime_type = mime_types[i];
3353 /* This is true if we already found a handler for a more specific
3354 mimetype. If its set we ignore any defaults for the less specific
3356 already_found_handler = (desktop_entries != NULL);
3358 /* Go through all apps listed in user and system dirs */
3359 for (dir_list = mime_info_cache->dirs;
3361 dir_list = dir_list->next)
3363 dir = dir_list->data;
3365 /* Pick the explicit default application if we got no result earlier
3366 * (ie, for more specific mime types)
3368 if (!already_found_handler)
3370 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
3374 /* Save the default entry if it's the first one we encounter */
3375 if (default_entry == NULL)
3376 default_entry = g_strdup (entry);
3380 /* Then added associations from mimeapps.list */
3381 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
3382 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3383 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3385 /* Then removed associations from mimeapps.list */
3386 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
3387 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
3388 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
3390 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
3391 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
3392 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3394 if (default_entry == NULL && old_default_entry == NULL && !already_found_handler)
3395 old_default_entry = g_strdup (default_entries[j]);
3397 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3401 /* Go through all entries that support the mimetype */
3402 for (dir_list = mime_info_cache->dirs;
3404 dir_list = dir_list->next)
3406 dir = dir_list->data;
3408 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
3409 for (tmp = list; tmp != NULL; tmp = tmp->next)
3410 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
3414 G_UNLOCK (mime_info_cache);
3416 g_strfreev (mime_types);
3418 /* If we have no default from mimeapps.list, take it from
3419 * defaults.list intead.
3421 * If we do have a default from mimeapps.list, free any one that came
3422 * from defaults.list.
3424 if (default_entry == NULL)
3425 default_entry = old_default_entry;
3427 g_free (old_default_entry);
3429 if (explicit_default != NULL)
3430 *explicit_default = default_entry;
3432 g_free (default_entry);
3434 g_list_free_full (removed_entries, g_free);
3436 desktop_entries = g_list_reverse (desktop_entries);
3438 return desktop_entries;
3441 /* GDesktopAppInfoLookup interface: */
3443 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
3445 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
3446 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
3449 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
3454 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
3455 * @lookup: a #GDesktopAppInfoLookup
3456 * @uri_scheme: a string containing a URI scheme.
3458 * Gets the default application for launching applications
3459 * using this URI scheme for a particular GDesktopAppInfoLookup
3462 * The GDesktopAppInfoLookup interface and this function is used
3463 * to implement g_app_info_get_default_for_uri_scheme() backends
3464 * in a GIO module. There is no reason for applications to use it
3465 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
3467 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3469 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
3472 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
3473 const char *uri_scheme)
3475 GDesktopAppInfoLookupIface *iface;
3477 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
3479 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
3481 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
3484 G_GNUC_END_IGNORE_DEPRECATIONS
3487 * g_desktop_app_info_get_startup_wm_class:
3488 * @info: a #GDesktopAppInfo that supports startup notify
3490 * Retrieves the StartupWMClass field from @info. This represents the
3491 * WM_CLASS property of the main window of the application, if launched
3494 * Returns: (transfer none): the startup WM class, or %NULL if none is set
3495 * in the desktop file.
3500 g_desktop_app_info_get_startup_wm_class (GDesktopAppInfo *info)
3502 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
3504 return info->startup_wm_class;
3508 * g_desktop_app_info_get_string:
3509 * @info: a #GDesktopAppInfo
3510 * @key: the key to look up
3512 * Looks up a string value in the keyfile backing @info.
3514 * The @key is looked up in the "Desktop Entry" group.
3516 * Returns: a newly allocated string, or %NULL if the key
3522 g_desktop_app_info_get_string (GDesktopAppInfo *info,
3525 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
3527 return g_key_file_get_string (info->keyfile,
3528 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
3532 * g_desktop_app_info_get_boolean:
3533 * @info: a #GDesktopAppInfo
3534 * @key: the key to look up
3536 * Looks up a boolean value in the keyfile backing @info.
3538 * The @key is looked up in the "Desktop Entry" group.
3540 * Returns: the boolean value, or %FALSE if the key
3546 g_desktop_app_info_get_boolean (GDesktopAppInfo *info,
3549 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
3551 return g_key_file_get_boolean (info->keyfile,
3552 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
3556 * g_desktop_app_info_has_key:
3557 * @info: a #GDesktopAppInfo
3558 * @key: the key to look up
3560 * Returns whether @key exists in the "Desktop Entry" group
3561 * of the keyfile backing @info.
3563 * Returns: %TRUE if the @key exists
3568 g_desktop_app_info_has_key (GDesktopAppInfo *info,
3571 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
3573 return g_key_file_has_key (info->keyfile,
3574 G_KEY_FILE_DESKTOP_GROUP, key, NULL);