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 ? */
111 guint startup_notify : 1;
113 /* FIXME: what about StartupWMClass ? */
117 UPDATE_MIME_NONE = 1 << 0,
118 UPDATE_MIME_SET_DEFAULT = 1 << 1,
119 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
120 UPDATE_MIME_REMOVE = 1 << 3,
121 UPDATE_MIME_SET_LAST_USED = 1 << 4,
124 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
125 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
126 g_desktop_app_info_iface_init))
129 search_path_init (gpointer data)
132 const char * const *data_dirs;
133 const char *user_data_dir;
136 data_dirs = g_get_system_data_dirs ();
137 length = g_strv_length ((char **) data_dirs);
139 args = g_new (char *, length + 2);
142 user_data_dir = g_get_user_data_dir ();
143 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
144 for (i = 0; i < length; i++)
145 args[j++] = g_build_filename (data_dirs[i],
146 "applications", NULL);
152 static const char * const *
153 get_applications_search_path (void)
155 static GOnce once_init = G_ONCE_INIT;
156 return g_once (&once_init, search_path_init, NULL);
160 g_desktop_app_info_finalize (GObject *object)
162 GDesktopAppInfo *info;
164 info = G_DESKTOP_APP_INFO (object);
166 g_free (info->desktop_id);
167 g_free (info->filename);
169 g_free (info->fullname);
170 g_free (info->comment);
171 g_free (info->icon_name);
173 g_object_unref (info->icon);
174 g_strfreev (info->only_show_in);
175 g_strfreev (info->not_show_in);
176 g_free (info->try_exec);
178 g_free (info->binary);
181 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
185 g_desktop_app_info_set_property(GObject *object,
190 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
195 self->filename = g_value_dup_string (value);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205 g_desktop_app_info_get_property(GObject *object,
210 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
215 g_value_set_string (value, self->filename);
218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
226 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
228 gobject_class->get_property = g_desktop_app_info_get_property;
229 gobject_class->set_property = g_desktop_app_info_set_property;
230 gobject_class->finalize = g_desktop_app_info_finalize;
233 * GDesktopAppInfo:filename
235 * The origin filename of this #GDesktopAppInfo
237 g_object_class_install_property (gobject_class,
239 g_param_spec_string ("filename", "Filename", "",
241 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
245 g_desktop_app_info_init (GDesktopAppInfo *local)
250 binary_from_exec (const char *exec)
252 const char *p, *start;
258 while (*p != ' ' && *p != 0)
261 return g_strndup (start, p - start);
266 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
273 start_group = g_key_file_get_start_group (key_file);
274 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
276 g_free (start_group);
279 g_free (start_group);
281 type = g_key_file_get_string (key_file,
282 G_KEY_FILE_DESKTOP_GROUP,
283 G_KEY_FILE_DESKTOP_KEY_TYPE,
285 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
292 try_exec = g_key_file_get_string (key_file,
293 G_KEY_FILE_DESKTOP_GROUP,
294 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
296 if (try_exec && try_exec[0] != '\0')
299 t = g_find_program_in_path (try_exec);
308 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
309 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
310 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
311 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
312 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
313 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);
314 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);
315 info->try_exec = try_exec;
316 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
317 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
318 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
319 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
320 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
321 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
326 if (g_path_is_absolute (info->icon_name))
330 file = g_file_new_for_path (info->icon_name);
331 info->icon = g_file_icon_new (file);
332 g_object_unref (file);
338 /* Work around a common mistake in desktop files */
339 if ((p = strrchr (info->icon_name, '.')) != NULL &&
340 (strcmp (p, ".png") == 0 ||
341 strcmp (p, ".xpm") == 0 ||
342 strcmp (p, ".svg") == 0))
345 info->icon = g_themed_icon_new (info->icon_name);
350 info->binary = binary_from_exec (info->exec);
352 if (info->path && info->path[0] == '\0')
362 g_desktop_app_info_load_file (GDesktopAppInfo *self)
365 gboolean retval = FALSE;
367 g_return_val_if_fail (self->filename != NULL, FALSE);
369 key_file = g_key_file_new ();
371 if (g_key_file_load_from_file (key_file,
376 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
379 g_key_file_free (key_file);
384 * g_desktop_app_info_new_from_keyfile:
385 * @key_file: an opened #GKeyFile
387 * Creates a new #GDesktopAppInfo.
389 * Returns: a new #GDesktopAppInfo or %NULL on error.
394 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
396 GDesktopAppInfo *info;
398 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
399 info->filename = NULL;
400 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
402 g_object_unref (info);
409 * g_desktop_app_info_new_from_filename:
410 * @filename: the path of a desktop file, in the GLib filename encoding
412 * Creates a new #GDesktopAppInfo.
414 * Returns: a new #GDesktopAppInfo or %NULL on error.
417 g_desktop_app_info_new_from_filename (const char *filename)
419 GDesktopAppInfo *info = NULL;
421 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
422 if (!g_desktop_app_info_load_file (info))
424 g_object_unref (info);
431 * g_desktop_app_info_new:
432 * @desktop_id: the desktop file id
434 * Creates a new #GDesktopAppInfo based on a desktop file id.
436 * A desktop file id is the basename of the desktop file, including the
437 * .desktop extension. GIO is looking for a desktop file with this name
438 * in the <filename>applications</filename> subdirectories of the XDG data
439 * directories (i.e. the directories specified in the
440 * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment
441 * variables). GIO also supports the prefix-to-subdirectory mapping that is
442 * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink>
443 * (i.e. a desktop id of kde-foo.desktop will match
444 * <filename>/usr/share/applications/kde/foo.desktop</filename>).
446 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
449 g_desktop_app_info_new (const char *desktop_id)
451 GDesktopAppInfo *appinfo;
452 const char * const *dirs;
456 dirs = get_applications_search_path ();
458 basename = g_strdup (desktop_id);
460 for (i = 0; dirs[i] != NULL; i++)
465 filename = g_build_filename (dirs[i], desktop_id, NULL);
466 appinfo = g_desktop_app_info_new_from_filename (filename);
472 while ((p = strchr (p, '-')) != NULL)
476 filename = g_build_filename (dirs[i], basename, NULL);
477 appinfo = g_desktop_app_info_new_from_filename (filename);
492 appinfo->desktop_id = g_strdup (desktop_id);
494 if (g_desktop_app_info_get_is_hidden (appinfo))
496 g_object_unref (appinfo);
504 g_desktop_app_info_dup (GAppInfo *appinfo)
506 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
507 GDesktopAppInfo *new_info;
509 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
511 new_info->filename = g_strdup (info->filename);
512 new_info->desktop_id = g_strdup (info->desktop_id);
514 new_info->name = g_strdup (info->name);
515 new_info->fullname = g_strdup (info->fullname);
516 new_info->comment = g_strdup (info->comment);
517 new_info->nodisplay = info->nodisplay;
518 new_info->icon_name = g_strdup (info->icon_name);
520 new_info->icon = g_object_ref (info->icon);
521 new_info->only_show_in = g_strdupv (info->only_show_in);
522 new_info->not_show_in = g_strdupv (info->not_show_in);
523 new_info->try_exec = g_strdup (info->try_exec);
524 new_info->exec = g_strdup (info->exec);
525 new_info->binary = g_strdup (info->binary);
526 new_info->path = g_strdup (info->path);
527 new_info->hidden = info->hidden;
528 new_info->terminal = info->terminal;
529 new_info->startup_notify = info->startup_notify;
531 return G_APP_INFO (new_info);
535 g_desktop_app_info_equal (GAppInfo *appinfo1,
538 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
539 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
541 if (info1->desktop_id == NULL ||
542 info2->desktop_id == NULL)
543 return info1 == info2;
545 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
549 g_desktop_app_info_get_id (GAppInfo *appinfo)
551 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
553 return info->desktop_id;
557 g_desktop_app_info_get_name (GAppInfo *appinfo)
559 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
561 if (info->name == NULL)
567 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
569 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
571 if (info->fullname == NULL)
572 return g_desktop_app_info_get_name (appinfo);
573 return info->fullname;
577 * g_desktop_app_info_get_is_hidden:
578 * @info: a #GDesktopAppInfo.
580 * A desktop file is hidden if the Hidden key in it is
583 * Returns: %TRUE if hidden, %FALSE otherwise.
586 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
592 * g_desktop_app_info_get_filename:
593 * @info: a #GDesktopAppInfo
595 * When @info was created from a known filename, return it. In some
596 * situations such as the #GDesktopAppInfo returned from
597 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
599 * Returns: The full path to the file for @info, or %NULL if not known.
603 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
605 return info->filename;
609 g_desktop_app_info_get_description (GAppInfo *appinfo)
611 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
613 return info->comment;
617 g_desktop_app_info_get_executable (GAppInfo *appinfo)
619 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
625 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
627 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
633 g_desktop_app_info_get_icon (GAppInfo *appinfo)
635 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
641 expand_macro_single (char macro, char *uri)
647 file = g_file_new_for_uri (uri);
648 path = g_file_get_path (file);
649 g_object_unref (file);
655 result = g_shell_quote (uri);
660 result = g_shell_quote (path);
666 name = g_path_get_dirname (path);
667 result = g_shell_quote (name);
675 name = g_path_get_basename (path);
676 result = g_shell_quote (name);
688 expand_macro (char macro,
690 GDesktopAppInfo *info,
693 GList *uris = *uri_list;
695 gboolean force_file_uri;
696 char force_file_uri_macro;
699 g_return_if_fail (exec != NULL);
701 /* On %u and %U, pass POSIX file path pointing to the URI via
702 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
703 * running or the URI doesn't have a POSIX file path via FUSE
704 * we'll just pass the URI.
706 force_file_uri_macro = macro;
707 force_file_uri = FALSE;
713 force_file_uri_macro = 'f';
714 force_file_uri = TRUE;
717 force_file_uri_macro = 'F';
718 force_file_uri = TRUE;
734 if (!force_file_uri ||
735 /* Pass URI if it contains an anchor */
736 strchr (uri, '#') != NULL)
738 expanded = expand_macro_single (macro, uri);
742 expanded = expand_macro_single (force_file_uri_macro, uri);
743 if (expanded == NULL)
744 expanded = expand_macro_single (macro, uri);
749 g_string_append (exec, expanded);
765 if (!force_file_uri ||
766 /* Pass URI if it contains an anchor */
767 strchr (uri, '#') != NULL)
769 expanded = expand_macro_single (macro, uri);
773 expanded = expand_macro_single (force_file_uri_macro, uri);
774 if (expanded == NULL)
775 expanded = expand_macro_single (macro, uri);
780 g_string_append (exec, expanded);
786 if (uris != NULL && expanded)
787 g_string_append_c (exec, ' ');
795 g_string_append (exec, "--icon ");
796 expanded = g_shell_quote (info->icon_name);
797 g_string_append (exec, expanded);
805 expanded = g_shell_quote (info->name);
806 g_string_append (exec, expanded);
814 expanded = g_shell_quote (info->filename);
815 g_string_append (exec, expanded);
820 case 'm': /* deprecated */
824 g_string_append_c (exec, '%');
832 expand_application_parameters (GDesktopAppInfo *info,
838 GList *uri_list = *uris;
839 const char *p = info->exec;
840 GString *expanded_exec;
843 if (info->exec == NULL)
845 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
846 _("Desktop file didn't specify Exec field"));
850 expanded_exec = g_string_new (NULL);
854 if (p[0] == '%' && p[1] != '\0')
856 expand_macro (p[1], expanded_exec, info, uris);
860 g_string_append_c (expanded_exec, *p);
865 /* No file substitutions */
866 if (uri_list == *uris && uri_list != NULL)
868 /* If there is no macro default to %f. This is also what KDE does */
869 g_string_append_c (expanded_exec, ' ');
870 expand_macro ('f', expanded_exec, info, uris);
873 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
874 g_string_free (expanded_exec, TRUE);
879 prepend_terminal_to_vector (int *argc,
886 char **term_argv = NULL;
891 g_return_val_if_fail (argc != NULL, FALSE);
892 g_return_val_if_fail (argv != NULL, FALSE);
900 /* compute size if not given */
903 for (i = 0; the_argv[i] != NULL; i++)
909 term_argv = g_new0 (char *, 3);
911 check = g_find_program_in_path ("gnome-terminal");
914 term_argv[0] = check;
915 /* Note that gnome-terminal takes -x and
916 * as -e in gnome-terminal is broken we use that. */
917 term_argv[1] = g_strdup ("-x");
922 check = g_find_program_in_path ("nxterm");
924 check = g_find_program_in_path ("color-xterm");
926 check = g_find_program_in_path ("rxvt");
928 check = g_find_program_in_path ("xterm");
930 check = g_find_program_in_path ("dtterm");
933 check = g_strdup ("xterm");
934 g_warning ("couldn't find a terminal, falling back to xterm");
936 term_argv[0] = check;
937 term_argv[1] = g_strdup ("-e");
940 real_argc = term_argc + *argc;
941 real_argv = g_new (char *, real_argc + 1);
943 for (i = 0; i < term_argc; i++)
944 real_argv[i] = term_argv[i];
946 for (j = 0; j < *argc; j++, i++)
947 real_argv[i] = (char *)the_argv[j];
955 /* we use g_free here as we sucked all the inner strings
956 * out from it into real_argv */
961 #endif /* G_OS_WIN32 */
965 create_files_for_uris (GList *uris)
972 for (iter = uris; iter; iter = iter->next)
974 GFile *file = g_file_new_for_uri ((char *)iter->data);
975 res = g_list_prepend (res, file);
978 return g_list_reverse (res);
983 GSpawnChildSetupFunc user_setup;
984 gpointer user_setup_data;
991 child_setup (gpointer user_data)
993 ChildSetupData *data = user_data;
996 g_setenv ("DISPLAY", data->display, TRUE);
999 g_setenv ("DESKTOP_STARTUP_ID", data->sn_id, TRUE);
1001 if (data->desktop_file)
1005 g_setenv ("GIO_LAUNCHED_DESKTOP_FILE", data->desktop_file, TRUE);
1007 g_snprintf (pid, 20, "%ld", (long)getpid ());
1008 g_setenv ("GIO_LAUNCHED_DESKTOP_FILE_PID", pid, TRUE);
1011 if (data->user_setup)
1012 data->user_setup (data->user_setup_data);
1016 notify_desktop_launch (GDBusConnection *session_bus,
1017 GDesktopAppInfo *info,
1019 const char *display,
1024 GVariantBuilder uri_variant;
1025 GVariantBuilder extras_variant;
1027 const char *desktop_file_id;
1028 const char *gio_desktop_file;
1030 if (session_bus == NULL)
1033 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
1034 for (iter = uris; iter; iter = iter->next)
1035 g_variant_builder_add (&uri_variant, "s", iter->data);
1037 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
1038 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
1039 g_variant_builder_add (&extras_variant, "{sv}",
1043 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
1044 if (gio_desktop_file != NULL)
1045 g_variant_builder_add (&extras_variant, "{sv}",
1046 "origin-desktop-file",
1047 g_variant_new_bytestring (gio_desktop_file));
1048 if (g_get_prgname () != NULL)
1049 g_variant_builder_add (&extras_variant, "{sv}",
1051 g_variant_new_bytestring (g_get_prgname ()));
1052 g_variant_builder_add (&extras_variant, "{sv}",
1055 (gint64)getpid ()));
1058 desktop_file_id = info->filename;
1059 else if (info->desktop_id)
1060 desktop_file_id = info->desktop_id;
1062 desktop_file_id = "";
1064 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
1065 "org.gtk.gio.DesktopAppInfo",
1067 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
1068 g_variant_new_bytestring (desktop_file_id),
1069 display ? display : "",
1073 g_dbus_connection_send_message (session_bus,
1077 g_object_unref (msg);
1080 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
1083 _g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
1085 GAppLaunchContext *launch_context,
1086 GSpawnFlags spawn_flags,
1087 GSpawnChildSetupFunc user_setup,
1088 gpointer user_setup_data,
1089 GDesktopAppLaunchCallback pid_callback,
1090 gpointer pid_callback_data,
1093 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1094 GDBusConnection *session_bus;
1095 gboolean completed = FALSE;
1099 ChildSetupData data;
1101 g_return_val_if_fail (appinfo != NULL, FALSE);
1105 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
1110 GList *launched_uris;
1114 if (!expand_application_parameters (info, &uris,
1115 &argc, &argv, error))
1118 /* Get the subset of URIs we're launching with this process */
1119 launched_uris = NULL;
1120 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
1121 launched_uris = g_list_prepend (launched_uris, iter->data);
1122 launched_uris = g_list_reverse (launched_uris);
1124 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
1126 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1127 _("Unable to find terminal required for application"));
1131 data.user_setup = user_setup;
1132 data.user_setup_data = user_setup_data;
1133 data.display = NULL;
1135 data.desktop_file = info->filename;
1139 GList *launched_files = create_files_for_uris (launched_uris);
1141 data.display = g_app_launch_context_get_display (launch_context,
1145 if (info->startup_notify)
1146 data.sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
1149 g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
1150 g_list_free (launched_files);
1153 if (!g_spawn_async (info->path,
1163 g_app_launch_context_launch_failed (launch_context, data.sn_id);
1165 g_free (data.sn_id);
1166 g_free (data.display);
1167 g_list_free (launched_uris);
1172 if (pid_callback != NULL)
1173 pid_callback (info, pid, pid_callback_data);
1175 notify_desktop_launch (session_bus,
1182 g_free (data.sn_id);
1183 g_free (data.display);
1184 g_list_free (launched_uris);
1189 while (uris != NULL);
1191 /* TODO - need to handle the process exiting immediately
1192 * after launching an app. See http://bugzilla.gnome.org/606960
1194 if (session_bus != NULL)
1196 /* This asynchronous flush holds a reference until it completes,
1197 * which ensures that the following unref won't immediately kill
1198 * the connection if we were the initial owner.
1200 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
1201 g_object_unref (session_bus);
1213 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
1215 GAppLaunchContext *launch_context,
1218 return _g_desktop_app_info_launch_uris_internal (appinfo, uris,
1220 _SPAWN_FLAGS_DEFAULT,
1221 NULL, NULL, NULL, NULL,
1226 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1228 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1230 return info->exec &&
1231 ((strstr (info->exec, "%u") != NULL) ||
1232 (strstr (info->exec, "%U") != NULL));
1236 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1238 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1240 return info->exec &&
1241 ((strstr (info->exec, "%f") != NULL) ||
1242 (strstr (info->exec, "%F") != NULL));
1246 g_desktop_app_info_launch (GAppInfo *appinfo,
1248 GAppLaunchContext *launch_context,
1258 uri = g_file_get_uri (files->data);
1259 uris = g_list_prepend (uris, uri);
1260 files = files->next;
1263 uris = g_list_reverse (uris);
1265 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1267 g_list_foreach (uris, (GFunc)g_free, NULL);
1274 * g_desktop_app_info_launch_uris_as_manager:
1275 * @appinfo: a #GDesktopAppInfo
1276 * @uris: (element-type utf8): List of URIs
1277 * @launch_context: a #GAppLaunchContext
1278 * @spawn_flags: #GSpawnFlags, used for each process
1279 * @user_setup: (scope call): a #GSpawnChildSetupFunc, used once for
1281 * @user_setup_data: (closure user_setup): User data for @user_setup
1282 * @pid_callback: (scope call): Callback for child processes
1283 * @pid_callback_data: (closure pid_callback): User data for @callback
1286 * This function performs the equivalent of g_app_info_launch_uris(),
1287 * but is intended primarily for operating system components that
1288 * launch applications. Ordinary applications should use
1289 * g_app_info_launch_uris().
1291 * In contrast to g_app_info_launch_uris(), all processes created will
1292 * always be run directly as children as if by the UNIX fork()/exec()
1295 * This guarantee allows additional control over the exact environment
1296 * of the child processes, which is provided via a setup function
1297 * @setup, as well as the process identifier of each child process via
1298 * @pid_callback. See g_spawn_async() for more information about the
1299 * semantics of the @setup function.
1302 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
1304 GAppLaunchContext *launch_context,
1305 GSpawnFlags spawn_flags,
1306 GSpawnChildSetupFunc user_setup,
1307 gpointer user_setup_data,
1308 GDesktopAppLaunchCallback pid_callback,
1309 gpointer pid_callback_data,
1312 return _g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
1323 G_LOCK_DEFINE_STATIC (g_desktop_env);
1324 static gchar *g_desktop_env = NULL;
1327 * g_desktop_app_info_set_desktop_env:
1328 * @desktop_env: a string specifying what desktop this is
1330 * Sets the name of the desktop that the application is running in.
1331 * This is used by g_app_info_should_show() to evaluate the
1332 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1333 * desktop entry fields.
1335 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1336 * Menu specification</ulink> recognizes the following:
1338 * <member>GNOME</member>
1339 * <member>KDE</member>
1340 * <member>ROX</member>
1341 * <member>XFCE</member>
1342 * <member>Old</member>
1345 * Should be called only once; subsequent calls are ignored.
1348 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1350 G_LOCK (g_desktop_env);
1352 g_desktop_env = g_strdup (desktop_env);
1353 G_UNLOCK (g_desktop_env);
1357 g_desktop_app_info_should_show (GAppInfo *appinfo)
1359 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1361 const gchar *desktop_env;
1364 if (info->nodisplay)
1367 G_LOCK (g_desktop_env);
1368 desktop_env = g_desktop_env;
1369 G_UNLOCK (g_desktop_env);
1371 if (info->only_show_in)
1373 if (desktop_env == NULL)
1377 for (i = 0; info->only_show_in[i] != NULL; i++)
1379 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1389 if (info->not_show_in && desktop_env)
1391 for (i = 0; info->not_show_in[i] != NULL; i++)
1393 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1407 ensure_dir (DirType type,
1410 char *path, *display_name;
1413 if (type == APP_DIR)
1414 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1416 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1419 if (g_mkdir_with_parents (path, 0700) == 0)
1423 display_name = g_filename_display_name (path);
1424 if (type == APP_DIR)
1425 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1426 _("Can't create user application configuration folder %s: %s"),
1427 display_name, g_strerror (errsv));
1429 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1430 _("Can't create user MIME configuration folder %s: %s"),
1431 display_name, g_strerror (errsv));
1433 g_free (display_name);
1440 update_mimeapps_list (const char *desktop_id,
1441 const char *content_type,
1442 UpdateMimeFlags flags,
1445 char *dirname, *filename, *string;
1447 gboolean load_succeeded, res, explicit_default;
1448 char **old_list, **list;
1450 gsize length, data_size;
1453 char **content_types;
1455 /* Don't add both at start and end */
1456 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
1457 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
1459 dirname = ensure_dir (APP_DIR, error);
1463 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1466 key_file = g_key_file_new ();
1467 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1468 if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1470 g_key_file_free (key_file);
1471 key_file = g_key_file_new ();
1476 content_types = g_new (char *, 2);
1477 content_types[0] = g_strdup (content_type);
1478 content_types[1] = NULL;
1482 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
1485 explicit_default = FALSE;
1487 for (k = 0; content_types && content_types[k]; k++)
1489 /* set as default, if requested so */
1490 string = g_key_file_get_string (key_file,
1491 DEFAULT_APPLICATIONS_GROUP,
1495 if (g_strcmp0 (string, desktop_id) != 0 &&
1496 (flags & UPDATE_MIME_SET_DEFAULT))
1499 string = g_strdup (desktop_id);
1501 /* add in the non-default list too, if it's not already there */
1502 flags |= UPDATE_MIME_SET_NON_DEFAULT;
1505 if (string == NULL || desktop_id == NULL)
1506 g_key_file_remove_key (key_file,
1507 DEFAULT_APPLICATIONS_GROUP,
1512 g_key_file_set_string (key_file,
1513 DEFAULT_APPLICATIONS_GROUP,
1517 explicit_default = TRUE;
1525 /* reuse the list from above */
1529 g_strfreev (content_types);
1530 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1533 for (k = 0; content_types && content_types[k]; k++)
1535 /* Add to the right place in the list */
1538 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1539 content_types[k], &length, NULL);
1541 list = g_new (char *, 1 + length + 1);
1545 /* if we're adding a last-used hint, just put the application in front of the list */
1546 if (flags & UPDATE_MIME_SET_LAST_USED)
1548 /* avoid adding this again as non-default later */
1549 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1550 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1552 list[i++] = g_strdup (desktop_id);
1557 for (j = 0; old_list[j] != NULL; j++)
1559 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1561 /* rewrite other entries if they're different from the new one */
1562 list[i++] = g_strdup (old_list[j]);
1564 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1566 /* we encountered an old entry which is equal to the one we're adding as non-default,
1567 * don't change its position in the list.
1569 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1570 list[i++] = g_strdup (old_list[j]);
1575 /* add it at the end of the list */
1576 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1577 list[i++] = g_strdup (desktop_id);
1581 g_strfreev (old_list);
1583 if (list[0] == NULL || desktop_id == NULL)
1584 g_key_file_remove_key (key_file,
1585 ADDED_ASSOCIATIONS_GROUP,
1590 g_key_file_set_string_list (key_file,
1591 ADDED_ASSOCIATIONS_GROUP,
1593 (const char * const *)list, i);
1595 /* if we had no explicit default set, we should add the system default to the
1596 * list, to avoid overriding it with applications from this list.
1598 if (!explicit_default)
1600 system_list = get_all_desktop_entries_for_mime_type (content_type, (const char **) list, FALSE, NULL);
1602 if (system_list != NULL)
1604 string = system_list->data;
1606 g_key_file_set_string (key_file,
1607 DEFAULT_APPLICATIONS_GROUP,
1612 g_list_free_full (system_list, g_free);
1621 /* reuse the list from above */
1625 g_strfreev (content_types);
1626 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1629 for (k = 0; content_types && content_types[k]; k++)
1631 /* Remove from removed associations group (unless remove) */
1634 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1635 content_types[k], &length, NULL);
1637 list = g_new (char *, 1 + length + 1);
1640 if (flags & UPDATE_MIME_REMOVE)
1641 list[i++] = g_strdup (desktop_id);
1644 for (j = 0; old_list[j] != NULL; j++)
1646 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1647 list[i++] = g_strdup (old_list[j]);
1652 g_strfreev (old_list);
1654 if (list[0] == NULL || desktop_id == NULL)
1655 g_key_file_remove_key (key_file,
1656 REMOVED_ASSOCIATIONS_GROUP,
1660 g_key_file_set_string_list (key_file,
1661 REMOVED_ASSOCIATIONS_GROUP,
1663 (const char * const *)list, i);
1668 g_strfreev (content_types);
1670 data = g_key_file_to_data (key_file, &data_size, error);
1671 g_key_file_free (key_file);
1673 res = g_file_set_contents (filename, data, data_size, error);
1675 mime_info_cache_reload (NULL);
1684 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
1685 const char *content_type,
1688 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1690 if (!g_desktop_app_info_ensure_saved (info, error))
1693 /* both add support for the content type and set as last used */
1694 return update_mimeapps_list (info->desktop_id, content_type,
1695 UPDATE_MIME_SET_NON_DEFAULT |
1696 UPDATE_MIME_SET_LAST_USED,
1701 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1702 const char *content_type,
1705 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1707 if (!g_desktop_app_info_ensure_saved (info, error))
1710 return update_mimeapps_list (info->desktop_id, content_type,
1711 UPDATE_MIME_SET_DEFAULT,
1716 update_program_done (GPid pid,
1720 /* Did the application exit correctly */
1721 if (WIFEXITED (status) &&
1722 WEXITSTATUS (status) == 0)
1724 /* Here we could clean out any caches in use */
1729 run_update_command (char *command,
1738 GError *error = NULL;
1741 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1743 if (g_spawn_async ("/", argv,
1745 G_SPAWN_SEARCH_PATH |
1746 G_SPAWN_STDOUT_TO_DEV_NULL |
1747 G_SPAWN_STDERR_TO_DEV_NULL |
1748 G_SPAWN_DO_NOT_REAP_CHILD,
1749 NULL, NULL, /* No setup function */
1752 g_child_watch_add (pid, update_program_done, NULL);
1755 /* If we get an error at this point, it's quite likely the user doesn't
1756 * have an installed copy of either 'update-mime-database' or
1757 * 'update-desktop-database'. I don't think we want to popup an error
1758 * dialog at this point, so we just do a g_warning to give the user a
1759 * chance of debugging it.
1761 g_warning ("%s", error->message);
1768 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1769 const char *extension,
1772 char *filename, *basename, *mimetype;
1776 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1779 dirname = ensure_dir (MIMETYPE_DIR, error);
1783 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1784 filename = g_build_filename (dirname, basename, NULL);
1788 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1790 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1795 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1796 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1797 " <mime-type type=\"%s\">\n"
1798 " <comment>%s document</comment>\n"
1799 " <glob pattern=\"*.%s\"/>\n"
1801 "</mime-info>\n", mimetype, extension, extension);
1803 g_file_set_contents (filename, contents, -1, NULL);
1806 run_update_command ("update-mime-database", "mime");
1810 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1820 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1821 const char *content_type,
1824 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1826 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1829 return update_mimeapps_list (info->desktop_id, content_type,
1830 UPDATE_MIME_SET_NON_DEFAULT,
1835 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1841 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1842 const char *content_type,
1845 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1847 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1850 return update_mimeapps_list (info->desktop_id, content_type,
1856 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1862 char *data, *desktop_id;
1867 if (info->filename != NULL)
1870 /* This is only used for object created with
1871 * g_app_info_create_from_commandline. All other
1872 * object should have a filename
1875 dirname = ensure_dir (APP_DIR, error);
1879 key_file = g_key_file_new ();
1881 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1882 "Encoding", "UTF-8");
1883 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1884 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1885 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1886 G_KEY_FILE_DESKTOP_KEY_TYPE,
1887 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1889 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1890 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1892 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1893 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1895 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1896 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1898 if (info->fullname != NULL)
1899 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1900 FULL_NAME_KEY, info->fullname);
1902 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1903 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1905 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1906 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1908 data = g_key_file_to_data (key_file, &data_size, NULL);
1909 g_key_file_free (key_file);
1911 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1912 filename = g_build_filename (dirname, desktop_id, NULL);
1913 g_free (desktop_id);
1916 fd = g_mkstemp (filename);
1921 display_name = g_filename_display_name (filename);
1922 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1923 _("Can't create user desktop file %s"), display_name);
1924 g_free (display_name);
1930 desktop_id = g_path_get_basename (filename);
1934 res = g_file_set_contents (filename, data, data_size, error);
1937 g_free (desktop_id);
1942 info->filename = filename;
1943 info->desktop_id = desktop_id;
1945 run_update_command ("update-desktop-database", "applications");
1951 g_desktop_app_info_can_delete (GAppInfo *appinfo)
1953 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1957 if (strstr (info->filename, "/userapp-"))
1958 return g_access (info->filename, W_OK) == 0;
1965 g_desktop_app_info_delete (GAppInfo *appinfo)
1967 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1971 if (g_remove (info->filename) == 0)
1973 update_mimeapps_list (info->desktop_id, NULL,
1977 g_free (info->filename);
1978 info->filename = NULL;
1979 g_free (info->desktop_id);
1980 info->desktop_id = NULL;
1990 * g_app_info_create_from_commandline:
1991 * @commandline: the commandline to use
1992 * @application_name: (allow-none): the application name, or %NULL to use @commandline
1993 * @flags: flags that can specify details of the created #GAppInfo
1994 * @error: a #GError location to store the error occuring, %NULL to ignore.
1996 * Creates a new #GAppInfo from the given information.
1998 * Returns: (transfer full): new #GAppInfo for given command.
2001 g_app_info_create_from_commandline (const char *commandline,
2002 const char *application_name,
2003 GAppInfoCreateFlags flags,
2008 GDesktopAppInfo *info;
2010 g_return_val_if_fail (commandline, NULL);
2012 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
2014 info->filename = NULL;
2015 info->desktop_id = NULL;
2017 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
2018 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
2019 info->hidden = FALSE;
2020 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
2021 info->exec = g_strconcat (commandline, " %u", NULL);
2023 info->exec = g_strconcat (commandline, " %f", NULL);
2024 info->nodisplay = TRUE;
2025 info->binary = binary_from_exec (info->exec);
2027 if (application_name)
2028 info->name = g_strdup (application_name);
2031 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
2032 split = g_strsplit (commandline, " ", 2);
2033 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
2035 info->name = basename;
2036 if (info->name == NULL)
2037 info->name = g_strdup ("custom");
2039 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
2041 return G_APP_INFO (info);
2045 g_desktop_app_info_iface_init (GAppInfoIface *iface)
2047 iface->dup = g_desktop_app_info_dup;
2048 iface->equal = g_desktop_app_info_equal;
2049 iface->get_id = g_desktop_app_info_get_id;
2050 iface->get_name = g_desktop_app_info_get_name;
2051 iface->get_description = g_desktop_app_info_get_description;
2052 iface->get_executable = g_desktop_app_info_get_executable;
2053 iface->get_icon = g_desktop_app_info_get_icon;
2054 iface->launch = g_desktop_app_info_launch;
2055 iface->supports_uris = g_desktop_app_info_supports_uris;
2056 iface->supports_files = g_desktop_app_info_supports_files;
2057 iface->launch_uris = g_desktop_app_info_launch_uris;
2058 iface->should_show = g_desktop_app_info_should_show;
2059 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
2060 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
2061 iface->add_supports_type = g_desktop_app_info_add_supports_type;
2062 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
2063 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
2064 iface->can_delete = g_desktop_app_info_can_delete;
2065 iface->do_delete = g_desktop_app_info_delete;
2066 iface->get_commandline = g_desktop_app_info_get_commandline;
2067 iface->get_display_name = g_desktop_app_info_get_display_name;
2068 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
2072 app_info_in_list (GAppInfo *info,
2075 while (list != NULL)
2077 if (g_app_info_equal (info, list->data))
2085 * g_app_info_get_recommended_for_type:
2086 * @content_type: the content type to find a #GAppInfo for
2088 * Gets a list of recommended #GAppInfos for a given content type, i.e.
2089 * those applications which claim to support the given content type exactly,
2090 * and not by MIME type subclassing.
2091 * Note that the first application of the list is the last used one, i.e.
2092 * the last one for which #g_app_info_set_as_last_used_for_type has been
2095 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2096 * for given @content_type or %NULL on error.
2101 g_app_info_get_recommended_for_type (const gchar *content_type)
2103 GList *desktop_entries, *l;
2105 GDesktopAppInfo *info;
2107 g_return_val_if_fail (content_type != NULL, NULL);
2109 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
2112 for (l = desktop_entries; l != NULL; l = l->next)
2114 char *desktop_entry = l->data;
2116 info = g_desktop_app_info_new (desktop_entry);
2119 if (app_info_in_list (G_APP_INFO (info), infos))
2120 g_object_unref (info);
2122 infos = g_list_prepend (infos, info);
2124 g_free (desktop_entry);
2127 g_list_free (desktop_entries);
2129 return g_list_reverse (infos);
2133 * g_app_info_get_fallback_for_type:
2134 * @content_type: the content type to find a #GAppInfo for
2136 * Gets a list of fallback #GAppInfos for a given content type, i.e.
2137 * those applications which claim to support the given content type
2138 * by MIME type subclassing and not directly.
2140 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2141 * for given @content_type or %NULL on error.
2146 g_app_info_get_fallback_for_type (const gchar *content_type)
2148 GList *desktop_entries, *l;
2149 GList *infos, *recommended_infos;
2150 GDesktopAppInfo *info;
2152 g_return_val_if_fail (content_type != NULL, NULL);
2154 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
2155 recommended_infos = g_app_info_get_recommended_for_type (content_type);
2158 for (l = desktop_entries; l != NULL; l = l->next)
2160 char *desktop_entry = l->data;
2162 info = g_desktop_app_info_new (desktop_entry);
2165 if (app_info_in_list (G_APP_INFO (info), infos) ||
2166 app_info_in_list (G_APP_INFO (info), recommended_infos))
2167 g_object_unref (info);
2169 infos = g_list_prepend (infos, info);
2171 g_free (desktop_entry);
2174 g_list_free (desktop_entries);
2175 g_list_free_full (recommended_infos, g_object_unref);
2177 return g_list_reverse (infos);
2181 * g_app_info_get_all_for_type:
2182 * @content_type: the content type to find a #GAppInfo for
2184 * Gets a list of all #GAppInfos for a given content type.
2186 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2187 * for given @content_type or %NULL on error.
2190 g_app_info_get_all_for_type (const char *content_type)
2192 GList *desktop_entries, *l;
2194 char *user_default = NULL;
2195 GDesktopAppInfo *info;
2197 g_return_val_if_fail (content_type != NULL, NULL);
2199 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2202 /* put the user default in front of the list, for compatibility */
2203 if (user_default != NULL)
2205 info = g_desktop_app_info_new (user_default);
2208 infos = g_list_prepend (infos, info);
2211 g_free (user_default);
2213 for (l = desktop_entries; l != NULL; l = l->next)
2215 char *desktop_entry = l->data;
2217 info = g_desktop_app_info_new (desktop_entry);
2220 if (app_info_in_list (G_APP_INFO (info), infos))
2221 g_object_unref (info);
2223 infos = g_list_prepend (infos, info);
2225 g_free (desktop_entry);
2228 g_list_free (desktop_entries);
2230 return g_list_reverse (infos);
2234 * g_app_info_reset_type_associations:
2235 * @content_type: a content type
2237 * Removes all changes to the type associations done by
2238 * g_app_info_set_as_default_for_type(),
2239 * g_app_info_set_as_default_for_extension(),
2240 * g_app_info_add_supports_type() or g_app_info_remove_supports_type().
2245 g_app_info_reset_type_associations (const char *content_type)
2247 update_mimeapps_list (NULL, content_type,
2253 * g_app_info_get_default_for_type:
2254 * @content_type: the content type to find a #GAppInfo for
2255 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
2258 * Gets the #GAppInfo that corresponds to a given content type.
2260 * Returns: (transfer full): #GAppInfo for given @content_type or
2264 g_app_info_get_default_for_type (const char *content_type,
2265 gboolean must_support_uris)
2267 GList *desktop_entries, *l;
2268 char *user_default = NULL;
2271 g_return_val_if_fail (content_type != NULL, NULL);
2273 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2277 if (user_default != NULL)
2279 info = (GAppInfo *) g_desktop_app_info_new (user_default);
2283 if (must_support_uris && !g_app_info_supports_uris (info))
2285 g_object_unref (info);
2291 g_free (user_default);
2295 g_list_free_full (desktop_entries, g_free);
2299 /* pick the first from the other list that matches our URI
2302 for (l = desktop_entries; l != NULL; l = l->next)
2304 char *desktop_entry = l->data;
2306 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
2309 if (must_support_uris && !g_app_info_supports_uris (info))
2311 g_object_unref (info);
2319 g_list_free_full (desktop_entries, g_free);
2325 * g_app_info_get_default_for_uri_scheme:
2326 * @uri_scheme: a string containing a URI scheme.
2328 * Gets the default application for launching applications
2329 * using this URI scheme. A URI scheme is the initial part
2330 * of the URI, up to but not including the ':', e.g. "http",
2333 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
2336 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
2339 char *content_type, *scheme_down;
2341 scheme_down = g_ascii_strdown (uri_scheme, -1);
2342 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
2343 g_free (scheme_down);
2344 app_info = g_app_info_get_default_for_type (content_type, FALSE);
2345 g_free (content_type);
2351 get_apps_from_dir (GHashTable *apps,
2352 const char *dirname,
2356 const char *basename;
2357 char *filename, *subprefix, *desktop_id;
2359 GDesktopAppInfo *appinfo;
2361 dir = g_dir_open (dirname, 0, NULL);
2364 while ((basename = g_dir_read_name (dir)) != NULL)
2366 filename = g_build_filename (dirname, basename, NULL);
2367 if (g_str_has_suffix (basename, ".desktop"))
2369 desktop_id = g_strconcat (prefix, basename, NULL);
2371 /* Use _extended so we catch NULLs too (hidden) */
2372 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
2374 appinfo = g_desktop_app_info_new_from_filename (filename);
2377 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
2379 g_object_unref (appinfo);
2384 if (appinfo || hidden)
2386 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
2390 /* Reuse instead of strdup here */
2391 appinfo->desktop_id = desktop_id;
2396 g_free (desktop_id);
2400 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
2402 subprefix = g_strconcat (prefix, basename, "-", NULL);
2403 get_apps_from_dir (apps, filename, subprefix);
2415 * g_app_info_get_all:
2417 * Gets a list of all of the applications currently registered
2420 * For desktop files, this includes applications that have
2421 * <literal>NoDisplay=true</literal> set or are excluded from
2422 * display by means of <literal>OnlyShowIn</literal> or
2423 * <literal>NotShowIn</literal>. See g_app_info_should_show().
2424 * The returned list does not include applications which have
2425 * the <literal>Hidden</literal> key set.
2427 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
2430 g_app_info_get_all (void)
2432 const char * const *dirs;
2434 GHashTableIter iter;
2439 dirs = get_applications_search_path ();
2441 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
2445 for (i = 0; dirs[i] != NULL; i++)
2446 get_apps_from_dir (apps, dirs[i], "");
2450 g_hash_table_iter_init (&iter, apps);
2451 while (g_hash_table_iter_next (&iter, NULL, &value))
2454 infos = g_list_prepend (infos, value);
2457 g_hash_table_destroy (apps);
2459 return g_list_reverse (infos);
2462 /* Cacheing of mimeinfo.cache and defaults.list files */
2466 GHashTable *mime_info_cache_map;
2467 GHashTable *defaults_list_map;
2468 GHashTable *mimeapps_list_added_map;
2469 GHashTable *mimeapps_list_removed_map;
2470 GHashTable *mimeapps_list_defaults_map;
2471 time_t mime_info_cache_timestamp;
2472 time_t defaults_list_timestamp;
2473 time_t mimeapps_list_timestamp;
2477 GList *dirs; /* mimeinfo.cache and defaults.list */
2478 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
2479 time_t last_stat_time;
2480 guint should_ping_mime_monitor : 1;
2483 static MimeInfoCache *mime_info_cache = NULL;
2484 G_LOCK_DEFINE_STATIC (mime_info_cache);
2486 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2487 const char *mime_type,
2488 char **new_desktop_file_ids);
2490 static MimeInfoCache * mime_info_cache_new (void);
2493 destroy_info_cache_value (gpointer key,
2497 g_list_foreach (value, (GFunc)g_free, NULL);
2498 g_list_free (value);
2502 destroy_info_cache_map (GHashTable *info_cache_map)
2504 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
2505 g_hash_table_destroy (info_cache_map);
2509 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2510 const char *cache_file,
2516 filename = g_build_filename (dir->path, cache_file, NULL);
2518 if (g_stat (filename, &buf) < 0)
2525 if (buf.st_mtime != *timestamp)
2531 /* Call with lock held */
2533 remove_all (gpointer key,
2542 mime_info_cache_blow_global_cache (void)
2544 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
2549 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2553 gchar *filename, **mime_types;
2560 if (dir->mime_info_cache_map != NULL &&
2561 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2562 &dir->mime_info_cache_timestamp))
2565 if (dir->mime_info_cache_map != NULL)
2566 destroy_info_cache_map (dir->mime_info_cache_map);
2568 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2569 (GDestroyNotify) g_free,
2572 key_file = g_key_file_new ();
2574 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2576 if (g_stat (filename, &buf) < 0)
2579 if (dir->mime_info_cache_timestamp > 0)
2580 mime_info_cache->should_ping_mime_monitor = TRUE;
2582 dir->mime_info_cache_timestamp = buf.st_mtime;
2584 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2589 if (load_error != NULL)
2592 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2595 if (load_error != NULL)
2598 for (i = 0; mime_types[i] != NULL; i++)
2600 gchar **desktop_file_ids;
2601 char *unaliased_type;
2602 desktop_file_ids = g_key_file_get_string_list (key_file,
2608 if (desktop_file_ids == NULL)
2611 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2612 mime_info_cache_dir_add_desktop_entries (dir,
2615 g_free (unaliased_type);
2617 g_strfreev (desktop_file_ids);
2620 g_strfreev (mime_types);
2621 g_key_file_free (key_file);
2626 g_key_file_free (key_file);
2628 if (mime_types != NULL)
2629 g_strfreev (mime_types);
2632 g_error_free (load_error);
2636 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2640 gchar *filename, **mime_types;
2641 char *unaliased_type;
2642 char **desktop_file_ids;
2649 if (dir->defaults_list_map != NULL &&
2650 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2651 &dir->defaults_list_timestamp))
2654 if (dir->defaults_list_map != NULL)
2655 g_hash_table_destroy (dir->defaults_list_map);
2656 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2657 g_free, (GDestroyNotify)g_strfreev);
2660 key_file = g_key_file_new ();
2662 filename = g_build_filename (dir->path, "defaults.list", NULL);
2663 if (g_stat (filename, &buf) < 0)
2666 if (dir->defaults_list_timestamp > 0)
2667 mime_info_cache->should_ping_mime_monitor = TRUE;
2669 dir->defaults_list_timestamp = buf.st_mtime;
2671 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2675 if (load_error != NULL)
2678 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2680 if (mime_types != NULL)
2682 for (i = 0; mime_types[i] != NULL; i++)
2684 desktop_file_ids = g_key_file_get_string_list (key_file,
2685 DEFAULT_APPLICATIONS_GROUP,
2689 if (desktop_file_ids == NULL)
2692 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2693 g_hash_table_replace (dir->defaults_list_map,
2698 g_strfreev (mime_types);
2701 g_key_file_free (key_file);
2706 g_key_file_free (key_file);
2708 if (mime_types != NULL)
2709 g_strfreev (mime_types);
2712 g_error_free (load_error);
2716 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2720 gchar *filename, **mime_types;
2721 char *unaliased_type;
2722 char **desktop_file_ids;
2730 if (dir->mimeapps_list_added_map != NULL &&
2731 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2732 &dir->mimeapps_list_timestamp))
2735 if (dir->mimeapps_list_added_map != NULL)
2736 g_hash_table_destroy (dir->mimeapps_list_added_map);
2737 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2738 g_free, (GDestroyNotify)g_strfreev);
2740 if (dir->mimeapps_list_removed_map != NULL)
2741 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2742 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2743 g_free, (GDestroyNotify)g_strfreev);
2745 if (dir->mimeapps_list_defaults_map != NULL)
2746 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2747 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2750 key_file = g_key_file_new ();
2752 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2753 if (g_stat (filename, &buf) < 0)
2756 if (dir->mimeapps_list_timestamp > 0)
2757 mime_info_cache->should_ping_mime_monitor = TRUE;
2759 dir->mimeapps_list_timestamp = buf.st_mtime;
2761 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2765 if (load_error != NULL)
2768 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2770 if (mime_types != NULL)
2772 for (i = 0; mime_types[i] != NULL; i++)
2774 desktop_file_ids = g_key_file_get_string_list (key_file,
2775 ADDED_ASSOCIATIONS_GROUP,
2779 if (desktop_file_ids == NULL)
2782 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2783 g_hash_table_replace (dir->mimeapps_list_added_map,
2788 g_strfreev (mime_types);
2791 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2793 if (mime_types != NULL)
2795 for (i = 0; mime_types[i] != NULL; i++)
2797 desktop_file_ids = g_key_file_get_string_list (key_file,
2798 REMOVED_ASSOCIATIONS_GROUP,
2802 if (desktop_file_ids == NULL)
2805 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2806 g_hash_table_replace (dir->mimeapps_list_removed_map,
2811 g_strfreev (mime_types);
2814 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2816 if (mime_types != NULL)
2818 for (i = 0; mime_types[i] != NULL; i++)
2820 desktop_id = g_key_file_get_string (key_file,
2821 DEFAULT_APPLICATIONS_GROUP,
2824 if (desktop_id == NULL)
2827 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2828 g_hash_table_replace (dir->mimeapps_list_defaults_map,
2833 g_strfreev (mime_types);
2836 g_key_file_free (key_file);
2841 g_key_file_free (key_file);
2843 if (mime_types != NULL)
2844 g_strfreev (mime_types);
2847 g_error_free (load_error);
2850 static MimeInfoCacheDir *
2851 mime_info_cache_dir_new (const char *path)
2853 MimeInfoCacheDir *dir;
2855 dir = g_new0 (MimeInfoCacheDir, 1);
2856 dir->path = g_strdup (path);
2862 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2867 if (dir->mime_info_cache_map != NULL)
2869 destroy_info_cache_map (dir->mime_info_cache_map);
2870 dir->mime_info_cache_map = NULL;
2874 if (dir->defaults_list_map != NULL)
2876 g_hash_table_destroy (dir->defaults_list_map);
2877 dir->defaults_list_map = NULL;
2880 if (dir->mimeapps_list_added_map != NULL)
2882 g_hash_table_destroy (dir->mimeapps_list_added_map);
2883 dir->mimeapps_list_added_map = NULL;
2886 if (dir->mimeapps_list_removed_map != NULL)
2888 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2889 dir->mimeapps_list_removed_map = NULL;
2892 if (dir->mimeapps_list_defaults_map != NULL)
2894 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2895 dir->mimeapps_list_defaults_map = NULL;
2902 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2903 const char *mime_type,
2904 char **new_desktop_file_ids)
2906 GList *desktop_file_ids;
2909 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2912 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2914 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
2915 desktop_file_ids = g_list_append (desktop_file_ids,
2916 g_strdup (new_desktop_file_ids[i]));
2919 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2923 mime_info_cache_init_dir_lists (void)
2925 const char * const *dirs;
2928 mime_info_cache = mime_info_cache_new ();
2930 dirs = get_applications_search_path ();
2932 for (i = 0; dirs[i] != NULL; i++)
2934 MimeInfoCacheDir *dir;
2936 dir = mime_info_cache_dir_new (dirs[i]);
2940 mime_info_cache_dir_init (dir);
2941 mime_info_cache_dir_init_defaults_list (dir);
2942 mime_info_cache_dir_init_mimeapps_list (dir);
2944 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2950 mime_info_cache_update_dir_lists (void)
2954 tmp = mime_info_cache->dirs;
2958 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2960 /* No need to do this if we had file monitors... */
2961 mime_info_cache_blow_global_cache ();
2962 mime_info_cache_dir_init (dir);
2963 mime_info_cache_dir_init_defaults_list (dir);
2964 mime_info_cache_dir_init_mimeapps_list (dir);
2971 mime_info_cache_init (void)
2973 G_LOCK (mime_info_cache);
2974 if (mime_info_cache == NULL)
2975 mime_info_cache_init_dir_lists ();
2981 if (now >= mime_info_cache->last_stat_time + 10)
2983 mime_info_cache_update_dir_lists ();
2984 mime_info_cache->last_stat_time = now;
2988 if (mime_info_cache->should_ping_mime_monitor)
2990 /* g_idle_add (emit_mime_changed, NULL); */
2991 mime_info_cache->should_ping_mime_monitor = FALSE;
2994 G_UNLOCK (mime_info_cache);
2997 static MimeInfoCache *
2998 mime_info_cache_new (void)
3000 MimeInfoCache *cache;
3002 cache = g_new0 (MimeInfoCache, 1);
3004 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
3005 (GDestroyNotify) g_free,
3006 (GDestroyNotify) g_free);
3011 mime_info_cache_free (MimeInfoCache *cache)
3016 g_list_foreach (cache->dirs,
3017 (GFunc) mime_info_cache_dir_free,
3019 g_list_free (cache->dirs);
3020 g_hash_table_destroy (cache->global_defaults_cache);
3025 * mime_info_cache_reload:
3026 * @dir: directory path which needs reloading.
3028 * Reload the mime information for the @dir.
3031 mime_info_cache_reload (const char *dir)
3033 /* FIXME: just reload the dir that needs reloading,
3034 * don't blow the whole cache
3036 if (mime_info_cache != NULL)
3038 G_LOCK (mime_info_cache);
3039 mime_info_cache_free (mime_info_cache);
3040 mime_info_cache = NULL;
3041 G_UNLOCK (mime_info_cache);
3046 append_desktop_entry (GList *list,
3047 const char *desktop_entry,
3048 GList *removed_entries)
3050 /* Add if not already in list, and valid */
3051 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
3052 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
3053 list = g_list_prepend (list, g_strdup (desktop_entry));
3059 * get_all_desktop_entries_for_mime_type:
3060 * @mime_type: a mime type.
3061 * @except: NULL or a strv list
3063 * Returns all the desktop ids for @mime_type. The desktop files
3064 * are listed in an order so that default applications are listed before
3065 * non-default ones, and handlers for inherited mimetypes are listed
3066 * after the base ones.
3068 * Optionally doesn't list the desktop ids given in the @except
3070 * Return value: a #GList containing the desktop ids which claim
3071 * to handle @mime_type.
3074 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
3075 const char **except,
3076 gboolean include_fallback,
3077 char **explicit_default)
3079 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
3080 MimeInfoCacheDir *dir;
3081 char *mime_type, *default_entry = NULL;
3084 char **default_entries;
3085 char **removed_associations;
3090 mime_info_cache_init ();
3092 if (include_fallback)
3094 /* collect all ancestors */
3095 mime_types = _g_unix_content_type_get_parents (base_mime_type);
3096 array = g_ptr_array_new ();
3097 for (i = 0; mime_types[i]; i++)
3098 g_ptr_array_add (array, mime_types[i]);
3099 g_free (mime_types);
3100 for (i = 0; i < array->len; i++)
3102 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
3103 for (j = 0; anc[j]; j++)
3105 for (k = 0; k < array->len; k++)
3107 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
3110 if (k == array->len) /* not found */
3111 g_ptr_array_add (array, g_strdup (anc[j]));
3115 g_ptr_array_add (array, NULL);
3116 mime_types = (char **)g_ptr_array_free (array, FALSE);
3120 mime_types = g_malloc0 (2 * sizeof (gchar *));
3121 mime_types[0] = g_strdup (base_mime_type);
3122 mime_types[1] = NULL;
3125 G_LOCK (mime_info_cache);
3127 removed_entries = NULL;
3128 desktop_entries = NULL;
3130 for (i = 0; except != NULL && except[i] != NULL; i++)
3131 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
3133 for (i = 0; mime_types[i] != NULL; i++)
3135 mime_type = mime_types[i];
3137 /* Go through all apps listed in user and system dirs */
3138 for (dir_list = mime_info_cache->dirs;
3140 dir_list = dir_list->next)
3142 dir = dir_list->data;
3144 /* Pick the explicit default application if we got no result earlier
3145 * (ie, for more specific mime types)
3147 if (desktop_entries == NULL)
3149 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
3153 /* Save the default entry if it's the first one we encounter */
3154 if (default_entry == NULL)
3155 default_entry = g_strdup (entry);
3159 /* Then added associations from mimeapps.list */
3160 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
3161 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3162 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3164 /* Then removed associations from mimeapps.list */
3165 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
3166 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
3167 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
3169 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
3170 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
3171 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3172 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3175 /* Go through all entries that support the mimetype */
3176 for (dir_list = mime_info_cache->dirs;
3178 dir_list = dir_list->next)
3180 dir = dir_list->data;
3182 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
3183 for (tmp = list; tmp != NULL; tmp = tmp->next)
3184 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
3188 G_UNLOCK (mime_info_cache);
3190 g_strfreev (mime_types);
3192 if (explicit_default != NULL)
3193 *explicit_default = default_entry;
3195 g_free (default_entry);
3197 g_list_foreach (removed_entries, (GFunc)g_free, NULL);
3198 g_list_free (removed_entries);
3200 desktop_entries = g_list_reverse (desktop_entries);
3202 return desktop_entries;
3205 /* GDesktopAppInfoLookup interface: */
3207 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
3208 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
3211 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
3216 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
3217 * @lookup: a #GDesktopAppInfoLookup
3218 * @uri_scheme: a string containing a URI scheme.
3220 * Gets the default application for launching applications
3221 * using this URI scheme for a particular GDesktopAppInfoLookup
3224 * The GDesktopAppInfoLookup interface and this function is used
3225 * to implement g_app_info_get_default_for_uri_scheme() backends
3226 * in a GIO module. There is no reason for applications to use it
3227 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
3229 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3231 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
3234 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
3235 const char *uri_scheme)
3237 GDesktopAppInfoLookupIface *iface;
3239 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
3241 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
3243 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);