[Xfce4-commits] <garcon:master> Use GIO for content loading in GarconMenuDirectory.

Nick Schermer nick at xfce.org
Mon Aug 17 18:16:13 CEST 2009


Updating branch refs/heads/master
         to 1146578a1d4f59d34422414fea0804c63ed4c34d (commit)
       from be6aa1b24fd35d3cd001b3543b2939205f3829d4 (commit)

commit 1146578a1d4f59d34422414fea0804c63ed4c34d
Author: Nick Schermer <nick at xfce.org>
Date:   Mon Aug 17 18:14:08 2009 +0200

    Use GIO for content loading in GarconMenuDirectory.
    
    Implement this in a new function garcon_menu_directory_new,
    just like we do in GarconMenuItem.

 garcon/garcon-menu-directory.c |  105 ++++++++++++++++++++-------------------
 garcon/garcon-menu-directory.h |   40 ++++++++-------
 garcon/garcon-menu.c           |    2 +-
 3 files changed, 76 insertions(+), 71 deletions(-)

diff --git a/garcon/garcon-menu-directory.c b/garcon/garcon-menu-directory.c
index 3f57aa4..87951c9 100644
--- a/garcon/garcon-menu-directory.c
+++ b/garcon/garcon-menu-directory.c
@@ -65,7 +65,6 @@ enum
 
 
 
-static void garcon_menu_directory_constructed  (GObject                  *object);
 static void garcon_menu_directory_finalize     (GObject                  *object);
 static void garcon_menu_directory_get_property (GObject                  *object,
                                                 guint                     prop_id,
@@ -75,7 +74,6 @@ static void garcon_menu_directory_set_property (GObject                  *object
                                                 guint                     prop_id,
                                                 const GValue             *value,
                                                 GParamSpec               *pspec);
-static void garcon_menu_directory_load         (GarconMenuDirectory      *directory);
 
 
 
@@ -120,7 +118,6 @@ garcon_menu_directory_class_init (GarconMenuDirectoryClass *klass)
   g_type_class_add_private (klass, sizeof(GarconMenuDirectoryPrivate));
 
   gobject_class = G_OBJECT_CLASS (klass);
-  gobject_class->constructed = garcon_menu_directory_constructed;
   gobject_class->finalize = garcon_menu_directory_finalize;
   gobject_class->get_property = garcon_menu_directory_get_property;
   gobject_class->set_property = garcon_menu_directory_set_property;
@@ -216,15 +213,6 @@ garcon_menu_directory_init (GarconMenuDirectory *directory)
 
 
 static void
-garcon_menu_directory_constructed (GObject *object)
-{
-  GarconMenuDirectory *directory = GARCON_MENU_DIRECTORY (object);
-  garcon_menu_directory_load (directory);
-}
-
-
-
-static void
 garcon_menu_directory_finalize (GObject *object)
 {
   GarconMenuDirectory *directory = GARCON_MENU_DIRECTORY (object);
@@ -326,49 +314,64 @@ garcon_menu_directory_set_property (GObject      *object,
 
 
 
-static void
-garcon_menu_directory_load (GarconMenuDirectory *directory)
+GarconMenuDirectory *
+garcon_menu_directory_new (GFile *file)
 {
-  GKeyFile    *entry;
-  GError      *error = NULL;
-  const gchar *name;
-  const gchar *comment;
-  const gchar *icon;
-  gchar       *filename;
-
-  g_return_if_fail (GARCON_IS_MENU_DIRECTORY (directory));
-
-  /* TODO: Use get_uri() here, together with g_file_read() and
-   * g_key_file_load_from_data() */
-
-  filename = g_file_get_path (directory->priv->file);
-  entry = g_key_file_new ();
-  g_key_file_load_from_file (entry, filename, G_KEY_FILE_NONE, &error);
-  g_free (filename);
+  GarconMenuDirectory *directory = NULL;
+  gchar               *contents;
+  gsize                length;
+  GKeyFile            *rc = NULL;
+  gchar               *name;
+  gchar               *comment;
+  gchar               *icon;
+  gboolean             no_display;
+  gboolean             succeed;
 
-  if (G_UNLIKELY (error != NULL))
-    {
-      g_error_free (error);
-      return;
-    }
-
-  /* Read directory information */
-  name = g_key_file_get_locale_string (entry, "Desktop Entry", "Name", NULL, NULL);
-  comment = g_key_file_get_locale_string (entry, "Desktop Entry", "Comment", NULL, NULL);
-  icon = g_key_file_get_locale_string (entry, "Desktop Entry", "Icon", NULL, NULL);
-
-  /* Pass data to the directory */
-  garcon_menu_directory_set_name (directory, name);
-  garcon_menu_directory_set_comment (directory, comment);
-  garcon_menu_directory_set_icon (directory, icon);
-  garcon_menu_directory_set_no_display (directory, g_key_file_get_boolean (entry, "Desktop Entry", "NoDisplay", NULL));
+  g_return_val_if_fail (GARCON_IS_MENU_DIRECTORY (directory), NULL);
+  g_return_val_if_fail (G_IS_FILE (directory->priv->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))
+    goto error;
+
+  /* Parse name, exec command and icon name */
+  name = g_key_file_get_locale_string (rc, "Desktop Entry", "Name", NULL, NULL);
+  comment = g_key_file_get_locale_string (rc, "Desktop Entry", "Comment", NULL, NULL);
+  icon = g_key_file_get_locale_string (rc, "Desktop Entry", "Icon", NULL, NULL);
+  no_display = g_key_file_get_boolean (rc, "Desktop Entry", "NoDisplay", NULL);
+  
+  /* Allocate a new directory instance */
+  directory = g_object_new (GARCON_TYPE_MENU_DIRECTORY,
+                            "file", file,
+                            "name", name,
+                            "comment", comment,
+                            "icon", icon,
+                            "no-display", no_display,
+                            NULL);
+
+  /* Cleanup */
+  g_free (name);
+  g_free (comment);
+  g_free (icon);
 
   /* Set rest of the private data directly */
-  directory->priv->only_show_in = g_key_file_get_string_list (entry, "Desktop Entry", "OnlyShowIn", NULL, NULL);
-  directory->priv->not_show_in = g_key_file_get_string_list (entry, "Desktop Entry", "NotShowIn", NULL, NULL);
-  directory->priv->hidden = g_key_file_get_boolean (entry, "Desktop Entry", "Hidden", NULL);
+  directory->priv->only_show_in = g_key_file_get_string_list (rc, "Desktop Entry", "OnlyShowIn", NULL, NULL);
+  directory->priv->not_show_in = g_key_file_get_string_list (rc, "Desktop Entry", "NotShowIn", NULL, NULL);
+  directory->priv->hidden = g_key_file_get_boolean (rc, "Desktop Entry", "Hidden", NULL);
+
+error:
+  /* Cleanup */
+  if (G_LIKELY (rc != NULL))
+    g_key_file_free (rc);
 
-  g_key_file_free (entry);
+  return directory;
 }
 
 
diff --git a/garcon/garcon-menu-directory.h b/garcon/garcon-menu-directory.h
index 6c8e7fc..fb49e25 100644
--- a/garcon/garcon-menu-directory.h
+++ b/garcon/garcon-menu-directory.h
@@ -55,25 +55,27 @@ struct _GarconMenuDirectory
 
 
 
-GType        garcon_menu_directory_get_type                (void) G_GNUC_CONST;
-
-GFile       *garcon_menu_directory_get_file                (GarconMenuDirectory *directory);
-const gchar *garcon_menu_directory_get_name                (GarconMenuDirectory *directory);
-void         garcon_menu_directory_set_name                (GarconMenuDirectory *directory,
-                                                            const gchar         *name);
-const gchar *garcon_menu_directory_get_comment             (GarconMenuDirectory *directory);
-void         garcon_menu_directory_set_comment             (GarconMenuDirectory *directory,
-                                                            const gchar         *comment);
-const gchar *garcon_menu_directory_get_icon                (GarconMenuDirectory *directory);
-void         garcon_menu_directory_set_icon                (GarconMenuDirectory *directory,
-                                                            const gchar         *icon);
-gboolean     garcon_menu_directory_get_no_display          (GarconMenuDirectory *directory);
-void         garcon_menu_directory_set_no_display          (GarconMenuDirectory *directory,
-                                                            gboolean             no_display);
-gboolean     garcon_menu_directory_get_hidden              (GarconMenuDirectory *directory);
-gboolean     garcon_menu_directory_get_show_in_environment (GarconMenuDirectory *directory);
-gboolean     garcon_menu_directory_get_visible             (GarconMenuDirectory *directory);
-gboolean     garcon_menu_directory_equal                   (GarconMenuDirectory *directory,
+GType                garcon_menu_directory_get_type                (void) G_GNUC_CONST;
+
+GarconMenuDirectory *garcon_menu_directory_new                     (GFile               *file) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
+
+GFile               *garcon_menu_directory_get_file                (GarconMenuDirectory *directory);
+const gchar         *garcon_menu_directory_get_name                (GarconMenuDirectory *directory);
+void                 garcon_menu_directory_set_name                (GarconMenuDirectory *directory,
+                                                                    const gchar         *name);
+const gchar         *garcon_menu_directory_get_comment             (GarconMenuDirectory *directory);
+void                 garcon_menu_directory_set_comment             (GarconMenuDirectory *directory,
+                                                                    const gchar         *comment);
+const gchar         *garcon_menu_directory_get_icon                (GarconMenuDirectory *directory);
+void                 garcon_menu_directory_set_icon                (GarconMenuDirectory *directory,
+                                                                    const gchar         *icon);
+gboolean             garcon_menu_directory_get_no_display          (GarconMenuDirectory *directory);
+void                 garcon_menu_directory_set_no_display          (GarconMenuDirectory *directory,
+                                                                    gboolean             no_display);
+gboolean             garcon_menu_directory_get_hidden              (GarconMenuDirectory *directory);
+gboolean             garcon_menu_directory_get_show_in_environment (GarconMenuDirectory *directory);
+gboolean             garcon_menu_directory_get_visible             (GarconMenuDirectory *directory);
+gboolean             garcon_menu_directory_equal                   (GarconMenuDirectory *directory,
                                                             GarconMenuDirectory *other);
 
 G_END_DECLS
diff --git a/garcon/garcon-menu.c b/garcon/garcon-menu.c
index a6e1dc6..6393ff7 100644
--- a/garcon/garcon-menu.c
+++ b/garcon/garcon-menu.c
@@ -784,7 +784,7 @@ garcon_menu_lookup_directory (GarconMenu  *menu,
       if (G_LIKELY (g_file_query_exists (file, NULL)))
         {
           /* Load menu directory */
-          directory = g_object_new (GARCON_TYPE_MENU_DIRECTORY, "file", file, NULL);
+          directory = garcon_menu_directory_new (file);
 
           /* Update search status */
           found = TRUE;



More information about the Xfce4-commits mailing list