aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/table/e-tree-model.c
blob: ba4ffa1d4b642f036d541f1bc6d87b88a66911ba (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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * e-tree-model.c: a Tree Model
 *
 * Author:
 *   Chris Toshok (toshok@ximian.com)
 *
 * Adapted from the gtree code and ETableModel.
 *
 * (C) 2000 Ximian, Inc.
 */
#include <config.h>

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

#include <gnome-xml/parser.h>
#include <gnome-xml/xmlmemory.h>

#include <gtk/gtksignal.h>
#include <stdlib.h>
#include "gal/util/e-util.h"
#include "gal/util/e-xml-utils.h"
#include "e-tree-model.h"

#define ETM_CLASS(e) ((ETreeModelClass *)((GtkObject *)e)->klass)

#define PARENT_TYPE E_TABLE_MODEL_TYPE

#define TREEPATH_CHUNK_AREA_SIZE (30 * sizeof (ETreePath))

static ETableModel *e_tree_model_parent_class;

struct ETreeModelPriv {
    GMemChunk  *node_chunk;
    ETreePath  *root;
    gboolean    root_visible;
    GArray     *row_array; /* used in the mapping between ETable and our tree */
    GHashTable *expanded_state; /* used for loading/saving expanded state */
    GString    *sort_group; /* for caching the last sort group info */
    gboolean    expanded_default; /* whether nodes are created expanded or collapsed by default */
    gint        frozen;
};

struct ETreePath {
    gboolean             expanded;
    gboolean             expanded_set;
    guint                visible_descendents;
    char                *save_id;
    ETreePathCompareFunc compare;
    gpointer             node_data;

    /* parent/child/sibling pointers */
    ETreePath           *parent;
    ETreePath           *next_sibling;
    ETreePath           *prev_sibling;
    ETreePath           *first_child;
    ETreePath           *last_child;
    guint32              num_children;
};

enum {
    NODE_CHANGED,
    NODE_INSERTED,
    NODE_REMOVED,
    NODE_COLLAPSED,
    NODE_EXPANDED,
    LAST_SIGNAL
};

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

static void add_visible_descendents_to_array (ETreeModel *etm, ETreePath *node, int *row, int *count);


/* ETreePath functions */

static int
e_tree_path_depth (ETreePath *path)
{
    int depth = 0;
    while (path) {
        depth ++;
        path = path->parent;
    }
    return depth;
}

static void
e_tree_path_insert (ETreePath *parent, int position, ETreePath *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 {
        ETreePath *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 (ETreePath *path)
{
    ETreePath *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;
}

void
e_tree_model_node_traverse (ETreeModel *model, ETreePath *path, ETreePathFunc func, gpointer data)
{
    ETreePath *child;

    g_return_if_fail (path);

    child = path->first_child;

    while (child) {
        ETreePath *next_child = child->next_sibling;
        e_tree_model_node_traverse (model, child, func, data);
        if (func (model, child, data) == TRUE)
            return;

        child = next_child;
    }
}



void
e_tree_model_freeze(ETreeModel *etm)
{
    ETreeModelPriv *priv = etm->priv;

    priv->frozen ++;
}

void
e_tree_model_thaw(ETreeModel *etm)
{
    ETreeModelPriv *priv = etm->priv;

    if (priv->frozen > 0)
        priv->frozen --;
    if (priv->frozen == 0) {
        e_table_model_changed(E_TABLE_MODEL(etm));
    }
}


/* virtual methods */

static void
etree_destroy (GtkObject *object)
{
    ETreeModel *etree = E_TREE_MODEL (object);
    ETreeModelPriv *priv = etree->priv;

    /* XXX lots of stuff to free here */
    g_array_free (priv->row_array, TRUE);
    g_hash_table_destroy (priv->expanded_state);

    g_string_free(priv->sort_group, TRUE);

    g_free (priv);

    GTK_OBJECT_CLASS (e_tree_model_parent_class)->destroy (object);
}

static ETreePath*
etree_get_root (ETreeModel *etm)
{
    ETreeModelPriv *priv = etm->priv;
    return priv->root;
}

static ETreePath*
etree_get_parent (ETreeModel *etm, ETreePath *path)
{
    g_return_val_if_fail (path, NULL);
    
    return path->parent;
}

static ETreePath*
etree_get_next (ETreeModel *etm, ETreePath *node)
{
    g_return_val_if_fail (node, NULL);

    return node->next_sibling;
}

static ETreePath*
etree_get_prev (ETreeModel *etm, ETreePath *node)
{
    g_return_val_if_fail (node, NULL);

    return node->prev_sibling;
}

static ETreePath*
etree_get_first_child (ETreeModel *etm, ETreePath *node)
{
    g_return_val_if_fail (node, NULL);

    return node->first_child;
}

static ETreePath*
etree_get_last_child (ETreeModel *etm, ETreePath *node)
{
    g_return_val_if_fail (node, NULL);

    return node->last_child;
}

static guint
etree_get_children (ETreeModel *etm, ETreePath* node, ETreePath ***paths)
{
    guint n_children;

    g_return_val_if_fail (node, 0);

    n_children = node->num_children;

    if (paths) {
        ETreePath *p;
        int i = 0;
        (*paths) = g_malloc (sizeof (ETreePath*) * n_children);
        for (p = node->first_child; p; p = p->next_sibling) {
            (*paths)[i++] = p;
        }
    }

    return n_children;
}

static gboolean
etree_is_expanded (ETreeModel *etm, ETreePath* node)
{
    g_return_val_if_fail (node, FALSE);
    
    return node->expanded;
}

static gboolean
etree_is_visible (ETreeModel *etm, ETreePath* node)
{
    g_return_val_if_fail (node, FALSE);

    for (node = node->parent; node; node = node->parent) {
        if (!node->expanded)
            return FALSE;
    }

    return TRUE;
}

static void
etree_set_expanded (ETreeModel *etm, ETreePath* node, gboolean expanded)
{
    ETreeModelPriv *priv = etm->priv;
    ETreePath *child;
    int row;

    g_return_if_fail (node);

    node->expanded_set = TRUE;

    if (node->expanded == expanded)
        return;

    if (expanded) {
        gboolean allow_expand = TRUE;
        e_tree_model_node_expanded (etm, node, &allow_expand);
        if (!allow_expand)
            return;
    }

    node->expanded = expanded;
    if (node->save_id) {
        g_hash_table_insert (priv->expanded_state, node->save_id, (gpointer)expanded);
    }

    /* if the node wasn't visible at present */
    if ((row = e_tree_model_row_of_node (etm, node)) == -1) {
        if (!expanded) {
            e_tree_model_node_collapsed (etm, node);
        }
        return;
    }
        
    row++;

    if (expanded) {
        ETreePath *parent;

        if (e_tree_model_node_is_visible (etm, node)) {
            node->visible_descendents = 0;
            for (child = node->first_child; child;
                 child = child->next_sibling) {
                add_visible_descendents_to_array (etm, child, &row, &node->visible_descendents);
            }
        }
        /* now iterate back up the tree, adding to our
           ancestors' visible descendents */

        for (parent = node->parent; parent; parent = parent->parent) {
            parent->visible_descendents += node->visible_descendents;
        }
    }
    else {
        int i;
        ETreePath *parent;

        if (e_tree_model_node_is_visible (etm, node)) {
            for (i = 0; i < node->visible_descendents; i ++) {
                priv->row_array = g_array_remove_index (priv->row_array, row);
                e_table_model_row_deleted (E_TABLE_MODEL (etm), row);
            }
        }
        /* now iterate back up the tree, subtracting from our
           ancestors' visible descendents */

        for (parent = node->parent; parent; parent = parent->parent) {
            parent->visible_descendents -= node->visible_descendents;
        }

        node->visible_descendents = 0;

        e_tree_model_node_collapsed (etm, node);
    }
}

void
e_tree_model_show_node (ETreeModel *etm, ETreePath* node)
{
    ETreePath *parent;

    parent = e_tree_model_node_get_parent(etm, node);
    if (parent) {
        e_tree_model_show_node(etm, parent);
        e_tree_model_node_set_expanded(etm, parent, TRUE);
    }
}

void
e_tree_model_set_expanded_default (ETreeModel *etree,
                   gboolean expanded)
{
    ETreeModelPriv *priv = etree->priv;

    priv->expanded_default = expanded;
}

/* fairly naive implementation */
static void
etree_set_expanded_recurse (ETreeModel *etm, ETreePath* node, gboolean expanded)
{
    ETreePath *child;

    e_tree_model_node_set_expanded (etm, node, expanded);

    for (child = node->first_child; child; child = child->next_sibling)
        e_tree_model_node_set_expanded_recurse (etm, child, expanded);
}

static ETreePath *
etree_node_at_row (ETreeModel *etree, int row)
{
    ETreeModelPriv *priv = etree->priv;

    g_return_val_if_fail (row < priv->row_array->len, NULL);

    return g_array_index (priv->row_array, ETreePath*, row);
}


/* ETable analogs */
static void*
etree_value_at (ETreeModel *etm, ETreePath* node, int col)
{
    /* shouldn't be called */
    g_assert (0);
    return NULL;
}

static GdkPixbuf*
etree_icon_at (ETreeModel *etm, ETreePath* node)
{
    /* shouldn't be called */
    g_assert (0);
    return NULL;
}

static void
etree_set_value_at (ETreeModel *etm, ETreePath* node, int col, const void *val)
{
    /* shouldn't be called */
    g_assert (0);
}

static gboolean
etree_is_editable (ETreeModel *etm, ETreePath* node, int col)
{
    /* shouldn't be called */
    g_assert(0);
    return FALSE;
}


/* ETable virtual functions we map */
static int
etable_row_count (ETableModel *etm)
{
    ETreeModel *tree = E_TREE_MODEL (etm);
    ETreeModelPriv *priv = tree->priv;
    return priv->row_array->len;
}

static void *
etable_value_at (ETableModel *etm, int col, int row)
{
    ETreeModel *etree = E_TREE_MODEL(etm);
    ETreeModelClass *et_class = ETM_CLASS(etm);
    ETreePath* node = e_tree_model_node_at_row (etree, row);

    g_return_val_if_fail (node, NULL);

    if (col == -1)
        return node;
    else if (col == -2)
        return etm;
    else
        return et_class->value_at (etree, node, col);
}

static void
etable_set_value_at (ETableModel *etm, int col, int row, const void *val)
{
    ETreeModel *etree = E_TREE_MODEL(etm);
    ETreeModelClass *et_class = ETM_CLASS(etm);
    ETreePath* node = e_tree_model_node_at_row (etree, row);

    g_return_if_fail (node);

    et_class->set_value_at (etree, node, col, val);
}

static gboolean
etable_is_cell_editable (ETableModel *etm, int col, int row)
{
    ETreeModel *etree = E_TREE_MODEL(etm);
    ETreeModelClass *et_class = ETM_CLASS(etm);
    ETreePath* node = e_tree_model_node_at_row (etree, row);

    g_return_val_if_fail (node, FALSE);

    return et_class->is_editable (etree, node, col);
}

static void
build_sort_group(GString *out, ETreePath *node)
{
    if (node->parent) {
        build_sort_group(out, node->parent);
    }
    g_string_sprintfa(out, "/%p", node);
}

static const char *
etable_row_sort_group(ETableModel *etm, int row)
{
    ETreeModel *etree = E_TREE_MODEL(etm);
    ETreeModelPriv *priv = etree->priv;
    ETreePath* node = e_tree_model_node_at_row (etree, row);

    g_return_val_if_fail (node, "");

    g_string_truncate(priv->sort_group, 0);
    if (node)
        build_sort_group(priv->sort_group, node);

    return priv->sort_group->str;
}

static gboolean
etable_has_sort_group(ETableModel *etm)
{
    /* could optimise for the flat &/or unexpanded tree case */
    return TRUE;
}


static void
e_tree_model_class_init (GtkObjectClass *klass)
{
    ETableModelClass *table_class = (ETableModelClass *) klass;
    ETreeModelClass *tree_class = (ETreeModelClass *) klass;

    e_tree_model_parent_class = gtk_type_class (PARENT_TYPE);
    
    klass->destroy = etree_destroy;

    e_tree_model_signals [NODE_CHANGED] =
        gtk_signal_new ("node_changed",
                GTK_RUN_LAST,
                klass->type,
                GTK_SIGNAL_OFFSET (ETreeModelClass, node_changed),
                gtk_marshal_NONE__POINTER,
                GTK_TYPE_NONE, 1, GTK_TYPE_POINTER);

    e_tree_model_signals [NODE_INSERTED] =
        gtk_signal_new ("node_inserted",
                GTK_RUN_LAST,
                klass->type,
                GTK_SIGNAL_OFFSET (ETreeModelClass, node_inserted),
                gtk_marshal_NONE__POINTER_POINTER,
                GTK_TYPE_NONE, 2, GTK_TYPE_POINTER, GTK_TYPE_POINTER);

    e_tree_model_signals [NODE_REMOVED] =
        gtk_signal_new ("node_removed",
                GTK_RUN_LAST,
                klass->type,
                GTK_SIGNAL_OFFSET (ETreeModelClass, node_removed),
                gtk_marshal_NONE__POINTER_POINTER,
                GTK_TYPE_NONE, 2, GTK_TYPE_POINTER, GTK_TYPE_POINTER);

    e_tree_model_signals [NODE_COLLAPSED] =
        gtk_signal_new ("node_collapsed",
                GTK_RUN_LAST,
                klass->type,
                GTK_SIGNAL_OFFSET (ETreeModelClass, node_collapsed),
                gtk_marshal_NONE__POINTER,
                GTK_TYPE_NONE, 1, GTK_TYPE_POINTER);

    e_tree_model_signals [NODE_EXPANDED] =
        gtk_signal_new ("node_expanded",
                GTK_RUN_LAST,
                klass->type,
                GTK_SIGNAL_OFFSET (ETreeModelClass, node_expanded),
                gtk_marshal_NONE__POINTER_POINTER,
                GTK_TYPE_NONE, 2, GTK_TYPE_POINTER, GTK_TYPE_POINTER);

    gtk_object_class_add_signals (klass, e_tree_model_signals, LAST_SIGNAL);

    table_class->row_count        = etable_row_count;
    table_class->value_at         = etable_value_at;
    table_class->set_value_at     = etable_set_value_at;
    table_class->is_cell_editable = etable_is_cell_editable;
#if 0
    /* XX need to pass these through */
    table_class->duplicate_value  = etable_duplicate_value;
    table_class->free_value       = etable_free_value;
    table_class->initialize_value = etable_initialize_value;
    table_class->value_is_empty   = etable_value_is_empty;
    table_class->value_to_string  = etable_value_to_string;
    table_class->thaw             = etable_thaw;
#endif

    table_class->row_sort_group   = etable_row_sort_group;
    table_class->has_sort_group   = etable_has_sort_group;

    tree_class->get_root          = etree_get_root;
    tree_class->get_prev          = etree_get_prev;
    tree_class->get_next          = etree_get_next;
    tree_class->get_first_child   = etree_get_first_child;
    tree_class->get_last_child    = etree_get_last_child;
    tree_class->get_parent        = etree_get_parent;

    tree_class->value_at          = etree_value_at;
    tree_class->icon_at           = etree_icon_at;
    tree_class->set_value_at      = etree_set_value_at;
    tree_class->is_editable       = etree_is_editable;

    tree_class->get_children      = etree_get_children;
    tree_class->is_expanded       = etree_is_expanded;
    tree_class->is_visible        = etree_is_visible;
    tree_class->set_expanded      = etree_set_expanded;
    tree_class->set_expanded_recurse = etree_set_expanded_recurse;
    tree_class->node_at_row       = etree_node_at_row;
}

static void
e_tree_init (GtkObject *object)
{
    ETreeModel *etree = (ETreeModel *)object;
    e_tree_model_construct (etree);
}

E_MAKE_TYPE(e_tree_model, "ETreeModel", ETreeModel, e_tree_model_class_init, e_tree_init, PARENT_TYPE)


/* signals */
void
e_tree_model_node_changed  (ETreeModel *tree_model, ETreePath *node)
{
    int row;
    g_return_if_fail (tree_model != NULL);
    g_return_if_fail (E_IS_TREE_MODEL (tree_model));
    
    row = e_tree_model_row_of_node (tree_model, node);
    if (row != -1)
        e_table_model_row_changed (E_TABLE_MODEL (tree_model), row);

    gtk_signal_emit (GTK_OBJECT (tree_model),
             e_tree_model_signals [NODE_CHANGED]);
}

void
e_tree_model_node_inserted (ETreeModel *tree_model,
                ETreePath *parent_node,
                ETreePath *inserted_node)
{
    int row;
    g_return_if_fail (tree_model != NULL);
    g_return_if_fail (E_IS_TREE_MODEL (tree_model));

    row = e_tree_model_row_of_node (tree_model, inserted_node);
    if (row != -1)
        e_table_model_row_inserted (E_TABLE_MODEL (tree_model), row);
    
    gtk_signal_emit (GTK_OBJECT (tree_model),
             e_tree_model_signals [NODE_INSERTED],
             parent_node, inserted_node);
}

void
e_tree_model_node_removed  (ETreeModel *tree_model, ETreePath *parent_node, ETreePath *removed_node)
{
    int row;

    g_return_if_fail (tree_model != NULL);
    g_return_if_fail (E_IS_TREE_MODEL (tree_model));

    row = e_tree_model_row_of_node (tree_model, removed_node);
    if (row != -1)
        e_table_model_row_deleted (E_TABLE_MODEL (tree_model), row);
    
    gtk_signal_emit (GTK_OBJECT (tree_model),
             e_tree_model_signals [NODE_REMOVED],
             parent_node, removed_node);
}

void
e_tree_model_node_collapsed (ETreeModel *tree_model, ETreePath *node)
{
    g_return_if_fail (tree_model != NULL);
    g_return_if_fail (E_IS_TREE_MODEL (tree_model));
    
    gtk_signal_emit (GTK_OBJECT (tree_model),
             e_tree_model_signals [NODE_COLLAPSED],
             node);
}

void
e_tree_model_node_expanded  (ETreeModel *tree_model, ETreePath *node, gboolean *allow_expand)
{
    g_return_if_fail (tree_model != NULL);
    g_return_if_fail (E_IS_TREE_MODEL (tree_model));
    
    gtk_signal_emit (GTK_OBJECT (tree_model),
             e_tree_model_signals [NODE_EXPANDED],
             node, allow_expand);
}



void
e_tree_model_construct (ETreeModel *etree)
{
    ETreeModelPriv *priv = g_new0 (ETreeModelPriv, 1);

    etree->priv = priv;

    priv->node_chunk = g_mem_chunk_create (ETreePath, TREEPATH_CHUNK_AREA_SIZE, G_ALLOC_AND_FREE);
    priv->root = NULL;
    priv->root_visible = TRUE;
    priv->row_array = g_array_new (FALSE, FALSE, sizeof(ETreePath*));
    priv->expanded_state = g_hash_table_new (g_str_hash, g_str_equal);
    priv->sort_group = g_string_new("");
    priv->frozen = 0;
}

ETreeModel *
e_tree_model_new ()
{
    ETreeModel *et;

    et = gtk_type_new (e_tree_model_get_type ());

    return et;
}

ETreePath *
e_tree_model_get_root (ETreeModel *etree)
{
    return ETM_CLASS(etree)->get_root(etree);
}

ETreePath *
e_tree_model_node_at_row (ETreeModel *etree, int row)
{
    return ETM_CLASS(etree)->node_at_row (etree, row);
}

GdkPixbuf *
e_tree_model_icon_of_node (ETreeModel *etree, ETreePath *path)
{
    return ETM_CLASS(etree)->icon_at (etree, path);
}

int
e_tree_model_row_of_node (ETreeModel *etree, ETreePath *node)
{
    ETreeModelPriv *priv = etree->priv;
    int i;

    for (i = 0; i < priv->row_array->len; i ++)
        if (g_array_index (priv->row_array, ETreePath*, i) == node)
            return i;

    return -1;
}

void
e_tree_model_root_node_set_visible (ETreeModel *etm, gboolean visible)
{
    ETreeModelPriv *priv = etm->priv;

    if (visible != priv->root_visible) {
        priv->root_visible = visible;
        if (priv->root) {
            if (visible) {
                priv->row_array = g_array_insert_val (priv->row_array, 0, priv->root);
            }
            else {
                ETreePath *root_path = e_tree_model_get_root (etm);
                e_tree_model_node_set_expanded (etm, root_path, TRUE);
                priv->row_array = g_array_remove_index (priv->row_array, 0);
            }
            
            e_table_model_changed (E_TABLE_MODEL (etm));
        }
    }
}

gboolean
e_tree_model_root_node_is_visible (ETreeModel *etm)
{
    ETreeModelPriv *priv = etm->priv;
    return priv->root_visible;
}

ETreePath *
e_tree_model_node_get_first_child (ETreeModel *etree, ETreePath *node)
{
    return ETM_CLASS(etree)->get_first_child(etree, node);
}

ETreePath *
e_tree_model_node_get_last_child (ETreeModel *etree, ETreePath *node)
{
    return ETM_CLASS(etree)->get_last_child(etree, node);
}


ETreePath *
e_tree_model_node_get_next (ETreeModel *etree, ETreePath *node)
{
    return ETM_CLASS(etree)->get_next(etree, node);
}

ETreePath *
e_tree_model_node_get_prev (ETreeModel *etree, ETreePath *node)
{
    return ETM_CLASS(etree)->get_prev(etree, node);
}

guint
e_tree_model_node_depth (ETreeModel *etree, ETreePath *path)
{
    return e_tree_path_depth (path) - 1;
}

ETreePath *
e_tree_model_node_get_parent (ETreeModel *etree, ETreePath *path)
{
    return ETM_CLASS(etree)->get_parent(etree, path);
}

gboolean
e_tree_model_node_is_root (ETreeModel *etree, ETreePath *path)
{
    return (e_tree_model_node_depth (etree, path) == 0);
}

gboolean
e_tree_model_node_is_expandable (ETreeModel *etree, ETreePath *path)
{
    return (e_tree_model_node_get_children (etree, path, NULL) > 0);
}

gboolean
e_tree_model_node_is_expanded (ETreeModel *etree, ETreePath *path)
{
    return ETM_CLASS(etree)->is_expanded (etree, path);
}

gboolean
e_tree_model_node_is_visible (ETreeModel *etree, ETreePath *path)
{
    return ETM_CLASS(etree)->is_visible (etree, path);
}

void
e_tree_model_node_set_expanded (ETreeModel *etree, ETreePath *path, gboolean expanded)
{
    ETM_CLASS(etree)->set_expanded (etree, path, expanded);
}

void
e_tree_model_node_set_expanded_recurse (ETreeModel *etree, ETreePath *path, gboolean expanded)
{
    ETM_CLASS(etree)->set_expanded_recurse (etree, path, expanded);
}

guint
e_tree_model_node_get_children (ETreeModel *etree, ETreePath *path, ETreePath ***paths)
{
    return ETM_CLASS(etree)->get_children (etree, path, paths);
}

guint
e_tree_model_node_num_visible_descendents (ETreeModel *etm, ETreePath *node)
{
    return node->visible_descendents;
}

gpointer
e_tree_model_node_get_data (ETreeModel *etm, ETreePath *node)
{
    g_return_val_if_fail (node, NULL);

    return node->node_data;
}

void
e_tree_model_node_set_data (ETreeModel *etm, ETreePath *node, gpointer node_data)
{
    g_return_if_fail (node);

    node->node_data = node_data;
}

ETreePath*
e_tree_model_node_insert (ETreeModel *tree_model,
              ETreePath *parent_path,
              int position,
              gpointer node_data)
{
    ETreeModelPriv *priv;
    ETreePath *new_path;

    priv = tree_model->priv;

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

    priv = tree_model->priv;

    new_path = g_chunk_new0 (ETreePath, priv->node_chunk);

    new_path->expanded = FALSE;
    new_path->node_data = node_data;

    if (parent_path != NULL) {

        if (parent_path->first_child == NULL
            && !parent_path->expanded_set) {
            e_tree_model_node_set_expanded (tree_model,
                            parent_path,
                            priv->expanded_default);
        }

        e_tree_path_insert (parent_path, position, new_path);

        if (e_tree_model_node_is_visible (tree_model, new_path)) {
            int parent_row, child_offset = 0;
            ETreePath *n;

            /* we need to iterate back up to the root, incrementing the number of visible
               descendents */
            for (n = parent_path; n; n = n->parent) {
                n->visible_descendents ++;
            }
                
            /* determine if we are inserting at the end of this parent */
            if (position == -1 || position == parent_path->num_children) {
                position = e_tree_model_node_num_visible_descendents (tree_model, parent_path) - 1;
            } else {
                /* if we're not inserting at the end of the array, position is the child node we're
                   inserting at, not the absolute row position - need to count expanded nodes before it too */
                int i = position;

                n = e_tree_model_node_get_first_child(tree_model, parent_path);
                while (n != NULL && i > 0) {
                    child_offset += n->visible_descendents;
                    n = n->next_sibling;
                    i--;
                }
            }


            /* if the parent is the root, no need to search for its position since it aint there */
            if (parent_path->parent == NULL) {
                parent_row = -1;
            } else {
                parent_row = e_tree_model_row_of_node (tree_model, parent_path);
            }

            e_table_model_pre_change(E_TABLE_MODEL(tree_model));

            priv->row_array = g_array_insert_val (priv->row_array,
                                  parent_row + position + 1 + child_offset, new_path);

            /* only do this if we know a changed signal isn't coming later on */
            if (priv->frozen == 0)
                e_table_model_row_inserted (E_TABLE_MODEL(tree_model), parent_row + position + 1 + child_offset);
        }

        if (parent_path->compare)
            e_tree_model_node_sort (tree_model, parent_path);
    }
    else {
        priv->root = new_path;
        if (priv->root_visible) {
            priv->row_array = g_array_insert_val (priv->row_array, 0, priv->root);
            e_table_model_row_inserted (E_TABLE_MODEL (tree_model), 0);
        }
        else {
            /* need to mark the new node as expanded or
                           we'll never see it's children */
            new_path->expanded = TRUE;
            new_path->expanded_set = TRUE;
        }
    }

    return new_path;
}

ETreePath*
e_tree_model_node_insert_id (ETreeModel *tree_model,
                 ETreePath *parent_path,
                 int position,
                 gpointer node_data,
                 const char *save_id)
{
    ETreePath *path = e_tree_model_node_insert (tree_model, parent_path, position, node_data);

    e_tree_model_node_set_save_id (tree_model, path, save_id);

    return path;
}

              
ETreePath *
e_tree_model_node_insert_before (ETreeModel *etree,
                 ETreePath *parent,
                 ETreePath *sibling,
                 gpointer node_data)
{
    ETreePath *child;
    int position = 0;
    for (child = parent->first_child; child; child = child->next_sibling) {
        if (child == sibling)
            break;
        position ++;
    }
    return e_tree_model_node_insert (etree, parent, position, node_data);
}

/* just blows away child data, doesn't take into account unlinking/etc */
static void
child_free(ETreeModel *etree, ETreePath *node)
{
    ETreePath *child, *next;
    ETreeModelPriv *priv = etree->priv;

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

    if (node->save_id) {
        g_hash_table_remove(priv->expanded_state, node->save_id);
        g_free(node->save_id);
    }
    g_chunk_free(node, priv->node_chunk);
}

gpointer
e_tree_model_node_remove (ETreeModel *etree, ETreePath *path)
{
    ETreeModelPriv *priv = etree->priv;
    ETreePath *parent = path->parent;
    gpointer ret = path->node_data;
    int row, visible = 0, base = 0;
    gboolean dochanged;

    /* work out what range of visible rows to remove */
    if (parent) {
        if (e_tree_model_node_is_visible(etree, path)) {
            base = e_tree_model_row_of_node(etree, path);
            visible = path->visible_descendents + 1;
        }
    } else if (path == priv->root) {
        priv->root = NULL;
        if (priv->root_visible) {
            base = 0;
            visible = path->visible_descendents + 1;
        } else {
            base = 0;
            visible = path->visible_descendents;
        }
    } else {
        g_warning("Trying to remove invalid path %p", path);
        return NULL;
    }

    /* 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 (visible > 0) {
        /* fix up the parent visible counts */
        for (; parent; parent = parent->parent) {
            parent->visible_descendents -= visible;
        }

        /* if we have a lot of nodes to remove, then we dont row_deleted each one */
        /* could probably be tuned, but this'll do, since its normally only when we
           remove the whole lot do we really care */
        dochanged = (visible > 1000) || (visible > (priv->row_array->len / 4));

        e_table_model_pre_change(E_TABLE_MODEL (etree));

        /* and physically remove them */
        if (visible == priv->row_array->len) {
            g_array_set_size(priv->row_array, 0);
        } else {
            memmove(&g_array_index(priv->row_array, ETreePath *, base),
                &g_array_index(priv->row_array, ETreePath *, base+visible),
                (priv->row_array->len - (base+visible)) * sizeof(ETreePath *));
            g_array_set_size(priv->row_array, priv->row_array->len - visible);
        }

        /* tell the system we've removed (these) nodes */
        if (priv->frozen == 0) {
            if (dochanged) {
                e_table_model_changed(E_TABLE_MODEL(etree));
            } else {
                for (row=visible-1;row>=0;row--) {
                    e_table_model_row_deleted(E_TABLE_MODEL(etree), row+base);
                }
            }
        }
    }

    child_free(etree, path);

    return ret;
}

static void
add_visible_descendents_to_array (ETreeModel *etm, ETreePath *node, int *row, int *count)
{
    ETreeModelPriv *priv = etm->priv;

    /* add a row for this node */
    e_table_model_pre_change(E_TABLE_MODEL (etm));
    priv->row_array = g_array_insert_val (priv->row_array, (*row), node);
    if (priv->frozen == 0)
        e_table_model_row_inserted (E_TABLE_MODEL (etm), (*row));
    (*row) ++;
    (*count) ++;

    /* then loop over its children, calling this routine for each
           of them */
    if (node->expanded) {
        ETreePath *child;
        for (child = node->first_child; child;
             child = child->next_sibling) {
            add_visible_descendents_to_array (etm, child, row, count);
        }
    }
}

static void
save_expanded_state_func (char *key, gboolean expanded, gpointer user_data)
{
    if (expanded) {
        xmlNode *root = (xmlNode*)user_data;
        xmlNode *node_root = xmlNewNode (NULL, (xmlChar*) "node");

        xmlAddChild (root, node_root);
        xmlNewChild (node_root, NULL, (xmlChar *) "id", (xmlChar*) key);
    }
}

gboolean
e_tree_model_save_expanded_state (ETreeModel *etm, const char *filename)
{
    xmlDoc *doc;
    xmlNode *root;
    int fd, rv;
    xmlChar *buf;
    int buf_size;
    ETreeModelPriv *priv = etm->priv;

    doc = xmlNewDoc ((xmlChar*) "1.0");
    root = xmlNewDocNode (doc, NULL,
                  (xmlChar *) "expanded_state",
                  NULL);
    xmlDocSetRootElement (doc, root);

    g_hash_table_foreach (priv->expanded_state,
                  (GHFunc)save_expanded_state_func,
                  root);

    fd = open (filename, O_CREAT | O_TRUNC | O_WRONLY, 0777);

    xmlDocDumpMemory (doc, &buf, &buf_size);

    if (buf == NULL) {
        g_error ("Failed to write %s: xmlBufferCreate() == NULL", filename);
        return FALSE;
    }

    rv = write (fd, buf, buf_size);
    xmlFree (buf);
    close (fd);

    if (0 > rv) {
        g_error ("Failed to write new %s: %d\n", filename, errno);
        unlink (filename);
        return FALSE;
    }

    return TRUE;
}

static char *
get_string_value (xmlNode *node,
          const char *name)
{
    xmlNode *p;
    xmlChar *xml_string;
    char *retval;

    p = e_xml_get_child_by_name (node, (xmlChar *) name);
    if (p == NULL)
        return NULL;

    p = e_xml_get_child_by_name (p, (xmlChar *) "text");
    if (p == NULL) /* there's no text between the tags, return the empty string */
        return g_strdup("");

    xml_string = xmlNodeListGetString (node->doc, p, 1);
    retval = g_strdup ((char *) xml_string);
    xmlFree (xml_string);

    return retval;
}

gboolean
e_tree_model_load_expanded_state (ETreeModel *etm, const char *filename)
{
    ETreeModelPriv *priv = etm->priv;
    xmlDoc *doc;
    xmlNode *root;
    xmlNode *child;

    doc = xmlParseFile (filename);
    if (!doc)
        return FALSE;

    root = xmlDocGetRootElement (doc);
    if (root == NULL || strcmp (root->name, "expanded_state")) {
        xmlFreeDoc (doc);
        return FALSE;
    }

    for (child = root->childs; child; child = child->next) {
        char *id;

        if (strcmp (child->name, "node")) {
            g_warning ("unknown node '%s' in %s", child->name, filename);
            continue;
        }

        id = get_string_value (child, "id");

        g_hash_table_insert (priv->expanded_state, id, (gpointer)TRUE);
    }

    xmlFreeDoc (doc);

    return TRUE;
}


void
e_tree_model_node_set_save_id (ETreeModel *etm, ETreePath *node, const char *id)
{
    char *key;
    gboolean expanded_state;
    ETreeModelPriv *priv;

    g_return_if_fail (E_TREE_MODEL (etm));
    g_return_if_fail (node);

    priv = etm->priv;

    if (g_hash_table_lookup_extended (priv->expanded_state,
                      id, (gpointer*)&key, (gpointer*)&expanded_state)) {

        e_tree_model_node_set_expanded (etm, node,
                        expanded_state);

        /* important that this comes after the e_tree_model_node_set_expanded */
        node->save_id = key;
    }
    else {
        node->save_id = g_strdup (id);

        g_hash_table_insert (priv->expanded_state, node->save_id, (gpointer)node->expanded);
    }
}



void
e_tree_model_node_set_compare_function (ETreeModel *tree_model,
                    ETreePath *node,
                    ETreePathCompareFunc compare)
{
    gboolean need_sort;

    g_return_if_fail (E_TREE_MODEL (tree_model));
    g_return_if_fail (node);

    need_sort = (compare != node->compare);

    node->compare = compare;

    if (need_sort)
        e_tree_model_node_sort (tree_model, node);
}

typedef struct {
    ETreeModel *model;
    ETreePath *path;
    ETreePathCompareFunc compare;
    gboolean was_expanded;
} ETreeSortInfo;

static gint
e_tree_model_node_compare (ETreeSortInfo *info1, ETreeSortInfo *info2)
{
    return info1->compare (info1->model, info1->path, info2->path);
}

void
e_tree_model_node_sort (ETreeModel *tree_model,
            ETreePath *node)
{
    int num_nodes = node->num_children;
    ETreeSortInfo *sort_info;
    int i;
    int child_index;
    gboolean node_expanded = e_tree_model_node_is_expanded (tree_model, node);
    ETreeModelPriv *priv = tree_model->priv;;

    g_return_if_fail (E_TREE_MODEL (tree_model));
    g_return_if_fail (node);

    g_return_if_fail (node->compare);

    if (num_nodes == 0)
        return;

    e_table_model_pre_change(E_TABLE_MODEL (tree_model));

    sort_info = g_new (ETreeSortInfo, num_nodes);

    child_index = e_tree_model_row_of_node (tree_model, node) + 1;

    /* collect our info and remove the children */
    for (i = 0; i < num_nodes; i ++) {
        sort_info[i].path = node->first_child;
        sort_info[i].was_expanded = e_tree_model_node_is_expanded (tree_model, sort_info[i].path);
        sort_info[i].model = tree_model;
        sort_info[i].compare = node->compare;

        e_tree_model_node_set_expanded(tree_model, sort_info[i].path, FALSE);
        if (node_expanded)
            priv->row_array = g_array_remove_index (priv->row_array, child_index);
        e_tree_path_unlink (sort_info[i].path);
    }

    /* sort things */
    qsort (sort_info, num_nodes, sizeof(ETreeSortInfo), (GCompareFunc)e_tree_model_node_compare);
    
    /* reinsert the children nodes into the tree in the sorted order */
    for (i = 0; i < num_nodes; i ++) {
        e_tree_path_insert (node, i, sort_info[i].path);
        if (node_expanded)
            priv->row_array = g_array_insert_val (priv->row_array, child_index + i,
                                  sort_info[i].path);
    }

    /* make another pass expanding the children as needed.  

       XXX this used to be in the loop above, but a recent change
       (either here or in the etable code) causes an assertion and
       a crash */
    for (i = 0; i < num_nodes; i ++) {
        e_tree_model_node_set_expanded (tree_model, sort_info[i].path, sort_info[i].was_expanded);
    }

    g_free (sort_info);

    if (priv->frozen == 0)
        e_table_model_changed (E_TABLE_MODEL (tree_model));
}