[gnome-software: 9/25] lib: Add menu path resolving code directly to GsApp




commit 074f1642eb5991c61f2fb7277165676434061858
Author: Philip Withnall <pwithnall endlessos org>
Date:   Mon Feb 1 23:17:32 2021 +0000

    lib: Add menu path resolving code directly to GsApp
    
    Move it from the `desktop-menu-path` plugin, so that it can be run even
    more on-demand than currently. Currently, the
    `GS_PLUGIN_REFINE_FLAGS_REQUIRE_MENU_PATH` flag is used.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 lib/gs-app.c                               | 50 ++++++++++++++++++
 plugins/core/gs-plugin-desktop-menu-path.c | 82 ------------------------------
 2 files changed, 50 insertions(+), 82 deletions(-)
---
diff --git a/lib/gs-app.c b/lib/gs-app.c
index 9dc4465f5..5b613fd69 100644
--- a/lib/gs-app.c
+++ b/lib/gs-app.c
@@ -40,6 +40,7 @@
 
 #include "gs-app-collation.h"
 #include "gs-app-private.h"
+#include "gs-desktop-data.h"
 #include "gs-enums.h"
 #include "gs-os-release.h"
 #include "gs-plugin.h"
@@ -2515,6 +2516,18 @@ gs_app_set_summary_missing (GsApp *app, const gchar *summary_missing)
        _g_set_str (&priv->summary_missing, summary_missing);
 }
 
+static gboolean
+_gs_app_has_desktop_group (GsApp *app, const gchar *desktop_group)
+{
+       guint i;
+       g_auto(GStrv) split = g_strsplit (desktop_group, "::", -1);
+       for (i = 0; split[i] != NULL; i++) {
+               if (!gs_app_has_category (app, split[i]))
+                       return FALSE;
+       }
+       return TRUE;
+}
+
 /**
  * gs_app_get_menu_path:
  * @app: a #GsApp
@@ -2532,6 +2545,43 @@ gs_app_get_menu_path (GsApp *app)
 {
        GsAppPrivate *priv = gs_app_get_instance_private (app);
        g_return_val_if_fail (GS_IS_APP (app), NULL);
+
+       /* Lazy load. */
+       if (priv->menu_path == NULL) {
+               const gchar *strv[] = { "", NULL, NULL };
+               const GsDesktopData *msdata;
+               gboolean found = FALSE;
+
+               /* find a top level category the app has */
+               msdata = gs_desktop_get_data ();
+               for (gsize i = 0; !found && msdata[i].id != NULL; i++) {
+                       const GsDesktopData *data = &msdata[i];
+                       for (gsize j = 0; !found && data->mapping[j].id != NULL; j++) {
+                               const GsDesktopMap *map = &data->mapping[j];
+                               g_autofree gchar *msgctxt = NULL;
+
+                               if (g_strcmp0 (map->id, "all") == 0)
+                                       continue;
+                               if (g_strcmp0 (map->id, "featured") == 0)
+                                       continue;
+                               msgctxt = g_strdup_printf ("Menu of %s", data->name);
+                               for (gsize k = 0; !found && map->fdo_cats[k] != NULL; k++) {
+                                       const gchar *tmp = msdata[i].mapping[j].fdo_cats[k];
+                                       if (_gs_app_has_desktop_group (app, tmp)) {
+                                               strv[0] = g_dgettext (GETTEXT_PACKAGE, msdata[i].name);
+                                               strv[1] = g_dpgettext2 (GETTEXT_PACKAGE, msgctxt,
+                                                                       msdata[i].mapping[j].name);
+                                               found = TRUE;
+                                               break;
+                                       }
+                               }
+                       }
+               }
+
+               /* always set something to avoid keep searching for this */
+               gs_app_set_menu_path (app, (gchar **) strv);
+       }
+
        return priv->menu_path;
 }
 
diff --git a/plugins/core/gs-plugin-desktop-menu-path.c b/plugins/core/gs-plugin-desktop-menu-path.c
index a1c53189f..61f08ff5b 100644
--- a/plugins/core/gs-plugin-desktop-menu-path.c
+++ b/plugins/core/gs-plugin-desktop-menu-path.c
@@ -25,85 +25,3 @@ gs_plugin_initialize (GsPlugin *plugin)
        /* need categories */
        gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "appstream");
 }
-
-static gboolean
-_gs_app_has_desktop_group (GsApp *app, const gchar *desktop_group)
-{
-       guint i;
-       g_auto(GStrv) split = g_strsplit (desktop_group, "::", -1);
-       for (i = 0; split[i] != NULL; i++) {
-               if (!gs_app_has_category (app, split[i]))
-                       return FALSE;
-       }
-       return TRUE;
-}
-
-/* adds the menu-path for applications */
-static gboolean
-refine_app (GsPlugin             *plugin,
-           GsApp                *app,
-           GsPluginRefineFlags   flags,
-           GCancellable         *cancellable,
-           GError              **error)
-{
-       const gchar *strv[] = { "", NULL, NULL };
-       const GsDesktopData *msdata;
-       gboolean found = FALSE;
-       guint i, j, k;
-
-       /* nothing to do here */
-       if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_MENU_PATH) == 0)
-               return TRUE;
-       if (gs_app_get_menu_path (app) != NULL)
-               return TRUE;
-
-       /* find a top level category the app has */
-       msdata = gs_desktop_get_data ();
-       for (i = 0; !found && msdata[i].id != NULL; i++) {
-               const GsDesktopData *data = &msdata[i];
-               for (j = 0; !found && data->mapping[j].id != NULL; j++) {
-                       const GsDesktopMap *map = &data->mapping[j];
-                       g_autofree gchar *msgctxt = NULL;
-
-                       if (g_strcmp0 (map->id, "all") == 0)
-                               continue;
-                       if (g_strcmp0 (map->id, "featured") == 0)
-                               continue;
-                       msgctxt = g_strdup_printf ("Menu of %s", data->name);
-                       for (k = 0; !found && map->fdo_cats[k] != NULL; k++) {
-                               const gchar *tmp = msdata[i].mapping[j].fdo_cats[k];
-                               if (_gs_app_has_desktop_group (app, tmp)) {
-                                       strv[0] = g_dgettext (GETTEXT_PACKAGE, msdata[i].name);
-                                       strv[1] = g_dpgettext2 (GETTEXT_PACKAGE, msgctxt,
-                                                               msdata[i].mapping[j].name);
-                                       found = TRUE;
-                                       break;
-                               }
-                       }
-               }
-       }
-
-       /* always set something to avoid keep searching for this */
-       gs_app_set_menu_path (app, (gchar **) strv);
-       return TRUE;
-}
-
-gboolean
-gs_plugin_refine (GsPlugin             *plugin,
-                 GsAppList            *list,
-                 GsPluginRefineFlags   flags,
-                 GCancellable         *cancellable,
-                 GError              **error)
-{
-       /* nothing to do here */
-       if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_MENU_PATH) == 0)
-               return TRUE;
-
-       for (guint i = 0; i < gs_app_list_length (list); i++) {
-               GsApp *app = gs_app_list_index (list, i);
-               if (!refine_app (plugin, app, flags, cancellable, error))
-                       return FALSE;
-       }
-
-       return TRUE;
-}


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