[gnome-software] Add the key color metadata replacement into a trivial plugin



commit e684ce9d2322a7198e3276cbad2147b1115af408
Author: Richard Hughes <richard hughsie com>
Date:   Tue May 16 14:11:24 2017 +0100

    Add the key color metadata replacement into a trivial plugin
    
    This also allows us to simplify setting the CSS markup on the widgets.

 contrib/gnome-software.spec.in               |    1 +
 plugins/core/gs-plugin-key-colors-metadata.c |   87 ++++++++++++++++++++++++++
 plugins/core/meson.build                     |   12 ++++
 src/gs-category-tile.c                       |    4 +-
 src/gs-common.c                              |   46 +-------------
 src/gs-common.h                              |    5 +-
 src/gs-details-page.c                        |    2 +-
 src/gs-feature-tile.c                        |   12 ++--
 src/gs-popular-tile.c                        |    2 +-
 src/gs-summary-tile.c                        |    2 +-
 src/gs-upgrade-banner.c                      |    2 +-
 11 files changed, 114 insertions(+), 61 deletions(-)
---
diff --git a/contrib/gnome-software.spec.in b/contrib/gnome-software.spec.in
index 7505b95..37fb972 100644
--- a/contrib/gnome-software.spec.in
+++ b/contrib/gnome-software.spec.in
@@ -173,6 +173,7 @@ glib-compile-schemas %{_datadir}/glib-2.0/schemas &> /dev/null || :
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_hardcoded-popular.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_icons.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_key-colors.so
+%{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_key-colors-metadata.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_modalias.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_odrs.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_os-release.so
diff --git a/plugins/core/gs-plugin-key-colors-metadata.c b/plugins/core/gs-plugin-key-colors-metadata.c
new file mode 100644
index 0000000..ba03a0e
--- /dev/null
+++ b/plugins/core/gs-plugin-key-colors-metadata.c
@@ -0,0 +1,87 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2017 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <config.h>
+
+#include <gnome-software.h>
+
+void
+gs_plugin_initialize (GsPlugin *plugin)
+{
+       gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "key-colors");
+}
+
+gboolean
+gs_plugin_refine_app (GsPlugin *plugin,
+                     GsApp *app,
+                     GsPluginRefineFlags flags,
+                     GCancellable *cancellable,
+                     GError **error)
+{
+       GPtrArray *key_colors;
+       const gchar *keys[] = {
+               "GnomeSoftware::AppTile-css",
+               "GnomeSoftware::FeatureTile-css",
+               "GnomeSoftware::UpgradeBanner-css",
+               NULL };
+
+       /* not set */
+       key_colors = gs_app_get_key_colors (app);
+       if (key_colors->len == 0)
+               return TRUE;
+
+       /* rewrite URIs */
+       for (guint i = 0; keys[i] != NULL; i++) {
+               const gchar *css;
+               g_autoptr(GString) css_new = NULL;
+
+               /* metadata is not set */
+               css = gs_app_get_metadata_item (app, keys[i]);
+               if (css == NULL)
+                       continue;
+               if (g_strstr_len (css, -1, "@keycolor") == NULL)
+                       continue;
+
+               /* replace key color values */
+               css_new = g_string_new (css);
+               for (guint j = 0; j < key_colors->len; j++) {
+                       GdkRGBA *color = g_ptr_array_index (key_colors, j);
+                       g_autofree gchar *key = NULL;
+                       g_autofree gchar *value = NULL;
+                       key = g_strdup_printf ("@keycolor-%02u@", j);
+                       value = g_strdup_printf ("rgb(%.0f,%.0f,%.0f)",
+                                                color->red * 255.f,
+                                                color->green * 255.f,
+                                                color->blue * 255.f);
+                       as_utils_string_replace (css_new, key, value);
+               }
+
+               /* only replace if it's different */
+               if (g_strcmp0 (css, css_new->str) != 0) {
+                       g_debug ("replacing %s with %s", css, css_new->str);
+                       gs_app_set_metadata (app, keys[i], NULL);
+                       gs_app_set_metadata (app, keys[i], css_new->str);
+               }
+
+       }
+
+       return TRUE;
+}
diff --git a/plugins/core/meson.build b/plugins/core/meson.build
index 1002b14..4fb2870 100644
--- a/plugins/core/meson.build
+++ b/plugins/core/meson.build
@@ -26,6 +26,18 @@ shared_module(
   dependencies : plugin_libs
 )
 
+shared_module(
+  'gs_plugin_key-colors-metadata',
+  sources : 'gs-plugin-key-colors-metadata.c',
+  include_directories : [
+    include_directories('../..'),
+    include_directories('../../lib'),
+  ],
+  install : true,
+  install_dir: plugin_dir,
+  c_args : cargs,
+  dependencies : plugin_libs
+)
 
 shared_module(
   'gs_plugin_provenance',
diff --git a/src/gs-category-tile.c b/src/gs-category-tile.c
index 82daf47..7d4c15c 100644
--- a/src/gs-category-tile.c
+++ b/src/gs-category-tile.c
@@ -63,9 +63,9 @@ gs_category_tile_refresh (GsCategoryTile *tile)
                g_autofree gchar *css = NULL;
                g_autofree gchar *color = gdk_rgba_to_string (tmp);;
                css = g_strdup_printf ("border-bottom: 3px solid %s", color);
-               gs_utils_widget_set_css_simple (GTK_WIDGET (tile), css);
+               gs_utils_widget_set_css (GTK_WIDGET (tile), css);
        } else {
-               gs_utils_widget_set_css_simple (GTK_WIDGET (tile), NULL);
+               gs_utils_widget_set_css (GTK_WIDGET (tile), NULL);
        }
 }
 
diff --git a/src/gs-common.c b/src/gs-common.c
index e2d6a81..995d7fa 100644
--- a/src/gs-common.c
+++ b/src/gs-common.c
@@ -365,7 +365,7 @@ gs_utils_widget_set_css_internal (GtkWidget *widget,
 }
 
 void
-gs_utils_widget_set_css_simple (GtkWidget *widget, const gchar *css)
+gs_utils_widget_set_css (GtkWidget *widget, const gchar *css)
 {
        g_autofree gchar *class_name = NULL;
        g_autoptr(GString) str = NULL;
@@ -385,50 +385,6 @@ gs_utils_widget_set_css_simple (GtkWidget *widget, const gchar *css)
        gs_utils_widget_set_css_internal (widget, class_name, str->str);
 }
 
-void
-gs_utils_widget_set_css_app (GsApp *app, GtkWidget *widget, const gchar *css)
-{
-       GPtrArray *key_colors;
-       guint i;
-       g_autofree gchar *class_name = NULL;
-       g_autoptr(GString) css_str = NULL;
-       g_autoptr(GString) str = g_string_sized_new (1024);
-
-       g_return_if_fail (GS_IS_APP (app));
-
-       /* invalid */
-       if (css == NULL) {
-               gs_utils_widget_set_css_simple (widget, css);
-               return;
-       }
-
-       /* replace any key colors */
-       css_str = g_string_new (css);
-       key_colors = gs_app_get_key_colors (app);
-       for (i = 0; i < key_colors->len; i++) {
-               GdkRGBA *color = g_ptr_array_index (key_colors, 1);
-               g_autofree gchar *key = NULL;
-               g_autofree gchar *value = NULL;
-               key = g_strdup_printf ("@keycolor-%02u@", i);
-               value = g_strdup_printf ("rgb(%.0f,%.0f,%.0f)",
-                                        color->red * 255.f,
-                                        color->green * 255.f,
-                                        color->blue * 255.f);
-               as_utils_string_replace (css_str, key, value);
-       }
-
-       /* make into a proper CSS class */
-       class_name = g_strdup_printf ("themed-widget_%p", widget);
-       g_string_append_printf (str, ".%s {\n", class_name);
-       g_string_append_printf (str, "%s\n", css_str->str);
-       g_string_append (str, "}");
-
-       g_string_append_printf (str, ".%s:hover {\n", class_name);
-       g_string_append (str, "  opacity: 0.9;\n");
-       g_string_append (str, "}\n");
-       gs_utils_widget_set_css_internal (widget, class_name, str->str);
-}
-
 static void
 do_not_expand (GtkWidget *child, gpointer data)
 {
diff --git a/src/gs-common.h b/src/gs-common.h
index b33b8f3..1582656 100644
--- a/src/gs-common.h
+++ b/src/gs-common.h
@@ -46,10 +46,7 @@ void gs_image_set_from_pixbuf                (GtkImage               *image,
                                                 const GdkPixbuf        *pixbuf);
 
 gboolean        gs_utils_is_current_desktop    (const gchar    *name);
-void            gs_utils_widget_set_css_app    (GsApp          *app,
-                                                GtkWidget      *widget,
-                                                const gchar    *metadata_css);
-void            gs_utils_widget_set_css_simple (GtkWidget      *widget,
+void            gs_utils_widget_set_css        (GtkWidget      *widget,
                                                 const gchar    *css);
 const gchar    *gs_utils_get_error_value       (const GError   *error);
 void            gs_utils_show_error_dialog     (GtkWindow      *parent,
diff --git a/src/gs-details-page.c b/src/gs-details-page.c
index d58c45c..ee6055c 100644
--- a/src/gs-details-page.c
+++ b/src/gs-details-page.c
@@ -1355,7 +1355,7 @@ gs_details_page_content_rating_set_css (GtkWidget *widget, guint age)
        }
        g_string_append_printf (css, "color: %s;\n", color_fg);
        g_string_append_printf (css, "background-color: %s;\n", color_bg);
-       gs_utils_widget_set_css_simple (widget, css->str);
+       gs_utils_widget_set_css (widget, css->str);
 }
 
 static void
diff --git a/src/gs-feature-tile.c b/src/gs-feature-tile.c
index 323e0de..d510d1a 100644
--- a/src/gs-feature-tile.c
+++ b/src/gs-feature-tile.c
@@ -68,12 +68,12 @@ app_state_changed_idle (gpointer user_data)
        css = gs_css_new ();
        if (markup != NULL)
                gs_css_parse (css, markup, NULL);
-       gs_utils_widget_set_css_app (tile->app, GTK_WIDGET (tile),
-                                    gs_css_get_markup_for_id (css, "tile"));
-       gs_utils_widget_set_css_simple (tile->title,
-                                       gs_css_get_markup_for_id (css, "name"));
-       gs_utils_widget_set_css_simple (tile->subtitle,
-                                       gs_css_get_markup_for_id (css, "summary"));
+       gs_utils_widget_set_css (GTK_WIDGET (tile),
+                                gs_css_get_markup_for_id (css, "tile"));
+       gs_utils_widget_set_css (tile->title,
+                                gs_css_get_markup_for_id (css, "name"));
+       gs_utils_widget_set_css (tile->subtitle,
+                                gs_css_get_markup_for_id (css, "summary"));
 
        accessible = gtk_widget_get_accessible (GTK_WIDGET (tile));
 
diff --git a/src/gs-popular-tile.c b/src/gs-popular-tile.c
index 1047e6d..2b6b30b 100644
--- a/src/gs-popular-tile.c
+++ b/src/gs-popular-tile.c
@@ -127,7 +127,7 @@ gs_popular_tile_set_app (GsAppTile *app_tile, GsApp *app)
 
        /* perhaps set custom css */
        css = gs_app_get_metadata_item (app, "GnomeSoftware::PopularTile-css");
-       gs_utils_widget_set_css_app (app, GTK_WIDGET (tile), css);
+       gs_utils_widget_set_css (GTK_WIDGET (tile), css);
 
        if (gs_app_get_pixbuf (tile->app) != NULL) {
                gs_image_set_from_pixbuf (GTK_IMAGE (tile->image), gs_app_get_pixbuf (tile->app));
diff --git a/src/gs-summary-tile.c b/src/gs-summary-tile.c
index 4162953..00e71bb 100644
--- a/src/gs-summary-tile.c
+++ b/src/gs-summary-tile.c
@@ -152,7 +152,7 @@ gs_summary_tile_set_app (GsAppTile *app_tile, GsApp *app)
 
        /* perhaps set custom css */
        css = gs_app_get_metadata_item (app, "GnomeSoftware::AppTile-css");
-       gs_utils_widget_set_css_app (app, GTK_WIDGET (tile), css);
+       gs_utils_widget_set_css (GTK_WIDGET (tile), css);
 
        /* some kinds have boring summaries */
        switch (gs_app_get_kind (app)) {
diff --git a/src/gs-upgrade-banner.c b/src/gs-upgrade-banner.c
index 4080f4a..edb43ef 100644
--- a/src/gs-upgrade-banner.c
+++ b/src/gs-upgrade-banner.c
@@ -228,7 +228,7 @@ gs_upgrade_banner_set_app (GsUpgradeBanner *self, GsApp *app)
 
        /* perhaps set custom css */
        css = gs_app_get_metadata_item (app, "GnomeSoftware::UpgradeBanner-css");
-       gs_utils_widget_set_css_app (app, priv->box_upgrades, css);
+       gs_utils_widget_set_css (priv->box_upgrades, css);
 
        gs_upgrade_banner_refresh (self);
 }


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