[gnome-software/wip/ubuntu-3-20: 31/34] snap: Use first featured snap as featured app



commit e358f25cfdd68d5c06f2ce41e9d182407373a2a8
Author: Robert Ancell <robert ancell canonical com>
Date:   Wed Jul 26 10:28:29 2017 +1200

    snap: Use first featured snap as featured app
    
    Use the first featured snap as the featured app. The remaining featured
    apps are used as the editors picks.
    
    We look for screenshots named banner.png and banner-icon.png to use
    as the featured app banner (they are not shown as normal screenshots).

 src/plugins/gs-plugin-snap.c |  103 +++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 102 insertions(+), 1 deletions(-)
---
diff --git a/src/plugins/gs-plugin-snap.c b/src/plugins/gs-plugin-snap.c
index ac20ac9..f160df7 100644
--- a/src/plugins/gs-plugin-snap.c
+++ b/src/plugins/gs-plugin-snap.c
@@ -175,6 +175,98 @@ gs_plugin_destroy (GsPlugin *plugin)
        g_hash_table_unref (plugin->priv->store_snaps);
 }
 
+static gboolean
+is_banner_image (const gchar *filename)
+{
+       /* Check if this screenshot was uploaded as "banner.png" or "banner.jpg".
+        * The server optionally adds a 7 character suffix onto it if it would collide with
+        * an existing name, e.g. "banner_MgEy4MI.png"
+        * See https://forum.snapcraft.io/t/improve-method-for-setting-featured-snap-banner-image-in-store/
+        */
+       return g_regex_match_simple ("^banner(?:_[a-zA-Z0-9]{7})?\\.(?:png|jpg)$", filename, 0, 0);
+}
+
+static gboolean
+is_banner_icon_image (const gchar *filename)
+{
+       /* Check if this screenshot was uploaded as "banner-icon.png" or "banner-icon.jpg".
+        * The server optionally adds a 7 character suffix onto it if it would collide with
+        * an existing name, e.g. "banner-icon_Ugn6pmj.png"
+        * See https://forum.snapcraft.io/t/improve-method-for-setting-featured-snap-banner-image-in-store/
+        */
+       return g_regex_match_simple ("^banner-icon(?:_[a-zA-Z0-9]{7})?\\.(?:png|jpg)$", filename, 0, 0);
+}
+
+gboolean
+gs_plugin_add_featured (GsPlugin *plugin,
+                       GList **list,
+                       GCancellable *cancellable,
+                       GError **error)
+{
+       g_autoptr(JsonArray) snaps = NULL;
+       JsonObject *snap;
+       g_autoptr(GsApp) app = NULL;
+       const gchar *banner_url = NULL, *icon_url = NULL;
+       g_autoptr(GString) background_css = NULL;
+
+       snaps = find_snaps (plugin, "featured", FALSE, NULL, cancellable, error);
+
+       if (snaps == NULL)
+               return FALSE;
+
+       if (json_array_get_length (snaps) < 1)
+               return TRUE;
+
+       /* use first snap as the featured app */
+       snap = json_array_get_object_element (snaps, 0);
+       app = snap_to_app (plugin, snap);
+
+       /* if has a sceenshot called 'banner.png' or 'banner-icon.png' then use them for the banner */
+       if (json_object_has_member (snap, "screenshots")) {
+               JsonArray *screenshots;
+               guint i;
+
+               screenshots = json_object_get_array_member (snap, "screenshots");
+               for (i = 0; i < json_array_get_length (screenshots); i++) {
+                       JsonObject *screenshot = json_array_get_object_element (screenshots, i);
+                       const gchar *url;
+                       g_autofree gchar *filename = NULL;
+
+                       url = json_object_get_string_member (screenshot, "url");
+                       filename = g_path_get_basename (url);
+                       if (is_banner_image (filename))
+                               banner_url = url;
+                       else if (is_banner_icon_image (filename))
+                               icon_url = url;
+               }
+       }
+
+       background_css = g_string_new ("");
+       if (icon_url != NULL)
+               g_string_append_printf (background_css,
+                                       "url('%s') left center / auto 100%% no-repeat, ",
+                                       icon_url);
+       else
+               g_string_append_printf (background_css,
+                                       "url('%s') left center / auto 100%% no-repeat, ",
+                                       json_object_get_string_member (snap, "icon"));
+       if (banner_url != NULL)
+               g_string_append_printf (background_css,
+                                       "url('%s') center / cover no-repeat;",
+                                       banner_url);
+       else
+               g_string_append_printf (background_css, "#FFFFFF;");
+       gs_app_add_kudo (app, GS_APP_KUDO_FEATURED_RECOMMENDED);
+       gs_app_set_metadata (app, "Featured::text-color", "#000000");
+       gs_app_set_metadata (app, "Featured::background", background_css->str);
+       gs_app_set_metadata (app, "Featured::stroke-color", "#000000");
+       gs_app_set_metadata (app, "Featured::text-shadow", "0 1px 1px rgba(255,255,255,0.5)");
+
+       gs_plugin_add_app (list, app);
+
+       return TRUE;
+}
+
 gboolean
 gs_plugin_add_popular (GsPlugin *plugin,
                       GList **list,
@@ -188,7 +280,8 @@ gs_plugin_add_popular (GsPlugin *plugin,
        if (snaps == NULL)
                return FALSE;
 
-       for (i = 0; i < json_array_get_length (snaps); i++) {
+       /* skip first snap - it is used as the featured app */
+       for (i = 1; i < json_array_get_length (snaps); i++) {
                JsonObject *snap = json_array_get_object_element (snaps, i);
                gs_plugin_add_app (list, snap_to_app (plugin, snap));
        }
@@ -427,9 +520,17 @@ gs_plugin_refine_app (GsPlugin *plugin,
                        screenshots = json_object_get_array_member (store_snap, "screenshots");
                        for (i = 0; i < json_array_get_length (screenshots); i++) {
                                JsonObject *screenshot = json_array_get_object_element (screenshots, i);
+                               const gchar *url;
+                               g_autofree gchar *filename = NULL;
                                g_autoptr(AsScreenshot) ss = NULL;
                                g_autoptr(AsImage) image = NULL;
 
+                               /* skip sceenshots used for banner when app is featured */
+                               url = json_object_get_string_member (screenshot, "url");
+                               filename = g_path_get_basename (url);
+                               if (is_banner_image (filename) || is_banner_icon_image (filename))
+                                       continue;
+
                                ss = as_screenshot_new ();
                                as_screenshot_set_kind (ss, AS_SCREENSHOT_KIND_NORMAL);
                                image = as_image_new ();


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