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,
453 GDesktopAppInfo *info,
456 GList *files = *file_list;
459 g_return_if_fail (exec != NULL);
469 expanded = expand_macro_single (macro, files->data);
472 g_string_append (exec, expanded);
486 expanded = expand_macro_single (macro, files->data);
489 g_string_append (exec, expanded);
495 if (files != NULL && expanded)
496 g_string_append_c (exec, ' ');
504 g_string_append (exec, "--icon ");
505 g_string_append (exec, info->icon_name);
511 g_string_append (exec, info->name);
516 g_string_append (exec, info->filename);
519 case 'm': /* deprecated */
523 g_string_append_c (exec, '%');
531 expand_application_parameters (GDesktopAppInfo *info,
537 GList *file_list = *files;
538 const char *p = info->exec;
539 GString *expanded_exec = g_string_new (NULL);
542 if (info->exec == NULL)
544 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
545 _("Desktop file didn't specify Exec field"));
551 if (p[0] == '%' && p[1] != '\0')
553 expand_macro (p[1], expanded_exec, info, files);
557 g_string_append_c (expanded_exec, *p);
562 /* No file substitutions */
563 if (file_list == *files && file_list != NULL)
565 /* If there is no macro default to %f. This is also what KDE does */
566 g_string_append_c (expanded_exec, ' ');
567 expand_macro ('f', expanded_exec, info, files);
570 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
571 g_string_free (expanded_exec, TRUE);
576 prepend_terminal_to_vector (int *argc,
583 char **term_argv = NULL;
588 g_return_val_if_fail (argc != NULL, FALSE);
589 g_return_val_if_fail (argv != NULL, FALSE);
597 /* compute size if not given */
600 for (i = 0; the_argv[i] != NULL; i++)
606 term_argv = g_new0 (char *, 3);
608 check = g_find_program_in_path ("gnome-terminal");
611 term_argv[0] = check;
612 /* Note that gnome-terminal takes -x and
613 * as -e in gnome-terminal is broken we use that. */
614 term_argv[1] = g_strdup ("-x");
619 check = g_find_program_in_path ("nxterm");
621 check = g_find_program_in_path ("color-xterm");
623 check = g_find_program_in_path ("rxvt");
625 check = g_find_program_in_path ("xterm");
627 check = g_find_program_in_path ("dtterm");
630 check = g_strdup ("xterm");
631 g_warning ("couldn't find a terminal, falling back to xterm");
633 term_argv[0] = check;
634 term_argv[1] = g_strdup ("-e");
637 real_argc = term_argc + *argc;
638 real_argv = g_new (char *, real_argc + 1);
640 for (i = 0; i < term_argc; i++)
641 real_argv[i] = term_argv[i];
643 for (j = 0; j < *argc; j++, i++)
644 real_argv[i] = (char *)the_argv[j];
652 /* we use g_free here as we sucked all the inner strings
653 * out from it into real_argv */
658 #endif /* G_OS_WIN32 */
661 /* '=' is the new '\0'.
662 * DO NOT CALL unless at least one string ends with '='
665 is_env (const char *a,
670 if (*a == 0 || *b == 0)
683 /* free with g_strfreev */
685 replace_env_var (char **old_environ,
687 const char *new_value)
689 int length, new_length;
690 int index, new_index;
694 /* do two things at once:
695 * - discover the length of the environment ('length')
696 * - find the location (if any) of the env var ('index')
699 for (length = 0; old_environ[length]; length++)
701 /* if we already have it in our environment, replace */
702 if (is_env (old_environ[length], env_var))
707 /* no current env var, no desired env value.
710 if (new_value == NULL && index == -1)
713 /* in all cases now, we will be using a modified environment.
714 * determine its length and allocated it.
717 * new_index = location to insert, if any
718 * new_length = length of the new array
719 * new_environ = the pointer array for the new environment
722 if (new_value == NULL && index >= 0)
724 /* in this case, we will be removing an entry */
725 new_length = length - 1;
728 else if (new_value != NULL && index < 0)
730 /* in this case, we will be adding an entry to the end */
731 new_length = length + 1;
735 /* in this case, we will be replacing the existing entry */
741 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
742 new_environ[new_length] = NULL;
744 /* now we do the copying.
745 * for each entry in the new environment, we decide what to do
749 for (new_i = 0; new_i < new_length; new_i++)
751 if (new_i == new_index)
753 /* insert our new item */
754 new_environ[new_i] = g_strconcat (env_var,
759 /* if we had an old entry, skip it now */
765 /* if this is the old DESKTOP_STARTUP_ID, skip it */
769 /* copy an old item */
770 new_environ[new_i] = g_strdup (old_environ[i]);
775 g_strfreev (old_environ);
781 dup_list_segment (GList *start,
787 while (start != NULL && start != end)
789 res = g_list_prepend (res, start->data);
793 return g_list_reverse (res);
797 g_desktop_app_info_launch (GAppInfo *appinfo,
799 GAppLaunchContext *launch_context,
802 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
803 gboolean completed = FALSE;
805 GList *launched_files;
812 g_return_val_if_fail (appinfo != NULL, FALSE);
820 if (!expand_application_parameters (info, &files,
821 &argc, &argv, error))
824 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
826 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
827 _("Unable to find terminal required for application"));
834 launched_files = dup_list_segment (old_files, files);
836 display = g_app_launch_context_get_display (launch_context,
841 if (info->startup_notify)
842 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
846 if (display || sn_id)
851 envp = replace_env_var (envp,
856 envp = replace_env_var (envp,
857 "DESKTOP_STARTUP_ID",
863 g_list_free (launched_files);
866 if (!g_spawn_async (info->path, /* working directory */
869 G_SPAWN_SEARCH_PATH /* flags */,
870 NULL /* child_setup */,
872 NULL /* child_pid */,
877 g_app_launch_context_launch_failed (launch_context, sn_id);
891 while (files != NULL);
903 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
905 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
908 (strstr (info->exec, "%u") != NULL) ||
909 (strstr (info->exec, "%U") != NULL);
913 g_desktop_app_info_supports_xdg_startup_notify (GAppInfo *appinfo)
915 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
917 return info->startup_notify;
921 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
923 GAppLaunchContext *launch_context,
933 file = g_file_new_for_uri (uris->data);
935 g_warning ("Invalid uri passed to g_desktop_app_info_launch_uris");
938 files = g_list_prepend (files, file);
941 files = g_list_reverse (files);
943 res = g_desktop_app_info_launch (appinfo, files, launch_context, error);
945 g_list_foreach (files, (GFunc)g_object_unref, NULL);
952 g_desktop_app_info_should_show (GAppInfo *appinfo,
953 const char *desktop_env)
955 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
962 if (info->only_show_in)
964 if (desktop_env == NULL)
968 for (i = 0; info->only_show_in[i] != NULL; i++)
970 if (strcmp (info->only_show_in[i], desktop_env) == 0)
980 if (info->not_show_in && desktop_env)
982 for (i = 0; info->not_show_in[i] != NULL; i++)
984 if (strcmp (info->not_show_in[i], desktop_env) == 0)
998 ensure_dir (DirType type,
1001 char *path, *display_name;
1004 if (type == APP_DIR)
1005 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1007 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1010 if (g_mkdir_with_parents (path, 0700) == 0)
1014 display_name = g_filename_display_name (path);
1015 if (type == APP_DIR)
1016 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1017 _("Can't create user application configuration folder %s: %s"),
1018 display_name, g_strerror (err));
1020 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1021 _("Can't create user MIME configuration folder %s: %s"),
1022 display_name, g_strerror (err));
1024 g_free (display_name);
1031 update_default_list (const char *desktop_id,
1032 const char *content_type,
1036 char *dirname, *filename;
1038 gboolean load_succeeded, res;
1041 gsize length, data_size;
1045 dirname = ensure_dir (APP_DIR, error);
1049 filename = g_build_filename (dirname, "defaults.list", NULL);
1052 key_file = g_key_file_new ();
1053 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1054 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1056 g_key_file_free (key_file);
1057 key_file = g_key_file_new ();
1061 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1062 content_type, &length, NULL);
1064 list = g_new (char *, 1 + length + 1);
1068 list[i++] = g_strdup (desktop_id);
1071 for (j = 0; old_list[j] != NULL; j++)
1073 if (strcmp (old_list[j], desktop_id) != 0)
1074 list[i++] = g_strdup (old_list[j]);
1079 g_strfreev (old_list);
1081 g_key_file_set_string_list (key_file,
1082 DEFAULT_APPLICATIONS_GROUP,
1084 (const char * const *)list, i);
1088 data = g_key_file_to_data (key_file, &data_size, error);
1089 g_key_file_free (key_file);
1091 res = g_file_set_contents (filename, data, data_size, error);
1093 mime_info_cache_reload (NULL);
1102 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1103 const char *content_type,
1106 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1108 if (!g_app_info_add_supports_type (appinfo, content_type, error))
1111 return update_default_list (info->desktop_id, content_type, TRUE, error);
1115 update_program_done (GPid pid,
1119 /* Did the application exit correctly */
1120 if (WIFEXITED (status) &&
1121 WEXITSTATUS (status) == 0)
1123 /* Here we could clean out any caches in use */
1128 run_update_command (char *command,
1137 GError *error = NULL;
1140 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1142 if (g_spawn_async ("/", argv,
1144 G_SPAWN_SEARCH_PATH |
1145 G_SPAWN_STDOUT_TO_DEV_NULL |
1146 G_SPAWN_STDERR_TO_DEV_NULL |
1147 G_SPAWN_DO_NOT_REAP_CHILD,
1148 NULL, NULL, /* No setup function */
1151 g_child_watch_add (pid, update_program_done, NULL);
1154 /* If we get an error at this point, it's quite likely the user doesn't
1155 * have an installed copy of either 'update-mime-database' or
1156 * 'update-desktop-database'. I don't think we want to popup an error
1157 * dialog at this point, so we just do a g_warning to give the user a
1158 * chance of debugging it.
1160 g_warning ("%s", error->message);
1167 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1168 const char *extension,
1171 char *filename, *basename, *mimetype;
1175 dirname = ensure_dir (MIMETYPE_DIR, error);
1179 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1180 filename = g_build_filename (dirname, basename, NULL);
1184 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1186 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:
1574 * @uri_scheme: a string containing a URI scheme.
1576 * Gets the default application for launching applications using this URI scheme.
1578 * TODO: This is currently unimplemented.
1583 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1585 /* TODO: Implement this using giomodules, reading the gconf settings
1586 * in /desktop/gnome/url-handlers
1593 get_apps_from_dir (GHashTable *apps,
1594 const char *dirname,
1598 const char *basename;
1599 char *filename, *subprefix, *desktop_id;
1601 GDesktopAppInfo *appinfo;
1603 dir = g_dir_open (dirname, 0, NULL);
1606 while ((basename = g_dir_read_name (dir)) != NULL)
1608 filename = g_build_filename (dirname, basename, NULL);
1609 if (g_str_has_suffix (basename, ".desktop"))
1611 desktop_id = g_strconcat (prefix, basename, NULL);
1613 /* Use _extended so we catch NULLs too (hidden) */
1614 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1616 appinfo = g_desktop_app_info_new_from_filename (filename);
1618 /* Don't return apps that don't take arguments */
1620 g_desktop_app_info_get_is_hidden (appinfo) &&
1621 strstr (appinfo->exec,"%U") == NULL &&
1622 strstr (appinfo->exec,"%u") == NULL &&
1623 strstr (appinfo->exec,"%f") == NULL &&
1624 strstr (appinfo->exec,"%F") == NULL)
1626 g_object_unref (appinfo);
1631 if (appinfo != NULL || hidden)
1633 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1637 /* Reuse instead of strdup here */
1638 appinfo->desktop_id = desktop_id;
1643 g_free (desktop_id);
1647 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1649 subprefix = g_strconcat (prefix, basename, "-", NULL);
1650 get_apps_from_dir (apps, filename, subprefix);
1661 collect_apps (gpointer key,
1665 GList **infos = user_data;
1668 *infos = g_list_prepend (*infos, value);
1673 * g_app_info_get_all:
1675 * Gets a list of all of the applications currently registered on this system.
1677 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1680 g_app_info_get_all (void)
1682 const char * const *dirs;
1687 dirs = get_applications_search_path ();
1689 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1693 for (i = 0; dirs[i] != NULL; i++)
1694 get_apps_from_dir (apps, dirs[i], "");
1698 g_hash_table_foreach (apps,
1702 g_hash_table_destroy (apps);
1704 return g_list_reverse (infos);
1707 /* Cacheing of mimeinfo.cache and defaults.list files */
1711 GHashTable *mime_info_cache_map;
1712 GHashTable *defaults_list_map;
1713 time_t mime_info_cache_timestamp;
1714 time_t defaults_list_timestamp;
1718 GList *dirs; /* mimeinfo.cache and defaults.list */
1719 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1720 time_t last_stat_time;
1721 guint should_ping_mime_monitor : 1;
1724 static MimeInfoCache *mime_info_cache = NULL;
1725 G_LOCK_DEFINE_STATIC (mime_info_cache);
1727 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1728 const char *mime_type,
1729 char **new_desktop_file_ids);
1731 static MimeInfoCache * mime_info_cache_new (void);
1734 destroy_info_cache_value (gpointer key,
1738 g_list_foreach (value, (GFunc)g_free, NULL);
1739 g_list_free (value);
1743 destroy_info_cache_map (GHashTable *info_cache_map)
1745 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1746 g_hash_table_destroy (info_cache_map);
1750 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1751 const char *cache_file,
1757 filename = g_build_filename (dir->path, cache_file, NULL);
1759 if (g_stat (filename, &buf) < 0)
1766 if (buf.st_mtime != *timestamp)
1772 /* Call with lock held */
1774 remove_all (gpointer key,
1783 mime_info_cache_blow_global_cache (void)
1785 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1790 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1794 gchar *filename, **mime_types;
1801 if (dir->mime_info_cache_map != NULL &&
1802 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1803 &dir->mime_info_cache_timestamp))
1806 if (dir->mime_info_cache_map != NULL)
1807 destroy_info_cache_map (dir->mime_info_cache_map);
1809 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1810 (GDestroyNotify) g_free,
1813 key_file = g_key_file_new ();
1815 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1817 if (g_stat (filename, &buf) < 0)
1820 if (dir->mime_info_cache_timestamp > 0)
1821 mime_info_cache->should_ping_mime_monitor = TRUE;
1823 dir->mime_info_cache_timestamp = buf.st_mtime;
1825 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1830 if (load_error != NULL)
1833 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1836 if (load_error != NULL)
1839 for (i = 0; mime_types[i] != NULL; i++)
1841 gchar **desktop_file_ids;
1842 char *unaliased_type;
1843 desktop_file_ids = g_key_file_get_string_list (key_file,
1849 if (desktop_file_ids == NULL)
1852 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1853 mime_info_cache_dir_add_desktop_entries (dir,
1856 g_free (unaliased_type);
1858 g_strfreev (desktop_file_ids);
1861 g_strfreev (mime_types);
1862 g_key_file_free (key_file);
1867 g_key_file_free (key_file);
1869 if (mime_types != NULL)
1870 g_strfreev (mime_types);
1873 g_error_free (load_error);
1877 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
1881 gchar *filename, **mime_types;
1882 char *unaliased_type;
1883 char **desktop_file_ids;
1890 if (dir->defaults_list_map != NULL &&
1891 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
1892 &dir->defaults_list_timestamp))
1895 if (dir->defaults_list_map != NULL)
1896 g_hash_table_destroy (dir->defaults_list_map);
1898 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1899 g_free, (GDestroyNotify)g_strfreev);
1901 key_file = g_key_file_new ();
1903 filename = g_build_filename (dir->path, "defaults.list", NULL);
1904 if (g_stat (filename, &buf) < 0)
1907 if (dir->defaults_list_timestamp > 0)
1908 mime_info_cache->should_ping_mime_monitor = TRUE;
1910 dir->defaults_list_timestamp = buf.st_mtime;
1912 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1916 if (load_error != NULL)
1919 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
1922 if (load_error != NULL)
1925 for (i = 0; mime_types[i] != NULL; i++)
1927 desktop_file_ids = g_key_file_get_string_list (key_file,
1928 DEFAULT_APPLICATIONS_GROUP,
1932 if (desktop_file_ids == NULL)
1935 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1936 g_hash_table_replace (dir->defaults_list_map,
1941 g_strfreev (mime_types);
1942 g_key_file_free (key_file);
1947 g_key_file_free (key_file);
1949 if (mime_types != NULL)
1950 g_strfreev (mime_types);
1953 g_error_free (load_error);
1956 static MimeInfoCacheDir *
1957 mime_info_cache_dir_new (const char *path)
1959 MimeInfoCacheDir *dir;
1961 dir = g_new0 (MimeInfoCacheDir, 1);
1962 dir->path = g_strdup (path);
1968 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
1973 if (dir->mime_info_cache_map != NULL)
1975 destroy_info_cache_map (dir->mime_info_cache_map);
1976 dir->mime_info_cache_map = NULL;
1980 if (dir->defaults_list_map != NULL)
1982 g_hash_table_destroy (dir->defaults_list_map);
1983 dir->defaults_list_map = NULL;
1990 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1991 const char *mime_type,
1992 char **new_desktop_file_ids)
1994 GList *desktop_file_ids;
1997 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2000 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2002 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2003 desktop_file_ids = g_list_append (desktop_file_ids,
2004 g_strdup (new_desktop_file_ids[i]));
2007 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2011 mime_info_cache_init_dir_lists (void)
2013 const char * const *dirs;
2016 mime_info_cache = mime_info_cache_new ();
2018 dirs = get_applications_search_path ();
2020 for (i = 0; dirs[i] != NULL; i++)
2022 MimeInfoCacheDir *dir;
2024 dir = mime_info_cache_dir_new (dirs[i]);
2028 mime_info_cache_dir_init (dir);
2029 mime_info_cache_dir_init_defaults_list (dir);
2031 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2037 mime_info_cache_update_dir_lists (void)
2041 tmp = mime_info_cache->dirs;
2045 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2047 /* No need to do this if we had file monitors... */
2048 mime_info_cache_blow_global_cache ();
2049 mime_info_cache_dir_init (dir);
2050 mime_info_cache_dir_init_defaults_list (dir);
2057 mime_info_cache_init (void)
2059 G_LOCK (mime_info_cache);
2060 if (mime_info_cache == NULL)
2061 mime_info_cache_init_dir_lists ();
2067 if (now >= mime_info_cache->last_stat_time + 10)
2069 mime_info_cache_update_dir_lists ();
2070 mime_info_cache->last_stat_time = now;
2074 if (mime_info_cache->should_ping_mime_monitor)
2076 /* g_idle_add (emit_mime_changed, NULL); */
2077 mime_info_cache->should_ping_mime_monitor = FALSE;
2080 G_UNLOCK (mime_info_cache);
2083 static MimeInfoCache *
2084 mime_info_cache_new (void)
2086 MimeInfoCache *cache;
2088 cache = g_new0 (MimeInfoCache, 1);
2090 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2091 (GDestroyNotify) g_free,
2092 (GDestroyNotify) g_free);
2097 mime_info_cache_free (MimeInfoCache *cache)
2102 g_list_foreach (cache->dirs,
2103 (GFunc) mime_info_cache_dir_free,
2105 g_list_free (cache->dirs);
2106 g_hash_table_destroy (cache->global_defaults_cache);
2111 * mime_info_cache_reload:
2112 * @dir: directory path which needs reloading.
2114 * Reload the mime information for the @dir.
2117 mime_info_cache_reload (const char *dir)
2119 /* FIXME: just reload the dir that needs reloading,
2120 * don't blow the whole cache
2122 if (mime_info_cache != NULL)
2124 G_LOCK (mime_info_cache);
2125 mime_info_cache_free (mime_info_cache);
2126 mime_info_cache = NULL;
2127 G_UNLOCK (mime_info_cache);
2132 append_desktop_entry (GList *list,
2133 const char *desktop_entry)
2135 /* Add if not already in list, and valid */
2136 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp))
2137 list = g_list_prepend (list, g_strdup (desktop_entry));
2143 * get_all_desktop_entries_for_mime_type:
2144 * @mime_type: a mime type.
2146 * Returns all the desktop filenames for @mime_type. The desktop files
2147 * are listed in an order so that default applications are listed before
2148 * non-default ones, and handlers for inherited mimetypes are listed
2149 * after the base ones.
2151 * Return value: a #GList containing the desktop filenames containing the
2155 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2157 GList *desktop_entries, *list, *dir_list, *tmp;
2158 MimeInfoCacheDir *dir;
2161 char **default_entries;
2164 mime_info_cache_init ();
2166 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2167 G_LOCK (mime_info_cache);
2169 desktop_entries = NULL;
2170 for (i = 0; mime_types[i] != NULL; i++)
2172 mime_type = mime_types[i];
2174 /* Go through all apps listed as defaults */
2175 for (dir_list = mime_info_cache->dirs;
2177 dir_list = dir_list->next)
2179 dir = dir_list->data;
2180 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2181 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2182 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j]);
2185 /* Go through all entries that support the mimetype */
2186 for (dir_list = mime_info_cache->dirs;
2188 dir_list = dir_list->next)
2190 dir = dir_list->data;
2192 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2193 for (tmp = list; tmp != NULL; tmp = tmp->next)
2194 desktop_entries = append_desktop_entry (desktop_entries, tmp->data);
2198 G_UNLOCK (mime_info_cache);
2200 g_strfreev (mime_types);
2202 desktop_entries = g_list_reverse (desktop_entries);
2204 return desktop_entries;
2207 #define __G_DESKTOP_APP_INFO_C__
2208 #include "gioaliasdef.c"