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