[gnome-software] trivial: Allow deduplicating, prefering installed applications



commit fffe0afc96e1090310e8078865cb27284b7aa7e8
Author: Richard Hughes <richard hughsie com>
Date:   Fri Sep 28 11:00:14 2018 +0100

    trivial: Allow deduplicating, prefering installed applications

 lib/gs-app-list-private.h |  2 ++
 lib/gs-app-list.c         | 22 ++++++++++++++++++++--
 lib/gs-self-test.c        | 21 +++++++++++++++++++++
 3 files changed, 43 insertions(+), 2 deletions(-)
---
diff --git a/lib/gs-app-list-private.h b/lib/gs-app-list-private.h
index 3cd89761..30223f97 100644
--- a/lib/gs-app-list-private.h
+++ b/lib/gs-app-list-private.h
@@ -54,6 +54,7 @@ typedef enum {
  * @GS_APP_LIST_FILTER_FLAG_KEY_ID:            Filter by ID
  * @GS_APP_LIST_FILTER_FLAG_KEY_SOURCE:                Filter by default source
  * @GS_APP_LIST_FILTER_FLAG_KEY_VERSION:       Filter by version
+ * @GS_APP_LIST_FILTER_FLAG_PREFER_INSTALLED:  Prefer installed applications
  *
  * Flags to use when filtering. The priority of eash #GsApp is used to choose
  * which application object to keep.
@@ -63,6 +64,7 @@ typedef enum {
        GS_APP_LIST_FILTER_FLAG_KEY_ID          = 1 << 0,
        GS_APP_LIST_FILTER_FLAG_KEY_SOURCE      = 1 << 1,
        GS_APP_LIST_FILTER_FLAG_KEY_VERSION     = 1 << 2,
+       GS_APP_LIST_FILTER_FLAG_PREFER_INSTALLED= 1 << 3,
        /*< private >*/
        GS_APP_LIST_FILTER_FLAG_LAST
 } GsAppListFilterFlags;
diff --git a/lib/gs-app-list.c b/lib/gs-app-list.c
index b0acd70d..640e1301 100644
--- a/lib/gs-app-list.c
+++ b/lib/gs-app-list.c
@@ -723,6 +723,25 @@ gs_app_list_randomize (GsAppList *list)
        g_rand_free (rand);
 }
 
+static gboolean
+gs_app_list_filter_app_is_better (GsApp *app, GsApp *found, GsAppListFilterFlags flags)
+{
+       /* optional 1st layer sort */
+       if ((flags & GS_APP_LIST_FILTER_FLAG_PREFER_INSTALLED) > 0) {
+               if (gs_app_is_installed (app) && !gs_app_is_installed (found))
+                       return TRUE;
+               if (!gs_app_is_installed (app) && gs_app_is_installed (found))
+                       return FALSE;
+       }
+
+       /* 2nd layer, priority and bundle kind */
+       if (gs_app_compare_priority (app, found) < 0)
+               return TRUE;
+
+       /* assume is worse */
+       return FALSE;
+}
+
 /**
  * gs_app_list_filter_duplicates:
  * @list: A #GsAppList
@@ -793,8 +812,7 @@ gs_app_list_filter_duplicates (GsAppList *list, GsAppListFilterFlags flags)
 
                /* better? */
                if (flags != GS_APP_LIST_FILTER_FLAG_NONE) {
-                       if (gs_app_get_priority (app) >
-                           gs_app_get_priority (found)) {
+                       if (gs_app_list_filter_app_is_better (app, found, flags)) {
                                g_debug ("using better %s (priority %u > %u)",
                                         key->str,
                                         gs_app_get_priority (app),
diff --git a/lib/gs-self-test.c b/lib/gs-self-test.c
index 6bc65976..fb44124a 100644
--- a/lib/gs-self-test.c
+++ b/lib/gs-self-test.c
@@ -421,6 +421,27 @@ gs_plugin_func (void)
        g_assert_cmpstr (gs_app_get_unique_id (gs_app_list_index (list, 0)), ==, 
"user/foo/repo-security/*/*/*");
        g_object_unref (list);
 
+       /* prefer installed applications */
+       list = gs_app_list_new ();
+       app = gs_app_new ("e");
+       gs_app_set_state (app, AS_APP_STATE_INSTALLED);
+       gs_app_set_unique_id (app, "user/foo/*/*/e/*");
+       gs_app_set_priority (app, 0);
+       gs_app_list_add (list, app);
+       g_object_unref (app);
+       app = gs_app_new ("e");
+       gs_app_set_state (app, AS_APP_STATE_AVAILABLE);
+       gs_app_set_unique_id (app, "user/bar/*/*/e/*");
+       gs_app_set_priority (app, 100);
+       gs_app_list_add (list, app);
+       g_object_unref (app);
+       gs_app_list_filter_duplicates (list,
+                                      GS_APP_LIST_FILTER_FLAG_KEY_ID|
+                                      GS_APP_LIST_FILTER_FLAG_PREFER_INSTALLED);
+       g_assert_cmpint (gs_app_list_length (list), ==, 1);
+       g_assert_cmpstr (gs_app_get_unique_id (gs_app_list_index (list, 0)), ==, "user/foo/*/*/e/*");
+       g_object_unref (list);
+
        /* use globs when adding */
        list = gs_app_list_new ();
        app = gs_app_new ("b");


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