2006-06-17 [colin] 2.3.0cvs20
[claws.git] / src / mh.c
index 63e996fd6f51411c21dca78cd264df806c82eb2d..76a603c159dc68556fd3be11785fdccfb385af57 100644 (file)
--- a/src/mh.c
+++ b/src/mh.c
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2002 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
  *
  * 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
@@ -14,7 +14,7 @@
  *
  * 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.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -24,6 +24,7 @@
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <dirent.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #  include <sys/time.h>
 #endif
 
-#include "intl.h"
 #include "folder.h"
 #include "mh.h"
 #include "procmsg.h"
 #include "procheader.h"
 #include "utils.h"
+#include "codeconv.h"
+#include "statusbar.h"
+#include "gtkutils.h"
+
+/* Define possible missing constants for Windows. */
+#ifdef G_OS_WIN32
+# ifndef S_IRGRP
+# define S_IRGRP 0
+# define S_IWGRP 0
+# endif
+# ifndef S_IROTH
+# define S_IROTH 0
+# define S_IWOTH 0
+# endif
+#endif
 
-static void    mh_folder_init                  (Folder         *folder,
-                                                const gchar    *name,
-                                                const gchar    *path);
 
-static GSList  *mh_get_uncached_msgs           (GHashTable     *msg_table,
-                                                FolderItem     *item);
+static void    mh_folder_init          (Folder         *folder,
+                                        const gchar    *name,
+                                        const gchar    *path);
+
+static Folder  *mh_folder_new          (const gchar    *name,
+                                        const gchar    *path);
+static void     mh_folder_destroy      (Folder         *folder);
+static gchar   *mh_fetch_msg           (Folder         *folder,
+                                        FolderItem     *item,
+                                        gint            num);
+static MsgInfo *mh_get_msginfo         (Folder         *folder,
+                                        FolderItem     *item,
+                                        gint            num);
+static gint     mh_add_msg             (Folder         *folder,
+                                        FolderItem     *dest,
+                                        const gchar    *file,
+                                        MsgFlags       *flags);
+static gint     mh_add_msgs            (Folder         *folder,
+                                        FolderItem     *dest,
+                                        GSList         *file_list,
+                                        GRelation      *relation);
+static gint     mh_copy_msg            (Folder         *folder,
+                                        FolderItem     *dest,
+                                        MsgInfo        *msginfo);
+static gint    mh_copy_msgs            (Folder         *folder, 
+                                        FolderItem     *dest, 
+                                        MsgInfoList    *msglist, 
+                                        GRelation      *relation);
+static gint     mh_remove_msg          (Folder         *folder,
+                                        FolderItem     *item,
+                                        gint            num);
+static gint    mh_remove_msgs          (Folder         *folder, 
+                                        FolderItem     *item, 
+                                        MsgInfoList    *msglist, 
+                                        GRelation      *relation);
+static gint     mh_remove_all_msg      (Folder         *folder,
+                                        FolderItem     *item);
+static gboolean mh_is_msg_changed      (Folder         *folder,
+                                        FolderItem     *item,
+                                        MsgInfo        *msginfo);
+
+static gint    mh_get_num_list         (Folder         *folder,
+                                        FolderItem     *item, 
+                                        GSList         **list, 
+                                        gboolean       *old_uids_valid);
+static gint    mh_scan_tree            (Folder         *folder);
+
+static gint    mh_create_tree          (Folder         *folder);
+static FolderItem *mh_create_folder    (Folder         *folder,
+                                        FolderItem     *parent,
+                                        const gchar    *name);
+static gint    mh_rename_folder                (Folder         *folder,
+                                        FolderItem     *item,
+                                        const gchar    *name);
+static gint    mh_remove_folder                (Folder         *folder,
+                                        FolderItem     *item);
+
+static gchar   *mh_get_new_msg_filename                (FolderItem     *dest);
+
 static MsgInfo *mh_parse_msg                   (const gchar    *file,
                                                 FolderItem     *item);
-static void    mh_scan_tree_recursive          (FolderItem     *item, 
-                                                GHashTable *pptable);
+static void    mh_remove_missing_folder_items  (Folder         *folder);
+static gchar   *mh_filename_from_utf8          (const gchar    *path);
+static gchar   *mh_filename_to_utf8            (const gchar    *path);
+static void    mh_scan_tree_recursive          (FolderItem     *item);
 
 static gboolean mh_rename_folder_func          (GNode          *node,
                                                 gpointer        data);
+static gchar   *mh_item_get_path               (Folder *folder, 
+                                                FolderItem *item);
+
+static gboolean mh_scan_required       (Folder         *folder,
+                                        FolderItem     *item);
+static int mh_item_close               (Folder         *folder,
+                                        FolderItem     *item);
+#if 0
+static gint mh_get_flags               (Folder *folder, FolderItem *item,
+                                        MsgInfoList *msginfo_list, GRelation *msgflags);
+#endif
+static void mh_write_sequences         (FolderItem     *item, gboolean remove_unseen);
+
+static FolderClass mh_class;
 
+FolderClass *mh_get_class(void)
+{
+       if (mh_class.idstr == NULL) {
+               mh_class.type = F_MH;
+               mh_class.idstr = "mh";
+               mh_class.uistr = "MH";
+               
+               /* Folder functions */
+               mh_class.new_folder = mh_folder_new;
+               mh_class.destroy_folder = mh_folder_destroy;
+               mh_class.set_xml = folder_local_set_xml;
+               mh_class.get_xml = folder_local_get_xml;
+               mh_class.scan_tree = mh_scan_tree;
+               mh_class.create_tree = mh_create_tree;
+
+               /* FolderItem functions */
+               mh_class.item_get_path = mh_item_get_path;
+               mh_class.create_folder = mh_create_folder;
+               mh_class.rename_folder = mh_rename_folder;
+               mh_class.remove_folder = mh_remove_folder;
+               mh_class.get_num_list = mh_get_num_list;
+               mh_class.scan_required = mh_scan_required;
+               mh_class.close = mh_item_close;
+               mh_class.get_flags = NULL; /*mh_get_flags */;
+
+               /* Message functions */
+               mh_class.get_msginfo = mh_get_msginfo;
+               mh_class.fetch_msg = mh_fetch_msg;
+               mh_class.add_msg = mh_add_msg;
+               mh_class.add_msgs = mh_add_msgs;
+               mh_class.copy_msg = mh_copy_msg;
+               mh_class.copy_msgs = mh_copy_msgs;
+               mh_class.remove_msg = mh_remove_msg;
+               mh_class.remove_msgs = mh_remove_msgs;
+               mh_class.remove_all_msg = mh_remove_all_msg;
+               mh_class.is_msg_changed = mh_is_msg_changed;
+       }
 
-Folder *mh_folder_new(const gchar *name, const gchar *path)
+       return &mh_class;
+}
+
+static Folder *mh_folder_new(const gchar *name, const gchar *path)
 {
        Folder *folder;
 
        folder = (Folder *)g_new0(MHFolder, 1);
+       folder->klass = &mh_class;
        mh_folder_init(folder, name, path);
 
        return folder;
 }
 
-void mh_folder_destroy(MHFolder *folder)
+static void mh_folder_destroy(Folder *folder)
 {
        folder_local_folder_destroy(LOCAL_FOLDER(folder));
 }
@@ -77,580 +203,393 @@ static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
 {
        folder_local_folder_init(folder, name, path);
 
-       folder->type = F_MH;
-
-       folder->get_msg_list        = mh_get_msg_list;
-       folder->fetch_msg           = mh_fetch_msg;
-       folder->add_msg             = mh_add_msg;
-       folder->move_msg            = mh_move_msg;
-       folder->move_msgs_with_dest = mh_move_msgs_with_dest;
-       folder->copy_msg            = mh_copy_msg;
-       folder->copy_msgs_with_dest = mh_copy_msgs_with_dest;
-       folder->remove_msg          = mh_remove_msg;
-       folder->remove_all_msg      = mh_remove_all_msg;
-       folder->is_msg_changed      = mh_is_msg_changed;
-       folder->scan                = mh_scan_folder;
-       folder->scan_tree           = mh_scan_tree;
-       folder->create_tree         = mh_create_tree;
-       folder->create_folder       = mh_create_folder;
-       folder->rename_folder       = mh_rename_folder;
-       folder->remove_folder       = mh_remove_folder;
 }
 
-GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
+gboolean mh_scan_required(Folder *folder, FolderItem *item)
 {
-       GSList *mlist;
-       GHashTable *msg_table;
        gchar *path;
        struct stat s;
-       gboolean scan_new = TRUE;
-#ifdef MEASURE_TIME
-       struct timeval tv_before, tv_after, tv_result;
-
-       gettimeofday(&tv_before, NULL);
-#endif
-
-       g_return_val_if_fail(item != NULL, NULL);
 
        path = folder_item_get_path(item);
+       g_return_val_if_fail(path != NULL, FALSE);
+
        if (stat(path, &s) < 0) {
                FILE_OP_ERROR(path, "stat");
-       } else {
-               time_t mtime;
+               g_free(path);
+               return FALSE;
+       }
 
-               mtime = MAX(s.st_mtime, s.st_ctime);
-               if (item->mtime == mtime) {
-                       debug_print("Folder is not modified.\n");
-                       scan_new = FALSE;
-               } else
-                       item->mtime = mtime;
+       if ((s.st_mtime > item->mtime) &&
+               (s.st_mtime - 3600 != item->mtime)) {
+               debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
+                           path,
+                           (long int) s.st_mtime,
+                           (long int) item->mtime);
+               g_free(path);
+               return TRUE;
        }
+
+       debug_print("MH scan not required: %s (%ld <= %ld)\n",
+                   path,
+                   (long int) s.st_mtime,
+                   (long int) item->mtime);
        g_free(path);
+       return FALSE;
+}
 
-       if (use_cache && !scan_new) {
-               mlist = procmsg_read_cache(item, FALSE);
-               if (!mlist)
-                       mlist = mh_get_uncached_msgs(NULL, item);
-       } else if (use_cache) {
-               GSList *newlist;
+void mh_get_last_num(Folder *folder, FolderItem *item)
+{
+       gchar *path;
+       DIR *dp;
+       struct dirent *d;
+       gint max = 0;
+       gint num;
 
-               mlist = procmsg_read_cache(item, TRUE);
-               msg_table = procmsg_msg_hash_table_create(mlist);
+       g_return_if_fail(item != NULL);
 
-               newlist = mh_get_uncached_msgs(msg_table, item);
-               if (msg_table)
-                       g_hash_table_destroy(msg_table);
+       debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
 
-               mlist = g_slist_concat(mlist, newlist);
-       } else
-               mlist = mh_get_uncached_msgs(NULL, item);
+       path = folder_item_get_path(item);
+       g_return_if_fail(path != NULL);
+       if (change_dir(path) < 0) {
+               g_free(path);
+               return;
+       }
+       g_free(path);
 
-       procmsg_set_flags(mlist, item);
+       if ((dp = opendir(".")) == NULL) {
+               FILE_OP_ERROR(item->path, "opendir");
+               return;
+       }
 
-#ifdef MEASURE_TIME
-       gettimeofday(&tv_after, NULL);
+       while ((d = readdir(dp)) != NULL) {
+               if ((num = to_number(d->d_name)) > 0 &&
+                   dirent_is_regular_file(d)) {
+                       if (max < num)
+                               max = num;
+               }
+       }
+       closedir(dp);
 
-       timersub(&tv_after, &tv_before, &tv_result);
-       g_print("mh_get_msg_list: %s: elapsed time: %ld.%06ld sec\n",
-               item->path, tv_result.tv_sec, tv_result.tv_usec);
-#endif
+       debug_print("Last number in dir %s = %d\n", item->path, max);
+       item->last_num = max;
+}
 
-       return mlist;
+gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
+{
+
+       gchar *path;
+       DIR *dp;
+       struct dirent *d;
+       gint num, nummsgs = 0;
+
+       g_return_val_if_fail(item != NULL, -1);
+
+       debug_print("mh_get_num_list(): Scanning %s ...\n", item->path);
+
+       *old_uids_valid = TRUE;
+
+       path = folder_item_get_path(item);
+       g_return_val_if_fail(path != NULL, -1);
+       if (change_dir(path) < 0) {
+               g_free(path);
+               return -1;
+       }
+       g_free(path);
+
+       if ((dp = opendir(".")) == NULL) {
+               FILE_OP_ERROR(item->path, "opendir");
+               return -1;
+       }
+
+       while ((d = readdir(dp)) != NULL) {
+               if ((num = to_number(d->d_name)) > 0) {
+                       *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
+                       nummsgs++;
+               }
+       }
+       closedir(dp);
+
+       item->mtime = time(NULL);
+       return nummsgs;
 }
 
-gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
+static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
 {
        gchar *path;
        gchar *file;
 
        g_return_val_if_fail(item != NULL, NULL);
-       g_return_val_if_fail(num > 0 && num <= item->last_num, NULL);
+       g_return_val_if_fail(num > 0, NULL);
 
        path = folder_item_get_path(item);
        file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
-       g_free(path);
+
        if (!is_file_exist(file)) {
                g_free(file);
+               g_free(path);
                return NULL;
        }
-
+       g_free(path);
        return file;
 }
 
-gchar *mh_get_newmsg_filename(FolderItem *dest)
+static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
+{
+       MsgInfo *msginfo;
+       gchar *file;
+
+       g_return_val_if_fail(item != NULL, NULL);
+       if (num <= 0)
+               return NULL;
+
+       file = mh_fetch_msg(folder, item, num);
+       if (!file) return NULL;
+
+       msginfo = mh_parse_msg(file, item);
+       if (msginfo)
+               msginfo->msgnum = num;
+
+       g_free(file);
+
+       return msginfo;
+}
+
+static gchar *mh_get_new_msg_filename(FolderItem *dest)
 {
        gchar *destfile;
        gchar *destpath;
-       gboolean found = FALSE;
 
        destpath = folder_item_get_path(dest);
        g_return_val_if_fail(destpath != NULL, NULL);
+
        if (!is_dir_exist(destpath))
                make_dir_hier(destpath);
 
-       do {
+       for (;;) {
                destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
                                           dest->last_num + 1);
-               if(is_file_exist(destfile)) {
+               if (is_file_entry_exist(destfile)) {
                        dest->last_num++;
                        g_free(destfile);
-               } else {
-                       found = TRUE;
-               }
-       } while(!found);
+               } else
+                       break;
+       }
+
        g_free(destpath);
 
        return destfile;
 }
 
-#define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
-{ \
-       MsgInfo newmsginfo; \
- \
-       newmsginfo.msgnum = dest->last_num; \
-       newmsginfo.flags = msginfo->flags; \
-       if (dest->stype == F_OUTBOX || \
-           dest->stype == F_QUEUE  || \
-           dest->stype == F_DRAFT  || \
-           dest->stype == F_TRASH) \
-               MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
-                                    MSG_NEW|MSG_UNREAD|MSG_DELETED); \
- \
-       procmsg_write_flags(&newmsginfo, fp); \
-}
-
-gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
-               gboolean remove_source)
+static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
 {
-       gchar *destfile;
+       gint ret;
+       GSList file_list;
+       MsgFileInfo fileinfo;
 
-       g_return_val_if_fail(dest != NULL, -1);
        g_return_val_if_fail(file != NULL, -1);
 
-       if (dest->last_num < 0) {
-               mh_scan_folder(folder, dest);
-               if (dest->last_num < 0) return -1;
-       }
-
-       destfile = mh_get_newmsg_filename(dest);
-       if(!destfile) return -1;
-
-       if (link(file, destfile) < 0) {
-               if (copy_file(file, destfile) < 0) {
-                       g_warning(_("can't copy message %s to %s\n"),
-                                 file, destfile);
-                       g_free(destfile);
-                       return -1;
-               }
-       }
-
-       if (remove_source) {
-               if (unlink(file) < 0)
-                       FILE_OP_ERROR(file, "unlink");
-       }
-
-       g_free(destfile);
-       dest->last_num++;
-       return dest->last_num;
-}
-
-static gint mh_do_move(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
-{
-       gchar *destdir;
-       gchar *srcfile;
-       gchar *destfile;
-       FILE *fp;
-       gint filemode = 0;
-       PrefsFolderItem *prefs;
-
-       g_return_val_if_fail(dest != NULL, -1);
-       g_return_val_if_fail(msginfo != NULL, -1);
-
-       if (msginfo->folder == dest) {
-               g_warning(_("the src folder is identical to the dest.\n"));
-               return -1;
-       }
-
-       if (dest->last_num < 0) {
-               mh_scan_folder(folder, dest);
-               if (dest->last_num < 0) return -1;
-       }
-
-       prefs = dest->prefs;
-
-       destdir = folder_item_get_path(dest);
-
-       debug_print(_("Moving message %s%c%d to %s ...\n"),
-                   msginfo->folder->path, G_DIR_SEPARATOR,
-                   msginfo->msgnum, dest->path);
-       srcfile = procmsg_get_message_file(msginfo);
-
-       destfile = mh_get_newmsg_filename(dest);
-       if(!destfile) return -1;
-
+       fileinfo.msginfo = NULL;
+       fileinfo.file = (gchar *)file;
+       fileinfo.flags = flags;
+       file_list.data = &fileinfo;
+       file_list.next = NULL;
 
-       if (move_file(srcfile, destfile) < 0) {
-               g_free(srcfile);
-               g_free(destfile);
-               g_free(destdir);
-               return -1;
-       }
-
-       if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
-               if (chmod(destfile, prefs->folder_chmod) < 0)
-                       FILE_OP_ERROR(destfile, "chmod");
-
-               /* for mark file */
-               filemode = prefs->folder_chmod;
-               if (filemode & S_IRGRP) filemode |= S_IWGRP;
-               if (filemode & S_IROTH) filemode |= S_IWOTH;
-       }
-
-       g_free(srcfile);
-       g_free(destfile);
-       dest->last_num++;
-
-       if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
-               g_warning(_("Can't open mark file.\n"));
-       else {
-               SET_DEST_MSG_FLAGS(fp, dest, msginfo);
-               if (filemode) {
-#if HAVE_FCHMOD
-                       fchmod(fileno(fp), filemode);
-#else
-                       gchar *markfile;
-
-                       markfile = folder_item_get_mark_file(dest);
-                       if (markfile) {
-                               chmod(markfile, filemode);
-                               g_free(markfile);
-                       }
-#endif
-               }
-
-               fclose(fp);
-       }
-       g_free(destdir);
-
-       return dest->last_num;
-}
-
-gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
-{
-       gchar *srcfile;
-       gint ret = 0;
-       g_return_val_if_fail(folder != NULL, -1);
-       g_return_val_if_fail(dest != NULL, -1);
-       g_return_val_if_fail(msginfo != NULL, -1);
-       g_return_val_if_fail(msginfo->folder != NULL, -1);
-       if (folder == msginfo->folder->folder)
-               return mh_do_move(folder, dest, msginfo);
-       srcfile = procmsg_get_message_file(msginfo);
-       if (!srcfile) return -1;
-       ret = mh_add_msg(folder, dest, srcfile, FALSE);
-       g_free(srcfile);
-       if (ret != -1) {
-               gchar *destdir;
-               FILE *fp;
-               destdir = folder_item_get_path(dest);
-               if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
-                       g_warning(_("Can't open mark file.\n"));
-               else {
-                       SET_DEST_MSG_FLAGS(fp, dest, msginfo);
-                       fclose(fp);
-               }
-               g_free(destdir);
-               ret = folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
-       }
+        ret = mh_add_msgs(folder, dest, &file_list, NULL);
+       return ret;
+} 
  
-       return ret;
-}
-
-static gint mh_do_move_msgs_with_dest(Folder *folder, FolderItem *dest,
-                                     GSList *msglist)
-{
-       gchar *destdir;
-       gchar *srcfile;
+static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list, 
+                 GRelation *relation)
+{ 
        gchar *destfile;
-       FILE *fp;
        GSList *cur;
-       MsgInfo *msginfo;
-       PrefsFolderItem *prefs;
+       MsgFileInfo *fileinfo;
 
        g_return_val_if_fail(dest != NULL, -1);
-       g_return_val_if_fail(msglist != NULL, -1);
+       g_return_val_if_fail(file_list != NULL, -1);
 
        if (dest->last_num < 0) {
-               mh_scan_folder(folder, dest);
+               mh_get_last_num(folder, dest);
                if (dest->last_num < 0) return -1;
        }
 
-       prefs = dest->prefs;
-
-       destdir = folder_item_get_path(dest);
-       if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
-               g_warning(_("Can't open mark file.\n"));
+       for (cur = file_list; cur != NULL; cur = cur->next) {
+               fileinfo = (MsgFileInfo *)cur->data;
 
-       for (cur = msglist; cur != NULL; cur = cur->next) {
-               msginfo = (MsgInfo *)cur->data;
-
-               if (msginfo->folder == dest) {
-                       g_warning(_("the src folder is identical to the dest.\n"));
-                       continue;
-               }
-               debug_print(_("Moving message %s%c%d to %s ...\n"),
-                           msginfo->folder->path, G_DIR_SEPARATOR,
-                           msginfo->msgnum, dest->path);
-
-               srcfile = procmsg_get_message_file(msginfo);
-               destfile = mh_get_newmsg_filename(dest);
-               if(!destfile) return -1;
+               destfile = mh_get_new_msg_filename(dest);
+               if (destfile == NULL) return -1;
 
-               if (move_file(srcfile, destfile) < 0) {
-                       g_free(srcfile);
-                       g_free(destfile);
-                       break;
+#ifdef G_OS_UNIX
+               if (link(fileinfo->file, destfile) < 0) {
+#endif
+                       if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
+                               g_warning(_("can't copy message %s to %s\n"),
+                                         fileinfo->file, destfile);
+                               g_free(destfile);
+                               return -1;
+                       }
+#ifdef G_OS_UNIX
                }
+#endif
 
-               g_free(srcfile);
+               if (relation != NULL)
+                       g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
                g_free(destfile);
                dest->last_num++;
-
-               if (fp) {
-                       SET_DEST_MSG_FLAGS(fp, dest, msginfo);
-               }
        }
-
-       g_free(destdir);
-       if (fp) fclose(fp);
-
+       mh_write_sequences(dest, TRUE);
        return dest->last_num;
 }
 
-gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
+static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
 {
-       MsgInfo *msginfo;
-       GSList *cur;
-       gint ret = 0;
+       GSList msglist;
 
-       msginfo = (MsgInfo *)msglist->data;
-       if (folder == msginfo->folder->folder)
-               return mh_do_move_msgs_with_dest(folder, dest, msglist);
+       g_return_val_if_fail(msginfo != NULL, -1);
 
-       for (cur = msglist; cur != NULL; cur = cur->next) {
-               msginfo = (MsgInfo *)cur->data;
-               ret = mh_move_msg(folder, dest, msginfo);
-               if (ret == -1) break;
-       }
+       msglist.data = msginfo;
+       msglist.next = NULL;
 
-       return ret;
+       return mh_copy_msgs(folder, dest, &msglist, NULL);      
 }
 
-gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
+static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist, 
+                        GRelation *relation)
 {
-       gchar *destdir;
+       gboolean dest_need_scan = FALSE;
        gchar *srcfile;
        gchar *destfile;
-       FILE *fp;
        gint filemode = 0;
-       PrefsFolderItem *prefs;
+       FolderItemPrefs *prefs;
+       MsgInfo *msginfo = NULL;
+       MsgInfoList *cur = NULL;
+       gint curnum = 0, total = 0;
+       gchar *srcpath = NULL;
+       gboolean full_fetch = FALSE;
 
        g_return_val_if_fail(dest != NULL, -1);
+       g_return_val_if_fail(msglist != NULL, -1);
+       
+       msginfo = (MsgInfo *)msglist->data;
+
        g_return_val_if_fail(msginfo != NULL, -1);
 
        if (msginfo->folder == dest) {
-               g_warning(_("the src folder is identical to the dest.\n"));
+               g_warning("the src folder is identical to the dest.\n");
                return -1;
        }
 
+       if (msginfo->folder->folder != dest->folder)
+               full_fetch = TRUE;
+
        if (dest->last_num < 0) {
-               mh_scan_folder(folder, dest);
+               mh_get_last_num(folder, dest);
                if (dest->last_num < 0) return -1;
        }
 
        prefs = dest->prefs;
 
-       destdir = folder_item_get_path(dest);
-       if (!is_dir_exist(destdir))
-               make_dir_hier(destdir);
+       srcpath = folder_item_get_path(msginfo->folder);
 
-       debug_print(_("Copying message %s%c%d to %s ...\n"),
-                   msginfo->folder->path, G_DIR_SEPARATOR,
-                   msginfo->msgnum, dest->path);
-       srcfile = procmsg_get_message_file(msginfo);
-       destfile = mh_get_newmsg_filename(dest);
-       if(!destfile) {
-               g_free(srcfile);
-               if (fp) fclose(fp);
-               return -1;
-       }
-       
-       dest->op_count--;
-
-       if (copy_file(srcfile, destfile) < 0) {
-               FILE_OP_ERROR(srcfile, "copy");
-               g_free(srcfile);
-               g_free(destfile);
-               g_free(destdir);
-               return -1;
-       }
-
-       if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
-               if (chmod(destfile, prefs->folder_chmod) < 0)
-                       FILE_OP_ERROR(destfile, "chmod");
-
-               /* for mark file */
-               filemode = prefs->folder_chmod;
-               if (filemode & S_IRGRP) filemode |= S_IWGRP;
-               if (filemode & S_IROTH) filemode |= S_IWOTH;
-       }
-
-       g_free(srcfile);
-       g_free(destfile);
-       dest->last_num++;
-
-       if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
-               g_warning(_("Can't open mark file.\n"));
-       else {
-               SET_DEST_MSG_FLAGS(fp, dest, msginfo);
-
-               if (filemode) {
-#if HAVE_FCHMOD
-                       fchmod(fileno(fp), filemode);
-#else
-                       gchar *markfile;
-
-                       markfile = folder_item_get_mark_file(dest);
-                       if (markfile) {
-                               chmod(markfile, filemode);
-                               g_free(markfile);
-                       }
-#endif
-               }
-
-               fclose(fp);
-       }
-       g_free(destdir);
-
-       return dest->last_num;
-}
-
-/*
-gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
-{
-       Folder * src_folder;
-       gchar * filename;
-       gint num;
-       gchar * destdir;
-       FILE * fp;
-
-       src_folder = msginfo->folder->folder;
-       
-       g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
-       
-       filename = src_folder->fetch_msg(src_folder,
-                                        msginfo->folder,
-                                        msginfo->msgnum);
-       if (filename == NULL)
-               return -1;
-
-       num = folder->add_msg(folder, dest, filename, FALSE);
-
-       destdir = folder_item_get_path(dest);
-
-       if (fp) {
-               MsgInfo newmsginfo;
-
-               newmsginfo.msgnum = dest->last_num;
-               newmsginfo.flags = msginfo->flags;
-               if (dest->stype == F_OUTBOX ||
-                   dest->stype == F_QUEUE  ||
-                   dest->stype == F_DRAFT  ||
-                   dest->stype == F_TRASH)
-                       MSG_UNSET_FLAGS(newmsginfo.flags,
-                                       MSG_NEW|MSG_UNREAD|MSG_DELETED);
-
-               procmsg_write_flags(&newmsginfo, fp);
-               fclose(fp);
-       }
-       
-       return num;
-}
-*/
-
-gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
-{
-       gchar *destdir;
-       gchar *srcfile;
-       gchar *destfile;
-       FILE *fp;
-       GSList *cur;
-       MsgInfo *msginfo;
-
-       g_return_val_if_fail(dest != NULL, -1);
-       g_return_val_if_fail(msglist != NULL, -1);
-
-       if (dest->last_num < 0) {
-               mh_scan_folder(folder, dest);
-               if (dest->last_num < 0) return -1;
+       total = g_slist_length(msglist);
+       if (total > 100) {
+               if (MSG_IS_MOVE(msginfo->flags))
+                       statusbar_print_all(_("Moving messages..."));
+               else
+                       statusbar_print_all(_("Copying messages..."));
        }
-
-       destdir = folder_item_get_path(dest);
-       if (!is_dir_exist(destdir))
-               make_dir_hier(destdir);
-
-       if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
-               g_warning(_("Can't open mark file.\n"));
-
-       for (cur = msglist; cur != NULL; cur = cur->next) {
+       for (cur = msglist; cur; cur = cur->next) {
                msginfo = (MsgInfo *)cur->data;
+               if (!msginfo) {
+                       goto err_reset_status;
+               }
+               if (!full_fetch) {
+                       srcfile = g_strconcat(srcpath, 
+                               G_DIR_SEPARATOR_S, 
+                               itos(msginfo->msgnum), NULL);
+               } else {
+                       srcfile = procmsg_get_message_file(msginfo);
+               }
+               if (!srcfile) {
+                       goto err_reset_status;
+               }
+               destfile = mh_get_new_msg_filename(dest);
+               if (!destfile) {
+                       g_free(srcfile);
+                       goto err_reset_status;
+               }
 
-               if (msginfo->folder == dest) {
-                       g_warning(_("the src folder is identical to the dest.\n"));
-                       continue;
+               if (total > 100) {
+                       statusbar_progress_all(curnum, total, 100);
+                       if (curnum % 100 == 0)
+                               GTK_EVENTS_FLUSH();
+                       curnum++;
                }
-               debug_print(_("Copying message %s%c%d to %s ...\n"),
+
+               debug_print("Copying message %s%c%d to %s ...\n",
                            msginfo->folder->path, G_DIR_SEPARATOR,
                            msginfo->msgnum, dest->path);
 
-               srcfile = procmsg_get_message_file(msginfo);
-               destfile = mh_get_newmsg_filename(dest);
-               if(!destfile) {
-                       g_free(srcfile);
-                       break;
-               }
 
-               if (copy_file(srcfile, destfile) < 0) {
+               if (MSG_IS_MOVE(msginfo->flags)) {
+                       if (move_file(srcfile, destfile, TRUE) < 0) {
+                               FILE_OP_ERROR(srcfile, "move");
+                               if (copy_file(srcfile, destfile, TRUE) < 0) {
+                                       FILE_OP_ERROR(srcfile, "copy");
+                                       g_free(srcfile);
+                                       g_free(destfile);
+                                       goto err_reset_status;
+                               }
+                       }
+               } else if (copy_file(srcfile, destfile, TRUE) < 0) {
                        FILE_OP_ERROR(srcfile, "copy");
                        g_free(srcfile);
                        g_free(destfile);
-                       break;
+                       goto err_reset_status;
+               } 
+               if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
+                       if (chmod(destfile, prefs->folder_chmod) < 0)
+                               FILE_OP_ERROR(destfile, "chmod");
+
+                       /* for mark file */
+                       filemode = prefs->folder_chmod;
+                       if (filemode & S_IRGRP) filemode |= S_IWGRP;
+                       if (filemode & S_IROTH) filemode |= S_IWOTH;
                }
-
+               if (relation)
+                       g_relation_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
                g_free(srcfile);
                g_free(destfile);
                dest->last_num++;
+       }
 
-               if (fp) {
-                       SET_DEST_MSG_FLAGS(fp, dest, msginfo);
-               }
+       g_free(srcpath);
+       mh_write_sequences(dest, TRUE);
+
+       dest_need_scan = mh_scan_required(dest->folder, dest);
+       if (!dest_need_scan)
+               dest->mtime = time(NULL);
+       
+       if (total > 100) {
+               statusbar_progress_all(0,0,0);
+               statusbar_pop_all();
+       }
+       return dest->last_num;
+err_reset_status:
+       g_free(srcpath);
+       mh_write_sequences(dest, TRUE);
+       if (total > 100) {
+               statusbar_progress_all(0,0,0);
+               statusbar_pop_all();
        }
+       return -1;
 
-       g_free(destdir);
-       if (fp) fclose(fp);
-
-       return dest->last_num;
 }
 
-gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
+static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
 {
+       gboolean need_scan = FALSE;
        gchar *file;
 
        g_return_val_if_fail(item != NULL, -1);
@@ -658,155 +597,137 @@ gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
        file = mh_fetch_msg(folder, item, num);
        g_return_val_if_fail(file != NULL, -1);
 
-       if (unlink(file) < 0) {
+       need_scan = mh_scan_required(folder, item);
+
+       if (g_unlink(file) < 0) {
                FILE_OP_ERROR(file, "unlink");
                g_free(file);
                return -1;
        }
 
+       if (!need_scan)
+               item->mtime = time(NULL);
+
        g_free(file);
        return 0;
 }
 
-gint mh_remove_all_msg(Folder *folder, FolderItem *item)
+static gint mh_remove_msgs(Folder *folder, FolderItem *item, 
+                   MsgInfoList *msglist, GRelation *relation)
 {
-       gchar *path;
-       gint val;
+       gboolean need_scan = FALSE;
+       gchar *path, *file;
+       MsgInfoList *cur;
 
        g_return_val_if_fail(item != NULL, -1);
 
        path = folder_item_get_path(item);
-       g_return_val_if_fail(path != NULL, -1);
-       val = remove_all_numbered_files(path);
-       g_free(path);
-
-       return val;
-}
+       
+       for (cur = msglist; cur; cur = cur->next) {
+               MsgInfo *msginfo = (MsgInfo *)cur->data;
+               if (msginfo == NULL)
+                       continue;
+               file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
+               if (file == NULL)
+                       continue;
+               
+               if (g_unlink(file) < 0) {
+                       g_free(file);
+                       continue;
+               }
+               
+               g_free(file);
+       }
 
-gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
-{
-       struct stat s;
+       need_scan = mh_scan_required(folder, item);
 
-       if (stat(itos(msginfo->msgnum), &s) < 0 ||
-           msginfo->size  != s.st_size ||
-           msginfo->mtime != s.st_mtime)
-               return TRUE;
+       if (!need_scan)
+               item->mtime = time(NULL);
 
-       return FALSE;
+       g_free(path);
+       return 0;
 }
 
-void mh_scan_folder(Folder *folder, FolderItem *item)
+static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
 {
        gchar *path;
-       DIR *dp;
-       struct dirent *d;
-       struct stat s;
-       gint max = 0;
-       gint num;
-       gint n_msg = 0;
-
-       g_return_if_fail(item != NULL);
+       gint val;
 
-       debug_print("mh_scan_folder(): Scanning %s ...\n", item->path);
+       g_return_val_if_fail(item != NULL, -1);
 
        path = folder_item_get_path(item);
-       g_return_if_fail(path != NULL);
-       if (change_dir(path) < 0) {
-               g_free(path);
-               return;
-       }
+       g_return_val_if_fail(path != NULL, -1);
+       val = remove_all_numbered_files(path);
        g_free(path);
 
-       if ((dp = opendir(".")) == NULL) {
-               FILE_OP_ERROR(item->path, "opendir");
-               return;
-       }
-
-       if (folder->ui_func)
-               folder->ui_func(folder, item, folder->ui_func_data);
-
-       while ((d = readdir(dp)) != NULL) {
-               if ((num = to_number(d->d_name)) >= 0 &&
-                   stat(d->d_name, &s) == 0 &&
-                   S_ISREG(s.st_mode)) {
-                       n_msg++;
-                       if (max < num)
-                               max = num;
-               }
-       }
+       mh_write_sequences(item, TRUE);
 
-       closedir(dp);
+       return val;
+}
 
-       if (n_msg == 0)
-               item->new = item->unread = item->total = 0;
-       else {
-               gint new, unread, total, min, max;
+static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
+                                 MsgInfo *msginfo)
+{
+       struct stat s;
 
-               procmsg_get_mark_sum(".", &new, &unread, &total, &min, &max, 0);
-               if (n_msg > total) {
-                       new += n_msg - total;
-                       unread += n_msg - total;
-               }
-               item->new = new;
-               item->unread = unread;
-               item->total = n_msg;
-       }
+       if (g_stat(itos(msginfo->msgnum), &s) < 0 ||
+           msginfo->size  != s.st_size || (
+               (msginfo->mtime - s.st_mtime != 0) &&
+               (msginfo->mtime - s.st_mtime != 3600) &&
+               (msginfo->mtime - s.st_mtime != -3600)))
+               return TRUE;
 
-       debug_print(_("Last number in dir %s = %d\n"), item->path, max);
-       item->last_num = max;
+       return FALSE;
 }
 
-void mh_scan_tree(Folder *folder)
+static gint mh_scan_tree(Folder *folder)
 {
        FolderItem *item;
        gchar *rootpath;
-       GHashTable *pptable;
-
-       g_return_if_fail(folder != NULL);
 
-       pptable = folder_persist_prefs_new(folder);
+       g_return_val_if_fail(folder != NULL, -1);
 
-       folder_tree_destroy(folder);
-       item = folder_item_new(folder->name, NULL);
-       item->folder = folder;
-       folder->node = g_node_new(item);
+       if (!folder->node) {
+               item = folder_item_new(folder, folder->name, NULL);
+               item->folder = folder;
+               folder->node = item->node = g_node_new(item);
+       } else
+               item = FOLDER_ITEM(folder->node->data);
 
        rootpath = folder_item_get_path(item);
        if (change_dir(rootpath) < 0) {
                g_free(rootpath);
-               return;
+               return -1;
        }
        g_free(rootpath);
 
-       mh_scan_tree_recursive(item, pptable);
-       
-       folder_persist_prefs_free(pptable);
+       mh_create_tree(folder);
+       mh_remove_missing_folder_items(folder);
+       mh_scan_tree_recursive(item);
+
+       return 0;
 }
 
 #define MAKE_DIR_IF_NOT_EXIST(dir) \
 { \
        if (!is_dir_exist(dir)) { \
                if (is_file_exist(dir)) { \
-                       g_warning(_("File `%s' already exists.\n" \
-                                   "Can't create folder."), dir); \
+                       g_warning("File `%s' already exists.\n" \
+                                   "Can't create folder.", dir); \
                        return -1; \
                } \
-               if (mkdir(dir, S_IRWXU) < 0) { \
-                       FILE_OP_ERROR(dir, "mkdir"); \
+               if (make_dir(dir) < 0) \
                        return -1; \
-               } \
-               if (chmod(dir, S_IRWXU) < 0) \
-                       FILE_OP_ERROR(dir, "chmod"); \
        } \
 }
 
-gint mh_create_tree(Folder *folder)
+static gint mh_create_tree(Folder *folder)
 {
        gchar *rootpath;
 
        g_return_val_if_fail(folder != NULL, -1);
 
-       CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
+       CHDIR_RETURN_VAL_IF_FAIL(get_mail_base_dir(), -1);
        rootpath = LOCAL_FOLDER(folder)->rootpath;
        MAKE_DIR_IF_NOT_EXIST(rootpath);
        CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
@@ -821,31 +742,74 @@ gint mh_create_tree(Folder *folder)
 
 #undef MAKE_DIR_IF_NOT_EXIST
 
-FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
-                            const gchar *name)
+static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
 {
-       gchar *path;
+       gchar *folder_path, *path;
+       gchar *real_path;
+       g_return_val_if_fail(folder != NULL, NULL);
+       g_return_val_if_fail(item != NULL, NULL);
+
+       folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
+       g_return_val_if_fail(folder_path != NULL, NULL);
+
+        /* FIXME: [W32] The code below does not correctly merge
+           relative filenames; there should be a function to handle
+           this.  */
+        if ( !is_relative_filename (folder_path) ) {
+                if (item->path)
+                        path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
+                                           item->path, NULL);
+                else
+                        path = g_strdup(folder_path);
+        } else {
+                if (item->path)
+                        path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
+                                           folder_path, G_DIR_SEPARATOR_S,
+                                           item->path, NULL);
+                else
+                        path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
+                                           folder_path, NULL);
+        }
+       g_free(folder_path);
+       real_path = mh_filename_from_utf8(path);
+       if (!is_dir_exist(real_path) && is_dir_exist(path)) {
+               /* mmh, older version did put utf8 filenames instead of
+                * the correct encoding */
+               rename(path, real_path);
+               folder_item_scan(item);
+       }
+
+       g_free(path);
+       return real_path;
+}
+
+static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
+                                   const gchar *name)
+{
+       gchar *path, *real_name;
        gchar *fullpath;
        FolderItem *new_item;
+       gchar *mh_sequences_filename;
+       FILE *mh_sequences_file;
 
        g_return_val_if_fail(folder != NULL, NULL);
        g_return_val_if_fail(parent != NULL, NULL);
        g_return_val_if_fail(name != NULL, NULL);
 
        path = folder_item_get_path(parent);
-       if (!is_dir_exist(path))
-               make_dir_hier(path);
-
-       fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
+       if (!is_dir_exist(path)) 
+               if (make_dir_hier(path) != 0)
+                       return NULL;
+               
+       real_name = mh_filename_from_utf8(name);
+       fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
+       g_free(real_name);
        g_free(path);
 
-       if (mkdir(fullpath, S_IRWXU) < 0) {
-               FILE_OP_ERROR(fullpath, "mkdir");
+       if (make_dir(fullpath) < 0) {
                g_free(fullpath);
                return NULL;
        }
-       if (chmod(fullpath, S_IRWXU) < 0)
-               FILE_OP_ERROR(fullpath, "chmod");
 
        g_free(fullpath);
 
@@ -854,19 +818,30 @@ FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
                                   NULL);
        else
                path = g_strdup(name);
-       new_item = folder_item_new(name, path);
+       new_item = folder_item_new(folder, name, path);
        folder_item_append(parent, new_item);
+
+       g_free(path);
+
+       path = folder_item_get_path(new_item);
+       mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
+                                           ".mh_sequences", NULL);
+       if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
+               fclose(mh_sequences_file);
+       }
+       g_free(mh_sequences_filename);
        g_free(path);
 
        return new_item;
 }
 
-gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
+static gint mh_rename_folder(Folder *folder, FolderItem *item,
+                            const gchar *name)
 {
+       gchar *real_name;
        gchar *oldpath;
        gchar *dirname;
-       gchar *newpath;
-       GNode *node;
+       gchar *newpath, *utf8newpath;
        gchar *paths[2];
 
        g_return_val_if_fail(folder != NULL, -1);
@@ -878,11 +853,12 @@ gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
        if (!is_dir_exist(oldpath))
                make_dir_hier(oldpath);
 
-       dirname = g_dirname(oldpath);
-       newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
-       g_free(dirname);
+       dirname = g_path_get_dirname(oldpath);
+       real_name = mh_filename_from_utf8(name);
+       newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
+       g_free(real_name);
 
-       if (rename(oldpath, newpath) < 0) {
+       if (g_rename(oldpath, newpath) < 0) {
                FILE_OP_ERROR(oldpath, "rename");
                g_free(oldpath);
                g_free(newpath);
@@ -893,20 +869,19 @@ gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
        g_free(newpath);
 
        if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
-               dirname = g_dirname(item->path);
-               newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
+               dirname = g_path_get_dirname(item->path);
+               utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
+                                         name, NULL);
                g_free(dirname);
        } else
-               newpath = g_strdup(name);
+               utf8newpath = g_strdup(name);
 
        g_free(item->name);
        item->name = g_strdup(name);
 
-       node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
-                          item);
        paths[0] = g_strdup(item->path);
-       paths[1] = newpath;
-       g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
+       paths[1] = utf8newpath;
+       g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
                        mh_rename_folder_func, paths);
 
        g_free(paths[0]);
@@ -914,7 +889,7 @@ gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
        return 0;
 }
 
-gint mh_remove_folder(Folder *folder, FolderItem *item)
+static gint mh_remove_folder(Folder *folder, FolderItem *item)
 {
        gchar *path;
 
@@ -934,253 +909,188 @@ gint mh_remove_folder(Folder *folder, FolderItem *item)
        return 0;
 }
 
-
-static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
-{
-       gchar *path;
-       DIR *dp;
-       struct dirent *d;
-       struct stat s;
-       GSList *newlist = NULL;
-       GSList *last = NULL;
-       MsgInfo *msginfo;
-       gint n_newmsg = 0;
-       gint num;
-
-       g_return_val_if_fail(item != NULL, NULL);
-
-       path = folder_item_get_path(item);
-       g_return_val_if_fail(path != NULL, NULL);
-       if (change_dir(path) < 0) {
-               g_free(path);
-               return NULL;
-       }
-       g_free(path);
-
-       if ((dp = opendir(".")) == NULL) {
-               FILE_OP_ERROR(item->path, "opendir");
-               return NULL;
-       }
-
-       debug_print(_("\tSearching uncached messages... "));
-
-       if (msg_table) {
-               while ((d = readdir(dp)) != NULL) {
-                       if ((num = to_number(d->d_name)) < 0) continue;
-                       if (stat(d->d_name, &s) < 0) {
-                               FILE_OP_ERROR(d->d_name, "stat");
-                               continue;
-                       }
-                       if (!S_ISREG(s.st_mode)) continue;
-
-                       msginfo = g_hash_table_lookup
-                               (msg_table, GUINT_TO_POINTER(num));
-
-                       if (!msginfo) {
-                               /* not found in the cache (uncached message) */
-                               msginfo = mh_parse_msg(d->d_name, item);
-                               if (!msginfo) continue;
-
-                               if (!newlist)
-                                       last = newlist =
-                                               g_slist_append(NULL, msginfo);
-                               else {
-                                       last = g_slist_append(last, msginfo);
-                                       last = last->next;
-                               }
-                               n_newmsg++;
-                       }
-               }
-       } else {
-               /* discard all previous cache */
-               while ((d = readdir(dp)) != NULL) {
-                       if (to_number(d->d_name) < 0) continue;
-                       if (stat(d->d_name, &s) < 0) {
-                               FILE_OP_ERROR(d->d_name, "stat");
-                               continue;
-                       }
-                       if (!S_ISREG(s.st_mode)) continue;
-
-                       msginfo = mh_parse_msg(d->d_name, item);
-                       if (!msginfo) continue;
-
-                       if (!newlist)
-                               last = newlist = g_slist_append(NULL, msginfo);
-                       else {
-                               last = g_slist_append(last, msginfo);
-                               last = last->next;
-                       }
-                       n_newmsg++;
-               }
-       }
-
-       closedir(dp);
-
-       if (n_newmsg)
-               debug_print(_("%d uncached message(s) found.\n"), n_newmsg);
-       else
-               debug_print(_("done.\n"));
-
-       /* sort new messages in numerical order */
-       if (newlist) {
-               debug_print(_("\tSorting uncached messages in numerical order... "));
-               newlist = g_slist_sort
-                       (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
-               debug_print(_("done.\n"));
-       }
-
-       return newlist;
-}
-
 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
 {
-       struct stat s;
        MsgInfo *msginfo;
        MsgFlags flags;
 
-       flags.perm_flags = MSG_NEW|MSG_UNREAD;
-       flags.tmp_flags = 0;
-
        g_return_val_if_fail(item != NULL, NULL);
        g_return_val_if_fail(file != NULL, NULL);
 
-       if (item->stype == F_QUEUE) {
+       flags.perm_flags = MSG_NEW|MSG_UNREAD;
+       flags.tmp_flags = 0;
+
+       if (folder_has_parent_of_type(item, F_QUEUE)) {
                MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
-       } else if (item->stype == F_DRAFT) {
+       } else if (folder_has_parent_of_type(item, F_DRAFT)) {
                MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
        }
 
-       msginfo = procheader_parse(file, flags, FALSE, FALSE);
+       msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
        if (!msginfo) return NULL;
 
        msginfo->msgnum = atoi(file);
        msginfo->folder = item;
 
-       if (stat(file, &s) < 0) {
-               FILE_OP_ERROR(file, "stat");
-               msginfo->size = 0;
-               msginfo->mtime = 0;
-       } else {
-               msginfo->size = s.st_size;
-               msginfo->mtime = s.st_mtime;
-       }
-
        return msginfo;
 }
 
-static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
+static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
 {
-       gchar *entry;
-       gboolean result;
+       FolderItem *item;
+       gchar *path;
+
+       g_return_val_if_fail(node->data != NULL, FALSE);
+
+       if (G_NODE_IS_ROOT(node))
+               return FALSE;
 
-       entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
-       result = is_dir_exist(entry);
-       g_free(entry);
+       item = FOLDER_ITEM(node->data);
+
+       path = folder_item_get_path(item);
+       if (!is_dir_exist(path)) {
+               debug_print("folder '%s' not found. removing...\n", path);
+               folder_item_remove(item);
+       }
+       g_free(path);
 
-       return result;
+       return FALSE;
 }
 
-/*
- * check whether PATH is a Maildir style mailbox.
- * This is the case if the 3 subdir: new, cur, tmp are existing.
- * This functon assumes that entry is an directory
- */
-static gboolean mh_is_maildir(const gchar *path)
+static void mh_remove_missing_folder_items(Folder *folder)
 {
-       return mh_is_maildir_one(path, "new") &&
-              mh_is_maildir_one(path, "cur") &&
-              mh_is_maildir_one(path, "tmp");
+       g_return_if_fail(folder != NULL);
+
+       debug_print("searching missing folders...\n");
+
+       g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
+                       mh_remove_missing_folder_items_func, folder);
 }
 
-static void mh_scan_tree_recursive(FolderItem *item, GHashTable *pptable)
+static void mh_scan_tree_recursive(FolderItem *item)
 {
+       Folder *folder;
+#ifdef G_OS_WIN32
+       GDir *dir;
+#else
        DIR *dp;
        struct dirent *d;
+#endif
+       const gchar *dir_name;
        struct stat s;
-       gchar *entry;
+       gchar *real_path, *entry, *utf8entry, *utf8name;
        gint n_msg = 0;
 
        g_return_if_fail(item != NULL);
        g_return_if_fail(item->folder != NULL);
 
-       dp = opendir(item->path ? item->path : ".");
+       folder = item->folder;
+
+       real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
+#ifdef G_OS_WIN32
+       dir = g_dir_open(real_path, 0, NULL);
+       if (!dir) {
+               g_warning("failed to open directory: %s\n", real_path);
+               g_free(real_path);
+               return;
+       }
+#else
+       dp = opendir(real_path);
        if (!dp) {
-               FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
+               FILE_OP_ERROR(real_path, "opendir");
                return;
        }
+#endif
+       g_free(real_path);
 
        debug_print("scanning %s ...\n",
                    item->path ? item->path
                    : LOCAL_FOLDER(item->folder)->rootpath);
-       if (item->folder->ui_func)
-               item->folder->ui_func(item->folder, item,
-                                     item->folder->ui_func_data);
+       if (folder->ui_func)
+               folder->ui_func(folder, item, folder->ui_func_data);
 
+#ifdef G_OS_WIN32
+       while ((dir_name = g_dir_read_name(dir)) != NULL) {
+#else
        while ((d = readdir(dp)) != NULL) {
-               if (d->d_name[0] == '.') continue;
+               dir_name = d->d_name;
+#endif
+               if (dir_name[0] == '.') continue;
 
+               utf8name = mh_filename_to_utf8(dir_name);
                if (item->path)
-                       entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
-                                           d->d_name, NULL);
+                       utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
+                                               utf8name, NULL);
                else
-                       entry = g_strdup(d->d_name);
-
-               if (stat(entry, &s) < 0) {
-                       FILE_OP_ERROR(entry, "stat");
-                       g_free(entry);
-                       continue;
-               }
-
-               if (S_ISDIR(s.st_mode)) {
-                       FolderItem *new_item;
+                       utf8entry = g_strdup(utf8name);
+               entry = mh_filename_from_utf8(utf8entry);
 
-                       if (mh_is_maildir(entry)) {
-                               g_free(entry);
-                               continue;
+               if (
+#if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
+                       d->d_type == DT_DIR ||
+                       (d->d_type == DT_UNKNOWN &&
+#endif
+                       g_stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
+#if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
+                       )
+#endif
+                  ) {
+                       FolderItem *new_item = NULL;
+                       GNode *node;
+
+                       node = item->node;
+                       for (node = node->children; node != NULL; node = node->next) {
+                               FolderItem *cur_item = FOLDER_ITEM(node->data);
+                               if (!strcmp2(cur_item->path, entry)) {
+                                       new_item = cur_item;
+                                       break;
+                               }
+                       }
+                       if (!new_item) {
+                               debug_print("new folder '%s' found.\n", entry);
+                               new_item = folder_item_new(folder, utf8name, utf8entry);
+                               folder_item_append(item, new_item);
                        }
 
-                       new_item = folder_item_new(d->d_name, entry);
-                       folder_item_append(item, new_item);
                        if (!item->path) {
-                               if (!strcmp(d->d_name, "inbox")) {
+                               if (!folder->inbox &&
+                                   !strcmp(dir_name, INBOX_DIR)) {
                                        new_item->stype = F_INBOX;
-                                       item->folder->inbox = new_item;
-                               } else if (!strcmp(d->d_name, "outbox")) {
+                                       folder->inbox = new_item;
+                               } else if (!folder->outbox &&
+                                          !strcmp(dir_name, OUTBOX_DIR)) {
                                        new_item->stype = F_OUTBOX;
-                                       item->folder->outbox = new_item;
-                               } else if (!strcmp(d->d_name, "draft")) {
+                                       folder->outbox = new_item;
+                               } else if (!folder->draft &&
+                                          !strcmp(dir_name, DRAFT_DIR)) {
                                        new_item->stype = F_DRAFT;
-                                       item->folder->draft = new_item;
-                               } else if (!strcmp(d->d_name, "queue")) {
+                                       folder->draft = new_item;
+                               } else if (!folder->queue &&
+                                          !strcmp(dir_name, QUEUE_DIR)) {
                                        new_item->stype = F_QUEUE;
-                                       item->folder->queue = new_item;
-                               } else if (!strcmp(d->d_name, "trash")) {
+                                       folder->queue = new_item;
+                               } else if (!folder->trash &&
+                                          !strcmp(dir_name, TRASH_DIR)) {
                                        new_item->stype = F_TRASH;
-                                       item->folder->trash = new_item;
+                                       folder->trash = new_item;
                                }
                        }
-                       folder_item_restore_persist_prefs(new_item, pptable);
-                       mh_scan_tree_recursive(new_item, pptable);
-               } else if (to_number(d->d_name) != -1) n_msg++;
+
+                       mh_scan_tree_recursive(new_item);
+               } else if (to_number(dir_name) > 0) n_msg++;
 
                g_free(entry);
+               g_free(utf8entry);
+               g_free(utf8name);
        }
 
+#ifdef G_OS_WIN32
+       g_dir_close(dir);
+#else
        closedir(dp);
+#endif
 
-       if (item->path) {
-               gint new, unread, total, min, max;
-
-               procmsg_get_mark_sum(item->path, &new, &unread, &total,
-                                    &min, &max, 0);
-               if (n_msg > total) {
-                       new += n_msg - total;
-                       unread += n_msg - total;
-               }
-               item->new = new;
-               item->unread = unread;
-               item->total = n_msg;
-       }
+       item->mtime = time(NULL);
 }
 
 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
@@ -1211,3 +1121,249 @@ static gboolean mh_rename_folder_func(GNode *node, gpointer data)
 
        return FALSE;
 }
+
+static gchar *mh_filename_from_utf8(const gchar *path)
+{
+       gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
+
+       if (!real_path) {
+               g_warning("mh_filename_from_utf8: faild to convert character set\n");
+               real_path = g_strdup(path);
+       }
+
+       return real_path;
+}
+
+static gchar *mh_filename_to_utf8(const gchar *path)
+{
+       gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
+       if (!utf8path) {
+               g_warning("mh_filename_to_utf8: faild to convert character set\n");
+               utf8path = g_strdup(path);
+       }
+
+       return utf8path;
+}
+
+static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
+{
+       MsgInfo *msginfo_a = (MsgInfo *) a;
+       MsgInfo *msginfo_b = (MsgInfo *) b;
+
+       return (msginfo_a->msgnum - msginfo_b->msgnum);
+}
+
+static gchar *get_unseen_seq_name(void)
+{
+       static gchar *seq_name = NULL;
+       if (!seq_name) {
+               gchar buf[BUFFSIZE];
+               gchar *tmp;
+               gchar *profile_path = g_strconcat(
+                       get_home_dir(), G_DIR_SEPARATOR_S,
+                       ".mh_profile", NULL);
+               FILE *fp = g_fopen(profile_path, "r");
+               if (fp) {
+                       while (fgets(buf, sizeof(buf), fp) != NULL) {
+                               if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
+                                       gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
+                                       while (*seq_tmp == ' ')
+                                               seq_tmp++;
+                                       seq_name = g_strdup(seq_tmp);
+                                       seq_name = strretchomp(seq_name);
+                                       break;
+                               }
+                       }
+                       fclose(fp);
+               }
+               if (!seq_name)
+                       seq_name = g_strdup("unseen");
+               tmp = g_strdup_printf("%s:", seq_name);
+               g_free(seq_name);
+               seq_name = tmp;
+       }
+       return seq_name;        
+}
+
+#if 0
+static gint mh_get_flags(Folder *folder, FolderItem *item,
+                           MsgInfoList *msginfo_list, GRelation *msgflags)
+{
+       gchar *mh_sequences_filename;
+       FILE *mh_sequences_file;
+       gchar buf[BUFFSIZE];
+       gchar *unseen_list = NULL;
+       gchar *path;
+       MsgInfoList *mcur = NULL;
+/*
+       GTimer *timer = g_timer_new();
+       g_timer_start(timer);
+*/
+       if (!item)
+               return 0;
+
+       /* don't update from .mh_sequences if the item's opened: mails may have
+        * been marked read/unread and it's not yet written in the file. */     
+       if (item->opened)
+               return 0;
+
+       path = folder_item_get_path(item);
+
+       mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
+                                           ".mh_sequences", NULL);
+       g_free(path);
+       if ((mh_sequences_file = g_fopen(mh_sequences_filename, "r+b")) != NULL) {
+               while (fgets(buf, sizeof(buf), mh_sequences_file) != NULL) {
+                       if (!strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name()))) {
+                               unseen_list = g_strdup(buf+strlen(get_unseen_seq_name()));
+                               break;
+                       }
+               }
+               fclose(mh_sequences_file);
+       }
+       
+       g_free(mh_sequences_filename);
+       
+       if (unseen_list) {
+               gchar *cur = NULL;
+               gchar *token = NULL, *next = NULL, *boundary = NULL;
+               gint num = 0;
+               GHashTable *unseen_table = g_hash_table_new(g_direct_hash, g_direct_equal);
+
+               cur = unseen_list = strretchomp(unseen_list);
+               debug_print("found unseen list in .mh_sequences: %s\n", unseen_list);
+next_token:
+               while (*cur && *cur == ' ')
+                       cur++;
+               
+               if ((next = strchr(cur, ' ')) != NULL) {
+                       token = cur;
+                       cur = next+1;
+                       *next = '\0';
+               } else {
+                       token = cur;
+                       cur = NULL;
+               }
+               
+               if ((boundary = strchr(token, '-')) != NULL) {
+                       gchar *start, *end;
+                       int i;
+                       start = token;
+                       end = boundary+1;
+                       *boundary='\0';
+                       for (i = atoi(start); i <= atoi(end); i++) {
+                               g_hash_table_insert(unseen_table, GINT_TO_POINTER(i), GINT_TO_POINTER(1));
+                       }
+               } else if ((num = atoi(token)) > 0) {
+                       g_hash_table_insert(unseen_table, GINT_TO_POINTER(num), GINT_TO_POINTER(1));
+               }
+               
+               if (cur)
+                       goto next_token;
+               for (mcur = msginfo_list; mcur; mcur = mcur->next) {
+                       MsgInfo *msginfo = (MsgInfo *)mcur->data;
+                       MsgPermFlags flags = msginfo->flags.perm_flags;
+                       if (g_hash_table_lookup(unseen_table, GINT_TO_POINTER(msginfo->msgnum))) {
+                               flags |= MSG_UNREAD;
+                       } else if (!(flags & MSG_NEW)) { /* don't mark new msgs as read */
+                               flags &= ~(MSG_UNREAD);
+                       }
+                       if (flags != msginfo->flags.perm_flags)
+                               g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
+               }
+               g_hash_table_destroy(unseen_table);
+               g_free(unseen_list);
+       }
+/*
+       g_timer_stop(timer);
+       printf("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
+       g_timer_destroy(timer);
+*/
+       return 0;
+}
+#endif
+
+static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
+{
+       gchar *mh_sequences_old, *mh_sequences_new;
+       FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
+       gchar buf[BUFFSIZE];
+       gchar *path = NULL;
+/*
+       GTimer *timer = g_timer_new();
+       g_timer_start(timer);
+*/
+       if (!item)
+               return;
+       
+       path = folder_item_get_path(item);
+
+       mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
+                                           ".mh_sequences", NULL);
+       mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
+                                           ".mh_sequences.new", NULL);
+       if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
+               GSList *msglist = folder_item_get_msg_list(item);
+               GSList *cur;
+               MsgInfo *info = NULL;
+               gint start = -1, end = -1;
+               gchar *sequence = g_strdup("");
+               msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
+               cur = msglist;
+               
+               /* write the unseen sequence if we don't have to scrap it */
+               if (!remove_unseen) do {
+                       info = (MsgInfo *)(cur ? cur->data:NULL);
+                       if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
+                               if (start < 0)
+                                       start = end = info->msgnum;
+                               else
+                                       end = info->msgnum;
+                       } else {
+                               if (start > 0 && end > 0) {
+                                       gchar *tmp = sequence;
+                                       if (start != end)
+                                               sequence = g_strdup_printf("%s %d-%d ", tmp, start, end);
+                                       else
+                                               sequence = g_strdup_printf("%s %d ", tmp, start);
+                                       g_free(tmp);
+                                       start = end = -1;
+                               }
+                       }
+                       cur = cur ? cur->next:NULL;
+               } while (cur || (start > 0 && end > 0));
+               if (sequence && strlen(sequence)) {
+                       fprintf(mh_sequences_new_fp, "%s%s\n", 
+                                       get_unseen_seq_name(), sequence);
+                       debug_print("wrote unseen sequence: '%s%s'\n", 
+                                       get_unseen_seq_name(), sequence);
+               }
+               /* rewrite the rest of the file */
+               if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
+                       while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
+                               if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
+                                       fprintf(mh_sequences_new_fp, "%s", buf);
+                       }
+                       fclose(mh_sequences_old_fp);
+               }
+               
+               fclose(mh_sequences_new_fp);
+               g_rename(mh_sequences_new, mh_sequences_old);
+               g_free(sequence);
+               procmsg_msg_list_free(msglist);
+       }
+       g_free(mh_sequences_old);
+       g_free(mh_sequences_new);
+       g_free(path);
+/*
+       g_timer_stop(timer);
+       printf("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
+       g_timer_destroy(timer);
+*/
+}
+
+static int mh_item_close(Folder *folder, FolderItem *item)
+{
+       mh_write_sequences(item, FALSE);
+       return 0;
+}