aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-tree-memory.c
blob: 30fa6465a1cb747b93b9b705dad3a02ad806a1d3 (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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
 * 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; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Chris Lahey <clahey@ximian.com>
 *      Chris Toshok <toshok@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include "e-tree-memory.h"

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>

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

#include "e-xml-utils.h"

#define E_TREE_MEMORY_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_TREE_MEMORY, ETreeMemoryPrivate))

G_DEFINE_TYPE (ETreeMemory, e_tree_memory, E_TYPE_TREE_MODEL)

enum {
    FILL_IN_CHILDREN,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0, };

typedef struct ETreeMemoryPath ETreeMemoryPath;

struct ETreeMemoryPath {
    gpointer         node_data;

    guint            children_computed : 1;

    /* parent/child/sibling pointers */
    ETreeMemoryPath *parent;
    ETreeMemoryPath *next_sibling;
    ETreeMemoryPath *prev_sibling;
    ETreeMemoryPath *first_child;
    ETreeMemoryPath *last_child;

    gint             num_children;
};

struct _ETreeMemoryPrivate {
    ETreeMemoryPath *root;

    /* whether nodes are created expanded
     * or collapsed by default */
    gboolean         expanded_default;

    gint             frozen;
    GFunc            destroy_func;
    gpointer         destroy_user_data;
};

/* ETreeMemoryPath functions */

static inline void
check_children (ETreeMemory *memory,
                ETreePath node)
{
    ETreeMemoryPath *path = node;
    if (!path->children_computed) {
        g_signal_emit (memory, signals[FILL_IN_CHILDREN], 0, node);
        path->children_computed = TRUE;
    }
}

static gint
e_tree_memory_path_depth (ETreeMemoryPath *path)
{
    gint depth = 0;

    g_return_val_if_fail (path != NULL, -1);

    for (path = path->parent; path; path = path->parent)
        depth++;
    return depth;
}

static void
e_tree_memory_path_insert (ETreeMemoryPath *parent,
                           gint position,
                           ETreeMemoryPath *child)
{
    g_return_if_fail (position <= parent->num_children && position >= -1);

    child->parent = parent;

    if (parent->first_child == NULL)
        parent->first_child = child;

    if (position == -1 || position == parent->num_children) {
        child->prev_sibling = parent->last_child;
        if (parent->last_child)
            parent->last_child->next_sibling = child;
        parent->last_child = child;
    } else {
        ETreeMemoryPath *c;
        for (c = parent->first_child; c; c = c->next_sibling) {
            if (position == 0) {
                child->next_sibling = c;
                child->prev_sibling = c->prev_sibling;

                if (child->next_sibling)
                    child->next_sibling->prev_sibling = child;
                if (child->prev_sibling)
                    child->prev_sibling->next_sibling = child;

                if (parent->first_child == c)
                    parent->first_child = child;
                break;
            }
            position--;
        }
    }

    parent->num_children++;
}

static void
e_tree_path_unlink (ETreeMemoryPath *path)
{
    ETreeMemoryPath *parent = path->parent;

    /* unlink first/last child if applicable */
    if (parent) {
        if (path == parent->first_child)
            parent->first_child = path->next_sibling;
        if (path == parent->last_child)
            parent->last_child = path->prev_sibling;

        parent->num_children--;
    }

    /* unlink prev/next sibling links */
    if (path->next_sibling)
        path->next_sibling->prev_sibling = path->prev_sibling;
    if (path->prev_sibling)
        path->prev_sibling->next_sibling = path->next_sibling;

    path->parent = NULL;
    path->next_sibling = NULL;
    path->prev_sibling = NULL;
}

/**
 * e_tree_memory_freeze:
 * @tree_memory: the ETreeModel to freeze.
 *
 * This function prepares an ETreeModel for a period of much change.
 * All signals regarding changes to the tree are deferred until we
 * thaw the tree.
 *
 **/
void
e_tree_memory_freeze (ETreeMemory *tree_memory)
{
    ETreeMemoryPrivate *priv = tree_memory->priv;

    if (priv->frozen == 0)
        e_tree_model_pre_change (E_TREE_MODEL (tree_memory));

    priv->frozen++;
}

/**
 * e_tree_memory_thaw:
 * @tree_memory: the ETreeMemory to thaw.
 *
 * This function thaws an ETreeMemory.  All the defered signals can add
 * up to a lot, we don't know - so we just emit a model_changed
 * signal.
 *
 **/
void
e_tree_memory_thaw (ETreeMemory *tree_memory)
{
    ETreeMemoryPrivate *priv = tree_memory->priv;

    if (priv->frozen > 0)
        priv->frozen--;
    if (priv->frozen == 0) {
        e_tree_model_node_changed (E_TREE_MODEL (tree_memory), priv->root);
    }
}

/* virtual methods */

static void
tree_memory_dispose (GObject *object)
{
    ETreeMemoryPrivate *priv;

    priv = E_TREE_MEMORY_GET_PRIVATE (object);

    if (priv->root)
        e_tree_memory_node_remove (
            E_TREE_MEMORY (object), priv->root);

    G_OBJECT_CLASS (e_tree_memory_parent_class)->dispose (object);
}

static ETreePath
tree_memory_get_root (ETreeModel *etm)
{
    ETreeMemoryPrivate *priv = E_TREE_MEMORY (etm)->priv;
    return priv->root;
}

static ETreePath
tree_memory_get_parent (ETreeModel *etm,
                 ETreePath node)
{
    ETreeMemoryPath *path = node;
    return path->parent;
}

static ETreePath
tree_memory_get_first_child (ETreeModel *etm,
                      ETreePath node)
{
    ETreeMemoryPath *path = node;

    check_children (E_TREE_MEMORY (etm), node);
    return path->first_child;
}

static ETreePath
tree_memory_get_last_child (ETreeModel *etm,
                     ETreePath node)
{
    ETreeMemoryPath *path = node;

    check_children (E_TREE_MEMORY (etm), node);
    return path->last_child;
}

static ETreePath
tree_memory_get_next (ETreeModel *etm,
               ETreePath node)
{
    ETreeMemoryPath *path = node;
    return path->next_sibling;
}

static ETreePath
tree_memory_get_prev (ETreeModel *etm,
               ETreePath node)
{
    ETreeMemoryPath *path = node;
    return path->prev_sibling;
}

static gboolean
tree_memory_is_root (ETreeModel *etm,
              ETreePath node)
{
    ETreeMemoryPath *path = node;
    return e_tree_memory_path_depth (path) == 0;
}

static gboolean
tree_memory_is_expandable (ETreeModel *etm,
                    ETreePath node)
{
    ETreeMemoryPath *path = node;

    check_children (E_TREE_MEMORY (etm), node);
    return path->first_child != NULL;
}

static guint
tree_memory_get_children (ETreeModel *etm,
                   ETreePath node,
                   ETreePath **nodes)
{
    ETreeMemoryPath *path = node;
    guint n_children;

    check_children (E_TREE_MEMORY (etm), node);

    n_children = path->num_children;

    if (nodes) {
        ETreeMemoryPath *p;
        gint i = 0;

        (*nodes) = g_new (ETreePath, n_children);
        for (p = path->first_child; p; p = p->next_sibling) {
            (*nodes)[i++] = p;
        }
    }

    return n_children;
}

static guint
tree_memory_depth (ETreeModel *etm,
            ETreePath path)
{
    return e_tree_memory_path_depth (path);
}

static gboolean
tree_memory_get_expanded_default (ETreeModel *etm)
{
    ETreeMemory *tree_memory = E_TREE_MEMORY (etm);
    ETreeMemoryPrivate *priv = tree_memory->priv;

    return priv->expanded_default;
}

static void
tree_memory_clear_children_computed (ETreeMemoryPath *path)
{
    for (path = path->first_child; path; path = path->next_sibling) {
        path->children_computed = FALSE;
        tree_memory_clear_children_computed (path);
    }
}

static void
tree_memory_node_request_collapse (ETreeModel *etm,
                            ETreePath node)
{
    ETreeModelClass *parent_class;

    if (node)
        tree_memory_clear_children_computed (node);

    parent_class = E_TREE_MODEL_CLASS (e_tree_memory_parent_class);

    if (parent_class->node_request_collapse != NULL)
        parent_class->node_request_collapse (etm, node);
}

static void
e_tree_memory_class_init (ETreeMemoryClass *class)
{
    GObjectClass *object_class;
    ETreeModelClass *tree_model_class;

    g_type_class_add_private (class, sizeof (ETreeMemoryPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->dispose = tree_memory_dispose;

    tree_model_class = E_TREE_MODEL_CLASS (class);
    tree_model_class->get_root = tree_memory_get_root;
    tree_model_class->get_prev = tree_memory_get_prev;
    tree_model_class->get_next = tree_memory_get_next;
    tree_model_class->get_first_child = tree_memory_get_first_child;
    tree_model_class->get_last_child = tree_memory_get_last_child;
    tree_model_class->get_parent = tree_memory_get_parent;

    tree_model_class->is_root = tree_memory_is_root;
    tree_model_class->is_expandable = tree_memory_is_expandable;
    tree_model_class->get_children = tree_memory_get_children;
    tree_model_class->depth = tree_memory_depth;
    tree_model_class->get_expanded_default = tree_memory_get_expanded_default;

    tree_model_class->node_request_collapse = tree_memory_node_request_collapse;

    class->fill_in_children = NULL;

    signals[FILL_IN_CHILDREN] = g_signal_new (
        "fill_in_children",
        G_TYPE_FROM_CLASS (object_class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (ETreeMemoryClass, fill_in_children),
        (GSignalAccumulator) NULL, NULL,
        g_cclosure_marshal_VOID__POINTER,
        G_TYPE_NONE, 1,
        G_TYPE_POINTER);
}

static void
e_tree_memory_init (ETreeMemory *tree_memory)
{
    tree_memory->priv = E_TREE_MEMORY_GET_PRIVATE (tree_memory);
}

/**
 * e_tree_memory_construct:
 * @tree_memory:
 *
 *
 **/
void
e_tree_memory_construct (ETreeMemory *tree_memory)
{
}

/**
 * e_tree_memory_new
 *
 * XXX docs here.
 *
 * return values: a newly constructed ETreeMemory.
 */
ETreeMemory *
e_tree_memory_new (void)
{
    return g_object_new (E_TYPE_TREE_MEMORY, NULL);
}

/**
 * e_tree_memory_set_expanded_default
 *
 * Sets the state of nodes to be append to a thread.
 * They will either be expanded or collapsed, according to
 * the value of @expanded.
 */
void
e_tree_memory_set_expanded_default (ETreeMemory *tree_memory,
                                    gboolean expanded)
{
    g_return_if_fail (tree_memory != NULL);

    tree_memory->priv->expanded_default = expanded;
}

/**
 * e_tree_memory_node_get_data:
 * @tree_memory:
 * @node:
 *
 *
 *
 * Return value:
 **/
gpointer
e_tree_memory_node_get_data (ETreeMemory *tree_memory,
                             ETreePath node)
{
    ETreeMemoryPath *path = node;

    g_return_val_if_fail (path, NULL);

    return path->node_data;
}

/**
 * e_tree_memory_node_set_data:
 * @tree_memory:
 * @node:
 * @node_data:
 *
 *
 **/
void
e_tree_memory_node_set_data (ETreeMemory *tree_memory,
                             ETreePath node,
                             gpointer node_data)
{
    ETreeMemoryPath *path = node;

    g_return_if_fail (path);

    path->node_data = node_data;
}

/**
 * e_tree_memory_node_insert:
 * @tree_memory:
 * @parent_node:
 * @position:
 * @node_data:
 *
 *
 *
 * Return value:
 **/
ETreePath
e_tree_memory_node_insert (ETreeMemory *tree_memory,
                           ETreePath parent_node,
                           gint position,
                           gpointer node_data)
{
    ETreeMemoryPrivate *priv;
    ETreeMemoryPath *new_path;
    ETreeMemoryPath *parent_path = parent_node;

    g_return_val_if_fail (tree_memory != NULL, NULL);

    priv = tree_memory->priv;

    g_return_val_if_fail (parent_path != NULL || priv->root == NULL, NULL);

    priv = tree_memory->priv;

    if (!tree_memory->priv->frozen)
        e_tree_model_pre_change (E_TREE_MODEL (tree_memory));

    new_path = g_slice_new0 (ETreeMemoryPath);

    new_path->node_data = node_data;
    new_path->children_computed = FALSE;

    if (parent_path != NULL) {
        e_tree_memory_path_insert (parent_path, position, new_path);
        if (!tree_memory->priv->frozen)
            e_tree_model_node_inserted (
                E_TREE_MODEL (tree_memory),
                parent_path, new_path);
    } else {
        priv->root = new_path;
        if (!tree_memory->priv->frozen)
            e_tree_model_node_changed (
                E_TREE_MODEL (tree_memory), new_path);
    }

    return new_path;
}

/**
 * e_tree_memory_node_insert_before:
 * @tree_memory:
 * @parent:
 * @sibling:
 * @node_data:
 *
 *
 *
 * Return value:
 **/
ETreePath
e_tree_memory_node_insert_before (ETreeMemory *tree_memory,
                                  ETreePath parent,
                                  ETreePath sibling,
                                  gpointer node_data)
{
    ETreeMemoryPath *child;
    ETreeMemoryPath *parent_path = parent;
    ETreeMemoryPath *sibling_path = sibling;
    gint position = 0;

    g_return_val_if_fail (tree_memory != NULL, NULL);

    if (sibling != NULL) {
        for (child = parent_path->first_child; child; child = child->next_sibling) {
            if (child == sibling_path)
                break;
            position++;
        }
    } else
        position = parent_path->num_children;
    return e_tree_memory_node_insert (tree_memory, parent, position, node_data);
}

/* just blows away child data, doesn't take into account unlinking/etc */
static void
child_free (ETreeMemory *tree_memory,
            ETreeMemoryPath *node)
{
    ETreeMemoryPath *child, *next;

    child = node->first_child;
    while (child) {
        next = child->next_sibling;
        child_free (tree_memory, child);
        child = next;
    }

    if (tree_memory->priv->destroy_func) {
        tree_memory->priv->destroy_func (node->node_data, tree_memory->priv->destroy_user_data);
    }

    g_slice_free (ETreeMemoryPath, node);
}

/**
 * e_tree_memory_node_remove:
 * @tree_memory:
 * @path:
 *
 *
 *
 * Return value:
 **/
gpointer
e_tree_memory_node_remove (ETreeMemory *tree_memory,
                           ETreePath node)
{
    ETreeMemoryPath *path = node;
    ETreeMemoryPath *parent = path->parent;
    ETreeMemoryPath *sibling;
    gpointer ret = path->node_data;
    gint old_position = 0;

    g_return_val_if_fail (tree_memory != NULL, NULL);

    if (!tree_memory->priv->frozen) {
        e_tree_model_pre_change (E_TREE_MODEL (tree_memory));
        for (old_position = 0, sibling = path;
             sibling;
             old_position++, sibling = sibling->prev_sibling)
            /* Empty intentionally*/;
        old_position--;
    }

    /* unlink this node - we only have to unlink the root node being removed,
     * since the others are only references from this node */
    e_tree_path_unlink (path);

    /*printf("removing %d nodes from position %d\n", visible, base);*/
    if (!tree_memory->priv->frozen)
        e_tree_model_node_removed (E_TREE_MODEL (tree_memory), parent, path, old_position);

    child_free (tree_memory, path);

    if (path == tree_memory->priv->root)
        tree_memory->priv->root = NULL;

    if (!tree_memory->priv->frozen)
        e_tree_model_node_deleted (E_TREE_MODEL (tree_memory), path);

    return ret;
}

typedef struct {
    ETreeMemory *memory;
    gpointer closure;
    ETreeMemorySortCallback callback;
} MemoryAndClosure;

static gint
sort_callback (gconstpointer data1,
               gconstpointer data2,
               gpointer user_data)
{
    ETreePath path1 = *(ETreePath *) data1;
    ETreePath path2 = *(ETreePath *) data2;
    MemoryAndClosure *mac = user_data;
    return (*mac->callback) (mac->memory, path1, path2, mac->closure);
}

void
e_tree_memory_sort_node (ETreeMemory *tree_memory,
                         ETreePath node,
                         ETreeMemorySortCallback callback,
                         gpointer user_data)
{
    ETreeMemoryPath **children;
    ETreeMemoryPath *child;
    gint count;
    gint i;
    ETreeMemoryPath *path = node;
    MemoryAndClosure mac;
    ETreeMemoryPath *last;

    e_tree_model_pre_change (E_TREE_MODEL (tree_memory));

    i = 0;
    for (child = path->first_child; child; child = child->next_sibling)
        i++;

    children = g_new (ETreeMemoryPath *, i);

    count = i;

    for (child = path->first_child, i = 0;
         child;
         child = child->next_sibling, i++) {
        children[i] = child;
    }

    mac.memory = tree_memory;
    mac.closure = user_data;
    mac.callback = callback;

    g_qsort_with_data (
        children, count, sizeof (ETreeMemoryPath *),
        sort_callback, &mac);

    path->first_child = NULL;
    last = NULL;
    for (i = 0;
         i < count;
         i++) {
        children[i]->prev_sibling = last;
        if (last)
            last->next_sibling = children[i];
        else
            path->first_child = children[i];
        last = children[i];
    }
    if (last)
        last->next_sibling = NULL;

    path->last_child = last;

    g_free (children);

    e_tree_model_node_changed (E_TREE_MODEL (tree_memory), node);
}

void
e_tree_memory_set_node_destroy_func (ETreeMemory *tree_memory,
                                     GFunc destroy_func,
                                     gpointer user_data)
{
    tree_memory->priv->destroy_func = destroy_func;
    tree_memory->priv->destroy_user_data = user_data;
}