aboutsummaryrefslogtreecommitdiffstats
path: root/mail/importers/evolution-mbox-importer.c
diff options
context:
space:
mode:
authorIain Holmes <iain@src.gnome.org>2001-03-13 10:26:18 +0800
committerIain Holmes <iain@src.gnome.org>2001-03-13 10:26:18 +0800
commit8c2f3a00ef98717e4203630c8119f2e84d2bb796 (patch)
treef4ec43d8f0e79adffebcfe80de345981a3dbf556 /mail/importers/evolution-mbox-importer.c
parentc807d96e58f7a4c6c2c306f6da26a63b608f840b (diff)
downloadgsoc2013-evolution-8c2f3a00ef98717e4203630c8119f2e84d2bb796.tar
gsoc2013-evolution-8c2f3a00ef98717e4203630c8119f2e84d2bb796.tar.gz
gsoc2013-evolution-8c2f3a00ef98717e4203630c8119f2e84d2bb796.tar.bz2
gsoc2013-evolution-8c2f3a00ef98717e4203630c8119f2e84d2bb796.tar.lz
gsoc2013-evolution-8c2f3a00ef98717e4203630c8119f2e84d2bb796.tar.xz
gsoc2013-evolution-8c2f3a00ef98717e4203630c8119f2e84d2bb796.tar.zst
gsoc2013-evolution-8c2f3a00ef98717e4203630c8119f2e84d2bb796.zip
All my changes to get the folder creation working, and the magic Netscape
importer. svn path=/trunk/; revision=8661
Diffstat (limited to 'mail/importers/evolution-mbox-importer.c')
-rw-r--r--mail/importers/evolution-mbox-importer.c233
1 files changed, 233 insertions, 0 deletions
diff --git a/mail/importers/evolution-mbox-importer.c b/mail/importers/evolution-mbox-importer.c
new file mode 100644
index 0000000000..241055cb10
--- /dev/null
+++ b/mail/importers/evolution-mbox-importer.c
@@ -0,0 +1,233 @@
+/* -*- 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>
+#include <camel/camel-mime-parser.h>
+#include <camel/camel-mime-part.h>
+
+#define MBOX_FACTORY_IID "OAFIID:GNOME_Evolution_Mail_Mbox_ImporterFactory"
+
+typedef struct {
+ MailImporter importer; /* Parent */
+
+ char *filename;
+ int num;
+ CamelMimeParser *mp;
+} 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;
+ gboolean done = FALSE;
+ CamelException *ex;
+
+ ex = camel_exception_new ();
+ if (camel_mime_parser_step (mbi->mp, 0, 0) == HSCAN_FROM) {
+ /* Import the next message */
+ CamelMimeMessage *msg;
+ CamelMessageInfo *info;
+
+ msg = camel_mime_message_new ();
+ if (camel_mime_part_construct_from_parser (CAMEL_MIME_PART (msg),
+ mbi->mp) == -1) {
+ g_warning ("Failed message %d", mbi->num);
+ camel_object_unref (CAMEL_OBJECT (msg));
+ done = TRUE;
+ }
+
+ /* write the mesg */
+ info = g_new0 (CamelMessageInfo, 1);
+ camel_folder_append_message (importer->folder, msg, info, ex);
+ g_free (info);
+ camel_object_unref (CAMEL_OBJECT (msg));
+ if (camel_exception_is_set (ex)) {
+ g_warning ("Failed message %d", mbi->num);
+ done = TRUE;
+ }
+ } else {
+ /* all messages have now been imported */
+ camel_folder_sync (importer->folder, FALSE, ex);
+ camel_folder_thaw (importer->folder);
+ importer->frozen = FALSE;
+ done = TRUE;
+ }
+
+ camel_exception_free (ex);
+ GNOME_Evolution_ImporterListener_notifyResult (listener,
+ GNOME_Evolution_ImporterListener_OK,
+ !done, ev);
+ return;
+}
+
+static gboolean
+support_format_fn (EvolutionImporter *importer,
+ const char *filename,
+ void *closure)
+{
+ char signature[6];
+ gboolean ret = FALSE;
+ int fd, n;
+
+ fd = open (filename, O_RDONLY);
+ if (fd == -1)
+ return FALSE;
+
+ n = read (fd, signature, 5);
+ if (n > 0) {
+ signature[n] = '\0';
+ if (!g_strncasecmp (signature, "From ", 5))
+ ret = TRUE;
+ }
+
+ close (fd);
+
+ return ret;
+}
+
+static void
+importer_destroy_cb (GtkObject *object,
+ MboxImporter *mbi)
+{
+ MailImporter *importer;
+
+ importer = (MailImporter *) mbi;
+ if (importer->frozen) {
+ camel_folder_sync (importer->folder, FALSE, NULL);
+ camel_folder_thaw (importer->folder);
+ }
+
+ if (importer->folder)
+ camel_object_unref (CAMEL_OBJECT (importer->folder));
+
+ g_free (mbi->filename);
+ if (mbi->mp)
+ camel_object_unref (CAMEL_OBJECT (mbi->mp));
+
+ g_free (mbi);
+}
+
+static gboolean
+load_file_fn (EvolutionImporter *eimporter,
+ const char *filename,
+ void *closure)
+{
+ MboxImporter *mbi;
+ MailImporter *importer;
+ int fd;
+
+ mbi = (MboxImporter *) closure;
+ importer = (MailImporter *) mbi;
+
+ mbi->filename = g_strdup (filename);
+
+ fd = open (filename, O_RDONLY);
+ if (fd == -1) {
+ g_warning ("Cannot open file");
+ return FALSE;
+ }
+
+ mbi->mp = camel_mime_parser_new ();
+ camel_mime_parser_scan_from (mbi->mp, TRUE);
+ if (camel_mime_parser_init_with_fd (mbi->mp, fd) == -1) {
+ g_warning ("Unable to process spool folder");
+ goto fail;
+ }
+
+ importer->mstream = NULL;
+ importer->folder = mail_tool_get_local_inbox (NULL);
+
+ if (importer->folder == NULL){
+ g_print ("Bad folder\n");
+ goto fail;
+ }
+
+ camel_folder_freeze (importer->folder);
+ importer->frozen = TRUE;
+
+ g_warning ("Okay, so everything is now ready to import that mbox file!");
+ return TRUE;
+
+ fail:
+ camel_object_unref (CAMEL_OBJECT (mbi->mp));
+ mbi->mp = NULL;
+
+ return FALSE;
+}
+
+static 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);
+}
+
+/* Entry point */
+void
+mail_importer_module_init (void)
+{
+ static gboolean initialised = FALSE;
+ BonoboGenericFactory *factory;
+
+ if (initialised == TRUE)
+ return;
+
+ factory = bonobo_generic_factory_new (MBOX_FACTORY_IID,
+ mbox_factory_fn, NULL);
+
+ if (factory == NULL)
+ g_warning ("Could not initialise Outlook importer factory.");
+
+ initialised = TRUE;
+}
+