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 #include "gcontenttypeprivate.h"
32 #include "gdesktopappinfo.h"
34 #include "gthemedicon.h"
35 #include "gfileicon.h"
36 #include <glib/gstdio.h>
41 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
42 #define MIME_CACHE_GROUP "MIME Cache"
44 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
46 static GList *get_all_desktop_entries_for_mime_type (const char *base_mime_type);
47 static void mime_info_cache_reload (const char *dir);
49 struct _GDesktopAppInfo
51 GObject parent_instance;
57 /* FIXME: what about GenericName ? */
71 guint startup_notify : 1;
72 /* FIXME: what about StartupWMClass ? */
75 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
76 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
77 g_desktop_app_info_iface_init))
80 search_path_init (gpointer data)
83 const char * const *data_dirs;
84 const char *user_data_dir;
87 data_dirs = g_get_system_data_dirs ();
88 length = g_strv_length ((char **) data_dirs);
90 args = g_new (char *, length + 2);
93 user_data_dir = g_get_user_data_dir ();
94 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
95 for (i = 0; i < length; i++)
96 args[j++] = g_build_filename (data_dirs[i],
97 "applications", NULL);
103 static const char * const *
104 get_applications_search_path (void)
106 static GOnce once_init = G_ONCE_INIT;
107 return g_once (&once_init, search_path_init, NULL);
111 g_desktop_app_info_finalize (GObject *object)
113 GDesktopAppInfo *info;
115 info = G_DESKTOP_APP_INFO (object);
117 g_free (info->desktop_id);
118 g_free (info->filename);
120 g_free (info->comment);
121 g_free (info->icon_name);
123 g_object_unref (info->icon);
124 g_strfreev (info->only_show_in);
125 g_strfreev (info->not_show_in);
126 g_free (info->try_exec);
128 g_free (info->binary);
131 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
135 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
137 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
139 gobject_class->finalize = g_desktop_app_info_finalize;
143 g_desktop_app_info_init (GDesktopAppInfo *local)
148 * g_desktop_app_info_new_from_filename:
149 * @filename: a string containing a file name.
151 * Returns: a new #GDesktopAppInfo or %NULL on error.
154 g_desktop_app_info_new_from_filename (const char *filename)
156 GDesktopAppInfo *info;
162 key_file = g_key_file_new ();
164 if (!g_key_file_load_from_file (key_file,
170 start_group = g_key_file_get_start_group (key_file);
171 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
173 g_free (start_group);
176 g_free (start_group);
178 type = g_key_file_get_string (key_file,
179 G_KEY_FILE_DESKTOP_GROUP,
180 G_KEY_FILE_DESKTOP_KEY_TYPE,
182 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
189 try_exec = g_key_file_get_string (key_file,
190 G_KEY_FILE_DESKTOP_GROUP,
191 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
196 t = g_find_program_in_path (try_exec);
205 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
206 info->filename = g_strdup (filename);
208 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
209 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
210 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
211 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
212 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);
213 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);
214 info->try_exec = try_exec;
215 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
216 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
217 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
218 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
219 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
224 if (g_path_is_absolute (info->icon_name))
228 file = g_file_new_for_path (info->icon_name);
229 info->icon = g_file_icon_new (file);
230 g_object_unref (file);
233 info->icon = g_themed_icon_new (info->icon_name);
244 while (*p != ' ' && *p != 0)
247 info->binary = g_strndup (start, p - start);
254 * g_desktop_app_info_new:
257 * Returns: a new #GDesktopAppInfo.
260 g_desktop_app_info_new (const char *desktop_id)
262 GDesktopAppInfo *appinfo;
263 const char * const *dirs;
266 dirs = get_applications_search_path ();
268 for (i = 0; dirs[i] != NULL; i++)
274 filename = g_build_filename (dirs[i], desktop_id, NULL);
275 appinfo = g_desktop_app_info_new_from_filename (filename);
282 basename = g_strdup (desktop_id);
284 while ((p = strchr (p, '-')) != NULL)
288 filename = g_build_filename (dirs[i], basename, NULL);
289 appinfo = g_desktop_app_info_new_from_filename (filename);
304 appinfo->desktop_id = g_strdup (desktop_id);
306 if (g_desktop_app_info_get_is_hidden (appinfo))
308 g_object_unref (appinfo);
316 g_desktop_app_info_dup (GAppInfo *appinfo)
318 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
319 GDesktopAppInfo *new_info;
321 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
323 new_info->filename = g_strdup (info->filename);
324 new_info->desktop_id = g_strdup (info->desktop_id);
326 new_info->name = g_strdup (info->name);
327 new_info->comment = g_strdup (info->comment);
328 new_info->nodisplay = info->nodisplay;
329 new_info->icon_name = g_strdup (info->icon_name);
330 new_info->icon = g_object_ref (info->icon);
331 new_info->only_show_in = g_strdupv (info->only_show_in);
332 new_info->not_show_in = g_strdupv (info->not_show_in);
333 new_info->try_exec = g_strdup (info->try_exec);
334 new_info->exec = g_strdup (info->exec);
335 new_info->binary = g_strdup (info->binary);
336 new_info->path = g_strdup (info->path);
337 new_info->hidden = info->hidden;
338 new_info->terminal = info->terminal;
339 new_info->startup_notify = info->startup_notify;
341 return G_APP_INFO (new_info);
345 g_desktop_app_info_equal (GAppInfo *appinfo1,
348 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
349 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
351 if (info1->desktop_id == NULL ||
352 info2->desktop_id == NULL)
355 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
359 g_desktop_app_info_get_id (GAppInfo *appinfo)
361 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
363 return info->desktop_id;
367 g_desktop_app_info_get_name (GAppInfo *appinfo)
369 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
371 if (info->name == NULL)
377 * g_desktop_app_info_get_is_hidden:
380 * Returns: %TRUE if hidden, %FALSE otherwise.
383 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
389 g_desktop_app_info_get_description (GAppInfo *appinfo)
391 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
393 return info->comment;
397 g_desktop_app_info_get_executable (GAppInfo *appinfo)
399 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
405 g_desktop_app_info_get_icon (GAppInfo *appinfo)
407 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
413 expand_macro_single (char macro, GFile *file)
418 path = g_file_get_path (file);
419 uri = g_file_get_uri (file);
425 result = g_shell_quote (uri);
430 result = g_shell_quote (path);
435 result = g_shell_quote (g_path_get_dirname (path));
440 result = g_shell_quote (g_path_get_basename (path));
451 expand_macro (char macro, GString *exec, GDesktopAppInfo *info, GList **file_list)
453 GList *files = *file_list;
456 g_return_if_fail (exec != NULL);
466 expanded = expand_macro_single (macro, files->data);
469 g_string_append (exec, expanded);
483 expanded = expand_macro_single (macro, files->data);
486 g_string_append (exec, expanded);
492 if (files != NULL && expanded)
493 g_string_append_c (exec, ' ');
501 g_string_append (exec, "--icon ");
502 g_string_append (exec, info->icon_name);
508 g_string_append (exec, info->name);
513 g_string_append (exec, info->filename);
516 case 'm': /* deprecated */
520 g_string_append_c (exec, '%');
528 expand_application_parameters (GDesktopAppInfo *info,
534 GList *file_list = *files;
535 const char *p = info->exec;
536 GString *expanded_exec = g_string_new (NULL);
539 if (info->exec == NULL)
541 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
542 _("Desktop file didn't specify Exec field"));
548 if (p[0] == '%' && p[1] != '\0')
550 expand_macro (p[1], expanded_exec, info, files);
554 g_string_append_c (expanded_exec, *p);
559 /* No file substitutions */
560 if (file_list == *files && file_list != NULL)
562 /* If there is no macro default to %f. This is also what KDE does */
563 g_string_append_c (expanded_exec, ' ');
564 expand_macro ('f', expanded_exec, info, files);
567 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
568 g_string_free (expanded_exec, TRUE);
573 prepend_terminal_to_vector (int *argc,
580 char **term_argv = NULL;
585 g_return_val_if_fail (argc != NULL, FALSE);
586 g_return_val_if_fail (argv != NULL, FALSE);
594 /* compute size if not given */
597 for (i = 0; the_argv[i] != NULL; i++)
603 term_argv = g_new0 (char *, 3);
605 check = g_find_program_in_path ("gnome-terminal");
608 term_argv[0] = check;
609 /* Note that gnome-terminal takes -x and
610 * as -e in gnome-terminal is broken we use that. */
611 term_argv[1] = g_strdup ("-x");
616 check = g_find_program_in_path ("nxterm");
618 check = g_find_program_in_path ("color-xterm");
620 check = g_find_program_in_path ("rxvt");
622 check = g_find_program_in_path ("xterm");
624 check = g_find_program_in_path ("dtterm");
627 check = g_strdup ("xterm");
628 g_warning ("couldn't find a terminal, falling back to xterm");
630 term_argv[0] = check;
631 term_argv[1] = g_strdup ("-e");
634 real_argc = term_argc + *argc;
635 real_argv = g_new (char *, real_argc + 1);
637 for (i = 0; i < term_argc; i++)
638 real_argv[i] = term_argv[i];
640 for (j = 0; j < *argc; j++, i++)
641 real_argv[i] = (char *)the_argv[j];
649 /* we use g_free here as we sucked all the inner strings
650 * out from it into real_argv */
655 #endif /* G_OS_WIN32 */
658 /* '=' is the new '\0'.
659 * DO NOT CALL unless at least one string ends with '='
662 is_env (const char *a,
667 if (*a == 0 || *b == 0)
680 /* free with g_strfreev */
682 replace_env_var (char **old_environ,
684 const char *new_value)
686 int length, new_length;
687 int index, new_index;
691 /* do two things at once:
692 * - discover the length of the environment ('length')
693 * - find the location (if any) of the env var ('index')
696 for (length = 0; old_environ[length]; length++)
698 /* if we already have it in our environment, replace */
699 if (is_env (old_environ[length], env_var))
704 /* no current env var, no desired env value.
707 if (new_value == NULL && index == -1)
710 /* in all cases now, we will be using a modified environment.
711 * determine its length and allocated it.
714 * new_index = location to insert, if any
715 * new_length = length of the new array
716 * new_environ = the pointer array for the new environment
719 if (new_value == NULL && index >= 0)
721 /* in this case, we will be removing an entry */
722 new_length = length - 1;
725 else if (new_value != NULL && index < 0)
727 /* in this case, we will be adding an entry to the end */
728 new_length = length + 1;
732 /* in this case, we will be replacing the existing entry */
738 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
739 new_environ[new_length] = NULL;
741 /* now we do the copying.
742 * for each entry in the new environment, we decide what to do
746 for (new_i = 0; new_i < new_length; new_i++)
748 if (new_i == new_index)
750 /* insert our new item */
751 new_environ[new_i] = g_strconcat (env_var,
756 /* if we had an old entry, skip it now */
762 /* if this is the old DESKTOP_STARTUP_ID, skip it */
766 /* copy an old item */
767 new_environ[new_i] = g_strdup (old_environ[i]);
772 g_strfreev (old_environ);
778 dup_list_segment (GList *start,
784 while (start != NULL && start != end)
786 res = g_list_prepend (res, start->data);
790 return g_list_reverse (res);
794 g_desktop_app_info_launch (GAppInfo *appinfo,
796 GAppLaunchContext *launch_context,
799 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
800 gboolean completed = FALSE;
802 GList *launched_files;
809 g_return_val_if_fail (appinfo != NULL, FALSE);
817 if (!expand_application_parameters (info, &files,
818 &argc, &argv, error))
821 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
823 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
824 _("Unable to find terminal required for application"));
831 launched_files = dup_list_segment (old_files, files);
833 display = g_app_launch_context_get_display (launch_context,
838 if (info->startup_notify)
839 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
843 if (display || sn_id)
848 envp = replace_env_var (envp,
853 envp = replace_env_var (envp,
854 "DESKTOP_STARTUP_ID",
860 g_list_free (launched_files);
863 if (!g_spawn_async (info->path, /* working directory */
866 G_SPAWN_SEARCH_PATH /* flags */,
867 NULL /* child_setup */,
869 NULL /* child_pid */,
874 g_app_launch_context_launch_failed (launch_context, sn_id);
888 while (files != NULL);
900 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
902 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
905 (strstr (info->exec, "%u") != NULL) ||
906 (strstr (info->exec, "%U") != NULL);
910 g_desktop_app_info_supports_xdg_startup_notify (GAppInfo *appinfo)
912 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
914 return info->startup_notify;
918 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
920 GAppLaunchContext *launch_context,
930 file = g_file_new_for_uri (uris->data);
932 g_warning ("Invalid uri passed to g_desktop_app_info_launch_uris");
935 files = g_list_prepend (files, file);
938 files = g_list_reverse (files);
940 res = g_desktop_app_info_launch (appinfo, files, launch_context, error);
942 g_list_foreach (files, (GFunc)g_object_unref, NULL);
949 g_desktop_app_info_should_show (GAppInfo *appinfo,
950 const char *desktop_env)
952 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
959 if (info->only_show_in)
961 if (desktop_env == NULL)
965 for (i = 0; info->only_show_in[i] != NULL; i++)
967 if (strcmp (info->only_show_in[i], desktop_env) == 0)
977 if (info->not_show_in && desktop_env)
979 for (i = 0; info->not_show_in[i] != NULL; i++)
981 if (strcmp (info->not_show_in[i], desktop_env) == 0)
995 ensure_dir (DirType type,
998 char *path, *display_name;
1001 if (type == APP_DIR)
1003 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1007 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1011 if (g_mkdir_with_parents (path, 0700) == 0)
1015 display_name = g_filename_display_name (path);
1016 if (type == APP_DIR)
1018 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1019 _("Can't create user application configuration folder %s: %s"),
1020 display_name, g_strerror (err));
1024 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1025 _("Can't create user MIME configuration folder %s: %s"),
1026 display_name, g_strerror (err));
1029 g_free (display_name);
1036 update_default_list (const char *desktop_id, const char *content_type, gboolean add, GError **error)
1038 char *dirname, *filename;
1040 gboolean load_succeeded, res;
1043 gsize length, data_size;
1047 dirname = ensure_dir (APP_DIR, error);
1051 filename = g_build_filename (dirname, "defaults.list", NULL);
1054 key_file = g_key_file_new ();
1055 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1056 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1058 g_key_file_free (key_file);
1059 key_file = g_key_file_new ();
1063 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1064 content_type, &length, NULL);
1066 list = g_new (char *, 1 + length + 1);
1070 list[i++] = g_strdup (desktop_id);
1073 for (j = 0; old_list[j] != NULL; j++)
1075 if (strcmp (old_list[j], desktop_id) != 0)
1076 list[i++] = g_strdup (old_list[j]);
1081 g_strfreev (old_list);
1083 g_key_file_set_string_list (key_file,
1084 DEFAULT_APPLICATIONS_GROUP,
1086 (const char * const *)list, i);
1090 data = g_key_file_to_data (key_file, &data_size, error);
1091 g_key_file_free (key_file);
1093 res = g_file_set_contents (filename, data, data_size, error);
1095 mime_info_cache_reload (NULL);
1104 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1105 const char *content_type,
1108 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1110 if (!g_app_info_add_supports_type (appinfo, content_type, error))
1113 return update_default_list (info->desktop_id, content_type, TRUE, error);
1117 update_program_done (GPid pid,
1121 /* Did the application exit correctly */
1122 if (WIFEXITED (status) &&
1123 WEXITSTATUS (status) == 0)
1125 /* Here we could clean out any caches in use */
1130 run_update_command (char *command,
1139 GError *error = NULL;
1142 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1144 if (g_spawn_async ("/", argv,
1146 G_SPAWN_SEARCH_PATH |
1147 G_SPAWN_STDOUT_TO_DEV_NULL |
1148 G_SPAWN_STDERR_TO_DEV_NULL |
1149 G_SPAWN_DO_NOT_REAP_CHILD,
1150 NULL, NULL, /* No setup function */
1153 g_child_watch_add (pid, update_program_done, NULL);
1156 /* If we get an error at this point, it's quite likely the user doesn't
1157 * have an installed copy of either 'update-mime-database' or
1158 * 'update-desktop-database'. I don't think we want to popup an error
1159 * dialog at this point, so we just do a g_warning to give the user a
1160 * chance of debugging it.
1162 g_warning ("%s", error->message);
1169 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1170 const char *extension,
1173 char *filename, *basename, *mimetype;
1177 dirname = ensure_dir (MIMETYPE_DIR, error);
1181 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1182 filename = g_build_filename (dirname, basename, NULL);
1186 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1188 if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
1192 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1193 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1194 " <mime-type type=\"%s\">\n"
1195 " <comment>%s document</comment>\n"
1196 " <glob pattern=\"*.%s\"/>\n"
1198 "</mime-info>\n", mimetype, extension, extension);
1200 g_file_set_contents (filename, contents, -1, NULL);
1203 run_update_command ("update-mime-database", "mime");
1207 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1217 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1218 const char *content_type,
1221 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1223 char *new_mimetypes, *old_mimetypes, *content;
1227 keyfile = g_key_file_new ();
1228 if (!g_key_file_load_from_file (keyfile, info->filename,
1229 G_KEY_FILE_KEEP_COMMENTS |
1230 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1232 g_key_file_free (keyfile);
1236 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1237 new_mimetypes = g_strconcat (content_type, ";", old_mimetypes, NULL);
1238 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1239 g_free (old_mimetypes);
1240 g_free (new_mimetypes);
1242 content = g_key_file_to_data (keyfile, NULL, NULL);
1243 g_key_file_free (keyfile);
1245 dirname = ensure_dir (APP_DIR, error);
1252 filename = g_build_filename (dirname, info->desktop_id, NULL);
1255 if (!g_file_set_contents (filename, content, -1, error))
1264 run_update_command ("update-desktop-database", "applications");
1269 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1271 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1274 user_dirname = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1275 return g_str_has_prefix (info->filename, user_dirname);
1279 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1280 const char *content_type,
1283 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1285 char *new_mimetypes, *old_mimetypes, *content;
1290 keyfile = g_key_file_new ();
1291 if (!g_key_file_load_from_file (keyfile, info->filename,
1292 G_KEY_FILE_KEEP_COMMENTS |
1293 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1295 g_key_file_free (keyfile);
1299 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1300 new_mimetypes = g_strdup (old_mimetypes);
1303 found = strstr (new_mimetypes, content_type);
1304 if (found && *(found + strlen (content_type)) == ';')
1306 char *rest = found + strlen (content_type) + 1;
1307 memmove (found, rest, strlen (rest) + 1);
1309 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1310 g_free (old_mimetypes);
1311 g_free (new_mimetypes);
1313 content = g_key_file_to_data (keyfile, NULL, NULL);
1314 g_key_file_free (keyfile);
1316 dirname = ensure_dir (APP_DIR, error);
1323 filename = g_build_filename (dirname, info->desktop_id, NULL);
1325 if (!g_file_set_contents (filename, content, -1, error))
1334 run_update_command ("update-desktop-database", "applications");
1336 return update_default_list (info->desktop_id, content_type, FALSE, error);
1340 * g_app_info_create_from_commandline:
1341 * @commandline: the commandline to use
1342 * @application_name: the application name, or %NULL to use @commandline
1343 * @flags: flags that can specify details of the created #GAppInfo
1344 * @error: a #GError location to store the error occuring, %NULL to ignore.
1346 * Creates a new #GAppInfo from the given information.
1348 * Returns: new #GAppInfo for given command.
1351 g_app_info_create_from_commandline (const char *commandline,
1352 const char *application_name,
1353 GAppInfoCreateFlags flags,
1359 char *basename, *exec, *filename, *comment;
1360 char *data, *desktop_id;
1363 GDesktopAppInfo *info;
1366 dirname = ensure_dir (APP_DIR, error);
1370 key_file = g_key_file_new ();
1372 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1373 "Encoding", "UTF-8");
1374 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1375 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1376 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1377 G_KEY_FILE_DESKTOP_KEY_TYPE,
1378 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1379 if (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL)
1380 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1381 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1383 exec = g_strconcat (commandline, " %f", NULL);
1384 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1385 G_KEY_FILE_DESKTOP_KEY_EXEC, exec);
1388 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1389 split = g_strsplit (commandline, " ", 2);
1390 basename = g_path_get_basename (split[0]);
1392 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1393 G_KEY_FILE_DESKTOP_KEY_NAME, application_name?application_name:basename);
1395 comment = g_strdup_printf (_("Custom definition for %s"), basename);
1396 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1397 G_KEY_FILE_DESKTOP_KEY_COMMENT, comment);
1400 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1401 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1403 data = g_key_file_to_data (key_file, &data_size, NULL);
1404 g_key_file_free (key_file);
1406 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", basename);
1408 filename = g_build_filename (dirname, desktop_id, NULL);
1409 g_free (desktop_id);
1412 fd = g_mkstemp (filename);
1417 display_name = g_filename_display_name (filename);
1418 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1419 _("Can't create user desktop file %s"), display_name);
1420 g_free (display_name);
1426 desktop_id = g_path_get_basename (filename);
1430 res = g_file_set_contents (filename, data, data_size, error);
1433 g_free (desktop_id);
1438 run_update_command ("update-desktop-database", "applications");
1440 info = g_desktop_app_info_new_from_filename (filename);
1443 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1444 _("Can't load just created desktop file"));
1446 info->desktop_id = g_strdup (desktop_id);
1448 g_free (desktop_id);
1450 return G_APP_INFO (info);
1455 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1457 iface->dup = g_desktop_app_info_dup;
1458 iface->equal = g_desktop_app_info_equal;
1459 iface->get_id = g_desktop_app_info_get_id;
1460 iface->get_name = g_desktop_app_info_get_name;
1461 iface->get_description = g_desktop_app_info_get_description;
1462 iface->get_executable = g_desktop_app_info_get_executable;
1463 iface->get_icon = g_desktop_app_info_get_icon;
1464 iface->launch = g_desktop_app_info_launch;
1465 iface->supports_uris = g_desktop_app_info_supports_uris;
1466 iface->supports_xdg_startup_notify = g_desktop_app_info_supports_xdg_startup_notify;
1467 iface->launch_uris = g_desktop_app_info_launch_uris;
1468 iface->should_show = g_desktop_app_info_should_show;
1469 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1470 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1471 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1472 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1473 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1477 app_info_in_list (GAppInfo *info, GList *l)
1481 if (g_app_info_equal (info, l->data))
1490 * g_app_info_get_all_for_type:
1491 * @content_type: the content type to find a #GAppInfo for
1493 * Gets a list of all #GAppInfo s for a given content type.
1495 * Returns: #GList of #GAppInfo s for given @content_type.
1498 g_app_info_get_all_for_type (const char *content_type)
1500 GList *desktop_entries, *l;
1502 GDesktopAppInfo *info;
1504 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1507 for (l = desktop_entries; l != NULL; l = l->next)
1509 char *desktop_entry = l->data;
1511 info = g_desktop_app_info_new (desktop_entry);
1514 if (app_info_in_list (G_APP_INFO (info), infos))
1515 g_object_unref (info);
1517 infos = g_list_prepend (infos, info);
1519 g_free (desktop_entry);
1522 g_list_free (desktop_entries);
1524 return g_list_reverse (infos);
1529 * g_app_info_get_default_for_type:
1530 * @content_type: the content type to find a #GAppInfo for
1531 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1534 * Gets the #GAppInfo that correspond to a given content type.
1536 * Returns: #GAppInfo for given @content_type.
1539 g_app_info_get_default_for_type (const char *content_type,
1540 gboolean must_support_uris)
1542 GList *desktop_entries, *l;
1545 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1548 for (l = desktop_entries; l != NULL; l = l->next)
1550 char *desktop_entry = l->data;
1552 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1555 if (must_support_uris && !g_app_info_supports_uris (info))
1557 g_object_unref (info);
1565 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1566 g_list_free (desktop_entries);
1573 * g_app_info_get_default_for_uri_scheme:
1576 * Returns: #GAppInfo
1579 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1581 /* TODO: Implement this using giomodules, reading the gconf settings
1582 * in /desktop/gnome/url-handlers
1589 get_apps_from_dir (GHashTable *apps, const char *dirname, const char *prefix)
1592 const char *basename;
1593 char *filename, *subprefix, *desktop_id;
1595 GDesktopAppInfo *appinfo;
1597 dir = g_dir_open (dirname, 0, NULL);
1600 while ((basename = g_dir_read_name (dir)) != NULL)
1602 filename = g_build_filename (dirname, basename, NULL);
1603 if (g_str_has_suffix (basename, ".desktop"))
1605 desktop_id = g_strconcat (prefix, basename, NULL);
1607 /* Use _extended so we catch NULLs too (hidden) */
1608 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1610 appinfo = g_desktop_app_info_new_from_filename (filename);
1612 /* Don't return apps that don't take arguments */
1614 g_desktop_app_info_get_is_hidden (appinfo) &&
1615 strstr (appinfo->exec,"%U") == NULL &&
1616 strstr (appinfo->exec,"%u") == NULL &&
1617 strstr (appinfo->exec,"%f") == NULL &&
1618 strstr (appinfo->exec,"%F") == NULL)
1620 g_object_unref (appinfo);
1625 if (appinfo != NULL || hidden)
1627 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1631 /* Reuse instead of strdup here */
1632 appinfo->desktop_id = desktop_id;
1637 g_free (desktop_id);
1641 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1643 subprefix = g_strconcat (prefix, basename, "-", NULL);
1644 get_apps_from_dir (apps, filename, subprefix);
1655 collect_apps (gpointer key,
1659 GList **infos = user_data;
1662 *infos = g_list_prepend (*infos, value);
1667 * g_app_info_get_all:
1669 * Returns: a newly allocated #GList of references to #GAppInfo s.
1672 g_app_info_get_all (void)
1674 const char * const *dirs;
1679 dirs = get_applications_search_path ();
1681 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1685 for (i = 0; dirs[i] != NULL; i++)
1686 get_apps_from_dir (apps, dirs[i], "");
1690 g_hash_table_foreach (apps,
1694 g_hash_table_destroy (apps);
1696 return g_list_reverse (infos);
1699 /* Cacheing of mimeinfo.cache and defaults.list files */
1703 GHashTable *mime_info_cache_map;
1704 GHashTable *defaults_list_map;
1705 time_t mime_info_cache_timestamp;
1706 time_t defaults_list_timestamp;
1710 GList *dirs; /* mimeinfo.cache and defaults.list */
1711 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1712 time_t last_stat_time;
1713 guint should_ping_mime_monitor : 1;
1716 static MimeInfoCache *mime_info_cache = NULL;
1717 G_LOCK_DEFINE_STATIC (mime_info_cache);
1719 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1720 const char *mime_type,
1721 char **new_desktop_file_ids);
1723 static MimeInfoCache * mime_info_cache_new (void);
1726 destroy_info_cache_value (gpointer key, GList *value, gpointer data)
1728 g_list_foreach (value, (GFunc)g_free, NULL);
1729 g_list_free (value);
1733 destroy_info_cache_map (GHashTable *info_cache_map)
1735 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1736 g_hash_table_destroy (info_cache_map);
1740 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1741 const char *cache_file,
1747 filename = g_build_filename (dir->path, cache_file, NULL);
1749 if (g_stat (filename, &buf) < 0)
1756 if (buf.st_mtime != *timestamp)
1762 /* Call with lock held */
1764 remove_all (gpointer key,
1773 mime_info_cache_blow_global_cache (void)
1775 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1780 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1784 gchar *filename, **mime_types;
1791 if (dir->mime_info_cache_map != NULL &&
1792 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1793 &dir->mime_info_cache_timestamp))
1796 if (dir->mime_info_cache_map != NULL)
1797 destroy_info_cache_map (dir->mime_info_cache_map);
1799 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1800 (GDestroyNotify) g_free,
1803 key_file = g_key_file_new ();
1805 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1807 if (g_stat (filename, &buf) < 0)
1810 if (dir->mime_info_cache_timestamp > 0)
1811 mime_info_cache->should_ping_mime_monitor = TRUE;
1813 dir->mime_info_cache_timestamp = buf.st_mtime;
1815 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1820 if (load_error != NULL)
1823 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1826 if (load_error != NULL)
1829 for (i = 0; mime_types[i] != NULL; i++)
1831 gchar **desktop_file_ids;
1832 char *unaliased_type;
1833 desktop_file_ids = g_key_file_get_string_list (key_file,
1839 if (desktop_file_ids == NULL)
1842 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1843 mime_info_cache_dir_add_desktop_entries (dir,
1846 g_free (unaliased_type);
1848 g_strfreev (desktop_file_ids);
1851 g_strfreev (mime_types);
1852 g_key_file_free (key_file);
1857 g_key_file_free (key_file);
1859 if (mime_types != NULL)
1860 g_strfreev (mime_types);
1863 g_error_free (load_error);
1867 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
1871 gchar *filename, **mime_types;
1872 char *unaliased_type;
1873 char **desktop_file_ids;
1880 if (dir->defaults_list_map != NULL &&
1881 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
1882 &dir->defaults_list_timestamp))
1885 if (dir->defaults_list_map != NULL)
1886 g_hash_table_destroy (dir->defaults_list_map);
1888 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1889 g_free, (GDestroyNotify)g_strfreev);
1891 key_file = g_key_file_new ();
1893 filename = g_build_filename (dir->path, "defaults.list", NULL);
1894 if (g_stat (filename, &buf) < 0)
1897 if (dir->defaults_list_timestamp > 0)
1898 mime_info_cache->should_ping_mime_monitor = TRUE;
1900 dir->defaults_list_timestamp = buf.st_mtime;
1902 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1906 if (load_error != NULL)
1909 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
1912 if (load_error != NULL)
1915 for (i = 0; mime_types[i] != NULL; i++)
1917 desktop_file_ids = g_key_file_get_string_list (key_file,
1918 DEFAULT_APPLICATIONS_GROUP,
1922 if (desktop_file_ids == NULL)
1925 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1926 g_hash_table_replace (dir->defaults_list_map,
1931 g_strfreev (mime_types);
1932 g_key_file_free (key_file);
1937 g_key_file_free (key_file);
1939 if (mime_types != NULL)
1940 g_strfreev (mime_types);
1943 g_error_free (load_error);
1946 static MimeInfoCacheDir *
1947 mime_info_cache_dir_new (const char *path)
1949 MimeInfoCacheDir *dir;
1951 dir = g_new0 (MimeInfoCacheDir, 1);
1952 dir->path = g_strdup (path);
1958 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
1963 if (dir->mime_info_cache_map != NULL)
1965 destroy_info_cache_map (dir->mime_info_cache_map);
1966 dir->mime_info_cache_map = NULL;
1970 if (dir->defaults_list_map != NULL)
1972 g_hash_table_destroy (dir->defaults_list_map);
1973 dir->defaults_list_map = NULL;
1980 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1981 const char *mime_type,
1982 char **new_desktop_file_ids)
1984 GList *desktop_file_ids;
1987 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
1990 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
1992 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
1993 desktop_file_ids = g_list_append (desktop_file_ids,
1994 g_strdup (new_desktop_file_ids[i]));
1997 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2001 mime_info_cache_init_dir_lists (void)
2003 const char * const *dirs;
2006 mime_info_cache = mime_info_cache_new ();
2008 dirs = get_applications_search_path ();
2010 for (i = 0; dirs[i] != NULL; i++)
2012 MimeInfoCacheDir *dir;
2014 dir = mime_info_cache_dir_new (dirs[i]);
2018 mime_info_cache_dir_init (dir);
2019 mime_info_cache_dir_init_defaults_list (dir);
2021 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2027 mime_info_cache_update_dir_lists (void)
2031 tmp = mime_info_cache->dirs;
2035 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2037 /* No need to do this if we had file monitors... */
2038 mime_info_cache_blow_global_cache ();
2039 mime_info_cache_dir_init (dir);
2040 mime_info_cache_dir_init_defaults_list (dir);
2047 mime_info_cache_init (void)
2049 G_LOCK (mime_info_cache);
2050 if (mime_info_cache == NULL)
2051 mime_info_cache_init_dir_lists ();
2057 if (now >= mime_info_cache->last_stat_time + 10)
2059 mime_info_cache_update_dir_lists ();
2060 mime_info_cache->last_stat_time = now;
2064 if (mime_info_cache->should_ping_mime_monitor)
2066 /* g_idle_add (emit_mime_changed, NULL); */
2067 mime_info_cache->should_ping_mime_monitor = FALSE;
2070 G_UNLOCK (mime_info_cache);
2073 static MimeInfoCache *
2074 mime_info_cache_new (void)
2076 MimeInfoCache *cache;
2078 cache = g_new0 (MimeInfoCache, 1);
2080 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2081 (GDestroyNotify) g_free,
2082 (GDestroyNotify) g_free);
2087 mime_info_cache_free (MimeInfoCache *cache)
2092 g_list_foreach (cache->dirs,
2093 (GFunc) mime_info_cache_dir_free,
2095 g_list_free (cache->dirs);
2096 g_hash_table_destroy (cache->global_defaults_cache);
2101 * mime_info_cache_reload:
2102 * @dir: directory path which needs reloading.
2104 * Reload the mime information for the @dir.
2107 mime_info_cache_reload (const char *dir)
2109 /* FIXME: just reload the dir that needs reloading,
2110 * don't blow the whole cache
2112 if (mime_info_cache != NULL)
2114 G_LOCK (mime_info_cache);
2115 mime_info_cache_free (mime_info_cache);
2116 mime_info_cache = NULL;
2117 G_UNLOCK (mime_info_cache);
2122 append_desktop_entry (GList *list, const char *desktop_entry)
2124 /* Add if not already in list, and valid */
2125 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp))
2126 list = g_list_prepend (list, g_strdup (desktop_entry));
2132 * get_all_desktop_entries_for_mime_type:
2133 * @mime_type: a mime type.
2135 * Returns all the desktop filenames for @mime_type. The desktop files
2136 * are listed in an order so that default applications are listed before
2137 * non-default ones, and handlers for inherited mimetypes are listed
2138 * after the base ones.
2140 * Return value: a #GList containing the desktop filenames containing the
2144 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2146 GList *desktop_entries, *list, *dir_list, *tmp;
2147 MimeInfoCacheDir *dir;
2150 char **default_entries;
2153 mime_info_cache_init ();
2155 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2156 G_LOCK (mime_info_cache);
2158 desktop_entries = NULL;
2159 for (i = 0; mime_types[i] != NULL; i++)
2161 mime_type = mime_types[i];
2163 /* Go through all apps listed as defaults */
2164 for (dir_list = mime_info_cache->dirs;
2166 dir_list = dir_list->next)
2168 dir = dir_list->data;
2169 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2170 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2171 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j]);
2174 /* Go through all entries that support the mimetype */
2175 for (dir_list = mime_info_cache->dirs;
2177 dir_list = dir_list->next) {
2178 dir = dir_list->data;
2180 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2181 for (tmp = list; tmp != NULL; tmp = tmp->next) {
2182 desktop_entries = append_desktop_entry (desktop_entries, tmp->data);
2187 G_UNLOCK (mime_info_cache);
2189 g_strfreev (mime_types);
2191 desktop_entries = g_list_reverse (desktop_entries);
2193 return desktop_entries;
2196 #define __G_DESKTOP_APP_INFO_C__
2197 #include "gioaliasdef.c"