2004-12-14 [colin] 0.9.13cvs17
[claws.git] / src / folderutils.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2004 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib.h>
21
22 #include "utils.h"
23 #include "prefs_common.h"
24 #include "folderutils.h"
25
26 gint folderutils_delete_duplicates(FolderItem *item,
27                                    DeleteDuplicatesMode mode)
28 {
29         GHashTable *table;
30         GSList *msglist, *cur, *duplist = NULL;
31         guint dups;
32         
33         if (item->folder->klass->remove_msg == NULL)
34                 return -1;
35         
36         debug_print("Deleting duplicated messages...\n");
37
38
39         msglist = folder_item_get_msg_list(item);
40         if (msglist == NULL)
41                 return 0;
42         table = g_hash_table_new(g_str_hash, g_str_equal);
43
44         folder_item_update_freeze();
45         for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
46                 MsgInfo *msginfo = (MsgInfo *) cur->data;
47                 MsgInfo *msginfo_dup = NULL;
48
49                 if (!msginfo || !msginfo->msgid || !*msginfo->msgid)
50                         continue;
51                 
52                 msginfo_dup = g_hash_table_lookup(table, msginfo->msgid);
53                 if (msginfo_dup == NULL)
54                         g_hash_table_insert(table, msginfo->msgid, msginfo);
55                 else {
56                         if ((MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_UNREAD(msginfo_dup->flags)) || 
57                             (MSG_IS_UNREAD(msginfo->flags) == MSG_IS_UNREAD(msginfo_dup->flags))) {
58                                 duplist = g_slist_append(duplist, msginfo);
59                         } else {
60                                 duplist = g_slist_append(duplist, msginfo_dup);
61                                 g_hash_table_insert(table, msginfo->msgid, msginfo);
62                         }
63                 }
64         }
65
66         if (duplist) {
67                 switch (mode) {
68                 case DELETE_DUPLICATES_REMOVE: {
69                         FolderItem *trash = item->folder->trash;
70
71                         if (item->stype == F_TRASH || trash == NULL)
72                                 folder_item_remove_msgs(item, duplist);
73                         else                    
74                                 folder_item_move_msgs(trash, duplist);
75                         break;
76                 }
77                 case DELETE_DUPLICATES_SETFLAG:
78                         for (cur = duplist; cur != NULL; cur = g_slist_next(cur)) {
79                                 MsgInfo *msginfo = (MsgInfo *) cur->data;
80
81                                 procmsg_msginfo_set_to_folder(msginfo, NULL);
82                                 procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
83                                 procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
84                         }
85                         break;
86                 default:
87                         break;
88                 }
89         }
90         dups = g_slist_length(duplist);
91         g_slist_free(duplist);
92
93         g_hash_table_destroy(table);
94
95         for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
96                 MsgInfo *msginfo = (MsgInfo *) cur->data;
97
98                 procmsg_msginfo_free(msginfo);
99         }
100         g_slist_free(msglist);
101
102         folder_item_update_thaw();
103
104         debug_print("done.\n");
105
106         return dups;    
107 }
108
109 void folderutils_mark_all_read(FolderItem *item)
110 {
111         MsgInfoList *msglist, *cur;
112
113         g_return_if_fail(item != NULL);
114
115         msglist = folder_item_get_msg_list(item);
116         if (msglist == NULL)
117                 return;
118
119         folder_item_update_freeze();
120         for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
121                 MsgInfo *msginfo = cur->data;
122
123                 if (msginfo->flags.perm_flags & (MSG_NEW | MSG_UNREAD))
124                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
125                 procmsg_msginfo_free(msginfo);
126         }
127         folder_item_update_thaw();
128
129         g_slist_free(msglist);
130 }