2005-02-17 [colin] 1.0.1cvs11.5
[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         msglist = folder_item_get_msg_list(item);
39         if (msglist == NULL)
40                 return 0;
41         table = g_hash_table_new(g_str_hash, g_str_equal);
42
43         folder_item_update_freeze();
44         for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
45                 MsgInfo *msginfo = (MsgInfo *) cur->data;
46                 MsgInfo *msginfo_dup = NULL;
47
48                 if (!msginfo || !msginfo->msgid || !*msginfo->msgid)
49                         continue;
50                 
51                 msginfo_dup = g_hash_table_lookup(table, msginfo->msgid);
52                 if (msginfo_dup == NULL)
53                         g_hash_table_insert(table, msginfo->msgid, msginfo);
54                 else {
55                         if ((MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_UNREAD(msginfo_dup->flags)) || 
56                             (MSG_IS_UNREAD(msginfo->flags) == MSG_IS_UNREAD(msginfo_dup->flags))) {
57                                 duplist = g_slist_append(duplist, msginfo);
58                         } else {
59                                 duplist = g_slist_append(duplist, msginfo_dup);
60                                 g_hash_table_insert(table, msginfo->msgid, msginfo);
61                         }
62                 }
63         }
64
65         if (duplist) {
66                 switch (mode) {
67                 case DELETE_DUPLICATES_REMOVE: {
68                         FolderItem *trash = item->folder->trash;
69
70                         if (item->stype == F_TRASH || trash == NULL)
71                                 folder_item_remove_msgs(item, duplist);
72                         else                    
73                                 folder_item_move_msgs(trash, duplist);
74                         break;
75                 }
76                 case DELETE_DUPLICATES_SETFLAG:
77                         for (cur = duplist; cur != NULL; cur = g_slist_next(cur)) {
78                                 MsgInfo *msginfo = (MsgInfo *) cur->data;
79
80                                 procmsg_msginfo_set_to_folder(msginfo, NULL);
81                                 procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
82                                 procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
83                         }
84                         break;
85                 default:
86                         break;
87                 }
88         }
89         dups = g_slist_length(duplist);
90         g_slist_free(duplist);
91
92         g_hash_table_destroy(table);
93
94         for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
95                 MsgInfo *msginfo = (MsgInfo *) cur->data;
96
97                 procmsg_msginfo_free(msginfo);
98         }
99         g_slist_free(msglist);
100
101         folder_item_update_thaw();
102
103         debug_print("done.\n");
104
105         return dups;    
106 }
107
108 void folderutils_mark_all_read(FolderItem *item)
109 {
110         MsgInfoList *msglist, *cur;
111
112         g_return_if_fail(item != NULL);
113
114         msglist = folder_item_get_msg_list(item);
115         if (msglist == NULL)
116                 return;
117
118         folder_item_update_freeze();
119         for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
120                 MsgInfo *msginfo = cur->data;
121
122                 if (msginfo->flags.perm_flags & (MSG_NEW | MSG_UNREAD))
123                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
124                 procmsg_msginfo_free(msginfo);
125         }
126         folder_item_update_thaw();
127
128         g_slist_free(msglist);
129 }