[gnome-software] Move the name out of gs_category_new()



commit 97a5009dc7dda7cd0947053a5dc12674167dddd6
Author: Richard Hughes <richard hughsie com>
Date:   Tue May 31 12:25:15 2016 +0100

    Move the name out of gs_category_new()
    
    Most of the time it's not required.

 src/gs-category.c                            |   50 +++++++++++++++++---------
 src/gs-category.h                            |    6 ++-
 src/gs-cmd.c                                 |    4 +-
 src/gs-plugin-loader.c                       |    4 +-
 src/gs-shell-overview.c                      |    4 +-
 src/plugins/gs-plugin-appstream.c            |    2 +-
 src/plugins/gs-plugin-menu-spec-categories.c |   13 ++++---
 7 files changed, 51 insertions(+), 32 deletions(-)
---
diff --git a/src/gs-category.c b/src/gs-category.c
index c6447c4..5934038 100644
--- a/src/gs-category.c
+++ b/src/gs-category.c
@@ -120,10 +120,42 @@ const gchar *
 gs_category_get_name (GsCategory *category)
 {
        g_return_val_if_fail (GS_IS_CATEGORY (category), NULL);
+
+       /* special case, we don't want translations in the plugins */
+       if (g_strcmp0 (category->id, "other") == 0) {
+               /* TRANSLATORS: this is where all applications that don't
+                * fit in other groups are put */
+               return _("Other");
+       }
+       if (g_strcmp0 (category->id, "all") == 0) {
+               /* TRANSLATORS: this is a subcategory matching all the
+                * different apps in the parent category, e.g. "Games" */
+               return _("All");
+       }
+       if (g_strcmp0 (category->id, "featured") == 0) {
+               /* TRANSLATORS: this is a subcategory of featured apps */
+               return _("Featured");
+       }
+
        return category->name;
 }
 
 /**
+ * gs_category_set_name:
+ * @category: a #GsCategory
+ * @name: a category name, or %NULL
+ *
+ * Sets the category name.
+ **/
+void
+gs_category_set_name (GsCategory *category, const gchar *name)
+{
+       g_return_if_fail (GS_IS_CATEGORY (category));
+       g_free (category->name);
+       category->name = g_strdup (name);
+}
+
+/**
  * gs_category_find_child:
  * @category: a #GsCategory
  * @id: a category ID, e.g. "other"
@@ -281,27 +313,11 @@ gs_category_init (GsCategory *category)
  * Returns: the new #GsCategory
  **/
 GsCategory *
-gs_category_new (const gchar *id, const gchar *name)
+gs_category_new (const gchar *id)
 {
        GsCategory *category;
-
-       /* special case, we don't want translations in the plugins */
-       if (g_strcmp0 (id, "other") == 0) {
-               /* TRANSLATORS: this is where all applications that don't
-                * fit in other groups are put */
-               name =_("Other");
-       } else if (g_strcmp0 (id, "all") == 0) {
-               /* TRANSLATORS: this is a subcategory matching all the
-                * different apps in the parent category, e.g. "Games" */
-               name =_("All");
-       } else if (g_strcmp0 (id, "featured") == 0) {
-               /* TRANSLATORS: this is a subcategory of featured apps */
-               name =_("Featured");
-       }
-
        category = g_object_new (GS_TYPE_CATEGORY, NULL);
        category->id = g_strdup (id);
-       category->name = g_strdup (name);
        return GS_CATEGORY (category);
 }
 
diff --git a/src/gs-category.h b/src/gs-category.h
index 3e3b65d..f597162 100644
--- a/src/gs-category.h
+++ b/src/gs-category.h
@@ -31,11 +31,13 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GsCategory, gs_category, GS, CATEGORY, GObject)
 
-GsCategory     *gs_category_new                (const gchar    *id,
-                                                const gchar    *name);
+GsCategory     *gs_category_new                (const gchar    *id);
 const gchar    *gs_category_get_id             (GsCategory     *category);
 GsCategory     *gs_category_get_parent         (GsCategory     *category);
+
 const gchar    *gs_category_get_name           (GsCategory     *category);
+void            gs_category_set_name           (GsCategory     *category,
+                                                const gchar    *name);
 
 GsCategory     *gs_category_find_child         (GsCategory     *category,
                                                 const gchar    *id);
diff --git a/src/gs-cmd.c b/src/gs-cmd.c
index 5acba2d..776ea7f 100644
--- a/src/gs-cmd.c
+++ b/src/gs-cmd.c
@@ -432,10 +432,10 @@ main (int argc, char **argv)
                g_autoptr(GsCategory) category = NULL;
                g_auto(GStrv) split = NULL;
                split = g_strsplit (argv[2], "/", 2);
-               category = gs_category_new (split[0], NULL);
+               category = gs_category_new (split[0]);
                if (g_strv_length (split) == 2) {
                        g_autoptr(GsCategory) child = NULL;
-                       child = gs_category_new (split[1], NULL);
+                       child = gs_category_new (split[1]);
                        gs_category_add_child (category, child);
                }
                for (i = 0; i < repeat; i++) {
diff --git a/src/gs-plugin-loader.c b/src/gs-plugin-loader.c
index a9053fe..8f5fd22 100644
--- a/src/gs-plugin-loader.c
+++ b/src/gs-plugin-loader.c
@@ -2207,14 +2207,14 @@ gs_plugin_loader_get_categories_thread_cb (GTask *task,
                        continue;
                if (gs_category_find_child (parent, "all") == NULL) {
                        g_autoptr(GsCategory) child = NULL;
-                       child = gs_category_new ("all", NULL);
+                       child = gs_category_new ("all");
                        gs_category_add_child (parent, child);
                        /* this is probably valid... */
                        gs_category_set_size (child, gs_category_get_size (parent));
                }
                if (gs_category_find_child (parent, "featured") == NULL) {
                        g_autoptr(GsCategory) child = NULL;
-                       child = gs_category_new ("featured", NULL);
+                       child = gs_category_new ("featured");
                        gs_category_add_child (parent, child);
                        /* this is probably valid... */
                        gs_category_set_size (child, 5);
diff --git a/src/gs-shell-overview.c b/src/gs-shell-overview.c
index 8251ec7..55898bd 100644
--- a/src/gs-shell-overview.c
+++ b/src/gs-shell-overview.c
@@ -420,8 +420,8 @@ gs_shell_overview_load (GsShellOverview *self)
                g_autoptr(GsCategory) category = NULL;
                g_autoptr(GsCategory) featured_category = NULL;
 
-               category = gs_category_new (category_of_day, NULL);
-               featured_category = gs_category_new ("featured", NULL);
+               category = gs_category_new (category_of_day);
+               featured_category = gs_category_new ("featured");
                gs_category_add_child (category, featured_category);
 
                load_data = g_slice_new0 (LoadData);
diff --git a/src/plugins/gs-plugin-appstream.c b/src/plugins/gs-plugin-appstream.c
index 8ce0b12..244f223 100644
--- a/src/plugins/gs-plugin-appstream.c
+++ b/src/plugins/gs-plugin-appstream.c
@@ -560,7 +560,7 @@ gs_plugin_add_categories_for_app (GPtrArray *list, AsApp *app)
                if (!found_subcat) {
                        category = gs_category_find_child (parent, "other");
                        if (category == NULL) {
-                               category = gs_category_new ("other", NULL);
+                               category = gs_category_new ("other");
                                gs_category_add_child (parent, category);
                                g_object_unref (category);
                        }
diff --git a/src/plugins/gs-plugin-menu-spec-categories.c b/src/plugins/gs-plugin-menu-spec-categories.c
index 94e026c..6937210 100644
--- a/src/plugins/gs-plugin-menu-spec-categories.c
+++ b/src/plugins/gs-plugin-menu-spec-categories.c
@@ -48,14 +48,15 @@ gs_plugin_add_categories (GsPlugin *plugin,
        for (i = 0; msdata[i].path != NULL; i++) {
                tmp = g_strstr_len (msdata[i].path, -1, "::");
                if (tmp == NULL) {
-                       category = gs_category_new (msdata[i].path,
-                                                   gettext(msdata[i].text));
+                       category = gs_category_new (msdata[i].path);
+                       gs_category_set_name (category, gettext (msdata[i].text));
                        g_ptr_array_add (list, category);
-                       g_snprintf(msgctxt, 100, "Menu subcategory of %s", msdata[i].text);
+                       g_snprintf (msgctxt, 100, "Menu subcategory of %s", msdata[i].text);
                } else {
-                       g_autoptr(GsCategory) sub = NULL;
-                       sub = gs_category_new (tmp + 2,
-                                              g_dpgettext2(GETTEXT_PACKAGE, msgctxt, msdata[i].text));
+                       g_autoptr(GsCategory) sub = gs_category_new (tmp + 2);
+                       gs_category_set_name (sub, g_dpgettext2 (GETTEXT_PACKAGE,
+                                                                msgctxt,
+                                                                msdata[i].text));
                        gs_category_add_child (category, sub);
                }
        }


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