aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-local.c
blob: 5db550d46defe32c8caf7ede972402e998d91d17 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* mail-local.c: Local mailbox support. */

/* 
 * Authors: 
 *  Michael Zucchi <NotZed@ximian.com>
 *  Peter Williams <peterw@ximian.com>
 *  Ettore Perazzoli <ettore@ximian.com>
 *  Dan Winship <danw@ximian.com>
 *
 * Copyright 2000 Ximian, Inc. (www.ximian.com)
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of version 2 of the GNU General Public 
 * License as published by the Free Software Foundation.
 *
 * 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 Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

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

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

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

#include "e-util/e-path.h"
#include "e-util/e-dialog-utils.h"
#include <gal/util/e-xml-utils.h>

#include "Evolution.h"
#include "evolution-storage.h"
#include "evolution-shell-component.h"
#include "evolution-storage-listener.h"

#include "camel/camel.h"
#include "camel/camel-vtrash-folder.h"

#include "mail.h"
#include "mail-local.h"
#include "mail-tools.h"
#include "mail-mt.h"
#include "mail-folder-cache.h"
#include "mail-vfolder.h"
#include "mail-ops.h"

#define d(x) 

/* sigh, required for passing around to some functions */
static GNOME_Evolution_Storage local_corba_storage = CORBA_OBJECT_NIL;

/* ** MailLocalStore ** (protos) ************************************************** */

#define MAIL_LOCAL_STORE_TYPE     (mail_local_store_get_type ())
#define MAIL_LOCAL_STORE(obj)     (CAMEL_CHECK_CAST((obj), MAIL_LOCAL_STORE_TYPE, MailLocalStore))
#define MAIL_LOCAL_STORE_CLASS(k) (CAMEL_CHECK_CLASS_CAST ((k), MAIL_LOCAL_STORE_TYPE, MailLocalStoreClass))
#define MAIL_IS_LOCAL_STORE(o)    (CAMEL_CHECK_TYPE((o), MAIL_LOCAL_STORE_TYPE))

typedef struct {
    CamelStore parent_object;

    /* stores CamelFolderInfo's of the folders we're supposed to know about, by uri */
    GHashTable *folder_infos;
    GMutex *folder_info_lock;

} MailLocalStore;

typedef struct {
    CamelStoreClass parent_class;
} MailLocalStoreClass;

static CamelType mail_local_store_get_type (void);

static MailLocalStore *global_local_store;

/* ** MailLocalFolder ** (protos) ************************************************* */

#define MAIL_LOCAL_FOLDER_TYPE     (mail_local_folder_get_type ())
#define MAIL_LOCAL_FOLDER(obj)     (CAMEL_CHECK_CAST((obj), MAIL_LOCAL_FOLDER_TYPE, MailLocalFolder))
#define MAIL_LOCAL_FOLDER_CLASS(k) (CAMEL_CHECK_CLASS_CAST ((k), MAIL_LOCAL_FOLDER_TYPE, MailLocalFolderClass))
#define MAIL_IS_LOCAL_FOLDER(o)    (CAMEL_CHECK_TYPE((o), MAIL_LOCAL_FOLDER_TYPE))

#define LOCAL_STORE_LOCK(folder)   (g_mutex_lock   (((MailLocalStore *)folder)->folder_info_lock))
#define LOCAL_STORE_UNLOCK(folder) (g_mutex_unlock (((MailLocalStore *)folder)->folder_info_lock))

struct _local_meta {
    char *path;     /* path of metainfo */

    char *format;       /* format of mailbox */
    char *name;     /* name of actual mbox */
    int indexed;        /* is body indexed? */
};

typedef struct {
    CamelFolder parent_object;

    CamelFolder *real_folder;
    CamelStore *real_store;

    char *description;
    char *real_path;

    struct _local_meta *meta;

    GMutex *real_folder_lock; /* no way to use the CamelFolder's lock, so... */
} MailLocalFolder;

typedef struct {
    CamelFolderClass parent_class;
} MailLocalFolderClass;

static CamelType mail_local_folder_get_type (void);

#ifdef ENABLE_THREADS
#define LOCAL_FOLDER_LOCK(folder)   (g_mutex_lock   (((MailLocalFolder *)folder)->real_folder_lock))
#define LOCAL_FOLDER_UNLOCK(folder) (g_mutex_unlock (((MailLocalFolder *)folder)->real_folder_lock))
#else
#define LOCAL_FOLDER_LOCK(folder)
#define LOCAL_FOLDER_UNLOCK(folder)
#endif

/* ** MailLocalFolder ************************************************************* */

static struct _local_meta *
load_metainfo(const char *path)
{
    xmlDocPtr doc = NULL;
    xmlNodePtr node;
    struct _local_meta *meta;
    struct stat st;
    
    d(printf("Loading folder metainfo from : %s\n", path));

    meta = g_malloc0(sizeof(*meta));
    meta->path = g_strdup(path);
    
    if (stat (path, &st) == -1 || !S_ISREG (st.st_mode))
        goto dodefault;
    
    doc = xmlParseFile(path);
    if (doc == NULL)
        goto dodefault;

    node = doc->children;
    if (strcmp(node->name, "folderinfo"))
        goto dodefault;

    node = node->children;
    while (node) {
        if (!strcmp(node->name, "folder")) {
            char *index, *txt;
            
            txt = xmlGetProp(node, "type");
            meta->format = g_strdup(txt?txt:"mbox");
            xmlFree(txt);
            
            txt = xmlGetProp(node, "name");
            meta->name = g_strdup(txt?txt:"mbox");
            xmlFree(txt);
            
            index = xmlGetProp(node, "index");
            if (index) {
                meta->indexed = atoi(index);
                xmlFree(index);
            } else
                meta->indexed = TRUE;
            
        }
        node = node->next;
    }
    xmlFreeDoc(doc);
    return meta;

 dodefault:
    meta->format = g_strdup("mbox"); /* defaults */
    meta->name = g_strdup("mbox");
    meta->indexed = TRUE;
    xmlFreeDoc(doc);
    return meta;
}

static void
free_metainfo(struct _local_meta *meta)
{
    g_free(meta->path);
    g_free(meta->format);
    g_free(meta->name);
    g_free(meta);
}

static gboolean
save_metainfo (struct _local_meta *meta)
{
    xmlDocPtr doc;
    xmlNodePtr root, node;
    int ret;
    
    d(printf("Saving folder metainfo to : %s\n", meta->path));
    
    doc = xmlNewDoc("1.0");
    root = xmlNewDocNode(doc, NULL, "folderinfo", NULL);
    xmlDocSetRootElement(doc, root);

    node  = xmlNewChild(root, NULL, "folder", NULL);
    xmlSetProp(node, "type", meta->format);
    xmlSetProp(node, "name", meta->name);
    xmlSetProp(node, "index", meta->indexed?"1":"0");
    
    ret = e_xml_save_file (meta->path, doc);
    
    xmlFreeDoc (doc);
    
    return ret == -1 ? FALSE : TRUE;
}

static CamelFolderClass *mlf_parent_class = NULL;

/* forward a bunch of functions to the real folder. This pretty
 * much sucks but I haven't found a better way of doing it.
 */

/* We need to do it without having locked our folder, otherwise
   we can get sync hangs with vfolders/trash */
static void
mlf_refresh_info(CamelFolder *folder, CamelException *ex)
{
    MailLocalFolder *mlf = MAIL_LOCAL_FOLDER(folder);
    CamelFolder *f;

    LOCAL_FOLDER_LOCK(mlf);
    f = mlf->real_folder;
    camel_object_ref(f);
    LOCAL_FOLDER_UNLOCK(mlf);

    camel_folder_refresh_info(f, ex);
    camel_object_unref(f);
}

static void
mlf_sync(CamelFolder *folder, gboolean expunge, CamelException *ex)
{
    MailLocalFolder *mlf = MAIL_LOCAL_FOLDER(folder);
    CamelFolder *f;

    LOCAL_FOLDER_LOCK(mlf);
    f = mlf->real_folder;
    camel_object_ref(f);
    LOCAL_FOLDER_UNLOCK(mlf);

    camel_folder_sync(f, expunge, ex);
    camel_object_unref(f);
}

static void
mlf_expunge(CamelFolder *folder, CamelException *ex)
{
    MailLocalFolder *mlf = MAIL_LOCAL_FOLDER(folder);
    CamelFolder *f;

    LOCAL_FOLDER_LOCK(mlf);
    f = mlf->real_folder;
    camel_object_ref(f);
    LOCAL_FOLDER_UNLOCK(mlf);

    camel_folder_expunge(f, ex);
    camel_object_unref(f);
}

static void
mlf_append_message(CamelFolder *folder, CamelMimeMessage *message, const CamelMessageInfo *info, char **appended_uid, CamelException *ex)
{
    MailLocalFolder *mlf = MAIL_LOCAL_FOLDER(folder);
    CamelFolder *f;

    LOCAL_FOLDER_LOCK(mlf);
    f = mlf->real_folder;
    camel_object_ref(f);
    LOCAL_FOLDER_UNLOCK(mlf);

    camel_folder_append_message(f, message, info, appended_uid, ex);
    camel_object_unref(f);
}

static CamelMimeMessage *
mlf_get_message(CamelFolder *folder, const char *uid, CamelException *ex)
{
    MailLocalFolder *mlf = MAIL_LOCAL_FOLDER(folder);
    CamelMimeMessage *ret;
    CamelFolder *f;

    LOCAL_FOLDER_LOCK(mlf);
    f = mlf->real_folder;
    camel_object_ref(f);
    LOCAL_FOLDER_UNLOCK(mlf);

    ret = camel_folder_get_message(f, uid, ex);
    camel_object_unref(f);

    return ret;
}

static GPtrArray *
mlf_search_by_expression(CamelFolder *folder, const char *expression, CamelException *ex)
{
    MailLocalFolder *mlf = MAIL_LOCAL_FOLDER (folder);
    GPtrArray *ret;
    CamelFolder *f;

    LOCAL_FOLDER_LOCK(mlf);
    f = mlf->real_folder;
    camel_object_ref(f);
    LOCAL_FOLDER_UNLOCK(mlf);

    ret = camel_folder_search_by_expression(f, expression, ex);
    camel_object_unref(f);

    return ret;
}

static GPtrArray *
mlf_search_by_uids(CamelFolder *folder, const char *expression, GPtrArray *uids, CamelException *ex)
{
    MailLocalFolder *mlf = MAIL_LOCAL_FOLDER (folder);
    GPtrArray *ret;
    CamelFolder *f;

    LOCAL_FOLDER_LOCK(mlf);
    f = mlf->real_folder;
    camel_object_ref(f);
    LOCAL_FOLDER_UNLOCK(mlf);

    ret = camel_folder_search_by_uids(f, expression, uids, ex);
    camel_object_unref(f);

    return ret;
}

static void
mlf_search_free(CamelFolder *folder, GPtrArray *result)
{
    MailLocalFolder *mlf = MAIL_LOCAL_FOLDER(folder);
    CamelFolder *f;

    LOCAL_FOLDER_LOCK(mlf);
    f = mlf->real_folder;
    camel_object_ref(f);
    LOCAL_FOLDER_UNLOCK(mlf);

    camel_folder_search_free(f, result);
    camel_object_unref(f);
}

static void
mlf_set_message_flags(CamelFolder *folder, const char *uid, guint32 flags, guint32 set)
{
    MailLocalFolder *mlf = MAIL_LOCAL_FOLDER(folder);
    CamelFolder *f;

    LOCAL_FOLDER_LOCK(mlf);
    f = mlf->real_folder;
    camel_object_ref(f);
    LOCAL_FOLDER_UNLOCK(mlf);

    camel_folder_set_message_flags(mlf->real_folder, uid, flags, set);
    camel_object_unref(f);
}

static void
mlf_set_message_user_flag(CamelFolder *folder, const char *uid, const char *name, gboolean value)
{
    MailLocalFolder *mlf = MAIL_LOCAL_FOLDER(folder);
    CamelFolder *f;

    LOCAL_FOLDER_LOCK(mlf);
    f = mlf->real_folder;
    camel_object_ref(f);
    LOCAL_FOLDER_UNLOCK(mlf);

    camel_folder_set_message_user_flag(mlf->real_folder, uid, name, value);
    camel_object_unref(f);
}

static void
mlf_set_message_user_tag(CamelFolder *folder, const char *uid, const char *name, const char *value)
{
    MailLocalFolder *mlf = MAIL_LOCAL_FOLDER(folder);
    CamelFolder *f;

    LOCAL_FOLDER_LOCK(mlf);
    f = mlf->real_folder;
    camel_object_ref(f);
    LOCAL_FOLDER_UNLOCK(mlf);

    camel_folder_set_message_user_tag(mlf->real_folder, uid, name, value);
    camel_object_unref(f);
}

/* Internal store-rename call, update our strings */
static void
mlf_rename(CamelFolder *folder, const char *new)
{
    MailLocalFolder *mlf = (MailLocalFolder *)folder;

    /* first, proxy it down */
    if (mlf->real_folder) {
        char *mbox = g_strdup_printf("%s/%s", new, mlf->meta->name);

        d(printf("renaming real folder to %s\n", mbox));

        camel_folder_rename(mlf->real_folder, mbox);
        g_free(mbox);
    }

    /* Then do our stuff */
    g_free(mlf->real_path);
    mlf->real_path = g_strdup(new);

    g_free(mlf->meta->path);
    mlf->meta->path = g_strdup_printf("%s/%s/local-metadata.xml", ((CamelService *)folder->parent_store)->url->path, new);

    /* Then pass it up */
    ((CamelFolderClass *)mlf_parent_class)->rename(folder, new);
}

/* and, conversely, forward the real folder's signals. */

static void
mlf_proxy_message_changed(CamelObject *real_folder, gpointer event_data, gpointer user_data)
{
    camel_object_trigger_event(user_data, "message_changed", event_data);
}

static void
mlf_proxy_folder_changed(CamelObject *real_folder, gpointer event_data, gpointer user_data)
{
    camel_object_trigger_event(user_data, "folder_changed", event_data);
}

static void
mlf_unset_folder (MailLocalFolder *mlf)
{
    CamelFolder *folder = (CamelFolder *)mlf;

    g_assert(mlf->real_folder);

    camel_object_unhook_event(mlf->real_folder,
                  "message_changed",
                  mlf_proxy_message_changed,
                  mlf);
    camel_object_unhook_event(mlf->real_folder,
                  "folder_changed",
                  mlf_proxy_folder_changed,
                  mlf);

    camel_object_unref(folder->summary);
    folder->summary = NULL;
    camel_object_unref(mlf->real_folder);
    mlf->real_folder = NULL;
    camel_object_unref(mlf->real_store);
    mlf->real_store = NULL;

    folder->permanent_flags = 0;
    folder->folder_flags = 0;
}

static gboolean
mlf_set_folder(MailLocalFolder *mlf, guint32 flags, CamelException *ex)
{
    CamelFolder *folder = (CamelFolder *)mlf;
    char *uri, *mbox;

    g_assert(mlf->real_folder == NULL);

    uri = g_strdup_printf("%s:%s", mlf->meta->format, ((CamelService *)folder->parent_store)->url->path);

    d(printf("opening real store: %s\n", uri));
    mlf->real_store = camel_session_get_store(session, uri, ex);
    g_free(uri);
    if (mlf->real_store == NULL)
        return FALSE;

    if (mlf->meta->indexed)
        flags |= CAMEL_STORE_FOLDER_BODY_INDEX;

    /* mlf->real_folder = camel_store_get_folder(mlf->real_store, mlf->meta->name, flags, ex); */
    mbox = g_strdup_printf("%s/%s", mlf->real_path, mlf->meta->name);
    d(printf("Opening mbox on real path: %s\n", mbox));
    mlf->real_folder = camel_store_get_folder(mlf->real_store, mbox, flags, ex);
    g_free(mbox);
    if (mlf->real_folder == NULL)
        return FALSE;

    if (mlf->real_folder->folder_flags & CAMEL_FOLDER_HAS_SUMMARY_CAPABILITY) {
        folder->summary = mlf->real_folder->summary;
        camel_object_ref(mlf->real_folder->summary);
    }

    folder->permanent_flags = mlf->real_folder->permanent_flags;
    folder->folder_flags = mlf->real_folder->folder_flags;

    camel_object_hook_event(mlf->real_folder, "message_changed", mlf_proxy_message_changed, mlf);
    camel_object_hook_event(mlf->real_folder, "folder_changed", mlf_proxy_folder_changed, mlf);

    return TRUE;
}

static int
mlf_getv(CamelObject *object, CamelException *ex, CamelArgGetV *args)
{
    CamelFolder *folder = (CamelFolder *)object;
    MailLocalFolder *mlf = (MailLocalFolder *)object;
    int i, count=args->argc;
    guint32 tag;

    for (i=0;i<args->argc;i++) {
        CamelArgGet *arg = &args->argv[i];
        
        tag = arg->tag;
        
        switch (tag & CAMEL_ARG_TAG) {
            /* CamelObject args */
        case CAMEL_OBJECT_ARG_DESCRIPTION:
            if (mlf->description == NULL) {
                int pathlen;

                /* string to describe a local folder as the location of a message */
                pathlen = strlen(evolution_dir) + strlen("local") + 1;
                if (strlen(folder->full_name) > pathlen)
                    mlf->description = g_strdup_printf(_("Local folders/%s"), folder->full_name+pathlen);
                else
                    mlf->description = g_strdup_printf(_("Local folders/%s"), folder->name);
            }
            *arg->ca_str = mlf->description;
            break;
        default:
            count--;
            continue;
        }

        arg->tag = (tag & CAMEL_ARG_TYPE) | CAMEL_ARG_IGNORE;
    }

    if (count)
        return ((CamelObjectClass *)mlf_parent_class)->getv(object, ex, args);

    return 0;
}

static void 
mlf_class_init (CamelObjectClass *camel_object_class)
{
    CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS (camel_object_class);

    /* override all the functions subclassed in providers/local/ */

    camel_folder_class->refresh_info = mlf_refresh_info;
    camel_folder_class->sync = mlf_sync;
    camel_folder_class->expunge = mlf_expunge;
    camel_folder_class->append_message = mlf_append_message;
    camel_folder_class->get_message = mlf_get_message;
    camel_folder_class->search_free = mlf_search_free;

    camel_folder_class->search_by_expression = mlf_search_by_expression;
    camel_folder_class->search_by_uids = mlf_search_by_uids;
    camel_folder_class->set_message_flags = mlf_set_message_flags;
    camel_folder_class->set_message_user_flag = mlf_set_message_user_flag;
    camel_folder_class->set_message_user_tag = mlf_set_message_user_tag;

    camel_folder_class->rename = mlf_rename;

    camel_object_class->getv = mlf_getv;
}

static void
mlf_init (CamelObject *obj, CamelObjectClass *klass)
{
    MailLocalFolder *mlf = MAIL_LOCAL_FOLDER (obj);

#ifdef ENABLE_THREADS
    mlf->real_folder_lock = g_mutex_new();
#endif
}

static void
mlf_finalize (CamelObject *obj)
{
    MailLocalFolder *mlf = MAIL_LOCAL_FOLDER (obj);

    if (mlf->real_folder)
        mlf_unset_folder(mlf);

    free_metainfo(mlf->meta);
    
    g_free (mlf->real_path);
    
#ifdef ENABLE_THREADS
    g_mutex_free (mlf->real_folder_lock);
#endif
}

static CamelType
mail_local_folder_get_type (void)
{
    static CamelType mail_local_folder_type = CAMEL_INVALID_TYPE;

    if (mail_local_folder_type == CAMEL_INVALID_TYPE) {
        mail_local_folder_type = camel_type_register(CAMEL_FOLDER_TYPE,
                                 "MailLocalFolder",
                                 sizeof (MailLocalFolder),
                                 sizeof (MailLocalFolderClass),
                                 mlf_class_init,
                                 NULL,
                                 mlf_init,
                                 mlf_finalize);
        mlf_parent_class = (CamelFolderClass *)CAMEL_FOLDER_TYPE;
    }

    return mail_local_folder_type;
}

static MailLocalFolder *
mail_local_folder_construct(MailLocalFolder *mlf, MailLocalStore *parent_store, const char *full_name, CamelException *ex)
{
    char *metapath, *name;
    
    name = g_path_get_basename (full_name);
    d(printf ("constructing local folder: full = %s, name = %s\n", full_name, name));
    camel_folder_construct (CAMEL_FOLDER (mlf), CAMEL_STORE (parent_store), full_name, name);
    g_free (name);
    
    mlf->real_path = g_strdup (((CamelFolder *) mlf)->full_name);
    
    metapath = g_strdup_printf ("%s/%s/local-metadata.xml", ((CamelService *) parent_store)->url->path, full_name);
    mlf->meta = load_metainfo (metapath);
    g_free (metapath);
    
    return mlf;
}

static gboolean
mail_local_folder_reconfigure (MailLocalFolder *mlf, const char *new_format, int index_body, CamelException *ex)
{
    CamelStore *fromstore = NULL;
    CamelFolder *fromfolder = NULL;
    char *oldformat = NULL;
    char *store_uri;
    GPtrArray *uids;
    int real_folder_frozen = FALSE;
    int format_change, index_changed;
    char *tmpname = NULL;
    char *mbox = NULL;

    format_change = strcmp(mlf->meta->format, new_format) != 0;
    index_changed = mlf->meta->indexed != index_body;

    if (format_change == FALSE && index_changed == FALSE)
        return TRUE;

    camel_operation_start(NULL, _("Reconfiguring folder"));

    /* first things first */
    g_assert (ex);
    LOCAL_FOLDER_LOCK (mlf);

    /* first, 'close' the old folder */
    if (mlf->real_folder) {
        camel_folder_sync(mlf->real_folder, FALSE, ex);
        if (camel_exception_is_set (ex))
            goto cleanup;
        mlf_unset_folder(mlf);
    }

    /* only indexed change, just re-open with new flags */
    if (!format_change) {
        mlf->meta->indexed = index_body;
        mlf_set_folder(mlf, CAMEL_STORE_FOLDER_CREATE, ex);
        save_metainfo(mlf->meta);
        goto cleanup;
    }

    store_uri = g_strdup_printf("%s:%s", mlf->meta->format, ((CamelService *)((CamelFolder *)mlf)->parent_store)->url->path);
    fromstore = camel_session_get_store(session, store_uri, ex);
    g_free(store_uri);
    if (fromstore == NULL)
        goto cleanup;

    oldformat = mlf->meta->format;
    mlf->meta->format = g_strdup(new_format);

    /* rename the old mbox and open it again, without indexing */
    tmpname = g_strdup_printf ("%s/%s_reconfig", mlf->real_path, mlf->meta->name);
    mbox = g_strdup_printf("%s/%s", mlf->real_path, mlf->meta->name);
    d(printf("renaming %s to %s, and opening it\n", mbox, tmpname));
    
    camel_store_rename_folder(fromstore, mbox, tmpname, ex);
    if (camel_exception_is_set(ex))
        goto cleanup;
    
    /* we dont need to set the create flag ... or need an index if it has one */
    fromfolder = camel_store_get_folder(fromstore, tmpname, 0, ex);
    if (fromfolder == NULL || camel_exception_is_set(ex)) {
        /* try and recover ... */
        camel_exception_clear(ex);
        camel_store_rename_folder(fromstore, tmpname, mbox, ex);
        goto cleanup;
    }
    
    /* create a new mbox */
    d(printf("Creating the destination mbox\n"));

    if (!mlf_set_folder(mlf, CAMEL_STORE_FOLDER_CREATE, ex)) {
        d(printf("cannot open destination folder\n"));
        /* try and recover ... */
        camel_exception_clear(ex);
        camel_store_rename_folder(fromstore, tmpname, mbox, ex);
        goto cleanup;
    }

    real_folder_frozen = TRUE;
    camel_folder_freeze(mlf->real_folder);

    uids = camel_folder_get_uids(fromfolder);
    camel_folder_transfer_messages_to(fromfolder, uids, mlf->real_folder, NULL, TRUE, ex);
    camel_folder_free_uids(fromfolder, uids);
    if (camel_exception_is_set(ex))
        goto cleanup;
    
    camel_folder_expunge(fromfolder, ex);
    
    d(printf("delete old mbox ...\n"));
    camel_object_unref(fromfolder);
    fromfolder = NULL;
    camel_store_delete_folder(fromstore, tmpname, ex);
    
    /* switch format */
    g_free(oldformat);
    oldformat = NULL;
    if (save_metainfo(mlf->meta) == FALSE) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot save folder metainfo; "
                    "you may find you can't\n"
                    "open this folder anymore: %s: %s"),
                      mlf->meta->path, strerror(errno));
    }
    
 cleanup:
    if (oldformat) {
        g_free(mlf->meta->format);
        mlf->meta->format = oldformat;
    }
    if (mlf->real_folder == NULL)
        mlf_set_folder (mlf, CAMEL_STORE_FOLDER_CREATE, ex);
    if (fromfolder)
        camel_object_unref(fromfolder);
    if (fromstore)
        camel_object_unref(fromstore);

    g_free(tmpname);
    g_free(mbox);

    LOCAL_FOLDER_UNLOCK (mlf);

    if (real_folder_frozen)
        camel_folder_thaw(mlf->real_folder);

    camel_operation_end(NULL);

    return !camel_exception_is_set(ex);
}
        
/* ******************************************************************************** */

static CamelObjectClass *local_store_parent_class = NULL;

static CamelFolder *
mls_get_folder(CamelStore *store, const char *folder_name, guint32 flags, CamelException *ex)
{
    MailLocalStore *local_store = MAIL_LOCAL_STORE (store);
    MailLocalFolder *folder;

    d(printf("--LOCAL-- get_folder: %s\n", folder_name));

    folder = (MailLocalFolder *)camel_object_new(MAIL_LOCAL_FOLDER_TYPE);
    folder = mail_local_folder_construct(folder, local_store, folder_name, ex);
    if (folder == NULL)
        return NULL;

    if (!mlf_set_folder(folder, flags, ex)) {
        camel_object_unref(folder);
        return NULL;
    }

    if (flags & CAMEL_STORE_FOLDER_CREATE) {
        if (save_metainfo(folder->meta) == FALSE) {
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                          _("Cannot save folder metainfo to %s: %s"),
                          folder->meta->path, g_strerror(errno));
            camel_object_unref(folder);
            return NULL;
        }
    }

    return (CamelFolder *)folder;
}

static void
mls_delete_folder(CamelStore *store, const char *folder_name, CamelException *ex)
{
    CamelStore *real_store;
    char *metapath, *uri, *mbox;
    CamelException local_ex;
    struct _local_meta *meta;

    d(printf("Deleting folder: %s %s\n", ((CamelService *)store)->url->path, folder_name));

    camel_exception_init(&local_ex);

    /* find the real store for this folder, and proxy the call */
    metapath = g_strdup_printf("%s%s/local-metadata.xml", ((CamelService *)store)->url->path, folder_name);
    meta = load_metainfo(metapath);
    uri = g_strdup_printf("%s:%s", meta->format, ((CamelService *)store)->url->path);
    real_store = (CamelStore *)camel_session_get_service(session, uri, CAMEL_PROVIDER_STORE, ex);
    g_free(uri);
    if (real_store == NULL) {
        g_free(metapath);
        free_metainfo(meta);
        camel_object_unref(real_store);
        return;
    }

    mbox = g_strdup_printf("%s/%s", folder_name, meta->name);
    camel_store_delete_folder(real_store, mbox, &local_ex);
    g_free(mbox);
    if (camel_exception_is_set(&local_ex)) {
        camel_exception_xfer(ex, &local_ex);
        g_free(metapath);
        free_metainfo(meta);
        camel_object_unref(real_store);
        return;
    }

    camel_object_unref((CamelObject *)real_store);

    free_metainfo(meta);

    if (unlink(metapath) == -1) {
        camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
                     _("Cannot delete folder metadata %s: %s"),
                     metapath, g_strerror(errno));
    }

    g_free(metapath);
}

static void
mls_rename_folder(CamelStore *store, const char *old_name, const char *new_name, CamelException *ex)
{
    CamelStore *real_store;
    /*MailLocalStore *mls = (MailLocalStore *)store;*/
    char *uri;
    /*CamelException local_ex;*/
    struct _local_meta *meta;
    char *oldname, *newname;
    char *oldmeta, *newmeta;
    struct stat st;

    /* folder:rename() updates all our in-memory data to match */

    /* FIXME: Need to lock the subfolder that matches this if its open
       Then rename it and unlock it when done */

    d(printf("Renaming folder from '%s' to '%s'\n", old_name, new_name));

    oldmeta = g_strdup_printf("%s%s/local-metadata.xml", ((CamelService *)store)->url->path, old_name);
    newmeta = g_strdup_printf("%s%s/local-metadata.xml", ((CamelService *)store)->url->path, new_name);

    meta = load_metainfo(oldmeta);
    uri = g_strdup_printf("%s:%s", meta->format, ((CamelService *)store)->url->path);
    real_store = (CamelStore *)camel_session_get_service(session, uri, CAMEL_PROVIDER_STORE, ex);
    g_free(uri);
    if (real_store == NULL) {
        g_free(newmeta);
        g_free(oldmeta);
        free_metainfo(meta);
        return;
    }

    oldname = g_strdup_printf("%s/%s", old_name, meta->name);
    newname = g_strdup_printf("%s/%s", new_name, meta->name);

    camel_store_rename_folder(real_store, oldname, newname, ex);
    if (!camel_exception_is_set(ex)) {
        /* If this fails?  Well, doesn't really matter but 'fail' anyway */
        if (stat(oldmeta, &st) == 0
            && rename(oldmeta, newmeta) == -1) {
            camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
                         _("Could not rename folder %s to %s: %s"),
                         old_name, new_name, strerror(errno));
        } else {
            /* So .. .the shell does a remove/add now the rename worked, so we dont
               have to do this.  However totally broken that idea might be */
#if 0
            CamelFolderInfo *info;
            const char *tmp;
            char *olduri, *newuri;

            olduri = g_strdup_printf("%s:%s%s", ((CamelService *)store)->url->protocol, ((CamelService *)store)->url->path, old_name);
            newuri = g_strdup_printf("%s:%s%s", ((CamelService *)store)->url->protocol, ((CamelService *)store)->url->path, new_name);
            info = g_hash_table_lookup(mls->folder_infos, olduri);
            if (info) {
                CamelRenameInfo reninfo;

                g_free(info->url);
                g_free(info->full_name);
                g_free(info->name);
                g_free(info->path);
                info->url = newuri;
                info->full_name = g_strdup(new_name);
                info->path = g_strdup_printf("/%s", new_name);
                tmp = strchr(new_name, '/');
                if (tmp == NULL)
                    tmp = new_name;
                info->name = g_strdup(tmp);
                g_hash_table_insert(mls->folder_infos, info->url, info);

                reninfo.new = info;
                reninfo.old_base = (char *)old_name;
                
                camel_object_trigger_event(store, "folder_renamed", &reninfo);
            } else {
                g_free(newuri);
                g_warning("Cannot find existing folder '%s' in table?\n", olduri);
            }

            g_free(olduri);
#endif
        }
    }

    g_free(newname);
    g_free(oldname);

    camel_object_unref(real_store);

    free_metainfo(meta);

    g_free(newmeta);
    g_free(oldmeta);
}

static char *
mls_get_name (CamelService *service, gboolean brief)
{
    if (brief)
        return g_strdup("local");

    return g_strdup("Local mail folders");
}

static void
mls_init (MailLocalStore *mls, MailLocalStoreClass *mlsclass)
{
    mls->folder_infos = g_hash_table_new(g_str_hash, g_str_equal);
    mls->folder_info_lock = g_mutex_new();
}

static void
free_info(void *key, void *value, void *data)
{
    CamelFolderInfo *info = value;

    camel_folder_info_free (info);
}

static void
mls_finalise(MailLocalStore *mls)
{
    g_hash_table_foreach(mls->folder_infos, (GHFunc)free_info, NULL);
    g_hash_table_destroy(mls->folder_infos);
    g_mutex_free(mls->folder_info_lock);
}

static void
mls_class_init (CamelObjectClass *camel_object_class)
{
    CamelStoreClass *camel_store_class = CAMEL_STORE_CLASS(camel_object_class);
    CamelServiceClass *camel_service_class = CAMEL_SERVICE_CLASS(camel_object_class);
    
    /* virtual method overload -- the bare minimum */
    camel_service_class->get_name    = mls_get_name;
    camel_store_class->get_folder    = mls_get_folder;
    camel_store_class->delete_folder = mls_delete_folder;
    camel_store_class->rename_folder = mls_rename_folder;

    local_store_parent_class = camel_type_get_global_classfuncs (CAMEL_STORE_TYPE);
}

static CamelType
mail_local_store_get_type (void)
{
    static CamelType mail_local_store_type = CAMEL_INVALID_TYPE;

    if (mail_local_store_type == CAMEL_INVALID_TYPE) {
        mail_local_store_type = camel_type_register (
            CAMEL_STORE_TYPE, "MailLocalStore",
            sizeof (MailLocalStore),
            sizeof (MailLocalStoreClass),
            (CamelObjectClassInitFunc) mls_class_init,
            NULL,
            (CamelObjectInitFunc) mls_init,
            (CamelObjectFinalizeFunc) mls_finalise);
    }

    return mail_local_store_type;
}

static void mail_local_store_add_folder(MailLocalStore *mls, const char *uri, const char *path, const char *name)
{
    CamelFolderInfo *info = NULL;
    CamelURL *url;

    d(printf("Shell adding folder: '%s' path = '%s'\n", uri, path));

    url = camel_url_new(uri, NULL);
    if (url == NULL) {
        g_warning("Shell trying to add invalid folder url: %s", uri);
        return;
    }
    if (url->path == NULL || url->path[0] == 0) {
        g_warning("Shell trying to add invalid folder url: %s", uri);
        camel_url_free(url);
        return;
    }

    LOCAL_STORE_LOCK(mls);

    if (g_hash_table_lookup(mls->folder_infos, uri)) {
        g_warning("Shell trying to add a folder I already have!");
    } else {
        info = g_malloc0(sizeof(*info));
        info->url = g_strdup(uri);
        info->full_name = g_strdup(url->path+1);
        info->name = g_strdup(name);
        info->unread_message_count = -1;
        info->path = g_strdup (path);
        g_hash_table_insert(mls->folder_infos, info->url, info);
    }

    LOCAL_STORE_UNLOCK(mls);

    camel_url_free(url);

    if (info) {
        /* FIXME: should copy info, so we dont get a removed while we're using it? */
        camel_object_trigger_event(mls, "folder_created", info);

        /* this is just so the folder is opened at least once to setup the folder
           counts etc in the display.  Joy eh?   The result is discarded. */
        mail_get_folder (uri, CAMEL_STORE_FOLDER_CREATE, NULL, NULL, mail_thread_queued_slow);
    }
}

struct _search_info {
    const char *path;
    CamelFolderInfo *info;
};

static void
remove_find_path(char *uri, CamelFolderInfo *info, struct _search_info *data)
{
    if (!strcmp(info->path, data->path))
        data->info = info;
}

static void mail_local_store_remove_folder(MailLocalStore *mls, const char *path)
{
    struct _search_info data = { path, NULL };

    d(printf("shell removing folder? '%s'\n", path));

    /* we're keyed on uri, not path, so have to search for it manually */

    LOCAL_STORE_LOCK(mls);
    g_hash_table_foreach(mls->folder_infos, (GHFunc)remove_find_path, &data);
    if (data.info)
        g_hash_table_remove(mls->folder_infos, data.info->url);
    LOCAL_STORE_UNLOCK(mls);

    if (data.info) {
        camel_object_trigger_event(mls, "folder_deleted", data.info);

        g_free(data.info->url);
        g_free(data.info->full_name);
        g_free(data.info->name);
        g_free(data.info);
    }
}

/* ** Local Provider ************************************************************** */

static CamelProvider local_provider = {
    "file", "Local mail", "Local mailbox file", "mail",
    CAMEL_PROVIDER_IS_STORAGE | CAMEL_PROVIDER_IS_EXTERNAL,
    CAMEL_URL_NEED_PATH,
    /* ... */
};

/* There's only one "file:" store. */
static guint
non_hash (gconstpointer key)
{
    return 0;
}

static gint
non_equal (gconstpointer ap, gconstpointer bp)
{
    const CamelURL *a = ap, *b = bp;

    return strcmp(a->protocol, "file") == 0
        && strcmp(a->protocol, b->protocol) == 0;
}

static void
mail_local_provider_init (void)
{
    /* Register with Camel to handle file: URLs */
    local_provider.object_types[CAMEL_PROVIDER_STORE] = MAIL_LOCAL_STORE_TYPE;

    local_provider.url_hash = non_hash;
    local_provider.url_equal = non_equal;
    camel_session_register_provider (session, &local_provider);
}

/* ** Local Storage Listener ****************************************************** */

static void
local_storage_new_folder_cb (EvolutionStorageListener *storage_listener,
                 const char *path,
                 const GNOME_Evolution_Folder *folder,
                 void *data)
{
    d(printf("Local folder new:\n"));
    d(printf(" path = '%s'\n uri = '%s'\n display = '%s'\n",
         path, folder->physicalUri, folder->displayName));

    /* We dont actually add the trash to our local folders list, get_trash is handled
       outside our internal folder list */

    if (strcmp(folder->type, "mail") == 0) {
        mail_local_store_add_folder(global_local_store, folder->physicalUri, path, folder->displayName);
    } else if (strcmp(folder->type, "vtrash") == 0) {
        CamelFolderInfo info;
        CamelURL *url;

        url = camel_url_new(folder->physicalUri, NULL);
        if (url == NULL) {
            g_warning("Shell trying to add invalid folder url: %s", folder->physicalUri);
            return;
        }
        if (url->path == NULL || url->path[0] == 0) {
            g_warning("Shell trying to add invalid folder url: %s", folder->physicalUri);
            camel_url_free(url);
            return;
        }

        memset(&info, 0, sizeof(info));
        info.full_name = CAMEL_VTRASH_NAME;
        info.name = folder->displayName;
        info.url = g_strdup_printf("vtrash:%s", folder->physicalUri);
        info.unread_message_count = 0;
        info.path = (char *)path;

        camel_object_trigger_event(global_local_store, "folder_created", &info);
        g_free(info.url);
        camel_url_free(url);
    }
}


static void
local_storage_removed_folder_cb (EvolutionStorageListener *storage_listener,
                 const char *path,
                 void *data)
{
    d(printf("Local folder remove:\n"));
    d(printf(" path = '%s'\n", path));

    mail_local_store_remove_folder(global_local_store, path);
}

static void
storage_listener_startup (EvolutionShellClient *shellclient)
{
    EvolutionStorageListener *local_storage_listener;
    GNOME_Evolution_StorageListener corba_local_storage_listener;
    GNOME_Evolution_Storage corba_storage;
    CORBA_Environment ev;

    d(printf("---- CALLING STORAGE LISTENER STARTUP ---\n"));

    local_corba_storage = corba_storage = evolution_shell_client_get_local_storage (shellclient);
    if (corba_storage == CORBA_OBJECT_NIL) {
        g_warning ("No local storage available from shell client!");
        return;
    }

    /* setup to record this store's changes */
    mail_note_store((CamelStore *)global_local_store, NULL, local_corba_storage, NULL, NULL);

    local_storage_listener = evolution_storage_listener_new ();
    corba_local_storage_listener = evolution_storage_listener_corba_objref (
        local_storage_listener);

    g_signal_connect(local_storage_listener,
             "new_folder",
             G_CALLBACK (local_storage_new_folder_cb),
             corba_storage);
    g_signal_connect(local_storage_listener,
             "removed_folder",
             G_CALLBACK (local_storage_removed_folder_cb),
             corba_storage);

    CORBA_exception_init (&ev);
    GNOME_Evolution_Storage_addListener (corba_storage,
                         corba_local_storage_listener, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("Cannot add a listener to the Local Storage.");
        CORBA_exception_free (&ev);
        return;
    }
    CORBA_exception_free (&ev);
}

/* ** The rest ******************************************************************** */

void
mail_local_storage_startup (EvolutionShellClient *shellclient, const char *evolution_path)
{
    mail_local_provider_init ();

    global_local_store = MAIL_LOCAL_STORE(camel_session_get_service (session, "file:/", CAMEL_PROVIDER_STORE, NULL));

    if (!global_local_store) {
        g_warning ("No local store!");
        return;
    }

    storage_listener_startup (shellclient);
}

void
mail_local_storage_shutdown (void)
{
    bonobo_object_release_unref (local_corba_storage, NULL);
    local_corba_storage = CORBA_OBJECT_NIL;
}


/*----------------------------------------------------------------------
 * Local folder reconfiguration stuff
 *----------------------------------------------------------------------*/

/*
   open new
   copy old->new
   close old
   rename old oldsave
   rename new old
   open oldsave
   delete oldsave

   close old
   rename oldtmp
   open new
   open oldtmp
   copy oldtmp new
   close oldtmp
   close oldnew

*/

/* we should have our own progress bar for this */

struct _reconfigure_msg {
    struct _mail_msg msg;

    char *uri;
    CamelFolder *folder;

    char *newtype;
    unsigned int index_body:1;
    GtkWidget *frame;
    GtkWidget *apply;
    GtkWidget *cancel;
    GtkWidget *check_index_body;
    GtkOptionMenu *optionlist;

    void (*done)(const char *uri, CamelFolder *folder, void*data);
    void *done_data;
};

/* hash table of folders that the user has a reconfig-folder dialog for */
static GHashTable *reconfigure_folder_hash = NULL;

static char *
reconfigure_folder_describe (struct _mail_msg *mm, int done)
{
    struct _reconfigure_msg *m = (struct _reconfigure_msg *)mm;
    
    return g_strdup_printf (_("Changing folder \"%s\" to \"%s\" format"),
                camel_folder_get_full_name (m->folder),
                m->newtype);
}

static void
reconfigure_folder_reconfigure (struct _mail_msg *mm)
{
    struct _reconfigure_msg *m = (struct _reconfigure_msg *)mm;

    d(printf("reconfiguring folder: %s to type %s\n", m->uri, m->newtype));
    
    mail_local_folder_reconfigure (MAIL_LOCAL_FOLDER (m->folder), m->newtype, m->index_body, &mm->ex);
}

static void
reconfigure_folder_reconfigured (struct _mail_msg *mm)
{
    struct _reconfigure_msg *m = (struct _reconfigure_msg *)mm;
    GtkWidget *dialog;
    /*char *uri;*/
    
    if (camel_exception_is_set (&mm->ex)) {
        dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
                         GTK_BUTTONS_OK, "%s",
                         _("If you can no longer open this mailbox, then\n"
                           "you may need to repair it manually."));
        gtk_dialog_run (GTK_DIALOG (dialog));
    }

    if (m->done)
        m->done(m->uri, m->folder, m->done_data);
}

static void
reconfigure_folder_free (struct _mail_msg *mm)
{
    struct _reconfigure_msg *m = (struct _reconfigure_msg *)mm;

    /* remove this folder from our hash since we are done with it */
    g_hash_table_remove (reconfigure_folder_hash, m->folder);
    if (g_hash_table_size (reconfigure_folder_hash) == 0) {
        /* additional cleanup */
        g_hash_table_destroy (reconfigure_folder_hash);
        reconfigure_folder_hash = NULL;
    }

    if (m->folder)
        camel_object_unref (m->folder);
    g_free(m->uri);
    g_free (m->newtype);
}

static struct _mail_msg_op reconfigure_folder_op = {
    reconfigure_folder_describe,
    reconfigure_folder_reconfigure,
    reconfigure_folder_reconfigured,
    reconfigure_folder_free,
};

static void
reconfigure_response(GtkDialog *dialog, int button, struct _reconfigure_msg *m)
{
    switch(button) {
    case GTK_RESPONSE_OK: {
        GtkWidget *menu, *item;

        menu = gtk_option_menu_get_menu(m->optionlist);
        item = gtk_menu_get_active(GTK_MENU(menu));
        m->newtype = g_strdup(g_object_get_data ((GObject *)item, "type"));
        m->index_body = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m->check_index_body));

        gtk_widget_set_sensitive (m->frame, FALSE);
        gtk_widget_set_sensitive (m->apply, FALSE);
        gtk_widget_set_sensitive (m->cancel, FALSE);
        
        e_thread_put (mail_thread_queued, (EMsg *)m);
        break; }
    case GTK_RESPONSE_CANCEL:
    default:
        if (m->done)
            m->done(m->uri, NULL, m->done_data);
        mail_msg_free ((struct _mail_msg *)m);
        break;
    }

    gtk_widget_destroy((GtkWidget *)dialog);
}

static void
reconfigure_got_folder(char *uri, CamelFolder *folder, void *data)
{
    GladeXML *gui;
    GtkDialog *gd;
    struct _reconfigure_msg *m = data;
    char *title;
    GList *p;
    GtkWidget *menu;
    char *currentformat;
    int index=0, history=0;

    if (folder == NULL
        || !MAIL_IS_LOCAL_FOLDER (folder)) {
        g_warning ("Trying to reconfigure nonexistant folder");
        /* error display ? */
        if (m->done)
            m->done(uri, NULL, m->done_data);
        mail_msg_free((struct _mail_msg *)m);
        return;
    }
    
    if (!reconfigure_folder_hash)
        reconfigure_folder_hash = g_hash_table_new (NULL, NULL);
    
    if ((gd = g_hash_table_lookup (reconfigure_folder_hash, folder))) {
        gdk_window_raise (GTK_WIDGET (gd)->window);
        if (m->done)
            m->done(uri, NULL, m->done_data);
        mail_msg_free((struct _mail_msg *)m);
        return;
    }
    
    gui = glade_xml_new (EVOLUTION_GLADEDIR "/local-config.glade", "dialog_format", NULL);
    gd = (GtkDialog *)glade_xml_get_widget (gui, "dialog_format");
    
    title = g_strdup_printf (_("Reconfigure /%s"),
                 camel_folder_get_full_name (folder));
    gtk_window_set_title (GTK_WINDOW (gd), title);
    g_free (title);

    m->uri = g_strdup(uri);
    m->frame = glade_xml_get_widget (gui, "frame_format");
    m->apply = glade_xml_get_widget (gui, "apply_format");
    m->cancel = glade_xml_get_widget (gui, "cancel_format");
    m->optionlist = (GtkOptionMenu *)glade_xml_get_widget (gui, "option_format");
    m->check_index_body = glade_xml_get_widget (gui, "check_index_body");
    m->newtype = NULL;
    m->folder = folder;
    camel_object_ref(folder);
    
    /* dynamically create the folder type list from camel */
    /* we assume the list is static and never freed */
    currentformat = MAIL_LOCAL_FOLDER (folder)->meta->format;
    p = camel_session_list_providers (session, TRUE);
    menu = gtk_menu_new ();
    while (p) {
        CamelProvider *cp = p->data;
        
        /* we only want local providers */
        if (cp->flags & CAMEL_PROVIDER_IS_LOCAL) {
            GtkWidget *item;
            char *label;
            
            if (!strcmp (cp->protocol, currentformat))
                history = index;
            
            label = g_strdup_printf("%s (%s)", cp->protocol, _(cp->name));
            item = gtk_menu_item_new_with_label (label);
            g_free (label);
            g_object_set_data ((GObject *) item, "type", cp->protocol);
            gtk_widget_show (item);
            gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
            index++;
        }
        p = p->next;
    }
    gtk_option_menu_remove_menu (GTK_OPTION_MENU (m->optionlist));
    gtk_option_menu_set_menu (GTK_OPTION_MENU(m->optionlist), menu);
    gtk_option_menu_set_history(GTK_OPTION_MENU(m->optionlist), history);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m->check_index_body), MAIL_LOCAL_FOLDER (folder)->meta->indexed);

    gtk_label_set_text ((GtkLabel *)glade_xml_get_widget (gui, "label_format"),
                MAIL_LOCAL_FOLDER (folder)->meta->format);
    
    g_signal_connect(gd, "response", G_CALLBACK(reconfigure_response), m);
    g_object_unref(gui);
    
    g_hash_table_insert (reconfigure_folder_hash, (gpointer) folder, (gpointer) gd);
    
    gtk_widget_show((GtkWidget *)gd);
}

void
mail_local_reconfigure_folder(const char *uri, void (*done)(const char *uri, CamelFolder *folder, void *data), void *done_data)
{
    struct _reconfigure_msg *m;

    if (strncmp(uri, "file:", 5) != 0) {
        e_notice (NULL, GTK_MESSAGE_WARNING,
              _("You cannot change the format of a non-local folder."));
        if (done) 
            done(uri, NULL, done_data);
        return;
    }

    m = mail_msg_new (&reconfigure_folder_op, NULL, sizeof (*m));
    m->done = done;
    m->done_data = done_data;

    mail_get_folder(uri, 0, reconfigure_got_folder, m, mail_thread_new);
}