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