aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-config.c
blob: c6676fbb777382fc992bd2dc08ebf6bb3b48f880 (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
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
/*
 * 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:
 *      Michael Zucchi <notzed@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

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

#include <gtk/gtk.h>
#include <glib/gi18n.h>

#include "e-config.h"

#include <glib/gi18n.h>

#define E_CONFIG_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_CONFIG, EConfigPrivate))

#define d(x)

struct _EConfigFactory {
    gchar *id;
    EConfigFactoryFunc func;
    gpointer user_data;
};

struct _menu_node {
    GSList *menu;
    EConfigItemsFunc free;
    EConfigItemsFunc abort;
    EConfigItemsFunc commit;
    gpointer data;
};

struct _widget_node {
    EConfig *config;

    struct _menu_node *context;
    EConfigItem *item;
    GtkWidget *widget; /* widget created by the factory, if any */
    GtkWidget *frame; /* if created by us */

    guint empty:1;      /* set if empty (i.e. hidden) */
};

struct _check_node {
    gchar *pageid;
    EConfigCheckFunc check;
    gpointer data;
};

struct _finish_page_node {
    gchar *pageid;
    gboolean is_finish;
    gint orig_type;
};

struct _EConfigPrivate {
    GList *menus;
    GList *widgets;
    GList *checks;
    GList *finish_pages;
};

static gpointer parent_class;

static void
config_finalize (GObject *object)
{
    EConfig *emp = (EConfig *)object;
    EConfigPrivate *p = emp->priv;
    GList *link;

    d(printf("finalising EConfig %p\n", o));

    g_free(emp->id);

    link = p->menus;
    while (link != NULL) {
        struct _menu_node *node = link->data;

        if (node->free)
            node->free (emp, node->menu, node->data);

        g_free (node);

        link = g_list_delete_link (link, link);
    }

    link = p->widgets;
    while (link != NULL) {
        struct _widget_node *node = link->data;

        /* disconnect the gtk_widget_destroyed function from the widget */
        if (node->widget)
            g_signal_handlers_disconnect_matched (
                node->widget, G_SIGNAL_MATCH_DATA,
                0, 0, NULL, NULL, &node->widget);

        g_free (node);

        link = g_list_delete_link (link, link);
    }

    link = p->checks;
    while (link != NULL) {
        struct _check_node *node = link->data;

        g_free (node->pageid);
        g_free (node);

        link = g_list_delete_link (link, link);
    }

    link = p->finish_pages;
    while (link != NULL) {
        struct _finish_page_node *node = link->data;

        g_free (node->pageid);
        g_free (node);

        link = g_list_delete_link (link, link);
    }

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
config_target_free (EConfig *config,
                    EConfigTarget *target)
{
    g_free (target);
    g_object_unref (config);
}

static void
config_set_target (EConfig *config,
                   EConfigTarget *target)
{
    if (config->target != NULL)
        e_config_target_free (config, target);

    config->target = target;
}

static void
config_class_init (EConfigClass *class)
{
    GObjectClass *object_class;

    parent_class = g_type_class_peek_parent (class);
    g_type_class_add_private (class, sizeof (EConfigPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->finalize = config_finalize;

    class->set_target = config_set_target;
    class->target_free = config_target_free;
}

static void
config_init (EConfig *config)
{
    config->priv = E_CONFIG_GET_PRIVATE (config);
}

/**
 * e_config_get_type:
 *
 * Standard GObject method.  Used to subclass for the concrete
 * implementations.
 *
 * Return value: EConfig type.
 **/
GType
e_config_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0)) {
        static const GTypeInfo type_info = {
            sizeof (EConfigClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) config_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,  /* class_data */
            sizeof (EConfig),
            0,     /* n_preallocs */
            (GInstanceInitFunc) config_init,
            NULL   /* value_table */
        };

        type = g_type_register_static (
            G_TYPE_OBJECT, "EConfig", &type_info, 0);
    }

    return type;
}

/**
 * e_config_construct:
 * @ep: The instance to initialise.
 * @type: The type of configuration manager, @E_CONFIG_BOOK or
 * @E_CONFIG_ASSISTANT.
 * @id: The name of the configuration window this manager drives.
 *
 * Used by implementing classes to initialise base parameters.
 *
 * Return value: @ep is returned.
 **/
EConfig *
e_config_construct (EConfig *ep, gint type, const gchar *id)
{
    g_return_val_if_fail (type == E_CONFIG_BOOK || type == E_CONFIG_ASSISTANT, NULL);

    ep->type = type;
    ep->id = g_strdup (id);

    return ep;
}

/**
 * e_config_add_items:
 * @ec: An initialised implementing instance of EConfig.
 * @items: A list of EConfigItem's to add to the configuration manager
 * @ec.
 * @commitfunc: If supplied, called to commit the configuration items
 * to persistent storage.
 * @abortfunc: If supplied, called to abort/undo the storage of these
 * items permanently.
 * @freefunc: If supplied, called to free the item list (and/or items)
 * once they are no longer needed.
 * @data: Data for the callback methods.
 *
 * Add new EConfigItems to the configuration window.  Nothing will be
 * done with them until the widget is built.
 *
 * TODO: perhaps commit and abort should just be signals.
 **/
void
e_config_add_items(EConfig *ec, GSList *items, EConfigItemsFunc commitfunc, EConfigItemsFunc abortfunc, EConfigItemsFunc freefunc, gpointer data)
{
    struct _menu_node *node;

    node = g_malloc(sizeof(*node));
    node->menu = items;
    node->commit = commitfunc;
    node->abort = abortfunc;
    node->free = freefunc;
    node->data = data;

    ec->priv->menus = g_list_append (ec->priv->menus, node);
}

/**
 * e_config_add_page_check:
 * @ec: Initialised implemeting instance of EConfig.
 * @pageid: pageid to check.
 * @check: checking callback.
 * @data: user-data for the callback.
 *
 * Add a page-checking function callback.  It will be called to validate the
 * data in the given page or pages.  If @pageid is NULL then it will be called
 * to validate every page, or the whole configuration window.
 *
 * In the latter case, the pageid in the callback will be either the
 * specific page being checked, or NULL when the whole config window
 * is being checked.
 *
 * The page check function is used to validate input before allowing
 * the assistant to continue or the notebook to close.
 **/
void
e_config_add_page_check(EConfig *ec, const gchar *pageid, EConfigCheckFunc check, gpointer data)
{
    struct _check_node *cn;

    cn = g_malloc0(sizeof(*cn));
    cn->pageid = g_strdup(pageid);
    cn->check = check;
    cn->data = data;

    ec->priv->checks = g_list_append (ec->priv->checks, cn);
}

static struct _finish_page_node *
find_page_finish (EConfig *config, const gchar *pageid)
{
    GList *link;

    link = config->priv->finish_pages;

    while (link != NULL) {
        struct _finish_page_node *node = link->data;

        if (g_str_equal (node->pageid, pageid))
            return node;

        link = g_list_next (link);
    }

    return NULL;
}

/**
 * e_config_set_page_is_finish:
 * @ec: Initialised implementing instance of EConfig.
 * @pageid: pageid to change the value on.
 * @can_finish: whether the pageid can finish immediately or not.
 *
 * With is_finish set on the pageid the page is treated as the last page in an assistant.
 **/
void
e_config_set_page_is_finish (EConfig *ec, const gchar *pageid, gboolean is_finish)
{
    struct _finish_page_node *fp;

    fp = find_page_finish (ec, pageid);

    if (is_finish) {
        if (!fp) {
            fp = g_malloc0 (sizeof (*fp));
            fp->pageid = g_strdup (pageid);
            ec->priv->finish_pages = g_list_append (
                ec->priv->finish_pages, fp);
        }

        fp->is_finish = TRUE;
    } else {
        if (fp)
            fp->is_finish = FALSE;
    }
}

static void
ec_add_static_items (EConfig *config)
{
    EConfigClass *class;
    GList *link;

    class = E_CONFIG_GET_CLASS (config);
    for (link = class->factories; link != NULL; link = link->next) {
        EConfigFactory *factory = link->data;

        if (factory->id == NULL || strcmp (factory->id, config->id) == 0)
            factory->func (config, factory->user_data);
    }
}

static gint
ep_cmp(gconstpointer ap, gconstpointer bp)
{
    struct _widget_node *a = *((gpointer *)ap);
    struct _widget_node *b = *((gpointer *)bp);

    return strcmp(a->item->path, b->item->path);
}

static struct _widget_node *
ec_assistant_find_page (EConfig *ec, GtkWidget *page, gint *page_index)
{
    struct _widget_node *wn = NULL;
    GList *link;

    g_return_val_if_fail (ec != NULL, NULL);
    g_return_val_if_fail (GTK_IS_ASSISTANT (ec->widget), NULL);
    g_return_val_if_fail (page != NULL, NULL);

    link = ec->priv->widgets;

    while (link != NULL) {
        wn = link->data;

        if (wn->frame == page
            && (wn->item->type == E_CONFIG_PAGE
            || wn->item->type == E_CONFIG_PAGE_START
            || wn->item->type == E_CONFIG_PAGE_FINISH))
                break;

        link = g_list_next (link);
    }

    g_return_val_if_fail (wn != NULL, NULL);

    if (wn->frame != page)
        wn = NULL;

    if (page_index) {
        if (wn) {
            GtkAssistant *assistant = GTK_ASSISTANT (ec->widget);
            gint index, count = gtk_assistant_get_n_pages (assistant);

            for (index = 0; index < count; index++) {
                if (gtk_assistant_get_nth_page (assistant, index) == page)
                    break;
            }

            if (index == count)
                index = -1;
            *page_index = index;
        } else {
            *page_index = -1;
        }
    }

    return wn;
}

static void
ec_assistant_check_current (EConfig *ec)
{
    struct _widget_node *wn;
    struct _finish_page_node *fp;
    GtkAssistant *assistant;
    GtkWidget *page;
    gint page_no;

    g_return_if_fail (GTK_IS_ASSISTANT (ec->widget));

    assistant = GTK_ASSISTANT (ec->widget);
    page_no = gtk_assistant_get_current_page (assistant);

    /* no page selected yet */
    if (page_no == -1)
        return;

    page = gtk_assistant_get_nth_page (assistant, page_no);
    g_return_if_fail (page != NULL);

    wn = ec_assistant_find_page (ec, page, NULL);
    g_return_if_fail (wn != NULL);

    /* this should come first, as the check function can change the finish state of the page */
    gtk_assistant_set_page_complete (assistant, page, e_config_page_check (ec, wn->item->path));

    fp = find_page_finish (ec, wn->item->path);
    if (fp) {
        GtkAssistantPageType pt = gtk_assistant_get_page_type (assistant, page);

        if (fp->is_finish && pt != GTK_ASSISTANT_PAGE_CONFIRM) {
            if (fp->orig_type == GTK_ASSISTANT_PAGE_CONTENT)
                fp->orig_type = pt;
            gtk_assistant_set_page_type (assistant, page, GTK_ASSISTANT_PAGE_CONFIRM);
        } else if (!fp->is_finish && pt != fp->orig_type) {
            gtk_assistant_set_page_type (assistant, page, fp->orig_type);
        }
    }

    gtk_assistant_update_buttons_state (assistant);
}

static gint
ec_assistant_forward (gint current_page, gpointer user_data)
{
    EConfig *ec = user_data;
    struct _widget_node *wn;
    gint next_page = current_page + 1;
    GList *link = NULL;

    d(printf("next page from '%d'\n", current_page));

    wn = ec_assistant_find_page (ec, gtk_assistant_get_nth_page (GTK_ASSISTANT (ec->widget), current_page), NULL);

    /* XXX This is a little kludgy.  We have to find
     *     the GList node for the page we just found. */
    if (wn != NULL)
        link = g_list_find (ec->priv->widgets, wn);

    if (link && link->next) {
        for (link = link->next; link->next; link = link->next) {
            wn = link->data;

            if (!wn->empty && wn->frame != NULL
                && (wn->item->type == E_CONFIG_PAGE
                || wn->item->type == E_CONFIG_PAGE_START
                || wn->item->type == E_CONFIG_PAGE_FINISH))
                break;
        }
    }

    if (link && link->next) {
        wn = link->data;
        ec_assistant_find_page (ec, wn->frame, &next_page);
        d(printf(" is %s (%d)\n",wn->item->path, next_page));
    }

    return next_page;
}

static void
ec_rebuild (EConfig *emp)
{
    EConfigPrivate *p = emp->priv;
    struct _widget_node *sectionnode = NULL, *pagenode = NULL;
    GtkWidget *book = NULL, *page = NULL, *section = NULL, *root = NULL, *assistant = NULL;
    gint pageno = 0, sectionno = 0, itemno = 0;
    struct _widget_node *last_active_page = NULL;
    gboolean is_assistant;
    GList *link;

    d(printf("target changed, rebuilding:\n"));

    /* TODO: This code is pretty complex, and will probably just
     * become more complex with time.  It could possibly be split
     * into the two base types, but there would be a lot of code
     * duplication */

    /* because rebuild destroys pages, and destroying active page causes crashes */
    is_assistant = emp->widget && GTK_IS_ASSISTANT (emp->widget);
    if (is_assistant) {
        gint page_index = gtk_assistant_get_current_page (GTK_ASSISTANT (emp->widget));
        if (page_index != -1)
            last_active_page = ec_assistant_find_page (emp, gtk_assistant_get_nth_page (GTK_ASSISTANT (emp->widget), page_index), NULL);
        gtk_assistant_set_current_page (GTK_ASSISTANT (emp->widget), 0);
    }

    for (link = p->widgets; link != NULL; link = g_list_next (link)) {
        struct _widget_node *wn = link->data;
        struct _EConfigItem *item = wn->item;
        const gchar *translated_label = NULL;
        GtkWidget *w;

        d(printf(" '%s'\n", item->path));

        if (item->label != NULL)
            translated_label = gettext (item->label);

        /* If the last section doesn't contain anything, hide it */
        if (sectionnode != NULL
            && sectionnode->frame != NULL
            && (item->type == E_CONFIG_PAGE_START
            || item->type == E_CONFIG_PAGE_FINISH
            || item->type == E_CONFIG_PAGE
            || item->type == E_CONFIG_SECTION
            || item->type == E_CONFIG_SECTION_TABLE)) {
            if ( (sectionnode->empty = itemno == 0) ) {
                gtk_widget_hide(sectionnode->frame);
                sectionno--;
            } else
                gtk_widget_show(sectionnode->frame);
            d(printf("%s section '%s' [sections=%d]\n", sectionnode->empty?"hiding":"showing", sectionnode->item->path, sectionno));
        }

        /* If the last page doesn't contain anything, hide it */
        if (pagenode != NULL
            && pagenode->frame != NULL
            && (item->type == E_CONFIG_PAGE_START
            || item->type == E_CONFIG_PAGE_FINISH
            || item->type == E_CONFIG_PAGE)) {
            if ( (pagenode->empty = sectionno == 0) ) {
                gtk_widget_hide(pagenode->frame);
                pageno--;
            } else
                gtk_widget_show(pagenode->frame);
            d(printf("%s page '%s' [section=%d]\n", pagenode->empty?"hiding":"showing", pagenode->item->path, pageno));
        }

        /* Now process the item */
        switch (item->type) {
        case E_CONFIG_BOOK:
        case E_CONFIG_ASSISTANT:
            /* Only one of BOOK or ASSISTANT may be define, it
               is used by the defining code to mark the
               type of the config window.  It is
               cross-checked with the code's defined
               type. */
            if (root != NULL) {
                g_warning("EConfig book/assistant redefined at: %s", item->path);
                break;
            }

            if (wn->widget == NULL) {
                if (item->type != emp->type) {
                    g_warning("EConfig book/assistant type mismatch");
                    break;
                }
                if (item->factory) {
                    root = item->factory(emp, item, NULL, wn->widget, wn->context->data);
                } else if (item->type == E_CONFIG_BOOK) {
                    root = gtk_notebook_new();
                    gtk_widget_show (root);
                } else if (item->type == E_CONFIG_ASSISTANT) {
                    root = gtk_assistant_new ();
                } else
                    abort();

                if (item->type == E_CONFIG_ASSISTANT) {
                    g_signal_connect_swapped (
                        root, "apply",
                        G_CALLBACK (e_config_commit), emp);
                    g_signal_connect_swapped (
                        root, "cancel",
                        G_CALLBACK (e_config_abort), emp);
                    g_signal_connect (
                        root, "cancel",
                        G_CALLBACK (gtk_widget_destroy), emp);
                    g_signal_connect (
                        root, "close",
                        G_CALLBACK (gtk_widget_destroy), NULL);
                    g_signal_connect_swapped (
                        root, "prepare",
                        G_CALLBACK (ec_assistant_check_current), emp);
                    gtk_assistant_set_forward_page_func (
                        GTK_ASSISTANT (root),
                        ec_assistant_forward, emp, NULL);
                }

                emp->widget = root;
                wn->widget = root;
            } else {
                root = wn->widget;
            }

            if (item->type == E_CONFIG_BOOK)
                book = root;
            else
                assistant = root;

            page = NULL;
            pagenode = NULL;
            section = NULL;
            sectionnode = NULL;
            pageno = 0;
            sectionno = 0;
            break;
        case E_CONFIG_PAGE_START:
        case E_CONFIG_PAGE_FINISH:
            if (root == NULL) {
                g_warning("EConfig page defined before container widget: %s", item->path);
                break;
            }
            if (emp->type != E_CONFIG_ASSISTANT) {
                g_warning("EConfig assistant start/finish pages can't be used on E_CONFIG_BOOKs");
                break;
            }

            if (item->type == E_CONFIG_PAGE_FINISH || wn->widget == NULL) {
                /* always rebuild finish page, to have it as the last page */
                if (item->factory) {
                    page = item->factory(emp, item, root, wn->frame, wn->context->data);
                } else {
                    page = gtk_vbox_new (FALSE, 0);
                    gtk_container_set_border_width (GTK_CONTAINER (page), 12);
                    if (pagenode) {
                        /* put after */
                        gint index = -1;
                        ec_assistant_find_page (emp, pagenode->frame, &index);
                        gtk_assistant_insert_page (GTK_ASSISTANT (assistant), page, index + 1);
                    } else {
                        gtk_assistant_prepend_page (GTK_ASSISTANT (assistant), page);
                    }

                    gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), page, item->type == E_CONFIG_PAGE_START ? GTK_ASSISTANT_PAGE_INTRO : GTK_ASSISTANT_PAGE_CONFIRM);
                    gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), page, translated_label);
                    gtk_widget_show_all (page);
                }

                if (wn->widget != NULL && wn->widget != page) {
                    gtk_widget_destroy (wn->widget);
                }

                wn->frame = page;
                wn->widget = page;

                if (page) {
                    const gchar *empty_xpm_img[] = {
                        "75 1 2 1",
                        "   c None",
                        ".  c #FFFFFF",
                        "                                                                           "};

                    /* left side place with a blue background on a start and finish page */
                    GdkPixbuf *spacer = gdk_pixbuf_new_from_xpm_data (empty_xpm_img);

                    gtk_assistant_set_page_side_image (GTK_ASSISTANT (assistant), page, spacer);

                    g_object_unref (spacer);
                }
            }

            pageno++;
            page = NULL;
            pagenode = wn; /* need this for previous page linking */
            section = NULL;
            sectionnode = NULL;
            sectionno = 1; /* never want to hide these */
            break;
        case E_CONFIG_PAGE: {
            /* CONFIG_PAGEs depend on the config type.
               E_CONFIG_BOOK:
                The page is a VBox, stored in the notebook.
               E_CONFIG_ASSISTANT
                The page is a VBox, stored in the GtkAssistant,
                any sections automatically added inside it. */
            sectionno = 0;
            if (root == NULL) {
                g_warning("EConfig page defined before container widget: %s", item->path);
                break;
            }

            if (item->factory) {
                page = item->factory(emp, item, root, wn->frame, wn->context->data);
                if (emp->type == E_CONFIG_ASSISTANT) {
                    wn->frame = page;
                } else {
                    wn->frame = page;
                    if (page)
                        gtk_notebook_reorder_child((GtkNotebook *)book, page, pageno);
                }
                if (page)
                    sectionno = 1;
            } else if (wn->widget == NULL) {
                if (emp->type == E_CONFIG_ASSISTANT) {
                    page = gtk_vbox_new (FALSE, 0);
                    gtk_container_set_border_width (GTK_CONTAINER (page), 12);
                    if (pagenode) {
                        /* put after */
                        gint index = -1;
                        ec_assistant_find_page (emp, pagenode->frame, &index);
                        gtk_assistant_insert_page (GTK_ASSISTANT (assistant), page, index + 1);
                    } else {
                        gtk_assistant_prepend_page (GTK_ASSISTANT (assistant), page);
                    }
                    gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), page, GTK_ASSISTANT_PAGE_CONTENT);
                    gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), page, translated_label);
                    gtk_widget_show_all (page);

                    wn->frame = page;
                } else {
                    w = gtk_label_new_with_mnemonic (translated_label);
                    gtk_widget_show(w);
                    page = gtk_vbox_new(FALSE, 12);
                    gtk_container_set_border_width((GtkContainer *)page, 12);
                    gtk_widget_show(page);
                    gtk_notebook_insert_page((GtkNotebook *)book, page, w, pageno);
                    wn->frame = page;
                }
            } else
                page = wn->widget;

            d(printf("page %d:%s widget %p\n", pageno, item->path, page));

            if (wn->widget && wn->widget != page) {
                d(printf("destroy old widget for page '%s' (%p)\n", item->path, wn->widget));
                gtk_widget_destroy (wn->widget);
            }

            pageno++;
            pagenode = wn;
            section = NULL;
            sectionnode = NULL;
            wn->widget = page;
            if (page)
                g_signal_connect(page, "destroy", G_CALLBACK(gtk_widget_destroyed), &wn->widget);
            break; }
        case E_CONFIG_SECTION:
        case E_CONFIG_SECTION_TABLE:
            /* The section factory is always called with
               the parent vbox object.  Even for assistant pages. */
            if (page == NULL) {
                /*g_warning("EConfig section '%s' has no parent page", item->path);*/
                section = NULL;
                wn->widget = NULL;
                wn->frame = NULL;
                goto nopage;
            }

            itemno = 0;
            if (item->factory) {
                section = item->factory(emp, item, page, wn->widget, wn->context->data);
                wn->frame = section;
                if (section)
                    itemno = 1;

                if (section
                    && ((item->type == E_CONFIG_SECTION && !GTK_IS_BOX(section))
                    || (item->type == E_CONFIG_SECTION_TABLE && !GTK_IS_TABLE(section))))
                    g_warning("EConfig section type is wrong");
            } else {
                GtkWidget *frame;
                GtkWidget *label = NULL;

                if (wn->frame) {
                    d(printf("Item %s, clearing generated section widget\n", wn->item->path));
                    gtk_widget_destroy(wn->frame);
                    wn->widget = NULL;
                    wn->frame = NULL;
                }

                if (translated_label != NULL) {
                    gchar *txt = g_markup_printf_escaped("<span weight=\"bold\">%s</span>", translated_label);

                    label = g_object_new(gtk_label_get_type(),
                                 "label", txt,
                                 "use_markup", TRUE,
                                 "xalign", 0.0, NULL);
                    g_free(txt);
                }

                if (item->type == E_CONFIG_SECTION)
                    section = gtk_vbox_new(FALSE, 6);
                else {
                    section = gtk_table_new(1, 1, FALSE);
                    gtk_table_set_col_spacings((GtkTable *)section, 6);
                    gtk_table_set_row_spacings((GtkTable *)section, 6);
                }

                frame = g_object_new(gtk_frame_get_type(),
                             "shadow_type", GTK_SHADOW_NONE,
                             "label_widget", label,
                             "child", g_object_new(gtk_alignment_get_type(),
                                       "left_padding", 12,
                                       "top_padding", 6,
                                       "child", section, NULL),
                             NULL);
                gtk_widget_show_all(frame);
                gtk_box_pack_start((GtkBox *)page, frame, FALSE, FALSE, 0);
                wn->frame = frame;
            }
        nopage:
            if (wn->widget && wn->widget != section) {
                d(printf("destroy old widget for section '%s'\n", item->path));
                gtk_widget_destroy(wn->widget);
            }

            d(printf("Item %s, setting section widget\n", wn->item->path));

            sectionno++;
            wn->widget = section;
            if (section)
                g_signal_connect(section, "destroy", G_CALLBACK(gtk_widget_destroyed), &wn->widget);
            sectionnode = wn;
            break;
        case E_CONFIG_ITEM:
        case E_CONFIG_ITEM_TABLE:
            /* generated sections never retain their widgets on a rebuild */
            if (sectionnode->item->factory == NULL)
                wn->widget = NULL;

            /* ITEMs are called with the section parent.
               The type depends on the section type,
               either a GtkTable, or a GtkVBox */
            w = NULL;
            if (section == NULL) {
                wn->widget = NULL;
                wn->frame = NULL;
                g_warning("EConfig item has no parent section: %s", item->path);
            } else if ((item->type == E_CONFIG_ITEM && !GTK_IS_BOX(section))
                 || (item->type == E_CONFIG_ITEM_TABLE && !GTK_IS_TABLE(section)))
                g_warning("EConfig item parent type is incorrect: %s", item->path);
            else if (item->factory)
                w = item->factory(emp, item, section, wn->widget, wn->context->data);

            d(printf("item %d:%s widget %p\n", itemno, item->path, w));

            if (wn->widget && wn->widget != w) {
                d(printf("destroy old widget for item '%s'\n", item->path));
                gtk_widget_destroy(wn->widget);
            }

            wn->widget = w;
            if (w) {
                g_signal_connect(w, "destroy", G_CALLBACK(gtk_widget_destroyed), &wn->widget);
                itemno++;
            }
            break;
        }
    }

    /* If the last section doesn't contain anything, hide it */
    if (sectionnode != NULL && sectionnode->frame != NULL) {
        if ( (sectionnode->empty = itemno == 0) ) {
            gtk_widget_hide(sectionnode->frame);
            sectionno--;
        } else
            gtk_widget_show(sectionnode->frame);
        d(printf("%s section '%s' [sections=%d]\n", sectionnode->empty?"hiding":"showing", sectionnode->item->path, sectionno));
    }

    /* If the last page doesn't contain anything, hide it */
    if (pagenode != NULL && pagenode->frame != NULL) {
        if ( (pagenode->empty = sectionno == 0) ) {
            gtk_widget_hide(pagenode->frame);
            pageno--;
        } else
            gtk_widget_show(pagenode->frame);
        d(printf("%s page '%s' [section=%d]\n", pagenode->empty?"hiding":"showing", pagenode->item->path, pageno));
    }

    if (book) {
        /* make this depend on flags?? */
        if (gtk_notebook_get_n_pages((GtkNotebook *)book) == 1) {
            gtk_notebook_set_show_tabs((GtkNotebook *)book, FALSE);
            gtk_notebook_set_show_border((GtkNotebook *)book, FALSE);
        }
    }

    if (is_assistant && last_active_page) {
        gint page_index = -1;

        ec_assistant_find_page (emp, last_active_page->frame, &page_index);
        gtk_assistant_set_current_page (GTK_ASSISTANT (emp->widget), page_index);
    }
}

/**
 * e_config_set_target:
 * @emp: An initialised EConfig.
 * @target: A target allocated from @emp.
 *
 * Sets the target object for the config window.  Generally the target
 * is set only once, and will supply its own "changed" signal which
 * can be used to drive the modal.  This is a virtual method so that
 * the implementing class can connect to the changed signal and
 * initiate a e_config_target_changed() call where appropriate.
 **/
void
e_config_set_target(EConfig *emp, EConfigTarget *target)
{
    if (emp->target != target)
        ((EConfigClass *)G_OBJECT_GET_CLASS(emp))->set_target(emp, target);
}

static void
ec_widget_destroy(GtkWidget *w, EConfig *ec)
{
    if (ec->target) {
        e_config_target_free(ec, ec->target);
        ec->target = NULL;
    }

    g_object_unref(ec);
}

/**
 * e_config_create_widget:
 * @emp: An initialised EConfig object.
 *
 * Create the widget described by @emp.  Only the core widget
 * appropriate for the given type is created, i.e. a GtkNotebook for
 * the E_CONFIG_BOOK type and a GtkAssistant for the E_CONFIG_ASSISTANT
 * type.
 *
 * This object will be self-driving, but will not close itself once
 * complete.
 *
 * Unless reffed otherwise, the management object @emp will be
 * finalized when the widget is.
 *
 * Return value: The widget, also available in @emp.widget
 **/
GtkWidget *
e_config_create_widget(EConfig *emp)
{
    EConfigPrivate *p = emp->priv;
    GPtrArray *items = g_ptr_array_new();
    GList *link;
    GSList *l;
    /*char *domain = NULL;*/
    gint i;

    g_return_val_if_fail (emp->target != NULL, NULL);

    ec_add_static_items(emp);

    /* FIXME: need to override old ones with new names */
    link = p->menus;
    while (link != NULL) {
        struct _menu_node *mnode = link->data;

        for (l=mnode->menu; l; l = l->next) {
            struct _EConfigItem *item = l->data;
            struct _widget_node *wn = g_malloc0(sizeof(*wn));

            wn->item = item;
            wn->context = mnode;
            wn->config = emp;
            g_ptr_array_add(items, wn);
        }

        link = g_list_next (link);
    }

    qsort(items->pdata, items->len, sizeof(items->pdata[0]), ep_cmp);

    for (i=0;i<items->len;i++)
        p->widgets = g_list_append (p->widgets, items->pdata[i]);

    g_ptr_array_free(items, TRUE);
    ec_rebuild(emp);

    /* auto-unref it */
    g_signal_connect(emp->widget, "destroy", G_CALLBACK(ec_widget_destroy), emp);

    /* FIXME: for some reason ec_rebuild puts the widget on page 1, this is just to override that */
    if (emp->type == E_CONFIG_BOOK)
        gtk_notebook_set_current_page((GtkNotebook *)emp->widget, 0);
    else {
        gtk_window_set_position (GTK_WINDOW (emp->widget), GTK_WIN_POS_CENTER);
        gtk_widget_show (emp->widget);
    }

    return emp->widget;
}

static void
ec_dialog_response(GtkWidget *d, gint id, EConfig *ec)
{
    if (id == GTK_RESPONSE_OK)
        e_config_commit(ec);
    else
        e_config_abort(ec);

    gtk_widget_destroy(d);
}

/**
 * e_config_create_window:
 * @emp: Initialised and configured EMConfig derived instance.
 * @parent: Parent window or NULL.
 * @title: Title of window or dialog.
 *
 * Create a managed GtkWindow object from @emp.  This window will be
 * fully driven by the EConfig @emp.  If @emp.type is
 * @E_CONFIG_ASSISTANT, then this will be a toplevel GtkWindow containing
 * a GtkAssistant.  If it is @E_CONFIG_BOOK then it will be a GtkDialog
 * containing a Notebook.
 *
 * Unless reffed otherwise, the management object @emp will be
 * finalized when the widget is.
 *
 * Return value: The window widget.  This is also stored in @emp.window.
 **/
GtkWidget *
e_config_create_window(EConfig *emp, GtkWindow *parent, const gchar *title)
{
    GtkWidget *w;

    e_config_create_widget(emp);

    if (emp->type == E_CONFIG_BOOK) {
        w = gtk_dialog_new_with_buttons(title, parent,
                        GTK_DIALOG_DESTROY_WITH_PARENT |
                        GTK_DIALOG_NO_SEPARATOR,
                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                        GTK_STOCK_OK, GTK_RESPONSE_OK,
                        NULL);
        g_signal_connect(w, "response", G_CALLBACK(ec_dialog_response), emp);

        gtk_widget_ensure_style (w);
        gtk_container_set_border_width (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (w))), 0);
        gtk_container_set_border_width (GTK_CONTAINER (gtk_dialog_get_action_area (GTK_DIALOG (w))), 12);

        gtk_box_pack_start((GtkBox *)gtk_dialog_get_content_area (((GtkDialog *)w)), emp->widget, TRUE, TRUE, 0);
    } else {
        /* response is handled directly by the assistant stuff */
        w = emp->widget;
        gtk_window_set_title ((GtkWindow *)w, title);
    }

    emp->window = w;
    gtk_widget_show(w);

    return w;
}

static void
ec_call_page_check (EConfig *emp)
{
    if (emp->type == E_CONFIG_ASSISTANT) {
        ec_assistant_check_current (emp);
    } else {
        if (emp->window) {
            if (e_config_page_check(emp, NULL)) {
                gtk_dialog_set_response_sensitive((GtkDialog *)emp->window, GTK_RESPONSE_OK, TRUE);
            } else {
                gtk_dialog_set_response_sensitive((GtkDialog *)emp->window, GTK_RESPONSE_OK, FALSE);
            }
        }
    }
}

static gboolean
ec_idle_handler_for_rebuild (gpointer data)
{
    EConfig *emp = (EConfig*) data;

    ec_rebuild (emp);
    ec_call_page_check (emp);

    return FALSE;
}

/**
 * e_config_target_changed:
 * @emp:
 * @how:
 *
 * Indicate that the target has changed.  This may be called by the
 * self-aware target itself, or by the driving code.  If @how is
 * %E_CONFIG_TARGET_CHANGED_REBUILD, then the entire configuration
 * widget may be recreated based on the changed target.
 *
 * This is used to sensitise Assistant next/back buttons and the Apply
 * button for the Notebook mode.
 **/
void e_config_target_changed(EConfig *emp, e_config_target_change_t how)
{
    if (how == E_CONFIG_TARGET_CHANGED_REBUILD) {
        g_idle_add (ec_idle_handler_for_rebuild, emp);
    } else {
        ec_call_page_check (emp);
    }

    /* virtual method/signal? */
}

/**
 * e_config_abort:
 * @ec:
 *
 * Signify that the stateful configuration changes must be discarded
 * to all listeners.  This is used by self-driven assistant or notebook, or
 * may be used by code using the widget directly.
 **/
void
e_config_abort (EConfig *config)
{
    GList *link;

    g_return_if_fail (E_IS_CONFIG (config));

    /* TODO: should these just be signals? */

    link = config->priv->menus;

    while (link != NULL) {
        struct _menu_node *node = link->data;

        if (node->abort != NULL)
            node->abort (config, node->menu, node->data);

        link = g_list_next (link);
    }
}

/**
 * e_config_commit:
 * @ec:
 *
 * Signify that the stateful configuration changes should be saved.
 * This is used by the self-driven assistant or notebook, or may be used
 * by code driving the widget directly.
 **/
void
e_config_commit (EConfig *config)
{
    GList *link;

    g_return_if_fail (E_IS_CONFIG (config));

    /* TODO: should these just be signals? */

    link = config->priv->menus;

    while (link != NULL) {
        struct _menu_node *node = link->data;

        if (node->commit != NULL)
            node->commit (config, node->menu, node->data);

        link = g_list_next (link);
    }
}

/**
 * e_config_page_check:
 * @config: an #EConfig
 * @pageid: the path of the page item
 *
 * Check that a given page is complete.  If @pageid is NULL, then check
 * the whole config.  No check is made that the page actually exists.
 *
 * Return value: FALSE if the data is inconsistent/incomplete.
 **/
gboolean
e_config_page_check (EConfig *config, const gchar *pageid)
{
    GList *link;

    link = config->priv->checks;

    while (link != NULL) {
        struct _check_node *node = link->data;

        if ((pageid == NULL
             || node->pageid == NULL
             || strcmp (node->pageid, pageid) == 0)
            && !node->check (config, pageid, node->data)) {
            return FALSE;
        }

        link = g_list_next (link);
    }

    return TRUE;
}

/**
 * e_config_page_get:
 * @ec:
 * @pageid: The path of the page item.
 *
 * Retrieve the page widget corresponding to @pageid.
 *
 * Return value: The page widget.  It will be the root GtkNotebook
 * container or the GtkVBox object inside the assistant.
 **/
GtkWidget *
e_config_page_get(EConfig *ec, const gchar *pageid)
{
    GList *link;

    link = ec->priv->widgets;

    while (link != NULL) {
        struct _widget_node *wn = link->data;

        if (!wn->empty
            && (wn->item->type == E_CONFIG_PAGE
            || wn->item->type == E_CONFIG_PAGE_START
            || wn->item->type == E_CONFIG_PAGE_FINISH)
            && !strcmp(wn->item->path, pageid))
            return wn->frame;

        link = g_list_next (link);
    }

    return NULL;
}

/**
 * e_config_page_next:
 * @ec:
 * @pageid: The path of the page item.
 *
 * Find the path of the next visible page after @pageid.  If @pageid
 * is NULL then find the first visible page.
 *
 * Return value: The path of the next page, or @NULL if @pageid was the
 * last configured and visible page.
 **/
const gchar *
e_config_page_next(EConfig *ec, const gchar *pageid)
{
    GList *link;
    gint found;

    link = g_list_first (ec->priv->widgets);
    found = pageid == NULL ? 1:0;

    while (link != NULL) {
        struct _widget_node *wn = link->data;

        if (!wn->empty
            && (wn->item->type == E_CONFIG_PAGE
            || wn->item->type == E_CONFIG_PAGE_START
            || wn->item->type == E_CONFIG_PAGE_FINISH)) {
            if (found)
                return wn->item->path;
            else if (strcmp(wn->item->path, pageid) == 0)
                found = 1;
        }

        link = g_list_next (link);
    }

    return NULL;
}

/**
 * e_config_page_next:
 * @ec:
 * @pageid: The path of the page item.
 *
 * Find the path of the previous visible page before @pageid.  If @pageid
 * is NULL then find the last visible page.
 *
 * Return value: The path of the previous page, or @NULL if @pageid was the
 * first configured and visible page.
 **/
const gchar *
e_config_page_prev(EConfig *ec, const gchar *pageid)
{
    GList *link;
    gint found;

    link = g_list_last (ec->priv->widgets);
    found = pageid == NULL ? 1:0;

    while (link != NULL) {
        struct _widget_node *wn = link->data;

        if (!wn->empty
            && (wn->item->type == E_CONFIG_PAGE
            || wn->item->type == E_CONFIG_PAGE_START
            || wn->item->type == E_CONFIG_PAGE_FINISH)) {
            if (found)
                return wn->item->path;
            else if (strcmp(wn->item->path, pageid) == 0)
                found = 1;
        }

        link = g_list_previous (link);
    }

    return NULL;
}

/* ********************************************************************** */

/**
 * e_config_class_add_factory:
 * @class: Implementing class pointer.
 * @id: The name of the configuration window you're interested in.
 * This may be NULL to be called for all windows.
 * @func: An EConfigFactoryFunc to call when the window @id is being
 * created.
 * @data: Callback data.
 *
 * Add a config factory which will be called to add_items() any
 * extra items's if wants to, to the current Config window.
 *
 * TODO: Make the id a pattern?
 *
 * Return value: A handle to the factory.
 **/
EConfigFactory *
e_config_class_add_factory (EConfigClass *class,
                            const gchar *id,
                            EConfigFactoryFunc func,
                            gpointer user_data)
{
    EConfigFactory *factory;

    g_return_val_if_fail (E_IS_CONFIG_CLASS (class), NULL);
    g_return_val_if_fail (func != NULL, NULL);

    factory = g_slice_new0 (EConfigFactory);
    factory->id = g_strdup (id);
    factory->func = func;
    factory->user_data = user_data;

    class->factories = g_list_append (class->factories, factory);

    return factory;
}

/**
 * e_config_class_remove_factory:
 * @factory: an #EConfigFactory
 *
 * Removes a config factory.
 **/
void
e_config_class_remove_factory (EConfigClass *class,
                               EConfigFactory *factory)
{
    g_return_if_fail (E_IS_CONFIG_CLASS (class));
    g_return_if_fail (factory != NULL);

    class->factories = g_list_remove (class->factories, factory);

    g_free (factory->id);

    g_slice_free (EConfigFactory, factory);
}

/**
 * e_config_target_new:
 * @ep: Parent EConfig object.
 * @type: type, up to implementor
 * @size: Size of object to allocate.
 *
 * Allocate a new config target suitable for this class.  Implementing
 * classes will define the actual content of the target.
 **/
gpointer e_config_target_new(EConfig *ep, gint type, gsize size)
{
    EConfigTarget *t;

    if (size < sizeof(EConfigTarget)) {
        g_warning ("Size is less than size of EConfigTarget\n");
        size = sizeof (EConfigTarget);
    }

    t = g_malloc0(size);
    t->config = ep;
    g_object_ref(ep);
    t->type = type;

    return t;
}

/**
 * e_config_target_free:
 * @ep: Parent EConfig object.
 * @o: The target to fre.
 *
 * Free a target.  The implementing class can override this method to
 * free custom targets.
 **/
void
e_config_target_free(EConfig *ep, gpointer o)
{
    EConfigTarget *t = o;

    ((EConfigClass *)G_OBJECT_GET_CLASS(ep))->target_free(ep, t);
}

/* ********************************************************************** */

/* Config menu plugin handler */

/*
<e-plugin
  class="org.gnome.mail.plugin.config:1.0"
  id="org.gnome.mail.plugin.config.item:1.0"
  type="shlib"
  location="/opt/gnome2/lib/camel/1.0/libcamelimap.so"
  name="imap"
  description="IMAP4 and IMAP4v1 mail store">
  <hook class="org.gnome.mail.configMenu:1.0"
        handler="HandleConfig">
  <menu id="any" target="select">
   <item
    type="item|toggle|radio|image|submenu|bar"
    active
    path="foo/bar"
    label="label"
    icon="foo"
    activate="ep_view_emacs"/>
  </menu>
  </extension>

*/

static gpointer emph_parent_class;
#define emph ((EConfigHook *)eph)

static const EPluginHookTargetKey ech_item_types[] = {
    { "book", E_CONFIG_BOOK },
    { "assistant", E_CONFIG_ASSISTANT },

    { "page", E_CONFIG_PAGE },
    { "page_start", E_CONFIG_PAGE_START },
    { "page_finish", E_CONFIG_PAGE_FINISH },
    { "section", E_CONFIG_SECTION },
    { "section_table", E_CONFIG_SECTION_TABLE },
    { "item", E_CONFIG_ITEM },
    { "item_table", E_CONFIG_ITEM_TABLE },
    { NULL },
};

static void
ech_commit(EConfig *ec, GSList *items, gpointer data)
{
    struct _EConfigHookGroup *group = data;

    if (group->commit && group->hook->hook.plugin->enabled)
        e_plugin_invoke(group->hook->hook.plugin, group->commit, ec->target);
}

static void
ech_abort(EConfig *ec, GSList *items, gpointer data)
{
    struct _EConfigHookGroup *group = data;

    if (group->abort && group->hook->hook.plugin->enabled)
        e_plugin_invoke(group->hook->hook.plugin, group->abort, ec->target);
}

static gboolean
ech_check(EConfig *ec, const gchar *pageid, gpointer data)
{
    struct _EConfigHookGroup *group = data;
    EConfigHookPageCheckData hdata;

    if (!group->hook->hook.plugin->enabled)
        return TRUE;

    hdata.config = ec;
    hdata.target = ec->target;
    hdata.pageid = pageid?pageid:"";

    return GPOINTER_TO_INT(e_plugin_invoke(group->hook->hook.plugin, group->check, &hdata));
}

static void
ech_config_factory(EConfig *emp, gpointer data)
{
    struct _EConfigHookGroup *group = data;

    d(printf("config factory called %s\n", group->id?group->id:"all menus"));

    if (emp->target->type != group->target_type
        || !group->hook->hook.plugin->enabled)
        return;

    if (group->items)
        e_config_add_items(emp, group->items, ech_commit, ech_abort, NULL, group);

    if (group->check)
        e_config_add_page_check(emp, NULL, ech_check, group);
}

static void
emph_free_item(struct _EConfigItem *item)
{
    g_free(item->path);
    g_free(item->label);
    g_free(item->user_data);
    g_free(item);
}

static void
emph_free_group(struct _EConfigHookGroup *group)
{
    g_slist_foreach(group->items, (GFunc)emph_free_item, NULL);
    g_slist_free(group->items);

    g_free(group->id);
    g_free(group);
}

static GtkWidget *
ech_config_widget_factory(EConfig *ec, EConfigItem *item, GtkWidget *parent, GtkWidget *old, gpointer data)
{
    struct _EConfigHookGroup *group = data;

    if (group->hook->hook.plugin->enabled) {
        EConfigHookItemFactoryData hdata;

        hdata.config = ec;
        hdata.item = item;
        hdata.target = ec->target;
        hdata.parent = parent;
        hdata.old = old;

        return (GtkWidget *)e_plugin_invoke(group->hook->hook.plugin, (gchar *)item->user_data, &hdata);
    } else
        return NULL;
}

static struct _EConfigItem *
emph_construct_item(EPluginHook *eph, EConfigHookGroup *menu, xmlNodePtr root, EConfigHookTargetMap *map)
{
    struct _EConfigItem *item;

    d(printf("  loading config item\n"));
    item = g_malloc0(sizeof(*item));
    if ((item->type = e_plugin_hook_id(root, ech_item_types, "type")) == -1)
        goto error;
    item->path = e_plugin_xml_prop(root, "path");
    item->label = e_plugin_xml_prop_domain(root, "label", eph->plugin->domain);
    item->user_data = e_plugin_xml_prop(root, "factory");

    if (item->path == NULL
        || (item->label == NULL && item->user_data == NULL))
        goto error;

    if (item->user_data)
        item->factory = ech_config_widget_factory;

    d(printf("   path=%s label=%s factory=%s\n", item->path, item->label, (gchar *)item->user_data));

    return item;
error:
    d(printf("error!\n"));
    emph_free_item(item);
    return NULL;
}

static struct _EConfigHookGroup *
emph_construct_menu(EPluginHook *eph, xmlNodePtr root)
{
    struct _EConfigHookGroup *menu;
    xmlNodePtr node;
    EConfigHookTargetMap *map;
    EConfigHookClass *class = (EConfigHookClass *)G_OBJECT_GET_CLASS(eph);
    gchar *tmp;

    d(printf(" loading menu\n"));
    menu = g_malloc0(sizeof(*menu));

    tmp = (gchar *)xmlGetProp(root, (const guchar *)"target");
    if (tmp == NULL)
        goto error;
    map = g_hash_table_lookup(class->target_map, tmp);
    xmlFree(tmp);
    if (map == NULL)
        goto error;

    menu->target_type = map->id;
    menu->id = e_plugin_xml_prop(root, "id");
    if (menu->id == NULL) {
        g_warning("Plugin '%s' missing 'id' field in group for '%s'\n", eph->plugin->name,
              ((EPluginHookClass *)G_OBJECT_GET_CLASS(eph))->id);
        goto error;
    }
    menu->check = e_plugin_xml_prop(root, "check");
    menu->commit = e_plugin_xml_prop(root, "commit");
    menu->abort = e_plugin_xml_prop(root, "abort");
    menu->hook = (EConfigHook *)eph;
    node = root->children;
    while (node) {
        if (0 == strcmp((gchar *)node->name, "item")) {
            struct _EConfigItem *item;

            item = emph_construct_item(eph, menu, node, map);
            if (item)
                menu->items = g_slist_append(menu->items, item);
        }
        node = node->next;
    }

    return menu;
error:
    emph_free_group(menu);
    return NULL;
}

static gint
emph_construct(EPluginHook *eph, EPlugin *ep, xmlNodePtr root)
{
    xmlNodePtr node;
    EConfigClass *class;

    d(printf("loading config hook\n"));

    if (((EPluginHookClass *)emph_parent_class)->construct(eph, ep, root) == -1)
        return -1;

    class = ((EConfigHookClass *)G_OBJECT_GET_CLASS(eph))->config_class;

    node = root->children;
    while (node) {
        if (strcmp((gchar *)node->name, "group") == 0) {
            struct _EConfigHookGroup *group;

            group = emph_construct_menu(eph, node);
            if (group) {
                e_config_class_add_factory(class, group->id, ech_config_factory, group);
                emph->groups = g_slist_append(emph->groups, group);
            }
        }
        node = node->next;
    }

    eph->plugin = ep;

    return 0;
}

static void
emph_finalize(GObject *o)
{
    EPluginHook *eph = (EPluginHook *)o;

    g_slist_foreach(emph->groups, (GFunc)emph_free_group, NULL);
    g_slist_free(emph->groups);

    ((GObjectClass *)emph_parent_class)->finalize(o);
}

static void
emph_class_init(EPluginHookClass *class)
{
    ((GObjectClass *)class)->finalize = emph_finalize;
    class->construct = emph_construct;

    /* this is actually an abstract implementation but list it anyway */
    class->id = "org.gnome.evolution.config:1.0";

    d(printf("EConfigHook: init class %p '%s'\n", class, g_type_name(((GObjectClass *)class)->g_type_class.g_type)));

    ((EConfigHookClass *)class)->target_map = g_hash_table_new(g_str_hash, g_str_equal);
    ((EConfigHookClass *)class)->config_class = g_type_class_ref(e_config_get_type());
}

/**
 * e_config_hook_get_type:
 *
 * Standard GObject function to get the object type.
 *
 * Return value: The EConfigHook class type.
 **/
GType
e_config_hook_get_type(void)
{
    static GType type = 0;

    if (!type) {
        static const GTypeInfo info = {
            sizeof(EConfigHookClass), NULL, NULL, (GClassInitFunc) emph_class_init, NULL, NULL,
            sizeof(EConfigHook), 0, (GInstanceInitFunc) NULL,
        };

        emph_parent_class = g_type_class_ref(e_plugin_hook_get_type());
        type = g_type_register_static(e_plugin_hook_get_type(), "EConfigHook", &info, 0);
    }

    return type;
}

/**
 * e_config_hook_class_add_target_map:
 *
 * @class: The dervied EconfigHook class.
 * @map: A map used to describe a single EConfigTarget type for this
 * class.
 *
 * Add a targe tmap to a concrete derived class of EConfig.  The
 * target map enumates the target types available for the implenting
 * class.
 **/
void
e_config_hook_class_add_target_map (EConfigHookClass *class,
                                    const EConfigHookTargetMap *map)
{
    g_hash_table_insert(class->target_map, (gpointer)map->type, (gpointer)map);
}