mce-setting.c 13.1 KB
Newer Older
1
/**
2 3
 * @file mce-setting.c
 * Runtime setting handling code for the Mode Control Entity
4 5
 * <p>
 * Copyright © 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
6
 * Copyright © 2012-2016 Jolla Ltd.
7 8
 * <p>
 * @author David Weinehall <david.weinehall@nokia.com>
9
 * @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
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/>.
 */

24
#include "mce-setting.h"
25

26
#include "mce-log.h"
27

28 29
#include <string.h>

30 31
/** Pointer to the GConf client */
static GConfClient *gconf_client = NULL;
32 33
/** Is GConf disabled on purpose */
static gboolean gconf_disabled = FALSE;
34 35 36
/** List of GConf notifiers */
static GSList *gconf_notifiers = NULL;

37 38 39 40 41 42
/** Check if gconf-key exists
 *
 * @param key Name of value
 *
 * @return TRUE if value exits, FALSE otherwise
 */
43
gboolean mce_setting_has_key(const gchar *const key)
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;
}

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;
}

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
 */
99
gboolean mce_setting_set_int(const gchar *const key, const gint value)
100 101 102
{
	gboolean status = FALSE;

103 104 105 106 107
	if( gconf_disabled ) {
		mce_log(LL_DEBUG, "blocked %s = %d", key, value);
		goto EXIT;
	}

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;
}

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
 */
129
gboolean mce_setting_set_string(const gchar *const key, const gchar *const value)
130 131 132
{
	gboolean status = FALSE;

133 134 135 136 137
	if( gconf_disabled ) {
		mce_log(LL_DEBUG, "blocked %s = \"%s\"", key, value);
		goto EXIT;
	}

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;
}

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
 */
159
gboolean mce_setting_get_bool(const gchar *const key, gboolean *value)
160 161 162
{
	gboolean status = FALSE;
	GError *error = NULL;
163
	GConfValue *gcv = 0;
164

165 166 167 168 169
	if( gconf_disabled ) {
		mce_log(LL_DEBUG, "blocked %s query", key);
		goto EXIT;
	}

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:
191 192
	if( gcv )
		gconf_value_free(gcv);
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
 */
205
gboolean mce_setting_get_int(const gchar *const key, gint *value)
206 207 208
{
	gboolean status = FALSE;
	GError *error = NULL;
209
	GConfValue *gcv = 0;
210

211 212 213 214 215
	if( gconf_disabled ) {
		mce_log(LL_DEBUG, "blocked %s query", key);
		goto EXIT;
	}

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:
237 238
	if( gcv )
		gconf_value_free(gcv);
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
 */
251
gboolean mce_setting_get_int_list(const gchar *const key, GSList **values)
252 253 254
{
	gboolean status = FALSE;
	GError *error = NULL;
255 256
	GConfValue *gcv = 0;
	GConfValue *gcv2;
257 258 259
	GSList *list;
	gint i;

260 261 262 263 264
	if( gconf_disabled ) {
		mce_log(LL_DEBUG, "blocked %s query", key);
		goto EXIT;
	}

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:
300 301
	if( gcv )
		gconf_value_free(gcv);
302 303 304 305 306
	g_clear_error(&error);

	return status;
}

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
 */
314
gboolean mce_setting_get_string(const gchar *const key, gchar **value)
315 316 317
{
	gboolean status = FALSE;
	GError *error = NULL;
318
	GConfValue *gcv = 0;
319

320 321 322 323 324
	if( gconf_disabled ) {
		mce_log(LL_DEBUG, "blocked %s query", key);
		goto EXIT;
	}

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:
346 347
	if( gcv )
		gconf_value_free(gcv);
348 349 350 351 352
	g_clear_error(&error);

	return status;
}

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
359 360
 * @param[out] cb_id Will contain the callback ID or zero on return
 *
361 362
 * @return TRUE on success, FALSE on failure
 */
363
gboolean mce_setting_notifier_add(const gchar *path, const gchar *key,
364 365 366 367 368
				const GConfClientNotifyFunc callback,
				guint *cb_id)
{
	GError *error = NULL;
	gboolean status = FALSE;
369
	guint id = 0;
370

371 372 373
	if( gconf_disabled ) {
		mce_log(LL_DEBUG, "blocked %s notifier", key);

374
		/* Returning failure could result in termination
375 376 377 378 379
		 * of mce process -> return bogus success if we
		 * have disabled gconf on purpose. */
		status = TRUE;
		goto EXIT;
	}
380

381 382 383 384
	gconf_client_add_dir(gconf_client, path,
			     GCONF_CLIENT_PRELOAD_NONE, &error);

	if (error != NULL) {
spiiroin's avatar
spiiroin committed
385
		mce_log(LL_WARN,
386 387 388
			"Could not add %s to directories watched by "
			"GConf client setting from GConf; %s",
			path, error->message);
389
		goto EXIT;
390 391
	}

392 393
	id = gconf_client_notify_add(gconf_client, key, callback,
				     NULL, NULL, &error);
394
	if (error != NULL) {
spiiroin's avatar
spiiroin committed
395
		mce_log(LL_WARN,
396 397
			"Could not register notifier for %s; %s",
			key, error->message);
398
		goto EXIT;
399 400
	}

401 402 403 404 405
	if( !id )
		goto EXIT;

	gconf_notifiers = g_slist_prepend(gconf_notifiers,
					  GINT_TO_POINTER(id));
406

407 408
	status = TRUE;

409
EXIT:
410 411
	g_clear_error(&error);

412
	return *cb_id = id, status;
413 414 415 416 417
}

/**
 * Remove a GConf notifier
 *
418 419 420
 * Calling with zero id is allowed and does nothing
 *
 * @param id The ID of the notifier to remove, or zero
421
 */
422
void mce_setting_notifier_remove(guint id)
423
{
424 425 426
	if( gconf_disabled )
		goto EXIT;

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));
432 433 434

EXIT:
	return;
435 436
}

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
 */
442
void mce_setting_notifier_remove_cb(gpointer cb_id, gpointer user_data)
443 444 445
{
	(void)user_data;

446
	mce_setting_notifier_remove(GPOINTER_TO_INT(cb_id));
447 448
}

449 450
/** Helper for getting path of gconf key
 */
451
static gchar *mce_setting_get_path(const gchar *key)
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
 */
471
void mce_setting_track_int(const gchar *key, gint *val, gint def,
472 473
			 GConfClientNotifyFunc cb, guint *cb_id)
{
474
	gchar *path = mce_setting_get_path(key);
475 476

	if( path && cb && cb_id )
477
		mce_setting_notifier_add(path, key, cb, cb_id);
478

479
	if( !mce_setting_get_int(key, val) && def != -1 )
480 481 482 483 484
		*val = def;

	g_free(path);
}

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
 */
494
void mce_setting_track_bool(const gchar *key, gboolean *val, gint def,
495 496
			  GConfClientNotifyFunc cb, guint *cb_id)
{
497
	gchar *path = mce_setting_get_path(key);
498 499

	if( path && cb && cb_id )
500
		mce_setting_notifier_add(path, key, cb, cb_id);
501

502
	if( !mce_setting_get_bool(key, val) && def != -1 )
503 504 505 506 507
		*val = (def != 0);

	g_free(path);
}

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
 */
520
void mce_setting_track_string(const gchar *key, gchar **val, const gchar *def,
521 522
			    GConfClientNotifyFunc cb, guint *cb_id)
{
523
	gchar *path = mce_setting_get_path(key);
524 525

	if( path && cb && cb_id )
526
		mce_setting_notifier_add(path, key, cb, cb_id);
527

528
	if( !mce_setting_get_string(key, val) && def != 0 )
529 530 531 532 533
		*val = g_strdup(def);

	g_free(path);
}

534 535 536 537 538
/**
 * Init function for the mce-gconf component
 *
 * @return TRUE on success, FALSE on failure
 */
539
gboolean mce_setting_init(void)
540 541 542
{
	gboolean status = FALSE;

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");
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
 */
558
void mce_setting_exit(void)
559
{
560
	if( gconf_client ) {
561
		/* Free the list of GConf notifiers */
562
		g_slist_foreach(gconf_notifiers, mce_setting_notifier_remove_cb, 0);
563 564 565
		gconf_notifiers = 0;

		/* Forget builtin-gconf client reference */
566
		gconf_client = 0;
567 568 569 570
	}

	return;
}