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