[gnome-software] Add icon directory to theme search path.



commit 7a3923b3808fca8aa31e247ff171827f2d3b96e5
Author: William Hua <william hua canonical com>
Date:   Wed Jul 30 18:08:17 2014 +0200

    Add icon directory to theme search path.
    
    Resolves: https://bugzilla.gnome.org/show_bug.cgi?id=733998
    
    Signed-off-by: Richard Hughes <richard hughsie com>

 src/gs-app.c                      |   29 ++++++++++++++++++++++++++++-
 src/gs-app.h                      |    3 +++
 src/gs-shell-details.c            |    8 ++++----
 src/gs-utils.c                    |   29 ++++++++++++++++++++++-------
 src/gs-utils.h                    |    1 +
 src/plugins/gs-plugin-appstream.c |   20 +++++++++++++-------
 6 files changed, 71 insertions(+), 19 deletions(-)
---
diff --git a/src/gs-app.c b/src/gs-app.c
index 2aeecc8..2f21865 100644
--- a/src/gs-app.c
+++ b/src/gs-app.c
@@ -58,6 +58,7 @@ struct GsAppPrivate
        gchar                   *name;
        GsAppQuality             name_quality;
        gchar                   *icon;
+       gchar                   *icon_path;
        GPtrArray               *sources;
        GPtrArray               *source_ids;
        gchar                   *project_group;
@@ -213,6 +214,8 @@ gs_app_to_string (GsApp *app)
                g_string_append_printf (str, "\tname:\t%s\n", priv->name);
        if (priv->icon != NULL)
                g_string_append_printf (str, "\ticon:\t%s\n", priv->icon);
+       if (priv->icon_path != NULL)
+               g_string_append_printf (str, "\ticon-path:\t%s\n", priv->icon_path);
        if (priv->version != NULL)
                g_string_append_printf (str, "\tversion:\t%s\n", priv->version);
        if (priv->version_ui != NULL)
@@ -814,6 +817,29 @@ gs_app_set_icon (GsApp *app, const gchar *icon)
 }
 
 /**
+ * gs_app_get_icon_path:
+ */
+const gchar *
+gs_app_get_icon_path (GsApp *app)
+{
+       g_return_val_if_fail (GS_IS_APP (app), NULL);
+       return app->priv->icon_path;
+}
+
+/**
+ * gs_app_set_icon_path:
+ */
+void
+gs_app_set_icon_path (GsApp *app, const gchar *icon_path)
+{
+       g_return_if_fail (GS_IS_APP (app));
+
+       /* save theme path */
+       g_free (app->priv->icon_path);
+       app->priv->icon_path = g_strdup (icon_path);
+}
+
+/**
  * gs_app_load_icon:
  */
 gboolean
@@ -826,7 +852,7 @@ gs_app_load_icon (GsApp *app, GError **error)
        g_return_val_if_fail (app->priv->icon != NULL, FALSE);
 
        /* either load from the theme or from a file */
-       pixbuf = gs_pixbuf_load (app->priv->icon, 64, error);
+       pixbuf = gs_pixbuf_load (app->priv->icon, app->priv->icon_path, 64, error);
        if (pixbuf == NULL) {
                ret = FALSE;
                goto out;
@@ -2010,6 +2036,7 @@ gs_app_finalize (GObject *object)
        g_free (priv->name);
        g_hash_table_unref (priv->urls);
        g_free (priv->icon);
+       g_free (priv->icon_path);
        g_free (priv->licence);
        g_free (priv->menu_path);
        g_free (priv->origin);
diff --git a/src/gs-app.h b/src/gs-app.h
index 3c64f04..cbd39c7 100644
--- a/src/gs-app.h
+++ b/src/gs-app.h
@@ -196,6 +196,9 @@ void                 gs_app_set_pixbuf              (GsApp          *app,
 const gchar    *gs_app_get_icon                (GsApp          *app);
 void            gs_app_set_icon                (GsApp          *app,
                                                 const gchar    *icon);
+const gchar    *gs_app_get_icon_path           (GsApp          *app);
+void            gs_app_set_icon_path           (GsApp          *app,
+                                                const gchar    *icon_path);
 gboolean        gs_app_load_icon               (GsApp          *app,
                                                 GError         **error);
 GdkPixbuf      *gs_app_get_featured_pixbuf     (GsApp          *app);
diff --git a/src/gs-shell-details.c b/src/gs-shell-details.c
index 48d938d..f70e0a2 100644
--- a/src/gs-shell-details.c
+++ b/src/gs-shell-details.c
@@ -545,7 +545,7 @@ gs_shell_details_refresh_all (GsShellDetails *shell_details)
        /* set the icon */
        tmp = gs_app_get_metadata_item (priv->app, "DataDir::desktop-icon");
        if (tmp != NULL) {
-               pixbuf = gs_pixbuf_load (tmp, 96, &error);
+               pixbuf = gs_pixbuf_load (tmp, NULL, 96, &error);
                if (pixbuf == NULL) {
                        g_warning ("Failed to load desktop icon: %s",
                                   error->message);
@@ -556,11 +556,11 @@ gs_shell_details_refresh_all (GsShellDetails *shell_details)
                pixbuf = gs_app_get_pixbuf (priv->app);
        if (pixbuf == NULL && gs_app_get_state (priv->app) == AS_APP_STATE_AVAILABLE_LOCAL) {
                if (gs_app_get_kind (priv->app) == GS_APP_KIND_SOURCE)
-                       pixbuf = gs_pixbuf_load ("x-package-repository", 96, NULL);
+                       pixbuf = gs_pixbuf_load ("x-package-repository", NULL, 96, NULL);
                else if (gs_shell_details_is_addon_id_kind (priv->app))
-                       pixbuf = gs_pixbuf_load ("application-x-addon", 96, NULL);
+                       pixbuf = gs_pixbuf_load ("application-x-addon", NULL, 96, NULL);
                else
-                       pixbuf = gs_pixbuf_load ("application-x-executable", 96, NULL);
+                       pixbuf = gs_pixbuf_load ("application-x-executable", NULL, 96, NULL);
        }
        if (pixbuf != NULL) {
                gtk_image_set_from_pixbuf (GTK_IMAGE (priv->application_details_icon), pixbuf);
diff --git a/src/gs-utils.c b/src/gs-utils.c
index 004d19b..b8ff61e 100644
--- a/src/gs-utils.c
+++ b/src/gs-utils.c
@@ -247,6 +247,7 @@ gs_mkdir_parent (const gchar *path, GError **error)
 
 static GtkIconTheme *icon_theme_singleton;
 static GMutex        icon_theme_lock;
+static GHashTable   *icon_theme_paths;
 
 static GtkIconTheme *
 icon_theme_get (void)
@@ -257,11 +258,29 @@ icon_theme_get (void)
        return icon_theme_singleton;
 }
 
+static void
+icon_theme_add_path (const gchar *path)
+{
+       if (path == NULL)
+               return;
+
+       if (icon_theme_paths == NULL)
+               icon_theme_paths = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+
+       if (!g_hash_table_contains (icon_theme_paths, path)) {
+               gtk_icon_theme_prepend_search_path (icon_theme_get (), path);
+               g_hash_table_add (icon_theme_paths, g_strdup (path));
+       }
+}
+
 /**
  * gs_pixbuf_load:
  **/
 GdkPixbuf *
-gs_pixbuf_load (const gchar *icon_name, guint icon_size, GError **error)
+gs_pixbuf_load (const gchar *icon_name,
+               const gchar *icon_path,
+               guint icon_size,
+               GError **error)
 {
        GdkPixbuf *pixbuf = NULL;
 
@@ -278,8 +297,9 @@ gs_pixbuf_load (const gchar *icon_name, guint icon_size, GError **error)
                                                           icon_size,
                                                           icon_size,
                                                           error);
-       } else if (g_strstr_len (icon_name, -1, ".") == NULL) {
+       } else {
                g_mutex_lock (&icon_theme_lock);
+               icon_theme_add_path (icon_path);
                pixbuf = gtk_icon_theme_load_icon (icon_theme_get (),
                                                   icon_name,
                                                   icon_size,
@@ -287,11 +307,6 @@ gs_pixbuf_load (const gchar *icon_name, guint icon_size, GError **error)
                                                   GTK_ICON_LOOKUP_FORCE_SIZE,
                                                   error);
                g_mutex_unlock (&icon_theme_lock);
-       } else {
-               g_set_error (error,
-                            GS_PLUGIN_ERROR,
-                            GS_PLUGIN_ERROR_FAILED,
-                            "Failed to load icon '%s'", icon_name);
        }
        return pixbuf;
 }
diff --git a/src/gs-utils.h b/src/gs-utils.h
index cba1d48..6c33eb3 100644
--- a/src/gs-utils.h
+++ b/src/gs-utils.h
@@ -46,6 +46,7 @@ guint  gs_string_replace              (GString        *string,
 gboolean gs_mkdir_parent               (const gchar    *path,
                                         GError         **error);
 GdkPixbuf *gs_pixbuf_load              (const gchar    *icon_name,
+                                        const gchar    *icon_path,
                                         guint           icon_size,
                                         GError         **error);
 void     gs_reboot                      (GCallback       reboot_failed);
diff --git a/src/plugins/gs-plugin-appstream.c b/src/plugins/gs-plugin-appstream.c
index efd2725..5b8c037 100644
--- a/src/plugins/gs-plugin-appstream.c
+++ b/src/plugins/gs-plugin-appstream.c
@@ -185,9 +185,10 @@ gs_plugin_refine_item_pixbuf (GsPlugin *plugin, GsApp *app, AsApp *item)
 {
        GError *error = NULL;
        const gchar *icon;
-       const gchar *icon_dir;
+       const gchar *icon_path;
        gboolean ret;
-       gchar *icon_path = NULL;
+       gchar *icon_no_ext = NULL;
+       gchar *tmp;
 
        icon = as_app_get_icon (item);
        switch (as_app_get_icon_kind (item)) {
@@ -205,11 +206,16 @@ gs_plugin_refine_item_pixbuf (GsPlugin *plugin, GsApp *app, AsApp *item)
                }
                break;
        case AS_ICON_KIND_CACHED:
+               icon_path = as_app_get_icon_path (item);
+
+               /* strip icon extension if present */
+               icon_no_ext = g_strdup (icon);
+               tmp = g_strrstr (icon_no_ext, ".png");
+               if (tmp != NULL)
+                       *tmp = '\0';
 
-               /* we assume <icon type="local">gnome-chess.png</icon> */
-               icon_dir = as_app_get_icon_path (item);
-               icon_path = g_build_filename (icon_dir, icon, NULL);
-               gs_app_set_icon (app, icon_path);
+               gs_app_set_icon (app, icon_no_ext);
+               gs_app_set_icon_path (app, icon_path);
                ret = gs_app_load_icon (app, &error);
                if (!ret) {
                        g_warning ("failed to load cached icon %s: %s",
@@ -223,7 +229,7 @@ gs_plugin_refine_item_pixbuf (GsPlugin *plugin, GsApp *app, AsApp *item)
                break;
        }
 out:
-       g_free (icon_path);
+       g_free (icon_no_ext);
 }
 
 /**


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