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