]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - common/configuration.c
Merge from trunk
[sojka/lightdm.git] / common / configuration.c
1 /*
2  * Copyright (C) 2010-2011 Robert Ancell.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  * 
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
9  * license.
10  */
11
12 #include "configuration.h"
13
14 struct ConfigurationPrivate
15 {
16     GKeyFile *key_file;
17 };
18
19 G_DEFINE_TYPE (Configuration, config, G_TYPE_OBJECT);
20
21 static Configuration *configuration_instance = NULL;
22
23 Configuration *
24 config_get_instance (void)
25 {
26     if (!configuration_instance)
27         configuration_instance = g_object_new (CONFIGURATION_TYPE, NULL);
28     return configuration_instance;
29 }
30
31 gboolean
32 config_load_from_file (Configuration *config, const gchar *path, GError **error)
33 {
34     GKeyFile *key_file;
35     gchar **groups;
36     int i;
37
38     key_file = g_key_file_new ();
39     if (!g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, error))
40         return FALSE;
41
42     groups = g_key_file_get_groups (key_file, NULL);
43     for (i = 0; groups[i]; i++)
44     {
45         gchar **keys;
46         int j;
47
48         keys = g_key_file_get_keys (key_file, groups[i], NULL, error);
49         if (!keys)
50             break;
51       
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));
54
55         g_strfreev (keys);
56     }
57     g_strfreev (groups);
58
59     return TRUE;
60 }
61
62 gchar **
63 config_get_groups (Configuration *config)
64 {
65     return g_key_file_get_groups (config->priv->key_file, NULL);
66 }
67
68 gchar **
69 config_get_keys (Configuration *config, const gchar *group_name)
70 {
71     return g_key_file_get_keys (config->priv->key_file, group_name, NULL, NULL);
72 }
73
74 gboolean
75 config_has_key (Configuration *config, const gchar *section, const gchar *key)
76 {
77     return g_key_file_has_key (config->priv->key_file, section, key, NULL);
78 }
79
80 void
81 config_set_string (Configuration *config, const gchar *section, const gchar *key, const gchar *value)
82 {
83     g_key_file_set_string (config->priv->key_file, section, key, value);
84 }
85
86 gchar *
87 config_get_string (Configuration *config, const gchar *section, const gchar *key)
88 {
89     return g_key_file_get_string (config->priv->key_file, section, key, NULL);
90 }
91
92 void
93 config_set_string_list (Configuration *config, const gchar *section, const gchar *key, const gchar **value, gsize length)
94 {
95     g_key_file_set_string_list (config->priv->key_file, section, key, value, length);
96 }
97
98 gchar **
99 config_get_string_list (Configuration *config, const gchar *section, const gchar *key)
100 {
101     return g_key_file_get_string_list (config->priv->key_file, section, key, NULL, NULL);
102 }
103
104 void
105 config_set_integer (Configuration *config, const gchar *section, const gchar *key, gint value)
106 {
107     g_key_file_set_integer (config->priv->key_file, section, key, value);
108 }
109
110 gint
111 config_get_integer (Configuration *config, const gchar *section, const gchar *key)
112 {
113     return g_key_file_get_integer (config->priv->key_file, section, key, NULL);
114 }
115
116 void
117 config_set_boolean (Configuration *config, const gchar *section, const gchar *key, gboolean value)
118 {
119     g_key_file_set_boolean (config->priv->key_file, section, key, value);
120 }
121
122 gboolean
123 config_get_boolean (Configuration *config, const gchar *section, const gchar *key)
124 {
125     return g_key_file_get_boolean (config->priv->key_file, section, key, NULL);
126 }
127
128 static void
129 config_init (Configuration *config)
130 {
131     config->priv = G_TYPE_INSTANCE_GET_PRIVATE (config, CONFIGURATION_TYPE, ConfigurationPrivate);
132     config->priv->key_file = g_key_file_new ();  
133 }
134
135 static void
136 config_finalize (GObject *object)
137 {
138     Configuration *self;
139
140     self = CONFIGURATION (object);
141
142     g_key_file_free (self->priv->key_file);
143
144     G_OBJECT_CLASS (config_parent_class)->finalize (object);  
145 }
146
147 static void
148 config_class_init (ConfigurationClass *klass)
149 {
150     GObjectClass *object_class = G_OBJECT_CLASS (klass);
151
152     object_class->finalize = config_finalize;  
153
154     g_type_class_add_private (klass, sizeof (ConfigurationPrivate));
155 }