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))
42 groups = g_key_file_get_groups (key_file, NULL);
43 for (i = 0; groups[i]; i++)
48 keys = g_key_file_get_keys (key_file, groups[i], NULL, error);
52 for (j = 0; keys[j]; j++)
53 g_key_file_set_value (config->priv->key_file, groups[i], keys[j], g_key_file_get_value (key_file, groups[i], keys[j], NULL));
63 config_get_groups (Configuration *config)
65 return g_key_file_get_groups (config->priv->key_file, NULL);
69 config_get_keys (Configuration *config, const gchar *group_name)
71 return g_key_file_get_keys (config->priv->key_file, group_name, NULL, NULL);
75 config_has_key (Configuration *config, const gchar *section, const gchar *key)
77 return g_key_file_has_key (config->priv->key_file, section, key, NULL);
81 config_set_string (Configuration *config, const gchar *section, const gchar *key, const gchar *value)
83 g_key_file_set_string (config->priv->key_file, section, key, value);
87 config_get_string (Configuration *config, const gchar *section, const gchar *key)
89 return g_key_file_get_string (config->priv->key_file, section, key, NULL);
93 config_set_string_list (Configuration *config, const gchar *section, const gchar *key, const gchar **value, gsize length)
95 g_key_file_set_string_list (config->priv->key_file, section, key, value, length);
99 config_get_string_list (Configuration *config, const gchar *section, const gchar *key)
101 return g_key_file_get_string_list (config->priv->key_file, section, key, NULL, NULL);
105 config_set_integer (Configuration *config, const gchar *section, const gchar *key, gint value)
107 g_key_file_set_integer (config->priv->key_file, section, key, value);
111 config_get_integer (Configuration *config, const gchar *section, const gchar *key)
113 return g_key_file_get_integer (config->priv->key_file, section, key, NULL);
117 config_set_boolean (Configuration *config, const gchar *section, const gchar *key, gboolean value)
119 g_key_file_set_boolean (config->priv->key_file, section, key, value);
123 config_get_boolean (Configuration *config, const gchar *section, const gchar *key)
125 return g_key_file_get_boolean (config->priv->key_file, section, key, NULL);
129 config_init (Configuration *config)
131 config->priv = G_TYPE_INSTANCE_GET_PRIVATE (config, CONFIGURATION_TYPE, ConfigurationPrivate);
132 config->priv->key_file = g_key_file_new ();
136 config_finalize (GObject *object)
140 self = CONFIGURATION (object);
142 g_key_file_free (self->priv->key_file);
144 G_OBJECT_CLASS (config_parent_class)->finalize (object);
148 config_class_init (ConfigurationClass *klass)
150 GObjectClass *object_class = G_OBJECT_CLASS (klass);
152 object_class->finalize = config_finalize;
154 g_type_class_add_private (klass, sizeof (ConfigurationPrivate));