make folder properties persistent so they are properly restored after rescanning...
[claws.git] / src / folder.c
index 85f07c291f9f9846e29b9cd2ceaac3dd827aaac1..35dbbf9ecd95c948f9a7d0e5cfeaac716f025d4f 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "intl.h"
 #include "folder.h"
+#include "folderview.h"
 #include "session.h"
 #include "imap.h"
 #include "news.h"
 #include "account.h"
 #include "prefs_account.h"
 #include "mbox_folder.h"
+#include "prefs_folder_item.h"
+
+#include <sys/stat.h>
+#include <sys/types.h>
 
 static GList *folder_list = NULL;
 
@@ -60,6 +65,12 @@ static gboolean folder_read_folder_func      (GNode          *node,
 static gchar *folder_get_list_path     (void);
 static void folder_write_list_recursive        (GNode          *node,
                                         gpointer        data);
+static void folder_update_op_count_rec (GNode          *node);
+
+
+static void folder_get_persist_prefs_recursive
+                                       (GNode *node, GHashTable *pptable);
+static gboolean persist_prefs_free     (gpointer key, gpointer val, gpointer data);
 
 
 Folder *folder_new(FolderType type, const gchar *name, const gchar *path)
@@ -154,6 +165,8 @@ FolderItem *folder_item_new(const gchar *name, const gchar *path)
        item->no_sub = FALSE;
        item->no_select = FALSE;
        item->collapsed = FALSE;
+       item->threaded  = TRUE;
+       item->ret_rcpt  = FALSE;
        item->parent = NULL;
        item->folder = NULL;
        item->data = NULL;
@@ -308,8 +321,11 @@ gint folder_read_list(void)
 {
        GNode *node;
        XMLNode *xmlnode;
+       gchar *path;
 
-       node = xml_parse_file(folder_get_list_path());
+       path = folder_get_list_path();
+       if (!is_file_exist(path)) return -1;
+       node = xml_parse_file(path);
        if (!node) return -1;
 
        xmlnode = node->data;
@@ -354,6 +370,54 @@ void folder_write_list(void)
                g_warning("failed to write folder list.\n");
 }
 
+struct TotalMsgCount
+{
+       guint new;
+       guint unread;
+       guint total;
+};
+
+static gboolean folder_count_total_msgs_func(GNode *node, gpointer data)
+{
+       FolderItem *item;
+       struct TotalMsgCount *count = (struct TotalMsgCount *)data;
+
+       g_return_val_if_fail(node->data != NULL, FALSE);
+
+       item = FOLDER_ITEM(node->data);
+       count->new += item->new;
+       count->unread += item->unread;
+       count->total += item->total;
+
+       return FALSE;
+}
+
+void folder_count_total_msgs(guint *new, guint *unread, guint *total)
+{
+       GList *list;
+       Folder *folder;
+       struct TotalMsgCount count;
+
+       count.new = count.unread = count.total = 0;
+
+       debug_print(_("Counting total number of messages...\n"));
+
+       for (list = folder_list; list != NULL; list = list->next) {
+               folder = FOLDER(list->data);
+               if (folder->node)
+                       g_node_traverse(folder->node, G_PRE_ORDER,
+                                       G_TRAVERSE_ALL, -1,
+                                       folder_count_total_msgs_func,
+                                       &count);
+       }
+
+       *new = count.new;
+       *unread = count.unread;
+       *total = count.total;
+
+       return;
+}
+
 Folder *folder_find_from_path(const gchar *path)
 {
        GList *list;
@@ -613,10 +677,12 @@ gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
        src_folder = msginfo->folder->folder;
 
        num = folder->copy_msg(folder, dest, msginfo);
-       if (num != -1)
+
+       if (num != -1) {
                src_folder->remove_msg(src_folder,
                                       msginfo->folder,
                                       msginfo->msgnum);
+       }                                      
        
        if (folder->finished_copy)
                folder->finished_copy(folder, dest);
@@ -644,6 +710,7 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
 
        num = folder->move_msgs_with_dest(folder, dest, msglist);
        if (num > 0) dest->last_num = num;
+       else dest->op_count = 0;
 
        return num;
 }
@@ -750,6 +817,7 @@ gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
 
        num = folder->copy_msgs_with_dest(folder, dest, msglist);
        if (num > 0) dest->last_num = num;
+       else dest->op_count = 0;
 
        return num;
 }
@@ -789,28 +857,35 @@ gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
 gint folder_item_remove_msg(FolderItem *item, gint num)
 {
        Folder *folder;
-       gint result;
+       gint ret;
 
        g_return_val_if_fail(item != NULL, -1);
 
        folder = item->folder;
+       if (item->last_num < 0) folder->scan(folder, item);
 
-       g_return_val_if_fail(folder->scan != NULL, -1);
-       g_return_val_if_fail(folder->remove_msg != NULL, -1);
+       ret = folder->remove_msg(folder, item, num);
+       if (ret == 0 && num == item->last_num)
+               folder->scan(folder, item);
 
-       if (folder->finished_remove)
-               folder->finished_remove(folder, item);
+       return ret;
+}
 
-       result = folder->remove_msg(folder, item, num);
+gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
+{
+       gint ret = 0;
 
-       if (item->last_num < 0) folder->scan(folder, item);
+       g_return_val_if_fail(item != NULL, -1);
 
-       if (result == 0){
-               if (folder->finished_remove)
-                       folder->finished_remove(folder, item);
+       while (msglist != NULL) {
+               MsgInfo *msginfo = (MsgInfo *)msglist->data;
+
+               ret = folder_item_remove_msg(item, msginfo->msgnum);
+               if (ret != 0) break;
+               msglist = msglist->next;
        }
 
-       return result;
+       return ret;
 }
 
 gint folder_item_remove_all_msg(FolderItem *item)
@@ -1040,7 +1115,8 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
        const gchar *name = NULL;
        const gchar *path = NULL;
        PrefsAccount *account = NULL;
-       gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE;
+       gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE, 
+                threaded = TRUE, ret_rcpt = FALSE, hidereadmsgs = FALSE;
        gint mtime = 0, new = 0, unread = 0, total = 0;
 
        g_return_val_if_fail(node->data != NULL, FALSE);
@@ -1092,6 +1168,12 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
                        no_select = *attr->value == '1' ? TRUE : FALSE;
                else if (!strcmp(attr->name, "collapsed"))
                        collapsed = *attr->value == '1' ? TRUE : FALSE;
+               else if (!strcmp(attr->name, "threaded"))
+                       threaded =  *attr->value == '1' ? TRUE : FALSE;
+               else if (!strcmp(attr->name, "hidereadmsgs"))
+                       hidereadmsgs =  *attr->value == '1' ? TRUE : FALSE;
+               else if (!strcmp(attr->name, "reqretrcpt"))
+                       ret_rcpt =  *attr->value == '1' ? TRUE : FALSE;
        }
 
        item = folder_item_new(name, path);
@@ -1104,6 +1186,9 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
        item->no_sub = no_sub;
        item->no_select = no_select;
        item->collapsed = collapsed;
+       item->threaded  = threaded;
+       item->hide_read_msgs  = hidereadmsgs;
+       item->ret_rcpt  = ret_rcpt;
        item->parent = FOLDER_ITEM(node->parent->data);
        item->folder = folder;
        switch (stype) {
@@ -1133,7 +1218,7 @@ static gboolean folder_read_folder_func(GNode *node, gpointer data)
        const gchar *name = NULL;
        const gchar *path = NULL;
        PrefsAccount *account = NULL;
-       gboolean collapsed = FALSE;
+       gboolean collapsed = FALSE, threaded = TRUE, ret_rcpt = FALSE;
 
        if (g_node_depth(node) != 2) return FALSE;
        g_return_val_if_fail(node->data != NULL, FALSE);
@@ -1170,6 +1255,10 @@ static gboolean folder_read_folder_func(GNode *node, gpointer data)
                                                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, "reqretrcpt"))
+                       ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
        }
 
        folder = folder_new(type, name, path);
@@ -1182,6 +1271,8 @@ static gboolean folder_read_folder_func(GNode *node, gpointer data)
        folder->node = node;
        folder_add(folder);
        FOLDER_ITEM(node->data)->collapsed = collapsed;
+       FOLDER_ITEM(node->data)->threaded  = threaded;
+       FOLDER_ITEM(node->data)->ret_rcpt  = ret_rcpt;
 
        g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
                        folder_build_tree, folder);
@@ -1235,6 +1326,8 @@ static void folder_write_list_recursive(GNode *node, gpointer data)
                                folder->account->account_id);
                if (item->collapsed && node->children)
                        fputs(" collapsed=\"1\"", fp);
+               if (item->ret_rcpt) 
+                       fputs(" reqretrcpt=\"1\"", fp);
        } else {
                fprintf(fp, "<folderitem type=\"%s\"",
                        folder_item_stype_str[item->stype]);
@@ -1257,8 +1350,18 @@ static void folder_write_list_recursive(GNode *node, gpointer data)
                        fputs(" no_select=\"1\"", fp);
                if (item->collapsed && node->children)
                        fputs(" collapsed=\"1\"", fp);
+               if (item->threaded)
+                       fputs(" threaded=\"1\"", fp);
+               else
+                       fputs(" threaded=\"0\"", fp);
+               if (item->hide_read_msgs)
+                       fputs(" hidereadmsgs=\"1\"", fp);
+               else
+                       fputs(" hidereadmsgs=\"0\"", fp);
+               if (item->ret_rcpt)
+                       fputs(" reqretrcpt=\"1\"", fp);
                fprintf(fp,
-                       " mtime=\"%ld\" new=\"%d\" unread=\"%d\" total=\"%d\"",
+                       " mtime=\"%lu\" new=\"%d\" unread=\"%d\" total=\"%d\"",
                        item->mtime, item->new, item->unread, item->total);
        }
 
@@ -1282,6 +1385,39 @@ static void folder_write_list_recursive(GNode *node, gpointer data)
                fputs(" />\n", fp);
 }
 
+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);
+               }
+               if (node->children) {
+                       GNode *child;
+
+                       child = node->children;
+                       while (child) {
+                               GNode *cur;
+
+                               cur = child;
+                               child = cur->next;
+                               folder_update_op_count_rec(cur);
+                       }
+               }
+       }
+}
+
+void folder_update_op_count() {
+       GList *cur;
+       Folder *folder;
+
+       for (cur = folder_list; cur != NULL; cur = cur->next) {
+               folder = cur->data;
+               folder_update_op_count_rec(folder->node);
+       }
+}
+
 typedef struct _type_str {
        gchar * str;
        gint type;
@@ -1352,18 +1488,14 @@ static gchar * folder_item_get_tree_identifier(FolderItem * item)
 
 gchar * folder_item_get_identifier(FolderItem * item)
 {
-       gchar * path;
        gchar * id;
        gchar * folder_str;
 
-       folder_str = folder_get_identifier(item->folder);
-
-       if (item->path == NULL) {
-               g_free(folder_str);
-               return NULL;
-       }
+       g_return_val_if_fail(item->path != NULL, NULL);
 
+       folder_str = folder_get_identifier(item->folder);
        id = g_strconcat(folder_str, "/", item->path, NULL);
+       g_free(folder_str);
 
        return id;
 }
@@ -1392,8 +1524,7 @@ FolderItem * folder_find_item_from_identifier(const gchar *identifier)
        gchar * name;
        gchar * path;
 
-       Xalloca(str, strlen(identifier) + 1, return NULL);
-       strcpy(str, identifier);
+       Xstrdup_a(str, identifier, return NULL);
 
        /* extract box type */
 
@@ -1431,3 +1562,142 @@ FolderItem * folder_find_item_from_identifier(const gchar *identifier)
                        folder_item_find_func, d);
        return d[1];
 }
+
+/* CLAWS: temporary local folder for filtering */
+
+static Folder *processing_folder;
+static FolderItem *processing_folder_item;
+
+static void folder_create_processing_folder(void)
+{
+#define PROCESSING_FOLDER ".processing"        
+       Folder     *tmpparent;
+       FolderItem *tmpfolder;
+       gchar      *tmpname;
+       struct stat s;
+
+       tmpparent = folder_get_default_folder();
+       g_assert(tmpparent);
+       debug_print("tmpparentroot %s\n", LOCAL_FOLDER(tmpparent)->rootpath);
+       if (LOCAL_FOLDER(tmpparent)->rootpath[0] == '/')
+               tmpname = g_strconcat(LOCAL_FOLDER(tmpparent)->rootpath,
+                                     G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
+                                     NULL);
+       else
+               tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
+                                     LOCAL_FOLDER(tmpparent)->rootpath,
+                                     G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
+                                     NULL);
+
+       processing_folder = folder_new(F_MH, "PROCESSING", LOCAL_FOLDER(tmpparent)->rootpath);
+       g_assert(processing_folder);
+
+       if (!is_dir_exist(tmpname)) {
+               debug_print("*TMP* creating %s\n", tmpname);
+               processing_folder_item = processing_folder->create_folder(processing_folder,
+                                                                         processing_folder->node->data,
+                                                                         PROCESSING_FOLDER);
+               g_assert(processing_folder_item);                                                                         
+       }
+       else {
+               debug_print("*TMP* already created\n");
+               processing_folder_item = folder_item_new(".processing", ".processing");
+               g_assert(processing_folder_item);
+               folder_item_append(processing_folder->node->data, processing_folder_item);
+       }
+       g_free(tmpname);
+}
+
+FolderItem *folder_get_default_processing(void)
+{
+       if (!processing_folder_item) {
+               folder_create_processing_folder();
+       }
+       return processing_folder_item;
+}
+
+/* folder_persist_prefs_new() - return hash table with persistent
+ * settings (and folder name as key). 
+ * (note that in claws other options are in the PREFS_FOLDER_ITEM_RC
+ * file, so those don't need to be included in PersistPref yet) 
+ */
+GHashTable *folder_persist_prefs_new(Folder *folder)
+{
+       GHashTable *pptable;
+
+       g_return_val_if_fail(folder, NULL);
+       pptable = g_hash_table_new(g_str_hash, g_str_equal);
+       folder_get_persist_prefs_recursive(folder->node, pptable);
+       return pptable;
+}
+
+void folder_persist_prefs_free(GHashTable *pptable)
+{
+       g_return_if_fail(pptable);
+       g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
+       g_hash_table_destroy(pptable);
+}
+
+const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
+{
+       if (pptable == NULL || name == NULL) return NULL;
+       return g_hash_table_lookup(pptable, name);
+}
+
+void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
+{
+       const PersistPrefs *pp;
+
+       pp = folder_get_persist_prefs(pptable, item->path); 
+       if (!pp) return;
+
+       /* CLAWS: since not all folder properties have been migrated to 
+        * folderlist.xml, we need to call the old stuff first before
+        * setting things that apply both to Main and Claws. */
+       prefs_folder_item_read_config(item); 
+        
+       item->collapsed = pp->collapsed;
+       item->threaded  = pp->threaded;
+       item->ret_rcpt  = pp->ret_rcpt;
+       item->hide_read_msgs = pp->hide_read_msgs;
+}
+
+static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
+{
+       FolderItem *item = FOLDER_ITEM(node->data);
+       PersistPrefs *pp;
+       GNode *child, *cur;
+
+       g_return_if_fail(node != NULL);
+       g_return_if_fail(item != NULL);
+
+       /* FIXME: item->path == NULL for top level folder, so this means that 
+        * properties of MH folder root will not be stored. Not quite important, 
+        * because the top level folder properties are not special anyway. */
+       if (item->path) {
+               pp = g_new0(PersistPrefs, 1);
+               g_return_if_fail(pp != NULL);
+               pp->collapsed = item->collapsed;
+               pp->threaded  = item->threaded;
+               pp->ret_rcpt  = item->ret_rcpt; 
+               pp->hide_read_msgs = item->hide_read_msgs;
+               g_hash_table_insert(pptable, item->path, pp);
+       }               
+
+       if (node->children) {
+               child = node->children;
+               while (child) {
+                       cur = child;
+                       child = cur->next;
+                       folder_get_persist_prefs_recursive(cur, pptable);
+               }
+       }       
+}
+
+static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
+{
+       if (val) 
+               g_free(val);
+       return TRUE;    
+}
+