aboutsummaryrefslogblamecommitdiffstats
path: root/mail/em-migrate.c
blob: af484f19178bb1cc2c3b72878bd9616d1643b4ea (plain) (tree)
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



























                                                                           
                   




                      
                  
                  
                  


                    

                               



                                   













































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                  
 









                                                            
 


































































































































































































































































                                                                                                                                        
 
                           






































































































































                                                                                                                  















































                                                                                     

                                                                                                             

                                    





































                                                                                                          
                                                 

                                       
                                   











                                                                                                         














                                                              
                                                                             
























































































                                                                                               







                                                                                      
                                






















                                                                   
                                                                               









                                                                                       



                                               
                                  



                                                                     

                              


                                                                             
                


                                 




                                                                             
                





                                                           
                

                                                      

























































                                                                                                                  

                 











                                                                                                    
                








                                                                                            
                











                                                                                                     
                




































                                                                                                      

                                                

         





































                                                                           
                                                                  
































                                                                                      
             
                                      



































                                                                                          
          
                                  
 
                                                            

 
          
                                                                             





































































                                                                                             
 

                                                                                                     
 




                                  
                                                              




                                                                     
                                                                                           


























                                                                                                 
                                                         





                                            
                                               

                          
                                                

                          
                                                                    

                          

                 






































































                                                                                              
                                 











































                                                                                                        
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2003 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <regex.h>
#include <errno.h>
#include <ctype.h>

#include <gtk/gtk.h>

#include <gconf/gconf-client.h>

#include <camel/camel.h>
#include <camel/camel-session.h>
#include <camel/camel-file-utils.h>

#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>

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

#include "mail-component.h" /* for em_uri_from_camel() */
#include "em-migrate.h"

#define d(x) x


/* upgrade helper functions */

static xmlNodePtr
xml_find_node (xmlNodePtr parent, const char *name)
{
    xmlNodePtr node;
    
    node = parent->children;
    while (node != NULL) {
        if (node->name && !strcmp (node->name, name))
            return node;
        
        node = node->next;
    }
    
    return NULL;
}

static int
upgrade_xml_uris (xmlDocPtr doc, char * (* upgrade_uri) (const char *uri))
{
    xmlNodePtr root, node;
    char *uri, *new;
    
    if (!doc || !(root = xmlDocGetRootElement (doc)))
        return 0;
    
    if (!root->name || strcmp (root->name, "filteroptions") != 0) {
        /* root node is not <filteroptions>, nothing to upgrade */
        return 0;
    }
    
    if (!(node = xml_find_node (root, "ruleset"))) {
        /* no ruleset node, nothing to upgrade */
        return 0;
    }
    
    node = node->children;
    while (node != NULL) {
        if (node->name && !strcmp (node->name, "rule")) {
            xmlNodePtr actionset, part, val, n;
            
            if ((actionset = xml_find_node (node, "actionset"))) {
                /* filters.xml */
                part = actionset->children;
                while (part != NULL) {
                    if (part->name && !strcmp (part->name, "part")) {
                        val = part->children;
                        while (val != NULL) {
                            if (val->name && !strcmp (val->name, "value")) {
                                char *type;
                                
                                type = xmlGetProp (val, "type");
                                if (type && !strcmp (type, "folder")) {
                                    if ((n = xml_find_node (val, "folder"))) {
                                        uri = xmlGetProp (n, "uri");
                                        new = upgrade_uri (uri);
                                        xmlFree (uri);
                                        
                                        xmlSetProp (n, "uri", new);
                                        g_free (new);
                                    }
                                }
                                
                                xmlFree (type);
                            }
                            
                            val = val->next;
                        }
                    }
                    
                    part = part->next;
                }
            } else if ((actionset = xml_find_node (node, "sources"))) {
                /* vfolders.xml */
                n = actionset->children;
                while (n != NULL) {
                    if (n->name && !strcmp (n->name, "folder")) {
                        uri = xmlGetProp (n, "uri");
                        new = upgrade_uri (uri);
                        xmlFree (uri);
                        
                        xmlSetProp (n, "uri", new);
                        g_free (new);
                    }
                    
                    n = n->next;
                }
            }
        }
        
        node = node->next;
    }
    
    return 0;
}

/* 1.0 upgrade functions & data */

/* as much info as we have on a given account */
struct _account_info_1_0 {
    char *name;
    char *uri;
    char *base_uri;
    union {
        struct {
            /* for imap */
            char *namespace;
            char *namespace_full;
            guint32 capabilities;
            GHashTable *folders;
            char dir_sep;
        } imap;
    } u;
};

struct _imap_folder_info_1_0 {
    char *folder;
    /* encoded?  decoded?  canonicalised? */
    char dir_sep;
};

static GHashTable *accounts_1_0 = NULL;
static GHashTable *accounts_name_1_0 = NULL;

static void
imap_folder_info_1_0_free (gpointer key, gpointer value, gpointer user_data)
{
    struct _imap_folder_info_1_0 *fi = value;
    
    g_free (fi->folder);
    g_free (fi);
}

static void
account_info_1_0_free (struct _account_info_1_0 *ai)
{
    g_free (ai->name);
    g_free (ai->uri);
    g_free (ai->base_uri);
    g_free (ai->u.imap.namespace);
    g_free (ai->u.imap.namespace_full);
    g_hash_table_foreach (ai->u.imap.folders, (GHFunc) imap_folder_info_1_0_free, NULL);
    g_hash_table_destroy (ai->u.imap.folders);
    g_free (ai);
}

static void
accounts_1_0_free (gpointer key, gpointer value, gpointer user_data)
{
    account_info_1_0_free (value);
}

static char hexnib[256] = {
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
     0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
    -1,10,11,12,13,14,15,16,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,10,11,12,13,14,15,16,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
};

static char *
hex_decode (const char *val)
{
    const unsigned char *p = (const unsigned char *) val;
    char *o, *res;
    
    o = res = g_malloc (strlen(val)/2 + 1);
    for (p = val; (p[0] && p[1]); p += 2)
        *o++ = (hexnib[p[0]] << 4) | hexnib[p[1]];
    *o = 0;
    
    return res;
}

static char *
url_decode (const char *val)
{
    const unsigned char *p = (const unsigned char *) val;
    char *o, *res, c;
    
    o = res = g_malloc (strlen (val) + 1);
    while (*p) {
        c = *p++;
        if (c == '%'
            && hexnib[p[0]] != -1 && hexnib[p[1]] != -1) {
            *o++ = (hexnib[p[0]] << 4) | hexnib[p[1]];
            p+=2;
        } else
            *o++ = c;
    }
    *o = 0;
    
    return res;
}

static xmlNodePtr
lookup_bconf_path (xmlDocPtr doc, const char *path)
{
    xmlNodePtr root;
    char *val;
    int found;
    
    root = doc->children;
    if (strcmp (root->name, "bonobo-config") != 0) {
        g_warning ("not bonobo-config xml file\n");
        return NULL;
    }
    
    root = root->children;
    while (root) {
        if (!strcmp (root->name, "section")) {
            val = xmlGetProp (root, "path");
            found = val && strcmp (val, path) == 0;
            xmlFree (val);
            if (found)
                break;
        }
        root = root->next;
    }
    
    return root;
}

static xmlNodePtr
bconf_get_entry (xmlNodePtr root, const char *name)
{
    xmlNodePtr node = root->children;
    int found;
    char *val;
    
    while (node) {
        if (!strcmp (node->name, "entry")) {
            val = xmlGetProp (node, "name");
            found = val && strcmp (val, name) == 0;
            xmlFree (val);
            if (found)
                break;
        }
        node = node->next;
    }
    
    return node;
}

static char *
bconf_get_value (xmlNodePtr root, const char *name)
{
    xmlNodePtr node = bconf_get_entry (root, name);
    
    if (node)
        return xmlGetProp (node, "value");
    else
        return NULL;
}

static char *
bconf_get_bool (xmlNodePtr root, const char *name)
{
    char *val, *res;
    
    if ((val = bconf_get_value (root, name))) {
        res = g_strdup (val[0] == '1' ? "true" : "false");
        xmlFree (val);
    } else
        res = NULL;
    
    return res;
}

static char *
bconf_get_long (xmlNodePtr root, const char *name)
{
    char *val, *res;
    
    if ((val = bconf_get_value (root, name))) {
        res = g_strdup (val);
        xmlFree (val);
    } else
        res = NULL;
    
    return res;
}

static char *
bconf_get_string (xmlNodePtr root, const char *name)
{
    char *val, *res;
    
    if ((val = bconf_get_value (root, name))) {
        res = hex_decode (val);
        xmlFree (val);
    } else
        res = NULL;
    
    return res;
}

static char *
get_base_uri (const char *val)
{
    const char *tmp;
    
    tmp = strchr (val, ':');
    if (tmp) {
        tmp++;
        if (strncmp (tmp, "//", 2) == 0)
            tmp += 2;
        tmp = strchr (tmp, '/');
    }
    
    if (tmp)
        return g_strndup (val, tmp-val);
    else
        return g_strdup (val);
}

static char *
upgrade_xml_uris_1_0 (const char *uri)
{
    char *out = NULL;
    
    /* upgrades camel uri's */
    if (strncmp (uri, "imap:", 5) == 0) {
        char *base_uri, dir_sep, *folder, *p;
        struct _account_info_1_0 *ai;
        
        /* add namespace, canonicalise dir_sep to / */
        base_uri = get_base_uri (uri);
        ai = g_hash_table_lookup (accounts_1_0, base_uri);
        
        if (ai == NULL) {
            g_free (base_uri);
            return NULL;
        }
        
        dir_sep = ai->u.imap.dir_sep;
        if (dir_sep == 0) {
            /* no dir_sep listed, try get it from the namespace, if set */
            if (ai->u.imap.namespace != NULL) {
                p = ai->u.imap.namespace;
                while ((dir_sep = *p++)) {
                    if (dir_sep < '0'
                        || (dir_sep > '9' && dir_sep < 'A')
                        || (dir_sep > 'Z' && dir_sep < 'a')
                        || (dir_sep > 'z')) {
                        break;
                    }
                    p++;
                }
            }
            
            /* give up ... */
            if (dir_sep == 0) {
                g_free (base_uri);
                return NULL;
            }
        }
        
        folder = g_strdup (uri + strlen (base_uri) + 1);
        
        /* Add the namespace before the mailbox name, unless the mailbox is INBOX */
        if (ai->u.imap.namespace && strcmp (folder, "INBOX") != 0)
            out = g_strdup_printf ("%s/%s/%s", base_uri, ai->u.imap.namespace, folder);
        else
            out = g_strdup_printf ("%s/%s", base_uri, folder);
        
        p = out;
        while (*p) {
            if (*p == dir_sep)
                *p = '/';
            p++;
        }
        
        g_free (folder);
        g_free (base_uri);
    } else if (strncmp (uri, "exchange:", 9) == 0) {
        char *base_uri, *folder, *p;
        
        /*  exchange://user@host/exchange/ * -> exchange://user@host/personal/ * */
        /*  Any url encoding (%xx) in the folder name is also removed */
        base_uri = get_base_uri (uri);
        uri += strlen (base_uri) + 1;
        if (strncmp (uri, "exchange/", 9) == 0) {
            folder = url_decode (uri + 9);
            p = strchr (folder, '/');
            out = g_strdup_printf ("%s/personal%s", base_uri, p ? p : "/");
            g_free (folder);
        }
    } else if (strncmp (uri, "exchanget:", 10) == 0) {
        /* these should be converted in the accounts table when it is loaded */
        g_warning ("exchanget: uri not converted: '%s'", uri);
    }
    
    return out;
}

static int
em_upgrade_xml_1_0 (xmlDocPtr doc)
{
    return upgrade_xml_uris (doc, upgrade_xml_uris_1_0);
}

static char *
parse_lsub (const char *lsub, char *dir_sep)
{
    static int comp;
    static regex_t pat;
    regmatch_t match[3];
    char *m = "^\\* LSUB \\([^)]*\\) \"?([^\" ]+)\"? \"?(.*)\"?$";
    
    if (!comp) {
        if (regcomp (&pat, m, REG_EXTENDED|REG_ICASE) == -1) {
            g_warning ("reg comp '%s' failed: %s", m, g_strerror (errno));
            return NULL;
        }
        comp = 1;
    }
    
    if (regexec (&pat, lsub, 3, match, 0) == 0) {
        if (match[1].rm_so != -1 && match[2].rm_so != -1) {
            if (dir_sep)
                *dir_sep = (match[1].rm_eo - match[1].rm_so == 1) ? lsub[match[1].rm_so] : 0;
            return g_strndup (lsub + match[2].rm_so, match[2].rm_eo - match[2].rm_so);
        }
    }
    
    return NULL;
}

static int
read_imap_storeinfo (struct _account_info_1_0 *si)
{
    FILE *storeinfo;
    guint32 tmp;
    char *buf, *folder, dir_sep, *path, *name, *p;
    struct _imap_folder_info_1_0 *fi;
    
    si->u.imap.folders = g_hash_table_new (g_str_hash, g_str_equal);
    
    /* get details from uri first */
    name = strstr (si->uri, ";override_namespace");
    if (name) {
        name = strstr (si->uri, ";namespace=");
        if (name) {
            char *end;
            
            name += strlen (";namespace=");
            if (*name == '\"') {
                name++;
                end = strchr (name, '\"');
            } else {
                end = strchr (name, ';');
            }
            
            if (end) {
                /* try get the dir_sep from the namespace */
                si->u.imap.namespace = g_strndup (name, end-name);
                
                p = si->u.imap.namespace;
                while ((dir_sep = *p++)) {
                    if (dir_sep < '0'
                        || (dir_sep > '9' && dir_sep < 'A')
                        || (dir_sep > 'Z' && dir_sep < 'a')
                        || (dir_sep > 'z')) {
                        si->u.imap.dir_sep = dir_sep;
                        break;
                    }
                    p++;
                }
            }
        }
    }
    
    /* now load storeinfo if it exists */
    path = g_build_filename (g_get_home_dir (), "evolution", "mail", "imap", si->base_uri + 7, "storeinfo", NULL);
    storeinfo = fopen (path, "r");
    g_free (path);
    if (storeinfo == NULL) {
        g_warning ("could not find imap store info '%s'", path);
        return -1;
    }
    
    /* ignore version */
    camel_file_util_decode_uint32 (storeinfo, &tmp);
    camel_file_util_decode_uint32 (storeinfo, &si->u.imap.capabilities);
    g_free (si->u.imap.namespace);
    camel_file_util_decode_string (storeinfo, &si->u.imap.namespace);
    camel_file_util_decode_uint32 (storeinfo, &tmp);
    si->u.imap.dir_sep = tmp;
    /* strip trailing dir_sep or / */
    if (si->u.imap.namespace
        && (si->u.imap.namespace[strlen (si->u.imap.namespace) - 1] == si->u.imap.dir_sep
        || si->u.imap.namespace[strlen (si->u.imap.namespace) - 1] == '/')) {
        si->u.imap.namespace[strlen (si->u.imap.namespace) - 1] = 0;
    }
    
    d(printf ("namespace '%s' dir_sep '%c'\n", si->u.imap.namespace, si->u.imap.dir_sep ? si->u.imap.dir_sep : '?'));
    
    while (camel_file_util_decode_string (storeinfo, &buf) == 0) {
        folder = parse_lsub (buf, &dir_sep);
        if (folder) {
            fi = g_new0 (struct _imap_folder_info_1_0, 1);
            fi->folder = folder;
            fi->dir_sep = dir_sep;
#if d(!)0
            printf (" add folder '%s' ", folder);
            if (dir_sep)
                printf ("'%c'\n", dir_sep);
            else
                printf ("NIL\n");
#endif
            g_hash_table_insert (si->u.imap.folders, fi->folder, fi);
        } else {
            g_warning ("Could not parse LIST result '%s'\n", buf);
        }
    }
    
    fclose (storeinfo);
    
    return 0;
}

static int
load_accounts_1_0 (xmlDocPtr doc)
{
    xmlNodePtr source;
    char *val, *tmp;
    int count = 0, i;
    char key[32];
    
    if (!(source = lookup_bconf_path (doc, "/Mail/Accounts")))
        return 0;
    
    if ((val = bconf_get_value (source, "num"))) {
        count = atoi (val);
        xmlFree (val);
    }
    
    /* load account upgrade info for each account */
    for (i = 0; i < count; i++) {
        struct _account_info_1_0 *ai;
        char *rawuri;
        
        sprintf (key, "source_url_%d", i);
        if (!(rawuri = bconf_get_value (source, key)))
            continue;
        
        ai = g_malloc0 (sizeof (struct _account_info_1_0));
        ai->uri = hex_decode (rawuri);
        ai->base_uri = get_base_uri (ai->uri);
        sprintf (key, "account_name_%d", i);
        ai->name = bconf_get_string (source, key);
        
        d(printf("load account '%s'\n", ai->uri));
        
        if (!strncmp (ai->uri, "imap:", 5)) {
            read_imap_storeinfo (ai);
        } else if (!strncmp (ai->uri, "exchange:", 9)) {
            xmlNodePtr node;
            
            d(printf (" upgrade exchange account\n"));
            /* small hack, poke the source_url into the transport_url for exchanget: transports
               - this will be picked up later in the conversion */
            sprintf (key, "transport_url_%d", i);
            node = bconf_get_entry (source, key);
            if (node && (val = xmlGetProp (node, "value"))) {
                tmp = hex_decode (val);
                xmlFree (val);
                if (strncmp (tmp, "exchanget:", 10) == 0)
                    xmlSetProp (node, "value", rawuri);
                g_free (tmp);
            } else {
                d(printf (" couldn't find transport uri?\n"));
            }
        }
        xmlFree (rawuri);
        
        g_hash_table_insert (accounts_1_0, ai->base_uri, ai);
        if (ai->name)
            g_hash_table_insert (accounts_name_1_0, ai->name, ai);
    }
    
    return 0;
}

static int
em_migrate_1_0 (const char *evolution_dir, xmlDocPtr config_xmldb, xmlDocPtr filters, xmlDocPtr vfolders, CamelException *ex)
{
    accounts_1_0 = g_hash_table_new (g_str_hash, g_str_equal);
    accounts_name_1_0 = g_hash_table_new (g_str_hash, g_str_equal); 
    load_accounts_1_0 (config_xmldb);
    
    em_upgrade_xml_1_0 (filters);
    em_upgrade_xml_1_0 (vfolders);
    
    g_hash_table_foreach (accounts_1_0, (GHFunc) accounts_1_0_free, NULL);
    g_hash_table_destroy (accounts_1_0);
    g_hash_table_destroy (accounts_name_1_0);
    
    return 0;
}


/* 1.2 upgrade functions */

static int
is_xml1encoded (const char *txt)
{
    const unsigned char *p;
    int isxml1 = FALSE;
    int is8bit = FALSE;
    
    p = (const unsigned char *)txt;
    while (*p) {
        if (p[0] == '\\' && p[1] == 'U' && p[2] == '+'
            && isxdigit (p[3]) && isxdigit (p[4]) && isxdigit (p[5]) && isxdigit (p[6])
            && p[7] == '\\') {
            isxml1 = TRUE;
            p+=7;
        } else if (p[0] >= 0x80)
            is8bit = TRUE;
        p++;
    }
    
    /* check for invalid utf8 that needs cleaning */
    if (is8bit && !isxml1)
        isxml1 = !g_utf8_validate (txt, -1, NULL);
    
    return isxml1;
}

static char *
decode_xml1 (const char *txt)
{
    GString *out = g_string_new ("");
    const unsigned char *p;
    char *res;
    
    /* convert:
       \U+XXXX\ -> utf8
       8 bit characters -> utf8 (iso-8859-1) */
    
    p = (const unsigned char *) txt;
    while (*p) {
        if (p[0] > 0x80
            || (p[0] == '\\' && p[1] == 'U' && p[2] == '+'
            && isxdigit (p[3]) && isxdigit (p[4]) && isxdigit (p[5]) && isxdigit (p[6])
            && p[7] == '\\')) {
            char utf8[8];
            gunichar u;
            
            if (p[0] == '\\') {
                memcpy (utf8, p + 3, 4);
                utf8[4] = 0;
                u = strtoul (utf8, NULL, 16);
                p+=7;
            } else
                u = p[0];
            utf8[g_unichar_to_utf8 (u, utf8)] = 0;
            g_string_append (out, utf8);
        } else {
            g_string_append_c (out, *p);
        }
        p++;
    }
    
    res = out->str;
    g_string_free (out, FALSE);
    
    return res;
}

static char *
utf8_reencode (const char *txt)
{
    GString *out = g_string_new ("");
    const unsigned char *p;
    char *res;
    
    /* convert:
        libxml1  8 bit utf8 converted to xml entities byte-by-byte chars -> utf8 */
    
    p =  (const unsigned char *) txt;
    
    while (*p) {
        g_string_append_c (out,(char) g_utf8_get_char (p));
        p = g_utf8_next_char (p);
    }
    
    res = out->str;
    if (g_utf8_validate (res, -1, NULL)) {
        g_string_free (out, FALSE);
        return res;
    } else {
        g_string_free (out, TRUE);
        return g_strdup (txt);
    }
}

static int
upgrade_xml_1_2_rec (xmlNodePtr node)
{
    const char *value_tags[] = { "string", "address", "regex", "file", "command", NULL };
    const char *rule_tags[] = { "title", NULL };
    const char *item_props[] = { "name", NULL };
    struct {
        const char *name;
        const char **tags;
        const char **props;
    } tags[] = {
        { "value", value_tags, NULL },
        { "rule", rule_tags, NULL },
        { "item", NULL, item_props },
        { 0 },
    };
    int changed = 0;
    xmlNodePtr work;
    int i,j;
    char *txt, *tmp;
    
    /* upgrades the content of a node, if the node has a specific parent/node name */
    
    for (i = 0; tags[i].name; i++) {
        if (!strcmp (node->name, tags[i].name)) {
            if (tags[i].tags != NULL) {
                work = node->children;
                while (work) {
                    for (j = 0; tags[i].tags[j]; j++) {
                        if (!strcmp (work->name, tags[i].tags[j])) {
                            txt = xmlNodeGetContent (work);
                            if (is_xml1encoded (txt)) {
                                tmp = decode_xml1 (txt);
                                d(printf ("upgrading xml node %s/%s '%s' -> '%s'\n",
                                      tags[i].name, tags[i].tags[j], txt, tmp));
                                xmlNodeSetContent (work, tmp);
                                changed = 1;
                                g_free (tmp);
                            }
                            xmlFree (txt);
                        }
                    }
                    work = work->next;
                }
                break;
            }
            
            if (tags[i].props != NULL) {
                for (j = 0; tags[i].props[j]; j++) {
                    txt = xmlGetProp (node, tags[i].props[j]);
                    tmp = utf8_reencode (txt);
                    d(printf ("upgrading xml property %s on node %s '%s' -> '%s'\n",
                          tags[i].props[j], tags[i].name, txt, tmp));
                    xmlSetProp (node, tags[i].props[j], tmp);
                    changed = 1;
                    g_free (tmp);
                    xmlFree (txt);
                }
            }
        }
    }
    
    node = node->children;
    while (node) {
        changed |= upgrade_xml_1_2_rec (node);
        node = node->next;
    }
    
    return changed;
}

static int
em_upgrade_xml_1_2 (xmlDocPtr doc)
{
    xmlNodePtr root;
    
    if (!doc || !(root = xmlDocGetRootElement (doc)))
        return 0;
    
    return upgrade_xml_1_2_rec (root);
}

/* ********************************************************************** */
/*  Tables for converting flat bonobo conf -> gconf xml blob          */
/* ********************************************************************** */

/* for remapping bonobo-conf account data into the new xml blob format */
/* These are used in build_xml, and order must match the lookup_table */
enum _map_t {
    MAP_END = 0,        /* end of line*/
    MAP_BOOL,       /* bool -> prop of name 'to' value true or false */
    MAP_LONG,       /* long -> prop of name 'to' value a long */
    MAP_STRING,     /* string -> prop of name 'to' */
    MAP_ENUM,       /* long/bool -> prop of name 'to', with the value indexed into the child map table's from field */
    MAP_CHILD,      /* a new child of name 'to' */
    MAP_MASK = 0x3f,
    MAP_CONTENT = 0x80, /* if set, create a new node of name 'to' instead of a property */
};

struct _map_table {
    char *from;
    char *to;
    int type;
    struct _map_table *child;
};

/* Mail/Accounts/ * */
struct _map_table cc_map[] = {
    { "account_always_cc_%i", "always", MAP_BOOL },
    { "account_always_cc_addrs_%i", "recipients", MAP_STRING|MAP_CONTENT },
    { NULL },
};

struct _map_table bcc_map[] = {
    { "account_always_cc_%i", "always", MAP_BOOL },
    { "account_always_bcc_addrs_%i", "recipients", MAP_STRING|MAP_CONTENT },
    { NULL },
};

struct _map_table pgp_map[] = {
    { "account_pgp_encrypt_to_self_%i", "encrypt-to-self", MAP_BOOL },
    { "account_pgp_always_trust_%i", "always-trust", MAP_BOOL },
    { "account_pgp_always_sign_%i", "always-sign", MAP_BOOL },
    { "account_pgp_no_imip_sign_%i", "no-imip-sign", MAP_BOOL },
    { "account_pgp_key_%i", "key-id", MAP_STRING|MAP_CONTENT },
    { NULL },
};

struct _map_table smime_map[] = {
    { "account_smime_encrypt_to_self_%i", "encrypt-to-self", MAP_BOOL },
    { "account_smime_always_sign_%i", "always-sign", MAP_BOOL },
    { "account_smime_key_%i", "key-id", MAP_STRING|MAP_CONTENT },
    { NULL },
};

struct _map_table identity_sig_map[] = {
    { "identity_autogenerated_signature_%i", "auto", MAP_BOOL },
    { "identity_def_signature_%i", "default", MAP_LONG },
    { NULL },
};

struct _map_table identity_map[] = {
    { "identity_name_%i", "name", MAP_STRING|MAP_CONTENT },
    { "identity_address_%i", "addr-spec", MAP_STRING|MAP_CONTENT },
    { "identity_reply_to_%i", "reply-to", MAP_STRING|MAP_CONTENT },
    { "identity_organization_%i", "organization", MAP_STRING|MAP_CONTENT },
    { NULL, "signature", MAP_CHILD, identity_sig_map },
    { NULL },
};

struct _map_table source_map[] = {
    { "source_save_passwd_%i", "save-passwd", MAP_BOOL },
    { "source_keep_on_server_%i", "keep-on-server", MAP_BOOL },
    { "source_auto_check_%i", "auto-check", MAP_BOOL },
    { "source_auto_check_time_%i", "auto-check-timeout", MAP_LONG },
    { "source_url_%i", "url", MAP_STRING|MAP_CONTENT },
    { NULL },
};

struct _map_table transport_map[] = {
    { "transport_save_passwd_%i", "save-passwd", MAP_BOOL },
    { "transport_url_%i", "url", MAP_STRING|MAP_CONTENT },
    { NULL },
};

struct _map_table account_map[] = {
    { "account_name_%i", "name", MAP_STRING },
    { "source_enabled_%i", "enabled", MAP_BOOL },
    { NULL, "identity", MAP_CHILD, identity_map },
    { NULL, "source", MAP_CHILD, source_map },
    { NULL, "transport", MAP_CHILD, transport_map },
    { "account_drafts_folder_uri_%i", "drafts-folder", MAP_STRING|MAP_CONTENT },
    { "account_sent_folder_uri_%i", "sent-folder", MAP_STRING|MAP_CONTENT },
    { NULL, "auto-cc", MAP_CHILD, cc_map },
    { NULL, "auto-bcc", MAP_CHILD, bcc_map },
    { NULL, "pgp", MAP_CHILD, pgp_map },
    { NULL, "smime", MAP_CHILD, smime_map },
    { NULL },
};

/* /Mail/Signatures/ * */
struct _map_table signature_format_map[] = {
    { "text/plain", },
    { "text/html", },
    { NULL }
};

struct _map_table signature_map[] = {
    { "name_%i", "name", MAP_STRING },
    { "html_%i", "format", MAP_ENUM, signature_format_map },
    { "filename_%i", "filename", MAP_STRING|MAP_CONTENT },
    { "script_%i", "script", MAP_STRING|MAP_CONTENT },
    { NULL },
};


static char *
get_name (const char *in, int index)
{
    GString *out = g_string_new ("");
    char c, *res;
    
    while ((c = *in++)) {
        if (c == '%') {
            c = *in++;
            switch (c) {
            case '%':
                g_string_append_c (out, '%');
                break;
            case 'i':
                g_string_append_printf (out, "%d", index);
                break;
            }
        } else {
            g_string_append_c (out, c);
        }
    }
    
    res = out->str;
    g_string_free (out, FALSE);
    
    return res;
}

static char *
bconf_lookup_bool (xmlNodePtr source, const char *name, struct _map_table *map)
{
    return bconf_get_bool (source, name);
}

static char *
bconf_lookup_long (xmlNodePtr source, const char *name, struct _map_table *map)
{
    return bconf_get_long (source, name);
}

static char *
bconf_lookup_string (xmlNodePtr source, const char *name, struct _map_table *map)
{
    return bconf_get_string (source, name);
}

static char *
bconf_lookup_enum (xmlNodePtr source, const char *name, struct _map_table *map)
{
    char *val;
    int index = 0, i;
    
    if ((val = bconf_get_value (source, name))) {
        index = atoi (val);
        xmlFree (val);
    }
    
    for (i = 0; map->child[i].from; i++)
        if (i == index)
            return g_strdup (map->child[i].from);
    
    return NULL;
}

typedef char * (*bconf_lookup_func) (xmlNodePtr, const char *, struct _map_table *);

static void
build_xml (xmlNodePtr root, struct _map_table *map, int index, xmlNodePtr source)
{
    bconf_lookup_func lookup_table[] = { bconf_lookup_bool, bconf_lookup_long, bconf_lookup_string, bconf_lookup_enum };
    char *name, *value;
    xmlNodePtr node;
    
    while (map->type != MAP_END) {
        if ((map->type & MAP_MASK) == MAP_CHILD) {
            node = xmlNewChild (root, NULL, map->to, NULL);
            build_xml (node, map->child, index, source);
        } else {
            name = get_name (map->from, index);
            value = lookup_table[(map->type & MAP_MASK) - 1] (source, name, map);
            
            d(printf ("key '%s=%s' -> ", name, value));
            
            if (map->type & MAP_CONTENT) {
                if (value && value[0])
                    xmlNewTextChild (root, NULL, map->to, value);
            } else {
                xmlSetProp (root, map->to, value);
            }
            g_free (value);
            g_free (name);
        }
        map++;
    }
}

static int
convert_xml_blob (GConfClient *gconf, xmlDocPtr doc, struct _map_table *map, const char *path,
          const char *outpath, const char *name, const char *idparam)
{
    xmlNodePtr source;
    int count = 0, i;
    GSList *list, *l;
    char *val;
    
    source = lookup_bconf_path (doc, path);
    if (source) {
        list = NULL;
        if ((val = bconf_get_value (source, "num"))) {
            count = atoi (val);
            xmlFree (val);
        }
        
        d(printf("Found %d blobs at %s\n", count, path));
        
        for (i = 0; i < count; i++) {
            xmlDocPtr docout;
            xmlChar *xmlbuf;
            int n;
            xmlNodePtr root;
            
            docout = xmlNewDoc ("1.0");
            root = xmlNewDocNode (docout, NULL, name, NULL);
            xmlDocSetRootElement (docout, root);
            
            /* This could be set with a MAP_UID type ... */
            if (idparam) {
                char buf[16];
                
                sprintf (buf, "%d", i);
                xmlSetProp (root, idparam, buf);
            }
            
            build_xml (root, map, i, source);
            
            xmlDocDumpMemory (docout, &xmlbuf, &n);
            xmlFreeDoc (docout);
            
            list = g_slist_append (list, xmlbuf);
        }
        
        gconf_client_set_list (gconf, outpath, GCONF_VALUE_STRING, list, NULL);
        while (list) {
            l = list->next;
            xmlFree (list->data);
            g_slist_free_1 (list);
            list = l;
        }
    } else {
        g_warning ("could not find '%s' in old config database, skipping", path);
    }
    
    return 0;
}

/* ********************************************************************** */
/*  Tables for bonobo conf -> gconf conversion                */
/* ********************************************************************** */

/* order important here, used to index a few tables below */
enum {
    BMAP_BOOL,
    BMAP_BOOLNOT,
    BMAP_INT,
    BMAP_STRING,
    BMAP_SIMPLESTRING,  /* a non-encoded string */
    BMAP_COLOUR,
    BMAP_MASK = 0x7f,
};

struct _gconf_map {
    char *from;
    char *to;
    int type;
};

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

static struct _gconf_map mail_accounts_map[] = {
    /* /Mail/Accounts - most entries are processed via the xml blob routine */
    /* This also works because the initial uid mapping is 1:1 with the list order */
    { "default_account", "mail/default_account", BMAP_SIMPLESTRING },
    { 0 },
};

static struct _gconf_map mail_display_map[] = {
    /* /Mail/Display */
    { "thread_list", "mail/display/thread_list", BMAP_BOOL },
    { "thread_subject", "mail/display/thread_subject", BMAP_BOOL },
    { "hide_deleted", "mail/display/show_deleted", BMAP_BOOLNOT },
    { "preview_pane", "mail/display/show_preview", BMAP_BOOL },
    { "paned_size", "mail/display/paned_size", BMAP_INT },
    { "seen_timeout", "mail/display/mark_seen_timeout", BMAP_INT },
    { "do_seen_timeout", "mail/display/mark_seen", BMAP_BOOL },
    { "http_images", "mail/display/load_http_images", BMAP_INT },
    { "citation_highlight", "mail/display/mark_citations", BMAP_BOOL },
    { "citation_color", "mail/display/citation_colour", BMAP_COLOUR },
    { "x_mailer_display_style", "mail/display/xmailer_mask", BMAP_INT },
    { 0 },
};

static struct _gconf_map mail_format_map[] = {
    /* /Mail/Format */
    { "message_display_style", "mail/display/message_style", BMAP_INT },
    { "send_html", "mail/composer/send_html", BMAP_BOOL },
    { "default_reply_style", "mail/format/reply_style", BMAP_INT },
    { "default_forward_style", "mail/format/forward_style", BMAP_INT },
    { "default_charset", "mail/composer/charset", BMAP_STRING },
    { "confirm_unwanted_html", "mail/prompts/unwanted_html", BMAP_BOOL },
    { 0 },
};

static struct _gconf_map mail_trash_map[] = {
    /* /Mail/Trash */
    { "empty_on_exit", "mail/trash/empty_on_exit", BMAP_BOOL },
    { 0 },
};

static struct _gconf_map mail_prompts_map[] = {
    /* /Mail/Prompts */
    { "confirm_expunge", "mail/prompts/expunge", BMAP_BOOL },
    { "empty_subject", "mail/prompts/empty_subject", BMAP_BOOL },
    { "only_bcc", "mail/prompts/only_bcc", BMAP_BOOL },
    { 0 }
};

static struct _gconf_map mail_filters_map[] = {
    /* /Mail/Filters */
    { "log", "mail/filters/log", BMAP_BOOL },
    { "log_path", "mail/filters/logfile", BMAP_STRING },
    { 0 }
};

static struct _gconf_map mail_notify_map[] = {
    /* /Mail/Notify */
    { "new_mail_notification", "mail/notify/type", BMAP_INT },
    { "new_mail_notification_sound_file", "mail/notify/sound", BMAP_STRING },
    { 0 }
};

static struct _gconf_map mail_filesel_map[] = {
    /* /Mail/Filesel */
    { "last_filesel_dir", "mail/save_dir", BMAP_STRING },
    { 0 }
};

static struct _gconf_map mail_composer_map[] = {
    /* /Mail/Composer */
    { "ViewFrom", "mail/composer/view/From", BMAP_BOOL },
    { "ViewReplyTo", "mail/composer/view/ReplyTo", BMAP_BOOL },
    { "ViewCC", "mail/composer/view/Cc", BMAP_BOOL },
    { "ViewBCC", "mail/composer/view/Bcc", BMAP_BOOL },
    { "ViewSubject", "mail/composer/view/Subject", BMAP_BOOL },
    { 0 },
};

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

static struct _gconf_map importer_elm_map[] = {
    /* /Importer/Elm */
    { "mail", "importer/elm/mail", BMAP_BOOL },
    { "mail-imported", "importer/elm/mail-imported", BMAP_BOOL },
    { 0 },
};

static struct _gconf_map importer_pine_map[] = {
    /* /Importer/Pine */
    { "mail", "importer/elm/mail", BMAP_BOOL },
    { "address", "importer/elm/address", BMAP_BOOL },
    { 0 },
};

static struct _gconf_map importer_netscape_map[] = {
    /* /Importer/Netscape */
    { "mail", "importer/netscape/mail", BMAP_BOOL },
    { "settings", "importer/netscape/settings", BMAP_BOOL },
    { "filters", "importer/netscape/filters", BMAP_BOOL },
    { 0 },
};

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

static struct {
    char *root;
    struct _gconf_map *map;
} gconf_remap_list[] = {
    { "/Mail/Accounts", mail_accounts_map },
    { "/Mail/Display", mail_display_map },
    { "/Mail/Format", mail_format_map },
    { "/Mail/Trash", mail_trash_map },
    { "/Mail/Prompts", mail_prompts_map },
    { "/Mail/Filters", mail_filters_map },
    { "/Mail/Notify", mail_notify_map },
    { "/Mail/Filesel", mail_filesel_map },
    { "/Mail/Composer", mail_composer_map },
    
    { "/Importer/Elm", importer_elm_map },
    { "/Importer/Pine", importer_pine_map },
    { "/Importer/Netscape", importer_netscape_map },
    
    { 0 },
};

struct {
    char *label;
    char *colour;
} label_default[5] = {
    { N_("Important"), "#ff0000" },  /* red */
    { N_("Work"),      "#ff8c00" },  /* orange */
    { N_("Personal"),  "#008b00" },  /* forest green */
    { N_("To Do"),     "#0000ff" },  /* blue */
    { N_("Later"),     "#8b008b" }   /* magenta */
};

/* remaps mail config from bconf to gconf */
static int
import_bonobo_config (xmlDocPtr config_xmldb, GConfClient *gconf)
{
    xmlNodePtr source;
    struct _gconf_map *map;
    char *path, *val, *tmp;
    GSList *list, *l;
    char buf[32];
    int i, j;
    
    /* process all flat config */
    for (i = 0; gconf_remap_list[i].root; i++) {
        d(printf ("Path: %s\n", gconf_remap_list[i].root));
        if (!(source = lookup_bconf_path (config_xmldb, gconf_remap_list[i].root)))
            continue;
        
        map = gconf_remap_list[i].map;
        for (j = 0; map[j].from; j++) {
            if (!(val = bconf_get_value (source, map[j].from)))
                continue;
            
            d(printf (" %s = '%s' -> %s [%d]\n",
                  map[j].from,
                  val == NULL ? "(null)" : val,
                  map[j].to,
                  map[j].type));
            
            path = g_strdup_printf ("/apps/evolution/%s", map[j].to);
            switch (map[j].type) {
            case BMAP_BOOL:
                gconf_client_set_bool (gconf, path, atoi (val), NULL);
                break;
            case BMAP_BOOLNOT:
                gconf_client_set_bool (gconf, path, !atoi (val), NULL);
                break;
            case BMAP_INT:
                gconf_client_set_int (gconf, path, atoi (val), NULL);
                break;
            case BMAP_STRING:
                tmp = hex_decode (val);
                gconf_client_set_string (gconf, path, tmp, NULL);
                g_free(tmp);
                break;
            case BMAP_SIMPLESTRING:
                gconf_client_set_string (gconf, path, val, NULL);
                break;
            case BMAP_COLOUR:
                sprintf (buf, "#%06x", atoi (val) & 0xffffff);
                gconf_client_set_string (gconf, path, buf, NULL);
                break;
            }
            
            /* FIXME: handle errors */
            g_free (path);
            xmlFree (val);
        }
    }
    
    /* Labels:
      label string + label colour as integer
       -> label string:# colour as hex */
    source = lookup_bconf_path (config_xmldb, "/Mail/Labels");
    if (source) {
        list = NULL;
        for (i = 0; i < 5; i++) {
            char labx[16], colx[16];
            char *lab, *col;
            
            sprintf (labx, "label_%d", i);
            sprintf (colx, "color_%d", i);
            lab = bconf_get_string (source, labx);
            if ((col = bconf_get_value (source, colx))) {
                sprintf (colx, "#%06x", atoi (col) & 0xffffff);
                xmlFree (col);
            } else
                strcpy (colx, label_default[i].colour);
            val = g_strdup_printf ("%s:%s", lab ? lab : label_default[i].label, colx);
            list = g_slist_append (list, val);
            g_free (lab);
        }
        
        gconf_client_set_list (gconf, "/apps/evolution/mail/labels", GCONF_VALUE_STRING, list, NULL);
        while (list) {
            l = list->next;
            g_free (list->data);
            g_slist_free_1 (list);
            list = l;
        }
    } else {
        g_warning ("could not find /Mail/Labels in old config database, skipping");
    }
    
    /* Accounts: The flat bonobo-config structure is remapped to a list of xml blobs.  Upgrades as necessary */
    convert_xml_blob (gconf, config_xmldb, account_map, "/Mail/Accounts", "/apps/evolution/mail/accounts", "account", "uid");
    /* Same for signatures */
    convert_xml_blob (gconf, config_xmldb, signature_map, "/Mail/Signatures", "/apps/evolution/mail/signatures", "signature", NULL);
    
    return 0;
}

static int
em_migrate_1_2 (const char *evolution_dir, xmlDocPtr config_xmldb, xmlDocPtr filters, xmlDocPtr vfolders, CamelException *ex)
{
    GConfClient *gconf;
    
    gconf = gconf_client_get_default ();
    import_bonobo_config (config_xmldb, gconf);
    g_object_unref (gconf);
    
    em_upgrade_xml_1_2 (filters);
    em_upgrade_xml_1_2 (vfolders);
    
    return 0;
}

/* 1.4 upgrade functions */

#define EM_MIGRATE_SESSION_TYPE     (em_migrate_session_get_type ())
#define EM_MIGRATE_SESSION(obj)     (CAMEL_CHECK_CAST((obj), EM_MIGRATE_SESSION_TYPE, EMMigrateSession))
#define EM_MIGRATE_SESSION_CLASS(k) (CAMEL_CHECK_CLASS_CAST ((k), EM_MIGRATE_SESSION_TYPE, EMMigrateSessionClass))
#define EM_MIGRATE_IS_SESSION(o)    (CAMEL_CHECK_TYPE((o), EM_MIGRATE_SESSION_TYPE))

typedef struct _EMMigrateSession {
    CamelSession parent_object;
    
    CamelStore *store;   /* new folder tree store */
    char *srcdir;        /* old folder tree path */
} EMMigrateSession;

typedef struct _EMMigrateSessionClass {
    CamelSessionClass parent_class;
    
} EMMigrateSessionClass;

static CamelType em_migrate_session_get_type (void);
static CamelSession *em_migrate_session_new (const char *path);

static void
class_init (EMMigrateSessionClass *klass)
{
    ;
}

static CamelType
em_migrate_session_get_type (void)
{
    static CamelType type = CAMEL_INVALID_TYPE;
    
    if (type == CAMEL_INVALID_TYPE) {
        type = camel_type_register (
            camel_session_get_type (),
            "EMMigrateSession",
            sizeof (EMMigrateSession),
            sizeof (EMMigrateSessionClass),
            (CamelObjectClassInitFunc) class_init,
            NULL,
            NULL,
            NULL);
    }
    
    return type;
}

static CamelSession *
em_migrate_session_new (const char *path)
{
    CamelSession *session;
    
    session = CAMEL_SESSION (camel_object_new (EM_MIGRATE_SESSION_TYPE));
    
    camel_session_construct (session, path);
    
    return session;
}


static GtkWidget *window;
static GtkLabel *label;
static GtkProgressBar *progress;

static void
em_migrate_setup_progress_dialog (void)
{
    GtkWidget *vbox, *hbox, *w;
    
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title ((GtkWindow *) window, _("Migrating..."));
    gtk_window_set_modal ((GtkWindow *) window, TRUE);
    gtk_container_set_border_width ((GtkContainer *) window, 6);
    
    vbox = gtk_vbox_new (FALSE, 6);
    gtk_widget_show (vbox);
    gtk_container_add ((GtkContainer *) window, vbox);
    
    w = gtk_label_new (_("The location and hierarchy of the Evolution mailbox "
                 "folders has changed since Evolution 1.x.\n\nPlease be "
                 "patient while Evolution migrates your folders..."));
    gtk_label_set_line_wrap ((GtkLabel *) w, TRUE);
    gtk_widget_show (w);
    gtk_box_pack_start_defaults ((GtkBox *) vbox, w);
    
    hbox = gtk_hbox_new (FALSE, 6);
    gtk_widget_show (hbox);
    gtk_box_pack_start_defaults ((GtkBox *) vbox, hbox);
    
    label = (GtkLabel *) gtk_label_new ("");
    gtk_widget_show ((GtkWidget *) label);
    gtk_box_pack_start_defaults ((GtkBox *) hbox, (GtkWidget *) label);
    
    progress = (GtkProgressBar *) gtk_progress_bar_new ();
    gtk_widget_show ((GtkWidget *) progress);
    gtk_box_pack_start_defaults ((GtkBox *) hbox, (GtkWidget *) progress);
    
    gtk_widget_show (window);
}

static void
em_migrate_close_progress_dialog (void)
{
    gtk_widget_destroy ((GtkWidget *) window);
}

static void
em_migrate_set_folder_name (const char *folder_name)
{
    char *text;
    
    text = g_strdup_printf (_("Migrating `%s':"), folder_name);
    gtk_label_set_text (label, text);
    g_free (text);
    
    gtk_progress_bar_set_fraction (progress, 0.0);
    
    while (gtk_events_pending ())
        gtk_main_iteration ();
}

static void
em_migrate_set_progress (double percent)
{
    char text[5];
    
    snprintf (text, sizeof (text), "%d%%", (int) (percent * 100.0f));
    
    gtk_progress_bar_set_fraction (progress, percent);
    gtk_progress_bar_set_text (progress, text);
    
    while (gtk_events_pending ())
        gtk_main_iteration ();
}

static gboolean
is_mail_folder (const char *metadata)
{
    xmlNodePtr node;
    xmlDocPtr doc;
    char *type;
    
    if (!(doc = xmlParseFile (metadata))) {
        g_warning ("Cannot parse `%s'", metadata);
        return FALSE;
    }
    
    if (!(node = xmlDocGetRootElement (doc))) {
        g_warning ("`%s' corrupt: document contains no root node", metadata);
        xmlFreeDoc (doc);
        return FALSE;
    }
    
    if (!node->name || strcmp (node->name, "efolder") != 0) {
        g_warning ("`%s' corrupt: root node is not 'efolder'", metadata);
        xmlFreeDoc (doc);
        return FALSE;
    }
    
    node = node->children;
    while (node != NULL) {
        if (node->name && !strcmp (node->name, "type")) {
            type = xmlNodeGetContent (node);
            if (!strcmp (type, "mail")) {
                xmlFreeDoc (doc);
                xmlFree (type);
                
                return TRUE;
            }
            
            xmlFree (type);
            
            break;
        }
        
        node = node->next;
    }
    
    xmlFreeDoc (doc);
    
    return FALSE;
}

static char *
get_local_store_uri (const char *dirname, const char *metadata, char **namep, int *index, CamelException *ex)
{
    char *protocol, *name, *buf;
    struct stat st;
    xmlNodePtr node;
    xmlDocPtr doc;
    
    if (stat (metadata, &st) == -1 || !S_ISREG (st.st_mode)) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, "`%s' is not a regular file", metadata);
        return NULL;
    }
    
    if (!(doc = xmlParseFile (metadata))) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, "cannot parse `%s'", metadata);
        return NULL;
    }
    
    if (!(node = xmlDocGetRootElement (doc)) || strcmp (node->name, "folderinfo") != 0) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, "`%s' is malformed", metadata);
        xmlFreeDoc (doc);
        return NULL;
    }
    
    node = node->children;
    while (node != NULL) {
        if (node->name && !strcmp (node->name, "folder")) {
            protocol = xmlGetProp (node, "type");
            name = xmlGetProp (node, "name");
            buf = xmlGetProp (node, "index");
            if (buf != NULL) {
                *index = atoi (buf);
                xmlFree (buf);
            } else {
                *index = 0;
            }
            
            xmlFreeDoc (doc);
            
            buf = g_strdup_printf ("%s:%s", protocol, dirname);
            xmlFree (protocol);
            
            *namep = g_strdup (name);
            xmlFree (name);
            
            return buf;
        }
        
        node = node->next;
    }
    
    xmlFreeDoc (doc);
    
    camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, "`%s' does not contain needed info", metadata);
    
    return NULL;
}

static int
cp (const char *src, const char *dest, gboolean show_progress)
{
    unsigned char readbuf[4096];
    ssize_t nread, nwritten;
    int errnosav, fd[2];
    size_t total = 0;
    struct stat st;
    
    if (stat (src, &st) == -1)
        return -1;
    
    if ((fd[0] = open (src, O_RDONLY)) == -1)
        return -1;
    
    if ((fd[1] = open (dest, O_WRONLY | O_CREAT | O_EXCL, 0666)) == -1) {
        errnosav = errno;
        close (fd[0]);
        errno = errnosav;
        return -1;
    }
    
    do {
        do {
            nread = read (fd[0], readbuf, sizeof (readbuf));
        } while (nread == -1 && errno == EINTR);
        
        if (nread == 0)
            break;
        else if (nread < 0)
            goto exception;
        
        do {
            nwritten = write (fd[1], readbuf, nread);
        } while (nwritten == -1 && errno == EINTR);
        
        if (nwritten < nread)
            goto exception;
        
        total += nwritten;
        
        if (show_progress)
            em_migrate_set_progress (((double) total) / ((double) st.st_size));
    } while (total < st.st_size);
    
    if (fsync (fd[1]) == -1)
        goto exception;
    
    close (fd[0]);
    close (fd[1]);
    
    return 0;
    
 exception:
    
    errnosav = errno;
    
    close (fd[0]);
    close (fd[1]);
    unlink (dest);
    
    errno = errnosav;
    
    return -1;
}

static GString *
mbox_build_filename (const char *toplevel_dir, const char *full_name)
{
    const char *start, *inptr = full_name;
    int subdirs = 0;
    GString *path;
    
    while (*inptr != '\0') {
        if (*inptr == '/')
            subdirs++;
        inptr++;
    }
    
    path = g_string_new (toplevel_dir);
    g_string_append_c (path, '/');
    
    inptr = full_name;
    while (*inptr != '\0') {
        start = inptr;
        while (*inptr != '/' && *inptr != '\0')
            inptr++;
        
        g_string_append_len (path, start, inptr - start);
        
        if (*inptr == '/') {
            g_string_append (path, ".sbd/");
            inptr++;
            
            /* strip extranaeous '/'s */
            while (*inptr == '/')
                inptr++;
        }
    }
    
    return path;
}

#define mbox_store_build_filename(s,n) mbox_build_filename (((CamelService *) s)->url->path, n)

static void
em_migrate_dir (EMMigrateSession *session, const char *dirname, const char *full_name)
{
    guint32 flags = CAMEL_STORE_FOLDER_CREATE;
    CamelFolder *old_folder, *new_folder;
    CamelStore *local_store;
    struct dirent *dent;
    CamelException ex;
    char *path, *name, *uri;
    GPtrArray *uids;
    struct stat st;
    int index, i;
    DIR *dir;
    
    path = g_strdup_printf ("%s/folder-metadata.xml", dirname);
    if (stat (path, &st) == -1 || !S_ISREG (st.st_mode)) {
        g_free (path);
        return;
    }
    
    if (!is_mail_folder (path)) {
        g_free (path);
        
        goto try_subdirs;
    }
    
    g_free (path);
    
    camel_exception_init (&ex);
    
    /* get old store & folder */
    path = g_strdup_printf ("%s/local-metadata.xml", dirname);
    if (!(uri = get_local_store_uri (dirname, path, &name, &index, &ex))) {
        g_warning ("error opening old store for `%s': %s", full_name, ex.desc);
        camel_exception_clear (&ex);
        g_free (path);
        
        /* try subfolders anyway? */
        goto try_subdirs;
    }
    
    g_free (path);
    
    em_migrate_set_folder_name (full_name);
    
    if (!strncmp (uri, "mbox:", 5)) {
        GString *src, *dest;
        size_t slen, dlen;
        char *p;
        
        src = g_string_new ("");
        g_string_append_printf (src, "%s/%s", uri + 5, name);
        g_free (name);
        
        dest = mbox_store_build_filename (session->store, full_name);
        p = strrchr (dest->str, '/');
        *p = '\0';
        
        slen = src->len;
        dlen = dest->len;
        
        if (camel_mkdir (dest->str, 0777) == -1 && errno != EEXIST) {
            g_string_free (dest, TRUE);
            g_string_free (src, TRUE);
            goto try_subdirs;
        }
        
        *p = '/';
        if (cp (src->str, dest->str, TRUE) == -1) {
            g_string_free (dest, TRUE);
            g_string_free (src, TRUE);
            goto try_subdirs;
        }
        
        g_string_append (src, ".ev-summary");
        g_string_append (dest, ".ev-summary");
        cp (src->str, dest->str, FALSE);
        
        if (index) {
            static char *ibex_ext[] = { ".ibex.index", ".ibex.index.data" };
            FILE *fp;
            int i;
            
            /* create a .cmeta file specifying to index the folder */
            g_string_truncate (dest, dlen);
            g_string_append (dest, ".cmeta");
            if ((fp = fopen (dest->str, "w+")) != NULL) {
                int fd = fileno (fp);
                
                /* write the magic string */
                if (fwrite ("CLMD", 4, 1, fp) != 1)
                    goto cmeta_err;
                
                /* write the version (1) */
                if (camel_file_util_encode_uint32 (fp, 1) == -1)
                    goto cmeta_err;
                
                /* write the meta count */
                if (camel_file_util_encode_uint32 (fp, 0) == -1)
                    goto cmeta_err;
                
                /* write the prop count (only prop is the index prop) */
                if (camel_file_util_encode_uint32 (fp, 1) == -1)
                    goto cmeta_err;
                
                /* write the index prop tag (== CAMEL_FOLDER_ARG_LAST|CAMEL_ARG_BOO) */
                if (camel_file_util_encode_uint32 (fp, CAMEL_FOLDER_ARG_LAST|CAMEL_ARG_BOO) == -1)
                    goto cmeta_err;
                
                /* write the index prop value */
                if (camel_file_util_encode_uint32 (fp, 1) == -1)
                    goto cmeta_err;
                
                fflush (fp);
                
                fd = fileno (fp);
                if (fsync (fd) == -1) {
                cmeta_err:
                    fclose (fp);
                    unlink (dest->str);
                } else {
                    fclose (fp);
                }
            }
            
            /* copy over the ibex files */
            for (i = 0; i < 2; i++) {
                g_string_truncate (src, slen);
                g_string_truncate (dest, dlen);
                
                g_string_append (src, ibex_ext[i]);
                g_string_append (dest, ibex_ext[i]);
                cp (src->str, dest->str, FALSE);
            }
        }
        
        g_string_free (dest, TRUE);
        g_string_free (src, TRUE);
    } else {
        if (!(local_store = camel_session_get_store ((CamelSession *) session, uri, &ex))) {
            g_warning ("error opening old store for `%s': %s", full_name, ex.desc);
            camel_exception_clear (&ex);
            g_free (name);
            g_free (uri);
            
            /* try subfolders anyway? */
            goto try_subdirs;
        }
        
        if (!(old_folder = camel_store_get_folder (local_store, name, 0, &ex))) {
            g_warning ("error opening old folder `%s': %s", full_name, ex.desc);
            camel_object_unref (local_store);
            camel_exception_clear (&ex);
            g_free (name);
            
            /* try subfolders anyway? */
            goto try_subdirs;
        }
        
        g_free (name);
        
        flags |= (index ? CAMEL_STORE_FOLDER_BODY_INDEX : 0);
        if (!(new_folder = camel_store_get_folder (session->store, full_name, flags, &ex))) {
            g_warning ("error creating new mbox folder `%s': %s", full_name, ex.desc);
            camel_object_unref (local_store);
            camel_object_unref (old_folder);
            camel_exception_clear (&ex);
            
            /* try subfolders anyway? */
            goto try_subdirs;
        }
        
        uids = camel_folder_get_uids (old_folder);
        for (i = 0; i < uids->len; i++) {
            CamelMimeMessage *message;
            CamelMessageInfo *info;
            
            if (!(info = camel_folder_get_message_info (old_folder, uids->pdata[i])))
                continue;
            
            if (!(message = camel_folder_get_message (old_folder, uids->pdata[i], &ex))) {
                camel_folder_free_message_info (old_folder, info);
                break;
            }
            
            camel_folder_append_message (new_folder, message, info, NULL, &ex);
            camel_folder_free_message_info (old_folder, info);
            camel_object_unref (message);
            
            if (camel_exception_is_set (&ex))
                break;
            
            em_migrate_set_progress (((double) i + 1) / ((double) uids->len));
        }
        
        camel_folder_free_uids (old_folder, uids);
        
        if (camel_exception_is_set (&ex)) {
            g_warning ("error migrating folder `%s': %s", full_name, ex.desc);
            camel_object_unref (local_store);
            camel_object_unref (old_folder);
            camel_object_unref (new_folder);
            camel_exception_clear (&ex);
            
            /* try subfolders anyway? */
            goto try_subdirs;
        }
        
        /*camel_object_unref (local_store);*/
        camel_object_unref (old_folder);
        camel_object_unref (new_folder);
    }
    
 try_subdirs:
    
    path = g_strdup_printf ("%s/subfolders", dirname);
    if (stat (path, &st) == -1 || !S_ISDIR (st.st_mode)) {
        g_free (path);
        return;
    }
    
    if (!(dir = opendir (path))) {
        g_warning ("cannot open `%s': %s", path, strerror (errno));
        g_free (path);
        return;
    }
    
    while ((dent = readdir (dir))) {
        char *full_path;
        
        if (dent->d_name[0] == '.')
            continue;
        
        full_path = g_strdup_printf ("%s/%s", path, dent->d_name);
        if (stat (full_path, &st) == -1 || !S_ISDIR (st.st_mode)) {
            g_free (full_path);
            continue;
        }
        
        name = g_strdup_printf ("%s/%s", full_name, dent->d_name);
        em_migrate_dir (session, full_path, name);
        g_free (full_path);
        g_free (name);
    }
    
    closedir (dir);
    
    g_free (path);
}

static void
em_migrate_local_folders_from_14_to_20 (EMMigrateSession *session)
{
    struct dirent *dent;
    struct stat st;
    DIR *dir;
    
    if (!(dir = opendir (session->srcdir))) {
        g_warning ("cannot open `%s': %s", session->srcdir, strerror (errno));
        return;
    }
    
    em_migrate_setup_progress_dialog ();
    
    while ((dent = readdir (dir))) {
        char *full_path;
        
        if (dent->d_name[0] == '.')
            continue;
        
        full_path = g_strdup_printf ("%s/%s", session->srcdir, dent->d_name);
        if (stat (full_path, &st) == -1 || !S_ISDIR (st.st_mode)) {
            g_free (full_path);
            continue;
        }
        
        em_migrate_dir (session, full_path, dent->d_name);
        g_free (full_path);
    }
    
    closedir (dir);
    
    em_migrate_close_progress_dialog ();
}

static char *
upgrade_xml_uris_1_4 (const char *uri)
{
    char *path, *prefix, *p;
    CamelURL *url;
    
    if (!strncmp (uri, "file:", 5)) {
        url = camel_url_new (uri, NULL);
        camel_url_set_protocol (url, "email");
        camel_url_set_user (url, "local");
        camel_url_set_host (url, "local");
        
        prefix = g_build_filename (g_get_home_dir (), "evolution", "local", NULL);
        g_assert (strncmp (url->path, prefix, strlen (prefix)) == 0);
        path = g_strdup (url->path + strlen (prefix));
        g_free (prefix);
        
        /* modify the path in-place */
        p = path + strlen (path) - 12;
        while (p > path) {
            if (!strncmp (p, "/subfolders/", 12))
                memmove (p, p + 11, strlen (p + 11) + 1);
            
            p--;
        }
        
        camel_url_set_path (url, path);
        g_free (path);
        
        path = camel_url_to_string (url, 0);
        camel_url_free (url);
        
        return path;
    } else {
        return em_uri_from_camel (uri);
    }
}

static int
em_upgrade_xml_1_4 (xmlDocPtr doc)
{
    return upgrade_xml_uris (doc, upgrade_xml_uris_1_4);
}

static int
em_upgrade_pop_uid_caches_1_4 (const char *evolution_dir, CamelException *ex)
{
    GString *oldpath, *newpath;
    struct dirent *dent;
    size_t olen, nlen;
    char *cache_dir;
    DIR *dir;
    
    /* open the old cache dir */
    cache_dir = g_build_filename (g_get_home_dir (), "evolution", "mail", "pop3", NULL);
    if (!(dir = opendir (cache_dir))) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Failed to migrate pop3 uid caches: %s"),
                      g_strerror (errno));
        g_warning ("cannot open `%s': %s", cache_dir, strerror (errno));
        g_free (cache_dir);
        return -1;
    }
    
    oldpath = g_string_new (cache_dir);
    g_string_append_c (oldpath, '/');
    olen = oldpath->len;
    g_free (cache_dir);
    
    cache_dir = g_build_filename (evolution_dir, "mail", "pop", NULL);
    if (camel_mkdir (cache_dir, 0777) == -1) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Failed to migrate pop3 uid caches: %s"),
                      g_strerror (errno));
        g_warning ("cannot create `%s': %s", cache_dir, strerror (errno));
        g_string_free (oldpath, TRUE);
        g_free (cache_dir);
        closedir (dir);
        return -1;
    }
    
    newpath = g_string_new (cache_dir);
    g_string_append_c (newpath, '/');
    nlen = newpath->len;
    g_free (cache_dir);
    
    while ((dent = readdir (dir))) {
        if (strncmp (dent->d_name, "cache-pop:__", 12) != 0)
            continue;
        
        g_string_truncate (oldpath, olen);
        g_string_truncate (newpath, nlen);
        
        g_string_append (oldpath, dent->d_name);
        g_string_append (newpath, dent->d_name + 12);
        
        /* strip the trailing '_' */
        g_string_truncate (newpath, newpath->len - 1);
        
        if (camel_mkdir (newpath->str, 0777) == -1) {
            g_warning ("cannot create `%s': %s", newpath->str, strerror (errno));
            continue;
        }
        
        g_string_append (newpath, "/uid-cache");
        cp (oldpath->str, newpath->str, FALSE);
    }
    
    g_string_free (oldpath, TRUE);
    g_string_free (newpath, TRUE);
    
    closedir (dir);
    
    return 0;
}


static int
em_migrate_1_4 (const char *evolution_dir, xmlDocPtr filters, xmlDocPtr vfolders, CamelException *ex)
{
    EMMigrateSession *session;
    CamelException lex;
    struct stat st;
    char *path;
    
    path = g_build_filename (evolution_dir, "mail", NULL);
    
    camel_init (path, TRUE);
    session = (EMMigrateSession *) em_migrate_session_new (path);
    g_free (path);
    
    session->srcdir = g_build_filename (g_get_home_dir (), "evolution", "local", NULL);
    
    path = g_strdup_printf ("mbox:%s/.evolution/mail/local", g_get_home_dir ());
    if (stat (path + 5, &st) == -1) {
        if (errno != ENOENT || camel_mkdir (path + 5, 0777) == -1) {
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                          _("Failed to create directory `%s': %s"),
                          path + 5, g_strerror (errno));
            g_free (session->srcdir);
            camel_object_unref (session);
            g_free (path);
            return -1;
        }
    }
    
    camel_exception_init (&lex);
    if (!(session->store = camel_session_get_store ((CamelSession *) session, path, &lex))) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Failed to open store for `%s': %s"),
                      path, lex.desc);
        g_free (session->srcdir);
        camel_object_unref (session);
        camel_exception_clear (&lex);
        g_free (path);
        return -1;
    }
    g_free (path);
    
    em_migrate_local_folders_from_14_to_20 (session);
    
    camel_object_unref (session->store);
    g_free (session->srcdir);
    
    camel_object_unref (session);
    
    if (em_upgrade_xml_1_4 (filters) == -1)
        return -1;
    
    if (em_upgrade_xml_1_4 (vfolders) == -1)
        return -1;
    
    if (em_upgrade_pop_uid_caches_1_4 (evolution_dir, ex) == -1)
        return -1;
    
    return 0;
}


static xmlDocPtr
emm_load_xml (const char *dirname, const char *filename)
{
    xmlDocPtr doc;
    struct stat st;
    char *path;
    
    path = g_strdup_printf ("%s/%s", dirname, filename);
    if (stat (path, &st) == -1 || !(doc = xmlParseFile (path))) {
        g_free (path);
        return NULL;
    }
    
    g_free (path);
    
    return doc;
}

static int
emm_save_xml (xmlDocPtr doc, const char *dirname, const char *filename)
{
    char *path;
    int retval;
    
    path = g_strdup_printf ("%s/%s", dirname, filename);
    retval = e_xml_save_file (path, doc);
    g_free (path);
    
    return retval;
}


int
em_migrate (const char *evolution_dir, int major, int minor, int revision, CamelException *ex)
{
    struct stat st;
    char *path;
    
    /* make sure ~/.evolution/mail exists */
    path = g_build_filename (evolution_dir, "mail", NULL);
    if (stat (path, &st) == -1) {
        if (errno != ENOENT || camel_mkdir (path, 0777) == -1) {
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, 
                          _("Failed to create directory `%s': %s"),
                          path, g_strerror (errno));
            g_free (path);
            return -1;
        }
    }
    
    g_free (path);
    
    if (major == 0) {
        /* special-case - this means brand new install of evolution */
        /* FIXME: create default folders and stuff... */
        
        return 0;
    }
    
    if (major == 1 && minor < 5) {
        xmlDocPtr config_xmldb = NULL, filters, vfolders;
        
        path = g_build_filename (g_get_home_dir (), "evolution", NULL);
        filters = emm_load_xml (path, "filters.xml");
        vfolders = emm_load_xml (path, "vfolders.xml");
        if (minor <= 2)
            config_xmldb = emm_load_xml (path, "config.xmldb");
        g_free (path);
        
        if (minor == 0) {
            if (em_migrate_1_0 (evolution_dir, config_xmldb, filters, vfolders, ex) == -1) {
                xmlFreeDoc (config_xmldb);
                xmlFreeDoc (filters);
                xmlFreeDoc (vfolders);
                return -1;
            }
        }
        
        if (minor <= 2) {
            if (em_migrate_1_2 (evolution_dir, config_xmldb, filters, vfolders, ex) == -1) {
                xmlFreeDoc (config_xmldb);
                xmlFreeDoc (filters);
                xmlFreeDoc (vfolders);
                return -1;
            }
            
            xmlFreeDoc (config_xmldb);
        }
        
        if (minor <= 4) {
            if (em_migrate_1_4 (evolution_dir, filters, vfolders, ex) == -1) {
                xmlFreeDoc (filters);
                xmlFreeDoc (vfolders);
                return -1;
            }
        }
        
        path = g_build_filename (evolution_dir, "mail", NULL);
        
        if (filters) {
            emm_save_xml (filters, path, "filters.xml");
            xmlFreeDoc (filters);
        }
        
        if (vfolders) {
            emm_save_xml (vfolders, path, "vfolders.xml");
            xmlFreeDoc (vfolders);
        }
        
        g_free (path);
    }
    
    return 0;
}