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