[gnome-software] Add a 'General' subcategory to return all the results without filtering



commit 23050aba52f8ccd4c13ea47b00712c65a96ee18b
Author: Richard Hughes <richard hughsie com>
Date:   Mon Sep 2 13:56:51 2013 +0100

    Add a 'General' subcategory to return all the results without filtering

 src/gs-category.c                 |   44 +++++++++++++++++++++++++++++++++++++
 src/gs-category.h                 |    1 +
 src/gs-plugin-loader.c            |    3 ++
 src/plugins/gs-plugin-appstream.c |    6 +++++
 4 files changed, 54 insertions(+), 0 deletions(-)
---
diff --git a/src/gs-category.c b/src/gs-category.c
index 18a4270..88cf446 100644
--- a/src/gs-category.c
+++ b/src/gs-category.c
@@ -21,6 +21,8 @@
 
 #include "config.h"
 
+#include <glib/gi18n.h>
+
 #include "gs-category.h"
 
 static void    gs_category_finalize    (GObject        *object);
@@ -80,6 +82,48 @@ gs_category_add_subcategory (GsCategory *category, GsCategory *subcategory)
         category->priv->subcategories = g_list_prepend (category->priv->subcategories, g_object_ref 
(subcategory));
 }
 
+/**
+ * gs_category_sort_subcategories_cb:
+ **/
+static gint
+gs_category_sort_subcategories_cb (gconstpointer a, gconstpointer b)
+{
+       return g_strcmp0 (gs_category_get_name (GS_CATEGORY (a)),
+                         gs_category_get_name (GS_CATEGORY (b)));
+}
+
+/**
+ * gs_category_sort_subcategories:
+ **/
+void
+gs_category_sort_subcategories (GsCategory *category)
+{
+       gboolean subcat_all = FALSE;
+       GList *l;
+       GsCategory *all;
+       GsCategoryPrivate *priv = category->priv;
+
+       /* nothing here */
+       if (priv->subcategories == NULL)
+               return;
+
+       /* ensure there is a general entry */
+       for (l = priv->subcategories; l != NULL; l = l->next) {
+               if (gs_category_get_id (GS_CATEGORY (l->data)) == NULL) {
+                       subcat_all = TRUE;
+                       break;
+               }
+       }
+       if (!subcat_all) {
+               all = gs_category_new (category, NULL, _("General"));
+               gs_category_add_subcategory (category, all);
+       }
+
+       /* actually sort the data */
+       priv->subcategories = g_list_sort (priv->subcategories,
+                                          gs_category_sort_subcategories_cb);
+}
+
 static void
 gs_category_class_init (GsCategoryClass *klass)
 {
diff --git a/src/gs-category.h b/src/gs-category.h
index 55cc047..f349c81 100644
--- a/src/gs-category.h
+++ b/src/gs-category.h
@@ -56,6 +56,7 @@ GsCategory      *gs_category_get_parent         (GsCategory             *categor
 const gchar    *gs_category_get_name           (GsCategory             *category);
 void            gs_category_set_name           (GsCategory             *category,
                                                 const gchar            *name);
+void            gs_category_sort_subcategories (GsCategory             *category);
 GList           *gs_category_get_subcategories  (GsCategory             *category);
 void             gs_category_add_subcategory    (GsCategory             *category,
                                                  GsCategory             *subcategory);
diff --git a/src/gs-plugin-loader.c b/src/gs-plugin-loader.c
index d7e2b4d..49a441f 100644
--- a/src/gs-plugin-loader.c
+++ b/src/gs-plugin-loader.c
@@ -1026,6 +1026,7 @@ cd_plugin_loader_get_categories_thread_cb (GSimpleAsyncResult *res,
        GsPluginLoader *plugin_loader = GS_PLUGIN_LOADER (object);
        GsPlugin *plugin;
        GsPluginResultsFunc plugin_func = NULL;
+       GList *l;
        guint i;
 
        /* run each plugin */
@@ -1063,6 +1064,8 @@ cd_plugin_loader_get_categories_thread_cb (GSimpleAsyncResult *res,
 
        /* sort by name */
        state->list = g_list_sort (state->list, gs_plugin_loader_category_sort_cb);
+       for (l = state->list; l != NULL; l = l->next)
+               gs_category_sort_subcategories (GS_CATEGORY (l->data));
 
        /* success */
        if (state->list == NULL) {
diff --git a/src/plugins/gs-plugin-appstream.c b/src/plugins/gs-plugin-appstream.c
index 93be7e4..d0179f7 100644
--- a/src/plugins/gs-plugin-appstream.c
+++ b/src/plugins/gs-plugin-appstream.c
@@ -743,6 +743,12 @@ gs_plugin_add_category_apps (GsPlugin *plugin,
        if (parent != NULL)
                search_id2 = gs_category_get_id (parent);
 
+       /* the "General" item has no ID */
+       if (search_id1 == NULL) {
+               search_id1 = search_id2;
+               search_id2 = NULL;
+       }
+
        /* just look at each app in turn */
        for (i = 0; i < plugin->priv->array->len; i++) {
                item = g_ptr_array_index (plugin->priv->array, i);


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