talons

Fork of Claws Mail https://www.claws-mail
Log | Files | Refs | README | LICENSE

folderutils.c (5553B)


      1 /*
      2  * Claws Mail -- a GTK based, lightweight, and fast e-mail client
      3  * Copyright (C) 2004-2012 Hiroyuki Yamamoto & The Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
     17  *
     18  */
     19 
     20 #include <glib.h>
     21 
     22 #include "utils.h"
     23 #include "folderutils.h"
     24 #include "prefs_account.h"
     25 #include "account.h"
     26 #include "mainwindow.h"
     27 #include "summaryview.h"
     28 
     29 static gboolean folderutils_mark_all_read_node_func(GNode *node, gpointer data);
     30 
     31 
     32 gint folderutils_delete_duplicates(FolderItem *item,
     33 				   DeleteDuplicatesMode mode)
     34 {
     35 	GHashTable *table;
     36 	GSList *msglist, *cur, *duplist = NULL;
     37 	guint dups;
     38 
     39 	if (item->folder->klass->remove_msg == NULL)
     40 		return -1;
     41 
     42 	debug_print("Deleting duplicated messages...\n");
     43 
     44 	msglist = folder_item_get_msg_list(item);
     45 	if (msglist == NULL)
     46 		return 0;
     47 	table = g_hash_table_new(g_str_hash, g_str_equal);
     48 
     49 	folder_item_update_freeze();
     50 	for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
     51 		MsgInfo *msginfo = (MsgInfo *) cur->data;
     52 		MsgInfo *msginfo_dup = NULL;
     53 
     54 		if (!msginfo || !msginfo->msgid || !*msginfo->msgid)
     55 			continue;
     56 
     57 		msginfo_dup = g_hash_table_lookup(table, msginfo->msgid);
     58 		if (msginfo_dup == NULL)
     59 			g_hash_table_insert(table, msginfo->msgid, msginfo);
     60 		else {
     61 			if ((MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_UNREAD(msginfo_dup->flags)) ||
     62 			    (MSG_IS_UNREAD(msginfo->flags) == MSG_IS_UNREAD(msginfo_dup->flags))) {
     63 				duplist = g_slist_prepend(duplist, msginfo);
     64 			} else {
     65 				duplist = g_slist_prepend(duplist, msginfo_dup);
     66 				g_hash_table_insert(table, msginfo->msgid, msginfo);
     67 			}
     68 		}
     69 	}
     70 
     71 	if (duplist) {
     72 		switch (mode) {
     73 		case DELETE_DUPLICATES_REMOVE: {
     74 			FolderItem *trash = NULL;
     75 			gboolean in_trash = FALSE;
     76 			PrefsAccount *ac;
     77 
     78 			if (NULL != (ac = account_find_from_item(item))) {
     79 				trash = account_get_special_folder(ac, F_TRASH);
     80 				in_trash = (trash != NULL && trash == item);
     81 			}
     82 			if (trash == NULL)
     83 				trash = item->folder->trash;
     84 
     85 			if (folder_has_parent_of_type(item, F_TRASH) || trash == NULL || in_trash)
     86 				folder_item_remove_msgs(item, duplist);
     87 			else
     88 				folder_item_move_msgs(trash, duplist);
     89 			break;
     90 		}
     91 		case DELETE_DUPLICATES_SETFLAG:
     92 			for (cur = duplist; cur != NULL; cur = g_slist_next(cur)) {
     93 				MsgInfo *msginfo = (MsgInfo *) cur->data;
     94 
     95 				procmsg_msginfo_set_to_folder(msginfo, NULL);
     96 				procmsg_msginfo_unset_flags(msginfo, MSG_MARKED,
     97 					MSG_MOVE | MSG_COPY | MSG_MOVE_DONE);
     98 				procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
     99 			}
    100 			break;
    101 		default:
    102 			break;
    103 		}
    104 	}
    105 	dups = g_slist_length(duplist);
    106 	g_slist_free(duplist);
    107 
    108 	g_hash_table_destroy(table);
    109 
    110 	for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
    111 		MsgInfo *msginfo = (MsgInfo *) cur->data;
    112 
    113 		procmsg_msginfo_free(&msginfo);
    114 	}
    115 	g_slist_free(msglist);
    116 
    117 	folder_item_update_thaw();
    118 
    119 	debug_print("done.\n");
    120 
    121 	return dups;
    122 }
    123 
    124 void folderutils_mark_all_read(FolderItem *item, gboolean mark_as_read)
    125 {
    126 	MsgInfoList *msglist, *cur;
    127 	MainWindow *mainwin = mainwindow_get_mainwindow();
    128 	int i = 0, m = 0;
    129 	gchar *msg = mark_as_read?"read":"unread";
    130 	void(*summary_mark_func)(SummaryView*, gboolean) =
    131 			mark_as_read?summary_mark_all_read:summary_mark_all_unread;
    132 
    133 	debug_print("marking all %s in item %s\n", msg, (item==NULL)?"NULL":item->name);
    134 	cm_return_if_fail(item != NULL);
    135 
    136 	folder_item_update_freeze();
    137 	if (mainwin && mainwin->summaryview &&
    138 	    mainwin->summaryview->folder_item == item) {
    139 		debug_print("folder opened, using summary\n");
    140 		summary_mark_func(mainwin->summaryview, FALSE);
    141 	} else {
    142 		msglist = folder_item_get_msg_list(item);
    143 		debug_print("got msglist %p\n", msglist);
    144 		if (msglist == NULL) {
    145 			folder_item_update_thaw();
    146 			return;
    147 		}
    148 		folder_item_set_batch(item, TRUE);
    149 		for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
    150 			MsgInfo *msginfo = cur->data;
    151 
    152 			if (mark_as_read) {
    153 				if (msginfo->flags.perm_flags & (MSG_NEW | MSG_UNREAD)) {
    154 					procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
    155 					m++;
    156 				}
    157 			} else {
    158 				if (!(msginfo->flags.perm_flags & MSG_UNREAD)) {
    159 					procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
    160 					m++;
    161 				}
    162 			}
    163 			i++;
    164 			procmsg_msginfo_free(&msginfo);
    165 		}
    166 		folder_item_set_batch(item, FALSE);
    167 		folder_item_close(item);
    168 		debug_print("marked %d messages out of %d as %s\n", m, i, msg);
    169 		g_slist_free(msglist);
    170 	}
    171 	folder_item_update_thaw();
    172 }
    173 
    174 static gboolean folderutils_mark_all_read_node_func(GNode *node, gpointer data)
    175 {
    176 	if (node) {
    177 		FolderItem *sub_item = (FolderItem *) node->data;
    178 		folderutils_mark_all_read(sub_item, (gboolean) GPOINTER_TO_INT(data));
    179 	}
    180 	return(FALSE);
    181 }
    182 
    183 void folderutils_mark_all_read_recursive(FolderItem *item, gboolean mark_as_read)
    184 {
    185 	cm_return_if_fail(item != NULL);
    186 
    187 	cm_return_if_fail(item->folder != NULL);
    188 	cm_return_if_fail(item->folder->node != NULL);
    189 
    190 	g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
    191 		folderutils_mark_all_read_node_func,
    192 		GINT_TO_POINTER(mark_as_read));
    193 }