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 ((item->stype == F_OUTBOX ||
1233 item->stype == F_QUEUE ||
1234 item->stype == F_DRAFT ||
1235 item->stype == F_TRASH) &&
1236 (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1237 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1238 if (MSG_IS_NEW(msginfo->flags))
1240 if (MSG_IS_UNREAD(msginfo->flags))
1242 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1246 procmsg_msginfo_free(msginfo);
1248 g_slist_free(exists_list);
1251 item->unread = unreadcnt;
1252 item->total = totalcnt;
1253 item->unreadmarked = unreadmarkedcnt;
1255 update_flags |= F_ITEM_UPDATE_MSGCNT;
1257 folder_item_update(item, update_flags);
1262 static void folder_item_scan_foreach_func(gpointer key, gpointer val,
1265 folder_item_scan(FOLDER_ITEM(key));
1268 void folder_item_scan_foreach(GHashTable *table)
1270 g_hash_table_foreach(table, folder_item_scan_foreach_func, NULL);
1273 void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
1275 gint *memusage = (gint *)data;
1277 if (item->cache == NULL)
1280 *memusage += msgcache_get_memory_usage(item->cache);
1283 gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
1285 FolderItem *fa = (FolderItem *)a;
1286 FolderItem *fb = (FolderItem *)b;
1288 return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
1291 void folder_find_expired_caches(FolderItem *item, gpointer data)
1293 GSList **folder_item_list = (GSList **)data;
1294 gint difftime, expiretime;
1296 if (item->cache == NULL)
1299 if (item->opened > 0)
1302 difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
1303 expiretime = prefs_common.cache_min_keep_time * 60;
1304 debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
1305 if (difftime > expiretime) {
1306 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
1310 void folder_item_free_cache(FolderItem *item)
1312 g_return_if_fail(item != NULL);
1314 if (item->cache == NULL)
1317 if (item->opened > 0)
1320 folder_item_write_cache(item);
1321 msgcache_destroy(item->cache);
1325 void folder_clean_cache_memory()
1329 folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);
1330 debug_print("Total cache memory usage: %d\n", memusage);
1332 if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
1333 GSList *folder_item_list = NULL, *listitem;
1335 debug_print("Trying to free cache memory\n");
1337 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);
1338 listitem = folder_item_list;
1339 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
1340 FolderItem *item = (FolderItem *)(listitem->data);
1342 debug_print("Freeing cache memory for %s\n", item->path);
1343 memusage -= msgcache_get_memory_usage(item->cache);
1344 folder_item_free_cache(item);
1345 listitem = listitem->next;
1347 g_slist_free(folder_item_list);
1351 void folder_item_read_cache(FolderItem *item)
1353 gchar *cache_file, *mark_file;
1355 g_return_if_fail(item != NULL);
1357 cache_file = folder_item_get_cache_file(item);
1358 mark_file = folder_item_get_mark_file(item);
1359 item->cache = msgcache_read_cache(item, cache_file);
1361 item->cache = msgcache_new();
1362 folder_item_scan(item);
1364 msgcache_read_mark(item->cache, mark_file);
1368 folder_clean_cache_memory();
1371 void folder_item_write_cache(FolderItem *item)
1373 gchar *cache_file, *mark_file;
1374 PrefsFolderItem *prefs;
1378 if (!item || !item->path || !item->cache)
1381 id = folder_item_get_identifier(item);
1382 debug_print("Save cache for folder %s\n", id);
1385 cache_file = folder_item_get_cache_file(item);
1386 mark_file = folder_item_get_mark_file(item);
1387 if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
1388 prefs = item->prefs;
1389 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
1390 /* for cache file */
1391 filemode = prefs->folder_chmod;
1392 if (filemode & S_IRGRP) filemode |= S_IWGRP;
1393 if (filemode & S_IROTH) filemode |= S_IWOTH;
1394 chmod(cache_file, filemode);
1402 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
1407 g_return_val_if_fail(item != NULL, NULL);
1409 folder = item->folder;
1411 folder_item_read_cache(item);
1413 if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
1416 g_return_val_if_fail(folder->class->get_msginfo, NULL);
1417 if ((msginfo = folder->class->get_msginfo(folder, item, num)) != NULL) {
1418 msgcache_add_msg(item->cache, msginfo);
1425 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
1430 g_return_val_if_fail(item != NULL, NULL);
1432 folder = item->folder;
1434 folder_item_read_cache(item);
1436 if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
1442 GSList *folder_item_get_msg_list(FolderItem *item)
1444 g_return_val_if_fail(item != NULL, NULL);
1446 if (item->cache == 0)
1447 folder_item_read_cache(item);
1449 g_return_val_if_fail(item->cache != NULL, NULL);
1451 return msgcache_get_msg_list(item->cache);
1454 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
1458 g_return_val_if_fail(item != NULL, NULL);
1460 folder = item->folder;
1462 g_return_val_if_fail(folder->class->fetch_msg != NULL, NULL);
1464 return folder->class->fetch_msg(folder, item, num);
1467 static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
1469 static HeaderEntry hentry[] = {{"Message-ID:", NULL, TRUE},
1470 {NULL, NULL, FALSE}};
1474 gchar buf[BUFFSIZE];
1476 if ((fp = fopen(file, "rb")) == NULL)
1479 if ((dest->stype == F_QUEUE) || (dest->stype == F_DRAFT))
1480 while (fgets(buf, sizeof(buf), fp) != NULL)
1481 if (buf[0] == '\r' || buf[0] == '\n') break;
1483 procheader_get_header_fields(fp, hentry);
1484 if (hentry[0].body) {
1485 extract_parenthesis(hentry[0].body, '<', '>');
1486 remove_space(hentry[0].body);
1487 if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
1488 msgnum = msginfo->msgnum;
1489 procmsg_msginfo_free(msginfo);
1491 debug_print("found message as uid %d\n", msgnum);
1495 g_free(hentry[0].body);
1496 hentry[0].body = NULL;
1502 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
1503 gboolean remove_source)
1509 g_return_val_if_fail(dest != NULL, -1);
1510 g_return_val_if_fail(file != NULL, -1);
1512 folder = dest->folder;
1514 g_return_val_if_fail(folder->class->add_msg != NULL, -1);
1517 folder_item_read_cache(dest);
1519 num = folder->class->add_msg(folder, dest, file, FALSE);
1522 msginfo = folder->class->get_msginfo(folder, dest, num);
1524 if (msginfo != NULL) {
1525 if (MSG_IS_NEW(msginfo->flags))
1527 if (MSG_IS_UNREAD(msginfo->flags))
1529 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1530 dest->unreadmarked++;
1531 if (procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD))
1532 procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
1535 msgcache_add_msg(dest->cache, msginfo);
1536 procmsg_msginfo_free(msginfo);
1538 folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1541 dest->last_num = num;
1542 } else if (num == 0) {
1543 folder_item_scan(dest);
1544 num = folder_item_get_msg_num_by_file(dest, file);
1547 if (num >= 0 && remove_source) {
1548 if (unlink(file) < 0)
1549 FILE_OP_ERROR(file, "unlink");
1556 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1561 g_return_val_if_fail(dest != NULL, -1);
1562 g_return_val_if_fail(msginfo != NULL, -1);
1564 folder = dest->folder;
1565 if (dest->last_num < 0) folder->scan(folder, dest);
1567 num = folder->move_msg(folder, dest, msginfo);
1568 if (num > 0) dest->last_num = num;
1574 FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest)
1578 FolderItem *new_item;
1579 FolderItem *next_item;
1582 gchar *old_id, *new_id;
1584 mlist = folder_item_get_msg_list(src);
1587 debug_print("Moving %s to %s\n", src->path, dest->path);
1588 new_item = folder_create_folder(dest, g_basename(src->path));
1589 if (new_item == NULL) {
1590 printf("Can't create folder\n");
1594 if (new_item->folder == NULL)
1595 new_item->folder = dest->folder;
1598 log_message(_("Moving %s to %s...\n"),
1599 src->name, new_item->path);
1600 folder_item_move_msgs_with_dest(new_item, mlist);
1603 prefs_folder_item_copy_prefs(src, new_item);
1604 new_item->collapsed = src->collapsed;
1605 new_item->thread_collapsed = src->thread_collapsed;
1606 new_item->threaded = src->threaded;
1607 new_item->ret_rcpt = src->ret_rcpt;
1608 new_item->hide_read_msgs = src->hide_read_msgs;
1609 new_item->sort_key = src->sort_key;
1610 new_item->sort_type = src->sort_type;
1612 prefs_matcher_write_config();
1615 srcnode = src->folder->node;
1616 srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
1617 srcnode = srcnode->children;
1618 while (srcnode != NULL) {
1619 if (srcnode && srcnode->data) {
1620 next_item = (FolderItem*) srcnode->data;
1621 srcnode = srcnode->next;
1622 if (folder_item_move_recursive(next_item, new_item) == NULL)
1626 old_id = folder_item_get_identifier(src);
1627 new_id = folder_item_get_identifier(new_item);
1628 debug_print("updating rules : %s => %s\n", old_id, new_id);
1630 src->folder->class->remove_folder(src->folder, src);
1631 folder_write_list();
1633 if (old_id != NULL && new_id != NULL)
1634 prefs_filtering_rename_path(old_id, new_id);
1641 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item)
1643 FolderItem *tmp = dest->parent;
1644 gchar * src_identifier, * dst_identifier;
1645 gchar * phys_srcpath, * phys_dstpath;
1650 return F_MOVE_FAILED_DEST_IS_CHILD;
1657 src_identifier = folder_item_get_identifier(src);
1658 dst_identifier = folder_item_get_identifier(dest);
1660 if(dst_identifier == NULL && dest->folder && dest->parent == NULL) {
1661 /* dest can be a root folder */
1662 dst_identifier = folder_get_identifier(dest->folder);
1664 if (src_identifier == NULL || dst_identifier == NULL) {
1665 debug_print("Can't get identifiers\n");
1666 return F_MOVE_FAILED;
1669 if (src->folder != dest->folder) {
1670 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
1673 phys_srcpath = folder_item_get_path(src);
1674 phys_dstpath = g_strconcat(folder_item_get_path(dest),G_DIR_SEPARATOR_S,g_basename(phys_srcpath),NULL);
1676 if (src->parent == dest || src == dest) {
1677 g_free(src_identifier);
1678 g_free(dst_identifier);
1679 g_free(phys_srcpath);
1680 g_free(phys_dstpath);
1681 return F_MOVE_FAILED_DEST_IS_PARENT;
1683 debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
1684 if ((tmp = folder_item_move_recursive(src, dest)) == NULL) {
1685 return F_MOVE_FAILED;
1689 src_node = g_node_find(src->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, src);
1691 g_node_destroy(src_node);
1693 debug_print("can't remove node: it's null!\n");
1694 /* not to much worry if remove fails, move has been done */
1696 g_free(src_identifier);
1697 g_free(dst_identifier);
1698 g_free(phys_srcpath);
1699 g_free(phys_dstpath);
1706 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1708 GSList *list = NULL;
1711 list = g_slist_append(list, msginfo);
1712 ret = folder_item_move_msgs_with_dest(dest, list);
1719 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1724 g_return_val_if_fail(dest != NULL, -1);
1725 g_return_val_if_fail(msglist != NULL, -1);
1727 folder = dest->folder;
1728 if (dest->last_num < 0) folder->scan(folder, dest);
1730 num = folder->move_msgs_with_dest(folder, dest, msglist);
1731 if (num > 0) dest->last_num = num;
1732 else dest->op_count = 0;
1738 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1742 GSList *newmsgnums = NULL;
1744 gint num, lastnum = -1;
1745 gboolean folderscan = FALSE;
1747 g_return_val_if_fail(dest != NULL, -1);
1748 g_return_val_if_fail(msglist != NULL, -1);
1750 folder = dest->folder;
1752 g_return_val_if_fail(folder->class->copy_msg != NULL, -1);
1753 g_return_val_if_fail(folder->class->remove_msg != NULL, -1);
1756 * Copy messages to destination folder and
1757 * store new message numbers in newmsgnums
1760 for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
1761 MsgInfo * msginfo = (MsgInfo *) l->data;
1763 if (!item && msginfo->folder != NULL)
1764 item = msginfo->folder;
1766 num = folder->class->copy_msg(folder, dest, msginfo);
1767 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1770 /* Read cache for dest folder */
1771 if (!dest->cache) folder_item_read_cache(dest);
1774 * Fetch new MsgInfos for new messages in dest folder,
1775 * add them to the msgcache and update folder message counts
1778 for (l = msglist; l != NULL; l = g_slist_next(l)) {
1779 MsgInfo *msginfo = (MsgInfo *) l->data;
1781 num = GPOINTER_TO_INT(l2->data);
1782 l2 = g_slist_next(l2);
1785 MsgInfo *newmsginfo;
1791 folder_item_scan(dest);
1794 file = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
1795 num = folder_item_get_msg_num_by_file(dest, file);
1806 newmsginfo = folder->class->get_msginfo(folder, dest, num);
1808 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1809 if (dest->stype == F_OUTBOX ||
1810 dest->stype == F_QUEUE ||
1811 dest->stype == F_DRAFT ||
1812 dest->stype == F_TRASH)
1813 MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1814 MSG_NEW|MSG_UNREAD|MSG_DELETED);
1815 msgcache_add_msg(dest->cache, newmsginfo);
1817 if (MSG_IS_NEW(newmsginfo->flags))
1819 if (MSG_IS_UNREAD(newmsginfo->flags))
1821 if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
1822 dest->unreadmarked++;
1823 if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
1824 procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
1826 folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1828 procmsg_msginfo_free(newmsginfo);
1831 newmsginfo = msgcache_get_msg(dest->cache, num);
1832 procmsg_msginfo_unset_flags(newmsginfo, ~0, ~0);
1833 procmsg_msginfo_set_flags(newmsginfo, msginfo->flags.perm_flags, 0);
1839 * Remove source messages from their folders if
1840 * copying was successfull and update folder
1844 for (l = msglist; l != NULL; l = g_slist_next(l)) {
1845 MsgInfo *msginfo = (MsgInfo *) l->data;
1847 num = GPOINTER_TO_INT(l2->data);
1848 l2 = g_slist_next(l2);
1851 item->folder->class->remove_msg(item->folder,
1855 folder_item_read_cache(item);
1856 msgcache_remove_msg(item->cache, msginfo->msgnum);
1858 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1859 msginfo->folder->new--;
1860 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1861 msginfo->folder->unread--;
1862 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1863 msginfo->folder->unreadmarked--;
1864 msginfo->folder->total--;
1865 folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1870 if (folder->class->finished_copy)
1871 folder->class->finished_copy(folder, dest);
1873 g_slist_free(newmsgnums);
1878 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1883 g_return_val_if_fail(dest != NULL, -1);
1884 g_return_val_if_fail(msginfo != NULL, -1);
1886 folder = dest->folder;
1887 if (dest->last_num < 0) folder->scan(folder, dest);
1889 num = folder->copy_msg(folder, dest, msginfo);
1890 if (num > 0) dest->last_num = num;
1896 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1898 GSList *list = NULL;
1901 list = g_slist_append(list, msginfo);
1902 ret = folder_item_copy_msgs_with_dest(dest, list);
1909 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1914 g_return_val_if_fail(dest != NULL, -1);
1915 g_return_val_if_fail(msglist != NULL, -1);
1917 folder = dest->folder;
1918 if (dest->last_num < 0) folder->scan(folder, dest);
1920 num = folder->copy_msgs_with_dest(folder, dest, msglist);
1921 if (num > 0) dest->last_num = num;
1922 else dest->op_count = 0;
1928 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1931 gint num, lastnum = -1;
1932 GSList *newmsgnums = NULL;
1934 gboolean folderscan = FALSE;
1936 g_return_val_if_fail(dest != NULL, -1);
1937 g_return_val_if_fail(msglist != NULL, -1);
1939 folder = dest->folder;
1941 g_return_val_if_fail(folder->class->copy_msg != NULL, -1);
1944 * Copy messages to destination folder and
1945 * store new message numbers in newmsgnums
1947 for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
1948 MsgInfo * msginfo = (MsgInfo *) l->data;
1950 num = folder->class->copy_msg(folder, dest, msginfo);
1951 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1954 /* Read cache for dest folder */
1955 if (!dest->cache) folder_item_read_cache(dest);
1958 * Fetch new MsgInfos for new messages in dest folder,
1959 * add them to the msgcache and update folder message counts
1962 for (l = msglist; l != NULL; l = g_slist_next(l)) {
1963 MsgInfo *msginfo = (MsgInfo *) l->data;
1965 num = GPOINTER_TO_INT(l2->data);
1966 l2 = g_slist_next(l2);
1969 MsgInfo *newmsginfo;
1975 folder_item_scan(dest);
1978 file = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
1979 num = folder_item_get_msg_num_by_file(dest, file);
1990 newmsginfo = folder->class->get_msginfo(folder, dest, num);
1992 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1993 if (dest->stype == F_OUTBOX ||
1994 dest->stype == F_QUEUE ||
1995 dest->stype == F_DRAFT ||
1996 dest->stype == F_TRASH)
1997 MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1998 MSG_NEW|MSG_UNREAD|MSG_DELETED);
1999 msgcache_add_msg(dest->cache, newmsginfo);
2001 if (MSG_IS_NEW(newmsginfo->flags))
2003 if (MSG_IS_UNREAD(newmsginfo->flags))
2005 if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
2006 dest->unreadmarked++;
2007 if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
2008 procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
2010 folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2012 procmsg_msginfo_free(newmsginfo);
2015 newmsginfo = msgcache_get_msg(dest->cache, num);
2016 procmsg_msginfo_unset_flags(newmsginfo, ~0, ~0);
2017 procmsg_msginfo_set_flags(newmsginfo, msginfo->flags.perm_flags, 0);
2022 if (folder->class->finished_copy)
2023 folder->class->finished_copy(folder, dest);
2025 g_slist_free(newmsgnums);
2029 gint folder_item_remove_msg(FolderItem *item, gint num)
2035 g_return_val_if_fail(item != NULL, -1);
2037 folder = item->folder;
2038 if (!item->cache) folder_item_read_cache(item);
2040 ret = folder->class->remove_msg(folder, item, num);
2042 msginfo = msgcache_get_msg(item->cache, num);
2043 if (msginfo != NULL) {
2044 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2046 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2048 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2049 item->unreadmarked--;
2050 procmsg_msginfo_free(msginfo);
2051 msgcache_remove_msg(item->cache, num);
2054 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2059 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
2064 g_return_val_if_fail(item != NULL, -1);
2065 folder = item->folder;
2066 g_return_val_if_fail(folder != NULL, -1);
2068 if (!item->cache) folder_item_read_cache(item);
2070 if (folder->class->remove_msgs) {
2071 ret = folder->class->remove_msgs(folder, item, msglist);
2073 folder_item_scan(item);
2077 while (msglist != NULL) {
2078 MsgInfo *msginfo = (MsgInfo *)msglist->data;
2080 ret = folder_item_remove_msg(item, msginfo->msgnum);
2081 if (ret != 0) break;
2082 msgcache_remove_msg(item->cache, msginfo->msgnum);
2083 msglist = msglist->next;
2089 gint folder_item_remove_all_msg(FolderItem *item)
2094 g_return_val_if_fail(item != NULL, -1);
2096 folder = item->folder;
2098 g_return_val_if_fail(folder->class->remove_all_msg != NULL, -1);
2100 result = folder->class->remove_all_msg(folder, item);
2103 if (folder->class->finished_remove)
2104 folder->class->finished_remove(folder, item);
2106 folder_item_free_cache(item);
2107 item->cache = msgcache_new();
2111 item->unreadmarked = 0;
2113 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2119 void folder_item_change_msg_flags(FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
2121 g_return_if_fail(item != NULL);
2122 g_return_if_fail(msginfo != NULL);
2124 if (item->folder->class->change_flags != NULL) {
2125 item->folder->class->change_flags(item->folder, item, msginfo, newflags);
2127 msginfo->flags.perm_flags = newflags;
2131 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
2135 g_return_val_if_fail(item != NULL, FALSE);
2137 folder = item->folder;
2139 g_return_val_if_fail(folder->class->is_msg_changed != NULL, -1);
2141 return folder->class->is_msg_changed(folder, item, msginfo);
2144 gchar *folder_item_get_cache_file(FolderItem *item)
2149 g_return_val_if_fail(item != NULL, NULL);
2150 g_return_val_if_fail(item->path != NULL, NULL);
2152 path = folder_item_get_path(item);
2153 g_return_val_if_fail(path != NULL, NULL);
2154 if (!is_dir_exist(path))
2155 make_dir_hier(path);
2156 file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
2162 gchar *folder_item_get_mark_file(FolderItem *item)
2167 g_return_val_if_fail(item != NULL, NULL);
2168 g_return_val_if_fail(item->path != NULL, NULL);
2170 path = folder_item_get_path(item);
2171 g_return_val_if_fail(path != NULL, NULL);
2172 if (!is_dir_exist(path))
2173 make_dir_hier(path);
2174 file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
2180 static gboolean folder_build_tree(GNode *node, gpointer data)
2182 Folder *folder = FOLDER(data);
2186 SpecialFolderItemType stype = F_NORMAL;
2187 const gchar *name = NULL;
2188 const gchar *path = NULL;
2189 PrefsAccount *account = NULL;
2190 gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE,
2191 threaded = TRUE, apply_sub = FALSE;
2192 gboolean ret_rcpt = FALSE, hidereadmsgs = FALSE,
2193 thread_collapsed = FALSE; /* CLAWS */
2194 FolderSortKey sort_key = SORT_BY_NONE;
2195 FolderSortType sort_type = SORT_ASCENDING;
2196 gint new = 0, unread = 0, total = 0, unreadmarked = 0;
2199 g_return_val_if_fail(node->data != NULL, FALSE);
2200 if (!node->parent) return FALSE;
2202 xmlnode = node->data;
2203 if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
2204 g_warning("tag name != \"folderitem\"\n");
2208 list = xmlnode->tag->attr;
2209 for (; list != NULL; list = list->next) {
2210 XMLAttr *attr = list->data;
2212 if (!attr || !attr->name || !attr->value) continue;
2213 if (!strcmp(attr->name, "type")) {
2214 if (!strcasecmp(attr->value, "normal"))
2216 else if (!strcasecmp(attr->value, "inbox"))
2218 else if (!strcasecmp(attr->value, "outbox"))
2220 else if (!strcasecmp(attr->value, "draft"))
2222 else if (!strcasecmp(attr->value, "queue"))
2224 else if (!strcasecmp(attr->value, "trash"))
2226 } else if (!strcmp(attr->name, "name"))
2228 else if (!strcmp(attr->name, "path"))
2230 else if (!strcmp(attr->name, "mtime"))
2231 mtime = strtoul(attr->value, NULL, 10);
2232 else if (!strcmp(attr->name, "new"))
2233 new = atoi(attr->value);
2234 else if (!strcmp(attr->name, "unread"))
2235 unread = atoi(attr->value);
2236 else if (!strcmp(attr->name, "unreadmarked"))
2237 unreadmarked = atoi(attr->value);
2238 else if (!strcmp(attr->name, "total"))
2239 total = atoi(attr->value);
2240 else if (!strcmp(attr->name, "no_sub"))
2241 no_sub = *attr->value == '1' ? TRUE : FALSE;
2242 else if (!strcmp(attr->name, "no_select"))
2243 no_select = *attr->value == '1' ? TRUE : FALSE;
2244 else if (!strcmp(attr->name, "collapsed"))
2245 collapsed = *attr->value == '1' ? TRUE : FALSE;
2246 else if (!strcmp(attr->name, "thread_collapsed"))
2247 thread_collapsed = *attr->value == '1' ? TRUE : FALSE;
2248 else if (!strcmp(attr->name, "threaded"))
2249 threaded = *attr->value == '1' ? TRUE : FALSE;
2250 else if (!strcmp(attr->name, "hidereadmsgs"))
2251 hidereadmsgs = *attr->value == '1' ? TRUE : FALSE;
2252 else if (!strcmp(attr->name, "reqretrcpt"))
2253 ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
2254 else if (!strcmp(attr->name, "sort_key")) {
2255 if (!strcmp(attr->value, "none"))
2256 sort_key = SORT_BY_NONE;
2257 else if (!strcmp(attr->value, "number"))
2258 sort_key = SORT_BY_NUMBER;
2259 else if (!strcmp(attr->value, "size"))
2260 sort_key = SORT_BY_SIZE;
2261 else if (!strcmp(attr->value, "date"))
2262 sort_key = SORT_BY_DATE;
2263 else if (!strcmp(attr->value, "from"))
2264 sort_key = SORT_BY_FROM;
2265 else if (!strcmp(attr->value, "subject"))
2266 sort_key = SORT_BY_SUBJECT;
2267 else if (!strcmp(attr->value, "score"))
2268 sort_key = SORT_BY_SCORE;
2269 else if (!strcmp(attr->value, "label"))
2270 sort_key = SORT_BY_LABEL;
2271 else if (!strcmp(attr->value, "mark"))
2272 sort_key = SORT_BY_MARK;
2273 else if (!strcmp(attr->value, "unread"))
2274 sort_key = SORT_BY_STATUS;
2275 else if (!strcmp(attr->value, "mime"))
2276 sort_key = SORT_BY_MIME;
2277 else if (!strcmp(attr->value, "to"))
2278 sort_key = SORT_BY_TO;
2279 else if (!strcmp(attr->value, "locked"))
2280 sort_key = SORT_BY_LOCKED;
2281 } else if (!strcmp(attr->name, "sort_type")) {
2282 if (!strcmp(attr->value, "ascending"))
2283 sort_type = SORT_ASCENDING;
2285 sort_type = SORT_DESCENDING;
2286 } else if (!strcmp(attr->name, "account_id")) {
2287 account = account_find_from_id(atoi(attr->value));
2288 if (!account) g_warning("account_id: %s not found\n",
2290 } else if (!strcmp(attr->name, "apply_sub"))
2291 apply_sub = *attr->value == '1' ? TRUE : FALSE;
2294 item = folder_item_new(folder, name, path);
2295 item->stype = stype;
2296 item->mtime = mtime;
2298 item->unread = unread;
2299 item->unreadmarked = unreadmarked;
2300 item->total = total;
2301 item->no_sub = no_sub;
2302 item->no_select = no_select;
2303 item->collapsed = collapsed;
2304 item->thread_collapsed = thread_collapsed;
2305 item->threaded = threaded;
2306 item->hide_read_msgs = hidereadmsgs;
2307 item->ret_rcpt = ret_rcpt;
2308 item->sort_key = sort_key;
2309 item->sort_type = sort_type;
2310 item->parent = FOLDER_ITEM(node->parent->data);
2311 item->folder = folder;
2313 case F_INBOX: folder->inbox = item; break;
2314 case F_OUTBOX: folder->outbox = item; break;
2315 case F_DRAFT: folder->draft = item; break;
2316 case F_QUEUE: folder->queue = item; break;
2317 case F_TRASH: folder->trash = item; break;
2320 item->account = account;
2321 item->apply_sub = apply_sub;
2322 prefs_folder_item_read_config(item);
2325 xml_free_node(xmlnode);
2330 static gboolean folder_read_folder_func(GNode *node, gpointer data)
2335 FolderClass *class = NULL;
2336 const gchar *name = NULL;
2337 const gchar *path = NULL;
2338 PrefsAccount *account = NULL;
2339 gboolean collapsed = FALSE, threaded = TRUE, apply_sub = FALSE;
2340 gboolean ret_rcpt = FALSE, thread_collapsed = FALSE; /* CLAWS */
2342 if (g_node_depth(node) != 2) return FALSE;
2343 g_return_val_if_fail(node->data != NULL, FALSE);
2345 xmlnode = node->data;
2346 if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
2347 g_warning("tag name != \"folder\"\n");
2350 g_node_unlink(node);
2351 list = xmlnode->tag->attr;
2352 for (; list != NULL; list = list->next) {
2353 XMLAttr *attr = list->data;
2355 if (!attr || !attr->name || !attr->value) continue;
2356 if (!strcmp(attr->name, "type"))
2357 class = folder_get_class_from_string(attr->value);
2358 else if (!strcmp(attr->name, "name"))
2360 else if (!strcmp(attr->name, "path"))
2362 else if (!strcmp(attr->name, "collapsed"))
2363 collapsed = *attr->value == '1' ? TRUE : FALSE;
2364 else if (!strcmp(attr->name, "thread_collapsed"))
2365 thread_collapsed = *attr->value == '1' ? TRUE : FALSE;
2366 else if (!strcmp(attr->name, "threaded"))
2367 threaded = *attr->value == '1' ? TRUE : FALSE;
2368 else if (!strcmp(attr->name, "account_id")) {
2369 account = account_find_from_id(atoi(attr->value));
2370 if (!account) g_warning("account_id: %s not found\n",
2372 } else if (!strcmp(attr->name, "apply_sub"))
2373 apply_sub = *attr->value == '1' ? TRUE : FALSE;
2374 else if (!strcmp(attr->name, "reqretrcpt"))
2375 ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
2378 folder = folder_new(class, name, path);
2379 g_return_val_if_fail(folder != NULL, FALSE);
2380 folder->account = account;
2381 if (account != NULL)
2382 account->folder = REMOTE_FOLDER(folder);
2383 node->data = folder->node->data;
2384 g_node_destroy(folder->node);
2385 folder->node = node;
2387 FOLDER_ITEM(node->data)->collapsed = collapsed;
2388 FOLDER_ITEM(node->data)->thread_collapsed = thread_collapsed;
2389 FOLDER_ITEM(node->data)->threaded = threaded;
2390 FOLDER_ITEM(node->data)->account = account;
2391 FOLDER_ITEM(node->data)->apply_sub = apply_sub;
2392 FOLDER_ITEM(node->data)->ret_rcpt = ret_rcpt;
2394 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2395 folder_build_tree, folder);
2400 static gchar *folder_get_list_path(void)
2402 static gchar *filename = NULL;
2405 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2411 #define PUT_ESCAPE_STR(fp, attr, str) \
2413 fputs(" " attr "=\"", fp); \
2414 xml_file_put_escape_str(fp, str); \
2418 static void folder_write_list_recursive(GNode *node, gpointer data)
2420 FILE *fp = (FILE *)data;
2423 static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
2424 "draft", "queue", "trash"};
2425 static gchar *sort_key_str[] = {"none", "number", "size", "date",
2426 "from", "subject", "score", "label",
2427 "mark", "unread", "mime", "to",
2429 g_return_if_fail(node != NULL);
2430 g_return_if_fail(fp != NULL);
2432 item = FOLDER_ITEM(node->data);
2433 g_return_if_fail(item != NULL);
2435 depth = g_node_depth(node);
2436 for (i = 0; i < depth; i++)
2439 Folder *folder = item->folder;
2441 fprintf(fp, "<folder type=\"%s\"", folder->class->idstr);
2443 PUT_ESCAPE_STR(fp, "name", folder->name);
2444 if (FOLDER_TYPE(folder) == F_MH || FOLDER_TYPE(folder) == F_MBOX)
2445 PUT_ESCAPE_STR(fp, "path",
2446 LOCAL_FOLDER(folder)->rootpath);
2447 if (item->collapsed && node->children)
2448 fputs(" collapsed=\"1\"", fp);
2449 if (folder->account)
2450 fprintf(fp, " account_id=\"%d\"",
2451 folder->account->account_id);
2452 if (item->apply_sub)
2453 fputs(" apply_sub=\"1\"", fp);
2455 fputs(" reqretrcpt=\"1\"", fp);
2457 fprintf(fp, "<folderitem type=\"%s\"",
2458 folder_item_stype_str[item->stype]);
2460 PUT_ESCAPE_STR(fp, "name", item->name);
2462 PUT_ESCAPE_STR(fp, "path", item->path);
2465 fputs(" no_sub=\"1\"", fp);
2466 if (item->no_select)
2467 fputs(" no_select=\"1\"", fp);
2468 if (item->collapsed && node->children)
2469 fputs(" collapsed=\"1\"", fp);
2471 fputs(" collapsed=\"0\"", fp);
2472 if (item->thread_collapsed)
2473 fputs(" thread_collapsed=\"1\"", fp);
2475 fputs(" thread_collapsed=\"0\"", fp);
2477 fputs(" threaded=\"1\"", fp);
2479 fputs(" threaded=\"0\"", fp);
2480 if (item->hide_read_msgs)
2481 fputs(" hidereadmsgs=\"1\"", fp);
2483 fputs(" hidereadmsgs=\"0\"", fp);
2485 fputs(" reqretrcpt=\"1\"", fp);
2487 if (item->sort_key != SORT_BY_NONE) {
2488 fprintf(fp, " sort_key=\"%s\"",
2489 sort_key_str[item->sort_key]);
2490 if (item->sort_type == SORT_ASCENDING)
2491 fprintf(fp, " sort_type=\"ascending\"");
2493 fprintf(fp, " sort_type=\"descending\"");
2497 " mtime=\"%lu\" new=\"%d\" unread=\"%d\" unreadmarked=\"%d\" total=\"%d\"",
2498 item->mtime, item->new, item->unread, item->unreadmarked, item->total);
2501 fprintf(fp, " account_id=\"%d\"",
2502 item->account->account_id);
2503 if (item->apply_sub)
2504 fputs(" apply_sub=\"1\"", fp);
2507 if (node->children) {
2511 child = node->children;
2517 folder_write_list_recursive(cur, data);
2520 for (i = 0; i < depth; i++)
2522 fprintf(fp, "</%s>\n", depth == 1 ? "folder" : "folderitem");
2527 static void folder_update_op_count_rec(GNode *node)
2529 FolderItem *fitem = FOLDER_ITEM(node->data);
2531 if (g_node_depth(node) > 0) {
2532 if (fitem->op_count > 0) {
2533 fitem->op_count = 0;
2534 folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
2536 if (node->children) {
2539 child = node->children;
2545 folder_update_op_count_rec(cur);
2551 void folder_update_op_count() {
2555 for (cur = folder_list; cur != NULL; cur = cur->next) {
2557 folder_update_op_count_rec(folder->node);
2561 typedef struct _type_str {
2568 static gchar * folder_item_get_tree_identifier(FolderItem * item)
2570 if (item->parent != NULL) {
2574 path = folder_item_get_tree_identifier(item->parent);
2578 id = g_strconcat(path, "/", item->name, NULL);
2584 return g_strconcat("/", item->name, NULL);
2589 /* CLAWS: temporary local folder for filtering */
2590 static Folder *processing_folder;
2591 static FolderItem *processing_folder_item;
2593 static void folder_create_processing_folder(void)
2595 #define PROCESSING_FOLDER ".processing"
2599 tmpparent = folder_get_default_folder();
2600 g_assert(tmpparent);
2601 debug_print("tmpparentroot %s\n", LOCAL_FOLDER(tmpparent)->rootpath);
2602 if (LOCAL_FOLDER(tmpparent)->rootpath[0] == '/')
2603 tmpname = g_strconcat(LOCAL_FOLDER(tmpparent)->rootpath,
2604 G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2607 tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2608 LOCAL_FOLDER(tmpparent)->rootpath,
2609 G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2612 processing_folder = folder_new(mh_get_class(), "PROCESSING", LOCAL_FOLDER(tmpparent)->rootpath);
2613 g_assert(processing_folder);
2615 if (!is_dir_exist(tmpname)) {
2616 debug_print("*TMP* creating %s\n", tmpname);
2617 processing_folder_item = processing_folder->class->create_folder(processing_folder,
2618 processing_folder->node->data,
2620 g_assert(processing_folder_item);
2623 debug_print("*TMP* already created\n");
2624 processing_folder_item = folder_item_new(processing_folder, ".processing", ".processing");
2625 g_assert(processing_folder_item);
2626 folder_item_append(processing_folder->node->data, processing_folder_item);
2631 FolderItem *folder_get_default_processing(void)
2633 if (!processing_folder_item) {
2634 folder_create_processing_folder();
2636 return processing_folder_item;
2639 /* folder_persist_prefs_new() - return hash table with persistent
2640 * settings (and folder name as key).
2641 * (note that in claws other options are in the PREFS_FOLDER_ITEM_RC
2642 * file, so those don't need to be included in PersistPref yet)
2644 GHashTable *folder_persist_prefs_new(Folder *folder)
2646 GHashTable *pptable;
2648 g_return_val_if_fail(folder, NULL);
2649 pptable = g_hash_table_new(g_str_hash, g_str_equal);
2650 folder_get_persist_prefs_recursive(folder->node, pptable);
2654 void folder_persist_prefs_free(GHashTable *pptable)
2656 g_return_if_fail(pptable);
2657 g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
2658 g_hash_table_destroy(pptable);
2661 const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
2663 if (pptable == NULL || name == NULL) return NULL;
2664 return g_hash_table_lookup(pptable, name);
2667 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
2669 const PersistPrefs *pp;
2670 gchar *id = folder_item_get_identifier(item);
2672 pp = folder_get_persist_prefs(pptable, id);
2677 /* CLAWS: since not all folder properties have been migrated to
2678 * folderlist.xml, we need to call the old stuff first before
2679 * setting things that apply both to Main and Claws. */
2680 prefs_folder_item_read_config(item);
2682 item->collapsed = pp->collapsed;
2683 item->thread_collapsed = pp->thread_collapsed;
2684 item->threaded = pp->threaded;
2685 item->ret_rcpt = pp->ret_rcpt;
2686 item->hide_read_msgs = pp->hide_read_msgs;
2687 item->sort_key = pp->sort_key;
2688 item->sort_type = pp->sort_type;
2691 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
2693 FolderItem *item = FOLDER_ITEM(node->data);
2698 g_return_if_fail(node != NULL);
2699 g_return_if_fail(item != NULL);
2701 /* NOTE: item->path == NULL means top level folder; not interesting
2702 * to store preferences of that one. */
2704 id = folder_item_get_identifier(item);
2705 pp = g_new0(PersistPrefs, 1);
2706 g_return_if_fail(pp != NULL);
2707 pp->collapsed = item->collapsed;
2708 pp->thread_collapsed = item->thread_collapsed;
2709 pp->threaded = item->threaded;
2710 pp->ret_rcpt = item->ret_rcpt;
2711 pp->hide_read_msgs = item->hide_read_msgs;
2712 pp->sort_key = item->sort_key;
2713 pp->sort_type = item->sort_type;
2714 g_hash_table_insert(pptable, id, pp);
2717 if (node->children) {
2718 child = node->children;
2722 folder_get_persist_prefs_recursive(cur, pptable);
2727 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
2736 void folder_item_apply_processing(FolderItem *item)
2738 GSList *processing_list;
2739 GSList *mlist, *cur;
2741 g_return_if_fail(item != NULL);
2743 processing_list = item->prefs->processing;
2744 if (processing_list == NULL)
2747 folder_item_update_freeze();
2749 mlist = folder_item_get_msg_list(item);
2750 for (cur = mlist ; cur != NULL ; cur = cur->next) {
2753 msginfo = (MsgInfo *) cur->data;
2754 filter_message_by_msginfo(processing_list, msginfo);
2755 procmsg_msginfo_free(msginfo);
2757 g_slist_free(mlist);
2759 folder_item_update_thaw();
2763 * functions for handling FolderItem content changes
2765 static gint folder_item_update_freeze_cnt = 0;
2768 * Notify the folder system about changes to a folder. If the
2769 * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
2770 * be invoked, otherwise the changes will be remebered until
2771 * the folder system is thawed.
2773 * \param item The FolderItem that was changed
2774 * \param update_flags Type of changed that was made
2776 void folder_item_update(FolderItem *item, FolderItemUpdateFlags update_flags)
2778 if (folder_item_update_freeze_cnt == 0) {
2779 FolderItemUpdateData source;
2782 source.update_flags = update_flags;
2783 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
2785 item->update_flags |= update_flags;
2789 void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
2791 GNode *node = item->folder->node;
2793 node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
2794 node = node->children;
2796 folder_item_update(item, update_flags);
2797 while (node != NULL) {
2798 if (node && node->data) {
2799 FolderItem *next_item = (FolderItem*) node->data;
2801 folder_item_update(next_item, update_flags);
2807 void folder_item_update_freeze()
2809 folder_item_update_freeze_cnt++;
2812 static void folder_item_update_func(FolderItem *item, gpointer data)
2814 FolderItemUpdateData source;
2816 if (item->update_flags) {
2818 source.update_flags = item->update_flags;
2819 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
2820 item->update_flags = 0;
2824 void folder_item_update_thaw()
2826 if (folder_item_update_freeze_cnt > 0)
2827 folder_item_update_freeze_cnt--;
2828 if (folder_item_update_freeze_cnt == 0) {
2829 /* Update all folders */
2830 folder_func_to_all_folders(folder_item_update_func, NULL);
2834 #undef PUT_ESCAPE_STR