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