[gnome-software/wip/mak/libas: 6/9] Transparently convert 6-part data-ids to 5-part IDs in public API




commit d7f389583a4704744d900524c2dadb8ebaed5b3a
Author: Matthias Klumpp <matthias tenstral net>
Date:   Thu Jan 28 19:52:18 2021 +0100

    Transparently convert 6-part data-ids to 5-part IDs in public API

 lib/gs-self-test.c   | 17 +++++++++++++++--
 lib/gs-utils.c       | 36 ++++++++++++++++++++++++++++++++++++
 lib/gs-utils.h       |  2 ++
 src/gs-application.c | 14 +++++++++-----
 4 files changed, 62 insertions(+), 7 deletions(-)
---
diff --git a/lib/gs-self-test.c b/lib/gs-self-test.c
index 5f951fdb4..98c2aaf1e 100644
--- a/lib/gs-self-test.c
+++ b/lib/gs-self-test.c
@@ -602,17 +602,30 @@ static void
 gs_app_unique_id_func (void)
 {
        g_autoptr(GsApp) app = gs_app_new (NULL);
+       g_autofree gchar *data_id = NULL;
        const gchar *unique_id;
 
-       unique_id = "system/flatpak/gnome/org.gnome.Software.desktop/master";
+       unique_id = "system/flatpak/gnome/org.gnome.Software/master";
        gs_app_set_from_unique_id (app, unique_id, AS_COMPONENT_KIND_DESKTOP_APP);
        g_assert (GS_IS_APP (app));
        g_assert_cmpint (gs_app_get_scope (app), ==, AS_COMPONENT_SCOPE_SYSTEM);
        g_assert_cmpint (gs_app_get_bundle_kind (app), ==, AS_BUNDLE_KIND_FLATPAK);
        g_assert_cmpstr (gs_app_get_origin (app), ==, "gnome");
        g_assert_cmpint (gs_app_get_kind (app), ==, AS_COMPONENT_KIND_DESKTOP_APP);
-       g_assert_cmpstr (gs_app_get_id (app), ==, "org.gnome.Software.desktop");
+       g_assert_cmpstr (gs_app_get_id (app), ==, "org.gnome.Software");
        g_assert_cmpstr (gs_app_get_branch (app), ==, "master");
+
+       /* test conversions from 6-part IDs */
+       data_id = gs_utils_unique_id_compat_convert (unique_id);
+       g_assert_cmpstr (data_id, ==, unique_id);
+       g_clear_pointer (&data_id, g_free);
+
+       data_id = gs_utils_unique_id_compat_convert ("not a unique ID");
+       g_assert_null (data_id);
+
+       data_id = gs_utils_unique_id_compat_convert 
("system/flatpak/gnome/desktop-app/org.gnome.Software/master");
+       g_assert_cmpstr (data_id, ==, unique_id);
+       g_clear_pointer (&data_id, g_free);
 }
 
 static void
diff --git a/lib/gs-utils.c b/lib/gs-utils.c
index 4c2e2616e..5e8deaab3 100644
--- a/lib/gs-utils.c
+++ b/lib/gs-utils.c
@@ -1223,6 +1223,42 @@ gs_utils_set_online_updates_timestamp (GSettings *settings)
        g_settings_set (settings, "update-notification-timestamp", "x", g_date_time_to_unix (now));
 }
 
+/**
+ * gs_utils_unique_id_compat_convert:
+ * @data_id: (nullable): A string that may be a unique component ID
+ *
+ * Converts the unique ID string from its legacy 6-part form into
+ * a new-style 5-part AppStream data-id.
+ * Does nothing if the string is already valid.
+ *
+ * See !583 for the history of this conversion.
+ *
+ * Returns: (nullable): A newly allocated string with the new-style data-id, or %NULL if input was no valid 
ID.
+ *
+ * Since: 40
+ **/
+gchar*
+gs_utils_unique_id_compat_convert (const gchar *data_id)
+{
+       g_auto(GStrv) parts = NULL;
+       if (data_id == NULL)
+               return NULL;
+
+       /* check for the most common case first: data-id is already valid */
+       if (as_utils_data_id_valid (data_id))
+               return g_strdup (data_id);
+
+       parts = g_strsplit (data_id, "/", -1);
+       if (g_strv_length (parts) != 6)
+               return NULL;
+       return g_strdup_printf ("%s/%s/%s/%s/%s",
+                               parts[0],
+                               parts[1],
+                               parts[2],
+                               parts[4],
+                               parts[5]);
+}
+
 static void
 gs_pixbuf_blur_private (GdkPixbuf *src, GdkPixbuf *dest, guint radius, guint8 *div_kernel_size)
 {
diff --git a/lib/gs-utils.h b/lib/gs-utils.h
index 4933595b5..ae9deb8bb 100644
--- a/lib/gs-utils.h
+++ b/lib/gs-utils.h
@@ -92,6 +92,8 @@ gboolean       gs_utils_parse_evr             (const gchar     *evr,
                                                 gchar          **out_release);
 void            gs_utils_set_online_updates_timestamp (GSettings *settings);
 
+gchar          *gs_utils_unique_id_compat_convert      (const gchar    *data_id);
+
 void            gs_utils_pixbuf_blur           (GdkPixbuf      *src,
                                                 guint          radius,
                                                 guint          iterations);
diff --git a/src/gs-application.c b/src/gs-application.c
index 5f95185c3..48f0c6aac 100644
--- a/src/gs-application.c
+++ b/src/gs-application.c
@@ -594,9 +594,11 @@ details_activated (GSimpleAction *action,
                gs_shell_reset_state (app->shell);
                gs_shell_show_search_result (app->shell, id, search);
        } else {
+               g_autofree gchar *data_id = NULL;
                g_autoptr(GsPluginJob) plugin_job = NULL;
-               if (as_utils_data_id_valid (id)) {
-                       g_autoptr(GsApp) a = gs_plugin_loader_app_create (app->plugin_loader, id);
+               data_id = gs_utils_unique_id_compat_convert (id);
+               if (data_id != NULL) {
+                       g_autoptr(GsApp) a = gs_plugin_loader_app_create (app->plugin_loader, data_id);
                        gs_shell_reset_state (app->shell);
                        gs_shell_show_app (app->shell, a);
                        return;
@@ -667,9 +669,11 @@ install_activated (GSimpleAction *action,
        const gchar *id;
        GsShellInteraction interaction;
        g_autoptr (GsApp) a = NULL;
+       g_autofree gchar *data_id = NULL;
 
        g_variant_get (parameter, "(&su)", &id, &interaction);
-       if (!as_utils_data_id_valid (id)) {
+       data_id = gs_utils_unique_id_compat_convert (id);
+       if (data_id == NULL) {
                g_warning ("Need to use a valid unique-id: %s", id);
                return;
        }
@@ -677,9 +681,9 @@ install_activated (GSimpleAction *action,
        if (interaction == GS_SHELL_INTERACTION_FULL)
                gs_application_present_window (app, NULL);
 
-       a = gs_plugin_loader_app_create (app->plugin_loader, id);
+       a = gs_plugin_loader_app_create (app->plugin_loader, data_id);
        if (a == NULL) {
-               g_warning ("Could not create app from unique-id: %s", id);
+               g_warning ("Could not create app from data-id: %s", data_id);
                return;
        }
 


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