forked from nemomobile/mce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmce-conf.c
363 lines (309 loc) · 8.22 KB
/
mce-conf.c
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
/**
* @file mce-conf.c
* Configuration option handling for MCE
* <p>
* Copyright © 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
* <p>
* @author David Weinehall <[email protected]>
*
* mce is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* mce 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mce. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include "mce.h"
#include "mce-conf.h"
#include "mce-log.h" /* mce_log(), LL_* */
/** Pointer to the keyfile structure where config values are read from */
static gpointer keyfile = NULL;
/**
* Get a boolean configuration value
*
* @param group The configuration group to get the value from
* @param key The configuration key to get the value of
* @param defaultval The default value to use if the key isn't set
* @param keyfileptr A keyfile pointer, or NULL to use the default keyfile
* @return The configuration value on success, the default value on failure
*/
gboolean mce_conf_get_bool(const gchar *group, const gchar *key,
const gboolean defaultval, gpointer keyfileptr)
{
gboolean tmp = FALSE;
GError *error = NULL;
if (keyfileptr == NULL) {
if (keyfile == NULL) {
mce_log(LL_ERR,
"Could not get config key %s/%s; "
"no configuration file initialised; "
"defaulting to `%d'",
group, key, defaultval);
tmp = defaultval;
goto EXIT;
} else {
keyfileptr = keyfile;
}
}
tmp = g_key_file_get_boolean(keyfileptr, group, key, &error);
if (error != NULL) {
mce_log(LL_WARN,
"Could not get config key %s/%s; %s; "
"defaulting to `%d'",
group, key, error->message, defaultval);
tmp = defaultval;
}
g_clear_error(&error);
EXIT:
return tmp;
}
/**
* Get an integer configuration value
*
* @param group The configuration group to get the value from
* @param key The configuration key to get the value of
* @param defaultval The default value to use if the key isn't set
* @param keyfileptr A keyfile pointer, or NULL to use the default keyfile
* @return The configuration value on success, the default value on failure
*/
gint mce_conf_get_int(const gchar *group, const gchar *key,
const gint defaultval, gpointer keyfileptr)
{
gint tmp = -1;
GError *error = NULL;
if (keyfileptr == NULL) {
if (keyfile == NULL) {
mce_log(LL_ERR,
"Could not get config key %s/%s; "
"no configuration file initialised; "
"defaulting to `%d'",
group, key, defaultval);
tmp = defaultval;
goto EXIT;
} else {
keyfileptr = keyfile;
}
}
tmp = g_key_file_get_integer(keyfileptr, group, key, &error);
if (error != NULL) {
mce_log(LL_WARN,
"Could not get config key %s/%s; %s; "
"defaulting to `%d'",
group, key, error->message, defaultval);
tmp = defaultval;
}
g_clear_error(&error);
EXIT:
return tmp;
}
/**
* Get an integer list configuration value
*
* @param group The configuration group to get the value from
* @param key The configuration key to get the value of
* @param length The length of the list
* @param keyfileptr A keyfile pointer, or NULL to use the default keyfile
* @return The configuration value on success, NULL on failure
*/
gint *mce_conf_get_int_list(const gchar *group, const gchar *key,
gsize *length, gpointer keyfileptr)
{
gint *tmp = NULL;
GError *error = NULL;
if (keyfileptr == NULL) {
if (keyfile == NULL) {
mce_log(LL_ERR,
"Could not get config key %s/%s; "
"no configuration file initialised",
group, key);
*length = 0;
goto EXIT;
} else {
keyfileptr = keyfile;
}
}
tmp = g_key_file_get_integer_list(keyfileptr, group, key,
length, &error);
if (error != NULL) {
mce_log(LL_WARN,
"Could not get config key %s/%s; %s",
group, key, error->message);
*length = 0;
}
g_clear_error(&error);
EXIT:
return tmp;
}
/**
* Get a string configuration value
*
* @param group The configuration group to get the value from
* @param key The configuration key to get the value of
* @param defaultval The default value to use if the key isn't set
* @param keyfileptr A keyfile pointer, or NULL to use the default keyfile
* @return The configuration value on success, the default value on failure
*/
gchar *mce_conf_get_string(const gchar *group, const gchar *key,
const gchar *defaultval, gpointer keyfileptr)
{
gchar *tmp = NULL;
GError *error = NULL;
if (keyfileptr == NULL) {
if (keyfile == NULL) {
mce_log(LL_ERR,
"Could not get config key %s/%s; "
"no configuration file initialised; "
"defaulting to `%s'",
group, key, defaultval);
if (defaultval != NULL)
tmp = g_strdup(defaultval);
goto EXIT;
} else {
keyfileptr = keyfile;
}
}
tmp = g_key_file_get_string(keyfileptr, group, key, &error);
if (error != NULL) {
mce_log(LL_WARN,
"Could not get config key %s/%s; %s; %s%s%s",
group, key, error->message,
defaultval ? "defaulting to `" : "no default set",
defaultval ? defaultval : "",
defaultval ? "'" : "");
if (defaultval != NULL)
tmp = g_strdup(defaultval);
}
g_clear_error(&error);
EXIT:
return tmp;
}
/**
* Get a string list configuration value
*
* @param group The configuration group to get the value from
* @param key The configuration key to get the value of
* @param length The length of the list
* @param keyfileptr A keyfile pointer, or NULL to use the default keyfile
* @return The configuration value on success, NULL on failure
*/
gchar **mce_conf_get_string_list(const gchar *group, const gchar *key,
gsize *length, gpointer keyfileptr)
{
gchar **tmp = NULL;
GError *error = NULL;
if (keyfileptr == NULL) {
if (keyfile == NULL) {
mce_log(LL_ERR,
"Could not get config key %s/%s; "
"no configuration file initialised",
group, key);
*length = 0;
goto EXIT;
} else {
keyfileptr = keyfile;
}
}
tmp = g_key_file_get_string_list(keyfileptr, group, key,
length, &error);
if (error != NULL) {
mce_log(LL_WARN,
"Could not get config key %s/%s; %s",
group, key, error->message);
*length = 0;
}
g_clear_error(&error);
EXIT:
return tmp;
}
gchar **mce_conf_get_keys(const gchar *group, gsize *length,
gpointer keyfileptr)
{
gchar **tmp = NULL;
GError *error = NULL;
if (keyfileptr == NULL) {
if (keyfile == NULL) {
mce_log(LL_ERR,
"Could not get config keys %s; "
"no configuration file initialised",
group);
*length = 0;
goto EXIT;
} else {
keyfileptr = keyfile;
}
}
tmp = g_key_file_get_keys(keyfileptr, group, length, &error);
if (error != NULL) {
mce_log(LL_WARN,
"Could not get config keys %s; %s",
group, error->message);
*length = 0;
}
g_clear_error(&error);
EXIT:
return tmp;
}
/**
* Free configuration file
*
* @param keyfileptr A pointer to the keyfile to free
*/
void mce_conf_free_conf_file(gpointer keyfileptr)
{
if (keyfileptr != NULL) {
g_key_file_free(keyfileptr);
}
}
/**
* Read configuration file
*
* @param conffile The full path to the configuration file to read
* @return A keyfile pointer on success, NULL on failure
*/
gpointer mce_conf_read_conf_file(const gchar *const conffile)
{
GError *error = NULL;
GKeyFile *keyfileptr;
if ((keyfileptr = g_key_file_new()) == NULL)
goto EXIT;
if (g_key_file_load_from_file(keyfileptr, conffile,
G_KEY_FILE_NONE, &error) == FALSE) {
mce_conf_free_conf_file(keyfileptr);
keyfileptr = NULL;
mce_log(LL_WARN, "Could not load %s; %s",
conffile, error->message);
goto EXIT;
}
EXIT:
g_clear_error(&error);
return keyfileptr;
}
/**
* Init function for the mce-conf component
*
* @return TRUE on success, FALSE on failure
*/
gboolean mce_conf_init(void)
{
gboolean status = FALSE;
if ((keyfile = mce_conf_read_conf_file(G_STRINGIFY(MCE_CONF_FILE))) == NULL) {
goto EXIT;
}
status = TRUE;
EXIT:
return status;
}
/**
* Exit function for the mce-conf component
*/
void mce_conf_exit(void)
{
mce_conf_free_conf_file(keyfile);
return;
}