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