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)
172 const char *p, *start;
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)
391 return info1 == info2;
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, char *uri)
460 file = g_file_new_for_uri (uri);
461 path = g_file_get_path (file);
462 g_object_unref (file);
468 result = g_shell_quote (uri);
473 result = g_shell_quote (path);
478 result = g_shell_quote (g_path_get_dirname (path));
483 result = g_shell_quote (g_path_get_basename (path));
493 expand_macro (char macro,
495 GDesktopAppInfo *info,
498 GList *uris = *uri_list;
501 g_return_if_fail (exec != NULL);
511 expanded = expand_macro_single (macro, uris->data);
514 g_string_append (exec, expanded);
528 expanded = expand_macro_single (macro, uris->data);
531 g_string_append (exec, expanded);
537 if (uris != NULL && expanded)
538 g_string_append_c (exec, ' ');
546 g_string_append (exec, "--icon ");
547 g_string_append (exec, info->icon_name);
553 g_string_append (exec, info->name);
558 g_string_append (exec, info->filename);
561 case 'm': /* deprecated */
565 g_string_append_c (exec, '%');
573 expand_application_parameters (GDesktopAppInfo *info,
579 GList *uri_list = *uris;
580 const char *p = info->exec;
581 GString *expanded_exec = g_string_new (NULL);
584 if (info->exec == NULL)
586 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
587 _("Desktop file didn't specify Exec field"));
593 if (p[0] == '%' && p[1] != '\0')
595 expand_macro (p[1], expanded_exec, info, uris);
599 g_string_append_c (expanded_exec, *p);
604 /* No file substitutions */
605 if (uri_list == *uris && uri_list != NULL)
607 /* If there is no macro default to %f. This is also what KDE does */
608 g_string_append_c (expanded_exec, ' ');
609 expand_macro ('f', expanded_exec, info, uris);
612 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
613 g_string_free (expanded_exec, TRUE);
618 prepend_terminal_to_vector (int *argc,
625 char **term_argv = NULL;
630 g_return_val_if_fail (argc != NULL, FALSE);
631 g_return_val_if_fail (argv != NULL, FALSE);
639 /* compute size if not given */
642 for (i = 0; the_argv[i] != NULL; i++)
648 term_argv = g_new0 (char *, 3);
650 check = g_find_program_in_path ("gnome-terminal");
653 term_argv[0] = check;
654 /* Note that gnome-terminal takes -x and
655 * as -e in gnome-terminal is broken we use that. */
656 term_argv[1] = g_strdup ("-x");
661 check = g_find_program_in_path ("nxterm");
663 check = g_find_program_in_path ("color-xterm");
665 check = g_find_program_in_path ("rxvt");
667 check = g_find_program_in_path ("xterm");
669 check = g_find_program_in_path ("dtterm");
672 check = g_strdup ("xterm");
673 g_warning ("couldn't find a terminal, falling back to xterm");
675 term_argv[0] = check;
676 term_argv[1] = g_strdup ("-e");
679 real_argc = term_argc + *argc;
680 real_argv = g_new (char *, real_argc + 1);
682 for (i = 0; i < term_argc; i++)
683 real_argv[i] = term_argv[i];
685 for (j = 0; j < *argc; j++, i++)
686 real_argv[i] = (char *)the_argv[j];
694 /* we use g_free here as we sucked all the inner strings
695 * out from it into real_argv */
700 #endif /* G_OS_WIN32 */
703 /* '=' is the new '\0'.
704 * DO NOT CALL unless at least one string ends with '='
707 is_env (const char *a,
712 if (*a == 0 || *b == 0)
725 /* free with g_strfreev */
727 replace_env_var (char **old_environ,
729 const char *new_value)
731 int length, new_length;
732 int index, new_index;
736 /* do two things at once:
737 * - discover the length of the environment ('length')
738 * - find the location (if any) of the env var ('index')
741 for (length = 0; old_environ[length]; length++)
743 /* if we already have it in our environment, replace */
744 if (is_env (old_environ[length], env_var))
749 /* no current env var, no desired env value.
752 if (new_value == NULL && index == -1)
755 /* in all cases now, we will be using a modified environment.
756 * determine its length and allocated it.
759 * new_index = location to insert, if any
760 * new_length = length of the new array
761 * new_environ = the pointer array for the new environment
764 if (new_value == NULL && index >= 0)
766 /* in this case, we will be removing an entry */
767 new_length = length - 1;
770 else if (new_value != NULL && index < 0)
772 /* in this case, we will be adding an entry to the end */
773 new_length = length + 1;
777 /* in this case, we will be replacing the existing entry */
783 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
784 new_environ[new_length] = NULL;
786 /* now we do the copying.
787 * for each entry in the new environment, we decide what to do
791 for (new_i = 0; new_i < new_length; new_i++)
793 if (new_i == new_index)
795 /* insert our new item */
796 new_environ[new_i] = g_strconcat (env_var,
801 /* if we had an old entry, skip it now */
807 /* if this is the old DESKTOP_STARTUP_ID, skip it */
811 /* copy an old item */
812 new_environ[new_i] = g_strdup (old_environ[i]);
817 g_strfreev (old_environ);
823 uri_list_segment_to_files (GList *start,
830 while (start != NULL && start != end)
832 file = g_file_new_for_uri ((char *)start->data);
833 res = g_list_prepend (res, file);
837 return g_list_reverse (res);
840 #ifdef HAVE__NSGETENVIRON
841 #define environ (*_NSGetEnviron())
842 #elif !defined(G_OS_WIN32)
844 /* According to the Single Unix Specification, environ is not in
845 * * any system header, although unistd.h often declares it.
847 extern char **environ;
851 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
853 GAppLaunchContext *launch_context,
856 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
857 gboolean completed = FALSE;
859 GList *launched_files;
866 g_return_val_if_fail (appinfo != NULL, FALSE);
874 if (!expand_application_parameters (info, &uris,
875 &argc, &argv, error))
878 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
880 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
881 _("Unable to find terminal required for application"));
888 launched_files = uri_list_segment_to_files (old_uris, uris);
890 display = g_app_launch_context_get_display (launch_context,
895 if (info->startup_notify)
896 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
900 if (display || sn_id)
904 envp = g_new0 (char *, 1);
906 envp = g_strdupv (environ);
910 envp = replace_env_var (envp,
915 envp = replace_env_var (envp,
916 "DESKTOP_STARTUP_ID",
922 g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
923 g_list_free (launched_files);
926 if (!g_spawn_async (info->path, /* working directory */
929 G_SPAWN_SEARCH_PATH /* flags */,
930 NULL /* child_setup */,
932 NULL /* child_pid */,
937 g_app_launch_context_launch_failed (launch_context, sn_id);
951 while (uris != NULL);
963 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
965 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
968 ((strstr (info->exec, "%u") != NULL) ||
969 (strstr (info->exec, "%U") != NULL));
973 g_desktop_app_info_supports_files (GAppInfo *appinfo)
975 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
978 ((strstr (info->exec, "%f") != NULL) ||
979 (strstr (info->exec, "%F") != NULL));
983 g_desktop_app_info_launch (GAppInfo *appinfo,
985 GAppLaunchContext *launch_context,
995 uri = g_file_get_uri (files->data);
996 uris = g_list_prepend (uris, uri);
1000 uris = g_list_reverse (uris);
1002 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1004 g_list_foreach (uris, (GFunc)g_free, NULL);
1010 G_LOCK_DEFINE_STATIC (g_desktop_env);
1011 static gchar *g_desktop_env = NULL;
1014 * g_desktop_app_info_set_desktop_env:
1015 * @desktop_env: a string specifying what desktop this is
1017 * Sets the name of the desktop that the application is running in.
1018 * This is used by g_app_info_should_show() to evaluate the
1019 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1020 * desktop entry fields.
1022 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1023 * Menu specification</ulink> recognizes the following:
1025 * <member>GNOME</member>
1026 * <member>KDE</member>
1027 * <member>ROX</member>
1028 * <member>XFCE</member>
1029 * <member>Old</member>
1032 * Should be called only once; subsequent calls are ignored.
1035 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1037 G_LOCK (g_desktop_env);
1039 g_desktop_env = g_strdup (desktop_env);
1040 G_UNLOCK (g_desktop_env);
1044 g_desktop_app_info_should_show (GAppInfo *appinfo)
1046 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1048 const gchar *desktop_env;
1051 if (info->nodisplay)
1054 G_LOCK (g_desktop_env);
1055 desktop_env = g_desktop_env;
1056 G_UNLOCK (g_desktop_env);
1058 if (info->only_show_in)
1060 if (desktop_env == NULL)
1064 for (i = 0; info->only_show_in[i] != NULL; i++)
1066 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1076 if (info->not_show_in && desktop_env)
1078 for (i = 0; info->not_show_in[i] != NULL; i++)
1080 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1094 ensure_dir (DirType type,
1097 char *path, *display_name;
1100 if (type == APP_DIR)
1101 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1103 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1106 if (g_mkdir_with_parents (path, 0700) == 0)
1110 display_name = g_filename_display_name (path);
1111 if (type == APP_DIR)
1112 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1113 _("Can't create user application configuration folder %s: %s"),
1114 display_name, g_strerror (err));
1116 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1117 _("Can't create user MIME configuration folder %s: %s"),
1118 display_name, g_strerror (err));
1120 g_free (display_name);
1127 update_default_list (const char *desktop_id,
1128 const char *content_type,
1129 gboolean add_at_start,
1130 gboolean add_at_end,
1134 char *dirname, *filename;
1136 gboolean load_succeeded, res;
1139 gsize length, data_size;
1143 /* Don't add both at start and end */
1144 g_assert (!(add_at_start && add_at_end));
1146 dirname = ensure_dir (APP_DIR, error);
1150 filename = g_build_filename (dirname, "defaults.list", NULL);
1153 key_file = g_key_file_new ();
1154 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1155 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1157 g_key_file_free (key_file);
1158 key_file = g_key_file_new ();
1161 /* Add to the right place in the list */
1164 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1165 content_type, &length, NULL);
1167 list = g_new (char *, 1 + length + 1);
1171 list[i++] = g_strdup (desktop_id);
1174 for (j = 0; old_list[j] != NULL; j++)
1176 if (strcmp (old_list[j], desktop_id) != 0)
1177 list[i++] = g_strdup (old_list[j]);
1181 list[i++] = g_strdup (desktop_id);
1184 g_strfreev (old_list);
1186 g_key_file_set_string_list (key_file,
1187 DEFAULT_APPLICATIONS_GROUP,
1189 (const char * const *)list, i);
1193 /* Remove from removed associations group (unless remove) */
1196 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1197 content_type, &length, NULL);
1199 list = g_new (char *, 1 + length + 1);
1203 list[i++] = g_strdup (desktop_id);
1206 for (j = 0; old_list[j] != NULL; j++)
1208 if (strcmp (old_list[j], desktop_id) != 0)
1209 list[i++] = g_strdup (old_list[j]);
1214 g_strfreev (old_list);
1216 if (list[0] == NULL)
1217 g_key_file_remove_key (key_file,
1218 REMOVED_ASSOCIATIONS_GROUP,
1222 g_key_file_set_string_list (key_file,
1223 REMOVED_ASSOCIATIONS_GROUP,
1225 (const char * const *)list, i);
1230 data = g_key_file_to_data (key_file, &data_size, error);
1231 g_key_file_free (key_file);
1233 res = g_file_set_contents (filename, data, data_size, error);
1235 mime_info_cache_reload (NULL);
1244 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1245 const char *content_type,
1248 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1250 if (!g_desktop_app_info_ensure_saved (info, error))
1253 return update_default_list (info->desktop_id, content_type, TRUE, FALSE, FALSE, error);
1257 update_program_done (GPid pid,
1261 /* Did the application exit correctly */
1262 if (WIFEXITED (status) &&
1263 WEXITSTATUS (status) == 0)
1265 /* Here we could clean out any caches in use */
1270 run_update_command (char *command,
1279 GError *error = NULL;
1282 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1284 if (g_spawn_async ("/", argv,
1286 G_SPAWN_SEARCH_PATH |
1287 G_SPAWN_STDOUT_TO_DEV_NULL |
1288 G_SPAWN_STDERR_TO_DEV_NULL |
1289 G_SPAWN_DO_NOT_REAP_CHILD,
1290 NULL, NULL, /* No setup function */
1293 g_child_watch_add (pid, update_program_done, NULL);
1296 /* If we get an error at this point, it's quite likely the user doesn't
1297 * have an installed copy of either 'update-mime-database' or
1298 * 'update-desktop-database'. I don't think we want to popup an error
1299 * dialog at this point, so we just do a g_warning to give the user a
1300 * chance of debugging it.
1302 g_warning ("%s", error->message);
1309 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1310 const char *extension,
1313 char *filename, *basename, *mimetype;
1317 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1320 dirname = ensure_dir (MIMETYPE_DIR, error);
1324 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1325 filename = g_build_filename (dirname, basename, NULL);
1329 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1331 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1336 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1337 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1338 " <mime-type type=\"%s\">\n"
1339 " <comment>%s document</comment>\n"
1340 " <glob pattern=\"*.%s\"/>\n"
1342 "</mime-info>\n", mimetype, extension, extension);
1344 g_file_set_contents (filename, contents, -1, NULL);
1347 run_update_command ("update-mime-database", "mime");
1351 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1361 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1362 const char *content_type,
1365 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1367 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1370 return update_default_list (info->desktop_id, content_type, FALSE, TRUE, FALSE, error);
1374 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1380 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1381 const char *content_type,
1384 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1386 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1389 return update_default_list (info->desktop_id, content_type, FALSE, FALSE, TRUE, error);
1393 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1399 char *data, *desktop_id;
1404 if (info->filename != NULL)
1407 /* This is only used for object created with
1408 * g_app_info_create_from_commandline. All other
1409 * object should have a filename
1412 dirname = ensure_dir (APP_DIR, error);
1416 key_file = g_key_file_new ();
1418 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1419 "Encoding", "UTF-8");
1420 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1421 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1422 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1423 G_KEY_FILE_DESKTOP_KEY_TYPE,
1424 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1426 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1427 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1429 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1430 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1432 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1433 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1435 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1436 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1438 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1439 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1441 data = g_key_file_to_data (key_file, &data_size, NULL);
1442 g_key_file_free (key_file);
1444 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1445 filename = g_build_filename (dirname, desktop_id, NULL);
1446 g_free (desktop_id);
1449 fd = g_mkstemp (filename);
1454 display_name = g_filename_display_name (filename);
1455 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1456 _("Can't create user desktop file %s"), display_name);
1457 g_free (display_name);
1463 desktop_id = g_path_get_basename (filename);
1467 res = g_file_set_contents (filename, data, data_size, error);
1470 g_free (desktop_id);
1475 info->filename = filename;
1476 info->desktop_id = desktop_id;
1478 run_update_command ("update-desktop-database", "applications");
1484 * g_app_info_create_from_commandline:
1485 * @commandline: the commandline to use
1486 * @application_name: the application name, or %NULL to use @commandline
1487 * @flags: flags that can specify details of the created #GAppInfo
1488 * @error: a #GError location to store the error occuring, %NULL to ignore.
1490 * Creates a new #GAppInfo from the given information.
1492 * Returns: new #GAppInfo for given command.
1495 g_app_info_create_from_commandline (const char *commandline,
1496 const char *application_name,
1497 GAppInfoCreateFlags flags,
1502 GDesktopAppInfo *info;
1504 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1506 info->filename = NULL;
1507 info->desktop_id = NULL;
1509 info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
1510 info->startup_notify = FALSE;
1511 info->hidden = FALSE;
1512 if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
1513 info->exec = g_strconcat (commandline, " %u", NULL);
1515 info->exec = g_strconcat (commandline, " %f", NULL);
1516 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
1517 info->nodisplay = TRUE;
1518 info->binary = binary_from_exec (info->exec);
1520 if (application_name)
1521 info->name = g_strdup (application_name);
1524 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1525 split = g_strsplit (commandline, " ", 2);
1526 basename = g_path_get_basename (split[0]);
1528 info->name = basename;
1529 if (info->name == NULL)
1530 info->name = g_strdup ("custom");
1533 return G_APP_INFO (info);
1537 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1539 iface->dup = g_desktop_app_info_dup;
1540 iface->equal = g_desktop_app_info_equal;
1541 iface->get_id = g_desktop_app_info_get_id;
1542 iface->get_name = g_desktop_app_info_get_name;
1543 iface->get_description = g_desktop_app_info_get_description;
1544 iface->get_executable = g_desktop_app_info_get_executable;
1545 iface->get_icon = g_desktop_app_info_get_icon;
1546 iface->launch = g_desktop_app_info_launch;
1547 iface->supports_uris = g_desktop_app_info_supports_uris;
1548 iface->supports_files = g_desktop_app_info_supports_files;
1549 iface->launch_uris = g_desktop_app_info_launch_uris;
1550 iface->should_show = g_desktop_app_info_should_show;
1551 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1552 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1553 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1554 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1555 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1559 app_info_in_list (GAppInfo *info,
1562 while (list != NULL)
1564 if (g_app_info_equal (info, list->data))
1573 * g_app_info_get_all_for_type:
1574 * @content_type: the content type to find a #GAppInfo for
1576 * Gets a list of all #GAppInfo s for a given content type.
1578 * Returns: #GList of #GAppInfo s for given @content_type
1579 * or %NULL on error.
1582 g_app_info_get_all_for_type (const char *content_type)
1584 GList *desktop_entries, *l;
1586 GDesktopAppInfo *info;
1588 g_return_val_if_fail (content_type != NULL, NULL);
1590 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1593 for (l = desktop_entries; l != NULL; l = l->next)
1595 char *desktop_entry = l->data;
1597 info = g_desktop_app_info_new (desktop_entry);
1600 if (app_info_in_list (G_APP_INFO (info), infos))
1601 g_object_unref (info);
1603 infos = g_list_prepend (infos, info);
1605 g_free (desktop_entry);
1608 g_list_free (desktop_entries);
1610 return g_list_reverse (infos);
1615 * g_app_info_get_default_for_type:
1616 * @content_type: the content type to find a #GAppInfo for
1617 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1620 * Gets the #GAppInfo that correspond to a given content type.
1622 * Returns: #GAppInfo for given @content_type or %NULL on error.
1625 g_app_info_get_default_for_type (const char *content_type,
1626 gboolean must_support_uris)
1628 GList *desktop_entries, *l;
1631 g_return_val_if_fail (content_type != NULL, NULL);
1633 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1636 for (l = desktop_entries; l != NULL; l = l->next)
1638 char *desktop_entry = l->data;
1640 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1643 if (must_support_uris && !g_app_info_supports_uris (info))
1645 g_object_unref (info);
1653 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1654 g_list_free (desktop_entries);
1660 * g_app_info_get_default_for_uri_scheme:
1661 * @uri_scheme: a string containing a URI scheme.
1663 * Gets the default application for launching applications
1664 * using this URI scheme.
1666 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
1669 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1671 static gsize lookup = 0;
1673 if (g_once_init_enter (&lookup))
1675 gsize setup_value = 1;
1676 GDesktopAppInfoLookup *lookup_instance;
1677 const char *use_this;
1678 GIOExtensionPoint *ep;
1679 GIOExtension *extension;
1682 use_this = g_getenv ("GIO_USE_URI_ASSOCIATION");
1684 /* Ensure vfs in modules loaded */
1685 _g_io_modules_ensure_loaded ();
1687 ep = g_io_extension_point_lookup (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1689 lookup_instance = NULL;
1692 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
1694 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1697 if (lookup_instance == NULL)
1699 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1701 extension = l->data;
1702 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1703 if (lookup_instance != NULL)
1708 if (lookup_instance != NULL)
1709 setup_value = (gsize)lookup_instance;
1711 g_once_init_leave (&lookup, setup_value);
1717 return g_desktop_app_info_lookup_get_default_for_uri_scheme (G_DESKTOP_APP_INFO_LOOKUP (lookup),
1723 get_apps_from_dir (GHashTable *apps,
1724 const char *dirname,
1728 const char *basename;
1729 char *filename, *subprefix, *desktop_id;
1731 GDesktopAppInfo *appinfo;
1733 dir = g_dir_open (dirname, 0, NULL);
1736 while ((basename = g_dir_read_name (dir)) != NULL)
1738 filename = g_build_filename (dirname, basename, NULL);
1739 if (g_str_has_suffix (basename, ".desktop"))
1741 desktop_id = g_strconcat (prefix, basename, NULL);
1743 /* Use _extended so we catch NULLs too (hidden) */
1744 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1746 appinfo = g_desktop_app_info_new_from_filename (filename);
1748 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1750 g_object_unref (appinfo);
1755 if (appinfo || hidden)
1757 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1761 /* Reuse instead of strdup here */
1762 appinfo->desktop_id = desktop_id;
1767 g_free (desktop_id);
1771 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1773 subprefix = g_strconcat (prefix, basename, "-", NULL);
1774 get_apps_from_dir (apps, filename, subprefix);
1786 * g_app_info_get_all:
1788 * Gets a list of all of the applications currently registered
1791 * For desktop files, this includes applications that have
1792 * <literal>NoDisplay=true</literal> set or are excluded from
1793 * display by means of <literal>OnlyShowIn</literal> or
1794 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1795 * The returned list does not include applications which have
1796 * the <literal>Hidden</literal> key set.
1798 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1801 g_app_info_get_all (void)
1803 const char * const *dirs;
1805 GHashTableIter iter;
1810 dirs = get_applications_search_path ();
1812 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1816 for (i = 0; dirs[i] != NULL; i++)
1817 get_apps_from_dir (apps, dirs[i], "");
1821 g_hash_table_iter_init (&iter, apps);
1822 while (g_hash_table_iter_next (&iter, NULL, &value))
1825 infos = g_list_prepend (infos, value);
1828 g_hash_table_destroy (apps);
1830 return g_list_reverse (infos);
1833 /* Cacheing of mimeinfo.cache and defaults.list files */
1837 GHashTable *mime_info_cache_map;
1838 GHashTable *defaults_list_map;
1839 GHashTable *defaults_list_removed_map;
1840 time_t mime_info_cache_timestamp;
1841 time_t defaults_list_timestamp;
1845 GList *dirs; /* mimeinfo.cache and defaults.list */
1846 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1847 time_t last_stat_time;
1848 guint should_ping_mime_monitor : 1;
1851 static MimeInfoCache *mime_info_cache = NULL;
1852 G_LOCK_DEFINE_STATIC (mime_info_cache);
1854 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1855 const char *mime_type,
1856 char **new_desktop_file_ids);
1858 static MimeInfoCache * mime_info_cache_new (void);
1861 destroy_info_cache_value (gpointer key,
1865 g_list_foreach (value, (GFunc)g_free, NULL);
1866 g_list_free (value);
1870 destroy_info_cache_map (GHashTable *info_cache_map)
1872 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1873 g_hash_table_destroy (info_cache_map);
1877 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1878 const char *cache_file,
1884 filename = g_build_filename (dir->path, cache_file, NULL);
1886 if (g_stat (filename, &buf) < 0)
1893 if (buf.st_mtime != *timestamp)
1899 /* Call with lock held */
1901 remove_all (gpointer key,
1910 mime_info_cache_blow_global_cache (void)
1912 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1917 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1921 gchar *filename, **mime_types;
1928 if (dir->mime_info_cache_map != NULL &&
1929 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1930 &dir->mime_info_cache_timestamp))
1933 if (dir->mime_info_cache_map != NULL)
1934 destroy_info_cache_map (dir->mime_info_cache_map);
1936 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1937 (GDestroyNotify) g_free,
1940 key_file = g_key_file_new ();
1942 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1944 if (g_stat (filename, &buf) < 0)
1947 if (dir->mime_info_cache_timestamp > 0)
1948 mime_info_cache->should_ping_mime_monitor = TRUE;
1950 dir->mime_info_cache_timestamp = buf.st_mtime;
1952 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1957 if (load_error != NULL)
1960 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1963 if (load_error != NULL)
1966 for (i = 0; mime_types[i] != NULL; i++)
1968 gchar **desktop_file_ids;
1969 char *unaliased_type;
1970 desktop_file_ids = g_key_file_get_string_list (key_file,
1976 if (desktop_file_ids == NULL)
1979 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1980 mime_info_cache_dir_add_desktop_entries (dir,
1983 g_free (unaliased_type);
1985 g_strfreev (desktop_file_ids);
1988 g_strfreev (mime_types);
1989 g_key_file_free (key_file);
1994 g_key_file_free (key_file);
1996 if (mime_types != NULL)
1997 g_strfreev (mime_types);
2000 g_error_free (load_error);
2004 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2008 gchar *filename, **mime_types;
2009 char *unaliased_type;
2010 char **desktop_file_ids;
2017 if (dir->defaults_list_map != NULL &&
2018 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2019 &dir->defaults_list_timestamp))
2022 if (dir->defaults_list_map != NULL)
2023 g_hash_table_destroy (dir->defaults_list_map);
2024 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2025 g_free, (GDestroyNotify)g_strfreev);
2027 if (dir->defaults_list_removed_map != NULL)
2028 g_hash_table_destroy (dir->defaults_list_removed_map);
2029 dir->defaults_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2030 g_free, (GDestroyNotify)g_strfreev);
2032 key_file = g_key_file_new ();
2034 filename = g_build_filename (dir->path, "defaults.list", NULL);
2035 if (g_stat (filename, &buf) < 0)
2038 if (dir->defaults_list_timestamp > 0)
2039 mime_info_cache->should_ping_mime_monitor = TRUE;
2041 dir->defaults_list_timestamp = buf.st_mtime;
2043 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2047 if (load_error != NULL)
2050 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2052 if (mime_types != NULL)
2054 for (i = 0; mime_types[i] != NULL; i++)
2056 desktop_file_ids = g_key_file_get_string_list (key_file,
2057 DEFAULT_APPLICATIONS_GROUP,
2061 if (desktop_file_ids == NULL)
2064 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2065 g_hash_table_replace (dir->defaults_list_map,
2070 g_strfreev (mime_types);
2073 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2075 if (mime_types != NULL)
2077 for (i = 0; mime_types[i] != NULL; i++)
2079 desktop_file_ids = g_key_file_get_string_list (key_file,
2080 REMOVED_ASSOCIATIONS_GROUP,
2084 if (desktop_file_ids == NULL)
2087 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2088 g_hash_table_replace (dir->defaults_list_removed_map,
2093 g_strfreev (mime_types);
2096 g_key_file_free (key_file);
2101 g_key_file_free (key_file);
2103 if (mime_types != NULL)
2104 g_strfreev (mime_types);
2107 g_error_free (load_error);
2110 static MimeInfoCacheDir *
2111 mime_info_cache_dir_new (const char *path)
2113 MimeInfoCacheDir *dir;
2115 dir = g_new0 (MimeInfoCacheDir, 1);
2116 dir->path = g_strdup (path);
2122 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2127 if (dir->mime_info_cache_map != NULL)
2129 destroy_info_cache_map (dir->mime_info_cache_map);
2130 dir->mime_info_cache_map = NULL;
2134 if (dir->defaults_list_map != NULL)
2136 g_hash_table_destroy (dir->defaults_list_map);
2137 dir->defaults_list_map = NULL;
2140 if (dir->defaults_list_removed_map != NULL)
2142 g_hash_table_destroy (dir->defaults_list_removed_map);
2143 dir->defaults_list_removed_map = NULL;
2150 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2151 const char *mime_type,
2152 char **new_desktop_file_ids)
2154 GList *desktop_file_ids;
2157 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2160 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2162 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2163 desktop_file_ids = g_list_append (desktop_file_ids,
2164 g_strdup (new_desktop_file_ids[i]));
2167 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2171 mime_info_cache_init_dir_lists (void)
2173 const char * const *dirs;
2176 mime_info_cache = mime_info_cache_new ();
2178 dirs = get_applications_search_path ();
2180 for (i = 0; dirs[i] != NULL; i++)
2182 MimeInfoCacheDir *dir;
2184 dir = mime_info_cache_dir_new (dirs[i]);
2188 mime_info_cache_dir_init (dir);
2189 mime_info_cache_dir_init_defaults_list (dir);
2191 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2197 mime_info_cache_update_dir_lists (void)
2201 tmp = mime_info_cache->dirs;
2205 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2207 /* No need to do this if we had file monitors... */
2208 mime_info_cache_blow_global_cache ();
2209 mime_info_cache_dir_init (dir);
2210 mime_info_cache_dir_init_defaults_list (dir);
2217 mime_info_cache_init (void)
2219 G_LOCK (mime_info_cache);
2220 if (mime_info_cache == NULL)
2221 mime_info_cache_init_dir_lists ();
2227 if (now >= mime_info_cache->last_stat_time + 10)
2229 mime_info_cache_update_dir_lists ();
2230 mime_info_cache->last_stat_time = now;
2234 if (mime_info_cache->should_ping_mime_monitor)
2236 /* g_idle_add (emit_mime_changed, NULL); */
2237 mime_info_cache->should_ping_mime_monitor = FALSE;
2240 G_UNLOCK (mime_info_cache);
2243 static MimeInfoCache *
2244 mime_info_cache_new (void)
2246 MimeInfoCache *cache;
2248 cache = g_new0 (MimeInfoCache, 1);
2250 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2251 (GDestroyNotify) g_free,
2252 (GDestroyNotify) g_free);
2257 mime_info_cache_free (MimeInfoCache *cache)
2262 g_list_foreach (cache->dirs,
2263 (GFunc) mime_info_cache_dir_free,
2265 g_list_free (cache->dirs);
2266 g_hash_table_destroy (cache->global_defaults_cache);
2271 * mime_info_cache_reload:
2272 * @dir: directory path which needs reloading.
2274 * Reload the mime information for the @dir.
2277 mime_info_cache_reload (const char *dir)
2279 /* FIXME: just reload the dir that needs reloading,
2280 * don't blow the whole cache
2282 if (mime_info_cache != NULL)
2284 G_LOCK (mime_info_cache);
2285 mime_info_cache_free (mime_info_cache);
2286 mime_info_cache = NULL;
2287 G_UNLOCK (mime_info_cache);
2292 append_desktop_entry (GList *list,
2293 const char *desktop_entry,
2294 GList *removed_entries)
2296 /* Add if not already in list, and valid */
2297 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2298 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2299 list = g_list_prepend (list, g_strdup (desktop_entry));
2305 * get_all_desktop_entries_for_mime_type:
2306 * @mime_type: a mime type.
2308 * Returns all the desktop ids for @mime_type. The desktop files
2309 * are listed in an order so that default applications are listed before
2310 * non-default ones, and handlers for inherited mimetypes are listed
2311 * after the base ones.
2313 * Return value: a #GList containing the desktop ids which claim
2314 * to handle @mime_type.
2317 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2319 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2320 MimeInfoCacheDir *dir;
2323 char **default_entries;
2324 char **removed_associations;
2327 mime_info_cache_init ();
2329 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2330 G_LOCK (mime_info_cache);
2332 removed_entries = NULL;
2333 desktop_entries = NULL;
2334 for (i = 0; mime_types[i] != NULL; i++)
2336 mime_type = mime_types[i];
2338 /* Go through all apps listed as defaults */
2339 for (dir_list = mime_info_cache->dirs;
2341 dir_list = dir_list->next)
2343 dir = dir_list->data;
2344 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2345 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2346 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2348 removed_associations = g_hash_table_lookup (dir->defaults_list_removed_map, mime_type);
2349 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2350 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2353 /* Go through all entries that support the mimetype */
2354 for (dir_list = mime_info_cache->dirs;
2356 dir_list = dir_list->next)
2358 dir = dir_list->data;
2360 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2361 for (tmp = list; tmp != NULL; tmp = tmp->next)
2362 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2366 G_UNLOCK (mime_info_cache);
2368 g_strfreev (mime_types);
2370 g_list_free (removed_entries);
2372 desktop_entries = g_list_reverse (desktop_entries);
2374 return desktop_entries;
2377 /* GDesktopAppInfoLookup interface: */
2379 static void g_desktop_app_info_lookup_base_init (gpointer g_class);
2380 static void g_desktop_app_info_lookup_class_init (gpointer g_class,
2381 gpointer class_data);
2384 g_desktop_app_info_lookup_get_type (void)
2386 static GType desktop_app_info_lookup_type = 0;
2388 if (! desktop_app_info_lookup_type)
2390 static const GTypeInfo desktop_app_info_lookup_info =
2392 sizeof (GDesktopAppInfoLookupIface), /* class_size */
2393 g_desktop_app_info_lookup_base_init, /* base_init */
2394 NULL, /* base_finalize */
2395 g_desktop_app_info_lookup_class_init,
2396 NULL, /* class_finalize */
2397 NULL, /* class_data */
2399 0, /* n_preallocs */
2403 desktop_app_info_lookup_type =
2404 g_type_register_static (G_TYPE_INTERFACE, I_("GDesktopAppInfoLookup"),
2405 &desktop_app_info_lookup_info, 0);
2407 g_type_interface_add_prerequisite (desktop_app_info_lookup_type, G_TYPE_OBJECT);
2410 return desktop_app_info_lookup_type;
2414 g_desktop_app_info_lookup_class_init (gpointer g_class,
2415 gpointer class_data)
2420 g_desktop_app_info_lookup_base_init (gpointer g_class)
2425 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
2426 const char *uri_scheme)
2428 GDesktopAppInfoLookupIface *iface;
2430 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), FALSE);
2432 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
2434 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
2437 #define __G_DESKTOP_APP_INFO_C__
2438 #include "gioaliasdef.c"