2004-11-09 [colin] 0.9.12cvs144.1
[claws.git] / src / folder.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33
34 #include "intl.h"
35 #include "folder.h"
36 #include "session.h"
37 #include "imap.h"
38 #include "news.h"
39 #include "mh.h"
40 #include "utils.h"
41 #include "xml.h"
42 #include "codeconv.h"
43 #include "prefs_gtk.h"
44 #include "account.h"
45 #include "filtering.h"
46 #include "procheader.h"
47 #include "hooks.h"
48 #include "log.h"
49 #include "folder_item_prefs.h"
50 #include "remotefolder.h"
51 #include "partial_download.h"
52
53 /* Dependecies to be removed ?! */
54 #include "prefs_common.h"
55 #include "prefs_account.h"
56
57 static GList *folder_list = NULL;
58 static GSList *class_list = NULL;
59 static GSList *folder_unloaded_list = NULL;
60
61 void folder_init                (Folder         *folder,
62                                  const gchar    *name);
63
64 static gchar *folder_get_list_path      (void);
65 static GNode *folder_get_xml_node       (Folder         *folder);
66 static Folder *folder_get_from_xml      (GNode          *node);
67 static void folder_update_op_count_rec  (GNode          *node);
68
69
70 static void folder_get_persist_prefs_recursive
71                                         (GNode *node, GHashTable *pptable);
72 static gboolean persist_prefs_free      (gpointer key, gpointer val, gpointer data);
73 void folder_item_read_cache             (FolderItem *item);
74 void folder_item_free_cache             (FolderItem *item);
75 gint folder_item_scan_full              (FolderItem *item, gboolean filtering);
76 static void folder_item_update_with_msg (FolderItem *item, FolderItemUpdateFlags update_flags,
77                                          MsgInfo *msg);
78
79 void folder_system_init(void)
80 {
81         folder_register_class(mh_get_class());
82         folder_register_class(imap_get_class());
83         folder_register_class(news_get_class());
84 }
85
86 GSList *folder_get_class_list(void)
87 {
88         return class_list;
89 }
90
91 void folder_register_class(FolderClass *klass)
92 {
93         GSList *xmllist, *cur;
94
95         debug_print("registering folder class %s\n", klass->idstr);
96
97         class_list = g_slist_append(class_list, klass);
98
99         xmllist = g_slist_copy(folder_unloaded_list);
100         for (cur = xmllist; cur != NULL; cur = g_slist_next(cur)) {
101                 GNode *node = (GNode *) cur->data;
102                 XMLNode *xmlnode = (XMLNode *) node->data;
103                 GList *cur = xmlnode->tag->attr;
104
105                 for (; cur != NULL; cur = g_list_next(cur)) {
106                         XMLAttr *attr = (XMLAttr *) cur->data;
107
108                         if (!attr || !attr->name || !attr->value) continue;
109                         if (!strcmp(attr->name, "type") && !strcmp(attr->value, klass->idstr)) {
110                                 Folder *folder;
111
112                                 folder = folder_get_from_xml(node);
113                                 folder_add(folder);
114                                 folder_unloaded_list = g_slist_remove(folder_unloaded_list, node);
115
116                                 cur = NULL;
117                                 continue;
118                         }
119                 }
120         }
121         g_slist_free(xmllist);
122 }
123
124 void folder_unregister_class(FolderClass *klass)
125 {
126         GList *folderlist, *cur;
127
128         debug_print("unregistering folder class %s\n", klass->idstr);
129
130         class_list = g_slist_remove(class_list, klass);
131
132         folderlist = g_list_copy(folder_get_list());
133         for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
134                 Folder *folder = (Folder *) cur->data;
135
136                 if (folder->klass == klass) {
137                         GNode *xmlnode = folder_get_xml_node(folder);
138                         folder_unloaded_list = g_slist_append(folder_unloaded_list, xmlnode);
139                         folder_destroy(folder);
140                 }
141         }
142         g_list_free(folderlist);
143 }
144
145 Folder *folder_new(FolderClass *klass, const gchar *name, const gchar *path)
146 {
147         Folder *folder = NULL;
148         FolderItem *item;
149
150         g_return_val_if_fail(klass != NULL, NULL);
151
152         name = name ? name : path;
153         folder = klass->new_folder(name, path);
154
155         /* Create root folder item */
156         item = folder_item_new(folder, name, NULL);
157         item->folder = folder;
158         folder->node = item->node = g_node_new(item);
159         folder->data = NULL;
160
161         return folder;
162 }
163
164 void folder_init(Folder *folder, const gchar *name)
165 {
166         g_return_if_fail(folder != NULL);
167
168         folder_set_name(folder, name);
169
170         /* Init folder data */
171         folder->account = NULL;
172         folder->sort = 0;
173         folder->inbox = NULL;
174         folder->outbox = NULL;
175         folder->draft = NULL;
176         folder->queue = NULL;
177         folder->trash = NULL;
178 }
179
180 void folder_destroy(Folder *folder)
181 {
182         g_return_if_fail(folder != NULL);
183         g_return_if_fail(folder->klass->destroy_folder != NULL);
184
185         folder_remove(folder);
186
187         folder_tree_destroy(folder);
188
189         folder->klass->destroy_folder(folder);
190
191         g_free(folder->name);
192         g_free(folder);
193 }
194
195 void folder_set_xml(Folder *folder, XMLTag *tag)
196 {
197         GList *cur;
198         FolderItem *rootitem = NULL;
199
200         if ((folder->node != NULL) && (folder->node->data != NULL))
201                 rootitem = (FolderItem *) folder->node->data;
202
203         for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
204                 XMLAttr *attr = (XMLAttr *) cur->data;
205
206                 if (!attr || !attr->name || !attr->value) continue;
207                 if (!strcmp(attr->name, "name")) {
208                         if (folder->name != NULL)
209                                 g_free(folder->name);
210                         folder->name = g_strdup(attr->value);
211                         if (rootitem != NULL) {
212                                 if (rootitem->name != NULL)
213                                         g_free(rootitem->name);
214                                 rootitem->name = g_strdup(attr->value);
215                         }
216                 } else if (!strcmp(attr->name, "account_id")) {
217                         PrefsAccount *account;
218
219                         account = account_find_from_id(atoi(attr->value));
220                         if (!account)
221                                 g_warning("account_id: %s not found\n", attr->value);
222                         else {
223                                 folder->account = account;
224                                 account->folder = folder;
225                         }
226                 } else if (!strcmp(attr->name, "collapsed")) {
227                         if (rootitem != NULL)
228                                 rootitem->collapsed = *attr->value == '1' ? TRUE : FALSE;
229                 } else if (!strcmp(attr->name, "sort")) {
230                         folder->sort = atoi(attr->value);
231                 }
232         }
233 }
234
235 XMLTag *folder_get_xml(Folder *folder)
236 {
237         XMLTag *tag;
238
239         tag = xml_tag_new("folder");
240
241         if (folder->name)
242                 xml_tag_add_attr(tag, xml_attr_new("name", folder->name));
243         if (folder->account)
244                 xml_tag_add_attr(tag, xml_attr_new_int("account_id", folder->account->account_id));
245         if (folder->node && folder->node->data) {
246                 FolderItem *rootitem = (FolderItem *) folder->node->data;
247
248                 xml_tag_add_attr(tag, xml_attr_new("collapsed", rootitem->collapsed ? "1" : "0"));
249         }
250         xml_tag_add_attr(tag, xml_attr_new_int("sort", folder->sort));
251
252         return tag;
253 }
254
255 FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path)
256 {
257         FolderItem *item = NULL;
258
259         if (folder->klass->item_new) {
260                 item = folder->klass->item_new(folder);
261         } else {
262                 item = g_new0(FolderItem, 1);
263         }
264
265         g_return_val_if_fail(item != NULL, NULL);
266
267         item->stype = F_NORMAL;
268         item->name = g_strdup(name);
269         item->path = g_strdup(path);
270         item->mtime = 0;
271         item->new_msgs = 0;
272         item->unread_msgs = 0;
273         item->unreadmarked_msgs = 0;
274         item->total_msgs = 0;
275         item->last_num = -1;
276         item->cache = NULL;
277         item->no_sub = FALSE;
278         item->no_select = FALSE;
279         item->collapsed = FALSE;
280         item->thread_collapsed = FALSE;
281         item->threaded  = TRUE;
282         item->ret_rcpt  = FALSE;
283         item->opened    = FALSE;
284         item->node = g_node_new(item);
285         item->folder = NULL;
286         item->account = NULL;
287         item->apply_sub = FALSE;
288         item->mark_queue = NULL;
289         item->data = NULL;
290
291         item->prefs = folder_item_prefs_new();
292
293         return item;
294 }
295
296 void folder_item_append(FolderItem *parent, FolderItem *item)
297 {
298         g_return_if_fail(parent != NULL);
299         g_return_if_fail(parent->folder != NULL);
300         g_return_if_fail(parent->node != NULL);
301         g_return_if_fail(item != NULL);
302
303         item->folder = parent->folder;
304         g_node_append(parent->node, item->node);
305 }
306
307 static gboolean folder_item_remove_func(GNode *node, gpointer data)
308 {
309         FolderItem *item = FOLDER_ITEM(node->data);
310         FolderUpdateData hookdata;
311
312         if (item->cache != NULL) {
313                 msgcache_destroy(item->cache);
314                 item->cache = NULL;
315         }
316
317         hookdata.folder = item->folder;
318         hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_REMOVE_FOLDERITEM;
319         hookdata.item = item;
320         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
321
322         folder_item_destroy(item);
323
324         return FALSE;
325 }
326
327 void folder_item_remove(FolderItem *item)
328 {
329         GNode *node;
330
331         g_return_if_fail(item != NULL);
332         g_return_if_fail(item->folder != NULL);
333         g_return_if_fail(item->node != NULL);
334
335         node = item->node;
336
337         if (item->folder->node == node)
338                 item->folder->node = NULL;
339
340         g_node_traverse(node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
341                         folder_item_remove_func, NULL);
342         g_node_destroy(node);
343 }
344
345 void folder_item_remove_children(FolderItem *item)
346 {
347         GNode *node, *next;
348
349         g_return_if_fail(item != NULL);
350         g_return_if_fail(item->folder != NULL);
351         g_return_if_fail(item->node != NULL);
352
353         node = item->node->children;
354         while (node != NULL) {
355                 next = node->next;
356                 folder_item_remove(FOLDER_ITEM(node->data));
357                 node = next;
358         }
359 }
360
361 void folder_item_destroy(FolderItem *item)
362 {
363         Folder *folder;
364
365         g_return_if_fail(item != NULL);
366
367         folder = item->folder;
368         if (folder) {
369                 if (folder->inbox == item)
370                         folder->inbox = NULL;
371                 else if (folder->outbox == item)
372                         folder->outbox = NULL;
373                 else if (folder->draft == item)
374                         folder->draft = NULL;
375                 else if (folder->queue == item)
376                         folder->queue = NULL;
377                 else if (folder->trash == item)
378                         folder->trash = NULL;
379         }
380
381         if (item->cache)
382                 folder_item_free_cache(item);
383         if (item->prefs)
384                 folder_item_prefs_free(item->prefs);
385         g_free(item->name);
386         g_free(item->path);
387
388         if (item->folder != NULL) {
389                 if(item->folder->klass->item_destroy) {
390                         item->folder->klass->item_destroy(item->folder, item);
391                 } else {
392                         g_free(item);
393                 }
394         }
395 }
396
397 FolderItem *folder_item_parent(FolderItem *item)
398 {
399         g_return_val_if_fail(item != NULL, NULL);
400         g_return_val_if_fail(item->node != NULL, NULL);
401
402         if (item->node->parent == NULL)
403                 return NULL;
404         return (FolderItem *) item->node->parent->data;
405 }
406
407 void folder_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
408 {
409         GList *cur;
410
411         for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
412                 XMLAttr *attr = (XMLAttr *) cur->data;
413
414                 if (!attr || !attr->name || !attr->value) continue;
415                 if (!strcmp(attr->name, "type")) {
416                         if (!g_ascii_strcasecmp(attr->value, "normal"))
417                                 item->stype = F_NORMAL;
418                         else if (!g_ascii_strcasecmp(attr->value, "inbox"))
419                                 item->stype = F_INBOX;
420                         else if (!g_ascii_strcasecmp(attr->value, "outbox"))
421                                 item->stype = F_OUTBOX;
422                         else if (!g_ascii_strcasecmp(attr->value, "draft"))
423                                 item->stype = F_DRAFT;
424                         else if (!g_ascii_strcasecmp(attr->value, "queue"))
425                                 item->stype = F_QUEUE;
426                         else if (!g_ascii_strcasecmp(attr->value, "trash"))
427                                 item->stype = F_TRASH;
428                 } else if (!strcmp(attr->name, "name")) {
429                         if (item->name != NULL)
430                                 g_free(item->name);
431                         item->name = g_strdup(attr->value);
432                 } else if (!strcmp(attr->name, "path")) {
433                         if (item->path != NULL)
434                                 g_free(item->path);
435                         item->path = g_strdup(attr->value);
436                 } else if (!strcmp(attr->name, "mtime"))
437                         item->mtime = strtoul(attr->value, NULL, 10);
438                 else if (!strcmp(attr->name, "new"))
439                         item->new_msgs = atoi(attr->value);
440                 else if (!strcmp(attr->name, "unread"))
441                         item->unread_msgs = atoi(attr->value);
442                 else if (!strcmp(attr->name, "unreadmarked"))
443                         item->unreadmarked_msgs = atoi(attr->value);
444                 else if (!strcmp(attr->name, "total"))
445                         item->total_msgs = atoi(attr->value);
446                 else if (!strcmp(attr->name, "no_sub"))
447                         item->no_sub = *attr->value == '1' ? TRUE : FALSE;
448                 else if (!strcmp(attr->name, "no_select"))
449                         item->no_select = *attr->value == '1' ? TRUE : FALSE;
450                 else if (!strcmp(attr->name, "collapsed"))
451                         item->collapsed = *attr->value == '1' ? TRUE : FALSE;
452                 else if (!strcmp(attr->name, "thread_collapsed"))
453                         item->thread_collapsed =  *attr->value == '1' ? TRUE : FALSE;
454                 else if (!strcmp(attr->name, "threaded"))
455                         item->threaded =  *attr->value == '1' ? TRUE : FALSE;
456                 else if (!strcmp(attr->name, "hidereadmsgs"))
457                         item->hide_read_msgs =  *attr->value == '1' ? TRUE : FALSE;
458                 else if (!strcmp(attr->name, "reqretrcpt"))
459                         item->ret_rcpt =  *attr->value == '1' ? TRUE : FALSE;
460                 else if (!strcmp(attr->name, "sort_key")) {
461                         if (!strcmp(attr->value, "none"))
462                                 item->sort_key = SORT_BY_NONE;
463                         else if (!strcmp(attr->value, "number"))
464                                 item->sort_key = SORT_BY_NUMBER;
465                         else if (!strcmp(attr->value, "size"))
466                                 item->sort_key = SORT_BY_SIZE;
467                         else if (!strcmp(attr->value, "date"))
468                                 item->sort_key = SORT_BY_DATE;
469                         else if (!strcmp(attr->value, "from"))
470                                 item->sort_key = SORT_BY_FROM;
471                         else if (!strcmp(attr->value, "subject"))
472                                 item->sort_key = SORT_BY_SUBJECT;
473                         else if (!strcmp(attr->value, "score"))
474                                 item->sort_key = SORT_BY_SCORE;
475                         else if (!strcmp(attr->value, "label"))
476                                 item->sort_key = SORT_BY_LABEL;
477                         else if (!strcmp(attr->value, "mark"))
478                                 item->sort_key = SORT_BY_MARK;
479                         else if (!strcmp(attr->value, "unread"))
480                                 item->sort_key = SORT_BY_STATUS;
481                         else if (!strcmp(attr->value, "mime"))
482                                 item->sort_key = SORT_BY_MIME;
483                         else if (!strcmp(attr->value, "to"))
484                                 item->sort_key = SORT_BY_TO;
485                         else if (!strcmp(attr->value, "locked"))
486                                 item->sort_key = SORT_BY_LOCKED;
487                 } else if (!strcmp(attr->name, "sort_type")) {
488                         if (!strcmp(attr->value, "ascending"))
489                                 item->sort_type = SORT_ASCENDING;
490                         else
491                                 item->sort_type = SORT_DESCENDING;
492                 } else if (!strcmp(attr->name, "account_id")) {
493                         PrefsAccount *account;
494
495                         account = account_find_from_id(atoi(attr->value));
496                         if (!account)
497                                 g_warning("account_id: %s not found\n", attr->value);
498                         else
499                                 item->account = account;
500                 } else if (!strcmp(attr->name, "apply_sub"))
501                         item->apply_sub = *attr->value == '1' ? TRUE : FALSE;
502         }
503 }
504
505 XMLTag *folder_item_get_xml(Folder *folder, FolderItem *item)
506 {
507         static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
508                                                  "draft", "queue", "trash"};
509         static gchar *sort_key_str[] = {"none", "number", "size", "date",
510                                         "from", "subject", "score", "label",
511                                         "mark", "unread", "mime", "to", 
512                                         "locked"};
513         XMLTag *tag;
514         gchar *value;
515
516         tag = xml_tag_new("folderitem");
517
518         xml_tag_add_attr(tag, xml_attr_new("type", folder_item_stype_str[item->stype]));
519         if (item->name)
520                 xml_tag_add_attr(tag, xml_attr_new("name", item->name));
521         if (item->path)
522                 xml_tag_add_attr(tag, xml_attr_new("path", item->path));
523         if (item->no_sub)
524                 xml_tag_add_attr(tag, xml_attr_new("no_sub", "1"));
525         if (item->no_select)
526                 xml_tag_add_attr(tag, xml_attr_new("no_select", "1"));
527         xml_tag_add_attr(tag, xml_attr_new("collapsed", item->collapsed && item->node->children ? "1" : "0"));
528         xml_tag_add_attr(tag, xml_attr_new("thread_collapsed", item->thread_collapsed ? "1" : "0"));
529         xml_tag_add_attr(tag, xml_attr_new("threaded", item->threaded ? "1" : "0"));
530         xml_tag_add_attr(tag, xml_attr_new("hidereadmsgs", item->hide_read_msgs ? "1" : "0"));
531         if (item->ret_rcpt)
532                 xml_tag_add_attr(tag, xml_attr_new("reqretrcpt", "1"));
533
534         if (item->sort_key != SORT_BY_NONE) {
535                 xml_tag_add_attr(tag, xml_attr_new("sort_key", sort_key_str[item->sort_key]));
536                 xml_tag_add_attr(tag, xml_attr_new("sort_type", item->sort_type == SORT_ASCENDING ? "ascending" : "descending"));
537         }
538
539         value = g_strdup_printf("%ld", (unsigned long int) item->mtime);
540         xml_tag_add_attr(tag, xml_attr_new("mtime", value));
541         g_free(value);
542         xml_tag_add_attr(tag, xml_attr_new_int("new", item->new_msgs));
543         xml_tag_add_attr(tag, xml_attr_new_int("unread", item->unread_msgs));
544         xml_tag_add_attr(tag, xml_attr_new_int("unreadmarked", item->unreadmarked_msgs));
545         xml_tag_add_attr(tag, xml_attr_new_int("total", item->total_msgs));
546
547         if (item->account)
548                 xml_tag_add_attr(tag, xml_attr_new_int("account_id", item->account->account_id));
549         if (item->apply_sub)
550                 xml_tag_add_attr(tag, xml_attr_new("apply_sub", "1"));
551
552         return tag;
553 }
554
555 void folder_set_ui_func(Folder *folder, FolderUIFunc func, gpointer data)
556 {
557         g_return_if_fail(folder != NULL);
558
559         folder->ui_func = func;
560         folder->ui_func_data = data;
561 }
562
563 void folder_set_name(Folder *folder, const gchar *name)
564 {
565         g_return_if_fail(folder != NULL);
566
567         g_free(folder->name);
568         folder->name = name ? g_strdup(name) : NULL;
569         if (folder->node && folder->node->data) {
570                 FolderItem *item = (FolderItem *)folder->node->data;
571
572                 g_free(item->name);
573                 item->name = name ? g_strdup(name) : NULL;
574         }
575 }
576
577 void folder_set_sort(Folder *folder, guint sort)
578 {
579         g_return_if_fail(folder != NULL);
580
581         if (folder->sort != sort) {
582                 folder_remove(folder);
583                 folder->sort = sort;
584                 folder_add(folder);
585         }
586 }
587
588 gboolean folder_tree_destroy_func(GNode *node, gpointer data) {
589         FolderItem *item = (FolderItem *) node->data;
590
591         folder_item_destroy(item);
592         return FALSE;
593 }
594
595 void folder_tree_destroy(Folder *folder)
596 {
597         GNode *node;
598
599         g_return_if_fail(folder != NULL);
600
601         node = folder->node;
602         
603         prefs_filtering_clear_folder(folder);
604
605         if (node != NULL) {
606                 g_node_traverse(node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
607                                 folder_tree_destroy_func, NULL);
608                 g_node_destroy(node);
609                 folder->node = NULL;
610         }
611 }
612
613 void folder_add(Folder *folder)
614 {
615         Folder *cur_folder;
616         GList *cur;
617         gint i;
618         FolderUpdateData hookdata;
619
620         g_return_if_fail(folder != NULL);
621
622         for (i = 0, cur = folder_list; cur != NULL; cur = cur->next, i++) {
623                 cur_folder = FOLDER(cur->data);
624                 if (cur_folder->sort < folder->sort)
625                         break;
626         }
627
628         folder_list = g_list_insert(folder_list, folder, i);
629
630         hookdata.folder = folder;
631         hookdata.update_flags = FOLDER_ADD_FOLDER;
632         hookdata.item = NULL;
633         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
634 }
635
636 void folder_remove(Folder *folder)
637 {
638         FolderUpdateData hookdata;
639
640         g_return_if_fail(folder != NULL);
641
642         folder_list = g_list_remove(folder_list, folder);
643
644         hookdata.folder = folder;
645         hookdata.update_flags = FOLDER_REMOVE_FOLDER;
646         hookdata.item = NULL;
647         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
648 }
649
650 GList *folder_get_list(void)
651 {
652         return folder_list;
653 }
654
655 gint folder_read_list(void)
656 {
657         GNode *node, *cur;
658         XMLNode *xmlnode;
659         gchar *path;
660
661         path = folder_get_list_path();
662         if (!is_file_exist(path)) return -1;
663         node = xml_parse_file(path);
664         if (!node) return -1;
665
666         xmlnode = node->data;
667         if (strcmp2(xmlnode->tag->tag, "folderlist") != 0) {
668                 g_warning("wrong folder list\n");
669                 xml_free_tree(node);
670                 return -1;
671         }
672
673         cur = node->children;
674         while (cur != NULL) {
675                 Folder *folder;
676
677                 folder = folder_get_from_xml(cur);
678                 if (folder != NULL)
679                         folder_add(folder);
680                 else
681                         folder_unloaded_list = g_slist_append(folder_unloaded_list,
682                                 (gpointer) xml_copy_tree(cur));
683                 cur = cur->next;
684         }
685
686         xml_free_tree(node);
687         if (folder_list)
688                 return 0;
689         else
690                 return -1;
691 }
692
693 void folder_write_list(void)
694 {
695         GList *list;
696         GSList *slist;
697         Folder *folder;
698         gchar *path;
699         PrefFile *pfile;
700         GNode *rootnode;
701         XMLNode *xmlnode;
702         XMLTag *tag;
703
704         path = folder_get_list_path();
705         if ((pfile = prefs_write_open(path)) == NULL) return;
706
707         xml_file_put_xml_decl(pfile->fp);
708         tag = xml_tag_new("folderlist");
709
710         xmlnode = xml_node_new(tag, NULL);
711
712         rootnode = g_node_new(xmlnode);
713
714         for (list = folder_list; list != NULL; list = list->next) {
715                 GNode *node;
716
717                 folder = list->data;
718                 node = folder_get_xml_node(folder);
719                 if (node != NULL)
720                         g_node_append(rootnode, node);
721         }
722
723         for (slist = folder_unloaded_list; slist != NULL; slist = g_slist_next(slist)) {
724                 GNode *node = (GNode *) slist->data;
725
726                 g_node_append(rootnode, (gpointer) xml_copy_tree(node));
727         }
728
729         xml_write_tree(rootnode, pfile->fp);
730
731         if (prefs_file_close(pfile) < 0)
732                 g_warning("failed to write folder list.\n");
733
734         xml_free_tree(rootnode);
735 }
736
737 gboolean folder_scan_tree_func(GNode *node, gpointer data)
738 {
739         GHashTable *pptable = (GHashTable *)data;
740         FolderItem *item = (FolderItem *)node->data;
741         
742         folder_item_restore_persist_prefs(item, pptable);
743         folder_item_scan_full(item, FALSE);
744
745         return FALSE;
746 }
747
748 void folder_scan_tree(Folder *folder)
749 {
750         GHashTable *pptable;
751         FolderUpdateData hookdata;
752         
753         if (!folder->klass->scan_tree)
754                 return;
755         
756         pptable = folder_persist_prefs_new(folder);
757
758         /*
759          * should be changed and tree update should be done without 
760          * destroying the tree first
761          */
762         folder_tree_destroy(folder);
763         folder->klass->scan_tree(folder);
764
765         hookdata.folder = folder;
766         hookdata.update_flags = FOLDER_TREE_CHANGED;
767         hookdata.item = NULL;
768         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
769
770         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_scan_tree_func, pptable);
771         folder_persist_prefs_free(pptable);
772
773         prefs_matcher_read_config();
774
775         folder_write_list();
776 }
777
778 FolderItem *folder_create_folder(FolderItem *parent, const gchar *name)
779 {
780         FolderItem *new_item;
781
782         new_item = parent->folder->klass->create_folder(parent->folder, parent, name);
783         if (new_item) {
784                 FolderUpdateData hookdata;
785
786                 new_item->cache = msgcache_new();
787
788                 hookdata.folder = new_item->folder;
789                 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDERITEM;
790                 hookdata.item = new_item;
791                 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
792         }
793
794         return new_item;
795 }
796
797 gint folder_item_rename(FolderItem *item, gchar *newname)
798 {
799         gint retval;
800
801         g_return_val_if_fail(item != NULL, -1);
802         g_return_val_if_fail(newname != NULL, -1);
803
804         retval = item->folder->klass->rename_folder(item->folder, item, newname);
805
806         if (retval >= 0) {
807                 FolderItemUpdateData hookdata;
808
809                 hookdata.item = item;
810                 hookdata.update_flags = F_ITEM_UPDATE_NAME;
811                 hookdata.msg = NULL;
812                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &hookdata);
813         }
814
815         return retval;
816 }
817
818 struct TotalMsgCount
819 {
820         guint new_msgs;
821         guint unread_msgs;
822         guint unreadmarked_msgs;
823         guint total_msgs;
824 };
825
826 struct FuncToAllFoldersData
827 {
828         FolderItemFunc  function;
829         gpointer        data;
830 };
831
832 static gboolean folder_func_to_all_folders_func(GNode *node, gpointer data)
833 {
834         FolderItem *item;
835         struct FuncToAllFoldersData *function_data = (struct FuncToAllFoldersData *) data;
836
837         g_return_val_if_fail(node->data != NULL, FALSE);
838
839         item = FOLDER_ITEM(node->data);
840         g_return_val_if_fail(item != NULL, FALSE);
841
842         function_data->function(item, function_data->data);
843
844         return FALSE;
845 }
846
847 void folder_func_to_all_folders(FolderItemFunc function, gpointer data)
848 {
849         GList *list;
850         Folder *folder;
851         struct FuncToAllFoldersData function_data;
852         
853         function_data.function = function;
854         function_data.data = data;
855
856         for (list = folder_list; list != NULL; list = list->next) {
857                 folder = FOLDER(list->data);
858                 if (folder->node)
859                         g_node_traverse(folder->node, G_PRE_ORDER,
860                                         G_TRAVERSE_ALL, -1,
861                                         folder_func_to_all_folders_func,
862                                         &function_data);
863         }
864 }
865
866 static void folder_count_total_msgs_func(FolderItem *item, gpointer data)
867 {
868         struct TotalMsgCount *count = (struct TotalMsgCount *)data;
869
870         count->new_msgs += item->new_msgs;
871         count->unread_msgs += item->unread_msgs;
872         count->unreadmarked_msgs += item->unreadmarked_msgs;
873         count->total_msgs += item->total_msgs;
874 }
875
876 struct TotalMsgStatus
877 {
878         guint new;
879         guint unread;
880         guint total;
881         GString *str;
882 };
883
884 static gboolean folder_get_status_full_all_func(GNode *node, gpointer data)
885 {
886         FolderItem *item;
887         struct TotalMsgStatus *status = (struct TotalMsgStatus *)data;
888         gchar *id;
889  
890         g_return_val_if_fail(node->data != NULL, FALSE);
891  
892         item = FOLDER_ITEM(node->data);
893
894         if (!item->path) return FALSE;
895
896         status->new += item->new_msgs;
897         status->unread += item->unread_msgs;
898         status->total += item->total_msgs;
899
900         if (status->str) {
901                 id = folder_item_get_identifier(item);
902                 g_string_append_printf(status->str, "%5d %5d %5d %s\n",
903                                   item->new_msgs, item->unread_msgs,
904                                   item->total_msgs, id);
905                 g_free(id);
906         }
907  
908         return FALSE;
909  }
910  
911 static void folder_get_status_full_all(GString *str, guint *new, guint *unread,
912                                        guint *total)
913 {
914         GList *list;
915         Folder *folder;
916         struct TotalMsgStatus status;
917  
918         status.new = status.unread = status.total = 0;
919         status.str = str;
920  
921         debug_print("Counting total number of messages...\n");
922  
923         for (list = folder_list; list != NULL; list = list->next) {
924                 folder = FOLDER(list->data);
925                 if (folder->node)
926                         g_node_traverse(folder->node, G_PRE_ORDER,
927                                         G_TRAVERSE_ALL, -1,
928                                         folder_get_status_full_all_func,
929                                         &status);
930         }
931  
932         *new = status.new;
933         *unread = status.unread;
934         *total = status.total;
935 }
936
937 gchar *folder_get_status(GPtrArray *folders, gboolean full)
938 {
939         guint new, unread, total;
940         GString *str;
941         gint i;
942         gchar *ret;
943
944         new = unread = total = 0;
945
946         str = g_string_new(NULL);
947
948         if (folders) {
949                 for (i = 0; i < folders->len; i++) {
950                         FolderItem *item;
951
952                         item = g_ptr_array_index(folders, i);
953                         new += item->new_msgs;
954                         unread += item->unread_msgs;
955                         total += item->total_msgs;
956
957                         if (full) {
958                                 gchar *id;
959
960                                 id = folder_item_get_identifier(item);
961                                 g_string_append_printf(str, "%5d %5d %5d %s\n",
962                                                   item->new_msgs, item->unread_msgs,
963                                                   item->total_msgs, id);
964                                 g_free(id);
965                         }
966                 }
967         } else {
968                 folder_get_status_full_all(full ? str : NULL,
969                                            &new, &unread, &total);
970         }
971
972         if (full)
973                 g_string_append_printf(str, "%5d %5d %5d\n", new, unread, total);
974         else
975                 g_string_append_printf(str, "%d %d %d\n", new, unread, total);
976
977         ret = str->str;
978         g_string_free(str, FALSE);
979  
980         return ret;
981 }
982
983 void folder_count_total_msgs(guint *new_msgs, guint *unread_msgs, guint *unreadmarked_msgs, guint *total_msgs)
984 {
985         struct TotalMsgCount count;
986
987         count.new_msgs = count.unread_msgs = count.unreadmarked_msgs = count.total_msgs = 0;
988
989         debug_print("Counting total number of messages...\n");
990
991         folder_func_to_all_folders(folder_count_total_msgs_func, &count);
992
993         *new_msgs = count.new_msgs;
994         *unread_msgs = count.unread_msgs;
995         *unreadmarked_msgs = count.unreadmarked_msgs;
996         *total_msgs = count.total_msgs;
997 }
998
999 Folder *folder_find_from_path(const gchar *path)
1000 {
1001         GList *list;
1002         Folder *folder;
1003
1004         for (list = folder_list; list != NULL; list = list->next) {
1005                 folder = list->data;
1006                 if ((FOLDER_TYPE(folder) == F_MH || 
1007                      FOLDER_TYPE(folder) == F_MBOX) &&
1008                     !path_cmp(LOCAL_FOLDER(folder)->rootpath, path))
1009                         return folder;
1010         }
1011
1012         return NULL;
1013 }
1014
1015 Folder *folder_find_from_name(const gchar *name, FolderClass *klass)
1016 {
1017         GList *list;
1018         Folder *folder;
1019
1020         for (list = folder_list; list != NULL; list = list->next) {
1021                 folder = list->data;
1022                 if (folder->klass == klass && 
1023                     strcmp2(name, folder->name) == 0)
1024                         return folder;
1025         }
1026
1027         return NULL;
1028 }
1029
1030 static gboolean folder_item_find_func(GNode *node, gpointer data)
1031 {
1032         FolderItem *item = node->data;
1033         gpointer *d = data;
1034         const gchar *path = d[0];
1035
1036         if (path_cmp(path, item->path) != 0)
1037                 return FALSE;
1038
1039         d[1] = item;
1040
1041         return TRUE;
1042 }
1043
1044 FolderItem *folder_find_item_from_path(const gchar *path)
1045 {
1046         Folder *folder;
1047         gpointer d[2];
1048
1049         folder = folder_get_default_folder();
1050         g_return_val_if_fail(folder != NULL, NULL);
1051
1052         d[0] = (gpointer)path;
1053         d[1] = NULL;
1054         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1055                         folder_item_find_func, d);
1056         return d[1];
1057 }
1058
1059 FolderItem *folder_find_child_item_by_name(FolderItem *item, const gchar *name)
1060 {
1061         GNode *node;
1062         FolderItem *child;
1063
1064         for (node = item->node->children; node != NULL; node = node->next) {
1065                 child = FOLDER_ITEM(node->data);
1066                 if (strcmp2(g_basename(child->path), name) == 0)
1067                         return child;
1068         }
1069
1070         return NULL;
1071 }
1072
1073 FolderClass *folder_get_class_from_string(const gchar *str)
1074 {
1075         GSList *classlist;
1076
1077         classlist = folder_get_class_list();
1078         for (; classlist != NULL; classlist = g_slist_next(classlist)) {
1079                 FolderClass *class = (FolderClass *) classlist->data;
1080                 if (g_ascii_strcasecmp(class->idstr, str) == 0)
1081                         return class;
1082         }
1083
1084         return NULL;
1085 }
1086
1087 gchar *folder_get_identifier(Folder *folder)
1088 {
1089         gchar *type_str;
1090
1091         g_return_val_if_fail(folder != NULL, NULL);
1092
1093         type_str = folder->klass->idstr;
1094         return g_strconcat("#", type_str, "/", folder->name, NULL);
1095 }
1096
1097 gchar *folder_item_get_identifier(FolderItem *item)
1098 {
1099         gchar *id = NULL;
1100         gchar *folder_id = NULL;
1101
1102         g_return_val_if_fail(item != NULL, NULL);
1103         g_return_val_if_fail(item->path != NULL, NULL);
1104
1105         folder_id = folder_get_identifier(item->folder);
1106         id = g_strconcat(folder_id, "/", item->path, NULL);
1107         g_free(folder_id);
1108
1109         return id;
1110 }
1111
1112 FolderItem *folder_find_item_from_identifier(const gchar *identifier)
1113 {
1114         Folder *folder;
1115         gpointer d[2];
1116         gchar *str;
1117         gchar *p;
1118         gchar *name;
1119         gchar *path;
1120         FolderClass *class;
1121
1122         g_return_val_if_fail(identifier != NULL, NULL);
1123
1124         if (*identifier != '#')
1125                 return folder_find_item_from_path(identifier);
1126
1127         Xstrdup_a(str, identifier, return NULL);
1128
1129         p = strchr(str, '/');
1130         if (!p)
1131                 return folder_find_item_from_path(identifier);
1132         *p = '\0';
1133         p++;
1134         class = folder_get_class_from_string(&str[1]);
1135         if (class == NULL)
1136                 return folder_find_item_from_path(identifier);
1137
1138         name = p;
1139         p = strchr(p, '/');
1140         if (!p)
1141                 return folder_find_item_from_path(identifier);
1142         *p = '\0';
1143         p++;
1144
1145         folder = folder_find_from_name(name, class);
1146         if (!folder)
1147                 return folder_find_item_from_path(identifier);
1148
1149         path = p;
1150
1151         d[0] = (gpointer)path;
1152         d[1] = NULL;
1153         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1154                         folder_item_find_func, d);
1155         return d[1];
1156 }
1157
1158 /**
1159  * Get a displayable name for a FolderItem
1160  *
1161  * \param item FolderItem for that a name should be created
1162  * \return Displayable name for item, returned string has to
1163  *         be freed
1164  */
1165 gchar *folder_item_get_name(FolderItem *item)
1166 {
1167         gchar *name = NULL;
1168
1169         g_return_val_if_fail(item != NULL, g_strdup(""));
1170
1171         switch (item->stype) {
1172         case F_INBOX:
1173                 name = g_strdup(!strcmp2(item->name, INBOX_DIR) ? _("Inbox") :
1174                                 item->name);
1175                 break;
1176         case F_OUTBOX:
1177                 name = g_strdup(!strcmp2(item->name, OUTBOX_DIR) ? _("Sent") :
1178                                 item->name);
1179                 break;
1180         case F_QUEUE:
1181                 name = g_strdup(!strcmp2(item->name, QUEUE_DIR) ? _("Queue") :
1182                                 item->name);
1183                 break;
1184         case F_TRASH:
1185                 name = g_strdup(!strcmp2(item->name, TRASH_DIR) ? _("Trash") :
1186                                 item->name);
1187                 break;
1188         case F_DRAFT:
1189                 name = g_strdup(!strcmp2(item->name, DRAFT_DIR) ? _("Drafts") :
1190                                 item->name);
1191                 break;
1192         default:
1193                 break;
1194         }
1195
1196         if (name == NULL) {
1197                 /*
1198                  * should probably be done by a virtual function,
1199                  * the folder knows the ui string and how to abbrev
1200                 */
1201                 if (folder_item_parent(item) == NULL) {
1202                         name = g_strconcat(item->name, " (", item->folder->klass->uistr, ")", NULL);
1203                 } else {
1204                         if (FOLDER_CLASS(item->folder) == news_get_class() &&
1205                             item->path && !strcmp2(item->name, item->path))
1206                                 name = get_abbrev_newsgroup_name
1207                                         (item->path,
1208                                          prefs_common.ng_abbrev_len);
1209                         else
1210                                 name = g_strdup(item->name);
1211                 }
1212         }
1213
1214         if (name == NULL)
1215                 name = g_strdup("");
1216
1217         return name;
1218 }
1219
1220 Folder *folder_get_default_folder(void)
1221 {
1222         return folder_list ? FOLDER(folder_list->data) : NULL;
1223 }
1224
1225 FolderItem *folder_get_default_inbox(void)
1226 {
1227         GList *flist;
1228
1229         for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1230                 Folder * folder = FOLDER(flist->data);
1231
1232                 if (folder == NULL)
1233                         continue;
1234                 if (folder->inbox == NULL)
1235                         continue;
1236
1237                 return folder->inbox;
1238         }
1239
1240         return NULL;
1241 }
1242
1243 FolderItem *folder_get_default_outbox(void)
1244 {
1245         GList *flist;
1246
1247         for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1248                 Folder * folder = FOLDER(flist->data);
1249
1250                 if (folder == NULL)
1251                         continue;
1252                 if (folder->outbox == NULL)
1253                         continue;
1254
1255                 return folder->outbox;
1256         }
1257
1258         return NULL;
1259 }
1260
1261 FolderItem *folder_get_default_draft(void)
1262 {
1263         GList *flist;
1264
1265         for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1266                 Folder * folder = FOLDER(flist->data);
1267
1268                 if (folder == NULL)
1269                         continue;
1270                 if (folder->draft == NULL)
1271                         continue;
1272
1273                 return folder->draft;
1274         }
1275
1276         return NULL;
1277 }
1278
1279 FolderItem *folder_get_default_queue(void)
1280 {
1281         GList *flist;
1282
1283         for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1284                 Folder * folder = FOLDER(flist->data);
1285
1286                 if (folder == NULL)
1287                         continue;
1288                 if (folder->queue == NULL)
1289                         continue;
1290
1291                 return folder->queue;
1292         }
1293
1294         return NULL;
1295 }
1296
1297 FolderItem *folder_get_default_trash(void)
1298 {
1299         GList *flist;
1300
1301         for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1302                 Folder * folder = FOLDER(flist->data);
1303
1304                 if (folder == NULL)
1305                         continue;
1306                 if (folder->trash == NULL)
1307                         continue;
1308
1309                 return folder->trash;
1310         }
1311
1312         return NULL;
1313 }
1314
1315 #define CREATE_FOLDER_IF_NOT_EXIST(member, dir, type)           \
1316 {                                                               \
1317         if (!folder->member) {                                  \
1318                 item = folder_item_new(folder, dir, dir);       \
1319                 item->stype = type;                             \
1320                 folder_item_append(rootitem, item);             \
1321                 folder->member = item;                          \
1322         }                                                       \
1323 }
1324
1325 void folder_set_missing_folders(void)
1326 {
1327         Folder *folder;
1328         FolderItem *rootitem;
1329         FolderItem *item;
1330         GList *list;
1331
1332         for (list = folder_list; list != NULL; list = list->next) {
1333                 folder = list->data;
1334                 if (FOLDER_TYPE(folder) != F_MH) continue;
1335                 rootitem = FOLDER_ITEM(folder->node->data);
1336                 g_return_if_fail(rootitem != NULL);
1337
1338                 if (folder->inbox && folder->outbox && folder->draft &&
1339                     folder->queue && folder->trash)
1340                         continue;
1341
1342                 if (folder->klass->create_tree(folder) < 0) {
1343                         g_warning("%s: can't create the folder tree.\n",
1344                                   LOCAL_FOLDER(folder)->rootpath);
1345                         continue;
1346                 }
1347
1348                 CREATE_FOLDER_IF_NOT_EXIST(inbox,  INBOX_DIR,  F_INBOX);
1349                 CREATE_FOLDER_IF_NOT_EXIST(outbox, OUTBOX_DIR, F_OUTBOX);
1350                 CREATE_FOLDER_IF_NOT_EXIST(draft,  DRAFT_DIR,  F_DRAFT);
1351                 CREATE_FOLDER_IF_NOT_EXIST(queue,  QUEUE_DIR,  F_QUEUE);
1352                 CREATE_FOLDER_IF_NOT_EXIST(trash,  TRASH_DIR,  F_TRASH);
1353         }
1354 }
1355
1356 static gboolean folder_unref_account_func(GNode *node, gpointer data)
1357 {
1358         FolderItem *item = node->data;
1359         PrefsAccount *account = data;
1360
1361         if (item->account == account)
1362                 item->account = NULL;
1363
1364         return FALSE;
1365 }
1366
1367 void folder_unref_account_all(PrefsAccount *account)
1368 {
1369         Folder *folder;
1370         GList *list;
1371
1372         if (!account) return;
1373
1374         for (list = folder_list; list != NULL; list = list->next) {
1375                 folder = list->data;
1376                 if (folder->account == account)
1377                         folder->account = NULL;
1378                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1379                                 folder_unref_account_func, account);
1380         }
1381 }
1382
1383 #undef CREATE_FOLDER_IF_NOT_EXIST
1384
1385 gchar *folder_item_get_path(FolderItem *item)
1386 {
1387         Folder *folder;
1388
1389         g_return_val_if_fail(item != NULL, NULL);
1390         folder = item->folder;
1391         g_return_val_if_fail(folder != NULL, NULL);
1392
1393         return folder->klass->item_get_path(folder, item);
1394 }
1395
1396 void folder_item_set_default_flags(FolderItem *dest, MsgFlags *flags)
1397 {
1398         if (!(dest->stype == F_OUTBOX ||
1399               dest->stype == F_QUEUE  ||
1400               dest->stype == F_DRAFT  ||
1401               dest->stype == F_TRASH)) {
1402                 flags->perm_flags = MSG_NEW|MSG_UNREAD;
1403         } else {
1404                 flags->perm_flags = 0;
1405         }
1406         if (FOLDER_TYPE(dest->folder) == F_MH) {
1407                 if (dest->stype == F_QUEUE) {
1408                         MSG_SET_TMP_FLAGS(*flags, MSG_QUEUED);
1409                 } else if (dest->stype == F_DRAFT) {
1410                         MSG_SET_TMP_FLAGS(*flags, MSG_DRAFT);
1411                 }
1412         }
1413 }
1414
1415 static gint folder_sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1416 {
1417         MsgInfo *msginfo_a = (MsgInfo *) a;
1418         MsgInfo *msginfo_b = (MsgInfo *) b;
1419
1420         return (msginfo_a->msgnum - msginfo_b->msgnum);
1421 }
1422
1423 static gint folder_sort_folder_list(gconstpointer a, gconstpointer b)
1424 {
1425         guint gint_a = GPOINTER_TO_INT(a);
1426         guint gint_b = GPOINTER_TO_INT(b);
1427         
1428         return (gint_a - gint_b);
1429 }
1430
1431 gint folder_item_open(FolderItem *item)
1432 {
1433         gchar *buf;
1434         if((item->folder->klass->scan_required != NULL) && (item->folder->klass->scan_required(item->folder, item))) {
1435                 folder_item_scan_full(item, TRUE);
1436         }
1437         folder_item_syncronize_flags(item);
1438         
1439         /* Processing */
1440         buf = g_strdup_printf(_("Processing (%s)...\n"), 
1441                               item->path ? item->path : item->name);
1442         debug_print("%s\n", buf);
1443         g_free(buf);
1444         
1445         folder_item_apply_processing(item);
1446
1447         item->opened = TRUE;
1448
1449         debug_print("done.\n");
1450
1451         return 0;
1452 }
1453
1454 gint folder_item_close(FolderItem *item)
1455 {
1456         GSList *mlist, *cur;
1457         Folder *folder;
1458         
1459         g_return_val_if_fail(item != NULL, -1);
1460
1461         if (item->new_msgs) {
1462                 folder_item_update_freeze();
1463                 mlist = folder_item_get_msg_list(item);
1464                 for (cur = mlist ; cur != NULL ; cur = cur->next) {
1465                         MsgInfo * msginfo;
1466
1467                         msginfo = (MsgInfo *) cur->data;
1468                         if (MSG_IS_NEW(msginfo->flags))
1469                                 procmsg_msginfo_unset_flags(msginfo, MSG_NEW, 0);
1470                         procmsg_msginfo_free(msginfo);
1471                 }
1472                 g_slist_free(mlist);
1473                 folder_item_update_thaw();
1474         }               
1475
1476         folder_item_write_cache(item);
1477         
1478         folder_item_update(item, F_ITEM_UPDATE_MSGCNT);
1479
1480         item->opened = FALSE;
1481         folder = item->folder;
1482
1483         if (folder->klass->close == NULL)
1484                 return 0;
1485
1486         return folder->klass->close(folder, item);
1487 }
1488
1489 static MsgInfoList *get_msginfos(FolderItem *item, MsgNumberList *numlist)
1490 {
1491         MsgInfoList *msglist = NULL;
1492         Folder *folder = item->folder;
1493
1494         if (folder->klass->get_msginfos != NULL)
1495                 msglist = folder->klass->get_msginfos(folder, item, numlist);
1496         else {
1497                 MsgNumberList *elem;
1498
1499                 for (elem = numlist; elem != NULL; elem = g_slist_next(elem)) {
1500                         MsgInfo *msginfo;
1501                         guint num;
1502
1503                         num = GPOINTER_TO_INT(elem->data);
1504                         msginfo = folder->klass->get_msginfo(folder, item, num);
1505                         if (msginfo != NULL)
1506                                 msglist = g_slist_prepend(msglist, msginfo);
1507                 }               
1508         }
1509
1510         return msglist;
1511 }
1512
1513 static MsgInfo *get_msginfo(FolderItem *item, guint num)
1514 {
1515         MsgNumberList numlist;
1516         MsgInfoList *msglist;
1517         MsgInfo *msginfo = NULL;
1518
1519         numlist.data = GINT_TO_POINTER(num);
1520         numlist.next = NULL;
1521         msglist = get_msginfos(item, &numlist);
1522         if (msglist != NULL)
1523                 msginfo = procmsg_msginfo_new_ref(msglist->data);
1524         procmsg_msg_list_free(msglist);
1525
1526         return msginfo;
1527 }
1528
1529 static gint syncronize_flags(FolderItem *item, MsgInfoList *msglist)
1530 {
1531         GRelation *relation;
1532         gint ret = 0;
1533         GSList *cur;
1534
1535         if(msglist == NULL)
1536                 return 0;
1537         if(item->folder->klass->get_flags == NULL)
1538                 return 0;
1539
1540         relation = g_relation_new(2);
1541         g_relation_index(relation, 0, g_direct_hash, g_direct_equal);
1542         if ((ret = item->folder->klass->get_flags(
1543             item->folder, item, msglist, relation)) == 0) {
1544                 GTuples *tuples;
1545                 MsgInfo *msginfo;
1546                 MsgPermFlags permflags;
1547                 gboolean skip;
1548
1549                 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1550                         msginfo = (MsgInfo *) cur->data;
1551                 
1552                         tuples = g_relation_select(relation, msginfo, 0);
1553                         skip = tuples->len < 1;
1554                         if (!skip)
1555                                 permflags = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1556                         g_tuples_destroy(tuples);
1557                         if (skip)
1558                                 continue;
1559                         
1560                         if (msginfo->flags.perm_flags != permflags) {
1561                                 procmsg_msginfo_set_flags(msginfo,
1562                                         permflags & ~msginfo->flags.perm_flags, 0);
1563                                 procmsg_msginfo_unset_flags(msginfo,
1564                                         ~permflags & msginfo->flags.perm_flags, 0);
1565                         }
1566                 }
1567         }
1568         g_relation_destroy(relation);   
1569
1570         return ret;
1571 }
1572
1573 gint folder_item_scan_full(FolderItem *item, gboolean filtering)
1574 {
1575         Folder *folder;
1576         GSList *folder_list = NULL, *cache_list = NULL;
1577         GSList *folder_list_cur, *cache_list_cur, *new_list = NULL;
1578         GSList *exists_list = NULL, *elem;
1579         GSList *newmsg_list = NULL;
1580         guint newcnt = 0, unreadcnt = 0, totalcnt = 0, unreadmarkedcnt = 0;
1581         guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
1582         gboolean update_flags = 0, old_uids_valid = FALSE;
1583     
1584         g_return_val_if_fail(item != NULL, -1);
1585         if (item->path == NULL) return -1;
1586
1587         folder = item->folder;
1588
1589         g_return_val_if_fail(folder != NULL, -1);
1590         g_return_val_if_fail(folder->klass->get_num_list != NULL, -1);
1591
1592         debug_print("Scanning folder %s for cache changes.\n", item->path);
1593
1594         /* Get list of messages for folder and cache */
1595         if (folder->klass->get_num_list(item->folder, item, &folder_list, &old_uids_valid) < 0) {
1596                 debug_print("Error fetching list of message numbers\n");
1597                 return(-1);
1598         }
1599
1600         if (old_uids_valid) {
1601                 if (!item->cache)
1602                         folder_item_read_cache(item);
1603                 cache_list = msgcache_get_msg_list(item->cache);
1604         } else {
1605                 if (item->cache)
1606                         msgcache_destroy(item->cache);
1607                 item->cache = msgcache_new();
1608                 cache_list = NULL;
1609         }
1610
1611         /* Sort both lists */
1612         cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
1613         folder_list = g_slist_sort(folder_list, folder_sort_folder_list);
1614
1615         cache_list_cur = cache_list;
1616         folder_list_cur = folder_list;
1617
1618         if (cache_list_cur != NULL) {
1619                 GSList *cache_list_last;
1620         
1621                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1622                 cache_list_last = g_slist_last(cache_list);
1623                 cache_max_num = ((MsgInfo *)cache_list_last->data)->msgnum;
1624         } else {
1625                 cache_cur_num = G_MAXINT;
1626                 cache_max_num = 0;
1627         }
1628
1629         if (folder_list_cur != NULL) {
1630                 GSList *folder_list_last;
1631         
1632                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1633                 folder_list_last = g_slist_last(folder_list);
1634                 folder_max_num = GPOINTER_TO_INT(folder_list_last->data);
1635         } else {
1636                 folder_cur_num = G_MAXINT;
1637                 folder_max_num = 0;
1638         }
1639
1640         while ((cache_cur_num != G_MAXINT) || (folder_cur_num != G_MAXINT)) {
1641                 /*
1642                  *  Message only exists in the folder
1643                  *  Remember message for fetching
1644                  */
1645                 if (folder_cur_num < cache_cur_num) {
1646                         gboolean add = FALSE;
1647
1648                         switch(FOLDER_TYPE(folder)) {
1649                                 case F_NEWS:
1650                                         if (folder_cur_num < cache_max_num)
1651                                                 break;
1652                                         
1653                                         if (folder->account->max_articles == 0) {
1654                                                 add = TRUE;
1655                                         }
1656
1657                                         if (folder_max_num <= folder->account->max_articles) {
1658                                                 add = TRUE;
1659                                         } else if (folder_cur_num > (folder_max_num - folder->account->max_articles)) {
1660                                                 add = TRUE;
1661                                         }
1662                                         break;
1663                                 default:
1664                                         add = TRUE;
1665                                         break;
1666                         }
1667                         
1668                         if (add) {
1669                                 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(folder_cur_num));
1670                                 debug_print("Remembered message %d for fetching\n", folder_cur_num);
1671                         }
1672
1673                         /* Move to next folder number */
1674                         folder_list_cur = folder_list_cur->next;
1675
1676                         if (folder_list_cur != NULL)
1677                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1678                         else
1679                                 folder_cur_num = G_MAXINT;
1680
1681                         continue;
1682                 }
1683
1684                 /*
1685                  *  Message only exists in the cache
1686                  *  Remove the message from the cache
1687                  */
1688                 if (cache_cur_num < folder_cur_num) {
1689                         msgcache_remove_msg(item->cache, cache_cur_num);
1690                         debug_print("Removed message %d from cache.\n", cache_cur_num);
1691
1692                         /* Move to next cache number */
1693                         cache_list_cur = cache_list_cur->next;
1694
1695                         if (cache_list_cur != NULL)
1696                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1697                         else
1698                                 cache_cur_num = G_MAXINT;
1699
1700                         update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1701
1702                         continue;
1703                 }
1704
1705                 /*
1706                  *  Message number exists in folder and cache!
1707                  *  Check if the message has been modified
1708                  */
1709                 if (cache_cur_num == folder_cur_num) {
1710                         MsgInfo *msginfo;
1711
1712                         msginfo = msgcache_get_msg(item->cache, folder_cur_num);
1713                         if (folder->klass->is_msg_changed && folder->klass->is_msg_changed(folder, item, msginfo)) {
1714                                 msgcache_remove_msg(item->cache, msginfo->msgnum);
1715                                 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(msginfo->msgnum));
1716                                 procmsg_msginfo_free(msginfo);
1717
1718                                 debug_print("Remembering message %d to update...\n", folder_cur_num);
1719                         } else
1720                                 exists_list = g_slist_prepend(exists_list, msginfo);
1721
1722                         /* Move to next folder and cache number */
1723                         cache_list_cur = cache_list_cur->next;
1724                         folder_list_cur = folder_list_cur->next;
1725
1726                         if (cache_list_cur != NULL)
1727                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1728                         else
1729                                 cache_cur_num = G_MAXINT;
1730
1731                         if (folder_list_cur != NULL)
1732                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1733                         else
1734                                 folder_cur_num = G_MAXINT;
1735
1736                         continue;
1737                 }
1738         }
1739         
1740         for(cache_list_cur = cache_list; cache_list_cur != NULL; cache_list_cur = g_slist_next(cache_list_cur))
1741                 procmsg_msginfo_free((MsgInfo *) cache_list_cur->data);
1742
1743         g_slist_free(cache_list);
1744         g_slist_free(folder_list);
1745
1746         if (new_list != NULL) {
1747                 newmsg_list = get_msginfos(item, new_list);
1748                 g_slist_free(new_list);
1749         }
1750
1751         syncronize_flags(item, exists_list);
1752
1753         folder_item_update_freeze();
1754         if (newmsg_list != NULL) {
1755                 GSList *elem;
1756
1757                 for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
1758                         MsgInfo *msginfo = (MsgInfo *) elem->data;
1759
1760                         msgcache_add_msg(item->cache, msginfo);
1761                         if ((filtering == TRUE) &&
1762                             (item->stype == F_INBOX) &&
1763                             (item->folder->account != NULL) && 
1764                             (item->folder->account->filter_on_recv) &&
1765                             procmsg_msginfo_filter(msginfo))
1766                                 procmsg_msginfo_free(msginfo);
1767                         else
1768                                 exists_list = g_slist_prepend(exists_list, msginfo);
1769                 }
1770                 g_slist_free(newmsg_list);
1771
1772                 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1773         }
1774
1775         for (elem = exists_list; elem != NULL; elem = g_slist_next(elem)) {
1776                 MsgInfo *msginfo;
1777
1778                 msginfo = elem->data;
1779                 if (MSG_IS_IGNORE_THREAD(msginfo->flags) && (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1780                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1781                 if (!MSG_IS_IGNORE_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD)) {
1782                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1783                         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
1784                 }
1785                 if ((item->stype == F_OUTBOX ||
1786                      item->stype == F_QUEUE  ||
1787                      item->stype == F_DRAFT  ||
1788                      item->stype == F_TRASH) &&
1789                     (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1790                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1791                 if (MSG_IS_NEW(msginfo->flags))
1792                         newcnt++;
1793                 if (MSG_IS_UNREAD(msginfo->flags))
1794                         unreadcnt++;
1795                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1796                         unreadmarkedcnt++;
1797                 totalcnt++;
1798
1799                 procmsg_msginfo_free(msginfo);
1800         }
1801         g_slist_free(exists_list);
1802
1803         item->new_msgs = newcnt;
1804         item->unread_msgs = unreadcnt;
1805         item->total_msgs = totalcnt;
1806         item->unreadmarked_msgs = unreadmarkedcnt;
1807
1808         update_flags |= F_ITEM_UPDATE_MSGCNT;
1809
1810         folder_item_update(item, update_flags);
1811         folder_item_update_thaw();
1812
1813         return 0;
1814 }
1815
1816 gint folder_item_scan(FolderItem *item)
1817 {
1818         return folder_item_scan_full(item, TRUE);
1819 }
1820
1821 static gboolean folder_scan_all_items_func(GNode *node, gpointer data)
1822 {
1823         FolderItem *item = node->data;
1824
1825         folder_item_scan(item);
1826
1827         return FALSE;
1828 }
1829
1830 void folder_scan_all_items(Folder * folder)
1831 {
1832         g_node_traverse(folder->node, G_PRE_ORDER,
1833                         G_TRAVERSE_ALL, -1, folder_scan_all_items_func, NULL);
1834 }
1835
1836 static void folder_item_scan_foreach_func(gpointer key, gpointer val,
1837                                           gpointer data)
1838 {
1839         folder_item_scan(FOLDER_ITEM(key));
1840 }
1841
1842 void folder_item_scan_foreach(GHashTable *table)
1843 {
1844         g_hash_table_foreach(table, folder_item_scan_foreach_func, NULL);
1845 }
1846
1847 void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
1848 {
1849         gint *memusage = (gint *)data;
1850
1851         if (item->cache == NULL)
1852                 return;
1853         
1854         *memusage += msgcache_get_memory_usage(item->cache);
1855 }
1856
1857 gint folder_item_syncronize_flags(FolderItem *item)
1858 {
1859         MsgInfoList *msglist = NULL;
1860         GSList *cur;
1861         gint ret = 0;
1862         
1863         g_return_val_if_fail(item != NULL, -1);
1864         g_return_val_if_fail(item->folder != NULL, -1);
1865         g_return_val_if_fail(item->folder->klass != NULL, -1);
1866         
1867         if (item->cache == NULL)
1868                 folder_item_read_cache(item);
1869         
1870         msglist = msgcache_get_msg_list(item->cache);
1871         
1872         ret = syncronize_flags(item, msglist);
1873
1874         for (cur = msglist; cur != NULL; cur = g_slist_next(cur))
1875                 procmsg_msginfo_free((MsgInfo *) cur->data);
1876         
1877         return ret;
1878 }
1879
1880 gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
1881 {
1882         FolderItem *fa = (FolderItem *)a;
1883         FolderItem *fb = (FolderItem *)b;
1884         
1885         return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
1886 }
1887
1888 void folder_find_expired_caches(FolderItem *item, gpointer data)
1889 {
1890         GSList **folder_item_list = (GSList **)data;
1891         gint difftime, expiretime;
1892         
1893         if (item->cache == NULL)
1894                 return;
1895
1896         if (item->opened > 0)
1897                 return;
1898
1899         difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
1900         expiretime = prefs_common.cache_min_keep_time * 60;
1901         debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
1902         if (difftime > expiretime) {
1903                 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
1904         }
1905 }
1906
1907 void folder_item_free_cache(FolderItem *item)
1908 {
1909         g_return_if_fail(item != NULL);
1910         
1911         if (item->cache == NULL)
1912                 return;
1913         
1914         if (item->opened > 0)
1915                 return;
1916
1917         folder_item_write_cache(item);
1918         msgcache_destroy(item->cache);
1919         item->cache = NULL;
1920 }
1921
1922 void folder_clean_cache_memory(void)
1923 {
1924         gint memusage = 0;
1925
1926         folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);       
1927         debug_print("Total cache memory usage: %d\n", memusage);
1928         
1929         if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
1930                 GSList *folder_item_list = NULL, *listitem;
1931                 
1932                 debug_print("Trying to free cache memory\n");
1933
1934                 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);      
1935                 listitem = folder_item_list;
1936                 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
1937                         FolderItem *item = (FolderItem *)(listitem->data);
1938
1939                         debug_print("Freeing cache memory for %s\n", item->path);
1940                         memusage -= msgcache_get_memory_usage(item->cache);
1941                         folder_item_free_cache(item);
1942                         listitem = listitem->next;
1943                 }
1944                 g_slist_free(folder_item_list);
1945         }
1946 }
1947
1948 void folder_item_read_cache(FolderItem *item)
1949 {
1950         gchar *cache_file, *mark_file;
1951         
1952         g_return_if_fail(item != NULL);
1953
1954         if (item->path != NULL) {
1955                 cache_file = folder_item_get_cache_file(item);
1956                 mark_file = folder_item_get_mark_file(item);
1957                 item->cache = msgcache_read_cache(item, cache_file);
1958                 if (!item->cache) {
1959                         MsgInfoList *list, *cur;
1960                         guint newcnt = 0, unreadcnt = 0, unreadmarkedcnt = 0;
1961                         MsgInfo *msginfo;
1962
1963                         item->cache = msgcache_new();
1964                         folder_item_scan_full(item, TRUE);
1965
1966                         msgcache_read_mark(item->cache, mark_file);
1967
1968                         list = msgcache_get_msg_list(item->cache);
1969                         for (cur = list; cur != NULL; cur = g_slist_next(cur)) {
1970                                 msginfo = cur->data;
1971
1972                                 if (MSG_IS_NEW(msginfo->flags))
1973                                         newcnt++;
1974                                 if (MSG_IS_UNREAD(msginfo->flags))
1975                                         unreadcnt++;
1976                                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1977                                         unreadmarkedcnt++;
1978                         }
1979                         item->new_msgs = newcnt;
1980                         item->unread_msgs = unreadcnt;
1981                         item->unreadmarked_msgs = unreadmarkedcnt;
1982                         procmsg_msg_list_free(list);
1983                 } else
1984                         msgcache_read_mark(item->cache, mark_file);
1985
1986                 g_free(cache_file);
1987                 g_free(mark_file);
1988         } else {
1989                 item->cache = msgcache_new();
1990         }
1991
1992         folder_clean_cache_memory();
1993 }
1994
1995 void folder_item_write_cache(FolderItem *item)
1996 {
1997         gchar *cache_file, *mark_file;
1998         FolderItemPrefs *prefs;
1999         gint filemode = 0;
2000         gchar *id;
2001         
2002         if (!item || !item->path || !item->cache)
2003                 return;
2004
2005         id = folder_item_get_identifier(item);
2006         debug_print("Save cache for folder %s\n", id);
2007         g_free(id);
2008
2009         cache_file = folder_item_get_cache_file(item);
2010         mark_file = folder_item_get_mark_file(item);
2011         if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
2012                 prefs = item->prefs;
2013                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
2014                         /* for cache file */
2015                         filemode = prefs->folder_chmod;
2016                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
2017                         if (filemode & S_IROTH) filemode |= S_IWOTH;
2018                         chmod(cache_file, filemode);
2019                 }
2020         }
2021
2022         g_free(cache_file);
2023         g_free(mark_file);
2024 }
2025
2026 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
2027 {
2028         Folder *folder;
2029         MsgInfo *msginfo = NULL;
2030         
2031         g_return_val_if_fail(item != NULL, NULL);
2032         
2033         folder = item->folder;
2034         if (!item->cache)
2035                 folder_item_read_cache(item);
2036         
2037         if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
2038                 return msginfo;
2039         
2040         msginfo = get_msginfo(item, num);
2041         if (msginfo != NULL) {
2042                 msgcache_add_msg(item->cache, msginfo);
2043                 return msginfo;
2044         }
2045         
2046         return NULL;
2047 }
2048
2049 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
2050 {
2051         Folder *folder;
2052         MsgInfo *msginfo;
2053         
2054         g_return_val_if_fail(item != NULL, NULL);
2055         g_return_val_if_fail(msgid != NULL, NULL);
2056         
2057         folder = item->folder;
2058         if (!item->cache)
2059                 folder_item_read_cache(item);
2060         
2061         if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
2062                 return msginfo;
2063
2064         return NULL;
2065 }
2066
2067 GSList *folder_item_get_msg_list(FolderItem *item)
2068 {
2069         g_return_val_if_fail(item != NULL, NULL);
2070         
2071         if (item->cache == 0)
2072                 folder_item_read_cache(item);
2073
2074         g_return_val_if_fail(item->cache != NULL, NULL);
2075         
2076         return msgcache_get_msg_list(item->cache);
2077 }
2078
2079 static void msginfo_set_mime_flags(GNode *node, gpointer data)
2080 {
2081         MsgInfo *msginfo = data;
2082         MimeInfo *mimeinfo = node->data;
2083
2084         if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT) {
2085                 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2086         } else if (mimeinfo->disposition == DISPOSITIONTYPE_UNKNOWN && 
2087                  mimeinfo->type != MIMETYPE_TEXT &&
2088                  mimeinfo->type != MIMETYPE_MULTIPART) {
2089                 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2090         }
2091
2092         /* don't descend below top level message for signed and encrypted info */
2093         if (mimeinfo->type == MIMETYPE_MESSAGE)
2094                 return;
2095
2096         if (privacy_mimeinfo_is_signed(mimeinfo)) {
2097                 procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
2098         }
2099
2100         if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
2101                 procmsg_msginfo_set_flags(msginfo, 0, MSG_ENCRYPTED);
2102         } else {
2103                 /* searching inside encrypted parts doesn't really make sense */
2104                 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2105         }
2106 }
2107
2108 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
2109 {
2110         Folder *folder;
2111         gchar *msgfile;
2112         MsgInfo *msginfo;
2113
2114         g_return_val_if_fail(item != NULL, NULL);
2115
2116         folder = item->folder;
2117
2118         g_return_val_if_fail(folder->klass->fetch_msg != NULL, NULL);
2119
2120         msgfile = folder->klass->fetch_msg(folder, item, num);
2121
2122         if (msgfile != NULL) {
2123                 msginfo = folder_item_get_msginfo(item, num);
2124                 if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
2125                         MimeInfo *mimeinfo;
2126
2127                         if (msginfo->folder->stype != F_QUEUE && 
2128                             msginfo->folder->stype != F_DRAFT)
2129                                 mimeinfo = procmime_scan_file(msgfile);
2130                         else
2131                                 mimeinfo = procmime_scan_queue_file(msgfile);
2132                         /* check for attachments */
2133                         if (mimeinfo != NULL) { 
2134                                 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2135                                 procmime_mimeinfo_free_all(mimeinfo);
2136
2137                                 procmsg_msginfo_set_flags(msginfo, 0, MSG_SCANNED);
2138                         }
2139                 }
2140         }
2141
2142         return msgfile;
2143 }
2144
2145 gint folder_item_fetch_all_msg(FolderItem *item)
2146 {
2147         Folder *folder;
2148         GSList *mlist;
2149         GSList *cur;
2150         gint num = 0;
2151         gint ret = 0;
2152
2153         g_return_val_if_fail(item != NULL, -1);
2154
2155         debug_print("fetching all messages in %s ...\n", item->path);
2156
2157         folder = item->folder;
2158
2159         if (folder->ui_func)
2160                 folder->ui_func(folder, item, folder->ui_func_data ?
2161                                 folder->ui_func_data : GINT_TO_POINTER(num));
2162
2163         mlist = folder_item_get_msg_list(item);
2164
2165         for (cur = mlist; cur != NULL; cur = cur->next) {
2166                 MsgInfo *msginfo = (MsgInfo *)cur->data;
2167                 gchar *msg;
2168
2169                 num++;
2170                 if (folder->ui_func)
2171                         folder->ui_func(folder, item,
2172                                         folder->ui_func_data ?
2173                                         folder->ui_func_data :
2174                                         GINT_TO_POINTER(num));
2175
2176                 msg = folder_item_fetch_msg(item, msginfo->msgnum);
2177                 if (!msg) {
2178                         g_warning("Can't fetch message %d. Aborting.\n",
2179                                   msginfo->msgnum);
2180                         ret = -1;
2181                         break;
2182                 }
2183                 g_free(msg);
2184         }
2185
2186         procmsg_msg_list_free(mlist);
2187
2188         return ret;
2189 }
2190
2191 static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
2192 {
2193         static HeaderEntry hentry[] = {{"Message-ID:",  NULL, TRUE},
2194                                        {NULL,           NULL, FALSE}};
2195         FILE *fp;
2196         MsgInfo *msginfo;
2197         gint msgnum = 0;
2198         gchar buf[BUFFSIZE];
2199
2200         if ((fp = fopen(file, "rb")) == NULL)
2201                 return 0;
2202
2203         if ((dest->stype == F_QUEUE) || (dest->stype == F_DRAFT))
2204                 while (fgets(buf, sizeof(buf), fp) != NULL)
2205                         if (buf[0] == '\r' || buf[0] == '\n') break;
2206
2207         procheader_get_header_fields(fp, hentry);
2208         if (hentry[0].body) {
2209                 extract_parenthesis(hentry[0].body, '<', '>');
2210                 remove_space(hentry[0].body);
2211                 if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
2212                         msgnum = msginfo->msgnum;
2213                         procmsg_msginfo_free(msginfo);
2214
2215                         debug_print("found message as uid %d\n", msgnum);
2216                 }
2217         }
2218         
2219         g_free(hentry[0].body);
2220         hentry[0].body = NULL;
2221         fclose(fp);
2222
2223         return msgnum;
2224 }
2225
2226 static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
2227 {
2228         MsgPermFlags perm_flags = 0;
2229         MsgTmpFlags tmp_flags = 0;
2230
2231         /* create new flags */
2232         if (source != NULL) {
2233                 /* copy original flags */
2234                 perm_flags = source->flags.perm_flags;
2235                 tmp_flags = source->flags.tmp_flags;
2236         } else {
2237                 perm_flags = dest->flags.perm_flags;
2238                 tmp_flags = dest->flags.tmp_flags;
2239         }
2240
2241         /* remove new, unread and deleted in special folders */
2242         if (dest->folder->stype == F_OUTBOX ||
2243             dest->folder->stype == F_QUEUE  ||
2244             dest->folder->stype == F_DRAFT  ||
2245             dest->folder->stype == F_TRASH)
2246                 perm_flags &= ~(MSG_NEW | MSG_UNREAD | MSG_DELETED);
2247
2248         /* set ignore flag of ignored parent exists */
2249         if (procmsg_msg_has_flagged_parent(dest, MSG_IGNORE_THREAD))
2250                 perm_flags |= MSG_IGNORE_THREAD;
2251
2252         /* Unset tmp flags that should not be copied */
2253         tmp_flags &= ~(MSG_MOVE | MSG_COPY);
2254
2255         /* unset flags that are set but should not */
2256         procmsg_msginfo_unset_flags(dest,
2257                                     dest->flags.perm_flags & ~perm_flags,
2258                                     dest->flags.tmp_flags  & ~tmp_flags);
2259         /* set new flags */
2260         procmsg_msginfo_set_flags(dest,
2261                                   ~dest->flags.perm_flags & perm_flags,
2262                                   ~dest->flags.tmp_flags  & tmp_flags);
2263 }
2264
2265 static void add_msginfo_to_cache(FolderItem *item, MsgInfo *newmsginfo, MsgInfo *flagsource)
2266 {
2267         /* update folder stats */
2268         if (MSG_IS_NEW(newmsginfo->flags))
2269                 item->new_msgs++;
2270         if (MSG_IS_UNREAD(newmsginfo->flags))
2271                 item->unread_msgs++;
2272         if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
2273                 item->unreadmarked_msgs++;
2274         item->total_msgs++;
2275
2276         folder_item_update_freeze();
2277         msgcache_add_msg(item->cache, newmsginfo);
2278         copy_msginfo_flags(flagsource, newmsginfo);
2279         folder_item_update_with_msg(item,  F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_ADDMSG, newmsginfo);
2280         folder_item_update_thaw();
2281 }
2282
2283 static void remove_msginfo_from_cache(FolderItem *item, MsgInfo *msginfo)
2284 {
2285         MsgInfoUpdate msginfo_update;
2286
2287         if (!item->cache)
2288                 folder_item_read_cache(item);
2289
2290         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2291                 msginfo->folder->new_msgs--;
2292         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2293                 msginfo->folder->unread_msgs--;
2294         if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2295                 msginfo->folder->unreadmarked_msgs--;
2296         msginfo->folder->total_msgs--;
2297
2298         msginfo_update.msginfo = msginfo;
2299         msginfo_update.flags = MSGINFO_UPDATE_DELETED;
2300         hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
2301
2302         msgcache_remove_msg(item->cache, msginfo->msgnum);
2303         folder_item_update_with_msg(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_REMOVEMSG, msginfo);
2304 }
2305
2306 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
2307                          MsgFlags *flags, gboolean remove_source)
2308 {
2309         GSList file_list;
2310         MsgFileInfo fileinfo;
2311
2312         g_return_val_if_fail(dest != NULL, -1);
2313         g_return_val_if_fail(file != NULL, -1);
2314  
2315         fileinfo.msginfo = NULL;
2316         fileinfo.file = (gchar *)file;
2317         fileinfo.flags = flags;
2318         file_list.data = &fileinfo;
2319         file_list.next = NULL;
2320
2321         return folder_item_add_msgs(dest, &file_list, remove_source);
2322 }
2323
2324 gint folder_item_add_msgs(FolderItem *dest, GSList *file_list,
2325                           gboolean remove_source)
2326 {
2327         Folder *folder;
2328         gint ret, num, lastnum = -1;
2329         GSList *file_cur;
2330         GRelation *relation;
2331         MsgFileInfo *fileinfo = NULL;
2332         gboolean folderscan = FALSE;
2333
2334         g_return_val_if_fail(dest != NULL, -1);
2335         g_return_val_if_fail(file_list != NULL, -1);
2336         g_return_val_if_fail(dest->folder != NULL, -1);
2337
2338         folder = dest->folder;
2339
2340         relation = g_relation_new(2);
2341         g_relation_index(relation, 0, g_direct_hash, g_direct_equal);
2342
2343         if (folder->klass->add_msgs != NULL) {
2344                 ret = folder->klass->add_msgs(folder, dest, file_list, relation);
2345                 if (ret < 0) {
2346                         g_relation_destroy(relation);
2347                         return ret;
2348                 }
2349         } else {
2350                 for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
2351                         fileinfo = (MsgFileInfo *) file_cur->data;
2352
2353                         ret = folder->klass->add_msg(folder, dest, fileinfo->file, fileinfo->flags);
2354                         if (ret < 0) {
2355                                 g_relation_destroy(relation);
2356                                 return ret;
2357                         }
2358                         g_relation_insert(relation, fileinfo, GINT_TO_POINTER(ret));
2359                 }
2360         }
2361
2362         for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
2363                 GTuples *tuples;
2364
2365                 fileinfo = (MsgFileInfo *) file_cur->data;
2366                 tuples = g_relation_select(relation, fileinfo, 0);
2367                 num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
2368                 g_tuples_destroy(tuples);
2369
2370                 if (num >= 0) {
2371                         MsgInfo *newmsginfo;
2372
2373                         if (num == 0) {
2374                                 if (!folderscan) {
2375                                         folder_item_scan_full(dest, FALSE);
2376                                         folderscan = TRUE;
2377                                 }
2378                                 num = folder_item_get_msg_num_by_file(dest, fileinfo->file);
2379                         }
2380
2381                         if (num > lastnum)
2382                                 lastnum = num;
2383
2384                         if (num >= 0 && remove_source) {
2385                                 if (unlink(fileinfo->file) < 0)
2386                                         FILE_OP_ERROR(fileinfo->file, "unlink");
2387                         }
2388
2389                         if (num == 0)
2390                                 continue;
2391
2392                         if (!folderscan && 
2393                             ((newmsginfo = get_msginfo(dest, num)) != NULL)) {
2394                                 add_msginfo_to_cache(dest, newmsginfo, NULL);
2395                                 procmsg_msginfo_free(newmsginfo);
2396                         } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
2397                                 /* TODO: set default flags */
2398                                 procmsg_msginfo_free(newmsginfo);
2399                         }
2400                 }
2401         }
2402
2403         g_relation_destroy(relation);
2404
2405         return lastnum;
2406 }
2407
2408 /*
2409 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
2410 {
2411         Folder *folder;
2412         gint num;
2413
2414         g_return_val_if_fail(dest != NULL, -1);
2415         g_return_val_if_fail(msginfo != NULL, -1);
2416
2417         folder = dest->folder;
2418         if (dest->last_num < 0) folder->scan(folder, dest);
2419
2420         num = folder->move_msg(folder, dest, msginfo);
2421         if (num > 0) dest->last_num = num;
2422
2423         return num;
2424 }
2425 */
2426                 
2427 FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest) 
2428 {
2429         GSList *mlist;
2430         FolderItem *new_item;
2431         FolderItem *next_item;
2432         GNode *srcnode;
2433         gchar *old_id, *new_id;
2434
2435         mlist = folder_item_get_msg_list(src);
2436
2437         /* move messages */
2438         debug_print("Moving %s to %s\n", src->path, dest->path);
2439         new_item = folder_create_folder(dest, src->name);
2440         if (new_item == NULL) {
2441                 printf("Can't create folder\n");
2442                 return NULL;
2443         }
2444         
2445         if (new_item->folder == NULL)
2446                 new_item->folder = dest->folder;
2447
2448         /* move messages */
2449         log_message(_("Moving %s to %s...\n"), 
2450                         src->name, new_item->path);
2451         if (mlist != NULL)
2452                 folder_item_move_msgs(new_item, mlist);
2453         
2454         /*copy prefs*/
2455         folder_item_prefs_copy_prefs(src, new_item);
2456         new_item->collapsed = src->collapsed;
2457         new_item->thread_collapsed = src->thread_collapsed;
2458         new_item->threaded  = src->threaded;
2459         new_item->ret_rcpt  = src->ret_rcpt;
2460         new_item->hide_read_msgs = src->hide_read_msgs;
2461         new_item->sort_key  = src->sort_key;
2462         new_item->sort_type = src->sort_type;
2463
2464         prefs_matcher_write_config();
2465         
2466         /* recurse */
2467         srcnode = src->folder->node;    
2468         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
2469         srcnode = srcnode->children;
2470         while (srcnode != NULL) {
2471                 if (srcnode && srcnode->data) {
2472                         next_item = (FolderItem*) srcnode->data;
2473                         srcnode = srcnode->next;
2474                         if (folder_item_move_recursive(next_item, new_item) == NULL)
2475                                 return NULL;
2476                 }
2477         }
2478         old_id = folder_item_get_identifier(src);
2479         new_id = folder_item_get_identifier(new_item);
2480         debug_print("updating rules : %s => %s\n", old_id, new_id);
2481
2482         /* if src supports removing, otherwise only copy folder */
2483         if (src->folder->klass->remove_folder != NULL)  
2484                 src->folder->klass->remove_folder(src->folder, src);
2485         folder_write_list();
2486
2487         if (old_id != NULL && new_id != NULL)
2488                 prefs_filtering_rename_path(old_id, new_id);
2489         g_free(old_id);
2490         g_free(new_id);
2491
2492         return new_item;
2493 }
2494
2495 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item)
2496 {
2497         FolderItem *tmp = folder_item_parent(dest);
2498         gchar * src_identifier, * dst_identifier;
2499         gchar * phys_srcpath, * phys_dstpath;
2500         
2501         while (tmp) {
2502                 if (tmp == src) {
2503                         return F_MOVE_FAILED_DEST_IS_CHILD;
2504                 }
2505                 tmp = folder_item_parent(tmp);;
2506         }
2507         
2508         tmp = folder_item_parent(src);
2509         
2510         src_identifier = folder_item_get_identifier(src);
2511         dst_identifier = folder_item_get_identifier(dest);
2512         
2513         if(dst_identifier == NULL && dest->folder && folder_item_parent(dest) == NULL) {
2514                 /* dest can be a root folder */
2515                 dst_identifier = folder_get_identifier(dest->folder);
2516         }
2517         if (src_identifier == NULL || dst_identifier == NULL) {
2518                 debug_print("Can't get identifiers\n");
2519                 return F_MOVE_FAILED;
2520         }
2521
2522         if (src->folder != dest->folder) {
2523                 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
2524         }
2525
2526         phys_srcpath = folder_item_get_path(src);
2527         phys_dstpath = g_strconcat(folder_item_get_path(dest),G_DIR_SEPARATOR_S,g_basename(phys_srcpath),NULL);
2528
2529         if (folder_item_parent(src) == dest || src == dest) {
2530                 g_free(src_identifier);
2531                 g_free(dst_identifier);
2532                 g_free(phys_srcpath);
2533                 g_free(phys_dstpath);
2534                 return F_MOVE_FAILED_DEST_IS_PARENT;
2535         }
2536         debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
2537         if ((tmp = folder_item_move_recursive(src, dest)) == NULL) {
2538                 return F_MOVE_FAILED;
2539         }
2540         
2541         g_free(src_identifier);
2542         g_free(dst_identifier);
2543         g_free(phys_srcpath);
2544         g_free(phys_dstpath);
2545
2546         *new_item = tmp;
2547
2548         return F_MOVE_OK;
2549 }
2550
2551 /**
2552  * Copy a list of message to a new folder and remove
2553  * source messages if wanted
2554  */
2555 static gint do_copy_msgs(FolderItem *dest, GSList *msglist, gboolean remove_source)
2556 {
2557         Folder *folder;
2558         GSList *l;
2559         gint num, lastnum = -1;
2560         gboolean folderscan = FALSE;
2561         GRelation *relation;
2562
2563         g_return_val_if_fail(dest != NULL, -1);
2564         g_return_val_if_fail(msglist != NULL, -1);
2565
2566         folder = dest->folder;
2567
2568         g_return_val_if_fail(folder->klass->copy_msg != NULL, -1);
2569
2570         relation = g_relation_new(2);
2571         g_relation_index(relation, 0, g_direct_hash, g_direct_equal);
2572         g_relation_index(relation, 1, g_direct_hash, g_direct_equal);
2573
2574         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
2575                 MsgInfo * msginfo = (MsgInfo *) l->data;
2576
2577                 if (msginfo->planned_download != 0) {
2578                         int old_planned = msginfo->planned_download;
2579                         partial_unmark(msginfo);
2580                         /* little hack to reenable after */
2581                         msginfo->planned_download = old_planned;
2582                 }
2583         }
2584
2585         /* 
2586          * Copy messages to destination folder and 
2587          * store new message numbers in newmsgnums
2588          */
2589         if (folder->klass->copy_msgs != NULL) {
2590                 if (folder->klass->copy_msgs(folder, dest, msglist, relation) < 0) {
2591                         g_relation_destroy(relation);
2592                         return -1;
2593                 }
2594         } else {
2595                 for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
2596                         MsgInfo * msginfo = (MsgInfo *) l->data;
2597
2598                         num = folder->klass->copy_msg(folder, dest, msginfo);
2599                         g_relation_insert(relation, msginfo, GINT_TO_POINTER(num));
2600                 }
2601         }
2602
2603         /* Read cache for dest folder */
2604         if (!dest->cache) folder_item_read_cache(dest);
2605
2606         /* 
2607          * Fetch new MsgInfos for new messages in dest folder,
2608          * add them to the msgcache and update folder message counts
2609          */
2610         if (g_relation_count(relation, GINT_TO_POINTER(0), 1) > 0) {
2611                 folder_item_scan_full(dest, FALSE);
2612                 folderscan = TRUE;
2613         }
2614
2615         for (l = msglist; l != NULL; l = g_slist_next(l)) {
2616                 MsgInfo *msginfo = (MsgInfo *) l->data;
2617                 GTuples *tuples;
2618
2619                 tuples = g_relation_select(relation, msginfo, 0);
2620                 num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
2621                 g_tuples_destroy(tuples);
2622
2623                 if (num >= 0) {
2624                         MsgInfo *newmsginfo = NULL;
2625
2626                         if (folderscan) {
2627                                 if (msginfo->msgid != NULL) {
2628                                         newmsginfo = folder_item_get_msginfo_by_msgid(dest, msginfo->msgid);
2629                                         if (newmsginfo != NULL) {
2630                                                 copy_msginfo_flags(msginfo, newmsginfo);
2631                                                 num = newmsginfo->msgnum;
2632                                         }
2633                                 }
2634                         } else {
2635                                 newmsginfo = get_msginfo(dest, num);
2636                                 if (newmsginfo != NULL) {
2637                                         add_msginfo_to_cache(dest, newmsginfo, msginfo);
2638                                 }
2639                         }
2640
2641                         if (msginfo->planned_download 
2642                             == POP3_PARTIAL_DLOAD_DELE) {
2643                                 partial_mark_for_delete(newmsginfo);
2644                         }
2645                         if (msginfo->planned_download 
2646                             == POP3_PARTIAL_DLOAD_DLOAD) {
2647                                 partial_mark_for_download(newmsginfo);
2648                         }
2649                         procmsg_msginfo_free(newmsginfo);
2650
2651
2652                         if (num > lastnum)
2653                                 lastnum = num;
2654                 }
2655         }
2656
2657         if (remove_source) {
2658                 /*
2659                  * Remove source messages from their folders if
2660                  * copying was successfull and update folder
2661                  * message counts
2662                  */
2663                 for (l = msglist; l != NULL; l = g_slist_next(l)) {
2664                         MsgInfo *msginfo = (MsgInfo *) l->data;
2665                         FolderItem *item = msginfo->folder;
2666                         GTuples *tuples;
2667
2668                         tuples = g_relation_select(relation, msginfo, 0);
2669                         num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
2670                         g_tuples_destroy(tuples);
2671
2672                         if ((num >= 0) && (item->folder->klass->remove_msg != NULL)) {
2673                                 item->folder->klass->remove_msg(item->folder,
2674                                                                 msginfo->folder,
2675                                                                 msginfo->msgnum);
2676                                 remove_msginfo_from_cache(item, msginfo);
2677                         }
2678                 }
2679         }
2680
2681         g_relation_destroy(relation);
2682         return lastnum;
2683 }
2684
2685 /**
2686  * Move a message to a new folder.
2687  *
2688  * \param dest Destination folder
2689  * \param msginfo The message
2690  */
2691 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
2692 {
2693         GSList list;
2694
2695         g_return_val_if_fail(dest != NULL, -1);
2696         g_return_val_if_fail(msginfo != NULL, -1);
2697
2698         list.data = msginfo;
2699         list.next = NULL;
2700
2701         return do_copy_msgs(dest, &list, TRUE);
2702 }
2703
2704 /**
2705  * Move a list of messages to a new folder.
2706  *
2707  * \param dest Destination folder
2708  * \param msglist List of messages
2709  */
2710 gint folder_item_move_msgs(FolderItem *dest, GSList *msglist)
2711 {
2712         g_return_val_if_fail(dest != NULL, -1);
2713         g_return_val_if_fail(msglist != NULL, -1);
2714
2715         return do_copy_msgs(dest, msglist, TRUE);
2716 }
2717
2718 /**
2719  * Copy a message to a new folder.
2720  *
2721  * \param dest Destination folder
2722  * \param msginfo The message
2723  */
2724 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
2725 {
2726         GSList list;
2727
2728         g_return_val_if_fail(dest != NULL, -1);
2729         g_return_val_if_fail(msginfo != NULL, -1);
2730     
2731         list.data = msginfo;
2732         list.next = NULL;
2733         
2734         return do_copy_msgs(dest, &list, FALSE);
2735 }
2736
2737 /**
2738  * Copy a list of messages to a new folder.
2739  *
2740  * \param dest Destination folder
2741  * \param msglist List of messages
2742  */
2743 gint folder_item_copy_msgs(FolderItem *dest, GSList *msglist)
2744 {
2745         g_return_val_if_fail(dest != NULL, -1);
2746         g_return_val_if_fail(msglist != NULL, -1);
2747
2748         return do_copy_msgs(dest, msglist, FALSE);
2749 }
2750
2751 gint folder_item_remove_msg(FolderItem *item, gint num)
2752 {
2753         Folder *folder;
2754         gint ret;
2755         MsgInfo *msginfo;
2756
2757         g_return_val_if_fail(item != NULL, -1);
2758         folder = item->folder;
2759         g_return_val_if_fail(folder->klass->remove_msg != NULL, -1);
2760
2761         if (!item->cache) folder_item_read_cache(item);
2762
2763         ret = folder->klass->remove_msg(folder, item, num);
2764
2765         msginfo = msgcache_get_msg(item->cache, num);
2766         if (msginfo != NULL) {
2767                 remove_msginfo_from_cache(item, msginfo);
2768                 procmsg_msginfo_free(msginfo);
2769         }
2770
2771         return ret;
2772 }
2773
2774 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
2775 {
2776         Folder *folder;
2777         gint ret = 0;
2778
2779         g_return_val_if_fail(item != NULL, -1);
2780         folder = item->folder;
2781         g_return_val_if_fail(folder != NULL, -1);
2782
2783         if (!item->cache) folder_item_read_cache(item);
2784
2785         folder_item_update_freeze();
2786         while (msglist != NULL) {
2787                 MsgInfo *msginfo = (MsgInfo *)msglist->data;
2788
2789                 ret = folder_item_remove_msg(item, msginfo->msgnum);
2790                 if (ret != 0) break;
2791                 msgcache_remove_msg(item->cache, msginfo->msgnum);
2792                 msglist = msglist->next;
2793         }
2794         folder_item_update_thaw();
2795
2796         return ret;
2797 }
2798
2799 gint folder_item_remove_all_msg(FolderItem *item)
2800 {
2801         Folder *folder;
2802         gint result;
2803
2804         g_return_val_if_fail(item != NULL, -1);
2805
2806         folder = item->folder;
2807
2808         g_return_val_if_fail(folder->klass->remove_all_msg != NULL, -1);
2809
2810         result = folder->klass->remove_all_msg(folder, item);
2811
2812         if (result == 0) {
2813                 folder_item_free_cache(item);
2814                 item->cache = msgcache_new();
2815
2816                 item->new_msgs = 0;
2817                 item->unread_msgs = 0;
2818                 item->unreadmarked_msgs = 0;
2819                 item->total_msgs = 0;
2820                 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2821         }
2822
2823         return result;
2824 }
2825
2826 void folder_item_change_msg_flags(FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
2827 {
2828         g_return_if_fail(item != NULL);
2829         g_return_if_fail(msginfo != NULL);
2830         
2831         if (item->folder->klass->change_flags != NULL) {
2832                 item->folder->klass->change_flags(item->folder, item, msginfo, newflags);
2833         } else {
2834                 msginfo->flags.perm_flags = newflags;
2835         }
2836 }
2837
2838 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
2839 {
2840         Folder *folder;
2841
2842         g_return_val_if_fail(item != NULL, FALSE);
2843
2844         folder = item->folder;
2845
2846         g_return_val_if_fail(folder->klass->is_msg_changed != NULL, -1);
2847
2848         return folder->klass->is_msg_changed(folder, item, msginfo);
2849 }
2850
2851 gchar *folder_item_get_cache_file(FolderItem *item)
2852 {
2853         gchar *path;
2854         gchar *file;
2855
2856         g_return_val_if_fail(item != NULL, NULL);
2857         g_return_val_if_fail(item->path != NULL, NULL);
2858
2859         path = folder_item_get_path(item);
2860         g_return_val_if_fail(path != NULL, NULL);
2861         if (!is_dir_exist(path))
2862                 make_dir_hier(path);
2863         file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
2864         g_free(path);
2865
2866         return file;
2867 }
2868
2869 gchar *folder_item_get_mark_file(FolderItem *item)
2870 {
2871         gchar *path;
2872         gchar *file;
2873
2874         g_return_val_if_fail(item != NULL, NULL);
2875         g_return_val_if_fail(item->path != NULL, NULL);
2876
2877         path = folder_item_get_path(item);
2878         g_return_val_if_fail(path != NULL, NULL);
2879         if (!is_dir_exist(path))
2880                 make_dir_hier(path);
2881         file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
2882         g_free(path);
2883
2884         return file;
2885 }
2886
2887 static gpointer xml_to_folder_item(gpointer nodedata, gpointer data)
2888 {
2889         XMLNode *xmlnode = (XMLNode *) nodedata;
2890         Folder *folder = (Folder *) data;
2891         FolderItem *item;
2892
2893         g_return_val_if_fail(xmlnode != NULL, NULL);
2894         g_return_val_if_fail(folder != NULL, NULL);
2895
2896         if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
2897                 g_warning("tag name != \"folderitem\"\n");
2898                 return NULL;
2899         }
2900
2901         item = folder_item_new(folder, "", "");
2902         if (folder->klass->item_set_xml != NULL)
2903                 folder->klass->item_set_xml(folder, item, xmlnode->tag);
2904         else
2905                 folder_item_set_xml(folder, item, xmlnode->tag);
2906
2907         item->folder = folder;
2908
2909         switch (item->stype) {
2910         case F_INBOX:  folder->inbox  = item; break;
2911         case F_OUTBOX: folder->outbox = item; break;
2912         case F_DRAFT:  folder->draft  = item; break;
2913         case F_QUEUE:  folder->queue  = item; break;
2914         case F_TRASH:  folder->trash  = item; break;
2915         default:       break;
2916         }
2917         folder_item_prefs_read_config(item);
2918
2919         return item;
2920 }
2921
2922 static gboolean folder_item_set_node(GNode *node, gpointer data)
2923 {
2924         FolderItem *item = (FolderItem *) node->data;
2925         item->node = node;
2926
2927         return FALSE;
2928 }
2929
2930 static Folder *folder_get_from_xml(GNode *node)
2931 {
2932         Folder *folder;
2933         XMLNode *xmlnode;
2934         GList *list;
2935         FolderClass *klass = NULL;
2936         GNode *cur;
2937
2938         g_return_val_if_fail(node->data != NULL, NULL);
2939
2940         xmlnode = node->data;
2941         if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
2942                 g_warning("tag name != \"folder\"\n");
2943                 return NULL;
2944         }
2945         list = xmlnode->tag->attr;
2946         for (; list != NULL; list = list->next) {
2947                 XMLAttr *attr = list->data;
2948
2949                 if (!attr || !attr->name || !attr->value) continue;
2950                 if (!strcmp(attr->name, "type"))
2951                         klass = folder_get_class_from_string(attr->value);
2952         }
2953         if (klass == NULL)
2954                 return NULL;
2955
2956         folder = folder_new(klass, "", "");
2957         g_return_val_if_fail(folder != NULL, NULL);
2958
2959         if (klass->set_xml)
2960                 klass->set_xml(folder, xmlnode->tag);
2961         else
2962                 folder_set_xml(folder, xmlnode->tag);
2963
2964         cur = node->children;
2965         while (cur != NULL) {
2966                 GNode *itemnode;
2967
2968                 itemnode = g_node_map(cur, xml_to_folder_item, (gpointer) folder);
2969                 g_node_append(folder->node, itemnode);
2970                 cur = cur->next;
2971         }
2972         g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, folder_item_set_node, NULL);
2973         
2974         return folder;
2975 }
2976
2977 static gchar *folder_get_list_path(void)
2978 {
2979         static gchar *filename = NULL;
2980
2981         if (!filename)
2982                 filename =  g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2983                                         FOLDER_LIST, NULL);
2984
2985         return filename;
2986 }
2987
2988 #define PUT_ESCAPE_STR(fp, attr, str)                   \
2989 {                                                       \
2990         fputs(" " attr "=\"", fp);                      \
2991         xml_file_put_escape_str(fp, str);               \
2992         fputs("\"", fp);                                \
2993 }
2994
2995 static gpointer folder_item_to_xml(gpointer nodedata, gpointer data)
2996 {
2997         FolderItem *item = (FolderItem *) nodedata;
2998         XMLTag *tag;
2999
3000         g_return_val_if_fail(item != NULL, NULL);
3001
3002         if (item->folder->klass->item_get_xml != NULL)
3003                 tag = item->folder->klass->item_get_xml(item->folder, item);
3004         else
3005                 tag = folder_item_get_xml(item->folder, item);
3006
3007         return xml_node_new(tag, NULL);;
3008 }
3009
3010 static GNode *folder_get_xml_node(Folder *folder)
3011 {
3012         GNode *node;
3013         XMLNode *xmlnode;
3014         XMLTag *tag;
3015
3016         g_return_val_if_fail(folder != NULL, NULL);
3017
3018         if (folder->klass->get_xml != NULL)
3019                 tag = folder->klass->get_xml(folder);
3020         else
3021                 tag = folder_get_xml(folder);
3022
3023         xml_tag_add_attr(tag, xml_attr_new("type", folder->klass->idstr));
3024
3025         xmlnode = xml_node_new(tag, NULL);
3026
3027         node = g_node_new(xmlnode);
3028         if (folder->node->children) {
3029                 GNode *cur;
3030
3031                 cur = folder->node->children;
3032                 while (cur) {
3033                         GNode *xmlnode;
3034
3035                         xmlnode = g_node_map(cur, folder_item_to_xml, (gpointer) folder);
3036                         g_node_append(node, xmlnode);
3037                         cur = cur->next;
3038                 }
3039         }
3040
3041         return node;
3042 }
3043
3044 static void folder_update_op_count_rec(GNode *node)
3045 {
3046         FolderItem *fitem = FOLDER_ITEM(node->data);
3047
3048         if (g_node_depth(node) > 0) {
3049                 if (fitem->op_count > 0) {
3050                         fitem->op_count = 0;
3051                         folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
3052                 }
3053                 if (node->children) {
3054                         GNode *child;
3055
3056                         child = node->children;
3057                         while (child) {
3058                                 GNode *cur;
3059
3060                                 cur = child;
3061                                 child = cur->next;
3062                                 folder_update_op_count_rec(cur);
3063                         }
3064                 }
3065         }
3066 }
3067
3068 void folder_update_op_count(void) 
3069 {
3070         GList *cur;
3071         Folder *folder;
3072
3073         for (cur = folder_list; cur != NULL; cur = cur->next) {
3074                 folder = cur->data;
3075                 folder_update_op_count_rec(folder->node);
3076         }
3077 }
3078
3079 typedef struct _type_str {
3080         gchar * str;
3081         gint type;
3082 } type_str;
3083
3084
3085 /*
3086 static gchar * folder_item_get_tree_identifier(FolderItem * item)
3087 {
3088         if (item->parent != NULL) {
3089                 gchar * path;
3090                 gchar * id;
3091
3092                 path = folder_item_get_tree_identifier(item->parent);
3093                 if (path == NULL)
3094                         return NULL;
3095
3096                 id = g_strconcat(path, "/", item->name, NULL);
3097                 g_free(path);
3098
3099                 return id;
3100         }
3101         else {
3102                 return g_strconcat("/", item->name, NULL);
3103         }
3104 }
3105 */
3106
3107 /* CLAWS: temporary local folder for filtering */
3108 #define TEMP_FOLDER "TEMP_FOLDER"
3109 #define PROCESSING_FOLDER_ITEM "processing"     
3110
3111 static FolderItem *processing_folder_item;
3112
3113 static void folder_create_processing_folder(void)
3114 {
3115         Folder *processing_folder;
3116         gchar      *tmpname;
3117
3118         if ((processing_folder = folder_find_from_name(TEMP_FOLDER, mh_get_class())) == NULL) {
3119                 gchar *tmppath;
3120
3121                 tmppath =
3122                     g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
3123                                 "tempfolder", NULL);
3124                 processing_folder =
3125                     folder_new(mh_get_class(), TEMP_FOLDER, tmppath);
3126                 g_free(tmppath);
3127         }
3128         g_assert(processing_folder != NULL);
3129
3130         debug_print("tmpparentroot %s\n", LOCAL_FOLDER(processing_folder)->rootpath);
3131         if (LOCAL_FOLDER(processing_folder)->rootpath[0] == '/')
3132                 tmpname = g_strconcat(LOCAL_FOLDER(processing_folder)->rootpath,
3133                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
3134                                       NULL);
3135         else
3136                 tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
3137                                       LOCAL_FOLDER(processing_folder)->rootpath,
3138                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
3139                                       NULL);
3140
3141         if (!is_dir_exist(tmpname)) {
3142                 debug_print("*TMP* creating %s\n", tmpname);
3143                 processing_folder_item = processing_folder->klass->create_folder(processing_folder,
3144                                                                                  processing_folder->node->data,
3145                                                                                  PROCESSING_FOLDER_ITEM);
3146         } else {
3147                 debug_print("*TMP* already created\n");
3148                 processing_folder_item = folder_item_new(processing_folder, PROCESSING_FOLDER_ITEM, PROCESSING_FOLDER_ITEM);
3149                 g_assert(processing_folder_item);
3150                 folder_item_append(processing_folder->node->data, processing_folder_item);
3151         }
3152         g_assert(processing_folder_item != NULL);
3153         g_free(tmpname);
3154 }
3155
3156 FolderItem *folder_get_default_processing(void)
3157 {
3158         if (!processing_folder_item) {
3159                 folder_create_processing_folder();
3160         }
3161         return processing_folder_item;
3162 }
3163
3164 /* folder_persist_prefs_new() - return hash table with persistent
3165  * settings (and folder name as key). 
3166  * (note that in claws other options are in the folder_item_prefs_RC
3167  * file, so those don't need to be included in PersistPref yet) 
3168  */
3169 GHashTable *folder_persist_prefs_new(Folder *folder)
3170 {
3171         GHashTable *pptable;
3172
3173         g_return_val_if_fail(folder, NULL);
3174         pptable = g_hash_table_new(g_str_hash, g_str_equal);
3175         folder_get_persist_prefs_recursive(folder->node, pptable);
3176         return pptable;
3177 }
3178
3179 void folder_persist_prefs_free(GHashTable *pptable)
3180 {
3181         g_return_if_fail(pptable);
3182         g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
3183         g_hash_table_destroy(pptable);
3184 }
3185
3186 const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
3187 {
3188         if (pptable == NULL || name == NULL) return NULL;
3189         return g_hash_table_lookup(pptable, name);
3190 }
3191
3192 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
3193 {
3194         const PersistPrefs *pp;
3195         gchar *id = folder_item_get_identifier(item);
3196
3197         pp = folder_get_persist_prefs(pptable, id); 
3198         g_free(id);
3199
3200         if (!pp) return;
3201
3202         /* CLAWS: since not all folder properties have been migrated to 
3203          * folderlist.xml, we need to call the old stuff first before
3204          * setting things that apply both to Main and Claws. */
3205         folder_item_prefs_read_config(item); 
3206
3207         item->collapsed = pp->collapsed;
3208         item->thread_collapsed = pp->thread_collapsed;
3209         item->threaded  = pp->threaded;
3210         item->ret_rcpt  = pp->ret_rcpt;
3211         item->hide_read_msgs = pp->hide_read_msgs;
3212         item->sort_key  = pp->sort_key;
3213         item->sort_type = pp->sort_type;
3214 }
3215
3216 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
3217 {
3218         FolderItem *item = FOLDER_ITEM(node->data);
3219         PersistPrefs *pp;
3220         GNode *child, *cur;
3221         gchar *id;
3222
3223         g_return_if_fail(node != NULL);
3224         g_return_if_fail(item != NULL);
3225
3226         /* NOTE: item->path == NULL means top level folder; not interesting
3227          * to store preferences of that one.  */
3228         if (item->path) {
3229                 id = folder_item_get_identifier(item);
3230                 pp = g_new0(PersistPrefs, 1);
3231                 g_return_if_fail(pp != NULL);
3232                 pp->collapsed = item->collapsed;
3233                 pp->thread_collapsed = item->thread_collapsed;
3234                 pp->threaded  = item->threaded;
3235                 pp->ret_rcpt  = item->ret_rcpt; 
3236                 pp->hide_read_msgs = item->hide_read_msgs;
3237                 pp->sort_key  = item->sort_key;
3238                 pp->sort_type = item->sort_type;
3239                 g_hash_table_insert(pptable, id, pp);
3240         }
3241
3242         if (node->children) {
3243                 child = node->children;
3244                 while (child) {
3245                         cur = child;
3246                         child = cur->next;
3247                         folder_get_persist_prefs_recursive(cur, pptable);
3248                 }
3249         }       
3250 }
3251
3252 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
3253 {
3254         if (key) 
3255                 g_free(key);
3256         if (val) 
3257                 g_free(val);
3258         return TRUE;    
3259 }
3260
3261 void folder_item_apply_processing(FolderItem *item)
3262 {
3263         GSList *processing_list;
3264         GSList *mlist, *cur;
3265         
3266         g_return_if_fail(item != NULL);
3267         
3268         processing_list = item->prefs->processing;
3269
3270         folder_item_update_freeze();
3271
3272         mlist = folder_item_get_msg_list(item);
3273         for (cur = mlist ; cur != NULL ; cur = cur->next) {
3274                 MsgInfo * msginfo;
3275
3276                 msginfo = (MsgInfo *) cur->data;
3277                 
3278                 /* reset parameters that can be modified by processing */
3279                 msginfo->hidden = 0;
3280                 msginfo->score = 0;
3281                 
3282                 /* apply pre global rules */
3283                 filter_message_by_msginfo(pre_global_processing, msginfo);
3284                 
3285                 /* apply rules of the folder */
3286                 filter_message_by_msginfo(processing_list, msginfo);
3287
3288                 /* apply post global rules */
3289                 filter_message_by_msginfo(post_global_processing, msginfo);
3290                 
3291                 procmsg_msginfo_free(msginfo);
3292         }
3293         g_slist_free(mlist);
3294
3295         folder_item_update_thaw();
3296 }
3297
3298 /*
3299  *  functions for handling FolderItem content changes
3300  */
3301 static gint folder_item_update_freeze_cnt = 0;
3302
3303 static void folder_item_update_with_msg(FolderItem *item, FolderItemUpdateFlags update_flags, MsgInfo *msg)
3304 {
3305         if (folder_item_update_freeze_cnt == 0 /* || (msg != NULL && item->opened) */) {
3306                 FolderItemUpdateData source;
3307         
3308                 source.item = item;
3309                 source.update_flags = update_flags;
3310                 source.msg = msg;
3311                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
3312         } else {
3313                 item->update_flags |= update_flags & ~(F_ITEM_UPDATE_ADDMSG | F_ITEM_UPDATE_REMOVEMSG);
3314         }
3315 }
3316
3317 /**
3318  * Notify the folder system about changes to a folder. If the
3319  * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
3320  * be invoked, otherwise the changes will be remebered until
3321  * the folder system is thawed.
3322  *
3323  * \param item The FolderItem that was changed
3324  * \param update_flags Type of changed that was made
3325  */
3326 void folder_item_update(FolderItem *item, FolderItemUpdateFlags update_flags)
3327 {
3328         folder_item_update_with_msg(item, update_flags, NULL);
3329 }
3330
3331 void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
3332 {
3333         GNode *node = item->folder->node;       
3334
3335         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
3336         node = node->children;
3337
3338         folder_item_update(item, update_flags);
3339         while (node != NULL) {
3340                 if (node && node->data) {
3341                         FolderItem *next_item = (FolderItem*) node->data;
3342
3343                         folder_item_update(next_item, update_flags);
3344                 }
3345                 node = node->next;
3346         }
3347 }
3348
3349 void folder_item_update_freeze(void)
3350 {
3351         folder_item_update_freeze_cnt++;
3352 }
3353
3354 static void folder_item_update_func(FolderItem *item, gpointer data)
3355 {
3356         FolderItemUpdateData source;
3357     
3358         if (item->update_flags) {
3359                 source.item = item;
3360                 source.update_flags = item->update_flags;
3361                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);                             
3362                 item->update_flags = 0;
3363         }
3364 }
3365
3366 void folder_item_update_thaw(void)
3367 {
3368         if (folder_item_update_freeze_cnt > 0)
3369                 folder_item_update_freeze_cnt--;
3370         if (folder_item_update_freeze_cnt == 0) {
3371                 /* Update all folders */
3372                 folder_func_to_all_folders(folder_item_update_func, NULL);
3373         }
3374 }
3375
3376 #undef PUT_ESCAPE_STR