[gnome-software/1268-appstream-urls-not-properly-supported: 302/304] gs-appstream: Expose url-to-app function as a library function




commit e3f7599b16b46cfd4b05891ff747d6d63bdce73a
Author: Milan Crha <mcrha redhat com>
Date:   Thu Jul 29 14:01:38 2021 +0200

    gs-appstream: Expose url-to-app function as a library function
    
    Moved from the gs-plugin-appstream. It can be used by other plugins,
    which use its own appstream silo.
    
    This also fixes a bug in the xpath, the gs_appstream_create_app()
    expects a `component` element, but this function passed the `id`
    element to it instead.

 lib/gs-appstream.c                 | 37 +++++++++++++++++++++++++++++++++++++
 lib/gs-appstream.h                 |  6 ++++++
 plugins/core/gs-plugin-appstream.c | 23 +----------------------
 3 files changed, 44 insertions(+), 22 deletions(-)
---
diff --git a/lib/gs-appstream.c b/lib/gs-appstream.c
index d48c8f14b..33b9856bd 100644
--- a/lib/gs-appstream.c
+++ b/lib/gs-appstream.c
@@ -1623,6 +1623,43 @@ gs_appstream_add_featured (XbSilo *silo,
        return TRUE;
 }
 
+gboolean
+gs_appstream_url_to_app (GsPlugin *plugin,
+                        XbSilo *silo,
+                        GsAppList *list,
+                        const gchar *url,
+                        GCancellable *cancellable,
+                        GError **error)
+{
+       g_autofree gchar *path = NULL;
+       g_autofree gchar *scheme = NULL;
+       g_autofree gchar *xpath = NULL;
+       g_autoptr(GPtrArray) components = NULL;
+
+       /* not us */
+       scheme = gs_utils_get_url_scheme (url);
+       if (g_strcmp0 (scheme, "appstream") != 0)
+               return TRUE;
+
+       path = gs_utils_get_url_path (url);
+       xpath = g_strdup_printf ("components/component/id[text()='%s']/..", path);
+       components = xb_silo_query (silo, xpath, 0, NULL);
+       if (components == NULL)
+               return TRUE;
+
+       for (guint i = 0; i < components->len; i++) {
+               XbNode *component = g_ptr_array_index (components, i);
+               g_autoptr(GsApp) app = NULL;
+               app = gs_appstream_create_app (plugin, silo, component, error);
+               if (app == NULL)
+                       return FALSE;
+               gs_app_set_scope (app, AS_COMPONENT_SCOPE_SYSTEM);
+               gs_app_list_add (list, app);
+       }
+
+       return TRUE;
+}
+
 void
 gs_appstream_component_add_keyword (XbBuilderNode *component, const gchar *str)
 {
diff --git a/lib/gs-appstream.h b/lib/gs-appstream.h
index 126410c70..5bddf9136 100644
--- a/lib/gs-appstream.h
+++ b/lib/gs-appstream.h
@@ -57,6 +57,12 @@ gboolean      gs_appstream_add_recent                (GsPlugin       *plugin,
                                                         guint64         age,
                                                         GCancellable   *cancellable,
                                                         GError         **error);
+gboolean       gs_appstream_url_to_app                 (GsPlugin       *plugin,
+                                                        XbSilo         *silo,
+                                                        GsAppList      *list,
+                                                        const gchar    *url,
+                                                        GCancellable   *cancellable,
+                                                        GError         **error);
 void            gs_appstream_component_add_extra_info  (XbBuilderNode  *component);
 void            gs_appstream_component_add_keyword     (XbBuilderNode  *component,
                                                         const gchar    *str);
diff --git a/plugins/core/gs-plugin-appstream.c b/plugins/core/gs-plugin-appstream.c
index fcdd9dc77..4518e19d3 100644
--- a/plugins/core/gs-plugin-appstream.c
+++ b/plugins/core/gs-plugin-appstream.c
@@ -691,36 +691,15 @@ gs_plugin_url_to_app (GsPlugin *plugin,
                      GError **error)
 {
        GsPluginData *priv = gs_plugin_get_data (plugin);
-       g_autofree gchar *path = NULL;
-       g_autofree gchar *scheme = NULL;
-       g_autofree gchar *xpath = NULL;
        g_autoptr(GRWLockReaderLocker) locker = NULL;
-       g_autoptr(GsApp) app = NULL;
-       g_autoptr(XbNode) component = NULL;
 
        /* check silo is valid */
        if (!gs_plugin_appstream_check_silo (plugin, cancellable, error))
                return FALSE;
 
-       /* not us */
-       scheme = gs_utils_get_url_scheme (url);
-       if (g_strcmp0 (scheme, "appstream") != 0)
-               return TRUE;
-
        locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
 
-       /* create app */
-       path = gs_utils_get_url_path (url);
-       xpath = g_strdup_printf ("components/component/id[text()='%s']", path);
-       component = xb_silo_query_first (priv->silo, xpath, NULL);
-       if (component == NULL)
-               return TRUE;
-       app = gs_appstream_create_app (plugin, priv->silo, component, error);
-       if (app == NULL)
-               return FALSE;
-       gs_app_set_scope (app, AS_COMPONENT_SCOPE_SYSTEM);
-       gs_app_list_add (list, app);
-       return TRUE;
+       return gs_appstream_url_to_app (plugin, priv->silo, list, url, cancellable, error);
 }
 
 static void


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