aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/dbx-import/dbx-importer.c
blob: 47e4139cda79e7b181955d5cd7d61df857fcf649 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* dbx-importer.c
*
* Author: David Woodhouse <dwmw2@infradead.org>
*
* Copyright © 2010 Intel Corporation
*
* Evolution parts largely lifted from pst-import.c:
*   Author: Chris Halls <chris.halls@credativ.co.uk>
*       Bharath Acharya <abharath@novell.com>
*   Copyright © 2006 Chris Halls
*
* Some DBX bits from libdbx:
*   Author: David Smith <Dave.S@Earthcorp.Com>
*    Copyright © 2001 David Smith
*
* 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/>
*
*/

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

#define G_LOG_DOMAIN "eplugin-readdbx"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>
#include <glib/gprintf.h>

#include <gtk/gtk.h>

#include <e-util/e-import.h>
#include <e-util/e-plugin.h>
#include <e-util/e-mktemp.h>

#include <shell/e-shell.h>
#include <shell/e-shell-window.h>
#include <shell/e-shell-view.h>

#include <libebook/e-contact.h>
#include <libebook/e-book.h>

#include <libecal/e-cal.h>
#include <libecal/e-cal-component.h>

#include <libedataserver/e-data-server-util.h>
#include <libedataserverui/e-source-selector-dialog.h>

#include <mail/e-mail-backend.h>
#include <mail/e-mail-local.h>
#include <mail/em-folder-selection-button.h>
#include <mail/mail-mt.h>
#include <mail/mail-tools.h>
#include <mail/em-utils.h>

#define d(x)

#ifdef WIN32
#ifdef gmtime_r
#undef gmtime_r
#endif
#define gmtime_r(tp,tmp) (gmtime(tp)?(*(tmp)=*gmtime(tp),(tmp)):0)
#endif

gboolean    org_gnome_evolution_readdbx_supported
                        (EPlugin *epl,
                         EImportTarget *target);
GtkWidget * org_gnome_evolution_readdbx_getwidget
                        (EImport *ei,
                         EImportTarget *target,
                         EImportImporter *im);
void        org_gnome_evolution_readdbx_import
                        (EImport *ei,
                         EImportTarget *target,
                         EImportImporter *im);
void        org_gnome_evolution_readdbx_cancel
                        (EImport *ei,
                         EImportTarget *target,
                         EImportImporter *im);
gint        e_plugin_lib_enable     (EPlugin *ep,
                         gint enable);

/* em-folder-selection-button.h is private, even though other internal
 * evo plugins use it!
   so declare the functions here
   TODO: sort out whether this should really be private
*/

typedef struct {
    MailMsg base;

    EImport *import;
    EImportTarget *target;

    GMutex *status_lock;
    gchar *status_what;
    gint status_pc;
    gint status_timeout_id;
    GCancellable *cancellable;

    guint32 *indices;
    guint32 index_count;

    gchar *uri;
    gint dbx_fd;
    //  dbx_file dbx;

    CamelOperation *cancel;
    CamelFolder *folder;
    gchar *parent_uri;
    gchar *folder_name;
    gchar *folder_uri;
    gint folder_count;
    gint current_item;
} DbxImporter;

static guchar oe56_mbox_sig[16] = {
    0xcf, 0xad, 0x12, 0xfe, 0xc5, 0xfd, 0x74, 0x6f,
    0x66, 0xe3, 0xd1, 0x11, 0x9a, 0x4e, 0x00, 0xc0
};
static guchar oe56_flist_sig[16] = {
    0xcf, 0xad, 0x12, 0xfe, 0xc6, 0xfd, 0x74, 0x6f,
    0x66, 0xe3, 0xd1, 0x11, 0x9a, 0x4e, 0x00, 0xc0
};
static guchar oe4_mbox_sig[8] = {
    0x4a, 0x4d, 0x46, 0x36, 0x03, 0x00, 0x01, 0x00
};

gboolean
org_gnome_evolution_readdbx_supported (EPlugin *epl, EImportTarget *target)
{
    gchar signature[16];
    gboolean ret = FALSE;
    gint fd, n;
    EImportTargetURI *s;
    gchar *filename;

    if (target->type != E_IMPORT_TARGET_URI) {
        return FALSE;
    }

    s = (EImportTargetURI *)target;

    if (s->uri_src == NULL) {
        return TRUE;
    }

    if (strncmp (s->uri_src, "file:///", strlen ("file:///")) != 0) {
        return FALSE;
    }

    filename = g_filename_from_uri (s->uri_src, NULL, NULL);
    fd = g_open (filename, O_RDONLY, 0);
    g_free (filename);

    if (fd != -1) {
        n = read (fd, signature, sizeof (signature));
        if (n == sizeof (signature)) {
            if (!memcmp (signature, oe56_mbox_sig, sizeof (oe56_mbox_sig))) {
                ret = TRUE;
            } else if (!memcmp (signature, oe56_flist_sig, sizeof (oe56_flist_sig))) {
                d(printf("Found DBX folder list file\n"));
            } else if (!memcmp (signature, oe4_mbox_sig, sizeof (oe4_mbox_sig))) {
                d(printf("Found OE4 DBX file\n"));
            }
        }
        close (fd);
    }

    return ret;
}

static void
folder_selected (EMFolderSelectionButton *button, EImportTargetURI *target)
{
    g_free (target->uri_dest);
    target->uri_dest = g_strdup (em_folder_selection_button_get_selection (button));
}

GtkWidget *
org_gnome_evolution_readdbx_getwidget (EImport *ei,
                                       EImportTarget *target,
                                       EImportImporter *im)
{
    EShell *shell;
    EShellBackend *shell_backend;
    EMailSession *session;
    GtkWidget *hbox, *w;
    GtkLabel *label;
    gchar *select_uri = NULL;

#if 1
    GtkWindow *window;
    /* preselect the folder selected in a mail view */
    window = e_shell_get_active_window (e_shell_get_default ());
    if (E_IS_SHELL_WINDOW (window)) {
        EShellWindow *shell_window;
        const gchar *view;

        shell_window = E_SHELL_WINDOW (window);
        view = e_shell_window_get_active_view (shell_window);

        if (view && g_str_equal (view, "mail")) {
            EShellView *shell_view = e_shell_window_get_shell_view (shell_window, view);

            if (shell_view) {
                EMFolderTree *folder_tree = NULL;
                EShellSidebar *shell_sidebar = e_shell_view_get_shell_sidebar (shell_view);

                g_object_get (shell_sidebar, "folder-tree", &folder_tree, NULL);

                if (folder_tree)
                    select_uri = em_folder_tree_get_selected_uri (folder_tree);
            }
        }
    }
#endif
    if (!select_uri)
        select_uri = g_strdup (e_mail_local_get_folder_uri (E_MAIL_LOCAL_FOLDER_INBOX));

    hbox = gtk_hbox_new (FALSE, 0);

    w = gtk_label_new_with_mnemonic (_("_Destination folder:"));
    gtk_box_pack_start ((GtkBox *)hbox, w, FALSE, TRUE, 6);

    label = GTK_LABEL (w);

    shell = e_shell_get_default ();
    shell_backend = e_shell_get_backend_by_name (shell, "mail");
    session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));

    w = em_folder_selection_button_new (
        session, _("Select folder"),
        _("Select folder to import into"));

    gtk_label_set_mnemonic_widget (label, w);
    em_folder_selection_button_set_selection ((EMFolderSelectionButton *)w, select_uri);
    folder_selected ((EMFolderSelectionButton *)w, (EImportTargetURI *)target);
    g_signal_connect (w, "selected", G_CALLBACK(folder_selected), target);
    gtk_box_pack_start ((GtkBox *)hbox, w, FALSE, TRUE, 6);

    w = gtk_vbox_new (FALSE, 0);
    gtk_box_pack_start ((GtkBox *)w, hbox, FALSE, FALSE, 0);
    gtk_widget_show_all (w);

    g_free (select_uri);

    return w;
}

static gchar *
dbx_import_describe (DbxImporter *m, gint complete)
{
    return g_strdup (_("Importing Outlook Express data"));
}

/* Types taken from libdbx and fixed */
struct _dbx_tableindexstruct {
    guint32 self;
    guint32 unknown1;
    guint32 anotherTablePtr;
    guint32 parent;
    gchar unknown2;
    gchar ptrCount;
    gchar reserve3;
    gchar reserve4;
    guint32 indexCount;
};

struct _dbx_indexstruct {
    guint32 indexptr;
    guint32 anotherTablePtr;
    guint32 indexCount;
};

#define INDEX_POINTER 0xE4
#define ITEM_COUNT 0xC4

struct _dbx_email_headerstruct {
    guint32 self;
    guint32 size;
    gushort u1;
    guchar count;
    guchar u2;
};

struct _dbx_block_hdrstruct {
    guint32 self;
    guint32 nextaddressoffset;
    gushort blocksize;
    guchar intcount;
    guchar unknown1;
    guint32 nextaddress;
};

static gint dbx_pread (gint fd, gpointer buf, guint32 count, guint32 offset)
{
    if (lseek (fd, offset, SEEK_SET) != offset)
        return -1;
    return read (fd, buf, count);
}

static gboolean dbx_load_index_table (DbxImporter *m, guint32 pos, guint32 *index_ofs)
{
    struct _dbx_tableindexstruct tindex;
    struct _dbx_indexstruct index;
    gint i;

    d(printf("Loading index table at 0x%x\n", pos));

    if (dbx_pread (m->dbx_fd, &tindex, sizeof (tindex), pos) != sizeof (tindex)) {
        g_set_error (
            &m->base.error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
            "Failed to read table index from DBX file");
        return FALSE;
    }
    tindex.anotherTablePtr = GUINT32_FROM_LE (tindex.anotherTablePtr);
    tindex.self = GUINT32_FROM_LE (tindex.self);
    tindex.indexCount = GUINT32_FROM_LE (tindex.indexCount);

    if (tindex.self != pos) {
        g_set_error (
            &m->base.error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
            "Corrupt DBX file: Index table at 0x%x does not "
            "point to itself", pos);
        return FALSE;
    }

    d(printf("Index at %x: indexCount %x, anotherTablePtr %x\n",
        pos, tindex.indexCount, tindex.anotherTablePtr));

    if (tindex.indexCount > 0) {
        if (!dbx_load_index_table (m, tindex.anotherTablePtr, index_ofs))
            return FALSE;
    }

    d(printf("Index at %x has ptrCount %d\n", pos, tindex.ptrCount));

    pos += sizeof (tindex);

    for (i = 0; i < tindex.ptrCount; i++) {
        if (dbx_pread (m->dbx_fd, &index, sizeof (index), pos) != sizeof (index)) {
            g_set_error (
                &m->base.error,
                CAMEL_ERROR, CAMEL_ERROR_GENERIC,
                "Failed to read index entry from DBX file");
            return FALSE;
        }
        index.indexptr = GUINT32_FROM_LE (index.indexptr);
        index.anotherTablePtr = GUINT32_FROM_LE (index.anotherTablePtr);
        index.indexCount = GUINT32_FROM_LE (index.indexCount);

        if (*index_ofs == m->index_count) {
            g_set_error (
                &m->base.error,
                CAMEL_ERROR, CAMEL_ERROR_GENERIC,
                "Corrupt DBX file: Seems to contain more "
                "than %d entries claimed in its header",
                m->index_count);
            return FALSE;
        }
        m->indices[(*index_ofs)++] = index.indexptr;
        if (index.indexCount > 0) {
            if (!dbx_load_index_table (m, index.anotherTablePtr, index_ofs))
                return FALSE;
        }
        pos += sizeof (index);
    }
    return TRUE;
}
static gboolean dbx_load_indices (DbxImporter *m)
{
    guint indexptr, itemcount;
    guint32 index_ofs = 0;

    if (dbx_pread (m->dbx_fd, &indexptr, 4, INDEX_POINTER) != 4) {
        g_set_error (
            &m->base.error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
            "Failed to read first index pointer from DBX file");
        return FALSE;
    }

    if (dbx_pread (m->dbx_fd, &itemcount, 4, ITEM_COUNT) != 4) {
        g_set_error (
            &m->base.error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
            "Failed to read item count from DBX file");
        return FALSE;
    }

    indexptr = GUINT32_FROM_LE (indexptr);
    m->index_count = itemcount = GUINT32_FROM_LE (itemcount);
    m->indices = g_malloc (itemcount * 4);

    d(printf("indexptr %x, itemcount %d\n", indexptr, itemcount));

    if (indexptr && !dbx_load_index_table (m, indexptr, &index_ofs))
        return FALSE;

    d(printf("Loaded %d of %d indices\n", index_ofs, m->index_count));

    if (index_ofs < m->index_count) {
        g_set_error (
            &m->base.error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
            "Corrupt DBX file: Seems to contain fewer than %d "
            "entries claimed in its header", m->index_count);
        return FALSE;
    }
    return TRUE;
}

static gboolean
dbx_read_mail_body (DbxImporter *m, guint32 offset, gint bodyfd)
{
    /* FIXME: We really ought to set up CamelStream that we can feed to the
       MIME parser, rather than using a temporary file */

    struct _dbx_block_hdrstruct hdr;
    guint32 buflen = 0x200;
    guchar *buffer = g_malloc (buflen);

    ftruncate (bodyfd, 0);
    lseek (bodyfd, 0, SEEK_SET);

    while (offset) {
        d(printf("Reading mail data chunk from %x\n", offset));

        if (dbx_pread (m->dbx_fd, &hdr, sizeof (hdr), offset) != sizeof (hdr)) {
            g_set_error (
                &m->base.error,
                CAMEL_ERROR, CAMEL_ERROR_GENERIC,
                "Failed to read mail data block from "
                "DBX file at offset %x", offset);
            return FALSE;
        }
        hdr.self = GUINT32_FROM_LE (hdr.self);
        hdr.blocksize = GUINT16_FROM_LE (hdr.blocksize);
        hdr.nextaddress = GUINT32_FROM_LE (hdr.nextaddress);

        if (hdr.self != offset) {
            g_set_error (
                &m->base.error,
                CAMEL_ERROR, CAMEL_ERROR_GENERIC,
                "Corrupt DBX file: Mail data block at "
                "0x%x does not point to itself", offset);
            return FALSE;
        }

        if (hdr.blocksize > buflen) {
            g_free (buffer);
            buflen = hdr.blocksize;
            buffer = g_malloc (buflen);
        }
        d(printf("Reading %d bytes from %lx\n", hdr.blocksize, offset + sizeof(hdr)));
        if (dbx_pread (m->dbx_fd, buffer, hdr.blocksize,
            offset + sizeof (hdr)) != hdr.blocksize) {
            g_set_error (
                &m->base.error,
                CAMEL_ERROR, CAMEL_ERROR_GENERIC,
                "Failed to read mail data from DBX file "
                "at offset %lx",
                (long)(offset + sizeof (hdr)));
            return FALSE;
        }
        if (write (bodyfd, buffer, hdr.blocksize) != hdr.blocksize) {
            g_set_error (
                &m->base.error,
                CAMEL_ERROR, CAMEL_ERROR_GENERIC,
                "Failed to write mail data to temporary file");
            return FALSE;
        }
        offset = hdr.nextaddress;
    }
    return TRUE;
}

static gboolean
dbx_read_email (DbxImporter *m, guint32 offset, gint bodyfd, gint *flags)
{
    struct _dbx_email_headerstruct hdr;
    guchar *buffer;
    guint32 dataptr = 0;
    gint i;

    if (dbx_pread (m->dbx_fd, &hdr, sizeof (hdr), offset) != sizeof (hdr)) {
        g_set_error (
            &m->base.error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
            "Failed to read mail header from DBX file at offset %x",
            offset);
        return FALSE;
    }
    hdr.self = GUINT32_FROM_LE (hdr.self);
    hdr.size = GUINT32_FROM_LE (hdr.size);

    if (hdr.self != offset) {
        g_set_error (
            &m->base.error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
            "Corrupt DBX file: Mail header at 0x%x does not "
            "point to itself", offset);
        return FALSE;
    }
    buffer = g_malloc (hdr.size);
    offset += sizeof (hdr);
    if (dbx_pread (m->dbx_fd, buffer, hdr.size, offset) != hdr.size) {
        g_set_error (
            &m->base.error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
            "Failed to read mail data block from DBX file "
            "at offset %x", offset);
        g_free (buffer);
        return FALSE;
    }

    for (i = 0; i < hdr.count; i++) {
        guchar type = buffer[i*4];
        gint val = buffer[i*4 + 1] + (buffer[i*4 + 2] << 8) + (buffer[i*4 + 3] << 16);

        switch (type) {
        case 0x01:
            *flags = buffer[hdr.count*4 + val];
            d(printf("Got type 0x01 flags %02x\n", *flags));
            break;
        case 0x81:
            *flags = val;
            d(printf("Got type 0x81 flags %02x\n", *flags));
            break;
        case 0x04:
            dataptr = GUINT32_FROM_LE (*(guint32 *)(buffer + hdr.count*4 + val));
            d(printf("Got type 0x04 data pointer %x\n", dataptr));
            break;
        case 0x84:
            dataptr = val;
            d(printf("Got type 0x84 data pointer %x\n", dataptr));
            break;
        default:
            /* We don't care about anything else */
            d(printf("Ignoring type %02x datum\n", type));
            break;
        }
    }
    g_free (buffer);

    if (!dataptr)
        return FALSE;

    return dbx_read_mail_body (m, dataptr, bodyfd);
}

static void
dbx_import_file (DbxImporter *m)
{
    EShell *shell;
    EShellBackend *shell_backend;
    EMailSession *session;
    GCancellable *cancellable;
    gchar *filename;
    CamelFolder *folder;
    gint tmpfile;
    gint i;
    gint missing = 0;
    m->status_what = NULL;
    filename = g_filename_from_uri (
        ((EImportTargetURI *)m->target)->uri_src, NULL, NULL);

    /* Destination folder, was set in our widget */
    m->parent_uri = g_strdup (((EImportTargetURI *)m->target)->uri_dest);

    cancellable = e_activity_get_cancellable (m->base.activity);

    /* XXX Dig up the EMailSession from the default EShell.
     *     Since the EImport framework doesn't allow for user
     *     data, I don't see how else to get to it. */
    shell = e_shell_get_default ();
    shell_backend = e_shell_get_backend_by_name (shell, "mail");
    session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));

    camel_operation_push_message (NULL, _("Importing '%s'"), filename);
    folder = e_mail_session_uri_to_folder_sync (
        session, m->parent_uri, CAMEL_STORE_FOLDER_CREATE,
        cancellable, &m->base.error);
    if (!folder)
        return;
    d(printf("importing to %s\n", camel_folder_get_full_name(folder)));

    camel_folder_freeze (folder);

    filename = g_filename_from_uri (
        ((EImportTargetURI *)m->target)->uri_src, NULL, NULL);
    m->dbx_fd = g_open (filename, O_RDONLY, 0);
    g_free (filename);

    if (m->dbx_fd == -1) {
        g_set_error (
            &m->base.error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
            "Failed to open import file");
        goto out;
    }

    if (!dbx_load_indices (m))
        goto out;

    tmpfile = e_mkstemp("dbx-import-XXXXXX");
    if (tmpfile == -1) {
        g_set_error (
            &m->base.error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
            "Failed to create temporary file for import");
        goto out;
    }

    for (i = 0; i < m->index_count; i++) {
        CamelMessageInfo *info;
        CamelMimeMessage *msg;
        CamelMimeParser *mp;
        gint dbx_flags = 0;
        gint flags = 0;
        gboolean success;

        camel_operation_progress (NULL, 100 * i / m->index_count);
        camel_operation_progress (cancellable, 100 * i / m->index_count);

        if (!dbx_read_email (m, m->indices[i], tmpfile, &dbx_flags)) {
            d(printf("Cannot read email index %d at %x\n",
                 i, m->indices[i]));
            if (m->base.error != NULL)
                goto out;
            missing++;
            continue;
        }
        if (dbx_flags & 0x40)
            flags |= CAMEL_MESSAGE_DELETED;
        if (dbx_flags & 0x80)
            flags |= CAMEL_MESSAGE_SEEN;
        if (dbx_flags & 0x80000)
            flags |= CAMEL_MESSAGE_ANSWERED;

        mp = camel_mime_parser_new ();

        lseek (tmpfile, 0, SEEK_SET);
        camel_mime_parser_init_with_fd (mp, tmpfile);

        msg = camel_mime_message_new ();
        if (!camel_mime_part_construct_from_parser_sync (
            (CamelMimePart *)msg, mp, NULL, NULL)) {
            /* set exception? */
            g_object_unref (msg);
            g_object_unref (mp);
            break;
        }

        info = camel_message_info_new (NULL);
        camel_message_info_set_flags (info, flags, ~0);
        success = camel_folder_append_message_sync (
            folder, msg, info, NULL,
            cancellable, &m->base.error);
        camel_message_info_free (info);
        g_object_unref (msg);

        if (!success) {
            g_object_unref (mp);
            break;
        }
    }
 out:
    if (m->dbx_fd != -1)
        close (m->dbx_fd);
    if (m->indices)
        g_free (m->indices);
    /* FIXME Not passing GCancellable or GError here. */
    camel_folder_synchronize_sync (folder, FALSE, NULL, NULL);
    camel_folder_thaw (folder);
    g_object_unref (folder);
    if (missing && m->base.error == NULL) {
        g_set_error (
            &m->base.error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
            "%d messages imported correctly; %d message "
            "bodies were not present in the DBX file",
            m->index_count - missing, missing);
    }
    camel_operation_pop_message (NULL);
}

static void
dbx_import_import (DbxImporter *m,
                   GCancellable *cancellable,
                   GError **error)
{
    dbx_import_file (m);
}

static void
dbx_import_imported (DbxImporter *m)
{
    e_import_complete (m->target->import, (EImportTarget *)m->target);
}

static void
dbx_import_free (DbxImporter *m)
{
    g_free (m->status_what);
    g_mutex_free (m->status_lock);

    g_source_remove (m->status_timeout_id);
    m->status_timeout_id = 0;

    g_free (m->folder_name);
    g_free (m->folder_uri);
    g_free (m->parent_uri);

    g_object_unref (m->import);
}

static MailMsgInfo dbx_import_info = {
    sizeof (DbxImporter),
    (MailMsgDescFunc) dbx_import_describe,
    (MailMsgExecFunc) dbx_import_import,
    (MailMsgDoneFunc) dbx_import_imported,
    (MailMsgFreeFunc) dbx_import_free,
};

static gboolean
dbx_status_timeout (gpointer data)
{
    DbxImporter *importer = data;
    gint pc;
    gchar *what;

    if (importer->status_what) {
        g_mutex_lock (importer->status_lock);
        what = importer->status_what;
        importer->status_what = NULL;
        pc = importer->status_pc;
        g_mutex_unlock (importer->status_lock);

        e_import_status (
            importer->target->import,
            (EImportTarget *) importer->target, what, pc);
    }

    return TRUE;
}

static void
dbx_status (CamelOperation *op, const gchar *what, gint pc, gpointer data)
{
    DbxImporter *importer = data;

    g_mutex_lock (importer->status_lock);
    g_free (importer->status_what);
    importer->status_what = g_strdup (what);
    importer->status_pc = pc;
    g_mutex_unlock (importer->status_lock);
}

/* Start the main import operation */
void
org_gnome_evolution_readdbx_import (EImport *ei,
                                    EImportTarget *target,
                                    EImportImporter *im)
{
    DbxImporter *m;

    m = mail_msg_new (&dbx_import_info);
    g_datalist_set_data (&target->data, "dbx-msg", m);
    m->import = ei;
    g_object_ref (m->import);
    m->target = target;

    m->parent_uri = NULL;
    m->folder_name = NULL;
    m->folder_uri = NULL;

    m->status_timeout_id = g_timeout_add (100, dbx_status_timeout, m);
    /*m->status_timeout_id = NULL;*/
    m->status_lock = g_mutex_new ();
    m->cancellable = camel_operation_new ();

    g_signal_connect (
        m->cancellable, "status",
        G_CALLBACK (dbx_status), m);

    mail_msg_unordered_push (m);
}

void
org_gnome_evolution_readdbx_cancel (EImport *ei,
                                    EImportTarget *target,
                                    EImportImporter *im)
{
    DbxImporter *m = g_datalist_get_data (&target->data, "dbx-msg");

    if (m) {
        g_cancellable_cancel (m->cancellable);
    }
}

gint
e_plugin_lib_enable (EPlugin *ep, gint enable)
{
    return 0;
}