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