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);
275 if (info->path && info->path[0] == '\0')
285 * g_desktop_app_info_new_from_filename:
286 * @filename: the path of a desktop file, in the GLib filename encoding
288 * Creates a new #GDesktopAppInfo.
290 * Returns: a new #GDesktopAppInfo or %NULL on error.
293 g_desktop_app_info_new_from_filename (const char *filename)
296 GDesktopAppInfo *info = NULL;
298 key_file = g_key_file_new ();
300 if (g_key_file_load_from_file (key_file,
305 info = g_desktop_app_info_new_from_keyfile (key_file);
307 info->filename = g_strdup (filename);
310 g_key_file_free (key_file);
316 * g_desktop_app_info_new:
317 * @desktop_id: the desktop file id
319 * Creates a new #GDesktopAppInfo.
321 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
324 g_desktop_app_info_new (const char *desktop_id)
326 GDesktopAppInfo *appinfo;
327 const char * const *dirs;
331 dirs = get_applications_search_path ();
333 basename = g_strdup (desktop_id);
335 for (i = 0; dirs[i] != NULL; i++)
340 filename = g_build_filename (dirs[i], desktop_id, NULL);
341 appinfo = g_desktop_app_info_new_from_filename (filename);
347 while ((p = strchr (p, '-')) != NULL)
351 filename = g_build_filename (dirs[i], basename, NULL);
352 appinfo = g_desktop_app_info_new_from_filename (filename);
367 appinfo->desktop_id = g_strdup (desktop_id);
369 if (g_desktop_app_info_get_is_hidden (appinfo))
371 g_object_unref (appinfo);
379 g_desktop_app_info_dup (GAppInfo *appinfo)
381 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
382 GDesktopAppInfo *new_info;
384 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
386 new_info->filename = g_strdup (info->filename);
387 new_info->desktop_id = g_strdup (info->desktop_id);
389 new_info->name = g_strdup (info->name);
390 new_info->comment = g_strdup (info->comment);
391 new_info->nodisplay = info->nodisplay;
392 new_info->icon_name = g_strdup (info->icon_name);
393 new_info->icon = g_object_ref (info->icon);
394 new_info->only_show_in = g_strdupv (info->only_show_in);
395 new_info->not_show_in = g_strdupv (info->not_show_in);
396 new_info->try_exec = g_strdup (info->try_exec);
397 new_info->exec = g_strdup (info->exec);
398 new_info->binary = g_strdup (info->binary);
399 new_info->path = g_strdup (info->path);
400 new_info->hidden = info->hidden;
401 new_info->terminal = info->terminal;
402 new_info->startup_notify = info->startup_notify;
404 return G_APP_INFO (new_info);
408 g_desktop_app_info_equal (GAppInfo *appinfo1,
411 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
412 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
414 if (info1->desktop_id == NULL ||
415 info2->desktop_id == NULL)
416 return info1 == info2;
418 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
422 g_desktop_app_info_get_id (GAppInfo *appinfo)
424 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
426 return info->desktop_id;
430 g_desktop_app_info_get_name (GAppInfo *appinfo)
432 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
434 if (info->name == NULL)
440 * g_desktop_app_info_get_is_hidden:
441 * @info: a #GDesktopAppInfo.
443 * A desktop file is hidden if the Hidden key in it is
446 * Returns: %TRUE if hidden, %FALSE otherwise.
449 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
455 g_desktop_app_info_get_description (GAppInfo *appinfo)
457 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
459 return info->comment;
463 g_desktop_app_info_get_executable (GAppInfo *appinfo)
465 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
471 g_desktop_app_info_get_icon (GAppInfo *appinfo)
473 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
479 expand_macro_single (char macro, char *uri)
485 file = g_file_new_for_uri (uri);
486 path = g_file_get_path (file);
487 g_object_unref (file);
493 result = g_shell_quote (uri);
498 result = g_shell_quote (path);
504 name = g_path_get_dirname (path);
505 result = g_shell_quote (name);
513 name = g_path_get_basename (path);
514 result = g_shell_quote (name);
526 expand_macro (char macro,
528 GDesktopAppInfo *info,
531 GList *uris = *uri_list;
534 g_return_if_fail (exec != NULL);
544 expanded = expand_macro_single (macro, uris->data);
547 g_string_append (exec, expanded);
561 expanded = expand_macro_single (macro, uris->data);
564 g_string_append (exec, expanded);
570 if (uris != NULL && expanded)
571 g_string_append_c (exec, ' ');
579 g_string_append (exec, "--icon ");
580 g_string_append (exec, info->icon_name);
586 g_string_append (exec, info->name);
591 g_string_append (exec, info->filename);
594 case 'm': /* deprecated */
598 g_string_append_c (exec, '%');
606 expand_application_parameters (GDesktopAppInfo *info,
612 GList *uri_list = *uris;
613 const char *p = info->exec;
614 GString *expanded_exec = g_string_new (NULL);
617 if (info->exec == NULL)
619 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
620 _("Desktop file didn't specify Exec field"));
626 if (p[0] == '%' && p[1] != '\0')
628 expand_macro (p[1], expanded_exec, info, uris);
632 g_string_append_c (expanded_exec, *p);
637 /* No file substitutions */
638 if (uri_list == *uris && uri_list != NULL)
640 /* If there is no macro default to %f. This is also what KDE does */
641 g_string_append_c (expanded_exec, ' ');
642 expand_macro ('f', expanded_exec, info, uris);
645 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
646 g_string_free (expanded_exec, TRUE);
651 prepend_terminal_to_vector (int *argc,
658 char **term_argv = NULL;
663 g_return_val_if_fail (argc != NULL, FALSE);
664 g_return_val_if_fail (argv != NULL, FALSE);
672 /* compute size if not given */
675 for (i = 0; the_argv[i] != NULL; i++)
681 term_argv = g_new0 (char *, 3);
683 check = g_find_program_in_path ("gnome-terminal");
686 term_argv[0] = check;
687 /* Note that gnome-terminal takes -x and
688 * as -e in gnome-terminal is broken we use that. */
689 term_argv[1] = g_strdup ("-x");
694 check = g_find_program_in_path ("nxterm");
696 check = g_find_program_in_path ("color-xterm");
698 check = g_find_program_in_path ("rxvt");
700 check = g_find_program_in_path ("xterm");
702 check = g_find_program_in_path ("dtterm");
705 check = g_strdup ("xterm");
706 g_warning ("couldn't find a terminal, falling back to xterm");
708 term_argv[0] = check;
709 term_argv[1] = g_strdup ("-e");
712 real_argc = term_argc + *argc;
713 real_argv = g_new (char *, real_argc + 1);
715 for (i = 0; i < term_argc; i++)
716 real_argv[i] = term_argv[i];
718 for (j = 0; j < *argc; j++, i++)
719 real_argv[i] = (char *)the_argv[j];
727 /* we use g_free here as we sucked all the inner strings
728 * out from it into real_argv */
733 #endif /* G_OS_WIN32 */
736 /* '=' is the new '\0'.
737 * DO NOT CALL unless at least one string ends with '='
740 is_env (const char *a,
745 if (*a == 0 || *b == 0)
758 /* free with g_strfreev */
760 replace_env_var (char **old_environ,
762 const char *new_value)
764 int length, new_length;
765 int index, new_index;
769 /* do two things at once:
770 * - discover the length of the environment ('length')
771 * - find the location (if any) of the env var ('index')
774 for (length = 0; old_environ[length]; length++)
776 /* if we already have it in our environment, replace */
777 if (is_env (old_environ[length], env_var))
782 /* no current env var, no desired env value.
785 if (new_value == NULL && index == -1)
788 /* in all cases now, we will be using a modified environment.
789 * determine its length and allocated it.
792 * new_index = location to insert, if any
793 * new_length = length of the new array
794 * new_environ = the pointer array for the new environment
797 if (new_value == NULL && index >= 0)
799 /* in this case, we will be removing an entry */
800 new_length = length - 1;
803 else if (new_value != NULL && index < 0)
805 /* in this case, we will be adding an entry to the end */
806 new_length = length + 1;
810 /* in this case, we will be replacing the existing entry */
816 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
817 new_environ[new_length] = NULL;
819 /* now we do the copying.
820 * for each entry in the new environment, we decide what to do
824 for (new_i = 0; new_i < new_length; new_i++)
826 if (new_i == new_index)
828 /* insert our new item */
829 new_environ[new_i] = g_strconcat (env_var,
834 /* if we had an old entry, skip it now */
840 /* if this is the old DESKTOP_STARTUP_ID, skip it */
844 /* copy an old item */
845 new_environ[new_i] = g_strdup (old_environ[i]);
850 g_strfreev (old_environ);
856 uri_list_segment_to_files (GList *start,
863 while (start != NULL && start != end)
865 file = g_file_new_for_uri ((char *)start->data);
866 res = g_list_prepend (res, file);
870 return g_list_reverse (res);
873 #ifdef HAVE__NSGETENVIRON
874 #define environ (*_NSGetEnviron())
875 #elif !defined(G_OS_WIN32)
877 /* According to the Single Unix Specification, environ is not in
878 * * any system header, although unistd.h often declares it.
880 extern char **environ;
884 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
886 GAppLaunchContext *launch_context,
889 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
890 gboolean completed = FALSE;
892 GList *launched_files;
899 g_return_val_if_fail (appinfo != NULL, FALSE);
907 if (!expand_application_parameters (info, &uris,
908 &argc, &argv, error))
911 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
913 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
914 _("Unable to find terminal required for application"));
921 launched_files = uri_list_segment_to_files (old_uris, uris);
923 display = g_app_launch_context_get_display (launch_context,
928 if (info->startup_notify)
929 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
933 if (display || sn_id)
937 envp = g_new0 (char *, 1);
939 envp = g_strdupv (environ);
943 envp = replace_env_var (envp,
948 envp = replace_env_var (envp,
949 "DESKTOP_STARTUP_ID",
955 g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
956 g_list_free (launched_files);
959 if (!g_spawn_async (info->path, /* working directory */
962 G_SPAWN_SEARCH_PATH /* flags */,
963 NULL /* child_setup */,
965 NULL /* child_pid */,
970 g_app_launch_context_launch_failed (launch_context, sn_id);
984 while (uris != NULL);
996 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
998 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1000 return info->exec &&
1001 ((strstr (info->exec, "%u") != NULL) ||
1002 (strstr (info->exec, "%U") != NULL));
1006 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1008 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1010 return info->exec &&
1011 ((strstr (info->exec, "%f") != NULL) ||
1012 (strstr (info->exec, "%F") != NULL));
1016 g_desktop_app_info_launch (GAppInfo *appinfo,
1018 GAppLaunchContext *launch_context,
1028 uri = g_file_get_uri (files->data);
1029 uris = g_list_prepend (uris, uri);
1030 files = files->next;
1033 uris = g_list_reverse (uris);
1035 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1037 g_list_foreach (uris, (GFunc)g_free, NULL);
1043 G_LOCK_DEFINE_STATIC (g_desktop_env);
1044 static gchar *g_desktop_env = NULL;
1047 * g_desktop_app_info_set_desktop_env:
1048 * @desktop_env: a string specifying what desktop this is
1050 * Sets the name of the desktop that the application is running in.
1051 * This is used by g_app_info_should_show() to evaluate the
1052 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1053 * desktop entry fields.
1055 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1056 * Menu specification</ulink> recognizes the following:
1058 * <member>GNOME</member>
1059 * <member>KDE</member>
1060 * <member>ROX</member>
1061 * <member>XFCE</member>
1062 * <member>Old</member>
1065 * Should be called only once; subsequent calls are ignored.
1068 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1070 G_LOCK (g_desktop_env);
1072 g_desktop_env = g_strdup (desktop_env);
1073 G_UNLOCK (g_desktop_env);
1077 g_desktop_app_info_should_show (GAppInfo *appinfo)
1079 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1081 const gchar *desktop_env;
1084 if (info->nodisplay)
1087 G_LOCK (g_desktop_env);
1088 desktop_env = g_desktop_env;
1089 G_UNLOCK (g_desktop_env);
1091 if (info->only_show_in)
1093 if (desktop_env == NULL)
1097 for (i = 0; info->only_show_in[i] != NULL; i++)
1099 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1109 if (info->not_show_in && desktop_env)
1111 for (i = 0; info->not_show_in[i] != NULL; i++)
1113 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1127 ensure_dir (DirType type,
1130 char *path, *display_name;
1133 if (type == APP_DIR)
1134 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1136 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1139 if (g_mkdir_with_parents (path, 0700) == 0)
1143 display_name = g_filename_display_name (path);
1144 if (type == APP_DIR)
1145 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1146 _("Can't create user application configuration folder %s: %s"),
1147 display_name, g_strerror (errsv));
1149 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1150 _("Can't create user MIME configuration folder %s: %s"),
1151 display_name, g_strerror (errsv));
1153 g_free (display_name);
1160 update_mimeapps_list (const char *desktop_id,
1161 const char *content_type,
1162 gboolean add_at_start,
1163 gboolean add_at_end,
1167 char *dirname, *filename;
1169 gboolean load_succeeded, res;
1172 gsize length, data_size;
1176 /* Don't add both at start and end */
1177 g_assert (!(add_at_start && add_at_end));
1179 dirname = ensure_dir (APP_DIR, error);
1183 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1186 key_file = g_key_file_new ();
1187 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1188 if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1190 g_key_file_free (key_file);
1191 key_file = g_key_file_new ();
1194 /* Add to the right place in the list */
1197 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1198 content_type, &length, NULL);
1200 list = g_new (char *, 1 + length + 1);
1204 list[i++] = g_strdup (desktop_id);
1207 for (j = 0; old_list[j] != NULL; j++)
1209 if (strcmp (old_list[j], desktop_id) != 0)
1210 list[i++] = g_strdup (old_list[j]);
1214 list[i++] = g_strdup (desktop_id);
1217 g_strfreev (old_list);
1219 g_key_file_set_string_list (key_file,
1220 ADDED_ASSOCIATIONS_GROUP,
1222 (const char * const *)list, i);
1226 /* Remove from removed associations group (unless remove) */
1229 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1230 content_type, &length, NULL);
1232 list = g_new (char *, 1 + length + 1);
1236 list[i++] = g_strdup (desktop_id);
1239 for (j = 0; old_list[j] != NULL; j++)
1241 if (strcmp (old_list[j], desktop_id) != 0)
1242 list[i++] = g_strdup (old_list[j]);
1247 g_strfreev (old_list);
1249 if (list[0] == NULL)
1250 g_key_file_remove_key (key_file,
1251 REMOVED_ASSOCIATIONS_GROUP,
1255 g_key_file_set_string_list (key_file,
1256 REMOVED_ASSOCIATIONS_GROUP,
1258 (const char * const *)list, i);
1263 data = g_key_file_to_data (key_file, &data_size, error);
1264 g_key_file_free (key_file);
1266 res = g_file_set_contents (filename, data, data_size, error);
1268 mime_info_cache_reload (NULL);
1277 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1278 const char *content_type,
1281 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1283 if (!g_desktop_app_info_ensure_saved (info, error))
1286 return update_mimeapps_list (info->desktop_id, content_type, TRUE, FALSE, FALSE, error);
1290 update_program_done (GPid pid,
1294 /* Did the application exit correctly */
1295 if (WIFEXITED (status) &&
1296 WEXITSTATUS (status) == 0)
1298 /* Here we could clean out any caches in use */
1303 run_update_command (char *command,
1312 GError *error = NULL;
1315 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1317 if (g_spawn_async ("/", argv,
1319 G_SPAWN_SEARCH_PATH |
1320 G_SPAWN_STDOUT_TO_DEV_NULL |
1321 G_SPAWN_STDERR_TO_DEV_NULL |
1322 G_SPAWN_DO_NOT_REAP_CHILD,
1323 NULL, NULL, /* No setup function */
1326 g_child_watch_add (pid, update_program_done, NULL);
1329 /* If we get an error at this point, it's quite likely the user doesn't
1330 * have an installed copy of either 'update-mime-database' or
1331 * 'update-desktop-database'. I don't think we want to popup an error
1332 * dialog at this point, so we just do a g_warning to give the user a
1333 * chance of debugging it.
1335 g_warning ("%s", error->message);
1342 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1343 const char *extension,
1346 char *filename, *basename, *mimetype;
1350 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1353 dirname = ensure_dir (MIMETYPE_DIR, error);
1357 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1358 filename = g_build_filename (dirname, basename, NULL);
1362 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1364 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1369 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1370 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1371 " <mime-type type=\"%s\">\n"
1372 " <comment>%s document</comment>\n"
1373 " <glob pattern=\"*.%s\"/>\n"
1375 "</mime-info>\n", mimetype, extension, extension);
1377 g_file_set_contents (filename, contents, -1, NULL);
1380 run_update_command ("update-mime-database", "mime");
1384 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1394 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1395 const char *content_type,
1398 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1400 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1403 return update_mimeapps_list (info->desktop_id, content_type, FALSE, TRUE, FALSE, error);
1407 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1413 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1414 const char *content_type,
1417 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1419 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1422 return update_mimeapps_list (info->desktop_id, content_type, FALSE, FALSE, TRUE, error);
1426 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1432 char *data, *desktop_id;
1437 if (info->filename != NULL)
1440 /* This is only used for object created with
1441 * g_app_info_create_from_commandline. All other
1442 * object should have a filename
1445 dirname = ensure_dir (APP_DIR, error);
1449 key_file = g_key_file_new ();
1451 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1452 "Encoding", "UTF-8");
1453 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1454 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1455 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1456 G_KEY_FILE_DESKTOP_KEY_TYPE,
1457 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1459 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1460 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1462 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1463 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1465 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1466 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1468 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1469 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1471 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1472 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1474 data = g_key_file_to_data (key_file, &data_size, NULL);
1475 g_key_file_free (key_file);
1477 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1478 filename = g_build_filename (dirname, desktop_id, NULL);
1479 g_free (desktop_id);
1482 fd = g_mkstemp (filename);
1487 display_name = g_filename_display_name (filename);
1488 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1489 _("Can't create user desktop file %s"), display_name);
1490 g_free (display_name);
1496 desktop_id = g_path_get_basename (filename);
1500 res = g_file_set_contents (filename, data, data_size, error);
1503 g_free (desktop_id);
1508 info->filename = filename;
1509 info->desktop_id = desktop_id;
1511 run_update_command ("update-desktop-database", "applications");
1517 * g_app_info_create_from_commandline:
1518 * @commandline: the commandline to use
1519 * @application_name: the application name, or %NULL to use @commandline
1520 * @flags: flags that can specify details of the created #GAppInfo
1521 * @error: a #GError location to store the error occuring, %NULL to ignore.
1523 * Creates a new #GAppInfo from the given information.
1525 * Returns: new #GAppInfo for given command.
1528 g_app_info_create_from_commandline (const char *commandline,
1529 const char *application_name,
1530 GAppInfoCreateFlags flags,
1535 GDesktopAppInfo *info;
1537 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1539 info->filename = NULL;
1540 info->desktop_id = NULL;
1542 info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
1543 info->startup_notify = FALSE;
1544 info->hidden = FALSE;
1545 if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
1546 info->exec = g_strconcat (commandline, " %u", NULL);
1548 info->exec = g_strconcat (commandline, " %f", NULL);
1549 info->nodisplay = TRUE;
1550 info->binary = binary_from_exec (info->exec);
1552 if (application_name)
1553 info->name = g_strdup (application_name);
1556 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1557 split = g_strsplit (commandline, " ", 2);
1558 basename = g_path_get_basename (split[0]);
1560 info->name = basename;
1561 if (info->name == NULL)
1562 info->name = g_strdup ("custom");
1564 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
1566 return G_APP_INFO (info);
1570 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1572 iface->dup = g_desktop_app_info_dup;
1573 iface->equal = g_desktop_app_info_equal;
1574 iface->get_id = g_desktop_app_info_get_id;
1575 iface->get_name = g_desktop_app_info_get_name;
1576 iface->get_description = g_desktop_app_info_get_description;
1577 iface->get_executable = g_desktop_app_info_get_executable;
1578 iface->get_icon = g_desktop_app_info_get_icon;
1579 iface->launch = g_desktop_app_info_launch;
1580 iface->supports_uris = g_desktop_app_info_supports_uris;
1581 iface->supports_files = g_desktop_app_info_supports_files;
1582 iface->launch_uris = g_desktop_app_info_launch_uris;
1583 iface->should_show = g_desktop_app_info_should_show;
1584 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1585 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1586 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1587 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1588 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1592 app_info_in_list (GAppInfo *info,
1595 while (list != NULL)
1597 if (g_app_info_equal (info, list->data))
1606 * g_app_info_get_all_for_type:
1607 * @content_type: the content type to find a #GAppInfo for
1609 * Gets a list of all #GAppInfo s for a given content type.
1611 * Returns: #GList of #GAppInfo s for given @content_type
1612 * or %NULL on error.
1615 g_app_info_get_all_for_type (const char *content_type)
1617 GList *desktop_entries, *l;
1619 GDesktopAppInfo *info;
1621 g_return_val_if_fail (content_type != NULL, NULL);
1623 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1626 for (l = desktop_entries; l != NULL; l = l->next)
1628 char *desktop_entry = l->data;
1630 info = g_desktop_app_info_new (desktop_entry);
1633 if (app_info_in_list (G_APP_INFO (info), infos))
1634 g_object_unref (info);
1636 infos = g_list_prepend (infos, info);
1638 g_free (desktop_entry);
1641 g_list_free (desktop_entries);
1643 return g_list_reverse (infos);
1648 * g_app_info_get_default_for_type:
1649 * @content_type: the content type to find a #GAppInfo for
1650 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1653 * Gets the #GAppInfo that correspond to a given content type.
1655 * Returns: #GAppInfo for given @content_type or %NULL on error.
1658 g_app_info_get_default_for_type (const char *content_type,
1659 gboolean must_support_uris)
1661 GList *desktop_entries, *l;
1664 g_return_val_if_fail (content_type != NULL, NULL);
1666 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1669 for (l = desktop_entries; l != NULL; l = l->next)
1671 char *desktop_entry = l->data;
1673 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1676 if (must_support_uris && !g_app_info_supports_uris (info))
1678 g_object_unref (info);
1686 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1687 g_list_free (desktop_entries);
1693 * g_app_info_get_default_for_uri_scheme:
1694 * @uri_scheme: a string containing a URI scheme.
1696 * Gets the default application for launching applications
1697 * using this URI scheme. A URI scheme is the initial part
1698 * of the URI, up to but not including the ':', e.g. "http",
1701 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
1704 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1706 static gsize lookup = 0;
1708 if (g_once_init_enter (&lookup))
1710 gsize setup_value = 1;
1711 GDesktopAppInfoLookup *lookup_instance;
1712 const char *use_this;
1713 GIOExtensionPoint *ep;
1714 GIOExtension *extension;
1717 use_this = g_getenv ("GIO_USE_URI_ASSOCIATION");
1719 /* Ensure vfs in modules loaded */
1720 _g_io_modules_ensure_loaded ();
1722 ep = g_io_extension_point_lookup (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1724 lookup_instance = NULL;
1727 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
1729 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1732 if (lookup_instance == NULL)
1734 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1736 extension = l->data;
1737 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1738 if (lookup_instance != NULL)
1743 if (lookup_instance != NULL)
1744 setup_value = (gsize)lookup_instance;
1746 g_once_init_leave (&lookup, setup_value);
1752 return g_desktop_app_info_lookup_get_default_for_uri_scheme (G_DESKTOP_APP_INFO_LOOKUP (lookup),
1758 get_apps_from_dir (GHashTable *apps,
1759 const char *dirname,
1763 const char *basename;
1764 char *filename, *subprefix, *desktop_id;
1766 GDesktopAppInfo *appinfo;
1768 dir = g_dir_open (dirname, 0, NULL);
1771 while ((basename = g_dir_read_name (dir)) != NULL)
1773 filename = g_build_filename (dirname, basename, NULL);
1774 if (g_str_has_suffix (basename, ".desktop"))
1776 desktop_id = g_strconcat (prefix, basename, NULL);
1778 /* Use _extended so we catch NULLs too (hidden) */
1779 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1781 appinfo = g_desktop_app_info_new_from_filename (filename);
1783 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1785 g_object_unref (appinfo);
1790 if (appinfo || hidden)
1792 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1796 /* Reuse instead of strdup here */
1797 appinfo->desktop_id = desktop_id;
1802 g_free (desktop_id);
1806 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1808 subprefix = g_strconcat (prefix, basename, "-", NULL);
1809 get_apps_from_dir (apps, filename, subprefix);
1821 * g_app_info_get_all:
1823 * Gets a list of all of the applications currently registered
1826 * For desktop files, this includes applications that have
1827 * <literal>NoDisplay=true</literal> set or are excluded from
1828 * display by means of <literal>OnlyShowIn</literal> or
1829 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1830 * The returned list does not include applications which have
1831 * the <literal>Hidden</literal> key set.
1833 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1836 g_app_info_get_all (void)
1838 const char * const *dirs;
1840 GHashTableIter iter;
1845 dirs = get_applications_search_path ();
1847 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1851 for (i = 0; dirs[i] != NULL; i++)
1852 get_apps_from_dir (apps, dirs[i], "");
1856 g_hash_table_iter_init (&iter, apps);
1857 while (g_hash_table_iter_next (&iter, NULL, &value))
1860 infos = g_list_prepend (infos, value);
1863 g_hash_table_destroy (apps);
1865 return g_list_reverse (infos);
1868 /* Cacheing of mimeinfo.cache and defaults.list files */
1872 GHashTable *mime_info_cache_map;
1873 GHashTable *defaults_list_map;
1874 GHashTable *mimeapps_list_added_map;
1875 GHashTable *mimeapps_list_removed_map;
1876 time_t mime_info_cache_timestamp;
1877 time_t defaults_list_timestamp;
1878 time_t mimeapps_list_timestamp;
1882 GList *dirs; /* mimeinfo.cache and defaults.list */
1883 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1884 time_t last_stat_time;
1885 guint should_ping_mime_monitor : 1;
1888 static MimeInfoCache *mime_info_cache = NULL;
1889 G_LOCK_DEFINE_STATIC (mime_info_cache);
1891 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1892 const char *mime_type,
1893 char **new_desktop_file_ids);
1895 static MimeInfoCache * mime_info_cache_new (void);
1898 destroy_info_cache_value (gpointer key,
1902 g_list_foreach (value, (GFunc)g_free, NULL);
1903 g_list_free (value);
1907 destroy_info_cache_map (GHashTable *info_cache_map)
1909 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1910 g_hash_table_destroy (info_cache_map);
1914 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1915 const char *cache_file,
1921 filename = g_build_filename (dir->path, cache_file, NULL);
1923 if (g_stat (filename, &buf) < 0)
1930 if (buf.st_mtime != *timestamp)
1936 /* Call with lock held */
1938 remove_all (gpointer key,
1947 mime_info_cache_blow_global_cache (void)
1949 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1954 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1958 gchar *filename, **mime_types;
1965 if (dir->mime_info_cache_map != NULL &&
1966 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1967 &dir->mime_info_cache_timestamp))
1970 if (dir->mime_info_cache_map != NULL)
1971 destroy_info_cache_map (dir->mime_info_cache_map);
1973 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1974 (GDestroyNotify) g_free,
1977 key_file = g_key_file_new ();
1979 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1981 if (g_stat (filename, &buf) < 0)
1984 if (dir->mime_info_cache_timestamp > 0)
1985 mime_info_cache->should_ping_mime_monitor = TRUE;
1987 dir->mime_info_cache_timestamp = buf.st_mtime;
1989 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1994 if (load_error != NULL)
1997 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2000 if (load_error != NULL)
2003 for (i = 0; mime_types[i] != NULL; i++)
2005 gchar **desktop_file_ids;
2006 char *unaliased_type;
2007 desktop_file_ids = g_key_file_get_string_list (key_file,
2013 if (desktop_file_ids == NULL)
2016 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2017 mime_info_cache_dir_add_desktop_entries (dir,
2020 g_free (unaliased_type);
2022 g_strfreev (desktop_file_ids);
2025 g_strfreev (mime_types);
2026 g_key_file_free (key_file);
2031 g_key_file_free (key_file);
2033 if (mime_types != NULL)
2034 g_strfreev (mime_types);
2037 g_error_free (load_error);
2041 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2045 gchar *filename, **mime_types;
2046 char *unaliased_type;
2047 char **desktop_file_ids;
2054 if (dir->defaults_list_map != NULL &&
2055 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2056 &dir->defaults_list_timestamp))
2059 if (dir->defaults_list_map != NULL)
2060 g_hash_table_destroy (dir->defaults_list_map);
2061 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2062 g_free, (GDestroyNotify)g_strfreev);
2065 key_file = g_key_file_new ();
2067 filename = g_build_filename (dir->path, "defaults.list", NULL);
2068 if (g_stat (filename, &buf) < 0)
2071 if (dir->defaults_list_timestamp > 0)
2072 mime_info_cache->should_ping_mime_monitor = TRUE;
2074 dir->defaults_list_timestamp = buf.st_mtime;
2076 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2080 if (load_error != NULL)
2083 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2085 if (mime_types != NULL)
2087 for (i = 0; mime_types[i] != NULL; i++)
2089 desktop_file_ids = g_key_file_get_string_list (key_file,
2090 DEFAULT_APPLICATIONS_GROUP,
2094 if (desktop_file_ids == NULL)
2097 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2098 g_hash_table_replace (dir->defaults_list_map,
2103 g_strfreev (mime_types);
2106 g_key_file_free (key_file);
2111 g_key_file_free (key_file);
2113 if (mime_types != NULL)
2114 g_strfreev (mime_types);
2117 g_error_free (load_error);
2121 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2125 gchar *filename, **mime_types;
2126 char *unaliased_type;
2127 char **desktop_file_ids;
2134 if (dir->mimeapps_list_added_map != NULL &&
2135 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2136 &dir->mimeapps_list_timestamp))
2139 if (dir->mimeapps_list_added_map != NULL)
2140 g_hash_table_destroy (dir->mimeapps_list_added_map);
2141 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2142 g_free, (GDestroyNotify)g_strfreev);
2144 if (dir->mimeapps_list_removed_map != NULL)
2145 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2146 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2147 g_free, (GDestroyNotify)g_strfreev);
2149 key_file = g_key_file_new ();
2151 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2152 if (g_stat (filename, &buf) < 0)
2155 if (dir->mimeapps_list_timestamp > 0)
2156 mime_info_cache->should_ping_mime_monitor = TRUE;
2158 dir->mimeapps_list_timestamp = buf.st_mtime;
2160 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2164 if (load_error != NULL)
2167 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2169 if (mime_types != NULL)
2171 for (i = 0; mime_types[i] != NULL; i++)
2173 desktop_file_ids = g_key_file_get_string_list (key_file,
2174 ADDED_ASSOCIATIONS_GROUP,
2178 if (desktop_file_ids == NULL)
2181 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2182 g_hash_table_replace (dir->mimeapps_list_added_map,
2187 g_strfreev (mime_types);
2190 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2192 if (mime_types != NULL)
2194 for (i = 0; mime_types[i] != NULL; i++)
2196 desktop_file_ids = g_key_file_get_string_list (key_file,
2197 REMOVED_ASSOCIATIONS_GROUP,
2201 if (desktop_file_ids == NULL)
2204 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2205 g_hash_table_replace (dir->mimeapps_list_removed_map,
2210 g_strfreev (mime_types);
2213 g_key_file_free (key_file);
2218 g_key_file_free (key_file);
2220 if (mime_types != NULL)
2221 g_strfreev (mime_types);
2224 g_error_free (load_error);
2227 static MimeInfoCacheDir *
2228 mime_info_cache_dir_new (const char *path)
2230 MimeInfoCacheDir *dir;
2232 dir = g_new0 (MimeInfoCacheDir, 1);
2233 dir->path = g_strdup (path);
2239 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2244 if (dir->mime_info_cache_map != NULL)
2246 destroy_info_cache_map (dir->mime_info_cache_map);
2247 dir->mime_info_cache_map = NULL;
2251 if (dir->defaults_list_map != NULL)
2253 g_hash_table_destroy (dir->defaults_list_map);
2254 dir->defaults_list_map = NULL;
2257 if (dir->mimeapps_list_added_map != NULL)
2259 g_hash_table_destroy (dir->mimeapps_list_added_map);
2260 dir->mimeapps_list_added_map = NULL;
2263 if (dir->mimeapps_list_removed_map != NULL)
2265 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2266 dir->mimeapps_list_removed_map = NULL;
2273 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2274 const char *mime_type,
2275 char **new_desktop_file_ids)
2277 GList *desktop_file_ids;
2280 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2283 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2285 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2286 desktop_file_ids = g_list_append (desktop_file_ids,
2287 g_strdup (new_desktop_file_ids[i]));
2290 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2294 mime_info_cache_init_dir_lists (void)
2296 const char * const *dirs;
2299 mime_info_cache = mime_info_cache_new ();
2301 dirs = get_applications_search_path ();
2303 for (i = 0; dirs[i] != NULL; i++)
2305 MimeInfoCacheDir *dir;
2307 dir = mime_info_cache_dir_new (dirs[i]);
2311 mime_info_cache_dir_init (dir);
2312 mime_info_cache_dir_init_defaults_list (dir);
2313 mime_info_cache_dir_init_mimeapps_list (dir);
2315 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2321 mime_info_cache_update_dir_lists (void)
2325 tmp = mime_info_cache->dirs;
2329 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2331 /* No need to do this if we had file monitors... */
2332 mime_info_cache_blow_global_cache ();
2333 mime_info_cache_dir_init (dir);
2334 mime_info_cache_dir_init_defaults_list (dir);
2335 mime_info_cache_dir_init_mimeapps_list (dir);
2342 mime_info_cache_init (void)
2344 G_LOCK (mime_info_cache);
2345 if (mime_info_cache == NULL)
2346 mime_info_cache_init_dir_lists ();
2352 if (now >= mime_info_cache->last_stat_time + 10)
2354 mime_info_cache_update_dir_lists ();
2355 mime_info_cache->last_stat_time = now;
2359 if (mime_info_cache->should_ping_mime_monitor)
2361 /* g_idle_add (emit_mime_changed, NULL); */
2362 mime_info_cache->should_ping_mime_monitor = FALSE;
2365 G_UNLOCK (mime_info_cache);
2368 static MimeInfoCache *
2369 mime_info_cache_new (void)
2371 MimeInfoCache *cache;
2373 cache = g_new0 (MimeInfoCache, 1);
2375 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2376 (GDestroyNotify) g_free,
2377 (GDestroyNotify) g_free);
2382 mime_info_cache_free (MimeInfoCache *cache)
2387 g_list_foreach (cache->dirs,
2388 (GFunc) mime_info_cache_dir_free,
2390 g_list_free (cache->dirs);
2391 g_hash_table_destroy (cache->global_defaults_cache);
2396 * mime_info_cache_reload:
2397 * @dir: directory path which needs reloading.
2399 * Reload the mime information for the @dir.
2402 mime_info_cache_reload (const char *dir)
2404 /* FIXME: just reload the dir that needs reloading,
2405 * don't blow the whole cache
2407 if (mime_info_cache != NULL)
2409 G_LOCK (mime_info_cache);
2410 mime_info_cache_free (mime_info_cache);
2411 mime_info_cache = NULL;
2412 G_UNLOCK (mime_info_cache);
2417 append_desktop_entry (GList *list,
2418 const char *desktop_entry,
2419 GList *removed_entries)
2421 /* Add if not already in list, and valid */
2422 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2423 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2424 list = g_list_prepend (list, g_strdup (desktop_entry));
2430 * get_all_desktop_entries_for_mime_type:
2431 * @mime_type: a mime type.
2433 * Returns all the desktop ids for @mime_type. The desktop files
2434 * are listed in an order so that default applications are listed before
2435 * non-default ones, and handlers for inherited mimetypes are listed
2436 * after the base ones.
2438 * Return value: a #GList containing the desktop ids which claim
2439 * to handle @mime_type.
2442 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2444 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2445 MimeInfoCacheDir *dir;
2448 char **default_entries;
2449 char **removed_associations;
2454 mime_info_cache_init ();
2456 /* collect all ancestors */
2457 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2458 array = g_ptr_array_new ();
2459 for (i = 0; mime_types[i]; i++)
2460 g_ptr_array_add (array, mime_types[i]);
2461 g_free (mime_types);
2462 for (i = 0; i < array->len; i++)
2464 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
2465 for (j = 0; anc[j]; j++)
2467 for (k = 0; k < array->len; k++)
2469 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
2472 if (k == array->len) /* not found */
2473 g_ptr_array_add (array, g_strdup (anc[j]));
2477 g_ptr_array_add (array, NULL);
2478 mime_types = (char **)g_ptr_array_free (array, FALSE);
2480 G_LOCK (mime_info_cache);
2482 removed_entries = NULL;
2483 desktop_entries = NULL;
2484 for (i = 0; mime_types[i] != NULL; i++)
2486 mime_type = mime_types[i];
2488 /* Go through all apps listed as defaults */
2489 for (dir_list = mime_info_cache->dirs;
2491 dir_list = dir_list->next)
2493 dir = dir_list->data;
2495 /* First added associations from mimeapps.list */
2496 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
2497 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2498 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2500 /* Then removed associations from mimeapps.list */
2501 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
2502 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2503 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2505 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
2506 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2507 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2508 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2511 /* Go through all entries that support the mimetype */
2512 for (dir_list = mime_info_cache->dirs;
2514 dir_list = dir_list->next)
2516 dir = dir_list->data;
2518 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2519 for (tmp = list; tmp != NULL; tmp = tmp->next)
2520 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2524 G_UNLOCK (mime_info_cache);
2526 g_strfreev (mime_types);
2528 g_list_free (removed_entries);
2530 desktop_entries = g_list_reverse (desktop_entries);
2532 return desktop_entries;
2535 /* GDesktopAppInfoLookup interface: */
2537 static void g_desktop_app_info_lookup_base_init (gpointer g_class);
2538 static void g_desktop_app_info_lookup_class_init (gpointer g_class,
2539 gpointer class_data);
2542 g_desktop_app_info_lookup_get_type (void)
2544 static volatile gsize g_define_type_id__volatile = 0;
2546 if (g_once_init_enter (&g_define_type_id__volatile))
2548 const GTypeInfo desktop_app_info_lookup_info =
2550 sizeof (GDesktopAppInfoLookupIface), /* class_size */
2551 g_desktop_app_info_lookup_base_init, /* base_init */
2552 NULL, /* base_finalize */
2553 g_desktop_app_info_lookup_class_init,
2554 NULL, /* class_finalize */
2555 NULL, /* class_data */
2557 0, /* n_preallocs */
2560 GType g_define_type_id =
2561 g_type_register_static (G_TYPE_INTERFACE, I_("GDesktopAppInfoLookup"),
2562 &desktop_app_info_lookup_info, 0);
2564 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
2566 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
2569 return g_define_type_id__volatile;
2573 g_desktop_app_info_lookup_class_init (gpointer g_class,
2574 gpointer class_data)
2579 g_desktop_app_info_lookup_base_init (gpointer g_class)
2584 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
2585 * @lookup: a #GDesktopAppInfoLookup
2586 * @uri_scheme: a string containing a URI scheme.
2588 * Gets the default application for launching applications
2589 * using this URI scheme for a particular GDesktopAppInfoLookup
2592 * The GDesktopAppInfoLookup interface and this function is used
2593 * to implement g_app_info_get_default_for_uri_scheme() backends
2594 * in a GIO module. There is no reason for applications to use it
2595 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
2597 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
2600 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
2601 const char *uri_scheme)
2603 GDesktopAppInfoLookupIface *iface;
2605 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
2607 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
2609 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
2612 #define __G_DESKTOP_APP_INFO_C__
2613 #include "gioaliasdef.c"