[gnome-software/gnome-3-22] trivial: Move out some shared functionality for searching by category



commit 353fefdab21b88742ad6f789fd831897b9afc625
Author: Richard Hughes <richard hughsie com>
Date:   Tue Sep 13 17:58:05 2016 +0100

    trivial: Move out some shared functionality for searching by category
    
    (cherry picked from commit 0e8090b49bdde9ac0b266ef359ccdb21e77ea5ff)

 src/plugins/gs-appstream.c        |  133 +++++++++++++++++++++++++++++++++++++
 src/plugins/gs-appstream.h        |   11 +++
 src/plugins/gs-plugin-appstream.c |  121 ++-------------------------------
 3 files changed, 152 insertions(+), 113 deletions(-)
---
diff --git a/src/plugins/gs-appstream.c b/src/plugins/gs-appstream.c
index e9984d9..cfdd490 100644
--- a/src/plugins/gs-appstream.c
+++ b/src/plugins/gs-appstream.c
@@ -796,3 +796,136 @@ gs_appstream_store_search (GsPlugin *plugin,
        }
        return TRUE;
 }
+
+static gboolean
+_as_app_matches_desktop_group_set (AsApp *app, gchar **desktop_groups)
+{
+       guint i;
+       for (i = 0; desktop_groups[i] != NULL; i++) {
+               if (!as_app_has_category (app, desktop_groups[i]))
+                       return FALSE;
+       }
+       return TRUE;
+}
+
+static gboolean
+_as_app_matches_desktop_group (AsApp *app, const gchar *desktop_group)
+{
+       g_auto(GStrv) split = g_strsplit (desktop_group, "::", -1);
+       return _as_app_matches_desktop_group_set (app, split);
+}
+
+static void
+gs_appstream_store_add_categories_for_app (GsCategory *parent, AsApp *app)
+{
+       GPtrArray *children;
+       GPtrArray *desktop_groups;
+       GsCategory *category;
+       guint i, j;
+
+       /* find all the sub-categories */
+       children = gs_category_get_children (parent);
+       for (j = 0; j < children->len; j++) {
+               gboolean matched = FALSE;
+               category = GS_CATEGORY (g_ptr_array_index (children, j));
+
+               /* do any desktop_groups match this application */
+               desktop_groups = gs_category_get_desktop_groups (category);
+               for (i = 0; i < desktop_groups->len; i++) {
+                       const gchar *desktop_group = g_ptr_array_index (desktop_groups, i);
+                       if (_as_app_matches_desktop_group (app, desktop_group)) {
+                               matched = TRUE;
+                               break;
+                       }
+               }
+               if (matched) {
+                       gs_category_increment_size (category);
+                       gs_category_increment_size (parent);
+               }
+       }
+}
+
+gboolean
+gs_appstream_store_add_category_apps (GsPlugin *plugin,
+                                     AsStore *store,
+                                     GsCategory *category,
+                                     GsAppList *list,
+                                     GCancellable *cancellable,
+                                     GError **error)
+{
+       GsPluginData *priv = gs_plugin_get_data (plugin);
+       GPtrArray *array;
+       GPtrArray *desktop_groups;
+       guint i;
+       guint j;
+       g_autoptr(AsProfileTask) ptask = NULL;
+
+       /* just look at each app in turn */
+       ptask = as_profile_start_literal (gs_plugin_get_profile (plugin),
+                                         "appstream::add-category-apps");
+       g_assert (ptask != NULL);
+       array = as_store_get_apps (store);
+       desktop_groups = gs_category_get_desktop_groups (category);
+       if (desktop_groups->len == 0) {
+               g_warning ("no desktop_groups for %s", gs_category_get_id (category));
+               return TRUE;
+       }
+       for (j = 0; j < desktop_groups->len; j++) {
+               const gchar *desktop_group = g_ptr_array_index (desktop_groups, j);
+               g_auto(GStrv) split = g_strsplit (desktop_group, "::", -1);
+
+               /* match the app */
+               for (i = 0; i < array->len; i++) {
+                       AsApp *item;
+                       g_autoptr(GsApp) app = NULL;
+
+                       /* no ID is invalid */
+                       item = g_ptr_array_index (array, i);
+                       if (as_app_get_id (item) == NULL)
+                               continue;
+
+                       /* match all the desktop groups */
+                       if (!_as_app_matches_desktop_group_set (item, split))
+                               continue;
+
+                       /* add all the data we can */
+                       app = gs_appstream_create_app (plugin, item);
+                       if (!gs_appstream_refine_app (plugin, app, item, error))
+                               return FALSE;
+                       gs_app_list_add (list, app);
+               }
+       }
+       return TRUE;
+}
+
+gboolean
+gs_appstream_store_add_categories (GsPlugin *plugin,
+                                  AsStore *store,
+                                  GPtrArray *list,
+                                  GCancellable *cancellable,
+                                  GError **error)
+{
+       AsApp *app;
+       GPtrArray *array;
+       guint i;
+       guint j;
+       g_autoptr(AsProfileTask) ptask = NULL;
+
+       /* find out how many packages are in each category */
+       ptask = as_profile_start_literal (gs_plugin_get_profile (plugin),
+                                         "appstream::add-categories");
+       g_assert (ptask != NULL);
+       array = as_store_get_apps (store);
+       for (i = 0; i < array->len; i++) {
+               app = g_ptr_array_index (array, i);
+               if (as_app_get_id (app) == NULL)
+                       continue;
+               if (as_app_get_priority (app) < 0)
+                       continue;
+               for (j = 0; j < list->len; j++) {
+                       GsCategory *parent = GS_CATEGORY (g_ptr_array_index (list, j));
+                       gs_appstream_store_add_categories_for_app (parent, app);
+               }
+       }
+       return TRUE;
+}
diff --git a/src/plugins/gs-appstream.h b/src/plugins/gs-appstream.h
index 7a7440d..0a19fcf 100644
--- a/src/plugins/gs-appstream.h
+++ b/src/plugins/gs-appstream.h
@@ -41,6 +41,17 @@ gboolean      gs_appstream_store_search              (GsPlugin       *plugin,
                                                         GsAppList      *list,
                                                         GCancellable   *cancellable,
                                                         GError         **error);
+gboolean        gs_appstream_store_add_categories      (GsPlugin       *plugin,
+                                                        AsStore        *store,
+                                                        GPtrArray      *list,
+                                                        GCancellable   *cancellable,
+                                                        GError         **error);
+gboolean        gs_appstream_store_add_category_apps   (GsPlugin       *plugin,
+                                                        AsStore        *store,
+                                                        GsCategory     *category,
+                                                        GsAppList      *list,
+                                                        GCancellable   *cancellable,
+                                                        GError         **error);
 
 G_END_DECLS
 
diff --git a/src/plugins/gs-plugin-appstream.c b/src/plugins/gs-plugin-appstream.c
index c947cce..dc5d199 100644
--- a/src/plugins/gs-plugin-appstream.c
+++ b/src/plugins/gs-plugin-appstream.c
@@ -522,24 +522,6 @@ gs_plugin_refine (GsPlugin *plugin,
        return TRUE;
 }
 
-static gboolean
-_as_app_matches_desktop_group_set (AsApp *app, gchar **desktop_groups)
-{
-       guint i;
-       for (i = 0; desktop_groups[i] != NULL; i++) {
-               if (!as_app_has_category (app, desktop_groups[i]))
-                       return FALSE;
-       }
-       return TRUE;
-}
-
-static gboolean
-_as_app_matches_desktop_group (AsApp *app, const gchar *desktop_group)
-{
-       g_auto(GStrv) split = g_strsplit (desktop_group, "::", -1);
-       return _as_app_matches_desktop_group_set (app, split);
-}
-
 gboolean
 gs_plugin_add_category_apps (GsPlugin *plugin,
                             GsCategory *category,
@@ -548,48 +530,12 @@ gs_plugin_add_category_apps (GsPlugin *plugin,
                             GError **error)
 {
        GsPluginData *priv = gs_plugin_get_data (plugin);
-       GPtrArray *array;
-       GPtrArray *desktop_groups;
-       guint i;
-       guint j;
-       g_autoptr(AsProfileTask) ptask = NULL;
-
-       /* just look at each app in turn */
-       ptask = as_profile_start_literal (gs_plugin_get_profile (plugin),
-                                         "appstream::add-category-apps");
-       g_assert (ptask != NULL);
-       array = as_store_get_apps (priv->store);
-       desktop_groups = gs_category_get_desktop_groups (category);
-       if (desktop_groups->len == 0) {
-               g_warning ("no desktop_groups for %s", gs_category_get_id (category));
-               return TRUE;
-       }
-       for (j = 0; j < desktop_groups->len; j++) {
-               const gchar *desktop_group = g_ptr_array_index (desktop_groups, j);
-               g_auto(GStrv) split = g_strsplit (desktop_group, "::", -1);
-
-               /* match the app */
-               for (i = 0; i < array->len; i++) {
-                       AsApp *item;
-                       g_autoptr(GsApp) app = NULL;
-
-                       /* no ID is invalid */
-                       item = g_ptr_array_index (array, i);
-                       if (as_app_get_id (item) == NULL)
-                               continue;
-
-                       /* match all the desktop groups */
-                       if (!_as_app_matches_desktop_group_set (item, split))
-                               continue;
-
-                       /* add all the data we can */
-                       app = gs_appstream_create_app (plugin, item);
-                       if (!gs_appstream_refine_app (plugin, app, item, error))
-                               return FALSE;
-                       gs_app_list_add (list, app);
-               }
-       }
-       return TRUE;
+       return gs_appstream_store_add_category_apps (plugin,
+                                                    priv->store,
+                                                    category,
+                                                    list,
+                                                    cancellable,
+                                                    error);
 }
 
 gboolean
@@ -638,36 +584,6 @@ gs_plugin_add_installed (GsPlugin *plugin,
        return TRUE;
 }
 
-static void
-gs_plugin_add_categories_for_app (GsCategory *parent, AsApp *app)
-{
-       GPtrArray *children;
-       GPtrArray *desktop_groups;
-       GsCategory *category;
-       guint i, j;
-
-       /* find all the sub-categories */
-       children = gs_category_get_children (parent);
-       for (j = 0; j < children->len; j++) {
-               gboolean matched = FALSE;
-               category = GS_CATEGORY (g_ptr_array_index (children, j));
-
-               /* do any desktop_groups match this application */
-               desktop_groups = gs_category_get_desktop_groups (category);
-               for (i = 0; i < desktop_groups->len; i++) {
-                       const gchar *desktop_group = g_ptr_array_index (desktop_groups, i);
-                       if (_as_app_matches_desktop_group (app, desktop_group)) {
-                               matched = TRUE;
-                               break;
-                       }
-               }
-               if (matched) {
-                       gs_category_increment_size (category);
-                       gs_category_increment_size (parent);
-               }
-       }
-}
-
 gboolean
 gs_plugin_add_categories (GsPlugin *plugin,
                          GPtrArray *list,
@@ -675,29 +591,8 @@ gs_plugin_add_categories (GsPlugin *plugin,
                          GError **error)
 {
        GsPluginData *priv = gs_plugin_get_data (plugin);
-       AsApp *app;
-       GPtrArray *array;
-       guint i;
-       guint j;
-       g_autoptr(AsProfileTask) ptask = NULL;
-
-       /* find out how many packages are in each category */
-       ptask = as_profile_start_literal (gs_plugin_get_profile (plugin),
-                                         "appstream::add-categories");
-       g_assert (ptask != NULL);
-       array = as_store_get_apps (priv->store);
-       for (i = 0; i < array->len; i++) {
-               app = g_ptr_array_index (array, i);
-               if (as_app_get_id (app) == NULL)
-                       continue;
-               if (as_app_get_priority (app) < 0)
-                       continue;
-               for (j = 0; j < list->len; j++) {
-                       GsCategory *parent = GS_CATEGORY (g_ptr_array_index (list, j));
-                       gs_plugin_add_categories_for_app (parent, app);
-               }
-       }
-       return TRUE;
+       return gs_appstream_store_add_categories (plugin, priv->store, list,
+                                                 cancellable, error);
 }
 
 gboolean


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