aboutsummaryrefslogtreecommitdiffstats
path: root/mail/importers/evolution-mbox-importer.c
blob: 804a13754367e1c2963a2957cbeff3c5dfd772f3 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* evolution-mbox-importer.c
 *
 * Authors: Iain Holmes <iain@ximian.com>
 *      Michael Zucchi <notzed@ximian.com>
 *
 * Copyright (C) 2001 Ximian, Inc.
 * Copyright (C) 2004 Novell, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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

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

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#include <gtk/gtkhbox.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtklabel.h>

#include <glib/gi18n.h>

#include <camel/camel-exception.h>

#include "mail/em-folder-selection-button.h"

#include "mail/mail-component.h"
#include "mail/mail-mt.h"

#include "mail-importer.h"

#include "e-util/e-import.h"

typedef struct {
    EImport *import;
    EImportTarget *target;

    GMutex *status_lock;
    char *status_what;
    int status_pc;
    int status_timeout_id;
    CamelOperation *cancel; /* cancel/status port */

    char *uri;
} MboxImporter;

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));
}

static GtkWidget *
mbox_getwidget(EImport *ei, EImportTarget *target, EImportImporter *im)
{
    GtkWidget *hbox, *w;
    
    hbox = gtk_hbox_new(FALSE, 0);

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

    w = em_folder_selection_button_new(_("Select folder"), _("Select folder to import into"));
    em_folder_selection_button_set_selection((EMFolderSelectionButton *)w,
                         mail_component_get_folder_uri(NULL, MAIL_COMPONENT_FOLDER_INBOX));
    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);

    return w;
}

static gboolean
mbox_supported(EImport *ei, EImportTarget *target, EImportImporter *im)
{
    char signature[6];
    gboolean ret = FALSE;
    int fd, n;
    EImportTargetURI *s;

    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;

    fd = open(s->uri_src + strlen("file://"), O_RDONLY);
    if (fd != -1) {
        n = read(fd, signature, 5);
        ret = n == 5 && memcmp(signature, "From ", 5) == 0;
        close(fd);
    }

    return ret; 
}

static void
mbox_status(CamelOperation *op, const char *what, int pc, void *data)
{
    MboxImporter *importer = data;

    if (pc == CAMEL_OPERATION_START)
        pc = 0;
    else if (pc == CAMEL_OPERATION_END)
        pc = 100;

    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);
}

static gboolean
mbox_status_timeout(void *data)
{
    MboxImporter *importer = data;
    int pc;
    char *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->import, (EImportTarget *)importer->target, what, pc);
    }

    return TRUE;
}

static void
mbox_import_done(void *data, CamelException *ex)
{
    MboxImporter *importer = data;

    g_source_remove(importer->status_timeout_id);
    g_free(importer->status_what);
    g_mutex_free(importer->status_lock);
    camel_operation_unref(importer->cancel);

    e_import_complete(importer->import, importer->target);
    g_free(importer);
}

static void
mbox_import(EImport *ei, EImportTarget *target, EImportImporter *im)
{
    MboxImporter *importer;

    /* TODO: do we validate target? */

    importer = g_malloc0(sizeof(*importer));
    g_datalist_set_data(&target->data, "mbox-data", importer);
    importer->import = ei;
    importer->target = target;
    importer->status_lock = g_mutex_new();
    importer->status_timeout_id = g_timeout_add(100, mbox_status_timeout, importer);
    importer->cancel = camel_operation_new(mbox_status, importer);

    mail_importer_import_mbox(((EImportTargetURI *)target)->uri_src+strlen("file://"), ((EImportTargetURI *)target)->uri_dest, importer->cancel, mbox_import_done, importer);
}

static void
mbox_cancel(EImport *ei, EImportTarget *target, EImportImporter *im)
{
    MboxImporter *importer = g_datalist_get_data(&target->data, "mbox-data");

    if (importer)
        camel_operation_cancel(importer->cancel);
}

static EImportImporter mbox_importer = {
    E_IMPORT_TARGET_URI,
    0,
    mbox_supported,
    mbox_getwidget,
    mbox_import,
    mbox_cancel,
};

EImportImporter *
mbox_importer_peek(void)
{
    mbox_importer.name = _("Berkeley Mailbox (mbox)");
    mbox_importer.description = _("Importer Berkeley Mailbox format folders");

    return &mbox_importer;
}