aboutsummaryrefslogtreecommitdiffstats
path: root/mail/e-mail-migrate.c
blob: 7f59d16a484219d461ebc5a3461d2872369c11df (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
/*
 * e-mail-migrate.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#include "e-mail-migrate.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <utime.h>
#include <unistd.h>
#include <dirent.h>
#include <regex.h>
#include <errno.h>
#include <ctype.h>

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

#include <gtk/gtk.h>

#include <gconf/gconf-client.h>

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

#include <e-util/e-util.h>
#include <libedataserver/e-xml-utils.h>
#include <libedataserver/e-data-server-util.h>
#include <e-util/e-xml-utils.h>

#include "e-util/e-account-utils.h"
#include "e-util/e-alert-dialog.h"
#include "e-util/e-util-private.h"
#include "e-util/e-plugin.h"
#include "e-util/e-signature-utils.h"

#include "shell/e-shell.h"
#include "shell/e-shell-migrate.h"

#include "e-mail-store.h"
#include "mail-config.h"
#include "em-utils.h"

#define d(x) x

struct _migrate_state_info {
    gchar *label_name;
    gdouble progress;
};

static gboolean
update_states_in_main_thread (const struct _migrate_state_info *info);

/* 1.4 upgrade functions */

#define EM_TYPE_MIGRATE_SESSION \
    (em_migrate_session_get_type ())

typedef struct _EMMigrateSession {
    CamelSession parent_object;

    CamelStore *store;   /* new folder tree store */
    gchar *srcdir;        /* old folder tree path */
} EMMigrateSession;

typedef struct _EMMigrateSessionClass {
    CamelSessionClass parent_class;

} EMMigrateSessionClass;

GType em_migrate_session_get_type (void);

G_DEFINE_TYPE (EMMigrateSession, em_migrate_session, CAMEL_TYPE_SESSION)

static CamelSession *em_migrate_session_new (const gchar *path);

static void
em_migrate_session_class_init (EMMigrateSessionClass *class)
{
}

static void
em_migrate_session_init (EMMigrateSession *session)
{
}

static CamelSession *
em_migrate_session_new (const gchar *path)
{
    CamelSession *session;

    session = g_object_new (EM_TYPE_MIGRATE_SESSION, NULL);
    camel_session_construct (session, path);

    return session;
}

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

static void
em_migrate_setup_progress_dialog (const gchar *title, const gchar *desc)
{
    GtkWidget *vbox, *hbox, *w;
    gchar *markup;

    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 (desc);

    gtk_label_set_line_wrap ((GtkLabel *) w, TRUE);
    gtk_widget_show (w);
    gtk_box_pack_start ((GtkBox *) vbox, w, TRUE, TRUE, 0);

    hbox = gtk_hbox_new (FALSE, 6);
    gtk_widget_show (hbox);
    gtk_box_pack_start ((GtkBox *) vbox, hbox, TRUE, TRUE, 0);

    label = (GtkLabel *) gtk_label_new ("");
    gtk_widget_show ((GtkWidget *) label);
    gtk_box_pack_start ((GtkBox *) hbox, (GtkWidget *) label, TRUE, TRUE, 0);

    progress = (GtkProgressBar *) gtk_progress_bar_new ();
    gtk_widget_show ((GtkWidget *) progress);
    gtk_box_pack_start ((GtkBox *) hbox, (GtkWidget *) progress, TRUE, TRUE, 0);

    /* Prepare the message */
    vbox = gtk_vbox_new (FALSE, 12);
    gtk_widget_show (vbox);
    gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);

    w = gtk_label_new (NULL);
    gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.0);
    markup = g_strconcat ("<big><b>", title ? title : _("Migration"), "</b></big>", NULL);
    gtk_label_set_markup (GTK_LABEL (w), markup);
    gtk_box_pack_start (GTK_BOX (vbox), w, TRUE, TRUE, 0);
    g_free (markup);

    w = gtk_label_new (desc);
    gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.0);
    gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
    gtk_box_pack_start (GTK_BOX (vbox), w, TRUE, TRUE, 0);

    /* Progress bar */
    w = gtk_vbox_new (FALSE, 6);
    gtk_box_pack_start (GTK_BOX (vbox), w, TRUE, TRUE, 0);

    label = GTK_LABEL (gtk_label_new (""));
    gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
    gtk_label_set_line_wrap (label, TRUE);
    gtk_widget_show (GTK_WIDGET (label));
    gtk_box_pack_start (GTK_BOX (w), GTK_WIDGET (label), TRUE, TRUE, 0);

    progress = GTK_PROGRESS_BAR (gtk_progress_bar_new ());
    gtk_widget_show (GTK_WIDGET (progress));
    gtk_box_pack_start (GTK_BOX (w), GTK_WIDGET (progress), TRUE, TRUE, 0);

    gtk_container_add (GTK_CONTAINER (window), hbox);
    gtk_widget_show_all (hbox);
    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 gchar *folder_name)
{
    gchar *text;

    text = g_strdup_printf (_("Migrating '%s':"), folder_name);
    gtk_label_set_text (label, text);
    g_free (text);
}

static void
em_migrate_set_progress (double percent)
{
    gchar text[5];

    snprintf (text, sizeof (text), "%d%%", (gint) (percent * 100.0f));

    gtk_progress_bar_set_fraction (progress, percent);
    gtk_progress_bar_set_text (progress, text);

    while (gtk_events_pending ())
        gtk_main_iteration ();
}

enum {
    CP_UNIQUE = 0,
    CP_OVERWRITE,
    CP_APPEND
};

static gint open_flags[3] = {
    O_WRONLY | O_CREAT | O_TRUNC,
    O_WRONLY | O_CREAT | O_TRUNC,
    O_WRONLY | O_CREAT | O_APPEND,
};

static gboolean
cp (const gchar *src, const gchar *dest, gboolean show_progress, gint mode)
{
    guchar readbuf[65536];
    gssize nread, nwritten;
    gint errnosav, readfd, writefd;
    gsize total = 0;
    struct stat st;
    struct utimbuf ut;

    /* if the dest file exists and has content, abort - we don't
     * want to corrupt their existing data */
    if (g_stat (dest, &st) == 0 && st.st_size > 0 && mode == CP_UNIQUE) {
        errno = EEXIST;
        return FALSE;
    }

    if (g_stat (src, &st) == -1
        || (readfd = g_open (src, O_RDONLY | O_BINARY, 0)) == -1)
        return FALSE;

    if ((writefd = g_open (dest, open_flags[mode] | O_BINARY, 0666)) == -1) {
        errnosav = errno;
        close (readfd);
        errno = errnosav;
        return FALSE;
    }

    do {
        do {
            nread = read (readfd, readbuf, sizeof (readbuf));
        } while (nread == -1 && errno == EINTR);

        if (nread == 0)
            break;
        else if (nread < 0)
            goto exception;

        do {
            nwritten = write (writefd, 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 (writefd) == -1)
        goto exception;

    close (readfd);
    if (close (writefd) == -1)
        goto failclose;

    ut.actime = st.st_atime;
    ut.modtime = st.st_mtime;
    utime (dest, &ut);
    chmod (dest, st.st_mode);

    return TRUE;

 exception:

    errnosav = errno;
    close (readfd);
    close (writefd);
    errno = errnosav;

 failclose:

    errnosav = errno;
    unlink (dest);
    errno = errnosav;

    return FALSE;
}

#ifndef G_OS_WIN32

#define SUBFOLDER_DIR_NAME     "subfolders"
#define SUBFOLDER_DIR_NAME_LEN 10

static void
em_update_accounts_2_11 (void)
{
    EAccountList *accounts;
    EIterator *iter;
    gboolean changed = FALSE;

    if (!(accounts = e_get_account_list ()))
        return;

    iter = e_list_get_iterator ((EList *) accounts);
    while (e_iterator_is_valid (iter)) {
        EAccount *account = (EAccount *) e_iterator_get (iter);

        if (g_str_has_prefix (account->source->url, "spool://")) {
            if (g_file_test (account->source->url + 8, G_FILE_TEST_IS_DIR)) {
                gchar *str = g_strdup_printf ("spooldir://%s", account->source->url + 8);

                g_free (account->source->url);
                account->source->url = str;
                changed = TRUE;
            }
        }

        e_iterator_next (iter);
    }

    g_object_unref (iter);

    if (changed)
        e_account_list_save (accounts);
}

#endif  /* !G_OS_WIN32 */

static gboolean
emm_setup_initial(const gchar *data_dir)
{
    GDir *dir;
    const gchar *d;
    gchar *local = NULL, *base;
    const gchar * const *language_names;

    /* special-case - this means brand new install of evolution */
    /* FIXME: create default folders and stuff... */

    d(printf("Setting up initial mail tree\n"));

    base = g_build_filename(data_dir, "local", NULL);
    if (g_mkdir_with_parents(base, 0700) == -1 && errno != EEXIST) {
        g_free(base);
        return FALSE;
    }

    /* e.g. try en-AU then en, etc */
    language_names = g_get_language_names ();
    while (*language_names != NULL) {
        local = g_build_filename (
            EVOLUTION_PRIVDATADIR, "default",
            *language_names, "mail", "local", NULL);
        if (g_file_test (local, G_FILE_TEST_EXISTS))
            break;
        g_free (local);
        language_names++;
    }

    /* Make sure we found one. */
    g_return_val_if_fail (*language_names != NULL, FALSE);

    dir = g_dir_open(local, 0, NULL);
    if (dir) {
        while ((d = g_dir_read_name(dir))) {
            gchar *src, *dest;

            src = g_build_filename(local, d, NULL);
            dest = g_build_filename(base, d, NULL);

            cp(src, dest, FALSE, CP_UNIQUE);
            g_free(dest);
            g_free(src);
        }
        g_dir_close(dir);
    }

    g_free(local);
    g_free(base);

    return TRUE;
}

static gboolean
is_in_plugs_list (GSList *list, const gchar *value)
{
    GSList *l;

    for (l = list; l; l = l->next) {
        if (l->data && !strcmp (l->data, value))
            return TRUE;
    }

    return FALSE;
}

/*
 * em_update_message_notify_settings_2_21
 * DBus plugin and sound email notification was merged to mail-notification plugin,
 * so move these options to new locations.
 */
static void
em_update_message_notify_settings_2_21 (void)
{
    GConfClient *client;
    GConfValue  *is_key;
    gboolean dbus, status;
    GSList *list;
    gchar *str;
    gint val;

    client = gconf_client_get_default ();

    is_key = gconf_client_get (client, "/apps/evolution/eplugin/mail-notification/dbus-enabled", NULL);
    if (is_key) {
        /* already migrated, so do not migrate again */
        gconf_value_free (is_key);
        g_object_unref (client);

        return;
    }

    gconf_client_set_bool (client, "/apps/evolution/eplugin/mail-notification/status-blink-icon",
                gconf_client_get_bool (client, "/apps/evolution/mail/notification/blink-status-icon", NULL), NULL);
    gconf_client_set_bool (client, "/apps/evolution/eplugin/mail-notification/status-notification",
                gconf_client_get_bool (client, "/apps/evolution/mail/notification/notification", NULL), NULL);

    list = gconf_client_get_list (client, "/apps/evolution/eplugin/disabled", GCONF_VALUE_STRING, NULL);
    dbus = !is_in_plugs_list (list, "org.gnome.evolution.new_mail_notify");
    status = !is_in_plugs_list (list, "org.gnome.evolution.mail_notification");

    gconf_client_set_bool (client, "/apps/evolution/eplugin/mail-notification/dbus-enabled", dbus, NULL);
    gconf_client_set_bool (client, "/apps/evolution/eplugin/mail-notification/status-enabled", status, NULL);

    if (!status) {
        /* enable this plugin, because it holds all those other things */
        GSList *plugins, *l;

        plugins = e_plugin_list_plugins ();

        for (l = plugins; l; l = l->next) {
            EPlugin *p = l->data;

            if (p && p->id && !strcmp (p->id, "org.gnome.evolution.mail_notification")) {
                e_plugin_enable (p, 1);
                break;
            }
        }

        g_slist_foreach (plugins, (GFunc)g_object_unref, NULL);
        g_slist_free (plugins);
    }

    g_slist_foreach (list, (GFunc) g_free, NULL);
    g_slist_free (list);

    val = gconf_client_get_int (client, "/apps/evolution/mail/notify/type", NULL);
    gconf_client_set_bool (client, "/apps/evolution/eplugin/mail-notification/sound-enabled", val == 1 || val == 2, NULL);
    gconf_client_set_bool (client, "/apps/evolution/eplugin/mail-notification/sound-beep", val == 0 || val == 1, NULL);

    str = gconf_client_get_string (client, "/apps/evolution/mail/notify/sound", NULL);
    gconf_client_set_string (client, "/apps/evolution/eplugin/mail-notification/sound-file", str ? str : "", NULL);
    g_free (str);

    g_object_unref (client);
}

/* fixing typo in SpamAssassin name */
static void
em_update_sa_junk_setting_2_23 (void)
{
    GConfClient *client;
    GConfValue  *key;

    client = gconf_client_get_default ();

    key = gconf_client_get (client, "/apps/evolution/mail/junk/default_plugin", NULL);
    if (key) {
        const gchar *str = gconf_value_get_string (key);

        if (str && strcmp (str, "Spamassasin") == 0)
            gconf_client_set_string (client, "/apps/evolution/mail/junk/default_plugin", "SpamAssassin", NULL);

        gconf_value_free (key);
        g_object_unref (client);

        return;
    }

    g_object_unref (client);
}

#ifndef G_OS_WIN32

static gboolean
update_states_in_main_thread (const struct _migrate_state_info * info)
{
    g_return_val_if_fail (info != NULL, FALSE);
    g_return_val_if_fail (info->label_name != NULL, FALSE);
    em_migrate_set_progress (info->progress);
    em_migrate_set_folder_name (info->label_name);
    g_free (info->label_name);
    g_free ( (gpointer)info);
    while (gtk_events_pending ())
        gtk_main_iteration ();
    return FALSE;
}

static void
migrate_folders(CamelStore *store, gboolean is_local, CamelFolderInfo *fi, const gchar *acc, CamelException *ex, gboolean *done, gint *nth_folder, gint total_folders)
{
    CamelFolder *folder;

    while (fi) {

        struct _migrate_state_info *info = g_malloc (sizeof (struct
                    _migrate_state_info));
        info->label_name = g_strdup_printf ("%s/%s", acc,
                fi->full_name);

        *nth_folder = *nth_folder + 1;

        info->progress = (double) (*nth_folder) / total_folders;
        g_idle_add ((GSourceFunc) update_states_in_main_thread, info);

        if (is_local)
                folder = camel_store_get_folder (store, fi->full_name, CAMEL_STORE_IS_MIGRATING, ex);
        else
                folder = camel_store_get_folder (store, fi->full_name, 0, ex);

        if (folder != NULL)
            camel_folder_summary_migrate_infos (folder->summary);
        migrate_folders(store, is_local, fi->child, acc, ex, done, nth_folder, total_folders);
        fi = fi->next;
    }

    if ((*nth_folder) == (total_folders - 1))
        *done = TRUE;
}

#endif /* G_OS_WIN32 */

/* This could be in CamelStore.ch */
static void
count_folders (CamelFolderInfo *fi, gint *count)
{
    while (fi) {
        *count = *count + 1;
        count_folders (fi->child, count);
        fi = fi->next;
    }
}

static CamelStore *
setup_local_store (EShellBackend *shell_backend,
                   EMMigrateSession *session)
{
    CamelURL *url;
    const gchar *data_dir;
    gchar *tmp;
    CamelStore *store;

    url = camel_url_new("mbox:", NULL);
    data_dir = e_shell_backend_get_data_dir (shell_backend);
    tmp = g_build_filename (data_dir, "local", NULL);
    camel_url_set_path(url, tmp);
    g_free(tmp);
    tmp = camel_url_to_string(url, 0);
    store = (CamelStore *)camel_session_get_service(CAMEL_SESSION (session), tmp, CAMEL_PROVIDER_STORE, NULL);
    g_free(tmp);

    return store;
}

#ifndef G_OS_WIN32

struct migrate_folders_to_db_structure {
        gchar *account_name;
        CamelException ex;
        CamelStore *store;
        CamelFolderInfo *info;
        gboolean done;
        gboolean is_local_store;
};

static void
migrate_folders_to_db_thread (struct migrate_folders_to_db_structure *migrate_dbs)
{
        gint num_of_folders = 0, nth_folder = 0;
        count_folders (migrate_dbs->info, &num_of_folders);
        migrate_folders (migrate_dbs->store, migrate_dbs->is_local_store, migrate_dbs->info,
                        migrate_dbs->account_name, &(migrate_dbs->ex), &(migrate_dbs->done),
                        &nth_folder, num_of_folders);
}

static void
migrate_to_db (EShellBackend *shell_backend)
{
    EMMigrateSession *session;
    EAccountList *accounts;
    EIterator *iter;
    gint i=0, len;
    CamelStore *store = NULL;
    CamelFolderInfo *info;
    const gchar *data_dir;

    if (!(accounts = e_get_account_list ()))
        return;

    iter = e_list_get_iterator ((EList *) accounts);
    len = e_list_length ((EList *) accounts);

    data_dir = e_shell_backend_get_data_dir (shell_backend);
    session = (EMMigrateSession *) em_migrate_session_new (data_dir);
    camel_session_set_online ((CamelSession *) session, FALSE);
    em_migrate_setup_progress_dialog (
        _("Migrating Folders"),
        _("The summary format of the Evolution mailbox "
          "folders has been moved to SQLite since Evolution 2.24.\n\nPlease be "
          "patient while Evolution migrates your folders..."));

    em_migrate_set_progress ( (double)i/(len+1));
    store = setup_local_store (shell_backend, session);
    info = camel_store_get_folder_info (store, NULL, CAMEL_STORE_FOLDER_INFO_RECURSIVE|CAMEL_STORE_FOLDER_INFO_FAST|CAMEL_STORE_FOLDER_INFO_SUBSCRIBED, NULL);
    if (info) {
        struct migrate_folders_to_db_structure migrate_dbs;

        if (g_str_has_suffix (((CamelService *)store)->url->path, ".evolution/mail/local"))
            migrate_dbs.is_local_store = TRUE;
        else
            migrate_dbs.is_local_store = FALSE;
        camel_exception_init (&migrate_dbs.ex);
        migrate_dbs.account_name = _("On This Computer");
        migrate_dbs.info = info;
        migrate_dbs.store = store;
        migrate_dbs.done = FALSE;

        g_thread_create ((GThreadFunc) migrate_folders_to_db_thread, &migrate_dbs, TRUE, NULL);
        while (!migrate_dbs.done)
            g_main_context_iteration (NULL, TRUE);
    }
    i++;
    em_migrate_set_progress ( (double)i/(len+1));

    while (e_iterator_is_valid (iter)) {
        EAccount *account = (EAccount *) e_iterator_get (iter);
        EAccountService *service;
        const gchar *name;

        service = account->source;
        name = account->name;
        em_migrate_set_progress ( (double)i/(len+1));
        if (account->enabled
            && service->url != NULL
            && service->url[0]
            && strncmp(service->url, "mbox:", 5) != 0) {

            CamelException ex;

            camel_exception_init (&ex);
            e_mail_store_add_by_uri (service->url, name);

            store = (CamelStore *) camel_session_get_service (CAMEL_SESSION (session), service->url, CAMEL_PROVIDER_STORE, &ex);
            info = camel_store_get_folder_info (store, NULL, CAMEL_STORE_FOLDER_INFO_RECURSIVE|CAMEL_STORE_FOLDER_INFO_FAST|CAMEL_STORE_FOLDER_INFO_SUBSCRIBED, &ex);
            if (info) {
                struct migrate_folders_to_db_structure migrate_dbs;

                migrate_dbs.ex = ex;
                migrate_dbs.account_name = account->name;
                migrate_dbs.info = info;
                migrate_dbs.store = store;
                migrate_dbs.done = FALSE;

                g_thread_create ((GThreadFunc) migrate_folders_to_db_thread, &migrate_dbs, TRUE, NULL);
                while (!migrate_dbs.done)
                    g_main_context_iteration (NULL, TRUE);
            } else
                printf("%s:%s: failed to get folder infos \n", G_STRLOC, G_STRFUNC);
            camel_exception_clear(&ex);

        }
        i++;
        e_iterator_next (iter);

    }

    //camel_session_set_online ((CamelSession *) session, TRUE);

    g_object_unref (iter);
    em_migrate_close_progress_dialog ();

    g_object_unref (session);
}

#endif

gboolean
e_mail_migrate (EShellBackend *shell_backend,
                gint major,
                gint minor,
                gint micro,
                GError **error)
{
    struct stat st;
    const gchar *data_dir;

    /* make sure ~/.evolution/mail exists */
    data_dir = e_shell_backend_get_data_dir (shell_backend);
    if (g_stat (data_dir, &st) == -1) {
        if (errno != ENOENT || g_mkdir_with_parents (data_dir, 0700) == -1) {
            g_set_error (
                error, E_SHELL_MIGRATE_ERROR,
                E_SHELL_MIGRATE_ERROR_FAILED,
                _("Unable to create local mail folders at "
                "'%s': %s"), data_dir, g_strerror (errno));
            return FALSE;
        }
    }

    if (major == 0)
        return emm_setup_initial (data_dir);

    if (major < 2 || (major == 2 && minor < 12)) {
#ifndef G_OS_WIN32
        em_update_accounts_2_11 ();
#else
        g_error ("Upgrading from ancient versions not supported on Windows");
#endif
    }

    if (major < 2 || (major == 2 && minor < 22))
#ifndef G_OS_WIN32
        em_update_message_notify_settings_2_21 ();
#else
        g_error ("Upgrading from ancient versions not supported on Windows");
#endif

    if (major < 2 || (major == 2 && minor < 24)) {
#ifndef G_OS_WIN32
        em_update_sa_junk_setting_2_23 ();
        migrate_to_db (shell_backend);
#else
        g_error ("Upgrading from ancient versions not supported on Windows");
#endif
    }

    return TRUE;
}