Fix bug #3574: Template addressing
[claws.git] / src / folder.c
blobad75045e013b2586c3e365369caf9f2ae6e2744d
1 /*
2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #ifdef WIN32
35 #include <w32lib.h>
36 #endif
38 #include "alertpanel.h"
39 #include "folder.h"
40 #include "session.h"
41 #include "inc.h"
42 #include "imap.h"
43 #include "news.h"
44 #include "mh.h"
45 #include "utils.h"
46 #include "xml.h"
47 #include "codeconv.h"
48 #include "prefs_gtk.h"
49 #include "account.h"
50 #include "filtering.h"
51 #include "procheader.h"
52 #include "hooks.h"
53 #include "log.h"
54 #include "folder_item_prefs.h"
55 #include "remotefolder.h"
56 #include "partial_download.h"
57 #include "statusbar.h"
58 #include "gtkutils.h"
59 #include "timing.h"
60 #include "compose.h"
61 #include "main.h"
62 #include "msgcache.h"
63 #include "privacy.h"
65 /* Dependecies to be removed ?! */
66 #include "prefs_common.h"
67 #include "prefs_account.h"
69 /* Define possible missing constants for Windows. */
70 #ifdef G_OS_WIN32
71 # ifndef S_IRGRP
72 # define S_IRGRP 0
73 # define S_IWGRP 0
74 # endif
75 # ifndef S_IROTH
76 # define S_IROTH 0
77 # define S_IWOTH 0
78 # endif
79 #endif
81 static GList *folder_list = NULL;
82 static GSList *class_list = NULL;
83 static GSList *folder_unloaded_list = NULL;
85 void folder_init (Folder *folder,
86 const gchar *name);
88 static gchar *folder_item_get_cache_file (FolderItem *item);
89 static gchar *folder_item_get_mark_file (FolderItem *item);
90 static gchar *folder_item_get_tags_file (FolderItem *item);
91 static GNode *folder_get_xml_node (Folder *folder);
92 static Folder *folder_get_from_xml (GNode *node);
93 static void folder_update_op_count_rec (GNode *node);
96 static void folder_get_persist_prefs_recursive
97 (GNode *node, GHashTable *pptable);
98 static gboolean persist_prefs_free (gpointer key, gpointer val, gpointer data);
99 static void folder_item_read_cache (FolderItem *item);
100 gint folder_item_scan_full (FolderItem *item, gboolean filtering);
101 static void folder_item_update_with_msg (FolderItem *item, FolderItemUpdateFlags update_flags,
102 MsgInfo *msg);
103 static GHashTable *folder_persist_prefs_new (Folder *folder);
104 static void folder_persist_prefs_free (GHashTable *pptable);
105 static void folder_item_restore_persist_prefs (FolderItem *item, GHashTable *pptable);
107 void folder_system_init(void)
109 folder_register_class(mh_get_class());
110 folder_register_class(imap_get_class());
111 folder_register_class(news_get_class());
114 static GSList *folder_get_class_list(void)
116 return class_list;
119 void folder_register_class(FolderClass *klass)
121 GSList *xmllist, *cur;
123 debug_print("registering folder class %s\n", klass->idstr);
125 class_list = g_slist_append(class_list, klass);
127 xmllist = g_slist_copy(folder_unloaded_list);
128 for (cur = xmllist; cur != NULL; cur = g_slist_next(cur)) {
129 GNode *node = (GNode *) cur->data;
130 XMLNode *xmlnode = (XMLNode *) node->data;
131 GList *cur = xmlnode->tag->attr;
133 for (; cur != NULL; cur = g_list_next(cur)) {
134 XMLAttr *attr = (XMLAttr *) cur->data;
136 if (!attr || !attr->name || !attr->value) continue;
137 if (!strcmp(attr->name, "type") && !strcmp(attr->value, klass->idstr)) {
138 Folder *folder;
140 folder = folder_get_from_xml(node);
141 if (folder) {
142 folder_add(folder);
143 folder_unloaded_list = g_slist_remove(folder_unloaded_list, node);
144 xml_free_tree(node);
146 cur = NULL;
147 continue;
151 g_slist_free(xmllist);
154 void folder_unregister_class(FolderClass *klass)
156 GList *folderlist, *cur;
158 debug_print("unregistering folder class %s\n", klass->idstr);
160 class_list = g_slist_remove(class_list, klass);
162 folderlist = g_list_copy(folder_get_list());
163 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
164 Folder *folder = (Folder *) cur->data;
166 if (folder->klass == klass) {
167 GNode *xmlnode = folder_get_xml_node(folder);
168 folder_unloaded_list = g_slist_append(folder_unloaded_list, xmlnode);
169 folder_destroy(folder);
172 g_list_free(folderlist);
174 if (klass->prefs_pages)
175 g_slist_free(klass->prefs_pages);
178 Folder *folder_new(FolderClass *klass, const gchar *name, const gchar *path)
180 Folder *folder = NULL;
181 FolderItem *item;
183 cm_return_val_if_fail(klass != NULL, NULL);
185 name = name ? name : path;
186 folder = klass->new_folder(name, path);
188 /* Create root folder item */
189 item = folder_item_new(folder, name, NULL);
190 if (item == NULL) {
191 return NULL;
193 item->folder = folder;
194 folder->node = item->node;
195 folder->data = NULL;
197 return folder;
200 void folder_init(Folder *folder, const gchar *name)
202 cm_return_if_fail(folder != NULL);
204 folder_set_name(folder, name);
206 /* Init folder data */
207 folder->account = NULL;
208 folder->sort = 0;
209 folder->inbox = NULL;
210 folder->outbox = NULL;
211 folder->draft = NULL;
212 folder->queue = NULL;
213 folder->trash = NULL;
216 static void reset_parent_type(FolderItem *item, gpointer data) {
217 item->parent_stype = -1;
220 void folder_item_change_type(FolderItem *item, SpecialFolderItemType newtype)
222 Folder *folder = NULL;
223 FolderUpdateData hookdata;
225 if (item == NULL)
226 return;
228 folder = item->folder;
229 /* unset previous root of newtype */
230 switch(newtype) {
231 case F_INBOX:
232 folder_item_change_type(folder->inbox, F_NORMAL);
233 folder->inbox = item;
234 break;
235 case F_OUTBOX:
236 folder_item_change_type(folder->outbox, F_NORMAL);
237 folder->outbox = item;
238 break;
239 case F_QUEUE:
240 folder_item_change_type(folder->queue, F_NORMAL);
241 folder->queue = item;
242 break;
243 case F_DRAFT:
244 folder_item_change_type(folder->draft, F_NORMAL);
245 folder->draft = item;
246 break;
247 case F_TRASH:
248 folder_item_change_type(folder->trash, F_NORMAL);
249 folder->trash = item;
250 break;
251 case F_NORMAL:
252 default:
253 break;
255 /* set new type for current folder and sons */
256 item->stype = newtype;
257 folder_func_to_all_folders(reset_parent_type, NULL);
259 hookdata.folder = folder;
260 hookdata.update_flags = FOLDER_TREE_CHANGED;
261 hookdata.item = NULL;
262 hookdata.item2 = NULL;
263 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
266 void folder_destroy(Folder *folder)
268 cm_return_if_fail(folder != NULL);
269 cm_return_if_fail(folder->klass->destroy_folder != NULL);
271 folder_remove(folder);
273 folder_tree_destroy(folder);
275 folder->klass->destroy_folder(folder);
277 g_free(folder->name);
278 g_free(folder);
281 void folder_set_xml(Folder *folder, XMLTag *tag)
283 GList *cur;
284 FolderItem *rootitem = NULL;
286 if ((folder->node != NULL) && (folder->node->data != NULL))
287 rootitem = (FolderItem *) folder->node->data;
289 for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
290 XMLAttr *attr = (XMLAttr *) cur->data;
292 if (!attr || !attr->name || !attr->value) continue;
293 if (!strcmp(attr->name, "name")) {
294 g_free(folder->name);
295 folder->name = g_strdup(attr->value);
296 if (rootitem != NULL) {
297 g_free(rootitem->name);
298 rootitem->name = g_strdup(attr->value);
300 } else if (!strcmp(attr->name, "account_id")) {
301 PrefsAccount *account;
303 account = account_find_from_id(atoi(attr->value));
304 if (!account)
305 g_warning("account_id: %s not found", attr->value);
306 else {
307 folder->account = account;
308 account->folder = folder;
310 } else if (!strcmp(attr->name, "collapsed")) {
311 if (rootitem != NULL)
312 rootitem->collapsed = *attr->value == '1' ? TRUE : FALSE;
313 } else if (!strcmp(attr->name, "sort")) {
314 folder->sort = atoi(attr->value);
319 XMLTag *folder_get_xml(Folder *folder)
321 XMLTag *tag;
323 tag = xml_tag_new("folder");
325 if (folder->name)
326 xml_tag_add_attr(tag, xml_attr_new("name", folder->name));
327 if (folder->account)
328 xml_tag_add_attr(tag, xml_attr_new_int("account_id", folder->account->account_id));
329 if (folder->node && folder->node->data) {
330 FolderItem *rootitem = (FolderItem *) folder->node->data;
332 xml_tag_add_attr(tag, xml_attr_new("collapsed", rootitem->collapsed ? "1" : "0"));
334 xml_tag_add_attr(tag, xml_attr_new_int("sort", folder->sort));
336 return tag;
339 FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path)
341 FolderItem *item = NULL;
343 cm_return_val_if_fail(folder != NULL, NULL);
345 if (folder->klass->item_new) {
346 item = folder->klass->item_new(folder);
347 } else {
348 item = g_new0(FolderItem, 1);
351 cm_return_val_if_fail(item != NULL, NULL);
353 item->stype = F_NORMAL;
355 if(!g_utf8_validate(name, -1, NULL)) {
356 item->name = g_malloc(strlen(name)*2+1);
357 conv_localetodisp(item->name, strlen(name)*2+1, name);
358 } else {
359 item->name = g_strdup(name);
362 item->path = g_strdup(path);
363 item->mtime = 0;
364 item->new_msgs = 0;
365 item->unread_msgs = 0;
366 item->unreadmarked_msgs = 0;
367 item->marked_msgs = 0;
368 item->total_msgs = 0;
369 item->replied_msgs = 0;
370 item->forwarded_msgs = 0;
371 item->locked_msgs = 0;
372 item->ignored_msgs = 0;
373 item->watched_msgs = 0;
374 item->order = 0;
375 item->last_num = -1;
376 item->cache = NULL;
377 item->no_sub = FALSE;
378 item->no_select = FALSE;
379 item->collapsed = FALSE;
380 item->thread_collapsed = FALSE;
381 item->threaded = TRUE;
382 item->ret_rcpt = FALSE;
383 item->opened = FALSE;
384 item->node = g_node_new(item);
385 item->folder = NULL;
386 item->account = NULL;
387 item->apply_sub = FALSE;
388 item->mark_queue = NULL;
389 item->data = NULL;
390 item->parent_stype = -1;
392 item->sort_key = prefs_common.default_sort_key;
393 item->sort_type = prefs_common.default_sort_type;
395 item->prefs = folder_item_prefs_new();
397 return item;
400 void folder_item_append(FolderItem *parent, FolderItem *item)
402 cm_return_if_fail(parent != NULL);
403 cm_return_if_fail(parent->folder != NULL);
404 cm_return_if_fail(parent->node != NULL);
405 cm_return_if_fail(item != NULL);
407 item->folder = parent->folder;
408 g_node_append(parent->node, item->node);
411 void folder_item_remove(FolderItem *item)
413 GNode *node, *start_node;
414 FolderUpdateData hookdata;
415 gchar *tags_file = NULL, *tags_dir = NULL;
417 cm_return_if_fail(item != NULL);
418 cm_return_if_fail(item->folder != NULL);
419 cm_return_if_fail(item->folder->node != NULL);
421 start_node = item->node;
423 node = item->folder->node;
425 node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
426 node = node->children;
428 /* remove my children */
429 while (node != NULL) {
430 if (node && node->data) {
431 FolderItem *sub_item = (FolderItem*) node->data;
432 node = node->next;
433 folder_item_remove(sub_item);
437 /* remove myself */
438 if (item->cache != NULL) {
439 msgcache_destroy(item->cache);
440 item->cache = NULL;
442 tags_file = folder_item_get_tags_file(item);
443 if (tags_file)
444 claws_unlink(tags_file);
445 tags_dir = g_path_get_dirname(tags_file);
446 if (tags_dir)
447 rmdir(tags_dir);
449 g_free(tags_file);
450 g_free(tags_dir);
452 hookdata.folder = item->folder;
453 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_REMOVE_FOLDERITEM;
454 hookdata.item = item;
455 hookdata.item2 = NULL;
456 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
458 node = start_node;
460 if (item->folder->node == node)
461 item->folder->node = NULL;
463 folder_item_destroy(item);
465 g_node_destroy(node);
468 void folder_item_remove_children(FolderItem *item)
470 GNode *node, *next;
472 cm_return_if_fail(item != NULL);
473 cm_return_if_fail(item->folder != NULL);
474 cm_return_if_fail(item->node != NULL);
476 node = item->node->children;
477 while (node != NULL) {
478 next = node->next;
479 folder_item_remove(FOLDER_ITEM(node->data));
480 node = next;
484 void folder_item_destroy(FolderItem *item)
486 Folder *folder;
488 cm_return_if_fail(item != NULL);
490 folder = item->folder;
491 if (folder) {
492 if (folder->inbox == item)
493 folder->inbox = NULL;
494 else if (folder->outbox == item)
495 folder->outbox = NULL;
496 else if (folder->draft == item)
497 folder->draft = NULL;
498 else if (folder->queue == item)
499 folder->queue = NULL;
500 else if (folder->trash == item)
501 folder->trash = NULL;
504 if (item->cache)
505 folder_item_free_cache(item, TRUE);
506 if (item->prefs)
507 folder_item_prefs_free(item->prefs);
508 g_free(item->name);
509 g_free(item->path);
511 if (item->folder != NULL) {
512 if(item->folder->klass->item_destroy) {
513 item->folder->klass->item_destroy(item->folder, item);
514 } else {
515 g_free(item);
520 FolderItem *folder_item_parent(FolderItem *item)
522 cm_return_val_if_fail(item != NULL, NULL);
523 cm_return_val_if_fail(item->node != NULL, NULL);
525 if (item->node->parent == NULL)
526 return NULL;
527 return (FolderItem *) item->node->parent->data;
530 void folder_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
532 GList *cur;
534 for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
535 XMLAttr *attr = (XMLAttr *) cur->data;
537 if (!attr || !attr->name || !attr->value) continue;
538 if (!strcmp(attr->name, "type")) {
539 if (!g_ascii_strcasecmp(attr->value, "normal"))
540 item->stype = F_NORMAL;
541 else if (!g_ascii_strcasecmp(attr->value, "inbox"))
542 item->stype = F_INBOX;
543 else if (!g_ascii_strcasecmp(attr->value, "outbox"))
544 item->stype = F_OUTBOX;
545 else if (!g_ascii_strcasecmp(attr->value, "draft"))
546 item->stype = F_DRAFT;
547 else if (!g_ascii_strcasecmp(attr->value, "queue"))
548 item->stype = F_QUEUE;
549 else if (!g_ascii_strcasecmp(attr->value, "trash"))
550 item->stype = F_TRASH;
551 } else if (!strcmp(attr->name, "name")) {
552 g_free(item->name);
553 item->name = g_strdup(attr->value);
554 } else if (!strcmp(attr->name, "path")) {
555 g_free(item->path);
556 item->path = g_strdup(attr->value);
557 } else if (!strcmp(attr->name, "mtime"))
558 item->mtime = strtoul(attr->value, NULL, 10);
559 else if (!strcmp(attr->name, "new"))
560 item->new_msgs = atoi(attr->value);
561 else if (!strcmp(attr->name, "unread"))
562 item->unread_msgs = atoi(attr->value);
563 else if (!strcmp(attr->name, "unreadmarked"))
564 item->unreadmarked_msgs = atoi(attr->value);
565 else if (!strcmp(attr->name, "marked"))
566 item->marked_msgs = atoi(attr->value);
567 else if (!strcmp(attr->name, "replied"))
568 item->replied_msgs = atoi(attr->value);
569 else if (!strcmp(attr->name, "forwarded"))
570 item->forwarded_msgs = atoi(attr->value);
571 else if (!strcmp(attr->name, "locked"))
572 item->locked_msgs = atoi(attr->value);
573 else if (!strcmp(attr->name, "ignored"))
574 item->ignored_msgs = atoi(attr->value);
575 else if (!strcmp(attr->name, "watched"))
576 item->watched_msgs = atoi(attr->value);
577 else if (!strcmp(attr->name, "order"))
578 item->order = atoi(attr->value);
579 else if (!strcmp(attr->name, "total"))
580 item->total_msgs = atoi(attr->value);
581 else if (!strcmp(attr->name, "no_sub"))
582 item->no_sub = *attr->value == '1' ? TRUE : FALSE;
583 else if (!strcmp(attr->name, "no_select"))
584 item->no_select = *attr->value == '1' ? TRUE : FALSE;
585 else if (!strcmp(attr->name, "collapsed"))
586 item->collapsed = *attr->value == '1' ? TRUE : FALSE;
587 else if (!strcmp(attr->name, "thread_collapsed"))
588 item->thread_collapsed = *attr->value == '1' ? TRUE : FALSE;
589 else if (!strcmp(attr->name, "threaded"))
590 item->threaded = *attr->value == '1' ? TRUE : FALSE;
591 else if (!strcmp(attr->name, "hidereadmsgs"))
592 item->hide_read_msgs = *attr->value == '1' ? TRUE : FALSE;
593 else if (!strcmp(attr->name, "hidedelmsgs"))
594 item->hide_del_msgs = *attr->value == '1' ? TRUE : FALSE;
595 else if (!strcmp(attr->name, "hidereadthreads"))
596 item->hide_read_threads = *attr->value == '1' ? TRUE : FALSE;
597 else if (!strcmp(attr->name, "reqretrcpt"))
598 item->ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
599 else if (!strcmp(attr->name, "sort_key")) {
600 if (!strcmp(attr->value, "none"))
601 item->sort_key = SORT_BY_NONE;
602 else if (!strcmp(attr->value, "number"))
603 item->sort_key = SORT_BY_NUMBER;
604 else if (!strcmp(attr->value, "size"))
605 item->sort_key = SORT_BY_SIZE;
606 else if (!strcmp(attr->value, "date"))
607 item->sort_key = SORT_BY_DATE;
608 else if (!strcmp(attr->value, "from"))
609 item->sort_key = SORT_BY_FROM;
610 else if (!strcmp(attr->value, "subject"))
611 item->sort_key = SORT_BY_SUBJECT;
612 else if (!strcmp(attr->value, "score"))
613 item->sort_key = SORT_BY_SCORE;
614 else if (!strcmp(attr->value, "label"))
615 item->sort_key = SORT_BY_LABEL;
616 else if (!strcmp(attr->value, "mark"))
617 item->sort_key = SORT_BY_MARK;
618 else if (!strcmp(attr->value, "unread"))
619 item->sort_key = SORT_BY_STATUS;
620 else if (!strcmp(attr->value, "mime"))
621 item->sort_key = SORT_BY_MIME;
622 else if (!strcmp(attr->value, "to"))
623 item->sort_key = SORT_BY_TO;
624 else if (!strcmp(attr->value, "locked"))
625 item->sort_key = SORT_BY_LOCKED;
626 else if (!strcmp(attr->value, "tags"))
627 item->sort_key = SORT_BY_TAGS;
628 else if (!strcmp(attr->value, "thread_date"))
629 item->sort_key = SORT_BY_THREAD_DATE;
630 } else if (!strcmp(attr->name, "sort_type")) {
631 if (!strcmp(attr->value, "ascending"))
632 item->sort_type = SORT_ASCENDING;
633 else
634 item->sort_type = SORT_DESCENDING;
635 } else if (!strcmp(attr->name, "account_id")) {
636 PrefsAccount *account;
638 account = account_find_from_id(atoi(attr->value));
639 if (!account)
640 g_warning("account_id: %s not found", attr->value);
641 else
642 item->account = account;
643 } else if (!strcmp(attr->name, "apply_sub")) {
644 item->apply_sub = *attr->value == '1' ? TRUE : FALSE;
645 } else if (!strcmp(attr->name, "last_seen")) {
646 if (!claws_crashed())
647 item->last_seen = atoi(attr->value);
648 else
649 item->last_seen = 0;
652 /* options without meaning in drafts */
653 if (item->stype == F_DRAFT)
654 item->hide_read_msgs =
655 item->hide_del_msgs =
656 item->hide_read_threads = FALSE;
659 XMLTag *folder_item_get_xml(Folder *folder, FolderItem *item)
661 static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
662 "draft", "queue", "trash"};
663 static gchar *sort_key_str[] = {"none", "number", "size", "date",
664 "from", "subject", "score", "label",
665 "mark", "unread", "mime", "to",
666 "locked", "tags", "thread_date" };
667 XMLTag *tag;
668 gchar *value;
670 tag = xml_tag_new("folderitem");
672 xml_tag_add_attr(tag, xml_attr_new("type", folder_item_stype_str[item->stype]));
673 if (item->name)
674 xml_tag_add_attr(tag, xml_attr_new("name", item->name));
675 if (item->path)
676 xml_tag_add_attr(tag, xml_attr_new("path", item->path));
677 if (item->no_sub)
678 xml_tag_add_attr(tag, xml_attr_new("no_sub", "1"));
679 if (item->no_select)
680 xml_tag_add_attr(tag, xml_attr_new("no_select", "1"));
681 xml_tag_add_attr(tag, xml_attr_new("collapsed", item->collapsed && item->node->children ? "1" : "0"));
682 xml_tag_add_attr(tag, xml_attr_new("thread_collapsed", item->thread_collapsed ? "1" : "0"));
683 xml_tag_add_attr(tag, xml_attr_new("threaded", item->threaded ? "1" : "0"));
684 xml_tag_add_attr(tag, xml_attr_new("hidereadmsgs", item->hide_read_msgs ? "1" : "0"));
685 xml_tag_add_attr(tag, xml_attr_new("hidedelmsgs", item->hide_del_msgs ? "1" : "0"));
686 xml_tag_add_attr(tag, xml_attr_new("hidereadthreads", item->hide_read_threads ? "1" : "0"));
687 if (item->ret_rcpt)
688 xml_tag_add_attr(tag, xml_attr_new("reqretrcpt", "1"));
690 if (item->sort_key != SORT_BY_NONE) {
691 xml_tag_add_attr(tag, xml_attr_new("sort_key", sort_key_str[item->sort_key]));
692 xml_tag_add_attr(tag, xml_attr_new("sort_type", item->sort_type == SORT_ASCENDING ? "ascending" : "descending"));
695 value = g_strdup_printf("%ld", (unsigned long int) item->mtime);
696 xml_tag_add_attr(tag, xml_attr_new("mtime", value));
697 g_free(value);
698 xml_tag_add_attr(tag, xml_attr_new_int("new", item->new_msgs));
699 xml_tag_add_attr(tag, xml_attr_new_int("unread", item->unread_msgs));
700 xml_tag_add_attr(tag, xml_attr_new_int("unreadmarked", item->unreadmarked_msgs));
701 xml_tag_add_attr(tag, xml_attr_new_int("marked", item->marked_msgs));
702 xml_tag_add_attr(tag, xml_attr_new_int("total", item->total_msgs));
703 xml_tag_add_attr(tag, xml_attr_new_int("replied", item->replied_msgs));
704 xml_tag_add_attr(tag, xml_attr_new_int("forwarded", item->forwarded_msgs));
705 xml_tag_add_attr(tag, xml_attr_new_int("locked", item->locked_msgs));
706 xml_tag_add_attr(tag, xml_attr_new_int("ignore", item->ignored_msgs));
707 xml_tag_add_attr(tag, xml_attr_new_int("watched", item->watched_msgs));
708 xml_tag_add_attr(tag, xml_attr_new_int("order", item->order));
710 if (item->account)
711 xml_tag_add_attr(tag, xml_attr_new_int("account_id", item->account->account_id));
712 if (item->apply_sub)
713 xml_tag_add_attr(tag, xml_attr_new("apply_sub", "1"));
715 xml_tag_add_attr(tag, xml_attr_new_int("last_seen", item->last_seen));
717 return tag;
720 void folder_set_ui_func(Folder *folder, FolderUIFunc func, gpointer data)
722 cm_return_if_fail(folder != NULL);
724 folder->ui_func = func;
725 folder->ui_func_data = data;
728 void folder_set_name(Folder *folder, const gchar *name)
730 cm_return_if_fail(folder != NULL);
732 g_free(folder->name);
733 folder->name = name ? g_strdup(name) : NULL;
734 if (folder->node && folder->node->data) {
735 FolderItem *item = (FolderItem *)folder->node->data;
737 g_free(item->name);
738 item->name = name ? g_strdup(name) : NULL;
742 void folder_set_sort(Folder *folder, guint sort)
744 cm_return_if_fail(folder != NULL);
746 if (folder->sort != sort) {
747 folder_remove(folder);
748 folder->sort = sort;
749 folder_add(folder);
753 static gboolean folder_tree_destroy_func(GNode *node, gpointer data) {
754 FolderItem *item = (FolderItem *) node->data;
756 folder_item_destroy(item);
757 return FALSE;
760 void folder_tree_destroy(Folder *folder)
762 GNode *node;
764 cm_return_if_fail(folder != NULL);
766 node = folder->node;
768 prefs_filtering_clear_folder(folder);
770 if (node != NULL) {
771 g_node_traverse(node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
772 folder_tree_destroy_func, NULL);
773 g_node_destroy(node);
774 folder->node = NULL;
778 void folder_add(Folder *folder)
780 Folder *cur_folder;
781 GList *cur;
782 gint i;
783 FolderUpdateData hookdata;
785 cm_return_if_fail(folder != NULL);
787 if ((FOLDER_TYPE(folder) == F_IMAP ||
788 FOLDER_TYPE(folder) == F_NEWS) &&
789 folder->account == NULL) {
790 return;
793 for (i = 0, cur = folder_list; cur != NULL; cur = cur->next, i++) {
794 cur_folder = FOLDER(cur->data);
795 if (cur_folder->sort < folder->sort)
796 break;
799 folder_list = g_list_insert(folder_list, folder, i);
801 hookdata.folder = folder;
802 hookdata.update_flags = FOLDER_ADD_FOLDER;
803 hookdata.item = NULL;
804 hookdata.item2 = NULL;
805 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
808 void folder_remove(Folder *folder)
810 FolderUpdateData hookdata;
812 cm_return_if_fail(folder != NULL);
814 folder_list = g_list_remove(folder_list, folder);
816 hookdata.folder = folder;
817 hookdata.update_flags = FOLDER_REMOVE_FOLDER;
818 hookdata.item = NULL;
819 hookdata.item2 = NULL;
820 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
823 GList *folder_get_list(void)
825 return folder_list;
828 gint folder_read_list(void)
830 GNode *node, *cur;
831 XMLNode *xmlnode;
832 gchar *path;
834 path = folder_get_list_path();
835 if (!is_file_exist(path)) return -1;
836 node = xml_parse_file(path);
837 if (!node) return -1;
839 xmlnode = node->data;
840 if (strcmp2(xmlnode->tag->tag, "folderlist") != 0) {
841 g_warning("wrong folder list");
842 xml_free_tree(node);
843 return -1;
846 cur = node->children;
847 while (cur != NULL) {
848 Folder *folder;
850 folder = folder_get_from_xml(cur);
851 if (folder != NULL)
852 folder_add(folder);
853 else
854 folder_unloaded_list = g_slist_append(folder_unloaded_list,
855 (gpointer) xml_copy_tree(cur));
856 cur = cur->next;
859 xml_free_tree(node);
860 if (folder_list || folder_unloaded_list)
861 return 0;
862 else
863 return -1;
866 void folder_write_list(void)
868 GList *list;
869 GSList *slist;
870 Folder *folder;
871 gchar *path;
872 PrefFile *pfile;
873 GNode *rootnode;
874 XMLNode *xmlnode;
875 XMLTag *tag;
877 path = folder_get_list_path();
878 if ((pfile = prefs_write_open(path)) == NULL) return;
880 if (xml_file_put_xml_decl(pfile->fp) < 0) {
881 prefs_file_close_revert(pfile);
882 g_warning("failed to start write folder list.");
883 return;
885 tag = xml_tag_new("folderlist");
887 xmlnode = xml_node_new(tag, NULL);
889 rootnode = g_node_new(xmlnode);
891 for (list = folder_list; list != NULL; list = list->next) {
892 GNode *node;
894 folder = list->data;
895 node = folder_get_xml_node(folder);
896 if (node != NULL)
897 g_node_append(rootnode, node);
900 for (slist = folder_unloaded_list; slist != NULL; slist = g_slist_next(slist)) {
901 GNode *node = (GNode *) slist->data;
903 g_node_append(rootnode, (gpointer) xml_copy_tree(node));
906 if (xml_write_tree(rootnode, pfile->fp) < 0) {
907 prefs_file_close_revert(pfile);
908 g_warning("failed to write folder list.");
909 } else if (prefs_file_close(pfile) < 0) {
910 g_warning("failed to write folder list.");
912 xml_free_tree(rootnode);
915 static gboolean folder_scan_tree_func(GNode *node, gpointer data)
917 GHashTable *pptable = (GHashTable *)data;
918 FolderItem *item = (FolderItem *)node->data;
920 folder_item_restore_persist_prefs(item, pptable);
921 folder_item_scan_full(item, FALSE);
923 return FALSE;
926 static gboolean folder_restore_prefs_func(GNode *node, gpointer data)
928 GHashTable *pptable = (GHashTable *)data;
929 FolderItem *item = (FolderItem *)node->data;
931 folder_item_restore_persist_prefs(item, pptable);
933 return FALSE;
936 void folder_scan_tree(Folder *folder, gboolean rebuild)
938 GHashTable *pptable;
939 FolderUpdateData hookdata;
940 Folder *old_folder = folder;
942 if (!folder->klass->scan_tree)
943 return;
945 pptable = folder_persist_prefs_new(folder);
947 if (rebuild)
948 folder_remove(folder);
950 if (folder->klass->scan_tree(folder) < 0) {
951 if (rebuild)
952 folder_add(old_folder);
953 return;
954 } else if (rebuild)
955 folder_add(folder);
957 hookdata.folder = folder;
958 hookdata.update_flags = FOLDER_TREE_CHANGED;
959 hookdata.item = NULL;
960 hookdata.item2 = NULL;
961 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
963 if (rebuild)
964 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_scan_tree_func, pptable);
965 else
966 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_restore_prefs_func, pptable);
968 folder_persist_prefs_free(pptable);
970 prefs_matcher_read_config();
972 folder_write_list();
975 FolderItem *folder_create_folder(FolderItem *parent, const gchar *name)
977 FolderItem *new_item;
979 cm_return_val_if_fail(parent != NULL, NULL);
981 new_item = parent->folder->klass->create_folder(parent->folder, parent, name);
982 if (new_item) {
983 FolderUpdateData hookdata;
985 new_item->cache = msgcache_new();
986 new_item->cache_dirty = TRUE;
987 new_item->mark_dirty = TRUE;
988 new_item->tags_dirty = TRUE;
990 hookdata.folder = new_item->folder;
991 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDERITEM;
992 hookdata.item = new_item;
993 hookdata.item2 = NULL;
994 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
997 return new_item;
1000 gint folder_item_rename(FolderItem *item, gchar *newname)
1002 gint retval;
1004 cm_return_val_if_fail(item != NULL, -1);
1005 cm_return_val_if_fail(newname != NULL, -1);
1007 retval = item->folder->klass->rename_folder(item->folder, item, newname);
1009 if (retval >= 0) {
1010 FolderItemUpdateData hookdata;
1011 FolderUpdateData hookdata2;
1013 hookdata.item = item;
1014 hookdata.update_flags = F_ITEM_UPDATE_NAME;
1015 hookdata.msg = NULL;
1016 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &hookdata);
1018 hookdata2.folder = item->folder;
1019 hookdata2.item = item;
1020 hookdata2.item2 = NULL;
1021 hookdata2.update_flags = FOLDER_RENAME_FOLDERITEM;
1022 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata2);
1025 return retval;
1028 struct TotalMsgCount
1030 guint new_msgs;
1031 guint unread_msgs;
1032 guint unreadmarked_msgs;
1033 guint marked_msgs;
1034 guint total_msgs;
1035 guint replied_msgs;
1036 guint forwarded_msgs;
1037 guint locked_msgs;
1038 guint ignored_msgs;
1039 guint watched_msgs;
1042 struct FuncToAllFoldersData
1044 FolderItemFunc function;
1045 gpointer data;
1048 static gboolean folder_func_to_all_folders_func(GNode *node, gpointer data)
1050 FolderItem *item;
1051 struct FuncToAllFoldersData *function_data = (struct FuncToAllFoldersData *) data;
1053 cm_return_val_if_fail(node->data != NULL, FALSE);
1055 item = FOLDER_ITEM(node->data);
1056 cm_return_val_if_fail(item != NULL, FALSE);
1058 function_data->function(item, function_data->data);
1060 return FALSE;
1063 void folder_func_to_all_folders(FolderItemFunc function, gpointer data)
1065 GList *list;
1066 Folder *folder;
1067 struct FuncToAllFoldersData function_data;
1069 function_data.function = function;
1070 function_data.data = data;
1072 for (list = folder_list; list != NULL; list = list->next) {
1073 folder = FOLDER(list->data);
1074 if (folder->node)
1075 g_node_traverse(folder->node, G_PRE_ORDER,
1076 G_TRAVERSE_ALL, -1,
1077 folder_func_to_all_folders_func,
1078 &function_data);
1082 static void folder_count_total_msgs_func(FolderItem *item, gpointer data)
1084 struct TotalMsgCount *count = (struct TotalMsgCount *)data;
1086 count->new_msgs += item->new_msgs;
1087 count->unread_msgs += item->unread_msgs;
1088 count->unreadmarked_msgs += item->unreadmarked_msgs;
1089 count->marked_msgs += item->marked_msgs;
1090 count->total_msgs += item->total_msgs;
1091 count->replied_msgs += item->replied_msgs;
1092 count->forwarded_msgs += item->forwarded_msgs;
1093 count->locked_msgs += item->locked_msgs;
1094 count->ignored_msgs += item->ignored_msgs;
1095 count->watched_msgs += item->watched_msgs;
1098 struct TotalMsgStatus
1100 guint new;
1101 guint unread;
1102 guint total;
1103 GString *str;
1106 static gboolean folder_get_status_full_all_func(GNode *node, gpointer data)
1108 FolderItem *item;
1109 struct TotalMsgStatus *status = (struct TotalMsgStatus *)data;
1110 gchar *id;
1112 cm_return_val_if_fail(node->data != NULL, FALSE);
1114 item = FOLDER_ITEM(node->data);
1116 if (!item->path) return FALSE;
1118 status->new += item->new_msgs;
1119 status->unread += item->unread_msgs;
1120 status->total += item->total_msgs;
1122 if (status->str) {
1123 id = folder_item_get_identifier(item);
1124 g_string_append_printf(status->str, "%5d %5d %5d %s\n",
1125 item->new_msgs, item->unread_msgs,
1126 item->total_msgs, id);
1127 g_free(id);
1130 return FALSE;
1133 static void folder_get_status_full_all(GString *str, guint *new, guint *unread,
1134 guint *total)
1136 GList *list;
1137 Folder *folder;
1138 struct TotalMsgStatus status;
1140 status.new = status.unread = status.total = 0;
1141 status.str = str;
1143 debug_print("Counting total number of messages...\n");
1145 for (list = folder_list; list != NULL; list = list->next) {
1146 folder = FOLDER(list->data);
1147 if (folder->node)
1148 g_node_traverse(folder->node, G_PRE_ORDER,
1149 G_TRAVERSE_ALL, -1,
1150 folder_get_status_full_all_func,
1151 &status);
1154 *new = status.new;
1155 *unread = status.unread;
1156 *total = status.total;
1159 gchar *folder_get_status(GPtrArray *folders, gboolean full)
1161 guint new, unread, total;
1162 GString *str;
1163 gint i;
1164 gchar *ret;
1166 new = unread = total = 0;
1168 str = g_string_new(NULL);
1170 if (folders) {
1171 for (i = 0; i < folders->len; i++) {
1172 FolderItem *item;
1174 item = g_ptr_array_index(folders, i);
1175 new += item->new_msgs;
1176 unread += item->unread_msgs;
1177 total += item->total_msgs;
1179 if (full) {
1180 gchar *id;
1182 id = folder_item_get_identifier(item);
1183 g_string_append_printf(str, "%5d %5d %5d %s\n",
1184 item->new_msgs, item->unread_msgs,
1185 item->total_msgs, id);
1186 g_free(id);
1189 } else {
1190 folder_get_status_full_all(full ? str : NULL,
1191 &new, &unread, &total);
1194 if (full)
1195 g_string_append_printf(str, "%5d %5d %5d\n", new, unread, total);
1196 else
1197 g_string_append_printf(str, "%d %d %d\n", new, unread, total);
1199 ret = str->str;
1200 g_string_free(str, FALSE);
1202 return ret;
1205 void folder_count_total_msgs(guint *new_msgs, guint *unread_msgs,
1206 guint *unreadmarked_msgs, guint *marked_msgs,
1207 guint *total_msgs, guint *replied_msgs,
1208 guint *forwarded_msgs, guint *locked_msgs,
1209 guint *ignored_msgs, guint *watched_msgs)
1211 struct TotalMsgCount count;
1213 count.new_msgs = count.unread_msgs = count.unreadmarked_msgs = 0;
1214 count.total_msgs = count.replied_msgs = count.forwarded_msgs = 0;
1215 count.locked_msgs = count.ignored_msgs = count.watched_msgs = 0;
1216 count.marked_msgs = 0;
1218 debug_print("Counting total number of messages...\n");
1220 folder_func_to_all_folders(folder_count_total_msgs_func, &count);
1222 *new_msgs = count.new_msgs;
1223 *unread_msgs = count.unread_msgs;
1224 *unreadmarked_msgs = count.unreadmarked_msgs;
1225 *marked_msgs = count.marked_msgs;
1226 *total_msgs = count.total_msgs;
1227 *replied_msgs = count.replied_msgs;
1228 *forwarded_msgs = count.forwarded_msgs;
1229 *locked_msgs = count.locked_msgs;
1230 *ignored_msgs = count.ignored_msgs;
1231 *watched_msgs = count.watched_msgs;
1234 Folder *folder_find_from_path(const gchar *path)
1236 GList *list;
1237 Folder *folder;
1239 for (list = folder_list; list != NULL; list = list->next) {
1240 folder = list->data;
1241 if ((FOLDER_TYPE(folder) == F_MH ||
1242 FOLDER_TYPE(folder) == F_MBOX) &&
1243 !path_cmp(LOCAL_FOLDER(folder)->rootpath, path))
1244 return folder;
1247 return NULL;
1250 Folder *folder_find_from_name(const gchar *name, FolderClass *klass)
1252 GList *list;
1253 Folder *folder;
1255 for (list = folder_list; list != NULL; list = list->next) {
1256 folder = list->data;
1257 if (folder->klass == klass &&
1258 strcmp2(name, folder->name) == 0)
1259 return folder;
1262 return NULL;
1265 static gboolean folder_item_find_func(GNode *node, gpointer data)
1267 FolderItem *item = node->data;
1268 gpointer *d = data;
1269 const gchar *path = d[0];
1271 if (path_cmp(path, item->path) != 0)
1272 return FALSE;
1274 d[1] = item;
1276 return TRUE;
1279 FolderItem *folder_find_item_from_path(const gchar *path)
1281 Folder *folder;
1282 gpointer d[2];
1283 GList *list = folder_get_list();
1285 folder = list ? list->data:NULL;
1287 cm_return_val_if_fail(folder != NULL, NULL);
1289 d[0] = (gpointer)path;
1290 d[1] = NULL;
1291 while (d[1] == NULL && list) {
1292 folder = FOLDER(list->data);
1293 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1294 folder_item_find_func, d);
1295 list = list->next;
1297 return d[1];
1300 static gboolean folder_item_find_func_real_path(GNode *node, gpointer data)
1302 FolderItem *item = node->data;
1303 gpointer *d = data;
1304 const gchar *path = d[0];
1305 gchar *tmp = folder_item_get_path(item);
1306 if (path_cmp(path, tmp) != 0) {
1307 g_free(tmp);
1308 return FALSE;
1310 g_free(tmp);
1311 d[1] = item;
1313 return TRUE;
1316 FolderItem *folder_find_item_from_real_path(const gchar *path)
1318 Folder *folder;
1319 gpointer d[2];
1320 GList *list = folder_get_list();
1322 folder = list ? list->data:NULL;
1324 cm_return_val_if_fail(folder != NULL, NULL);
1326 d[0] = (gpointer)path;
1327 d[1] = NULL;
1328 while (d[1] == NULL && list) {
1329 folder = FOLDER(list->data);
1330 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1331 folder_item_find_func_real_path, d);
1332 list = list->next;
1334 return d[1];
1337 FolderItem *folder_find_child_item_by_name(FolderItem *item, const gchar *name)
1339 GNode *node;
1340 FolderItem *child;
1342 for (node = item->node->children; node != NULL; node = node->next) {
1343 child = FOLDER_ITEM(node->data);
1344 if (strcmp2(child->name, name) == 0) {
1345 return child;
1349 return NULL;
1352 FolderClass *folder_get_class_from_string(const gchar *str)
1354 GSList *classlist;
1356 classlist = folder_get_class_list();
1357 for (; classlist != NULL; classlist = g_slist_next(classlist)) {
1358 FolderClass *class = (FolderClass *) classlist->data;
1359 if (g_ascii_strcasecmp(class->idstr, str) == 0)
1360 return class;
1363 return NULL;
1366 gchar *folder_get_identifier(Folder *folder)
1368 gchar *type_str;
1370 cm_return_val_if_fail(folder != NULL, NULL);
1372 type_str = folder->klass->idstr;
1373 return g_strconcat("#", type_str, "/", folder->name, NULL);
1376 gchar *folder_item_get_identifier(FolderItem *item)
1378 gchar *id = NULL;
1379 gchar *folder_id = NULL;
1381 cm_return_val_if_fail(item != NULL, NULL);
1383 if (item->path == NULL)
1384 return NULL;
1386 folder_id = folder_get_identifier(item->folder);
1387 id = g_strconcat(folder_id, "/", item->path, NULL);
1388 g_free(folder_id);
1390 return id;
1393 Folder *folder_find_from_identifier(const gchar *identifier)
1395 gchar *str;
1396 gchar *p;
1397 gchar *name;
1398 FolderClass *class;
1400 cm_return_val_if_fail(identifier != NULL, NULL);
1402 if (*identifier != '#')
1403 return NULL;
1405 Xstrdup_a(str, identifier, return NULL);
1407 p = strchr(str, '/');
1408 if (!p)
1409 return NULL;
1410 *p = '\0';
1411 p++;
1412 class = folder_get_class_from_string(&str[1]);
1413 if (class == NULL)
1414 return NULL;
1416 name = p;
1417 p = strchr(p, '/');
1418 if (p)
1419 return NULL;
1421 return folder_find_from_name(name, class);
1424 FolderItem *folder_find_item_from_identifier(const gchar *identifier)
1426 Folder *folder;
1427 gpointer d[2];
1428 gchar *str;
1429 gchar *p;
1430 gchar *name;
1431 gchar *path;
1432 FolderClass *class;
1434 cm_return_val_if_fail(identifier != NULL, NULL);
1436 if (*identifier != '#')
1437 return folder_find_item_from_path(identifier);
1439 Xstrdup_a(str, identifier, return NULL);
1441 p = strchr(str, '/');
1442 if (!p)
1443 return folder_find_item_from_path(identifier);
1444 *p = '\0';
1445 p++;
1446 class = folder_get_class_from_string(&str[1]);
1447 if (class == NULL)
1448 return folder_find_item_from_path(identifier);
1450 name = p;
1451 p = strchr(p, '/');
1452 if (!p)
1453 return folder_find_item_from_path(identifier);
1454 *p = '\0';
1455 p++;
1457 folder = folder_find_from_name(name, class);
1458 if (!folder)
1459 return folder_find_item_from_path(identifier);
1461 path = p;
1463 d[0] = (gpointer)path;
1464 d[1] = NULL;
1465 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1466 folder_item_find_func, d);
1467 return d[1];
1470 /** Returns the FolderItem from a given identifier
1472 * The FolderItem is created if it doesn't already exist.
1473 * If creation failed, the function returns NULL.
1475 * Identifiers are of the form #type/Mailbox/FolderA/FolderB/FolderC
1477 FolderItem *folder_get_item_from_identifier(const gchar *identifier)
1479 FolderItem *item, *last_parent;
1480 Folder *folder;
1481 gchar *p1, *p2, *str;
1482 size_t len;
1483 FolderClass *class;
1484 gboolean created_something = FALSE;
1486 item = folder_find_item_from_identifier(identifier);
1487 if(item)
1488 return item;
1490 /* trivial sanity check: need at least # and two slashes */
1491 len = strlen(identifier);
1492 if(len < 3)
1493 return NULL;
1495 /* make sure identifier ends with a slash */
1496 if(identifier[len-1] == G_DIR_SEPARATOR) {
1497 Xstrdup_a(str, identifier, return NULL);
1499 else {
1500 Xstrndup_a(str, identifier, len+1, return NULL);
1501 str[len] = G_DIR_SEPARATOR;
1504 /* find folder class */
1505 p1 = strchr(str, G_DIR_SEPARATOR);
1506 if(!p1)
1507 return NULL;
1508 *p1 = '\0';
1509 class = folder_get_class_from_string(&str[1]);
1510 if(!class)
1511 return NULL;
1512 *p1 = G_DIR_SEPARATOR;
1513 ++p1;
1515 /* find folder from class and name */
1516 p2 = strchr(p1, G_DIR_SEPARATOR);
1517 if(!p2)
1518 return NULL;
1519 *p2 = '\0';
1520 folder = folder_find_from_name(p1, class);
1521 if(!folder)
1522 return NULL;
1523 *p2 = G_DIR_SEPARATOR;
1524 ++p2;
1525 p1 = p2;
1527 /* Now, move forward and make sure all sections in the path exist */
1528 last_parent = folder->node->data;
1529 while((p1 = strchr(p1, G_DIR_SEPARATOR)) != NULL) {
1530 *p1 = '\0';
1531 item = folder_find_item_from_identifier(str);
1532 if(!item) {
1533 item = folder_create_folder(last_parent, p2);
1534 if(!item)
1535 return NULL;
1536 debug_print("Created folder '%s'\n", str);
1537 created_something = TRUE;
1538 if(prefs_common.inherit_folder_props && (last_parent != item->folder->node->data)) {
1539 folder_item_prefs_copy_prefs(last_parent, item);
1542 last_parent = item;
1543 *p1 = G_DIR_SEPARATOR;
1544 ++p1;
1545 p2 = p1;
1548 if(created_something)
1549 folder_write_list();
1551 return item;
1556 * Get a displayable name for a FolderItem
1558 * \param item FolderItem for that a name should be created
1559 * \return Displayable name for item, returned string has to
1560 * be freed
1562 gchar *folder_item_get_name(FolderItem *item)
1564 gchar *name = NULL;
1566 cm_return_val_if_fail(item != NULL, g_strdup(""));
1568 switch (item->stype) {
1569 case F_INBOX:
1570 name = g_strdup(!strcmp2(item->name, INBOX_DIR) ? _("Inbox") :
1571 item->name);
1572 break;
1573 case F_OUTBOX:
1574 name = g_strdup(!strcmp2(item->name, OUTBOX_DIR) ? _("Sent") :
1575 item->name);
1576 break;
1577 case F_QUEUE:
1578 name = g_strdup(!strcmp2(item->name, QUEUE_DIR) ? _("Queue") :
1579 item->name);
1580 break;
1581 case F_TRASH:
1582 name = g_strdup(!strcmp2(item->name, TRASH_DIR) ? _("Trash") :
1583 item->name);
1584 break;
1585 case F_DRAFT:
1586 name = g_strdup(!strcmp2(item->name, DRAFT_DIR) ? _("Drafts") :
1587 item->name);
1588 break;
1589 default:
1590 break;
1593 if (name == NULL) {
1595 * should probably be done by a virtual function,
1596 * the folder knows the ui string and how to abbrev
1598 if (folder_item_parent(item) == NULL) {
1599 name = g_strconcat(item->name, " (", item->folder->klass->uistr, ")", NULL);
1600 } else {
1601 if (FOLDER_CLASS(item->folder) == news_get_class() &&
1602 item->path && !strcmp2(item->name, item->path))
1603 name = get_abbrev_newsgroup_name
1604 (item->path,
1605 prefs_common.ng_abbrev_len);
1606 else
1607 name = g_strdup(item->name);
1611 if (name == NULL)
1612 name = g_strdup("");
1614 return name;
1617 gboolean folder_have_mailbox (void)
1619 GList *cur;
1620 for (cur = folder_list; cur != NULL; cur = g_list_next(cur)) {
1621 Folder *folder = FOLDER(cur->data);
1622 if (folder->inbox && folder->outbox)
1623 return TRUE;
1625 return FALSE;
1628 FolderItem *folder_get_default_inbox(void)
1630 GList *flist;
1632 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1633 Folder * folder = FOLDER(flist->data);
1635 if (folder == NULL)
1636 continue;
1637 if (folder->inbox == NULL)
1638 continue;
1639 if (folder->klass->type == F_UNKNOWN)
1640 continue;
1642 return folder->inbox;
1645 return NULL;
1648 FolderItem *folder_get_default_inbox_for_class(FolderType type)
1650 GList *flist;
1652 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1653 Folder * folder = FOLDER(flist->data);
1655 if (folder == NULL)
1656 continue;
1657 if (folder->inbox == NULL)
1658 continue;
1659 if (folder->klass->type != type)
1660 continue;
1662 return folder->inbox;
1665 return NULL;
1668 FolderItem *folder_get_default_outbox(void)
1670 GList *flist;
1672 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1673 Folder * folder = FOLDER(flist->data);
1675 if (folder == NULL)
1676 continue;
1677 if (folder->outbox == NULL)
1678 continue;
1679 if (folder->klass->type == F_UNKNOWN)
1680 continue;
1682 return folder->outbox;
1685 return NULL;
1688 FolderItem *folder_get_default_outbox_for_class(FolderType type)
1690 GList *flist;
1692 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1693 Folder * folder = FOLDER(flist->data);
1695 if (folder == NULL)
1696 continue;
1697 if (folder->outbox == NULL)
1698 continue;
1699 if (folder->klass->type != type)
1700 continue;
1702 return folder->outbox;
1705 return NULL;
1708 FolderItem *folder_get_default_draft(void)
1710 GList *flist;
1712 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1713 Folder * folder = FOLDER(flist->data);
1715 if (folder == NULL)
1716 continue;
1717 if (folder->draft == NULL)
1718 continue;
1719 if (folder->klass->type == F_UNKNOWN)
1720 continue;
1722 return folder->draft;
1725 return NULL;
1728 FolderItem *folder_get_default_draft_for_class(FolderType type)
1730 GList *flist;
1732 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1733 Folder * folder = FOLDER(flist->data);
1735 if (folder == NULL)
1736 continue;
1737 if (folder->draft == NULL)
1738 continue;
1739 if (folder->klass->type != type)
1740 continue;
1742 return folder->draft;
1745 return NULL;
1748 FolderItem *folder_get_default_queue(void)
1750 GList *flist;
1752 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1753 Folder * folder = FOLDER(flist->data);
1755 if (folder == NULL)
1756 continue;
1757 if (folder->queue == NULL)
1758 continue;
1759 if (folder->klass->type == F_UNKNOWN)
1760 continue;
1762 return folder->queue;
1765 return NULL;
1768 FolderItem *folder_get_default_queue_for_class(FolderType type)
1770 GList *flist;
1772 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1773 Folder * folder = FOLDER(flist->data);
1775 if (folder == NULL)
1776 continue;
1777 if (folder->queue == NULL)
1778 continue;
1779 if (folder->klass->type != type)
1780 continue;
1782 return folder->queue;
1785 return NULL;
1788 FolderItem *folder_get_default_trash(void)
1790 GList *flist;
1792 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1793 Folder * folder = FOLDER(flist->data);
1795 if (folder == NULL)
1796 continue;
1797 if (folder->trash == NULL)
1798 continue;
1799 if (folder->klass->type == F_UNKNOWN)
1800 continue;
1802 return folder->trash;
1805 return NULL;
1808 FolderItem *folder_get_default_trash_for_class(FolderType type)
1810 GList *flist;
1812 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1813 Folder * folder = FOLDER(flist->data);
1815 if (folder == NULL)
1816 continue;
1817 if (folder->trash == NULL)
1818 continue;
1819 if (folder->klass->type != type)
1820 continue;
1822 return folder->trash;
1825 return NULL;
1828 #define CREATE_FOLDER_IF_NOT_EXIST(member, dir, type) \
1830 if (!folder->member) { \
1831 item = folder_item_new(folder, dir, dir); \
1832 item->stype = type; \
1833 folder_item_append(rootitem, item); \
1834 folder->member = item; \
1838 void folder_set_missing_folders(void)
1840 Folder *folder;
1841 FolderItem *rootitem;
1842 FolderItem *item;
1843 GList *list;
1845 for (list = folder_list; list != NULL; list = list->next) {
1846 folder = list->data;
1847 if (FOLDER_TYPE(folder) != F_MH) continue;
1848 rootitem = FOLDER_ITEM(folder->node->data);
1849 cm_return_if_fail(rootitem != NULL);
1851 if (folder->inbox && folder->outbox && folder->draft &&
1852 folder->queue && folder->trash)
1853 continue;
1855 if (folder->klass->create_tree(folder) < 0) {
1856 g_warning("%s: can't create the folder tree.",
1857 LOCAL_FOLDER(folder)->rootpath);
1858 continue;
1861 CREATE_FOLDER_IF_NOT_EXIST(inbox, INBOX_DIR, F_INBOX);
1862 CREATE_FOLDER_IF_NOT_EXIST(outbox, OUTBOX_DIR, F_OUTBOX);
1863 CREATE_FOLDER_IF_NOT_EXIST(draft, DRAFT_DIR, F_DRAFT);
1864 CREATE_FOLDER_IF_NOT_EXIST(queue, QUEUE_DIR, F_QUEUE);
1865 CREATE_FOLDER_IF_NOT_EXIST(trash, TRASH_DIR, F_TRASH);
1869 static gboolean folder_unref_account_func(GNode *node, gpointer data)
1871 FolderItem *item = node->data;
1872 PrefsAccount *account = data;
1874 if (item->account == account)
1875 item->account = NULL;
1877 return FALSE;
1880 void folder_unref_account_all(PrefsAccount *account)
1882 Folder *folder;
1883 GList *list;
1885 if (!account) return;
1887 for (list = folder_list; list != NULL; list = list->next) {
1888 folder = list->data;
1889 if (folder->account == account)
1890 folder->account = NULL;
1891 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1892 folder_unref_account_func, account);
1896 #undef CREATE_FOLDER_IF_NOT_EXIST
1898 gchar *folder_item_get_path(FolderItem *item)
1900 Folder *folder;
1902 cm_return_val_if_fail(item != NULL, NULL);
1903 folder = item->folder;
1904 cm_return_val_if_fail(folder != NULL, NULL);
1906 return folder->klass->item_get_path(folder, item);
1909 static gint folder_sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1911 MsgInfo *msginfo_a = (MsgInfo *) a;
1912 MsgInfo *msginfo_b = (MsgInfo *) b;
1914 return (msginfo_a->msgnum - msginfo_b->msgnum);
1917 static gint folder_sort_folder_list(gconstpointer a, gconstpointer b)
1919 guint gint_a = GPOINTER_TO_INT(a);
1920 guint gint_b = GPOINTER_TO_INT(b);
1922 return (gint_a - gint_b);
1925 static gint syncronize_flags(FolderItem *item, MsgInfoList *msglist)
1927 GHashTable *relation;
1928 gint ret = 0;
1929 GSList *cur;
1931 if(msglist == NULL)
1932 return 0;
1933 if(item->folder->klass->get_flags == NULL)
1934 return 0;
1935 if (item->no_select)
1936 return 0;
1938 relation = g_hash_table_new(g_direct_hash, g_direct_equal);
1939 if ((ret = item->folder->klass->get_flags(
1940 item->folder, item, msglist, relation)) == 0) {
1941 gpointer data, old_key;
1942 MsgInfo *msginfo;
1943 MsgPermFlags permflags = 0;
1945 folder_item_update_freeze();
1946 folder_item_set_batch(item, TRUE);
1947 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1948 msginfo = (MsgInfo *) cur->data;
1950 if (g_hash_table_lookup_extended(relation, msginfo, &old_key, &data)) {
1951 permflags = GPOINTER_TO_INT(data);
1953 if (msginfo->flags.perm_flags != permflags) {
1954 procmsg_msginfo_change_flags(msginfo,
1955 permflags & ~msginfo->flags.perm_flags, 0,
1956 ~permflags & msginfo->flags.perm_flags, 0);
1960 folder_item_set_batch(item, FALSE);
1961 folder_item_update_thaw();
1963 g_hash_table_destroy(relation);
1965 return ret;
1968 static gint folder_item_syncronize_flags(FolderItem *item)
1970 MsgInfoList *msglist = NULL;
1971 GSList *cur;
1972 gint ret = 0;
1974 cm_return_val_if_fail(item != NULL, -1);
1975 cm_return_val_if_fail(item->folder != NULL, -1);
1976 cm_return_val_if_fail(item->folder->klass != NULL, -1);
1977 if (item->no_select)
1978 return -1;
1980 item->scanning = ITEM_SCANNING_WITH_FLAGS;
1982 if (item->cache == NULL)
1983 folder_item_read_cache(item);
1985 msglist = msgcache_get_msg_list(item->cache);
1987 ret = syncronize_flags(item, msglist);
1989 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1990 procmsg_msginfo_free((MsgInfo **)&(cur->data));
1993 g_slist_free(msglist);
1995 item->scanning = ITEM_NOT_SCANNING;
1997 return ret;
2000 static void folder_item_process_open (FolderItem *item,
2001 void (*before_proc_func)(gpointer data),
2002 void (*after_proc_func)(gpointer data),
2003 gpointer data)
2005 gchar *buf;
2006 if (item == NULL)
2007 return;
2008 if((item->folder->klass->scan_required != NULL) &&
2009 (item->folder->klass->scan_required(item->folder, item))) {
2010 folder_item_scan_full(item, TRUE);
2011 } else {
2012 folder_item_syncronize_flags(item);
2015 /* Processing */
2016 if (item->prefs->enable_processing_when_opening) {
2017 buf = g_strdup_printf(_("Processing (%s)...\n"),
2018 item->path ? item->path : item->name);
2019 g_free(buf);
2021 if (before_proc_func)
2022 before_proc_func(data);
2024 folder_item_apply_processing(item);
2026 if (after_proc_func)
2027 after_proc_func(data);
2029 item->processing_pending = FALSE;
2030 return;
2033 gint folder_item_open(FolderItem *item)
2035 START_TIMING("");
2036 if (item->no_select)
2037 return -1;
2039 if (item->scanning != ITEM_NOT_SCANNING) {
2040 debug_print("%s is scanning... \n", item->path ? item->path : item->name);
2041 return -2;
2044 item->processing_pending = TRUE;
2045 folder_item_process_open (item, NULL, NULL, NULL);
2047 item->opened = TRUE;
2048 END_TIMING();
2049 return 0;
2052 gint folder_item_close(FolderItem *item)
2054 GSList *mlist, *cur;
2055 Folder *folder;
2057 cm_return_val_if_fail(item != NULL, -1);
2059 if (item->no_select)
2060 return -1;
2062 if (item->new_msgs) {
2063 folder_item_update_freeze();
2064 mlist = folder_item_get_msg_list(item);
2065 for (cur = mlist ; cur != NULL ; cur = cur->next) {
2066 MsgInfo * msginfo;
2068 msginfo = (MsgInfo *) cur->data;
2069 if (MSG_IS_NEW(msginfo->flags))
2070 procmsg_msginfo_unset_flags(msginfo, MSG_NEW, 0);
2071 procmsg_msginfo_free(&msginfo);
2073 g_slist_free(mlist);
2074 folder_item_update_thaw();
2077 folder_item_write_cache(item);
2079 folder_item_update(item, F_ITEM_UPDATE_MSGCNT);
2081 item->opened = FALSE;
2082 folder = item->folder;
2084 if (folder->klass->close == NULL)
2085 return 0;
2087 return folder->klass->close(folder, item);
2090 static MsgInfoList *get_msginfos(FolderItem *item, MsgNumberList *numlist)
2092 MsgInfoList *msglist = NULL;
2093 Folder *folder = item->folder;
2094 if (item->no_select)
2095 return NULL;
2097 if (folder->klass->get_msginfos != NULL)
2098 msglist = folder->klass->get_msginfos(folder, item, numlist);
2099 else {
2100 MsgNumberList *elem;
2102 for (elem = numlist; elem != NULL; elem = g_slist_next(elem)) {
2103 MsgInfo *msginfo;
2104 guint num;
2106 num = GPOINTER_TO_INT(elem->data);
2107 msginfo = folder->klass->get_msginfo(folder, item, num);
2108 if (msginfo != NULL)
2109 msglist = g_slist_prepend(msglist, msginfo);
2113 return msglist;
2116 static MsgInfo *get_msginfo(FolderItem *item, guint num)
2118 MsgNumberList numlist;
2119 MsgInfoList *msglist;
2120 MsgInfo *msginfo = NULL;
2122 numlist.data = GINT_TO_POINTER(num);
2123 numlist.next = NULL;
2124 msglist = get_msginfos(item, &numlist);
2125 if (msglist != NULL)
2126 msginfo = procmsg_msginfo_new_ref(msglist->data);
2127 procmsg_msg_list_free(msglist);
2129 return msginfo;
2132 gint folder_item_scan_full(FolderItem *item, gboolean filtering)
2134 Folder *folder;
2135 GSList *folder_list = NULL, *cache_list = NULL;
2136 GSList *folder_list_cur, *cache_list_cur, *new_list = NULL;
2137 GSList *exists_list = NULL, *elem;
2138 GSList *newmsg_list = NULL;
2139 guint newcnt = 0, unreadcnt = 0, totalcnt = 0;
2140 guint markedcnt = 0, unreadmarkedcnt = 0;
2141 guint repliedcnt = 0, forwardedcnt = 0;
2142 guint lockedcnt = 0, ignoredcnt = 0, watchedcnt = 0;
2144 guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
2145 gboolean update_flags = 0, old_uids_valid = FALSE;
2146 GHashTable *subject_table = NULL;
2148 cm_return_val_if_fail(item != NULL, -1);
2149 if (item->path == NULL) return -1;
2151 folder = item->folder;
2153 cm_return_val_if_fail(folder != NULL, -1);
2154 cm_return_val_if_fail(folder->klass->get_num_list != NULL, -1);
2156 item->scanning = ITEM_SCANNING_WITH_FLAGS;
2158 debug_print("Scanning folder %s for cache changes.\n", item->path ? item->path : "(null)");
2160 /* Get list of messages for folder and cache */
2161 if (folder->klass->get_num_list(item->folder, item, &folder_list, &old_uids_valid) < 0) {
2162 debug_print("Error fetching list of message numbers\n");
2163 item->scanning = ITEM_NOT_SCANNING;
2164 return(-1);
2167 if(prefs_common.thread_by_subject) {
2168 subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2171 if (old_uids_valid) {
2172 if (!item->cache)
2173 folder_item_read_cache(item);
2174 cache_list = msgcache_get_msg_list(item->cache);
2175 } else {
2176 if (item->cache)
2177 msgcache_destroy(item->cache);
2178 item->cache = msgcache_new();
2179 item->cache_dirty = TRUE;
2180 item->mark_dirty = TRUE;
2181 item->tags_dirty = TRUE;
2182 cache_list = NULL;
2185 /* Sort both lists */
2186 cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
2187 folder_list = g_slist_sort(folder_list, folder_sort_folder_list);
2189 cache_list_cur = cache_list;
2190 folder_list_cur = folder_list;
2192 if (cache_list_cur != NULL) {
2193 GSList *cache_list_last;
2195 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
2196 cache_list_last = g_slist_last(cache_list);
2197 cache_max_num = ((MsgInfo *)cache_list_last->data)->msgnum;
2198 } else {
2199 cache_cur_num = G_MAXUINT;
2200 cache_max_num = 0;
2203 if (folder_list_cur != NULL) {
2204 GSList *folder_list_last;
2206 folder_cur_num = GPOINTER_TO_UINT(folder_list_cur->data);
2207 folder_list_last = g_slist_last(folder_list);
2208 folder_max_num = GPOINTER_TO_UINT(folder_list_last->data);
2209 } else {
2210 folder_cur_num = G_MAXUINT;
2211 folder_max_num = 0;
2214 while ((cache_cur_num != G_MAXUINT) || (folder_cur_num != G_MAXUINT)) {
2216 * Message only exists in the folder
2217 * Remember message for fetching
2219 if (folder_cur_num < cache_cur_num) {
2220 gboolean add = FALSE;
2222 switch(FOLDER_TYPE(folder)) {
2223 case F_NEWS:
2224 if (folder_cur_num < cache_max_num)
2225 break;
2227 if (folder->account->max_articles == 0) {
2228 add = TRUE;
2231 if (folder_max_num <= folder->account->max_articles) {
2232 add = TRUE;
2233 } else if (folder_cur_num > (folder_max_num - folder->account->max_articles)) {
2234 add = TRUE;
2236 break;
2237 default:
2238 add = TRUE;
2239 break;
2242 if (add) {
2243 new_list = g_slist_prepend(new_list, GUINT_TO_POINTER(folder_cur_num));
2244 debug_print("Remembered message %u for fetching\n", folder_cur_num);
2247 /* Move to next folder number */
2248 if (folder_list_cur)
2249 folder_list_cur = folder_list_cur->next;
2251 if (folder_list_cur != NULL)
2252 folder_cur_num = GPOINTER_TO_UINT(folder_list_cur->data);
2253 else
2254 folder_cur_num = G_MAXUINT;
2256 continue;
2260 * Message only exists in the cache
2261 * Remove the message from the cache
2263 if (cache_cur_num < folder_cur_num) {
2264 msgcache_remove_msg(item->cache, cache_cur_num);
2265 debug_print("Removed message %u from cache.\n", cache_cur_num);
2267 /* Move to next cache number */
2268 if (cache_list_cur)
2269 cache_list_cur = cache_list_cur->next;
2271 if (cache_list_cur != NULL)
2272 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
2273 else
2274 cache_cur_num = G_MAXUINT;
2276 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
2278 continue;
2282 * Message number exists in folder and cache!
2283 * Check if the message has been modified
2285 if (cache_cur_num == folder_cur_num) {
2286 MsgInfo *msginfo;
2288 msginfo = msgcache_get_msg(item->cache, folder_cur_num);
2289 if (msginfo && folder->klass->is_msg_changed && folder->klass->is_msg_changed(folder, item, msginfo)) {
2290 msgcache_remove_msg(item->cache, msginfo->msgnum);
2291 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(msginfo->msgnum));
2292 procmsg_msginfo_free(&msginfo);
2294 debug_print("Remembering message %u to update...\n", folder_cur_num);
2295 } else if (msginfo) {
2296 exists_list = g_slist_prepend(exists_list, msginfo);
2298 if(prefs_common.thread_by_subject &&
2299 MSG_IS_IGNORE_THREAD(msginfo->flags) &&
2300 !subject_table_lookup(subject_table, msginfo->subject)) {
2301 subject_table_insert(subject_table, msginfo->subject, msginfo);
2305 /* Move to next folder and cache number */
2306 if (cache_list_cur)
2307 cache_list_cur = cache_list_cur->next;
2309 if (folder_list_cur)
2310 folder_list_cur = folder_list_cur->next;
2312 if (cache_list_cur != NULL)
2313 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
2314 else
2315 cache_cur_num = G_MAXUINT;
2317 if (folder_list_cur != NULL)
2318 folder_cur_num = GPOINTER_TO_UINT(folder_list_cur->data);
2319 else
2320 folder_cur_num = G_MAXUINT;
2322 continue;
2326 for(cache_list_cur = cache_list; cache_list_cur != NULL; cache_list_cur = g_slist_next(cache_list_cur))
2327 procmsg_msginfo_free((MsgInfo **)&(cache_list_cur->data));
2329 g_slist_free(cache_list);
2330 g_slist_free(folder_list);
2332 if (new_list != NULL) {
2333 GSList *tmp_list = NULL;
2334 newmsg_list = get_msginfos(item, new_list);
2335 g_slist_free(new_list);
2336 tmp_list = g_slist_concat(g_slist_copy(exists_list), g_slist_copy(newmsg_list));
2337 syncronize_flags(item, tmp_list);
2338 g_slist_free(tmp_list);
2339 } else {
2340 syncronize_flags(item, exists_list);
2343 folder_item_update_freeze();
2345 item->scanning = ITEM_SCANNING;
2347 if (newmsg_list != NULL) {
2348 GSList *elem, *to_filter = NULL;
2349 gboolean do_filter = (filtering == TRUE) &&
2350 (item->stype == F_INBOX) &&
2351 (item->folder->account != NULL) &&
2352 (item->folder->account->filter_on_recv);
2354 for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
2355 MsgInfo *msginfo = (MsgInfo *) elem->data;
2357 msgcache_add_msg(item->cache, msginfo);
2358 if (!do_filter) {
2359 exists_list = g_slist_prepend(exists_list, msginfo);
2361 if(prefs_common.thread_by_subject &&
2362 MSG_IS_IGNORE_THREAD(msginfo->flags) &&
2363 !subject_table_lookup(subject_table, msginfo->subject)) {
2364 subject_table_insert(subject_table, msginfo->subject, msginfo);
2369 if (do_filter) {
2370 GSList *unfiltered;
2372 folder_item_set_batch(item, TRUE);
2373 procmsg_msglist_filter(newmsg_list, item->folder->account,
2374 &to_filter, &unfiltered,
2375 TRUE);
2376 folder_item_set_batch(item, FALSE);
2378 filtering_move_and_copy_msgs(newmsg_list);
2379 if (to_filter != NULL) {
2380 for (elem = to_filter; elem; elem = g_slist_next(elem)) {
2381 MsgInfo *msginfo = (MsgInfo *)elem->data;
2382 procmsg_msginfo_free(&msginfo);
2384 g_slist_free(to_filter);
2386 if (unfiltered != NULL) {
2387 for (elem = unfiltered; elem; elem = g_slist_next(elem)) {
2388 MsgInfo *msginfo = (MsgInfo *)elem->data;
2389 exists_list = g_slist_prepend(exists_list, msginfo);
2391 if(prefs_common.thread_by_subject &&
2392 MSG_IS_IGNORE_THREAD(msginfo->flags) &&
2393 !subject_table_lookup(subject_table, msginfo->subject)) {
2394 subject_table_insert(subject_table, msginfo->subject, msginfo);
2397 g_slist_free(unfiltered);
2399 if (prefs_common.real_time_sync)
2400 folder_item_synchronise(item);
2401 } else {
2402 if (prefs_common.real_time_sync)
2403 folder_item_synchronise(item);
2406 g_slist_free(newmsg_list);
2408 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
2411 folder_item_set_batch(item, TRUE);
2412 for (elem = exists_list; elem != NULL; elem = g_slist_next(elem)) {
2413 MsgInfo *msginfo, *parent_msginfo;
2415 msginfo = elem->data;
2416 if (MSG_IS_IGNORE_THREAD(msginfo->flags) && (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2417 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2418 if (!MSG_IS_IGNORE_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD)) {
2419 procmsg_msginfo_change_flags(msginfo, MSG_IGNORE_THREAD, 0, MSG_NEW | MSG_UNREAD, 0);
2421 if (!MSG_IS_WATCH_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_WATCH_THREAD)) {
2422 procmsg_msginfo_set_flags(msginfo, MSG_WATCH_THREAD, 0);
2424 if(prefs_common.thread_by_subject && !msginfo->inreplyto &&
2425 !msginfo->references && !MSG_IS_IGNORE_THREAD(msginfo->flags) &&
2426 (parent_msginfo = subject_table_lookup(subject_table, msginfo->subject)))
2428 if(MSG_IS_IGNORE_THREAD(parent_msginfo->flags)) {
2429 procmsg_msginfo_change_flags(msginfo, MSG_IGNORE_THREAD, 0,
2430 MSG_NEW | MSG_UNREAD, 0);
2433 if ((folder_has_parent_of_type(item, F_OUTBOX) ||
2434 folder_has_parent_of_type(item, F_QUEUE) ||
2435 folder_has_parent_of_type(item, F_TRASH)) &&
2436 (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2437 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2438 if (MSG_IS_NEW(msginfo->flags))
2439 newcnt++;
2440 if (MSG_IS_UNREAD(msginfo->flags))
2441 unreadcnt++;
2442 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2443 unreadmarkedcnt++;
2444 if (MSG_IS_MARKED(msginfo->flags))
2445 markedcnt++;
2446 if (MSG_IS_REPLIED(msginfo->flags))
2447 repliedcnt++;
2448 if (MSG_IS_FORWARDED(msginfo->flags))
2449 forwardedcnt++;
2450 if (MSG_IS_LOCKED(msginfo->flags))
2451 lockedcnt++;
2452 if (MSG_IS_IGNORE_THREAD(msginfo->flags))
2453 ignoredcnt++;
2454 if (MSG_IS_WATCH_THREAD(msginfo->flags))
2455 watchedcnt++;
2457 totalcnt++;
2459 procmsg_msginfo_free(&msginfo);
2461 folder_item_set_batch(item, FALSE);
2462 g_slist_free(exists_list);
2464 if(prefs_common.thread_by_subject) {
2465 g_hash_table_destroy(subject_table);
2468 if (item->new_msgs != newcnt || item->unread_msgs != unreadcnt
2469 || item->total_msgs != totalcnt || item->marked_msgs != markedcnt
2470 || item->unreadmarked_msgs != unreadmarkedcnt
2471 || item->replied_msgs != repliedcnt || item->forwarded_msgs != forwardedcnt
2472 || item->locked_msgs != lockedcnt || item->ignored_msgs != ignoredcnt
2473 || item->watched_msgs != watchedcnt) {
2474 update_flags |= F_ITEM_UPDATE_CONTENT;
2477 item->new_msgs = newcnt;
2478 item->unread_msgs = unreadcnt;
2479 item->total_msgs = totalcnt;
2480 item->unreadmarked_msgs = unreadmarkedcnt;
2481 item->marked_msgs = markedcnt;
2482 item->replied_msgs = repliedcnt;
2483 item->forwarded_msgs = forwardedcnt;
2484 item->locked_msgs = lockedcnt;
2485 item->ignored_msgs = ignoredcnt;
2486 item->watched_msgs = watchedcnt;
2488 update_flags |= F_ITEM_UPDATE_MSGCNT;
2490 folder_item_update(item, update_flags);
2491 folder_item_update_thaw();
2493 item->scanning = ITEM_NOT_SCANNING;
2495 return 0;
2498 gint folder_item_scan(FolderItem *item)
2500 return folder_item_scan_full(item, TRUE);
2503 static void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
2505 gint *memusage = (gint *)data;
2507 if (item->cache == NULL)
2508 return;
2510 *memusage += msgcache_get_memory_usage(item->cache);
2513 static gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
2515 FolderItem *fa = (FolderItem *)a;
2516 FolderItem *fb = (FolderItem *)b;
2518 return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
2521 static void folder_find_expired_caches(FolderItem *item, gpointer data)
2523 GSList **folder_item_list = (GSList **)data;
2524 gint difftime, expiretime;
2526 if (item->cache == NULL)
2527 return;
2529 if (item->opened > 0)
2530 return;
2532 difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
2533 expiretime = prefs_common.cache_min_keep_time * 60;
2534 debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
2536 if (difftime > expiretime && !item->opened && !item->processing_pending) {
2537 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
2541 gboolean folder_item_free_cache(FolderItem *item, gboolean force)
2543 cm_return_val_if_fail(item != NULL, TRUE);
2545 if (item->cache == NULL)
2546 return TRUE;
2548 if (item->opened > 0 && !force)
2549 return FALSE;
2551 folder_item_write_cache(item);
2552 msgcache_destroy(item->cache);
2553 item->cache = NULL;
2554 return TRUE;
2557 void folder_clean_cache_memory_force(void)
2559 int old_cache_max_mem_usage = prefs_common.cache_max_mem_usage;
2560 int old_cache_min_keep_time = prefs_common.cache_min_keep_time;
2562 prefs_common.cache_max_mem_usage = 0;
2563 prefs_common.cache_min_keep_time = 0;
2565 folder_clean_cache_memory(NULL);
2567 prefs_common.cache_max_mem_usage = old_cache_max_mem_usage;
2568 prefs_common.cache_min_keep_time = old_cache_min_keep_time;
2571 void folder_clean_cache_memory(FolderItem *protected_item)
2573 gint memusage = 0;
2575 folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);
2576 debug_print("Total cache memory usage: %d\n", memusage);
2578 if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
2579 GSList *folder_item_list = NULL, *listitem;
2581 debug_print("Trying to free cache memory\n");
2583 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);
2584 listitem = folder_item_list;
2585 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
2586 FolderItem *item = (FolderItem *)(listitem->data);
2587 gint cache_size = 0;
2588 if (item == protected_item) {
2589 listitem = listitem->next;
2590 continue;
2592 debug_print("Freeing cache memory for %s\n", item->path ? item->path : item->name);
2593 cache_size = msgcache_get_memory_usage(item->cache);
2594 if (folder_item_free_cache(item, FALSE))
2595 memusage -= cache_size;
2597 listitem = listitem->next;
2599 g_slist_free(folder_item_list);
2603 static void folder_item_remove_cached_msg(FolderItem *item, MsgInfo *msginfo)
2605 Folder *folder = item->folder;
2607 cm_return_if_fail(folder != NULL);
2609 if (folder->klass->remove_cached_msg == NULL)
2610 return;
2612 folder->klass->remove_cached_msg(folder, item, msginfo);
2615 static void folder_item_clean_local_files(FolderItem *item, gint days)
2617 cm_return_if_fail(item != NULL);
2618 cm_return_if_fail(item->folder != NULL);
2620 if (FOLDER_TYPE(item->folder) == F_IMAP ||
2621 FOLDER_TYPE(item->folder) == F_NEWS) {
2622 GSList *msglist = folder_item_get_msg_list(item);
2623 GSList *cur;
2624 time_t t = time(NULL);
2625 for (cur = msglist; cur; cur = cur->next) {
2626 MsgInfo *msginfo = (MsgInfo *)cur->data;
2627 gint age = (t - msginfo->date_t) / (60*60*24);
2628 if (age > days)
2629 folder_item_remove_cached_msg(item, msginfo);
2631 procmsg_msg_list_free(msglist);
2635 static void folder_item_read_cache(FolderItem *item)
2637 gchar *cache_file, *mark_file, *tags_file;
2638 START_TIMING("");
2639 cm_return_if_fail(item != NULL);
2641 if (item->path != NULL) {
2642 cache_file = folder_item_get_cache_file(item);
2643 mark_file = folder_item_get_mark_file(item);
2644 tags_file = folder_item_get_tags_file(item);
2645 item->cache = msgcache_read_cache(item, cache_file);
2646 item->cache_dirty = FALSE;
2647 item->mark_dirty = FALSE;
2648 item->tags_dirty = FALSE;
2649 if (!item->cache) {
2650 MsgInfoList *list, *cur;
2651 guint newcnt = 0, unreadcnt = 0;
2652 guint markedcnt = 0, unreadmarkedcnt = 0;
2653 guint repliedcnt = 0, forwardedcnt = 0;
2654 guint lockedcnt = 0, ignoredcnt = 0;
2655 guint watchedcnt = 0;
2656 MsgInfo *msginfo;
2658 item->cache = msgcache_new();
2659 item->cache_dirty = TRUE;
2660 item->mark_dirty = TRUE;
2661 item->tags_dirty = TRUE;
2662 folder_item_scan_full(item, TRUE);
2664 msgcache_read_mark(item->cache, mark_file);
2666 list = msgcache_get_msg_list(item->cache);
2667 for (cur = list; cur != NULL; cur = g_slist_next(cur)) {
2668 msginfo = cur->data;
2670 if (MSG_IS_NEW(msginfo->flags))
2671 newcnt++;
2672 if (MSG_IS_UNREAD(msginfo->flags))
2673 unreadcnt++;
2674 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2675 unreadmarkedcnt++;
2676 if (MSG_IS_MARKED(msginfo->flags))
2677 markedcnt++;
2678 if (MSG_IS_REPLIED(msginfo->flags))
2679 repliedcnt++;
2680 if (MSG_IS_FORWARDED(msginfo->flags))
2681 forwardedcnt++;
2682 if (MSG_IS_LOCKED(msginfo->flags))
2683 lockedcnt++;
2684 if (MSG_IS_IGNORE_THREAD(msginfo->flags))
2685 ignoredcnt++;
2686 if (MSG_IS_WATCH_THREAD(msginfo->flags))
2687 watchedcnt++;
2688 procmsg_msginfo_unset_flags(msginfo, MSG_FULLY_CACHED, 0);
2690 item->new_msgs = newcnt;
2691 item->unread_msgs = unreadcnt;
2692 item->unreadmarked_msgs = unreadmarkedcnt;
2693 item->marked_msgs = markedcnt;
2694 item->replied_msgs = repliedcnt;
2695 item->forwarded_msgs = forwardedcnt;
2696 item->locked_msgs = lockedcnt;
2697 item->ignored_msgs = ignoredcnt;
2698 item->watched_msgs = watchedcnt;
2699 procmsg_msg_list_free(list);
2700 } else
2701 msgcache_read_mark(item->cache, mark_file);
2703 msgcache_read_tags(item->cache, tags_file);
2705 g_free(cache_file);
2706 g_free(mark_file);
2707 g_free(tags_file);
2708 } else {
2709 item->cache = msgcache_new();
2710 item->cache_dirty = TRUE;
2711 item->mark_dirty = TRUE;
2712 item->tags_dirty = TRUE;
2715 END_TIMING();
2716 folder_clean_cache_memory(item);
2719 void folder_item_write_cache(FolderItem *item)
2721 gchar *cache_file = NULL, *mark_file = NULL, *tags_file = NULL;
2722 FolderItemPrefs *prefs;
2723 gint filemode = 0;
2724 gchar *id;
2725 time_t last_mtime = (time_t)0;
2726 gboolean need_scan = FALSE;
2728 if (!item || !item->path || !item->cache)
2729 return;
2731 last_mtime = item->mtime;
2732 if (item->folder->klass->set_mtime) {
2733 if (item->folder->klass->scan_required)
2734 need_scan = item->folder->klass->scan_required(item->folder, item);
2735 else
2736 need_scan = TRUE;
2739 id = folder_item_get_identifier(item);
2740 debug_print("Save cache for folder %s\n", id);
2741 g_free(id);
2743 if (item->cache_dirty)
2744 cache_file = folder_item_get_cache_file(item);
2745 if (item->cache_dirty || item->mark_dirty)
2746 mark_file = folder_item_get_mark_file(item);
2747 if (item->cache_dirty || item->tags_dirty)
2748 tags_file = folder_item_get_tags_file(item);
2749 if (msgcache_write(cache_file, mark_file, tags_file, item->cache) < 0) {
2750 prefs = item->prefs;
2751 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
2752 /* for cache file */
2753 filemode = prefs->folder_chmod;
2754 if (filemode & S_IRGRP) filemode |= S_IWGRP;
2755 if (filemode & S_IROTH) filemode |= S_IWOTH;
2756 if (cache_file != NULL)
2757 chmod(cache_file, filemode);
2759 } else {
2760 item->cache_dirty = FALSE;
2761 item->mark_dirty = FALSE;
2762 item->tags_dirty = FALSE;
2765 if (!need_scan && item->folder->klass->set_mtime) {
2766 if (item->mtime == last_mtime) {
2767 item->folder->klass->set_mtime(item->folder, item);
2771 g_free(cache_file);
2772 g_free(mark_file);
2773 g_free(tags_file);
2776 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
2778 MsgInfo *msginfo = NULL;
2780 cm_return_val_if_fail(item != NULL, NULL);
2781 if (item->no_select)
2782 return NULL;
2783 if (!item->cache)
2784 folder_item_read_cache(item);
2786 if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
2787 return msginfo;
2789 msginfo = get_msginfo(item, num);
2790 if (msginfo != NULL) {
2791 msgcache_add_msg(item->cache, msginfo);
2792 return msginfo;
2795 return NULL;
2798 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
2800 MsgInfo *msginfo;
2802 cm_return_val_if_fail(item != NULL, NULL);
2803 cm_return_val_if_fail(msgid != NULL, NULL);
2804 if (item->no_select)
2805 return NULL;
2807 if (!item->cache)
2808 folder_item_read_cache(item);
2810 if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
2811 return msginfo;
2813 return NULL;
2816 GSList *folder_item_get_msg_list(FolderItem *item)
2818 cm_return_val_if_fail(item != NULL, NULL);
2819 if (item->no_select)
2820 return NULL;
2822 if (item->cache == 0)
2823 folder_item_read_cache(item);
2825 cm_return_val_if_fail(item->cache != NULL, NULL);
2827 return msgcache_get_msg_list(item->cache);
2830 static void msginfo_set_mime_flags(GNode *node, gpointer data)
2832 MsgInfo *msginfo = data;
2833 MimeInfo *mimeinfo = node->data;
2835 if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT
2836 && (!mimeinfo->subtype || (strcmp(mimeinfo->subtype, "pgp-signature") &&
2837 strcmp(mimeinfo->subtype, "x-pkcs7-signature") &&
2838 strcmp(mimeinfo->subtype, "pkcs7-signature")))) {
2839 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2840 } else if (mimeinfo->disposition == DISPOSITIONTYPE_UNKNOWN &&
2841 mimeinfo->id == NULL &&
2842 mimeinfo->type != MIMETYPE_TEXT &&
2843 mimeinfo->type != MIMETYPE_MULTIPART) {
2844 if (!mimeinfo->subtype
2845 || (strcmp(mimeinfo->subtype, "pgp-signature") &&
2846 strcmp(mimeinfo->subtype, "x-pkcs7-signature") &&
2847 strcmp(mimeinfo->subtype, "pkcs7-signature")))
2848 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2849 } else if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE &&
2850 mimeinfo->id == NULL &&
2851 (strcmp(mimeinfo->subtype, "pgp-signature") &&
2852 strcmp(mimeinfo->subtype, "x-pkcs7-signature") &&
2853 strcmp(mimeinfo->subtype, "pkcs7-signature")) &&
2854 (procmime_mimeinfo_get_parameter(mimeinfo, "name") != NULL ||
2855 procmime_mimeinfo_get_parameter(mimeinfo, "filename") != NULL)) {
2856 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2859 /* don't descend below top level message for signed and encrypted info */
2860 if (mimeinfo->type == MIMETYPE_MESSAGE)
2861 return;
2863 if (privacy_mimeinfo_is_signed(mimeinfo)) {
2864 procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
2867 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
2868 procmsg_msginfo_set_flags(msginfo, 0, MSG_ENCRYPTED);
2869 } else {
2870 /* searching inside encrypted parts doesn't really make sense */
2871 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2875 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
2877 Folder *folder;
2878 gchar *msgfile;
2879 MsgInfo *msginfo;
2881 cm_return_val_if_fail(item != NULL, NULL);
2883 folder = item->folder;
2885 cm_return_val_if_fail(folder->klass->fetch_msg != NULL, NULL);
2886 if (item->no_select)
2887 return NULL;
2889 msgfile = folder->klass->fetch_msg(folder, item, num);
2891 if (msgfile != NULL) {
2892 msginfo = folder_item_get_msginfo(item, num);
2893 if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
2894 MimeInfo *mimeinfo;
2896 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
2897 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
2898 mimeinfo = procmime_scan_file(msgfile);
2899 else
2900 mimeinfo = procmime_scan_queue_file(msgfile);
2901 /* check for attachments */
2902 if (mimeinfo != NULL) {
2903 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2904 procmime_mimeinfo_free_all(&mimeinfo);
2906 procmsg_msginfo_set_flags(msginfo, 0, MSG_SCANNED);
2909 procmsg_msginfo_free(&msginfo);
2912 return msgfile;
2915 gchar *folder_item_fetch_msg_full(FolderItem *item, gint num, gboolean headers,
2916 gboolean body)
2918 Folder *folder;
2919 gchar *msgfile;
2920 MsgInfo *msginfo;
2922 cm_return_val_if_fail(item != NULL, NULL);
2923 if (item->no_select)
2924 return NULL;
2926 folder = item->folder;
2928 if (folder->klass->fetch_msg_full == NULL)
2929 return folder_item_fetch_msg(item, num);
2931 if (item->prefs->offlinesync && prefs_common.real_time_sync)
2932 msgfile = folder->klass->fetch_msg_full(folder, item, num,
2933 TRUE, TRUE);
2934 else
2935 msgfile = folder->klass->fetch_msg_full(folder, item, num,
2936 headers, body);
2938 if (msgfile != NULL) {
2939 msginfo = folder_item_get_msginfo(item, num);
2940 if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
2941 MimeInfo *mimeinfo;
2943 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
2944 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
2945 mimeinfo = procmime_scan_file(msgfile);
2946 else
2947 mimeinfo = procmime_scan_queue_file(msgfile);
2948 /* check for attachments */
2949 if (mimeinfo != NULL) {
2950 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2951 procmime_mimeinfo_free_all(&mimeinfo);
2953 procmsg_msginfo_set_flags(msginfo, 0, MSG_SCANNED);
2956 procmsg_msginfo_free(&msginfo);
2959 return msgfile;
2963 static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
2965 static HeaderEntry hentry[] = {{"Message-ID:", NULL, TRUE},
2966 {NULL, NULL, FALSE}};
2967 FILE *fp;
2968 MsgInfo *msginfo;
2969 gint msgnum = 0;
2970 gchar buf[BUFFSIZE];
2972 if ((fp = g_fopen(file, "rb")) == NULL)
2973 return 0;
2975 if ((folder_has_parent_of_type(dest, F_QUEUE)) ||
2976 (folder_has_parent_of_type(dest, F_DRAFT)))
2977 while (fgets(buf, sizeof(buf), fp) != NULL) {
2978 /* new way */
2979 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
2980 strlen("X-Claws-End-Special-Headers:"))) ||
2981 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
2982 strlen("X-Sylpheed-End-Special-Headers:"))))
2983 break;
2984 /* old way */
2985 if (buf[0] == '\r' || buf[0] == '\n') break;
2986 /* from other mailers */
2987 if (!strncmp(buf, "Date: ", 6)
2988 || !strncmp(buf, "To: ", 4)
2989 || !strncmp(buf, "From: ", 6)
2990 || !strncmp(buf, "Subject: ", 9)) {
2991 rewind(fp);
2992 break;
2996 procheader_get_header_fields(fp, hentry);
2997 debug_print("looking for %s\n", hentry[0].body);
2998 if (hentry[0].body) {
2999 extract_parenthesis(hentry[0].body, '<', '>');
3000 remove_space(hentry[0].body);
3001 if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
3002 msgnum = msginfo->msgnum;
3003 procmsg_msginfo_free(&msginfo);
3005 debug_print("found message as uid %d\n", msgnum);
3009 g_free(hentry[0].body);
3010 hentry[0].body = NULL;
3011 fclose(fp);
3013 return msgnum;
3016 static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
3018 MsgPermFlags perm_flags = 0;
3019 MsgTmpFlags tmp_flags = 0;
3021 /* create new flags */
3022 if (source != NULL) {
3023 /* copy original flags */
3024 perm_flags = source->flags.perm_flags;
3025 tmp_flags = source->flags.tmp_flags;
3026 } else {
3027 perm_flags = dest->flags.perm_flags;
3028 tmp_flags = dest->flags.tmp_flags;
3031 /* remove new, unread and deleted in special folders */
3032 if (folder_has_parent_of_type(dest->folder, F_OUTBOX) ||
3033 folder_has_parent_of_type(dest->folder, F_QUEUE) ||
3034 folder_has_parent_of_type(dest->folder, F_DRAFT) ||
3035 folder_has_parent_of_type(dest->folder, F_TRASH))
3036 perm_flags &= ~(MSG_NEW | MSG_UNREAD | MSG_DELETED);
3038 /* set ignore flag of ignored parent exists */
3039 if (procmsg_msg_has_flagged_parent(dest, MSG_IGNORE_THREAD))
3040 perm_flags |= MSG_IGNORE_THREAD;
3042 /* unset FULLY_CACHED flags */
3043 perm_flags &= ~MSG_FULLY_CACHED;
3045 if (procmsg_msg_has_flagged_parent(dest, MSG_WATCH_THREAD))
3046 perm_flags |= MSG_WATCH_THREAD;
3048 /* Unset tmp flags that should not be copied */
3049 tmp_flags &= ~(MSG_MOVE | MSG_COPY | MSG_MOVE_DONE);
3051 /* unset flags that are set but should not */
3052 /* and set new flags */
3053 procmsg_msginfo_change_flags(dest,
3054 ~dest->flags.perm_flags & perm_flags,
3055 ~dest->flags.tmp_flags & tmp_flags,
3056 dest->flags.perm_flags & ~perm_flags,
3057 dest->flags.tmp_flags & ~tmp_flags);
3059 if (source && source->tags) {
3060 g_slist_free(dest->tags);
3061 dest->tags = g_slist_copy(source->tags);
3062 folder_item_commit_tags(dest->folder, dest, dest->tags, NULL);
3066 static void add_msginfo_to_cache(FolderItem *item, MsgInfo *newmsginfo, MsgInfo *flagsource)
3068 /* update folder stats */
3069 if (MSG_IS_NEW(newmsginfo->flags))
3070 item->new_msgs++;
3071 if (MSG_IS_UNREAD(newmsginfo->flags))
3072 item->unread_msgs++;
3073 if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
3074 item->unreadmarked_msgs++;
3075 if (MSG_IS_MARKED(newmsginfo->flags))
3076 item->marked_msgs++;
3077 if (MSG_IS_REPLIED(newmsginfo->flags))
3078 item->replied_msgs++;
3079 if (MSG_IS_FORWARDED(newmsginfo->flags))
3080 item->forwarded_msgs++;
3081 if (MSG_IS_LOCKED(newmsginfo->flags))
3082 item->locked_msgs++;
3083 if (MSG_IS_IGNORE_THREAD(newmsginfo->flags))
3084 item->ignored_msgs++;
3085 if (MSG_IS_WATCH_THREAD(newmsginfo->flags))
3086 item->watched_msgs++;
3087 item->total_msgs++;
3089 folder_item_update_freeze();
3091 if (!item->cache)
3092 folder_item_read_cache(item);
3094 msgcache_add_msg(item->cache, newmsginfo);
3095 copy_msginfo_flags(flagsource, newmsginfo);
3096 folder_item_update_with_msg(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_ADDMSG, newmsginfo);
3097 folder_item_update_thaw();
3100 static void remove_msginfo_from_cache(FolderItem *item, MsgInfo *msginfo)
3102 MsgInfoUpdate msginfo_update;
3104 if (!item->cache)
3105 folder_item_read_cache(item);
3107 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
3108 msginfo->folder->new_msgs--;
3109 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
3110 msginfo->folder->unread_msgs--;
3111 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
3112 msginfo->folder->unreadmarked_msgs--;
3113 if (MSG_IS_MARKED(msginfo->flags))
3114 item->marked_msgs--;
3115 if (MSG_IS_REPLIED(msginfo->flags))
3116 item->replied_msgs--;
3117 if (MSG_IS_FORWARDED(msginfo->flags))
3118 item->forwarded_msgs--;
3119 if (MSG_IS_LOCKED(msginfo->flags))
3120 item->locked_msgs--;
3121 if (MSG_IS_IGNORE_THREAD(msginfo->flags))
3122 item->ignored_msgs--;
3123 if (MSG_IS_WATCH_THREAD(msginfo->flags))
3124 item->watched_msgs--;
3126 msginfo->folder->total_msgs--;
3128 msginfo_update.msginfo = msginfo;
3129 msginfo_update.flags = MSGINFO_UPDATE_DELETED;
3130 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
3132 msgcache_remove_msg(item->cache, msginfo->msgnum);
3133 folder_item_update_with_msg(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_REMOVEMSG, msginfo);
3136 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
3137 MsgFlags *flags, gboolean remove_source)
3139 GSList file_list;
3140 MsgFileInfo fileinfo;
3142 cm_return_val_if_fail(dest != NULL, -1);
3143 cm_return_val_if_fail(file != NULL, -1);
3145 fileinfo.msginfo = NULL;
3146 fileinfo.file = (gchar *)file;
3147 fileinfo.flags = flags;
3148 file_list.data = &fileinfo;
3149 file_list.next = NULL;
3151 return folder_item_add_msgs(dest, &file_list, remove_source);
3154 gint folder_item_add_msgs(FolderItem *dest, GSList *file_list,
3155 gboolean remove_source)
3157 Folder *folder;
3158 gint ret, num, lastnum = -1;
3159 GSList *file_cur;
3160 GHashTable *relation;
3161 MsgFileInfo *fileinfo = NULL;
3162 gboolean folderscan = FALSE;
3164 cm_return_val_if_fail(dest != NULL, -1);
3165 cm_return_val_if_fail(file_list != NULL, -1);
3166 cm_return_val_if_fail(dest->folder != NULL, -1);
3167 if (dest->no_select)
3168 return -1;
3170 folder = dest->folder;
3172 relation = g_hash_table_new(g_direct_hash, g_direct_equal);
3174 if (folder->klass->add_msgs != NULL) {
3175 ret = folder->klass->add_msgs(folder, dest, file_list, relation);
3176 if (ret < 0) {
3177 g_hash_table_destroy(relation);
3178 return ret;
3180 } else {
3181 for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
3182 fileinfo = (MsgFileInfo *) file_cur->data;
3184 ret = folder->klass->add_msg(folder, dest, fileinfo->file, fileinfo->flags);
3185 if (ret < 0) {
3186 g_hash_table_destroy(relation);
3187 return ret;
3189 g_hash_table_insert(relation, fileinfo, GINT_TO_POINTER(ret));
3193 for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
3194 gpointer data, old_key;
3196 fileinfo = (MsgFileInfo *) file_cur->data;
3197 if (g_hash_table_lookup_extended(relation, fileinfo, &old_key, &data))
3198 num = GPOINTER_TO_INT(data);
3199 else
3200 num = -1;
3202 if (num >= 0) {
3203 MsgInfo *newmsginfo;
3205 if (num == 0) {
3206 if (!folderscan) {
3207 folder_item_scan_full(dest, FALSE);
3208 folderscan = TRUE;
3210 num = folder_item_get_msg_num_by_file(dest, fileinfo->file);
3211 debug_print("got num %d\n", num);
3214 if (num > lastnum)
3215 lastnum = num;
3217 if (num >= 0 && remove_source) {
3218 if (claws_unlink(fileinfo->file) < 0)
3219 FILE_OP_ERROR(fileinfo->file, "unlink");
3222 if (num == 0)
3223 continue;
3225 if (!folderscan &&
3226 ((newmsginfo = get_msginfo(dest, num)) != NULL)) {
3227 add_msginfo_to_cache(dest, newmsginfo, NULL);
3228 procmsg_msginfo_free(&newmsginfo);
3229 } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
3230 /* TODO: set default flags */
3231 procmsg_msginfo_free(&newmsginfo);
3236 g_hash_table_destroy(relation);
3238 return lastnum;
3241 static FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest, gboolean copy)
3243 GSList *mlist;
3244 FolderItem *new_item;
3245 FolderItem *next_item;
3246 GNode *srcnode;
3247 gchar *old_id, *new_id;
3248 FolderUpdateData hookdata;
3250 /* move messages */
3251 debug_print("%s %s to %s\n", copy?"Copying":"Moving", src->path, dest->path);
3252 new_item = folder_create_folder(dest, src->name);
3253 if (new_item == NULL) {
3254 g_print("Can't create folder\n");
3255 return NULL;
3258 if (new_item->folder == NULL)
3259 new_item->folder = dest->folder;
3261 /* move messages */
3262 log_message(LOG_PROTOCOL, copy ?_("Copying %s to %s...\n"):_("Moving %s to %s...\n"),
3263 src->name, new_item->path);
3265 /*copy prefs*/
3266 folder_item_prefs_copy_prefs(src, new_item);
3268 /* copy internal data */
3269 if (src->folder->klass == new_item->folder->klass &&
3270 src->folder->klass->copy_private_data != NULL)
3271 src->folder->klass->copy_private_data(src->folder,
3272 src, new_item);
3273 new_item->collapsed = src->collapsed;
3274 new_item->thread_collapsed = src->thread_collapsed;
3275 new_item->threaded = src->threaded;
3276 new_item->ret_rcpt = src->ret_rcpt;
3277 new_item->hide_read_msgs = src->hide_read_msgs;
3278 new_item->hide_del_msgs = src->hide_del_msgs;
3279 new_item->hide_read_threads = src->hide_read_threads;
3280 new_item->sort_key = src->sort_key;
3281 new_item->sort_type = src->sort_type;
3283 mlist = folder_item_get_msg_list(src);
3285 if (mlist != NULL) {
3286 if (copy)
3287 folder_item_copy_msgs(new_item, mlist);
3288 else
3289 folder_item_move_msgs(new_item, mlist);
3290 procmsg_msg_list_free(mlist);
3293 prefs_matcher_write_config();
3295 /* recurse */
3296 srcnode = src->folder->node;
3297 srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
3298 srcnode = srcnode->children;
3299 while (srcnode != NULL) {
3300 if (srcnode && srcnode->data) {
3301 next_item = (FolderItem*) srcnode->data;
3302 srcnode = srcnode->next;
3303 if (folder_item_move_recursive(next_item, new_item, copy) == NULL) {
3304 return NULL;
3308 old_id = folder_item_get_identifier(src);
3309 new_id = folder_item_get_identifier(new_item);
3311 hookdata.folder = src->folder;
3312 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_MOVE_FOLDERITEM;
3313 hookdata.item = src;
3314 hookdata.item2 = new_item;
3315 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
3317 /* if src supports removing, otherwise only copy folder */
3318 if (src->folder->klass->remove_folder != NULL && !copy)
3319 src->folder->klass->remove_folder(src->folder, src);
3320 folder_write_list();
3322 if (!copy) {
3323 debug_print("updating rules : %s => %s\n", old_id, new_id);
3324 if (old_id != NULL && new_id != NULL) {
3325 prefs_filtering_rename_path(old_id, new_id);
3326 account_rename_path(old_id, new_id);
3329 g_free(old_id);
3330 g_free(new_id);
3332 return new_item;
3335 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item, gboolean copy)
3337 FolderItem *tmp = folder_item_parent(dest);
3338 gchar * src_identifier, * dst_identifier;
3339 gchar * phys_srcpath, * phys_dstpath, *tmppath;
3341 while (tmp) {
3342 if (tmp == src) {
3343 return F_MOVE_FAILED_DEST_IS_CHILD;
3345 tmp = folder_item_parent(tmp);
3348 src_identifier = folder_item_get_identifier(src);
3349 dst_identifier = folder_item_get_identifier(dest);
3351 if(dst_identifier == NULL && dest->folder && folder_item_parent(dest) == NULL) {
3352 /* dest can be a root folder */
3353 dst_identifier = folder_get_identifier(dest->folder);
3355 if (src_identifier == NULL || dst_identifier == NULL) {
3356 debug_print("Can't get identifiers\n");
3357 return F_MOVE_FAILED;
3360 if (src->folder != dest->folder && !copy) {
3361 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
3364 phys_srcpath = folder_item_get_path(src);
3365 tmppath = folder_item_get_path(dest);
3366 phys_dstpath = g_strconcat(tmppath,
3367 G_DIR_SEPARATOR_S,
3368 g_path_get_basename(phys_srcpath),
3369 NULL);
3370 g_free(tmppath);
3372 if (folder_item_parent(src) == dest || src == dest) {
3373 g_free(src_identifier);
3374 g_free(dst_identifier);
3375 g_free(phys_srcpath);
3376 g_free(phys_dstpath);
3377 return F_MOVE_FAILED_DEST_IS_PARENT;
3379 debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
3380 if ((tmp = folder_item_move_recursive(src, dest, copy)) == NULL) {
3381 return F_MOVE_FAILED;
3384 g_free(src_identifier);
3385 g_free(dst_identifier);
3386 g_free(phys_srcpath);
3387 g_free(phys_dstpath);
3389 *new_item = tmp;
3391 return F_MOVE_OK;
3394 struct find_data
3396 gboolean found;
3398 static void find_num(gpointer key, gpointer value, gpointer data)
3400 struct find_data *fdata = (struct find_data *)data;
3401 if (GPOINTER_TO_INT(value) == 0)
3402 fdata->found = TRUE;
3405 static gboolean some_msgs_have_zero_num(GHashTable *hashtable)
3407 struct find_data fdata;
3409 fdata.found = FALSE;
3410 g_hash_table_foreach(hashtable, find_num, &fdata);
3412 return fdata.found;
3416 * Copy a list of message to a new folder and remove
3417 * source messages if wanted
3419 static gint do_copy_msgs(FolderItem *dest, GSList *msglist, gboolean remove_source)
3421 Folder *folder;
3422 GSList *l;
3423 gint num, lastnum = -1;
3424 gboolean folderscan = FALSE;
3425 GHashTable *relation;
3426 GSList *not_moved = NULL;
3427 gint total = 0, curmsg = 0;
3428 MsgInfo *msginfo = NULL;
3430 cm_return_val_if_fail(dest != NULL, -1);
3431 cm_return_val_if_fail(msglist != NULL, -1);
3433 folder = dest->folder;
3435 cm_return_val_if_fail(folder->klass->copy_msg != NULL, -1);
3436 if (dest->no_select)
3437 return -1;
3439 msginfo = (MsgInfo *)msglist->data;
3441 if (!msginfo)
3442 return -1;
3444 if (!MSG_IS_QUEUED(msginfo->flags) &&
3445 MSG_IS_DRAFT(msginfo->flags) &&
3446 folder_has_parent_of_type(dest, F_QUEUE)) {
3447 GSList *cur = msglist;
3448 gboolean queue_err = FALSE;
3449 for (; cur; cur = cur->next) {
3450 Compose *compose = NULL;
3451 FolderItem *queue = dest;
3452 int val = 0;
3454 msginfo = (MsgInfo *)cur->data;
3455 compose = compose_reedit(msginfo, TRUE);
3456 if (compose == NULL) {
3457 queue_err = TRUE;
3458 continue;
3460 val = compose_queue(compose, NULL, &queue, NULL,
3461 FALSE);
3462 if (val < 0) {
3463 queue_err = TRUE;
3464 } else if (remove_source) {
3465 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
3467 if (val == 0)
3468 compose_close(compose);
3470 return queue_err ? -1:0;
3473 relation = g_hash_table_new(g_direct_hash, g_direct_equal);
3475 for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
3476 MsgInfo * msginfo = (MsgInfo *) l->data;
3478 if (msginfo->planned_download != 0) {
3479 int old_planned = msginfo->planned_download;
3480 partial_unmark(msginfo);
3481 /* little hack to reenable after */
3482 msginfo->planned_download = old_planned;
3487 * Copy messages to destination folder and
3488 * store new message numbers in newmsgnums
3490 if (folder->klass->copy_msgs != NULL) {
3491 if (folder->klass->copy_msgs(folder, dest, msglist, relation) < 0) {
3492 g_hash_table_destroy(relation);
3493 return -1;
3495 } else {
3496 MsgInfo * msginfo;
3497 l = msglist;
3499 /* immediately stop if src and dest folders are identical */
3500 if (l != NULL) {
3501 msginfo = (MsgInfo *) l->data;
3502 if (msginfo != NULL && msginfo->folder == dest) {
3503 g_hash_table_destroy(relation);
3504 return -1;
3508 for (; l != NULL ; l = g_slist_next(l)) {
3509 msginfo = (MsgInfo *) l->data;
3511 num = folder->klass->copy_msg(folder, dest, msginfo);
3512 if (num > 0)
3513 g_hash_table_insert(relation, msginfo, GINT_TO_POINTER(num));
3514 else
3515 not_moved = g_slist_prepend(not_moved, msginfo);
3519 if (remove_source) {
3520 MsgInfo *msginfo = (MsgInfo *) msglist->data;
3521 FolderItem *item = msginfo->folder;
3523 * Remove source messages from their folders if
3524 * copying was successfull and update folder
3525 * message counts
3527 if (not_moved == NULL && item->folder->klass->remove_msgs) {
3528 item->folder->klass->remove_msgs(item->folder,
3529 msginfo->folder,
3530 msglist,
3531 relation);
3533 for (l = msglist; l != NULL; l = g_slist_next(l)) {
3534 gpointer old_key, data;
3535 msginfo = (MsgInfo *) l->data;
3536 item = msginfo->folder;
3538 if (g_hash_table_lookup_extended(relation, msginfo, &old_key, &data))
3539 num = GPOINTER_TO_INT(data);
3540 else
3541 num = 0;
3543 if (g_slist_find(not_moved, msginfo))
3544 continue;
3546 if ((num >= 0) && (item->folder->klass->remove_msg != NULL)) {
3547 if (!item->folder->klass->remove_msgs)
3548 item->folder->klass->remove_msg(item->folder,
3549 msginfo->folder,
3550 msginfo->msgnum);
3551 remove_msginfo_from_cache(item, msginfo);
3556 /* Read cache for dest folder */
3557 if (!dest->cache) folder_item_read_cache(dest);
3560 * Fetch new MsgInfos for new messages in dest folder,
3561 * add them to the msgcache and update folder message counts
3563 if (some_msgs_have_zero_num(relation)) {
3564 folder_item_scan_full(dest, FALSE);
3565 folderscan = TRUE;
3568 statusbar_print_all(_("Updating cache for %s..."), dest->path ? dest->path : "(null)");
3569 total = g_slist_length(msglist);
3571 if (FOLDER_TYPE(dest->folder) == F_IMAP && total > 1) {
3572 folder_item_scan_full(dest, FALSE);
3573 folderscan = TRUE;
3575 folder_item_set_batch(dest, TRUE);
3576 for (l = msglist; l != NULL; l = g_slist_next(l)) {
3577 MsgInfo *msginfo = (MsgInfo *) l->data;
3578 gpointer data, old_key;
3580 if (!msginfo)
3581 continue;
3583 if (g_hash_table_lookup_extended(relation, msginfo, &old_key, &data))
3584 num = GPOINTER_TO_INT(data);
3585 else
3586 num = 0;
3588 statusbar_progress_all(curmsg++,total, 100);
3589 if (curmsg % 100 == 0)
3590 GTK_EVENTS_FLUSH();
3592 if (num >= 0) {
3593 MsgInfo *newmsginfo = NULL;
3595 if (!folderscan && num > 0) {
3596 newmsginfo = get_msginfo(dest, num);
3597 if (newmsginfo != NULL) {
3598 add_msginfo_to_cache(dest, newmsginfo, msginfo);
3601 if (newmsginfo == NULL) {
3602 if (!folderscan) {
3603 folder_item_scan_full(dest, FALSE);
3604 folderscan = TRUE;
3606 if (msginfo->msgid != NULL) {
3607 newmsginfo = folder_item_get_msginfo_by_msgid(dest, msginfo->msgid);
3608 if (newmsginfo != NULL) {
3609 copy_msginfo_flags(msginfo, newmsginfo);
3610 num = newmsginfo->msgnum;
3614 if (newmsginfo != NULL
3615 && msginfo->planned_download == POP3_PARTIAL_DLOAD_DELE) {
3616 partial_mark_for_delete(newmsginfo);
3618 if (newmsginfo != NULL
3619 && msginfo->planned_download == POP3_PARTIAL_DLOAD_DLOAD) {
3620 partial_mark_for_download(newmsginfo);
3622 if (!MSG_IS_POSTFILTERED (msginfo->flags)) {
3623 procmsg_msginfo_set_flags ( msginfo, MSG_POSTFILTERED, 0);
3624 if (newmsginfo) {
3625 procmsg_msginfo_set_flags (newmsginfo, MSG_POSTFILTERED, 0);
3626 hooks_invoke (MAIL_POSTFILTERING_HOOKLIST, newmsginfo);
3629 procmsg_msginfo_free(&newmsginfo);
3632 if (num > lastnum)
3633 lastnum = num;
3636 folder_item_set_batch(dest, FALSE);
3637 statusbar_progress_all(0,0,0);
3638 statusbar_pop_all();
3640 g_hash_table_destroy(relation);
3641 if (not_moved != NULL) {
3642 g_slist_free(not_moved);
3643 return -1;
3644 } else
3645 return lastnum;
3649 * Move a message to a new folder.
3651 * \param dest Destination folder
3652 * \param msginfo The message
3654 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
3656 GSList list;
3658 cm_return_val_if_fail(dest != NULL, -1);
3659 cm_return_val_if_fail(msginfo != NULL, -1);
3661 list.data = msginfo;
3662 list.next = NULL;
3664 return do_copy_msgs(dest, &list, TRUE);
3668 * Move a list of messages to a new folder.
3670 * \param dest Destination folder
3671 * \param msglist List of messages
3673 gint folder_item_move_msgs(FolderItem *dest, GSList *msglist)
3675 gint result = -1;
3676 cm_return_val_if_fail(dest != NULL, -1);
3677 cm_return_val_if_fail(msglist != NULL, -1);
3678 inc_lock();
3679 result = do_copy_msgs(dest, msglist, TRUE);
3680 inc_unlock();
3681 return result;
3685 * Copy a message to a new folder.
3687 * \param dest Destination folder
3688 * \param msginfo The message
3690 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
3692 GSList list;
3694 cm_return_val_if_fail(dest != NULL, -1);
3695 cm_return_val_if_fail(msginfo != NULL, -1);
3697 list.data = msginfo;
3698 list.next = NULL;
3700 return do_copy_msgs(dest, &list, FALSE);
3704 * Copy a list of messages to a new folder.
3706 * \param dest Destination folder
3707 * \param msglist List of messages
3709 gint folder_item_copy_msgs(FolderItem *dest, GSList *msglist)
3711 gint result;
3712 cm_return_val_if_fail(dest != NULL, -1);
3713 cm_return_val_if_fail(msglist != NULL, -1);
3715 inc_lock();
3716 result = do_copy_msgs(dest, msglist, FALSE);
3717 inc_unlock();
3719 return result;
3722 gint folder_item_remove_msg(FolderItem *item, gint num)
3724 Folder *folder;
3725 gint ret;
3726 MsgInfo *msginfo;
3728 cm_return_val_if_fail(item != NULL, -1);
3729 folder = item->folder;
3730 cm_return_val_if_fail(folder->klass->remove_msg != NULL, -1);
3731 if (item->no_select)
3732 return -1;
3734 if (!item->cache) folder_item_read_cache(item);
3736 msginfo = msgcache_get_msg(item->cache, num);
3737 if (msginfo && MSG_IS_LOCKED(msginfo->flags)) {
3738 procmsg_msginfo_free(&msginfo);
3739 return -1;
3741 ret = folder->klass->remove_msg(folder, item, num);
3743 if (msginfo != NULL) {
3744 if (ret == 0)
3745 remove_msginfo_from_cache(item, msginfo);
3746 procmsg_msginfo_free(&msginfo);
3749 return ret;
3752 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
3754 Folder *folder;
3755 gint ret = 0;
3756 GSList *real_list = NULL, *cur = NULL;
3758 cm_return_val_if_fail(item != NULL, -1);
3759 folder = item->folder;
3760 cm_return_val_if_fail(folder != NULL, -1);
3761 if (item->no_select)
3762 return -1;
3763 inc_lock();
3764 if (!item->cache) folder_item_read_cache(item);
3766 folder_item_update_freeze();
3768 /* filter out locked mails */
3769 for (cur = msglist; cur; cur = cur->next) {
3770 MsgInfo *info = (MsgInfo *)cur->data;
3771 if (!MSG_IS_LOCKED(info->flags))
3772 real_list = g_slist_prepend(real_list, info);
3775 real_list = g_slist_reverse(real_list);
3777 if (item->folder->klass->remove_msgs) {
3778 ret = item->folder->klass->remove_msgs(item->folder,
3779 item,
3780 real_list,
3781 NULL);
3783 cur = real_list;
3784 while (ret == 0 && cur != NULL) {
3785 MsgInfo *msginfo = (MsgInfo *)cur->data;
3786 if (msginfo && MSG_IS_LOCKED(msginfo->flags)) {
3787 cur = cur->next;
3788 continue;
3790 if (!item->folder->klass->remove_msgs)
3791 ret = folder_item_remove_msg(item, msginfo->msgnum);
3792 if (ret != 0) break;
3793 msgcache_remove_msg(item->cache, msginfo->msgnum);
3794 cur = cur->next;
3796 g_slist_free(real_list);
3797 folder_item_scan_full(item, FALSE);
3798 folder_item_update_thaw();
3799 inc_unlock();
3800 return ret;
3803 gint folder_item_expunge(FolderItem *item)
3805 Folder *folder = item->folder;
3806 gint result = 0;
3807 if (folder == NULL)
3808 return -1;
3809 if (folder->klass->expunge) {
3810 GSList *msglist = folder_item_get_msg_list(item);
3811 GSList *cur;
3812 result = folder->klass->expunge(folder, item);
3813 if (result == 0) {
3814 for (cur = msglist; cur; cur = cur->next) {
3815 MsgInfo *msginfo = (MsgInfo *)cur->data;
3816 if (MSG_IS_DELETED(msginfo->flags)) {
3817 remove_msginfo_from_cache(item, msginfo);
3821 procmsg_msg_list_free(msglist);
3823 return result;
3826 gint folder_item_remove_all_msg(FolderItem *item)
3828 Folder *folder;
3829 gint result;
3831 cm_return_val_if_fail(item != NULL, -1);
3832 if (item->no_select)
3833 return -1;
3835 folder = item->folder;
3837 inc_lock();
3838 if (folder->klass->remove_all_msg != NULL) {
3839 result = folder->klass->remove_all_msg(folder, item);
3841 if (result == 0) {
3842 folder_item_free_cache(item, TRUE);
3843 item->cache = msgcache_new();
3844 item->cache_dirty = TRUE;
3845 item->mark_dirty = TRUE;
3846 item->tags_dirty = TRUE;
3848 } else {
3849 MsgInfoList *msglist;
3851 msglist = folder_item_get_msg_list(item);
3852 result = folder_item_remove_msgs(item, msglist);
3853 procmsg_msg_list_free(msglist);
3856 if (result == 0) {
3857 item->new_msgs = 0;
3858 item->unread_msgs = 0;
3859 item->unreadmarked_msgs = 0;
3860 item->marked_msgs = 0;
3861 item->total_msgs = 0;
3862 item->replied_msgs = 0;
3863 item->forwarded_msgs = 0;
3864 item->locked_msgs = 0;
3865 item->ignored_msgs = 0;
3866 item->watched_msgs = 0;
3867 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
3870 inc_unlock();
3871 return result;
3874 void folder_item_change_msg_flags(FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
3876 cm_return_if_fail(item != NULL);
3877 cm_return_if_fail(msginfo != NULL);
3879 item->mark_dirty = TRUE;
3881 if (item->no_select)
3882 return;
3884 if (item->folder->klass->change_flags != NULL && item->scanning != ITEM_SCANNING_WITH_FLAGS) {
3885 item->folder->klass->change_flags(item->folder, item, msginfo, newflags);
3886 } else {
3887 msginfo->flags.perm_flags = newflags;
3891 void folder_item_commit_tags(FolderItem *item, MsgInfo *msginfo, GSList *tags_set, GSList *tags_unset)
3893 Folder *folder = NULL;
3895 if (!msginfo)
3896 return;
3897 if (!item)
3898 return;
3899 if (!tags_set && !tags_unset)
3900 return;
3902 folder = item->folder;
3903 if (!folder)
3904 return;
3906 item->tags_dirty = TRUE;
3908 if (folder->klass->commit_tags == NULL)
3909 return;
3911 folder->klass->commit_tags(item, msginfo, tags_set, tags_unset);
3914 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
3916 Folder *folder;
3918 cm_return_val_if_fail(item != NULL, FALSE);
3919 if (item->no_select)
3920 return FALSE;
3922 folder = item->folder;
3924 cm_return_val_if_fail(folder->klass->is_msg_changed != NULL, -1);
3926 return folder->klass->is_msg_changed(folder, item, msginfo);
3929 void folder_item_discard_cache(FolderItem *item)
3931 gchar *dir;
3932 gchar *cache;
3934 if (!item)
3935 return;
3937 if (item->cache) {
3938 msgcache_destroy(item->cache);
3939 item->cache = NULL;
3941 dir = folder_item_get_path(item);
3942 if (is_dir_exist(dir))
3943 remove_all_numbered_files(dir);
3944 g_free(dir);
3946 cache = folder_item_get_cache_file(item);
3947 if (is_file_exist(cache))
3948 claws_unlink(cache);
3949 g_free(cache);
3953 static gchar *folder_item_get_cache_file(FolderItem *item)
3955 gchar *path;
3956 gchar *file;
3957 gchar *old_file;
3959 cm_return_val_if_fail(item != NULL, NULL);
3960 cm_return_val_if_fail(item->path != NULL, NULL);
3962 path = folder_item_get_path(item);
3963 cm_return_val_if_fail(path != NULL, NULL);
3964 if (!is_dir_exist(path))
3965 make_dir_hier(path);
3966 file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
3967 old_file = g_strconcat(path, G_DIR_SEPARATOR_S, OLD_CACHE_FILE, NULL);
3969 if (!is_file_exist(file) && is_file_exist(old_file))
3970 move_file(old_file, file, FALSE);
3971 g_free(old_file);
3972 g_free(path);
3974 return file;
3977 static gchar *folder_item_get_mark_file(FolderItem *item)
3979 gchar *path;
3980 gchar *file;
3981 gchar *old_file;
3983 cm_return_val_if_fail(item != NULL, NULL);
3984 cm_return_val_if_fail(item->path != NULL, NULL);
3986 path = folder_item_get_path(item);
3987 cm_return_val_if_fail(path != NULL, NULL);
3988 if (!is_dir_exist(path))
3989 make_dir_hier(path);
3990 file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
3991 old_file = g_strconcat(path, G_DIR_SEPARATOR_S, OLD_MARK_FILE, NULL);
3993 if (!is_file_exist(file) && is_file_exist(old_file))
3994 move_file(old_file, file, FALSE);
3995 g_free(old_file);
3996 g_free(path);
3998 return file;
4001 static gchar *folder_item_get_tags_file(FolderItem *item)
4003 gchar *path;
4004 gchar *identifier;
4005 gchar *file;
4007 /* we save tags files in rc_dir, because tagsrc is there too,
4008 * and storing tags directly in the mailboxes would give strange
4009 * result when using another Claws mailbox from another install
4010 * with different tags. */
4012 cm_return_val_if_fail(item != NULL, NULL);
4014 identifier = folder_item_get_identifier(item);
4015 cm_return_val_if_fail(identifier != NULL, NULL);
4017 #ifdef G_OS_WIN32
4018 while (strchr(identifier, '/'))
4019 *strchr(identifier, '/') = '\\';
4020 #endif
4022 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
4023 TAGS_DIR, G_DIR_SEPARATOR_S,
4024 identifier, NULL);
4026 g_free(identifier);
4028 if (!is_dir_exist(path))
4029 make_dir_hier(path);
4031 file = g_strconcat(path, G_DIR_SEPARATOR_S, TAGS_FILE, NULL);
4033 g_free(path);
4035 return file;
4038 static gpointer xml_to_folder_item(gpointer nodedata, gpointer data)
4040 XMLNode *xmlnode = (XMLNode *) nodedata;
4041 Folder *folder = (Folder *) data;
4042 FolderItem *item;
4044 cm_return_val_if_fail(xmlnode != NULL, NULL);
4045 cm_return_val_if_fail(folder != NULL, NULL);
4047 if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
4048 g_warning("tag name != \"folderitem\"");
4049 return NULL;
4052 item = folder_item_new(folder, "", "");
4053 g_node_destroy(item->node);
4054 if (folder->klass->item_set_xml != NULL)
4055 folder->klass->item_set_xml(folder, item, xmlnode->tag);
4056 else
4057 folder_item_set_xml(folder, item, xmlnode->tag);
4059 item->folder = folder;
4061 switch (item->stype) {
4062 case F_INBOX: folder->inbox = item; break;
4063 case F_OUTBOX: folder->outbox = item; break;
4064 case F_DRAFT: folder->draft = item; break;
4065 case F_QUEUE: folder->queue = item; break;
4066 case F_TRASH: folder->trash = item; break;
4067 default: break;
4069 folder_item_prefs_read_config(item);
4071 return item;
4074 static gboolean folder_item_set_node(GNode *node, gpointer data)
4076 cm_return_val_if_fail(node->data != NULL, -1);
4078 FolderItem *item = (FolderItem *) node->data;
4079 item->node = node;
4081 return FALSE;
4084 static Folder *folder_get_from_xml(GNode *node)
4086 Folder *folder;
4087 XMLNode *xmlnode;
4088 GList *list;
4089 FolderClass *klass = NULL;
4090 GNode *cur;
4092 cm_return_val_if_fail(node->data != NULL, NULL);
4094 xmlnode = node->data;
4095 if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
4096 g_warning("tag name != \"folder\"");
4097 return NULL;
4099 list = xmlnode->tag->attr;
4100 for (; list != NULL; list = list->next) {
4101 XMLAttr *attr = list->data;
4103 if (!attr || !attr->name || !attr->value) continue;
4104 if (!strcmp(attr->name, "type"))
4105 klass = folder_get_class_from_string(attr->value);
4107 if (klass == NULL)
4108 return NULL;
4110 folder = folder_new(klass, "", "");
4111 cm_return_val_if_fail(folder != NULL, NULL);
4113 if (klass->set_xml)
4114 klass->set_xml(folder, xmlnode->tag);
4115 else
4116 folder_set_xml(folder, xmlnode->tag);
4118 cur = node->children;
4119 while (cur != NULL) {
4120 GNode *itemnode;
4122 itemnode = g_node_map(cur, xml_to_folder_item, (gpointer) folder);
4123 g_node_append(folder->node, itemnode);
4124 cur = cur->next;
4126 g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, folder_item_set_node, NULL);
4128 return folder;
4131 gchar *folder_get_list_path(void)
4133 static gchar *filename = NULL;
4135 if (!filename)
4136 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
4137 FOLDER_LIST, NULL);
4139 return filename;
4142 static gpointer folder_item_to_xml(gpointer nodedata, gpointer data)
4144 FolderItem *item = (FolderItem *) nodedata;
4145 XMLTag *tag;
4147 cm_return_val_if_fail(item != NULL, NULL);
4149 if (item->folder->klass->item_get_xml != NULL)
4150 tag = item->folder->klass->item_get_xml(item->folder, item);
4151 else
4152 tag = folder_item_get_xml(item->folder, item);
4154 return xml_node_new(tag, NULL);
4157 static GNode *folder_get_xml_node(Folder *folder)
4159 GNode *node;
4160 XMLNode *xmlnode;
4161 XMLTag *tag;
4163 cm_return_val_if_fail(folder != NULL, NULL);
4165 if (folder->klass->get_xml != NULL)
4166 tag = folder->klass->get_xml(folder);
4167 else
4168 tag = folder_get_xml(folder);
4170 xml_tag_add_attr(tag, xml_attr_new("type", folder->klass->idstr));
4172 xmlnode = xml_node_new(tag, NULL);
4174 node = g_node_new(xmlnode);
4176 cm_return_val_if_fail (folder->node != NULL, NULL);
4178 if (folder->node->children) {
4179 GNode *cur;
4181 cur = folder->node->children;
4182 while (cur) {
4183 GNode *xmlnode;
4185 xmlnode = g_node_map(cur, folder_item_to_xml, (gpointer) folder);
4186 g_node_append(node, xmlnode);
4187 cur = cur->next;
4191 return node;
4194 static void folder_update_op_count_rec(GNode *node)
4196 FolderItem *fitem = FOLDER_ITEM(node->data);
4198 if (g_node_depth(node) > 0) {
4199 if (fitem->op_count > 0) {
4200 fitem->op_count = 0;
4201 folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
4203 if (node->children) {
4204 GNode *child;
4206 child = node->children;
4207 while (child) {
4208 GNode *cur;
4210 cur = child;
4211 child = cur->next;
4212 folder_update_op_count_rec(cur);
4218 void folder_update_op_count(void)
4220 GList *cur;
4221 Folder *folder;
4223 for (cur = folder_list; cur != NULL; cur = cur->next) {
4224 folder = cur->data;
4225 folder_update_op_count_rec(folder->node);
4229 typedef struct _type_str {
4230 gchar * str;
4231 gint type;
4232 } type_str;
4236 static gchar * folder_item_get_tree_identifier(FolderItem * item)
4238 if (item->parent != NULL) {
4239 gchar * path;
4240 gchar * id;
4242 path = folder_item_get_tree_identifier(item->parent);
4243 if (path == NULL)
4244 return NULL;
4246 id = g_strconcat(path, "/", item->name, NULL);
4247 g_free(path);
4249 return id;
4251 else {
4252 return g_strconcat("/", item->name, NULL);
4257 /* CLAWS: temporary local folder for filtering */
4258 #define TEMP_FOLDER "TEMP_FOLDER"
4259 #define PROCESSING_FOLDER_ITEM "processing"
4261 static FolderItem *folder_create_processing_folder(int account_id)
4263 static Folder *processing_folder = NULL;
4264 FolderItem *processing_folder_item;
4266 gchar *processing_folder_item_name = NULL;
4268 processing_folder_item_name = g_strdup_printf("%s-%d", PROCESSING_FOLDER_ITEM, account_id);
4270 if (processing_folder == NULL) {
4271 gchar *tmppath =
4272 g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
4273 "tempfolder", NULL);
4274 processing_folder =
4275 folder_new(mh_get_class(), TEMP_FOLDER, tmppath);
4276 g_free(tmppath);
4277 tmppath = NULL;
4279 g_assert(processing_folder != NULL);
4280 processing_folder->klass->scan_tree(processing_folder);
4282 g_assert(processing_folder != NULL);
4284 processing_folder_item = folder_find_child_item_by_name(FOLDER_ITEM(processing_folder->node->data),
4285 processing_folder_item_name);
4286 if (processing_folder_item) {
4287 debug_print("*TMP* already created %s\n", folder_item_get_path(processing_folder_item));
4288 } else {
4289 processing_folder_item = processing_folder->klass->create_folder(processing_folder,
4290 processing_folder->node->data,
4291 processing_folder_item_name);
4292 folder_item_append(FOLDER_ITEM(processing_folder->node->data), processing_folder_item);
4293 debug_print("*TMP* creating %s\n", folder_item_get_path(processing_folder_item));
4295 g_free(processing_folder_item_name);
4296 g_assert(processing_folder_item != NULL);
4298 return(processing_folder_item);
4301 FolderItem *folder_get_default_processing(int account_id)
4303 return folder_create_processing_folder(account_id);
4306 /* folder_persist_prefs_new() - return hash table with persistent
4307 * settings (and folder name as key).
4308 * (note that in claws other options are in the folder_item_prefs_RC
4309 * file, so those don't need to be included in PersistPref yet)
4311 static GHashTable *folder_persist_prefs_new(Folder *folder)
4313 GHashTable *pptable;
4315 cm_return_val_if_fail(folder, NULL);
4316 pptable = g_hash_table_new(g_str_hash, g_str_equal);
4317 folder_get_persist_prefs_recursive(folder->node, pptable);
4318 return pptable;
4321 static void folder_persist_prefs_free(GHashTable *pptable)
4323 cm_return_if_fail(pptable);
4324 g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
4325 g_hash_table_destroy(pptable);
4328 static const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
4330 if (pptable == NULL || name == NULL) return NULL;
4331 return g_hash_table_lookup(pptable, name);
4334 static void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
4336 const PersistPrefs *pp;
4337 gchar *id = folder_item_get_identifier(item);
4339 pp = folder_get_persist_prefs(pptable, id);
4340 g_free(id);
4342 if (!pp) return;
4344 /* CLAWS: since not all folder properties have been migrated to
4345 * folderlist.xml, we need to call the old stuff first before
4346 * setting things that apply both to Main and Claws. */
4347 folder_item_prefs_read_config(item);
4349 item->collapsed = pp->collapsed;
4350 item->thread_collapsed = pp->thread_collapsed;
4351 item->threaded = pp->threaded;
4352 item->ret_rcpt = pp->ret_rcpt;
4353 item->hide_read_msgs = pp->hide_read_msgs;
4354 item->hide_del_msgs = pp->hide_del_msgs;
4355 item->hide_read_threads = pp->hide_read_threads;
4356 item->sort_key = pp->sort_key;
4357 item->sort_type = pp->sort_type;
4360 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
4362 FolderItem *item = FOLDER_ITEM(node->data);
4363 PersistPrefs *pp;
4364 GNode *child, *cur;
4365 gchar *id;
4367 cm_return_if_fail(node != NULL);
4368 cm_return_if_fail(item != NULL);
4370 /* NOTE: item->path == NULL means top level folder; not interesting
4371 * to store preferences of that one. */
4372 if (item->path) {
4373 id = folder_item_get_identifier(item);
4374 pp = g_new0(PersistPrefs, 1);
4375 cm_return_if_fail(pp != NULL);
4376 pp->collapsed = item->collapsed;
4377 pp->thread_collapsed = item->thread_collapsed;
4378 pp->threaded = item->threaded;
4379 pp->ret_rcpt = item->ret_rcpt;
4380 pp->hide_read_msgs = item->hide_read_msgs;
4381 pp->hide_del_msgs = item->hide_del_msgs;
4382 pp->hide_read_threads = item->hide_read_threads;
4383 pp->sort_key = item->sort_key;
4384 pp->sort_type = item->sort_type;
4385 g_hash_table_insert(pptable, id, pp);
4388 if (node->children) {
4389 child = node->children;
4390 while (child) {
4391 cur = child;
4392 child = cur->next;
4393 folder_get_persist_prefs_recursive(cur, pptable);
4398 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
4400 g_free(key);
4401 g_free(val);
4402 return TRUE;
4405 void folder_item_apply_processing(FolderItem *item)
4407 GSList *processing_list;
4408 GSList *mlist, *cur;
4409 guint total = 0, curmsg = 0;
4410 gint last_apply_per_account;
4412 cm_return_if_fail(item != NULL);
4414 if (item->no_select)
4415 return;
4417 processing_list = item->prefs->processing;
4419 if (!pre_global_processing && !processing_list
4420 && !post_global_processing)
4421 return;
4423 debug_print("processing %s\n", item->name);
4424 folder_item_update_freeze();
4426 inc_lock();
4428 mlist = folder_item_get_msg_list(item);
4429 total = g_slist_length(mlist);
4430 statusbar_print_all(_("Processing messages..."));
4432 last_apply_per_account = prefs_common.apply_per_account_filtering_rules;
4433 prefs_common.apply_per_account_filtering_rules = FILTERING_ACCOUNT_RULES_SKIP;
4435 folder_item_set_batch(item, TRUE);
4436 for (cur = mlist ; cur != NULL ; cur = cur->next) {
4437 MsgInfo * msginfo;
4439 msginfo = (MsgInfo *) cur->data;
4441 /* reset parameters that can be modified by processing */
4442 msginfo->hidden = 0;
4443 msginfo->score = 0;
4445 statusbar_progress_all(curmsg++,total, 10);
4447 /* apply pre global rules */
4448 filter_message_by_msginfo(pre_global_processing, msginfo, NULL,
4449 FILTERING_PRE_PROCESSING, NULL);
4451 /* apply rules of the folder */
4452 filter_message_by_msginfo(processing_list, msginfo, NULL,
4453 FILTERING_FOLDER_PROCESSING, item->name);
4455 /* apply post global rules */
4456 filter_message_by_msginfo(post_global_processing, msginfo, NULL,
4457 FILTERING_POST_PROCESSING, NULL);
4458 if (curmsg % 1000 == 0)
4459 GTK_EVENTS_FLUSH();
4461 folder_item_set_batch(item, FALSE);
4463 prefs_common.apply_per_account_filtering_rules = last_apply_per_account;
4465 if (pre_global_processing || processing_list
4466 || post_global_processing)
4467 filtering_move_and_copy_msgs(mlist);
4468 for (cur = mlist ; cur != NULL ; cur = cur->next) {
4469 procmsg_msginfo_free((MsgInfo **)&(cur->data));
4471 g_slist_free(mlist);
4473 statusbar_progress_all(0,0,0);
4474 statusbar_pop_all();
4476 inc_unlock();
4478 folder_item_update_thaw();
4482 * functions for handling FolderItem content changes
4484 static gint folder_item_update_freeze_cnt = 0;
4486 static void folder_item_update_with_msg(FolderItem *item, FolderItemUpdateFlags update_flags, MsgInfo *msg)
4488 if (folder_item_update_freeze_cnt == 0 /* || (msg != NULL && item->opened) */) {
4489 FolderItemUpdateData source;
4491 source.item = item;
4492 source.update_flags = update_flags;
4493 source.msg = msg;
4494 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
4495 } else {
4496 item->update_flags |= update_flags & ~(F_ITEM_UPDATE_ADDMSG | F_ITEM_UPDATE_REMOVEMSG);
4501 * Notify the folder system about changes to a folder. If the
4502 * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
4503 * be invoked, otherwise the changes will be remebered until
4504 * the folder system is thawed.
4506 * \param item The FolderItem that was changed
4507 * \param update_flags Type of changed that was made
4509 void folder_item_update(FolderItem *item, FolderItemUpdateFlags update_flags)
4511 folder_item_update_with_msg(item, update_flags, NULL);
4514 void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
4516 GNode *node = item->folder->node;
4518 node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
4519 node = node->children;
4521 folder_item_update(item, update_flags);
4522 while (node != NULL) {
4523 if (node && node->data) {
4524 FolderItem *next_item = (FolderItem*) node->data;
4526 folder_item_update(next_item, update_flags);
4528 node = node->next;
4532 void folder_item_update_freeze(void)
4534 folder_item_update_freeze_cnt++;
4537 static void folder_item_update_func(FolderItem *item, gpointer data)
4539 FolderItemUpdateData source;
4541 if (item->update_flags) {
4542 source.item = item;
4543 source.update_flags = item->update_flags;
4544 source.msg = NULL;
4545 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
4546 item->update_flags = 0;
4550 void folder_item_update_thaw(void)
4552 if (folder_item_update_freeze_cnt > 0)
4553 folder_item_update_freeze_cnt--;
4554 if (folder_item_update_freeze_cnt == 0) {
4555 /* Update all folders */
4556 folder_func_to_all_folders(folder_item_update_func, NULL);
4560 void folder_item_synchronise(FolderItem *item)
4562 if (!item)
4563 return;
4564 if (item->prefs->offlinesync && item->folder->klass->synchronise) {
4565 statuswindow_print_all(_("Synchronising %s for offline use...\n"), item->path ? item->path : "(null)");
4566 item->folder->klass->synchronise(item,
4567 item->prefs->offlinesync_days);
4568 if (item->prefs->offlinesync_days > 0 &&
4569 item->prefs->remove_old_bodies)
4570 folder_item_clean_local_files(item, item->prefs->offlinesync_days);
4571 statuswindow_pop_all();
4575 static void folder_item_synchronise_func(FolderItem *item, gpointer data)
4577 Folder *folder = (Folder *)data;
4578 if (folder == NULL || item->folder == folder) {
4579 folder_item_synchronise(item);
4583 void folder_synchronise(Folder *folder)
4585 folder_func_to_all_folders(folder_item_synchronise_func, folder);
4588 typedef struct _WantSyncData {
4589 Folder *folder;
4590 gboolean want_sync;
4591 } WantSyncData;
4593 static void folder_item_want_synchronise_func(FolderItem *item, gpointer data)
4595 WantSyncData *want_sync_data = (WantSyncData *)data;
4597 if (want_sync_data->folder == NULL || item->folder == want_sync_data->folder) {
4598 if (item->prefs->offlinesync && item->folder->klass->synchronise)
4599 want_sync_data->want_sync |= TRUE;
4603 gboolean folder_want_synchronise(Folder *folder)
4605 WantSyncData *want_sync_data = g_new0(WantSyncData, 1);
4606 gboolean result;
4607 want_sync_data->folder = folder;
4608 want_sync_data->want_sync = FALSE;
4610 folder_func_to_all_folders(folder_item_want_synchronise_func, want_sync_data);
4611 result = want_sync_data->want_sync;
4612 g_free(want_sync_data);
4613 if (result > 0)
4614 debug_print("Folder %s wants sync\n", folder->name);
4615 return result;
4618 void folder_item_set_batch (FolderItem *item, gboolean batch)
4620 if (!item || !item->folder)
4621 return;
4622 if (item->folder->klass->set_batch) {
4623 item->folder->klass->set_batch(item->folder, item, batch);
4627 gboolean folder_has_parent_of_type(FolderItem *item,
4628 SpecialFolderItemType type)
4630 FolderItem *cur = item;
4632 if (!item)
4633 return FALSE;
4634 /* if we already know it, make it short */
4635 if (item->parent_stype != -1) {
4636 return (item->parent_stype == type);
4639 /* if we don't, find the type from the first possible parent,
4640 * and set our parent type to be faster next time */
4641 while (cur) {
4642 if (cur->stype == type || cur->parent_stype == type) {
4643 item->parent_stype = type;
4644 return TRUE;
4646 cur = folder_item_parent(cur);
4649 /* if we didn't match what was asked, we didn't return. If our
4650 * parent type is unknown, we may as well find it now to be faster
4651 * later. */
4652 if (item->parent_stype == -1) {
4653 cur = item;
4654 while (cur) {
4655 /* here's an exception: Inbox subfolders are normal. */
4656 if (item->parent_stype == -1 && cur->stype == F_INBOX
4657 && item != cur) {
4658 item->parent_stype = F_NORMAL;
4659 break;
4661 /* ah, we know this parent's parent's type, we may as
4662 * well copy it instead of going up the full way */
4663 if (cur->parent_stype != -1) {
4664 item->parent_stype = cur->parent_stype;
4665 break;
4667 /* we found a parent that has a special type. That's
4668 * our parent type. */
4669 if (cur->stype != F_NORMAL) {
4670 cur->parent_stype = cur->stype;
4671 item->parent_stype = cur->stype;
4672 break;
4674 /* if we didn't find anything, go up once more */
4675 cur = folder_item_parent(cur);
4677 /* as we still didn't find anything, our parents must all be
4678 * normal. */
4679 if (item->parent_stype == -1) {
4680 item->parent_stype = F_NORMAL;
4683 return FALSE;
4686 gboolean folder_is_child_of(FolderItem *item, FolderItem *parent)
4688 if (item == NULL || parent == NULL)
4689 return FALSE;
4691 while (item != NULL) {
4692 if (parent == item)
4693 return TRUE;
4695 item = folder_item_parent(item);
4698 return FALSE;
4702 gboolean folder_subscribe (const gchar *uri)
4704 GList *cur;
4705 for (cur = folder_get_list(); cur != NULL; cur = g_list_next(cur)) {
4706 Folder *folder = (Folder *) cur->data;
4708 if (folder->klass->subscribe
4709 && folder->klass->subscribe(folder, uri)) {
4710 return TRUE;
4713 return FALSE;
4717 gboolean folder_get_sort_type (Folder *folder,
4718 FolderSortKey *sort_key,
4719 FolderSortType *sort_type)
4721 if (!folder || !sort_key || !sort_type)
4722 return FALSE;
4723 if (folder->klass->get_sort_type == NULL)
4724 return FALSE;
4725 folder->klass->get_sort_type(folder, sort_key, sort_type);
4726 return TRUE;
4729 gint folder_item_search_msgs (Folder *folder,
4730 FolderItem *container,
4731 MsgNumberList **msgs,
4732 gboolean *on_server,
4733 MatcherList *predicate,
4734 SearchProgressNotify progress_cb,
4735 gpointer progress_data)
4737 gint result = -1;
4739 folder_item_update_freeze();
4741 if (folder->klass->search_msgs)
4742 result = folder->klass->search_msgs(folder, container,
4743 msgs, on_server, predicate, progress_cb, progress_data);
4744 if (result < 0)
4745 result = folder_item_search_msgs_local(folder, container,
4746 msgs, on_server, predicate, progress_cb, progress_data);
4748 folder_item_update_thaw();
4750 return result;
4753 MsgNumberList *folder_item_get_number_list(FolderItem *item)
4755 GSList *nums = NULL;
4756 GSList *msglist = folder_item_get_msg_list(item);
4758 nums = procmsg_get_number_list_for_msgs(msglist);
4759 procmsg_msg_list_free(msglist);
4761 return nums;
4764 gint folder_item_search_msgs_local (Folder *folder,
4765 FolderItem *container,
4766 MsgNumberList **msgs,
4767 gboolean *on_server,
4768 MatcherList *predicate,
4769 SearchProgressNotify progress_cb,
4770 gpointer progress_data)
4772 GSList *result = NULL;
4773 GSList *cur = NULL;
4774 gint matched_count = 0;
4775 guint processed_count = 0;
4776 gint msgcount;
4777 GSList *nums = NULL;
4779 if (*msgs == NULL) {
4780 nums = folder_item_get_number_list(container);
4781 } else {
4782 nums = *msgs;
4785 msgcount = g_slist_length(nums);
4787 if (msgcount < 0)
4788 return -1;
4790 for (cur = nums; cur != NULL; cur = cur->next) {
4791 guint msgnum = GPOINTER_TO_UINT(cur->data);
4792 MsgInfo *msg = folder_item_get_msginfo(container, msgnum);
4794 if (msg == NULL) {
4795 g_slist_free(result);
4796 return -1;
4799 if (matcherlist_match(predicate, msg)) {
4800 result = g_slist_prepend(result, GUINT_TO_POINTER(msg->msgnum));
4801 matched_count++;
4803 processed_count++;
4805 if (progress_cb != NULL
4806 && !progress_cb(progress_data, FALSE, processed_count,
4807 matched_count, msgcount))
4808 break;
4811 g_slist_free(nums);
4812 *msgs = g_slist_reverse(result);
4814 return matched_count;
4817 /* Tests if a local (on disk) folder name is acceptable. */
4818 gboolean folder_local_name_ok(const gchar *name)
4820 #ifdef G_OS_WIN32
4821 if (name[0] == '.' || name[strlen(name) - 1] == '.') {
4822 alertpanel_error(_("A folder name cannot begin or end with a dot."));
4823 return FALSE;
4825 if (name[strlen(name) - 1] == ' ') {
4826 alertpanel_error(_("A folder name can not end with a space."));
4827 return FALSE;
4829 #endif
4831 return TRUE;