2006-02-20 [wwp] 2.0.0cvs66
[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
28 gint folderutils_delete_duplicates(FolderItem *item,
29                                    DeleteDuplicatesMode mode)
30 {
31         GHashTable *table;
32         GSList *msglist, *cur, *duplist = NULL;
33         guint dups;
34         
35         if (item->folder->klass->remove_msg == NULL)
36                 return -1;
37         
38         debug_print("Deleting duplicated messages...\n");
39
40         msglist = folder_item_get_msg_list(item);
41         if (msglist == NULL)
42                 return 0;
43         table = g_hash_table_new(g_str_hash, g_str_equal);
44
45         folder_item_update_freeze();
46         for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
47                 MsgInfo *msginfo = (MsgInfo *) cur->data;
48                 MsgInfo *msginfo_dup = NULL;
49
50                 if (!msginfo || !msginfo->msgid || !*msginfo->msgid)
51                         continue;
52                 
53                 msginfo_dup = g_hash_table_lookup(table, msginfo->msgid);
54                 if (msginfo_dup == NULL)
55                         g_hash_table_insert(table, msginfo->msgid, msginfo);
56                 else {
57                         if ((MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_UNREAD(msginfo_dup->flags)) || 
58                             (MSG_IS_UNREAD(msginfo->flags) == MSG_IS_UNREAD(msginfo_dup->flags))) {
59                                 duplist = g_slist_append(duplist, msginfo);
60                         } else {
61                                 duplist = g_slist_append(duplist, msginfo_dup);
62                                 g_hash_table_insert(table, msginfo->msgid, msginfo);
63                         }
64                 }
65         }
66
67         if (duplist) {
68                 switch (mode) {
69                 case DELETE_DUPLICATES_REMOVE: {
70                         FolderItem *trash = NULL;
71                         gboolean in_trash = FALSE;
72                         PrefsAccount *ac;
73
74                         if (NULL != (ac = account_find_from_item(item))) {
75                                 trash = account_get_special_folder(ac, F_TRASH);
76                                 in_trash = (trash != NULL && trash == item);
77                         }
78                         if (trash == NULL)
79                                 trash = item->folder->trash;
80
81                         if (folder_has_parent_of_type(item, F_TRASH) || trash == NULL || in_trash)
82                                 folder_item_remove_msgs(item, duplist);
83                         else                    
84                                 folder_item_move_msgs(trash, duplist);
85                         break;
86                 }
87                 case DELETE_DUPLICATES_SETFLAG:
88                         for (cur = duplist; cur != NULL; cur = g_slist_next(cur)) {
89                                 MsgInfo *msginfo = (MsgInfo *) cur->data;
90
91                                 procmsg_msginfo_set_to_folder(msginfo, NULL);
92                                 procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
93                                 procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
94                         }
95                         break;
96                 default:
97                         break;
98                 }
99         }
100         dups = g_slist_length(duplist);
101         g_slist_free(duplist);
102
103         g_hash_table_destroy(table);
104
105         for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
106                 MsgInfo *msginfo = (MsgInfo *) cur->data;
107
108                 procmsg_msginfo_free(msginfo);
109         }
110         g_slist_free(msglist);
111
112         folder_item_update_thaw();
113
114         debug_print("done.\n");
115
116         return dups;    
117 }
118
119 void folderutils_mark_all_read(FolderItem *item)
120 {
121         MsgInfoList *msglist, *cur;
122
123         g_return_if_fail(item != NULL);
124
125         msglist = folder_item_get_msg_list(item);
126         if (msglist == NULL)
127                 return;
128
129         folder_item_update_freeze();
130         folder_item_set_batch(item, TRUE);
131         for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
132                 MsgInfo *msginfo = cur->data;
133
134                 if (msginfo->flags.perm_flags & (MSG_NEW | MSG_UNREAD))
135                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
136                 procmsg_msginfo_free(msginfo);
137         }
138         folder_item_set_batch(item, FALSE);
139         folder_item_update_thaw();
140
141         g_slist_free(msglist);
142 }