Fix error creation and warning
[claws.git] / src / folder.c
blob6651fc4e6d287f0de411b7760218681e8af7b65a
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2020 the Claws Mail team and Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 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>
35 #include "alertpanel.h"
36 #include "folder.h"
37 #include "session.h"
38 #include "inc.h"
39 #include "imap.h"
40 #include "news.h"
41 #include "mh.h"
42 #include "utils.h"
43 #include "xml.h"
44 #include "codeconv.h"
45 #include "prefs_gtk.h"
46 #include "account.h"
47 #include "filtering.h"
48 #include "procheader.h"
49 #include "hooks.h"
50 #include "log.h"
51 #include "folder_item_prefs.h"
52 #include "remotefolder.h"
53 #include "partial_download.h"
54 #include "statusbar.h"
55 #include "gtkutils.h"
56 #include "timing.h"
57 #include "compose.h"
58 #include "main.h"
59 #include "msgcache.h"
60 #include "privacy.h"
61 #include "prefs_common.h"
62 #include "prefs_migration.h"
63 #include "file-utils.h"
65 /* Dependecies to be removed ?! */
66 #include "prefs_account.h"
68 /* Define possible missing constants for Windows. */
69 #ifdef G_OS_WIN32
70 # ifndef S_IRGRP
71 # define S_IRGRP 0
72 # define S_IWGRP 0
73 # endif
74 # ifndef S_IROTH
75 # define S_IROTH 0
76 # define S_IWOTH 0
77 # endif
78 #endif
80 static GList *folder_list = NULL;
81 static GSList *class_list = NULL;
82 static GSList *folder_unloaded_list = NULL;
84 void folder_init (Folder *folder,
85 const gchar *name);
87 static gchar *folder_item_get_cache_file (FolderItem *item);
88 static gchar *folder_item_get_mark_file (FolderItem *item);
89 static gchar *folder_item_get_tags_file (FolderItem *item);
90 static GNode *folder_get_xml_node (Folder *folder);
91 static Folder *folder_get_from_xml (GNode *node);
92 static void folder_update_op_count_rec (GNode *node);
95 static void folder_get_persist_prefs_recursive
96 (GNode *node, GHashTable *pptable);
97 static gboolean persist_prefs_free (gpointer key, gpointer val, gpointer data);
98 static void folder_item_read_cache (FolderItem *item);
99 gint folder_item_scan_full (FolderItem *item, gboolean filtering);
100 static void folder_item_update_with_msg (FolderItem *item, FolderItemUpdateFlags update_flags,
101 MsgInfo *msg);
102 static GHashTable *folder_persist_prefs_new (Folder *folder);
103 static void folder_persist_prefs_free (GHashTable *pptable);
104 static void folder_item_restore_persist_prefs (FolderItem *item, GHashTable *pptable);
106 void folder_system_init(void)
108 folder_register_class(mh_get_class());
109 folder_register_class(imap_get_class());
110 folder_register_class(news_get_class());
113 static GSList *folder_get_class_list(void)
115 return class_list;
118 void folder_register_class(FolderClass *klass)
120 GSList *xmllist, *cur;
122 debug_print("registering folder class %s\n", klass->idstr);
124 class_list = g_slist_append(class_list, klass);
126 xmllist = g_slist_copy(folder_unloaded_list);
127 for (cur = xmllist; cur != NULL; cur = g_slist_next(cur)) {
128 GNode *node = (GNode *) cur->data;
129 XMLNode *xmlnode = (XMLNode *) node->data;
130 GList *cur = xmlnode->tag->attr;
132 for (; cur != NULL; cur = g_list_next(cur)) {
133 XMLAttr *attr = (XMLAttr *) cur->data;
135 if (!attr || !attr->name || !attr->value) continue;
136 if (!strcmp(attr->name, "type") && !strcmp(attr->value, klass->idstr)) {
137 Folder *folder;
139 folder = folder_get_from_xml(node);
140 if (folder) {
141 folder_add(folder);
142 folder_unloaded_list = g_slist_remove(folder_unloaded_list, node);
143 xml_free_tree(node);
145 cur = NULL;
146 continue;
150 g_slist_free(xmllist);
153 void folder_unregister_class(FolderClass *klass)
155 GList *folderlist, *cur;
157 debug_print("unregistering folder class %s\n", klass->idstr);
159 class_list = g_slist_remove(class_list, klass);
161 folderlist = g_list_copy(folder_get_list());
162 for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
163 Folder *folder = (Folder *) cur->data;
165 if (folder->klass == klass) {
166 GNode *xmlnode = folder_get_xml_node(folder);
167 folder_unloaded_list = g_slist_append(folder_unloaded_list, xmlnode);
168 folder_destroy(folder);
171 g_list_free(folderlist);
173 if (klass->prefs_pages)
174 g_slist_free(klass->prefs_pages);
177 Folder *folder_new(FolderClass *klass, const gchar *name, const gchar *path)
179 Folder *folder = NULL;
180 FolderItem *item;
182 cm_return_val_if_fail(klass != NULL, NULL);
184 name = name ? name : path;
185 folder = klass->new_folder(name, path);
187 /* Create root folder item */
188 item = folder_item_new(folder, name, NULL);
189 if (item == NULL) {
190 return NULL;
192 item->folder = folder;
193 folder->node = item->node;
194 folder->data = NULL;
196 return folder;
199 void folder_init(Folder *folder, const gchar *name)
201 cm_return_if_fail(folder != NULL);
203 folder_set_name(folder, name);
205 /* Init folder data */
206 folder->account = NULL;
207 folder->sort = 0;
208 folder->inbox = NULL;
209 folder->outbox = NULL;
210 folder->draft = NULL;
211 folder->queue = NULL;
212 folder->trash = NULL;
215 static void reset_parent_type(FolderItem *item, gpointer data) {
216 item->parent_stype = -1;
219 void folder_item_change_type(FolderItem *item, SpecialFolderItemType newtype)
221 Folder *folder = NULL;
222 FolderUpdateData hookdata;
224 if (item == NULL)
225 return;
227 folder = item->folder;
228 /* unset previous root of newtype */
229 switch(newtype) {
230 case F_INBOX:
231 folder_item_change_type(folder->inbox, F_NORMAL);
232 folder->inbox = item;
233 break;
234 case F_OUTBOX:
235 folder_item_change_type(folder->outbox, F_NORMAL);
236 folder->outbox = item;
237 break;
238 case F_QUEUE:
239 folder_item_change_type(folder->queue, F_NORMAL);
240 folder->queue = item;
241 break;
242 case F_DRAFT:
243 folder_item_change_type(folder->draft, F_NORMAL);
244 folder->draft = item;
245 break;
246 case F_TRASH:
247 folder_item_change_type(folder->trash, F_NORMAL);
248 folder->trash = item;
249 break;
250 case F_NORMAL:
251 default:
252 break;
254 /* set new type for current folder and sons */
255 item->stype = newtype;
256 folder_func_to_all_folders(reset_parent_type, NULL);
258 hookdata.folder = folder;
259 hookdata.update_flags = FOLDER_TREE_CHANGED;
260 hookdata.item = NULL;
261 hookdata.item2 = NULL;
262 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
265 void folder_destroy(Folder *folder)
267 cm_return_if_fail(folder != NULL);
268 cm_return_if_fail(folder->klass->destroy_folder != NULL);
270 folder_remove(folder);
272 folder_tree_destroy(folder);
274 folder->klass->destroy_folder(folder);
276 g_free(folder->name);
277 g_free(folder);
280 void folder_set_xml(Folder *folder, XMLTag *tag)
282 GList *cur;
283 FolderItem *rootitem = NULL;
285 if ((folder->node != NULL) && (folder->node->data != NULL))
286 rootitem = (FolderItem *) folder->node->data;
288 for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
289 XMLAttr *attr = (XMLAttr *) cur->data;
291 if (!attr || !attr->name || !attr->value) continue;
292 if (!strcmp(attr->name, "name")) {
293 g_free(folder->name);
294 folder->name = g_strdup(attr->value);
295 if (rootitem != NULL) {
296 g_free(rootitem->name);
297 rootitem->name = g_strdup(attr->value);
299 } else if (!strcmp(attr->name, "account_id")) {
300 PrefsAccount *account;
302 account = account_find_from_id(atoi(attr->value));
303 if (!account)
304 g_warning("account_id: %s not found", attr->value);
305 else {
306 folder->account = account;
307 account->folder = folder;
309 } else if (!strcmp(attr->name, "collapsed")) {
310 if (rootitem != NULL)
311 rootitem->collapsed = *attr->value == '1' ? TRUE : FALSE;
312 } else if (!strcmp(attr->name, "sort")) {
313 folder->sort = atoi(attr->value);
318 XMLTag *folder_get_xml(Folder *folder)
320 XMLTag *tag;
322 tag = xml_tag_new("folder");
324 if (folder->name)
325 xml_tag_add_attr(tag, xml_attr_new("name", folder->name));
326 if (folder->account)
327 xml_tag_add_attr(tag, xml_attr_new_int("account_id", folder->account->account_id));
328 if (folder->node && folder->node->data) {
329 FolderItem *rootitem = (FolderItem *) folder->node->data;
331 xml_tag_add_attr(tag, xml_attr_new("collapsed", rootitem->collapsed ? "1" : "0"));
333 xml_tag_add_attr(tag, xml_attr_new_int("sort", folder->sort));
335 return tag;
338 FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path)
340 FolderItem *item = NULL;
342 cm_return_val_if_fail(folder != NULL, NULL);
344 if (folder->klass->item_new) {
345 item = folder->klass->item_new(folder);
346 } else {
347 item = g_new0(FolderItem, 1);
350 cm_return_val_if_fail(item != NULL, NULL);
352 item->stype = F_NORMAL;
354 if(!g_utf8_validate(name, -1, NULL)) {
355 item->name = g_malloc(strlen(name)*2+1);
356 conv_localetodisp(item->name, strlen(name)*2+1, name);
357 } else {
358 item->name = g_strdup(name);
361 item->path = g_strdup(path);
362 item->mtime = 0;
363 item->new_msgs = 0;
364 item->unread_msgs = 0;
365 item->unreadmarked_msgs = 0;
366 item->marked_msgs = 0;
367 item->total_msgs = 0;
368 item->replied_msgs = 0;
369 item->forwarded_msgs = 0;
370 item->locked_msgs = 0;
371 item->ignored_msgs = 0;
372 item->watched_msgs = 0;
373 item->order = 0;
374 item->last_num = -1;
375 item->cache = NULL;
376 item->no_sub = FALSE;
377 item->no_select = FALSE;
378 item->collapsed = FALSE;
379 item->thread_collapsed = prefs_common.folder_default_thread_collapsed;
380 item->threaded = prefs_common.folder_default_thread;
381 item->hide_read_threads = prefs_common.folder_default_hide_read_threads;
382 item->hide_read_msgs = prefs_common.folder_default_hide_read_msgs;
383 item->hide_del_msgs = prefs_common.folder_default_hide_del_msgs;
384 item->ret_rcpt = FALSE;
385 item->opened = FALSE;
386 item->node = g_node_new(item);
387 item->folder = NULL;
388 item->account = NULL;
389 item->apply_sub = FALSE;
390 item->mark_queue = NULL;
391 item->data = NULL;
392 item->parent_stype = -1;
394 item->sort_key = prefs_common.default_sort_key;
395 item->sort_type = prefs_common.default_sort_type;
397 item->prefs = folder_item_prefs_new();
399 return item;
402 void folder_item_append(FolderItem *parent, FolderItem *item)
404 cm_return_if_fail(parent != NULL);
405 cm_return_if_fail(parent->folder != NULL);
406 cm_return_if_fail(parent->node != NULL);
407 cm_return_if_fail(item != NULL);
409 item->folder = parent->folder;
410 g_node_append(parent->node, item->node);
413 void folder_item_remove(FolderItem *item)
415 GNode *node, *start_node;
416 FolderUpdateData hookdata;
417 gchar *tags_file = NULL;
419 cm_return_if_fail(item != NULL);
420 cm_return_if_fail(item->folder != NULL);
421 cm_return_if_fail(item->folder->node != NULL);
423 start_node = item->node;
425 node = item->folder->node;
427 node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
428 node = node->children;
430 /* remove my children */
431 while (node != NULL) {
432 if (node && node->data) {
433 FolderItem *sub_item = (FolderItem*) node->data;
434 node = node->next;
435 folder_item_remove(sub_item);
439 /* remove myself */
440 if (item->cache != NULL) {
441 msgcache_destroy(item->cache);
442 item->cache = NULL;
445 tags_file = folder_item_get_tags_file(item);
446 if (tags_file) {
447 gchar *tags_dir;
449 claws_unlink(tags_file);
451 tags_dir = g_path_get_dirname(tags_file);
452 if (tags_dir)
453 rmdir(tags_dir);
454 g_free(tags_dir);
456 g_free(tags_file);
458 hookdata.folder = item->folder;
459 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_REMOVE_FOLDERITEM;
460 hookdata.item = item;
461 hookdata.item2 = NULL;
462 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
464 node = start_node;
466 if (item->folder->node == node)
467 item->folder->node = NULL;
469 folder_item_destroy(item);
471 g_node_destroy(node);
474 void folder_item_remove_children(FolderItem *item)
476 GNode *node, *next;
478 cm_return_if_fail(item != NULL);
479 cm_return_if_fail(item->folder != NULL);
480 cm_return_if_fail(item->node != NULL);
482 node = item->node->children;
483 while (node != NULL) {
484 next = node->next;
485 folder_item_remove(FOLDER_ITEM(node->data));
486 node = next;
490 void folder_item_destroy(FolderItem *item)
492 Folder *folder;
494 cm_return_if_fail(item != NULL);
496 folder = item->folder;
497 if (folder) {
498 if (folder->inbox == item)
499 folder->inbox = NULL;
500 else if (folder->outbox == item)
501 folder->outbox = NULL;
502 else if (folder->draft == item)
503 folder->draft = NULL;
504 else if (folder->queue == item)
505 folder->queue = NULL;
506 else if (folder->trash == item)
507 folder->trash = NULL;
510 if (item->cache)
511 folder_item_free_cache(item, TRUE);
512 if (item->prefs)
513 folder_item_prefs_free(item->prefs);
514 g_free(item->name);
515 g_free(item->path);
517 if (item->folder != NULL) {
518 if(item->folder->klass->item_destroy) {
519 item->folder->klass->item_destroy(item->folder, item);
520 } else {
521 g_free(item);
526 FolderItem *folder_item_parent(FolderItem *item)
528 cm_return_val_if_fail(item != NULL, NULL);
529 cm_return_val_if_fail(item->node != NULL, NULL);
531 if (item->node->parent == NULL)
532 return NULL;
533 return (FolderItem *) item->node->parent->data;
536 void folder_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
538 GList *cur;
540 for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
541 XMLAttr *attr = (XMLAttr *) cur->data;
543 if (!attr || !attr->name || !attr->value) continue;
544 if (!strcmp(attr->name, "type")) {
545 if (!g_ascii_strcasecmp(attr->value, "normal"))
546 item->stype = F_NORMAL;
547 else if (!g_ascii_strcasecmp(attr->value, "inbox"))
548 item->stype = F_INBOX;
549 else if (!g_ascii_strcasecmp(attr->value, "outbox"))
550 item->stype = F_OUTBOX;
551 else if (!g_ascii_strcasecmp(attr->value, "draft"))
552 item->stype = F_DRAFT;
553 else if (!g_ascii_strcasecmp(attr->value, "queue"))
554 item->stype = F_QUEUE;
555 else if (!g_ascii_strcasecmp(attr->value, "trash"))
556 item->stype = F_TRASH;
557 } else if (!strcmp(attr->name, "name")) {
558 g_free(item->name);
559 item->name = g_strdup(attr->value);
560 } else if (!strcmp(attr->name, "path")) {
561 g_free(item->path);
562 item->path = g_strdup(attr->value);
563 } else if (!strcmp(attr->name, "mtime"))
564 item->mtime = strtoul(attr->value, NULL, 10);
565 else if (!strcmp(attr->name, "new"))
566 item->new_msgs = atoi(attr->value);
567 else if (!strcmp(attr->name, "unread"))
568 item->unread_msgs = atoi(attr->value);
569 else if (!strcmp(attr->name, "unreadmarked"))
570 item->unreadmarked_msgs = atoi(attr->value);
571 else if (!strcmp(attr->name, "marked"))
572 item->marked_msgs = atoi(attr->value);
573 else if (!strcmp(attr->name, "replied"))
574 item->replied_msgs = atoi(attr->value);
575 else if (!strcmp(attr->name, "forwarded"))
576 item->forwarded_msgs = atoi(attr->value);
577 else if (!strcmp(attr->name, "locked"))
578 item->locked_msgs = atoi(attr->value);
579 else if (!strcmp(attr->name, "ignored"))
580 item->ignored_msgs = atoi(attr->value);
581 else if (!strcmp(attr->name, "watched"))
582 item->watched_msgs = atoi(attr->value);
583 else if (!strcmp(attr->name, "order"))
584 item->order = atoi(attr->value);
585 else if (!strcmp(attr->name, "total"))
586 item->total_msgs = atoi(attr->value);
587 else if (!strcmp(attr->name, "no_sub"))
588 item->no_sub = *attr->value == '1' ? TRUE : FALSE;
589 else if (!strcmp(attr->name, "no_select"))
590 item->no_select = *attr->value == '1' ? TRUE : FALSE;
591 else if (!strcmp(attr->name, "collapsed"))
592 item->collapsed = *attr->value == '1' ? TRUE : FALSE;
593 else if (!strcmp(attr->name, "thread_collapsed"))
594 item->thread_collapsed = *attr->value == '1' ? TRUE : FALSE;
595 else if (!strcmp(attr->name, "threaded"))
596 item->threaded = *attr->value == '1' ? TRUE : FALSE;
597 else if (!strcmp(attr->name, "hidereadmsgs"))
598 item->hide_read_msgs = *attr->value == '1' ? TRUE : FALSE;
599 else if (!strcmp(attr->name, "hidedelmsgs"))
600 item->hide_del_msgs = *attr->value == '1' ? TRUE : FALSE;
601 else if (!strcmp(attr->name, "hidereadthreads"))
602 item->hide_read_threads = *attr->value == '1' ? TRUE : FALSE;
603 else if (!strcmp(attr->name, "reqretrcpt"))
604 item->ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
605 else if (!strcmp(attr->name, "sort_key")) {
606 if (!strcmp(attr->value, "none"))
607 item->sort_key = SORT_BY_NONE;
608 else if (!strcmp(attr->value, "number"))
609 item->sort_key = SORT_BY_NUMBER;
610 else if (!strcmp(attr->value, "size"))
611 item->sort_key = SORT_BY_SIZE;
612 else if (!strcmp(attr->value, "date"))
613 item->sort_key = SORT_BY_DATE;
614 else if (!strcmp(attr->value, "from"))
615 item->sort_key = SORT_BY_FROM;
616 else if (!strcmp(attr->value, "subject"))
617 item->sort_key = SORT_BY_SUBJECT;
618 else if (!strcmp(attr->value, "score"))
619 item->sort_key = SORT_BY_SCORE;
620 else if (!strcmp(attr->value, "label"))
621 item->sort_key = SORT_BY_LABEL;
622 else if (!strcmp(attr->value, "mark"))
623 item->sort_key = SORT_BY_MARK;
624 else if (!strcmp(attr->value, "unread"))
625 item->sort_key = SORT_BY_STATUS;
626 else if (!strcmp(attr->value, "mime"))
627 item->sort_key = SORT_BY_MIME;
628 else if (!strcmp(attr->value, "to"))
629 item->sort_key = SORT_BY_TO;
630 else if (!strcmp(attr->value, "locked"))
631 item->sort_key = SORT_BY_LOCKED;
632 else if (!strcmp(attr->value, "tags"))
633 item->sort_key = SORT_BY_TAGS;
634 else if (!strcmp(attr->value, "thread_date"))
635 item->sort_key = SORT_BY_THREAD_DATE;
636 } else if (!strcmp(attr->name, "sort_type")) {
637 if (!strcmp(attr->value, "ascending"))
638 item->sort_type = SORT_ASCENDING;
639 else
640 item->sort_type = SORT_DESCENDING;
641 } else if (!strcmp(attr->name, "account_id")) {
642 PrefsAccount *account;
644 account = account_find_from_id(atoi(attr->value));
645 if (!account)
646 g_warning("account_id: %s not found", attr->value);
647 else
648 item->account = account;
649 } else if (!strcmp(attr->name, "apply_sub")) {
650 item->apply_sub = *attr->value == '1' ? TRUE : FALSE;
651 } else if (!strcmp(attr->name, "last_seen")) {
652 if (!claws_crashed())
653 item->last_seen = atoi(attr->value);
654 else
655 item->last_seen = 0;
658 /* options without meaning in drafts */
659 if (item->stype == F_DRAFT)
660 item->hide_read_msgs =
661 item->hide_del_msgs =
662 item->hide_read_threads = FALSE;
665 XMLTag *folder_item_get_xml(Folder *folder, FolderItem *item)
667 static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
668 "draft", "queue", "trash"};
669 static gchar *sort_key_str[] = {"none", "number", "size", "date",
670 "from", "subject", "score", "label",
671 "mark", "unread", "mime", "to",
672 "locked", "tags", "thread_date" };
673 XMLTag *tag;
674 gchar *value;
676 tag = xml_tag_new("folderitem");
678 xml_tag_add_attr(tag, xml_attr_new("type", folder_item_stype_str[item->stype]));
679 if (item->name)
680 xml_tag_add_attr(tag, xml_attr_new("name", item->name));
681 if (item->path)
682 xml_tag_add_attr(tag, xml_attr_new("path", item->path));
683 if (item->no_sub)
684 xml_tag_add_attr(tag, xml_attr_new("no_sub", "1"));
685 if (item->no_select)
686 xml_tag_add_attr(tag, xml_attr_new("no_select", "1"));
687 xml_tag_add_attr(tag, xml_attr_new("collapsed", item->collapsed && item->node->children ? "1" : "0"));
688 xml_tag_add_attr(tag, xml_attr_new("thread_collapsed", item->thread_collapsed ? "1" : "0"));
689 xml_tag_add_attr(tag, xml_attr_new("threaded", item->threaded ? "1" : "0"));
690 xml_tag_add_attr(tag, xml_attr_new("hidereadmsgs", item->hide_read_msgs ? "1" : "0"));
691 xml_tag_add_attr(tag, xml_attr_new("hidedelmsgs", item->hide_del_msgs ? "1" : "0"));
692 xml_tag_add_attr(tag, xml_attr_new("hidereadthreads", item->hide_read_threads ? "1" : "0"));
693 if (item->ret_rcpt)
694 xml_tag_add_attr(tag, xml_attr_new("reqretrcpt", "1"));
696 if (item->sort_key != SORT_BY_NONE) {
697 xml_tag_add_attr(tag, xml_attr_new("sort_key", sort_key_str[item->sort_key]));
698 xml_tag_add_attr(tag, xml_attr_new("sort_type", item->sort_type == SORT_ASCENDING ? "ascending" : "descending"));
701 value = g_strdup_printf("%ld", (unsigned long int) item->mtime);
702 xml_tag_add_attr(tag, xml_attr_new("mtime", value));
703 g_free(value);
704 xml_tag_add_attr(tag, xml_attr_new_int("new", item->new_msgs));
705 xml_tag_add_attr(tag, xml_attr_new_int("unread", item->unread_msgs));
706 xml_tag_add_attr(tag, xml_attr_new_int("unreadmarked", item->unreadmarked_msgs));
707 xml_tag_add_attr(tag, xml_attr_new_int("marked", item->marked_msgs));
708 xml_tag_add_attr(tag, xml_attr_new_int("total", item->total_msgs));
709 xml_tag_add_attr(tag, xml_attr_new_int("replied", item->replied_msgs));
710 xml_tag_add_attr(tag, xml_attr_new_int("forwarded", item->forwarded_msgs));
711 xml_tag_add_attr(tag, xml_attr_new_int("locked", item->locked_msgs));
712 xml_tag_add_attr(tag, xml_attr_new_int("ignore", item->ignored_msgs));
713 xml_tag_add_attr(tag, xml_attr_new_int("watched", item->watched_msgs));
714 xml_tag_add_attr(tag, xml_attr_new_int("order", item->order));
716 if (item->account)
717 xml_tag_add_attr(tag, xml_attr_new_int("account_id", item->account->account_id));
718 if (item->apply_sub)
719 xml_tag_add_attr(tag, xml_attr_new("apply_sub", "1"));
721 xml_tag_add_attr(tag, xml_attr_new_int("last_seen", item->last_seen));
723 return tag;
726 void folder_set_ui_func(Folder *folder, FolderUIFunc func, gpointer data)
728 cm_return_if_fail(folder != NULL);
730 folder->ui_func = func;
731 folder->ui_func_data = data;
734 void folder_set_name(Folder *folder, const gchar *name)
736 cm_return_if_fail(folder != NULL);
738 g_free(folder->name);
739 folder->name = name ? g_strdup(name) : NULL;
740 if (folder->node && folder->node->data) {
741 FolderItem *item = (FolderItem *)folder->node->data;
743 g_free(item->name);
744 item->name = name ? g_strdup(name) : NULL;
748 void folder_set_sort(Folder *folder, guint sort)
750 cm_return_if_fail(folder != NULL);
752 if (folder->sort != sort) {
753 folder_remove(folder);
754 folder->sort = sort;
755 folder_add(folder);
759 static gboolean folder_tree_destroy_func(GNode *node, gpointer data) {
760 FolderItem *item = (FolderItem *) node->data;
762 folder_item_destroy(item);
763 return FALSE;
766 void folder_tree_destroy(Folder *folder)
768 GNode *node;
770 cm_return_if_fail(folder != NULL);
772 node = folder->node;
774 prefs_filtering_clear_folder(folder);
776 if (node != NULL) {
777 g_node_traverse(node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
778 folder_tree_destroy_func, NULL);
779 g_node_destroy(node);
780 folder->node = NULL;
784 void folder_add(Folder *folder)
786 Folder *cur_folder;
787 GList *cur;
788 gint i;
789 FolderUpdateData hookdata;
791 cm_return_if_fail(folder != NULL);
793 if ((FOLDER_TYPE(folder) == F_IMAP ||
794 FOLDER_TYPE(folder) == F_NEWS) &&
795 folder->account == NULL) {
796 return;
799 for (i = 0, cur = folder_list; cur != NULL; cur = cur->next, i++) {
800 cur_folder = FOLDER(cur->data);
801 if (cur_folder->sort < folder->sort)
802 break;
805 folder_list = g_list_insert(folder_list, folder, i);
807 hookdata.folder = folder;
808 hookdata.update_flags = FOLDER_ADD_FOLDER;
809 hookdata.item = NULL;
810 hookdata.item2 = NULL;
811 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
814 void folder_remove(Folder *folder)
816 FolderUpdateData hookdata;
818 cm_return_if_fail(folder != NULL);
820 folder_list = g_list_remove(folder_list, folder);
822 hookdata.folder = folder;
823 hookdata.update_flags = FOLDER_REMOVE_FOLDER;
824 hookdata.item = NULL;
825 hookdata.item2 = NULL;
826 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
829 GList *folder_get_list(void)
831 return folder_list;
834 gint folder_read_list(void)
836 GNode *node, *cur;
837 XMLNode *xmlnode;
838 gchar *path;
839 GList *list;
840 gint config_version = -1;
842 path = folder_get_list_path();
843 if (!is_file_exist(path)) return -1;
844 node = xml_parse_file(path);
845 if (!node) return -1;
847 xmlnode = node->data;
848 if (g_strcmp0(xmlnode->tag->tag, "folderlist") != 0) {
849 g_warning("wrong folder list");
850 xml_free_tree(node);
851 return -1;
854 cur = node->children;
855 while (cur != NULL) {
856 Folder *folder;
858 folder = folder_get_from_xml(cur);
859 if (folder != NULL)
860 folder_add(folder);
861 else
862 folder_unloaded_list = g_slist_append(folder_unloaded_list,
863 (gpointer) xml_copy_tree(cur));
864 cur = cur->next;
867 for (list = xmlnode->tag->attr; list != NULL; list = list->next) {
868 XMLAttr *attr = list->data;
870 if (!attr || !attr->name || !attr->value) continue;
871 if (!strcmp(attr->name, "config_version")) {
872 config_version = atoi(attr->value);
873 debug_print("Found folderlist config_version %d\n", config_version);
877 xml_free_tree(node);
879 if (prefs_update_config_version_folderlist(config_version) < 0) {
880 debug_print("Folderlist configuration file version upgrade failed\n");
881 return -2;
884 if (folder_list || folder_unloaded_list)
885 return 0;
886 else
887 return -1;
890 void folder_write_list(void)
892 GList *list;
893 GSList *slist;
894 Folder *folder;
895 gchar *path;
896 PrefFile *pfile;
897 GNode *rootnode;
898 XMLNode *xmlnode;
899 XMLTag *tag;
901 path = folder_get_list_path();
902 if ((pfile = prefs_write_open(path)) == NULL) return;
904 if (xml_file_put_xml_decl(pfile->fp) < 0) {
905 prefs_file_close_revert(pfile);
906 g_warning("failed to start write folder list");
907 return;
909 tag = xml_tag_new("folderlist");
910 xml_tag_add_attr(tag, xml_attr_new_int("config_version",
911 CLAWS_CONFIG_VERSION));
913 xmlnode = xml_node_new(tag, NULL);
915 rootnode = g_node_new(xmlnode);
917 for (list = folder_list; list != NULL; list = list->next) {
918 GNode *node;
920 folder = list->data;
921 node = folder_get_xml_node(folder);
922 if (node != NULL)
923 g_node_append(rootnode, node);
926 for (slist = folder_unloaded_list; slist != NULL; slist = g_slist_next(slist)) {
927 GNode *node = (GNode *) slist->data;
929 g_node_append(rootnode, (gpointer) xml_copy_tree(node));
932 if (xml_write_tree(rootnode, pfile->fp) < 0) {
933 prefs_file_close_revert(pfile);
934 g_warning("failed to write folder list");
935 } else if (prefs_file_close(pfile) < 0) {
936 g_warning("failed to write folder list");
938 xml_free_tree(rootnode);
941 static gboolean folder_scan_tree_func(GNode *node, gpointer data)
943 GHashTable *pptable = (GHashTable *)data;
944 FolderItem *item = (FolderItem *)node->data;
946 folder_item_restore_persist_prefs(item, pptable);
947 folder_item_scan_full(item, FALSE);
949 return FALSE;
952 static gboolean folder_restore_prefs_func(GNode *node, gpointer data)
954 GHashTable *pptable = (GHashTable *)data;
955 FolderItem *item = (FolderItem *)node->data;
957 folder_item_restore_persist_prefs(item, pptable);
959 return FALSE;
962 void folder_scan_tree(Folder *folder, gboolean rebuild)
964 GHashTable *pptable;
965 FolderUpdateData hookdata;
966 Folder *old_folder = folder;
968 if (!folder->klass->scan_tree)
969 return;
971 pptable = folder_persist_prefs_new(folder);
973 if (rebuild)
974 folder_remove(folder);
976 if (folder->klass->scan_tree(folder) < 0) {
977 if (rebuild)
978 folder_add(old_folder);
979 return;
980 } else if (rebuild)
981 folder_add(folder);
983 hookdata.folder = folder;
984 hookdata.update_flags = FOLDER_TREE_CHANGED;
985 hookdata.item = NULL;
986 hookdata.item2 = NULL;
987 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
989 if (rebuild)
990 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_scan_tree_func, pptable);
991 else
992 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_restore_prefs_func, pptable);
994 folder_persist_prefs_free(pptable);
996 prefs_matcher_read_config();
998 folder_write_list();
1001 FolderItem *folder_create_folder(FolderItem *parent, const gchar *name)
1003 FolderItem *new_item;
1005 cm_return_val_if_fail(parent != NULL, NULL);
1007 new_item = parent->folder->klass->create_folder(parent->folder, parent, name);
1008 if (new_item) {
1009 FolderUpdateData hookdata;
1011 new_item->cache = msgcache_new();
1012 new_item->cache_dirty = TRUE;
1013 new_item->mark_dirty = TRUE;
1014 new_item->tags_dirty = TRUE;
1016 hookdata.folder = new_item->folder;
1017 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDERITEM;
1018 hookdata.item = new_item;
1019 hookdata.item2 = NULL;
1020 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
1023 return new_item;
1026 gint folder_item_rename(FolderItem *item, gchar *newname)
1028 gint retval;
1030 cm_return_val_if_fail(item != NULL, -1);
1031 cm_return_val_if_fail(newname != NULL, -1);
1033 retval = item->folder->klass->rename_folder(item->folder, item, newname);
1035 if (retval >= 0) {
1036 FolderItemUpdateData hookdata;
1037 FolderUpdateData hookdata2;
1039 hookdata.item = item;
1040 hookdata.update_flags = F_ITEM_UPDATE_NAME;
1041 hookdata.msg = NULL;
1042 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &hookdata);
1044 hookdata2.folder = item->folder;
1045 hookdata2.item = item;
1046 hookdata2.item2 = NULL;
1047 hookdata2.update_flags = FOLDER_RENAME_FOLDERITEM;
1048 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata2);
1051 return retval;
1054 struct TotalMsgCount
1056 guint new_msgs;
1057 guint unread_msgs;
1058 guint unreadmarked_msgs;
1059 guint marked_msgs;
1060 guint total_msgs;
1061 guint replied_msgs;
1062 guint forwarded_msgs;
1063 guint locked_msgs;
1064 guint ignored_msgs;
1065 guint watched_msgs;
1068 struct FuncToAllFoldersData
1070 FolderItemFunc function;
1071 gpointer data;
1074 static gboolean folder_func_to_all_folders_func(GNode *node, gpointer data)
1076 FolderItem *item;
1077 struct FuncToAllFoldersData *function_data = (struct FuncToAllFoldersData *) data;
1079 cm_return_val_if_fail(node->data != NULL, FALSE);
1081 item = FOLDER_ITEM(node->data);
1082 cm_return_val_if_fail(item != NULL, FALSE);
1084 function_data->function(item, function_data->data);
1086 return FALSE;
1089 void folder_func_to_all_folders(FolderItemFunc function, gpointer data)
1091 GList *list;
1092 Folder *folder;
1093 struct FuncToAllFoldersData function_data;
1095 function_data.function = function;
1096 function_data.data = data;
1098 for (list = folder_list; list != NULL; list = list->next) {
1099 folder = FOLDER(list->data);
1100 if (folder->node)
1101 g_node_traverse(folder->node, G_PRE_ORDER,
1102 G_TRAVERSE_ALL, -1,
1103 folder_func_to_all_folders_func,
1104 &function_data);
1108 static void folder_count_total_msgs_func(FolderItem *item, gpointer data)
1110 struct TotalMsgCount *count = (struct TotalMsgCount *)data;
1112 count->new_msgs += item->new_msgs;
1113 count->unread_msgs += item->unread_msgs;
1114 count->unreadmarked_msgs += item->unreadmarked_msgs;
1115 count->marked_msgs += item->marked_msgs;
1116 count->total_msgs += item->total_msgs;
1117 count->replied_msgs += item->replied_msgs;
1118 count->forwarded_msgs += item->forwarded_msgs;
1119 count->locked_msgs += item->locked_msgs;
1120 count->ignored_msgs += item->ignored_msgs;
1121 count->watched_msgs += item->watched_msgs;
1124 struct TotalMsgStatus
1126 guint new;
1127 guint unread;
1128 guint total;
1129 GString *str;
1132 static gboolean folder_get_status_full_all_func(GNode *node, gpointer data)
1134 FolderItem *item;
1135 struct TotalMsgStatus *status = (struct TotalMsgStatus *)data;
1136 gchar *id;
1138 cm_return_val_if_fail(node->data != NULL, FALSE);
1140 item = FOLDER_ITEM(node->data);
1142 if (!item->path) return FALSE;
1144 status->new += item->new_msgs;
1145 status->unread += item->unread_msgs;
1146 status->total += item->total_msgs;
1148 if (status->str) {
1149 id = folder_item_get_identifier(item);
1150 g_string_append_printf(status->str, "%5d %5d %5d %s\n",
1151 item->new_msgs, item->unread_msgs,
1152 item->total_msgs, id);
1153 g_free(id);
1156 return FALSE;
1159 static void folder_get_status_full_all(GString *str, guint *new, guint *unread,
1160 guint *total)
1162 GList *list;
1163 Folder *folder;
1164 struct TotalMsgStatus status;
1166 status.new = status.unread = status.total = 0;
1167 status.str = str;
1169 debug_print("Counting total number of messages...\n");
1171 for (list = folder_list; list != NULL; list = list->next) {
1172 folder = FOLDER(list->data);
1173 if (folder->node)
1174 g_node_traverse(folder->node, G_PRE_ORDER,
1175 G_TRAVERSE_ALL, -1,
1176 folder_get_status_full_all_func,
1177 &status);
1180 *new = status.new;
1181 *unread = status.unread;
1182 *total = status.total;
1185 gchar *folder_get_status(GPtrArray *folders, gboolean full)
1187 guint new, unread, total;
1188 GString *str;
1189 gint i;
1190 gchar *ret;
1192 new = unread = total = 0;
1194 str = g_string_new(NULL);
1196 if (folders) {
1197 for (i = 0; i < folders->len; i++) {
1198 FolderItem *item;
1200 item = g_ptr_array_index(folders, i);
1201 new += item->new_msgs;
1202 unread += item->unread_msgs;
1203 total += item->total_msgs;
1205 if (full) {
1206 gchar *id;
1208 id = folder_item_get_identifier(item);
1209 g_string_append_printf(str, "%5d %5d %5d %s\n",
1210 item->new_msgs, item->unread_msgs,
1211 item->total_msgs, id);
1212 g_free(id);
1215 } else {
1216 folder_get_status_full_all(full ? str : NULL,
1217 &new, &unread, &total);
1220 if (full)
1221 g_string_append_printf(str, "%5d %5d %5d\n", new, unread, total);
1222 else
1223 g_string_append_printf(str, "%d %d %d\n", new, unread, total);
1225 ret = str->str;
1226 g_string_free(str, FALSE);
1228 return ret;
1231 void folder_count_total_msgs(guint *new_msgs, guint *unread_msgs,
1232 guint *unreadmarked_msgs, guint *marked_msgs,
1233 guint *total_msgs, guint *replied_msgs,
1234 guint *forwarded_msgs, guint *locked_msgs,
1235 guint *ignored_msgs, guint *watched_msgs)
1237 struct TotalMsgCount count;
1239 count.new_msgs = count.unread_msgs = count.unreadmarked_msgs = 0;
1240 count.total_msgs = count.replied_msgs = count.forwarded_msgs = 0;
1241 count.locked_msgs = count.ignored_msgs = count.watched_msgs = 0;
1242 count.marked_msgs = 0;
1244 debug_print("Counting total number of messages...\n");
1246 folder_func_to_all_folders(folder_count_total_msgs_func, &count);
1248 *new_msgs = count.new_msgs;
1249 *unread_msgs = count.unread_msgs;
1250 *unreadmarked_msgs = count.unreadmarked_msgs;
1251 *marked_msgs = count.marked_msgs;
1252 *total_msgs = count.total_msgs;
1253 *replied_msgs = count.replied_msgs;
1254 *forwarded_msgs = count.forwarded_msgs;
1255 *locked_msgs = count.locked_msgs;
1256 *ignored_msgs = count.ignored_msgs;
1257 *watched_msgs = count.watched_msgs;
1260 Folder *folder_find_from_path(const gchar *path)
1262 GList *list;
1263 Folder *folder;
1265 for (list = folder_list; list != NULL; list = list->next) {
1266 folder = list->data;
1267 if ((FOLDER_TYPE(folder) == F_MH ||
1268 FOLDER_TYPE(folder) == F_MBOX) &&
1269 !path_cmp(LOCAL_FOLDER(folder)->rootpath, path))
1270 return folder;
1273 return NULL;
1276 Folder *folder_find_from_name(const gchar *name, FolderClass *klass)
1278 GList *list;
1279 Folder *folder;
1281 for (list = folder_list; list != NULL; list = list->next) {
1282 folder = list->data;
1283 if (folder->klass == klass &&
1284 g_strcmp0(name, folder->name) == 0)
1285 return folder;
1288 return NULL;
1291 static gboolean folder_item_find_func(GNode *node, gpointer data)
1293 FolderItem *item = node->data;
1294 gpointer *d = data;
1295 const gchar *path = d[0];
1297 if (path_cmp(path, item->path) != 0)
1298 return FALSE;
1300 d[1] = item;
1302 return TRUE;
1305 FolderItem *folder_find_item_from_path(const gchar *path)
1307 Folder *folder;
1308 gpointer d[2];
1309 GList *list = folder_get_list();
1311 folder = list ? list->data:NULL;
1313 cm_return_val_if_fail(folder != NULL, NULL);
1315 d[0] = (gpointer)path;
1316 d[1] = NULL;
1317 while (d[1] == NULL && list) {
1318 folder = FOLDER(list->data);
1319 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1320 folder_item_find_func, d);
1321 list = list->next;
1323 return d[1];
1326 static gboolean folder_item_find_func_real_path(GNode *node, gpointer data)
1328 FolderItem *item = node->data;
1329 gpointer *d = data;
1330 const gchar *path = d[0];
1331 gchar *tmp = folder_item_get_path(item);
1332 if (path_cmp(path, tmp) != 0) {
1333 g_free(tmp);
1334 return FALSE;
1336 g_free(tmp);
1337 d[1] = item;
1339 return TRUE;
1342 FolderItem *folder_find_item_from_real_path(const gchar *path)
1344 Folder *folder;
1345 gpointer d[2];
1346 GList *list = folder_get_list();
1348 folder = list ? list->data:NULL;
1350 cm_return_val_if_fail(folder != NULL, NULL);
1352 d[0] = (gpointer)path;
1353 d[1] = NULL;
1354 while (d[1] == NULL && list) {
1355 folder = FOLDER(list->data);
1356 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1357 folder_item_find_func_real_path, d);
1358 list = list->next;
1360 return d[1];
1363 FolderItem *folder_find_child_item_by_name(FolderItem *item, const gchar *name)
1365 GNode *node;
1366 FolderItem *child;
1368 for (node = item->node->children; node != NULL; node = node->next) {
1369 child = FOLDER_ITEM(node->data);
1370 if (g_strcmp0(child->name, name) == 0) {
1371 return child;
1375 return NULL;
1378 FolderClass *folder_get_class_from_string(const gchar *str)
1380 GSList *classlist;
1382 classlist = folder_get_class_list();
1383 for (; classlist != NULL; classlist = g_slist_next(classlist)) {
1384 FolderClass *class = (FolderClass *) classlist->data;
1385 if (g_ascii_strcasecmp(class->idstr, str) == 0)
1386 return class;
1389 return NULL;
1392 gchar *folder_get_identifier(Folder *folder)
1394 gchar *type_str;
1396 cm_return_val_if_fail(folder != NULL, NULL);
1398 type_str = folder->klass->idstr;
1399 return g_strconcat("#", type_str, "/", folder->name, NULL);
1402 gchar *folder_item_get_identifier(FolderItem *item)
1404 gchar *id = NULL;
1405 gchar *folder_id = NULL;
1407 cm_return_val_if_fail(item != NULL, NULL);
1409 if (item->path == NULL)
1410 return NULL;
1412 folder_id = folder_get_identifier(item->folder);
1413 id = g_strconcat(folder_id, "/", item->path, NULL);
1414 g_free(folder_id);
1416 return id;
1419 Folder *folder_find_from_identifier(const gchar *identifier)
1421 gchar *str;
1422 gchar *p;
1423 gchar *name;
1424 FolderClass *class;
1426 cm_return_val_if_fail(identifier != NULL, NULL);
1428 if (*identifier != '#')
1429 return NULL;
1431 Xstrdup_a(str, identifier, return NULL);
1433 p = strchr(str, '/');
1434 if (!p)
1435 return NULL;
1436 *p = '\0';
1437 p++;
1438 class = folder_get_class_from_string(&str[1]);
1439 if (class == NULL)
1440 return NULL;
1442 name = p;
1443 p = strchr(p, '/');
1444 if (p)
1445 return NULL;
1447 return folder_find_from_name(name, class);
1450 FolderItem *folder_find_item_from_identifier(const gchar *identifier)
1452 Folder *folder;
1453 gpointer d[2];
1454 gchar *str;
1455 gchar *p;
1456 gchar *name;
1457 gchar *path;
1458 FolderClass *class;
1460 cm_return_val_if_fail(identifier != NULL, NULL);
1462 if (*identifier != '#')
1463 return folder_find_item_from_path(identifier);
1465 Xstrdup_a(str, identifier, return NULL);
1467 p = strchr(str, '/');
1468 if (!p)
1469 return folder_find_item_from_path(identifier);
1470 *p = '\0';
1471 p++;
1472 class = folder_get_class_from_string(&str[1]);
1473 if (class == NULL)
1474 return folder_find_item_from_path(identifier);
1476 name = p;
1477 p = strchr(p, '/');
1478 if (!p)
1479 return folder_find_item_from_path(identifier);
1480 *p = '\0';
1481 p++;
1483 folder = folder_find_from_name(name, class);
1484 if (!folder)
1485 return folder_find_item_from_path(identifier);
1487 path = p;
1489 d[0] = (gpointer)path;
1490 d[1] = NULL;
1491 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1492 folder_item_find_func, d);
1493 return d[1];
1496 /** Returns the FolderItem from a given identifier
1498 * The FolderItem is created if it doesn't already exist.
1499 * If creation failed, the function returns NULL.
1501 * Identifiers are of the form #type/Mailbox/FolderA/FolderB/FolderC
1503 FolderItem *folder_get_item_from_identifier(const gchar *identifier)
1505 FolderItem *item, *last_parent;
1506 Folder *folder;
1507 gchar *p1, *p2, *str;
1508 size_t len;
1509 FolderClass *class;
1510 gboolean created_something = FALSE;
1512 item = folder_find_item_from_identifier(identifier);
1513 if(item)
1514 return item;
1516 /* trivial sanity check: need at least # and two slashes */
1517 len = strlen(identifier);
1518 if(len < 3)
1519 return NULL;
1521 /* make sure identifier ends with a slash */
1522 if(identifier[len-1] == G_DIR_SEPARATOR) {
1523 Xstrdup_a(str, identifier, return NULL);
1525 else {
1526 Xstrndup_a(str, identifier, len+1, return NULL);
1527 str[len] = G_DIR_SEPARATOR;
1530 /* find folder class */
1531 p1 = strchr(str, G_DIR_SEPARATOR);
1532 if(!p1)
1533 return NULL;
1534 *p1 = '\0';
1535 class = folder_get_class_from_string(&str[1]);
1536 if(!class)
1537 return NULL;
1538 *p1 = G_DIR_SEPARATOR;
1539 ++p1;
1541 /* find folder from class and name */
1542 p2 = strchr(p1, G_DIR_SEPARATOR);
1543 if(!p2)
1544 return NULL;
1545 *p2 = '\0';
1546 folder = folder_find_from_name(p1, class);
1547 if(!folder)
1548 return NULL;
1549 *p2 = G_DIR_SEPARATOR;
1550 ++p2;
1551 p1 = p2;
1553 /* Now, move forward and make sure all sections in the path exist */
1554 last_parent = folder->node->data;
1555 while((p1 = strchr(p1, G_DIR_SEPARATOR)) != NULL) {
1556 *p1 = '\0';
1557 item = folder_find_item_from_identifier(str);
1558 if(!item) {
1559 item = folder_create_folder(last_parent, p2);
1560 if(!item)
1561 return NULL;
1562 debug_print("Created folder '%s'\n", str);
1563 created_something = TRUE;
1564 if(prefs_common.inherit_folder_props && (last_parent != item->folder->node->data)) {
1565 folder_item_prefs_copy_prefs(last_parent, item);
1568 last_parent = item;
1569 *p1 = G_DIR_SEPARATOR;
1570 ++p1;
1571 p2 = p1;
1574 if(created_something)
1575 folder_write_list();
1577 return item;
1582 * Get a displayable name for a FolderItem
1584 * \param item FolderItem for that a name should be created
1585 * \return Displayable name for item, returned string has to
1586 * be freed
1588 gchar *folder_item_get_name(FolderItem *item)
1590 gchar *name = NULL;
1592 cm_return_val_if_fail(item != NULL, g_strdup(""));
1594 switch (item->stype) {
1595 case F_INBOX:
1596 name = g_strdup(!g_strcmp0(item->name, INBOX_DIR) ? _("Inbox") :
1597 item->name);
1598 break;
1599 case F_OUTBOX:
1600 name = g_strdup(!g_strcmp0(item->name, OUTBOX_DIR) ? _("Sent") :
1601 item->name);
1602 break;
1603 case F_QUEUE:
1604 name = g_strdup(!g_strcmp0(item->name, QUEUE_DIR) ? _("Queue") :
1605 item->name);
1606 break;
1607 case F_TRASH:
1608 name = g_strdup(!g_strcmp0(item->name, TRASH_DIR) ? _("Trash") :
1609 item->name);
1610 break;
1611 case F_DRAFT:
1612 name = g_strdup(!g_strcmp0(item->name, DRAFT_DIR) ? _("Drafts") :
1613 item->name);
1614 break;
1615 default:
1616 break;
1619 if (name == NULL) {
1621 * should probably be done by a virtual function,
1622 * the folder knows the ui string and how to abbrev
1624 if (folder_item_parent(item) == NULL) {
1625 name = g_strconcat(item->name, " (", item->folder->klass->uistr, ")", NULL);
1626 } else {
1627 if (FOLDER_CLASS(item->folder) == news_get_class() &&
1628 item->path && !g_strcmp0(item->name, item->path))
1629 name = get_abbrev_newsgroup_name
1630 (item->path,
1631 prefs_common.ng_abbrev_len);
1632 else
1633 name = g_strdup(item->name);
1637 if (name == NULL)
1638 name = g_strdup("");
1640 return name;
1643 gboolean folder_have_mailbox (void)
1645 GList *cur;
1646 for (cur = folder_list; cur != NULL; cur = g_list_next(cur)) {
1647 Folder *folder = FOLDER(cur->data);
1648 if (folder->inbox && folder->outbox)
1649 return TRUE;
1651 return FALSE;
1654 FolderItem *folder_get_default_inbox(void)
1656 GList *flist;
1658 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1659 Folder * folder = FOLDER(flist->data);
1661 if (folder == NULL)
1662 continue;
1663 if (folder->inbox == NULL)
1664 continue;
1665 if (folder->klass->type == F_UNKNOWN)
1666 continue;
1668 return folder->inbox;
1671 return NULL;
1674 FolderItem *folder_get_default_inbox_for_class(FolderType type)
1676 GList *flist;
1678 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1679 Folder * folder = FOLDER(flist->data);
1681 if (folder == NULL)
1682 continue;
1683 if (folder->inbox == NULL)
1684 continue;
1685 if (folder->klass->type != type)
1686 continue;
1688 return folder->inbox;
1691 return NULL;
1694 FolderItem *folder_get_default_outbox(void)
1696 GList *flist;
1698 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1699 Folder * folder = FOLDER(flist->data);
1701 if (folder == NULL)
1702 continue;
1703 if (folder->outbox == NULL)
1704 continue;
1705 if (folder->klass->type == F_UNKNOWN)
1706 continue;
1708 return folder->outbox;
1711 return NULL;
1714 FolderItem *folder_get_default_outbox_for_class(FolderType type)
1716 GList *flist;
1718 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1719 Folder * folder = FOLDER(flist->data);
1721 if (folder == NULL)
1722 continue;
1723 if (folder->outbox == NULL)
1724 continue;
1725 if (folder->klass->type != type)
1726 continue;
1728 return folder->outbox;
1731 return NULL;
1734 FolderItem *folder_get_default_draft(void)
1736 GList *flist;
1738 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1739 Folder * folder = FOLDER(flist->data);
1741 if (folder == NULL)
1742 continue;
1743 if (folder->draft == NULL)
1744 continue;
1745 if (folder->klass->type == F_UNKNOWN)
1746 continue;
1748 return folder->draft;
1751 return NULL;
1754 FolderItem *folder_get_default_draft_for_class(FolderType type)
1756 GList *flist;
1758 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1759 Folder * folder = FOLDER(flist->data);
1761 if (folder == NULL)
1762 continue;
1763 if (folder->draft == NULL)
1764 continue;
1765 if (folder->klass->type != type)
1766 continue;
1768 return folder->draft;
1771 return NULL;
1774 FolderItem *folder_get_default_queue(void)
1776 GList *flist;
1778 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1779 Folder * folder = FOLDER(flist->data);
1781 if (folder == NULL)
1782 continue;
1783 if (folder->queue == NULL)
1784 continue;
1785 if (folder->klass->type == F_UNKNOWN)
1786 continue;
1788 return folder->queue;
1791 return NULL;
1794 FolderItem *folder_get_default_queue_for_class(FolderType type)
1796 GList *flist;
1798 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1799 Folder * folder = FOLDER(flist->data);
1801 if (folder == NULL)
1802 continue;
1803 if (folder->queue == NULL)
1804 continue;
1805 if (folder->klass->type != type)
1806 continue;
1808 return folder->queue;
1811 return NULL;
1814 FolderItem *folder_get_default_trash(void)
1816 GList *flist;
1818 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1819 Folder * folder = FOLDER(flist->data);
1821 if (folder == NULL)
1822 continue;
1823 if (folder->trash == NULL)
1824 continue;
1825 if (folder->klass->type == F_UNKNOWN)
1826 continue;
1828 return folder->trash;
1831 return NULL;
1834 FolderItem *folder_get_default_trash_for_class(FolderType type)
1836 GList *flist;
1838 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1839 Folder * folder = FOLDER(flist->data);
1841 if (folder == NULL)
1842 continue;
1843 if (folder->trash == NULL)
1844 continue;
1845 if (folder->klass->type != type)
1846 continue;
1848 return folder->trash;
1851 return NULL;
1854 #define CREATE_FOLDER_IF_NOT_EXIST(member, dir, type) \
1856 if (!folder->member) { \
1857 item = folder_item_new(folder, dir, dir); \
1858 item->stype = type; \
1859 folder_item_append(rootitem, item); \
1860 folder->member = item; \
1864 void folder_set_missing_folders(void)
1866 Folder *folder;
1867 FolderItem *rootitem;
1868 FolderItem *item;
1869 GList *list;
1871 for (list = folder_list; list != NULL; list = list->next) {
1872 folder = list->data;
1873 if (FOLDER_TYPE(folder) != F_MH) continue;
1874 rootitem = FOLDER_ITEM(folder->node->data);
1875 cm_return_if_fail(rootitem != NULL);
1877 if (folder->inbox && folder->outbox && folder->draft &&
1878 folder->queue && folder->trash)
1879 continue;
1881 if (folder->klass->create_tree(folder) < 0) {
1882 g_warning("%s: can't create the folder tree",
1883 LOCAL_FOLDER(folder)->rootpath);
1884 continue;
1887 CREATE_FOLDER_IF_NOT_EXIST(inbox, INBOX_DIR, F_INBOX);
1888 CREATE_FOLDER_IF_NOT_EXIST(outbox, OUTBOX_DIR, F_OUTBOX);
1889 CREATE_FOLDER_IF_NOT_EXIST(draft, DRAFT_DIR, F_DRAFT);
1890 CREATE_FOLDER_IF_NOT_EXIST(queue, QUEUE_DIR, F_QUEUE);
1891 CREATE_FOLDER_IF_NOT_EXIST(trash, TRASH_DIR, F_TRASH);
1895 static gboolean folder_unref_account_func(GNode *node, gpointer data)
1897 FolderItem *item = node->data;
1898 PrefsAccount *account = data;
1900 if (item->account == account)
1901 item->account = NULL;
1903 return FALSE;
1906 void folder_unref_account_all(PrefsAccount *account)
1908 Folder *folder;
1909 GList *list;
1911 if (!account) return;
1913 for (list = folder_list; list != NULL; list = list->next) {
1914 folder = list->data;
1915 if (folder->account == account)
1916 folder->account = NULL;
1917 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1918 folder_unref_account_func, account);
1922 #undef CREATE_FOLDER_IF_NOT_EXIST
1924 gchar *folder_item_get_path(FolderItem *item)
1926 Folder *folder;
1928 cm_return_val_if_fail(item != NULL, NULL);
1929 folder = item->folder;
1930 cm_return_val_if_fail(folder != NULL, NULL);
1932 return folder->klass->item_get_path(folder, item);
1935 static gint folder_sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1937 MsgInfo *msginfo_a = (MsgInfo *) a;
1938 MsgInfo *msginfo_b = (MsgInfo *) b;
1940 return (msginfo_a->msgnum - msginfo_b->msgnum);
1943 static gint folder_sort_folder_list(gconstpointer a, gconstpointer b)
1945 guint gint_a = GPOINTER_TO_INT(a);
1946 guint gint_b = GPOINTER_TO_INT(b);
1948 return (gint_a - gint_b);
1951 static gint syncronize_flags(FolderItem *item, MsgInfoList *msglist)
1953 GHashTable *relation;
1954 gint ret = 0;
1955 GSList *cur;
1957 if(msglist == NULL)
1958 return 0;
1959 if(item->folder->klass->get_flags == NULL)
1960 return 0;
1961 if (item->no_select)
1962 return 0;
1964 relation = g_hash_table_new(g_direct_hash, g_direct_equal);
1965 if ((ret = item->folder->klass->get_flags(
1966 item->folder, item, msglist, relation)) == 0) {
1967 gpointer data, old_key;
1968 MsgInfo *msginfo;
1969 MsgPermFlags permflags = 0;
1971 folder_item_update_freeze();
1972 folder_item_set_batch(item, TRUE);
1973 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1974 msginfo = (MsgInfo *) cur->data;
1976 if (g_hash_table_lookup_extended(relation, msginfo, &old_key, &data)) {
1977 permflags = GPOINTER_TO_INT(data);
1979 if (msginfo->flags.perm_flags != permflags) {
1980 procmsg_msginfo_change_flags(msginfo,
1981 permflags & ~msginfo->flags.perm_flags, 0,
1982 ~permflags & msginfo->flags.perm_flags, 0);
1986 folder_item_set_batch(item, FALSE);
1987 folder_item_update_thaw();
1989 g_hash_table_destroy(relation);
1991 return ret;
1994 static gint folder_item_syncronize_flags(FolderItem *item)
1996 MsgInfoList *msglist = NULL;
1997 GSList *cur;
1998 gint ret = 0;
2000 cm_return_val_if_fail(item != NULL, -1);
2001 cm_return_val_if_fail(item->folder != NULL, -1);
2002 cm_return_val_if_fail(item->folder->klass != NULL, -1);
2003 if (item->no_select)
2004 return -1;
2006 item->scanning = ITEM_SCANNING_WITH_FLAGS;
2008 if (item->cache == NULL)
2009 folder_item_read_cache(item);
2011 msglist = msgcache_get_msg_list(item->cache);
2013 ret = syncronize_flags(item, msglist);
2015 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
2016 procmsg_msginfo_free((MsgInfo **)&(cur->data));
2019 g_slist_free(msglist);
2021 item->scanning = ITEM_NOT_SCANNING;
2023 return ret;
2026 static void folder_item_process_open (FolderItem *item,
2027 void (*before_proc_func)(gpointer data),
2028 void (*after_proc_func)(gpointer data),
2029 gpointer data)
2031 gchar *buf;
2032 if (item == NULL)
2033 return;
2034 if((item->folder->klass->scan_required != NULL) &&
2035 (item->folder->klass->scan_required(item->folder, item))) {
2036 folder_item_scan_full(item, TRUE);
2037 } else {
2038 folder_item_syncronize_flags(item);
2041 /* Processing */
2042 if (item->prefs->enable_processing_when_opening) {
2043 buf = g_strdup_printf(_("Processing (%s)...\n"),
2044 item->path ? item->path : item->name);
2045 g_free(buf);
2047 if (before_proc_func)
2048 before_proc_func(data);
2050 folder_item_apply_processing(item);
2052 if (after_proc_func)
2053 after_proc_func(data);
2055 item->processing_pending = FALSE;
2056 return;
2059 gint folder_item_open(FolderItem *item)
2061 START_TIMING("");
2062 if (item->no_select)
2063 return -1;
2065 if (item->scanning != ITEM_NOT_SCANNING) {
2066 debug_print("%s is scanning... \n", item->path ? item->path : item->name);
2067 return -2;
2070 item->processing_pending = TRUE;
2071 folder_item_process_open (item, NULL, NULL, NULL);
2073 item->opened = TRUE;
2074 END_TIMING();
2075 return 0;
2078 gint folder_item_close(FolderItem *item)
2080 GSList *mlist, *cur;
2081 Folder *folder;
2083 cm_return_val_if_fail(item != NULL, -1);
2085 if (item->no_select)
2086 return -1;
2088 if (item->new_msgs) {
2089 folder_item_update_freeze();
2090 mlist = folder_item_get_msg_list(item);
2091 for (cur = mlist ; cur != NULL ; cur = cur->next) {
2092 MsgInfo * msginfo;
2094 msginfo = (MsgInfo *) cur->data;
2095 if (MSG_IS_NEW(msginfo->flags))
2096 procmsg_msginfo_unset_flags(msginfo, MSG_NEW, 0);
2097 procmsg_msginfo_free(&msginfo);
2099 g_slist_free(mlist);
2100 folder_item_update_thaw();
2103 folder_item_write_cache(item);
2105 folder_item_update(item, F_ITEM_UPDATE_MSGCNT);
2107 item->opened = FALSE;
2108 folder = item->folder;
2110 if (folder->klass->close == NULL)
2111 return 0;
2113 return folder->klass->close(folder, item);
2116 static MsgInfoList *get_msginfos(FolderItem *item, MsgNumberList *numlist)
2118 MsgInfoList *msglist = NULL;
2119 Folder *folder = item->folder;
2120 if (item->no_select)
2121 return NULL;
2123 if (folder->klass->get_msginfos != NULL)
2124 msglist = folder->klass->get_msginfos(folder, item, numlist);
2125 else {
2126 MsgNumberList *elem;
2128 for (elem = numlist; elem != NULL; elem = g_slist_next(elem)) {
2129 MsgInfo *msginfo;
2130 guint num;
2132 num = GPOINTER_TO_INT(elem->data);
2133 msginfo = folder->klass->get_msginfo(folder, item, num);
2134 if (msginfo != NULL)
2135 msglist = g_slist_prepend(msglist, msginfo);
2139 return msglist;
2142 static MsgInfo *get_msginfo(FolderItem *item, guint num)
2144 MsgNumberList numlist;
2145 MsgInfoList *msglist;
2146 MsgInfo *msginfo = NULL;
2148 numlist.data = GINT_TO_POINTER(num);
2149 numlist.next = NULL;
2150 msglist = get_msginfos(item, &numlist);
2151 if (msglist != NULL)
2152 msginfo = procmsg_msginfo_new_ref(msglist->data);
2153 procmsg_msg_list_free(msglist);
2155 return msginfo;
2158 gint folder_item_scan_full(FolderItem *item, gboolean filtering)
2160 Folder *folder;
2161 GSList *folder_list = NULL, *cache_list = NULL;
2162 GSList *folder_list_cur, *cache_list_cur, *new_list = NULL;
2163 GSList *exists_list = NULL, *elem;
2164 GSList *newmsg_list = NULL;
2165 guint newcnt = 0, unreadcnt = 0, totalcnt = 0;
2166 guint markedcnt = 0, unreadmarkedcnt = 0;
2167 guint repliedcnt = 0, forwardedcnt = 0;
2168 guint lockedcnt = 0, ignoredcnt = 0, watchedcnt = 0;
2170 guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
2171 gboolean update_flags = 0, old_uids_valid = FALSE;
2172 GHashTable *subject_table = NULL;
2174 cm_return_val_if_fail(item != NULL, -1);
2175 if (item->path == NULL) return -1;
2177 folder = item->folder;
2179 cm_return_val_if_fail(folder != NULL, -1);
2180 cm_return_val_if_fail(folder->klass->get_num_list != NULL, -1);
2182 item->scanning = ITEM_SCANNING_WITH_FLAGS;
2184 debug_print("Scanning folder %s for cache changes.\n", item->path ? item->path : "(null)");
2186 /* Get list of messages for folder and cache */
2187 if (folder->klass->get_num_list(item->folder, item, &folder_list, &old_uids_valid) < 0) {
2188 debug_print("Error fetching list of message numbers\n");
2189 item->scanning = ITEM_NOT_SCANNING;
2190 return(-1);
2193 if(prefs_common.thread_by_subject) {
2194 subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2197 if (old_uids_valid) {
2198 if (!item->cache)
2199 folder_item_read_cache(item);
2200 cache_list = msgcache_get_msg_list(item->cache);
2201 } else {
2202 if (item->cache)
2203 msgcache_destroy(item->cache);
2204 item->cache = msgcache_new();
2205 item->cache_dirty = TRUE;
2206 item->mark_dirty = TRUE;
2207 item->tags_dirty = TRUE;
2208 cache_list = NULL;
2211 /* Sort both lists */
2212 cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
2213 folder_list = g_slist_sort(folder_list, folder_sort_folder_list);
2215 cache_list_cur = cache_list;
2216 folder_list_cur = folder_list;
2218 if (cache_list_cur != NULL) {
2219 GSList *cache_list_last;
2221 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
2222 cache_list_last = g_slist_last(cache_list);
2223 cache_max_num = ((MsgInfo *)cache_list_last->data)->msgnum;
2224 } else {
2225 cache_cur_num = G_MAXUINT;
2226 cache_max_num = 0;
2229 if (folder_list_cur != NULL) {
2230 GSList *folder_list_last;
2232 folder_cur_num = GPOINTER_TO_UINT(folder_list_cur->data);
2233 folder_list_last = g_slist_last(folder_list);
2234 folder_max_num = GPOINTER_TO_UINT(folder_list_last->data);
2235 } else {
2236 folder_cur_num = G_MAXUINT;
2237 folder_max_num = 0;
2240 while ((cache_cur_num != G_MAXUINT) || (folder_cur_num != G_MAXUINT)) {
2242 * Message only exists in the folder
2243 * Remember message for fetching
2245 if (folder_cur_num < cache_cur_num) {
2246 gboolean add = FALSE;
2248 switch(FOLDER_TYPE(folder)) {
2249 case F_NEWS:
2250 if (folder_cur_num < cache_max_num)
2251 break;
2253 if (folder->account->max_articles == 0) {
2254 add = TRUE;
2257 if (folder_max_num <= folder->account->max_articles) {
2258 add = TRUE;
2259 } else if (folder_cur_num > (folder_max_num - folder->account->max_articles)) {
2260 add = TRUE;
2262 break;
2263 default:
2264 add = TRUE;
2265 break;
2268 if (add) {
2269 new_list = g_slist_prepend(new_list, GUINT_TO_POINTER(folder_cur_num));
2270 debug_print("Remembered message %u for fetching\n", folder_cur_num);
2273 /* Move to next folder number */
2274 if (folder_list_cur)
2275 folder_list_cur = folder_list_cur->next;
2277 if (folder_list_cur != NULL)
2278 folder_cur_num = GPOINTER_TO_UINT(folder_list_cur->data);
2279 else
2280 folder_cur_num = G_MAXUINT;
2282 continue;
2286 * Message only exists in the cache
2287 * Remove the message from the cache
2289 if (cache_cur_num < folder_cur_num) {
2290 msgcache_remove_msg(item->cache, cache_cur_num);
2291 debug_print("Removed message %u from cache.\n", cache_cur_num);
2293 /* Move to next cache number */
2294 if (cache_list_cur)
2295 cache_list_cur = cache_list_cur->next;
2297 if (cache_list_cur != NULL)
2298 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
2299 else
2300 cache_cur_num = G_MAXUINT;
2302 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
2304 continue;
2308 * Message number exists in folder and cache!
2309 * Check if the message has been modified
2311 if (cache_cur_num == folder_cur_num) {
2312 MsgInfo *msginfo;
2314 msginfo = msgcache_get_msg(item->cache, folder_cur_num);
2315 if (msginfo && folder->klass->is_msg_changed && folder->klass->is_msg_changed(folder, item, msginfo)) {
2316 msgcache_remove_msg(item->cache, msginfo->msgnum);
2317 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(msginfo->msgnum));
2318 procmsg_msginfo_free(&msginfo);
2320 debug_print("Remembering message %u to update...\n", folder_cur_num);
2321 } else if (msginfo) {
2322 exists_list = g_slist_prepend(exists_list, msginfo);
2324 if(prefs_common.thread_by_subject &&
2325 MSG_IS_IGNORE_THREAD(msginfo->flags) &&
2326 !subject_table_lookup(subject_table, msginfo->subject)) {
2327 subject_table_insert(subject_table, msginfo->subject, msginfo);
2331 /* Move to next folder and cache number */
2332 if (cache_list_cur)
2333 cache_list_cur = cache_list_cur->next;
2335 if (folder_list_cur)
2336 folder_list_cur = folder_list_cur->next;
2338 if (cache_list_cur != NULL)
2339 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
2340 else
2341 cache_cur_num = G_MAXUINT;
2343 if (folder_list_cur != NULL)
2344 folder_cur_num = GPOINTER_TO_UINT(folder_list_cur->data);
2345 else
2346 folder_cur_num = G_MAXUINT;
2348 continue;
2352 for(cache_list_cur = cache_list; cache_list_cur != NULL; cache_list_cur = g_slist_next(cache_list_cur))
2353 procmsg_msginfo_free((MsgInfo **)&(cache_list_cur->data));
2355 g_slist_free(cache_list);
2356 g_slist_free(folder_list);
2358 if (new_list != NULL) {
2359 GSList *tmp_list = NULL;
2360 newmsg_list = get_msginfos(item, new_list);
2361 g_slist_free(new_list);
2362 tmp_list = g_slist_concat(g_slist_copy(exists_list), g_slist_copy(newmsg_list));
2363 syncronize_flags(item, tmp_list);
2364 g_slist_free(tmp_list);
2365 } else {
2366 syncronize_flags(item, exists_list);
2369 folder_item_update_freeze();
2371 item->scanning = ITEM_SCANNING;
2373 if (newmsg_list != NULL) {
2374 GSList *elem, *to_filter = NULL;
2375 gboolean do_filter = (filtering == TRUE) &&
2376 (item->folder->account != NULL) &&
2377 (item->folder->account->filter_on_recv) &&
2378 ((item->stype == F_INBOX) ||
2379 ((item->stype == F_NORMAL) &&
2380 (FOLDER_TYPE(item->folder) == F_NEWS)));
2382 for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
2383 MsgInfo *msginfo = (MsgInfo *) elem->data;
2385 msgcache_add_msg(item->cache, msginfo);
2386 if (!do_filter) {
2387 exists_list = g_slist_prepend(exists_list, msginfo);
2389 if(prefs_common.thread_by_subject &&
2390 MSG_IS_IGNORE_THREAD(msginfo->flags) &&
2391 !subject_table_lookup(subject_table, msginfo->subject)) {
2392 subject_table_insert(subject_table, msginfo->subject, msginfo);
2397 if (do_filter) {
2398 GSList *unfiltered;
2400 folder_item_set_batch(item, TRUE);
2401 procmsg_msglist_filter(newmsg_list, item->folder->account,
2402 &to_filter, &unfiltered,
2403 TRUE);
2404 folder_item_set_batch(item, FALSE);
2406 filtering_move_and_copy_msgs(newmsg_list);
2407 if (to_filter != NULL) {
2408 for (elem = to_filter; elem; elem = g_slist_next(elem)) {
2409 MsgInfo *msginfo = (MsgInfo *)elem->data;
2410 procmsg_msginfo_free(&msginfo);
2412 g_slist_free(to_filter);
2414 if (unfiltered != NULL) {
2415 for (elem = unfiltered; elem; elem = g_slist_next(elem)) {
2416 MsgInfo *msginfo = (MsgInfo *)elem->data;
2417 exists_list = g_slist_prepend(exists_list, msginfo);
2419 if(prefs_common.thread_by_subject &&
2420 MSG_IS_IGNORE_THREAD(msginfo->flags) &&
2421 !subject_table_lookup(subject_table, msginfo->subject)) {
2422 subject_table_insert(subject_table, msginfo->subject, msginfo);
2425 g_slist_free(unfiltered);
2427 if (prefs_common.real_time_sync)
2428 folder_item_synchronise(item);
2429 } else {
2430 if (prefs_common.real_time_sync)
2431 folder_item_synchronise(item);
2434 g_slist_free(newmsg_list);
2436 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
2439 folder_item_set_batch(item, TRUE);
2440 for (elem = exists_list; elem != NULL; elem = g_slist_next(elem)) {
2441 MsgInfo *msginfo, *parent_msginfo;
2443 msginfo = elem->data;
2444 if (MSG_IS_IGNORE_THREAD(msginfo->flags) && (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2445 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2446 if (!MSG_IS_IGNORE_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD)) {
2447 procmsg_msginfo_change_flags(msginfo, MSG_IGNORE_THREAD, 0, MSG_NEW | MSG_UNREAD, 0);
2449 if (!MSG_IS_WATCH_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_WATCH_THREAD)) {
2450 procmsg_msginfo_set_flags(msginfo, MSG_WATCH_THREAD, 0);
2452 if(prefs_common.thread_by_subject && !msginfo->inreplyto &&
2453 !msginfo->references && !MSG_IS_IGNORE_THREAD(msginfo->flags) &&
2454 (parent_msginfo = subject_table_lookup(subject_table, msginfo->subject)))
2456 if(MSG_IS_IGNORE_THREAD(parent_msginfo->flags)) {
2457 procmsg_msginfo_change_flags(msginfo, MSG_IGNORE_THREAD, 0,
2458 MSG_NEW | MSG_UNREAD, 0);
2461 if ((folder_has_parent_of_type(item, F_OUTBOX) ||
2462 folder_has_parent_of_type(item, F_QUEUE) ||
2463 folder_has_parent_of_type(item, F_TRASH)) &&
2464 (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2465 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2466 if (MSG_IS_NEW(msginfo->flags))
2467 newcnt++;
2468 if (MSG_IS_UNREAD(msginfo->flags))
2469 unreadcnt++;
2470 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2471 unreadmarkedcnt++;
2472 if (MSG_IS_MARKED(msginfo->flags))
2473 markedcnt++;
2474 if (MSG_IS_REPLIED(msginfo->flags))
2475 repliedcnt++;
2476 if (MSG_IS_FORWARDED(msginfo->flags))
2477 forwardedcnt++;
2478 if (MSG_IS_LOCKED(msginfo->flags))
2479 lockedcnt++;
2480 if (MSG_IS_IGNORE_THREAD(msginfo->flags))
2481 ignoredcnt++;
2482 if (MSG_IS_WATCH_THREAD(msginfo->flags))
2483 watchedcnt++;
2485 totalcnt++;
2487 procmsg_msginfo_free(&msginfo);
2489 folder_item_set_batch(item, FALSE);
2490 g_slist_free(exists_list);
2492 if(prefs_common.thread_by_subject) {
2493 g_hash_table_destroy(subject_table);
2496 if (item->new_msgs != newcnt || item->unread_msgs != unreadcnt
2497 || item->total_msgs != totalcnt || item->marked_msgs != markedcnt
2498 || item->unreadmarked_msgs != unreadmarkedcnt
2499 || item->replied_msgs != repliedcnt || item->forwarded_msgs != forwardedcnt
2500 || item->locked_msgs != lockedcnt || item->ignored_msgs != ignoredcnt
2501 || item->watched_msgs != watchedcnt) {
2502 update_flags |= F_ITEM_UPDATE_CONTENT;
2505 item->new_msgs = newcnt;
2506 item->unread_msgs = unreadcnt;
2507 item->total_msgs = totalcnt;
2508 item->unreadmarked_msgs = unreadmarkedcnt;
2509 item->marked_msgs = markedcnt;
2510 item->replied_msgs = repliedcnt;
2511 item->forwarded_msgs = forwardedcnt;
2512 item->locked_msgs = lockedcnt;
2513 item->ignored_msgs = ignoredcnt;
2514 item->watched_msgs = watchedcnt;
2516 update_flags |= F_ITEM_UPDATE_MSGCNT;
2518 folder_item_update(item, update_flags);
2519 folder_item_update_thaw();
2521 item->scanning = ITEM_NOT_SCANNING;
2523 return 0;
2526 gint folder_item_scan(FolderItem *item)
2528 return folder_item_scan_full(item, TRUE);
2531 static void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
2533 gint *memusage = (gint *)data;
2535 if (item->cache == NULL)
2536 return;
2538 *memusage += msgcache_get_memory_usage(item->cache);
2541 static gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
2543 FolderItem *fa = (FolderItem *)a;
2544 FolderItem *fb = (FolderItem *)b;
2546 return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
2549 static void folder_find_expired_caches(FolderItem *item, gpointer data)
2551 GSList **folder_item_list = (GSList **)data;
2552 gint difftime, expiretime;
2554 if (item->cache == NULL)
2555 return;
2557 if (item->opened > 0)
2558 return;
2560 difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
2561 expiretime = prefs_common.cache_min_keep_time * 60;
2562 debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
2564 if (difftime > expiretime && !item->opened && !item->processing_pending) {
2565 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
2569 gboolean folder_item_free_cache(FolderItem *item, gboolean force)
2571 cm_return_val_if_fail(item != NULL, TRUE);
2573 if (item->cache == NULL)
2574 return TRUE;
2576 if (item->opened > 0 && !force)
2577 return FALSE;
2579 folder_item_write_cache(item);
2580 msgcache_destroy(item->cache);
2581 item->cache = NULL;
2582 return TRUE;
2585 void folder_clean_cache_memory_force(void)
2587 int old_cache_max_mem_usage = prefs_common.cache_max_mem_usage;
2588 int old_cache_min_keep_time = prefs_common.cache_min_keep_time;
2590 prefs_common.cache_max_mem_usage = 0;
2591 prefs_common.cache_min_keep_time = 0;
2593 folder_clean_cache_memory(NULL);
2595 prefs_common.cache_max_mem_usage = old_cache_max_mem_usage;
2596 prefs_common.cache_min_keep_time = old_cache_min_keep_time;
2599 void folder_clean_cache_memory(FolderItem *protected_item)
2601 gint memusage = 0;
2603 folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);
2604 debug_print("Total cache memory usage: %d\n", memusage);
2606 if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
2607 GSList *folder_item_list = NULL, *listitem;
2609 debug_print("Trying to free cache memory\n");
2611 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);
2612 listitem = folder_item_list;
2613 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
2614 FolderItem *item = (FolderItem *)(listitem->data);
2615 gint cache_size = 0;
2616 if (item == protected_item) {
2617 listitem = listitem->next;
2618 continue;
2620 debug_print("Freeing cache memory for %s\n", item->path ? item->path : item->name);
2621 cache_size = msgcache_get_memory_usage(item->cache);
2622 if (folder_item_free_cache(item, FALSE))
2623 memusage -= cache_size;
2625 listitem = listitem->next;
2627 g_slist_free(folder_item_list);
2631 static void folder_item_remove_cached_msg(FolderItem *item, MsgInfo *msginfo)
2633 Folder *folder = item->folder;
2635 cm_return_if_fail(folder != NULL);
2637 if (folder->klass->remove_cached_msg == NULL)
2638 return;
2640 folder->klass->remove_cached_msg(folder, item, msginfo);
2643 static void folder_item_clean_local_files(FolderItem *item, gint days)
2645 cm_return_if_fail(item != NULL);
2646 cm_return_if_fail(item->folder != NULL);
2648 if (FOLDER_TYPE(item->folder) == F_IMAP ||
2649 FOLDER_TYPE(item->folder) == F_NEWS) {
2650 GSList *msglist = folder_item_get_msg_list(item);
2651 GSList *cur;
2652 time_t t = time(NULL);
2653 for (cur = msglist; cur; cur = cur->next) {
2654 MsgInfo *msginfo = (MsgInfo *)cur->data;
2655 gint age = (t - msginfo->date_t) / (60*60*24);
2656 if (age > days)
2657 folder_item_remove_cached_msg(item, msginfo);
2659 procmsg_msg_list_free(msglist);
2663 static void folder_item_read_cache(FolderItem *item)
2665 gchar *cache_file, *mark_file, *tags_file;
2666 START_TIMING("");
2667 cm_return_if_fail(item != NULL);
2669 if (item->path != NULL) {
2670 cache_file = folder_item_get_cache_file(item);
2671 mark_file = folder_item_get_mark_file(item);
2672 tags_file = folder_item_get_tags_file(item);
2673 item->cache = msgcache_read_cache(item, cache_file);
2674 item->cache_dirty = FALSE;
2675 item->mark_dirty = FALSE;
2676 item->tags_dirty = FALSE;
2677 if (!item->cache) {
2678 MsgInfoList *list, *cur;
2679 guint newcnt = 0, unreadcnt = 0;
2680 guint markedcnt = 0, unreadmarkedcnt = 0;
2681 guint repliedcnt = 0, forwardedcnt = 0;
2682 guint lockedcnt = 0, ignoredcnt = 0;
2683 guint watchedcnt = 0;
2684 MsgInfo *msginfo;
2686 item->cache = msgcache_new();
2687 item->cache_dirty = TRUE;
2688 item->mark_dirty = TRUE;
2689 item->tags_dirty = TRUE;
2690 folder_item_scan_full(item, TRUE);
2692 msgcache_read_mark(item->cache, mark_file);
2694 list = msgcache_get_msg_list(item->cache);
2695 for (cur = list; cur != NULL; cur = g_slist_next(cur)) {
2696 msginfo = cur->data;
2698 if (MSG_IS_NEW(msginfo->flags))
2699 newcnt++;
2700 if (MSG_IS_UNREAD(msginfo->flags))
2701 unreadcnt++;
2702 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2703 unreadmarkedcnt++;
2704 if (MSG_IS_MARKED(msginfo->flags))
2705 markedcnt++;
2706 if (MSG_IS_REPLIED(msginfo->flags))
2707 repliedcnt++;
2708 if (MSG_IS_FORWARDED(msginfo->flags))
2709 forwardedcnt++;
2710 if (MSG_IS_LOCKED(msginfo->flags))
2711 lockedcnt++;
2712 if (MSG_IS_IGNORE_THREAD(msginfo->flags))
2713 ignoredcnt++;
2714 if (MSG_IS_WATCH_THREAD(msginfo->flags))
2715 watchedcnt++;
2716 procmsg_msginfo_unset_flags(msginfo, MSG_FULLY_CACHED, 0);
2718 item->new_msgs = newcnt;
2719 item->unread_msgs = unreadcnt;
2720 item->unreadmarked_msgs = unreadmarkedcnt;
2721 item->marked_msgs = markedcnt;
2722 item->replied_msgs = repliedcnt;
2723 item->forwarded_msgs = forwardedcnt;
2724 item->locked_msgs = lockedcnt;
2725 item->ignored_msgs = ignoredcnt;
2726 item->watched_msgs = watchedcnt;
2727 procmsg_msg_list_free(list);
2728 } else
2729 msgcache_read_mark(item->cache, mark_file);
2731 msgcache_read_tags(item->cache, tags_file);
2733 g_free(cache_file);
2734 g_free(mark_file);
2735 g_free(tags_file);
2736 } else {
2737 item->cache = msgcache_new();
2738 item->cache_dirty = TRUE;
2739 item->mark_dirty = TRUE;
2740 item->tags_dirty = TRUE;
2743 END_TIMING();
2744 folder_clean_cache_memory(item);
2747 void folder_item_write_cache(FolderItem *item)
2749 gchar *cache_file = NULL, *mark_file = NULL, *tags_file = NULL;
2750 FolderItemPrefs *prefs;
2751 gint filemode = 0;
2752 gchar *id;
2753 time_t last_mtime = (time_t)0;
2754 gboolean need_scan = FALSE;
2756 if (!item || !item->path || !item->cache)
2757 return;
2759 last_mtime = item->mtime;
2760 if (item->folder->klass->set_mtime) {
2761 if (item->folder->klass->scan_required)
2762 need_scan = item->folder->klass->scan_required(item->folder, item);
2763 else
2764 need_scan = TRUE;
2767 id = folder_item_get_identifier(item);
2768 debug_print("Save cache for folder %s\n", id);
2769 g_free(id);
2771 if (item->cache_dirty)
2772 cache_file = folder_item_get_cache_file(item);
2773 if (item->cache_dirty || item->mark_dirty)
2774 mark_file = folder_item_get_mark_file(item);
2775 if (item->cache_dirty || item->tags_dirty)
2776 tags_file = folder_item_get_tags_file(item);
2777 if (msgcache_write(cache_file, mark_file, tags_file, item->cache) == 0) {
2778 prefs = item->prefs;
2779 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
2780 filemode = prefs->folder_chmod;
2781 if (filemode & S_IRGRP) filemode |= S_IWGRP;
2782 if (filemode & S_IROTH) filemode |= S_IWOTH;
2783 if (cache_file) {
2784 if (chmod(cache_file, filemode) < 0)
2785 FILE_OP_ERROR(cache_file, "chmod");
2787 if (mark_file) {
2788 if (chmod(mark_file, filemode) < 0)
2789 FILE_OP_ERROR(mark_file, "chmod");
2792 } else {
2793 item->cache_dirty = TRUE;
2794 item->mark_dirty = TRUE;
2795 item->tags_dirty = TRUE;
2798 if (!need_scan && item->folder->klass->set_mtime) {
2799 if (item->mtime == last_mtime) {
2800 item->folder->klass->set_mtime(item->folder, item);
2804 g_free(cache_file);
2805 g_free(mark_file);
2806 g_free(tags_file);
2809 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
2811 MsgInfo *msginfo = NULL;
2813 cm_return_val_if_fail(item != NULL, NULL);
2814 if (item->no_select)
2815 return NULL;
2816 if (!item->cache)
2817 folder_item_read_cache(item);
2819 if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
2820 return msginfo;
2822 msginfo = get_msginfo(item, num);
2823 if (msginfo != NULL) {
2824 msgcache_add_msg(item->cache, msginfo);
2825 return msginfo;
2828 return NULL;
2831 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
2833 MsgInfo *msginfo;
2835 cm_return_val_if_fail(item != NULL, NULL);
2836 cm_return_val_if_fail(msgid != NULL, NULL);
2837 if (item->no_select)
2838 return NULL;
2840 if (!item->cache)
2841 folder_item_read_cache(item);
2843 if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
2844 return msginfo;
2846 return NULL;
2849 GSList *folder_item_get_msg_list(FolderItem *item)
2851 cm_return_val_if_fail(item != NULL, NULL);
2852 if (item->no_select)
2853 return NULL;
2855 if (item->cache == 0)
2856 folder_item_read_cache(item);
2858 cm_return_val_if_fail(item->cache != NULL, NULL);
2860 return msgcache_get_msg_list(item->cache);
2863 static void msginfo_set_mime_flags(GNode *node, gpointer data)
2865 MsgInfo *msginfo = data;
2866 MimeInfo *mimeinfo = node->data;
2868 if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT &&
2869 (!mimeinfo->subtype || (strcmp(mimeinfo->subtype, "pgp-signature") &&
2870 strcmp(mimeinfo->subtype, "x-pkcs7-signature") &&
2871 strcmp(mimeinfo->subtype, "pkcs7-signature")))) {
2872 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2873 } else if (mimeinfo->disposition == DISPOSITIONTYPE_UNKNOWN &&
2874 mimeinfo->id == NULL &&
2875 mimeinfo->type != MIMETYPE_TEXT &&
2876 mimeinfo->type != MIMETYPE_MULTIPART) {
2877 if (!mimeinfo->subtype
2878 || (strcmp(mimeinfo->subtype, "pgp-signature") &&
2879 strcmp(mimeinfo->subtype, "x-pkcs7-signature") &&
2880 strcmp(mimeinfo->subtype, "pkcs7-signature")))
2881 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2882 } else if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE &&
2883 mimeinfo->id == NULL &&
2884 (strcmp(mimeinfo->subtype, "pgp-signature") &&
2885 strcmp(mimeinfo->subtype, "x-pkcs7-signature") &&
2886 strcmp(mimeinfo->subtype, "pkcs7-signature")) &&
2887 (procmime_mimeinfo_get_parameter(mimeinfo, "name") != NULL ||
2888 procmime_mimeinfo_get_parameter(mimeinfo, "filename") != NULL)) {
2889 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2892 /* don't descend below top level message for signed and encrypted info */
2893 if (mimeinfo->type == MIMETYPE_MESSAGE)
2894 return;
2896 if (privacy_mimeinfo_is_signed(mimeinfo)) {
2897 procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
2900 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
2901 procmsg_msginfo_set_flags(msginfo, 0, MSG_ENCRYPTED);
2902 } else {
2903 /* searching inside encrypted parts doesn't really make sense */
2904 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2908 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
2910 Folder *folder;
2911 gchar *msgfile;
2912 MsgInfo *msginfo;
2914 cm_return_val_if_fail(item != NULL, NULL);
2916 folder = item->folder;
2918 cm_return_val_if_fail(folder->klass->fetch_msg != NULL, NULL);
2919 if (item->no_select)
2920 return NULL;
2922 msgfile = folder->klass->fetch_msg(folder, item, num);
2924 if (msgfile != NULL) {
2925 msginfo = folder_item_get_msginfo(item, num);
2926 if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
2927 MimeInfo *mimeinfo;
2929 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
2930 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
2931 mimeinfo = procmime_scan_file(msgfile);
2932 else
2933 mimeinfo = procmime_scan_queue_file(msgfile);
2934 /* check for attachments */
2935 if (mimeinfo != NULL) {
2936 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2937 procmime_mimeinfo_free_all(&mimeinfo);
2939 procmsg_msginfo_set_flags(msginfo, 0, MSG_SCANNED);
2942 procmsg_msginfo_free(&msginfo);
2945 return msgfile;
2948 gchar *folder_item_fetch_msg_full(FolderItem *item, gint num, gboolean headers,
2949 gboolean body)
2951 Folder *folder;
2952 gchar *msgfile;
2953 MsgInfo *msginfo;
2955 cm_return_val_if_fail(item != NULL, NULL);
2956 if (item->no_select)
2957 return NULL;
2959 folder = item->folder;
2961 if (folder->klass->fetch_msg_full == NULL)
2962 return folder_item_fetch_msg(item, num);
2964 if (item->prefs->offlinesync && prefs_common.real_time_sync)
2965 msgfile = folder->klass->fetch_msg_full(folder, item, num,
2966 TRUE, TRUE);
2967 else
2968 msgfile = folder->klass->fetch_msg_full(folder, item, num,
2969 headers, body);
2971 if (msgfile != NULL) {
2972 msginfo = folder_item_get_msginfo(item, num);
2973 if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
2974 MimeInfo *mimeinfo;
2976 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
2977 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
2978 mimeinfo = procmime_scan_file(msgfile);
2979 else
2980 mimeinfo = procmime_scan_queue_file(msgfile);
2981 /* check for attachments */
2982 if (mimeinfo != NULL) {
2983 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2984 procmime_mimeinfo_free_all(&mimeinfo);
2986 procmsg_msginfo_set_flags(msginfo, 0, MSG_SCANNED);
2989 procmsg_msginfo_free(&msginfo);
2992 return msgfile;
2996 static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
2998 static HeaderEntry hentry[] = {{"Message-ID:", NULL, TRUE},
2999 {NULL, NULL, FALSE}};
3000 FILE *fp;
3001 MsgInfo *msginfo;
3002 gint msgnum = 0;
3003 gchar buf[BUFFSIZE];
3005 if ((fp = claws_fopen(file, "rb")) == NULL)
3006 return 0;
3008 if ((folder_has_parent_of_type(dest, F_QUEUE)) ||
3009 (folder_has_parent_of_type(dest, F_DRAFT)))
3010 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
3011 /* new way */
3012 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
3013 strlen("X-Claws-End-Special-Headers:"))) ||
3014 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
3015 strlen("X-Sylpheed-End-Special-Headers:"))))
3016 break;
3017 /* old way */
3018 if (buf[0] == '\r' || buf[0] == '\n') break;
3019 /* from other mailers */
3020 if (!strncmp(buf, "Date: ", 6)
3021 || !strncmp(buf, "To: ", 4)
3022 || !strncmp(buf, "From: ", 6)
3023 || !strncmp(buf, "Subject: ", 9)) {
3024 rewind(fp);
3025 break;
3029 procheader_get_header_fields(fp, hentry);
3030 debug_print("looking for %s\n", hentry[0].body);
3031 if (hentry[0].body) {
3032 extract_parenthesis(hentry[0].body, '<', '>');
3033 remove_space(hentry[0].body);
3034 if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
3035 msgnum = msginfo->msgnum;
3036 procmsg_msginfo_free(&msginfo);
3038 debug_print("found message as uid %d\n", msgnum);
3042 g_free(hentry[0].body);
3043 hentry[0].body = NULL;
3044 claws_fclose(fp);
3046 return msgnum;
3049 static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
3051 MsgPermFlags perm_flags = 0;
3052 MsgTmpFlags tmp_flags = 0;
3054 /* create new flags */
3055 if (source != NULL) {
3056 /* copy original flags */
3057 perm_flags = source->flags.perm_flags;
3058 tmp_flags = source->flags.tmp_flags;
3059 } else {
3060 perm_flags = dest->flags.perm_flags;
3061 tmp_flags = dest->flags.tmp_flags;
3064 /* remove new, unread and deleted in special folders */
3065 if (folder_has_parent_of_type(dest->folder, F_OUTBOX) ||
3066 folder_has_parent_of_type(dest->folder, F_QUEUE) ||
3067 folder_has_parent_of_type(dest->folder, F_DRAFT) ||
3068 folder_has_parent_of_type(dest->folder, F_TRASH))
3069 perm_flags &= ~(MSG_NEW | MSG_UNREAD | MSG_DELETED);
3071 /* set ignore flag of ignored parent exists */
3072 if (procmsg_msg_has_flagged_parent(dest, MSG_IGNORE_THREAD))
3073 perm_flags |= MSG_IGNORE_THREAD;
3075 /* unset FULLY_CACHED flags */
3076 perm_flags &= ~MSG_FULLY_CACHED;
3078 if (procmsg_msg_has_flagged_parent(dest, MSG_WATCH_THREAD))
3079 perm_flags |= MSG_WATCH_THREAD;
3081 /* Unset tmp flags that should not be copied */
3082 tmp_flags &= ~(MSG_MOVE | MSG_COPY | MSG_MOVE_DONE);
3084 /* unset flags that are set but should not */
3085 /* and set new flags */
3086 procmsg_msginfo_change_flags(dest,
3087 ~dest->flags.perm_flags & perm_flags,
3088 ~dest->flags.tmp_flags & tmp_flags,
3089 dest->flags.perm_flags & ~perm_flags,
3090 dest->flags.tmp_flags & ~tmp_flags);
3092 if (source && source->tags) {
3093 g_slist_free(dest->tags);
3094 dest->tags = g_slist_copy(source->tags);
3095 folder_item_commit_tags(dest->folder, dest, dest->tags, NULL);
3099 static void add_msginfo_to_cache(FolderItem *item, MsgInfo *newmsginfo, MsgInfo *flagsource)
3101 /* update folder stats */
3102 if (MSG_IS_NEW(newmsginfo->flags))
3103 item->new_msgs++;
3104 if (MSG_IS_UNREAD(newmsginfo->flags))
3105 item->unread_msgs++;
3106 if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
3107 item->unreadmarked_msgs++;
3108 if (MSG_IS_MARKED(newmsginfo->flags))
3109 item->marked_msgs++;
3110 if (MSG_IS_REPLIED(newmsginfo->flags))
3111 item->replied_msgs++;
3112 if (MSG_IS_FORWARDED(newmsginfo->flags))
3113 item->forwarded_msgs++;
3114 if (MSG_IS_LOCKED(newmsginfo->flags))
3115 item->locked_msgs++;
3116 if (MSG_IS_IGNORE_THREAD(newmsginfo->flags))
3117 item->ignored_msgs++;
3118 if (MSG_IS_WATCH_THREAD(newmsginfo->flags))
3119 item->watched_msgs++;
3120 item->total_msgs++;
3122 folder_item_update_freeze();
3124 if (!item->cache)
3125 folder_item_read_cache(item);
3127 msgcache_add_msg(item->cache, newmsginfo);
3128 copy_msginfo_flags(flagsource, newmsginfo);
3129 folder_item_update_with_msg(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_ADDMSG, newmsginfo);
3130 folder_item_update_thaw();
3133 static void remove_msginfo_from_cache(FolderItem *item, MsgInfo *msginfo)
3135 MsgInfoUpdate msginfo_update;
3137 if (!item->cache)
3138 folder_item_read_cache(item);
3140 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
3141 msginfo->folder->new_msgs--;
3142 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
3143 msginfo->folder->unread_msgs--;
3144 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
3145 msginfo->folder->unreadmarked_msgs--;
3146 if (MSG_IS_MARKED(msginfo->flags))
3147 item->marked_msgs--;
3148 if (MSG_IS_REPLIED(msginfo->flags))
3149 item->replied_msgs--;
3150 if (MSG_IS_FORWARDED(msginfo->flags))
3151 item->forwarded_msgs--;
3152 if (MSG_IS_LOCKED(msginfo->flags))
3153 item->locked_msgs--;
3154 if (MSG_IS_IGNORE_THREAD(msginfo->flags))
3155 item->ignored_msgs--;
3156 if (MSG_IS_WATCH_THREAD(msginfo->flags))
3157 item->watched_msgs--;
3159 msginfo->folder->total_msgs--;
3161 msginfo_update.msginfo = msginfo;
3162 msginfo_update.flags = MSGINFO_UPDATE_DELETED;
3163 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
3165 msgcache_remove_msg(item->cache, msginfo->msgnum);
3166 folder_item_update_with_msg(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_REMOVEMSG, msginfo);
3169 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
3170 MsgFlags *flags, gboolean remove_source)
3172 GSList file_list;
3173 MsgFileInfo fileinfo;
3175 cm_return_val_if_fail(dest != NULL, -1);
3176 cm_return_val_if_fail(file != NULL, -1);
3178 fileinfo.msginfo = NULL;
3179 fileinfo.file = (gchar *)file;
3180 fileinfo.flags = flags;
3181 file_list.data = &fileinfo;
3182 file_list.next = NULL;
3184 return folder_item_add_msgs(dest, &file_list, remove_source);
3187 gint folder_item_add_msgs(FolderItem *dest, GSList *file_list,
3188 gboolean remove_source)
3190 Folder *folder;
3191 gint ret, num, lastnum = -1;
3192 GSList *file_cur;
3193 GHashTable *relation;
3194 MsgFileInfo *fileinfo = NULL;
3195 gboolean folderscan = FALSE;
3197 cm_return_val_if_fail(dest != NULL, -1);
3198 cm_return_val_if_fail(file_list != NULL, -1);
3199 cm_return_val_if_fail(dest->folder != NULL, -1);
3200 if (dest->no_select)
3201 return -1;
3203 folder = dest->folder;
3205 relation = g_hash_table_new(g_direct_hash, g_direct_equal);
3207 if (folder->klass->add_msgs != NULL) {
3208 ret = folder->klass->add_msgs(folder, dest, file_list, relation);
3209 if (ret < 0) {
3210 g_hash_table_destroy(relation);
3211 return ret;
3213 } else {
3214 for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
3215 fileinfo = (MsgFileInfo *) file_cur->data;
3217 ret = folder->klass->add_msg(folder, dest, fileinfo->file, fileinfo->flags);
3218 if (ret < 0) {
3219 g_hash_table_destroy(relation);
3220 return ret;
3222 g_hash_table_insert(relation, fileinfo, GINT_TO_POINTER(ret));
3226 for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
3227 gpointer data, old_key;
3229 fileinfo = (MsgFileInfo *) file_cur->data;
3230 if (g_hash_table_lookup_extended(relation, fileinfo, &old_key, &data))
3231 num = GPOINTER_TO_INT(data);
3232 else
3233 num = -1;
3235 if (num >= 0) {
3236 MsgInfo *newmsginfo;
3238 if (num == 0) {
3239 if (!folderscan) {
3240 folder_item_scan_full(dest, FALSE);
3241 folderscan = TRUE;
3243 num = folder_item_get_msg_num_by_file(dest, fileinfo->file);
3244 debug_print("got num %d\n", num);
3247 if (num > lastnum)
3248 lastnum = num;
3250 if (num >= 0 && remove_source) {
3251 if (claws_unlink(fileinfo->file) < 0)
3252 FILE_OP_ERROR(fileinfo->file, "unlink");
3255 if (num == 0)
3256 continue;
3258 if (!folderscan &&
3259 ((newmsginfo = get_msginfo(dest, num)) != NULL)) {
3260 add_msginfo_to_cache(dest, newmsginfo, NULL);
3261 procmsg_msginfo_free(&newmsginfo);
3262 } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
3263 /* TODO: set default flags */
3264 procmsg_msginfo_free(&newmsginfo);
3269 g_hash_table_destroy(relation);
3271 return lastnum;
3274 static FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest, gboolean copy)
3276 GSList *mlist;
3277 FolderItem *new_item;
3278 FolderItem *next_item;
3279 GNode *srcnode;
3280 gchar *old_id, *new_id;
3281 FolderUpdateData hookdata;
3283 /* move messages */
3284 debug_print("%s %s to %s\n", copy?"Copying":"Moving", src->path, dest->path);
3285 new_item = folder_create_folder(dest, src->name);
3286 if (new_item == NULL) {
3287 g_print("Can't create folder\n");
3288 return NULL;
3291 if (new_item->folder == NULL)
3292 new_item->folder = dest->folder;
3294 /* move messages */
3295 log_message(LOG_PROTOCOL, copy ?_("Copying %s to %s...\n"):_("Moving %s to %s...\n"),
3296 src->name, new_item->path);
3298 /*copy prefs*/
3299 folder_item_prefs_copy_prefs(src, new_item);
3301 /* copy internal data */
3302 if (src->folder->klass == new_item->folder->klass &&
3303 src->folder->klass->copy_private_data != NULL)
3304 src->folder->klass->copy_private_data(src->folder,
3305 src, new_item);
3306 new_item->collapsed = src->collapsed;
3307 new_item->thread_collapsed = src->thread_collapsed;
3308 new_item->threaded = src->threaded;
3309 new_item->ret_rcpt = src->ret_rcpt;
3310 new_item->hide_read_msgs = src->hide_read_msgs;
3311 new_item->hide_del_msgs = src->hide_del_msgs;
3312 new_item->hide_read_threads = src->hide_read_threads;
3313 new_item->sort_key = src->sort_key;
3314 new_item->sort_type = src->sort_type;
3316 mlist = folder_item_get_msg_list(src);
3318 if (mlist != NULL) {
3319 if (copy)
3320 folder_item_copy_msgs(new_item, mlist);
3321 else
3322 folder_item_move_msgs(new_item, mlist);
3323 procmsg_msg_list_free(mlist);
3326 prefs_matcher_write_config();
3328 /* recurse */
3329 srcnode = src->folder->node;
3330 srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
3331 srcnode = srcnode->children;
3332 while (srcnode != NULL) {
3333 if (srcnode && srcnode->data) {
3334 next_item = (FolderItem*) srcnode->data;
3335 srcnode = srcnode->next;
3336 if (folder_item_move_recursive(next_item, new_item, copy) == NULL) {
3337 return NULL;
3341 old_id = folder_item_get_identifier(src);
3342 new_id = folder_item_get_identifier(new_item);
3344 hookdata.folder = src->folder;
3345 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_MOVE_FOLDERITEM;
3346 hookdata.item = src;
3347 hookdata.item2 = new_item;
3348 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
3350 /* if src supports removing, otherwise only copy folder */
3351 if (src->folder->klass->remove_folder != NULL && !copy)
3352 src->folder->klass->remove_folder(src->folder, src);
3353 folder_write_list();
3355 if (!copy) {
3356 debug_print("updating rules : %s => %s\n", old_id, new_id);
3357 if (old_id != NULL && new_id != NULL) {
3358 prefs_filtering_rename_path(old_id, new_id);
3359 account_rename_path(old_id, new_id);
3362 g_free(old_id);
3363 g_free(new_id);
3365 return new_item;
3368 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item, gboolean copy)
3370 FolderItem *tmp = folder_item_parent(dest);
3371 gchar * src_identifier, * dst_identifier;
3372 gchar * phys_srcpath, * phys_dstpath, *tmppath, *tmpname;
3374 while (tmp) {
3375 if (tmp == src) {
3376 return F_MOVE_FAILED_DEST_IS_CHILD;
3378 tmp = folder_item_parent(tmp);
3381 /* both dst and src can be root folders */
3382 src_identifier = folder_item_get_identifier(src);
3383 if (src_identifier == NULL && src->folder && folder_item_parent(src) == NULL) {
3384 src_identifier = folder_get_identifier(src->folder);
3387 dst_identifier = folder_item_get_identifier(dest);
3388 if(dst_identifier == NULL && dest->folder && folder_item_parent(dest) == NULL) {
3389 dst_identifier = folder_get_identifier(dest->folder);
3392 if (src_identifier == NULL || dst_identifier == NULL) {
3393 debug_print("Can't get identifiers\n");
3394 if (src_identifier)
3395 g_free(src_identifier);
3396 if (dst_identifier)
3397 g_free(dst_identifier);
3398 return F_MOVE_FAILED;
3401 if (src->folder != dest->folder && !copy) {
3402 g_free(src_identifier);
3403 g_free(dst_identifier);
3404 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
3406 g_free(src_identifier);
3407 g_free(dst_identifier);
3409 phys_srcpath = folder_item_get_path(src);
3410 tmppath = folder_item_get_path(dest);
3411 tmpname = g_path_get_basename(phys_srcpath);
3412 phys_dstpath = g_strconcat(tmppath,
3413 G_DIR_SEPARATOR_S,
3414 tmpname,
3415 NULL);
3416 g_free(tmpname);
3417 g_free(tmppath);
3419 if (folder_item_parent(src) == dest || src == dest) {
3420 g_free(phys_srcpath);
3421 g_free(phys_dstpath);
3422 return F_MOVE_FAILED_DEST_IS_PARENT;
3424 debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
3425 if ((tmp = folder_item_move_recursive(src, dest, copy)) == NULL) {
3426 g_free(phys_srcpath);
3427 g_free(phys_dstpath);
3428 return F_MOVE_FAILED;
3431 g_free(phys_srcpath);
3432 g_free(phys_dstpath);
3434 *new_item = tmp;
3436 return F_MOVE_OK;
3439 struct find_data
3441 gboolean found;
3443 static void find_num(gpointer key, gpointer value, gpointer data)
3445 struct find_data *fdata = (struct find_data *)data;
3446 if (GPOINTER_TO_INT(value) == 0)
3447 fdata->found = TRUE;
3450 static gboolean some_msgs_have_zero_num(GHashTable *hashtable)
3452 struct find_data fdata;
3454 fdata.found = FALSE;
3455 g_hash_table_foreach(hashtable, find_num, &fdata);
3457 return fdata.found;
3461 * Copy a list of message to a new folder and remove
3462 * source messages if wanted
3464 static gint do_copy_msgs(FolderItem *dest, GSList *msglist, gboolean remove_source)
3466 Folder *folder;
3467 GSList *l;
3468 gint num, lastnum = -1;
3469 gboolean folderscan = FALSE;
3470 GHashTable *relation;
3471 GSList *not_moved = NULL;
3472 gint total = 0, curmsg = 0;
3473 MsgInfo *msginfo = NULL;
3475 cm_return_val_if_fail(dest != NULL, -1);
3476 cm_return_val_if_fail(msglist != NULL, -1);
3478 folder = dest->folder;
3480 cm_return_val_if_fail(folder->klass->copy_msg != NULL, -1);
3481 if (dest->no_select)
3482 return -1;
3484 msginfo = (MsgInfo *)msglist->data;
3486 if (!msginfo)
3487 return -1;
3489 if (!MSG_IS_QUEUED(msginfo->flags) &&
3490 MSG_IS_DRAFT(msginfo->flags) &&
3491 folder_has_parent_of_type(dest, F_QUEUE)) {
3492 GSList *cur = msglist;
3493 gboolean queue_err = FALSE;
3494 for (; cur; cur = cur->next) {
3495 Compose *compose = NULL;
3496 FolderItem *queue = dest;
3497 ComposeQueueResult val = COMPOSE_QUEUE_SUCCESS;
3499 msginfo = (MsgInfo *)cur->data;
3500 compose = compose_reedit(msginfo, TRUE);
3501 if (compose == NULL) {
3502 queue_err = TRUE;
3503 continue;
3505 val = compose_queue(compose, NULL, &queue, NULL,
3506 FALSE);
3507 if (val != COMPOSE_QUEUE_SUCCESS) {
3508 queue_err = TRUE;
3509 } else if (remove_source) {
3510 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
3512 if (val == COMPOSE_QUEUE_SUCCESS)
3513 compose_close(compose);
3515 return queue_err ? -1:0;
3518 relation = g_hash_table_new(g_direct_hash, g_direct_equal);
3520 for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
3521 MsgInfo * msginfo = (MsgInfo *) l->data;
3523 if (msginfo->planned_download != 0) {
3524 int old_planned = msginfo->planned_download;
3525 partial_unmark(msginfo);
3526 /* little hack to reenable after */
3527 msginfo->planned_download = old_planned;
3532 * Copy messages to destination folder and
3533 * store new message numbers in newmsgnums
3535 if (folder->klass->copy_msgs != NULL) {
3536 if (folder->klass->copy_msgs(folder, dest, msglist, relation) < 0) {
3537 g_hash_table_destroy(relation);
3538 return -1;
3540 } else {
3541 MsgInfo * msginfo;
3542 l = msglist;
3544 /* immediately stop if src and dest folders are identical */
3545 if (l != NULL) {
3546 msginfo = (MsgInfo *) l->data;
3547 if (msginfo != NULL && msginfo->folder == dest) {
3548 g_hash_table_destroy(relation);
3549 return -1;
3553 for (; l != NULL ; l = g_slist_next(l)) {
3554 msginfo = (MsgInfo *) l->data;
3556 num = folder->klass->copy_msg(folder, dest, msginfo);
3557 if (num > 0)
3558 g_hash_table_insert(relation, msginfo, GINT_TO_POINTER(num));
3559 else
3560 not_moved = g_slist_prepend(not_moved, msginfo);
3564 if (remove_source) {
3565 MsgInfo *msginfo = (MsgInfo *) msglist->data;
3566 FolderItem *item = msginfo->folder;
3568 * Remove source messages from their folders if
3569 * copying was successfull and update folder
3570 * message counts
3572 if (not_moved == NULL && item->folder->klass->remove_msgs) {
3573 item->folder->klass->remove_msgs(item->folder,
3574 msginfo->folder,
3575 msglist,
3576 relation);
3578 for (l = msglist; l != NULL; l = g_slist_next(l)) {
3579 gpointer old_key, data;
3580 msginfo = (MsgInfo *) l->data;
3581 item = msginfo->folder;
3583 if (g_hash_table_lookup_extended(relation, msginfo, &old_key, &data))
3584 num = GPOINTER_TO_INT(data);
3585 else
3586 num = 0;
3588 if (g_slist_find(not_moved, msginfo))
3589 continue;
3591 if ((num >= 0) && (item->folder->klass->remove_msg != NULL)) {
3592 if (!item->folder->klass->remove_msgs)
3593 item->folder->klass->remove_msg(item->folder,
3594 msginfo->folder,
3595 msginfo->msgnum);
3596 remove_msginfo_from_cache(item, msginfo);
3601 /* Read cache for dest folder */
3602 if (!dest->cache) folder_item_read_cache(dest);
3605 * Fetch new MsgInfos for new messages in dest folder,
3606 * add them to the msgcache and update folder message counts
3608 if (some_msgs_have_zero_num(relation)) {
3609 folder_item_scan_full(dest, FALSE);
3610 folderscan = TRUE;
3613 statusbar_print_all(_("Updating cache for %s..."), dest->path ? dest->path : "(null)");
3614 total = g_slist_length(msglist);
3616 if (FOLDER_TYPE(dest->folder) == F_IMAP && total > 1) {
3617 folder_item_scan_full(dest, FALSE);
3618 folderscan = TRUE;
3620 folder_item_set_batch(dest, TRUE);
3621 for (l = msglist; l != NULL; l = g_slist_next(l)) {
3622 MsgInfo *msginfo = (MsgInfo *) l->data;
3623 gpointer data, old_key;
3625 if (!msginfo)
3626 continue;
3628 if (g_hash_table_lookup_extended(relation, msginfo, &old_key, &data))
3629 num = GPOINTER_TO_INT(data);
3630 else
3631 num = 0;
3633 statusbar_progress_all(curmsg++,total, 100);
3634 if (curmsg % 100 == 0)
3635 GTK_EVENTS_FLUSH();
3637 if (num >= 0) {
3638 MsgInfo *newmsginfo = NULL;
3640 if (!folderscan && num > 0) {
3641 newmsginfo = get_msginfo(dest, num);
3642 if (newmsginfo != NULL) {
3643 add_msginfo_to_cache(dest, newmsginfo, msginfo);
3646 if (newmsginfo == NULL) {
3647 if (!folderscan) {
3648 folder_item_scan_full(dest, FALSE);
3649 folderscan = TRUE;
3651 if (msginfo->msgid != NULL) {
3652 newmsginfo = folder_item_get_msginfo_by_msgid(dest, msginfo->msgid);
3653 if (newmsginfo != NULL) {
3654 copy_msginfo_flags(msginfo, newmsginfo);
3655 num = newmsginfo->msgnum;
3659 if (newmsginfo != NULL
3660 && msginfo->planned_download == POP3_PARTIAL_DLOAD_DELE) {
3661 partial_mark_for_delete(newmsginfo);
3663 if (newmsginfo != NULL
3664 && msginfo->planned_download == POP3_PARTIAL_DLOAD_DLOAD) {
3665 partial_mark_for_download(newmsginfo);
3667 if (!MSG_IS_POSTFILTERED (msginfo->flags)) {
3668 procmsg_msginfo_set_flags ( msginfo, MSG_POSTFILTERED, 0);
3669 if (newmsginfo) {
3670 procmsg_msginfo_set_flags (newmsginfo, MSG_POSTFILTERED, 0);
3671 hooks_invoke (MAIL_POSTFILTERING_HOOKLIST, newmsginfo);
3674 procmsg_msginfo_free(&newmsginfo);
3677 if (num > lastnum)
3678 lastnum = num;
3681 folder_item_set_batch(dest, FALSE);
3682 statusbar_progress_all(0,0,0);
3683 statusbar_pop_all();
3685 g_hash_table_destroy(relation);
3686 if (not_moved != NULL) {
3687 g_slist_free(not_moved);
3688 return -1;
3689 } else
3690 return lastnum;
3694 * Move a message to a new folder.
3696 * \param dest Destination folder
3697 * \param msginfo The message
3699 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
3701 GSList list;
3703 cm_return_val_if_fail(dest != NULL, -1);
3704 cm_return_val_if_fail(msginfo != NULL, -1);
3706 list.data = msginfo;
3707 list.next = NULL;
3709 return do_copy_msgs(dest, &list, TRUE);
3713 * Move a list of messages to a new folder.
3715 * \param dest Destination folder
3716 * \param msglist List of messages
3718 gint folder_item_move_msgs(FolderItem *dest, GSList *msglist)
3720 gint result = -1;
3721 cm_return_val_if_fail(dest != NULL, -1);
3722 cm_return_val_if_fail(msglist != NULL, -1);
3723 inc_lock();
3724 result = do_copy_msgs(dest, msglist, TRUE);
3725 inc_unlock();
3726 return result;
3730 * Copy a message to a new folder.
3732 * \param dest Destination folder
3733 * \param msginfo The message
3735 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
3737 GSList list;
3739 cm_return_val_if_fail(dest != NULL, -1);
3740 cm_return_val_if_fail(msginfo != NULL, -1);
3742 list.data = msginfo;
3743 list.next = NULL;
3745 return do_copy_msgs(dest, &list, FALSE);
3749 * Copy a list of messages to a new folder.
3751 * \param dest Destination folder
3752 * \param msglist List of messages
3754 gint folder_item_copy_msgs(FolderItem *dest, GSList *msglist)
3756 gint result;
3757 cm_return_val_if_fail(dest != NULL, -1);
3758 cm_return_val_if_fail(msglist != NULL, -1);
3760 inc_lock();
3761 result = do_copy_msgs(dest, msglist, FALSE);
3762 inc_unlock();
3764 return result;
3767 gint folder_item_remove_msg(FolderItem *item, gint num)
3769 Folder *folder;
3770 gint ret;
3771 MsgInfo *msginfo;
3773 cm_return_val_if_fail(item != NULL, -1);
3774 folder = item->folder;
3775 cm_return_val_if_fail(folder->klass->remove_msg != NULL, -1);
3776 if (item->no_select)
3777 return -1;
3779 if (!item->cache) folder_item_read_cache(item);
3781 msginfo = msgcache_get_msg(item->cache, num);
3782 if (msginfo && MSG_IS_LOCKED(msginfo->flags)) {
3783 procmsg_msginfo_free(&msginfo);
3784 return -1;
3786 ret = folder->klass->remove_msg(folder, item, num);
3788 if (msginfo != NULL) {
3789 if (ret == 0)
3790 remove_msginfo_from_cache(item, msginfo);
3791 procmsg_msginfo_free(&msginfo);
3794 return ret;
3797 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
3799 Folder *folder;
3800 gint ret = 0;
3801 GSList *real_list = NULL, *cur = NULL;
3803 cm_return_val_if_fail(item != NULL, -1);
3804 folder = item->folder;
3805 cm_return_val_if_fail(folder != NULL, -1);
3806 if (item->no_select)
3807 return -1;
3808 inc_lock();
3809 if (!item->cache) folder_item_read_cache(item);
3811 folder_item_update_freeze();
3813 /* filter out locked mails */
3814 for (cur = msglist; cur; cur = cur->next) {
3815 MsgInfo *info = (MsgInfo *)cur->data;
3816 if (!MSG_IS_LOCKED(info->flags))
3817 real_list = g_slist_prepend(real_list, info);
3820 real_list = g_slist_reverse(real_list);
3822 if (item->folder->klass->remove_msgs) {
3823 ret = item->folder->klass->remove_msgs(item->folder,
3824 item,
3825 real_list,
3826 NULL);
3828 cur = real_list;
3829 while (ret == 0 && cur != NULL) {
3830 MsgInfo *msginfo = (MsgInfo *)cur->data;
3831 if (msginfo && MSG_IS_LOCKED(msginfo->flags)) {
3832 cur = cur->next;
3833 continue;
3835 if (!item->folder->klass->remove_msgs)
3836 ret = folder_item_remove_msg(item, msginfo->msgnum);
3837 if (ret != 0) break;
3838 msgcache_remove_msg(item->cache, msginfo->msgnum);
3839 cur = cur->next;
3841 g_slist_free(real_list);
3842 folder_item_scan_full(item, FALSE);
3843 folder_item_update_thaw();
3844 inc_unlock();
3845 return ret;
3848 gint folder_item_expunge(FolderItem *item)
3850 Folder *folder = item->folder;
3851 gint result = 0;
3852 if (folder == NULL)
3853 return -1;
3854 if (folder->klass->expunge) {
3855 GSList *msglist = folder_item_get_msg_list(item);
3856 GSList *cur;
3857 result = folder->klass->expunge(folder, item);
3858 if (result == 0) {
3859 for (cur = msglist; cur; cur = cur->next) {
3860 MsgInfo *msginfo = (MsgInfo *)cur->data;
3861 if (MSG_IS_DELETED(msginfo->flags)) {
3862 remove_msginfo_from_cache(item, msginfo);
3866 procmsg_msg_list_free(msglist);
3868 return result;
3871 gint folder_item_remove_all_msg(FolderItem *item)
3873 Folder *folder;
3874 gint result;
3876 cm_return_val_if_fail(item != NULL, -1);
3877 if (item->no_select)
3878 return -1;
3880 folder = item->folder;
3882 inc_lock();
3883 if (folder->klass->remove_all_msg != NULL) {
3884 result = folder->klass->remove_all_msg(folder, item);
3886 if (result == 0) {
3887 folder_item_free_cache(item, TRUE);
3888 item->cache = msgcache_new();
3889 item->cache_dirty = TRUE;
3890 item->mark_dirty = TRUE;
3891 item->tags_dirty = TRUE;
3893 } else {
3894 MsgInfoList *msglist;
3896 msglist = folder_item_get_msg_list(item);
3897 result = folder_item_remove_msgs(item, msglist);
3898 procmsg_msg_list_free(msglist);
3901 if (result == 0) {
3902 item->new_msgs = 0;
3903 item->unread_msgs = 0;
3904 item->unreadmarked_msgs = 0;
3905 item->marked_msgs = 0;
3906 item->total_msgs = 0;
3907 item->replied_msgs = 0;
3908 item->forwarded_msgs = 0;
3909 item->locked_msgs = 0;
3910 item->ignored_msgs = 0;
3911 item->watched_msgs = 0;
3912 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
3915 inc_unlock();
3916 return result;
3919 void folder_item_change_msg_flags(FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
3921 cm_return_if_fail(item != NULL);
3922 cm_return_if_fail(msginfo != NULL);
3924 item->mark_dirty = TRUE;
3926 if (item->no_select)
3927 return;
3929 if (item->folder->klass->change_flags != NULL && item->scanning != ITEM_SCANNING_WITH_FLAGS) {
3930 item->folder->klass->change_flags(item->folder, item, msginfo, newflags);
3931 } else {
3932 msginfo->flags.perm_flags = newflags;
3936 void folder_item_commit_tags(FolderItem *item, MsgInfo *msginfo, GSList *tags_set, GSList *tags_unset)
3938 Folder *folder = NULL;
3940 if (!msginfo)
3941 return;
3942 if (!item)
3943 return;
3944 if (!tags_set && !tags_unset)
3945 return;
3947 folder = item->folder;
3948 if (!folder)
3949 return;
3951 item->tags_dirty = TRUE;
3953 if (folder->klass->commit_tags == NULL)
3954 return;
3956 folder->klass->commit_tags(item, msginfo, tags_set, tags_unset);
3959 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
3961 Folder *folder;
3963 cm_return_val_if_fail(item != NULL, FALSE);
3964 if (item->no_select)
3965 return FALSE;
3967 folder = item->folder;
3969 cm_return_val_if_fail(folder->klass->is_msg_changed != NULL, -1);
3971 return folder->klass->is_msg_changed(folder, item, msginfo);
3974 void folder_item_discard_cache(FolderItem *item)
3976 gchar *dir;
3977 gchar *cache;
3979 if (!item)
3980 return;
3982 if (item->cache) {
3983 msgcache_destroy(item->cache);
3984 item->cache = NULL;
3986 dir = folder_item_get_path(item);
3987 if (is_dir_exist(dir))
3988 remove_all_numbered_files(dir);
3989 g_free(dir);
3991 cache = folder_item_get_cache_file(item);
3992 if (is_file_exist(cache))
3993 claws_unlink(cache);
3994 g_free(cache);
3998 static gchar *folder_item_get_cache_file(FolderItem *item)
4000 gchar *path;
4001 gchar *file;
4002 gchar *old_file;
4004 cm_return_val_if_fail(item != NULL, NULL);
4006 path = folder_item_get_path(item);
4007 cm_return_val_if_fail(path != NULL, NULL);
4008 if (!is_dir_exist(path))
4009 make_dir_hier(path);
4010 file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
4011 old_file = g_strconcat(path, G_DIR_SEPARATOR_S, OLD_CACHE_FILE, NULL);
4013 if (!is_file_exist(file) && is_file_exist(old_file))
4014 move_file(old_file, file, FALSE);
4015 g_free(old_file);
4016 g_free(path);
4018 return file;
4021 static gchar *folder_item_get_mark_file(FolderItem *item)
4023 gchar *path;
4024 gchar *file;
4025 gchar *old_file;
4027 cm_return_val_if_fail(item != NULL, NULL);
4028 cm_return_val_if_fail(item->path != NULL, NULL);
4030 path = folder_item_get_path(item);
4031 cm_return_val_if_fail(path != NULL, NULL);
4032 if (!is_dir_exist(path))
4033 make_dir_hier(path);
4034 file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
4035 old_file = g_strconcat(path, G_DIR_SEPARATOR_S, OLD_MARK_FILE, NULL);
4037 if (!is_file_exist(file) && is_file_exist(old_file))
4038 move_file(old_file, file, FALSE);
4039 g_free(old_file);
4040 g_free(path);
4042 return file;
4045 static gchar *folder_item_get_tags_file(FolderItem *item)
4047 gchar *path;
4048 gchar *identifier;
4049 gchar *file;
4051 /* we save tags files in rc_dir, because tagsrc is there too,
4052 * and storing tags directly in the mailboxes would give strange
4053 * result when using another Claws mailbox from another install
4054 * with different tags. */
4056 cm_return_val_if_fail(item != NULL, NULL);
4058 identifier = folder_item_get_identifier(item);
4059 cm_return_val_if_fail(identifier != NULL, NULL);
4061 #ifdef G_OS_WIN32
4062 while (strchr(identifier, '/'))
4063 *strchr(identifier, '/') = '\\';
4064 #endif
4066 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
4067 TAGS_DIR, G_DIR_SEPARATOR_S,
4068 identifier, NULL);
4070 g_free(identifier);
4072 if (!is_dir_exist(path))
4073 make_dir_hier(path);
4075 file = g_strconcat(path, G_DIR_SEPARATOR_S, TAGS_FILE, NULL);
4077 g_free(path);
4079 return file;
4082 static gpointer xml_to_folder_item(gpointer nodedata, gpointer data)
4084 XMLNode *xmlnode = (XMLNode *) nodedata;
4085 Folder *folder = (Folder *) data;
4086 FolderItem *item;
4088 cm_return_val_if_fail(xmlnode != NULL, NULL);
4089 cm_return_val_if_fail(folder != NULL, NULL);
4091 if (g_strcmp0(xmlnode->tag->tag, "folderitem") != 0) {
4092 g_warning("tag name != \"folderitem\"");
4093 return NULL;
4096 item = folder_item_new(folder, "", "");
4097 g_node_destroy(item->node);
4098 if (folder->klass->item_set_xml != NULL)
4099 folder->klass->item_set_xml(folder, item, xmlnode->tag);
4100 else
4101 folder_item_set_xml(folder, item, xmlnode->tag);
4103 item->folder = folder;
4105 switch (item->stype) {
4106 case F_INBOX: folder->inbox = item; break;
4107 case F_OUTBOX: folder->outbox = item; break;
4108 case F_DRAFT: folder->draft = item; break;
4109 case F_QUEUE: folder->queue = item; break;
4110 case F_TRASH: folder->trash = item; break;
4111 default: break;
4113 folder_item_prefs_read_config(item);
4115 return item;
4118 static gboolean folder_item_set_node(GNode *node, gpointer data)
4120 cm_return_val_if_fail(node->data != NULL, -1);
4122 FolderItem *item = (FolderItem *) node->data;
4123 item->node = node;
4125 return FALSE;
4128 static Folder *folder_get_from_xml(GNode *node)
4130 Folder *folder;
4131 XMLNode *xmlnode;
4132 GList *list;
4133 FolderClass *klass = NULL;
4134 GNode *cur;
4136 cm_return_val_if_fail(node->data != NULL, NULL);
4138 xmlnode = node->data;
4139 if (g_strcmp0(xmlnode->tag->tag, "folder") != 0) {
4140 g_warning("tag name != \"folder\"");
4141 return NULL;
4143 list = xmlnode->tag->attr;
4144 for (; list != NULL; list = list->next) {
4145 XMLAttr *attr = list->data;
4147 if (!attr || !attr->name || !attr->value) continue;
4148 if (!strcmp(attr->name, "type"))
4149 klass = folder_get_class_from_string(attr->value);
4151 if (klass == NULL)
4152 return NULL;
4154 folder = folder_new(klass, "", "");
4155 cm_return_val_if_fail(folder != NULL, NULL);
4157 if (klass->set_xml)
4158 klass->set_xml(folder, xmlnode->tag);
4159 else
4160 folder_set_xml(folder, xmlnode->tag);
4162 cur = node->children;
4163 while (cur != NULL) {
4164 GNode *itemnode;
4166 itemnode = g_node_map(cur, xml_to_folder_item, (gpointer) folder);
4167 g_node_append(folder->node, itemnode);
4168 cur = cur->next;
4170 g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, folder_item_set_node, NULL);
4172 return folder;
4175 gchar *folder_get_list_path(void)
4177 static gchar *filename = NULL;
4179 if (!filename)
4180 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
4181 FOLDER_LIST, NULL);
4183 return filename;
4186 static gpointer folder_item_to_xml(gpointer nodedata, gpointer data)
4188 FolderItem *item = (FolderItem *) nodedata;
4189 XMLTag *tag;
4191 cm_return_val_if_fail(item != NULL, NULL);
4193 if (item->folder->klass->item_get_xml != NULL)
4194 tag = item->folder->klass->item_get_xml(item->folder, item);
4195 else
4196 tag = folder_item_get_xml(item->folder, item);
4198 return xml_node_new(tag, NULL);
4201 static GNode *folder_get_xml_node(Folder *folder)
4203 GNode *node;
4204 XMLNode *xmlnode;
4205 XMLTag *tag;
4207 cm_return_val_if_fail(folder != NULL, NULL);
4209 if (folder->klass->get_xml != NULL)
4210 tag = folder->klass->get_xml(folder);
4211 else
4212 tag = folder_get_xml(folder);
4214 xml_tag_add_attr(tag, xml_attr_new("type", folder->klass->idstr));
4216 xmlnode = xml_node_new(tag, NULL);
4218 node = g_node_new(xmlnode);
4220 cm_return_val_if_fail (folder->node != NULL, NULL);
4222 if (folder->node->children) {
4223 GNode *cur;
4225 cur = folder->node->children;
4226 while (cur) {
4227 GNode *xmlnode;
4229 xmlnode = g_node_map(cur, folder_item_to_xml, (gpointer) folder);
4230 g_node_append(node, xmlnode);
4231 cur = cur->next;
4235 return node;
4238 static void folder_update_op_count_rec(GNode *node)
4240 FolderItem *fitem = FOLDER_ITEM(node->data);
4242 if (g_node_depth(node) > 0) {
4243 if (fitem->op_count > 0) {
4244 fitem->op_count = 0;
4245 folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
4247 if (node->children) {
4248 GNode *child;
4250 child = node->children;
4251 while (child) {
4252 GNode *cur;
4254 cur = child;
4255 child = cur->next;
4256 folder_update_op_count_rec(cur);
4262 void folder_update_op_count(void)
4264 GList *cur;
4265 Folder *folder;
4267 for (cur = folder_list; cur != NULL; cur = cur->next) {
4268 folder = cur->data;
4269 folder_update_op_count_rec(folder->node);
4273 typedef struct _type_str {
4274 gchar * str;
4275 gint type;
4276 } type_str;
4280 static gchar * folder_item_get_tree_identifier(FolderItem * item)
4282 if (item->parent != NULL) {
4283 gchar * path;
4284 gchar * id;
4286 path = folder_item_get_tree_identifier(item->parent);
4287 if (path == NULL)
4288 return NULL;
4290 id = g_strconcat(path, "/", item->name, NULL);
4291 g_free(path);
4293 return id;
4295 else {
4296 return g_strconcat("/", item->name, NULL);
4301 /* CLAWS: temporary local folder for filtering */
4302 #define TEMP_FOLDER "TEMP_FOLDER"
4303 #define PROCESSING_FOLDER_ITEM "processing"
4305 static FolderItem *folder_create_processing_folder(int account_id)
4307 static Folder *processing_folder = NULL;
4308 FolderItem *processing_folder_item;
4310 gchar *processing_folder_item_name = NULL;
4312 processing_folder_item_name = g_strdup_printf("%s-%d", PROCESSING_FOLDER_ITEM, account_id);
4314 if (processing_folder == NULL) {
4315 gchar *tmppath =
4316 g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
4317 "tempfolder", NULL);
4318 processing_folder =
4319 folder_new(mh_get_class(), TEMP_FOLDER, tmppath);
4320 g_free(tmppath);
4321 tmppath = NULL;
4323 g_assert(processing_folder != NULL);
4324 processing_folder->klass->scan_tree(processing_folder);
4326 g_assert(processing_folder != NULL);
4328 processing_folder_item = folder_find_child_item_by_name(FOLDER_ITEM(processing_folder->node->data),
4329 processing_folder_item_name);
4330 if (processing_folder_item) {
4331 debug_print("*TMP* already created %s\n", folder_item_get_path(processing_folder_item));
4332 } else {
4333 processing_folder_item = processing_folder->klass->create_folder(processing_folder,
4334 processing_folder->node->data,
4335 processing_folder_item_name);
4336 folder_item_append(FOLDER_ITEM(processing_folder->node->data), processing_folder_item);
4337 debug_print("*TMP* creating %s\n", folder_item_get_path(processing_folder_item));
4339 g_free(processing_folder_item_name);
4340 g_assert(processing_folder_item != NULL);
4342 return(processing_folder_item);
4345 FolderItem *folder_get_default_processing(int account_id)
4347 return folder_create_processing_folder(account_id);
4350 /* folder_persist_prefs_new() - return hash table with persistent
4351 * settings (and folder name as key).
4352 * (note that in claws other options are in the folder_item_prefs_RC
4353 * file, so those don't need to be included in PersistPref yet)
4355 static GHashTable *folder_persist_prefs_new(Folder *folder)
4357 GHashTable *pptable;
4359 cm_return_val_if_fail(folder, NULL);
4360 pptable = g_hash_table_new(g_str_hash, g_str_equal);
4361 folder_get_persist_prefs_recursive(folder->node, pptable);
4362 return pptable;
4365 static void folder_persist_prefs_free(GHashTable *pptable)
4367 cm_return_if_fail(pptable);
4368 g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
4369 g_hash_table_destroy(pptable);
4372 static const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
4374 if (pptable == NULL || name == NULL) return NULL;
4375 return g_hash_table_lookup(pptable, name);
4378 static void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
4380 const PersistPrefs *pp;
4381 gchar *id = folder_item_get_identifier(item);
4383 pp = folder_get_persist_prefs(pptable, id);
4384 g_free(id);
4386 if (!pp) return;
4388 /* CLAWS: since not all folder properties have been migrated to
4389 * folderlist.xml, we need to call the old stuff first before
4390 * setting things that apply both to Main and Claws. */
4391 folder_item_prefs_read_config(item);
4393 item->collapsed = pp->collapsed;
4394 item->thread_collapsed = pp->thread_collapsed;
4395 item->threaded = pp->threaded;
4396 item->ret_rcpt = pp->ret_rcpt;
4397 item->hide_read_msgs = pp->hide_read_msgs;
4398 item->hide_del_msgs = pp->hide_del_msgs;
4399 item->hide_read_threads = pp->hide_read_threads;
4400 item->sort_key = pp->sort_key;
4401 item->sort_type = pp->sort_type;
4404 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
4406 FolderItem *item = FOLDER_ITEM(node->data);
4407 PersistPrefs *pp;
4408 GNode *child, *cur;
4409 gchar *id;
4411 cm_return_if_fail(node != NULL);
4412 cm_return_if_fail(item != NULL);
4414 /* NOTE: item->path == NULL means top level folder; not interesting
4415 * to store preferences of that one. */
4416 if (item->path) {
4417 id = folder_item_get_identifier(item);
4418 pp = g_new0(PersistPrefs, 1);
4419 if (!pp) {
4420 g_free(id);
4421 return;
4423 pp->collapsed = item->collapsed;
4424 pp->thread_collapsed = item->thread_collapsed;
4425 pp->threaded = item->threaded;
4426 pp->ret_rcpt = item->ret_rcpt;
4427 pp->hide_read_msgs = item->hide_read_msgs;
4428 pp->hide_del_msgs = item->hide_del_msgs;
4429 pp->hide_read_threads = item->hide_read_threads;
4430 pp->sort_key = item->sort_key;
4431 pp->sort_type = item->sort_type;
4432 g_hash_table_insert(pptable, id, pp);
4435 if (node->children) {
4436 child = node->children;
4437 while (child) {
4438 cur = child;
4439 child = cur->next;
4440 folder_get_persist_prefs_recursive(cur, pptable);
4445 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
4447 g_free(key);
4448 g_free(val);
4449 return TRUE;
4452 void folder_item_apply_processing(FolderItem *item)
4454 GSList *processing_list;
4455 GSList *mlist, *cur;
4456 guint total = 0, curmsg = 0;
4457 gint last_apply_per_account;
4459 cm_return_if_fail(item != NULL);
4461 if (item->no_select)
4462 return;
4464 processing_list = item->prefs->processing;
4466 if (!processing_enabled(pre_global_processing) &&
4467 !processing_enabled(processing_list) &&
4468 !processing_enabled(post_global_processing))
4469 return;
4471 debug_print("processing %s\n", item->name);
4472 folder_item_update_freeze();
4474 inc_lock();
4476 mlist = folder_item_get_msg_list(item);
4477 total = g_slist_length(mlist);
4478 statusbar_print_all(_("Processing messages..."));
4480 last_apply_per_account = prefs_common.apply_per_account_filtering_rules;
4481 prefs_common.apply_per_account_filtering_rules = FILTERING_ACCOUNT_RULES_SKIP;
4483 folder_item_set_batch(item, TRUE);
4484 for (cur = mlist ; cur != NULL ; cur = cur->next) {
4485 MsgInfo * msginfo;
4487 msginfo = (MsgInfo *) cur->data;
4489 /* reset parameters that can be modified by processing */
4490 msginfo->hidden = 0;
4491 msginfo->score = 0;
4493 statusbar_progress_all(curmsg++,total, 10);
4495 /* apply pre global rules */
4496 filter_message_by_msginfo(pre_global_processing, msginfo, NULL,
4497 FILTERING_PRE_PROCESSING, NULL);
4499 /* apply rules of the folder */
4500 filter_message_by_msginfo(processing_list, msginfo, NULL,
4501 FILTERING_FOLDER_PROCESSING, item->name);
4503 /* apply post global rules */
4504 filter_message_by_msginfo(post_global_processing, msginfo, NULL,
4505 FILTERING_POST_PROCESSING, NULL);
4506 if (curmsg % 1000 == 0)
4507 GTK_EVENTS_FLUSH();
4509 folder_item_set_batch(item, FALSE);
4511 prefs_common.apply_per_account_filtering_rules = last_apply_per_account;
4513 if (pre_global_processing || processing_list
4514 || post_global_processing)
4515 filtering_move_and_copy_msgs(mlist);
4516 for (cur = mlist ; cur != NULL ; cur = cur->next) {
4517 procmsg_msginfo_free((MsgInfo **)&(cur->data));
4519 g_slist_free(mlist);
4521 statusbar_progress_all(0,0,0);
4522 statusbar_pop_all();
4524 inc_unlock();
4526 folder_item_update_thaw();
4530 * functions for handling FolderItem content changes
4532 static gint folder_item_update_freeze_cnt = 0;
4534 static void folder_item_update_with_msg(FolderItem *item, FolderItemUpdateFlags update_flags, MsgInfo *msg)
4536 if (folder_item_update_freeze_cnt == 0 /* || (msg != NULL && item->opened) */) {
4537 FolderItemUpdateData source;
4539 source.item = item;
4540 source.update_flags = update_flags;
4541 source.msg = msg;
4542 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
4543 } else {
4544 item->update_flags |= update_flags & ~(F_ITEM_UPDATE_ADDMSG | F_ITEM_UPDATE_REMOVEMSG);
4549 * Notify the folder system about changes to a folder. If the
4550 * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
4551 * be invoked, otherwise the changes will be remebered until
4552 * the folder system is thawed.
4554 * \param item The FolderItem that was changed
4555 * \param update_flags Type of changed that was made
4557 void folder_item_update(FolderItem *item, FolderItemUpdateFlags update_flags)
4559 folder_item_update_with_msg(item, update_flags, NULL);
4562 void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
4564 GNode *node = item->folder->node;
4566 node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
4567 node = node->children;
4569 folder_item_update(item, update_flags);
4570 while (node != NULL) {
4571 if (node && node->data) {
4572 FolderItem *next_item = (FolderItem*) node->data;
4574 folder_item_update(next_item, update_flags);
4576 node = node->next;
4580 void folder_item_update_freeze(void)
4582 folder_item_update_freeze_cnt++;
4585 static void folder_item_update_func(FolderItem *item, gpointer data)
4587 FolderItemUpdateData source;
4589 if (item->update_flags) {
4590 source.item = item;
4591 source.update_flags = item->update_flags;
4592 source.msg = NULL;
4593 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
4594 item->update_flags = 0;
4598 void folder_item_update_thaw(void)
4600 if (folder_item_update_freeze_cnt > 0)
4601 folder_item_update_freeze_cnt--;
4602 if (folder_item_update_freeze_cnt == 0) {
4603 /* Update all folders */
4604 folder_func_to_all_folders(folder_item_update_func, NULL);
4608 void folder_item_synchronise(FolderItem *item)
4610 if (!item)
4611 return;
4612 if (item->prefs->offlinesync && item->folder->klass->synchronise) {
4613 statusbar_print_all(_("Synchronising %s for offline use...\n"), item->path ? item->path : "(null)");
4614 item->folder->klass->synchronise(item,
4615 item->prefs->offlinesync_days);
4616 if (item->prefs->offlinesync_days > 0 &&
4617 item->prefs->remove_old_bodies)
4618 folder_item_clean_local_files(item, item->prefs->offlinesync_days);
4619 statusbar_pop_all();
4623 static void folder_item_synchronise_func(FolderItem *item, gpointer data)
4625 Folder *folder = (Folder *)data;
4626 if (folder == NULL || item->folder == folder) {
4627 folder_item_synchronise(item);
4631 void folder_synchronise(Folder *folder)
4633 folder_func_to_all_folders(folder_item_synchronise_func, folder);
4636 typedef struct _WantSyncData {
4637 Folder *folder;
4638 gboolean want_sync;
4639 } WantSyncData;
4641 static void folder_item_want_synchronise_func(FolderItem *item, gpointer data)
4643 WantSyncData *want_sync_data = (WantSyncData *)data;
4645 if (want_sync_data->folder == NULL || item->folder == want_sync_data->folder) {
4646 if (item->prefs->offlinesync && item->folder->klass->synchronise)
4647 want_sync_data->want_sync |= TRUE;
4651 gboolean folder_want_synchronise(Folder *folder)
4653 WantSyncData *want_sync_data = g_new0(WantSyncData, 1);
4654 gboolean result;
4655 want_sync_data->folder = folder;
4656 want_sync_data->want_sync = FALSE;
4658 folder_func_to_all_folders(folder_item_want_synchronise_func, want_sync_data);
4659 result = want_sync_data->want_sync;
4660 g_free(want_sync_data);
4661 if (result > 0)
4662 debug_print("Folder %s wants sync\n", folder->name);
4663 return result;
4666 void folder_item_set_batch (FolderItem *item, gboolean batch)
4668 if (!item || !item->folder)
4669 return;
4670 if (item->folder->klass->set_batch) {
4671 item->folder->klass->set_batch(item->folder, item, batch);
4675 gboolean folder_has_parent_of_type(FolderItem *item,
4676 SpecialFolderItemType type)
4678 FolderItem *cur = item;
4680 if (!item)
4681 return FALSE;
4682 /* if we already know it, make it short */
4683 if (item->parent_stype != -1) {
4684 return (item->parent_stype == type);
4687 /* if we don't, find the type from the first possible parent,
4688 * and set our parent type to be faster next time */
4689 while (cur) {
4690 if (cur->stype == type || cur->parent_stype == type) {
4691 item->parent_stype = type;
4692 return TRUE;
4694 cur = folder_item_parent(cur);
4697 /* if we didn't match what was asked, we didn't return. If our
4698 * parent type is unknown, we may as well find it now to be faster
4699 * later. */
4700 if (item->parent_stype == -1) {
4701 cur = item;
4702 while (cur) {
4703 /* here's an exception: Inbox subfolders are normal. */
4704 if (item->parent_stype == -1 && cur->stype == F_INBOX
4705 && item != cur) {
4706 item->parent_stype = F_NORMAL;
4707 break;
4709 /* ah, we know this parent's parent's type, we may as
4710 * well copy it instead of going up the full way */
4711 if (cur->parent_stype != -1) {
4712 item->parent_stype = cur->parent_stype;
4713 break;
4715 /* we found a parent that has a special type. That's
4716 * our parent type. */
4717 if (cur->stype != F_NORMAL) {
4718 cur->parent_stype = cur->stype;
4719 item->parent_stype = cur->stype;
4720 break;
4722 /* if we didn't find anything, go up once more */
4723 cur = folder_item_parent(cur);
4725 /* as we still didn't find anything, our parents must all be
4726 * normal. */
4727 if (item->parent_stype == -1) {
4728 item->parent_stype = F_NORMAL;
4731 return FALSE;
4734 gboolean folder_is_child_of(FolderItem *item, FolderItem *parent)
4736 if (item == NULL || parent == NULL)
4737 return FALSE;
4739 while (item != NULL) {
4740 if (parent == item)
4741 return TRUE;
4743 item = folder_item_parent(item);
4746 return FALSE;
4750 gboolean folder_subscribe (const gchar *uri)
4752 GList *cur;
4753 for (cur = folder_get_list(); cur != NULL; cur = g_list_next(cur)) {
4754 Folder *folder = (Folder *) cur->data;
4756 if (folder->klass->subscribe
4757 && folder->klass->subscribe(folder, uri)) {
4758 return TRUE;
4761 return FALSE;
4765 gboolean folder_get_sort_type (Folder *folder,
4766 FolderSortKey *sort_key,
4767 FolderSortType *sort_type)
4769 if (!folder || !sort_key || !sort_type)
4770 return FALSE;
4771 if (folder->klass->get_sort_type == NULL)
4772 return FALSE;
4773 folder->klass->get_sort_type(folder, sort_key, sort_type);
4774 return TRUE;
4777 gint folder_item_search_msgs (Folder *folder,
4778 FolderItem *container,
4779 MsgNumberList **msgs,
4780 gboolean *on_server,
4781 MatcherList *predicate,
4782 SearchProgressNotify progress_cb,
4783 gpointer progress_data)
4785 gint result = -1;
4787 folder_item_update_freeze();
4789 if (folder->klass->search_msgs)
4790 result = folder->klass->search_msgs(folder, container,
4791 msgs, on_server, predicate, progress_cb, progress_data);
4792 if (result < 0)
4793 result = folder_item_search_msgs_local(folder, container,
4794 msgs, on_server, predicate, progress_cb, progress_data);
4796 folder_item_update_thaw();
4798 return result;
4801 MsgNumberList *folder_item_get_number_list(FolderItem *item)
4803 GSList *nums = NULL;
4804 GSList *msglist = folder_item_get_msg_list(item);
4806 nums = procmsg_get_number_list_for_msgs(msglist);
4807 procmsg_msg_list_free(msglist);
4809 return nums;
4812 gint folder_item_search_msgs_local (Folder *folder,
4813 FolderItem *container,
4814 MsgNumberList **msgs,
4815 gboolean *on_server,
4816 MatcherList *predicate,
4817 SearchProgressNotify progress_cb,
4818 gpointer progress_data)
4820 GSList *result = NULL;
4821 GSList *cur = NULL;
4822 gint matched_count = 0;
4823 guint processed_count = 0;
4824 gint msgcount;
4825 GSList *nums = NULL;
4827 if (*msgs == NULL) {
4828 nums = folder_item_get_number_list(container);
4829 } else {
4830 nums = *msgs;
4833 msgcount = g_slist_length(nums);
4835 if (msgcount < 0)
4836 return -1;
4838 for (cur = nums; cur != NULL; cur = cur->next) {
4839 guint msgnum = GPOINTER_TO_UINT(cur->data);
4840 MsgInfo *msg = folder_item_get_msginfo(container, msgnum);
4842 if (msg == NULL) {
4843 g_slist_free(result);
4844 return -1;
4847 if (matcherlist_match(predicate, msg)) {
4848 result = g_slist_prepend(result, GUINT_TO_POINTER(msg->msgnum));
4849 matched_count++;
4851 processed_count++;
4853 procmsg_msginfo_free(&msg);
4855 if (progress_cb != NULL
4856 && !progress_cb(progress_data, FALSE, processed_count,
4857 matched_count, msgcount))
4858 break;
4861 g_slist_free(nums);
4862 *msgs = g_slist_reverse(result);
4864 return matched_count;
4867 /* Tests if a local (on disk) folder name is acceptable. */
4868 gboolean folder_local_name_ok(const gchar *name)
4870 #ifdef G_OS_WIN32
4871 if (name[0] == '.' || name[strlen(name) - 1] == '.') {
4872 alertpanel_error(_("A folder name cannot begin or end with a dot."));
4873 return FALSE;
4875 if (name[strlen(name) - 1] == ' ') {
4876 alertpanel_error(_("A folder name can not end with a space."));
4877 return FALSE;
4879 #endif
4881 return TRUE;