aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-xml-utils.c
blob: c3f9a03ce802717480517344f991b4570de06d6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Authors:
 *      Chris Lahey <clahey@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-xml-utils.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <locale.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <math.h>

#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>

#include "e-misc-utils.h"

/* Returns the first child with the name child_name and the "lang"
 * attribute that matches the current LC_MESSAGES, or else, the first
 * child with the name child_name and no "lang" attribute.
 */
xmlNode *
e_xml_get_child_by_name_by_lang (const xmlNode *parent,
                                 const xmlChar *child_name,
                                 const gchar *lang)
{
#ifdef G_OS_WIN32
    gchar *freeme = NULL;
#endif
    xmlNode *child;
    /* This is the default version of the string. */
    xmlNode *C = NULL;

    g_return_val_if_fail (parent != NULL, NULL);
    g_return_val_if_fail (child_name != NULL, NULL);

    if (lang == NULL) {
#ifndef G_OS_WIN32
#ifdef HAVE_LC_MESSAGES
        lang = setlocale (LC_MESSAGES, NULL);
#else
        lang = setlocale (LC_CTYPE, NULL);
#endif
#else
        lang = freeme = g_win32_getlocale ();
#endif
    }
    for (child = parent->xmlChildrenNode; child != NULL; child = child->next) {
        if (xmlStrcmp (child->name, child_name) == 0) {
            xmlChar *this_lang = xmlGetProp (
                child, (const guchar *)"lang");
            if (this_lang == NULL) {
                C = child;
            } else if (xmlStrcmp (this_lang, (xmlChar *) lang) == 0) {
#ifdef G_OS_WIN32
                g_free (freeme);
#endif
                return child;
            }
        }
    }
#ifdef G_OS_WIN32
    g_free (freeme);
#endif
    return C;
}

static xmlNode *
e_xml_get_child_by_name_by_lang_list_with_score (const xmlNode *parent,
                                                 const gchar *name,
                                                 const GList *lang_list,
                                                 gint *best_lang_score)
{
    xmlNodePtr best_node = NULL, node;

    for (node = parent->xmlChildrenNode; node != NULL; node = node->next) {
        xmlChar *lang;

        if (node->name == NULL || strcmp ((gchar *) node->name, name) != 0) {
            continue;
        }
        lang = xmlGetProp (node, (const guchar *)"xml:lang");
        if (lang != NULL) {
            const GList *l;
            gint i;

            for (l = lang_list, i = 0;
                 l != NULL && i < *best_lang_score;
                 l = l->next, i++) {
                if (strcmp ((gchar *) l->data, (gchar *) lang) == 0) {
                    best_node = node;
                    *best_lang_score = i;
                }
            }
        } else {
            if (best_node == NULL) {
                best_node = node;
            }
        }
        xmlFree (lang);
        if (*best_lang_score == 0) {
            return best_node;
        }
    }

    return best_node;
}

xmlNode *
e_xml_get_child_by_name_by_lang_list (const xmlNode *parent,
                                      const gchar *name,
                                      const GList *lang_list)
{
    gint best_lang_score = INT_MAX;

    g_return_val_if_fail (parent != NULL, NULL);
    g_return_val_if_fail (name != NULL, NULL);

    if (lang_list == NULL) {
        const gchar * const *language_names;

        language_names = g_get_language_names ();
        while (*language_names != NULL)
            lang_list = g_list_append (
                (GList *) lang_list, (gchar *) * language_names++);
    }
    return e_xml_get_child_by_name_by_lang_list_with_score
        (parent,name,
         lang_list,
         &best_lang_score);
}

xmlNode *
e_xml_get_child_by_name_no_lang (const xmlNode *parent,
                                 const gchar *name)
{
    xmlNodePtr node;

    g_return_val_if_fail (parent != NULL, NULL);
    g_return_val_if_fail (name != NULL, NULL);

    for (node = parent->xmlChildrenNode; node != NULL; node = node->next) {
        xmlChar *lang;

        if (node->name == NULL || strcmp ((gchar *) node->name, name) != 0) {
            continue;
        }
        lang = xmlGetProp (node, (const guchar *)"xml:lang");
        if (lang == NULL) {
            return node;
        }
        xmlFree (lang);
    }

    return NULL;
}

gint
e_xml_get_integer_prop_by_name (const xmlNode *parent,
                                const xmlChar *prop_name)
{
    g_return_val_if_fail (parent != NULL, 0);
    g_return_val_if_fail (prop_name != NULL, 0);

    return e_xml_get_integer_prop_by_name_with_default (parent, prop_name, 0);
}

gint
e_xml_get_integer_prop_by_name_with_default (const xmlNode *parent,
                                             const xmlChar *prop_name,
                                             gint def)
{
    xmlChar *prop;
    gint ret_val = def;

    g_return_val_if_fail (parent != NULL, 0);
    g_return_val_if_fail (prop_name != NULL, 0);

    prop = xmlGetProp ((xmlNode *) parent, prop_name);
    if (prop != NULL) {
        (void) sscanf ((gchar *) prop, "%d", &ret_val);
        xmlFree (prop);
    }
    return ret_val;
}

void
e_xml_set_integer_prop_by_name (xmlNode *parent,
                                const xmlChar *prop_name,
                                gint value)
{
    gchar *valuestr;

    g_return_if_fail (parent != NULL);
    g_return_if_fail (prop_name != NULL);

    valuestr = g_strdup_printf ("%d", value);
    xmlSetProp (parent, prop_name, (guchar *) valuestr);
    g_free (valuestr);
}

guint
e_xml_get_uint_prop_by_name (const xmlNode *parent,
                             const xmlChar *prop_name)
{
    g_return_val_if_fail (parent != NULL, 0);
    g_return_val_if_fail (prop_name != NULL, 0);

    return e_xml_get_uint_prop_by_name_with_default (parent, prop_name, 0);
}

guint
e_xml_get_uint_prop_by_name_with_default (const xmlNode *parent,
                                          const xmlChar *prop_name,
                                          guint def)
{
    xmlChar *prop;
    guint ret_val = def;

    g_return_val_if_fail (parent != NULL, 0);
    g_return_val_if_fail (prop_name != NULL, 0);

    prop = xmlGetProp ((xmlNode *) parent, prop_name);
    if (prop != NULL) {
        (void) sscanf ((gchar *) prop, "%u", &ret_val);
        xmlFree (prop);
    }
    return ret_val;
}

void
e_xml_set_uint_prop_by_name (xmlNode *parent,
                             const xmlChar *prop_name,
                             guint value)
{
    gchar *valuestr;

    g_return_if_fail (parent != NULL);
    g_return_if_fail (prop_name != NULL);

    valuestr = g_strdup_printf ("%u", value);
    xmlSetProp (parent, prop_name, (guchar *) valuestr);
    g_free (valuestr);
}

gboolean
e_xml_get_bool_prop_by_name (const xmlNode *parent,
                             const xmlChar *prop_name)
{
    g_return_val_if_fail (parent != NULL, 0);
    g_return_val_if_fail (prop_name != NULL, 0);

    return e_xml_get_bool_prop_by_name_with_default (
        parent, prop_name, FALSE);
}

gboolean
e_xml_get_bool_prop_by_name_with_default (const xmlNode *parent,
                                          const xmlChar *prop_name,
                                          gboolean def)
{
    xmlChar *prop;
    gboolean ret_val = def;

    g_return_val_if_fail (parent != NULL, 0);
    g_return_val_if_fail (prop_name != NULL, 0);

    prop = xmlGetProp ((xmlNode *) parent, prop_name);
    if (prop != NULL) {
        if (g_ascii_strcasecmp ((gchar *) prop, "true") == 0) {
            ret_val = TRUE;
        } else if (g_ascii_strcasecmp ((gchar *) prop, "false") == 0) {
            ret_val = FALSE;
        }
        xmlFree (prop);
    }
    return ret_val;
}

void
e_xml_set_bool_prop_by_name (xmlNode *parent,
                             const xmlChar *prop_name,
                             gboolean value)
{
    g_return_if_fail (parent != NULL);
    g_return_if_fail (prop_name != NULL);

    if (value) {
        xmlSetProp (parent, prop_name, (const guchar *)"true");
    } else {
        xmlSetProp (parent, prop_name, (const guchar *)"false");
    }
}

gdouble
e_xml_get_double_prop_by_name (const xmlNode *parent,
                               const xmlChar *prop_name)
{
    g_return_val_if_fail (parent != NULL, 0);
    g_return_val_if_fail (prop_name != NULL, 0);

    return e_xml_get_double_prop_by_name_with_default (parent, prop_name, 0.0);
}

gdouble
e_xml_get_double_prop_by_name_with_default (const xmlNode *parent,
                                            const xmlChar *prop_name,
                                            gdouble def)
{
    xmlChar *prop;
    gdouble ret_val = def;

    g_return_val_if_fail (parent != NULL, 0);
    g_return_val_if_fail (prop_name != NULL, 0);

    prop = xmlGetProp ((xmlNode *) parent, prop_name);
    if (prop != NULL) {
        ret_val = e_flexible_strtod ((gchar *) prop, NULL);
        xmlFree (prop);
    }
    return ret_val;
}

void
e_xml_set_double_prop_by_name (xmlNode *parent,
                               const xmlChar *prop_name,
                               gdouble value)
{
    gchar buffer[E_ASCII_DTOSTR_BUF_SIZE];
    gchar *format;

    g_return_if_fail (parent != NULL);
    g_return_if_fail (prop_name != NULL);

    if (fabs (value) < 1e9 && fabs (value) > 1e-5) {
        format = g_strdup_printf ("%%.%df", DBL_DIG);
    } else {
        format = g_strdup_printf ("%%.%dg", DBL_DIG);
    }
    e_ascii_dtostr (buffer, sizeof (buffer), format, value);
    g_free (format);

    xmlSetProp (parent, prop_name, (const guchar *) buffer);
}

gchar *
e_xml_get_string_prop_by_name (const xmlNode *parent,
                               const xmlChar *prop_name)
{
    g_return_val_if_fail (parent != NULL, NULL);
    g_return_val_if_fail (prop_name != NULL, NULL);

    return e_xml_get_string_prop_by_name_with_default (parent, prop_name, NULL);
}

gchar *
e_xml_get_string_prop_by_name_with_default (const xmlNode *parent,
                                            const xmlChar *prop_name,
                                            const gchar *def)
{
    xmlChar *prop;
    gchar *ret_val;

    g_return_val_if_fail (parent != NULL, NULL);
    g_return_val_if_fail (prop_name != NULL, NULL);

    prop = xmlGetProp ((xmlNode *) parent, prop_name);
    if (prop != NULL) {
        ret_val = g_strdup ((gchar *) prop);
        xmlFree (prop);
    } else {
        ret_val = g_strdup (def);
    }
    return ret_val;
}

void
e_xml_set_string_prop_by_name (xmlNode *parent,
                               const xmlChar *prop_name,
                               const gchar *value)
{
    g_return_if_fail (parent != NULL);
    g_return_if_fail (prop_name != NULL);

    if (value != NULL) {
        xmlSetProp (parent, prop_name, (guchar *) value);
    }
}

gchar *
e_xml_get_translated_string_prop_by_name (const xmlNode *parent,
                                          const xmlChar *prop_name)
{
    xmlChar *prop;
    gchar *ret_val = NULL;
    gchar *combined_name;

    g_return_val_if_fail (parent != NULL, NULL);
    g_return_val_if_fail (prop_name != NULL, NULL);

    prop = xmlGetProp ((xmlNode *) parent, prop_name);
    if (prop != NULL) {
        ret_val = g_strdup ((gchar *) prop);
        xmlFree (prop);
        return ret_val;
    }

    combined_name = g_strdup_printf ("_%s", prop_name);
    prop = xmlGetProp ((xmlNode *) parent, (guchar *) combined_name);
    if (prop != NULL) {
        ret_val = g_strdup (gettext ((gchar *) prop));
        xmlFree (prop);
    }
    g_free (combined_name);

    return ret_val;
}