goa: Add missing linker flag (for real).
[platform/upstream/evolution-data-server.git] / libedataserver / e-xml-hash-utils.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU Lesser General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <libxml/entities.h>
27 #include <libxml/tree.h>
28 #include <libxml/xmlmemory.h>
29
30 #include <glib/gstdio.h>
31
32 #include "e-xml-hash-utils.h"
33 #include "e-xml-utils.h"
34
35 /**
36  * e_xml_to_hash:
37  * @doc: The #xmlDoc to store in a hash table.
38  * @type: The value type to use as a key in the hash table.
39  *
40  * Creates a #GHashTable representation of the #xmlDoc @doc.
41  * If @type is * @E_XML_HASH_TYPE_PROPERTY, all XML nodes will be
42  * indexed in the #GHashTable by name. If @type is
43  * %E_XML_HASH_TYPE_OBJECT_UID, then XML objects will be indexed in
44  * the hash by their UID (other nodes will still be indexed by name).
45  *
46  * Returns: (transfer full): The newly-created #GHashTable representation
47  * of @doc.
48  **/
49 GHashTable *
50 e_xml_to_hash (xmlDoc *doc,
51                EXmlHashType type)
52 {
53         xmlNode *root, *node;
54         xmlChar *key, *value;
55         GHashTable *hash;
56
57         hash = g_hash_table_new (g_str_hash, g_str_equal);
58
59         root = xmlDocGetRootElement (doc);
60         for (node = root->xmlChildrenNode; node; node = node->next) {
61                 if (node->name == NULL || node->type != XML_ELEMENT_NODE)
62                         continue;
63
64                 if (type == E_XML_HASH_TYPE_OBJECT_UID &&
65                     !strcmp ((gchar *) node->name, "object"))
66                         key = xmlGetProp (node, (xmlChar *)"uid");
67                 else
68                         key = xmlStrdup (node->name);
69
70                 if (!key) {
71                         g_warning ("Key not found!!");
72                         continue;
73                 }
74
75                 value = xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
76                 if (!value) {
77                         xmlFree (key);
78                         g_warning ("Found a key with no value!!");
79                         continue;
80                 }
81
82                 g_hash_table_insert (
83                         hash, g_strdup ((gchar *) key),
84                         g_strdup ((gchar *) value));
85
86                 xmlFree (key);
87                 xmlFree (value);
88         }
89
90         return hash;
91 }
92
93 struct save_data {
94         EXmlHashType type;
95         xmlDoc *doc;
96         xmlNode *root;
97 };
98
99 static void
100 foreach_save_func (gpointer key,
101                    gpointer value,
102                    gpointer user_data)
103 {
104         struct save_data *sd = user_data;
105         xmlNodePtr new_node;
106         xmlChar *enc;
107
108         if (sd->type == E_XML_HASH_TYPE_OBJECT_UID) {
109                 new_node = xmlNewNode (NULL, (xmlChar *)"object");
110                 xmlNewProp (new_node, (xmlChar *)"uid", (const xmlChar *) key);
111         } else
112                 new_node = xmlNewNode (NULL, (const xmlChar *) key);
113
114         enc = xmlEncodeEntitiesReentrant (sd->doc, value);
115         xmlNodeSetContent (new_node, enc);
116         xmlFree (enc);
117
118         xmlAddChild (sd->root, new_node);
119 }
120
121 /**
122  * e_xml_from_hash:
123  * @hash: The #GHashTable to extract the XML from
124  * @type: The #EXmlHashType used to store the XML
125  * @root_name: The name to call the new #xmlDoc
126  *
127  * Uses the key/value pair representation of an XML structure in @hash
128  * to build an equivalent #xmlDoc. This is the reverse of e_xml_to_hash().
129  *
130  * Returns: the #xmlDoc created from the data in @hash
131  **/
132 xmlDoc *
133 e_xml_from_hash (GHashTable *hash,
134                  EXmlHashType type,
135                  const gchar *root_name)
136 {
137         xmlDoc *doc;
138         struct save_data sd;
139
140         doc = xmlNewDoc ((xmlChar *)"1.0");
141         doc->encoding = xmlStrdup ((xmlChar *)"UTF-8");
142         sd.type = type;
143         sd.doc = doc;
144         sd.root = xmlNewDocNode (doc, NULL, (xmlChar *) root_name, NULL);
145         xmlDocSetRootElement (doc, sd.root);
146
147         g_hash_table_foreach (hash, foreach_save_func, &sd);
148         return doc;
149 }
150
151 static void
152 free_values (gpointer key,
153              gpointer value,
154              gpointer data)
155 {
156         g_free (key);
157         g_free (value);
158 }
159
160 /**
161  * e_xml_destroy_hash:
162  * @hash: the #GHashTable to destroy
163  *
164  * Frees the memory used by @hash and its contents.
165  **/
166 void
167 e_xml_destroy_hash (GHashTable *hash)
168 {
169         g_hash_table_foreach (hash, free_values, NULL);
170         g_hash_table_destroy (hash);
171 }
172
173 /**
174  * EXmlHash:
175  *
176  * A hash table representation of an XML file.
177  **/
178 struct EXmlHash {
179         gchar *filename;
180         GHashTable *objects;
181 };
182
183 /**
184  * e_xmlhash_new:
185  * @filename: the name of an XML file
186  *
187  * Creates a new #EXmlHash from the file @filename. If @filename does
188  * not already exist, an empty #EXmlHash will be created.
189  *
190  * Returns: The new #EXmlHash structure, or %NULL if unable to parse
191  *          @filename.
192  **/
193 EXmlHash *
194 e_xmlhash_new (const gchar *filename)
195 {
196         EXmlHash *hash;
197         xmlDoc *doc = NULL;
198
199         g_return_val_if_fail (filename != NULL, NULL);
200
201         hash = g_new0 (EXmlHash, 1);
202         hash->filename = g_strdup (filename);
203
204         if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
205                 doc = e_xml_parse_file (filename);
206                 if (!doc) {
207                         e_xmlhash_destroy (hash);
208
209                         return NULL;
210                 }
211                 hash->objects = e_xml_to_hash (doc, E_XML_HASH_TYPE_OBJECT_UID);
212                 xmlFreeDoc (doc);
213         } else {
214                 hash->objects = g_hash_table_new (g_str_hash, g_str_equal);
215         }
216
217         return hash;
218 }
219
220 /**
221  * e_xmlhash_add:
222  * @hash: the #EXmlHash to add an entry to
223  * @key: the key to use for the entry
224  * @data: the value of the new entry
225  *
226  * Adds a new key/value pair to the #EXmlHash @hash.
227  **/
228 void
229 e_xmlhash_add (EXmlHash *hash,
230                const gchar *key,
231                const gchar *data)
232 {
233         g_return_if_fail (hash != NULL);
234         g_return_if_fail (key != NULL);
235         g_return_if_fail (data != NULL);
236
237         e_xmlhash_remove (hash, key);
238         g_hash_table_insert (hash->objects, g_strdup (key), g_strdup (data));
239 }
240
241 /**
242  * e_xmlhash_remove:
243  * @hash: the #EXmlHash to remove an entry from
244  * @key: the key of the entry to remove
245  *
246  * Remove the entry in @hash with key equal to @key, if it exists.
247  **/
248 void
249 e_xmlhash_remove (EXmlHash *hash,
250                   const gchar *key)
251 {
252         gpointer orig_key;
253         gpointer orig_value;
254         gboolean found;
255
256         g_return_if_fail (hash != NULL);
257         g_return_if_fail (key != NULL);
258
259         found = g_hash_table_lookup_extended (
260                 hash->objects, key, &orig_key, &orig_value);
261         if (found) {
262                 g_hash_table_remove (hash->objects, key);
263                 g_free (orig_key);
264                 g_free (orig_value);
265         }
266 }
267
268 /**
269  * e_xmlhash_compare:
270  * @hash: the #EXmlHash to compare against
271  * @key: the key of the hash entry to compare with
272  * @compare_data: the data to compare against the hash entry
273  *
274  * Compares the value with key equal to @key in @hash against
275  * @compare_data.
276  *
277  * Returns: E_XMLHASH_STATUS_SAME if the value and @compare_data are
278  *          equal,E_XMLHASH_STATUS_DIFFERENT if they are different, or
279  *          E_XMLHASH_STATUS_NOT_FOUND if there is no entry in @hash with
280  *          its key equal to @key.
281  **/
282 EXmlHashStatus
283 e_xmlhash_compare (EXmlHash *hash,
284                    const gchar *key,
285                    const gchar *compare_data)
286 {
287         gchar *data;
288         gint rc;
289
290         g_return_val_if_fail (hash != NULL, E_XMLHASH_STATUS_NOT_FOUND);
291         g_return_val_if_fail (key != NULL, E_XMLHASH_STATUS_NOT_FOUND);
292         g_return_val_if_fail (compare_data != NULL, E_XMLHASH_STATUS_NOT_FOUND);
293
294         data = g_hash_table_lookup (hash->objects, key);
295         if (!data)
296                 return E_XMLHASH_STATUS_NOT_FOUND;
297
298         rc = strcmp (data, compare_data);
299         if (rc == 0)
300                 return E_XMLHASH_STATUS_SAME;
301
302         return E_XMLHASH_STATUS_DIFFERENT;
303 }
304
305 typedef struct {
306         EXmlHashFunc func;
307         gpointer user_data;
308 } foreach_data_t;
309
310 static void
311 foreach_hash_func (gpointer key,
312                    gpointer value,
313                    gpointer user_data)
314 {
315         foreach_data_t *data = (foreach_data_t *) user_data;
316
317         data->func ((const gchar *) key, (const gchar *) value, data->user_data);
318 }
319
320 /**
321  * e_xmlhash_foreach_key:
322  * @hash: an #EXmlHash
323  * @func: (scope async): the #EXmlHashFunc to execute on the data in @hash
324  * @user_data: the data to pass to @func
325  *
326  * Executes @func against each key/value pair in @hash.
327  **/
328 void
329 e_xmlhash_foreach_key (EXmlHash *hash,
330                        EXmlHashFunc func,
331                        gpointer user_data)
332 {
333         foreach_data_t data;
334
335         g_return_if_fail (hash != NULL);
336         g_return_if_fail (func != NULL);
337
338         data.func = func;
339         data.user_data = user_data;
340         g_hash_table_foreach (hash->objects, foreach_hash_func, &data);
341 }
342
343 /**
344  * e_xmlhash_foreach_key_remove:
345  * @hash: an #EXmlHash
346  * @func: (scope async): the #EXmlHashFunc to execute on the data in @hash
347  * @user_data: the data to pass to @func
348  *
349  * Calls g_hash_table_foreach_remove() on @hash<!-- -->'s internal hash
350  * table.  See g_hash_table_foreach_remove() for details.
351  **/
352 void
353 e_xmlhash_foreach_key_remove (EXmlHash *hash,
354                               EXmlHashRemoveFunc func,
355                               gpointer user_data)
356 {
357         g_return_if_fail (hash != NULL);
358         g_return_if_fail (func != NULL);
359
360         g_hash_table_foreach_remove (hash->objects, (GHRFunc) func, user_data);
361 }
362
363 /**
364  * e_xmlhash_write:
365  * @hash: The #EXmlHash to write.
366  *
367  * Writes the XML represented by @hash to the file originally passed
368  * to e_xmlhash_new().
369  **/
370 void
371 e_xmlhash_write (EXmlHash *hash)
372 {
373         xmlDoc *doc;
374
375         g_return_if_fail (hash != NULL);
376
377         doc = e_xml_from_hash (
378                 hash->objects, E_XML_HASH_TYPE_OBJECT_UID, "xmlhash");
379
380         e_xml_save_file (hash->filename, doc);
381
382         xmlFreeDoc (doc);
383 }
384
385 /**
386  * e_xmlhash_destroy:
387  * @hash: The #EXmlHash to destroy.
388  *
389  * Frees the memory associated with @hash.
390  **/
391 void
392 e_xmlhash_destroy (EXmlHash *hash)
393 {
394         g_return_if_fail (hash != NULL);
395
396         g_free (hash->filename);
397         if (hash->objects)
398                 e_xml_destroy_hash (hash->objects);
399
400         g_free (hash);
401 }