[gnome-software/987-removing-layered-local-package-on-silverblue-does-not-notify-the-user-that-it-was-actually: 37/41] gs-common: Introduce gs_utils_invoke_reboot_async()
- From: Milan Crha <mcrha src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-software/987-removing-layered-local-package-on-silverblue-does-not-notify-the-user-that-it-was-actually: 37/41] gs-common: Introduce gs_utils_invoke_reboot_async()
- Date: Wed, 21 Apr 2021 15:56:45 +0000 (UTC)
commit 9b9f3ef6a7fc53fca4aa365df2bb9770e28292b7
Author: Milan Crha <mcrha redhat com>
Date: Wed Apr 21 17:51:57 2021 +0200
gs-common: Introduce gs_utils_invoke_reboot_async()
It's used on multiple places, thus avoid code duplication.
src/gs-application.c | 44 +++--------------------------------------
src/gs-common.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++
src/gs-common.h | 3 +++
3 files changed, 61 insertions(+), 41 deletions(-)
---
diff --git a/src/gs-application.c b/src/gs-application.c
index ffe984500..5d58e9431 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"
@@ -427,42 +428,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_async (NULL, reboot_failed_cb, app);
}
static void
@@ -470,17 +442,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_async (NULL, NULL, NULL);
}
static void
diff --git a/src/gs-common.c b/src/gs-common.c
index eea004573..5732e2b63 100644
--- a/src/gs-common.c
+++ b/src/gs-common.c
@@ -765,3 +765,58 @@ 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_async:
+ * @cancellable: (nullable): a %GCancellable for the call, or %NULL
+ * @ready_callback: (nullable): a callback to be called after the call is finished, or %NULL
+ * @user_data: user data for the @ready_callback
+ *
+ * Asynchronously 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.
+ *
+ * When the @ready_callback is %NULL, a default callback is used, which shows
+ * a runtime warning (using g_warning) on the console when the call fails.
+ *
+ * Since: 41
+ **/
+void
+gs_utils_invoke_reboot_async (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..2090e0b31 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_async (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]