aboutsummaryrefslogtreecommitdiffstats
path: root/mail/importers/evolution-mbox-importer.c
blob: 8433a1df17abc55ee03eb0b43cc2638e71302be2 (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
/*
 * 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.
 *
 * 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 Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Authors:
 *      Iain Holmes <iain@ximian.com>
 *      Michael Zucchi <notzed@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

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

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

#include "shell/e-shell.h"
#include "shell/e-shell-window.h"
#include "shell/e-shell-view.h"
#include "shell/e-shell-sidebar.h"

#include "mail/e-mail-backend.h"
#include "mail/em-folder-selection-button.h"
#include "mail/em-folder-tree-model.h"
#include "mail/em-folder-tree.h"

#include "mail-importer.h"

typedef struct {
    EImport *import;
    EImportTarget *target;

    GMutex status_lock;
    gchar *status_what;
    gint status_pc;
    gint status_timeout_id;
    GCancellable *cancellable;  /* cancel/status port */

    gchar *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_folder_uri (button));
}

static GtkWidget *
mbox_getwidget (EImport *ei,
                EImportTarget *target,
                EImportImporter *im)
{
    EShell *shell;
    EShellBackend *shell_backend;
    EMailBackend *backend;
    EMailSession *session;
    GtkWindow *window;
    GtkWidget *hbox, *w;
    GtkLabel *label;
    gchar *select_uri = NULL;

    /* XXX Dig up the mail backend 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");

    backend = E_MAIL_BACKEND (shell_backend);
    session = e_mail_backend_get_session (backend);

    /* preselect the folder selected in a mail view */
    window = e_shell_get_active_window (shell);
    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;
            EShellSidebar *shell_sidebar;
            EMFolderTree *folder_tree = NULL;

            shell_view = e_shell_window_get_shell_view (
                shell_window, view);

            shell_sidebar =
                e_shell_view_get_shell_sidebar (shell_view);

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

            select_uri =
                em_folder_tree_get_selected_uri (folder_tree);

            g_object_unref (folder_tree);
        }
    }

    if (!select_uri) {
        const gchar *uri;
        uri = e_mail_session_get_local_folder_uri (
            session, E_MAIL_LOCAL_FOLDER_INBOX);
        select_uri = g_strdup (uri);
    }

    hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);

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

    label = GTK_LABEL (w);

    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_folder_uri (
        EM_FOLDER_SELECTION_BUTTON (w), select_uri);
    folder_selected (
        EM_FOLDER_SELECTION_BUTTON (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_box_new (GTK_ORIENTATION_VERTICAL, 0);
    gtk_box_pack_start ((GtkBox *) w, hbox, FALSE, FALSE, 0);
    gtk_widget_show_all (w);

    g_free (select_uri);

    return w;
}

static gboolean
mbox_supported (EImport *ei,
                EImportTarget *target,
                EImportImporter *im)
{
    gchar signature[6];
    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, 5);
        ret = n == 5 && memcmp (signature, "From ", 5) == 0;
        close (fd);
    }

    return ret;
}

static void
mbox_status (CamelOperation *op,
             const gchar *what,
             gint pc,
             gpointer data)
{
    MboxImporter *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);
}

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

    return TRUE;
}

static void
mbox_import_done (gpointer data,
                  GError **error)
{
    MboxImporter *importer = data;

    g_source_remove (importer->status_timeout_id);
    g_free (importer->status_what);
    g_mutex_clear (&importer->status_lock);
    g_object_unref (importer->cancellable);

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

static void
mbox_import (EImport *ei,
             EImportTarget *target,
             EImportImporter *im)
{
    EShell *shell;
    EShellBackend *shell_backend;
    EMailSession *session;
    MboxImporter *importer;
    gchar *filename;

    /* 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));

    /* 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;
    g_mutex_init (&importer->status_lock);
    importer->status_timeout_id =
        e_named_timeout_add (100, mbox_status_timeout, importer);
    importer->cancellable = camel_operation_new ();

    g_signal_connect (
        importer->cancellable, "status",
        G_CALLBACK (mbox_status), importer);

    filename = g_filename_from_uri (
        ((EImportTargetURI *) target)->uri_src, NULL, NULL);
    mail_importer_import_mbox (
        session, filename, ((EImportTargetURI *) target)->uri_dest,
        importer->cancellable, mbox_import_done, importer);
    g_free (filename);
}

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

    if (importer)
        g_cancellable_cancel (importer->cancellable);
}

static MboxImporterCreatePreviewFunc create_preview_func = NULL;
static MboxImporterFillPreviewFunc fill_preview_func = NULL;

void
mbox_importer_set_preview_funcs (MboxImporterCreatePreviewFunc create_func,
                                 MboxImporterFillPreviewFunc fill_func)
{
    create_preview_func = create_func;
    fill_preview_func = fill_func;
}

static void
preview_selection_changed_cb (GtkTreeSelection *selection,
                              EWebViewPreview *preview)
{
    GtkTreeIter iter;
    GtkTreeModel *model = NULL;
    gboolean found = FALSE;

    g_return_if_fail (selection != NULL);
    g_return_if_fail (preview != NULL);
    g_return_if_fail (fill_preview_func != NULL);

    if (gtk_tree_selection_get_selected (selection, &model, &iter) && model) {
        CamelMimeMessage *msg = NULL;

        gtk_tree_model_get (model, &iter, 2, &msg, -1);

        if (msg) {
            found = TRUE;
            fill_preview_func (G_OBJECT (preview), msg);
            g_object_unref (msg);
        }
    }

    if (!found) {
        e_web_view_preview_begin_update (preview);
        e_web_view_preview_end_update (preview);
    }
}

static GtkWidget *
mbox_get_preview (EImport *ei,
                  EImportTarget *target,
                  EImportImporter *im)
{
    GtkWidget *preview = NULL;
    EImportTargetURI *s = (EImportTargetURI *) target;
    gchar *filename;
    gint fd;
    CamelMimeParser *mp;
    GtkListStore *store = NULL;
    GtkTreeIter iter;
    GtkWidget *preview_widget = NULL;

    if (!create_preview_func || !fill_preview_func)
        return NULL;

    filename = g_filename_from_uri (s->uri_src, NULL, NULL);
    if (!filename) {
        g_message (G_STRLOC ": Couldn't get filename from URI '%s'", s->uri_src);
        return NULL;
    }

    fd = g_open (filename, O_RDONLY | O_BINARY, 0);
    if (fd == -1) {
        g_warning (
            "Cannot find source file to import '%s': %s",
            filename, g_strerror (errno));
        g_free (filename);
        return NULL;
    }

    g_free (filename);

    mp = camel_mime_parser_new ();
    camel_mime_parser_scan_from (mp, TRUE);
    if (camel_mime_parser_init_with_fd (mp, fd) == -1) {
        goto cleanup;
    }

    while (camel_mime_parser_step (mp, NULL, NULL) == CAMEL_MIME_PARSER_STATE_FROM) {
        CamelMimeMessage *msg;
        gchar *from;

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

        if (!store)
            store = gtk_list_store_new (
                3, G_TYPE_STRING, G_TYPE_STRING,
                CAMEL_TYPE_MIME_MESSAGE);

        from = NULL;
        if (camel_mime_message_get_from (msg))
            from = camel_address_encode (
                CAMEL_ADDRESS (
                camel_mime_message_get_from (msg)));

        gtk_list_store_append (store, &iter);
        gtk_list_store_set (
            store, &iter,
            0, camel_mime_message_get_subject (msg) ?
            camel_mime_message_get_subject (msg) : "",
            1, from ? from : "", 2, msg, -1);

        g_object_unref (msg);
        g_free (from);

        camel_mime_parser_step (mp, NULL, NULL);
    }

    if (store) {
        GtkTreeView *tree_view;
        GtkTreeSelection *selection;

        preview = e_web_view_preview_new ();
        gtk_widget_show (preview);

        tree_view = e_web_view_preview_get_tree_view (
            E_WEB_VIEW_PREVIEW (preview));
        if (!tree_view) {
            g_warn_if_reached ();
            gtk_widget_destroy (preview);
            preview = NULL;
            goto cleanup;
        }

        gtk_tree_view_set_model (tree_view, GTK_TREE_MODEL (store));
        g_object_unref (store);

        /* Translators: Column header for a message subject */
        gtk_tree_view_insert_column_with_attributes (
            tree_view, -1, C_("mboxImp", "Subject"),
            gtk_cell_renderer_text_new (), "text", 0, NULL);

        /* Translators: Column header for a message From address */
        gtk_tree_view_insert_column_with_attributes (
            tree_view, -1, C_("mboxImp", "From"),
            gtk_cell_renderer_text_new (), "text", 1, NULL);

        if (gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL) > 1)
            e_web_view_preview_show_tree_view (
                E_WEB_VIEW_PREVIEW (preview));

        create_preview_func (G_OBJECT (preview), &preview_widget);
        if (!preview_widget) {
            g_warn_if_reached ();
            goto cleanup;
        }

        e_web_view_preview_set_preview (
            E_WEB_VIEW_PREVIEW (preview), preview_widget);
        gtk_widget_show (preview_widget);

        selection = gtk_tree_view_get_selection (tree_view);
        if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter)) {
            g_warn_if_reached ();
            goto cleanup;
        }
        gtk_tree_selection_select_iter (selection, &iter);

        g_signal_connect (
            selection, "changed",
            G_CALLBACK (preview_selection_changed_cb), preview);

        preview_selection_changed_cb (
            selection, E_WEB_VIEW_PREVIEW (preview));
    }

 cleanup:
    g_object_unref (mp);

    /* 'fd' is freed together with 'mp' */
    /* coverity[leaked_handle] */
    return preview;
}

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

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

    return &mbox_importer;
}