aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ephy-node-db.c
blob: 8f553491a64f3a81ac9f369f920a3f5aceb14377 (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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/* 
 *  Copyright (C) 2002 Jorn Baayen <jorn@nl.linux.org>
 *  Copyright (C) 2003 Marco Pesenti Gritti
 *  Copyright (C) 2003 Christian Persch
 *
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  $Id$
 */

#include "config.h"

#include "ephy-node-db.h"
#include "ephy-file-helpers.h"
#include "ephy-debug.h"

#include <libxml/xmlreader.h>
#include <libxml/xmlwriter.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>

static void ephy_node_db_class_init (EphyNodeDbClass *klass);
static void ephy_node_db_init (EphyNodeDb *node);
static void ephy_node_db_finalize (GObject *object);

/* FIXME I want to find a better way to deal with "root" nodes */
#define RESERVED_IDS 30

enum
{
    PROP_0,
    PROP_NAME,
    PROP_IMMUTABLE,
};

#define EPHY_NODE_DB_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_NODE_DB, EphyNodeDbPrivate))

struct _EphyNodeDbPrivate
{
    char *name;
    gboolean immutable;

    guint id_factory;

    GPtrArray *id_to_node;
};

static GHashTable *ephy_node_databases = NULL;

static GObjectClass *parent_class = NULL;

GType
ephy_node_db_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0))
    {
        static const GTypeInfo our_info =
        {
            sizeof (EphyNodeDbClass),
            NULL,
            NULL,
            (GClassInitFunc) ephy_node_db_class_init,
            NULL,
            NULL,
            sizeof (EphyNodeDb),
            0,
            (GInstanceInitFunc) ephy_node_db_init
        };

        type = g_type_register_static (G_TYPE_OBJECT,
                           "EphyNodeDb",
                           &our_info, 0);
    }

    return type;
}

static void
ephy_node_db_set_name (EphyNodeDb *db, const char *name)
{
    db->priv->name = g_strdup (name);

    if (ephy_node_databases == NULL)
    {
        ephy_node_databases = g_hash_table_new (g_str_hash, g_str_equal);
    }

    g_hash_table_insert (ephy_node_databases, db->priv->name, db);
}

static void
ephy_node_db_get_property (GObject *object,
                           guint prop_id,
                           GValue *value,
                           GParamSpec *pspec)
{
    EphyNodeDb *db;

    db = EPHY_NODE_DB (object);

    switch (prop_id)
    {
        case PROP_NAME:
            g_value_set_string (value, db->priv->name);
            break;
        case PROP_IMMUTABLE:
            g_value_set_boolean (value, ephy_node_db_is_immutable (db));
            break;
    }
}

static void
ephy_node_db_set_property (GObject *object,
                           guint prop_id,
                           const GValue *value,
                           GParamSpec *pspec)
{
    EphyNodeDb *db;

    db = EPHY_NODE_DB (object);

    switch (prop_id)
    {
        case PROP_NAME:
            ephy_node_db_set_name (db, g_value_get_string (value));
            break;
        case PROP_IMMUTABLE:
            ephy_node_db_set_immutable (db, g_value_get_boolean (value));
            break;
    }
}

static void
ephy_node_db_class_init (EphyNodeDbClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    parent_class = g_type_class_peek_parent (klass);

    object_class->finalize = ephy_node_db_finalize;
        object_class->set_property = ephy_node_db_set_property;
        object_class->get_property = ephy_node_db_get_property;

    g_object_class_install_property (object_class,
                                         PROP_NAME,
                                         g_param_spec_string  ("name",
                                                               "Name",
                                                               "Name",
                                                               NULL,
                                                               G_PARAM_READWRITE));

    g_object_class_install_property (object_class,
                                         PROP_IMMUTABLE,
                                         g_param_spec_boolean  ("immutable",
                                "Immutable",
                                "Immutable",
                                FALSE,
                                G_PARAM_READWRITE));

    g_type_class_add_private (object_class, sizeof (EphyNodeDbPrivate));
}

static void
ephy_node_db_init (EphyNodeDb *db)
{
    db->priv = EPHY_NODE_DB_GET_PRIVATE (db);

    /* id to node */
    db->priv->id_to_node = g_ptr_array_new ();

    /* id factory */
    db->priv->id_factory = RESERVED_IDS;
}

static void
ephy_node_db_finalize (GObject *object)
{
    EphyNodeDb *db = EPHY_NODE_DB (object);

    g_hash_table_remove (ephy_node_databases, db->priv->name);
    if (g_hash_table_size (ephy_node_databases) == 0)
    {
        g_hash_table_destroy (ephy_node_databases);
    }

    g_ptr_array_free (db->priv->id_to_node, TRUE);

    g_free (db->priv->name);

    G_OBJECT_CLASS (parent_class)->finalize (object);
}

/**
 * ephy_node_db_get_by_name:
 * @name: the name of the desired #EphyNodeDb
 *
 * Returns the #EphyNodeDb named @name, or %NULL if no such database exists.
 *
 * Return value: an #EphyNodeDb
 **/
EphyNodeDb *
ephy_node_db_get_by_name (const char *name)
{
    EphyNodeDb *ret;

    ret = g_hash_table_lookup (ephy_node_databases, name);

    return ret;
}

/**
 * ephy_node_db_new:
 * @name: the name of the new #EphyNodeDb
 *
 * Creates and returns a new #EphyNodeDb, named @name.
 *
 * Return value: the new #EphyNodeDb
 **/
EphyNodeDb *
ephy_node_db_new (const char *name)
{
    EphyNodeDb *db;

    db = EPHY_NODE_DB (g_object_new (EPHY_TYPE_NODE_DB,
                     "name", name,
                         NULL));

    g_return_val_if_fail (db->priv != NULL, NULL);

    return db;
}

static inline EphyNode *
node_from_id_real (EphyNodeDb *db, guint id)
{
    EphyNode *ret = NULL;

    if (id < db->priv->id_to_node->len)
        ret = g_ptr_array_index (db->priv->id_to_node, id);

    return ret;
}

/**
 * ephy_node_db_get_name:
 * @db: an #EphyNodeDb
 *
 * Return value: the name of @db
 **/
const char *
ephy_node_db_get_name (EphyNodeDb *db)
{
    return db->priv->name;
}

/**
 * ephy_node_db_is_immutable:
 * @db: an #EphyNodeDb
 *
 * Return value: %TRUE if @db is immutable
 **/
gboolean
ephy_node_db_is_immutable (EphyNodeDb *db)
{
    return db->priv->immutable;
}

/**
 * ephy_node_db_set_immutable:
 * @db: an #EphyNodeDb
 * @immutable: %TRUE to make @db immutable
 *
 * If @immutable is %TRUE, sets @db immutable (read-only). Otherwise, sets @db
 * to be read-write.
 **/
void
ephy_node_db_set_immutable (EphyNodeDb *db, gboolean immutable)
{
    db->priv->immutable = immutable;

    g_object_notify (G_OBJECT (db), "immutable");
}

/**
 * ephy_node_db_get_node_from_id:
 * @db: an #EphyNodeDb
 * @id: an id specifying an #EphyNode in @db
 *
 * Returns the #EphyNode with id @id from @db, or %NULL if no such id exists.
 *
 * Return value: an #EphyNode
 **/
EphyNode *
ephy_node_db_get_node_from_id (EphyNodeDb *db, guint id)
{
    EphyNode *ret = NULL;

    ret = node_from_id_real (db, id);

    return ret;
}

guint
_ephy_node_db_new_id (EphyNodeDb *db)
{
    guint ret;

    while (node_from_id_real (db, db->priv->id_factory) != NULL)
    {
        db->priv->id_factory++;
    }

    ret = db->priv->id_factory;

    return ret;
}

void
_ephy_node_db_add_id (EphyNodeDb *db,
              guint id,
              EphyNode *node)
{
    /* resize array if needed */
    if (id >= db->priv->id_to_node->len)
        g_ptr_array_set_size (db->priv->id_to_node, id + 1);

    g_ptr_array_index (db->priv->id_to_node, id) = node;
}

void
_ephy_node_db_remove_id (EphyNodeDb *db,
             guint id)
{
    g_ptr_array_index (db->priv->id_to_node, id) = NULL;

    /* reset id factory so we use the freed node id */
    db->priv->id_factory = RESERVED_IDS;
}

/**
 * ephy_node_db_load_from_file:
 * @db: a new #EphyNodeDb
 * @xml_file: the filename from which @db will be populated
 * @xml_root: the root element in @xml_file
 * @xml_version: the required version attribute in the @xml_root
 *
 * Populates @db with data from @xml_file. The node database will be populated
 * with everything inside of the @xml_root tag from @xml_file. If @xml_version
 * is different from the version attribute of the @xml_root element, this
 * function will fail.
 *
 * The data will most probably have been stored using
 * ephy_node_db_write_to_xml_safe().
 *
 * Return value: %TRUE if successful
 **/
gboolean
ephy_node_db_load_from_file (EphyNodeDb *db,
                 const char *xml_file,
                 const xmlChar *xml_root,
                 const xmlChar *xml_version)
{
    xmlTextReaderPtr reader;
    gboolean success = TRUE;
    gboolean was_immutable;
    int ret;

    LOG ("ephy_node_db_load_from_file %s", xml_file);

    START_PROFILER ("loading node db")

    if (g_file_test (xml_file, G_FILE_TEST_EXISTS) == FALSE)
    {
        return FALSE;
    }

    reader = xmlNewTextReaderFilename (xml_file);
    if (reader == NULL)
    {
        return FALSE;
    }

    was_immutable = db->priv->immutable;
    db->priv->immutable = FALSE;

    ret = xmlTextReaderRead (reader);
    while (ret == 1)
    {
        const xmlChar *name;
        xmlReaderTypes type;
        gboolean skip = FALSE;

        name = xmlTextReaderConstName (reader);
        type = xmlTextReaderNodeType (reader);

        if (xmlStrEqual (name, "node")
            && type == XML_READER_TYPE_ELEMENT)
        {
            xmlNodePtr subtree;

            /* grow the subtree and load the node from it */
            subtree = xmlTextReaderExpand (reader);

            if (subtree != NULL)
            {
                ephy_node_new_from_xml (db, subtree);
            }           
                
            skip = TRUE;
        }
        else if (xmlStrEqual (name, xml_root)
             && type == XML_READER_TYPE_ELEMENT)
        {
            xmlChar *version;

            /* check version info */
            version = xmlTextReaderGetAttribute (reader, "version");
            if (xmlStrEqual (version, xml_version) == FALSE)
            {
                success = FALSE;
                xmlFree (version);

                break;
            }

            xmlFree (version);
        }

        /* next one, please */
        ret = skip ? xmlTextReaderNext (reader)
               : xmlTextReaderRead (reader);
    }

    xmlFreeTextReader (reader);

    db->priv->immutable = was_immutable;

    STOP_PROFILER ("loading node db")

    return (success && ret == 0);
}

static int
ephy_node_db_write_to_xml_valist (EphyNodeDb *db,
                  const xmlChar *filename,
                  const xmlChar *root,
                  const xmlChar *version,
                  const xmlChar *comment,
                  EphyNode *first_node,
                  va_list argptr)
{
    xmlTextWriterPtr writer;
    EphyNode *node;
    int ret;

    LOG ("Saving node db to %s", filename);

    START_PROFILER ("Saving node db")

    /* FIXME: do we want to turn compression on ? */
    writer = xmlNewTextWriterFilename (filename, 0);
    if (writer == NULL)
    {
        return -1;
    }

    ret = xmlTextWriterSetIndent (writer, 1);
    if (ret < 0) goto out;

    ret = xmlTextWriterSetIndentString (writer, "  ");
    if (ret < 0) goto out;

    ret = xmlTextWriterStartDocument (writer, "1.0", NULL, NULL);
    if (ret < 0) goto out;

    ret = xmlTextWriterStartElement (writer, root);
    if (ret < 0) goto out;

    ret = xmlTextWriterWriteAttribute (writer, "version", version);
    if (ret < 0) goto out;

    if (comment != NULL)
    {
        ret = xmlTextWriterWriteComment (writer, comment);
        if (ret < 0) goto out;
    }

    node = first_node;
    while (node != NULL)
    {
        GPtrArray *children;
        guint n_exceptions, i;
        GSList *exceptions = NULL;

        n_exceptions = va_arg (argptr, guint);
        for (i=0; i < n_exceptions; i++)
        {
            exceptions = g_slist_prepend (exceptions,
                              va_arg (argptr, EphyNode *));
        }

        children = ephy_node_get_children (node);
        for (i=0; i < children->len; i++)
        {
            EphyNode *kid;
        
            kid = g_ptr_array_index (children, i);
        
            if (g_slist_find (exceptions, kid) == NULL)
            {
                ret = ephy_node_write_to_xml (kid, writer);
                if (ret < 0) break;
            }
        }
        if (ret < 0) break;

        g_slist_free (exceptions);

        node = va_arg (argptr, EphyNode *);
    }
    if (ret < 0) goto out;

    ret = xmlTextWriterEndElement (writer); /* root */
    if (ret < 0) goto out;

    ret = xmlTextWriterEndDocument (writer);
    if (ret < 0) goto out;

out:
    xmlFreeTextWriter (writer);

    STOP_PROFILER ("Saving node db")

    return ret >= 0 ? 0 : -1;
}

/**
 * ephy_node_db_write_to_xml_safe:
 * @db: an #EphyNodeDb
 * @filename: the XML file in which @db's data will be stored
 * @root: the desired root element in @filename
 * @version: the version attribute to the @root element
 * @comment: a comment to place directly inside the @root element of @filename
 * @node: The first node of data to write
 * @Varargs: number of exceptions, list of their #EphyNodes, and more such
 *       sequences, followed by %NULL
 *
 * Writes @db's data to an XML file for storage. The data can be retrieved in
 * the future using ephy_node_db_load_from_file().
 *
 * The function arguments are straightforward until @node, at which point some
 * explanation is necessary.
 *
 * The variable argument list starts at @node, which is an #EphyNode containing
 * data to write to @filename. The next argument is an integer specifying the
 * number of <quote>exception</quote> nodes. After this integer, that number of
 * #EphyNode arguments should be given. Each of these <quote>exception</quote>
 * nodes determines which data out of @node will <emphasis>not</emphasis> be
 * written to @filename.
 *
 * To insert all of an #EphyNode's contents without exception, simply give the
 * integer %0 after @node.
 *
 * The remainder of this function's arguments will be groups of such #EphyNode -
 * integer - (list of #EphyNode<!-- -->s). Finally, the last argument must be
 * %NULL.
 *
 * Return value: %0 on success or a negative number on failure
 **/
int
ephy_node_db_write_to_xml_safe (EphyNodeDb *db,
                const xmlChar *filename,
                const xmlChar *root,
                const xmlChar *version,
                const xmlChar *comment,
                EphyNode *node, ...)
{
    va_list argptr;
    int ret = 0;
    char *tmp_file;

    tmp_file = g_strconcat (filename, ".tmp", NULL);

    va_start (argptr, node);
 
    ret = ephy_node_db_write_to_xml_valist
        (db, tmp_file, root, version, comment, node, argptr);

    va_end (argptr);

    if (ret < 0)
    {
        g_warning ("Failed to write XML data to %s", tmp_file);
        goto failed;
    }

    if (ephy_file_switch_temp_file (filename, tmp_file) == FALSE)
    {
        ret = -1;
    }

failed:
    g_free (tmp_file);

    return ret;
}