[libpeas] Store icons as a GIcon when possible



commit 2a367a4228a211078c2c042440dccc6efe516455
Author: Garrett Regier <garrettregier gmail com>
Date:   Thu Feb 14 03:30:57 2013 -0800

    Store icons as a GIcon when possible
    
    This allows stock id icons to work.

 libpeas-gtk/peas-gtk-plugin-manager-store.c       |   88 ++++++++++-------
 libpeas-gtk/peas-gtk-plugin-manager-store.h       |    4 +-
 libpeas-gtk/peas-gtk-plugin-manager-view.c        |   20 ++--
 tests/libpeas-gtk/plugin-manager-store.c          |  108 ++++++++++++++-------
 tests/libpeas-gtk/plugins/valid-stock-icon.plugin |    2 +-
 5 files changed, 136 insertions(+), 86 deletions(-)
---
diff --git a/libpeas-gtk/peas-gtk-plugin-manager-store.c b/libpeas-gtk/peas-gtk-plugin-manager-store.c
index 417eca6..7efc51e 100644
--- a/libpeas-gtk/peas-gtk-plugin-manager-store.c
+++ b/libpeas-gtk/peas-gtk-plugin-manager-store.c
@@ -26,6 +26,8 @@
 #include <config.h>
 #endif
 
+#include <gio/gio.h>
+
 #include <libpeas/peas-plugin-info.h>
 
 #include "peas-gtk-plugin-manager-store.h"
@@ -33,8 +35,8 @@
 static const GType ColumnTypes[] = {
   G_TYPE_BOOLEAN, /* Enabled */
   G_TYPE_BOOLEAN, /* Enabled Visible */
-  G_TYPE_OBJECT,  /* Pixbuf Icon */
-  G_TYPE_STRING,  /* Stock Icon */
+  G_TYPE_OBJECT,  /* GIcon Icon */
+  G_TYPE_STRING,  /* Stock ID Icon */
   G_TYPE_BOOLEAN, /* Icon Visible */
   G_TYPE_STRING,  /* Info */
   G_TYPE_BOOLEAN, /* Info Visible */
@@ -69,7 +71,8 @@ update_plugin (PeasGtkPluginManagerStore *store,
   gboolean builtin;
   gchar *markup;
   const gchar *icon_name;
-  GdkPixbuf *icon_pixbuf = NULL;
+  const gchar *icon_stock_id = NULL;
+  GIcon *icon_gicon = NULL;
 
   loaded = peas_plugin_info_is_loaded (info);
   available = peas_plugin_info_is_available (info, NULL);
@@ -89,66 +92,77 @@ update_plugin (PeasGtkPluginManagerStore *store,
 
   if (!available)
     {
-      icon_name = GTK_STOCK_DIALOG_ERROR;
+      icon_gicon = g_themed_icon_new ("dialog-error");
     }
   else
     {
-      gchar *icon_filename;
+      gchar *icon_path;
 
       icon_name = peas_plugin_info_get_icon_name (info);
-      icon_filename = g_build_filename (peas_plugin_info_get_data_dir (info),
-                                        icon_name,
-                                        NULL);
-
-      if (!gtk_icon_theme_has_icon (gtk_icon_theme_get_default (), icon_name))
-        icon_name = "libpeas-plugin";
+      icon_path = g_build_filename (peas_plugin_info_get_data_dir (info),
+                                    icon_name,
+                                    NULL);
 
-      /* Prevent warning for the common case that icon_filename
+      /* Prevent warning for the common case that icon_path
        * does not exist but warn when it is a directory
        */
-      if (g_file_test (icon_filename, G_FILE_TEST_EXISTS))
+      if (g_file_test (icon_path, G_FILE_TEST_EXISTS))
         {
-          GError *error = NULL;
-          gint width, height;
+          GFile *icon_file;
 
-          /* Attempt to load the icon scaled to the correct size */
-          if (!gtk_icon_size_lookup (GTK_ICON_SIZE_SMALL_TOOLBAR,
-                                     &width, &height))
-            {
-              icon_pixbuf = gdk_pixbuf_new_from_file (icon_filename, &error);
-            }
-          else
-            {
-              icon_pixbuf = gdk_pixbuf_new_from_file_at_size (icon_filename,
-                                                              width, height,
-                                                              &error);
-            }
+          icon_file = g_file_new_for_path (icon_path);
+          icon_gicon = g_file_icon_new (icon_file);
+
+          g_object_unref (icon_file);
+        }
+      else
+        {
+          gint i;
+          GtkIconTheme *icon_theme;
+          const gchar * const *names;
+          gboolean found_icon = FALSE;
+
+          icon_gicon = g_themed_icon_new_with_default_fallbacks (icon_name);
+
+          icon_theme = gtk_icon_theme_get_default ();
+          names = g_themed_icon_get_names (G_THEMED_ICON (icon_gicon));
+
+          for (i = 0; !found_icon && i < g_strv_length ((gchar **) names); ++i)
+            found_icon = gtk_icon_theme_has_icon (icon_theme, names[i]);
 
-          if (error == NULL)
-            icon_name = NULL;
-          else
+          if (!found_icon)
             {
-              g_warning ("Error while loading icon: %s", error->message);
-              g_error_free (error);
+              GtkStockItem stock_item;
+
+              g_clear_object (&icon_gicon);
+
+              if (gtk_stock_lookup (icon_name, &stock_item))
+                {
+                  icon_stock_id = icon_name;
+                }
+              else
+                {
+                  icon_gicon = g_themed_icon_new ("libpeas-plugin");
+                }
             }
         }
 
-      g_free (icon_filename);
+      g_free (icon_path);
     }
 
   gtk_list_store_set (GTK_LIST_STORE (store), iter,
     PEAS_GTK_PLUGIN_MANAGER_STORE_ENABLED_COLUMN,        loaded,
     PEAS_GTK_PLUGIN_MANAGER_STORE_CAN_ENABLE_COLUMN,     !builtin && available,
-    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_PIXBUF_COLUMN,    icon_pixbuf,
-    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_NAME_COLUMN,      icon_name,
+    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_GICON_COLUMN,     icon_gicon,
+    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_STOCK_ID_COLUMN,  icon_stock_id,
     PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_VISIBLE_COLUMN,   !available,
     PEAS_GTK_PLUGIN_MANAGER_STORE_INFO_COLUMN,           markup,
     PEAS_GTK_PLUGIN_MANAGER_STORE_INFO_SENSITIVE_COLUMN, available && (!builtin || loaded),
     PEAS_GTK_PLUGIN_MANAGER_STORE_PLUGIN_COLUMN,         info,
     -1);
 
-  if (icon_pixbuf != NULL)
-    g_object_unref (icon_pixbuf);
+  if (icon_gicon != NULL)
+    g_object_unref (icon_gicon);
 
   g_free (markup);
 }
diff --git a/libpeas-gtk/peas-gtk-plugin-manager-store.h b/libpeas-gtk/peas-gtk-plugin-manager-store.h
index 6a0fa7d..ece25f1 100644
--- a/libpeas-gtk/peas-gtk-plugin-manager-store.h
+++ b/libpeas-gtk/peas-gtk-plugin-manager-store.h
@@ -34,8 +34,8 @@ G_BEGIN_DECLS
 typedef enum {
   PEAS_GTK_PLUGIN_MANAGER_STORE_ENABLED_COLUMN = 0,
   PEAS_GTK_PLUGIN_MANAGER_STORE_CAN_ENABLE_COLUMN,
-  PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_PIXBUF_COLUMN,
-  PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_NAME_COLUMN,
+  PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_GICON_COLUMN,
+  PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_STOCK_ID_COLUMN,
   PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_VISIBLE_COLUMN,
   PEAS_GTK_PLUGIN_MANAGER_STORE_INFO_COLUMN,
   PEAS_GTK_PLUGIN_MANAGER_STORE_INFO_SENSITIVE_COLUMN,
diff --git a/libpeas-gtk/peas-gtk-plugin-manager-view.c b/libpeas-gtk/peas-gtk-plugin-manager-view.c
index d315af2..bce090f 100644
--- a/libpeas-gtk/peas-gtk-plugin-manager-view.c
+++ b/libpeas-gtk/peas-gtk-plugin-manager-view.c
@@ -469,23 +469,25 @@ plugin_icon_data_func (GtkTreeViewColumn *column,
                        GtkTreeModel      *model,
                        GtkTreeIter       *iter)
 {
-  GdkPixbuf *icon_pixbuf;
-  gchar *icon_name;
+  GIcon *icon_gicon;
+  gchar *icon_stock_id;
 
   gtk_tree_model_get (model, iter,
-    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_PIXBUF_COLUMN, &icon_pixbuf,
-    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_NAME_COLUMN, &icon_name,
+    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_GICON_COLUMN, &icon_gicon,
+    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_STOCK_ID_COLUMN, &icon_stock_id,
     -1);
 
-  if (icon_pixbuf == NULL)
-    g_object_set (cell, "icon-name", icon_name, NULL);
+  if (icon_gicon == NULL)
+    {
+      g_object_set (cell, "stock-id", icon_stock_id, NULL);
+    }
   else
     {
-      g_object_set (cell, "pixbuf", icon_pixbuf, NULL);
-      g_object_unref (icon_pixbuf);
+      g_object_set (cell, "gicon", icon_gicon, NULL);
+      g_object_unref (icon_gicon);
     }
 
-  g_free (icon_name);
+  g_free (icon_stock_id);
 }
 
 static void
diff --git a/tests/libpeas-gtk/plugin-manager-store.c b/tests/libpeas-gtk/plugin-manager-store.c
index 0b3d77d..1f4237a 100644
--- a/tests/libpeas-gtk/plugin-manager-store.c
+++ b/tests/libpeas-gtk/plugin-manager-store.c
@@ -125,29 +125,68 @@ verify_model (TestFixture    *fixture,
               PeasPluginInfo *info,
               gboolean        can_enable,
               const gchar    *icon_name,
+              GType           icon_type,
               gboolean        icon_visible,
               gboolean        info_sensitive)
 {
   GtkTreeIter iter;
   gboolean model_can_enable, model_icon_visible, model_info_sensitive;
-  gchar *model_icon_name;
+  GIcon *model_icon_gicon;
+  gchar *model_icon_stock_id;
 
   g_assert (peas_gtk_plugin_manager_store_get_iter_from_plugin (fixture->store,
                                                                 &iter, info));
 
   gtk_tree_model_get (fixture->model, &iter,
     PEAS_GTK_PLUGIN_MANAGER_STORE_CAN_ENABLE_COLUMN,     &model_can_enable,
-    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_NAME_COLUMN,      &model_icon_name,
+    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_GICON_COLUMN,     &model_icon_gicon,
+    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_STOCK_ID_COLUMN,  &model_icon_stock_id,
     PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_VISIBLE_COLUMN,   &model_icon_visible,
     PEAS_GTK_PLUGIN_MANAGER_STORE_INFO_SENSITIVE_COLUMN, &model_info_sensitive,
     -1);
 
   g_assert_cmpint (model_can_enable, ==, can_enable);
-  g_assert_cmpstr (model_icon_name, ==, icon_name);
   g_assert_cmpint (model_icon_visible, ==, icon_visible);
   g_assert_cmpint (model_info_sensitive, ==, info_sensitive);
 
-  g_free (model_icon_name);
+  if (icon_type == G_TYPE_INVALID)
+    {
+      g_assert_cmpstr (model_icon_stock_id, ==, icon_name);
+    }
+  else
+    {
+      g_assert (g_type_is_a (G_OBJECT_TYPE (model_icon_gicon), icon_type));
+
+      if (icon_type == G_TYPE_FILE_ICON)
+        {
+          GFile *file;
+          gchar *basename;
+
+          file = g_file_icon_get_file (G_FILE_ICON (model_icon_gicon));
+          basename = g_file_get_basename (file);
+
+          g_assert_cmpstr (basename, ==, icon_name);
+
+          g_free (basename);
+        }
+      else if (icon_type == G_TYPE_THEMED_ICON)
+        {
+          GThemedIcon *themed_icon;
+          const gchar * const *icon_names;
+
+          themed_icon = G_THEMED_ICON (model_icon_gicon);
+          icon_names = g_themed_icon_get_names (themed_icon);
+
+          g_assert (icon_names);
+          g_assert_cmpstr (icon_names[0], ==, icon_name);
+        }
+      else
+        {
+          g_assert_not_reached ();
+        }
+    }
+
+  g_free (model_icon_stock_id);
 }
 
 static void
@@ -157,7 +196,8 @@ test_gtk_plugin_manager_store_verify_loadable (TestFixture *fixture)
 
   info = peas_engine_get_plugin_info (fixture->engine, "loadable");
 
-  verify_model (fixture, info, TRUE, "libpeas-plugin", FALSE, TRUE);
+  verify_model (fixture, info, TRUE, "libpeas-plugin", G_TYPE_THEMED_ICON,
+                FALSE, TRUE);
 }
 
 static void
@@ -169,11 +209,13 @@ test_gtk_plugin_manager_store_verify_unavailable (TestFixture *fixture)
 
   info = peas_engine_get_plugin_info (fixture->engine, "unavailable");
 
-  verify_model (fixture, info, TRUE, "libpeas-plugin", FALSE, TRUE);
+  verify_model (fixture, info, TRUE, "libpeas-plugin", G_TYPE_THEMED_ICON,
+                FALSE, TRUE);
 
   peas_engine_load_plugin (fixture->engine, info);
 
-  verify_model (fixture, info, FALSE, GTK_STOCK_DIALOG_ERROR, TRUE, FALSE);
+  verify_model (fixture, info, FALSE, "dialog-error",
+                G_TYPE_THEMED_ICON, TRUE, FALSE);
 }
 
 static void
@@ -183,11 +225,13 @@ test_gtk_plugin_manager_store_verify_builtin (TestFixture *fixture)
 
   info = peas_engine_get_plugin_info (fixture->engine, "builtin");
 
-  verify_model (fixture, info, FALSE, "libpeas-plugin", FALSE, FALSE);
+  verify_model (fixture, info, FALSE, "libpeas-plugin", G_TYPE_THEMED_ICON,
+                FALSE, FALSE);
 
   peas_engine_load_plugin (fixture->engine, info);
 
-  verify_model (fixture, info, FALSE, "libpeas-plugin", FALSE, TRUE);
+  verify_model (fixture, info, FALSE, "libpeas-plugin", G_TYPE_THEMED_ICON,
+                FALSE, TRUE);
 }
 
 static void
@@ -224,59 +268,49 @@ test_gtk_plugin_manager_store_verify_info (TestFixture *fixture)
 static void
 verify_icon (TestFixture *fixture,
              const gchar *plugin_name,
-             gboolean     has_pixbuf,
-             const gchar *icon_name)
+             const gchar *icon_name,
+             GType        icon_type)
 {
   PeasPluginInfo *info;
-  GtkTreeIter iter;
-  GdkPixbuf *model_icon_pixbuf;
-  gchar *model_icon_name;
 
   info = peas_engine_get_plugin_info (fixture->engine, plugin_name);
-  g_assert (peas_gtk_plugin_manager_store_get_iter_from_plugin (fixture->store,
-                                                                &iter, info));
 
-  gtk_tree_model_get (fixture->model, &iter,
-    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_PIXBUF_COLUMN, &model_icon_pixbuf,
-    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_NAME_COLUMN, &model_icon_name,
-    -1);
-
-  if (has_pixbuf)
-    g_assert (GDK_IS_PIXBUF (model_icon_pixbuf));
-  else
-    g_assert (!GDK_IS_PIXBUF (model_icon_pixbuf));
-
-  g_assert_cmpstr (model_icon_name, ==, icon_name);
-
-  if (model_icon_pixbuf != NULL)
-    g_object_unref (model_icon_pixbuf);
-
-  if (model_icon_name != NULL)
-    g_free (model_icon_name);
+  verify_model (fixture, info, TRUE, icon_name, icon_type, FALSE, TRUE);
 }
 
 static void
 test_gtk_plugin_manager_store_valid_custom_icon (TestFixture *fixture)
 {
-  verify_icon (fixture, "valid-custom-icon", TRUE, NULL);
+  verify_icon (fixture, "valid-custom-icon", "exists.png", G_TYPE_FILE_ICON);
 }
 
 static void
 test_gtk_plugin_manager_store_valid_stock_icon (TestFixture *fixture)
 {
-  verify_icon (fixture, "valid-stock-icon", FALSE, "gtk-about");
+  GtkIconTheme *icon_theme;
+  GType icon_type = icon_type = G_TYPE_INVALID;
+
+  icon_theme = gtk_icon_theme_get_default ();
+
+  /* Usually the theme does not have this icon */
+  if (gtk_icon_theme_has_icon (icon_theme, "gtk-unindent"))
+    icon_type = G_TYPE_THEMED_ICON;
+
+  verify_icon (fixture, "valid-stock-icon", "gtk-unindent", icon_type);
 }
 
 static void
 test_gtk_plugin_manager_store_invalid_custom_icon (TestFixture *fixture)
 {
-  verify_icon (fixture, "invalid-custom-icon", FALSE, "libpeas-plugin");
+  verify_icon (fixture, "invalid-custom-icon", "libpeas-plugin",
+               G_TYPE_THEMED_ICON);
 }
 
 static void
 test_gtk_plugin_manager_store_invalid_stock_icon (TestFixture *fixture)
 {
-  verify_icon (fixture, "invalid-stock-icon", FALSE, "libpeas-plugin");
+  verify_icon (fixture, "invalid-stock-icon", "libpeas-plugin",
+               G_TYPE_THEMED_ICON);
 }
 
 static void
diff --git a/tests/libpeas-gtk/plugins/valid-stock-icon.plugin 
b/tests/libpeas-gtk/plugins/valid-stock-icon.plugin
index e965641..027ef5f 100644
--- a/tests/libpeas-gtk/plugins/valid-stock-icon.plugin
+++ b/tests/libpeas-gtk/plugins/valid-stock-icon.plugin
@@ -2,6 +2,6 @@
 Module=valid-stock-icon
 Name=Valid Stock Icon
 Description=A plugin that has a valid stock icon.
-Icon=gtk-about
+Icon=gtk-unindent
 Authors=Garrett Regier
 Copyright=Copyright  2010 Garrett Regier


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