[gnome-software/wip/rancell/reviews-popular] ubuntu-reviews: Set popular apps from ratings
- From: Robert Ancell <rancell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-software/wip/rancell/reviews-popular] ubuntu-reviews: Set popular apps from ratings
- Date: Sun, 23 Jul 2017 23:50:24 +0000 (UTC)
commit ec5ff667e5459d0fcca01c45bf5d525ec9c7dfd2
Author: Robert Ancell <robert ancell canonical com>
Date: Fri Apr 1 16:37:45 2016 +1300
ubuntu-reviews: Set popular apps from ratings
plugins/ubuntu-reviews/gs-plugin-ubuntu-reviews.c | 92 +++++++++++++++++++++
1 files changed, 92 insertions(+), 0 deletions(-)
---
diff --git a/plugins/ubuntu-reviews/gs-plugin-ubuntu-reviews.c
b/plugins/ubuntu-reviews/gs-plugin-ubuntu-reviews.c
index 1851248..b815712 100644
--- a/plugins/ubuntu-reviews/gs-plugin-ubuntu-reviews.c
+++ b/plugins/ubuntu-reviews/gs-plugin-ubuntu-reviews.c
@@ -986,3 +986,95 @@ gs_plugin_review_remove (GsPlugin *plugin,
return TRUE;
}
+
+typedef struct {
+ gchar *package_name;
+ gint rating;
+} PopularEntry;
+
+static gint
+popular_sqlite_cb (void *data,
+ gint argc,
+ gchar **argv,
+ gchar **col_name)
+{
+ GList **list = data;
+ PopularEntry *entry;
+
+ entry = g_slice_new (PopularEntry);
+ entry->package_name = g_strdup (argv[0]);
+ entry->rating = gs_utils_get_wilson_rating (g_ascii_strtoll (argv[1], NULL, 10),
+ g_ascii_strtoll (argv[2], NULL, 10),
+ g_ascii_strtoll (argv[3], NULL, 10),
+ g_ascii_strtoll (argv[4], NULL, 10),
+ g_ascii_strtoll (argv[5], NULL, 10));
+ *list = g_list_prepend (*list, entry);
+
+ return 0;
+}
+
+static gint
+compare_popular_entry (gconstpointer a, gconstpointer b)
+{
+ PopularEntry *ea = a, *eb = b;
+ return eb->rating - ea->rating;
+}
+
+static void
+free_popular_entry (gpointer data)
+{
+ PopularEntry *entry = data;
+ g_free (entry->package_name);
+ g_slice_free (PopularEntry, entry);
+}
+
+gboolean
+gs_plugin_add_popular (GsPlugin *plugin,
+ GsAppList *list,
+ GCancellable *cancellable,
+ GError **error)
+{
+ GsPluginData *priv = gs_plugin_get_data (plugin);
+ gint result;
+ GList *entries = NULL, *link;
+ char *error_msg = NULL;
+
+ /* Load database once */
+ if (g_once_init_enter (&priv->db_loaded)) {
+ gboolean ret = load_database (plugin, cancellable, error);
+ g_once_init_leave (&priv->db_loaded, TRUE);
+ if (!ret)
+ return FALSE;
+ }
+
+ result = sqlite3_exec (priv->db,
+ "SELECT package_name, one_star_count, two_star_count, three_star_count,
four_star_count, five_star_count FROM review_stats",
+ popular_sqlite_cb,
+ &entries,
+ &error_msg);
+ if (result != SQLITE_OK) {
+ g_set_error (error,
+ GS_PLUGIN_ERROR,
+ GS_PLUGIN_ERROR_FAILED,
+ "SQL error: %s", error_msg);
+ sqlite3_free (error_msg);
+ return FALSE;
+ }
+
+ entries = g_list_sort (entries, compare_popular_entry);
+ for (link = entries; link; link = link->next) {
+ PopularEntry *entry = link->data;
+ g_autoptr(GsApp) app = NULL;
+
+ /* Need four stars to show */
+ if (entry->rating < 80)
+ break;
+
+ app = gs_app_new (entry->package_name);
+ gs_app_add_source (app, entry->package_name);
+ gs_app_list_add (list, app);
+ }
+ g_list_free_full (entries, free_popular_entry);
+
+ return TRUE;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]