]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-plist.c
UOA: Do not segfault when "Done" or "Cancel" button clicked but widget is not ready yet
[empathy.git] / libempathy-gtk / empathy-plist.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2008 Christophe Fergeau <teuf@gnome.org>
4  * Based on itdb_plist parser from the gtkpod project.
5  *
6  * The code contained in this file is free software; you can redistribute
7  * it and/or modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either version
9  * 2.1 of the License, or (at your option) any later version.
10  *
11  * This file is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this code; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24 #include <libxml/tree.h>
25 #include <telepathy-glib/telepathy-glib.h>
26
27 #include "empathy-plist.h"
28
29 static GValue *empathy_plist_parse_node (xmlNode *a_node);
30
31 static GValue *
32 empathy_plist_parse_integer (xmlNode *a_node)
33 {
34         char *str_val;
35         char *end_ptr;
36         gint int_val;
37
38         str_val = (char *) xmlNodeGetContent (a_node);
39         int_val = strtol (str_val, &end_ptr, 0);
40         if (*end_ptr != '\0') {
41                 xmlFree (str_val);
42                 return NULL;
43         }
44         xmlFree (str_val);
45
46         return tp_g_value_slice_new_int (int_val);
47 }
48
49 static GValue *
50 empathy_plist_parse_string (xmlNode *a_node)
51 {
52         char *str_val;
53         GValue *value;
54
55         str_val = (char *) xmlNodeGetContent (a_node);
56
57         value = tp_g_value_slice_new_string (str_val);
58
59         xmlFree (str_val);
60
61         return value;
62 }
63
64 static GValue *
65 empathy_plist_parse_real (xmlNode *a_node)
66 {
67         char *str_val;
68         char *end_ptr;
69         gdouble double_val;
70
71         str_val = (char *) xmlNodeGetContent (a_node);
72         double_val = g_ascii_strtod (str_val, &end_ptr);
73         if (*end_ptr != '\0') {
74                 xmlFree (str_val);
75                 return NULL;
76         }
77         xmlFree (str_val);
78
79         return tp_g_value_slice_new_double (double_val);
80 }
81
82 static GValue *
83 empathy_plist_parse_boolean (xmlNode *a_node)
84 {
85         gboolean bool_val;
86
87         if (strcmp ((char *) a_node->name, "true") == 0) {
88                 bool_val = TRUE;
89         } else if (strcmp ((char *) a_node->name, "false") == 0) {
90                 bool_val = FALSE;
91         } else {
92                 return NULL;
93         }
94
95         return tp_g_value_slice_new_boolean (bool_val);
96 }
97
98 static GValue *
99 empathy_plist_parse_data (xmlNode *a_node)
100 {
101         char *str_val;
102         guchar *raw_data;
103         gsize len;
104         GValue *value;
105
106         str_val = (char *) xmlNodeGetContent (a_node);
107         raw_data = g_base64_decode (str_val, &len);
108         xmlFree (str_val);
109
110         value = tp_g_value_slice_new_bytes (len, raw_data);
111
112         g_free (raw_data);
113
114         return value;
115 }
116
117 static xmlNode *
118 empathy_plist_parse_one_dict_entry (xmlNode *a_node, GHashTable *dict)
119 {
120         xmlNode *cur_node = a_node;
121         xmlChar *key_name;
122         GValue *value;
123
124         while (cur_node &&
125                (xmlStrcmp (cur_node->name, (xmlChar *) "key") != 0)) {
126                 cur_node = cur_node->next;
127         }
128         if (!cur_node) {
129                 return NULL;
130         }
131         key_name = xmlNodeGetContent (cur_node);
132         cur_node = cur_node->next;
133         while (cur_node && xmlIsBlankNode (cur_node)) {
134                 cur_node = cur_node->next;
135         }
136         if (!cur_node) {
137                 xmlFree (key_name);
138                 return NULL;
139         }
140
141         value = empathy_plist_parse_node (cur_node);
142         if (value) {
143                 g_hash_table_insert (dict, g_strdup ((char *) key_name), value);
144         }
145         xmlFree (key_name);
146
147         return cur_node->next;
148 }
149
150 static GValue *
151 empathy_plist_parse_dict (xmlNode *a_node)
152 {
153         xmlNode *cur_node = a_node->children;
154         GHashTable *dict;
155
156         dict = g_hash_table_new_full (g_str_hash, g_str_equal,
157                                       g_free, (GDestroyNotify) tp_g_value_slice_free);
158
159         while (cur_node) {
160                 if (xmlIsBlankNode (cur_node)) {
161                         cur_node = cur_node->next;
162                 } else {
163                         cur_node = empathy_plist_parse_one_dict_entry (cur_node, dict);
164                 }
165         }
166
167         return tp_g_value_slice_new_take_boxed (G_TYPE_HASH_TABLE, dict);
168 }
169
170 typedef GValue *(*ParseCallback) (xmlNode *);
171
172 struct Parser {
173         const char * const type_name;
174         ParseCallback parser;
175 };
176
177 static const struct Parser parsers[] = { {"integer", empathy_plist_parse_integer},
178                                          {"real",    empathy_plist_parse_real},
179                                          {"string",  empathy_plist_parse_string},
180                                          {"true",    empathy_plist_parse_boolean},
181                                          {"false",   empathy_plist_parse_boolean},
182                                          {"data",    empathy_plist_parse_data},
183                                          {"dict",    empathy_plist_parse_dict},
184                                          {NULL,   NULL} };
185
186 static ParseCallback
187 empathy_plist_get_parser_for_type (const xmlChar *type)
188 {
189         guint i = 0;
190
191         while (parsers[i].type_name) {
192                 if (xmlStrcmp (type, (xmlChar *) parsers[i].type_name) == 0) {
193                         if (parsers[i].parser) {
194                                 return parsers[i].parser;
195                         }
196                 }
197                 i++;
198         }
199         return NULL;
200 }
201
202 static GValue *
203 empathy_plist_parse_node (xmlNode *a_node)
204 {
205         ParseCallback parser;
206
207         g_return_val_if_fail (a_node != NULL, NULL);
208         parser = empathy_plist_get_parser_for_type (a_node->name);
209         if (parser) {
210                 return parser (a_node);
211         } else {
212                 return NULL;
213         }
214 }
215
216 static GValue *
217 empathy_plist_parse (xmlNode * a_node)
218 {
219         xmlNode *cur_node;
220
221         if (!a_node) {
222                 return NULL;
223         }
224         if (xmlStrcmp (a_node->name, (xmlChar *) "plist") != 0) {
225                 return NULL;
226         }
227         cur_node = a_node->xmlChildrenNode;
228         while (cur_node && (xmlIsBlankNode (cur_node))) {
229                 cur_node = cur_node->next;
230         }
231         if (cur_node) {
232                 return empathy_plist_parse_node (cur_node);
233         }
234
235         return NULL;
236 }
237
238 /**
239  * empathy_plist_parse_from_file:
240  * @filename: file containing XML plist data to parse
241  *
242  * Parses the XML plist file. If an error occurs during the parsing,
243  * empathy_plist_parse_from_file() will return NULL.
244  *
245  * Returns: NULL on error, a newly allocated
246  * #GValue otherwise. Free it using tp_g_value_slice_free()
247  */
248 GValue *
249 empathy_plist_parse_from_file (const char *filename)
250 {
251         xmlDoc *doc = NULL;
252         xmlNode *root_element = NULL;
253         GValue *parsed_doc;
254
255         doc = xmlReadFile (filename, NULL, 0);
256
257         if (!doc) {
258                 return NULL;
259         }
260
261         root_element = xmlDocGetRootElement (doc);
262
263         parsed_doc = empathy_plist_parse (root_element);
264
265         xmlFreeDoc (doc);
266
267         return parsed_doc;
268 }
269
270 /**
271  * empathy_plist_parse_from_memory:
272  * @data:   memory location containing XML plist data to parse
273  * @len:        length in bytes of the string to parse
274  *
275  * Parses the XML plist file stored in @data which length is @len
276  * bytes. If an error occurs during the parsing,
277  * empathy_plist_parse_from_memory() will return NULL.
278  *
279  * Returns: NULL on error, a newly allocated
280  * #GValue otherwise. Free it using tp_g_value_slice_free()
281  */
282 GValue *
283 empathy_plist_parse_from_memory (const char *data, gsize len)
284 {
285         xmlDoc *doc = NULL;
286         xmlNode *root_element = NULL;
287         GValue *parsed_doc;
288
289         doc = xmlReadMemory (data, len, "noname.xml", NULL, 0);
290
291         if (doc == NULL) {
292                 return NULL;
293         }
294
295         root_element = xmlDocGetRootElement (doc);
296
297         parsed_doc = empathy_plist_parse (root_element);
298
299         xmlFreeDoc (doc);
300
301         return parsed_doc;
302 }
303