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