0.8.11claws60
[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 *class)
93 {
94         debug_print("registering folder class %s\n", class->idstr);
95         classlist = g_slist_append(classlist, class);
96 }
97
98 Folder *folder_new(FolderClass *class, const gchar *name, const gchar *path)
99 {
100         Folder *folder = NULL;
101         FolderItem *item;
102
103         g_return_val_if_fail(class != NULL, NULL);
104
105         name = name ? name : path;
106         folder = class->new(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->class->destroy != 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->class->destroy(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->class->item_new) {
180                 item = folder->class->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 = 0;
192         item->unread = 0;
193         item->unreadmarked = 0;
194         item->total = 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->class->item_destroy) {
263                         item->folder->class->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->class->scan_tree)
422                 return;
423         
424         pptable = folder_persist_prefs_new(folder);
425         folder_tree_destroy(folder);
426
427         folder->class->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->class->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;
451         guint unread;
452         guint unreadmarked;
453         guint total;
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 += item->new;
501         count->unread += item->unread;
502         count->unreadmarked += item->unreadmarked;
503         count->total += item->total;
504 }
505
506 void folder_count_total_msgs(guint *new, guint *unread, guint *unreadmarked, guint *total)
507 {
508         struct TotalMsgCount count;
509
510         count.new = count.unread = count.unreadmarked = count.total = 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 = count.new;
517         *unread = count.unread;
518         *unreadmarked = count.unreadmarked;
519         *total = count.total;
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 *class)
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->class == class && 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->class->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->class->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->class->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) {
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->class->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->class->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->class->check_msgnum_validity || 
1039             folder->class->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->class->is_msg_changed && folder->class->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->class->get_msginfos) {
1187                         newmsg_list = folder->class->get_msginfos(folder, item, new_list);
1188                 } else if (folder->class->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->class->get_msginfo(folder, item, num);
1197                                 if (msginfo != NULL) {
1198                                         newmsg_list = g_slist_prepend(newmsg_list, msginfo);
1199                                         debug_print("Added newly found message %d to cache.\n", num);
1200                                 }
1201                         }
1202                 }
1203                 g_slist_free(new_list);
1204         }
1205
1206         if (newmsg_list != NULL) {
1207                 GSList *elem;
1208
1209                 for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem))
1210                         msgcache_add_msg(item->cache, elem->data);
1211
1212                 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1213                 
1214                 exists_list = g_slist_concat(exists_list, newmsg_list);
1215         }
1216
1217         for (elem = exists_list; elem != NULL; elem = g_slist_next(elem)) {
1218                 MsgInfo *msginfo;
1219
1220                 msginfo = elem->data;
1221                 if (MSG_IS_IGNORE_THREAD(msginfo->flags) && (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1222                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1223                 if (!MSG_IS_IGNORE_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD)) {
1224                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1225                         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
1226                 }
1227                 if ((item->stype == F_OUTBOX ||
1228                      item->stype == F_QUEUE  ||
1229                      item->stype == F_DRAFT  ||
1230                      item->stype == F_TRASH) &&
1231                     (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1232                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1233                 if (MSG_IS_NEW(msginfo->flags))
1234                         newcnt++;
1235                 if (MSG_IS_UNREAD(msginfo->flags))
1236                         unreadcnt++;
1237                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1238                         unreadmarkedcnt++;
1239                 totalcnt++;
1240
1241                 procmsg_msginfo_free(msginfo);
1242         }
1243         g_slist_free(exists_list);
1244
1245         item->new = newcnt;
1246         item->unread = unreadcnt;
1247         item->total = totalcnt;
1248         item->unreadmarked = unreadmarkedcnt;
1249
1250         update_flags |= F_ITEM_UPDATE_MSGCNT;
1251
1252         folder_item_update(item, update_flags);
1253
1254         return 0;
1255 }
1256
1257 static void folder_item_scan_foreach_func(gpointer key, gpointer val,
1258                                           gpointer data)
1259 {
1260         folder_item_scan(FOLDER_ITEM(key));
1261 }
1262
1263 void folder_item_scan_foreach(GHashTable *table)
1264 {
1265         g_hash_table_foreach(table, folder_item_scan_foreach_func, NULL);
1266 }
1267
1268 void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
1269 {
1270         gint *memusage = (gint *)data;
1271
1272         if (item->cache == NULL)
1273                 return;
1274         
1275         *memusage += msgcache_get_memory_usage(item->cache);
1276 }
1277
1278 gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
1279 {
1280         FolderItem *fa = (FolderItem *)a;
1281         FolderItem *fb = (FolderItem *)b;
1282         
1283         return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
1284 }
1285
1286 void folder_find_expired_caches(FolderItem *item, gpointer data)
1287 {
1288         GSList **folder_item_list = (GSList **)data;
1289         gint difftime, expiretime;
1290         
1291         if (item->cache == NULL)
1292                 return;
1293
1294         if (item->opened > 0)
1295                 return;
1296
1297         difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
1298         expiretime = prefs_common.cache_min_keep_time * 60;
1299         debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
1300         if (difftime > expiretime) {
1301                 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
1302         }
1303 }
1304
1305 void folder_item_free_cache(FolderItem *item)
1306 {
1307         g_return_if_fail(item != NULL);
1308         
1309         if (item->cache == NULL)
1310                 return;
1311         
1312         if (item->opened > 0)
1313                 return;
1314
1315         folder_item_write_cache(item);
1316         msgcache_destroy(item->cache);
1317         item->cache = NULL;
1318 }
1319
1320 void folder_clean_cache_memory()
1321 {
1322         gint memusage = 0;
1323
1324         folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);       
1325         debug_print("Total cache memory usage: %d\n", memusage);
1326         
1327         if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
1328                 GSList *folder_item_list = NULL, *listitem;
1329                 
1330                 debug_print("Trying to free cache memory\n");
1331
1332                 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);      
1333                 listitem = folder_item_list;
1334                 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
1335                         FolderItem *item = (FolderItem *)(listitem->data);
1336
1337                         debug_print("Freeing cache memory for %s\n", item->path);
1338                         memusage -= msgcache_get_memory_usage(item->cache);
1339                         folder_item_free_cache(item);
1340                         listitem = listitem->next;
1341                 }
1342                 g_slist_free(folder_item_list);
1343         }
1344 }
1345
1346 void folder_item_read_cache(FolderItem *item)
1347 {
1348         gchar *cache_file, *mark_file;
1349         
1350         g_return_if_fail(item != NULL);
1351
1352         cache_file = folder_item_get_cache_file(item);
1353         mark_file = folder_item_get_mark_file(item);
1354         item->cache = msgcache_read_cache(item, cache_file);
1355         if (!item->cache) {
1356                 item->cache = msgcache_new();
1357                 folder_item_scan(item);
1358         }
1359         msgcache_read_mark(item->cache, mark_file);
1360         g_free(cache_file);
1361         g_free(mark_file);
1362
1363         folder_clean_cache_memory();
1364 }
1365
1366 void folder_item_write_cache(FolderItem *item)
1367 {
1368         gchar *cache_file, *mark_file;
1369         PrefsFolderItem *prefs;
1370         gint filemode = 0;
1371         gchar *id;
1372         
1373         if (!item || !item->path || !item->cache)
1374                 return;
1375
1376         id = folder_item_get_identifier(item);
1377         debug_print("Save cache for folder %s\n", id);
1378         g_free(id);
1379
1380         cache_file = folder_item_get_cache_file(item);
1381         mark_file = folder_item_get_mark_file(item);
1382         if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
1383                 prefs = item->prefs;
1384                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
1385                         /* for cache file */
1386                         filemode = prefs->folder_chmod;
1387                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
1388                         if (filemode & S_IROTH) filemode |= S_IWOTH;
1389                         chmod(cache_file, filemode);
1390                 }
1391         }
1392
1393         g_free(cache_file);
1394         g_free(mark_file);
1395 }
1396
1397 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
1398 {
1399         Folder *folder;
1400         MsgInfo *msginfo;
1401         
1402         g_return_val_if_fail(item != NULL, NULL);
1403         
1404         folder = item->folder;
1405         if (!item->cache)
1406                 folder_item_read_cache(item);
1407         
1408         if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
1409                 return msginfo;
1410         
1411         g_return_val_if_fail(folder->class->get_msginfo, NULL);
1412         if ((msginfo = folder->class->get_msginfo(folder, item, num)) != NULL) {
1413                 msgcache_add_msg(item->cache, msginfo);
1414                 return msginfo;
1415         }
1416         
1417         return NULL;
1418 }
1419
1420 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
1421 {
1422         Folder *folder;
1423         MsgInfo *msginfo;
1424         
1425         g_return_val_if_fail(item != NULL, NULL);
1426         
1427         folder = item->folder;
1428         if (!item->cache)
1429                 folder_item_read_cache(item);
1430         
1431         if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
1432                 return msginfo;
1433
1434         return NULL;
1435 }
1436
1437 GSList *folder_item_get_msg_list(FolderItem *item)
1438 {
1439         g_return_val_if_fail(item != NULL, NULL);
1440         
1441         if (item->cache == 0)
1442                 folder_item_read_cache(item);
1443
1444         g_return_val_if_fail(item->cache != NULL, NULL);
1445         
1446         return msgcache_get_msg_list(item->cache);
1447 }
1448
1449 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
1450 {
1451         Folder *folder;
1452
1453         g_return_val_if_fail(item != NULL, NULL);
1454
1455         folder = item->folder;
1456
1457         g_return_val_if_fail(folder->class->fetch_msg != NULL, NULL);
1458
1459         return folder->class->fetch_msg(folder, item, num);
1460 }
1461
1462 static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
1463 {
1464         static HeaderEntry hentry[] = {{"Message-ID:",  NULL, TRUE},
1465                                        {NULL,           NULL, FALSE}};
1466         FILE *fp;
1467         MsgInfo *msginfo;
1468         gint msgnum = 0;
1469         gchar buf[BUFFSIZE];
1470
1471         if ((fp = fopen(file, "rb")) == NULL)
1472                 return 0;
1473
1474         if ((dest->stype == F_QUEUE) || (dest->stype == F_DRAFT))
1475                 while (fgets(buf, sizeof(buf), fp) != NULL)
1476                         if (buf[0] == '\r' || buf[0] == '\n') break;
1477
1478         procheader_get_header_fields(fp, hentry);
1479         if (hentry[0].body) {
1480                 extract_parenthesis(hentry[0].body, '<', '>');
1481                 remove_space(hentry[0].body);
1482                 if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
1483                         msgnum = msginfo->msgnum;
1484                         procmsg_msginfo_free(msginfo);
1485
1486                         debug_print("found message as uid %d\n", msgnum);
1487                 }
1488         }
1489         
1490         g_free(hentry[0].body);
1491         hentry[0].body = NULL;
1492         fclose(fp);
1493
1494         return msgnum;
1495 }
1496
1497 static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
1498 {
1499         MsgPermFlags perm_flags = 0;
1500         MsgTmpFlags tmp_flags = 0;
1501
1502         /* create new flags */
1503         if (source != NULL) {
1504                 /* copy original flags */
1505                 perm_flags = source->flags.perm_flags;
1506                 tmp_flags = source->flags.tmp_flags;
1507         } else {
1508                 perm_flags = dest->flags.perm_flags;
1509                 tmp_flags = dest->flags.tmp_flags;
1510         }
1511
1512         /* remove new, unread and deleted in special folders */
1513         if (dest->folder->stype == F_OUTBOX ||
1514             dest->folder->stype == F_QUEUE  ||
1515             dest->folder->stype == F_DRAFT  ||
1516             dest->folder->stype == F_TRASH)
1517                 perm_flags &= ~(MSG_NEW | MSG_UNREAD | MSG_DELETED);
1518
1519         /* set ignore flag of ignored parent exists */
1520         if (procmsg_msg_has_flagged_parent(dest, MSG_IGNORE_THREAD))
1521                 perm_flags |= MSG_IGNORE_THREAD;
1522
1523         /* Unset tmp flags that should not be copied */
1524         tmp_flags &= ~(MSG_MOVE | MSG_COPY);
1525
1526         /* unset flags that are set but should not */
1527         procmsg_msginfo_unset_flags(dest,
1528                                     dest->flags.perm_flags & ~perm_flags,
1529                                     dest->flags.tmp_flags  & ~tmp_flags);
1530         /* set new flags */
1531         procmsg_msginfo_set_flags(dest,
1532                                   ~dest->flags.perm_flags & perm_flags,
1533                                   ~dest->flags.tmp_flags  & tmp_flags);
1534
1535         folder_item_update(dest->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1536 }
1537
1538 static void add_msginfo_to_cache(FolderItem *item, MsgInfo *newmsginfo, MsgInfo *flagsource)
1539 {
1540         /* update folder stats */
1541         if (MSG_IS_NEW(newmsginfo->flags))
1542                 item->new++;
1543         if (MSG_IS_UNREAD(newmsginfo->flags))
1544                 item->unread++;
1545         if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
1546                 item->unreadmarked++;
1547         item->total++;
1548
1549         copy_msginfo_flags(flagsource, newmsginfo);
1550
1551         msgcache_add_msg(item->cache, newmsginfo);
1552 }
1553
1554 static void remove_msginfo_from_cache(FolderItem *item, MsgInfo *msginfo)
1555 {
1556         if (!item->cache)
1557             folder_item_read_cache(item);
1558
1559         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1560                 msginfo->folder->new--;
1561         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1562                 msginfo->folder->unread--;
1563         if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1564                 msginfo->folder->unreadmarked--;
1565         msginfo->folder->total--;                       
1566
1567         msgcache_remove_msg(item->cache, msginfo->msgnum);
1568         folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1569 }
1570
1571 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
1572                          gboolean remove_source)
1573 {
1574         Folder *folder;
1575         gint num;
1576         MsgInfo *msginfo;
1577
1578         g_return_val_if_fail(dest != NULL, -1);
1579         g_return_val_if_fail(file != NULL, -1);
1580
1581         folder = dest->folder;
1582
1583         g_return_val_if_fail(folder->class->add_msg != NULL, -1);
1584
1585         if (!dest->cache)
1586                 folder_item_read_cache(dest);
1587
1588         num = folder->class->add_msg(folder, dest, file, FALSE);
1589
1590         if (num > 0) {
1591                 msginfo = folder->class->get_msginfo(folder, dest, num);
1592
1593                 if (msginfo != NULL) {
1594                         add_msginfo_to_cache(dest, msginfo, NULL);
1595                         procmsg_msginfo_free(msginfo);
1596                 }
1597
1598                 dest->last_num = num;
1599         } else if (num == 0) {
1600                 folder_item_scan(dest);
1601                 num = folder_item_get_msg_num_by_file(dest, file);
1602         }
1603
1604         if (num >= 0 && remove_source) {
1605                 if (unlink(file) < 0)
1606                         FILE_OP_ERROR(file, "unlink");
1607         }
1608
1609         return num;
1610 }
1611
1612 /*
1613 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1614 {
1615         Folder *folder;
1616         gint num;
1617
1618         g_return_val_if_fail(dest != NULL, -1);
1619         g_return_val_if_fail(msginfo != NULL, -1);
1620
1621         folder = dest->folder;
1622         if (dest->last_num < 0) folder->scan(folder, dest);
1623
1624         num = folder->move_msg(folder, dest, msginfo);
1625         if (num > 0) dest->last_num = num;
1626
1627         return num;
1628 }
1629 */
1630                 
1631 FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest) 
1632 {
1633         GSList *mlist;
1634         FolderItem *new_item;
1635         FolderItem *next_item;
1636         GNode *srcnode;
1637         gchar *old_id, *new_id;
1638
1639         mlist = folder_item_get_msg_list(src);
1640
1641         /* move messages */
1642         debug_print("Moving %s to %s\n", src->path, dest->path);
1643         new_item = folder_create_folder(dest, g_basename(src->path));
1644         if (new_item == NULL) {
1645                 printf("Can't create folder\n");
1646                 return NULL;
1647         }
1648         
1649         if (new_item->folder == NULL)
1650                 new_item->folder = dest->folder;
1651
1652         /* move messages */
1653         log_message(_("Moving %s to %s...\n"), 
1654                         src->name, new_item->path);
1655         folder_item_move_msgs_with_dest(new_item, mlist);
1656         
1657         /*copy prefs*/
1658         prefs_folder_item_copy_prefs(src, new_item);
1659         new_item->collapsed = src->collapsed;
1660         new_item->thread_collapsed = src->thread_collapsed;
1661         new_item->threaded  = src->threaded;
1662         new_item->ret_rcpt  = src->ret_rcpt;
1663         new_item->hide_read_msgs = src->hide_read_msgs;
1664         new_item->sort_key  = src->sort_key;
1665         new_item->sort_type = src->sort_type;
1666
1667         prefs_matcher_write_config();
1668         
1669         /* recurse */
1670         srcnode = src->folder->node;    
1671         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
1672         srcnode = srcnode->children;
1673         while (srcnode != NULL) {
1674                 if (srcnode && srcnode->data) {
1675                         next_item = (FolderItem*) srcnode->data;
1676                         srcnode = srcnode->next;
1677                         if (folder_item_move_recursive(next_item, new_item) == NULL)
1678                                 return NULL;
1679                 }
1680         }
1681         old_id = folder_item_get_identifier(src);
1682         new_id = folder_item_get_identifier(new_item);
1683         debug_print("updating rules : %s => %s\n", old_id, new_id);
1684         
1685         src->folder->class->remove_folder(src->folder, src);
1686         folder_write_list();
1687
1688         if (old_id != NULL && new_id != NULL)
1689                 prefs_filtering_rename_path(old_id, new_id);
1690         g_free(old_id);
1691         g_free(new_id);
1692
1693         return new_item;
1694 }
1695
1696 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item)
1697 {
1698         FolderItem *tmp = dest->parent;
1699         gchar * src_identifier, * dst_identifier;
1700         gchar * phys_srcpath, * phys_dstpath;
1701         GNode *src_node;
1702         
1703         while (tmp) {
1704                 if (tmp == src) {
1705                         return F_MOVE_FAILED_DEST_IS_CHILD;
1706                 }
1707                 tmp = tmp->parent;
1708         }
1709         
1710         tmp = src->parent;
1711         
1712         src_identifier = folder_item_get_identifier(src);
1713         dst_identifier = folder_item_get_identifier(dest);
1714         
1715         if(dst_identifier == NULL && dest->folder && dest->parent == NULL) {
1716                 /* dest can be a root folder */
1717                 dst_identifier = folder_get_identifier(dest->folder);
1718         }
1719         if (src_identifier == NULL || dst_identifier == NULL) {
1720                 debug_print("Can't get identifiers\n");
1721                 return F_MOVE_FAILED;
1722         }
1723
1724         if (src->folder != dest->folder) {
1725                 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
1726         }
1727
1728         phys_srcpath = folder_item_get_path(src);
1729         phys_dstpath = g_strconcat(folder_item_get_path(dest),G_DIR_SEPARATOR_S,g_basename(phys_srcpath),NULL);
1730
1731         if (src->parent == dest || src == dest) {
1732                 g_free(src_identifier);
1733                 g_free(dst_identifier);
1734                 g_free(phys_srcpath);
1735                 g_free(phys_dstpath);
1736                 return F_MOVE_FAILED_DEST_IS_PARENT;
1737         }
1738         debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
1739         if ((tmp = folder_item_move_recursive(src, dest)) == NULL) {
1740                 return F_MOVE_FAILED;
1741         }
1742         
1743         /* update rules */
1744         src_node = g_node_find(src->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, src);
1745         if (src_node) 
1746                 g_node_destroy(src_node);
1747         else
1748                 debug_print("can't remove node: it's null!\n");
1749         /* not to much worry if remove fails, move has been done */
1750         
1751         g_free(src_identifier);
1752         g_free(dst_identifier);
1753         g_free(phys_srcpath);
1754         g_free(phys_dstpath);
1755
1756         *new_item = tmp;
1757
1758         return F_MOVE_OK;
1759 }
1760
1761 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1762 {
1763         GSList *list = NULL;
1764         gint ret;
1765
1766         list = g_slist_append(list, msginfo);
1767         ret = folder_item_move_msgs_with_dest(dest, list);
1768         g_slist_free(list);
1769         
1770         return ret;
1771 }
1772
1773 /*
1774 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1775 {
1776         Folder *folder;
1777         gint num;
1778
1779         g_return_val_if_fail(dest != NULL, -1);
1780         g_return_val_if_fail(msglist != NULL, -1);
1781
1782         folder = dest->folder;
1783         if (dest->last_num < 0) folder->scan(folder, dest);
1784
1785         num = folder->move_msgs_with_dest(folder, dest, msglist);
1786         if (num > 0) dest->last_num = num;
1787         else dest->op_count = 0;
1788
1789         return num;
1790 }
1791 */
1792
1793
1794
1795 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1796 {
1797         Folder *folder;
1798         FolderItem *item;
1799         GSList *newmsgnums = NULL;
1800         GSList *l, *l2;
1801         gint num, lastnum = -1;
1802         gboolean folderscan = FALSE;
1803
1804         g_return_val_if_fail(dest != NULL, -1);
1805         g_return_val_if_fail(msglist != NULL, -1);
1806
1807         folder = dest->folder;
1808
1809         g_return_val_if_fail(folder->class->copy_msg != NULL, -1);
1810         g_return_val_if_fail(folder->class->remove_msg != NULL, -1);
1811
1812         /* 
1813          * Copy messages to destination folder and 
1814          * store new message numbers in newmsgnums
1815          */
1816         item = NULL;
1817         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
1818                 MsgInfo * msginfo = (MsgInfo *) l->data;
1819
1820                 if (!item && msginfo->folder != NULL)
1821                         item = msginfo->folder;
1822
1823                 num = folder->class->copy_msg(folder, dest, msginfo);
1824                 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1825         }
1826
1827         /* Read cache for dest folder */
1828         if (!dest->cache) folder_item_read_cache(dest);
1829
1830         /* 
1831          * Fetch new MsgInfos for new messages in dest folder,
1832          * add them to the msgcache and update folder message counts
1833          */
1834         l2 = newmsgnums;
1835         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1836                 MsgInfo *msginfo = (MsgInfo *) l->data;
1837
1838                 num = GPOINTER_TO_INT(l2->data);
1839                 l2 = g_slist_next(l2);
1840
1841                 if (num >= 0) {
1842                         MsgInfo *newmsginfo;
1843
1844                         if (num == 0) {
1845                                 gchar *file;
1846
1847                                 if (!folderscan) {
1848                                         folder_item_scan(dest);
1849                                         folderscan = TRUE;
1850                                 }
1851                                 file = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
1852                                 num = folder_item_get_msg_num_by_file(dest, file);
1853                                 g_free(file);
1854                         }
1855
1856                         if (num > lastnum)
1857                                 lastnum = num;
1858
1859                         if (num == 0)
1860                                 continue;
1861
1862                         if (!folderscan && 
1863                             ((newmsginfo = folder->class->get_msginfo(folder, dest, num)) != NULL)) {
1864                                 newmsginfo = folder->class->get_msginfo(folder, dest, num);
1865                                 add_msginfo_to_cache(dest, newmsginfo, msginfo);
1866                                 procmsg_msginfo_free(newmsginfo);
1867                         } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
1868                                 copy_msginfo_flags(msginfo, newmsginfo);
1869                                 procmsg_msginfo_free(newmsginfo);
1870                         }
1871                 }
1872         }
1873
1874         /*
1875          * Remove source messages from their folders if
1876          * copying was successfull and update folder
1877          * message counts
1878          */
1879         l2 = newmsgnums;
1880         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1881                 MsgInfo *msginfo = (MsgInfo *) l->data;
1882
1883                 num = GPOINTER_TO_INT(l2->data);
1884                 l2 = g_slist_next(l2);
1885                 
1886                 if (num >= 0) {
1887                         item->folder->class->remove_msg(item->folder,
1888                                                         msginfo->folder,
1889                                                         msginfo->msgnum);
1890                         remove_msginfo_from_cache(item, msginfo);
1891                 }
1892         }
1893
1894
1895         if (folder->class->finished_copy)
1896                 folder->class->finished_copy(folder, dest);
1897
1898         g_slist_free(newmsgnums);
1899         return lastnum;
1900 }
1901
1902 /*
1903 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1904 {
1905         Folder *folder;
1906         gint num;
1907
1908         g_return_val_if_fail(dest != NULL, -1);
1909         g_return_val_if_fail(msginfo != NULL, -1);
1910
1911         folder = dest->folder;
1912         if (dest->last_num < 0) folder->scan(folder, dest);
1913
1914         num = folder->copy_msg(folder, dest, msginfo);
1915         if (num > 0) dest->last_num = num;
1916
1917         return num;
1918 }
1919 */
1920
1921 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1922 {
1923         GSList *list = NULL;
1924         gint ret;
1925
1926         list = g_slist_append(list, msginfo);
1927         ret = folder_item_copy_msgs_with_dest(dest, list);
1928         g_slist_free(list);
1929         
1930         return ret;
1931 }
1932
1933 /*
1934 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1935 {
1936         Folder *folder;
1937         gint num;
1938
1939         g_return_val_if_fail(dest != NULL, -1);
1940         g_return_val_if_fail(msglist != NULL, -1);
1941
1942         folder = dest->folder;
1943         if (dest->last_num < 0) folder->scan(folder, dest);
1944
1945         num = folder->copy_msgs_with_dest(folder, dest, msglist);
1946         if (num > 0) dest->last_num = num;
1947         else dest->op_count = 0;
1948
1949         return num;
1950 }
1951 */
1952
1953 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1954 {
1955         Folder *folder;
1956         gint num, lastnum = -1;
1957         GSList *newmsgnums = NULL;
1958         GSList *l, *l2;
1959         gboolean folderscan = FALSE;
1960
1961         g_return_val_if_fail(dest != NULL, -1);
1962         g_return_val_if_fail(msglist != NULL, -1);
1963
1964         folder = dest->folder;
1965  
1966         g_return_val_if_fail(folder->class->copy_msg != NULL, -1);
1967
1968         /* 
1969          * Copy messages to destination folder and 
1970          * store new message numbers in newmsgnums
1971          */
1972         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
1973                 MsgInfo * msginfo = (MsgInfo *) l->data;
1974
1975                 num = folder->class->copy_msg(folder, dest, msginfo);
1976                 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1977         }
1978
1979         /* Read cache for dest folder */
1980         if (!dest->cache) folder_item_read_cache(dest);
1981
1982         /* 
1983          * Fetch new MsgInfos for new messages in dest folder,
1984          * add them to the msgcache and update folder message counts
1985          */
1986         l2 = newmsgnums;
1987         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1988                 MsgInfo *msginfo = (MsgInfo *) l->data;
1989
1990                 num = GPOINTER_TO_INT(l2->data);
1991                 l2 = g_slist_next(l2);
1992
1993                 if (num >= 0) {
1994                         MsgInfo *newmsginfo;
1995
1996                         if (num == 0) {
1997                                 gchar *file;
1998
1999                                 if (!folderscan) {
2000                                         folder_item_scan(dest);
2001                                         folderscan = TRUE;
2002                                 }
2003                                 file = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
2004                                 num = folder_item_get_msg_num_by_file(dest, file);
2005                                 g_free(file);
2006                         }
2007         
2008                         if (num > lastnum)
2009                                 lastnum = num;
2010
2011                         if (num == 0)
2012                                 continue;
2013
2014                         if (!folderscan && 
2015                             ((newmsginfo = folder->class->get_msginfo(folder, dest, num)) != NULL)) {
2016                                 newmsginfo = folder->class->get_msginfo(folder, dest, num);
2017                                 add_msginfo_to_cache(dest, newmsginfo, msginfo);
2018                                 procmsg_msginfo_free(newmsginfo);
2019                         } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
2020                                 copy_msginfo_flags(msginfo, newmsginfo);
2021                                 procmsg_msginfo_free(newmsginfo);
2022                         }
2023                 }
2024         }
2025         
2026         if (folder->class->finished_copy)
2027                 folder->class->finished_copy(folder, dest);
2028
2029         g_slist_free(newmsgnums);
2030         return lastnum;
2031 }
2032
2033 gint folder_item_remove_msg(FolderItem *item, gint num)
2034 {
2035         Folder *folder;
2036         gint ret;
2037         MsgInfo *msginfo;
2038
2039         g_return_val_if_fail(item != NULL, -1);
2040
2041         folder = item->folder;
2042         if (!item->cache) folder_item_read_cache(item);
2043
2044         ret = folder->class->remove_msg(folder, item, num);
2045
2046         msginfo = msgcache_get_msg(item->cache, num);
2047         if (msginfo != NULL) {
2048                 remove_msginfo_from_cache(item, msginfo);
2049                 procmsg_msginfo_free(msginfo);
2050         }
2051         item->total--;
2052         folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2053
2054         return ret;
2055 }
2056
2057 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
2058 {
2059         Folder *folder;
2060         gint ret = 0;
2061
2062         g_return_val_if_fail(item != NULL, -1);
2063         folder = item->folder;
2064         g_return_val_if_fail(folder != NULL, -1);
2065
2066         if (!item->cache) folder_item_read_cache(item);
2067
2068         if (folder->class->remove_msgs) {
2069                 ret = folder->class->remove_msgs(folder, item, msglist);
2070                 if (ret == 0)
2071                         folder_item_scan(item);
2072                 return ret;
2073         }
2074
2075         while (msglist != NULL) {
2076                 MsgInfo *msginfo = (MsgInfo *)msglist->data;
2077
2078                 ret = folder_item_remove_msg(item, msginfo->msgnum);
2079                 if (ret != 0) break;
2080                 msgcache_remove_msg(item->cache, msginfo->msgnum);
2081                 msglist = msglist->next;
2082         }
2083
2084         return ret;
2085 }
2086
2087 gint folder_item_remove_all_msg(FolderItem *item)
2088 {
2089         Folder *folder;
2090         gint result;
2091
2092         g_return_val_if_fail(item != NULL, -1);
2093
2094         folder = item->folder;
2095
2096         g_return_val_if_fail(folder->class->remove_all_msg != NULL, -1);
2097
2098         result = folder->class->remove_all_msg(folder, item);
2099
2100         if (result == 0) {
2101                 if (folder->class->finished_remove)
2102                         folder->class->finished_remove(folder, item);
2103
2104                 folder_item_free_cache(item);
2105                 item->cache = msgcache_new();
2106
2107                 item->new = 0;
2108                 item->unread = 0;
2109                 item->unreadmarked = 0;
2110                 item->total = 0;
2111                 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2112         }
2113
2114         return result;
2115 }
2116
2117 void folder_item_change_msg_flags(FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
2118 {
2119         g_return_if_fail(item != NULL);
2120         g_return_if_fail(msginfo != NULL);
2121         
2122         if (item->folder->class->change_flags != NULL) {
2123                 item->folder->class->change_flags(item->folder, item, msginfo, newflags);
2124         } else {
2125                 msginfo->flags.perm_flags = newflags;
2126         }
2127 }
2128
2129 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
2130 {
2131         Folder *folder;
2132
2133         g_return_val_if_fail(item != NULL, FALSE);
2134
2135         folder = item->folder;
2136
2137         g_return_val_if_fail(folder->class->is_msg_changed != NULL, -1);
2138
2139         return folder->class->is_msg_changed(folder, item, msginfo);
2140 }
2141
2142 gchar *folder_item_get_cache_file(FolderItem *item)
2143 {
2144         gchar *path;
2145         gchar *file;
2146
2147         g_return_val_if_fail(item != NULL, NULL);
2148         g_return_val_if_fail(item->path != NULL, NULL);
2149
2150         path = folder_item_get_path(item);
2151         g_return_val_if_fail(path != NULL, NULL);
2152         if (!is_dir_exist(path))
2153                 make_dir_hier(path);
2154         file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
2155         g_free(path);
2156
2157         return file;
2158 }
2159
2160 gchar *folder_item_get_mark_file(FolderItem *item)
2161 {
2162         gchar *path;
2163         gchar *file;
2164
2165         g_return_val_if_fail(item != NULL, NULL);
2166         g_return_val_if_fail(item->path != NULL, NULL);
2167
2168         path = folder_item_get_path(item);
2169         g_return_val_if_fail(path != NULL, NULL);
2170         if (!is_dir_exist(path))
2171                 make_dir_hier(path);
2172         file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
2173         g_free(path);
2174
2175         return file;
2176 }
2177
2178 static gboolean folder_build_tree(GNode *node, gpointer data)
2179 {
2180         Folder *folder = FOLDER(data);
2181         FolderItem *item;
2182         XMLNode *xmlnode;
2183         GList *list;
2184         SpecialFolderItemType stype = F_NORMAL;
2185         const gchar *name = NULL;
2186         const gchar *path = NULL;
2187         PrefsAccount *account = NULL;
2188         gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE, 
2189                  threaded = TRUE, apply_sub = FALSE;
2190         gboolean ret_rcpt = FALSE, hidereadmsgs = FALSE,
2191                  thread_collapsed = FALSE; /* CLAWS */
2192         FolderSortKey sort_key = SORT_BY_NONE;
2193         FolderSortType sort_type = SORT_ASCENDING;
2194         gint new = 0, unread = 0, total = 0, unreadmarked = 0;
2195         time_t mtime = 0;
2196
2197         g_return_val_if_fail(node->data != NULL, FALSE);
2198         if (!node->parent) return FALSE;
2199
2200         xmlnode = node->data;
2201         if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
2202                 g_warning("tag name != \"folderitem\"\n");
2203                 return FALSE;
2204         }
2205
2206         list = xmlnode->tag->attr;
2207         for (; list != NULL; list = list->next) {
2208                 XMLAttr *attr = list->data;
2209
2210                 if (!attr || !attr->name || !attr->value) continue;
2211                 if (!strcmp(attr->name, "type")) {
2212                         if (!strcasecmp(attr->value, "normal"))
2213                                 stype = F_NORMAL;
2214                         else if (!strcasecmp(attr->value, "inbox"))
2215                                 stype = F_INBOX;
2216                         else if (!strcasecmp(attr->value, "outbox"))
2217                                 stype = F_OUTBOX;
2218                         else if (!strcasecmp(attr->value, "draft"))
2219                                 stype = F_DRAFT;
2220                         else if (!strcasecmp(attr->value, "queue"))
2221                                 stype = F_QUEUE;
2222                         else if (!strcasecmp(attr->value, "trash"))
2223                                 stype = F_TRASH;
2224                 } else if (!strcmp(attr->name, "name"))
2225                         name = attr->value;
2226                 else if (!strcmp(attr->name, "path"))
2227                         path = attr->value;
2228                 else if (!strcmp(attr->name, "mtime"))
2229                         mtime = strtoul(attr->value, NULL, 10);
2230                 else if (!strcmp(attr->name, "new"))
2231                         new = atoi(attr->value);
2232                 else if (!strcmp(attr->name, "unread"))
2233                         unread = atoi(attr->value);
2234                 else if (!strcmp(attr->name, "unreadmarked"))
2235                         unreadmarked = atoi(attr->value);
2236                 else if (!strcmp(attr->name, "total"))
2237                         total = atoi(attr->value);
2238                 else if (!strcmp(attr->name, "no_sub"))
2239                         no_sub = *attr->value == '1' ? TRUE : FALSE;
2240                 else if (!strcmp(attr->name, "no_select"))
2241                         no_select = *attr->value == '1' ? TRUE : FALSE;
2242                 else if (!strcmp(attr->name, "collapsed"))
2243                         collapsed = *attr->value == '1' ? TRUE : FALSE;
2244                 else if (!strcmp(attr->name, "thread_collapsed"))
2245                         thread_collapsed =  *attr->value == '1' ? TRUE : FALSE;
2246                 else if (!strcmp(attr->name, "threaded"))
2247                         threaded =  *attr->value == '1' ? TRUE : FALSE;
2248                 else if (!strcmp(attr->name, "hidereadmsgs"))
2249                         hidereadmsgs =  *attr->value == '1' ? TRUE : FALSE;
2250                 else if (!strcmp(attr->name, "reqretrcpt"))
2251                         ret_rcpt =  *attr->value == '1' ? TRUE : FALSE;
2252                 else if (!strcmp(attr->name, "sort_key")) {
2253                         if (!strcmp(attr->value, "none"))
2254                                 sort_key = SORT_BY_NONE;
2255                         else if (!strcmp(attr->value, "number"))
2256                                 sort_key = SORT_BY_NUMBER;
2257                         else if (!strcmp(attr->value, "size"))
2258                                 sort_key = SORT_BY_SIZE;
2259                         else if (!strcmp(attr->value, "date"))
2260                                 sort_key = SORT_BY_DATE;
2261                         else if (!strcmp(attr->value, "from"))
2262                                 sort_key = SORT_BY_FROM;
2263                         else if (!strcmp(attr->value, "subject"))
2264                                 sort_key = SORT_BY_SUBJECT;
2265                         else if (!strcmp(attr->value, "score"))
2266                                 sort_key = SORT_BY_SCORE;
2267                         else if (!strcmp(attr->value, "label"))
2268                                 sort_key = SORT_BY_LABEL;
2269                         else if (!strcmp(attr->value, "mark"))
2270                                 sort_key = SORT_BY_MARK;
2271                         else if (!strcmp(attr->value, "unread"))
2272                                 sort_key = SORT_BY_STATUS;
2273                         else if (!strcmp(attr->value, "mime"))
2274                                 sort_key = SORT_BY_MIME;
2275                         else if (!strcmp(attr->value, "to"))
2276                                 sort_key = SORT_BY_TO;
2277                         else if (!strcmp(attr->value, "locked"))
2278                                 sort_key = SORT_BY_LOCKED;
2279                 } else if (!strcmp(attr->name, "sort_type")) {
2280                         if (!strcmp(attr->value, "ascending"))
2281                                 sort_type = SORT_ASCENDING;
2282                         else
2283                                 sort_type = SORT_DESCENDING;
2284                 } else if (!strcmp(attr->name, "account_id")) {
2285                         account = account_find_from_id(atoi(attr->value));
2286                         if (!account) g_warning("account_id: %s not found\n",
2287                                                 attr->value);
2288                 } else if (!strcmp(attr->name, "apply_sub"))
2289                         apply_sub = *attr->value == '1' ? TRUE : FALSE;
2290         }
2291
2292         item = folder_item_new(folder, name, path);
2293         item->stype = stype;
2294         item->mtime = mtime;
2295         item->new = new;
2296         item->unread = unread;
2297         item->unreadmarked = unreadmarked;
2298         item->total = total;
2299         item->no_sub = no_sub;
2300         item->no_select = no_select;
2301         item->collapsed = collapsed;
2302         item->thread_collapsed = thread_collapsed;
2303         item->threaded  = threaded;
2304         item->hide_read_msgs  = hidereadmsgs;
2305         item->ret_rcpt  = ret_rcpt;
2306         item->sort_key  = sort_key;
2307         item->sort_type = sort_type;
2308         item->parent = FOLDER_ITEM(node->parent->data);
2309         item->folder = folder;
2310         switch (stype) {
2311         case F_INBOX:  folder->inbox  = item; break;
2312         case F_OUTBOX: folder->outbox = item; break;
2313         case F_DRAFT:  folder->draft  = item; break;
2314         case F_QUEUE:  folder->queue  = item; break;
2315         case F_TRASH:  folder->trash  = item; break;
2316         default:       break;
2317         }
2318         item->account = account;
2319         item->apply_sub = apply_sub;
2320         prefs_folder_item_read_config(item);
2321
2322         node->data = item;
2323         xml_free_node(xmlnode);
2324
2325         return FALSE;
2326 }
2327
2328 static gboolean folder_read_folder_func(GNode *node, gpointer data)
2329 {
2330         Folder *folder;
2331         XMLNode *xmlnode;
2332         GList *list;
2333         FolderClass *class = NULL;
2334         const gchar *name = NULL;
2335         const gchar *path = NULL;
2336         PrefsAccount *account = NULL;
2337         gboolean collapsed = FALSE, threaded = TRUE, apply_sub = FALSE;
2338         gboolean ret_rcpt = FALSE, thread_collapsed = FALSE; /* CLAWS */
2339
2340         if (g_node_depth(node) != 2) return FALSE;
2341         g_return_val_if_fail(node->data != NULL, FALSE);
2342
2343         xmlnode = node->data;
2344         if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
2345                 g_warning("tag name != \"folder\"\n");
2346                 return TRUE;
2347         }
2348         g_node_unlink(node);
2349         list = xmlnode->tag->attr;
2350         for (; list != NULL; list = list->next) {
2351                 XMLAttr *attr = list->data;
2352
2353                 if (!attr || !attr->name || !attr->value) continue;
2354                 if (!strcmp(attr->name, "type"))
2355                         class = folder_get_class_from_string(attr->value);
2356                 else if (!strcmp(attr->name, "name"))
2357                         name = attr->value;
2358                 else if (!strcmp(attr->name, "path"))
2359                         path = attr->value;
2360                 else if (!strcmp(attr->name, "collapsed"))
2361                         collapsed = *attr->value == '1' ? TRUE : FALSE;
2362                 else if (!strcmp(attr->name, "thread_collapsed"))
2363                         thread_collapsed = *attr->value == '1' ? TRUE : FALSE;
2364                 else if (!strcmp(attr->name, "threaded"))
2365                         threaded = *attr->value == '1' ? TRUE : FALSE;
2366                 else if (!strcmp(attr->name, "account_id")) {
2367                         account = account_find_from_id(atoi(attr->value));
2368                         if (!account) g_warning("account_id: %s not found\n",
2369                                                 attr->value);
2370                 } else if (!strcmp(attr->name, "apply_sub"))
2371                         apply_sub = *attr->value == '1' ? TRUE : FALSE;
2372                 else if (!strcmp(attr->name, "reqretrcpt"))
2373                         ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
2374         }
2375
2376         folder = folder_new(class, name, path);
2377         g_return_val_if_fail(folder != NULL, FALSE);
2378         folder->account = account;
2379         if (account != NULL)
2380                 account->folder = REMOTE_FOLDER(folder);
2381         node->data = folder->node->data;
2382         g_node_destroy(folder->node);
2383         folder->node = node;
2384         folder_add(folder);
2385         FOLDER_ITEM(node->data)->collapsed = collapsed;
2386         FOLDER_ITEM(node->data)->thread_collapsed = thread_collapsed;
2387         FOLDER_ITEM(node->data)->threaded  = threaded;
2388         FOLDER_ITEM(node->data)->account   = account;
2389         FOLDER_ITEM(node->data)->apply_sub = apply_sub;
2390         FOLDER_ITEM(node->data)->ret_rcpt  = ret_rcpt;
2391
2392         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2393                         folder_build_tree, folder);
2394
2395         return FALSE;
2396 }
2397
2398 static gchar *folder_get_list_path(void)
2399 {
2400         static gchar *filename = NULL;
2401
2402         if (!filename)
2403                 filename =  g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2404                                         FOLDER_LIST, NULL);
2405
2406         return filename;
2407 }
2408
2409 #define PUT_ESCAPE_STR(fp, attr, str)                   \
2410 {                                                       \
2411         fputs(" " attr "=\"", fp);                      \
2412         xml_file_put_escape_str(fp, str);               \
2413         fputs("\"", fp);                                \
2414 }
2415
2416 static void folder_write_list_recursive(GNode *node, gpointer data)
2417 {
2418         FILE *fp = (FILE *)data;
2419         FolderItem *item;
2420         gint i, depth;
2421         static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
2422                                                  "draft", "queue", "trash"};
2423         static gchar *sort_key_str[] = {"none", "number", "size", "date",
2424                                         "from", "subject", "score", "label",
2425                                         "mark", "unread", "mime", "to", 
2426                                         "locked"};
2427         g_return_if_fail(node != NULL);
2428         g_return_if_fail(fp != NULL);
2429
2430         item = FOLDER_ITEM(node->data);
2431         g_return_if_fail(item != NULL);
2432
2433         depth = g_node_depth(node);
2434         for (i = 0; i < depth; i++)
2435                 fputs("    ", fp);
2436         if (depth == 1) {
2437                 Folder *folder = item->folder;
2438
2439                 fprintf(fp, "<folder type=\"%s\"", folder->class->idstr);
2440                 if (folder->name)
2441                         PUT_ESCAPE_STR(fp, "name", folder->name);
2442                 if (FOLDER_TYPE(folder) == F_MH || FOLDER_TYPE(folder) == F_MBOX)
2443                         PUT_ESCAPE_STR(fp, "path",
2444                                        LOCAL_FOLDER(folder)->rootpath);
2445                 if (item->collapsed && node->children)
2446                         fputs(" collapsed=\"1\"", fp);
2447                 if (folder->account)
2448                         fprintf(fp, " account_id=\"%d\"",
2449                                 folder->account->account_id);
2450                 if (item->apply_sub)
2451                         fputs(" apply_sub=\"1\"", fp);
2452                 if (item->ret_rcpt) 
2453                         fputs(" reqretrcpt=\"1\"", fp);
2454         } else {
2455                 fprintf(fp, "<folderitem type=\"%s\"",
2456                         folder_item_stype_str[item->stype]);
2457                 if (item->name)
2458                         PUT_ESCAPE_STR(fp, "name", item->name);
2459                 if (item->path)
2460                         PUT_ESCAPE_STR(fp, "path", item->path);
2461
2462                 if (item->no_sub)
2463                         fputs(" no_sub=\"1\"", fp);
2464                 if (item->no_select)
2465                         fputs(" no_select=\"1\"", fp);
2466                 if (item->collapsed && node->children)
2467                         fputs(" collapsed=\"1\"", fp);
2468                 else
2469                         fputs(" collapsed=\"0\"", fp);
2470                 if (item->thread_collapsed)
2471                         fputs(" thread_collapsed=\"1\"", fp);
2472                 else
2473                         fputs(" thread_collapsed=\"0\"", fp);
2474                 if (item->threaded)
2475                         fputs(" threaded=\"1\"", fp);
2476                 else
2477                         fputs(" threaded=\"0\"", fp);
2478                 if (item->hide_read_msgs)
2479                         fputs(" hidereadmsgs=\"1\"", fp);
2480                 else
2481                         fputs(" hidereadmsgs=\"0\"", fp);
2482                 if (item->ret_rcpt)
2483                         fputs(" reqretrcpt=\"1\"", fp);
2484
2485                 if (item->sort_key != SORT_BY_NONE) {
2486                         fprintf(fp, " sort_key=\"%s\"",
2487                                 sort_key_str[item->sort_key]);
2488                         if (item->sort_type == SORT_ASCENDING)
2489                                 fprintf(fp, " sort_type=\"ascending\"");
2490                         else
2491                                 fprintf(fp, " sort_type=\"descending\"");
2492                 }
2493
2494                 fprintf(fp,
2495                         " mtime=\"%lu\" new=\"%d\" unread=\"%d\" unreadmarked=\"%d\" total=\"%d\"",
2496                         item->mtime, item->new, item->unread, item->unreadmarked, item->total);
2497
2498                 if (item->account)
2499                         fprintf(fp, " account_id=\"%d\"",
2500                                 item->account->account_id);
2501                 if (item->apply_sub)
2502                         fputs(" apply_sub=\"1\"", fp);
2503         }
2504
2505         if (node->children) {
2506                 GNode *child;
2507                 fputs(">\n", fp);
2508
2509                 child = node->children;
2510                 while (child) {
2511                         GNode *cur;
2512
2513                         cur = child;
2514                         child = cur->next;
2515                         folder_write_list_recursive(cur, data);
2516                 }
2517
2518                 for (i = 0; i < depth; i++)
2519                         fputs("    ", fp);
2520                 fprintf(fp, "</%s>\n", depth == 1 ? "folder" : "folderitem");
2521         } else
2522                 fputs(" />\n", fp);
2523 }
2524
2525 static void folder_update_op_count_rec(GNode *node)
2526 {
2527         FolderItem *fitem = FOLDER_ITEM(node->data);
2528
2529         if (g_node_depth(node) > 0) {
2530                 if (fitem->op_count > 0) {
2531                         fitem->op_count = 0;
2532                         folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
2533                 }
2534                 if (node->children) {
2535                         GNode *child;
2536
2537                         child = node->children;
2538                         while (child) {
2539                                 GNode *cur;
2540
2541                                 cur = child;
2542                                 child = cur->next;
2543                                 folder_update_op_count_rec(cur);
2544                         }
2545                 }
2546         }
2547 }
2548
2549 void folder_update_op_count() {
2550         GList *cur;
2551         Folder *folder;
2552
2553         for (cur = folder_list; cur != NULL; cur = cur->next) {
2554                 folder = cur->data;
2555                 folder_update_op_count_rec(folder->node);
2556         }
2557 }
2558
2559 typedef struct _type_str {
2560         gchar * str;
2561         gint type;
2562 } type_str;
2563
2564
2565 /*
2566 static gchar * folder_item_get_tree_identifier(FolderItem * item)
2567 {
2568         if (item->parent != NULL) {
2569                 gchar * path;
2570                 gchar * id;
2571
2572                 path = folder_item_get_tree_identifier(item->parent);
2573                 if (path == NULL)
2574                         return NULL;
2575
2576                 id = g_strconcat(path, "/", item->name, NULL);
2577                 g_free(path);
2578
2579                 return id;
2580         }
2581         else {
2582                 return g_strconcat("/", item->name, NULL);
2583         }
2584 }
2585 */
2586
2587 /* CLAWS: temporary local folder for filtering */
2588 static Folder *processing_folder;
2589 static FolderItem *processing_folder_item;
2590
2591 static void folder_create_processing_folder(void)
2592 {
2593 #define PROCESSING_FOLDER ".processing" 
2594         Folder     *tmpparent;
2595         gchar      *tmpname;
2596
2597         tmpparent = folder_get_default_folder();
2598         g_assert(tmpparent);
2599         debug_print("tmpparentroot %s\n", LOCAL_FOLDER(tmpparent)->rootpath);
2600         if (LOCAL_FOLDER(tmpparent)->rootpath[0] == '/')
2601                 tmpname = g_strconcat(LOCAL_FOLDER(tmpparent)->rootpath,
2602                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2603                                       NULL);
2604         else
2605                 tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2606                                       LOCAL_FOLDER(tmpparent)->rootpath,
2607                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2608                                       NULL);
2609
2610         processing_folder = folder_new(mh_get_class(), "PROCESSING", LOCAL_FOLDER(tmpparent)->rootpath);
2611         g_assert(processing_folder);
2612
2613         if (!is_dir_exist(tmpname)) {
2614                 debug_print("*TMP* creating %s\n", tmpname);
2615                 processing_folder_item = processing_folder->class->create_folder(processing_folder,
2616                                                                                  processing_folder->node->data,
2617                                                                                  PROCESSING_FOLDER);
2618                 g_assert(processing_folder_item);                                                                         
2619         }
2620         else {
2621                 debug_print("*TMP* already created\n");
2622                 processing_folder_item = folder_item_new(processing_folder, ".processing", ".processing");
2623                 g_assert(processing_folder_item);
2624                 folder_item_append(processing_folder->node->data, processing_folder_item);
2625         }
2626         g_free(tmpname);
2627 }
2628
2629 FolderItem *folder_get_default_processing(void)
2630 {
2631         if (!processing_folder_item) {
2632                 folder_create_processing_folder();
2633         }
2634         return processing_folder_item;
2635 }
2636
2637 /* folder_persist_prefs_new() - return hash table with persistent
2638  * settings (and folder name as key). 
2639  * (note that in claws other options are in the PREFS_FOLDER_ITEM_RC
2640  * file, so those don't need to be included in PersistPref yet) 
2641  */
2642 GHashTable *folder_persist_prefs_new(Folder *folder)
2643 {
2644         GHashTable *pptable;
2645
2646         g_return_val_if_fail(folder, NULL);
2647         pptable = g_hash_table_new(g_str_hash, g_str_equal);
2648         folder_get_persist_prefs_recursive(folder->node, pptable);
2649         return pptable;
2650 }
2651
2652 void folder_persist_prefs_free(GHashTable *pptable)
2653 {
2654         g_return_if_fail(pptable);
2655         g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
2656         g_hash_table_destroy(pptable);
2657 }
2658
2659 const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
2660 {
2661         if (pptable == NULL || name == NULL) return NULL;
2662         return g_hash_table_lookup(pptable, name);
2663 }
2664
2665 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
2666 {
2667         const PersistPrefs *pp;
2668         gchar *id = folder_item_get_identifier(item);
2669
2670         pp = folder_get_persist_prefs(pptable, id); 
2671         g_free(id);
2672
2673         if (!pp) return;
2674
2675         /* CLAWS: since not all folder properties have been migrated to 
2676          * folderlist.xml, we need to call the old stuff first before
2677          * setting things that apply both to Main and Claws. */
2678         prefs_folder_item_read_config(item); 
2679          
2680         item->collapsed = pp->collapsed;
2681         item->thread_collapsed = pp->thread_collapsed;
2682         item->threaded  = pp->threaded;
2683         item->ret_rcpt  = pp->ret_rcpt;
2684         item->hide_read_msgs = pp->hide_read_msgs;
2685         item->sort_key  = pp->sort_key;
2686         item->sort_type = pp->sort_type;
2687 }
2688
2689 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
2690 {
2691         FolderItem *item = FOLDER_ITEM(node->data);
2692         PersistPrefs *pp;
2693         GNode *child, *cur;
2694         gchar *id;
2695
2696         g_return_if_fail(node != NULL);
2697         g_return_if_fail(item != NULL);
2698
2699         /* NOTE: item->path == NULL means top level folder; not interesting
2700          * to store preferences of that one.  */
2701         if (item->path) {
2702                 id = folder_item_get_identifier(item);
2703                 pp = g_new0(PersistPrefs, 1);
2704                 g_return_if_fail(pp != NULL);
2705                 pp->collapsed = item->collapsed;
2706                 pp->thread_collapsed = item->thread_collapsed;
2707                 pp->threaded  = item->threaded;
2708                 pp->ret_rcpt  = item->ret_rcpt; 
2709                 pp->hide_read_msgs = item->hide_read_msgs;
2710                 pp->sort_key  = item->sort_key;
2711                 pp->sort_type = item->sort_type;
2712                 g_hash_table_insert(pptable, id, pp);
2713         }
2714
2715         if (node->children) {
2716                 child = node->children;
2717                 while (child) {
2718                         cur = child;
2719                         child = cur->next;
2720                         folder_get_persist_prefs_recursive(cur, pptable);
2721                 }
2722         }       
2723 }
2724
2725 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
2726 {
2727         if (key) 
2728                 g_free(key);
2729         if (val) 
2730                 g_free(val);
2731         return TRUE;    
2732 }
2733
2734 void folder_item_apply_processing(FolderItem *item)
2735 {
2736         GSList *processing_list;
2737         GSList *mlist, *cur;
2738         
2739         g_return_if_fail(item != NULL);
2740         
2741         processing_list = item->prefs->processing;
2742         if (processing_list == NULL)
2743                 return;
2744
2745         folder_item_update_freeze();
2746
2747         mlist = folder_item_get_msg_list(item);
2748         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2749                 MsgInfo * msginfo;
2750
2751                 msginfo = (MsgInfo *) cur->data;
2752                 filter_message_by_msginfo(processing_list, msginfo);
2753                 procmsg_msginfo_free(msginfo);
2754         }
2755         g_slist_free(mlist);
2756
2757         folder_item_update_thaw();
2758 }
2759
2760 /*
2761  *  functions for handling FolderItem content changes
2762  */
2763 static gint folder_item_update_freeze_cnt = 0;
2764
2765 /**
2766  * Notify the folder system about changes to a folder. If the
2767  * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
2768  * be invoked, otherwise the changes will be remebered until
2769  * the folder system is thawed.
2770  *
2771  * \param item The FolderItem that was changed
2772  * \param update_flags Type of changed that was made
2773  */
2774 void folder_item_update(FolderItem *item, FolderItemUpdateFlags update_flags)
2775 {
2776         if (folder_item_update_freeze_cnt == 0) {
2777                 FolderItemUpdateData source;
2778         
2779                 source.item = item;
2780                 source.update_flags = update_flags;
2781                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
2782         } else {
2783                 item->update_flags |= update_flags;
2784         }
2785 }
2786
2787 void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
2788 {
2789         GNode *node = item->folder->node;       
2790
2791         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
2792         node = node->children;
2793
2794         folder_item_update(item, update_flags);
2795         while (node != NULL) {
2796                 if (node && node->data) {
2797                         FolderItem *next_item = (FolderItem*) node->data;
2798
2799                         folder_item_update(next_item, update_flags);
2800                 }
2801                 node = node->next;
2802         }
2803 }
2804
2805 void folder_item_update_freeze()
2806 {
2807         folder_item_update_freeze_cnt++;
2808 }
2809
2810 static void folder_item_update_func(FolderItem *item, gpointer data)
2811 {
2812         FolderItemUpdateData source;
2813     
2814         if (item->update_flags) {
2815                 source.item = item;
2816                 source.update_flags = item->update_flags;
2817                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);                             
2818                 item->update_flags = 0;
2819         }
2820 }
2821
2822 void folder_item_update_thaw()
2823 {
2824         if (folder_item_update_freeze_cnt > 0)
2825                 folder_item_update_freeze_cnt--;
2826         if (folder_item_update_freeze_cnt == 0) {
2827                 /* Update all folders */
2828                 folder_func_to_all_folders(folder_item_update_func, NULL);
2829         }
2830 }
2831
2832 #undef PUT_ESCAPE_STR