dkp-client-device.c 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* -*- 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>

29
#include "egg-debug.h"
30 31
#include "egg-obj-list.h"

32 33
#include "dkp-client-device.h"
#include "dkp-object.h"
34
#include "dkp-stats-obj.h"
35 36 37 38 39 40 41 42 43 44 45 46 47
#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;
48
	DBusGProxy		*proxy_device;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	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) {
78
		egg_debug ("Couldn't call GetAll() to get properties for %s: %s", device->priv->object_path, error->message);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
		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) {
97
		egg_warning ("Cannot get device properties for %s", device->priv->object_path);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
		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;
124
	DBusGProxy *proxy_device;
125 126 127 128 129 130 131 132 133 134 135 136
	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) {
137
		egg_warning ("Couldn't connect to system bus: %s", error->message);
138 139 140 141 142 143 144 145
		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) {
146
		egg_warning ("Couldn't connect to proxy");
147 148 149 150
		goto out;
	}

	/* connect to the correct path for all the other methods */
151
	proxy_device = dbus_g_proxy_new_for_name (device->priv->bus, "org.freedesktop.DeviceKit.Power",
152
						  object_path, "org.freedesktop.DeviceKit.Power.Device");
153
	if (proxy_device == NULL) {
154
		egg_warning ("Couldn't connect to proxy");
155 156 157 158
		goto out;
	}

	/* listen to Changed */
159 160
	dbus_g_proxy_add_signal (proxy_device, "Changed", G_TYPE_INVALID);
	dbus_g_proxy_connect_signal (proxy_device, "Changed",
161 162 163
				     G_CALLBACK (dkp_client_device_changed_cb), device, NULL);

	/* yay */
164
	egg_debug ("using object_path: %s", object_path);
165
	device->priv->proxy_device = proxy_device;
166 167 168 169 170 171
	device->priv->proxy_props = proxy_props;
	device->priv->object_path = g_strdup (object_path);

	/* coldplug */
	ret = dkp_client_device_refresh_internal (device);
	if (!ret)
172
		egg_warning ("cannot refresh");
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
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;
}

197
/**
198
 * dkp_client_device_print_history:
199 200
 **/
static gboolean
201
dkp_client_device_print_history (const DkpClientDevice *device, const gchar *type)
202 203
{
	guint i;
204
	EggObjList *array;
205 206 207 208
	DkpHistoryObj *obj;
	gboolean ret = FALSE;

	/* get a fair chunk of data */
209
	array = dkp_client_device_get_history (device, type, 120);
210 211 212 213
	if (array == NULL)
		goto out;

	/* pretty print */
214
	g_print ("  History (%s):\n", type);
215
	for (i=0; i<array->len; i++) {
216
		obj = (DkpHistoryObj *) egg_obj_list_index (array, i);
217 218
		g_print ("    %i\t%.3f\t%s\n", obj->time, obj->value, dkp_device_state_to_text (obj->state));
	}
219
	g_object_unref (array);
220 221 222 223 224
	ret = TRUE;
out:
	return ret;
}

225 226 227 228 229 230 231 232 233 234 235
/**
 * 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);

236 237 238
	/* if we can, get history */
	dkp_client_device_print_history (device, "charge");
	dkp_client_device_print_history (device, "rate");
239

240 241 242 243 244 245 246 247 248 249 250 251 252
	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);
253
	g_return_val_if_fail (device->priv->proxy_device != NULL, FALSE);
254 255

	/* just refresh the device */
256
	ret = dbus_g_proxy_call (device->priv->proxy_device, "Refresh", &error,
257 258
				 G_TYPE_INVALID, G_TYPE_INVALID);
	if (!ret) {
259
		egg_debug ("Refresh() on %s failed: %s", device->priv->object_path, error->message);
260 261 262 263 264 265 266 267
		g_error_free (error);
		goto out;
	}
out:
	return ret;
}

/**
268
 * dkp_client_device_get_history:
269 270 271
 *
 * Returns an array of %DkpHistoryObj's
 **/
272
EggObjList *
273
dkp_client_device_get_history (const DkpClientDevice *device, const gchar *type, guint timespec)
274 275 276 277 278 279 280 281
{
	GError *error = NULL;
	GType g_type_gvalue_array;
	GPtrArray *gvalue_ptr_array = NULL;
	GValueArray *gva;
	GValue *gv;
	guint i;
	DkpHistoryObj *obj;
282
	EggObjList *array = NULL;
283 284 285 286 287 288 289 290 291 292 293 294
	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 */
295
	ret = dbus_g_proxy_call (device->priv->proxy_device, "GetHistory", &error,
296 297 298 299 300 301
				 G_TYPE_STRING, type,
				 G_TYPE_UINT, timespec,
				 G_TYPE_INVALID,
				 g_type_gvalue_array, &gvalue_ptr_array,
				 G_TYPE_INVALID);
	if (!ret) {
302
		egg_debug ("GetHistory(%s,%i) on %s failed: %s", type, timespec,
303 304 305 306 307 308 309 310 311 312
			   device->priv->object_path, error->message);
		g_error_free (error);
		goto out;
	}

	/* no data */
	if (gvalue_ptr_array->len == 0)
		goto out;

	/* convert */
313 314 315 316
	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);

317 318 319 320 321 322 323 324 325 326 327 328 329
	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);
330
		obj->state = dkp_device_state_from_text (g_value_get_string (gv));
331
		g_value_unset (gv);
332 333
		egg_obj_list_add (array, obj);
		dkp_history_obj_free (obj);
334 335 336 337 338 339 340 341 342
		g_value_array_free (gva);
	}

out:
	if (gvalue_ptr_array != NULL)
		g_ptr_array_free (gvalue_ptr_array, TRUE);
	return array;
}

343 344 345 346 347
/**
 * dkp_client_device_get_statistics:
 *
 * Returns an array of %DkpStatsObj's
 **/
348
EggObjList *
349 350 351 352 353 354 355 356 357
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;
358
	EggObjList *array = NULL;
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
	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) {
376
		egg_debug ("GetStatistics(%s) on %s failed: %s", type,
377 378 379 380 381 382 383 384 385 386
			   device->priv->object_path, error->message);
		g_error_free (error);
		goto out;
	}

	/* no data */
	if (gvalue_ptr_array->len == 0)
		goto out;

	/* convert */
387 388 389
	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);
390 391
	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);
392

393 394 395 396 397 398 399 400 401 402 403 404
	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 */
405 406
		egg_obj_list_add (array, obj);
		dkp_stats_obj_free (obj);
407 408 409 410 411 412 413 414
		g_value_array_free (gva);
	}
out:
	if (gvalue_ptr_array != NULL)
		g_ptr_array_free (gvalue_ptr_array, TRUE);
	return array;
}

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
/**
 * 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;
451
	device->priv->proxy_device = NULL;
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
	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);
471 472
	if (device->priv->proxy_device != NULL)
		g_object_unref (device->priv->proxy_device);
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	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);
}