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"
69 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
70 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type,
72 gboolean include_fallback,
73 char **explicit_default);
74 static void mime_info_cache_reload (const char *dir);
75 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
81 * Information about an installed application from a desktop file.
83 struct _GDesktopAppInfo
85 GObject parent_instance;
91 /* FIXME: what about GenericName ? */
106 guint startup_notify : 1;
108 /* FIXME: what about StartupWMClass ? */
112 UPDATE_MIME_NONE = 1 << 0,
113 UPDATE_MIME_SET_DEFAULT = 1 << 1,
114 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
115 UPDATE_MIME_REMOVE = 1 << 3,
116 UPDATE_MIME_SET_LAST_USED = 1 << 4,
119 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
120 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
121 g_desktop_app_info_iface_init))
124 search_path_init (gpointer data)
127 const char * const *data_dirs;
128 const char *user_data_dir;
131 data_dirs = g_get_system_data_dirs ();
132 length = g_strv_length ((char **) data_dirs);
134 args = g_new (char *, length + 2);
137 user_data_dir = g_get_user_data_dir ();
138 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
139 for (i = 0; i < length; i++)
140 args[j++] = g_build_filename (data_dirs[i],
141 "applications", NULL);
147 static const char * const *
148 get_applications_search_path (void)
150 static GOnce once_init = G_ONCE_INIT;
151 return g_once (&once_init, search_path_init, NULL);
155 g_desktop_app_info_finalize (GObject *object)
157 GDesktopAppInfo *info;
159 info = G_DESKTOP_APP_INFO (object);
161 g_free (info->desktop_id);
162 g_free (info->filename);
164 g_free (info->fullname);
165 g_free (info->comment);
166 g_free (info->icon_name);
168 g_object_unref (info->icon);
169 g_strfreev (info->only_show_in);
170 g_strfreev (info->not_show_in);
171 g_free (info->try_exec);
173 g_free (info->binary);
176 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
180 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
182 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
184 gobject_class->finalize = g_desktop_app_info_finalize;
188 g_desktop_app_info_init (GDesktopAppInfo *local)
193 binary_from_exec (const char *exec)
195 const char *p, *start;
201 while (*p != ' ' && *p != 0)
204 return g_strndup (start, p - start);
209 * g_desktop_app_info_new_from_keyfile:
210 * @key_file: an opened #GKeyFile
212 * Creates a new #GDesktopAppInfo.
214 * Returns: a new #GDesktopAppInfo or %NULL on error.
219 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
221 GDesktopAppInfo *info;
226 start_group = g_key_file_get_start_group (key_file);
227 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
229 g_free (start_group);
232 g_free (start_group);
234 type = g_key_file_get_string (key_file,
235 G_KEY_FILE_DESKTOP_GROUP,
236 G_KEY_FILE_DESKTOP_KEY_TYPE,
238 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
245 try_exec = g_key_file_get_string (key_file,
246 G_KEY_FILE_DESKTOP_GROUP,
247 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
249 if (try_exec && try_exec[0] != '\0')
252 t = g_find_program_in_path (try_exec);
261 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
262 info->filename = NULL;
264 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
265 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
266 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
267 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
268 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
269 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);
270 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);
271 info->try_exec = try_exec;
272 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
273 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
274 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
275 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
276 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
277 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
282 if (g_path_is_absolute (info->icon_name))
286 file = g_file_new_for_path (info->icon_name);
287 info->icon = g_file_icon_new (file);
288 g_object_unref (file);
294 /* Work around a common mistake in desktop files */
295 if ((p = strrchr (info->icon_name, '.')) != NULL &&
296 (strcmp (p, ".png") == 0 ||
297 strcmp (p, ".xpm") == 0 ||
298 strcmp (p, ".svg") == 0))
301 info->icon = g_themed_icon_new (info->icon_name);
306 info->binary = binary_from_exec (info->exec);
308 if (info->path && info->path[0] == '\0')
318 * g_desktop_app_info_new_from_filename:
319 * @filename: the path of a desktop file, in the GLib filename encoding
321 * Creates a new #GDesktopAppInfo.
323 * Returns: a new #GDesktopAppInfo or %NULL on error.
326 g_desktop_app_info_new_from_filename (const char *filename)
329 GDesktopAppInfo *info = NULL;
331 key_file = g_key_file_new ();
333 if (g_key_file_load_from_file (key_file,
338 info = g_desktop_app_info_new_from_keyfile (key_file);
340 info->filename = g_strdup (filename);
343 g_key_file_free (key_file);
349 * g_desktop_app_info_new:
350 * @desktop_id: the desktop file id
352 * Creates a new #GDesktopAppInfo based on a desktop file id.
354 * A desktop file id is the basename of the desktop file, including the
355 * .desktop extension. GIO is looking for a desktop file with this name
356 * in the <filename>applications</filename> subdirectories of the XDG data
357 * directories (i.e. the directories specified in the
358 * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment
359 * variables). GIO also supports the prefix-to-subdirectory mapping that is
360 * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink>
361 * (i.e. a desktop id of kde-foo.desktop will match
362 * <filename>/usr/share/applications/kde/foo.desktop</filename>).
364 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
367 g_desktop_app_info_new (const char *desktop_id)
369 GDesktopAppInfo *appinfo;
370 const char * const *dirs;
374 dirs = get_applications_search_path ();
376 basename = g_strdup (desktop_id);
378 for (i = 0; dirs[i] != NULL; i++)
383 filename = g_build_filename (dirs[i], desktop_id, NULL);
384 appinfo = g_desktop_app_info_new_from_filename (filename);
390 while ((p = strchr (p, '-')) != NULL)
394 filename = g_build_filename (dirs[i], basename, NULL);
395 appinfo = g_desktop_app_info_new_from_filename (filename);
410 appinfo->desktop_id = g_strdup (desktop_id);
412 if (g_desktop_app_info_get_is_hidden (appinfo))
414 g_object_unref (appinfo);
422 g_desktop_app_info_dup (GAppInfo *appinfo)
424 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
425 GDesktopAppInfo *new_info;
427 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
429 new_info->filename = g_strdup (info->filename);
430 new_info->desktop_id = g_strdup (info->desktop_id);
432 new_info->name = g_strdup (info->name);
433 new_info->fullname = g_strdup (info->fullname);
434 new_info->comment = g_strdup (info->comment);
435 new_info->nodisplay = info->nodisplay;
436 new_info->icon_name = g_strdup (info->icon_name);
438 new_info->icon = g_object_ref (info->icon);
439 new_info->only_show_in = g_strdupv (info->only_show_in);
440 new_info->not_show_in = g_strdupv (info->not_show_in);
441 new_info->try_exec = g_strdup (info->try_exec);
442 new_info->exec = g_strdup (info->exec);
443 new_info->binary = g_strdup (info->binary);
444 new_info->path = g_strdup (info->path);
445 new_info->hidden = info->hidden;
446 new_info->terminal = info->terminal;
447 new_info->startup_notify = info->startup_notify;
449 return G_APP_INFO (new_info);
453 g_desktop_app_info_equal (GAppInfo *appinfo1,
456 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
457 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
459 if (info1->desktop_id == NULL ||
460 info2->desktop_id == NULL)
461 return info1 == info2;
463 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
467 g_desktop_app_info_get_id (GAppInfo *appinfo)
469 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
471 return info->desktop_id;
475 g_desktop_app_info_get_name (GAppInfo *appinfo)
477 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
479 if (info->name == NULL)
485 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
487 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
489 if (info->fullname == NULL)
490 return g_desktop_app_info_get_name (appinfo);
491 return info->fullname;
495 * g_desktop_app_info_get_is_hidden:
496 * @info: a #GDesktopAppInfo.
498 * A desktop file is hidden if the Hidden key in it is
501 * Returns: %TRUE if hidden, %FALSE otherwise.
504 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
510 * g_desktop_app_info_get_filename:
511 * @info: a #GDesktopAppInfo
513 * When @info was created from a known filename, return it. In some
514 * situations such as the #GDesktopAppInfo returned from
515 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
517 * Returns: The full path to the file for @info, or %NULL if not known.
521 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
523 return info->filename;
527 g_desktop_app_info_get_description (GAppInfo *appinfo)
529 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
531 return info->comment;
535 g_desktop_app_info_get_executable (GAppInfo *appinfo)
537 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
543 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
545 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
551 g_desktop_app_info_get_icon (GAppInfo *appinfo)
553 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
559 expand_macro_single (char macro, char *uri)
565 file = g_file_new_for_uri (uri);
566 path = g_file_get_path (file);
567 g_object_unref (file);
573 result = g_shell_quote (uri);
578 result = g_shell_quote (path);
584 name = g_path_get_dirname (path);
585 result = g_shell_quote (name);
593 name = g_path_get_basename (path);
594 result = g_shell_quote (name);
606 expand_macro (char macro,
608 GDesktopAppInfo *info,
611 GList *uris = *uri_list;
613 gboolean force_file_uri;
614 char force_file_uri_macro;
617 g_return_if_fail (exec != NULL);
619 /* On %u and %U, pass POSIX file path pointing to the URI via
620 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
621 * running or the URI doesn't have a POSIX file path via FUSE
622 * we'll just pass the URI.
624 force_file_uri_macro = macro;
625 force_file_uri = FALSE;
631 force_file_uri_macro = 'f';
632 force_file_uri = TRUE;
635 force_file_uri_macro = 'F';
636 force_file_uri = TRUE;
652 if (!force_file_uri ||
653 /* Pass URI if it contains an anchor */
654 strchr (uri, '#') != NULL)
656 expanded = expand_macro_single (macro, uri);
660 expanded = expand_macro_single (force_file_uri_macro, uri);
661 if (expanded == NULL)
662 expanded = expand_macro_single (macro, uri);
667 g_string_append (exec, expanded);
683 if (!force_file_uri ||
684 /* Pass URI if it contains an anchor */
685 strchr (uri, '#') != NULL)
687 expanded = expand_macro_single (macro, uri);
691 expanded = expand_macro_single (force_file_uri_macro, uri);
692 if (expanded == NULL)
693 expanded = expand_macro_single (macro, uri);
698 g_string_append (exec, expanded);
704 if (uris != NULL && expanded)
705 g_string_append_c (exec, ' ');
713 g_string_append (exec, "--icon ");
714 expanded = g_shell_quote (info->icon_name);
715 g_string_append (exec, expanded);
723 expanded = g_shell_quote (info->name);
724 g_string_append (exec, expanded);
732 expanded = g_shell_quote (info->filename);
733 g_string_append (exec, expanded);
738 case 'm': /* deprecated */
742 g_string_append_c (exec, '%');
750 expand_application_parameters (GDesktopAppInfo *info,
756 GList *uri_list = *uris;
757 const char *p = info->exec;
758 GString *expanded_exec;
761 if (info->exec == NULL)
763 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
764 _("Desktop file didn't specify Exec field"));
768 expanded_exec = g_string_new (NULL);
772 if (p[0] == '%' && p[1] != '\0')
774 expand_macro (p[1], expanded_exec, info, uris);
778 g_string_append_c (expanded_exec, *p);
783 /* No file substitutions */
784 if (uri_list == *uris && uri_list != NULL)
786 /* If there is no macro default to %f. This is also what KDE does */
787 g_string_append_c (expanded_exec, ' ');
788 expand_macro ('f', expanded_exec, info, uris);
791 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
792 g_string_free (expanded_exec, TRUE);
797 prepend_terminal_to_vector (int *argc,
804 char **term_argv = NULL;
809 g_return_val_if_fail (argc != NULL, FALSE);
810 g_return_val_if_fail (argv != NULL, FALSE);
818 /* compute size if not given */
821 for (i = 0; the_argv[i] != NULL; i++)
827 term_argv = g_new0 (char *, 3);
829 check = g_find_program_in_path ("gnome-terminal");
832 term_argv[0] = check;
833 /* Note that gnome-terminal takes -x and
834 * as -e in gnome-terminal is broken we use that. */
835 term_argv[1] = g_strdup ("-x");
840 check = g_find_program_in_path ("nxterm");
842 check = g_find_program_in_path ("color-xterm");
844 check = g_find_program_in_path ("rxvt");
846 check = g_find_program_in_path ("xterm");
848 check = g_find_program_in_path ("dtterm");
851 check = g_strdup ("xterm");
852 g_warning ("couldn't find a terminal, falling back to xterm");
854 term_argv[0] = check;
855 term_argv[1] = g_strdup ("-e");
858 real_argc = term_argc + *argc;
859 real_argv = g_new (char *, real_argc + 1);
861 for (i = 0; i < term_argc; i++)
862 real_argv[i] = term_argv[i];
864 for (j = 0; j < *argc; j++, i++)
865 real_argv[i] = (char *)the_argv[j];
873 /* we use g_free here as we sucked all the inner strings
874 * out from it into real_argv */
879 #endif /* G_OS_WIN32 */
883 create_files_for_uris (GList *uris)
890 for (iter = uris; iter; iter = iter->next)
892 GFile *file = g_file_new_for_uri ((char *)iter->data);
893 res = g_list_prepend (res, file);
896 return g_list_reverse (res);
901 GSpawnChildSetupFunc user_setup;
902 gpointer user_setup_data;
909 child_setup (gpointer user_data)
911 ChildSetupData *data = user_data;
914 g_setenv ("DISPLAY", data->display, TRUE);
917 g_setenv ("DESKTOP_STARTUP_ID", data->sn_id, TRUE);
919 if (data->desktop_file)
923 g_setenv ("GIO_LAUNCHED_DESKTOP_FILE", data->desktop_file, TRUE);
925 g_snprintf (pid, 20, "%ld", (long)getpid ());
926 g_setenv ("GIO_LAUNCHED_DESKTOP_FILE_PID", pid, TRUE);
929 if (data->user_setup)
930 data->user_setup (data->user_setup_data);
934 notify_desktop_launch (GDBusConnection *session_bus,
935 GDesktopAppInfo *info,
942 GVariantBuilder uri_variant;
943 GVariantBuilder extras_variant;
945 const char *desktop_file_id;
946 const char *gio_desktop_file;
948 if (session_bus == NULL)
951 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
952 for (iter = uris; iter; iter = iter->next)
953 g_variant_builder_add (&uri_variant, "s", iter->data);
955 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
956 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
957 g_variant_builder_add (&extras_variant, "{sv}",
961 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
962 if (gio_desktop_file != NULL)
963 g_variant_builder_add (&extras_variant, "{sv}",
964 "origin-desktop-file",
965 g_variant_new_bytestring (gio_desktop_file));
966 g_variant_builder_add (&extras_variant, "{sv}",
968 g_variant_new_bytestring (g_get_prgname ()));
969 g_variant_builder_add (&extras_variant, "{sv}",
975 desktop_file_id = info->filename;
976 else if (info->desktop_id)
977 desktop_file_id = info->desktop_id;
979 desktop_file_id = "";
981 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
982 "org.gtk.gio.DesktopAppInfo",
984 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
985 g_variant_new_bytestring (desktop_file_id),
986 display ? display : "",
990 g_dbus_connection_send_message (session_bus,
994 g_object_unref (msg);
997 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
1000 _g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
1002 GAppLaunchContext *launch_context,
1003 GSpawnFlags spawn_flags,
1004 GSpawnChildSetupFunc user_setup,
1005 gpointer user_setup_data,
1006 GDesktopAppLaunchCallback pid_callback,
1007 gpointer pid_callback_data,
1010 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1011 GDBusConnection *session_bus;
1012 gboolean completed = FALSE;
1016 ChildSetupData data;
1018 g_return_val_if_fail (appinfo != NULL, FALSE);
1022 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
1027 GList *launched_uris;
1031 if (!expand_application_parameters (info, &uris,
1032 &argc, &argv, error))
1035 /* Get the subset of URIs we're launching with this process */
1036 launched_uris = NULL;
1037 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
1038 launched_uris = g_list_prepend (launched_uris, iter->data);
1039 launched_uris = g_list_reverse (launched_uris);
1041 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
1043 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1044 _("Unable to find terminal required for application"));
1048 data.user_setup = user_setup;
1049 data.user_setup_data = user_setup_data;
1050 data.display = NULL;
1052 data.desktop_file = info->filename;
1056 GList *launched_files = create_files_for_uris (launched_uris);
1058 data.display = g_app_launch_context_get_display (launch_context,
1062 if (info->startup_notify)
1063 data.sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
1066 g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
1067 g_list_free (launched_files);
1070 if (!g_spawn_async (info->path,
1080 g_app_launch_context_launch_failed (launch_context, data.sn_id);
1082 g_free (data.sn_id);
1083 g_free (data.display);
1084 g_list_free (launched_uris);
1089 if (pid_callback != NULL)
1090 pid_callback (info, pid, pid_callback_data);
1092 notify_desktop_launch (session_bus,
1099 g_free (data.sn_id);
1100 g_free (data.display);
1101 g_list_free (launched_uris);
1106 while (uris != NULL);
1108 /* TODO - need to handle the process exiting immediately
1109 * after launching an app. See http://bugzilla.gnome.org/606960
1111 if (session_bus != NULL)
1113 /* This asynchronous flush holds a reference until it completes,
1114 * which ensures that the following unref won't immediately kill
1115 * the connection if we were the initial owner.
1117 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
1118 g_object_unref (session_bus);
1130 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
1132 GAppLaunchContext *launch_context,
1135 return _g_desktop_app_info_launch_uris_internal (appinfo, uris,
1137 _SPAWN_FLAGS_DEFAULT,
1138 NULL, NULL, NULL, NULL,
1143 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1145 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1147 return info->exec &&
1148 ((strstr (info->exec, "%u") != NULL) ||
1149 (strstr (info->exec, "%U") != NULL));
1153 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1155 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1157 return info->exec &&
1158 ((strstr (info->exec, "%f") != NULL) ||
1159 (strstr (info->exec, "%F") != NULL));
1163 g_desktop_app_info_launch (GAppInfo *appinfo,
1165 GAppLaunchContext *launch_context,
1175 uri = g_file_get_uri (files->data);
1176 uris = g_list_prepend (uris, uri);
1177 files = files->next;
1180 uris = g_list_reverse (uris);
1182 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1184 g_list_foreach (uris, (GFunc)g_free, NULL);
1191 * g_desktop_app_info_launch_uris_as_manager:
1192 * @appinfo: a #GDesktopAppInfo
1193 * @uris: (element-type utf8): List of URIs
1194 * @launch_context: a #GAppLaunchContext
1195 * @spawn_flags: #GSpawnFlags, used for each process
1196 * @user_setup: (scope call): a #GSpawnChildSetupFunc, used once for
1198 * @user_setup_data: (closure user_setup): User data for @user_setup
1199 * @pid_callback: (scope call): Callback for child processes
1200 * @pid_callback_data: (closure pid_callback): User data for @callback
1203 * This function performs the equivalent of g_app_info_launch_uris(),
1204 * but is intended primarily for operating system components that
1205 * launch applications. Ordinary applications should use
1206 * g_app_info_launch_uris().
1208 * In contrast to g_app_info_launch_uris(), all processes created will
1209 * always be run directly as children as if by the UNIX fork()/exec()
1212 * This guarantee allows additional control over the exact environment
1213 * of the child processes, which is provided via a setup function
1214 * @setup, as well as the process identifier of each child process via
1215 * @pid_callback. See g_spawn_async() for more information about the
1216 * semantics of the @setup function.
1219 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
1221 GAppLaunchContext *launch_context,
1222 GSpawnFlags spawn_flags,
1223 GSpawnChildSetupFunc user_setup,
1224 gpointer user_setup_data,
1225 GDesktopAppLaunchCallback pid_callback,
1226 gpointer pid_callback_data,
1229 return _g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
1240 G_LOCK_DEFINE_STATIC (g_desktop_env);
1241 static gchar *g_desktop_env = NULL;
1244 * g_desktop_app_info_set_desktop_env:
1245 * @desktop_env: a string specifying what desktop this is
1247 * Sets the name of the desktop that the application is running in.
1248 * This is used by g_app_info_should_show() to evaluate the
1249 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1250 * desktop entry fields.
1252 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1253 * Menu specification</ulink> recognizes the following:
1255 * <member>GNOME</member>
1256 * <member>KDE</member>
1257 * <member>ROX</member>
1258 * <member>XFCE</member>
1259 * <member>Old</member>
1262 * Should be called only once; subsequent calls are ignored.
1265 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1267 G_LOCK (g_desktop_env);
1269 g_desktop_env = g_strdup (desktop_env);
1270 G_UNLOCK (g_desktop_env);
1274 g_desktop_app_info_should_show (GAppInfo *appinfo)
1276 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1278 const gchar *desktop_env;
1281 if (info->nodisplay)
1284 G_LOCK (g_desktop_env);
1285 desktop_env = g_desktop_env;
1286 G_UNLOCK (g_desktop_env);
1288 if (info->only_show_in)
1290 if (desktop_env == NULL)
1294 for (i = 0; info->only_show_in[i] != NULL; i++)
1296 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1306 if (info->not_show_in && desktop_env)
1308 for (i = 0; info->not_show_in[i] != NULL; i++)
1310 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1324 ensure_dir (DirType type,
1327 char *path, *display_name;
1330 if (type == APP_DIR)
1331 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1333 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1336 if (g_mkdir_with_parents (path, 0700) == 0)
1340 display_name = g_filename_display_name (path);
1341 if (type == APP_DIR)
1342 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1343 _("Can't create user application configuration folder %s: %s"),
1344 display_name, g_strerror (errsv));
1346 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1347 _("Can't create user MIME configuration folder %s: %s"),
1348 display_name, g_strerror (errsv));
1350 g_free (display_name);
1357 update_mimeapps_list (const char *desktop_id,
1358 const char *content_type,
1359 UpdateMimeFlags flags,
1362 char *dirname, *filename, *string;
1364 gboolean load_succeeded, res, explicit_default;
1365 char **old_list, **list;
1367 gsize length, data_size;
1370 char **content_types;
1372 /* Don't add both at start and end */
1373 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
1374 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
1376 dirname = ensure_dir (APP_DIR, error);
1380 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1383 key_file = g_key_file_new ();
1384 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1385 if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1387 g_key_file_free (key_file);
1388 key_file = g_key_file_new ();
1393 content_types = g_new (char *, 2);
1394 content_types[0] = g_strdup (content_type);
1395 content_types[1] = NULL;
1399 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
1402 explicit_default = FALSE;
1404 for (k = 0; content_types && content_types[k]; k++)
1406 /* set as default, if requested so */
1407 string = g_key_file_get_string (key_file,
1408 DEFAULT_APPLICATIONS_GROUP,
1412 if (g_strcmp0 (string, desktop_id) != 0 &&
1413 (flags & UPDATE_MIME_SET_DEFAULT))
1416 string = g_strdup (desktop_id);
1418 /* add in the non-default list too, if it's not already there */
1419 flags |= UPDATE_MIME_SET_NON_DEFAULT;
1422 if (string == NULL || desktop_id == NULL)
1423 g_key_file_remove_key (key_file,
1424 DEFAULT_APPLICATIONS_GROUP,
1429 g_key_file_set_string (key_file,
1430 DEFAULT_APPLICATIONS_GROUP,
1434 explicit_default = TRUE;
1442 /* reuse the list from above */
1446 g_strfreev (content_types);
1447 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1450 for (k = 0; content_types && content_types[k]; k++)
1452 /* Add to the right place in the list */
1455 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1456 content_types[k], &length, NULL);
1458 list = g_new (char *, 1 + length + 1);
1462 /* if we're adding a last-used hint, just put the application in front of the list */
1463 if (flags & UPDATE_MIME_SET_LAST_USED)
1465 /* avoid adding this again as non-default later */
1466 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1467 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1469 list[i++] = g_strdup (desktop_id);
1474 for (j = 0; old_list[j] != NULL; j++)
1476 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1478 /* rewrite other entries if they're different from the new one */
1479 list[i++] = g_strdup (old_list[j]);
1481 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1483 /* we encountered an old entry which is equal to the one we're adding as non-default,
1484 * don't change its position in the list.
1486 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1487 list[i++] = g_strdup (old_list[j]);
1492 /* add it at the end of the list */
1493 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1494 list[i++] = g_strdup (desktop_id);
1498 g_strfreev (old_list);
1500 if (list[0] == NULL || desktop_id == NULL)
1501 g_key_file_remove_key (key_file,
1502 ADDED_ASSOCIATIONS_GROUP,
1507 g_key_file_set_string_list (key_file,
1508 ADDED_ASSOCIATIONS_GROUP,
1510 (const char * const *)list, i);
1512 /* if we had no explicit default set, we should add the system default to the
1513 * list, to avoid overriding it with applications from this list.
1515 if (!explicit_default)
1517 system_list = get_all_desktop_entries_for_mime_type (content_type, (const char **) list, FALSE, NULL);
1519 if (system_list != NULL)
1521 string = system_list->data;
1523 g_key_file_set_string (key_file,
1524 DEFAULT_APPLICATIONS_GROUP,
1529 g_list_free_full (system_list, g_free);
1538 /* reuse the list from above */
1542 g_strfreev (content_types);
1543 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1546 for (k = 0; content_types && content_types[k]; k++)
1548 /* Remove from removed associations group (unless remove) */
1551 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1552 content_types[k], &length, NULL);
1554 list = g_new (char *, 1 + length + 1);
1557 if (flags & UPDATE_MIME_REMOVE)
1558 list[i++] = g_strdup (desktop_id);
1561 for (j = 0; old_list[j] != NULL; j++)
1563 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1564 list[i++] = g_strdup (old_list[j]);
1569 g_strfreev (old_list);
1571 if (list[0] == NULL || desktop_id == NULL)
1572 g_key_file_remove_key (key_file,
1573 REMOVED_ASSOCIATIONS_GROUP,
1577 g_key_file_set_string_list (key_file,
1578 REMOVED_ASSOCIATIONS_GROUP,
1580 (const char * const *)list, i);
1585 g_strfreev (content_types);
1587 data = g_key_file_to_data (key_file, &data_size, error);
1588 g_key_file_free (key_file);
1590 res = g_file_set_contents (filename, data, data_size, error);
1592 mime_info_cache_reload (NULL);
1601 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
1602 const char *content_type,
1605 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1607 if (!g_desktop_app_info_ensure_saved (info, error))
1610 /* both add support for the content type and set as last used */
1611 return update_mimeapps_list (info->desktop_id, content_type,
1612 UPDATE_MIME_SET_NON_DEFAULT |
1613 UPDATE_MIME_SET_LAST_USED,
1618 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1619 const char *content_type,
1622 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1624 if (!g_desktop_app_info_ensure_saved (info, error))
1627 return update_mimeapps_list (info->desktop_id, content_type,
1628 UPDATE_MIME_SET_DEFAULT,
1633 update_program_done (GPid pid,
1637 /* Did the application exit correctly */
1638 if (WIFEXITED (status) &&
1639 WEXITSTATUS (status) == 0)
1641 /* Here we could clean out any caches in use */
1646 run_update_command (char *command,
1655 GError *error = NULL;
1658 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1660 if (g_spawn_async ("/", argv,
1662 G_SPAWN_SEARCH_PATH |
1663 G_SPAWN_STDOUT_TO_DEV_NULL |
1664 G_SPAWN_STDERR_TO_DEV_NULL |
1665 G_SPAWN_DO_NOT_REAP_CHILD,
1666 NULL, NULL, /* No setup function */
1669 g_child_watch_add (pid, update_program_done, NULL);
1672 /* If we get an error at this point, it's quite likely the user doesn't
1673 * have an installed copy of either 'update-mime-database' or
1674 * 'update-desktop-database'. I don't think we want to popup an error
1675 * dialog at this point, so we just do a g_warning to give the user a
1676 * chance of debugging it.
1678 g_warning ("%s", error->message);
1685 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1686 const char *extension,
1689 char *filename, *basename, *mimetype;
1693 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1696 dirname = ensure_dir (MIMETYPE_DIR, error);
1700 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1701 filename = g_build_filename (dirname, basename, NULL);
1705 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1707 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1712 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1713 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1714 " <mime-type type=\"%s\">\n"
1715 " <comment>%s document</comment>\n"
1716 " <glob pattern=\"*.%s\"/>\n"
1718 "</mime-info>\n", mimetype, extension, extension);
1720 g_file_set_contents (filename, contents, -1, NULL);
1723 run_update_command ("update-mime-database", "mime");
1727 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1737 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1738 const char *content_type,
1741 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1743 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1746 return update_mimeapps_list (info->desktop_id, content_type,
1747 UPDATE_MIME_SET_NON_DEFAULT,
1752 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1758 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1759 const char *content_type,
1762 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1764 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1767 return update_mimeapps_list (info->desktop_id, content_type,
1773 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1779 char *data, *desktop_id;
1784 if (info->filename != NULL)
1787 /* This is only used for object created with
1788 * g_app_info_create_from_commandline. All other
1789 * object should have a filename
1792 dirname = ensure_dir (APP_DIR, error);
1796 key_file = g_key_file_new ();
1798 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1799 "Encoding", "UTF-8");
1800 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1801 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1802 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1803 G_KEY_FILE_DESKTOP_KEY_TYPE,
1804 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1806 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1807 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1809 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1810 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1812 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1813 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1815 if (info->fullname != NULL)
1816 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1817 FULL_NAME_KEY, info->fullname);
1819 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1820 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1822 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1823 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1825 data = g_key_file_to_data (key_file, &data_size, NULL);
1826 g_key_file_free (key_file);
1828 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1829 filename = g_build_filename (dirname, desktop_id, NULL);
1830 g_free (desktop_id);
1833 fd = g_mkstemp (filename);
1838 display_name = g_filename_display_name (filename);
1839 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1840 _("Can't create user desktop file %s"), display_name);
1841 g_free (display_name);
1847 desktop_id = g_path_get_basename (filename);
1851 res = g_file_set_contents (filename, data, data_size, error);
1854 g_free (desktop_id);
1859 info->filename = filename;
1860 info->desktop_id = desktop_id;
1862 run_update_command ("update-desktop-database", "applications");
1868 g_desktop_app_info_can_delete (GAppInfo *appinfo)
1870 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1874 if (strstr (info->filename, "/userapp-"))
1875 return g_access (info->filename, W_OK) == 0;
1882 g_desktop_app_info_delete (GAppInfo *appinfo)
1884 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1888 if (g_remove (info->filename) == 0)
1890 update_mimeapps_list (info->desktop_id, NULL,
1894 g_free (info->filename);
1895 info->filename = NULL;
1896 g_free (info->desktop_id);
1897 info->desktop_id = NULL;
1907 * g_app_info_create_from_commandline:
1908 * @commandline: the commandline to use
1909 * @application_name: (allow-none): the application name, or %NULL to use @commandline
1910 * @flags: flags that can specify details of the created #GAppInfo
1911 * @error: a #GError location to store the error occuring, %NULL to ignore.
1913 * Creates a new #GAppInfo from the given information.
1915 * Returns: (transfer full): new #GAppInfo for given command.
1918 g_app_info_create_from_commandline (const char *commandline,
1919 const char *application_name,
1920 GAppInfoCreateFlags flags,
1925 GDesktopAppInfo *info;
1927 g_return_val_if_fail (commandline, NULL);
1929 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1931 info->filename = NULL;
1932 info->desktop_id = NULL;
1934 info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
1935 info->startup_notify = flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION;
1936 info->hidden = FALSE;
1937 if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
1938 info->exec = g_strconcat (commandline, " %u", NULL);
1940 info->exec = g_strconcat (commandline, " %f", NULL);
1941 info->nodisplay = TRUE;
1942 info->binary = binary_from_exec (info->exec);
1944 if (application_name)
1945 info->name = g_strdup (application_name);
1948 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1949 split = g_strsplit (commandline, " ", 2);
1950 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
1952 info->name = basename;
1953 if (info->name == NULL)
1954 info->name = g_strdup ("custom");
1956 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
1958 return G_APP_INFO (info);
1962 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1964 iface->dup = g_desktop_app_info_dup;
1965 iface->equal = g_desktop_app_info_equal;
1966 iface->get_id = g_desktop_app_info_get_id;
1967 iface->get_name = g_desktop_app_info_get_name;
1968 iface->get_description = g_desktop_app_info_get_description;
1969 iface->get_executable = g_desktop_app_info_get_executable;
1970 iface->get_icon = g_desktop_app_info_get_icon;
1971 iface->launch = g_desktop_app_info_launch;
1972 iface->supports_uris = g_desktop_app_info_supports_uris;
1973 iface->supports_files = g_desktop_app_info_supports_files;
1974 iface->launch_uris = g_desktop_app_info_launch_uris;
1975 iface->should_show = g_desktop_app_info_should_show;
1976 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1977 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1978 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1979 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1980 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1981 iface->can_delete = g_desktop_app_info_can_delete;
1982 iface->do_delete = g_desktop_app_info_delete;
1983 iface->get_commandline = g_desktop_app_info_get_commandline;
1984 iface->get_display_name = g_desktop_app_info_get_display_name;
1985 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
1989 app_info_in_list (GAppInfo *info,
1992 while (list != NULL)
1994 if (g_app_info_equal (info, list->data))
2002 * g_app_info_get_recommended_for_type:
2003 * @content_type: the content type to find a #GAppInfo for
2005 * Gets a list of recommended #GAppInfos for a given content type, i.e.
2006 * those applications which claim to support the given content type exactly,
2007 * and not by MIME type subclassing.
2008 * Note that the first application of the list is the last used one, i.e.
2009 * the last one for which #g_app_info_set_as_last_used_for_type has been
2012 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2013 * for given @content_type or %NULL on error.
2018 g_app_info_get_recommended_for_type (const gchar *content_type)
2020 GList *desktop_entries, *l;
2022 GDesktopAppInfo *info;
2024 g_return_val_if_fail (content_type != NULL, NULL);
2026 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
2029 for (l = desktop_entries; l != NULL; l = l->next)
2031 char *desktop_entry = l->data;
2033 info = g_desktop_app_info_new (desktop_entry);
2036 if (app_info_in_list (G_APP_INFO (info), infos))
2037 g_object_unref (info);
2039 infos = g_list_prepend (infos, info);
2041 g_free (desktop_entry);
2044 g_list_free (desktop_entries);
2046 return g_list_reverse (infos);
2050 * g_app_info_get_fallback_for_type:
2051 * @content_type: the content type to find a #GAppInfo for
2053 * Gets a list of fallback #GAppInfos for a given content type, i.e.
2054 * those applications which claim to support the given content type
2055 * by MIME type subclassing and not directly.
2057 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2058 * for given @content_type or %NULL on error.
2063 g_app_info_get_fallback_for_type (const gchar *content_type)
2065 GList *desktop_entries, *l;
2066 GList *infos, *recommended_infos;
2067 GDesktopAppInfo *info;
2069 g_return_val_if_fail (content_type != NULL, NULL);
2071 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
2072 recommended_infos = g_app_info_get_recommended_for_type (content_type);
2075 for (l = desktop_entries; l != NULL; l = l->next)
2077 char *desktop_entry = l->data;
2079 info = g_desktop_app_info_new (desktop_entry);
2082 if (app_info_in_list (G_APP_INFO (info), infos) ||
2083 app_info_in_list (G_APP_INFO (info), recommended_infos))
2084 g_object_unref (info);
2086 infos = g_list_prepend (infos, info);
2088 g_free (desktop_entry);
2091 g_list_free (desktop_entries);
2092 g_list_free_full (recommended_infos, g_object_unref);
2094 return g_list_reverse (infos);
2098 * g_app_info_get_all_for_type:
2099 * @content_type: the content type to find a #GAppInfo for
2101 * Gets a list of all #GAppInfos for a given content type.
2103 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2104 * for given @content_type or %NULL on error.
2107 g_app_info_get_all_for_type (const char *content_type)
2109 GList *desktop_entries, *l;
2111 char *user_default = NULL;
2112 GDesktopAppInfo *info;
2114 g_return_val_if_fail (content_type != NULL, NULL);
2116 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2119 /* put the user default in front of the list, for compatibility */
2120 if (user_default != NULL)
2122 info = g_desktop_app_info_new (user_default);
2125 infos = g_list_prepend (infos, info);
2128 g_free (user_default);
2130 for (l = desktop_entries; l != NULL; l = l->next)
2132 char *desktop_entry = l->data;
2134 info = g_desktop_app_info_new (desktop_entry);
2137 if (app_info_in_list (G_APP_INFO (info), infos))
2138 g_object_unref (info);
2140 infos = g_list_prepend (infos, info);
2142 g_free (desktop_entry);
2145 g_list_free (desktop_entries);
2147 return g_list_reverse (infos);
2151 * g_app_info_reset_type_associations:
2152 * @content_type: a content type
2154 * Removes all changes to the type associations done by
2155 * g_app_info_set_as_default_for_type(),
2156 * g_app_info_set_as_default_for_extension(),
2157 * g_app_info_add_supports_type() or g_app_info_remove_supports_type().
2162 g_app_info_reset_type_associations (const char *content_type)
2164 update_mimeapps_list (NULL, content_type,
2170 * g_app_info_get_default_for_type:
2171 * @content_type: the content type to find a #GAppInfo for
2172 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
2175 * Gets the #GAppInfo that corresponds to a given content type.
2177 * Returns: (transfer full): #GAppInfo for given @content_type or
2181 g_app_info_get_default_for_type (const char *content_type,
2182 gboolean must_support_uris)
2184 GList *desktop_entries, *l;
2185 char *user_default = NULL;
2188 g_return_val_if_fail (content_type != NULL, NULL);
2190 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2194 if (user_default != NULL)
2196 info = (GAppInfo *) g_desktop_app_info_new (user_default);
2200 if (must_support_uris && !g_app_info_supports_uris (info))
2202 g_object_unref (info);
2208 g_free (user_default);
2212 g_list_free_full (desktop_entries, g_free);
2216 /* pick the first from the other list that matches our URI
2219 for (l = desktop_entries; l != NULL; l = l->next)
2221 char *desktop_entry = l->data;
2223 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
2226 if (must_support_uris && !g_app_info_supports_uris (info))
2228 g_object_unref (info);
2236 g_list_free_full (desktop_entries, g_free);
2242 * g_app_info_get_default_for_uri_scheme:
2243 * @uri_scheme: a string containing a URI scheme.
2245 * Gets the default application for launching applications
2246 * using this URI scheme. A URI scheme is the initial part
2247 * of the URI, up to but not including the ':', e.g. "http",
2250 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
2253 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
2256 char *content_type, *scheme_down;
2258 scheme_down = g_ascii_strdown (uri_scheme, -1);
2259 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
2260 g_free (scheme_down);
2261 app_info = g_app_info_get_default_for_type (content_type, FALSE);
2262 g_free (content_type);
2268 get_apps_from_dir (GHashTable *apps,
2269 const char *dirname,
2273 const char *basename;
2274 char *filename, *subprefix, *desktop_id;
2276 GDesktopAppInfo *appinfo;
2278 dir = g_dir_open (dirname, 0, NULL);
2281 while ((basename = g_dir_read_name (dir)) != NULL)
2283 filename = g_build_filename (dirname, basename, NULL);
2284 if (g_str_has_suffix (basename, ".desktop"))
2286 desktop_id = g_strconcat (prefix, basename, NULL);
2288 /* Use _extended so we catch NULLs too (hidden) */
2289 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
2291 appinfo = g_desktop_app_info_new_from_filename (filename);
2294 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
2296 g_object_unref (appinfo);
2301 if (appinfo || hidden)
2303 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
2307 /* Reuse instead of strdup here */
2308 appinfo->desktop_id = desktop_id;
2313 g_free (desktop_id);
2317 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
2319 subprefix = g_strconcat (prefix, basename, "-", NULL);
2320 get_apps_from_dir (apps, filename, subprefix);
2332 * g_app_info_get_all:
2334 * Gets a list of all of the applications currently registered
2337 * For desktop files, this includes applications that have
2338 * <literal>NoDisplay=true</literal> set or are excluded from
2339 * display by means of <literal>OnlyShowIn</literal> or
2340 * <literal>NotShowIn</literal>. See g_app_info_should_show().
2341 * The returned list does not include applications which have
2342 * the <literal>Hidden</literal> key set.
2344 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
2347 g_app_info_get_all (void)
2349 const char * const *dirs;
2351 GHashTableIter iter;
2356 dirs = get_applications_search_path ();
2358 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
2362 for (i = 0; dirs[i] != NULL; i++)
2363 get_apps_from_dir (apps, dirs[i], "");
2367 g_hash_table_iter_init (&iter, apps);
2368 while (g_hash_table_iter_next (&iter, NULL, &value))
2371 infos = g_list_prepend (infos, value);
2374 g_hash_table_destroy (apps);
2376 return g_list_reverse (infos);
2379 /* Cacheing of mimeinfo.cache and defaults.list files */
2383 GHashTable *mime_info_cache_map;
2384 GHashTable *defaults_list_map;
2385 GHashTable *mimeapps_list_added_map;
2386 GHashTable *mimeapps_list_removed_map;
2387 GHashTable *mimeapps_list_defaults_map;
2388 time_t mime_info_cache_timestamp;
2389 time_t defaults_list_timestamp;
2390 time_t mimeapps_list_timestamp;
2394 GList *dirs; /* mimeinfo.cache and defaults.list */
2395 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
2396 time_t last_stat_time;
2397 guint should_ping_mime_monitor : 1;
2400 static MimeInfoCache *mime_info_cache = NULL;
2401 G_LOCK_DEFINE_STATIC (mime_info_cache);
2403 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2404 const char *mime_type,
2405 char **new_desktop_file_ids);
2407 static MimeInfoCache * mime_info_cache_new (void);
2410 destroy_info_cache_value (gpointer key,
2414 g_list_foreach (value, (GFunc)g_free, NULL);
2415 g_list_free (value);
2419 destroy_info_cache_map (GHashTable *info_cache_map)
2421 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
2422 g_hash_table_destroy (info_cache_map);
2426 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2427 const char *cache_file,
2433 filename = g_build_filename (dir->path, cache_file, NULL);
2435 if (g_stat (filename, &buf) < 0)
2442 if (buf.st_mtime != *timestamp)
2448 /* Call with lock held */
2450 remove_all (gpointer key,
2459 mime_info_cache_blow_global_cache (void)
2461 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
2466 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2470 gchar *filename, **mime_types;
2477 if (dir->mime_info_cache_map != NULL &&
2478 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2479 &dir->mime_info_cache_timestamp))
2482 if (dir->mime_info_cache_map != NULL)
2483 destroy_info_cache_map (dir->mime_info_cache_map);
2485 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2486 (GDestroyNotify) g_free,
2489 key_file = g_key_file_new ();
2491 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2493 if (g_stat (filename, &buf) < 0)
2496 if (dir->mime_info_cache_timestamp > 0)
2497 mime_info_cache->should_ping_mime_monitor = TRUE;
2499 dir->mime_info_cache_timestamp = buf.st_mtime;
2501 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2506 if (load_error != NULL)
2509 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2512 if (load_error != NULL)
2515 for (i = 0; mime_types[i] != NULL; i++)
2517 gchar **desktop_file_ids;
2518 char *unaliased_type;
2519 desktop_file_ids = g_key_file_get_string_list (key_file,
2525 if (desktop_file_ids == NULL)
2528 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2529 mime_info_cache_dir_add_desktop_entries (dir,
2532 g_free (unaliased_type);
2534 g_strfreev (desktop_file_ids);
2537 g_strfreev (mime_types);
2538 g_key_file_free (key_file);
2543 g_key_file_free (key_file);
2545 if (mime_types != NULL)
2546 g_strfreev (mime_types);
2549 g_error_free (load_error);
2553 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2557 gchar *filename, **mime_types;
2558 char *unaliased_type;
2559 char **desktop_file_ids;
2566 if (dir->defaults_list_map != NULL &&
2567 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2568 &dir->defaults_list_timestamp))
2571 if (dir->defaults_list_map != NULL)
2572 g_hash_table_destroy (dir->defaults_list_map);
2573 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2574 g_free, (GDestroyNotify)g_strfreev);
2577 key_file = g_key_file_new ();
2579 filename = g_build_filename (dir->path, "defaults.list", NULL);
2580 if (g_stat (filename, &buf) < 0)
2583 if (dir->defaults_list_timestamp > 0)
2584 mime_info_cache->should_ping_mime_monitor = TRUE;
2586 dir->defaults_list_timestamp = buf.st_mtime;
2588 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2592 if (load_error != NULL)
2595 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2597 if (mime_types != NULL)
2599 for (i = 0; mime_types[i] != NULL; i++)
2601 desktop_file_ids = g_key_file_get_string_list (key_file,
2602 DEFAULT_APPLICATIONS_GROUP,
2606 if (desktop_file_ids == NULL)
2609 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2610 g_hash_table_replace (dir->defaults_list_map,
2615 g_strfreev (mime_types);
2618 g_key_file_free (key_file);
2623 g_key_file_free (key_file);
2625 if (mime_types != NULL)
2626 g_strfreev (mime_types);
2629 g_error_free (load_error);
2633 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2637 gchar *filename, **mime_types;
2638 char *unaliased_type;
2639 char **desktop_file_ids;
2647 if (dir->mimeapps_list_added_map != NULL &&
2648 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2649 &dir->mimeapps_list_timestamp))
2652 if (dir->mimeapps_list_added_map != NULL)
2653 g_hash_table_destroy (dir->mimeapps_list_added_map);
2654 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2655 g_free, (GDestroyNotify)g_strfreev);
2657 if (dir->mimeapps_list_removed_map != NULL)
2658 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2659 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2660 g_free, (GDestroyNotify)g_strfreev);
2662 if (dir->mimeapps_list_defaults_map != NULL)
2663 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2664 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2667 key_file = g_key_file_new ();
2669 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2670 if (g_stat (filename, &buf) < 0)
2673 if (dir->mimeapps_list_timestamp > 0)
2674 mime_info_cache->should_ping_mime_monitor = TRUE;
2676 dir->mimeapps_list_timestamp = buf.st_mtime;
2678 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2682 if (load_error != NULL)
2685 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2687 if (mime_types != NULL)
2689 for (i = 0; mime_types[i] != NULL; i++)
2691 desktop_file_ids = g_key_file_get_string_list (key_file,
2692 ADDED_ASSOCIATIONS_GROUP,
2696 if (desktop_file_ids == NULL)
2699 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2700 g_hash_table_replace (dir->mimeapps_list_added_map,
2705 g_strfreev (mime_types);
2708 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2710 if (mime_types != NULL)
2712 for (i = 0; mime_types[i] != NULL; i++)
2714 desktop_file_ids = g_key_file_get_string_list (key_file,
2715 REMOVED_ASSOCIATIONS_GROUP,
2719 if (desktop_file_ids == NULL)
2722 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2723 g_hash_table_replace (dir->mimeapps_list_removed_map,
2728 g_strfreev (mime_types);
2731 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2733 if (mime_types != NULL)
2735 for (i = 0; mime_types[i] != NULL; i++)
2737 desktop_id = g_key_file_get_string (key_file,
2738 DEFAULT_APPLICATIONS_GROUP,
2741 if (desktop_id == NULL)
2744 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2745 g_hash_table_replace (dir->mimeapps_list_defaults_map,
2750 g_strfreev (mime_types);
2753 g_key_file_free (key_file);
2758 g_key_file_free (key_file);
2760 if (mime_types != NULL)
2761 g_strfreev (mime_types);
2764 g_error_free (load_error);
2767 static MimeInfoCacheDir *
2768 mime_info_cache_dir_new (const char *path)
2770 MimeInfoCacheDir *dir;
2772 dir = g_new0 (MimeInfoCacheDir, 1);
2773 dir->path = g_strdup (path);
2779 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2784 if (dir->mime_info_cache_map != NULL)
2786 destroy_info_cache_map (dir->mime_info_cache_map);
2787 dir->mime_info_cache_map = NULL;
2791 if (dir->defaults_list_map != NULL)
2793 g_hash_table_destroy (dir->defaults_list_map);
2794 dir->defaults_list_map = NULL;
2797 if (dir->mimeapps_list_added_map != NULL)
2799 g_hash_table_destroy (dir->mimeapps_list_added_map);
2800 dir->mimeapps_list_added_map = NULL;
2803 if (dir->mimeapps_list_removed_map != NULL)
2805 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2806 dir->mimeapps_list_removed_map = NULL;
2809 if (dir->mimeapps_list_defaults_map != NULL)
2811 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2812 dir->mimeapps_list_defaults_map = NULL;
2819 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2820 const char *mime_type,
2821 char **new_desktop_file_ids)
2823 GList *desktop_file_ids;
2826 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2829 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2831 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
2832 desktop_file_ids = g_list_append (desktop_file_ids,
2833 g_strdup (new_desktop_file_ids[i]));
2836 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2840 mime_info_cache_init_dir_lists (void)
2842 const char * const *dirs;
2845 mime_info_cache = mime_info_cache_new ();
2847 dirs = get_applications_search_path ();
2849 for (i = 0; dirs[i] != NULL; i++)
2851 MimeInfoCacheDir *dir;
2853 dir = mime_info_cache_dir_new (dirs[i]);
2857 mime_info_cache_dir_init (dir);
2858 mime_info_cache_dir_init_defaults_list (dir);
2859 mime_info_cache_dir_init_mimeapps_list (dir);
2861 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2867 mime_info_cache_update_dir_lists (void)
2871 tmp = mime_info_cache->dirs;
2875 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2877 /* No need to do this if we had file monitors... */
2878 mime_info_cache_blow_global_cache ();
2879 mime_info_cache_dir_init (dir);
2880 mime_info_cache_dir_init_defaults_list (dir);
2881 mime_info_cache_dir_init_mimeapps_list (dir);
2888 mime_info_cache_init (void)
2890 G_LOCK (mime_info_cache);
2891 if (mime_info_cache == NULL)
2892 mime_info_cache_init_dir_lists ();
2898 if (now >= mime_info_cache->last_stat_time + 10)
2900 mime_info_cache_update_dir_lists ();
2901 mime_info_cache->last_stat_time = now;
2905 if (mime_info_cache->should_ping_mime_monitor)
2907 /* g_idle_add (emit_mime_changed, NULL); */
2908 mime_info_cache->should_ping_mime_monitor = FALSE;
2911 G_UNLOCK (mime_info_cache);
2914 static MimeInfoCache *
2915 mime_info_cache_new (void)
2917 MimeInfoCache *cache;
2919 cache = g_new0 (MimeInfoCache, 1);
2921 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2922 (GDestroyNotify) g_free,
2923 (GDestroyNotify) g_free);
2928 mime_info_cache_free (MimeInfoCache *cache)
2933 g_list_foreach (cache->dirs,
2934 (GFunc) mime_info_cache_dir_free,
2936 g_list_free (cache->dirs);
2937 g_hash_table_destroy (cache->global_defaults_cache);
2942 * mime_info_cache_reload:
2943 * @dir: directory path which needs reloading.
2945 * Reload the mime information for the @dir.
2948 mime_info_cache_reload (const char *dir)
2950 /* FIXME: just reload the dir that needs reloading,
2951 * don't blow the whole cache
2953 if (mime_info_cache != NULL)
2955 G_LOCK (mime_info_cache);
2956 mime_info_cache_free (mime_info_cache);
2957 mime_info_cache = NULL;
2958 G_UNLOCK (mime_info_cache);
2963 append_desktop_entry (GList *list,
2964 const char *desktop_entry,
2965 GList *removed_entries)
2967 /* Add if not already in list, and valid */
2968 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2969 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2970 list = g_list_prepend (list, g_strdup (desktop_entry));
2976 * get_all_desktop_entries_for_mime_type:
2977 * @mime_type: a mime type.
2978 * @except: NULL or a strv list
2980 * Returns all the desktop ids for @mime_type. The desktop files
2981 * are listed in an order so that default applications are listed before
2982 * non-default ones, and handlers for inherited mimetypes are listed
2983 * after the base ones.
2985 * Optionally doesn't list the desktop ids given in the @except
2987 * Return value: a #GList containing the desktop ids which claim
2988 * to handle @mime_type.
2991 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
2992 const char **except,
2993 gboolean include_fallback,
2994 char **explicit_default)
2996 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2997 MimeInfoCacheDir *dir;
2998 char *mime_type, *default_entry = NULL;
3001 char **default_entries;
3002 char **removed_associations;
3007 mime_info_cache_init ();
3009 if (include_fallback)
3011 /* collect all ancestors */
3012 mime_types = _g_unix_content_type_get_parents (base_mime_type);
3013 array = g_ptr_array_new ();
3014 for (i = 0; mime_types[i]; i++)
3015 g_ptr_array_add (array, mime_types[i]);
3016 g_free (mime_types);
3017 for (i = 0; i < array->len; i++)
3019 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
3020 for (j = 0; anc[j]; j++)
3022 for (k = 0; k < array->len; k++)
3024 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
3027 if (k == array->len) /* not found */
3028 g_ptr_array_add (array, g_strdup (anc[j]));
3032 g_ptr_array_add (array, NULL);
3033 mime_types = (char **)g_ptr_array_free (array, FALSE);
3037 mime_types = g_malloc0 (2 * sizeof (gchar *));
3038 mime_types[0] = g_strdup (base_mime_type);
3039 mime_types[1] = NULL;
3042 G_LOCK (mime_info_cache);
3044 removed_entries = NULL;
3045 desktop_entries = NULL;
3047 for (i = 0; except != NULL && except[i] != NULL; i++)
3048 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
3050 for (i = 0; mime_types[i] != NULL; i++)
3052 mime_type = mime_types[i];
3054 /* Go through all apps listed in user and system dirs */
3055 for (dir_list = mime_info_cache->dirs;
3057 dir_list = dir_list->next)
3059 dir = dir_list->data;
3061 /* Pick the explicit default application */
3062 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
3066 /* Save the default entry if it's the first one we encounter */
3067 if (default_entry == NULL)
3068 default_entry = g_strdup (entry);
3071 /* Then added associations from mimeapps.list */
3072 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
3073 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3074 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3076 /* Then removed associations from mimeapps.list */
3077 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
3078 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
3079 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
3081 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
3082 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
3083 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3084 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3087 /* Go through all entries that support the mimetype */
3088 for (dir_list = mime_info_cache->dirs;
3090 dir_list = dir_list->next)
3092 dir = dir_list->data;
3094 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
3095 for (tmp = list; tmp != NULL; tmp = tmp->next)
3096 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
3100 G_UNLOCK (mime_info_cache);
3102 g_strfreev (mime_types);
3104 if (explicit_default != NULL)
3105 *explicit_default = default_entry;
3107 g_free (default_entry);
3109 g_list_foreach (removed_entries, (GFunc)g_free, NULL);
3110 g_list_free (removed_entries);
3112 desktop_entries = g_list_reverse (desktop_entries);
3114 return desktop_entries;
3117 /* GDesktopAppInfoLookup interface: */
3119 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
3120 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
3123 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
3128 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
3129 * @lookup: a #GDesktopAppInfoLookup
3130 * @uri_scheme: a string containing a URI scheme.
3132 * Gets the default application for launching applications
3133 * using this URI scheme for a particular GDesktopAppInfoLookup
3136 * The GDesktopAppInfoLookup interface and this function is used
3137 * to implement g_app_info_get_default_for_uri_scheme() backends
3138 * in a GIO module. There is no reason for applications to use it
3139 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
3141 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3143 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
3146 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
3147 const char *uri_scheme)
3149 GDesktopAppInfoLookupIface *iface;
3151 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
3153 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
3155 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);