summaryrefslogtreecommitdiff
path: root/libupower-glib/up-client.c
blob: 2fb5280e53c8d3351adce9d2d23f61c5497736f2 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008-2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * SECTION:up-client
 * @short_description: Main client object for accessing the UPower daemon
 *
 * A helper GObject to use for accessing UPower information, and to be notified
 * when it is changed.
 *
 * See also: #UpDevice
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include <dbus/dbus-glib.h>

#include "up-client.h"
#include "up-device.h"

static void	up_client_class_init	(UpClientClass	*klass);
static void	up_client_init		(UpClient	*client);
static void	up_client_finalize	(GObject	*object);

#define UP_CLIENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_CLIENT, UpClientPrivate))

/**
 * UpClientPrivate:
 *
 * Private #UpClient data
 **/
struct _UpClientPrivate
{
	DBusGConnection		*bus;
	DBusGProxy		*proxy;
	DBusGProxy		*prop_proxy;
	GPtrArray		*array;
	gboolean		 have_properties;
	gchar			*daemon_version;
	gboolean		 can_suspend;
	gboolean		 can_hibernate;
	gboolean		 lid_is_closed;
	gboolean		 on_battery;
	gboolean		 on_low_battery;
	gboolean		 lid_is_present;
	gboolean		 is_docked;
	gboolean		 done_enumerate;
};

enum {
	UP_CLIENT_DEVICE_ADDED,
	UP_CLIENT_DEVICE_CHANGED,
	UP_CLIENT_DEVICE_REMOVED,
	UP_CLIENT_CHANGED,
	UP_CLIENT_LAST_SIGNAL
};

enum {
	PROP_0,
	PROP_DAEMON_VERSION,
	PROP_CAN_SUSPEND,
	PROP_CAN_HIBERNATE,
	PROP_ON_BATTERY,
	PROP_ON_LOW_BATTERY,
	PROP_LID_IS_CLOSED,
	PROP_LID_IS_PRESENT,
	PROP_IS_DOCKED,
	PROP_LAST
};

static guint signals [UP_CLIENT_LAST_SIGNAL] = { 0 };
static gpointer up_client_object = NULL;

G_DEFINE_TYPE (UpClient, up_client, G_TYPE_OBJECT)

/*
 * up_client_get_device:
 */
static UpDevice *
up_client_get_device (UpClient *client, const gchar *object_path)
{
	guint i;
	const gchar *object_path_tmp;
	UpDevice *device;
	UpClientPrivate *priv = client->priv;

	for (i=0; i<priv->array->len; i++) {
		device = g_ptr_array_index (priv->array, i);
		object_path_tmp = up_device_get_object_path (device);
		if (g_strcmp0 (object_path_tmp, object_path) == 0)
			return device;
	}
	return NULL;
}

/**
 * up_client_get_devices:
 * @client: a #UpClient instance.
 *
 * Get a copy of the device objects.
 * You must have called up_client_enumerate_devices_sync() before calling this
 * function.
 *
 * Return value: (transfer full): an array of #UpDevice objects, free with g_ptr_array_unref()
 *
 * Since: 0.9.0
 **/
GPtrArray *
up_client_get_devices (UpClient *client)
{
	g_return_val_if_fail (UP_IS_CLIENT (client), NULL);
	g_return_val_if_fail (client->priv->done_enumerate, NULL);
	return g_ptr_array_ref (client->priv->array);
}

/*
 * up_client_get_devices_private:
 */
static GPtrArray *
up_client_get_devices_private (UpClient *client, GError **error)
{
	gboolean ret;
	GError *error_local = NULL;
	GPtrArray *devices = NULL;
	GType g_type_array;

	if (!client->priv->proxy)
		return NULL;
	g_type_array = dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH);
	ret = dbus_g_proxy_call (client->priv->proxy, "EnumerateDevices", &error_local,
				 G_TYPE_INVALID,
				 g_type_array, &devices,
				 G_TYPE_INVALID);
	if (!ret) {
		g_warning ("Couldn't enumerate devices: %s", error_local->message);
		g_set_error (error, 1, 0, "%s", error_local->message);
		g_error_free (error_local);
	}
	return devices;
}

/**
 * up_client_suspend_sync:
 * @client: a #UpClient instance.
 * @cancellable: a #GCancellable or %NULL
 * @error: a #GError, or %NULL.
 *
 * Puts the computer into a low power state, but state is not preserved if the
 * power is lost.
 *
 * NOTE: The system is still consuming a small amount of power
 *
 * Return value: TRUE if system suspended okay, FALSE other wise.
 *
 * Since: 0.9.0
 **/
gboolean
up_client_suspend_sync (UpClient *client, GCancellable *cancellable, GError **error)
{
	gboolean ret;
	GError *error_local = NULL;

	g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
	g_return_val_if_fail (client->priv->proxy != NULL, FALSE);

	ret = dbus_g_proxy_call (client->priv->proxy, "Suspend", &error_local,
				 G_TYPE_INVALID, G_TYPE_INVALID);
	if (!ret) {
		/* DBus might time out, which is okay */
		if (g_error_matches (error_local, DBUS_GERROR, DBUS_GERROR_NO_REPLY)) {
			g_debug ("DBUS timed out, but recovering");
			ret = TRUE;
			goto out;
		}

		/* an actual error */
		g_warning ("Couldn't suspend: %s", error_local->message);
		g_set_error (error, 1, 0, "%s", error_local->message);
	}
out:
	if (error_local != NULL)
		g_error_free (error_local);
	return ret;
}

/**
 * up_client_hibernate_sync:
 * @client: a #UpClient instance.
 * @cancellable: a #GCancellable or %NULL
 * @error: a #GError.
 *
 * Puts the computer into a low power state, where state is preserved if the
 * power is lost.
 *
 * Return value: TRUE if system suspended okay, FALSE other wise.
 *
 * Since: 0.9.0
 **/
gboolean
up_client_hibernate_sync (UpClient *client, GCancellable *cancellable, GError **error)
{
	gboolean ret;
	GError *error_local = NULL;

	g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
	g_return_val_if_fail (client->priv->proxy != NULL, FALSE);

	ret = dbus_g_proxy_call (client->priv->proxy, "Hibernate", &error_local,
				 G_TYPE_INVALID, G_TYPE_INVALID);
	if (!ret) {
		/* DBus might time out, which is okay */
		if (g_error_matches (error_local, DBUS_GERROR, DBUS_GERROR_NO_REPLY)) {
			g_debug ("DBUS timed out, but recovering");
			ret = TRUE;
			goto out;
		}

		/* an actual error */
		g_warning ("Couldn't hibernate: %s", error_local->message);
		g_set_error (error, 1, 0, "%s", error_local->message);
	}
out:
	if (error_local != NULL)
		g_error_free (error_local);
	return ret;
}

/**
 * up_client_about_to_sleep_sync:
 * @client: a #UpClient instance.
 * @cancellable: a #GCancellable or %NULL
 * @error: a #GError, or %NULL.
 *
 * Tells UPower that we are soon to reqest either Suspend() or Hibernate()
 * and that session and system components should be notified of this.
 *
 * Return value: TRUE if system suspended okay, FALSE other wise.
 *
 * Since: 0.9.1
 **/
gboolean
up_client_about_to_sleep_sync (UpClient *client, GCancellable *cancellable, GError **error)
{
	gboolean ret;
	GError *error_local = NULL;

	g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
	g_return_val_if_fail (client->priv->proxy != NULL, FALSE);

	ret = dbus_g_proxy_call (client->priv->proxy, "AboutToSleep", &error_local,
				 G_TYPE_INVALID, G_TYPE_INVALID);
	if (!ret) {
		/* DBus might time out, which is okay */
		if (g_error_matches (error_local, DBUS_GERROR, DBUS_GERROR_NO_REPLY)) {
			g_debug ("DBUS timed out, but recovering");
			ret = TRUE;
			goto out;
		}

		/* an actual error */
		g_warning ("Couldn't sent that we were about to sleep: %s", error_local->message);
		g_set_error (error, 1, 0, "%s", error_local->message);
	}
out:
	if (error_local != NULL)
		g_error_free (error_local);
	return ret;
}

/**
 * up_client_get_properties_sync:
 * @client: a #UpClient instance.
 * @cancellable: a #GCancellable or %NULL
 * @error: a #GError, or %NULL.
 *
 * Get all the properties from UPower daemon.
 *
 * Return value: %TRUE for success, else %FALSE.
 *
 * Since: 0.9.0
 **/
gboolean
up_client_get_properties_sync (UpClient *client, GCancellable *cancellable, GError **error)
{
	gboolean ret = TRUE;
	gboolean allowed = FALSE;
	GHashTable *props;
	GValue *value;

	props = NULL;

	if (client->priv->have_properties)
		goto out;
	if (!client->priv->prop_proxy)
		goto out;

	error = NULL;
	ret = dbus_g_proxy_call (client->priv->prop_proxy, "GetAll", error,
				 G_TYPE_STRING, "org.freedesktop.UPower",
				 G_TYPE_INVALID,
				 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), &props,
				 G_TYPE_INVALID);
	if (!ret)
		goto out;

	value = g_hash_table_lookup (props, "DaemonVersion");
	if (value == NULL) {
		g_warning ("No 'DaemonVersion' property");
		goto out;
	}
	client->priv->daemon_version = g_strdup (g_value_get_string (value));

	value = g_hash_table_lookup (props, "CanSuspend");
	if (value == NULL) {
		g_warning ("No 'CanSuspend' property");
		goto out;
	}
	
	ret = dbus_g_proxy_call (client->priv->proxy, "SuspendAllowed", error,
		G_TYPE_INVALID, G_TYPE_BOOLEAN, &allowed, G_TYPE_INVALID);
	if (!ret)
		goto out;

	ret = g_value_get_boolean (value) && allowed;
	if (ret != client->priv->can_suspend) {
		client->priv->can_suspend = ret;
		g_object_notify (G_OBJECT(client), "can-suspend");
	}

	value = g_hash_table_lookup (props, "CanHibernate");
	if (value == NULL) {
		g_warning ("No 'CanHibernate' property");
		goto out;
	}
	ret = dbus_g_proxy_call (client->priv->proxy, "HibernateAllowed", error,
		G_TYPE_INVALID, G_TYPE_BOOLEAN, &allowed, G_TYPE_INVALID);
	if (!ret)
		goto out;

	ret = g_value_get_boolean (value) && allowed;
	if (ret != client->priv->can_hibernate) {
		client->priv->can_hibernate = ret;
		g_object_notify (G_OBJECT(client), "can-hibernate");
	}

	value = g_hash_table_lookup (props, "LidIsClosed");
	if (value == NULL) {
		g_warning ("No 'LidIsClosed' property");
		goto out;
	}
	ret = g_value_get_boolean (value);
	if (ret != client->priv->lid_is_closed) {
		client->priv->lid_is_closed = ret;
		g_object_notify (G_OBJECT(client), "lid-is-closed");
	}

	value = g_hash_table_lookup (props, "OnBattery");
	if (value == NULL) {
		g_warning ("No 'OnBattery' property");
		goto out;
	}
	ret = g_value_get_boolean (value);
	if (ret != client->priv->on_battery) {
		client->priv->on_battery = ret;
		g_object_notify (G_OBJECT(client), "on-battery");
	}

	value = g_hash_table_lookup (props, "OnLowBattery");
	if (value == NULL) {
		g_warning ("No 'OnLowBattery' property");
		goto out;
	}
	ret = g_value_get_boolean (value);
	if (ret != client->priv->on_low_battery) {
		client->priv->on_low_battery = ret;
		g_object_notify (G_OBJECT(client), "on-low-battery");
	}

	value = g_hash_table_lookup (props, "LidIsPresent");
	if (value == NULL) {
		g_warning ("No 'LidIsPresent' property");
		goto out;
	}
	ret = g_value_get_boolean (value);
	if (ret != client->priv->lid_is_present) {
		client->priv->lid_is_present = ret;
		g_object_notify (G_OBJECT(client), "lid-is-present");
	}

	value = g_hash_table_lookup (props, "IsDocked");
	if (value == NULL) {
		g_warning ("No 'IsDocked' property");
		goto out;
	}
	ret = g_value_get_boolean (value);
	if (ret != client->priv->is_docked) {
		client->priv->is_docked = ret;
		g_object_notify (G_OBJECT(client), "is-docked");
	}

	/* cached */
	client->priv->have_properties = TRUE;

out:
	if (props != NULL)
		g_hash_table_unref (props);
	return ret;
}

/**
 * up_client_get_daemon_version:
 * @client: a #UpClient instance.
 *
 * Get UPower daemon version.
 *
 * Return value: string containing the daemon version, e.g. 008
 *
 * Since: 0.9.0
 **/
const gchar *
up_client_get_daemon_version (UpClient *client)
{
	g_return_val_if_fail (UP_IS_CLIENT (client), NULL);
	up_client_get_properties_sync (client, NULL, NULL);
	return client->priv->daemon_version;
}

/**
 * up_client_get_can_hibernate:
 * @client: a #UpClient instance.
 *
 * Get whether the system is able to hibernate.
 *
 * Return value: TRUE if system can hibernate, FALSE other wise.
 *
 * Since: 0.9.0
 **/
gboolean
up_client_get_can_hibernate (UpClient *client)
{
	g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
	up_client_get_properties_sync (client, NULL, NULL);
	return client->priv->can_hibernate;
}

/**
 * up_client_get_lid_is_closed:
 * @client: a #UpClient instance.
 *
 * Get whether the laptop lid is closed.
 *
 * Return value: %TRUE if lid is closed or %FALSE otherwise.
 *
 * Since: 0.9.0
 */
gboolean
up_client_get_lid_is_closed (UpClient *client)
{
	g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
	up_client_get_properties_sync (client, NULL, NULL);
	return client->priv->lid_is_closed;
}

/**
 * up_client_get_lid_is_present:
 * @client: a #UpClient instance.
 *
 * Get whether a laptop lid is present on this machine.
 *
 * Return value: %TRUE if the machine has a laptop lid
 *
 * Since: 0.9.2
 */
gboolean
up_client_get_lid_is_present (UpClient *client)
{
	g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
	up_client_get_properties_sync (client, NULL, NULL);
	return client->priv->lid_is_present;
}

/**
 * up_client_get_is_docked:
 * @client: a #UpClient instance.
 *
 * Get whether the machine is docked into a docking station.
 *
 * Return value: %TRUE if the machine is docked
 *
 * Since: 0.9.2
 */
gboolean
up_client_get_is_docked (UpClient *client)
{
	g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
	up_client_get_properties_sync (client, NULL, NULL);
	return client->priv->is_docked;
}

/**
 * up_client_get_can_suspend:
 * @client: a #UpClient instance.
 *
 * Get whether the system is able to suspend.
 *
 * Return value: TRUE if system can suspend, FALSE other wise.
 *
 * Since: 0.9.0
 **/
gboolean
up_client_get_can_suspend (UpClient *client)
{
	g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
	up_client_get_properties_sync (client, NULL, NULL);
	return client->priv->can_suspend;
}

/**
 * up_client_get_on_battery:
 * @client: a #UpClient instance.
 *
 * Get whether the system is running on battery power.
 *
 * Return value: TRUE if the system is currently running on battery, FALSE other wise.
 *
 * Since: 0.9.0
 **/
gboolean
up_client_get_on_battery (UpClient *client)
{
	g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
	up_client_get_properties_sync (client, NULL, NULL);
	return client->priv->on_battery;
}

/**
 * up_client_get_on_low_battery:
 * @client: a #UpClient instance.
 *
 * Get whether the system is running on low battery power.
 *
 * Return value: TRUE if the system is currently on low battery power, FALSE other wise.
 *
 * Since: 0.9.0
 **/
gboolean
up_client_get_on_low_battery (UpClient *client)
{
	g_return_val_if_fail (UP_IS_CLIENT (client), FALSE);
	up_client_get_properties_sync (client, NULL, NULL);
	return client->priv->on_low_battery;
}

/*
 * up_client_add:
 */
static void
up_client_add (UpClient *client, const gchar *object_path)
{
	UpDevice *device = NULL;
	UpDevice *device_tmp;
	gboolean ret;

	/* check existing list for this object path */
	device_tmp = up_client_get_device (client, object_path);
	if (device_tmp != NULL) {
		g_warning ("already added: %s", object_path);
		goto out;
	}

	/* create new device */
	device = up_device_new ();
	ret = up_device_set_object_path_sync (device, object_path, NULL, NULL);
	if (!ret)
		goto out;

	/* add to array */
	g_ptr_array_add (client->priv->array, g_object_ref (device));
	g_signal_emit (client, signals [UP_CLIENT_DEVICE_ADDED], 0, device);
out:
	if (device != NULL)
		g_object_unref (device);
}

/*
 * up_client_added_cb:
 */
static void
up_device_added_cb (DBusGProxy *proxy, const gchar *object_path, UpClient *client)
{
	up_client_add (client, object_path);
}

/*
 * up_client_changed_cb:
 */
static void
up_device_changed_cb (DBusGProxy *proxy, const gchar *object_path, UpClient *client)
{
	UpDevice *device;
	device = up_client_get_device (client, object_path);
	if (device != NULL)
		g_signal_emit (client, signals [UP_CLIENT_DEVICE_CHANGED], 0, device);
}

/*
 * up_client_removed_cb:
 */
static void
up_device_removed_cb (DBusGProxy *proxy, const gchar *object_path, UpClient *client)
{
	UpDevice *device;
	device = up_client_get_device (client, object_path);
	if (device != NULL) {
		g_signal_emit (client, signals [UP_CLIENT_DEVICE_REMOVED], 0, device);
		g_ptr_array_remove (client->priv->array, device);
	}
}

/*
 * up_client_changed_cb:
 */
static void
up_client_changed_cb (DBusGProxy *proxy, UpClient *client)
{
	client->priv->have_properties = FALSE;
	g_signal_emit (client, signals [UP_CLIENT_CHANGED], 0);
}

static void
up_client_get_property (GObject *object,
			 guint prop_id,
			 GValue *value,
			 GParamSpec *pspec)
{
	UpClient *client;
	client = UP_CLIENT (object);

	up_client_get_properties_sync (client, NULL, NULL);

	switch (prop_id) {
	case PROP_DAEMON_VERSION:
		g_value_set_string (value, client->priv->daemon_version);
		break;
	case PROP_CAN_SUSPEND:
		g_value_set_boolean (value, client->priv->can_suspend);
		break;
	case PROP_CAN_HIBERNATE:
		g_value_set_boolean (value, client->priv->can_hibernate);
		break;
	case PROP_ON_BATTERY:
		g_value_set_boolean (value, client->priv->on_battery);
		break;
	case PROP_ON_LOW_BATTERY:
		g_value_set_boolean (value, client->priv->on_low_battery);
		break;
	case PROP_LID_IS_CLOSED:
		g_value_set_boolean (value, client->priv->lid_is_closed);
		break;
	case PROP_LID_IS_PRESENT:
		g_value_set_boolean (value, client->priv->lid_is_present);
		break;
	case PROP_IS_DOCKED:
		g_value_set_boolean (value, client->priv->is_docked);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

/*
 * up_client_class_init:
 * @klass: The UpClientClass
 */
static void
up_client_class_init (UpClientClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->get_property = up_client_get_property;
	object_class->finalize = up_client_finalize;

	/**
	 * UpClient:daemon-version:
	 *
	 * The daemon version.
	 *
	 * Since: 0.9.0
	 */
	g_object_class_install_property (object_class,
					 PROP_DAEMON_VERSION,
					 g_param_spec_string ("daemon-version",
							      "Daemon version",
							      NULL,
							      NULL,
							      G_PARAM_READABLE));
	/**
	 * UpClient:can-suspend:
	 *
	 * If the computer can suspend.
	 *
	 * Since: 0.9.0
	 */
	g_object_class_install_property (object_class,
					 PROP_CAN_SUSPEND,
					 g_param_spec_boolean ("can-suspend",
							       "If the computer can suspend",
							       NULL,
							       FALSE,
							       G_PARAM_READABLE));
	/**
	 * UpClient:can-hibernate:
	 *
	 * If the computer can hibernate.
	 *
	 * Since: 0.9.0
	 */
	g_object_class_install_property (object_class,
					 PROP_CAN_HIBERNATE,
					 g_param_spec_boolean ("can-hibernate",
							       "If the computer can hibernate",
							       NULL,
							       FALSE,
							       G_PARAM_READABLE));
	/**
	 * UpClient:on-battery:
	 *
	 * If the computer is on battery power.
	 *
	 * Since: 0.9.0
	 */
	g_object_class_install_property (object_class,
					 PROP_ON_BATTERY,
					 g_param_spec_boolean ("on-battery",
							       "If the computer is on battery power",
							       NULL,
							       FALSE,
							       G_PARAM_READABLE));
	/**
	 * UpClient:on-low-battery:
	 *
	 * If the computer is on low battery power.
	 *
	 * Since: 0.9.0
	 */
	g_object_class_install_property (object_class,
					 PROP_ON_LOW_BATTERY,
					 g_param_spec_boolean ("on-low-battery",
							       "If the computer is on low battery power",
							       NULL,
							       FALSE,
							       G_PARAM_READABLE));
	/**
	 * UpClient:lid-is-closed:
	 *
	 * If the laptop lid is closed.
	 *
	 * Since: 0.9.0
	 */
	g_object_class_install_property (object_class,
					 PROP_LID_IS_CLOSED,
					 g_param_spec_boolean ("lid-is-closed",
							       "If the laptop lid is closed",
							       NULL,
							       FALSE,
							       G_PARAM_READABLE));
	/**
	 * UpClient:lid-is-present:
	 *
	 * If a laptop lid is present.
	 *
	 * Since: 0.9.0
	 */
	g_object_class_install_property (object_class,
					 PROP_LID_IS_PRESENT,
					 g_param_spec_boolean ("lid-is-present",
							       "If a laptop lid is present",
							       NULL,
							       FALSE,
							       G_PARAM_READABLE));

	/**
	 * UpClient:is-docked:
	 *
	 * If the laptop is docked
	 *
	 * Since: 0.9.8
	 */
	g_object_class_install_property (object_class,
					 PROP_IS_DOCKED,
					 g_param_spec_boolean ("is-docked",
							       "If a laptop is docked",
							       NULL,
							       FALSE,
							       G_PARAM_READABLE));

	/**
	 * UpClient::device-added:
	 * @client: the #UpClient instance that emitted the signal
	 * @device: the #UpDevice that was added.
	 *
	 * The ::device-added signal is emitted when a power device is added.
	 *
	 * Since: 0.9.0
	 **/
	signals [UP_CLIENT_DEVICE_ADDED] =
		g_signal_new ("device-added",
			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (UpClientClass, device_added),
			      NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
			      G_TYPE_NONE, 1, UP_TYPE_DEVICE);

	/**
	 * UpClient::device-removed:
	 * @client: the #UpClient instance that emitted the signal
	 * @device: the #UpDevice that was removed.
	 *
	 * The ::device-added signal is emitted when a power device is removed.
	 *
	 * Since: 0.9.0
	 **/
	signals [UP_CLIENT_DEVICE_REMOVED] =
		g_signal_new ("device-removed",
			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (UpClientClass, device_removed),
			      NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
			      G_TYPE_NONE, 1, UP_TYPE_DEVICE);

	/**
	 * UpClient::device-changed:
	 * @client: the #UpClient instance that emitted the signal
	 * @device: the #UpDevice that was changed.
	 *
	 * The ::device-changed signal is emitted when a power device is changed.
	 *
	 * Since: 0.9.0
	 **/
	signals [UP_CLIENT_DEVICE_CHANGED] =
		g_signal_new ("device-changed",
			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (UpClientClass, device_changed),
			      NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
			      G_TYPE_NONE, 1, UP_TYPE_DEVICE);

	/**
	 * UpClient::changed:
	 * @client: the #UpDevice instance that emitted the signal
	 *
	 * The ::changed signal is emitted when properties may have changed.
	 *
	 * Since: 0.9.0
	 **/
	signals [UP_CLIENT_CHANGED] =
		g_signal_new ("changed",
			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (UpClientClass, changed),
			      NULL, NULL, g_cclosure_marshal_VOID__VOID,
			      G_TYPE_NONE, 0);

	g_type_class_add_private (klass, sizeof (UpClientPrivate));
}

/**
 * up_client_enumerate_devices_sync:
 * @client: a #UpClient instance.
 * @error: a #GError, or %NULL.
 *
 * Enumerates all the devices from the daemon.
 *
 * Return value: %TRUE for success, else %FALSE.
 *
 * Since: 0.9.0
 **/
gboolean
up_client_enumerate_devices_sync (UpClient *client, GCancellable *cancellable, GError **error)
{
	const gchar *object_path;
	GPtrArray *devices;
	guint i;
	gboolean ret = TRUE;

	/* already done */
	if (client->priv->done_enumerate)
		goto out;

	/* coldplug */
	devices = up_client_get_devices_private (client, error);
	if (devices == NULL) {
		ret = FALSE;
		goto out;
	}
	for (i=0; i<devices->len; i++) {
		object_path = (const gchar *) g_ptr_array_index (devices, i);
		up_client_add (client, object_path);
	}

	/* only do this once per instance */
	client->priv->done_enumerate = TRUE;
out:
	return ret;
}

/*
 * up_client_init:
 * @client: This class instance
 */
static void
up_client_init (UpClient *client)
{
	GError *error = NULL;

	client->priv = UP_CLIENT_GET_PRIVATE (client);
	client->priv->array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
	client->priv->have_properties = FALSE;
	client->priv->done_enumerate = FALSE;

	/* get on the bus */
	client->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
	if (client->priv->bus == NULL) {
		g_warning ("Couldn't connect to system bus: %s", error->message);
		g_error_free (error);
		goto out;
	}

	/* connect to main interface */
	client->priv->proxy = dbus_g_proxy_new_for_name (client->priv->bus,
							 "org.freedesktop.UPower",
							 "/org/freedesktop/UPower",
							 "org.freedesktop.UPower");
	if (client->priv->proxy == NULL) {
		g_warning ("Couldn't connect to proxy");
		goto out;
	}

	/* connect to properties interface */
	client->priv->prop_proxy = dbus_g_proxy_new_for_name (client->priv->bus,
							      "org.freedesktop.UPower",
							      "/org/freedesktop/UPower",
							      "org.freedesktop.DBus.Properties");
	if (client->priv->prop_proxy == NULL) {
		g_warning ("Couldn't connect to proxy");
		goto out;
	}

	dbus_g_proxy_add_signal (client->priv->proxy, "DeviceAdded", G_TYPE_STRING, G_TYPE_INVALID);
	dbus_g_proxy_add_signal (client->priv->proxy, "DeviceRemoved", G_TYPE_STRING, G_TYPE_INVALID);
	dbus_g_proxy_add_signal (client->priv->proxy, "DeviceChanged", G_TYPE_STRING, G_TYPE_INVALID);
	dbus_g_proxy_add_signal (client->priv->proxy, "Changed", G_TYPE_INVALID);

	/* all callbacks */
	dbus_g_proxy_connect_signal (client->priv->proxy, "DeviceAdded",
				     G_CALLBACK (up_device_added_cb), client, NULL);
	dbus_g_proxy_connect_signal (client->priv->proxy, "DeviceRemoved",
				     G_CALLBACK (up_device_removed_cb), client, NULL);
	dbus_g_proxy_connect_signal (client->priv->proxy, "DeviceChanged",
				     G_CALLBACK (up_device_changed_cb), client, NULL);
	dbus_g_proxy_connect_signal (client->priv->proxy, "Changed",
				     G_CALLBACK (up_client_changed_cb), client, NULL);
out:
	return;
}

/*
 * up_client_finalize:
 */
static void
up_client_finalize (GObject *object)
{
	UpClient *client;

	g_return_if_fail (UP_IS_CLIENT (object));

	client = UP_CLIENT (object);

	g_ptr_array_unref (client->priv->array);

	if (client->priv->bus)
		dbus_g_connection_unref (client->priv->bus);

	if (client->priv->proxy != NULL)
		g_object_unref (client->priv->proxy);

	if (client->priv->prop_proxy != NULL)
		g_object_unref (client->priv->prop_proxy);

	g_free (client->priv->daemon_version);

	G_OBJECT_CLASS (up_client_parent_class)->finalize (object);
}

/**
 * up_client_new:
 *
 * Creates a new #UpClient object.
 *
 * Return value: a new UpClient object.
 *
 * Since: 0.9.0
 **/
UpClient *
up_client_new (void)
{
	if (up_client_object != NULL) {
		g_object_ref (up_client_object);
	} else {
		up_client_object = g_object_new (UP_TYPE_CLIENT, NULL);
		g_object_add_weak_pointer (up_client_object, &up_client_object);
	}
	return UP_CLIENT (up_client_object);
}