[gnome-software/wip/hughsie/GsApp-custom: 2/3] fwupd: Use custom GsApp GTypes for results



commit 19eeb8fde2a04f52cad9442038467e07404e9740
Author: Richard Hughes <richard hughsie com>
Date:   Mon Feb 11 11:39:52 2019 +0000

    fwupd: Use custom GsApp GTypes for results

 plugins/fwupd/gs-fwupd-app.c    | 81 ++++++++++++++++++++++++++++++++++++-----
 plugins/fwupd/gs-fwupd-app.h    |  4 ++
 plugins/fwupd/gs-plugin-fwupd.c |  3 ++
 3 files changed, 78 insertions(+), 10 deletions(-)
---
diff --git a/plugins/fwupd/gs-fwupd-app.c b/plugins/fwupd/gs-fwupd-app.c
index 6c9d50a2..9e5b2283 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,26 @@ 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)
+{
+}
diff --git a/plugins/fwupd/gs-fwupd-app.h b/plugins/fwupd/gs-fwupd-app.h
index fdb63e52..c9745b4b 100644
--- a/plugins/fwupd/gs-fwupd-app.h
+++ b/plugins/fwupd/gs-fwupd-app.h
@@ -12,6 +12,10 @@
 
 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)
+
 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 914d8a55..88a3cbde 100644
--- a/plugins/fwupd/gs-plugin-fwupd.c
+++ b/plugins/fwupd/gs-plugin-fwupd.c
@@ -97,6 +97,9 @@ gs_plugin_initialize (GsPlugin *plugin)
                                             SOUP_TYPE_CONTENT_DECODER);
        gs_plugin_set_soup_session (plugin, soup_session);
 
+       /* unique to us */
+       gs_plugin_set_app_gtype (plugin, GS_TYPE_FWUPD_APP);
+
        /* set name of MetaInfo file */
        gs_plugin_set_appstream_id (plugin, "org.gnome.Software.Plugin.Fwupd");
 }


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