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