File: folderutils.c

package info (click to toggle)
claws-mail 4.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 51,124 kB
  • sloc: ansic: 268,194; cpp: 19,477; xml: 11,269; sh: 5,794; perl: 2,767; makefile: 2,509; yacc: 2,470; python: 334; lex: 293
file content (198 lines) | stat: -rw-r--r-- 5,707 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
 * Copyright (C) 2004-2012 Hiroyuki Yamamoto & The Claws Mail Team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */

#include "config.h"

#include <glib.h>

#include "utils.h"
#include "prefs_common.h"
#include "folderutils.h"
#include "prefs_account.h"
#include "account.h"
#include "mainwindow.h"
#include "summaryview.h"

static gboolean folderutils_mark_all_read_node_func(GNode *node, gpointer data);


gint folderutils_delete_duplicates(FolderItem *item,
				   DeleteDuplicatesMode mode)
{
	GHashTable *table;
	GSList *msglist, *cur, *duplist = NULL;
	guint dups;
	
	if (item->folder->klass->remove_msg == NULL)
		return -1;
	
	debug_print("Deleting duplicated messages...\n");

	msglist = folder_item_get_msg_list(item);
	if (msglist == NULL)
		return 0;
	table = g_hash_table_new(g_str_hash, g_str_equal);

	folder_item_update_freeze();
	for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
		MsgInfo *msginfo = (MsgInfo *) cur->data;
		MsgInfo *msginfo_dup = NULL;

		if (!msginfo || !msginfo->msgid || !*msginfo->msgid)
			continue;
		
		msginfo_dup = g_hash_table_lookup(table, msginfo->msgid);
		if (msginfo_dup == NULL)
			g_hash_table_insert(table, msginfo->msgid, msginfo);
		else {
			if ((MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_UNREAD(msginfo_dup->flags)) || 
			    (MSG_IS_UNREAD(msginfo->flags) == MSG_IS_UNREAD(msginfo_dup->flags))) {
				duplist = g_slist_prepend(duplist, msginfo);
			} else {
				duplist = g_slist_prepend(duplist, msginfo_dup);
				g_hash_table_insert(table, msginfo->msgid, msginfo);
			}
		}
	}

	if (duplist) {
		switch (mode) {
		case DELETE_DUPLICATES_REMOVE: {
			FolderItem *trash = NULL;
			gboolean in_trash = FALSE;
			PrefsAccount *ac;

			if (NULL != (ac = account_find_from_item(item))) {
				trash = account_get_special_folder(ac, F_TRASH);
				in_trash = (trash != NULL && trash == item);
			}
			if (trash == NULL)
				trash = item->folder->trash;

			if (folder_has_parent_of_type(item, F_TRASH) || trash == NULL || in_trash)
				folder_item_remove_msgs(item, duplist);
			else 			
				folder_item_move_msgs(trash, duplist);
			break;
		}
		case DELETE_DUPLICATES_SETFLAG:
			for (cur = duplist; cur != NULL; cur = g_slist_next(cur)) {
				MsgInfo *msginfo = (MsgInfo *) cur->data;

				procmsg_msginfo_set_to_folder(msginfo, NULL);
				procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, 
					MSG_MOVE | MSG_COPY | MSG_MOVE_DONE);
				procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
			}
			break;
		default:
			break;
		}
	}
	dups = g_slist_length(duplist);
	g_slist_free(duplist);

	g_hash_table_destroy(table);

	for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
		MsgInfo *msginfo = (MsgInfo *) cur->data;

		procmsg_msginfo_free(&msginfo);
	}
	g_slist_free(msglist);

	folder_item_update_thaw();

	debug_print("done.\n");

	return dups;
}

void folderutils_mark_all_read(FolderItem *item, gboolean mark_as_read)
{
	MsgInfoList *msglist, *cur;
	MainWindow *mainwin = mainwindow_get_mainwindow();
	int i = 0, m = 0;
	gchar *msg = mark_as_read?"read":"unread";
	void(*summary_mark_func)(SummaryView*, gboolean) =
			mark_as_read?summary_mark_all_read:summary_mark_all_unread;

	debug_print("marking all %s in item %s\n", msg, (item==NULL)?"NULL":item->name);
	cm_return_if_fail(item != NULL);

	folder_item_update_freeze();
	if (mainwin && mainwin->summaryview &&
	    mainwin->summaryview->folder_item == item) {
		debug_print("folder opened, using summary\n");
		summary_mark_func(mainwin->summaryview, FALSE);
	} else {
		msglist = folder_item_get_msg_list(item);
		debug_print("got msglist %p\n", msglist);
		if (msglist == NULL) {
			folder_item_update_thaw();
			return;
		}
		folder_item_set_batch(item, TRUE);
		for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
			MsgInfo *msginfo = cur->data;

			if (mark_as_read) {
				if (msginfo->flags.perm_flags & (MSG_NEW | MSG_UNREAD)) {
					procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
					m++;
				}
			} else {
				if (!(msginfo->flags.perm_flags & MSG_UNREAD)) {
					procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
					m++;
				}
			}
			i++;
			procmsg_msginfo_free(&msginfo);
		}
		folder_item_set_batch(item, FALSE);
		folder_item_close(item);
		debug_print("marked %d messages out of %d as %s\n", m, i, msg);
		g_slist_free(msglist);
	}
	folder_item_update_thaw();
}

static gboolean folderutils_mark_all_read_node_func(GNode *node, gpointer data)
{
	if (node) {
		FolderItem *sub_item = (FolderItem *) node->data;
		if (prefs_common.run_processingrules_before_mark_all)
			folderview_run_processing(sub_item);
		folderutils_mark_all_read(sub_item, (gboolean) GPOINTER_TO_INT(data));
	}
	return(FALSE);
}

void folderutils_mark_all_read_recursive(FolderItem *item, gboolean mark_as_read)
{
	cm_return_if_fail(item != NULL);

	cm_return_if_fail(item->folder != NULL);
	cm_return_if_fail(item->folder->node != NULL);

	g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
		folderutils_mark_all_read_node_func,
		GINT_TO_POINTER(mark_as_read));
}