aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/table/e-tree-table-adapter.c
blob: 552b15ddf6406db96a703f86cc74994d119f92df (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * e-tree-table-adapter.c
 * Copyright 2000, 2001, Ximian, Inc.
 *
 * Authors:
 *   Chris Lahey <clahey@ximian.com>
 *   Chris Toshok <toshok@ximian.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License, version 2, as published by the Free Software Foundation.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */


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

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

#include <gtk/gtksignal.h>
#include <gnome-xml/tree.h>
#include <gnome-xml/parser.h>
#include "gal/util/e-util.h"
#include "gal/util/e-xml-utils.h"
#include "e-tree-table-adapter.h"

#define PARENT_TYPE E_TABLE_MODEL_TYPE
#define d(x)

#define INCREMENT_AMOUNT 100

static ETableModelClass *parent_class;


struct ETreeTableAdapterPriv {
    ETreeModel  *source;
    int          n_map;
    int          n_vals_allocated;
    ETreePath   *map_table;
    GHashTable  *attributes;

    guint        root_visible : 1;

    int          last_access;

    int          tree_model_pre_change_id;
    int          tree_model_no_change_id;
    int          tree_model_node_changed_id;
    int          tree_model_node_data_changed_id;
    int          tree_model_node_col_changed_id;
    int          tree_model_node_inserted_id;
    int          tree_model_node_removed_id;
    int          tree_model_node_request_collapse_id;
};

typedef struct ETreeTableAdapterNode {
    guint expanded : 1;
    guint expandable : 1;
    guint expandable_set : 1;

    /* parent/child/sibling pointers */
    guint32                num_visible_children;
} ETreeTableAdapterNode;

static ETreeTableAdapterNode *
find_node(ETreeTableAdapter *adapter, ETreePath path)
{
    ETreeTableAdapterNode *node;

    if (path == NULL)
        return NULL;

    if (e_tree_model_has_save_id(adapter->priv->source)) {
        char *save_id;
        save_id = e_tree_model_get_save_id(adapter->priv->source, path);
        node = g_hash_table_lookup(adapter->priv->attributes, save_id);
        g_free(save_id);
    } else {
        node = g_hash_table_lookup(adapter->priv->attributes, path);
    }
    if (node && !node->expandable_set) {
        node->expandable = e_tree_model_node_is_expandable(adapter->priv->source, path);
        node->expandable_set = 1;
    }

    return node;
}

static ETreeTableAdapterNode *
find_or_create_node(ETreeTableAdapter *etta, ETreePath path)
{
    ETreeTableAdapterNode *node;

    node = find_node(etta, path);

    if (!node) {
        node = g_new(ETreeTableAdapterNode, 1);
        if (e_tree_model_node_is_root(etta->priv->source, path))
            node->expanded = TRUE;
        else
            node->expanded = e_tree_model_get_expanded_default(etta->priv->source);
        node->expandable = e_tree_model_node_is_expandable(etta->priv->source, path);
        node->expandable_set = 1;
        node->num_visible_children = 0;

        if (e_tree_model_has_save_id(etta->priv->source)) {
            char *save_id;
            save_id = e_tree_model_get_save_id(etta->priv->source, path);
            g_hash_table_insert(etta->priv->attributes, save_id, node);
        } else {
            g_hash_table_insert(etta->priv->attributes, path, node);
        }
    }

    return node;
}

static gboolean
e_tree_table_adapter_node_would_be_expanded (ETreeTableAdapter *etta, ETreePath path)
{
    ETreeTableAdapterNode *node;

    if (e_tree_model_node_is_root(etta->priv->source, path) &&
        !etta->priv->root_visible)
        return TRUE;

    node = find_node (etta, path);
    if (node)
        return node->expanded;
    else {
        if (e_tree_model_node_is_root(etta->priv->source, path))
            return TRUE;
        else
            return e_tree_model_get_expanded_default(etta->priv->source);
    }
}

static void
add_expanded_node(ETreeTableAdapter *etta, char *save_id, gboolean expanded)
{
    ETreeTableAdapterNode *node;

    node = g_hash_table_lookup(etta->priv->attributes, save_id);

    if (node) {
        node->expandable_set = 0;
        node->expanded = expanded;
        return;
    }

    node = g_new(ETreeTableAdapterNode, 1);

    node->expanded = expanded;
    node->expandable = 0;
    node->expandable_set = 0;
    node->num_visible_children = 0;

    g_hash_table_insert(etta->priv->attributes, save_id, node);
}

static void
etta_expand_to(ETreeTableAdapter *etta, int size)
{
    if (size > etta->priv->n_vals_allocated) {
        etta->priv->n_vals_allocated = MAX(etta->priv->n_vals_allocated + INCREMENT_AMOUNT, size);
        etta->priv->map_table = g_renew (ETreePath, etta->priv->map_table, etta->priv->n_vals_allocated);
    }

}

static void
etta_update_parent_child_counts(ETreeTableAdapter *etta, ETreePath path, int change)
{
    for (path = e_tree_model_node_get_parent(etta->priv->source, path);
         path;
         path = e_tree_model_node_get_parent(etta->priv->source, path)) {
        ETreeTableAdapterNode *node = find_or_create_node(etta, path);
        node->num_visible_children += change;
    }
    etta->priv->n_map += change;
}

static int
find_next_node_maybe_deleted(ETreeTableAdapter *adapter, int row)
{
    ETreePath path = adapter->priv->map_table[row];
    if (path) {
        ETreeTableAdapterNode *current = find_node (adapter, path);

        row += (current ? current->num_visible_children : 0) + 1;
        if (row >= adapter->priv->n_map)
            return -1;
        return row;
    } else
        return -1;
}

static int
find_first_child_node_maybe_deleted(ETreeTableAdapter *adapter, int row)
{
    if (row != -1) {
        ETreePath path = adapter->priv->map_table[row];
        if (e_tree_table_adapter_node_would_be_expanded (adapter, path)) {
            row ++;
            if (row >= adapter->priv->n_map)
                return -1;
            return row;
        } else
            return -1;
    } else
        return 0;
}

static int
find_next_node(ETreeTableAdapter *adapter, int row)
{
    ETreePath path = adapter->priv->map_table[row];
    if (path) {
        ETreePath next_sibling = e_tree_model_node_get_next(adapter->priv->source, path);
        ETreeTableAdapterNode *current = find_node (adapter, path);
        if (next_sibling) {
            row += (current ? current->num_visible_children : 0) + 1;
            if (row > adapter->priv->n_map)
                return -1;
            return row;
        } else
            return -1;
    } else
        return -1;
}

static int
find_first_child_node(ETreeTableAdapter *adapter, int row)
{
    if (row != -1) {
        ETreePath path = adapter->priv->map_table[row];
        ETreePath first_child = e_tree_model_node_get_first_child(adapter->priv->source, path);
        if (first_child && e_tree_table_adapter_node_is_expanded (adapter, path)) {
            row ++;
            if (row > adapter->priv->n_map)
                return -1;
            return row;
        } else
            return -1;
    } else
        return 0;
}

static int
find_child_row_num_maybe_deleted(ETreeTableAdapter *etta, int row, ETreePath path)
{
    row = find_first_child_node_maybe_deleted(etta, row);

    while (row != -1 && path != etta->priv->map_table[row]) {
        row = find_next_node_maybe_deleted(etta, row);
    }

    return row;
}

static int
find_row_num(ETreeTableAdapter *etta, ETreePath path)
{
    int depth;
    ETreePath *sequence;
    int i;
    int row;

    if (etta->priv->map_table == NULL)
        return -1;
    if (etta->priv->n_map == 0)
        return -1;

    if (path == NULL)
        return -1;

    if (etta->priv->last_access != -1) {
        int end = MIN(etta->priv->n_map, etta->priv->last_access + 10);
        int start = MAX(0, etta->priv->last_access - 10);
        int initial = MAX (MIN (etta->priv->last_access, end), start);
        for (i = initial; i < end; i++) {
            if(etta->priv->map_table[i] == path) {
                d(g_print("Found last access %d at row %d. (find_row_num)\n", etta->priv->last_access, i));
                return i;
            }
        }
        for (i = initial - 1; i >= start; i--) {
            if(etta->priv->map_table[i] == path) {
                d(g_print("Found last access %d at row %d. (find_row_num)\n", etta->priv->last_access, i));
                return i;
            }
        }
    }


    depth = e_tree_model_node_depth(etta->priv->source, path);

    sequence = g_new(ETreePath, depth + 1);

    sequence[0] = path;

    for (i = 0; i < depth; i++) {
        sequence[i + 1] = e_tree_model_node_get_parent(etta->priv->source, sequence[i]);

        if (!e_tree_table_adapter_node_is_expanded (etta, sequence[i + 1])) {
            g_free(sequence);
            return -1;
        }
    }

    row = 0;

    for (i = depth; i >= 0; i --) {
        while (row != -1 && row < etta->priv->n_map && sequence[i] != etta->priv->map_table[row]) {
            row = find_next_node(etta, row);
        }
        if (row >= etta->priv->n_map)
            break;
        if (row == -1)
            break;
        if (i == 0)
            break;
        row = find_first_child_node(etta, row);
    }
    g_free (sequence);

    if (row >= etta->priv->n_map)
        row = -1;

    d(g_print("Didn't find last access %d. Setting to %d. (find_row_num)\n", etta->priv->last_access, row));
    etta->priv->last_access = row;
    return row;
}

static int
array_size_from_path(ETreeTableAdapter *etta, ETreePath path)
{
    int size = 1;

    if (e_tree_table_adapter_node_is_expanded (etta, path)) {
        ETreePath children;

        for (children = e_tree_model_node_get_first_child(etta->priv->source, path); 
             children; 
             children = e_tree_model_node_get_next(etta->priv->source, children)) {
            size += array_size_from_path(etta, children);
        }
    }

    return size;
}

static int
fill_array_from_path(ETreeTableAdapter *etta, ETreePath *array, ETreePath path)
{
    ETreeTableAdapterNode *node = NULL;
    int index = 0;

    array[index] = path;

    index ++;

    node = find_node(etta, path);

    if (e_tree_table_adapter_node_is_expanded (etta, path)) {
        ETreePath children;

        if (!node)
            node = find_or_create_node(etta, path);

        for (children = e_tree_model_node_get_first_child(etta->priv->source, path); 
             children; 
             children = e_tree_model_node_get_next(etta->priv->source, children)) {
            index += fill_array_from_path(etta, array + index, children);
        }
    }

    if (node)
        node->num_visible_children = index - 1;

    return index;
}

static void
free_string (gpointer key, gpointer value, gpointer data)
{
    g_free(key);
}

static void
etta_destroy (GtkObject *object)
{
    ETreeTableAdapter *etta = E_TREE_TABLE_ADAPTER (object);

    if (etta->priv->source && e_tree_model_has_save_id(etta->priv->source)) {
        g_hash_table_foreach(etta->priv->attributes, free_string, NULL);
    }
    g_hash_table_destroy (etta->priv->attributes);

    if (etta->priv->source) {
        gtk_signal_disconnect (GTK_OBJECT (etta->priv->source),
                       etta->priv->tree_model_pre_change_id);
        gtk_signal_disconnect (GTK_OBJECT (etta->priv->source),
                       etta->priv->tree_model_no_change_id);
        gtk_signal_disconnect (GTK_OBJECT (etta->priv->source),
                       etta->priv->tree_model_node_changed_id);
        gtk_signal_disconnect (GTK_OBJECT (etta->priv->source),
                       etta->priv->tree_model_node_data_changed_id);
        gtk_signal_disconnect (GTK_OBJECT (etta->priv->source),
                       etta->priv->tree_model_node_col_changed_id);
        gtk_signal_disconnect (GTK_OBJECT (etta->priv->source),
                       etta->priv->tree_model_node_inserted_id);
        gtk_signal_disconnect (GTK_OBJECT (etta->priv->source),
                       etta->priv->tree_model_node_removed_id);
        gtk_signal_disconnect (GTK_OBJECT (etta->priv->source),
                       etta->priv->tree_model_node_request_collapse_id);

        gtk_object_unref (GTK_OBJECT (etta->priv->source));
        etta->priv->source = NULL;

        etta->priv->tree_model_pre_change_id = 0;
        etta->priv->tree_model_no_change_id = 0;
        etta->priv->tree_model_node_changed_id = 0;
        etta->priv->tree_model_node_data_changed_id = 0;
        etta->priv->tree_model_node_col_changed_id = 0;
        etta->priv->tree_model_node_inserted_id = 0;
        etta->priv->tree_model_node_removed_id = 0;
        etta->priv->tree_model_node_request_collapse_id = 0;
    }

    g_free (etta->priv->map_table);

    g_free (etta->priv);

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

static int
etta_column_count (ETableModel *etm)
{
    ETreeTableAdapter *etta = (ETreeTableAdapter *)etm;

    return e_tree_model_column_count (etta->priv->source);
}

static gboolean
etta_has_save_id (ETableModel *etm)
{
    ETreeTableAdapter *etta = (ETreeTableAdapter *)etm;

    return e_tree_model_has_save_id (etta->priv->source);
}

static gchar *
etta_get_save_id (ETableModel *etm, int row)
{
    ETreeTableAdapter *etta = (ETreeTableAdapter *)etm;

    if (etta->priv->root_visible)
        return e_tree_model_get_save_id (etta->priv->source, etta->priv->map_table [row]);
    else
        return e_tree_model_get_save_id (etta->priv->source, etta->priv->map_table [row + 1]);
}

static gboolean
etta_has_change_pending (ETableModel *etm)
{
    ETreeTableAdapter *etta = (ETreeTableAdapter *)etm;

    return e_tree_model_has_change_pending (etta->priv->source);
}


static int
etta_row_count (ETableModel *etm)
{
    ETreeTableAdapter *etta = (ETreeTableAdapter *)etm;

    if (etta->priv->root_visible)
        return etta->priv->n_map;
    else {
        if (etta->priv->n_map > 0)
            return etta->priv->n_map - 1;
        else
            return 0;
    }
}

static void *
etta_value_at (ETableModel *etm, int col, int row)
{
    ETreeTableAdapter *etta = (ETreeTableAdapter *)etm;

#if 0
    etta->priv->last_access = row;
    d(g_print("g) Setting last_access to %d\n", row));
#endif

    switch (col) {
    case -1:
        if (etta->priv->root_visible)
            return etta->priv->map_table [row];
        else
            return etta->priv->map_table [row + 1];
    case -2:
        return etta->priv->source;
    case -3:
        return etta;
    default:
        if (etta->priv->root_visible)
            return e_tree_model_value_at (etta->priv->source, etta->priv->map_table [row], col);
        else
            return e_tree_model_value_at (etta->priv->source, etta->priv->map_table [row + 1], col);
    }
}

static void
etta_set_value_at (ETableModel *etm, int col, int row, const void *val)
{
    ETreeTableAdapter *etta = (ETreeTableAdapter *)etm;

    etta->priv->last_access = row;
    d(g_print("h) Setting last_access to %d\n", row));
    if (etta->priv->root_visible)
        e_tree_model_set_value_at (etta->priv->source, etta->priv->map_table [row], col, val);
    else
        e_tree_model_set_value_at (etta->priv->source, etta->priv->map_table [row + 1], col, val);
}

static gboolean
etta_is_cell_editable (ETableModel *etm, int col, int row)
{
    ETreeTableAdapter *etta = (ETreeTableAdapter *)etm;

    if (etta->priv->root_visible)
        return e_tree_model_node_is_editable (etta->priv->source, etta->priv->map_table [row], col);
    else
        return e_tree_model_node_is_editable (etta->priv->source, etta->priv->map_table [row + 1], col);
}

static void
etta_append_row (ETableModel *etm, ETableModel *source, int row)
{
#if 0
    ETreeTableAdapter *etta = (ETreeTableAdapter *)etm;
    e_table_model_append_row (etta->priv->source, source, row);
#endif
}

static void *
etta_duplicate_value (ETableModel *etm, int col, const void *value)
{
    ETreeTableAdapter *etta = (ETreeTableAdapter *)etm;

    return e_tree_model_duplicate_value (etta->priv->source, col, value);
}

static void
etta_free_value (ETableModel *etm, int col, void *value)
{
    ETreeTableAdapter *etta = (ETreeTableAdapter *)etm;

    e_tree_model_free_value (etta->priv->source, col, value);
}

static void *
etta_initialize_value (ETableModel *etm, int col)
{
    ETreeTableAdapter *etta = (ETreeTableAdapter *)etm;

    return e_tree_model_initialize_value (etta->priv->source, col);
}

static gboolean
etta_value_is_empty (ETableModel *etm, int col, const void *value)
{
    ETreeTableAdapter *etta = (ETreeTableAdapter *)etm;

    return e_tree_model_value_is_empty (etta->priv->source, col, value);
}

static char *
etta_value_to_string (ETableModel *etm, int col, const void *value)
{
    ETreeTableAdapter *etta = (ETreeTableAdapter *)etm;

    return e_tree_model_value_to_string (etta->priv->source, col, value);
}

static void
etta_class_init (ETreeTableAdapterClass *klass)
{
    ETableModelClass *table_class   = (ETableModelClass *) klass;
    GtkObjectClass *object_class    = (GtkObjectClass *) klass;

    parent_class                    = gtk_type_class (PARENT_TYPE);
    
    object_class->destroy           = etta_destroy;

    table_class->column_count       = etta_column_count;
    table_class->row_count          = etta_row_count;
    table_class->append_row         = etta_append_row;

    table_class->value_at           = etta_value_at;
    table_class->set_value_at       = etta_set_value_at;
    table_class->is_cell_editable   = etta_is_cell_editable;

    table_class->has_save_id        = etta_has_save_id;
    table_class->get_save_id        = etta_get_save_id;

    table_class->has_change_pending = etta_has_change_pending;
    table_class->duplicate_value    = etta_duplicate_value;
    table_class->free_value         = etta_free_value;
    table_class->initialize_value   = etta_initialize_value;
    table_class->value_is_empty     = etta_value_is_empty;
    table_class->value_to_string    = etta_value_to_string;
}

static void
etta_init (ETreeTableAdapter *etta)
{
    etta->priv                                      = g_new(ETreeTableAdapterPriv, 1);

    etta->priv->source                              = NULL;

    etta->priv->n_map                               = 0;
    etta->priv->n_vals_allocated                    = 0;
    etta->priv->map_table                           = NULL;
    etta->priv->attributes                          = NULL;

    etta->priv->root_visible                        = TRUE;

    etta->priv->last_access                         = 0;

    etta->priv->tree_model_pre_change_id            = 0;
    etta->priv->tree_model_no_change_id             = 0;
    etta->priv->tree_model_node_changed_id          = 0;
    etta->priv->tree_model_node_data_changed_id     = 0;
    etta->priv->tree_model_node_col_changed_id      = 0;
    etta->priv->tree_model_node_inserted_id         = 0;
    etta->priv->tree_model_node_removed_id          = 0;
    etta->priv->tree_model_node_request_collapse_id = 0;
}

E_MAKE_TYPE(e_tree_table_adapter, "ETreeTableAdapter", ETreeTableAdapter, etta_class_init, etta_init, PARENT_TYPE)

static void
etta_proxy_pre_change (ETreeModel *etm, ETreeTableAdapter *etta)
{
    e_table_model_pre_change(E_TABLE_MODEL(etta));
}

static void
etta_proxy_no_change (ETreeModel *etm, ETreeTableAdapter *etta)
{
    e_table_model_no_change(E_TABLE_MODEL(etta));
}

static void
etta_proxy_node_changed (ETreeModel *etm, ETreePath path, ETreeTableAdapter *etta)
{
    if (e_tree_model_node_is_root(etm, path)) {
        int size;

        size = array_size_from_path(etta, path);
        etta_expand_to(etta, size);
        etta->priv->n_map = size;
        fill_array_from_path(etta, etta->priv->map_table, path);
    } else {
        int row = find_row_num(etta, path);
        int size;
        int old_size;
        ETreeTableAdapterNode *node;

        if (row == -1) {
            e_table_model_no_change(E_TABLE_MODEL(etta));
            return;
        }

        size = array_size_from_path(etta, path);

        node = find_node(etta, path);
        if (node)
            old_size = node->num_visible_children + 1;
        else
            old_size = 1;

        etta_expand_to(etta, etta->priv->n_map + size - old_size);

        memmove(etta->priv->map_table + row + size,
            etta->priv->map_table + row + old_size,
            (etta->priv->n_map - row - old_size) * sizeof (ETreePath));
        fill_array_from_path(etta, etta->priv->map_table + row, path);
        etta_update_parent_child_counts(etta, path, size - old_size);
    }

    e_table_model_changed(E_TABLE_MODEL(etta));
}

static void
etta_proxy_node_data_changed (ETreeModel *etm, ETreePath path, ETreeTableAdapter *etta)
{
    int row = find_row_num(etta, path);
    if (row != -1) {
        if (etta->priv->root_visible)
            e_table_model_row_changed(E_TABLE_MODEL(etta), row);
        else if (row != 0)
            e_table_model_row_changed(E_TABLE_MODEL(etta), row - 1);
        else
            e_table_model_no_change(E_TABLE_MODEL(etta));
    } else
        e_table_model_no_change(E_TABLE_MODEL(etta));
}

static void
etta_proxy_node_col_changed (ETreeModel *etm, ETreePath path, int col, ETreeTableAdapter *etta)
{
    int row = find_row_num(etta, path);
    if (row != -1) {
        if (etta->priv->root_visible)
            e_table_model_cell_changed(E_TABLE_MODEL(etta), col, row);
        else if (row != 0)
            e_table_model_cell_changed(E_TABLE_MODEL(etta), col, row - 1);
        else
            e_table_model_no_change(E_TABLE_MODEL(etta));
    } else
        e_table_model_no_change(E_TABLE_MODEL(etta));
}

static void
etta_proxy_node_inserted (ETreeModel *etm, ETreePath parent, ETreePath child, ETreeTableAdapter *etta)
{
    int row;

    if (e_tree_model_node_is_root(etm, child)) {
        row = 0;
    } else {
        ETreePath children;
        int parent_row;
        ETreeTableAdapterNode *parent_node;

        parent_row = find_row_num(etta, parent);
        if (parent_row == -1) {
            e_table_model_no_change(E_TABLE_MODEL(etta));
            return;
        }

        parent_node = find_or_create_node(etta, parent);
        if (parent_node->expandable != e_tree_model_node_is_expandable(etta->priv->source, parent)) {
            e_table_model_pre_change(E_TABLE_MODEL(etta));
            parent_node->expandable = e_tree_model_node_is_expandable(etta->priv->source, parent);
            parent_node->expandable_set = 1;
            if (etta->priv->root_visible)
                e_table_model_row_changed(E_TABLE_MODEL(etta), parent_row);
            else if (parent_row != 0)
                e_table_model_row_changed(E_TABLE_MODEL(etta), parent_row - 1);
            else
                e_table_model_no_change(E_TABLE_MODEL(etta));
        }

        if (!e_tree_table_adapter_node_is_expanded (etta, parent)) {
            e_table_model_no_change(E_TABLE_MODEL(etta));
            return;
        }

        row = find_first_child_node(etta, parent_row);
        children = e_tree_model_node_get_first_child(etta->priv->source, parent);

        while (row != -1 &&
               row <= parent_row + parent_node->num_visible_children &&
               children != NULL &&
               children == etta->priv->map_table[row]) {
            children = e_tree_model_node_get_next(etta->priv->source, children);
            row = find_next_node(etta, row);
        }
    }

    if (row != -1) {
        int size;

        size = array_size_from_path(etta, child);

        etta_expand_to(etta, etta->priv->n_map + size);

        memmove(etta->priv->map_table + row + size,
            etta->priv->map_table + row,
            (etta->priv->n_map - row) * sizeof (ETreePath));

        fill_array_from_path(etta, etta->priv->map_table + row, child);
        etta_update_parent_child_counts(etta, child, size);

        if (etta->priv->root_visible)
            e_table_model_rows_inserted(E_TABLE_MODEL(etta), row, size);
        else if (row != 0)
            e_table_model_rows_inserted(E_TABLE_MODEL(etta), row - 1, size);
        else
            e_table_model_rows_inserted(E_TABLE_MODEL(etta), 0, size - 1);
    } else
        e_table_model_no_change(E_TABLE_MODEL(etta));
}

static void
etta_proxy_node_removed (ETreeModel *etm, ETreePath parent, ETreePath child, int old_position, ETreeTableAdapter *etta)
{
    int parent_row = find_row_num(etta, parent);
    int row = find_child_row_num_maybe_deleted(etta, parent_row, child);
    ETreeTableAdapterNode *parent_node = find_node(etta, parent);
    if (parent_row != -1 && parent_node) {
        if (parent_node->expandable != e_tree_model_node_is_expandable(etta->priv->source, parent)) {
            e_table_model_pre_change(E_TABLE_MODEL(etta));
            parent_node->expandable = e_tree_model_node_is_expandable(etta->priv->source, parent);
            if (etta->priv->root_visible)
                e_table_model_row_changed(E_TABLE_MODEL(etta), parent_row);
            else if (parent_row != 0)
                e_table_model_row_changed(E_TABLE_MODEL(etta), parent_row - 1);
            else
                e_table_model_no_change(E_TABLE_MODEL(etta));
        }
    }

    if (row != -1) {
        ETreeTableAdapterNode *node = find_node(etta, child);
        int to_remove = (node ? node->num_visible_children : 0) + 1;

        memmove(etta->priv->map_table + row,
            etta->priv->map_table + row + to_remove,
            (etta->priv->n_map - row - to_remove) * sizeof (ETreePath));

        if (parent_node)
            parent_node->num_visible_children -= to_remove;
        if (parent)
            etta_update_parent_child_counts(etta, parent, - to_remove);

        if (etta->priv->root_visible)
            e_table_model_rows_deleted(E_TABLE_MODEL(etta), row, to_remove);
        else if (row != 0)
            e_table_model_rows_deleted(E_TABLE_MODEL(etta), row - 1, to_remove);
        else
            e_table_model_rows_deleted(E_TABLE_MODEL(etta), 0, to_remove - 1);
    } else
        e_table_model_no_change(E_TABLE_MODEL(etta));
}

static void
etta_proxy_node_request_collapse (ETreeModel *etm, ETreePath node, ETreeTableAdapter *etta)
{
    e_tree_table_adapter_node_set_expanded(etta, node, FALSE);
}

ETableModel *
e_tree_table_adapter_construct (ETreeTableAdapter *etta, ETreeModel *source)
{
    ETreePath root;

    etta->priv->source = source;
    gtk_object_ref (GTK_OBJECT (source));

    if (e_tree_model_has_save_id(source))
        etta->priv->attributes = g_hash_table_new(g_str_hash, g_str_equal);
    else
        etta->priv->attributes = g_hash_table_new(NULL, NULL);

    root = e_tree_model_get_root (source);

    if (root) {
        etta->priv->n_map = array_size_from_path(etta, root);
        etta->priv->n_vals_allocated = etta->priv->n_map;
        etta->priv->map_table = g_new(ETreePath, etta->priv->n_map);
        fill_array_from_path(etta, etta->priv->map_table, root);
    }

    etta->priv->tree_model_pre_change_id            = gtk_signal_connect (GTK_OBJECT (source), "pre_change",
                                          GTK_SIGNAL_FUNC (etta_proxy_pre_change), etta);
    etta->priv->tree_model_no_change_id             = gtk_signal_connect (GTK_OBJECT (source), "no_change",
                                          GTK_SIGNAL_FUNC (etta_proxy_no_change), etta);
    etta->priv->tree_model_node_changed_id          = gtk_signal_connect (GTK_OBJECT (source), "node_changed",
                                          GTK_SIGNAL_FUNC (etta_proxy_node_changed), etta);
    etta->priv->tree_model_node_data_changed_id     = gtk_signal_connect (GTK_OBJECT (source), "node_data_changed",
                                          GTK_SIGNAL_FUNC (etta_proxy_node_data_changed), etta);
    etta->priv->tree_model_node_col_changed_id      = gtk_signal_connect (GTK_OBJECT (source), "node_col_changed",
                                          GTK_SIGNAL_FUNC (etta_proxy_node_col_changed), etta);
    etta->priv->tree_model_node_inserted_id         = gtk_signal_connect (GTK_OBJECT (source), "node_inserted",
                                          GTK_SIGNAL_FUNC (etta_proxy_node_inserted), etta);
    etta->priv->tree_model_node_removed_id          = gtk_signal_connect (GTK_OBJECT (source), "node_removed",
                                          GTK_SIGNAL_FUNC (etta_proxy_node_removed), etta);
    etta->priv->tree_model_node_request_collapse_id = gtk_signal_connect (GTK_OBJECT (source), "node_request_collapse",
                                          GTK_SIGNAL_FUNC (etta_proxy_node_request_collapse), etta);

    return E_TABLE_MODEL (etta);
}

ETableModel *
e_tree_table_adapter_new (ETreeModel *source)
{
    ETreeTableAdapter *etta = gtk_type_new (E_TREE_TABLE_ADAPTER_TYPE);

    e_tree_table_adapter_construct (etta, source);

    return (ETableModel *) etta;
}

typedef struct {
    xmlNode *root;
    ETreeModel *tree;
} TreeAndRoot;

static void
save_expanded_state_func (gpointer keyp, gpointer value, gpointer data)
{
    gchar *key = keyp;
    ETreeTableAdapterNode *node = value;
    TreeAndRoot *tar = data;
    xmlNode *root = tar->root;
    ETreeModel *etm = tar->tree;
    xmlNode *xmlnode;

    if (node->expanded != e_tree_model_get_expanded_default(etm)) {
        xmlnode = xmlNewChild (root, NULL, "node", NULL);
        e_xml_set_string_prop_by_name(xmlnode, "id", key);
    }
}

void
e_tree_table_adapter_save_expanded_state (ETreeTableAdapter *etta, const char *filename)
{
    ETreeTableAdapterPriv *priv;
    char *tmp, *slash;
    TreeAndRoot tar;
    xmlDocPtr doc;
    xmlNode *root;
    int ret;
    
    g_return_if_fail(etta != NULL);

    priv = etta->priv; 

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

    e_xml_set_integer_prop_by_name (root, "vers", 2);
    e_xml_set_bool_prop_by_name (root, "default", e_tree_model_get_expanded_default (priv->source));

    tar.root = root;
    tar.tree = etta->priv->source;
    
    g_hash_table_foreach (priv->attributes,
                  save_expanded_state_func,
                  &tar);
    
    tmp = alloca (strlen (filename) + 5);
    slash = strrchr (filename, '/');
    if (slash)
        sprintf (tmp, "%.*s.#%s", slash - filename + 1, filename, slash + 1);
    else
        sprintf (tmp, ".#%s", filename);
    
    ret = e_xml_save_file (tmp, doc);
    if (ret != -1)
        ret = rename (tmp, filename);
    
    if (ret == -1)
        unlink (tmp);
    
    xmlFreeDoc (doc);
}

void
e_tree_table_adapter_load_expanded_state (ETreeTableAdapter *etta, const char *filename)
{
    ETreeTableAdapterPriv *priv;
    xmlDoc *doc;
    xmlNode *root;
    xmlNode *child;
    int vers;
    gboolean model_default, saved_default;
    int size;
    ETreePath path;

    g_return_if_fail(etta != NULL);

    priv = etta->priv;

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

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

    vers = e_xml_get_integer_prop_by_name_with_default (root, "vers", 0);
    if (vers > 2) {
        xmlFreeDoc (doc);
        return;
    }
    model_default = e_tree_model_get_expanded_default (priv->source);
    saved_default = e_xml_get_bool_prop_by_name_with_default (root, "default", !model_default);
    if (saved_default != model_default) {
        xmlFreeDoc (doc);
        return;
    }

    e_table_model_pre_change(E_TABLE_MODEL(etta));

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

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

        id = e_xml_get_string_prop_by_name_with_default (child, "id", "");

        if (!strcmp(id, "")) {
            g_free(id);
            continue;
        }

        add_expanded_node(etta, id, !model_default);
    }
    

    xmlFreeDoc (doc);

    path = e_tree_model_get_root (etta->priv->source);
    size = array_size_from_path(etta, path);
    etta_expand_to(etta, size);
    etta->priv->n_map = size;
    fill_array_from_path(etta, etta->priv->map_table, path);

    e_table_model_changed (E_TABLE_MODEL (etta));
}

void         e_tree_table_adapter_root_node_set_visible (ETreeTableAdapter *etta, gboolean visible)
{
    if (etta->priv->root_visible == visible)
        return;

    e_table_model_pre_change (E_TABLE_MODEL(etta));

    etta->priv->root_visible = visible;
    if (!visible) {
        ETreePath root = e_tree_model_get_root(etta->priv->source);
        if (root)
            e_tree_table_adapter_node_set_expanded(etta, root, TRUE);
    }
    e_table_model_changed(E_TABLE_MODEL(etta));
}

void         e_tree_table_adapter_node_set_expanded (ETreeTableAdapter *etta, ETreePath path, gboolean expanded)
{
    ETreeTableAdapterNode *node;
    int row;

    if (!expanded && e_tree_model_node_is_root (etta->priv->source, path) && !etta->priv->root_visible)
        return;

    node = find_or_create_node(etta, path);

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

    node->expanded = expanded;
        
    row = find_row_num(etta, path);
    if (row != -1) {
        e_table_model_pre_change (E_TABLE_MODEL(etta));

        if (etta->priv->root_visible) {
            e_table_model_pre_change (E_TABLE_MODEL(etta));
            e_table_model_row_changed(E_TABLE_MODEL(etta), row);
        } else if (row != 0) {
            e_table_model_pre_change (E_TABLE_MODEL(etta));
            e_table_model_row_changed(E_TABLE_MODEL(etta), row - 1);
        }

        if (expanded) {
            int num_children = array_size_from_path(etta, path) - 1;
            etta_expand_to(etta, etta->priv->n_map + num_children);
            memmove(etta->priv->map_table + row + 1 + num_children,
                etta->priv->map_table + row + 1,
                (etta->priv->n_map - row - 1) * sizeof (ETreePath));
            fill_array_from_path(etta, etta->priv->map_table + row, path);
            etta_update_parent_child_counts(etta, path, num_children);
            if (num_children != 0) {
                if (etta->priv->root_visible)
                    e_table_model_rows_inserted(E_TABLE_MODEL(etta), row + 1, num_children);
                else
                    e_table_model_rows_inserted(E_TABLE_MODEL(etta), row, num_children);
            } else
                e_table_model_no_change(E_TABLE_MODEL(etta));
        } else {
            int num_children = node->num_visible_children;
            g_assert (etta->priv->n_map >= row + 1 + num_children);
            memmove(etta->priv->map_table + row + 1,
                etta->priv->map_table + row + 1 + num_children,
                (etta->priv->n_map - row - 1 - num_children) * sizeof (ETreePath));
            node->num_visible_children = 0;
            etta_update_parent_child_counts(etta, path, - num_children);
            if (num_children != 0) {
                if (etta->priv->root_visible)
                    e_table_model_rows_deleted(E_TABLE_MODEL(etta), row + 1, num_children);
                else
                    e_table_model_rows_deleted(E_TABLE_MODEL(etta), row, num_children);
            } else
                e_table_model_no_change(E_TABLE_MODEL(etta));
        }
    }
}

void         e_tree_table_adapter_node_set_expanded_recurse (ETreeTableAdapter *etta, ETreePath path, gboolean expanded)
{
    ETreePath children;

    e_tree_table_adapter_node_set_expanded(etta, path, expanded);

    for (children = e_tree_model_node_get_first_child(etta->priv->source, path); 
         children; 
         children = e_tree_model_node_get_next(etta->priv->source, children)) {
        e_tree_table_adapter_node_set_expanded_recurse(etta, children, expanded);
    }
}

ETreePath    e_tree_table_adapter_node_at_row (ETreeTableAdapter *etta, int row)
{
    if (row < 0)
        return NULL;
    if (etta->priv->root_visible) {
        if (row < etta->priv->n_map)
            return etta->priv->map_table[row];
    } else {
        if (row + 1 < etta->priv->n_map)
            return etta->priv->map_table[row + 1];
    }
    return NULL;
}

int    e_tree_table_adapter_row_of_node (ETreeTableAdapter *etta, ETreePath path)
{
    if (etta->priv->root_visible)
        return find_row_num(etta, path);
    else {
        int row_num = find_row_num (etta, path);
        if (row_num != -1)
            return row_num - 1;
        else
            return row_num;
    }
}

gboolean     e_tree_table_adapter_root_node_is_visible(ETreeTableAdapter *etta)
{
    return etta->priv->root_visible;
}

void         e_tree_table_adapter_show_node (ETreeTableAdapter *etta, ETreePath path)
{
    ETreePath parent;

    parent = e_tree_model_node_get_parent(etta->priv->source, path);

    while (parent) {
        e_tree_table_adapter_node_set_expanded(etta, parent, TRUE);
        parent = e_tree_model_node_get_parent(etta->priv->source, parent);
    }
}

gboolean     e_tree_table_adapter_node_is_expanded (ETreeTableAdapter *etta, ETreePath path)
{
    if (!e_tree_model_node_is_expandable (etta->priv->source, path))
        return FALSE;

    return e_tree_table_adapter_node_would_be_expanded (etta, path);
}