1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2008-2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
* @short_description: Main client object for accessing the UPower daemon
26
* A helper GObject to use for accessing UPower information, and to be notified
37
#include <dbus/dbus-glib.h>
39
#include "up-client.h"
40
#include "up-device.h"
42
static void up_client_class_init (UpClientClass *klass);
43
static void up_client_init (UpClient *client);
44
static void up_client_finalize (GObject *object);
46
#define UP_CLIENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_CLIENT, UpClientPrivate))
51
* Private #UpClient data
53
struct _UpClientPrivate
57
DBusGProxy *prop_proxy;
59
gboolean have_properties;
60
gchar *daemon_version;
62
gboolean can_hibernate;
63
gboolean lid_is_closed;
65
gboolean on_low_battery;
66
gboolean lid_is_present;
67
gboolean lid_force_sleep;
69
gboolean done_enumerate;
73
UP_CLIENT_DEVICE_ADDED,
74
UP_CLIENT_DEVICE_CHANGED,
75
UP_CLIENT_DEVICE_REMOVED,
77
UP_CLIENT_NOTIFY_SLEEP,
78
UP_CLIENT_NOTIFY_RESUME,
96
static guint signals [UP_CLIENT_LAST_SIGNAL] = { 0 };
97
static gpointer up_client_object = NULL;
99
G_DEFINE_TYPE (UpClient, up_client, G_TYPE_OBJECT)
102
* up_client_get_device:
105
up_client_get_device (UpClient *client, const gchar *object_path)
108
const gchar *object_path_tmp;
110
UpClientPrivate *priv = client->priv;
112
for (i=0; i<priv->array->len; i++) {
113
device = g_ptr_array_index (priv->array, i);
114
object_path_tmp = up_device_get_object_path (device);
115
if (g_strcmp0 (object_path_tmp, object_path) == 0)
122
* up_client_get_devices:
123
* @client: a #UpClient instance.
125
* Get a copy of the device objects.
126
* You must have called up_client_enumerate_devices_sync() before calling this
129
* Return value: (transfer full): an array of #UpDevice objects, free with g_ptr_array_unref()
134
up_client_get_devices (UpClient *client)
136
g_return_val_if_fail (UP_IS_CLIENT (client), NULL);
137
g_return_val_if_fail (client->priv->done_enumerate, NULL);
138
return g_ptr_array_ref (client->priv->array);
142
* up_client_get_devices_private:
145
up_client_get_devices_private (UpClient *client, GError **error)
148
GError *error_local = NULL;
149
GPtrArray *devices = NULL;
152
if (!client->priv->proxy)
154
g_type_array = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH);
155
ret = dbus_g_proxy_call (client->priv->proxy, "EnumerateDevices", &error_local,
157
g_type_array, &devices,
160
g_warning ("Couldn't enumerate devices: %s", error_local->message);
161
g_set_error (error, 1, 0, "%s", error_local->message);
162
g_error_free (error_local);
168
* up_client_suspend_sync:
169
* @client: a #UpClient instance.
170
* @cancellable: a #GCancellable or %NULL
171
* @error: a #GError, or %NULL.
173
* Puts the computer into a low power state, but state is not preserved if the
176
* NOTE: The system is still consuming a small amount of power
178
* Return value: TRUE if system suspended okay, FALSE other wise.
183
up_client_suspend_sync (UpClient *client, GCancellable *cancellable, GError **error)
186
GError *error_local = NULL;
188
g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
189
g_return_val_if_fail (client->priv->proxy != NULL, FALSE);
191
ret = dbus_g_proxy_call (client->priv->proxy, "Suspend", &error_local,
192
G_TYPE_INVALID, G_TYPE_INVALID);
194
/* DBus might time out, which is okay */
195
if (g_error_matches (error_local, DBUS_GERROR, DBUS_GERROR_NO_REPLY)) {
196
g_debug ("DBUS timed out, but recovering");
201
/* an actual error */
202
g_warning ("Couldn't suspend: %s", error_local->message);
203
g_set_error (error, 1, 0, "%s", error_local->message);
206
if (error_local != NULL)
207
g_error_free (error_local);
212
* up_client_hibernate_sync:
213
* @client: a #UpClient instance.
214
* @cancellable: a #GCancellable or %NULL
217
* Puts the computer into a low power state, where state is preserved if the
220
* Return value: TRUE if system suspended okay, FALSE other wise.
225
up_client_hibernate_sync (UpClient *client, GCancellable *cancellable, GError **error)
228
GError *error_local = NULL;
230
g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
231
g_return_val_if_fail (client->priv->proxy != NULL, FALSE);
233
ret = dbus_g_proxy_call (client->priv->proxy, "Hibernate", &error_local,
234
G_TYPE_INVALID, G_TYPE_INVALID);
236
/* DBus might time out, which is okay */
237
if (g_error_matches (error_local, DBUS_GERROR, DBUS_GERROR_NO_REPLY)) {
238
g_debug ("DBUS timed out, but recovering");
243
/* an actual error */
244
g_warning ("Couldn't hibernate: %s", error_local->message);
245
g_set_error (error, 1, 0, "%s", error_local->message);
248
if (error_local != NULL)
249
g_error_free (error_local);
254
* up_client_about_to_sleep_sync:
255
* @client: a #UpClient instance.
256
* @sleep_kind: a sleep type, e.g. %UP_SLEEP_KIND_SUSPEND
257
* @cancellable: a #GCancellable or %NULL
258
* @error: a #GError, or %NULL.
260
* Tells UPower that we are soon to reqest either Suspend() or Hibernate()
261
* and that session and system components should be notified of this.
263
* Return value: TRUE if system suspended okay, FALSE other wise.
268
up_client_about_to_sleep_sync (UpClient *client,
269
UpSleepKind sleep_kind,
270
GCancellable *cancellable,
274
GError *error_local = NULL;
276
g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
277
g_return_val_if_fail (client->priv->proxy != NULL, FALSE);
279
ret = dbus_g_proxy_call (client->priv->proxy, "AboutToSleep", &error_local,
280
G_TYPE_STRING, sleep_kind,
284
/* DBus might time out, which is okay */
285
if (g_error_matches (error_local, DBUS_GERROR, DBUS_GERROR_NO_REPLY)) {
286
g_debug ("DBUS timed out, but recovering");
291
/* an actual error */
292
g_warning ("Couldn't sent that we were about to sleep: %s", error_local->message);
293
g_set_error (error, 1, 0, "%s", error_local->message);
296
if (error_local != NULL)
297
g_error_free (error_local);
302
* up_client_get_properties_sync:
303
* @client: a #UpClient instance.
304
* @cancellable: a #GCancellable or %NULL
305
* @error: a #GError, or %NULL.
307
* Get all the properties from UPower daemon.
309
* Return value: %TRUE for success, else %FALSE.
314
up_client_get_properties_sync (UpClient *client, GCancellable *cancellable, GError **error)
320
#ifdef ENABLE_DEPRECATED
321
gboolean allowed = FALSE;
326
if (client->priv->have_properties)
328
if (!client->priv->prop_proxy)
333
ret = dbus_g_proxy_call (client->priv->prop_proxy, "GetAll", error,
334
G_TYPE_STRING, "org.freedesktop.UPower",
336
dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), &props,
341
value = g_hash_table_lookup (props, "DaemonVersion");
343
g_warning ("No 'DaemonVersion' property");
346
g_free (client->priv->daemon_version);
347
client->priv->daemon_version = g_strdup (g_value_get_string (value));
349
#ifdef ENABLE_DEPRECATED
350
value = g_hash_table_lookup (props, "CanSuspend");
352
g_warning ("No 'CanSuspend' property");
356
ret = dbus_g_proxy_call (client->priv->proxy, "SuspendAllowed", error,
357
G_TYPE_INVALID, G_TYPE_BOOLEAN, &allowed, G_TYPE_INVALID);
361
prop_val = g_value_get_boolean (value) && allowed;
362
if (prop_val != client->priv->can_suspend) {
363
client->priv->can_suspend = ret;
364
g_object_notify (G_OBJECT(client), "can-suspend");
367
value = g_hash_table_lookup (props, "CanHibernate");
369
g_warning ("No 'CanHibernate' property");
372
ret = dbus_g_proxy_call (client->priv->proxy, "HibernateAllowed", error,
373
G_TYPE_INVALID, G_TYPE_BOOLEAN, &allowed, G_TYPE_INVALID);
377
prop_val = g_value_get_boolean (value) && allowed;
378
if (prop_val != client->priv->can_hibernate) {
379
client->priv->can_hibernate = ret;
380
g_object_notify (G_OBJECT(client), "can-hibernate");
382
#endif /* ENABLE_DEPRECATED */
384
value = g_hash_table_lookup (props, "LidIsClosed");
386
g_warning ("No 'LidIsClosed' property");
389
prop_val = g_value_get_boolean (value);
390
if (prop_val != client->priv->lid_is_closed) {
391
client->priv->lid_is_closed = ret;
392
g_object_notify (G_OBJECT(client), "lid-is-closed");
395
value = g_hash_table_lookup (props, "OnBattery");
397
g_warning ("No 'OnBattery' property");
400
prop_val = g_value_get_boolean (value);
401
if (prop_val != client->priv->on_battery) {
402
client->priv->on_battery = ret;
403
g_object_notify (G_OBJECT(client), "on-battery");
406
value = g_hash_table_lookup (props, "OnLowBattery");
408
g_warning ("No 'OnLowBattery' property");
411
prop_val = g_value_get_boolean (value);
412
if (prop_val != client->priv->on_low_battery) {
413
client->priv->on_low_battery = ret;
414
g_object_notify (G_OBJECT(client), "on-low-battery");
417
value = g_hash_table_lookup (props, "LidIsPresent");
419
g_warning ("No 'LidIsPresent' property");
422
prop_val = g_value_get_boolean (value);
423
if (prop_val != client->priv->lid_is_present) {
424
client->priv->lid_is_present = ret;
425
g_object_notify (G_OBJECT(client), "lid-is-present");
428
value = g_hash_table_lookup (props, "IsDocked");
430
g_warning ("No 'IsDocked' property");
433
prop_val = g_value_get_boolean (value);
434
if (ret != client->priv->is_docked) {
435
client->priv->is_docked = ret;
436
g_object_notify (G_OBJECT(client), "is-docked");
439
value = g_hash_table_lookup (props, "LidForceSleep");
441
g_warning ("No 'LidForceSleep' property");
444
prop_val = g_value_get_boolean (value);
445
if (prop_val != client->priv->lid_force_sleep) {
446
client->priv->lid_force_sleep = ret;
447
g_object_notify (G_OBJECT(client), "lid-force-sleep");
451
client->priv->have_properties = TRUE;
455
g_hash_table_unref (props);
460
* up_client_get_daemon_version:
461
* @client: a #UpClient instance.
463
* Get UPower daemon version.
465
* Return value: string containing the daemon version, e.g. 008
470
up_client_get_daemon_version (UpClient *client)
472
g_return_val_if_fail (UP_IS_CLIENT (client), NULL);
473
up_client_get_properties_sync (client, NULL, NULL);
474
return client->priv->daemon_version;
478
* up_client_get_can_hibernate:
479
* @client: a #UpClient instance.
481
* Get whether the system is able to hibernate.
483
* Return value: TRUE if system can hibernate, FALSE other wise.
488
up_client_get_can_hibernate (UpClient *client)
490
g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
491
up_client_get_properties_sync (client, NULL, NULL);
492
return client->priv->can_hibernate;
496
* up_client_get_lid_is_closed:
497
* @client: a #UpClient instance.
499
* Get whether the laptop lid is closed.
501
* Return value: %TRUE if lid is closed or %FALSE otherwise.
506
up_client_get_lid_is_closed (UpClient *client)
508
g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
509
up_client_get_properties_sync (client, NULL, NULL);
510
return client->priv->lid_is_closed;
514
* up_client_get_lid_is_present:
515
* @client: a #UpClient instance.
517
* Get whether a laptop lid is present on this machine.
519
* Return value: %TRUE if the machine has a laptop lid
524
up_client_get_lid_is_present (UpClient *client)
526
g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
527
up_client_get_properties_sync (client, NULL, NULL);
528
return client->priv->lid_is_present;
532
* up_client_get_lid_force_sleep:
533
* @client: a #UpClient instance.
535
* Get whether the laptop has to sleep when the lid is closed.
537
* Return value: %TRUE if the session has to suspend
542
up_client_get_lid_force_sleep (UpClient *client)
544
g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
545
up_client_get_properties_sync (client, NULL, NULL);
546
return client->priv->lid_force_sleep;
550
* up_client_get_is_docked:
551
* @client: a #UpClient instance.
553
* Get whether the machine is docked into a docking station.
555
* Return value: %TRUE if the machine is docked
560
up_client_get_is_docked (UpClient *client)
562
g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
563
up_client_get_properties_sync (client, NULL, NULL);
564
return client->priv->is_docked;
568
* up_client_get_can_suspend:
569
* @client: a #UpClient instance.
571
* Get whether the system is able to suspend.
573
* Return value: TRUE if system can suspend, FALSE other wise.
578
up_client_get_can_suspend (UpClient *client)
580
g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
581
up_client_get_properties_sync (client, NULL, NULL);
582
return client->priv->can_suspend;
586
* up_client_get_on_battery:
587
* @client: a #UpClient instance.
589
* Get whether the system is running on battery power.
591
* Return value: TRUE if the system is currently running on battery, FALSE other wise.
596
up_client_get_on_battery (UpClient *client)
598
g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
599
up_client_get_properties_sync (client, NULL, NULL);
600
return client->priv->on_battery;
604
* up_client_get_on_low_battery:
605
* @client: a #UpClient instance.
607
* Get whether the system is running on low battery power.
609
* Return value: TRUE if the system is currently on low battery power, FALSE other wise.
614
up_client_get_on_low_battery (UpClient *client)
616
g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
617
up_client_get_properties_sync (client, NULL, NULL);
618
return client->priv->on_low_battery;
625
up_client_add (UpClient *client, const gchar *object_path)
627
UpDevice *device = NULL;
628
UpDevice *device_tmp;
631
/* check existing list for this object path */
632
device_tmp = up_client_get_device (client, object_path);
633
if (device_tmp != NULL) {
634
g_warning ("already added: %s", object_path);
638
/* create new device */
639
device = up_device_new ();
640
ret = up_device_set_object_path_sync (device, object_path, NULL, NULL);
645
g_ptr_array_add (client->priv->array, g_object_ref (device));
646
g_signal_emit (client, signals [UP_CLIENT_DEVICE_ADDED], 0, device);
649
g_object_unref (device);
653
* up_client_added_cb:
656
up_device_added_cb (DBusGProxy *proxy, const gchar *object_path, UpClient *client)
658
up_client_add (client, object_path);
662
* up_client_changed_cb:
665
up_device_changed_cb (DBusGProxy *proxy, const gchar *object_path, UpClient *client)
668
device = up_client_get_device (client, object_path);
670
g_signal_emit (client, signals [UP_CLIENT_DEVICE_CHANGED], 0, device);
674
* up_client_notify_sleep_cb:
677
up_client_notify_sleep_cb (DBusGProxy *proxy, const gchar *sleep_kind, UpClient *client)
679
g_signal_emit (client, signals [UP_CLIENT_NOTIFY_SLEEP], 0,
680
up_sleep_kind_from_string (sleep_kind));
684
* up_client_notify_resume_cb:
687
up_client_notify_resume_cb (DBusGProxy *proxy, const gchar *sleep_kind, UpClient *client)
689
g_signal_emit (client, signals [UP_CLIENT_NOTIFY_RESUME], 0,
690
up_sleep_kind_from_string (sleep_kind));
694
* up_client_removed_cb:
697
up_device_removed_cb (DBusGProxy *proxy, const gchar *object_path, UpClient *client)
700
device = up_client_get_device (client, object_path);
701
if (device != NULL) {
702
g_signal_emit (client, signals [UP_CLIENT_DEVICE_REMOVED], 0, device);
703
g_ptr_array_remove (client->priv->array, device);
708
* up_client_changed_cb:
711
up_client_changed_cb (DBusGProxy *proxy, UpClient *client)
713
client->priv->have_properties = FALSE;
714
g_signal_emit (client, signals [UP_CLIENT_CHANGED], 0);
718
up_client_get_property (GObject *object,
724
client = UP_CLIENT (object);
726
up_client_get_properties_sync (client, NULL, NULL);
729
case PROP_DAEMON_VERSION:
730
g_value_set_string (value, client->priv->daemon_version);
732
case PROP_CAN_SUSPEND:
733
g_value_set_boolean (value, client->priv->can_suspend);
735
case PROP_CAN_HIBERNATE:
736
g_value_set_boolean (value, client->priv->can_hibernate);
738
case PROP_ON_BATTERY:
739
g_value_set_boolean (value, client->priv->on_battery);
741
case PROP_ON_LOW_BATTERY:
742
g_value_set_boolean (value, client->priv->on_low_battery);
744
case PROP_LID_IS_CLOSED:
745
g_value_set_boolean (value, client->priv->lid_is_closed);
747
case PROP_LID_IS_PRESENT:
748
g_value_set_boolean (value, client->priv->lid_is_present);
750
case PROP_LID_FORCE_SLEEP:
751
g_value_set_boolean (value, client->priv->lid_force_sleep);
754
g_value_set_boolean (value, client->priv->is_docked);
757
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
763
* up_client_class_init:
764
* @klass: The UpClientClass
767
up_client_class_init (UpClientClass *klass)
769
GObjectClass *object_class = G_OBJECT_CLASS (klass);
771
object_class->get_property = up_client_get_property;
772
object_class->finalize = up_client_finalize;
775
* UpClient:daemon-version:
777
* The daemon version.
781
g_object_class_install_property (object_class,
783
g_param_spec_string ("daemon-version",
789
* UpClient:can-suspend:
791
* If the computer can suspend.
795
g_object_class_install_property (object_class,
797
g_param_spec_boolean ("can-suspend",
798
"If the computer can suspend",
803
* UpClient:can-hibernate:
805
* If the computer can hibernate.
809
g_object_class_install_property (object_class,
811
g_param_spec_boolean ("can-hibernate",
812
"If the computer can hibernate",
817
* UpClient:on-battery:
819
* If the computer is on battery power.
823
g_object_class_install_property (object_class,
825
g_param_spec_boolean ("on-battery",
826
"If the computer is on battery power",
831
* UpClient:on-low-battery:
833
* If the computer is on low battery power.
837
g_object_class_install_property (object_class,
839
g_param_spec_boolean ("on-low-battery",
840
"If the computer is on low battery power",
845
* UpClient:lid-is-closed:
847
* If the laptop lid is closed.
851
g_object_class_install_property (object_class,
853
g_param_spec_boolean ("lid-is-closed",
854
"If the laptop lid is closed",
859
* UpClient:lid-is-present:
861
* If a laptop lid is present.
865
g_object_class_install_property (object_class,
867
g_param_spec_boolean ("lid-is-present",
868
"If a laptop lid is present",
874
* UpClient:lid-force-sleep:
876
* If a laptop has to sleep if the lid is closed.
880
g_object_class_install_property (object_class,
881
PROP_LID_FORCE_SLEEP,
882
g_param_spec_boolean ("lid-force-sleep",
883
"If a laptop has to sleep on lid close",
889
* UpClient:is-docked:
891
* If the laptop is docked
895
g_object_class_install_property (object_class,
897
g_param_spec_boolean ("is-docked",
898
"If a laptop is docked",
904
* UpClient::device-added:
905
* @client: the #UpClient instance that emitted the signal
906
* @device: the #UpDevice that was added.
908
* The ::device-added signal is emitted when a power device is added.
912
signals [UP_CLIENT_DEVICE_ADDED] =
913
g_signal_new ("device-added",
914
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
915
G_STRUCT_OFFSET (UpClientClass, device_added),
916
NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
917
G_TYPE_NONE, 1, UP_TYPE_DEVICE);
920
* UpClient::device-removed:
921
* @client: the #UpClient instance that emitted the signal
922
* @device: the #UpDevice that was removed.
924
* The ::device-added signal is emitted when a power device is removed.
928
signals [UP_CLIENT_DEVICE_REMOVED] =
929
g_signal_new ("device-removed",
930
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
931
G_STRUCT_OFFSET (UpClientClass, device_removed),
932
NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
933
G_TYPE_NONE, 1, UP_TYPE_DEVICE);
936
* UpClient::device-changed:
937
* @client: the #UpClient instance that emitted the signal
938
* @device: the #UpDevice that was changed.
940
* The ::device-changed signal is emitted when a power device is changed.
944
signals [UP_CLIENT_DEVICE_CHANGED] =
945
g_signal_new ("device-changed",
946
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
947
G_STRUCT_OFFSET (UpClientClass, device_changed),
948
NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
949
G_TYPE_NONE, 1, UP_TYPE_DEVICE);
953
* @client: the #UpClient instance that emitted the signal
955
* The ::changed signal is emitted when properties may have changed.
959
signals [UP_CLIENT_CHANGED] =
960
g_signal_new ("changed",
961
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
962
G_STRUCT_OFFSET (UpClientClass, changed),
963
NULL, NULL, g_cclosure_marshal_VOID__VOID,
967
* UpClient::notify-sleep:
968
* @client: the #UpClient instance that emitted the signal
969
* @sleep_kind: the #UpSleepKind
971
* The ::notify-sleep signal is emitted when system sleep is
972
* about to occur. Applications have about 1 second to do
973
* anything they need to do. There is no way to stop the sleep
978
signals [UP_CLIENT_NOTIFY_SLEEP] =
979
g_signal_new ("notify-sleep",
980
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
981
G_STRUCT_OFFSET (UpClientClass, notify_sleep),
982
NULL, NULL, g_cclosure_marshal_VOID__UINT,
983
G_TYPE_NONE, 1, G_TYPE_UINT);
986
* UpClient::notify-resume:
987
* @client: the #UpClient instance that emitted the signal
988
* @sleep_kind: the #UpSleepKind
990
* The ::notify-resume signal is emitted when the system has
995
signals [UP_CLIENT_NOTIFY_RESUME] =
996
g_signal_new ("notify-resume",
997
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
998
G_STRUCT_OFFSET (UpClientClass, notify_resume),
999
NULL, NULL, g_cclosure_marshal_VOID__UINT,
1000
G_TYPE_NONE, 1, G_TYPE_UINT);
1002
g_type_class_add_private (klass, sizeof (UpClientPrivate));
1006
* up_client_enumerate_devices_sync:
1007
* @client: a #UpClient instance.
1008
* @error: a #GError, or %NULL.
1010
* Enumerates all the devices from the daemon.
1012
* Return value: %TRUE for success, else %FALSE.
1017
up_client_enumerate_devices_sync (UpClient *client, GCancellable *cancellable, GError **error)
1019
const gchar *object_path;
1022
gboolean ret = TRUE;
1025
if (client->priv->done_enumerate)
1029
devices = up_client_get_devices_private (client, error);
1030
if (devices == NULL) {
1034
for (i=0; i<devices->len; i++) {
1035
object_path = (const gchar *) g_ptr_array_index (devices, i);
1036
up_client_add (client, object_path);
1039
/* only do this once per instance */
1040
client->priv->done_enumerate = TRUE;
1047
* @client: This class instance
1050
up_client_init (UpClient *client)
1052
GError *error = NULL;
1054
client->priv = UP_CLIENT_GET_PRIVATE (client);
1055
client->priv->array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
1056
client->priv->have_properties = FALSE;
1057
client->priv->done_enumerate = FALSE;
1059
/* get on the bus */
1060
client->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
1061
if (client->priv->bus == NULL) {
1062
g_warning ("Couldn't connect to system bus: %s", error->message);
1063
g_error_free (error);
1067
/* connect to main interface */
1068
client->priv->proxy = dbus_g_proxy_new_for_name (client->priv->bus,
1069
"org.freedesktop.UPower",
1070
"/org/freedesktop/UPower",
1071
"org.freedesktop.UPower");
1072
if (client->priv->proxy == NULL) {
1073
g_warning ("Couldn't connect to proxy");
1077
/* connect to properties interface */
1078
client->priv->prop_proxy = dbus_g_proxy_new_for_name (client->priv->bus,
1079
"org.freedesktop.UPower",
1080
"/org/freedesktop/UPower",
1081
"org.freedesktop.DBus.Properties");
1082
if (client->priv->prop_proxy == NULL) {
1083
g_warning ("Couldn't connect to proxy");
1087
dbus_g_proxy_add_signal (client->priv->proxy, "DeviceAdded", G_TYPE_STRING, G_TYPE_INVALID);
1088
dbus_g_proxy_add_signal (client->priv->proxy, "DeviceRemoved", G_TYPE_STRING, G_TYPE_INVALID);
1089
dbus_g_proxy_add_signal (client->priv->proxy, "DeviceChanged", G_TYPE_STRING, G_TYPE_INVALID);
1090
dbus_g_proxy_add_signal (client->priv->proxy, "Changed", G_TYPE_INVALID);
1091
dbus_g_proxy_add_signal (client->priv->proxy, "NotifySleep", G_TYPE_STRING, G_TYPE_INVALID);
1092
dbus_g_proxy_add_signal (client->priv->proxy, "NotifyResume", G_TYPE_STRING, G_TYPE_INVALID);
1095
dbus_g_proxy_connect_signal (client->priv->proxy, "DeviceAdded",
1096
G_CALLBACK (up_device_added_cb), client, NULL);
1097
dbus_g_proxy_connect_signal (client->priv->proxy, "DeviceRemoved",
1098
G_CALLBACK (up_device_removed_cb), client, NULL);
1099
dbus_g_proxy_connect_signal (client->priv->proxy, "DeviceChanged",
1100
G_CALLBACK (up_device_changed_cb), client, NULL);
1101
dbus_g_proxy_connect_signal (client->priv->proxy, "Changed",
1102
G_CALLBACK (up_client_changed_cb), client, NULL);
1103
dbus_g_proxy_connect_signal (client->priv->proxy, "NotifySleep",
1104
G_CALLBACK (up_client_notify_sleep_cb), client, NULL);
1105
dbus_g_proxy_connect_signal (client->priv->proxy, "NotifyResume",
1106
G_CALLBACK (up_client_notify_resume_cb), client, NULL);
1112
* up_client_finalize:
1115
up_client_finalize (GObject *object)
1119
g_return_if_fail (UP_IS_CLIENT (object));
1121
client = UP_CLIENT (object);
1123
g_ptr_array_unref (client->priv->array);
1125
if (client->priv->bus)
1126
dbus_g_connection_unref (client->priv->bus);
1128
if (client->priv->proxy != NULL)
1129
g_object_unref (client->priv->proxy);
1131
if (client->priv->prop_proxy != NULL)
1132
g_object_unref (client->priv->prop_proxy);
1134
g_free (client->priv->daemon_version);
1136
G_OBJECT_CLASS (up_client_parent_class)->finalize (object);
1142
* Creates a new #UpClient object.
1144
* Return value: a new UpClient object.
1149
up_client_new (void)
1151
if (up_client_object != NULL) {
1152
g_object_ref (up_client_object);
1154
up_client_object = g_object_new (UP_TYPE_CLIENT, NULL);
1155
g_object_add_weak_pointer (up_client_object, &up_client_object);
1157
return UP_CLIENT (up_client_object);