[gnome-software/wip/kalev/flatpak-noenumerate-rewrite: 10/10] flatpak: Rewrite noenumarate=true filtering code



commit 828ee9fc2c0e53ab7c3e98debee94361db904394
Author: Kalev Lember <klember redhat com>
Date:   Mon Nov 19 19:04:33 2018 +0100

    flatpak: Rewrite noenumarate=true filtering code
    
    For apps installed from repos that have noenumarate=true, we were using
    the origin name (e.g. "org.frozen_bubble.frozen-bubble-origin") and
    tried to figure out the installed ref based on that. The code would
    split on - and append .desktop and end up with something like
    "org.frozen_bubble.frozen.desktop", which is pretty far from the real
    app name "org.frozen_bubble.frozen-bubble".
    
    This commit replaces this by just asking flatpak what ref is installed
    from that repo.
    
    This is slightly made more complicated by the fact that the flatpak API
    only appears in flatpak 1.1.1, but we need to support 1.0 as well, so
    this commit also adds fallback to parsing the key file manually.
    
    Fixes: https://gitlab.gnome.org/GNOME/gnome-software/issues/195

 plugins/flatpak/gs-flatpak.c | 57 +++++++++++++++++++++++++++++++++++++-------
 1 file changed, 48 insertions(+), 9 deletions(-)
---
diff --git a/plugins/flatpak/gs-flatpak.c b/plugins/flatpak/gs-flatpak.c
index e85a8566..bd2d0851 100644
--- a/plugins/flatpak/gs-flatpak.c
+++ b/plugins/flatpak/gs-flatpak.c
@@ -244,6 +244,39 @@ gs_flatpak_remove_prefixed_names (AsApp *app)
        }
 }
 
+#if !FLATPAK_CHECK_VERSION(1,1,1)
+static gchar *
+gs_flatpak_get_xremote_main_ref (GsFlatpak *self, FlatpakRemote *xremote, GError **error)
+{
+               g_autoptr(GFile) dir = NULL;
+               g_autofree gchar *dir_path = NULL;
+               g_autofree gchar *config_fn = NULL;
+               g_autofree gchar *group = NULL;
+               g_autofree gchar *main_ref = NULL;
+               g_autoptr(GKeyFile) kf = NULL;
+
+               /* figure out the path to the config keyfile */
+               dir = flatpak_installation_get_path (self->installation);
+               if (dir == NULL)
+                       return NULL;
+               dir_path = g_file_get_path (dir);
+               if (dir_path == NULL)
+                       return NULL;
+               config_fn = g_build_filename (dir_path, "repo", "config", NULL);
+
+               kf = g_key_file_new ();
+               if (!g_key_file_load_from_file (kf, config_fn, G_KEY_FILE_NONE, error))
+                       return NULL;
+
+               group = g_strdup_printf ("remote \"%s\"", flatpak_remote_get_name (xremote));
+               main_ref = g_key_file_get_string (kf, group, "xa.main-ref", error);
+               if (main_ref == NULL)
+                       return NULL;
+
+               return g_steal_pointer (&main_ref);
+}
+#endif
+
 static gboolean
 gs_flatpak_add_apps_from_xremote (GsFlatpak *self,
                                  FlatpakRemote *xremote,
@@ -254,7 +287,7 @@ gs_flatpak_add_apps_from_xremote (GsFlatpak *self,
        g_autofree gchar *appstream_dir_fn = NULL;
        g_autofree gchar *appstream_fn = NULL;
        g_autofree gchar *default_branch = NULL;
-       g_autofree gchar *only_app_id = NULL;
+       g_autofree gchar *main_ref = NULL;
        g_autoptr(AsStore) store = NULL;
        g_autoptr(GFile) appstream_dir = NULL;
        g_autoptr(GFile) file = NULL;
@@ -306,10 +339,14 @@ gs_flatpak_add_apps_from_xremote (GsFlatpak *self,
 
        /* only add the specific app for noenumerate=true */
        if (flatpak_remote_get_noenumerate (xremote)) {
-               g_autofree gchar *tmp = NULL;
-               tmp = g_strdup (flatpak_remote_get_name (xremote));
-               g_strdelimit (tmp, "-", '\0');
-               only_app_id = g_strdup_printf ("%s.desktop", tmp);
+#if FLATPAK_CHECK_VERSION(1,1,1)
+               main_ref = flatpak_remote_get_main_ref (xremote);
+#else
+               g_autoptr(GError) error_local = NULL;
+               main_ref = gs_flatpak_get_xremote_main_ref (self, xremote, &error_local);
+               if (main_ref == NULL)
+                       g_warning ("failed to main ref: %s", error_local->message);
+#endif
        }
 
        /* do we want to filter to the default branch */
@@ -323,10 +360,12 @@ gs_flatpak_add_apps_from_xremote (GsFlatpak *self,
                AsApp *app = g_ptr_array_index (apps, i);
 
                /* filter to app */
-               if (only_app_id != NULL &&
-                   g_strcmp0 (as_app_get_id (app), only_app_id) != 0) {
-                       as_app_set_kind (app, AS_APP_KIND_UNKNOWN);
-                       continue;
+               if (flatpak_remote_get_noenumerate (xremote)) {
+                       AsBundle *bundle = as_app_get_bundle_default (app);
+                       if (bundle == NULL || main_ref == NULL)
+                               continue;
+                       if (g_strcmp0 (as_bundle_get_id (bundle), main_ref) != 0)
+                               continue;
                }
 
                /* filter by branch */


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