allow dropping a folder to a root folder
[claws.git] / src / folder.c
index 33d1498d8b7cada21df8f1e641750c0dbefb5751..fd31aa5db5ad748f8371bb08f0f79f542f27469d 100644 (file)
@@ -73,6 +73,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 +93,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 +117,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_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 +190,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);
@@ -206,7 +201,6 @@ FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path
        item->stype = F_NORMAL;
        item->name = g_strdup(name);
        item->path = g_strdup(path);
-       item->account = NULL;
        item->mtime = 0;
        item->new = 0;
        item->unread = 0;
@@ -221,6 +215,8 @@ FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path
        item->opened    = FALSE;
        item->parent = NULL;
        item->folder = NULL;
+       item->account = NULL;
+       item->apply_sub = FALSE;
        item->mark_queue = NULL;
        item->data = NULL;
 
@@ -269,21 +265,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)
@@ -324,7 +317,8 @@ void folder_tree_destroy(Folder *folder)
        prefs_filtering_clear();
 
        g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_tree_destroy_func, NULL);
-       g_node_destroy(folder->node);
+       if (folder->node)
+               g_node_destroy(folder->node);
 
        folder->inbox = NULL;
        folder->outbox = NULL;
@@ -428,6 +422,8 @@ gboolean folder_scan_tree_func(GNode *node, gpointer data)
        FolderItem *item = (FolderItem *)node->data;
        
        folder_item_restore_persist_prefs(item, pptable);
+
+       return FALSE;
 }
 
 void folder_scan_tree(Folder *folder)
@@ -448,6 +444,17 @@ void folder_scan_tree(Folder *folder)
        prefs_matcher_read_config();
 }
 
+FolderItem *folder_create_folder(FolderItem *parent, const gchar *name)
+{
+       FolderItem *new_item;
+
+       new_item = parent->folder->create_folder(parent->folder, parent, name);
+       if (new_item)
+               new_item->cache = msgcache_new();
+
+       return new_item;
+}
+
 struct TotalMsgCount
 {
        guint new;
@@ -781,6 +788,33 @@ void folder_set_missing_folders(void)
        }
 }
 
+static gboolean folder_unref_account_func(GNode *node, gpointer data)
+{
+       FolderItem *item = node->data;
+       PrefsAccount *account = data;
+
+       if (item->account == account)
+               item->account = NULL;
+
+       return FALSE;
+}
+
+void folder_unref_account_all(PrefsAccount *account)
+{
+       Folder *folder;
+       GList *list;
+
+       if (!account) return;
+
+       for (list = folder_list; list != NULL; list = list->next) {
+               folder = list->data;
+               if (folder->account == account)
+                       folder->account = NULL;
+               g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
+                               folder_unref_account_func, account);
+       }
+}
+
 #undef CREATE_FOLDER_IF_NOT_EXIST
 
 gchar *folder_get_path(Folder *folder)
@@ -789,33 +823,35 @@ gchar *folder_get_path(Folder *folder)
 
        g_return_val_if_fail(folder != NULL, NULL);
 
-       if (FOLDER_TYPE(folder) == F_MH)
-               path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
-       else if (FOLDER_TYPE(folder) == F_MBOX) {
-               path = mbox_get_virtual_path(folder);
-               if (path == NULL)
-                       return NULL;
-               path = g_strconcat(get_mbox_cache_dir(),
-                                         G_DIR_SEPARATOR_S, path, NULL);
-               return path;
-       }
-       else if (FOLDER_TYPE(folder) == F_IMAP) {
-               g_return_val_if_fail(folder->account != NULL, NULL);
-               path = g_strconcat(get_imap_cache_dir(),
-                                  G_DIR_SEPARATOR_S,
-                                  folder->account->recv_server,
-                                  G_DIR_SEPARATOR_S,
-                                  folder->account->userid,
-                                  NULL);
-       } else if (FOLDER_TYPE(folder) == F_NEWS) {
-               g_return_val_if_fail(folder->account != NULL, NULL);
-               path = g_strconcat(get_news_cache_dir(),
-                                  G_DIR_SEPARATOR_S,
-                                  folder->account->nntp_server,
-                                  NULL);
-       } else
-               path = NULL;
+       switch(FOLDER_TYPE(folder)) {
+
+               case F_MH:
+                       path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
+                       break;
+
+               case F_IMAP:
+                       g_return_val_if_fail(folder->account != NULL, NULL);
+                       path = g_strconcat(get_imap_cache_dir(),
+                                          G_DIR_SEPARATOR_S,
+                                          folder->account->recv_server,
+                                          G_DIR_SEPARATOR_S,
+                                          folder->account->userid,
+                                          NULL);
+                       break;
+
+               case F_NEWS:
+                       g_return_val_if_fail(folder->account != NULL, NULL);
+                       path = g_strconcat(get_news_cache_dir(),
+                                          G_DIR_SEPARATOR_S,
+                                          folder->account->nntp_server,
+                                          NULL);
+                       break;
 
+               default:
+                       path = NULL;
+                       break;
+       }
+       
        return path;
 }
 
@@ -826,26 +862,37 @@ gchar *folder_item_get_path(FolderItem *item)
 
        g_return_val_if_fail(item != NULL, NULL);
 
-       folder_path = folder_get_path(item->folder);
-       g_return_val_if_fail(folder_path != NULL, NULL);
+       if(FOLDER_TYPE(item->folder) != F_MBOX) {
+               folder_path = folder_get_path(item->folder);
+               g_return_val_if_fail(folder_path != NULL, NULL);
 
-       if (folder_path[0] == G_DIR_SEPARATOR) {
-               if (item->path)
-                       path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
-                                          item->path, NULL);
-               else
-                       path = g_strdup(folder_path);
+               if (folder_path[0] == G_DIR_SEPARATOR) {
+                       if (item->path)
+                               path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
+                                                  item->path, NULL);
+                       else
+                               path = g_strdup(folder_path);
+               } else {
+                       if (item->path)
+                               path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
+                                                  folder_path, G_DIR_SEPARATOR_S,
+                                                  item->path, NULL);
+                       else
+                               path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
+                                                  folder_path, NULL);
+               }
+
+               g_free(folder_path);
        } else {
-               if (item->path)
-                       path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
-                                          folder_path, G_DIR_SEPARATOR_S,
-                                          item->path, NULL);
-               else
-                       path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
-                                          folder_path, NULL);
-       }
+               gchar *itempath;
 
-       g_free(folder_path);
+               itempath = mbox_get_virtual_path(item);
+               if (itempath == NULL)
+                       return NULL;
+               path = g_strconcat(get_mbox_cache_dir(),
+                                         G_DIR_SEPARATOR_S, itempath, NULL);
+               g_free(itempath);
+       }
        return path;
 }
 
@@ -873,20 +920,29 @@ void folder_item_set_default_flags(FolderItem *dest, MsgFlags *flags)
        }
 }
 
-typedef enum {
-    IN_CACHE  = 1 << 0,
-    IN_FOLDER = 1 << 1,
-} FolderScanInfo;
+static gint folder_sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
+{
+       MsgInfo *msginfo_a = (MsgInfo *) a;
+       MsgInfo *msginfo_b = (MsgInfo *) b;
+
+       return (msginfo_a->msgnum - msginfo_b->msgnum);
+}
+
+static gint folder_sort_folder_list(gconstpointer a, gconstpointer b)
+{
+       guint gint_a = GPOINTER_TO_INT(a);
+       guint gint_b = GPOINTER_TO_INT(b);
+       
+       return (gint_a - gint_b);
+}
 
 gint folder_item_scan(FolderItem *item)
 {
        Folder *folder;
-       GSList *folder_list, *cache_list, *elem, *new_list = NULL;
-       gint i;
-       guint min = 0xffffffff, max = 0, cache_max = 0;
-       FolderScanInfo *folderscaninfo;
+       GSList *folder_list = NULL, *cache_list = NULL, *folder_list_cur, *cache_list_cur, *new_list = NULL;
        guint newcnt = 0, unreadcnt = 0, totalcnt = 0;
-       
+       guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
+
        g_return_val_if_fail(item != NULL, -1);
        if (item->path == NULL) return -1;
 
@@ -898,8 +954,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);
@@ -909,66 +970,56 @@ gint folder_item_scan(FolderItem *item)
                item->cache = msgcache_new();
                cache_list = NULL;
        }
-       folder_list = folder->get_num_list(item->folder, item);
-
-       /* Get min und max number in folder */
-       for (elem = cache_list; elem != NULL; elem = elem->next) {
-               MsgInfo *msginfo = (MsgInfo *)elem->data;
-
-               min = MIN(msginfo->msgnum, min);
-               max = MAX(msginfo->msgnum, max);
-       }
-       cache_max = max;
-       for (elem = folder_list; elem != NULL; elem = elem->next) {
-               guint num = GPOINTER_TO_INT(elem->data);
-
-               min = MIN(num, min);
-               max = MAX(num, max);
-       }
 
-       debug_print("Folder message number range from %d to %d\n", min, max);
+       /* Sort both lists */
+       cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
+       folder_list = g_slist_sort(folder_list, folder_sort_folder_list);
 
-       if (max == 0) {
-               for (elem = cache_list; elem != NULL; elem = elem->next) {
-                       MsgInfo *msginfo = (MsgInfo *)elem->data;
+       cache_list_cur = cache_list;
+       folder_list_cur = folder_list;
 
-                       procmsg_msginfo_free(msginfo);
-               }
-               g_slist_free(folder_list);
-               g_slist_free(cache_list);
-
-               return 0;
-       }
-
-       folderscaninfo = g_new0(FolderScanInfo, max - min + 1);
-
-       for (elem = folder_list; elem != NULL; elem = elem->next) {
-               guint num = GPOINTER_TO_INT(elem->data);
-
-               folderscaninfo[num - min] |= IN_FOLDER;
+       if (cache_list_cur != NULL) {
+               GSList *cache_list_last;
+       
+               cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
+               cache_list_last = g_slist_last(cache_list);
+               cache_max_num = ((MsgInfo *)cache_list_last->data)->msgnum;
+       } else {
+               cache_cur_num = G_MAXINT;
+               cache_max_num = 0;
        }
-       for (elem = cache_list; elem != NULL; elem = elem->next) {
-               MsgInfo *msginfo = (MsgInfo *)elem->data;
 
-               folderscaninfo[msginfo->msgnum - min] |= IN_CACHE;
-               procmsg_msginfo_free(msginfo);
+       if (folder_list_cur != NULL) {
+               GSList *folder_list_last;
+       
+               folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
+               folder_list_last = g_slist_last(folder_list);
+               folder_max_num = GPOINTER_TO_INT(folder_list_last->data);
+       } else {
+               folder_cur_num = G_MAXINT;
+               folder_max_num = 0;
        }
 
-       for (i = max - min; i >= 0; i--) {
-               guint num;
-
-               num = i + min;
-               /* Add message to cache if in folder and not in cache */
-               if ( (folderscaninfo[i] & IN_FOLDER) && 
-                  !(folderscaninfo[i] & IN_CACHE)) {
+       while ((cache_cur_num != G_MAXINT) || (folder_cur_num != G_MAXINT)) {
+               /*
+                *  Message only exists in the folder
+                *  Remember message for fetching
+                */
+               if (folder_cur_num < cache_cur_num) {
                        gboolean add = FALSE;
 
                        switch(folder->type) {
                                case F_NEWS:
-                                       if ((num > cache_max) && 
-                                          ((prefs_common.max_articles == 0) ||
-                                           (max < prefs_common.max_articles) ||
-                                           (num > (max - prefs_common.max_articles)))) {
+                                       if (folder_cur_num < cache_max_num)
+                                               break;
+                                       
+                                       if (prefs_common.max_articles == 0) {
+                                               add = TRUE;
+                                       }
+
+                                       if (folder_max_num <= prefs_common.max_articles) {
+                                               add = TRUE;
+                                       } else if (folder_cur_num > (folder_max_num - prefs_common.max_articles)) {
                                                add = TRUE;
                                        }
                                        break;
@@ -978,28 +1029,54 @@ gint folder_item_scan(FolderItem *item)
                        }
                        
                        if (add) {
-                               new_list = g_slist_prepend(new_list, GINT_TO_POINTER(num));
-                               debug_print("Remembered message %d for fetching\n", num);
+                               new_list = g_slist_prepend(new_list, GINT_TO_POINTER(folder_cur_num));
+                               debug_print("Remembered message %d for fetching\n", folder_cur_num);
                        }
+
+                       /* Move to next folder number */
+                       folder_list_cur = folder_list_cur->next;
+
+                       if (folder_list_cur != NULL)
+                               folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
+                       else
+                               folder_cur_num = G_MAXINT;
+
+                       continue;
                }
-               /* Remove message from cache if not in folder and in cache */
-               if (!(folderscaninfo[i] & IN_FOLDER) && 
-                   (folderscaninfo[i] & IN_CACHE)) {
-                       msgcache_remove_msg(item->cache, i + min);
-                       debug_print("Removed message %d from cache.\n", num);
+
+               /*
+                *  Message only exists in the cache
+                *  Remove the message from the cache
+                */
+               if (cache_cur_num < folder_cur_num) {
+                       msgcache_remove_msg(item->cache, cache_cur_num);
+                       debug_print("Removed message %d from cache.\n", cache_cur_num);
+
+                       /* Move to next cache number */
+                       cache_list_cur = cache_list_cur->next;
+
+                       if (cache_list_cur != NULL)
+                               cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
+                       else
+                               cache_cur_num = G_MAXINT;
+
+                       continue;
                }
-               /* Check if msginfo needs update if in cache and in folder */
-               if ((folderscaninfo[i] & IN_FOLDER) && 
-                  (folderscaninfo[i] & IN_CACHE)) {
+
+               /*
+                *  Message number exists in folder and cache!
+                *  Check if the message has been modified
+                */
+               if (cache_cur_num == folder_cur_num) {
                        MsgInfo *msginfo;
 
-                       msginfo = msgcache_get_msg(item->cache, num);
+                       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);
 
-                               if (NULL != (newmsginfo = folder->fetch_msginfo(folder, item, num))) {
+                               if (NULL != (newmsginfo = folder->fetch_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++;
@@ -1008,7 +1085,7 @@ gint folder_item_scan(FolderItem *item)
                                        procmsg_msginfo_free(newmsginfo);
                                }                                       
 
-                               debug_print("Updated msginfo for message %d.\n", num);
+                               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++;
@@ -1017,10 +1094,34 @@ gint folder_item_scan(FolderItem *item)
                        }
                        totalcnt++;
                        procmsg_msginfo_free(msginfo);
+
+                       /* Move to next folder and cache number */
+                       cache_list_cur = cache_list_cur->next;
+                       folder_list_cur = folder_list_cur->next;
+
+                       if (cache_list_cur != NULL)
+                               cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
+                       else
+                               cache_cur_num = G_MAXINT;
+
+                       if (folder_list_cur != NULL)
+                               folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
+                       else
+                               folder_cur_num = G_MAXINT;
+
+                       continue;
                }
        }
 
+       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->fetch_msginfos) {
+               GSList *elem;
                GSList *newmsg_list;
                MsgInfo *msginfo;
                
@@ -1037,9 +1138,10 @@ gint folder_item_scan(FolderItem *item)
                                procmsg_msginfo_free(msginfo);
                        }
                        g_slist_free(newmsg_list);
-                       folderview_update_item(item, FALSE);
                }
        } else if (folder->fetch_msginfo) {
+               GSList *elem;
+       
                for (elem = new_list; elem != NULL; elem = g_slist_next(elem)) {
                        MsgInfo *msginfo;
                        guint num;
@@ -1057,17 +1159,13 @@ gint folder_item_scan(FolderItem *item)
                                debug_print("Added newly found message %d to cache.\n", num);
                        }
                }
-               folderview_update_item(item, FALSE);
        }
 
        item->new = newcnt;
        item->unread = unreadcnt;
        item->total = totalcnt;
        
-       g_slist_free(folder_list);
-       g_slist_free(cache_list);
        g_slist_free(new_list);
-       g_free(folderscaninfo);
 
        folderview_update_item(item, FALSE);
 
@@ -1307,6 +1405,7 @@ gint folder_item_add_msg(FolderItem *dest, const gchar *file,
                        if (MSG_IS_UNREAD(msginfo->flags))
                                dest->unread++;
                        dest->total++;
+                       dest->need_update = TRUE;
 
                        msgcache_add_msg(dest->cache, msginfo);
 
@@ -1337,6 +1436,115 @@ gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
        return num;
 }
 */
+               
+gint folder_item_move_recursive (FolderItem *src, FolderItem *dest) 
+{
+       GSList *mlist;
+       GSList *cur;
+       FolderItem *new_item;
+       FolderItem *next_item;
+       GNode *srcnode;
+
+       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 -1;
+       }
+       
+       for (cur = mlist ; cur != NULL ; cur = cur->next) {
+               MsgInfo * msginfo;
+               msginfo = (MsgInfo *) cur->data;
+               folder_item_move_msg(new_item, msginfo);
+       }
+       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;
+                       if (folder_item_move_recursive(next_item, new_item) != 0)
+                               return -1;
+               }
+               srcnode = srcnode->next;
+       }
+       return 0;
+}
+
+FolderItem *folder_item_move_to(FolderItem *src, FolderItem *dest)
+{
+       FolderItem *tmp = dest->parent;
+       char * srcpath, * dstpath;
+       char * phys_srcpath, * phys_dstpath;
+
+       while (tmp) {
+               if (tmp == src) {
+                       alertpanel_error(_("Can't move a folder to one of its children."));
+                       return NULL;
+               }
+               tmp = tmp->parent;
+       }
+       
+       tmp = src->parent;
+       
+       srcpath = folder_item_get_identifier(src);
+       dstpath = folder_item_get_identifier(dest);
+       
+       if(dstpath == NULL && dest->folder && dest->parent == NULL) {
+               /* dest can be a root folder */
+               dstpath = folder_get_identifier(dest->folder);
+       }
+       if (srcpath == NULL || dstpath == NULL) {
+               printf("Can't get identifiers\n");
+               return NULL;
+       }
+
+       phys_srcpath = folder_item_get_path(src);
+       phys_dstpath = g_strconcat(folder_item_get_path(dest),G_DIR_SEPARATOR_S,g_basename(srcpath),NULL);
+
+       if (src->parent == dest) {
+               alertpanel_error(_("Source and destination are the same."));
+               g_free(srcpath);
+               g_free(dstpath);
+               g_free(phys_srcpath);
+               g_free(phys_dstpath);
+               return NULL;
+       }
+       debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
+       if (folder_item_move_recursive(src, dest) != 0) {
+               alertpanel_error(_("Move failed !"));
+               return NULL;
+       }
+       
+       /* update rules */
+       debug_print("updating rules ....\n");
+       prefs_filtering_rename_path(srcpath, g_strconcat(dstpath, 
+                                               G_DIR_SEPARATOR_S, 
+                                               g_basename(srcpath), 
+                                               NULL));
+
+       src->folder->remove_folder(src->folder, src);
+       /* not to much worry if remove fails, move has been done */
+       
+       folder_write_list();
+
+       /* rescan parents */
+       debug_print("rescanning foldertree ....\n");
+       folderview_rescan_tree(dest->folder);
+               
+       g_free(srcpath);
+       g_free(dstpath);
+       g_free(phys_srcpath);
+       g_free(phys_dstpath);
+       return folder_find_item_from_identifier(g_strconcat(dstpath, 
+                                                       G_DIR_SEPARATOR_S, 
+                                                       g_basename(srcpath), 
+                                                       NULL));
+}
 
 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
 {
@@ -1360,7 +1568,8 @@ gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
        
        if (num != -1) {
                MsgInfo *newmsginfo;
-
+    
+               /* Add new msginfo to dest folder */
                if (NULL != (newmsginfo = folder->fetch_msginfo(folder, dest, num))) {
                        newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
                        
@@ -1370,26 +1579,29 @@ gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
                                                     MSG_NEW|MSG_UNREAD|MSG_DELETED);
                        msgcache_add_msg(dest->cache, newmsginfo);
 
-                       /* CLAWS */
-                       if (src_folder->remove_msg)
-                               src_folder->remove_msg(src_folder, msginfo->folder,
-                                                      msginfo->msgnum);
-                       
-                       msgcache_remove_msg(msginfo->folder->cache, msginfo->msgnum);
-
-                       if (MSG_IS_NEW(msginfo->flags))
-                               msginfo->folder->new--;
                        if (MSG_IS_NEW(newmsginfo->flags))
                                dest->new++;
-                       if (MSG_IS_UNREAD(msginfo->flags))
-                               msginfo->folder->unread--;
                        if (MSG_IS_UNREAD(newmsginfo->flags))
                                dest->unread++;
-                       msginfo->folder->total--;
                        dest->total++;
+                       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);
+
+                       if (MSG_IS_NEW(msginfo->flags))
+                               msginfo->folder->new--;
+                       if (MSG_IS_UNREAD(msginfo->flags))
+                               msginfo->folder->unread--;
+                       msginfo->folder->total--;
+                       msginfo->folder->need_update = TRUE;
+               }
        }
        
        if (folder->finished_copy)
@@ -1481,6 +1693,7 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
                                if (MSG_IS_UNREAD(newmsginfo->flags))
                                        dest->unread++;
                                dest->total++;
+                               dest->need_update = TRUE;
 
                                procmsg_msginfo_free(newmsginfo);
                        }
@@ -1512,6 +1725,7 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
                        if (MSG_IS_UNREAD(msginfo->flags))
                                msginfo->folder->unread--;
                        msginfo->folder->total--;                       
+                       msginfo->folder->need_update = TRUE;
                }
 
                l2 = g_slist_next(l2);
@@ -1577,6 +1791,7 @@ gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
                        if (MSG_IS_UNREAD(newmsginfo->flags))
                                dest->unread++;
                        dest->total++;
+                       dest->need_update = TRUE;
 
                        procmsg_msginfo_free(newmsginfo);
                }                       
@@ -1635,7 +1850,7 @@ gint folder_item_copy_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
@@ -1665,6 +1880,7 @@ gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
                                if (MSG_IS_UNREAD(newmsginfo->flags))
                                        dest->unread++;
                                dest->total++;
+                               dest->need_update = TRUE;
 
                                procmsg_msginfo_free(newmsginfo);
                        }
@@ -1702,15 +1918,25 @@ gint folder_item_remove_msg(FolderItem *item, gint num)
                msgcache_remove_msg(item->cache, num);
        }
        item->total--;
+       item->need_update = TRUE;
 
        return ret;
 }
 
 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
 {
+       Folder *folder;
        gint ret = 0;
 
        g_return_val_if_fail(item != NULL, -1);
+       
+       folder = item->folder;
+       if (folder->remove_msgs) {
+               ret = folder->remove_msgs(folder, item, msglist);
+               if (ret == 0)
+                       folder->scan(folder);
+               return ret;
+       }
 
        if (!item->cache) folder_item_read_cache(item);
 
@@ -1749,6 +1975,7 @@ gint folder_item_remove_all_msg(FolderItem *item)
                item->new = 0;
                item->unread = 0;
                item->total = 0;
+               item->need_update = TRUE;
        }
 
        return result;
@@ -1814,7 +2041,8 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
        const gchar *path = NULL;
        PrefsAccount *account = NULL;
        gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE, 
-                threaded = TRUE, ret_rcpt = FALSE, hidereadmsgs = FALSE;
+                threaded = TRUE, apply_sub = FALSE;
+       gboolean ret_rcpt = FALSE, hidereadmsgs = FALSE; /* CLAWS */
        FolderSortKey sort_key = SORT_BY_NONE;
        FolderSortType sort_type = SORT_ASCENDING;
        gint new = 0, unread = 0, total = 0;
@@ -1851,11 +2079,7 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
                        name = attr->value;
                else if (!strcmp(attr->name, "path"))
                        path = attr->value;
-               else if (!strcmp(attr->name, "account_id")) {
-                       account = account_find_from_id(atoi(attr->value));
-                       if (!account) g_warning("account_id: %s not found\n",
-                                               attr->value);
-               } else if (!strcmp(attr->name, "mtime"))
+               else if (!strcmp(attr->name, "mtime"))
                        mtime = strtoul(attr->value, NULL, 10);
                else if (!strcmp(attr->name, "new"))
                        new = atoi(attr->value);
@@ -1905,12 +2129,16 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
                                sort_type = SORT_ASCENDING;
                        else
                                sort_type = SORT_DESCENDING;
-               }
+               } else if (!strcmp(attr->name, "account_id")) {
+                       account = account_find_from_id(atoi(attr->value));
+                       if (!account) g_warning("account_id: %s not found\n",
+                                               attr->value);
+               } else if (!strcmp(attr->name, "apply_sub"))
+                       apply_sub = *attr->value == '1' ? TRUE : FALSE;
        }
 
        item = folder_item_new(folder, name, path);
        item->stype = stype;
-       item->account = account;
        item->mtime = mtime;
        item->new = new;
        item->unread = unread;
@@ -1933,7 +2161,8 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
        case F_TRASH:  folder->trash  = item; break;
        default:       break;
        }
-
+       item->account = account;
+       item->apply_sub = apply_sub;
        prefs_folder_item_read_config(item);
 
        node->data = item;
@@ -1951,7 +2180,8 @@ static gboolean folder_read_folder_func(GNode *node, gpointer data)
        const gchar *name = NULL;
        const gchar *path = NULL;
        PrefsAccount *account = NULL;
-       gboolean collapsed = FALSE, threaded = TRUE, ret_rcpt = FALSE;
+       gboolean collapsed = FALSE, threaded = TRUE, apply_sub = FALSE;
+       gboolean ret_rcpt = FALSE; /* CLAWS */
 
        if (g_node_depth(node) != 2) return FALSE;
        g_return_val_if_fail(node->data != NULL, FALSE);
@@ -1982,14 +2212,16 @@ static gboolean folder_read_folder_func(GNode *node, gpointer data)
                        name = attr->value;
                else if (!strcmp(attr->name, "path"))
                        path = attr->value;
+               else if (!strcmp(attr->name, "collapsed"))
+                       collapsed = *attr->value == '1' ? TRUE : FALSE;
+               else if (!strcmp(attr->name, "threaded"))
+                       threaded = *attr->value == '1' ? TRUE : FALSE;
                else if (!strcmp(attr->name, "account_id")) {
                        account = account_find_from_id(atoi(attr->value));
                        if (!account) g_warning("account_id: %s not found\n",
                                                attr->value);
-               } else if (!strcmp(attr->name, "collapsed"))
-                       collapsed = *attr->value == '1' ? TRUE : FALSE;
-               else if (!strcmp(attr->name, "threaded"))
-                       threaded = *attr->value == '1' ? TRUE : FALSE;
+               } else if (!strcmp(attr->name, "apply_sub"))
+                       apply_sub = *attr->value == '1' ? TRUE : FALSE;
                else if (!strcmp(attr->name, "reqretrcpt"))
                        ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
        }
@@ -2005,6 +2237,8 @@ static gboolean folder_read_folder_func(GNode *node, gpointer data)
        folder_add(folder);
        FOLDER_ITEM(node->data)->collapsed = collapsed;
        FOLDER_ITEM(node->data)->threaded  = threaded;
+       FOLDER_ITEM(node->data)->account   = account;
+       FOLDER_ITEM(node->data)->apply_sub = apply_sub;
        FOLDER_ITEM(node->data)->ret_rcpt  = ret_rcpt;
 
        g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
@@ -2027,7 +2261,7 @@ static gchar *folder_get_list_path(void)
 static void folder_write_list_recursive(GNode *node, gpointer data)
 {
        FILE *fp = (FILE *)data;
-       FolderItem *item = FOLDER_ITEM(node->data);
+       FolderItem *item;
        gint i, depth;
        static gchar *folder_type_str[] = {"mh", "mbox", "maildir", "imap",
                                           "news", "unknown"};
@@ -2036,7 +2270,10 @@ static void folder_write_list_recursive(GNode *node, gpointer data)
        static gchar *sort_key_str[] = {"none", "number", "size", "date",
                                        "from", "subject", "score", "label",
                                        "mark", "unread", "mime", "locked" };
+       g_return_if_fail(node != NULL);
+       g_return_if_fail(fp != NULL);
 
+       item = FOLDER_ITEM(node->data);
        g_return_if_fail(item != NULL);
 
        depth = g_node_depth(node);
@@ -2057,11 +2294,13 @@ static void folder_write_list_recursive(GNode *node, gpointer data)
                                (fp, LOCAL_FOLDER(folder)->rootpath);
                        fputs("\"", fp);
                }
+               if (item->collapsed && node->children)
+                       fputs(" collapsed=\"1\"", fp);
                if (folder->account)
                        fprintf(fp, " account_id=\"%d\"",
                                folder->account->account_id);
-               if (item->collapsed && node->children)
-                       fputs(" collapsed=\"1\"", fp);
+               if (item->apply_sub)
+                       fputs(" apply_sub=\"1\"", fp);
                if (item->ret_rcpt) 
                        fputs(" reqretrcpt=\"1\"", fp);
        } else {
@@ -2077,9 +2316,7 @@ static void folder_write_list_recursive(GNode *node, gpointer data)
                        xml_file_put_escape_str(fp, item->path);
                        fputs("\"", fp);
                }
-               if (item->account)
-                       fprintf(fp, " account_id=\"%d\"",
-                               item->account->account_id);
+               
                if (item->no_sub)
                        fputs(" no_sub=\"1\"", fp);
                if (item->no_select)
@@ -2109,6 +2346,12 @@ 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);
+                       
+               if (item->account)
+                       fprintf(fp, " account_id=\"%d\"",
+                               item->account->account_id);
+               if (item->apply_sub)
+                       fputs(" apply_sub=\"1\"", fp);
        }
 
        if (node->children) {
@@ -2341,14 +2584,12 @@ void folder_item_apply_processing(FolderItem *item)
 {
        GSList *processing_list;
        GSList *mlist, *cur;
-       GHashTable *folder_table;
        
        g_return_if_fail(item != NULL);
        
        processing_list = item->prefs->processing;
        if (processing_list == NULL)
                return;
-       folder_table = g_hash_table_new(NULL, NULL);
 
        mlist = folder_item_get_msg_list(item);
        
@@ -2356,15 +2597,11 @@ void folder_item_apply_processing(FolderItem *item)
                MsgInfo * msginfo;
 
                msginfo = (MsgInfo *) cur->data;
-               filter_message_by_msginfo(processing_list, msginfo,
-                                         folder_table);
+               filter_message_by_msginfo(processing_list, msginfo);
                procmsg_msginfo_free(msginfo);
        }
        
-       /* folder_item_scan_foreach(summaryview->folder_table); */
-       folderview_update_item_foreach(folder_table, FALSE);
+       folderview_update_items_when_required(FALSE);
 
        g_slist_free(mlist);
-       
-       g_hash_table_destroy(folder_table);
 }