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;
1175 char **content_types;
1177 /* Don't add both at start and end */
1178 g_assert (!(add_at_start && add_at_end));
1180 dirname = ensure_dir (APP_DIR, error);
1184 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1187 key_file = g_key_file_new ();
1188 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1189 if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1191 g_key_file_free (key_file);
1192 key_file = g_key_file_new ();
1197 content_types = g_new (char *, 2);
1198 content_types[0] = g_strdup (content_type);
1199 content_types[1] = NULL;
1203 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1206 for (k = 0; content_types && content_types[k]; k++)
1208 /* Add to the right place in the list */
1211 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1212 content_types[k], &length, NULL);
1214 list = g_new (char *, 1 + length + 1);
1218 list[i++] = g_strdup (desktop_id);
1221 for (j = 0; old_list[j] != NULL; j++)
1223 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1224 list[i++] = g_strdup (old_list[j]);
1228 list[i++] = g_strdup (desktop_id);
1231 g_strfreev (old_list);
1233 if (list[0] == NULL || desktop_id == NULL)
1234 g_key_file_remove_key (key_file,
1235 ADDED_ASSOCIATIONS_GROUP,
1239 g_key_file_set_string_list (key_file,
1240 ADDED_ASSOCIATIONS_GROUP,
1242 (const char * const *)list, i);
1249 /* reuse the list from above */
1253 g_strfreev (content_types);
1254 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1257 for (k = 0; content_types && content_types[k]; k++)
1259 /* Remove from removed associations group (unless remove) */
1262 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1263 content_types[k], &length, NULL);
1265 list = g_new (char *, 1 + length + 1);
1269 list[i++] = g_strdup (desktop_id);
1272 for (j = 0; old_list[j] != NULL; j++)
1274 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1275 list[i++] = g_strdup (old_list[j]);
1280 g_strfreev (old_list);
1282 if (list[0] == NULL || desktop_id == NULL)
1283 g_key_file_remove_key (key_file,
1284 REMOVED_ASSOCIATIONS_GROUP,
1288 g_key_file_set_string_list (key_file,
1289 REMOVED_ASSOCIATIONS_GROUP,
1291 (const char * const *)list, i);
1296 g_strfreev (content_types);
1298 data = g_key_file_to_data (key_file, &data_size, error);
1299 g_key_file_free (key_file);
1301 res = g_file_set_contents (filename, data, data_size, error);
1303 mime_info_cache_reload (NULL);
1312 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1313 const char *content_type,
1316 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1318 if (!g_desktop_app_info_ensure_saved (info, error))
1321 return update_mimeapps_list (info->desktop_id, content_type, TRUE, FALSE, FALSE, error);
1325 update_program_done (GPid pid,
1329 /* Did the application exit correctly */
1330 if (WIFEXITED (status) &&
1331 WEXITSTATUS (status) == 0)
1333 /* Here we could clean out any caches in use */
1338 run_update_command (char *command,
1347 GError *error = NULL;
1350 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1352 if (g_spawn_async ("/", argv,
1354 G_SPAWN_SEARCH_PATH |
1355 G_SPAWN_STDOUT_TO_DEV_NULL |
1356 G_SPAWN_STDERR_TO_DEV_NULL |
1357 G_SPAWN_DO_NOT_REAP_CHILD,
1358 NULL, NULL, /* No setup function */
1361 g_child_watch_add (pid, update_program_done, NULL);
1364 /* If we get an error at this point, it's quite likely the user doesn't
1365 * have an installed copy of either 'update-mime-database' or
1366 * 'update-desktop-database'. I don't think we want to popup an error
1367 * dialog at this point, so we just do a g_warning to give the user a
1368 * chance of debugging it.
1370 g_warning ("%s", error->message);
1377 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1378 const char *extension,
1381 char *filename, *basename, *mimetype;
1385 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1388 dirname = ensure_dir (MIMETYPE_DIR, error);
1392 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1393 filename = g_build_filename (dirname, basename, NULL);
1397 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1399 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1404 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1405 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1406 " <mime-type type=\"%s\">\n"
1407 " <comment>%s document</comment>\n"
1408 " <glob pattern=\"*.%s\"/>\n"
1410 "</mime-info>\n", mimetype, extension, extension);
1412 g_file_set_contents (filename, contents, -1, NULL);
1415 run_update_command ("update-mime-database", "mime");
1419 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1429 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1430 const char *content_type,
1433 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1435 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1438 return update_mimeapps_list (info->desktop_id, content_type, FALSE, TRUE, FALSE, error);
1442 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1448 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1449 const char *content_type,
1452 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1454 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1457 return update_mimeapps_list (info->desktop_id, content_type, FALSE, FALSE, TRUE, error);
1461 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1467 char *data, *desktop_id;
1472 if (info->filename != NULL)
1475 /* This is only used for object created with
1476 * g_app_info_create_from_commandline. All other
1477 * object should have a filename
1480 dirname = ensure_dir (APP_DIR, error);
1484 key_file = g_key_file_new ();
1486 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1487 "Encoding", "UTF-8");
1488 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1489 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1490 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1491 G_KEY_FILE_DESKTOP_KEY_TYPE,
1492 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1494 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1495 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1497 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1498 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1500 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1501 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1503 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1504 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1506 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1507 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1509 data = g_key_file_to_data (key_file, &data_size, NULL);
1510 g_key_file_free (key_file);
1512 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1513 filename = g_build_filename (dirname, desktop_id, NULL);
1514 g_free (desktop_id);
1517 fd = g_mkstemp (filename);
1522 display_name = g_filename_display_name (filename);
1523 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1524 _("Can't create user desktop file %s"), display_name);
1525 g_free (display_name);
1531 desktop_id = g_path_get_basename (filename);
1535 res = g_file_set_contents (filename, data, data_size, error);
1538 g_free (desktop_id);
1543 info->filename = filename;
1544 info->desktop_id = desktop_id;
1546 run_update_command ("update-desktop-database", "applications");
1552 g_desktop_app_info_can_delete (GAppInfo *appinfo)
1554 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1557 return g_access (info->filename, W_OK) == 0;
1563 g_desktop_app_info_delete (GAppInfo *appinfo)
1565 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1569 if (g_remove (info->filename) == 0)
1571 update_mimeapps_list (info->desktop_id, NULL, FALSE, FALSE, FALSE, NULL);
1573 g_free (info->filename);
1574 info->filename = NULL;
1575 g_free (info->desktop_id);
1576 info->desktop_id = NULL;
1586 * g_app_info_create_from_commandline:
1587 * @commandline: the commandline to use
1588 * @application_name: the application name, or %NULL to use @commandline
1589 * @flags: flags that can specify details of the created #GAppInfo
1590 * @error: a #GError location to store the error occuring, %NULL to ignore.
1592 * Creates a new #GAppInfo from the given information.
1594 * Returns: new #GAppInfo for given command.
1597 g_app_info_create_from_commandline (const char *commandline,
1598 const char *application_name,
1599 GAppInfoCreateFlags flags,
1604 GDesktopAppInfo *info;
1606 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1608 info->filename = NULL;
1609 info->desktop_id = NULL;
1611 info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
1612 info->startup_notify = FALSE;
1613 info->hidden = FALSE;
1614 if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
1615 info->exec = g_strconcat (commandline, " %u", NULL);
1617 info->exec = g_strconcat (commandline, " %f", NULL);
1618 info->nodisplay = TRUE;
1619 info->binary = binary_from_exec (info->exec);
1621 if (application_name)
1622 info->name = g_strdup (application_name);
1625 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1626 split = g_strsplit (commandline, " ", 2);
1627 basename = g_path_get_basename (split[0]);
1629 info->name = basename;
1630 if (info->name == NULL)
1631 info->name = g_strdup ("custom");
1633 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
1635 return G_APP_INFO (info);
1639 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1641 iface->dup = g_desktop_app_info_dup;
1642 iface->equal = g_desktop_app_info_equal;
1643 iface->get_id = g_desktop_app_info_get_id;
1644 iface->get_name = g_desktop_app_info_get_name;
1645 iface->get_description = g_desktop_app_info_get_description;
1646 iface->get_executable = g_desktop_app_info_get_executable;
1647 iface->get_icon = g_desktop_app_info_get_icon;
1648 iface->launch = g_desktop_app_info_launch;
1649 iface->supports_uris = g_desktop_app_info_supports_uris;
1650 iface->supports_files = g_desktop_app_info_supports_files;
1651 iface->launch_uris = g_desktop_app_info_launch_uris;
1652 iface->should_show = g_desktop_app_info_should_show;
1653 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1654 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1655 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1656 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1657 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1658 iface->can_delete = g_desktop_app_info_can_delete;
1659 iface->do_delete = g_desktop_app_info_delete;
1663 app_info_in_list (GAppInfo *info,
1666 while (list != NULL)
1668 if (g_app_info_equal (info, list->data))
1677 * g_app_info_get_all_for_type:
1678 * @content_type: the content type to find a #GAppInfo for
1680 * Gets a list of all #GAppInfo s for a given content type.
1682 * Returns: #GList of #GAppInfo s for given @content_type
1683 * or %NULL on error.
1686 g_app_info_get_all_for_type (const char *content_type)
1688 GList *desktop_entries, *l;
1690 GDesktopAppInfo *info;
1692 g_return_val_if_fail (content_type != NULL, NULL);
1694 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1697 for (l = desktop_entries; l != NULL; l = l->next)
1699 char *desktop_entry = l->data;
1701 info = g_desktop_app_info_new (desktop_entry);
1704 if (app_info_in_list (G_APP_INFO (info), infos))
1705 g_object_unref (info);
1707 infos = g_list_prepend (infos, info);
1709 g_free (desktop_entry);
1712 g_list_free (desktop_entries);
1714 return g_list_reverse (infos);
1718 * g_app_info_reset_type_associations:
1719 * content_type: a content type
1721 * Removes all changes to the type associations done by
1722 * g_app_info_set_as_default_for_type(),
1723 * g_app_info_set_as_default_for_extension(),
1724 * g_app_info_add_supports_type() of g_app_info_remove_supports_type().
1729 g_app_info_reset_type_associations (const char *content_type)
1731 update_mimeapps_list (NULL, content_type, FALSE, FALSE, FALSE, NULL);
1735 * g_app_info_get_default_for_type:
1736 * @content_type: the content type to find a #GAppInfo for
1737 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1740 * Gets the #GAppInfo that correspond to a given content type.
1742 * Returns: #GAppInfo for given @content_type or %NULL on error.
1745 g_app_info_get_default_for_type (const char *content_type,
1746 gboolean must_support_uris)
1748 GList *desktop_entries, *l;
1751 g_return_val_if_fail (content_type != NULL, NULL);
1753 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1756 for (l = desktop_entries; l != NULL; l = l->next)
1758 char *desktop_entry = l->data;
1760 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1763 if (must_support_uris && !g_app_info_supports_uris (info))
1765 g_object_unref (info);
1773 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1774 g_list_free (desktop_entries);
1780 * g_app_info_get_default_for_uri_scheme:
1781 * @uri_scheme: a string containing a URI scheme.
1783 * Gets the default application for launching applications
1784 * using this URI scheme. A URI scheme is the initial part
1785 * of the URI, up to but not including the ':', e.g. "http",
1788 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
1791 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1793 static gsize lookup = 0;
1795 if (g_once_init_enter (&lookup))
1797 gsize setup_value = 1;
1798 GDesktopAppInfoLookup *lookup_instance;
1799 const char *use_this;
1800 GIOExtensionPoint *ep;
1801 GIOExtension *extension;
1804 use_this = g_getenv ("GIO_USE_URI_ASSOCIATION");
1806 /* Ensure vfs in modules loaded */
1807 _g_io_modules_ensure_loaded ();
1809 ep = g_io_extension_point_lookup (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1811 lookup_instance = NULL;
1814 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
1816 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1819 if (lookup_instance == NULL)
1821 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1823 extension = l->data;
1824 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1825 if (lookup_instance != NULL)
1830 if (lookup_instance != NULL)
1831 setup_value = (gsize)lookup_instance;
1833 g_once_init_leave (&lookup, setup_value);
1839 return g_desktop_app_info_lookup_get_default_for_uri_scheme (G_DESKTOP_APP_INFO_LOOKUP (lookup),
1845 get_apps_from_dir (GHashTable *apps,
1846 const char *dirname,
1850 const char *basename;
1851 char *filename, *subprefix, *desktop_id;
1853 GDesktopAppInfo *appinfo;
1855 dir = g_dir_open (dirname, 0, NULL);
1858 while ((basename = g_dir_read_name (dir)) != NULL)
1860 filename = g_build_filename (dirname, basename, NULL);
1861 if (g_str_has_suffix (basename, ".desktop"))
1863 desktop_id = g_strconcat (prefix, basename, NULL);
1865 /* Use _extended so we catch NULLs too (hidden) */
1866 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1868 appinfo = g_desktop_app_info_new_from_filename (filename);
1870 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1872 g_object_unref (appinfo);
1877 if (appinfo || hidden)
1879 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1883 /* Reuse instead of strdup here */
1884 appinfo->desktop_id = desktop_id;
1889 g_free (desktop_id);
1893 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1895 subprefix = g_strconcat (prefix, basename, "-", NULL);
1896 get_apps_from_dir (apps, filename, subprefix);
1908 * g_app_info_get_all:
1910 * Gets a list of all of the applications currently registered
1913 * For desktop files, this includes applications that have
1914 * <literal>NoDisplay=true</literal> set or are excluded from
1915 * display by means of <literal>OnlyShowIn</literal> or
1916 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1917 * The returned list does not include applications which have
1918 * the <literal>Hidden</literal> key set.
1920 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1923 g_app_info_get_all (void)
1925 const char * const *dirs;
1927 GHashTableIter iter;
1932 dirs = get_applications_search_path ();
1934 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1938 for (i = 0; dirs[i] != NULL; i++)
1939 get_apps_from_dir (apps, dirs[i], "");
1943 g_hash_table_iter_init (&iter, apps);
1944 while (g_hash_table_iter_next (&iter, NULL, &value))
1947 infos = g_list_prepend (infos, value);
1950 g_hash_table_destroy (apps);
1952 return g_list_reverse (infos);
1955 /* Cacheing of mimeinfo.cache and defaults.list files */
1959 GHashTable *mime_info_cache_map;
1960 GHashTable *defaults_list_map;
1961 GHashTable *mimeapps_list_added_map;
1962 GHashTable *mimeapps_list_removed_map;
1963 time_t mime_info_cache_timestamp;
1964 time_t defaults_list_timestamp;
1965 time_t mimeapps_list_timestamp;
1969 GList *dirs; /* mimeinfo.cache and defaults.list */
1970 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1971 time_t last_stat_time;
1972 guint should_ping_mime_monitor : 1;
1975 static MimeInfoCache *mime_info_cache = NULL;
1976 G_LOCK_DEFINE_STATIC (mime_info_cache);
1978 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1979 const char *mime_type,
1980 char **new_desktop_file_ids);
1982 static MimeInfoCache * mime_info_cache_new (void);
1985 destroy_info_cache_value (gpointer key,
1989 g_list_foreach (value, (GFunc)g_free, NULL);
1990 g_list_free (value);
1994 destroy_info_cache_map (GHashTable *info_cache_map)
1996 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1997 g_hash_table_destroy (info_cache_map);
2001 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2002 const char *cache_file,
2008 filename = g_build_filename (dir->path, cache_file, NULL);
2010 if (g_stat (filename, &buf) < 0)
2017 if (buf.st_mtime != *timestamp)
2023 /* Call with lock held */
2025 remove_all (gpointer key,
2034 mime_info_cache_blow_global_cache (void)
2036 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
2041 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2045 gchar *filename, **mime_types;
2052 if (dir->mime_info_cache_map != NULL &&
2053 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2054 &dir->mime_info_cache_timestamp))
2057 if (dir->mime_info_cache_map != NULL)
2058 destroy_info_cache_map (dir->mime_info_cache_map);
2060 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2061 (GDestroyNotify) g_free,
2064 key_file = g_key_file_new ();
2066 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2068 if (g_stat (filename, &buf) < 0)
2071 if (dir->mime_info_cache_timestamp > 0)
2072 mime_info_cache->should_ping_mime_monitor = TRUE;
2074 dir->mime_info_cache_timestamp = buf.st_mtime;
2076 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2081 if (load_error != NULL)
2084 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2087 if (load_error != NULL)
2090 for (i = 0; mime_types[i] != NULL; i++)
2092 gchar **desktop_file_ids;
2093 char *unaliased_type;
2094 desktop_file_ids = g_key_file_get_string_list (key_file,
2100 if (desktop_file_ids == NULL)
2103 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2104 mime_info_cache_dir_add_desktop_entries (dir,
2107 g_free (unaliased_type);
2109 g_strfreev (desktop_file_ids);
2112 g_strfreev (mime_types);
2113 g_key_file_free (key_file);
2118 g_key_file_free (key_file);
2120 if (mime_types != NULL)
2121 g_strfreev (mime_types);
2124 g_error_free (load_error);
2128 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2132 gchar *filename, **mime_types;
2133 char *unaliased_type;
2134 char **desktop_file_ids;
2141 if (dir->defaults_list_map != NULL &&
2142 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2143 &dir->defaults_list_timestamp))
2146 if (dir->defaults_list_map != NULL)
2147 g_hash_table_destroy (dir->defaults_list_map);
2148 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2149 g_free, (GDestroyNotify)g_strfreev);
2152 key_file = g_key_file_new ();
2154 filename = g_build_filename (dir->path, "defaults.list", NULL);
2155 if (g_stat (filename, &buf) < 0)
2158 if (dir->defaults_list_timestamp > 0)
2159 mime_info_cache->should_ping_mime_monitor = TRUE;
2161 dir->defaults_list_timestamp = buf.st_mtime;
2163 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2167 if (load_error != NULL)
2170 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2172 if (mime_types != NULL)
2174 for (i = 0; mime_types[i] != NULL; i++)
2176 desktop_file_ids = g_key_file_get_string_list (key_file,
2177 DEFAULT_APPLICATIONS_GROUP,
2181 if (desktop_file_ids == NULL)
2184 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2185 g_hash_table_replace (dir->defaults_list_map,
2190 g_strfreev (mime_types);
2193 g_key_file_free (key_file);
2198 g_key_file_free (key_file);
2200 if (mime_types != NULL)
2201 g_strfreev (mime_types);
2204 g_error_free (load_error);
2208 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2212 gchar *filename, **mime_types;
2213 char *unaliased_type;
2214 char **desktop_file_ids;
2221 if (dir->mimeapps_list_added_map != NULL &&
2222 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2223 &dir->mimeapps_list_timestamp))
2226 if (dir->mimeapps_list_added_map != NULL)
2227 g_hash_table_destroy (dir->mimeapps_list_added_map);
2228 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2229 g_free, (GDestroyNotify)g_strfreev);
2231 if (dir->mimeapps_list_removed_map != NULL)
2232 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2233 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2234 g_free, (GDestroyNotify)g_strfreev);
2236 key_file = g_key_file_new ();
2238 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2239 if (g_stat (filename, &buf) < 0)
2242 if (dir->mimeapps_list_timestamp > 0)
2243 mime_info_cache->should_ping_mime_monitor = TRUE;
2245 dir->mimeapps_list_timestamp = buf.st_mtime;
2247 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2251 if (load_error != NULL)
2254 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2256 if (mime_types != NULL)
2258 for (i = 0; mime_types[i] != NULL; i++)
2260 desktop_file_ids = g_key_file_get_string_list (key_file,
2261 ADDED_ASSOCIATIONS_GROUP,
2265 if (desktop_file_ids == NULL)
2268 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2269 g_hash_table_replace (dir->mimeapps_list_added_map,
2274 g_strfreev (mime_types);
2277 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2279 if (mime_types != NULL)
2281 for (i = 0; mime_types[i] != NULL; i++)
2283 desktop_file_ids = g_key_file_get_string_list (key_file,
2284 REMOVED_ASSOCIATIONS_GROUP,
2288 if (desktop_file_ids == NULL)
2291 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2292 g_hash_table_replace (dir->mimeapps_list_removed_map,
2297 g_strfreev (mime_types);
2300 g_key_file_free (key_file);
2305 g_key_file_free (key_file);
2307 if (mime_types != NULL)
2308 g_strfreev (mime_types);
2311 g_error_free (load_error);
2314 static MimeInfoCacheDir *
2315 mime_info_cache_dir_new (const char *path)
2317 MimeInfoCacheDir *dir;
2319 dir = g_new0 (MimeInfoCacheDir, 1);
2320 dir->path = g_strdup (path);
2326 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2331 if (dir->mime_info_cache_map != NULL)
2333 destroy_info_cache_map (dir->mime_info_cache_map);
2334 dir->mime_info_cache_map = NULL;
2338 if (dir->defaults_list_map != NULL)
2340 g_hash_table_destroy (dir->defaults_list_map);
2341 dir->defaults_list_map = NULL;
2344 if (dir->mimeapps_list_added_map != NULL)
2346 g_hash_table_destroy (dir->mimeapps_list_added_map);
2347 dir->mimeapps_list_added_map = NULL;
2350 if (dir->mimeapps_list_removed_map != NULL)
2352 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2353 dir->mimeapps_list_removed_map = NULL;
2360 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2361 const char *mime_type,
2362 char **new_desktop_file_ids)
2364 GList *desktop_file_ids;
2367 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2370 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2372 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2373 desktop_file_ids = g_list_append (desktop_file_ids,
2374 g_strdup (new_desktop_file_ids[i]));
2377 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2381 mime_info_cache_init_dir_lists (void)
2383 const char * const *dirs;
2386 mime_info_cache = mime_info_cache_new ();
2388 dirs = get_applications_search_path ();
2390 for (i = 0; dirs[i] != NULL; i++)
2392 MimeInfoCacheDir *dir;
2394 dir = mime_info_cache_dir_new (dirs[i]);
2398 mime_info_cache_dir_init (dir);
2399 mime_info_cache_dir_init_defaults_list (dir);
2400 mime_info_cache_dir_init_mimeapps_list (dir);
2402 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2408 mime_info_cache_update_dir_lists (void)
2412 tmp = mime_info_cache->dirs;
2416 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2418 /* No need to do this if we had file monitors... */
2419 mime_info_cache_blow_global_cache ();
2420 mime_info_cache_dir_init (dir);
2421 mime_info_cache_dir_init_defaults_list (dir);
2422 mime_info_cache_dir_init_mimeapps_list (dir);
2429 mime_info_cache_init (void)
2431 G_LOCK (mime_info_cache);
2432 if (mime_info_cache == NULL)
2433 mime_info_cache_init_dir_lists ();
2439 if (now >= mime_info_cache->last_stat_time + 10)
2441 mime_info_cache_update_dir_lists ();
2442 mime_info_cache->last_stat_time = now;
2446 if (mime_info_cache->should_ping_mime_monitor)
2448 /* g_idle_add (emit_mime_changed, NULL); */
2449 mime_info_cache->should_ping_mime_monitor = FALSE;
2452 G_UNLOCK (mime_info_cache);
2455 static MimeInfoCache *
2456 mime_info_cache_new (void)
2458 MimeInfoCache *cache;
2460 cache = g_new0 (MimeInfoCache, 1);
2462 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2463 (GDestroyNotify) g_free,
2464 (GDestroyNotify) g_free);
2469 mime_info_cache_free (MimeInfoCache *cache)
2474 g_list_foreach (cache->dirs,
2475 (GFunc) mime_info_cache_dir_free,
2477 g_list_free (cache->dirs);
2478 g_hash_table_destroy (cache->global_defaults_cache);
2483 * mime_info_cache_reload:
2484 * @dir: directory path which needs reloading.
2486 * Reload the mime information for the @dir.
2489 mime_info_cache_reload (const char *dir)
2491 /* FIXME: just reload the dir that needs reloading,
2492 * don't blow the whole cache
2494 if (mime_info_cache != NULL)
2496 G_LOCK (mime_info_cache);
2497 mime_info_cache_free (mime_info_cache);
2498 mime_info_cache = NULL;
2499 G_UNLOCK (mime_info_cache);
2504 append_desktop_entry (GList *list,
2505 const char *desktop_entry,
2506 GList *removed_entries)
2508 /* Add if not already in list, and valid */
2509 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2510 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2511 list = g_list_prepend (list, g_strdup (desktop_entry));
2517 * get_all_desktop_entries_for_mime_type:
2518 * @mime_type: a mime type.
2520 * Returns all the desktop ids for @mime_type. The desktop files
2521 * are listed in an order so that default applications are listed before
2522 * non-default ones, and handlers for inherited mimetypes are listed
2523 * after the base ones.
2525 * Return value: a #GList containing the desktop ids which claim
2526 * to handle @mime_type.
2529 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2531 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2532 MimeInfoCacheDir *dir;
2535 char **default_entries;
2536 char **removed_associations;
2541 mime_info_cache_init ();
2543 /* collect all ancestors */
2544 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2545 array = g_ptr_array_new ();
2546 for (i = 0; mime_types[i]; i++)
2547 g_ptr_array_add (array, mime_types[i]);
2548 g_free (mime_types);
2549 for (i = 0; i < array->len; i++)
2551 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
2552 for (j = 0; anc[j]; j++)
2554 for (k = 0; k < array->len; k++)
2556 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
2559 if (k == array->len) /* not found */
2560 g_ptr_array_add (array, g_strdup (anc[j]));
2564 g_ptr_array_add (array, NULL);
2565 mime_types = (char **)g_ptr_array_free (array, FALSE);
2567 G_LOCK (mime_info_cache);
2569 removed_entries = NULL;
2570 desktop_entries = NULL;
2571 for (i = 0; mime_types[i] != NULL; i++)
2573 mime_type = mime_types[i];
2575 /* Go through all apps listed as defaults */
2576 for (dir_list = mime_info_cache->dirs;
2578 dir_list = dir_list->next)
2580 dir = dir_list->data;
2582 /* First added associations from mimeapps.list */
2583 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
2584 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2585 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2587 /* Then removed associations from mimeapps.list */
2588 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
2589 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2590 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2592 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
2593 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2594 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2595 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2598 /* Go through all entries that support the mimetype */
2599 for (dir_list = mime_info_cache->dirs;
2601 dir_list = dir_list->next)
2603 dir = dir_list->data;
2605 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2606 for (tmp = list; tmp != NULL; tmp = tmp->next)
2607 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2611 G_UNLOCK (mime_info_cache);
2613 g_strfreev (mime_types);
2615 g_list_free (removed_entries);
2617 desktop_entries = g_list_reverse (desktop_entries);
2619 return desktop_entries;
2622 /* GDesktopAppInfoLookup interface: */
2624 static void g_desktop_app_info_lookup_base_init (gpointer g_class);
2625 static void g_desktop_app_info_lookup_class_init (gpointer g_class,
2626 gpointer class_data);
2629 g_desktop_app_info_lookup_get_type (void)
2631 static volatile gsize g_define_type_id__volatile = 0;
2633 if (g_once_init_enter (&g_define_type_id__volatile))
2635 const GTypeInfo desktop_app_info_lookup_info =
2637 sizeof (GDesktopAppInfoLookupIface), /* class_size */
2638 g_desktop_app_info_lookup_base_init, /* base_init */
2639 NULL, /* base_finalize */
2640 g_desktop_app_info_lookup_class_init,
2641 NULL, /* class_finalize */
2642 NULL, /* class_data */
2644 0, /* n_preallocs */
2647 GType g_define_type_id =
2648 g_type_register_static (G_TYPE_INTERFACE, I_("GDesktopAppInfoLookup"),
2649 &desktop_app_info_lookup_info, 0);
2651 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
2653 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
2656 return g_define_type_id__volatile;
2660 g_desktop_app_info_lookup_class_init (gpointer g_class,
2661 gpointer class_data)
2666 g_desktop_app_info_lookup_base_init (gpointer g_class)
2671 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
2672 * @lookup: a #GDesktopAppInfoLookup
2673 * @uri_scheme: a string containing a URI scheme.
2675 * Gets the default application for launching applications
2676 * using this URI scheme for a particular GDesktopAppInfoLookup
2679 * The GDesktopAppInfoLookup interface and this function is used
2680 * to implement g_app_info_get_default_for_uri_scheme() backends
2681 * in a GIO module. There is no reason for applications to use it
2682 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
2684 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
2687 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
2688 const char *uri_scheme)
2690 GDesktopAppInfoLookupIface *iface;
2692 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
2694 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
2696 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
2699 #define __G_DESKTOP_APP_INFO_C__
2700 #include "gioaliasdef.c"