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"
38 #include "gthemedicon.h"
39 #include "gfileicon.h"
40 #include <glib/gstdio.h>
42 #include "giomodule-priv.h"
47 * SECTION:gdesktopappinfo
48 * @short_description: Application information from desktop files
49 * @include: gio/gdesktopappinfo.h
51 * #GDesktopAppInfo is an implementation of #GAppInfo based on
56 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
57 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
58 #define MIME_CACHE_GROUP "MIME Cache"
60 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
61 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type);
62 static void mime_info_cache_reload (const char *dir);
63 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
69 * Information about an installed application from a desktop file.
71 struct _GDesktopAppInfo
73 GObject parent_instance;
79 /* FIXME: what about GenericName ? */
93 guint startup_notify : 1;
94 /* FIXME: what about StartupWMClass ? */
97 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
98 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
99 g_desktop_app_info_iface_init))
102 search_path_init (gpointer data)
105 const char * const *data_dirs;
106 const char *user_data_dir;
109 data_dirs = g_get_system_data_dirs ();
110 length = g_strv_length ((char **) data_dirs);
112 args = g_new (char *, length + 2);
115 user_data_dir = g_get_user_data_dir ();
116 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
117 for (i = 0; i < length; i++)
118 args[j++] = g_build_filename (data_dirs[i],
119 "applications", NULL);
125 static const char * const *
126 get_applications_search_path (void)
128 static GOnce once_init = G_ONCE_INIT;
129 return g_once (&once_init, search_path_init, NULL);
133 g_desktop_app_info_finalize (GObject *object)
135 GDesktopAppInfo *info;
137 info = G_DESKTOP_APP_INFO (object);
139 g_free (info->desktop_id);
140 g_free (info->filename);
142 g_free (info->comment);
143 g_free (info->icon_name);
145 g_object_unref (info->icon);
146 g_strfreev (info->only_show_in);
147 g_strfreev (info->not_show_in);
148 g_free (info->try_exec);
150 g_free (info->binary);
153 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
157 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
159 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
161 gobject_class->finalize = g_desktop_app_info_finalize;
165 g_desktop_app_info_init (GDesktopAppInfo *local)
170 binary_from_exec (const char *exec)
178 while (*p != ' ' && *p != 0)
181 return g_strndup (start, p - start);
186 * g_desktop_app_info_new_from_filename:
187 * @filename: a string containing a file name.
189 * Creates a new #GDesktopAppInfo.
191 * Returns: a new #GDesktopAppInfo or %NULL on error.
194 g_desktop_app_info_new_from_filename (const char *filename)
196 GDesktopAppInfo *info;
202 key_file = g_key_file_new ();
204 if (!g_key_file_load_from_file (key_file,
209 g_key_file_free (key_file);
213 start_group = g_key_file_get_start_group (key_file);
214 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
216 g_free (start_group);
217 g_key_file_free (key_file);
220 g_free (start_group);
222 type = g_key_file_get_string (key_file,
223 G_KEY_FILE_DESKTOP_GROUP,
224 G_KEY_FILE_DESKTOP_KEY_TYPE,
226 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
229 g_key_file_free (key_file);
234 try_exec = g_key_file_get_string (key_file,
235 G_KEY_FILE_DESKTOP_GROUP,
236 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
241 t = g_find_program_in_path (try_exec);
245 g_key_file_free (key_file);
251 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
252 info->filename = g_strdup (filename);
254 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
255 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
256 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
257 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
258 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);
259 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);
260 info->try_exec = try_exec;
261 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
262 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
263 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
264 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
265 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
267 g_key_file_free (key_file);
272 if (g_path_is_absolute (info->icon_name))
276 file = g_file_new_for_path (info->icon_name);
277 info->icon = g_file_icon_new (file);
278 g_object_unref (file);
281 info->icon = g_themed_icon_new (info->icon_name);
285 info->binary = binary_from_exec (info->exec);
291 * g_desktop_app_info_new:
292 * @desktop_id: the desktop file id
294 * Creates a new #GDesktopAppInfo.
296 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
299 g_desktop_app_info_new (const char *desktop_id)
301 GDesktopAppInfo *appinfo;
302 const char * const *dirs;
306 dirs = get_applications_search_path ();
308 basename = g_strdup (desktop_id);
310 for (i = 0; dirs[i] != NULL; i++)
315 filename = g_build_filename (dirs[i], desktop_id, NULL);
316 appinfo = g_desktop_app_info_new_from_filename (filename);
322 while ((p = strchr (p, '-')) != NULL)
326 filename = g_build_filename (dirs[i], basename, NULL);
327 appinfo = g_desktop_app_info_new_from_filename (filename);
342 appinfo->desktop_id = g_strdup (desktop_id);
344 if (g_desktop_app_info_get_is_hidden (appinfo))
346 g_object_unref (appinfo);
354 g_desktop_app_info_dup (GAppInfo *appinfo)
356 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
357 GDesktopAppInfo *new_info;
359 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
361 new_info->filename = g_strdup (info->filename);
362 new_info->desktop_id = g_strdup (info->desktop_id);
364 new_info->name = g_strdup (info->name);
365 new_info->comment = g_strdup (info->comment);
366 new_info->nodisplay = info->nodisplay;
367 new_info->icon_name = g_strdup (info->icon_name);
368 new_info->icon = g_object_ref (info->icon);
369 new_info->only_show_in = g_strdupv (info->only_show_in);
370 new_info->not_show_in = g_strdupv (info->not_show_in);
371 new_info->try_exec = g_strdup (info->try_exec);
372 new_info->exec = g_strdup (info->exec);
373 new_info->binary = g_strdup (info->binary);
374 new_info->path = g_strdup (info->path);
375 new_info->hidden = info->hidden;
376 new_info->terminal = info->terminal;
377 new_info->startup_notify = info->startup_notify;
379 return G_APP_INFO (new_info);
383 g_desktop_app_info_equal (GAppInfo *appinfo1,
386 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
387 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
389 if (info1->desktop_id == NULL ||
390 info2->desktop_id == NULL)
393 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
397 g_desktop_app_info_get_id (GAppInfo *appinfo)
399 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
401 return info->desktop_id;
405 g_desktop_app_info_get_name (GAppInfo *appinfo)
407 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
409 if (info->name == NULL)
415 * g_desktop_app_info_get_is_hidden:
416 * @info: a #GDesktopAppInfo.
418 * A desktop file is hidden if the Hidden key in it is
421 * Returns: %TRUE if hidden, %FALSE otherwise.
424 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
430 g_desktop_app_info_get_description (GAppInfo *appinfo)
432 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
434 return info->comment;
438 g_desktop_app_info_get_executable (GAppInfo *appinfo)
440 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
446 g_desktop_app_info_get_icon (GAppInfo *appinfo)
448 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
454 expand_macro_single (char macro, GFile *file)
459 path = g_file_get_path (file);
460 uri = g_file_get_uri (file);
466 result = g_shell_quote (uri);
471 result = g_shell_quote (path);
476 result = g_shell_quote (g_path_get_dirname (path));
481 result = g_shell_quote (g_path_get_basename (path));
492 expand_macro (char macro,
494 GDesktopAppInfo *info,
497 GList *files = *file_list;
500 g_return_if_fail (exec != NULL);
510 expanded = expand_macro_single (macro, files->data);
513 g_string_append (exec, expanded);
527 expanded = expand_macro_single (macro, files->data);
530 g_string_append (exec, expanded);
536 if (files != NULL && expanded)
537 g_string_append_c (exec, ' ');
545 g_string_append (exec, "--icon ");
546 g_string_append (exec, info->icon_name);
552 g_string_append (exec, info->name);
557 g_string_append (exec, info->filename);
560 case 'm': /* deprecated */
564 g_string_append_c (exec, '%');
572 expand_application_parameters (GDesktopAppInfo *info,
578 GList *file_list = *files;
579 const char *p = info->exec;
580 GString *expanded_exec = g_string_new (NULL);
583 if (info->exec == NULL)
585 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
586 _("Desktop file didn't specify Exec field"));
592 if (p[0] == '%' && p[1] != '\0')
594 expand_macro (p[1], expanded_exec, info, files);
598 g_string_append_c (expanded_exec, *p);
603 /* No file substitutions */
604 if (file_list == *files && file_list != NULL)
606 /* If there is no macro default to %f. This is also what KDE does */
607 g_string_append_c (expanded_exec, ' ');
608 expand_macro ('f', expanded_exec, info, files);
611 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
612 g_string_free (expanded_exec, TRUE);
617 prepend_terminal_to_vector (int *argc,
624 char **term_argv = NULL;
629 g_return_val_if_fail (argc != NULL, FALSE);
630 g_return_val_if_fail (argv != NULL, FALSE);
638 /* compute size if not given */
641 for (i = 0; the_argv[i] != NULL; i++)
647 term_argv = g_new0 (char *, 3);
649 check = g_find_program_in_path ("gnome-terminal");
652 term_argv[0] = check;
653 /* Note that gnome-terminal takes -x and
654 * as -e in gnome-terminal is broken we use that. */
655 term_argv[1] = g_strdup ("-x");
660 check = g_find_program_in_path ("nxterm");
662 check = g_find_program_in_path ("color-xterm");
664 check = g_find_program_in_path ("rxvt");
666 check = g_find_program_in_path ("xterm");
668 check = g_find_program_in_path ("dtterm");
671 check = g_strdup ("xterm");
672 g_warning ("couldn't find a terminal, falling back to xterm");
674 term_argv[0] = check;
675 term_argv[1] = g_strdup ("-e");
678 real_argc = term_argc + *argc;
679 real_argv = g_new (char *, real_argc + 1);
681 for (i = 0; i < term_argc; i++)
682 real_argv[i] = term_argv[i];
684 for (j = 0; j < *argc; j++, i++)
685 real_argv[i] = (char *)the_argv[j];
693 /* we use g_free here as we sucked all the inner strings
694 * out from it into real_argv */
699 #endif /* G_OS_WIN32 */
702 /* '=' is the new '\0'.
703 * DO NOT CALL unless at least one string ends with '='
706 is_env (const char *a,
711 if (*a == 0 || *b == 0)
724 /* free with g_strfreev */
726 replace_env_var (char **old_environ,
728 const char *new_value)
730 int length, new_length;
731 int index, new_index;
735 /* do two things at once:
736 * - discover the length of the environment ('length')
737 * - find the location (if any) of the env var ('index')
740 for (length = 0; old_environ[length]; length++)
742 /* if we already have it in our environment, replace */
743 if (is_env (old_environ[length], env_var))
748 /* no current env var, no desired env value.
751 if (new_value == NULL && index == -1)
754 /* in all cases now, we will be using a modified environment.
755 * determine its length and allocated it.
758 * new_index = location to insert, if any
759 * new_length = length of the new array
760 * new_environ = the pointer array for the new environment
763 if (new_value == NULL && index >= 0)
765 /* in this case, we will be removing an entry */
766 new_length = length - 1;
769 else if (new_value != NULL && index < 0)
771 /* in this case, we will be adding an entry to the end */
772 new_length = length + 1;
776 /* in this case, we will be replacing the existing entry */
782 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
783 new_environ[new_length] = NULL;
785 /* now we do the copying.
786 * for each entry in the new environment, we decide what to do
790 for (new_i = 0; new_i < new_length; new_i++)
792 if (new_i == new_index)
794 /* insert our new item */
795 new_environ[new_i] = g_strconcat (env_var,
800 /* if we had an old entry, skip it now */
806 /* if this is the old DESKTOP_STARTUP_ID, skip it */
810 /* copy an old item */
811 new_environ[new_i] = g_strdup (old_environ[i]);
816 g_strfreev (old_environ);
822 dup_list_segment (GList *start,
828 while (start != NULL && start != end)
830 res = g_list_prepend (res, start->data);
834 return g_list_reverse (res);
837 #ifdef HAVE__NSGETENVIRON
838 #define environ (*_NSGetEnviron())
839 #elif !defined(G_OS_WIN32)
841 /* According to the Single Unix Specification, environ is not in
842 * * any system header, although unistd.h often declares it.
844 extern char **environ;
848 g_desktop_app_info_launch (GAppInfo *appinfo,
850 GAppLaunchContext *launch_context,
853 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
854 gboolean completed = FALSE;
856 GList *launched_files;
863 g_return_val_if_fail (appinfo != NULL, FALSE);
871 if (!expand_application_parameters (info, &files,
872 &argc, &argv, error))
875 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
877 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
878 _("Unable to find terminal required for application"));
885 launched_files = dup_list_segment (old_files, files);
887 display = g_app_launch_context_get_display (launch_context,
892 if (info->startup_notify)
893 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
897 if (display || sn_id)
901 envp = g_new0 (char *, 1);
903 envp = g_strdupv (environ);
907 envp = replace_env_var (envp,
912 envp = replace_env_var (envp,
913 "DESKTOP_STARTUP_ID",
919 g_list_free (launched_files);
922 if (!g_spawn_async (info->path, /* working directory */
925 G_SPAWN_SEARCH_PATH /* flags */,
926 NULL /* child_setup */,
928 NULL /* child_pid */,
933 g_app_launch_context_launch_failed (launch_context, sn_id);
947 while (files != NULL);
959 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
961 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
964 ((strstr (info->exec, "%u") != NULL) ||
965 (strstr (info->exec, "%U") != NULL));
969 g_desktop_app_info_supports_files (GAppInfo *appinfo)
971 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
974 ((strstr (info->exec, "%f") != NULL) ||
975 (strstr (info->exec, "%F") != NULL));
979 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
981 GAppLaunchContext *launch_context,
991 file = g_file_new_for_uri (uris->data);
993 g_warning ("Invalid uri passed to g_desktop_app_info_launch_uris");
996 files = g_list_prepend (files, file);
999 files = g_list_reverse (files);
1001 res = g_desktop_app_info_launch (appinfo, files, launch_context, error);
1003 g_list_foreach (files, (GFunc)g_object_unref, NULL);
1004 g_list_free (files);
1009 G_LOCK_DEFINE_STATIC (g_desktop_env);
1010 static gchar *g_desktop_env = NULL;
1013 * g_desktop_app_info_set_desktop_env:
1014 * @desktop_env: a string specifying what desktop this is
1016 * Sets the name of the desktop that the application is running in.
1017 * This is used by g_app_info_should_show() to evaluate the
1018 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1019 * desktop entry fields.
1021 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1022 * Menu specification</ulink> recognizes the following:
1024 * <member>GNOME</member>
1025 * <member>KDE</member>
1026 * <member>ROX</member>
1027 * <member>XFCE</member>
1028 * <member>Old</member>
1031 * Should be called only once; subsequent calls are ignored.
1034 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1036 G_LOCK (g_desktop_env);
1038 g_desktop_env = g_strdup (desktop_env);
1039 G_UNLOCK (g_desktop_env);
1043 g_desktop_app_info_should_show (GAppInfo *appinfo)
1045 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1047 const gchar *desktop_env;
1050 if (info->nodisplay)
1053 G_LOCK (g_desktop_env);
1054 desktop_env = g_desktop_env;
1055 G_UNLOCK (g_desktop_env);
1057 if (info->only_show_in)
1059 if (desktop_env == NULL)
1063 for (i = 0; info->only_show_in[i] != NULL; i++)
1065 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1075 if (info->not_show_in && desktop_env)
1077 for (i = 0; info->not_show_in[i] != NULL; i++)
1079 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1093 ensure_dir (DirType type,
1096 char *path, *display_name;
1099 if (type == APP_DIR)
1100 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1102 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1105 if (g_mkdir_with_parents (path, 0700) == 0)
1109 display_name = g_filename_display_name (path);
1110 if (type == APP_DIR)
1111 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1112 _("Can't create user application configuration folder %s: %s"),
1113 display_name, g_strerror (err));
1115 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1116 _("Can't create user MIME configuration folder %s: %s"),
1117 display_name, g_strerror (err));
1119 g_free (display_name);
1126 update_default_list (const char *desktop_id,
1127 const char *content_type,
1128 gboolean add_at_start,
1129 gboolean add_at_end,
1133 char *dirname, *filename;
1135 gboolean load_succeeded, res;
1138 gsize length, data_size;
1142 /* Don't add both at start and end */
1143 g_assert (!(add_at_start && add_at_end));
1145 dirname = ensure_dir (APP_DIR, error);
1149 filename = g_build_filename (dirname, "defaults.list", NULL);
1152 key_file = g_key_file_new ();
1153 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1154 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1156 g_key_file_free (key_file);
1157 key_file = g_key_file_new ();
1160 /* Add to the right place in the list */
1163 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1164 content_type, &length, NULL);
1166 list = g_new (char *, 1 + length + 1);
1170 list[i++] = g_strdup (desktop_id);
1173 for (j = 0; old_list[j] != NULL; j++)
1175 if (strcmp (old_list[j], desktop_id) != 0)
1176 list[i++] = g_strdup (old_list[j]);
1180 list[i++] = g_strdup (desktop_id);
1183 g_strfreev (old_list);
1185 g_key_file_set_string_list (key_file,
1186 DEFAULT_APPLICATIONS_GROUP,
1188 (const char * const *)list, i);
1192 /* Remove from removed associations group (unless remove) */
1195 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1196 content_type, &length, NULL);
1198 list = g_new (char *, 1 + length + 1);
1202 list[i++] = g_strdup (desktop_id);
1205 for (j = 0; old_list[j] != NULL; j++)
1207 if (strcmp (old_list[j], desktop_id) != 0)
1208 list[i++] = g_strdup (old_list[j]);
1213 g_strfreev (old_list);
1215 if (list[0] == NULL)
1216 g_key_file_remove_key (key_file,
1217 REMOVED_ASSOCIATIONS_GROUP,
1221 g_key_file_set_string_list (key_file,
1222 REMOVED_ASSOCIATIONS_GROUP,
1224 (const char * const *)list, i);
1229 data = g_key_file_to_data (key_file, &data_size, error);
1230 g_key_file_free (key_file);
1232 res = g_file_set_contents (filename, data, data_size, error);
1234 mime_info_cache_reload (NULL);
1243 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1244 const char *content_type,
1247 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1249 if (!g_desktop_app_info_ensure_saved (info, error))
1252 return update_default_list (info->desktop_id, content_type, TRUE, FALSE, FALSE, error);
1256 update_program_done (GPid pid,
1260 /* Did the application exit correctly */
1261 if (WIFEXITED (status) &&
1262 WEXITSTATUS (status) == 0)
1264 /* Here we could clean out any caches in use */
1269 run_update_command (char *command,
1278 GError *error = NULL;
1281 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1283 if (g_spawn_async ("/", argv,
1285 G_SPAWN_SEARCH_PATH |
1286 G_SPAWN_STDOUT_TO_DEV_NULL |
1287 G_SPAWN_STDERR_TO_DEV_NULL |
1288 G_SPAWN_DO_NOT_REAP_CHILD,
1289 NULL, NULL, /* No setup function */
1292 g_child_watch_add (pid, update_program_done, NULL);
1295 /* If we get an error at this point, it's quite likely the user doesn't
1296 * have an installed copy of either 'update-mime-database' or
1297 * 'update-desktop-database'. I don't think we want to popup an error
1298 * dialog at this point, so we just do a g_warning to give the user a
1299 * chance of debugging it.
1301 g_warning ("%s", error->message);
1308 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1309 const char *extension,
1312 char *filename, *basename, *mimetype;
1316 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1319 dirname = ensure_dir (MIMETYPE_DIR, error);
1323 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1324 filename = g_build_filename (dirname, basename, NULL);
1328 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1330 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1335 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1336 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1337 " <mime-type type=\"%s\">\n"
1338 " <comment>%s document</comment>\n"
1339 " <glob pattern=\"*.%s\"/>\n"
1341 "</mime-info>\n", mimetype, extension, extension);
1343 g_file_set_contents (filename, contents, -1, NULL);
1346 run_update_command ("update-mime-database", "mime");
1350 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1360 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1361 const char *content_type,
1364 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1366 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1369 return update_default_list (info->desktop_id, content_type, FALSE, TRUE, FALSE, error);
1373 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1379 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1380 const char *content_type,
1383 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1385 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1388 return update_default_list (info->desktop_id, content_type, FALSE, FALSE, TRUE, error);
1392 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1397 char *basename, *filename;
1398 char *data, *desktop_id;
1403 if (info->filename != NULL)
1406 /* This is only used for object created with
1407 * g_app_info_create_from_commandline. All other
1408 * object should have a filename
1411 dirname = ensure_dir (APP_DIR, error);
1415 key_file = g_key_file_new ();
1417 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1418 "Encoding", "UTF-8");
1419 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1420 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1421 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1422 G_KEY_FILE_DESKTOP_KEY_TYPE,
1423 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1425 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1426 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1428 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1429 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1431 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1432 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1434 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1435 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1437 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1438 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1440 data = g_key_file_to_data (key_file, &data_size, NULL);
1441 g_key_file_free (key_file);
1443 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1444 filename = g_build_filename (dirname, desktop_id, NULL);
1445 g_free (desktop_id);
1448 fd = g_mkstemp (filename);
1453 display_name = g_filename_display_name (filename);
1454 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1455 _("Can't create user desktop file %s"), display_name);
1456 g_free (display_name);
1462 desktop_id = g_path_get_basename (filename);
1466 res = g_file_set_contents (filename, data, data_size, error);
1469 g_free (desktop_id);
1474 info->filename = filename;
1475 info->desktop_id = desktop_id;
1477 run_update_command ("update-desktop-database", "applications");
1483 * g_app_info_create_from_commandline:
1484 * @commandline: the commandline to use
1485 * @application_name: the application name, or %NULL to use @commandline
1486 * @flags: flags that can specify details of the created #GAppInfo
1487 * @error: a #GError location to store the error occuring, %NULL to ignore.
1489 * Creates a new #GAppInfo from the given information.
1491 * Returns: new #GAppInfo for given command.
1494 g_app_info_create_from_commandline (const char *commandline,
1495 const char *application_name,
1496 GAppInfoCreateFlags flags,
1501 GDesktopAppInfo *info;
1503 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1505 info->filename = NULL;
1506 info->desktop_id = NULL;
1508 info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
1509 info->startup_notify = FALSE;
1510 info->hidden = FALSE;
1511 if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
1512 info->exec = g_strconcat (commandline, " %u", NULL);
1514 info->exec = g_strconcat (commandline, " %f", NULL);
1515 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
1516 info->nodisplay = TRUE;
1517 info->binary = binary_from_exec (info->exec);
1519 if (application_name)
1520 info->name = g_strdup (application_name);
1523 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1524 split = g_strsplit (commandline, " ", 2);
1525 basename = g_path_get_basename (split[0]);
1527 info->name = basename;
1528 if (info->name == NULL)
1529 info->name = g_strdup ("custom");
1532 return G_APP_INFO (info);
1536 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1538 iface->dup = g_desktop_app_info_dup;
1539 iface->equal = g_desktop_app_info_equal;
1540 iface->get_id = g_desktop_app_info_get_id;
1541 iface->get_name = g_desktop_app_info_get_name;
1542 iface->get_description = g_desktop_app_info_get_description;
1543 iface->get_executable = g_desktop_app_info_get_executable;
1544 iface->get_icon = g_desktop_app_info_get_icon;
1545 iface->launch = g_desktop_app_info_launch;
1546 iface->supports_uris = g_desktop_app_info_supports_uris;
1547 iface->supports_files = g_desktop_app_info_supports_files;
1548 iface->launch_uris = g_desktop_app_info_launch_uris;
1549 iface->should_show = g_desktop_app_info_should_show;
1550 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1551 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1552 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1553 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1554 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1558 app_info_in_list (GAppInfo *info,
1561 while (list != NULL)
1563 if (g_app_info_equal (info, list->data))
1572 * g_app_info_get_all_for_type:
1573 * @content_type: the content type to find a #GAppInfo for
1575 * Gets a list of all #GAppInfo s for a given content type.
1577 * Returns: #GList of #GAppInfo s for given @content_type
1578 * or %NULL on error.
1581 g_app_info_get_all_for_type (const char *content_type)
1583 GList *desktop_entries, *l;
1585 GDesktopAppInfo *info;
1587 g_return_val_if_fail (content_type != NULL, NULL);
1589 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1592 for (l = desktop_entries; l != NULL; l = l->next)
1594 char *desktop_entry = l->data;
1596 info = g_desktop_app_info_new (desktop_entry);
1599 if (app_info_in_list (G_APP_INFO (info), infos))
1600 g_object_unref (info);
1602 infos = g_list_prepend (infos, info);
1604 g_free (desktop_entry);
1607 g_list_free (desktop_entries);
1609 return g_list_reverse (infos);
1614 * g_app_info_get_default_for_type:
1615 * @content_type: the content type to find a #GAppInfo for
1616 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1619 * Gets the #GAppInfo that correspond to a given content type.
1621 * Returns: #GAppInfo for given @content_type or %NULL on error.
1624 g_app_info_get_default_for_type (const char *content_type,
1625 gboolean must_support_uris)
1627 GList *desktop_entries, *l;
1630 g_return_val_if_fail (content_type != NULL, NULL);
1632 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1635 for (l = desktop_entries; l != NULL; l = l->next)
1637 char *desktop_entry = l->data;
1639 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1642 if (must_support_uris && !g_app_info_supports_uris (info))
1644 g_object_unref (info);
1652 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1653 g_list_free (desktop_entries);
1659 * g_app_info_get_default_for_uri_scheme:
1660 * @uri_scheme: a string containing a URI scheme.
1662 * Gets the default application for launching applications
1663 * using this URI scheme.
1665 * TODO: This is currently unimplemented.
1670 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1672 static gsize lookup = 0;
1674 if (g_once_init_enter (&lookup))
1676 gsize setup_value = 1;
1677 GDesktopAppInfoLookup *lookup_instance;
1678 const char *use_this;
1679 GIOExtensionPoint *ep;
1680 GIOExtension *extension;
1683 use_this = g_getenv ("GIO_USE_URI_ASSOCIATION");
1685 /* Ensure vfs in modules loaded */
1686 _g_io_modules_ensure_loaded ();
1688 ep = g_io_extension_point_lookup (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1690 lookup_instance = NULL;
1693 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
1695 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1698 if (lookup_instance == NULL)
1700 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1702 extension = l->data;
1703 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1704 if (lookup_instance != NULL)
1709 if (lookup_instance != NULL)
1710 setup_value = (gsize)lookup_instance;
1712 g_once_init_leave (&lookup, setup_value);
1718 return g_desktop_app_info_lookup_get_default_for_uri_scheme (G_DESKTOP_APP_INFO_LOOKUP (lookup),
1724 get_apps_from_dir (GHashTable *apps,
1725 const char *dirname,
1729 const char *basename;
1730 char *filename, *subprefix, *desktop_id;
1732 GDesktopAppInfo *appinfo;
1734 dir = g_dir_open (dirname, 0, NULL);
1737 while ((basename = g_dir_read_name (dir)) != NULL)
1739 filename = g_build_filename (dirname, basename, NULL);
1740 if (g_str_has_suffix (basename, ".desktop"))
1742 desktop_id = g_strconcat (prefix, basename, NULL);
1744 /* Use _extended so we catch NULLs too (hidden) */
1745 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1747 appinfo = g_desktop_app_info_new_from_filename (filename);
1749 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1751 g_object_unref (appinfo);
1756 if (appinfo || hidden)
1758 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1762 /* Reuse instead of strdup here */
1763 appinfo->desktop_id = desktop_id;
1768 g_free (desktop_id);
1772 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1774 subprefix = g_strconcat (prefix, basename, "-", NULL);
1775 get_apps_from_dir (apps, filename, subprefix);
1787 * g_app_info_get_all:
1789 * Gets a list of all of the applications currently registered
1792 * For desktop files, this includes applications that have
1793 * <literal>NoDisplay=true</literal> set or are excluded from
1794 * display by means of <literal>OnlyShowIn</literal> or
1795 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1796 * The returned list does not include applications which have
1797 * the <literal>Hidden</literal> key set.
1799 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1802 g_app_info_get_all (void)
1804 const char * const *dirs;
1806 GHashTableIter iter;
1811 dirs = get_applications_search_path ();
1813 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1817 for (i = 0; dirs[i] != NULL; i++)
1818 get_apps_from_dir (apps, dirs[i], "");
1822 g_hash_table_iter_init (&iter, apps);
1823 while (g_hash_table_iter_next (&iter, NULL, &value))
1826 infos = g_list_prepend (infos, value);
1829 g_hash_table_destroy (apps);
1831 return g_list_reverse (infos);
1834 /* Cacheing of mimeinfo.cache and defaults.list files */
1838 GHashTable *mime_info_cache_map;
1839 GHashTable *defaults_list_map;
1840 GHashTable *defaults_list_removed_map;
1841 time_t mime_info_cache_timestamp;
1842 time_t defaults_list_timestamp;
1846 GList *dirs; /* mimeinfo.cache and defaults.list */
1847 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1848 time_t last_stat_time;
1849 guint should_ping_mime_monitor : 1;
1852 static MimeInfoCache *mime_info_cache = NULL;
1853 G_LOCK_DEFINE_STATIC (mime_info_cache);
1855 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1856 const char *mime_type,
1857 char **new_desktop_file_ids);
1859 static MimeInfoCache * mime_info_cache_new (void);
1862 destroy_info_cache_value (gpointer key,
1866 g_list_foreach (value, (GFunc)g_free, NULL);
1867 g_list_free (value);
1871 destroy_info_cache_map (GHashTable *info_cache_map)
1873 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1874 g_hash_table_destroy (info_cache_map);
1878 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1879 const char *cache_file,
1885 filename = g_build_filename (dir->path, cache_file, NULL);
1887 if (g_stat (filename, &buf) < 0)
1894 if (buf.st_mtime != *timestamp)
1900 /* Call with lock held */
1902 remove_all (gpointer key,
1911 mime_info_cache_blow_global_cache (void)
1913 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1918 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1922 gchar *filename, **mime_types;
1929 if (dir->mime_info_cache_map != NULL &&
1930 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1931 &dir->mime_info_cache_timestamp))
1934 if (dir->mime_info_cache_map != NULL)
1935 destroy_info_cache_map (dir->mime_info_cache_map);
1937 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1938 (GDestroyNotify) g_free,
1941 key_file = g_key_file_new ();
1943 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1945 if (g_stat (filename, &buf) < 0)
1948 if (dir->mime_info_cache_timestamp > 0)
1949 mime_info_cache->should_ping_mime_monitor = TRUE;
1951 dir->mime_info_cache_timestamp = buf.st_mtime;
1953 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1958 if (load_error != NULL)
1961 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1964 if (load_error != NULL)
1967 for (i = 0; mime_types[i] != NULL; i++)
1969 gchar **desktop_file_ids;
1970 char *unaliased_type;
1971 desktop_file_ids = g_key_file_get_string_list (key_file,
1977 if (desktop_file_ids == NULL)
1980 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1981 mime_info_cache_dir_add_desktop_entries (dir,
1984 g_free (unaliased_type);
1986 g_strfreev (desktop_file_ids);
1989 g_strfreev (mime_types);
1990 g_key_file_free (key_file);
1995 g_key_file_free (key_file);
1997 if (mime_types != NULL)
1998 g_strfreev (mime_types);
2001 g_error_free (load_error);
2005 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2009 gchar *filename, **mime_types;
2010 char *unaliased_type;
2011 char **desktop_file_ids;
2018 if (dir->defaults_list_map != NULL &&
2019 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2020 &dir->defaults_list_timestamp))
2023 if (dir->defaults_list_map != NULL)
2024 g_hash_table_destroy (dir->defaults_list_map);
2025 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2026 g_free, (GDestroyNotify)g_strfreev);
2028 if (dir->defaults_list_removed_map != NULL)
2029 g_hash_table_destroy (dir->defaults_list_removed_map);
2030 dir->defaults_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2031 g_free, (GDestroyNotify)g_strfreev);
2033 key_file = g_key_file_new ();
2035 filename = g_build_filename (dir->path, "defaults.list", NULL);
2036 if (g_stat (filename, &buf) < 0)
2039 if (dir->defaults_list_timestamp > 0)
2040 mime_info_cache->should_ping_mime_monitor = TRUE;
2042 dir->defaults_list_timestamp = buf.st_mtime;
2044 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2048 if (load_error != NULL)
2051 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2053 if (mime_types != NULL)
2055 for (i = 0; mime_types[i] != NULL; i++)
2057 desktop_file_ids = g_key_file_get_string_list (key_file,
2058 DEFAULT_APPLICATIONS_GROUP,
2062 if (desktop_file_ids == NULL)
2065 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2066 g_hash_table_replace (dir->defaults_list_map,
2071 g_strfreev (mime_types);
2074 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2076 if (mime_types != NULL)
2078 for (i = 0; mime_types[i] != NULL; i++)
2080 desktop_file_ids = g_key_file_get_string_list (key_file,
2081 REMOVED_ASSOCIATIONS_GROUP,
2085 if (desktop_file_ids == NULL)
2088 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2089 g_hash_table_replace (dir->defaults_list_removed_map,
2094 g_strfreev (mime_types);
2097 g_key_file_free (key_file);
2102 g_key_file_free (key_file);
2104 if (mime_types != NULL)
2105 g_strfreev (mime_types);
2108 g_error_free (load_error);
2111 static MimeInfoCacheDir *
2112 mime_info_cache_dir_new (const char *path)
2114 MimeInfoCacheDir *dir;
2116 dir = g_new0 (MimeInfoCacheDir, 1);
2117 dir->path = g_strdup (path);
2123 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2128 if (dir->mime_info_cache_map != NULL)
2130 destroy_info_cache_map (dir->mime_info_cache_map);
2131 dir->mime_info_cache_map = NULL;
2135 if (dir->defaults_list_map != NULL)
2137 g_hash_table_destroy (dir->defaults_list_map);
2138 dir->defaults_list_map = NULL;
2141 if (dir->defaults_list_removed_map != NULL)
2143 g_hash_table_destroy (dir->defaults_list_removed_map);
2144 dir->defaults_list_removed_map = NULL;
2151 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2152 const char *mime_type,
2153 char **new_desktop_file_ids)
2155 GList *desktop_file_ids;
2158 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2161 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2163 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2164 desktop_file_ids = g_list_append (desktop_file_ids,
2165 g_strdup (new_desktop_file_ids[i]));
2168 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2172 mime_info_cache_init_dir_lists (void)
2174 const char * const *dirs;
2177 mime_info_cache = mime_info_cache_new ();
2179 dirs = get_applications_search_path ();
2181 for (i = 0; dirs[i] != NULL; i++)
2183 MimeInfoCacheDir *dir;
2185 dir = mime_info_cache_dir_new (dirs[i]);
2189 mime_info_cache_dir_init (dir);
2190 mime_info_cache_dir_init_defaults_list (dir);
2192 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2198 mime_info_cache_update_dir_lists (void)
2202 tmp = mime_info_cache->dirs;
2206 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2208 /* No need to do this if we had file monitors... */
2209 mime_info_cache_blow_global_cache ();
2210 mime_info_cache_dir_init (dir);
2211 mime_info_cache_dir_init_defaults_list (dir);
2218 mime_info_cache_init (void)
2220 G_LOCK (mime_info_cache);
2221 if (mime_info_cache == NULL)
2222 mime_info_cache_init_dir_lists ();
2228 if (now >= mime_info_cache->last_stat_time + 10)
2230 mime_info_cache_update_dir_lists ();
2231 mime_info_cache->last_stat_time = now;
2235 if (mime_info_cache->should_ping_mime_monitor)
2237 /* g_idle_add (emit_mime_changed, NULL); */
2238 mime_info_cache->should_ping_mime_monitor = FALSE;
2241 G_UNLOCK (mime_info_cache);
2244 static MimeInfoCache *
2245 mime_info_cache_new (void)
2247 MimeInfoCache *cache;
2249 cache = g_new0 (MimeInfoCache, 1);
2251 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2252 (GDestroyNotify) g_free,
2253 (GDestroyNotify) g_free);
2258 mime_info_cache_free (MimeInfoCache *cache)
2263 g_list_foreach (cache->dirs,
2264 (GFunc) mime_info_cache_dir_free,
2266 g_list_free (cache->dirs);
2267 g_hash_table_destroy (cache->global_defaults_cache);
2272 * mime_info_cache_reload:
2273 * @dir: directory path which needs reloading.
2275 * Reload the mime information for the @dir.
2278 mime_info_cache_reload (const char *dir)
2280 /* FIXME: just reload the dir that needs reloading,
2281 * don't blow the whole cache
2283 if (mime_info_cache != NULL)
2285 G_LOCK (mime_info_cache);
2286 mime_info_cache_free (mime_info_cache);
2287 mime_info_cache = NULL;
2288 G_UNLOCK (mime_info_cache);
2293 append_desktop_entry (GList *list,
2294 const char *desktop_entry,
2295 GList *removed_entries)
2297 /* Add if not already in list, and valid */
2298 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2299 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2300 list = g_list_prepend (list, g_strdup (desktop_entry));
2306 * get_all_desktop_entries_for_mime_type:
2307 * @mime_type: a mime type.
2309 * Returns all the desktop ids for @mime_type. The desktop files
2310 * are listed in an order so that default applications are listed before
2311 * non-default ones, and handlers for inherited mimetypes are listed
2312 * after the base ones.
2314 * Return value: a #GList containing the desktop ids which claim
2315 * to handle @mime_type.
2318 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2320 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2321 MimeInfoCacheDir *dir;
2324 char **default_entries;
2325 char **removed_associations;
2328 mime_info_cache_init ();
2330 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2331 G_LOCK (mime_info_cache);
2333 removed_entries = NULL;
2334 desktop_entries = NULL;
2335 for (i = 0; mime_types[i] != NULL; i++)
2337 mime_type = mime_types[i];
2339 /* Go through all apps listed as defaults */
2340 for (dir_list = mime_info_cache->dirs;
2342 dir_list = dir_list->next)
2344 dir = dir_list->data;
2345 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2346 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2347 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2349 removed_associations = g_hash_table_lookup (dir->defaults_list_removed_map, mime_type);
2350 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2351 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2354 /* Go through all entries that support the mimetype */
2355 for (dir_list = mime_info_cache->dirs;
2357 dir_list = dir_list->next)
2359 dir = dir_list->data;
2361 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2362 for (tmp = list; tmp != NULL; tmp = tmp->next)
2363 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2367 G_UNLOCK (mime_info_cache);
2369 g_strfreev (mime_types);
2371 g_list_free (removed_entries);
2373 desktop_entries = g_list_reverse (desktop_entries);
2375 return desktop_entries;
2378 /* GDesktopAppInfoLookup interface: */
2380 static void g_desktop_app_info_lookup_base_init (gpointer g_class);
2381 static void g_desktop_app_info_lookup_class_init (gpointer g_class,
2382 gpointer class_data);
2385 g_desktop_app_info_lookup_get_type (void)
2387 static GType desktop_app_info_lookup_type = 0;
2389 if (! desktop_app_info_lookup_type)
2391 static const GTypeInfo desktop_app_info_lookup_info =
2393 sizeof (GDesktopAppInfoLookupIface), /* class_size */
2394 g_desktop_app_info_lookup_base_init, /* base_init */
2395 NULL, /* base_finalize */
2396 g_desktop_app_info_lookup_class_init,
2397 NULL, /* class_finalize */
2398 NULL, /* class_data */
2400 0, /* n_preallocs */
2404 desktop_app_info_lookup_type =
2405 g_type_register_static (G_TYPE_INTERFACE, I_("GDesktopAppInfoLookup"),
2406 &desktop_app_info_lookup_info, 0);
2408 g_type_interface_add_prerequisite (desktop_app_info_lookup_type, G_TYPE_OBJECT);
2411 return desktop_app_info_lookup_type;
2415 g_desktop_app_info_lookup_class_init (gpointer g_class,
2416 gpointer class_data)
2421 g_desktop_app_info_lookup_base_init (gpointer g_class)
2426 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
2427 const char *uri_scheme)
2429 GDesktopAppInfoLookupIface *iface;
2431 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), FALSE);
2433 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
2435 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
2438 #define __G_DESKTOP_APP_INFO_C__
2439 #include "gioaliasdef.c"