1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
---|
2 | * |
---|
3 | * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu> |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation; either version 2 of the License, or |
---|
8 | * (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
18 | * |
---|
19 | */ |
---|
20 | |
---|
21 | #include "config.h" |
---|
22 | |
---|
23 | #include <stdlib.h> |
---|
24 | #include <stdio.h> |
---|
25 | #include <fcntl.h> |
---|
26 | #include <unistd.h> |
---|
27 | #include <string.h> |
---|
28 | #include <signal.h> |
---|
29 | #include <sys/stat.h> |
---|
30 | #include <sys/types.h> |
---|
31 | #include <errno.h> |
---|
32 | |
---|
33 | #include <glib.h> |
---|
34 | #include <glib/gi18n.h> |
---|
35 | #include <glib/gstdio.h> |
---|
36 | #include <glib-object.h> |
---|
37 | |
---|
38 | #include "gdm-settings-utils.h" |
---|
39 | |
---|
40 | struct _GdmSettingsEntry |
---|
41 | { |
---|
42 | char *key; |
---|
43 | char *signature; |
---|
44 | char *default_value; |
---|
45 | char *value; |
---|
46 | }; |
---|
47 | |
---|
48 | GdmSettingsEntry * |
---|
49 | gdm_settings_entry_new (void) |
---|
50 | { |
---|
51 | GdmSettingsEntry *entry = NULL; |
---|
52 | |
---|
53 | entry = g_new0 (GdmSettingsEntry, 1); |
---|
54 | entry->key = NULL; |
---|
55 | entry->signature = NULL; |
---|
56 | entry->value = NULL; |
---|
57 | entry->default_value = NULL; |
---|
58 | |
---|
59 | return entry; |
---|
60 | } |
---|
61 | |
---|
62 | const char * |
---|
63 | gdm_settings_entry_get_key (GdmSettingsEntry *entry) |
---|
64 | { |
---|
65 | return entry->key; |
---|
66 | } |
---|
67 | |
---|
68 | const char * |
---|
69 | gdm_settings_entry_get_signature (GdmSettingsEntry *entry) |
---|
70 | { |
---|
71 | return entry->signature; |
---|
72 | } |
---|
73 | |
---|
74 | const char * |
---|
75 | gdm_settings_entry_get_default_value (GdmSettingsEntry *entry) |
---|
76 | { |
---|
77 | return entry->default_value; |
---|
78 | } |
---|
79 | |
---|
80 | const char * |
---|
81 | gdm_settings_entry_get_value (GdmSettingsEntry *entry) |
---|
82 | { |
---|
83 | return entry->value; |
---|
84 | } |
---|
85 | |
---|
86 | void |
---|
87 | gdm_settings_entry_set_value (GdmSettingsEntry *entry, |
---|
88 | const char *value) |
---|
89 | { |
---|
90 | g_free (entry->value); |
---|
91 | entry->value = g_strdup (value); |
---|
92 | } |
---|
93 | |
---|
94 | void |
---|
95 | gdm_settings_entry_free (GdmSettingsEntry *entry) |
---|
96 | { |
---|
97 | g_free (entry->key); |
---|
98 | g_free (entry->signature); |
---|
99 | g_free (entry->default_value); |
---|
100 | g_free (entry->value); |
---|
101 | g_free (entry); |
---|
102 | } |
---|
103 | |
---|
104 | typedef struct { |
---|
105 | GSList *list; |
---|
106 | GdmSettingsEntry *entry; |
---|
107 | gboolean in_key; |
---|
108 | gboolean in_signature; |
---|
109 | gboolean in_default; |
---|
110 | } ParserInfo; |
---|
111 | |
---|
112 | static void |
---|
113 | start_element_cb (GMarkupParseContext *ctx, |
---|
114 | const char *element_name, |
---|
115 | const char **attribute_names, |
---|
116 | const char **attribute_values, |
---|
117 | gpointer user_data, |
---|
118 | GError **error) |
---|
119 | { |
---|
120 | ParserInfo *info; |
---|
121 | |
---|
122 | info = (ParserInfo *) user_data; |
---|
123 | |
---|
124 | /*g_debug ("parsing start: '%s'", element_name);*/ |
---|
125 | |
---|
126 | if (strcmp (element_name, "schema") == 0) { |
---|
127 | info->entry = gdm_settings_entry_new (); |
---|
128 | } else if (strcmp (element_name, "key") == 0) { |
---|
129 | info->in_key = TRUE; |
---|
130 | } else if (strcmp (element_name, "signature") == 0) { |
---|
131 | info->in_signature = TRUE; |
---|
132 | } else if (strcmp (element_name, "default") == 0) { |
---|
133 | info->in_default = TRUE; |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | static void |
---|
138 | add_schema_entry (ParserInfo *info) |
---|
139 | { |
---|
140 | /*g_debug ("Inserting entry %s", info->entry->key);*/ |
---|
141 | |
---|
142 | info->list = g_slist_prepend (info->list, info->entry); |
---|
143 | } |
---|
144 | |
---|
145 | static void |
---|
146 | end_element_cb (GMarkupParseContext *ctx, |
---|
147 | const char *element_name, |
---|
148 | gpointer user_data, |
---|
149 | GError **error) |
---|
150 | { |
---|
151 | ParserInfo *info; |
---|
152 | |
---|
153 | info = (ParserInfo *) user_data; |
---|
154 | |
---|
155 | /*g_debug ("parsing end: '%s'", element_name);*/ |
---|
156 | |
---|
157 | if (strcmp (element_name, "schema") == 0) { |
---|
158 | add_schema_entry (info); |
---|
159 | } else if (strcmp (element_name, "key") == 0) { |
---|
160 | info->in_key = FALSE; |
---|
161 | } else if (strcmp (element_name, "signature") == 0) { |
---|
162 | info->in_signature = FALSE; |
---|
163 | } else if (strcmp (element_name, "default") == 0) { |
---|
164 | info->in_default = FALSE; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | static void |
---|
169 | text_cb (GMarkupParseContext *ctx, |
---|
170 | const char *text, |
---|
171 | gsize text_len, |
---|
172 | gpointer user_data, |
---|
173 | GError **error) |
---|
174 | { |
---|
175 | ParserInfo *info; |
---|
176 | char *t; |
---|
177 | |
---|
178 | info = (ParserInfo *) user_data; |
---|
179 | |
---|
180 | t = g_strndup (text, text_len); |
---|
181 | |
---|
182 | if (info->in_key) { |
---|
183 | info->entry->key = g_strdup (t); |
---|
184 | } else if (info->in_signature) { |
---|
185 | info->entry->signature = g_strdup (t); |
---|
186 | } else if (info->in_default) { |
---|
187 | info->entry->default_value = g_strdup (t); |
---|
188 | } |
---|
189 | |
---|
190 | g_free (t); |
---|
191 | |
---|
192 | } |
---|
193 | |
---|
194 | static void |
---|
195 | error_cb (GMarkupParseContext *ctx, |
---|
196 | GError *error, |
---|
197 | gpointer user_data) |
---|
198 | { |
---|
199 | } |
---|
200 | |
---|
201 | static GMarkupParser parser = { |
---|
202 | start_element_cb, |
---|
203 | end_element_cb, |
---|
204 | text_cb, |
---|
205 | NULL, |
---|
206 | error_cb |
---|
207 | }; |
---|
208 | |
---|
209 | gboolean |
---|
210 | gdm_settings_parse_schemas (const char *file, |
---|
211 | const char *root, |
---|
212 | GSList **schemas) |
---|
213 | { |
---|
214 | GMarkupParseContext *ctx; |
---|
215 | ParserInfo *info; |
---|
216 | char *contents; |
---|
217 | gsize len; |
---|
218 | GError *error; |
---|
219 | gboolean res; |
---|
220 | |
---|
221 | g_return_val_if_fail (file != NULL, FALSE); |
---|
222 | g_return_val_if_fail (root != NULL, FALSE); |
---|
223 | |
---|
224 | g_assert (schemas != NULL); |
---|
225 | |
---|
226 | contents = NULL; |
---|
227 | error = NULL; |
---|
228 | res = g_file_get_contents (file, &contents, &len, &error); |
---|
229 | if (! res) { |
---|
230 | g_warning ("Unable to read schemas file: %s", error->message); |
---|
231 | g_error_free (error); |
---|
232 | return FALSE; |
---|
233 | } |
---|
234 | |
---|
235 | info = g_new0 (ParserInfo, 1); |
---|
236 | ctx = g_markup_parse_context_new (&parser, 0, info, NULL); |
---|
237 | g_markup_parse_context_parse (ctx, contents, len, NULL); |
---|
238 | |
---|
239 | *schemas = info->list; |
---|
240 | |
---|
241 | g_markup_parse_context_free (ctx); |
---|
242 | g_free (info); |
---|
243 | g_free (contents); |
---|
244 | |
---|
245 | return TRUE; |
---|
246 | } |
---|
247 | |
---|
248 | char * |
---|
249 | gdm_settings_parse_double_as_value (gdouble doubleval) |
---|
250 | { |
---|
251 | char result[G_ASCII_DTOSTR_BUF_SIZE]; |
---|
252 | |
---|
253 | g_ascii_dtostr (result, sizeof (result), doubleval); |
---|
254 | |
---|
255 | return g_strdup (result); |
---|
256 | } |
---|
257 | |
---|
258 | char * |
---|
259 | gdm_settings_parse_integer_as_value (int intval) |
---|
260 | |
---|
261 | { |
---|
262 | return g_strdup_printf ("%d", intval); |
---|
263 | } |
---|
264 | |
---|
265 | char * |
---|
266 | gdm_settings_parse_boolean_as_value (gboolean boolval) |
---|
267 | { |
---|
268 | if (boolval) { |
---|
269 | return g_strdup ("true"); |
---|
270 | } else { |
---|
271 | return g_strdup ("false"); |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | |
---|
276 | /* adapted from GKeyFile */ |
---|
277 | gboolean |
---|
278 | gdm_settings_parse_value_as_boolean (const char *value, |
---|
279 | gboolean *bool) |
---|
280 | { |
---|
281 | if (g_ascii_strcasecmp (value, "true") == 0 || strcmp (value, "1") == 0) { |
---|
282 | *bool = TRUE; |
---|
283 | return TRUE; |
---|
284 | } else if (g_ascii_strcasecmp (value, "false") == 0 || strcmp (value, "0") == 0) { |
---|
285 | *bool = FALSE; |
---|
286 | return TRUE; |
---|
287 | } else { |
---|
288 | return FALSE; |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | gboolean |
---|
293 | gdm_settings_parse_value_as_integer (const char *value, |
---|
294 | int *intval) |
---|
295 | { |
---|
296 | char *end_of_valid_int; |
---|
297 | glong long_value; |
---|
298 | gint int_value; |
---|
299 | |
---|
300 | errno = 0; |
---|
301 | long_value = strtol (value, &end_of_valid_int, 10); |
---|
302 | |
---|
303 | if (*value == '\0' || *end_of_valid_int != '\0') { |
---|
304 | return FALSE; |
---|
305 | } |
---|
306 | |
---|
307 | int_value = long_value; |
---|
308 | if (int_value != long_value || errno == ERANGE) { |
---|
309 | return FALSE; |
---|
310 | } |
---|
311 | |
---|
312 | *intval = int_value; |
---|
313 | |
---|
314 | return TRUE; |
---|
315 | } |
---|
316 | |
---|
317 | gboolean |
---|
318 | gdm_settings_parse_value_as_double (const char *value, |
---|
319 | gdouble *doubleval) |
---|
320 | { |
---|
321 | char *end_of_valid_d; |
---|
322 | gdouble double_value = 0; |
---|
323 | |
---|
324 | double_value = g_ascii_strtod (value, &end_of_valid_d); |
---|
325 | |
---|
326 | if (*end_of_valid_d != '\0' || end_of_valid_d == value) { |
---|
327 | return FALSE; |
---|
328 | } |
---|
329 | |
---|
330 | *doubleval = double_value; |
---|
331 | return TRUE; |
---|
332 | } |
---|