[gnome-menus] libmenu: Make gmenu_tree_directory_get_icon() return a GIcon



commit 17961198a182d5f68e4773427b1c8f735e0a01cd
Author: Vincent Untz <vuntz gnome org>
Date:   Thu Jul 21 15:48:35 2011 +0200

    libmenu: Make gmenu_tree_directory_get_icon() return a GIcon
    
    It's much better to use GIcon than icon name strings.

 libmenu/desktop-entries.c |   50 ++++++++++++++++++++++++++++++++++++++------
 libmenu/desktop-entries.h |    4 +--
 libmenu/gmenu-tree.c      |   10 ++++++++-
 libmenu/gmenu-tree.h      |    2 +-
 4 files changed, 54 insertions(+), 12 deletions(-)
---
diff --git a/libmenu/desktop-entries.c b/libmenu/desktop-entries.c
index a441f65..eaf7f53 100644
--- a/libmenu/desktop-entries.c
+++ b/libmenu/desktop-entries.c
@@ -54,7 +54,7 @@ typedef struct
   char     *name;
   char     *generic_name;
   char     *comment;
-  char     *icon;
+  GIcon    *icon;
 
   guint     nodisplay   : 1;
   guint     hidden      : 1;
@@ -87,6 +87,41 @@ unix_basename_from_path (const char *path)
     return path;
 }
 
+static GIcon *
+key_file_get_icon (GKeyFile *key_file)
+{
+  GIcon *icon = NULL;
+  gchar *icon_name;
+
+  icon_name = g_key_file_get_locale_string (key_file, DESKTOP_ENTRY_GROUP,
+                                            "Icon", NULL, NULL);
+  if (!icon_name)
+    return NULL;
+
+  if (g_path_is_absolute (icon_name)) {
+    GFile *file;
+
+    file = g_file_new_for_path (icon_name);
+    icon = g_file_icon_new (file);
+    g_object_unref (file);
+  } else {
+    char *p;
+
+    /* Work around a common mistake in desktop files */
+    if ((p = strrchr (icon_name, '.')) != NULL &&
+        (strcmp (p, ".png") == 0 ||
+         strcmp (p, ".xpm") == 0 ||
+         strcmp (p, ".svg") == 0))
+      *p = 0;
+
+    icon = g_themed_icon_new (icon_name);
+  }
+
+  g_free (icon_name);
+
+  return icon;
+}
+
 static gboolean
 key_file_get_show_in_gnome (GKeyFile *key_file)
 {
@@ -165,7 +200,7 @@ desktop_entry_load_directory (DesktopEntry  *entry,
 
   entry_directory->generic_name = g_key_file_get_locale_string (key_file, DESKTOP_ENTRY_GROUP, "GenericName", NULL, NULL);
   entry_directory->comment      = g_key_file_get_locale_string (key_file, DESKTOP_ENTRY_GROUP, "Comment", NULL, NULL);
-  entry_directory->icon         = g_key_file_get_locale_string (key_file, DESKTOP_ENTRY_GROUP, "Icon", NULL, NULL);
+  entry_directory->icon         = key_file_get_icon (key_file);
   entry_directory->nodisplay    = g_key_file_get_boolean (key_file,
                                                           DESKTOP_ENTRY_GROUP,
                                                           "NoDisplay",
@@ -315,7 +350,7 @@ desktop_entry_reload (DesktopEntry *entry)
       g_free (entry_directory->comment);
       entry_directory->comment = NULL;
 
-      g_free (entry_directory->icon);
+      g_object_unref (entry_directory->icon);
       entry_directory->icon = NULL;
     }
   else
@@ -390,7 +425,7 @@ desktop_entry_copy (DesktopEntry *entry)
 
       retval_directory->name         = g_strdup (entry_directory->name);
       retval_directory->comment      = g_strdup (entry_directory->comment);
-      retval_directory->icon         = g_strdup (entry_directory->icon);
+      retval_directory->icon         = g_object_ref (entry_directory->icon);
       retval_directory->nodisplay    = entry_directory->nodisplay;
       retval_directory->hidden       = entry_directory->hidden;
       retval_directory->showin       = entry_directory->showin;
@@ -429,7 +464,7 @@ desktop_entry_unref (DesktopEntry *entry)
       g_free (entry_directory->comment);
       entry_directory->comment = NULL;
 
-      g_free (entry_directory->icon);
+      g_object_unref (entry_directory->icon);
       entry_directory->icon = NULL;
     }
   else
@@ -480,10 +515,11 @@ desktop_entry_get_comment (DesktopEntry *entry)
   return ((DesktopEntryDirectory*)entry)->comment;
 }
 
-const char *
+GIcon *
 desktop_entry_get_icon (DesktopEntry *entry)
 {
-  g_return_val_if_fail (entry->type == DESKTOP_ENTRY_DIRECTORY, NULL);
+  if (entry->type == DESKTOP_ENTRY_DESKTOP)
+    return g_app_info_get_icon (G_APP_INFO (((DesktopEntryDesktop*)entry)->appinfo));
   return ((DesktopEntryDirectory*)entry)->icon;
 }
 
diff --git a/libmenu/desktop-entries.h b/libmenu/desktop-entries.h
index 5ef9b63..07b7c85 100644
--- a/libmenu/desktop-entries.h
+++ b/libmenu/desktop-entries.h
@@ -46,13 +46,11 @@ const char       *desktop_entry_get_basename (DesktopEntry *entry);
 const char       *desktop_entry_get_name     (DesktopEntry *entry);
 const char       *desktop_entry_get_generic_name (DesktopEntry *entry);
 const char       *desktop_entry_get_comment  (DesktopEntry *entry);
+GIcon            *desktop_entry_get_icon     (DesktopEntry *entry);
 gboolean          desktop_entry_get_hidden   (DesktopEntry *entry);
 gboolean          desktop_entry_get_no_display (DesktopEntry *entry);
 gboolean          desktop_entry_get_show_in  (DesktopEntry *entry);
 
-/* Only valid for DESKTOP_ENTRY_DIRECTORY */
-const char       *desktop_entry_get_icon     (DesktopEntry *entry);
-
 /* Only valid for DESKTOP_ENTRY_DESKTOP */
 GDesktopAppInfo  *desktop_entry_get_app_info (DesktopEntry *entry);
 gboolean desktop_entry_has_categories (DesktopEntry *entry);
diff --git a/libmenu/gmenu-tree.c b/libmenu/gmenu-tree.c
index a7f9cdb..1464aa3 100644
--- a/libmenu/gmenu-tree.c
+++ b/libmenu/gmenu-tree.c
@@ -959,7 +959,15 @@ gmenu_tree_directory_get_comment (GMenuTreeDirectory *directory)
   return desktop_entry_get_comment (directory->directory_entry);
 }
 
-const char *
+/**
+ * gmenu_tree_directory_get_icon:
+ * @directory: a #GMenuTreeDirectory
+ *
+ * Gets the icon for the directory.
+ *
+ * Returns: (transfer none): The #GIcon for this directory
+ */
+GIcon *
 gmenu_tree_directory_get_icon (GMenuTreeDirectory *directory)
 {
   g_return_val_if_fail (directory != NULL, NULL);
diff --git a/libmenu/gmenu-tree.h b/libmenu/gmenu-tree.h
index 7fd740b..7ab872f 100644
--- a/libmenu/gmenu-tree.h
+++ b/libmenu/gmenu-tree.h
@@ -101,7 +101,7 @@ GMenuTreeDirectory *gmenu_tree_directory_get_parent    (GMenuTreeDirectory *dire
 const char *gmenu_tree_directory_get_name              (GMenuTreeDirectory *directory);
 const char *gmenu_tree_directory_get_generic_name      (GMenuTreeDirectory *directory);
 const char *gmenu_tree_directory_get_comment           (GMenuTreeDirectory *directory);
-const char *gmenu_tree_directory_get_icon              (GMenuTreeDirectory *directory);
+GIcon      *gmenu_tree_directory_get_icon              (GMenuTreeDirectory *directory);
 const char *gmenu_tree_directory_get_desktop_file_path (GMenuTreeDirectory *directory);
 const char *gmenu_tree_directory_get_menu_id           (GMenuTreeDirectory *directory);
 



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