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 #include "gcontenttypeprivate.h"
36 #include "gdesktopappinfo.h"
39 #include "gthemedicon.h"
40 #include "gfileicon.h"
41 #include <glib/gstdio.h>
43 #include "giomodule-priv.h"
49 * SECTION:gdesktopappinfo
50 * @short_description: Application information from desktop files
51 * @include: gio/gdesktopappinfo.h
53 * #GDesktopAppInfo is an implementation of #GAppInfo based on
58 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
59 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
60 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
61 #define MIME_CACHE_GROUP "MIME Cache"
63 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
64 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type);
65 static void mime_info_cache_reload (const char *dir);
66 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
72 * Information about an installed application from a desktop file.
74 struct _GDesktopAppInfo
76 GObject parent_instance;
82 /* FIXME: what about GenericName ? */
96 guint startup_notify : 1;
97 /* FIXME: what about StartupWMClass ? */
100 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
101 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
102 g_desktop_app_info_iface_init))
105 search_path_init (gpointer data)
108 const char * const *data_dirs;
109 const char *user_data_dir;
112 data_dirs = g_get_system_data_dirs ();
113 length = g_strv_length ((char **) data_dirs);
115 args = g_new (char *, length + 2);
118 user_data_dir = g_get_user_data_dir ();
119 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
120 for (i = 0; i < length; i++)
121 args[j++] = g_build_filename (data_dirs[i],
122 "applications", NULL);
128 static const char * const *
129 get_applications_search_path (void)
131 static GOnce once_init = G_ONCE_INIT;
132 return g_once (&once_init, search_path_init, NULL);
136 g_desktop_app_info_finalize (GObject *object)
138 GDesktopAppInfo *info;
140 info = G_DESKTOP_APP_INFO (object);
142 g_free (info->desktop_id);
143 g_free (info->filename);
145 g_free (info->comment);
146 g_free (info->icon_name);
148 g_object_unref (info->icon);
149 g_strfreev (info->only_show_in);
150 g_strfreev (info->not_show_in);
151 g_free (info->try_exec);
153 g_free (info->binary);
156 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
160 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
162 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
164 gobject_class->finalize = g_desktop_app_info_finalize;
168 g_desktop_app_info_init (GDesktopAppInfo *local)
173 binary_from_exec (const char *exec)
175 const char *p, *start;
181 while (*p != ' ' && *p != 0)
184 return g_strndup (start, p - start);
189 * g_desktop_app_info_new_from_keyfile:
190 * @key_file: an opened #GKeyFile
192 * Creates a new #GDesktopAppInfo.
194 * Returns: a new #GDesktopAppInfo or %NULL on error.
199 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
201 GDesktopAppInfo *info;
206 start_group = g_key_file_get_start_group (key_file);
207 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
209 g_free (start_group);
212 g_free (start_group);
214 type = g_key_file_get_string (key_file,
215 G_KEY_FILE_DESKTOP_GROUP,
216 G_KEY_FILE_DESKTOP_KEY_TYPE,
218 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
225 try_exec = g_key_file_get_string (key_file,
226 G_KEY_FILE_DESKTOP_GROUP,
227 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
229 if (try_exec && try_exec[0] != '\0')
232 t = g_find_program_in_path (try_exec);
242 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
243 info->filename = NULL;
245 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
246 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
247 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
248 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
249 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);
250 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);
251 info->try_exec = try_exec;
252 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
253 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
254 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
255 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
256 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
261 if (g_path_is_absolute (info->icon_name))
265 file = g_file_new_for_path (info->icon_name);
266 info->icon = g_file_icon_new (file);
267 g_object_unref (file);
270 info->icon = g_themed_icon_new (info->icon_name);
274 info->binary = binary_from_exec (info->exec);
280 * g_desktop_app_info_new_from_filename:
281 * @filename: a string containing a file name.
283 * Creates a new #GDesktopAppInfo.
285 * Returns: a new #GDesktopAppInfo or %NULL on error.
288 g_desktop_app_info_new_from_filename (const char *filename)
291 GDesktopAppInfo *info = NULL;
293 key_file = g_key_file_new ();
295 if (g_key_file_load_from_file (key_file,
300 info = g_desktop_app_info_new_from_keyfile (key_file);
302 info->filename = g_strdup (filename);
305 g_key_file_free (key_file);
311 * g_desktop_app_info_new:
312 * @desktop_id: the desktop file id
314 * Creates a new #GDesktopAppInfo.
316 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
319 g_desktop_app_info_new (const char *desktop_id)
321 GDesktopAppInfo *appinfo;
322 const char * const *dirs;
326 dirs = get_applications_search_path ();
328 basename = g_strdup (desktop_id);
330 for (i = 0; dirs[i] != NULL; i++)
335 filename = g_build_filename (dirs[i], desktop_id, NULL);
336 appinfo = g_desktop_app_info_new_from_filename (filename);
342 while ((p = strchr (p, '-')) != NULL)
346 filename = g_build_filename (dirs[i], basename, NULL);
347 appinfo = g_desktop_app_info_new_from_filename (filename);
362 appinfo->desktop_id = g_strdup (desktop_id);
364 if (g_desktop_app_info_get_is_hidden (appinfo))
366 g_object_unref (appinfo);
374 g_desktop_app_info_dup (GAppInfo *appinfo)
376 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
377 GDesktopAppInfo *new_info;
379 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
381 new_info->filename = g_strdup (info->filename);
382 new_info->desktop_id = g_strdup (info->desktop_id);
384 new_info->name = g_strdup (info->name);
385 new_info->comment = g_strdup (info->comment);
386 new_info->nodisplay = info->nodisplay;
387 new_info->icon_name = g_strdup (info->icon_name);
388 new_info->icon = g_object_ref (info->icon);
389 new_info->only_show_in = g_strdupv (info->only_show_in);
390 new_info->not_show_in = g_strdupv (info->not_show_in);
391 new_info->try_exec = g_strdup (info->try_exec);
392 new_info->exec = g_strdup (info->exec);
393 new_info->binary = g_strdup (info->binary);
394 new_info->path = g_strdup (info->path);
395 new_info->hidden = info->hidden;
396 new_info->terminal = info->terminal;
397 new_info->startup_notify = info->startup_notify;
399 return G_APP_INFO (new_info);
403 g_desktop_app_info_equal (GAppInfo *appinfo1,
406 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
407 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
409 if (info1->desktop_id == NULL ||
410 info2->desktop_id == NULL)
411 return info1 == info2;
413 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
417 g_desktop_app_info_get_id (GAppInfo *appinfo)
419 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
421 return info->desktop_id;
425 g_desktop_app_info_get_name (GAppInfo *appinfo)
427 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
429 if (info->name == NULL)
435 * g_desktop_app_info_get_is_hidden:
436 * @info: a #GDesktopAppInfo.
438 * A desktop file is hidden if the Hidden key in it is
441 * Returns: %TRUE if hidden, %FALSE otherwise.
444 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
450 g_desktop_app_info_get_description (GAppInfo *appinfo)
452 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
454 return info->comment;
458 g_desktop_app_info_get_executable (GAppInfo *appinfo)
460 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
466 g_desktop_app_info_get_icon (GAppInfo *appinfo)
468 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
474 expand_macro_single (char macro, char *uri)
480 file = g_file_new_for_uri (uri);
481 path = g_file_get_path (file);
482 g_object_unref (file);
488 result = g_shell_quote (uri);
493 result = g_shell_quote (path);
498 result = g_shell_quote (g_path_get_dirname (path));
503 result = g_shell_quote (g_path_get_basename (path));
513 expand_macro (char macro,
515 GDesktopAppInfo *info,
518 GList *uris = *uri_list;
521 g_return_if_fail (exec != NULL);
531 expanded = expand_macro_single (macro, uris->data);
534 g_string_append (exec, expanded);
548 expanded = expand_macro_single (macro, uris->data);
551 g_string_append (exec, expanded);
557 if (uris != NULL && expanded)
558 g_string_append_c (exec, ' ');
566 g_string_append (exec, "--icon ");
567 g_string_append (exec, info->icon_name);
573 g_string_append (exec, info->name);
578 g_string_append (exec, info->filename);
581 case 'm': /* deprecated */
585 g_string_append_c (exec, '%');
593 expand_application_parameters (GDesktopAppInfo *info,
599 GList *uri_list = *uris;
600 const char *p = info->exec;
601 GString *expanded_exec = g_string_new (NULL);
604 if (info->exec == NULL)
606 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
607 _("Desktop file didn't specify Exec field"));
613 if (p[0] == '%' && p[1] != '\0')
615 expand_macro (p[1], expanded_exec, info, uris);
619 g_string_append_c (expanded_exec, *p);
624 /* No file substitutions */
625 if (uri_list == *uris && uri_list != NULL)
627 /* If there is no macro default to %f. This is also what KDE does */
628 g_string_append_c (expanded_exec, ' ');
629 expand_macro ('f', expanded_exec, info, uris);
632 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
633 g_string_free (expanded_exec, TRUE);
638 prepend_terminal_to_vector (int *argc,
645 char **term_argv = NULL;
650 g_return_val_if_fail (argc != NULL, FALSE);
651 g_return_val_if_fail (argv != NULL, FALSE);
659 /* compute size if not given */
662 for (i = 0; the_argv[i] != NULL; i++)
668 term_argv = g_new0 (char *, 3);
670 check = g_find_program_in_path ("gnome-terminal");
673 term_argv[0] = check;
674 /* Note that gnome-terminal takes -x and
675 * as -e in gnome-terminal is broken we use that. */
676 term_argv[1] = g_strdup ("-x");
681 check = g_find_program_in_path ("nxterm");
683 check = g_find_program_in_path ("color-xterm");
685 check = g_find_program_in_path ("rxvt");
687 check = g_find_program_in_path ("xterm");
689 check = g_find_program_in_path ("dtterm");
692 check = g_strdup ("xterm");
693 g_warning ("couldn't find a terminal, falling back to xterm");
695 term_argv[0] = check;
696 term_argv[1] = g_strdup ("-e");
699 real_argc = term_argc + *argc;
700 real_argv = g_new (char *, real_argc + 1);
702 for (i = 0; i < term_argc; i++)
703 real_argv[i] = term_argv[i];
705 for (j = 0; j < *argc; j++, i++)
706 real_argv[i] = (char *)the_argv[j];
714 /* we use g_free here as we sucked all the inner strings
715 * out from it into real_argv */
720 #endif /* G_OS_WIN32 */
723 /* '=' is the new '\0'.
724 * DO NOT CALL unless at least one string ends with '='
727 is_env (const char *a,
732 if (*a == 0 || *b == 0)
745 /* free with g_strfreev */
747 replace_env_var (char **old_environ,
749 const char *new_value)
751 int length, new_length;
752 int index, new_index;
756 /* do two things at once:
757 * - discover the length of the environment ('length')
758 * - find the location (if any) of the env var ('index')
761 for (length = 0; old_environ[length]; length++)
763 /* if we already have it in our environment, replace */
764 if (is_env (old_environ[length], env_var))
769 /* no current env var, no desired env value.
772 if (new_value == NULL && index == -1)
775 /* in all cases now, we will be using a modified environment.
776 * determine its length and allocated it.
779 * new_index = location to insert, if any
780 * new_length = length of the new array
781 * new_environ = the pointer array for the new environment
784 if (new_value == NULL && index >= 0)
786 /* in this case, we will be removing an entry */
787 new_length = length - 1;
790 else if (new_value != NULL && index < 0)
792 /* in this case, we will be adding an entry to the end */
793 new_length = length + 1;
797 /* in this case, we will be replacing the existing entry */
803 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
804 new_environ[new_length] = NULL;
806 /* now we do the copying.
807 * for each entry in the new environment, we decide what to do
811 for (new_i = 0; new_i < new_length; new_i++)
813 if (new_i == new_index)
815 /* insert our new item */
816 new_environ[new_i] = g_strconcat (env_var,
821 /* if we had an old entry, skip it now */
827 /* if this is the old DESKTOP_STARTUP_ID, skip it */
831 /* copy an old item */
832 new_environ[new_i] = g_strdup (old_environ[i]);
837 g_strfreev (old_environ);
843 uri_list_segment_to_files (GList *start,
850 while (start != NULL && start != end)
852 file = g_file_new_for_uri ((char *)start->data);
853 res = g_list_prepend (res, file);
857 return g_list_reverse (res);
860 #ifdef HAVE__NSGETENVIRON
861 #define environ (*_NSGetEnviron())
862 #elif !defined(G_OS_WIN32)
864 /* According to the Single Unix Specification, environ is not in
865 * * any system header, although unistd.h often declares it.
867 extern char **environ;
871 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
873 GAppLaunchContext *launch_context,
876 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
877 gboolean completed = FALSE;
879 GList *launched_files;
886 g_return_val_if_fail (appinfo != NULL, FALSE);
894 if (!expand_application_parameters (info, &uris,
895 &argc, &argv, error))
898 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
900 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
901 _("Unable to find terminal required for application"));
908 launched_files = uri_list_segment_to_files (old_uris, uris);
910 display = g_app_launch_context_get_display (launch_context,
915 if (info->startup_notify)
916 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
920 if (display || sn_id)
924 envp = g_new0 (char *, 1);
926 envp = g_strdupv (environ);
930 envp = replace_env_var (envp,
935 envp = replace_env_var (envp,
936 "DESKTOP_STARTUP_ID",
942 g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
943 g_list_free (launched_files);
946 if (!g_spawn_async (info->path, /* working directory */
949 G_SPAWN_SEARCH_PATH /* flags */,
950 NULL /* child_setup */,
952 NULL /* child_pid */,
957 g_app_launch_context_launch_failed (launch_context, sn_id);
971 while (uris != NULL);
983 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
985 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
988 ((strstr (info->exec, "%u") != NULL) ||
989 (strstr (info->exec, "%U") != NULL));
993 g_desktop_app_info_supports_files (GAppInfo *appinfo)
995 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
998 ((strstr (info->exec, "%f") != NULL) ||
999 (strstr (info->exec, "%F") != NULL));
1003 g_desktop_app_info_launch (GAppInfo *appinfo,
1005 GAppLaunchContext *launch_context,
1015 uri = g_file_get_uri (files->data);
1016 uris = g_list_prepend (uris, uri);
1017 files = files->next;
1020 uris = g_list_reverse (uris);
1022 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1024 g_list_foreach (uris, (GFunc)g_free, NULL);
1030 G_LOCK_DEFINE_STATIC (g_desktop_env);
1031 static gchar *g_desktop_env = NULL;
1034 * g_desktop_app_info_set_desktop_env:
1035 * @desktop_env: a string specifying what desktop this is
1037 * Sets the name of the desktop that the application is running in.
1038 * This is used by g_app_info_should_show() to evaluate the
1039 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1040 * desktop entry fields.
1042 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1043 * Menu specification</ulink> recognizes the following:
1045 * <member>GNOME</member>
1046 * <member>KDE</member>
1047 * <member>ROX</member>
1048 * <member>XFCE</member>
1049 * <member>Old</member>
1052 * Should be called only once; subsequent calls are ignored.
1055 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1057 G_LOCK (g_desktop_env);
1059 g_desktop_env = g_strdup (desktop_env);
1060 G_UNLOCK (g_desktop_env);
1064 g_desktop_app_info_should_show (GAppInfo *appinfo)
1066 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1068 const gchar *desktop_env;
1071 if (info->nodisplay)
1074 G_LOCK (g_desktop_env);
1075 desktop_env = g_desktop_env;
1076 G_UNLOCK (g_desktop_env);
1078 if (info->only_show_in)
1080 if (desktop_env == NULL)
1084 for (i = 0; info->only_show_in[i] != NULL; i++)
1086 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1096 if (info->not_show_in && desktop_env)
1098 for (i = 0; info->not_show_in[i] != NULL; i++)
1100 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1114 ensure_dir (DirType type,
1117 char *path, *display_name;
1120 if (type == APP_DIR)
1121 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1123 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1126 if (g_mkdir_with_parents (path, 0700) == 0)
1130 display_name = g_filename_display_name (path);
1131 if (type == APP_DIR)
1132 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1133 _("Can't create user application configuration folder %s: %s"),
1134 display_name, g_strerror (errsv));
1136 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1137 _("Can't create user MIME configuration folder %s: %s"),
1138 display_name, g_strerror (errsv));
1140 g_free (display_name);
1147 update_mimeapps_list (const char *desktop_id,
1148 const char *content_type,
1149 gboolean add_at_start,
1150 gboolean add_at_end,
1154 char *dirname, *filename;
1156 gboolean load_succeeded, res;
1159 gsize length, data_size;
1163 /* Don't add both at start and end */
1164 g_assert (!(add_at_start && add_at_end));
1166 dirname = ensure_dir (APP_DIR, error);
1170 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1173 key_file = g_key_file_new ();
1174 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1175 if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1177 g_key_file_free (key_file);
1178 key_file = g_key_file_new ();
1181 /* Add to the right place in the list */
1184 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1185 content_type, &length, NULL);
1187 list = g_new (char *, 1 + length + 1);
1191 list[i++] = g_strdup (desktop_id);
1194 for (j = 0; old_list[j] != NULL; j++)
1196 if (strcmp (old_list[j], desktop_id) != 0)
1197 list[i++] = g_strdup (old_list[j]);
1201 list[i++] = g_strdup (desktop_id);
1204 g_strfreev (old_list);
1206 g_key_file_set_string_list (key_file,
1207 ADDED_ASSOCIATIONS_GROUP,
1209 (const char * const *)list, i);
1213 /* Remove from removed associations group (unless remove) */
1216 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1217 content_type, &length, NULL);
1219 list = g_new (char *, 1 + length + 1);
1223 list[i++] = g_strdup (desktop_id);
1226 for (j = 0; old_list[j] != NULL; j++)
1228 if (strcmp (old_list[j], desktop_id) != 0)
1229 list[i++] = g_strdup (old_list[j]);
1234 g_strfreev (old_list);
1236 if (list[0] == NULL)
1237 g_key_file_remove_key (key_file,
1238 REMOVED_ASSOCIATIONS_GROUP,
1242 g_key_file_set_string_list (key_file,
1243 REMOVED_ASSOCIATIONS_GROUP,
1245 (const char * const *)list, i);
1250 data = g_key_file_to_data (key_file, &data_size, error);
1251 g_key_file_free (key_file);
1253 res = g_file_set_contents (filename, data, data_size, error);
1255 mime_info_cache_reload (NULL);
1264 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1265 const char *content_type,
1268 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1270 if (!g_desktop_app_info_ensure_saved (info, error))
1273 return update_mimeapps_list (info->desktop_id, content_type, TRUE, FALSE, FALSE, error);
1277 update_program_done (GPid pid,
1281 /* Did the application exit correctly */
1282 if (WIFEXITED (status) &&
1283 WEXITSTATUS (status) == 0)
1285 /* Here we could clean out any caches in use */
1290 run_update_command (char *command,
1299 GError *error = NULL;
1302 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1304 if (g_spawn_async ("/", argv,
1306 G_SPAWN_SEARCH_PATH |
1307 G_SPAWN_STDOUT_TO_DEV_NULL |
1308 G_SPAWN_STDERR_TO_DEV_NULL |
1309 G_SPAWN_DO_NOT_REAP_CHILD,
1310 NULL, NULL, /* No setup function */
1313 g_child_watch_add (pid, update_program_done, NULL);
1316 /* If we get an error at this point, it's quite likely the user doesn't
1317 * have an installed copy of either 'update-mime-database' or
1318 * 'update-desktop-database'. I don't think we want to popup an error
1319 * dialog at this point, so we just do a g_warning to give the user a
1320 * chance of debugging it.
1322 g_warning ("%s", error->message);
1329 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1330 const char *extension,
1333 char *filename, *basename, *mimetype;
1337 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1340 dirname = ensure_dir (MIMETYPE_DIR, error);
1344 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1345 filename = g_build_filename (dirname, basename, NULL);
1349 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1351 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1356 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1357 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1358 " <mime-type type=\"%s\">\n"
1359 " <comment>%s document</comment>\n"
1360 " <glob pattern=\"*.%s\"/>\n"
1362 "</mime-info>\n", mimetype, extension, extension);
1364 g_file_set_contents (filename, contents, -1, NULL);
1367 run_update_command ("update-mime-database", "mime");
1371 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1381 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1382 const char *content_type,
1385 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1387 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1390 return update_mimeapps_list (info->desktop_id, content_type, FALSE, TRUE, FALSE, error);
1394 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1400 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1401 const char *content_type,
1404 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1406 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1409 return update_mimeapps_list (info->desktop_id, content_type, FALSE, FALSE, TRUE, error);
1413 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1419 char *data, *desktop_id;
1424 if (info->filename != NULL)
1427 /* This is only used for object created with
1428 * g_app_info_create_from_commandline. All other
1429 * object should have a filename
1432 dirname = ensure_dir (APP_DIR, error);
1436 key_file = g_key_file_new ();
1438 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1439 "Encoding", "UTF-8");
1440 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1441 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1442 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1443 G_KEY_FILE_DESKTOP_KEY_TYPE,
1444 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1446 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1447 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1449 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1450 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1452 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1453 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1455 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1456 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1458 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1459 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1461 data = g_key_file_to_data (key_file, &data_size, NULL);
1462 g_key_file_free (key_file);
1464 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1465 filename = g_build_filename (dirname, desktop_id, NULL);
1466 g_free (desktop_id);
1469 fd = g_mkstemp (filename);
1474 display_name = g_filename_display_name (filename);
1475 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1476 _("Can't create user desktop file %s"), display_name);
1477 g_free (display_name);
1483 desktop_id = g_path_get_basename (filename);
1487 res = g_file_set_contents (filename, data, data_size, error);
1490 g_free (desktop_id);
1495 info->filename = filename;
1496 info->desktop_id = desktop_id;
1498 run_update_command ("update-desktop-database", "applications");
1504 * g_app_info_create_from_commandline:
1505 * @commandline: the commandline to use
1506 * @application_name: the application name, or %NULL to use @commandline
1507 * @flags: flags that can specify details of the created #GAppInfo
1508 * @error: a #GError location to store the error occuring, %NULL to ignore.
1510 * Creates a new #GAppInfo from the given information.
1512 * Returns: new #GAppInfo for given command.
1515 g_app_info_create_from_commandline (const char *commandline,
1516 const char *application_name,
1517 GAppInfoCreateFlags flags,
1522 GDesktopAppInfo *info;
1524 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1526 info->filename = NULL;
1527 info->desktop_id = NULL;
1529 info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
1530 info->startup_notify = FALSE;
1531 info->hidden = FALSE;
1532 if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
1533 info->exec = g_strconcat (commandline, " %u", NULL);
1535 info->exec = g_strconcat (commandline, " %f", NULL);
1536 info->nodisplay = TRUE;
1537 info->binary = binary_from_exec (info->exec);
1539 if (application_name)
1540 info->name = g_strdup (application_name);
1543 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1544 split = g_strsplit (commandline, " ", 2);
1545 basename = g_path_get_basename (split[0]);
1547 info->name = basename;
1548 if (info->name == NULL)
1549 info->name = g_strdup ("custom");
1551 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
1553 return G_APP_INFO (info);
1557 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1559 iface->dup = g_desktop_app_info_dup;
1560 iface->equal = g_desktop_app_info_equal;
1561 iface->get_id = g_desktop_app_info_get_id;
1562 iface->get_name = g_desktop_app_info_get_name;
1563 iface->get_description = g_desktop_app_info_get_description;
1564 iface->get_executable = g_desktop_app_info_get_executable;
1565 iface->get_icon = g_desktop_app_info_get_icon;
1566 iface->launch = g_desktop_app_info_launch;
1567 iface->supports_uris = g_desktop_app_info_supports_uris;
1568 iface->supports_files = g_desktop_app_info_supports_files;
1569 iface->launch_uris = g_desktop_app_info_launch_uris;
1570 iface->should_show = g_desktop_app_info_should_show;
1571 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1572 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1573 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1574 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1575 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1579 app_info_in_list (GAppInfo *info,
1582 while (list != NULL)
1584 if (g_app_info_equal (info, list->data))
1593 * g_app_info_get_all_for_type:
1594 * @content_type: the content type to find a #GAppInfo for
1596 * Gets a list of all #GAppInfo s for a given content type.
1598 * Returns: #GList of #GAppInfo s for given @content_type
1599 * or %NULL on error.
1602 g_app_info_get_all_for_type (const char *content_type)
1604 GList *desktop_entries, *l;
1606 GDesktopAppInfo *info;
1608 g_return_val_if_fail (content_type != NULL, NULL);
1610 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1613 for (l = desktop_entries; l != NULL; l = l->next)
1615 char *desktop_entry = l->data;
1617 info = g_desktop_app_info_new (desktop_entry);
1620 if (app_info_in_list (G_APP_INFO (info), infos))
1621 g_object_unref (info);
1623 infos = g_list_prepend (infos, info);
1625 g_free (desktop_entry);
1628 g_list_free (desktop_entries);
1630 return g_list_reverse (infos);
1635 * g_app_info_get_default_for_type:
1636 * @content_type: the content type to find a #GAppInfo for
1637 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1640 * Gets the #GAppInfo that correspond to a given content type.
1642 * Returns: #GAppInfo for given @content_type or %NULL on error.
1645 g_app_info_get_default_for_type (const char *content_type,
1646 gboolean must_support_uris)
1648 GList *desktop_entries, *l;
1651 g_return_val_if_fail (content_type != NULL, NULL);
1653 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1656 for (l = desktop_entries; l != NULL; l = l->next)
1658 char *desktop_entry = l->data;
1660 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1663 if (must_support_uris && !g_app_info_supports_uris (info))
1665 g_object_unref (info);
1673 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1674 g_list_free (desktop_entries);
1680 * g_app_info_get_default_for_uri_scheme:
1681 * @uri_scheme: a string containing a URI scheme.
1683 * Gets the default application for launching applications
1684 * using this URI scheme. A URI scheme is the initial part
1685 * of the URI, up to but not including the ':', e.g. "http",
1688 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
1691 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1693 static gsize lookup = 0;
1695 if (g_once_init_enter (&lookup))
1697 gsize setup_value = 1;
1698 GDesktopAppInfoLookup *lookup_instance;
1699 const char *use_this;
1700 GIOExtensionPoint *ep;
1701 GIOExtension *extension;
1704 use_this = g_getenv ("GIO_USE_URI_ASSOCIATION");
1706 /* Ensure vfs in modules loaded */
1707 _g_io_modules_ensure_loaded ();
1709 ep = g_io_extension_point_lookup (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1711 lookup_instance = NULL;
1714 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
1716 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1719 if (lookup_instance == NULL)
1721 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1723 extension = l->data;
1724 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1725 if (lookup_instance != NULL)
1730 if (lookup_instance != NULL)
1731 setup_value = (gsize)lookup_instance;
1733 g_once_init_leave (&lookup, setup_value);
1739 return g_desktop_app_info_lookup_get_default_for_uri_scheme (G_DESKTOP_APP_INFO_LOOKUP (lookup),
1745 get_apps_from_dir (GHashTable *apps,
1746 const char *dirname,
1750 const char *basename;
1751 char *filename, *subprefix, *desktop_id;
1753 GDesktopAppInfo *appinfo;
1755 dir = g_dir_open (dirname, 0, NULL);
1758 while ((basename = g_dir_read_name (dir)) != NULL)
1760 filename = g_build_filename (dirname, basename, NULL);
1761 if (g_str_has_suffix (basename, ".desktop"))
1763 desktop_id = g_strconcat (prefix, basename, NULL);
1765 /* Use _extended so we catch NULLs too (hidden) */
1766 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1768 appinfo = g_desktop_app_info_new_from_filename (filename);
1770 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1772 g_object_unref (appinfo);
1777 if (appinfo || hidden)
1779 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1783 /* Reuse instead of strdup here */
1784 appinfo->desktop_id = desktop_id;
1789 g_free (desktop_id);
1793 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1795 subprefix = g_strconcat (prefix, basename, "-", NULL);
1796 get_apps_from_dir (apps, filename, subprefix);
1808 * g_app_info_get_all:
1810 * Gets a list of all of the applications currently registered
1813 * For desktop files, this includes applications that have
1814 * <literal>NoDisplay=true</literal> set or are excluded from
1815 * display by means of <literal>OnlyShowIn</literal> or
1816 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1817 * The returned list does not include applications which have
1818 * the <literal>Hidden</literal> key set.
1820 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1823 g_app_info_get_all (void)
1825 const char * const *dirs;
1827 GHashTableIter iter;
1832 dirs = get_applications_search_path ();
1834 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1838 for (i = 0; dirs[i] != NULL; i++)
1839 get_apps_from_dir (apps, dirs[i], "");
1843 g_hash_table_iter_init (&iter, apps);
1844 while (g_hash_table_iter_next (&iter, NULL, &value))
1847 infos = g_list_prepend (infos, value);
1850 g_hash_table_destroy (apps);
1852 return g_list_reverse (infos);
1855 /* Cacheing of mimeinfo.cache and defaults.list files */
1859 GHashTable *mime_info_cache_map;
1860 GHashTable *defaults_list_map;
1861 GHashTable *mimeapps_list_added_map;
1862 GHashTable *mimeapps_list_removed_map;
1863 time_t mime_info_cache_timestamp;
1864 time_t defaults_list_timestamp;
1865 time_t mimeapps_list_timestamp;
1869 GList *dirs; /* mimeinfo.cache and defaults.list */
1870 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1871 time_t last_stat_time;
1872 guint should_ping_mime_monitor : 1;
1875 static MimeInfoCache *mime_info_cache = NULL;
1876 G_LOCK_DEFINE_STATIC (mime_info_cache);
1878 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1879 const char *mime_type,
1880 char **new_desktop_file_ids);
1882 static MimeInfoCache * mime_info_cache_new (void);
1885 destroy_info_cache_value (gpointer key,
1889 g_list_foreach (value, (GFunc)g_free, NULL);
1890 g_list_free (value);
1894 destroy_info_cache_map (GHashTable *info_cache_map)
1896 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1897 g_hash_table_destroy (info_cache_map);
1901 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1902 const char *cache_file,
1908 filename = g_build_filename (dir->path, cache_file, NULL);
1910 if (g_stat (filename, &buf) < 0)
1917 if (buf.st_mtime != *timestamp)
1923 /* Call with lock held */
1925 remove_all (gpointer key,
1934 mime_info_cache_blow_global_cache (void)
1936 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1941 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1945 gchar *filename, **mime_types;
1952 if (dir->mime_info_cache_map != NULL &&
1953 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1954 &dir->mime_info_cache_timestamp))
1957 if (dir->mime_info_cache_map != NULL)
1958 destroy_info_cache_map (dir->mime_info_cache_map);
1960 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1961 (GDestroyNotify) g_free,
1964 key_file = g_key_file_new ();
1966 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1968 if (g_stat (filename, &buf) < 0)
1971 if (dir->mime_info_cache_timestamp > 0)
1972 mime_info_cache->should_ping_mime_monitor = TRUE;
1974 dir->mime_info_cache_timestamp = buf.st_mtime;
1976 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1981 if (load_error != NULL)
1984 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1987 if (load_error != NULL)
1990 for (i = 0; mime_types[i] != NULL; i++)
1992 gchar **desktop_file_ids;
1993 char *unaliased_type;
1994 desktop_file_ids = g_key_file_get_string_list (key_file,
2000 if (desktop_file_ids == NULL)
2003 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2004 mime_info_cache_dir_add_desktop_entries (dir,
2007 g_free (unaliased_type);
2009 g_strfreev (desktop_file_ids);
2012 g_strfreev (mime_types);
2013 g_key_file_free (key_file);
2018 g_key_file_free (key_file);
2020 if (mime_types != NULL)
2021 g_strfreev (mime_types);
2024 g_error_free (load_error);
2028 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2032 gchar *filename, **mime_types;
2033 char *unaliased_type;
2034 char **desktop_file_ids;
2041 if (dir->defaults_list_map != NULL &&
2042 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2043 &dir->defaults_list_timestamp))
2046 if (dir->defaults_list_map != NULL)
2047 g_hash_table_destroy (dir->defaults_list_map);
2048 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2049 g_free, (GDestroyNotify)g_strfreev);
2052 key_file = g_key_file_new ();
2054 filename = g_build_filename (dir->path, "defaults.list", NULL);
2055 if (g_stat (filename, &buf) < 0)
2058 if (dir->defaults_list_timestamp > 0)
2059 mime_info_cache->should_ping_mime_monitor = TRUE;
2061 dir->defaults_list_timestamp = buf.st_mtime;
2063 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2067 if (load_error != NULL)
2070 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2072 if (mime_types != NULL)
2074 for (i = 0; mime_types[i] != NULL; i++)
2076 desktop_file_ids = g_key_file_get_string_list (key_file,
2077 DEFAULT_APPLICATIONS_GROUP,
2081 if (desktop_file_ids == NULL)
2084 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2085 g_hash_table_replace (dir->defaults_list_map,
2090 g_strfreev (mime_types);
2093 g_key_file_free (key_file);
2098 g_key_file_free (key_file);
2100 if (mime_types != NULL)
2101 g_strfreev (mime_types);
2104 g_error_free (load_error);
2108 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2112 gchar *filename, **mime_types;
2113 char *unaliased_type;
2114 char **desktop_file_ids;
2121 if (dir->mimeapps_list_added_map != NULL &&
2122 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2123 &dir->mimeapps_list_timestamp))
2126 if (dir->mimeapps_list_added_map != NULL)
2127 g_hash_table_destroy (dir->mimeapps_list_added_map);
2128 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2129 g_free, (GDestroyNotify)g_strfreev);
2131 if (dir->mimeapps_list_removed_map != NULL)
2132 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2133 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2134 g_free, (GDestroyNotify)g_strfreev);
2136 key_file = g_key_file_new ();
2138 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2139 if (g_stat (filename, &buf) < 0)
2142 if (dir->mimeapps_list_timestamp > 0)
2143 mime_info_cache->should_ping_mime_monitor = TRUE;
2145 dir->mimeapps_list_timestamp = buf.st_mtime;
2147 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2151 if (load_error != NULL)
2154 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2156 if (mime_types != NULL)
2158 for (i = 0; mime_types[i] != NULL; i++)
2160 desktop_file_ids = g_key_file_get_string_list (key_file,
2161 ADDED_ASSOCIATIONS_GROUP,
2165 if (desktop_file_ids == NULL)
2168 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2169 g_hash_table_replace (dir->mimeapps_list_added_map,
2174 g_strfreev (mime_types);
2177 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2179 if (mime_types != NULL)
2181 for (i = 0; mime_types[i] != NULL; i++)
2183 desktop_file_ids = g_key_file_get_string_list (key_file,
2184 REMOVED_ASSOCIATIONS_GROUP,
2188 if (desktop_file_ids == NULL)
2191 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2192 g_hash_table_replace (dir->mimeapps_list_removed_map,
2197 g_strfreev (mime_types);
2200 g_key_file_free (key_file);
2205 g_key_file_free (key_file);
2207 if (mime_types != NULL)
2208 g_strfreev (mime_types);
2211 g_error_free (load_error);
2214 static MimeInfoCacheDir *
2215 mime_info_cache_dir_new (const char *path)
2217 MimeInfoCacheDir *dir;
2219 dir = g_new0 (MimeInfoCacheDir, 1);
2220 dir->path = g_strdup (path);
2226 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2231 if (dir->mime_info_cache_map != NULL)
2233 destroy_info_cache_map (dir->mime_info_cache_map);
2234 dir->mime_info_cache_map = NULL;
2238 if (dir->defaults_list_map != NULL)
2240 g_hash_table_destroy (dir->defaults_list_map);
2241 dir->defaults_list_map = NULL;
2244 if (dir->mimeapps_list_added_map != NULL)
2246 g_hash_table_destroy (dir->mimeapps_list_added_map);
2247 dir->mimeapps_list_added_map = NULL;
2250 if (dir->mimeapps_list_removed_map != NULL)
2252 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2253 dir->mimeapps_list_removed_map = NULL;
2260 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2261 const char *mime_type,
2262 char **new_desktop_file_ids)
2264 GList *desktop_file_ids;
2267 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2270 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2272 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2273 desktop_file_ids = g_list_append (desktop_file_ids,
2274 g_strdup (new_desktop_file_ids[i]));
2277 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2281 mime_info_cache_init_dir_lists (void)
2283 const char * const *dirs;
2286 mime_info_cache = mime_info_cache_new ();
2288 dirs = get_applications_search_path ();
2290 for (i = 0; dirs[i] != NULL; i++)
2292 MimeInfoCacheDir *dir;
2294 dir = mime_info_cache_dir_new (dirs[i]);
2298 mime_info_cache_dir_init (dir);
2299 mime_info_cache_dir_init_defaults_list (dir);
2300 mime_info_cache_dir_init_mimeapps_list (dir);
2302 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2308 mime_info_cache_update_dir_lists (void)
2312 tmp = mime_info_cache->dirs;
2316 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2318 /* No need to do this if we had file monitors... */
2319 mime_info_cache_blow_global_cache ();
2320 mime_info_cache_dir_init (dir);
2321 mime_info_cache_dir_init_defaults_list (dir);
2322 mime_info_cache_dir_init_mimeapps_list (dir);
2329 mime_info_cache_init (void)
2331 G_LOCK (mime_info_cache);
2332 if (mime_info_cache == NULL)
2333 mime_info_cache_init_dir_lists ();
2339 if (now >= mime_info_cache->last_stat_time + 10)
2341 mime_info_cache_update_dir_lists ();
2342 mime_info_cache->last_stat_time = now;
2346 if (mime_info_cache->should_ping_mime_monitor)
2348 /* g_idle_add (emit_mime_changed, NULL); */
2349 mime_info_cache->should_ping_mime_monitor = FALSE;
2352 G_UNLOCK (mime_info_cache);
2355 static MimeInfoCache *
2356 mime_info_cache_new (void)
2358 MimeInfoCache *cache;
2360 cache = g_new0 (MimeInfoCache, 1);
2362 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2363 (GDestroyNotify) g_free,
2364 (GDestroyNotify) g_free);
2369 mime_info_cache_free (MimeInfoCache *cache)
2374 g_list_foreach (cache->dirs,
2375 (GFunc) mime_info_cache_dir_free,
2377 g_list_free (cache->dirs);
2378 g_hash_table_destroy (cache->global_defaults_cache);
2383 * mime_info_cache_reload:
2384 * @dir: directory path which needs reloading.
2386 * Reload the mime information for the @dir.
2389 mime_info_cache_reload (const char *dir)
2391 /* FIXME: just reload the dir that needs reloading,
2392 * don't blow the whole cache
2394 if (mime_info_cache != NULL)
2396 G_LOCK (mime_info_cache);
2397 mime_info_cache_free (mime_info_cache);
2398 mime_info_cache = NULL;
2399 G_UNLOCK (mime_info_cache);
2404 append_desktop_entry (GList *list,
2405 const char *desktop_entry,
2406 GList *removed_entries)
2408 /* Add if not already in list, and valid */
2409 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2410 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2411 list = g_list_prepend (list, g_strdup (desktop_entry));
2417 * get_all_desktop_entries_for_mime_type:
2418 * @mime_type: a mime type.
2420 * Returns all the desktop ids for @mime_type. The desktop files
2421 * are listed in an order so that default applications are listed before
2422 * non-default ones, and handlers for inherited mimetypes are listed
2423 * after the base ones.
2425 * Return value: a #GList containing the desktop ids which claim
2426 * to handle @mime_type.
2429 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2431 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2432 MimeInfoCacheDir *dir;
2435 char **default_entries;
2436 char **removed_associations;
2439 mime_info_cache_init ();
2441 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2442 G_LOCK (mime_info_cache);
2444 removed_entries = NULL;
2445 desktop_entries = NULL;
2446 for (i = 0; mime_types[i] != NULL; i++)
2448 mime_type = mime_types[i];
2450 /* Go through all apps listed as defaults */
2451 for (dir_list = mime_info_cache->dirs;
2453 dir_list = dir_list->next)
2455 dir = dir_list->data;
2457 /* First added associations from mimeapps.list */
2458 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
2459 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2460 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2462 /* Then removed assiciations from mimeapps.list */
2463 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
2464 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2465 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2467 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
2468 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2469 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2470 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2473 /* Go through all entries that support the mimetype */
2474 for (dir_list = mime_info_cache->dirs;
2476 dir_list = dir_list->next)
2478 dir = dir_list->data;
2480 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2481 for (tmp = list; tmp != NULL; tmp = tmp->next)
2482 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2486 G_UNLOCK (mime_info_cache);
2488 g_strfreev (mime_types);
2490 g_list_free (removed_entries);
2492 desktop_entries = g_list_reverse (desktop_entries);
2494 return desktop_entries;
2497 /* GDesktopAppInfoLookup interface: */
2499 static void g_desktop_app_info_lookup_base_init (gpointer g_class);
2500 static void g_desktop_app_info_lookup_class_init (gpointer g_class,
2501 gpointer class_data);
2504 g_desktop_app_info_lookup_get_type (void)
2506 static GType desktop_app_info_lookup_type = 0;
2508 if (! desktop_app_info_lookup_type)
2510 static const GTypeInfo desktop_app_info_lookup_info =
2512 sizeof (GDesktopAppInfoLookupIface), /* class_size */
2513 g_desktop_app_info_lookup_base_init, /* base_init */
2514 NULL, /* base_finalize */
2515 g_desktop_app_info_lookup_class_init,
2516 NULL, /* class_finalize */
2517 NULL, /* class_data */
2519 0, /* n_preallocs */
2523 desktop_app_info_lookup_type =
2524 g_type_register_static (G_TYPE_INTERFACE, I_("GDesktopAppInfoLookup"),
2525 &desktop_app_info_lookup_info, 0);
2527 g_type_interface_add_prerequisite (desktop_app_info_lookup_type, G_TYPE_OBJECT);
2530 return desktop_app_info_lookup_type;
2534 g_desktop_app_info_lookup_class_init (gpointer g_class,
2535 gpointer class_data)
2540 g_desktop_app_info_lookup_base_init (gpointer g_class)
2545 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
2546 * @lookup: a #GDesktopAppInfoLookup
2547 * @uri_scheme: a string containing a URI scheme.
2549 * Gets the default application for launching applications
2550 * using this URI scheme for a particular GDesktopAppInfoLookup
2553 * The GDesktopAppInfoLookup interface and this function is used
2554 * to implement g_app_info_get_default_for_uri_scheme() backends
2555 * in a GIO module. There is no reason for applications to use it
2556 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
2558 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
2561 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
2562 const char *uri_scheme)
2564 GDesktopAppInfoLookupIface *iface;
2566 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
2568 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
2570 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
2573 #define __G_DESKTOP_APP_INFO_C__
2574 #include "gioaliasdef.c"