[gnome-software] Save the icon in the GsApp object



commit 08b3341bd6ff484991f02c5a019c53fc11862414
Author: Richard Hughes <richard hughsie com>
Date:   Thu Oct 17 20:55:48 2013 +0100

    Save the icon in the GsApp object
    
    We'll need this for future usage.

 src/gs-app.c                      |   49 ++++++++++++++++++++++--------
 src/gs-app.h                      |    5 ++-
 src/gs-plugin-loader.c            |    2 +-
 src/plugins/gs-plugin-appstream.c |   60 ++++++++++++++++--------------------
 4 files changed, 67 insertions(+), 49 deletions(-)
---
diff --git a/src/gs-app.c b/src/gs-app.c
index d6322b4..1bd5006 100644
--- a/src/gs-app.c
+++ b/src/gs-app.c
@@ -56,6 +56,7 @@ struct GsAppPrivate
 {
        gchar                   *id;
        gchar                   *name;
+       gchar                   *icon;
        gchar                   *source;
        gchar                   *project_group;
        gchar                   *version;
@@ -210,6 +211,8 @@ gs_app_to_string (GsApp *app)
                g_string_append_printf (str, "\tid:\t%s\n", priv->id);
        if (priv->name != NULL)
                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->version != NULL)
                g_string_append_printf (str, "\tversion:\t%s\n", priv->version);
        if (priv->version_ui != NULL)
@@ -555,35 +558,54 @@ gs_app_get_pixbuf (GsApp *app)
 }
 
 /**
- * gs_app_set_icon_name:
+ * gs_app_get_icon:
+ */
+const gchar *
+gs_app_get_icon (GsApp *app)
+{
+       g_return_val_if_fail (GS_IS_APP (app), NULL);
+       return app->priv->icon;
+}
+
+/**
+ * gs_app_set_icon:
  */
 gboolean
-gs_app_set_icon_name (GsApp *app, const gchar *icon_name, GError **error)
+gs_app_set_icon (GsApp *app, const gchar *icon, GError **error)
 {
-       GdkPixbuf *pixbuf;
+       GdkPixbuf *pixbuf = NULL;
        gboolean ret = TRUE;
 
        g_return_val_if_fail (GS_IS_APP (app), FALSE);
-       g_return_val_if_fail (icon_name != NULL, FALSE);
+       g_return_val_if_fail (icon != NULL, FALSE);
+
+       /* save icon */
+       g_free (app->priv->icon);
+       app->priv->icon = g_strdup (icon);
 
        /* either load from the theme or from a file */
-       if (icon_name[0] == '/') {
-               pixbuf = gdk_pixbuf_new_from_file_at_size (icon_name,
+       if (icon[0] == '/') {
+               pixbuf = gdk_pixbuf_new_from_file_at_size (icon,
                                                           64, 64,
                                                           error);
-       } else {
+               if (pixbuf == NULL) {
+                       ret = FALSE;
+                       goto out;
+               }
+               gs_app_set_pixbuf (app, pixbuf);
+       } else if (g_strstr_len (icon, -1, ".") == NULL) {
                pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
-                                                  icon_name,
+                                                  icon,
                                                   64,
                                                   GTK_ICON_LOOKUP_USE_BUILTIN |
                                                   GTK_ICON_LOOKUP_FORCE_SIZE,
                                                   error);
+               if (pixbuf == NULL) {
+                       ret = FALSE;
+                       goto out;
+               }
+               gs_app_set_pixbuf (app, pixbuf);
        }
-       if (pixbuf == NULL) {
-               ret = FALSE;
-               goto out;
-       }
-       gs_app_set_pixbuf (app, pixbuf);
 out:
        if (pixbuf != NULL)
                g_object_unref (pixbuf);
@@ -1414,6 +1436,7 @@ gs_app_finalize (GObject *object)
        g_free (priv->id);
        g_free (priv->name);
        g_hash_table_unref (priv->urls);
+       g_free (priv->icon);
        g_free (priv->licence);
        g_free (priv->menu_path);
        g_free (priv->source);
diff --git a/src/gs-app.h b/src/gs-app.h
index 8074050..2480ad7 100644
--- a/src/gs-app.h
+++ b/src/gs-app.h
@@ -163,8 +163,9 @@ void                 gs_app_set_management_plugin   (GsApp          *app,
 GdkPixbuf      *gs_app_get_pixbuf              (GsApp          *app);
 void            gs_app_set_pixbuf              (GsApp          *app,
                                                 GdkPixbuf      *pixbuf);
-gboolean        gs_app_set_icon_name           (GsApp          *app,
-                                                const gchar    *icon_name,
+const gchar    *gs_app_get_icon                (GsApp          *app);
+gboolean        gs_app_set_icon                (GsApp          *app,
+                                                const gchar    *icon,
                                                 GError         **error);
 GdkPixbuf      *gs_app_get_featured_pixbuf     (GsApp          *app);
 void            gs_app_set_featured_pixbuf     (GsApp          *app,
diff --git a/src/gs-plugin-loader.c b/src/gs-plugin-loader.c
index 1437c54..836c2cc 100644
--- a/src/gs-plugin-loader.c
+++ b/src/gs-plugin-loader.c
@@ -1205,7 +1205,7 @@ gs_plugin_loader_convert_unavailable_app (GsApp *app, const gchar *search)
        gs_app_set_summary_missing (app, tmp->str);
        gs_app_set_kind (app, GS_APP_KIND_MISSING);
        gs_app_set_size (app, GS_APP_SIZE_MISSING);
-       gs_app_set_icon_name (app, "dialog-question-symbolic", NULL);
+       gs_app_set_icon (app, "dialog-question-symbolic", NULL);
        g_string_free (tmp, TRUE);
        return TRUE;
 }
diff --git a/src/plugins/gs-plugin-appstream.c b/src/plugins/gs-plugin-appstream.c
index 47f67c6..e76412e 100644
--- a/src/plugins/gs-plugin-appstream.c
+++ b/src/plugins/gs-plugin-appstream.c
@@ -284,12 +284,11 @@ out:
  *
  * Basically, we have to stick on the extension and stat() each file in turn.
  */
-static GdkPixbuf *
+static gchar *
 gs_plugin_refine_search_pixbuf (GsPlugin *plugin,
                                const gchar *icon_dir,
                                const gchar *icon)
 {
-       GdkPixbuf *pixbuf = NULL;
        const gchar *exensions[] = { "png", "svg", "gif", "ico", "xcf", NULL };
        gchar *icon_path;
        guint i;
@@ -300,15 +299,13 @@ gs_plugin_refine_search_pixbuf (GsPlugin *plugin,
                                             icon_dir,
                                             icon,
                                             exensions[i]);
-               pixbuf = gdk_pixbuf_new_from_file_at_size (icon_path,
-                                                          plugin->pixbuf_size,
-                                                          plugin->pixbuf_size,
-                                                          NULL);
+               if (g_file_test (icon_path, G_FILE_TEST_EXISTS))
+                       goto out;
                g_free (icon_path);
-               if (pixbuf != NULL)
-                       break;
+               icon_path = NULL;
        }
-       return pixbuf;
+out:
+       return icon_path;
 }
 
 /**
@@ -317,24 +314,21 @@ gs_plugin_refine_search_pixbuf (GsPlugin *plugin,
 static void
 gs_plugin_refine_item_pixbuf (GsPlugin *plugin, GsApp *app, AppstreamApp *item)
 {
+       GError *error = NULL;
        const gchar *icon;
        const gchar *icon_dir;
+       gboolean ret;
        gchar *icon_path = NULL;
-       GdkPixbuf *pixbuf = NULL;
-       GError *error = NULL;
 
        icon = appstream_app_get_icon (item);
        switch (appstream_app_get_icon_kind (item)) {
        case APPSTREAM_APP_ICON_KIND_STOCK:
-               pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
-                                                  icon,
-                                                  plugin->pixbuf_size,
-                                                  GTK_ICON_LOOKUP_USE_BUILTIN |
-                                                  GTK_ICON_LOOKUP_FORCE_SIZE,
-                                                  &error);
-               if (pixbuf == NULL) {
-                       g_warning ("failed to load stock icon %s", icon);
+               ret = gs_app_set_icon (app, icon, &error);
+               if (!ret) {
+                       g_warning ("failed to load stock icon %s: %s",
+                                  icon, error->message);
                        g_error_free (error);
+                       goto out;
                }
                break;
        case APPSTREAM_APP_ICON_KIND_CACHED:
@@ -342,30 +336,30 @@ gs_plugin_refine_item_pixbuf (GsPlugin *plugin, GsApp *app, AppstreamApp *item)
                /* we assume <icon type="local">gnome-chess.png</icon> */
                icon_dir = appstream_app_get_userdata (item);
                icon_path = g_build_filename (icon_dir, icon, NULL);
-               pixbuf = gdk_pixbuf_new_from_file_at_size (icon_path,
-                                                          plugin->pixbuf_size,
-                                                          plugin->pixbuf_size,
-                                                          &error);
-               if (pixbuf == NULL) {
+               ret = gs_app_set_icon (app, icon_path, &error);
+               if (!ret) {
                        g_warning ("falling back to searching for %s", icon_path);
                        g_error_free (error);
+                       g_free (icon_path);
 
                        /* we are not going to be doing this forever,
                         * SO FIX YOUR APPSTREAM METADATA */
-                       pixbuf = gs_plugin_refine_search_pixbuf (plugin, icon_dir, icon);
-                       if (pixbuf == NULL)
-                               g_warning ("failed to load cached icon %s", icon_path);
+                       icon_path = gs_plugin_refine_search_pixbuf (plugin, icon_dir, icon);
+                       if (icon_path == NULL) {
+                               g_warning ("failed to load cached icon %s", icon);
+                               goto out;
+                       }
+                       ret = gs_app_set_icon (app, icon_path, &error);
+                       g_warning ("failed to load cached icon %s: %s",
+                                  icon, error->message);
+                       g_error_free (error);
+                       goto out;
                }
                break;
        default:
                break;
        }
-
-       /* did we load anything */
-       if (pixbuf != NULL) {
-               gs_app_set_pixbuf (app, pixbuf);
-               g_object_unref (pixbuf);
-       }
+out:
        g_free (icon_path);
 }
 


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