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