From: Alexander Larsson Date: Tue, 29 Jan 2008 14:07:07 +0000 (+0000) Subject: Lazily create the desktop files for appinfos created by X-Git-Url: http://git.openbox.org/?a=commitdiff_plain;h=3b78d8000b97d68ef3490a1804c7624c0298bb66;p=dana%2Fcg-glib.git Lazily create the desktop files for appinfos created by 2008-01-29 Alexander Larsson * gdesktopappinfo.c: Lazily create the desktop files for appinfos created by g_app_info_create_from_commandline() when needed for mime associations. This allows run-time use of GAppInfo object without creating unnecessary files on disk. svn path=/trunk/; revision=6412 --- diff --git a/gio/ChangeLog b/gio/ChangeLog index 84664a5a..762f7440 100644 --- a/gio/ChangeLog +++ b/gio/ChangeLog @@ -1,3 +1,12 @@ +2008-01-29 Alexander Larsson + + * gdesktopappinfo.c: + Lazily create the desktop files for appinfos created + by g_app_info_create_from_commandline() when needed + for mime associations. This allows run-time use + of GAppInfo object without creating unnecessary + files on disk. + 2008-01-29 Alexander Larsson * gio.symbols: diff --git a/gio/gdesktopappinfo.c b/gio/gdesktopappinfo.c index 9f1c2429..b21341d4 100644 --- a/gio/gdesktopappinfo.c +++ b/gio/gdesktopappinfo.c @@ -56,10 +56,11 @@ #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations" #define MIME_CACHE_GROUP "MIME Cache" -static void g_desktop_app_info_iface_init (GAppInfoIface *iface); - -static GList *get_all_desktop_entries_for_mime_type (const char *base_mime_type); -static void mime_info_cache_reload (const char *dir); +static void g_desktop_app_info_iface_init (GAppInfoIface *iface); +static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type); +static void mime_info_cache_reload (const char *dir); +static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info, + GError **error); /** * GDesktopAppInfo: @@ -164,6 +165,22 @@ g_desktop_app_info_init (GDesktopAppInfo *local) { } +static char * +binary_from_exec (const char *exec) +{ + char *p, *start; + + p = exec; + while (*p == ' ') + p++; + start = p; + while (*p != ' ' && *p != 0) + p++; + + return g_strndup (start, p - start); + +} + /** * g_desktop_app_info_new_from_filename: * @filename: a string containing a file name. @@ -264,18 +281,7 @@ g_desktop_app_info_new_from_filename (const char *filename) } if (info->exec) - { - char *p, *start; - - p = info->exec; - while (*p == ' ') - p++; - start = p; - while (*p != ' ' && *p != 0) - p++; - - info->binary = g_strndup (start, p - start); - } + info->binary = binary_from_exec (info->exec); return info; } @@ -1239,6 +1245,9 @@ g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo, { GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); + if (!g_desktop_app_info_ensure_saved (info, error)) + return FALSE; + return update_default_list (info->desktop_id, content_type, TRUE, FALSE, FALSE, error); } @@ -1303,6 +1312,9 @@ g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo, char *dirname; gboolean res; + if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error)) + return FALSE; + dirname = ensure_dir (MIMETYPE_DIR, error); if (!dirname) return FALSE; @@ -1350,6 +1362,9 @@ g_desktop_app_info_add_supports_type (GAppInfo *appinfo, { GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); + if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error)) + return FALSE; + return update_default_list (info->desktop_id, content_type, FALSE, TRUE, FALSE, error); } @@ -1366,39 +1381,35 @@ g_desktop_app_info_remove_supports_type (GAppInfo *appinfo, { GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); + if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error)) + return FALSE; + return update_default_list (info->desktop_id, content_type, FALSE, FALSE, TRUE, error); } -/** - * g_app_info_create_from_commandline: - * @commandline: the commandline to use - * @application_name: the application name, or %NULL to use @commandline - * @flags: flags that can specify details of the created #GAppInfo - * @error: a #GError location to store the error occuring, %NULL to ignore. - * - * Creates a new #GAppInfo from the given information. - * - * Returns: new #GAppInfo for given command. - **/ -GAppInfo * -g_app_info_create_from_commandline (const char *commandline, - const char *application_name, - GAppInfoCreateFlags flags, - GError **error) +static gboolean +g_desktop_app_info_ensure_saved (GDesktopAppInfo *info, + GError **error) { GKeyFile *key_file; char *dirname; - char **split; - char *basename, *exec, *filename, *comment; + char *basename, *filename; char *data, *desktop_id; gsize data_size; int fd; - GDesktopAppInfo *info; gboolean res; + + if (info->filename != NULL) + return TRUE; + /* This is only used for object created with + * g_app_info_create_from_commandline. All other + * object should have a filename + */ + dirname = ensure_dir (APP_DIR, error); if (!dirname) - return NULL; + return FALSE; key_file = g_key_file_new (); @@ -1409,26 +1420,18 @@ g_app_info_create_from_commandline (const char *commandline, g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TYPE, G_KEY_FILE_DESKTOP_TYPE_APPLICATION); - if (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) + if (info->terminal) g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE); - exec = g_strconcat (commandline, " %f", NULL); g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP, - G_KEY_FILE_DESKTOP_KEY_EXEC, exec); - g_free (exec); + G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec); - /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */ - split = g_strsplit (commandline, " ", 2); - basename = g_path_get_basename (split[0]); - g_strfreev (split); g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP, - G_KEY_FILE_DESKTOP_KEY_NAME, application_name?application_name:basename); + G_KEY_FILE_DESKTOP_KEY_NAME, info->name); - comment = g_strdup_printf (_("Custom definition for %s"), basename); g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP, - G_KEY_FILE_DESKTOP_KEY_COMMENT, comment); - g_free (comment); + G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment); g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE); @@ -1436,8 +1439,7 @@ g_app_info_create_from_commandline (const char *commandline, data = g_key_file_to_data (key_file, &data_size, NULL); g_key_file_free (key_file); - desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", basename); - g_free (basename); + desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name); filename = g_build_filename (dirname, desktop_id, NULL); g_free (desktop_id); g_free (dirname); @@ -1453,7 +1455,7 @@ g_app_info_create_from_commandline (const char *commandline, g_free (display_name); g_free (filename); g_free (data); - return NULL; + return FALSE; } desktop_id = g_path_get_basename (filename); @@ -1465,25 +1467,67 @@ g_app_info_create_from_commandline (const char *commandline, { g_free (desktop_id); g_free (filename); - return NULL; + return FALSE; } + info->filename = filename; + info->desktop_id = desktop_id; + run_update_command ("update-desktop-database", "applications"); - info = g_desktop_app_info_new_from_filename (filename); - g_free (filename); - if (info == NULL) - g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, - _("Can't load just created desktop file")); + return TRUE; +} + +/** + * g_app_info_create_from_commandline: + * @commandline: the commandline to use + * @application_name: the application name, or %NULL to use @commandline + * @flags: flags that can specify details of the created #GAppInfo + * @error: a #GError location to store the error occuring, %NULL to ignore. + * + * Creates a new #GAppInfo from the given information. + * + * Returns: new #GAppInfo for given command. + **/ +GAppInfo * +g_app_info_create_from_commandline (const char *commandline, + const char *application_name, + GAppInfoCreateFlags flags, + GError **error) +{ + char **split; + char *basename; + GDesktopAppInfo *info; + + info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL); + + info->filename = NULL; + info->desktop_id = NULL; + + info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL; + info->startup_notify = FALSE; + info->hidden = FALSE; + info->exec = g_strconcat (commandline, " %f", NULL); + info->comment = g_strdup_printf (_("Custom definition for %s"), info->name); + info->nodisplay = TRUE; + info->binary = binary_from_exec (info->exec); + + if (application_name) + info->name = g_strdup (application_name); else - info->desktop_id = g_strdup (desktop_id); - - g_free (desktop_id); + { + /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */ + split = g_strsplit (commandline, " ", 2); + basename = g_path_get_basename (split[0]); + g_strfreev (split); + info->name = basename; + if (info->name == NULL) + info->name = g_strdup ("custom"); + } return G_APP_INFO (info); } - static void g_desktop_app_info_iface_init (GAppInfoIface *iface) {