[gnome-software] Add gs_plugin_list_randomize() for plugins to use



commit 866314f7dd20ba6d23af82a939bc4a31491aee7e
Author: Richard Hughes <richard hughsie com>
Date:   Fri Jan 31 15:06:16 2014 +0000

    Add gs_plugin_list_randomize() for plugins to use
    
    The order of the list only changes once per day as it uses the day of the year
    as the random seed.

 src/gs-plugin.c |   45 ++++++++++++++++++++++++++++++++++++++++++++-
 src/gs-plugin.h |    1 +
 2 files changed, 45 insertions(+), 1 deletions(-)
---
diff --git a/src/gs-plugin.c b/src/gs-plugin.c
index 28fa778..adcf67a 100644
--- a/src/gs-plugin.c
+++ b/src/gs-plugin.c
@@ -105,7 +105,50 @@ gs_plugin_list_filter (GList **list, GsPluginListFilter func, gpointer user_data
 }
 
 /**
- * gs_plugin_list_copy:
+ * gs_plugin_list_randomize_cb:
+ */
+static gint
+gs_plugin_list_randomize_cb (gconstpointer a, gconstpointer b)
+{
+       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");
+       return g_strcmp0 (k1, k2);
+}
+
+/**
+ * gs_plugin_list_randomize:
+ *
+ * Randomize the order of the list, but don't change the order until the next day
+ **/
+void
+gs_plugin_list_randomize (GList **list)
+{
+       GDateTime *date;
+       GList *l;
+       GRand *rand;
+       GsApp *app;
+       gchar sort_key[] = { '\0', '\0', '\0', '\0' };
+
+       rand = g_rand_new ();
+       date = g_date_time_new_now_utc ();
+       g_rand_set_seed (rand, g_date_time_get_day_of_year (date));
+       for (l = *list; l != NULL; l = l->next) {
+               app = GS_APP (l->data);
+               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);
+       }
+       *list = g_list_sort (*list, gs_plugin_list_randomize_cb);
+
+       g_rand_free (rand);
+       g_date_time_unref (date);
+}
+
+/**
+ * gs_plugin_list_filter_duplicates:
  **/
 void
 gs_plugin_list_filter_duplicates (GList **list)
diff --git a/src/gs-plugin.h b/src/gs-plugin.h
index a698738..ae28de3 100644
--- a/src/gs-plugin.h
+++ b/src/gs-plugin.h
@@ -156,6 +156,7 @@ void                 gs_plugin_list_filter                  (GList          **list,
                                                         GsPluginListFilter func,
                                                         gpointer        user_data);
 void            gs_plugin_list_filter_duplicates       (GList          **list);
+void            gs_plugin_list_randomize               (GList          **list);
 
 void            gs_plugin_status_update                (GsPlugin       *plugin,
                                                         GsApp          *app,


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