[gnome-software/wip/hughsie/unique_id: 1/5] Add gs_app_get_unique_id() and use it for de-duplication



commit 727bb73688686a9c2c495db67b0482d058a42d08
Author: Richard Hughes <richard hughsie com>
Date:   Thu Jul 28 16:30:06 2016 +0100

    Add gs_app_get_unique_id() and use it for de-duplication
    
    At the moment the unique-id falls back to the possibly-prefixed ID.

 src/gs-app-list.c |   12 ++++++------
 src/gs-app.c      |   39 +++++++++++++++++++++++++++++++++++++++
 src/gs-app.h      |    3 +++
 3 files changed, 48 insertions(+), 6 deletions(-)
---
diff --git a/src/gs-app-list.c b/src/gs-app-list.c
index da3d851..9a02bbf 100644
--- a/src/gs-app-list.c
+++ b/src/gs-app-list.c
@@ -67,7 +67,7 @@ gs_app_list_add (GsAppList *list, GsApp *app)
        g_return_if_fail (GS_IS_APP (app));
 
        /* if we're lazy-loading the ID then we can't filter for duplicates */
-       id = gs_app_get_id (app);
+       id = gs_app_get_unique_id (app);
        if (id == NULL) {
                g_ptr_array_add (list->array, g_object_ref (app));
                return;
@@ -266,7 +266,7 @@ gs_app_list_filter_duplicates (GsAppList *list, GsAppListFilterFlags flags)
                                      g_free, (GDestroyNotify) g_object_unref);
        for (i = 0; i < list->array->len; i++) {
                app = gs_app_list_index (list, i);
-               id = gs_app_get_id (app);
+               id = gs_app_get_unique_id (app);
                if (flags & GS_APP_LIST_FILTER_FLAG_PRIORITY)
                        id = gs_app_get_id_no_prefix (app);
                if (id == NULL) {
@@ -276,7 +276,7 @@ gs_app_list_filter_duplicates (GsAppList *list, GsAppListFilterFlags flags)
                }
                found = g_hash_table_lookup (hash, id);
                if (found == NULL) {
-                       g_debug ("found new %s", gs_app_get_id (app));
+                       g_debug ("found new %s", id);
                        g_hash_table_insert (hash,
                                             g_strdup (id),
                                             g_object_ref (app));
@@ -288,7 +288,7 @@ gs_app_list_filter_duplicates (GsAppList *list, GsAppListFilterFlags flags)
                        if (gs_app_get_priority (app) >
                            gs_app_get_priority (found)) {
                                g_debug ("using better %s (priority %u > %u)",
-                                        gs_app_get_id (app),
+                                        id,
                                         gs_app_get_priority (app),
                                         gs_app_get_priority (found));
                                g_hash_table_insert (hash,
@@ -297,12 +297,12 @@ gs_app_list_filter_duplicates (GsAppList *list, GsAppListFilterFlags flags)
                                continue;
                        }
                        g_debug ("ignoring worse duplicate %s (priority %u > %u)",
-                                gs_app_get_id (app),
+                                id,
                                 gs_app_get_priority (app),
                                 gs_app_get_priority (found));
                        continue;
                }
-               g_debug ("ignoring duplicate %s", gs_app_get_id (app));
+               g_debug ("ignoring duplicate %s", id);
                continue;
        }
 
diff --git a/src/gs-app.c b/src/gs-app.c
index 253b62b..11ebf0e 100644
--- a/src/gs-app.c
+++ b/src/gs-app.c
@@ -59,6 +59,7 @@ struct _GsApp
        GObject                  parent_instance;
 
        gchar                   *id;
+       gchar                   *unique_id;
        gchar                   *name;
        GsAppQuality             name_quality;
        GPtrArray               *icons;
@@ -254,6 +255,8 @@ gs_app_to_string (GsApp *app)
                gs_app_kv_printf (str, "progress", "%u%%", app->progress);
        if (app->id != NULL)
                gs_app_kv_lpad (str, "id", app->id);
+       if (app->unique_id != NULL)
+               gs_app_kv_lpad (str, "unique-id", app->unique_id);
        if ((app->kudos & GS_APP_KUDO_MY_LANGUAGE) > 0)
                gs_app_kv_lpad (str, "kudo", "my-language");
        if ((app->kudos & GS_APP_KUDO_RECENT_RELEASE) > 0)
@@ -829,6 +832,41 @@ gs_app_set_kind (GsApp *app, AsAppKind kind)
 }
 
 /**
+ * gs_app_get_unique_id:
+ * @app: a #GsApp
+ *
+ * Gets the unique application ID used for de-duplication.
+ * If nothing has been set the value from gs_app_get_id() will be used.
+ *
+ * Returns: The unique ID, e.g. "flatpak:gimp.desktop:3.22", or %NULL
+ **/
+const gchar *
+gs_app_get_unique_id (GsApp *app)
+{
+       g_return_val_if_fail (GS_IS_APP (app), NULL);
+       if (app->unique_id == NULL)
+               return app->id;
+       return app->unique_id;
+}
+
+/**
+ * gs_app_set_unique_id:
+ * @app: a #GsApp
+ * @unique_id: a unique application ID, e.g. "flatpak:gimp.desktop:3.22"
+ *
+ * Sets the unique application ID. Any #GsApp using the same ID will be
+ * deduplicated. This means that applications that can exist from more than
+ * one plugin should use this method.
+ */
+void
+gs_app_set_unique_id (GsApp *app, const gchar *unique_id)
+{
+       g_return_if_fail (GS_IS_APP (app));
+       g_free (app->unique_id);
+       app->unique_id = g_strdup (unique_id);
+}
+
+/**
  * gs_app_get_name:
  * @app: a #GsApp
  *
@@ -2844,6 +2882,7 @@ gs_app_finalize (GObject *object)
        GsApp *app = GS_APP (object);
 
        g_free (app->id);
+       g_free (app->unique_id);
        g_free (app->name);
        g_hash_table_unref (app->urls);
        g_free (app->license);
diff --git a/src/gs-app.h b/src/gs-app.h
index d7dacc6..c65e570 100644
--- a/src/gs-app.h
+++ b/src/gs-app.h
@@ -114,6 +114,9 @@ void                 gs_app_set_state_recover       (GsApp          *app);
 guint           gs_app_get_progress            (GsApp          *app);
 void            gs_app_set_progress            (GsApp          *app,
                                                 guint           percentage);
+const gchar    *gs_app_get_unique_id           (GsApp          *app);
+void            gs_app_set_unique_id           (GsApp          *app,
+                                                const gchar    *unique_id);
 const gchar    *gs_app_get_name                (GsApp          *app);
 void            gs_app_set_name                (GsApp          *app,
                                                 GsAppQuality    quality,


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