[gnome-software/wip/hughsie/GsApp-custom: 64/64] flatpak: Use a custom GsApp GTypes for results



commit 36225af265d0d973312ab19faa7f8dc59b717935
Author: Richard Hughes <richard hughsie com>
Date:   Mon Feb 11 12:17:28 2019 +0000

    flatpak: Use a custom GsApp GTypes for results

 plugins/flatpak/gs-flatpak-app.c    | 230 +++++++++++++++++++++++++++---------
 plugins/flatpak/gs-flatpak-app.h    |   4 +
 plugins/flatpak/gs-flatpak.c        |  12 +-
 plugins/flatpak/gs-plugin-flatpak.c |   3 +
 4 files changed, 192 insertions(+), 57 deletions(-)
---
diff --git a/plugins/flatpak/gs-flatpak-app.c b/plugins/flatpak/gs-flatpak-app.c
index 78e4e9b1..0f587877 100644
--- a/plugins/flatpak/gs-flatpak-app.c
+++ b/plugins/flatpak/gs-flatpak-app.c
@@ -11,61 +11,149 @@
 
 #include "gs-flatpak-app.h"
 
+struct _GsFlatpakApp
+{
+       GsApp                    parent_instance;
+       FlatpakRefKind           ref_kind;
+       gchar                   *ref_arch;
+       gchar                   *ref_branch;
+       gchar                   *ref_name;
+       gchar                   *commit;
+       gchar                   *object_id;
+       gchar                   *repo_gpgkey;
+       gchar                   *repo_url;
+       gchar                   *runtime_url;
+       gchar                   *main_app_ref;
+       GsFlatpakAppFileKind     file_kind;
+};
+
+G_DEFINE_TYPE (GsFlatpakApp, gs_flatpak_app, GS_TYPE_APP)
+
+static gboolean
+_g_set_str (gchar **str_ptr, const gchar *new_str)
+{
+       if (*str_ptr == new_str || g_strcmp0 (*str_ptr, new_str) == 0)
+               return FALSE;
+       g_free (*str_ptr);
+       *str_ptr = g_strdup (new_str);
+       return TRUE;
+}
+
+static const gchar *
+gs_flatpak_app_file_kind_to_string (GsFlatpakAppFileKind file_kind)
+{
+       if (file_kind == GS_FLATPAK_APP_FILE_KIND_REPO)
+               return "flatpakrepo";
+       if (file_kind == GS_FLATPAK_APP_FILE_KIND_REF)
+               return "flatpakref";
+       if (file_kind == GS_FLATPAK_APP_FILE_KIND_BUNDLE)
+               return "flatpak";
+       return NULL;
+}
+
+static void
+gs_flatpak_app_to_string (GsApp *app, GString *str)
+{
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       gs_utils_append_key_value (str, 20, "flatpak::ref-kind",
+                                  gs_flatpak_app_get_ref_kind_as_str (app));
+       if (self->ref_name != NULL) {
+               gs_utils_append_key_value (str, 20, "flatpak::ref-name",
+                                          self->ref_name);
+       }
+       if (self->ref_arch != NULL) {
+               gs_utils_append_key_value (str, 20, "flatpak::ref-arch",
+                                          self->ref_arch);
+       }
+       if (self->ref_branch != NULL) {
+               gs_utils_append_key_value (str, 20, "flatpak::ref-branch",
+                                          self->ref_branch);
+       }
+       if (self->commit != NULL)
+               gs_utils_append_key_value (str, 20, "flatpak::commit",
+                                  self->commit);
+       if (self->object_id != NULL) {
+               gs_utils_append_key_value (str, 20, "flatpak::object-id",
+                                          self->object_id);
+       }
+       if (self->repo_gpgkey != NULL) {
+               gs_utils_append_key_value (str, 20, "flatpak::repo-gpgkey",
+                                          self->repo_gpgkey);
+       }
+       if (self->repo_url != NULL) {
+               gs_utils_append_key_value (str, 20, "flatpak::repo-url",
+                                          self->repo_url);
+       }
+       if (self->runtime_url != NULL) {
+               gs_utils_append_key_value (str, 20, "flatpak::runtime-url",
+                                          self->runtime_url);
+       }
+       if (self->main_app_ref != NULL) {
+               gs_utils_append_key_value (str, 20, "flatpak::main-app-ref",
+                                          self->main_app_ref);
+       }
+       if (self->file_kind != GS_FLATPAK_APP_FILE_KIND_UNKNOWN) {
+               gs_utils_append_key_value (str, 20, "flatpak::file-kind",
+                                          gs_flatpak_app_file_kind_to_string (self->file_kind));
+       }
+}
+
 const gchar *
 gs_flatpak_app_get_ref_name (GsApp *app)
 {
-       return gs_app_get_metadata_item (app, "flatpak::RefName");
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       return self->ref_name;
 }
 
 const gchar *
 gs_flatpak_app_get_ref_arch (GsApp *app)
 {
-       return gs_app_get_metadata_item (app, "flatpak::RefArch");
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       return self->ref_arch;
 }
 
 const gchar *
 gs_flatpak_app_get_ref_branch (GsApp *app)
 {
-       return gs_app_get_metadata_item (app, "flatpak::RefBranch");
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       return self->ref_branch;
 }
 
 const gchar *
 gs_flatpak_app_get_commit (GsApp *app)
 {
-       return gs_app_get_metadata_item (app, "flatpak::Commit");
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       return self->commit;
 }
 
 GsFlatpakAppFileKind
 gs_flatpak_app_get_file_kind (GsApp *app)
 {
-       GVariant *tmp = gs_app_get_metadata_variant (app, "flatpak::FileKind");
-       if (tmp == NULL)
-               return GS_FLATPAK_APP_FILE_KIND_UNKNOWN;
-       return g_variant_get_uint32 (tmp);
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       return self->file_kind;
 }
 
 const gchar *
 gs_flatpak_app_get_runtime_url (GsApp *app)
 {
-       return gs_app_get_metadata_item (app, "flatpak::RuntimeUrl");
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       return self->runtime_url;
 }
 
 FlatpakRefKind
 gs_flatpak_app_get_ref_kind (GsApp *app)
 {
-       GVariant *tmp = gs_app_get_metadata_variant (app, "flatpak::RefKind");
-       if (tmp == NULL)
-               return FLATPAK_REF_KIND_APP;
-       return g_variant_get_uint32 (tmp);
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       return self->ref_kind;
 }
 
 const gchar *
 gs_flatpak_app_get_ref_kind_as_str (GsApp *app)
 {
-       FlatpakRefKind ref_kind = gs_flatpak_app_get_ref_kind (app);
-       if (ref_kind == FLATPAK_REF_KIND_APP)
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       if (self->ref_kind == FLATPAK_REF_KIND_APP)
                return "app";
-       if (ref_kind == FLATPAK_REF_KIND_RUNTIME)
+       if (self->ref_kind == FLATPAK_REF_KIND_RUNTIME)
                return "runtime";
        return NULL;
 }
@@ -73,117 +161,151 @@ gs_flatpak_app_get_ref_kind_as_str (GsApp *app)
 const gchar *
 gs_flatpak_app_get_object_id (GsApp *app)
 {
-       return gs_app_get_metadata_item (app, "flatpak::ObjectID");
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       return self->object_id;
 }
 
 const gchar *
 gs_flatpak_app_get_repo_gpgkey (GsApp *app)
 {
-       return gs_app_get_metadata_item (app, "flatpak::RepoGpgKey");
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       return self->repo_gpgkey;
 }
 
 const gchar *
 gs_flatpak_app_get_repo_url (GsApp *app)
 {
-       return gs_app_get_metadata_item (app, "flatpak::RepoUrl");
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       return self->repo_url;
+}
+
+const gchar *
+gs_flatpak_app_get_main_app_ref_name (GsApp *app)
+{
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       return self->main_app_ref;
 }
 
 gchar *
 gs_flatpak_app_get_ref_display (GsApp *app)
 {
-       const gchar *ref_kind_as_str = gs_flatpak_app_get_ref_kind_as_str (app);
-       const gchar *ref_name = gs_flatpak_app_get_ref_name (app);
-       const gchar *ref_arch = gs_flatpak_app_get_ref_arch (app);
-       const gchar *ref_branch = gs_flatpak_app_get_ref_branch (app);
-
-       g_return_val_if_fail (ref_kind_as_str != NULL, NULL);
-       g_return_val_if_fail (ref_name != NULL, NULL);
-       g_return_val_if_fail (ref_arch != NULL, NULL);
-       g_return_val_if_fail (ref_branch != NULL, NULL);
-
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
        return g_strdup_printf ("%s/%s/%s/%s",
-                               ref_kind_as_str,
-                               ref_name,
-                               ref_arch,
-                               ref_branch);
+                               gs_flatpak_app_get_ref_kind_as_str (app),
+                               self->ref_name,
+                               self->ref_arch,
+                               self->ref_branch);
 }
 
 void
 gs_flatpak_app_set_ref_name (GsApp *app, const gchar *val)
 {
-       gs_app_set_metadata (app, "flatpak::RefName", val);
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       _g_set_str (&self->ref_name, val);
 }
 
 void
 gs_flatpak_app_set_ref_arch (GsApp *app, const gchar *val)
 {
-       gs_app_set_metadata (app, "flatpak::RefArch", val);
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       _g_set_str (&self->ref_arch, val);
 }
 
 void
 gs_flatpak_app_set_ref_branch (GsApp *app, const gchar *val)
 {
-       gs_app_set_metadata (app, "flatpak::RefBranch", val);
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       _g_set_str (&self->ref_branch, val);
 }
 
 void
 gs_flatpak_app_set_commit (GsApp *app, const gchar *val)
 {
-       gs_app_set_metadata (app, "flatpak::Commit", val);
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       _g_set_str (&self->commit, val);
 }
 
 void
 gs_flatpak_app_set_file_kind (GsApp *app, GsFlatpakAppFileKind file_kind)
 {
-       g_autoptr(GVariant) tmp = g_variant_new_uint32 (file_kind);
-       gs_app_set_metadata_variant (app, "flatpak::FileKind", tmp);
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       self->file_kind = file_kind;
 }
 
 void
 gs_flatpak_app_set_runtime_url (GsApp *app, const gchar *val)
 {
-       gs_app_set_metadata (app, "flatpak::RuntimeUrl", val);
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       _g_set_str (&self->runtime_url, val);
 }
 
 void
 gs_flatpak_app_set_ref_kind (GsApp *app, FlatpakRefKind ref_kind)
 {
-       g_autoptr(GVariant) tmp = g_variant_new_uint32 (ref_kind);
-       gs_app_set_metadata_variant (app, "flatpak::RefKind", tmp);
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       self->ref_kind = ref_kind;
 }
 
 void
 gs_flatpak_app_set_object_id (GsApp *app, const gchar *val)
 {
-       gs_app_set_metadata (app, "flatpak::ObjectID", val);
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       _g_set_str (&self->object_id, val);
 }
 
 void
 gs_flatpak_app_set_repo_gpgkey (GsApp *app, const gchar *val)
 {
-       gs_app_set_metadata (app, "flatpak::RepoGpgKey", val);
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       _g_set_str (&self->repo_gpgkey, val);
 }
 
 void
 gs_flatpak_app_set_repo_url (GsApp *app, const gchar *val)
 {
-       gs_app_set_metadata (app, "flatpak::RepoUrl", val);
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       _g_set_str (&self->repo_url, val);
 }
 
-GsApp *
-gs_flatpak_app_new (const gchar *id)
+void
+gs_flatpak_app_set_main_app_ref_name (GsApp *app, const gchar *val)
 {
-       return GS_APP (g_object_new (GS_TYPE_APP, "id", id, NULL));
+       GsFlatpakApp *self = GS_FLATPAK_APP (app);
+       _g_set_str (&self->main_app_ref, val);
 }
 
-void
-gs_flatpak_app_set_main_app_ref_name (GsApp *app, const gchar *main_app_ref)
+static void
+gs_flatpak_app_finalize (GObject *object)
 {
-       gs_app_set_metadata (app, "flatpak::mainApp", main_app_ref);
+       GsFlatpakApp *self = GS_FLATPAK_APP (object);
+       g_free (self->ref_arch);
+       g_free (self->ref_branch);
+       g_free (self->ref_name);
+       g_free (self->commit);
+       g_free (self->object_id);
+       g_free (self->runtime_url);
+       g_free (self->repo_gpgkey);
+       g_free (self->repo_url);
+       g_free (self->main_app_ref);
+       G_OBJECT_CLASS (gs_flatpak_app_parent_class)->finalize (object);
 }
 
-const gchar *
-gs_flatpak_app_get_main_app_ref_name (GsApp *app)
+static void
+gs_flatpak_app_class_init (GsFlatpakAppClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       GsAppClass *klass_app = GS_APP_CLASS (klass);
+       klass_app->to_string = gs_flatpak_app_to_string;
+       object_class->finalize = gs_flatpak_app_finalize;
+}
+
+static void
+gs_flatpak_app_init (GsFlatpakApp *self)
+{
+}
+
+GsApp *
+gs_flatpak_app_new (const gchar *id)
 {
-       return gs_app_get_metadata_item (app, "flatpak::mainApp");
+       return GS_APP (g_object_new (GS_TYPE_FLATPAK_APP, "id", id, NULL));
 }
diff --git a/plugins/flatpak/gs-flatpak-app.h b/plugins/flatpak/gs-flatpak-app.h
index ba4c23cd..496ea181 100644
--- a/plugins/flatpak/gs-flatpak-app.h
+++ b/plugins/flatpak/gs-flatpak-app.h
@@ -12,6 +12,10 @@
 
 G_BEGIN_DECLS
 
+#define GS_TYPE_FLATPAK_APP (gs_flatpak_app_get_type ())
+
+G_DECLARE_FINAL_TYPE (GsFlatpakApp, gs_flatpak_app, GS, FLATPAK_APP, GsApp)
+
 typedef enum {
        GS_FLATPAK_APP_FILE_KIND_UNKNOWN,
        GS_FLATPAK_APP_FILE_KIND_REPO,
diff --git a/plugins/flatpak/gs-flatpak.c b/plugins/flatpak/gs-flatpak.c
index d0a67791..9a896a25 100644
--- a/plugins/flatpak/gs-flatpak.c
+++ b/plugins/flatpak/gs-flatpak.c
@@ -246,10 +246,16 @@ gs_flatpak_create_app (GsFlatpak *self, const gchar *origin, FlatpakRef *xref)
        if (origin != NULL)
                gs_app_set_origin (app, origin);
 
-       /* return the ref'd cached copy */
+       /* we already have one and it's a Flatpak app, return the ref'd cached copy */
        app_cached = gs_plugin_cache_lookup (self->plugin, gs_app_get_unique_id (app));
-       if (app_cached != NULL)
-               return app_cached;
+       if (app_cached != NULL) {
+               if (GS_IS_FLATPAK_APP (app_cached)) {
+                       return app_cached;
+               } else {
+                       g_warning ("Found cached app in Flatpak plugin that is not a Flatpak app: %s; ",
+                                  gs_app_get_unique_id (app_cached));
+               }
+       }
 
        /* fallback values */
        if (gs_app_get_kind (app) == AS_APP_KIND_RUNTIME) {
diff --git a/plugins/flatpak/gs-plugin-flatpak.c b/plugins/flatpak/gs-plugin-flatpak.c
index 11f1cf20..716f2042 100644
--- a/plugins/flatpak/gs-plugin-flatpak.c
+++ b/plugins/flatpak/gs-plugin-flatpak.c
@@ -63,6 +63,9 @@ gs_plugin_initialize (GsPlugin *plugin)
                                          g_permission_get_can_acquire (permission);
        }
 
+       /* unique to us */
+       gs_plugin_set_app_gtype (plugin, GS_TYPE_FLATPAK_APP);
+
        /* used for self tests */
        priv->destdir_for_tests = g_getenv ("GS_SELF_TEST_FLATPAK_DATADIR");
 }


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