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