[libpeas] Allow the Icon in *.plugin to be located in the data dir



commit e6dd4b3bc574e25268b2ad1922b295434591dde7
Author: Garrett Regier <alias301 gmail com>
Date:   Sun Jan 16 13:40:46 2011 -0800

    Allow the Icon in *.plugin to be located in the data dir

 libpeas-gtk/peas-gtk-plugin-manager-store.c    |   70 +++++++++++++++++++-----
 libpeas-gtk/peas-gtk-plugin-manager-store.h    |    3 +-
 libpeas-gtk/peas-gtk-plugin-manager-view.c     |   32 ++++++++++--
 peas-demo/plugins/secondtime/gnome-foot.png    |  Bin 0 -> 2916 bytes
 peas-demo/plugins/secondtime/secondtime.plugin |    1 +
 tests/libpeas-gtk/plugin-manager-store.c       |   18 +++---
 tests/libpeas/plugin-info.c                    |    2 +-
 tests/plugins/full-info.plugin                 |    2 +-
 8 files changed, 99 insertions(+), 29 deletions(-)
---
diff --git a/libpeas-gtk/peas-gtk-plugin-manager-store.c b/libpeas-gtk/peas-gtk-plugin-manager-store.c
index 07a312f..32b0eee 100644
--- a/libpeas-gtk/peas-gtk-plugin-manager-store.c
+++ b/libpeas-gtk/peas-gtk-plugin-manager-store.c
@@ -33,7 +33,8 @@
 static const GType ColumnTypes[] = {
   G_TYPE_BOOLEAN, /* Enabled */
   G_TYPE_BOOLEAN, /* Enabled Visible */
-  G_TYPE_STRING,  /* Icon */
+  G_TYPE_OBJECT,  /* Pixbuf Icon */
+  G_TYPE_STRING,  /* Stock Icon */
   G_TYPE_BOOLEAN, /* Icon Visible */
   G_TYPE_STRING,  /* Info */
   G_TYPE_BOOLEAN, /* Info Visible */
@@ -62,6 +63,7 @@ update_plugin (PeasGtkPluginManagerStore *store,
   gboolean builtin;
   gchar *markup;
   const gchar *icon_name;
+  GdkPixbuf *icon_pixbuf = NULL;
 
   loaded = peas_plugin_info_is_loaded (info);
   available = peas_plugin_info_is_available (info);
@@ -79,27 +81,69 @@ update_plugin (PeasGtkPluginManagerStore *store,
                                         peas_plugin_info_get_description (info));
     }
 
-  if (peas_plugin_info_is_available (info))
+  if (!available)
     {
-      icon_name = peas_plugin_info_get_icon_name (info);
-      if (!gtk_icon_theme_has_icon (gtk_icon_theme_get_default (), icon_name))
-        icon_name = "libpeas-plugin";
+      icon_name = GTK_STOCK_DIALOG_ERROR;
     }
   else
     {
-      icon_name = GTK_STOCK_DIALOG_ERROR;
+      gchar *icon_filename;
+
+      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";
+
+      /* Prevent warning for the common case that icon_filename
+       * does not exist but warn when it is a directory
+       */
+      if (g_file_test (icon_filename, G_FILE_TEST_EXISTS))
+        {
+          GError *error = NULL;
+          gint width, height;
+
+          /* 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);
+            }
+
+          if (error == NULL)
+            icon_name = NULL;
+          else
+            {
+              g_warning ("Error while loading icon: %s", error->message);
+              g_error_free (error);
+            }
+        }
+
+      g_free (icon_filename);
     }
 
   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_COLUMN,             icon_name,
-    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,
+    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_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);
+
   g_free (markup);
 }
 
diff --git a/libpeas-gtk/peas-gtk-plugin-manager-store.h b/libpeas-gtk/peas-gtk-plugin-manager-store.h
index 592d35f..587686a 100644
--- a/libpeas-gtk/peas-gtk-plugin-manager-store.h
+++ b/libpeas-gtk/peas-gtk-plugin-manager-store.h
@@ -34,7 +34,8 @@ G_BEGIN_DECLS
 enum {
   PEAS_GTK_PLUGIN_MANAGER_STORE_ENABLED_COLUMN = 0,
   PEAS_GTK_PLUGIN_MANAGER_STORE_CAN_ENABLE_COLUMN,
-  PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_COLUMN,
+  PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_PIXBUF_COLUMN,
+  PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_NAME_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 9741347..36c79c2 100644
--- a/libpeas-gtk/peas-gtk-plugin-manager-view.c
+++ b/libpeas-gtk/peas-gtk-plugin-manager-view.c
@@ -459,6 +459,31 @@ popup_menu_cb (GtkTreeView              *tree_view,
 }
 
 static void
+plugin_icon_data_func (GtkTreeViewColumn *column,
+                       GtkCellRenderer   *cell,
+                       GtkTreeModel      *model,
+                       GtkTreeIter       *iter)
+{
+  GdkPixbuf *icon_pixbuf;
+  gchar *icon_name;
+
+  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,
+    -1);
+
+  if (icon_pixbuf == NULL)
+    g_object_set (cell, "icon-name", icon_name, NULL);
+  else
+    {
+      g_object_set (cell, "pixbuf", icon_pixbuf, NULL);
+      g_object_unref (icon_pixbuf);
+    }
+
+  g_free (icon_name);
+}
+
+static void
 peas_gtk_plugin_manager_view_init (PeasGtkPluginManagerView *view)
 {
   GtkTreeViewColumn *column;
@@ -507,10 +532,9 @@ peas_gtk_plugin_manager_view_init (PeasGtkPluginManagerView *view)
   cell = gtk_cell_renderer_pixbuf_new ();
   gtk_tree_view_column_pack_start (column, cell, FALSE);
   g_object_set (cell, "stock-size", GTK_ICON_SIZE_SMALL_TOOLBAR, NULL);
-  gtk_tree_view_column_set_attributes (column, cell,
-                                       //"sensitive", PEAS_GTK_PLUGIN_MANAGER_STORE_CAN_ENABLE_COLUMN,
-                                       "icon-name", PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_COLUMN,
-                                       NULL);
+  gtk_tree_view_column_set_cell_data_func (column, cell,
+                                           (GtkTreeCellDataFunc) plugin_icon_data_func,
+                                           NULL, NULL);
 
   cell = gtk_cell_renderer_text_new ();
   gtk_tree_view_column_pack_start (column, cell, TRUE);
diff --git a/peas-demo/plugins/secondtime/gnome-foot.png b/peas-demo/plugins/secondtime/gnome-foot.png
new file mode 100644
index 0000000..0476658
Binary files /dev/null and b/peas-demo/plugins/secondtime/gnome-foot.png differ
diff --git a/peas-demo/plugins/secondtime/secondtime.plugin b/peas-demo/plugins/secondtime/secondtime.plugin
index 8509afa..a34647d 100644
--- a/peas-demo/plugins/secondtime/secondtime.plugin
+++ b/peas-demo/plugins/secondtime/secondtime.plugin
@@ -2,6 +2,7 @@
 Module=secondtime
 Depends=helloworld
 IAge=2
+Icon=gnome-foot.png
 Name=A Second Time!
 Description=Inserts a box containing "A second time!" in every windows.
 Authors=Steve Frécinaux <code istique net>
diff --git a/tests/libpeas-gtk/plugin-manager-store.c b/tests/libpeas-gtk/plugin-manager-store.c
index db21ce4..1b653e6 100644
--- a/tests/libpeas-gtk/plugin-manager-store.c
+++ b/tests/libpeas-gtk/plugin-manager-store.c
@@ -168,29 +168,29 @@ static void
 verify_model (TestFixture    *fixture,
               PeasPluginInfo *info,
               gboolean        can_enable,
-              const gchar    *icon,
+              const gchar    *icon_name,
               gboolean        icon_visible,
               gboolean        info_sensitive)
 {
   GtkTreeIter iter;
   gboolean model_can_enable, model_icon_visible, model_info_sensitive;
-  gchar *model_icon;
+  gchar *model_icon_name;
 
   testing_get_iter_for_plugin_info (fixture->view, info, &iter);
 
   gtk_tree_model_get (fixture->model, &iter,
-    PEAS_GTK_PLUGIN_MANAGER_STORE_CAN_ENABLE_COLUMN,      &model_can_enable,
-    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_COLUMN,            &model_icon,
-    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_VISIBLE_COLUMN,    &model_icon_visible,
-    PEAS_GTK_PLUGIN_MANAGER_STORE_INFO_SENSITIVE_COLUMN,  &model_info_sensitive,
+    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_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, ==, icon);
+  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);
+  g_free (model_icon_name);
 }
 
 static void
@@ -275,7 +275,7 @@ verify_icon (TestFixture *fixture,
   testing_get_iter_for_plugin_info (fixture->view, info, &iter);
 
   gtk_tree_model_get (fixture->model, &iter,
-    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_COLUMN, &model_icon_name,
+    PEAS_GTK_PLUGIN_MANAGER_STORE_ICON_NAME_COLUMN, &model_icon_name,
     -1);
 
   g_assert_cmpstr (model_icon_name, ==, icon_name);
diff --git a/tests/libpeas/plugin-info.c b/tests/libpeas/plugin-info.c
index 5f8bd95..de0f2fd 100644
--- a/tests/libpeas/plugin-info.c
+++ b/tests/libpeas/plugin-info.c
@@ -79,7 +79,7 @@ test_plugin_info_verify_full_info (PeasEngine *engine)
 
   g_assert_cmpstr (peas_plugin_info_get_name (info), ==, "Full Info");
   g_assert_cmpstr (peas_plugin_info_get_description (info), ==, "Has full info.");
-  g_assert_cmpstr (peas_plugin_info_get_icon_name (info), ==, "full-info-icon");
+  g_assert_cmpstr (peas_plugin_info_get_icon_name (info), ==, "gtk-ok");
   g_assert_cmpstr (peas_plugin_info_get_website (info), ==, "http://live.gnome.org/Libpeas";);
   g_assert_cmpstr (peas_plugin_info_get_copyright (info), ==, "Copyright © 2010 Garrett Regier");
   g_assert_cmpstr (peas_plugin_info_get_version (info), ==, "1.0");
diff --git a/tests/plugins/full-info.plugin b/tests/plugins/full-info.plugin
index 21ee9c9..5cb1390 100644
--- a/tests/plugins/full-info.plugin
+++ b/tests/plugins/full-info.plugin
@@ -8,6 +8,6 @@ Description=Has full info.
 Authors=Garrett Regier
 Copyright=Copyright © 2010 Garrett Regier
 Website=http://live.gnome.org/Libpeas
-Icon=full-info-icon
+Icon=gtk-ok
 Version=1.0
 Help=http://git.gnome.org/browse/libpeas



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