* src/folder.c
[claws.git] / src / folder.c
index 9500878af4ab9ab415b7c8d82982824c0184862a..a005c1c1f8257cb83b1c0eb4f4fce0bfdd2410e0 100644 (file)
@@ -33,7 +33,6 @@
 
 #include "intl.h"
 #include "folder.h"
-#include "folderview.h"
 #include "session.h"
 #include "imap.h"
 #include "news.h"
 #include "utils.h"
 #include "xml.h"
 #include "codeconv.h"
-#include "prefs.h"
-#include "prefs_common.h"
+#include "prefs_gtk.h"
 #include "account.h"
-#include "prefs_account.h"
 #include "filtering.h"
 #include "scoring.h"
 #include "prefs_folder_item.h"
 #include "procheader.h"
+#include "hooks.h"
+#include "log.h"
+
+/* Dependecies to be removed ?! */
+#include "prefs_common.h"
+#include "prefs_account.h"
+#include "prefs_folder_item.h"
 
 static GList *folder_list = NULL;
 
@@ -73,6 +77,7 @@ void folder_item_free_cache           (FolderItem *item);
 Folder *folder_new(FolderType type, const gchar *name, const gchar *path)
 {
        Folder *folder = NULL;
+       FolderItem *item;
 
        name = name ? name : path;
        switch (type) {
@@ -92,19 +97,22 @@ Folder *folder_new(FolderType type, const gchar *name, const gchar *path)
                return NULL;
        }
 
+       /* Create root folder item */
+       item = folder_item_new(folder, name, NULL);
+       item->folder = folder;
+       folder->node = g_node_new(item);
+       folder->data = NULL;
+
        return folder;
 }
 
 static void folder_init(Folder *folder, const gchar *name)
 {
-       FolderItem *item;
-
        g_return_if_fail(folder != NULL);
 
        folder_set_name(folder, name);
 
        /* Init folder data */
-       folder->type = F_UNKNOWN;
        folder->account = NULL;
        folder->inbox = NULL;
        folder->outbox = NULL;
@@ -113,19 +121,16 @@ static void folder_init(Folder *folder, const gchar *name)
        folder->trash = NULL;
 
        /* Init Folder functions */
+       folder->item_new = NULL;
+       folder->item_destroy = NULL;
        folder->fetch_msg = NULL;
-       folder->fetch_msginfo = NULL;
-       folder->fetch_msginfos = NULL;
+       folder->get_msginfo = NULL;
+       folder->get_msginfos = NULL;
        folder->get_num_list = NULL;
        folder->ui_func = NULL;
        folder->ui_func_data = NULL;
+       folder->change_flags = NULL;
        folder->check_msgnum_validity = NULL;
-
-       /* Create root folder item */
-       item = folder_item_new(name, NULL);
-       item->folder = folder;
-       folder->node = g_node_new(item);
-       folder->data = NULL;
 }
 
 void folder_local_folder_init(Folder *folder, const gchar *name,
@@ -145,22 +150,9 @@ void folder_remote_folder_init(Folder *folder, const gchar *name,
 void folder_destroy(Folder *folder)
 {
        g_return_if_fail(folder != NULL);
+       g_return_if_fail(folder->destroy != NULL);
 
-       switch (folder->type) {
-       case F_MBOX:
-               mbox_folder_destroy(MBOX_FOLDER(folder));
-       case F_MH:
-               mh_folder_destroy(MH_FOLDER(folder));
-               break;
-       case F_IMAP:
-               imap_folder_destroy(IMAP_FOLDER(folder));
-               break;
-       case F_NEWS:
-               news_folder_destroy(NEWS_FOLDER(folder));
-               break;
-       default:
-               break;
-       }
+       folder->destroy(folder);
 
        folder_list = g_list_remove(folder_list, folder);
 
@@ -198,30 +190,39 @@ Folder *maildir_folder_new(const gchar *name, const gchar *path)
 }
 #endif
 
-FolderItem *folder_item_new(const gchar *name, const gchar *path)
+FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path)
 {
-       FolderItem *item;
+       FolderItem *item = NULL;
 
-       item = g_new0(FolderItem, 1);
+       if (folder->item_new) {
+               item = folder->item_new(folder);
+       } else {
+               item = g_new0(FolderItem, 1);
+       }
+
+       g_return_val_if_fail(item != NULL, NULL);
 
        item->stype = F_NORMAL;
        item->name = g_strdup(name);
        item->path = g_strdup(path);
-       item->account = NULL;
        item->mtime = 0;
        item->new = 0;
        item->unread = 0;
+       item->unreadmarked = 0;
        item->total = 0;
        item->last_num = -1;
        item->cache = NULL;
        item->no_sub = FALSE;
        item->no_select = FALSE;
        item->collapsed = FALSE;
+       item->thread_collapsed = FALSE;
        item->threaded  = TRUE;
        item->ret_rcpt  = FALSE;
        item->opened    = FALSE;
        item->parent = NULL;
        item->folder = NULL;
+       item->account = NULL;
+       item->apply_sub = FALSE;
        item->mark_queue = NULL;
        item->data = NULL;
 
@@ -268,13 +269,20 @@ void folder_item_destroy(FolderItem *item)
 {
        g_return_if_fail(item != NULL);
 
-       debug_print(_("Destroying folder item %s\n"), item->path);
-       
-       if(item->cache)
+       debug_print("Destroying folder item %s\n", item->path);
+
+       if (item->cache)
                folder_item_free_cache(item);
        g_free(item->name);
        g_free(item->path);
-       g_free(item);
+
+       if (item->folder != NULL) {
+               if(item->folder->item_destroy) {
+                       item->folder->item_destroy(item->folder, item);
+               } else {
+                       g_free(item);
+               }
+       }
 }
 
 void folder_set_ui_func(Folder *folder, FolderUIFunc func, gpointer data)
@@ -308,11 +316,15 @@ gboolean folder_tree_destroy_func(GNode *node, gpointer data) {
 
 void folder_tree_destroy(Folder *folder)
 {
+       g_return_if_fail(folder != NULL);
+       g_return_if_fail(folder->node != NULL);
+       
        prefs_scoring_clear();
        prefs_filtering_clear();
 
        g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_tree_destroy_func, NULL);
-       g_node_destroy(folder->node);
+       if (folder->node)
+               g_node_destroy(folder->node);
 
        folder->inbox = NULL;
        folder->outbox = NULL;
@@ -406,7 +418,7 @@ void folder_write_list(void)
 
        fputs("</folderlist>\n", pfile->fp);
 
-       if (prefs_write_close(pfile) < 0)
+       if (prefs_file_close(pfile) < 0)
                g_warning("failed to write folder list.\n");
 }
 
@@ -416,30 +428,46 @@ gboolean folder_scan_tree_func(GNode *node, gpointer data)
        FolderItem *item = (FolderItem *)node->data;
        
        folder_item_restore_persist_prefs(item, pptable);
+
+       return FALSE;
 }
 
 void folder_scan_tree(Folder *folder)
 {
        GHashTable *pptable;
        
-       if(!folder->scan_tree)
+       if (!folder->scan_tree)
                return;
        
        pptable = folder_persist_prefs_new(folder);
        folder_tree_destroy(folder);
 
        folder->scan_tree(folder);
-       
+
        g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_scan_tree_func, pptable);
        folder_persist_prefs_free(pptable);
 
        prefs_matcher_read_config();
+
+       folder_write_list();
+}
+
+FolderItem *folder_create_folder(FolderItem *parent, const gchar *name)
+{
+       FolderItem *new_item;
+
+       new_item = parent->folder->create_folder(parent->folder, parent, name);
+       if (new_item)
+               new_item->cache = msgcache_new();
+
+       return new_item;
 }
 
 struct TotalMsgCount
 {
        guint new;
        guint unread;
+       guint unreadmarked;
        guint total;
 };
 
@@ -489,21 +517,23 @@ static void folder_count_total_msgs_func(FolderItem *item, gpointer data)
 
        count->new += item->new;
        count->unread += item->unread;
+       count->unreadmarked += item->unreadmarked;
        count->total += item->total;
 }
 
-void folder_count_total_msgs(guint *new, guint *unread, guint *total)
+void folder_count_total_msgs(guint *new, guint *unread, guint *unreadmarked, guint *total)
 {
        struct TotalMsgCount count;
 
-       count.new = count.unread = count.total = 0;
+       count.new = count.unread = count.unreadmarked = count.total = 0;
 
-       debug_print(_("Counting total number of messages...\n"));
+       debug_print("Counting total number of messages...\n");
 
        folder_func_to_all_folders(folder_count_total_msgs_func, &count);
 
        *new = count.new;
        *unread = count.unread;
+       *unreadmarked = count.unreadmarked;
        *total = count.total;
 }
 
@@ -728,14 +758,14 @@ FolderItem *folder_get_default_trash(void)
        return folder->trash;
 }
 
-#define CREATE_FOLDER_IF_NOT_EXIST(member, dir, type)  \
-{                                                      \
-       if (!folder->member) {                          \
-               item = folder_item_new(dir, dir);       \
-               item->stype = type;                     \
-               folder_item_append(rootitem, item);     \
-               folder->member = item;                  \
-       }                                               \
+#define CREATE_FOLDER_IF_NOT_EXIST(member, dir, type)          \
+{                                                              \
+       if (!folder->member) {                                  \
+               item = folder_item_new(folder, dir, dir);       \
+               item->stype = type;                             \
+               folder_item_append(rootitem, item);             \
+               folder->member = item;                          \
+       }                                                       \
 }
 
 void folder_set_missing_folders(void)
@@ -769,8 +799,73 @@ void folder_set_missing_folders(void)
        }
 }
 
+static gboolean folder_unref_account_func(GNode *node, gpointer data)
+{
+       FolderItem *item = node->data;
+       PrefsAccount *account = data;
+
+       if (item->account == account)
+               item->account = NULL;
+
+       return FALSE;
+}
+
+void folder_unref_account_all(PrefsAccount *account)
+{
+       Folder *folder;
+       GList *list;
+
+       if (!account) return;
+
+       for (list = folder_list; list != NULL; list = list->next) {
+               folder = list->data;
+               if (folder->account == account)
+                       folder->account = NULL;
+               g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
+                               folder_unref_account_func, account);
+       }
+}
+
 #undef CREATE_FOLDER_IF_NOT_EXIST
 
+gchar *folder_get_path(Folder *folder)
+{
+       gchar *path;
+
+       g_return_val_if_fail(folder != NULL, NULL);
+
+       switch(FOLDER_TYPE(folder)) {
+
+               case F_MH:
+                       path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
+                       break;
+
+               case F_IMAP:
+                       g_return_val_if_fail(folder->account != NULL, NULL);
+                       path = g_strconcat(get_imap_cache_dir(),
+                                          G_DIR_SEPARATOR_S,
+                                          folder->account->recv_server,
+                                          G_DIR_SEPARATOR_S,
+                                          folder->account->userid,
+                                          NULL);
+                       break;
+
+               case F_NEWS:
+                       g_return_val_if_fail(folder->account != NULL, NULL);
+                       path = g_strconcat(get_news_cache_dir(),
+                                          G_DIR_SEPARATOR_S,
+                                          folder->account->nntp_server,
+                                          NULL);
+                       break;
+
+               default:
+                       path = NULL;
+                       break;
+       }
+       
+       return path;
+}
+
 gchar *folder_item_get_path(FolderItem *item)
 {
        gchar *folder_path;
@@ -778,54 +873,37 @@ gchar *folder_item_get_path(FolderItem *item)
 
        g_return_val_if_fail(item != NULL, NULL);
 
-       if (FOLDER_TYPE(item->folder) == F_MH)
-               folder_path = g_strdup(LOCAL_FOLDER(item->folder)->rootpath);
-       else if (FOLDER_TYPE(item->folder) == F_MBOX) {
-               path = mbox_get_virtual_path(item);
-               if (path == NULL)
-                       return NULL;
-               folder_path = g_strconcat(get_mbox_cache_dir(),
-                                         G_DIR_SEPARATOR_S, path, NULL);
-               g_free(path);
-
-               return folder_path;
-       }
-       else if (FOLDER_TYPE(item->folder) == F_IMAP) {
-               g_return_val_if_fail(item->folder->account != NULL, NULL);
-               folder_path = g_strconcat(get_imap_cache_dir(),
-                                         G_DIR_SEPARATOR_S,
-                                         item->folder->account->recv_server,
-                                         G_DIR_SEPARATOR_S,
-                                         item->folder->account->userid,
-                                         NULL);
-       } else if (FOLDER_TYPE(item->folder) == F_NEWS) {
-               g_return_val_if_fail(item->folder->account != NULL, NULL);
-               folder_path = g_strconcat(get_news_cache_dir(),
-                                         G_DIR_SEPARATOR_S,
-                                         item->folder->account->nntp_server,
-                                         NULL);
-       } else
-               return NULL;
+       if(FOLDER_TYPE(item->folder) != F_MBOX) {
+               folder_path = folder_get_path(item->folder);
+               g_return_val_if_fail(folder_path != NULL, NULL);
 
-       g_return_val_if_fail(folder_path != NULL, NULL);
+               if (folder_path[0] == G_DIR_SEPARATOR) {
+                       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);
+               }
 
-       if (folder_path[0] == G_DIR_SEPARATOR) {
-               if (item->path)
-                       path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
-                                          item->path, NULL);
-               else
-                       path = g_strdup(folder_path);
+               g_free(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);
-       }
+               gchar *itempath;
 
-       g_free(folder_path);
+               itempath = mbox_get_virtual_path(item);
+               if (itempath == NULL)
+                       return NULL;
+               path = g_strconcat(get_mbox_cache_dir(),
+                                         G_DIR_SEPARATOR_S, itempath, NULL);
+               g_free(itempath);
+       }
        return path;
 }
 
@@ -853,187 +931,325 @@ void folder_item_set_default_flags(FolderItem *dest, MsgFlags *flags)
        }
 }
 
-typedef enum {
-    IN_CACHE  = 1 << 0,
-    IN_FOLDER = 1 << 1,
-} FolderScanInfo;
+static gint folder_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 gint folder_sort_folder_list(gconstpointer a, gconstpointer b)
+{
+       guint gint_a = GPOINTER_TO_INT(a);
+       guint gint_b = GPOINTER_TO_INT(b);
+       
+       return (gint_a - gint_b);
+}
+
+gint folder_item_open(FolderItem *item)
+{
+       if(((item->folder->type == F_IMAP) && !item->no_select) || (item->folder->type == F_NEWS)) {
+               folder_item_scan(item);
+       }
+
+       /* Processing */
+       if(item->prefs->processing != NULL) {
+               gchar *buf;
+               
+               buf = g_strdup_printf(_("Processing (%s)...\n"), item->path);
+               debug_print("%s\n", buf);
+               g_free(buf);
+       
+               folder_item_apply_processing(item);
+
+               debug_print("done.\n");
+       }
+
+       return 0;
+}
+
+void folder_item_close(FolderItem *item)
+{
+       GSList *mlist, *cur;
+       
+       g_return_if_fail(item != NULL);
+
+       if (item->new) {
+               mlist = folder_item_get_msg_list(item);
+               for (cur = mlist ; cur != NULL ; cur = cur->next) {
+                       MsgInfo * msginfo;
+
+                       msginfo = (MsgInfo *) cur->data;
+                       if (MSG_IS_NEW(msginfo->flags))
+                               procmsg_msginfo_unset_flags(msginfo, MSG_NEW, 0);
+                       procmsg_msginfo_free(msginfo);
+               }
+               g_slist_free(mlist);
+       }               
+
+       folder_item_write_cache(item);
+       
+       folder_item_update(item, F_ITEM_UPDATE_MSGCNT);
+}
 
 gint folder_item_scan(FolderItem *item)
 {
        Folder *folder;
-       GSList *folder_list, *cache_list, *elem, *new_list = NULL;
-       gint i;
-       guint min = 0xffffffff, max = 0, cache_max = 0;
-       FolderScanInfo *folderscaninfo;
-       guint newcnt = 0, unreadcnt = 0, totalcnt = 0;
-       
+       GSList *folder_list = NULL, *cache_list = NULL, *folder_list_cur, *cache_list_cur, *new_list = NULL;
+       guint newcnt = 0, unreadcnt = 0, totalcnt = 0, unreadmarkedcnt = 0;
+       guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
+       gboolean update_flags = 0;
+    
        g_return_val_if_fail(item != NULL, -1);
-       if(item->path == NULL) return -1;
+       if (item->path == NULL) return -1;
 
        folder = item->folder;
 
        g_return_val_if_fail(folder != NULL, -1);
        g_return_val_if_fail(folder->get_num_list != NULL, -1);
 
-       debug_print(_("Scanning folder %s for cache changes.\n"), item->path);
+       debug_print("Scanning folder %s for cache changes.\n", item->path);
 
        /* Get list of messages for folder and cache */
-       if(!folder->check_msgnum_validity || 
-          folder->check_msgnum_validity(folder, item)) {
-               if(!item->cache)
+       if (folder->get_num_list(item->folder, item, &folder_list) < 0) {
+               debug_print("Error fetching list of message numbers\n");
+               return(-1);
+       }
+
+       if (!folder->check_msgnum_validity || 
+           folder->check_msgnum_validity(folder, item)) {
+               if (!item->cache)
                        folder_item_read_cache(item);
                cache_list = msgcache_get_msg_list(item->cache);
        } else {
-               if(item->cache)
+               if (item->cache)
                        msgcache_destroy(item->cache);
                item->cache = msgcache_new();
                cache_list = NULL;
        }
-       folder_list = folder->get_num_list(item->folder, item);
 
-       /* Get min und max number in folder */
-       for(elem = cache_list; elem != NULL; elem = elem->next) {
-               MsgInfo *msginfo = (MsgInfo *)elem->data;
+       /* Sort both lists */
+       cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
+       folder_list = g_slist_sort(folder_list, folder_sort_folder_list);
 
-               min = MIN(msginfo->msgnum, min);
-               max = MAX(msginfo->msgnum, max);
-       }
-       cache_max = max;
-       for(elem = folder_list; elem != NULL; elem = elem->next) {
-               guint num = GPOINTER_TO_INT(elem->data);
+       cache_list_cur = cache_list;
+       folder_list_cur = folder_list;
 
-               min = MIN(num, min);
-               max = MAX(num, max);
+       if (cache_list_cur != NULL) {
+               GSList *cache_list_last;
+       
+               cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
+               cache_list_last = g_slist_last(cache_list);
+               cache_max_num = ((MsgInfo *)cache_list_last->data)->msgnum;
+       } else {
+               cache_cur_num = G_MAXINT;
+               cache_max_num = 0;
        }
 
-       debug_print("Folder message number range from %d to %d\n", min, max);
+       if (folder_list_cur != NULL) {
+               GSList *folder_list_last;
+       
+               folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
+               folder_list_last = g_slist_last(folder_list);
+               folder_max_num = GPOINTER_TO_INT(folder_list_last->data);
+       } else {
+               folder_cur_num = G_MAXINT;
+               folder_max_num = 0;
+       }
 
-       if(max == 0) {
-               for(elem = cache_list; elem != NULL; elem = elem->next) {
-                       MsgInfo *msginfo = (MsgInfo *)elem->data;
+       while ((cache_cur_num != G_MAXINT) || (folder_cur_num != G_MAXINT)) {
+               /*
+                *  Message only exists in the folder
+                *  Remember message for fetching
+                */
+               if (folder_cur_num < cache_cur_num) {
+                       gboolean add = FALSE;
+
+                       switch(folder->type) {
+                               case F_NEWS:
+                                       if (folder_cur_num < cache_max_num)
+                                               break;
+                                       
+                                       if (prefs_common.max_articles == 0) {
+                                               add = TRUE;
+                                       }
+
+                                       if (folder_max_num <= prefs_common.max_articles) {
+                                               add = TRUE;
+                                       } else if (folder_cur_num > (folder_max_num - prefs_common.max_articles)) {
+                                               add = TRUE;
+                                       }
+                                       break;
+                               default:
+                                       add = TRUE;
+                                       break;
+                       }
+                       
+                       if (add) {
+                               new_list = g_slist_prepend(new_list, GINT_TO_POINTER(folder_cur_num));
+                               debug_print("Remembered message %d for fetching\n", folder_cur_num);
+                       }
 
-                       procmsg_msginfo_free(msginfo);
-               }
-               g_slist_free(folder_list);
-               g_slist_free(cache_list);
+                       /* Move to next folder number */
+                       folder_list_cur = folder_list_cur->next;
 
-               return 0;
-       }
+                       if (folder_list_cur != NULL)
+                               folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
+                       else
+                               folder_cur_num = G_MAXINT;
 
-       folderscaninfo = g_new0(FolderScanInfo, max - min + 1);
+                       continue;
+               }
 
-       for(elem = folder_list; elem != NULL; elem = elem->next) {
-               guint num = GPOINTER_TO_INT(elem->data);
+               /*
+                *  Message only exists in the cache
+                *  Remove the message from the cache
+                */
+               if (cache_cur_num < folder_cur_num) {
+                       msgcache_remove_msg(item->cache, cache_cur_num);
+                       debug_print("Removed message %d from cache.\n", cache_cur_num);
 
-               folderscaninfo[num - min] |= IN_FOLDER;
-       }
-       for(elem = cache_list; elem != NULL; elem = elem->next) {
-               MsgInfo *msginfo = (MsgInfo *)elem->data;
+                       /* Move to next cache number */
+                       cache_list_cur = cache_list_cur->next;
 
-               folderscaninfo[msginfo->msgnum - min] |= IN_CACHE;
-               procmsg_msginfo_free(msginfo);
-       }
+                       if (cache_list_cur != NULL)
+                               cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
+                       else
+                               cache_cur_num = G_MAXINT;
 
-       for(i = max - min; i >= 0; i--) {
-               guint num;
+                       update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
 
-               num = i + min;
-               /* Add message to cache if in folder and not in cache */
-               if( (folderscaninfo[i] & IN_FOLDER) && 
-                  !(folderscaninfo[i] & IN_CACHE) && 
-                   (folder->type != F_NEWS ||
-                       (((prefs_common.max_articles == 0) || (num > (max - prefs_common.max_articles))) &&
-                       (num > cache_max)))
-                   ) {
-                       new_list = g_slist_prepend(new_list, GINT_TO_POINTER(num));
-                       debug_print(_("Remembered message %d for fetching\n"), num);
-               }
-               /* Remove message from cache if not in folder and in cache */
-               if(!(folderscaninfo[i] & IN_FOLDER) && 
-                   (folderscaninfo[i] & IN_CACHE)) {
-                       msgcache_remove_msg(item->cache, i + min);
-                       debug_print(_("Removed message %d from cache.\n"), num);
+                       continue;
                }
-               /* Check if msginfo needs update if in cache and in folder */
-               if((folderscaninfo[i] & IN_FOLDER) && 
-                  (folderscaninfo[i] & IN_CACHE) &&
-                  (folder->is_msg_changed != NULL)) {
+
+               /*
+                *  Message number exists in folder and cache!
+                *  Check if the message has been modified
+                */
+               if (cache_cur_num == folder_cur_num) {
                        MsgInfo *msginfo;
 
-                       msginfo = msgcache_get_msg(item->cache, num);
-                       if(folder->is_msg_changed(folder, item, msginfo)) {
+                       msginfo = msgcache_get_msg(item->cache, folder_cur_num);
+                       if (folder->is_msg_changed && folder->is_msg_changed(folder, item, msginfo)) {
                                MsgInfo *newmsginfo;
 
                                msgcache_remove_msg(item->cache, msginfo->msgnum);
 
-                               newmsginfo = folder->fetch_msginfo(folder, item, num);
-                               msgcache_add_msg(item->cache, newmsginfo);
-                               if(MSG_IS_NEW(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
-                                       newcnt++;
-                               if(MSG_IS_UNREAD(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
-                                       unreadcnt++;
-                               procmsg_msginfo_free(newmsginfo);
-
-                               debug_print(_("Updated msginfo for message %d.\n"), num);
+                               if (NULL != (newmsginfo = folder->get_msginfo(folder, item, folder_cur_num))) {
+                                       msgcache_add_msg(item->cache, newmsginfo);
+                                       if (MSG_IS_NEW(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
+                                               newcnt++;
+                                       if (MSG_IS_UNREAD(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
+                                               unreadcnt++;
+                                       if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
+                                               unreadmarkedcnt++;
+                                       if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
+                                               procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
+                                       procmsg_msginfo_free(newmsginfo);
+                               }                                       
+
+                               debug_print("Updated msginfo for message %d.\n", folder_cur_num);
                        } else {
-                               if(MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
                                        newcnt++;
-                               if(MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
                                        unreadcnt++;
+                               if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
+                                       unreadmarkedcnt++;
+                               if (!MSG_IS_IGNORE_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD))
+                                       procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
                        }
                        totalcnt++;
                        procmsg_msginfo_free(msginfo);
+
+                       /* Move to next folder and cache number */
+                       cache_list_cur = cache_list_cur->next;
+                       folder_list_cur = folder_list_cur->next;
+
+                       if (cache_list_cur != NULL)
+                               cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
+                       else
+                               cache_cur_num = G_MAXINT;
+
+                       if (folder_list_cur != NULL)
+                               folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
+                       else
+                               folder_cur_num = G_MAXINT;
+
+                       update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
+
+                       continue;
                }
        }
 
-       if(folder->fetch_msginfos) {
+       for(cache_list_cur = cache_list; cache_list_cur != NULL; cache_list_cur = g_slist_next(cache_list_cur)) {
+               procmsg_msginfo_free((MsgInfo *) cache_list_cur->data);
+       }
+
+       g_slist_free(cache_list);
+       g_slist_free(folder_list);
+
+       if (folder->get_msginfos) {
+               GSList *elem;
                GSList *newmsg_list;
                MsgInfo *msginfo;
                
-               if(new_list) {
-                       newmsg_list = folder->fetch_msginfos(folder, item, new_list);
-                       for(elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
+               if (new_list) {
+                       newmsg_list = folder->get_msginfos(folder, item, new_list);
+                       for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
                                msginfo = (MsgInfo *) elem->data;
                                msgcache_add_msg(item->cache, msginfo);
-                               if(MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
                                        newcnt++;
-                               if(MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
                                        unreadcnt++;
+                               if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
+                                       unreadmarkedcnt++;
+                               if (procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD))
+                                       procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
                                totalcnt++;
                                procmsg_msginfo_free(msginfo);
+
+                               update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
                        }
                        g_slist_free(newmsg_list);
-                       folderview_update_item(item, FALSE);
                }
-       } else if (folder->fetch_msginfo) {
-               for(elem = new_list; elem != NULL; elem = g_slist_next(elem)) {
+       } else if (folder->get_msginfo) {
+               GSList *elem;
+       
+               for (elem = new_list; elem != NULL; elem = g_slist_next(elem)) {
                        MsgInfo *msginfo;
                        guint num;
 
                        num = GPOINTER_TO_INT(elem->data);
-                       msginfo = folder->fetch_msginfo(folder, item, num);
-                       if(msginfo != NULL) {
+                       msginfo = folder->get_msginfo(folder, item, num);
+                       if (msginfo != NULL) {
                                msgcache_add_msg(item->cache, msginfo);
-                               if(MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
                                    newcnt++;
-                               if(MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
                                    unreadcnt++;
+                               if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
+                                       unreadmarkedcnt++;
+                               if (procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD))
+                                       procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
                                totalcnt++;
                                procmsg_msginfo_free(msginfo);
-                               debug_print(_("Added newly found message %d to cache.\n"), num);
+                               debug_print("Added newly found message %d to cache.\n", num);
                        }
+
+                       update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
                }
-               folderview_update_item(item, FALSE);
        }
 
        item->new = newcnt;
        item->unread = unreadcnt;
        item->total = totalcnt;
-       
-       g_slist_free(folder_list);
-       g_slist_free(cache_list);
+       item->unreadmarked = unreadmarkedcnt;
        g_slist_free(new_list);
-       g_free(folderscaninfo);
+
+       folder_item_update(item, update_flags);
 
        return 0;
 }
@@ -1053,7 +1269,7 @@ void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
 {
        gint *memusage = (gint *)data;
 
-       if(item->cache == NULL)
+       if (item->cache == NULL)
                return;
        
        *memusage += msgcache_get_memory_usage(item->cache);
@@ -1072,13 +1288,16 @@ void folder_find_expired_caches(FolderItem *item, gpointer data)
        GSList **folder_item_list = (GSList **)data;
        gint difftime, expiretime;
        
-       if(item->cache == NULL)
+       if (item->cache == NULL)
+               return;
+
+       if (item->opened > 0)
                return;
 
        difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
        expiretime = prefs_common.cache_min_keep_time * 60;
-       debug_print(_("Cache unused time: %d (Expire time: %d)\n"), difftime, expiretime);
-       if(difftime > expiretime) {
+       debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
+       if (difftime > expiretime) {
                *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
        }
 }
@@ -1087,9 +1306,12 @@ void folder_item_free_cache(FolderItem *item)
 {
        g_return_if_fail(item != NULL);
        
-       if(item->cache == NULL)
+       if (item->cache == NULL)
                return;
        
+       if (item->opened > 0)
+               return;
+
        folder_item_write_cache(item);
        msgcache_destroy(item->cache);
        item->cache = NULL;
@@ -1100,21 +1322,21 @@ void folder_clean_cache_memory()
        gint memusage = 0;
 
        folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);       
-       debug_print(_("Total cache memory usage: %d\n"), memusage);
+       debug_print("Total cache memory usage: %d\n", memusage);
        
-       if(memusage > (prefs_common.cache_max_mem_usage * 1024)) {
+       if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
                GSList *folder_item_list = NULL, *listitem;
                
-               debug_print(_("Trying to free cache memory\n"));
+               debug_print("Trying to free cache memory\n");
 
                folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);      
                listitem = folder_item_list;
                while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
                        FolderItem *item = (FolderItem *)(listitem->data);
 
-                       debug_print(_("Freeing cache memory for %s\n"), item->path);
+                       debug_print("Freeing cache memory for %s\n", item->path);
                        memusage -= msgcache_get_memory_usage(item->cache);
-                       folder_item_free_cache(item);
+                       folder_item_free_cache(item);
                        listitem = listitem->next;
                }
                g_slist_free(folder_item_list);
@@ -1130,7 +1352,7 @@ void folder_item_read_cache(FolderItem *item)
        cache_file = folder_item_get_cache_file(item);
        mark_file = folder_item_get_mark_file(item);
        item->cache = msgcache_read_cache(item, cache_file);
-       if(!item->cache) {
+       if (!item->cache) {
                item->cache = msgcache_new();
                folder_item_scan(item);
        }
@@ -1152,12 +1374,12 @@ void folder_item_write_cache(FolderItem *item)
                return;
 
        id = folder_item_get_identifier(item);
-       debug_print(_("Save cache for folder %s\n"), id);
+       debug_print("Save cache for folder %s\n", id);
        g_free(id);
 
        cache_file = folder_item_get_cache_file(item);
        mark_file = folder_item_get_mark_file(item);
-       if(msgcache_write(cache_file, mark_file, item->cache) < 0) {
+       if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
                prefs = item->prefs;
                if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
                        /* for cache file */
@@ -1172,7 +1394,7 @@ void folder_item_write_cache(FolderItem *item)
        g_free(mark_file);
 }
 
-MsgInfo *folder_item_fetch_msginfo(FolderItem *item, gint num)
+MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
 {
        Folder *folder;
        MsgInfo *msginfo;
@@ -1180,22 +1402,43 @@ MsgInfo *folder_item_fetch_msginfo(FolderItem *item, gint num)
        g_return_val_if_fail(item != NULL, NULL);
        
        folder = item->folder;
-       if(!item->cache)
+       if (!item->cache)
                folder_item_read_cache(item);
        
-       if((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
+       if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
                return msginfo;
        
-       g_return_val_if_fail(folder->fetch_msginfo, NULL);
-       msginfo = folder->fetch_msginfo(folder, item, num);
-       return msginfo;
+       g_return_val_if_fail(folder->get_msginfo, NULL);
+       if ((msginfo = folder->get_msginfo(folder, item, num)) != NULL) {
+               msgcache_add_msg(item->cache, msginfo);
+               return msginfo;
+       }
+       
+       return NULL;
+}
+
+MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
+{
+       Folder *folder;
+       MsgInfo *msginfo;
+       
+       g_return_val_if_fail(item != NULL, NULL);
+       
+       folder = item->folder;
+       if (!item->cache)
+               folder_item_read_cache(item);
+       
+       if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
+               return msginfo;
+
+       return NULL;
 }
 
 GSList *folder_item_get_msg_list(FolderItem *item)
 {
        g_return_val_if_fail(item != NULL, NULL);
        
-       if(item->cache == 0)
+       if (item->cache == 0)
                folder_item_read_cache(item);
 
        g_return_val_if_fail(item->cache != NULL, NULL);
@@ -1216,6 +1459,41 @@ gchar *folder_item_fetch_msg(FolderItem *item, gint num)
        return folder->fetch_msg(folder, item, num);
 }
 
+static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
+{
+       static HeaderEntry hentry[] = {{"Message-ID:",  NULL, TRUE},
+                                      {NULL,           NULL, FALSE}};
+       FILE *fp;
+       MsgInfo *msginfo;
+       gint msgnum = 0;
+       gchar buf[BUFFSIZE];
+
+       if ((fp = fopen(file, "rb")) == NULL)
+               return 0;
+
+       if ((dest->stype == F_QUEUE) || (dest->stype == F_DRAFT))
+               while (fgets(buf, sizeof(buf), fp) != NULL)
+                       if (buf[0] == '\r' || buf[0] == '\n') break;
+
+       procheader_get_header_fields(fp, hentry);
+       if (hentry[0].body) {
+               extract_parenthesis(hentry[0].body, '<', '>');
+               remove_space(hentry[0].body);
+               if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
+                       msgnum = msginfo->msgnum;
+                       procmsg_msginfo_free(msginfo);
+
+                       debug_print("found message as uid %d\n", msgnum);
+               }
+       }
+       
+       g_free(hentry[0].body);
+       hentry[0].body = NULL;
+       fclose(fp);
+
+       return msgnum;
+}
+
 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
                         gboolean remove_source)
 {
@@ -1233,21 +1511,38 @@ gint folder_item_add_msg(FolderItem *dest, const gchar *file,
        if (!dest->cache)
                folder_item_read_cache(dest);
 
-       num = folder->add_msg(folder, dest, file, remove_source);
+       num = folder->add_msg(folder, dest, file, FALSE);
 
         if (num > 0) {
-               msginfo = folder->fetch_msginfo(folder, dest, num);
-
-               if(MSG_IS_NEW(msginfo->flags))
-                       dest->new++;
-               if(MSG_IS_UNREAD(msginfo->flags))
-                       dest->unread++;
-               dest->total++;
+               msginfo = folder->get_msginfo(folder, dest, num);
+
+               if (msginfo != NULL) {
+                       if (MSG_IS_NEW(msginfo->flags))
+                               dest->new++;
+                       if (MSG_IS_UNREAD(msginfo->flags))
+                               dest->unread++;
+                       if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
+                               dest->unreadmarked++;
+                       if (procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD))
+                               procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
+                       dest->total++;
+
+                       msgcache_add_msg(dest->cache, msginfo);
+                       procmsg_msginfo_free(msginfo);
+
+                       folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
+               }
 
                 dest->last_num = num;
-                msgcache_add_msg(dest->cache, msginfo);
-               procmsg_msginfo_free(msginfo);
-        }
+        } else if (num == 0) {
+               folder_item_scan(dest);
+               num = folder_item_get_msg_num_by_file(dest, file);
+       }
+
+       if (num >= 0 && remove_source) {
+               if (unlink(file) < 0)
+                       FILE_OP_ERROR(file, "unlink");
+       }
 
        return num;
 }
@@ -1270,12 +1565,153 @@ gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
        return num;
 }
 */
+               
+FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest) 
+{
+       GSList *mlist;
+       GSList *cur;
+       FolderItem *new_item;
+       FolderItem *next_item;
+       GNode *srcnode;
+       int cnt = 0;
+       gchar *old_id, *new_id;
+
+       mlist = folder_item_get_msg_list(src);
+
+       /* move messages */
+       debug_print("Moving %s to %s\n", src->path, dest->path);
+       new_item = folder_create_folder(dest, g_basename(src->path));
+       if (new_item == NULL) {
+               printf("Can't create folder\n");
+               return NULL;
+       }
+       
+       if (new_item->folder == NULL)
+               new_item->folder = dest->folder;
+
+       /* move messages */
+       for (cur = mlist ; cur != NULL ; cur = cur->next) {
+               MsgInfo * msginfo;
+               cnt++;
+               if (cnt%500)
+                       log_message(_("Moving %s to %s (%d%%)...\n"), src->name, 
+                                       new_item->path,
+                                       100*cnt/g_slist_length(mlist));
+               msginfo = (MsgInfo *) cur->data;
+               folder_item_move_msg(new_item, msginfo);
+
+               procmsg_msginfo_free(msginfo);
+       }
+       
+       /*copy prefs*/
+       prefs_folder_item_copy_prefs(src, new_item);
+       new_item->collapsed = src->collapsed;
+       new_item->thread_collapsed = src->thread_collapsed;
+       new_item->threaded  = src->threaded;
+       new_item->ret_rcpt  = src->ret_rcpt;
+       new_item->hide_read_msgs = src->hide_read_msgs;
+       new_item->sort_key  = src->sort_key;
+       new_item->sort_type = src->sort_type;
+
+       prefs_matcher_write_config();
+       
+       /* recurse */
+       srcnode = src->folder->node;    
+       srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
+       srcnode = srcnode->children;
+       while (srcnode != NULL) {
+               if (srcnode && srcnode->data) {
+                       next_item = (FolderItem*) srcnode->data;
+                       srcnode = srcnode->next;
+                       if (folder_item_move_recursive(next_item, new_item) == NULL)
+                               return NULL;
+               }
+       }
+       old_id = folder_item_get_identifier(src);
+       new_id = folder_item_get_identifier(new_item);
+       debug_print("updating rules : %s => %s\n", old_id, new_id);
+       
+       src->folder->remove_folder(src->folder, src);
+       folder_write_list();
+
+       if (old_id != NULL && new_id != NULL)
+               prefs_filtering_rename_path(old_id, new_id);
+       g_free(old_id);
+       g_free(new_id);
+
+       return new_item;
+}
+
+gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item)
+{
+       FolderItem *tmp = dest->parent;
+       gchar * src_identifier, * dst_identifier;
+       gchar * phys_srcpath, * phys_dstpath;
+       GNode *src_node;
+       
+       while (tmp) {
+               if (tmp == src) {
+                       return F_MOVE_FAILED_DEST_IS_CHILD;
+               }
+               tmp = tmp->parent;
+       }
+       
+       tmp = src->parent;
+       
+       src_identifier = folder_item_get_identifier(src);
+       dst_identifier = folder_item_get_identifier(dest);
+       
+       if(dst_identifier == NULL && dest->folder && dest->parent == NULL) {
+               /* dest can be a root folder */
+               dst_identifier = folder_get_identifier(dest->folder);
+       }
+       if (src_identifier == NULL || dst_identifier == NULL) {
+               printf("Can't get identifiers\n");
+               return F_MOVE_FAILED;
+       }
+
+       if (src->folder != dest->folder) {
+               return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
+       }
+
+       phys_srcpath = folder_item_get_path(src);
+       phys_dstpath = g_strconcat(folder_item_get_path(dest),G_DIR_SEPARATOR_S,g_basename(phys_srcpath),NULL);
+
+       if (src->parent == dest || src == dest) {
+               g_free(src_identifier);
+               g_free(dst_identifier);
+               g_free(phys_srcpath);
+               g_free(phys_dstpath);
+               return F_MOVE_FAILED_DEST_IS_PARENT;
+       }
+       debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
+       if ((tmp = folder_item_move_recursive(src, dest)) == NULL) {
+               return F_MOVE_FAILED;
+       }
+       
+       /* update rules */
+       src_node = g_node_find(src->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, src);
+       if (src_node) 
+               g_node_destroy(src_node);
+       else
+               debug_print("can't remove node: it's null!\n");
+       /* not to much worry if remove fails, move has been done */
+       
+       g_free(src_identifier);
+       g_free(dst_identifier);
+       g_free(phys_srcpath);
+       g_free(phys_dstpath);
+
+       *new_item = tmp;
+
+       return F_MOVE_OK;
+}
 
 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
 {
        Folder *folder;
        gint num;
-       Folder * src_folder;
+       Folder *src_folder;
 
        g_return_val_if_fail(dest != NULL, -1);
        g_return_val_if_fail(msginfo != NULL, -1);
@@ -1291,39 +1727,48 @@ gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
 
        num = folder->copy_msg(folder, dest, msginfo);
        
-       if (num != -1) {
+       if (num > 0) {
                MsgInfo *newmsginfo;
+    
+               /* Add new msginfo to dest folder */
+               if (NULL != (newmsginfo = folder->get_msginfo(folder, dest, num))) {
+                       newmsginfo->flags.perm_flags = msginfo->flags.perm_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);
+                       msgcache_add_msg(dest->cache, newmsginfo);
+
+                       if (MSG_IS_NEW(newmsginfo->flags))
+                               dest->new++;
+                       if (MSG_IS_UNREAD(newmsginfo->flags))
+                               dest->unread++;
+                       if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
+                               dest->unreadmarked++;
+                       if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
+                               procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
+                       dest->total++;
+                       folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
+
+                       procmsg_msginfo_free(newmsginfo);
+               }
 
-               newmsginfo = folder->fetch_msginfo(folder, dest, num);
-               newmsginfo->flags.perm_flags = msginfo->flags.perm_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);
-               msgcache_add_msg(dest->cache, newmsginfo);
-
-               /* CLAWS */
-               if(src_folder->remove_msg) {
-                       src_folder->remove_msg(src_folder,
-                                              msginfo->folder,
+               /* remove source message from it's folder */
+               if (src_folder->remove_msg) {
+                       src_folder->remove_msg(src_folder, msginfo->folder,
                                               msginfo->msgnum);
+                       msgcache_remove_msg(msginfo->folder->cache, msginfo->msgnum);
+
+                       if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               msginfo->folder->new--;
+                       if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               msginfo->folder->unread--;
+                       if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
+                               msginfo->folder->unreadmarked--;
+                       msginfo->folder->total--;
+                       folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
                }
-                msgcache_remove_msg(msginfo->folder->cache, msginfo->msgnum);
-
-               if (MSG_IS_NEW(msginfo->flags))
-                       msginfo->folder->new--;
-               if (MSG_IS_NEW(newmsginfo->flags))
-                       dest->new++;
-               if (MSG_IS_UNREAD(msginfo->flags))
-                       msginfo->folder->unread--;
-               if (MSG_IS_UNREAD(newmsginfo->flags))
-                       dest->unread++;
-               msginfo->folder->total--;
-               dest->total++;
-
-               procmsg_msginfo_free(newmsginfo);
        }
        
        if (folder->finished_copy)
@@ -1355,8 +1800,9 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
 {
        Folder *folder;
-       FolderItem * item;
-       GSList * l;
+       FolderItem *item;
+       GSList *newmsgnums = NULL;
+       GSList *l, *l2;
        gint num;
 
        g_return_val_if_fail(dest != NULL, -1);
@@ -1367,22 +1813,39 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
        g_return_val_if_fail(folder->copy_msg != NULL, -1);
        g_return_val_if_fail(folder->remove_msg != NULL, -1);
 
-       if (!dest->cache) folder_item_read_cache(dest);
-
+       /* 
+        * Copy messages to destination folder and 
+        * store new message numbers in newmsgnums
+        */
        item = NULL;
-       for(l = msglist ; l != NULL ; l = g_slist_next(l)) {
+       for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
                MsgInfo * msginfo = (MsgInfo *) l->data;
 
                if (!item && msginfo->folder != NULL)
                        item = msginfo->folder;
-               if (!item->cache) folder_item_read_cache(dest);
 
                num = folder->copy_msg(folder, dest, msginfo);
-               if (num != -1) {
+               newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
+       }
+
+       /* Read cache for dest folder */
+       if (!dest->cache) folder_item_read_cache(dest);
+       
+       /* 
+        * Fetch new MsgInfos for new messages in dest folder,
+        * add them to the msgcache and update folder message counts
+        */
+       l2 = newmsgnums;
+       for (l = msglist; l != NULL; l = g_slist_next(l)) {
+               MsgInfo *msginfo = (MsgInfo *) l->data;
+
+               num = GPOINTER_TO_INT(l2->data);
+
+               if (num > 0) {
                        MsgInfo *newmsginfo;
 
-                       newmsginfo = folder->fetch_msginfo(folder, dest, num);
-                       if(newmsginfo) {
+                       newmsginfo = folder->get_msginfo(folder, dest, num);
+                       if (newmsginfo) {
                                newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
                                if (dest->stype == F_OUTBOX ||
                                    dest->stype == F_QUEUE  ||
@@ -1392,29 +1855,60 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
                                                             MSG_NEW|MSG_UNREAD|MSG_DELETED);
                                msgcache_add_msg(dest->cache, newmsginfo);
 
-                               if (MSG_IS_NEW(msginfo->flags))
-                                       msginfo->folder->new--;
                                if (MSG_IS_NEW(newmsginfo->flags))
                                        dest->new++;
-                               if (MSG_IS_UNREAD(msginfo->flags))
-                                       msginfo->folder->unread--;
                                if (MSG_IS_UNREAD(newmsginfo->flags))
                                        dest->unread++;
-                               msginfo->folder->total--;
+                               if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
+                                       dest->unreadmarked++;
+                               if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
+                                       procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
                                dest->total++;
+                               folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
 
                                procmsg_msginfo_free(newmsginfo);
                        }
+               }
+               l2 = g_slist_next(l2);
+       }
+
+       /*
+        * Remove source messages from their folders if
+        * copying was successfull and update folder
+        * message counts
+        */
+       l2 = newmsgnums;
+       for (l = msglist; l != NULL; l = g_slist_next(l)) {
+               MsgInfo *msginfo = (MsgInfo *) l->data;
+
+               num = GPOINTER_TO_INT(l2->data);
+               
+               if (num > 0) {
                        item->folder->remove_msg(item->folder,
                                                 msginfo->folder,
                                                 msginfo->msgnum);
+                       if (!item->cache)
+                               folder_item_read_cache(item);
                        msgcache_remove_msg(item->cache, msginfo->msgnum);
+
+                       if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               msginfo->folder->new--;
+                       if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               msginfo->folder->unread--;
+                       if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
+                               msginfo->folder->unreadmarked--;
+                       msginfo->folder->total--;                       
+                       folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
                }
-       }
+
+               l2 = g_slist_next(l2);
+       }
+
 
        if (folder->finished_copy)
                folder->finished_copy(folder, dest);
 
+       g_slist_free(newmsgnums);
        return dest->last_num;
 }
 
@@ -1452,26 +1946,32 @@ gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
        if (!dest->cache) folder_item_read_cache(dest);
        
        num = folder->copy_msg(folder, dest, msginfo);
-       if (num != -1) {
+       if (num > 0) {
                MsgInfo *newmsginfo;
 
-               newmsginfo = folder->fetch_msginfo(folder, dest, num);
-               newmsginfo->flags.perm_flags = msginfo->flags.perm_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);
-               msgcache_add_msg(dest->cache, newmsginfo);
-
-               if (MSG_IS_NEW(newmsginfo->flags))
-                       dest->new++;
-               if (MSG_IS_UNREAD(newmsginfo->flags))
-                       dest->unread++;
-               dest->total++;
-
-               procmsg_msginfo_free(newmsginfo);
+               if (NULL != (newmsginfo = folder->get_msginfo(folder, dest, num))) {
+                       newmsginfo->flags.perm_flags = msginfo->flags.perm_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);
+                       msgcache_add_msg(dest->cache, newmsginfo);
+
+                       if (MSG_IS_NEW(newmsginfo->flags))
+                               dest->new++;
+                       if (MSG_IS_UNREAD(newmsginfo->flags))
+                               dest->unread++;
+                       if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
+                               dest->unreadmarked++;
+                       if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
+                               procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
+                       dest->total++;
+                       folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
+
+                       procmsg_msginfo_free(newmsginfo);
+               }                       
        }
 
        if (folder->finished_copy)
@@ -1504,7 +2004,8 @@ gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
 {
        Folder *folder;
        gint num;
-       GSList * l;
+       GSList *newmsgnums = NULL;
+       GSList *l, *l2;
 
        g_return_val_if_fail(dest != NULL, -1);
        g_return_val_if_fail(msglist != NULL, -1);
@@ -1513,17 +2014,35 @@ gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
  
        g_return_val_if_fail(folder->copy_msg != NULL, -1);
 
-       if (!dest->cache) folder_item_read_cache(dest);
-
-       for(l = msglist ; l != NULL ; l = g_slist_next(l)) {
+       /* 
+        * Copy messages to destination folder and 
+        * store new message numbers in newmsgnums
+        */
+       for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
                MsgInfo * msginfo = (MsgInfo *) l->data;
 
                num = folder->copy_msg(folder, dest, msginfo);
-               if (num != -1) {
+               newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
+       }
+
+       /* Read cache for dest folder */
+       if (!dest->cache) folder_item_read_cache(dest);
+
+       /* 
+        * Fetch new MsgInfos for new messages in dest folder,
+        * add them to the msgcache and update folder message counts
+        */
+       l2 = newmsgnums;
+       for (l = msglist; l != NULL; l = g_slist_next(l)) {
+               MsgInfo *msginfo = (MsgInfo *) l->data;
+
+               num = GPOINTER_TO_INT(l2->data);
+
+               if (num > 0) {
                        MsgInfo *newmsginfo;
 
-                       newmsginfo = folder->fetch_msginfo(folder, dest, num);
-                       if(newmsginfo) {
+                       newmsginfo = folder->get_msginfo(folder, dest, num);
+                       if (newmsginfo) {
                                newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
                                if (dest->stype == F_OUTBOX ||
                                    dest->stype == F_QUEUE  ||
@@ -1537,16 +2056,23 @@ gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
                                        dest->new++;
                                if (MSG_IS_UNREAD(newmsginfo->flags))
                                        dest->unread++;
+                               if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
+                                       dest->unreadmarked++;
+                               if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
+                                       procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
                                dest->total++;
+                               folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
 
                                procmsg_msginfo_free(newmsginfo);
                        }
                }
+               l2 = g_slist_next(l2);
        }
-
+       
        if (folder->finished_copy)
                folder->finished_copy(folder, dest);
 
+       g_slist_free(newmsgnums);
        return dest->last_num;
 }
 
@@ -1564,25 +2090,40 @@ gint folder_item_remove_msg(FolderItem *item, gint num)
        ret = folder->remove_msg(folder, item, num);
 
        msginfo = msgcache_get_msg(item->cache, num);
-       if(MSG_IS_NEW(msginfo->flags))
-               item->new--;
-       if(MSG_IS_UNREAD(msginfo->flags))
-               item->unread--;
+       if (msginfo != NULL) {
+               if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                       item->new--;
+               if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                       item->unread--;
+               if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
+                       item->unreadmarked--;
+               procmsg_msginfo_free(msginfo);
+               msgcache_remove_msg(item->cache, num);
+       }
        item->total--;
-       procmsg_msginfo_free(msginfo);
-       msgcache_remove_msg(item->cache, num);
+       folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
 
        return ret;
 }
 
 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
 {
+       Folder *folder;
        gint ret = 0;
 
        g_return_val_if_fail(item != NULL, -1);
+       folder = item->folder;
+       g_return_val_if_fail(folder != NULL, -1);
 
        if (!item->cache) folder_item_read_cache(item);
 
+       if (folder->remove_msgs) {
+               ret = folder->remove_msgs(folder, item, msglist);
+               if (ret == 0)
+                       folder_item_scan(item);
+               return ret;
+       }
+
        while (msglist != NULL) {
                MsgInfo *msginfo = (MsgInfo *)msglist->data;
 
@@ -1611,6 +2152,15 @@ gint folder_item_remove_all_msg(FolderItem *item)
        if (result == 0) {
                if (folder->finished_remove)
                        folder->finished_remove(folder, item);
+
+               folder_item_free_cache(item);
+               item->cache = msgcache_new();
+
+               item->new = 0;
+               item->unread = 0;
+               item->unreadmarked = 0;
+               item->total = 0;
+               folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
        }
 
        return result;
@@ -1676,10 +2226,12 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
        const gchar *path = NULL;
        PrefsAccount *account = NULL;
        gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE, 
-                threaded = TRUE, ret_rcpt = FALSE, hidereadmsgs = FALSE;
+                threaded = TRUE, apply_sub = FALSE;
+       gboolean ret_rcpt = FALSE, hidereadmsgs = FALSE,
+                thread_collapsed = FALSE; /* CLAWS */
        FolderSortKey sort_key = SORT_BY_NONE;
        FolderSortType sort_type = SORT_ASCENDING;
-       gint new = 0, unread = 0, total = 0;
+       gint new = 0, unread = 0, total = 0, unreadmarked = 0;
        time_t mtime = 0;
 
        g_return_val_if_fail(node->data != NULL, FALSE);
@@ -1713,16 +2265,14 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
                        name = attr->value;
                else if (!strcmp(attr->name, "path"))
                        path = attr->value;
-               else if (!strcmp(attr->name, "account_id")) {
-                       account = account_find_from_id(atoi(attr->value));
-                       if (!account) g_warning("account_id: %s not found\n",
-                                               attr->value);
-               } else if (!strcmp(attr->name, "mtime"))
+               else if (!strcmp(attr->name, "mtime"))
                        mtime = strtoul(attr->value, NULL, 10);
                else if (!strcmp(attr->name, "new"))
                        new = atoi(attr->value);
                else if (!strcmp(attr->name, "unread"))
                        unread = atoi(attr->value);
+               else if (!strcmp(attr->name, "unreadmarked"))
+                       unreadmarked = atoi(attr->value);
                else if (!strcmp(attr->name, "total"))
                        total = atoi(attr->value);
                else if (!strcmp(attr->name, "no_sub"))
@@ -1731,6 +2281,8 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
                        no_select = *attr->value == '1' ? TRUE : FALSE;
                else if (!strcmp(attr->name, "collapsed"))
                        collapsed = *attr->value == '1' ? TRUE : FALSE;
+               else if (!strcmp(attr->name, "thread_collapsed"))
+                       thread_collapsed =  *attr->value == '1' ? TRUE : FALSE;
                else if (!strcmp(attr->name, "threaded"))
                        threaded =  *attr->value == '1' ? TRUE : FALSE;
                else if (!strcmp(attr->name, "hidereadmsgs"))
@@ -1757,9 +2309,11 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
                        else if (!strcmp(attr->value, "mark"))
                                sort_key = SORT_BY_MARK;
                        else if (!strcmp(attr->value, "unread"))
-                               sort_key = SORT_BY_UNREAD;
+                               sort_key = SORT_BY_STATUS;
                        else if (!strcmp(attr->value, "mime"))
                                sort_key = SORT_BY_MIME;
+                       else if (!strcmp(attr->value, "to"))
+                               sort_key = SORT_BY_TO;
                        else if (!strcmp(attr->value, "locked"))
                                sort_key = SORT_BY_LOCKED;
                } else if (!strcmp(attr->name, "sort_type")) {
@@ -1767,19 +2321,25 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
                                sort_type = SORT_ASCENDING;
                        else
                                sort_type = SORT_DESCENDING;
-               }
+               } else if (!strcmp(attr->name, "account_id")) {
+                       account = account_find_from_id(atoi(attr->value));
+                       if (!account) g_warning("account_id: %s not found\n",
+                                               attr->value);
+               } else if (!strcmp(attr->name, "apply_sub"))
+                       apply_sub = *attr->value == '1' ? TRUE : FALSE;
        }
 
-       item = folder_item_new(name, path);
+       item = folder_item_new(folder, name, path);
        item->stype = stype;
-       item->account = account;
        item->mtime = mtime;
        item->new = new;
        item->unread = unread;
+       item->unreadmarked = unreadmarked;
        item->total = total;
        item->no_sub = no_sub;
        item->no_select = no_select;
        item->collapsed = collapsed;
+       item->thread_collapsed = thread_collapsed;
        item->threaded  = threaded;
        item->hide_read_msgs  = hidereadmsgs;
        item->ret_rcpt  = ret_rcpt;
@@ -1795,7 +2355,8 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
        case F_TRASH:  folder->trash  = item; break;
        default:       break;
        }
-
+       item->account = account;
+       item->apply_sub = apply_sub;
        prefs_folder_item_read_config(item);
 
        node->data = item;
@@ -1813,7 +2374,8 @@ static gboolean folder_read_folder_func(GNode *node, gpointer data)
        const gchar *name = NULL;
        const gchar *path = NULL;
        PrefsAccount *account = NULL;
-       gboolean collapsed = FALSE, threaded = TRUE, ret_rcpt = FALSE;
+       gboolean collapsed = FALSE, threaded = TRUE, apply_sub = FALSE;
+       gboolean ret_rcpt = FALSE, thread_collapsed = FALSE; /* CLAWS */
 
        if (g_node_depth(node) != 2) return FALSE;
        g_return_val_if_fail(node->data != NULL, FALSE);
@@ -1844,14 +2406,18 @@ static gboolean folder_read_folder_func(GNode *node, gpointer data)
                        name = attr->value;
                else if (!strcmp(attr->name, "path"))
                        path = attr->value;
+               else if (!strcmp(attr->name, "collapsed"))
+                       collapsed = *attr->value == '1' ? TRUE : FALSE;
+               else if (!strcmp(attr->name, "thread_collapsed"))
+                       thread_collapsed = *attr->value == '1' ? TRUE : FALSE;
+               else if (!strcmp(attr->name, "threaded"))
+                       threaded = *attr->value == '1' ? TRUE : FALSE;
                else if (!strcmp(attr->name, "account_id")) {
                        account = account_find_from_id(atoi(attr->value));
                        if (!account) g_warning("account_id: %s not found\n",
                                                attr->value);
-               } else if (!strcmp(attr->name, "collapsed"))
-                       collapsed = *attr->value == '1' ? TRUE : FALSE;
-               else if (!strcmp(attr->name, "threaded"))
-                       threaded = *attr->value == '1' ? TRUE : FALSE;
+               } else if (!strcmp(attr->name, "apply_sub"))
+                       apply_sub = *attr->value == '1' ? TRUE : FALSE;
                else if (!strcmp(attr->name, "reqretrcpt"))
                        ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
        }
@@ -1866,7 +2432,10 @@ static gboolean folder_read_folder_func(GNode *node, gpointer data)
        folder->node = node;
        folder_add(folder);
        FOLDER_ITEM(node->data)->collapsed = collapsed;
+       FOLDER_ITEM(node->data)->thread_collapsed = thread_collapsed;
        FOLDER_ITEM(node->data)->threaded  = threaded;
+       FOLDER_ITEM(node->data)->account   = account;
+       FOLDER_ITEM(node->data)->apply_sub = apply_sub;
        FOLDER_ITEM(node->data)->ret_rcpt  = ret_rcpt;
 
        g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
@@ -1886,10 +2455,17 @@ static gchar *folder_get_list_path(void)
        return filename;
 }
 
+#define PUT_ESCAPE_STR(fp, attr, str)                  \
+{                                                      \
+       fputs(" " attr "=\"", fp);                      \
+       xml_file_put_escape_str(fp, str);               \
+       fputs("\"", fp);                                \
+}
+
 static void folder_write_list_recursive(GNode *node, gpointer data)
 {
        FILE *fp = (FILE *)data;
-       FolderItem *item = FOLDER_ITEM(node->data);
+       FolderItem *item;
        gint i, depth;
        static gchar *folder_type_str[] = {"mh", "mbox", "maildir", "imap",
                                           "news", "unknown"};
@@ -1897,8 +2473,12 @@ static void folder_write_list_recursive(GNode *node, gpointer data)
                                                 "draft", "queue", "trash"};
        static gchar *sort_key_str[] = {"none", "number", "size", "date",
                                        "from", "subject", "score", "label",
-                                       "mark", "unread", "mime", "locked" };
+                                       "mark", "unread", "mime", "to", 
+                                       "locked"};
+       g_return_if_fail(node != NULL);
+       g_return_if_fail(fp != NULL);
 
+       item = FOLDER_ITEM(node->data);
        g_return_if_fail(item != NULL);
 
        depth = g_node_depth(node);
@@ -1908,46 +2488,40 @@ static void folder_write_list_recursive(GNode *node, gpointer data)
                Folder *folder = item->folder;
 
                fprintf(fp, "<folder type=\"%s\"", folder_type_str[folder->type]);
-               if (folder->name) {
-                       fputs(" name=\"", fp);
-                       xml_file_put_escape_str(fp, folder->name);
-                       fputs("\"", fp);
-               }
-               if ((folder->type == F_MH) || (folder->type == F_MBOX)) {
-                       fputs(" path=\"", fp);
-                       xml_file_put_escape_str
-                               (fp, LOCAL_FOLDER(folder)->rootpath);
-                       fputs("\"", fp);
-               }
+               if (folder->name)
+                       PUT_ESCAPE_STR(fp, "name", folder->name);
+               if (folder->type == F_MH || folder->type == F_MBOX)
+                       PUT_ESCAPE_STR(fp, "path",
+                                      LOCAL_FOLDER(folder)->rootpath);
+               if (item->collapsed && node->children)
+                       fputs(" collapsed=\"1\"", fp);
                if (folder->account)
                        fprintf(fp, " account_id=\"%d\"",
                                folder->account->account_id);
-               if (item->collapsed && node->children)
-                       fputs(" collapsed=\"1\"", fp);
+               if (item->apply_sub)
+                       fputs(" apply_sub=\"1\"", fp);
                if (item->ret_rcpt) 
                        fputs(" reqretrcpt=\"1\"", fp);
        } else {
                fprintf(fp, "<folderitem type=\"%s\"",
                        folder_item_stype_str[item->stype]);
-               if (item->name) {
-                       fputs(" name=\"", fp);
-                       xml_file_put_escape_str(fp, item->name);
-                       fputs("\"", fp);
-               }
-               if (item->path) {
-                       fputs(" path=\"", fp);
-                       xml_file_put_escape_str(fp, item->path);
-                       fputs("\"", fp);
-               }
-               if (item->account)
-                       fprintf(fp, " account_id=\"%d\"",
-                               item->account->account_id);
+               if (item->name)
+                       PUT_ESCAPE_STR(fp, "name", item->name);
+               if (item->path)
+                       PUT_ESCAPE_STR(fp, "path", item->path);
+
                if (item->no_sub)
                        fputs(" no_sub=\"1\"", fp);
                if (item->no_select)
                        fputs(" no_select=\"1\"", fp);
                if (item->collapsed && node->children)
                        fputs(" collapsed=\"1\"", fp);
+               else
+                       fputs(" collapsed=\"0\"", fp);
+               if (item->thread_collapsed)
+                       fputs(" thread_collapsed=\"1\"", fp);
+               else
+                       fputs(" thread_collapsed=\"0\"", fp);
                if (item->threaded)
                        fputs(" threaded=\"1\"", fp);
                else
@@ -1969,8 +2543,14 @@ static void folder_write_list_recursive(GNode *node, gpointer data)
                }
 
                fprintf(fp,
-                       " mtime=\"%lu\" new=\"%d\" unread=\"%d\" total=\"%d\"",
-                       item->mtime, item->new, item->unread, item->total);
+                       " mtime=\"%lu\" new=\"%d\" unread=\"%d\" unreadmarked=\"%d\" total=\"%d\"",
+                       item->mtime, item->new, item->unread, item->unreadmarked, item->total);
+
+               if (item->account)
+                       fprintf(fp, " account_id=\"%d\"",
+                               item->account->account_id);
+               if (item->apply_sub)
+                       fputs(" apply_sub=\"1\"", fp);
        }
 
        if (node->children) {
@@ -1993,13 +2573,14 @@ static void folder_write_list_recursive(GNode *node, gpointer data)
                fputs(" />\n", fp);
 }
 
-static void folder_update_op_count_rec(GNode *node) {
+static void folder_update_op_count_rec(GNode *node)
+{
        FolderItem *fitem = FOLDER_ITEM(node->data);
 
        if (g_node_depth(node) > 0) {
                if (fitem->op_count > 0) {
                        fitem->op_count = 0;
-                       folderview_update_item(fitem, 0);
+                       folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
                }
                if (node->children) {
                        GNode *child;
@@ -2089,7 +2670,7 @@ static void folder_create_processing_folder(void)
        }
        else {
                debug_print("*TMP* already created\n");
-               processing_folder_item = folder_item_new(".processing", ".processing");
+               processing_folder_item = folder_item_new(processing_folder, ".processing", ".processing");
                g_assert(processing_folder_item);
                folder_item_append(processing_folder->node->data, processing_folder_item);
        }
@@ -2135,8 +2716,11 @@ const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *na
 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
 {
        const PersistPrefs *pp;
+       gchar *id = folder_item_get_identifier(item);
+
+       pp = folder_get_persist_prefs(pptable, id); 
+       g_free(id);
 
-       pp = folder_get_persist_prefs(pptable, item->path); 
        if (!pp) return;
 
        /* CLAWS: since not all folder properties have been migrated to 
@@ -2145,6 +2729,7 @@ void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
        prefs_folder_item_read_config(item); 
         
        item->collapsed = pp->collapsed;
+       item->thread_collapsed = pp->thread_collapsed;
        item->threaded  = pp->threaded;
        item->ret_rcpt  = pp->ret_rcpt;
        item->hide_read_msgs = pp->hide_read_msgs;
@@ -2157,24 +2742,26 @@ static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
        FolderItem *item = FOLDER_ITEM(node->data);
        PersistPrefs *pp;
        GNode *child, *cur;
+       gchar *id;
 
        g_return_if_fail(node != NULL);
        g_return_if_fail(item != NULL);
 
-       /* FIXME: item->path == NULL for top level folder, so this means that 
-        * properties of MH folder root will not be stored. Not quite important, 
-        * because the top level folder properties are not special anyway. */
+       /* NOTE: item->path == NULL means top level folder; not interesting
+        * to store preferences of that one.  */
        if (item->path) {
+               id = folder_item_get_identifier(item);
                pp = g_new0(PersistPrefs, 1);
                g_return_if_fail(pp != NULL);
                pp->collapsed = item->collapsed;
+               pp->thread_collapsed = item->thread_collapsed;
                pp->threaded  = item->threaded;
                pp->ret_rcpt  = item->ret_rcpt; 
                pp->hide_read_msgs = item->hide_read_msgs;
                pp->sort_key  = item->sort_key;
                pp->sort_type = item->sort_type;
-               g_hash_table_insert(pptable, item->path, pp);
-       }               
+               g_hash_table_insert(pptable, id, pp);
+       }
 
        if (node->children) {
                child = node->children;
@@ -2188,8 +2775,109 @@ static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
 
 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
 {
+       if (key) 
+               g_free(key);
        if (val) 
                g_free(val);
        return TRUE;    
 }
 
+void folder_item_apply_processing(FolderItem *item)
+{
+       GSList *processing_list;
+       GSList *mlist, *cur;
+       
+       g_return_if_fail(item != NULL);
+       
+       processing_list = item->prefs->processing;
+       if (processing_list == NULL)
+               return;
+
+       folder_item_update_freeze();
+
+       mlist = folder_item_get_msg_list(item);
+       for (cur = mlist ; cur != NULL ; cur = cur->next) {
+               MsgInfo * msginfo;
+
+               msginfo = (MsgInfo *) cur->data;
+               filter_message_by_msginfo(processing_list, msginfo);
+               procmsg_msginfo_free(msginfo);
+       }
+       g_slist_free(mlist);
+
+       folder_item_update_thaw();
+}
+
+/*
+ *  functions for handling FolderItem content changes
+ */
+static gint folder_item_update_freeze_cnt = 0;
+
+/**
+ * Notify the folder system about changes to a folder. If the
+ * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
+ * be invoked, otherwise the changes will be remebered until
+ * the folder system is thawed.
+ *
+ * \param item The FolderItem that was changed
+ * \param update_flags Type of changed that was made
+ */
+void folder_item_update(FolderItem *item, FolderItemUpdateFlags update_flags)
+{
+       if (folder_item_update_freeze_cnt == 0) {
+               FolderItemUpdateData source;
+       
+               source.item = item;
+               source.update_flags = update_flags;
+               hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
+       } else {
+               item->update_flags |= update_flags;
+       }
+}
+
+void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
+{
+       GNode *node = item->folder->node;       
+
+       node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
+       node = node->children;
+
+       folder_item_update(item, update_flags);
+       while (node != NULL) {
+               if (node && node->data) {
+                       FolderItem *next_item = (FolderItem*) node->data;
+
+                       folder_item_update(next_item, update_flags);
+               }
+               node = node->next;
+       }
+}
+
+void folder_item_update_freeze()
+{
+       folder_item_update_freeze_cnt++;
+}
+
+static void folder_item_update_func(FolderItem *item, gpointer data)
+{
+       FolderItemUpdateData source;
+    
+       if (item->update_flags) {
+               source.item = item;
+               source.update_flags = item->update_flags;
+               hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);                             
+               item->update_flags = 0;
+       }
+}
+
+void folder_item_update_thaw()
+{
+       if (folder_item_update_freeze_cnt > 0)
+               folder_item_update_freeze_cnt--;
+       if (folder_item_update_freeze_cnt == 0) {
+               /* Update all folders */
+               folder_func_to_all_folders(folder_item_update_func, NULL);
+       }
+}
+
+#undef PUT_ESCAPE_STR