[gnome-software: 15/25] gs-category: Add gs_category_new_for_desktop_data()




commit 7dd1ba2515e8f60bdafde28cb91f93147f36e569
Author: Philip Withnall <pwithnall endlessos org>
Date:   Tue Feb 2 00:16:24 2021 +0000

    gs-category: Add gs_category_new_for_desktop_data()
    
    This moves construction of a `GsCategory` for `GsDesktopData` from
    outside `gs-category.c` to inside, which should allow for easier
    optimisation of it in subsequent commits.
    
    It introduces no functional changes.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 lib/gs-category-manager.c | 24 +-----------------------
 lib/gs-category.c         | 42 ++++++++++++++++++++++++++++++++++++++++++
 lib/gs-category.h         |  4 ++++
 3 files changed, 47 insertions(+), 23 deletions(-)
---
diff --git a/lib/gs-category-manager.c b/lib/gs-category-manager.c
index fd42857de..adf7395ab 100644
--- a/lib/gs-category-manager.c
+++ b/lib/gs-category-manager.c
@@ -70,30 +70,8 @@ gs_category_manager_init (GsCategoryManager *self)
         * of gs_desktop_get_data() match reality. */
        msdata = gs_desktop_get_data ();
        for (gsize i = 0; msdata[i].id != NULL; i++) {
-               g_autoptr(GsCategory) category = NULL;
-               g_autofree gchar *msgctxt = NULL;
-
-               /* add parent category */
-               category = gs_category_new (msdata[i].id);
-               gs_category_set_icon (category, msdata[i].icon);
-               gs_category_set_name (category, gettext (msdata[i].name));
-               gs_category_set_score (category, msdata[i].score);
-               msgctxt = g_strdup_printf ("Menu of %s", msdata[i].name);
-
-               /* add subcategories */
-               for (gsize j = 0; msdata[i].mapping[j].id != NULL; j++) {
-                       const GsDesktopMap *map = &msdata[i].mapping[j];
-                       g_autoptr(GsCategory) sub = gs_category_new (map->id);
-                       for (gsize k = 0; map->fdo_cats[k] != NULL; k++)
-                               gs_category_add_desktop_group (sub, map->fdo_cats[k]);
-                       gs_category_set_name (sub, g_dpgettext2 (GETTEXT_PACKAGE,
-                                                                msgctxt,
-                                                                map->name));
-                       gs_category_add_child (category, sub);
-               }
-
                g_assert (i < G_N_ELEMENTS (self->categories) - 1);
-               self->categories[i] = g_steal_pointer (&category));
+               self->categories[i] = gs_category_new_for_desktop_data (&msdata[i]);
        }
 
        g_assert (self->categories[G_N_ELEMENTS (self->categories) - 2] != NULL);
diff --git a/lib/gs-category.c b/lib/gs-category.c
index 92d75739a..f661dcfad 100644
--- a/lib/gs-category.c
+++ b/lib/gs-category.c
@@ -21,6 +21,7 @@
 #include <glib/gi18n.h>
 
 #include "gs-category-private.h"
+#include "gs-desktop-data.h"
 
 struct _GsCategory
 {
@@ -680,3 +681,44 @@ gs_category_new (const gchar *id)
        category->id = g_strdup (id);
        return GS_CATEGORY (category);
 }
+
+/**
+ * gs_category_new_for_desktop_data:
+ * @data: data for the category, which must be static and constant
+ *
+ * Create a new #GsCategory instance which wraps the desktop category
+ * information in @data. Where possible, the static data will be reused, so
+ * @data must be static and constant across the lifetime of the process.
+ *
+ * Returns: (transfer full): a new #GsCategory wrapping @data
+ * Since: 40
+ */
+GsCategory *
+gs_category_new_for_desktop_data (const GsDesktopData *data)
+{
+       g_autofree gchar *msgctxt = NULL;
+       g_autoptr(GsCategory) category = NULL;
+
+       /* parent category */
+       category = g_object_new (GS_TYPE_CATEGORY, NULL);
+
+       gs_category_set_icon_name (category, data->icon);
+       gs_category_set_name (category, gettext (data->name));
+       gs_category_set_score (category, data->score);
+
+       /* add subcategories */
+       msgctxt = g_strdup_printf ("Menu of %s", data->name);
+
+       for (gsize j = 0; data->mapping[j].id != NULL; j++) {
+               const GsDesktopMap *map = &data->mapping[j];
+               g_autoptr(GsCategory) sub = gs_category_new (map->id);
+               for (gsize k = 0; map->fdo_cats[k] != NULL; k++)
+                       gs_category_add_desktop_group (sub, map->fdo_cats[k]);
+               gs_category_set_name (sub, g_dpgettext2 (GETTEXT_PACKAGE,
+                                                        msgctxt,
+                                                        map->name));
+               gs_category_add_child (category, sub);
+       }
+
+       return g_steal_pointer (&category);
+}
diff --git a/lib/gs-category.h b/lib/gs-category.h
index cd15360e7..c49de86fc 100644
--- a/lib/gs-category.h
+++ b/lib/gs-category.h
@@ -13,12 +13,16 @@
 #include <glib.h>
 #include <glib-object.h>
 
+#include "gs-desktop-data.h"
+
 G_BEGIN_DECLS
 
 #define GS_TYPE_CATEGORY (gs_category_get_type ())
 
 G_DECLARE_FINAL_TYPE (GsCategory, gs_category, GS, CATEGORY, GObject)
 
+GsCategory     *gs_category_new_for_desktop_data       (const GsDesktopData    *data);
+
 GsCategory     *gs_category_new                (const gchar    *id);
 const gchar    *gs_category_get_id             (GsCategory     *category);
 GsCategory     *gs_category_get_parent         (GsCategory     *category);


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