aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-config.c
blob: 080b8e46e0c7731f5f7aa1bd452c333d82c7d25a (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2001 Ximian, Inc. (www.ximian.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */

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

#include <pwd.h>
#include <ctype.h>

#include <glib.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-config.h>
#include <libgnomeui/gnome-dialog.h>
#include <libgnomeui/gnome-stock.h>
#include <gtkhtml/gtkhtml.h>
#include <glade/glade.h>

#include <bonobo/bonobo-object.h>
#include <bonobo/bonobo-generic-factory.h>
#include <bonobo/bonobo-context.h>
#include <bonobo/bonobo-moniker-util.h>
#include <bonobo/bonobo-exception.h>
#include <bonobo-conf/bonobo-config-database.h>

#include <gal/util/e-util.h>
#include <gal/unicode/gunicode.h>
#include <e-util/e-html-utils.h>
#include <e-util/e-url.h>
#include "mail.h"
#include "mail-config.h"
#include "mail-ops.h"
#include "mail-mt.h"

#include "Mail.h"

typedef struct {
    Bonobo_ConfigDatabase db;

    gboolean show_preview;
    gboolean thread_list;
    gboolean hide_deleted;
    gint paned_size;
    gboolean send_html;
    gboolean citation_highlight;
    guint32  citation_color;
    gboolean prompt_empty_subject;
    gboolean prompt_only_bcc;
    gboolean do_seen_timeout;
    gint seen_timeout;
    gboolean empty_trash_on_exit;
    
    GSList *accounts;
    gint default_account;

    GSList *news;
    
    char *pgp_path;
    CamelPgpType pgp_type;
    gboolean remember_pgp_passphrase;

    MailConfigHTTPMode http_mode;
    MailConfigForwardStyle default_forward_style;
    MailConfigDisplayStyle message_display_style;
    char *default_charset;
    
    GHashTable *threaded_hash;
    GHashTable *preview_hash;
} MailConfig;

static MailConfig *config = NULL;

#define MAIL_CONFIG_IID "OAFIID:GNOME_Evolution_MailConfig_Factory"

/* Prototypes */
static void config_read (void);
static void mail_config_set_default_account_num (gint new_default);


/* Identity */
MailConfigIdentity *
identity_copy (const MailConfigIdentity *id)
{
    MailConfigIdentity *new;
    
    g_return_val_if_fail (id != NULL, NULL);
    
    new = g_new0 (MailConfigIdentity, 1);
    new->name = g_strdup (id->name);
    new->address = g_strdup (id->address);
    new->organization = g_strdup (id->organization);
    new->signature = g_strdup (id->signature);
    new->html_signature = g_strdup (id->html_signature);
    new->has_html_signature = id->has_html_signature;

    return new;
}

void
identity_destroy (MailConfigIdentity *id)
{
    if (!id)
        return;
    
    g_free (id->name);
    g_free (id->address);
    g_free (id->organization);
    g_free (id->signature);
    g_free (id->html_signature);
    
    g_free (id);
}

/* Service */
MailConfigService *
service_copy (const MailConfigService *source) 
{
    MailConfigService *new;
    
    g_return_val_if_fail (source != NULL, NULL);
    
    new = g_new0 (MailConfigService, 1);
    new->url = g_strdup (source->url);
    new->keep_on_server = source->keep_on_server;
    new->auto_check = source->auto_check;
    new->auto_check_time = source->auto_check_time;
    new->enabled = source->enabled;
    new->save_passwd = source->save_passwd;
    
    return new;
}

void
service_destroy (MailConfigService *source)
{
    if (!source)
        return;
    
    g_free (source->url);
    
    g_free (source);
}

void
service_destroy_each (gpointer item, gpointer data)
{
    service_destroy ((MailConfigService *)item);
}

/* Account */
MailConfigAccount *
account_copy (const MailConfigAccount *account) 
{
    MailConfigAccount *new;
    
    g_return_val_if_fail (account != NULL, NULL);
    
    new = g_new0 (MailConfigAccount, 1);
    new->name = g_strdup (account->name);
    
    new->id = identity_copy (account->id);
    new->source = service_copy (account->source);
    new->transport = service_copy (account->transport);
    
    new->drafts_folder_name = g_strdup (account->drafts_folder_name);
    new->drafts_folder_uri = g_strdup (account->drafts_folder_uri);
    new->sent_folder_name = g_strdup (account->sent_folder_name);
    new->sent_folder_uri = g_strdup (account->sent_folder_uri);
    
    new->pgp_key = g_strdup (account->pgp_key);
    new->pgp_encrypt_to_self = account->pgp_encrypt_to_self;
    
    new->smime_key = g_strdup (account->smime_key);
    new->smime_encrypt_to_self = account->smime_encrypt_to_self;
    
    return new;
}

void
account_destroy (MailConfigAccount *account)
{
    if (!account)
        return;
    
    g_free (account->name);
    
    identity_destroy (account->id);
    service_destroy (account->source);
    service_destroy (account->transport);
    
    g_free (account->drafts_folder_name);
    g_free (account->drafts_folder_uri);
    g_free (account->sent_folder_name);
    g_free (account->sent_folder_uri);
    
    g_free (account->pgp_key);
    g_free (account->smime_key);
    
    g_free (account);
}

void
account_destroy_each (gpointer item, gpointer data)
{
    account_destroy ((MailConfigAccount *)item);
}

/* Config struct routines */
void
mail_config_init (void)
{
    Bonobo_ConfigDatabase db;
    CORBA_Environment ev;

    if (config)
        return;
    
    CORBA_exception_init (&ev);

    db = bonobo_get_object ("wombat:", "Bonobo/ConfigDatabase", &ev);
    
    if (BONOBO_EX (&ev) || db == CORBA_OBJECT_NIL) {
           
        CORBA_exception_free (&ev);
        return;
    }

    CORBA_exception_free (&ev);

    config = g_new0 (MailConfig, 1);

    config->db = db;
    
    config_read ();
}

void
mail_config_clear (void)
{
    if (!config)
        return;
    
    if (config->accounts) {
        g_slist_foreach (config->accounts, account_destroy_each, NULL);
        g_slist_free (config->accounts);
        config->accounts = NULL;
    }
    
    if (config->news) {
        g_slist_foreach (config->news, service_destroy_each, NULL);
        g_slist_free (config->news);
        config->news = NULL;
    }
}

static void
config_read (void)
{
    gint len, i, default_num;

    mail_config_clear ();
        
    len = bonobo_config_get_long_with_default (config->db, 
            "/Mail/Accounts/num", 0, NULL);

    for (i = 0; i < len; i++) {
        MailConfigAccount *account;
        MailConfigIdentity *id;
        MailConfigService *source;
        MailConfigService *transport;
        gchar *path, *val;
        
        account = g_new0 (MailConfigAccount, 1);
        path = g_strdup_printf ("/Mail/Accounts/account_name_%d", i);
        account->name = bonobo_config_get_string (config->db, path, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/account_drafts_folder_name_%d", i);
        val = bonobo_config_get_string (config->db, path, NULL);
        g_free (path);
        if (val && *val)
            account->drafts_folder_name = val;
        else
            g_free (val);
        
        path = g_strdup_printf ("/Mail/Accounts/account_drafts_folder_uri_%d", i);
        val = bonobo_config_get_string (config->db, path, NULL);
        g_free (path);
        if (val && *val)
            account->drafts_folder_uri = val;
        else
            g_free (val);
        
        path = g_strdup_printf ("/Mail/Accounts/account_sent_folder_name_%d", i);
        val = bonobo_config_get_string (config->db, path, NULL);
        g_free (path);
        if (val && *val)
            account->sent_folder_name = val;
        else
            g_free (val);
        
        path = g_strdup_printf ("/Mail/Accounts/account_sent_folder_uri_%d", i);
        val = bonobo_config_get_string (config->db, path, NULL);
        g_free (path);
        if (val && *val)
            account->sent_folder_uri = val;
        else
            g_free (val);
        
        /* get the pgp info */
        path = g_strdup_printf ("/Mail/Accounts/account_pgp_key_%d", i);
        val = bonobo_config_get_string (config->db, path, NULL);
        g_free (path);
        if (val && *val)
            account->pgp_key = val;
        else
            g_free (val);
        
        path = g_strdup_printf ("/Mail/Accounts/account_pgp_encrypt_to_self_%d", i);
        account->pgp_encrypt_to_self = bonobo_config_get_boolean_with_default (
                config->db, path, TRUE, NULL);
        g_free (path);
        
        /* get the s/mime info */
        path = g_strdup_printf ("/Mail/Accounts/account_smime_key_%d", i);
        val = bonobo_config_get_string (config->db, path, NULL);
        g_free (path);
        if (val && *val)
            account->smime_key = val;
        else
            g_free (val);
        
        path = g_strdup_printf ("/Mail/Accounts/account_smime_encrypt_to_self_%d", i);
        account->smime_encrypt_to_self = bonobo_config_get_boolean_with_default (
                config->db, path, TRUE, NULL);
        g_free (path);
        
        /* get the identity info */
        id = g_new0 (MailConfigIdentity, 1);        
        path = g_strdup_printf ("/Mail/Accounts/identity_name_%d", i);
        id->name = bonobo_config_get_string (config->db, path, NULL);
        g_free (path);

        path = g_strdup_printf ("/Mail/Accounts/identity_address_%d", i);
        id->address = bonobo_config_get_string (config->db, path, NULL);
        g_free (path);

        path = g_strdup_printf ("/Mail/Accounts/identity_organization_%d", i);
        id->organization = bonobo_config_get_string (config->db, path, NULL);
        g_free (path);

        path = g_strdup_printf ("/Mail/Accounts/identity_signature_%d", i);
        id->signature = bonobo_config_get_string (config->db, path, NULL);
        g_free (path);
        path = g_strdup_printf ("/Mail/Accounts/identity_html_signature_%d", i);
        id->html_signature = bonobo_config_get_string (config->db, path, NULL);
        g_free (path);
        path = g_strdup_printf ("/Mail/Accounts/identity_has_html_signature_%d", i);
        id->has_html_signature = bonobo_config_get_boolean_with_default (
            config->db, path, FALSE, NULL);
        g_free (path);

        /* get the source */
        source = g_new0 (MailConfigService, 1);

        path = g_strdup_printf ("/Mail/Accounts/source_url_%d", i);
        val = bonobo_config_get_string (config->db, path, NULL);
        g_free (path);
        if (val && *val)
            source->url = val;
        else
            g_free (val);
        
        path = g_strdup_printf ("/Mail/Accounts/source_keep_on_server_%d", i);
        source->keep_on_server = bonobo_config_get_boolean (config->db, path, NULL);
        g_free (path);

        path = g_strdup_printf ("/Mail/Accounts/source_auto_check_%d", i);
        source->auto_check = bonobo_config_get_boolean_with_default (
                        config->db, path, FALSE, NULL);
        g_free (path);

        path = g_strdup_printf ("/Mail/Accounts/source_auto_check_time_%d", i);
        source->auto_check_time = bonobo_config_get_long_with_default ( 
            config->db, path, -1, NULL);

        if (source->auto_check && source->auto_check_time <= 0) {
            source->auto_check_time = 5;
            source->auto_check = FALSE;
        }

        g_free (path);

        path = g_strdup_printf ("/Mail/Accounts/source_enabled_%d", i);
        source->enabled = bonobo_config_get_boolean_with_default (
            config->db, path, TRUE, NULL);
        g_free (path);

        path = g_strdup_printf 
            ("/Mail/Accounts/source_save_passwd_%d", i);
        source->save_passwd = bonobo_config_get_boolean_with_default ( 
            config->db, path, TRUE, NULL);
        g_free (path);
        
        /* get the transport */
        transport = g_new0 (MailConfigService, 1);
        path = g_strdup_printf ("/Mail/Accounts/transport_url_%d", i);
        val = bonobo_config_get_string (config->db, path, NULL);
        g_free (path);
        if (val && *val)
            transport->url = val;
        else
            g_free (val);
        
        path = g_strdup_printf ("/Mail/Accounts/transport_save_passwd_%d", i);
        transport->save_passwd = bonobo_config_get_boolean (config->db, path, NULL);
        g_free (path);
        
        account->id = id;
        account->source = source;
        account->transport = transport;
        
        config->accounts = g_slist_append (config->accounts, account);
    }


    default_num = bonobo_config_get_long_with_default (config->db,
        "/Mail/Accounts/default_account", 0, NULL);

    mail_config_set_default_account_num (default_num);

#ifdef ENABLE_NNTP
    /* News */
    
    len = bonobo_config_get_long_with_default (config->db, 
            "/News/Sources/num", 0, NULL);
    for (i = 0; i < len; i++) {
        MailConfigService *n;
        gchar *path, *r;
        
        path = g_strdup_printf ("/News/Sources/url_%d", i);

        if ((r = bonobo_config_get_string (config->db, path, NULL))) {

            n = g_new0 (MailConfigService, 1);      
            n->url = r;
            config->news = g_slist_append (config->news, n);
        } 

        g_free (path);
        
    }
#endif
    
    /* Format */
    config->send_html = bonobo_config_get_boolean_with_default (config->db,
            "/Mail/Format/send_html", FALSE, NULL);

    /* Citation */
    config->citation_highlight = bonobo_config_get_boolean_with_default (
        config->db, "/Mail/Display/citation_highlight", TRUE, NULL);
    
    config->citation_color = bonobo_config_get_long_with_default (
        config->db, "/Mail/Display/citation_color", 0x737373, NULL);
    
    /* Mark as seen toggle */
    config->do_seen_timeout = bonobo_config_get_boolean_with_default (
        config->db, "/Mail/Display/do_seen_timeout", TRUE, NULL);

    /* Mark as seen timeout */
    config->seen_timeout = bonobo_config_get_long_with_default (config->db,
                "/Mail/Display/seen_timeout", 1500, NULL);
    
    /* Show Messages Threaded */
    config->thread_list = bonobo_config_get_boolean_with_default (
                config->db, "/Mail/Display/thread_list", FALSE, NULL);
    
    config->show_preview = bonobo_config_get_boolean_with_default (
        config->db, "/Mail/Display/preview_pane", TRUE, NULL);
    
    /* Hide deleted automatically */
    config->hide_deleted = bonobo_config_get_boolean_with_default (
        config->db, "/Mail/Display/hide_deleted", FALSE, NULL);
    
    /* Size of vpaned in mail view */
    config->paned_size = bonobo_config_get_long_with_default (config->db, 
                "/Mail/Display/paned_size", 200, NULL);

    /* Empty Subject */
    config->prompt_empty_subject = bonobo_config_get_boolean_with_default (
        config->db, "/Mail/Prompts/empty_subject", TRUE, NULL);
    
    /* Only Bcc */
    config->prompt_only_bcc = bonobo_config_get_boolean_with_default (
        config->db, "/Mail/Prompts/only_bcc", TRUE, NULL);

    /* PGP/GPG */
    config->pgp_path = bonobo_config_get_string (config->db, 
                             "/Mail/PGP/path", NULL);
    
    config->pgp_type = bonobo_config_get_long_with_default (config->db, 
            "/Mail/PGP/type", CAMEL_PGP_TYPE_NONE, NULL);
    
    config->remember_pgp_passphrase = bonobo_config_get_boolean_with_default (
        config->db, "/Mail/PGP/remember_passphrase", TRUE, NULL);
    
    /* HTTP images */
    config->http_mode = bonobo_config_get_long_with_default (config->db, 
        "/Mail/Display/http_images", MAIL_CONFIG_HTTP_SOMETIMES, NULL);
    
    /* Forwarding */
    config->default_forward_style = bonobo_config_get_long_with_default (
        config->db, "/Mail/Format/default_forward_style", 
        MAIL_CONFIG_FORWARD_ATTACHED, NULL);
    
    /* Message Display */
    config->message_display_style = bonobo_config_get_long_with_default (
        config->db, "/Mail/Format/message_display_style", 
        MAIL_CONFIG_DISPLAY_NORMAL, NULL);
    
    /* Default charset */
    config->default_charset = bonobo_config_get_string (config->db, 
            "/Mail/Format/default_charset", NULL);
    
    if (!config->default_charset) {
        g_get_charset (&config->default_charset);
        if (!config->default_charset ||
            !g_strcasecmp (config->default_charset, "US-ASCII"))
            config->default_charset = g_strdup ("ISO-8859-1");
        else
            config->default_charset = g_strdup (config->default_charset);
    }
    
    /* Trash folders */
    config->empty_trash_on_exit = bonobo_config_get_boolean_with_default (
        config->db, "/Mail/Trash/empty_on_exit", FALSE, NULL);
}

#define bonobo_config_set_string_wrapper(db, path, val, ev) bonobo_config_set_string (db, path, val ? val : "", ev)

void
mail_config_write (void)
{
    CORBA_Environment ev;
    gint len, i, default_num;
    
    /* Accounts */
    
    if (!config)
        return;
    
    CORBA_exception_init (&ev);
    Bonobo_ConfigDatabase_removeDir (config->db, "/Mail/Accounts", &ev);
    CORBA_exception_init (&ev);
    Bonobo_ConfigDatabase_removeDir (config->db, "/News/Sources", &ev);
    CORBA_exception_init (&ev);
    Bonobo_ConfigDatabase_sync (config->db, &ev);
    
    len = g_slist_length (config->accounts);
    bonobo_config_set_long (config->db,
                "/Mail/Accounts/num", len, NULL);
    
    default_num = mail_config_get_default_account_num ();
    bonobo_config_set_long (config->db,
                "/Mail/Accounts/default_account", default_num, NULL);
    
    for (i = 0; i < len; i++) {
        MailConfigAccount *account;
        gchar *path;
        
        account = g_slist_nth_data (config->accounts, i);
        
        /* account info */
        path = g_strdup_printf ("/Mail/Accounts/account_name_%d", i);
        bonobo_config_set_string_wrapper (config->db, path, account->name, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/account_drafts_folder_name_%d", i);
        bonobo_config_set_string_wrapper (config->db, path, 
                          account->drafts_folder_name, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/account_drafts_folder_uri_%d", i);
        bonobo_config_set_string_wrapper (config->db, path, 
                          account->drafts_folder_uri, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/account_sent_folder_name_%d", i);
        bonobo_config_set_string_wrapper (config->db, path, 
                          account->sent_folder_name, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/account_sent_folder_uri_%d", i);
        bonobo_config_set_string_wrapper (config->db, path, 
                          account->sent_folder_uri, NULL);
        g_free (path);
        
        /* account pgp options */
        path = g_strdup_printf ("/Mail/Accounts/account_pgp_key_%d", i);
        bonobo_config_set_string_wrapper (config->db, path, account->pgp_key, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/account_pgp_encrypt_to_self_%d", i);
        bonobo_config_set_boolean (config->db, path, 
                       account->pgp_encrypt_to_self, NULL);
        g_free (path);
        
        /* account s/mime options */
        path = g_strdup_printf ("/Mail/Accounts/account_smime_key_%d", i);
        bonobo_config_set_string_wrapper (config->db, path, account->smime_key, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/account_smime_encrypt_to_self_%d", i);
        bonobo_config_set_boolean (config->db, path, account->smime_encrypt_to_self, NULL);
        g_free (path);
        
        /* identity info */
        path = g_strdup_printf ("/Mail/Accounts/identity_name_%d", i);
        bonobo_config_set_string_wrapper (config->db, path, account->id->name, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/identity_address_%d", i);
        bonobo_config_set_string_wrapper (config->db, path, account->id->address, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/identity_organization_%d", i);
        bonobo_config_set_string_wrapper (config->db, path, account->id->organization, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/identity_signature_%d", i);
        bonobo_config_set_string_wrapper (config->db, path, account->id->signature, NULL);
        g_free (path);
        
        path = g_strdup_printf ("identity_html_signature_%d", i);
        bonobo_config_set_string_wrapper (config->db, path, account->id->html_signature, NULL);
        g_free (path);
        
        path = g_strdup_printf ("identity_has_html_signature_%d", i);
        bonobo_config_set_boolean (config->db, path, account->id->has_html_signature, NULL);
        g_free (path);
        
        /* source info */
        path = g_strdup_printf ("/Mail/Accounts/source_url_%d", i);
        bonobo_config_set_string_wrapper (config->db, path, account->source->url, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/source_keep_on_server_%d", i);
        bonobo_config_set_boolean (config->db, path, account->source->keep_on_server, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/source_auto_check_%d", i);
        bonobo_config_set_boolean (config->db, path, account->source->auto_check, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/source_auto_check_time_%d", i);
        bonobo_config_set_long (config->db, path, account->source->auto_check_time, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/source_enabled_%d", i);
        bonobo_config_set_boolean (config->db, path, account->source->enabled, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/source_save_passwd_%d", i);
        bonobo_config_set_boolean (config->db, path, account->source->save_passwd, NULL);
        g_free (path);
        
        /* transport info */
        path = g_strdup_printf ("/Mail/Accounts/transport_url_%d", i);
        bonobo_config_set_string_wrapper (config->db, path, account->transport->url, NULL);
        g_free (path);
        
        path = g_strdup_printf ("/Mail/Accounts/transport_save_passwd_%d", i);
        bonobo_config_set_boolean (config->db, path, account->transport->save_passwd, NULL);
        g_free (path);
    }
    
#ifdef ENABLE_NNTP
    /* News */
    
    len = g_slist_length (config->news);
    bonobo_config_set_long (config->db, "/News/Sources/num", len, NULL);
    for (i = 0; i < len; i++) {
        MailConfigService *n;
        gchar *path;
        
        n = g_slist_nth_data (config->news, i);
        
        path = g_strdup_printf ("/News/Sources/url_%d", i);
        bonobo_config_set_string_wrapper (config->db, path, n->url, NULL);
        g_free (path);
    }
    
#endif
    
    CORBA_exception_init (&ev);
    Bonobo_ConfigDatabase_sync (config->db, &ev);
    CORBA_exception_free (&ev);
}

static gboolean
hash_save_state (gpointer key, gpointer value, gpointer user_data)
{
    char *path;
    gboolean bool = GPOINTER_TO_INT (value);
    
    path = g_strconcat ("/Mail/", (char *)user_data, "/", (char *)key, 
                NULL);
    bonobo_config_set_boolean (config->db, path, bool, NULL);
    g_free (path);
    g_free (key);
    
    return TRUE;
}

void
mail_config_write_on_exit (void)
{
    CORBA_Environment ev;
    GSList *sources;
    MailConfigService *s;
    
    /* Show Messages Threaded */
    bonobo_config_set_boolean (config->db, "/Mail/Display/thread_list", 
                   config->thread_list, NULL);
    
    /* Show Message Preview */
    bonobo_config_set_boolean (config->db, "/Mail/Display/preview_pane",
                   config->show_preview, NULL);
    
    /* Hide deleted automatically */
    bonobo_config_set_boolean (config->db, "/Mail/Display/hide_deleted", 
                   config->hide_deleted, NULL);
    
    /* Size of vpaned in mail view */
    bonobo_config_set_long (config->db, "/Mail/Display/paned_size", 
                config->paned_size, NULL);

    /* Mark as seen toggle */
    bonobo_config_set_boolean (config->db, "/Mail/Display/do_seen_timeout",
                   config->do_seen_timeout, NULL);
    /* Mark as seen timeout */
    bonobo_config_set_long (config->db, "/Mail/Display/seen_timeout", 
                config->seen_timeout, NULL);
    
    /* Format */
    bonobo_config_set_boolean (config->db, "/Mail/Format/send_html", 
                   config->send_html, NULL);
    
    /* Citation */
    bonobo_config_set_boolean (config->db, 
                   "/Mail/Display/citation_highlight", 
                   config->citation_highlight, NULL);
    
    bonobo_config_set_long (config->db, "/Mail/Display/citation_color",
                config->citation_color, NULL);
    
    /* Empty Subject */
    bonobo_config_set_boolean (config->db, "/Mail/Prompts/empty_subject",
                   config->prompt_empty_subject, NULL); 
    
    /* Only Bcc */
    bonobo_config_set_boolean (config->db, "/Mail/Prompts/only_bcc",
                   config->prompt_only_bcc, NULL);  
    
    /* PGP/GPG */
    bonobo_config_set_string_wrapper (config->db, "/Mail/PGP/path", 
                      config->pgp_path, NULL);
    
    bonobo_config_set_long (config->db, "/Mail/PGP/type", 
                config->pgp_type, NULL);
    
    bonobo_config_set_boolean (config->db, "/Mail/Prompts/remember_passphrase",
                   config->remember_pgp_passphrase, NULL);  
    
    /* HTTP images */
    bonobo_config_set_long (config->db, "/Mail/Display/http_images", 
                config->http_mode, NULL);
    
    /* Forwarding */
    bonobo_config_set_long (config->db, 
                "/Mail/Format/default_forward_style", 
                config->default_forward_style, NULL);
    
    /* Message Display */
    bonobo_config_set_long (config->db, 
                "/Mail/Format/message_display_style", 
                config->message_display_style, NULL);
    
    /* Default charset */
    bonobo_config_set_string_wrapper (config->db, "/Mail/Format/default_charset", 
                      config->default_charset, NULL);
    
    /* Trash folders */
    bonobo_config_set_boolean (config->db, "/Mail/Trash/empty_on_exit", 
                   config->empty_trash_on_exit, NULL);
    
    g_hash_table_foreach_remove (config->threaded_hash, 
                     hash_save_state, "Threads");
    
    g_hash_table_foreach_remove (config->preview_hash, 
                     hash_save_state, "Preview");
    
    CORBA_exception_init (&ev);
    Bonobo_ConfigDatabase_sync (config->db, &ev);
    CORBA_exception_free (&ev);
    
    /* Passwords */
    /* fixme: still depends on gnome-config */
    gnome_config_private_clean_section ("/Evolution/Passwords");
    sources = mail_config_get_sources ();
    for ( ; sources; sources = sources->next) {
        s = sources->data;
        if (s->save_passwd && s->url)
            mail_session_remember_password (s->url);
    }
    g_slist_free (sources);
    gnome_config_sync ();
                
    /* now do cleanup */
    mail_config_clear ();
}

/* Accessor functions */
gboolean
mail_config_is_configured (void)
{
    return config->accounts != NULL;
}

static char *
uri_to_key (const char *uri)
{
    char *rval;
    int i = 0;
    
    if (!uri)
        return NULL;
    
    rval = g_strdup (uri);
    
    while (rval [i]) {  
        if (rval [i] == '/' || rval [i] == ':')
            rval [i] = '_';
        
        i++;
    }
    
    return rval;
}

gboolean
mail_config_get_empty_trash_on_exit (void)
{
    return config->empty_trash_on_exit;
}

void
mail_config_set_empty_trash_on_exit (gboolean value)
{
    config->empty_trash_on_exit = value;
}

gboolean
mail_config_get_show_preview (const char *uri)
{
    if (uri) {
        gpointer key, val;
        char *dbkey;
        
        dbkey = uri_to_key (uri);
        
        if (!config->preview_hash)
            config->preview_hash = g_hash_table_new (g_str_hash, g_str_equal);
        
        if (!g_hash_table_lookup_extended (config->preview_hash, dbkey, &key, &val)) {
            gboolean value;
            char *str;
            
            str = g_strdup_printf ("/Mail/Preview/%s", dbkey);
            value = bonobo_config_get_boolean_with_default (config->db, str, TRUE, NULL);
            g_free (str);
            
            g_hash_table_insert (config->preview_hash, dbkey,
                         GINT_TO_POINTER (value));
            
            return value;
        } else
            return GPOINTER_TO_INT (val);
    }
    
    /* return the default value */
    
    return config->show_preview;
}

void
mail_config_set_show_preview (const char *uri, gboolean value)
{
    if (uri) {
        char *dbkey = uri_to_key (uri);
        gpointer key, val;
        
        if (!config->preview_hash)
            config->preview_hash = g_hash_table_new (g_str_hash, g_str_equal);
        
        if (g_hash_table_lookup_extended (config->preview_hash, dbkey, &key, &val)) {
            g_hash_table_insert (config->preview_hash, dbkey,
                         GINT_TO_POINTER (value));
            g_free (dbkey);
        } else {
            g_hash_table_insert (config->preview_hash, dbkey, 
                         GINT_TO_POINTER (value));
        }
    } else
        config->show_preview = value;
}

gboolean
mail_config_get_thread_list (const char *uri)
{
    if (uri) {
        gpointer key, val;
        char *dbkey;
        
        dbkey = uri_to_key (uri);
        
        if (!config->threaded_hash)
            config->threaded_hash = g_hash_table_new (g_str_hash, g_str_equal);
        
        if (!g_hash_table_lookup_extended (config->threaded_hash, dbkey, &key, &val)) {
            gboolean value;
            char *str;
            
            str = g_strdup_printf ("/Mail/Threads/%s", dbkey);
            value = bonobo_config_get_boolean_with_default (config->db, str, FALSE, NULL);
            g_free (str);
            
            g_hash_table_insert (config->threaded_hash, dbkey,
                         GINT_TO_POINTER (value));
            
            return value;
        } else
            return GPOINTER_TO_INT (val);
    }
    
    /* return the default value */
    
    return config->thread_list;
}

void
mail_config_set_thread_list (const char *uri, gboolean value)
{
    if (uri) {
        char *dbkey = uri_to_key (uri);
        gpointer key, val;
        
        if (!config->threaded_hash)
            config->threaded_hash = g_hash_table_new (g_str_hash, g_str_equal);
        
        if (g_hash_table_lookup_extended (config->threaded_hash, dbkey, &key, &val)) {
            g_hash_table_insert (config->threaded_hash, dbkey,
                         GINT_TO_POINTER (value));
            g_free (dbkey);
        } else {
            g_hash_table_insert (config->threaded_hash, dbkey, 
                         GINT_TO_POINTER (value));
        }
    } else
        config->thread_list = value;
}

gboolean
mail_config_get_hide_deleted (void)
{
    return config->hide_deleted;
}

void
mail_config_set_hide_deleted (gboolean value)
{
    config->hide_deleted = value;
}

gint
mail_config_get_paned_size (void)
{
    return config->paned_size;
}

void
mail_config_set_paned_size (gint value)
{
    config->paned_size = value;
}

gboolean
mail_config_get_send_html (void)
{
    return config->send_html;
}

void
mail_config_set_send_html (gboolean send_html)
{
    config->send_html = send_html;
}

gboolean
mail_config_get_citation_highlight (void)
{
    return config->citation_highlight;
}

void
mail_config_set_citation_highlight (gboolean citation_highlight)
{
    config->citation_highlight = citation_highlight;
}

guint32
mail_config_get_citation_color (void)
{
    return config->citation_color;
}

void
mail_config_set_citation_color (guint32 citation_color)
{
    config->citation_color = citation_color;
}

gboolean
mail_config_get_do_seen_timeout (void)
{
    return config->do_seen_timeout;
}

void
mail_config_set_do_seen_timeout (gboolean do_seen_timeout)
{
    config->do_seen_timeout = do_seen_timeout;
}

gint
mail_config_get_mark_as_seen_timeout (void)
{
    return config->seen_timeout;
}

void
mail_config_set_mark_as_seen_timeout (gint timeout)
{
    config->seen_timeout = timeout;
}

gboolean
mail_config_get_prompt_empty_subject (void)
{
    return config->prompt_empty_subject;
}

void
mail_config_set_prompt_empty_subject (gboolean value)
{
    config->prompt_empty_subject = value;
}

gboolean
mail_config_get_prompt_only_bcc (void)
{
    return config->prompt_only_bcc;
}

void
mail_config_set_prompt_only_bcc (gboolean value)
{
    config->prompt_only_bcc = value;
}


struct {
    char *bin;
    CamelPgpType type;
} binaries[] = {
    { "gpg", CAMEL_PGP_TYPE_GPG },
    { "pgpv", CAMEL_PGP_TYPE_PGP5 },
    { "pgp", CAMEL_PGP_TYPE_PGP2 },
    { NULL, CAMEL_PGP_TYPE_NONE }
};

/* FIXME: what about PGP 6.x? And I assume we want to "prefer" GnuPG
   over the other, which is done now, but after that do we have a
   order-of-preference for the rest? */
static void
auto_detect_pgp_variables (void)
{
    CamelPgpType type = CAMEL_PGP_TYPE_NONE;
    const char *PATH, *path;
    char *pgp = NULL;
    
    PATH = getenv ("PATH");
    
    path = PATH;
    while (path && *path && !type) {
        const char *pend = strchr (path, ':');
        char *dirname;
        int i;
        
        if (pend) {
            /* don't even think of using "." */
            if (!strncmp (path, ".", pend - path)) {
                path = pend + 1;
                continue;
            }
            
            dirname = g_strndup (path, pend - path);
            path = pend + 1;
        } else {
            /* don't even think of using "." */
            if (!strcmp (path, "."))
                break;
            
            dirname = g_strdup (path);
            path = NULL;
        }
        
        for (i = 0; binaries[i].bin; i++) {
            struct stat st;
            
            pgp = g_strdup_printf ("%s/%s", dirname, binaries[i].bin);
            /* make sure the file exists *and* is executable? */
            if (stat (pgp, &st) != -1 && st.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) {
                type = binaries[i].type;
                break;
            }
            
            g_free (pgp);
            pgp = NULL;
        }
        
        g_free (dirname);
    }
    
    if (pgp && type) {
        mail_config_set_pgp_path (pgp);
        mail_config_set_pgp_type (type);
    }
    
    g_free (pgp);
}

CamelPgpType
mail_config_get_pgp_type (void)
{
    if (!config->pgp_path || !config->pgp_type)
        auto_detect_pgp_variables ();
    
    return config->pgp_type;
}

void
mail_config_set_pgp_type (CamelPgpType pgp_type)
{
    config->pgp_type = pgp_type;
}

const char *
mail_config_get_pgp_path (void)
{
    if (!config->pgp_path || !config->pgp_type)
        auto_detect_pgp_variables ();
    
    return config->pgp_path;
}

void
mail_config_set_pgp_path (const char *pgp_path)
{
    g_free (config->pgp_path);
    
    config->pgp_path = g_strdup (pgp_path);
}

gboolean
mail_config_get_remember_pgp_passphrase (void)
{
    return config->remember_pgp_passphrase;
}

void
mail_config_set_remember_pgp_passphrase (gboolean value)
{
    config->remember_pgp_passphrase = value;
}

MailConfigHTTPMode
mail_config_get_http_mode (void)
{
    return config->http_mode;
}

void
mail_config_set_http_mode (MailConfigHTTPMode mode)
{
    config->http_mode = mode;
}

MailConfigForwardStyle
mail_config_get_default_forward_style (void)
{
    return config->default_forward_style;
}

void
mail_config_set_default_forward_style (MailConfigForwardStyle style)
{
    config->default_forward_style = style;
}

MailConfigDisplayStyle
mail_config_get_message_display_style (void)
{
    return config->message_display_style;
}

void
mail_config_set_message_display_style (MailConfigDisplayStyle style)
{
    config->message_display_style = style;
}

const char *
mail_config_get_default_charset (void)
{
    return config->default_charset;
}

void
mail_config_set_default_charset (const char *charset)
{
    g_free (config->default_charset);
    config->default_charset = g_strdup (charset);
}


const MailConfigAccount *
mail_config_get_default_account (void)
{
    MailConfigAccount *retval;
    
    if (!config->accounts)
        return NULL;
    
    retval = g_slist_nth_data (config->accounts,
                   config->default_account);
    
    /* Looks like we have no default, so make the first account
           the default */
    if (retval == NULL) {
        mail_config_set_default_account_num (0);
        retval = config->accounts->data;
    }
    
    return retval;
}

const MailConfigAccount *
mail_config_get_account_by_name (const char *account_name)
{
    /* FIXME: this should really use a hash */
    const MailConfigAccount *account;
    GSList *l;
    
    l = config->accounts;
    while (l) {
        account = l->data;
        if (account && !strcmp (account->name, account_name))
            return account;
        
        l = l->next;
    }
    
    return NULL;
}

const MailConfigAccount *
mail_config_get_account_by_source_url (const char *source_url)
{
    const MailConfigAccount *account;
    GSList *l;

    g_return_val_if_fail (source_url != NULL, NULL);

    l = config->accounts;
    while (l) {
        account = l->data;
        if (account
            && account->source 
            && account->source->url
            && e_url_equal (account->source->url, source_url))
            return account;
        
        l = l->next;
    }
    
    return NULL;
}

const GSList *
mail_config_get_accounts (void)
{
    return config->accounts;
}

void
mail_config_add_account (MailConfigAccount *account)
{
    config->accounts = g_slist_append (config->accounts, account);
}

const GSList *
mail_config_remove_account (MailConfigAccount *account)
{
    /* Removing the current default, so make the first account the
           default */
    if (account == mail_config_get_default_account ())
        config->default_account = 0;

    config->accounts = g_slist_remove (config->accounts, account);
    account_destroy (account);

    return config->accounts;
}

gint
mail_config_get_default_account_num (void)
{
    return config->default_account;
}

static void
mail_config_set_default_account_num (gint new_default)
{
    config->default_account = new_default;
}

void
mail_config_set_default_account (const MailConfigAccount *account)
{
    int position;
    
    position = g_slist_index (config->accounts, (void*)account);
    
    config->default_account = position;
    
    return;
}

const MailConfigIdentity *
mail_config_get_default_identity (void)
{
    const MailConfigAccount *account;
    
    account = mail_config_get_default_account ();
    if (account)
        return account->id;
    else
        return NULL;
}

const MailConfigService *
mail_config_get_default_transport (void)
{
    const MailConfigAccount *account;
    
    account = mail_config_get_default_account ();
    if (account)
        return account->transport;
    else
        return NULL;
}

const MailConfigService *
mail_config_get_default_news (void)
{
    if (!config->news)
        return NULL;
    
    return (MailConfigService *)config->news->data;
}

const GSList *
mail_config_get_news (void)
{
    return config->news;
}

void
mail_config_add_news (MailConfigService *news) 
{
    config->news = g_slist_append (config->news, news);
}

const GSList *
mail_config_remove_news (MailConfigService *news)
{
    config->news = g_slist_remove (config->news, news);
    service_destroy (news);
    
    return config->news;
}

GSList *
mail_config_get_sources (void)
{
    const GSList *accounts;
    GSList *sources = NULL;
    
    accounts = mail_config_get_accounts ();
    while (accounts) {
        const MailConfigAccount *account = accounts->data;
        
        if (account->source)
            sources = g_slist_append (sources, account->source);
        
        accounts = accounts->next;
    }
    
    return sources;
}

char *
mail_config_folder_to_cachename (CamelFolder *folder, const char *prefix)
{
    CamelService *service = CAMEL_SERVICE (folder->parent_store);
    char *url, *filename;

    /* This is the way it is for backward compatibility with
     * the way it was, not because it's necessarily a good thing.
     */

    url = camel_url_to_string (service->url, CAMEL_URL_HIDE_PASSWORD | CAMEL_URL_HIDE_PARAMS);

    /* Really we want to check CAMEL_IS_LOCAL_FOLDER here, but we
     * can't do that.
     */
    if (service->provider->flags & CAMEL_PROVIDER_IS_REMOTE) {
        char *store_url = url;
        url = g_strdup_printf ("%s/%s", store_url, folder->full_name);
        g_free (store_url);
    }
    e_filename_make_safe (url);

    filename = g_strdup_printf ("%s/config/%s%s", evolution_dir, prefix, url);
    g_free (url);
    
    return filename;
}


/* Async service-checking/authtype-lookup code. */
struct _check_msg {
    struct _mail_msg msg;

    const char *url;
    CamelProviderType type;
    GList **authtypes;
    gboolean *success;
};

static void
check_service_check (struct _mail_msg *mm)
{
    struct _check_msg *m = (struct _check_msg *)mm;
    CamelService *service = NULL;

    camel_operation_register(mm->cancel);

    service = camel_session_get_service (session, m->url, m->type, &mm->ex);
    if (!service) {
        camel_operation_unregister(mm->cancel);
        return;
    }

    if (m->authtypes)
        *m->authtypes = camel_service_query_auth_types (service, &mm->ex);
    else
        camel_service_connect (service, &mm->ex);

    camel_object_unref (CAMEL_OBJECT (service));
    *m->success = !camel_exception_is_set(&mm->ex);

    camel_operation_unregister(mm->cancel);
}

static struct _mail_msg_op check_service_op = {
    NULL,
    check_service_check,
    NULL,
    NULL
};

static void
check_cancelled (GnomeDialog *dialog, int button, gpointer data)
{
    int *msg_id = data;

    mail_msg_cancel (*msg_id);
}

/**
 * mail_config_check_service:
 * @url: service url
 * @type: provider type
 * @authtypes: set to list of supported authtypes on return if non-%NULL.
 *
 * Checks the service for validity. If @authtypes is non-%NULL, it will
 * be filled in with a list of supported authtypes.
 *
 * Return value: %TRUE on success or %FALSE on error.
 **/
gboolean
mail_config_check_service (const char *url, CamelProviderType type, GList **authtypes)
{
    gboolean ret = FALSE;
    struct _check_msg *m;
    int id;
    GtkWidget *dialog, *label;

    m = mail_msg_new(&check_service_op, NULL, sizeof(*m));
    m->url = url;
    m->type = type;
    m->authtypes = authtypes;
    m->success = &ret;

    id = m->msg.seq;
    e_thread_put(mail_thread_queued, (EMsg *)m);

    dialog = gnome_dialog_new (_("Connecting to server..."),
                   GNOME_STOCK_BUTTON_CANCEL,
                   NULL);
    label = gtk_label_new (_("Connecting to server..."));
    gtk_box_pack_start (GTK_BOX(GNOME_DIALOG (dialog)->vbox),
                label, TRUE, TRUE, 10);
    gnome_dialog_set_close (GNOME_DIALOG (dialog), FALSE);
    gtk_signal_connect (GTK_OBJECT (dialog), "clicked",
                GTK_SIGNAL_FUNC (check_cancelled), &id);
    gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
    gtk_widget_show_all (dialog);

    mail_msg_wait(id);

    gtk_widget_destroy (dialog);

    return ret;
}

/* MailConfig Bonobo object */
#define PARENT_TYPE BONOBO_X_OBJECT_TYPE
static BonoboObjectClass *parent_class = NULL;

/* For the bonobo object */
typedef struct _EvolutionMailConfig EvolutionMailConfig;
typedef struct _EvolutionMailConfigClass EvolutionMailConfigClass;

struct _EvolutionMailConfig {
    BonoboXObject parent;
};

struct _EvolutionMailConfigClass {
    BonoboXObjectClass parent_class;

    POA_GNOME_Evolution_MailConfig__epv epv;
};

static void
impl_GNOME_Evolution_MailConfig_addAccount (PortableServer_Servant servant,
                        const GNOME_Evolution_MailConfig_Account *account,
                        CORBA_Environment *ev)
{
    GNOME_Evolution_MailConfig_Service source, transport;
    GNOME_Evolution_MailConfig_Identity id;
    MailConfigAccount *mail_account;
    MailConfigService *mail_service;
    MailConfigIdentity *mail_id;

    mail_account = g_new0 (MailConfigAccount, 1);
    mail_account->name = g_strdup (account->name);

    /* Copy ID */
    id = account->id;
    mail_id = g_new0 (MailConfigIdentity, 1);
    mail_id->name = g_strdup (id.name);
    mail_id->address = g_strdup (id.address);
    mail_id->organization = g_strdup (id.organization);
    mail_id->signature = g_strdup (id.signature);
    mail_id->html_signature = g_strdup (id.html_signature);
    mail_id->has_html_signature = id.has_html_signature;

    mail_account->id = mail_id;

    /* Copy source */
    source = account->source;
    mail_service = g_new0 (MailConfigService, 1);
    mail_service->url = g_strdup (source.url);
    mail_service->keep_on_server = source.keep_on_server;
    mail_service->auto_check = source.auto_check;
    mail_service->auto_check_time = source.auto_check_time;
    mail_service->save_passwd = source.save_passwd;
    mail_service->enabled = source.enabled;

    mail_account->source = mail_service;

    /* Copy transport */
    transport = account->transport;
    mail_service = g_new0 (MailConfigService, 1);
    mail_service->url = g_strdup (transport.url);
    mail_service->keep_on_server = transport.keep_on_server;
    mail_service->auto_check = transport.auto_check;
    mail_service->auto_check_time = transport.auto_check_time;
    mail_service->save_passwd = transport.save_passwd;
    mail_service->enabled = transport.enabled;

    mail_account->transport = mail_service;

    /* Add new account */
    mail_config_add_account (mail_account);
}

static void
evolution_mail_config_class_init (EvolutionMailConfigClass *klass)
{
    POA_GNOME_Evolution_MailConfig__epv *epv = &klass->epv;

    parent_class = gtk_type_class (PARENT_TYPE);
    epv->addAccount = impl_GNOME_Evolution_MailConfig_addAccount;
}

static void
evolution_mail_config_init (EvolutionMailConfig *config)
{
}

BONOBO_X_TYPE_FUNC_FULL (EvolutionMailConfig,
             GNOME_Evolution_MailConfig,
             PARENT_TYPE,
             evolution_mail_config);

static BonoboObject *
evolution_mail_config_factory_fn (BonoboGenericFactory *factory,
                  void *closure)
{
    EvolutionMailConfig *config;

    g_warning ("Made");
    config = gtk_type_new (evolution_mail_config_get_type ());
    return BONOBO_OBJECT (config);
}

void
evolution_mail_config_factory_init (void)
{
    BonoboGenericFactory *factory;
    
    g_warning ("Starting mail config");
    factory = bonobo_generic_factory_new (MAIL_CONFIG_IID, 
                          evolution_mail_config_factory_fn,
                          NULL);
    if (factory == NULL) {
        g_warning ("Error starting MailConfig");
    }

    g_warning ("Registered");
    bonobo_running_context_auto_exit_unref (BONOBO_OBJECT (factory));
}