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 GENERIC_NAME_KEY "GenericName"
68 #define FULL_NAME_KEY "X-GNOME-FullName"
75 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
76 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type,
78 gboolean include_fallback,
79 char **explicit_default);
80 static void mime_info_cache_reload (const char *dir);
81 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
87 * Information about an installed application from a desktop file.
89 struct _GDesktopAppInfo
91 GObject parent_instance;
113 guint startup_notify : 1;
115 /* FIXME: what about StartupWMClass ? */
119 UPDATE_MIME_NONE = 1 << 0,
120 UPDATE_MIME_SET_DEFAULT = 1 << 1,
121 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
122 UPDATE_MIME_REMOVE = 1 << 3,
123 UPDATE_MIME_SET_LAST_USED = 1 << 4,
126 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
127 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
128 g_desktop_app_info_iface_init))
131 search_path_init (gpointer data)
134 const char * const *data_dirs;
135 const char *user_data_dir;
138 data_dirs = g_get_system_data_dirs ();
139 length = g_strv_length ((char **) data_dirs);
141 args = g_new (char *, length + 2);
144 user_data_dir = g_get_user_data_dir ();
145 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
146 for (i = 0; i < length; i++)
147 args[j++] = g_build_filename (data_dirs[i],
148 "applications", NULL);
154 static const char * const *
155 get_applications_search_path (void)
157 static GOnce once_init = G_ONCE_INIT;
158 return g_once (&once_init, search_path_init, NULL);
162 g_desktop_app_info_finalize (GObject *object)
164 GDesktopAppInfo *info;
166 info = G_DESKTOP_APP_INFO (object);
168 g_free (info->desktop_id);
169 g_free (info->filename);
171 g_free (info->generic_name);
172 g_free (info->fullname);
173 g_free (info->comment);
174 g_free (info->icon_name);
176 g_object_unref (info->icon);
177 g_strfreev (info->only_show_in);
178 g_strfreev (info->not_show_in);
179 g_free (info->try_exec);
181 g_free (info->binary);
183 g_free (info->categories);
185 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
189 g_desktop_app_info_set_property(GObject *object,
194 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
199 self->filename = g_value_dup_string (value);
203 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209 g_desktop_app_info_get_property(GObject *object,
214 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
219 g_value_set_string (value, self->filename);
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
230 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
232 gobject_class->get_property = g_desktop_app_info_get_property;
233 gobject_class->set_property = g_desktop_app_info_set_property;
234 gobject_class->finalize = g_desktop_app_info_finalize;
237 * GDesktopAppInfo:filename
239 * The origin filename of this #GDesktopAppInfo
241 g_object_class_install_property (gobject_class,
243 g_param_spec_string ("filename", "Filename", "",
245 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
249 g_desktop_app_info_init (GDesktopAppInfo *local)
254 binary_from_exec (const char *exec)
256 const char *p, *start;
262 while (*p != ' ' && *p != 0)
265 return g_strndup (start, p - start);
270 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
277 start_group = g_key_file_get_start_group (key_file);
278 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
280 g_free (start_group);
283 g_free (start_group);
285 type = g_key_file_get_string (key_file,
286 G_KEY_FILE_DESKTOP_GROUP,
287 G_KEY_FILE_DESKTOP_KEY_TYPE,
289 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
296 try_exec = g_key_file_get_string (key_file,
297 G_KEY_FILE_DESKTOP_GROUP,
298 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
300 if (try_exec && try_exec[0] != '\0')
303 t = g_find_program_in_path (try_exec);
312 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
313 info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
314 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
315 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
316 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
317 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
318 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);
319 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);
320 info->try_exec = try_exec;
321 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
322 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
323 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
324 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
325 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
326 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
327 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
332 if (g_path_is_absolute (info->icon_name))
336 file = g_file_new_for_path (info->icon_name);
337 info->icon = g_file_icon_new (file);
338 g_object_unref (file);
344 /* Work around a common mistake in desktop files */
345 if ((p = strrchr (info->icon_name, '.')) != NULL &&
346 (strcmp (p, ".png") == 0 ||
347 strcmp (p, ".xpm") == 0 ||
348 strcmp (p, ".svg") == 0))
351 info->icon = g_themed_icon_new (info->icon_name);
356 info->binary = binary_from_exec (info->exec);
358 if (info->path && info->path[0] == '\0')
368 g_desktop_app_info_load_file (GDesktopAppInfo *self)
371 gboolean retval = FALSE;
373 g_return_val_if_fail (self->filename != NULL, FALSE);
375 key_file = g_key_file_new ();
377 if (g_key_file_load_from_file (key_file,
382 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
385 g_key_file_free (key_file);
390 * g_desktop_app_info_new_from_keyfile:
391 * @key_file: an opened #GKeyFile
393 * Creates a new #GDesktopAppInfo.
395 * Returns: a new #GDesktopAppInfo or %NULL on error.
400 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
402 GDesktopAppInfo *info;
404 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
405 info->filename = NULL;
406 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
408 g_object_unref (info);
415 * g_desktop_app_info_new_from_filename:
416 * @filename: the path of a desktop file, in the GLib filename encoding
418 * Creates a new #GDesktopAppInfo.
420 * Returns: a new #GDesktopAppInfo or %NULL on error.
423 g_desktop_app_info_new_from_filename (const char *filename)
425 GDesktopAppInfo *info = NULL;
427 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
428 if (!g_desktop_app_info_load_file (info))
430 g_object_unref (info);
437 * g_desktop_app_info_new:
438 * @desktop_id: the desktop file id
440 * Creates a new #GDesktopAppInfo based on a desktop file id.
442 * A desktop file id is the basename of the desktop file, including the
443 * .desktop extension. GIO is looking for a desktop file with this name
444 * in the <filename>applications</filename> subdirectories of the XDG data
445 * directories (i.e. the directories specified in the
446 * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment
447 * variables). GIO also supports the prefix-to-subdirectory mapping that is
448 * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink>
449 * (i.e. a desktop id of kde-foo.desktop will match
450 * <filename>/usr/share/applications/kde/foo.desktop</filename>).
452 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
455 g_desktop_app_info_new (const char *desktop_id)
457 GDesktopAppInfo *appinfo;
458 const char * const *dirs;
462 dirs = get_applications_search_path ();
464 basename = g_strdup (desktop_id);
466 for (i = 0; dirs[i] != NULL; i++)
471 filename = g_build_filename (dirs[i], desktop_id, NULL);
472 appinfo = g_desktop_app_info_new_from_filename (filename);
478 while ((p = strchr (p, '-')) != NULL)
482 filename = g_build_filename (dirs[i], basename, NULL);
483 appinfo = g_desktop_app_info_new_from_filename (filename);
498 appinfo->desktop_id = g_strdup (desktop_id);
500 if (g_desktop_app_info_get_is_hidden (appinfo))
502 g_object_unref (appinfo);
510 g_desktop_app_info_dup (GAppInfo *appinfo)
512 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
513 GDesktopAppInfo *new_info;
515 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
517 new_info->filename = g_strdup (info->filename);
518 new_info->desktop_id = g_strdup (info->desktop_id);
520 new_info->name = g_strdup (info->name);
521 new_info->generic_name = g_strdup (info->generic_name);
522 new_info->fullname = g_strdup (info->fullname);
523 new_info->comment = g_strdup (info->comment);
524 new_info->nodisplay = info->nodisplay;
525 new_info->icon_name = g_strdup (info->icon_name);
527 new_info->icon = g_object_ref (info->icon);
528 new_info->only_show_in = g_strdupv (info->only_show_in);
529 new_info->not_show_in = g_strdupv (info->not_show_in);
530 new_info->try_exec = g_strdup (info->try_exec);
531 new_info->exec = g_strdup (info->exec);
532 new_info->binary = g_strdup (info->binary);
533 new_info->path = g_strdup (info->path);
534 new_info->hidden = info->hidden;
535 new_info->terminal = info->terminal;
536 new_info->startup_notify = info->startup_notify;
538 return G_APP_INFO (new_info);
542 g_desktop_app_info_equal (GAppInfo *appinfo1,
545 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
546 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
548 if (info1->desktop_id == NULL ||
549 info2->desktop_id == NULL)
550 return info1 == info2;
552 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
556 g_desktop_app_info_get_id (GAppInfo *appinfo)
558 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
560 return info->desktop_id;
564 g_desktop_app_info_get_name (GAppInfo *appinfo)
566 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
568 if (info->name == NULL)
574 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
576 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
578 if (info->fullname == NULL)
579 return g_desktop_app_info_get_name (appinfo);
580 return info->fullname;
584 * g_desktop_app_info_get_is_hidden:
585 * @info: a #GDesktopAppInfo.
587 * A desktop file is hidden if the Hidden key in it is
590 * Returns: %TRUE if hidden, %FALSE otherwise.
593 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
599 * g_desktop_app_info_get_filename:
600 * @info: a #GDesktopAppInfo
602 * When @info was created from a known filename, return it. In some
603 * situations such as the #GDesktopAppInfo returned from
604 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
606 * Returns: The full path to the file for @info, or %NULL if not known.
610 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
612 return info->filename;
616 g_desktop_app_info_get_description (GAppInfo *appinfo)
618 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
620 return info->comment;
624 g_desktop_app_info_get_executable (GAppInfo *appinfo)
626 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
632 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
634 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
640 g_desktop_app_info_get_icon (GAppInfo *appinfo)
642 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
648 * g_desktop_app_info_get_categories:
649 * @info: a #GDesktopAppInfo
651 * Returns: The unparsed Categories key from the file; i.e. no attempt
652 * is made to split it by ';' or validate it.
655 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
657 return info->categories;
661 * g_desktop_app_info_get_generic_name:
662 * @info: a #GDesktopAppInfo
664 * Returns: The value of the GenericName key
667 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
669 return info->generic_name;
673 expand_macro_single (char macro, char *uri)
679 file = g_file_new_for_uri (uri);
680 path = g_file_get_path (file);
681 g_object_unref (file);
687 result = g_shell_quote (uri);
692 result = g_shell_quote (path);
698 name = g_path_get_dirname (path);
699 result = g_shell_quote (name);
707 name = g_path_get_basename (path);
708 result = g_shell_quote (name);
720 expand_macro (char macro,
722 GDesktopAppInfo *info,
725 GList *uris = *uri_list;
727 gboolean force_file_uri;
728 char force_file_uri_macro;
731 g_return_if_fail (exec != NULL);
733 /* On %u and %U, pass POSIX file path pointing to the URI via
734 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
735 * running or the URI doesn't have a POSIX file path via FUSE
736 * we'll just pass the URI.
738 force_file_uri_macro = macro;
739 force_file_uri = FALSE;
745 force_file_uri_macro = 'f';
746 force_file_uri = TRUE;
749 force_file_uri_macro = 'F';
750 force_file_uri = TRUE;
766 if (!force_file_uri ||
767 /* Pass URI if it contains an anchor */
768 strchr (uri, '#') != NULL)
770 expanded = expand_macro_single (macro, uri);
774 expanded = expand_macro_single (force_file_uri_macro, uri);
775 if (expanded == NULL)
776 expanded = expand_macro_single (macro, uri);
781 g_string_append (exec, expanded);
797 if (!force_file_uri ||
798 /* Pass URI if it contains an anchor */
799 strchr (uri, '#') != NULL)
801 expanded = expand_macro_single (macro, uri);
805 expanded = expand_macro_single (force_file_uri_macro, uri);
806 if (expanded == NULL)
807 expanded = expand_macro_single (macro, uri);
812 g_string_append (exec, expanded);
818 if (uris != NULL && expanded)
819 g_string_append_c (exec, ' ');
827 g_string_append (exec, "--icon ");
828 expanded = g_shell_quote (info->icon_name);
829 g_string_append (exec, expanded);
837 expanded = g_shell_quote (info->name);
838 g_string_append (exec, expanded);
846 expanded = g_shell_quote (info->filename);
847 g_string_append (exec, expanded);
852 case 'm': /* deprecated */
856 g_string_append_c (exec, '%');
864 expand_application_parameters (GDesktopAppInfo *info,
870 GList *uri_list = *uris;
871 const char *p = info->exec;
872 GString *expanded_exec;
875 if (info->exec == NULL)
877 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
878 _("Desktop file didn't specify Exec field"));
882 expanded_exec = g_string_new (NULL);
886 if (p[0] == '%' && p[1] != '\0')
888 expand_macro (p[1], expanded_exec, info, uris);
892 g_string_append_c (expanded_exec, *p);
897 /* No file substitutions */
898 if (uri_list == *uris && uri_list != NULL)
900 /* If there is no macro default to %f. This is also what KDE does */
901 g_string_append_c (expanded_exec, ' ');
902 expand_macro ('f', expanded_exec, info, uris);
905 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
906 g_string_free (expanded_exec, TRUE);
911 prepend_terminal_to_vector (int *argc,
918 char **term_argv = NULL;
923 g_return_val_if_fail (argc != NULL, FALSE);
924 g_return_val_if_fail (argv != NULL, FALSE);
932 /* compute size if not given */
935 for (i = 0; the_argv[i] != NULL; i++)
941 term_argv = g_new0 (char *, 3);
943 check = g_find_program_in_path ("gnome-terminal");
946 term_argv[0] = check;
947 /* Note that gnome-terminal takes -x and
948 * as -e in gnome-terminal is broken we use that. */
949 term_argv[1] = g_strdup ("-x");
954 check = g_find_program_in_path ("nxterm");
956 check = g_find_program_in_path ("color-xterm");
958 check = g_find_program_in_path ("rxvt");
960 check = g_find_program_in_path ("xterm");
962 check = g_find_program_in_path ("dtterm");
965 check = g_strdup ("xterm");
966 g_warning ("couldn't find a terminal, falling back to xterm");
968 term_argv[0] = check;
969 term_argv[1] = g_strdup ("-e");
972 real_argc = term_argc + *argc;
973 real_argv = g_new (char *, real_argc + 1);
975 for (i = 0; i < term_argc; i++)
976 real_argv[i] = term_argv[i];
978 for (j = 0; j < *argc; j++, i++)
979 real_argv[i] = (char *)the_argv[j];
987 /* we use g_free here as we sucked all the inner strings
988 * out from it into real_argv */
993 #endif /* G_OS_WIN32 */
997 create_files_for_uris (GList *uris)
1004 for (iter = uris; iter; iter = iter->next)
1006 GFile *file = g_file_new_for_uri ((char *)iter->data);
1007 res = g_list_prepend (res, file);
1010 return g_list_reverse (res);
1015 GSpawnChildSetupFunc user_setup;
1016 gpointer user_setup_data;
1023 child_setup (gpointer user_data)
1025 ChildSetupData *data = user_data;
1028 g_setenv ("DISPLAY", data->display, TRUE);
1031 g_setenv ("DESKTOP_STARTUP_ID", data->sn_id, TRUE);
1033 if (data->desktop_file)
1037 g_setenv ("GIO_LAUNCHED_DESKTOP_FILE", data->desktop_file, TRUE);
1039 g_snprintf (pid, 20, "%ld", (long)getpid ());
1040 g_setenv ("GIO_LAUNCHED_DESKTOP_FILE_PID", pid, TRUE);
1043 if (data->user_setup)
1044 data->user_setup (data->user_setup_data);
1048 notify_desktop_launch (GDBusConnection *session_bus,
1049 GDesktopAppInfo *info,
1051 const char *display,
1056 GVariantBuilder uri_variant;
1057 GVariantBuilder extras_variant;
1059 const char *desktop_file_id;
1060 const char *gio_desktop_file;
1062 if (session_bus == NULL)
1065 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
1066 for (iter = uris; iter; iter = iter->next)
1067 g_variant_builder_add (&uri_variant, "s", iter->data);
1069 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
1070 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
1071 g_variant_builder_add (&extras_variant, "{sv}",
1075 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
1076 if (gio_desktop_file != NULL)
1077 g_variant_builder_add (&extras_variant, "{sv}",
1078 "origin-desktop-file",
1079 g_variant_new_bytestring (gio_desktop_file));
1080 if (g_get_prgname () != NULL)
1081 g_variant_builder_add (&extras_variant, "{sv}",
1083 g_variant_new_bytestring (g_get_prgname ()));
1084 g_variant_builder_add (&extras_variant, "{sv}",
1087 (gint64)getpid ()));
1090 desktop_file_id = info->filename;
1091 else if (info->desktop_id)
1092 desktop_file_id = info->desktop_id;
1094 desktop_file_id = "";
1096 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
1097 "org.gtk.gio.DesktopAppInfo",
1099 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
1100 g_variant_new_bytestring (desktop_file_id),
1101 display ? display : "",
1105 g_dbus_connection_send_message (session_bus,
1109 g_object_unref (msg);
1112 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
1115 _g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
1117 GAppLaunchContext *launch_context,
1118 GSpawnFlags spawn_flags,
1119 GSpawnChildSetupFunc user_setup,
1120 gpointer user_setup_data,
1121 GDesktopAppLaunchCallback pid_callback,
1122 gpointer pid_callback_data,
1125 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1126 GDBusConnection *session_bus;
1127 gboolean completed = FALSE;
1131 ChildSetupData data;
1133 g_return_val_if_fail (appinfo != NULL, FALSE);
1137 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
1142 GList *launched_uris;
1146 if (!expand_application_parameters (info, &uris,
1147 &argc, &argv, error))
1150 /* Get the subset of URIs we're launching with this process */
1151 launched_uris = NULL;
1152 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
1153 launched_uris = g_list_prepend (launched_uris, iter->data);
1154 launched_uris = g_list_reverse (launched_uris);
1156 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
1158 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1159 _("Unable to find terminal required for application"));
1163 data.user_setup = user_setup;
1164 data.user_setup_data = user_setup_data;
1165 data.display = NULL;
1167 data.desktop_file = info->filename;
1171 GList *launched_files = create_files_for_uris (launched_uris);
1173 data.display = g_app_launch_context_get_display (launch_context,
1177 if (info->startup_notify)
1178 data.sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
1181 g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
1182 g_list_free (launched_files);
1185 if (!g_spawn_async (info->path,
1195 g_app_launch_context_launch_failed (launch_context, data.sn_id);
1197 g_free (data.sn_id);
1198 g_free (data.display);
1199 g_list_free (launched_uris);
1204 if (pid_callback != NULL)
1205 pid_callback (info, pid, pid_callback_data);
1207 notify_desktop_launch (session_bus,
1214 g_free (data.sn_id);
1215 g_free (data.display);
1216 g_list_free (launched_uris);
1221 while (uris != NULL);
1223 /* TODO - need to handle the process exiting immediately
1224 * after launching an app. See http://bugzilla.gnome.org/606960
1226 if (session_bus != NULL)
1228 /* This asynchronous flush holds a reference until it completes,
1229 * which ensures that the following unref won't immediately kill
1230 * the connection if we were the initial owner.
1232 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
1233 g_object_unref (session_bus);
1245 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
1247 GAppLaunchContext *launch_context,
1250 return _g_desktop_app_info_launch_uris_internal (appinfo, uris,
1252 _SPAWN_FLAGS_DEFAULT,
1253 NULL, NULL, NULL, NULL,
1258 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1260 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1262 return info->exec &&
1263 ((strstr (info->exec, "%u") != NULL) ||
1264 (strstr (info->exec, "%U") != NULL));
1268 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1270 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1272 return info->exec &&
1273 ((strstr (info->exec, "%f") != NULL) ||
1274 (strstr (info->exec, "%F") != NULL));
1278 g_desktop_app_info_launch (GAppInfo *appinfo,
1280 GAppLaunchContext *launch_context,
1290 uri = g_file_get_uri (files->data);
1291 uris = g_list_prepend (uris, uri);
1292 files = files->next;
1295 uris = g_list_reverse (uris);
1297 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1299 g_list_foreach (uris, (GFunc)g_free, NULL);
1306 * g_desktop_app_info_launch_uris_as_manager:
1307 * @appinfo: a #GDesktopAppInfo
1308 * @uris: (element-type utf8): List of URIs
1309 * @launch_context: a #GAppLaunchContext
1310 * @spawn_flags: #GSpawnFlags, used for each process
1311 * @user_setup: (scope call): a #GSpawnChildSetupFunc, used once for
1313 * @user_setup_data: (closure user_setup): User data for @user_setup
1314 * @pid_callback: (scope call): Callback for child processes
1315 * @pid_callback_data: (closure pid_callback): User data for @callback
1318 * This function performs the equivalent of g_app_info_launch_uris(),
1319 * but is intended primarily for operating system components that
1320 * launch applications. Ordinary applications should use
1321 * g_app_info_launch_uris().
1323 * In contrast to g_app_info_launch_uris(), all processes created will
1324 * always be run directly as children as if by the UNIX fork()/exec()
1327 * This guarantee allows additional control over the exact environment
1328 * of the child processes, which is provided via a setup function
1329 * @setup, as well as the process identifier of each child process via
1330 * @pid_callback. See g_spawn_async() for more information about the
1331 * semantics of the @setup function.
1334 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
1336 GAppLaunchContext *launch_context,
1337 GSpawnFlags spawn_flags,
1338 GSpawnChildSetupFunc user_setup,
1339 gpointer user_setup_data,
1340 GDesktopAppLaunchCallback pid_callback,
1341 gpointer pid_callback_data,
1344 return _g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
1355 G_LOCK_DEFINE_STATIC (g_desktop_env);
1356 static gchar *g_desktop_env = NULL;
1359 * g_desktop_app_info_set_desktop_env:
1360 * @desktop_env: a string specifying what desktop this is
1362 * Sets the name of the desktop that the application is running in.
1363 * This is used by g_app_info_should_show() to evaluate the
1364 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1365 * desktop entry fields.
1367 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1368 * Menu specification</ulink> recognizes the following:
1370 * <member>GNOME</member>
1371 * <member>KDE</member>
1372 * <member>ROX</member>
1373 * <member>XFCE</member>
1374 * <member>Old</member>
1377 * Should be called only once; subsequent calls are ignored.
1380 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1382 G_LOCK (g_desktop_env);
1384 g_desktop_env = g_strdup (desktop_env);
1385 G_UNLOCK (g_desktop_env);
1389 g_desktop_app_info_should_show (GAppInfo *appinfo)
1391 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1393 const gchar *desktop_env;
1396 if (info->nodisplay)
1399 G_LOCK (g_desktop_env);
1400 desktop_env = g_desktop_env;
1401 G_UNLOCK (g_desktop_env);
1403 if (info->only_show_in)
1405 if (desktop_env == NULL)
1409 for (i = 0; info->only_show_in[i] != NULL; i++)
1411 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1421 if (info->not_show_in && desktop_env)
1423 for (i = 0; info->not_show_in[i] != NULL; i++)
1425 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1439 ensure_dir (DirType type,
1442 char *path, *display_name;
1445 if (type == APP_DIR)
1446 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1448 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1451 if (g_mkdir_with_parents (path, 0700) == 0)
1455 display_name = g_filename_display_name (path);
1456 if (type == APP_DIR)
1457 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1458 _("Can't create user application configuration folder %s: %s"),
1459 display_name, g_strerror (errsv));
1461 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1462 _("Can't create user MIME configuration folder %s: %s"),
1463 display_name, g_strerror (errsv));
1465 g_free (display_name);
1472 update_mimeapps_list (const char *desktop_id,
1473 const char *content_type,
1474 UpdateMimeFlags flags,
1477 char *dirname, *filename, *string;
1479 gboolean load_succeeded, res, explicit_default;
1480 char **old_list, **list;
1482 gsize length, data_size;
1485 char **content_types;
1487 /* Don't add both at start and end */
1488 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
1489 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
1491 dirname = ensure_dir (APP_DIR, error);
1495 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1498 key_file = g_key_file_new ();
1499 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1500 if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1502 g_key_file_free (key_file);
1503 key_file = g_key_file_new ();
1508 content_types = g_new (char *, 2);
1509 content_types[0] = g_strdup (content_type);
1510 content_types[1] = NULL;
1514 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
1517 explicit_default = FALSE;
1519 for (k = 0; content_types && content_types[k]; k++)
1521 /* set as default, if requested so */
1522 string = g_key_file_get_string (key_file,
1523 DEFAULT_APPLICATIONS_GROUP,
1527 if (g_strcmp0 (string, desktop_id) != 0 &&
1528 (flags & UPDATE_MIME_SET_DEFAULT))
1531 string = g_strdup (desktop_id);
1533 /* add in the non-default list too, if it's not already there */
1534 flags |= UPDATE_MIME_SET_NON_DEFAULT;
1537 if (string == NULL || desktop_id == NULL)
1538 g_key_file_remove_key (key_file,
1539 DEFAULT_APPLICATIONS_GROUP,
1544 g_key_file_set_string (key_file,
1545 DEFAULT_APPLICATIONS_GROUP,
1549 explicit_default = TRUE;
1557 /* reuse the list from above */
1561 g_strfreev (content_types);
1562 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1565 for (k = 0; content_types && content_types[k]; k++)
1567 /* Add to the right place in the list */
1570 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1571 content_types[k], &length, NULL);
1573 list = g_new (char *, 1 + length + 1);
1577 /* if we're adding a last-used hint, just put the application in front of the list */
1578 if (flags & UPDATE_MIME_SET_LAST_USED)
1580 /* avoid adding this again as non-default later */
1581 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1582 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1584 list[i++] = g_strdup (desktop_id);
1589 for (j = 0; old_list[j] != NULL; j++)
1591 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1593 /* rewrite other entries if they're different from the new one */
1594 list[i++] = g_strdup (old_list[j]);
1596 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1598 /* we encountered an old entry which is equal to the one we're adding as non-default,
1599 * don't change its position in the list.
1601 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1602 list[i++] = g_strdup (old_list[j]);
1607 /* add it at the end of the list */
1608 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1609 list[i++] = g_strdup (desktop_id);
1613 g_strfreev (old_list);
1615 if (list[0] == NULL || desktop_id == NULL)
1616 g_key_file_remove_key (key_file,
1617 ADDED_ASSOCIATIONS_GROUP,
1622 g_key_file_set_string_list (key_file,
1623 ADDED_ASSOCIATIONS_GROUP,
1625 (const char * const *)list, i);
1627 /* if we had no explicit default set, we should add the system default to the
1628 * list, to avoid overriding it with applications from this list.
1630 if (!explicit_default)
1632 system_list = get_all_desktop_entries_for_mime_type (content_type, (const char **) list, FALSE, NULL);
1634 if (system_list != NULL)
1636 string = system_list->data;
1638 g_key_file_set_string (key_file,
1639 DEFAULT_APPLICATIONS_GROUP,
1644 g_list_free_full (system_list, g_free);
1653 /* reuse the list from above */
1657 g_strfreev (content_types);
1658 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1661 for (k = 0; content_types && content_types[k]; k++)
1663 /* Remove from removed associations group (unless remove) */
1666 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1667 content_types[k], &length, NULL);
1669 list = g_new (char *, 1 + length + 1);
1672 if (flags & UPDATE_MIME_REMOVE)
1673 list[i++] = g_strdup (desktop_id);
1676 for (j = 0; old_list[j] != NULL; j++)
1678 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1679 list[i++] = g_strdup (old_list[j]);
1684 g_strfreev (old_list);
1686 if (list[0] == NULL || desktop_id == NULL)
1687 g_key_file_remove_key (key_file,
1688 REMOVED_ASSOCIATIONS_GROUP,
1692 g_key_file_set_string_list (key_file,
1693 REMOVED_ASSOCIATIONS_GROUP,
1695 (const char * const *)list, i);
1700 g_strfreev (content_types);
1702 data = g_key_file_to_data (key_file, &data_size, error);
1703 g_key_file_free (key_file);
1705 res = g_file_set_contents (filename, data, data_size, error);
1707 mime_info_cache_reload (NULL);
1716 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
1717 const char *content_type,
1720 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1722 if (!g_desktop_app_info_ensure_saved (info, error))
1725 /* both add support for the content type and set as last used */
1726 return update_mimeapps_list (info->desktop_id, content_type,
1727 UPDATE_MIME_SET_NON_DEFAULT |
1728 UPDATE_MIME_SET_LAST_USED,
1733 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1734 const char *content_type,
1737 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1739 if (!g_desktop_app_info_ensure_saved (info, error))
1742 return update_mimeapps_list (info->desktop_id, content_type,
1743 UPDATE_MIME_SET_DEFAULT,
1748 update_program_done (GPid pid,
1752 /* Did the application exit correctly */
1753 if (WIFEXITED (status) &&
1754 WEXITSTATUS (status) == 0)
1756 /* Here we could clean out any caches in use */
1761 run_update_command (char *command,
1770 GError *error = NULL;
1773 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1775 if (g_spawn_async ("/", argv,
1777 G_SPAWN_SEARCH_PATH |
1778 G_SPAWN_STDOUT_TO_DEV_NULL |
1779 G_SPAWN_STDERR_TO_DEV_NULL |
1780 G_SPAWN_DO_NOT_REAP_CHILD,
1781 NULL, NULL, /* No setup function */
1784 g_child_watch_add (pid, update_program_done, NULL);
1787 /* If we get an error at this point, it's quite likely the user doesn't
1788 * have an installed copy of either 'update-mime-database' or
1789 * 'update-desktop-database'. I don't think we want to popup an error
1790 * dialog at this point, so we just do a g_warning to give the user a
1791 * chance of debugging it.
1793 g_warning ("%s", error->message);
1800 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1801 const char *extension,
1804 char *filename, *basename, *mimetype;
1808 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1811 dirname = ensure_dir (MIMETYPE_DIR, error);
1815 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1816 filename = g_build_filename (dirname, basename, NULL);
1820 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1822 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1827 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1828 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1829 " <mime-type type=\"%s\">\n"
1830 " <comment>%s document</comment>\n"
1831 " <glob pattern=\"*.%s\"/>\n"
1833 "</mime-info>\n", mimetype, extension, extension);
1835 g_file_set_contents (filename, contents, -1, NULL);
1838 run_update_command ("update-mime-database", "mime");
1842 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1852 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1853 const char *content_type,
1856 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1858 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1861 return update_mimeapps_list (info->desktop_id, content_type,
1862 UPDATE_MIME_SET_NON_DEFAULT,
1867 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1873 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1874 const char *content_type,
1877 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1879 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1882 return update_mimeapps_list (info->desktop_id, content_type,
1888 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1894 char *data, *desktop_id;
1899 if (info->filename != NULL)
1902 /* This is only used for object created with
1903 * g_app_info_create_from_commandline. All other
1904 * object should have a filename
1907 dirname = ensure_dir (APP_DIR, error);
1911 key_file = g_key_file_new ();
1913 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1914 "Encoding", "UTF-8");
1915 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1916 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1917 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1918 G_KEY_FILE_DESKTOP_KEY_TYPE,
1919 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1921 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1922 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1924 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1925 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1927 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1928 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1930 if (info->generic_name != NULL)
1931 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1932 GENERIC_NAME_KEY, info->generic_name);
1934 if (info->fullname != NULL)
1935 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1936 FULL_NAME_KEY, info->fullname);
1938 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1939 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1941 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1942 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1944 data = g_key_file_to_data (key_file, &data_size, NULL);
1945 g_key_file_free (key_file);
1947 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1948 filename = g_build_filename (dirname, desktop_id, NULL);
1949 g_free (desktop_id);
1952 fd = g_mkstemp (filename);
1957 display_name = g_filename_display_name (filename);
1958 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1959 _("Can't create user desktop file %s"), display_name);
1960 g_free (display_name);
1966 desktop_id = g_path_get_basename (filename);
1970 res = g_file_set_contents (filename, data, data_size, error);
1973 g_free (desktop_id);
1978 info->filename = filename;
1979 info->desktop_id = desktop_id;
1981 run_update_command ("update-desktop-database", "applications");
1987 g_desktop_app_info_can_delete (GAppInfo *appinfo)
1989 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1993 if (strstr (info->filename, "/userapp-"))
1994 return g_access (info->filename, W_OK) == 0;
2001 g_desktop_app_info_delete (GAppInfo *appinfo)
2003 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2007 if (g_remove (info->filename) == 0)
2009 update_mimeapps_list (info->desktop_id, NULL,
2013 g_free (info->filename);
2014 info->filename = NULL;
2015 g_free (info->desktop_id);
2016 info->desktop_id = NULL;
2026 * g_app_info_create_from_commandline:
2027 * @commandline: the commandline to use
2028 * @application_name: (allow-none): the application name, or %NULL to use @commandline
2029 * @flags: flags that can specify details of the created #GAppInfo
2030 * @error: a #GError location to store the error occuring, %NULL to ignore.
2032 * Creates a new #GAppInfo from the given information.
2034 * Returns: (transfer full): new #GAppInfo for given command.
2037 g_app_info_create_from_commandline (const char *commandline,
2038 const char *application_name,
2039 GAppInfoCreateFlags flags,
2044 GDesktopAppInfo *info;
2046 g_return_val_if_fail (commandline, NULL);
2048 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
2050 info->filename = NULL;
2051 info->desktop_id = NULL;
2053 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
2054 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
2055 info->hidden = FALSE;
2056 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
2057 info->exec = g_strconcat (commandline, " %u", NULL);
2059 info->exec = g_strconcat (commandline, " %f", NULL);
2060 info->nodisplay = TRUE;
2061 info->binary = binary_from_exec (info->exec);
2063 if (application_name)
2064 info->name = g_strdup (application_name);
2067 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
2068 split = g_strsplit (commandline, " ", 2);
2069 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
2071 info->name = basename;
2072 if (info->name == NULL)
2073 info->name = g_strdup ("custom");
2075 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
2077 return G_APP_INFO (info);
2081 g_desktop_app_info_iface_init (GAppInfoIface *iface)
2083 iface->dup = g_desktop_app_info_dup;
2084 iface->equal = g_desktop_app_info_equal;
2085 iface->get_id = g_desktop_app_info_get_id;
2086 iface->get_name = g_desktop_app_info_get_name;
2087 iface->get_description = g_desktop_app_info_get_description;
2088 iface->get_executable = g_desktop_app_info_get_executable;
2089 iface->get_icon = g_desktop_app_info_get_icon;
2090 iface->launch = g_desktop_app_info_launch;
2091 iface->supports_uris = g_desktop_app_info_supports_uris;
2092 iface->supports_files = g_desktop_app_info_supports_files;
2093 iface->launch_uris = g_desktop_app_info_launch_uris;
2094 iface->should_show = g_desktop_app_info_should_show;
2095 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
2096 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
2097 iface->add_supports_type = g_desktop_app_info_add_supports_type;
2098 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
2099 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
2100 iface->can_delete = g_desktop_app_info_can_delete;
2101 iface->do_delete = g_desktop_app_info_delete;
2102 iface->get_commandline = g_desktop_app_info_get_commandline;
2103 iface->get_display_name = g_desktop_app_info_get_display_name;
2104 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
2108 app_info_in_list (GAppInfo *info,
2111 while (list != NULL)
2113 if (g_app_info_equal (info, list->data))
2121 * g_app_info_get_recommended_for_type:
2122 * @content_type: the content type to find a #GAppInfo for
2124 * Gets a list of recommended #GAppInfos for a given content type, i.e.
2125 * those applications which claim to support the given content type exactly,
2126 * and not by MIME type subclassing.
2127 * Note that the first application of the list is the last used one, i.e.
2128 * the last one for which #g_app_info_set_as_last_used_for_type has been
2131 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2132 * for given @content_type or %NULL on error.
2137 g_app_info_get_recommended_for_type (const gchar *content_type)
2139 GList *desktop_entries, *l;
2141 GDesktopAppInfo *info;
2143 g_return_val_if_fail (content_type != NULL, NULL);
2145 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
2148 for (l = desktop_entries; l != NULL; l = l->next)
2150 char *desktop_entry = l->data;
2152 info = g_desktop_app_info_new (desktop_entry);
2155 if (app_info_in_list (G_APP_INFO (info), infos))
2156 g_object_unref (info);
2158 infos = g_list_prepend (infos, info);
2160 g_free (desktop_entry);
2163 g_list_free (desktop_entries);
2165 return g_list_reverse (infos);
2169 * g_app_info_get_fallback_for_type:
2170 * @content_type: the content type to find a #GAppInfo for
2172 * Gets a list of fallback #GAppInfos for a given content type, i.e.
2173 * those applications which claim to support the given content type
2174 * by MIME type subclassing and not directly.
2176 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2177 * for given @content_type or %NULL on error.
2182 g_app_info_get_fallback_for_type (const gchar *content_type)
2184 GList *desktop_entries, *l;
2185 GList *infos, *recommended_infos;
2186 GDesktopAppInfo *info;
2188 g_return_val_if_fail (content_type != NULL, NULL);
2190 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
2191 recommended_infos = g_app_info_get_recommended_for_type (content_type);
2194 for (l = desktop_entries; l != NULL; l = l->next)
2196 char *desktop_entry = l->data;
2198 info = g_desktop_app_info_new (desktop_entry);
2201 if (app_info_in_list (G_APP_INFO (info), infos) ||
2202 app_info_in_list (G_APP_INFO (info), recommended_infos))
2203 g_object_unref (info);
2205 infos = g_list_prepend (infos, info);
2207 g_free (desktop_entry);
2210 g_list_free (desktop_entries);
2211 g_list_free_full (recommended_infos, g_object_unref);
2213 return g_list_reverse (infos);
2217 * g_app_info_get_all_for_type:
2218 * @content_type: the content type to find a #GAppInfo for
2220 * Gets a list of all #GAppInfos for a given content type.
2222 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2223 * for given @content_type or %NULL on error.
2226 g_app_info_get_all_for_type (const char *content_type)
2228 GList *desktop_entries, *l;
2230 char *user_default = NULL;
2231 GDesktopAppInfo *info;
2233 g_return_val_if_fail (content_type != NULL, NULL);
2235 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2238 /* put the user default in front of the list, for compatibility */
2239 if (user_default != NULL)
2241 info = g_desktop_app_info_new (user_default);
2244 infos = g_list_prepend (infos, info);
2247 g_free (user_default);
2249 for (l = desktop_entries; l != NULL; l = l->next)
2251 char *desktop_entry = l->data;
2253 info = g_desktop_app_info_new (desktop_entry);
2256 if (app_info_in_list (G_APP_INFO (info), infos))
2257 g_object_unref (info);
2259 infos = g_list_prepend (infos, info);
2261 g_free (desktop_entry);
2264 g_list_free (desktop_entries);
2266 return g_list_reverse (infos);
2270 * g_app_info_reset_type_associations:
2271 * @content_type: a content type
2273 * Removes all changes to the type associations done by
2274 * g_app_info_set_as_default_for_type(),
2275 * g_app_info_set_as_default_for_extension(),
2276 * g_app_info_add_supports_type() or g_app_info_remove_supports_type().
2281 g_app_info_reset_type_associations (const char *content_type)
2283 update_mimeapps_list (NULL, content_type,
2289 * g_app_info_get_default_for_type:
2290 * @content_type: the content type to find a #GAppInfo for
2291 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
2294 * Gets the #GAppInfo that corresponds to a given content type.
2296 * Returns: (transfer full): #GAppInfo for given @content_type or
2300 g_app_info_get_default_for_type (const char *content_type,
2301 gboolean must_support_uris)
2303 GList *desktop_entries, *l;
2304 char *user_default = NULL;
2307 g_return_val_if_fail (content_type != NULL, NULL);
2309 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2313 if (user_default != NULL)
2315 info = (GAppInfo *) g_desktop_app_info_new (user_default);
2319 if (must_support_uris && !g_app_info_supports_uris (info))
2321 g_object_unref (info);
2327 g_free (user_default);
2331 g_list_free_full (desktop_entries, g_free);
2335 /* pick the first from the other list that matches our URI
2338 for (l = desktop_entries; l != NULL; l = l->next)
2340 char *desktop_entry = l->data;
2342 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
2345 if (must_support_uris && !g_app_info_supports_uris (info))
2347 g_object_unref (info);
2355 g_list_free_full (desktop_entries, g_free);
2361 * g_app_info_get_default_for_uri_scheme:
2362 * @uri_scheme: a string containing a URI scheme.
2364 * Gets the default application for launching applications
2365 * using this URI scheme. A URI scheme is the initial part
2366 * of the URI, up to but not including the ':', e.g. "http",
2369 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
2372 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
2375 char *content_type, *scheme_down;
2377 scheme_down = g_ascii_strdown (uri_scheme, -1);
2378 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
2379 g_free (scheme_down);
2380 app_info = g_app_info_get_default_for_type (content_type, FALSE);
2381 g_free (content_type);
2387 get_apps_from_dir (GHashTable *apps,
2388 const char *dirname,
2392 const char *basename;
2393 char *filename, *subprefix, *desktop_id;
2395 GDesktopAppInfo *appinfo;
2397 dir = g_dir_open (dirname, 0, NULL);
2400 while ((basename = g_dir_read_name (dir)) != NULL)
2402 filename = g_build_filename (dirname, basename, NULL);
2403 if (g_str_has_suffix (basename, ".desktop"))
2405 desktop_id = g_strconcat (prefix, basename, NULL);
2407 /* Use _extended so we catch NULLs too (hidden) */
2408 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
2410 appinfo = g_desktop_app_info_new_from_filename (filename);
2413 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
2415 g_object_unref (appinfo);
2420 if (appinfo || hidden)
2422 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
2426 /* Reuse instead of strdup here */
2427 appinfo->desktop_id = desktop_id;
2432 g_free (desktop_id);
2436 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
2438 subprefix = g_strconcat (prefix, basename, "-", NULL);
2439 get_apps_from_dir (apps, filename, subprefix);
2451 * g_app_info_get_all:
2453 * Gets a list of all of the applications currently registered
2456 * For desktop files, this includes applications that have
2457 * <literal>NoDisplay=true</literal> set or are excluded from
2458 * display by means of <literal>OnlyShowIn</literal> or
2459 * <literal>NotShowIn</literal>. See g_app_info_should_show().
2460 * The returned list does not include applications which have
2461 * the <literal>Hidden</literal> key set.
2463 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
2466 g_app_info_get_all (void)
2468 const char * const *dirs;
2470 GHashTableIter iter;
2475 dirs = get_applications_search_path ();
2477 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
2481 for (i = 0; dirs[i] != NULL; i++)
2482 get_apps_from_dir (apps, dirs[i], "");
2486 g_hash_table_iter_init (&iter, apps);
2487 while (g_hash_table_iter_next (&iter, NULL, &value))
2490 infos = g_list_prepend (infos, value);
2493 g_hash_table_destroy (apps);
2495 return g_list_reverse (infos);
2498 /* Cacheing of mimeinfo.cache and defaults.list files */
2502 GHashTable *mime_info_cache_map;
2503 GHashTable *defaults_list_map;
2504 GHashTable *mimeapps_list_added_map;
2505 GHashTable *mimeapps_list_removed_map;
2506 GHashTable *mimeapps_list_defaults_map;
2507 time_t mime_info_cache_timestamp;
2508 time_t defaults_list_timestamp;
2509 time_t mimeapps_list_timestamp;
2513 GList *dirs; /* mimeinfo.cache and defaults.list */
2514 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
2515 time_t last_stat_time;
2516 guint should_ping_mime_monitor : 1;
2519 static MimeInfoCache *mime_info_cache = NULL;
2520 G_LOCK_DEFINE_STATIC (mime_info_cache);
2522 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2523 const char *mime_type,
2524 char **new_desktop_file_ids);
2526 static MimeInfoCache * mime_info_cache_new (void);
2529 destroy_info_cache_value (gpointer key,
2533 g_list_foreach (value, (GFunc)g_free, NULL);
2534 g_list_free (value);
2538 destroy_info_cache_map (GHashTable *info_cache_map)
2540 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
2541 g_hash_table_destroy (info_cache_map);
2545 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2546 const char *cache_file,
2552 filename = g_build_filename (dir->path, cache_file, NULL);
2554 if (g_stat (filename, &buf) < 0)
2561 if (buf.st_mtime != *timestamp)
2567 /* Call with lock held */
2569 remove_all (gpointer key,
2578 mime_info_cache_blow_global_cache (void)
2580 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
2585 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2589 gchar *filename, **mime_types;
2596 if (dir->mime_info_cache_map != NULL &&
2597 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2598 &dir->mime_info_cache_timestamp))
2601 if (dir->mime_info_cache_map != NULL)
2602 destroy_info_cache_map (dir->mime_info_cache_map);
2604 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2605 (GDestroyNotify) g_free,
2608 key_file = g_key_file_new ();
2610 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2612 if (g_stat (filename, &buf) < 0)
2615 if (dir->mime_info_cache_timestamp > 0)
2616 mime_info_cache->should_ping_mime_monitor = TRUE;
2618 dir->mime_info_cache_timestamp = buf.st_mtime;
2620 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2625 if (load_error != NULL)
2628 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2631 if (load_error != NULL)
2634 for (i = 0; mime_types[i] != NULL; i++)
2636 gchar **desktop_file_ids;
2637 char *unaliased_type;
2638 desktop_file_ids = g_key_file_get_string_list (key_file,
2644 if (desktop_file_ids == NULL)
2647 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2648 mime_info_cache_dir_add_desktop_entries (dir,
2651 g_free (unaliased_type);
2653 g_strfreev (desktop_file_ids);
2656 g_strfreev (mime_types);
2657 g_key_file_free (key_file);
2662 g_key_file_free (key_file);
2664 if (mime_types != NULL)
2665 g_strfreev (mime_types);
2668 g_error_free (load_error);
2672 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2676 gchar *filename, **mime_types;
2677 char *unaliased_type;
2678 char **desktop_file_ids;
2685 if (dir->defaults_list_map != NULL &&
2686 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2687 &dir->defaults_list_timestamp))
2690 if (dir->defaults_list_map != NULL)
2691 g_hash_table_destroy (dir->defaults_list_map);
2692 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2693 g_free, (GDestroyNotify)g_strfreev);
2696 key_file = g_key_file_new ();
2698 filename = g_build_filename (dir->path, "defaults.list", NULL);
2699 if (g_stat (filename, &buf) < 0)
2702 if (dir->defaults_list_timestamp > 0)
2703 mime_info_cache->should_ping_mime_monitor = TRUE;
2705 dir->defaults_list_timestamp = buf.st_mtime;
2707 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2711 if (load_error != NULL)
2714 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2716 if (mime_types != NULL)
2718 for (i = 0; mime_types[i] != NULL; i++)
2720 desktop_file_ids = g_key_file_get_string_list (key_file,
2721 DEFAULT_APPLICATIONS_GROUP,
2725 if (desktop_file_ids == NULL)
2728 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2729 g_hash_table_replace (dir->defaults_list_map,
2734 g_strfreev (mime_types);
2737 g_key_file_free (key_file);
2742 g_key_file_free (key_file);
2744 if (mime_types != NULL)
2745 g_strfreev (mime_types);
2748 g_error_free (load_error);
2752 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2756 gchar *filename, **mime_types;
2757 char *unaliased_type;
2758 char **desktop_file_ids;
2766 if (dir->mimeapps_list_added_map != NULL &&
2767 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2768 &dir->mimeapps_list_timestamp))
2771 if (dir->mimeapps_list_added_map != NULL)
2772 g_hash_table_destroy (dir->mimeapps_list_added_map);
2773 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2774 g_free, (GDestroyNotify)g_strfreev);
2776 if (dir->mimeapps_list_removed_map != NULL)
2777 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2778 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2779 g_free, (GDestroyNotify)g_strfreev);
2781 if (dir->mimeapps_list_defaults_map != NULL)
2782 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2783 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2786 key_file = g_key_file_new ();
2788 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2789 if (g_stat (filename, &buf) < 0)
2792 if (dir->mimeapps_list_timestamp > 0)
2793 mime_info_cache->should_ping_mime_monitor = TRUE;
2795 dir->mimeapps_list_timestamp = buf.st_mtime;
2797 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2801 if (load_error != NULL)
2804 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2806 if (mime_types != NULL)
2808 for (i = 0; mime_types[i] != NULL; i++)
2810 desktop_file_ids = g_key_file_get_string_list (key_file,
2811 ADDED_ASSOCIATIONS_GROUP,
2815 if (desktop_file_ids == NULL)
2818 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2819 g_hash_table_replace (dir->mimeapps_list_added_map,
2824 g_strfreev (mime_types);
2827 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2829 if (mime_types != NULL)
2831 for (i = 0; mime_types[i] != NULL; i++)
2833 desktop_file_ids = g_key_file_get_string_list (key_file,
2834 REMOVED_ASSOCIATIONS_GROUP,
2838 if (desktop_file_ids == NULL)
2841 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2842 g_hash_table_replace (dir->mimeapps_list_removed_map,
2847 g_strfreev (mime_types);
2850 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2852 if (mime_types != NULL)
2854 for (i = 0; mime_types[i] != NULL; i++)
2856 desktop_id = g_key_file_get_string (key_file,
2857 DEFAULT_APPLICATIONS_GROUP,
2860 if (desktop_id == NULL)
2863 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2864 g_hash_table_replace (dir->mimeapps_list_defaults_map,
2869 g_strfreev (mime_types);
2872 g_key_file_free (key_file);
2877 g_key_file_free (key_file);
2879 if (mime_types != NULL)
2880 g_strfreev (mime_types);
2883 g_error_free (load_error);
2886 static MimeInfoCacheDir *
2887 mime_info_cache_dir_new (const char *path)
2889 MimeInfoCacheDir *dir;
2891 dir = g_new0 (MimeInfoCacheDir, 1);
2892 dir->path = g_strdup (path);
2898 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2903 if (dir->mime_info_cache_map != NULL)
2905 destroy_info_cache_map (dir->mime_info_cache_map);
2906 dir->mime_info_cache_map = NULL;
2910 if (dir->defaults_list_map != NULL)
2912 g_hash_table_destroy (dir->defaults_list_map);
2913 dir->defaults_list_map = NULL;
2916 if (dir->mimeapps_list_added_map != NULL)
2918 g_hash_table_destroy (dir->mimeapps_list_added_map);
2919 dir->mimeapps_list_added_map = NULL;
2922 if (dir->mimeapps_list_removed_map != NULL)
2924 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2925 dir->mimeapps_list_removed_map = NULL;
2928 if (dir->mimeapps_list_defaults_map != NULL)
2930 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2931 dir->mimeapps_list_defaults_map = NULL;
2938 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2939 const char *mime_type,
2940 char **new_desktop_file_ids)
2942 GList *desktop_file_ids;
2945 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2948 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2950 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
2951 desktop_file_ids = g_list_append (desktop_file_ids,
2952 g_strdup (new_desktop_file_ids[i]));
2955 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2959 mime_info_cache_init_dir_lists (void)
2961 const char * const *dirs;
2964 mime_info_cache = mime_info_cache_new ();
2966 dirs = get_applications_search_path ();
2968 for (i = 0; dirs[i] != NULL; i++)
2970 MimeInfoCacheDir *dir;
2972 dir = mime_info_cache_dir_new (dirs[i]);
2976 mime_info_cache_dir_init (dir);
2977 mime_info_cache_dir_init_defaults_list (dir);
2978 mime_info_cache_dir_init_mimeapps_list (dir);
2980 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2986 mime_info_cache_update_dir_lists (void)
2990 tmp = mime_info_cache->dirs;
2994 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2996 /* No need to do this if we had file monitors... */
2997 mime_info_cache_blow_global_cache ();
2998 mime_info_cache_dir_init (dir);
2999 mime_info_cache_dir_init_defaults_list (dir);
3000 mime_info_cache_dir_init_mimeapps_list (dir);
3007 mime_info_cache_init (void)
3009 G_LOCK (mime_info_cache);
3010 if (mime_info_cache == NULL)
3011 mime_info_cache_init_dir_lists ();
3017 if (now >= mime_info_cache->last_stat_time + 10)
3019 mime_info_cache_update_dir_lists ();
3020 mime_info_cache->last_stat_time = now;
3024 if (mime_info_cache->should_ping_mime_monitor)
3026 /* g_idle_add (emit_mime_changed, NULL); */
3027 mime_info_cache->should_ping_mime_monitor = FALSE;
3030 G_UNLOCK (mime_info_cache);
3033 static MimeInfoCache *
3034 mime_info_cache_new (void)
3036 MimeInfoCache *cache;
3038 cache = g_new0 (MimeInfoCache, 1);
3040 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
3041 (GDestroyNotify) g_free,
3042 (GDestroyNotify) g_free);
3047 mime_info_cache_free (MimeInfoCache *cache)
3052 g_list_foreach (cache->dirs,
3053 (GFunc) mime_info_cache_dir_free,
3055 g_list_free (cache->dirs);
3056 g_hash_table_destroy (cache->global_defaults_cache);
3061 * mime_info_cache_reload:
3062 * @dir: directory path which needs reloading.
3064 * Reload the mime information for the @dir.
3067 mime_info_cache_reload (const char *dir)
3069 /* FIXME: just reload the dir that needs reloading,
3070 * don't blow the whole cache
3072 if (mime_info_cache != NULL)
3074 G_LOCK (mime_info_cache);
3075 mime_info_cache_free (mime_info_cache);
3076 mime_info_cache = NULL;
3077 G_UNLOCK (mime_info_cache);
3082 append_desktop_entry (GList *list,
3083 const char *desktop_entry,
3084 GList *removed_entries)
3086 /* Add if not already in list, and valid */
3087 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
3088 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
3089 list = g_list_prepend (list, g_strdup (desktop_entry));
3095 * get_all_desktop_entries_for_mime_type:
3096 * @mime_type: a mime type.
3097 * @except: NULL or a strv list
3099 * Returns all the desktop ids for @mime_type. The desktop files
3100 * are listed in an order so that default applications are listed before
3101 * non-default ones, and handlers for inherited mimetypes are listed
3102 * after the base ones.
3104 * Optionally doesn't list the desktop ids given in the @except
3106 * Return value: a #GList containing the desktop ids which claim
3107 * to handle @mime_type.
3110 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
3111 const char **except,
3112 gboolean include_fallback,
3113 char **explicit_default)
3115 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
3116 MimeInfoCacheDir *dir;
3117 char *mime_type, *default_entry = NULL;
3120 char **default_entries;
3121 char **removed_associations;
3126 mime_info_cache_init ();
3128 if (include_fallback)
3130 /* collect all ancestors */
3131 mime_types = _g_unix_content_type_get_parents (base_mime_type);
3132 array = g_ptr_array_new ();
3133 for (i = 0; mime_types[i]; i++)
3134 g_ptr_array_add (array, mime_types[i]);
3135 g_free (mime_types);
3136 for (i = 0; i < array->len; i++)
3138 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
3139 for (j = 0; anc[j]; j++)
3141 for (k = 0; k < array->len; k++)
3143 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
3146 if (k == array->len) /* not found */
3147 g_ptr_array_add (array, g_strdup (anc[j]));
3151 g_ptr_array_add (array, NULL);
3152 mime_types = (char **)g_ptr_array_free (array, FALSE);
3156 mime_types = g_malloc0 (2 * sizeof (gchar *));
3157 mime_types[0] = g_strdup (base_mime_type);
3158 mime_types[1] = NULL;
3161 G_LOCK (mime_info_cache);
3163 removed_entries = NULL;
3164 desktop_entries = NULL;
3166 for (i = 0; except != NULL && except[i] != NULL; i++)
3167 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
3169 for (i = 0; mime_types[i] != NULL; i++)
3171 mime_type = mime_types[i];
3173 /* Go through all apps listed in user and system dirs */
3174 for (dir_list = mime_info_cache->dirs;
3176 dir_list = dir_list->next)
3178 dir = dir_list->data;
3180 /* Pick the explicit default application if we got no result earlier
3181 * (ie, for more specific mime types)
3183 if (desktop_entries == NULL)
3185 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
3189 /* Save the default entry if it's the first one we encounter */
3190 if (default_entry == NULL)
3191 default_entry = g_strdup (entry);
3195 /* Then added associations from mimeapps.list */
3196 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
3197 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3198 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3200 /* Then removed associations from mimeapps.list */
3201 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
3202 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
3203 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
3205 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
3206 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
3207 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3208 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3211 /* Go through all entries that support the mimetype */
3212 for (dir_list = mime_info_cache->dirs;
3214 dir_list = dir_list->next)
3216 dir = dir_list->data;
3218 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
3219 for (tmp = list; tmp != NULL; tmp = tmp->next)
3220 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
3224 G_UNLOCK (mime_info_cache);
3226 g_strfreev (mime_types);
3228 if (explicit_default != NULL)
3229 *explicit_default = default_entry;
3231 g_free (default_entry);
3233 g_list_foreach (removed_entries, (GFunc)g_free, NULL);
3234 g_list_free (removed_entries);
3236 desktop_entries = g_list_reverse (desktop_entries);
3238 return desktop_entries;
3241 /* GDesktopAppInfoLookup interface: */
3243 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
3244 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
3247 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
3252 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
3253 * @lookup: a #GDesktopAppInfoLookup
3254 * @uri_scheme: a string containing a URI scheme.
3256 * Gets the default application for launching applications
3257 * using this URI scheme for a particular GDesktopAppInfoLookup
3260 * The GDesktopAppInfoLookup interface and this function is used
3261 * to implement g_app_info_get_default_for_uri_scheme() backends
3262 * in a GIO module. There is no reason for applications to use it
3263 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
3265 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3267 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
3270 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
3271 const char *uri_scheme)
3273 GDesktopAppInfoLookupIface *iface;
3275 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
3277 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
3279 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);