[gnome-software] Use a per-list sort key when randomizing a list



commit eba14fb319c3f801c8a83044d959a641fda6ce32
Author: Richard Hughes <richard hughsie com>
Date:   Tue Feb 25 14:45:54 2014 +0000

    Use a per-list sort key when randomizing a list
    
    If we use the same sort key, applications in multiple lists (e.g. popular and
    installed) will be sorted incorrectly. Also, as we're now adding more metadata
    that's only going to be used just once, remove the sort metadata after the list
    has been ordered.

 src/gs-app.c    |    6 ++++++
 src/gs-plugin.c |   22 ++++++++++++++++------
 2 files changed, 22 insertions(+), 6 deletions(-)
---
diff --git a/src/gs-app.c b/src/gs-app.c
index d7411ff..609fda1 100644
--- a/src/gs-app.c
+++ b/src/gs-app.c
@@ -1389,6 +1389,12 @@ gs_app_set_metadata (GsApp *app, const gchar *key, const gchar *value)
 
        g_return_if_fail (GS_IS_APP (app));
 
+       /* if no value, then remove the key */
+       if (value == NULL) {
+               g_hash_table_remove (app->priv->metadata, key);
+               return;
+       }
+
        /* check we're not overwriting */
        found = g_hash_table_lookup (app->priv->metadata, key);
        if (found != NULL) {
diff --git a/src/gs-plugin.c b/src/gs-plugin.c
index b602313..53ad8c6 100644
--- a/src/gs-plugin.c
+++ b/src/gs-plugin.c
@@ -142,12 +142,16 @@ gs_plugin_list_filter (GList **list, GsPluginListFilter func, gpointer user_data
  * gs_plugin_list_randomize_cb:
  */
 static gint
-gs_plugin_list_randomize_cb (gconstpointer a, gconstpointer b)
+gs_plugin_list_randomize_cb (gconstpointer a, gconstpointer b, gpointer user_data)
 {
        const gchar *k1;
        const gchar *k2;
-       k1 = gs_app_get_metadata_item (GS_APP (a), "Plugin::sort-key");
-       k2 = gs_app_get_metadata_item (GS_APP (b), "Plugin::sort-key");
+       gchar *key;
+
+       key = g_strdup_printf ("Plugin::sort-key[%p]", user_data);
+       k1 = gs_app_get_metadata_item (GS_APP (a), key);
+       k2 = gs_app_get_metadata_item (GS_APP (b), key);
+       g_free (key);
        return g_strcmp0 (k1, k2);
 }
 
@@ -163,8 +167,10 @@ gs_plugin_list_randomize (GList **list)
        GList *l;
        GRand *rand;
        GsApp *app;
+       gchar *key;
        gchar sort_key[] = { '\0', '\0', '\0', '\0' };
 
+       key = g_strdup_printf ("Plugin::sort-key[%p]", list);
        rand = g_rand_new ();
        date = g_date_time_new_now_utc ();
        g_rand_set_seed (rand, g_date_time_get_day_of_year (date));
@@ -173,10 +179,14 @@ gs_plugin_list_randomize (GList **list)
                sort_key[0] = g_rand_int_range (rand, (gint32) 'A', (gint32) 'Z');
                sort_key[1] = g_rand_int_range (rand, (gint32) 'A', (gint32) 'Z');
                sort_key[2] = g_rand_int_range (rand, (gint32) 'A', (gint32) 'Z');
-               gs_app_set_metadata (app, "Plugin::sort-key", sort_key);
+               gs_app_set_metadata (app, key, sort_key);
        }
-       *list = g_list_sort (*list, gs_plugin_list_randomize_cb);
-
+       *list = g_list_sort_with_data (*list, gs_plugin_list_randomize_cb, list);
+       for (l = *list; l != NULL; l = l->next) {
+               app = GS_APP (l->data);
+               gs_app_set_metadata (app, key, NULL);
+       }
+       g_free (key);
        g_rand_free (rand);
        g_date_time_unref (date);
 }


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