[Xfce4-commits] <garcon:master> Handle local desktop file loading directly.

Nick Schermer noreply at xfce.org
Sun Jun 5 21:38:01 CEST 2011


Updating branch refs/heads/master
         to 84065e78a3932dfe3ed9a62d21d9bfe9ced76d55 (commit)
       from 3d3e623ac9e8fa1e03f74143c8bcfb9f0dd752a0 (commit)

commit 84065e78a3932dfe3ed9a62d21d9bfe9ced76d55
Author: Nick Schermer <nick at xfce.org>
Date:   Sun Jun 5 20:48:03 2011 +0200

    Handle local desktop file loading directly.
    
    This is a tiny bit faster and uses less memory.

 garcon/garcon-menu-directory.c |   20 +++--------------
 garcon/garcon-menu-item.c      |   38 ++++----------------------------
 garcon/garcon-private.c        |   46 ++++++++++++++++++++++++++++++++++++++++
 garcon/garcon-private.h        |    3 ++
 4 files changed, 58 insertions(+), 49 deletions(-)

diff --git a/garcon/garcon-menu-directory.c b/garcon/garcon-menu-directory.c
index 4dafe58..937635b 100644
--- a/garcon/garcon-menu-directory.c
+++ b/garcon/garcon-menu-directory.c
@@ -27,6 +27,7 @@
 
 #include <garcon/garcon-environment.h>
 #include <garcon/garcon-menu-directory.h>
+#include <garcon/garcon-private.h>
 
 
 
@@ -342,31 +343,18 @@ GarconMenuDirectory *
 garcon_menu_directory_new (GFile *file)
 {
   GarconMenuDirectory *directory = NULL;
-  gchar               *contents;
-  gsize                length;
   GKeyFile            *rc;
   gchar               *name;
   gchar               *comment;
   gchar               *icon_name;
   gboolean             no_display;
-  gboolean             succeed;
 
   g_return_val_if_fail (G_IS_FILE (file), NULL);
 
-  /* Load the contents of the file */
-  if (!g_file_load_contents (file, NULL, &contents, &length, NULL, NULL))
-    return NULL;
-
   /* Open the keyfile */
-  rc = g_key_file_new ();
-  succeed = g_key_file_load_from_data (rc, contents, length, G_KEY_FILE_NONE, NULL);
-  g_free (contents);
-  if (G_UNLIKELY (!succeed))
-    {
-      /* Cleanup and leave */
-      g_key_file_free (rc);
-      return NULL;
-    }
+  rc = _garcon_keyfile_load (file, NULL);
+  if (G_UNLIKELY (rc == NULL))
+    return NULL;
 
   /* Parse name, exec command and icon name */
   name = g_key_file_get_locale_string (rc, G_KEY_FILE_DESKTOP_GROUP,
diff --git a/garcon/garcon-menu-item.c b/garcon/garcon-menu-item.c
index ffaa3c4..e49bbfe 100644
--- a/garcon/garcon-menu-item.c
+++ b/garcon/garcon-menu-item.c
@@ -702,9 +702,6 @@ garcon_menu_item_new (GFile *file)
 {
   GarconMenuItem *item = NULL;
   GKeyFile       *rc;
-  gchar          *contents;
-  gsize           length = 0;
-  gboolean        succeed;
   GList          *categories = NULL;
   gboolean        terminal;
   gboolean        no_display;
@@ -722,19 +719,10 @@ garcon_menu_item_new (GFile *file)
 
   g_return_val_if_fail (G_IS_FILE (file), NULL);
 
-  /* Load the contents of the file */
-  if (!g_file_load_contents (file, NULL, &contents, &length, NULL, NULL) || length == 0)
-    return NULL;
-
   /* Open the keyfile */
-  rc = g_key_file_new ();
-  succeed = g_key_file_load_from_data (rc, contents, length, G_KEY_FILE_NONE, NULL);
-  g_free (contents);
-  if (G_UNLIKELY (!succeed))
-    {
-      g_key_file_free (rc);
-      return NULL;
-    }
+  rc = _garcon_keyfile_load (file, NULL);
+  if (G_UNLIKELY (rc == NULL))
+    return NULL;
 
   /* Parse name and exec command */
   name = GET_LOCALE_KEY (string, G_KEY_FILE_DESKTOP_KEY_NAME);
@@ -867,38 +855,22 @@ garcon_menu_item_reload_from_file (GarconMenuItem  *item,
 {
   GKeyFile *rc;
   gboolean  boolean;
-  gboolean  succeed;
   GList    *categories = NULL;
   GList    *lp;
   GList    *old_categories = NULL;
   gchar   **mt;
   gchar   **str_list;
-  gchar    *contents;
   gchar    *string;
   gchar    *name;
   gchar    *exec;
-  gsize     length = 0;
 
   g_return_val_if_fail (GARCON_IS_MENU_ITEM (item), FALSE);
   g_return_val_if_fail (G_IS_FILE (file), FALSE);
   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
-  /* Load the contents of the file */
-  if (!g_file_load_contents (file, NULL, &contents, &length, NULL, error))
-    return FALSE;
-
-  /* Leave when the file is empty */
-  if (G_UNLIKELY (length == 0))
-    {
-      g_set_error_literal (error, 0, 0, "The desktop file is empty.");
-      return FALSE;
-    }
-
   /* Open the keyfile */
-  rc = g_key_file_new ();
-  succeed = g_key_file_load_from_data (rc, contents, length, G_KEY_FILE_NONE, error);
-  g_free (contents);
-  if (G_UNLIKELY (!succeed))
+  rc = _garcon_keyfile_load (file, error);
+  if (G_UNLIKELY (rc == NULL))
     return FALSE;
 
   /* Check if there is a name and exec key */
diff --git a/garcon/garcon-private.c b/garcon/garcon-private.c
index bca7eba..b8108e8 100644
--- a/garcon/garcon-private.c
+++ b/garcon/garcon-private.c
@@ -107,3 +107,49 @@ _garcon_file_get_uri_relative_to_file (const gchar *path,
 
   return uri;
 }
+
+
+
+
+GKeyFile *
+_garcon_keyfile_load (GFile   *file,
+                      GError **error)
+{
+  GKeyFile *rc;
+  gchar    *path;
+  gchar    *contents;
+  gsize     length = 0;
+  gboolean  result = FALSE;
+
+  /* Open the keyfile */
+  rc = g_key_file_new ();
+
+  /* Load the contents of the file */
+  if (G_LIKELY (g_file_is_native (file)))
+    {
+      path = g_file_get_path (file);
+      result = g_key_file_load_from_file (rc, path, G_KEY_FILE_NONE, error);
+      g_free (path);
+    }
+  else if (g_file_load_contents (file, NULL, &contents, &length, NULL, error))
+    {
+      /* Leave when the file is empty */
+      if (G_UNLIKELY (length == 0))
+        {
+          g_set_error_literal (error, 0, 0, "The desktop file is empty.");
+          g_key_file_free (rc);
+          return NULL;
+        }
+
+      result = g_key_file_load_from_data (rc, contents, length, G_KEY_FILE_NONE, error);
+      g_free (contents);
+    }
+
+  if (!result)
+    {
+      g_key_file_free (rc);
+      return NULL;
+    }
+
+  return rc;
+}
diff --git a/garcon/garcon-private.h b/garcon/garcon-private.h
index 1d46fb3..4882ab3 100644
--- a/garcon/garcon-private.h
+++ b/garcon/garcon-private.h
@@ -41,6 +41,9 @@ GFile    *_garcon_file_new_relative_to_file     (const gchar *path,
 gchar    *_garcon_file_get_uri_relative_to_file (const gchar *path,
                                                  GFile       *file);
 
+GKeyFile *_garcon_keyfile_load                   (GFile      *file,
+                                                  GError    **error);
+
 G_END_DECLS
 
 #endif /* !__GARCON_PRIVATE_H__ */



More information about the Xfce4-commits mailing list