]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - common/configuration.c
Fix small leaks detected by valgrind.
[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     {
41         g_key_file_free (key_file);
42         return FALSE;
43     }
44
45     groups = g_key_file_get_groups (key_file, NULL);
46     for (i = 0; groups[i]; i++)
47     {
48         gchar **keys;
49         int j;
50
51         keys = g_key_file_get_keys (key_file, groups[i], NULL, error);
52         if (!keys)
53             break;
54       
55         for (j = 0; keys[j]; j++)
56         {
57             gchar *value;
58
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);
61             g_free (value);
62         }
63
64         g_strfreev (keys);
65     }
66     g_strfreev (groups);
67
68     g_key_file_free (key_file);
69
70     return TRUE;
71 }
72
73 gchar **
74 config_get_groups (Configuration *config)
75 {
76     return g_key_file_get_groups (config->priv->key_file, NULL);
77 }
78
79 gchar **
80 config_get_keys (Configuration *config, const gchar *group_name)
81 {
82     return g_key_file_get_keys (config->priv->key_file, group_name, NULL, NULL);
83 }
84
85 gboolean
86 config_has_key (Configuration *config, const gchar *section, const gchar *key)
87 {
88     return g_key_file_has_key (config->priv->key_file, section, key, NULL);
89 }
90
91 void
92 config_set_string (Configuration *config, const gchar *section, const gchar *key, const gchar *value)
93 {
94     g_key_file_set_string (config->priv->key_file, section, key, value);
95 }
96
97 gchar *
98 config_get_string (Configuration *config, const gchar *section, const gchar *key)
99 {
100     return g_key_file_get_string (config->priv->key_file, section, key, NULL);
101 }
102
103 void
104 config_set_string_list (Configuration *config, const gchar *section, const gchar *key, const gchar **value, gsize length)
105 {
106     g_key_file_set_string_list (config->priv->key_file, section, key, value, length);
107 }
108
109 gchar **
110 config_get_string_list (Configuration *config, const gchar *section, const gchar *key)
111 {
112     return g_key_file_get_string_list (config->priv->key_file, section, key, NULL, NULL);
113 }
114
115 void
116 config_set_integer (Configuration *config, const gchar *section, const gchar *key, gint value)
117 {
118     g_key_file_set_integer (config->priv->key_file, section, key, value);
119 }
120
121 gint
122 config_get_integer (Configuration *config, const gchar *section, const gchar *key)
123 {
124     return g_key_file_get_integer (config->priv->key_file, section, key, NULL);
125 }
126
127 void
128 config_set_boolean (Configuration *config, const gchar *section, const gchar *key, gboolean value)
129 {
130     g_key_file_set_boolean (config->priv->key_file, section, key, value);
131 }
132
133 gboolean
134 config_get_boolean (Configuration *config, const gchar *section, const gchar *key)
135 {
136     return g_key_file_get_boolean (config->priv->key_file, section, key, NULL);
137 }
138
139 static void
140 config_init (Configuration *config)
141 {
142     config->priv = G_TYPE_INSTANCE_GET_PRIVATE (config, CONFIGURATION_TYPE, ConfigurationPrivate);
143     config->priv->key_file = g_key_file_new ();  
144 }
145
146 static void
147 config_finalize (GObject *object)
148 {
149     Configuration *self;
150
151     self = CONFIGURATION (object);
152
153     g_key_file_free (self->priv->key_file);
154
155     G_OBJECT_CLASS (config_parent_class)->finalize (object);  
156 }
157
158 static void
159 config_class_init (ConfigurationClass *klass)
160 {
161     GObjectClass *object_class = G_OBJECT_CLASS (klass);
162
163     object_class->finalize = config_finalize;  
164
165     g_type_class_add_private (klass, sizeof (ConfigurationPrivate));
166 }