Fix marking all as (un)read recursively when the confirmation
[claws.git] / src / folderutils.c
index 9460c1ffbc227e962ec2f204c31e8299cf8a5d8d..355af8553194c50dcc93037df2489f31e5138266 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2004 Hiroyuki Yamamoto & The Sylpheed-Claws Team
+ * Copyright (C) 2004-2012 Hiroyuki Yamamoto & The Claws Mail Team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #include <glib.h>
 #include "utils.h"
 #include "prefs_common.h"
 #include "folderutils.h"
+#include "prefs_account.h"
+#include "account.h"
+#include "mainwindow.h"
+#include "summaryview.h"
 
 gint folderutils_delete_duplicates(FolderItem *item,
                                   DeleteDuplicatesMode mode)
@@ -35,13 +39,12 @@ gint folderutils_delete_duplicates(FolderItem *item,
        
        debug_print("Deleting duplicated messages...\n");
 
-       folder_item_update_freeze();
-
        msglist = folder_item_get_msg_list(item);
        if (msglist == NULL)
                return 0;
        table = g_hash_table_new(g_str_hash, g_str_equal);
 
+       folder_item_update_freeze();
        for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
                MsgInfo *msginfo = (MsgInfo *) cur->data;
                MsgInfo *msginfo_dup = NULL;
@@ -55,9 +58,9 @@ gint folderutils_delete_duplicates(FolderItem *item,
                else {
                        if ((MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_UNREAD(msginfo_dup->flags)) || 
                            (MSG_IS_UNREAD(msginfo->flags) == MSG_IS_UNREAD(msginfo_dup->flags))) {
-                               duplist = g_slist_append(duplist, msginfo);
+                               duplist = g_slist_prepend(duplist, msginfo);
                        } else {
-                               duplist = g_slist_append(duplist, msginfo_dup);
+                               duplist = g_slist_prepend(duplist, msginfo_dup);
                                g_hash_table_insert(table, msginfo->msgid, msginfo);
                        }
                }
@@ -66,9 +69,18 @@ gint folderutils_delete_duplicates(FolderItem *item,
        if (duplist) {
                switch (mode) {
                case DELETE_DUPLICATES_REMOVE: {
-                       FolderItem *trash = item->folder->trash;
+                       FolderItem *trash = NULL;
+                       gboolean in_trash = FALSE;
+                       PrefsAccount *ac;
 
-                       if (item->stype == F_TRASH || trash == NULL)
+                       if (NULL != (ac = account_find_from_item(item))) {
+                               trash = account_get_special_folder(ac, F_TRASH);
+                               in_trash = (trash != NULL && trash == item);
+                       }
+                       if (trash == NULL)
+                               trash = item->folder->trash;
+
+                       if (folder_has_parent_of_type(item, F_TRASH) || trash == NULL || in_trash)
                                folder_item_remove_msgs(item, duplist);
                        else                    
                                folder_item_move_msgs(trash, duplist);
@@ -79,7 +91,8 @@ gint folderutils_delete_duplicates(FolderItem *item,
                                MsgInfo *msginfo = (MsgInfo *) cur->data;
 
                                procmsg_msginfo_set_to_folder(msginfo, NULL);
-                               procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
+                               procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, 
+                                       MSG_MOVE | MSG_COPY | MSG_MOVE_DONE);
                                procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
                        }
                        break;
@@ -95,7 +108,7 @@ gint folderutils_delete_duplicates(FolderItem *item,
        for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
                MsgInfo *msginfo = (MsgInfo *) cur->data;
 
-               procmsg_msginfo_free(msginfo);
+               procmsg_msginfo_free(&msginfo);
        }
        g_slist_free(msglist);
 
@@ -103,28 +116,133 @@ gint folderutils_delete_duplicates(FolderItem *item,
 
        debug_print("done.\n");
 
-       return dups;    
+       return dups;
 }
 
 void folderutils_mark_all_read(FolderItem *item)
 {
        MsgInfoList *msglist, *cur;
+       MainWindow *mainwin = mainwindow_get_mainwindow();
+       int i = 0, m = 0;
+       debug_print("marking all read in item %s\n", (item==NULL)?"NULL":item->name);
+       cm_return_if_fail(item != NULL);
 
-       g_return_if_fail(item != NULL);
 
-       msglist = folder_item_get_msg_list(item);
-       if (msglist == NULL)
-               return;
+       folder_item_update_freeze();
+       if (mainwin && mainwin->summaryview &&
+           mainwin->summaryview->folder_item == item) {
+               debug_print("folder opened, using summary\n");
+               summary_mark_all_read(mainwin->summaryview, FALSE);
+       } else {
+               msglist = folder_item_get_msg_list(item);
+               debug_print("got msglist %p\n", msglist);
+               if (msglist == NULL) {
+                       folder_item_update_thaw();
+                       return;
+               }
+               folder_item_set_batch(item, TRUE);
+               for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
+                       MsgInfo *msginfo = cur->data;
+
+                       if (msginfo->flags.perm_flags & (MSG_NEW | MSG_UNREAD)) {
+                               procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
+                               m++;
+                       }
+                       i++;
+                       procmsg_msginfo_free(&msginfo);
+               }
+               folder_item_set_batch(item, FALSE);
+               folder_item_close(item);
+               debug_print("marked %d messages out of %d as read\n", m, i);
+               g_slist_free(msglist);
+       }
+       folder_item_update_thaw();
+}
+
+void folderutils_mark_all_unread(FolderItem *item)
+{
+       MsgInfoList *msglist, *cur;
+       MainWindow *mainwin = mainwindow_get_mainwindow();
+       int i = 0, m = 0;
+       debug_print("marking all unread in item %s\n", (item==NULL)?"NULL":item->name);
+       cm_return_if_fail(item != NULL);
+
 
        folder_item_update_freeze();
-       for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
-               MsgInfo *msginfo = cur->data;
+       if (mainwin && mainwin->summaryview &&
+           mainwin->summaryview->folder_item == item) {
+               debug_print("folder opened, using summary\n");
+               summary_mark_all_unread(mainwin->summaryview, FALSE);
+       } else {
+               msglist = folder_item_get_msg_list(item);
+               debug_print("got msglist %p\n", msglist);
+               if (msglist == NULL) {
+                       folder_item_update_thaw();
+                       return;
+               }
+               folder_item_set_batch(item, TRUE);
+               for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
+                       MsgInfo *msginfo = cur->data;
 
-               if (msginfo->flags.perm_flags & (MSG_NEW | MSG_UNREAD))
-                       procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
-               procmsg_msginfo_free(msginfo);
+                       if (!(msginfo->flags.perm_flags & MSG_UNREAD)) {
+                               procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
+                               m++;
+                       }
+                       i++;
+                       procmsg_msginfo_free(&msginfo);
+               }
+               folder_item_set_batch(item, FALSE);
+               folder_item_close(item);
+               debug_print("marked %d messages out of %d as unread\n", m, i);
+               g_slist_free(msglist);
        }
        folder_item_update_thaw();
+}
 
-       g_slist_free(msglist);
+void folderutils_mark_all_read_recursive(FolderItem *item)
+{
+       GNode *node;
+
+       cm_return_if_fail(item != NULL);
+
+       folderutils_mark_all_read(item);
+
+       cm_return_if_fail(item->folder != NULL);
+       cm_return_if_fail(item->folder->node != NULL);
+
+       node = item->folder->node;
+       node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
+       node = node->children;
+
+       while (node != NULL) {
+               if (node->data != NULL) {
+                       FolderItem *sub_item = (FolderItem *) node->data;
+                       node = node->next;
+                       folderutils_mark_all_read_recursive(sub_item);
+               }
+       }
+}
+
+void folderutils_mark_all_unread_recursive(FolderItem *item)
+{
+       GNode *node;
+
+       cm_return_if_fail(item != NULL);
+
+       folderutils_mark_all_unread(item);
+
+       cm_return_if_fail(item->folder != NULL);
+       cm_return_if_fail(item->folder->node != NULL);
+
+       node = item->folder->node;
+       node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
+       node = node->children;
+
+       while (node != NULL) {
+               if (node->data != NULL) {
+                       FolderItem *sub_item = (FolderItem *) node->data;
+                       node = node->next;
+                       folderutils_mark_all_unread_recursive(sub_item);
+               }
+       }
 }