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))
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,
1480 while (list != NULL)
1482 if (g_app_info_equal (info, list->data))
1491 * g_app_info_get_all_for_type:
1492 * @content_type: the content type to find a #GAppInfo for
1494 * Gets a list of all #GAppInfo s for a given content type.
1496 * Returns: #GList of #GAppInfo s for given @content_type.
1499 g_app_info_get_all_for_type (const char *content_type)
1501 GList *desktop_entries, *l;
1503 GDesktopAppInfo *info;
1505 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1508 for (l = desktop_entries; l != NULL; l = l->next)
1510 char *desktop_entry = l->data;
1512 info = _g_desktop_app_info_new (desktop_entry);
1515 if (app_info_in_list (G_APP_INFO (info), infos))
1516 g_object_unref (info);
1518 infos = g_list_prepend (infos, info);
1520 g_free (desktop_entry);
1523 g_list_free (desktop_entries);
1525 return g_list_reverse (infos);
1530 * g_app_info_get_default_for_type:
1531 * @content_type: the content type to find a #GAppInfo for
1532 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1535 * Gets the #GAppInfo that correspond to a given content type.
1537 * Returns: #GAppInfo for given @content_type.
1540 g_app_info_get_default_for_type (const char *content_type,
1541 gboolean must_support_uris)
1543 GList *desktop_entries, *l;
1546 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1549 for (l = desktop_entries; l != NULL; l = l->next)
1551 char *desktop_entry = l->data;
1553 info = (GAppInfo *)_g_desktop_app_info_new (desktop_entry);
1556 if (must_support_uris && !g_app_info_supports_uris (info))
1558 g_object_unref (info);
1566 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1567 g_list_free (desktop_entries);
1574 * g_app_info_get_default_for_uri_scheme:
1577 * Returns: #GAppInfo
1580 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1582 /* TODO: Implement this using giomodules, reading the gconf settings
1583 * in /desktop/gnome/url-handlers
1590 get_apps_from_dir (GHashTable *apps,
1591 const char *dirname,
1595 const char *basename;
1596 char *filename, *subprefix, *desktop_id;
1598 GDesktopAppInfo *appinfo;
1600 dir = g_dir_open (dirname, 0, NULL);
1603 while ((basename = g_dir_read_name (dir)) != NULL)
1605 filename = g_build_filename (dirname, basename, NULL);
1606 if (g_str_has_suffix (basename, ".desktop"))
1608 desktop_id = g_strconcat (prefix, basename, NULL);
1610 /* Use _extended so we catch NULLs too (hidden) */
1611 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1613 appinfo = _g_desktop_app_info_new_from_filename (filename);
1615 /* Don't return apps that don't take arguments */
1617 _g_desktop_app_info_get_is_hidden (appinfo) &&
1618 strstr (appinfo->exec,"%U") == NULL &&
1619 strstr (appinfo->exec,"%u") == NULL &&
1620 strstr (appinfo->exec,"%f") == NULL &&
1621 strstr (appinfo->exec,"%F") == NULL)
1623 g_object_unref (appinfo);
1628 if (appinfo != NULL || hidden)
1630 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1634 /* Reuse instead of strdup here */
1635 appinfo->desktop_id = desktop_id;
1640 g_free (desktop_id);
1644 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1646 subprefix = g_strconcat (prefix, basename, "-", NULL);
1647 get_apps_from_dir (apps, filename, subprefix);
1658 collect_apps (gpointer key,
1662 GList **infos = user_data;
1665 *infos = g_list_prepend (*infos, value);
1670 * g_app_info_get_all:
1672 * Returns: a newly allocated #GList of references to #GAppInfo s.
1675 g_app_info_get_all (void)
1677 const char * const *dirs;
1682 dirs = get_applications_search_path ();
1684 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1688 for (i = 0; dirs[i] != NULL; i++)
1689 get_apps_from_dir (apps, dirs[i], "");
1693 g_hash_table_foreach (apps,
1697 g_hash_table_destroy (apps);
1699 return g_list_reverse (infos);
1702 /* Cacheing of mimeinfo.cache and defaults.list files */
1706 GHashTable *mime_info_cache_map;
1707 GHashTable *defaults_list_map;
1708 time_t mime_info_cache_timestamp;
1709 time_t defaults_list_timestamp;
1713 GList *dirs; /* mimeinfo.cache and defaults.list */
1714 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1715 time_t last_stat_time;
1716 guint should_ping_mime_monitor : 1;
1719 static MimeInfoCache *mime_info_cache = NULL;
1720 G_LOCK_DEFINE_STATIC (mime_info_cache);
1722 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1723 const char *mime_type,
1724 char **new_desktop_file_ids);
1726 static MimeInfoCache * mime_info_cache_new (void);
1729 destroy_info_cache_value (gpointer key,
1733 g_list_foreach (value, (GFunc)g_free, NULL);
1734 g_list_free (value);
1738 destroy_info_cache_map (GHashTable *info_cache_map)
1740 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1741 g_hash_table_destroy (info_cache_map);
1745 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1746 const char *cache_file,
1752 filename = g_build_filename (dir->path, cache_file, NULL);
1754 if (g_stat (filename, &buf) < 0)
1761 if (buf.st_mtime != *timestamp)
1767 /* Call with lock held */
1769 remove_all (gpointer key,
1778 mime_info_cache_blow_global_cache (void)
1780 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1785 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1789 gchar *filename, **mime_types;
1796 if (dir->mime_info_cache_map != NULL &&
1797 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1798 &dir->mime_info_cache_timestamp))
1801 if (dir->mime_info_cache_map != NULL)
1802 destroy_info_cache_map (dir->mime_info_cache_map);
1804 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1805 (GDestroyNotify) g_free,
1808 key_file = g_key_file_new ();
1810 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1812 if (g_stat (filename, &buf) < 0)
1815 if (dir->mime_info_cache_timestamp > 0)
1816 mime_info_cache->should_ping_mime_monitor = TRUE;
1818 dir->mime_info_cache_timestamp = buf.st_mtime;
1820 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1825 if (load_error != NULL)
1828 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1831 if (load_error != NULL)
1834 for (i = 0; mime_types[i] != NULL; i++)
1836 gchar **desktop_file_ids;
1837 char *unaliased_type;
1838 desktop_file_ids = g_key_file_get_string_list (key_file,
1844 if (desktop_file_ids == NULL)
1847 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1848 mime_info_cache_dir_add_desktop_entries (dir,
1851 g_free (unaliased_type);
1853 g_strfreev (desktop_file_ids);
1856 g_strfreev (mime_types);
1857 g_key_file_free (key_file);
1862 g_key_file_free (key_file);
1864 if (mime_types != NULL)
1865 g_strfreev (mime_types);
1868 g_error_free (load_error);
1872 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
1876 gchar *filename, **mime_types;
1877 char *unaliased_type;
1878 char **desktop_file_ids;
1885 if (dir->defaults_list_map != NULL &&
1886 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
1887 &dir->defaults_list_timestamp))
1890 if (dir->defaults_list_map != NULL)
1891 g_hash_table_destroy (dir->defaults_list_map);
1893 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1894 g_free, (GDestroyNotify)g_strfreev);
1896 key_file = g_key_file_new ();
1898 filename = g_build_filename (dir->path, "defaults.list", NULL);
1899 if (g_stat (filename, &buf) < 0)
1902 if (dir->defaults_list_timestamp > 0)
1903 mime_info_cache->should_ping_mime_monitor = TRUE;
1905 dir->defaults_list_timestamp = buf.st_mtime;
1907 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1911 if (load_error != NULL)
1914 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
1917 if (load_error != NULL)
1920 for (i = 0; mime_types[i] != NULL; i++)
1922 desktop_file_ids = g_key_file_get_string_list (key_file,
1923 DEFAULT_APPLICATIONS_GROUP,
1927 if (desktop_file_ids == NULL)
1930 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1931 g_hash_table_replace (dir->defaults_list_map,
1936 g_strfreev (mime_types);
1937 g_key_file_free (key_file);
1942 g_key_file_free (key_file);
1944 if (mime_types != NULL)
1945 g_strfreev (mime_types);
1948 g_error_free (load_error);
1951 static MimeInfoCacheDir *
1952 mime_info_cache_dir_new (const char *path)
1954 MimeInfoCacheDir *dir;
1956 dir = g_new0 (MimeInfoCacheDir, 1);
1957 dir->path = g_strdup (path);
1963 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
1968 if (dir->mime_info_cache_map != NULL)
1970 destroy_info_cache_map (dir->mime_info_cache_map);
1971 dir->mime_info_cache_map = NULL;
1975 if (dir->defaults_list_map != NULL)
1977 g_hash_table_destroy (dir->defaults_list_map);
1978 dir->defaults_list_map = NULL;
1985 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1986 const char *mime_type,
1987 char **new_desktop_file_ids)
1989 GList *desktop_file_ids;
1992 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
1995 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
1997 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
1998 desktop_file_ids = g_list_append (desktop_file_ids,
1999 g_strdup (new_desktop_file_ids[i]));
2002 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2006 mime_info_cache_init_dir_lists (void)
2008 const char * const *dirs;
2011 mime_info_cache = mime_info_cache_new ();
2013 dirs = get_applications_search_path ();
2015 for (i = 0; dirs[i] != NULL; i++)
2017 MimeInfoCacheDir *dir;
2019 dir = mime_info_cache_dir_new (dirs[i]);
2023 mime_info_cache_dir_init (dir);
2024 mime_info_cache_dir_init_defaults_list (dir);
2026 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2032 mime_info_cache_update_dir_lists (void)
2036 tmp = mime_info_cache->dirs;
2040 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2042 /* No need to do this if we had file monitors... */
2043 mime_info_cache_blow_global_cache ();
2044 mime_info_cache_dir_init (dir);
2045 mime_info_cache_dir_init_defaults_list (dir);
2052 mime_info_cache_init (void)
2054 G_LOCK (mime_info_cache);
2055 if (mime_info_cache == NULL)
2056 mime_info_cache_init_dir_lists ();
2062 if (now >= mime_info_cache->last_stat_time + 10)
2064 mime_info_cache_update_dir_lists ();
2065 mime_info_cache->last_stat_time = now;
2069 if (mime_info_cache->should_ping_mime_monitor)
2071 /* g_idle_add (emit_mime_changed, NULL); */
2072 mime_info_cache->should_ping_mime_monitor = FALSE;
2075 G_UNLOCK (mime_info_cache);
2078 static MimeInfoCache *
2079 mime_info_cache_new (void)
2081 MimeInfoCache *cache;
2083 cache = g_new0 (MimeInfoCache, 1);
2085 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2086 (GDestroyNotify) g_free,
2087 (GDestroyNotify) g_free);
2092 mime_info_cache_free (MimeInfoCache *cache)
2097 g_list_foreach (cache->dirs,
2098 (GFunc) mime_info_cache_dir_free,
2100 g_list_free (cache->dirs);
2101 g_hash_table_destroy (cache->global_defaults_cache);
2106 * mime_info_cache_reload:
2107 * @dir: directory path which needs reloading.
2109 * Reload the mime information for the @dir.
2112 mime_info_cache_reload (const char *dir)
2114 /* FIXME: just reload the dir that needs reloading,
2115 * don't blow the whole cache
2117 if (mime_info_cache != NULL)
2119 G_LOCK (mime_info_cache);
2120 mime_info_cache_free (mime_info_cache);
2121 mime_info_cache = NULL;
2122 G_UNLOCK (mime_info_cache);
2127 append_desktop_entry (GList *list,
2128 const char *desktop_entry)
2130 /* Add if not already in list, and valid */
2131 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp))
2132 list = g_list_prepend (list, g_strdup (desktop_entry));
2138 * get_all_desktop_entries_for_mime_type:
2139 * @mime_type: a mime type.
2141 * Returns all the desktop filenames for @mime_type. The desktop files
2142 * are listed in an order so that default applications are listed before
2143 * non-default ones, and handlers for inherited mimetypes are listed
2144 * after the base ones.
2146 * Return value: a #GList containing the desktop filenames containing the
2150 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2152 GList *desktop_entries, *list, *dir_list, *tmp;
2153 MimeInfoCacheDir *dir;
2156 char **default_entries;
2159 mime_info_cache_init ();
2161 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2162 G_LOCK (mime_info_cache);
2164 desktop_entries = NULL;
2165 for (i = 0; mime_types[i] != NULL; i++)
2167 mime_type = mime_types[i];
2169 /* Go through all apps listed as defaults */
2170 for (dir_list = mime_info_cache->dirs;
2172 dir_list = dir_list->next)
2174 dir = dir_list->data;
2175 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2176 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2177 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j]);
2180 /* Go through all entries that support the mimetype */
2181 for (dir_list = mime_info_cache->dirs;
2183 dir_list = dir_list->next)
2185 dir = dir_list->data;
2187 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2188 for (tmp = list; tmp != NULL; tmp = tmp->next)
2189 desktop_entries = append_desktop_entry (desktop_entries, tmp->data);
2193 G_UNLOCK (mime_info_cache);
2195 g_strfreev (mime_types);
2197 desktop_entries = g_list_reverse (desktop_entries);
2199 return desktop_entries;
2202 #define __G_DESKTOP_APP_INFO_C__
2203 #include "gioaliasdef.c"