summaryrefslogtreecommitdiff
path: root/libdevkit-power
diff options
context:
space:
mode:
Diffstat (limited to 'libdevkit-power')
-rw-r--r--libdevkit-power/Makefile.am9
-rw-r--r--libdevkit-power/dkp-client-device.c384
-rw-r--r--libdevkit-power/dkp-client-device.h72
-rw-r--r--libdevkit-power/dkp-client.c289
-rw-r--r--libdevkit-power/dkp-client.h66
5 files changed, 818 insertions, 2 deletions
diff --git a/libdevkit-power/Makefile.am b/libdevkit-power/Makefile.am
index 30bb5dd..ea8c61b 100644
--- a/libdevkit-power/Makefile.am
+++ b/libdevkit-power/Makefile.am
@@ -1,6 +1,6 @@
INCLUDES = \
$(GLIB_CFLAGS) \
- $(DBUS_CFLAGS) \
+ $(DBUS_GLIB_CFLAGS) \
-I$(top_srcdir)/libdevkit-power \
-DPACKAGE_DATA_DIR=\""$(datadir)"\" \
-DPACKAGE_LOCALE_DIR=\""$(prefix)/$(DATADIRNAME)/locale"\"
@@ -11,6 +11,10 @@ noinst_LTLIBRARIES = \
libdevkit_power_la_SOURCES = \
dkp-debug.c \
dkp-debug.h \
+ dkp-client.c \
+ dkp-client.h \
+ dkp-client-device.c \
+ dkp-client-device.h \
dkp-history-obj.h \
dkp-history-obj.c \
dkp-enum.c \
@@ -18,7 +22,8 @@ libdevkit_power_la_SOURCES = \
dkp-object.c \
dkp-object.h
-libdevkit_power_la_LIBADD = $(INTLLIBS) $(GLIB_LIBS)
+libdevkit_power_la_LIBADD = $(INTLLIBS) $(GLIB_LIBS) $(DBUS_GLIB_LIBS)
+
clean-local:
rm -f *~
diff --git a/libdevkit-power/dkp-client-device.c b/libdevkit-power/dkp-client-device.c
new file mode 100644
index 0000000..4402bc6
--- /dev/null
+++ b/libdevkit-power/dkp-client-device.c
@@ -0,0 +1,384 @@
+/* -*- 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 "dkp-debug.h"
+#include "dkp-client-device.h"
+#include "dkp-object.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) {
+ dkp_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) {
+ dkp_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) {
+ dkp_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) {
+ dkp_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) {
+ dkp_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 */
+ dkp_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)
+ dkp_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:
+ **/
+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 stats */
+ dkp_client_device_get_statistics (device, "charge", 120);
+ dkp_client_device_get_statistics (device, "rate", 120);
+ 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) {
+ dkp_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_statistics:
+ *
+ * Returns an array of %DkpHistoryObj's
+ **/
+GPtrArray *
+dkp_client_device_get_statistics (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;
+ GPtrArray *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, "GetStatistics", &error,
+ G_TYPE_STRING, type,
+ G_TYPE_UINT, timespec,
+ G_TYPE_INVALID,
+ g_type_gvalue_array, &gvalue_ptr_array,
+ G_TYPE_INVALID);
+ if (!ret) {
+ dkp_debug ("GetStatistics(%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 = g_ptr_array_sized_new (gvalue_ptr_array->len);
+ 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);
+ g_ptr_array_add (array, 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);
+}
+
diff --git a/libdevkit-power/dkp-client-device.h b/libdevkit-power/dkp-client-device.h
new file mode 100644
index 0000000..646d4f6
--- /dev/null
+++ b/libdevkit-power/dkp-client-device.h
@@ -0,0 +1,72 @@
+/* -*- 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>
+
+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);
+GPtrArray *dkp_client_device_get_statistics (const DkpClientDevice *device,
+ const gchar *type,
+ guint timespec);
+
+G_END_DECLS
+
+#endif /* __DKP_CLIENT_DEVICE_H */
+
diff --git a/libdevkit-power/dkp-client.c b/libdevkit-power/dkp-client.c
new file mode 100644
index 0000000..d82bdc0
--- /dev/null
+++ b/libdevkit-power/dkp-client.c
@@ -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 "dkp-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) {
+ dkp_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) {
+ dkp_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) {
+ dkp_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);
+}
+
diff --git a/libdevkit-power/dkp-client.h b/libdevkit-power/dkp-client.h
new file mode 100644
index 0000000..5c9fb61
--- /dev/null
+++ b/libdevkit-power/dkp-client.h
@@ -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 */
+