Mercurial > audlegacy
annotate libaudacious/configdb_gconf.c @ 1446:7610ab1233a7 trunk
[svn] - be more paranoid about NULL pointers
author | nenolod |
---|---|
date | Fri, 28 Jul 2006 02:19:18 -0700 |
parents | c8cf439179b8 |
children | a69db3a15bf2 |
rev | line source |
---|---|
0 | 1 /* This program is free software; you can redistribute it and/or modify |
2 * it under the terms of the GNU General Public License as published by | |
3 * the Free Software Foundation; either version 2 of the License, or | |
4 * (at your option) any later version. | |
5 * | |
6 * This program is distributed in the hope that it will be useful, | |
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
9 * GNU General Public License for more details. | |
10 * | |
11 * You should have received a copy of the GNU General Public License | |
12 * along with this program; if not, write to the Free Software | |
13 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
14 */ | |
15 | |
16 #include "configdb.h" | |
17 | |
18 #include <string.h> | |
19 #include <gconf/gconf-client.h> | |
20 | |
21 | |
22 #define BMP_CONFIG_BASE_PATH "/apps/audacious" | |
23 | |
24 | |
25 struct _ConfigDb { | |
26 GConfClient *client; | |
27 }; | |
28 | |
29 | |
30 ConfigDb * | |
31 bmp_cfg_db_open() | |
32 { | |
33 ConfigDb *db; | |
34 | |
35 db = g_new(ConfigDb, 1); | |
36 | |
37 db->client = gconf_client_get_default(); | |
38 | |
39 return db; | |
40 } | |
41 | |
42 void | |
43 bmp_cfg_db_close(ConfigDb * db) | |
44 { | |
1446 | 45 g_return_if_fail(db != NULL); |
46 | |
0 | 47 g_object_unref(db->client); |
813
c8cf439179b8
[svn] - Fix a ton and a half of memory leaks, via the wonderful Leonardo Boshell <leonardop -at- gentoo.org>.
nenolod
parents:
0
diff
changeset
|
48 g_free(db); |
0 | 49 } |
50 | |
51 static gchar * | |
52 build_keypath(const gchar * section, | |
53 const gchar * key) | |
54 { | |
55 if (section == NULL) { | |
56 return g_strconcat(BMP_CONFIG_BASE_PATH, "/", key, NULL); | |
57 } | |
58 else { | |
59 return g_strconcat(BMP_CONFIG_BASE_PATH, "/", section, "/", | |
60 key, NULL); | |
61 } | |
62 } | |
63 | |
64 static gboolean | |
65 bmp_cfg_db_get_value(ConfigDb * db, | |
66 const gchar * section, | |
67 const gchar * key, | |
68 GConfValue ** value) | |
69 { | |
70 gchar *path; | |
71 | |
72 g_return_val_if_fail(db != NULL, FALSE); | |
73 g_return_val_if_fail(key != NULL, FALSE); | |
74 g_return_val_if_fail(value != NULL, FALSE); | |
75 | |
76 path = build_keypath(section, key); | |
77 *value = gconf_client_get(db->client, path, NULL); | |
78 g_free(path); | |
79 | |
80 return (*value != NULL) ? TRUE : FALSE; | |
81 } | |
82 | |
83 static void | |
84 bmp_cfg_db_set_value(ConfigDb * db, | |
85 const gchar * section, | |
86 const gchar * key, | |
87 GConfValue * value) | |
88 { | |
89 gchar *path; | |
90 | |
91 g_return_if_fail(db != NULL); | |
92 g_return_if_fail(key != NULL); | |
93 g_return_if_fail(value != NULL); | |
94 | |
95 path = build_keypath(section, key); | |
96 gconf_client_set(db->client, path, value, NULL); | |
97 g_free(path); | |
98 } | |
99 | |
100 gboolean | |
101 bmp_cfg_db_get_string(ConfigDb * db, | |
102 const gchar * section, | |
103 const gchar * key, | |
104 gchar ** value) | |
105 { | |
106 GConfValue *cval; | |
107 | |
108 if (!bmp_cfg_db_get_value(db, section, key, &cval)) | |
109 return FALSE; | |
110 if (cval->type != GCONF_VALUE_STRING) | |
111 return FALSE; | |
112 *value = strdup(gconf_value_get_string(cval)); | |
113 gconf_value_free(cval); | |
114 | |
115 return TRUE; | |
116 } | |
117 | |
118 gboolean | |
119 bmp_cfg_db_get_int(ConfigDb * db, | |
120 const gchar * section, | |
121 const gchar * key, | |
122 gint * value) | |
123 { | |
124 GConfValue *cval; | |
125 | |
126 if (!bmp_cfg_db_get_value(db, section, key, &cval)) | |
127 return FALSE; | |
128 if (cval->type != GCONF_VALUE_INT) | |
129 return FALSE; | |
130 *value = gconf_value_get_int(cval); | |
131 gconf_value_free(cval); | |
132 | |
133 return TRUE; | |
134 } | |
135 | |
136 gboolean | |
137 bmp_cfg_db_get_bool(ConfigDb * db, | |
138 const gchar * section, | |
139 const gchar * key, | |
140 gboolean * value) | |
141 { | |
142 GConfValue *cval; | |
143 | |
144 if (!bmp_cfg_db_get_value(db, section, key, &cval)) | |
145 return FALSE; | |
146 if (cval->type != GCONF_VALUE_BOOL) | |
147 return FALSE; | |
148 *value = gconf_value_get_bool(cval); | |
149 gconf_value_free(cval); | |
150 | |
151 return TRUE; | |
152 } | |
153 | |
154 gboolean | |
155 bmp_cfg_db_get_float(ConfigDb * db, | |
156 const gchar * section, | |
157 const gchar * key, | |
158 gfloat * value) | |
159 { | |
160 GConfValue *cval; | |
161 | |
162 if (!bmp_cfg_db_get_value(db, section, key, &cval)) | |
163 return FALSE; | |
164 if (cval->type != GCONF_VALUE_FLOAT) | |
165 return FALSE; | |
166 *value = gconf_value_get_float(cval); | |
167 gconf_value_free(cval); | |
168 | |
169 return TRUE; | |
170 } | |
171 | |
172 gboolean | |
173 bmp_cfg_db_get_double(ConfigDb * db, | |
174 const gchar * section, | |
175 const gchar * key, | |
176 gdouble * value) | |
177 { | |
178 GConfValue *cval; | |
179 | |
180 if (!bmp_cfg_db_get_value(db, section, key, &cval)) | |
181 return FALSE; | |
182 if (cval->type != GCONF_VALUE_FLOAT) | |
183 return FALSE; | |
184 *value = gconf_value_get_float(cval); | |
185 gconf_value_free(cval); | |
186 | |
187 return TRUE; | |
188 } | |
189 | |
190 void | |
191 bmp_cfg_db_set_string(ConfigDb * db, | |
192 const gchar * section, | |
193 const gchar * key, | |
194 gchar * value) | |
195 { | |
196 GConfValue *cval; | |
197 | |
198 cval = gconf_value_new(GCONF_VALUE_STRING); | |
199 gconf_value_set_string(cval, value); | |
200 bmp_cfg_db_set_value(db, section, key, cval); | |
201 gconf_value_free(cval); | |
202 } | |
203 | |
204 void | |
205 bmp_cfg_db_set_int(ConfigDb * db, | |
206 const gchar * section, | |
207 const gchar * key, | |
208 gint value) | |
209 { | |
210 GConfValue *cval; | |
211 | |
212 cval = gconf_value_new(GCONF_VALUE_INT); | |
213 gconf_value_set_int(cval, value); | |
214 bmp_cfg_db_set_value(db, section, key, cval); | |
215 gconf_value_free(cval); | |
216 } | |
217 | |
218 void | |
219 bmp_cfg_db_set_bool(ConfigDb * db, | |
220 const gchar * section, | |
221 const gchar * key, | |
222 gboolean value) | |
223 { | |
224 GConfValue *cval; | |
225 | |
226 cval = gconf_value_new(GCONF_VALUE_BOOL); | |
227 gconf_value_set_bool(cval, value); | |
228 bmp_cfg_db_set_value(db, section, key, cval); | |
229 gconf_value_free(cval); | |
230 } | |
231 | |
232 void | |
233 bmp_cfg_db_set_float(ConfigDb * db, | |
234 const gchar * section, | |
235 const gchar * key, | |
236 gfloat value) | |
237 { | |
238 GConfValue *cval; | |
239 | |
240 cval = gconf_value_new(GCONF_VALUE_FLOAT); | |
241 gconf_value_set_float(cval, value); | |
242 bmp_cfg_db_set_value(db, section, key, cval); | |
243 gconf_value_free(cval); | |
244 } | |
245 | |
246 void | |
247 bmp_cfg_db_set_double(ConfigDb * db, | |
248 const gchar * section, | |
249 const gchar * key, | |
250 gdouble value) | |
251 { | |
252 GConfValue *cval; | |
253 | |
254 cval = gconf_value_new(GCONF_VALUE_FLOAT); | |
255 gconf_value_set_float(cval, value); | |
256 bmp_cfg_db_set_value(db, section, key, cval); | |
257 gconf_value_free(cval); | |
258 } | |
259 | |
260 void | |
261 bmp_cfg_db_unset_key(ConfigDb * db, | |
262 const gchar * section, | |
263 const gchar * key) | |
264 { | |
265 gchar *path; | |
266 | |
267 g_return_if_fail(db != NULL); | |
268 g_return_if_fail(key != NULL); | |
269 | |
270 path = build_keypath(section, key); | |
271 gconf_client_unset(db->client, path, NULL); | |
272 g_free(path); | |
273 } |