* src/folder.c
[claws.git] / src / folder.c
index 001c21869e46b1fe07dbe56d4879588c32d4187d..056d8bbdd9f40289535191717b9f8b6fb9146561 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;
 
@@ -442,6 +445,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)
@@ -936,13 +941,58 @@ 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);
+       
+       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);
+       }
+       
+       folder_update_item(item, FALSE);
+
+       g_slist_free(mlist);
+}
+
 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 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;
 
@@ -1041,6 +1091,8 @@ gint folder_item_scan(FolderItem *item)
                        else
                                folder_cur_num = G_MAXINT;
 
+                       contentchange = TRUE;
+
                        continue;
                }
 
@@ -1060,6 +1112,8 @@ gint folder_item_scan(FolderItem *item)
                        else
                                cache_cur_num = G_MAXINT;
 
+                       contentchange = TRUE;
+
                        continue;
                }
 
@@ -1109,6 +1163,8 @@ gint folder_item_scan(FolderItem *item)
                        else
                                folder_cur_num = G_MAXINT;
 
+                       contentchange = TRUE;
+
                        continue;
                }
        }
@@ -1167,7 +1223,7 @@ gint folder_item_scan(FolderItem *item)
        
        g_slist_free(new_list);
 
-       folderview_update_item(item, FALSE);
+       folder_update_item(item, contentchange);
 
        return 0;
 }
@@ -1444,7 +1500,7 @@ FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest)
        FolderItem *new_item;
        FolderItem *next_item;
        GNode *srcnode;
-       
+       int cnt = 0;
        mlist = folder_item_get_msg_list(src);
 
        /* move messages */
@@ -1455,14 +1511,23 @@ 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;
 
        /* 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*/
@@ -1474,6 +1539,8 @@ FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest)
        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);
@@ -1492,7 +1559,7 @@ FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest)
 FolderItem *folder_item_move_to(FolderItem *src, FolderItem *dest)
 {
        FolderItem *tmp = dest->parent;
-       char * srcpath, * dstpath;
+       char * src_identifier, * dst_identifier, * new_identifier;
        char * phys_srcpath, * phys_dstpath;
        GNode *src_node;
        
@@ -1506,54 +1573,53 @@ FolderItem *folder_item_move_to(FolderItem *src, FolderItem *dest)
        
        tmp = src->parent;
        
-       srcpath = folder_item_get_identifier(src);
-       dstpath = folder_item_get_identifier(dest);
+       src_identifier = folder_item_get_identifier(src);
+       dst_identifier = folder_item_get_identifier(dest);
        
-       if(dstpath == NULL && dest->folder && dest->parent == NULL) {
+       if(dst_identifier == NULL && dest->folder && dest->parent == NULL) {
                /* dest can be a root folder */
-               dstpath = folder_get_identifier(dest->folder);
+               dst_identifier = folder_get_identifier(dest->folder);
        }
-       if (srcpath == NULL || dstpath == NULL) {
+       if (src_identifier == NULL || dst_identifier == 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);
+       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."));
-               g_free(srcpath);
-               g_free(dstpath);
+               g_free(src_identifier);
+               g_free(dst_identifier);
                g_free(phys_srcpath);
                g_free(phys_dstpath);
                return NULL;
        }
        debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
        if ((tmp = folder_item_move_recursive(src, dest)) == NULL) {
-               alertpanel_error(_("Move failed !"));
+               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);
        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: is null !\n");
+               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(srcpath);
-       g_free(dstpath);
+       g_free(src_identifier);
+       g_free(dst_identifier);
+       g_free(new_identifier);
        g_free(phys_srcpath);
        g_free(phys_dstpath);
 
@@ -2388,13 +2454,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;
@@ -2615,7 +2682,101 @@ 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);
 }
+
+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 data)
+{
+       gboolean contentchange = GPOINTER_TO_INT(data);
+       
+       if (item->need_update) {
+               folder_item_update_callback_execute(item, 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;
+       }
+}