make folder properties persistent so they are properly restored after rescanning...
[claws.git] / src / folder.c
index 7afe51781d279d6ed65f391a6537d44c636f13d7..35dbbf9ecd95c948f9a7d0e5cfeaac716f025d4f 100644 (file)
 #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;
 
@@ -64,6 +68,11 @@ static void folder_write_list_recursive      (GNode          *node,
 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)
 {
        Folder *folder = NULL;
@@ -361,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;
@@ -814,6 +871,23 @@ gint folder_item_remove_msg(FolderItem *item, gint num)
        return ret;
 }
 
+gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
+{
+       gint ret = 0;
+
+       g_return_val_if_fail(item != NULL, -1);
+
+       while (msglist != NULL) {
+               MsgInfo *msginfo = (MsgInfo *)msglist->data;
+
+               ret = folder_item_remove_msg(item, msginfo->msgnum);
+               if (ret != 0) break;
+               msglist = msglist->next;
+       }
+
+       return ret;
+}
+
 gint folder_item_remove_all_msg(FolderItem *item)
 {
        Folder *folder;
@@ -1042,7 +1116,7 @@ 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;
+                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);
@@ -1096,6 +1170,8 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
                        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;
        }
@@ -1111,6 +1187,7 @@ static gboolean folder_build_tree(GNode *node, gpointer data)
        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;
@@ -1249,8 +1326,6 @@ 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->threaded)
-                       fputs(" threaded=\"1\"", fp);
                if (item->ret_rcpt) 
                        fputs(" reqretrcpt=\"1\"", fp);
        } else {
@@ -1277,10 +1352,16 @@ static void folder_write_list_recursive(GNode *node, gpointer data)
                        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);
        }
 
@@ -1407,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;
 }
@@ -1447,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 */
 
@@ -1486,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;    
+}
+