[gnome-software] fwupd: Use a subclassed GsApp object



commit b41e0a23222b93a5903458961134f4dbad69d009
Author: Richard Hughes <richard hughsie com>
Date:   Fri Jul 14 08:12:06 2017 +0100

    fwupd: Use a subclassed GsApp object
    
    This is much cleaner than abusing the metadata dictionary.

 plugins/fwupd/gs-fwupd-app.c    |  125 +++++++++++++++++++++++++++++++++++++++
 plugins/fwupd/gs-fwupd-app.h    |   49 +++++++++++++++
 plugins/fwupd/gs-plugin-fwupd.c |   23 ++++---
 plugins/fwupd/meson.build       |    5 +-
 4 files changed, 191 insertions(+), 11 deletions(-)
---
diff --git a/plugins/fwupd/gs-fwupd-app.c b/plugins/fwupd/gs-fwupd-app.c
new file mode 100644
index 0000000..3ed2697
--- /dev/null
+++ b/plugins/fwupd/gs-fwupd-app.c
@@ -0,0 +1,125 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2017 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+
+#include <string.h>
+
+#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 *fwupd_app = GS_FWUPD_APP (app);
+       gs_utils_append_key_value (str, 20, "fwupd::device-id",
+                                  fwupd_app->device_id);
+       gs_utils_append_key_value (str, 20, "fwupd::update-uri",
+                                  fwupd_app->update_uri);
+       gs_utils_append_key_value (str, 20, "fwupd::is-locked",
+                                  fwupd_app->is_locked ? "yes" : "no");
+}
+
+const gchar *
+gs_fwupd_app_get_device_id (GsApp *app)
+{
+       GsFwupdApp *fwupd_app = GS_FWUPD_APP (app);
+       return fwupd_app->device_id;
+}
+
+const gchar *
+gs_fwupd_app_get_update_uri (GsApp *app)
+{
+       GsFwupdApp *fwupd_app = GS_FWUPD_APP (app);
+       return fwupd_app->update_uri;
+}
+
+gboolean
+gs_fwupd_app_get_is_locked (GsApp *app)
+{
+       GsFwupdApp *fwupd_app = GS_FWUPD_APP (app);
+       return fwupd_app->is_locked;
+}
+
+void
+gs_fwupd_app_set_device_id (GsApp *app, const gchar *device_id)
+{
+       GsFwupdApp *fwupd_app = GS_FWUPD_APP (app);
+       _g_set_str (&fwupd_app->device_id, device_id);
+}
+
+void
+gs_fwupd_app_set_update_uri (GsApp *app, const gchar *update_uri)
+{
+       GsFwupdApp *fwupd_app = GS_FWUPD_APP (app);
+       _g_set_str (&fwupd_app->update_uri, update_uri);
+}
+
+void
+gs_fwupd_app_set_is_locked (GsApp *app, gboolean is_locked)
+{
+       GsFwupdApp *fwupd_app = GS_FWUPD_APP (app);
+       fwupd_app->is_locked = is_locked;
+}
+
+static void
+gs_fwupd_app_finalize (GObject *object)
+{
+       GsFwupdApp *fwupd_app = GS_FWUPD_APP (object);
+       g_free (fwupd_app->device_id);
+       g_free (fwupd_app->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 *fwupd_app)
+{
+}
+
+/* vim: set noexpandtab: */
diff --git a/plugins/fwupd/gs-fwupd-app.h b/plugins/fwupd/gs-fwupd-app.h
new file mode 100644
index 0000000..dc93f77
--- /dev/null
+++ b/plugins/fwupd/gs-fwupd-app.h
@@ -0,0 +1,49 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2017 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef __GS_FWUPD_APP_H
+#define __GS_FWUPD_APP_H
+
+#include <gnome-software.h>
+#include <fwupd.h>
+
+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);
+
+void                    gs_fwupd_app_set_device_id             (GsApp          *app,
+                                                                const gchar    *device_id);
+void                    gs_fwupd_app_set_update_uri            (GsApp          *app,
+                                                                const gchar    *update_uri);
+void                    gs_fwupd_app_set_is_locked             (GsApp          *app,
+                                                                gboolean        is_locked);
+
+G_END_DECLS
+
+#endif /* __GS_FWUPD_APP_H */
+
+/* vim: set noexpandtab: */
diff --git a/plugins/fwupd/gs-plugin-fwupd.c b/plugins/fwupd/gs-plugin-fwupd.c
index 53ea239..9feadba 100644
--- a/plugins/fwupd/gs-plugin-fwupd.c
+++ b/plugins/fwupd/gs-plugin-fwupd.c
@@ -30,6 +30,8 @@
 
 #include <gnome-software.h>
 
+#include "gs-fwupd-app.h"
+
 /*
  * SECTION:
  * Queries for new firmware and schedules it to be installed as required.
@@ -114,6 +116,9 @@ gs_plugin_initialize (GsPlugin *plugin)
                return;
        }
 
+       /* 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");
 }
@@ -351,7 +356,7 @@ gs_plugin_fwupd_new_app_from_results (GsPlugin *plugin, FwupdResult *res)
        id = fwupd_result_get_unique_id (res);
        app = gs_plugin_cache_lookup (plugin, id);
        if (app == NULL) {
-               app = gs_app_new (id);
+               app = gs_plugin_app_new (plugin, id);
                gs_plugin_cache_add (plugin, id, app);
        }
 
@@ -360,8 +365,7 @@ gs_plugin_fwupd_new_app_from_results (GsPlugin *plugin, FwupdResult *res)
        gs_app_add_quirk (app, AS_APP_QUIRK_NOT_LAUNCHABLE);
        gs_app_set_management_plugin (app, "fwupd");
        gs_app_add_category (app, "System");
-       gs_app_set_metadata (app, "fwupd::DeviceID",
-                            fwupd_device_get_id (dev));
+       gs_fwupd_app_set_device_id (app, fwupd_device_get_id (dev));
 
        /* something can be done */
        flags = fwupd_device_get_flags (dev);
@@ -426,8 +430,7 @@ gs_plugin_fwupd_new_app_from_results (GsPlugin *plugin, FwupdResult *res)
        if (fwupd_release_get_uri (rel) != NULL) {
                gs_app_set_origin_hostname (app,
                                            fwupd_release_get_uri (rel));
-               gs_app_set_metadata (app, "fwupd::UpdateURI",
-                                    fwupd_release_get_uri (rel));
+               gs_fwupd_app_set_update_uri (app, fwupd_release_get_uri (rel));
        }
        if (fwupd_device_get_description (dev) != NULL) {
                g_autofree gchar *tmp = NULL;
@@ -690,7 +693,7 @@ gs_plugin_fwupd_add_updates (GsPlugin *plugin,
                        if (!is_downloaded)
                                continue;
                        app = gs_plugin_fwupd_new_app_from_results (plugin, res);
-                       gs_app_set_metadata (app, "fwupd::IsLocked", "");
+                       gs_fwupd_app_set_is_locked (app, TRUE);
                        gs_app_list_add (list, app);
                        continue;
                }
@@ -941,7 +944,7 @@ gs_plugin_fwupd_install (GsPlugin *plugin,
        /* file does not yet exist */
        filename = g_file_get_path (local_file);
        if (!g_file_query_exists (local_file, cancellable)) {
-               const gchar *uri = gs_app_get_metadata_item (app, "fwupd::UpdateURI");
+               const gchar *uri = gs_fwupd_app_get_update_uri (app);
                gs_app_set_state (app, AS_APP_STATE_INSTALLING);
                if (!gs_plugin_download_file (plugin, app, uri, filename,
                                              cancellable, error))
@@ -949,7 +952,7 @@ gs_plugin_fwupd_install (GsPlugin *plugin,
        }
 
        /* limit to single device? */
-       device_id = gs_app_get_metadata_item (app, "fwupd::DeviceID");
+       device_id = gs_fwupd_app_get_device_id (app);
        if (device_id == NULL)
                device_id = FWUPD_DEVICE_ID_ANY;
 
@@ -999,9 +1002,9 @@ gs_plugin_update_app (GsPlugin *plugin,
                return TRUE;
 
        /* locked devices need unlocking, rather than installing */
-       if (gs_app_get_metadata_item (app, "fwupd::IsLocked") != NULL) {
+       if (gs_fwupd_app_get_is_locked (app)) {
                const gchar *device_id;
-               device_id = gs_app_get_metadata_item (app, "fwupd::DeviceID");
+               device_id = gs_fwupd_app_get_device_id (app);
                if (device_id == NULL) {
                        g_set_error_literal (error,
                                             GS_PLUGIN_ERROR,
diff --git a/plugins/fwupd/meson.build b/plugins/fwupd/meson.build
index 40969fc..00c6f73 100644
--- a/plugins/fwupd/meson.build
+++ b/plugins/fwupd/meson.build
@@ -3,7 +3,10 @@ cargs += ['-DLOCALPLUGINDIR="' + meson.current_build_dir() + '"']
 
 shared_module(
   'gs_plugin_fwupd',
-  sources : 'gs-plugin-fwupd.c',
+  sources : [
+    'gs-fwupd-app.c',
+    'gs-plugin-fwupd.c',
+  ],
   include_directories : [
     include_directories('../..'),
     include_directories('../../lib'),


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