[gnome-software] trivial: Add functionality to truncate a GsAppList to a set size



commit bba56649413f97cd1d2d98b3e33843b97eb638b7
Author: Richard Hughes <richard hughsie com>
Date:   Wed Apr 19 19:58:46 2017 +0100

    trivial: Add functionality to truncate a GsAppList to a set size

 lib/gs-app-list-private.h |    2 ++
 lib/gs-app-list.c         |   40 ++++++++++++++++++++++++++++++++++++++++
 lib/gs-self-test.c        |   21 +++++++++++++++++++++
 3 files changed, 63 insertions(+), 0 deletions(-)
---
diff --git a/lib/gs-app-list-private.h b/lib/gs-app-list-private.h
index 96e0769..e032f13 100644
--- a/lib/gs-app-list-private.h
+++ b/lib/gs-app-list-private.h
@@ -57,6 +57,8 @@ void           gs_app_list_filter_duplicates  (GsAppList      *list,
                                                 GsAppListFilterFlags flags);
 void            gs_app_list_randomize          (GsAppList      *list);
 void            gs_app_list_remove_all         (GsAppList      *list);
+void            gs_app_list_truncate           (GsAppList      *list,
+                                                guint           length);
 
 G_END_DECLS
 
diff --git a/lib/gs-app-list.c b/lib/gs-app-list.c
index bf6c59f..97197e6 100644
--- a/lib/gs-app-list.c
+++ b/lib/gs-app-list.c
@@ -327,6 +327,46 @@ gs_app_list_sort (GsAppList *list, GsAppListSortFunc func, gpointer user_data)
        g_ptr_array_sort_with_data (list->array, gs_app_list_sort_cb, &helper);
 }
 
+/**
+ * gs_app_list_truncate:
+ * @list: A #GsAppList
+ * @length: the new length
+ *
+ * Truncates the application list. It is an error if @length is larger than the
+ * size of the list.
+ *
+ * Since: 3.24
+ **/
+void
+gs_app_list_truncate (GsAppList *list, guint length)
+{
+       g_autoptr(GMutexLocker) locker = NULL;
+
+       g_return_if_fail (GS_IS_APP_LIST (list));
+       g_return_if_fail (length <= list->array->len);
+
+       /* everything */
+       if (length == 0) {
+               gs_app_list_remove_all (list);
+               return;
+       }
+
+       /* remove the apps in the positions larger than the length */
+       locker = g_mutex_locker_new (&list->mutex);
+       for (guint i = length; i < list->array->len; i++) {
+               GsApp *app = g_ptr_array_index (list->array, i);
+               const gchar *unique_id;
+               unique_id = gs_app_get_unique_id (app);
+               if (unique_id != NULL) {
+                       GsApp *app_tmp = g_hash_table_lookup (list->hash_by_id, unique_id);
+                       if (app_tmp != NULL)
+                               g_hash_table_remove (list->hash_by_id, unique_id);
+               }
+
+       }
+       g_ptr_array_set_size (list->array, length);
+}
+
 static gint
 gs_app_list_randomize_cb (gconstpointer a, gconstpointer b, gpointer user_data)
 {
diff --git a/lib/gs-self-test.c b/lib/gs-self-test.c
index ea97983..61066d8 100644
--- a/lib/gs-self-test.c
+++ b/lib/gs-self-test.c
@@ -328,6 +328,27 @@ gs_plugin_func (void)
        g_object_unref (app);
        g_assert_cmpint (gs_app_list_length (list), ==, 0);
        g_object_unref (list);
+
+       /* truncate list */
+       list = gs_app_list_new ();
+       app = gs_app_new ("a");
+       gs_app_list_add (list, app);
+       g_object_unref (app);
+       app = gs_app_new ("b");
+       gs_app_list_add (list, app);
+       g_object_unref (app);
+       app = gs_app_new ("c");
+       gs_app_list_add (list, app);
+       g_object_unref (app);
+       gs_app_list_truncate (list, 3);
+       g_assert_cmpint (gs_app_list_length (list), ==, 3);
+       gs_app_list_truncate (list, 2);
+       g_assert_cmpint (gs_app_list_length (list), ==, 2);
+       gs_app_list_truncate (list, 1);
+       g_assert_cmpint (gs_app_list_length (list), ==, 1);
+       gs_app_list_truncate (list, 0);
+       g_assert_cmpint (gs_app_list_length (list), ==, 0);
+       g_object_unref (list);
 }
 
 static gpointer


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