Author: kelnos
Date: 2009-07-16 07:16:26 +0000 (Thu, 16 Jul 2009)
New Revision: 30319

Modified:
   xfce4-session/trunk/xfce4-session/shutdown.c
   xfce4-session/trunk/xfce4-session/shutdown.h
   xfce4-session/trunk/xfce4-session/xfsm-global.h
   xfce4-session/trunk/xfce4-session/xfsm-shutdown-helper.c
   xfce4-session/trunk/xfce4-session/xfsm-shutdown-helper.h
Log:
query HAL if suspend/hibernate are supported

also clear up internal confusion by getting rid of the
XfsmShutdownCommand type and just using XfsmShutdownType everywhere

Modified: xfce4-session/trunk/xfce4-session/shutdown.c
===================================================================
--- xfce4-session/trunk/xfce4-session/shutdown.c        2009-07-15 15:41:36 UTC 
(rev 30318)
+++ xfce4-session/trunk/xfce4-session/shutdown.c        2009-07-16 07:16:26 UTC 
(rev 30319)
@@ -548,6 +548,11 @@
         gtk_widget_set_sensitive (hibernate_button, FALSE);
     }
 
+  if (suspend_button && !xfsm_shutdown_helper_supports (shutdown_helper, 
XFSM_SHUTDOWN_SUSPEND))
+    gtk_widget_hide (suspend_button);
+  if (hibernate_button && !xfsm_shutdown_helper_supports (shutdown_helper, 
XFSM_SHUTDOWN_HIBERNATE))
+    gtk_widget_hide (hibernate_button);
+
   /* save portion of the root window covered by the dialog */
   if (!accessibility && shutdown_helper != NULL)
     {

Modified: xfce4-session/trunk/xfce4-session/shutdown.h
===================================================================
--- xfce4-session/trunk/xfce4-session/shutdown.h        2009-07-15 15:41:36 UTC 
(rev 30318)
+++ xfce4-session/trunk/xfce4-session/shutdown.h        2009-07-16 07:16:26 UTC 
(rev 30319)
@@ -24,16 +24,7 @@
 
 #include <glib.h>
 
-/* */
-typedef enum
-{
-  XFSM_SHUTDOWN_ASK = 0,
-  XFSM_SHUTDOWN_LOGOUT,
-  XFSM_SHUTDOWN_HALT,
-  XFSM_SHUTDOWN_REBOOT,
-  XFSM_SHUTDOWN_SUSPEND,
-  XFSM_SHUTDOWN_HIBERNATE,
-} XfsmShutdownType;
+#include "xfsm-global.h"
 
 /* prototypes */
 extern gboolean        shutdownDialog(const gchar *sessionName,

Modified: xfce4-session/trunk/xfce4-session/xfsm-global.h
===================================================================
--- xfce4-session/trunk/xfce4-session/xfsm-global.h     2009-07-15 15:41:36 UTC 
(rev 30318)
+++ xfce4-session/trunk/xfce4-session/xfsm-global.h     2009-07-16 07:16:26 UTC 
(rev 30319)
@@ -28,6 +28,15 @@
 
 #include <xfce4-session/xfsm-splash-screen.h>
 
+typedef enum
+{
+  XFSM_SHUTDOWN_ASK = 0,
+  XFSM_SHUTDOWN_LOGOUT,
+  XFSM_SHUTDOWN_HALT,
+  XFSM_SHUTDOWN_REBOOT,
+  XFSM_SHUTDOWN_SUSPEND,
+  XFSM_SHUTDOWN_HIBERNATE,
+} XfsmShutdownType;
 
 typedef struct _FailsafeClient FailsafeClient;
 struct _FailsafeClient

Modified: xfce4-session/trunk/xfce4-session/xfsm-shutdown-helper.c
===================================================================
--- xfce4-session/trunk/xfce4-session/xfsm-shutdown-helper.c    2009-07-15 
15:41:36 UTC (rev 30318)
+++ xfce4-session/trunk/xfce4-session/xfsm-shutdown-helper.c    2009-07-16 
07:16:26 UTC (rev 30319)
@@ -67,7 +67,7 @@
 
 static struct
 {
-  XfsmShutdownCommand command;
+  XfsmShutdownType command;
   gchar * name;
 } command_name_map[] = {
   { XFSM_SHUTDOWN_HALT,      "Shutdown"  },
@@ -88,18 +88,12 @@
 };
 
 
-
-static gboolean
-xfsm_shutdown_helper_hal_check (XfsmShutdownHelper *helper,
-                                GError **error)
+static DBusConnection *
+xfsm_shutdown_helper_dbus_connect (GError **error)
 {
   DBusConnection *connection;
-  DBusMessage    *message;
-  DBusMessage    *result;
   DBusError       derror;
 
-  g_return_val_if_fail (helper && (!error || !*error), FALSE);
-
   /* initialize the error */
   dbus_error_init (&derror);
 
@@ -111,9 +105,31 @@
       if (error)
         dbus_set_g_error (error, &derror);
       dbus_error_free (&derror);
-      return FALSE;
+      return NULL;
     }
 
+  return connection;
+}
+
+
+static gboolean
+xfsm_shutdown_helper_hal_check (XfsmShutdownHelper *helper,
+                                GError **error)
+{
+  DBusConnection *connection;
+  DBusMessage    *message;
+  DBusMessage    *result;
+  DBusError       derror;
+
+  g_return_val_if_fail (helper && (!error || !*error), FALSE);
+
+  connection = xfsm_shutdown_helper_dbus_connect (error);
+  if (!connection)
+    return FALSE;
+
+  /* initialize the error */
+  dbus_error_init (&derror);
+
   /* this is a simple trick to check whether we are allowed to
    * use the org.freedesktop.Hal.Device.SystemPowerManagement
    * interface without shutting down/rebooting now.
@@ -166,7 +182,7 @@
 
 static gboolean
 xfsm_shutdown_helper_hal_send (XfsmShutdownHelper *helper,
-                               XfsmShutdownCommand command,
+                               XfsmShutdownType command,
                                GError **error)
 {
   DBusConnection *connection;
@@ -201,20 +217,13 @@
       return FALSE;
     }
 
+  connection = xfsm_shutdown_helper_dbus_connect (error);
+  if(!connection)
+    return FALSE;
+
   /* initialize the error */
   dbus_error_init (&derror);
 
-  /* connect to the system message bus */
-  connection = dbus_bus_get (DBUS_BUS_SYSTEM, &derror);
-  if (G_UNLIKELY (connection == NULL))
-    {
-      g_warning (G_STRLOC ": Failed to connect to the system message bus: %s", 
derror.message);
-      if (error)
-        dbus_set_g_error (error, &derror);
-      dbus_error_free (&derror);
-      return FALSE;
-    }
-
   /* send the appropriate message to HAL, telling it to shutdown or reboot the 
system */
   message = dbus_message_new_method_call ("org.freedesktop.Hal",
                                           
"/org/freedesktop/Hal/devices/computer",
@@ -245,6 +254,65 @@
 
 
 
+static gboolean
+xfsm_shutdown_helper_check_hal_property (const gchar *object_path,
+                                         const gchar *property_name)
+{
+  DBusConnection *connection;
+  DBusMessage    *message;
+  DBusMessage    *result;
+  DBusError       derror;
+  dbus_bool_t     response = FALSE;
+
+  connection = xfsm_shutdown_helper_dbus_connect (NULL);
+  if (!connection)
+    return FALSE;
+
+  message = dbus_message_new_method_call ("org.freedesktop.Hal",
+                                          object_path,
+                                          "org.freedesktop.Hal.Device",
+                                          "GetPropertyBoolean");
+  dbus_message_append_args (message,
+                            DBUS_TYPE_STRING, &property_name,
+                            DBUS_TYPE_INVALID);
+
+  dbus_error_init(&derror);
+  result = dbus_connection_send_with_reply_and_block (connection, message, -1, 
&derror);
+  dbus_message_unref (message);
+
+  if (G_UNLIKELY (result == NULL))
+    {
+      g_warning (G_STRLOC ": Failed to contact HAL: %s", derror.message);
+      dbus_error_free (&derror);
+      return FALSE;
+    }
+
+  if (!result)
+    return FALSE;
+
+  if (dbus_set_error_from_message (&derror, result))
+    {
+      dbus_error_free (&derror);
+      response = FALSE;
+    }
+  else
+    {
+      if (!dbus_message_get_args (result, &derror,
+                                  DBUS_TYPE_BOOLEAN, &response,
+                                  DBUS_TYPE_INVALID))
+        {
+          dbus_error_free (&derror);
+          response = FALSE;
+        }
+    }
+
+  dbus_message_unref (result);
+
+  return response;
+}
+
+
+
 XfsmShutdownHelper*
 xfsm_shutdown_helper_spawn (GError **error)
 {
@@ -517,15 +585,16 @@
 
 gboolean
 xfsm_shutdown_helper_send_command (XfsmShutdownHelper *helper,
-                                   XfsmShutdownCommand command,
+                                   XfsmShutdownType command,
                                    GError **error)
 {
-  static gchar *command_table[] = { "POWEROFF", "REBOOT" };
+  const gchar *command_str = NULL;
   gchar         response[256];
 
   g_return_val_if_fail (helper != NULL, FALSE);
   g_return_val_if_fail (!helper->need_password, FALSE);
   g_return_val_if_fail (!error || !*error, FALSE);
+  g_return_val_if_fail (command != XFSM_SHUTDOWN_ASK, FALSE);
 
   /* check if we can use HAL to perform the requested action */
   if (G_LIKELY (helper->use_hal))
@@ -536,20 +605,30 @@
   else
     {
       /* we don't support hibernate or suspend without HAL */
-      if (command == XFSM_SHUTDOWN_SUSPEND || command == 
XFSM_SHUTDOWN_HIBERNATE)
+      switch (command)
         {
-          if (error)
-            {
-              g_set_error (error, DBUS_GERROR, DBUS_GERROR_SERVICE_UNKNOWN,
-                           _("Suspend and Hibernate are only supported through 
HAL, which is unavailable"));
-            }
-          return FALSE;
+          case XFSM_SHUTDOWN_HALT:
+            command_str = "POWEROFF";
+            break;
+
+          case XFSM_SHUTDOWN_REBOOT:
+            command_str = "REBOOT";
+            break;
+
+          case XFSM_SHUTDOWN_SUSPEND:
+          case XFSM_SHUTDOWN_HIBERNATE:
+            if (error)
+              {
+                g_set_error (error, DBUS_GERROR, DBUS_GERROR_SERVICE_UNKNOWN,
+                             _("Suspend and Hibernate are only supported 
through HAL, which is unavailable"));
+              }
+            /* fall through */
+          default:
+            return FALSE;
         }
 
       /* send it to our associated sudo'ed process */
-      /* -2 is not a magic number, it's to get the right offset in 
command_table array */
-      /* because in enum, XFSM_SHUTDOWN_HALT = 2 and XFSM_SHUTDOWN_REBOOT = 3 
*/
-      fprintf (helper->outfile, "%s\n", command_table[command - 2]);
+      fprintf (helper->outfile, "%s\n", command_str);
       fflush (helper->outfile);
 
       if (ferror (helper->outfile))
@@ -590,6 +669,52 @@
 
 
 
+gboolean
+xfsm_shutdown_helper_supports (XfsmShutdownHelper *helper,
+                               XfsmShutdownType shutdown_type)
+{
+  if (helper->use_hal)
+    {
+      switch (shutdown_type)
+        {
+          case XFSM_SHUTDOWN_SUSPEND:
+            if (xfsm_shutdown_helper_check_hal_property 
("/org/freedesktop/Hal/devices/computer",
+                                                         
"power_management.can_suspend"))
+              {
+                return TRUE;
+              }
+            return FALSE;
+
+          case XFSM_SHUTDOWN_HIBERNATE:
+            if (xfsm_shutdown_helper_check_hal_property 
("/org/freedesktop/Hal/devices/computer",
+                                                         
"power_management.can_hibernate"))
+              {
+                return TRUE;
+              }
+            return FALSE;
+
+          default:
+            return TRUE;
+        }
+    }
+  else
+    {
+      switch (shutdown_type)
+        {
+          case XFSM_SHUTDOWN_SUSPEND:
+          case XFSM_SHUTDOWN_HIBERNATE:
+            return FALSE;
+
+          default:
+            return TRUE;
+        }
+    }
+
+  g_assert_not_reached();
+}
+
+
+
 void
 xfsm_shutdown_helper_destroy (XfsmShutdownHelper *helper)
 {

Modified: xfce4-session/trunk/xfce4-session/xfsm-shutdown-helper.h
===================================================================
--- xfce4-session/trunk/xfce4-session/xfsm-shutdown-helper.h    2009-07-15 
15:41:36 UTC (rev 30318)
+++ xfce4-session/trunk/xfce4-session/xfsm-shutdown-helper.h    2009-07-16 
07:16:26 UTC (rev 30319)
@@ -24,14 +24,9 @@
 
 #include <glib.h>
 
+#include "xfsm-global.h"
 
-typedef enum
-{
-  XFSM_SHUTDOWN_COMMAND_POWEROFF   = 0,
-  XFSM_SHUTDOWN_COMMAND_REBOOT     = 1,
-} XfsmShutdownCommand;
 
-
 typedef struct _XfsmShutdownHelper XfsmShutdownHelper;
 
 
@@ -43,9 +38,12 @@
                                              const gchar        *password);
 
 gboolean xfsm_shutdown_helper_send_command (XfsmShutdownHelper *helper,
-                                            XfsmShutdownCommand command,
+                                            XfsmShutdownType command,
                                             GError **error);
 
+gboolean xfsm_shutdown_helper_supports (XfsmShutdownHelper *helper,
+                                        XfsmShutdownType shutdown_type);
+
 void xfsm_shutdown_helper_destroy (XfsmShutdownHelper *helper);
 
 #endif /* !__XFSM_SHUTDOWN_HELPER_H__ */

_______________________________________________
Xfce4-commits mailing list
Xfce4-commits@xfce.org
http://foo-projects.org/mailman/listinfo/xfce4-commits

Reply via email to