gdm r5624 - in trunk: . gui/simple-greeter



Author: mccann
Date: Wed Jan 23 15:36:16 2008
New Revision: 5624
URL: http://svn.gnome.org/viewvc/gdm?rev=5624&view=rev

Log:
2008-01-23  William Jon McCann  <mccann jhu edu>

	* configure.ac:
	* gui/simple-greeter/gdm-greeter-login-window.c: (try_system_stop),
	(try_system_restart), (system_restart_auth_cb),
	(system_stop_auth_cb), (do_system_restart), (do_system_stop),
	(restart_button_clicked), (shutdown_button_clicked), (load_theme):
	Initial support for CK/PK system restart/stop functionality.
	We still need to use a different action when multiple users are
	logged in.



Modified:
   trunk/ChangeLog
   trunk/configure.ac
   trunk/gui/simple-greeter/gdm-greeter-login-window.c

Modified: trunk/configure.ac
==============================================================================
--- trunk/configure.ac	(original)
+++ trunk/configure.ac	Wed Jan 23 15:36:16 2008
@@ -47,6 +47,7 @@
 LIBGLADE_REQUIRED_VERSION=1.99.2
 SCROLLKEEPER_REQUIRED_VERSION=0.1.4
 GCONF_REQUIRED_VERSION=2.6.1
+POLICYKIT_REQUIRED_VERSION=0.7
 
 EXTRA_COMPILE_WARNINGS(yes)
 
@@ -86,6 +87,7 @@
         gtk+-2.0 >= $GTK_REQUIRED_VERSION
         libglade-2.0 >= $LIBGLADE_REQUIRED_VERSION
         gnome-vfs-2.0 >= $GNOME_VFS_REQUIRED_VERSION
+        polkit-gnome >= $POLICYKIT_REQUIRED_VERSION
 )
 AC_SUBST(SIMPLE_GREETER_CFLAGS)
 AC_SUBST(SIMPLE_GREETER_LIBS)

Modified: trunk/gui/simple-greeter/gdm-greeter-login-window.c
==============================================================================
--- trunk/gui/simple-greeter/gdm-greeter-login-window.c	(original)
+++ trunk/gui/simple-greeter/gdm-greeter-login-window.c	Wed Jan 23 15:36:16 2008
@@ -44,6 +44,12 @@
 
 #include <glade/glade-xml.h>
 
+#define DBUS_API_SUBJECT_TO_CHANGE
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+
+#include <polkit-gnome/polkit-gnome.h>
+
 #include "gdm-greeter-login-window.h"
 #include "gdm-user-chooser-widget.h"
 #include "gdm-session-chooser-widget.h"
@@ -55,6 +61,15 @@
 #define PW_ENTRY_SIZE GDM_MAX_PASS
 #endif
 
+#define CK_NAME      "org.freedesktop.ConsoleKit"
+#define CK_PATH      "/org/freedesktop/ConsoleKit"
+#define CK_INTERFACE "org.freedesktop.ConsoleKit"
+
+#define CK_MANAGER_PATH      "/org/freedesktop/ConsoleKit/Manager"
+#define CK_MANAGER_INTERFACE "org.freedesktop.ConsoleKit.Manager"
+#define CK_SEAT_INTERFACE    "org.freedesktop.ConsoleKit.Seat"
+#define CK_SESSION_INTERFACE "org.freedesktop.ConsoleKit.Session"
+
 #define GLADE_XML_FILE "gdm-greeter-login-window.glade"
 
 #define GDM_GREETER_LOGIN_WINDOW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDM_TYPE_GREETER_LOGIN_WINDOW, GdmGreeterLoginWindowPrivate))
@@ -461,6 +476,232 @@
         do_cancel (login_window);
 }
 
+static gboolean
+try_system_stop (DBusGConnection *connection,
+                 GError         **error)
+{
+        DBusGProxy      *proxy;
+        gboolean         res;
+
+        g_debug ("GdmGreeterLoginWindow: trying to stop system");
+
+        proxy = dbus_g_proxy_new_for_name (connection,
+                                           CK_NAME,
+                                           CK_MANAGER_PATH,
+                                           CK_MANAGER_INTERFACE);
+        res = dbus_g_proxy_call_with_timeout (proxy,
+                                              "Stop",
+                                              INT_MAX,
+                                              error,
+                                              /* parameters: */
+                                              G_TYPE_INVALID,
+                                              /* return values: */
+                                              G_TYPE_INVALID);
+        return res;
+}
+
+static gboolean
+try_system_restart (DBusGConnection *connection,
+                    GError         **error)
+{
+        DBusGProxy      *proxy;
+        gboolean         res;
+
+        g_debug ("GdmGreeterLoginWindow: trying to restart system");
+
+        proxy = dbus_g_proxy_new_for_name (connection,
+                                           CK_NAME,
+                                           CK_MANAGER_PATH,
+                                           CK_MANAGER_INTERFACE);
+        res = dbus_g_proxy_call_with_timeout (proxy,
+                                              "Restart",
+                                              INT_MAX,
+                                              error,
+                                              /* parameters: */
+                                              G_TYPE_INVALID,
+                                              /* return values: */
+                                              G_TYPE_INVALID);
+        return res;
+}
+
+static void
+system_restart_auth_cb (PolKitAction          *action,
+                        gboolean               gained_privilege,
+                        GError                *error,
+                        GdmGreeterLoginWindow *login_window)
+{
+        GError          *local_error;
+        DBusGConnection *connection;
+        gboolean         res;
+
+        if (! gained_privilege) {
+                return;
+        }
+
+        local_error = NULL;
+        connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &local_error);
+        if (connection == NULL) {
+                g_warning ("Unable to get system bus connection: %s", local_error->message);
+                g_error_free (local_error);
+                return;
+        }
+
+        res = try_system_restart (connection, &local_error);
+        if (! res) {
+                g_warning ("Unable to restart system: %s", local_error->message);
+                g_error_free (local_error);
+                return;
+        }
+}
+
+static void
+system_stop_auth_cb (PolKitAction          *action,
+                     gboolean               gained_privilege,
+                     GError                *error,
+                     GdmGreeterLoginWindow *login_window)
+{
+        GError          *local_error;
+        DBusGConnection *connection;
+        gboolean         res;
+
+        if (! gained_privilege) {
+                return;
+        }
+
+        local_error = NULL;
+        connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &local_error);
+        if (connection == NULL) {
+                g_warning ("Unable to get system bus connection: %s", local_error->message);
+                g_error_free (local_error);
+                return;
+        }
+
+        res = try_system_stop (connection, &local_error);
+        if (! res) {
+                g_warning ("Unable to ssystem: %s", local_error->message);
+                g_error_free (local_error);
+                return;
+        }
+}
+
+static void
+do_system_restart (GdmGreeterLoginWindow *login_window)
+{
+        gboolean         res;
+        GError          *error;
+        DBusGConnection *connection;
+
+        error = NULL;
+        connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
+        if (connection == NULL) {
+                g_warning ("Unable to get system bus connection: %s", error->message);
+                g_error_free (error);
+                return;
+        }
+
+        res = try_system_restart (connection, &error);
+        if (! res) {
+                g_debug ("GdmGreeterLoginWindow: unable to restart system: %s", error->message);
+
+                if (dbus_g_error_has_name (error, "org.freedesktop.ConsoleKit.Manager.NotPrivileged")) {
+                        PolKitAction *action;
+                        guint         xid;
+                        pid_t         pid;
+
+                        xid = 0;
+                        pid = getpid ();
+
+                        action = polkit_action_new ();
+                        /* FIXME: check num users */
+                        polkit_action_set_action_id (action, "org.freedesktop.consolekit.system.restart");
+
+                        g_error_free (error);
+                        error = NULL;
+                        res = polkit_gnome_auth_obtain (action,
+                                                        xid,
+                                                        pid,
+                                                        (PolKitGnomeAuthCB) system_restart_auth_cb,
+                                                        login_window,
+                                                        &error);
+                        polkit_action_unref (action);
+
+                        if (! res) {
+                                g_warning ("Unable to request privilege for action: %s", error->message);
+                                g_error_free (error);
+                        }
+
+                }
+        }
+
+}
+
+
+static void
+do_system_stop (GdmGreeterLoginWindow *login_window)
+{
+        gboolean         res;
+        GError          *error;
+        DBusGConnection *connection;
+
+        error = NULL;
+        connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
+        if (connection == NULL) {
+                g_warning ("Unable to get system bus connection: %s", error->message);
+                g_error_free (error);
+                return;
+        }
+
+        res = try_system_stop (connection, &error);
+        if (! res) {
+                g_debug ("GdmGreeterLoginWindow: unable to restart system: %s", error->message);
+                if (dbus_g_error_has_name (error, "org.freedesktop.ConsoleKit.Manager.NotPrivileged")) {
+                        PolKitAction *action;
+                        guint         xid;
+                        pid_t         pid;
+
+                        xid = 0;
+                        pid = getpid ();
+
+                        action = polkit_action_new ();
+                        /* FIXME: check num users */
+                        polkit_action_set_action_id (action, "org.freedesktop.consolekit.system.stop");
+
+                        g_error_free (error);
+                        error = NULL;
+                        res = polkit_gnome_auth_obtain (action,
+                                                        xid,
+                                                        pid,
+                                                        (PolKitGnomeAuthCB) system_stop_auth_cb,
+                                                        login_window,
+                                                        &error);
+                        polkit_action_unref (action);
+
+                        if (! res) {
+                                g_warning ("Unable to request privilege for action: %s", error->message);
+                                g_error_free (error);
+                        }
+
+                }
+        }
+
+}
+
+static void
+restart_button_clicked (GtkButton             *button,
+                        GdmGreeterLoginWindow *login_window)
+{
+        g_debug ("GdmGreeterLoginWindow: restart button clicked");
+        do_system_restart (login_window);
+}
+
+static void
+shutdown_button_clicked (GtkButton             *button,
+                         GdmGreeterLoginWindow *login_window)
+{
+        g_debug ("GdmGreeterLoginWindow: stop button clicked");
+        do_system_stop (login_window);
+}
+
 static void
 on_user_chosen (GdmUserChooserWidget  *user_chooser,
                 GdmGreeterLoginWindow *login_window)
@@ -851,6 +1092,11 @@
         button = glade_xml_get_widget (login_window->priv->xml, "cancel-button");
         g_signal_connect (button, "clicked", G_CALLBACK (cancel_button_clicked), login_window);
 
+        button = glade_xml_get_widget (login_window->priv->xml, "restart-button");
+        g_signal_connect (button, "clicked", G_CALLBACK (restart_button_clicked), login_window);
+        button = glade_xml_get_widget (login_window->priv->xml, "shutdown-button");
+        g_signal_connect (button, "clicked", G_CALLBACK (shutdown_button_clicked), login_window);
+
         entry = glade_xml_get_widget (login_window->priv->xml, "auth-prompt-entry");
         /* Only change the invisible character if it '*' otherwise assume it is OK */
         if ('*' == gtk_entry_get_invisible_char (GTK_ENTRY (entry))) {



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