add support for Ayatana indicator to Notification plugin
[claws.git] / src / folder.c
blobd22add68fa3e74fe11c16af95c36b0f5389bea8b
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2024 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;
1191 new = unread = total = 0;
1193 str = g_string_new(NULL);
1195 if (folders) {
1196 for (i = 0; i < folders->len; i++) {
1197 FolderItem *item;
1199 item = g_ptr_array_index(folders, i);
1200 new += item->new_msgs;
1201 unread += item->unread_msgs;
1202 total += item->total_msgs;
1204 if (full) {
1205 gchar *id;
1207 id = folder_item_get_identifier(item);
1208 g_string_append_printf(str, "%5d %5d %5d %s\n",
1209 item->new_msgs, item->unread_msgs,
1210 item->total_msgs, id);
1211 g_free(id);
1214 } else {
1215 folder_get_status_full_all(full ? str : NULL,
1216 &new, &unread, &total);
1219 if (full)
1220 g_string_append_printf(str, "%5d %5d %5d\n", new, unread, total);
1221 else
1222 g_string_append_printf(str, "%d %d %d\n", new, unread, total);
1224 return g_string_free(str, FALSE);
1227 void folder_count_total_msgs(guint *new_msgs, guint *unread_msgs,
1228 guint *unreadmarked_msgs, guint *marked_msgs,
1229 guint *total_msgs, guint *replied_msgs,
1230 guint *forwarded_msgs, guint *locked_msgs,
1231 guint *ignored_msgs, guint *watched_msgs)
1233 struct TotalMsgCount count;
1235 count.new_msgs = count.unread_msgs = count.unreadmarked_msgs = 0;
1236 count.total_msgs = count.replied_msgs = count.forwarded_msgs = 0;
1237 count.locked_msgs = count.ignored_msgs = count.watched_msgs = 0;
1238 count.marked_msgs = 0;
1240 debug_print("Counting total number of messages...\n");
1242 folder_func_to_all_folders(folder_count_total_msgs_func, &count);
1244 *new_msgs = count.new_msgs;
1245 *unread_msgs = count.unread_msgs;
1246 *unreadmarked_msgs = count.unreadmarked_msgs;
1247 *marked_msgs = count.marked_msgs;
1248 *total_msgs = count.total_msgs;
1249 *replied_msgs = count.replied_msgs;
1250 *forwarded_msgs = count.forwarded_msgs;
1251 *locked_msgs = count.locked_msgs;
1252 *ignored_msgs = count.ignored_msgs;
1253 *watched_msgs = count.watched_msgs;
1256 Folder *folder_find_from_path(const gchar *path)
1258 GList *list;
1259 Folder *folder;
1261 for (list = folder_list; list != NULL; list = list->next) {
1262 folder = list->data;
1263 if ((FOLDER_TYPE(folder) == F_MH ||
1264 FOLDER_TYPE(folder) == F_MBOX) &&
1265 !path_cmp(LOCAL_FOLDER(folder)->rootpath, path))
1266 return folder;
1269 return NULL;
1272 Folder *folder_find_from_name(const gchar *name, FolderClass *klass)
1274 GList *list;
1275 Folder *folder;
1277 for (list = folder_list; list != NULL; list = list->next) {
1278 folder = list->data;
1279 if (folder->klass == klass &&
1280 g_strcmp0(name, folder->name) == 0)
1281 return folder;
1284 return NULL;
1287 static gboolean folder_item_find_func(GNode *node, gpointer data)
1289 FolderItem *item = node->data;
1290 gpointer *d = data;
1291 const gchar *path = d[0];
1293 if (path_cmp(path, item->path) != 0)
1294 return FALSE;
1296 d[1] = item;
1298 return TRUE;
1301 FolderItem *folder_find_item_from_path(const gchar *path)
1303 Folder *folder;
1304 gpointer d[2];
1305 GList *list = folder_get_list();
1307 folder = list ? list->data:NULL;
1309 cm_return_val_if_fail(folder != NULL, NULL);
1311 d[0] = (gpointer)path;
1312 d[1] = NULL;
1313 while (d[1] == NULL && list) {
1314 folder = FOLDER(list->data);
1315 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1316 folder_item_find_func, d);
1317 list = list->next;
1319 return d[1];
1322 static gboolean folder_item_find_func_real_path(GNode *node, gpointer data)
1324 FolderItem *item = node->data;
1325 gpointer *d = data;
1326 const gchar *path = d[0];
1327 gchar *tmp = folder_item_get_path(item);
1328 if (path_cmp(path, tmp) != 0) {
1329 g_free(tmp);
1330 return FALSE;
1332 g_free(tmp);
1333 d[1] = item;
1335 return TRUE;
1338 FolderItem *folder_find_item_from_real_path(const gchar *path)
1340 Folder *folder;
1341 gpointer d[2];
1342 GList *list = folder_get_list();
1344 folder = list ? list->data:NULL;
1346 cm_return_val_if_fail(folder != NULL, NULL);
1348 d[0] = (gpointer)path;
1349 d[1] = NULL;
1350 while (d[1] == NULL && list) {
1351 folder = FOLDER(list->data);
1352 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1353 folder_item_find_func_real_path, d);
1354 list = list->next;
1356 return d[1];
1359 FolderItem *folder_find_child_item_by_name(FolderItem *item, const gchar *name)
1361 GNode *node;
1362 FolderItem *child;
1364 for (node = item->node->children; node != NULL; node = node->next) {
1365 child = FOLDER_ITEM(node->data);
1366 if (g_strcmp0(child->name, name) == 0) {
1367 return child;
1371 return NULL;
1374 FolderClass *folder_get_class_from_string(const gchar *str)
1376 GSList *classlist;
1378 classlist = folder_get_class_list();
1379 for (; classlist != NULL; classlist = g_slist_next(classlist)) {
1380 FolderClass *class = (FolderClass *) classlist->data;
1381 if (g_ascii_strcasecmp(class->idstr, str) == 0)
1382 return class;
1385 return NULL;
1388 gchar *folder_get_identifier(Folder *folder)
1390 gchar *type_str;
1392 cm_return_val_if_fail(folder != NULL, NULL);
1394 type_str = folder->klass->idstr;
1395 return g_strconcat("#", type_str, "/", folder->name, NULL);
1398 gchar *folder_item_get_identifier(FolderItem *item)
1400 gchar *id = NULL;
1401 gchar *folder_id = NULL;
1403 cm_return_val_if_fail(item != NULL, NULL);
1405 if (item->path == NULL)
1406 return NULL;
1408 folder_id = folder_get_identifier(item->folder);
1409 id = g_strconcat(folder_id, "/", item->path, NULL);
1410 g_free(folder_id);
1412 return id;
1415 Folder *folder_find_from_identifier(const gchar *identifier)
1417 gchar *str;
1418 gchar *p;
1419 gchar *name;
1420 FolderClass *class;
1422 cm_return_val_if_fail(identifier != NULL, NULL);
1424 if (*identifier != '#')
1425 return NULL;
1427 Xstrdup_a(str, identifier, return NULL);
1429 p = strchr(str, '/');
1430 if (!p)
1431 return NULL;
1432 *p = '\0';
1433 p++;
1434 class = folder_get_class_from_string(&str[1]);
1435 if (class == NULL)
1436 return NULL;
1438 name = p;
1439 p = strchr(p, '/');
1440 if (p)
1441 return NULL;
1443 return folder_find_from_name(name, class);
1446 FolderItem *folder_find_item_from_identifier(const gchar *identifier)
1448 Folder *folder;
1449 gpointer d[2];
1450 gchar *str;
1451 gchar *p;
1452 gchar *name;
1453 gchar *path;
1454 FolderClass *class;
1456 cm_return_val_if_fail(identifier != NULL, NULL);
1458 if (*identifier != '#')
1459 return folder_find_item_from_path(identifier);
1461 Xstrdup_a(str, identifier, return NULL);
1463 p = strchr(str, '/');
1464 if (!p)
1465 return folder_find_item_from_path(identifier);
1466 *p = '\0';
1467 p++;
1468 class = folder_get_class_from_string(&str[1]);
1469 if (class == NULL)
1470 return folder_find_item_from_path(identifier);
1472 name = p;
1473 p = strchr(p, '/');
1474 if (!p)
1475 return folder_find_item_from_path(identifier);
1476 *p = '\0';
1477 p++;
1479 folder = folder_find_from_name(name, class);
1480 if (!folder)
1481 return folder_find_item_from_path(identifier);
1483 path = p;
1485 d[0] = (gpointer)path;
1486 d[1] = NULL;
1487 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1488 folder_item_find_func, d);
1489 return d[1];
1492 /** Returns the FolderItem from a given identifier
1494 * The FolderItem is created if it doesn't already exist.
1495 * If creation failed, the function returns NULL.
1497 * Identifiers are of the form #type/Mailbox/FolderA/FolderB/FolderC
1499 FolderItem *folder_get_item_from_identifier(const gchar *identifier)
1501 FolderItem *item, *last_parent;
1502 Folder *folder;
1503 gchar *p1, *p2, *str;
1504 size_t len;
1505 FolderClass *class;
1506 gboolean created_something = FALSE;
1508 item = folder_find_item_from_identifier(identifier);
1509 if(item)
1510 return item;
1512 /* trivial sanity check: need at least # and two slashes */
1513 len = strlen(identifier);
1514 if(len < 3)
1515 return NULL;
1517 /* make sure identifier ends with a slash */
1518 if(identifier[len-1] == G_DIR_SEPARATOR) {
1519 Xstrdup_a(str, identifier, return NULL);
1521 else {
1522 Xstrndup_a(str, identifier, len+1, return NULL);
1523 str[len] = G_DIR_SEPARATOR;
1526 /* find folder class */
1527 p1 = strchr(str, G_DIR_SEPARATOR);
1528 if(!p1)
1529 return NULL;
1530 *p1 = '\0';
1531 class = folder_get_class_from_string(&str[1]);
1532 if(!class)
1533 return NULL;
1534 *p1 = G_DIR_SEPARATOR;
1535 ++p1;
1537 /* find folder from class and name */
1538 p2 = strchr(p1, G_DIR_SEPARATOR);
1539 if(!p2)
1540 return NULL;
1541 *p2 = '\0';
1542 folder = folder_find_from_name(p1, class);
1543 if(!folder)
1544 return NULL;
1545 *p2 = G_DIR_SEPARATOR;
1546 ++p2;
1547 p1 = p2;
1549 /* Now, move forward and make sure all sections in the path exist */
1550 last_parent = folder->node->data;
1551 while((p1 = strchr(p1, G_DIR_SEPARATOR)) != NULL) {
1552 *p1 = '\0';
1553 item = folder_find_item_from_identifier(str);
1554 if(!item) {
1555 item = folder_create_folder(last_parent, p2);
1556 if(!item)
1557 return NULL;
1558 debug_print("Created folder '%s'\n", str);
1559 created_something = TRUE;
1560 if(prefs_common.inherit_folder_props && (last_parent != item->folder->node->data)) {
1561 folder_item_prefs_copy_prefs(last_parent, item);
1564 last_parent = item;
1565 *p1 = G_DIR_SEPARATOR;
1566 ++p1;
1567 p2 = p1;
1570 if(created_something)
1571 folder_write_list();
1573 return item;
1578 * Get a displayable name for a FolderItem
1580 * \param item FolderItem for that a name should be created
1581 * \return Displayable name for item, returned string has to
1582 * be freed
1584 gchar *folder_item_get_name(FolderItem *item)
1586 gchar *name = NULL;
1588 cm_return_val_if_fail(item != NULL, g_strdup(""));
1590 switch (item->stype) {
1591 case F_INBOX:
1592 name = g_strdup(!g_strcmp0(item->name, INBOX_DIR) ? _("Inbox") :
1593 item->name);
1594 break;
1595 case F_OUTBOX:
1596 name = g_strdup(!g_strcmp0(item->name, OUTBOX_DIR) ? _("Sent") :
1597 item->name);
1598 break;
1599 case F_QUEUE:
1600 name = g_strdup(!g_strcmp0(item->name, QUEUE_DIR) ? _("Queue") :
1601 item->name);
1602 break;
1603 case F_TRASH:
1604 name = g_strdup(!g_strcmp0(item->name, TRASH_DIR) ? _("Trash") :
1605 item->name);
1606 break;
1607 case F_DRAFT:
1608 name = g_strdup(!g_strcmp0(item->name, DRAFT_DIR) ? _("Drafts") :
1609 item->name);
1610 break;
1611 default:
1612 break;
1615 if (name == NULL) {
1617 * should probably be done by a virtual function,
1618 * the folder knows the ui string and how to abbrev
1620 if (folder_item_parent(item) == NULL) {
1621 name = g_strconcat(item->name, " (", item->folder->klass->uistr, ")", NULL);
1622 } else {
1623 if (FOLDER_CLASS(item->folder) == news_get_class() &&
1624 item->path && !g_strcmp0(item->name, item->path))
1625 name = get_abbrev_newsgroup_name
1626 (item->path,
1627 prefs_common.ng_abbrev_len);
1628 else
1629 name = g_strdup(item->name);
1633 if (name == NULL)
1634 name = g_strdup("");
1636 return name;
1639 gboolean folder_have_mailbox (void)
1641 GList *cur;
1642 for (cur = folder_list; cur != NULL; cur = g_list_next(cur)) {
1643 Folder *folder = FOLDER(cur->data);
1644 if (folder->inbox && folder->outbox)
1645 return TRUE;
1647 return FALSE;
1650 FolderItem *folder_get_default_inbox(void)
1652 GList *flist;
1654 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1655 Folder * folder = FOLDER(flist->data);
1657 if (folder == NULL)
1658 continue;
1659 if (folder->inbox == NULL)
1660 continue;
1661 if (folder->klass->type == F_UNKNOWN)
1662 continue;
1664 return folder->inbox;
1667 return NULL;
1670 FolderItem *folder_get_default_inbox_for_class(FolderType type)
1672 GList *flist;
1674 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1675 Folder * folder = FOLDER(flist->data);
1677 if (folder == NULL)
1678 continue;
1679 if (folder->inbox == NULL)
1680 continue;
1681 if (folder->klass->type != type)
1682 continue;
1684 return folder->inbox;
1687 return NULL;
1690 FolderItem *folder_get_default_outbox(void)
1692 GList *flist;
1694 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1695 Folder * folder = FOLDER(flist->data);
1697 if (folder == NULL)
1698 continue;
1699 if (folder->outbox == NULL)
1700 continue;
1701 if (folder->klass->type == F_UNKNOWN)
1702 continue;
1704 return folder->outbox;
1707 return NULL;
1710 FolderItem *folder_get_default_outbox_for_class(FolderType type)
1712 GList *flist;
1714 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1715 Folder * folder = FOLDER(flist->data);
1717 if (folder == NULL)
1718 continue;
1719 if (folder->outbox == NULL)
1720 continue;
1721 if (folder->klass->type != type)
1722 continue;
1724 return folder->outbox;
1727 return NULL;
1730 FolderItem *folder_get_default_draft(void)
1732 GList *flist;
1734 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1735 Folder * folder = FOLDER(flist->data);
1737 if (folder == NULL)
1738 continue;
1739 if (folder->draft == NULL)
1740 continue;
1741 if (folder->klass->type == F_UNKNOWN)
1742 continue;
1744 return folder->draft;
1747 return NULL;
1750 FolderItem *folder_get_default_draft_for_class(FolderType type)
1752 GList *flist;
1754 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1755 Folder * folder = FOLDER(flist->data);
1757 if (folder == NULL)
1758 continue;
1759 if (folder->draft == NULL)
1760 continue;
1761 if (folder->klass->type != type)
1762 continue;
1764 return folder->draft;
1767 return NULL;
1770 FolderItem *folder_get_default_queue(void)
1772 GList *flist;
1774 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1775 Folder * folder = FOLDER(flist->data);
1777 if (folder == NULL)
1778 continue;
1779 if (folder->queue == NULL)
1780 continue;
1781 if (folder->klass->type == F_UNKNOWN)
1782 continue;
1784 return folder->queue;
1787 return NULL;
1790 FolderItem *folder_get_default_queue_for_class(FolderType type)
1792 GList *flist;
1794 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1795 Folder * folder = FOLDER(flist->data);
1797 if (folder == NULL)
1798 continue;
1799 if (folder->queue == NULL)
1800 continue;
1801 if (folder->klass->type != type)
1802 continue;
1804 return folder->queue;
1807 return NULL;
1810 FolderItem *folder_get_default_trash(void)
1812 GList *flist;
1814 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1815 Folder * folder = FOLDER(flist->data);
1817 if (folder == NULL)
1818 continue;
1819 if (folder->trash == NULL)
1820 continue;
1821 if (folder->klass->type == F_UNKNOWN)
1822 continue;
1824 return folder->trash;
1827 return NULL;
1830 FolderItem *folder_get_default_trash_for_class(FolderType type)
1832 GList *flist;
1834 for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1835 Folder * folder = FOLDER(flist->data);
1837 if (folder == NULL)
1838 continue;
1839 if (folder->trash == NULL)
1840 continue;
1841 if (folder->klass->type != type)
1842 continue;
1844 return folder->trash;
1847 return NULL;
1850 #define CREATE_FOLDER_IF_NOT_EXIST(member, dir, type) \
1852 if (!folder->member) { \
1853 item = folder_item_new(folder, dir, dir); \
1854 item->stype = type; \
1855 folder_item_append(rootitem, item); \
1856 folder->member = item; \
1860 void folder_set_missing_folders(void)
1862 Folder *folder;
1863 FolderItem *rootitem;
1864 FolderItem *item;
1865 GList *list;
1867 for (list = folder_list; list != NULL; list = list->next) {
1868 folder = list->data;
1869 if (FOLDER_TYPE(folder) != F_MH) continue;
1870 rootitem = FOLDER_ITEM(folder->node->data);
1871 cm_return_if_fail(rootitem != NULL);
1873 if (folder->inbox && folder->outbox && folder->draft &&
1874 folder->queue && folder->trash)
1875 continue;
1877 if (folder->klass->create_tree(folder) < 0) {
1878 g_warning("%s: can't create the folder tree",
1879 LOCAL_FOLDER(folder)->rootpath);
1880 continue;
1883 CREATE_FOLDER_IF_NOT_EXIST(inbox, INBOX_DIR, F_INBOX);
1884 CREATE_FOLDER_IF_NOT_EXIST(outbox, OUTBOX_DIR, F_OUTBOX);
1885 CREATE_FOLDER_IF_NOT_EXIST(draft, DRAFT_DIR, F_DRAFT);
1886 CREATE_FOLDER_IF_NOT_EXIST(queue, QUEUE_DIR, F_QUEUE);
1887 CREATE_FOLDER_IF_NOT_EXIST(trash, TRASH_DIR, F_TRASH);
1891 static gboolean folder_unref_account_func(GNode *node, gpointer data)
1893 FolderItem *item = node->data;
1894 PrefsAccount *account = data;
1896 if (item->account == account)
1897 item->account = NULL;
1899 return FALSE;
1902 void folder_unref_account_all(PrefsAccount *account)
1904 Folder *folder;
1905 GList *list;
1907 if (!account) return;
1909 for (list = folder_list; list != NULL; list = list->next) {
1910 folder = list->data;
1911 if (folder->account == account)
1912 folder->account = NULL;
1913 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1914 folder_unref_account_func, account);
1918 #undef CREATE_FOLDER_IF_NOT_EXIST
1920 gchar *folder_item_get_path(FolderItem *item)
1922 Folder *folder;
1924 cm_return_val_if_fail(item != NULL, NULL);
1925 folder = item->folder;
1926 cm_return_val_if_fail(folder != NULL, NULL);
1928 return folder->klass->item_get_path(folder, item);
1931 static gint folder_sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1933 MsgInfo *msginfo_a = (MsgInfo *) a;
1934 MsgInfo *msginfo_b = (MsgInfo *) b;
1936 return (msginfo_a->msgnum - msginfo_b->msgnum);
1939 static gint folder_sort_folder_list(gconstpointer a, gconstpointer b)
1941 guint gint_a = GPOINTER_TO_INT(a);
1942 guint gint_b = GPOINTER_TO_INT(b);
1944 return (gint_a - gint_b);
1947 static gint syncronize_flags(FolderItem *item, MsgInfoList *msglist)
1949 GHashTable *relation;
1950 gint ret = 0;
1951 GSList *cur;
1953 if(msglist == NULL)
1954 return 0;
1955 if(item->folder->klass->get_flags == NULL)
1956 return 0;
1957 if (item->no_select)
1958 return 0;
1960 relation = g_hash_table_new(g_direct_hash, g_direct_equal);
1961 if ((ret = item->folder->klass->get_flags(
1962 item->folder, item, msglist, relation)) == 0) {
1963 gpointer data, old_key;
1964 MsgInfo *msginfo;
1965 MsgPermFlags permflags = 0;
1967 folder_item_update_freeze();
1968 folder_item_set_batch(item, TRUE);
1969 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1970 msginfo = (MsgInfo *) cur->data;
1972 if (g_hash_table_lookup_extended(relation, msginfo, &old_key, &data)) {
1973 permflags = GPOINTER_TO_INT(data);
1975 if (msginfo->flags.perm_flags != permflags) {
1976 procmsg_msginfo_change_flags(msginfo,
1977 permflags & ~msginfo->flags.perm_flags, 0,
1978 ~permflags & msginfo->flags.perm_flags, 0);
1982 folder_item_set_batch(item, FALSE);
1983 folder_item_update_thaw();
1985 g_hash_table_destroy(relation);
1987 return ret;
1990 static gint folder_item_syncronize_flags(FolderItem *item)
1992 MsgInfoList *msglist = NULL;
1993 GSList *cur;
1994 gint ret = 0;
1996 cm_return_val_if_fail(item != NULL, -1);
1997 cm_return_val_if_fail(item->folder != NULL, -1);
1998 cm_return_val_if_fail(item->folder->klass != NULL, -1);
1999 if (item->no_select)
2000 return -1;
2002 item->scanning = ITEM_SCANNING_WITH_FLAGS;
2004 if (item->cache == NULL)
2005 folder_item_read_cache(item);
2007 msglist = msgcache_get_msg_list(item->cache);
2009 ret = syncronize_flags(item, msglist);
2011 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
2012 procmsg_msginfo_free((MsgInfo **)&(cur->data));
2015 g_slist_free(msglist);
2017 item->scanning = ITEM_NOT_SCANNING;
2019 return ret;
2022 static void folder_item_process_open (FolderItem *item,
2023 void (*before_proc_func)(gpointer data),
2024 void (*after_proc_func)(gpointer data),
2025 gpointer data)
2027 gchar *buf;
2028 if (item == NULL)
2029 return;
2030 if((item->folder->klass->scan_required != NULL) &&
2031 (item->folder->klass->scan_required(item->folder, item))) {
2032 folder_item_scan_full(item, TRUE);
2033 } else {
2034 folder_item_syncronize_flags(item);
2037 /* Processing */
2038 if (item->prefs->enable_processing_when_opening) {
2039 buf = g_strdup_printf(_("Processing (%s)...\n"),
2040 item->path ? item->path : item->name);
2041 g_free(buf);
2043 if (before_proc_func)
2044 before_proc_func(data);
2046 folder_item_apply_processing(item);
2048 if (after_proc_func)
2049 after_proc_func(data);
2051 item->processing_pending = FALSE;
2052 return;
2055 gint folder_item_open(FolderItem *item)
2057 START_TIMING("");
2058 if (item->no_select)
2059 return -1;
2061 if (item->scanning != ITEM_NOT_SCANNING) {
2062 debug_print("%s is scanning... \n", item->path ? item->path : item->name);
2063 return -2;
2066 item->processing_pending = TRUE;
2067 folder_item_process_open (item, NULL, NULL, NULL);
2069 item->opened = TRUE;
2070 END_TIMING();
2071 return 0;
2074 gint folder_item_close(FolderItem *item)
2076 GSList *mlist, *cur;
2077 Folder *folder;
2079 cm_return_val_if_fail(item != NULL, -1);
2081 if (item->no_select)
2082 return -1;
2084 if (item->new_msgs) {
2085 folder_item_update_freeze();
2086 mlist = folder_item_get_msg_list(item);
2087 for (cur = mlist ; cur != NULL ; cur = cur->next) {
2088 MsgInfo * msginfo;
2090 msginfo = (MsgInfo *) cur->data;
2091 if (MSG_IS_NEW(msginfo->flags))
2092 procmsg_msginfo_unset_flags(msginfo, MSG_NEW, 0);
2093 procmsg_msginfo_free(&msginfo);
2095 g_slist_free(mlist);
2096 folder_item_update_thaw();
2099 folder_item_write_cache(item);
2101 folder_item_update(item, F_ITEM_UPDATE_MSGCNT);
2103 item->opened = FALSE;
2104 folder = item->folder;
2106 if (folder->klass->close == NULL)
2107 return 0;
2109 return folder->klass->close(folder, item);
2112 static MsgInfoList *get_msginfos(FolderItem *item, MsgNumberList *numlist)
2114 MsgInfoList *msglist = NULL;
2115 Folder *folder = item->folder;
2116 if (item->no_select)
2117 return NULL;
2119 if (folder->klass->get_msginfos != NULL)
2120 msglist = folder->klass->get_msginfos(folder, item, numlist);
2121 else {
2122 MsgNumberList *elem;
2124 for (elem = numlist; elem != NULL; elem = g_slist_next(elem)) {
2125 MsgInfo *msginfo;
2126 guint num;
2128 num = GPOINTER_TO_INT(elem->data);
2129 msginfo = folder->klass->get_msginfo(folder, item, num);
2130 if (msginfo != NULL)
2131 msglist = g_slist_prepend(msglist, msginfo);
2135 return msglist;
2138 static MsgInfo *get_msginfo(FolderItem *item, guint num)
2140 MsgNumberList numlist;
2141 MsgInfoList *msglist;
2142 MsgInfo *msginfo = NULL;
2144 numlist.data = GINT_TO_POINTER(num);
2145 numlist.next = NULL;
2146 msglist = get_msginfos(item, &numlist);
2147 if (msglist != NULL)
2148 msginfo = procmsg_msginfo_new_ref(msglist->data);
2149 procmsg_msg_list_free(msglist);
2151 return msginfo;
2154 gint folder_item_scan_full(FolderItem *item, gboolean filtering)
2156 Folder *folder;
2157 GSList *folder_list = NULL, *cache_list = NULL;
2158 GSList *folder_list_cur, *cache_list_cur, *new_list = NULL;
2159 GSList *exists_list = NULL, *elem;
2160 GSList *newmsg_list = NULL;
2161 guint newcnt = 0, unreadcnt = 0, totalcnt = 0;
2162 guint markedcnt = 0, unreadmarkedcnt = 0;
2163 guint repliedcnt = 0, forwardedcnt = 0;
2164 guint lockedcnt = 0, ignoredcnt = 0, watchedcnt = 0;
2166 guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
2167 gboolean update_flags = 0, old_uids_valid = FALSE;
2168 GHashTable *subject_table = NULL;
2170 cm_return_val_if_fail(item != NULL, -1);
2171 if (item->path == NULL) return -1;
2173 folder = item->folder;
2175 cm_return_val_if_fail(folder != NULL, -1);
2176 cm_return_val_if_fail(folder->klass->get_num_list != NULL, -1);
2178 item->scanning = ITEM_SCANNING_WITH_FLAGS;
2180 debug_print("Scanning folder %s for cache changes.\n", item->path ? item->path : "(null)");
2182 /* Get list of messages for folder and cache */
2183 if (folder->klass->get_num_list(item->folder, item, &folder_list, &old_uids_valid) < 0) {
2184 debug_print("Error fetching list of message numbers\n");
2185 item->scanning = ITEM_NOT_SCANNING;
2186 return(-1);
2189 if(prefs_common.thread_by_subject) {
2190 subject_table = g_hash_table_new(g_str_hash, g_str_equal);
2193 if (old_uids_valid) {
2194 if (!item->cache)
2195 folder_item_read_cache(item);
2196 cache_list = msgcache_get_msg_list(item->cache);
2197 } else {
2198 if (item->cache)
2199 msgcache_destroy(item->cache);
2200 item->cache = msgcache_new();
2201 item->cache_dirty = TRUE;
2202 item->mark_dirty = TRUE;
2203 item->tags_dirty = TRUE;
2204 cache_list = NULL;
2207 /* Sort both lists */
2208 cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
2209 folder_list = g_slist_sort(folder_list, folder_sort_folder_list);
2211 cache_list_cur = cache_list;
2212 folder_list_cur = folder_list;
2214 if (cache_list_cur != NULL) {
2215 GSList *cache_list_last;
2217 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
2218 cache_list_last = g_slist_last(cache_list);
2219 cache_max_num = ((MsgInfo *)cache_list_last->data)->msgnum;
2220 } else {
2221 cache_cur_num = G_MAXUINT;
2222 cache_max_num = 0;
2225 if (folder_list_cur != NULL) {
2226 GSList *folder_list_last;
2228 folder_cur_num = GPOINTER_TO_UINT(folder_list_cur->data);
2229 folder_list_last = g_slist_last(folder_list);
2230 folder_max_num = GPOINTER_TO_UINT(folder_list_last->data);
2231 } else {
2232 folder_cur_num = G_MAXUINT;
2233 folder_max_num = 0;
2236 while ((cache_cur_num != G_MAXUINT) || (folder_cur_num != G_MAXUINT)) {
2238 * Message only exists in the folder
2239 * Remember message for fetching
2241 if (folder_cur_num < cache_cur_num) {
2242 gboolean add = FALSE;
2244 switch(FOLDER_TYPE(folder)) {
2245 case F_NEWS:
2246 if (folder_cur_num < cache_max_num)
2247 break;
2249 if (folder->account->max_articles == 0) {
2250 add = TRUE;
2253 if (folder_max_num <= folder->account->max_articles) {
2254 add = TRUE;
2255 } else if (folder_cur_num > (folder_max_num - folder->account->max_articles)) {
2256 add = TRUE;
2258 break;
2259 default:
2260 add = TRUE;
2261 break;
2264 if (add) {
2265 new_list = g_slist_prepend(new_list, GUINT_TO_POINTER(folder_cur_num));
2266 debug_print("Remembered message %u for fetching\n", folder_cur_num);
2269 /* Move to next folder number */
2270 if (folder_list_cur)
2271 folder_list_cur = folder_list_cur->next;
2273 if (folder_list_cur != NULL)
2274 folder_cur_num = GPOINTER_TO_UINT(folder_list_cur->data);
2275 else
2276 folder_cur_num = G_MAXUINT;
2278 continue;
2282 * Message only exists in the cache
2283 * Remove the message from the cache
2285 if (cache_cur_num < folder_cur_num) {
2286 msgcache_remove_msg(item->cache, cache_cur_num);
2287 debug_print("Removed message %u from cache.\n", cache_cur_num);
2289 /* Move to next cache number */
2290 if (cache_list_cur)
2291 cache_list_cur = cache_list_cur->next;
2293 if (cache_list_cur != NULL)
2294 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
2295 else
2296 cache_cur_num = G_MAXUINT;
2298 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
2300 continue;
2304 * Message number exists in folder and cache!
2305 * Check if the message has been modified
2307 if (cache_cur_num == folder_cur_num) {
2308 MsgInfo *msginfo;
2310 msginfo = msgcache_get_msg(item->cache, folder_cur_num);
2311 if (msginfo && folder->klass->is_msg_changed && folder->klass->is_msg_changed(folder, item, msginfo)) {
2312 msgcache_remove_msg(item->cache, msginfo->msgnum);
2313 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(msginfo->msgnum));
2314 procmsg_msginfo_free(&msginfo);
2316 debug_print("Remembering message %u to update...\n", folder_cur_num);
2317 } else if (msginfo) {
2318 exists_list = g_slist_prepend(exists_list, msginfo);
2320 if(prefs_common.thread_by_subject &&
2321 MSG_IS_IGNORE_THREAD(msginfo->flags) &&
2322 !subject_table_lookup(subject_table, msginfo->subject)) {
2323 subject_table_insert(subject_table, msginfo->subject, msginfo);
2327 /* Move to next folder and cache number */
2328 if (cache_list_cur)
2329 cache_list_cur = cache_list_cur->next;
2331 if (folder_list_cur)
2332 folder_list_cur = folder_list_cur->next;
2334 if (cache_list_cur != NULL)
2335 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
2336 else
2337 cache_cur_num = G_MAXUINT;
2339 if (folder_list_cur != NULL)
2340 folder_cur_num = GPOINTER_TO_UINT(folder_list_cur->data);
2341 else
2342 folder_cur_num = G_MAXUINT;
2344 continue;
2348 for(cache_list_cur = cache_list; cache_list_cur != NULL; cache_list_cur = g_slist_next(cache_list_cur))
2349 procmsg_msginfo_free((MsgInfo **)&(cache_list_cur->data));
2351 g_slist_free(cache_list);
2352 g_slist_free(folder_list);
2354 if (new_list != NULL) {
2355 GSList *tmp_list = NULL;
2356 newmsg_list = get_msginfos(item, new_list);
2357 g_slist_free(new_list);
2358 tmp_list = g_slist_concat(g_slist_copy(exists_list), g_slist_copy(newmsg_list));
2359 syncronize_flags(item, tmp_list);
2360 g_slist_free(tmp_list);
2361 } else {
2362 syncronize_flags(item, exists_list);
2365 folder_item_update_freeze();
2367 item->scanning = ITEM_SCANNING;
2369 if (newmsg_list != NULL) {
2370 GSList *elem, *to_filter = NULL;
2371 gboolean do_filter = (filtering == TRUE) &&
2372 (item->folder->account != NULL) &&
2373 (item->folder->account->filter_on_recv) &&
2374 ((item->stype == F_INBOX) ||
2375 ((item->stype == F_NORMAL) &&
2376 (FOLDER_TYPE(item->folder) == F_NEWS)));
2378 for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
2379 MsgInfo *msginfo = (MsgInfo *) elem->data;
2381 msgcache_add_msg(item->cache, msginfo);
2382 if (!do_filter) {
2383 exists_list = g_slist_prepend(exists_list, msginfo);
2385 if(prefs_common.thread_by_subject &&
2386 MSG_IS_IGNORE_THREAD(msginfo->flags) &&
2387 !subject_table_lookup(subject_table, msginfo->subject)) {
2388 subject_table_insert(subject_table, msginfo->subject, msginfo);
2393 if (do_filter) {
2394 GSList *unfiltered;
2396 folder_item_set_batch(item, TRUE);
2397 procmsg_msglist_filter(newmsg_list, item->folder->account,
2398 &to_filter, &unfiltered,
2399 TRUE);
2400 folder_item_set_batch(item, FALSE);
2402 filtering_move_and_copy_msgs(newmsg_list);
2403 if (to_filter != NULL) {
2404 for (elem = to_filter; elem; elem = g_slist_next(elem)) {
2405 MsgInfo *msginfo = (MsgInfo *)elem->data;
2406 procmsg_msginfo_free(&msginfo);
2408 g_slist_free(to_filter);
2410 if (unfiltered != NULL) {
2411 for (elem = unfiltered; elem; elem = g_slist_next(elem)) {
2412 MsgInfo *msginfo = (MsgInfo *)elem->data;
2413 exists_list = g_slist_prepend(exists_list, msginfo);
2415 if(prefs_common.thread_by_subject &&
2416 MSG_IS_IGNORE_THREAD(msginfo->flags) &&
2417 !subject_table_lookup(subject_table, msginfo->subject)) {
2418 subject_table_insert(subject_table, msginfo->subject, msginfo);
2421 g_slist_free(unfiltered);
2423 if (prefs_common.real_time_sync)
2424 folder_item_synchronise(item);
2425 } else {
2426 if (prefs_common.real_time_sync)
2427 folder_item_synchronise(item);
2430 g_slist_free(newmsg_list);
2432 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
2435 folder_item_set_batch(item, TRUE);
2436 for (elem = exists_list; elem != NULL; elem = g_slist_next(elem)) {
2437 MsgInfo *msginfo, *parent_msginfo;
2439 msginfo = elem->data;
2440 if (MSG_IS_IGNORE_THREAD(msginfo->flags) && (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2441 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2442 if (!MSG_IS_IGNORE_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD)) {
2443 procmsg_msginfo_change_flags(msginfo, MSG_IGNORE_THREAD, 0, MSG_NEW | MSG_UNREAD, 0);
2445 if (!MSG_IS_WATCH_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_WATCH_THREAD)) {
2446 procmsg_msginfo_set_flags(msginfo, MSG_WATCH_THREAD, 0);
2448 if(prefs_common.thread_by_subject && !msginfo->inreplyto &&
2449 !msginfo->references && !MSG_IS_IGNORE_THREAD(msginfo->flags) &&
2450 (parent_msginfo = subject_table_lookup(subject_table, msginfo->subject)))
2452 if(MSG_IS_IGNORE_THREAD(parent_msginfo->flags)) {
2453 procmsg_msginfo_change_flags(msginfo, MSG_IGNORE_THREAD, 0,
2454 MSG_NEW | MSG_UNREAD, 0);
2457 if ((folder_has_parent_of_type(item, F_OUTBOX) ||
2458 folder_has_parent_of_type(item, F_QUEUE) ||
2459 folder_has_parent_of_type(item, F_TRASH)) &&
2460 (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
2461 procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2462 if (MSG_IS_NEW(msginfo->flags))
2463 newcnt++;
2464 if (MSG_IS_UNREAD(msginfo->flags))
2465 unreadcnt++;
2466 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2467 unreadmarkedcnt++;
2468 if (MSG_IS_MARKED(msginfo->flags))
2469 markedcnt++;
2470 if (MSG_IS_REPLIED(msginfo->flags))
2471 repliedcnt++;
2472 if (MSG_IS_FORWARDED(msginfo->flags))
2473 forwardedcnt++;
2474 if (MSG_IS_LOCKED(msginfo->flags))
2475 lockedcnt++;
2476 if (MSG_IS_IGNORE_THREAD(msginfo->flags))
2477 ignoredcnt++;
2478 if (MSG_IS_WATCH_THREAD(msginfo->flags))
2479 watchedcnt++;
2481 totalcnt++;
2483 procmsg_msginfo_free(&msginfo);
2485 folder_item_set_batch(item, FALSE);
2486 g_slist_free(exists_list);
2488 if(prefs_common.thread_by_subject) {
2489 g_hash_table_destroy(subject_table);
2492 if (item->new_msgs != newcnt || item->unread_msgs != unreadcnt
2493 || item->total_msgs != totalcnt || item->marked_msgs != markedcnt
2494 || item->unreadmarked_msgs != unreadmarkedcnt
2495 || item->replied_msgs != repliedcnt || item->forwarded_msgs != forwardedcnt
2496 || item->locked_msgs != lockedcnt || item->ignored_msgs != ignoredcnt
2497 || item->watched_msgs != watchedcnt) {
2498 update_flags |= F_ITEM_UPDATE_CONTENT;
2501 item->new_msgs = newcnt;
2502 item->unread_msgs = unreadcnt;
2503 item->total_msgs = totalcnt;
2504 item->unreadmarked_msgs = unreadmarkedcnt;
2505 item->marked_msgs = markedcnt;
2506 item->replied_msgs = repliedcnt;
2507 item->forwarded_msgs = forwardedcnt;
2508 item->locked_msgs = lockedcnt;
2509 item->ignored_msgs = ignoredcnt;
2510 item->watched_msgs = watchedcnt;
2512 update_flags |= F_ITEM_UPDATE_MSGCNT;
2514 folder_item_update(item, update_flags);
2515 folder_item_update_thaw();
2517 item->scanning = ITEM_NOT_SCANNING;
2519 return 0;
2522 gint folder_item_scan(FolderItem *item)
2524 return folder_item_scan_full(item, TRUE);
2527 static void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
2529 gint *memusage = (gint *)data;
2531 if (item->cache == NULL)
2532 return;
2534 *memusage += msgcache_get_memory_usage(item->cache);
2537 static gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
2539 FolderItem *fa = (FolderItem *)a;
2540 FolderItem *fb = (FolderItem *)b;
2542 return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
2545 static void folder_find_expired_caches(FolderItem *item, gpointer data)
2547 GSList **folder_item_list = (GSList **)data;
2548 gint difftime, expiretime;
2550 if (item->cache == NULL)
2551 return;
2553 if (item->opened > 0)
2554 return;
2556 difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
2557 expiretime = prefs_common.cache_min_keep_time * 60;
2558 debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
2560 if (difftime > expiretime && !item->opened && !item->processing_pending) {
2561 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
2565 gboolean folder_item_free_cache(FolderItem *item, gboolean force)
2567 cm_return_val_if_fail(item != NULL, TRUE);
2569 if (item->cache == NULL)
2570 return TRUE;
2572 if (item->opened > 0 && !force)
2573 return FALSE;
2575 folder_item_write_cache(item);
2576 msgcache_destroy(item->cache);
2577 item->cache = NULL;
2578 return TRUE;
2581 void folder_clean_cache_memory_force(void)
2583 int old_cache_max_mem_usage = prefs_common.cache_max_mem_usage;
2584 int old_cache_min_keep_time = prefs_common.cache_min_keep_time;
2586 prefs_common.cache_max_mem_usage = 0;
2587 prefs_common.cache_min_keep_time = 0;
2589 folder_clean_cache_memory(NULL);
2591 prefs_common.cache_max_mem_usage = old_cache_max_mem_usage;
2592 prefs_common.cache_min_keep_time = old_cache_min_keep_time;
2595 void folder_clean_cache_memory(FolderItem *protected_item)
2597 gint memusage = 0;
2599 folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);
2600 debug_print("Total cache memory usage: %d\n", memusage);
2602 if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
2603 GSList *folder_item_list = NULL, *listitem;
2605 debug_print("Trying to free cache memory\n");
2607 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);
2608 listitem = folder_item_list;
2609 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
2610 FolderItem *item = (FolderItem *)(listitem->data);
2611 gint cache_size = 0;
2612 if (item == protected_item) {
2613 listitem = listitem->next;
2614 continue;
2616 debug_print("Freeing cache memory for %s\n", item->path ? item->path : item->name);
2617 cache_size = msgcache_get_memory_usage(item->cache);
2618 if (folder_item_free_cache(item, FALSE))
2619 memusage -= cache_size;
2621 listitem = listitem->next;
2623 g_slist_free(folder_item_list);
2627 static void folder_item_remove_cached_msg(FolderItem *item, MsgInfo *msginfo)
2629 Folder *folder = item->folder;
2631 cm_return_if_fail(folder != NULL);
2633 if (folder->klass->remove_cached_msg == NULL)
2634 return;
2636 folder->klass->remove_cached_msg(folder, item, msginfo);
2639 static void folder_item_clean_local_files(FolderItem *item, gint days)
2641 cm_return_if_fail(item != NULL);
2642 cm_return_if_fail(item->folder != NULL);
2644 if (FOLDER_TYPE(item->folder) == F_IMAP ||
2645 FOLDER_TYPE(item->folder) == F_NEWS) {
2646 GSList *msglist = folder_item_get_msg_list(item);
2647 GSList *cur;
2648 time_t t = time(NULL);
2649 for (cur = msglist; cur; cur = cur->next) {
2650 MsgInfo *msginfo = (MsgInfo *)cur->data;
2651 gint age = (t - msginfo->date_t) / (60*60*24);
2652 if (age > days)
2653 folder_item_remove_cached_msg(item, msginfo);
2655 procmsg_msg_list_free(msglist);
2659 static void folder_item_read_cache(FolderItem *item)
2661 gchar *cache_file, *mark_file, *tags_file;
2662 START_TIMING("");
2663 cm_return_if_fail(item != NULL);
2665 if (item->path != NULL) {
2666 cache_file = folder_item_get_cache_file(item);
2667 mark_file = folder_item_get_mark_file(item);
2668 tags_file = folder_item_get_tags_file(item);
2669 item->cache = msgcache_read_cache(item, cache_file);
2670 item->cache_dirty = FALSE;
2671 item->mark_dirty = FALSE;
2672 item->tags_dirty = FALSE;
2673 if (!item->cache) {
2674 MsgInfoList *list, *cur;
2675 guint newcnt = 0, unreadcnt = 0;
2676 guint markedcnt = 0, unreadmarkedcnt = 0;
2677 guint repliedcnt = 0, forwardedcnt = 0;
2678 guint lockedcnt = 0, ignoredcnt = 0;
2679 guint watchedcnt = 0;
2680 MsgInfo *msginfo;
2682 item->cache = msgcache_new();
2683 item->cache_dirty = TRUE;
2684 item->mark_dirty = TRUE;
2685 item->tags_dirty = TRUE;
2686 folder_item_scan_full(item, TRUE);
2688 msgcache_read_mark(item->cache, mark_file);
2690 list = msgcache_get_msg_list(item->cache);
2691 for (cur = list; cur != NULL; cur = g_slist_next(cur)) {
2692 msginfo = cur->data;
2694 if (MSG_IS_NEW(msginfo->flags))
2695 newcnt++;
2696 if (MSG_IS_UNREAD(msginfo->flags))
2697 unreadcnt++;
2698 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2699 unreadmarkedcnt++;
2700 if (MSG_IS_MARKED(msginfo->flags))
2701 markedcnt++;
2702 if (MSG_IS_REPLIED(msginfo->flags))
2703 repliedcnt++;
2704 if (MSG_IS_FORWARDED(msginfo->flags))
2705 forwardedcnt++;
2706 if (MSG_IS_LOCKED(msginfo->flags))
2707 lockedcnt++;
2708 if (MSG_IS_IGNORE_THREAD(msginfo->flags))
2709 ignoredcnt++;
2710 if (MSG_IS_WATCH_THREAD(msginfo->flags))
2711 watchedcnt++;
2712 procmsg_msginfo_unset_flags(msginfo, MSG_FULLY_CACHED, 0);
2714 item->new_msgs = newcnt;
2715 item->unread_msgs = unreadcnt;
2716 item->unreadmarked_msgs = unreadmarkedcnt;
2717 item->marked_msgs = markedcnt;
2718 item->replied_msgs = repliedcnt;
2719 item->forwarded_msgs = forwardedcnt;
2720 item->locked_msgs = lockedcnt;
2721 item->ignored_msgs = ignoredcnt;
2722 item->watched_msgs = watchedcnt;
2723 procmsg_msg_list_free(list);
2724 } else
2725 msgcache_read_mark(item->cache, mark_file);
2727 msgcache_read_tags(item->cache, tags_file);
2729 g_free(cache_file);
2730 g_free(mark_file);
2731 g_free(tags_file);
2732 } else {
2733 item->cache = msgcache_new();
2734 item->cache_dirty = TRUE;
2735 item->mark_dirty = TRUE;
2736 item->tags_dirty = TRUE;
2739 END_TIMING();
2740 folder_clean_cache_memory(item);
2743 void folder_item_write_cache(FolderItem *item)
2745 gchar *cache_file = NULL, *mark_file = NULL, *tags_file = NULL;
2746 FolderItemPrefs *prefs;
2747 gint filemode = 0;
2748 gchar *id;
2749 time_t last_mtime = (time_t)0;
2750 gboolean need_scan = FALSE;
2752 if (!item || !item->path || !item->cache)
2753 return;
2755 last_mtime = item->mtime;
2756 if (item->folder->klass->set_mtime) {
2757 if (item->folder->klass->scan_required)
2758 need_scan = item->folder->klass->scan_required(item->folder, item);
2759 else
2760 need_scan = TRUE;
2763 id = folder_item_get_identifier(item);
2764 debug_print("Save cache for folder %s\n", id);
2765 g_free(id);
2767 if (item->cache_dirty)
2768 cache_file = folder_item_get_cache_file(item);
2769 if (item->cache_dirty || item->mark_dirty)
2770 mark_file = folder_item_get_mark_file(item);
2771 if (item->cache_dirty || item->tags_dirty)
2772 tags_file = folder_item_get_tags_file(item);
2773 if (msgcache_write(cache_file, mark_file, tags_file, item->cache) == 0) {
2774 prefs = item->prefs;
2775 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
2776 filemode = prefs->folder_chmod;
2777 if (filemode & S_IRGRP) filemode |= S_IWGRP;
2778 if (filemode & S_IROTH) filemode |= S_IWOTH;
2779 if (cache_file) {
2780 if (chmod(cache_file, filemode) < 0)
2781 FILE_OP_ERROR(cache_file, "chmod");
2783 if (mark_file) {
2784 if (chmod(mark_file, filemode) < 0)
2785 FILE_OP_ERROR(mark_file, "chmod");
2788 } else {
2789 item->cache_dirty = TRUE;
2790 item->mark_dirty = TRUE;
2791 item->tags_dirty = TRUE;
2794 if (!need_scan && item->folder->klass->set_mtime) {
2795 if (item->mtime == last_mtime) {
2796 item->folder->klass->set_mtime(item->folder, item);
2800 g_free(cache_file);
2801 g_free(mark_file);
2802 g_free(tags_file);
2805 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
2807 MsgInfo *msginfo = NULL;
2809 cm_return_val_if_fail(item != NULL, NULL);
2810 if (item->no_select)
2811 return NULL;
2812 if (!item->cache)
2813 folder_item_read_cache(item);
2815 if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
2816 return msginfo;
2818 msginfo = get_msginfo(item, num);
2819 if (msginfo != NULL) {
2820 msgcache_add_msg(item->cache, msginfo);
2821 return msginfo;
2824 return NULL;
2827 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
2829 MsgInfo *msginfo;
2831 cm_return_val_if_fail(item != NULL, NULL);
2832 cm_return_val_if_fail(msgid != NULL, NULL);
2833 if (item->no_select)
2834 return NULL;
2836 if (!item->cache)
2837 folder_item_read_cache(item);
2839 if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
2840 return msginfo;
2842 return NULL;
2845 GSList *folder_item_get_msg_list(FolderItem *item)
2847 cm_return_val_if_fail(item != NULL, NULL);
2848 if (item->no_select)
2849 return NULL;
2851 if (item->cache == 0)
2852 folder_item_read_cache(item);
2854 cm_return_val_if_fail(item->cache != NULL, NULL);
2856 return msgcache_get_msg_list(item->cache);
2859 static void msginfo_set_mime_flags(GNode *node, gpointer data)
2861 MsgInfo *msginfo = data;
2862 MimeInfo *mimeinfo = node->data;
2864 if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT &&
2865 (!mimeinfo->subtype || (strcmp(mimeinfo->subtype, "pgp-signature") &&
2866 strcmp(mimeinfo->subtype, "x-pkcs7-signature") &&
2867 strcmp(mimeinfo->subtype, "pkcs7-signature")))) {
2868 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2869 } else if (mimeinfo->disposition == DISPOSITIONTYPE_UNKNOWN &&
2870 mimeinfo->id == NULL &&
2871 mimeinfo->type != MIMETYPE_TEXT &&
2872 mimeinfo->type != MIMETYPE_MULTIPART) {
2873 if (!mimeinfo->subtype
2874 || (strcmp(mimeinfo->subtype, "pgp-signature") &&
2875 strcmp(mimeinfo->subtype, "x-pkcs7-signature") &&
2876 strcmp(mimeinfo->subtype, "pkcs7-signature")))
2877 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2878 } else if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE &&
2879 mimeinfo->id == NULL &&
2880 (strcmp(mimeinfo->subtype, "pgp-signature") &&
2881 strcmp(mimeinfo->subtype, "x-pkcs7-signature") &&
2882 strcmp(mimeinfo->subtype, "pkcs7-signature")) &&
2883 (procmime_mimeinfo_get_parameter(mimeinfo, "name") != NULL ||
2884 procmime_mimeinfo_get_parameter(mimeinfo, "filename") != NULL)) {
2885 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2888 /* don't descend below top level message for signed and encrypted info */
2889 if (mimeinfo->type == MIMETYPE_MESSAGE)
2890 return;
2892 if (privacy_mimeinfo_is_signed(mimeinfo)) {
2893 procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
2896 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
2897 procmsg_msginfo_set_flags(msginfo, 0, MSG_ENCRYPTED);
2898 } else {
2899 /* searching inside encrypted parts doesn't really make sense */
2900 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2904 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
2906 Folder *folder;
2907 gchar *msgfile;
2908 MsgInfo *msginfo;
2910 cm_return_val_if_fail(item != NULL, NULL);
2912 folder = item->folder;
2914 cm_return_val_if_fail(folder->klass->fetch_msg != NULL, NULL);
2915 if (item->no_select)
2916 return NULL;
2918 msgfile = folder->klass->fetch_msg(folder, item, num);
2920 if (msgfile != NULL) {
2921 msginfo = folder_item_get_msginfo(item, num);
2922 if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
2923 MimeInfo *mimeinfo;
2925 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
2926 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
2927 mimeinfo = procmime_scan_file(msgfile);
2928 else
2929 mimeinfo = procmime_scan_queue_file(msgfile);
2930 /* check for attachments */
2931 if (mimeinfo != NULL) {
2932 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2933 procmime_mimeinfo_free_all(&mimeinfo);
2935 procmsg_msginfo_set_flags(msginfo, 0, MSG_SCANNED);
2938 procmsg_msginfo_free(&msginfo);
2941 return msgfile;
2944 gchar *folder_item_fetch_msg_full(FolderItem *item, gint num, gboolean headers,
2945 gboolean body)
2947 Folder *folder;
2948 gchar *msgfile;
2949 MsgInfo *msginfo;
2951 cm_return_val_if_fail(item != NULL, NULL);
2952 if (item->no_select)
2953 return NULL;
2955 folder = item->folder;
2957 if (folder->klass->fetch_msg_full == NULL)
2958 return folder_item_fetch_msg(item, num);
2960 if (item->prefs->offlinesync && prefs_common.real_time_sync)
2961 msgfile = folder->klass->fetch_msg_full(folder, item, num,
2962 TRUE, TRUE);
2963 else
2964 msgfile = folder->klass->fetch_msg_full(folder, item, num,
2965 headers, body);
2967 if (msgfile != NULL) {
2968 msginfo = folder_item_get_msginfo(item, num);
2969 if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
2970 MimeInfo *mimeinfo;
2972 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
2973 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
2974 mimeinfo = procmime_scan_file(msgfile);
2975 else
2976 mimeinfo = procmime_scan_queue_file(msgfile);
2977 /* check for attachments */
2978 if (mimeinfo != NULL) {
2979 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2980 procmime_mimeinfo_free_all(&mimeinfo);
2982 procmsg_msginfo_set_flags(msginfo, 0, MSG_SCANNED);
2985 procmsg_msginfo_free(&msginfo);
2988 return msgfile;
2992 static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
2994 static HeaderEntry hentry[] = {{"Message-ID:", NULL, TRUE},
2995 {NULL, NULL, FALSE}};
2996 FILE *fp;
2997 MsgInfo *msginfo;
2998 gint msgnum = 0;
2999 gchar buf[BUFFSIZE];
3001 if ((fp = claws_fopen(file, "rb")) == NULL)
3002 return 0;
3004 if ((folder_has_parent_of_type(dest, F_QUEUE)) ||
3005 (folder_has_parent_of_type(dest, F_DRAFT)))
3006 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
3007 /* new way */
3008 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
3009 strlen("X-Claws-End-Special-Headers:"))) ||
3010 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
3011 strlen("X-Sylpheed-End-Special-Headers:"))))
3012 break;
3013 /* old way */
3014 if (buf[0] == '\r' || buf[0] == '\n') break;
3015 /* from other mailers */
3016 if (!strncmp(buf, "Date: ", 6)
3017 || !strncmp(buf, "To: ", 4)
3018 || !strncmp(buf, "From: ", 6)
3019 || !strncmp(buf, "Subject: ", 9)) {
3020 rewind(fp);
3021 break;
3025 procheader_get_header_fields(fp, hentry);
3026 debug_print("looking for %s\n", hentry[0].body);
3027 if (hentry[0].body) {
3028 extract_parenthesis(hentry[0].body, '<', '>');
3029 remove_space(hentry[0].body);
3030 if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
3031 msgnum = msginfo->msgnum;
3032 procmsg_msginfo_free(&msginfo);
3034 debug_print("found message as uid %d\n", msgnum);
3038 g_free(hentry[0].body);
3039 hentry[0].body = NULL;
3040 claws_fclose(fp);
3042 return msgnum;
3045 static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
3047 MsgPermFlags perm_flags = 0;
3048 MsgTmpFlags tmp_flags = 0;
3050 /* create new flags */
3051 if (source != NULL) {
3052 /* copy original flags */
3053 perm_flags = source->flags.perm_flags;
3054 tmp_flags = source->flags.tmp_flags;
3055 } else {
3056 perm_flags = dest->flags.perm_flags;
3057 tmp_flags = dest->flags.tmp_flags;
3060 /* remove new, unread and deleted in special folders */
3061 if (folder_has_parent_of_type(dest->folder, F_OUTBOX) ||
3062 folder_has_parent_of_type(dest->folder, F_QUEUE) ||
3063 folder_has_parent_of_type(dest->folder, F_DRAFT) ||
3064 folder_has_parent_of_type(dest->folder, F_TRASH))
3065 perm_flags &= ~(MSG_NEW | MSG_UNREAD | MSG_DELETED);
3067 /* set ignore flag of ignored parent exists */
3068 if (procmsg_msg_has_flagged_parent(dest, MSG_IGNORE_THREAD))
3069 perm_flags |= MSG_IGNORE_THREAD;
3071 /* unset FULLY_CACHED flags */
3072 perm_flags &= ~MSG_FULLY_CACHED;
3074 if (procmsg_msg_has_flagged_parent(dest, MSG_WATCH_THREAD))
3075 perm_flags |= MSG_WATCH_THREAD;
3077 /* Unset tmp flags that should not be copied */
3078 tmp_flags &= ~(MSG_MOVE | MSG_COPY | MSG_MOVE_DONE);
3080 /* unset flags that are set but should not */
3081 /* and set new flags */
3082 procmsg_msginfo_change_flags(dest,
3083 ~dest->flags.perm_flags & perm_flags,
3084 ~dest->flags.tmp_flags & tmp_flags,
3085 dest->flags.perm_flags & ~perm_flags,
3086 dest->flags.tmp_flags & ~tmp_flags);
3088 if (source && source->tags) {
3089 g_slist_free(dest->tags);
3090 dest->tags = g_slist_copy(source->tags);
3091 folder_item_commit_tags(dest->folder, dest, dest->tags, NULL);
3095 static void add_msginfo_to_cache(FolderItem *item, MsgInfo *newmsginfo, MsgInfo *flagsource)
3097 /* update folder stats */
3098 if (MSG_IS_NEW(newmsginfo->flags))
3099 item->new_msgs++;
3100 if (MSG_IS_UNREAD(newmsginfo->flags))
3101 item->unread_msgs++;
3102 if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
3103 item->unreadmarked_msgs++;
3104 if (MSG_IS_MARKED(newmsginfo->flags))
3105 item->marked_msgs++;
3106 if (MSG_IS_REPLIED(newmsginfo->flags))
3107 item->replied_msgs++;
3108 if (MSG_IS_FORWARDED(newmsginfo->flags))
3109 item->forwarded_msgs++;
3110 if (MSG_IS_LOCKED(newmsginfo->flags))
3111 item->locked_msgs++;
3112 if (MSG_IS_IGNORE_THREAD(newmsginfo->flags))
3113 item->ignored_msgs++;
3114 if (MSG_IS_WATCH_THREAD(newmsginfo->flags))
3115 item->watched_msgs++;
3116 item->total_msgs++;
3118 folder_item_update_freeze();
3120 if (!item->cache)
3121 folder_item_read_cache(item);
3123 msgcache_add_msg(item->cache, newmsginfo);
3124 copy_msginfo_flags(flagsource, newmsginfo);
3125 folder_item_update_with_msg(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_ADDMSG, newmsginfo);
3126 folder_item_update_thaw();
3129 static void remove_msginfo_from_cache(FolderItem *item, MsgInfo *msginfo)
3131 MsgInfoUpdate msginfo_update;
3133 if (!item->cache)
3134 folder_item_read_cache(item);
3136 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
3137 msginfo->folder->new_msgs--;
3138 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
3139 msginfo->folder->unread_msgs--;
3140 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
3141 msginfo->folder->unreadmarked_msgs--;
3142 if (MSG_IS_MARKED(msginfo->flags))
3143 item->marked_msgs--;
3144 if (MSG_IS_REPLIED(msginfo->flags))
3145 item->replied_msgs--;
3146 if (MSG_IS_FORWARDED(msginfo->flags))
3147 item->forwarded_msgs--;
3148 if (MSG_IS_LOCKED(msginfo->flags))
3149 item->locked_msgs--;
3150 if (MSG_IS_IGNORE_THREAD(msginfo->flags))
3151 item->ignored_msgs--;
3152 if (MSG_IS_WATCH_THREAD(msginfo->flags))
3153 item->watched_msgs--;
3155 msginfo->folder->total_msgs--;
3157 msginfo_update.msginfo = msginfo;
3158 msginfo_update.flags = MSGINFO_UPDATE_DELETED;
3159 hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
3161 msgcache_remove_msg(item->cache, msginfo->msgnum);
3162 folder_item_update_with_msg(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_REMOVEMSG, msginfo);
3165 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
3166 MsgFlags *flags, gboolean remove_source)
3168 GSList file_list;
3169 MsgFileInfo fileinfo;
3171 cm_return_val_if_fail(dest != NULL, -1);
3172 cm_return_val_if_fail(file != NULL, -1);
3174 fileinfo.msginfo = NULL;
3175 fileinfo.file = (gchar *)file;
3176 fileinfo.flags = flags;
3177 file_list.data = &fileinfo;
3178 file_list.next = NULL;
3180 return folder_item_add_msgs(dest, &file_list, remove_source);
3183 gint folder_item_add_msgs(FolderItem *dest, GSList *file_list,
3184 gboolean remove_source)
3186 Folder *folder;
3187 gint ret, num, lastnum = -1;
3188 GSList *file_cur;
3189 GHashTable *relation;
3190 MsgFileInfo *fileinfo = NULL;
3191 gboolean folderscan = FALSE;
3193 cm_return_val_if_fail(dest != NULL, -1);
3194 cm_return_val_if_fail(file_list != NULL, -1);
3195 cm_return_val_if_fail(dest->folder != NULL, -1);
3196 if (dest->no_select)
3197 return -1;
3199 folder = dest->folder;
3201 relation = g_hash_table_new(g_direct_hash, g_direct_equal);
3203 if (folder->klass->add_msgs != NULL) {
3204 ret = folder->klass->add_msgs(folder, dest, file_list, relation);
3205 if (ret < 0) {
3206 g_hash_table_destroy(relation);
3207 return ret;
3209 } else {
3210 for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
3211 fileinfo = (MsgFileInfo *) file_cur->data;
3213 ret = folder->klass->add_msg(folder, dest, fileinfo->file, fileinfo->flags);
3214 if (ret < 0) {
3215 g_hash_table_destroy(relation);
3216 return ret;
3218 g_hash_table_insert(relation, fileinfo, GINT_TO_POINTER(ret));
3222 for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
3223 gpointer data, old_key;
3225 fileinfo = (MsgFileInfo *) file_cur->data;
3226 if (g_hash_table_lookup_extended(relation, fileinfo, &old_key, &data))
3227 num = GPOINTER_TO_INT(data);
3228 else
3229 num = -1;
3231 if (num >= 0) {
3232 MsgInfo *newmsginfo;
3234 if (num == 0) {
3235 if (!folderscan) {
3236 folder_item_scan_full(dest, FALSE);
3237 folderscan = TRUE;
3239 num = folder_item_get_msg_num_by_file(dest, fileinfo->file);
3240 debug_print("got num %d\n", num);
3243 if (num > lastnum)
3244 lastnum = num;
3246 if (num >= 0 && remove_source) {
3247 if (claws_unlink(fileinfo->file) < 0)
3248 FILE_OP_ERROR(fileinfo->file, "unlink");
3251 if (num == 0)
3252 continue;
3254 if (!folderscan &&
3255 ((newmsginfo = get_msginfo(dest, num)) != NULL)) {
3256 add_msginfo_to_cache(dest, newmsginfo, NULL);
3257 procmsg_msginfo_free(&newmsginfo);
3258 } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
3259 /* TODO: set default flags */
3260 procmsg_msginfo_free(&newmsginfo);
3265 g_hash_table_destroy(relation);
3267 return lastnum;
3270 static FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest, gboolean copy)
3272 GSList *mlist;
3273 FolderItem *new_item;
3274 FolderItem *next_item;
3275 GNode *srcnode;
3276 gchar *old_id, *new_id;
3277 FolderUpdateData hookdata;
3279 /* move messages */
3280 debug_print("%s %s to %s\n", copy?"Copying":"Moving", src->path, dest->path);
3281 new_item = folder_create_folder(dest, src->name);
3282 if (new_item == NULL) {
3283 g_print("Can't create folder\n");
3284 return NULL;
3287 if (new_item->folder == NULL)
3288 new_item->folder = dest->folder;
3290 /* move messages */
3291 log_message(LOG_PROTOCOL, copy ?_("Copying %s to %s...\n"):_("Moving %s to %s...\n"),
3292 src->name, new_item->path);
3294 /*copy prefs*/
3295 folder_item_prefs_copy_prefs(src, new_item);
3297 /* copy internal data */
3298 if (src->folder->klass == new_item->folder->klass &&
3299 src->folder->klass->copy_private_data != NULL)
3300 src->folder->klass->copy_private_data(src->folder,
3301 src, new_item);
3302 new_item->collapsed = src->collapsed;
3303 new_item->thread_collapsed = src->thread_collapsed;
3304 new_item->threaded = src->threaded;
3305 new_item->ret_rcpt = src->ret_rcpt;
3306 new_item->hide_read_msgs = src->hide_read_msgs;
3307 new_item->hide_del_msgs = src->hide_del_msgs;
3308 new_item->hide_read_threads = src->hide_read_threads;
3309 new_item->sort_key = src->sort_key;
3310 new_item->sort_type = src->sort_type;
3312 mlist = folder_item_get_msg_list(src);
3314 if (mlist != NULL) {
3315 if (copy)
3316 folder_item_copy_msgs(new_item, mlist);
3317 else
3318 folder_item_move_msgs(new_item, mlist);
3319 procmsg_msg_list_free(mlist);
3322 prefs_matcher_write_config();
3324 /* recurse */
3325 srcnode = src->folder->node;
3326 srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
3327 srcnode = srcnode->children;
3328 while (srcnode != NULL) {
3329 if (srcnode && srcnode->data) {
3330 next_item = (FolderItem*) srcnode->data;
3331 srcnode = srcnode->next;
3332 if (folder_item_move_recursive(next_item, new_item, copy) == NULL) {
3333 return NULL;
3337 old_id = folder_item_get_identifier(src);
3338 new_id = folder_item_get_identifier(new_item);
3340 hookdata.folder = src->folder;
3341 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_MOVE_FOLDERITEM;
3342 hookdata.item = src;
3343 hookdata.item2 = new_item;
3344 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
3346 /* if src supports removing, otherwise only copy folder */
3347 if (src->folder->klass->remove_folder != NULL && !copy)
3348 src->folder->klass->remove_folder(src->folder, src);
3349 folder_write_list();
3351 if (!copy) {
3352 debug_print("updating rules : %s => %s\n", old_id, new_id);
3353 if (old_id != NULL && new_id != NULL) {
3354 prefs_filtering_rename_path(old_id, new_id);
3355 account_rename_path(old_id, new_id);
3358 g_free(old_id);
3359 g_free(new_id);
3361 return new_item;
3364 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item, gboolean copy)
3366 FolderItem *tmp = folder_item_parent(dest);
3367 gchar * src_identifier, * dst_identifier;
3368 gchar * phys_srcpath, * phys_dstpath, *tmppath, *tmpname;
3370 while (tmp) {
3371 if (tmp == src) {
3372 return F_MOVE_FAILED_DEST_IS_CHILD;
3374 tmp = folder_item_parent(tmp);
3377 /* both dst and src can be root folders */
3378 src_identifier = folder_item_get_identifier(src);
3379 if (src_identifier == NULL && src->folder && folder_item_parent(src) == NULL) {
3380 src_identifier = folder_get_identifier(src->folder);
3383 dst_identifier = folder_item_get_identifier(dest);
3384 if(dst_identifier == NULL && dest->folder && folder_item_parent(dest) == NULL) {
3385 dst_identifier = folder_get_identifier(dest->folder);
3388 if (src_identifier == NULL || dst_identifier == NULL) {
3389 debug_print("Can't get identifiers\n");
3390 if (src_identifier)
3391 g_free(src_identifier);
3392 if (dst_identifier)
3393 g_free(dst_identifier);
3394 return F_MOVE_FAILED;
3397 if (src->folder != dest->folder && !copy) {
3398 g_free(src_identifier);
3399 g_free(dst_identifier);
3400 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
3402 g_free(src_identifier);
3403 g_free(dst_identifier);
3405 phys_srcpath = folder_item_get_path(src);
3406 tmppath = folder_item_get_path(dest);
3407 tmpname = g_path_get_basename(phys_srcpath);
3408 phys_dstpath = g_strconcat(tmppath,
3409 G_DIR_SEPARATOR_S,
3410 tmpname,
3411 NULL);
3412 g_free(tmpname);
3413 g_free(tmppath);
3415 if (folder_item_parent(src) == dest || src == dest) {
3416 g_free(phys_srcpath);
3417 g_free(phys_dstpath);
3418 return F_MOVE_FAILED_DEST_IS_PARENT;
3420 debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
3421 if ((tmp = folder_item_move_recursive(src, dest, copy)) == NULL) {
3422 g_free(phys_srcpath);
3423 g_free(phys_dstpath);
3424 return F_MOVE_FAILED;
3427 g_free(phys_srcpath);
3428 g_free(phys_dstpath);
3430 *new_item = tmp;
3432 return F_MOVE_OK;
3435 struct find_data
3437 gboolean found;
3439 static void find_num(gpointer key, gpointer value, gpointer data)
3441 struct find_data *fdata = (struct find_data *)data;
3442 if (GPOINTER_TO_INT(value) == 0)
3443 fdata->found = TRUE;
3446 static gboolean some_msgs_have_zero_num(GHashTable *hashtable)
3448 struct find_data fdata;
3450 fdata.found = FALSE;
3451 g_hash_table_foreach(hashtable, find_num, &fdata);
3453 return fdata.found;
3457 * Copy a list of message to a new folder and remove
3458 * source messages if wanted
3460 static gint do_copy_msgs(FolderItem *dest, GSList *msglist, gboolean remove_source)
3462 Folder *folder;
3463 GSList *l;
3464 gint num, lastnum = -1;
3465 gboolean folderscan = FALSE;
3466 GHashTable *relation;
3467 GSList *not_moved = NULL;
3468 gint total = 0, curmsg = 0;
3469 MsgInfo *msginfo = NULL;
3471 cm_return_val_if_fail(dest != NULL, -1);
3472 cm_return_val_if_fail(msglist != NULL, -1);
3474 folder = dest->folder;
3476 cm_return_val_if_fail(folder->klass->copy_msg != NULL, -1);
3477 if (dest->no_select)
3478 return -1;
3480 msginfo = (MsgInfo *)msglist->data;
3482 if (!msginfo)
3483 return -1;
3485 if (!MSG_IS_QUEUED(msginfo->flags) &&
3486 MSG_IS_DRAFT(msginfo->flags) &&
3487 folder_has_parent_of_type(dest, F_QUEUE)) {
3488 GSList *cur = msglist;
3489 gboolean queue_err = FALSE;
3490 for (; cur; cur = cur->next) {
3491 Compose *compose = NULL;
3492 FolderItem *queue = dest;
3493 ComposeQueueResult val = COMPOSE_QUEUE_SUCCESS;
3495 msginfo = (MsgInfo *)cur->data;
3496 compose = compose_reedit(msginfo, TRUE);
3497 if (compose == NULL) {
3498 queue_err = TRUE;
3499 continue;
3501 val = compose_queue(compose, NULL, &queue, NULL,
3502 FALSE);
3503 if (val != COMPOSE_QUEUE_SUCCESS) {
3504 queue_err = TRUE;
3505 } else if (remove_source) {
3506 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
3508 if (val == COMPOSE_QUEUE_SUCCESS)
3509 compose_close(compose);
3511 return queue_err ? -1:0;
3514 relation = g_hash_table_new(g_direct_hash, g_direct_equal);
3516 for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
3517 MsgInfo * msginfo = (MsgInfo *) l->data;
3519 if (msginfo->planned_download != 0) {
3520 int old_planned = msginfo->planned_download;
3521 partial_unmark(msginfo);
3522 /* little hack to reenable after */
3523 msginfo->planned_download = old_planned;
3528 * Copy messages to destination folder and
3529 * store new message numbers in newmsgnums
3531 if (folder->klass->copy_msgs != NULL) {
3532 if (folder->klass->copy_msgs(folder, dest, msglist, relation) < 0) {
3533 g_hash_table_destroy(relation);
3534 return -1;
3536 } else {
3537 MsgInfo * msginfo;
3538 l = msglist;
3540 /* immediately stop if src and dest folders are identical */
3541 if (l != NULL) {
3542 msginfo = (MsgInfo *) l->data;
3543 if (msginfo != NULL && msginfo->folder == dest) {
3544 g_hash_table_destroy(relation);
3545 return -1;
3549 for (; l != NULL ; l = g_slist_next(l)) {
3550 msginfo = (MsgInfo *) l->data;
3552 num = folder->klass->copy_msg(folder, dest, msginfo);
3553 if (num > 0)
3554 g_hash_table_insert(relation, msginfo, GINT_TO_POINTER(num));
3555 else
3556 not_moved = g_slist_prepend(not_moved, msginfo);
3560 if (remove_source) {
3561 MsgInfo *msginfo = (MsgInfo *) msglist->data;
3562 FolderItem *item = msginfo->folder;
3564 * Remove source messages from their folders if
3565 * copying was successfull and update folder
3566 * message counts
3568 if (not_moved == NULL && item->folder->klass->remove_msgs) {
3569 item->folder->klass->remove_msgs(item->folder,
3570 msginfo->folder,
3571 msglist,
3572 relation);
3574 for (l = msglist; l != NULL; l = g_slist_next(l)) {
3575 gpointer old_key, data;
3576 msginfo = (MsgInfo *) l->data;
3577 item = msginfo->folder;
3579 if (g_hash_table_lookup_extended(relation, msginfo, &old_key, &data))
3580 num = GPOINTER_TO_INT(data);
3581 else
3582 num = 0;
3584 if (g_slist_find(not_moved, msginfo))
3585 continue;
3587 if ((num >= 0) && (item->folder->klass->remove_msg != NULL)) {
3588 if (!item->folder->klass->remove_msgs)
3589 item->folder->klass->remove_msg(item->folder,
3590 msginfo->folder,
3591 msginfo->msgnum);
3592 remove_msginfo_from_cache(item, msginfo);
3597 /* Read cache for dest folder */
3598 if (!dest->cache) folder_item_read_cache(dest);
3601 * Fetch new MsgInfos for new messages in dest folder,
3602 * add them to the msgcache and update folder message counts
3604 if (some_msgs_have_zero_num(relation)) {
3605 folder_item_scan_full(dest, FALSE);
3606 folderscan = TRUE;
3609 statusbar_print_all(_("Updating cache for %s..."), dest->path ? dest->path : "(null)");
3610 total = g_slist_length(msglist);
3612 if (FOLDER_TYPE(dest->folder) == F_IMAP && total > 1) {
3613 folder_item_scan_full(dest, FALSE);
3614 folderscan = TRUE;
3616 folder_item_set_batch(dest, TRUE);
3617 for (l = msglist; l != NULL; l = g_slist_next(l)) {
3618 MsgInfo *msginfo = (MsgInfo *) l->data;
3619 gpointer data, old_key;
3621 if (!msginfo)
3622 continue;
3624 if (g_hash_table_lookup_extended(relation, msginfo, &old_key, &data))
3625 num = GPOINTER_TO_INT(data);
3626 else
3627 num = 0;
3629 statusbar_progress_all(curmsg++,total, 100);
3630 if (curmsg % 100 == 0)
3631 GTK_EVENTS_FLUSH();
3633 if (num >= 0) {
3634 MsgInfo *newmsginfo = NULL;
3636 if (!folderscan && num > 0) {
3637 newmsginfo = get_msginfo(dest, num);
3638 if (newmsginfo != NULL) {
3639 add_msginfo_to_cache(dest, newmsginfo, msginfo);
3642 if (newmsginfo == NULL) {
3643 if (!folderscan) {
3644 folder_item_scan_full(dest, FALSE);
3645 folderscan = TRUE;
3647 if (msginfo->msgid != NULL) {
3648 newmsginfo = folder_item_get_msginfo_by_msgid(dest, msginfo->msgid);
3649 if (newmsginfo != NULL) {
3650 copy_msginfo_flags(msginfo, newmsginfo);
3651 num = newmsginfo->msgnum;
3655 if (newmsginfo != NULL
3656 && msginfo->planned_download == POP3_PARTIAL_DLOAD_DELE) {
3657 partial_mark_for_delete(newmsginfo);
3659 if (newmsginfo != NULL
3660 && msginfo->planned_download == POP3_PARTIAL_DLOAD_DLOAD) {
3661 partial_mark_for_download(newmsginfo);
3663 if (!MSG_IS_POSTFILTERED (msginfo->flags)) {
3664 procmsg_msginfo_set_flags ( msginfo, MSG_POSTFILTERED, 0);
3665 if (newmsginfo) {
3666 procmsg_msginfo_set_flags (newmsginfo, MSG_POSTFILTERED, 0);
3667 hooks_invoke (MAIL_POSTFILTERING_HOOKLIST, newmsginfo);
3670 procmsg_msginfo_free(&newmsginfo);
3673 if (num > lastnum)
3674 lastnum = num;
3677 folder_item_set_batch(dest, FALSE);
3678 statusbar_progress_all(0,0,0);
3679 statusbar_pop_all();
3681 g_hash_table_destroy(relation);
3682 if (not_moved != NULL) {
3683 g_slist_free(not_moved);
3684 return -1;
3685 } else
3686 return lastnum;
3690 * Move a message to a new folder.
3692 * \param dest Destination folder
3693 * \param msginfo The message
3695 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
3697 GSList list;
3699 cm_return_val_if_fail(dest != NULL, -1);
3700 cm_return_val_if_fail(msginfo != NULL, -1);
3702 list.data = msginfo;
3703 list.next = NULL;
3705 return do_copy_msgs(dest, &list, TRUE);
3709 * Move a list of messages to a new folder.
3711 * \param dest Destination folder
3712 * \param msglist List of messages
3714 gint folder_item_move_msgs(FolderItem *dest, GSList *msglist)
3716 gint result = -1;
3717 cm_return_val_if_fail(dest != NULL, -1);
3718 cm_return_val_if_fail(msglist != NULL, -1);
3719 inc_lock();
3720 result = do_copy_msgs(dest, msglist, TRUE);
3721 inc_unlock();
3722 return result;
3726 * Copy a message to a new folder.
3728 * \param dest Destination folder
3729 * \param msginfo The message
3731 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
3733 GSList list;
3735 cm_return_val_if_fail(dest != NULL, -1);
3736 cm_return_val_if_fail(msginfo != NULL, -1);
3738 list.data = msginfo;
3739 list.next = NULL;
3741 return do_copy_msgs(dest, &list, FALSE);
3745 * Copy a list of messages to a new folder.
3747 * \param dest Destination folder
3748 * \param msglist List of messages
3750 gint folder_item_copy_msgs(FolderItem *dest, GSList *msglist)
3752 gint result;
3753 cm_return_val_if_fail(dest != NULL, -1);
3754 cm_return_val_if_fail(msglist != NULL, -1);
3756 inc_lock();
3757 result = do_copy_msgs(dest, msglist, FALSE);
3758 inc_unlock();
3760 return result;
3763 gint folder_item_remove_msg(FolderItem *item, gint num)
3765 Folder *folder;
3766 gint ret;
3767 MsgInfo *msginfo;
3769 cm_return_val_if_fail(item != NULL, -1);
3770 folder = item->folder;
3771 cm_return_val_if_fail(folder->klass->remove_msg != NULL, -1);
3772 if (item->no_select)
3773 return -1;
3775 if (!item->cache) folder_item_read_cache(item);
3777 msginfo = msgcache_get_msg(item->cache, num);
3778 if (msginfo && MSG_IS_LOCKED(msginfo->flags)) {
3779 procmsg_msginfo_free(&msginfo);
3780 return -1;
3782 ret = folder->klass->remove_msg(folder, item, num);
3784 if (msginfo != NULL) {
3785 if (ret == 0)
3786 remove_msginfo_from_cache(item, msginfo);
3787 procmsg_msginfo_free(&msginfo);
3790 return ret;
3793 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
3795 Folder *folder;
3796 gint ret = 0;
3797 GSList *real_list = NULL, *cur = NULL;
3799 cm_return_val_if_fail(item != NULL, -1);
3800 folder = item->folder;
3801 cm_return_val_if_fail(folder != NULL, -1);
3802 if (item->no_select)
3803 return -1;
3804 inc_lock();
3805 if (!item->cache) folder_item_read_cache(item);
3807 folder_item_update_freeze();
3809 /* filter out locked mails */
3810 for (cur = msglist; cur; cur = cur->next) {
3811 MsgInfo *info = (MsgInfo *)cur->data;
3812 if (!MSG_IS_LOCKED(info->flags))
3813 real_list = g_slist_prepend(real_list, info);
3816 real_list = g_slist_reverse(real_list);
3818 if (item->folder->klass->remove_msgs) {
3819 ret = item->folder->klass->remove_msgs(item->folder,
3820 item,
3821 real_list,
3822 NULL);
3824 cur = real_list;
3825 while (ret == 0 && cur != NULL) {
3826 MsgInfo *msginfo = (MsgInfo *)cur->data;
3827 if (msginfo && MSG_IS_LOCKED(msginfo->flags)) {
3828 cur = cur->next;
3829 continue;
3831 if (!item->folder->klass->remove_msgs)
3832 ret = folder_item_remove_msg(item, msginfo->msgnum);
3833 if (ret != 0) break;
3834 msgcache_remove_msg(item->cache, msginfo->msgnum);
3835 cur = cur->next;
3837 g_slist_free(real_list);
3838 folder_item_scan_full(item, FALSE);
3839 folder_item_update_thaw();
3840 inc_unlock();
3841 return ret;
3844 gint folder_item_expunge(FolderItem *item)
3846 Folder *folder = item->folder;
3847 gint result = 0;
3848 if (folder == NULL)
3849 return -1;
3850 if (folder->klass->expunge) {
3851 GSList *msglist = folder_item_get_msg_list(item);
3852 GSList *cur;
3853 result = folder->klass->expunge(folder, item);
3854 if (result == 0) {
3855 for (cur = msglist; cur; cur = cur->next) {
3856 MsgInfo *msginfo = (MsgInfo *)cur->data;
3857 if (MSG_IS_DELETED(msginfo->flags)) {
3858 remove_msginfo_from_cache(item, msginfo);
3862 procmsg_msg_list_free(msglist);
3864 return result;
3867 gint folder_item_remove_all_msg(FolderItem *item)
3869 Folder *folder;
3870 gint result;
3872 cm_return_val_if_fail(item != NULL, -1);
3873 if (item->no_select)
3874 return -1;
3876 folder = item->folder;
3878 inc_lock();
3879 if (folder->klass->remove_all_msg != NULL) {
3880 result = folder->klass->remove_all_msg(folder, item);
3882 if (result == 0) {
3883 folder_item_free_cache(item, TRUE);
3884 item->cache = msgcache_new();
3885 item->cache_dirty = TRUE;
3886 item->mark_dirty = TRUE;
3887 item->tags_dirty = TRUE;
3889 } else {
3890 MsgInfoList *msglist;
3892 msglist = folder_item_get_msg_list(item);
3893 result = folder_item_remove_msgs(item, msglist);
3894 procmsg_msg_list_free(msglist);
3897 if (result == 0) {
3898 item->new_msgs = 0;
3899 item->unread_msgs = 0;
3900 item->unreadmarked_msgs = 0;
3901 item->marked_msgs = 0;
3902 item->total_msgs = 0;
3903 item->replied_msgs = 0;
3904 item->forwarded_msgs = 0;
3905 item->locked_msgs = 0;
3906 item->ignored_msgs = 0;
3907 item->watched_msgs = 0;
3908 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
3911 inc_unlock();
3912 return result;
3915 void folder_item_change_msg_flags(FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
3917 cm_return_if_fail(item != NULL);
3918 cm_return_if_fail(msginfo != NULL);
3920 item->mark_dirty = TRUE;
3922 if (item->no_select)
3923 return;
3925 if (item->folder->klass->change_flags != NULL && item->scanning != ITEM_SCANNING_WITH_FLAGS) {
3926 item->folder->klass->change_flags(item->folder, item, msginfo, newflags);
3927 } else {
3928 msginfo->flags.perm_flags = newflags;
3932 void folder_item_commit_tags(FolderItem *item, MsgInfo *msginfo, GSList *tags_set, GSList *tags_unset)
3934 Folder *folder = NULL;
3936 if (!msginfo)
3937 return;
3938 if (!item)
3939 return;
3940 if (!tags_set && !tags_unset)
3941 return;
3943 folder = item->folder;
3944 if (!folder)
3945 return;
3947 item->tags_dirty = TRUE;
3949 if (folder->klass->commit_tags == NULL)
3950 return;
3952 folder->klass->commit_tags(item, msginfo, tags_set, tags_unset);
3955 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
3957 Folder *folder;
3959 cm_return_val_if_fail(item != NULL, FALSE);
3960 if (item->no_select)
3961 return FALSE;
3963 folder = item->folder;
3965 cm_return_val_if_fail(folder->klass->is_msg_changed != NULL, -1);
3967 return folder->klass->is_msg_changed(folder, item, msginfo);
3970 void folder_item_discard_cache(FolderItem *item)
3972 gchar *dir;
3973 gchar *cache;
3975 if (!item)
3976 return;
3978 if (item->cache) {
3979 msgcache_destroy(item->cache);
3980 item->cache = NULL;
3982 dir = folder_item_get_path(item);
3983 if (is_dir_exist(dir))
3984 remove_all_numbered_files(dir);
3985 g_free(dir);
3987 cache = folder_item_get_cache_file(item);
3988 if (is_file_exist(cache))
3989 claws_unlink(cache);
3990 g_free(cache);
3994 static gchar *folder_item_get_cache_file(FolderItem *item)
3996 gchar *path;
3997 gchar *file;
3998 gchar *old_file;
4000 cm_return_val_if_fail(item != NULL, NULL);
4002 path = folder_item_get_path(item);
4003 cm_return_val_if_fail(path != NULL, NULL);
4004 if (!is_dir_exist(path))
4005 make_dir_hier(path);
4006 file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
4007 old_file = g_strconcat(path, G_DIR_SEPARATOR_S, OLD_CACHE_FILE, NULL);
4009 if (!is_file_exist(file) && is_file_exist(old_file))
4010 move_file(old_file, file, FALSE);
4011 g_free(old_file);
4012 g_free(path);
4014 return file;
4017 static gchar *folder_item_get_mark_file(FolderItem *item)
4019 gchar *path;
4020 gchar *file;
4021 gchar *old_file;
4023 cm_return_val_if_fail(item != NULL, NULL);
4024 cm_return_val_if_fail(item->path != NULL, NULL);
4026 path = folder_item_get_path(item);
4027 cm_return_val_if_fail(path != NULL, NULL);
4028 if (!is_dir_exist(path))
4029 make_dir_hier(path);
4030 file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
4031 old_file = g_strconcat(path, G_DIR_SEPARATOR_S, OLD_MARK_FILE, NULL);
4033 if (!is_file_exist(file) && is_file_exist(old_file))
4034 move_file(old_file, file, FALSE);
4035 g_free(old_file);
4036 g_free(path);
4038 return file;
4041 static gchar *folder_item_get_tags_file(FolderItem *item)
4043 gchar *path;
4044 gchar *identifier;
4045 gchar *file;
4047 /* we save tags files in rc_dir, because tagsrc is there too,
4048 * and storing tags directly in the mailboxes would give strange
4049 * result when using another Claws mailbox from another install
4050 * with different tags. */
4052 cm_return_val_if_fail(item != NULL, NULL);
4054 identifier = folder_item_get_identifier(item);
4055 cm_return_val_if_fail(identifier != NULL, NULL);
4057 #ifdef G_OS_WIN32
4058 while (strchr(identifier, '/'))
4059 *strchr(identifier, '/') = '\\';
4060 #endif
4062 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
4063 TAGS_DIR, G_DIR_SEPARATOR_S,
4064 identifier, NULL);
4066 g_free(identifier);
4068 if (!is_dir_exist(path))
4069 make_dir_hier(path);
4071 file = g_strconcat(path, G_DIR_SEPARATOR_S, TAGS_FILE, NULL);
4073 g_free(path);
4075 return file;
4078 static gpointer xml_to_folder_item(gpointer nodedata, gpointer data)
4080 XMLNode *xmlnode = (XMLNode *) nodedata;
4081 Folder *folder = (Folder *) data;
4082 FolderItem *item;
4084 cm_return_val_if_fail(xmlnode != NULL, NULL);
4085 cm_return_val_if_fail(folder != NULL, NULL);
4087 if (g_strcmp0(xmlnode->tag->tag, "folderitem") != 0) {
4088 g_warning("tag name != \"folderitem\"");
4089 return NULL;
4092 item = folder_item_new(folder, "", "");
4093 g_node_destroy(item->node);
4094 if (folder->klass->item_set_xml != NULL)
4095 folder->klass->item_set_xml(folder, item, xmlnode->tag);
4096 else
4097 folder_item_set_xml(folder, item, xmlnode->tag);
4099 item->folder = folder;
4101 switch (item->stype) {
4102 case F_INBOX: folder->inbox = item; break;
4103 case F_OUTBOX: folder->outbox = item; break;
4104 case F_DRAFT: folder->draft = item; break;
4105 case F_QUEUE: folder->queue = item; break;
4106 case F_TRASH: folder->trash = item; break;
4107 default: break;
4109 folder_item_prefs_read_config(item);
4111 return item;
4114 static gboolean folder_item_set_node(GNode *node, gpointer data)
4116 cm_return_val_if_fail(node->data != NULL, -1);
4118 FolderItem *item = (FolderItem *) node->data;
4119 item->node = node;
4121 return FALSE;
4124 static Folder *folder_get_from_xml(GNode *node)
4126 Folder *folder;
4127 XMLNode *xmlnode;
4128 GList *list;
4129 FolderClass *klass = NULL;
4130 GNode *cur;
4132 cm_return_val_if_fail(node->data != NULL, NULL);
4134 xmlnode = node->data;
4135 if (g_strcmp0(xmlnode->tag->tag, "folder") != 0) {
4136 g_warning("tag name != \"folder\"");
4137 return NULL;
4139 list = xmlnode->tag->attr;
4140 for (; list != NULL; list = list->next) {
4141 XMLAttr *attr = list->data;
4143 if (!attr || !attr->name || !attr->value) continue;
4144 if (!strcmp(attr->name, "type"))
4145 klass = folder_get_class_from_string(attr->value);
4147 if (klass == NULL)
4148 return NULL;
4150 folder = folder_new(klass, "", "");
4151 cm_return_val_if_fail(folder != NULL, NULL);
4153 if (klass->set_xml)
4154 klass->set_xml(folder, xmlnode->tag);
4155 else
4156 folder_set_xml(folder, xmlnode->tag);
4158 cur = node->children;
4159 while (cur != NULL) {
4160 GNode *itemnode;
4162 itemnode = g_node_map(cur, xml_to_folder_item, (gpointer) folder);
4163 g_node_append(folder->node, itemnode);
4164 cur = cur->next;
4166 g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, folder_item_set_node, NULL);
4168 return folder;
4171 gchar *folder_get_list_path(void)
4173 static gchar *filename = NULL;
4175 if (!filename)
4176 filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
4177 FOLDER_LIST, NULL);
4179 return filename;
4182 static gpointer folder_item_to_xml(gpointer nodedata, gpointer data)
4184 FolderItem *item = (FolderItem *) nodedata;
4185 XMLTag *tag;
4187 cm_return_val_if_fail(item != NULL, NULL);
4189 if (item->folder->klass->item_get_xml != NULL)
4190 tag = item->folder->klass->item_get_xml(item->folder, item);
4191 else
4192 tag = folder_item_get_xml(item->folder, item);
4194 return xml_node_new(tag, NULL);
4197 static GNode *folder_get_xml_node(Folder *folder)
4199 GNode *node;
4200 XMLNode *xmlnode;
4201 XMLTag *tag;
4203 cm_return_val_if_fail(folder != NULL, NULL);
4205 if (folder->klass->get_xml != NULL)
4206 tag = folder->klass->get_xml(folder);
4207 else
4208 tag = folder_get_xml(folder);
4210 xml_tag_add_attr(tag, xml_attr_new("type", folder->klass->idstr));
4212 xmlnode = xml_node_new(tag, NULL);
4214 node = g_node_new(xmlnode);
4216 cm_return_val_if_fail (folder->node != NULL, NULL);
4218 if (folder->node->children) {
4219 GNode *cur;
4221 cur = folder->node->children;
4222 while (cur) {
4223 GNode *xmlnode;
4225 xmlnode = g_node_map(cur, folder_item_to_xml, (gpointer) folder);
4226 g_node_append(node, xmlnode);
4227 cur = cur->next;
4231 return node;
4234 static void folder_update_op_count_rec(GNode *node)
4236 FolderItem *fitem = FOLDER_ITEM(node->data);
4238 if (g_node_depth(node) > 0) {
4239 if (fitem->op_count > 0) {
4240 fitem->op_count = 0;
4241 folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
4243 if (node->children) {
4244 GNode *child;
4246 child = node->children;
4247 while (child) {
4248 GNode *cur;
4250 cur = child;
4251 child = cur->next;
4252 folder_update_op_count_rec(cur);
4258 void folder_update_op_count(void)
4260 GList *cur;
4261 Folder *folder;
4263 for (cur = folder_list; cur != NULL; cur = cur->next) {
4264 folder = cur->data;
4265 folder_update_op_count_rec(folder->node);
4269 typedef struct _type_str {
4270 gchar * str;
4271 gint type;
4272 } type_str;
4276 static gchar * folder_item_get_tree_identifier(FolderItem * item)
4278 if (item->parent != NULL) {
4279 gchar * path;
4280 gchar * id;
4282 path = folder_item_get_tree_identifier(item->parent);
4283 if (path == NULL)
4284 return NULL;
4286 id = g_strconcat(path, "/", item->name, NULL);
4287 g_free(path);
4289 return id;
4291 else {
4292 return g_strconcat("/", item->name, NULL);
4297 /* CLAWS: temporary local folder for filtering */
4298 #define TEMP_FOLDER "TEMP_FOLDER"
4299 #define PROCESSING_FOLDER_ITEM "processing"
4301 static FolderItem *folder_create_processing_folder(int account_id)
4303 static Folder *processing_folder = NULL;
4304 FolderItem *processing_folder_item;
4306 gchar *processing_folder_item_name = NULL;
4308 processing_folder_item_name = g_strdup_printf("%s-%d", PROCESSING_FOLDER_ITEM, account_id);
4310 if (processing_folder == NULL) {
4311 gchar *tmppath =
4312 g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
4313 "tempfolder", NULL);
4314 processing_folder =
4315 folder_new(mh_get_class(), TEMP_FOLDER, tmppath);
4316 g_free(tmppath);
4317 tmppath = NULL;
4319 g_assert(processing_folder != NULL);
4320 processing_folder->klass->scan_tree(processing_folder);
4322 g_assert(processing_folder != NULL);
4324 processing_folder_item = folder_find_child_item_by_name(FOLDER_ITEM(processing_folder->node->data),
4325 processing_folder_item_name);
4326 if (processing_folder_item) {
4327 debug_print("*TMP* already created %s\n", folder_item_get_path(processing_folder_item));
4328 } else {
4329 processing_folder_item = processing_folder->klass->create_folder(processing_folder,
4330 processing_folder->node->data,
4331 processing_folder_item_name);
4332 folder_item_append(FOLDER_ITEM(processing_folder->node->data), processing_folder_item);
4333 debug_print("*TMP* creating %s\n", folder_item_get_path(processing_folder_item));
4335 g_free(processing_folder_item_name);
4336 g_assert(processing_folder_item != NULL);
4338 return(processing_folder_item);
4341 FolderItem *folder_get_default_processing(int account_id)
4343 return folder_create_processing_folder(account_id);
4346 /* folder_persist_prefs_new() - return hash table with persistent
4347 * settings (and folder name as key).
4348 * (note that in claws other options are in the folder_item_prefs_RC
4349 * file, so those don't need to be included in PersistPref yet)
4351 static GHashTable *folder_persist_prefs_new(Folder *folder)
4353 GHashTable *pptable;
4355 cm_return_val_if_fail(folder, NULL);
4356 pptable = g_hash_table_new(g_str_hash, g_str_equal);
4357 folder_get_persist_prefs_recursive(folder->node, pptable);
4358 return pptable;
4361 static void folder_persist_prefs_free(GHashTable *pptable)
4363 cm_return_if_fail(pptable);
4364 g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
4365 g_hash_table_destroy(pptable);
4368 static const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
4370 if (pptable == NULL || name == NULL) return NULL;
4371 return g_hash_table_lookup(pptable, name);
4374 static void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
4376 const PersistPrefs *pp;
4377 gchar *id = folder_item_get_identifier(item);
4379 pp = folder_get_persist_prefs(pptable, id);
4380 g_free(id);
4382 if (!pp) return;
4384 /* CLAWS: since not all folder properties have been migrated to
4385 * folderlist.xml, we need to call the old stuff first before
4386 * setting things that apply both to Main and Claws. */
4387 folder_item_prefs_read_config(item);
4389 item->collapsed = pp->collapsed;
4390 item->thread_collapsed = pp->thread_collapsed;
4391 item->threaded = pp->threaded;
4392 item->ret_rcpt = pp->ret_rcpt;
4393 item->hide_read_msgs = pp->hide_read_msgs;
4394 item->hide_del_msgs = pp->hide_del_msgs;
4395 item->hide_read_threads = pp->hide_read_threads;
4396 item->sort_key = pp->sort_key;
4397 item->sort_type = pp->sort_type;
4400 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
4402 FolderItem *item = FOLDER_ITEM(node->data);
4403 PersistPrefs *pp;
4404 GNode *child, *cur;
4405 gchar *id;
4407 cm_return_if_fail(node != NULL);
4408 cm_return_if_fail(item != NULL);
4410 /* NOTE: item->path == NULL means top level folder; not interesting
4411 * to store preferences of that one. */
4412 if (item->path) {
4413 id = folder_item_get_identifier(item);
4414 pp = g_new0(PersistPrefs, 1);
4415 if (!pp) {
4416 g_free(id);
4417 return;
4419 pp->collapsed = item->collapsed;
4420 pp->thread_collapsed = item->thread_collapsed;
4421 pp->threaded = item->threaded;
4422 pp->ret_rcpt = item->ret_rcpt;
4423 pp->hide_read_msgs = item->hide_read_msgs;
4424 pp->hide_del_msgs = item->hide_del_msgs;
4425 pp->hide_read_threads = item->hide_read_threads;
4426 pp->sort_key = item->sort_key;
4427 pp->sort_type = item->sort_type;
4428 g_hash_table_insert(pptable, id, pp);
4431 if (node->children) {
4432 child = node->children;
4433 while (child) {
4434 cur = child;
4435 child = cur->next;
4436 folder_get_persist_prefs_recursive(cur, pptable);
4441 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
4443 g_free(key);
4444 g_free(val);
4445 return TRUE;
4448 void folder_item_apply_processing(FolderItem *item)
4450 GSList *processing_list;
4451 GSList *mlist, *cur;
4452 guint total = 0, curmsg = 0;
4453 gint last_apply_per_account;
4455 cm_return_if_fail(item != NULL);
4457 if (item->no_select)
4458 return;
4460 processing_list = item->prefs->processing;
4462 if (!processing_enabled(pre_global_processing) &&
4463 !processing_enabled(processing_list) &&
4464 !processing_enabled(post_global_processing))
4465 return;
4467 debug_print("processing %s\n", item->name);
4468 folder_item_update_freeze();
4470 inc_lock();
4472 mlist = folder_item_get_msg_list(item);
4473 total = g_slist_length(mlist);
4474 statusbar_print_all(_("Processing messages..."));
4476 last_apply_per_account = prefs_common.apply_per_account_filtering_rules;
4477 prefs_common.apply_per_account_filtering_rules = FILTERING_ACCOUNT_RULES_SKIP;
4479 folder_item_set_batch(item, TRUE);
4480 for (cur = mlist ; cur != NULL ; cur = cur->next) {
4481 MsgInfo * msginfo;
4483 msginfo = (MsgInfo *) cur->data;
4485 /* reset parameters that can be modified by processing */
4486 msginfo->hidden = 0;
4487 msginfo->score = 0;
4489 statusbar_progress_all(curmsg++,total, 10);
4491 /* apply pre global rules */
4492 filter_message_by_msginfo(pre_global_processing, msginfo, NULL,
4493 FILTERING_PRE_PROCESSING, NULL);
4495 /* apply rules of the folder */
4496 filter_message_by_msginfo(processing_list, msginfo, NULL,
4497 FILTERING_FOLDER_PROCESSING, item->name);
4499 /* apply post global rules */
4500 filter_message_by_msginfo(post_global_processing, msginfo, NULL,
4501 FILTERING_POST_PROCESSING, NULL);
4502 if (curmsg % 1000 == 0)
4503 GTK_EVENTS_FLUSH();
4505 folder_item_set_batch(item, FALSE);
4507 prefs_common.apply_per_account_filtering_rules = last_apply_per_account;
4509 if (pre_global_processing || processing_list
4510 || post_global_processing)
4511 filtering_move_and_copy_msgs(mlist);
4512 for (cur = mlist ; cur != NULL ; cur = cur->next) {
4513 procmsg_msginfo_free((MsgInfo **)&(cur->data));
4515 g_slist_free(mlist);
4517 statusbar_progress_all(0,0,0);
4518 statusbar_pop_all();
4520 inc_unlock();
4522 folder_item_update_thaw();
4526 * functions for handling FolderItem content changes
4528 static gint folder_item_update_freeze_cnt = 0;
4530 static void folder_item_update_with_msg(FolderItem *item, FolderItemUpdateFlags update_flags, MsgInfo *msg)
4532 if (folder_item_update_freeze_cnt == 0 /* || (msg != NULL && item->opened) */) {
4533 FolderItemUpdateData source;
4535 source.item = item;
4536 source.update_flags = update_flags;
4537 source.msg = msg;
4538 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
4539 } else {
4540 item->update_flags |= update_flags & ~(F_ITEM_UPDATE_ADDMSG | F_ITEM_UPDATE_REMOVEMSG);
4545 * Notify the folder system about changes to a folder. If the
4546 * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
4547 * be invoked, otherwise the changes will be remebered until
4548 * the folder system is thawed.
4550 * \param item The FolderItem that was changed
4551 * \param update_flags Type of changed that was made
4553 void folder_item_update(FolderItem *item, FolderItemUpdateFlags update_flags)
4555 folder_item_update_with_msg(item, update_flags, NULL);
4558 void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
4560 GNode *node = item->folder->node;
4562 node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
4563 node = node->children;
4565 folder_item_update(item, update_flags);
4566 while (node != NULL) {
4567 if (node && node->data) {
4568 FolderItem *next_item = (FolderItem*) node->data;
4570 folder_item_update(next_item, update_flags);
4572 node = node->next;
4576 void folder_item_update_freeze(void)
4578 folder_item_update_freeze_cnt++;
4581 static void folder_item_update_func(FolderItem *item, gpointer data)
4583 FolderItemUpdateData source;
4585 if (item->update_flags) {
4586 source.item = item;
4587 source.update_flags = item->update_flags;
4588 source.msg = NULL;
4589 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
4590 item->update_flags = 0;
4594 void folder_item_update_thaw(void)
4596 if (folder_item_update_freeze_cnt > 0)
4597 folder_item_update_freeze_cnt--;
4598 if (folder_item_update_freeze_cnt == 0) {
4599 /* Update all folders */
4600 folder_func_to_all_folders(folder_item_update_func, NULL);
4604 void folder_item_synchronise(FolderItem *item)
4606 if (!item)
4607 return;
4608 if (item->prefs->offlinesync && item->folder->klass->synchronise) {
4609 statusbar_print_all(_("Synchronising %s for offline use...\n"), item->path ? item->path : "(null)");
4610 item->folder->klass->synchronise(item,
4611 item->prefs->offlinesync_days);
4612 if (item->prefs->offlinesync_days > 0 &&
4613 item->prefs->remove_old_bodies)
4614 folder_item_clean_local_files(item, item->prefs->offlinesync_days);
4615 statusbar_pop_all();
4619 static void folder_item_synchronise_func(FolderItem *item, gpointer data)
4621 Folder *folder = (Folder *)data;
4622 if (folder == NULL || item->folder == folder) {
4623 folder_item_synchronise(item);
4627 void folder_synchronise(Folder *folder)
4629 folder_func_to_all_folders(folder_item_synchronise_func, folder);
4632 typedef struct _WantSyncData {
4633 Folder *folder;
4634 gboolean want_sync;
4635 } WantSyncData;
4637 static void folder_item_want_synchronise_func(FolderItem *item, gpointer data)
4639 WantSyncData *want_sync_data = (WantSyncData *)data;
4641 if (want_sync_data->folder == NULL || item->folder == want_sync_data->folder) {
4642 if (item->prefs->offlinesync && item->folder->klass->synchronise)
4643 want_sync_data->want_sync |= TRUE;
4647 gboolean folder_want_synchronise(Folder *folder)
4649 WantSyncData *want_sync_data = g_new0(WantSyncData, 1);
4650 gboolean result;
4651 want_sync_data->folder = folder;
4652 want_sync_data->want_sync = FALSE;
4654 folder_func_to_all_folders(folder_item_want_synchronise_func, want_sync_data);
4655 result = want_sync_data->want_sync;
4656 g_free(want_sync_data);
4657 if (result > 0)
4658 debug_print("Folder %s wants sync\n", folder->name);
4659 return result;
4662 void folder_item_set_batch (FolderItem *item, gboolean batch)
4664 if (!item || !item->folder)
4665 return;
4666 if (item->folder->klass->set_batch) {
4667 item->folder->klass->set_batch(item->folder, item, batch);
4671 gboolean folder_has_parent_of_type(FolderItem *item,
4672 SpecialFolderItemType type)
4674 FolderItem *cur = item;
4676 if (!item)
4677 return FALSE;
4678 /* if we already know it, make it short */
4679 if (item->parent_stype != -1) {
4680 return (item->parent_stype == type);
4683 /* if we don't, find the type from the first possible parent,
4684 * and set our parent type to be faster next time */
4685 while (cur) {
4686 if (cur->stype == type || cur->parent_stype == type) {
4687 item->parent_stype = type;
4688 return TRUE;
4690 cur = folder_item_parent(cur);
4693 /* if we didn't match what was asked, we didn't return. If our
4694 * parent type is unknown, we may as well find it now to be faster
4695 * later. */
4696 if (item->parent_stype == -1) {
4697 cur = item;
4698 while (cur) {
4699 /* here's an exception: Inbox subfolders are normal. */
4700 if (item->parent_stype == -1 && cur->stype == F_INBOX
4701 && item != cur) {
4702 item->parent_stype = F_NORMAL;
4703 break;
4705 /* ah, we know this parent's parent's type, we may as
4706 * well copy it instead of going up the full way */
4707 if (cur->parent_stype != -1) {
4708 item->parent_stype = cur->parent_stype;
4709 break;
4711 /* we found a parent that has a special type. That's
4712 * our parent type. */
4713 if (cur->stype != F_NORMAL) {
4714 cur->parent_stype = cur->stype;
4715 item->parent_stype = cur->stype;
4716 break;
4718 /* if we didn't find anything, go up once more */
4719 cur = folder_item_parent(cur);
4721 /* as we still didn't find anything, our parents must all be
4722 * normal. */
4723 if (item->parent_stype == -1) {
4724 item->parent_stype = F_NORMAL;
4727 return FALSE;
4730 gboolean folder_is_child_of(FolderItem *item, FolderItem *parent)
4732 if (item == NULL || parent == NULL)
4733 return FALSE;
4735 while (item != NULL) {
4736 if (parent == item)
4737 return TRUE;
4739 item = folder_item_parent(item);
4742 return FALSE;
4746 gboolean folder_subscribe (const gchar *uri)
4748 GList *cur;
4749 for (cur = folder_get_list(); cur != NULL; cur = g_list_next(cur)) {
4750 Folder *folder = (Folder *) cur->data;
4752 if (folder->klass->subscribe
4753 && folder->klass->subscribe(folder, uri)) {
4754 return TRUE;
4757 return FALSE;
4761 gboolean folder_get_sort_type (Folder *folder,
4762 FolderSortKey *sort_key,
4763 FolderSortType *sort_type)
4765 if (!folder || !sort_key || !sort_type)
4766 return FALSE;
4767 if (folder->klass->get_sort_type == NULL)
4768 return FALSE;
4769 folder->klass->get_sort_type(folder, sort_key, sort_type);
4770 return TRUE;
4773 gint folder_item_search_msgs (Folder *folder,
4774 FolderItem *container,
4775 MsgNumberList **msgs,
4776 gboolean *on_server,
4777 MatcherList *predicate,
4778 SearchProgressNotify progress_cb,
4779 gpointer progress_data)
4781 gint result = -1;
4783 folder_item_update_freeze();
4785 if (folder->klass->search_msgs)
4786 result = folder->klass->search_msgs(folder, container,
4787 msgs, on_server, predicate, progress_cb, progress_data);
4788 if (result < 0)
4789 result = folder_item_search_msgs_local(folder, container,
4790 msgs, on_server, predicate, progress_cb, progress_data);
4792 folder_item_update_thaw();
4794 return result;
4797 MsgNumberList *folder_item_get_number_list(FolderItem *item)
4799 GSList *nums = NULL;
4800 GSList *msglist = folder_item_get_msg_list(item);
4802 nums = procmsg_get_number_list_for_msgs(msglist);
4803 procmsg_msg_list_free(msglist);
4805 return nums;
4808 gint folder_item_search_msgs_local (Folder *folder,
4809 FolderItem *container,
4810 MsgNumberList **msgs,
4811 gboolean *on_server,
4812 MatcherList *predicate,
4813 SearchProgressNotify progress_cb,
4814 gpointer progress_data)
4816 GSList *result = NULL;
4817 GSList *cur = NULL;
4818 gint matched_count = 0;
4819 guint processed_count = 0;
4820 gint msgcount;
4821 GSList *nums = NULL;
4823 if (*msgs == NULL) {
4824 nums = folder_item_get_number_list(container);
4825 } else {
4826 nums = *msgs;
4829 msgcount = g_slist_length(nums);
4831 if (msgcount < 0)
4832 return -1;
4834 for (cur = nums; cur != NULL; cur = cur->next) {
4835 guint msgnum = GPOINTER_TO_UINT(cur->data);
4836 MsgInfo *msg = folder_item_get_msginfo(container, msgnum);
4838 if (msg == NULL) {
4839 g_slist_free(result);
4840 return -1;
4843 if (matcherlist_match(predicate, msg)) {
4844 result = g_slist_prepend(result, GUINT_TO_POINTER(msg->msgnum));
4845 matched_count++;
4847 processed_count++;
4849 procmsg_msginfo_free(&msg);
4851 if (progress_cb != NULL
4852 && !progress_cb(progress_data, FALSE, processed_count,
4853 matched_count, msgcount))
4854 break;
4857 g_slist_free(nums);
4858 *msgs = g_slist_reverse(result);
4860 return matched_count;
4863 /* Tests if a local (on disk) folder name is acceptable. */
4864 gboolean folder_local_name_ok(const gchar *name)
4866 #ifdef G_OS_WIN32
4867 if (name[0] == '.' || name[strlen(name) - 1] == '.') {
4868 alertpanel_error(_("A folder name cannot begin or end with a dot."));
4869 return FALSE;
4871 if (name[strlen(name) - 1] == ' ') {
4872 alertpanel_error(_("A folder name can not end with a space."));
4873 return FALSE;
4875 #endif
4877 return TRUE;