-
Notifications
You must be signed in to change notification settings - Fork 9
/
mce-setting.c
570 lines (464 loc) · 13.1 KB
/
mce-setting.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
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
/**
* @file mce-setting.c
* Runtime setting handling code for the Mode Control Entity
* <p>
* Copyright © 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
* Copyright © 2012-2016 Jolla Ltd.
* <p>
* @author David Weinehall <[email protected]>
* @author Simo Piiroinen <[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 "mce-setting.h"
#include "mce-log.h"
#include <string.h>
/** Pointer to the GConf client */
static GConfClient *gconf_client = NULL;
/** Is GConf disabled on purpose */
static gboolean gconf_disabled = FALSE;
/** List of GConf notifiers */
static GSList *gconf_notifiers = NULL;
/** Check if gconf-key exists
*
* @param key Name of value
*
* @return TRUE if value exits, FALSE otherwise
*/
gboolean mce_setting_has_key(const gchar *const key)
{
gboolean res = FALSE;
GConfValue *val = 0;
GError *err = 0;
if( gconf_disabled )
goto EXIT;
val = gconf_client_get(gconf_client, key, &err);
res = (val != 0);
EXIT:
g_clear_error(&err);
gconf_value_free(val);
return res;
}
/**Set an boolean GConf key to the specified value
*
* @param key The GConf key to set the value of
* @param value The value to set the key to
*
* @return TRUE on success, FALSE on failure
*/
gboolean mce_setting_set_bool(const gchar *const key, const gboolean value)
{
gboolean status = FALSE;
if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s = %d", key, value);
goto EXIT;
}
if( !gconf_client_set_bool(gconf_client, key, value, NULL) ) {
mce_log(LL_WARN, "Failed to write %s to GConf", key);
goto EXIT;
}
/* synchronise if possible, ignore errors */
gconf_client_suggest_sync(gconf_client, NULL);
status = TRUE;
EXIT:
return status;
}
/**
* Set an integer GConf key to the specified value
*
* @param key The GConf key to set the value of
* @param value The value to set the key to
* @return TRUE on success, FALSE on failure
*/
gboolean mce_setting_set_int(const gchar *const key, const gint value)
{
gboolean status = FALSE;
if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s = %d", key, value);
goto EXIT;
}
if (gconf_client_set_int(gconf_client, key, value, NULL) == FALSE) {
mce_log(LL_WARN, "Failed to write %s to GConf", key);
goto EXIT;
}
/* synchronise if possible, ignore errors */
gconf_client_suggest_sync(gconf_client, NULL);
status = TRUE;
EXIT:
return status;
}
/**
* Set an string GConf key to the specified value
*
* @param key The GConf key to set the value of
* @param value The value to set the key to
* @return TRUE on success, FALSE on failure
*/
gboolean mce_setting_set_string(const gchar *const key, const gchar *const value)
{
gboolean status = FALSE;
if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s = \"%s\"", key, value);
goto EXIT;
}
if (gconf_client_set_string(gconf_client, key, value, NULL) == FALSE) {
mce_log(LL_WARN, "Failed to write %s to GConf", key);
goto EXIT;
}
/* synchronise if possible, ignore errors */
gconf_client_suggest_sync(gconf_client, NULL);
status = TRUE;
EXIT:
return status;
}
/**
* Return a boolean from the specified GConf key
*
* @param key The GConf key to get the value from
* @param[out] value Will contain the value on return, if successful
* @return TRUE on success, FALSE on failure
*/
gboolean mce_setting_get_bool(const gchar *const key, gboolean *value)
{
gboolean status = FALSE;
GError *error = NULL;
GConfValue *gcv = 0;
if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s query", key);
goto EXIT;
}
gcv = gconf_client_get(gconf_client, key, &error);
if (gcv == NULL) {
mce_log((error != NULL) ? LL_WARN : LL_INFO,
"Could not retrieve %s from GConf; %s",
key, (error != NULL) ? error->message : "Key not set");
goto EXIT;
}
if (gcv->type != GCONF_VALUE_BOOL) {
mce_log(LL_ERR,
"GConf key %s should have type: %d, but has type: %d",
key, GCONF_VALUE_BOOL, gcv->type);
goto EXIT;
}
*value = gconf_value_get_bool(gcv);
status = TRUE;
EXIT:
if( gcv )
gconf_value_free(gcv);
g_clear_error(&error);
return status;
}
/**
* Return an integer from the specified GConf key
*
* @param key The GConf key to get the value from
* @param[out] value Will contain the value on return
* @return TRUE on success, FALSE on failure
*/
gboolean mce_setting_get_int(const gchar *const key, gint *value)
{
gboolean status = FALSE;
GError *error = NULL;
GConfValue *gcv = 0;
if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s query", key);
goto EXIT;
}
gcv = gconf_client_get(gconf_client, key, &error);
if (gcv == NULL) {
mce_log((error != NULL) ? LL_WARN : LL_INFO,
"Could not retrieve %s from GConf; %s",
key, (error != NULL) ? error->message : "Key not set");
goto EXIT;
}
if (gcv->type != GCONF_VALUE_INT) {
mce_log(LL_ERR,
"GConf key %s should have type: %d, but has type: %d",
key, GCONF_VALUE_INT, gcv->type);
goto EXIT;
}
*value = gconf_value_get_int(gcv);
status = TRUE;
EXIT:
if( gcv )
gconf_value_free(gcv);
g_clear_error(&error);
return status;
}
/**
* Return an integer list from the specified GConf key
*
* @param key The GConf key to get the values from
* @param[out] values Will contain an GSList with the values on return
* @return TRUE on success, FALSE on failure
*/
gboolean mce_setting_get_int_list(const gchar *const key, GSList **values)
{
gboolean status = FALSE;
GError *error = NULL;
GConfValue *gcv = 0;
GConfValue *gcv2;
GSList *list;
gint i;
if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s query", key);
goto EXIT;
}
gcv = gconf_client_get(gconf_client, key, &error);
if (gcv == NULL) {
mce_log((error != NULL) ? LL_WARN : LL_INFO,
"Could not retrieve %s from GConf; %s",
key, (error != NULL) ? error->message : "Key not set");
goto EXIT;
}
if ((gcv->type != GCONF_VALUE_LIST) ||
(gconf_value_get_list_type(gcv) != GCONF_VALUE_INT)) {
mce_log(LL_ERR,
"GConf key %s should have type: %d<%d>, but has type: %d<%d>",
key, GCONF_VALUE_LIST, GCONF_VALUE_INT,
gcv->type, gconf_value_get_list_type(gcv));
goto EXIT;
}
list = gconf_value_get_list(gcv);
for (i = 0; (gcv2 = g_slist_nth_data(list, i)) != NULL; i++) {
gint data;
data = gconf_value_get_int(gcv2);
/* Prepend is more efficient than append */
*values = g_slist_prepend(*values, GINT_TO_POINTER(data));
}
/* Reverse the list, since we want the entries in the right order */
*values = g_slist_reverse(*values);
status = TRUE;
EXIT:
if( gcv )
gconf_value_free(gcv);
g_clear_error(&error);
return status;
}
/**
* Return an string from the specified GConf key
*
* @param key The GConf key to get the values from
* @param[out] value Will contain a newly allocated string with the value
* @return TRUE on success, FALSE on failure
*/
gboolean mce_setting_get_string(const gchar *const key, gchar **value)
{
gboolean status = FALSE;
GError *error = NULL;
GConfValue *gcv = 0;
if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s query", key);
goto EXIT;
}
gcv = gconf_client_get(gconf_client, key, &error);
if (gcv == NULL) {
mce_log((error != NULL) ? LL_WARN : LL_INFO,
"Could not retrieve %s from GConf; %s",
key, (error != NULL) ? error->message : "Key not set");
goto EXIT;
}
if ((gcv->type != GCONF_VALUE_STRING)) {
mce_log(LL_ERR,
"GConf key %s should have type: %d, but has type: %d",
key, GCONF_VALUE_STRING, gcv->type);
goto EXIT;
}
*value = g_strdup(gconf_value_get_string(gcv));
status = TRUE;
EXIT:
if( gcv )
gconf_value_free(gcv);
g_clear_error(&error);
return status;
}
/**
* Add a GConf notifier
*
* @param path The GConf directory to watch
* @param key The GConf key to add the notifier for
* @param callback The callback function
* @param[out] cb_id Will contain the callback ID or zero on return
*
* @return TRUE on success, FALSE on failure
*/
gboolean mce_setting_notifier_add(const gchar *path, const gchar *key,
const GConfClientNotifyFunc callback,
guint *cb_id)
{
GError *error = NULL;
gboolean status = FALSE;
guint id = 0;
if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s notifier", key);
/* Returning failure could result in termination
* of mce process -> return bogus success if we
* have disabled gconf on purpose. */
status = TRUE;
goto EXIT;
}
gconf_client_add_dir(gconf_client, path,
GCONF_CLIENT_PRELOAD_NONE, &error);
if (error != NULL) {
mce_log(LL_WARN,
"Could not add %s to directories watched by "
"GConf client setting from GConf; %s",
path, error->message);
goto EXIT;
}
id = gconf_client_notify_add(gconf_client, key, callback,
NULL, NULL, &error);
if (error != NULL) {
mce_log(LL_WARN,
"Could not register notifier for %s; %s",
key, error->message);
goto EXIT;
}
if( !id )
goto EXIT;
gconf_notifiers = g_slist_prepend(gconf_notifiers,
GINT_TO_POINTER(id));
status = TRUE;
EXIT:
g_clear_error(&error);
return *cb_id = id, status;
}
/**
* Remove a GConf notifier
*
* Calling with zero id is allowed and does nothing
*
* @param id The ID of the notifier to remove, or zero
*/
void mce_setting_notifier_remove(guint id)
{
if( gconf_disabled )
goto EXIT;
if( !id )
goto EXIT;
gconf_client_notify_remove(gconf_client, id);
gconf_notifiers = g_slist_remove(gconf_notifiers, GINT_TO_POINTER(id));
EXIT:
return;
}
/** Helper callback for removing GConf notifiers with g_slist_foreach
*
* @param cb_id The ID of the notifier to remove
* @param user_data Unused
*/
void mce_setting_notifier_remove_cb(gpointer cb_id, gpointer user_data)
{
(void)user_data;
mce_setting_notifier_remove(GPOINTER_TO_INT(cb_id));
}
/** Helper for getting path of gconf key
*/
static gchar *mce_setting_get_path(const gchar *key)
{
gchar *res = 0;
const gchar *end = strrchr(key, '/');
if( end )
res = g_strndup(key, end - key);
return res;
}
/** Get initial value of integer setting and start tracking changes
*
* @param key key name
* @param val where to store the initial value
* @param def default value to use if getting key value fails;
* or -1 to leave *val unmodified
* @param cb change notification callback
* @param cb_id where to store notification callback id
*/
void mce_setting_track_int(const gchar *key, gint *val, gint def,
GConfClientNotifyFunc cb, guint *cb_id)
{
gchar *path = mce_setting_get_path(key);
if( path && cb && cb_id )
mce_setting_notifier_add(path, key, cb, cb_id);
if( !mce_setting_get_int(key, val) && def != -1 )
*val = def;
g_free(path);
}
/** Get initial value of boolean setting and start tracking changes
*
* @param key key name
* @param val where to store the initial value
* @param def default value to use if getting key value fails;
* or -1 to leave *val unmodified
* @param cb change notification callback
* @param cb_id where to store notification callback id
*/
void mce_setting_track_bool(const gchar *key, gboolean *val, gint def,
GConfClientNotifyFunc cb, guint *cb_id)
{
gchar *path = mce_setting_get_path(key);
if( path && cb && cb_id )
mce_setting_notifier_add(path, key, cb, cb_id);
if( !mce_setting_get_bool(key, val) && def != -1 )
*val = (def != 0);
g_free(path);
}
/** Get initial value of integer setting and start tracking changes
*
* Note: Caller must release returned string with g_free() when it
* is no longer needed.
*
* @param key key name
* @param val where to store the initial value
* @param def default value to use if getting key value fails;
* or NULL to leave *val unmodified
* @param cb change notification callback
* @param cb_id where to store notification callback id
*/
void mce_setting_track_string(const gchar *key, gchar **val, const gchar *def,
GConfClientNotifyFunc cb, guint *cb_id)
{
gchar *path = mce_setting_get_path(key);
if( path && cb && cb_id )
mce_setting_notifier_add(path, key, cb, cb_id);
if( !mce_setting_get_string(key, val) && def != 0 )
*val = g_strdup(def);
g_free(path);
}
/**
* Init function for the mce-gconf component
*
* @return TRUE on success, FALSE on failure
*/
gboolean mce_setting_init(void)
{
gboolean status = FALSE;
/* Get the default builtin-gconf client */
if( !(gconf_client = gconf_client_get_default()) ) {
mce_log(LL_CRIT, "Could not get default builtin-gconf client");
goto EXIT;
}
status = TRUE;
EXIT:
return status;
}
/**
* Exit function for the mce-gconf component
*/
void mce_setting_exit(void)
{
if( gconf_client ) {
/* Free the list of GConf notifiers */
g_slist_foreach(gconf_notifiers, mce_setting_notifier_remove_cb, 0);
gconf_notifiers = 0;
/* Forget builtin-gconf client reference */
gconf_client = 0;
}
return;
}