* sync with main changes of 2002-10-04
[claws.git] / src / folder.c
index b91fa29f2befbc07f4f5664eb49dd8117c437007..84918a89d5060433a570be3a9e164140839e40e5 100644 (file)
@@ -206,7 +206,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 +220,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;
 
@@ -429,6 +430,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)
@@ -449,6 +452,16 @@ 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);
+       new_item->cache = msgcache_new();
+
+       return new_item;
+}
+
 struct TotalMsgCount
 {
        guint new;
@@ -790,33 +803,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;
 }
 
@@ -827,26 +842,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;
 }
 
@@ -934,8 +960,10 @@ gint folder_item_scan(FolderItem *item)
                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
+       } else {
                cache_cur_num = G_MAXINT;
+               cache_max_num = 0;
+       }
 
        if (folder_list_cur != NULL) {
                GSList *folder_list_last;
@@ -943,8 +971,10 @@ gint folder_item_scan(FolderItem *item)
                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
+       } else {
                folder_cur_num = G_MAXINT;
+               folder_max_num = 0;
+       }
 
        while ((cache_cur_num != G_MAXINT) || (folder_cur_num != G_MAXINT)) {
                /*
@@ -1084,7 +1114,6 @@ 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;
@@ -1106,7 +1135,6 @@ 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;
@@ -1406,7 +1434,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;
                        
@@ -1416,26 +1445,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)
@@ -1527,6 +1559,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);
                        }
@@ -1558,6 +1591,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);
@@ -1623,6 +1657,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);
                }                       
@@ -1681,7 +1716,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
@@ -1711,6 +1746,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);
                        }
@@ -1748,6 +1784,7 @@ gint folder_item_remove_msg(FolderItem *item, gint num)
                msgcache_remove_msg(item->cache, num);
        }
        item->total--;
+       item->need_update = TRUE;
 
        return ret;
 }
@@ -1795,6 +1832,7 @@ gint folder_item_remove_all_msg(FolderItem *item)
                item->new = 0;
                item->unread = 0;
                item->total = 0;
+               item->need_update = TRUE;
        }
 
        return result;
@@ -1860,7 +1898,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;
@@ -1897,11 +1936,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);
@@ -1951,12 +1986,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;
@@ -1979,7 +2018,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;
@@ -1997,7 +2037,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);
@@ -2028,14 +2069,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;
        }
@@ -2051,6 +2094,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,
@@ -2106,11 +2151,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 {
@@ -2126,9 +2173,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)
@@ -2158,6 +2203,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) {
@@ -2390,14 +2441,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);
        
@@ -2405,15 +2454,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);
 }