aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-bconf-map.c
blob: 785e5033f343867010e3c3e62bb64a95db900444 (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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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 General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */


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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <glib.h>

#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>

#include "e-bconf-map.h"


#define d(x)


static signed char hexnib[256] = {
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
     0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
    -1,10,11,12,13,14,15,16,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,10,11,12,13,14,15,16,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
};

char *
e_bconf_hex_decode (const char *val)
{
    const unsigned char *p = (const unsigned char *) val;
    char *o, *res;

    o = res = g_malloc (strlen (val) / 2 + 1);
    for (p = (const unsigned char *)val; (p[0] && p[1]); p += 2)
        *o++ = (hexnib[p[0]] << 4) | hexnib[p[1]];
    *o = 0;

    return res;
}

char *
e_bconf_url_decode (const char *val)
{
    const unsigned char *p = (const unsigned char *) val;
    char *o, *res, c;

    o = res = g_malloc (strlen (val) + 1);
    while (*p) {
        c = *p++;
        if (c == '%'
            && hexnib[p[0]] != -1 && hexnib[p[1]] != -1) {
            *o++ = (hexnib[p[0]] << 4) | hexnib[p[1]];
            p+=2;
        } else
            *o++ = c;
    }
    *o = 0;

    return res;
}


xmlNodePtr
e_bconf_get_path (xmlDocPtr doc, const char *path)
{
    xmlNodePtr root;
    char *val;
    int found;

    root = doc->children;
    if (strcmp ((char *)root->name, "bonobo-config") != 0) {
        g_warning ("not bonobo-config xml file");
        return NULL;
    }

    root = root->children;
    while (root) {
        if (!strcmp ((char *)root->name, "section")) {
            val = (char *)xmlGetProp (root, (const unsigned char *)"path");
            found = val && strcmp (val, path) == 0;
            xmlFree (val);
            if (found)
                break;
        }
        root = root->next;
    }

    return root;
}

xmlNodePtr
e_bconf_get_entry (xmlNodePtr root, const char *name)
{
    xmlNodePtr node = root->children;
    int found;
    char *val;

    while (node) {
        if (!strcmp ((char *)node->name, "entry")) {
            val = (char *)xmlGetProp (node, (const unsigned char *)"name");
            found = val && strcmp (val, name) == 0;
            xmlFree (val);
            if (found)
                break;
        }
        node = node->next;
    }

    return node;
}

char *
e_bconf_get_value (xmlNodePtr root, const char *name)
{
    xmlNodePtr node = e_bconf_get_entry (root, name);
    char *prop, *val = NULL;

    if (node && (prop = (char *)xmlGetProp (node, (const unsigned char *)"value"))) {
        val = g_strdup (prop);
        xmlFree (prop);
    }

    return val;
}

char *
e_bconf_get_bool (xmlNodePtr root, const char *name)
{
    char *val, *res;

    if ((val = e_bconf_get_value (root, name))) {
        res = g_strdup (val[0] == '1' ? "true" : "false");
        g_free (val);
    } else
        res = NULL;

    return res;
}

char *
e_bconf_get_long (xmlNodePtr root, const char *name)
{
    char *val, *res;

    if ((val = e_bconf_get_value (root, name))) {
        res = g_strdup (val);
        g_free (val);
    } else
        res = NULL;

    return res;
}

char *
e_bconf_get_string (xmlNodePtr root, const char *name)
{
    char *val, *res;

    if ((val = e_bconf_get_value (root, name))) {
        res = e_bconf_hex_decode (val);
        g_free (val);
    } else
        res = NULL;

    return res;
}


/* lookup functions */
typedef char * (* bconf_lookup_func) (xmlNodePtr root, const char *name, e_bconf_map_t *nap);

static char *
bconf_lookup_bool (xmlNodePtr root, const char *name, e_bconf_map_t *map)
{
    return e_bconf_get_bool (root, name);
}

static char *
bconf_lookup_long (xmlNodePtr root, const char *name, e_bconf_map_t *map)
{
    return e_bconf_get_long (root, name);
}

static char *
bconf_lookup_string (xmlNodePtr root, const char *name, e_bconf_map_t *map)
{
    return e_bconf_get_string (root, name);
}

static char *
bconf_lookup_enum (xmlNodePtr root, const char *name, e_bconf_map_t *map)
{
    int index = 0, i;
    char *val;

    if ((val = e_bconf_get_value (root, name))) {
        index = atoi (val);
        g_free (val);
    }

    for (i = 0; map->child[i].from; i++) {
        if (i == index)
            return g_strdup (map->child[i].from);
    }

    return NULL;
}


static bconf_lookup_func lookup_table[] = {
    bconf_lookup_bool, bconf_lookup_long, bconf_lookup_string, bconf_lookup_enum
};

static char *
get_name (const char *in, int index)
{
    GString *out = g_string_new ("");
    char c, *res;

    while ((c = *in++)) {
        if (c == '%') {
            c = *in++;
            switch (c) {
            case '%':
                g_string_append_c (out, '%');
                break;
            case 'i':
                g_string_append_printf (out, "%d", index);
                break;
            }
        } else {
            g_string_append_c (out, c);
        }
    }

    res = out->str;
    g_string_free (out, FALSE);

    return res;
}

static void
build_xml (xmlNodePtr root, e_bconf_map_t *map, int index, xmlNodePtr source)
{
    char *name, *value;
    xmlNodePtr node;

    while (map->type != E_BCONF_MAP_END) {
        if ((map->type & E_BCONF_MAP_MASK) == E_BCONF_MAP_CHILD) {
            node = xmlNewChild (root, NULL, (unsigned char *)map->to, NULL);
            build_xml (node, map->child, index, source);
        } else {
            name = get_name (map->from, index);
            value = lookup_table[(map->type & E_BCONF_MAP_MASK) - 1] (source, name, map);

            d(printf ("key '%s=%s' -> ", name, value));

            if (map->type & E_BCONF_MAP_CONTENT) {
                if (value && value[0])
                    xmlNewTextChild (root, NULL, (unsigned char *)map->to, (unsigned char *)value);
            } else {
                xmlSetProp (root, (unsigned char *)map->to, (unsigned char *)value);
            }

            g_free (value);
            g_free (name);
        }
        map++;
    }
}


int
e_bconf_import_xml_blob (GConfClient *gconf, xmlDocPtr config_xmldb, e_bconf_map_t *map,
             const char *bconf_path, const char *gconf_path,
             const char *name, const char *idparam)
{
    xmlNodePtr source;
    int count = 0, i;
    GSList *list, *l;
    char *val;

    source = e_bconf_get_path (config_xmldb, bconf_path);
    if (source) {
        list = NULL;
        if ((val = e_bconf_get_value (source, "num"))) {
            count = atoi (val);
            g_free (val);
        }

        d(printf("Found %d blobs at %s\n", count, bconf_path));

        for (i = 0; i < count; i++) {
            xmlDocPtr doc;
            xmlNodePtr root;
            xmlChar *xmlbuf;
            int n;

            doc = xmlNewDoc ((const unsigned char *)"1.0");
            root = xmlNewDocNode (doc, NULL, (unsigned char *)name, NULL);
            xmlDocSetRootElement (doc, root);

            /* This could be set with a MAP_UID type ... */
            if (idparam) {
                char buf[16];

                sprintf (buf, "%d", i);
                xmlSetProp (root, (unsigned char *)idparam, (unsigned char *)buf);
            }

            build_xml (root, map, i, source);

            xmlDocDumpMemory (doc, &xmlbuf, &n);
            xmlFreeDoc (doc);

            list = g_slist_append (list, xmlbuf);
        }

        gconf_client_set_list (gconf, gconf_path, GCONF_VALUE_STRING, list, NULL);

        while (list) {
            l = list->next;
            xmlFree (list->data);
            g_slist_free_1 (list);
            list = l;
        }
    } else {
        g_warning ("could not find '%s' in old config database, skipping", bconf_path);
    }

    return 0;
}


static int gconf_type[] = { GCONF_VALUE_BOOL, GCONF_VALUE_BOOL, GCONF_VALUE_INT, GCONF_VALUE_STRING, GCONF_VALUE_STRING };

int
e_bconf_import (GConfClient *gconf, xmlDocPtr config_xmldb, e_gconf_map_list_t *remap_list)
{
    char *path, *val, *tmp;
    e_gconf_map_t *map;
    xmlNodePtr source;
    GSList *list, *l;
    char buf[32];
    int i, j, k;

    /* process all flat config */
    for (i = 0; remap_list[i].root; i++) {
        d(printf ("Path: %s\n", remap_list[i].root));
        if (!(source = e_bconf_get_path (config_xmldb, remap_list[i].root)))
            continue;

        map = remap_list[i].map;
        for (j = 0; map[j].from; j++) {
            if (map[j].type & E_GCONF_MAP_LIST) {
                /* collapse a multi-entry indexed field into a list */
                list = NULL;
                k = 0;
                do {
                    path = get_name (map[j].from, k);
                    val = e_bconf_get_value (source, path);
                    d(printf ("finding path '%s' = '%s'\n", path, val));
                    g_free (path);
                    if (val) {
                        switch (map[j].type & E_GCONF_MAP_MASK) {
                        case E_GCONF_MAP_BOOL:
                        case E_GCONF_MAP_INT:
                            list = g_slist_append (list, GINT_TO_POINTER (atoi (val)));
                            break;
                        case E_GCONF_MAP_STRING:
                            d(printf (" -> '%s'\n", e_bconf_hex_decode (val)));
                            list = g_slist_append (list, e_bconf_hex_decode (val));
                            break;
                        }

                        g_free (val);
                        k++;
                    }
                } while (val);

                if (list) {
                    path = g_strdup_printf ("/apps/evolution/%s", map[j].to);
                    gconf_client_set_list (gconf, path, gconf_type[map[j].type & E_GCONF_MAP_MASK], list, NULL);
                    g_free (path);
                    if ((map[j].type & E_GCONF_MAP_MASK) == E_GCONF_MAP_STRING)
                        g_slist_foreach (list, (GFunc) g_free, NULL);
                    g_slist_free (list);
                }

                continue;
            } else if (map[j].type == E_GCONF_MAP_ANYLIST) {
                val = NULL;
            } else {
                if (!(val = e_bconf_get_value (source, map[j].from)))
                    continue;
            }

            d(printf (" %s = '%s' -> %s [%d]\n",
                  map[j].from,
                  val == NULL ? "(null)" : val,
                  map[j].to,
                  map[j].type));

            path = g_strdup_printf ("/apps/evolution/%s", map[j].to);
            switch (map[j].type) {
            case E_GCONF_MAP_BOOL:
                gconf_client_set_bool (gconf, path, atoi (val), NULL);
                break;
            case E_GCONF_MAP_BOOLNOT:
                gconf_client_set_bool (gconf, path, !atoi (val), NULL);
                break;
            case E_GCONF_MAP_INT:
                gconf_client_set_int (gconf, path, atoi (val), NULL);
                break;
            case E_GCONF_MAP_STRING:
                tmp = e_bconf_hex_decode (val);
                gconf_client_set_string (gconf, path, tmp, NULL);
                g_free (tmp);
                break;
            case E_GCONF_MAP_SIMPLESTRING:
                gconf_client_set_string (gconf, path, val, NULL);
                break;
            case E_GCONF_MAP_FLOAT:
                gconf_client_set_float (gconf, path, strtod (val, NULL), NULL);
                break;
            case E_GCONF_MAP_STRLIST: {
                char *v = e_bconf_hex_decode (val);
                char **t = g_strsplit (v, " !<-->!", 8196);

                list = NULL;
                for (k = 0; t[k]; k++) {
                    list = g_slist_append (list, t[k]);
                    d(printf ("  [%d] = '%s'\n", k, t[k]));
                }

                gconf_client_set_list (gconf, path, GCONF_VALUE_STRING, list, NULL);
                g_slist_free (list);
                g_strfreev (t);
                g_free (v);
                break; }
            case E_GCONF_MAP_ANYLIST: {
                xmlNodePtr node = source->children;
                list = NULL;

                /* find the entry node */
                while (node) {
                    if (!strcmp ((char *)node->name, "entry")) {
                        int found;

                        if ((tmp = (char *)xmlGetProp (node, (const unsigned char *)"name"))) {
                            found = strcmp ((char *)tmp, map[j].from) == 0;
                            xmlFree (tmp);
                            if (found)
                                break;
                        }
                    }

                    node = node->next;
                }

                /* find the the any block */
                if (node) {
                    node = node->children;
                    while (node) {
                        if (strcmp ((char *)node->name, "any") == 0)
                            break;
                        node = node->next;
                    }
                }

                /* skip to the value inside it */
                if (node) {
                    node = node->children;
                    while (node) {
                        if (strcmp ((char *)node->name, "value") == 0)
                            break;
                        node = node->next;
                    }
                }

                if (node) {
                    node = node->children;
                    while (node) {
                        if (strcmp ((char *)node->name, "value") == 0)
                            list = g_slist_append (list, xmlNodeGetContent (node));
                        node = node->next;
                    }
                }

                /* & store */
                if (list) {
                    gconf_client_set_list (gconf, path, GCONF_VALUE_STRING, list, NULL);
                    while (list) {
                        l = list->next;
                        xmlFree (list->data);
                        g_slist_free_1 (list);
                        list = l;
                    }
                }

                break; }
            case E_GCONF_MAP_COLOUR:
                sprintf (buf, "#%06x", atoi (val) & 0xffffff);
                gconf_client_set_string (gconf, path, buf, NULL);
                break;
            }

            /* FIXME: handle errors */
            g_free (path);
            g_free (val);
        }
    }

    return 0;
}