[gnome-software] Ensure the AppStream plugin adds all relevant icons



commit 785f06d4acc6e14bf78ac479a1970bd19a172c55
Author: Richard Hughes <richard hughsie com>
Date:   Fri May 27 15:25:51 2016 +0100

    Ensure the AppStream plugin adds all relevant icons
    
    This means we can have a missing stock icon in the AppStream XML and the
    application still shows, and we can also show the application icons with the
    correct theme.

 src/plugins/gs-appstream.c |  102 +++++++++++++++++++++++++++++--------------
 1 files changed, 69 insertions(+), 33 deletions(-)
---
diff --git a/src/plugins/gs-appstream.c b/src/plugins/gs-appstream.c
index b0cbbfb..948485f 100644
--- a/src/plugins/gs-appstream.c
+++ b/src/plugins/gs-appstream.c
@@ -27,32 +27,67 @@
 
 #define        GS_APPSTREAM_MAX_SCREENSHOTS    5
 
+static AsIcon *
+gs_appstream_get_icon_by_kind (AsApp *app, AsIconKind icon_kind)
+{
+       GPtrArray *icons;
+       guint i;
+
+       icons = as_app_get_icons (app);
+       for (i = 0; i < icons->len; i++) {
+               AsIcon *icon = g_ptr_array_index (icons, i);
+               if (as_icon_get_kind (icon) == icon_kind)
+                       return icon;
+       }
+       return NULL;
+}
+
+static AsIcon *
+gs_appstream_get_icon_by_kind_and_size (AsApp *app, AsIconKind icon_kind, guint sz)
+{
+       GPtrArray *icons;
+       guint i;
+
+       icons = as_app_get_icons (app);
+       for (i = 0; i < icons->len; i++) {
+               AsIcon *icon = g_ptr_array_index (icons, i);
+               if (as_icon_get_kind (icon) == icon_kind &&
+                   as_icon_get_width (icon) == sz &&
+                   as_icon_get_height (icon) == sz)
+                       return icon;
+       }
+       return NULL;
+}
+
 static void
 gs_refine_item_pixbuf (GsPlugin *plugin, GsApp *app, AsApp *item)
 {
        AsIcon *icon;
-       g_autoptr(GError) error = NULL;
 
-       icon = as_app_get_icon_default (item);
-       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,
-                                                         NULL);
-                       as_icon_set_filename (icon, fn);
-                       cachedir = g_path_get_basename (fn);
-                       as_icon_set_prefix (icon, cachedir);
-               }
+       /* try a stock icon first */
+       icon = gs_appstream_get_icon_by_kind (item, AS_ICON_KIND_STOCK);
+       if (icon != NULL)
                gs_app_add_icon (app, icon);
-               break;
-       case AS_ICON_KIND_STOCK:
+
+       /* if HiDPI get a 128px cached icon */
+       if (gs_plugin_get_scale (plugin) == 2) {
+               icon = gs_appstream_get_icon_by_kind_and_size (item,
+                                                              AS_ICON_KIND_CACHED,
+                                                              128);
+               if (icon != NULL)
+                       gs_app_add_icon (app, icon);
+       }
+
+       /* non-HiDPI cached icon */
+       icon = gs_appstream_get_icon_by_kind_and_size (item,
+                                                      AS_ICON_KIND_CACHED,
+                                                      64);
+       if (icon != NULL)
                gs_app_add_icon (app, icon);
-               break;
-       case AS_ICON_KIND_LOCAL:
+
+       /* prefer local */
+       icon = gs_appstream_get_icon_by_kind (item, AS_ICON_KIND_LOCAL);
+       if (icon != NULL) {
                /* 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) {
@@ -61,22 +96,23 @@ gs_refine_item_pixbuf (GsPlugin *plugin, GsApp *app, AsApp *item)
                        as_icon_set_kind (icon, AS_ICON_KIND_STOCK);
                }
                gs_app_add_icon (app, icon);
-               break;
-       case AS_ICON_KIND_CACHED:
-               if (gs_plugin_get_scale (plugin) == 2)
-                       icon = as_app_get_icon_for_size (item, 128, 128);
-               if (icon == NULL)
-                       icon = as_app_get_icon_for_size (item, 64, 64);
-               if (icon == NULL) {
-                       g_warning ("failed to find cached icon %s",
-                                  as_app_get_id (item));
-                       return;
+       }
+
+       /* remote as a last resort */
+       icon = gs_appstream_get_icon_by_kind (item, AS_ICON_KIND_REMOTE);
+       if (icon != NULL) {
+               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,
+                                                         NULL);
+                       as_icon_set_filename (icon, fn);
+                       cachedir = g_path_get_basename (fn);
+                       as_icon_set_prefix (icon, cachedir);
                }
                gs_app_add_icon (app, icon);
-               break;
-       default:
-               g_warning ("icon kind unknown for %s", as_app_get_id (item));
-               break;
        }
 }
 


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