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