1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright © 2007 Ryan Lortie
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Author: Alexander Larsson <alexl@redhat.com>
31 #ifdef HAVE_CRT_EXTERNS_H
32 #include <crt_externs.h>
35 #undef G_DISABLE_DEPRECATED
37 #include "gcontenttypeprivate.h"
38 #include "gdesktopappinfo.h"
41 #include "gthemedicon.h"
42 #include "gfileicon.h"
43 #include <glib/gstdio.h>
45 #include "giomodule-priv.h"
50 * SECTION:gdesktopappinfo
51 * @title: GDesktopAppInfo
52 * @short_description: Application information from desktop files
53 * @include: gio/gdesktopappinfo.h
55 * #GDesktopAppInfo is an implementation of #GAppInfo based on
58 * Note that <filename><gio/gdesktopappinfo.h></filename> belongs to
59 * the UNIX-specific GIO interfaces, thus you have to use the
60 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
63 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
64 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
65 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
66 #define MIME_CACHE_GROUP "MIME Cache"
67 #define FULL_NAME_KEY "X-GNOME-FullName"
74 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
75 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type,
77 gboolean include_fallback,
78 char **explicit_default);
79 static void mime_info_cache_reload (const char *dir);
80 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
86 * Information about an installed application from a desktop file.
88 struct _GDesktopAppInfo
90 GObject parent_instance;
96 /* FIXME: what about GenericName ? */
112 guint startup_notify : 1;
114 /* FIXME: what about StartupWMClass ? */
118 UPDATE_MIME_NONE = 1 << 0,
119 UPDATE_MIME_SET_DEFAULT = 1 << 1,
120 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
121 UPDATE_MIME_REMOVE = 1 << 3,
122 UPDATE_MIME_SET_LAST_USED = 1 << 4,
125 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
126 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
127 g_desktop_app_info_iface_init))
130 search_path_init (gpointer data)
133 const char * const *data_dirs;
134 const char *user_data_dir;
137 data_dirs = g_get_system_data_dirs ();
138 length = g_strv_length ((char **) data_dirs);
140 args = g_new (char *, length + 2);
143 user_data_dir = g_get_user_data_dir ();
144 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
145 for (i = 0; i < length; i++)
146 args[j++] = g_build_filename (data_dirs[i],
147 "applications", NULL);
153 static const char * const *
154 get_applications_search_path (void)
156 static GOnce once_init = G_ONCE_INIT;
157 return g_once (&once_init, search_path_init, NULL);
161 g_desktop_app_info_finalize (GObject *object)
163 GDesktopAppInfo *info;
165 info = G_DESKTOP_APP_INFO (object);
167 g_free (info->desktop_id);
168 g_free (info->filename);
170 g_free (info->fullname);
171 g_free (info->comment);
172 g_free (info->icon_name);
174 g_object_unref (info->icon);
175 g_strfreev (info->only_show_in);
176 g_strfreev (info->not_show_in);
177 g_free (info->try_exec);
179 g_free (info->binary);
181 g_free (info->categories);
183 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
187 g_desktop_app_info_set_property(GObject *object,
192 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
197 self->filename = g_value_dup_string (value);
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 g_desktop_app_info_get_property(GObject *object,
212 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
217 g_value_set_string (value, self->filename);
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
228 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
230 gobject_class->get_property = g_desktop_app_info_get_property;
231 gobject_class->set_property = g_desktop_app_info_set_property;
232 gobject_class->finalize = g_desktop_app_info_finalize;
235 * GDesktopAppInfo:filename
237 * The origin filename of this #GDesktopAppInfo
239 g_object_class_install_property (gobject_class,
241 g_param_spec_string ("filename", "Filename", "",
243 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
247 g_desktop_app_info_init (GDesktopAppInfo *local)
252 binary_from_exec (const char *exec)
254 const char *p, *start;
260 while (*p != ' ' && *p != 0)
263 return g_strndup (start, p - start);
268 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
275 start_group = g_key_file_get_start_group (key_file);
276 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
278 g_free (start_group);
281 g_free (start_group);
283 type = g_key_file_get_string (key_file,
284 G_KEY_FILE_DESKTOP_GROUP,
285 G_KEY_FILE_DESKTOP_KEY_TYPE,
287 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
294 try_exec = g_key_file_get_string (key_file,
295 G_KEY_FILE_DESKTOP_GROUP,
296 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
298 if (try_exec && try_exec[0] != '\0')
301 t = g_find_program_in_path (try_exec);
310 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
311 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
312 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
313 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
314 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
315 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);
316 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);
317 info->try_exec = try_exec;
318 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
319 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
320 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
321 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
322 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
323 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
324 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
329 if (g_path_is_absolute (info->icon_name))
333 file = g_file_new_for_path (info->icon_name);
334 info->icon = g_file_icon_new (file);
335 g_object_unref (file);
341 /* Work around a common mistake in desktop files */
342 if ((p = strrchr (info->icon_name, '.')) != NULL &&
343 (strcmp (p, ".png") == 0 ||
344 strcmp (p, ".xpm") == 0 ||
345 strcmp (p, ".svg") == 0))
348 info->icon = g_themed_icon_new (info->icon_name);
353 info->binary = binary_from_exec (info->exec);
355 if (info->path && info->path[0] == '\0')
365 g_desktop_app_info_load_file (GDesktopAppInfo *self)
368 gboolean retval = FALSE;
370 g_return_val_if_fail (self->filename != NULL, FALSE);
372 key_file = g_key_file_new ();
374 if (g_key_file_load_from_file (key_file,
379 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
382 g_key_file_free (key_file);
387 * g_desktop_app_info_new_from_keyfile:
388 * @key_file: an opened #GKeyFile
390 * Creates a new #GDesktopAppInfo.
392 * Returns: a new #GDesktopAppInfo or %NULL on error.
397 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
399 GDesktopAppInfo *info;
401 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
402 info->filename = NULL;
403 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
405 g_object_unref (info);
412 * g_desktop_app_info_new_from_filename:
413 * @filename: the path of a desktop file, in the GLib filename encoding
415 * Creates a new #GDesktopAppInfo.
417 * Returns: a new #GDesktopAppInfo or %NULL on error.
420 g_desktop_app_info_new_from_filename (const char *filename)
422 GDesktopAppInfo *info = NULL;
424 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
425 if (!g_desktop_app_info_load_file (info))
427 g_object_unref (info);
434 * g_desktop_app_info_new:
435 * @desktop_id: the desktop file id
437 * Creates a new #GDesktopAppInfo based on a desktop file id.
439 * A desktop file id is the basename of the desktop file, including the
440 * .desktop extension. GIO is looking for a desktop file with this name
441 * in the <filename>applications</filename> subdirectories of the XDG data
442 * directories (i.e. the directories specified in the
443 * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment
444 * variables). GIO also supports the prefix-to-subdirectory mapping that is
445 * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink>
446 * (i.e. a desktop id of kde-foo.desktop will match
447 * <filename>/usr/share/applications/kde/foo.desktop</filename>).
449 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
452 g_desktop_app_info_new (const char *desktop_id)
454 GDesktopAppInfo *appinfo;
455 const char * const *dirs;
459 dirs = get_applications_search_path ();
461 basename = g_strdup (desktop_id);
463 for (i = 0; dirs[i] != NULL; i++)
468 filename = g_build_filename (dirs[i], desktop_id, NULL);
469 appinfo = g_desktop_app_info_new_from_filename (filename);
475 while ((p = strchr (p, '-')) != NULL)
479 filename = g_build_filename (dirs[i], basename, NULL);
480 appinfo = g_desktop_app_info_new_from_filename (filename);
495 appinfo->desktop_id = g_strdup (desktop_id);
497 if (g_desktop_app_info_get_is_hidden (appinfo))
499 g_object_unref (appinfo);
507 g_desktop_app_info_dup (GAppInfo *appinfo)
509 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
510 GDesktopAppInfo *new_info;
512 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
514 new_info->filename = g_strdup (info->filename);
515 new_info->desktop_id = g_strdup (info->desktop_id);
517 new_info->name = g_strdup (info->name);
518 new_info->fullname = g_strdup (info->fullname);
519 new_info->comment = g_strdup (info->comment);
520 new_info->nodisplay = info->nodisplay;
521 new_info->icon_name = g_strdup (info->icon_name);
523 new_info->icon = g_object_ref (info->icon);
524 new_info->only_show_in = g_strdupv (info->only_show_in);
525 new_info->not_show_in = g_strdupv (info->not_show_in);
526 new_info->try_exec = g_strdup (info->try_exec);
527 new_info->exec = g_strdup (info->exec);
528 new_info->binary = g_strdup (info->binary);
529 new_info->path = g_strdup (info->path);
530 new_info->hidden = info->hidden;
531 new_info->terminal = info->terminal;
532 new_info->startup_notify = info->startup_notify;
534 return G_APP_INFO (new_info);
538 g_desktop_app_info_equal (GAppInfo *appinfo1,
541 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
542 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
544 if (info1->desktop_id == NULL ||
545 info2->desktop_id == NULL)
546 return info1 == info2;
548 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
552 g_desktop_app_info_get_id (GAppInfo *appinfo)
554 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
556 return info->desktop_id;
560 g_desktop_app_info_get_name (GAppInfo *appinfo)
562 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
564 if (info->name == NULL)
570 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
572 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
574 if (info->fullname == NULL)
575 return g_desktop_app_info_get_name (appinfo);
576 return info->fullname;
580 * g_desktop_app_info_get_is_hidden:
581 * @info: a #GDesktopAppInfo.
583 * A desktop file is hidden if the Hidden key in it is
586 * Returns: %TRUE if hidden, %FALSE otherwise.
589 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
595 * g_desktop_app_info_get_filename:
596 * @info: a #GDesktopAppInfo
598 * When @info was created from a known filename, return it. In some
599 * situations such as the #GDesktopAppInfo returned from
600 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
602 * Returns: The full path to the file for @info, or %NULL if not known.
606 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
608 return info->filename;
612 g_desktop_app_info_get_description (GAppInfo *appinfo)
614 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
616 return info->comment;
620 g_desktop_app_info_get_executable (GAppInfo *appinfo)
622 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
628 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
630 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
636 g_desktop_app_info_get_icon (GAppInfo *appinfo)
638 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
644 * g_desktop_app_info_get_categories:
645 * @info: a #GDesktopAppInfo
647 * Returns: The unparsed Categories key from the file; i.e. no attempt
648 * is made to split it by ';' or validate it.
651 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
653 return info->categories;
657 expand_macro_single (char macro, char *uri)
663 file = g_file_new_for_uri (uri);
664 path = g_file_get_path (file);
665 g_object_unref (file);
671 result = g_shell_quote (uri);
676 result = g_shell_quote (path);
682 name = g_path_get_dirname (path);
683 result = g_shell_quote (name);
691 name = g_path_get_basename (path);
692 result = g_shell_quote (name);
704 expand_macro (char macro,
706 GDesktopAppInfo *info,
709 GList *uris = *uri_list;
711 gboolean force_file_uri;
712 char force_file_uri_macro;
715 g_return_if_fail (exec != NULL);
717 /* On %u and %U, pass POSIX file path pointing to the URI via
718 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
719 * running or the URI doesn't have a POSIX file path via FUSE
720 * we'll just pass the URI.
722 force_file_uri_macro = macro;
723 force_file_uri = FALSE;
729 force_file_uri_macro = 'f';
730 force_file_uri = TRUE;
733 force_file_uri_macro = 'F';
734 force_file_uri = TRUE;
750 if (!force_file_uri ||
751 /* Pass URI if it contains an anchor */
752 strchr (uri, '#') != NULL)
754 expanded = expand_macro_single (macro, uri);
758 expanded = expand_macro_single (force_file_uri_macro, uri);
759 if (expanded == NULL)
760 expanded = expand_macro_single (macro, uri);
765 g_string_append (exec, expanded);
781 if (!force_file_uri ||
782 /* Pass URI if it contains an anchor */
783 strchr (uri, '#') != NULL)
785 expanded = expand_macro_single (macro, uri);
789 expanded = expand_macro_single (force_file_uri_macro, uri);
790 if (expanded == NULL)
791 expanded = expand_macro_single (macro, uri);
796 g_string_append (exec, expanded);
802 if (uris != NULL && expanded)
803 g_string_append_c (exec, ' ');
811 g_string_append (exec, "--icon ");
812 expanded = g_shell_quote (info->icon_name);
813 g_string_append (exec, expanded);
821 expanded = g_shell_quote (info->name);
822 g_string_append (exec, expanded);
830 expanded = g_shell_quote (info->filename);
831 g_string_append (exec, expanded);
836 case 'm': /* deprecated */
840 g_string_append_c (exec, '%');
848 expand_application_parameters (GDesktopAppInfo *info,
854 GList *uri_list = *uris;
855 const char *p = info->exec;
856 GString *expanded_exec;
859 if (info->exec == NULL)
861 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
862 _("Desktop file didn't specify Exec field"));
866 expanded_exec = g_string_new (NULL);
870 if (p[0] == '%' && p[1] != '\0')
872 expand_macro (p[1], expanded_exec, info, uris);
876 g_string_append_c (expanded_exec, *p);
881 /* No file substitutions */
882 if (uri_list == *uris && uri_list != NULL)
884 /* If there is no macro default to %f. This is also what KDE does */
885 g_string_append_c (expanded_exec, ' ');
886 expand_macro ('f', expanded_exec, info, uris);
889 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
890 g_string_free (expanded_exec, TRUE);
895 prepend_terminal_to_vector (int *argc,
902 char **term_argv = NULL;
907 g_return_val_if_fail (argc != NULL, FALSE);
908 g_return_val_if_fail (argv != NULL, FALSE);
916 /* compute size if not given */
919 for (i = 0; the_argv[i] != NULL; i++)
925 term_argv = g_new0 (char *, 3);
927 check = g_find_program_in_path ("gnome-terminal");
930 term_argv[0] = check;
931 /* Note that gnome-terminal takes -x and
932 * as -e in gnome-terminal is broken we use that. */
933 term_argv[1] = g_strdup ("-x");
938 check = g_find_program_in_path ("nxterm");
940 check = g_find_program_in_path ("color-xterm");
942 check = g_find_program_in_path ("rxvt");
944 check = g_find_program_in_path ("xterm");
946 check = g_find_program_in_path ("dtterm");
949 check = g_strdup ("xterm");
950 g_warning ("couldn't find a terminal, falling back to xterm");
952 term_argv[0] = check;
953 term_argv[1] = g_strdup ("-e");
956 real_argc = term_argc + *argc;
957 real_argv = g_new (char *, real_argc + 1);
959 for (i = 0; i < term_argc; i++)
960 real_argv[i] = term_argv[i];
962 for (j = 0; j < *argc; j++, i++)
963 real_argv[i] = (char *)the_argv[j];
971 /* we use g_free here as we sucked all the inner strings
972 * out from it into real_argv */
977 #endif /* G_OS_WIN32 */
981 create_files_for_uris (GList *uris)
988 for (iter = uris; iter; iter = iter->next)
990 GFile *file = g_file_new_for_uri ((char *)iter->data);
991 res = g_list_prepend (res, file);
994 return g_list_reverse (res);
999 GSpawnChildSetupFunc user_setup;
1000 gpointer user_setup_data;
1007 child_setup (gpointer user_data)
1009 ChildSetupData *data = user_data;
1012 g_setenv ("DISPLAY", data->display, TRUE);
1015 g_setenv ("DESKTOP_STARTUP_ID", data->sn_id, TRUE);
1017 if (data->desktop_file)
1021 g_setenv ("GIO_LAUNCHED_DESKTOP_FILE", data->desktop_file, TRUE);
1023 g_snprintf (pid, 20, "%ld", (long)getpid ());
1024 g_setenv ("GIO_LAUNCHED_DESKTOP_FILE_PID", pid, TRUE);
1027 if (data->user_setup)
1028 data->user_setup (data->user_setup_data);
1032 notify_desktop_launch (GDBusConnection *session_bus,
1033 GDesktopAppInfo *info,
1035 const char *display,
1040 GVariantBuilder uri_variant;
1041 GVariantBuilder extras_variant;
1043 const char *desktop_file_id;
1044 const char *gio_desktop_file;
1046 if (session_bus == NULL)
1049 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
1050 for (iter = uris; iter; iter = iter->next)
1051 g_variant_builder_add (&uri_variant, "s", iter->data);
1053 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
1054 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
1055 g_variant_builder_add (&extras_variant, "{sv}",
1059 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
1060 if (gio_desktop_file != NULL)
1061 g_variant_builder_add (&extras_variant, "{sv}",
1062 "origin-desktop-file",
1063 g_variant_new_bytestring (gio_desktop_file));
1064 if (g_get_prgname () != NULL)
1065 g_variant_builder_add (&extras_variant, "{sv}",
1067 g_variant_new_bytestring (g_get_prgname ()));
1068 g_variant_builder_add (&extras_variant, "{sv}",
1071 (gint64)getpid ()));
1074 desktop_file_id = info->filename;
1075 else if (info->desktop_id)
1076 desktop_file_id = info->desktop_id;
1078 desktop_file_id = "";
1080 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
1081 "org.gtk.gio.DesktopAppInfo",
1083 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
1084 g_variant_new_bytestring (desktop_file_id),
1085 display ? display : "",
1089 g_dbus_connection_send_message (session_bus,
1093 g_object_unref (msg);
1096 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
1099 _g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
1101 GAppLaunchContext *launch_context,
1102 GSpawnFlags spawn_flags,
1103 GSpawnChildSetupFunc user_setup,
1104 gpointer user_setup_data,
1105 GDesktopAppLaunchCallback pid_callback,
1106 gpointer pid_callback_data,
1109 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1110 GDBusConnection *session_bus;
1111 gboolean completed = FALSE;
1115 ChildSetupData data;
1117 g_return_val_if_fail (appinfo != NULL, FALSE);
1121 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
1126 GList *launched_uris;
1130 if (!expand_application_parameters (info, &uris,
1131 &argc, &argv, error))
1134 /* Get the subset of URIs we're launching with this process */
1135 launched_uris = NULL;
1136 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
1137 launched_uris = g_list_prepend (launched_uris, iter->data);
1138 launched_uris = g_list_reverse (launched_uris);
1140 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
1142 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1143 _("Unable to find terminal required for application"));
1147 data.user_setup = user_setup;
1148 data.user_setup_data = user_setup_data;
1149 data.display = NULL;
1151 data.desktop_file = info->filename;
1155 GList *launched_files = create_files_for_uris (launched_uris);
1157 data.display = g_app_launch_context_get_display (launch_context,
1161 if (info->startup_notify)
1162 data.sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
1165 g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
1166 g_list_free (launched_files);
1169 if (!g_spawn_async (info->path,
1179 g_app_launch_context_launch_failed (launch_context, data.sn_id);
1181 g_free (data.sn_id);
1182 g_free (data.display);
1183 g_list_free (launched_uris);
1188 if (pid_callback != NULL)
1189 pid_callback (info, pid, pid_callback_data);
1191 notify_desktop_launch (session_bus,
1198 g_free (data.sn_id);
1199 g_free (data.display);
1200 g_list_free (launched_uris);
1205 while (uris != NULL);
1207 /* TODO - need to handle the process exiting immediately
1208 * after launching an app. See http://bugzilla.gnome.org/606960
1210 if (session_bus != NULL)
1212 /* This asynchronous flush holds a reference until it completes,
1213 * which ensures that the following unref won't immediately kill
1214 * the connection if we were the initial owner.
1216 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
1217 g_object_unref (session_bus);
1229 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
1231 GAppLaunchContext *launch_context,
1234 return _g_desktop_app_info_launch_uris_internal (appinfo, uris,
1236 _SPAWN_FLAGS_DEFAULT,
1237 NULL, NULL, NULL, NULL,
1242 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1244 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1246 return info->exec &&
1247 ((strstr (info->exec, "%u") != NULL) ||
1248 (strstr (info->exec, "%U") != NULL));
1252 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1254 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1256 return info->exec &&
1257 ((strstr (info->exec, "%f") != NULL) ||
1258 (strstr (info->exec, "%F") != NULL));
1262 g_desktop_app_info_launch (GAppInfo *appinfo,
1264 GAppLaunchContext *launch_context,
1274 uri = g_file_get_uri (files->data);
1275 uris = g_list_prepend (uris, uri);
1276 files = files->next;
1279 uris = g_list_reverse (uris);
1281 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1283 g_list_foreach (uris, (GFunc)g_free, NULL);
1290 * g_desktop_app_info_launch_uris_as_manager:
1291 * @appinfo: a #GDesktopAppInfo
1292 * @uris: (element-type utf8): List of URIs
1293 * @launch_context: a #GAppLaunchContext
1294 * @spawn_flags: #GSpawnFlags, used for each process
1295 * @user_setup: (scope call): a #GSpawnChildSetupFunc, used once for
1297 * @user_setup_data: (closure user_setup): User data for @user_setup
1298 * @pid_callback: (scope call): Callback for child processes
1299 * @pid_callback_data: (closure pid_callback): User data for @callback
1302 * This function performs the equivalent of g_app_info_launch_uris(),
1303 * but is intended primarily for operating system components that
1304 * launch applications. Ordinary applications should use
1305 * g_app_info_launch_uris().
1307 * In contrast to g_app_info_launch_uris(), all processes created will
1308 * always be run directly as children as if by the UNIX fork()/exec()
1311 * This guarantee allows additional control over the exact environment
1312 * of the child processes, which is provided via a setup function
1313 * @setup, as well as the process identifier of each child process via
1314 * @pid_callback. See g_spawn_async() for more information about the
1315 * semantics of the @setup function.
1318 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
1320 GAppLaunchContext *launch_context,
1321 GSpawnFlags spawn_flags,
1322 GSpawnChildSetupFunc user_setup,
1323 gpointer user_setup_data,
1324 GDesktopAppLaunchCallback pid_callback,
1325 gpointer pid_callback_data,
1328 return _g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
1339 G_LOCK_DEFINE_STATIC (g_desktop_env);
1340 static gchar *g_desktop_env = NULL;
1343 * g_desktop_app_info_set_desktop_env:
1344 * @desktop_env: a string specifying what desktop this is
1346 * Sets the name of the desktop that the application is running in.
1347 * This is used by g_app_info_should_show() to evaluate the
1348 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1349 * desktop entry fields.
1351 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1352 * Menu specification</ulink> recognizes the following:
1354 * <member>GNOME</member>
1355 * <member>KDE</member>
1356 * <member>ROX</member>
1357 * <member>XFCE</member>
1358 * <member>Old</member>
1361 * Should be called only once; subsequent calls are ignored.
1364 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1366 G_LOCK (g_desktop_env);
1368 g_desktop_env = g_strdup (desktop_env);
1369 G_UNLOCK (g_desktop_env);
1373 g_desktop_app_info_should_show (GAppInfo *appinfo)
1375 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1377 const gchar *desktop_env;
1380 if (info->nodisplay)
1383 G_LOCK (g_desktop_env);
1384 desktop_env = g_desktop_env;
1385 G_UNLOCK (g_desktop_env);
1387 if (info->only_show_in)
1389 if (desktop_env == NULL)
1393 for (i = 0; info->only_show_in[i] != NULL; i++)
1395 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1405 if (info->not_show_in && desktop_env)
1407 for (i = 0; info->not_show_in[i] != NULL; i++)
1409 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1423 ensure_dir (DirType type,
1426 char *path, *display_name;
1429 if (type == APP_DIR)
1430 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1432 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1435 if (g_mkdir_with_parents (path, 0700) == 0)
1439 display_name = g_filename_display_name (path);
1440 if (type == APP_DIR)
1441 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1442 _("Can't create user application configuration folder %s: %s"),
1443 display_name, g_strerror (errsv));
1445 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1446 _("Can't create user MIME configuration folder %s: %s"),
1447 display_name, g_strerror (errsv));
1449 g_free (display_name);
1456 update_mimeapps_list (const char *desktop_id,
1457 const char *content_type,
1458 UpdateMimeFlags flags,
1461 char *dirname, *filename, *string;
1463 gboolean load_succeeded, res, explicit_default;
1464 char **old_list, **list;
1466 gsize length, data_size;
1469 char **content_types;
1471 /* Don't add both at start and end */
1472 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
1473 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
1475 dirname = ensure_dir (APP_DIR, error);
1479 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1482 key_file = g_key_file_new ();
1483 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1484 if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1486 g_key_file_free (key_file);
1487 key_file = g_key_file_new ();
1492 content_types = g_new (char *, 2);
1493 content_types[0] = g_strdup (content_type);
1494 content_types[1] = NULL;
1498 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
1501 explicit_default = FALSE;
1503 for (k = 0; content_types && content_types[k]; k++)
1505 /* set as default, if requested so */
1506 string = g_key_file_get_string (key_file,
1507 DEFAULT_APPLICATIONS_GROUP,
1511 if (g_strcmp0 (string, desktop_id) != 0 &&
1512 (flags & UPDATE_MIME_SET_DEFAULT))
1515 string = g_strdup (desktop_id);
1517 /* add in the non-default list too, if it's not already there */
1518 flags |= UPDATE_MIME_SET_NON_DEFAULT;
1521 if (string == NULL || desktop_id == NULL)
1522 g_key_file_remove_key (key_file,
1523 DEFAULT_APPLICATIONS_GROUP,
1528 g_key_file_set_string (key_file,
1529 DEFAULT_APPLICATIONS_GROUP,
1533 explicit_default = TRUE;
1541 /* reuse the list from above */
1545 g_strfreev (content_types);
1546 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1549 for (k = 0; content_types && content_types[k]; k++)
1551 /* Add to the right place in the list */
1554 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1555 content_types[k], &length, NULL);
1557 list = g_new (char *, 1 + length + 1);
1561 /* if we're adding a last-used hint, just put the application in front of the list */
1562 if (flags & UPDATE_MIME_SET_LAST_USED)
1564 /* avoid adding this again as non-default later */
1565 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1566 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1568 list[i++] = g_strdup (desktop_id);
1573 for (j = 0; old_list[j] != NULL; j++)
1575 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1577 /* rewrite other entries if they're different from the new one */
1578 list[i++] = g_strdup (old_list[j]);
1580 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1582 /* we encountered an old entry which is equal to the one we're adding as non-default,
1583 * don't change its position in the list.
1585 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1586 list[i++] = g_strdup (old_list[j]);
1591 /* add it at the end of the list */
1592 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1593 list[i++] = g_strdup (desktop_id);
1597 g_strfreev (old_list);
1599 if (list[0] == NULL || desktop_id == NULL)
1600 g_key_file_remove_key (key_file,
1601 ADDED_ASSOCIATIONS_GROUP,
1606 g_key_file_set_string_list (key_file,
1607 ADDED_ASSOCIATIONS_GROUP,
1609 (const char * const *)list, i);
1611 /* if we had no explicit default set, we should add the system default to the
1612 * list, to avoid overriding it with applications from this list.
1614 if (!explicit_default)
1616 system_list = get_all_desktop_entries_for_mime_type (content_type, (const char **) list, FALSE, NULL);
1618 if (system_list != NULL)
1620 string = system_list->data;
1622 g_key_file_set_string (key_file,
1623 DEFAULT_APPLICATIONS_GROUP,
1628 g_list_free_full (system_list, g_free);
1637 /* reuse the list from above */
1641 g_strfreev (content_types);
1642 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1645 for (k = 0; content_types && content_types[k]; k++)
1647 /* Remove from removed associations group (unless remove) */
1650 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1651 content_types[k], &length, NULL);
1653 list = g_new (char *, 1 + length + 1);
1656 if (flags & UPDATE_MIME_REMOVE)
1657 list[i++] = g_strdup (desktop_id);
1660 for (j = 0; old_list[j] != NULL; j++)
1662 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1663 list[i++] = g_strdup (old_list[j]);
1668 g_strfreev (old_list);
1670 if (list[0] == NULL || desktop_id == NULL)
1671 g_key_file_remove_key (key_file,
1672 REMOVED_ASSOCIATIONS_GROUP,
1676 g_key_file_set_string_list (key_file,
1677 REMOVED_ASSOCIATIONS_GROUP,
1679 (const char * const *)list, i);
1684 g_strfreev (content_types);
1686 data = g_key_file_to_data (key_file, &data_size, error);
1687 g_key_file_free (key_file);
1689 res = g_file_set_contents (filename, data, data_size, error);
1691 mime_info_cache_reload (NULL);
1700 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
1701 const char *content_type,
1704 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1706 if (!g_desktop_app_info_ensure_saved (info, error))
1709 /* both add support for the content type and set as last used */
1710 return update_mimeapps_list (info->desktop_id, content_type,
1711 UPDATE_MIME_SET_NON_DEFAULT |
1712 UPDATE_MIME_SET_LAST_USED,
1717 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1718 const char *content_type,
1721 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1723 if (!g_desktop_app_info_ensure_saved (info, error))
1726 return update_mimeapps_list (info->desktop_id, content_type,
1727 UPDATE_MIME_SET_DEFAULT,
1732 update_program_done (GPid pid,
1736 /* Did the application exit correctly */
1737 if (WIFEXITED (status) &&
1738 WEXITSTATUS (status) == 0)
1740 /* Here we could clean out any caches in use */
1745 run_update_command (char *command,
1754 GError *error = NULL;
1757 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1759 if (g_spawn_async ("/", argv,
1761 G_SPAWN_SEARCH_PATH |
1762 G_SPAWN_STDOUT_TO_DEV_NULL |
1763 G_SPAWN_STDERR_TO_DEV_NULL |
1764 G_SPAWN_DO_NOT_REAP_CHILD,
1765 NULL, NULL, /* No setup function */
1768 g_child_watch_add (pid, update_program_done, NULL);
1771 /* If we get an error at this point, it's quite likely the user doesn't
1772 * have an installed copy of either 'update-mime-database' or
1773 * 'update-desktop-database'. I don't think we want to popup an error
1774 * dialog at this point, so we just do a g_warning to give the user a
1775 * chance of debugging it.
1777 g_warning ("%s", error->message);
1784 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1785 const char *extension,
1788 char *filename, *basename, *mimetype;
1792 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1795 dirname = ensure_dir (MIMETYPE_DIR, error);
1799 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1800 filename = g_build_filename (dirname, basename, NULL);
1804 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1806 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1811 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1812 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1813 " <mime-type type=\"%s\">\n"
1814 " <comment>%s document</comment>\n"
1815 " <glob pattern=\"*.%s\"/>\n"
1817 "</mime-info>\n", mimetype, extension, extension);
1819 g_file_set_contents (filename, contents, -1, NULL);
1822 run_update_command ("update-mime-database", "mime");
1826 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1836 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1837 const char *content_type,
1840 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1842 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1845 return update_mimeapps_list (info->desktop_id, content_type,
1846 UPDATE_MIME_SET_NON_DEFAULT,
1851 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1857 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1858 const char *content_type,
1861 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1863 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1866 return update_mimeapps_list (info->desktop_id, content_type,
1872 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1878 char *data, *desktop_id;
1883 if (info->filename != NULL)
1886 /* This is only used for object created with
1887 * g_app_info_create_from_commandline. All other
1888 * object should have a filename
1891 dirname = ensure_dir (APP_DIR, error);
1895 key_file = g_key_file_new ();
1897 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1898 "Encoding", "UTF-8");
1899 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1900 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1901 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1902 G_KEY_FILE_DESKTOP_KEY_TYPE,
1903 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1905 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1906 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1908 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1909 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1911 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1912 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1914 if (info->fullname != NULL)
1915 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1916 FULL_NAME_KEY, info->fullname);
1918 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1919 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1921 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1922 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1924 data = g_key_file_to_data (key_file, &data_size, NULL);
1925 g_key_file_free (key_file);
1927 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1928 filename = g_build_filename (dirname, desktop_id, NULL);
1929 g_free (desktop_id);
1932 fd = g_mkstemp (filename);
1937 display_name = g_filename_display_name (filename);
1938 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1939 _("Can't create user desktop file %s"), display_name);
1940 g_free (display_name);
1946 desktop_id = g_path_get_basename (filename);
1950 res = g_file_set_contents (filename, data, data_size, error);
1953 g_free (desktop_id);
1958 info->filename = filename;
1959 info->desktop_id = desktop_id;
1961 run_update_command ("update-desktop-database", "applications");
1967 g_desktop_app_info_can_delete (GAppInfo *appinfo)
1969 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1973 if (strstr (info->filename, "/userapp-"))
1974 return g_access (info->filename, W_OK) == 0;
1981 g_desktop_app_info_delete (GAppInfo *appinfo)
1983 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1987 if (g_remove (info->filename) == 0)
1989 update_mimeapps_list (info->desktop_id, NULL,
1993 g_free (info->filename);
1994 info->filename = NULL;
1995 g_free (info->desktop_id);
1996 info->desktop_id = NULL;
2006 * g_app_info_create_from_commandline:
2007 * @commandline: the commandline to use
2008 * @application_name: (allow-none): the application name, or %NULL to use @commandline
2009 * @flags: flags that can specify details of the created #GAppInfo
2010 * @error: a #GError location to store the error occuring, %NULL to ignore.
2012 * Creates a new #GAppInfo from the given information.
2014 * Returns: (transfer full): new #GAppInfo for given command.
2017 g_app_info_create_from_commandline (const char *commandline,
2018 const char *application_name,
2019 GAppInfoCreateFlags flags,
2024 GDesktopAppInfo *info;
2026 g_return_val_if_fail (commandline, NULL);
2028 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
2030 info->filename = NULL;
2031 info->desktop_id = NULL;
2033 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
2034 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
2035 info->hidden = FALSE;
2036 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
2037 info->exec = g_strconcat (commandline, " %u", NULL);
2039 info->exec = g_strconcat (commandline, " %f", NULL);
2040 info->nodisplay = TRUE;
2041 info->binary = binary_from_exec (info->exec);
2043 if (application_name)
2044 info->name = g_strdup (application_name);
2047 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
2048 split = g_strsplit (commandline, " ", 2);
2049 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
2051 info->name = basename;
2052 if (info->name == NULL)
2053 info->name = g_strdup ("custom");
2055 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
2057 return G_APP_INFO (info);
2061 g_desktop_app_info_iface_init (GAppInfoIface *iface)
2063 iface->dup = g_desktop_app_info_dup;
2064 iface->equal = g_desktop_app_info_equal;
2065 iface->get_id = g_desktop_app_info_get_id;
2066 iface->get_name = g_desktop_app_info_get_name;
2067 iface->get_description = g_desktop_app_info_get_description;
2068 iface->get_executable = g_desktop_app_info_get_executable;
2069 iface->get_icon = g_desktop_app_info_get_icon;
2070 iface->launch = g_desktop_app_info_launch;
2071 iface->supports_uris = g_desktop_app_info_supports_uris;
2072 iface->supports_files = g_desktop_app_info_supports_files;
2073 iface->launch_uris = g_desktop_app_info_launch_uris;
2074 iface->should_show = g_desktop_app_info_should_show;
2075 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
2076 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
2077 iface->add_supports_type = g_desktop_app_info_add_supports_type;
2078 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
2079 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
2080 iface->can_delete = g_desktop_app_info_can_delete;
2081 iface->do_delete = g_desktop_app_info_delete;
2082 iface->get_commandline = g_desktop_app_info_get_commandline;
2083 iface->get_display_name = g_desktop_app_info_get_display_name;
2084 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
2088 app_info_in_list (GAppInfo *info,
2091 while (list != NULL)
2093 if (g_app_info_equal (info, list->data))
2101 * g_app_info_get_recommended_for_type:
2102 * @content_type: the content type to find a #GAppInfo for
2104 * Gets a list of recommended #GAppInfos for a given content type, i.e.
2105 * those applications which claim to support the given content type exactly,
2106 * and not by MIME type subclassing.
2107 * Note that the first application of the list is the last used one, i.e.
2108 * the last one for which #g_app_info_set_as_last_used_for_type has been
2111 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2112 * for given @content_type or %NULL on error.
2117 g_app_info_get_recommended_for_type (const gchar *content_type)
2119 GList *desktop_entries, *l;
2121 GDesktopAppInfo *info;
2123 g_return_val_if_fail (content_type != NULL, NULL);
2125 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
2128 for (l = desktop_entries; l != NULL; l = l->next)
2130 char *desktop_entry = l->data;
2132 info = g_desktop_app_info_new (desktop_entry);
2135 if (app_info_in_list (G_APP_INFO (info), infos))
2136 g_object_unref (info);
2138 infos = g_list_prepend (infos, info);
2140 g_free (desktop_entry);
2143 g_list_free (desktop_entries);
2145 return g_list_reverse (infos);
2149 * g_app_info_get_fallback_for_type:
2150 * @content_type: the content type to find a #GAppInfo for
2152 * Gets a list of fallback #GAppInfos for a given content type, i.e.
2153 * those applications which claim to support the given content type
2154 * by MIME type subclassing and not directly.
2156 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2157 * for given @content_type or %NULL on error.
2162 g_app_info_get_fallback_for_type (const gchar *content_type)
2164 GList *desktop_entries, *l;
2165 GList *infos, *recommended_infos;
2166 GDesktopAppInfo *info;
2168 g_return_val_if_fail (content_type != NULL, NULL);
2170 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
2171 recommended_infos = g_app_info_get_recommended_for_type (content_type);
2174 for (l = desktop_entries; l != NULL; l = l->next)
2176 char *desktop_entry = l->data;
2178 info = g_desktop_app_info_new (desktop_entry);
2181 if (app_info_in_list (G_APP_INFO (info), infos) ||
2182 app_info_in_list (G_APP_INFO (info), recommended_infos))
2183 g_object_unref (info);
2185 infos = g_list_prepend (infos, info);
2187 g_free (desktop_entry);
2190 g_list_free (desktop_entries);
2191 g_list_free_full (recommended_infos, g_object_unref);
2193 return g_list_reverse (infos);
2197 * g_app_info_get_all_for_type:
2198 * @content_type: the content type to find a #GAppInfo for
2200 * Gets a list of all #GAppInfos for a given content type.
2202 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2203 * for given @content_type or %NULL on error.
2206 g_app_info_get_all_for_type (const char *content_type)
2208 GList *desktop_entries, *l;
2210 char *user_default = NULL;
2211 GDesktopAppInfo *info;
2213 g_return_val_if_fail (content_type != NULL, NULL);
2215 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2218 /* put the user default in front of the list, for compatibility */
2219 if (user_default != NULL)
2221 info = g_desktop_app_info_new (user_default);
2224 infos = g_list_prepend (infos, info);
2227 g_free (user_default);
2229 for (l = desktop_entries; l != NULL; l = l->next)
2231 char *desktop_entry = l->data;
2233 info = g_desktop_app_info_new (desktop_entry);
2236 if (app_info_in_list (G_APP_INFO (info), infos))
2237 g_object_unref (info);
2239 infos = g_list_prepend (infos, info);
2241 g_free (desktop_entry);
2244 g_list_free (desktop_entries);
2246 return g_list_reverse (infos);
2250 * g_app_info_reset_type_associations:
2251 * @content_type: a content type
2253 * Removes all changes to the type associations done by
2254 * g_app_info_set_as_default_for_type(),
2255 * g_app_info_set_as_default_for_extension(),
2256 * g_app_info_add_supports_type() or g_app_info_remove_supports_type().
2261 g_app_info_reset_type_associations (const char *content_type)
2263 update_mimeapps_list (NULL, content_type,
2269 * g_app_info_get_default_for_type:
2270 * @content_type: the content type to find a #GAppInfo for
2271 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
2274 * Gets the #GAppInfo that corresponds to a given content type.
2276 * Returns: (transfer full): #GAppInfo for given @content_type or
2280 g_app_info_get_default_for_type (const char *content_type,
2281 gboolean must_support_uris)
2283 GList *desktop_entries, *l;
2284 char *user_default = NULL;
2287 g_return_val_if_fail (content_type != NULL, NULL);
2289 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2293 if (user_default != NULL)
2295 info = (GAppInfo *) g_desktop_app_info_new (user_default);
2299 if (must_support_uris && !g_app_info_supports_uris (info))
2301 g_object_unref (info);
2307 g_free (user_default);
2311 g_list_free_full (desktop_entries, g_free);
2315 /* pick the first from the other list that matches our URI
2318 for (l = desktop_entries; l != NULL; l = l->next)
2320 char *desktop_entry = l->data;
2322 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
2325 if (must_support_uris && !g_app_info_supports_uris (info))
2327 g_object_unref (info);
2335 g_list_free_full (desktop_entries, g_free);
2341 * g_app_info_get_default_for_uri_scheme:
2342 * @uri_scheme: a string containing a URI scheme.
2344 * Gets the default application for launching applications
2345 * using this URI scheme. A URI scheme is the initial part
2346 * of the URI, up to but not including the ':', e.g. "http",
2349 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
2352 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
2355 char *content_type, *scheme_down;
2357 scheme_down = g_ascii_strdown (uri_scheme, -1);
2358 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
2359 g_free (scheme_down);
2360 app_info = g_app_info_get_default_for_type (content_type, FALSE);
2361 g_free (content_type);
2367 get_apps_from_dir (GHashTable *apps,
2368 const char *dirname,
2372 const char *basename;
2373 char *filename, *subprefix, *desktop_id;
2375 GDesktopAppInfo *appinfo;
2377 dir = g_dir_open (dirname, 0, NULL);
2380 while ((basename = g_dir_read_name (dir)) != NULL)
2382 filename = g_build_filename (dirname, basename, NULL);
2383 if (g_str_has_suffix (basename, ".desktop"))
2385 desktop_id = g_strconcat (prefix, basename, NULL);
2387 /* Use _extended so we catch NULLs too (hidden) */
2388 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
2390 appinfo = g_desktop_app_info_new_from_filename (filename);
2393 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
2395 g_object_unref (appinfo);
2400 if (appinfo || hidden)
2402 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
2406 /* Reuse instead of strdup here */
2407 appinfo->desktop_id = desktop_id;
2412 g_free (desktop_id);
2416 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
2418 subprefix = g_strconcat (prefix, basename, "-", NULL);
2419 get_apps_from_dir (apps, filename, subprefix);
2431 * g_app_info_get_all:
2433 * Gets a list of all of the applications currently registered
2436 * For desktop files, this includes applications that have
2437 * <literal>NoDisplay=true</literal> set or are excluded from
2438 * display by means of <literal>OnlyShowIn</literal> or
2439 * <literal>NotShowIn</literal>. See g_app_info_should_show().
2440 * The returned list does not include applications which have
2441 * the <literal>Hidden</literal> key set.
2443 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
2446 g_app_info_get_all (void)
2448 const char * const *dirs;
2450 GHashTableIter iter;
2455 dirs = get_applications_search_path ();
2457 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
2461 for (i = 0; dirs[i] != NULL; i++)
2462 get_apps_from_dir (apps, dirs[i], "");
2466 g_hash_table_iter_init (&iter, apps);
2467 while (g_hash_table_iter_next (&iter, NULL, &value))
2470 infos = g_list_prepend (infos, value);
2473 g_hash_table_destroy (apps);
2475 return g_list_reverse (infos);
2478 /* Cacheing of mimeinfo.cache and defaults.list files */
2482 GHashTable *mime_info_cache_map;
2483 GHashTable *defaults_list_map;
2484 GHashTable *mimeapps_list_added_map;
2485 GHashTable *mimeapps_list_removed_map;
2486 GHashTable *mimeapps_list_defaults_map;
2487 time_t mime_info_cache_timestamp;
2488 time_t defaults_list_timestamp;
2489 time_t mimeapps_list_timestamp;
2493 GList *dirs; /* mimeinfo.cache and defaults.list */
2494 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
2495 time_t last_stat_time;
2496 guint should_ping_mime_monitor : 1;
2499 static MimeInfoCache *mime_info_cache = NULL;
2500 G_LOCK_DEFINE_STATIC (mime_info_cache);
2502 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2503 const char *mime_type,
2504 char **new_desktop_file_ids);
2506 static MimeInfoCache * mime_info_cache_new (void);
2509 destroy_info_cache_value (gpointer key,
2513 g_list_foreach (value, (GFunc)g_free, NULL);
2514 g_list_free (value);
2518 destroy_info_cache_map (GHashTable *info_cache_map)
2520 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
2521 g_hash_table_destroy (info_cache_map);
2525 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2526 const char *cache_file,
2532 filename = g_build_filename (dir->path, cache_file, NULL);
2534 if (g_stat (filename, &buf) < 0)
2541 if (buf.st_mtime != *timestamp)
2547 /* Call with lock held */
2549 remove_all (gpointer key,
2558 mime_info_cache_blow_global_cache (void)
2560 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
2565 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2569 gchar *filename, **mime_types;
2576 if (dir->mime_info_cache_map != NULL &&
2577 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2578 &dir->mime_info_cache_timestamp))
2581 if (dir->mime_info_cache_map != NULL)
2582 destroy_info_cache_map (dir->mime_info_cache_map);
2584 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2585 (GDestroyNotify) g_free,
2588 key_file = g_key_file_new ();
2590 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2592 if (g_stat (filename, &buf) < 0)
2595 if (dir->mime_info_cache_timestamp > 0)
2596 mime_info_cache->should_ping_mime_monitor = TRUE;
2598 dir->mime_info_cache_timestamp = buf.st_mtime;
2600 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2605 if (load_error != NULL)
2608 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2611 if (load_error != NULL)
2614 for (i = 0; mime_types[i] != NULL; i++)
2616 gchar **desktop_file_ids;
2617 char *unaliased_type;
2618 desktop_file_ids = g_key_file_get_string_list (key_file,
2624 if (desktop_file_ids == NULL)
2627 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2628 mime_info_cache_dir_add_desktop_entries (dir,
2631 g_free (unaliased_type);
2633 g_strfreev (desktop_file_ids);
2636 g_strfreev (mime_types);
2637 g_key_file_free (key_file);
2642 g_key_file_free (key_file);
2644 if (mime_types != NULL)
2645 g_strfreev (mime_types);
2648 g_error_free (load_error);
2652 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2656 gchar *filename, **mime_types;
2657 char *unaliased_type;
2658 char **desktop_file_ids;
2665 if (dir->defaults_list_map != NULL &&
2666 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2667 &dir->defaults_list_timestamp))
2670 if (dir->defaults_list_map != NULL)
2671 g_hash_table_destroy (dir->defaults_list_map);
2672 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2673 g_free, (GDestroyNotify)g_strfreev);
2676 key_file = g_key_file_new ();
2678 filename = g_build_filename (dir->path, "defaults.list", NULL);
2679 if (g_stat (filename, &buf) < 0)
2682 if (dir->defaults_list_timestamp > 0)
2683 mime_info_cache->should_ping_mime_monitor = TRUE;
2685 dir->defaults_list_timestamp = buf.st_mtime;
2687 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2691 if (load_error != NULL)
2694 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2696 if (mime_types != NULL)
2698 for (i = 0; mime_types[i] != NULL; i++)
2700 desktop_file_ids = g_key_file_get_string_list (key_file,
2701 DEFAULT_APPLICATIONS_GROUP,
2705 if (desktop_file_ids == NULL)
2708 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2709 g_hash_table_replace (dir->defaults_list_map,
2714 g_strfreev (mime_types);
2717 g_key_file_free (key_file);
2722 g_key_file_free (key_file);
2724 if (mime_types != NULL)
2725 g_strfreev (mime_types);
2728 g_error_free (load_error);
2732 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2736 gchar *filename, **mime_types;
2737 char *unaliased_type;
2738 char **desktop_file_ids;
2746 if (dir->mimeapps_list_added_map != NULL &&
2747 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2748 &dir->mimeapps_list_timestamp))
2751 if (dir->mimeapps_list_added_map != NULL)
2752 g_hash_table_destroy (dir->mimeapps_list_added_map);
2753 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2754 g_free, (GDestroyNotify)g_strfreev);
2756 if (dir->mimeapps_list_removed_map != NULL)
2757 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2758 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2759 g_free, (GDestroyNotify)g_strfreev);
2761 if (dir->mimeapps_list_defaults_map != NULL)
2762 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2763 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2766 key_file = g_key_file_new ();
2768 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2769 if (g_stat (filename, &buf) < 0)
2772 if (dir->mimeapps_list_timestamp > 0)
2773 mime_info_cache->should_ping_mime_monitor = TRUE;
2775 dir->mimeapps_list_timestamp = buf.st_mtime;
2777 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2781 if (load_error != NULL)
2784 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2786 if (mime_types != NULL)
2788 for (i = 0; mime_types[i] != NULL; i++)
2790 desktop_file_ids = g_key_file_get_string_list (key_file,
2791 ADDED_ASSOCIATIONS_GROUP,
2795 if (desktop_file_ids == NULL)
2798 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2799 g_hash_table_replace (dir->mimeapps_list_added_map,
2804 g_strfreev (mime_types);
2807 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2809 if (mime_types != NULL)
2811 for (i = 0; mime_types[i] != NULL; i++)
2813 desktop_file_ids = g_key_file_get_string_list (key_file,
2814 REMOVED_ASSOCIATIONS_GROUP,
2818 if (desktop_file_ids == NULL)
2821 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2822 g_hash_table_replace (dir->mimeapps_list_removed_map,
2827 g_strfreev (mime_types);
2830 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2832 if (mime_types != NULL)
2834 for (i = 0; mime_types[i] != NULL; i++)
2836 desktop_id = g_key_file_get_string (key_file,
2837 DEFAULT_APPLICATIONS_GROUP,
2840 if (desktop_id == NULL)
2843 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2844 g_hash_table_replace (dir->mimeapps_list_defaults_map,
2849 g_strfreev (mime_types);
2852 g_key_file_free (key_file);
2857 g_key_file_free (key_file);
2859 if (mime_types != NULL)
2860 g_strfreev (mime_types);
2863 g_error_free (load_error);
2866 static MimeInfoCacheDir *
2867 mime_info_cache_dir_new (const char *path)
2869 MimeInfoCacheDir *dir;
2871 dir = g_new0 (MimeInfoCacheDir, 1);
2872 dir->path = g_strdup (path);
2878 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2883 if (dir->mime_info_cache_map != NULL)
2885 destroy_info_cache_map (dir->mime_info_cache_map);
2886 dir->mime_info_cache_map = NULL;
2890 if (dir->defaults_list_map != NULL)
2892 g_hash_table_destroy (dir->defaults_list_map);
2893 dir->defaults_list_map = NULL;
2896 if (dir->mimeapps_list_added_map != NULL)
2898 g_hash_table_destroy (dir->mimeapps_list_added_map);
2899 dir->mimeapps_list_added_map = NULL;
2902 if (dir->mimeapps_list_removed_map != NULL)
2904 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2905 dir->mimeapps_list_removed_map = NULL;
2908 if (dir->mimeapps_list_defaults_map != NULL)
2910 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2911 dir->mimeapps_list_defaults_map = NULL;
2918 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2919 const char *mime_type,
2920 char **new_desktop_file_ids)
2922 GList *desktop_file_ids;
2925 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2928 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2930 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
2931 desktop_file_ids = g_list_append (desktop_file_ids,
2932 g_strdup (new_desktop_file_ids[i]));
2935 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2939 mime_info_cache_init_dir_lists (void)
2941 const char * const *dirs;
2944 mime_info_cache = mime_info_cache_new ();
2946 dirs = get_applications_search_path ();
2948 for (i = 0; dirs[i] != NULL; i++)
2950 MimeInfoCacheDir *dir;
2952 dir = mime_info_cache_dir_new (dirs[i]);
2956 mime_info_cache_dir_init (dir);
2957 mime_info_cache_dir_init_defaults_list (dir);
2958 mime_info_cache_dir_init_mimeapps_list (dir);
2960 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2966 mime_info_cache_update_dir_lists (void)
2970 tmp = mime_info_cache->dirs;
2974 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2976 /* No need to do this if we had file monitors... */
2977 mime_info_cache_blow_global_cache ();
2978 mime_info_cache_dir_init (dir);
2979 mime_info_cache_dir_init_defaults_list (dir);
2980 mime_info_cache_dir_init_mimeapps_list (dir);
2987 mime_info_cache_init (void)
2989 G_LOCK (mime_info_cache);
2990 if (mime_info_cache == NULL)
2991 mime_info_cache_init_dir_lists ();
2997 if (now >= mime_info_cache->last_stat_time + 10)
2999 mime_info_cache_update_dir_lists ();
3000 mime_info_cache->last_stat_time = now;
3004 if (mime_info_cache->should_ping_mime_monitor)
3006 /* g_idle_add (emit_mime_changed, NULL); */
3007 mime_info_cache->should_ping_mime_monitor = FALSE;
3010 G_UNLOCK (mime_info_cache);
3013 static MimeInfoCache *
3014 mime_info_cache_new (void)
3016 MimeInfoCache *cache;
3018 cache = g_new0 (MimeInfoCache, 1);
3020 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
3021 (GDestroyNotify) g_free,
3022 (GDestroyNotify) g_free);
3027 mime_info_cache_free (MimeInfoCache *cache)
3032 g_list_foreach (cache->dirs,
3033 (GFunc) mime_info_cache_dir_free,
3035 g_list_free (cache->dirs);
3036 g_hash_table_destroy (cache->global_defaults_cache);
3041 * mime_info_cache_reload:
3042 * @dir: directory path which needs reloading.
3044 * Reload the mime information for the @dir.
3047 mime_info_cache_reload (const char *dir)
3049 /* FIXME: just reload the dir that needs reloading,
3050 * don't blow the whole cache
3052 if (mime_info_cache != NULL)
3054 G_LOCK (mime_info_cache);
3055 mime_info_cache_free (mime_info_cache);
3056 mime_info_cache = NULL;
3057 G_UNLOCK (mime_info_cache);
3062 append_desktop_entry (GList *list,
3063 const char *desktop_entry,
3064 GList *removed_entries)
3066 /* Add if not already in list, and valid */
3067 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
3068 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
3069 list = g_list_prepend (list, g_strdup (desktop_entry));
3075 * get_all_desktop_entries_for_mime_type:
3076 * @mime_type: a mime type.
3077 * @except: NULL or a strv list
3079 * Returns all the desktop ids for @mime_type. The desktop files
3080 * are listed in an order so that default applications are listed before
3081 * non-default ones, and handlers for inherited mimetypes are listed
3082 * after the base ones.
3084 * Optionally doesn't list the desktop ids given in the @except
3086 * Return value: a #GList containing the desktop ids which claim
3087 * to handle @mime_type.
3090 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
3091 const char **except,
3092 gboolean include_fallback,
3093 char **explicit_default)
3095 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
3096 MimeInfoCacheDir *dir;
3097 char *mime_type, *default_entry = NULL;
3100 char **default_entries;
3101 char **removed_associations;
3106 mime_info_cache_init ();
3108 if (include_fallback)
3110 /* collect all ancestors */
3111 mime_types = _g_unix_content_type_get_parents (base_mime_type);
3112 array = g_ptr_array_new ();
3113 for (i = 0; mime_types[i]; i++)
3114 g_ptr_array_add (array, mime_types[i]);
3115 g_free (mime_types);
3116 for (i = 0; i < array->len; i++)
3118 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
3119 for (j = 0; anc[j]; j++)
3121 for (k = 0; k < array->len; k++)
3123 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
3126 if (k == array->len) /* not found */
3127 g_ptr_array_add (array, g_strdup (anc[j]));
3131 g_ptr_array_add (array, NULL);
3132 mime_types = (char **)g_ptr_array_free (array, FALSE);
3136 mime_types = g_malloc0 (2 * sizeof (gchar *));
3137 mime_types[0] = g_strdup (base_mime_type);
3138 mime_types[1] = NULL;
3141 G_LOCK (mime_info_cache);
3143 removed_entries = NULL;
3144 desktop_entries = NULL;
3146 for (i = 0; except != NULL && except[i] != NULL; i++)
3147 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
3149 for (i = 0; mime_types[i] != NULL; i++)
3151 mime_type = mime_types[i];
3153 /* Go through all apps listed in user and system dirs */
3154 for (dir_list = mime_info_cache->dirs;
3156 dir_list = dir_list->next)
3158 dir = dir_list->data;
3160 /* Pick the explicit default application if we got no result earlier
3161 * (ie, for more specific mime types)
3163 if (desktop_entries == NULL)
3165 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
3169 /* Save the default entry if it's the first one we encounter */
3170 if (default_entry == NULL)
3171 default_entry = g_strdup (entry);
3175 /* Then added associations from mimeapps.list */
3176 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
3177 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3178 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3180 /* Then removed associations from mimeapps.list */
3181 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
3182 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
3183 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
3185 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
3186 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
3187 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3188 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3191 /* Go through all entries that support the mimetype */
3192 for (dir_list = mime_info_cache->dirs;
3194 dir_list = dir_list->next)
3196 dir = dir_list->data;
3198 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
3199 for (tmp = list; tmp != NULL; tmp = tmp->next)
3200 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
3204 G_UNLOCK (mime_info_cache);
3206 g_strfreev (mime_types);
3208 if (explicit_default != NULL)
3209 *explicit_default = default_entry;
3211 g_free (default_entry);
3213 g_list_foreach (removed_entries, (GFunc)g_free, NULL);
3214 g_list_free (removed_entries);
3216 desktop_entries = g_list_reverse (desktop_entries);
3218 return desktop_entries;
3221 /* GDesktopAppInfoLookup interface: */
3223 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
3224 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
3227 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
3232 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
3233 * @lookup: a #GDesktopAppInfoLookup
3234 * @uri_scheme: a string containing a URI scheme.
3236 * Gets the default application for launching applications
3237 * using this URI scheme for a particular GDesktopAppInfoLookup
3240 * The GDesktopAppInfoLookup interface and this function is used
3241 * to implement g_app_info_get_default_for_uri_scheme() backends
3242 * in a GIO module. There is no reason for applications to use it
3243 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
3245 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3247 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
3250 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
3251 const char *uri_scheme)
3253 GDesktopAppInfoLookupIface *iface;
3255 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
3257 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
3259 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);