[gnome-software] Move all the icon loading to the icons plugin



commit f457dae6560799c3cd8ec9a738c0504547231d4e
Author: Richard Hughes <richard hughsie com>
Date:   Fri May 27 14:48:12 2016 +0100

    Move all the icon loading to the icons plugin

 src/plugins/gs-appstream.c    |   20 +++---
 src/plugins/gs-plugin-icons.c |  179 ++++++++++++++++++++++++-----------------
 2 files changed, 114 insertions(+), 85 deletions(-)
---
diff --git a/src/plugins/gs-appstream.c b/src/plugins/gs-appstream.c
index e53b199..ee5044d 100644
--- a/src/plugins/gs-appstream.c
+++ b/src/plugins/gs-appstream.c
@@ -32,14 +32,13 @@ gs_refine_item_pixbuf (GsPlugin *plugin, GsApp *app, AsApp *item)
 {
        AsIcon *icon;
        g_autoptr(GError) error = NULL;
-       g_autofree gchar *fn = NULL;
-       g_autofree gchar *cachedir = NULL;
 
        icon = as_app_get_icon_default (item);
-       gs_app_set_icon (app, icon);
        switch (as_icon_get_kind (icon)) {
        case AS_ICON_KIND_REMOTE:
                if (as_icon_get_filename (icon) == NULL) {
+                       g_autofree gchar *fn = NULL;
+                       g_autofree gchar *cachedir = NULL;
                        fn = gs_utils_get_cache_filename ("icons",
                                                          as_icon_get_name (icon),
                                                          GS_UTILS_CACHE_FLAG_WRITEABLE,
@@ -48,14 +47,20 @@ gs_refine_item_pixbuf (GsPlugin *plugin, GsApp *app, AsApp *item)
                        cachedir = g_path_get_basename (fn);
                        as_icon_set_prefix (icon, cachedir);
                }
+               gs_app_set_icon (app, icon);
                break;
        case AS_ICON_KIND_STOCK:
+               gs_app_set_icon (app, icon);
                break;
        case AS_ICON_KIND_LOCAL:
                /* does not exist, so try to find using the icon theme */
                if (as_icon_get_kind (icon) == AS_ICON_KIND_LOCAL &&
-                   as_icon_get_filename (icon) == NULL)
+                   as_icon_get_filename (icon) == NULL) {
+                       g_debug ("converting missing LOCAL icon %s to STOCK",
+                                as_icon_get_name (icon));
                        as_icon_set_kind (icon, AS_ICON_KIND_STOCK);
+               }
+               gs_app_set_icon (app, icon);
                break;
        case AS_ICON_KIND_CACHED:
                if (gs_plugin_get_scale (plugin) == 2)
@@ -67,12 +72,7 @@ gs_refine_item_pixbuf (GsPlugin *plugin, GsApp *app, AsApp *item)
                                   as_app_get_id (item));
                        return;
                }
-               if (!as_icon_load (icon, AS_ICON_LOAD_FLAG_SEARCH_SIZE, &error)) {
-                       g_warning ("failed to load cached icon %s: %s",
-                                  as_icon_get_name (icon), error->message);
-                               return;
-               }
-               gs_app_set_pixbuf (app, as_icon_get_pixbuf (icon));
+               gs_app_set_icon (app, icon);
                break;
        default:
                g_warning ("icon kind unknown for %s", as_app_get_id (item));
diff --git a/src/plugins/gs-plugin-icons.c b/src/plugins/gs-plugin-icons.c
index 6d9651a..e6f3cae 100644
--- a/src/plugins/gs-plugin-icons.c
+++ b/src/plugins/gs-plugin-icons.c
@@ -63,7 +63,10 @@ gs_plugin_destroy (GsPlugin *plugin)
 }
 
 static gboolean
-gs_plugin_icons_download (GsPlugin *plugin, const gchar *uri, const gchar *filename, GError **error)
+gs_plugin_icons_download (GsPlugin *plugin,
+                         const gchar *uri,
+                         const gchar *filename,
+                         GError **error)
 {
        guint status_code;
        g_autoptr(GdkPixbuf) pixbuf_new = NULL;
@@ -111,29 +114,61 @@ gs_plugin_icons_download (GsPlugin *plugin, const gchar *uri, const gchar *filen
        return gdk_pixbuf_save (pixbuf_new, filename, "png", error, NULL);
 }
 
-static gboolean
-gs_plugin_icons_load_local (GsPlugin *plugin, GsApp *app, GError **error)
+static GdkPixbuf *
+gs_plugin_icons_load_local (GsPlugin *plugin, AsIcon *icon, GError **error)
 {
-       AsIcon *icon;
-       g_autoptr(GdkPixbuf) pixbuf = NULL;
+       if (as_icon_get_filename (icon) == NULL) {
+               g_set_error_literal (error,
+                                    GS_PLUGIN_ERROR,
+                                    GS_PLUGIN_ERROR_FAILED,
+                                    "icon has no filename");
+               return NULL;
+       }
+       return gdk_pixbuf_new_from_file_at_size (as_icon_get_filename (icon),
+                                                64 * gs_plugin_get_scale (plugin),
+                                                64 * gs_plugin_get_scale (plugin),
+                                                error);
+}
 
-       icon = gs_app_get_icon (app);
+static GdkPixbuf *
+gs_plugin_icons_load_remote (GsPlugin *plugin, AsIcon *icon, GError **error)
+{
+       const gchar *fn;
+       gchar *found;
+
+       /* not applicable for remote */
+       if (as_icon_get_url (icon) == NULL) {
+               g_set_error_literal (error,
+                                    GS_PLUGIN_ERROR,
+                                    GS_PLUGIN_ERROR_FAILED,
+                                    "icon has no URL");
+               return NULL;
+       }
        if (as_icon_get_filename (icon) == NULL) {
-               g_set_error (error,
-                            GS_PLUGIN_ERROR,
-                            GS_PLUGIN_ERROR_FAILED,
-                            "%s icon has no filename",
-                            gs_app_get_id (app));
-               return FALSE;
+               g_error ("MOO");
+               return NULL;
        }
-       pixbuf = gdk_pixbuf_new_from_file_at_size (as_icon_get_filename (icon),
-                                                  64 * gs_plugin_get_scale (plugin),
-                                                  64 * gs_plugin_get_scale (plugin),
-                                                  error);
-       if (pixbuf == NULL)
-               return FALSE;
-       gs_app_set_pixbuf (app, pixbuf);
-       return TRUE;
+
+       /* a REMOTE that's really LOCAL */
+       if (g_str_has_prefix (as_icon_get_url (icon), "file://")) {
+               as_icon_set_filename (icon, as_icon_get_url (icon) + 7);
+               as_icon_set_kind (icon, AS_ICON_KIND_LOCAL);
+               return gs_plugin_icons_load_local (plugin, icon, error);
+       }
+
+       /* convert filename from jpg to png */
+       fn = as_icon_get_filename (icon);
+       found = g_strstr_len (fn, -1, ".jpg");
+       if (found != NULL)
+               memcpy (found, ".png", 4);
+
+       /* create runtime dir and download */
+       if (!gs_mkdir_parent (fn, error))
+               return NULL;
+       if (!gs_plugin_icons_download (plugin, as_icon_get_url (icon), fn, error))
+               return NULL;
+       as_icon_set_kind (icon, AS_ICON_KIND_LOCAL);
+       return gs_plugin_icons_load_local (plugin, icon, error);
 }
 
 static void
@@ -148,34 +183,35 @@ gs_plugin_icons_add_theme_path (GsPlugin *plugin, const gchar *path)
        }
 }
 
-static gboolean
-gs_plugin_icons_load_stock (GsPlugin *plugin, GsApp *app, GError **error)
+static GdkPixbuf *
+gs_plugin_icons_load_stock (GsPlugin *plugin, AsIcon *icon, GError **error)
 {
        GsPluginData *priv = gs_plugin_get_data (plugin);
-       AsIcon *icon;
-       g_autoptr(GdkPixbuf) pixbuf = NULL;
        g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->icon_theme_lock);
 
-       icon = gs_app_get_icon (app);
+       /* required */
        if (as_icon_get_name (icon) == NULL) {
-               g_set_error (error,
-                            GS_PLUGIN_ERROR,
-                            GS_PLUGIN_ERROR_FAILED,
-                            "%s icon has no name",
-                            gs_app_get_id (app));
-               return FALSE;
+               g_set_error_literal (error,
+                                    GS_PLUGIN_ERROR,
+                                    GS_PLUGIN_ERROR_FAILED,
+                                    "icon has no name");
+               return NULL;
        }
        gs_plugin_icons_add_theme_path (plugin, as_icon_get_prefix (icon));
-       pixbuf = gtk_icon_theme_load_icon (priv->icon_theme,
-                                          as_icon_get_name (icon),
-                                          64 * gs_plugin_get_scale (plugin),
-                                          GTK_ICON_LOOKUP_USE_BUILTIN |
-                                          GTK_ICON_LOOKUP_FORCE_SIZE,
-                                          error);
-       if (pixbuf == NULL)
-               return FALSE;
-       gs_app_set_pixbuf (app, pixbuf);
-       return TRUE;
+       return gtk_icon_theme_load_icon (priv->icon_theme,
+                                        as_icon_get_name (icon),
+                                        64 * gs_plugin_get_scale (plugin),
+                                        GTK_ICON_LOOKUP_USE_BUILTIN |
+                                        GTK_ICON_LOOKUP_FORCE_SIZE,
+                                        error);
+}
+
+static GdkPixbuf *
+gs_plugin_icons_load_cached (GsPlugin *plugin, AsIcon *icon, GError **error)
+{
+       if (!as_icon_load (icon, AS_ICON_LOAD_FLAG_SEARCH_SIZE, error))
+               return NULL;
+       return g_object_ref (as_icon_get_pixbuf (icon));
 }
 
 gboolean
@@ -185,9 +221,8 @@ gs_plugin_refine_app (GsPlugin *plugin,
                      GCancellable *cancellable,
                      GError **error)
 {
-       AsIcon *ic;
-       const gchar *fn;
-       gchar *found;
+       AsIcon *icon;
+       g_autoptr(GdkPixbuf) pixbuf = NULL;
 
        /* not required */
        if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_ICON) == 0)
@@ -196,40 +231,34 @@ gs_plugin_refine_app (GsPlugin *plugin,
        /* invalid */
        if (gs_app_get_pixbuf (app) != NULL)
                return TRUE;
-       ic = gs_app_get_icon (app);
-       if (ic == NULL)
+       icon = gs_app_get_icon (app);
+       if (icon == NULL)
                return TRUE;
 
        /* handle different icon types */
-       if (as_icon_get_kind (ic) == AS_ICON_KIND_LOCAL)
-               return gs_plugin_icons_load_local (plugin, app, error);
-       if (as_icon_get_kind (ic) == AS_ICON_KIND_STOCK)
-               return gs_plugin_icons_load_stock (plugin, app, error);
-
-       /* not applicable for remote */
-       if (as_icon_get_url (ic) == NULL)
-               return TRUE;
-       if (as_icon_get_filename (ic) == NULL)
-               return TRUE;
-
-       /* a REMOTE that's really LOCAL */
-       if (g_str_has_prefix (as_icon_get_url (ic), "file://")) {
-               as_icon_set_filename (ic, as_icon_get_url (ic) + 7);
-               as_icon_set_kind (ic, AS_ICON_KIND_LOCAL);
-               return gs_plugin_icons_load_local (plugin, app, error);
+       switch (as_icon_get_kind (icon)) {
+       case AS_ICON_KIND_LOCAL:
+               pixbuf = gs_plugin_icons_load_local (plugin, icon, error);
+               break;
+       case AS_ICON_KIND_STOCK:
+               pixbuf = gs_plugin_icons_load_stock (plugin, icon, error);
+               break;
+       case AS_ICON_KIND_REMOTE:
+               pixbuf = gs_plugin_icons_load_remote (plugin, icon, error);
+               break;
+       case AS_ICON_KIND_CACHED:
+               pixbuf = gs_plugin_icons_load_cached (plugin, icon, error);
+               break;
+       default:
+               g_set_error (error,
+                            GS_PLUGIN_ERROR,
+                            GS_PLUGIN_ERROR_FAILED,
+                            "icon kind '%s' unknown",
+                            as_icon_kind_to_string (as_icon_get_kind (icon)));
+               break;
        }
-
-       /* convert filename from jpg to png */
-       fn = as_icon_get_filename (ic);
-       found = g_strstr_len (fn, -1, ".jpg");
-       if (found != NULL)
-               memcpy (found, ".png", 4);
-
-       /* create runtime dir and download */
-       if (!gs_mkdir_parent (fn, error))
-               return FALSE;
-       if (!gs_plugin_icons_download (plugin, as_icon_get_url (ic), fn, error))
+       if (pixbuf == NULL)
                return FALSE;
-       as_icon_set_kind (ic, AS_ICON_KIND_LOCAL);
-       return gs_plugin_icons_load_local (plugin, app, error);
+       gs_app_set_pixbuf (app, pixbuf);
+       return TRUE;
 }


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