summaryrefslogtreecommitdiff
path: root/libdevkit-power/dkp-history-obj.c
blob: 5727cb93f9b5ff55a3ffa913b6b48d2df25b03e4 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * 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
 *
 */

#include <glib.h>
#include <glib-object.h>
#include <string.h>
#include <stdlib.h>

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

/**
 * dkp_history_obj_clear_internal:
 **/
static void
dkp_history_obj_clear_internal (DkpHistoryObj *obj)
{
	obj->time = 0;
	obj->value = 0.0f;
	obj->state = 0;
}

/**
 * dkp_history_obj_copy:
 **/
DkpHistoryObj *
dkp_history_obj_copy (const DkpHistoryObj *cobj)
{
	DkpHistoryObj *obj;
	obj = g_new0 (DkpHistoryObj, 1);
	obj->time = cobj->time;
	obj->value = cobj->value;
	obj->state = cobj->state;
	return obj;
}

/**
 * dkp_history_obj_equal:
 **/
gboolean
dkp_history_obj_equal (const DkpHistoryObj *obj1, const DkpHistoryObj *obj2)
{
	if (obj1->time == obj2->time &&
	    obj1->value == obj2->value &&
	    obj1->state == obj2->state)
		return TRUE;
	return FALSE;
}

/**
 * dkp_history_obj_print:
 **/
gboolean
dkp_history_obj_print (const DkpHistoryObj *obj)
{
	g_print ("%i\t%.3f\t%s", obj->time, obj->value, dkp_device_state_to_text (obj->state));
	return TRUE;
}

/**
 * dkp_history_obj_new:
 **/
DkpHistoryObj *
dkp_history_obj_new (void)
{
	DkpHistoryObj *obj;
	obj = g_new0 (DkpHistoryObj, 1);
	dkp_history_obj_clear_internal (obj);
	return obj;
}

/**
 * dkp_history_obj_clear:
 **/
gboolean
dkp_history_obj_clear (DkpHistoryObj *obj)
{
	if (obj == NULL)
		return FALSE;
	dkp_history_obj_free (obj);
	dkp_history_obj_clear_internal (obj);
	return TRUE;
}

/**
 * dkp_history_obj_free:
 **/
gboolean
dkp_history_obj_free (DkpHistoryObj *obj)
{
	if (obj == NULL)
		return FALSE;
	g_free (obj);
	return TRUE;
}

/**
 * dkp_history_obj_create:
 **/
DkpHistoryObj *
dkp_history_obj_create (gdouble value, DkpDeviceState state)
{
	DkpHistoryObj *obj;
	GTimeVal timeval;

	g_get_current_time (&timeval);
	obj = dkp_history_obj_new ();
	obj->time = timeval.tv_sec;
	obj->value = value;
	obj->state = state;
	return obj;
}

/**
 * dkp_history_obj_from_string:
 **/
DkpHistoryObj *
dkp_history_obj_from_string (const gchar *text)
{
	DkpHistoryObj *obj = NULL;
	gchar **parts = NULL;
	guint length;

	if (text == NULL)
		goto out;

	/* split by tab */
	parts = g_strsplit (text, "\t", 0);
	length = g_strv_length (parts);
	if (length != 3) {
		egg_warning ("invalid string: '%s'", text);
		goto out;
	}

	/* parse and create */
	obj = dkp_history_obj_new ();
	obj->time = atoi (parts[0]);
	obj->value = atof (parts[1]);
	obj->state = dkp_device_state_from_text (parts[2]);
out:
	g_strfreev (parts);
	return obj;
}

/**
 * dkp_history_obj_to_string:
 **/
gchar *
dkp_history_obj_to_string (const DkpHistoryObj *obj)
{
	if (obj == NULL)
		return NULL;
	return g_strdup_printf ("%i\t%.3f\t%s", obj->time, obj->value, dkp_device_state_to_text (obj->state));
}