[gnome-software/wip/hughsie/cachekey: 3/4] fwupd: Use custom GsApp GType



commit ac2fd182bf6af1fd4492f4774f194d53cbf259b8
Author: Richard Hughes <richard hughsie com>
Date:   Thu Jun 20 11:38:48 2019 +0100

    fwupd: Use custom GsApp GType

 plugins/fwupd/gs-fwupd-app.c    | 87 ++++++++++++++++++++++++++++++++++++-----
 plugins/fwupd/gs-fwupd-app.h    |  5 +++
 plugins/fwupd/gs-plugin-fwupd.c | 13 ++++--
 3 files changed, 91 insertions(+), 14 deletions(-)
---
diff --git a/plugins/fwupd/gs-fwupd-app.c b/plugins/fwupd/gs-fwupd-app.c
index 6c9d50a2..27e9eff9 100644
--- a/plugins/fwupd/gs-fwupd-app.c
+++ b/plugins/fwupd/gs-fwupd-app.c
@@ -11,44 +11,82 @@
 
 #include "gs-fwupd-app.h"
 
+struct _GsFwupdApp
+{
+       GsApp                    parent_instance;
+       gchar                   *device_id;
+       gchar                   *update_uri;
+       gboolean                 is_locked;
+};
+
+G_DEFINE_TYPE (GsFwupdApp, gs_fwupd_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 void
+gs_fwupd_app_to_string (GsApp *app, GString *str)
+{
+       GsFwupdApp *self = GS_FWUPD_APP (app);
+       if (self->device_id != NULL) {
+               gs_utils_append_key_value (str, 20, "fwupd::device-id",
+                                          self->device_id);
+       }
+       if (self->update_uri != NULL) {
+               gs_utils_append_key_value (str, 20, "fwupd::update-uri",
+                                          self->update_uri);
+       }
+       gs_utils_append_key_value (str, 20, "fwupd::is-locked",
+                                  self->is_locked ? "yes" : "no");
+}
+
 const gchar *
 gs_fwupd_app_get_device_id (GsApp *app)
 {
-       return gs_app_get_metadata_item (app, "fwupd::DeviceID");
+       GsFwupdApp *self = GS_FWUPD_APP (app);
+       return self->device_id;
 }
 
 const gchar *
 gs_fwupd_app_get_update_uri (GsApp *app)
 {
-       return gs_app_get_metadata_item (app, "fwupd::UpdateID");
+       GsFwupdApp *self = GS_FWUPD_APP (app);
+       return self->update_uri;
 }
 
 gboolean
 gs_fwupd_app_get_is_locked (GsApp *app)
 {
-       GVariant *tmp = gs_app_get_metadata_variant (app, "fwupd::IsLocked");
-       if (tmp == NULL)
-               return FALSE;
-       return g_variant_get_boolean (tmp);
+       GsFwupdApp *self = GS_FWUPD_APP (app);
+       return self->is_locked;
 }
 
 void
 gs_fwupd_app_set_device_id (GsApp *app, const gchar *device_id)
 {
-       gs_app_set_metadata (app, "fwupd::DeviceID", device_id);
+       GsFwupdApp *self = GS_FWUPD_APP (app);
+       _g_set_str (&self->device_id, device_id);
 }
 
 void
 gs_fwupd_app_set_update_uri (GsApp *app, const gchar *update_uri)
 {
-       gs_app_set_metadata (app, "fwupd::UpdateID", update_uri);
+       GsFwupdApp *self = GS_FWUPD_APP (app);
+       _g_set_str (&self->update_uri, update_uri);
 }
 
 void
 gs_fwupd_app_set_is_locked (GsApp *app, gboolean is_locked)
 {
-       g_autoptr(GVariant) tmp = g_variant_new_boolean (is_locked);
-       gs_app_set_metadata_variant (app, "fwupd::IsLocked", tmp);
+       GsFwupdApp *self = GS_FWUPD_APP (app);
+       self->is_locked = is_locked;
 }
 
 void
@@ -157,3 +195,32 @@ gs_fwupd_app_set_from_release (GsApp *app, FwupdRelease *rel)
                        gs_app_set_update_details (app, tmp);
        }
 }
+
+static void
+gs_fwupd_app_finalize (GObject *object)
+{
+       GsFwupdApp *self = GS_FWUPD_APP (object);
+       g_free (self->device_id);
+       g_free (self->update_uri);
+       G_OBJECT_CLASS (gs_fwupd_app_parent_class)->finalize (object);
+}
+
+static void
+gs_fwupd_app_class_init (GsFwupdAppClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       GsAppClass *klass_app = GS_APP_CLASS (klass);
+       klass_app->to_string = gs_fwupd_app_to_string;
+       object_class->finalize = gs_fwupd_app_finalize;
+}
+
+static void
+gs_fwupd_app_init (GsFwupdApp *self)
+{
+}
+
+GsApp *
+gs_fwupd_app_new (const gchar *id)
+{
+       return GS_APP (g_object_new (GS_TYPE_FWUPD_APP, "id", id, NULL));
+}
diff --git a/plugins/fwupd/gs-fwupd-app.h b/plugins/fwupd/gs-fwupd-app.h
index fdb63e52..b898740e 100644
--- a/plugins/fwupd/gs-fwupd-app.h
+++ b/plugins/fwupd/gs-fwupd-app.h
@@ -12,6 +12,11 @@
 
 G_BEGIN_DECLS
 
+#define GS_TYPE_FWUPD_APP (gs_fwupd_app_get_type ())
+
+G_DECLARE_FINAL_TYPE (GsFwupdApp, gs_fwupd_app, GS, FWUPD_APP, GsApp)
+
+GsApp                  *gs_fwupd_app_new                       (const gchar    *id);
 const gchar            *gs_fwupd_app_get_device_id             (GsApp          *app);
 const gchar            *gs_fwupd_app_get_update_uri            (GsApp          *app);
 gboolean                gs_fwupd_app_get_is_locked             (GsApp          *app);
diff --git a/plugins/fwupd/gs-plugin-fwupd.c b/plugins/fwupd/gs-plugin-fwupd.c
index 071471b2..0ec1d367 100644
--- a/plugins/fwupd/gs-plugin-fwupd.c
+++ b/plugins/fwupd/gs-plugin-fwupd.c
@@ -114,6 +114,12 @@ gs_plugin_destroy (GsPlugin *plugin)
        g_object_unref (priv->client);
 }
 
+GsApp *
+gs_plugin_create_app (GsPlugin *plugin, const gchar *id)
+{
+       return gs_fwupd_app_new (id);
+}
+
 void
 gs_plugin_adopt_app (GsPlugin *plugin, GsApp *app)
 {
@@ -219,7 +225,6 @@ gs_plugin_setup (GsPlugin *plugin, GCancellable *cancellable, GError **error)
 
        /* add source */
        priv->cached_origin = gs_app_new (gs_plugin_get_name (plugin));
-       gs_app_set_kind (priv->cached_origin, AS_APP_KIND_SOURCE);
        gs_app_set_bundle_kind (priv->cached_origin, AS_BUNDLE_KIND_CABINET);
 
        /* add the source to the plugin cache which allows us to match the
@@ -266,7 +271,7 @@ gs_plugin_fwupd_new_app_from_device (GsPlugin *plugin, FwupdDevice *dev)
                                       NULL);
        app = gs_plugin_cache_lookup (plugin, id);
        if (app == NULL) {
-               app = gs_app_new (id);
+               app = gs_fwupd_app_new (id);
                gs_plugin_cache_add (plugin, id, app);
        }
 
@@ -315,7 +320,7 @@ gs_plugin_fwupd_new_app_from_device_raw (GsPlugin *plugin, FwupdDevice *device)
 
        /* create a GsApp based on the device, not the release */
        id = gs_plugin_fwupd_build_device_id (device);
-       app = gs_app_new (id);
+       app = gs_fwupd_app_new (id);
        gs_app_set_kind (app, AS_APP_KIND_FIRMWARE);
        gs_app_set_scope (app, AS_APP_SCOPE_SYSTEM);
        gs_app_set_state (app, AS_APP_STATE_INSTALLED);
@@ -1019,7 +1024,7 @@ gs_plugin_add_sources (GsPlugin *plugin,
 
                /* create something that we can use to enable/disable */
                id = g_strdup_printf ("org.fwupd.%s.remote", fwupd_remote_get_id (remote));
-               app = gs_app_new (id);
+               app = gs_fwupd_app_new (id);
                gs_app_set_kind (app, AS_APP_KIND_SOURCE);
                gs_app_set_scope (app, AS_APP_SCOPE_SYSTEM);
                gs_app_set_state (app, fwupd_remote_get_enabled (remote) ?


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