0.9.5claws30
[claws.git] / src / folder.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <stdio.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33
34 #include "intl.h"
35 #include "folder.h"
36 #include "session.h"
37 #include "imap.h"
38 #include "news.h"
39 #include "mh.h"
40 #include "mbox_folder.h"
41 #include "utils.h"
42 #include "xml.h"
43 #include "codeconv.h"
44 #include "prefs_gtk.h"
45 #include "account.h"
46 #include "filtering.h"
47 #include "scoring.h"
48 #include "procheader.h"
49 #include "hooks.h"
50 #include "log.h"
51 #include "folder_item_prefs.h"
52
53 /* Dependecies to be removed ?! */
54 #include "prefs_common.h"
55 #include "prefs_account.h"
56
57 static GList *folder_list = NULL;
58
59 static void folder_init         (Folder         *folder,
60                                  const gchar    *name);
61
62 static gboolean folder_read_folder_func (GNode          *node,
63                                          gpointer        data);
64 static gchar *folder_get_list_path      (void);
65 static void folder_write_list_recursive (GNode          *node,
66                                          gpointer        data);
67 static void folder_update_op_count_rec  (GNode          *node);
68
69
70 static void folder_get_persist_prefs_recursive
71                                         (GNode *node, GHashTable *pptable);
72 static gboolean persist_prefs_free      (gpointer key, gpointer val, gpointer data);
73 void folder_item_read_cache             (FolderItem *item);
74 void folder_item_free_cache             (FolderItem *item);
75 gint folder_item_scan_full              (FolderItem *item, gboolean filtering);
76
77 static GSList *classlist;
78
79 void folder_system_init(void)
80 {
81         folder_register_class(mh_get_class());
82         folder_register_class(imap_get_class());
83         folder_register_class(news_get_class());
84         folder_register_class(mbox_get_class());
85 }
86
87 GSList *folder_get_class_list(void)
88 {
89         return classlist;
90 }
91
92 void folder_register_class(FolderClass *klass)
93 {
94         debug_print("registering folder class %s\n", klass->idstr);
95         classlist = g_slist_append(classlist, klass);
96 }
97
98 Folder *folder_new(FolderClass *klass, const gchar *name, const gchar *path)
99 {
100         Folder *folder = NULL;
101         FolderItem *item;
102
103         g_return_val_if_fail(klass != NULL, NULL);
104
105         name = name ? name : path;
106         folder = klass->new_folder(name, path);
107
108         /* Create root folder item */
109         item = folder_item_new(folder, name, NULL);
110         item->folder = folder;
111         folder->node = item->node = g_node_new(item);
112         folder->data = NULL;
113
114         return folder;
115 }
116
117 static void folder_init(Folder *folder, const gchar *name)
118 {
119         g_return_if_fail(folder != NULL);
120
121         folder_set_name(folder, name);
122
123         /* Init folder data */
124         folder->account = NULL;
125         folder->inbox = NULL;
126         folder->outbox = NULL;
127         folder->draft = NULL;
128         folder->queue = NULL;
129         folder->trash = NULL;
130 }
131
132 void folder_local_folder_init(Folder *folder, const gchar *name,
133                               const gchar *path)
134 {
135         folder_init(folder, name);
136         LOCAL_FOLDER(folder)->rootpath = g_strdup(path);
137 }
138
139 void folder_remote_folder_init(Folder *folder, const gchar *name,
140                                const gchar *path)
141 {
142         folder_init(folder, name);
143         REMOTE_FOLDER(folder)->session = NULL;
144 }
145
146 void folder_destroy(Folder *folder)
147 {
148         g_return_if_fail(folder != NULL);
149         g_return_if_fail(folder->klass->destroy_folder != NULL);
150
151         folder_list = g_list_remove(folder_list, folder);
152
153         folder_tree_destroy(folder);
154
155         folder->klass->destroy_folder(folder);
156
157         g_free(folder->name);
158         g_free(folder);
159 }
160
161 void folder_local_folder_destroy(LocalFolder *lfolder)
162 {
163         g_return_if_fail(lfolder != NULL);
164
165         g_free(lfolder->rootpath);
166 }
167
168 void folder_remote_folder_destroy(RemoteFolder *rfolder)
169 {
170         g_return_if_fail(rfolder != NULL);
171
172         if (rfolder->session)
173                 session_destroy(rfolder->session);
174 }
175
176 FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path)
177 {
178         FolderItem *item = NULL;
179
180         if (folder->klass->item_new) {
181                 item = folder->klass->item_new(folder);
182         } else {
183                 item = g_new0(FolderItem, 1);
184         }
185
186         g_return_val_if_fail(item != NULL, NULL);
187
188         item->stype = F_NORMAL;
189         item->name = g_strdup(name);
190         item->path = g_strdup(path);
191         item->mtime = 0;
192         item->new_msgs = 0;
193         item->unread_msgs = 0;
194         item->unreadmarked_msgs = 0;
195         item->total_msgs = 0;
196         item->last_num = -1;
197         item->cache = NULL;
198         item->no_sub = FALSE;
199         item->no_select = FALSE;
200         item->collapsed = FALSE;
201         item->thread_collapsed = FALSE;
202         item->threaded  = TRUE;
203         item->ret_rcpt  = FALSE;
204         item->opened    = FALSE;
205         item->node = NULL;
206         item->parent = NULL;
207         item->folder = NULL;
208         item->account = NULL;
209         item->apply_sub = FALSE;
210         item->mark_queue = NULL;
211         item->data = NULL;
212
213         item->prefs = folder_item_prefs_new();
214
215         return item;
216 }
217
218 void folder_item_append(FolderItem *parent, FolderItem *item)
219 {
220         g_return_if_fail(parent != NULL);
221         g_return_if_fail(parent->folder != NULL);
222         g_return_if_fail(parent->node != NULL);
223         g_return_if_fail(item != NULL);
224
225         item->parent = parent;
226         item->folder = parent->folder;
227         item->node = g_node_append_data(parent->node, item);
228 }
229
230 static gboolean folder_item_remove_func(GNode *node, gpointer data)
231 {
232         FolderItem *item = FOLDER_ITEM(node->data);
233         FolderUpdateData hookdata;
234
235         if (item->cache != NULL) {
236                 msgcache_destroy(item->cache);
237                 item->cache = NULL;
238         }
239
240         hookdata.folder = item->folder;
241         hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_REMOVE_FOLDERITEM;
242         hookdata.item = item;
243         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
244
245         folder_item_destroy(item);
246
247         return FALSE;
248 }
249
250 void folder_item_remove(FolderItem *item)
251 {
252         GNode *node;
253
254         g_return_if_fail(item != NULL);
255         g_return_if_fail(item->folder != NULL);
256         g_return_if_fail(item->node != NULL);
257
258         node = item->node;
259
260         if (item->folder->node == node)
261                 item->folder->node = NULL;
262
263         g_node_traverse(node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
264                         folder_item_remove_func, NULL);
265         g_node_destroy(node);
266 }
267
268 void folder_item_remove_children(FolderItem *item)
269 {
270         GNode *node, *next;
271
272         g_return_if_fail(item != NULL);
273         g_return_if_fail(item->folder != NULL);
274         g_return_if_fail(item->node != NULL);
275
276         node = item->node->children;
277         while (node != NULL) {
278                 next = node->next;
279                 folder_item_remove(FOLDER_ITEM(node->data));
280                 node = next;
281         }
282 }
283
284 void folder_item_destroy(FolderItem *item)
285 {
286         Folder *folder;
287
288         g_return_if_fail(item != NULL);
289
290         folder = item->folder;
291         if (folder) {
292                 if (folder->inbox == item)
293                         folder->inbox = NULL;
294                 else if (folder->outbox == item)
295                         folder->outbox = NULL;
296                 else if (folder->draft == item)
297                         folder->draft = NULL;
298                 else if (folder->queue == item)
299                         folder->queue = NULL;
300                 else if (folder->trash == item)
301                         folder->trash = NULL;
302         }
303
304         if (item->cache)
305                 folder_item_free_cache(item);
306         if (item->prefs)
307                 folder_item_prefs_free(item->prefs);
308         g_free(item->name);
309         g_free(item->path);
310
311         if (item->folder != NULL) {
312                 if(item->folder->klass->item_destroy) {
313                         item->folder->klass->item_destroy(item->folder, item);
314                 } else {
315                         g_free(item);
316                 }
317         }
318 }
319
320 void folder_set_ui_func(Folder *folder, FolderUIFunc func, gpointer data)
321 {
322         g_return_if_fail(folder != NULL);
323
324         folder->ui_func = func;
325         folder->ui_func_data = data;
326 }
327
328 void folder_set_name(Folder *folder, const gchar *name)
329 {
330         g_return_if_fail(folder != NULL);
331
332         g_free(folder->name);
333         folder->name = name ? g_strdup(name) : NULL;
334         if (folder->node && folder->node->data) {
335                 FolderItem *item = (FolderItem *)folder->node->data;
336
337                 g_free(item->name);
338                 item->name = name ? g_strdup(name) : NULL;
339         }
340 }
341
342 gboolean folder_tree_destroy_func(GNode *node, gpointer data) {
343         FolderItem *item = (FolderItem *) node->data;
344
345         folder_item_destroy(item);
346         return FALSE;
347 }
348
349 void folder_tree_destroy(Folder *folder)
350 {
351         GNode *node;
352
353         g_return_if_fail(folder != NULL);
354
355         node = folder->node;
356         
357         prefs_scoring_clear_folder(folder);
358         prefs_filtering_clear_folder(folder);
359
360         if (node != NULL) {
361                 g_node_traverse(node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
362                                 folder_tree_destroy_func, NULL);
363                 g_node_destroy(node);
364                 folder->node = NULL;
365         }
366 }
367
368 void folder_add(Folder *folder)
369 {
370         Folder *cur_folder;
371         GList *cur;
372         gint i;
373
374         g_return_if_fail(folder != NULL);
375
376         for (i = 0, cur = folder_list; cur != NULL; cur = cur->next, i++) {
377                 cur_folder = FOLDER(cur->data);
378                 if (FOLDER_TYPE(folder) == F_MH) {
379                         if (FOLDER_TYPE(cur_folder) != F_MH) break;
380                 } else if (FOLDER_TYPE(folder) == F_MBOX) {
381                         if (FOLDER_TYPE(cur_folder) != F_MH &&
382                             FOLDER_TYPE(cur_folder) != F_MBOX) break;
383                 } else if (FOLDER_TYPE(folder) == F_IMAP) {
384                         if (FOLDER_TYPE(cur_folder) != F_MH &&
385                             FOLDER_TYPE(cur_folder) != F_MBOX &&
386                             FOLDER_TYPE(cur_folder) != F_IMAP) break;
387                 } else if (FOLDER_TYPE(folder) == F_NEWS) {
388                         if (FOLDER_TYPE(cur_folder) != F_MH &&
389                             FOLDER_TYPE(cur_folder) != F_MBOX &&
390                             FOLDER_TYPE(cur_folder) != F_IMAP &&
391                             FOLDER_TYPE(cur_folder) != F_NEWS) break;
392                 }
393         }
394
395         folder_list = g_list_insert(folder_list, folder, i);
396 }
397
398 GList *folder_get_list(void)
399 {
400         return folder_list;
401 }
402
403 gint folder_read_list(void)
404 {
405         GNode *node;
406         XMLNode *xmlnode;
407         gchar *path;
408
409         path = folder_get_list_path();
410         if (!is_file_exist(path)) return -1;
411         node = xml_parse_file(path);
412         if (!node) return -1;
413
414         xmlnode = node->data;
415         if (strcmp2(xmlnode->tag->tag, "folderlist") != 0) {
416                 g_warning("wrong folder list\n");
417                 xml_free_tree(node);
418                 return -1;
419         }
420
421         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, 2,
422                         folder_read_folder_func, NULL);
423
424         xml_free_tree(node);
425         if (folder_list)
426                 return 0;
427         else
428                 return -1;
429 }
430
431 void folder_write_list(void)
432 {
433         GList *list;
434         Folder *folder;
435         gchar *path;
436         PrefFile *pfile;
437
438         path = folder_get_list_path();
439         if ((pfile = prefs_write_open(path)) == NULL) return;
440
441         fprintf(pfile->fp, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
442                 conv_get_current_charset_str());
443         fputs("\n<folderlist>\n", pfile->fp);
444
445         for (list = folder_list; list != NULL; list = list->next) {
446                 folder = list->data;
447                 folder_write_list_recursive(folder->node, pfile->fp);
448         }
449
450         fputs("</folderlist>\n", pfile->fp);
451
452         if (prefs_file_close(pfile) < 0)
453                 g_warning("failed to write folder list.\n");
454 }
455
456 gboolean folder_scan_tree_func(GNode *node, gpointer data)
457 {
458         GHashTable *pptable = (GHashTable *)data;
459         FolderItem *item = (FolderItem *)node->data;
460         
461         folder_item_restore_persist_prefs(item, pptable);
462         folder_item_scan_full(item, FALSE);
463
464         return FALSE;
465 }
466
467 void folder_scan_tree(Folder *folder)
468 {
469         GHashTable *pptable;
470         FolderUpdateData hookdata;
471         
472         if (!folder->klass->scan_tree)
473                 return;
474         
475         pptable = folder_persist_prefs_new(folder);
476
477         /*
478          * should be changed and tree update should be done without 
479          * destroying the tree first
480          */
481         folder_tree_destroy(folder);
482         folder->klass->scan_tree(folder);
483
484         hookdata.folder = folder;
485         hookdata.update_flags = FOLDER_TREE_CHANGED;
486         hookdata.item = NULL;
487         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
488
489         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_scan_tree_func, pptable);
490         folder_persist_prefs_free(pptable);
491
492         prefs_matcher_read_config();
493
494         folder_write_list();
495 }
496
497 FolderItem *folder_create_folder(FolderItem *parent, const gchar *name)
498 {
499         FolderItem *new_item;
500         FolderUpdateData hookdata;
501
502         new_item = parent->folder->klass->create_folder(parent->folder, parent, name);
503         if (new_item) {
504                 new_item->cache = msgcache_new();
505
506                 hookdata.folder = new_item->folder;
507                 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_NEW_FOLDERITEM;
508                 hookdata.item = new_item;
509                 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
510         }
511
512         return new_item;
513 }
514
515 struct TotalMsgCount
516 {
517         guint new_msgs;
518         guint unread_msgs;
519         guint unreadmarked_msgs;
520         guint total_msgs;
521 };
522
523 struct FuncToAllFoldersData
524 {
525         FolderItemFunc  function;
526         gpointer        data;
527 };
528
529 static gboolean folder_func_to_all_folders_func(GNode *node, gpointer data)
530 {
531         FolderItem *item;
532         struct FuncToAllFoldersData *function_data = (struct FuncToAllFoldersData *) data;
533
534         g_return_val_if_fail(node->data != NULL, FALSE);
535
536         item = FOLDER_ITEM(node->data);
537         g_return_val_if_fail(item != NULL, FALSE);
538
539         function_data->function(item, function_data->data);
540
541         return FALSE;
542 }
543
544 void folder_func_to_all_folders(FolderItemFunc function, gpointer data)
545 {
546         GList *list;
547         Folder *folder;
548         struct FuncToAllFoldersData function_data;
549         
550         function_data.function = function;
551         function_data.data = data;
552
553         for (list = folder_list; list != NULL; list = list->next) {
554                 folder = FOLDER(list->data);
555                 if (folder->node)
556                         g_node_traverse(folder->node, G_PRE_ORDER,
557                                         G_TRAVERSE_ALL, -1,
558                                         folder_func_to_all_folders_func,
559                                         &function_data);
560         }
561 }
562
563 static void folder_count_total_msgs_func(FolderItem *item, gpointer data)
564 {
565         struct TotalMsgCount *count = (struct TotalMsgCount *)data;
566
567         count->new_msgs += item->new_msgs;
568         count->unread_msgs += item->unread_msgs;
569         count->unreadmarked_msgs += item->unreadmarked_msgs;
570         count->total_msgs += item->total_msgs;
571 }
572
573 struct TotalMsgStatus
574 {
575         guint new;
576         guint unread;
577         guint total;
578         GString *str;
579 };
580
581 static gboolean folder_get_status_full_all_func(GNode *node, gpointer data)
582 {
583         FolderItem *item;
584         struct TotalMsgStatus *status = (struct TotalMsgStatus *)data;
585         gchar *id;
586  
587         g_return_val_if_fail(node->data != NULL, FALSE);
588  
589         item = FOLDER_ITEM(node->data);
590
591         if (!item->path) return FALSE;
592
593         status->new += item->new_msgs;
594         status->unread += item->unread_msgs;
595         status->total += item->total_msgs;
596
597         if (status->str) {
598                 id = folder_item_get_identifier(item);
599                 g_string_sprintfa(status->str, "%5d %5d %5d %s\n",
600                                   item->new_msgs, item->unread_msgs,
601                                   item->total_msgs, id);
602                 g_free(id);
603         }
604  
605         return FALSE;
606  }
607  
608 static void folder_get_status_full_all(GString *str, guint *new, guint *unread,
609                                        guint *total)
610 {
611         GList *list;
612         Folder *folder;
613         struct TotalMsgStatus status;
614  
615         status.new = status.unread = status.total = 0;
616         status.str = str;
617  
618         debug_print("Counting total number of messages...\n");
619  
620         for (list = folder_list; list != NULL; list = list->next) {
621                 folder = FOLDER(list->data);
622                 if (folder->node)
623                         g_node_traverse(folder->node, G_PRE_ORDER,
624                                         G_TRAVERSE_ALL, -1,
625                                         folder_get_status_full_all_func,
626                                         &status);
627         }
628  
629         *new = status.new;
630         *unread = status.unread;
631         *total = status.total;
632 }
633
634 gchar *folder_get_status(GPtrArray *folders, gboolean full)
635 {
636         guint new, unread, total;
637         GString *str;
638         gint i;
639         gchar *ret;
640
641         new = unread = total = 0;
642
643         str = g_string_new(NULL);
644
645         if (folders) {
646                 for (i = 0; i < folders->len; i++) {
647                         FolderItem *item;
648
649                         item = g_ptr_array_index(folders, i);
650                         new += item->new_msgs;
651                         unread += item->unread_msgs;
652                         total += item->total_msgs;
653
654                         if (full) {
655                                 gchar *id;
656
657                                 id = folder_item_get_identifier(item);
658                                 g_string_sprintfa(str, "%5d %5d %5d %s\n",
659                                                   item->new_msgs, item->unread_msgs,
660                                                   item->total_msgs, id);
661                                 g_free(id);
662                         }
663                 }
664         } else {
665                 folder_get_status_full_all(full ? str : NULL,
666                                            &new, &unread, &total);
667         }
668
669         if (full)
670                 g_string_sprintfa(str, "%5d %5d %5d\n", new, unread, total);
671         else
672                 g_string_sprintfa(str, "%d %d %d\n", new, unread, total);
673
674         ret = str->str;
675         g_string_free(str, FALSE);
676  
677         return ret;
678 }
679
680 void folder_count_total_msgs(guint *new_msgs, guint *unread_msgs, guint *unreadmarked_msgs, guint *total_msgs)
681 {
682         struct TotalMsgCount count;
683
684         count.new_msgs = count.unread_msgs = count.unreadmarked_msgs = count.total_msgs = 0;
685
686         debug_print("Counting total number of messages...\n");
687
688         folder_func_to_all_folders(folder_count_total_msgs_func, &count);
689
690         *new_msgs = count.new_msgs;
691         *unread_msgs = count.unread_msgs;
692         *unreadmarked_msgs = count.unreadmarked_msgs;
693         *total_msgs = count.total_msgs;
694 }
695
696 Folder *folder_find_from_path(const gchar *path)
697 {
698         GList *list;
699         Folder *folder;
700
701         for (list = folder_list; list != NULL; list = list->next) {
702                 folder = list->data;
703                 if ((FOLDER_TYPE(folder) == F_MH || FOLDER_TYPE(folder) == F_MBOX) &&
704                     !path_cmp(LOCAL_FOLDER(folder)->rootpath, path))
705                         return folder;
706         }
707
708         return NULL;
709 }
710
711 Folder *folder_find_from_name(const gchar *name, FolderClass *klass)
712 {
713         GList *list;
714         Folder *folder;
715
716         for (list = folder_list; list != NULL; list = list->next) {
717                 folder = list->data;
718                 if (folder->klass == klass && strcmp2(name, folder->name) == 0)
719                         return folder;
720         }
721
722         return NULL;
723 }
724
725 static gboolean folder_item_find_func(GNode *node, gpointer data)
726 {
727         FolderItem *item = node->data;
728         gpointer *d = data;
729         const gchar *path = d[0];
730
731         if (path_cmp(path, item->path) != 0)
732                 return FALSE;
733
734         d[1] = item;
735
736         return TRUE;
737 }
738
739 FolderItem *folder_find_item_from_path(const gchar *path)
740 {
741         Folder *folder;
742         gpointer d[2];
743
744         folder = folder_get_default_folder();
745         g_return_val_if_fail(folder != NULL, NULL);
746
747         d[0] = (gpointer)path;
748         d[1] = NULL;
749         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
750                         folder_item_find_func, d);
751         return d[1];
752 }
753
754 FolderClass *folder_get_class_from_string(const gchar *str)
755 {
756         GSList *classlist;
757
758         classlist = folder_get_class_list();
759         for (; classlist != NULL; classlist = g_slist_next(classlist)) {
760                 FolderClass *class = (FolderClass *) classlist->data;
761                 if (g_strcasecmp(class->idstr, str) == 0)
762                         return class;
763         }
764
765         return NULL;
766 }
767
768 gchar *folder_get_identifier(Folder *folder)
769 {
770         gchar *type_str;
771
772         g_return_val_if_fail(folder != NULL, NULL);
773
774         type_str = folder->klass->idstr;
775         return g_strconcat("#", type_str, "/", folder->name, NULL);
776 }
777
778 gchar *folder_item_get_identifier(FolderItem *item)
779 {
780         gchar *id;
781         gchar *folder_id;
782
783         g_return_val_if_fail(item != NULL, NULL);
784         g_return_val_if_fail(item->path != NULL, NULL);
785
786         folder_id = folder_get_identifier(item->folder);
787         id = g_strconcat(folder_id, "/", item->path, NULL);
788         g_free(folder_id);
789
790         return id;
791 }
792
793 FolderItem *folder_find_item_from_identifier(const gchar *identifier)
794 {
795         Folder *folder;
796         gpointer d[2];
797         gchar *str;
798         gchar *p;
799         gchar *name;
800         gchar *path;
801         FolderClass *class;
802
803         g_return_val_if_fail(identifier != NULL, NULL);
804
805         if (*identifier != '#')
806                 return folder_find_item_from_path(identifier);
807
808         Xstrdup_a(str, identifier, return NULL);
809
810         p = strchr(str, '/');
811         if (!p)
812                 return folder_find_item_from_path(identifier);
813         *p = '\0';
814         p++;
815         class = folder_get_class_from_string(&str[1]);
816         if (class == NULL)
817                 return folder_find_item_from_path(identifier);
818
819         name = p;
820         p = strchr(p, '/');
821         if (!p)
822                 return folder_find_item_from_path(identifier);
823         *p = '\0';
824         p++;
825
826         folder = folder_find_from_name(name, class);
827         if (!folder)
828                 return folder_find_item_from_path(identifier);
829
830         path = p;
831
832         d[0] = (gpointer)path;
833         d[1] = NULL;
834         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
835                         folder_item_find_func, d);
836         return d[1];
837 }
838
839 /**
840  * Get a displayable name for a FolderItem
841  *
842  * \param item FolderItem for that a name should be created
843  * \return Displayable name for item, returned string has to
844  *         be freed
845  */
846 gchar *folder_item_get_name(FolderItem *item)
847 {
848         gchar *name = NULL;
849
850         g_return_val_if_fail(item != NULL, g_strdup(""));
851
852         switch (item->stype) {
853         case F_INBOX:
854                 name = g_strdup(!strcmp2(item->name, INBOX_DIR) ? _("Inbox") :
855                                 item->name);
856                 break;
857         case F_OUTBOX:
858                 name = g_strdup(!strcmp2(item->name, OUTBOX_DIR) ? _("Sent") :
859                                 item->name);
860                 break;
861         case F_QUEUE:
862                 name = g_strdup(!strcmp2(item->name, QUEUE_DIR) ? _("Queue") :
863                                 item->name);
864                 break;
865         case F_TRASH:
866                 name = g_strdup(!strcmp2(item->name, TRASH_DIR) ? _("Trash") :
867                                 item->name);
868                 break;
869         case F_DRAFT:
870                 name = g_strdup(!strcmp2(item->name, DRAFT_DIR) ? _("Drafts") :
871                                 item->name);
872                 break;
873         default:
874                 break;
875         }
876
877         if (name == NULL) {
878                 /*
879                  * should probably be done by a virtual function,
880                  * the folder knows the ui string and how to abbrev
881                 */
882                 if (!item->parent) {
883                         name = g_strconcat(item->name, " (", item->folder->klass->uistr, ")", NULL);
884                 } else {
885                         if (FOLDER_CLASS(item->folder) == news_get_class() &&
886                             item->path && !strcmp2(item->name, item->path))
887                                 name = get_abbrev_newsgroup_name
888                                         (item->path,
889                                          prefs_common.ng_abbrev_len);
890                         else
891                                 name = g_strdup(item->name);
892                 }
893         }
894
895         if (name == NULL)
896                 name = g_strdup("");
897
898         return name;
899 }
900
901 Folder *folder_get_default_folder(void)
902 {
903         return folder_list ? FOLDER(folder_list->data) : NULL;
904 }
905
906 FolderItem *folder_get_default_inbox(void)
907 {
908         Folder *folder;
909
910         if (!folder_list) return NULL;
911         folder = FOLDER(folder_list->data);
912         g_return_val_if_fail(folder != NULL, NULL);
913         return folder->inbox;
914 }
915
916 FolderItem *folder_get_default_outbox(void)
917 {
918         Folder *folder;
919
920         if (!folder_list) return NULL;
921         folder = FOLDER(folder_list->data);
922         g_return_val_if_fail(folder != NULL, NULL);
923         return folder->outbox;
924 }
925
926 FolderItem *folder_get_default_draft(void)
927 {
928         Folder *folder;
929
930         if (!folder_list) return NULL;
931         folder = FOLDER(folder_list->data);
932         g_return_val_if_fail(folder != NULL, NULL);
933         return folder->draft;
934 }
935
936 FolderItem *folder_get_default_queue(void)
937 {
938         Folder *folder;
939
940         if (!folder_list) return NULL;
941         folder = FOLDER(folder_list->data);
942         g_return_val_if_fail(folder != NULL, NULL);
943         return folder->queue;
944 }
945
946 FolderItem *folder_get_default_trash(void)
947 {
948         Folder *folder;
949
950         if (!folder_list) return NULL;
951         folder = FOLDER(folder_list->data);
952         g_return_val_if_fail(folder != NULL, NULL);
953         return folder->trash;
954 }
955
956 #define CREATE_FOLDER_IF_NOT_EXIST(member, dir, type)           \
957 {                                                               \
958         if (!folder->member) {                                  \
959                 item = folder_item_new(folder, dir, dir);       \
960                 item->stype = type;                             \
961                 folder_item_append(rootitem, item);             \
962                 folder->member = item;                          \
963         }                                                       \
964 }
965
966 void folder_set_missing_folders(void)
967 {
968         Folder *folder;
969         FolderItem *rootitem;
970         FolderItem *item;
971         GList *list;
972
973         for (list = folder_list; list != NULL; list = list->next) {
974                 folder = list->data;
975                 if (FOLDER_TYPE(folder) != F_MH) continue;
976                 rootitem = FOLDER_ITEM(folder->node->data);
977                 g_return_if_fail(rootitem != NULL);
978
979                 if (folder->inbox && folder->outbox && folder->draft &&
980                     folder->queue && folder->trash)
981                         continue;
982
983                 if (folder->klass->create_tree(folder) < 0) {
984                         g_warning("%s: can't create the folder tree.\n",
985                                   LOCAL_FOLDER(folder)->rootpath);
986                         continue;
987                 }
988
989                 CREATE_FOLDER_IF_NOT_EXIST(inbox,  INBOX_DIR,  F_INBOX);
990                 CREATE_FOLDER_IF_NOT_EXIST(outbox, OUTBOX_DIR, F_OUTBOX);
991                 CREATE_FOLDER_IF_NOT_EXIST(draft,  DRAFT_DIR,  F_DRAFT);
992                 CREATE_FOLDER_IF_NOT_EXIST(queue,  QUEUE_DIR,  F_QUEUE);
993                 CREATE_FOLDER_IF_NOT_EXIST(trash,  TRASH_DIR,  F_TRASH);
994         }
995 }
996
997 static gboolean folder_unref_account_func(GNode *node, gpointer data)
998 {
999         FolderItem *item = node->data;
1000         PrefsAccount *account = data;
1001
1002         if (item->account == account)
1003                 item->account = NULL;
1004
1005         return FALSE;
1006 }
1007
1008 void folder_unref_account_all(PrefsAccount *account)
1009 {
1010         Folder *folder;
1011         GList *list;
1012
1013         if (!account) return;
1014
1015         for (list = folder_list; list != NULL; list = list->next) {
1016                 folder = list->data;
1017                 if (folder->account == account)
1018                         folder->account = NULL;
1019                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1020                                 folder_unref_account_func, account);
1021         }
1022 }
1023
1024 #undef CREATE_FOLDER_IF_NOT_EXIST
1025
1026 gchar *folder_item_get_path(FolderItem *item)
1027 {
1028         Folder *folder;
1029
1030         g_return_val_if_fail(item != NULL, NULL);
1031         folder = item->folder;
1032         g_return_val_if_fail(folder != NULL, NULL);
1033
1034         return folder->klass->item_get_path(folder, item);
1035 }
1036
1037 void folder_item_set_default_flags(FolderItem *dest, MsgFlags *flags)
1038 {
1039         if (!(dest->stype == F_OUTBOX ||
1040               dest->stype == F_QUEUE  ||
1041               dest->stype == F_DRAFT  ||
1042               dest->stype == F_TRASH)) {
1043                 flags->perm_flags = MSG_NEW|MSG_UNREAD;
1044         } else {
1045                 flags->perm_flags = 0;
1046         }
1047         flags->tmp_flags = MSG_CACHED;
1048         if (FOLDER_TYPE(dest->folder) == F_MH) {
1049                 if (dest->stype == F_QUEUE) {
1050                         MSG_SET_TMP_FLAGS(*flags, MSG_QUEUED);
1051                 } else if (dest->stype == F_DRAFT) {
1052                         MSG_SET_TMP_FLAGS(*flags, MSG_DRAFT);
1053                 }
1054         }
1055 }
1056
1057 static gint folder_sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1058 {
1059         MsgInfo *msginfo_a = (MsgInfo *) a;
1060         MsgInfo *msginfo_b = (MsgInfo *) b;
1061
1062         return (msginfo_a->msgnum - msginfo_b->msgnum);
1063 }
1064
1065 static gint folder_sort_folder_list(gconstpointer a, gconstpointer b)
1066 {
1067         guint gint_a = GPOINTER_TO_INT(a);
1068         guint gint_b = GPOINTER_TO_INT(b);
1069         
1070         return (gint_a - gint_b);
1071 }
1072
1073 gint folder_item_open(FolderItem *item)
1074 {
1075         if((item->folder->klass->scan_required != NULL) && (item->folder->klass->scan_required(item->folder, item))) {
1076                 folder_item_scan_full(item, TRUE);
1077         }
1078
1079         /* Processing */
1080         if(item->prefs->processing != NULL) {
1081                 gchar *buf;
1082                 
1083                 buf = g_strdup_printf(_("Processing (%s)...\n"), item->path);
1084                 debug_print("%s\n", buf);
1085                 g_free(buf);
1086         
1087                 folder_item_apply_processing(item);
1088
1089                 debug_print("done.\n");
1090         }
1091
1092         return 0;
1093 }
1094
1095 gint folder_item_close(FolderItem *item)
1096 {
1097         GSList *mlist, *cur;
1098         Folder *folder;
1099         
1100         g_return_val_if_fail(item != NULL, -1);
1101
1102         if (item->new_msgs) {
1103                 folder_item_update_freeze();
1104                 mlist = folder_item_get_msg_list(item);
1105                 for (cur = mlist ; cur != NULL ; cur = cur->next) {
1106                         MsgInfo * msginfo;
1107
1108                         msginfo = (MsgInfo *) cur->data;
1109                         if (MSG_IS_NEW(msginfo->flags))
1110                                 procmsg_msginfo_unset_flags(msginfo, MSG_NEW, 0);
1111                         procmsg_msginfo_free(msginfo);
1112                 }
1113                 g_slist_free(mlist);
1114                 folder_item_update_thaw();
1115         }               
1116
1117         folder_item_write_cache(item);
1118         
1119         folder_item_update(item, F_ITEM_UPDATE_MSGCNT);
1120
1121         item->opened = FALSE;
1122         folder = item->folder;
1123
1124         if (folder->klass->close == NULL)
1125                 return 0;
1126
1127         return folder->klass->close(folder, item);
1128 }
1129
1130 gint folder_item_scan_full(FolderItem *item, gboolean filtering)
1131 {
1132         Folder *folder;
1133         GSList *folder_list = NULL, *cache_list = NULL;
1134         GSList *folder_list_cur, *cache_list_cur, *new_list = NULL;
1135         GSList *exists_list = NULL, *elem;
1136         GSList *newmsg_list = NULL;
1137         guint newcnt = 0, unreadcnt = 0, totalcnt = 0, unreadmarkedcnt = 0;
1138         guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
1139         gboolean update_flags = 0, old_uids_valid = FALSE;
1140     
1141         g_return_val_if_fail(item != NULL, -1);
1142         if (item->path == NULL) return -1;
1143
1144         folder = item->folder;
1145
1146         g_return_val_if_fail(folder != NULL, -1);
1147         g_return_val_if_fail(folder->klass->get_num_list != NULL, -1);
1148
1149         debug_print("Scanning folder %s for cache changes.\n", item->path);
1150
1151         /* Get list of messages for folder and cache */
1152         if (folder->klass->get_num_list(item->folder, item, &folder_list, &old_uids_valid) < 0) {
1153                 debug_print("Error fetching list of message numbers\n");
1154                 return(-1);
1155         }
1156
1157         if (old_uids_valid) {
1158                 if (!item->cache)
1159                         folder_item_read_cache(item);
1160                 cache_list = msgcache_get_msg_list(item->cache);
1161         } else {
1162                 if (item->cache)
1163                         msgcache_destroy(item->cache);
1164                 item->cache = msgcache_new();
1165                 cache_list = NULL;
1166         }
1167
1168         /* Sort both lists */
1169         cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
1170         folder_list = g_slist_sort(folder_list, folder_sort_folder_list);
1171
1172         cache_list_cur = cache_list;
1173         folder_list_cur = folder_list;
1174
1175         if (cache_list_cur != NULL) {
1176                 GSList *cache_list_last;
1177         
1178                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1179                 cache_list_last = g_slist_last(cache_list);
1180                 cache_max_num = ((MsgInfo *)cache_list_last->data)->msgnum;
1181         } else {
1182                 cache_cur_num = G_MAXINT;
1183                 cache_max_num = 0;
1184         }
1185
1186         if (folder_list_cur != NULL) {
1187                 GSList *folder_list_last;
1188         
1189                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1190                 folder_list_last = g_slist_last(folder_list);
1191                 folder_max_num = GPOINTER_TO_INT(folder_list_last->data);
1192         } else {
1193                 folder_cur_num = G_MAXINT;
1194                 folder_max_num = 0;
1195         }
1196
1197         while ((cache_cur_num != G_MAXINT) || (folder_cur_num != G_MAXINT)) {
1198                 /*
1199                  *  Message only exists in the folder
1200                  *  Remember message for fetching
1201                  */
1202                 if (folder_cur_num < cache_cur_num) {
1203                         gboolean add = FALSE;
1204
1205                         switch(FOLDER_TYPE(folder)) {
1206                                 case F_NEWS:
1207                                         if (folder_cur_num < cache_max_num)
1208                                                 break;
1209                                         
1210                                         if (folder->account->max_articles == 0) {
1211                                                 add = TRUE;
1212                                         }
1213
1214                                         if (folder_max_num <= folder->account->max_articles) {
1215                                                 add = TRUE;
1216                                         } else if (folder_cur_num > (folder_max_num - folder->account->max_articles)) {
1217                                                 add = TRUE;
1218                                         }
1219                                         break;
1220                                 default:
1221                                         add = TRUE;
1222                                         break;
1223                         }
1224                         
1225                         if (add) {
1226                                 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(folder_cur_num));
1227                                 debug_print("Remembered message %d for fetching\n", folder_cur_num);
1228                         }
1229
1230                         /* Move to next folder number */
1231                         folder_list_cur = folder_list_cur->next;
1232
1233                         if (folder_list_cur != NULL)
1234                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1235                         else
1236                                 folder_cur_num = G_MAXINT;
1237
1238                         continue;
1239                 }
1240
1241                 /*
1242                  *  Message only exists in the cache
1243                  *  Remove the message from the cache
1244                  */
1245                 if (cache_cur_num < folder_cur_num) {
1246                         msgcache_remove_msg(item->cache, cache_cur_num);
1247                         debug_print("Removed message %d from cache.\n", cache_cur_num);
1248
1249                         /* Move to next cache number */
1250                         cache_list_cur = cache_list_cur->next;
1251
1252                         if (cache_list_cur != NULL)
1253                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1254                         else
1255                                 cache_cur_num = G_MAXINT;
1256
1257                         update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1258
1259                         continue;
1260                 }
1261
1262                 /*
1263                  *  Message number exists in folder and cache!
1264                  *  Check if the message has been modified
1265                  */
1266                 if (cache_cur_num == folder_cur_num) {
1267                         MsgInfo *msginfo;
1268
1269                         msginfo = msgcache_get_msg(item->cache, folder_cur_num);
1270                         if (folder->klass->is_msg_changed && folder->klass->is_msg_changed(folder, item, msginfo)) {
1271                                 msgcache_remove_msg(item->cache, msginfo->msgnum);
1272                                 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(msginfo->msgnum));
1273                                 procmsg_msginfo_free(msginfo);
1274
1275                                 debug_print("Remembering message %d to update...\n", folder_cur_num);
1276                         } else
1277                                 exists_list = g_slist_prepend(exists_list, msginfo);
1278
1279                         /* Move to next folder and cache number */
1280                         cache_list_cur = cache_list_cur->next;
1281                         folder_list_cur = folder_list_cur->next;
1282
1283                         if (cache_list_cur != NULL)
1284                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1285                         else
1286                                 cache_cur_num = G_MAXINT;
1287
1288                         if (folder_list_cur != NULL)
1289                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1290                         else
1291                                 folder_cur_num = G_MAXINT;
1292
1293                         continue;
1294                 }
1295         }
1296         
1297         for(cache_list_cur = cache_list; cache_list_cur != NULL; cache_list_cur = g_slist_next(cache_list_cur))
1298                 procmsg_msginfo_free((MsgInfo *) cache_list_cur->data);
1299
1300         g_slist_free(cache_list);
1301         g_slist_free(folder_list);
1302
1303         if (new_list != NULL) {
1304                 if (folder->klass->get_msginfos) {
1305                         newmsg_list = folder->klass->get_msginfos(folder, item, new_list);
1306                 } else if (folder->klass->get_msginfo) {
1307                         GSList *elem;
1308         
1309                         for (elem = new_list; elem != NULL; elem = g_slist_next(elem)) {
1310                                 MsgInfo *msginfo;
1311                                 guint num;
1312
1313                                 num = GPOINTER_TO_INT(elem->data);
1314                                 msginfo = folder->klass->get_msginfo(folder, item, num);
1315                                 if (msginfo != NULL) {
1316                                         newmsg_list = g_slist_prepend(newmsg_list, msginfo);
1317                                         debug_print("Added newly found message %d to cache.\n", num);
1318                                 }
1319                         }
1320                 }
1321                 g_slist_free(new_list);
1322         }
1323
1324         folder_item_update_freeze();
1325         if (newmsg_list != NULL) {
1326                 GSList *elem;
1327
1328                 for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
1329                         MsgInfo *msginfo = (MsgInfo *) elem->data;
1330
1331                         msgcache_add_msg(item->cache, msginfo);
1332                         if ((filtering == TRUE) &&
1333                             (item->stype == F_INBOX) &&
1334                             (item->folder->account != NULL) && 
1335                             (item->folder->account->filter_on_recv) &&
1336                             procmsg_msginfo_filter(msginfo))
1337                                 procmsg_msginfo_free(msginfo);
1338                         else
1339                                 exists_list = g_slist_prepend(exists_list, msginfo);
1340                 }
1341                 g_slist_free(newmsg_list);
1342
1343                 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1344         }
1345
1346         for (elem = exists_list; elem != NULL; elem = g_slist_next(elem)) {
1347                 MsgInfo *msginfo;
1348
1349                 msginfo = elem->data;
1350                 if (MSG_IS_IGNORE_THREAD(msginfo->flags) && (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1351                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1352                 if (!MSG_IS_IGNORE_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD)) {
1353                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1354                         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
1355                 }
1356                 if ((item->stype == F_OUTBOX ||
1357                      item->stype == F_QUEUE  ||
1358                      item->stype == F_DRAFT  ||
1359                      item->stype == F_TRASH) &&
1360                     (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1361                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1362                 if (MSG_IS_NEW(msginfo->flags))
1363                         newcnt++;
1364                 if (MSG_IS_UNREAD(msginfo->flags))
1365                         unreadcnt++;
1366                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1367                         unreadmarkedcnt++;
1368                 totalcnt++;
1369
1370                 procmsg_msginfo_free(msginfo);
1371         }
1372         g_slist_free(exists_list);
1373
1374         item->new_msgs = newcnt;
1375         item->unread_msgs = unreadcnt;
1376         item->total_msgs = totalcnt;
1377         item->unreadmarked_msgs = unreadmarkedcnt;
1378
1379         update_flags |= F_ITEM_UPDATE_MSGCNT;
1380
1381         folder_item_update(item, update_flags);
1382         folder_item_update_thaw();
1383
1384         return 0;
1385 }
1386
1387 gint folder_item_scan(FolderItem *item)
1388 {
1389         return folder_item_scan_full(item, TRUE);
1390 }
1391
1392 static gboolean folder_scan_all_items_func(GNode *node, gpointer data)
1393 {
1394         FolderItem *item = node->data;
1395
1396         folder_item_scan(item);
1397
1398         return FALSE;
1399 }
1400
1401 void folder_scan_all_items(Folder * folder)
1402 {
1403         g_node_traverse(folder->node, G_PRE_ORDER,
1404                         G_TRAVERSE_ALL, -1, folder_scan_all_items_func, NULL);
1405 }
1406
1407 static void folder_item_scan_foreach_func(gpointer key, gpointer val,
1408                                           gpointer data)
1409 {
1410         folder_item_scan(FOLDER_ITEM(key));
1411 }
1412
1413 void folder_item_scan_foreach(GHashTable *table)
1414 {
1415         g_hash_table_foreach(table, folder_item_scan_foreach_func, NULL);
1416 }
1417
1418 void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
1419 {
1420         gint *memusage = (gint *)data;
1421
1422         if (item->cache == NULL)
1423                 return;
1424         
1425         *memusage += msgcache_get_memory_usage(item->cache);
1426 }
1427
1428 gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
1429 {
1430         FolderItem *fa = (FolderItem *)a;
1431         FolderItem *fb = (FolderItem *)b;
1432         
1433         return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
1434 }
1435
1436 void folder_find_expired_caches(FolderItem *item, gpointer data)
1437 {
1438         GSList **folder_item_list = (GSList **)data;
1439         gint difftime, expiretime;
1440         
1441         if (item->cache == NULL)
1442                 return;
1443
1444         if (item->opened > 0)
1445                 return;
1446
1447         difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
1448         expiretime = prefs_common.cache_min_keep_time * 60;
1449         debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
1450         if (difftime > expiretime) {
1451                 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
1452         }
1453 }
1454
1455 void folder_item_free_cache(FolderItem *item)
1456 {
1457         g_return_if_fail(item != NULL);
1458         
1459         if (item->cache == NULL)
1460                 return;
1461         
1462         if (item->opened > 0)
1463                 return;
1464
1465         folder_item_write_cache(item);
1466         msgcache_destroy(item->cache);
1467         item->cache = NULL;
1468 }
1469
1470 void folder_clean_cache_memory(void)
1471 {
1472         gint memusage = 0;
1473
1474         folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);       
1475         debug_print("Total cache memory usage: %d\n", memusage);
1476         
1477         if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
1478                 GSList *folder_item_list = NULL, *listitem;
1479                 
1480                 debug_print("Trying to free cache memory\n");
1481
1482                 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);      
1483                 listitem = folder_item_list;
1484                 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
1485                         FolderItem *item = (FolderItem *)(listitem->data);
1486
1487                         debug_print("Freeing cache memory for %s\n", item->path);
1488                         memusage -= msgcache_get_memory_usage(item->cache);
1489                         folder_item_free_cache(item);
1490                         listitem = listitem->next;
1491                 }
1492                 g_slist_free(folder_item_list);
1493         }
1494 }
1495
1496 void folder_item_read_cache(FolderItem *item)
1497 {
1498         gchar *cache_file, *mark_file;
1499         
1500         g_return_if_fail(item != NULL);
1501
1502         cache_file = folder_item_get_cache_file(item);
1503         mark_file = folder_item_get_mark_file(item);
1504         item->cache = msgcache_read_cache(item, cache_file);
1505         if (!item->cache) {
1506                 item->cache = msgcache_new();
1507                 folder_item_scan_full(item, TRUE);
1508         }
1509         msgcache_read_mark(item->cache, mark_file);
1510         g_free(cache_file);
1511         g_free(mark_file);
1512
1513         folder_clean_cache_memory();
1514 }
1515
1516 void folder_item_write_cache(FolderItem *item)
1517 {
1518         gchar *cache_file, *mark_file;
1519         FolderItemPrefs *prefs;
1520         gint filemode = 0;
1521         gchar *id;
1522         
1523         if (!item || !item->path || !item->cache)
1524                 return;
1525
1526         id = folder_item_get_identifier(item);
1527         debug_print("Save cache for folder %s\n", id);
1528         g_free(id);
1529
1530         cache_file = folder_item_get_cache_file(item);
1531         mark_file = folder_item_get_mark_file(item);
1532         if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
1533                 prefs = item->prefs;
1534                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
1535                         /* for cache file */
1536                         filemode = prefs->folder_chmod;
1537                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
1538                         if (filemode & S_IROTH) filemode |= S_IWOTH;
1539                         chmod(cache_file, filemode);
1540                 }
1541         }
1542
1543         g_free(cache_file);
1544         g_free(mark_file);
1545 }
1546
1547 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
1548 {
1549         Folder *folder;
1550         MsgInfo *msginfo;
1551         
1552         g_return_val_if_fail(item != NULL, NULL);
1553         
1554         folder = item->folder;
1555         if (!item->cache)
1556                 folder_item_read_cache(item);
1557         
1558         if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
1559                 return msginfo;
1560         
1561         g_return_val_if_fail(folder->klass->get_msginfo, NULL);
1562         if ((msginfo = folder->klass->get_msginfo(folder, item, num)) != NULL) {
1563                 msgcache_add_msg(item->cache, msginfo);
1564                 return msginfo;
1565         }
1566         
1567         return NULL;
1568 }
1569
1570 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
1571 {
1572         Folder *folder;
1573         MsgInfo *msginfo;
1574         
1575         g_return_val_if_fail(item != NULL, NULL);
1576         g_return_val_if_fail(msgid != NULL, NULL);
1577         
1578         folder = item->folder;
1579         if (!item->cache)
1580                 folder_item_read_cache(item);
1581         
1582         if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
1583                 return msginfo;
1584
1585         return NULL;
1586 }
1587
1588 GSList *folder_item_get_msg_list(FolderItem *item)
1589 {
1590         g_return_val_if_fail(item != NULL, NULL);
1591         
1592         if (item->cache == 0)
1593                 folder_item_read_cache(item);
1594
1595         g_return_val_if_fail(item->cache != NULL, NULL);
1596         
1597         return msgcache_get_msg_list(item->cache);
1598 }
1599
1600 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
1601 {
1602         Folder *folder;
1603
1604         g_return_val_if_fail(item != NULL, NULL);
1605
1606         folder = item->folder;
1607
1608         g_return_val_if_fail(folder->klass->fetch_msg != NULL, NULL);
1609
1610         return folder->klass->fetch_msg(folder, item, num);
1611 }
1612
1613 static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
1614 {
1615         static HeaderEntry hentry[] = {{"Message-ID:",  NULL, TRUE},
1616                                        {NULL,           NULL, FALSE}};
1617         FILE *fp;
1618         MsgInfo *msginfo;
1619         gint msgnum = 0;
1620         gchar buf[BUFFSIZE];
1621
1622         if ((fp = fopen(file, "rb")) == NULL)
1623                 return 0;
1624
1625         if ((dest->stype == F_QUEUE) || (dest->stype == F_DRAFT))
1626                 while (fgets(buf, sizeof(buf), fp) != NULL)
1627                         if (buf[0] == '\r' || buf[0] == '\n') break;
1628
1629         procheader_get_header_fields(fp, hentry);
1630         if (hentry[0].body) {
1631                 extract_parenthesis(hentry[0].body, '<', '>');
1632                 remove_space(hentry[0].body);
1633                 if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
1634                         msgnum = msginfo->msgnum;
1635                         procmsg_msginfo_free(msginfo);
1636
1637                         debug_print("found message as uid %d\n", msgnum);
1638                 }
1639         }
1640         
1641         g_free(hentry[0].body);
1642         hentry[0].body = NULL;
1643         fclose(fp);
1644
1645         return msgnum;
1646 }
1647
1648 static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
1649 {
1650         MsgPermFlags perm_flags = 0;
1651         MsgTmpFlags tmp_flags = 0;
1652
1653         /* create new flags */
1654         if (source != NULL) {
1655                 /* copy original flags */
1656                 perm_flags = source->flags.perm_flags;
1657                 tmp_flags = source->flags.tmp_flags;
1658         } else {
1659                 perm_flags = dest->flags.perm_flags;
1660                 tmp_flags = dest->flags.tmp_flags;
1661         }
1662
1663         /* remove new, unread and deleted in special folders */
1664         if (dest->folder->stype == F_OUTBOX ||
1665             dest->folder->stype == F_QUEUE  ||
1666             dest->folder->stype == F_DRAFT  ||
1667             dest->folder->stype == F_TRASH)
1668                 perm_flags &= ~(MSG_NEW | MSG_UNREAD | MSG_DELETED);
1669
1670         /* set ignore flag of ignored parent exists */
1671         if (procmsg_msg_has_flagged_parent(dest, MSG_IGNORE_THREAD))
1672                 perm_flags |= MSG_IGNORE_THREAD;
1673
1674         /* Unset tmp flags that should not be copied */
1675         tmp_flags &= ~(MSG_MOVE | MSG_COPY);
1676
1677         /* unset flags that are set but should not */
1678         procmsg_msginfo_unset_flags(dest,
1679                                     dest->flags.perm_flags & ~perm_flags,
1680                                     dest->flags.tmp_flags  & ~tmp_flags);
1681         /* set new flags */
1682         procmsg_msginfo_set_flags(dest,
1683                                   ~dest->flags.perm_flags & perm_flags,
1684                                   ~dest->flags.tmp_flags  & tmp_flags);
1685
1686         folder_item_update(dest->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1687 }
1688
1689 static void add_msginfo_to_cache(FolderItem *item, MsgInfo *newmsginfo, MsgInfo *flagsource)
1690 {
1691         /* update folder stats */
1692         if (MSG_IS_NEW(newmsginfo->flags))
1693                 item->new_msgs++;
1694         if (MSG_IS_UNREAD(newmsginfo->flags))
1695                 item->unread_msgs++;
1696         if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
1697                 item->unreadmarked_msgs++;
1698         item->total_msgs++;
1699
1700         copy_msginfo_flags(flagsource, newmsginfo);
1701
1702         msgcache_add_msg(item->cache, newmsginfo);
1703 }
1704
1705 static void remove_msginfo_from_cache(FolderItem *item, MsgInfo *msginfo)
1706 {
1707         MsgInfoUpdate msginfo_update;
1708
1709         if (!item->cache)
1710                 folder_item_read_cache(item);
1711
1712         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1713                 msginfo->folder->new_msgs--;
1714         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1715                 msginfo->folder->unread_msgs--;
1716         if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1717                 msginfo->folder->unreadmarked_msgs--;
1718         msginfo->folder->total_msgs--;
1719
1720         msginfo_update.msginfo = msginfo;
1721         msginfo_update.flags = MSGINFO_UPDATE_DELETED;
1722         hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
1723
1724         msgcache_remove_msg(item->cache, msginfo->msgnum);
1725         folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1726 }
1727
1728 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
1729                          MsgFlags *flags, gboolean remove_source)
1730 {
1731         GSList file_list;
1732         MsgFileInfo fileinfo;
1733
1734         g_return_val_if_fail(dest != NULL, -1);
1735         g_return_val_if_fail(file != NULL, -1);
1736  
1737         fileinfo.msginfo = NULL;
1738         fileinfo.file = (gchar *)file;
1739         fileinfo.flags = flags;
1740         file_list.data = &fileinfo;
1741         file_list.next = NULL;
1742
1743         return folder_item_add_msgs(dest, &file_list, remove_source);
1744 }
1745
1746 gint folder_item_add_msgs(FolderItem *dest, GSList *file_list,
1747                           gboolean remove_source)
1748 {
1749         Folder *folder;
1750         gint ret, num, lastnum = -1;
1751         GSList *file_cur;
1752         GRelation *relation;
1753         MsgFileInfo *fileinfo = NULL;
1754         gboolean folderscan = FALSE;
1755
1756         g_return_val_if_fail(dest != NULL, -1);
1757         g_return_val_if_fail(file_list != NULL, -1);
1758         g_return_val_if_fail(dest->folder != NULL, -1);
1759
1760         folder = dest->folder;
1761
1762         relation = g_relation_new(2);
1763         g_relation_index(relation, 0, g_direct_hash, g_direct_equal);
1764
1765         if (folder->klass->add_msgs != NULL) {
1766                 ret = folder->klass->add_msgs(folder, dest, file_list, relation);
1767                 if (ret < 0) {
1768                         g_relation_destroy(relation);
1769                         return ret;
1770                 }
1771         } else {
1772                 for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
1773                         fileinfo = (MsgFileInfo *) file_cur->data;
1774
1775                         ret = folder->klass->add_msg(folder, dest, fileinfo->file, fileinfo->flags);
1776                         if (ret < 0) {
1777                                 g_relation_destroy(relation);
1778                                 return ret;
1779                         }
1780                         g_relation_insert(relation, fileinfo, GINT_TO_POINTER(ret));
1781                 }
1782         }
1783
1784         for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
1785                 GTuples *tuples;
1786
1787                 fileinfo = (MsgFileInfo *) file_cur->data;
1788                 tuples = g_relation_select(relation, fileinfo, 0);
1789                 num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1790                 g_tuples_destroy(tuples);
1791
1792                 if (num >= 0) {
1793                         MsgInfo *newmsginfo;
1794
1795                         if (num == 0) {
1796                                 if (!folderscan) {
1797                                         folder_item_scan_full(dest, FALSE);
1798                                         folderscan = TRUE;
1799                                 }
1800                                 num = folder_item_get_msg_num_by_file(dest, fileinfo->file);
1801                         }
1802
1803                         if (num > lastnum)
1804                                 lastnum = num;
1805
1806                         if (num >= 0 && remove_source) {
1807                                 if (unlink(fileinfo->file) < 0)
1808                                         FILE_OP_ERROR(fileinfo->file, "unlink");
1809                         }
1810
1811                         if (num == 0)
1812                                 continue;
1813
1814                         if (!folderscan && 
1815                             ((newmsginfo = folder->klass->get_msginfo(folder, dest, num)) != NULL)) {
1816                                 add_msginfo_to_cache(dest, newmsginfo, NULL);
1817                                 procmsg_msginfo_free(newmsginfo);
1818                         } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
1819                                 /* TODO: set default flags */
1820                                 procmsg_msginfo_free(newmsginfo);
1821                         }
1822                 }
1823         }
1824
1825         g_relation_destroy(relation);
1826
1827         return lastnum;
1828 }
1829
1830 /*
1831 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1832 {
1833         Folder *folder;
1834         gint num;
1835
1836         g_return_val_if_fail(dest != NULL, -1);
1837         g_return_val_if_fail(msginfo != NULL, -1);
1838
1839         folder = dest->folder;
1840         if (dest->last_num < 0) folder->scan(folder, dest);
1841
1842         num = folder->move_msg(folder, dest, msginfo);
1843         if (num > 0) dest->last_num = num;
1844
1845         return num;
1846 }
1847 */
1848                 
1849 FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest) 
1850 {
1851         GSList *mlist;
1852         FolderItem *new_item;
1853         FolderItem *next_item;
1854         GNode *srcnode;
1855         gchar *old_id, *new_id;
1856
1857         mlist = folder_item_get_msg_list(src);
1858
1859         /* move messages */
1860         debug_print("Moving %s to %s\n", src->path, dest->path);
1861         new_item = folder_create_folder(dest, g_basename(src->path));
1862         if (new_item == NULL) {
1863                 printf("Can't create folder\n");
1864                 return NULL;
1865         }
1866         
1867         if (new_item->folder == NULL)
1868                 new_item->folder = dest->folder;
1869
1870         /* move messages */
1871         log_message(_("Moving %s to %s...\n"), 
1872                         src->name, new_item->path);
1873         folder_item_move_msgs(new_item, mlist);
1874         
1875         /*copy prefs*/
1876         folder_item_prefs_copy_prefs(src, new_item);
1877         new_item->collapsed = src->collapsed;
1878         new_item->thread_collapsed = src->thread_collapsed;
1879         new_item->threaded  = src->threaded;
1880         new_item->ret_rcpt  = src->ret_rcpt;
1881         new_item->hide_read_msgs = src->hide_read_msgs;
1882         new_item->sort_key  = src->sort_key;
1883         new_item->sort_type = src->sort_type;
1884
1885         prefs_matcher_write_config();
1886         
1887         /* recurse */
1888         srcnode = src->folder->node;    
1889         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
1890         srcnode = srcnode->children;
1891         while (srcnode != NULL) {
1892                 if (srcnode && srcnode->data) {
1893                         next_item = (FolderItem*) srcnode->data;
1894                         srcnode = srcnode->next;
1895                         if (folder_item_move_recursive(next_item, new_item) == NULL)
1896                                 return NULL;
1897                 }
1898         }
1899         old_id = folder_item_get_identifier(src);
1900         new_id = folder_item_get_identifier(new_item);
1901         debug_print("updating rules : %s => %s\n", old_id, new_id);
1902         
1903         src->folder->klass->remove_folder(src->folder, src);
1904         folder_write_list();
1905
1906         if (old_id != NULL && new_id != NULL)
1907                 prefs_filtering_rename_path(old_id, new_id);
1908         g_free(old_id);
1909         g_free(new_id);
1910
1911         return new_item;
1912 }
1913
1914 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item)
1915 {
1916         FolderItem *tmp = dest->parent;
1917         gchar * src_identifier, * dst_identifier;
1918         gchar * phys_srcpath, * phys_dstpath;
1919         
1920         while (tmp) {
1921                 if (tmp == src) {
1922                         return F_MOVE_FAILED_DEST_IS_CHILD;
1923                 }
1924                 tmp = tmp->parent;
1925         }
1926         
1927         tmp = src->parent;
1928         
1929         src_identifier = folder_item_get_identifier(src);
1930         dst_identifier = folder_item_get_identifier(dest);
1931         
1932         if(dst_identifier == NULL && dest->folder && dest->parent == NULL) {
1933                 /* dest can be a root folder */
1934                 dst_identifier = folder_get_identifier(dest->folder);
1935         }
1936         if (src_identifier == NULL || dst_identifier == NULL) {
1937                 debug_print("Can't get identifiers\n");
1938                 return F_MOVE_FAILED;
1939         }
1940
1941         if (src->folder != dest->folder) {
1942                 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
1943         }
1944
1945         phys_srcpath = folder_item_get_path(src);
1946         phys_dstpath = g_strconcat(folder_item_get_path(dest),G_DIR_SEPARATOR_S,g_basename(phys_srcpath),NULL);
1947
1948         if (src->parent == dest || src == dest) {
1949                 g_free(src_identifier);
1950                 g_free(dst_identifier);
1951                 g_free(phys_srcpath);
1952                 g_free(phys_dstpath);
1953                 return F_MOVE_FAILED_DEST_IS_PARENT;
1954         }
1955         debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
1956         if ((tmp = folder_item_move_recursive(src, dest)) == NULL) {
1957                 return F_MOVE_FAILED;
1958         }
1959         
1960         g_free(src_identifier);
1961         g_free(dst_identifier);
1962         g_free(phys_srcpath);
1963         g_free(phys_dstpath);
1964
1965         *new_item = tmp;
1966
1967         return F_MOVE_OK;
1968 }
1969
1970 /**
1971  * Copy a list of message to a new folder and remove
1972  * source messages if wanted
1973  */
1974 static gint do_copy_msgs(FolderItem *dest, GSList *msglist, gboolean remove_source)
1975 {
1976         Folder *folder;
1977         GSList *l;
1978         gint num, lastnum = -1;
1979         gboolean folderscan = FALSE;
1980         GRelation *relation;
1981
1982         g_return_val_if_fail(dest != NULL, -1);
1983         g_return_val_if_fail(msglist != NULL, -1);
1984
1985         folder = dest->folder;
1986
1987         g_return_val_if_fail(folder->klass->copy_msg != NULL, -1);
1988
1989         relation = g_relation_new(2);
1990         g_relation_index(relation, 0, g_direct_hash, g_direct_equal);
1991         g_relation_index(relation, 1, g_direct_hash, g_direct_equal);
1992
1993         /* 
1994          * Copy messages to destination folder and 
1995          * store new message numbers in newmsgnums
1996          */
1997         if (folder->klass->copy_msgs != NULL) {
1998                 if (folder->klass->copy_msgs(folder, dest, msglist, relation) < 0) {
1999                         g_relation_destroy(relation);
2000                         return -1;
2001                 }
2002         } else {
2003                 for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
2004                         MsgInfo * msginfo = (MsgInfo *) l->data;
2005
2006                         num = folder->klass->copy_msg(folder, dest, msginfo);
2007                         g_relation_insert(relation, msginfo, GINT_TO_POINTER(num));
2008                 }
2009         }
2010
2011         /* Read cache for dest folder */
2012         if (!dest->cache) folder_item_read_cache(dest);
2013
2014         /* 
2015          * Fetch new MsgInfos for new messages in dest folder,
2016          * add them to the msgcache and update folder message counts
2017          */
2018         if (g_relation_count(relation, GINT_TO_POINTER(0), 1) > 0) {
2019                 folder_item_scan_full(dest, FALSE);
2020                 folderscan = TRUE;
2021         }
2022
2023         for (l = msglist; l != NULL; l = g_slist_next(l)) {
2024                 MsgInfo *msginfo = (MsgInfo *) l->data;
2025                 GTuples *tuples;
2026
2027                 tuples = g_relation_select(relation, msginfo, 0);
2028                 num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
2029                 g_tuples_destroy(tuples);
2030
2031                 if (num >= 0) {
2032                         MsgInfo *newmsginfo;
2033
2034                         if (folderscan) {
2035                                 if (msginfo->msgid != NULL) {
2036                                         newmsginfo = folder_item_get_msginfo_by_msgid(dest, msginfo->msgid);
2037                                         if (newmsginfo != NULL) {
2038                                                 copy_msginfo_flags(msginfo, newmsginfo);
2039                                                 num = newmsginfo->msgnum;
2040                                                 procmsg_msginfo_free(newmsginfo);
2041                                         }
2042                                 }
2043                         } else {
2044                                 newmsginfo = folder->klass->get_msginfo(folder, dest, num);
2045                                 if (newmsginfo != NULL) {
2046                                         add_msginfo_to_cache(dest, newmsginfo, msginfo);
2047                                         procmsg_msginfo_free(newmsginfo);
2048                                 }
2049                         }
2050
2051                         if (num > lastnum)
2052                                 lastnum = num;
2053                 }
2054         }
2055
2056         if (remove_source) {
2057                 /*
2058                  * Remove source messages from their folders if
2059                  * copying was successfull and update folder
2060                  * message counts
2061                  */
2062                 for (l = msglist; l != NULL; l = g_slist_next(l)) {
2063                         MsgInfo *msginfo = (MsgInfo *) l->data;
2064                         FolderItem *item = msginfo->folder;
2065                         GTuples *tuples;
2066
2067                         tuples = g_relation_select(relation, msginfo, 0);
2068                         num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
2069                         g_tuples_destroy(tuples);
2070
2071                         if ((num >= 0) && (item->folder->klass->remove_msg != NULL)) {
2072                                 item->folder->klass->remove_msg(item->folder,
2073                                                                 msginfo->folder,
2074                                                                 msginfo->msgnum);
2075                                 remove_msginfo_from_cache(item, msginfo);
2076                         }
2077                 }
2078         }
2079
2080         if (folder->klass->finished_copy)
2081                 folder->klass->finished_copy(folder, dest);
2082
2083         g_relation_destroy(relation);
2084         return lastnum;
2085 }
2086
2087 /**
2088  * Move a message to a new folder.
2089  *
2090  * \param dest Destination folder
2091  * \param msginfo The message
2092  */
2093 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
2094 {
2095         GSList list;
2096
2097         g_return_val_if_fail(dest != NULL, -1);
2098         g_return_val_if_fail(msginfo != NULL, -1);
2099
2100         list.data = msginfo;
2101         list.next = NULL;
2102
2103         return do_copy_msgs(dest, &list, TRUE);
2104 }
2105
2106 /**
2107  * Move a list of messages to a new folder.
2108  *
2109  * \param dest Destination folder
2110  * \param msglist List of messages
2111  */
2112 gint folder_item_move_msgs(FolderItem *dest, GSList *msglist)
2113 {
2114         g_return_val_if_fail(dest != NULL, -1);
2115         g_return_val_if_fail(msglist != NULL, -1);
2116
2117         return do_copy_msgs(dest, msglist, TRUE);
2118 }
2119
2120 /**
2121  * Copy a message to a new folder.
2122  *
2123  * \param dest Destination folder
2124  * \param msginfo The message
2125  */
2126 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
2127 {
2128         GSList list;
2129
2130         g_return_val_if_fail(dest != NULL, -1);
2131         g_return_val_if_fail(msginfo != NULL, -1);
2132     
2133         list.data = msginfo;
2134         list.next = NULL;
2135         
2136         return do_copy_msgs(dest, &list, FALSE);
2137 }
2138
2139 /**
2140  * Copy a list of messages to a new folder.
2141  *
2142  * \param dest Destination folder
2143  * \param msglist List of messages
2144  */
2145 gint folder_item_copy_msgs(FolderItem *dest, GSList *msglist)
2146 {
2147         g_return_val_if_fail(dest != NULL, -1);
2148         g_return_val_if_fail(msglist != NULL, -1);
2149
2150         return do_copy_msgs(dest, msglist, FALSE);
2151 }
2152
2153 gint folder_item_remove_msg(FolderItem *item, gint num)
2154 {
2155         Folder *folder;
2156         gint ret;
2157         MsgInfo *msginfo;
2158
2159         g_return_val_if_fail(item != NULL, -1);
2160         folder = item->folder;
2161         g_return_val_if_fail(folder->klass->remove_msg != NULL, -1);
2162
2163         if (!item->cache) folder_item_read_cache(item);
2164
2165         ret = folder->klass->remove_msg(folder, item, num);
2166
2167         msginfo = msgcache_get_msg(item->cache, num);
2168         if (msginfo != NULL) {
2169                 remove_msginfo_from_cache(item, msginfo);
2170                 procmsg_msginfo_free(msginfo);
2171         }
2172         folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2173
2174         return ret;
2175 }
2176
2177 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
2178 {
2179         Folder *folder;
2180         gint ret = 0;
2181
2182         g_return_val_if_fail(item != NULL, -1);
2183         folder = item->folder;
2184         g_return_val_if_fail(folder != NULL, -1);
2185
2186         if (!item->cache) folder_item_read_cache(item);
2187
2188         while (msglist != NULL) {
2189                 MsgInfo *msginfo = (MsgInfo *)msglist->data;
2190
2191                 ret = folder_item_remove_msg(item, msginfo->msgnum);
2192                 if (ret != 0) break;
2193                 msgcache_remove_msg(item->cache, msginfo->msgnum);
2194                 msglist = msglist->next;
2195         }
2196
2197         return ret;
2198 }
2199
2200 gint folder_item_remove_all_msg(FolderItem *item)
2201 {
2202         Folder *folder;
2203         gint result;
2204
2205         g_return_val_if_fail(item != NULL, -1);
2206
2207         folder = item->folder;
2208
2209         g_return_val_if_fail(folder->klass->remove_all_msg != NULL, -1);
2210
2211         result = folder->klass->remove_all_msg(folder, item);
2212
2213         if (result == 0) {
2214                 if (folder->klass->finished_remove)
2215                         folder->klass->finished_remove(folder, item);
2216
2217                 folder_item_free_cache(item);
2218                 item->cache = msgcache_new();
2219
2220                 item->new_msgs = 0;
2221                 item->unread_msgs = 0;
2222                 item->unreadmarked_msgs = 0;
2223                 item->total_msgs = 0;
2224                 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2225         }
2226
2227         return result;
2228 }
2229
2230 void folder_item_change_msg_flags(FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
2231 {
2232         g_return_if_fail(item != NULL);
2233         g_return_if_fail(msginfo != NULL);
2234         
2235         if (item->folder->klass->change_flags != NULL) {
2236                 item->folder->klass->change_flags(item->folder, item, msginfo, newflags);
2237         } else {
2238                 msginfo->flags.perm_flags = newflags;
2239         }
2240 }
2241
2242 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
2243 {
2244         Folder *folder;
2245
2246         g_return_val_if_fail(item != NULL, FALSE);
2247
2248         folder = item->folder;
2249
2250         g_return_val_if_fail(folder->klass->is_msg_changed != NULL, -1);
2251
2252         return folder->klass->is_msg_changed(folder, item, msginfo);
2253 }
2254
2255 gchar *folder_item_get_cache_file(FolderItem *item)
2256 {
2257         gchar *path;
2258         gchar *file;
2259
2260         g_return_val_if_fail(item != NULL, NULL);
2261         g_return_val_if_fail(item->path != NULL, NULL);
2262
2263         path = folder_item_get_path(item);
2264         g_return_val_if_fail(path != NULL, NULL);
2265         if (!is_dir_exist(path))
2266                 make_dir_hier(path);
2267         file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
2268         g_free(path);
2269
2270         return file;
2271 }
2272
2273 gchar *folder_item_get_mark_file(FolderItem *item)
2274 {
2275         gchar *path;
2276         gchar *file;
2277
2278         g_return_val_if_fail(item != NULL, NULL);
2279         g_return_val_if_fail(item->path != NULL, NULL);
2280
2281         path = folder_item_get_path(item);
2282         g_return_val_if_fail(path != NULL, NULL);
2283         if (!is_dir_exist(path))
2284                 make_dir_hier(path);
2285         file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
2286         g_free(path);
2287
2288         return file;
2289 }
2290
2291 static gboolean folder_build_tree(GNode *node, gpointer data)
2292 {
2293         Folder *folder = FOLDER(data);
2294         FolderItem *item;
2295         XMLNode *xmlnode;
2296         GList *list;
2297         SpecialFolderItemType stype = F_NORMAL;
2298         const gchar *name = NULL;
2299         const gchar *path = NULL;
2300         PrefsAccount *account = NULL;
2301         gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE, 
2302                  threaded = TRUE, apply_sub = FALSE;
2303         gboolean ret_rcpt = FALSE, hidereadmsgs = FALSE,
2304                  thread_collapsed = FALSE; /* CLAWS */
2305         FolderSortKey sort_key = SORT_BY_NONE;
2306         FolderSortType sort_type = SORT_ASCENDING;
2307         gint new = 0, unread = 0, total = 0, unreadmarked = 0;
2308         time_t mtime = 0;
2309
2310         g_return_val_if_fail(node->data != NULL, FALSE);
2311         if (!node->parent) return FALSE;
2312
2313         xmlnode = node->data;
2314         if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
2315                 g_warning("tag name != \"folderitem\"\n");
2316                 return FALSE;
2317         }
2318
2319         list = xmlnode->tag->attr;
2320         for (; list != NULL; list = list->next) {
2321                 XMLAttr *attr = list->data;
2322
2323                 if (!attr || !attr->name || !attr->value) continue;
2324                 if (!strcmp(attr->name, "type")) {
2325                         if (!strcasecmp(attr->value, "normal"))
2326                                 stype = F_NORMAL;
2327                         else if (!strcasecmp(attr->value, "inbox"))
2328                                 stype = F_INBOX;
2329                         else if (!strcasecmp(attr->value, "outbox"))
2330                                 stype = F_OUTBOX;
2331                         else if (!strcasecmp(attr->value, "draft"))
2332                                 stype = F_DRAFT;
2333                         else if (!strcasecmp(attr->value, "queue"))
2334                                 stype = F_QUEUE;
2335                         else if (!strcasecmp(attr->value, "trash"))
2336                                 stype = F_TRASH;
2337                 } else if (!strcmp(attr->name, "name"))
2338                         name = attr->value;
2339                 else if (!strcmp(attr->name, "path"))
2340                         path = attr->value;
2341                 else if (!strcmp(attr->name, "mtime"))
2342                         mtime = strtoul(attr->value, NULL, 10);
2343                 else if (!strcmp(attr->name, "new"))
2344                         new = atoi(attr->value);
2345                 else if (!strcmp(attr->name, "unread"))
2346                         unread = atoi(attr->value);
2347                 else if (!strcmp(attr->name, "unreadmarked"))
2348                         unreadmarked = atoi(attr->value);
2349                 else if (!strcmp(attr->name, "total"))
2350                         total = atoi(attr->value);
2351                 else if (!strcmp(attr->name, "no_sub"))
2352                         no_sub = *attr->value == '1' ? TRUE : FALSE;
2353                 else if (!strcmp(attr->name, "no_select"))
2354                         no_select = *attr->value == '1' ? TRUE : FALSE;
2355                 else if (!strcmp(attr->name, "collapsed"))
2356                         collapsed = *attr->value == '1' ? TRUE : FALSE;
2357                 else if (!strcmp(attr->name, "thread_collapsed"))
2358                         thread_collapsed =  *attr->value == '1' ? TRUE : FALSE;
2359                 else if (!strcmp(attr->name, "threaded"))
2360                         threaded =  *attr->value == '1' ? TRUE : FALSE;
2361                 else if (!strcmp(attr->name, "hidereadmsgs"))
2362                         hidereadmsgs =  *attr->value == '1' ? TRUE : FALSE;
2363                 else if (!strcmp(attr->name, "reqretrcpt"))
2364                         ret_rcpt =  *attr->value == '1' ? TRUE : FALSE;
2365                 else if (!strcmp(attr->name, "sort_key")) {
2366                         if (!strcmp(attr->value, "none"))
2367                                 sort_key = SORT_BY_NONE;
2368                         else if (!strcmp(attr->value, "number"))
2369                                 sort_key = SORT_BY_NUMBER;
2370                         else if (!strcmp(attr->value, "size"))
2371                                 sort_key = SORT_BY_SIZE;
2372                         else if (!strcmp(attr->value, "date"))
2373                                 sort_key = SORT_BY_DATE;
2374                         else if (!strcmp(attr->value, "from"))
2375                                 sort_key = SORT_BY_FROM;
2376                         else if (!strcmp(attr->value, "subject"))
2377                                 sort_key = SORT_BY_SUBJECT;
2378                         else if (!strcmp(attr->value, "score"))
2379                                 sort_key = SORT_BY_SCORE;
2380                         else if (!strcmp(attr->value, "label"))
2381                                 sort_key = SORT_BY_LABEL;
2382                         else if (!strcmp(attr->value, "mark"))
2383                                 sort_key = SORT_BY_MARK;
2384                         else if (!strcmp(attr->value, "unread"))
2385                                 sort_key = SORT_BY_STATUS;
2386                         else if (!strcmp(attr->value, "mime"))
2387                                 sort_key = SORT_BY_MIME;
2388                         else if (!strcmp(attr->value, "to"))
2389                                 sort_key = SORT_BY_TO;
2390                         else if (!strcmp(attr->value, "locked"))
2391                                 sort_key = SORT_BY_LOCKED;
2392                 } else if (!strcmp(attr->name, "sort_type")) {
2393                         if (!strcmp(attr->value, "ascending"))
2394                                 sort_type = SORT_ASCENDING;
2395                         else
2396                                 sort_type = SORT_DESCENDING;
2397                 } else if (!strcmp(attr->name, "account_id")) {
2398                         account = account_find_from_id(atoi(attr->value));
2399                         if (!account) g_warning("account_id: %s not found\n",
2400                                                 attr->value);
2401                 } else if (!strcmp(attr->name, "apply_sub"))
2402                         apply_sub = *attr->value == '1' ? TRUE : FALSE;
2403         }
2404
2405         item = folder_item_new(folder, name, path);
2406         item->stype = stype;
2407         item->mtime = mtime;
2408         item->new_msgs = new;
2409         item->unread_msgs = unread;
2410         item->unreadmarked_msgs = unreadmarked;
2411         item->total_msgs = total;
2412         item->no_sub = no_sub;
2413         item->no_select = no_select;
2414         item->collapsed = collapsed;
2415         item->thread_collapsed = thread_collapsed;
2416         item->threaded  = threaded;
2417         item->hide_read_msgs  = hidereadmsgs;
2418         item->ret_rcpt  = ret_rcpt;
2419         item->sort_key  = sort_key;
2420         item->sort_type = sort_type;
2421         item->node = node;
2422         item->parent = FOLDER_ITEM(node->parent->data);
2423         item->folder = folder;
2424         switch (stype) {
2425         case F_INBOX:  folder->inbox  = item; break;
2426         case F_OUTBOX: folder->outbox = item; break;
2427         case F_DRAFT:  folder->draft  = item; break;
2428         case F_QUEUE:  folder->queue  = item; break;
2429         case F_TRASH:  folder->trash  = item; break;
2430         default:       break;
2431         }
2432         item->account = account;
2433         item->apply_sub = apply_sub;
2434         folder_item_prefs_read_config(item);
2435
2436         node->data = item;
2437         xml_free_node(xmlnode);
2438
2439         return FALSE;
2440 }
2441
2442 static gboolean folder_read_folder_func(GNode *node, gpointer data)
2443 {
2444         Folder *folder;
2445         FolderItem *item;
2446         XMLNode *xmlnode;
2447         GList *list;
2448         FolderClass *class = NULL;
2449         const gchar *name = NULL;
2450         const gchar *path = NULL;
2451         PrefsAccount *account = NULL;
2452         gboolean collapsed = FALSE, threaded = TRUE, apply_sub = FALSE;
2453         gboolean ret_rcpt = FALSE, thread_collapsed = FALSE; /* CLAWS */
2454
2455         if (g_node_depth(node) != 2) return FALSE;
2456         g_return_val_if_fail(node->data != NULL, FALSE);
2457
2458         xmlnode = node->data;
2459         if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
2460                 g_warning("tag name != \"folder\"\n");
2461                 return TRUE;
2462         }
2463         g_node_unlink(node);
2464         list = xmlnode->tag->attr;
2465         for (; list != NULL; list = list->next) {
2466                 XMLAttr *attr = list->data;
2467
2468                 if (!attr || !attr->name || !attr->value) continue;
2469                 if (!strcmp(attr->name, "type"))
2470                         class = folder_get_class_from_string(attr->value);
2471                 else if (!strcmp(attr->name, "name"))
2472                         name = attr->value;
2473                 else if (!strcmp(attr->name, "path"))
2474                         path = attr->value;
2475                 else if (!strcmp(attr->name, "collapsed"))
2476                         collapsed = *attr->value == '1' ? TRUE : FALSE;
2477                 else if (!strcmp(attr->name, "thread_collapsed"))
2478                         thread_collapsed = *attr->value == '1' ? TRUE : FALSE;
2479                 else if (!strcmp(attr->name, "threaded"))
2480                         threaded = *attr->value == '1' ? TRUE : FALSE;
2481                 else if (!strcmp(attr->name, "account_id")) {
2482                         account = account_find_from_id(atoi(attr->value));
2483                         if (!account) g_warning("account_id: %s not found\n",
2484                                                 attr->value);
2485                 } else if (!strcmp(attr->name, "apply_sub"))
2486                         apply_sub = *attr->value == '1' ? TRUE : FALSE;
2487                 else if (!strcmp(attr->name, "reqretrcpt"))
2488                         ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
2489         }
2490
2491         folder = folder_new(class, name, path);
2492         g_return_val_if_fail(folder != NULL, FALSE);
2493         folder->account = account;
2494         if (account != NULL)
2495                 account->folder = REMOTE_FOLDER(folder);
2496         item = FOLDER_ITEM(folder->node->data);
2497         node->data = item;
2498         item->node = node;
2499         g_node_destroy(folder->node);
2500         folder->node = node;
2501         folder_add(folder);
2502         item->collapsed = collapsed;
2503         item->thread_collapsed = thread_collapsed;
2504         item->threaded  = threaded;
2505         item->account   = account;
2506         item->apply_sub = apply_sub;
2507         item->ret_rcpt  = ret_rcpt;
2508
2509         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2510                         folder_build_tree, folder);
2511
2512         return FALSE;
2513 }
2514
2515 static gchar *folder_get_list_path(void)
2516 {
2517         static gchar *filename = NULL;
2518
2519         if (!filename)
2520                 filename =  g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2521                                         FOLDER_LIST, NULL);
2522
2523         return filename;
2524 }
2525
2526 #define PUT_ESCAPE_STR(fp, attr, str)                   \
2527 {                                                       \
2528         fputs(" " attr "=\"", fp);                      \
2529         xml_file_put_escape_str(fp, str);               \
2530         fputs("\"", fp);                                \
2531 }
2532
2533 static void folder_write_list_recursive(GNode *node, gpointer data)
2534 {
2535         FILE *fp = (FILE *)data;
2536         FolderItem *item;
2537         gint i, depth;
2538         static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
2539                                                  "draft", "queue", "trash"};
2540         static gchar *sort_key_str[] = {"none", "number", "size", "date",
2541                                         "from", "subject", "score", "label",
2542                                         "mark", "unread", "mime", "to", 
2543                                         "locked"};
2544         g_return_if_fail(node != NULL);
2545         g_return_if_fail(fp != NULL);
2546
2547         item = FOLDER_ITEM(node->data);
2548         g_return_if_fail(item != NULL);
2549
2550         depth = g_node_depth(node);
2551         for (i = 0; i < depth; i++)
2552                 fputs("    ", fp);
2553         if (depth == 1) {
2554                 Folder *folder = item->folder;
2555
2556                 fprintf(fp, "<folder type=\"%s\"", folder->klass->idstr);
2557                 if (folder->name)
2558                         PUT_ESCAPE_STR(fp, "name", folder->name);
2559                 if (FOLDER_TYPE(folder) == F_MH || FOLDER_TYPE(folder) == F_MBOX || FOLDER_TYPE(folder) == F_MAILDIR)
2560                         PUT_ESCAPE_STR(fp, "path",
2561                                        LOCAL_FOLDER(folder)->rootpath);
2562                 if (item->collapsed && node->children)
2563                         fputs(" collapsed=\"1\"", fp);
2564                 if (folder->account)
2565                         fprintf(fp, " account_id=\"%d\"",
2566                                 folder->account->account_id);
2567                 if (item->apply_sub)
2568                         fputs(" apply_sub=\"1\"", fp);
2569                 if (item->ret_rcpt) 
2570                         fputs(" reqretrcpt=\"1\"", fp);
2571         } else {
2572                 fprintf(fp, "<folderitem type=\"%s\"",
2573                         folder_item_stype_str[item->stype]);
2574                 if (item->name)
2575                         PUT_ESCAPE_STR(fp, "name", item->name);
2576                 if (item->path)
2577                         PUT_ESCAPE_STR(fp, "path", item->path);
2578
2579                 if (item->no_sub)
2580                         fputs(" no_sub=\"1\"", fp);
2581                 if (item->no_select)
2582                         fputs(" no_select=\"1\"", fp);
2583                 if (item->collapsed && node->children)
2584                         fputs(" collapsed=\"1\"", fp);
2585                 else
2586                         fputs(" collapsed=\"0\"", fp);
2587                 if (item->thread_collapsed)
2588                         fputs(" thread_collapsed=\"1\"", fp);
2589                 else
2590                         fputs(" thread_collapsed=\"0\"", fp);
2591                 if (item->threaded)
2592                         fputs(" threaded=\"1\"", fp);
2593                 else
2594                         fputs(" threaded=\"0\"", fp);
2595                 if (item->hide_read_msgs)
2596                         fputs(" hidereadmsgs=\"1\"", fp);
2597                 else
2598                         fputs(" hidereadmsgs=\"0\"", fp);
2599                 if (item->ret_rcpt)
2600                         fputs(" reqretrcpt=\"1\"", fp);
2601
2602                 if (item->sort_key != SORT_BY_NONE) {
2603                         fprintf(fp, " sort_key=\"%s\"",
2604                                 sort_key_str[item->sort_key]);
2605                         if (item->sort_type == SORT_ASCENDING)
2606                                 fprintf(fp, " sort_type=\"ascending\"");
2607                         else
2608                                 fprintf(fp, " sort_type=\"descending\"");
2609                 }
2610
2611                 fprintf(fp,
2612                         " mtime=\"%lu\" new=\"%d\" unread=\"%d\" unreadmarked=\"%d\" total=\"%d\"",
2613                         item->mtime, item->new_msgs, item->unread_msgs, item->unreadmarked_msgs, item->total_msgs);
2614
2615                 if (item->account)
2616                         fprintf(fp, " account_id=\"%d\"",
2617                                 item->account->account_id);
2618                 if (item->apply_sub)
2619                         fputs(" apply_sub=\"1\"", fp);
2620         }
2621
2622         if (node->children) {
2623                 GNode *child;
2624                 fputs(">\n", fp);
2625
2626                 child = node->children;
2627                 while (child) {
2628                         GNode *cur;
2629
2630                         cur = child;
2631                         child = cur->next;
2632                         folder_write_list_recursive(cur, data);
2633                 }
2634
2635                 for (i = 0; i < depth; i++)
2636                         fputs("    ", fp);
2637                 fprintf(fp, "</%s>\n", depth == 1 ? "folder" : "folderitem");
2638         } else
2639                 fputs(" />\n", fp);
2640 }
2641
2642 static void folder_update_op_count_rec(GNode *node)
2643 {
2644         FolderItem *fitem = FOLDER_ITEM(node->data);
2645
2646         if (g_node_depth(node) > 0) {
2647                 if (fitem->op_count > 0) {
2648                         fitem->op_count = 0;
2649                         folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
2650                 }
2651                 if (node->children) {
2652                         GNode *child;
2653
2654                         child = node->children;
2655                         while (child) {
2656                                 GNode *cur;
2657
2658                                 cur = child;
2659                                 child = cur->next;
2660                                 folder_update_op_count_rec(cur);
2661                         }
2662                 }
2663         }
2664 }
2665
2666 void folder_update_op_count(void) 
2667 {
2668         GList *cur;
2669         Folder *folder;
2670
2671         for (cur = folder_list; cur != NULL; cur = cur->next) {
2672                 folder = cur->data;
2673                 folder_update_op_count_rec(folder->node);
2674         }
2675 }
2676
2677 typedef struct _type_str {
2678         gchar * str;
2679         gint type;
2680 } type_str;
2681
2682
2683 /*
2684 static gchar * folder_item_get_tree_identifier(FolderItem * item)
2685 {
2686         if (item->parent != NULL) {
2687                 gchar * path;
2688                 gchar * id;
2689
2690                 path = folder_item_get_tree_identifier(item->parent);
2691                 if (path == NULL)
2692                         return NULL;
2693
2694                 id = g_strconcat(path, "/", item->name, NULL);
2695                 g_free(path);
2696
2697                 return id;
2698         }
2699         else {
2700                 return g_strconcat("/", item->name, NULL);
2701         }
2702 }
2703 */
2704
2705 /* CLAWS: temporary local folder for filtering */
2706 #define TEMP_FOLDER "TEMP_FOLDER"
2707 #define PROCESSING_FOLDER_ITEM "processing"     
2708
2709 static FolderItem *processing_folder_item;
2710
2711 static void folder_create_processing_folder(void)
2712 {
2713         Folder *processing_folder;
2714         gchar      *tmpname;
2715
2716         if ((processing_folder = folder_find_from_name(TEMP_FOLDER, mh_get_class())) == NULL) {
2717                 gchar *tmppath;
2718
2719                 tmppath =
2720                     g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2721                                 "tempfolder", NULL);
2722                 processing_folder =
2723                     folder_new(mh_get_class(), TEMP_FOLDER, tmppath);
2724                 g_free(tmppath);
2725         }
2726         g_assert(processing_folder != NULL);
2727
2728         debug_print("tmpparentroot %s\n", LOCAL_FOLDER(processing_folder)->rootpath);
2729         if (LOCAL_FOLDER(processing_folder)->rootpath[0] == '/')
2730                 tmpname = g_strconcat(LOCAL_FOLDER(processing_folder)->rootpath,
2731                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
2732                                       NULL);
2733         else
2734                 tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2735                                       LOCAL_FOLDER(processing_folder)->rootpath,
2736                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
2737                                       NULL);
2738
2739         if (!is_dir_exist(tmpname)) {
2740                 debug_print("*TMP* creating %s\n", tmpname);
2741                 processing_folder_item = processing_folder->klass->create_folder(processing_folder,
2742                                                                                  processing_folder->node->data,
2743                                                                                  PROCESSING_FOLDER_ITEM);
2744         } else {
2745                 debug_print("*TMP* already created\n");
2746                 processing_folder_item = folder_item_new(processing_folder, PROCESSING_FOLDER_ITEM, PROCESSING_FOLDER_ITEM);
2747                 g_assert(processing_folder_item);
2748                 folder_item_append(processing_folder->node->data, processing_folder_item);
2749         }
2750         g_assert(processing_folder_item != NULL);
2751         g_free(tmpname);
2752 }
2753
2754 FolderItem *folder_get_default_processing(void)
2755 {
2756         if (!processing_folder_item) {
2757                 folder_create_processing_folder();
2758         }
2759         return processing_folder_item;
2760 }
2761
2762 /* folder_persist_prefs_new() - return hash table with persistent
2763  * settings (and folder name as key). 
2764  * (note that in claws other options are in the folder_item_prefs_RC
2765  * file, so those don't need to be included in PersistPref yet) 
2766  */
2767 GHashTable *folder_persist_prefs_new(Folder *folder)
2768 {
2769         GHashTable *pptable;
2770
2771         g_return_val_if_fail(folder, NULL);
2772         pptable = g_hash_table_new(g_str_hash, g_str_equal);
2773         folder_get_persist_prefs_recursive(folder->node, pptable);
2774         return pptable;
2775 }
2776
2777 void folder_persist_prefs_free(GHashTable *pptable)
2778 {
2779         g_return_if_fail(pptable);
2780         g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
2781         g_hash_table_destroy(pptable);
2782 }
2783
2784 const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
2785 {
2786         if (pptable == NULL || name == NULL) return NULL;
2787         return g_hash_table_lookup(pptable, name);
2788 }
2789
2790 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
2791 {
2792         const PersistPrefs *pp;
2793         gchar *id = folder_item_get_identifier(item);
2794
2795         pp = folder_get_persist_prefs(pptable, id); 
2796         g_free(id);
2797
2798         if (!pp) return;
2799
2800         /* CLAWS: since not all folder properties have been migrated to 
2801          * folderlist.xml, we need to call the old stuff first before
2802          * setting things that apply both to Main and Claws. */
2803         folder_item_prefs_read_config(item); 
2804          
2805         item->collapsed = pp->collapsed;
2806         item->thread_collapsed = pp->thread_collapsed;
2807         item->threaded  = pp->threaded;
2808         item->ret_rcpt  = pp->ret_rcpt;
2809         item->hide_read_msgs = pp->hide_read_msgs;
2810         item->sort_key  = pp->sort_key;
2811         item->sort_type = pp->sort_type;
2812 }
2813
2814 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
2815 {
2816         FolderItem *item = FOLDER_ITEM(node->data);
2817         PersistPrefs *pp;
2818         GNode *child, *cur;
2819         gchar *id;
2820
2821         g_return_if_fail(node != NULL);
2822         g_return_if_fail(item != NULL);
2823
2824         /* NOTE: item->path == NULL means top level folder; not interesting
2825          * to store preferences of that one.  */
2826         if (item->path) {
2827                 id = folder_item_get_identifier(item);
2828                 pp = g_new0(PersistPrefs, 1);
2829                 g_return_if_fail(pp != NULL);
2830                 pp->collapsed = item->collapsed;
2831                 pp->thread_collapsed = item->thread_collapsed;
2832                 pp->threaded  = item->threaded;
2833                 pp->ret_rcpt  = item->ret_rcpt; 
2834                 pp->hide_read_msgs = item->hide_read_msgs;
2835                 pp->sort_key  = item->sort_key;
2836                 pp->sort_type = item->sort_type;
2837                 g_hash_table_insert(pptable, id, pp);
2838         }
2839
2840         if (node->children) {
2841                 child = node->children;
2842                 while (child) {
2843                         cur = child;
2844                         child = cur->next;
2845                         folder_get_persist_prefs_recursive(cur, pptable);
2846                 }
2847         }       
2848 }
2849
2850 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
2851 {
2852         if (key) 
2853                 g_free(key);
2854         if (val) 
2855                 g_free(val);
2856         return TRUE;    
2857 }
2858
2859 void folder_item_apply_processing(FolderItem *item)
2860 {
2861         GSList *processing_list;
2862         GSList *mlist, *cur;
2863         
2864         g_return_if_fail(item != NULL);
2865         
2866         processing_list = item->prefs->processing;
2867         if (processing_list == NULL)
2868                 return;
2869
2870         folder_item_update_freeze();
2871
2872         mlist = folder_item_get_msg_list(item);
2873         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2874                 MsgInfo * msginfo;
2875
2876                 msginfo = (MsgInfo *) cur->data;
2877                 filter_message_by_msginfo(processing_list, msginfo);
2878                 procmsg_msginfo_free(msginfo);
2879         }
2880         g_slist_free(mlist);
2881
2882         folder_item_update_thaw();
2883 }
2884
2885 /*
2886  *  functions for handling FolderItem content changes
2887  */
2888 static gint folder_item_update_freeze_cnt = 0;
2889
2890 /**
2891  * Notify the folder system about changes to a folder. If the
2892  * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
2893  * be invoked, otherwise the changes will be remebered until
2894  * the folder system is thawed.
2895  *
2896  * \param item The FolderItem that was changed
2897  * \param update_flags Type of changed that was made
2898  */
2899 void folder_item_update(FolderItem *item, FolderItemUpdateFlags update_flags)
2900 {
2901         if (folder_item_update_freeze_cnt == 0) {
2902                 FolderItemUpdateData source;
2903         
2904                 source.item = item;
2905                 source.update_flags = update_flags;
2906                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
2907         } else {
2908                 item->update_flags |= update_flags;
2909         }
2910 }
2911
2912 void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
2913 {
2914         GNode *node = item->folder->node;       
2915
2916         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
2917         node = node->children;
2918
2919         folder_item_update(item, update_flags);
2920         while (node != NULL) {
2921                 if (node && node->data) {
2922                         FolderItem *next_item = (FolderItem*) node->data;
2923
2924                         folder_item_update(next_item, update_flags);
2925                 }
2926                 node = node->next;
2927         }
2928 }
2929
2930 void folder_item_update_freeze(void)
2931 {
2932         folder_item_update_freeze_cnt++;
2933 }
2934
2935 static void folder_item_update_func(FolderItem *item, gpointer data)
2936 {
2937         FolderItemUpdateData source;
2938     
2939         if (item->update_flags) {
2940                 source.item = item;
2941                 source.update_flags = item->update_flags;
2942                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);                             
2943                 item->update_flags = 0;
2944         }
2945 }
2946
2947 void folder_item_update_thaw(void)
2948 {
2949         if (folder_item_update_freeze_cnt > 0)
2950                 folder_item_update_freeze_cnt--;
2951         if (folder_item_update_freeze_cnt == 0) {
2952                 /* Update all folders */
2953                 folder_func_to_all_folders(folder_item_update_func, NULL);
2954         }
2955 }
2956
2957 #undef PUT_ESCAPE_STR