[gnome-software/wip/hughsie/appstream-editor: 11/12] Add a plugin to rewrite CSS resources



commit d00773096e07eb79db2f6bd0dbfc0ba01309dab0
Author: Richard Hughes <richard hughsie com>
Date:   Sat May 6 20:08:35 2017 +0100

    Add a plugin to rewrite CSS resources

 contrib/gnome-software.spec.in            |    1 +
 plugins/core/gs-plugin-rewrite-resource.c |   65 +++++++++++++++++++++++++++++
 plugins/core/meson.build                  |   13 ++++++
 3 files changed, 79 insertions(+), 0 deletions(-)
---
diff --git a/contrib/gnome-software.spec.in b/contrib/gnome-software.spec.in
index ab82d24..0b7600e 100644
--- a/contrib/gnome-software.spec.in
+++ b/contrib/gnome-software.spec.in
@@ -188,6 +188,7 @@ glib-compile-schemas %{_datadir}/glib-2.0/schemas &> /dev/null || :
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_provenance-license.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_provenance.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_repos.so
+%{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_rewrite-resource.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_rpm.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_shell-extensions.so
 %{_libdir}/gs-plugins-%{gs_plugin_version}/libgs_plugin_steam.so
diff --git a/plugins/core/gs-plugin-rewrite-resource.c b/plugins/core/gs-plugin-rewrite-resource.c
new file mode 100644
index 0000000..32b9065
--- /dev/null
+++ b/plugins/core/gs-plugin-rewrite-resource.c
@@ -0,0 +1,65 @@
+/* -*- 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)
+{
+       /* let appstream add metadata first */
+       gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "appstream");
+}
+
+gboolean
+gs_plugin_refine_app (GsPlugin *plugin,
+                     GsApp *app,
+                     GsPluginRefineFlags flags,
+                     GCancellable *cancellable,
+                     GError **error)
+{
+       const gchar *keys[] = {
+               "GnomeSoftware::AppTile-css",
+               "GnomeSoftware::FeatureTile-css",
+               "GnomeSoftware::UpgradeBanner-css",
+               NULL };
+
+       /* rewrite URIs */
+       for (guint i = 0; keys[i] != NULL; i++) {
+               const gchar *css = gs_app_get_metadata_item (app, keys[i]);
+               if (css != NULL) {
+                       g_autofree gchar *css_new = NULL;
+                       css_new = gs_plugin_download_rewrite_resource (plugin,
+                                                                      css,
+                                                                      cancellable,
+                                                                      error);
+                       if (css_new == NULL)
+                               return FALSE;
+                       if (g_strcmp0 (css, css_new) != 0) {
+                               g_debug ("replacing %s with %s", css, css_new);
+                               gs_app_set_metadata (app, keys[i], NULL);
+                               gs_app_set_metadata (app, keys[i], css_new);
+                       }
+               }
+       }
+       return TRUE;
+}
diff --git a/plugins/core/meson.build b/plugins/core/meson.build
index 584322a..ef07d47 100644
--- a/plugins/core/meson.build
+++ b/plugins/core/meson.build
@@ -154,6 +154,19 @@ shared_module(
   dependencies : plugin_libs
 )
 
+shared_module(
+  'gs_plugin_rewrite-resource',
+  sources : 'gs-plugin-rewrite-resource.c',
+  include_directories : [
+    include_directories('../..'),
+    include_directories('../../lib'),
+  ],
+  install : true,
+  install_dir: plugin_dir,
+  c_args : cargs,
+  dependencies : plugin_libs
+)
+
 if get_option('enable-tests')
   cargs += ['-DLOCALPLUGINDIR="' + meson.current_build_dir() + '"']
   cargs += ['-DTESTDATADIR="' + join_paths(meson.current_build_dir(), 'tests') + '"']


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