[gnome-software: 3/5] gs-plugin-event: Allow specifying properties at construction time




commit 8872ae8ade442bd5344c4d0fbe3a58ada42b8791
Author: Philip Withnall <pwithnall endlessos org>
Date:   Mon Jan 31 18:36:50 2022 +0000

    gs-plugin-event: Allow specifying properties at construction time
    
    Since the properties are construct-only, they should be specified at
    construction time. This fixes the anti-pattern of creating event objects
    and then calling multiple setters on them.
    
    This introduces no functional changes.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 lib/gs-odrs-provider.c                      | 10 ++++---
 lib/gs-plugin-event.c                       | 21 +++++++++++---
 lib/gs-plugin-event.h                       |  3 +-
 lib/gs-plugin-loader.c                      | 43 +++++++++++++++--------------
 plugins/eos-updater/gs-plugin-eos-updater.c | 22 +++++++++------
 plugins/flatpak/gs-flatpak.c                | 17 +++++++-----
 plugins/flatpak/gs-plugin-flatpak.c         | 30 ++++++++++++--------
 7 files changed, 90 insertions(+), 56 deletions(-)
---
diff --git a/lib/gs-odrs-provider.c b/lib/gs-odrs-provider.c
index 7d12722bb..c2587c512 100644
--- a/lib/gs-odrs-provider.c
+++ b/lib/gs-odrs-provider.c
@@ -1308,11 +1308,13 @@ gs_odrs_provider_refresh (GsOdrsProvider  *self,
                                    /* TRANSLATORS: status text when downloading */
                                    _("Downloading application ratings…"));
        if (!gs_plugin_download_file (plugin, app_dl, uri, cache_filename, cancellable, &error_local)) {
-               g_autoptr(GsPluginEvent) event = gs_plugin_event_new ();
+               g_autoptr(GsPluginEvent) event = NULL;
+
+               event = gs_plugin_event_new ("error", error_local,
+                                            "action", GS_PLUGIN_ACTION_DOWNLOAD,
+                                            "origin", self->cached_origin,
+                                            NULL);
 
-               gs_plugin_event_set_error (event, error_local);
-               gs_plugin_event_set_action (event, GS_PLUGIN_ACTION_DOWNLOAD);
-               gs_plugin_event_set_origin (event, self->cached_origin);
                if (gs_plugin_has_flags (plugin, GS_PLUGIN_FLAGS_INTERACTIVE))
                        gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
                else
diff --git a/lib/gs-plugin-event.c b/lib/gs-plugin-event.c
index 82fe543be..88d7b283d 100644
--- a/lib/gs-plugin-event.c
+++ b/lib/gs-plugin-event.c
@@ -24,6 +24,7 @@
 
 #include <glib.h>
 
+#include "gs-enums.h"
 #include "gs-plugin-private.h"
 #include "gs-plugin-event.h"
 #include "gs-utils.h"
@@ -447,17 +448,29 @@ gs_plugin_event_init (GsPluginEvent *event)
 
 /**
  * gs_plugin_event_new:
+ * @first_property_name: the name of the first property
+ * @...: the value of the first property, followed by zero or more pairs of
+ *   property name/value pairs, then %NULL
  *
  * Creates a new event.
  *
- * Returns: A newly allocated #GsPluginEvent
+ * The arguments are as for g_object_new(): property name/value pairs to set
+ * the properties of the event.
  *
- * Since: 3.22
+ * Returns: (transfer full): A newly allocated #GsPluginEvent
+ *
+ * Since: 42
  **/
 GsPluginEvent *
-gs_plugin_event_new (void)
+gs_plugin_event_new (const gchar *first_property_name,
+                    ...)
 {
        GsPluginEvent *event;
-       event = g_object_new (GS_TYPE_PLUGIN_EVENT, NULL);
+       va_list args;
+
+       va_start (args, first_property_name);
+       event = GS_PLUGIN_EVENT (g_object_new_valist (GS_TYPE_PLUGIN_EVENT, first_property_name, args));
+       va_end (args);
+
        return GS_PLUGIN_EVENT (event);
 }
diff --git a/lib/gs-plugin-event.h b/lib/gs-plugin-event.h
index 090c5b128..bdad7b9eb 100644
--- a/lib/gs-plugin-event.h
+++ b/lib/gs-plugin-event.h
@@ -38,7 +38,8 @@ typedef enum {
        GS_PLUGIN_EVENT_FLAG_LAST  /*< skip >*/
 } GsPluginEventFlag;
 
-GsPluginEvent          *gs_plugin_event_new            (void);
+GsPluginEvent          *gs_plugin_event_new            (const gchar            *first_property_name,
+                                                        ...) G_GNUC_NULL_TERMINATED;
 
 const gchar            *gs_plugin_event_get_unique_id  (GsPluginEvent          *event);
 
diff --git a/lib/gs-plugin-loader.c b/lib/gs-plugin-loader.c
index c14006a15..1ac61383f 100644
--- a/lib/gs-plugin-loader.c
+++ b/lib/gs-plugin-loader.c
@@ -346,6 +346,8 @@ gs_plugin_loader_claim_error (GsPluginLoader *plugin_loader,
        g_autofree gchar *app_id = NULL;
        g_autofree gchar *origin_id = NULL;
        g_autoptr(GsPluginEvent) event = NULL;
+       g_autoptr(GsApp) event_app = NULL;
+       g_autoptr(GsApp) event_origin = NULL;
 
        g_return_if_fail (GS_IS_PLUGIN_LOADER (plugin_loader));
        g_return_if_fail (error != NULL);
@@ -374,22 +376,15 @@ gs_plugin_loader_claim_error (GsPluginLoader *plugin_loader,
                error_copy->code = GS_PLUGIN_ERROR_FAILED;
        }
 
-       /* create event which is handled by the GsShell */
-       event = gs_plugin_event_new ();
-       gs_plugin_event_set_error (event, error_copy);
-       gs_plugin_event_set_action (event, action);
-       if (app != NULL)
-               gs_plugin_event_set_app (event, app);
-       if (interactive)
-               gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
-       gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);
-
        /* set the app and origin IDs if we managed to scrape them from the error above */
+       event_app = g_object_ref (app);
+       event_origin = NULL;
+
        if (plugin != NULL && as_utils_data_id_valid (app_id)) {
                g_autoptr(GsApp) cached_app = gs_plugin_cache_lookup (plugin, app_id);
                if (cached_app != NULL) {
                        g_debug ("found app %s in error", app_id);
-                       gs_plugin_event_set_app (event, cached_app);
+                       g_set_object (&event_app, cached_app);
                } else {
                        g_debug ("no unique ID found for app %s", app_id);
                }
@@ -398,12 +393,22 @@ gs_plugin_loader_claim_error (GsPluginLoader *plugin_loader,
                g_autoptr(GsApp) origin = gs_plugin_cache_lookup (plugin, origin_id);
                if (origin != NULL) {
                        g_debug ("found origin %s in error", origin_id);
-                       gs_plugin_event_set_origin (event, origin);
+                       g_set_object (&event_origin, origin);
                } else {
                        g_debug ("no unique ID found for origin %s", origin_id);
                }
        }
 
+       /* create event which is handled by the GsShell */
+       event = gs_plugin_event_new ("error", error_copy,
+                                    "action", action,
+                                    "app", event_app,
+                                    "origin", event_origin,
+                                    NULL);
+       if (interactive)
+               gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
+       gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);
+
        /* add event to the queue */
        gs_plugin_loader_add_event (plugin_loader, event);
 }
@@ -2012,22 +2017,20 @@ gs_plugin_loader_software_app_created_cb (GObject *source_object,
 {
        GsPluginLoader *plugin_loader = GS_PLUGIN_LOADER (source_object);
        g_autoptr(GsApp) app = NULL;
-       g_autoptr(GsPluginEvent) event = gs_plugin_event_new ();
+       g_autoptr(GsPluginEvent) event = NULL;
        g_autoptr(GError) error = NULL;
 
        app = gs_plugin_loader_app_create_finish (plugin_loader, result, NULL);
 
-       /* add app */
-       gs_plugin_event_set_action (event, GS_PLUGIN_ACTION_UNKNOWN);
-       if (app != NULL)
-               gs_plugin_event_set_app (event, app);
-
-       /* add error */
        g_set_error_literal (&error,
                             GS_PLUGIN_ERROR,
                             GS_PLUGIN_ERROR_RESTART_REQUIRED,
                             "A restart is required");
-       gs_plugin_event_set_error (event, error);
+       event = gs_plugin_event_new ("action", GS_PLUGIN_ACTION_UNKNOWN,
+                                    "app", app,
+                                    "error", error,
+                                    NULL);
+
        gs_plugin_loader_add_event (plugin_loader, event);
 }
 
diff --git a/plugins/eos-updater/gs-plugin-eos-updater.c b/plugins/eos-updater/gs-plugin-eos-updater.c
index 88c23fa72..d182e376c 100644
--- a/plugins/eos-updater/gs-plugin-eos-updater.c
+++ b/plugins/eos-updater/gs-plugin-eos-updater.c
@@ -862,14 +862,17 @@ gs_plugin_app_upgrade_download (GsPlugin *plugin,
                        } else {
                                /* Display an error to the user. */
                                g_autoptr(GError) error_local = NULL;
-                               g_autoptr(GsPluginEvent) event = gs_plugin_event_new ();
+                               g_autoptr(GsPluginEvent) event = NULL;
+
                                g_set_error_literal (&error_local, GS_PLUGIN_ERROR,
                                                     GS_PLUGIN_ERROR_FAILED,
                                                     _("EOS update service could not fetch and apply the 
update."));
                                gs_eos_updater_error_convert (&error_local);
-                               gs_plugin_event_set_app (event, app);
-                               gs_plugin_event_set_action (event, GS_PLUGIN_ACTION_UPGRADE_DOWNLOAD);
-                               gs_plugin_event_set_error (event, error_local);
+
+                               event = gs_plugin_event_new ("app", app,
+                                                            "action", GS_PLUGIN_ACTION_UPGRADE_DOWNLOAD,
+                                                            "error", error_local,
+                                                            NULL);
                                gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);
                                gs_plugin_report_event (plugin, event);
 
@@ -936,11 +939,14 @@ gs_plugin_app_upgrade_download (GsPlugin *plugin,
                        /* Display an error to the user, unless they cancelled
                         * the download. */
                        if (!eos_updater_error_is_cancelled (error_name)) {
-                               g_autoptr(GsPluginEvent) event = gs_plugin_event_new ();
+                               g_autoptr(GsPluginEvent) event = NULL;
+
                                gs_eos_updater_error_convert (&error_local);
-                               gs_plugin_event_set_app (event, app);
-                               gs_plugin_event_set_action (event, GS_PLUGIN_ACTION_UPGRADE_DOWNLOAD);
-                               gs_plugin_event_set_error (event, error_local);
+
+                               event = gs_plugin_event_new ("app", app,
+                                                            "action", GS_PLUGIN_ACTION_UPGRADE_DOWNLOAD,
+                                                            "error", error_local,
+                                                            NULL);
                                gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);
                                gs_plugin_report_event (plugin, event);
                        }
diff --git a/plugins/flatpak/gs-flatpak.c b/plugins/flatpak/gs-flatpak.c
index 8db5bcf83..a3f3b003e 100644
--- a/plugins/flatpak/gs-flatpak.c
+++ b/plugins/flatpak/gs-flatpak.c
@@ -1259,9 +1259,9 @@ gs_flatpak_refresh_appstream (GsFlatpak *self, guint cache_age,
 
                        /* allow the plugin loader to decide if this should be
                         * shown the user, possibly only for interactive jobs */
-                       event = gs_plugin_event_new ();
                        gs_flatpak_error_convert (&error_local);
-                       gs_plugin_event_set_error (event, error_local);
+                       event = gs_plugin_event_new ("error", error_local,
+                                                    NULL);
                        gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);
                        gs_plugin_report_event (self->plugin, event);
                        continue;
@@ -3212,8 +3212,8 @@ gs_flatpak_refine_addons (GsFlatpak *self,
                g_autoptr(GError) error_local = g_error_new_literal (GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_FAILED,
                        errors->str);
 
-               event = gs_plugin_event_new ();
-               gs_plugin_event_set_error (event, error_local);
+               event = gs_plugin_event_new ("error", error_local,
+                                            NULL);
                gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);
                gs_plugin_report_event (self->plugin, event);
        }
@@ -3738,10 +3738,13 @@ gs_flatpak_file_to_app_ref (GsFlatpak *self,
        /* get the new appstream data (nonfatal for failure) */
        if (!gs_flatpak_refresh_appstream_remote (self, remote_name,
                                                  cancellable, &error_local)) {
-               g_autoptr(GsPluginEvent) event = gs_plugin_event_new ();
+               g_autoptr(GsPluginEvent) event = NULL;
+
                gs_flatpak_error_convert (&error_local);
-               gs_plugin_event_set_app (event, app);
-               gs_plugin_event_set_error (event, error_local);
+
+               event = gs_plugin_event_new ("app", app,
+                                            "error", error_local,
+                                            NULL);
                gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);
                gs_plugin_report_event (self->plugin, event);
                g_clear_error (&error_local);
diff --git a/plugins/flatpak/gs-plugin-flatpak.c b/plugins/flatpak/gs-plugin-flatpak.c
index 99dae01b6..fadd3be3b 100644
--- a/plugins/flatpak/gs-plugin-flatpak.c
+++ b/plugins/flatpak/gs-plugin-flatpak.c
@@ -144,11 +144,13 @@ static void
 gs_plugin_flatpak_report_warning (GsPlugin *plugin,
                                  GError **error)
 {
-       g_autoptr(GsPluginEvent) event = gs_plugin_event_new ();
+       g_autoptr(GsPluginEvent) event = NULL;
        g_assert (error != NULL);
        if (*error != NULL && (*error)->domain != GS_PLUGIN_ERROR)
                gs_flatpak_error_convert (error);
-       gs_plugin_event_set_error (event, *error);
+
+       event = gs_plugin_event_new ("error", *error,
+                                    NULL);
        gs_plugin_event_add_flag (event,
                                  GS_PLUGIN_EVENT_FLAG_WARNING);
        gs_plugin_report_event (plugin, event);
@@ -820,9 +822,10 @@ _webflow_start (FlatpakTransaction *transaction,
 
                        g_warning ("Failed to start browser %s: %s", browser, error_local->message);
 
-                       event = gs_plugin_event_new ();
                        gs_flatpak_error_convert (&error_local);
-                       gs_plugin_event_set_error (event, error_local);
+
+                       event = gs_plugin_event_new ("error", error_local,
+                                                    NULL);
                        gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);
                        gs_plugin_report_event (plugin, event);
 
@@ -834,9 +837,10 @@ _webflow_start (FlatpakTransaction *transaction,
 
                        g_warning ("Failed to show url: %s", error_local->message);
 
-                       event = gs_plugin_event_new ();
                        gs_flatpak_error_convert (&error_local);
-                       gs_plugin_event_set_error (event, error_local);
+
+                       event = gs_plugin_event_new ("error", error_local,
+                                                    NULL);
                        gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);
                        gs_plugin_report_event (plugin, event);
 
@@ -965,9 +969,10 @@ gs_plugin_download (GsPlugin *plugin, GsAppList *list,
 
                                g_warning ("Skipping update for ‘%s’: %s", ref, error_local->message);
 
-                               event = gs_plugin_event_new ();
                                gs_flatpak_error_convert (&error_local);
-                               gs_plugin_event_set_error (event, error_local);
+
+                               event = gs_plugin_event_new ("error", error_local,
+                                                            NULL);
                                gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);
                                gs_plugin_report_event (plugin, event);
                        } else {
@@ -1052,8 +1057,8 @@ gs_flatpak_cover_addons_in_transaction (GsPlugin *plugin,
                g_autoptr(GError) error_local = g_error_new_literal (GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_FAILED,
                        errors->str);
 
-               event = gs_plugin_event_new ();
-               gs_plugin_event_set_error (event, error_local);
+               event = gs_plugin_event_new ("error", error_local,
+                                            NULL);
                gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);
                gs_plugin_report_event (plugin, event);
        }
@@ -1371,9 +1376,10 @@ gs_plugin_flatpak_update (GsPlugin *plugin,
 
                        g_warning ("Skipping update for ‘%s’: %s", ref, error_local->message);
 
-                       event = gs_plugin_event_new ();
                        gs_flatpak_error_convert (&error_local);
-                       gs_plugin_event_set_error (event, error_local);
+
+                       event = gs_plugin_event_new ("error", error_local,
+                                                    NULL);
                        gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);
                        gs_plugin_report_event (plugin, event);
                } else {


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