diff options
-rw-r--r-- | mail/ChangeLog | 9 | ||||
-rw-r--r-- | mail/Makefile.am | 2 | ||||
-rw-r--r-- | mail/em-migrate.c | 536 | ||||
-rw-r--r-- | mail/em-migrate.h | 40 | ||||
-rw-r--r-- | mail/mail-component.c | 87 |
5 files changed, 626 insertions, 48 deletions
diff --git a/mail/ChangeLog b/mail/ChangeLog index ec638640d7..5fd8347b08 100644 --- a/mail/ChangeLog +++ b/mail/ChangeLog @@ -1,3 +1,12 @@ +2003-11-05 Jeffrey Stedfast <fejj@ximian.com> + + * em-migrate.[c,h]: New source files to migrate from the old mail + directory to the new mail directory. + + * mail-component.c (mail_component_init): Changed to use + ~/.evolution and added code to migrate the old mail folders over + if ~/.evolution/mail does not yet exist. + 2003-11-03 Ettore Perazzoli <ettore@ximian.com> * GNOME_Evolution_Mail.server.in.in: Use "evolution2:config_item" diff --git a/mail/Makefile.am b/mail/Makefile.am index d0bd2eb38f..6b79d2f7e7 100644 --- a/mail/Makefile.am +++ b/mail/Makefile.am @@ -98,6 +98,8 @@ libevolution_mail_la_SOURCES = \ em-marshal.h \ em-message-browser.c \ em-message-browser.h \ + em-migrate.c \ + em-migrate.h \ em-composer-utils.c \ em-composer-utils.h \ em-popup.c \ diff --git a/mail/em-migrate.c b/mail/em-migrate.c new file mode 100644 index 0000000000..4a4c31f904 --- /dev/null +++ b/mail/em-migrate.c @@ -0,0 +1,536 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Authors: Jeffrey Stedfast <fejj@ximian.com> + * + * Copyright 2003 Ximian, Inc. (www.ximian.com) + * + * 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 Street #330, Boston, MA 02111-1307, USA. + * + */ + + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <dirent.h> +#include <errno.h> + +#include <gtk/gtk.h> + +#include <camel/camel.h> +#include <camel/camel-session.h> +#include <camel/camel-file-utils.h> + +#include <libxml/tree.h> +#include <libxml/parser.h> +#include <libxml/xmlmemory.h> + +#include "mail-component.h" + +#include "em-migrate.h" + + +#define EM_MIGRATE_SESSION_TYPE (em_migrate_session_get_type ()) +#define EM_MIGRATE_SESSION(obj) (CAMEL_CHECK_CAST((obj), EM_MIGRATE_SESSION_TYPE, EMMigrateSession)) +#define EM_MIGRATE_SESSION_CLASS(k) (CAMEL_CHECK_CLASS_CAST ((k), EM_MIGRATE_SESSION_TYPE, EMMigrateSessionClass)) +#define EM_MIGRATE_IS_SESSION(o) (CAMEL_CHECK_TYPE((o), EM_MIGRATE_SESSION_TYPE)) + +typedef struct _EMMigrateSession { + CamelSession parent_object; + + CamelStore *store; /* new folder tree store */ + char *srcdir; /* old folder tree path */ +} EMMigrateSession; + +typedef struct _EMMigrateSessionClass { + CamelSessionClass parent_class; + +} EMMigrateSessionClass; + +static CamelType em_migrate_session_get_type (void); +static CamelSession *em_migrate_session_new (const char *path); + +static void +class_init (EMMigrateSessionClass *klass) +{ + ; +} + +static CamelType +em_migrate_session_get_type (void) +{ + static CamelType type = CAMEL_INVALID_TYPE; + + if (type == CAMEL_INVALID_TYPE) { + type = camel_type_register ( + camel_session_get_type (), + "EMMigrateSession", + sizeof (EMMigrateSession), + sizeof (EMMigrateSessionClass), + (CamelObjectClassInitFunc) class_init, + NULL, + NULL, + NULL); + } + + return type; +} + +static CamelSession * +em_migrate_session_new (const char *path) +{ + CamelSession *session; + + session = CAMEL_SESSION (camel_object_new (EM_MIGRATE_SESSION_TYPE)); + + camel_session_construct (session, path); + + return session; +} + + +static GtkWidget *window; +static GtkLabel *label; +static GtkProgressBar *progress; + +static void +em_migrate_setup_progress_dialog (void) +{ + GtkWidget *vbox, *hbox, *w; + + window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_title ((GtkWindow *) window, _("Migrating...")); + gtk_window_set_modal ((GtkWindow *) window, TRUE); + gtk_container_set_border_width ((GtkContainer *) window, 6); + + vbox = gtk_vbox_new (FALSE, 6); + gtk_widget_show (vbox); + gtk_container_add ((GtkContainer *) window, vbox); + + w = gtk_label_new (_("The location and hierarchy of the Evolution mailbox " + "folders has changed since Evolution 1.x.\n\nPlease be " + "patient while Evolution migrates your folders...")); + gtk_label_set_line_wrap ((GtkLabel *) w, TRUE); + gtk_widget_show (w); + gtk_box_pack_start_defaults ((GtkBox *) vbox, w); + + hbox = gtk_hbox_new (FALSE, 6); + gtk_widget_show (hbox); + gtk_box_pack_start_defaults ((GtkBox *) vbox, hbox); + + label = (GtkLabel *) gtk_label_new (""); + gtk_widget_show ((GtkWidget *) label); + gtk_box_pack_start_defaults ((GtkBox *) hbox, (GtkWidget *) label); + + progress = (GtkProgressBar *) gtk_progress_bar_new (); + gtk_widget_show ((GtkWidget *) progress); + gtk_box_pack_start_defaults ((GtkBox *) hbox, (GtkWidget *) progress); + + gtk_widget_show (window); +} + +static void +em_migrate_close_progress_dialog (void) +{ + gtk_widget_destroy ((GtkWidget *) window); +} + +static void +em_migrate_set_folder_name (const char *folder_name) +{ + char *text; + + text = g_strdup_printf (_("Migrating `%s':"), folder_name); + gtk_label_set_text (label, text); + g_free (text); + + gtk_progress_bar_set_fraction (progress, 0.0); + + while (gtk_events_pending ()) + gtk_main_iteration (); +} + +static void +em_migrate_set_progress (double percent) +{ + char text[5]; + + snprintf (text, sizeof (text), "%d%%", (int) (percent * 100.0f)); + + gtk_progress_bar_set_fraction (progress, percent); + gtk_progress_bar_set_text (progress, text); + + while (gtk_events_pending ()) + gtk_main_iteration (); +} + + +static gboolean +is_mail_folder (const char *metadata) +{ + xmlNodePtr node; + xmlDocPtr doc; + char *type; + + if (!(doc = xmlParseFile (metadata))) { + g_warning ("Cannot parse `%s'", metadata); + return FALSE; + } + + if (!(node = xmlDocGetRootElement (doc))) { + g_warning ("`%s' corrupt: document contains no root node", metadata); + xmlFreeDoc (doc); + return FALSE; + } + + if (!node->name || strcmp (node->name, "efolder") != 0) { + g_warning ("`%s' corrupt: root node is not 'efolder'", metadata); + xmlFreeDoc (doc); + return FALSE; + } + + node = node->children; + while (node != NULL) { + if (node->name && !strcmp (node->name, "type")) { + type = xmlNodeGetContent (node); + if (!strcmp (type, "mail")) { + xmlFreeDoc (doc); + xmlFree (type); + + return TRUE; + } + + xmlFree (type); + + break; + } + + node = node->next; + } + + xmlFreeDoc (doc); + + return FALSE; +} + +static CamelStore * +get_local_store (CamelSession *session, const char *dirname, const char *metadata, char **namep, int *index, CamelException *ex) +{ + char *protocol, *name, *buf; + CamelStore *store; + struct stat st; + xmlNodePtr node; + xmlDocPtr doc; + + if (stat (metadata, &st) == -1 || !S_ISREG (st.st_mode)) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, "`%s' is not a regular file", metadata); + return NULL; + } + + if (!(doc = xmlParseFile (metadata))) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, "cannot parse `%s'", metadata); + return NULL; + } + + if (!(node = xmlDocGetRootElement (doc)) || strcmp (node->name, "folderinfo") != 0) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, "`%s' is malformed", metadata); + xmlFreeDoc (doc); + return NULL; + } + + node = node->children; + while (node != NULL) { + if (node->name && !strcmp (node->name, "folder")) { + protocol = xmlGetProp (node, "type"); + name = xmlGetProp (node, "name"); + buf = xmlGetProp (node, "index"); + if (buf != NULL) { + *index = atoi (buf); + xmlFree (buf); + } else { + *index = 0; + } + + xmlFreeDoc (doc); + + buf = g_strdup_printf ("%s:%s", protocol, dirname); + xmlFree (protocol); + + if ((store = camel_session_get_store (session, buf, ex))) + *namep = g_strdup (name); + else + *namep = NULL; + + xmlFree (name); + + return store; + } + + node = node->next; + } + + xmlFreeDoc (doc); + + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, "`%s' does not contain needed info", metadata); + + return NULL; +} + +static void +em_migrate_dir (EMMigrateSession *session, const char *dirname, const char *full_name) +{ + guint32 flags = CAMEL_STORE_FOLDER_CREATE; + CamelFolder *old_folder, *new_folder; + CamelStore *local_store; + struct dirent *dent; + CamelException ex; + char *path, *name; + GPtrArray *uids; + struct stat st; + int index, i; + DIR *dir; + + path = g_strdup_printf ("%s/folder-metadata.xml", dirname); + if (stat (path, &st) == -1 || !S_ISREG (st.st_mode)) { + g_free (path); + return; + } + + if (!is_mail_folder (path)) { + g_free (path); + + goto try_subdirs; + } + + g_free (path); + + camel_exception_init (&ex); + + /* get old store & folder */ + path = g_strdup_printf ("%s/local-metadata.xml", dirname); + if (!(local_store = get_local_store ((CamelSession *) session, dirname, path, &name, &index, &ex))) { + g_warning ("error opening old store for `%s': %s", full_name, ex.desc); + camel_exception_clear (&ex); + g_free (path); + + /* try subfolders anyway? */ + goto try_subdirs; + } + + g_free (path); + + if (!(old_folder = camel_store_get_folder (local_store, name, 0, &ex))) { + g_warning ("error opening old folder `%s': %s", full_name, ex.desc); + camel_object_unref (local_store); + camel_exception_clear (&ex); + g_free (name); + + /* try subfolders anyway? */ + goto try_subdirs; + } + + g_free (name); + + flags |= (index ? CAMEL_STORE_FOLDER_BODY_INDEX : 0); + if (!(new_folder = camel_store_get_folder (session->store, full_name, flags, &ex))) { + g_warning ("error creating new mbox folder `%s': %s", full_name, ex.desc); + camel_object_unref (local_store); + camel_object_unref (old_folder); + camel_exception_clear (&ex); + + /* try subfolders anyway? */ + goto try_subdirs; + } + + em_migrate_set_folder_name (full_name); + + uids = camel_folder_get_uids (old_folder); + for (i = 0; i < uids->len; i++) { + CamelMimeMessage *message; + CamelMessageInfo *info; + + if (!(info = camel_folder_get_message_info (old_folder, uids->pdata[i]))) + continue; + + if (!(message = camel_folder_get_message (old_folder, uids->pdata[i], &ex))) { + camel_folder_free_message_info (old_folder, info); + break; + } + + camel_folder_append_message (new_folder, message, info, NULL, &ex); + camel_folder_free_message_info (old_folder, info); + if (camel_exception_is_set (&ex)) + break; + + em_migrate_set_progress (((double) i + 1) / ((double) uids->len)); + } + camel_folder_free_uids (old_folder, uids); + + if (camel_exception_is_set (&ex)) { + g_warning ("error migrating folder `%s': %s", full_name, ex.desc); + camel_object_unref (local_store); + camel_object_unref (old_folder); + camel_object_unref (new_folder); + camel_exception_clear (&ex); + + /* try subfolders anyway? */ + goto try_subdirs; + } + + /*camel_object_unref (local_store);*/ + camel_object_unref (old_folder); + camel_object_unref (new_folder); + + try_subdirs: + + path = g_strdup_printf ("%s/subfolders", dirname); + if (stat (path, &st) == -1 || !S_ISDIR (st.st_mode)) { + g_free (path); + return; + } + + if (!(dir = opendir (path))) { + g_warning ("cannot open `%s': %s", path, strerror (errno)); + g_free (path); + return; + } + + while ((dent = readdir (dir))) { + char *full_path; + + if (dent->d_name[0] == '.') + continue; + + full_path = g_strdup_printf ("%s/%s", path, dent->d_name); + if (stat (full_path, &st) == -1 || !S_ISDIR (st.st_mode)) { + g_free (full_path); + continue; + } + + name = g_strdup_printf ("%s/%s", full_name, dent->d_name); + em_migrate_dir (session, full_path, name); + g_free (full_path); + g_free (name); + } + + closedir (dir); + + g_free (path); +} + +static void +em_migrate_local_folders (EMMigrateSession *session) +{ + struct dirent *dent; + struct stat st; + DIR *dir; + + if (!(dir = opendir (session->srcdir))) { + g_warning ("cannot open `%s': %s", session->srcdir, strerror (errno)); + return; + } + + em_migrate_setup_progress_dialog (); + + while ((dent = readdir (dir))) { + char *full_path; + + if (dent->d_name[0] == '.') + continue; + + full_path = g_strdup_printf ("%s/%s", session->srcdir, dent->d_name); + if (stat (full_path, &st) == -1 || !S_ISDIR (st.st_mode)) { + g_free (full_path); + continue; + } + + em_migrate_dir (session, full_path, dent->d_name); + g_free (full_path); + } + + closedir (dir); + + em_migrate_close_progress_dialog (); +} + + +int +em_migrate (MailComponent *component, CamelException *ex) +{ + const char *evolution_dir; + EMMigrateSession *session; + CamelException lex; + struct stat st; + char *path; + + evolution_dir = mail_component_peek_base_directory (component); + path = g_strdup_printf ("%s/mail", evolution_dir); + if (stat (path, &st) == -1) { + if (errno != ENOENT || camel_mkdir (path, 0777) == -1) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + _("Failed to create directory `%s': %s"), + path, g_strerror (errno)); + g_free (path); + return -1; + } + } + + camel_init (path, TRUE); + session = (EMMigrateSession *) em_migrate_session_new (path); + g_free (path); + + session->srcdir = g_strdup_printf ("%s/evolution/local", g_get_home_dir ()); + + path = g_strdup_printf ("mbox:%s/.evolution/mail/local", g_get_home_dir ()); + if (stat (path + 5, &st) == -1) { + if (errno != ENOENT || camel_mkdir (path + 5, 0777) == -1) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + _("Failed to create directory `%s': %s"), + path + 5, g_strerror (errno)); + g_free (session->srcdir); + camel_object_unref (session); + g_free (path); + return -1; + } + } + + camel_exception_init (&lex); + if (!(session->store = camel_session_get_store ((CamelSession *) session, path, &lex))) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + _("Failed to open store for `%s': %s"), + path, lex.desc); + g_free (session->srcdir); + camel_object_unref (session); + camel_exception_clear (&lex); + g_free (path); + return -1; + } + g_free (path); + + em_migrate_local_folders (session); + + camel_object_unref (session->store); + g_free (session->srcdir); + + camel_object_unref (session); + + return 0; +} diff --git a/mail/em-migrate.h b/mail/em-migrate.h new file mode 100644 index 0000000000..b4cf51e725 --- /dev/null +++ b/mail/em-migrate.h @@ -0,0 +1,40 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Authors: Jeffrey Stedfast <fejj@ximian.com> + * + * Copyright 2003 Ximian, Inc. (www.ximian.com) + * + * 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 Street #330, Boston, MA 02111-1307, USA. + * + */ + + +#ifndef __EM_MIGRATE_H__ +#define __EM_MIGRATE_H__ + +#include <camel/camel-exception.h> + +#ifdef __cplusplus +extern "C" { +#pragma } +#endif /* __cplusplus */ + +int em_migrate (struct _MailComponent *component, CamelException *ex); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __EM_MIGRATE_H__ */ diff --git a/mail/mail-component.c b/mail/mail-component.c index 20e956cebd..afb769564b 100644 --- a/mail/mail-component.c +++ b/mail/mail-component.c @@ -25,6 +25,7 @@ #include <config.h> #endif +#include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> @@ -52,6 +53,7 @@ #include "em-popup.h" #include "em-utils.h" +#include "em-migrate.h" #include <gtk/gtklabel.h> @@ -62,6 +64,7 @@ #include <gal/e-table/e-tree-memory.h> #include <camel/camel.h> +#include <camel/camel-file-utils.h> #include <bonobo/bonobo-control.h> #include <bonobo/bonobo-widget.h> @@ -926,7 +929,7 @@ impl_createControls (PortableServer_Servant servant, GtkWidget *view_widget; BonoboControl *sidebar_control; BonoboControl *view_control; - + browser = e_storage_browser_new (priv->storage_set, "/", create_view_callback, NULL); tree_widget = e_storage_browser_peek_tree_widget (browser); @@ -977,67 +980,55 @@ mail_component_init (MailComponent *component) { MailComponentPrivate *priv; EAccountList *accounts; - + struct stat st; + char *mail_dir; + priv = g_new0 (MailComponentPrivate, 1); component->priv = priv; - - /* EPFIXME: Move to a private directory. */ - /* EPFIXME: Create the directory. */ - priv->base_directory = g_build_filename (g_get_home_dir (), "evolution", NULL); - + + priv->base_directory = g_build_filename (g_get_home_dir (), ".evolution", NULL); + if (camel_mkdir (priv->base_directory, 0777) == -1 && errno != EEXIST) + abort (); + /* EPFIXME: Turn into an object? */ mail_session_init (priv->base_directory); - + priv->async_event = mail_async_event_new(); priv->storages_hash = g_hash_table_new (NULL, NULL); - + priv->folder_type_registry = e_folder_type_registry_new (); priv->storage_set = e_storage_set_new (priv->folder_type_registry); - -#if 0 /* EPFIXME TODO somehow */ - for (i = 0; i < sizeof (standard_folders) / sizeof (standard_folders[0]); i++) - *standard_folders[i].uri = g_strdup_printf ("file://%s/local/%s", evolution_dir, standard_folders[i].name); -#endif + + /* migrate evolution 1.x folders to 2.0's location/format */ + mail_dir = g_strdup_printf ("%s/mail", priv->base_directory); + if (stat (mail_dir, &st) == -1) { + CamelException ex; + + camel_exception_init (&ex); + if (em_migrate (component, &ex) == -1) { + GtkWidget *dialog; + + dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE, + _("Some of your mail folders were unable to be migrated:\n%s"), + camel_exception_get_description (&ex)); + + g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), dialog); + gtk_widget_show (dialog); + + camel_exception_clear (&ex); + } + } + g_free (mail_dir); + setup_local_store (component); - + accounts = mail_config_get_accounts (); load_accounts(component, accounts); - -#if 0 - /* EPFIXME? */ - mail_local_storage_startup (shell_client, evolution_dir); - mail_importer_init (shell_client); - - for (i = 0; i < sizeof (standard_folders) / sizeof (standard_folders[0]); i++) { - mail_msg_wait (mail_get_folder (*standard_folders[i].uri, CAMEL_STORE_FOLDER_CREATE, - got_folder, standard_folders[i].folder, mail_thread_new)); - } -#endif /* mail_autoreceive_setup (); EPFIXME keep it off for testing */ - + setup_search_context (component); - -#if 0 - /* EPFIXME this shouldn't be here. */ - if (mail_config_is_corrupt ()) { - GtkWidget *dialog; - - dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE, - _("Some of your mail settings seem corrupt, " - "please check that everything is in order.")); - g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), dialog); - gtk_widget_show (dialog); - } -#endif - -#if 0 - /* EPFIXME if we nuke the summary this is not necessary anymore. */ - - /* Everything should be ready now */ - evolution_folder_info_notify_ready (); -#endif - + /* EPFIXME not sure about this. */ go_online (component); } |