2 * Copyright (C) 2010-2011 Robert Ancell.
3 * Author: Robert Ancell <robert.ancell@canonical.com>
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option) any later
8 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
12 #include "configuration.h"
14 struct ConfigurationPrivate
19 G_DEFINE_TYPE (Configuration, config, G_TYPE_OBJECT);
21 static Configuration *configuration_instance = NULL;
24 config_get_instance (void)
26 if (!configuration_instance)
27 configuration_instance = g_object_new (CONFIGURATION_TYPE, NULL);
28 return configuration_instance;
32 config_load_from_file (Configuration *config, const gchar *path, GError **error)
38 key_file = g_key_file_new ();
39 if (!g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, error))
41 g_key_file_free (key_file);
45 groups = g_key_file_get_groups (key_file, NULL);
46 for (i = 0; groups[i]; i++)
51 keys = g_key_file_get_keys (key_file, groups[i], NULL, error);
55 for (j = 0; keys[j]; j++)
59 value = g_key_file_get_value (key_file, groups[i], keys[j], NULL);
60 g_key_file_set_value (config->priv->key_file, groups[i], keys[j], value);
68 g_key_file_free (key_file);
74 config_get_groups (Configuration *config)
76 return g_key_file_get_groups (config->priv->key_file, NULL);
80 config_get_keys (Configuration *config, const gchar *group_name)
82 return g_key_file_get_keys (config->priv->key_file, group_name, NULL, NULL);
86 config_has_key (Configuration *config, const gchar *section, const gchar *key)
88 return g_key_file_has_key (config->priv->key_file, section, key, NULL);
92 config_set_string (Configuration *config, const gchar *section, const gchar *key, const gchar *value)
94 g_key_file_set_string (config->priv->key_file, section, key, value);
98 config_get_string (Configuration *config, const gchar *section, const gchar *key)
100 return g_key_file_get_string (config->priv->key_file, section, key, NULL);
104 config_set_string_list (Configuration *config, const gchar *section, const gchar *key, const gchar **value, gsize length)
106 g_key_file_set_string_list (config->priv->key_file, section, key, value, length);
110 config_get_string_list (Configuration *config, const gchar *section, const gchar *key)
112 return g_key_file_get_string_list (config->priv->key_file, section, key, NULL, NULL);
116 config_set_integer (Configuration *config, const gchar *section, const gchar *key, gint value)
118 g_key_file_set_integer (config->priv->key_file, section, key, value);
122 config_get_integer (Configuration *config, const gchar *section, const gchar *key)
124 return g_key_file_get_integer (config->priv->key_file, section, key, NULL);
128 config_set_boolean (Configuration *config, const gchar *section, const gchar *key, gboolean value)
130 g_key_file_set_boolean (config->priv->key_file, section, key, value);
134 config_get_boolean (Configuration *config, const gchar *section, const gchar *key)
136 return g_key_file_get_boolean (config->priv->key_file, section, key, NULL);
140 config_init (Configuration *config)
142 config->priv = G_TYPE_INSTANCE_GET_PRIVATE (config, CONFIGURATION_TYPE, ConfigurationPrivate);
143 config->priv->key_file = g_key_file_new ();
147 config_finalize (GObject *object)
151 self = CONFIGURATION (object);
153 g_key_file_free (self->priv->key_file);
155 G_OBJECT_CLASS (config_parent_class)->finalize (object);
159 config_class_init (ConfigurationClass *klass)
161 GObjectClass *object_class = G_OBJECT_CLASS (klass);
163 object_class->finalize = config_finalize;
165 g_type_class_add_private (klass, sizeof (ConfigurationPrivate));