gnome-power-manager r3055 - in trunk: . libdevkit-power



Author: rhughes
Date: Mon Nov  3 17:20:59 2008
New Revision: 3055
URL: http://svn.gnome.org/viewvc/gnome-power-manager?rev=3055&view=rev

Log:
2008-11-03  Richard Hughes  <richard hughsie com>

* Makefile.am:
* configure.ac:
* libdevkit-power/Makefile.am:
* libdevkit-power/dkp-client-device.c:
(dkp_client_device_get_device_properties),
(dkp_client_device_refresh_internal),
(dkp_client_device_changed_cb),
(dkp_client_device_set_object_path),
(dkp_client_device_get_object_path),
(dkp_client_device_get_object), (dkp_client_device_print_history),
(dkp_client_device_print), (dkp_client_device_refresh),
(dkp_client_device_get_history),
(dkp_client_device_get_statistics), (dkp_client_device_class_init),
(dkp_client_device_init), (dkp_client_device_finalize),
(dkp_client_device_new):
* libdevkit-power/dkp-client-device.h:
* libdevkit-power/dkp-client.c: (dkp_client_get_device),
(dkp_client_enumerate_devices), (dkp_client_add),
(dkp_client_remove), (dkp_client_added_cb),
(dkp_client_changed_cb), (dkp_client_removed_cb),
(dkp_client_class_init), (dkp_client_init), (dkp_client_finalize),
(dkp_client_new):
* libdevkit-power/dkp-client.h:
* libdevkit-power/dkp-enum.c: (dkp_device_type_to_text),
(dkp_device_type_from_text), (dkp_device_state_to_text),
(dkp_device_state_from_text), (dkp_device_technology_to_text),
(dkp_device_technology_from_text):
* libdevkit-power/dkp-enum.h:
* libdevkit-power/dkp-history-obj.c:
(dkp_history_obj_clear_internal), (dkp_history_obj_copy),
(dkp_history_obj_equal), (dkp_history_obj_print),
(dkp_history_obj_new), (dkp_history_obj_clear),
(dkp_history_obj_free), (dkp_history_obj_create),
(dkp_history_obj_from_string), (dkp_history_obj_to_string):
* libdevkit-power/dkp-history-obj.h:
* libdevkit-power/dkp-object.c: (dkp_object_clear_internal),
(dkp_object_collect_props), (dkp_object_set_from_map),
(dkp_object_copy), (dkp_object_equal), (dkp_object_time_to_text),
(dkp_object_bool_to_text), (dkp_object_print), (dkp_object_diff),
(dkp_object_new), (dkp_object_free_internal), (dkp_object_free),
(dkp_object_clear), (dkp_object_get_id):
* libdevkit-power/dkp-object.h:
* libdevkit-power/dkp-stats-obj.c: (dkp_stats_obj_copy),
(dkp_stats_obj_new), (dkp_stats_obj_free), (dkp_stats_obj_create),
(dkp_stats_obj_from_string), (dkp_stats_obj_to_string):
* libdevkit-power/dkp-stats-obj.h:
* libdevkit-power/egg-debug.c:
* libdevkit-power/egg-debug.h:
* libdevkit-power/egg-obj-list.c:
* libdevkit-power/egg-obj-list.h:
* libdevkit-power/egg-string.c:
* libdevkit-power/egg-string.h:
Add libdevkit-power, a client access library for DeviceKit-power.
This is only compiled when configured with --enable-devicekit-power


Added:
   trunk/libdevkit-power/
   trunk/libdevkit-power/Makefile.am
   trunk/libdevkit-power/dkp-client-device.c
   trunk/libdevkit-power/dkp-client-device.h
   trunk/libdevkit-power/dkp-client.c
   trunk/libdevkit-power/dkp-client.h
   trunk/libdevkit-power/dkp-enum.c
   trunk/libdevkit-power/dkp-enum.h
   trunk/libdevkit-power/dkp-history-obj.c
   trunk/libdevkit-power/dkp-history-obj.h
   trunk/libdevkit-power/dkp-object.c
   trunk/libdevkit-power/dkp-object.h
   trunk/libdevkit-power/dkp-stats-obj.c
   trunk/libdevkit-power/dkp-stats-obj.h
   trunk/libdevkit-power/egg-debug.c   (contents, props changed)
   trunk/libdevkit-power/egg-debug.h   (contents, props changed)
   trunk/libdevkit-power/egg-obj-list.c   (contents, props changed)
   trunk/libdevkit-power/egg-obj-list.h   (contents, props changed)
   trunk/libdevkit-power/egg-string.c   (contents, props changed)
   trunk/libdevkit-power/egg-string.h   (contents, props changed)
Modified:
   trunk/ChangeLog
   trunk/Makefile.am
   trunk/configure.ac

Modified: trunk/Makefile.am
==============================================================================
--- trunk/Makefile.am	(original)
+++ trunk/Makefile.am	Mon Nov  3 17:20:59 2008
@@ -1,8 +1,13 @@
 
 ACLOCAL_AMFLAGS = -I m4
 
-SUBDIRS =						\
-	libhal-glib					\
+SUBDIRS = libhal-glib
+
+if HAVE_DK_POWER
+SUBDIRS += libdevkit-power
+endif
+
+SUBDIRS += 						\
 	src						\
 	po						\
 	docs						\

Modified: trunk/configure.ac
==============================================================================
--- trunk/configure.ac	(original)
+++ trunk/configure.ac	Mon Nov  3 17:20:59 2008
@@ -607,6 +607,7 @@
 applets/brightness/Makefile
 applets/inhibit/Makefile
 libhal-glib/Makefile
+libdevkit-power/Makefile
 docs/Makefile
 man/Makefile
 help/Makefile

Added: trunk/libdevkit-power/Makefile.am
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/Makefile.am	Mon Nov  3 17:20:59 2008
@@ -0,0 +1,42 @@
+INCLUDES = \
+	$(GLIB_CFLAGS)						\
+	$(DBUS_CFLAGS)						\
+	-I$(top_srcdir)/libdevkit-power				\
+	-DEGG_LOG_FILE=\""$(DKP_LOG_DIR)/DeviceKit-power"\"	\
+	-DEGG_VERBOSE="\"DKP_VERBOSE\""				\
+	-DEGG_LOGGING="\"DKP_LOGGING\""				\
+	-DEGG_CONSOLE="\"DKP_CONSOLE\""				\
+	-DPACKAGE_DATA_DIR=\""$(datadir)"\"			\
+	-DPACKAGE_LOCALE_DIR=\""$(prefix)/$(DATADIRNAME)/locale"\"
+
+noinst_LTLIBRARIES =						\
+	libdevkit-power.la
+
+libdevkit_power_la_SOURCES =					\
+	egg-debug.c						\
+	egg-debug.h						\
+	egg-string.c						\
+	egg-string.h						\
+	egg-obj-list.c						\
+	egg-obj-list.h						\
+	dkp-client.c						\
+	dkp-client.h						\
+	dkp-client-device.c					\
+	dkp-client-device.h					\
+	dkp-history-obj.h					\
+	dkp-history-obj.c					\
+	dkp-stats-obj.h						\
+	dkp-stats-obj.c						\
+	dkp-enum.c						\
+	dkp-enum.h						\
+	dkp-object.c						\
+	dkp-object.h
+
+libdevkit_power_la_LIBADD = $(INTLLIBS) $(GLIB_LIBS) $(DBUS_LIBS)
+
+
+clean-local:
+	rm -f *~
+
+CLEANFILES = $(BUILT_SOURCES)
+

Added: trunk/libdevkit-power/dkp-client-device.c
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/dkp-client-device.c	Mon Nov  3 17:20:59 2008
@@ -0,0 +1,492 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 Richard Hughes <richard hughsie com>
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <glib.h>
+#include <dbus/dbus-glib.h>
+
+#include "egg-debug.h"
+#include "egg-obj-list.h"
+
+#include "dkp-client-device.h"
+#include "dkp-object.h"
+#include "dkp-stats-obj.h"
+#include "dkp-history-obj.h"
+
+static void	dkp_client_device_class_init	(DkpClientDeviceClass	*klass);
+static void	dkp_client_device_init		(DkpClientDevice	*device);
+static void	dkp_client_device_finalize	(GObject		*object);
+
+#define DKP_CLIENT_DEVICE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DKP_TYPE_CLIENT_DEVICE, DkpClientDevicePrivate))
+
+struct DkpClientDevicePrivate
+{
+	gchar			*object_path;
+	DkpObject		*obj;
+	DBusGConnection		*bus;
+	DBusGProxy		*proxy_device;
+	DBusGProxy		*proxy_props;
+};
+
+enum {
+	DKP_CLIENT_DEVICE_CHANGED,
+	DKP_CLIENT_DEVICE_LAST_SIGNAL
+};
+
+static guint signals [DKP_CLIENT_DEVICE_LAST_SIGNAL] = { 0 };
+
+G_DEFINE_TYPE (DkpClientDevice, dkp_client_device, G_TYPE_OBJECT)
+
+/**
+ * dkp_client_device_get_device_properties:
+ **/
+static GHashTable *
+dkp_client_device_get_device_properties (DkpClientDevice *device)
+{
+	gboolean ret;
+	GError *error = NULL;
+	GHashTable *hash_table = NULL;
+
+	ret = dbus_g_proxy_call (device->priv->proxy_props, "GetAll", &error,
+				 G_TYPE_STRING, "org.freedesktop.DeviceKit.Power.Device",
+				 G_TYPE_INVALID,
+				 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
+				 &hash_table,
+				 G_TYPE_INVALID);
+	if (!ret) {
+		egg_debug ("Couldn't call GetAll() to get properties for %s: %s", device->priv->object_path, error->message);
+		g_error_free (error);
+		goto out;
+	}
+out:
+	return hash_table;
+}
+
+/**
+ * dkp_client_device_refresh_internal:
+ **/
+static gboolean
+dkp_client_device_refresh_internal (DkpClientDevice *device)
+{
+	GHashTable *hash;
+
+	/* get all the properties */
+	hash = dkp_client_device_get_device_properties (device);
+	if (hash == NULL) {
+		egg_warning ("Cannot get device properties for %s", device->priv->object_path);
+		return FALSE;
+	}
+	dkp_object_set_from_map (device->priv->obj, hash);
+	g_hash_table_unref (hash);
+	return TRUE;
+}
+
+/**
+ * dkp_client_device_changed_cb:
+ **/
+static void
+dkp_client_device_changed_cb (DBusGProxy *proxy, DkpClientDevice *device)
+{
+	g_return_if_fail (DKP_IS_CLIENT_DEVICE (device));
+	dkp_client_device_refresh_internal (device);
+	g_signal_emit (device, signals [DKP_CLIENT_DEVICE_CHANGED], 0, device->priv->obj);
+}
+
+/**
+ * dkp_client_device_set_object_path:
+ **/
+gboolean
+dkp_client_device_set_object_path (DkpClientDevice *device, const gchar *object_path)
+{
+	GError *error = NULL;
+	gboolean ret = FALSE;
+	DBusGProxy *proxy_device;
+	DBusGProxy *proxy_props;
+
+	g_return_val_if_fail (DKP_IS_CLIENT_DEVICE (device), FALSE);
+
+	if (device->priv->object_path != NULL)
+		return FALSE;
+	if (object_path == NULL)
+		return FALSE;
+
+	/* connect to the bus */
+	device->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
+	if (device->priv->bus == NULL) {
+		egg_warning ("Couldn't connect to system bus: %s", error->message);
+		g_error_free (error);
+		goto out;
+	}
+
+	/* connect to the correct path for properties */
+	proxy_props = dbus_g_proxy_new_for_name (device->priv->bus, "org.freedesktop.DeviceKit.Power",
+						 object_path, "org.freedesktop.DBus.Properties");
+	if (proxy_props == NULL) {
+		egg_warning ("Couldn't connect to proxy");
+		goto out;
+	}
+
+	/* connect to the correct path for all the other methods */
+	proxy_device = dbus_g_proxy_new_for_name (device->priv->bus, "org.freedesktop.DeviceKit.Power",
+						  object_path, "org.freedesktop.DeviceKit.Power.Device");
+	if (proxy_device == NULL) {
+		egg_warning ("Couldn't connect to proxy");
+		goto out;
+	}
+
+	/* listen to Changed */
+	dbus_g_proxy_add_signal (proxy_device, "Changed", G_TYPE_INVALID);
+	dbus_g_proxy_connect_signal (proxy_device, "Changed",
+				     G_CALLBACK (dkp_client_device_changed_cb), device, NULL);
+
+	/* yay */
+	egg_debug ("using object_path: %s", object_path);
+	device->priv->proxy_device = proxy_device;
+	device->priv->proxy_props = proxy_props;
+	device->priv->object_path = g_strdup (object_path);
+
+	/* coldplug */
+	ret = dkp_client_device_refresh_internal (device);
+	if (!ret)
+		egg_warning ("cannot refresh");
+out:
+	return ret;
+}
+
+/**
+ * dkp_client_device_get_object_path:
+ **/
+const gchar *
+dkp_client_device_get_object_path (const DkpClientDevice *device)
+{
+	g_return_val_if_fail (DKP_IS_CLIENT_DEVICE (device), NULL);
+	return device->priv->object_path;
+}
+
+/**
+ * dkp_client_device_get_object:
+ **/
+const DkpObject *
+dkp_client_device_get_object (const DkpClientDevice *device)
+{
+	g_return_val_if_fail (DKP_IS_CLIENT_DEVICE (device), NULL);
+	return device->priv->obj;
+}
+
+/**
+ * dkp_client_device_print_history:
+ **/
+static gboolean
+dkp_client_device_print_history (const DkpClientDevice *device, const gchar *type)
+{
+	guint i;
+	EggObjList *array;
+	DkpHistoryObj *obj;
+	gboolean ret = FALSE;
+
+	/* get a fair chunk of data */
+	array = dkp_client_device_get_history (device, type, 120);
+	if (array == NULL)
+		goto out;
+
+	/* pretty print */
+	g_print ("  History (%s):\n", type);
+	for (i=0; i<array->len; i++) {
+		obj = (DkpHistoryObj *) egg_obj_list_index (array, i);
+		g_print ("    %i\t%.3f\t%s\n", obj->time, obj->value, dkp_device_state_to_text (obj->state));
+	}
+	g_object_unref (array);
+	ret = TRUE;
+out:
+	return ret;
+}
+
+/**
+ * dkp_client_device_print:
+ **/
+gboolean
+dkp_client_device_print (const DkpClientDevice *device)
+{
+	g_return_val_if_fail (DKP_IS_CLIENT_DEVICE (device), FALSE);
+
+	/* print to screen */
+	dkp_object_print (device->priv->obj);
+
+	/* if we can, get history */
+	dkp_client_device_print_history (device, "charge");
+	dkp_client_device_print_history (device, "rate");
+
+	return TRUE;
+}
+
+/**
+ * dkp_client_device_refresh:
+ **/
+gboolean
+dkp_client_device_refresh (DkpClientDevice *device)
+{
+	GError *error = NULL;
+	gboolean ret;
+
+	g_return_val_if_fail (DKP_IS_CLIENT_DEVICE (device), FALSE);
+	g_return_val_if_fail (device->priv->proxy_device != NULL, FALSE);
+
+	/* just refresh the device */
+	ret = dbus_g_proxy_call (device->priv->proxy_device, "Refresh", &error,
+				 G_TYPE_INVALID, G_TYPE_INVALID);
+	if (!ret) {
+		egg_debug ("Refresh() on %s failed: %s", device->priv->object_path, error->message);
+		g_error_free (error);
+		goto out;
+	}
+out:
+	return ret;
+}
+
+/**
+ * dkp_client_device_get_history:
+ *
+ * Returns an array of %DkpHistoryObj's
+ **/
+EggObjList *
+dkp_client_device_get_history (const DkpClientDevice *device, const gchar *type, guint timespec)
+{
+	GError *error = NULL;
+	GType g_type_gvalue_array;
+	GPtrArray *gvalue_ptr_array = NULL;
+	GValueArray *gva;
+	GValue *gv;
+	guint i;
+	DkpHistoryObj *obj;
+	EggObjList *array = NULL;
+	gboolean ret;
+
+	g_return_val_if_fail (DKP_IS_CLIENT_DEVICE (device), FALSE);
+
+	g_type_gvalue_array = dbus_g_type_get_collection ("GPtrArray",
+					dbus_g_type_get_struct("GValueArray",
+						G_TYPE_UINT,
+						G_TYPE_DOUBLE,
+						G_TYPE_STRING,
+						G_TYPE_INVALID));
+
+	/* get compound data */
+	ret = dbus_g_proxy_call (device->priv->proxy_device, "GetHistory", &error,
+				 G_TYPE_STRING, type,
+				 G_TYPE_UINT, timespec,
+				 G_TYPE_INVALID,
+				 g_type_gvalue_array, &gvalue_ptr_array,
+				 G_TYPE_INVALID);
+	if (!ret) {
+		egg_debug ("GetHistory(%s,%i) on %s failed: %s", type, timespec,
+			   device->priv->object_path, error->message);
+		g_error_free (error);
+		goto out;
+	}
+
+	/* no data */
+	if (gvalue_ptr_array->len == 0)
+		goto out;
+
+	/* convert */
+	array = egg_obj_list_new ();
+	egg_obj_list_set_copy (array, (EggObjListCopyFunc) dkp_history_obj_copy);
+	egg_obj_list_set_free (array, (EggObjListFreeFunc) dkp_history_obj_free);
+
+	for (i=0; i<gvalue_ptr_array->len; i++) {
+		gva = (GValueArray *) g_ptr_array_index (gvalue_ptr_array, i);
+		obj = dkp_history_obj_new ();
+		/* 0 */
+		gv = g_value_array_get_nth (gva, 0);
+		obj->time = g_value_get_uint (gv);
+		g_value_unset (gv);
+		/* 1 */
+		gv = g_value_array_get_nth (gva, 1);
+		obj->value = g_value_get_double (gv);
+		g_value_unset (gv);
+		/* 2 */
+		gv = g_value_array_get_nth (gva, 2);
+		obj->state = dkp_device_state_from_text (g_value_get_string (gv));
+		g_value_unset (gv);
+		egg_obj_list_add (array, obj);
+		dkp_history_obj_free (obj);
+		g_value_array_free (gva);
+	}
+
+out:
+	if (gvalue_ptr_array != NULL)
+		g_ptr_array_free (gvalue_ptr_array, TRUE);
+	return array;
+}
+
+/**
+ * dkp_client_device_get_statistics:
+ *
+ * Returns an array of %DkpStatsObj's
+ **/
+EggObjList *
+dkp_client_device_get_statistics (const DkpClientDevice *device, const gchar *type)
+{
+	GError *error = NULL;
+	GType g_type_gvalue_array;
+	GPtrArray *gvalue_ptr_array = NULL;
+	GValueArray *gva;
+	GValue *gv;
+	guint i;
+	DkpStatsObj *obj;
+	EggObjList *array = NULL;
+	gboolean ret;
+
+	g_return_val_if_fail (DKP_IS_CLIENT_DEVICE (device), FALSE);
+
+	g_type_gvalue_array = dbus_g_type_get_collection ("GPtrArray",
+					dbus_g_type_get_struct("GValueArray",
+						G_TYPE_DOUBLE,
+						G_TYPE_DOUBLE,
+						G_TYPE_INVALID));
+
+	/* get compound data */
+	ret = dbus_g_proxy_call (device->priv->proxy_device, "GetStatistics", &error,
+				 G_TYPE_STRING, type,
+				 G_TYPE_INVALID,
+				 g_type_gvalue_array, &gvalue_ptr_array,
+				 G_TYPE_INVALID);
+	if (!ret) {
+		egg_debug ("GetStatistics(%s) on %s failed: %s", type,
+			   device->priv->object_path, error->message);
+		g_error_free (error);
+		goto out;
+	}
+
+	/* no data */
+	if (gvalue_ptr_array->len == 0)
+		goto out;
+
+	/* convert */
+	array = egg_obj_list_new ();
+	egg_obj_list_set_copy (array, (EggObjListCopyFunc) dkp_stats_obj_copy);
+	egg_obj_list_set_free (array, (EggObjListFreeFunc) dkp_stats_obj_free);
+	egg_obj_list_set_to_string (array, (EggObjListToStringFunc) dkp_stats_obj_to_string);
+	egg_obj_list_set_from_string (array, (EggObjListFromStringFunc) dkp_stats_obj_from_string);
+
+	for (i=0; i<gvalue_ptr_array->len; i++) {
+		gva = (GValueArray *) g_ptr_array_index (gvalue_ptr_array, i);
+		obj = dkp_stats_obj_new ();
+		/* 0 */
+		gv = g_value_array_get_nth (gva, 0);
+		obj->value = g_value_get_double (gv);
+		g_value_unset (gv);
+		/* 1 */
+		gv = g_value_array_get_nth (gva, 1);
+		obj->accuracy = g_value_get_double (gv);
+		g_value_unset (gv);
+		/* 2 */
+		egg_obj_list_add (array, obj);
+		dkp_stats_obj_free (obj);
+		g_value_array_free (gva);
+	}
+out:
+	if (gvalue_ptr_array != NULL)
+		g_ptr_array_free (gvalue_ptr_array, TRUE);
+	return array;
+}
+
+/**
+ * dkp_client_device_class_init:
+ * @klass: The DkpClientDeviceClass
+ **/
+static void
+dkp_client_device_class_init (DkpClientDeviceClass *klass)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+	object_class->finalize = dkp_client_device_finalize;
+
+	/**
+	 * PkClient::changed:
+	 * @device: the #DkpClientDevice instance that emitted the signal
+	 * @obj: the #DkpObject that has changed
+	 *
+	 * The ::changed signal is emitted when the device data has changed.
+	 **/
+	signals [DKP_CLIENT_DEVICE_CHANGED] =
+		g_signal_new ("changed",
+			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
+			      G_STRUCT_OFFSET (DkpClientDeviceClass, changed),
+			      NULL, NULL, g_cclosure_marshal_VOID__POINTER,
+			      G_TYPE_NONE, 1, G_TYPE_POINTER);
+
+	g_type_class_add_private (klass, sizeof (DkpClientDevicePrivate));
+}
+
+/**
+ * dkp_client_device_init:
+ * @client_device: This class instance
+ **/
+static void
+dkp_client_device_init (DkpClientDevice *device)
+{
+	device->priv = DKP_CLIENT_DEVICE_GET_PRIVATE (device);
+	device->priv->object_path = NULL;
+	device->priv->proxy_device = NULL;
+	device->priv->proxy_props = NULL;
+	device->priv->obj = dkp_object_new ();
+}
+
+/**
+ * dkp_client_device_finalize:
+ * @object: The object to finalize
+ **/
+static void
+dkp_client_device_finalize (GObject *object)
+{
+	DkpClientDevice *device;
+
+	g_return_if_fail (DKP_IS_CLIENT_DEVICE (object));
+
+	device = DKP_CLIENT_DEVICE (object);
+
+	g_free (device->priv->object_path);
+	dkp_object_free (device->priv->obj);
+	if (device->priv->proxy_device != NULL)
+		g_object_unref (device->priv->proxy_device);
+	if (device->priv->proxy_props != NULL)
+		g_object_unref (device->priv->proxy_props);
+	dbus_g_connection_unref (device->priv->bus);
+
+	G_OBJECT_CLASS (dkp_client_device_parent_class)->finalize (object);
+}
+
+/**
+ * dkp_client_device_new:
+ *
+ * Return value: a new DkpClientDevice object.
+ **/
+DkpClientDevice *
+dkp_client_device_new (void)
+{
+	DkpClientDevice *device;
+	device = g_object_new (DKP_TYPE_CLIENT_DEVICE, NULL);
+	return DKP_CLIENT_DEVICE (device);
+}
+

Added: trunk/libdevkit-power/dkp-client-device.h
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/dkp-client-device.h	Mon Nov  3 17:20:59 2008
@@ -0,0 +1,75 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 Richard Hughes <richard hughsie com>
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __DKP_CLIENT_DEVICE_H
+#define __DKP_CLIENT_DEVICE_H
+
+#include <glib-object.h>
+#include <dkp-enum.h>
+#include <dkp-object.h>
+#include "egg-obj-list.h"
+
+G_BEGIN_DECLS
+
+#define DKP_TYPE_CLIENT_DEVICE		(dkp_client_device_get_type ())
+#define DKP_CLIENT_DEVICE(o)		(G_TYPE_CHECK_INSTANCE_CAST ((o), DKP_TYPE_CLIENT_DEVICE, DkpClientDevice))
+#define DKP_CLIENT_DEVICE_CLASS(k)	(G_TYPE_CHECK_CLASS_CAST((k), DKP_TYPE_CLIENT_DEVICE, DkpClientDeviceClass))
+#define DKP_IS_CLIENT_DEVICE(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), DKP_TYPE_CLIENT_DEVICE))
+#define DKP_IS_CLIENT_DEVICE_CLASS(k)	(G_TYPE_CHECK_CLASS_TYPE ((k), DKP_TYPE_CLIENT_DEVICE))
+#define DKP_CLIENT_DEVICE_GET_CLASS(o)	(G_TYPE_INSTANCE_GET_CLASS ((o), DKP_TYPE_CLIENT_DEVICE, DkpClientDeviceClass))
+#define DKP_CLIENT_DEVICE_ERROR		(dkp_client_device_error_quark ())
+#define DKP_CLIENT_DEVICE_TYPE_ERROR	(dkp_client_device_error_get_type ())
+
+typedef struct DkpClientDevicePrivate DkpClientDevicePrivate;
+
+typedef struct
+{
+	 GObject		 parent;
+	 DkpClientDevicePrivate	*priv;
+} DkpClientDevice;
+
+typedef struct
+{
+	GObjectClass		 parent_class;
+	void			(*changed)		(DkpClientDevice	*device,
+							 const DkpObject	*obj);
+} DkpClientDeviceClass;
+
+GType		 dkp_client_device_get_type		(void) G_GNUC_CONST;
+DkpClientDevice	*dkp_client_device_new			(void);
+
+const DkpObject	*dkp_client_device_get_object		(const DkpClientDevice	*device);
+const gchar	*dkp_client_device_get_object_path	(const DkpClientDevice	*device);
+gboolean	 dkp_client_device_set_object_path	(DkpClientDevice	*device,
+							 const gchar		*object_path);
+
+gboolean	 dkp_client_device_print		(const DkpClientDevice	*device);
+gboolean	 dkp_client_device_refresh		(DkpClientDevice	*device);
+EggObjList	*dkp_client_device_get_history		(const DkpClientDevice	*device,
+							 const gchar		*type,
+							 guint			 timespec);
+EggObjList	*dkp_client_device_get_statistics	(const DkpClientDevice	*device,
+							 const gchar		*type);
+
+G_END_DECLS
+
+#endif /* __DKP_CLIENT_DEVICE_H */
+

Added: trunk/libdevkit-power/dkp-client.c
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/dkp-client.c	Mon Nov  3 17:20:59 2008
@@ -0,0 +1,289 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 Richard Hughes <richard hughsie com>
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <glib.h>
+#include <dbus/dbus-glib.h>
+
+#include "egg-debug.h"
+#include "dkp-client.h"
+#include "dkp-client-device.h"
+
+static void	dkp_client_class_init	(DkpClientClass	*klass);
+static void	dkp_client_init		(DkpClient	*client);
+static void	dkp_client_finalize	(GObject	*object);
+
+#define DKP_CLIENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DKP_TYPE_CLIENT, DkpClientPrivate))
+
+struct DkpClientPrivate
+{
+	DBusGConnection		*bus;
+	DBusGProxy		*proxy;
+	GHashTable		*hash;
+	GPtrArray		*array;
+};
+
+enum {
+	DKP_CLIENT_ADDED,
+	DKP_CLIENT_CHANGED,
+	DKP_CLIENT_REMOVED,
+	DKP_CLIENT_LAST_SIGNAL
+};
+
+static guint signals [DKP_CLIENT_LAST_SIGNAL] = { 0 };
+
+G_DEFINE_TYPE (DkpClient, dkp_client, G_TYPE_OBJECT)
+
+/**
+ * dkp_client_get_device:
+ **/
+static DkpClientDevice *
+dkp_client_get_device (DkpClient *client, const gchar *object_path)
+{
+	DkpClientDevice *device;
+	device = g_hash_table_lookup (client->priv->hash, object_path);
+	return device;
+}
+
+/**
+ * dkp_client_enumerate_devices:
+ **/
+GPtrArray *
+dkp_client_enumerate_devices (const DkpClient *client)
+{
+	gboolean ret;
+	GError *error = NULL;
+	GPtrArray *devices = NULL;
+	GType g_type_array;
+
+	g_type_array = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH);
+	ret = dbus_g_proxy_call (client->priv->proxy, "EnumerateDevices", &error,
+				 G_TYPE_INVALID,
+				 g_type_array, &devices,
+				 G_TYPE_INVALID);
+	if (!ret) {
+		egg_warning ("Couldn't enumerate devices: %s", error->message);
+		g_error_free (error);
+	}
+	return devices;
+}
+
+/**
+ * dkp_client_add:
+ **/
+static DkpClientDevice *
+dkp_client_add (DkpClient *client, const gchar *object_path)
+{
+	DkpClientDevice *device;
+
+	/* create new device */
+	device = dkp_client_device_new ();
+	dkp_client_device_set_object_path (device, object_path);
+
+	g_ptr_array_add (client->priv->array, device);
+	g_hash_table_insert (client->priv->hash, g_strdup (object_path), device);
+	return device;
+}
+
+/**
+ * dkp_client_remove:
+ **/
+static gboolean
+dkp_client_remove (DkpClient *client, DkpClientDevice *device)
+{
+	/* deallocate it */
+	g_object_unref (device);
+
+	g_ptr_array_remove (client->priv->array, device);
+	g_hash_table_remove (client->priv->hash, device);
+	return TRUE;
+}
+
+/**
+ * dkp_client_added_cb:
+ **/
+static void
+dkp_client_added_cb (DBusGProxy *proxy, const gchar *object_path, DkpClient *client)
+{
+	DkpClientDevice *device;
+
+	/* create new device */
+	device = dkp_client_add (client, object_path);
+	g_signal_emit (client, signals [DKP_CLIENT_ADDED], 0, device);
+}
+
+/**
+ * dkp_client_changed_cb:
+ **/
+static void
+dkp_client_changed_cb (DBusGProxy *proxy, const gchar *object_path, DkpClient *client)
+{
+	DkpClientDevice *device;
+	device = dkp_client_get_device (client, object_path);
+	if (device != NULL)
+		g_signal_emit (client, signals [DKP_CLIENT_CHANGED], 0, device);
+}
+
+/**
+ * dkp_client_removed_cb:
+ **/
+static void
+dkp_client_removed_cb (DBusGProxy *proxy, const gchar *object_path, DkpClient *client)
+{
+	DkpClientDevice *device;
+	device = dkp_client_get_device (client, object_path);
+	if (device != NULL)
+		g_signal_emit (client, signals [DKP_CLIENT_REMOVED], 0, device);
+	dkp_client_remove (client, device);
+}
+
+/**
+ * dkp_client_class_init:
+ * @klass: The DkpClientClass
+ **/
+static void
+dkp_client_class_init (DkpClientClass *klass)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+	object_class->finalize = dkp_client_finalize;
+
+	signals [DKP_CLIENT_ADDED] =
+		g_signal_new ("added",
+			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
+			      G_STRUCT_OFFSET (DkpClientClass, added),
+			      NULL, NULL, g_cclosure_marshal_VOID__POINTER,
+			      G_TYPE_NONE, 1, G_TYPE_POINTER);
+	signals [DKP_CLIENT_REMOVED] =
+		g_signal_new ("removed",
+			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
+			      G_STRUCT_OFFSET (DkpClientClass, removed),
+			      NULL, NULL, g_cclosure_marshal_VOID__POINTER,
+			      G_TYPE_NONE, 1, G_TYPE_POINTER);
+	signals [DKP_CLIENT_CHANGED] =
+		g_signal_new ("changed",
+			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
+			      G_STRUCT_OFFSET (DkpClientClass, changed),
+			      NULL, NULL, g_cclosure_marshal_VOID__POINTER,
+			      G_TYPE_NONE, 1, G_TYPE_POINTER);
+
+	g_type_class_add_private (klass, sizeof (DkpClientPrivate));
+}
+
+/**
+ * dkp_client_init:
+ * @client: This class instance
+ **/
+static void
+dkp_client_init (DkpClient *client)
+{
+	GError *error = NULL;
+	const gchar *object_path;
+	GPtrArray *devices;
+	guint i;
+
+	client->priv = DKP_CLIENT_GET_PRIVATE (client);
+	client->priv->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+	client->priv->array = g_ptr_array_new ();
+
+	/* get on the bus */
+	client->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
+	if (client->priv->bus == NULL) {
+		egg_warning ("Couldn't connect to system bus: %s", error->message);
+		g_error_free (error);
+		goto out;
+	}
+
+	/* connect to main interface */
+	client->priv->proxy = dbus_g_proxy_new_for_name (client->priv->bus, "org.freedesktop.DeviceKit.Power",
+							 "/", "org.freedesktop.DeviceKit.Power");
+	if (client->priv->proxy == NULL) {
+		egg_warning ("Couldn't connect to proxy");
+		goto out;
+	}
+
+	dbus_g_proxy_add_signal (client->priv->proxy, "DeviceAdded", G_TYPE_STRING, G_TYPE_INVALID);
+	dbus_g_proxy_add_signal (client->priv->proxy, "DeviceRemoved", G_TYPE_STRING, G_TYPE_INVALID);
+	dbus_g_proxy_add_signal (client->priv->proxy, "DeviceChanged", G_TYPE_STRING, G_TYPE_INVALID);
+
+	/* all callbacks */
+	dbus_g_proxy_connect_signal (client->priv->proxy, "DeviceAdded",
+				     G_CALLBACK (dkp_client_added_cb), client, NULL);
+	dbus_g_proxy_connect_signal (client->priv->proxy, "DeviceRemoved",
+				     G_CALLBACK (dkp_client_removed_cb), client, NULL);
+	dbus_g_proxy_connect_signal (client->priv->proxy, "DeviceChanged",
+				     G_CALLBACK (dkp_client_changed_cb), client, NULL);
+
+	/* coldplug */
+	devices = dkp_client_enumerate_devices (client);
+	if (devices == NULL)
+		goto out;
+	for (i=0; i<devices->len; i++) {
+		object_path = (const gchar *) g_ptr_array_index (devices, i);
+		dkp_client_add (client, object_path);
+	}
+out:
+	return;
+}
+
+/**
+ * dkp_client_finalize:
+ * @object: The object to finalize
+ **/
+static void
+dkp_client_finalize (GObject *object)
+{
+	DkpClient *client;
+	DkpClientDevice *device;
+	guint i;
+
+	g_return_if_fail (DKP_IS_CLIENT (object));
+
+	client = DKP_CLIENT (object);
+
+	/* free any devices */
+	for (i=0; i<client->priv->array->len; i++) {
+		device = (DkpClientDevice *) g_ptr_array_index (client->priv->array, i);
+		dkp_client_remove (client, device);
+	}
+
+	g_ptr_array_free (client->priv->array, TRUE);
+	g_hash_table_unref (client->priv->hash);
+	dbus_g_connection_unref (client->priv->bus);
+
+	G_OBJECT_CLASS (dkp_client_parent_class)->finalize (object);
+}
+
+/**
+ * dkp_client_new:
+ *
+ * Return value: a new DkpClient object.
+ **/
+DkpClient *
+dkp_client_new (void)
+{
+	DkpClient *client;
+	client = g_object_new (DKP_TYPE_CLIENT, NULL);
+	return DKP_CLIENT (client);
+}
+

Added: trunk/libdevkit-power/dkp-client.h
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/dkp-client.h	Mon Nov  3 17:20:59 2008
@@ -0,0 +1,66 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 Richard Hughes <richard hughsie com>
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __DKP_CLIENT_H
+#define __DKP_CLIENT_H
+
+#include <glib-object.h>
+#include <dkp-enum.h>
+#include "dkp-client-device.h"
+
+G_BEGIN_DECLS
+
+#define DKP_TYPE_CLIENT			(dkp_client_get_type ())
+#define DKP_CLIENT(o)			(G_TYPE_CHECK_INSTANCE_CAST ((o), DKP_TYPE_CLIENT, DkpClient))
+#define DKP_CLIENT_CLASS(k)		(G_TYPE_CHECK_CLASS_CAST((k), DKP_TYPE_CLIENT, DkpClientClass))
+#define DKP_IS_CLIENT(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), DKP_TYPE_CLIENT))
+#define DKP_IS_CLIENT_CLASS(k)		(G_TYPE_CHECK_CLASS_TYPE ((k), DKP_TYPE_CLIENT))
+#define DKP_CLIENT_GET_CLASS(o)		(G_TYPE_INSTANCE_GET_CLASS ((o), DKP_TYPE_CLIENT, DkpClientClass))
+#define DKP_CLIENT_ERROR		(dkp_client_error_quark ())
+#define DKP_CLIENT_TYPE_ERROR		(dkp_client_error_get_type ())
+
+typedef struct DkpClientPrivate DkpClientPrivate;
+
+typedef struct
+{
+	 GObject		 parent;
+	 DkpClientPrivate	*priv;
+} DkpClient;
+
+typedef struct
+{
+	GObjectClass		 parent_class;
+	void			(*added)		(DkpClient		*client,
+							 const DkpClientDevice	*device);
+	void			(*changed)		(DkpClient		*client,
+							 const DkpClientDevice	*device);
+	void			(*removed)		(DkpClient		*client,
+							 const DkpClientDevice	*device);
+} DkpClientClass;
+
+GType		 dkp_client_get_type			(void) G_GNUC_CONST;
+DkpClient	*dkp_client_new				(void);
+GPtrArray	*dkp_client_enumerate_devices		(const DkpClient	*client);
+
+G_END_DECLS
+
+#endif /* __DKP_CLIENT_H */
+

Added: trunk/libdevkit-power/dkp-enum.c
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/dkp-enum.c	Mon Nov  3 17:20:59 2008
@@ -0,0 +1,205 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 David Zeuthen <david fubar dk>
+ * Copyright (C) 2008 Richard Hughes <richard hughsie com>
+ *
+ * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include <glib.h>
+#include <string.h>
+#include "egg-debug.h"
+#include "egg-string.h"
+
+#include "dkp-enum.h"
+
+/**
+ * dkp_device_type_to_text:
+ **/
+const gchar *
+dkp_device_type_to_text (DkpDeviceType type_enum)
+{
+	const gchar *type = NULL;
+	switch (type_enum) {
+	case DKP_DEVICE_TYPE_LINE_POWER:
+		type = "line-power";
+		break;
+	case DKP_DEVICE_TYPE_BATTERY:
+		type = "battery";
+		break;
+	case DKP_DEVICE_TYPE_UPS:
+		type = "ups";
+		break;
+	case DKP_DEVICE_TYPE_MONITOR:
+		type = "monitor";
+		break;
+	case DKP_DEVICE_TYPE_MOUSE:
+		type = "mouse";
+		break;
+	case DKP_DEVICE_TYPE_KEYBOARD:
+		type = "keyboard";
+		break;
+	case DKP_DEVICE_TYPE_PDA:
+		type = "pda";
+		break;
+	case DKP_DEVICE_TYPE_PHONE:
+		type = "phone";
+		break;
+	case DKP_DEVICE_TYPE_UNKNOWN:
+		type = "unknown";
+		break;
+	default:
+		g_assert_not_reached ();
+		break;
+	}
+	return type;
+}
+
+/**
+ * dkp_device_type_from_text:
+ **/
+DkpDeviceType
+dkp_device_type_from_text (const gchar *type)
+{
+	if (type == NULL)
+		return DKP_DEVICE_TYPE_UNKNOWN;
+	if (egg_strequal (type, "line-power"))
+		return DKP_DEVICE_TYPE_LINE_POWER;
+	if (egg_strequal (type, "battery"))
+		return DKP_DEVICE_TYPE_BATTERY;
+	if (egg_strequal (type, "ups"))
+		return DKP_DEVICE_TYPE_UPS;
+	if (egg_strequal (type, "monitor"))
+		return DKP_DEVICE_TYPE_MONITOR;
+	if (egg_strequal (type, "mouse"))
+		return DKP_DEVICE_TYPE_MOUSE;
+	if (egg_strequal (type, "keyboard"))
+		return DKP_DEVICE_TYPE_KEYBOARD;
+	if (egg_strequal (type, "pda"))
+		return DKP_DEVICE_TYPE_PDA;
+	if (egg_strequal (type, "phone"))
+		return DKP_DEVICE_TYPE_PHONE;
+	return DKP_DEVICE_TYPE_UNKNOWN;
+}
+
+/**
+ * dkp_device_state_to_text:
+ **/
+const gchar *
+dkp_device_state_to_text (DkpDeviceState state_enum)
+{
+	const gchar *state = NULL;
+	switch (state_enum) {
+	case DKP_DEVICE_STATE_CHARGING:
+		state = "charging";
+		break;
+	case DKP_DEVICE_STATE_DISCHARGING:
+		state = "discharging";
+		break;
+	case DKP_DEVICE_STATE_EMPTY:
+		state = "empty";
+		break;
+	case DKP_DEVICE_STATE_FULLY_CHARGED:
+		state = "fully-charged";
+		break;
+	case DKP_DEVICE_STATE_UNKNOWN:
+		state = "unknown";
+		break;
+	default:
+		g_assert_not_reached ();
+		break;
+	}
+	return state;
+}
+
+/**
+ * dkp_device_state_from_text:
+ **/
+DkpDeviceState
+dkp_device_state_from_text (const gchar *state)
+{
+	if (state == NULL)
+		return DKP_DEVICE_STATE_UNKNOWN;
+	if (egg_strequal (state, "charging"))
+		return DKP_DEVICE_STATE_CHARGING;
+	if (egg_strequal (state, "discharging"))
+		return DKP_DEVICE_STATE_DISCHARGING;
+	if (egg_strequal (state, "empty"))
+		return DKP_DEVICE_STATE_EMPTY;
+	if (egg_strequal (state, "fully-charged"))
+		return DKP_DEVICE_STATE_FULLY_CHARGED;
+	return DKP_DEVICE_STATE_UNKNOWN;
+}
+
+/**
+ * dkp_device_technology_to_text:
+ **/
+const gchar *
+dkp_device_technology_to_text (DkpDeviceTechnology technology_enum)
+{
+	const gchar *technology = NULL;
+	switch (technology_enum) {
+	case DKP_DEVICE_TECHNOLGY_LITHIUM_ION:
+		technology = "lithium-ion";
+		break;
+	case DKP_DEVICE_TECHNOLGY_LITHIUM_POLYMER:
+		technology = "lithium-polymer";
+		break;
+	case DKP_DEVICE_TECHNOLGY_LITHIUM_IRON_PHOSPHATE:
+		technology = "lithium-iron-phosphate";
+		break;
+	case DKP_DEVICE_TECHNOLGY_LEAD_ACID:
+		technology = "lead-acid";
+		break;
+	case DKP_DEVICE_TECHNOLGY_NICKEL_CADMIUM:
+		technology = "nickel-cadmium";
+		break;
+	case DKP_DEVICE_TECHNOLGY_NICKEL_METAL_HYDRIDE:
+		technology = "nickel-metal-hydride";
+		break;
+	case DKP_DEVICE_TECHNOLGY_UNKNOWN:
+		technology = "unknown";
+		break;
+	default:
+		g_assert_not_reached ();
+		break;
+	}
+	return technology;
+}
+
+/**
+ * dkp_device_technology_from_text:
+ **/
+DkpDeviceTechnology
+dkp_device_technology_from_text (const gchar *technology)
+{
+	if (technology == NULL)
+		return DKP_DEVICE_TECHNOLGY_UNKNOWN;
+	if (egg_strequal (technology, "lithium-ion"))
+		return DKP_DEVICE_TECHNOLGY_LITHIUM_ION;
+	if (egg_strequal (technology, "lithium-polymer"))
+		return DKP_DEVICE_TECHNOLGY_LITHIUM_POLYMER;
+	if (egg_strequal (technology, "lithium-iron-phosphate"))
+		return DKP_DEVICE_TECHNOLGY_LITHIUM_IRON_PHOSPHATE;
+	if (egg_strequal (technology, "lead-acid"))
+		return DKP_DEVICE_TECHNOLGY_LEAD_ACID;
+	if (egg_strequal (technology, "nickel-cadmium"))
+		return DKP_DEVICE_TECHNOLGY_NICKEL_CADMIUM;
+	if (egg_strequal (technology, "nickel-metal-hydride"))
+		return DKP_DEVICE_TECHNOLGY_NICKEL_METAL_HYDRIDE;
+	return DKP_DEVICE_TECHNOLGY_UNKNOWN;
+}
+

Added: trunk/libdevkit-power/dkp-enum.h
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/dkp-enum.h	Mon Nov  3 17:20:59 2008
@@ -0,0 +1,69 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 David Zeuthen <david fubar dk>
+ * Copyright (C) 2008 Richard Hughes <richard hughsie com>
+ *
+ * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __DKP_ENUM_H__
+#define __DKP_ENUM_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+typedef enum {
+	DKP_DEVICE_TYPE_LINE_POWER,
+	DKP_DEVICE_TYPE_BATTERY,
+	DKP_DEVICE_TYPE_UPS,
+	DKP_DEVICE_TYPE_MONITOR,
+	DKP_DEVICE_TYPE_MOUSE,
+	DKP_DEVICE_TYPE_KEYBOARD,
+	DKP_DEVICE_TYPE_PDA,
+	DKP_DEVICE_TYPE_PHONE,
+	DKP_DEVICE_TYPE_UNKNOWN
+} DkpDeviceType;
+
+typedef enum {
+	DKP_DEVICE_STATE_CHARGING,
+	DKP_DEVICE_STATE_DISCHARGING,
+	DKP_DEVICE_STATE_EMPTY,
+	DKP_DEVICE_STATE_FULLY_CHARGED,
+	DKP_DEVICE_STATE_UNKNOWN
+} DkpDeviceState;
+
+typedef enum {
+	DKP_DEVICE_TECHNOLGY_LITHIUM_ION,
+	DKP_DEVICE_TECHNOLGY_LITHIUM_POLYMER,
+	DKP_DEVICE_TECHNOLGY_LITHIUM_IRON_PHOSPHATE,
+	DKP_DEVICE_TECHNOLGY_LEAD_ACID,
+	DKP_DEVICE_TECHNOLGY_NICKEL_CADMIUM,
+	DKP_DEVICE_TECHNOLGY_NICKEL_METAL_HYDRIDE,
+	DKP_DEVICE_TECHNOLGY_UNKNOWN
+} DkpDeviceTechnology;
+
+const gchar	*dkp_device_type_to_text	(DkpDeviceType		 type_enum);
+const gchar	*dkp_device_state_to_text	(DkpDeviceState		 state_enum);
+const gchar	*dkp_device_technology_to_text	(DkpDeviceTechnology	 technology_enum);
+DkpDeviceType	 dkp_device_type_from_text	(const gchar		*type);
+DkpDeviceState	 dkp_device_state_from_text	(const gchar		*state);
+DkpDeviceTechnology dkp_device_technology_from_text (const gchar	*technology);
+
+G_END_DECLS
+
+#endif /* __DKP_ENUM_H__ */
+

Added: trunk/libdevkit-power/dkp-history-obj.c
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/dkp-history-obj.c	Mon Nov  3 17:20:59 2008
@@ -0,0 +1,173 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 Richard Hughes <richard hughsie com>
+ *
+ * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include <glib.h>
+#include <glib-object.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "egg-debug.h"
+#include "dkp-enum.h"
+#include "dkp-history-obj.h"
+
+/**
+ * dkp_history_obj_clear_internal:
+ **/
+static void
+dkp_history_obj_clear_internal (DkpHistoryObj *obj)
+{
+	obj->time = 0;
+	obj->value = 0.0f;
+	obj->state = 0;
+}
+
+/**
+ * dkp_history_obj_copy:
+ **/
+DkpHistoryObj *
+dkp_history_obj_copy (const DkpHistoryObj *cobj)
+{
+	DkpHistoryObj *obj;
+	obj = g_new0 (DkpHistoryObj, 1);
+	obj->time = cobj->time;
+	obj->value = cobj->value;
+	obj->state = cobj->state;
+	return obj;
+}
+
+/**
+ * dkp_history_obj_equal:
+ **/
+gboolean
+dkp_history_obj_equal (const DkpHistoryObj *obj1, const DkpHistoryObj *obj2)
+{
+	if (obj1->time == obj2->time &&
+	    obj1->value == obj2->value &&
+	    obj1->state == obj2->state)
+		return TRUE;
+	return FALSE;
+}
+
+/**
+ * dkp_history_obj_print:
+ **/
+gboolean
+dkp_history_obj_print (const DkpHistoryObj *obj)
+{
+	g_print ("%i\t%.3f\t%s", obj->time, obj->value, dkp_device_state_to_text (obj->state));
+	return TRUE;
+}
+
+/**
+ * dkp_history_obj_new:
+ **/
+DkpHistoryObj *
+dkp_history_obj_new (void)
+{
+	DkpHistoryObj *obj;
+	obj = g_new0 (DkpHistoryObj, 1);
+	dkp_history_obj_clear_internal (obj);
+	return obj;
+}
+
+/**
+ * dkp_history_obj_clear:
+ **/
+gboolean
+dkp_history_obj_clear (DkpHistoryObj *obj)
+{
+	if (obj == NULL)
+		return FALSE;
+	dkp_history_obj_free (obj);
+	dkp_history_obj_clear_internal (obj);
+	return TRUE;
+}
+
+/**
+ * dkp_history_obj_free:
+ **/
+gboolean
+dkp_history_obj_free (DkpHistoryObj *obj)
+{
+	if (obj == NULL)
+		return FALSE;
+	g_free (obj);
+	return TRUE;
+}
+
+/**
+ * dkp_history_obj_create:
+ **/
+DkpHistoryObj *
+dkp_history_obj_create (gdouble value, DkpDeviceState state)
+{
+	DkpHistoryObj *obj;
+	GTimeVal timeval;
+
+	g_get_current_time (&timeval);
+	obj = dkp_history_obj_new ();
+	obj->time = timeval.tv_sec;
+	obj->value = value;
+	obj->state = state;
+	return obj;
+}
+
+/**
+ * dkp_history_obj_from_string:
+ **/
+DkpHistoryObj *
+dkp_history_obj_from_string (const gchar *text)
+{
+	DkpHistoryObj *obj = NULL;
+	gchar **parts = NULL;
+	guint length;
+
+	if (text == NULL)
+		goto out;
+
+	/* split by tab */
+	parts = g_strsplit (text, "\t", 0);
+	length = g_strv_length (parts);
+	if (length != 3) {
+		egg_warning ("invalid string: '%s'", text);
+		goto out;
+	}
+
+	/* parse and create */
+	obj = dkp_history_obj_new ();
+	obj->time = atoi (parts[0]);
+	obj->value = atof (parts[1]);
+	obj->state = dkp_device_state_from_text (parts[2]);
+out:
+	g_strfreev (parts);
+	return obj;
+}
+
+/**
+ * dkp_history_obj_to_string:
+ **/
+gchar *
+dkp_history_obj_to_string (const DkpHistoryObj *obj)
+{
+	if (obj == NULL)
+		return NULL;
+	return g_strdup_printf ("%i\t%.3f\t%s", obj->time, obj->value, dkp_device_state_to_text (obj->state));
+}
+

Added: trunk/libdevkit-power/dkp-history-obj.h
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/dkp-history-obj.h	Mon Nov  3 17:20:59 2008
@@ -0,0 +1,51 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 Richard Hughes <richard hughsie com>
+ *
+ * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __DKP_HISTORY_OBJ_H__
+#define __DKP_HISTORY_OBJ_H__
+
+#include <glib.h>
+#include "dkp-enum.h"
+
+G_BEGIN_DECLS
+
+typedef struct
+{
+	guint			 time;
+	gdouble			 value;
+	DkpDeviceState		 state;
+} DkpHistoryObj;
+
+DkpHistoryObj	*dkp_history_obj_new		(void);
+gboolean	 dkp_history_obj_clear		(DkpHistoryObj		*obj);
+gboolean	 dkp_history_obj_free		(DkpHistoryObj		*obj);
+DkpHistoryObj	*dkp_history_obj_copy		(const DkpHistoryObj	*cobj);
+gboolean	 dkp_history_obj_print		(const DkpHistoryObj	*obj);
+DkpHistoryObj	*dkp_history_obj_create		(gdouble		 value,
+						 DkpDeviceState		 state);
+gboolean	 dkp_history_obj_equal		(const DkpHistoryObj	*obj1,
+						 const DkpHistoryObj	*obj2);
+DkpHistoryObj	*dkp_history_obj_from_string	(const gchar		*text);
+gchar		*dkp_history_obj_to_string	(const DkpHistoryObj	*obj);
+
+G_END_DECLS
+
+#endif /* __DKP_HISTORY_OBJ_H__ */
+

Added: trunk/libdevkit-power/dkp-object.c
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/dkp-object.c	Mon Nov  3 17:20:59 2008
@@ -0,0 +1,523 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 Richard Hughes <richard hughsie com>
+ *
+ * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include <glib.h>
+#include <glib-object.h>
+#include <string.h>
+
+#include "egg-debug.h"
+#include "egg-string.h"
+
+#include "dkp-enum.h"
+#include "dkp-object.h"
+
+/**
+ * dkp_object_clear_internal:
+ **/
+static void
+dkp_object_clear_internal (DkpObject *obj)
+{
+	obj->type = DKP_DEVICE_TYPE_UNKNOWN;
+	obj->update_time = 0;
+	obj->energy = -1;
+	obj->energy_full = -1;
+	obj->energy_full_design = -1;
+	obj->energy_rate = -1;
+	obj->percentage = -1;
+	obj->capacity = -1;
+	obj->time_to_empty = -1;
+	obj->time_to_full = -1;
+	obj->state = DKP_DEVICE_STATE_UNKNOWN;
+	obj->technology = DKP_DEVICE_TECHNOLGY_UNKNOWN;
+	obj->vendor = NULL;
+	obj->model = NULL;
+	obj->serial = NULL;
+	obj->native_path = NULL;
+	obj->online = FALSE;
+	obj->is_present = FALSE;
+	obj->power_supply = FALSE;
+	obj->is_rechargeable = FALSE;
+	obj->has_history = FALSE;
+	obj->has_statistics = FALSE;
+}
+
+/**
+ * dkp_object_collect_props:
+ **/
+static void
+dkp_object_collect_props (const char *key, const GValue *value, DkpObject *obj)
+{
+	gboolean handled = TRUE;
+
+	if (egg_strequal (key, "native-path"))
+		obj->native_path = g_strdup (g_value_get_string (value));
+	else if (egg_strequal (key, "vendor"))
+		obj->vendor = g_strdup (g_value_get_string (value));
+	else if (egg_strequal (key, "model"))
+		obj->model = g_strdup (g_value_get_string (value));
+	else if (egg_strequal (key, "serial"))
+		obj->serial = g_strdup (g_value_get_string (value));
+	else if (egg_strequal (key, "update-time"))
+		obj->update_time = g_value_get_uint64 (value);
+	else if (egg_strequal (key, "type"))
+		obj->type = dkp_device_type_from_text (g_value_get_string (value));
+	else if (egg_strequal (key, "online"))
+		obj->online = g_value_get_boolean (value);
+	else if (egg_strequal (key, "has-history"))
+		obj->has_history = g_value_get_boolean (value);
+	else if (egg_strequal (key, "has-statistics"))
+		obj->has_statistics = g_value_get_boolean (value);
+	else if (egg_strequal (key, "energy"))
+		obj->energy = g_value_get_double (value);
+	else if (egg_strequal (key, "energy-empty"))
+		obj->energy_empty = g_value_get_double (value);
+	else if (egg_strequal (key, "energy-full"))
+		obj->energy_full = g_value_get_double (value);
+	else if (egg_strequal (key, "energy-full-design"))
+		obj->energy_full_design = g_value_get_double (value);
+	else if (egg_strequal (key, "energy-rate"))
+		obj->energy_rate = g_value_get_double (value);
+	else if (egg_strequal (key, "time-to-full"))
+		obj->time_to_full = g_value_get_int64 (value);
+	else if (egg_strequal (key, "time-to-empty"))
+		obj->time_to_empty = g_value_get_int64 (value);
+	else if (egg_strequal (key, "percentage"))
+		obj->percentage = g_value_get_double (value);
+	else if (egg_strequal (key, "technology"))
+		obj->technology = dkp_device_technology_from_text (g_value_get_string (value));
+	else if (egg_strequal (key, "is-present"))
+		obj->is_present = g_value_get_boolean (value);
+	else if (egg_strequal (key, "is-rechargeable"))
+		obj->is_rechargeable = g_value_get_boolean (value);
+	else if (egg_strequal (key, "power-supply"))
+		obj->power_supply = g_value_get_boolean (value);
+	else if (egg_strequal (key, "capacity"))
+		obj->capacity = g_value_get_double (value);
+	else if (egg_strequal (key, "state"))
+		obj->state = dkp_device_state_from_text (g_value_get_string (value));
+	else
+		handled = FALSE;
+
+	if (!handled)
+		egg_warning ("unhandled property '%s'", key);
+}
+
+/**
+ * dkp_object_set_from_map:
+ **/
+gboolean
+dkp_object_set_from_map	(DkpObject *obj, GHashTable *hash_table)
+{
+	g_hash_table_foreach (hash_table, (GHFunc) dkp_object_collect_props, obj);
+	return TRUE;
+}
+
+/**
+ * dkp_object_copy:
+ **/
+DkpObject *
+dkp_object_copy (const DkpObject *cobj)
+{
+	DkpObject *obj;
+	obj = g_new0 (DkpObject, 1);
+
+	obj->type = cobj->type;
+	obj->update_time = cobj->update_time;
+	obj->energy = cobj->energy;
+	obj->energy_full = cobj->energy_full;
+	obj->energy_full_design = cobj->energy_full_design;
+	obj->energy_rate = cobj->energy_rate;
+	obj->percentage = cobj->percentage;
+	obj->capacity = cobj->capacity;
+	obj->time_to_empty = cobj->time_to_empty;
+	obj->time_to_full = cobj->time_to_full;
+	obj->state = cobj->state;
+	obj->technology = cobj->technology;
+	obj->vendor = g_strdup (cobj->vendor);
+	obj->model = g_strdup (cobj->model);
+	obj->serial = g_strdup (cobj->serial);
+	obj->native_path = g_strdup (cobj->native_path);
+	obj->online = cobj->online;
+	obj->is_present = cobj->is_present;
+	obj->power_supply = cobj->power_supply;
+	obj->is_rechargeable = cobj->is_rechargeable;
+	obj->has_history = cobj->has_history;
+	obj->has_statistics = cobj->has_statistics;
+
+	return obj;
+}
+
+/**
+ * dkp_object_equal:
+ **/
+gboolean
+dkp_object_equal (const DkpObject *obj1, const DkpObject *obj2)
+{
+	if (obj1->type == obj2->type &&
+	    obj1->update_time == obj2->update_time &&
+	    obj1->energy == obj2->energy &&
+	    obj1->energy_full == obj2->energy_full &&
+	    obj1->energy_full_design == obj2->energy_full_design &&
+	    obj1->energy_rate == obj2->energy_rate &&
+	    obj1->percentage == obj2->percentage &&
+	    obj1->has_history == obj2->has_history &&
+	    obj1->has_statistics == obj2->has_statistics &&
+	    obj1->capacity == obj2->capacity &&
+	    obj1->time_to_empty == obj2->time_to_empty &&
+	    obj1->time_to_full == obj2->time_to_full &&
+	    obj1->state == obj2->state &&
+	    obj1->technology == obj2->technology &&
+	    egg_strequal (obj1->vendor, obj2->vendor) &&
+	    egg_strequal (obj1->model, obj2->model) &&
+	    egg_strequal (obj1->serial, obj2->serial) &&
+	    egg_strequal (obj1->native_path, obj2->native_path) &&
+	    obj1->online == obj2->online &&
+	    obj1->is_present == obj2->is_present &&
+	    obj1->power_supply == obj2->power_supply &&
+	    obj1->is_rechargeable == obj2->is_rechargeable)
+		return TRUE;
+	return FALSE;
+}
+
+/**
+ * dkp_object_time_to_text:
+ **/
+static gchar *
+dkp_object_time_to_text (gint seconds)
+{
+	gfloat value = seconds;
+
+	if (value < 0)
+		return g_strdup ("unknown");
+	if (value < 60)
+		return g_strdup_printf ("%.0f seconds", value);
+	value /= 60.0;
+	if (value < 60)
+		return g_strdup_printf ("%.1f minutes", value);
+	value /= 60.0;
+	if (value < 60)
+		return g_strdup_printf ("%.1f hours", value);
+	value /= 24.0;
+	return g_strdup_printf ("%.1f days", value);
+}
+
+/**
+ * dkp_object_bool_to_text:
+ **/
+static const gchar *
+dkp_object_bool_to_text (gboolean ret)
+{
+	return ret ? "yes" : "no";
+}
+
+/**
+ * dkp_object_print:
+ **/
+gboolean
+dkp_object_print (const DkpObject *obj)
+{
+	gboolean ret = TRUE;
+	struct tm *time_tm;
+	time_t t;
+	gchar time_buf[256];
+	gchar *time_str;
+
+	/* get a human readable time */
+	t = (time_t) obj->update_time;
+	time_tm = localtime (&t);
+	strftime (time_buf, sizeof time_buf, "%c", time_tm);
+
+	g_print ("  native-path:          %s\n", obj->native_path);
+	if (!egg_strzero (obj->vendor))
+		g_print ("  vendor:               %s\n", obj->vendor);
+	if (!egg_strzero (obj->model))
+		g_print ("  model:                %s\n", obj->model);
+	if (!egg_strzero (obj->serial))
+		g_print ("  serial:               %s\n", obj->serial);
+	g_print ("  power supply:         %s\n", dkp_object_bool_to_text (obj->power_supply));
+	g_print ("  updated:              %s (%d seconds ago)\n", time_buf, (int) (time (NULL) - obj->update_time));
+	g_print ("  has history:          %s\n", dkp_object_bool_to_text (obj->has_history));
+	g_print ("  has statistics:       %s\n", dkp_object_bool_to_text (obj->has_statistics));
+	g_print ("  %s\n", dkp_device_type_to_text (obj->type));
+
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
+	    obj->type == DKP_DEVICE_TYPE_MOUSE ||
+	    obj->type == DKP_DEVICE_TYPE_KEYBOARD ||
+	    obj->type == DKP_DEVICE_TYPE_UPS)
+		g_print ("    present:             %s\n", dkp_object_bool_to_text (obj->is_present));
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
+	    obj->type == DKP_DEVICE_TYPE_MOUSE ||
+	    obj->type == DKP_DEVICE_TYPE_KEYBOARD)
+		g_print ("    rechargeable:        %s\n", dkp_object_bool_to_text (obj->is_rechargeable));
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
+	    obj->type == DKP_DEVICE_TYPE_MOUSE ||
+	    obj->type == DKP_DEVICE_TYPE_KEYBOARD)
+		g_print ("    state:               %s\n", dkp_device_state_to_text (obj->state));
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY) {
+		g_print ("    energy:              %g Wh\n", obj->energy);
+		g_print ("    energy-empty:        %g Wh\n", obj->energy_empty);
+		g_print ("    energy-full:         %g Wh\n", obj->energy_full);
+		g_print ("    energy-full-design:  %g Wh\n", obj->energy_full_design);
+	}
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
+	    obj->type == DKP_DEVICE_TYPE_MONITOR)
+		g_print ("    energy-rate:         %g W\n", obj->energy_rate);
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
+	    obj->type == DKP_DEVICE_TYPE_UPS) {
+		if (obj->time_to_full >= 0) {
+			time_str = dkp_object_time_to_text (obj->time_to_full);
+			g_print ("    time to full:        %s\n", time_str);
+			g_free (time_str);
+		}
+		if (obj->time_to_empty >= 0) {
+			time_str = dkp_object_time_to_text (obj->time_to_empty);
+			g_print ("    time to empty:       %s\n", time_str);
+			g_free (time_str);
+		}
+	}
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
+	    obj->type == DKP_DEVICE_TYPE_MOUSE ||
+	    obj->type == DKP_DEVICE_TYPE_KEYBOARD ||
+	    obj->type == DKP_DEVICE_TYPE_UPS)
+		g_print ("    percentage:          %g%%\n", obj->percentage);
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY)
+		g_print ("    capacity:            %g%%\n", obj->capacity);
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY)
+		g_print ("    technology:          %s\n", dkp_device_technology_to_text (obj->technology));
+	if (obj->type == DKP_DEVICE_TYPE_LINE_POWER)
+		g_print ("    online:             %s\n", dkp_object_bool_to_text (obj->online));
+
+	return ret;
+}
+
+/**
+ * dkp_object_diff:
+ **/
+gboolean
+dkp_object_diff (const DkpObject *old, const DkpObject *obj)
+{
+	gchar *time_str;
+	gchar *time_str_old;
+
+	g_print ("  native-path:          %s\n", obj->native_path);
+	if (!egg_strequal (obj->vendor, old->vendor))
+		g_print ("  vendor:               %s -> %s\n", old->vendor, obj->vendor);
+	if (!egg_strequal (obj->model, old->model))
+		g_print ("  model:                %s -> %s\n", old->model, obj->model);
+	if (!egg_strequal (obj->serial, old->serial))
+		g_print ("  serial:               %s -> %s\n", old->serial, obj->serial);
+	if (obj->has_history != old->has_history)
+		g_print ("  has history:          %s -> %s\n",
+			 dkp_object_bool_to_text (old->has_history),
+			 dkp_object_bool_to_text (obj->has_history));
+	if (obj->has_statistics != old->has_statistics)
+		g_print ("  has statistics:       %s -> %s\n",
+			 dkp_object_bool_to_text (old->has_statistics),
+			 dkp_object_bool_to_text (obj->has_statistics));
+
+	g_print ("  %s\n", dkp_device_type_to_text (obj->type));
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
+	    obj->type == DKP_DEVICE_TYPE_MOUSE ||
+	    obj->type == DKP_DEVICE_TYPE_KEYBOARD)
+		if (old->is_present != obj->is_present)
+			g_print ("    present:             %s -> %s\n",
+				 dkp_object_bool_to_text (old->is_present),
+				 dkp_object_bool_to_text (obj->is_present));
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY)
+		if (old->is_rechargeable != obj->is_rechargeable)
+			g_print ("    rechargeable:        %s -> %s\n",
+				 dkp_object_bool_to_text (old->is_rechargeable),
+				 dkp_object_bool_to_text (obj->is_rechargeable));
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
+	    obj->type == DKP_DEVICE_TYPE_MOUSE ||
+	    obj->type == DKP_DEVICE_TYPE_KEYBOARD ||
+	    obj->type == DKP_DEVICE_TYPE_UPS)
+		if (old->state != obj->state)
+			g_print ("    state:               %s -> %s\n",
+				 dkp_device_state_to_text (old->state),
+				 dkp_device_state_to_text (obj->state));
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY) {
+		if (old->energy != obj->energy)
+			g_print ("    energy:              %g -> %g Wh\n",
+				 old->energy,
+				 obj->energy);
+		if (old->energy_empty != obj->energy_empty)
+			g_print ("    energy-empty:        %g -> %g Wh\n",
+				 old->energy_empty,
+				 obj->energy_empty);
+		if (old->energy_full != obj->energy_full)
+			g_print ("    energy-full:         %g -> %g Wh\n",
+				 old->energy_full,
+				 obj->energy_full);
+		if (old->energy_full_design != obj->energy_full_design)
+			g_print ("    energy-full-design:  %g -> %g Wh\n",
+				 old->energy_full_design,
+				 obj->energy_full_design);
+	}
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
+	    obj->type == DKP_DEVICE_TYPE_MONITOR)
+		if (old->energy_rate != obj->energy_rate)
+			g_print ("    energy-rate:         %g -> %g W\n",
+				 old->energy_rate, obj->energy_rate);
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
+	    obj->type == DKP_DEVICE_TYPE_UPS) {
+		if (old->time_to_full != obj->time_to_full) {
+			time_str_old = dkp_object_time_to_text (old->time_to_full);
+			time_str = dkp_object_time_to_text (obj->time_to_full);
+			g_print ("    time to full:        %s -> %s\n", time_str_old, time_str);
+			g_free (time_str_old);
+			g_free (time_str);
+		}
+		if (old->time_to_empty != obj->time_to_empty) {
+			time_str_old = dkp_object_time_to_text (old->time_to_empty);
+			time_str = dkp_object_time_to_text (obj->time_to_empty);
+			g_print ("    time to empty:       %s -> %s\n", time_str_old, time_str);
+			g_free (time_str_old);
+			g_free (time_str);
+		}
+	}
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
+	    obj->type == DKP_DEVICE_TYPE_UPS ||
+	    obj->type == DKP_DEVICE_TYPE_MOUSE ||
+	    obj->type == DKP_DEVICE_TYPE_KEYBOARD)
+		if (old->percentage != obj->percentage)
+			g_print ("    percentage:          %g%% -> %g%%\n",
+				 old->percentage, obj->percentage);
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY)
+		if (old->capacity != obj->capacity)
+			g_print ("    capacity:            %g%% -> %g%%\n",
+				 old->capacity, obj->capacity);
+	if (obj->type == DKP_DEVICE_TYPE_BATTERY)
+		if (old->technology != obj->technology)
+			g_print ("    technology:          %s -> %s\n",
+				 dkp_device_technology_to_text (old->technology),
+				 dkp_device_technology_to_text (obj->technology));
+	if (obj->type == DKP_DEVICE_TYPE_LINE_POWER)
+		if (old->online != obj->online)
+			g_print ("    online:             %s -> %s\n",
+				 dkp_object_bool_to_text (old->online),
+				 dkp_object_bool_to_text (obj->online));
+	return TRUE;
+}
+
+/**
+ * dkp_object_new:
+ **/
+DkpObject *
+dkp_object_new (void)
+{
+	DkpObject *obj;
+	obj = g_new0 (DkpObject, 1);
+	dkp_object_clear_internal (obj);
+	return obj;
+}
+
+/**
+ * dkp_object_free_internal:
+ **/
+static gboolean
+dkp_object_free_internal (DkpObject *obj)
+{
+	g_free (obj->vendor);
+	g_free (obj->model);
+	g_free (obj->serial);
+	g_free (obj->native_path);
+	return TRUE;
+}
+
+/**
+ * dkp_object_free:
+ **/
+gboolean
+dkp_object_free (DkpObject *obj)
+{
+	if (obj == NULL)
+		return FALSE;
+	dkp_object_free_internal (obj);
+	g_free (obj);
+	return TRUE;
+}
+
+/**
+ * dkp_object_clear:
+ **/
+gboolean
+dkp_object_clear (DkpObject *obj)
+{
+	if (obj == NULL)
+		return FALSE;
+	dkp_object_free_internal (obj);
+	dkp_object_clear_internal (obj);
+	return TRUE;
+}
+
+/**
+ * dkp_object_get_id:
+ **/
+gchar *
+dkp_object_get_id (DkpObject *obj)
+{
+	GString *string;
+	gchar *id = NULL;
+
+	/* only valid for devices supplying the system */
+	if (!obj->power_supply)
+		return id;
+
+	/* only valid for batteries */
+	if (obj->type != DKP_DEVICE_TYPE_BATTERY)
+		return id;
+
+	/* we don't have an ID if we are not present */
+	if (!obj->is_present)
+		return id;
+
+	string = g_string_new ("");
+
+	/* in an ideal world, model-capacity-serial */
+	if (obj->model != NULL && strlen (obj->model) > 2) {
+		g_string_append (string, obj->model);
+		g_string_append_c (string, '-');
+	}
+	if (obj->energy_full_design > 0) {
+		g_string_append_printf (string, "%i", (guint) obj->energy_full_design);
+		g_string_append_c (string, '-');
+	}
+	if (obj->serial != NULL && strlen (obj->serial) > 2) {
+		g_string_append (string, obj->serial);
+		g_string_append_c (string, '-');
+	}
+
+	/* make sure we are sane */
+	if (string->len == 0) {
+		/* just use something generic */
+		g_string_append (string, "generic_id");
+	} else {
+		/* remove trailing '-' */
+		g_string_set_size (string, string->len - 1);
+	}
+
+	/* the id may have invalid chars that need to be replaced */
+	id = g_string_free (string, FALSE);
+	g_strdelimit (id, "\\\t\"' /", '_');
+
+	return id;
+}
+

Added: trunk/libdevkit-power/dkp-object.h
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/dkp-object.h	Mon Nov  3 17:20:59 2008
@@ -0,0 +1,71 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 Richard Hughes <richard hughsie com>
+ *
+ * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __DKP_OBJECT_H__
+#define __DKP_OBJECT_H__
+
+#include <glib.h>
+#include "dkp-enum.h"
+
+G_BEGIN_DECLS
+
+typedef struct {
+	guint64		 	 update_time;
+	gchar			*vendor;
+	gchar			*model;
+	gchar			*serial;
+	gchar			*native_path;
+	gboolean		 power_supply;
+	gboolean		 online;
+	gboolean		 is_present;
+	gboolean		 is_rechargeable;
+	gboolean		 has_history;
+	gboolean		 has_statistics;
+	DkpDeviceType		 type;
+	DkpDeviceState		 state;
+	DkpDeviceTechnology	 technology;
+	gdouble			 capacity;		/* percent */
+	gdouble			 energy;		/* Watt Hours */
+	gdouble			 energy_empty;		/* Watt Hours */
+	gdouble			 energy_full;		/* Watt Hours */
+	gdouble			 energy_full_design;	/* Watt Hours */
+	gdouble			 energy_rate;		/* Watts */
+	gint64			 time_to_empty;		/* seconds */
+	gint64			 time_to_full;		/* seconds */
+	gdouble			 percentage;		/* percent */
+} DkpObject;
+
+DkpObject	*dkp_object_new			(void);
+gboolean	 dkp_object_clear		(DkpObject		*obj);
+gboolean	 dkp_object_free		(DkpObject		*obj);
+gchar		*dkp_object_get_id		(DkpObject		*obj);
+DkpObject	*dkp_object_copy		(const DkpObject	*cobj);
+gboolean	 dkp_object_print		(const DkpObject	*obj);
+gboolean	 dkp_object_diff		(const DkpObject	*old,
+						 const DkpObject	*obj);
+gboolean	 dkp_object_equal		(const DkpObject	*obj1,
+						 const DkpObject	*obj2);
+gboolean	 dkp_object_set_from_map	(DkpObject		*obj,
+						 GHashTable		*hash_table);
+
+G_END_DECLS
+
+#endif /* __DKP_OBJECT_H__ */
+

Added: trunk/libdevkit-power/dkp-stats-obj.c
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/dkp-stats-obj.c	Mon Nov  3 17:20:59 2008
@@ -0,0 +1,120 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 Richard Hughes <richard hughsie com>
+ *
+ * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include <glib.h>
+#include <glib-object.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "egg-debug.h"
+#include "dkp-stats-obj.h"
+
+/**
+ * dkp_stats_obj_copy:
+ **/
+DkpStatsObj *
+dkp_stats_obj_copy (const DkpStatsObj *cobj)
+{
+	DkpStatsObj *obj;
+	obj = g_new0 (DkpStatsObj, 1);
+	obj->value = cobj->value;
+	obj->accuracy = cobj->accuracy;
+	return obj;
+}
+
+/**
+ * dkp_stats_obj_new:
+ **/
+DkpStatsObj *
+dkp_stats_obj_new (void)
+{
+	DkpStatsObj *obj;
+	obj = g_new0 (DkpStatsObj, 1);
+	obj->value = 0.0f;
+	obj->accuracy = 0;
+	return obj;
+}
+
+/**
+ * dkp_stats_obj_free:
+ **/
+gboolean
+dkp_stats_obj_free (DkpStatsObj *obj)
+{
+	if (obj == NULL)
+		return FALSE;
+	g_free (obj);
+	return TRUE;
+}
+
+/**
+ * dkp_stats_obj_create:
+ **/
+DkpStatsObj *
+dkp_stats_obj_create (gdouble value, gdouble accuracy)
+{
+	DkpStatsObj *obj;
+	obj = dkp_stats_obj_new ();
+	obj->value = value;
+	obj->accuracy = accuracy;
+	return obj;
+}
+
+/**
+ * dkp_stats_obj_from_string:
+ **/
+DkpStatsObj *
+dkp_stats_obj_from_string (const gchar *text)
+{
+	DkpStatsObj *obj = NULL;
+	gchar **parts = NULL;
+	guint length;
+
+	if (text == NULL)
+		goto out;
+
+	/* split by tab */
+	parts = g_strsplit (text, "\t", 0);
+	length = g_strv_length (parts);
+	if (length != 2) {
+		egg_warning ("invalid string: '%s'", text);
+		goto out;
+	}
+
+	/* parse and create */
+	obj = dkp_stats_obj_new ();
+	obj->value = atoi (parts[0]);
+	obj->accuracy = atof (parts[1]);
+out:
+	g_strfreev (parts);
+	return obj;
+}
+
+/**
+ * dkp_stats_obj_to_string:
+ **/
+gchar *
+dkp_stats_obj_to_string (const DkpStatsObj *obj)
+{
+	if (obj == NULL)
+		return NULL;
+	return g_strdup_printf ("%.2f\t%.2f", obj->value, obj->accuracy);
+}
+

Added: trunk/libdevkit-power/dkp-stats-obj.h
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/dkp-stats-obj.h	Mon Nov  3 17:20:59 2008
@@ -0,0 +1,46 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 Richard Hughes <richard hughsie com>
+ *
+ * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __DKP_STATS_OBJ_H__
+#define __DKP_STATS_OBJ_H__
+
+#include <glib.h>
+#include "dkp-enum.h"
+
+G_BEGIN_DECLS
+
+typedef struct
+{
+	gdouble			 value;
+	gdouble			 accuracy;
+} DkpStatsObj;
+
+DkpStatsObj	*dkp_stats_obj_new		(void);
+gboolean	 dkp_stats_obj_free		(DkpStatsObj		*obj);
+DkpStatsObj	*dkp_stats_obj_copy		(const DkpStatsObj	*cobj);
+DkpStatsObj	*dkp_stats_obj_create		(gdouble		 value,
+						 gdouble		 accuracy);
+DkpStatsObj	*dkp_stats_obj_from_string	(const gchar		*text);
+gchar		*dkp_stats_obj_to_string	(const DkpStatsObj	*obj);
+
+G_END_DECLS
+
+#endif /* __DKP_STATS_OBJ_H__ */
+

Added: trunk/libdevkit-power/egg-debug.c
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/egg-debug.c	Mon Nov  3 17:20:59 2008
@@ -0,0 +1 @@
+link ../src/egg-debug.c
\ No newline at end of file

Added: trunk/libdevkit-power/egg-debug.h
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/egg-debug.h	Mon Nov  3 17:20:59 2008
@@ -0,0 +1 @@
+link ../src/egg-debug.h
\ No newline at end of file

Added: trunk/libdevkit-power/egg-obj-list.c
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/egg-obj-list.c	Mon Nov  3 17:20:59 2008
@@ -0,0 +1 @@
+link ../src/egg-obj-list.c
\ No newline at end of file

Added: trunk/libdevkit-power/egg-obj-list.h
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/egg-obj-list.h	Mon Nov  3 17:20:59 2008
@@ -0,0 +1 @@
+link ../src/egg-obj-list.h
\ No newline at end of file

Added: trunk/libdevkit-power/egg-string.c
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/egg-string.c	Mon Nov  3 17:20:59 2008
@@ -0,0 +1 @@
+link ../src/egg-string.c
\ No newline at end of file

Added: trunk/libdevkit-power/egg-string.h
==============================================================================
--- (empty file)
+++ trunk/libdevkit-power/egg-string.h	Mon Nov  3 17:20:59 2008
@@ -0,0 +1 @@
+link ../src/egg-string.h
\ No newline at end of file



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