~xavi-garcia-mena/ubuntu/vivid/upower/percentages-power-off

« back to all changes in this revision

Viewing changes to devkit-power-gobject/dkp-device.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2010-02-16 10:16:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100216101624-2cmwqsr1ndftdd87
Tags: upstream-0.9.0+git20100216.72bb2
ImportĀ upstreamĀ versionĀ 0.9.0+git20100216.72bb2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
 
4
 *
 
5
 * Licensed under the GNU General Public License Version 2
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <stdlib.h>
 
25
#include <stdio.h>
 
26
#include <glib.h>
 
27
#include <dbus/dbus-glib.h>
 
28
#include <string.h>
 
29
 
 
30
#include "dkp-device.h"
 
31
#include "dkp-stats-obj.h"
 
32
#include "dkp-history-obj.h"
 
33
 
 
34
static void     dkp_device_class_init   (DkpDeviceClass *klass);
 
35
static void     dkp_device_init         (DkpDevice      *device);
 
36
static void     dkp_device_finalize     (GObject                *object);
 
37
 
 
38
#define DKP_DEVICE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DKP_TYPE_DEVICE, DkpDevicePrivate))
 
39
 
 
40
struct DkpDevicePrivate
 
41
{
 
42
        gchar                   *object_path;
 
43
        DBusGConnection         *bus;
 
44
        DBusGProxy              *proxy_device;
 
45
        DBusGProxy              *proxy_props;
 
46
 
 
47
        /* properties */
 
48
        guint64                  update_time;
 
49
        gchar                   *vendor;
 
50
        gchar                   *model;
 
51
        gchar                   *serial;
 
52
        gchar                   *native_path;
 
53
        gboolean                 power_supply;
 
54
        gboolean                 online;
 
55
        gboolean                 is_present;
 
56
        gboolean                 is_rechargeable;
 
57
        gboolean                 has_history;
 
58
        gboolean                 has_statistics;
 
59
        DkpDeviceType            type;
 
60
        DkpDeviceState           state;
 
61
        DkpDeviceTechnology      technology;
 
62
        gdouble                  capacity;              /* percent */
 
63
        gdouble                  energy;                /* Watt Hours */
 
64
        gdouble                  energy_empty;          /* Watt Hours */
 
65
        gdouble                  energy_full;           /* Watt Hours */
 
66
        gdouble                  energy_full_design;    /* Watt Hours */
 
67
        gdouble                  energy_rate;           /* Watts */
 
68
        gdouble                  voltage;               /* Volts */
 
69
        gint64                   time_to_empty;         /* seconds */
 
70
        gint64                   time_to_full;          /* seconds */
 
71
        gdouble                  percentage;            /* percent */
 
72
        gboolean                 recall_notice;
 
73
        gchar                   *recall_vendor;
 
74
        gchar                   *recall_url;
 
75
};
 
76
 
 
77
enum {
 
78
        PROP_0,
 
79
        PROP_UPDATE_TIME,
 
80
        PROP_VENDOR,
 
81
        PROP_MODEL,
 
82
        PROP_SERIAL,
 
83
        PROP_NATIVE_PATH,
 
84
        PROP_POWER_SUPPLY,
 
85
        PROP_ONLINE,
 
86
        PROP_IS_PRESENT,
 
87
        PROP_IS_RECHARGEABLE,
 
88
        PROP_HAS_HISTORY,
 
89
        PROP_HAS_STATISTICS,
 
90
        PROP_TYPE,
 
91
        PROP_STATE,
 
92
        PROP_TECHNOLOGY,
 
93
        PROP_CAPACITY,
 
94
        PROP_ENERGY,
 
95
        PROP_ENERGY_EMPTY,
 
96
        PROP_ENERGY_FULL,
 
97
        PROP_ENERGY_FULL_DESIGN,
 
98
        PROP_ENERGY_RATE,
 
99
        PROP_VOLTAGE,
 
100
        PROP_TIME_TO_EMPTY,
 
101
        PROP_TIME_TO_FULL,
 
102
        PROP_PERCENTAGE,
 
103
        PROP_RECALL_NOTICE,
 
104
        PROP_RECALL_VENDOR,
 
105
        PROP_RECALL_URL,
 
106
        PROP_LAST
 
107
};
 
108
 
 
109
enum {
 
110
        DKP_DEVICE_CHANGED,
 
111
        DKP_DEVICE_LAST_SIGNAL
 
112
};
 
113
 
 
114
static guint signals [DKP_DEVICE_LAST_SIGNAL] = { 0 };
 
115
 
 
116
G_DEFINE_TYPE (DkpDevice, dkp_device, G_TYPE_OBJECT)
 
117
 
 
118
/**
 
119
 * dkp_device_get_device_properties:
 
120
 **/
 
121
static GHashTable *
 
122
dkp_device_get_device_properties (DkpDevice *device, GError **error)
 
123
{
 
124
        gboolean ret;
 
125
        GError *error_local = NULL;
 
126
        GHashTable *hash_table = NULL;
 
127
 
 
128
        ret = dbus_g_proxy_call (device->priv->proxy_props, "GetAll", &error_local,
 
129
                                 G_TYPE_STRING, "org.freedesktop.UPower.Device",
 
130
                                 G_TYPE_INVALID,
 
131
                                 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
 
132
                                 &hash_table,
 
133
                                 G_TYPE_INVALID);
 
134
        if (!ret) {
 
135
                g_set_error (error, 1, 0, "Couldn't call GetAll() to get properties for %s: %s", device->priv->object_path, error_local->message);
 
136
                g_error_free (error_local);
 
137
                goto out;
 
138
        }
 
139
out:
 
140
        return hash_table;
 
141
}
 
142
 
 
143
/**
 
144
 * dkp_device_collect_props_cb:
 
145
 **/
 
146
static void
 
147
dkp_device_collect_props_cb (const char *key, const GValue *value, DkpDevice *device)
 
148
{
 
149
        if (g_strcmp0 (key, "NativePath") == 0) {
 
150
                g_free (device->priv->native_path);
 
151
                device->priv->native_path = g_strdup (g_value_get_string (value));
 
152
        } else if (g_strcmp0 (key, "Vendor") == 0) {
 
153
                g_free (device->priv->vendor);
 
154
                device->priv->vendor = g_strdup (g_value_get_string (value));
 
155
        } else if (g_strcmp0 (key, "Model") == 0) {
 
156
                g_free (device->priv->model);
 
157
                device->priv->model = g_strdup (g_value_get_string (value));
 
158
        } else if (g_strcmp0 (key, "Serial") == 0) {
 
159
                g_free (device->priv->serial);
 
160
                device->priv->serial = g_strdup (g_value_get_string (value));
 
161
        } else if (g_strcmp0 (key, "UpdateTime") == 0) {
 
162
                device->priv->update_time = g_value_get_uint64 (value);
 
163
        } else if (g_strcmp0 (key, "Type") == 0) {
 
164
                device->priv->type = g_value_get_uint (value);
 
165
        } else if (g_strcmp0 (key, "Online") == 0) {
 
166
                device->priv->online = g_value_get_boolean (value);
 
167
        } else if (g_strcmp0 (key, "HasHistory") == 0) {
 
168
                device->priv->has_history = g_value_get_boolean (value);
 
169
        } else if (g_strcmp0 (key, "HasStatistics") == 0) {
 
170
                device->priv->has_statistics = g_value_get_boolean (value);
 
171
        } else if (g_strcmp0 (key, "Energy") == 0) {
 
172
                device->priv->energy = g_value_get_double (value);
 
173
        } else if (g_strcmp0 (key, "EnergyEmpty") == 0) {
 
174
                device->priv->energy_empty = g_value_get_double (value);
 
175
        } else if (g_strcmp0 (key, "EnergyFull") == 0) {
 
176
                device->priv->energy_full = g_value_get_double (value);
 
177
        } else if (g_strcmp0 (key, "EnergyFullDesign") == 0) {
 
178
                device->priv->energy_full_design = g_value_get_double (value);
 
179
        } else if (g_strcmp0 (key, "EnergyRate") == 0) {
 
180
                device->priv->energy_rate = g_value_get_double (value);
 
181
        } else if (g_strcmp0 (key, "Voltage") == 0) {
 
182
                device->priv->voltage = g_value_get_double (value);
 
183
        } else if (g_strcmp0 (key, "TimeToFull") == 0) {
 
184
                device->priv->time_to_full = g_value_get_int64 (value);
 
185
        } else if (g_strcmp0 (key, "TimeToEmpty") == 0) {
 
186
                device->priv->time_to_empty = g_value_get_int64 (value);
 
187
        } else if (g_strcmp0 (key, "Percentage") == 0) {
 
188
                device->priv->percentage = g_value_get_double (value);
 
189
        } else if (g_strcmp0 (key, "Technology") == 0) {
 
190
                device->priv->technology = g_value_get_uint (value);
 
191
        } else if (g_strcmp0 (key, "IsPresent") == 0) {
 
192
                device->priv->is_present = g_value_get_boolean (value);
 
193
        } else if (g_strcmp0 (key, "IsRechargeable") == 0) {
 
194
                device->priv->is_rechargeable = g_value_get_boolean (value);
 
195
        } else if (g_strcmp0 (key, "PowerSupply") == 0) {
 
196
                device->priv->power_supply = g_value_get_boolean (value);
 
197
        } else if (g_strcmp0 (key, "Capacity") == 0) {
 
198
                device->priv->capacity = g_value_get_double (value);
 
199
        } else if (g_strcmp0 (key, "State") == 0) {
 
200
                device->priv->state = g_value_get_uint (value);
 
201
        } else if (g_strcmp0 (key, "RecallNotice") == 0) {
 
202
                device->priv->recall_notice = g_value_get_boolean (value);
 
203
        } else if (g_strcmp0 (key, "RecallVendor") == 0) {
 
204
                g_free (device->priv->recall_vendor);
 
205
                device->priv->recall_vendor = g_strdup (g_value_get_string (value));
 
206
        } else if (g_strcmp0 (key, "RecallUrl") == 0) {
 
207
                g_free (device->priv->recall_url);
 
208
                device->priv->recall_url = g_strdup (g_value_get_string (value));
 
209
        } else {
 
210
                g_warning ("unhandled property '%s'", key);
 
211
        }
 
212
}
 
213
 
 
214
/**
 
215
 * dkp_device_refresh_internal:
 
216
 **/
 
217
static gboolean
 
218
dkp_device_refresh_internal (DkpDevice *device, GError **error)
 
219
{
 
220
        GHashTable *hash;
 
221
        GError *error_local = NULL;
 
222
 
 
223
        /* get all the properties */
 
224
        hash = dkp_device_get_device_properties (device, &error_local);
 
225
        if (hash == NULL) {
 
226
                g_set_error (error, 1, 0, "Cannot get device properties for %s: %s", device->priv->object_path, error_local->message);
 
227
                g_error_free (error_local);
 
228
                return FALSE;
 
229
        }
 
230
        g_hash_table_foreach (hash, (GHFunc) dkp_device_collect_props_cb, device);
 
231
        g_hash_table_unref (hash);
 
232
        return TRUE;
 
233
}
 
234
 
 
235
/**
 
236
 * dkp_device_changed_cb:
 
237
 **/
 
238
static void
 
239
dkp_device_changed_cb (DBusGProxy *proxy, DkpDevice *device)
 
240
{
 
241
        g_return_if_fail (DKP_IS_DEVICE (device));
 
242
        dkp_device_refresh_internal (device, NULL);
 
243
        g_signal_emit (device, signals [DKP_DEVICE_CHANGED], 0, NULL); //TODO xxx
 
244
}
 
245
 
 
246
/**
 
247
 * dkp_device_set_object_path:
 
248
 **/
 
249
gboolean
 
250
dkp_device_set_object_path (DkpDevice *device, const gchar *object_path, GError **error)
 
251
{
 
252
        GError *error_local = NULL;
 
253
        gboolean ret = FALSE;
 
254
        DBusGProxy *proxy_device;
 
255
        DBusGProxy *proxy_props;
 
256
 
 
257
        g_return_val_if_fail (DKP_IS_DEVICE (device), FALSE);
 
258
 
 
259
        if (device->priv->object_path != NULL)
 
260
                return FALSE;
 
261
        if (object_path == NULL)
 
262
                return FALSE;
 
263
 
 
264
        /* connect to the bus */
 
265
        device->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error_local);
 
266
        if (device->priv->bus == NULL) {
 
267
                g_set_error (error, 1, 0, "Couldn't connect to system bus: %s", error_local->message);
 
268
                g_error_free (error_local);
 
269
                goto out;
 
270
        }
 
271
 
 
272
        /* connect to the correct path for properties */
 
273
        proxy_props = dbus_g_proxy_new_for_name (device->priv->bus, "org.freedesktop.UPower",
 
274
                                                 object_path, "org.freedesktop.DBus.Properties");
 
275
        if (proxy_props == NULL) {
 
276
                g_set_error_literal (error, 1, 0, "Couldn't connect to proxy");
 
277
                goto out;
 
278
        }
 
279
 
 
280
        /* connect to the correct path for all the other methods */
 
281
        proxy_device = dbus_g_proxy_new_for_name (device->priv->bus, "org.freedesktop.UPower",
 
282
                                                  object_path, "org.freedesktop.UPower.Device");
 
283
        if (proxy_device == NULL) {
 
284
                g_set_error_literal (error, 1, 0, "Couldn't connect to proxy");
 
285
                goto out;
 
286
        }
 
287
 
 
288
        /* listen to Changed */
 
289
        dbus_g_proxy_add_signal (proxy_device, "Changed", G_TYPE_INVALID);
 
290
        dbus_g_proxy_connect_signal (proxy_device, "Changed",
 
291
                                     G_CALLBACK (dkp_device_changed_cb), device, NULL);
 
292
 
 
293
        /* yay */
 
294
        device->priv->proxy_device = proxy_device;
 
295
        device->priv->proxy_props = proxy_props;
 
296
        device->priv->object_path = g_strdup (object_path);
 
297
 
 
298
        /* coldplug */
 
299
        ret = dkp_device_refresh_internal (device, &error_local);
 
300
        if (!ret) {
 
301
                g_set_error (error, 1, 0, "cannot refresh: %s", error_local->message);
 
302
                g_error_free (error_local);
 
303
        }
 
304
out:
 
305
        return ret;
 
306
}
 
307
 
 
308
/**
 
309
 * dkp_device_get_object_path:
 
310
 **/
 
311
const gchar *
 
312
dkp_device_get_object_path (const DkpDevice *device)
 
313
{
 
314
        g_return_val_if_fail (DKP_IS_DEVICE (device), NULL);
 
315
        return device->priv->object_path;
 
316
}
 
317
 
 
318
/**
 
319
 * dkp_device_print_history:
 
320
 **/
 
321
static gboolean
 
322
dkp_device_print_history (const DkpDevice *device, const gchar *type)
 
323
{
 
324
        guint i;
 
325
        GPtrArray *array;
 
326
        const DkpHistoryObj *obj;
 
327
        gboolean ret = FALSE;
 
328
 
 
329
        /* get a fair chunk of data */
 
330
        array = dkp_device_get_history (device, type, 120, 10, NULL);
 
331
        if (array == NULL)
 
332
                goto out;
 
333
 
 
334
        /* pretty print */
 
335
        g_print ("  History (%s):\n", type);
 
336
        for (i=0; i<array->len; i++) {
 
337
                obj = (const DkpHistoryObj *) g_ptr_array_index (array, i);
 
338
                g_print ("    %i\t%.3f\t%s\n", obj->time, obj->value, dkp_device_state_to_text (obj->state));
 
339
        }
 
340
        g_ptr_array_unref (array);
 
341
        ret = TRUE;
 
342
out:
 
343
        return ret;
 
344
}
 
345
 
 
346
/**
 
347
 * dkp_device_print_bool_to_text:
 
348
 **/
 
349
static const gchar *
 
350
dkp_device_print_bool_to_text (gboolean ret)
 
351
{
 
352
        return ret ? "yes" : "no";
 
353
}
 
354
 
 
355
/**
 
356
 * dkp_device_print_time_to_text:
 
357
 **/
 
358
static gchar *
 
359
dkp_device_print_time_to_text (gint seconds)
 
360
{
 
361
        gfloat value = seconds;
 
362
 
 
363
        if (value < 0)
 
364
                return g_strdup ("unknown");
 
365
        if (value < 60)
 
366
                return g_strdup_printf ("%.0f seconds", value);
 
367
        value /= 60.0;
 
368
        if (value < 60)
 
369
                return g_strdup_printf ("%.1f minutes", value);
 
370
        value /= 60.0;
 
371
        if (value < 60)
 
372
                return g_strdup_printf ("%.1f hours", value);
 
373
        value /= 24.0;
 
374
        return g_strdup_printf ("%.1f days", value);
 
375
}
 
376
 
 
377
/**
 
378
 * dkp_device_print:
 
379
 **/
 
380
gboolean
 
381
dkp_device_print (const DkpDevice *device)
 
382
{
 
383
        struct tm *time_tm;
 
384
        time_t t;
 
385
        gchar time_buf[256];
 
386
        gchar *time_str;
 
387
 
 
388
        g_return_val_if_fail (DKP_IS_DEVICE (device), FALSE);
 
389
 
 
390
        /* get a human readable time */
 
391
        t = (time_t) device->priv->update_time;
 
392
        time_tm = localtime (&t);
 
393
        strftime (time_buf, sizeof time_buf, "%c", time_tm);
 
394
 
 
395
        g_print ("  native-path:          %s\n", device->priv->native_path);
 
396
        if (device->priv->vendor != NULL && device->priv->vendor[0] != '\0')
 
397
                g_print ("  vendor:               %s\n", device->priv->vendor);
 
398
        if (device->priv->model != NULL && device->priv->model[0] != '\0')
 
399
                g_print ("  model:                %s\n", device->priv->model);
 
400
        if (device->priv->serial != NULL && device->priv->serial[0] != '\0')
 
401
                g_print ("  serial:               %s\n", device->priv->serial);
 
402
        g_print ("  power supply:         %s\n", dkp_device_print_bool_to_text (device->priv->power_supply));
 
403
        g_print ("  updated:              %s (%d seconds ago)\n", time_buf, (int) (time (NULL) - device->priv->update_time));
 
404
        g_print ("  has history:          %s\n", dkp_device_print_bool_to_text (device->priv->has_history));
 
405
        g_print ("  has statistics:       %s\n", dkp_device_print_bool_to_text (device->priv->has_statistics));
 
406
        g_print ("  %s\n", dkp_device_type_to_text (device->priv->type));
 
407
 
 
408
        if (device->priv->type == DKP_DEVICE_TYPE_BATTERY ||
 
409
            device->priv->type == DKP_DEVICE_TYPE_MOUSE ||
 
410
            device->priv->type == DKP_DEVICE_TYPE_KEYBOARD ||
 
411
            device->priv->type == DKP_DEVICE_TYPE_UPS)
 
412
                g_print ("    present:             %s\n", dkp_device_print_bool_to_text (device->priv->is_present));
 
413
        if (device->priv->type == DKP_DEVICE_TYPE_BATTERY ||
 
414
            device->priv->type == DKP_DEVICE_TYPE_MOUSE ||
 
415
            device->priv->type == DKP_DEVICE_TYPE_KEYBOARD)
 
416
                g_print ("    rechargeable:        %s\n", dkp_device_print_bool_to_text (device->priv->is_rechargeable));
 
417
        if (device->priv->type == DKP_DEVICE_TYPE_BATTERY ||
 
418
            device->priv->type == DKP_DEVICE_TYPE_MOUSE ||
 
419
            device->priv->type == DKP_DEVICE_TYPE_KEYBOARD ||
 
420
            device->priv->type == DKP_DEVICE_TYPE_UPS)
 
421
                g_print ("    state:               %s\n", dkp_device_state_to_text (device->priv->state));
 
422
        if (device->priv->type == DKP_DEVICE_TYPE_BATTERY) {
 
423
                g_print ("    energy:              %g Wh\n", device->priv->energy);
 
424
                g_print ("    energy-empty:        %g Wh\n", device->priv->energy_empty);
 
425
                g_print ("    energy-full:         %g Wh\n", device->priv->energy_full);
 
426
                g_print ("    energy-full-design:  %g Wh\n", device->priv->energy_full_design);
 
427
        }
 
428
        if (device->priv->type == DKP_DEVICE_TYPE_BATTERY ||
 
429
            device->priv->type == DKP_DEVICE_TYPE_MONITOR)
 
430
                g_print ("    energy-rate:         %g W\n", device->priv->energy_rate);
 
431
        if (device->priv->type == DKP_DEVICE_TYPE_UPS ||
 
432
            device->priv->type == DKP_DEVICE_TYPE_BATTERY ||
 
433
            device->priv->type == DKP_DEVICE_TYPE_MONITOR) {
 
434
                if (device->priv->voltage > 0)
 
435
                        g_print ("    voltage:             %g V\n", device->priv->voltage);
 
436
        }
 
437
        if (device->priv->type == DKP_DEVICE_TYPE_BATTERY ||
 
438
            device->priv->type == DKP_DEVICE_TYPE_UPS) {
 
439
                if (device->priv->time_to_full > 0) {
 
440
                        time_str = dkp_device_print_time_to_text (device->priv->time_to_full);
 
441
                        g_print ("    time to full:        %s\n", time_str);
 
442
                        g_free (time_str);
 
443
                }
 
444
                if (device->priv->time_to_empty > 0) {
 
445
                        time_str = dkp_device_print_time_to_text (device->priv->time_to_empty);
 
446
                        g_print ("    time to empty:       %s\n", time_str);
 
447
                        g_free (time_str);
 
448
                }
 
449
        }
 
450
        if (device->priv->type == DKP_DEVICE_TYPE_BATTERY ||
 
451
            device->priv->type == DKP_DEVICE_TYPE_MOUSE ||
 
452
            device->priv->type == DKP_DEVICE_TYPE_KEYBOARD ||
 
453
            device->priv->type == DKP_DEVICE_TYPE_UPS)
 
454
                g_print ("    percentage:          %g%%\n", device->priv->percentage);
 
455
        if (device->priv->type == DKP_DEVICE_TYPE_BATTERY) {
 
456
                if (device->priv->capacity > 0)
 
457
                        g_print ("    capacity:            %g%%\n", device->priv->capacity);
 
458
        }
 
459
        if (device->priv->type == DKP_DEVICE_TYPE_BATTERY) {
 
460
                if (device->priv->technology != DKP_DEVICE_TECHNOLOGY_UNKNOWN)
 
461
                        g_print ("    technology:          %s\n", dkp_device_technology_to_text (device->priv->technology));
 
462
        }
 
463
        if (device->priv->type == DKP_DEVICE_TYPE_LINE_POWER)
 
464
                g_print ("    online:             %s\n", dkp_device_print_bool_to_text (device->priv->online));
 
465
        if (device->priv->type == DKP_DEVICE_TYPE_BATTERY) {
 
466
                if (device->priv->recall_notice) {
 
467
                        g_print ("    recall vendor:       %s\n", device->priv->recall_vendor);
 
468
                        g_print ("    recall url:          %s\n", device->priv->recall_url);
 
469
                }
 
470
        }
 
471
 
 
472
        /* if we can, get history */
 
473
        if (device->priv->has_history) {
 
474
                dkp_device_print_history (device, "charge");
 
475
                dkp_device_print_history (device, "rate");
 
476
        }
 
477
 
 
478
        return TRUE;
 
479
}
 
480
 
 
481
/**
 
482
 * dkp_device_refresh:
 
483
 **/
 
484
gboolean
 
485
dkp_device_refresh (DkpDevice *device, GError **error)
 
486
{
 
487
        GError *error_local = NULL;
 
488
        gboolean ret;
 
489
 
 
490
        g_return_val_if_fail (DKP_IS_DEVICE (device), FALSE);
 
491
        g_return_val_if_fail (device->priv->proxy_device != NULL, FALSE);
 
492
 
 
493
        /* just refresh the device */
 
494
        ret = dbus_g_proxy_call (device->priv->proxy_device, "Refresh", &error_local,
 
495
                                 G_TYPE_INVALID, G_TYPE_INVALID);
 
496
        if (!ret) {
 
497
                g_set_error (error, 1, 0, "Refresh() on %s failed: %s", device->priv->object_path, error_local->message);
 
498
                g_error_free (error_local);
 
499
                goto out;
 
500
        }
 
501
out:
 
502
        return ret;
 
503
}
 
504
 
 
505
/**
 
506
 * dkp_device_get_history:
 
507
 *
 
508
 * Returns an array of %DkpHistoryObj's, free with g_ptr_array_unref()
 
509
 **/
 
510
GPtrArray *
 
511
dkp_device_get_history (const DkpDevice *device, const gchar *type, guint timespec, guint resolution, GError **error)
 
512
{
 
513
        GError *error_local = NULL;
 
514
        GType g_type_gvalue_array;
 
515
        GPtrArray *gvalue_ptr_array = NULL;
 
516
        GValueArray *gva;
 
517
        GValue *gv;
 
518
        guint i;
 
519
        DkpHistoryObj *obj;
 
520
        GPtrArray *array = NULL;
 
521
        gboolean ret;
 
522
 
 
523
        g_return_val_if_fail (DKP_IS_DEVICE (device), NULL);
 
524
        g_return_val_if_fail (device->priv->proxy_device != NULL, NULL);
 
525
 
 
526
        g_type_gvalue_array = dbus_g_type_get_collection ("GPtrArray",
 
527
                                        dbus_g_type_get_struct("GValueArray",
 
528
                                                G_TYPE_UINT,
 
529
                                                G_TYPE_DOUBLE,
 
530
                                                G_TYPE_UINT,
 
531
                                                G_TYPE_INVALID));
 
532
 
 
533
        /* get compound data */
 
534
        ret = dbus_g_proxy_call (device->priv->proxy_device, "GetHistory", &error_local,
 
535
                                 G_TYPE_STRING, type,
 
536
                                 G_TYPE_UINT, timespec,
 
537
                                 G_TYPE_UINT, resolution,
 
538
                                 G_TYPE_INVALID,
 
539
                                 g_type_gvalue_array, &gvalue_ptr_array,
 
540
                                 G_TYPE_INVALID);
 
541
        if (!ret) {
 
542
                g_set_error (error, 1, 0, "GetHistory(%s,%i) on %s failed: %s", type, timespec,
 
543
                           device->priv->object_path, error_local->message);
 
544
                g_error_free (error_local);
 
545
                goto out;
 
546
        }
 
547
 
 
548
        /* no data */
 
549
        if (gvalue_ptr_array->len == 0) {
 
550
                g_set_error_literal (error, 1, 0, "no data");
 
551
                goto out;
 
552
        }
 
553
 
 
554
        /* convert */
 
555
        array = g_ptr_array_new_with_free_func ((GDestroyNotify) dkp_history_obj_free);
 
556
 
 
557
        for (i=0; i<gvalue_ptr_array->len; i++) {
 
558
                gva = (GValueArray *) g_ptr_array_index (gvalue_ptr_array, i);
 
559
                obj = dkp_history_obj_new ();
 
560
                /* 0 */
 
561
                gv = g_value_array_get_nth (gva, 0);
 
562
                obj->time = g_value_get_uint (gv);
 
563
                g_value_unset (gv);
 
564
                /* 1 */
 
565
                gv = g_value_array_get_nth (gva, 1);
 
566
                obj->value = g_value_get_double (gv);
 
567
                g_value_unset (gv);
 
568
                /* 2 */
 
569
                gv = g_value_array_get_nth (gva, 2);
 
570
                obj->state = g_value_get_uint (gv);
 
571
                g_value_unset (gv);
 
572
                g_ptr_array_add (array, obj);
 
573
                g_value_array_free (gva);
 
574
        }
 
575
 
 
576
out:
 
577
        if (gvalue_ptr_array != NULL)
 
578
                g_ptr_array_free (gvalue_ptr_array, TRUE);
 
579
        return array;
 
580
}
 
581
 
 
582
/**
 
583
 * dkp_device_get_statistics:
 
584
 *
 
585
 * Returns an array of %DkpStatsObj's
 
586
 **/
 
587
GPtrArray *
 
588
dkp_device_get_statistics (const DkpDevice *device, const gchar *type, GError **error)
 
589
{
 
590
        GError *error_local = NULL;
 
591
        GType g_type_gvalue_array;
 
592
        GPtrArray *gvalue_ptr_array = NULL;
 
593
        GValueArray *gva;
 
594
        GValue *gv;
 
595
        guint i;
 
596
        DkpStatsObj *obj;
 
597
        GPtrArray *array = NULL;
 
598
        gboolean ret;
 
599
 
 
600
        g_return_val_if_fail (DKP_IS_DEVICE (device), NULL);
 
601
        g_return_val_if_fail (device->priv->proxy_device != NULL, NULL);
 
602
 
 
603
        g_type_gvalue_array = dbus_g_type_get_collection ("GPtrArray",
 
604
                                        dbus_g_type_get_struct("GValueArray",
 
605
                                                G_TYPE_DOUBLE,
 
606
                                                G_TYPE_DOUBLE,
 
607
                                                G_TYPE_INVALID));
 
608
 
 
609
        /* get compound data */
 
610
        ret = dbus_g_proxy_call (device->priv->proxy_device, "GetStatistics", &error_local,
 
611
                                 G_TYPE_STRING, type,
 
612
                                 G_TYPE_INVALID,
 
613
                                 g_type_gvalue_array, &gvalue_ptr_array,
 
614
                                 G_TYPE_INVALID);
 
615
        if (!ret) {
 
616
                g_set_error (error, 1, 0, "GetStatistics(%s) on %s failed: %s", type,
 
617
                                      device->priv->object_path, error_local->message);
 
618
                g_error_free (error_local);
 
619
                goto out;
 
620
        }
 
621
 
 
622
        /* no data */
 
623
        if (gvalue_ptr_array->len == 0) {
 
624
                g_set_error_literal (error, 1, 0, "no data");
 
625
                goto out;
 
626
        }
 
627
 
 
628
        /* convert */
 
629
        array = g_ptr_array_new ();
 
630
 
 
631
        for (i=0; i<gvalue_ptr_array->len; i++) {
 
632
                gva = (GValueArray *) g_ptr_array_index (gvalue_ptr_array, i);
 
633
                obj = dkp_stats_obj_new ();
 
634
                /* 0 */
 
635
                gv = g_value_array_get_nth (gva, 0);
 
636
                obj->value = g_value_get_double (gv);
 
637
                g_value_unset (gv);
 
638
                /* 1 */
 
639
                gv = g_value_array_get_nth (gva, 1);
 
640
                obj->accuracy = g_value_get_double (gv);
 
641
                g_value_unset (gv);
 
642
                /* 2 */
 
643
                g_ptr_array_add (array, obj);
 
644
                g_value_array_free (gva);
 
645
        }
 
646
out:
 
647
        if (gvalue_ptr_array != NULL)
 
648
                g_ptr_array_free (gvalue_ptr_array, TRUE);
 
649
        return array;
 
650
}
 
651
 
 
652
/**
 
653
 * dkp_device_set_property:
 
654
 **/
 
655
static void
 
656
dkp_device_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
 
657
{
 
658
        DkpDevice *device = DKP_DEVICE (object);
 
659
 
 
660
        switch (prop_id) {
 
661
        case PROP_NATIVE_PATH:
 
662
                g_free (device->priv->native_path);
 
663
                device->priv->native_path = g_strdup (g_value_get_string (value));
 
664
                break;
 
665
        case PROP_VENDOR:
 
666
                g_free (device->priv->vendor);
 
667
                device->priv->vendor = g_strdup (g_value_get_string (value));
 
668
                break;
 
669
        case PROP_MODEL:
 
670
                g_free (device->priv->model);
 
671
                device->priv->model = g_strdup (g_value_get_string (value));
 
672
                break;
 
673
        case PROP_SERIAL:
 
674
                g_free (device->priv->serial);
 
675
                device->priv->serial = g_strdup (g_value_get_string (value));
 
676
                break;
 
677
        case PROP_UPDATE_TIME:
 
678
                device->priv->update_time = g_value_get_uint64 (value);
 
679
                break;
 
680
        case PROP_TYPE:
 
681
                device->priv->type = g_value_get_uint (value);
 
682
                break;
 
683
        case PROP_POWER_SUPPLY:
 
684
                device->priv->power_supply = g_value_get_boolean (value);
 
685
                break;
 
686
        case PROP_ONLINE:
 
687
                device->priv->online = g_value_get_boolean (value);
 
688
                break;
 
689
        case PROP_IS_PRESENT:
 
690
                device->priv->is_present = g_value_get_boolean (value);
 
691
                break;
 
692
        case PROP_IS_RECHARGEABLE:
 
693
                device->priv->is_rechargeable = g_value_get_boolean (value);
 
694
                break;
 
695
        case PROP_HAS_HISTORY:
 
696
                device->priv->has_history = g_value_get_boolean (value);
 
697
                break;
 
698
        case PROP_HAS_STATISTICS:
 
699
                device->priv->has_statistics = g_value_get_boolean (value);
 
700
                break;
 
701
        case PROP_STATE:
 
702
                device->priv->state = g_value_get_uint (value);
 
703
                break;
 
704
        case PROP_CAPACITY:
 
705
                device->priv->capacity = g_value_get_double (value);
 
706
                break;
 
707
        case PROP_ENERGY:
 
708
                device->priv->energy = g_value_get_double (value);
 
709
                break;
 
710
        case PROP_ENERGY_EMPTY:
 
711
                device->priv->energy_empty = g_value_get_double (value);
 
712
                break;
 
713
        case PROP_ENERGY_FULL:
 
714
                device->priv->energy_full = g_value_get_double (value);
 
715
                break;
 
716
        case PROP_ENERGY_FULL_DESIGN:
 
717
                device->priv->energy_full_design = g_value_get_double (value);
 
718
                break;
 
719
        case PROP_ENERGY_RATE:
 
720
                device->priv->energy_rate = g_value_get_double (value);
 
721
                break;
 
722
        case PROP_VOLTAGE:
 
723
                device->priv->voltage = g_value_get_double (value);
 
724
                break;
 
725
        case PROP_TIME_TO_EMPTY:
 
726
                device->priv->time_to_empty = g_value_get_int64 (value);
 
727
                break;
 
728
        case PROP_TIME_TO_FULL:
 
729
                device->priv->time_to_full = g_value_get_int64 (value);
 
730
                break;
 
731
        case PROP_PERCENTAGE:
 
732
                device->priv->percentage = g_value_get_double (value);
 
733
                break;
 
734
        case PROP_TECHNOLOGY:
 
735
                device->priv->technology = g_value_get_uint (value);
 
736
                break;
 
737
        case PROP_RECALL_NOTICE:
 
738
                device->priv->recall_notice = g_value_get_boolean (value);
 
739
                break;
 
740
        case PROP_RECALL_VENDOR:
 
741
                g_free (device->priv->recall_vendor);
 
742
                device->priv->recall_vendor = g_strdup (g_value_get_string (value));
 
743
                break;
 
744
        case PROP_RECALL_URL:
 
745
                g_free (device->priv->recall_url);
 
746
                device->priv->recall_url = g_strdup (g_value_get_string (value));
 
747
                break;
 
748
        default:
 
749
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
750
                break;
 
751
        }
 
752
}
 
753
 
 
754
/**
 
755
 * dkp_device_get_property:
 
756
 **/
 
757
static void
 
758
dkp_device_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
 
759
{
 
760
        DkpDevice *device = DKP_DEVICE (object);
 
761
 
 
762
        switch (prop_id) {
 
763
        case PROP_UPDATE_TIME:
 
764
                g_value_set_uint64 (value, device->priv->update_time);
 
765
                break;
 
766
        case PROP_VENDOR:
 
767
                g_value_set_string (value, device->priv->vendor);
 
768
                break;
 
769
        case PROP_MODEL:
 
770
                g_value_set_string (value, device->priv->model);
 
771
                break;
 
772
        case PROP_SERIAL:
 
773
                g_value_set_string (value, device->priv->serial);
 
774
                break;
 
775
        case PROP_NATIVE_PATH:
 
776
                g_value_set_string (value, device->priv->native_path);
 
777
                break;
 
778
        case PROP_POWER_SUPPLY:
 
779
                g_value_set_boolean (value, device->priv->power_supply);
 
780
                break;
 
781
        case PROP_ONLINE:
 
782
                g_value_set_boolean (value, device->priv->online);
 
783
                break;
 
784
        case PROP_IS_PRESENT:
 
785
                g_value_set_boolean (value, device->priv->is_present);
 
786
                break;
 
787
        case PROP_IS_RECHARGEABLE:
 
788
                g_value_set_boolean (value, device->priv->is_rechargeable);
 
789
                break;
 
790
        case PROP_HAS_HISTORY:
 
791
                g_value_set_boolean (value, device->priv->has_history);
 
792
                break;
 
793
        case PROP_HAS_STATISTICS:
 
794
                g_value_set_boolean (value, device->priv->has_statistics);
 
795
                break;
 
796
        case PROP_TYPE:
 
797
                g_value_set_uint (value, device->priv->type);
 
798
                break;
 
799
        case PROP_STATE:
 
800
                g_value_set_uint (value, device->priv->state);
 
801
                break;
 
802
        case PROP_TECHNOLOGY:
 
803
                g_value_set_uint (value, device->priv->technology);
 
804
                break;
 
805
        case PROP_CAPACITY:
 
806
                g_value_set_double (value, device->priv->capacity);
 
807
                break;
 
808
        case PROP_ENERGY:
 
809
                g_value_set_double (value, device->priv->energy);
 
810
                break;
 
811
        case PROP_ENERGY_EMPTY:
 
812
                g_value_set_double (value, device->priv->energy_empty);
 
813
                break;
 
814
        case PROP_ENERGY_FULL:
 
815
                g_value_set_double (value, device->priv->energy_full);
 
816
                break;
 
817
        case PROP_ENERGY_FULL_DESIGN:
 
818
                g_value_set_double (value, device->priv->energy_full_design);
 
819
                break;
 
820
        case PROP_ENERGY_RATE:
 
821
                g_value_set_double (value, device->priv->energy_rate);
 
822
                break;
 
823
        case PROP_VOLTAGE:
 
824
                g_value_set_double (value, device->priv->voltage);
 
825
                break;
 
826
        case PROP_TIME_TO_EMPTY:
 
827
                g_value_set_int64 (value, device->priv->time_to_empty);
 
828
                break;
 
829
        case PROP_TIME_TO_FULL:
 
830
                g_value_set_int64 (value, device->priv->time_to_full);
 
831
                break;
 
832
        case PROP_PERCENTAGE:
 
833
                g_value_set_double (value, device->priv->percentage);
 
834
                break;
 
835
        case PROP_RECALL_NOTICE:
 
836
                g_value_set_boolean (value, device->priv->recall_notice);
 
837
                break;
 
838
        case PROP_RECALL_VENDOR:
 
839
                g_value_set_string (value, device->priv->recall_vendor);
 
840
                break;
 
841
        case PROP_RECALL_URL:
 
842
                g_value_set_string (value, device->priv->recall_url);
 
843
                break;
 
844
        default:
 
845
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
846
                break;
 
847
    }
 
848
}
 
849
 
 
850
/**
 
851
 * dkp_device_class_init:
 
852
 * @klass: The DkpDeviceClass
 
853
 **/
 
854
static void
 
855
dkp_device_class_init (DkpDeviceClass *klass)
 
856
{
 
857
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
858
        object_class->finalize = dkp_device_finalize;
 
859
        object_class->set_property = dkp_device_set_property;
 
860
        object_class->get_property = dkp_device_get_property;
 
861
 
 
862
        /**
 
863
         * PkClient::changed:
 
864
         * @device: the #DkpDevice instance that emitted the signal
 
865
         * @obj: the #DkpObject that has changed //TODO
 
866
         *
 
867
         * The ::changed signal is emitted when the device data has changed.
 
868
         **/
 
869
        signals [DKP_DEVICE_CHANGED] =
 
870
                g_signal_new ("changed",
 
871
                              G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
 
872
                              G_STRUCT_OFFSET (DkpDeviceClass, changed),
 
873
                              NULL, NULL, g_cclosure_marshal_VOID__POINTER,
 
874
                              G_TYPE_NONE, 1, G_TYPE_POINTER);
 
875
 
 
876
        /**
 
877
         * DkpDevice:update-time:
 
878
         */
 
879
        g_object_class_install_property (object_class,
 
880
                                         PROP_UPDATE_TIME,
 
881
                                         g_param_spec_uint64 ("update-time",
 
882
                                                              NULL, NULL,
 
883
                                                              0, G_MAXUINT64, 0,
 
884
                                                              G_PARAM_READWRITE));
 
885
        /**
 
886
         * DkpDevice:vendor:
 
887
         */
 
888
        g_object_class_install_property (object_class,
 
889
                                         PROP_VENDOR,
 
890
                                         g_param_spec_string ("vendor",
 
891
                                                              NULL, NULL,
 
892
                                                              NULL,
 
893
                                                              G_PARAM_READWRITE));
 
894
        /**
 
895
         * DkpDevice:model:
 
896
         */
 
897
        g_object_class_install_property (object_class,
 
898
                                         PROP_MODEL,
 
899
                                         g_param_spec_string ("model",
 
900
                                                              NULL, NULL,
 
901
                                                              NULL,
 
902
                                                              G_PARAM_READWRITE));
 
903
        /**
 
904
         * DkpDevice:serial:
 
905
         */
 
906
        g_object_class_install_property (object_class,
 
907
                                         PROP_SERIAL,
 
908
                                         g_param_spec_string ("serial",
 
909
                                                              NULL, NULL,
 
910
                                                              NULL,
 
911
                                                              G_PARAM_READWRITE));
 
912
        /**
 
913
         * DkpDevice:native-path:
 
914
         */
 
915
        g_object_class_install_property (object_class,
 
916
                                         PROP_NATIVE_PATH,
 
917
                                         g_param_spec_string ("native-path",
 
918
                                                              NULL, NULL,
 
919
                                                              NULL,
 
920
                                                              G_PARAM_READWRITE));
 
921
        /**
 
922
         * DkpDevice:power-supply:
 
923
         */
 
924
        g_object_class_install_property (object_class,
 
925
                                         PROP_POWER_SUPPLY,
 
926
                                         g_param_spec_boolean ("power-supply",
 
927
                                                               NULL, NULL,
 
928
                                                               FALSE,
 
929
                                                               G_PARAM_READWRITE));
 
930
        /**
 
931
         * DkpDevice:online:
 
932
         */
 
933
        g_object_class_install_property (object_class,
 
934
                                         PROP_ONLINE,
 
935
                                         g_param_spec_boolean ("online",
 
936
                                                               NULL, NULL,
 
937
                                                               FALSE,
 
938
                                                               G_PARAM_READWRITE));
 
939
        /**
 
940
         * DkpDevice:is-present:
 
941
         */
 
942
        g_object_class_install_property (object_class,
 
943
                                         PROP_IS_PRESENT,
 
944
                                         g_param_spec_boolean ("is-present",
 
945
                                                               NULL, NULL,
 
946
                                                               FALSE,
 
947
                                                               G_PARAM_READWRITE));
 
948
        /**
 
949
         * DkpDevice:is-rechargeable:
 
950
         */
 
951
        g_object_class_install_property (object_class,
 
952
                                         PROP_IS_RECHARGEABLE,
 
953
                                         g_param_spec_boolean ("is-rechargeable",
 
954
                                                               NULL, NULL,
 
955
                                                               FALSE,
 
956
                                                               G_PARAM_READWRITE));
 
957
        /**
 
958
         * DkpDevice:has-history:
 
959
         */
 
960
        g_object_class_install_property (object_class,
 
961
                                         PROP_HAS_HISTORY,
 
962
                                         g_param_spec_boolean ("has-history",
 
963
                                                               NULL, NULL,
 
964
                                                               FALSE,
 
965
                                                               G_PARAM_READWRITE));
 
966
        /**
 
967
         * DkpDevice:has-statistics:
 
968
         */
 
969
        g_object_class_install_property (object_class,
 
970
                                         PROP_HAS_STATISTICS,
 
971
                                         g_param_spec_boolean ("has-statistics",
 
972
                                                               NULL, NULL,
 
973
                                                               FALSE,
 
974
                                                               G_PARAM_READWRITE));
 
975
        /**
 
976
         * DkpDevice:type:
 
977
         */
 
978
        g_object_class_install_property (object_class,
 
979
                                         PROP_TYPE,
 
980
                                         g_param_spec_uint ("type",
 
981
                                                            NULL, NULL,
 
982
                                                            DKP_DEVICE_TYPE_UNKNOWN,
 
983
                                                            DKP_DEVICE_TYPE_LAST,
 
984
                                                            DKP_DEVICE_TYPE_UNKNOWN,
 
985
                                                            G_PARAM_READWRITE));
 
986
        /**
 
987
         * DkpDevice:state:
 
988
         */
 
989
        g_object_class_install_property (object_class,
 
990
                                         PROP_STATE,
 
991
                                         g_param_spec_uint ("state",
 
992
                                                            NULL, NULL,
 
993
                                                            DKP_DEVICE_STATE_UNKNOWN,
 
994
                                                            DKP_DEVICE_STATE_LAST,
 
995
                                                            DKP_DEVICE_STATE_UNKNOWN,
 
996
                                                            G_PARAM_READWRITE));
 
997
        /**
 
998
         * DkpDevice:technology:
 
999
         */
 
1000
        g_object_class_install_property (object_class,
 
1001
                                         PROP_TECHNOLOGY,
 
1002
                                         g_param_spec_uint ("technology",
 
1003
                                                            NULL, NULL,
 
1004
                                                            DKP_DEVICE_TECHNOLOGY_UNKNOWN,
 
1005
                                                            DKP_DEVICE_TECHNOLOGY_LAST,
 
1006
                                                            DKP_DEVICE_TECHNOLOGY_UNKNOWN,
 
1007
                                                            G_PARAM_READWRITE));
 
1008
        /**
 
1009
         * DkpDevice:capacity:
 
1010
         */
 
1011
        g_object_class_install_property (object_class,
 
1012
                                         PROP_CAPACITY,
 
1013
                                         g_param_spec_double ("capacity", NULL, NULL,
 
1014
                                                              0.0, 100.f, 100.0,
 
1015
                                                              G_PARAM_READWRITE));
 
1016
        /**
 
1017
         * DkpDevice:energy:
 
1018
         */
 
1019
        g_object_class_install_property (object_class,
 
1020
                                         PROP_ENERGY,
 
1021
                                         g_param_spec_double ("energy", NULL, NULL,
 
1022
                                                              0.0, G_MAXDOUBLE, 0.0,
 
1023
                                                              G_PARAM_READWRITE));
 
1024
        /**
 
1025
         * DkpDevice:energy-empty:
 
1026
         */
 
1027
        g_object_class_install_property (object_class,
 
1028
                                         PROP_ENERGY_EMPTY,
 
1029
                                         g_param_spec_double ("energy-empty", NULL, NULL,
 
1030
                                                              0.0, G_MAXDOUBLE, 0.0,
 
1031
                                                              G_PARAM_READWRITE));
 
1032
        /**
 
1033
         * DkpDevice:energy-full:
 
1034
         */
 
1035
        g_object_class_install_property (object_class,
 
1036
                                         PROP_ENERGY_FULL,
 
1037
                                         g_param_spec_double ("energy-full", NULL, NULL,
 
1038
                                                              0.0, G_MAXDOUBLE, 0.0,
 
1039
                                                              G_PARAM_READWRITE));
 
1040
        /**
 
1041
         * DkpDevice:energy-full-design:
 
1042
         */
 
1043
        g_object_class_install_property (object_class,
 
1044
                                         PROP_ENERGY_FULL_DESIGN,
 
1045
                                         g_param_spec_double ("energy-full-design", NULL, NULL,
 
1046
                                                              0.0, G_MAXDOUBLE, 0.0,
 
1047
                                                              G_PARAM_READWRITE));
 
1048
        /**
 
1049
         * DkpDevice:energy-rate:
 
1050
         */
 
1051
        g_object_class_install_property (object_class,
 
1052
                                         PROP_ENERGY_RATE,
 
1053
                                         g_param_spec_double ("energy-rate", NULL, NULL,
 
1054
                                                              0.0, G_MAXDOUBLE, 0.0,
 
1055
                                                              G_PARAM_READWRITE));
 
1056
        /**
 
1057
         * DkpDevice:voltage:
 
1058
         */
 
1059
        g_object_class_install_property (object_class,
 
1060
                                         PROP_VOLTAGE,
 
1061
                                         g_param_spec_double ("voltage", NULL, NULL,
 
1062
                                                              0.0, G_MAXDOUBLE, 0.0,
 
1063
                                                              G_PARAM_READWRITE));
 
1064
        /**
 
1065
         * DkpDevice:time-to-empty:
 
1066
         */
 
1067
        g_object_class_install_property (object_class,
 
1068
                                         PROP_TIME_TO_EMPTY,
 
1069
                                         g_param_spec_int64 ("time-to-empty", NULL, NULL,
 
1070
                                                              0, G_MAXINT64, 0,
 
1071
                                                              G_PARAM_READWRITE));
 
1072
        /**
 
1073
         * DkpDevice:time-to-full:
 
1074
         */
 
1075
        g_object_class_install_property (object_class,
 
1076
                                         PROP_TIME_TO_FULL,
 
1077
                                         g_param_spec_int64 ("time-to-full", NULL, NULL,
 
1078
                                                              0, G_MAXINT64, 0,
 
1079
                                                              G_PARAM_READWRITE));
 
1080
        /**
 
1081
         * DkpDevice:percentage:
 
1082
         */
 
1083
        g_object_class_install_property (object_class,
 
1084
                                         PROP_PERCENTAGE,
 
1085
                                         g_param_spec_double ("percentage", NULL, NULL,
 
1086
                                                              0.0, 100.f, 100.0,
 
1087
                                                              G_PARAM_READWRITE));
 
1088
        /**
 
1089
         * DkpDevice:recall-notice:
 
1090
         */
 
1091
        g_object_class_install_property (object_class,
 
1092
                                         PROP_RECALL_NOTICE,
 
1093
                                         g_param_spec_boolean ("recall-notice",
 
1094
                                                               NULL, NULL,
 
1095
                                                               FALSE,
 
1096
                                                               G_PARAM_READWRITE));
 
1097
        /**
 
1098
         * DkpDevice:recall-vendor:
 
1099
         */
 
1100
        g_object_class_install_property (object_class,
 
1101
                                         PROP_RECALL_VENDOR,
 
1102
                                         g_param_spec_string ("recall-vendor",
 
1103
                                                              NULL, NULL,
 
1104
                                                              NULL,
 
1105
                                                              G_PARAM_READWRITE));
 
1106
        /**
 
1107
         * DkpDevice:recall-url:
 
1108
         */
 
1109
        g_object_class_install_property (object_class,
 
1110
                                         PROP_RECALL_URL,
 
1111
                                         g_param_spec_string ("recall-url",
 
1112
                                                              NULL, NULL,
 
1113
                                                              NULL,
 
1114
                                                              G_PARAM_READWRITE));
 
1115
 
 
1116
        g_type_class_add_private (klass, sizeof (DkpDevicePrivate));
 
1117
}
 
1118
 
 
1119
/**
 
1120
 * dkp_device_init:
 
1121
 * @device: This class instance
 
1122
 **/
 
1123
static void
 
1124
dkp_device_init (DkpDevice *device)
 
1125
{
 
1126
        device->priv = DKP_DEVICE_GET_PRIVATE (device);
 
1127
        device->priv->object_path = NULL;
 
1128
        device->priv->proxy_device = NULL;
 
1129
        device->priv->proxy_props = NULL;
 
1130
}
 
1131
 
 
1132
/**
 
1133
 * dkp_device_finalize:
 
1134
 * @object: The object to finalize
 
1135
 **/
 
1136
static void
 
1137
dkp_device_finalize (GObject *object)
 
1138
{
 
1139
        DkpDevice *device;
 
1140
 
 
1141
        g_return_if_fail (DKP_IS_DEVICE (object));
 
1142
 
 
1143
        device = DKP_DEVICE (object);
 
1144
 
 
1145
        g_free (device->priv->object_path);
 
1146
        g_free (device->priv->vendor);
 
1147
        g_free (device->priv->model);
 
1148
        g_free (device->priv->serial);
 
1149
        g_free (device->priv->native_path);
 
1150
        g_free (device->priv->recall_vendor);
 
1151
        g_free (device->priv->recall_url);
 
1152
        if (device->priv->proxy_device != NULL)
 
1153
                g_object_unref (device->priv->proxy_device);
 
1154
        if (device->priv->proxy_props != NULL)
 
1155
                g_object_unref (device->priv->proxy_props);
 
1156
 
 
1157
        G_OBJECT_CLASS (dkp_device_parent_class)->finalize (object);
 
1158
}
 
1159
 
 
1160
/**
 
1161
 * dkp_device_new:
 
1162
 *
 
1163
 * Return value: a new DkpDevice object.
 
1164
 **/
 
1165
DkpDevice *
 
1166
dkp_device_new (void)
 
1167
{
 
1168
        DkpDevice *device;
 
1169
        device = g_object_new (DKP_TYPE_DEVICE, NULL);
 
1170
        return DKP_DEVICE (device);
 
1171
}
 
1172