[gnome-software: 7/14] snap: Refactor code mapping categories to sections




commit bb05a1f3f50c9ca20cc66dc2ed6ba80fab84756f
Author: Philip Withnall <pwithnall endlessos org>
Date:   Wed May 4 15:13:52 2022 +0100

    snap: Refactor code mapping categories to sections
    
    This introduces no functional changes, but does eliminate runtime
    string-splitting of compile-time constant strings, and makes the code a
    bit less hardcoded to particular categories.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 plugins/snap/gs-plugin-snap.c | 76 +++++++++++++++++++++----------------------
 1 file changed, 38 insertions(+), 38 deletions(-)
---
diff --git a/plugins/snap/gs-plugin-snap.c b/plugins/snap/gs-plugin-snap.c
index baa4893b7..6fee274ba 100644
--- a/plugins/snap/gs-plugin-snap.c
+++ b/plugins/snap/gs-plugin-snap.c
@@ -615,16 +615,9 @@ gs_plugin_add_category_apps (GsPlugin *plugin,
        GsPluginSnap *self = GS_PLUGIN_SNAP (plugin);
        g_autoptr(SnapdClient) client = NULL;
        g_autofree gchar *category_path = NULL;
-       const gchar *sections = NULL;
+       const gchar * const *sections = NULL;
        gboolean interactive = gs_plugin_has_flags (plugin, GS_PLUGIN_FLAGS_INTERACTIVE);
 
-       /* Create client. */
-       client = get_client (self, interactive, error);
-       if (client == NULL)
-               return FALSE;
-
-       category_path = category_build_full_path (category);
-
        /*
         * Unused categories:
         *
@@ -635,39 +628,46 @@ gs_plugin_add_category_apps (GsPlugin *plugin,
         * server-and-cloud
         * entertainment
         */
+       const struct {
+               const gchar *category_path;
+               const gchar *sections[4];
+       } category_to_sections_map[] = {
+               { "play/featured", { "games", NULL, }},
+               { "create/featured", { "photo-and-video", "art-and-design", "music-and-video", NULL, }},
+               { "socialize/featured", { "social", "news-and-weather", NULL, }},
+               { "work/featured", { "productivity", "finance", "utilities", NULL, }},
+               { "develop/featured", { "development", NULL, }},
+               { "learn/featured", { "education", "science", "books-and-reference", NULL, }},
+       };
 
-       if (strcmp (category_path, "play/featured") == 0)
-               sections = "games";
-       else if (strcmp (category_path, "create/featured") == 0)
-               sections = "photo-and-video;art-and-design;music-and-video";
-       else if (strcmp (category_path, "socialize/featured") == 0)
-               sections = "social;news-and-weather";
-       else if (strcmp (category_path, "work/featured") == 0)
-               sections = "productivity;finance;utilities";
-       else if (strcmp (category_path, "develop/featured") == 0)
-               sections = "development";
-       else if (strcmp (category_path, "learn/featured") == 0)
-               sections = "education;science;books-and-reference";
-
-       if (sections != NULL) {
-               g_auto(GStrv) tokens = NULL;
-               int i;
-
-               tokens = g_strsplit (sections, ";", -1);
-               for (i = 0; tokens[i] != NULL; i++) {
-                       g_autoptr(GPtrArray) snaps = NULL;
-                       guint j;
-
-                       snaps = find_snaps (self, client, SNAPD_FIND_FLAGS_SCOPE_WIDE,
-                                           tokens[i], NULL, cancellable, error);
-                       if (snaps == NULL)
-                               return FALSE;
-                       for (j = 0; j < snaps->len; j++) {
-                               g_autoptr(GsApp) app = snap_to_app (self, g_ptr_array_index (snaps, j), NULL);
-                               gs_app_list_add (list, app);
-                       }
+       /* Create client. */
+       client = get_client (self, interactive, error);
+       if (client == NULL)
+               return FALSE;
+
+       category_path = category_build_full_path (category);
+
+       for (gsize i = 0; i < G_N_ELEMENTS (category_to_sections_map); i++) {
+               if (g_str_equal (category_to_sections_map[i].category_path, category_path)) {
+                       sections = category_to_sections_map[i].sections;
+                       break;
+               }
+       }
+
+       for (gsize i = 0; sections != NULL && sections[i] != NULL; i++) {
+               g_autoptr(GPtrArray) snaps = NULL;
+
+               snaps = find_snaps (self, client, SNAPD_FIND_FLAGS_SCOPE_WIDE,
+                                   sections[i], NULL, cancellable, error);
+               if (snaps == NULL)
+                       return FALSE;
+
+               for (guint j = 0; j < snaps->len; j++) {
+                       g_autoptr(GsApp) app = snap_to_app (self, g_ptr_array_index (snaps, j), NULL);
+                       gs_app_list_add (list, app);
                }
        }
+
        return TRUE;
 }
 


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