2.10.0 unleashed
[claws.git] / src / folderutils.c
blob0a3b1eff0add7077f35555d9b32b5db5761bf494
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2004-2007 Hiroyuki Yamamoto & The Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <glib.h>
22 #include "utils.h"
23 #include "prefs_common.h"
24 #include "folderutils.h"
25 #include "prefs_account.h"
26 #include "account.h"
27 #include "mainwindow.h"
28 #include "summaryview.h"
30 gint folderutils_delete_duplicates(FolderItem *item,
31 DeleteDuplicatesMode mode)
33 GHashTable *table;
34 GSList *msglist, *cur, *duplist = NULL;
35 guint dups;
37 if (item->folder->klass->remove_msg == NULL)
38 return -1;
40 debug_print("Deleting duplicated messages...\n");
42 msglist = folder_item_get_msg_list(item);
43 if (msglist == NULL)
44 return 0;
45 table = g_hash_table_new(g_str_hash, g_str_equal);
47 folder_item_update_freeze();
48 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
49 MsgInfo *msginfo = (MsgInfo *) cur->data;
50 MsgInfo *msginfo_dup = NULL;
52 if (!msginfo || !msginfo->msgid || !*msginfo->msgid)
53 continue;
55 msginfo_dup = g_hash_table_lookup(table, msginfo->msgid);
56 if (msginfo_dup == NULL)
57 g_hash_table_insert(table, msginfo->msgid, msginfo);
58 else {
59 if ((MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_UNREAD(msginfo_dup->flags)) ||
60 (MSG_IS_UNREAD(msginfo->flags) == MSG_IS_UNREAD(msginfo_dup->flags))) {
61 duplist = g_slist_append(duplist, msginfo);
62 } else {
63 duplist = g_slist_append(duplist, msginfo_dup);
64 g_hash_table_insert(table, msginfo->msgid, msginfo);
69 if (duplist) {
70 switch (mode) {
71 case DELETE_DUPLICATES_REMOVE: {
72 FolderItem *trash = NULL;
73 gboolean in_trash = FALSE;
74 PrefsAccount *ac;
76 if (NULL != (ac = account_find_from_item(item))) {
77 trash = account_get_special_folder(ac, F_TRASH);
78 in_trash = (trash != NULL && trash == item);
80 if (trash == NULL)
81 trash = item->folder->trash;
83 if (folder_has_parent_of_type(item, F_TRASH) || trash == NULL || in_trash)
84 folder_item_remove_msgs(item, duplist);
85 else
86 folder_item_move_msgs(trash, duplist);
87 break;
89 case DELETE_DUPLICATES_SETFLAG:
90 for (cur = duplist; cur != NULL; cur = g_slist_next(cur)) {
91 MsgInfo *msginfo = (MsgInfo *) cur->data;
93 procmsg_msginfo_set_to_folder(msginfo, NULL);
94 procmsg_msginfo_unset_flags(msginfo, MSG_MARKED,
95 MSG_MOVE | MSG_COPY | MSG_MOVE_DONE);
96 procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
98 break;
99 default:
100 break;
103 dups = g_slist_length(duplist);
104 g_slist_free(duplist);
106 g_hash_table_destroy(table);
108 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
109 MsgInfo *msginfo = (MsgInfo *) cur->data;
111 procmsg_msginfo_free(msginfo);
113 g_slist_free(msglist);
115 folder_item_update_thaw();
117 debug_print("done.\n");
119 return dups;
122 void folderutils_mark_all_read(FolderItem *item)
124 MsgInfoList *msglist, *cur;
125 MainWindow *mainwin = mainwindow_get_mainwindow();
126 int i = 0, m = 0;
127 debug_print("marking all read in item %s\n", (item==NULL)?"NULL":item->name);
128 g_return_if_fail(item != NULL);
131 folder_item_update_freeze();
132 if (mainwin && mainwin->summaryview &&
133 mainwin->summaryview->folder_item == item) {
134 debug_print("folder opened, using summary\n");
135 summary_mark_all_read(mainwin->summaryview);
136 } else {
137 msglist = folder_item_get_msg_list(item);
138 debug_print("got msglist %p\n", msglist);
139 if (msglist == NULL) {
140 folder_item_update_thaw();
141 return;
143 folder_item_set_batch(item, TRUE);
144 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
145 MsgInfo *msginfo = cur->data;
147 if (msginfo->flags.perm_flags & (MSG_NEW | MSG_UNREAD)) {
148 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
149 m++;
151 i++;
152 procmsg_msginfo_free(msginfo);
154 folder_item_set_batch(item, FALSE);
155 folder_item_close(item);
156 debug_print("marked %d messages out of %d as read\n", m, i);
157 g_slist_free(msglist);
159 folder_item_update_thaw();