aboutsummaryrefslogtreecommitdiffstats
path: root/mail/evolution-mbox-importer.c
blob: 3556f2b68f4c29076d1cd1b2740e686540d27727 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* evolution-mbox-importer.c
 *
 * Authors: Iain Holmes <iain@ximian.com>
 *
 * Copyright (C) 2001  Ximian, Inc.
 *
 * 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 Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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

#include <bonobo/bonobo-object.h>
#include <bonobo/bonobo-generic-factory.h>

#include <stdio.h>

#include <importer/evolution-importer.h>
#include <importer/GNOME_Evolution_Importer.h>

#include "mail-importer.h"

#include <camel/camel-exception.h>

typedef struct {
    MailImporter importer; /* Parent */

    char *filename;
    FILE *handle;
    off_t size;
} MboxImporter;


/* EvolutionImporter methods */

static void
process_item_fn (EvolutionImporter *eimporter,
         CORBA_Object listener,
         void *closure,
         CORBA_Environment *ev)
{
    MboxImporter *mbi = (MboxImporter *) closure;
    MailImporter *importer = (MailImporter *) mbi;
    CamelException *ex;
    static char *line = NULL;

    if (line == NULL)
        line = g_new0 (char, 4096);

    if (line != NULL) { 
        /* We had a From line the last time
           so just add it and start again */
        mail_importer_add_line (importer, line, FALSE);
    }

    if (fgets (line, 4096, mbi->handle) == NULL) {
        if (*line != '\0')
            mail_importer_add_line (importer, line, TRUE); 
        /* Must be the end */

        g_free (line);
        line = NULL;
        GNOME_Evolution_ImporterListener_notifyResult (listener,
                                   GNOME_Evolution_ImporterListener_OK,
                                   FALSE, ev);
        
        ex = camel_exception_new ();
        camel_folder_thaw (importer->folder);
        camel_folder_sync (importer->folder, FALSE, ex);
        camel_exception_free (ex);
        fclose (mbi->handle);
        mbi->handle = NULL;
        return;
    }

    mail_importer_add_line (importer, line, FALSE);
    while (strncmp (line, "From ", 5) != 0) {
        mail_importer_add_line (importer, line, FALSE);

        if (fgets (line, 4096, mbi->handle) == NULL) {
            if (*line != '\0')
                mail_importer_add_line (importer, line, TRUE);

            g_free (line);
            line = NULL;
            GNOME_Evolution_ImporterListener_notifyResult (listener,
                                       GNOME_Evolution_ImporterListener_OK,
                                       FALSE, ev);
            ex = camel_exception_new ();
            camel_folder_thaw (importer->folder);
            camel_folder_sync (importer->folder, FALSE, ex);
            camel_exception_free (ex);
            fclose (mbi->handle);
            mbi->handle = NULL;
            return;
        }
    }   
    
    mail_importer_add_line (importer, "\0", TRUE);
    GNOME_Evolution_ImporterListener_notifyResult (listener, 
                               GNOME_Evolution_ImporterListener_OK,
                               TRUE, ev);
        
    return;
}

static gboolean
support_format_fn (EvolutionImporter *importer,
           const char *filename,
           void *closure)
{
    FILE *handle;
    char signature[5];
    
    handle = fopen (filename, "rb");
    if (handle == NULL)
        return FALSE; /* Can't open file: Can't support it :) */
    
    /* SIGNATURE */
    fread (&signature, 5, 1, handle); 
    if (strcmp (signature, "From ") == 0) {
        fclose (handle);
        return TRUE;
    }

    fclose (handle);
    return FALSE; 
}

static void
importer_destroy_cb (GtkObject *object,
             MboxImporter *mbi)
{
    MailImporter *importer;

    importer = (MailImporter *) mbi;
    if (importer->folder)
        camel_object_unref (CAMEL_OBJECT (importer->folder));

    g_free (mbi->filename);
    if (mbi->handle)
        fclose (mbi->handle);

    g_free (mbi);
}

static gboolean
load_file_fn (EvolutionImporter *eimporter,
          const char *filename,
          void *closure)
{
    MboxImporter *mbi;
    MailImporter *importer;
    struct stat buf;

    mbi = (MboxImporter *) closure;
    importer = (MailImporter *) mbi;

    mbi->filename = g_strdup (filename);

    mbi->handle = fopen (filename, "rb");
    if (mbi->handle == NULL) {
        g_warning ("Cannot open file");
        return FALSE;
    }

    /* Get size of file */
    if (stat (filename, &buf) == -1) {
        g_warning ("Cannot stat file");
        return FALSE;
    }
    
    mbi->size = buf.st_size;

    importer->mstream = NULL;
    importer->folder = mail_importer_get_folder ("Inbox", NULL);

    if (importer->folder == NULL){
        g_print ("Bad folder\n");
        return FALSE;
    }

    camel_folder_freeze (importer->folder);
    return TRUE;
}

BonoboObject *
mbox_factory_fn (BonoboGenericFactory *_factory,
         void *closure)
{
    EvolutionImporter *importer;
    MboxImporter *mbox;
    
    mbox = g_new0 (MboxImporter, 1);
    importer = evolution_importer_new (support_format_fn, load_file_fn,
                       process_item_fn, NULL, mbox);
    gtk_signal_connect (GTK_OBJECT (importer), "destroy",
                GTK_SIGNAL_FUNC (importer_destroy_cb), mbox);
    
    return BONOBO_OBJECT (importer);
}