2006-08-28 [paul] 2.4.0cvs104
[claws.git] / src / mh.c
index b41ab8b8309e09accc785ff9673aec6ee8b25a26..d88d2f677f15efe265a479b44dd051fae4045225 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-2005 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
 #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,
@@ -68,9 +83,17 @@ static gint     mh_add_msgs          (Folder         *folder,
 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,
@@ -109,6 +132,13 @@ static gchar   *mh_item_get_path           (Folder *folder,
 
 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;
 
@@ -134,14 +164,18 @@ FolderClass *mh_get_class(void)
                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;
        }
@@ -189,16 +223,16 @@ gboolean mh_scan_required(Folder *folder, FolderItem *item)
                (s.st_mtime - 3600 != item->mtime)) {
                debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
                            path,
-                           s.st_mtime,
-                           item->mtime);
+                           (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,
-                   s.st_mtime,
-                   item->mtime);
+                   (long int) s.st_mtime,
+                   (long int) item->mtime);
        g_free(path);
        return FALSE;
 }
@@ -293,9 +327,10 @@ static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
 
        if (!is_file_exist(file)) {
                g_free(file);
+               g_free(path);
                return NULL;
        }
-
+       g_free(path);
        return file;
 }
 
@@ -305,7 +340,8 @@ static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
        gchar *file;
 
        g_return_val_if_fail(item != NULL, NULL);
-       g_return_val_if_fail(num > 0, NULL);
+       if (num <= 0)
+               return NULL;
 
        file = mh_fetch_msg(folder, item, num);
        if (!file) return NULL;
@@ -384,32 +420,60 @@ static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                destfile = mh_get_new_msg_filename(dest);
                if (destfile == NULL) return -1;
 
+#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
+
                if (relation != NULL)
                        g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
                g_free(destfile);
                dest->last_num++;
        }
-
+       mh_write_sequences(dest, TRUE);
        return dest->last_num;
 }
 
 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
+{
+       GSList msglist;
+
+       g_return_val_if_fail(msginfo != NULL, -1);
+
+       msglist.data = msginfo;
+       msglist.next = NULL;
+
+       return mh_copy_msgs(folder, dest, &msglist, NULL);      
+}
+
+static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist, 
+                        GRelation *relation)
 {
        gboolean dest_need_scan = FALSE;
        gchar *srcfile;
        gchar *destfile;
        gint filemode = 0;
        FolderItemPrefs *prefs;
+       MsgInfo *msginfo = NULL;
+       MsgInfoList *cur = NULL;
+       gint curnum = 0, total = 0;
+       gchar *srcpath = NULL;
+       gboolean full_fetch = FALSE;
+       time_t last_mtime = (time_t)0;
 
        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) {
@@ -417,6 +481,9 @@ static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
                return -1;
        }
 
+       if (msginfo->folder->folder != dest->folder)
+               full_fetch = TRUE;
+
        if (dest->last_num < 0) {
                mh_get_last_num(folder, dest);
                if (dest->last_num < 0) return -1;
@@ -424,57 +491,113 @@ static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
 
        prefs = dest->prefs;
 
-       srcfile = procmsg_get_message_file(msginfo);
-       destfile = mh_get_new_msg_filename(dest);
-       if (!destfile) {
-               g_free(srcfile);
-               return -1;
+       srcpath = folder_item_get_path(msginfo->folder);
+
+       dest_need_scan = mh_scan_required(dest->folder, dest);
+       last_mtime = dest->mtime;
+
+       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..."));
        }
-       
-       debug_print("Copying message %s%c%d to %s ...\n",
-                   msginfo->folder->path, G_DIR_SEPARATOR,
-                   msginfo->msgnum, dest->path);
-       
+       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 (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",
+                           msginfo->folder->path, G_DIR_SEPARATOR,
+                           msginfo->msgnum, dest->path);
 
-       if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
-       &&  dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
-               if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
+
+               if (MSG_IS_MOVE(msginfo->flags)) {
+                       msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
+                       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 {
+                               /* say unlinking's not necessary */
+                               msginfo->flags.tmp_flags |= MSG_MOVE_DONE;
+                       }
+               } else if (copy_file(srcfile, destfile, TRUE) < 0) {
+                       FILE_OP_ERROR(srcfile, "copy");
                        g_free(srcfile);
                        g_free(destfile);
-                       return -1;
+                       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;
                }
-       } else if (copy_file(srcfile, destfile, TRUE) < 0) {
-               FILE_OP_ERROR(srcfile, "copy");
+               if (relation)
+                       g_relation_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
                g_free(srcfile);
                g_free(destfile);
-               return -1;
+               dest->last_num++;
        }
 
-       dest_need_scan = mh_scan_required(dest->folder, dest);
-       if (!dest_need_scan)
-               dest->mtime = time(NULL);
-
+       g_free(srcpath);
+       mh_write_sequences(dest, TRUE);
 
-       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 (dest->mtime == last_mtime && !dest_need_scan)
+               dest->mtime = time(NULL);
+       
+       if (total > 100) {
+               statusbar_progress_all(0,0,0);
+               statusbar_pop_all();
        }
-
-       g_free(srcfile);
-       g_free(destfile);
-       dest->last_num++;
-
        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;
+
 }
 
 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
 {
        gboolean need_scan = FALSE;
+       time_t last_mtime = (time_t)0;
        gchar *file;
 
        g_return_val_if_fail(item != NULL, -1);
@@ -483,20 +606,89 @@ static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
        g_return_val_if_fail(file != NULL, -1);
 
        need_scan = mh_scan_required(folder, item);
+       last_mtime = item->mtime;
 
-       if (unlink(file) < 0) {
+       if (g_unlink(file) < 0) {
                FILE_OP_ERROR(file, "unlink");
                g_free(file);
                return -1;
        }
 
-       if (!need_scan)
+       if (item->mtime == last_mtime && !need_scan)
                item->mtime = time(NULL);
 
        g_free(file);
        return 0;
 }
 
+static gint mh_remove_msgs(Folder *folder, FolderItem *item, 
+                   MsgInfoList *msglist, GRelation *relation)
+{
+       gboolean need_scan = FALSE;
+       gchar *path, *file;
+       time_t last_mtime = (time_t)0;
+       MsgInfoList *cur;
+       gint total = 0, curnum = 0;
+
+       g_return_val_if_fail(item != NULL, -1);
+
+       path = folder_item_get_path(item);
+       
+       need_scan = mh_scan_required(folder, item);
+       last_mtime = item->mtime;
+
+       total = g_slist_length(msglist);
+       if (total > 100) {
+               statusbar_print_all(_("Deleting messages..."));
+       }
+
+       for (cur = msglist; cur; cur = cur->next) {
+               MsgInfo *msginfo = (MsgInfo *)cur->data;
+               GTuples *tuples;
+               gint num;
+               if (msginfo == NULL)
+                       continue;
+               if (MSG_IS_MOVE(msginfo->flags) && MSG_IS_MOVE_DONE(msginfo->flags)) {
+                       msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
+                       continue;
+               }
+               if (total > 100) {
+                       statusbar_progress_all(curnum, total, 100);
+                       if (curnum % 100 == 0)
+                               GTK_EVENTS_FLUSH();
+                       curnum++;
+               }
+
+               if (relation) {
+                       tuples = g_relation_select(relation, msginfo, 0);
+                       num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
+                       g_tuples_destroy(tuples);
+                       if (num <= 0)
+                               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);
+       }
+
+       if (total > 100) {
+               statusbar_progress_all(0,0,0);
+               statusbar_pop_all();
+       }
+       if (item->mtime == last_mtime && !need_scan)
+               item->mtime = time(NULL);
+
+       g_free(path);
+       return 0;
+}
+
 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
 {
        gchar *path;
@@ -509,6 +701,8 @@ static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
        val = remove_all_numbered_files(path);
        g_free(path);
 
+       mh_write_sequences(item, TRUE);
+
        return val;
 }
 
@@ -517,7 +711,7 @@ static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
 {
        struct stat s;
 
-       if (stat(itos(msginfo->msgnum), &s) < 0 ||
+       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) &&
@@ -574,7 +768,7 @@ static gint mh_create_tree(Folder *folder)
 
        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);
@@ -592,14 +786,17 @@ static gint mh_create_tree(Folder *folder)
 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
 {
        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);
 
-        if (folder_path[0] == G_DIR_SEPARATOR) {
+        /* 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);
@@ -615,8 +812,16 @@ static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
                                            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);
+       }
 
-       return path;
+       g_free(path);
+       return real_path;
 }
 
 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
@@ -662,7 +867,7 @@ static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
        path = folder_item_get_path(new_item);
        mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
                                            ".mh_sequences", NULL);
-       if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
+       if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
                fclose(mh_sequences_file);
        }
        g_free(mh_sequences_filename);
@@ -694,7 +899,7 @@ static gint mh_rename_folder(Folder *folder, FolderItem *item,
        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);
@@ -756,9 +961,9 @@ static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
        flags.perm_flags = MSG_NEW|MSG_UNREAD;
        flags.tmp_flags = 0;
 
-       if (item->stype == F_QUEUE) {
+       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);
        }
 
@@ -771,32 +976,6 @@ static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
        return msginfo;
 }
 
-#if 0
-static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
-{
-       gchar *entry;
-       gboolean result;
-
-       entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
-       result = is_dir_exist(entry);
-       g_free(entry);
-
-       return result;
-}
-
-/*
- * 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)
-{
-       return mh_is_maildir_one(path, "new") &&
-              mh_is_maildir_one(path, "cur") &&
-              mh_is_maildir_one(path, "tmp");
-}
-#endif
-
 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
 {
        FolderItem *item;
@@ -832,8 +1011,13 @@ static void mh_remove_missing_folder_items(Folder *folder)
 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 *real_path, *entry, *utf8entry, *utf8name;
        gint n_msg = 0;
@@ -844,11 +1028,20 @@ static void mh_scan_tree_recursive(FolderItem *item)
        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(real_path, "opendir");
                return;
        }
+#endif
        g_free(real_path);
 
        debug_print("scanning %s ...\n",
@@ -857,10 +1050,15 @@ static void mh_scan_tree_recursive(FolderItem *item)
        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(d->d_name);
+               utf8name = mh_filename_to_utf8(dir_name);
                if (item->path)
                        utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
                                                utf8name, NULL);
@@ -869,27 +1067,18 @@ static void mh_scan_tree_recursive(FolderItem *item)
                entry = mh_filename_from_utf8(utf8entry);
 
                if (
-#ifdef HAVE_DIRENT_D_TYPE
+#if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
                        d->d_type == DT_DIR ||
                        (d->d_type == DT_UNKNOWN &&
 #endif
-                       stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
-#ifdef HAVE_DIRENT_D_TYPE
+                       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;
 
-#if 0
-                       if (mh_is_maildir(entry)) {
-                               g_free(entry);
-                               g_free(utf8entry);
-                               g_free(utf8name);
-                               continue;
-                       }
-#endif
-
                        node = item->node;
                        for (node = node->children; node != NULL; node = node->next) {
                                FolderItem *cur_item = FOLDER_ITEM(node->data);
@@ -906,55 +1095,43 @@ static void mh_scan_tree_recursive(FolderItem *item)
 
                        if (!item->path) {
                                if (!folder->inbox &&
-                                   !strcmp(d->d_name, INBOX_DIR)) {
+                                   !strcmp(dir_name, INBOX_DIR)) {
                                        new_item->stype = F_INBOX;
                                        folder->inbox = new_item;
                                } else if (!folder->outbox &&
-                                          !strcmp(d->d_name, OUTBOX_DIR)) {
+                                          !strcmp(dir_name, OUTBOX_DIR)) {
                                        new_item->stype = F_OUTBOX;
                                        folder->outbox = new_item;
                                } else if (!folder->draft &&
-                                          !strcmp(d->d_name, DRAFT_DIR)) {
+                                          !strcmp(dir_name, DRAFT_DIR)) {
                                        new_item->stype = F_DRAFT;
                                        folder->draft = new_item;
                                } else if (!folder->queue &&
-                                          !strcmp(d->d_name, QUEUE_DIR)) {
+                                          !strcmp(dir_name, QUEUE_DIR)) {
                                        new_item->stype = F_QUEUE;
                                        folder->queue = new_item;
                                } else if (!folder->trash &&
-                                          !strcmp(d->d_name, TRASH_DIR)) {
+                                          !strcmp(dir_name, TRASH_DIR)) {
                                        new_item->stype = F_TRASH;
                                        folder->trash = new_item;
                                }
                        }
 
                        mh_scan_tree_recursive(new_item);
-               } else if (to_number(d->d_name) != -1) n_msg++;
+               } 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
 
        item->mtime = time(NULL);
-
-/*
-       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;
-       }
-*/
 }
 
 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
@@ -988,14 +1165,10 @@ static gboolean mh_rename_folder_func(GNode *node, gpointer data)
 
 static gchar *mh_filename_from_utf8(const gchar *path)
 {
-       const gchar *src_codeset = CS_UTF_8;
-       const gchar *dest_codeset = conv_get_locale_charset_str();
-       gchar *real_path;
+       gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
 
-       real_path = conv_codeset_strdup(path, src_codeset, dest_codeset);
        if (!real_path) {
                g_warning("mh_filename_from_utf8: faild to convert character set\n");
-               /* FIXME: show dialog? */
                real_path = g_strdup(path);
        }
 
@@ -1004,16 +1177,234 @@ static gchar *mh_filename_from_utf8(const gchar *path)
 
 static gchar *mh_filename_to_utf8(const gchar *path)
 {
-       const gchar *src_codeset = conv_get_locale_charset_str();
-       const gchar *dest_codeset = CS_UTF_8;
-       gchar *utf8path;
-
-       utf8path = conv_codeset_strdup(path, src_codeset, dest_codeset);
+       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");
-               /* FIXME: show dialog? */
                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;
+}