glib r6412 - trunk/gio



Author: alexl
Date: Tue Jan 29 14:07:07 2008
New Revision: 6412
URL: http://svn.gnome.org/viewvc/glib?rev=6412&view=rev

Log:
2008-01-29  Alexander Larsson  <alexl redhat com>

        * 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.



Modified:
   trunk/gio/ChangeLog
   trunk/gio/gdesktopappinfo.c

Modified: trunk/gio/gdesktopappinfo.c
==============================================================================
--- trunk/gio/gdesktopappinfo.c	(original)
+++ trunk/gio/gdesktopappinfo.c	Tue Jan 29 14:07:07 2008
@@ -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 @@
 {
 }
 
+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 @@
     }
   
   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 @@
 {
   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 @@
   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 @@
 {
   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 @@
 {
   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_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 @@
   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_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_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)
 {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]