0.8.10claws81
[claws.git] / src / folder.c
index de8a42c2c3236dc659bf9d9642989c80e6ed9f76..3a6535cc95577f4a69e3b454ab9000b1114ae96a 100644 (file)
@@ -41,7 +41,7 @@
 #include "utils.h"
 #include "xml.h"
 #include "codeconv.h"
-#include "prefs.h"
+#include "prefs_gtk.h"
 #include "account.h"
 #include "filtering.h"
 #include "scoring.h"
@@ -215,6 +215,7 @@ FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path
        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;
@@ -417,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");
 }
 
@@ -989,17 +990,19 @@ void folder_item_close(FolderItem *item)
 
        folder_item_write_cache(item);
        
-       folder_update_item(item, FALSE);
-
+       folder_item_update(item, F_ITEM_UPDATE_MSGCNT);
 }
 
 gint folder_item_scan(FolderItem *item)
 {
        Folder *folder;
-       GSList *folder_list = NULL, *cache_list = NULL, *folder_list_cur, *cache_list_cur, *new_list = NULL;
+       GSList *folder_list = NULL, *cache_list = NULL;
+       GSList *folder_list_cur, *cache_list_cur, *new_list = NULL;
+       GSList *exists_list = NULL, *elem;
+       GSList *newmsg_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 contentchange = FALSE;
+       gboolean update_flags = 0;
     
        g_return_val_if_fail(item != NULL, -1);
        if (item->path == NULL) return -1;
@@ -1071,13 +1074,13 @@ gint folder_item_scan(FolderItem *item)
                                        if (folder_cur_num < cache_max_num)
                                                break;
                                        
-                                       if (prefs_common.max_articles == 0) {
+                                       if (folder->account->max_articles == 0) {
                                                add = TRUE;
                                        }
 
-                                       if (folder_max_num <= prefs_common.max_articles) {
+                                       if (folder_max_num <= folder->account->max_articles) {
                                                add = TRUE;
-                                       } else if (folder_cur_num > (folder_max_num - prefs_common.max_articles)) {
+                                       } else if (folder_cur_num > (folder_max_num - folder->account->max_articles)) {
                                                add = TRUE;
                                        }
                                        break;
@@ -1099,8 +1102,6 @@ gint folder_item_scan(FolderItem *item)
                        else
                                folder_cur_num = G_MAXINT;
 
-                       contentchange = TRUE;
-
                        continue;
                }
 
@@ -1120,7 +1121,7 @@ gint folder_item_scan(FolderItem *item)
                        else
                                cache_cur_num = G_MAXINT;
 
-                       contentchange = TRUE;
+                       update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
 
                        continue;
                }
@@ -1134,32 +1135,13 @@ gint folder_item_scan(FolderItem *item)
 
                        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);
+                               new_list = g_slist_prepend(new_list, GINT_TO_POINTER(msginfo->msgnum));
+                               procmsg_msginfo_free(msginfo);
 
-                               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++;
-                                       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))
-                                       newcnt++;
-                               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++;
-                       }
-                       totalcnt++;
-                       procmsg_msginfo_free(msginfo);
+                               debug_print("Remembering message %d to update...\n", folder_cur_num);
+                       } else
+                               exists_list = g_slist_prepend(exists_list, msginfo);
 
                        /* Move to next folder and cache number */
                        cache_list_cur = cache_list_cur->next;
@@ -1175,71 +1157,74 @@ gint folder_item_scan(FolderItem *item)
                        else
                                folder_cur_num = G_MAXINT;
 
-                       contentchange = TRUE;
-
                        continue;
                }
        }
-
-       for(cache_list_cur = cache_list; cache_list_cur != NULL; cache_list_cur = g_slist_next(cache_list_cur)) {
+       
+       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) {
+       if (new_list != NULL) {
+               if (folder->get_msginfos) {
                        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))
-                                       newcnt++;
-                               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++;
-                               totalcnt++;
-                               procmsg_msginfo_free(msginfo);
-                       }
-                       g_slist_free(newmsg_list);
-               }
-       } else if (folder->get_msginfo) {
-               GSList *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->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))
-                                   newcnt++;
-                               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++;
-                               totalcnt++;
-                               procmsg_msginfo_free(msginfo);
-                               debug_print("Added newly found message %d to cache.\n", num);
+                       for (elem = new_list; elem != NULL; elem = g_slist_next(elem)) {
+                               MsgInfo *msginfo;
+                               guint num;
+
+                               num = GPOINTER_TO_INT(elem->data);
+                               msginfo = folder->get_msginfo(folder, item, num);
+                               if (msginfo != NULL) {
+                                       newmsg_list = g_slist_prepend(newmsg_list, msginfo);
+                                       debug_print("Added newly found message %d to cache.\n", num);
+                               }
                        }
                }
+               g_slist_free(new_list);
        }
 
+       if (newmsg_list != NULL) {
+               GSList *elem;
+
+               for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem))
+                       msgcache_add_msg(item->cache, elem->data);
+
+               update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
+               
+               exists_list = g_slist_concat(exists_list, newmsg_list);
+       }
+
+       for (elem = exists_list; elem != NULL; elem = g_slist_next(elem)) {
+               MsgInfo *msginfo;
+
+               msginfo = elem->data;
+               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))
+                       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);
+       }
+       g_slist_free(exists_list);
+
        item->new = newcnt;
        item->unread = unreadcnt;
        item->total = totalcnt;
        item->unreadmarked = unreadmarkedcnt;
-       g_slist_free(new_list);
 
-       folder_update_item(item, contentchange);
+       update_flags |= F_ITEM_UPDATE_MSGCNT;
+
+       folder_item_update(item, update_flags);
 
        return 0;
 }
@@ -1449,6 +1434,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)
 {
@@ -1466,7 +1486,7 @@ 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->get_msginfo(folder, dest, num);
@@ -1478,16 +1498,26 @@ gint folder_item_add_msg(FolderItem *dest, const gchar *file,
                                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++;
-                       dest->need_update = TRUE;
 
                        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;
-        }
+        } 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;
 }
@@ -1551,6 +1581,7 @@ FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest)
        /*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;
@@ -1653,70 +1684,14 @@ gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_ite
 
 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
 {
-       Folder *folder;
-       gint num;
-       Folder *src_folder;
-
-       g_return_val_if_fail(dest != NULL, -1);
-       g_return_val_if_fail(msginfo != NULL, -1);
-
-       folder = dest->folder;
-
-       g_return_val_if_fail(folder->remove_msg != NULL, -1);
-       g_return_val_if_fail(folder->copy_msg != NULL, -1);
-
-       if (!dest->cache) folder_item_read_cache(dest);
-
-       src_folder = msginfo->folder->folder;
-
-       num = folder->copy_msg(folder, dest, msginfo);
-       
-       if (num != -1) {
-               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++;
-                       dest->total++;
-                       dest->need_update = TRUE;
-
-                       procmsg_msginfo_free(newmsginfo);
-               }
-
-               /* 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);
+       GSList *list = NULL;
+       gint ret;
 
-                       if (MSG_IS_NEW(msginfo->flags))
-                               msginfo->folder->new--;
-                       if (MSG_IS_UNREAD(msginfo->flags))
-                               msginfo->folder->unread--;
-                       if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
-                               msginfo->folder->unreadmarked--;
-                       msginfo->folder->total--;
-                       msginfo->folder->need_update = TRUE;
-               }
-       }
+       list = g_slist_append(list, msginfo);
+       ret = folder_item_move_msgs_with_dest(dest, list);
+       g_slist_free(list);
        
-       if (folder->finished_copy)
-               folder->finished_copy(folder, dest);
-
-       return num;
+       return ret;
 }
 
 /*
@@ -1745,7 +1720,8 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
        FolderItem *item;
        GSList *newmsgnums = NULL;
        GSList *l, *l2;
-       gint num;
+       gint num, lastnum = -1;
+       gboolean folderscan = FALSE;
 
        g_return_val_if_fail(dest != NULL, -1);
        g_return_val_if_fail(msglist != NULL, -1);
@@ -1772,7 +1748,7 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
 
        /* 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
@@ -1782,34 +1758,60 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
                MsgInfo *msginfo = (MsgInfo *) l->data;
 
                num = GPOINTER_TO_INT(l2->data);
+               l2 = g_slist_next(l2);
 
-               if (num != -1) {
+               if (num >= 0) {
                        MsgInfo *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  ||
-                                   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++;
-                               dest->total++;
-                               dest->need_update = TRUE;
-
-                               procmsg_msginfo_free(newmsginfo);
+                       if (num == 0) {
+                               gchar *file;
+
+                               if (!folderscan) {
+                                       folder_item_scan(dest);
+                                       folderscan = TRUE;
+                               }
+                               file = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
+                               num = folder_item_get_msg_num_by_file(dest, file);
+                               g_free(file);
+                       }
+
+                       if (num > lastnum)
+                               lastnum = num;
+
+                       if (num == 0)
+                               continue;
+
+                       if (!folderscan) {
+                               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  ||
+                                           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);
+                               }
+                       } else {
+                               newmsginfo = msgcache_get_msg(dest->cache, num);
+                               procmsg_msginfo_unset_flags(newmsginfo, ~0, ~0);
+                               procmsg_msginfo_set_flags(newmsginfo, msginfo->flags.perm_flags, 0);
                        }
                }
-               l2 = g_slist_next(l2);
        }
 
        /*
@@ -1822,8 +1824,9 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
                MsgInfo *msginfo = (MsgInfo *) l->data;
 
                num = GPOINTER_TO_INT(l2->data);
+               l2 = g_slist_next(l2);
                
-               if (num != -1) {
+               if (num >= 0) {
                        item->folder->remove_msg(item->folder,
                                                 msginfo->folder,
                                                 msginfo->msgnum);
@@ -1831,17 +1834,15 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
                                folder_item_read_cache(item);
                        msgcache_remove_msg(item->cache, msginfo->msgnum);
 
-                       if (MSG_IS_NEW(msginfo->flags))
+                       if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
                                msginfo->folder->new--;
-                       if (MSG_IS_UNREAD(msginfo->flags))
+                       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--;                       
-                       msginfo->folder->need_update = TRUE;
+                       folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
                }
-
-               l2 = g_slist_next(l2);
        }
 
 
@@ -1849,7 +1850,7 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
                folder->finished_copy(folder, dest);
 
        g_slist_free(newmsgnums);
-       return dest->last_num;
+       return lastnum;
 }
 
 /*
@@ -1873,49 +1874,14 @@ gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
 
 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
 {
-       Folder *folder;
-       gint num;
-
-       g_return_val_if_fail(dest != NULL, -1);
-       g_return_val_if_fail(msginfo != NULL, -1);
-
-       folder = dest->folder;
-
-       g_return_val_if_fail(folder->copy_msg != NULL, -1);
+       GSList *list = NULL;
+       gint ret;
 
-       if (!dest->cache) folder_item_read_cache(dest);
+       list = g_slist_append(list, msginfo);
+       ret = folder_item_copy_msgs_with_dest(dest, list);
+       g_slist_free(list);
        
-       num = folder->copy_msg(folder, dest, msginfo);
-       if (num != -1) {
-               MsgInfo *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++;
-                       dest->total++;
-                       dest->need_update = TRUE;
-
-                       procmsg_msginfo_free(newmsginfo);
-               }                       
-       }
-
-       if (folder->finished_copy)
-               folder->finished_copy(folder, dest);
-
-       return num;
+       return ret;
 }
 
 /*
@@ -1941,9 +1907,10 @@ gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
 {
        Folder *folder;
-       gint num;
+       gint num, lastnum = -1;
        GSList *newmsgnums = NULL;
        GSList *l, *l2;
+       gboolean folderscan = FALSE;
 
        g_return_val_if_fail(dest != NULL, -1);
        g_return_val_if_fail(msglist != NULL, -1);
@@ -1975,34 +1942,60 @@ gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
                MsgInfo *msginfo = (MsgInfo *) l->data;
 
                num = GPOINTER_TO_INT(l2->data);
+               l2 = g_slist_next(l2);
 
-               if (num != -1) {
+               if (num >= 0) {
                        MsgInfo *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  ||
-                                   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++;
-                               dest->total++;
-                               dest->need_update = TRUE;
-
-                               procmsg_msginfo_free(newmsginfo);
+                       if (num == 0) {
+                               gchar *file;
+
+                               if (!folderscan) {
+                                       folder_item_scan(dest);
+                                       folderscan = TRUE;
+                               }
+                               file = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
+                               num = folder_item_get_msg_num_by_file(dest, file);
+                               g_free(file);
+                       }
+       
+                       if (num > lastnum)
+                               lastnum = num;
+
+                       if (num == 0)
+                               continue;
+
+                       if (!folderscan) {
+                               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  ||
+                                           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);
+                               }
+                       } else {
+                               newmsginfo = msgcache_get_msg(dest->cache, num);
+                               procmsg_msginfo_unset_flags(newmsginfo, ~0, ~0);
+                               procmsg_msginfo_set_flags(newmsginfo, msginfo->flags.perm_flags, 0);
                        }
                }
-               l2 = g_slist_next(l2);
        }
        
        if (folder->finished_copy)
@@ -2027,9 +2020,9 @@ gint folder_item_remove_msg(FolderItem *item, gint num)
 
        msginfo = msgcache_get_msg(item->cache, num);
        if (msginfo != NULL) {
-               if (MSG_IS_NEW(msginfo->flags))
+               if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
                        item->new--;
-               if (MSG_IS_UNREAD(msginfo->flags))
+               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--;
@@ -2037,7 +2030,7 @@ gint folder_item_remove_msg(FolderItem *item, gint num)
                msgcache_remove_msg(item->cache, num);
        }
        item->total--;
-       item->need_update = TRUE;
+       folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
 
        return ret;
 }
@@ -2096,7 +2089,7 @@ gint folder_item_remove_all_msg(FolderItem *item)
                item->unread = 0;
                item->unreadmarked = 0;
                item->total = 0;
-               item->need_update = TRUE;
+               folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
        }
 
        return result;
@@ -2163,7 +2156,8 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
        PrefsAccount *account = NULL;
        gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE, 
                 threaded = TRUE, apply_sub = FALSE;
-       gboolean ret_rcpt = FALSE, hidereadmsgs = FALSE; /* CLAWS */
+       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, unreadmarked = 0;
@@ -2216,6 +2210,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"))
@@ -2242,9 +2238,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")) {
@@ -2270,6 +2268,7 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
        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;
@@ -2305,7 +2304,7 @@ static gboolean folder_read_folder_func(GNode *node, gpointer data)
        const gchar *path = NULL;
        PrefsAccount *account = NULL;
        gboolean collapsed = FALSE, threaded = TRUE, apply_sub = FALSE;
-       gboolean ret_rcpt = FALSE; /* CLAWS */
+       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);
@@ -2338,6 +2337,8 @@ static gboolean folder_read_folder_func(GNode *node, gpointer data)
                        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")) {
@@ -2360,6 +2361,7 @@ 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;
@@ -2400,7 +2402,8 @@ 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);
 
@@ -2442,6 +2445,12 @@ static void folder_write_list_recursive(GNode *node, gpointer data)
                        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
@@ -2500,7 +2509,7 @@ static void folder_update_op_count_rec(GNode *node)
        if (g_node_depth(node) > 0) {
                if (fitem->op_count > 0) {
                        fitem->op_count = 0;
-                       folder_update_item(fitem, FALSE);
+                       folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
                }
                if (node->children) {
                        GNode *child;
@@ -2649,6 +2658,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;
@@ -2673,6 +2683,7 @@ static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
                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;
@@ -2711,8 +2722,9 @@ void folder_item_apply_processing(FolderItem *item)
        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;
 
@@ -2720,63 +2732,81 @@ void folder_item_apply_processing(FolderItem *item)
                filter_message_by_msginfo(processing_list, msginfo);
                procmsg_msginfo_free(msginfo);
        }
-       
-       folder_update_items_when_required(FALSE);
-
        g_slist_free(mlist);
+
+       folder_item_update_thaw();
 }
 
 /*
- *  Callback handling for FolderItem content changes
+ *  functions for handling FolderItem content changes
  */
-void folder_update_item(FolderItem *item, gboolean contentchange)
-{
-       FolderItemUpdateData source;
-       
-       source.item = item;
-       source.content_change = contentchange;
-       hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
-}
+static gint folder_item_update_freeze_cnt = 0;
 
-static void folder_update_item_func(FolderItem *item, gpointer contentchange)
+/**
+ * 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)
 {
-       FolderItemUpdateData source;
-
-       if (item->need_update) {
+       if (folder_item_update_freeze_cnt == 0) {
+               FolderItemUpdateData source;
+       
                source.item = item;
-               source.content_change = GPOINTER_TO_INT(contentchange);
-               hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
-
-               item->need_update = FALSE;
+               source.update_flags = update_flags;
+               hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
+       } else {
+               item->update_flags |= update_flags;
        }
 }
 
-void folder_update_items_when_required(gboolean contentchange)
-{
-       folder_func_to_all_folders(folder_update_item_func, GINT_TO_POINTER(contentchange));
-}
-
-void folder_update_item_recursive(FolderItem *item, gboolean update_summary)
+void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
 {
        GNode *node = item->folder->node;       
-       FolderItemUpdateData source;
 
        node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
        node = node->children;
 
-       source.item = item;
-       source.content_change = update_summary; 
-       hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
+       folder_item_update(item, update_flags);
        while (node != NULL) {
                if (node && node->data) {
                        FolderItem *next_item = (FolderItem*) node->data;
 
-                       source.item = next_item;
-                       source.content_change = update_summary; 
-                       hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
+                       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