0.8.8claws18
[claws.git] / src / folder.c
index 2a61cc49695906838a7beb64ed0eb149dba9d2b2..8a18558ee32a564806e863b6152e6f85362d36fc 100644 (file)
 #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"
 #include "prefs_folder_item.h"
 #include "procheader.h"
-#include "statusbar.h"
+#include "hooks.h"
+#include "log.h"
 
 /* Dependecies to be removed ?! */
 #include "prefs_common.h"
@@ -123,8 +124,8 @@ static void folder_init(Folder *folder, const gchar *name)
        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;
@@ -207,6 +208,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;
@@ -464,6 +466,7 @@ struct TotalMsgCount
 {
        guint new;
        guint unread;
+       guint unreadmarked;
        guint total;
 };
 
@@ -513,14 +516,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");
 
@@ -528,6 +532,7 @@ void folder_count_total_msgs(guint *new, guint *unread, guint *total)
 
        *new = count.new;
        *unread = count.unread;
+       *unreadmarked = count.unreadmarked;
        *total = count.total;
 }
 
@@ -968,19 +973,21 @@ void folder_item_close(FolderItem *item)
        GSList *mlist, *cur;
        
        g_return_if_fail(item != NULL);
-       
-       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);
+       if (item->new) {
+               mlist = folder_item_get_msg_list(item);
+               for (cur = mlist ; cur != NULL ; cur = cur->next) {
+                       MsgInfo * msginfo;
 
-       folder_item_write_cache(olditem);
+                       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);
 
@@ -990,7 +997,7 @@ gint folder_item_scan(FolderItem *item)
 {
        Folder *folder;
        GSList *folder_list = NULL, *cache_list = NULL, *folder_list_cur, *cache_list_cur, *new_list = NULL;
-       guint newcnt = 0, unreadcnt = 0, totalcnt = 0;
+       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;
     
@@ -1131,12 +1138,16 @@ 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++;
+                                       if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
+                                               procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
                                        procmsg_msginfo_free(newmsginfo);
                                }                                       
 
@@ -1146,6 +1157,10 @@ 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++;
+                               if (procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD))
+                                       procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
                        }
                        totalcnt++;
                        procmsg_msginfo_free(msginfo);
@@ -1177,13 +1192,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);
@@ -1191,12 +1206,16 @@ 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++;
+                               if (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(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)) {
@@ -1204,13 +1223,17 @@ 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++;
+                               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);
@@ -1221,7 +1244,7 @@ gint folder_item_scan(FolderItem *item)
        item->new = newcnt;
        item->unread = unreadcnt;
        item->total = totalcnt;
-       
+       item->unreadmarked = unreadmarkedcnt;
        g_slist_free(new_list);
 
        folder_update_item(item, contentchange);
@@ -1369,7 +1392,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;
@@ -1383,8 +1406,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;
        }
@@ -1392,7 +1415,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;
@@ -1454,13 +1477,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++;
+                       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;
 
@@ -1502,6 +1529,8 @@ FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest)
        FolderItem *next_item;
        GNode *srcnode;
        int cnt = 0;
+       gchar *old_id, *new_id;
+
        mlist = folder_item_get_msg_list(src);
 
        /* move messages */
@@ -1512,8 +1541,6 @@ FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest)
                return NULL;
        }
        
-       statusbar_print_all(_("Moving %s to %s..."), src->name, new_item->path);
-
        if (new_item->folder == NULL)
                new_item->folder = dest->folder;
 
@@ -1522,13 +1549,13 @@ FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest)
                MsgInfo * msginfo;
                cnt++;
                if (cnt%500)
-                       statusbar_print_all(_("Moving %s to %s (%d%%)..."), src->name, 
+                       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);
-               if (cnt%500)
-                       statusbar_pop_all();
+
+               procmsg_msginfo_free(msginfo);
        }
        
        /*copy prefs*/
@@ -1549,25 +1576,36 @@ FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest)
        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;
                }
-               srcnode = srcnode->next;
        }
+       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;
 }
 
-FolderItem *folder_item_move_to(FolderItem *src, FolderItem *dest)
+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;
+       gchar * src_identifier, * dst_identifier;
+       gchar * phys_srcpath, * phys_dstpath;
        GNode *src_node;
        
        while (tmp) {
                if (tmp == src) {
-                       alertpanel_error(_("Can't move a folder to one of its children."));
-                       return NULL;
+                       return F_MOVE_FAILED_DEST_IS_CHILD;
                }
                tmp = tmp->parent;
        }
@@ -1583,28 +1621,29 @@ FolderItem *folder_item_move_to(FolderItem *src, FolderItem *dest)
        }
        if (src_identifier == NULL || dst_identifier == NULL) {
                printf("Can't get identifiers\n");
-               return NULL;
+               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) {
-               alertpanel_error(_("Source and destination are the same."));
+       if (src->parent == dest || src == dest) {
                g_free(src_identifier);
                g_free(dst_identifier);
                g_free(phys_srcpath);
                g_free(phys_dstpath);
-               return NULL;
+               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) {
-               alertpanel_error(_("Move failed!"));
-               return NULL;
+               return F_MOVE_FAILED;
        }
        
        /* update rules */
-       src->folder->remove_folder(src->folder, src);
        src_node = g_node_find(src->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, src);
        if (src_node) 
                g_node_destroy(src_node);
@@ -1612,19 +1651,14 @@ FolderItem *folder_item_move_to(FolderItem *src, FolderItem *dest)
                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);
 
-       return tmp;
+       *new_item = tmp;
+
+       return F_MOVE_OK;
 }
 
 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
@@ -1651,7 +1685,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  ||
@@ -1664,6 +1698,10 @@ 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++;
+                       if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
+                               procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
                        dest->total++;
                        dest->need_update = TRUE;
 
@@ -1680,6 +1718,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;
                }
@@ -1758,7 +1798,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 ||
@@ -1773,6 +1813,10 @@ 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++;
+                               if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
+                                       procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
                                dest->total++;
                                dest->need_update = TRUE;
 
@@ -1805,6 +1849,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;
                }
@@ -1857,7 +1903,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  ||
@@ -1871,6 +1917,10 @@ 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++;
+                       if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
+                               procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
                        dest->total++;
                        dest->need_update = TRUE;
 
@@ -1945,7 +1995,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 ||
@@ -1960,6 +2010,10 @@ 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++;
                                dest->need_update = TRUE;
 
@@ -1995,6 +2049,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);
        }
@@ -2010,17 +2066,18 @@ gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
        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->scan(folder);
+                       folder_item_scan(item);
                return ret;
        }
 
-       if (!item->cache) folder_item_read_cache(item);
-
        while (msglist != NULL) {
                MsgInfo *msginfo = (MsgInfo *)msglist->data;
 
@@ -2055,6 +2112,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;
        }
@@ -2126,7 +2184,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);
@@ -2166,6 +2224,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"))
@@ -2203,6 +2263,8 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
                                sort_key = SORT_BY_UNREAD;
                        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")) {
@@ -2223,6 +2285,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;
@@ -2357,7 +2420,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);
 
@@ -2420,8 +2484,8 @@ 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\"",
@@ -2686,74 +2750,24 @@ void folder_item_apply_processing(FolderItem *item)
 /*
  *  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);
+       FolderItemUpdateData source;
+       
+       source.item = item;
+       source.content_change = contentchange;
+       hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
 }
 
 static void folder_update_item_func(FolderItem *item, gpointer contentchange)
 {
+       FolderItemUpdateData source;
+
        if (item->need_update) {
-               folder_item_update_callback_execute(item, GPOINTER_TO_INT(contentchange));
+               source.item = item;
+               source.content_change = GPOINTER_TO_INT(contentchange);
+               hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
+
                item->need_update = FALSE;
        }
 }
@@ -2766,13 +2780,21 @@ void folder_update_items_when_required(gboolean contentchange)
 void folder_update_item_recursive(FolderItem *item, gboolean update_summary)
 {
        GNode *node = item->folder->node;       
+       FolderItemUpdateData source;
+
        node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
        node = node->children;
-       folder_item_update_callback_execute(item, update_summary);
+
+       source.item = item;
+       source.content_change = update_summary; 
+       hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
        while (node != NULL) {
                if (node && node->data) {
                        FolderItem *next_item = (FolderItem*) node->data;
-                       folder_item_update_callback_execute(next_item, update_summary);
+
+                       source.item = next_item;
+                       source.content_change = update_summary; 
+                       hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
                }
                node = node->next;
        }