1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2009 Richard Hughes <richard@hughsie.com>
5
* Licensed under the GNU General Public License Version 2
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
#include <dbus/dbus-glib.h>
29
#include "up-wakeups.h"
31
static void up_wakeups_class_init (UpWakeupsClass *klass);
32
static void up_wakeups_init (UpWakeups *wakeups);
33
static void up_wakeups_finalize (GObject *object);
35
#define UP_WAKEUPS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_WAKEUPS, UpWakeupsPrivate))
37
struct UpWakeupsPrivate
41
DBusGProxy *prop_proxy;
42
gboolean has_capability;
43
gboolean have_properties;
47
UP_WAKEUPS_DATA_CHANGED,
48
UP_WAKEUPS_TOTAL_CHANGED,
49
UP_WAKEUPS_LAST_SIGNAL
52
static guint signals [UP_WAKEUPS_LAST_SIGNAL] = { 0 };
54
G_DEFINE_TYPE (UpWakeups, up_wakeups, G_TYPE_OBJECT)
57
* up_wakeups_get_total_sync:
58
* @wakeups: a #UpWakeups instance.
59
* @cancellable: a #GCancellable or %NULL
60
* @error: a #GError, or %NULL.
62
* Gets the the total number of wakeups per second from the daemon.
64
* Return value: number of wakeups per second.
69
up_wakeups_get_total_sync (UpWakeups *wakeups, GCancellable *cancellable, GError **error)
73
GError *error_local = NULL;
75
g_return_val_if_fail (UP_IS_WAKEUPS (wakeups), FALSE);
76
g_return_val_if_fail (wakeups->priv->proxy != NULL, FALSE);
78
ret = dbus_g_proxy_call (wakeups->priv->proxy, "GetTotal", &error_local,
83
g_warning ("Couldn't get total: %s", error_local->message);
84
g_set_error (error, 1, 0, "%s", error_local->message);
85
g_error_free (error_local);
91
* up_wakeups_get_data_sync:
92
* @wakeups: a #UpWakeups instance.
93
* @cancellable: a #GCancellable or %NULL
94
* @error: a #GError, or %NULL.
96
* Gets the wakeups data from the daemon.
98
* Return value: an array of %UpWakeupItem's
103
up_wakeups_get_data_sync (UpWakeups *wakeups, GCancellable *cancellable, GError **error)
105
GError *error_local = NULL;
106
GType g_type_gvalue_array;
107
GPtrArray *gvalue_ptr_array = NULL;
112
GPtrArray *array = NULL;
115
g_return_val_if_fail (UP_IS_WAKEUPS (wakeups), NULL);
116
g_return_val_if_fail (wakeups->priv->proxy != NULL, NULL);
118
g_type_gvalue_array = dbus_g_type_get_collection ("GPtrArray",
119
dbus_g_type_get_struct("GValueArray",
127
/* get compound data */
128
ret = dbus_g_proxy_call (wakeups->priv->proxy, "GetData", &error_local,
130
g_type_gvalue_array, &gvalue_ptr_array,
133
g_warning ("GetData on failed: %s", error_local->message);
134
g_set_error (error, 1, 0, "%s", error_local->message);
135
g_error_free (error_local);
140
if (gvalue_ptr_array->len == 0)
144
array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
145
for (i=0; i<gvalue_ptr_array->len; i++) {
146
gva = (GValueArray *) g_ptr_array_index (gvalue_ptr_array, i);
147
item = up_wakeup_item_new ();
150
gv = g_value_array_get_nth (gva, 0);
151
up_wakeup_item_set_is_userspace (item, g_value_get_boolean (gv));
155
gv = g_value_array_get_nth (gva, 1);
156
up_wakeup_item_set_id (item, g_value_get_uint (gv));
160
gv = g_value_array_get_nth (gva, 2);
161
up_wakeup_item_set_value (item, g_value_get_double (gv));
165
gv = g_value_array_get_nth (gva, 3);
166
up_wakeup_item_set_cmdline (item, g_value_get_string (gv));
170
gv = g_value_array_get_nth (gva, 4);
171
up_wakeup_item_set_details (item, g_value_get_string (gv));
175
g_ptr_array_add (array, item);
176
g_value_array_free (gva);
179
if (gvalue_ptr_array != NULL)
180
g_ptr_array_unref (gvalue_ptr_array);
185
* up_wakeups_ensure_properties:
188
up_wakeups_ensure_properties (UpWakeups *wakeups)
197
if (wakeups->priv->have_properties)
201
ret = dbus_g_proxy_call (wakeups->priv->prop_proxy, "GetAll", &error,
202
G_TYPE_STRING, "org.freedesktop.UPower.Wakeups",
204
dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), &props,
207
g_warning ("Error invoking GetAll() to get properties: %s", error->message);
208
g_error_free (error);
212
value = g_hash_table_lookup (props, "HasCapability");
214
g_warning ("No 'HasCapability' property");
217
wakeups->priv->has_capability = g_value_get_boolean (value);
220
wakeups->priv->have_properties = TRUE;
224
g_hash_table_unref (props);
228
* up_wakeups_get_properties_sync:
229
* @wakeups: a #UpWakeups instance.
230
* @cancellable: a #GCancellable or %NULL
231
* @error: a #GError, or %NULL.
233
* Gets properties from the daemon about wakeup data.
235
* Return value: %TRUE if supported
240
up_wakeups_get_properties_sync (UpWakeups *wakeups, GCancellable *cancellable, GError **error)
242
g_return_val_if_fail (UP_IS_WAKEUPS (wakeups), FALSE);
243
up_wakeups_ensure_properties (wakeups);
248
* up_wakeups_get_has_capability:
249
* @wakeups: a #UpWakeups instance.
251
* Returns if the daemon supports getting the wakeup data.
253
* Return value: %TRUE if supported
258
up_wakeups_get_has_capability (UpWakeups *wakeups)
260
g_return_val_if_fail (UP_IS_WAKEUPS (wakeups), FALSE);
261
up_wakeups_ensure_properties (wakeups);
262
return wakeups->priv->has_capability;
266
* up_wakeups_total_changed_cb:
269
up_wakeups_total_changed_cb (DBusGProxy *proxy, guint value, UpWakeups *wakeups)
271
g_signal_emit (wakeups, signals [UP_WAKEUPS_TOTAL_CHANGED], 0, value);
275
* up_wakeups_data_changed_cb:
278
up_wakeups_data_changed_cb (DBusGProxy *proxy, UpWakeups *wakeups)
280
g_signal_emit (wakeups, signals [UP_WAKEUPS_DATA_CHANGED], 0);
284
* up_wakeups_class_init:
287
up_wakeups_class_init (UpWakeupsClass *klass)
289
GObjectClass *object_class = G_OBJECT_CLASS (klass);
290
object_class->finalize = up_wakeups_finalize;
292
signals [UP_WAKEUPS_DATA_CHANGED] =
293
g_signal_new ("data-changed",
294
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
295
G_STRUCT_OFFSET (UpWakeupsClass, data_changed),
296
NULL, NULL, g_cclosure_marshal_VOID__VOID,
298
signals [UP_WAKEUPS_TOTAL_CHANGED] =
299
g_signal_new ("total-changed",
300
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
301
G_STRUCT_OFFSET (UpWakeupsClass, data_changed),
302
NULL, NULL, g_cclosure_marshal_VOID__UINT,
303
G_TYPE_NONE, 1, G_TYPE_UINT);
305
g_type_class_add_private (klass, sizeof (UpWakeupsPrivate));
312
up_wakeups_init (UpWakeups *wakeups)
314
GError *error = NULL;
316
wakeups->priv = UP_WAKEUPS_GET_PRIVATE (wakeups);
317
wakeups->priv->has_capability = FALSE;
318
wakeups->priv->have_properties = FALSE;
321
wakeups->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
322
if (wakeups->priv->bus == NULL) {
323
g_warning ("Couldn't connect to system bus: %s", error->message);
324
g_error_free (error);
328
/* connect to properties interface */
329
wakeups->priv->prop_proxy = dbus_g_proxy_new_for_name (wakeups->priv->bus,
330
"org.freedesktop.UPower",
331
"/org/freedesktop/UPower/Wakeups",
332
"org.freedesktop.DBus.Properties");
333
if (wakeups->priv->prop_proxy == NULL) {
334
g_warning ("Couldn't connect to proxy");
338
/* connect to main interface */
339
wakeups->priv->proxy = dbus_g_proxy_new_for_name (wakeups->priv->bus,
340
"org.freedesktop.UPower",
341
"/org/freedesktop/UPower/Wakeups",
342
"org.freedesktop.UPower.Wakeups");
343
if (wakeups->priv->proxy == NULL) {
344
g_warning ("Couldn't connect to proxy");
347
dbus_g_proxy_add_signal (wakeups->priv->proxy, "TotalChanged", G_TYPE_UINT, G_TYPE_INVALID);
348
dbus_g_proxy_add_signal (wakeups->priv->proxy, "DataChanged", G_TYPE_INVALID);
351
dbus_g_proxy_connect_signal (wakeups->priv->proxy, "TotalChanged",
352
G_CALLBACK (up_wakeups_total_changed_cb), wakeups, NULL);
353
dbus_g_proxy_connect_signal (wakeups->priv->proxy, "DataChanged",
354
G_CALLBACK (up_wakeups_data_changed_cb), wakeups, NULL);
360
* up_wakeups_finalize:
363
up_wakeups_finalize (GObject *object)
367
g_return_if_fail (UP_IS_WAKEUPS (object));
369
wakeups = UP_WAKEUPS (object);
370
if (wakeups->priv->proxy != NULL)
371
g_object_unref (wakeups->priv->proxy);
372
if (wakeups->priv->prop_proxy != NULL)
373
g_object_unref (wakeups->priv->prop_proxy);
375
G_OBJECT_CLASS (up_wakeups_parent_class)->finalize (object);
381
* Gets a new object to allow querying the wakeups data from the server.
383
* Return value: the a new @UpWakeups object.
388
up_wakeups_new (void)
391
wakeups = g_object_new (UP_TYPE_WAKEUPS, NULL);
392
return UP_WAKEUPS (wakeups);