summaryrefslogtreecommitdiff
path: root/src/dkp-source.c
blob: c14ded6e7c30dbbfce8cd421c817fae4a7688b8a (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 David Zeuthen <david@fubar.dk>
 * Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
 *
 * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <string.h>
#include <math.h>

#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gi18n-lib.h>
#include <glib-object.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <devkit-gobject.h>
#include <polkit-dbus/polkit-dbus.h>

#include "sysfs-utils.h"
#include "dkp-debug.h"
#include "dkp-enum.h"
#include "dkp-object.h"
#include "dkp-source.h"
#include "dkp-history.h"
#include "dkp-history-obj.h"
#include "dkp-marshal.h"
#include "dkp-source-glue.h"

#define DK_POWER_MIN_CHARGED_PERCENTAGE	60

struct DkpSourcePrivate
{
	DBusGConnection		*system_bus_connection;
	DBusGProxy		*system_bus_proxy;
	DkpDaemon		*daemon;
	DevkitDevice		*d;
	DkpHistory		*history;
	gchar			*object_path;
	guint			 poll_timer_id;
	DkpObject		*obj;
	gboolean		 has_coldplug_values;
	gdouble			 battery_energy_old;
	GTimeVal		 battery_energy_old_timespec;
};

static void	dkp_source_class_init	(DkpSourceClass	*klass);
static void	dkp_source_init		(DkpSource	*source);
static void	dkp_source_finalize	(GObject	*object);
static void	dkp_source_reset_values	(DkpSource	*source);
static gboolean dkp_source_update	(DkpSource	*source);

enum
{
	PROP_0,
	PROP_NATIVE_PATH,
	PROP_VENDOR,
	PROP_MODEL,
	PROP_SERIAL,
	PROP_UPDATE_TIME,
	PROP_TYPE,
	PROP_LINE_POWER_ONLINE,
	PROP_POWER_SUPPLY,
	PROP_BATTERY_CAPACITY,
	PROP_BATTERY_IS_PRESENT,
	PROP_BATTERY_IS_RECHARGEABLE,
	PROP_BATTERY_STATE,
	PROP_BATTERY_ENERGY,
	PROP_BATTERY_ENERGY_EMPTY,
	PROP_BATTERY_ENERGY_FULL,
	PROP_BATTERY_ENERGY_FULL_DESIGN,
	PROP_BATTERY_ENERGY_RATE,
	PROP_BATTERY_TIME_TO_EMPTY,
	PROP_BATTERY_TIME_TO_FULL,
	PROP_BATTERY_PERCENTAGE,
	PROP_BATTERY_TECHNOLOGY,
};

enum
{
	CHANGED_SIGNAL,
	LAST_SIGNAL,
};

static guint signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE (DkpSource, dkp_source, DKP_SOURCE_TYPE_DEVICE)
#define DKP_SOURCE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DKP_SOURCE_TYPE_SOURCE, DkpSourcePrivate))

static const char	*dkp_source_get_object_path	(DkpDevice *device);
static gboolean		 dkp_source_get_on_battery	(DkpDevice *device, gboolean *on_battery);
static gboolean		 dkp_source_get_low_battery	(DkpDevice *device, gboolean *low_battery);
static void		 dkp_source_removed	 	(DkpDevice *device);
static gboolean		 dkp_source_changed	 	(DkpDevice *device, DevkitDevice *d, gboolean synthesized);

/**
 * dkp_source_error_quark:
 **/
GQuark
dkp_source_error_quark (void)
{
	static GQuark ret = 0;

	if (ret == 0) {
		ret = g_quark_from_static_string ("dkp_source_error");
	}

	return ret;
}

#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }

/**
 * dkp_source_error_get_type:
 **/
GType
dkp_source_error_get_type (void)
{
	static GType etype = 0;

	if (etype == 0)
	{
		static const GEnumValue values[] =
			{
				ENUM_ENTRY (DKP_SOURCE_ERROR_GENERAL, "GeneralError"),
				{ 0, 0, 0 }
			};
		g_assert (DKP_SOURCE_NUM_ERRORS == G_N_ELEMENTS (values) - 1);
		etype = g_enum_register_static ("DkpSourceError", values);
	}
	return etype;
}

/**
 * dkp_source_get_property:
 **/
static void
dkp_source_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
	DkpSource *source = DKP_SOURCE (object);
	DkpObject *obj = source->priv->obj;

	switch (prop_id) {
	case PROP_NATIVE_PATH:
		g_value_set_string (value, obj->native_path);
		break;
	case PROP_VENDOR:
		g_value_set_string (value, obj->vendor);
		break;
	case PROP_MODEL:
		g_value_set_string (value, obj->model);
		break;
	case PROP_SERIAL:
		g_value_set_string (value, obj->serial);
		break;
	case PROP_UPDATE_TIME:
		g_value_set_uint64 (value, obj->update_time);
		break;
	case PROP_TYPE:
		g_value_set_string (value, dkp_source_type_to_text (obj->type));
		break;
	case PROP_POWER_SUPPLY:
		g_value_set_boolean (value, obj->power_supply);
		break;
	case PROP_LINE_POWER_ONLINE:
		g_value_set_boolean (value, obj->line_power_online);
		break;
	case PROP_BATTERY_IS_PRESENT:
		g_value_set_boolean (value, obj->battery_is_present);
		break;
	case PROP_BATTERY_IS_RECHARGEABLE:
		g_value_set_boolean (value, obj->battery_is_rechargeable);
		break;
	case PROP_BATTERY_STATE:
		g_value_set_string (value, dkp_source_state_to_text (obj->battery_state));
		break;
	case PROP_BATTERY_CAPACITY:
		g_value_set_double (value, obj->battery_capacity);
		break;
	case PROP_BATTERY_ENERGY:
		g_value_set_double (value, obj->battery_energy);
		break;
	case PROP_BATTERY_ENERGY_EMPTY:
		g_value_set_double (value, obj->battery_energy_empty);
		break;
	case PROP_BATTERY_ENERGY_FULL:
		g_value_set_double (value, obj->battery_energy_full);
		break;
	case PROP_BATTERY_ENERGY_FULL_DESIGN:
		g_value_set_double (value, obj->battery_energy_full_design);
		break;
	case PROP_BATTERY_ENERGY_RATE:
		g_value_set_double (value, obj->battery_energy_rate);
		break;
	case PROP_BATTERY_TIME_TO_EMPTY:
		g_value_set_int64 (value, obj->battery_time_to_empty);
		break;
	case PROP_BATTERY_TIME_TO_FULL:
		g_value_set_int64 (value, obj->battery_time_to_full);
		break;
	case PROP_BATTERY_PERCENTAGE:
		g_value_set_double (value, obj->battery_percentage);
		break;
	case PROP_BATTERY_TECHNOLOGY:
		g_value_set_string (value, dkp_source_technology_to_text (obj->battery_technology));
		break;

	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

/**
 * dkp_source_class_init:
 **/
static void
dkp_source_class_init (DkpSourceClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	DkpDeviceClass *device_class = DKP_DEVICE_CLASS (klass);

	object_class->finalize = dkp_source_finalize;
	object_class->get_property = dkp_source_get_property;
	device_class->changed = dkp_source_changed;
	device_class->removed = dkp_source_removed;
	device_class->get_object_path = dkp_source_get_object_path;
	device_class->get_on_battery = dkp_source_get_on_battery;
	device_class->get_low_battery = dkp_source_get_low_battery;

	g_type_class_add_private (klass, sizeof (DkpSourcePrivate));

	signals[CHANGED_SIGNAL] =
		g_signal_new ("changed",
			      G_OBJECT_CLASS_TYPE (klass),
			      G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
			      0, NULL, NULL,
			      g_cclosure_marshal_VOID__VOID,
			      G_TYPE_NONE, 0);

	dbus_g_object_type_install_info (DKP_SOURCE_TYPE_SOURCE, &dbus_glib_dkp_source_object_info);

	g_object_class_install_property (
		object_class,
		PROP_NATIVE_PATH,
		g_param_spec_string ("native-path", NULL, NULL, NULL, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_VENDOR,
		g_param_spec_string ("vendor", NULL, NULL, NULL, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_MODEL,
		g_param_spec_string ("model", NULL, NULL, NULL, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_SERIAL,
		g_param_spec_string ("serial", NULL, NULL, NULL, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_UPDATE_TIME,
		g_param_spec_uint64 ("update-time", NULL, NULL, 0, G_MAXUINT64, 0, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_TYPE,
		g_param_spec_string ("type", NULL, NULL, NULL, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_BATTERY_IS_PRESENT,
		g_param_spec_boolean ("power-supply", NULL, NULL, FALSE, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_LINE_POWER_ONLINE,
		g_param_spec_boolean ("line-power-online", NULL, NULL, FALSE, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_BATTERY_ENERGY,
		g_param_spec_double ("battery-energy", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_BATTERY_IS_PRESENT,
		g_param_spec_boolean ("battery-is-present", NULL, NULL, FALSE, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_BATTERY_IS_RECHARGEABLE,
		g_param_spec_boolean ("battery-is-rechargeable", NULL, NULL, FALSE, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_BATTERY_STATE,
		g_param_spec_string ("battery-state", NULL, NULL, NULL, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_BATTERY_CAPACITY,
		g_param_spec_double ("battery-capacity", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_BATTERY_ENERGY_EMPTY,
		g_param_spec_double ("battery-energy-empty", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_BATTERY_ENERGY_FULL,
		g_param_spec_double ("battery-energy-full", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_BATTERY_ENERGY_FULL_DESIGN,
		g_param_spec_double ("battery-energy-full-design", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_BATTERY_ENERGY_RATE,
		g_param_spec_double ("battery-energy-rate", NULL, NULL, -G_MAXDOUBLE, G_MAXDOUBLE, 0, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_BATTERY_TIME_TO_EMPTY,
		g_param_spec_int64 ("battery-time-to-empty", NULL, NULL, -1, G_MAXINT64, -1, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_BATTERY_TIME_TO_FULL,
		g_param_spec_int64 ("battery-time-to-full", NULL, NULL, -1, G_MAXINT64, -1, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_BATTERY_PERCENTAGE,
		g_param_spec_double ("battery-percentage", NULL, NULL, -1, 100, -1, G_PARAM_READABLE));
	g_object_class_install_property (
		object_class,
		PROP_BATTERY_TECHNOLOGY,
		g_param_spec_string ("battery-technology", NULL, NULL, NULL, G_PARAM_READABLE));

	dbus_g_error_domain_register (DKP_SOURCE_ERROR, NULL, DKP_SOURCE_TYPE_ERROR);
}

/**
 * dkp_source_init:
 **/
static void
dkp_source_init (DkpSource *source)
{
	source->priv = DKP_SOURCE_GET_PRIVATE (source);
	dkp_source_reset_values (source);
}

/**
 * dkp_source_finalize:
 **/
static void
dkp_source_finalize (GObject *object)
{
	DkpSource *source;

	g_return_if_fail (object != NULL);
	g_return_if_fail (DKP_IS_SOURCE (object));

	source = DKP_SOURCE (object);
	g_return_if_fail (source->priv != NULL);

	g_object_unref (source->priv->d);
	g_object_unref (source->priv->daemon);
	g_object_unref (source->priv->history);
	dkp_object_free (source->priv->obj);

	if (source->priv->poll_timer_id > 0)
		g_source_remove (source->priv->poll_timer_id);

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

/**
 * dkp_source_compute_object_path_from_basename:
 **/
static char *
dkp_source_compute_object_path_from_basename (const char *native_path_basename)
{
	gchar *basename;
	gchar *object_path;
	unsigned int n;

	/* TODO: need to be more thorough with making proper object
	 * names that won't make D-Bus crash. This is just to cope
	 * with dm-0...
	 */
	basename = g_path_get_basename (native_path_basename);
	for (n = 0; basename[n] != '\0'; n++)
		if (basename[n] == '-')
			basename[n] = '_';
	object_path = g_build_filename ("/sources/", basename, NULL);
	g_free (basename);

	return object_path;
}

/**
 * dkp_source_compute_object_path:
 **/
static gchar *
dkp_source_compute_object_path (const char *native_path)
{
	gchar *basename;
	gchar *object_path;

	basename = g_path_get_basename (native_path);
	object_path = dkp_source_compute_object_path_from_basename (basename);
	g_free (basename);
	return object_path;
}

/**
 * dkp_source_register_source:
 **/
static gboolean
dkp_source_register_source (DkpSource *source)
{
	DBusConnection *connection;
	GError *error = NULL;

	source->priv->system_bus_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
	if (source->priv->system_bus_connection == NULL) {
		if (error != NULL) {
			g_critical ("error getting system bus: %s", error->message);
			g_error_free (error);
		}
		goto error;
	}
	connection = dbus_g_connection_get_connection (source->priv->system_bus_connection);

	source->priv->object_path = dkp_source_compute_object_path (source->priv->obj->native_path);

	dbus_g_connection_register_g_object (source->priv->system_bus_connection,
					     source->priv->object_path, G_OBJECT (source));

	source->priv->system_bus_proxy = dbus_g_proxy_new_for_name (source->priv->system_bus_connection,
								    DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);

	return TRUE;

error:
	return FALSE;
}

/**
 * dkp_source_new:
 **/
DkpSource *
dkp_source_new (DkpDaemon *daemon, DevkitDevice *d)
{
	DkpSource *source;
	const gchar *native_path;
	gchar *id;

	source = NULL;
	native_path = devkit_device_get_native_path (d);

	source = DKP_SOURCE (g_object_new (DKP_SOURCE_TYPE_SOURCE, NULL));
	source->priv->d = g_object_ref (d);
	source->priv->daemon = g_object_ref (daemon);
	source->priv->obj = dkp_object_new ();
	source->priv->obj->native_path = g_strdup (native_path);
	source->priv->history = dkp_history_new ();

	/* detect what kind of device we are */
	if (sysfs_file_exists (native_path, "online")) {
		source->priv->obj->type = DKP_SOURCE_TYPE_LINE_POWER;
	} else {
		/* this is correct, UPS and CSR are not in the kernel */
		source->priv->obj->type = DKP_SOURCE_TYPE_BATTERY;
	}

	/* coldplug */
	if (!dkp_source_update (source)) {
		dkp_warning ("coldplug of %s failed", source->priv->obj->native_path);
		g_object_unref (source);
		source = NULL;
		goto out;
	}

	/* register on the bus */
	if (!dkp_source_register_source (DKP_SOURCE (source))) {
		dkp_warning ("failed to register on the bus");
		g_object_unref (source);
		source = NULL;
		goto out;
	}

	/* get the id so we can load the old history */
	id = dkp_object_get_id (source->priv->obj);
	if (id != NULL)
		dkp_history_set_id (source->priv->history, id);
	g_free (id);

out:
	return source;
}

/**
 * dkp_source_emit_changed:
 **/
static void
dkp_source_emit_changed (DkpSource *source)
{
	dkp_debug ("emitting changed on %s", source->priv->obj->native_path);
	g_signal_emit_by_name (source->priv->daemon, "device-changed",
			       source->priv->object_path, NULL);
	g_signal_emit (source, signals[CHANGED_SIGNAL], 0);
}

/**
 * dkp_source_changed:
 **/
static gboolean
dkp_source_changed (DkpDevice *device, DevkitDevice *d, gboolean synthesized)
{
	DkpSource *source = DKP_SOURCE (device);
	gboolean keep_source;

	g_object_unref (source->priv->d);
	source->priv->d = g_object_ref (d);

	keep_source = dkp_source_update (source);

	/* this 'change' event might prompt us to remove the source */
	if (!keep_source)
		goto out;

	/* no, it's good .. keep it */
	dkp_source_emit_changed (source);

out:
	return keep_source;
}

/**
 * dkp_source_removed:
 **/
void
dkp_source_removed (DkpDevice *device)
{
}

/**
 * dkp_source_get_object_path:
 **/
static const char *
dkp_source_get_object_path (DkpDevice *device)
{
	DkpSource *source = DKP_SOURCE (device);
	return source->priv->object_path;
}

/**
 * dkp_source_update_line_power:
 **/
static gboolean
dkp_source_update_line_power (DkpSource *source)
{
	DkpObject *obj = source->priv->obj;
	DkpObject *obj_old;
	gboolean ret;

	/* make a copy so we can see if anything changed */
	obj_old = dkp_object_copy (obj);

	/* force true */
	obj->power_supply = TRUE;

	/* get new AC value */
	obj->line_power_online = sysfs_get_int (obj->native_path, "online");

	/* initial value */
	if (!source->priv->has_coldplug_values) {
		dkp_object_print (obj);
		source->priv->has_coldplug_values = TRUE;
		goto out;
	}

	/* print difference */
	ret = !dkp_object_equal (obj, obj_old);
	if (ret)
		dkp_object_diff (obj_old, obj);
out:
	dkp_object_free (obj_old);
	return TRUE;
}

/**
 * dkp_source_reset_values:
 **/
static void
dkp_source_reset_values (DkpSource *source)
{
	source->priv->has_coldplug_values = FALSE;
	source->priv->battery_energy_old = -1;
	source->priv->battery_energy_old_timespec.tv_sec = 0;
	dkp_object_clear (source->priv->obj);
}

/**
 * dkp_source_get_on_battery:
 **/
static gboolean
dkp_source_get_on_battery (DkpDevice *device, gboolean *on_battery)
{
	DkpSource *source = DKP_SOURCE (device);

	g_return_val_if_fail (DKP_IS_SOURCE (source), FALSE);
	g_return_val_if_fail (on_battery != NULL, FALSE);

	if (source->priv->obj->type != DKP_SOURCE_TYPE_BATTERY)
		return FALSE;
	if (!source->priv->obj->battery_is_present)
		return FALSE;

	*on_battery = (source->priv->obj->battery_state == DKP_SOURCE_STATE_DISCHARGING);
	return TRUE;
}

/**
 * dkp_source_get_low_battery:
 **/
static gboolean
dkp_source_get_low_battery (DkpDevice *device, gboolean *low_battery)
{
	gboolean ret;
	gboolean on_battery;
	DkpSource *source = DKP_SOURCE (device);

	g_return_val_if_fail (DKP_IS_SOURCE (source), FALSE);
	g_return_val_if_fail (low_battery != NULL, FALSE);

	/* reuse the common checks */
	ret = dkp_source_get_on_battery (device, &on_battery);
	if (!ret)
		return FALSE;

	/* shortcut */
	if (!on_battery) {
		*low_battery = FALSE;
		return TRUE;
	}

	*low_battery = (source->priv->obj->battery_percentage < 10);
	return TRUE;
}

/**
 * dkp_source_calculate_battery_rate:
 **/
static void
dkp_source_calculate_battery_rate (DkpSource *source)
{
	guint time;
	gdouble energy;
	GTimeVal now;
	DkpObject *obj = source->priv->obj;

	if (obj->battery_energy < 0)
		return;

	if (source->priv->battery_energy_old < 0)
		return;

	if (source->priv->battery_energy_old == obj->battery_energy)
		return;

	/* get the time difference */
	g_get_current_time (&now);
	time = now.tv_sec - source->priv->battery_energy_old_timespec.tv_sec;

	if (time == 0)
		return;

	/* get the difference in charge */
	energy = source->priv->battery_energy_old - obj->battery_energy;
	if (energy < 0.1)
		return;

	/* probably okay */
	obj->battery_energy_rate = energy * 3600 / time;
}

/**
 * dkp_source_update_battery:
 *
 * Return value: TRUE if we changed
 **/
static gboolean
dkp_source_update_battery (DkpSource *source)
{
	gchar *status = NULL;
	gboolean ret;
	gboolean just_added = FALSE;
	DkpSourceState battery_state;
	DkpObject *obj = source->priv->obj;
	DkpObject *obj_old;

	/* make a copy so we can see if anything changed */
	obj_old = dkp_object_copy (obj);

	/* have we just been removed? */
	obj->battery_is_present = sysfs_get_bool (obj->native_path, "present");
	if (!obj->battery_is_present) {
		dkp_source_reset_values (source);
		obj->type = DKP_SOURCE_TYPE_BATTERY;
		goto out;
	}

	/* initial values */
	if (!source->priv->has_coldplug_values) {
		gchar *technology_native;

		/* when we add via sysfs power_supply class then we know this is true */
		obj->power_supply = TRUE;

		/* the ACPI spec is bad at defining battery type constants */
		technology_native = g_strstrip (sysfs_get_string (obj->native_path, "technology"));
		obj->battery_technology = dkp_acpi_to_source_technology (technology_native);
		g_free (technology_native);

		obj->vendor = g_strstrip (sysfs_get_string (obj->native_path, "manufacturer"));
		obj->model = g_strstrip (sysfs_get_string (obj->native_path, "model_name"));
		obj->serial = g_strstrip (sysfs_get_string (obj->native_path, "serial_number"));

		/* assume true for laptops */
		obj->battery_is_rechargeable = TRUE;

		/* these don't change at runtime */
		obj->battery_energy_full =
			sysfs_get_double (obj->native_path, "energy_full") / 1000000.0;
		obj->battery_energy_full_design =
			sysfs_get_double (obj->native_path, "energy_full_design") / 1000000.0;

		/* the last full cannot be bigger than the design */
		if (obj->battery_energy_full > obj->battery_energy_full_design)
			obj->battery_energy_full = obj->battery_energy_full_design;

		/* calculate how broken our battery is */
		obj->battery_capacity = obj->battery_energy_full_design / obj->battery_energy_full * 100.0f;
		if (obj->battery_capacity < 0)
			obj->battery_capacity = 0;
		if (obj->battery_capacity > 100.0)
			obj->battery_capacity = 100.0;

		/* we only coldplug once, as these values will never change */
		source->priv->has_coldplug_values = TRUE;
		just_added = TRUE;
	}

	status = g_strstrip (sysfs_get_string (obj->native_path, "status"));
	if (strcasecmp (status, "charging") == 0)
		battery_state = DKP_SOURCE_STATE_CHARGING;
	else if (strcasecmp (status, "discharging") == 0)
		battery_state = DKP_SOURCE_STATE_DISCHARGING;
	else if (strcasecmp (status, "full") == 0)
		battery_state = DKP_SOURCE_STATE_FULLY_CHARGED;
	else if (strcasecmp (status, "empty") == 0)
		battery_state = DKP_SOURCE_STATE_EMPTY;
	else {
		dkp_warning ("unknown status string: %s", status);
		battery_state = DKP_SOURCE_STATE_UNKNOWN;
	}

	/* get the currect charge */
	obj->battery_energy =
		sysfs_get_double (obj->native_path, "energy_avg") / 1000000.0;
	if (obj->battery_energy == 0)
		obj->battery_energy =
			sysfs_get_double (obj->native_path, "energy_now") / 1000000.0;

	/* some batteries don't update last_full attribute */
	if (obj->battery_energy > obj->battery_energy_full)
		obj->battery_energy_full = obj->battery_energy;

	obj->battery_energy_rate =
		fabs (sysfs_get_double (obj->native_path, "current_now") / 1000000.0);

	/* ACPI gives out the special 'Ones' value for rate when it's unable
	 * to calculate the true rate. We should set the rate zero, and wait
	 * for the BIOS to stabilise. */
	if (obj->battery_energy_rate == 0xffff)
		obj->battery_energy_rate = -1;

	/* sanity check to less than 100W */
	if (obj->battery_energy_rate > 100*1000)
		obj->battery_energy_rate = -1;

	/* the hardware reporting failed -- try to calculate this */
	if (obj->battery_energy_rate < 0) {
		dkp_source_calculate_battery_rate (source);
	}

	/* charging has a negative rate */
	if (obj->battery_energy_rate > 0 && battery_state == DKP_SOURCE_STATE_CHARGING)
		obj->battery_energy_rate *= -1.0;

	/* get a precise percentage */
	obj->battery_percentage = 100.0 * obj->battery_energy / obj->battery_energy_full;
	if (obj->battery_percentage < 0)
		obj->battery_percentage = 0;
	if (obj->battery_percentage > 100.0)
		obj->battery_percentage = 100.0;

	/* calculate a quick and dirty time remaining value */
	obj->battery_time_to_empty = -1;
	obj->battery_time_to_full = -1;
	if (obj->battery_energy_rate > 0) {
		if (battery_state == DKP_SOURCE_STATE_DISCHARGING) {
			obj->battery_time_to_empty = 3600 * (obj->battery_energy / obj->battery_energy_rate);
		} else if (battery_state == DKP_SOURCE_STATE_CHARGING) {
			obj->battery_time_to_full = 3600 * ((obj->battery_energy_full - obj->battery_energy) / obj->battery_energy_rate);
		}
	}
	/* check the remaining time is under a set limit, to deal with broken
	   primary batteries rate */
	if (obj->battery_time_to_empty > (100 * 60 * 60))
		obj->battery_time_to_empty = -1;
	if (obj->battery_time_to_full > (100 * 60 * 60))
		obj->battery_time_to_full = -1;

	/* set the old status */
	source->priv->battery_energy_old = obj->battery_energy;
	g_get_current_time (&source->priv->battery_energy_old_timespec);

	/* we changed state */
	if (obj->battery_state != battery_state) {
		source->priv->battery_energy_old = -1;
		obj->battery_state = battery_state;
	}

out:
	/* did anything change? */
	ret = !dkp_object_equal (obj, obj_old);
	if (!just_added && ret)
		dkp_object_diff (obj_old, obj);
	dkp_object_free (obj_old);

	/* just for debugging */
	if (just_added)
		dkp_object_print (obj);

	/* save new history */
	if (ret) {
		dkp_history_set_state (source->priv->history, obj->battery_state);
		dkp_history_set_charge_data (source->priv->history, obj->battery_percentage);
		dkp_history_set_rate_data (source->priv->history, obj->battery_energy_rate);
	}

	g_free (status);
	return ret;
}

/**
 * dkp_source_poll_battery:
 **/
static gboolean
dkp_source_poll_battery (DkpSource *source)
{
	gboolean ret;
	DkpObject *obj = source->priv->obj;

	dkp_debug ("No updates on source %s for 30 seconds; forcing update", obj->native_path);
	source->priv->poll_timer_id = 0;
	ret = dkp_source_update (source);
	if (ret) {
		dkp_source_emit_changed (source);
	}
	return FALSE;
}

/**
 * dkp_source_update:
 *
 * Return value: TRUE if we changed
 **/
static gboolean
dkp_source_update (DkpSource *source)
{
	gboolean ret;
	GTimeVal time;
	DkpObject *obj = source->priv->obj;

	if (source->priv->poll_timer_id > 0) {
		g_source_remove (source->priv->poll_timer_id);
		source->priv->poll_timer_id = 0;
	}

	g_get_current_time (&time);
	obj->update_time = time.tv_sec;

	switch (source->priv->obj->type) {
	case DKP_SOURCE_TYPE_LINE_POWER:
		ret = dkp_source_update_line_power (source);
		break;
	case DKP_SOURCE_TYPE_BATTERY:
		ret = dkp_source_update_battery (source);
		/* Seems that we don't get change uevents from the
		 * kernel on some BIOS types; set up a timer to poll
		 * if we are charging or discharging */
		if (obj->battery_state == DKP_SOURCE_STATE_CHARGING ||
		    obj->battery_state == DKP_SOURCE_STATE_DISCHARGING)
			source->priv->poll_timer_id = g_timeout_add_seconds (30, (GSourceFunc) dkp_source_poll_battery, source);
		break;
	default:
		g_assert_not_reached ();
		break;
	}

	return ret;
}

#define DKP_DBUS_STRUCT_UINT_DOUBLE_STRING (dbus_g_type_get_struct ("GValueArray", \
	G_TYPE_UINT, G_TYPE_DOUBLE, G_TYPE_STRING, G_TYPE_INVALID))

/**
 * dkp_source_get_statistics:
 **/
gboolean
dkp_source_get_statistics (DkpSource *source, const gchar *type, guint timespan, DBusGMethodInvocation *context)
{
	GError *error;
	GPtrArray *array;
	GPtrArray *complex;
	const DkpHistoryObj *obj;
	GValue *value;
	guint i;

	g_return_val_if_fail (DKP_IS_SOURCE (source), FALSE);
	g_return_val_if_fail (type != NULL, FALSE);

	/* get the correct data */
	if (strcmp (type, "rate") == 0)
		array = dkp_history_get_rate_data (source->priv->history, timespan);
	else if (strcmp (type, "charge") == 0)
		array = dkp_history_get_charge_data (source->priv->history, timespan);
	else {
		error = g_error_new (DKP_DAEMON_ERROR, DKP_DAEMON_ERROR_GENERAL, "type '%s' not recognised", type);
		dbus_g_method_return_error (context, error);
		goto out;
	}

	/* maybe the device doesn't support histories */
	if (array == NULL) {
		error = g_error_new (DKP_DAEMON_ERROR, DKP_DAEMON_ERROR_GENERAL, "device has no history");
		dbus_g_method_return_error (context, error);
		goto out;
	}

	/* copy data to dbus struct */
	complex = g_ptr_array_sized_new (array->len);
	for (i=0; i<array->len; i++) {
		obj = (const DkpHistoryObj *) g_ptr_array_index (array, i);
		value = g_new0 (GValue, 1);
		g_value_init (value, DKP_DBUS_STRUCT_UINT_DOUBLE_STRING);
		g_value_take_boxed (value, dbus_g_type_specialized_construct (DKP_DBUS_STRUCT_UINT_DOUBLE_STRING));
		dbus_g_type_struct_set (value, 0, obj->time, 1, obj->value, 2, dkp_source_state_to_text (obj->state), -1);
		g_ptr_array_add (complex, g_value_get_boxed (value));
		g_free (value);
	}

	g_ptr_array_free (array, TRUE);
	dbus_g_method_return (context, complex);
out:
	return TRUE;
}

/**
 * dkp_source_refresh:
 **/
gboolean
dkp_source_refresh (DkpSource *source, DBusGMethodInvocation *context)
{
	g_return_val_if_fail (DKP_IS_SOURCE (source), FALSE);

	dkp_source_update (source);
	dbus_g_method_return (context);
	return TRUE;
}