[gnome-software/wip/hughsie/XbBuilderSource-adapter] Fix loading appdata.yml.gz files



commit 18eb458dcd8bdb4f7d12beea6c0209afaa7d387c
Author: Richard Hughes <richard hughsie com>
Date:   Tue Feb 26 18:16:03 2019 +0000

    Fix loading appdata.yml.gz files
    
    Use new API in libxmlb that allows us to chain the content type conversions.

 meson.build                                        |  2 +-
 plugins/core/gs-plugin-appstream.c                 | 36 +++++++++++++---------
 plugins/flatpak/gs-flatpak.c                       | 19 +++++++-----
 .../shell-extensions/gs-plugin-shell-extensions.c  | 16 +++++-----
 4 files changed, 42 insertions(+), 31 deletions(-)
---
diff --git a/meson.build b/meson.build
index 46324002..60035072 100644
--- a/meson.build
+++ b/meson.build
@@ -98,7 +98,7 @@ conf.set('HAVE_LINUX_UNISTD_H', cc.has_header('linux/unistd.h'))
 
 appstream_glib = dependency('appstream-glib', version : '>= 0.7.14')
 gdk_pixbuf = dependency('gdk-pixbuf-2.0', version : '>= 2.32.0')
-libxmlb = dependency('xmlb', version : '>= 0.1.4', fallback : ['libxmlb', 'libxmlb_dep'])
+libxmlb = dependency('xmlb', version : '>= 0.1.7', fallback : ['libxmlb', 'libxmlb_dep'])
 gio_unix = dependency('gio-unix-2.0', version : '>= 2.56.0')
 gmodule = dependency('gmodule-2.0')
 goa = dependency('goa-1.0')
diff --git a/plugins/core/gs-plugin-appstream.c b/plugins/core/gs-plugin-appstream.c
index b035d080..81891f8d 100644
--- a/plugins/core/gs-plugin-appstream.c
+++ b/plugins/core/gs-plugin-appstream.c
@@ -191,19 +191,24 @@ gs_plugin_appstream_load_appdata (GsPlugin *plugin,
 
 static GInputStream *
 gs_plugin_appstream_load_desktop_cb (XbBuilderSource *self,
-                                    GFile *file,
+                                    XbBuilderSourceCtx *ctx,
                                     gpointer user_data,
                                     GCancellable *cancellable,
                                     GError **error)
 {
-       g_autofree gchar *fn = g_file_get_path (file);
-       g_autoptr(AsApp) app = as_app_new ();
        GString *xml;
-       if (!as_app_parse_file (app, fn, AS_APP_PARSE_FLAG_USE_FALLBACKS, error))
+       g_autoptr(AsApp) app = as_app_new ();
+       g_autoptr(GBytes) bytes = NULL;
+       bytes = xb_builder_source_ctx_get_bytes (ctx, cancellable, error);
+       if (bytes == NULL)
+               return NULL;
+       as_app_set_id (app, xb_builder_source_ctx_get_filename (ctx));
+       if (!as_app_parse_data (app, bytes, AS_APP_PARSE_FLAG_USE_FALLBACKS, error))
                return NULL;
        xml = as_app_to_xml (app, error);
        if (xml == NULL)
                return NULL;
+       g_string_prepend (xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        return g_memory_input_stream_new_from_data (g_string_free (xml, FALSE), -1, g_free);
 }
 
@@ -220,10 +225,8 @@ gs_plugin_appstream_load_desktop_fn (GsPlugin *plugin,
        g_autoptr(XbBuilderSource) source = xb_builder_source_new ();
 
        /* add support for desktop files */
-       xb_builder_source_add_converter (source,
-                                        "application/x-desktop",
-                                        gs_plugin_appstream_load_desktop_cb,
-                                        NULL, NULL);
+       xb_builder_source_add_adapter (source, "application/x-desktop",
+                                      gs_plugin_appstream_load_desktop_cb, NULL, NULL);
 
        /* add a dummy package name */
        fixup = xb_builder_fixup_new ("AddDesktopPackageName",
@@ -287,18 +290,23 @@ gs_plugin_appstream_load_desktop (GsPlugin *plugin,
 
 static GInputStream *
 gs_plugin_appstream_load_dep11_cb (XbBuilderSource *self,
-                                  GFile *file,
+                                  XbBuilderSourceCtx *ctx,
                                   gpointer user_data,
                                   GCancellable *cancellable,
                                   GError **error)
 {
        GString *xml;
        g_autoptr(AsStore) store = as_store_new ();
-       if (!as_store_from_file (store, file, NULL, cancellable, error))
+       g_autoptr(GBytes) bytes = NULL;
+       bytes = xb_builder_source_ctx_get_bytes (ctx, cancellable, error);
+       if (bytes == NULL)
+               return NULL;
+       if (!as_store_from_bytes (store, bytes, cancellable, error))
                return FALSE;
        xml = as_store_to_xml (store, AS_NODE_INSERT_FLAG_NONE);
        if (xml == NULL)
                return NULL;
+       g_string_prepend (xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        return g_memory_input_stream_new_from_data (g_string_free (xml, FALSE), -1, g_free);
 }
 
@@ -318,10 +326,10 @@ gs_plugin_appstream_load_appstream_fn (GsPlugin *plugin,
        g_autoptr(XbBuilderSource) source = xb_builder_source_new ();
 
        /* add support for DEP-11 files */
-       xb_builder_source_add_converter (source,
-                                        "application/x-yaml",
-                                        gs_plugin_appstream_load_dep11_cb,
-                                        NULL, NULL);
+       xb_builder_source_add_adapter (source,
+                                      "application/x-yaml",
+                                      gs_plugin_appstream_load_dep11_cb,
+                                      NULL, NULL);
 
        /* add source */
        if (!xb_builder_source_load_file (source, file,
diff --git a/plugins/flatpak/gs-flatpak.c b/plugins/flatpak/gs-flatpak.c
index 7b7c67ea..05b1bd97 100644
--- a/plugins/flatpak/gs-flatpak.c
+++ b/plugins/flatpak/gs-flatpak.c
@@ -552,19 +552,24 @@ gs_flatpak_add_apps_from_xremote (GsFlatpak *self,
 
 static GInputStream *
 gs_plugin_appstream_load_desktop_cb (XbBuilderSource *self,
-                                    GFile *file,
+                                    XbBuilderSourceCtx *ctx,
                                     gpointer user_data,
                                     GCancellable *cancellable,
                                     GError **error)
 {
-       g_autofree gchar *fn = g_file_get_path (file);
-       g_autoptr(AsApp) app = as_app_new ();
        GString *xml;
-       if (!as_app_parse_file (app, fn, AS_APP_PARSE_FLAG_USE_FALLBACKS, error))
+       g_autoptr(AsApp) app = as_app_new ();
+       g_autoptr(GBytes) bytes = NULL;
+       bytes = xb_builder_source_ctx_get_bytes (ctx, cancellable, error);
+       if (bytes == NULL)
+               return NULL;
+       as_app_set_id (app, xb_builder_source_ctx_get_filename (ctx));
+       if (!as_app_parse_data (app, bytes, AS_APP_PARSE_FLAG_USE_FALLBACKS, error))
                return NULL;
        xml = as_app_to_xml (app, error);
        if (xml == NULL)
                return NULL;
+       g_string_prepend (xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        return g_memory_input_stream_new_from_data (g_string_free (xml, FALSE), -1, g_free);
 }
 
@@ -582,10 +587,8 @@ gs_flatpak_load_desktop_fn (GsFlatpak *self,
        g_autoptr(XbBuilderFixup) fixup = NULL;
 
        /* add support for desktop files */
-       xb_builder_source_add_converter (source,
-                                        "application/x-desktop",
-                                        gs_plugin_appstream_load_desktop_cb,
-                                        NULL, NULL);
+       xb_builder_source_add_adapter (source, "application/x-desktop",
+                                      gs_plugin_appstream_load_desktop_cb, NULL, NULL);
 
        /* add the flatpak search keyword */
        fixup = xb_builder_fixup_new ("AddKeywordFlatpak",
diff --git a/plugins/shell-extensions/gs-plugin-shell-extensions.c 
b/plugins/shell-extensions/gs-plugin-shell-extensions.c
index d427ca73..1de48091 100644
--- a/plugins/shell-extensions/gs-plugin-shell-extensions.c
+++ b/plugins/shell-extensions/gs-plugin-shell-extensions.c
@@ -654,7 +654,7 @@ gs_plugin_shell_extensions_parse_app (GsPlugin *plugin,
 
 static GInputStream *
 gs_plugin_appstream_load_json_cb (XbBuilderSource *self,
-                                 GFile *file,
+                                 XbBuilderSourceCtx *ctx,
                                  gpointer user_data,
                                  GCancellable *cancellable,
                                  GError **error)
@@ -665,14 +665,15 @@ gs_plugin_appstream_load_json_cb (XbBuilderSource *self,
        JsonNode *json_root;
        JsonObject *json_item;
        gchar *xml;
-       g_autofree gchar *fn = g_file_get_path (file);
        g_autoptr(AsApp) app = as_app_new ();
        g_autoptr(JsonParser) json_parser = NULL;
        g_autoptr(XbBuilderNode) apps = NULL;
 
        /* parse the data and find the success */
        json_parser = json_parser_new ();
-       if (!json_parser_load_from_file (json_parser, fn, error)) {
+       if (!json_parser_load_from_stream (json_parser,
+                                          xb_builder_source_ctx_get_stream (ctx),
+                                          cancellable, error)) {
                gs_utils_error_convert_json_glib (error);
                return NULL;
        }
@@ -735,7 +736,7 @@ gs_plugin_appstream_load_json_cb (XbBuilderSource *self,
        }
 
        /* convert back to XML */
-       xml = xb_builder_node_export (apps, XB_NODE_EXPORT_FLAG_NONE, error);
+       xml = xb_builder_node_export (apps, XB_NODE_EXPORT_FLAG_ADD_HEADER, error);
        if (xml == NULL)
                return NULL;
        return g_memory_input_stream_new_from_data (xml, -1, g_free);
@@ -823,10 +824,9 @@ _check_silo (GsPlugin *plugin, GCancellable *cancellable, GError **error)
                                          error);
        if (fn == NULL)
                return FALSE;
-       xb_builder_source_add_converter (source,
-                                        "application/json",
-                                        gs_plugin_appstream_load_json_cb,
-                                        plugin, NULL);
+       xb_builder_source_add_adapter (source, "application/json",
+                                      gs_plugin_appstream_load_json_cb,
+                                      plugin, NULL);
        file = g_file_new_for_path (fn);
        if (!xb_builder_source_load_file (source, file,
                                          XB_BUILDER_SOURCE_FLAG_WATCH_FILE,


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