1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * This file is part of tlm
6 * Copyright (C) 2013-2014 Intel Corporation.
8 * Contact: Imran Zaman <imran.zaman@intel.com>
9 * Amarnath Valluri <amarnath.valluri@linux.intel.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
29 #include <glib/gstdio.h>
32 #include "tlm-config.h"
33 #include "tlm-config-general.h"
38 * @short_description: tlm configuration information
39 * @include: tlm-config.h
41 * #TlmConfig holds configuration information as a set of keys and values
42 * (integer or strings). The key names are defined in
43 * <link linkend="tlm-General-configuration">General config keys</link>.
44 * Note that authentication and account plugins may use plugin-specific
45 * configuration keys; see plugins' documentation for specifics.
47 * The configuration is retrieved from the tlm configuration file. See below
48 * for where the file is searched for.
50 * <refsect1><title>Usage</title></refsect1>
51 * Following code snippet demonstrates how to create and use config object:
54 * TlmConfig* config = tlm_config_new ();
55 * const gchar *str = tlm_config_get_string (config,
56 * TLM_CONFIG_GENERAL, TLM_CONFIG_GENERAL_PAM_SERVICE, 0);
57 * g_object_unref(config);
61 * <refsect1><title>Where the configuration file is searched for</title>
64 * If tlm has been compiled with --enable-debug, then these locations are used,
65 * in decreasing order of priority:
66 * - TLM_CONF_FILE environment variable
67 * - g_get_user_config_dir() + "tlm.conf"
68 * - each of g_get_system_config_dirs() + "tlm.conf"
70 * Otherwise, the config file location is determined at compilation time as
71 * $(sysconfdir) + "tlm.conf"
73 * <refsect1><title>Example configuration file</title></refsect1>
75 * See example configuration file here:
76 * <ulink url="https://github.com/01org/tlm/blob/master/data/tlm.conf.in">
77 * tlm configuration file</ulink>
84 * Opaque structure for the object.
89 * @parent_class: parent class object
91 * Opaque structure for the class.
94 struct _TlmConfigPrivate
96 gchar *config_file_path;
97 GHashTable *config_table;
100 G_DEFINE_TYPE_WITH_PRIVATE (TlmConfig, tlm_config, G_TYPE_OBJECT);
103 _check_config_file (const gchar *path)
105 gchar *fn = g_build_filename (path, "tlm.conf", NULL);
106 DBG ("check config at %s", fn);
107 if (g_access (fn, R_OK) == 0)
114 _load_config (TlmConfig *self)
116 TlmConfigPrivate *priv = self->priv;
118 gchar **groups = NULL;
121 GKeyFile *settings = g_key_file_new ();
123 const gchar * const *sysconfdirs;
125 if (!priv->config_file_path) {
126 const gchar *cfg_env = g_getenv ("TLM_CONF_FILE");
128 if (g_access (cfg_env, R_OK) == 0)
129 priv->config_file_path = g_strdup (cfg_env);
132 if (!priv->config_file_path) {
133 priv->config_file_path = _check_config_file (TLM_SYSCONF_DIR);
135 if (!priv->config_file_path) {
136 sysconfdirs = g_get_system_config_dirs ();
137 while (*sysconfdirs != NULL) {
138 gchar *sys_cfg = _check_config_file (*sysconfdirs);
140 priv->config_file_path = sys_cfg;
147 if (priv->config_file_path) {
148 DBG ("loading TLM config from %s", priv->config_file_path);
149 if (!g_key_file_load_from_file (settings,
150 priv->config_file_path,
151 G_KEY_FILE_NONE, &err)) {
152 WARN ("error reading config file at '%s': %s",
153 priv->config_file_path, err->message);
155 g_key_file_free (settings);
160 WARN ("No valid configuration file found");
161 g_key_file_free (settings);
165 groups = g_key_file_get_groups (settings, &n_groups);
167 for (i = 0; i < n_groups; i++) {
170 GHashTable *group_table = NULL;
171 gchar **keys = g_key_file_get_keys (settings,
176 WARN ("fail to read group '%s': %s", groups[i], err->message);
181 group_table = (GHashTable *) g_hash_table_lookup (
182 priv->config_table, groups[i]);
184 group_table = g_hash_table_new_full (
188 (GDestroyNotify)g_free);
190 for (j = 0; j < n_keys; j++) {
191 gchar *value = g_key_file_get_value (settings,
196 WARN ("fail to read key '%s/%s': %s", groups[i], keys[j],
202 DBG ("found config : '%s/%s' - '%s'", groups[i], keys[j], value);
204 g_hash_table_insert (group_table,
205 (gpointer)g_strdup (keys[j]),
210 g_hash_table_insert (priv->config_table,
211 g_strdup (groups[i]),
219 g_key_file_free (settings);
229 const gchar *e_val = 0;
231 e_val = g_getenv("TLM_ACCOUNT_PLUGIN");
233 tlm_config_set_string (self,
235 TLM_CONFIG_GENERAL_ACCOUNTS_PLUGIN, e_val);
237 #endif /* ENABLE_DEBUG */
240 * tlm_config_get_group:
241 * @self: (transfer none): an instance of #TlmConfig
242 * @group: the group name
244 * Retrives the configuration in given #group as #GHashTable
246 * Returns: (transfer none): the key, value paired dictionary if found,
250 tlm_config_get_group (
254 g_return_val_if_fail (self && TLM_IS_CONFIG(self), NULL);
255 g_return_val_if_fail (group && group[0], NULL);
257 return (GHashTable *) g_hash_table_lookup (self->priv->config_table,
262 * tlm_config_get_string:
263 * @self: (transfer none): an instance of #TlmConfig
264 * @group: (transfer none): the group name, NULL refers to General
265 * @key: (transfer none): the key name
267 * Get a string configuration value.
269 * Returns: the value corresponding to the key as an string. If the
270 * key does not exist, NULL is returned.
273 tlm_config_get_string (
278 g_return_val_if_fail (self && TLM_IS_CONFIG (self), NULL);
279 g_return_val_if_fail (key && key[0], NULL);
281 GHashTable *group_table = tlm_config_get_group (self, group);
282 if (!group_table) return NULL;
284 return (const gchar *) g_hash_table_lookup (group_table, key);
288 * tlm_config_get_string_default:
289 * @self: (transfer none): an instance of #TlmConfig
290 * @group: (transfer none): the group name, NULL refers to General
291 * @key: (transfer none): the key name
292 * @value: (transfer none): default value
294 * Get a string configuration value.
296 * Returns: the value corresponding to the key as an string. If the
297 * key does not exist, given default value is returned.
300 tlm_config_get_string_default (
307 g_return_val_if_fail (self && TLM_IS_CONFIG (self), NULL);
308 g_return_val_if_fail (key && key[0], NULL);
310 GHashTable *group_table = tlm_config_get_group (self, group);
311 if (!group_table) return NULL;
313 res = g_hash_table_lookup (group_table, key);
320 * tlm_config_set_string:
321 * @self: (transfer none): an instance of #TlmConfig
322 * @group: (transfer none): the group name
323 * @key: (transfer none): the key name
324 * @value: (transfer none): the value
326 * Sets the configuration value to the provided string.
329 tlm_config_set_string (
335 g_return_if_fail (self && TLM_IS_CONFIG (self));
336 g_return_if_fail (key && key[0]);
338 if (!group) group = TLM_CONFIG_GENERAL;
340 GHashTable *group_table = tlm_config_get_group (self, group);
342 group_table = g_hash_table_new_full (g_str_hash,
346 g_hash_table_insert (self->priv->config_table,
347 (gpointer)g_strdup (group),
348 (gpointer)group_table);
351 g_hash_table_insert (group_table,
352 (gpointer) g_strdup (key),
353 (gpointer) g_strdup (value));
357 * tlm_config_get_int:
358 * @self: (transfer none): an instance of #TlmConfig
359 * @group: the group name
361 * @retval: value to be returned in case key is not found
363 * Get an integer configuration value.
365 * Returns: the value corresponding to the key as an integer. If the key does
366 * not exist or cannot be converted to the integer, retval is returned.
375 const gchar *str_value = tlm_config_get_string (self, group, key);
376 return (gint) (str_value ? atoi (str_value) : retval);
380 * tlm_config_set_int:
381 * @self: (transfer none): an instance of #TlmConfig
382 * @group: the group name
386 * Sets the configuration value to the provided integer.
396 g_return_if_fail (self && TLM_IS_CONFIG (self));
398 s_value = g_strdup_printf ("%d", value);
399 if (!s_value) return;
401 tlm_config_set_string (self, group, key, s_value);
407 * tlm_config_get_uint:
408 * @self: (transfer none): an instance of #TlmConfig
409 * @group: (transfer none): the group name
410 * @key: (transfer none): the key name
411 * @retval: value to be returned in case key is not found
413 * Get an unsigned integer configuration value.
415 * Returns: the value corresponding to the key as an unsigned integer. If the
416 * key does not exist or cannot be converted to the integer, retval is returned.
419 tlm_config_get_uint (
426 const gchar *str_value = tlm_config_get_string (self, group, key);
428 if (str_value && (*str_value == '-')) {
431 if (!str_value || sscanf (str_value, "%u", &value) <= 0) value = retval;
438 * tlm_config_set_uint:
439 * @self: (transfer none): an instance of #TlmConfig
440 * @group: (transfer none): the group name
444 * Sets the configuration value to the provided unsigned integer.
447 tlm_config_set_uint (
454 g_return_if_fail (self && TLM_IS_CONFIG (self));
456 s_value = g_strdup_printf ("%u", value);
457 if (!s_value) return;
459 tlm_config_set_string (self, group, key, s_value);
464 * tlm_config_get_boolean:
465 * @self: (transfer none): an instance of #TlmConfig
466 * @group: (transfer none): the group name
467 * @key: (transfer none): the key name
468 * @retval: value to be returned in case key is not found
470 * Get a boolean configuration value.
472 * Returns: the value corresponding to the key as boolean. If the
473 * key does not exist or cannot be converted to boolean, retval is returned.
476 tlm_config_get_boolean (
482 const gchar *str_value = NULL;
484 g_return_val_if_fail (self && TLM_IS_CONFIG (self), retval);
486 str_value = tlm_config_get_string (self, group, key);
490 if (g_ascii_strncasecmp (str_value, "false", 5) == 0 ||
491 g_ascii_strncasecmp (str_value, "no", 2) == 0)
494 if (g_ascii_strncasecmp (str_value, "true", 4) == 0 ||
495 g_ascii_strncasecmp (str_value, "yes", 3) == 0)
498 if (sscanf (str_value, "%d", &value) <= 0)
501 return (gboolean) value;
505 * tlm_config_set_boolean:
506 * @self: (transfer none): an instance of #TlmConfig
507 * @group: the group name
511 * Sets the configuration value to the provided #value.
514 tlm_config_set_boolean (
520 g_return_if_fail (self && TLM_IS_CONFIG (self));
522 tlm_config_set_int (self, group, key, (gint)value);
525 * tlm_config_has_group:
526 * @self: (transfer none): an instance of #TlmConfig
527 * @group: the group name
529 * Checks if any configuration available for #group.
531 * Returns: TRUE if found, FALSE otherwise.
534 tlm_config_has_group (
538 g_return_val_if_fail (self && TLM_IS_CONFIG (self), FALSE);
539 g_return_val_if_fail (group, FALSE);
541 return g_hash_table_contains (self->priv->config_table,
542 (gconstpointer)group);
546 * tlm_config_has_key:
547 * @self: (transfer none): an instance of #TlmConfig
548 * @group: the group name
551 * Checks if #key is in given #group.
553 * Returns: TRUE, if found, FALSE otherwise.
562 GHashTable *group_table = NULL;
563 g_return_val_if_fail (self && TLM_IS_CONFIG (self), FALSE);
564 g_return_val_if_fail (key, FALSE);
566 if (!group) group = TLM_CONFIG_GENERAL;
568 group_table = tlm_config_get_group (self, group);
569 if (!group_table) return FALSE;
571 return g_hash_table_contains (group_table, (gconstpointer)key);
575 _cleanup (TlmConfig *self)
577 if (self->priv->config_table) {
578 g_hash_table_unref (self->priv->config_table);
579 self->priv->config_table = NULL;
582 if (self->priv->config_file_path) {
583 g_free (self->priv->config_file_path);
584 self->priv->config_file_path = NULL;
593 g_return_if_fail (object && TLM_IS_CONFIG (object));
595 self = TLM_CONFIG (object);
599 G_OBJECT_CLASS (tlm_config_parent_class)->dispose (object);
603 tlm_config_finalize (
606 g_return_if_fail (object && TLM_IS_CONFIG (object));
608 G_OBJECT_CLASS (tlm_config_parent_class)->finalize (object);
612 tlm_config_class_init (
613 TlmConfigClass *klass)
615 GObjectClass *object_class = G_OBJECT_CLASS (klass);
617 object_class->dispose = tlm_config_dispose;
618 object_class->finalize = tlm_config_finalize;
622 _initialize (TlmConfig *self)
624 self->priv->config_file_path = NULL;
625 self->priv->config_table = g_hash_table_new_full (
629 (GDestroyNotify)g_hash_table_unref);
632 if (!_load_config (self))
633 WARN ("load configuration failed, using default settings");
636 _load_environment (self);
644 self->priv = tlm_config_get_instance_private (self);
650 * @self: (transfer none): an instance of #TlmConfig
652 * Reloads the configuration.
659 g_return_if_fail (self && TLM_IS_CONFIG (self));
661 DBG ("reload configuration");
669 * Create a #TlmConfig object.
671 * Returns: an instance of #TlmConfig.
676 return TLM_CONFIG (g_object_new (TLM_TYPE_CONFIG, NULL));