enable IMAP folder dnd
[claws.git] / src / folder.c
index aee1c4c57461afb07df2a4c84972847f79d03334..49370dd62710d9cd232b35ae49b68717dbc448d4 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 "xml.h"
 #include "codeconv.h"
 #include "prefs.h"
-#include "prefs_common.h"
 #include "account.h"
-#include "prefs_account.h"
 #include "filtering.h"
 #include "scoring.h"
 #include "prefs_folder_item.h"
 #include "procheader.h"
+#include "statusbar.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 +76,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,13 +96,17 @@ 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);
@@ -112,19 +120,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(folder, name, NULL);
-       item->folder = folder;
-       folder->node = g_node_new(item);
-       folder->data = NULL;
 }
 
 void folder_local_folder_init(Folder *folder, const gchar *name,
@@ -188,17 +193,10 @@ FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path
 {
        FolderItem *item = NULL;
 
-       switch (folder->type) {
-       case F_IMAP:
-               item = imap_folder_item_new();
-               break;
-       case F_MH:
-       case F_NEWS:
-       case F_MBOX:
+       if (folder->item_new) {
+               item = folder->item_new(folder);
+       } else {
                item = g_new0(FolderItem, 1);
-               break;
-       default:
-               return NULL;
        }
 
        g_return_val_if_fail(item != NULL, NULL);
@@ -209,6 +207,7 @@ FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path
        item->mtime = 0;
        item->new = 0;
        item->unread = 0;
+       item->unreadmarked = 0;
        item->total = 0;
        item->last_num = -1;
        item->cache = NULL;
@@ -270,21 +269,18 @@ void folder_item_destroy(FolderItem *item)
 
        debug_print("Destroying folder item %s\n", item->path);
 
-       if (item->folder != NULL) {
-               switch (item->folder->type) {
-               case F_IMAP:
-                       imap_folder_item_destroy(item);
-                       break;
-               default:
-                       break;
-               }
-       }
-
        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)
@@ -450,6 +446,8 @@ void folder_scan_tree(Folder *folder)
        folder_persist_prefs_free(pptable);
 
        prefs_matcher_read_config();
+
+       folder_write_list();
 }
 
 FolderItem *folder_create_folder(FolderItem *parent, const gchar *name)
@@ -457,7 +455,8 @@ FolderItem *folder_create_folder(FolderItem *parent, const gchar *name)
        FolderItem *new_item;
 
        new_item = parent->folder->create_folder(parent->folder, parent, name);
-       new_item->cache = msgcache_new();
+       if (new_item)
+               new_item->cache = msgcache_new();
 
        return new_item;
 }
@@ -466,6 +465,7 @@ struct TotalMsgCount
 {
        guint new;
        guint unread;
+       guint unreadmarked;
        guint total;
 };
 
@@ -515,14 +515,15 @@ 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");
 
@@ -530,6 +531,7 @@ void folder_count_total_msgs(guint *new, guint *unread, guint *total)
 
        *new = count.new;
        *unread = count.unread;
+       *unreadmarked = count.unreadmarked;
        *total = count.total;
 }
 
@@ -943,13 +945,61 @@ static gint folder_sort_folder_list(gconstpointer a, gconstpointer 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_update_item(item, FALSE);
+
+}
+
 gint folder_item_scan(FolderItem *item)
 {
        Folder *folder;
-       GSList *folder_list, *cache_list, *folder_list_cur, *cache_list_cur, *new_list = NULL;
-       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 contentchange = FALSE;
+    
        g_return_val_if_fail(item != NULL, -1);
        if (item->path == NULL) return -1;
 
@@ -961,8 +1011,13 @@ gint folder_item_scan(FolderItem *item)
        debug_print("Scanning folder %s for cache changes.\n", item->path);
 
        /* Get list of messages for folder and 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)) {
+           folder->check_msgnum_validity(folder, item)) {
                if (!item->cache)
                        folder_item_read_cache(item);
                cache_list = msgcache_get_msg_list(item->cache);
@@ -972,7 +1027,6 @@ gint folder_item_scan(FolderItem *item)
                item->cache = msgcache_new();
                cache_list = NULL;
        }
-       folder_list = folder->get_num_list(item->folder, item);
 
        /* Sort both lists */
        cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
@@ -1044,6 +1098,8 @@ gint folder_item_scan(FolderItem *item)
                        else
                                folder_cur_num = G_MAXINT;
 
+                       contentchange = TRUE;
+
                        continue;
                }
 
@@ -1063,6 +1119,8 @@ gint folder_item_scan(FolderItem *item)
                        else
                                cache_cur_num = G_MAXINT;
 
+                       contentchange = TRUE;
+
                        continue;
                }
 
@@ -1079,12 +1137,14 @@ gint folder_item_scan(FolderItem *item)
 
                                msgcache_remove_msg(item->cache, msginfo->msgnum);
 
-                               if (NULL != (newmsginfo = folder->fetch_msginfo(folder, item, folder_cur_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++;
                                        procmsg_msginfo_free(newmsginfo);
                                }                                       
 
@@ -1094,6 +1154,8 @@ gint folder_item_scan(FolderItem *item)
                                        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);
@@ -1112,6 +1174,8 @@ gint folder_item_scan(FolderItem *item)
                        else
                                folder_cur_num = G_MAXINT;
 
+                       contentchange = TRUE;
+
                        continue;
                }
        }
@@ -1123,13 +1187,13 @@ gint folder_item_scan(FolderItem *item)
        g_slist_free(cache_list);
        g_slist_free(folder_list);
 
-       if (folder->fetch_msginfos) {
+       if (folder->get_msginfos) {
                GSList *elem;
                GSList *newmsg_list;
                MsgInfo *msginfo;
                
                if (new_list) {
-                       newmsg_list = folder->fetch_msginfos(folder, item, 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);
@@ -1137,12 +1201,14 @@ gint folder_item_scan(FolderItem *item)
                                        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->fetch_msginfo) {
+       } else if (folder->get_msginfo) {
                GSList *elem;
        
                for (elem = new_list; elem != NULL; elem = g_slist_next(elem)) {
@@ -1150,13 +1216,15 @@ gint folder_item_scan(FolderItem *item)
                        guint num;
 
                        num = GPOINTER_TO_INT(elem->data);
-                       msginfo = folder->fetch_msginfo(folder, item, num);
+                       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);
@@ -1167,10 +1235,10 @@ gint folder_item_scan(FolderItem *item)
        item->new = newcnt;
        item->unread = unreadcnt;
        item->total = totalcnt;
-       
+       item->unreadmarked = unreadmarkedcnt;
        g_slist_free(new_list);
 
-       folderview_update_item(item, FALSE);
+       folder_update_item(item, contentchange);
 
        return 0;
 }
@@ -1315,7 +1383,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;
@@ -1329,8 +1397,8 @@ MsgInfo *folder_item_fetch_msginfo(FolderItem *item, gint num)
        if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
                return msginfo;
        
-       g_return_val_if_fail(folder->fetch_msginfo, NULL);
-       if ((msginfo = folder->fetch_msginfo(folder, item, num)) != NULL) {
+       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;
        }
@@ -1338,7 +1406,7 @@ MsgInfo *folder_item_fetch_msginfo(FolderItem *item, gint num)
        return NULL;
 }
 
-MsgInfo *folder_item_fetch_msginfo_by_id(FolderItem *item, const gchar *msgid)
+MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
 {
        Folder *folder;
        MsgInfo *msginfo;
@@ -1400,14 +1468,17 @@ gint folder_item_add_msg(FolderItem *dest, const gchar *file,
        num = folder->add_msg(folder, dest, file, remove_source);
 
         if (num > 0) {
-               msginfo = folder->fetch_msginfo(folder, dest, num);
+               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++;
                        dest->total++;
+                       dest->need_update = TRUE;
 
                        msgcache_add_msg(dest->cache, msginfo);
 
@@ -1438,6 +1509,139 @@ 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;
+       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;
+       }
+       
+       statusbar_print_all(_("Moving %s to %s..."), src->name, new_item->path);
+
+       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)
+                       statusbar_print_all(_("Moving %s to %s (%d%%)..."), src->name, 
+                                       new_item->path,
+                                       100*cnt/g_slist_length(mlist));
+               msginfo = (MsgInfo *) cur->data;
+               folder_item_move_msg(new_item, msginfo);
+               if (cnt%500)
+                       statusbar_pop_all();
+       }
+       
+       /*copy prefs*/
+       prefs_folder_item_copy_prefs(src, new_item);
+       new_item->collapsed = src->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;
+               }
+       }
+       src->folder->remove_folder(src->folder, src);
+
+       return new_item;
+}
+
+gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item)
+{
+       FolderItem *tmp = dest->parent;
+       char * src_identifier, * dst_identifier, * new_identifier;
+       char * 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;
+       }
+
+       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) {
+               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 */
+       
+       debug_print("updating rules ....\n");
+       new_identifier = g_strconcat(dst_identifier, G_DIR_SEPARATOR_S, g_basename(phys_srcpath), NULL);
+       prefs_filtering_rename_path(src_identifier, new_identifier);
+
+       folder_write_list();
+
+       g_free(src_identifier);
+       g_free(dst_identifier);
+       g_free(new_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)
 {
@@ -1463,7 +1667,7 @@ gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
                MsgInfo *newmsginfo;
     
                /* Add new msginfo to dest folder */
-               if (NULL != (newmsginfo = folder->fetch_msginfo(folder, dest, num))) {
+               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  ||
@@ -1476,6 +1680,8 @@ gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
                                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;
 
@@ -1492,6 +1698,8 @@ gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
                                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;
                }
@@ -1570,7 +1778,7 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
                if (num != -1) {
                        MsgInfo *newmsginfo;
 
-                       newmsginfo = folder->fetch_msginfo(folder, dest, num);
+                       newmsginfo = folder->get_msginfo(folder, dest, num);
                        if (newmsginfo) {
                                newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
                                if (dest->stype == F_OUTBOX ||
@@ -1585,6 +1793,8 @@ gint folder_item_move_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++;
                                dest->total++;
                                dest->need_update = TRUE;
 
@@ -1617,6 +1827,8 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
                                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;
                }
@@ -1669,7 +1881,7 @@ gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
        if (num != -1) {
                MsgInfo *newmsginfo;
 
-               if (NULL != (newmsginfo = folder->fetch_msginfo(folder, dest, num))) {
+               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  ||
@@ -1683,6 +1895,8 @@ gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
                                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;
 
@@ -1757,7 +1971,7 @@ gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
                if (num != -1) {
                        MsgInfo *newmsginfo;
 
-                       newmsginfo = folder->fetch_msginfo(folder, dest, num);
+                       newmsginfo = folder->get_msginfo(folder, dest, num);
                        if (newmsginfo) {
                                newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
                                if (dest->stype == F_OUTBOX ||
@@ -1772,6 +1986,8 @@ 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++;
                                dest->total++;
                                dest->need_update = TRUE;
 
@@ -1807,6 +2023,8 @@ gint folder_item_remove_msg(FolderItem *item, gint num)
                        item->new--;
                if (MSG_IS_UNREAD(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);
        }
@@ -1818,12 +2036,22 @@ gint folder_item_remove_msg(FolderItem *item, gint num)
 
 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;
 
@@ -1858,6 +2086,7 @@ gint folder_item_remove_all_msg(FolderItem *item)
 
                item->new = 0;
                item->unread = 0;
+               item->unreadmarked = 0;
                item->total = 0;
                item->need_update = TRUE;
        }
@@ -1929,7 +2158,7 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
        gboolean ret_rcpt = FALSE, hidereadmsgs = 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);
@@ -1969,6 +2198,8 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
                        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"))
@@ -2026,6 +2257,7 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
        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;
@@ -2142,6 +2374,13 @@ 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;
@@ -2167,17 +2406,11 @@ 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)
@@ -2190,17 +2423,11 @@ static void folder_write_list_recursive(GNode *node, gpointer data)
        } 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->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)
@@ -2228,9 +2455,9 @@ 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);
@@ -2258,13 +2485,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_update_item(fitem, FALSE);
                }
                if (node->children) {
                        GNode *child;
@@ -2485,7 +2713,104 @@ void folder_item_apply_processing(FolderItem *item)
                procmsg_msginfo_free(msginfo);
        }
        
-       folderview_update_items_when_required(FALSE);
+       folder_update_items_when_required(FALSE);
 
        g_slist_free(mlist);
 }
+
+/*
+ *  Callback handling for FolderItem content changes
+ */
+GSList *folder_item_update_callbacks_list = NULL;
+gint   folder_item_update_callbacks_nextid = 0;
+
+struct FolderItemUpdateCallback
+{
+       gint                    id;
+       FolderItemUpdateFunc    func;
+       gpointer                data;
+};
+
+gint folder_item_update_callback_register(FolderItemUpdateFunc func, gpointer data)
+{
+       struct FolderItemUpdateCallback *callback;
+
+       g_return_val_if_fail(func != NULL, -1);
+
+       folder_item_update_callbacks_nextid++;
+
+       callback = g_new0(struct FolderItemUpdateCallback, 1);
+       callback->id = folder_item_update_callbacks_nextid;
+       callback->func = func;
+       callback->data = data;
+
+       folder_item_update_callbacks_list =
+               g_slist_append(folder_item_update_callbacks_list, callback);
+
+       return folder_item_update_callbacks_nextid;
+}
+
+void folder_item_update_callback_unregister(gint id)
+{
+       GSList *list, *next;
+
+       for (list = folder_item_update_callbacks_list; list != NULL; list = next) {
+               struct FolderItemUpdateCallback *callback;
+
+               next = list->next;
+
+               callback = list->data;
+               if (callback->id == id) {
+                       folder_item_update_callbacks_list =
+                               g_slist_remove(folder_item_update_callbacks_list, callback);
+                       g_free(callback);
+               }
+       }
+}
+
+static void folder_item_update_callback_execute(FolderItem *item, gboolean contentchange)
+{
+       GSList *list;
+
+       for (list = folder_item_update_callbacks_list; list != NULL; list = list->next) {
+               struct FolderItemUpdateCallback *callback;
+
+               callback = list->data;
+               callback->func(item, contentchange, callback->data);
+       }
+}
+
+void folder_update_item(FolderItem *item, gboolean contentchange)
+{
+       folder_item_update_callback_execute(item, contentchange);
+}
+
+static void folder_update_item_func(FolderItem *item, gpointer contentchange)
+{
+       if (item->need_update) {
+               folder_item_update_callback_execute(item, GPOINTER_TO_INT(contentchange));
+               item->need_update = FALSE;
+       }
+}
+
+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)
+{
+       GNode *node = item->folder->node;       
+       node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
+       node = node->children;
+       folder_item_update_callback_execute(item, update_summary);
+       while (node != NULL) {
+               if (node && node->data) {
+                       FolderItem *next_item = (FolderItem*) node->data;
+                       folder_item_update_callback_execute(next_item, update_summary);
+               }
+               node = node->next;
+       }
+}
+
+#undef PUT_ESCAPE_STR