2006-06-27 [ticho] 2.3.1cvs24
[claws.git] / src / folderutils.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2004-2006 Hiroyuki Yamamoto & The Sylpheed-Claws Team
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include <glib.h>
21
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"
29
30 gint folderutils_delete_duplicates(FolderItem *item,
31                                    DeleteDuplicatesMode mode)
32 {
33         GHashTable *table;
34         GSList *msglist, *cur, *duplist = NULL;
35         guint dups;
36         
37         if (item->folder->klass->remove_msg == NULL)
38                 return -1;
39         
40         debug_print("Deleting duplicated messages...\n");
41
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);
46
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;
51
52                 if (!msginfo || !msginfo->msgid || !*msginfo->msgid)
53                         continue;
54                 
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);
65                         }
66                 }
67         }
68
69         if (duplist) {
70                 switch (mode) {
71                 case DELETE_DUPLICATES_REMOVE: {
72                         FolderItem *trash = NULL;
73                         gboolean in_trash = FALSE;
74                         PrefsAccount *ac;
75
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);
79                         }
80                         if (trash == NULL)
81                                 trash = item->folder->trash;
82
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;
88                 }
89                 case DELETE_DUPLICATES_SETFLAG:
90                         for (cur = duplist; cur != NULL; cur = g_slist_next(cur)) {
91                                 MsgInfo *msginfo = (MsgInfo *) cur->data;
92
93                                 procmsg_msginfo_set_to_folder(msginfo, NULL);
94                                 procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
95                                 procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
96                         }
97                         break;
98                 default:
99                         break;
100                 }
101         }
102         dups = g_slist_length(duplist);
103         g_slist_free(duplist);
104
105         g_hash_table_destroy(table);
106
107         for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
108                 MsgInfo *msginfo = (MsgInfo *) cur->data;
109
110                 procmsg_msginfo_free(msginfo);
111         }
112         g_slist_free(msglist);
113
114         folder_item_update_thaw();
115
116         debug_print("done.\n");
117
118         return dups;    
119 }
120
121 void folderutils_mark_all_read(FolderItem *item)
122 {
123         MsgInfoList *msglist, *cur;
124         MainWindow *mainwin = mainwindow_get_mainwindow();
125
126         g_return_if_fail(item != NULL);
127
128
129         folder_item_update_freeze();
130         if (mainwin && mainwin->summaryview &&
131             mainwin->summaryview->folder_item == item) {
132                 summary_mark_all_read(mainwin->summaryview);
133         } else {
134                 msglist = folder_item_get_msg_list(item);
135                 if (msglist == NULL) {
136                         folder_item_update_thaw();
137                         return;
138                 }
139                 folder_item_set_batch(item, TRUE);
140                 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
141                         MsgInfo *msginfo = cur->data;
142
143                         if (msginfo->flags.perm_flags & (MSG_NEW | MSG_UNREAD))
144                                 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
145                         procmsg_msginfo_free(msginfo);
146                 }
147                 folder_item_set_batch(item, FALSE);
148                 folder_item_close(item);
149
150                 g_slist_free(msglist);
151         }
152         folder_item_update_thaw();
153 }