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