[Xfce4-commits] [xfce/xfce4-power-manager] 01/03: port xfpm to libupower-glib / add support for upower-0.99.0 API

noreply at xfce.org noreply at xfce.org
Sun Apr 13 19:35:28 CEST 2014


This is an automated email from the git hooks/post-receive script.

eric pushed a commit to branch master
in repository xfce/xfce4-power-manager.

commit ae97be6f3500eea509d61c914e22c5355e7d57de
Author: Stefan Seyfried <seife+dev at b1-systems.com>
Date:   Sun Apr 13 12:05:19 2014 +0200

    port xfpm to libupower-glib / add support for upower-0.99.0 API
    
    * port most of the upower interfaces to upower-glib
    * conditionalize stuff that has been deprecated in upower >= 0.99.0
    * suspend / hibernate is handled by systemd/logind
    * if upower is >= 0.99.0 and logind is not running, log errors
      (as functionality will be missing)
    
    Signed-off-by: Eric Koegel <eric.koegel at gmail.com>
---
 configure.ac.in         |    2 +
 src/Makefile.am         |    4 +
 src/xfpm-battery.c      |  136 ++++++++----------------
 src/xfpm-battery.h      |    3 +-
 src/xfpm-power-common.c |   79 --------------
 src/xfpm-power-common.h |   13 +--
 src/xfpm-power-info.c   |  220 ++++++++++++++-------------------------
 src/xfpm-power.c        |  265 ++++++++++++++++-------------------------------
 8 files changed, 225 insertions(+), 497 deletions(-)

diff --git a/configure.ac.in b/configure.ac.in
index 7a4af87..527f399 100644
--- a/configure.ac.in
+++ b/configure.ac.in
@@ -64,6 +64,7 @@ m4_define([libxfce4util_minimum_version],[4.10.0])
 m4_define([libxfce4panel_minimum_version],[4.10.0])
 
 m4_define([libnotify_minimum_version], [0.4.1])
+m4_define([upower_minimum_version], [0.9.8])
 m4_define([xrandr_minimum_version], [1.2.0])
 m4_define([x11_minimum_version], [1.0.0])
 
@@ -78,6 +79,7 @@ XDT_CHECK_PACKAGE([XFCONF], [libxfconf-0],[xfconf_minimum_version])
 XDT_CHECK_PACKAGE([LIBXFCE4UI], [libxfce4ui-1],[libxfce4ui_minimum_version])
 XDT_CHECK_PACKAGE([LIBXFCE4UTIL], [libxfce4util-1.0],[libxfce4util_minimum_version])
 XDT_CHECK_PACKAGE([LIBNOTIFY],[libnotify], [libnotify_minimum_version])
+XDT_CHECK_PACKAGE([UPOWER],[upower-glib], [upower_minimum_version])
 XDT_CHECK_PACKAGE([XRANDR],[xrandr], [xrandr_minimum_version])
 XDT_CHECK_PACKAGE([X11], [x11], [x11_minimum_version])
 
diff --git a/src/Makefile.am b/src/Makefile.am
index 316a823..9899733 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -56,6 +56,7 @@ xfce4_power_manager_CFLAGS =                    \
 	$(LIBXFCE4UI_CFLAGS)                    \
 	$(XFCONF_CFLAGS)                        \
 	$(LIBNOTIFY_CFLAGS)                     \
+	$(UPOWER_CFLAGS)                        \
 	$(XRANDR_CFLAGS)			\
 	$(DPMS_CFLAGS)           		\
 	$(PLATFORM_CPPFLAGS)			\
@@ -74,6 +75,7 @@ xfce4_power_manager_LDADD =                     \
 	$(LIBXFCE4UI_LIBS)                      \
 	$(XFCONF_LIBS)                          \
 	$(LIBNOTIFY_LIBS)                       \
+	$(UPOWER_LIBS)                          \
 	$(XRANDR_LIBS)				\
 	$(DPMS_LIBS)
 
@@ -90,6 +92,7 @@ xfce4_power_information_CFLAGS =		\
 	-DG_LOG_DOMAIN=\"xfce4-power-information\"\
 	$(GOBJECT_CFLAGS)                       \
 	$(DBUS_GLIB_CFLAGS)                     \
+	$(UPOWER_CFLAGS)                        \
 	$(LIBXFCE4UI_CFLAGS)			\
 	$(PLATFORM_CPPFLAGS)			\
 	$(PLATFORM_CFLAGS)
@@ -101,6 +104,7 @@ xfce4_power_information_LDFLAGS =		\
 xfce4_power_information_LDADD =			\
 	$(GOBJECT_LIBS)                         \
 	$(DBUS_GLIB_LIBS)                       \
+	$(UPOWER_LIBS)                          \
 	$(LIBXFCE4UI_LIBS)			\
 	$(top_builddir)/libdbus/libxfpmdbus.la
 
diff --git a/src/xfpm-battery.c b/src/xfpm-battery.c
index 2034c60..9d1b590 100644
--- a/src/xfpm-battery.c
+++ b/src/xfpm-battery.c
@@ -27,6 +27,7 @@
 #include <string.h>
 
 #include <gtk/gtk.h>
+#include <upower.h>
 
 #include <libxfce4util/libxfce4util.h>
 
@@ -53,8 +54,8 @@ struct XfpmBatteryPrivate
     XfpmXfconf             *conf;
     XfpmNotify		   *notify;
     XfpmButton             *button;
-    DBusGProxy             *proxy;
-    DBusGProxy 		   *proxy_prop;
+    UpDevice               *device;
+    UpClient               *client;
 
     gchar		   *icon_prefix;
 
@@ -71,6 +72,7 @@ struct XfpmBatteryPrivate
 
     gulong		    sig;
     gulong		    sig_bt;
+    gulong		    sig_up;
 
     guint                   notify_idle;
 };
@@ -525,45 +527,28 @@ xfpm_battery_check_charge (XfpmBattery *battery)
 }
 
 static void
-xfpm_battery_refresh (XfpmBattery *battery, GHashTable *props)
+xfpm_battery_refresh (XfpmBattery *battery, UpDevice *device)
 {
-    GValue *value;
+    gboolean present;
     guint state;
-
-    value = g_hash_table_lookup (props, "IsPresent");
-
-    if ( value == NULL )
-    {
-	g_warning ("No 'IsPresent' property found");
-	goto out;
-    }
-
-    battery->priv->present = g_value_get_boolean (value);
-
-    value = g_hash_table_lookup (props, "State");
-
-    if ( value == NULL )
-    {
-	g_warning ("No 'State' property found");
-    }
-
-    state = g_value_get_uint (value);
+    gdouble percentage;
+    guint64 to_empty, to_full;
+    g_object_get(device,
+		 "is-present", &present,
+		 "percentage", &percentage,
+		 "state", &state,
+		 "time-to-empty", &to_empty,
+		 "time-to-full", &to_full,
+		 NULL);
+
+    battery->priv->present = present;
     if ( state != battery->priv->state )
     {
 	battery->priv->state = state;
 	xfpm_battery_refresh_visible (battery);
 	xfpm_battery_notify_state (battery);
     }
-
-    value = g_hash_table_lookup (props, "Percentage");
-
-    if ( value == NULL )
-    {
-	g_warning ("No 'Percentage' property found on battery device");
-	goto out;
-    }
-
-    battery->priv->percentage = (guint) g_value_get_double (value);
+    battery->priv->percentage = (guint) percentage;
 
     xfpm_battery_check_charge (battery);
 
@@ -572,29 +557,9 @@ xfpm_battery_refresh (XfpmBattery *battery, GHashTable *props)
     if ( battery->priv->type == XFPM_DEVICE_TYPE_BATTERY ||
 	 battery->priv->type == XFPM_DEVICE_TYPE_UPS )
     {
-	value = g_hash_table_lookup (props, "TimeToEmpty");
-
-	if ( value == NULL )
-	{
-	    g_warning ("No 'TimeToEmpty' property found on battery device");
-	    goto out;
-	}
-
-	battery->priv->time_to_empty = g_value_get_int64 (value);
-
-	value = g_hash_table_lookup (props, "TimeToFull");
-
-	if ( value == NULL )
-	{
-	    g_warning ("No 'TimeToFull' property found on battery device");
-	    goto out;
-	}
-
-	battery->priv->time_to_full = g_value_get_int64 (value);
+	battery->priv->time_to_empty = to_empty;
+	battery->priv->time_to_full  = to_empty;
     }
-
-    out:
-	g_hash_table_destroy (props);
 }
 
 static void
@@ -605,30 +570,13 @@ xfpm_battery_button_pressed_cb (XfpmButton *button, XfpmButtonKey type, XfpmBatt
 }
 
 static void
-xfpm_battery_changed_cb (DBusGProxy *proxy, XfpmBattery *battery)
+xfpm_battery_changed_cb (UpDevice *device,
+#if UP_CHECK_VERSION(0, 99, 0)
+			 GParamSpec *pspec,
+#endif
+			 XfpmBattery *battery)
 {
-    GHashTable *props;
-    GValue *value;
-    const gchar *cstr;
-    const gchar *p;
-
-    props = xfpm_power_get_interface_properties (battery->priv->proxy_prop,
-						 UPOWER_IFACE_DEVICE);
-
-    value = g_hash_table_lookup (props, "NativePath");
-    if ( value )
-    {
-	cstr = g_value_get_string (value);
-	p = strrchr (cstr, '/');
-	if ( p && (strncmp( p, "/hid-", 5 ) == 0) )
-	{
-	    XFPM_DEBUG("Ignoring battery '%s' - is a HID device\n", cstr);
-	    return;
-	}
-    }
-
-    if ( props )
-	xfpm_battery_refresh (battery, props);
+    xfpm_battery_refresh (battery, device);
 }
 
 static gboolean
@@ -760,7 +708,8 @@ xfpm_battery_init (XfpmBattery *battery)
 
     battery->priv->conf          = xfpm_xfconf_new ();
     battery->priv->notify        = xfpm_notify_new ();
-    battery->priv->proxy_prop    = NULL;
+    battery->priv->device        = NULL;
+    battery->priv->client        = NULL;
     battery->priv->state         = XFPM_DEVICE_STATE_UNKNOWN;
     battery->priv->type          = XFPM_DEVICE_TYPE_UNKNOWN;
     battery->priv->charge        = XFPM_BATTERY_CHARGE_UNKNOWN;
@@ -790,8 +739,8 @@ xfpm_battery_finalize (GObject *object)
     if (battery->priv->notify_idle != 0)
         g_source_remove (battery->priv->notify_idle);
 
-    dbus_g_proxy_disconnect_signal (battery->priv->proxy, "Changed",
-				    G_CALLBACK (xfpm_battery_changed_cb), battery);
+    if ( g_signal_handler_is_connected (battery->priv->device, battery->priv->sig_up ) )
+	g_signal_handler_disconnect (G_OBJECT (battery->priv->device), battery->priv->sig_up);
 
     if ( g_signal_handler_is_connected (battery->priv->conf, battery->priv->sig ) )
 	g_signal_handler_disconnect (G_OBJECT (battery->priv->conf), battery->priv->sig);
@@ -799,8 +748,7 @@ xfpm_battery_finalize (GObject *object)
      if ( g_signal_handler_is_connected (battery->priv->button, battery->priv->sig_bt ) )
 	g_signal_handler_disconnect (G_OBJECT (battery->priv->button), battery->priv->sig_bt);
 
-    g_object_unref (battery->priv->proxy);
-    g_object_unref (battery->priv->proxy_prop);
+    g_object_unref (battery->priv->device);
     g_object_unref (battery->priv->conf);
     g_object_unref (battery->priv->notify);
     g_object_unref (battery->priv->button);
@@ -884,26 +832,28 @@ xfpm_battery_new (void)
 }
 
 void xfpm_battery_monitor_device (XfpmBattery *battery,
-				  DBusGProxy *proxy,
-				  DBusGProxy *proxy_prop,
+				  const char *object_path,
 				  XfpmDeviceType device_type)
 {
+    UpDevice *device;
     battery->priv->type = device_type;
-    battery->priv->proxy_prop = proxy_prop;
-    battery->priv->proxy = proxy;
+    battery->priv->client = up_client_new();
     battery->priv->icon_prefix = xfpm_battery_get_icon_prefix_device_enum_type (device_type);
     battery->priv->battery_name = xfpm_battery_get_name (device_type);
 
-
-    dbus_g_proxy_add_signal (proxy, "Changed", G_TYPE_INVALID);
-    dbus_g_proxy_connect_signal (proxy, "Changed",
-				 G_CALLBACK (xfpm_battery_changed_cb), battery, NULL);
-
+    device = up_device_new();
+    up_device_set_object_path_sync (device, object_path, NULL, NULL);
+    battery->priv->device = device;
+#if UP_CHECK_VERSION(0, 99, 0)
+    battery->priv->sig_up = g_signal_connect (battery->priv->device, "notify", G_CALLBACK (xfpm_battery_changed_cb), battery);
+#else
+    battery->priv->sig_up = g_signal_connect (battery->priv->device, "changed", G_CALLBACK (xfpm_battery_changed_cb), battery);
+#endif
     g_object_set (G_OBJECT (battery),
 		  "has-tooltip", TRUE,
 		  NULL);
 
-    xfpm_battery_changed_cb (proxy, battery);
+    xfpm_battery_refresh (battery, device);
 }
 
 XfpmDeviceType xfpm_battery_get_device_type (XfpmBattery *battery)
diff --git a/src/xfpm-battery.h b/src/xfpm-battery.h
index 4d4ca34..a3b57cf 100644
--- a/src/xfpm-battery.h
+++ b/src/xfpm-battery.h
@@ -56,8 +56,7 @@ GType        		    xfpm_battery_get_type        (void) G_GNUC_CONST;
 GtkStatusIcon              *xfpm_battery_new             (void);
 
 void			    xfpm_battery_monitor_device  (XfpmBattery *battery,
-							  DBusGProxy *proxy,
-							  DBusGProxy *proxy_prop,
+							  const char *object_path,
 							  XfpmDeviceType device_type);
 
 XfpmDeviceType	   	    xfpm_battery_get_device_type (XfpmBattery *battery);
diff --git a/src/xfpm-power-common.c b/src/xfpm-power-common.c
index 39ecc18..d2c4ab5 100644
--- a/src/xfpm-power-common.c
+++ b/src/xfpm-power-common.c
@@ -31,85 +31,6 @@
 #include "xfpm-icons.h"
 
 /**
- * xfpm_power_enumerate_devices:
- * 
- **/
-GPtrArray *
-xfpm_power_enumerate_devices (DBusGProxy *proxy)
-{
-    gboolean ret;
-    GError *error = NULL;
-    GPtrArray *array = NULL;
-    GType g_type_array;
-
-    g_type_array = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH);
-    
-    ret = dbus_g_proxy_call (proxy, "EnumerateDevices", &error,
-			     G_TYPE_INVALID,
-			     g_type_array, &array,
-			     G_TYPE_INVALID);
-    if (!ret) 
-    {
-	g_critical ("Couldn't enumerate power devices: %s", error->message);
-	g_error_free (error);
-    }
-    
-    return array;
-}
-
-/**
- * xfpm_power_get_interface_properties:
- * 
- **/
-GHashTable *xfpm_power_get_interface_properties (DBusGProxy *proxy_prop, const gchar *iface_name)
-{
-    gboolean ret;
-    GError *error = NULL;
-    GHashTable *props = NULL;
-
-    props = NULL;
-
-    ret = dbus_g_proxy_call (proxy_prop, "GetAll", &error,
-			     G_TYPE_STRING, iface_name,
-			     G_TYPE_INVALID,
-			     dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), &props,
-			     G_TYPE_INVALID);
-			    
-    if (!ret) 
-    {
-	g_warning ("Unable to get interface properties for : %s : %s", iface_name, error->message);
-	g_error_free (error);
-    }
-    
-    return props;
-}
-
-/**
- * xfpm_power_get_interface_property:
- * 
- **/
-GValue xfpm_power_get_interface_property (DBusGProxy *proxy, const gchar *iface_name, const gchar *prop_name)
-{
-    gboolean ret;
-    GError *error = NULL;
-    GValue value = { 0, };
-
-    ret = dbus_g_proxy_call (proxy, "Get", &error,
-			     G_TYPE_STRING, iface_name,
-			     G_TYPE_STRING, prop_name,
-			     G_TYPE_INVALID,
-			     G_TYPE_VALUE, &value, G_TYPE_INVALID);
-							
-    if (!ret) 
-    {
-	g_warning ("Unable to get property %s on interface  %s : %s", prop_name, iface_name, error->message);
-	g_error_free (error);
-    }
-    
-    return value;
-}
-
-/**
  * xfpm_power_translate_device_type:
  * 
  **/
diff --git a/src/xfpm-power-common.h b/src/xfpm-power-common.h
index 1040aed..dbf6e16 100644
--- a/src/xfpm-power-common.h
+++ b/src/xfpm-power-common.h
@@ -33,18 +33,13 @@
 #define UPOWER_PATH_WAKEUPS   "/org/freedesktop/UPower/Wakeups"
 #define UPOWER_IFACE_WAKEUPS  "org.freedesktop.UPower.Wakeups"
 
-#define POLKIT_AUTH_SUSPEND   "org.freedesktop.upower.suspend"
-#define	POLKIT_AUTH_HIBERNATE "org.freedesktop.upower.hibernate"
+#define POLKIT_AUTH_SUSPEND_UPOWER	"org.freedesktop.upower.suspend"
+#define POLKIT_AUTH_HIBERNATE_UPOWER	"org.freedesktop.upower.hibernate"
 
+#define POLKIT_AUTH_SUSPEND_LOGIND	"org.freedesktop.login1.suspend"
+#define POLKIT_AUTH_HIBERNATE_LOGIND	"org.freedesktop.login1.hibernate"
 
-GPtrArray 	*xfpm_power_enumerate_devices		(DBusGProxy *proxy);
 
-GHashTable	*xfpm_power_get_interface_properties 	(DBusGProxy *proxy_prop, 
-							 const gchar *iface_name);
-
-GValue 		 xfpm_power_get_interface_property   	(DBusGProxy *proxy, 
-							 const gchar *iface_name, 
-							 const gchar *prop_name);
 
 const gchar 	*xfpm_power_translate_device_type 	(guint type);
 
diff --git a/src/xfpm-power-info.c b/src/xfpm-power-info.c
index a53a6be..263ccbd 100644
--- a/src/xfpm-power-info.c
+++ b/src/xfpm-power-info.c
@@ -39,6 +39,7 @@
 
 #include <dbus/dbus-glib.h>
 #include <dbus/dbus-glib-lowlevel.h>
+#include <upower.h>
 
 #include "xfpm-icons.h"
 #include "xfpm-power-common.h"
@@ -59,6 +60,8 @@ typedef struct
     
     GtkWidget	    *wakeups; /* Tree view processor wakeups*/
     
+    UpClient        *upower;
+
 } XfpmInfo;
 
 enum
@@ -253,18 +256,9 @@ gpm_stats_format_details (const gchar *command_details)
 }
 
 static gchar *
-xfpm_info_get_energy_property (GHashTable *props, const gchar *prop, const gchar *unit)
+xfpm_info_get_energy_property (gdouble energy, const gchar *unit)
 {
-    GValue *value;
     gchar *val = NULL;
-    gdouble energy;
-    
-    value = g_hash_table_lookup (props, prop);
-    
-    if ( !value )
-	return NULL;
-	
-    energy = g_value_get_double (value);
     
     val = g_strdup_printf ("%.1f %s", energy, unit);
     
@@ -301,7 +295,7 @@ xfpm_info_add_sidebar_icon (XfpmInfo *info, const gchar *name, const gchar *icon
 }
 
 static void
-xfpm_info_add_device_view (XfpmInfo *info, GHashTable *props, const gchar *object_path)
+xfpm_info_add_device_view (XfpmInfo *info, UpDevice *device, const gchar *object_path)
 {
     GtkWidget *view;
 
@@ -309,11 +303,12 @@ xfpm_info_add_device_view (XfpmInfo *info, GHashTable *props, const gchar *objec
     GtkTreeIter iter;
     GtkTreeViewColumn *col;
     GtkCellRenderer *renderer;
-    GValue *value;
-    const gchar *cstr;
     gchar *str;
     gint i = 0;
-    guint type = 0;
+    guint type = 0, tech = 0;
+    gdouble energy_full_design = -1.0, energy_full = -1.0, energy_empty = -1.0, voltage = -1.0, percent = -1.0;
+    gboolean p_supply = FALSE;
+    gchar *model = NULL, *vendor = NULL, *serial = NULL;
     const gchar *battery_type = NULL;
     
     view = gtk_tree_view_new ();
@@ -352,11 +347,26 @@ xfpm_info_add_device_view (XfpmInfo *info, GHashTable *props, const gchar *objec
     
     
     /*Type*/
-    value = g_hash_table_lookup (props, "Type");
-    
-    if ( value )
+    /* hack, this depends on XFPM_DEVICE_TYPE_* being in sync with UP_DEVICE_KIND_* */
+    g_object_get (device,
+		  "kind", &type,
+		  "power-supply", &p_supply,
+		  "model", &model,
+		  "vendor", &vendor,
+		  "serial", &serial,
+		  "technology", &tech,
+		  "energy-full-design", &energy_full_design,
+		  "energy-full", &energy_full,
+		  "energy-empty", &energy_empty,
+		  "voltage", &voltage,
+		  "percentage", &percent,
+		  NULL);
+
+    if (type > XFPM_DEVICE_TYPE_PHONE)
+        type = XFPM_DEVICE_TYPE_UNKNOWN;
+
+    if (type != XFPM_DEVICE_TYPE_UNKNOWN)
     {
-	type = g_value_get_uint (value);
 	battery_type = xfpm_power_translate_device_type (type);
 	gtk_list_store_append (list_store, &iter);
 	gtk_list_store_set (list_store, &iter, 
@@ -366,55 +376,37 @@ xfpm_info_add_device_view (XfpmInfo *info, GHashTable *props, const gchar *objec
 	i++;
     }
     
-    value = g_hash_table_lookup (props, "PowerSupply");
-    
-    if ( value )
-    {
-	gtk_list_store_append (list_store, &iter);
-	gtk_list_store_set (list_store, &iter, 
-			    XFPM_DEVICE_INFO_NAME, _("PowerSupply"), 
-			    XFPM_DEVICE_INFO_VALUE, g_value_get_boolean (value) == TRUE ? _("True") : _("False"),
-			    -1);
-	i++;
-    }
+    gtk_list_store_append (list_store, &iter);
+    gtk_list_store_set (list_store, &iter,
+			XFPM_DEVICE_INFO_NAME, _("PowerSupply"),
+			XFPM_DEVICE_INFO_VALUE, p_supply == TRUE ? _("True") : _("False"),
+			-1);
+    i++;
     
     if ( type != XFPM_DEVICE_TYPE_LINE_POWER )
     {
 	/*Model*/
-	value = g_hash_table_lookup (props, "Model");
-	
-	if ( value )
-	{
-	    cstr = g_value_get_string (value);
-	    if ( cstr && strlen (cstr) > 0)
-	    {
-		gtk_list_store_append (list_store, &iter);
-		gtk_list_store_set (list_store, &iter, 
-				    XFPM_DEVICE_INFO_NAME, _("Model"), 
-				    XFPM_DEVICE_INFO_VALUE, g_value_get_string (value),
-				    -1);
-		i++;
-	    }
-	}
-	
-	/*Technology*/
-	value = g_hash_table_lookup (props, "Technology");
-	
-	if ( value )
+	if (model && strlen (model) > 0)
 	{
 	    gtk_list_store_append (list_store, &iter);
-	    gtk_list_store_set (list_store, &iter, 
-				XFPM_DEVICE_INFO_NAME, _("Technology"), 
-				XFPM_DEVICE_INFO_VALUE, xfpm_power_translate_technology (g_value_get_uint (value)),
+	    gtk_list_store_set (list_store, &iter,
+				XFPM_DEVICE_INFO_NAME, _("Model"),
+				XFPM_DEVICE_INFO_VALUE, model,
 				-1);
 	    i++;
 	}
 	
-	value = g_hash_table_lookup (props, "Percentage");
+	gtk_list_store_append (list_store, &iter);
+	gtk_list_store_set (list_store, &iter,
+			    XFPM_DEVICE_INFO_NAME, _("Technology"),
+			    XFPM_DEVICE_INFO_VALUE, xfpm_power_translate_technology (tech),
+			    -1);
+	i++;
 
-	if ( value )
+	/*Percentage*/
+	if (percent >= 0)
 	{
-	    str = g_strdup_printf("%d", (guint) g_value_get_double (value));
+	    str = g_strdup_printf("%d", (guint) percent);
 	    gtk_list_store_append (list_store, &iter);
 	    gtk_list_store_set (list_store, &iter,
 				XFPM_DEVICE_INFO_NAME, _("Energy percent"),
@@ -424,11 +416,10 @@ xfpm_info_add_device_view (XfpmInfo *info, GHashTable *props, const gchar *objec
 	    g_free(str);
 	}
 
-	/* TRANSLATORS: Unit here is What hour*/
-	str = xfpm_info_get_energy_property (props, "EnergyFullDesign", _("Wh"));
-	
-	if ( str )
+	if (energy_full_design > 0)
 	{
+	    /* TRANSLATORS: Unit here is Watt hour*/
+	    str = xfpm_info_get_energy_property (energy_full_design, _("Wh"));
 	    gtk_list_store_append (list_store, &iter);
 	    gtk_list_store_set (list_store, &iter, 
 				XFPM_DEVICE_INFO_NAME, _("Energy full design"), 
@@ -438,11 +429,10 @@ xfpm_info_add_device_view (XfpmInfo *info, GHashTable *props, const gchar *objec
 	    g_free (str);
 	}
 	
-	/* TRANSLATORS: Unit here is What hour*/
-	str = xfpm_info_get_energy_property (props, "EnergyFull", _("Wh"));
-	
-	if ( str )
+	if (energy_full > 0)
 	{
+	    /* TRANSLATORS: Unit here is Watt hour*/
+	    str = xfpm_info_get_energy_property (energy_full, _("Wh"));
 	    gtk_list_store_append (list_store, &iter);
 	    gtk_list_store_set (list_store, &iter, 
 				XFPM_DEVICE_INFO_NAME, _("Energy full"), 
@@ -452,11 +442,10 @@ xfpm_info_add_device_view (XfpmInfo *info, GHashTable *props, const gchar *objec
 	    g_free (str);
 	}
 	
-	/* TRANSLATORS: Unit here is What hour*/
-	str = xfpm_info_get_energy_property (props, "EnergyEmpty", _("Wh"));
-	
-	if ( str )
+	if (energy_empty > 0)
 	{
+	    /* TRANSLATORS: Unit here is Watt hour*/
+	    str = xfpm_info_get_energy_property (energy_empty, _("Wh"));
 	    gtk_list_store_append (list_store, &iter);
 	    gtk_list_store_set (list_store, &iter, 
 				XFPM_DEVICE_INFO_NAME, _("Energy empty"), 
@@ -466,10 +455,10 @@ xfpm_info_add_device_view (XfpmInfo *info, GHashTable *props, const gchar *objec
 	    g_free (str);
 	}
 	
-	/* TRANSLATORS: Unit here is volt*/
-	str = xfpm_info_get_energy_property (props, "Voltage", _("V"));
-	if ( str )
+	if (voltage > 0)
 	{
+	    /* TRANSLATORS: Unit here is Volt*/
+	    str = xfpm_info_get_energy_property (voltage, _("V"));
 	    gtk_list_store_append (list_store, &iter);
 	    gtk_list_store_set (list_store, &iter, 
 				XFPM_DEVICE_INFO_NAME, _("Voltage"), 
@@ -478,52 +467,25 @@ xfpm_info_add_device_view (XfpmInfo *info, GHashTable *props, const gchar *objec
 	    i++;
 	    g_free (str);
 	}
-	
-	/*Percentage*/
-	str = xfpm_info_get_energy_property (props, "Percentage", _("%"));
-	if ( str )
+
+	if (vendor && strlen (vendor) > 0)
 	{
 	    gtk_list_store_append (list_store, &iter);
 	    gtk_list_store_set (list_store, &iter,
-				XFPM_DEVICE_INFO_NAME, _("Percentage"),
-				XFPM_DEVICE_INFO_VALUE, str,
+				XFPM_DEVICE_INFO_NAME, _("Vendor"),
+				XFPM_DEVICE_INFO_VALUE, vendor,
 				-1);
 	    i++;
-	    g_free (str);
 	}
 
-	/*Vendor*/
-	value = g_hash_table_lookup (props, "Vendor");
-	
-	if ( value )
+	if (serial && strlen (serial) > 0)
 	{
-	    cstr = g_value_get_string (value);
-	    if ( cstr && strlen (cstr) > 0)
-	    {
-		gtk_list_store_append (list_store, &iter);
-		gtk_list_store_set (list_store, &iter, 
-				    XFPM_DEVICE_INFO_NAME, _("Vendor"), 
-				    XFPM_DEVICE_INFO_VALUE, g_value_get_string (value),
-				    -1);
-		i++;
-	    }
-	}
-	
-	/*Serial*/
-	value = g_hash_table_lookup (props, "Serial");
-	
-	if ( value )
-	{
-	    cstr = g_value_get_string (value);
-	    if ( cstr && strlen (cstr) > 0)
-	    {
-		gtk_list_store_append (list_store, &iter);
-		gtk_list_store_set (list_store, &iter, 
-				    XFPM_DEVICE_INFO_NAME, _("Serial"), 
-				    XFPM_DEVICE_INFO_VALUE, g_value_get_string (value),
-				    -1);
-		i++;
-	    }
+	    gtk_list_store_append (list_store, &iter);
+	    gtk_list_store_set (list_store, &iter,
+				XFPM_DEVICE_INFO_NAME, _("Serial"),
+				XFPM_DEVICE_INFO_VALUE, serial,
+				-1);
+	    i++;
 	}
     }
     
@@ -534,29 +496,10 @@ xfpm_info_add_device_view (XfpmInfo *info, GHashTable *props, const gchar *objec
 }
 
 static void
-xfpm_info_add_device (XfpmInfo *info, const gchar *object_path)
+xfpm_info_add_device (XfpmInfo *info, UpDevice *device)
 {
-    DBusGProxy *proxy_prop;
-    GHashTable *props;
-    
-    proxy_prop = dbus_g_proxy_new_for_name (info->bus, 
-					    UPOWER_NAME,
-					    object_path,
-					    DBUS_INTERFACE_PROPERTIES);
-					    
-    if ( !proxy_prop )
-    {
-	g_warning ("Unable to create proxy for : %s", object_path);
-	return;
-    }
-    
-    props = xfpm_power_get_interface_properties (proxy_prop, UPOWER_IFACE_DEVICE);
-    
-    if ( props )
-    {
-	xfpm_info_add_device_view (info, props, object_path);
-	g_hash_table_destroy (props);
-    }
+    const gchar *object_path = up_device_get_object_path(device);
+    xfpm_info_add_device_view (info, device, object_path);
 }
 
 static void
@@ -564,22 +507,18 @@ xfpm_info_power_devices (XfpmInfo *info)
 {
     GPtrArray *array = NULL;
     guint i;
-    
+#if !UP_CHECK_VERSION(0, 99, 0)
+    up_client_enumerate_devices_sync(info->upower, NULL, NULL);
+#endif
     /*Check for upower/devkit power here*/
-
-    info->power_proxy = dbus_g_proxy_new_for_name (info->bus,
-						   UPOWER_NAME,
-						   UPOWER_PATH,
-						   UPOWER_IFACE);
-    
-    array = xfpm_power_enumerate_devices (info->power_proxy);
+    array = up_client_get_devices(info->upower);
     
     if ( array )
     {
 	for ( i = 0; i < array->len; i++)
 	{
-	    const gchar *object_path = ( const gchar *) g_ptr_array_index (array, i);
-	    xfpm_info_add_device (info, object_path);
+	    UpDevice *device = g_ptr_array_index (array, i);
+	    xfpm_info_add_device (info, device);
 	}
 	g_ptr_array_free (array, TRUE);
     }
@@ -928,6 +867,7 @@ xfpm_info_new (void)
     info->power_proxy   = NULL;
     info->wakeups_proxy = NULL;
     info->dialog        = NULL;
+    info->upower        = up_client_new ();
     
     return info;
 }
diff --git a/src/xfpm-power.c b/src/xfpm-power.c
index 618944a..35fbd12 100644
--- a/src/xfpm-power.c
+++ b/src/xfpm-power.c
@@ -28,6 +28,7 @@
 
 #include <dbus/dbus-glib.h>
 #include <dbus/dbus-glib-lowlevel.h>
+#include <upower.h>
 
 #include <libxfce4util/libxfce4util.h>
 #include <libxfce4ui/libxfce4ui.h>
@@ -72,8 +73,7 @@ struct XfpmPowerPrivate
 {
     DBusGConnection *bus;
 
-    DBusGProxy      *proxy;
-    DBusGProxy      *proxy_prop;
+    UpClient        *upower;
 
     GHashTable      *hash;
 
@@ -148,61 +148,28 @@ G_DEFINE_TYPE (XfpmPower, xfpm_power, G_TYPE_OBJECT)
 static void
 xfpm_power_check_polkit_auth (XfpmPower *power)
 {
-    power->priv->auth_suspend = xfpm_polkit_check_auth (power->priv->polkit,
-							POLKIT_AUTH_SUSPEND);
-
-    power->priv->auth_hibernate = xfpm_polkit_check_auth (power->priv->polkit,
-							  POLKIT_AUTH_HIBERNATE);
-
-}
-#endif
-
-static void
-xfpm_power_check_pm (XfpmPower *power, GHashTable *props)
-{
-    GValue *value;
-    gboolean ret;
-
-    value = g_hash_table_lookup (props, "CanSuspend");
-
-    if (value == NULL)
+    const char *suspend, *hibernate;
+    if (LOGIND_RUNNING())
     {
-	g_warning ("No 'CanSuspend' property");
+	suspend   = POLKIT_AUTH_SUSPEND_LOGIND;
+	hibernate = POLKIT_AUTH_HIBERNATE_LOGIND;
     }
-    ret = g_value_get_boolean (value);
-
-    if (ret != power->priv->can_suspend)
-    {
-	power->priv->can_suspend = ret;
-    }
-
-    value = g_hash_table_lookup (props, "CanHibernate");
-
-    if (value == NULL)
+    else
     {
-	g_warning ("No 'CanHibernate' property");
+	suspend   = POLKIT_AUTH_SUSPEND_UPOWER;
+	hibernate = POLKIT_AUTH_HIBERNATE_UPOWER;
     }
+    power->priv->auth_suspend = xfpm_polkit_check_auth (power->priv->polkit,
+							suspend);
 
-    ret = g_value_get_boolean (value);
-
-    if (ret != power->priv->can_hibernate)
-    {
-	power->priv->can_hibernate = ret;
-    }
+    power->priv->auth_hibernate = xfpm_polkit_check_auth (power->priv->polkit,
+							  hibernate);
 }
+#endif
 
 static void
-xfpm_power_check_power (XfpmPower *power, GHashTable *props)
+xfpm_power_check_power (XfpmPower *power, gboolean on_battery)
 {
-    GValue *value;
-    gboolean on_battery;
-
-    value = g_hash_table_lookup (props, "OnBattery");
-
-    if (G_LIKELY (value))
-    {
-	on_battery = g_value_get_boolean (value);
-
 	if (on_battery != power->priv->on_battery )
 	{
 	    GList *list;
@@ -218,42 +185,15 @@ xfpm_power_check_power (XfpmPower *power, GHashTable *props)
 			      NULL);
 	    }
 	}
-    }
-    else
-    {
-	g_warning ("No 'OnBattery' property");
-    }
 }
 
 static void
-xfpm_power_check_lid (XfpmPower *power, GHashTable *props)
+xfpm_power_check_lid (XfpmPower *power, gboolean present, gboolean closed)
 {
-    GValue *value;
-
-    value = g_hash_table_lookup (props, "LidIsPresent");
-
-    if (value == NULL)
-    {
-	g_warning ("No 'LidIsPresent' property");
-	return;
-    }
-
-    power->priv->lid_is_present = g_value_get_boolean (value);
+    power->priv->lid_is_present = present;
 
     if (power->priv->lid_is_present)
     {
-	gboolean closed;
-
-	value = g_hash_table_lookup (props, "LidIsClosed");
-
-	if (value == NULL)
-	{
-	    g_warning ("No 'LidIsClosed' property");
-	    return;
-	}
-
-	closed = g_value_get_boolean (value);
-
 	if (closed != power->priv->lid_is_closed )
 	{
 	    power->priv->lid_is_closed = closed;
@@ -276,10 +216,15 @@ xfpm_power_check_lid (XfpmPower *power, GHashTable *props)
 static void
 xfpm_power_get_properties (XfpmPower *power)
 {
-    GHashTable *props;
-
-    props = xfpm_power_get_interface_properties (power->priv->proxy_prop, UPOWER_IFACE);
+    gboolean on_battery;
+    gboolean lid_is_closed;
+    gboolean lid_is_present;
 
+    /* TODO: newer versions of upower don't have that => logind handles it */
+#if !UP_CHECK_VERSION(0, 99, 0)
+    power->priv->can_suspend = up_client_get_can_suspend(power->priv->upower);
+    power->priv->can_hibernate = up_client_get_can_hibernate(power->priv->upower);
+#else
     if ( LOGIND_RUNNING () )
     {
         g_object_get (G_OBJECT (power->priv->systemd),
@@ -291,13 +236,17 @@ xfpm_power_get_properties (XfpmPower *power)
     }
     else
     {
-	xfpm_power_check_pm (power, props);
+	g_warning("Error: using upower >= 0.99.0 but logind is not running");
+	g_warning("       suspend / hibernate will not work!");
     }
-
-    xfpm_power_check_lid (power, props);
-    xfpm_power_check_power (power, props);
-
-    g_hash_table_destroy (props);
+#endif
+    g_object_get (power->priv->upower,
+                  "on-battery", &on_battery,
+                  "lid-is-closed", &lid_is_closed,
+                  "lid-is-present", &lid_is_present,
+                  NULL);
+    xfpm_power_check_lid (power, lid_is_present, lid_is_closed);
+    xfpm_power_check_power (power, on_battery);
 }
 
 static void
@@ -370,9 +319,19 @@ xfpm_power_sleep (XfpmPower *power, const gchar *sleep_time, gboolean force)
     }
     else
     {
-	dbus_g_proxy_call (power->priv->proxy, sleep_time, &error,
-			   G_TYPE_INVALID,
-			   G_TYPE_INVALID);
+#if !UP_CHECK_VERSION(0, 99, 0)
+	if (!g_strcmp0 (sleep_time, "Hibernate"))
+	{
+	    up_client_hibernate_sync(power->priv->upower, NULL, &error);
+	}
+	else
+	{
+	    up_client_suspend_sync(power->priv->upower, NULL, &error);
+	}
+#else
+	g_warning("Error: using upower >= 0.99.0 but logind is not running");
+	g_warning("       suspend / hibernate will not work!");
+#endif
     }
 
     if ( error )
@@ -1028,26 +987,18 @@ xfpm_power_battery_charge_changed_cb (XfpmBattery *battery, XfpmPower *power)
 }
 
 static void
-xfpm_power_add_device (XfpmPower *power, const gchar *object_path)
+xfpm_power_add_device (UpDevice *device, XfpmPower *power)
 {
-    DBusGProxy *proxy_prop;
     guint device_type = XFPM_DEVICE_TYPE_UNKNOWN;
-    GValue value;
+    const gchar *object_path = up_device_get_object_path(device);
 
-    proxy_prop = dbus_g_proxy_new_for_name (power->priv->bus,
-					    UPOWER_NAME,
-					    object_path,
-					    DBUS_INTERFACE_PROPERTIES);
-
-    if ( !proxy_prop )
-    {
-	g_warning ("Unable to create proxy for : %s", object_path);
-	return;
-    }
-
-    value = xfpm_power_get_interface_property (proxy_prop, UPOWER_IFACE_DEVICE, "Type");
+    /* hack, this depends on XFPM_DEVICE_TYPE_* being in sync with UP_DEVICE_KIND_* */
+    g_object_get (device,
+		  "kind", &device_type,
+		  NULL);
 
-    device_type = g_value_get_uint (&value);
+    if (device_type > XFPM_DEVICE_TYPE_PHONE)
+        device_type = XFPM_DEVICE_TYPE_UNKNOWN;
 
     XFPM_DEBUG_ENUM (device_type, XFPM_TYPE_DEVICE_TYPE, " device added");
 
@@ -1058,20 +1009,13 @@ xfpm_power_add_device (XfpmPower *power, const gchar *object_path)
 	 device_type == XFPM_DEVICE_TYPE_PHONE)
     {
 	GtkStatusIcon *battery;
-	DBusGProxy *proxy;
 	XFPM_DEBUG_ENUM (device_type, XFPM_TYPE_DEVICE_TYPE,
 			"Battery device detected at : %s", object_path);
-	proxy = dbus_g_proxy_new_for_name (power->priv->bus,
-					   UPOWER_NAME,
-					   object_path,
-					   UPOWER_IFACE_DEVICE);
 	battery = xfpm_battery_new ();
 	gtk_status_icon_set_visible (battery, FALSE);
 	xfpm_battery_monitor_device (XFPM_BATTERY (battery),
-				     proxy,
-				     proxy_prop,
+				     object_path,
 				     device_type);
-
 	g_hash_table_insert (power->priv->hash, g_strdup (object_path), battery);
 
 	g_signal_connect (battery, "popup-menu",
@@ -1082,32 +1026,32 @@ xfpm_power_add_device (XfpmPower *power, const gchar *object_path)
 
 	xfpm_power_refresh_adaptor_visible (power);
     }
-    else if ( device_type != XFPM_DEVICE_TYPE_LINE_POWER )
-    {
-	g_warning ("Unable to monitor unknown power device with object_path : %s", object_path);
-	g_object_unref (proxy_prop);
-    }
 }
 
 static void
 xfpm_power_get_power_devices (XfpmPower *power)
 {
+#if !UP_CHECK_VERSION(0, 99, 0)
+    /* the device-add callback is called for each device */
+    up_client_enumerate_devices_sync(power->priv->upower, NULL, NULL);
+#else
     GPtrArray *array = NULL;
     guint i;
 
-    array = xfpm_power_enumerate_devices (power->priv->proxy);
+    array = up_client_get_devices(power->priv->upower);
 
     if ( array )
     {
 	for ( i = 0; i < array->len; i++)
 	{
-	    const gchar *object_path = ( const gchar *) g_ptr_array_index (array, i);
+	    UpDevice *device = g_ptr_array_index (array, i);
+	    const gchar *object_path = up_device_get_object_path(device);
 	    XFPM_DEBUG ("Power device detected at : %s", object_path);
-	    xfpm_power_add_device (power, object_path);
+	    xfpm_power_add_device (device, power);
 	}
 	g_ptr_array_free (array, TRUE);
     }
-
+#endif
 }
 
 static void
@@ -1124,23 +1068,36 @@ xfpm_power_inhibit_changed_cb (XfpmInhibit *inhibit, gboolean is_inhibit, XfpmPo
 }
 
 static void
-xfpm_power_changed_cb (DBusGProxy *proxy, XfpmPower *power)
+xfpm_power_changed_cb (UpClient *upower,
+#if UP_CHECK_VERSION(0, 99, 0)
+		       GParamSpec *pspec,
+#endif
+		       XfpmPower *power)
 {
     xfpm_power_get_properties (power);
     xfpm_power_refresh_adaptor_visible (power);
 }
 
 static void
-xfpm_power_device_added_cb (DBusGProxy *proxy, const gchar *object_path, XfpmPower *power)
+xfpm_power_device_added_cb (UpClient *upower, UpDevice *device, XfpmPower *power)
 {
-    xfpm_power_add_device (power, object_path);
+    xfpm_power_add_device (device, power);
 }
 
+#if UP_CHECK_VERSION(0, 99, 0)
+static void
+xfpm_power_device_removed_cb (UpClient *upower, const gchar *object_path, XfpmPower *power)
+{
+    xfpm_power_remove_device (power, object_path);
+}
+#else
 static void
-xfpm_power_device_removed_cb (DBusGProxy *proxy, const gchar *object_path, XfpmPower *power)
+xfpm_power_device_removed_cb (UpClient *upower, UpDevice *device, XfpmPower *power)
 {
+    const gchar *object_path = up_device_get_object_path(device);
     xfpm_power_remove_device (power, object_path);
 }
+#endif
 
 static void
 xfpm_power_device_changed_cb (DBusGProxy *proxy, const gchar *object_path, XfpmPower *power)
@@ -1378,6 +1335,7 @@ xfpm_power_init (XfpmPower *power)
     power->priv->inhibit = xfpm_inhibit_new ();
     power->priv->notify  = xfpm_notify_new ();
     power->priv->conf    = xfpm_xfconf_new ();
+    power->priv->upower  = up_client_new ();
 
     power->priv->systemd = NULL;
     power->priv->console = NULL;
@@ -1407,44 +1365,19 @@ xfpm_power_init (XfpmPower *power)
 	goto out;
     }
 
-    power->priv->proxy = dbus_g_proxy_new_for_name (power->priv->bus,
-						    UPOWER_NAME,
-						    UPOWER_PATH,
-						    UPOWER_IFACE);
-
-
-    power->priv->proxy_prop = dbus_g_proxy_new_for_name (power->priv->bus,
-							 UPOWER_NAME,
-							 UPOWER_PATH,
-							 DBUS_INTERFACE_PROPERTIES);
-    if (power->priv->proxy_prop == NULL)
-    {
-	g_critical ("Unable to create proxy for %s", UPOWER_NAME);
-	goto out;
-    }
-
+    g_signal_connect (power->priv->upower, "device-added", G_CALLBACK (xfpm_power_device_added_cb), power);
+    g_signal_connect (power->priv->upower, "device-removed", G_CALLBACK (xfpm_power_device_removed_cb), power);
+#if UP_CHECK_VERSION(0, 99, 0)
+    g_signal_connect (power->priv->upower, "notify", G_CALLBACK (xfpm_power_changed_cb), power);
+#else
+    g_signal_connect (power->priv->upower, "changed", G_CALLBACK (xfpm_power_changed_cb), power);
+#endif
     xfpm_power_get_power_devices (power);
     xfpm_power_get_properties (power);
 #ifdef ENABLE_POLKIT
     xfpm_power_check_polkit_auth (power);
 #endif
 
-    dbus_g_proxy_add_signal (power->priv->proxy, "Changed", G_TYPE_INVALID);
-    dbus_g_proxy_add_signal (power->priv->proxy, "DeviceAdded", G_TYPE_STRING, G_TYPE_INVALID);
-    dbus_g_proxy_add_signal (power->priv->proxy, "DeviceRemoved", G_TYPE_STRING, G_TYPE_INVALID);
-    dbus_g_proxy_add_signal (power->priv->proxy, "DeviceChanged", G_TYPE_STRING, G_TYPE_INVALID);
-
-    dbus_g_proxy_connect_signal (power->priv->proxy, "Changed",
-				 G_CALLBACK (xfpm_power_changed_cb), power, NULL);
-    dbus_g_proxy_connect_signal (power->priv->proxy, "DeviceRemoved",
-				 G_CALLBACK (xfpm_power_device_removed_cb), power, NULL);
-    dbus_g_proxy_connect_signal (power->priv->proxy, "DeviceAdded",
-				 G_CALLBACK (xfpm_power_device_added_cb), power, NULL);
-
-    dbus_g_proxy_connect_signal (power->priv->proxy, "DeviceChanged",
-				 G_CALLBACK (xfpm_power_device_changed_cb), power, NULL);
-
-
 out:
     xfpm_power_refresh_adaptor_visible (power);
 
@@ -1512,22 +1445,6 @@ xfpm_power_finalize (GObject *object)
 
     dbus_g_connection_unref (power->priv->bus);
 
-    if ( power->priv->proxy )
-    {
-	dbus_g_proxy_disconnect_signal (power->priv->proxy, "Changed",
-					G_CALLBACK (xfpm_power_changed_cb), power);
-	dbus_g_proxy_disconnect_signal (power->priv->proxy, "DeviceRemoved",
-					G_CALLBACK (xfpm_power_device_removed_cb), power);
-	dbus_g_proxy_disconnect_signal (power->priv->proxy, "DeviceAdded",
-					G_CALLBACK (xfpm_power_device_added_cb), power);
-	dbus_g_proxy_disconnect_signal (power->priv->proxy, "DeviceChanged",
-					G_CALLBACK (xfpm_power_device_changed_cb), power);
-	g_object_unref (power->priv->proxy);
-    }
-
-    if ( power->priv->proxy_prop )
-	g_object_unref (power->priv->proxy_prop);
-
     g_hash_table_destroy (power->priv->hash);
 
 #ifdef ENABLE_POLKIT

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list