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:
255 * @desktop_id: the desktop file id
257 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
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:
378 * @info: a #GDesktopAppInfo.
380 * A desktop file is hidden if the Hidden key in it is
383 * Returns: %TRUE if hidden, %FALSE otherwise.
386 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
392 g_desktop_app_info_get_description (GAppInfo *appinfo)
394 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
396 return info->comment;
400 g_desktop_app_info_get_executable (GAppInfo *appinfo)
402 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
408 g_desktop_app_info_get_icon (GAppInfo *appinfo)
410 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
416 expand_macro_single (char macro, GFile *file)
421 path = g_file_get_path (file);
422 uri = g_file_get_uri (file);
428 result = g_shell_quote (uri);
433 result = g_shell_quote (path);
438 result = g_shell_quote (g_path_get_dirname (path));
443 result = g_shell_quote (g_path_get_basename (path));
454 expand_macro (char macro,
456 GDesktopAppInfo *info,
459 GList *files = *file_list;
462 g_return_if_fail (exec != NULL);
472 expanded = expand_macro_single (macro, files->data);
475 g_string_append (exec, expanded);
489 expanded = expand_macro_single (macro, files->data);
492 g_string_append (exec, expanded);
498 if (files != NULL && expanded)
499 g_string_append_c (exec, ' ');
507 g_string_append (exec, "--icon ");
508 g_string_append (exec, info->icon_name);
514 g_string_append (exec, info->name);
519 g_string_append (exec, info->filename);
522 case 'm': /* deprecated */
526 g_string_append_c (exec, '%');
534 expand_application_parameters (GDesktopAppInfo *info,
540 GList *file_list = *files;
541 const char *p = info->exec;
542 GString *expanded_exec = g_string_new (NULL);
545 if (info->exec == NULL)
547 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
548 _("Desktop file didn't specify Exec field"));
554 if (p[0] == '%' && p[1] != '\0')
556 expand_macro (p[1], expanded_exec, info, files);
560 g_string_append_c (expanded_exec, *p);
565 /* No file substitutions */
566 if (file_list == *files && file_list != NULL)
568 /* If there is no macro default to %f. This is also what KDE does */
569 g_string_append_c (expanded_exec, ' ');
570 expand_macro ('f', expanded_exec, info, files);
573 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
574 g_string_free (expanded_exec, TRUE);
579 prepend_terminal_to_vector (int *argc,
586 char **term_argv = NULL;
591 g_return_val_if_fail (argc != NULL, FALSE);
592 g_return_val_if_fail (argv != NULL, FALSE);
600 /* compute size if not given */
603 for (i = 0; the_argv[i] != NULL; i++)
609 term_argv = g_new0 (char *, 3);
611 check = g_find_program_in_path ("gnome-terminal");
614 term_argv[0] = check;
615 /* Note that gnome-terminal takes -x and
616 * as -e in gnome-terminal is broken we use that. */
617 term_argv[1] = g_strdup ("-x");
622 check = g_find_program_in_path ("nxterm");
624 check = g_find_program_in_path ("color-xterm");
626 check = g_find_program_in_path ("rxvt");
628 check = g_find_program_in_path ("xterm");
630 check = g_find_program_in_path ("dtterm");
633 check = g_strdup ("xterm");
634 g_warning ("couldn't find a terminal, falling back to xterm");
636 term_argv[0] = check;
637 term_argv[1] = g_strdup ("-e");
640 real_argc = term_argc + *argc;
641 real_argv = g_new (char *, real_argc + 1);
643 for (i = 0; i < term_argc; i++)
644 real_argv[i] = term_argv[i];
646 for (j = 0; j < *argc; j++, i++)
647 real_argv[i] = (char *)the_argv[j];
655 /* we use g_free here as we sucked all the inner strings
656 * out from it into real_argv */
661 #endif /* G_OS_WIN32 */
664 /* '=' is the new '\0'.
665 * DO NOT CALL unless at least one string ends with '='
668 is_env (const char *a,
673 if (*a == 0 || *b == 0)
686 /* free with g_strfreev */
688 replace_env_var (char **old_environ,
690 const char *new_value)
692 int length, new_length;
693 int index, new_index;
697 /* do two things at once:
698 * - discover the length of the environment ('length')
699 * - find the location (if any) of the env var ('index')
702 for (length = 0; old_environ[length]; length++)
704 /* if we already have it in our environment, replace */
705 if (is_env (old_environ[length], env_var))
710 /* no current env var, no desired env value.
713 if (new_value == NULL && index == -1)
716 /* in all cases now, we will be using a modified environment.
717 * determine its length and allocated it.
720 * new_index = location to insert, if any
721 * new_length = length of the new array
722 * new_environ = the pointer array for the new environment
725 if (new_value == NULL && index >= 0)
727 /* in this case, we will be removing an entry */
728 new_length = length - 1;
731 else if (new_value != NULL && index < 0)
733 /* in this case, we will be adding an entry to the end */
734 new_length = length + 1;
738 /* in this case, we will be replacing the existing entry */
744 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
745 new_environ[new_length] = NULL;
747 /* now we do the copying.
748 * for each entry in the new environment, we decide what to do
752 for (new_i = 0; new_i < new_length; new_i++)
754 if (new_i == new_index)
756 /* insert our new item */
757 new_environ[new_i] = g_strconcat (env_var,
762 /* if we had an old entry, skip it now */
768 /* if this is the old DESKTOP_STARTUP_ID, skip it */
772 /* copy an old item */
773 new_environ[new_i] = g_strdup (old_environ[i]);
778 g_strfreev (old_environ);
784 dup_list_segment (GList *start,
790 while (start != NULL && start != end)
792 res = g_list_prepend (res, start->data);
796 return g_list_reverse (res);
800 g_desktop_app_info_launch (GAppInfo *appinfo,
802 GAppLaunchContext *launch_context,
805 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
806 gboolean completed = FALSE;
808 GList *launched_files;
815 g_return_val_if_fail (appinfo != NULL, FALSE);
823 if (!expand_application_parameters (info, &files,
824 &argc, &argv, error))
827 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
829 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
830 _("Unable to find terminal required for application"));
837 launched_files = dup_list_segment (old_files, files);
839 display = g_app_launch_context_get_display (launch_context,
844 if (info->startup_notify)
845 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
849 if (display || sn_id)
854 envp = replace_env_var (envp,
859 envp = replace_env_var (envp,
860 "DESKTOP_STARTUP_ID",
866 g_list_free (launched_files);
869 if (!g_spawn_async (info->path, /* working directory */
872 G_SPAWN_SEARCH_PATH /* flags */,
873 NULL /* child_setup */,
875 NULL /* child_pid */,
880 g_app_launch_context_launch_failed (launch_context, sn_id);
894 while (files != NULL);
906 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
908 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
911 (strstr (info->exec, "%u") != NULL) ||
912 (strstr (info->exec, "%U") != NULL);
916 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
918 GAppLaunchContext *launch_context,
928 file = g_file_new_for_uri (uris->data);
930 g_warning ("Invalid uri passed to g_desktop_app_info_launch_uris");
933 files = g_list_prepend (files, file);
936 files = g_list_reverse (files);
938 res = g_desktop_app_info_launch (appinfo, files, launch_context, error);
940 g_list_foreach (files, (GFunc)g_object_unref, NULL);
947 g_desktop_app_info_should_show (GAppInfo *appinfo,
948 const char *desktop_env)
950 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
957 if (info->only_show_in)
959 if (desktop_env == NULL)
963 for (i = 0; info->only_show_in[i] != NULL; i++)
965 if (strcmp (info->only_show_in[i], desktop_env) == 0)
975 if (info->not_show_in && desktop_env)
977 for (i = 0; info->not_show_in[i] != NULL; i++)
979 if (strcmp (info->not_show_in[i], desktop_env) == 0)
993 ensure_dir (DirType type,
996 char *path, *display_name;
1000 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1002 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1005 if (g_mkdir_with_parents (path, 0700) == 0)
1009 display_name = g_filename_display_name (path);
1010 if (type == APP_DIR)
1011 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1012 _("Can't create user application configuration folder %s: %s"),
1013 display_name, g_strerror (err));
1015 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1016 _("Can't create user MIME configuration folder %s: %s"),
1017 display_name, g_strerror (err));
1019 g_free (display_name);
1026 update_default_list (const char *desktop_id,
1027 const char *content_type,
1031 char *dirname, *filename;
1033 gboolean load_succeeded, res;
1036 gsize length, data_size;
1040 dirname = ensure_dir (APP_DIR, error);
1044 filename = g_build_filename (dirname, "defaults.list", NULL);
1047 key_file = g_key_file_new ();
1048 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1049 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1051 g_key_file_free (key_file);
1052 key_file = g_key_file_new ();
1056 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1057 content_type, &length, NULL);
1059 list = g_new (char *, 1 + length + 1);
1063 list[i++] = g_strdup (desktop_id);
1066 for (j = 0; old_list[j] != NULL; j++)
1068 if (strcmp (old_list[j], desktop_id) != 0)
1069 list[i++] = g_strdup (old_list[j]);
1074 g_strfreev (old_list);
1076 g_key_file_set_string_list (key_file,
1077 DEFAULT_APPLICATIONS_GROUP,
1079 (const char * const *)list, i);
1083 data = g_key_file_to_data (key_file, &data_size, error);
1084 g_key_file_free (key_file);
1086 res = g_file_set_contents (filename, data, data_size, error);
1088 mime_info_cache_reload (NULL);
1097 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1098 const char *content_type,
1101 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1103 if (!g_app_info_add_supports_type (appinfo, content_type, error))
1106 return update_default_list (info->desktop_id, content_type, TRUE, error);
1110 update_program_done (GPid pid,
1114 /* Did the application exit correctly */
1115 if (WIFEXITED (status) &&
1116 WEXITSTATUS (status) == 0)
1118 /* Here we could clean out any caches in use */
1123 run_update_command (char *command,
1132 GError *error = NULL;
1135 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1137 if (g_spawn_async ("/", argv,
1139 G_SPAWN_SEARCH_PATH |
1140 G_SPAWN_STDOUT_TO_DEV_NULL |
1141 G_SPAWN_STDERR_TO_DEV_NULL |
1142 G_SPAWN_DO_NOT_REAP_CHILD,
1143 NULL, NULL, /* No setup function */
1146 g_child_watch_add (pid, update_program_done, NULL);
1149 /* If we get an error at this point, it's quite likely the user doesn't
1150 * have an installed copy of either 'update-mime-database' or
1151 * 'update-desktop-database'. I don't think we want to popup an error
1152 * dialog at this point, so we just do a g_warning to give the user a
1153 * chance of debugging it.
1155 g_warning ("%s", error->message);
1162 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1163 const char *extension,
1166 char *filename, *basename, *mimetype;
1170 dirname = ensure_dir (MIMETYPE_DIR, error);
1174 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1175 filename = g_build_filename (dirname, basename, NULL);
1179 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1181 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1186 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1187 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1188 " <mime-type type=\"%s\">\n"
1189 " <comment>%s document</comment>\n"
1190 " <glob pattern=\"*.%s\"/>\n"
1192 "</mime-info>\n", mimetype, extension, extension);
1194 g_file_set_contents (filename, contents, -1, NULL);
1197 run_update_command ("update-mime-database", "mime");
1201 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1211 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1212 const char *content_type,
1215 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1217 char *new_mimetypes, *old_mimetypes, *content;
1221 keyfile = g_key_file_new ();
1222 if (!g_key_file_load_from_file (keyfile, info->filename,
1223 G_KEY_FILE_KEEP_COMMENTS |
1224 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1226 g_key_file_free (keyfile);
1230 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1231 new_mimetypes = g_strconcat (content_type, ";", old_mimetypes, NULL);
1232 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1233 g_free (old_mimetypes);
1234 g_free (new_mimetypes);
1236 content = g_key_file_to_data (keyfile, NULL, NULL);
1237 g_key_file_free (keyfile);
1239 dirname = ensure_dir (APP_DIR, error);
1246 filename = g_build_filename (dirname, info->desktop_id, NULL);
1249 if (!g_file_set_contents (filename, content, -1, error))
1258 run_update_command ("update-desktop-database", "applications");
1263 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1265 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1268 user_dirname = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1269 return g_str_has_prefix (info->filename, user_dirname);
1273 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1274 const char *content_type,
1277 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1279 char *new_mimetypes, *old_mimetypes, *content;
1284 keyfile = g_key_file_new ();
1285 if (!g_key_file_load_from_file (keyfile, info->filename,
1286 G_KEY_FILE_KEEP_COMMENTS |
1287 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1289 g_key_file_free (keyfile);
1293 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1294 new_mimetypes = g_strdup (old_mimetypes);
1297 found = strstr (new_mimetypes, content_type);
1298 if (found && *(found + strlen (content_type)) == ';')
1300 char *rest = found + strlen (content_type) + 1;
1301 memmove (found, rest, strlen (rest) + 1);
1303 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1304 g_free (old_mimetypes);
1305 g_free (new_mimetypes);
1307 content = g_key_file_to_data (keyfile, NULL, NULL);
1308 g_key_file_free (keyfile);
1310 dirname = ensure_dir (APP_DIR, error);
1317 filename = g_build_filename (dirname, info->desktop_id, NULL);
1319 if (!g_file_set_contents (filename, content, -1, error))
1328 run_update_command ("update-desktop-database", "applications");
1330 return update_default_list (info->desktop_id, content_type, FALSE, error);
1334 * g_app_info_create_from_commandline:
1335 * @commandline: the commandline to use
1336 * @application_name: the application name, or %NULL to use @commandline
1337 * @flags: flags that can specify details of the created #GAppInfo
1338 * @error: a #GError location to store the error occuring, %NULL to ignore.
1340 * Creates a new #GAppInfo from the given information.
1342 * Returns: new #GAppInfo for given command.
1345 g_app_info_create_from_commandline (const char *commandline,
1346 const char *application_name,
1347 GAppInfoCreateFlags flags,
1353 char *basename, *exec, *filename, *comment;
1354 char *data, *desktop_id;
1357 GDesktopAppInfo *info;
1360 dirname = ensure_dir (APP_DIR, error);
1364 key_file = g_key_file_new ();
1366 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1367 "Encoding", "UTF-8");
1368 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1369 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1370 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1371 G_KEY_FILE_DESKTOP_KEY_TYPE,
1372 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1373 if (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL)
1374 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1375 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1377 exec = g_strconcat (commandline, " %f", NULL);
1378 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1379 G_KEY_FILE_DESKTOP_KEY_EXEC, exec);
1382 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1383 split = g_strsplit (commandline, " ", 2);
1384 basename = g_path_get_basename (split[0]);
1386 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1387 G_KEY_FILE_DESKTOP_KEY_NAME, application_name?application_name:basename);
1389 comment = g_strdup_printf (_("Custom definition for %s"), basename);
1390 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1391 G_KEY_FILE_DESKTOP_KEY_COMMENT, comment);
1394 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1395 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1397 data = g_key_file_to_data (key_file, &data_size, NULL);
1398 g_key_file_free (key_file);
1400 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", basename);
1402 filename = g_build_filename (dirname, desktop_id, NULL);
1403 g_free (desktop_id);
1406 fd = g_mkstemp (filename);
1411 display_name = g_filename_display_name (filename);
1412 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1413 _("Can't create user desktop file %s"), display_name);
1414 g_free (display_name);
1420 desktop_id = g_path_get_basename (filename);
1424 res = g_file_set_contents (filename, data, data_size, error);
1427 g_free (desktop_id);
1432 run_update_command ("update-desktop-database", "applications");
1434 info = g_desktop_app_info_new_from_filename (filename);
1437 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1438 _("Can't load just created desktop file"));
1440 info->desktop_id = g_strdup (desktop_id);
1442 g_free (desktop_id);
1444 return G_APP_INFO (info);
1449 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1451 iface->dup = g_desktop_app_info_dup;
1452 iface->equal = g_desktop_app_info_equal;
1453 iface->get_id = g_desktop_app_info_get_id;
1454 iface->get_name = g_desktop_app_info_get_name;
1455 iface->get_description = g_desktop_app_info_get_description;
1456 iface->get_executable = g_desktop_app_info_get_executable;
1457 iface->get_icon = g_desktop_app_info_get_icon;
1458 iface->launch = g_desktop_app_info_launch;
1459 iface->supports_uris = g_desktop_app_info_supports_uris;
1460 iface->launch_uris = g_desktop_app_info_launch_uris;
1461 iface->should_show = g_desktop_app_info_should_show;
1462 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1463 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1464 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1465 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1466 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1470 app_info_in_list (GAppInfo *info,
1473 while (list != NULL)
1475 if (g_app_info_equal (info, list->data))
1484 * g_app_info_get_all_for_type:
1485 * @content_type: the content type to find a #GAppInfo for
1487 * Gets a list of all #GAppInfo s for a given content type.
1489 * Returns: #GList of #GAppInfo s for given @content_type.
1492 g_app_info_get_all_for_type (const char *content_type)
1494 GList *desktop_entries, *l;
1496 GDesktopAppInfo *info;
1498 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1501 for (l = desktop_entries; l != NULL; l = l->next)
1503 char *desktop_entry = l->data;
1505 info = g_desktop_app_info_new (desktop_entry);
1508 if (app_info_in_list (G_APP_INFO (info), infos))
1509 g_object_unref (info);
1511 infos = g_list_prepend (infos, info);
1513 g_free (desktop_entry);
1516 g_list_free (desktop_entries);
1518 return g_list_reverse (infos);
1523 * g_app_info_get_default_for_type:
1524 * @content_type: the content type to find a #GAppInfo for
1525 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1528 * Gets the #GAppInfo that correspond to a given content type.
1530 * Returns: #GAppInfo for given @content_type.
1533 g_app_info_get_default_for_type (const char *content_type,
1534 gboolean must_support_uris)
1536 GList *desktop_entries, *l;
1539 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1542 for (l = desktop_entries; l != NULL; l = l->next)
1544 char *desktop_entry = l->data;
1546 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1549 if (must_support_uris && !g_app_info_supports_uris (info))
1551 g_object_unref (info);
1559 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1560 g_list_free (desktop_entries);
1567 * g_app_info_get_default_for_uri_scheme:
1568 * @uri_scheme: a string containing a URI scheme.
1570 * Gets the default application for launching applications using this URI scheme.
1572 * TODO: This is currently unimplemented.
1577 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1579 /* TODO: Implement this using giomodules, reading the gconf settings
1580 * in /desktop/gnome/url-handlers
1587 get_apps_from_dir (GHashTable *apps,
1588 const char *dirname,
1592 const char *basename;
1593 char *filename, *subprefix, *desktop_id;
1595 GDesktopAppInfo *appinfo;
1597 dir = g_dir_open (dirname, 0, NULL);
1600 while ((basename = g_dir_read_name (dir)) != NULL)
1602 filename = g_build_filename (dirname, basename, NULL);
1603 if (g_str_has_suffix (basename, ".desktop"))
1605 desktop_id = g_strconcat (prefix, basename, NULL);
1607 /* Use _extended so we catch NULLs too (hidden) */
1608 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1610 appinfo = g_desktop_app_info_new_from_filename (filename);
1612 /* Don't return apps that don't take arguments */
1614 g_desktop_app_info_get_is_hidden (appinfo) &&
1615 strstr (appinfo->exec,"%U") == NULL &&
1616 strstr (appinfo->exec,"%u") == NULL &&
1617 strstr (appinfo->exec,"%f") == NULL &&
1618 strstr (appinfo->exec,"%F") == NULL)
1620 g_object_unref (appinfo);
1625 if (appinfo != NULL || hidden)
1627 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1631 /* Reuse instead of strdup here */
1632 appinfo->desktop_id = desktop_id;
1637 g_free (desktop_id);
1641 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1643 subprefix = g_strconcat (prefix, basename, "-", NULL);
1644 get_apps_from_dir (apps, filename, subprefix);
1655 collect_apps (gpointer key,
1659 GList **infos = user_data;
1662 *infos = g_list_prepend (*infos, value);
1667 * g_app_info_get_all:
1669 * Gets a list of all of the applications currently registered on this system.
1671 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1674 g_app_info_get_all (void)
1676 const char * const *dirs;
1681 dirs = get_applications_search_path ();
1683 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1687 for (i = 0; dirs[i] != NULL; i++)
1688 get_apps_from_dir (apps, dirs[i], "");
1692 g_hash_table_foreach (apps,
1696 g_hash_table_destroy (apps);
1698 return g_list_reverse (infos);
1701 /* Cacheing of mimeinfo.cache and defaults.list files */
1705 GHashTable *mime_info_cache_map;
1706 GHashTable *defaults_list_map;
1707 time_t mime_info_cache_timestamp;
1708 time_t defaults_list_timestamp;
1712 GList *dirs; /* mimeinfo.cache and defaults.list */
1713 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1714 time_t last_stat_time;
1715 guint should_ping_mime_monitor : 1;
1718 static MimeInfoCache *mime_info_cache = NULL;
1719 G_LOCK_DEFINE_STATIC (mime_info_cache);
1721 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1722 const char *mime_type,
1723 char **new_desktop_file_ids);
1725 static MimeInfoCache * mime_info_cache_new (void);
1728 destroy_info_cache_value (gpointer key,
1732 g_list_foreach (value, (GFunc)g_free, NULL);
1733 g_list_free (value);
1737 destroy_info_cache_map (GHashTable *info_cache_map)
1739 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1740 g_hash_table_destroy (info_cache_map);
1744 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1745 const char *cache_file,
1751 filename = g_build_filename (dir->path, cache_file, NULL);
1753 if (g_stat (filename, &buf) < 0)
1760 if (buf.st_mtime != *timestamp)
1766 /* Call with lock held */
1768 remove_all (gpointer key,
1777 mime_info_cache_blow_global_cache (void)
1779 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1784 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1788 gchar *filename, **mime_types;
1795 if (dir->mime_info_cache_map != NULL &&
1796 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1797 &dir->mime_info_cache_timestamp))
1800 if (dir->mime_info_cache_map != NULL)
1801 destroy_info_cache_map (dir->mime_info_cache_map);
1803 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1804 (GDestroyNotify) g_free,
1807 key_file = g_key_file_new ();
1809 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1811 if (g_stat (filename, &buf) < 0)
1814 if (dir->mime_info_cache_timestamp > 0)
1815 mime_info_cache->should_ping_mime_monitor = TRUE;
1817 dir->mime_info_cache_timestamp = buf.st_mtime;
1819 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1824 if (load_error != NULL)
1827 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1830 if (load_error != NULL)
1833 for (i = 0; mime_types[i] != NULL; i++)
1835 gchar **desktop_file_ids;
1836 char *unaliased_type;
1837 desktop_file_ids = g_key_file_get_string_list (key_file,
1843 if (desktop_file_ids == NULL)
1846 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1847 mime_info_cache_dir_add_desktop_entries (dir,
1850 g_free (unaliased_type);
1852 g_strfreev (desktop_file_ids);
1855 g_strfreev (mime_types);
1856 g_key_file_free (key_file);
1861 g_key_file_free (key_file);
1863 if (mime_types != NULL)
1864 g_strfreev (mime_types);
1867 g_error_free (load_error);
1871 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
1875 gchar *filename, **mime_types;
1876 char *unaliased_type;
1877 char **desktop_file_ids;
1884 if (dir->defaults_list_map != NULL &&
1885 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
1886 &dir->defaults_list_timestamp))
1889 if (dir->defaults_list_map != NULL)
1890 g_hash_table_destroy (dir->defaults_list_map);
1892 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1893 g_free, (GDestroyNotify)g_strfreev);
1895 key_file = g_key_file_new ();
1897 filename = g_build_filename (dir->path, "defaults.list", NULL);
1898 if (g_stat (filename, &buf) < 0)
1901 if (dir->defaults_list_timestamp > 0)
1902 mime_info_cache->should_ping_mime_monitor = TRUE;
1904 dir->defaults_list_timestamp = buf.st_mtime;
1906 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1910 if (load_error != NULL)
1913 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
1916 if (load_error != NULL)
1919 for (i = 0; mime_types[i] != NULL; i++)
1921 desktop_file_ids = g_key_file_get_string_list (key_file,
1922 DEFAULT_APPLICATIONS_GROUP,
1926 if (desktop_file_ids == NULL)
1929 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1930 g_hash_table_replace (dir->defaults_list_map,
1935 g_strfreev (mime_types);
1936 g_key_file_free (key_file);
1941 g_key_file_free (key_file);
1943 if (mime_types != NULL)
1944 g_strfreev (mime_types);
1947 g_error_free (load_error);
1950 static MimeInfoCacheDir *
1951 mime_info_cache_dir_new (const char *path)
1953 MimeInfoCacheDir *dir;
1955 dir = g_new0 (MimeInfoCacheDir, 1);
1956 dir->path = g_strdup (path);
1962 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
1967 if (dir->mime_info_cache_map != NULL)
1969 destroy_info_cache_map (dir->mime_info_cache_map);
1970 dir->mime_info_cache_map = NULL;
1974 if (dir->defaults_list_map != NULL)
1976 g_hash_table_destroy (dir->defaults_list_map);
1977 dir->defaults_list_map = NULL;
1984 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1985 const char *mime_type,
1986 char **new_desktop_file_ids)
1988 GList *desktop_file_ids;
1991 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
1994 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
1996 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
1997 desktop_file_ids = g_list_append (desktop_file_ids,
1998 g_strdup (new_desktop_file_ids[i]));
2001 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2005 mime_info_cache_init_dir_lists (void)
2007 const char * const *dirs;
2010 mime_info_cache = mime_info_cache_new ();
2012 dirs = get_applications_search_path ();
2014 for (i = 0; dirs[i] != NULL; i++)
2016 MimeInfoCacheDir *dir;
2018 dir = mime_info_cache_dir_new (dirs[i]);
2022 mime_info_cache_dir_init (dir);
2023 mime_info_cache_dir_init_defaults_list (dir);
2025 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2031 mime_info_cache_update_dir_lists (void)
2035 tmp = mime_info_cache->dirs;
2039 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2041 /* No need to do this if we had file monitors... */
2042 mime_info_cache_blow_global_cache ();
2043 mime_info_cache_dir_init (dir);
2044 mime_info_cache_dir_init_defaults_list (dir);
2051 mime_info_cache_init (void)
2053 G_LOCK (mime_info_cache);
2054 if (mime_info_cache == NULL)
2055 mime_info_cache_init_dir_lists ();
2061 if (now >= mime_info_cache->last_stat_time + 10)
2063 mime_info_cache_update_dir_lists ();
2064 mime_info_cache->last_stat_time = now;
2068 if (mime_info_cache->should_ping_mime_monitor)
2070 /* g_idle_add (emit_mime_changed, NULL); */
2071 mime_info_cache->should_ping_mime_monitor = FALSE;
2074 G_UNLOCK (mime_info_cache);
2077 static MimeInfoCache *
2078 mime_info_cache_new (void)
2080 MimeInfoCache *cache;
2082 cache = g_new0 (MimeInfoCache, 1);
2084 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2085 (GDestroyNotify) g_free,
2086 (GDestroyNotify) g_free);
2091 mime_info_cache_free (MimeInfoCache *cache)
2096 g_list_foreach (cache->dirs,
2097 (GFunc) mime_info_cache_dir_free,
2099 g_list_free (cache->dirs);
2100 g_hash_table_destroy (cache->global_defaults_cache);
2105 * mime_info_cache_reload:
2106 * @dir: directory path which needs reloading.
2108 * Reload the mime information for the @dir.
2111 mime_info_cache_reload (const char *dir)
2113 /* FIXME: just reload the dir that needs reloading,
2114 * don't blow the whole cache
2116 if (mime_info_cache != NULL)
2118 G_LOCK (mime_info_cache);
2119 mime_info_cache_free (mime_info_cache);
2120 mime_info_cache = NULL;
2121 G_UNLOCK (mime_info_cache);
2126 append_desktop_entry (GList *list,
2127 const char *desktop_entry)
2129 /* Add if not already in list, and valid */
2130 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp))
2131 list = g_list_prepend (list, g_strdup (desktop_entry));
2137 * get_all_desktop_entries_for_mime_type:
2138 * @mime_type: a mime type.
2140 * Returns all the desktop filenames for @mime_type. The desktop files
2141 * are listed in an order so that default applications are listed before
2142 * non-default ones, and handlers for inherited mimetypes are listed
2143 * after the base ones.
2145 * Return value: a #GList containing the desktop filenames containing the
2149 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2151 GList *desktop_entries, *list, *dir_list, *tmp;
2152 MimeInfoCacheDir *dir;
2155 char **default_entries;
2158 mime_info_cache_init ();
2160 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2161 G_LOCK (mime_info_cache);
2163 desktop_entries = NULL;
2164 for (i = 0; mime_types[i] != NULL; i++)
2166 mime_type = mime_types[i];
2168 /* Go through all apps listed as defaults */
2169 for (dir_list = mime_info_cache->dirs;
2171 dir_list = dir_list->next)
2173 dir = dir_list->data;
2174 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2175 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2176 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j]);
2179 /* Go through all entries that support the mimetype */
2180 for (dir_list = mime_info_cache->dirs;
2182 dir_list = dir_list->next)
2184 dir = dir_list->data;
2186 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2187 for (tmp = list; tmp != NULL; tmp = tmp->next)
2188 desktop_entries = append_desktop_entry (desktop_entries, tmp->data);
2192 G_UNLOCK (mime_info_cache);
2194 g_strfreev (mime_types);
2196 desktop_entries = g_list_reverse (desktop_entries);
2198 return desktop_entries;
2201 #define __G_DESKTOP_APP_INFO_C__
2202 #include "gioaliasdef.c"