summaryrefslogtreecommitdiff
path: root/libupower-glib/up-types.c
blob: 7cf9573146588c7eae6ac35b73f9b726e345a065 (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
/* -*- 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * SECTION:up-types
 * @short_description: Types used by UPower and libupower-glib
 * @see_also: #UpClient, #UpDevice
 *
 * These helper functions provide a way to marshal enumerated values to
 * text and back again.
 */

#include "config.h"

#include <glib.h>

#include "up-types.h"

/**
 * up_device_kind_to_string:
 *
 * Converts a #UpDeviceKind to a string.
 *
 * Return value: identifier string
 *
 * Since: 0.9.0
 **/
const gchar *
up_device_kind_to_string (UpDeviceKind type_enum)
{
	switch (type_enum) {
	case UP_DEVICE_KIND_LINE_POWER:
		return "line-power";
	case UP_DEVICE_KIND_BATTERY:
		return "battery";
	case UP_DEVICE_KIND_UPS:
		return "ups";
	case UP_DEVICE_KIND_MONITOR:
		return "monitor";
	case UP_DEVICE_KIND_MOUSE:
		return "mouse";
	case UP_DEVICE_KIND_KEYBOARD:
		return "keyboard";
	case UP_DEVICE_KIND_PDA:
		return "pda";
	case UP_DEVICE_KIND_PHONE:
		return "phone";
	case UP_DEVICE_KIND_MEDIA_PLAYER:
		return "media-player";
	case UP_DEVICE_KIND_TABLET:
		return "tablet";
	case UP_DEVICE_KIND_COMPUTER:
		return "computer";
	case UP_DEVICE_KIND_GAMING_INPUT:
		return "gaming-input";
	case UP_DEVICE_KIND_PEN:
		return "pen";
	default:
		return "unknown";
	}
	g_assert_not_reached ();
}

/**
 * up_device_kind_from_string:
 *
 * Converts a string to a #UpDeviceKind.
 *
 * Return value: enumerated value
 *
 * Since: 0.9.0
 **/
UpDeviceKind
up_device_kind_from_string (const gchar *type)
{
	if (type == NULL)
		return UP_DEVICE_KIND_UNKNOWN;
	if (g_str_equal (type, "line-power"))
		return UP_DEVICE_KIND_LINE_POWER;
	if (g_str_equal (type, "battery"))
		return UP_DEVICE_KIND_BATTERY;
	if (g_str_equal (type, "ups"))
		return UP_DEVICE_KIND_UPS;
	if (g_str_equal (type, "monitor"))
		return UP_DEVICE_KIND_MONITOR;
	if (g_str_equal (type, "mouse"))
		return UP_DEVICE_KIND_MOUSE;
	if (g_str_equal (type, "keyboard"))
		return UP_DEVICE_KIND_KEYBOARD;
	if (g_str_equal (type, "pda"))
		return UP_DEVICE_KIND_PDA;
	if (g_str_equal (type, "phone"))
		return UP_DEVICE_KIND_PHONE;
	if (g_str_equal (type, "media-player"))
		return UP_DEVICE_KIND_MEDIA_PLAYER;
	if (g_str_equal (type, "tablet"))
		return UP_DEVICE_KIND_TABLET;
	if (g_str_equal (type, "gaming-input"))
		return UP_DEVICE_KIND_GAMING_INPUT;
	if (g_str_equal (type, "pen"))
		return UP_DEVICE_KIND_PEN;
	return UP_DEVICE_KIND_UNKNOWN;
}

/**
 * up_device_state_to_string:
 *
 * Converts a #UpDeviceState to a string.
 *
 * Return value: identifier string
 *
 * Since: 0.9.0
 **/
const gchar *
up_device_state_to_string (UpDeviceState state_enum)
{
	switch (state_enum) {
	case UP_DEVICE_STATE_CHARGING:
		return "charging";
	case UP_DEVICE_STATE_DISCHARGING:
		return "discharging";
	case UP_DEVICE_STATE_EMPTY:
		return "empty";
	case UP_DEVICE_STATE_FULLY_CHARGED:
		return "fully-charged";
	case UP_DEVICE_STATE_PENDING_CHARGE:
		return "pending-charge";
	case UP_DEVICE_STATE_PENDING_DISCHARGE:
		return "pending-discharge";
	default:
		return "unknown";
	}
	g_assert_not_reached ();
}

/**
 * up_device_state_from_string:
 *
 * Converts a string to a #UpDeviceState.
 *
 * Return value: enumerated value
 *
 * Since: 0.9.0
 **/
UpDeviceState
up_device_state_from_string (const gchar *state)
{
	if (state == NULL)
		return UP_DEVICE_STATE_UNKNOWN;
	if (g_str_equal (state, "charging"))
		return UP_DEVICE_STATE_CHARGING;
	if (g_str_equal (state, "discharging"))
		return UP_DEVICE_STATE_DISCHARGING;
	if (g_str_equal (state, "empty"))
		return UP_DEVICE_STATE_EMPTY;
	if (g_str_equal (state, "fully-charged"))
		return UP_DEVICE_STATE_FULLY_CHARGED;
	if (g_str_equal (state, "pending-charge"))
		return UP_DEVICE_STATE_PENDING_CHARGE;
	if (g_str_equal (state, "pending-discharge"))
		return UP_DEVICE_STATE_PENDING_DISCHARGE;
	return UP_DEVICE_STATE_UNKNOWN;
}

/**
 * up_device_technology_to_string:
 *
 * Converts a #UpDeviceTechnology to a string.
 *
 * Return value: identifier string
 *
 * Since: 0.9.0
 **/
const gchar *
up_device_technology_to_string (UpDeviceTechnology technology_enum)
{
	switch (technology_enum) {
	case UP_DEVICE_TECHNOLOGY_LITHIUM_ION:
		return "lithium-ion";
	case UP_DEVICE_TECHNOLOGY_LITHIUM_POLYMER:
		return "lithium-polymer";
	case UP_DEVICE_TECHNOLOGY_LITHIUM_IRON_PHOSPHATE:
		return "lithium-iron-phosphate";
	case UP_DEVICE_TECHNOLOGY_LEAD_ACID:
		return "lead-acid";
	case UP_DEVICE_TECHNOLOGY_NICKEL_CADMIUM:
		return "nickel-cadmium";
	case UP_DEVICE_TECHNOLOGY_NICKEL_METAL_HYDRIDE:
		return "nickel-metal-hydride";
	default:
		return "unknown";
	}
	g_assert_not_reached ();
}

/**
 * up_device_technology_from_string:
 *
 * Converts a string to a #UpDeviceTechnology.
 *
 * Return value: enumerated value
 *
 * Since: 0.9.0
 **/
UpDeviceTechnology
up_device_technology_from_string (const gchar *technology)
{
	if (technology == NULL)
		return UP_DEVICE_TECHNOLOGY_UNKNOWN;
	if (g_str_equal (technology, "lithium-ion"))
		return UP_DEVICE_TECHNOLOGY_LITHIUM_ION;
	if (g_str_equal (technology, "lithium-polymer"))
		return UP_DEVICE_TECHNOLOGY_LITHIUM_POLYMER;
	if (g_str_equal (technology, "lithium-iron-phosphate"))
		return UP_DEVICE_TECHNOLOGY_LITHIUM_IRON_PHOSPHATE;
	if (g_str_equal (technology, "lead-acid"))
		return UP_DEVICE_TECHNOLOGY_LEAD_ACID;
	if (g_str_equal (technology, "nickel-cadmium"))
		return UP_DEVICE_TECHNOLOGY_NICKEL_CADMIUM;
	if (g_str_equal (technology, "nickel-metal-hydride"))
		return UP_DEVICE_TECHNOLOGY_NICKEL_METAL_HYDRIDE;
	return UP_DEVICE_TECHNOLOGY_UNKNOWN;
}

/**
 * up_device_level_to_string:
 *
 * Converts a #UpDeviceLevel to a string.
 *
 * Return value: identifier string
 *
 * Since: 1.0
 **/
const gchar *
up_device_level_to_string (UpDeviceLevel level_enum)
{
	switch (level_enum) {
	case UP_DEVICE_LEVEL_UNKNOWN:
		return "unknown";
	case UP_DEVICE_LEVEL_NONE:
		return "none";
	case UP_DEVICE_LEVEL_DISCHARGING:
		return "discharging";
	case UP_DEVICE_LEVEL_LOW:
		return "low";
	case UP_DEVICE_LEVEL_CRITICAL:
		return "critical";
	case UP_DEVICE_LEVEL_ACTION:
		return "action";
	case UP_DEVICE_LEVEL_NORMAL:
		return "normal";
	case UP_DEVICE_LEVEL_HIGH:
		return "high";
	case UP_DEVICE_LEVEL_FULL:
		return "full";
	default:
		return "unknown";
	}
	g_assert_not_reached ();
}

/**
 * up_device_level_from_string:
 *
 * Converts a string to a #UpDeviceLevel.
 *
 * Return value: enumerated value
 *
 * Since: 1.0
 **/
UpDeviceLevel
up_device_level_from_string (const gchar *level)
{
	if (level == NULL)
		return UP_DEVICE_LEVEL_UNKNOWN;
	if (g_str_equal (level, "unknown"))
		return UP_DEVICE_LEVEL_UNKNOWN;
	if (g_str_equal (level, "none"))
		return UP_DEVICE_LEVEL_NONE;
	if (g_str_equal (level, "discharging"))
		return UP_DEVICE_LEVEL_DISCHARGING;
	if (g_str_equal (level, "low"))
		return UP_DEVICE_LEVEL_LOW;
	if (g_str_equal (level, "critical"))
		return UP_DEVICE_LEVEL_CRITICAL;
	if (g_str_equal (level, "action"))
		return UP_DEVICE_LEVEL_ACTION;
	if (g_str_equal (level, "normal"))
		return UP_DEVICE_LEVEL_NORMAL;
	if (g_str_equal (level, "high"))
		return UP_DEVICE_LEVEL_HIGH;
	if (g_str_equal (level, "full"))
		return UP_DEVICE_LEVEL_FULL;
	return UP_DEVICE_LEVEL_UNKNOWN;
}