Skip to content

Latest commit

 

History

History
570 lines (464 loc) · 13.1 KB

mce-setting.c

File metadata and controls

570 lines (464 loc) · 13.1 KB
 
Dec 16, 2010
Dec 16, 2010
1
/**
Mar 31, 2016
Mar 31, 2016
2
3
* @file mce-setting.c
* Runtime setting handling code for the Mode Control Entity
Dec 16, 2010
Dec 16, 2010
4
5
* <p>
* Copyright © 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
Mar 31, 2016
Mar 31, 2016
6
* Copyright © 2012-2016 Jolla Ltd.
Dec 16, 2010
Dec 16, 2010
7
8
* <p>
* @author David Weinehall <david.weinehall@nokia.com>
Mar 31, 2016
Mar 31, 2016
9
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
Dec 16, 2010
Dec 16, 2010
10
11
12
13
14
15
16
17
18
19
20
21
22
23
*
* 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/>.
*/
Mar 31, 2016
Mar 31, 2016
24
#include "mce-setting.h"
Dec 16, 2010
Dec 16, 2010
25
May 27, 2014
May 27, 2014
26
#include "mce-log.h"
Dec 16, 2010
Dec 16, 2010
27
Oct 24, 2014
Oct 24, 2014
28
29
#include <string.h>
Dec 16, 2010
Dec 16, 2010
30
31
/** Pointer to the GConf client */
static GConfClient *gconf_client = NULL;
Nov 16, 2012
Nov 16, 2012
32
33
/** Is GConf disabled on purpose */
static gboolean gconf_disabled = FALSE;
Dec 16, 2010
Dec 16, 2010
34
35
36
/** List of GConf notifiers */
static GSList *gconf_notifiers = NULL;
Jun 17, 2014
Jun 17, 2014
37
38
39
40
41
42
/** Check if gconf-key exists
*
* @param key Name of value
*
* @return TRUE if value exits, FALSE otherwise
*/
Mar 31, 2016
Mar 31, 2016
43
gboolean mce_setting_has_key(const gchar *const key)
Jun 17, 2014
Jun 17, 2014
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{
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;
}
Dec 21, 2018
Dec 21, 2018
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
/**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;
}
Dec 16, 2010
Dec 16, 2010
92
93
94
95
96
97
98
/**
* 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
*/
Mar 31, 2016
Mar 31, 2016
99
gboolean mce_setting_set_int(const gchar *const key, const gint value)
Dec 16, 2010
Dec 16, 2010
100
101
102
{
gboolean status = FALSE;
Nov 16, 2012
Nov 16, 2012
103
104
105
106
107
if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s = %d", key, value);
goto EXIT;
}
Dec 16, 2010
Dec 16, 2010
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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;
}
Sep 12, 2011
Sep 12, 2011
122
123
124
125
126
127
128
/**
* 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
*/
Mar 31, 2016
Mar 31, 2016
129
gboolean mce_setting_set_string(const gchar *const key, const gchar *const value)
Sep 12, 2011
Sep 12, 2011
130
131
132
{
gboolean status = FALSE;
Nov 16, 2012
Nov 16, 2012
133
134
135
136
137
if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s = \"%s\"", key, value);
goto EXIT;
}
Sep 12, 2011
Sep 12, 2011
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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;
}
Dec 16, 2010
Dec 16, 2010
152
153
154
155
156
157
158
/**
* 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
*/
Mar 31, 2016
Mar 31, 2016
159
gboolean mce_setting_get_bool(const gchar *const key, gboolean *value)
Dec 16, 2010
Dec 16, 2010
160
161
162
{
gboolean status = FALSE;
GError *error = NULL;
Apr 29, 2014
Apr 29, 2014
163
GConfValue *gcv = 0;
Dec 16, 2010
Dec 16, 2010
164
Nov 16, 2012
Nov 16, 2012
165
166
167
168
169
if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s query", key);
goto EXIT;
}
Dec 16, 2010
Dec 16, 2010
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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:
Apr 29, 2014
Apr 29, 2014
191
192
if( gcv )
gconf_value_free(gcv);
Dec 16, 2010
Dec 16, 2010
193
194
195
196
197
198
199
200
201
202
203
204
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
*/
Mar 31, 2016
Mar 31, 2016
205
gboolean mce_setting_get_int(const gchar *const key, gint *value)
Dec 16, 2010
Dec 16, 2010
206
207
208
{
gboolean status = FALSE;
GError *error = NULL;
Apr 29, 2014
Apr 29, 2014
209
GConfValue *gcv = 0;
Dec 16, 2010
Dec 16, 2010
210
Nov 16, 2012
Nov 16, 2012
211
212
213
214
215
if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s query", key);
goto EXIT;
}
Dec 16, 2010
Dec 16, 2010
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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:
Apr 29, 2014
Apr 29, 2014
237
238
if( gcv )
gconf_value_free(gcv);
Dec 16, 2010
Dec 16, 2010
239
240
241
242
243
244
245
246
247
248
249
250
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
*/
Mar 31, 2016
Mar 31, 2016
251
gboolean mce_setting_get_int_list(const gchar *const key, GSList **values)
Dec 16, 2010
Dec 16, 2010
252
253
254
{
gboolean status = FALSE;
GError *error = NULL;
Apr 29, 2014
Apr 29, 2014
255
256
GConfValue *gcv = 0;
GConfValue *gcv2;
Dec 16, 2010
Dec 16, 2010
257
258
259
GSList *list;
gint i;
Nov 16, 2012
Nov 16, 2012
260
261
262
263
264
if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s query", key);
goto EXIT;
}
Dec 16, 2010
Dec 16, 2010
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
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:
Apr 29, 2014
Apr 29, 2014
300
301
if( gcv )
gconf_value_free(gcv);
Dec 16, 2010
Dec 16, 2010
302
303
304
305
306
g_clear_error(&error);
return status;
}
Sep 12, 2011
Sep 12, 2011
307
308
309
310
311
312
313
/**
* 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
*/
Mar 31, 2016
Mar 31, 2016
314
gboolean mce_setting_get_string(const gchar *const key, gchar **value)
Sep 12, 2011
Sep 12, 2011
315
316
317
{
gboolean status = FALSE;
GError *error = NULL;
Apr 29, 2014
Apr 29, 2014
318
GConfValue *gcv = 0;
Sep 12, 2011
Sep 12, 2011
319
Nov 16, 2012
Nov 16, 2012
320
321
322
323
324
if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s query", key);
goto EXIT;
}
Sep 12, 2011
Sep 12, 2011
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
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:
Apr 29, 2014
Apr 29, 2014
346
347
if( gcv )
gconf_value_free(gcv);
Sep 12, 2011
Sep 12, 2011
348
349
350
351
352
g_clear_error(&error);
return status;
}
Dec 16, 2010
Dec 16, 2010
353
354
355
356
357
358
/**
* 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
Jun 16, 2014
Jun 16, 2014
359
360
* @param[out] cb_id Will contain the callback ID or zero on return
*
Dec 16, 2010
Dec 16, 2010
361
362
* @return TRUE on success, FALSE on failure
*/
Mar 31, 2016
Mar 31, 2016
363
gboolean mce_setting_notifier_add(const gchar *path, const gchar *key,
Dec 16, 2010
Dec 16, 2010
364
365
366
367
368
const GConfClientNotifyFunc callback,
guint *cb_id)
{
GError *error = NULL;
gboolean status = FALSE;
Jun 16, 2014
Jun 16, 2014
369
guint id = 0;
Dec 16, 2010
Dec 16, 2010
370
Nov 16, 2012
Nov 16, 2012
371
372
373
if( gconf_disabled ) {
mce_log(LL_DEBUG, "blocked %s notifier", key);
Jun 16, 2014
Jun 16, 2014
374
/* Returning failure could result in termination
Nov 16, 2012
Nov 16, 2012
375
376
377
378
379
* of mce process -> return bogus success if we
* have disabled gconf on purpose. */
status = TRUE;
goto EXIT;
}
Jun 16, 2014
Jun 16, 2014
380
Dec 16, 2010
Dec 16, 2010
381
382
383
384
gconf_client_add_dir(gconf_client, path,
GCONF_CLIENT_PRELOAD_NONE, &error);
if (error != NULL) {
Jan 14, 2014
Jan 14, 2014
385
mce_log(LL_WARN,
Dec 16, 2010
Dec 16, 2010
386
387
388
"Could not add %s to directories watched by "
"GConf client setting from GConf; %s",
path, error->message);
Jun 16, 2014
Jun 16, 2014
389
goto EXIT;
Dec 16, 2010
Dec 16, 2010
390
391
}
Jun 16, 2014
Jun 16, 2014
392
393
id = gconf_client_notify_add(gconf_client, key, callback,
NULL, NULL, &error);
Dec 16, 2010
Dec 16, 2010
394
if (error != NULL) {
Jan 14, 2014
Jan 14, 2014
395
mce_log(LL_WARN,
Dec 16, 2010
Dec 16, 2010
396
397
"Could not register notifier for %s; %s",
key, error->message);
Jun 16, 2014
Jun 16, 2014
398
goto EXIT;
Dec 16, 2010
Dec 16, 2010
399
400
}
Jun 16, 2014
Jun 16, 2014
401
402
403
404
405
if( !id )
goto EXIT;
gconf_notifiers = g_slist_prepend(gconf_notifiers,
GINT_TO_POINTER(id));
Jun 16, 2014
Jun 16, 2014
406
Dec 16, 2010
Dec 16, 2010
407
408
status = TRUE;
Nov 16, 2012
Nov 16, 2012
409
EXIT:
Dec 16, 2010
Dec 16, 2010
410
411
g_clear_error(&error);
Jun 16, 2014
Jun 16, 2014
412
return *cb_id = id, status;
Dec 16, 2010
Dec 16, 2010
413
414
415
416
417
}
/**
* Remove a GConf notifier
*
Jun 16, 2014
Jun 16, 2014
418
419
420
* Calling with zero id is allowed and does nothing
*
* @param id The ID of the notifier to remove, or zero
Dec 16, 2010
Dec 16, 2010
421
*/
Mar 31, 2016
Mar 31, 2016
422
void mce_setting_notifier_remove(guint id)
Dec 16, 2010
Dec 16, 2010
423
{
Nov 16, 2012
Nov 16, 2012
424
425
426
if( gconf_disabled )
goto EXIT;
Jun 16, 2014
Jun 16, 2014
427
428
429
430
431
if( !id )
goto EXIT;
gconf_client_notify_remove(gconf_client, id);
gconf_notifiers = g_slist_remove(gconf_notifiers, GINT_TO_POINTER(id));
Nov 16, 2012
Nov 16, 2012
432
433
434
EXIT:
return;
Dec 16, 2010
Dec 16, 2010
435
436
}
Jun 16, 2014
Jun 16, 2014
437
438
439
440
441
/** Helper callback for removing GConf notifiers with g_slist_foreach
*
* @param cb_id The ID of the notifier to remove
* @param user_data Unused
*/
Mar 31, 2016
Mar 31, 2016
442
void mce_setting_notifier_remove_cb(gpointer cb_id, gpointer user_data)
Jun 16, 2014
Jun 16, 2014
443
444
445
{
(void)user_data;
Mar 31, 2016
Mar 31, 2016
446
mce_setting_notifier_remove(GPOINTER_TO_INT(cb_id));
Jun 16, 2014
Jun 16, 2014
447
448
}
Oct 24, 2014
Oct 24, 2014
449
450
/** Helper for getting path of gconf key
*/
Mar 31, 2016
Mar 31, 2016
451
static gchar *mce_setting_get_path(const gchar *key)
Oct 24, 2014
Oct 24, 2014
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
{
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
*/
Mar 31, 2016
Mar 31, 2016
471
void mce_setting_track_int(const gchar *key, gint *val, gint def,
Oct 24, 2014
Oct 24, 2014
472
473
GConfClientNotifyFunc cb, guint *cb_id)
{
Mar 31, 2016
Mar 31, 2016
474
gchar *path = mce_setting_get_path(key);
Oct 24, 2014
Oct 24, 2014
475
476
if( path && cb && cb_id )
Mar 31, 2016
Mar 31, 2016
477
mce_setting_notifier_add(path, key, cb, cb_id);
Oct 24, 2014
Oct 24, 2014
478
Mar 31, 2016
Mar 31, 2016
479
if( !mce_setting_get_int(key, val) && def != -1 )
Oct 24, 2014
Oct 24, 2014
480
481
482
483
484
*val = def;
g_free(path);
}
Jan 29, 2015
Jan 29, 2015
485
486
487
488
489
490
491
492
493
/** 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
*/
Mar 31, 2016
Mar 31, 2016
494
void mce_setting_track_bool(const gchar *key, gboolean *val, gint def,
Jan 29, 2015
Jan 29, 2015
495
496
GConfClientNotifyFunc cb, guint *cb_id)
{
Mar 31, 2016
Mar 31, 2016
497
gchar *path = mce_setting_get_path(key);
Jan 29, 2015
Jan 29, 2015
498
499
if( path && cb && cb_id )
Mar 31, 2016
Mar 31, 2016
500
mce_setting_notifier_add(path, key, cb, cb_id);
Jan 29, 2015
Jan 29, 2015
501
Mar 31, 2016
Mar 31, 2016
502
if( !mce_setting_get_bool(key, val) && def != -1 )
Jan 29, 2015
Jan 29, 2015
503
504
505
506
507
*val = (def != 0);
g_free(path);
}
Oct 24, 2014
Oct 24, 2014
508
509
510
511
512
513
514
515
516
517
518
519
/** 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
*/
Mar 31, 2016
Mar 31, 2016
520
void mce_setting_track_string(const gchar *key, gchar **val, const gchar *def,
Oct 24, 2014
Oct 24, 2014
521
522
GConfClientNotifyFunc cb, guint *cb_id)
{
Mar 31, 2016
Mar 31, 2016
523
gchar *path = mce_setting_get_path(key);
Oct 24, 2014
Oct 24, 2014
524
525
if( path && cb && cb_id )
Mar 31, 2016
Mar 31, 2016
526
mce_setting_notifier_add(path, key, cb, cb_id);
Oct 24, 2014
Oct 24, 2014
527
Mar 31, 2016
Mar 31, 2016
528
if( !mce_setting_get_string(key, val) && def != 0 )
Oct 24, 2014
Oct 24, 2014
529
530
531
532
533
*val = g_strdup(def);
g_free(path);
}
Dec 16, 2010
Dec 16, 2010
534
535
536
537
538
/**
* Init function for the mce-gconf component
*
* @return TRUE on success, FALSE on failure
*/
Mar 31, 2016
Mar 31, 2016
539
gboolean mce_setting_init(void)
Dec 16, 2010
Dec 16, 2010
540
541
542
{
gboolean status = FALSE;
May 20, 2014
May 20, 2014
543
544
545
/* Get the default builtin-gconf client */
if( !(gconf_client = gconf_client_get_default()) ) {
mce_log(LL_CRIT, "Could not get default builtin-gconf client");
Dec 16, 2010
Dec 16, 2010
546
547
548
549
550
551
552
553
554
555
556
557
goto EXIT;
}
status = TRUE;
EXIT:
return status;
}
/**
* Exit function for the mce-gconf component
*/
Mar 31, 2016
Mar 31, 2016
558
void mce_setting_exit(void)
Dec 16, 2010
Dec 16, 2010
559
{
May 20, 2014
May 20, 2014
560
if( gconf_client ) {
Dec 16, 2010
Dec 16, 2010
561
/* Free the list of GConf notifiers */
Mar 31, 2016
Mar 31, 2016
562
g_slist_foreach(gconf_notifiers, mce_setting_notifier_remove_cb, 0);
May 20, 2014
May 20, 2014
563
564
565
gconf_notifiers = 0;
/* Forget builtin-gconf client reference */
Dec 11, 2012
Dec 11, 2012
566
gconf_client = 0;
Dec 16, 2010
Dec 16, 2010
567
568
569
570
}
return;
}