aboutsummaryrefslogtreecommitdiff
path: root/src/device.c
blob: 80b3208276b44975b0d9f75f855eb5b18a02e8a6 (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
/*

A simple Device structure used internally by indicator-power

Copyright 2012 Canonical Ltd.

Authors:
    Charles Kerr <charles.kerr@canonical.com>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 3.0 as published by the Free Software Foundation.

This library 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 version 3.0 for more details.

You should have received a copy of the GNU General Public
License along with this library. If not, see
<http://www.gnu.org/licenses/>.
*/

#include "device.h"

struct _IndicatorPowerDevicePrivate
{
	UpDeviceKind kind;
	UpDeviceState state;
	gchar * object_path;
	gchar * icon;
	gdouble percentage;
	time_t time;
};

#define INDICATOR_POWER_DEVICE_GET_PRIVATE(o) (INDICATOR_POWER_DEVICE(o)->priv)

/* Properties */
/* Enum for the properties so that they can be quickly found and looked up. */
enum {
	PROP_0,
	PROP_KIND,
	PROP_STATE,
	PROP_OBJECT_PATH,
	PROP_ICON,
	PROP_PERCENTAGE,
	PROP_TIME
};

/* GObject stuff */
static void indicator_power_device_class_init (IndicatorPowerDeviceClass *klass);
static void indicator_power_device_init       (IndicatorPowerDevice *self);
static void indicator_power_device_dispose    (GObject *object);
static void indicator_power_device_finalize   (GObject *object);
static void set_property (GObject*, guint prop_id, const GValue*, GParamSpec* );
static void get_property (GObject*, guint prop_id,       GValue*, GParamSpec* );

G_DEFINE_TYPE (IndicatorPowerDevice, indicator_power_device, G_TYPE_OBJECT);

static void
indicator_power_device_class_init (IndicatorPowerDeviceClass *klass)
{
	GParamSpec * pspec;
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (klass, sizeof (IndicatorPowerDevicePrivate));

	object_class->dispose = indicator_power_device_dispose;
	object_class->finalize = indicator_power_device_finalize;
	object_class->set_property = set_property;
	object_class->get_property = get_property;

	pspec = g_param_spec_int (INDICATOR_POWER_DEVICE_KIND, "kind", "The device's UpDeviceKind",
                                  UP_DEVICE_KIND_UNKNOWN, UP_DEVICE_KIND_LAST, UP_DEVICE_KIND_UNKNOWN,
			          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
	g_object_class_install_property (object_class, PROP_KIND, pspec);

	pspec = g_param_spec_int (INDICATOR_POWER_DEVICE_STATE, "state", "The device's UpDeviceState",
                                  UP_DEVICE_STATE_UNKNOWN, UP_DEVICE_STATE_LAST, UP_DEVICE_STATE_UNKNOWN,
			          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
	g_object_class_install_property (object_class, PROP_STATE, pspec);

	pspec = g_param_spec_string (INDICATOR_POWER_DEVICE_OBJECT_PATH, "object path", "The device's DBus object path", NULL,
			             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
	g_object_class_install_property (object_class, PROP_OBJECT_PATH, pspec);

	pspec = g_param_spec_string (INDICATOR_POWER_DEVICE_ICON, "icon", "The device's icon", NULL,
			             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
	g_object_class_install_property (object_class, PROP_ICON, pspec);

	pspec = g_param_spec_double (INDICATOR_POWER_DEVICE_PERCENTAGE, "percentage", "percent charged",
                                     0.0, 100.0, 0.0,
			             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
	g_object_class_install_property (object_class, PROP_PERCENTAGE, pspec);

	pspec = g_param_spec_uint64 (INDICATOR_POWER_DEVICE_TIME, "time", "time left",
                                     0, G_MAXUINT64, 0,
			             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
	g_object_class_install_property (object_class, PROP_TIME, pspec);
}

/* Initialize an instance */
static void
indicator_power_device_init (IndicatorPowerDevice *self)
{
	IndicatorPowerDevicePrivate * priv;

        priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
                                            INDICATOR_POWER_DEVICE_TYPE,
                                            IndicatorPowerDevicePrivate);
        priv->kind = UP_DEVICE_KIND_UNKNOWN;
        priv->state = UP_DEVICE_STATE_UNKNOWN;
        priv->object_path = NULL;
        priv->icon = NULL;
        priv->percentage = 0.0;
        priv->time = 0;

	self->priv = priv;
}

static void
indicator_power_device_dispose (GObject *object)
{
	G_OBJECT_CLASS (indicator_power_device_parent_class)->dispose (object);
}

static void
indicator_power_device_finalize (GObject *object)
{
	IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(object);
	IndicatorPowerDevicePrivate * priv = self->priv;

	//g_clear_pointer (&priv->object_path, g_free);
	//g_clear_pointer (&priv->icon, g_free);
	g_free (priv->object_path);
	priv->object_path = NULL;
	g_free (priv->icon);
	priv->icon = NULL;
}

/***
****
***/

static void
get_property (GObject * o, guint  prop_id, GValue * value, GParamSpec * pspec)
{
        IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(o);
        IndicatorPowerDevicePrivate * priv = self->priv;

        switch (prop_id)
	{
		case PROP_KIND:
			g_value_set_int (value, priv->kind);
			break;

		case PROP_STATE:
			g_value_set_int (value, priv->state);
			break;

		case PROP_OBJECT_PATH:
			g_value_set_string (value, priv->object_path);
			break;

		case PROP_ICON:
			g_value_set_string (value, priv->icon);
			break;

		case PROP_PERCENTAGE:
			g_value_set_double (value, priv->percentage);
			break;

		case PROP_TIME:
			g_value_set_uint64 (value, priv->time);
			break;

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

static void
set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * pspec)
{
        IndicatorPowerDevice * self = INDICATOR_POWER_DEVICE(o);
        IndicatorPowerDevicePrivate * priv = self->priv;

        switch (prop_id)
	{
		case PROP_KIND:
			priv->kind = g_value_get_int (value);
			break;

		case PROP_STATE:
			priv->state = g_value_get_int (value);
			break;

		case PROP_OBJECT_PATH:
			g_free (priv->object_path);
			priv->object_path = g_value_dup_string (value);
			break;

		case PROP_ICON:
			g_free (priv->icon);
			priv->icon = g_value_dup_string (value);
			break;

		case PROP_PERCENTAGE:
			priv->percentage = g_value_get_double (value);
			break;

		case PROP_TIME:
			priv->time = g_value_get_uint64(value);
			break;

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

/***
****
***/

UpDeviceKind
indicator_power_device_get_kind  (const IndicatorPowerDevice * device)
{
	g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN);

        return device->priv->kind;
}

UpDeviceState
indicator_power_device_get_state (const IndicatorPowerDevice * device)
{
	g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_STATE_UNKNOWN);

        return device->priv->state;
}

const gchar *
indicator_power_device_get_object_path (const IndicatorPowerDevice * device)
{
	g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN);

        return device->priv->object_path;
}

const gchar *
indicator_power_device_get_icon (const IndicatorPowerDevice * device)
{
	g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN);

        return device->priv->icon;
}

gdouble
indicator_power_device_get_percentage (const IndicatorPowerDevice * device)
{
	g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN);

        return device->priv->percentage;
}

time_t
indicator_power_device_get_time (const IndicatorPowerDevice * device)
{
	g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), UP_DEVICE_KIND_UNKNOWN);

        return device->priv->time;
}

/***
****
***/

IndicatorPowerDevice *
indicator_power_device_new (const gchar * object_path,
                            UpDeviceKind  kind,
                            const gchar * icon_path,
                            gdouble percentage,
                            UpDeviceState state,
                            time_t timestamp)
{
	GObject * o = g_object_new (INDICATOR_POWER_DEVICE_TYPE,
	                            INDICATOR_POWER_DEVICE_KIND, kind,
	                            INDICATOR_POWER_DEVICE_STATE, state,
	                            INDICATOR_POWER_DEVICE_OBJECT_PATH, object_path,
	                            INDICATOR_POWER_DEVICE_ICON, icon_path,
	                            INDICATOR_POWER_DEVICE_PERCENTAGE, percentage,
	                            INDICATOR_POWER_DEVICE_TIME, (guint64)timestamp,
	                            NULL);
	return INDICATOR_POWER_DEVICE(o);
}

IndicatorPowerDevice *
indicator_power_device_new_from_variant (GVariant * v)
{
	UpDeviceKind kind = UP_DEVICE_KIND_UNKNOWN;
	UpDeviceState state = UP_DEVICE_STATE_UNKNOWN;
	const gchar * icon = NULL;
	const gchar * object_path = NULL;
	gdouble percentage = 0;
	guint64 time = 0;

	g_variant_get (v, "(&su&sdut)",
	               &object_path,
	               &kind,
	               &icon,
	               &percentage,
	               &state,
	               &time);

	return indicator_power_device_new (object_path,
	                                   kind,
	                                   icon,
	                                   percentage,
	                                   state,
	                                   time);
}