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 #define g_desktop_app_info_get_type _g_desktop_app_info_get_type
76 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
77 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
78 g_desktop_app_info_iface_init))
81 search_path_init (gpointer data)
84 const char * const *data_dirs;
85 const char *user_data_dir;
88 data_dirs = g_get_system_data_dirs ();
89 length = g_strv_length ((char **) data_dirs);
91 args = g_new (char *, length + 2);
94 user_data_dir = g_get_user_data_dir ();
95 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
96 for (i = 0; i < length; i++)
97 args[j++] = g_build_filename (data_dirs[i],
98 "applications", NULL);
104 static const char * const *
105 get_applications_search_path (void)
107 static GOnce once_init = G_ONCE_INIT;
108 return g_once (&once_init, search_path_init, NULL);
112 g_desktop_app_info_finalize (GObject *object)
114 GDesktopAppInfo *info;
116 info = G_DESKTOP_APP_INFO (object);
118 g_free (info->desktop_id);
119 g_free (info->filename);
121 g_free (info->comment);
122 g_free (info->icon_name);
124 g_object_unref (info->icon);
125 g_strfreev (info->only_show_in);
126 g_strfreev (info->not_show_in);
127 g_free (info->try_exec);
129 g_free (info->binary);
132 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
136 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
138 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
140 gobject_class->finalize = g_desktop_app_info_finalize;
144 g_desktop_app_info_init (GDesktopAppInfo *local)
149 * g_desktop_app_info_new_from_filename:
150 * @filename: a string containing a file name.
152 * Returns: a new #GDesktopAppInfo or %NULL on error.
155 _g_desktop_app_info_new_from_filename (const char *filename)
157 GDesktopAppInfo *info;
163 key_file = g_key_file_new ();
165 if (!g_key_file_load_from_file (key_file,
171 start_group = g_key_file_get_start_group (key_file);
172 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
174 g_free (start_group);
177 g_free (start_group);
179 type = g_key_file_get_string (key_file,
180 G_KEY_FILE_DESKTOP_GROUP,
181 G_KEY_FILE_DESKTOP_KEY_TYPE,
183 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
190 try_exec = g_key_file_get_string (key_file,
191 G_KEY_FILE_DESKTOP_GROUP,
192 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
197 t = g_find_program_in_path (try_exec);
206 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
207 info->filename = g_strdup (filename);
209 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
210 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
211 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
212 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
213 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);
214 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);
215 info->try_exec = try_exec;
216 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
217 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
218 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
219 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
220 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
225 if (g_path_is_absolute (info->icon_name))
229 file = g_file_new_for_path (info->icon_name);
230 info->icon = g_file_icon_new (file);
231 g_object_unref (file);
234 info->icon = g_themed_icon_new (info->icon_name);
245 while (*p != ' ' && *p != 0)
248 info->binary = g_strndup (start, p - start);
255 * g_desktop_app_info_new:
258 * Returns: a new #GDesktopAppInfo.
261 _g_desktop_app_info_new (const char *desktop_id)
263 GDesktopAppInfo *appinfo;
264 const char * const *dirs;
267 dirs = get_applications_search_path ();
269 for (i = 0; dirs[i] != NULL; i++)
275 filename = g_build_filename (dirs[i], desktop_id, NULL);
276 appinfo = _g_desktop_app_info_new_from_filename (filename);
283 basename = g_strdup (desktop_id);
285 while ((p = strchr (p, '-')) != NULL)
289 filename = g_build_filename (dirs[i], basename, NULL);
290 appinfo = _g_desktop_app_info_new_from_filename (filename);
305 appinfo->desktop_id = g_strdup (desktop_id);
307 if (_g_desktop_app_info_get_is_hidden (appinfo))
309 g_object_unref (appinfo);
317 g_desktop_app_info_dup (GAppInfo *appinfo)
319 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
320 GDesktopAppInfo *new_info;
322 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
324 new_info->filename = g_strdup (info->filename);
325 new_info->desktop_id = g_strdup (info->desktop_id);
327 new_info->name = g_strdup (info->name);
328 new_info->comment = g_strdup (info->comment);
329 new_info->nodisplay = info->nodisplay;
330 new_info->icon_name = g_strdup (info->icon_name);
331 new_info->icon = g_object_ref (info->icon);
332 new_info->only_show_in = g_strdupv (info->only_show_in);
333 new_info->not_show_in = g_strdupv (info->not_show_in);
334 new_info->try_exec = g_strdup (info->try_exec);
335 new_info->exec = g_strdup (info->exec);
336 new_info->binary = g_strdup (info->binary);
337 new_info->path = g_strdup (info->path);
338 new_info->hidden = info->hidden;
339 new_info->terminal = info->terminal;
340 new_info->startup_notify = info->startup_notify;
342 return G_APP_INFO (new_info);
346 g_desktop_app_info_equal (GAppInfo *appinfo1,
349 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
350 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
352 if (info1->desktop_id == NULL ||
353 info2->desktop_id == NULL)
356 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
360 g_desktop_app_info_get_id (GAppInfo *appinfo)
362 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
364 return info->desktop_id;
368 g_desktop_app_info_get_name (GAppInfo *appinfo)
370 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
372 if (info->name == NULL)
378 * g_desktop_app_info_get_is_hidden:
381 * Returns: %TRUE if hidden, %FALSE otherwise.
384 _g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
390 g_desktop_app_info_get_description (GAppInfo *appinfo)
392 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
394 return info->comment;
398 g_desktop_app_info_get_executable (GAppInfo *appinfo)
400 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
406 g_desktop_app_info_get_icon (GAppInfo *appinfo)
408 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
414 expand_macro_single (char macro, GFile *file)
419 path = g_file_get_path (file);
420 uri = g_file_get_uri (file);
426 result = g_shell_quote (uri);
431 result = g_shell_quote (path);
436 result = g_shell_quote (g_path_get_dirname (path));
441 result = g_shell_quote (g_path_get_basename (path));
452 expand_macro (char macro,
454 GDesktopAppInfo *info,
457 GList *files = *file_list;
460 g_return_if_fail (exec != NULL);
470 expanded = expand_macro_single (macro, files->data);
473 g_string_append (exec, expanded);
487 expanded = expand_macro_single (macro, files->data);
490 g_string_append (exec, expanded);
496 if (files != NULL && expanded)
497 g_string_append_c (exec, ' ');
505 g_string_append (exec, "--icon ");
506 g_string_append (exec, info->icon_name);
512 g_string_append (exec, info->name);
517 g_string_append (exec, info->filename);
520 case 'm': /* deprecated */
524 g_string_append_c (exec, '%');
532 expand_application_parameters (GDesktopAppInfo *info,
538 GList *file_list = *files;
539 const char *p = info->exec;
540 GString *expanded_exec = g_string_new (NULL);
543 if (info->exec == NULL)
545 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
546 _("Desktop file didn't specify Exec field"));
552 if (p[0] == '%' && p[1] != '\0')
554 expand_macro (p[1], expanded_exec, info, files);
558 g_string_append_c (expanded_exec, *p);
563 /* No file substitutions */
564 if (file_list == *files && file_list != NULL)
566 /* If there is no macro default to %f. This is also what KDE does */
567 g_string_append_c (expanded_exec, ' ');
568 expand_macro ('f', expanded_exec, info, files);
571 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
572 g_string_free (expanded_exec, TRUE);
577 prepend_terminal_to_vector (int *argc,
584 char **term_argv = NULL;
589 g_return_val_if_fail (argc != NULL, FALSE);
590 g_return_val_if_fail (argv != NULL, FALSE);
598 /* compute size if not given */
601 for (i = 0; the_argv[i] != NULL; i++)
607 term_argv = g_new0 (char *, 3);
609 check = g_find_program_in_path ("gnome-terminal");
612 term_argv[0] = check;
613 /* Note that gnome-terminal takes -x and
614 * as -e in gnome-terminal is broken we use that. */
615 term_argv[1] = g_strdup ("-x");
620 check = g_find_program_in_path ("nxterm");
622 check = g_find_program_in_path ("color-xterm");
624 check = g_find_program_in_path ("rxvt");
626 check = g_find_program_in_path ("xterm");
628 check = g_find_program_in_path ("dtterm");
631 check = g_strdup ("xterm");
632 g_warning ("couldn't find a terminal, falling back to xterm");
634 term_argv[0] = check;
635 term_argv[1] = g_strdup ("-e");
638 real_argc = term_argc + *argc;
639 real_argv = g_new (char *, real_argc + 1);
641 for (i = 0; i < term_argc; i++)
642 real_argv[i] = term_argv[i];
644 for (j = 0; j < *argc; j++, i++)
645 real_argv[i] = (char *)the_argv[j];
653 /* we use g_free here as we sucked all the inner strings
654 * out from it into real_argv */
659 #endif /* G_OS_WIN32 */
662 /* '=' is the new '\0'.
663 * DO NOT CALL unless at least one string ends with '='
666 is_env (const char *a,
671 if (*a == 0 || *b == 0)
684 /* free with g_strfreev */
686 replace_env_var (char **old_environ,
688 const char *new_value)
690 int length, new_length;
691 int index, new_index;
695 /* do two things at once:
696 * - discover the length of the environment ('length')
697 * - find the location (if any) of the env var ('index')
700 for (length = 0; old_environ[length]; length++)
702 /* if we already have it in our environment, replace */
703 if (is_env (old_environ[length], env_var))
708 /* no current env var, no desired env value.
711 if (new_value == NULL && index == -1)
714 /* in all cases now, we will be using a modified environment.
715 * determine its length and allocated it.
718 * new_index = location to insert, if any
719 * new_length = length of the new array
720 * new_environ = the pointer array for the new environment
723 if (new_value == NULL && index >= 0)
725 /* in this case, we will be removing an entry */
726 new_length = length - 1;
729 else if (new_value != NULL && index < 0)
731 /* in this case, we will be adding an entry to the end */
732 new_length = length + 1;
736 /* in this case, we will be replacing the existing entry */
742 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
743 new_environ[new_length] = NULL;
745 /* now we do the copying.
746 * for each entry in the new environment, we decide what to do
750 for (new_i = 0; new_i < new_length; new_i++)
752 if (new_i == new_index)
754 /* insert our new item */
755 new_environ[new_i] = g_strconcat (env_var,
760 /* if we had an old entry, skip it now */
766 /* if this is the old DESKTOP_STARTUP_ID, skip it */
770 /* copy an old item */
771 new_environ[new_i] = g_strdup (old_environ[i]);
776 g_strfreev (old_environ);
782 dup_list_segment (GList *start,
788 while (start != NULL && start != end)
790 res = g_list_prepend (res, start->data);
794 return g_list_reverse (res);
798 g_desktop_app_info_launch (GAppInfo *appinfo,
800 GAppLaunchContext *launch_context,
803 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
804 gboolean completed = FALSE;
806 GList *launched_files;
813 g_return_val_if_fail (appinfo != NULL, FALSE);
821 if (!expand_application_parameters (info, &files,
822 &argc, &argv, error))
825 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
827 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
828 _("Unable to find terminal required for application"));
835 launched_files = dup_list_segment (old_files, files);
837 display = g_app_launch_context_get_display (launch_context,
842 if (info->startup_notify)
843 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
847 if (display || sn_id)
852 envp = replace_env_var (envp,
857 envp = replace_env_var (envp,
858 "DESKTOP_STARTUP_ID",
864 g_list_free (launched_files);
867 if (!g_spawn_async (info->path, /* working directory */
870 G_SPAWN_SEARCH_PATH /* flags */,
871 NULL /* child_setup */,
873 NULL /* child_pid */,
878 g_app_launch_context_launch_failed (launch_context, sn_id);
892 while (files != NULL);
904 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
906 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
909 (strstr (info->exec, "%u") != NULL) ||
910 (strstr (info->exec, "%U") != NULL);
914 g_desktop_app_info_supports_xdg_startup_notify (GAppInfo *appinfo)
916 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
918 return info->startup_notify;
922 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
924 GAppLaunchContext *launch_context,
934 file = g_file_new_for_uri (uris->data);
936 g_warning ("Invalid uri passed to g_desktop_app_info_launch_uris");
939 files = g_list_prepend (files, file);
942 files = g_list_reverse (files);
944 res = g_desktop_app_info_launch (appinfo, files, launch_context, error);
946 g_list_foreach (files, (GFunc)g_object_unref, NULL);
953 g_desktop_app_info_should_show (GAppInfo *appinfo,
954 const char *desktop_env)
956 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
963 if (info->only_show_in)
965 if (desktop_env == NULL)
969 for (i = 0; info->only_show_in[i] != NULL; i++)
971 if (strcmp (info->only_show_in[i], desktop_env) == 0)
981 if (info->not_show_in && desktop_env)
983 for (i = 0; info->not_show_in[i] != NULL; i++)
985 if (strcmp (info->not_show_in[i], desktop_env) == 0)
999 ensure_dir (DirType type,
1002 char *path, *display_name;
1005 if (type == APP_DIR)
1006 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1008 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)
1017 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1018 _("Can't create user application configuration folder %s: %s"),
1019 display_name, g_strerror (err));
1021 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1022 _("Can't create user MIME configuration folder %s: %s"),
1023 display_name, g_strerror (err));
1025 g_free (display_name);
1032 update_default_list (const char *desktop_id,
1033 const char *content_type,
1037 char *dirname, *filename;
1039 gboolean load_succeeded, res;
1042 gsize length, data_size;
1046 dirname = ensure_dir (APP_DIR, error);
1050 filename = g_build_filename (dirname, "defaults.list", NULL);
1053 key_file = g_key_file_new ();
1054 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1055 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1057 g_key_file_free (key_file);
1058 key_file = g_key_file_new ();
1062 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1063 content_type, &length, NULL);
1065 list = g_new (char *, 1 + length + 1);
1069 list[i++] = g_strdup (desktop_id);
1072 for (j = 0; old_list[j] != NULL; j++)
1074 if (strcmp (old_list[j], desktop_id) != 0)
1075 list[i++] = g_strdup (old_list[j]);
1080 g_strfreev (old_list);
1082 g_key_file_set_string_list (key_file,
1083 DEFAULT_APPLICATIONS_GROUP,
1085 (const char * const *)list, i);
1089 data = g_key_file_to_data (key_file, &data_size, error);
1090 g_key_file_free (key_file);
1092 res = g_file_set_contents (filename, data, data_size, error);
1094 mime_info_cache_reload (NULL);
1103 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1104 const char *content_type,
1107 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1109 if (!g_app_info_add_supports_type (appinfo, content_type, error))
1112 return update_default_list (info->desktop_id, content_type, TRUE, error);
1116 update_program_done (GPid pid,
1120 /* Did the application exit correctly */
1121 if (WIFEXITED (status) &&
1122 WEXITSTATUS (status) == 0)
1124 /* Here we could clean out any caches in use */
1129 run_update_command (char *command,
1138 GError *error = NULL;
1141 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1143 if (g_spawn_async ("/", argv,
1145 G_SPAWN_SEARCH_PATH |
1146 G_SPAWN_STDOUT_TO_DEV_NULL |
1147 G_SPAWN_STDERR_TO_DEV_NULL |
1148 G_SPAWN_DO_NOT_REAP_CHILD,
1149 NULL, NULL, /* No setup function */
1152 g_child_watch_add (pid, update_program_done, NULL);
1155 /* If we get an error at this point, it's quite likely the user doesn't
1156 * have an installed copy of either 'update-mime-database' or
1157 * 'update-desktop-database'. I don't think we want to popup an error
1158 * dialog at this point, so we just do a g_warning to give the user a
1159 * chance of debugging it.
1161 g_warning ("%s", error->message);
1168 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1169 const char *extension,
1172 char *filename, *basename, *mimetype;
1176 dirname = ensure_dir (MIMETYPE_DIR, error);
1180 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1181 filename = g_build_filename (dirname, basename, NULL);
1185 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1187 if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
1191 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1192 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1193 " <mime-type type=\"%s\">\n"
1194 " <comment>%s document</comment>\n"
1195 " <glob pattern=\"*.%s\"/>\n"
1197 "</mime-info>\n", mimetype, extension, extension);
1199 g_file_set_contents (filename, contents, -1, NULL);
1202 run_update_command ("update-mime-database", "mime");
1206 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1216 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1217 const char *content_type,
1220 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1222 char *new_mimetypes, *old_mimetypes, *content;
1226 keyfile = g_key_file_new ();
1227 if (!g_key_file_load_from_file (keyfile, info->filename,
1228 G_KEY_FILE_KEEP_COMMENTS |
1229 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1231 g_key_file_free (keyfile);
1235 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1236 new_mimetypes = g_strconcat (content_type, ";", old_mimetypes, NULL);
1237 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1238 g_free (old_mimetypes);
1239 g_free (new_mimetypes);
1241 content = g_key_file_to_data (keyfile, NULL, NULL);
1242 g_key_file_free (keyfile);
1244 dirname = ensure_dir (APP_DIR, error);
1251 filename = g_build_filename (dirname, info->desktop_id, NULL);
1254 if (!g_file_set_contents (filename, content, -1, error))
1263 run_update_command ("update-desktop-database", "applications");
1268 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1270 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1273 user_dirname = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1274 return g_str_has_prefix (info->filename, user_dirname);
1278 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1279 const char *content_type,
1282 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1284 char *new_mimetypes, *old_mimetypes, *content;
1289 keyfile = g_key_file_new ();
1290 if (!g_key_file_load_from_file (keyfile, info->filename,
1291 G_KEY_FILE_KEEP_COMMENTS |
1292 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1294 g_key_file_free (keyfile);
1298 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1299 new_mimetypes = g_strdup (old_mimetypes);
1302 found = strstr (new_mimetypes, content_type);
1303 if (found && *(found + strlen (content_type)) == ';')
1305 char *rest = found + strlen (content_type) + 1;
1306 memmove (found, rest, strlen (rest) + 1);
1308 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1309 g_free (old_mimetypes);
1310 g_free (new_mimetypes);
1312 content = g_key_file_to_data (keyfile, NULL, NULL);
1313 g_key_file_free (keyfile);
1315 dirname = ensure_dir (APP_DIR, error);
1322 filename = g_build_filename (dirname, info->desktop_id, NULL);
1324 if (!g_file_set_contents (filename, content, -1, error))
1333 run_update_command ("update-desktop-database", "applications");
1335 return update_default_list (info->desktop_id, content_type, FALSE, error);
1339 * g_app_info_create_from_commandline:
1340 * @commandline: the commandline to use
1341 * @application_name: the application name, or %NULL to use @commandline
1342 * @flags: flags that can specify details of the created #GAppInfo
1343 * @error: a #GError location to store the error occuring, %NULL to ignore.
1345 * Creates a new #GAppInfo from the given information.
1347 * Returns: new #GAppInfo for given command.
1350 g_app_info_create_from_commandline (const char *commandline,
1351 const char *application_name,
1352 GAppInfoCreateFlags flags,
1358 char *basename, *exec, *filename, *comment;
1359 char *data, *desktop_id;
1362 GDesktopAppInfo *info;
1365 dirname = ensure_dir (APP_DIR, error);
1369 key_file = g_key_file_new ();
1371 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1372 "Encoding", "UTF-8");
1373 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1374 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1375 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1376 G_KEY_FILE_DESKTOP_KEY_TYPE,
1377 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1378 if (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL)
1379 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1380 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1382 exec = g_strconcat (commandline, " %f", NULL);
1383 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1384 G_KEY_FILE_DESKTOP_KEY_EXEC, exec);
1387 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1388 split = g_strsplit (commandline, " ", 2);
1389 basename = g_path_get_basename (split[0]);
1391 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1392 G_KEY_FILE_DESKTOP_KEY_NAME, application_name?application_name:basename);
1394 comment = g_strdup_printf (_("Custom definition for %s"), basename);
1395 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1396 G_KEY_FILE_DESKTOP_KEY_COMMENT, comment);
1399 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1400 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1402 data = g_key_file_to_data (key_file, &data_size, NULL);
1403 g_key_file_free (key_file);
1405 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", basename);
1407 filename = g_build_filename (dirname, desktop_id, NULL);
1408 g_free (desktop_id);
1411 fd = g_mkstemp (filename);
1416 display_name = g_filename_display_name (filename);
1417 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1418 _("Can't create user desktop file %s"), display_name);
1419 g_free (display_name);
1425 desktop_id = g_path_get_basename (filename);
1429 res = g_file_set_contents (filename, data, data_size, error);
1432 g_free (desktop_id);
1437 run_update_command ("update-desktop-database", "applications");
1439 info = _g_desktop_app_info_new_from_filename (filename);
1442 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1443 _("Can't load just created desktop file"));
1445 info->desktop_id = g_strdup (desktop_id);
1447 g_free (desktop_id);
1449 return G_APP_INFO (info);
1454 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1456 iface->dup = g_desktop_app_info_dup;
1457 iface->equal = g_desktop_app_info_equal;
1458 iface->get_id = g_desktop_app_info_get_id;
1459 iface->get_name = g_desktop_app_info_get_name;
1460 iface->get_description = g_desktop_app_info_get_description;
1461 iface->get_executable = g_desktop_app_info_get_executable;
1462 iface->get_icon = g_desktop_app_info_get_icon;
1463 iface->launch = g_desktop_app_info_launch;
1464 iface->supports_uris = g_desktop_app_info_supports_uris;
1465 iface->supports_xdg_startup_notify = g_desktop_app_info_supports_xdg_startup_notify;
1466 iface->launch_uris = g_desktop_app_info_launch_uris;
1467 iface->should_show = g_desktop_app_info_should_show;
1468 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1469 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1470 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1471 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1472 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1476 app_info_in_list (GAppInfo *info,
1479 while (list != NULL)
1481 if (g_app_info_equal (info, list->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,
1590 const char *dirname,
1594 const char *basename;
1595 char *filename, *subprefix, *desktop_id;
1597 GDesktopAppInfo *appinfo;
1599 dir = g_dir_open (dirname, 0, NULL);
1602 while ((basename = g_dir_read_name (dir)) != NULL)
1604 filename = g_build_filename (dirname, basename, NULL);
1605 if (g_str_has_suffix (basename, ".desktop"))
1607 desktop_id = g_strconcat (prefix, basename, NULL);
1609 /* Use _extended so we catch NULLs too (hidden) */
1610 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1612 appinfo = _g_desktop_app_info_new_from_filename (filename);
1614 /* Don't return apps that don't take arguments */
1616 _g_desktop_app_info_get_is_hidden (appinfo) &&
1617 strstr (appinfo->exec,"%U") == NULL &&
1618 strstr (appinfo->exec,"%u") == NULL &&
1619 strstr (appinfo->exec,"%f") == NULL &&
1620 strstr (appinfo->exec,"%F") == NULL)
1622 g_object_unref (appinfo);
1627 if (appinfo != NULL || hidden)
1629 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1633 /* Reuse instead of strdup here */
1634 appinfo->desktop_id = desktop_id;
1639 g_free (desktop_id);
1643 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1645 subprefix = g_strconcat (prefix, basename, "-", NULL);
1646 get_apps_from_dir (apps, filename, subprefix);
1657 collect_apps (gpointer key,
1661 GList **infos = user_data;
1664 *infos = g_list_prepend (*infos, value);
1669 * g_app_info_get_all:
1671 * Returns: a newly allocated #GList of references to #GAppInfo s.
1674 g_app_info_get_all (void)
1676 const char * const *dirs;
1681 dirs = get_applications_search_path ();
1683 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1687 for (i = 0; dirs[i] != NULL; i++)
1688 get_apps_from_dir (apps, dirs[i], "");
1692 g_hash_table_foreach (apps,
1696 g_hash_table_destroy (apps);
1698 return g_list_reverse (infos);
1701 /* Cacheing of mimeinfo.cache and defaults.list files */
1705 GHashTable *mime_info_cache_map;
1706 GHashTable *defaults_list_map;
1707 time_t mime_info_cache_timestamp;
1708 time_t defaults_list_timestamp;
1712 GList *dirs; /* mimeinfo.cache and defaults.list */
1713 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1714 time_t last_stat_time;
1715 guint should_ping_mime_monitor : 1;
1718 static MimeInfoCache *mime_info_cache = NULL;
1719 G_LOCK_DEFINE_STATIC (mime_info_cache);
1721 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1722 const char *mime_type,
1723 char **new_desktop_file_ids);
1725 static MimeInfoCache * mime_info_cache_new (void);
1728 destroy_info_cache_value (gpointer key,
1732 g_list_foreach (value, (GFunc)g_free, NULL);
1733 g_list_free (value);
1737 destroy_info_cache_map (GHashTable *info_cache_map)
1739 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1740 g_hash_table_destroy (info_cache_map);
1744 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1745 const char *cache_file,
1751 filename = g_build_filename (dir->path, cache_file, NULL);
1753 if (g_stat (filename, &buf) < 0)
1760 if (buf.st_mtime != *timestamp)
1766 /* Call with lock held */
1768 remove_all (gpointer key,
1777 mime_info_cache_blow_global_cache (void)
1779 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1784 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1788 gchar *filename, **mime_types;
1795 if (dir->mime_info_cache_map != NULL &&
1796 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1797 &dir->mime_info_cache_timestamp))
1800 if (dir->mime_info_cache_map != NULL)
1801 destroy_info_cache_map (dir->mime_info_cache_map);
1803 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1804 (GDestroyNotify) g_free,
1807 key_file = g_key_file_new ();
1809 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1811 if (g_stat (filename, &buf) < 0)
1814 if (dir->mime_info_cache_timestamp > 0)
1815 mime_info_cache->should_ping_mime_monitor = TRUE;
1817 dir->mime_info_cache_timestamp = buf.st_mtime;
1819 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1824 if (load_error != NULL)
1827 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1830 if (load_error != NULL)
1833 for (i = 0; mime_types[i] != NULL; i++)
1835 gchar **desktop_file_ids;
1836 char *unaliased_type;
1837 desktop_file_ids = g_key_file_get_string_list (key_file,
1843 if (desktop_file_ids == NULL)
1846 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1847 mime_info_cache_dir_add_desktop_entries (dir,
1850 g_free (unaliased_type);
1852 g_strfreev (desktop_file_ids);
1855 g_strfreev (mime_types);
1856 g_key_file_free (key_file);
1861 g_key_file_free (key_file);
1863 if (mime_types != NULL)
1864 g_strfreev (mime_types);
1867 g_error_free (load_error);
1871 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
1875 gchar *filename, **mime_types;
1876 char *unaliased_type;
1877 char **desktop_file_ids;
1884 if (dir->defaults_list_map != NULL &&
1885 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
1886 &dir->defaults_list_timestamp))
1889 if (dir->defaults_list_map != NULL)
1890 g_hash_table_destroy (dir->defaults_list_map);
1892 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1893 g_free, (GDestroyNotify)g_strfreev);
1895 key_file = g_key_file_new ();
1897 filename = g_build_filename (dir->path, "defaults.list", NULL);
1898 if (g_stat (filename, &buf) < 0)
1901 if (dir->defaults_list_timestamp > 0)
1902 mime_info_cache->should_ping_mime_monitor = TRUE;
1904 dir->defaults_list_timestamp = buf.st_mtime;
1906 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1910 if (load_error != NULL)
1913 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
1916 if (load_error != NULL)
1919 for (i = 0; mime_types[i] != NULL; i++)
1921 desktop_file_ids = g_key_file_get_string_list (key_file,
1922 DEFAULT_APPLICATIONS_GROUP,
1926 if (desktop_file_ids == NULL)
1929 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1930 g_hash_table_replace (dir->defaults_list_map,
1935 g_strfreev (mime_types);
1936 g_key_file_free (key_file);
1941 g_key_file_free (key_file);
1943 if (mime_types != NULL)
1944 g_strfreev (mime_types);
1947 g_error_free (load_error);
1950 static MimeInfoCacheDir *
1951 mime_info_cache_dir_new (const char *path)
1953 MimeInfoCacheDir *dir;
1955 dir = g_new0 (MimeInfoCacheDir, 1);
1956 dir->path = g_strdup (path);
1962 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
1967 if (dir->mime_info_cache_map != NULL)
1969 destroy_info_cache_map (dir->mime_info_cache_map);
1970 dir->mime_info_cache_map = NULL;
1974 if (dir->defaults_list_map != NULL)
1976 g_hash_table_destroy (dir->defaults_list_map);
1977 dir->defaults_list_map = NULL;
1984 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1985 const char *mime_type,
1986 char **new_desktop_file_ids)
1988 GList *desktop_file_ids;
1991 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
1994 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
1996 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
1997 desktop_file_ids = g_list_append (desktop_file_ids,
1998 g_strdup (new_desktop_file_ids[i]));
2001 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2005 mime_info_cache_init_dir_lists (void)
2007 const char * const *dirs;
2010 mime_info_cache = mime_info_cache_new ();
2012 dirs = get_applications_search_path ();
2014 for (i = 0; dirs[i] != NULL; i++)
2016 MimeInfoCacheDir *dir;
2018 dir = mime_info_cache_dir_new (dirs[i]);
2022 mime_info_cache_dir_init (dir);
2023 mime_info_cache_dir_init_defaults_list (dir);
2025 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2031 mime_info_cache_update_dir_lists (void)
2035 tmp = mime_info_cache->dirs;
2039 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2041 /* No need to do this if we had file monitors... */
2042 mime_info_cache_blow_global_cache ();
2043 mime_info_cache_dir_init (dir);
2044 mime_info_cache_dir_init_defaults_list (dir);
2051 mime_info_cache_init (void)
2053 G_LOCK (mime_info_cache);
2054 if (mime_info_cache == NULL)
2055 mime_info_cache_init_dir_lists ();
2061 if (now >= mime_info_cache->last_stat_time + 10)
2063 mime_info_cache_update_dir_lists ();
2064 mime_info_cache->last_stat_time = now;
2068 if (mime_info_cache->should_ping_mime_monitor)
2070 /* g_idle_add (emit_mime_changed, NULL); */
2071 mime_info_cache->should_ping_mime_monitor = FALSE;
2074 G_UNLOCK (mime_info_cache);
2077 static MimeInfoCache *
2078 mime_info_cache_new (void)
2080 MimeInfoCache *cache;
2082 cache = g_new0 (MimeInfoCache, 1);
2084 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2085 (GDestroyNotify) g_free,
2086 (GDestroyNotify) g_free);
2091 mime_info_cache_free (MimeInfoCache *cache)
2096 g_list_foreach (cache->dirs,
2097 (GFunc) mime_info_cache_dir_free,
2099 g_list_free (cache->dirs);
2100 g_hash_table_destroy (cache->global_defaults_cache);
2105 * mime_info_cache_reload:
2106 * @dir: directory path which needs reloading.
2108 * Reload the mime information for the @dir.
2111 mime_info_cache_reload (const char *dir)
2113 /* FIXME: just reload the dir that needs reloading,
2114 * don't blow the whole cache
2116 if (mime_info_cache != NULL)
2118 G_LOCK (mime_info_cache);
2119 mime_info_cache_free (mime_info_cache);
2120 mime_info_cache = NULL;
2121 G_UNLOCK (mime_info_cache);
2126 append_desktop_entry (GList *list,
2127 const char *desktop_entry)
2129 /* Add if not already in list, and valid */
2130 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp))
2131 list = g_list_prepend (list, g_strdup (desktop_entry));
2137 * get_all_desktop_entries_for_mime_type:
2138 * @mime_type: a mime type.
2140 * Returns all the desktop filenames for @mime_type. The desktop files
2141 * are listed in an order so that default applications are listed before
2142 * non-default ones, and handlers for inherited mimetypes are listed
2143 * after the base ones.
2145 * Return value: a #GList containing the desktop filenames containing the
2149 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2151 GList *desktop_entries, *list, *dir_list, *tmp;
2152 MimeInfoCacheDir *dir;
2155 char **default_entries;
2158 mime_info_cache_init ();
2160 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2161 G_LOCK (mime_info_cache);
2163 desktop_entries = NULL;
2164 for (i = 0; mime_types[i] != NULL; i++)
2166 mime_type = mime_types[i];
2168 /* Go through all apps listed as defaults */
2169 for (dir_list = mime_info_cache->dirs;
2171 dir_list = dir_list->next)
2173 dir = dir_list->data;
2174 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2175 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2176 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j]);
2179 /* Go through all entries that support the mimetype */
2180 for (dir_list = mime_info_cache->dirs;
2182 dir_list = dir_list->next) {
2183 dir = dir_list->data;
2185 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2186 for (tmp = list; tmp != NULL; tmp = tmp->next) {
2187 desktop_entries = append_desktop_entry (desktop_entries, tmp->data);
2192 G_UNLOCK (mime_info_cache);
2194 g_strfreev (mime_types);
2196 desktop_entries = g_list_reverse (desktop_entries);
2198 return desktop_entries;
2201 #define __G_DESKTOP_APP_INFO_C__
2202 #include "gioaliasdef.c"