[Goodies-commits] r7026 - in xfce4-power-manager/trunk: . libxfpm po src

Ali Abdallah aliov at xfce.org
Sun Mar 29 15:39:50 CEST 2009


Author: aliov
Date: 2009-03-29 13:39:50 +0000 (Sun, 29 Mar 2009)
New Revision: 7026

Added:
   xfce4-power-manager/trunk/src/xfpm-shutdown.c
   xfce4-power-manager/trunk/src/xfpm-shutdown.h
Removed:
   xfce4-power-manager/trunk/libxfpm/hal-iface.c
   xfce4-power-manager/trunk/libxfpm/hal-iface.h
Modified:
   xfce4-power-manager/trunk/ChangeLog
   xfce4-power-manager/trunk/libxfpm/Makefile.am
   xfce4-power-manager/trunk/po/POTFILES.in
   xfce4-power-manager/trunk/src/Makefile.am
   xfce4-power-manager/trunk/src/xfpm-cpu.c
   xfce4-power-manager/trunk/src/xfpm-engine.c
   xfce4-power-manager/trunk/src/xfpm-errors.c
   xfce4-power-manager/trunk/src/xfpm-errors.h
   xfce4-power-manager/trunk/src/xfpm-supply.c
   xfce4-power-manager/trunk/src/xfpm-tray-icon.c
Log:
Added xfpmshutdown to sync all the request

Modified: xfce4-power-manager/trunk/ChangeLog
===================================================================
--- xfce4-power-manager/trunk/ChangeLog	2009-03-29 12:43:11 UTC (rev 7025)
+++ xfce4-power-manager/trunk/ChangeLog	2009-03-29 13:39:50 UTC (rev 7026)
@@ -1,4 +1,7 @@
 
+2009-03-29 15:39 Ali aliov at xfce.org 
+	 * : Added xfpmshutdown to sync all the request
+
 2009-03-29 12:22 Ali aliov at xfce.org 
 	 * : Added power off button action
 

Modified: xfce4-power-manager/trunk/libxfpm/Makefile.am
===================================================================
--- xfce4-power-manager/trunk/libxfpm/Makefile.am	2009-03-29 12:43:11 UTC (rev 7025)
+++ xfce4-power-manager/trunk/libxfpm/Makefile.am	2009-03-29 13:39:50 UTC (rev 7026)
@@ -10,8 +10,6 @@
 	hal-monitor.h			\
 	hal-manager.c			\
 	hal-manager.h			\
-	hal-iface.c			\
-	hal-iface.h			\
 	hal-device.c			\
 	hal-device.h
 

Modified: xfce4-power-manager/trunk/po/POTFILES.in
===================================================================
--- xfce4-power-manager/trunk/po/POTFILES.in	2009-03-29 12:43:11 UTC (rev 7025)
+++ xfce4-power-manager/trunk/po/POTFILES.in	2009-03-29 13:39:50 UTC (rev 7026)
@@ -19,6 +19,7 @@
 src/xfpm-manager.c
 src/xfpm-inhibit.c
 src/xfpm-screen-saver.c
+src/xfpm-shutdown.c
 src/xfce4-power-manager.desktop.in
 libxfpm/hal-battery.c
 libxfpm/hal-device.c

Modified: xfce4-power-manager/trunk/src/Makefile.am
===================================================================
--- xfce4-power-manager/trunk/src/Makefile.am	2009-03-29 12:43:11 UTC (rev 7025)
+++ xfce4-power-manager/trunk/src/Makefile.am	2009-03-29 13:39:50 UTC (rev 7026)
@@ -45,6 +45,8 @@
 	xfpm-screen-saver.h			\
 	xfpm-tray-icon.c			\
 	xfpm-tray-icon.h			\
+	xfpm-shutdown.c				\
+	xfpm-shutdown.h				\
 	xfpm-network-manager.c			\
 	xfpm-network-manager.h			\
 	xfpm-errors.c				\

Modified: xfce4-power-manager/trunk/src/xfpm-cpu.c
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-cpu.c	2009-03-29 12:43:11 UTC (rev 7025)
+++ xfce4-power-manager/trunk/src/xfpm-cpu.c	2009-03-29 13:39:50 UTC (rev 7026)
@@ -23,22 +23,16 @@
 #endif
 
 #include <stdio.h>
-
-#ifdef HAVE_STDLIB_H
 #include <stdlib.h>
-#endif
-
-#ifdef HAVE_STRING_H
 #include <string.h>
-#endif
 
-#ifdef HAVE_ERRNO_H
-#include <errno.h>
-#endif
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
 
 #include <libxfce4util/libxfce4util.h>
 
-#include "libxfpm/hal-iface.h"
+#include "libxfpm/hal-manager.h"
 #include "libxfpm/xfpm-string.h"
 
 #include "xfpm-cpu.h"
@@ -57,22 +51,152 @@
 
 struct XfpmCpuPrivate
 {
-    XfpmAdapter   *adapter;
-    XfpmXfconf    *conf;
-    HalIface      *iface;
+    DBusGConnection *bus;
+    XfpmAdapter     *adapter;
+    XfpmXfconf      *conf;
     
-    gboolean 	   on_battery;
-    guint8         cpu_governors;
+    gboolean 	     on_battery;
+    guint8           cpu_governors;
 };
 
 G_DEFINE_TYPE(XfpmCpu, xfpm_cpu, G_TYPE_OBJECT)
 
+static gboolean
+xfpm_cpu_check_interface (XfpmCpu *cpu)
+{
+    DBusMessage *message;
+    DBusMessage *reply;
+    DBusError error ;
+    
+    message = dbus_message_new_method_call ("org.freedesktop.Hal",
+					    "/org/freedesktop/Hal",
+					    "org.freedesktop.Hal.Device.CPUFreq",
+					    "JustToCheck");
+    
+    if (!message)
+    	return FALSE;
+    
+    dbus_error_init (&error);
+    
+    reply = 
+    	dbus_connection_send_with_reply_and_block (dbus_g_connection_get_connection(cpu->priv->bus),
+						   message, 2000, &error);
+    dbus_message_unref (message);
+    
+    if ( reply ) dbus_message_unref (reply);
+    
+    if ( dbus_error_is_set(&error) )
+    {
+	if ( !g_strcmp0 (error.name, "org.freedesktop.DBus.Error.UnknownMethod") )
+        {
+            dbus_error_free(&error);
+	    return TRUE;
+        }
+    }
+    return FALSE;
+}
+
+static gchar **
+xfpm_cpu_get_hal_available_governors (XfpmCpu *cpu)
+{
+    gchar **governors = NULL;
+    GError *error = NULL;
+    DBusGProxy *proxy = dbus_g_proxy_new_for_name (cpu->priv->bus,
+						   "org.freedesktop.Hal",
+						   "/org/freedesktop/Hal/devices/computer",
+						   "org.freedesktop.Hal.Device.CPUFreq");
+    if ( !proxy )
+    {
+	g_critical ("Failed to create proxy");
+	goto out;
+    }
+    
+    dbus_g_proxy_call (proxy, "GetCPUFreqAvailableGovernors", &error,
+		       G_TYPE_INVALID,
+		       G_TYPE_STRV, &governors,
+		       G_TYPE_INVALID);
+		       
+    if ( error )
+    {
+	g_warning ("Failed to get cpu governors: %s", error->message);
+	g_error_free (error);
+    }
+    
+    g_object_unref (proxy);
+out:
+    return governors;
+}
+
+static gchar *
+xfpm_cpu_get_hal_current_governor (XfpmCpu *cpu)
+{
+    gchar *governor = NULL;
+    GError *error = NULL;
+    
+    DBusGProxy *proxy = dbus_g_proxy_new_for_name (cpu->priv->bus,
+						   "org.freedesktop.Hal",
+						   "/org/freedesktop/Hal/devices/computer",
+						   "org.freedesktop.Hal.Device.CPUFreq");
+    if ( !proxy )
+    {
+	g_critical ("Failed to create proxy");
+	goto out;
+    }
+    
+    dbus_g_proxy_call (proxy, "GetCPUFreqGovernor", &error,
+		       G_TYPE_INVALID,
+		       G_TYPE_STRING, &governor,
+		       G_TYPE_INVALID);
+    
+    if ( error )
+    {
+	g_warning ("Failed to get cpu governor: %s", error->message);
+	g_error_free (error);
+    }
+    
+    g_object_unref (proxy);
+out:
+    return governor;
+}
+
+static gboolean
+xfpm_cpu_set_hal_governor (XfpmCpu *cpu, const gchar *governor)
+{
+    gboolean ret = FALSE;
+    GError *error = NULL;
+    
+    DBusGProxy *proxy = dbus_g_proxy_new_for_name (cpu->priv->bus,
+						   "org.freedesktop.Hal",
+						   "/org/freedesktop/Hal/devices/computer",
+						   "org.freedesktop.Hal.Device.CPUFreq");
+    if ( !proxy )
+    {
+	g_critical ("Failed to create proxy");
+	goto out;
+    }
+    
+    ret = dbus_g_proxy_call (proxy, "SetCPUFreqGovernor", &error,
+	  		     G_TYPE_STRING, governor,
+			     G_TYPE_INVALID,
+			     G_TYPE_INVALID);
+			     
+    if ( error )
+    {
+	g_warning ("Failed to set cpu governor: %s", error->message);
+	g_error_free (error);
+    }
+    		     
+    g_object_unref (proxy);
+out:
+    return ret;
+}
+
 static void
 xfpm_cpu_set_governor (XfpmCpu *cpu, const gchar *governor)
 {
     TRACE("Settings cpu governor to %s", governor);
     
-    if (! hal_iface_set_cpu_governor(cpu->priv->iface, governor, NULL))
+    if (! xfpm_cpu_set_hal_governor(cpu, governor))
     {
     	g_critical ("Unable to set CPU governor to %s", governor);
     }
@@ -97,16 +221,14 @@
     gchar *current_governor = NULL;
     XfpmCpuGovernor governor_enum;
     
-    current_governor = hal_iface_get_cpu_current_governor (cpu->priv->iface, NULL);
+    current_governor = xfpm_cpu_get_hal_current_governor (cpu);
     
     if ( !current_governor )
     {
 	g_warning ("Unable to get current governor");
 	return CPU_UNKNOWN;
     }
-    
     governor_enum = _governor_name_to_enum (current_governor);
-    
     g_free (current_governor);
     return governor_enum;
 }
@@ -119,7 +241,7 @@
 {
     int i;
     gchar **governors = NULL;
-    governors = hal_iface_get_cpu_governors (cpu->priv->iface, NULL);
+    governors = xfpm_cpu_get_hal_available_governors (cpu);
 	
     if ( !governors || !governors[0])
     {
@@ -137,24 +259,17 @@
 	else if ( xfpm_strequal(governors[i], "performance") )
 	    cpu->priv->cpu_governors |= CPU_PERFORMANCE;
     }
-    hal_iface_free_string_array (governors);
+    
+    hal_manager_free_string_array (governors);
 }
 
 static gboolean
 xfpm_cpu_check_iface (XfpmCpu *cpu)
 {
-    gboolean caller_privilege, cpu_freq_iface;
-    g_object_get (G_OBJECT(cpu->priv->iface), 
-		  "caller-privilege", &caller_privilege,
-		  "cpu-freq-iface", &cpu_freq_iface,
-		  NULL);
+    gboolean cpu_freq_iface;
+    
+    cpu_freq_iface = xfpm_cpu_check_interface (cpu);
 		  
-    if ( !caller_privilege )
-    {
-	g_warning ("Using CPU FREQ interface permission denied");
-	return FALSE;
-    }
-    
     if (!cpu_freq_iface)
     {
 	g_warning ("CPU FREQ interface cannot be used");
@@ -230,8 +345,11 @@
     cpu->priv = XFPM_CPU_GET_PRIVATE(cpu);
     cpu->priv->cpu_governors = 0;
     
-    cpu->priv->iface         = hal_iface_new ();
+    cpu->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
     
+    if (!cpu->priv->bus)
+	goto out;
+    
     if ( xfpm_cpu_check_iface (cpu))
     {
 	cpu->priv->adapter       = xfpm_adapter_new ();
@@ -246,6 +364,8 @@
 	cpu->priv->on_battery = !xfpm_adapter_get_present (cpu->priv->adapter);
     }
     
+out:
+    ;
 }
 
 static void
@@ -258,11 +378,11 @@
     if ( cpu->priv->conf )
 	g_object_unref (cpu->priv->conf);
 	
-    if ( cpu->priv->iface )
-	g_object_unref (cpu->priv->iface);
-	
     if ( cpu->priv->adapter)
 	g_object_unref (cpu->priv->adapter);
+	
+    if ( cpu->priv->bus )
+	dbus_g_connection_unref (cpu->priv->bus);
 
     G_OBJECT_CLASS(xfpm_cpu_parent_class)->finalize(object);
 }

Modified: xfce4-power-manager/trunk/src/xfpm-engine.c
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-engine.c	2009-03-29 12:43:11 UTC (rev 7025)
+++ xfce4-power-manager/trunk/src/xfpm-engine.c	2009-03-29 13:39:50 UTC (rev 7026)
@@ -23,25 +23,13 @@
 #endif
 
 #include <stdio.h>
-
-#ifdef HAVE_STDLIB_H
 #include <stdlib.h>
-#endif
-
-#ifdef HAVE_STRING_H
 #include <string.h>
-#endif
 
-#ifdef HAVE_ERRNO_H
-#include <errno.h>
-#endif
-
 #include <glib.h>
 
 #include <libxfce4util/libxfce4util.h>
-#include <xfconf/xfconf.h>
 
-#include "libxfpm/hal-iface.h"
 #include "libxfpm/hal-manager.h"
 #include "libxfpm/xfpm-string.h"
 #include "libxfpm/xfpm-common.h"
@@ -61,7 +49,7 @@
 #include "xfpm-lid-hal.h"
 #include "xfpm-inhibit.h"
 #include "xfpm-backlight.h"
-#include "xfpm-screen-saver.h"
+#include "xfpm-shutdown.h"
 #include "xfpm-errors.h"
 #include "xfpm-config.h"
 
@@ -78,122 +66,115 @@
 
 struct XfpmEnginePrivate
 {
-  XfpmXfconf *conf;
-  XfpmSupply *supply;
-  XfpmNotify *notify;
+    XfpmXfconf 		*conf;
+    XfpmSupply 		*supply;
+    XfpmNotify 		*notify;
 
-  XfpmCpu *cpu;
-  XfpmButtonXf86 *xf86_button;
-  XfpmLidHal *lid;
-  XfpmBacklight *bk;
-  XfpmAdapter *adapter;
-  XfpmInhibit *inhibit;
-  HalIface *iface;
+    XfpmCpu 		*cpu;
+    XfpmButtonXf86 	*xf86_button;
+    XfpmLidHal 		*lid;
+    XfpmBacklight 	*bk;
+    XfpmAdapter 	*adapter;
+    XfpmInhibit 	*inhibit;
+    XfpmShutdown        *shutdown;
 #ifdef HAVE_DPMS
-  XfpmDpms *dpms;
+    XfpmDpms *dpms;
 #endif
+    gboolean inhibited;
 
-  gboolean inhibited;
+    guint8 power_management;
+    gboolean on_battery;
+    gboolean is_laptop;
 
-  guint8 power_management;
-  gboolean on_battery;
-
-  gboolean block_shutdown;
-
-  gboolean is_laptop;
-  gboolean has_lcd_brightness;
-  gboolean has_lid;
+    gboolean has_lcd_brightness;
+    gboolean has_lid;
 };
 
 G_DEFINE_TYPE (XfpmEngine, xfpm_engine, G_TYPE_OBJECT)
 
 static gboolean xfpm_engine_do_suspend (XfpmEngine * engine)
 {
-  GError *error = NULL;
+    GError *error = NULL;
 
-  hal_iface_shutdown (engine->priv->iface, "Suspend", &error);
+    xfpm_suspend (engine->priv->shutdown, &error);
 
-  if (error)
+    if (error)
     {
-      g_warning ("%s", error->message);
-      g_error_free (error);
+	g_warning ("%s", error->message);
+	g_error_free (error);
     }
-  xfpm_send_message_to_network_manager ("wake");
-  engine->priv->block_shutdown = FALSE;
-  return FALSE;
+    xfpm_send_message_to_network_manager ("wake");
+    return FALSE;
 }
 
 static gboolean
 xfpm_engine_do_hibernate (XfpmEngine * engine)
 {
-  GError *error = NULL;
+    GError *error = NULL;
 
-  hal_iface_shutdown (engine->priv->iface, "Hibernate", &error);
+    xfpm_hibernate (engine->priv->shutdown, &error);
 
-  if (error)
+    if (error)
     {
-      g_warning ("%s", error->message);
-      g_error_free (error);
+	g_warning ("%s", error->message);
+	g_error_free (error);
     }
-    
-  xfpm_send_message_to_network_manager ("wake");
-  engine->priv->block_shutdown = FALSE;
-  return FALSE;
+    xfpm_send_message_to_network_manager ("wake");
+    return FALSE;
 }
 
 static gboolean
 xfpm_engine_do_shutdown (XfpmEngine * engine)
 {
-  GError *error = NULL;
+    GError *error = NULL;
 
-  hal_iface_shutdown (engine->priv->iface, "Shutdown", &error);
+    xfpm_shutdown (engine->priv->shutdown, &error);
 
-  if (error)
+    if (error)
     {
-      g_warning ("%s", error->message);
-      g_error_free (error);
+	g_warning ("%s", error->message);
+	g_error_free (error);
     }
-  return FALSE;
+    return FALSE;
 }
 
 static void
 xfpm_engine_shutdown_request (XfpmEngine * engine,
 			      XfpmShutdownRequest shutdown, gboolean critical)
 {
-  gboolean lock_screen;
-  const gchar *action = xfpm_int_to_shutdown_string (shutdown);
+    gboolean lock_screen;
+    const gchar *action = xfpm_int_to_shutdown_string (shutdown);
 
-  lock_screen =
-    xfpm_xfconf_get_property_bool (engine->priv->conf, LOCK_SCREEN_ON_SLEEP);
+    lock_screen =
+	xfpm_xfconf_get_property_bool (engine->priv->conf, LOCK_SCREEN_ON_SLEEP);
 
-  if (xfpm_strequal (action, "Nothing"))
+    if (xfpm_strequal (action, "Nothing"))
     {
-      TRACE ("Sleep button disabled in configuration");
-      return;
+	TRACE ("Sleep button disabled in configuration");
+	return;
     }
-  else if (!engine->priv->inhibited)
+    else if (!engine->priv->inhibited)
     {
-      TRACE ("Going to do %s\n", action);
-      xfpm_send_message_to_network_manager ("sleep");
-      engine->priv->block_shutdown = TRUE;
+	TRACE ("Going to do %s\n", action);
+	xfpm_send_message_to_network_manager ("sleep");
 
-      if (shutdown == XFPM_DO_SHUTDOWN)
+	if (shutdown == XFPM_DO_SHUTDOWN)
 	{
-	  xfpm_engine_do_shutdown (engine);
+	    xfpm_engine_do_shutdown (engine);
 	}
-      else if (shutdown == XFPM_DO_HIBERNATE)
+	else if (shutdown == XFPM_DO_HIBERNATE)
 	{
-	  g_timeout_add_seconds (4, (GSourceFunc) xfpm_engine_do_hibernate,
-				 engine);
+	    g_timeout_add_seconds (4, (GSourceFunc) xfpm_engine_do_hibernate,
+				   engine);
 	}
-      else if (shutdown == XFPM_DO_SUSPEND)
+	else if (shutdown == XFPM_DO_SUSPEND)
 	{
-	  g_timeout_add_seconds (4, (GSourceFunc) xfpm_engine_do_suspend,
-				 engine);
+	    g_timeout_add_seconds (4, (GSourceFunc) xfpm_engine_do_suspend,
+				   engine);
 	}
 
-      if (lock_screen)
-	xfpm_lock_screen ();
+	if (lock_screen)
+	    xfpm_lock_screen ();
     }
 }
 
@@ -203,7 +184,7 @@
 					 XfpmShutdownRequest action,
 					 XfpmEngine * engine)
 {
-  xfpm_engine_shutdown_request (engine, action, critical);
+    xfpm_engine_shutdown_request (engine, action, critical);
 }
 
 static void
@@ -216,14 +197,9 @@
     if ( type != BUTTON_POWER_OFF && type != BUTTON_SLEEP )
 	return;
 
-    if (engine->priv->block_shutdown)
-	return;
-  
     shutdown = xfpm_xfconf_get_property_enum (engine->priv->conf, 
 					      type == BUTTON_POWER_OFF ? POWER_SWITCH_CFG :
 					      SLEEP_SWITCH_CFG );
-    g_print("---------------Configuration is %d\n", shutdown);
-    
     TRACE ("Accepting shutdown request");
     xfpm_engine_shutdown_request (engine, shutdown, FALSE);
 }
@@ -231,41 +207,36 @@
 static void
 xfpm_engine_lid_closed_cb (XfpmLidHal * lid, XfpmEngine * engine)
 {
-  XfpmShutdownRequest shutdown;
+    XfpmShutdownRequest shutdown;
 
-  shutdown = xfpm_xfconf_get_property_enum (engine->priv->conf,
-					    engine->priv->
-					    on_battery ?
-					    LID_SWITCH_ON_BATTERY_CFG :
-					    LID_SWITCH_ON_AC_CFG);
+    shutdown = xfpm_xfconf_get_property_enum (engine->priv->conf,
+					      engine->priv->
+					      on_battery ?
+					      LID_SWITCH_ON_BATTERY_CFG :
+					      LID_SWITCH_ON_AC_CFG);
 
-  if (engine->priv->block_shutdown)
-    return;
-
-  TRACE ("Accepting shutdown request");
-
-  xfpm_engine_shutdown_request (engine, shutdown, FALSE);
+    xfpm_engine_shutdown_request (engine, shutdown, FALSE);
 }
 
 static void
 xfpm_engine_check_hal_iface (XfpmEngine * engine)
 {
-  gboolean can_suspend, can_hibernate, caller, cpu;
+    gboolean can_suspend, can_hibernate, caller;
 
-  if (!hal_iface_connect (engine->priv->iface))
-    return;
+    g_object_get (G_OBJECT (engine->priv->shutdown),
+		  "caller-privilege", &caller,
+		  "can-suspend", &can_suspend,
+		  "can-hibernate", &can_hibernate,
+		  NULL);
 
-  g_object_get (G_OBJECT (engine->priv->iface),
-		"caller-privilege", &caller,
-		"can-suspend", &can_suspend,
-		"can-hibernate", &can_hibernate,
-		"cpu-freq-iface", &cpu, NULL);
+    if ( caller )
+    {
+	if (can_hibernate)
+	    engine->priv->power_management |= SYSTEM_CAN_HIBERNATE;
+	if (can_suspend)
+	    engine->priv->power_management |= SYSTEM_CAN_SUSPEND;
+    }
 
-  if (can_hibernate)
-    engine->priv->power_management |= SYSTEM_CAN_HIBERNATE;
-  if (can_suspend)
-    engine->priv->power_management |= SYSTEM_CAN_SUSPEND;
-
   //FIXME: Show errors here
 }
 
@@ -286,46 +257,46 @@
     g_object_unref (manager);
 
 #ifdef HAVE_DPMS
-  engine->priv->dpms = xfpm_dpms_new ();
+    engine->priv->dpms = xfpm_dpms_new ();
 #endif
-  if (engine->priv->is_laptop)
-    engine->priv->cpu = xfpm_cpu_new ();
+    if (engine->priv->is_laptop)
+	engine->priv->cpu = xfpm_cpu_new ();
 
-  engine->priv->supply = xfpm_supply_new (engine->priv->power_management);
-  g_signal_connect (G_OBJECT (engine->priv->supply), "shutdown-request",
-		    G_CALLBACK (xfpm_engine_shutdown_request_battery_cb),
-		    engine);
-  xfpm_supply_monitor (engine->priv->supply);
+    engine->priv->supply = xfpm_supply_new (engine->priv->power_management);
+    g_signal_connect (G_OBJECT (engine->priv->supply), "shutdown-request",
+		      G_CALLBACK (xfpm_engine_shutdown_request_battery_cb),
+		      engine);
+    xfpm_supply_monitor (engine->priv->supply);
 
   /*
    * Keys from XF86
    */
-  engine->priv->xf86_button = xfpm_button_xf86_new ();
+    engine->priv->xf86_button = xfpm_button_xf86_new ();
 
-  g_signal_connect (engine->priv->xf86_button, "xf86-button-pressed",
-		    G_CALLBACK (xfpm_engine_xf86_button_pressed_cb), engine);
+    g_signal_connect (engine->priv->xf86_button, "xf86-button-pressed",
+		      G_CALLBACK (xfpm_engine_xf86_button_pressed_cb), engine);
 
   /*
    * Lid from HAL 
    */
-  if (engine->priv->is_laptop)
+    if (engine->priv->is_laptop)
     {
-      engine->priv->lid = xfpm_lid_hal_new ();
-      engine->priv->has_lid = xfpm_lid_hw_found (engine->priv->lid);
-      if (engine->priv->has_lid)
-	g_signal_connect (engine->priv->lid, "lid-closed",
-			  G_CALLBACK (xfpm_engine_lid_closed_cb), engine);
-      else
-	g_object_unref (engine->priv->lid);
+	engine->priv->lid = xfpm_lid_hal_new ();
+	engine->priv->has_lid = xfpm_lid_hw_found (engine->priv->lid);
+	if (engine->priv->has_lid)
+	    g_signal_connect (engine->priv->lid, "lid-closed",
+			      G_CALLBACK (xfpm_engine_lid_closed_cb), engine);
+	else
+	    g_object_unref (engine->priv->lid);
     }
 
   /*
    * Brightness HAL
    */
-  if (engine->priv->is_laptop)
+    if (engine->priv->is_laptop)
     {
-      engine->priv->bk = xfpm_backlight_new ();
-      engine->priv->has_lcd_brightness =
+	engine->priv->bk = xfpm_backlight_new ();
+	engine->priv->has_lcd_brightness =
 	xfpm_backlight_has_hw (engine->priv->bk);
     }
 }
@@ -334,112 +305,109 @@
 xfpm_engine_adapter_changed_cb (XfpmAdapter * adapter, gboolean present,
 				XfpmEngine * engine)
 {
-  engine->priv->on_battery = !present;
+    engine->priv->on_battery = !present;
 }
 
 static void
 xfpm_engine_inhibit_changed_cb (XfpmInhibit * inhibit, gboolean inhibited,
 				XfpmEngine * engine)
 {
-  engine->priv->inhibited = inhibited;
+    engine->priv->inhibited = inhibited;
 }
 
 static void
 xfpm_engine_class_init (XfpmEngineClass * klass)
 {
-  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+    GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-  object_class->finalize = xfpm_engine_finalize;
+    object_class->finalize = xfpm_engine_finalize;
 
-  g_type_class_add_private (klass, sizeof (XfpmEnginePrivate));
+    g_type_class_add_private (klass, sizeof (XfpmEnginePrivate));
 
-  xfpm_engine_dbus_class_init (klass);
+    xfpm_engine_dbus_class_init (klass);
 }
 
 static void
 xfpm_engine_init (XfpmEngine * engine)
 {
-  engine->priv = XFPM_ENGINE_GET_PRIVATE (engine);
+    engine->priv = XFPM_ENGINE_GET_PRIVATE (engine);
 
-  engine->priv->iface = hal_iface_new ();
-  engine->priv->adapter = xfpm_adapter_new ();
-  engine->priv->notify = xfpm_notify_new ();
+    engine->priv->shutdown = xfpm_shutdown_new ();
+    engine->priv->adapter = xfpm_adapter_new ();
+    engine->priv->notify = xfpm_notify_new ();
 
-  engine->priv->inhibit = xfpm_inhibit_new ();
-  engine->priv->inhibited = FALSE;
+    engine->priv->inhibit = xfpm_inhibit_new ();
+    engine->priv->inhibited = FALSE;
 
-  g_signal_connect (engine->priv->inhibit, "has-inhibit-changed",
-		    G_CALLBACK (xfpm_engine_inhibit_changed_cb), engine);
+    g_signal_connect (engine->priv->inhibit, "has-inhibit-changed",
+		      G_CALLBACK (xfpm_engine_inhibit_changed_cb), engine);
 
-  engine->priv->conf = NULL;
-  engine->priv->supply = NULL;
+    engine->priv->conf = NULL;
+    engine->priv->supply = NULL;
 #ifdef HAVE_DPMS
-  engine->priv->dpms = NULL;
+    engine->priv->dpms = NULL;
 #endif
-  engine->priv->cpu = NULL;
-  engine->priv->xf86_button = NULL;
-  engine->priv->lid = NULL;
-  engine->priv->bk = NULL;
+    engine->priv->cpu = NULL;
+    engine->priv->xf86_button = NULL;
+    engine->priv->lid = NULL;
+    engine->priv->bk = NULL;
 
-  engine->priv->power_management = 0;
+    engine->priv->power_management = 0;
 
-  xfpm_engine_dbus_init (engine);
+    xfpm_engine_dbus_init (engine);
 
-  engine->priv->conf = xfpm_xfconf_new ();
+    engine->priv->conf = xfpm_xfconf_new ();
 
-  engine->priv->on_battery =
-    !xfpm_adapter_get_present (engine->priv->adapter);
+    engine->priv->on_battery =
+	!xfpm_adapter_get_present (engine->priv->adapter);
 
-  g_signal_connect (engine->priv->adapter, "adapter-changed",
-		    G_CALLBACK (xfpm_engine_adapter_changed_cb), engine);
+    g_signal_connect (engine->priv->adapter, "adapter-changed",
+		      G_CALLBACK (xfpm_engine_adapter_changed_cb), engine);
 
-  xfpm_engine_load_all (engine);
+    xfpm_engine_load_all (engine);
 }
 
 static void
 xfpm_engine_finalize (GObject * object)
 {
-  XfpmEngine *engine;
+    XfpmEngine *engine;
 
-  engine = XFPM_ENGINE (object);
+    engine = XFPM_ENGINE (object);
 
-  g_object_unref (engine->priv->conf);
+    g_object_unref (engine->priv->conf);
 
-  if (engine->priv->supply)
     g_object_unref (engine->priv->supply);
 
 #ifdef HAVE_DPMS
-  if (engine->priv->dpms)
-    g_object_unref (engine->priv->dpms);
+    if (engine->priv->dpms)
+	g_object_unref (engine->priv->dpms);
 #endif
 
-  if (engine->priv->cpu)
-    g_object_unref (engine->priv->cpu);
+    if (engine->priv->cpu)
+	g_object_unref (engine->priv->cpu);
 
-  if (engine->priv->lid)
-    g_object_unref (engine->priv->lid);
+    if (engine->priv->lid)
+	g_object_unref (engine->priv->lid);
 
-  if (engine->priv->iface)
-    g_object_unref (engine->priv->iface);
+    g_object_unref (engine->priv->shutdown);
 
-  if (engine->priv->adapter)
     g_object_unref (engine->priv->adapter);
 
-  if (engine->priv->bk)
-    g_object_unref (engine->priv->bk);
+    if (engine->priv->bk)
+	g_object_unref (engine->priv->bk);
 
-  g_object_unref (engine->priv->notify);
+    g_object_unref (engine->priv->notify);
 
-  G_OBJECT_CLASS (xfpm_engine_parent_class)->finalize (object);
+    G_OBJECT_CLASS (xfpm_engine_parent_class)->finalize (object);
 }
 
 XfpmEngine *
 xfpm_engine_new (void)
 {
-  XfpmEngine *engine = NULL;
-  engine = g_object_new (XFPM_TYPE_ENGINE, NULL);
+    XfpmEngine *engine = NULL;
+    engine = g_object_new (XFPM_TYPE_ENGINE, NULL);
 
-  return engine;
+    return engine;
 }
 
 void
@@ -450,16 +418,16 @@
 		      gboolean * can_hibernate,
 		      gboolean * has_lcd_brightness, gboolean * has_lid)
 {
-  g_return_if_fail (XFPM_IS_ENGINE (engine));
+    g_return_if_fail (XFPM_IS_ENGINE (engine));
 
-  g_object_get (G_OBJECT (engine->priv->iface),
-		"caller-privilege", user_privilege,
-		"can-suspend", can_suspend,
-		"can-hibernate", can_hibernate, NULL);
+    g_object_get (G_OBJECT (engine->priv->shutdown),
+		  "caller-privilege", user_privilege,
+		  "can-suspend", can_suspend,
+		  "can-hibernate", can_hibernate, NULL);
 
-  *system_laptop = engine->priv->is_laptop;
-  *has_lcd_brightness = engine->priv->has_lcd_brightness;
-  *has_lid = engine->priv->has_lid;
+    *system_laptop = engine->priv->is_laptop;
+    *has_lcd_brightness = engine->priv->has_lcd_brightness;
+    *has_lid = engine->priv->has_lid;
 }
 
 /*
@@ -495,120 +463,120 @@
 static void
 xfpm_engine_dbus_class_init (XfpmEngineClass * klass)
 {
-  dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (klass),
-				   &dbus_glib_xfpm_engine_object_info);
+    dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (klass),
+				     &dbus_glib_xfpm_engine_object_info);
 
-  dbus_g_error_domain_register (XFPM_ERROR,
-				"org.freedesktop.PowerManagement",
-				XFPM_TYPE_ERROR);
+    dbus_g_error_domain_register (XFPM_ERROR,
+				  "org.freedesktop.PowerManagement",
+				  XFPM_TYPE_ERROR);
 }
 
 static void
 xfpm_engine_dbus_init (XfpmEngine * engine)
 {
-  DBusGConnection *bus = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
+    DBusGConnection *bus = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
 
-  dbus_g_connection_register_g_object (bus,
-				       "/org/freedesktop/PowerManagement",
-				       G_OBJECT (engine));
+    dbus_g_connection_register_g_object (bus,
+				         "/org/freedesktop/PowerManagement",
+				         G_OBJECT (engine));
 }
 
 static gboolean
 xfpm_engine_dbus_hibernate (XfpmEngine * engine, GError ** error)
 {
-  TRACE ("Hibernate message received");
-  gboolean caller_privilege, can_hibernate;
+    TRACE ("Hibernate message received");
+    gboolean caller_privilege, can_hibernate;
 
-  g_object_get (G_OBJECT (engine->priv->iface),
-		"caller-privilege", &caller_privilege,
-		"can-hibernate", &can_hibernate, NULL);
+    g_object_get (G_OBJECT (engine->priv->shutdown),
+		  "caller-privilege", &caller_privilege,
+		  "can-hibernate", &can_hibernate, NULL);
 
-  if (!caller_privilege)
+    if (!caller_privilege)
     {
-      g_set_error (error, XFPM_ERROR, XFPM_ERROR_PERMISSION_DENIED,
-		   _("Permission denied"));
-      return FALSE;
+	g_set_error (error, XFPM_ERROR, XFPM_ERROR_PERMISSION_DENIED,
+		    _("Permission denied"));
+	return FALSE;
     }
 
-  if (!can_hibernate)
+    if (!can_hibernate)
     {
-      g_set_error (error, XFPM_ERROR, XFPM_ERROR_HIBERNATE_NOT_SUPPORTED,
-		   _("Hibernate not supported"));
-      return FALSE;
+	g_set_error (error, XFPM_ERROR, XFPM_ERROR_HIBERNATE_NOT_SUPPORTED,
+		    _("Hibernate not supported"));
+	return FALSE;
     }
 
-  xfpm_engine_shutdown_request (engine, XFPM_DO_HIBERNATE, FALSE);
+    xfpm_engine_shutdown_request (engine, XFPM_DO_HIBERNATE, FALSE);
 
-  return TRUE;
+    return TRUE;
 }
 
 static gboolean
 xfpm_engine_dbus_suspend (XfpmEngine * engine, GError ** error)
 {
-  TRACE ("Suspend message received");
-  gboolean caller_privilege, can_suspend;
+    TRACE ("Suspend message received");
+    gboolean caller_privilege, can_suspend;
 
-  g_object_get (G_OBJECT (engine->priv->iface),
-		"caller-privilege", &caller_privilege,
-		"can-suspend", &can_suspend, NULL);
+    g_object_get (G_OBJECT (engine->priv->shutdown),
+		  "caller-privilege", &caller_privilege,
+		  "can-suspend", &can_suspend, NULL);
 
-  if (!caller_privilege)
+    if (!caller_privilege)
     {
-      g_set_error (error, XFPM_ERROR, XFPM_ERROR_PERMISSION_DENIED,
-		   _("Permission denied"));
-      return FALSE;
+	g_set_error (error, XFPM_ERROR, XFPM_ERROR_PERMISSION_DENIED,
+		    _("Permission denied"));
+	return FALSE;
     }
 
-  if (!can_suspend)
+    if (!can_suspend)
     {
-      g_set_error (error, XFPM_ERROR, XFPM_ERROR_SUSPEND_NOT_SUPPORTED,
-		   _("Suspend not supported"));
-      return FALSE;
+	g_set_error (error, XFPM_ERROR, XFPM_ERROR_SUSPEND_NOT_SUPPORTED,
+		    _("Suspend not supported"));
+	return FALSE;
     }
 
-  xfpm_engine_shutdown_request (engine, XFPM_DO_SUSPEND, FALSE);
+    xfpm_engine_shutdown_request (engine, XFPM_DO_SUSPEND, FALSE);
 
-  return TRUE;
+    return TRUE;
 }
 
 static gboolean
 xfpm_engine_dbus_can_hibernate (XfpmEngine * engine,
 				gboolean * OUT_can_hibernate, GError ** error)
 {
-  TRACE ("Can hibernate message received");
-  g_object_get (G_OBJECT (engine->priv->iface),
-		"can-hibernate", OUT_can_hibernate, NULL);
+    TRACE ("Can hibernate message received");
+    g_object_get (G_OBJECT (engine->priv->shutdown),
+		    "can-hibernate", OUT_can_hibernate, NULL);
 
-  return TRUE;
+    return TRUE;
 }
 
 static gboolean
 xfpm_engine_dbus_can_suspend (XfpmEngine * engine,
 			      gboolean * OUT_can_suspend, GError ** error)
 {
-  TRACE ("Can suspend message received");
-  g_object_get (G_OBJECT (engine->priv->iface),
-		"can-suspend", OUT_can_suspend, NULL);
+    TRACE ("Can suspend message received");
+    g_object_get (G_OBJECT (engine->priv->shutdown),
+		  "can-suspend", OUT_can_suspend, NULL);
 
-  return TRUE;
+    return TRUE;
 }
 
 static gboolean
 xfpm_engine_dbus_get_on_battery (XfpmEngine * engine,
 				 gboolean * OUT_on_battery, GError ** error)
 {
-  TRACE ("On battery message received");
-  *OUT_on_battery = engine->priv->on_battery;
+    TRACE ("On battery message received");
+    *OUT_on_battery = engine->priv->on_battery;
 
-  return TRUE;
+    return TRUE;
 }
 
 static gboolean
 xfpm_engine_dbus_get_low_battery (XfpmEngine * engine,
 				  gboolean * OUT_low_battery, GError ** error)
 {
-  TRACE ("On low battery message received");
-  *OUT_low_battery = xfpm_supply_on_low_battery (engine->priv->supply);
+    TRACE ("On low battery message received");
+    *OUT_low_battery = xfpm_supply_on_low_battery (engine->priv->supply);
 
-  return TRUE;
+    return TRUE;
 }

Modified: xfce4-power-manager/trunk/src/xfpm-errors.c
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-errors.c	2009-03-29 12:43:11 UTC (rev 7025)
+++ xfce4-power-manager/trunk/src/xfpm-errors.c	2009-03-29 13:39:50 UTC (rev 7026)
@@ -50,6 +50,8 @@
 	    { XFPM_ERROR_HIBERNATE_NOT_SUPPORTED, "XFPM_ERROR_HIBERNATE_NOT_SUPPORTED", "HibernateNotSupported" },
 	    { XFPM_ERROR_SUSPEND_HYBRID_NOT_SUPPORTED, "XFPM_ERROR_SUSPEND_HYBRID_NOT_SUPPORTED", "SuspendHybridNotSupported" },
 	    { XFPM_ERROR_INVALID_COOKIE, "XFPM_ERROR_INVALID_COOKIE", "InvalidCookie" },
+	    { XFPM_ERROR_HAL_DISCONNECTED, "XFPM_ERROR_HAL_DISCONNECTED", "HalDisconnected" },
+	    { XFPM_ERROR_SLEEP_FAILED, "XFPM_ERROR_SLEEP_FAILED", "SleepFailed" },
 	    { 0, NULL, NULL }
 	};
 	

Modified: xfce4-power-manager/trunk/src/xfpm-errors.h
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-errors.h	2009-03-29 12:43:11 UTC (rev 7025)
+++ xfce4-power-manager/trunk/src/xfpm-errors.h	2009-03-29 13:39:50 UTC (rev 7026)
@@ -36,7 +36,9 @@
     XFPM_ERROR_SUSPEND_NOT_SUPPORTED,
     XFPM_ERROR_HIBERNATE_NOT_SUPPORTED,
     XFPM_ERROR_SUSPEND_HYBRID_NOT_SUPPORTED,
-    XFPM_ERROR_INVALID_COOKIE
+    XFPM_ERROR_INVALID_COOKIE,
+    XFPM_ERROR_HAL_DISCONNECTED,
+    XFPM_ERROR_SLEEP_FAILED
     
 } XfpmError;
 

Added: xfce4-power-manager/trunk/src/xfpm-shutdown.c
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-shutdown.c	                        (rev 0)
+++ xfce4-power-manager/trunk/src/xfpm-shutdown.c	2009-03-29 13:39:50 UTC (rev 7026)
@@ -0,0 +1,430 @@
+/*
+ * * Copyright (C) 2009 Ali <aliov at xfce.org>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+
+#include <libxfce4util/libxfce4util.h>
+
+#include <glib.h>
+
+#include "libxfpm/hal-monitor.h"
+#include "libxfpm/hal-device.h"
+#include "libxfpm/hal-monitor.h"
+
+#include "libxfpm/xfpm-string.h"
+#include "xfpm-shutdown.h"
+#include "xfpm-errors.h"
+
+/* Init */
+static void xfpm_shutdown_class_init (XfpmShutdownClass *klass);
+static void xfpm_shutdown_init       (XfpmShutdown *shutdown);
+static void xfpm_shutdown_finalize   (GObject *object);
+
+static void xfpm_shutdown_get_property (GObject *object,
+				        guint prop_id,
+				        GValue *value,
+				        GParamSpec *pspec);
+
+#define XFPM_SHUTDOWN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), XFPM_TYPE_SHUTDOWN, XfpmShutdownPrivate))
+
+struct XfpmShutdownPrivate
+{
+    DBusGConnection *bus;
+    HalMonitor      *monitor;
+
+    gboolean         connected;
+    gboolean         can_suspend;
+    gboolean         can_hibernate;
+    gboolean         caller_privilege;
+    
+    gboolean         block_shutdown;
+};
+
+enum
+{
+    PROP_0,
+    PROP_CALLER_PRIVILEGE,
+    PROP_CAN_SUSPEND,
+    PROP_CAN_HIBERNATE,
+};
+
+static gpointer xfpm_shutdown_object = NULL;
+
+G_DEFINE_TYPE(XfpmShutdown, xfpm_shutdown, G_TYPE_OBJECT)
+
+static gboolean
+xfpm_shutdown_check_interface (XfpmShutdown *shutdown, const gchar *interface)
+{
+    DBusMessage *message;
+    DBusMessage *reply;
+    DBusError error ;
+    
+    message = dbus_message_new_method_call ("org.freedesktop.Hal",
+					    "/org/freedesktop/Hal",
+					    interface,
+					    "JustToCheck");
+    
+    if (!message)
+    	return FALSE;
+    
+    dbus_error_init (&error);
+    
+    reply = 
+    	dbus_connection_send_with_reply_and_block (dbus_g_connection_get_connection(shutdown->priv->bus),
+						   message, 2000, &error);
+    dbus_message_unref (message);
+    
+    if ( reply ) dbus_message_unref (reply);
+    
+    if ( dbus_error_is_set(&error) )
+    {
+	if ( !g_strcmp0 (error.name, "org.freedesktop.DBus.Error.UnknownMethod") )
+        {
+            dbus_error_free(&error);
+	    return TRUE;
+        }
+    }
+    return FALSE;
+}
+
+static void
+xfpm_shutdown_power_management_check (XfpmShutdown *shutdown)
+{
+    HalDevice *device;
+    device = hal_device_new ();
+    hal_device_set_udi (device,  "/org/freedesktop/Hal/devices/computer");
+    
+    shutdown->priv->caller_privilege =
+	xfpm_shutdown_check_interface (shutdown,  "org.freedesktop.Hal.Device.SystemPowerManagement");
+    
+    shutdown->priv->can_suspend   = hal_device_get_property_bool (device, "power_management.can_suspend");
+    shutdown->priv->can_hibernate = hal_device_get_property_bool (device, "power_management.can_hibernate");
+    g_object_unref (device);
+}
+
+static void
+xfpm_shutdown_connection_changed_cb (HalMonitor *monitor, gboolean connected, XfpmShutdown *shutdown)
+{
+    TRACE ("Hal connection changed=%s", xfpm_bool_to_string (connected));
+    shutdown->priv->connected = connected;
+}
+
+static void
+xfpm_shutdown_class_init(XfpmShutdownClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
+
+    object_class->get_property = xfpm_shutdown_get_property;
+    g_object_class_install_property(object_class,
+				    PROP_CALLER_PRIVILEGE,
+				    g_param_spec_boolean("caller-privilege",
+							 NULL, NULL,
+							 FALSE,
+							 G_PARAM_READABLE));
+							 
+    g_object_class_install_property(object_class,
+				    PROP_CAN_SUSPEND,
+				    g_param_spec_boolean("can-suspend",
+							 NULL, NULL,
+							 FALSE,
+							 G_PARAM_READABLE));
+							 
+    g_object_class_install_property(object_class,
+				    PROP_CAN_HIBERNATE,
+				    g_param_spec_boolean("can-hibernate",
+							 NULL, NULL,
+							 FALSE,
+							 G_PARAM_READABLE));
+							 
+    object_class->finalize = xfpm_shutdown_finalize;
+    g_type_class_add_private(klass,sizeof(XfpmShutdownPrivate));
+}
+
+static void
+xfpm_shutdown_init (XfpmShutdown *shutdown)
+{
+    GError *error = NULL;
+    shutdown->priv = XFPM_SHUTDOWN_GET_PRIVATE(shutdown);
+    
+    shutdown->priv->bus 			= NULL;
+    shutdown->priv->connected			= FALSE;
+    shutdown->priv->can_suspend    		= FALSE;
+    shutdown->priv->can_hibernate  		= FALSE;
+    shutdown->priv->caller_privilege        	= FALSE;
+    
+    shutdown->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
+    
+    if ( error )
+    {
+	g_critical ("Unable to get system bus %s.", error->message);
+	g_error_free (error);
+	return;
+    }
+    
+    shutdown->priv->monitor = hal_monitor_new ();
+    g_signal_connect (shutdown->priv->monitor, "connection-changed",
+		      G_CALLBACK (xfpm_shutdown_connection_changed_cb), shutdown);
+    shutdown->priv->connected = hal_monitor_get_connected (shutdown->priv->monitor);
+    
+    xfpm_shutdown_power_management_check (shutdown);
+}
+
+static void xfpm_shutdown_get_property (GObject *object,
+				        guint prop_id,
+				        GValue *value,
+				        GParamSpec *pspec)
+{
+    XfpmShutdown *shutdown;
+    shutdown = XFPM_SHUTDOWN (object);
+
+    switch(prop_id)
+    {
+	case PROP_CALLER_PRIVILEGE:
+	    g_value_set_boolean (value, shutdown->priv->caller_privilege );
+	    break;
+	case PROP_CAN_SUSPEND:
+	    g_value_set_boolean (value, shutdown->priv->can_suspend );
+	    break;
+	case PROP_CAN_HIBERNATE:
+	    g_value_set_boolean (value, shutdown->priv->can_hibernate);
+	    break;
+	default:
+	    G_OBJECT_WARN_INVALID_PROPERTY_ID(object,prop_id,pspec);
+	    break;
+    }
+}
+
+static void
+xfpm_shutdown_finalize(GObject *object)
+{
+    XfpmShutdown *shutdown;
+
+    shutdown = XFPM_SHUTDOWN(object);
+    
+    if ( shutdown->priv->bus )
+	dbus_g_connection_unref (shutdown->priv->bus);	
+
+    if ( shutdown->priv->monitor )
+	g_object_unref (shutdown->priv->monitor);
+	
+    G_OBJECT_CLASS(xfpm_shutdown_parent_class)->finalize(object);
+}
+
+gboolean xfpm_shutdown_internal (DBusConnection *bus, const gchar *shutdown, GError **gerror)
+{
+    DBusMessage *message, *reply = NULL;
+    DBusError error;
+    gint exit_code;
+    
+    message = dbus_message_new_method_call ("org.freedesktop.Hal",
+					    "/org/freedesktop/Hal/devices/computer",
+					    "org.freedesktop.Hal.Device.SystemPowerManagement",
+					    shutdown);
+    if ( !message )
+    {
+	g_set_error ( gerror, 0, 0, "Out of memory");
+	return FALSE;
+    }
+    
+    if ( !g_strcmp0("Suspend", shutdown ) )
+    {
+	gint seconds = 0;
+    	dbus_message_append_args (message, DBUS_TYPE_INT32, &seconds, DBUS_TYPE_INVALID);
+    }
+    
+    dbus_error_init (&error);
+    
+    reply = dbus_connection_send_with_reply_and_block (bus,
+						       message,
+						       -1,
+						       &error);
+    dbus_message_unref (message);
+
+    if ( dbus_error_is_set(&error) )
+    {
+	dbus_set_g_error (gerror, &error);
+	return FALSE;
+    }
+    
+    switch (dbus_message_get_type(reply) )
+    {
+	case DBUS_MESSAGE_TYPE_METHOD_RETURN:
+		dbus_message_get_args (reply, NULL,
+				       DBUS_TYPE_INT32,
+				       &exit_code,
+				       DBUS_TYPE_INVALID);
+		dbus_message_unref (reply);
+		if ( exit_code == 0) return TRUE;
+		else
+		{
+		    g_set_error (gerror, 0, 0, "System failed to sleep");
+		    return FALSE;
+		}
+		break;
+	case DBUS_MESSAGE_TYPE_ERROR:
+		dbus_message_unref (reply);
+		g_set_error ( gerror, 0, 0, "Failed to sleep");
+		return FALSE;
+		break;
+	default:
+		dbus_message_unref ( reply );
+		g_set_error ( gerror, 0, 0, "Failed to sleep");
+		return FALSE;
+    }
+    return TRUE;
+}
+
+static const gchar *
+_filter_error_message(const gchar *error)
+{
+    if( xfpm_strequal ("No back-end for your operating system", error))
+    {
+        return _("No back-end for your operating system");
+    }
+    else if ( xfpm_strequal ("No hibernate script found", error) )
+    {
+        return _("No hibernate script found");
+    }
+    else if ( xfpm_strequal ("No suspend script found", error) )
+    {
+	return _("No suspend script found");
+    }
+    else if ( xfpm_strequal ("No suspend method found", error) )
+    {
+        return _("No suspend method found");
+    }
+    else if ( xfpm_strequal ("No hibernate method found", error))
+    {
+        return _("No hibernate method found");
+    }
+    else if ( xfpm_strequal ("Out of memory", error) )
+    {
+	return _("Out of memory");
+    }
+    else if ( xfpm_strequal ("System failed to sleep", error ) )
+    {
+	return _("System failed to sleep");
+    }
+    return NULL;
+}
+
+XfpmShutdown *
+xfpm_shutdown_new(void)
+{
+    if ( xfpm_shutdown_object != NULL )
+    {
+	g_object_ref (xfpm_shutdown_object);
+    }
+    else
+    {
+	xfpm_shutdown_object = g_object_new (XFPM_TYPE_SHUTDOWN, NULL);
+	g_object_add_weak_pointer (xfpm_shutdown_object, &xfpm_shutdown_object);
+    }
+    return XFPM_SHUTDOWN (xfpm_shutdown_object);
+}
+
+void xfpm_shutdown	(XfpmShutdown *shutdown, GError **error)
+{
+    g_return_if_fail (XFPM_IS_SHUTDOWN(shutdown));
+    
+    if ( G_UNLIKELY (shutdown->priv->connected == FALSE) )
+    {
+	g_set_error (error, XFPM_ERROR, XFPM_ERROR_HAL_DISCONNECTED, _("Hal daemon is currently not connected"));
+	return;
+    }
+    
+    if ( shutdown->priv->block_shutdown )
+	return;
+    xfpm_shutdown_internal (dbus_g_connection_get_connection(shutdown->priv->bus), "Shutdown", NULL);
+}
+
+void xfpm_hibernate (XfpmShutdown *shutdown, GError **error)
+{
+    GError *error_internal = NULL;
+    const gchar *error_message;
+    
+    g_return_if_fail (XFPM_IS_SHUTDOWN(shutdown));
+    
+    if ( G_UNLIKELY (shutdown->priv->connected == FALSE) )
+    {
+	g_set_error (error, XFPM_ERROR, XFPM_ERROR_HAL_DISCONNECTED, _("Hal daemon is currently not connected"));
+	return;
+    }
+
+    if ( shutdown->priv->block_shutdown )
+	return;
+
+    shutdown->priv->block_shutdown = TRUE;
+    xfpm_shutdown_internal (dbus_g_connection_get_connection(shutdown->priv->bus), "Hibernate", &error_internal);
+    shutdown->priv->block_shutdown = FALSE;
+     
+    if ( error_internal )
+    {
+	g_warning ("%s", error_internal->message);
+	error_message = _filter_error_message (error_internal->message);
+	if ( error_message )
+	{
+	    g_set_error (error, XFPM_ERROR, XFPM_ERROR_SLEEP_FAILED, "%s", error_message);
+	}
+	g_error_free (error_internal);
+    }
+}
+
+void xfpm_suspend (XfpmShutdown *shutdown, GError **error)
+{
+    GError *error_internal = NULL;
+    const gchar *error_message;
+    
+    g_return_if_fail (XFPM_IS_SHUTDOWN(shutdown));
+    
+    if ( G_UNLIKELY (shutdown->priv->connected == FALSE) )
+    {
+	g_set_error (error, XFPM_ERROR, XFPM_ERROR_HAL_DISCONNECTED, _("Hal daemon is currently not connected"));
+	return;
+    }
+    
+    if ( shutdown->priv->block_shutdown )
+	return;
+
+    shutdown->priv->block_shutdown = TRUE;
+    xfpm_shutdown_internal (dbus_g_connection_get_connection(shutdown->priv->bus), "Suspend", &error_internal);
+    shutdown->priv->block_shutdown = FALSE;
+     
+    if ( error_internal )
+    {
+	g_warning ("%s", error_internal->message);
+	error_message = _filter_error_message (error_internal->message);
+	if ( error_message )
+	{
+	    g_set_error ( error, XFPM_ERROR, XFPM_ERROR_SLEEP_FAILED, "%s", error_message);
+	}
+	g_error_free (error_internal);
+    }
+}

Added: xfce4-power-manager/trunk/src/xfpm-shutdown.h
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-shutdown.h	                        (rev 0)
+++ xfce4-power-manager/trunk/src/xfpm-shutdown.h	2009-03-29 13:39:50 UTC (rev 7026)
@@ -0,0 +1,69 @@
+/*
+ * * Copyright (C) 2009 Ali <aliov at xfce.org>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef __XFPM_SHUTDOWN_H
+#define __XFPM_SHUTDOWN_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define XFPM_TYPE_SHUTDOWN        (xfpm_shutdown_get_type () )
+#define XFPM_SHUTDOWN(o)          (G_TYPE_CHECK_INSTANCE_CAST((o), XFPM_TYPE_SHUTDOWN, XfpmShutdown))
+#define XFPM_IS_SHUTDOWN(o)       (G_TYPE_CHECK_INSTANCE_TYPE((o), XFPM_TYPE_SHUTDOWN))
+
+typedef enum
+{
+    SYSTEM_CAN_HIBERNATE         =  (1<<0),
+    SYSTEM_CAN_SUSPEND           =  (1<<1),
+    
+} SystemPowerManagement;
+
+typedef struct XfpmShutdownPrivate XfpmShutdownPrivate;
+
+typedef struct
+{
+    GObject		  parent;
+    XfpmShutdownPrivate	 *priv;
+	
+} XfpmShutdown;
+
+typedef struct
+{
+    GObjectClass 	  parent_class;
+	
+} XfpmShutdownClass;
+
+GType        		  xfpm_shutdown_get_type        (void) G_GNUC_CONST;
+XfpmShutdown       	 *xfpm_shutdown_new             (void);
+
+void                      xfpm_shutdown			(XfpmShutdown *shutdown,
+							 GError **error);
+
+void                      xfpm_hibernate                (XfpmShutdown *shutdown,
+							 GError **error);
+
+void                      xfpm_suspend                  (XfpmShutdown *shutdown,
+							 GError **error);
+
+
+G_END_DECLS
+
+#endif /* __XFPM_SHUTDOWN_H */

Modified: xfce4-power-manager/trunk/src/xfpm-supply.c
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-supply.c	2009-03-29 12:43:11 UTC (rev 7025)
+++ xfce4-power-manager/trunk/src/xfpm-supply.c	2009-03-29 13:39:50 UTC (rev 7026)
@@ -31,7 +31,6 @@
 #include <libxfcegui4/libxfcegui4.h>
 
 #include "libxfpm/hal-power.h"
-#include "libxfpm/hal-iface.h"
 #include "libxfpm/xfpm-string.h"
 #include "libxfpm/xfpm-common.h"
 #include "libxfpm/xfpm-notify.h"
@@ -44,6 +43,7 @@
 #include "xfpm-xfconf.h"
 #include "xfpm-tray-icon.h"
 #include "xfpm-config.h"
+#include "xfpm-shutdown.h"
 #include "xfpm-marshal.h"
 
 /* Init */

Modified: xfce4-power-manager/trunk/src/xfpm-tray-icon.c
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-tray-icon.c	2009-03-29 12:43:11 UTC (rev 7025)
+++ xfce4-power-manager/trunk/src/xfpm-tray-icon.c	2009-03-29 13:39:50 UTC (rev 7026)
@@ -33,11 +33,12 @@
 #include <libxfcegui4/libxfcegui4.h>
 
 #include "libxfpm/xfpm-common.h"
-#include "libxfpm/hal-iface.h"
 #include "libxfpm/xfpm-string.h"
+#include "libxfpm/xfpm-notify.h"
 
 #include "xfpm-tray-icon.h"
 #include "xfpm-network-manager.h"
+#include "xfpm-shutdown.h"
 #include "xfpm-xfconf.h"
 #include "xfpm-config.h"
 
@@ -51,8 +52,10 @@
 
 struct XfpmTrayIconPrivate
 {
-    HalIface      *iface;
+    XfpmShutdown  *shutdown;
     XfpmXfconf    *conf;
+    XfpmNotify    *notify;
+    
     GtkStatusIcon *icon;
     GQuark         icon_quark;
 };
@@ -99,13 +102,22 @@
 {
     GError *error = NULL;
 
-    hal_iface_shutdown (tray->priv->iface, "Suspend", &error);
+    xfpm_suspend (tray->priv->shutdown, &error);
 
     if (error)
     {
 	g_warning ("%s", error->message);
+	xfpm_notify_show_notification (tray->priv->notify, 
+				      _("Xfce power manager"), 
+				       error->message, 
+				       xfpm_tray_icon_get_icon_name (tray),
+				       10000,
+				       FALSE,
+				       XFPM_NOTIFY_CRITICAL,
+				       tray->priv->icon);
 	g_error_free (error);
     }
+    xfpm_send_message_to_network_manager ("wake");
     return FALSE;
 }
 
@@ -114,13 +126,22 @@
 {
     GError *error = NULL;
 
-    hal_iface_shutdown (tray->priv->iface, "Hibernate", &error);
-
+    xfpm_hibernate (tray->priv->shutdown, &error);
+    
     if (error)
     {
 	g_warning ("%s", error->message);
+	xfpm_notify_show_notification (tray->priv->notify, 
+				      _("Xfce power manager"), 
+				       error->message, 
+				       xfpm_tray_icon_get_icon_name (tray),
+				       10000,
+				       FALSE,
+				       XFPM_NOTIFY_CRITICAL,
+				       tray->priv->icon);
 	g_error_free (error);
     }
+    xfpm_send_message_to_network_manager ("wake");
     return FALSE;
 }
 
@@ -169,9 +190,11 @@
     		  
     GtkWidget *menu, *mi, *img;
     menu = gtk_menu_new();
-    gboolean can_suspend, can_hibernate, caller;
+    gboolean can_suspend = FALSE;
+    gboolean can_hibernate = FALSE ;
+    gboolean caller = FALSE;
 
-    g_object_get (G_OBJECT (tray->priv->iface),
+    g_object_get (G_OBJECT (tray->priv->shutdown),
 		  "caller-privilege", &caller,
 		  "can-suspend", &can_suspend,
 		  "can-hibernate", &can_hibernate,
@@ -283,8 +306,9 @@
     tray->priv = XFPM_TRAY_ICON_GET_PRIVATE(tray);
     
     tray->priv->icon  = gtk_status_icon_new();
-    tray->priv->iface = hal_iface_new ();
+    tray->priv->shutdown = xfpm_shutdown_new ();
     tray->priv->conf  = xfpm_xfconf_new ();
+    tray->priv->notify = xfpm_notify_new ();
     
     tray->priv->icon_quark = 0;
     
@@ -304,9 +328,11 @@
 
     g_object_unref (tray->priv->icon);
 	
-    g_object_unref (tray->priv->iface);
+    g_object_unref (tray->priv->shutdown);
     
     g_object_unref (tray->priv->conf);
+    
+    g_object_unref (tray->priv->notify);
 
     G_OBJECT_CLASS(xfpm_tray_icon_parent_class)->finalize(object);
 }




More information about the Goodies-commits mailing list