[gnome-software/135-restart-and-install-update-button-does-not-trigger-reboot-when-using-lxde] gs-common: Try different methods when invoking reboot



commit 78b5f3e6abd10bbf1fb9d4b5823785ba23321ad0
Author: Milan Crha <mcrha redhat com>
Date:   Tue Jun 21 10:55:02 2022 +0200

    gs-common: Try different methods when invoking reboot
    
    Different desktop environments use different methods to reboot
    the machine. Use a common D-Bus method and then try the one
    suitable for the current desktop environment.
    
    Closes https://gitlab.gnome.org/GNOME/gnome-software/-/issues/135

 src/gs-common.c | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 172 insertions(+), 11 deletions(-)
---
diff --git a/src/gs-common.c b/src/gs-common.c
index 62100947b..b2eca4de1 100644
--- a/src/gs-common.c
+++ b/src/gs-common.c
@@ -911,9 +911,9 @@ gs_utils_reboot_call_done_cb (GObject *source,
 }
 
 static void
-gs_utils_invoke_reboot_ready_cb (GObject *source_object,
-                                GAsyncResult *result,
-                                gpointer user_data)
+gs_utils_invoke_reboot_ready3_cb (GObject *source_object,
+                                 GAsyncResult *result,
+                                 gpointer user_data)
 {
        g_autoptr(GTask) task = user_data;
        g_autoptr(GVariant) ret_val = NULL;
@@ -923,11 +923,171 @@ gs_utils_invoke_reboot_ready_cb (GObject *source_object,
        if (ret_val != NULL) {
                g_task_return_boolean (task, TRUE);
        } else {
+               g_dbus_error_strip_remote_error (error);
                g_prefix_error (&error, "Failed to call %s:", (const gchar *) g_task_get_task_data (task));
                g_task_return_error (task, g_steal_pointer (&error));
        }
 }
 
+static void
+gs_utils_invoke_reboot_ready2_cb (GObject *source_object,
+                                 GAsyncResult *result,
+                                 gpointer user_data)
+{
+       g_autoptr(GTask) task = user_data;
+       g_autoptr(GVariant) ret_val = NULL;
+       g_autoptr(GError) error = NULL;
+
+       ret_val = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), result, &error);
+       if (ret_val != NULL) {
+               g_task_return_boolean (task, TRUE);
+       } else {
+               g_autoptr(GDBusConnection) bus = NULL;
+               g_autoptr(GCancellable) cancellable = NULL;
+
+               g_dbus_error_strip_remote_error (error);
+               g_prefix_error (&error, "Failed to call %s:", (const gchar *) g_task_get_task_data (task));
+
+               if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
+                       g_task_return_error (task, g_steal_pointer (&error));
+                       return;
+               }
+
+               g_debug ("%s", error->message);
+               g_clear_error (&error);
+
+               if (g_task_get_cancellable (task) != NULL)
+                       cancellable = g_object_ref (g_task_get_cancellable (task));
+
+               bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, &error);
+               if (bus == NULL) {
+                       g_prefix_error (&error, "%s", "Failed to get D-Bus session bus:");
+                       g_task_return_error (task, g_steal_pointer (&error));
+                       return;
+               }
+
+               g_task_set_task_data (task, (gpointer) "org.gnome.SessionManager.Reboot", 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, cancellable,
+                                       gs_utils_invoke_reboot_ready3_cb,
+                                       g_steal_pointer (&task));
+       }
+}
+
+static void
+gs_utils_invoke_reboot_ready1_cb (GObject *source_object,
+                                 GAsyncResult *result,
+                                 gpointer user_data)
+{
+       g_autoptr(GTask) task = user_data;
+       g_autoptr(GVariant) ret_val = NULL;
+       g_autoptr(GError) error = NULL;
+
+       ret_val = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), result, &error);
+       if (ret_val != NULL) {
+               g_task_return_boolean (task, TRUE);
+       } else {
+               g_autoptr(GDBusConnection) bus = NULL;
+               g_autoptr(GCancellable) cancellable = NULL;
+               const gchar *xdg_desktop;
+               gboolean call_session_manager = FALSE;
+
+               g_dbus_error_strip_remote_error (error);
+               g_prefix_error (&error, "Failed to call %s:", (const gchar *) g_task_get_task_data (task));
+
+               if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
+                       g_task_return_error (task, g_steal_pointer (&error));
+                       return;
+               }
+
+               g_debug ("%s", error->message);
+               g_clear_error (&error);
+
+               if (g_task_get_cancellable (task) != NULL)
+                       cancellable = g_object_ref (g_task_get_cancellable (task));
+
+               bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, &error);
+               if (bus == NULL) {
+                       g_prefix_error (&error, "%s", "Failed to get D-Bus session bus:");
+                       g_task_return_error (task, g_steal_pointer (&error));
+                       return;
+               }
+
+               xdg_desktop = g_getenv ("XDG_CURRENT_DESKTOP");
+               if (xdg_desktop != NULL) {
+                       if (strstr (xdg_desktop, "KDE")) {
+                               g_task_set_task_data (task, (gpointer) "org.kde.Shutdown.logoutAndReboot", 
NULL);
+                               g_dbus_connection_call (bus,
+                                                       "org.kde.Shutdown",
+                                                       "/Shutdown",
+                                                       "org.kde.Shutdown",
+                                                       "logoutAndReboot",
+                                                       NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
+                                                       G_MAXINT, cancellable,
+                                                       gs_utils_invoke_reboot_ready2_cb,
+                                                       g_steal_pointer (&task));
+                       } else if (strstr (xdg_desktop, "LXDE")) {
+                               g_task_set_task_data (task, (gpointer) 
"org.lxde.SessionManager.RequestReboot", NULL);
+                               g_dbus_connection_call (bus,
+                                                       "org.lxde.SessionManager",
+                                                       "/org/lxde/SessionManager",
+                                                       "org.lxde.SessionManager",
+                                                       "RequestReboot",
+                                                       NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
+                                                       G_MAXINT, cancellable,
+                                                       gs_utils_invoke_reboot_ready2_cb,
+                                                       g_steal_pointer (&task));
+                       } else if (strstr (xdg_desktop, "MATE")) {
+                               g_task_set_task_data (task, (gpointer) 
"org.gnome.SessionManager.RequestReboot", NULL);
+                               g_dbus_connection_call (bus,
+                                                       "org.gnome.SessionManager",
+                                                       "/org/gnome/SessionManager",
+                                                       "org.gnome.SessionManager",
+                                                       "RequestReboot",
+                                                       NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
+                                                       G_MAXINT, cancellable,
+                                                       gs_utils_invoke_reboot_ready2_cb,
+                                                       g_steal_pointer (&task));
+                       } else if (strstr (xdg_desktop, "XFCE")) {
+                               g_task_set_task_data (task, (gpointer) "org.xfce.Session.Manager.Restart", 
NULL);
+                               g_dbus_connection_call (bus,
+                                                       "org.xfce.SessionManager",
+                                                       "/org/xfce/SessionManager",
+                                                       "org.xfce.Session.Manager",
+                                                       "Restart",
+                                                       g_variant_new ("(b)", TRUE), /* allow_save */
+                                                       NULL, G_DBUS_CALL_FLAGS_NONE,
+                                                       G_MAXINT, cancellable,
+                                                       gs_utils_invoke_reboot_ready2_cb,
+                                                       g_steal_pointer (&task));
+                       } else {
+                               /* Let the "GNOME" and "X-Cinnamon" be the default */
+                               call_session_manager = TRUE;
+                       }
+               } else {
+                       call_session_manager = TRUE;
+               }
+
+               if (call_session_manager) {
+                       g_task_set_task_data (task, (gpointer) "org.gnome.SessionManager.Reboot", 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, cancellable,
+                                               gs_utils_invoke_reboot_ready3_cb,
+                                               g_steal_pointer (&task));
+               }
+       }
+}
+
 /**
  * gs_utils_invoke_reboot_async:
  * @cancellable: (nullable): a %GCancellable for the call, or %NULL
@@ -956,23 +1116,24 @@ gs_utils_invoke_reboot_async (GCancellable *cancellable,
 
        task = g_task_new (NULL, cancellable, ready_callback, user_data);
        g_task_set_source_tag (task, gs_utils_invoke_reboot_async);
-       g_task_set_task_data (task, (gpointer) "org.gnome.SessionManager.Reboot", NULL);
+       g_task_set_task_data (task, (gpointer) "org.freedesktop.login1.Manager.Reboot", NULL);
 
-       bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, &error);
+       bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, &error);
        if (bus == NULL) {
-               g_prefix_error (&error, "%s", "Failed to get D-Bus session bus:");
+               g_prefix_error (&error, "%s", "Failed to get D-Bus system bus:");
                g_task_return_error (task, g_steal_pointer (&error));
                return;
        }
 
        g_dbus_connection_call (bus,
-                               "org.gnome.SessionManager",
-                               "/org/gnome/SessionManager",
-                               "org.gnome.SessionManager",
+                               "org.freedesktop.login1",
+                               "/org/freedesktop/login1",
+                               "org.freedesktop.login1.Manager",
                                "Reboot",
-                               NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
+                               g_variant_new ("(b)", TRUE), /* interactive */
+                               NULL, G_DBUS_CALL_FLAGS_NONE,
                                G_MAXINT, cancellable,
-                               gs_utils_invoke_reboot_ready_cb,
+                               gs_utils_invoke_reboot_ready1_cb,
                                g_steal_pointer (&task));
 }
 


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