2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
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.
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.
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.
29 #include <sys/types.h>
40 #include "mbox_folder.h"
44 #include "prefs_gtk.h"
46 #include "filtering.h"
48 #include "prefs_folder_item.h"
49 #include "procheader.h"
53 /* Dependecies to be removed ?! */
54 #include "prefs_common.h"
55 #include "prefs_account.h"
56 #include "prefs_folder_item.h"
58 static GList *folder_list = NULL;
60 static void folder_init (Folder *folder,
63 static gboolean folder_read_folder_func (GNode *node,
65 static gchar *folder_get_list_path (void);
66 static void folder_write_list_recursive (GNode *node,
68 static void folder_update_op_count_rec (GNode *node);
71 static void folder_get_persist_prefs_recursive
72 (GNode *node, GHashTable *pptable);
73 static gboolean persist_prefs_free (gpointer key, gpointer val, gpointer data);
74 void folder_item_read_cache (FolderItem *item);
75 void folder_item_free_cache (FolderItem *item);
77 static GSList *classlist;
79 void folder_system_init()
81 folder_register_class(mh_get_class());
82 folder_register_class(imap_get_class());
83 folder_register_class(news_get_class());
84 folder_register_class(mbox_get_class());
87 GSList *folder_get_class_list()
92 void folder_register_class(FolderClass *class)
94 debug_print("registering folder class %s\n", class->idstr);
95 classlist = g_slist_append(classlist, class);
98 Folder *folder_new(FolderClass *class, const gchar *name, const gchar *path)
100 Folder *folder = NULL;
103 g_return_val_if_fail(class != NULL, NULL);
105 name = name ? name : path;
106 folder = class->new(name, path);
108 /* Create root folder item */
109 item = folder_item_new(folder, name, NULL);
110 item->folder = folder;
111 folder->node = g_node_new(item);
117 static void folder_init(Folder *folder, const gchar *name)
119 g_return_if_fail(folder != NULL);
121 folder_set_name(folder, name);
123 /* Init folder data */
124 folder->account = NULL;
125 folder->inbox = NULL;
126 folder->outbox = NULL;
127 folder->draft = NULL;
128 folder->queue = NULL;
129 folder->trash = NULL;
132 void folder_local_folder_init(Folder *folder, const gchar *name,
135 folder_init(folder, name);
136 LOCAL_FOLDER(folder)->rootpath = g_strdup(path);
139 void folder_remote_folder_init(Folder *folder, const gchar *name,
142 folder_init(folder, name);
143 REMOTE_FOLDER(folder)->session = NULL;
146 void folder_destroy(Folder *folder)
148 g_return_if_fail(folder != NULL);
149 g_return_if_fail(folder->class->destroy != NULL);
151 folder_list = g_list_remove(folder_list, folder);
153 folder_tree_destroy(folder);
154 g_free(folder->name);
157 folder->class->destroy(folder);
160 void folder_local_folder_destroy(LocalFolder *lfolder)
162 g_return_if_fail(lfolder != NULL);
164 g_free(lfolder->rootpath);
167 void folder_remote_folder_destroy(RemoteFolder *rfolder)
169 g_return_if_fail(rfolder != NULL);
171 if (rfolder->session)
172 session_destroy(rfolder->session);
175 FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path)
177 FolderItem *item = NULL;
179 if (folder->class->item_new) {
180 item = folder->class->item_new(folder);
182 item = g_new0(FolderItem, 1);
185 g_return_val_if_fail(item != NULL, NULL);
187 item->stype = F_NORMAL;
188 item->name = g_strdup(name);
189 item->path = g_strdup(path);
193 item->unreadmarked = 0;
197 item->no_sub = FALSE;
198 item->no_select = FALSE;
199 item->collapsed = FALSE;
200 item->thread_collapsed = FALSE;
201 item->threaded = TRUE;
202 item->ret_rcpt = FALSE;
203 item->opened = FALSE;
206 item->account = NULL;
207 item->apply_sub = FALSE;
208 item->mark_queue = NULL;
211 item->prefs = prefs_folder_item_new();
216 void folder_item_append(FolderItem *parent, FolderItem *item)
220 g_return_if_fail(parent != NULL);
221 g_return_if_fail(parent->folder != NULL);
222 g_return_if_fail(item != NULL);
224 node = parent->folder->node;
225 node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, parent);
226 g_return_if_fail(node != NULL);
228 item->parent = parent;
229 item->folder = parent->folder;
230 g_node_append_data(node, item);
233 void folder_item_remove(FolderItem *item)
237 g_return_if_fail(item != NULL);
238 g_return_if_fail(item->folder != NULL);
240 node = item->folder->node;
241 node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
242 g_return_if_fail(node != NULL);
244 /* TODO: free all FolderItem's first */
245 if (item->folder->node == node)
246 item->folder->node = NULL;
247 g_node_destroy(node);
250 void folder_item_destroy(FolderItem *item)
252 g_return_if_fail(item != NULL);
254 debug_print("Destroying folder item %s\n", item->path);
257 folder_item_free_cache(item);
261 if (item->folder != NULL) {
262 if(item->folder->class->item_destroy) {
263 item->folder->class->item_destroy(item->folder, item);
270 void folder_set_ui_func(Folder *folder, FolderUIFunc func, gpointer data)
272 g_return_if_fail(folder != NULL);
274 folder->ui_func = func;
275 folder->ui_func_data = data;
278 void folder_set_name(Folder *folder, const gchar *name)
280 g_return_if_fail(folder != NULL);
282 g_free(folder->name);
283 folder->name = name ? g_strdup(name) : NULL;
284 if (folder->node && folder->node->data) {
285 FolderItem *item = (FolderItem *)folder->node->data;
288 item->name = name ? g_strdup(name) : NULL;
292 gboolean folder_tree_destroy_func(GNode *node, gpointer data) {
293 FolderItem *item = (FolderItem *) node->data;
295 folder_item_destroy(item);
299 void folder_tree_destroy(Folder *folder)
301 g_return_if_fail(folder != NULL);
302 g_return_if_fail(folder->node != NULL);
304 prefs_scoring_clear();
305 prefs_filtering_clear();
307 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_tree_destroy_func, NULL);
309 g_node_destroy(folder->node);
311 folder->inbox = NULL;
312 folder->outbox = NULL;
313 folder->draft = NULL;
314 folder->queue = NULL;
315 folder->trash = NULL;
319 void folder_add(Folder *folder)
325 g_return_if_fail(folder != NULL);
327 for (i = 0, cur = folder_list; cur != NULL; cur = cur->next, i++) {
328 cur_folder = FOLDER(cur->data);
329 if (FOLDER_TYPE(folder) == F_MH) {
330 if (FOLDER_TYPE(cur_folder) != F_MH) break;
331 } else if (FOLDER_TYPE(folder) == F_MBOX) {
332 if (FOLDER_TYPE(cur_folder) != F_MH &&
333 FOLDER_TYPE(cur_folder) != F_MBOX) break;
334 } else if (FOLDER_TYPE(folder) == F_IMAP) {
335 if (FOLDER_TYPE(cur_folder) != F_MH &&
336 FOLDER_TYPE(cur_folder) != F_MBOX &&
337 FOLDER_TYPE(cur_folder) != F_IMAP) break;
338 } else if (FOLDER_TYPE(folder) == F_NEWS) {
339 if (FOLDER_TYPE(cur_folder) != F_MH &&
340 FOLDER_TYPE(cur_folder) != F_MBOX &&
341 FOLDER_TYPE(cur_folder) != F_IMAP &&
342 FOLDER_TYPE(cur_folder) != F_NEWS) break;
346 folder_list = g_list_insert(folder_list, folder, i);
349 GList *folder_get_list(void)
354 gint folder_read_list(void)
360 path = folder_get_list_path();
361 if (!is_file_exist(path)) return -1;
362 node = xml_parse_file(path);
363 if (!node) return -1;
365 xmlnode = node->data;
366 if (strcmp2(xmlnode->tag->tag, "folderlist") != 0) {
367 g_warning("wrong folder list\n");
372 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, 2,
373 folder_read_folder_func, NULL);
382 void folder_write_list(void)
389 path = folder_get_list_path();
390 if ((pfile = prefs_write_open(path)) == NULL) return;
392 fprintf(pfile->fp, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
393 conv_get_current_charset_str());
394 fputs("\n<folderlist>\n", pfile->fp);
396 for (list = folder_list; list != NULL; list = list->next) {
398 folder_write_list_recursive(folder->node, pfile->fp);
401 fputs("</folderlist>\n", pfile->fp);
403 if (prefs_file_close(pfile) < 0)
404 g_warning("failed to write folder list.\n");
407 gboolean folder_scan_tree_func(GNode *node, gpointer data)
409 GHashTable *pptable = (GHashTable *)data;
410 FolderItem *item = (FolderItem *)node->data;
412 folder_item_restore_persist_prefs(item, pptable);
417 void folder_scan_tree(Folder *folder)
421 if (!folder->class->scan_tree)
424 pptable = folder_persist_prefs_new(folder);
425 folder_tree_destroy(folder);
427 folder->class->scan_tree(folder);
429 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_scan_tree_func, pptable);
430 folder_persist_prefs_free(pptable);
432 prefs_matcher_read_config();
437 FolderItem *folder_create_folder(FolderItem *parent, const gchar *name)
439 FolderItem *new_item;
441 new_item = parent->folder->class->create_folder(parent->folder, parent, name);
443 new_item->cache = msgcache_new();
456 struct FuncToAllFoldersData
458 FolderItemFunc function;
462 static gboolean folder_func_to_all_folders_func(GNode *node, gpointer data)
465 struct FuncToAllFoldersData *function_data = (struct FuncToAllFoldersData *) data;
467 g_return_val_if_fail(node->data != NULL, FALSE);
469 item = FOLDER_ITEM(node->data);
470 g_return_val_if_fail(item != NULL, FALSE);
472 function_data->function(item, function_data->data);
477 void folder_func_to_all_folders(FolderItemFunc function, gpointer data)
481 struct FuncToAllFoldersData function_data;
483 function_data.function = function;
484 function_data.data = data;
486 for (list = folder_list; list != NULL; list = list->next) {
487 folder = FOLDER(list->data);
489 g_node_traverse(folder->node, G_PRE_ORDER,
491 folder_func_to_all_folders_func,
496 static void folder_count_total_msgs_func(FolderItem *item, gpointer data)
498 struct TotalMsgCount *count = (struct TotalMsgCount *)data;
500 count->new += item->new;
501 count->unread += item->unread;
502 count->unreadmarked += item->unreadmarked;
503 count->total += item->total;
506 void folder_count_total_msgs(guint *new, guint *unread, guint *unreadmarked, guint *total)
508 struct TotalMsgCount count;
510 count.new = count.unread = count.unreadmarked = count.total = 0;
512 debug_print("Counting total number of messages...\n");
514 folder_func_to_all_folders(folder_count_total_msgs_func, &count);
517 *unread = count.unread;
518 *unreadmarked = count.unreadmarked;
519 *total = count.total;
522 Folder *folder_find_from_path(const gchar *path)
527 for (list = folder_list; list != NULL; list = list->next) {
529 if ((FOLDER_TYPE(folder) == F_MH || FOLDER_TYPE(folder) == F_MBOX) &&
530 !path_cmp(LOCAL_FOLDER(folder)->rootpath, path))
537 Folder *folder_find_from_name(const gchar *name, FolderClass *class)
542 for (list = folder_list; list != NULL; list = list->next) {
544 if (folder->class == class && strcmp2(name, folder->name) == 0)
551 static gboolean folder_item_find_func(GNode *node, gpointer data)
553 FolderItem *item = node->data;
555 const gchar *path = d[0];
557 if (path_cmp(path, item->path) != 0)
565 FolderItem *folder_find_item_from_path(const gchar *path)
570 folder = folder_get_default_folder();
571 g_return_val_if_fail(folder != NULL, NULL);
573 d[0] = (gpointer)path;
575 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
576 folder_item_find_func, d);
580 FolderClass *folder_get_class_from_string(const gchar *str)
584 classlist = folder_get_class_list();
585 for (; classlist != NULL; classlist = g_slist_next(classlist)) {
586 FolderClass *class = (FolderClass *) classlist->data;
587 if (g_strcasecmp(class->idstr, str) == 0)
594 gchar *folder_get_identifier(Folder *folder)
598 g_return_val_if_fail(folder != NULL, NULL);
600 type_str = folder->class->idstr;
601 return g_strconcat("#", type_str, "/", folder->name, NULL);
604 gchar *folder_item_get_identifier(FolderItem *item)
609 g_return_val_if_fail(item != NULL, NULL);
610 g_return_val_if_fail(item->path != NULL, NULL);
612 folder_id = folder_get_identifier(item->folder);
613 id = g_strconcat(folder_id, "/", item->path, NULL);
619 FolderItem *folder_find_item_from_identifier(const gchar *identifier)
629 g_return_val_if_fail(identifier != NULL, NULL);
631 if (*identifier != '#')
632 return folder_find_item_from_path(identifier);
634 Xstrdup_a(str, identifier, return NULL);
636 p = strchr(str, '/');
638 return folder_find_item_from_path(identifier);
641 class = folder_get_class_from_string(&str[1]);
643 return folder_find_item_from_path(identifier);
648 return folder_find_item_from_path(identifier);
652 folder = folder_find_from_name(name, class);
654 return folder_find_item_from_path(identifier);
658 d[0] = (gpointer)path;
660 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
661 folder_item_find_func, d);
666 * Get a displayable name for a FolderItem
668 * \param item FolderItem for that a name should be created
669 * \return Displayable name for item, returned string has to
672 gchar *folder_item_get_name(FolderItem *item)
676 switch (item->stype) {
678 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
679 !strcmp2(item->name, INBOX_DIR) ? _("Inbox") :
683 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
684 !strcmp2(item->name, OUTBOX_DIR) ? _("Sent") :
688 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
689 !strcmp2(item->name, QUEUE_DIR) ? _("Queue") :
693 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
694 !strcmp2(item->name, TRASH_DIR) ? _("Trash") :
698 name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
699 !strcmp2(item->name, DRAFT_DIR) ? _("Drafts") :
708 * should probably be done by a virtual function,
709 * the folder knows the ui string and how to abbrev
712 name = g_strconcat(item->name, " (", item->folder->class->uistr, ")", NULL);
714 if (FOLDER_CLASS(item->folder) == news_get_class() &&
715 item->path && !strcmp2(item->name, item->path))
716 name = get_abbrev_newsgroup_name
718 prefs_common.ng_abbrev_len);
720 name = g_strdup(item->name);
730 Folder *folder_get_default_folder(void)
732 return folder_list ? FOLDER(folder_list->data) : NULL;
735 FolderItem *folder_get_default_inbox(void)
739 if (!folder_list) return NULL;
740 folder = FOLDER(folder_list->data);
741 g_return_val_if_fail(folder != NULL, NULL);
742 return folder->inbox;
745 FolderItem *folder_get_default_outbox(void)
749 if (!folder_list) return NULL;
750 folder = FOLDER(folder_list->data);
751 g_return_val_if_fail(folder != NULL, NULL);
752 return folder->outbox;
755 FolderItem *folder_get_default_draft(void)
759 if (!folder_list) return NULL;
760 folder = FOLDER(folder_list->data);
761 g_return_val_if_fail(folder != NULL, NULL);
762 return folder->draft;
765 FolderItem *folder_get_default_queue(void)
769 if (!folder_list) return NULL;
770 folder = FOLDER(folder_list->data);
771 g_return_val_if_fail(folder != NULL, NULL);
772 return folder->queue;
775 FolderItem *folder_get_default_trash(void)
779 if (!folder_list) return NULL;
780 folder = FOLDER(folder_list->data);
781 g_return_val_if_fail(folder != NULL, NULL);
782 return folder->trash;
785 #define CREATE_FOLDER_IF_NOT_EXIST(member, dir, type) \
787 if (!folder->member) { \
788 item = folder_item_new(folder, dir, dir); \
789 item->stype = type; \
790 folder_item_append(rootitem, item); \
791 folder->member = item; \
795 void folder_set_missing_folders(void)
798 FolderItem *rootitem;
802 for (list = folder_list; list != NULL; list = list->next) {
804 if (FOLDER_TYPE(folder) != F_MH) continue;
805 rootitem = FOLDER_ITEM(folder->node->data);
806 g_return_if_fail(rootitem != NULL);
808 if (folder->inbox && folder->outbox && folder->draft &&
809 folder->queue && folder->trash)
812 if (folder->class->create_tree(folder) < 0) {
813 g_warning("%s: can't create the folder tree.\n",
814 LOCAL_FOLDER(folder)->rootpath);
818 CREATE_FOLDER_IF_NOT_EXIST(inbox, INBOX_DIR, F_INBOX);
819 CREATE_FOLDER_IF_NOT_EXIST(outbox, OUTBOX_DIR, F_OUTBOX);
820 CREATE_FOLDER_IF_NOT_EXIST(draft, DRAFT_DIR, F_DRAFT);
821 CREATE_FOLDER_IF_NOT_EXIST(queue, QUEUE_DIR, F_QUEUE);
822 CREATE_FOLDER_IF_NOT_EXIST(trash, TRASH_DIR, F_TRASH);
826 static gboolean folder_unref_account_func(GNode *node, gpointer data)
828 FolderItem *item = node->data;
829 PrefsAccount *account = data;
831 if (item->account == account)
832 item->account = NULL;
837 void folder_unref_account_all(PrefsAccount *account)
842 if (!account) return;
844 for (list = folder_list; list != NULL; list = list->next) {
846 if (folder->account == account)
847 folder->account = NULL;
848 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
849 folder_unref_account_func, account);
853 #undef CREATE_FOLDER_IF_NOT_EXIST
855 gchar *folder_get_path(Folder *folder)
859 g_return_val_if_fail(folder != NULL, NULL);
861 switch(FOLDER_TYPE(folder)) {
864 path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
868 g_return_val_if_fail(folder->account != NULL, NULL);
869 path = g_strconcat(get_imap_cache_dir(),
871 folder->account->recv_server,
873 folder->account->userid,
878 g_return_val_if_fail(folder->account != NULL, NULL);
879 path = g_strconcat(get_news_cache_dir(),
881 folder->account->nntp_server,
893 gchar *folder_item_get_path(FolderItem *item)
898 g_return_val_if_fail(item != NULL, NULL);
900 if(FOLDER_TYPE(item->folder) != F_MBOX) {
901 folder_path = folder_get_path(item->folder);
902 g_return_val_if_fail(folder_path != NULL, NULL);
904 if (folder_path[0] == G_DIR_SEPARATOR) {
906 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
909 path = g_strdup(folder_path);
912 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
913 folder_path, G_DIR_SEPARATOR_S,
916 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
924 itempath = mbox_get_virtual_path(item);
925 if (itempath == NULL)
927 path = g_strconcat(get_mbox_cache_dir(),
928 G_DIR_SEPARATOR_S, itempath, NULL);
934 void folder_item_set_default_flags(FolderItem *dest, MsgFlags *flags)
936 if (!(dest->stype == F_OUTBOX ||
937 dest->stype == F_QUEUE ||
938 dest->stype == F_DRAFT ||
939 dest->stype == F_TRASH)) {
940 flags->perm_flags = MSG_NEW|MSG_UNREAD;
942 flags->perm_flags = 0;
944 flags->tmp_flags = MSG_CACHED;
945 if (FOLDER_TYPE(dest->folder) == F_MH) {
946 if (dest->stype == F_QUEUE) {
947 MSG_SET_TMP_FLAGS(*flags, MSG_QUEUED);
948 } else if (dest->stype == F_DRAFT) {
949 MSG_SET_TMP_FLAGS(*flags, MSG_DRAFT);
954 static gint folder_sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
956 MsgInfo *msginfo_a = (MsgInfo *) a;
957 MsgInfo *msginfo_b = (MsgInfo *) b;
959 return (msginfo_a->msgnum - msginfo_b->msgnum);
962 static gint folder_sort_folder_list(gconstpointer a, gconstpointer b)
964 guint gint_a = GPOINTER_TO_INT(a);
965 guint gint_b = GPOINTER_TO_INT(b);
967 return (gint_a - gint_b);
970 gint folder_item_open(FolderItem *item)
972 if(((FOLDER_TYPE(item->folder) == F_IMAP) && !item->no_select) || (FOLDER_TYPE(item->folder) == F_NEWS)) {
973 folder_item_scan(item);
977 if(item->prefs->processing != NULL) {
980 buf = g_strdup_printf(_("Processing (%s)...\n"), item->path);
981 debug_print("%s\n", buf);
984 folder_item_apply_processing(item);
986 debug_print("done.\n");
992 void folder_item_close(FolderItem *item)
996 g_return_if_fail(item != NULL);
999 mlist = folder_item_get_msg_list(item);
1000 for (cur = mlist ; cur != NULL ; cur = cur->next) {
1003 msginfo = (MsgInfo *) cur->data;
1004 if (MSG_IS_NEW(msginfo->flags))
1005 procmsg_msginfo_unset_flags(msginfo, MSG_NEW, 0);
1006 procmsg_msginfo_free(msginfo);
1008 g_slist_free(mlist);
1011 folder_item_write_cache(item);
1013 folder_item_update(item, F_ITEM_UPDATE_MSGCNT);
1016 gint folder_item_scan(FolderItem *item)
1019 GSList *folder_list = NULL, *cache_list = NULL;
1020 GSList *folder_list_cur, *cache_list_cur, *new_list = NULL;
1021 GSList *exists_list = NULL, *elem;
1022 GSList *newmsg_list = NULL;
1023 guint newcnt = 0, unreadcnt = 0, totalcnt = 0, unreadmarkedcnt = 0;
1024 guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
1025 gboolean update_flags = 0;
1027 g_return_val_if_fail(item != NULL, -1);
1028 if (item->path == NULL) return -1;
1030 folder = item->folder;
1032 g_return_val_if_fail(folder != NULL, -1);
1033 g_return_val_if_fail(folder->class->get_num_list != NULL, -1);
1035 debug_print("Scanning folder %s for cache changes.\n", item->path);
1037 /* Get list of messages for folder and cache */
1038 if (folder->class->get_num_list(item->folder, item, &folder_list) < 0) {
1039 debug_print("Error fetching list of message numbers\n");
1043 if (!folder->class->check_msgnum_validity ||
1044 folder->class->check_msgnum_validity(folder, item)) {
1046 folder_item_read_cache(item);
1047 cache_list = msgcache_get_msg_list(item->cache);
1050 msgcache_destroy(item->cache);
1051 item->cache = msgcache_new();
1055 /* Sort both lists */
1056 cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
1057 folder_list = g_slist_sort(folder_list, folder_sort_folder_list);
1059 cache_list_cur = cache_list;
1060 folder_list_cur = folder_list;
1062 if (cache_list_cur != NULL) {
1063 GSList *cache_list_last;
1065 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1066 cache_list_last = g_slist_last(cache_list);
1067 cache_max_num = ((MsgInfo *)cache_list_last->data)->msgnum;
1069 cache_cur_num = G_MAXINT;
1073 if (folder_list_cur != NULL) {
1074 GSList *folder_list_last;
1076 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1077 folder_list_last = g_slist_last(folder_list);
1078 folder_max_num = GPOINTER_TO_INT(folder_list_last->data);
1080 folder_cur_num = G_MAXINT;
1084 while ((cache_cur_num != G_MAXINT) || (folder_cur_num != G_MAXINT)) {
1086 * Message only exists in the folder
1087 * Remember message for fetching
1089 if (folder_cur_num < cache_cur_num) {
1090 gboolean add = FALSE;
1092 switch(FOLDER_TYPE(folder)) {
1094 if (folder_cur_num < cache_max_num)
1097 if (folder->account->max_articles == 0) {
1101 if (folder_max_num <= folder->account->max_articles) {
1103 } else if (folder_cur_num > (folder_max_num - folder->account->max_articles)) {
1113 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(folder_cur_num));
1114 debug_print("Remembered message %d for fetching\n", folder_cur_num);
1117 /* Move to next folder number */
1118 folder_list_cur = folder_list_cur->next;
1120 if (folder_list_cur != NULL)
1121 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1123 folder_cur_num = G_MAXINT;
1129 * Message only exists in the cache
1130 * Remove the message from the cache
1132 if (cache_cur_num < folder_cur_num) {
1133 msgcache_remove_msg(item->cache, cache_cur_num);
1134 debug_print("Removed message %d from cache.\n", cache_cur_num);
1136 /* Move to next cache number */
1137 cache_list_cur = cache_list_cur->next;
1139 if (cache_list_cur != NULL)
1140 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1142 cache_cur_num = G_MAXINT;
1144 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1150 * Message number exists in folder and cache!
1151 * Check if the message has been modified
1153 if (cache_cur_num == folder_cur_num) {
1156 msginfo = msgcache_get_msg(item->cache, folder_cur_num);
1157 if (folder->class->is_msg_changed && folder->class->is_msg_changed(folder, item, msginfo)) {
1158 msgcache_remove_msg(item->cache, msginfo->msgnum);
1159 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(msginfo->msgnum));
1160 procmsg_msginfo_free(msginfo);
1162 debug_print("Remembering message %d to update...\n", folder_cur_num);
1164 exists_list = g_slist_prepend(exists_list, msginfo);
1166 /* Move to next folder and cache number */
1167 cache_list_cur = cache_list_cur->next;
1168 folder_list_cur = folder_list_cur->next;
1170 if (cache_list_cur != NULL)
1171 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1173 cache_cur_num = G_MAXINT;
1175 if (folder_list_cur != NULL)
1176 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1178 folder_cur_num = G_MAXINT;
1184 for(cache_list_cur = cache_list; cache_list_cur != NULL; cache_list_cur = g_slist_next(cache_list_cur))
1185 procmsg_msginfo_free((MsgInfo *) cache_list_cur->data);
1187 g_slist_free(cache_list);
1188 g_slist_free(folder_list);
1190 if (new_list != NULL) {
1191 if (folder->class->get_msginfos) {
1192 newmsg_list = folder->class->get_msginfos(folder, item, new_list);
1193 } else if (folder->class->get_msginfo) {
1196 for (elem = new_list; elem != NULL; elem = g_slist_next(elem)) {
1200 num = GPOINTER_TO_INT(elem->data);
1201 msginfo = folder->class->get_msginfo(folder, item, num);
1202 if (msginfo != NULL) {
1203 newmsg_list = g_slist_prepend(newmsg_list, msginfo);
1204 debug_print("Added newly found message %d to cache.\n", num);
1208 g_slist_free(new_list);
1211 if (newmsg_list != NULL) {
1214 for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem))
1215 msgcache_add_msg(item->cache, elem->data);
1217 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1219 exists_list = g_slist_concat(exists_list, newmsg_list);
1222 for (elem = exists_list; elem != NULL; elem = g_slist_next(elem)) {
1225 msginfo = elem->data;
1226 if (MSG_IS_IGNORE_THREAD(msginfo->flags) && (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1227 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1228 if (!MSG_IS_IGNORE_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD)) {
1229 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1230 procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
1232 if (MSG_IS_NEW(msginfo->flags))
1234 if (MSG_IS_UNREAD(msginfo->flags))
1236 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1240 procmsg_msginfo_free(msginfo);
1242 g_slist_free(exists_list);
1245 item->unread = unreadcnt;
1246 item->total = totalcnt;
1247 item->unreadmarked = unreadmarkedcnt;
1249 update_flags |= F_ITEM_UPDATE_MSGCNT;
1251 folder_item_update(item, update_flags);
1256 static void folder_item_scan_foreach_func(gpointer key, gpointer val,
1259 folder_item_scan(FOLDER_ITEM(key));
1262 void folder_item_scan_foreach(GHashTable *table)
1264 g_hash_table_foreach(table, folder_item_scan_foreach_func, NULL);
1267 void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
1269 gint *memusage = (gint *)data;
1271 if (item->cache == NULL)
1274 *memusage += msgcache_get_memory_usage(item->cache);
1277 gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
1279 FolderItem *fa = (FolderItem *)a;
1280 FolderItem *fb = (FolderItem *)b;
1282 return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
1285 void folder_find_expired_caches(FolderItem *item, gpointer data)
1287 GSList **folder_item_list = (GSList **)data;
1288 gint difftime, expiretime;
1290 if (item->cache == NULL)
1293 if (item->opened > 0)
1296 difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
1297 expiretime = prefs_common.cache_min_keep_time * 60;
1298 debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
1299 if (difftime > expiretime) {
1300 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
1304 void folder_item_free_cache(FolderItem *item)
1306 g_return_if_fail(item != NULL);
1308 if (item->cache == NULL)
1311 if (item->opened > 0)
1314 folder_item_write_cache(item);
1315 msgcache_destroy(item->cache);
1319 void folder_clean_cache_memory()
1323 folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);
1324 debug_print("Total cache memory usage: %d\n", memusage);
1326 if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
1327 GSList *folder_item_list = NULL, *listitem;
1329 debug_print("Trying to free cache memory\n");
1331 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);
1332 listitem = folder_item_list;
1333 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
1334 FolderItem *item = (FolderItem *)(listitem->data);
1336 debug_print("Freeing cache memory for %s\n", item->path);
1337 memusage -= msgcache_get_memory_usage(item->cache);
1338 folder_item_free_cache(item);
1339 listitem = listitem->next;
1341 g_slist_free(folder_item_list);
1345 void folder_item_read_cache(FolderItem *item)
1347 gchar *cache_file, *mark_file;
1349 g_return_if_fail(item != NULL);
1351 cache_file = folder_item_get_cache_file(item);
1352 mark_file = folder_item_get_mark_file(item);
1353 item->cache = msgcache_read_cache(item, cache_file);
1355 item->cache = msgcache_new();
1356 folder_item_scan(item);
1358 msgcache_read_mark(item->cache, mark_file);
1362 folder_clean_cache_memory();
1365 void folder_item_write_cache(FolderItem *item)
1367 gchar *cache_file, *mark_file;
1368 PrefsFolderItem *prefs;
1372 if (!item || !item->path || !item->cache)
1375 id = folder_item_get_identifier(item);
1376 debug_print("Save cache for folder %s\n", id);
1379 cache_file = folder_item_get_cache_file(item);
1380 mark_file = folder_item_get_mark_file(item);
1381 if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
1382 prefs = item->prefs;
1383 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
1384 /* for cache file */
1385 filemode = prefs->folder_chmod;
1386 if (filemode & S_IRGRP) filemode |= S_IWGRP;
1387 if (filemode & S_IROTH) filemode |= S_IWOTH;
1388 chmod(cache_file, filemode);
1396 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
1401 g_return_val_if_fail(item != NULL, NULL);
1403 folder = item->folder;
1405 folder_item_read_cache(item);
1407 if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
1410 g_return_val_if_fail(folder->class->get_msginfo, NULL);
1411 if ((msginfo = folder->class->get_msginfo(folder, item, num)) != NULL) {
1412 msgcache_add_msg(item->cache, msginfo);
1419 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
1424 g_return_val_if_fail(item != NULL, NULL);
1426 folder = item->folder;
1428 folder_item_read_cache(item);
1430 if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
1436 GSList *folder_item_get_msg_list(FolderItem *item)
1438 g_return_val_if_fail(item != NULL, NULL);
1440 if (item->cache == 0)
1441 folder_item_read_cache(item);
1443 g_return_val_if_fail(item->cache != NULL, NULL);
1445 return msgcache_get_msg_list(item->cache);
1448 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
1452 g_return_val_if_fail(item != NULL, NULL);
1454 folder = item->folder;
1456 g_return_val_if_fail(folder->class->fetch_msg != NULL, NULL);
1458 return folder->class->fetch_msg(folder, item, num);
1461 static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
1463 static HeaderEntry hentry[] = {{"Message-ID:", NULL, TRUE},
1464 {NULL, NULL, FALSE}};
1468 gchar buf[BUFFSIZE];
1470 if ((fp = fopen(file, "rb")) == NULL)
1473 if ((dest->stype == F_QUEUE) || (dest->stype == F_DRAFT))
1474 while (fgets(buf, sizeof(buf), fp) != NULL)
1475 if (buf[0] == '\r' || buf[0] == '\n') break;
1477 procheader_get_header_fields(fp, hentry);
1478 if (hentry[0].body) {
1479 extract_parenthesis(hentry[0].body, '<', '>');
1480 remove_space(hentry[0].body);
1481 if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
1482 msgnum = msginfo->msgnum;
1483 procmsg_msginfo_free(msginfo);
1485 debug_print("found message as uid %d\n", msgnum);
1489 g_free(hentry[0].body);
1490 hentry[0].body = NULL;
1496 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
1497 gboolean remove_source)
1503 g_return_val_if_fail(dest != NULL, -1);
1504 g_return_val_if_fail(file != NULL, -1);
1506 folder = dest->folder;
1508 g_return_val_if_fail(folder->class->add_msg != NULL, -1);
1511 folder_item_read_cache(dest);
1513 num = folder->class->add_msg(folder, dest, file, FALSE);
1516 msginfo = folder->class->get_msginfo(folder, dest, num);
1518 if (msginfo != NULL) {
1519 if (MSG_IS_NEW(msginfo->flags))
1521 if (MSG_IS_UNREAD(msginfo->flags))
1523 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1524 dest->unreadmarked++;
1525 if (procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD))
1526 procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
1529 msgcache_add_msg(dest->cache, msginfo);
1530 procmsg_msginfo_free(msginfo);
1532 folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1535 dest->last_num = num;
1536 } else if (num == 0) {
1537 folder_item_scan(dest);
1538 num = folder_item_get_msg_num_by_file(dest, file);
1541 if (num >= 0 && remove_source) {
1542 if (unlink(file) < 0)
1543 FILE_OP_ERROR(file, "unlink");
1550 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1555 g_return_val_if_fail(dest != NULL, -1);
1556 g_return_val_if_fail(msginfo != NULL, -1);
1558 folder = dest->folder;
1559 if (dest->last_num < 0) folder->scan(folder, dest);
1561 num = folder->move_msg(folder, dest, msginfo);
1562 if (num > 0) dest->last_num = num;
1568 FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest)
1572 FolderItem *new_item;
1573 FolderItem *next_item;
1576 gchar *old_id, *new_id;
1578 mlist = folder_item_get_msg_list(src);
1581 debug_print("Moving %s to %s\n", src->path, dest->path);
1582 new_item = folder_create_folder(dest, g_basename(src->path));
1583 if (new_item == NULL) {
1584 printf("Can't create folder\n");
1588 if (new_item->folder == NULL)
1589 new_item->folder = dest->folder;
1592 for (cur = mlist ; cur != NULL ; cur = cur->next) {
1596 log_message(_("Moving %s to %s (%d%%)...\n"), src->name,
1598 100*cnt/g_slist_length(mlist));
1599 msginfo = (MsgInfo *) cur->data;
1600 folder_item_move_msg(new_item, msginfo);
1602 procmsg_msginfo_free(msginfo);
1606 prefs_folder_item_copy_prefs(src, new_item);
1607 new_item->collapsed = src->collapsed;
1608 new_item->thread_collapsed = src->thread_collapsed;
1609 new_item->threaded = src->threaded;
1610 new_item->ret_rcpt = src->ret_rcpt;
1611 new_item->hide_read_msgs = src->hide_read_msgs;
1612 new_item->sort_key = src->sort_key;
1613 new_item->sort_type = src->sort_type;
1615 prefs_matcher_write_config();
1618 srcnode = src->folder->node;
1619 srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
1620 srcnode = srcnode->children;
1621 while (srcnode != NULL) {
1622 if (srcnode && srcnode->data) {
1623 next_item = (FolderItem*) srcnode->data;
1624 srcnode = srcnode->next;
1625 if (folder_item_move_recursive(next_item, new_item) == NULL)
1629 old_id = folder_item_get_identifier(src);
1630 new_id = folder_item_get_identifier(new_item);
1631 debug_print("updating rules : %s => %s\n", old_id, new_id);
1633 src->folder->class->remove_folder(src->folder, src);
1634 folder_write_list();
1636 if (old_id != NULL && new_id != NULL)
1637 prefs_filtering_rename_path(old_id, new_id);
1644 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item)
1646 FolderItem *tmp = dest->parent;
1647 gchar * src_identifier, * dst_identifier;
1648 gchar * phys_srcpath, * phys_dstpath;
1653 return F_MOVE_FAILED_DEST_IS_CHILD;
1660 src_identifier = folder_item_get_identifier(src);
1661 dst_identifier = folder_item_get_identifier(dest);
1663 if(dst_identifier == NULL && dest->folder && dest->parent == NULL) {
1664 /* dest can be a root folder */
1665 dst_identifier = folder_get_identifier(dest->folder);
1667 if (src_identifier == NULL || dst_identifier == NULL) {
1668 debug_print("Can't get identifiers\n");
1669 return F_MOVE_FAILED;
1672 if (src->folder != dest->folder) {
1673 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
1676 phys_srcpath = folder_item_get_path(src);
1677 phys_dstpath = g_strconcat(folder_item_get_path(dest),G_DIR_SEPARATOR_S,g_basename(phys_srcpath),NULL);
1679 if (src->parent == dest || src == dest) {
1680 g_free(src_identifier);
1681 g_free(dst_identifier);
1682 g_free(phys_srcpath);
1683 g_free(phys_dstpath);
1684 return F_MOVE_FAILED_DEST_IS_PARENT;
1686 debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
1687 if ((tmp = folder_item_move_recursive(src, dest)) == NULL) {
1688 return F_MOVE_FAILED;
1692 src_node = g_node_find(src->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, src);
1694 g_node_destroy(src_node);
1696 debug_print("can't remove node: it's null!\n");
1697 /* not to much worry if remove fails, move has been done */
1699 g_free(src_identifier);
1700 g_free(dst_identifier);
1701 g_free(phys_srcpath);
1702 g_free(phys_dstpath);
1709 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1711 GSList *list = NULL;
1714 list = g_slist_append(list, msginfo);
1715 ret = folder_item_move_msgs_with_dest(dest, list);
1722 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1727 g_return_val_if_fail(dest != NULL, -1);
1728 g_return_val_if_fail(msglist != NULL, -1);
1730 folder = dest->folder;
1731 if (dest->last_num < 0) folder->scan(folder, dest);
1733 num = folder->move_msgs_with_dest(folder, dest, msglist);
1734 if (num > 0) dest->last_num = num;
1735 else dest->op_count = 0;
1741 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1745 GSList *newmsgnums = NULL;
1747 gint num, lastnum = -1;
1748 gboolean folderscan = FALSE;
1750 g_return_val_if_fail(dest != NULL, -1);
1751 g_return_val_if_fail(msglist != NULL, -1);
1753 folder = dest->folder;
1755 g_return_val_if_fail(folder->class->copy_msg != NULL, -1);
1756 g_return_val_if_fail(folder->class->remove_msg != NULL, -1);
1759 * Copy messages to destination folder and
1760 * store new message numbers in newmsgnums
1763 for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
1764 MsgInfo * msginfo = (MsgInfo *) l->data;
1766 if (!item && msginfo->folder != NULL)
1767 item = msginfo->folder;
1769 num = folder->class->copy_msg(folder, dest, msginfo);
1770 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1773 /* Read cache for dest folder */
1774 if (!dest->cache) folder_item_read_cache(dest);
1777 * Fetch new MsgInfos for new messages in dest folder,
1778 * add them to the msgcache and update folder message counts
1781 for (l = msglist; l != NULL; l = g_slist_next(l)) {
1782 MsgInfo *msginfo = (MsgInfo *) l->data;
1784 num = GPOINTER_TO_INT(l2->data);
1785 l2 = g_slist_next(l2);
1788 MsgInfo *newmsginfo;
1794 folder_item_scan(dest);
1797 file = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
1798 num = folder_item_get_msg_num_by_file(dest, file);
1809 newmsginfo = folder->class->get_msginfo(folder, dest, num);
1811 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1812 if (dest->stype == F_OUTBOX ||
1813 dest->stype == F_QUEUE ||
1814 dest->stype == F_DRAFT ||
1815 dest->stype == F_TRASH)
1816 MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1817 MSG_NEW|MSG_UNREAD|MSG_DELETED);
1818 msgcache_add_msg(dest->cache, newmsginfo);
1820 if (MSG_IS_NEW(newmsginfo->flags))
1822 if (MSG_IS_UNREAD(newmsginfo->flags))
1824 if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
1825 dest->unreadmarked++;
1826 if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
1827 procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
1829 folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1831 procmsg_msginfo_free(newmsginfo);
1834 newmsginfo = msgcache_get_msg(dest->cache, num);
1835 procmsg_msginfo_unset_flags(newmsginfo, ~0, ~0);
1836 procmsg_msginfo_set_flags(newmsginfo, msginfo->flags.perm_flags, 0);
1842 * Remove source messages from their folders if
1843 * copying was successfull and update folder
1847 for (l = msglist; l != NULL; l = g_slist_next(l)) {
1848 MsgInfo *msginfo = (MsgInfo *) l->data;
1850 num = GPOINTER_TO_INT(l2->data);
1851 l2 = g_slist_next(l2);
1854 item->folder->class->remove_msg(item->folder,
1858 folder_item_read_cache(item);
1859 msgcache_remove_msg(item->cache, msginfo->msgnum);
1861 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1862 msginfo->folder->new--;
1863 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1864 msginfo->folder->unread--;
1865 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1866 msginfo->folder->unreadmarked--;
1867 msginfo->folder->total--;
1868 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1873 if (folder->class->finished_copy)
1874 folder->class->finished_copy(folder, dest);
1876 g_slist_free(newmsgnums);
1881 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1886 g_return_val_if_fail(dest != NULL, -1);
1887 g_return_val_if_fail(msginfo != NULL, -1);
1889 folder = dest->folder;
1890 if (dest->last_num < 0) folder->scan(folder, dest);
1892 num = folder->copy_msg(folder, dest, msginfo);
1893 if (num > 0) dest->last_num = num;
1899 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1901 GSList *list = NULL;
1904 list = g_slist_append(list, msginfo);
1905 ret = folder_item_copy_msgs_with_dest(dest, list);
1912 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1917 g_return_val_if_fail(dest != NULL, -1);
1918 g_return_val_if_fail(msglist != NULL, -1);
1920 folder = dest->folder;
1921 if (dest->last_num < 0) folder->scan(folder, dest);
1923 num = folder->copy_msgs_with_dest(folder, dest, msglist);
1924 if (num > 0) dest->last_num = num;
1925 else dest->op_count = 0;
1931 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1934 gint num, lastnum = -1;
1935 GSList *newmsgnums = NULL;
1937 gboolean folderscan = FALSE;
1939 g_return_val_if_fail(dest != NULL, -1);
1940 g_return_val_if_fail(msglist != NULL, -1);
1942 folder = dest->folder;
1944 g_return_val_if_fail(folder->class->copy_msg != NULL, -1);
1947 * Copy messages to destination folder and
1948 * store new message numbers in newmsgnums
1950 for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
1951 MsgInfo * msginfo = (MsgInfo *) l->data;
1953 num = folder->class->copy_msg(folder, dest, msginfo);
1954 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1957 /* Read cache for dest folder */
1958 if (!dest->cache) folder_item_read_cache(dest);
1961 * Fetch new MsgInfos for new messages in dest folder,
1962 * add them to the msgcache and update folder message counts
1965 for (l = msglist; l != NULL; l = g_slist_next(l)) {
1966 MsgInfo *msginfo = (MsgInfo *) l->data;
1968 num = GPOINTER_TO_INT(l2->data);
1969 l2 = g_slist_next(l2);
1972 MsgInfo *newmsginfo;
1978 folder_item_scan(dest);
1981 file = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
1982 num = folder_item_get_msg_num_by_file(dest, file);
1993 newmsginfo = folder->class->get_msginfo(folder, dest, num);
1995 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1996 if (dest->stype == F_OUTBOX ||
1997 dest->stype == F_QUEUE ||
1998 dest->stype == F_DRAFT ||
1999 dest->stype == F_TRASH)
2000 MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
2001 MSG_NEW|MSG_UNREAD|MSG_DELETED);
2002 msgcache_add_msg(dest->cache, newmsginfo);
2004 if (MSG_IS_NEW(newmsginfo->flags))
2006 if (MSG_IS_UNREAD(newmsginfo->flags))
2008 if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
2009 dest->unreadmarked++;
2010 if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
2011 procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
2013 folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2015 procmsg_msginfo_free(newmsginfo);
2018 newmsginfo = msgcache_get_msg(dest->cache, num);
2019 procmsg_msginfo_unset_flags(newmsginfo, ~0, ~0);
2020 procmsg_msginfo_set_flags(newmsginfo, msginfo->flags.perm_flags, 0);
2025 if (folder->class->finished_copy)
2026 folder->class->finished_copy(folder, dest);
2028 g_slist_free(newmsgnums);
2032 gint folder_item_remove_msg(FolderItem *item, gint num)
2038 g_return_val_if_fail(item != NULL, -1);
2040 folder = item->folder;
2041 if (!item->cache) folder_item_read_cache(item);
2043 ret = folder->class->remove_msg(folder, item, num);
2045 msginfo = msgcache_get_msg(item->cache, num);
2046 if (msginfo != NULL) {
2047 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2049 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2051 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2052 item->unreadmarked--;
2053 procmsg_msginfo_free(msginfo);
2054 msgcache_remove_msg(item->cache, num);
2057 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2062 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
2067 g_return_val_if_fail(item != NULL, -1);
2068 folder = item->folder;
2069 g_return_val_if_fail(folder != NULL, -1);
2071 if (!item->cache) folder_item_read_cache(item);
2073 if (folder->class->remove_msgs) {
2074 ret = folder->class->remove_msgs(folder, item, msglist);
2076 folder_item_scan(item);
2080 while (msglist != NULL) {
2081 MsgInfo *msginfo = (MsgInfo *)msglist->data;
2083 ret = folder_item_remove_msg(item, msginfo->msgnum);
2084 if (ret != 0) break;
2085 msgcache_remove_msg(item->cache, msginfo->msgnum);
2086 msglist = msglist->next;
2092 gint folder_item_remove_all_msg(FolderItem *item)
2097 g_return_val_if_fail(item != NULL, -1);
2099 folder = item->folder;
2101 g_return_val_if_fail(folder->class->remove_all_msg != NULL, -1);
2103 result = folder->class->remove_all_msg(folder, item);
2106 if (folder->class->finished_remove)
2107 folder->class->finished_remove(folder, item);
2109 folder_item_free_cache(item);
2110 item->cache = msgcache_new();
2114 item->unreadmarked = 0;
2116 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2122 void folder_item_change_msg_flags(FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
2124 g_return_if_fail(item != NULL);
2125 g_return_if_fail(msginfo != NULL);
2127 if (item->folder->class->change_flags != NULL) {
2128 item->folder->class->change_flags(item->folder, item, msginfo, newflags);
2130 msginfo->flags.perm_flags = newflags;
2134 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
2138 g_return_val_if_fail(item != NULL, FALSE);
2140 folder = item->folder;
2142 g_return_val_if_fail(folder->class->is_msg_changed != NULL, -1);
2144 return folder->class->is_msg_changed(folder, item, msginfo);
2147 gchar *folder_item_get_cache_file(FolderItem *item)
2152 g_return_val_if_fail(item != NULL, NULL);
2153 g_return_val_if_fail(item->path != NULL, NULL);
2155 path = folder_item_get_path(item);
2156 g_return_val_if_fail(path != NULL, NULL);
2157 if (!is_dir_exist(path))
2158 make_dir_hier(path);
2159 file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
2165 gchar *folder_item_get_mark_file(FolderItem *item)
2170 g_return_val_if_fail(item != NULL, NULL);
2171 g_return_val_if_fail(item->path != NULL, NULL);
2173 path = folder_item_get_path(item);
2174 g_return_val_if_fail(path != NULL, NULL);
2175 if (!is_dir_exist(path))
2176 make_dir_hier(path);
2177 file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
2183 static gboolean folder_build_tree(GNode *node, gpointer data)
2185 Folder *folder = FOLDER(data);
2189 SpecialFolderItemType stype = F_NORMAL;
2190 const gchar *name = NULL;
2191 const gchar *path = NULL;
2192 PrefsAccount *account = NULL;
2193 gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE,
2194 threaded = TRUE, apply_sub = FALSE;
2195 gboolean ret_rcpt = FALSE, hidereadmsgs = FALSE,
2196 thread_collapsed = FALSE; /* CLAWS */
2197 FolderSortKey sort_key = SORT_BY_NONE;
2198 FolderSortType sort_type = SORT_ASCENDING;
2199 gint new = 0, unread = 0, total = 0, unreadmarked = 0;
2202 g_return_val_if_fail(node->data != NULL, FALSE);
2203 if (!node->parent) return FALSE;
2205 xmlnode = node->data;
2206 if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
2207 g_warning("tag name != \"folderitem\"\n");
2211 list = xmlnode->tag->attr;
2212 for (; list != NULL; list = list->next) {
2213 XMLAttr *attr = list->data;
2215 if (!attr || !attr->name || !attr->value) continue;
2216 if (!strcmp(attr->name, "type")) {
2217 if (!strcasecmp(attr->value, "normal"))
2219 else if (!strcasecmp(attr->value, "inbox"))
2221 else if (!strcasecmp(attr->value, "outbox"))
2223 else if (!strcasecmp(attr->value, "draft"))
2225 else if (!strcasecmp(attr->value, "queue"))
2227 else if (!strcasecmp(attr->value, "trash"))
2229 } else if (!strcmp(attr->name, "name"))
2231 else if (!strcmp(attr->name, "path"))
2233 else if (!strcmp(attr->name, "mtime"))
2234 mtime = strtoul(attr->value, NULL, 10);
2235 else if (!strcmp(attr->name, "new"))
2236 new = atoi(attr->value);
2237 else if (!strcmp(attr->name, "unread"))
2238 unread = atoi(attr->value);
2239 else if (!strcmp(attr->name, "unreadmarked"))
2240 unreadmarked = atoi(attr->value);
2241 else if (!strcmp(attr->name, "total"))
2242 total = atoi(attr->value);
2243 else if (!strcmp(attr->name, "no_sub"))
2244 no_sub = *attr->value == '1' ? TRUE : FALSE;
2245 else if (!strcmp(attr->name, "no_select"))
2246 no_select = *attr->value == '1' ? TRUE : FALSE;
2247 else if (!strcmp(attr->name, "collapsed"))
2248 collapsed = *attr->value == '1' ? TRUE : FALSE;
2249 else if (!strcmp(attr->name, "thread_collapsed"))
2250 thread_collapsed = *attr->value == '1' ? TRUE : FALSE;
2251 else if (!strcmp(attr->name, "threaded"))
2252 threaded = *attr->value == '1' ? TRUE : FALSE;
2253 else if (!strcmp(attr->name, "hidereadmsgs"))
2254 hidereadmsgs = *attr->value == '1' ? TRUE : FALSE;
2255 else if (!strcmp(attr->name, "reqretrcpt"))
2256 ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
2257 else if (!strcmp(attr->name, "sort_key")) {
2258 if (!strcmp(attr->value, "none"))
2259 sort_key = SORT_BY_NONE;
2260 else if (!strcmp(attr->value, "number"))
2261 sort_key = SORT_BY_NUMBER;
2262 else if (!strcmp(attr->value, "size"))
2263 sort_key = SORT_BY_SIZE;
2264 else if (!strcmp(attr->value, "date"))
2265 sort_key = SORT_BY_DATE;
2266 else if (!strcmp(attr->value, "from"))
2267 sort_key = SORT_BY_FROM;
2268 else if (!strcmp(attr->value, "subject"))
2269 sort_key = SORT_BY_SUBJECT;
2270 else if (!strcmp(attr->value, "score"))
2271 sort_key = SORT_BY_SCORE;
2272 else if (!strcmp(attr->value, "label"))
2273 sort_key = SORT_BY_LABEL;
2274 else if (!strcmp(attr->value, "mark"))
2275 sort_key = SORT_BY_MARK;
2276 else if (!strcmp(attr->value, "unread"))
2277 sort_key = SORT_BY_STATUS;
2278 else if (!strcmp(attr->value, "mime"))
2279 sort_key = SORT_BY_MIME;
2280 else if (!strcmp(attr->value, "to"))
2281 sort_key = SORT_BY_TO;
2282 else if (!strcmp(attr->value, "locked"))
2283 sort_key = SORT_BY_LOCKED;
2284 } else if (!strcmp(attr->name, "sort_type")) {
2285 if (!strcmp(attr->value, "ascending"))
2286 sort_type = SORT_ASCENDING;
2288 sort_type = SORT_DESCENDING;
2289 } else if (!strcmp(attr->name, "account_id")) {
2290 account = account_find_from_id(atoi(attr->value));
2291 if (!account) g_warning("account_id: %s not found\n",
2293 } else if (!strcmp(attr->name, "apply_sub"))
2294 apply_sub = *attr->value == '1' ? TRUE : FALSE;
2297 item = folder_item_new(folder, name, path);
2298 item->stype = stype;
2299 item->mtime = mtime;
2301 item->unread = unread;
2302 item->unreadmarked = unreadmarked;
2303 item->total = total;
2304 item->no_sub = no_sub;
2305 item->no_select = no_select;
2306 item->collapsed = collapsed;
2307 item->thread_collapsed = thread_collapsed;
2308 item->threaded = threaded;
2309 item->hide_read_msgs = hidereadmsgs;
2310 item->ret_rcpt = ret_rcpt;
2311 item->sort_key = sort_key;
2312 item->sort_type = sort_type;
2313 item->parent = FOLDER_ITEM(node->parent->data);
2314 item->folder = folder;
2316 case F_INBOX: folder->inbox = item; break;
2317 case F_OUTBOX: folder->outbox = item; break;
2318 case F_DRAFT: folder->draft = item; break;
2319 case F_QUEUE: folder->queue = item; break;
2320 case F_TRASH: folder->trash = item; break;
2323 item->account = account;
2324 item->apply_sub = apply_sub;
2325 prefs_folder_item_read_config(item);
2328 xml_free_node(xmlnode);
2333 static gboolean folder_read_folder_func(GNode *node, gpointer data)
2338 FolderClass *class = NULL;
2339 const gchar *name = NULL;
2340 const gchar *path = NULL;
2341 PrefsAccount *account = NULL;
2342 gboolean collapsed = FALSE, threaded = TRUE, apply_sub = FALSE;
2343 gboolean ret_rcpt = FALSE, thread_collapsed = FALSE; /* CLAWS */
2345 if (g_node_depth(node) != 2) return FALSE;
2346 g_return_val_if_fail(node->data != NULL, FALSE);
2348 xmlnode = node->data;
2349 if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
2350 g_warning("tag name != \"folder\"\n");
2353 g_node_unlink(node);
2354 list = xmlnode->tag->attr;
2355 for (; list != NULL; list = list->next) {
2356 XMLAttr *attr = list->data;
2358 if (!attr || !attr->name || !attr->value) continue;
2359 if (!strcmp(attr->name, "type"))
2360 class = folder_get_class_from_string(attr->value);
2361 else if (!strcmp(attr->name, "name"))
2363 else if (!strcmp(attr->name, "path"))
2365 else if (!strcmp(attr->name, "collapsed"))
2366 collapsed = *attr->value == '1' ? TRUE : FALSE;
2367 else if (!strcmp(attr->name, "thread_collapsed"))
2368 thread_collapsed = *attr->value == '1' ? TRUE : FALSE;
2369 else if (!strcmp(attr->name, "threaded"))
2370 threaded = *attr->value == '1' ? TRUE : FALSE;
2371 else if (!strcmp(attr->name, "account_id")) {
2372 account = account_find_from_id(atoi(attr->value));
2373 if (!account) g_warning("account_id: %s not found\n",
2375 } else if (!strcmp(attr->name, "apply_sub"))
2376 apply_sub = *attr->value == '1' ? TRUE : FALSE;
2377 else if (!strcmp(attr->name, "reqretrcpt"))
2378 ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
2381 folder = folder_new(class, name, path);
2382 g_return_val_if_fail(folder != NULL, FALSE);
2383 folder->account = account;
2384 if (account != NULL)
2385 account->folder = REMOTE_FOLDER(folder);
2386 node->data = folder->node->data;
2387 g_node_destroy(folder->node);
2388 folder->node = node;
2390 FOLDER_ITEM(node->data)->collapsed = collapsed;
2391 FOLDER_ITEM(node->data)->thread_collapsed = thread_collapsed;
2392 FOLDER_ITEM(node->data)->threaded = threaded;
2393 FOLDER_ITEM(node->data)->account = account;
2394 FOLDER_ITEM(node->data)->apply_sub = apply_sub;
2395 FOLDER_ITEM(node->data)->ret_rcpt = ret_rcpt;
2397 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2398 folder_build_tree, folder);
2403 static gchar *folder_get_list_path(void)
2405 static gchar *filename = NULL;
2408 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2414 #define PUT_ESCAPE_STR(fp, attr, str) \
2416 fputs(" " attr "=\"", fp); \
2417 xml_file_put_escape_str(fp, str); \
2421 static void folder_write_list_recursive(GNode *node, gpointer data)
2423 FILE *fp = (FILE *)data;
2426 static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
2427 "draft", "queue", "trash"};
2428 static gchar *sort_key_str[] = {"none", "number", "size", "date",
2429 "from", "subject", "score", "label",
2430 "mark", "unread", "mime", "to",
2432 g_return_if_fail(node != NULL);
2433 g_return_if_fail(fp != NULL);
2435 item = FOLDER_ITEM(node->data);
2436 g_return_if_fail(item != NULL);
2438 depth = g_node_depth(node);
2439 for (i = 0; i < depth; i++)
2442 Folder *folder = item->folder;
2444 fprintf(fp, "<folder type=\"%s\"", folder->class->idstr);
2446 PUT_ESCAPE_STR(fp, "name", folder->name);
2447 if (FOLDER_TYPE(folder) == F_MH || FOLDER_TYPE(folder) == F_MBOX)
2448 PUT_ESCAPE_STR(fp, "path",
2449 LOCAL_FOLDER(folder)->rootpath);
2450 if (item->collapsed && node->children)
2451 fputs(" collapsed=\"1\"", fp);
2452 if (folder->account)
2453 fprintf(fp, " account_id=\"%d\"",
2454 folder->account->account_id);
2455 if (item->apply_sub)
2456 fputs(" apply_sub=\"1\"", fp);
2458 fputs(" reqretrcpt=\"1\"", fp);
2460 fprintf(fp, "<folderitem type=\"%s\"",
2461 folder_item_stype_str[item->stype]);
2463 PUT_ESCAPE_STR(fp, "name", item->name);
2465 PUT_ESCAPE_STR(fp, "path", item->path);
2468 fputs(" no_sub=\"1\"", fp);
2469 if (item->no_select)
2470 fputs(" no_select=\"1\"", fp);
2471 if (item->collapsed && node->children)
2472 fputs(" collapsed=\"1\"", fp);
2474 fputs(" collapsed=\"0\"", fp);
2475 if (item->thread_collapsed)
2476 fputs(" thread_collapsed=\"1\"", fp);
2478 fputs(" thread_collapsed=\"0\"", fp);
2480 fputs(" threaded=\"1\"", fp);
2482 fputs(" threaded=\"0\"", fp);
2483 if (item->hide_read_msgs)
2484 fputs(" hidereadmsgs=\"1\"", fp);
2486 fputs(" hidereadmsgs=\"0\"", fp);
2488 fputs(" reqretrcpt=\"1\"", fp);
2490 if (item->sort_key != SORT_BY_NONE) {
2491 fprintf(fp, " sort_key=\"%s\"",
2492 sort_key_str[item->sort_key]);
2493 if (item->sort_type == SORT_ASCENDING)
2494 fprintf(fp, " sort_type=\"ascending\"");
2496 fprintf(fp, " sort_type=\"descending\"");
2500 " mtime=\"%lu\" new=\"%d\" unread=\"%d\" unreadmarked=\"%d\" total=\"%d\"",
2501 item->mtime, item->new, item->unread, item->unreadmarked, item->total);
2504 fprintf(fp, " account_id=\"%d\"",
2505 item->account->account_id);
2506 if (item->apply_sub)
2507 fputs(" apply_sub=\"1\"", fp);
2510 if (node->children) {
2514 child = node->children;
2520 folder_write_list_recursive(cur, data);
2523 for (i = 0; i < depth; i++)
2525 fprintf(fp, "</%s>\n", depth == 1 ? "folder" : "folderitem");
2530 static void folder_update_op_count_rec(GNode *node)
2532 FolderItem *fitem = FOLDER_ITEM(node->data);
2534 if (g_node_depth(node) > 0) {
2535 if (fitem->op_count > 0) {
2536 fitem->op_count = 0;
2537 folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
2539 if (node->children) {
2542 child = node->children;
2548 folder_update_op_count_rec(cur);
2554 void folder_update_op_count() {
2558 for (cur = folder_list; cur != NULL; cur = cur->next) {
2560 folder_update_op_count_rec(folder->node);
2564 typedef struct _type_str {
2571 static gchar * folder_item_get_tree_identifier(FolderItem * item)
2573 if (item->parent != NULL) {
2577 path = folder_item_get_tree_identifier(item->parent);
2581 id = g_strconcat(path, "/", item->name, NULL);
2587 return g_strconcat("/", item->name, NULL);
2592 /* CLAWS: temporary local folder for filtering */
2593 static Folder *processing_folder;
2594 static FolderItem *processing_folder_item;
2596 static void folder_create_processing_folder(void)
2598 #define PROCESSING_FOLDER ".processing"
2602 tmpparent = folder_get_default_folder();
2603 g_assert(tmpparent);
2604 debug_print("tmpparentroot %s\n", LOCAL_FOLDER(tmpparent)->rootpath);
2605 if (LOCAL_FOLDER(tmpparent)->rootpath[0] == '/')
2606 tmpname = g_strconcat(LOCAL_FOLDER(tmpparent)->rootpath,
2607 G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2610 tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2611 LOCAL_FOLDER(tmpparent)->rootpath,
2612 G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2615 processing_folder = folder_new(mh_get_class(), "PROCESSING", LOCAL_FOLDER(tmpparent)->rootpath);
2616 g_assert(processing_folder);
2618 if (!is_dir_exist(tmpname)) {
2619 debug_print("*TMP* creating %s\n", tmpname);
2620 processing_folder_item = processing_folder->class->create_folder(processing_folder,
2621 processing_folder->node->data,
2623 g_assert(processing_folder_item);
2626 debug_print("*TMP* already created\n");
2627 processing_folder_item = folder_item_new(processing_folder, ".processing", ".processing");
2628 g_assert(processing_folder_item);
2629 folder_item_append(processing_folder->node->data, processing_folder_item);
2634 FolderItem *folder_get_default_processing(void)
2636 if (!processing_folder_item) {
2637 folder_create_processing_folder();
2639 return processing_folder_item;
2642 /* folder_persist_prefs_new() - return hash table with persistent
2643 * settings (and folder name as key).
2644 * (note that in claws other options are in the PREFS_FOLDER_ITEM_RC
2645 * file, so those don't need to be included in PersistPref yet)
2647 GHashTable *folder_persist_prefs_new(Folder *folder)
2649 GHashTable *pptable;
2651 g_return_val_if_fail(folder, NULL);
2652 pptable = g_hash_table_new(g_str_hash, g_str_equal);
2653 folder_get_persist_prefs_recursive(folder->node, pptable);
2657 void folder_persist_prefs_free(GHashTable *pptable)
2659 g_return_if_fail(pptable);
2660 g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
2661 g_hash_table_destroy(pptable);
2664 const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
2666 if (pptable == NULL || name == NULL) return NULL;
2667 return g_hash_table_lookup(pptable, name);
2670 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
2672 const PersistPrefs *pp;
2673 gchar *id = folder_item_get_identifier(item);
2675 pp = folder_get_persist_prefs(pptable, id);
2680 /* CLAWS: since not all folder properties have been migrated to
2681 * folderlist.xml, we need to call the old stuff first before
2682 * setting things that apply both to Main and Claws. */
2683 prefs_folder_item_read_config(item);
2685 item->collapsed = pp->collapsed;
2686 item->thread_collapsed = pp->thread_collapsed;
2687 item->threaded = pp->threaded;
2688 item->ret_rcpt = pp->ret_rcpt;
2689 item->hide_read_msgs = pp->hide_read_msgs;
2690 item->sort_key = pp->sort_key;
2691 item->sort_type = pp->sort_type;
2694 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
2696 FolderItem *item = FOLDER_ITEM(node->data);
2701 g_return_if_fail(node != NULL);
2702 g_return_if_fail(item != NULL);
2704 /* NOTE: item->path == NULL means top level folder; not interesting
2705 * to store preferences of that one. */
2707 id = folder_item_get_identifier(item);
2708 pp = g_new0(PersistPrefs, 1);
2709 g_return_if_fail(pp != NULL);
2710 pp->collapsed = item->collapsed;
2711 pp->thread_collapsed = item->thread_collapsed;
2712 pp->threaded = item->threaded;
2713 pp->ret_rcpt = item->ret_rcpt;
2714 pp->hide_read_msgs = item->hide_read_msgs;
2715 pp->sort_key = item->sort_key;
2716 pp->sort_type = item->sort_type;
2717 g_hash_table_insert(pptable, id, pp);
2720 if (node->children) {
2721 child = node->children;
2725 folder_get_persist_prefs_recursive(cur, pptable);
2730 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
2739 void folder_item_apply_processing(FolderItem *item)
2741 GSList *processing_list;
2742 GSList *mlist, *cur;
2744 g_return_if_fail(item != NULL);
2746 processing_list = item->prefs->processing;
2747 if (processing_list == NULL)
2750 folder_item_update_freeze();
2752 mlist = folder_item_get_msg_list(item);
2753 for (cur = mlist ; cur != NULL ; cur = cur->next) {
2756 msginfo = (MsgInfo *) cur->data;
2757 filter_message_by_msginfo(processing_list, msginfo);
2758 procmsg_msginfo_free(msginfo);
2760 g_slist_free(mlist);
2762 folder_item_update_thaw();
2766 * functions for handling FolderItem content changes
2768 static gint folder_item_update_freeze_cnt = 0;
2771 * Notify the folder system about changes to a folder. If the
2772 * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
2773 * be invoked, otherwise the changes will be remebered until
2774 * the folder system is thawed.
2776 * \param item The FolderItem that was changed
2777 * \param update_flags Type of changed that was made
2779 void folder_item_update(FolderItem *item, FolderItemUpdateFlags update_flags)
2781 if (folder_item_update_freeze_cnt == 0) {
2782 FolderItemUpdateData source;
2785 source.update_flags = update_flags;
2786 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
2788 item->update_flags |= update_flags;
2792 void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
2794 GNode *node = item->folder->node;
2796 node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
2797 node = node->children;
2799 folder_item_update(item, update_flags);
2800 while (node != NULL) {
2801 if (node && node->data) {
2802 FolderItem *next_item = (FolderItem*) node->data;
2804 folder_item_update(next_item, update_flags);
2810 void folder_item_update_freeze()
2812 folder_item_update_freeze_cnt++;
2815 static void folder_item_update_func(FolderItem *item, gpointer data)
2817 FolderItemUpdateData source;
2819 if (item->update_flags) {
2821 source.update_flags = item->update_flags;
2822 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
2823 item->update_flags = 0;
2827 void folder_item_update_thaw()
2829 if (folder_item_update_freeze_cnt > 0)
2830 folder_item_update_freeze_cnt--;
2831 if (folder_item_update_freeze_cnt == 0) {
2832 /* Update all folders */
2833 folder_func_to_all_folders(folder_item_update_func, NULL);
2837 #undef PUT_ESCAPE_STR