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 * Creates a new #GDesktopAppInfo.
153 * Returns: a new #GDesktopAppInfo or %NULL on error.
156 g_desktop_app_info_new_from_filename (const char *filename)
158 GDesktopAppInfo *info;
164 key_file = g_key_file_new ();
166 if (!g_key_file_load_from_file (key_file,
172 start_group = g_key_file_get_start_group (key_file);
173 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
175 g_free (start_group);
178 g_free (start_group);
180 type = g_key_file_get_string (key_file,
181 G_KEY_FILE_DESKTOP_GROUP,
182 G_KEY_FILE_DESKTOP_KEY_TYPE,
184 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
191 try_exec = g_key_file_get_string (key_file,
192 G_KEY_FILE_DESKTOP_GROUP,
193 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
198 t = g_find_program_in_path (try_exec);
207 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
208 info->filename = g_strdup (filename);
210 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
211 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
212 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
213 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
214 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);
215 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);
216 info->try_exec = try_exec;
217 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
218 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
219 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
220 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
221 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
226 if (g_path_is_absolute (info->icon_name))
230 file = g_file_new_for_path (info->icon_name);
231 info->icon = g_file_icon_new (file);
232 g_object_unref (file);
235 info->icon = g_themed_icon_new (info->icon_name);
246 while (*p != ' ' && *p != 0)
249 info->binary = g_strndup (start, p - start);
256 * g_desktop_app_info_new:
257 * @desktop_id: the desktop file id
259 * Creates a new #GDesktopAppInfo.
261 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
264 g_desktop_app_info_new (const char *desktop_id)
266 GDesktopAppInfo *appinfo;
267 const char * const *dirs;
270 dirs = get_applications_search_path ();
272 for (i = 0; dirs[i] != NULL; i++)
278 filename = g_build_filename (dirs[i], desktop_id, NULL);
279 appinfo = g_desktop_app_info_new_from_filename (filename);
286 basename = g_strdup (desktop_id);
288 while ((p = strchr (p, '-')) != NULL)
292 filename = g_build_filename (dirs[i], basename, NULL);
293 appinfo = g_desktop_app_info_new_from_filename (filename);
308 appinfo->desktop_id = g_strdup (desktop_id);
310 if (g_desktop_app_info_get_is_hidden (appinfo))
312 g_object_unref (appinfo);
320 g_desktop_app_info_dup (GAppInfo *appinfo)
322 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
323 GDesktopAppInfo *new_info;
325 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
327 new_info->filename = g_strdup (info->filename);
328 new_info->desktop_id = g_strdup (info->desktop_id);
330 new_info->name = g_strdup (info->name);
331 new_info->comment = g_strdup (info->comment);
332 new_info->nodisplay = info->nodisplay;
333 new_info->icon_name = g_strdup (info->icon_name);
334 new_info->icon = g_object_ref (info->icon);
335 new_info->only_show_in = g_strdupv (info->only_show_in);
336 new_info->not_show_in = g_strdupv (info->not_show_in);
337 new_info->try_exec = g_strdup (info->try_exec);
338 new_info->exec = g_strdup (info->exec);
339 new_info->binary = g_strdup (info->binary);
340 new_info->path = g_strdup (info->path);
341 new_info->hidden = info->hidden;
342 new_info->terminal = info->terminal;
343 new_info->startup_notify = info->startup_notify;
345 return G_APP_INFO (new_info);
349 g_desktop_app_info_equal (GAppInfo *appinfo1,
352 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
353 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
355 if (info1->desktop_id == NULL ||
356 info2->desktop_id == NULL)
359 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
363 g_desktop_app_info_get_id (GAppInfo *appinfo)
365 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
367 return info->desktop_id;
371 g_desktop_app_info_get_name (GAppInfo *appinfo)
373 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
375 if (info->name == NULL)
381 * g_desktop_app_info_get_is_hidden:
382 * @info: a #GDesktopAppInfo.
384 * A desktop file is hidden if the Hidden key in it is
387 * Returns: %TRUE if hidden, %FALSE otherwise.
390 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
396 g_desktop_app_info_get_description (GAppInfo *appinfo)
398 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
400 return info->comment;
404 g_desktop_app_info_get_executable (GAppInfo *appinfo)
406 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
412 g_desktop_app_info_get_icon (GAppInfo *appinfo)
414 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
420 expand_macro_single (char macro, GFile *file)
425 path = g_file_get_path (file);
426 uri = g_file_get_uri (file);
432 result = g_shell_quote (uri);
437 result = g_shell_quote (path);
442 result = g_shell_quote (g_path_get_dirname (path));
447 result = g_shell_quote (g_path_get_basename (path));
458 expand_macro (char macro,
460 GDesktopAppInfo *info,
463 GList *files = *file_list;
466 g_return_if_fail (exec != NULL);
476 expanded = expand_macro_single (macro, files->data);
479 g_string_append (exec, expanded);
493 expanded = expand_macro_single (macro, files->data);
496 g_string_append (exec, expanded);
502 if (files != NULL && expanded)
503 g_string_append_c (exec, ' ');
511 g_string_append (exec, "--icon ");
512 g_string_append (exec, info->icon_name);
518 g_string_append (exec, info->name);
523 g_string_append (exec, info->filename);
526 case 'm': /* deprecated */
530 g_string_append_c (exec, '%');
538 expand_application_parameters (GDesktopAppInfo *info,
544 GList *file_list = *files;
545 const char *p = info->exec;
546 GString *expanded_exec = g_string_new (NULL);
549 if (info->exec == NULL)
551 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
552 _("Desktop file didn't specify Exec field"));
558 if (p[0] == '%' && p[1] != '\0')
560 expand_macro (p[1], expanded_exec, info, files);
564 g_string_append_c (expanded_exec, *p);
569 /* No file substitutions */
570 if (file_list == *files && file_list != NULL)
572 /* If there is no macro default to %f. This is also what KDE does */
573 g_string_append_c (expanded_exec, ' ');
574 expand_macro ('f', expanded_exec, info, files);
577 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
578 g_string_free (expanded_exec, TRUE);
583 prepend_terminal_to_vector (int *argc,
590 char **term_argv = NULL;
595 g_return_val_if_fail (argc != NULL, FALSE);
596 g_return_val_if_fail (argv != NULL, FALSE);
604 /* compute size if not given */
607 for (i = 0; the_argv[i] != NULL; i++)
613 term_argv = g_new0 (char *, 3);
615 check = g_find_program_in_path ("gnome-terminal");
618 term_argv[0] = check;
619 /* Note that gnome-terminal takes -x and
620 * as -e in gnome-terminal is broken we use that. */
621 term_argv[1] = g_strdup ("-x");
626 check = g_find_program_in_path ("nxterm");
628 check = g_find_program_in_path ("color-xterm");
630 check = g_find_program_in_path ("rxvt");
632 check = g_find_program_in_path ("xterm");
634 check = g_find_program_in_path ("dtterm");
637 check = g_strdup ("xterm");
638 g_warning ("couldn't find a terminal, falling back to xterm");
640 term_argv[0] = check;
641 term_argv[1] = g_strdup ("-e");
644 real_argc = term_argc + *argc;
645 real_argv = g_new (char *, real_argc + 1);
647 for (i = 0; i < term_argc; i++)
648 real_argv[i] = term_argv[i];
650 for (j = 0; j < *argc; j++, i++)
651 real_argv[i] = (char *)the_argv[j];
659 /* we use g_free here as we sucked all the inner strings
660 * out from it into real_argv */
665 #endif /* G_OS_WIN32 */
668 /* '=' is the new '\0'.
669 * DO NOT CALL unless at least one string ends with '='
672 is_env (const char *a,
677 if (*a == 0 || *b == 0)
690 /* free with g_strfreev */
692 replace_env_var (char **old_environ,
694 const char *new_value)
696 int length, new_length;
697 int index, new_index;
701 /* do two things at once:
702 * - discover the length of the environment ('length')
703 * - find the location (if any) of the env var ('index')
706 for (length = 0; old_environ[length]; length++)
708 /* if we already have it in our environment, replace */
709 if (is_env (old_environ[length], env_var))
714 /* no current env var, no desired env value.
717 if (new_value == NULL && index == -1)
720 /* in all cases now, we will be using a modified environment.
721 * determine its length and allocated it.
724 * new_index = location to insert, if any
725 * new_length = length of the new array
726 * new_environ = the pointer array for the new environment
729 if (new_value == NULL && index >= 0)
731 /* in this case, we will be removing an entry */
732 new_length = length - 1;
735 else if (new_value != NULL && index < 0)
737 /* in this case, we will be adding an entry to the end */
738 new_length = length + 1;
742 /* in this case, we will be replacing the existing entry */
748 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
749 new_environ[new_length] = NULL;
751 /* now we do the copying.
752 * for each entry in the new environment, we decide what to do
756 for (new_i = 0; new_i < new_length; new_i++)
758 if (new_i == new_index)
760 /* insert our new item */
761 new_environ[new_i] = g_strconcat (env_var,
766 /* if we had an old entry, skip it now */
772 /* if this is the old DESKTOP_STARTUP_ID, skip it */
776 /* copy an old item */
777 new_environ[new_i] = g_strdup (old_environ[i]);
782 g_strfreev (old_environ);
788 dup_list_segment (GList *start,
794 while (start != NULL && start != end)
796 res = g_list_prepend (res, start->data);
800 return g_list_reverse (res);
804 g_desktop_app_info_launch (GAppInfo *appinfo,
806 GAppLaunchContext *launch_context,
809 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
810 gboolean completed = FALSE;
812 GList *launched_files;
819 g_return_val_if_fail (appinfo != NULL, FALSE);
827 if (!expand_application_parameters (info, &files,
828 &argc, &argv, error))
831 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
833 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
834 _("Unable to find terminal required for application"));
841 launched_files = dup_list_segment (old_files, files);
843 display = g_app_launch_context_get_display (launch_context,
848 if (info->startup_notify)
849 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
853 if (display || sn_id)
858 envp = replace_env_var (envp,
863 envp = replace_env_var (envp,
864 "DESKTOP_STARTUP_ID",
870 g_list_free (launched_files);
873 if (!g_spawn_async (info->path, /* working directory */
876 G_SPAWN_SEARCH_PATH /* flags */,
877 NULL /* child_setup */,
879 NULL /* child_pid */,
884 g_app_launch_context_launch_failed (launch_context, sn_id);
898 while (files != NULL);
910 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
912 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
915 (strstr (info->exec, "%u") != NULL) ||
916 (strstr (info->exec, "%U") != NULL);
920 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
922 GAppLaunchContext *launch_context,
932 file = g_file_new_for_uri (uris->data);
934 g_warning ("Invalid uri passed to g_desktop_app_info_launch_uris");
937 files = g_list_prepend (files, file);
940 files = g_list_reverse (files);
942 res = g_desktop_app_info_launch (appinfo, files, launch_context, error);
944 g_list_foreach (files, (GFunc)g_object_unref, NULL);
951 g_desktop_app_info_should_show (GAppInfo *appinfo,
952 const char *desktop_env)
954 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
961 if (info->only_show_in)
963 if (desktop_env == NULL)
967 for (i = 0; info->only_show_in[i] != NULL; i++)
969 if (strcmp (info->only_show_in[i], desktop_env) == 0)
979 if (info->not_show_in && desktop_env)
981 for (i = 0; info->not_show_in[i] != NULL; i++)
983 if (strcmp (info->not_show_in[i], desktop_env) == 0)
997 ensure_dir (DirType type,
1000 char *path, *display_name;
1003 if (type == APP_DIR)
1004 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1006 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1009 if (g_mkdir_with_parents (path, 0700) == 0)
1013 display_name = g_filename_display_name (path);
1014 if (type == APP_DIR)
1015 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1016 _("Can't create user application configuration folder %s: %s"),
1017 display_name, g_strerror (err));
1019 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1020 _("Can't create user MIME configuration folder %s: %s"),
1021 display_name, g_strerror (err));
1023 g_free (display_name);
1030 update_default_list (const char *desktop_id,
1031 const char *content_type,
1035 char *dirname, *filename;
1037 gboolean load_succeeded, res;
1040 gsize length, data_size;
1044 dirname = ensure_dir (APP_DIR, error);
1048 filename = g_build_filename (dirname, "defaults.list", NULL);
1051 key_file = g_key_file_new ();
1052 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1053 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1055 g_key_file_free (key_file);
1056 key_file = g_key_file_new ();
1060 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1061 content_type, &length, NULL);
1063 list = g_new (char *, 1 + length + 1);
1067 list[i++] = g_strdup (desktop_id);
1070 for (j = 0; old_list[j] != NULL; j++)
1072 if (strcmp (old_list[j], desktop_id) != 0)
1073 list[i++] = g_strdup (old_list[j]);
1078 g_strfreev (old_list);
1080 g_key_file_set_string_list (key_file,
1081 DEFAULT_APPLICATIONS_GROUP,
1083 (const char * const *)list, i);
1087 data = g_key_file_to_data (key_file, &data_size, error);
1088 g_key_file_free (key_file);
1090 res = g_file_set_contents (filename, data, data_size, error);
1092 mime_info_cache_reload (NULL);
1101 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1102 const char *content_type,
1105 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1107 if (!g_app_info_add_supports_type (appinfo, content_type, error))
1110 return update_default_list (info->desktop_id, content_type, TRUE, error);
1114 update_program_done (GPid pid,
1118 /* Did the application exit correctly */
1119 if (WIFEXITED (status) &&
1120 WEXITSTATUS (status) == 0)
1122 /* Here we could clean out any caches in use */
1127 run_update_command (char *command,
1136 GError *error = NULL;
1139 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1141 if (g_spawn_async ("/", argv,
1143 G_SPAWN_SEARCH_PATH |
1144 G_SPAWN_STDOUT_TO_DEV_NULL |
1145 G_SPAWN_STDERR_TO_DEV_NULL |
1146 G_SPAWN_DO_NOT_REAP_CHILD,
1147 NULL, NULL, /* No setup function */
1150 g_child_watch_add (pid, update_program_done, NULL);
1153 /* If we get an error at this point, it's quite likely the user doesn't
1154 * have an installed copy of either 'update-mime-database' or
1155 * 'update-desktop-database'. I don't think we want to popup an error
1156 * dialog at this point, so we just do a g_warning to give the user a
1157 * chance of debugging it.
1159 g_warning ("%s", error->message);
1166 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1167 const char *extension,
1170 char *filename, *basename, *mimetype;
1174 dirname = ensure_dir (MIMETYPE_DIR, error);
1178 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1179 filename = g_build_filename (dirname, basename, NULL);
1183 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1185 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1190 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1191 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1192 " <mime-type type=\"%s\">\n"
1193 " <comment>%s document</comment>\n"
1194 " <glob pattern=\"*.%s\"/>\n"
1196 "</mime-info>\n", mimetype, extension, extension);
1198 g_file_set_contents (filename, contents, -1, NULL);
1201 run_update_command ("update-mime-database", "mime");
1205 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1215 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1216 const char *content_type,
1219 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1221 char *new_mimetypes, *old_mimetypes, *content;
1225 keyfile = g_key_file_new ();
1226 if (!g_key_file_load_from_file (keyfile, info->filename,
1227 G_KEY_FILE_KEEP_COMMENTS |
1228 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1230 g_key_file_free (keyfile);
1234 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1235 new_mimetypes = g_strconcat (content_type, ";", old_mimetypes, NULL);
1236 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1237 g_free (old_mimetypes);
1238 g_free (new_mimetypes);
1240 content = g_key_file_to_data (keyfile, NULL, NULL);
1241 g_key_file_free (keyfile);
1243 dirname = ensure_dir (APP_DIR, error);
1250 filename = g_build_filename (dirname, info->desktop_id, NULL);
1253 if (!g_file_set_contents (filename, content, -1, error))
1262 run_update_command ("update-desktop-database", "applications");
1267 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1269 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1272 user_dirname = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1273 return g_str_has_prefix (info->filename, user_dirname);
1277 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1278 const char *content_type,
1281 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1283 char *new_mimetypes, *old_mimetypes, *content;
1288 keyfile = g_key_file_new ();
1289 if (!g_key_file_load_from_file (keyfile, info->filename,
1290 G_KEY_FILE_KEEP_COMMENTS |
1291 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1293 g_key_file_free (keyfile);
1297 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1298 new_mimetypes = g_strdup (old_mimetypes);
1301 found = strstr (new_mimetypes, content_type);
1302 if (found && *(found + strlen (content_type)) == ';')
1304 char *rest = found + strlen (content_type) + 1;
1305 memmove (found, rest, strlen (rest) + 1);
1307 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1308 g_free (old_mimetypes);
1309 g_free (new_mimetypes);
1311 content = g_key_file_to_data (keyfile, NULL, NULL);
1312 g_key_file_free (keyfile);
1314 dirname = ensure_dir (APP_DIR, error);
1321 filename = g_build_filename (dirname, info->desktop_id, NULL);
1323 if (!g_file_set_contents (filename, content, -1, error))
1332 run_update_command ("update-desktop-database", "applications");
1334 return update_default_list (info->desktop_id, content_type, FALSE, error);
1338 * g_app_info_create_from_commandline:
1339 * @commandline: the commandline to use
1340 * @application_name: the application name, or %NULL to use @commandline
1341 * @flags: flags that can specify details of the created #GAppInfo
1342 * @error: a #GError location to store the error occuring, %NULL to ignore.
1344 * Creates a new #GAppInfo from the given information.
1346 * Returns: new #GAppInfo for given command.
1349 g_app_info_create_from_commandline (const char *commandline,
1350 const char *application_name,
1351 GAppInfoCreateFlags flags,
1357 char *basename, *exec, *filename, *comment;
1358 char *data, *desktop_id;
1361 GDesktopAppInfo *info;
1364 dirname = ensure_dir (APP_DIR, error);
1368 key_file = g_key_file_new ();
1370 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1371 "Encoding", "UTF-8");
1372 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1373 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1374 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1375 G_KEY_FILE_DESKTOP_KEY_TYPE,
1376 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1377 if (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL)
1378 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1379 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1381 exec = g_strconcat (commandline, " %f", NULL);
1382 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1383 G_KEY_FILE_DESKTOP_KEY_EXEC, exec);
1386 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1387 split = g_strsplit (commandline, " ", 2);
1388 basename = g_path_get_basename (split[0]);
1390 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1391 G_KEY_FILE_DESKTOP_KEY_NAME, application_name?application_name:basename);
1393 comment = g_strdup_printf (_("Custom definition for %s"), basename);
1394 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1395 G_KEY_FILE_DESKTOP_KEY_COMMENT, comment);
1398 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1399 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1401 data = g_key_file_to_data (key_file, &data_size, NULL);
1402 g_key_file_free (key_file);
1404 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", basename);
1406 filename = g_build_filename (dirname, desktop_id, NULL);
1407 g_free (desktop_id);
1410 fd = g_mkstemp (filename);
1415 display_name = g_filename_display_name (filename);
1416 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1417 _("Can't create user desktop file %s"), display_name);
1418 g_free (display_name);
1424 desktop_id = g_path_get_basename (filename);
1428 res = g_file_set_contents (filename, data, data_size, error);
1431 g_free (desktop_id);
1436 run_update_command ("update-desktop-database", "applications");
1438 info = g_desktop_app_info_new_from_filename (filename);
1441 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1442 _("Can't load just created desktop file"));
1444 info->desktop_id = g_strdup (desktop_id);
1446 g_free (desktop_id);
1448 return G_APP_INFO (info);
1453 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1455 iface->dup = g_desktop_app_info_dup;
1456 iface->equal = g_desktop_app_info_equal;
1457 iface->get_id = g_desktop_app_info_get_id;
1458 iface->get_name = g_desktop_app_info_get_name;
1459 iface->get_description = g_desktop_app_info_get_description;
1460 iface->get_executable = g_desktop_app_info_get_executable;
1461 iface->get_icon = g_desktop_app_info_get_icon;
1462 iface->launch = g_desktop_app_info_launch;
1463 iface->supports_uris = g_desktop_app_info_supports_uris;
1464 iface->launch_uris = g_desktop_app_info_launch_uris;
1465 iface->should_show = g_desktop_app_info_should_show;
1466 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1467 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1468 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1469 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1470 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1474 app_info_in_list (GAppInfo *info,
1477 while (list != NULL)
1479 if (g_app_info_equal (info, list->data))
1488 * g_app_info_get_all_for_type:
1489 * @content_type: the content type to find a #GAppInfo for
1491 * Gets a list of all #GAppInfo s for a given content type.
1493 * Returns: #GList of #GAppInfo s for given @content_type.
1496 g_app_info_get_all_for_type (const char *content_type)
1498 GList *desktop_entries, *l;
1500 GDesktopAppInfo *info;
1502 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1505 for (l = desktop_entries; l != NULL; l = l->next)
1507 char *desktop_entry = l->data;
1509 info = g_desktop_app_info_new (desktop_entry);
1512 if (app_info_in_list (G_APP_INFO (info), infos))
1513 g_object_unref (info);
1515 infos = g_list_prepend (infos, info);
1517 g_free (desktop_entry);
1520 g_list_free (desktop_entries);
1522 return g_list_reverse (infos);
1527 * g_app_info_get_default_for_type:
1528 * @content_type: the content type to find a #GAppInfo for
1529 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1532 * Gets the #GAppInfo that correspond to a given content type.
1534 * Returns: #GAppInfo for given @content_type.
1537 g_app_info_get_default_for_type (const char *content_type,
1538 gboolean must_support_uris)
1540 GList *desktop_entries, *l;
1543 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1546 for (l = desktop_entries; l != NULL; l = l->next)
1548 char *desktop_entry = l->data;
1550 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1553 if (must_support_uris && !g_app_info_supports_uris (info))
1555 g_object_unref (info);
1563 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1564 g_list_free (desktop_entries);
1571 * g_app_info_get_default_for_uri_scheme:
1572 * @uri_scheme: a string containing a URI scheme.
1574 * Gets the default application for launching applications using this URI scheme.
1576 * TODO: This is currently unimplemented.
1581 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1583 /* TODO: Implement this using giomodules, reading the gconf settings
1584 * in /desktop/gnome/url-handlers
1591 get_apps_from_dir (GHashTable *apps,
1592 const char *dirname,
1596 const char *basename;
1597 char *filename, *subprefix, *desktop_id;
1599 GDesktopAppInfo *appinfo;
1601 dir = g_dir_open (dirname, 0, NULL);
1604 while ((basename = g_dir_read_name (dir)) != NULL)
1606 filename = g_build_filename (dirname, basename, NULL);
1607 if (g_str_has_suffix (basename, ".desktop"))
1609 desktop_id = g_strconcat (prefix, basename, NULL);
1611 /* Use _extended so we catch NULLs too (hidden) */
1612 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1614 appinfo = g_desktop_app_info_new_from_filename (filename);
1616 /* Don't return apps that don't take arguments */
1618 g_desktop_app_info_get_is_hidden (appinfo) &&
1619 strstr (appinfo->exec,"%U") == NULL &&
1620 strstr (appinfo->exec,"%u") == NULL &&
1621 strstr (appinfo->exec,"%f") == NULL &&
1622 strstr (appinfo->exec,"%F") == NULL)
1624 g_object_unref (appinfo);
1629 if (appinfo != NULL || hidden)
1631 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1635 /* Reuse instead of strdup here */
1636 appinfo->desktop_id = desktop_id;
1641 g_free (desktop_id);
1645 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1647 subprefix = g_strconcat (prefix, basename, "-", NULL);
1648 get_apps_from_dir (apps, filename, subprefix);
1660 * g_app_info_get_all:
1662 * Gets a list of all of the applications currently registered on this system.
1664 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1667 g_app_info_get_all (void)
1669 const char * const *dirs;
1671 GHashTableIter iter;
1676 dirs = get_applications_search_path ();
1678 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1682 for (i = 0; dirs[i] != NULL; i++)
1683 get_apps_from_dir (apps, dirs[i], "");
1687 g_hash_table_iter_init (&iter, apps);
1688 while (g_hash_table_iter_next (&iter, &key, NULL))
1689 infos = g_list_prepend (infos, key);
1691 g_hash_table_destroy (apps);
1693 return g_list_reverse (infos);
1696 /* Cacheing of mimeinfo.cache and defaults.list files */
1700 GHashTable *mime_info_cache_map;
1701 GHashTable *defaults_list_map;
1702 time_t mime_info_cache_timestamp;
1703 time_t defaults_list_timestamp;
1707 GList *dirs; /* mimeinfo.cache and defaults.list */
1708 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1709 time_t last_stat_time;
1710 guint should_ping_mime_monitor : 1;
1713 static MimeInfoCache *mime_info_cache = NULL;
1714 G_LOCK_DEFINE_STATIC (mime_info_cache);
1716 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1717 const char *mime_type,
1718 char **new_desktop_file_ids);
1720 static MimeInfoCache * mime_info_cache_new (void);
1723 destroy_info_cache_value (gpointer key,
1727 g_list_foreach (value, (GFunc)g_free, NULL);
1728 g_list_free (value);
1732 destroy_info_cache_map (GHashTable *info_cache_map)
1734 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1735 g_hash_table_destroy (info_cache_map);
1739 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1740 const char *cache_file,
1746 filename = g_build_filename (dir->path, cache_file, NULL);
1748 if (g_stat (filename, &buf) < 0)
1755 if (buf.st_mtime != *timestamp)
1761 /* Call with lock held */
1763 remove_all (gpointer key,
1772 mime_info_cache_blow_global_cache (void)
1774 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1779 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1783 gchar *filename, **mime_types;
1790 if (dir->mime_info_cache_map != NULL &&
1791 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1792 &dir->mime_info_cache_timestamp))
1795 if (dir->mime_info_cache_map != NULL)
1796 destroy_info_cache_map (dir->mime_info_cache_map);
1798 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1799 (GDestroyNotify) g_free,
1802 key_file = g_key_file_new ();
1804 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1806 if (g_stat (filename, &buf) < 0)
1809 if (dir->mime_info_cache_timestamp > 0)
1810 mime_info_cache->should_ping_mime_monitor = TRUE;
1812 dir->mime_info_cache_timestamp = buf.st_mtime;
1814 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1819 if (load_error != NULL)
1822 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1825 if (load_error != NULL)
1828 for (i = 0; mime_types[i] != NULL; i++)
1830 gchar **desktop_file_ids;
1831 char *unaliased_type;
1832 desktop_file_ids = g_key_file_get_string_list (key_file,
1838 if (desktop_file_ids == NULL)
1841 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1842 mime_info_cache_dir_add_desktop_entries (dir,
1845 g_free (unaliased_type);
1847 g_strfreev (desktop_file_ids);
1850 g_strfreev (mime_types);
1851 g_key_file_free (key_file);
1856 g_key_file_free (key_file);
1858 if (mime_types != NULL)
1859 g_strfreev (mime_types);
1862 g_error_free (load_error);
1866 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
1870 gchar *filename, **mime_types;
1871 char *unaliased_type;
1872 char **desktop_file_ids;
1879 if (dir->defaults_list_map != NULL &&
1880 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
1881 &dir->defaults_list_timestamp))
1884 if (dir->defaults_list_map != NULL)
1885 g_hash_table_destroy (dir->defaults_list_map);
1887 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1888 g_free, (GDestroyNotify)g_strfreev);
1890 key_file = g_key_file_new ();
1892 filename = g_build_filename (dir->path, "defaults.list", NULL);
1893 if (g_stat (filename, &buf) < 0)
1896 if (dir->defaults_list_timestamp > 0)
1897 mime_info_cache->should_ping_mime_monitor = TRUE;
1899 dir->defaults_list_timestamp = buf.st_mtime;
1901 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1905 if (load_error != NULL)
1908 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
1911 if (load_error != NULL)
1914 for (i = 0; mime_types[i] != NULL; i++)
1916 desktop_file_ids = g_key_file_get_string_list (key_file,
1917 DEFAULT_APPLICATIONS_GROUP,
1921 if (desktop_file_ids == NULL)
1924 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1925 g_hash_table_replace (dir->defaults_list_map,
1930 g_strfreev (mime_types);
1931 g_key_file_free (key_file);
1936 g_key_file_free (key_file);
1938 if (mime_types != NULL)
1939 g_strfreev (mime_types);
1942 g_error_free (load_error);
1945 static MimeInfoCacheDir *
1946 mime_info_cache_dir_new (const char *path)
1948 MimeInfoCacheDir *dir;
1950 dir = g_new0 (MimeInfoCacheDir, 1);
1951 dir->path = g_strdup (path);
1957 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
1962 if (dir->mime_info_cache_map != NULL)
1964 destroy_info_cache_map (dir->mime_info_cache_map);
1965 dir->mime_info_cache_map = NULL;
1969 if (dir->defaults_list_map != NULL)
1971 g_hash_table_destroy (dir->defaults_list_map);
1972 dir->defaults_list_map = NULL;
1979 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1980 const char *mime_type,
1981 char **new_desktop_file_ids)
1983 GList *desktop_file_ids;
1986 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
1989 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
1991 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
1992 desktop_file_ids = g_list_append (desktop_file_ids,
1993 g_strdup (new_desktop_file_ids[i]));
1996 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2000 mime_info_cache_init_dir_lists (void)
2002 const char * const *dirs;
2005 mime_info_cache = mime_info_cache_new ();
2007 dirs = get_applications_search_path ();
2009 for (i = 0; dirs[i] != NULL; i++)
2011 MimeInfoCacheDir *dir;
2013 dir = mime_info_cache_dir_new (dirs[i]);
2017 mime_info_cache_dir_init (dir);
2018 mime_info_cache_dir_init_defaults_list (dir);
2020 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2026 mime_info_cache_update_dir_lists (void)
2030 tmp = mime_info_cache->dirs;
2034 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2036 /* No need to do this if we had file monitors... */
2037 mime_info_cache_blow_global_cache ();
2038 mime_info_cache_dir_init (dir);
2039 mime_info_cache_dir_init_defaults_list (dir);
2046 mime_info_cache_init (void)
2048 G_LOCK (mime_info_cache);
2049 if (mime_info_cache == NULL)
2050 mime_info_cache_init_dir_lists ();
2056 if (now >= mime_info_cache->last_stat_time + 10)
2058 mime_info_cache_update_dir_lists ();
2059 mime_info_cache->last_stat_time = now;
2063 if (mime_info_cache->should_ping_mime_monitor)
2065 /* g_idle_add (emit_mime_changed, NULL); */
2066 mime_info_cache->should_ping_mime_monitor = FALSE;
2069 G_UNLOCK (mime_info_cache);
2072 static MimeInfoCache *
2073 mime_info_cache_new (void)
2075 MimeInfoCache *cache;
2077 cache = g_new0 (MimeInfoCache, 1);
2079 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2080 (GDestroyNotify) g_free,
2081 (GDestroyNotify) g_free);
2086 mime_info_cache_free (MimeInfoCache *cache)
2091 g_list_foreach (cache->dirs,
2092 (GFunc) mime_info_cache_dir_free,
2094 g_list_free (cache->dirs);
2095 g_hash_table_destroy (cache->global_defaults_cache);
2100 * mime_info_cache_reload:
2101 * @dir: directory path which needs reloading.
2103 * Reload the mime information for the @dir.
2106 mime_info_cache_reload (const char *dir)
2108 /* FIXME: just reload the dir that needs reloading,
2109 * don't blow the whole cache
2111 if (mime_info_cache != NULL)
2113 G_LOCK (mime_info_cache);
2114 mime_info_cache_free (mime_info_cache);
2115 mime_info_cache = NULL;
2116 G_UNLOCK (mime_info_cache);
2121 append_desktop_entry (GList *list,
2122 const char *desktop_entry)
2124 /* Add if not already in list, and valid */
2125 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp))
2126 list = g_list_prepend (list, g_strdup (desktop_entry));
2132 * get_all_desktop_entries_for_mime_type:
2133 * @mime_type: a mime type.
2135 * Returns all the desktop filenames for @mime_type. The desktop files
2136 * are listed in an order so that default applications are listed before
2137 * non-default ones, and handlers for inherited mimetypes are listed
2138 * after the base ones.
2140 * Return value: a #GList containing the desktop filenames containing the
2144 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2146 GList *desktop_entries, *list, *dir_list, *tmp;
2147 MimeInfoCacheDir *dir;
2150 char **default_entries;
2153 mime_info_cache_init ();
2155 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2156 G_LOCK (mime_info_cache);
2158 desktop_entries = NULL;
2159 for (i = 0; mime_types[i] != NULL; i++)
2161 mime_type = mime_types[i];
2163 /* Go through all apps listed as defaults */
2164 for (dir_list = mime_info_cache->dirs;
2166 dir_list = dir_list->next)
2168 dir = dir_list->data;
2169 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2170 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2171 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j]);
2174 /* Go through all entries that support the mimetype */
2175 for (dir_list = mime_info_cache->dirs;
2177 dir_list = dir_list->next)
2179 dir = dir_list->data;
2181 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2182 for (tmp = list; tmp != NULL; tmp = tmp->next)
2183 desktop_entries = append_desktop_entry (desktop_entries, tmp->data);
2187 G_UNLOCK (mime_info_cache);
2189 g_strfreev (mime_types);
2191 desktop_entries = g_list_reverse (desktop_entries);
2193 return desktop_entries;
2196 #define __G_DESKTOP_APP_INFO_C__
2197 #include "gioaliasdef.c"