[gnome-software] Make gs_app_list_filter_duplicates not change app order.
- From: Robert Ancell <rancell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-software] Make gs_app_list_filter_duplicates not change app order.
- Date: Fri, 18 Aug 2017 04:21:13 +0000 (UTC)
commit a0d8f2f6c9de5f145f845c6fd2723d9d6d3450ef
Author: Robert Ancell <robert ancell canonical com>
Date: Fri Aug 18 16:15:51 2017 +1200
Make gs_app_list_filter_duplicates not change app order.
Previously filter_duplicates would put non-duplicate apps back in an
order determinted by their storage in a GHashTable. This change
refactors the function so the order is retained.
lib/gs-app-list.c | 38 +++++++++++++++++++-------------------
1 files changed, 19 insertions(+), 19 deletions(-)
---
diff --git a/lib/gs-app-list.c b/lib/gs-app-list.c
index 3b595df..49f682f 100644
--- a/lib/gs-app-list.c
+++ b/lib/gs-app-list.c
@@ -469,6 +469,7 @@ gs_app_list_randomize (GsAppList *list)
/**
* gs_app_list_filter_duplicates:
* @list: A #GsAppList
+ * @flags: a #GsAppListFilterFlags, e.g. GS_APP_LIST_FILTER_KEY_ID
*
* Filter any duplicate applications from the list.
*
@@ -478,17 +479,16 @@ void
gs_app_list_filter_duplicates (GsAppList *list, GsAppListFilterFlags flags)
{
g_autoptr(GHashTable) hash = NULL;
- g_autoptr(GList) values = NULL;
- g_autoptr(GPtrArray) apps_no_id = NULL;
+ g_autoptr(GHashTable) kept_apps = NULL;
+ g_autoptr(GsAppList) old = NULL;
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&list->mutex);
g_return_if_fail (GS_IS_APP_LIST (list));
/* a hash table to hold apps with unique app ids */
- hash = g_hash_table_new_full (g_str_hash, g_str_equal,
- g_free, (GDestroyNotify) g_object_unref);
- /* an array to hold apps that have NULL app ids */
- apps_no_id = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
+ hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+ /* a hash table containing apps we want to keep */
+ kept_apps = g_hash_table_new (g_direct_hash, g_direct_equal);
for (guint i = 0; i < list->array->len; i++) {
GsApp *app;
@@ -519,7 +519,7 @@ gs_app_list_filter_duplicates (GsAppList *list, GsAppListFilterFlags flags)
if (key->len == 0) {
g_autofree gchar *str = gs_app_to_string (app);
g_debug ("adding without deduplication as no app key: %s", str);
- g_ptr_array_add (apps_no_id, g_object_ref (app));
+ g_hash_table_add (kept_apps, app);
continue;
}
found = g_hash_table_lookup (hash, key->str);
@@ -527,7 +527,8 @@ gs_app_list_filter_duplicates (GsAppList *list, GsAppListFilterFlags flags)
g_debug ("found new %s", key->str);
g_hash_table_insert (hash,
g_strdup (key->str),
- g_object_ref (app));
+ app);
+ g_hash_table_add (kept_apps, app);
continue;
}
@@ -541,7 +542,9 @@ gs_app_list_filter_duplicates (GsAppList *list, GsAppListFilterFlags flags)
gs_app_get_priority (found));
g_hash_table_insert (hash,
g_strdup (key->str),
- g_object_ref (app));
+ app);
+ g_hash_table_remove (kept_apps, found);
+ g_hash_table_add (kept_apps, app);
continue;
}
g_debug ("ignoring worse duplicate %s (priority %u > %u)",
@@ -554,18 +557,15 @@ gs_app_list_filter_duplicates (GsAppList *list, GsAppListFilterFlags flags)
continue;
}
- /* add back the best results to the existing list */
+ /* deep copy to a temp list and clear the current one */
+ old = gs_app_list_copy (list);
gs_app_list_remove_all_safe (list);
- values = g_hash_table_get_values (hash);
- for (GList *l = values; l != NULL; l = l->next) {
- GsApp *app = GS_APP (l->data);
- gs_app_list_add_safe (list, app);
- }
- /* add back apps with NULL app ids to the existing list */
- for (guint i = 0; i < apps_no_id->len; i++) {
- GsApp *app = g_ptr_array_index (apps_no_id, i);
- gs_app_list_add_safe (list, app);
+ /* add back the apps we want to keep */
+ for (guint i = 0; i < old->array->len; i++) {
+ GsApp *app = gs_app_list_index (old, i);
+ if (g_hash_table_contains (kept_apps, app))
+ gs_app_list_add_safe (list, app);
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]