summaryrefslogtreecommitdiff
path: root/src/dkp-history.c
blob: 75fc26c42684bfaa317137171bcc7ffcfdd8589a (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <glib/gi18n.h>
#include <gio/gio.h>

#include "egg-debug.h"
#include "dkp-history.h"
#include "dkp-stats-obj.h"
#include "dkp-history-obj.h"

static void	dkp_history_class_init	(DkpHistoryClass	*klass);
static void	dkp_history_init	(DkpHistory		*history);
static void	dkp_history_finalize	(GObject		*object);

#define DKP_HISTORY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DKP_TYPE_HISTORY, DkpHistoryPrivate))

#define DKP_HISTORY_SAVE_INTERVAL	5 /* seconds */

struct DkpHistoryPrivate
{
	gchar			*id;
	gdouble			 rate_last;
	gint64			 time_full_last;
	gint64			 time_empty_last;
	gdouble			 percentage_last;
	DkpDeviceState		 state;
	EggObjList		*data_rate;
	EggObjList		*data_charge;
	EggObjList		*data_time_full;
	EggObjList		*data_time_empty;
	guint			 save_id;
};

enum {
	DKP_HISTORY_PROGRESS,
	DKP_HISTORY_LAST_SIGNAL
};

G_DEFINE_TYPE (DkpHistory, dkp_history, G_TYPE_OBJECT)
#define DKP_HISTORY_FILE_HEADER	"PackageKit Profile"

/**
 * dkp_history_get_history_list:
 **/
static EggObjList *
dkp_history_new_history_list (void)
{
	EggObjList *list;
	list = egg_obj_list_new ();
	egg_obj_list_set_new (list, (EggObjListNewFunc) dkp_history_obj_new);
	egg_obj_list_set_copy (list, (EggObjListCopyFunc) dkp_history_obj_copy);
	egg_obj_list_set_free (list, (EggObjListFreeFunc) dkp_history_obj_free);
	egg_obj_list_set_to_string (list, (EggObjListToStringFunc) dkp_history_obj_to_string);
	egg_obj_list_set_from_string (list, (EggObjListFromStringFunc) dkp_history_obj_from_string);
	return list;
}

/**
 * dkp_history_get_stats_list:
 **/
static EggObjList *
dkp_history_new_stats_list (void)
{
	EggObjList *list;
	list = egg_obj_list_new ();
	egg_obj_list_set_new (list, (EggObjListNewFunc) dkp_stats_obj_new);
	egg_obj_list_set_copy (list, (EggObjListCopyFunc) dkp_stats_obj_copy);
	egg_obj_list_set_free (list, (EggObjListFreeFunc) dkp_stats_obj_free);
	egg_obj_list_set_to_string (list, (EggObjListToStringFunc) dkp_stats_obj_to_string);
	egg_obj_list_set_from_string (list, (EggObjListFromStringFunc) dkp_stats_obj_from_string);
	return list;
}

/**
 * dkp_history_array_limit_x_size:
 * @array: The data we have for a specific graph
 * @max_num: The max desired points
 *
 * We need to reduce the number of data points else the graph will take a long
 * time to plot accuracy we don't need at the larger scales.
 * This will not reduce the scale or range of the data.
 **/
static gboolean
dkp_history_array_limit_x_size (EggObjList *array, guint max_num)
{
	const DkpHistoryObj *obj;
	gfloat div;
	gfloat running_count = 0.0f;
	guint length;
	guint a;
	guint last;
	guint first;

	egg_debug ("length of array (before) %i", array->len);

	/* sanity check */
	length = array->len;
	if (length < max_num) {
		egg_debug ("no limit possible as under limit");
		return FALSE;
	}

	/* last element */
	obj = (const DkpHistoryObj *) egg_obj_list_index (array, length-1);
	last = obj->time;
	obj = (const DkpHistoryObj *) egg_obj_list_index (array, 0);
	first = obj->time;

	div = (first - last) / (gfloat) max_num;
	egg_debug ("Using a x division of %f", div);

	/* Reduces the number of points to a pre-set level using a time
	 * division algorithm so we don't keep diluting the previous
	 * data with a conventional 1-in-x type algorithm. */
	for (a=0; a < length; a++) {
		obj = (const DkpHistoryObj *) egg_obj_list_index (array, a);
		if (obj->time - last >= running_count) {
			/* keep valid obj */
			running_count = running_count + div;
		} else {
			/* remove obj */
			egg_obj_list_remove (array, (const gpointer) obj);

			/* decrement the array length as we removed a obj */
			length--;
			/* re-evaluate the 'current' item */
			a--;
		}
	}

	/* check length */
	egg_debug ("length of array (after) %i", array->len);

	return TRUE;
}

/**
 * dkp_history_copy_array_timespan:
 **/
static EggObjList *
dkp_history_copy_array_timespan (const EggObjList *array, guint timespan)
{
	guint i;
	const DkpHistoryObj *obj;
	EggObjList *array_new;
	guint start;

	/* no data */
	if (array->len == 0)
		return NULL;

	/* new data */
	array_new = dkp_history_new_history_list ();

	/* treat the timespan like a range, and search backwards */
	obj = (const DkpHistoryObj *) egg_obj_list_index (array, array->len-1);
	start = obj->time;
	for (i=array->len-1; i>0; i--) {
		obj = (const DkpHistoryObj *) egg_obj_list_index (array, i);
		if (start - obj->time < timespan)
			egg_obj_list_add (array_new, (const gpointer) obj);
	}

	/* FIXME: only add a certain number of points */
	if (0) dkp_history_array_limit_x_size (array_new, 100);

	return array_new;
}

/**
 * dkp_history_get_data:
 **/
EggObjList *
dkp_history_get_data (DkpHistory *history, DkpHistoryType type, guint timespan)
{
	EggObjList *array;
	const EggObjList *array_data = NULL;

	g_return_val_if_fail (DKP_IS_HISTORY (history), NULL);

	if (history->priv->id == NULL)
		return NULL;

	if (type == DKP_HISTORY_TYPE_CHARGE)
		array_data = history->priv->data_charge;
	else if (type == DKP_HISTORY_TYPE_RATE)
		array_data = history->priv->data_rate;
	else if (type == DKP_HISTORY_TYPE_TIME_FULL)
		array_data = history->priv->data_time_full;
	else if (type == DKP_HISTORY_TYPE_TIME_EMPTY)
		array_data = history->priv->data_time_empty;

	/* not recognised */
	if (array_data == NULL)
		return NULL;

	array = dkp_history_copy_array_timespan (array_data, timespan);
	return array;
}

/**
 * dkp_history_get_profile_data:
 **/
EggObjList *
dkp_history_get_profile_data (DkpHistory *history, gboolean charging)
{
	guint i;
	guint non_zero_accuracy = 0;
	gfloat average;
	guint bin;
	guint oldbin = 999;
	const DkpHistoryObj *obj_last = NULL;
	const DkpHistoryObj *obj;
	const DkpHistoryObj *obj_old = NULL;
	DkpStatsObj *stats;
	EggObjList *array;
	EggObjList *data;
	guint time;
	gdouble value;
	gdouble total_value = 0.0f;

	g_return_val_if_fail (DKP_IS_HISTORY (history), NULL);

	/* create 100 item list and set to zero */
	data = dkp_history_new_stats_list ();
	for (i=0; i<101; i++) {
		stats = dkp_stats_obj_create (0.0f, 0.0f);
		egg_obj_list_add (data, stats);
		dkp_stats_obj_free (stats);
	}

	array = history->priv->data_charge;
	for (i=0; i<array->len; i++) {
		obj = (const DkpHistoryObj *) egg_obj_list_index (array, i);
		if (obj_last == NULL || obj->state != obj_last->state) {
//			egg_debug ("ignoring %i as not the same as prev", obj->time);
			obj_old = NULL;
			goto cont;
		}

		/* round to the nearest int */
		bin = rint (obj->value);
//		egg_debug ("data %f binned into %i", obj->value, bin);
		if (oldbin != bin) {
//			egg_debug ("bin changed!, %i->%i", oldbin, bin);
			oldbin = bin;
			if (obj_old != NULL) {
				/* not enough or too much difference */
				value = fabs (obj->value - obj_old->value);
				if (value < 0.01f) {
//					egg_debug ("ignoring as value difference too small: %f", value);
					obj_old = NULL;
					goto cont;
				}
				if (value > 3.0f) {
//					egg_debug ("ignoring as value difference too large: %f", value);
					obj_old = NULL;
					goto cont;
				}

				time = obj->time - obj_old->time;
//				egg_debug ("time difference at %i is %i", bin, time);
				/* use the accuracy field as a counter for now */
				if ((charging && obj->state == DKP_DEVICE_STATE_CHARGING) ||
				    (!charging && obj->state == DKP_DEVICE_STATE_DISCHARGING)) {
					stats = (DkpStatsObj *) egg_obj_list_index (data, bin);
					stats->value += time;
					stats->accuracy++;
				}
			}
			obj_old = obj;
		}
cont:
		obj_last = obj;
	}

	/* divide the value by the number of samples to make the average */
	for (i=0; i<101; i++) {
		stats = (DkpStatsObj *) egg_obj_list_index (data, i);
		if (stats->accuracy != 0)
			stats->value = stats->value / stats->accuracy;
	}

	/* find non-zero accuracy values for the average */
	for (i=0; i<101; i++) {
		stats = (DkpStatsObj *) egg_obj_list_index (data, i);
		if (stats->accuracy > 0) {
			total_value += stats->value;
			non_zero_accuracy++;
		}
	}

	/* average */
	average = total_value / non_zero_accuracy;
	egg_debug ("average is %f", average);

	/* make the values a factor of 0, so that 1.0 is twice the
	 * average, and -1.0 is half the average */
	for (i=0; i<101; i++) {
		stats = (DkpStatsObj *) egg_obj_list_index (data, i);
		if (stats->accuracy > 0)
			stats->value = (stats->value - average) / average;
		else
			stats->value = 0.0f;
	}

	/* accuracy is a percentage scale, where each cycle = 20% */
	for (i=0; i<101; i++) {
		stats = (DkpStatsObj *) egg_obj_list_index (data, i);
		stats->accuracy *= 20;
		if (stats->accuracy > 100.0f)
			stats->accuracy = 100.0f;
	}

	return data;
}

/**
 * dkp_history_get_filename:
 **/
static gchar *
dkp_history_get_filename (DkpHistory *history, const gchar *type)
{
	gchar *path;
	gchar *filename;

	filename = g_strdup_printf ("history-%s-%s.dat", type, history->priv->id);
	path = g_build_filename (PACKAGE_LOCALSTATE_DIR, "lib", "DeviceKit-power", filename, NULL);
	g_free (filename);
	return path;
}

/**
 * dkp_history_save_data:
 **/
static gboolean
dkp_history_save_data (DkpHistory *history)
{
	gchar *filename;

	/* we have an ID? */
	if (history->priv->id == NULL) {
		egg_warning ("no ID, cannot save");
		return FALSE;
	}

	/* save rate history to disk */
	filename = dkp_history_get_filename (history, "rate");
	egg_obj_list_to_file (history->priv->data_rate, filename);
	g_free (filename);

	/* save charge history to disk */
	filename = dkp_history_get_filename (history, "charge");
	egg_obj_list_to_file (history->priv->data_charge, filename);
	g_free (filename);

	/* save charge history to disk */
	filename = dkp_history_get_filename (history, "time-full");
	egg_obj_list_to_file (history->priv->data_time_full, filename);
	g_free (filename);

	/* save charge history to disk */
	filename = dkp_history_get_filename (history, "time-empty");
	egg_obj_list_to_file (history->priv->data_time_empty, filename);
	g_free (filename);

	return TRUE;
}

/**
 * dkp_history_schedule_save_cb:
 **/
static gboolean
dkp_history_schedule_save_cb (DkpHistory *history)
{
	dkp_history_save_data (history);
	return FALSE;
}

/**
 * dkp_history_is_low_power:
 **/
static gboolean
dkp_history_is_low_power (DkpHistory *history)
{
	guint length;
	const DkpHistoryObj *obj;

	/* current status is always up to date */
	if (history->priv->state != DKP_DEVICE_STATE_DISCHARGING)
		return FALSE;

	/* have we got any data? */
	length = history->priv->data_charge->len;
	if (length == 0)
		return FALSE;

	/* get the last saved charge object */
	obj = (const DkpHistoryObj *) egg_obj_list_index (history->priv->data_charge, length-1);
	if (obj->state != DKP_DEVICE_STATE_DISCHARGING)
		return FALSE;

	/* high enough */
	if (obj->value > 10)
		return FALSE;

	/* we are low power */
	return TRUE;
}

/**
 * dkp_history_schedule_save:
 **/
static gboolean
dkp_history_schedule_save (DkpHistory *history)
{
	gboolean ret;

	/* if low power, then don't batch up save requests */
	ret = dkp_history_is_low_power (history);
	if (ret) {
		dkp_history_save_data (history);
		return TRUE;
	}

	/* we already have one saved, cancel and reschedule */
	if (history->priv->save_id != 0) {
		egg_debug ("deferring as others queued");
		g_source_remove (history->priv->save_id);
		history->priv->save_id = g_timeout_add_seconds (DKP_HISTORY_SAVE_INTERVAL,
								(GSourceFunc) dkp_history_schedule_save_cb, history);
		return TRUE;
	}

	/* nothing scheduled, do new */
	egg_debug ("saving in %i seconds", DKP_HISTORY_SAVE_INTERVAL);
	history->priv->save_id = g_timeout_add_seconds (DKP_HISTORY_SAVE_INTERVAL,
							(GSourceFunc) dkp_history_schedule_save_cb, history);

	return TRUE;
}

/**
 * dkp_history_load_data:
 **/
static gboolean
dkp_history_load_data (DkpHistory *history)
{
	gchar *filename;
	DkpHistoryObj *obj;

	/* load rate history from disk */
	filename = dkp_history_get_filename (history, "rate");
	egg_obj_list_from_file (history->priv->data_rate, filename);
	g_free (filename);

	/* load charge history from disk */
	filename = dkp_history_get_filename (history, "charge");
	egg_obj_list_from_file (history->priv->data_charge, filename);
	g_free (filename);

	/* load charge history from disk */
	filename = dkp_history_get_filename (history, "time-full");
	egg_obj_list_from_file (history->priv->data_time_full, filename);
	g_free (filename);

	/* load charge history from disk */
	filename = dkp_history_get_filename (history, "time-empty");
	egg_obj_list_from_file (history->priv->data_time_empty, filename);
	g_free (filename);

	/* save a marker so we don't use incomplete percentages */
	obj = dkp_history_obj_create (0, DKP_DEVICE_STATE_UNKNOWN);
	egg_obj_list_add (history->priv->data_rate, obj);
	egg_obj_list_add (history->priv->data_charge, obj);
	egg_obj_list_add (history->priv->data_time_full, obj);
	egg_obj_list_add (history->priv->data_time_empty, obj);
	dkp_history_obj_free (obj);
	dkp_history_schedule_save (history);

	return TRUE;
}

/**
 * dkp_history_set_id:
 **/
gboolean
dkp_history_set_id (DkpHistory *history, const gchar *id)
{
	gboolean ret;

	g_return_val_if_fail (DKP_IS_HISTORY (history), FALSE);

	if (history->priv->id != NULL)
		return FALSE;
	if (id == NULL)
		return FALSE;

	egg_debug ("using id: %s", id);
	history->priv->id = g_strdup (id);
	/* load all previous data */
	ret = dkp_history_load_data (history);
	return ret;
}

/**
 * dkp_history_set_state:
 **/
gboolean
dkp_history_set_state (DkpHistory *history, DkpDeviceState state)
{
	g_return_val_if_fail (DKP_IS_HISTORY (history), FALSE);

	if (history->priv->id == NULL)
		return FALSE;
	history->priv->state = state;
	return TRUE;
}

/**
 * dkp_history_set_charge_data:
 **/
gboolean
dkp_history_set_charge_data (DkpHistory *history, gdouble percentage)
{
	DkpHistoryObj *obj;

	g_return_val_if_fail (DKP_IS_HISTORY (history), FALSE);

	if (history->priv->id == NULL)
		return FALSE;
	if (history->priv->state == DKP_DEVICE_STATE_UNKNOWN)
		return FALSE;
	if (history->priv->percentage_last == percentage)
		return FALSE;

	/* add to array and schedule save file */
	obj = dkp_history_obj_create (percentage, history->priv->state);
	egg_obj_list_add (history->priv->data_charge, obj);
	dkp_history_obj_free (obj);
	dkp_history_schedule_save (history);

	/* save last value */
	history->priv->percentage_last = percentage;

	return TRUE;
}

/**
 * dkp_history_set_rate_data:
 **/
gboolean
dkp_history_set_rate_data (DkpHistory *history, gdouble rate)
{
	DkpHistoryObj *obj;

	g_return_val_if_fail (DKP_IS_HISTORY (history), FALSE);

	if (history->priv->id == NULL)
		return FALSE;
	if (history->priv->state == DKP_DEVICE_STATE_UNKNOWN)
		return FALSE;
	if (history->priv->rate_last == rate)
		return FALSE;

	/* add to array and schedule save file */
	obj = dkp_history_obj_create (rate, history->priv->state);
	egg_obj_list_add (history->priv->data_rate, obj);
	dkp_history_obj_free (obj);
	dkp_history_schedule_save (history);

	/* save last value */
	history->priv->rate_last = rate;

	return TRUE;
}

/**
 * dkp_history_set_time_full_data:
 **/
gboolean
dkp_history_set_time_full_data (DkpHistory *history, gint64 time)
{
	DkpHistoryObj *obj;

	g_return_val_if_fail (DKP_IS_HISTORY (history), FALSE);

	if (history->priv->id == NULL)
		return FALSE;
	if (history->priv->state == DKP_DEVICE_STATE_UNKNOWN)
		return FALSE;
	if (time < 0)
		return FALSE;
	if (history->priv->time_full_last == time)
		return FALSE;

	/* add to array and schedule save file */
	obj = dkp_history_obj_create ((gdouble) time, history->priv->state);
	egg_obj_list_add (history->priv->data_time_full, obj);
	dkp_history_obj_free (obj);
	dkp_history_schedule_save (history);

	/* save last value */
	history->priv->time_full_last = time;

	return TRUE;
}

/**
 * dkp_history_set_time_empty_data:
 **/
gboolean
dkp_history_set_time_empty_data (DkpHistory *history, gint64 time)
{
	DkpHistoryObj *obj;

	g_return_val_if_fail (DKP_IS_HISTORY (history), FALSE);

	if (history->priv->id == NULL)
		return FALSE;
	if (history->priv->state == DKP_DEVICE_STATE_UNKNOWN)
		return FALSE;
	if (time < 0)
		return FALSE;
	if (history->priv->time_empty_last == time)
		return FALSE;

	/* add to array and schedule save file */
	obj = dkp_history_obj_create ((gdouble) time, history->priv->state);
	egg_obj_list_add (history->priv->data_time_empty, obj);
	dkp_history_obj_free (obj);
	dkp_history_schedule_save (history);

	/* save last value */
	history->priv->time_empty_last = time;

	return TRUE;
}

/**
 * dkp_history_class_init:
 * @klass: The DkpHistoryClass
 **/
static void
dkp_history_class_init (DkpHistoryClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = dkp_history_finalize;
	g_type_class_add_private (klass, sizeof (DkpHistoryPrivate));
}

/**
 * dkp_history_init:
 * @history: This class instance
 **/
static void
dkp_history_init (DkpHistory *history)
{
	history->priv = DKP_HISTORY_GET_PRIVATE (history);
	history->priv->id = NULL;
	history->priv->rate_last = 0;
	history->priv->percentage_last = 0;
	history->priv->state = DKP_DEVICE_STATE_UNKNOWN;
	history->priv->data_rate = dkp_history_new_history_list ();
	history->priv->data_charge = dkp_history_new_history_list ();
	history->priv->data_time_full = dkp_history_new_history_list ();
	history->priv->data_time_empty = dkp_history_new_history_list ();
	history->priv->save_id = 0;
}

/**
 * dkp_history_finalize:
 * @object: The object to finalize
 **/
static void
dkp_history_finalize (GObject *object)
{
	DkpHistory *history;

	g_return_if_fail (DKP_IS_HISTORY (object));

	history = DKP_HISTORY (object);

	/* save */
	dkp_history_save_data (history);

	g_object_unref (history->priv->data_rate);
	g_object_unref (history->priv->data_charge);
	g_object_unref (history->priv->data_time_full);
	g_object_unref (history->priv->data_time_empty);
	g_free (history->priv->id);

	g_return_if_fail (history->priv != NULL);

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

/**
 * dkp_history_new:
 *
 * Return value: a new DkpHistory object.
 **/
DkpHistory *
dkp_history_new (void)
{
	DkpHistory *history;
	history = g_object_new (DKP_TYPE_HISTORY, NULL);
	return DKP_HISTORY (history);
}