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