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);
241 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
242 info->filename = NULL;
244 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
245 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
246 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
247 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
248 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);
249 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);
250 info->try_exec = try_exec;
251 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
252 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
253 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
254 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
255 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
260 if (g_path_is_absolute (info->icon_name))
264 file = g_file_new_for_path (info->icon_name);
265 info->icon = g_file_icon_new (file);
266 g_object_unref (file);
269 info->icon = g_themed_icon_new (info->icon_name);
273 info->binary = binary_from_exec (info->exec);
279 * g_desktop_app_info_new_from_filename:
280 * @filename: a string containing a file name.
282 * Creates a new #GDesktopAppInfo.
284 * Returns: a new #GDesktopAppInfo or %NULL on error.
287 g_desktop_app_info_new_from_filename (const char *filename)
290 GDesktopAppInfo *info = NULL;
292 key_file = g_key_file_new ();
294 if (g_key_file_load_from_file (key_file,
299 info = g_desktop_app_info_new_from_keyfile (key_file);
301 info->filename = g_strdup (filename);
304 g_key_file_free (key_file);
310 * g_desktop_app_info_new:
311 * @desktop_id: the desktop file id
313 * Creates a new #GDesktopAppInfo.
315 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
318 g_desktop_app_info_new (const char *desktop_id)
320 GDesktopAppInfo *appinfo;
321 const char * const *dirs;
325 dirs = get_applications_search_path ();
327 basename = g_strdup (desktop_id);
329 for (i = 0; dirs[i] != NULL; i++)
334 filename = g_build_filename (dirs[i], desktop_id, NULL);
335 appinfo = g_desktop_app_info_new_from_filename (filename);
341 while ((p = strchr (p, '-')) != NULL)
345 filename = g_build_filename (dirs[i], basename, NULL);
346 appinfo = g_desktop_app_info_new_from_filename (filename);
361 appinfo->desktop_id = g_strdup (desktop_id);
363 if (g_desktop_app_info_get_is_hidden (appinfo))
365 g_object_unref (appinfo);
373 g_desktop_app_info_dup (GAppInfo *appinfo)
375 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
376 GDesktopAppInfo *new_info;
378 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
380 new_info->filename = g_strdup (info->filename);
381 new_info->desktop_id = g_strdup (info->desktop_id);
383 new_info->name = g_strdup (info->name);
384 new_info->comment = g_strdup (info->comment);
385 new_info->nodisplay = info->nodisplay;
386 new_info->icon_name = g_strdup (info->icon_name);
387 new_info->icon = g_object_ref (info->icon);
388 new_info->only_show_in = g_strdupv (info->only_show_in);
389 new_info->not_show_in = g_strdupv (info->not_show_in);
390 new_info->try_exec = g_strdup (info->try_exec);
391 new_info->exec = g_strdup (info->exec);
392 new_info->binary = g_strdup (info->binary);
393 new_info->path = g_strdup (info->path);
394 new_info->hidden = info->hidden;
395 new_info->terminal = info->terminal;
396 new_info->startup_notify = info->startup_notify;
398 return G_APP_INFO (new_info);
402 g_desktop_app_info_equal (GAppInfo *appinfo1,
405 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
406 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
408 if (info1->desktop_id == NULL ||
409 info2->desktop_id == NULL)
410 return info1 == info2;
412 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
416 g_desktop_app_info_get_id (GAppInfo *appinfo)
418 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
420 return info->desktop_id;
424 g_desktop_app_info_get_name (GAppInfo *appinfo)
426 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
428 if (info->name == NULL)
434 * g_desktop_app_info_get_is_hidden:
435 * @info: a #GDesktopAppInfo.
437 * A desktop file is hidden if the Hidden key in it is
440 * Returns: %TRUE if hidden, %FALSE otherwise.
443 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
449 g_desktop_app_info_get_description (GAppInfo *appinfo)
451 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
453 return info->comment;
457 g_desktop_app_info_get_executable (GAppInfo *appinfo)
459 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
465 g_desktop_app_info_get_icon (GAppInfo *appinfo)
467 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
473 expand_macro_single (char macro, char *uri)
479 file = g_file_new_for_uri (uri);
480 path = g_file_get_path (file);
481 g_object_unref (file);
487 result = g_shell_quote (uri);
492 result = g_shell_quote (path);
497 result = g_shell_quote (g_path_get_dirname (path));
502 result = g_shell_quote (g_path_get_basename (path));
512 expand_macro (char macro,
514 GDesktopAppInfo *info,
517 GList *uris = *uri_list;
520 g_return_if_fail (exec != NULL);
530 expanded = expand_macro_single (macro, uris->data);
533 g_string_append (exec, expanded);
547 expanded = expand_macro_single (macro, uris->data);
550 g_string_append (exec, expanded);
556 if (uris != NULL && expanded)
557 g_string_append_c (exec, ' ');
565 g_string_append (exec, "--icon ");
566 g_string_append (exec, info->icon_name);
572 g_string_append (exec, info->name);
577 g_string_append (exec, info->filename);
580 case 'm': /* deprecated */
584 g_string_append_c (exec, '%');
592 expand_application_parameters (GDesktopAppInfo *info,
598 GList *uri_list = *uris;
599 const char *p = info->exec;
600 GString *expanded_exec = g_string_new (NULL);
603 if (info->exec == NULL)
605 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
606 _("Desktop file didn't specify Exec field"));
612 if (p[0] == '%' && p[1] != '\0')
614 expand_macro (p[1], expanded_exec, info, uris);
618 g_string_append_c (expanded_exec, *p);
623 /* No file substitutions */
624 if (uri_list == *uris && uri_list != NULL)
626 /* If there is no macro default to %f. This is also what KDE does */
627 g_string_append_c (expanded_exec, ' ');
628 expand_macro ('f', expanded_exec, info, uris);
631 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
632 g_string_free (expanded_exec, TRUE);
637 prepend_terminal_to_vector (int *argc,
644 char **term_argv = NULL;
649 g_return_val_if_fail (argc != NULL, FALSE);
650 g_return_val_if_fail (argv != NULL, FALSE);
658 /* compute size if not given */
661 for (i = 0; the_argv[i] != NULL; i++)
667 term_argv = g_new0 (char *, 3);
669 check = g_find_program_in_path ("gnome-terminal");
672 term_argv[0] = check;
673 /* Note that gnome-terminal takes -x and
674 * as -e in gnome-terminal is broken we use that. */
675 term_argv[1] = g_strdup ("-x");
680 check = g_find_program_in_path ("nxterm");
682 check = g_find_program_in_path ("color-xterm");
684 check = g_find_program_in_path ("rxvt");
686 check = g_find_program_in_path ("xterm");
688 check = g_find_program_in_path ("dtterm");
691 check = g_strdup ("xterm");
692 g_warning ("couldn't find a terminal, falling back to xterm");
694 term_argv[0] = check;
695 term_argv[1] = g_strdup ("-e");
698 real_argc = term_argc + *argc;
699 real_argv = g_new (char *, real_argc + 1);
701 for (i = 0; i < term_argc; i++)
702 real_argv[i] = term_argv[i];
704 for (j = 0; j < *argc; j++, i++)
705 real_argv[i] = (char *)the_argv[j];
713 /* we use g_free here as we sucked all the inner strings
714 * out from it into real_argv */
719 #endif /* G_OS_WIN32 */
722 /* '=' is the new '\0'.
723 * DO NOT CALL unless at least one string ends with '='
726 is_env (const char *a,
731 if (*a == 0 || *b == 0)
744 /* free with g_strfreev */
746 replace_env_var (char **old_environ,
748 const char *new_value)
750 int length, new_length;
751 int index, new_index;
755 /* do two things at once:
756 * - discover the length of the environment ('length')
757 * - find the location (if any) of the env var ('index')
760 for (length = 0; old_environ[length]; length++)
762 /* if we already have it in our environment, replace */
763 if (is_env (old_environ[length], env_var))
768 /* no current env var, no desired env value.
771 if (new_value == NULL && index == -1)
774 /* in all cases now, we will be using a modified environment.
775 * determine its length and allocated it.
778 * new_index = location to insert, if any
779 * new_length = length of the new array
780 * new_environ = the pointer array for the new environment
783 if (new_value == NULL && index >= 0)
785 /* in this case, we will be removing an entry */
786 new_length = length - 1;
789 else if (new_value != NULL && index < 0)
791 /* in this case, we will be adding an entry to the end */
792 new_length = length + 1;
796 /* in this case, we will be replacing the existing entry */
802 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
803 new_environ[new_length] = NULL;
805 /* now we do the copying.
806 * for each entry in the new environment, we decide what to do
810 for (new_i = 0; new_i < new_length; new_i++)
812 if (new_i == new_index)
814 /* insert our new item */
815 new_environ[new_i] = g_strconcat (env_var,
820 /* if we had an old entry, skip it now */
826 /* if this is the old DESKTOP_STARTUP_ID, skip it */
830 /* copy an old item */
831 new_environ[new_i] = g_strdup (old_environ[i]);
836 g_strfreev (old_environ);
842 uri_list_segment_to_files (GList *start,
849 while (start != NULL && start != end)
851 file = g_file_new_for_uri ((char *)start->data);
852 res = g_list_prepend (res, file);
856 return g_list_reverse (res);
859 #ifdef HAVE__NSGETENVIRON
860 #define environ (*_NSGetEnviron())
861 #elif !defined(G_OS_WIN32)
863 /* According to the Single Unix Specification, environ is not in
864 * * any system header, although unistd.h often declares it.
866 extern char **environ;
870 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
872 GAppLaunchContext *launch_context,
875 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
876 gboolean completed = FALSE;
878 GList *launched_files;
885 g_return_val_if_fail (appinfo != NULL, FALSE);
893 if (!expand_application_parameters (info, &uris,
894 &argc, &argv, error))
897 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
899 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
900 _("Unable to find terminal required for application"));
907 launched_files = uri_list_segment_to_files (old_uris, uris);
909 display = g_app_launch_context_get_display (launch_context,
914 if (info->startup_notify)
915 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
919 if (display || sn_id)
923 envp = g_new0 (char *, 1);
925 envp = g_strdupv (environ);
929 envp = replace_env_var (envp,
934 envp = replace_env_var (envp,
935 "DESKTOP_STARTUP_ID",
941 g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
942 g_list_free (launched_files);
945 if (!g_spawn_async (info->path, /* working directory */
948 G_SPAWN_SEARCH_PATH /* flags */,
949 NULL /* child_setup */,
951 NULL /* child_pid */,
956 g_app_launch_context_launch_failed (launch_context, sn_id);
970 while (uris != NULL);
982 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
984 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
987 ((strstr (info->exec, "%u") != NULL) ||
988 (strstr (info->exec, "%U") != NULL));
992 g_desktop_app_info_supports_files (GAppInfo *appinfo)
994 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
997 ((strstr (info->exec, "%f") != NULL) ||
998 (strstr (info->exec, "%F") != NULL));
1002 g_desktop_app_info_launch (GAppInfo *appinfo,
1004 GAppLaunchContext *launch_context,
1014 uri = g_file_get_uri (files->data);
1015 uris = g_list_prepend (uris, uri);
1016 files = files->next;
1019 uris = g_list_reverse (uris);
1021 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1023 g_list_foreach (uris, (GFunc)g_free, NULL);
1029 G_LOCK_DEFINE_STATIC (g_desktop_env);
1030 static gchar *g_desktop_env = NULL;
1033 * g_desktop_app_info_set_desktop_env:
1034 * @desktop_env: a string specifying what desktop this is
1036 * Sets the name of the desktop that the application is running in.
1037 * This is used by g_app_info_should_show() to evaluate the
1038 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1039 * desktop entry fields.
1041 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1042 * Menu specification</ulink> recognizes the following:
1044 * <member>GNOME</member>
1045 * <member>KDE</member>
1046 * <member>ROX</member>
1047 * <member>XFCE</member>
1048 * <member>Old</member>
1051 * Should be called only once; subsequent calls are ignored.
1054 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1056 G_LOCK (g_desktop_env);
1058 g_desktop_env = g_strdup (desktop_env);
1059 G_UNLOCK (g_desktop_env);
1063 g_desktop_app_info_should_show (GAppInfo *appinfo)
1065 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1067 const gchar *desktop_env;
1070 if (info->nodisplay)
1073 G_LOCK (g_desktop_env);
1074 desktop_env = g_desktop_env;
1075 G_UNLOCK (g_desktop_env);
1077 if (info->only_show_in)
1079 if (desktop_env == NULL)
1083 for (i = 0; info->only_show_in[i] != NULL; i++)
1085 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1095 if (info->not_show_in && desktop_env)
1097 for (i = 0; info->not_show_in[i] != NULL; i++)
1099 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1113 ensure_dir (DirType type,
1116 char *path, *display_name;
1119 if (type == APP_DIR)
1120 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1122 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1125 if (g_mkdir_with_parents (path, 0700) == 0)
1129 display_name = g_filename_display_name (path);
1130 if (type == APP_DIR)
1131 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1132 _("Can't create user application configuration folder %s: %s"),
1133 display_name, g_strerror (errsv));
1135 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1136 _("Can't create user MIME configuration folder %s: %s"),
1137 display_name, g_strerror (errsv));
1139 g_free (display_name);
1146 update_mimeapps_list (const char *desktop_id,
1147 const char *content_type,
1148 gboolean add_at_start,
1149 gboolean add_at_end,
1153 char *dirname, *filename;
1155 gboolean load_succeeded, res;
1158 gsize length, data_size;
1162 /* Don't add both at start and end */
1163 g_assert (!(add_at_start && add_at_end));
1165 dirname = ensure_dir (APP_DIR, error);
1169 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1172 key_file = g_key_file_new ();
1173 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1174 if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1176 g_key_file_free (key_file);
1177 key_file = g_key_file_new ();
1180 /* Add to the right place in the list */
1183 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1184 content_type, &length, NULL);
1186 list = g_new (char *, 1 + length + 1);
1190 list[i++] = g_strdup (desktop_id);
1193 for (j = 0; old_list[j] != NULL; j++)
1195 if (strcmp (old_list[j], desktop_id) != 0)
1196 list[i++] = g_strdup (old_list[j]);
1200 list[i++] = g_strdup (desktop_id);
1203 g_strfreev (old_list);
1205 g_key_file_set_string_list (key_file,
1206 ADDED_ASSOCIATIONS_GROUP,
1208 (const char * const *)list, i);
1212 /* Remove from removed associations group (unless remove) */
1215 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1216 content_type, &length, NULL);
1218 list = g_new (char *, 1 + length + 1);
1222 list[i++] = g_strdup (desktop_id);
1225 for (j = 0; old_list[j] != NULL; j++)
1227 if (strcmp (old_list[j], desktop_id) != 0)
1228 list[i++] = g_strdup (old_list[j]);
1233 g_strfreev (old_list);
1235 if (list[0] == NULL)
1236 g_key_file_remove_key (key_file,
1237 REMOVED_ASSOCIATIONS_GROUP,
1241 g_key_file_set_string_list (key_file,
1242 REMOVED_ASSOCIATIONS_GROUP,
1244 (const char * const *)list, i);
1249 data = g_key_file_to_data (key_file, &data_size, error);
1250 g_key_file_free (key_file);
1252 res = g_file_set_contents (filename, data, data_size, error);
1254 mime_info_cache_reload (NULL);
1263 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1264 const char *content_type,
1267 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1269 if (!g_desktop_app_info_ensure_saved (info, error))
1272 return update_mimeapps_list (info->desktop_id, content_type, TRUE, FALSE, FALSE, error);
1276 update_program_done (GPid pid,
1280 /* Did the application exit correctly */
1281 if (WIFEXITED (status) &&
1282 WEXITSTATUS (status) == 0)
1284 /* Here we could clean out any caches in use */
1289 run_update_command (char *command,
1298 GError *error = NULL;
1301 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1303 if (g_spawn_async ("/", argv,
1305 G_SPAWN_SEARCH_PATH |
1306 G_SPAWN_STDOUT_TO_DEV_NULL |
1307 G_SPAWN_STDERR_TO_DEV_NULL |
1308 G_SPAWN_DO_NOT_REAP_CHILD,
1309 NULL, NULL, /* No setup function */
1312 g_child_watch_add (pid, update_program_done, NULL);
1315 /* If we get an error at this point, it's quite likely the user doesn't
1316 * have an installed copy of either 'update-mime-database' or
1317 * 'update-desktop-database'. I don't think we want to popup an error
1318 * dialog at this point, so we just do a g_warning to give the user a
1319 * chance of debugging it.
1321 g_warning ("%s", error->message);
1328 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1329 const char *extension,
1332 char *filename, *basename, *mimetype;
1336 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1339 dirname = ensure_dir (MIMETYPE_DIR, error);
1343 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1344 filename = g_build_filename (dirname, basename, NULL);
1348 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1350 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1355 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1356 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1357 " <mime-type type=\"%s\">\n"
1358 " <comment>%s document</comment>\n"
1359 " <glob pattern=\"*.%s\"/>\n"
1361 "</mime-info>\n", mimetype, extension, extension);
1363 g_file_set_contents (filename, contents, -1, NULL);
1366 run_update_command ("update-mime-database", "mime");
1370 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1380 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1381 const char *content_type,
1384 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1386 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1389 return update_mimeapps_list (info->desktop_id, content_type, FALSE, TRUE, FALSE, error);
1393 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1399 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1400 const char *content_type,
1403 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1405 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1408 return update_mimeapps_list (info->desktop_id, content_type, FALSE, FALSE, TRUE, error);
1412 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1418 char *data, *desktop_id;
1423 if (info->filename != NULL)
1426 /* This is only used for object created with
1427 * g_app_info_create_from_commandline. All other
1428 * object should have a filename
1431 dirname = ensure_dir (APP_DIR, error);
1435 key_file = g_key_file_new ();
1437 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1438 "Encoding", "UTF-8");
1439 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1440 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1441 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1442 G_KEY_FILE_DESKTOP_KEY_TYPE,
1443 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1445 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1446 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1448 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1449 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1451 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1452 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1454 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1455 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1457 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1458 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1460 data = g_key_file_to_data (key_file, &data_size, NULL);
1461 g_key_file_free (key_file);
1463 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1464 filename = g_build_filename (dirname, desktop_id, NULL);
1465 g_free (desktop_id);
1468 fd = g_mkstemp (filename);
1473 display_name = g_filename_display_name (filename);
1474 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1475 _("Can't create user desktop file %s"), display_name);
1476 g_free (display_name);
1482 desktop_id = g_path_get_basename (filename);
1486 res = g_file_set_contents (filename, data, data_size, error);
1489 g_free (desktop_id);
1494 info->filename = filename;
1495 info->desktop_id = desktop_id;
1497 run_update_command ("update-desktop-database", "applications");
1503 * g_app_info_create_from_commandline:
1504 * @commandline: the commandline to use
1505 * @application_name: the application name, or %NULL to use @commandline
1506 * @flags: flags that can specify details of the created #GAppInfo
1507 * @error: a #GError location to store the error occuring, %NULL to ignore.
1509 * Creates a new #GAppInfo from the given information.
1511 * Returns: new #GAppInfo for given command.
1514 g_app_info_create_from_commandline (const char *commandline,
1515 const char *application_name,
1516 GAppInfoCreateFlags flags,
1521 GDesktopAppInfo *info;
1523 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1525 info->filename = NULL;
1526 info->desktop_id = NULL;
1528 info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
1529 info->startup_notify = FALSE;
1530 info->hidden = FALSE;
1531 if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
1532 info->exec = g_strconcat (commandline, " %u", NULL);
1534 info->exec = g_strconcat (commandline, " %f", NULL);
1535 info->nodisplay = TRUE;
1536 info->binary = binary_from_exec (info->exec);
1538 if (application_name)
1539 info->name = g_strdup (application_name);
1542 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1543 split = g_strsplit (commandline, " ", 2);
1544 basename = g_path_get_basename (split[0]);
1546 info->name = basename;
1547 if (info->name == NULL)
1548 info->name = g_strdup ("custom");
1550 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
1552 return G_APP_INFO (info);
1556 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1558 iface->dup = g_desktop_app_info_dup;
1559 iface->equal = g_desktop_app_info_equal;
1560 iface->get_id = g_desktop_app_info_get_id;
1561 iface->get_name = g_desktop_app_info_get_name;
1562 iface->get_description = g_desktop_app_info_get_description;
1563 iface->get_executable = g_desktop_app_info_get_executable;
1564 iface->get_icon = g_desktop_app_info_get_icon;
1565 iface->launch = g_desktop_app_info_launch;
1566 iface->supports_uris = g_desktop_app_info_supports_uris;
1567 iface->supports_files = g_desktop_app_info_supports_files;
1568 iface->launch_uris = g_desktop_app_info_launch_uris;
1569 iface->should_show = g_desktop_app_info_should_show;
1570 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1571 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1572 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1573 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1574 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1578 app_info_in_list (GAppInfo *info,
1581 while (list != NULL)
1583 if (g_app_info_equal (info, list->data))
1592 * g_app_info_get_all_for_type:
1593 * @content_type: the content type to find a #GAppInfo for
1595 * Gets a list of all #GAppInfo s for a given content type.
1597 * Returns: #GList of #GAppInfo s for given @content_type
1598 * or %NULL on error.
1601 g_app_info_get_all_for_type (const char *content_type)
1603 GList *desktop_entries, *l;
1605 GDesktopAppInfo *info;
1607 g_return_val_if_fail (content_type != NULL, NULL);
1609 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1612 for (l = desktop_entries; l != NULL; l = l->next)
1614 char *desktop_entry = l->data;
1616 info = g_desktop_app_info_new (desktop_entry);
1619 if (app_info_in_list (G_APP_INFO (info), infos))
1620 g_object_unref (info);
1622 infos = g_list_prepend (infos, info);
1624 g_free (desktop_entry);
1627 g_list_free (desktop_entries);
1629 return g_list_reverse (infos);
1634 * g_app_info_get_default_for_type:
1635 * @content_type: the content type to find a #GAppInfo for
1636 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1639 * Gets the #GAppInfo that correspond to a given content type.
1641 * Returns: #GAppInfo for given @content_type or %NULL on error.
1644 g_app_info_get_default_for_type (const char *content_type,
1645 gboolean must_support_uris)
1647 GList *desktop_entries, *l;
1650 g_return_val_if_fail (content_type != NULL, NULL);
1652 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1655 for (l = desktop_entries; l != NULL; l = l->next)
1657 char *desktop_entry = l->data;
1659 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1662 if (must_support_uris && !g_app_info_supports_uris (info))
1664 g_object_unref (info);
1672 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1673 g_list_free (desktop_entries);
1679 * g_app_info_get_default_for_uri_scheme:
1680 * @uri_scheme: a string containing a URI scheme.
1682 * Gets the default application for launching applications
1683 * using this URI scheme. A URI scheme is the initial part
1684 * of the URI, up to but not including the ':', e.g. "http",
1687 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
1690 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1692 static gsize lookup = 0;
1694 if (g_once_init_enter (&lookup))
1696 gsize setup_value = 1;
1697 GDesktopAppInfoLookup *lookup_instance;
1698 const char *use_this;
1699 GIOExtensionPoint *ep;
1700 GIOExtension *extension;
1703 use_this = g_getenv ("GIO_USE_URI_ASSOCIATION");
1705 /* Ensure vfs in modules loaded */
1706 _g_io_modules_ensure_loaded ();
1708 ep = g_io_extension_point_lookup (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1710 lookup_instance = NULL;
1713 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
1715 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1718 if (lookup_instance == NULL)
1720 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1722 extension = l->data;
1723 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1724 if (lookup_instance != NULL)
1729 if (lookup_instance != NULL)
1730 setup_value = (gsize)lookup_instance;
1732 g_once_init_leave (&lookup, setup_value);
1738 return g_desktop_app_info_lookup_get_default_for_uri_scheme (G_DESKTOP_APP_INFO_LOOKUP (lookup),
1744 get_apps_from_dir (GHashTable *apps,
1745 const char *dirname,
1749 const char *basename;
1750 char *filename, *subprefix, *desktop_id;
1752 GDesktopAppInfo *appinfo;
1754 dir = g_dir_open (dirname, 0, NULL);
1757 while ((basename = g_dir_read_name (dir)) != NULL)
1759 filename = g_build_filename (dirname, basename, NULL);
1760 if (g_str_has_suffix (basename, ".desktop"))
1762 desktop_id = g_strconcat (prefix, basename, NULL);
1764 /* Use _extended so we catch NULLs too (hidden) */
1765 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1767 appinfo = g_desktop_app_info_new_from_filename (filename);
1769 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1771 g_object_unref (appinfo);
1776 if (appinfo || hidden)
1778 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1782 /* Reuse instead of strdup here */
1783 appinfo->desktop_id = desktop_id;
1788 g_free (desktop_id);
1792 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1794 subprefix = g_strconcat (prefix, basename, "-", NULL);
1795 get_apps_from_dir (apps, filename, subprefix);
1807 * g_app_info_get_all:
1809 * Gets a list of all of the applications currently registered
1812 * For desktop files, this includes applications that have
1813 * <literal>NoDisplay=true</literal> set or are excluded from
1814 * display by means of <literal>OnlyShowIn</literal> or
1815 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1816 * The returned list does not include applications which have
1817 * the <literal>Hidden</literal> key set.
1819 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1822 g_app_info_get_all (void)
1824 const char * const *dirs;
1826 GHashTableIter iter;
1831 dirs = get_applications_search_path ();
1833 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1837 for (i = 0; dirs[i] != NULL; i++)
1838 get_apps_from_dir (apps, dirs[i], "");
1842 g_hash_table_iter_init (&iter, apps);
1843 while (g_hash_table_iter_next (&iter, NULL, &value))
1846 infos = g_list_prepend (infos, value);
1849 g_hash_table_destroy (apps);
1851 return g_list_reverse (infos);
1854 /* Cacheing of mimeinfo.cache and defaults.list files */
1858 GHashTable *mime_info_cache_map;
1859 GHashTable *defaults_list_map;
1860 GHashTable *mimeapps_list_added_map;
1861 GHashTable *mimeapps_list_removed_map;
1862 time_t mime_info_cache_timestamp;
1863 time_t defaults_list_timestamp;
1864 time_t mimeapps_list_timestamp;
1868 GList *dirs; /* mimeinfo.cache and defaults.list */
1869 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1870 time_t last_stat_time;
1871 guint should_ping_mime_monitor : 1;
1874 static MimeInfoCache *mime_info_cache = NULL;
1875 G_LOCK_DEFINE_STATIC (mime_info_cache);
1877 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1878 const char *mime_type,
1879 char **new_desktop_file_ids);
1881 static MimeInfoCache * mime_info_cache_new (void);
1884 destroy_info_cache_value (gpointer key,
1888 g_list_foreach (value, (GFunc)g_free, NULL);
1889 g_list_free (value);
1893 destroy_info_cache_map (GHashTable *info_cache_map)
1895 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1896 g_hash_table_destroy (info_cache_map);
1900 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1901 const char *cache_file,
1907 filename = g_build_filename (dir->path, cache_file, NULL);
1909 if (g_stat (filename, &buf) < 0)
1916 if (buf.st_mtime != *timestamp)
1922 /* Call with lock held */
1924 remove_all (gpointer key,
1933 mime_info_cache_blow_global_cache (void)
1935 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1940 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1944 gchar *filename, **mime_types;
1951 if (dir->mime_info_cache_map != NULL &&
1952 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1953 &dir->mime_info_cache_timestamp))
1956 if (dir->mime_info_cache_map != NULL)
1957 destroy_info_cache_map (dir->mime_info_cache_map);
1959 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1960 (GDestroyNotify) g_free,
1963 key_file = g_key_file_new ();
1965 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1967 if (g_stat (filename, &buf) < 0)
1970 if (dir->mime_info_cache_timestamp > 0)
1971 mime_info_cache->should_ping_mime_monitor = TRUE;
1973 dir->mime_info_cache_timestamp = buf.st_mtime;
1975 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1980 if (load_error != NULL)
1983 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1986 if (load_error != NULL)
1989 for (i = 0; mime_types[i] != NULL; i++)
1991 gchar **desktop_file_ids;
1992 char *unaliased_type;
1993 desktop_file_ids = g_key_file_get_string_list (key_file,
1999 if (desktop_file_ids == NULL)
2002 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2003 mime_info_cache_dir_add_desktop_entries (dir,
2006 g_free (unaliased_type);
2008 g_strfreev (desktop_file_ids);
2011 g_strfreev (mime_types);
2012 g_key_file_free (key_file);
2017 g_key_file_free (key_file);
2019 if (mime_types != NULL)
2020 g_strfreev (mime_types);
2023 g_error_free (load_error);
2027 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2031 gchar *filename, **mime_types;
2032 char *unaliased_type;
2033 char **desktop_file_ids;
2040 if (dir->defaults_list_map != NULL &&
2041 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2042 &dir->defaults_list_timestamp))
2045 if (dir->defaults_list_map != NULL)
2046 g_hash_table_destroy (dir->defaults_list_map);
2047 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2048 g_free, (GDestroyNotify)g_strfreev);
2051 key_file = g_key_file_new ();
2053 filename = g_build_filename (dir->path, "defaults.list", NULL);
2054 if (g_stat (filename, &buf) < 0)
2057 if (dir->defaults_list_timestamp > 0)
2058 mime_info_cache->should_ping_mime_monitor = TRUE;
2060 dir->defaults_list_timestamp = buf.st_mtime;
2062 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2066 if (load_error != NULL)
2069 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2071 if (mime_types != NULL)
2073 for (i = 0; mime_types[i] != NULL; i++)
2075 desktop_file_ids = g_key_file_get_string_list (key_file,
2076 DEFAULT_APPLICATIONS_GROUP,
2080 if (desktop_file_ids == NULL)
2083 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2084 g_hash_table_replace (dir->defaults_list_map,
2089 g_strfreev (mime_types);
2092 g_key_file_free (key_file);
2097 g_key_file_free (key_file);
2099 if (mime_types != NULL)
2100 g_strfreev (mime_types);
2103 g_error_free (load_error);
2107 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2111 gchar *filename, **mime_types;
2112 char *unaliased_type;
2113 char **desktop_file_ids;
2120 if (dir->mimeapps_list_added_map != NULL &&
2121 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2122 &dir->mimeapps_list_timestamp))
2125 if (dir->mimeapps_list_added_map != NULL)
2126 g_hash_table_destroy (dir->mimeapps_list_added_map);
2127 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2128 g_free, (GDestroyNotify)g_strfreev);
2130 if (dir->mimeapps_list_removed_map != NULL)
2131 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2132 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2133 g_free, (GDestroyNotify)g_strfreev);
2135 key_file = g_key_file_new ();
2137 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2138 if (g_stat (filename, &buf) < 0)
2141 if (dir->mimeapps_list_timestamp > 0)
2142 mime_info_cache->should_ping_mime_monitor = TRUE;
2144 dir->mimeapps_list_timestamp = buf.st_mtime;
2146 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2150 if (load_error != NULL)
2153 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2155 if (mime_types != NULL)
2157 for (i = 0; mime_types[i] != NULL; i++)
2159 desktop_file_ids = g_key_file_get_string_list (key_file,
2160 ADDED_ASSOCIATIONS_GROUP,
2164 if (desktop_file_ids == NULL)
2167 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2168 g_hash_table_replace (dir->mimeapps_list_added_map,
2173 g_strfreev (mime_types);
2176 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2178 if (mime_types != NULL)
2180 for (i = 0; mime_types[i] != NULL; i++)
2182 desktop_file_ids = g_key_file_get_string_list (key_file,
2183 REMOVED_ASSOCIATIONS_GROUP,
2187 if (desktop_file_ids == NULL)
2190 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2191 g_hash_table_replace (dir->mimeapps_list_removed_map,
2196 g_strfreev (mime_types);
2199 g_key_file_free (key_file);
2204 g_key_file_free (key_file);
2206 if (mime_types != NULL)
2207 g_strfreev (mime_types);
2210 g_error_free (load_error);
2213 static MimeInfoCacheDir *
2214 mime_info_cache_dir_new (const char *path)
2216 MimeInfoCacheDir *dir;
2218 dir = g_new0 (MimeInfoCacheDir, 1);
2219 dir->path = g_strdup (path);
2225 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2230 if (dir->mime_info_cache_map != NULL)
2232 destroy_info_cache_map (dir->mime_info_cache_map);
2233 dir->mime_info_cache_map = NULL;
2237 if (dir->defaults_list_map != NULL)
2239 g_hash_table_destroy (dir->defaults_list_map);
2240 dir->defaults_list_map = NULL;
2243 if (dir->mimeapps_list_added_map != NULL)
2245 g_hash_table_destroy (dir->mimeapps_list_added_map);
2246 dir->mimeapps_list_added_map = NULL;
2249 if (dir->mimeapps_list_removed_map != NULL)
2251 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2252 dir->mimeapps_list_removed_map = NULL;
2259 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2260 const char *mime_type,
2261 char **new_desktop_file_ids)
2263 GList *desktop_file_ids;
2266 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2269 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2271 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2272 desktop_file_ids = g_list_append (desktop_file_ids,
2273 g_strdup (new_desktop_file_ids[i]));
2276 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2280 mime_info_cache_init_dir_lists (void)
2282 const char * const *dirs;
2285 mime_info_cache = mime_info_cache_new ();
2287 dirs = get_applications_search_path ();
2289 for (i = 0; dirs[i] != NULL; i++)
2291 MimeInfoCacheDir *dir;
2293 dir = mime_info_cache_dir_new (dirs[i]);
2297 mime_info_cache_dir_init (dir);
2298 mime_info_cache_dir_init_defaults_list (dir);
2299 mime_info_cache_dir_init_mimeapps_list (dir);
2301 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2307 mime_info_cache_update_dir_lists (void)
2311 tmp = mime_info_cache->dirs;
2315 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2317 /* No need to do this if we had file monitors... */
2318 mime_info_cache_blow_global_cache ();
2319 mime_info_cache_dir_init (dir);
2320 mime_info_cache_dir_init_defaults_list (dir);
2321 mime_info_cache_dir_init_mimeapps_list (dir);
2328 mime_info_cache_init (void)
2330 G_LOCK (mime_info_cache);
2331 if (mime_info_cache == NULL)
2332 mime_info_cache_init_dir_lists ();
2338 if (now >= mime_info_cache->last_stat_time + 10)
2340 mime_info_cache_update_dir_lists ();
2341 mime_info_cache->last_stat_time = now;
2345 if (mime_info_cache->should_ping_mime_monitor)
2347 /* g_idle_add (emit_mime_changed, NULL); */
2348 mime_info_cache->should_ping_mime_monitor = FALSE;
2351 G_UNLOCK (mime_info_cache);
2354 static MimeInfoCache *
2355 mime_info_cache_new (void)
2357 MimeInfoCache *cache;
2359 cache = g_new0 (MimeInfoCache, 1);
2361 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2362 (GDestroyNotify) g_free,
2363 (GDestroyNotify) g_free);
2368 mime_info_cache_free (MimeInfoCache *cache)
2373 g_list_foreach (cache->dirs,
2374 (GFunc) mime_info_cache_dir_free,
2376 g_list_free (cache->dirs);
2377 g_hash_table_destroy (cache->global_defaults_cache);
2382 * mime_info_cache_reload:
2383 * @dir: directory path which needs reloading.
2385 * Reload the mime information for the @dir.
2388 mime_info_cache_reload (const char *dir)
2390 /* FIXME: just reload the dir that needs reloading,
2391 * don't blow the whole cache
2393 if (mime_info_cache != NULL)
2395 G_LOCK (mime_info_cache);
2396 mime_info_cache_free (mime_info_cache);
2397 mime_info_cache = NULL;
2398 G_UNLOCK (mime_info_cache);
2403 append_desktop_entry (GList *list,
2404 const char *desktop_entry,
2405 GList *removed_entries)
2407 /* Add if not already in list, and valid */
2408 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2409 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2410 list = g_list_prepend (list, g_strdup (desktop_entry));
2416 * get_all_desktop_entries_for_mime_type:
2417 * @mime_type: a mime type.
2419 * Returns all the desktop ids for @mime_type. The desktop files
2420 * are listed in an order so that default applications are listed before
2421 * non-default ones, and handlers for inherited mimetypes are listed
2422 * after the base ones.
2424 * Return value: a #GList containing the desktop ids which claim
2425 * to handle @mime_type.
2428 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2430 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2431 MimeInfoCacheDir *dir;
2434 char **default_entries;
2435 char **removed_associations;
2438 mime_info_cache_init ();
2440 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2441 G_LOCK (mime_info_cache);
2443 removed_entries = NULL;
2444 desktop_entries = NULL;
2445 for (i = 0; mime_types[i] != NULL; i++)
2447 mime_type = mime_types[i];
2449 /* Go through all apps listed as defaults */
2450 for (dir_list = mime_info_cache->dirs;
2452 dir_list = dir_list->next)
2454 dir = dir_list->data;
2456 /* First added associations from mimeapps.list */
2457 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
2458 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2459 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2461 /* Then removed assiciations from mimeapps.list */
2462 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
2463 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2464 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2466 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
2467 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2468 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2469 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2472 /* Go through all entries that support the mimetype */
2473 for (dir_list = mime_info_cache->dirs;
2475 dir_list = dir_list->next)
2477 dir = dir_list->data;
2479 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2480 for (tmp = list; tmp != NULL; tmp = tmp->next)
2481 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2485 G_UNLOCK (mime_info_cache);
2487 g_strfreev (mime_types);
2489 g_list_free (removed_entries);
2491 desktop_entries = g_list_reverse (desktop_entries);
2493 return desktop_entries;
2496 /* GDesktopAppInfoLookup interface: */
2498 static void g_desktop_app_info_lookup_base_init (gpointer g_class);
2499 static void g_desktop_app_info_lookup_class_init (gpointer g_class,
2500 gpointer class_data);
2503 g_desktop_app_info_lookup_get_type (void)
2505 static volatile gsize g_define_type_id__volatile = 0;
2507 if (g_once_init_enter (&g_define_type_id__volatile))
2509 const GTypeInfo desktop_app_info_lookup_info =
2511 sizeof (GDesktopAppInfoLookupIface), /* class_size */
2512 g_desktop_app_info_lookup_base_init, /* base_init */
2513 NULL, /* base_finalize */
2514 g_desktop_app_info_lookup_class_init,
2515 NULL, /* class_finalize */
2516 NULL, /* class_data */
2518 0, /* n_preallocs */
2521 GType g_define_type_id =
2522 g_type_register_static (G_TYPE_INTERFACE, I_("GDesktopAppInfoLookup"),
2523 &desktop_app_info_lookup_info, 0);
2525 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
2527 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
2530 return g_define_type_id__volatile;
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"