[gnome-software/987-removing-layered-local-package-on-silverblue-does-not-notify-the-user-that-it-was-actually] gs-common: Introduce gs_utils_invoke_reboot()



commit 40d0187d1d93fe9ba3cb7ebbfc7e6e495c207269
Author: Milan Crha <mcrha redhat com>
Date:   Fri Apr 16 11:46:50 2021 +0200

    gs-common: Introduce gs_utils_invoke_reboot()
    
    It's used on multiple places, thus avoid code duplication.

 src/gs-application.c | 44 +++-----------------------------------------
 src/gs-common.c      | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gs-common.h      |  3 +++
 3 files changed, 58 insertions(+), 41 deletions(-)
---
diff --git a/src/gs-application.c b/src/gs-application.c
index a3a1745ac..4471a7e63 100644
--- a/src/gs-application.c
+++ b/src/gs-application.c
@@ -31,6 +31,7 @@
 #endif
 
 #include "gs-build-ident.h"
+#include "gs-common.h"
 #include "gs-debug.h"
 #include "gs-first-run-dialog.h"
 #include "gs-shell.h"
@@ -414,42 +415,13 @@ offline_update_cb (GsPluginLoader *plugin_loader,
                   GAsyncResult *res,
                   GsApplication *app)
 {
-       g_autoptr(GDBusConnection) bus = NULL;
        g_autoptr(GError) error = NULL;
        if (!gs_plugin_loader_job_action_finish (plugin_loader, res, &error)) {
                g_warning ("Failed to trigger offline update: %s", error->message);
                return;
        }
 
-       /* trigger reboot */
-       bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
-       g_dbus_connection_call (bus,
-                               "org.gnome.SessionManager",
-                               "/org/gnome/SessionManager",
-                               "org.gnome.SessionManager",
-                               "Reboot",
-                               NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
-                               G_MAXINT, NULL,
-                               reboot_failed_cb,
-                               app);
-}
-
-static void
-gs_application_reboot_failed_cb (GObject *source,
-                                GAsyncResult *res,
-                                gpointer user_data)
-{
-       g_autoptr(GError) error = NULL;
-       g_autoptr(GVariant) retval = NULL;
-
-       /* get result */
-       retval = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), res, &error);
-       if (retval != NULL)
-               return;
-       if (error != NULL) {
-               g_warning ("Calling org.gnome.SessionManager.Reboot failed: %s",
-                          error->message);
-       }
+       gs_utils_invoke_reboot (NULL, reboot_failed_cb, app);
 }
 
 static void
@@ -457,17 +429,7 @@ reboot_activated (GSimpleAction *action,
                   GVariant      *parameter,
                   gpointer       data)
 {
-       g_autoptr(GDBusConnection) bus = NULL;
-       bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
-       g_dbus_connection_call (bus,
-                               "org.gnome.SessionManager",
-                               "/org/gnome/SessionManager",
-                               "org.gnome.SessionManager",
-                               "Reboot",
-                               NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
-                               G_MAXINT, NULL,
-                               gs_application_reboot_failed_cb,
-                               NULL);
+       gs_utils_invoke_reboot (NULL, NULL, NULL);
 }
 
 static void
diff --git a/src/gs-common.c b/src/gs-common.c
index eea004573..86a86c091 100644
--- a/src/gs-common.c
+++ b/src/gs-common.c
@@ -765,3 +765,55 @@ gs_utils_time_to_string (gint64 unix_time_seconds)
                                                  "%d years ago", years_ago),
                                        years_ago);
 }
+
+static void
+gs_utils_reboot_call_done_cb (GObject *source,
+                             GAsyncResult *res,
+                             gpointer user_data)
+{
+       g_autoptr(GError) error = NULL;
+       g_autoptr(GVariant) retval = NULL;
+
+       /* get result */
+       retval = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), res, &error);
+       if (retval != NULL)
+               return;
+       if (error != NULL) {
+               g_warning ("Calling org.gnome.SessionManager.Reboot failed: %s",
+                          error->message);
+       }
+}
+
+/**
+ * gs_utils_invoke_reboot:
+ * @cancellable: (nullable): a %GCancellable for the call, or %NULL
+ * @ready_callback: a callback to be called after the call is finished
+ * @user_data: user data for the @ready_callback
+ *
+ * Invokes a reboot request using D-Bus. The @ready_callback should
+ * use g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error);
+ * to get the result of the operation.
+ *
+ * Since: 41
+ **/
+void
+gs_utils_invoke_reboot (GCancellable *cancellable,
+                       GAsyncReadyCallback ready_callback,
+                       gpointer user_data)
+{
+       g_autoptr(GDBusConnection) bus = NULL;
+       bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
+
+       if (!ready_callback)
+               ready_callback = gs_utils_reboot_call_done_cb;
+
+       g_dbus_connection_call (bus,
+                               "org.gnome.SessionManager",
+                               "/org/gnome/SessionManager",
+                               "org.gnome.SessionManager",
+                               "Reboot",
+                               NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
+                               G_MAXINT, cancellable,
+                               ready_callback,
+                               user_data);
+}
diff --git a/src/gs-common.h b/src/gs-common.h
index 068e36086..b49cba5f8 100644
--- a/src/gs-common.h
+++ b/src/gs-common.h
@@ -48,5 +48,8 @@ gboolean       gs_utils_list_has_component_fuzzy      (GsAppList      *list,
                                                 GsApp          *app);
 void            gs_utils_reboot_notify         (GsAppList      *list);
 gchar          *gs_utils_time_to_string        (gint64          unix_time_seconds);
+void            gs_utils_invoke_reboot         (GCancellable   *cancellable,
+                                                GAsyncReadyCallback ready_callback,
+                                                gpointer        user_data);
 
 G_END_DECLS


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