* sync with main changes of 2002-10-04
[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 #undef CREATE_FOLDER_IF_NOT_EXIST
799
800 gchar *folder_get_path(Folder *folder)
801 {
802         gchar *path;
803
804         g_return_val_if_fail(folder != NULL, NULL);
805
806         switch(FOLDER_TYPE(folder)) {
807
808                 case F_MH:
809                         path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
810                         break;
811
812                 case F_IMAP:
813                         g_return_val_if_fail(folder->account != NULL, NULL);
814                         path = g_strconcat(get_imap_cache_dir(),
815                                            G_DIR_SEPARATOR_S,
816                                            folder->account->recv_server,
817                                            G_DIR_SEPARATOR_S,
818                                            folder->account->userid,
819                                            NULL);
820                         break;
821
822                 case F_NEWS:
823                         g_return_val_if_fail(folder->account != NULL, NULL);
824                         path = g_strconcat(get_news_cache_dir(),
825                                            G_DIR_SEPARATOR_S,
826                                            folder->account->nntp_server,
827                                            NULL);
828                         break;
829
830                 default:
831                         path = NULL;
832                         break;
833         }
834         
835         return path;
836 }
837
838 gchar *folder_item_get_path(FolderItem *item)
839 {
840         gchar *folder_path;
841         gchar *path;
842
843         g_return_val_if_fail(item != NULL, NULL);
844
845         if(FOLDER_TYPE(item->folder) != F_MBOX) {
846                 folder_path = folder_get_path(item->folder);
847                 g_return_val_if_fail(folder_path != NULL, NULL);
848
849                 if (folder_path[0] == G_DIR_SEPARATOR) {
850                         if (item->path)
851                                 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
852                                                    item->path, NULL);
853                         else
854                                 path = g_strdup(folder_path);
855                 } else {
856                         if (item->path)
857                                 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
858                                                    folder_path, G_DIR_SEPARATOR_S,
859                                                    item->path, NULL);
860                         else
861                                 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
862                                                    folder_path, NULL);
863                 }
864
865                 g_free(folder_path);
866         } else {
867                 gchar *itempath;
868
869                 itempath = mbox_get_virtual_path(item);
870                 if (itempath == NULL)
871                         return NULL;
872                 path = g_strconcat(get_mbox_cache_dir(),
873                                           G_DIR_SEPARATOR_S, itempath, NULL);
874                 g_free(itempath);
875         }
876         return path;
877 }
878
879 void folder_item_set_default_flags(FolderItem *dest, MsgFlags *flags)
880 {
881         if (!(dest->stype == F_OUTBOX ||
882               dest->stype == F_QUEUE  ||
883               dest->stype == F_DRAFT  ||
884               dest->stype == F_TRASH)) {
885                 flags->perm_flags = MSG_NEW|MSG_UNREAD;
886         } else {
887                 flags->perm_flags = 0;
888         }
889         flags->tmp_flags = MSG_CACHED;
890         if (dest->folder->type == F_MH) {
891                 if (dest->stype == F_QUEUE) {
892                         MSG_SET_TMP_FLAGS(*flags, MSG_QUEUED);
893                 } else if (dest->stype == F_DRAFT) {
894                         MSG_SET_TMP_FLAGS(*flags, MSG_DRAFT);
895                 }
896         } else if (dest->folder->type == F_IMAP) {
897                 MSG_SET_TMP_FLAGS(*flags, MSG_IMAP);
898         } else if (dest->folder->type == F_NEWS) {
899                 MSG_SET_TMP_FLAGS(*flags, MSG_NEWS);
900         }
901 }
902
903 static gint folder_sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
904 {
905         MsgInfo *msginfo_a = (MsgInfo *) a;
906         MsgInfo *msginfo_b = (MsgInfo *) b;
907
908         return (msginfo_a->msgnum - msginfo_b->msgnum);
909 }
910
911 static gint folder_sort_folder_list(gconstpointer a, gconstpointer b)
912 {
913         guint gint_a = GPOINTER_TO_INT(a);
914         guint gint_b = GPOINTER_TO_INT(b);
915         
916         return (gint_a - gint_b);
917 }
918
919 gint folder_item_scan(FolderItem *item)
920 {
921         Folder *folder;
922         GSList *folder_list, *cache_list, *folder_list_cur, *cache_list_cur, *new_list = NULL;
923         guint newcnt = 0, unreadcnt = 0, totalcnt = 0;
924         guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
925
926         g_return_val_if_fail(item != NULL, -1);
927         if (item->path == NULL) return -1;
928
929         folder = item->folder;
930
931         g_return_val_if_fail(folder != NULL, -1);
932         g_return_val_if_fail(folder->get_num_list != NULL, -1);
933
934         debug_print("Scanning folder %s for cache changes.\n", item->path);
935
936         /* Get list of messages for folder and cache */
937         if (!folder->check_msgnum_validity || 
938            folder->check_msgnum_validity(folder, item)) {
939                 if (!item->cache)
940                         folder_item_read_cache(item);
941                 cache_list = msgcache_get_msg_list(item->cache);
942         } else {
943                 if (item->cache)
944                         msgcache_destroy(item->cache);
945                 item->cache = msgcache_new();
946                 cache_list = NULL;
947         }
948         folder_list = folder->get_num_list(item->folder, item);
949
950         /* Sort both lists */
951         cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
952         folder_list = g_slist_sort(folder_list, folder_sort_folder_list);
953
954         cache_list_cur = cache_list;
955         folder_list_cur = folder_list;
956
957         if (cache_list_cur != NULL) {
958                 GSList *cache_list_last;
959         
960                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
961                 cache_list_last = g_slist_last(cache_list);
962                 cache_max_num = ((MsgInfo *)cache_list_last->data)->msgnum;
963         } else {
964                 cache_cur_num = G_MAXINT;
965                 cache_max_num = 0;
966         }
967
968         if (folder_list_cur != NULL) {
969                 GSList *folder_list_last;
970         
971                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
972                 folder_list_last = g_slist_last(folder_list);
973                 folder_max_num = GPOINTER_TO_INT(folder_list_last->data);
974         } else {
975                 folder_cur_num = G_MAXINT;
976                 folder_max_num = 0;
977         }
978
979         while ((cache_cur_num != G_MAXINT) || (folder_cur_num != G_MAXINT)) {
980                 /*
981                  *  Message only exists in the folder
982                  *  Remember message for fetching
983                  */
984                 if (folder_cur_num < cache_cur_num) {
985                         gboolean add = FALSE;
986
987                         switch(folder->type) {
988                                 case F_NEWS:
989                                         if (folder_cur_num < cache_max_num)
990                                                 break;
991                                         
992                                         if (prefs_common.max_articles == 0) {
993                                                 add = TRUE;
994                                         }
995
996                                         if (folder_max_num <= prefs_common.max_articles) {
997                                                 add = TRUE;
998                                         } else if (folder_cur_num > (folder_max_num - prefs_common.max_articles)) {
999                                                 add = TRUE;
1000                                         }
1001                                         break;
1002                                 default:
1003                                         add = TRUE;
1004                                         break;
1005                         }
1006                         
1007                         if (add) {
1008                                 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(folder_cur_num));
1009                                 debug_print("Remembered message %d for fetching\n", folder_cur_num);
1010                         }
1011
1012                         /* Move to next folder number */
1013                         folder_list_cur = folder_list_cur->next;
1014
1015                         if (folder_list_cur != NULL)
1016                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1017                         else
1018                                 folder_cur_num = G_MAXINT;
1019
1020                         continue;
1021                 }
1022
1023                 /*
1024                  *  Message only exists in the cache
1025                  *  Remove the message from the cache
1026                  */
1027                 if (cache_cur_num < folder_cur_num) {
1028                         msgcache_remove_msg(item->cache, cache_cur_num);
1029                         debug_print("Removed message %d from cache.\n", cache_cur_num);
1030
1031                         /* Move to next cache number */
1032                         cache_list_cur = cache_list_cur->next;
1033
1034                         if (cache_list_cur != NULL)
1035                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1036                         else
1037                                 cache_cur_num = G_MAXINT;
1038
1039                         continue;
1040                 }
1041
1042                 /*
1043                  *  Message number exists in folder and cache!
1044                  *  Check if the message has been modified
1045                  */
1046                 if (cache_cur_num == folder_cur_num) {
1047                         MsgInfo *msginfo;
1048
1049                         msginfo = msgcache_get_msg(item->cache, folder_cur_num);
1050                         if (folder->is_msg_changed && folder->is_msg_changed(folder, item, msginfo)) {
1051                                 MsgInfo *newmsginfo;
1052
1053                                 msgcache_remove_msg(item->cache, msginfo->msgnum);
1054
1055                                 if (NULL != (newmsginfo = folder->fetch_msginfo(folder, item, folder_cur_num))) {
1056                                         msgcache_add_msg(item->cache, newmsginfo);
1057                                         if (MSG_IS_NEW(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
1058                                                 newcnt++;
1059                                         if (MSG_IS_UNREAD(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
1060                                                 unreadcnt++;
1061                                         procmsg_msginfo_free(newmsginfo);
1062                                 }                                       
1063
1064                                 debug_print("Updated msginfo for message %d.\n", folder_cur_num);
1065                         } else {
1066                                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1067                                         newcnt++;
1068                                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1069                                         unreadcnt++;
1070                         }
1071                         totalcnt++;
1072                         procmsg_msginfo_free(msginfo);
1073
1074                         /* Move to next folder and cache number */
1075                         cache_list_cur = cache_list_cur->next;
1076                         folder_list_cur = folder_list_cur->next;
1077
1078                         if (cache_list_cur != NULL)
1079                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1080                         else
1081                                 cache_cur_num = G_MAXINT;
1082
1083                         if (folder_list_cur != NULL)
1084                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1085                         else
1086                                 folder_cur_num = G_MAXINT;
1087
1088                         continue;
1089                 }
1090         }
1091
1092         for(cache_list_cur = cache_list; cache_list_cur != NULL; cache_list_cur = g_slist_next(cache_list_cur)) {
1093                 procmsg_msginfo_free((MsgInfo *) cache_list_cur->data);
1094         }
1095
1096         g_slist_free(cache_list);
1097         g_slist_free(folder_list);
1098
1099         if (folder->fetch_msginfos) {
1100                 GSList *elem;
1101                 GSList *newmsg_list;
1102                 MsgInfo *msginfo;
1103                 
1104                 if (new_list) {
1105                         newmsg_list = folder->fetch_msginfos(folder, item, new_list);
1106                         for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
1107                                 msginfo = (MsgInfo *) elem->data;
1108                                 msgcache_add_msg(item->cache, msginfo);
1109                                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1110                                         newcnt++;
1111                                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1112                                         unreadcnt++;
1113                                 totalcnt++;
1114                                 procmsg_msginfo_free(msginfo);
1115                         }
1116                         g_slist_free(newmsg_list);
1117                 }
1118         } else if (folder->fetch_msginfo) {
1119                 GSList *elem;
1120         
1121                 for (elem = new_list; elem != NULL; elem = g_slist_next(elem)) {
1122                         MsgInfo *msginfo;
1123                         guint num;
1124
1125                         num = GPOINTER_TO_INT(elem->data);
1126                         msginfo = folder->fetch_msginfo(folder, item, num);
1127                         if (msginfo != NULL) {
1128                                 msgcache_add_msg(item->cache, msginfo);
1129                                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1130                                     newcnt++;
1131                                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1132                                     unreadcnt++;
1133                                 totalcnt++;
1134                                 procmsg_msginfo_free(msginfo);
1135                                 debug_print("Added newly found message %d to cache.\n", num);
1136                         }
1137                 }
1138         }
1139
1140         item->new = newcnt;
1141         item->unread = unreadcnt;
1142         item->total = totalcnt;
1143         
1144         g_slist_free(new_list);
1145
1146         folderview_update_item(item, FALSE);
1147
1148         return 0;
1149 }
1150
1151 static void folder_item_scan_foreach_func(gpointer key, gpointer val,
1152                                           gpointer data)
1153 {
1154         folder_item_scan(FOLDER_ITEM(key));
1155 }
1156
1157 void folder_item_scan_foreach(GHashTable *table)
1158 {
1159         g_hash_table_foreach(table, folder_item_scan_foreach_func, NULL);
1160 }
1161
1162 void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
1163 {
1164         gint *memusage = (gint *)data;
1165
1166         if (item->cache == NULL)
1167                 return;
1168         
1169         *memusage += msgcache_get_memory_usage(item->cache);
1170 }
1171
1172 gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
1173 {
1174         FolderItem *fa = (FolderItem *)a;
1175         FolderItem *fb = (FolderItem *)b;
1176         
1177         return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
1178 }
1179
1180 void folder_find_expired_caches(FolderItem *item, gpointer data)
1181 {
1182         GSList **folder_item_list = (GSList **)data;
1183         gint difftime, expiretime;
1184         
1185         if (item->cache == NULL)
1186                 return;
1187
1188         if (item->opened > 0)
1189                 return;
1190
1191         difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
1192         expiretime = prefs_common.cache_min_keep_time * 60;
1193         debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
1194         if (difftime > expiretime) {
1195                 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
1196         }
1197 }
1198
1199 void folder_item_free_cache(FolderItem *item)
1200 {
1201         g_return_if_fail(item != NULL);
1202         
1203         if (item->cache == NULL)
1204                 return;
1205         
1206         if (item->opened > 0)
1207                 return;
1208
1209         folder_item_write_cache(item);
1210         msgcache_destroy(item->cache);
1211         item->cache = NULL;
1212 }
1213
1214 void folder_clean_cache_memory()
1215 {
1216         gint memusage = 0;
1217
1218         folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);       
1219         debug_print("Total cache memory usage: %d\n", memusage);
1220         
1221         if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
1222                 GSList *folder_item_list = NULL, *listitem;
1223                 
1224                 debug_print("Trying to free cache memory\n");
1225
1226                 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);      
1227                 listitem = folder_item_list;
1228                 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
1229                         FolderItem *item = (FolderItem *)(listitem->data);
1230
1231                         debug_print("Freeing cache memory for %s\n", item->path);
1232                         memusage -= msgcache_get_memory_usage(item->cache);
1233                         folder_item_free_cache(item);
1234                         listitem = listitem->next;
1235                 }
1236                 g_slist_free(folder_item_list);
1237         }
1238 }
1239
1240 void folder_item_read_cache(FolderItem *item)
1241 {
1242         gchar *cache_file, *mark_file;
1243         
1244         g_return_if_fail(item != NULL);
1245
1246         cache_file = folder_item_get_cache_file(item);
1247         mark_file = folder_item_get_mark_file(item);
1248         item->cache = msgcache_read_cache(item, cache_file);
1249         if (!item->cache) {
1250                 item->cache = msgcache_new();
1251                 folder_item_scan(item);
1252         }
1253         msgcache_read_mark(item->cache, mark_file);
1254         g_free(cache_file);
1255         g_free(mark_file);
1256
1257         folder_clean_cache_memory();
1258 }
1259
1260 void folder_item_write_cache(FolderItem *item)
1261 {
1262         gchar *cache_file, *mark_file;
1263         PrefsFolderItem *prefs;
1264         gint filemode = 0;
1265         gchar *id;
1266         
1267         if (!item || !item->path || !item->cache)
1268                 return;
1269
1270         id = folder_item_get_identifier(item);
1271         debug_print("Save cache for folder %s\n", id);
1272         g_free(id);
1273
1274         cache_file = folder_item_get_cache_file(item);
1275         mark_file = folder_item_get_mark_file(item);
1276         if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
1277                 prefs = item->prefs;
1278                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
1279                         /* for cache file */
1280                         filemode = prefs->folder_chmod;
1281                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
1282                         if (filemode & S_IROTH) filemode |= S_IWOTH;
1283                         chmod(cache_file, filemode);
1284                 }
1285         }
1286
1287         g_free(cache_file);
1288         g_free(mark_file);
1289 }
1290
1291 MsgInfo *folder_item_fetch_msginfo(FolderItem *item, gint num)
1292 {
1293         Folder *folder;
1294         MsgInfo *msginfo;
1295         
1296         g_return_val_if_fail(item != NULL, NULL);
1297         
1298         folder = item->folder;
1299         if (!item->cache)
1300                 folder_item_read_cache(item);
1301         
1302         if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
1303                 return msginfo;
1304         
1305         g_return_val_if_fail(folder->fetch_msginfo, NULL);
1306         if ((msginfo = folder->fetch_msginfo(folder, item, num)) != NULL) {
1307                 msgcache_add_msg(item->cache, msginfo);
1308                 return msginfo;
1309         }
1310         
1311         return NULL;
1312 }
1313
1314 MsgInfo *folder_item_fetch_msginfo_by_id(FolderItem *item, const gchar *msgid)
1315 {
1316         Folder *folder;
1317         MsgInfo *msginfo;
1318         
1319         g_return_val_if_fail(item != NULL, NULL);
1320         
1321         folder = item->folder;
1322         if (!item->cache)
1323                 folder_item_read_cache(item);
1324         
1325         if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
1326                 return msginfo;
1327
1328         return NULL;
1329 }
1330
1331 GSList *folder_item_get_msg_list(FolderItem *item)
1332 {
1333         g_return_val_if_fail(item != NULL, NULL);
1334         
1335         if (item->cache == 0)
1336                 folder_item_read_cache(item);
1337
1338         g_return_val_if_fail(item->cache != NULL, NULL);
1339         
1340         return msgcache_get_msg_list(item->cache);
1341 }
1342
1343 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
1344 {
1345         Folder *folder;
1346
1347         g_return_val_if_fail(item != NULL, NULL);
1348
1349         folder = item->folder;
1350
1351         g_return_val_if_fail(folder->fetch_msg != NULL, NULL);
1352
1353         return folder->fetch_msg(folder, item, num);
1354 }
1355
1356 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
1357                          gboolean remove_source)
1358 {
1359         Folder *folder;
1360         gint num;
1361         MsgInfo *msginfo;
1362
1363         g_return_val_if_fail(dest != NULL, -1);
1364         g_return_val_if_fail(file != NULL, -1);
1365
1366         folder = dest->folder;
1367
1368         g_return_val_if_fail(folder->add_msg != NULL, -1);
1369
1370         if (!dest->cache)
1371                 folder_item_read_cache(dest);
1372
1373         num = folder->add_msg(folder, dest, file, remove_source);
1374
1375         if (num > 0) {
1376                 msginfo = folder->fetch_msginfo(folder, dest, num);
1377
1378                 if (msginfo != NULL) {
1379                         if (MSG_IS_NEW(msginfo->flags))
1380                                 dest->new++;
1381                         if (MSG_IS_UNREAD(msginfo->flags))
1382                                 dest->unread++;
1383                         dest->total++;
1384
1385                         msgcache_add_msg(dest->cache, msginfo);
1386
1387                         procmsg_msginfo_free(msginfo);
1388                 }
1389
1390                 dest->last_num = num;
1391         }
1392
1393         return num;
1394 }
1395
1396 /*
1397 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1398 {
1399         Folder *folder;
1400         gint num;
1401
1402         g_return_val_if_fail(dest != NULL, -1);
1403         g_return_val_if_fail(msginfo != NULL, -1);
1404
1405         folder = dest->folder;
1406         if (dest->last_num < 0) folder->scan(folder, dest);
1407
1408         num = folder->move_msg(folder, dest, msginfo);
1409         if (num > 0) dest->last_num = num;
1410
1411         return num;
1412 }
1413 */
1414
1415 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1416 {
1417         Folder *folder;
1418         gint num;
1419         Folder *src_folder;
1420
1421         g_return_val_if_fail(dest != NULL, -1);
1422         g_return_val_if_fail(msginfo != NULL, -1);
1423
1424         folder = dest->folder;
1425
1426         g_return_val_if_fail(folder->remove_msg != NULL, -1);
1427         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1428
1429         if (!dest->cache) folder_item_read_cache(dest);
1430
1431         src_folder = msginfo->folder->folder;
1432
1433         num = folder->copy_msg(folder, dest, msginfo);
1434         
1435         if (num != -1) {
1436                 MsgInfo *newmsginfo;
1437     
1438                 /* Add new msginfo to dest folder */
1439                 if (NULL != (newmsginfo = folder->fetch_msginfo(folder, dest, num))) {
1440                         newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1441                         
1442                         if (dest->stype == F_OUTBOX || dest->stype == F_QUEUE  ||
1443                             dest->stype == F_DRAFT  || dest->stype == F_TRASH)
1444                                 MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1445                                                      MSG_NEW|MSG_UNREAD|MSG_DELETED);
1446                         msgcache_add_msg(dest->cache, newmsginfo);
1447
1448                         if (MSG_IS_NEW(newmsginfo->flags))
1449                                 dest->new++;
1450                         if (MSG_IS_UNREAD(newmsginfo->flags))
1451                                 dest->unread++;
1452                         dest->total++;
1453                         dest->need_update = TRUE;
1454
1455                         procmsg_msginfo_free(newmsginfo);
1456                 }
1457
1458                 /* remove source message from it's folder */
1459                 if (src_folder->remove_msg) {
1460                         src_folder->remove_msg(src_folder, msginfo->folder,
1461                                                msginfo->msgnum);
1462                         msgcache_remove_msg(msginfo->folder->cache, msginfo->msgnum);
1463
1464                         if (MSG_IS_NEW(msginfo->flags))
1465                                 msginfo->folder->new--;
1466                         if (MSG_IS_UNREAD(msginfo->flags))
1467                                 msginfo->folder->unread--;
1468                         msginfo->folder->total--;
1469                         msginfo->folder->need_update = TRUE;
1470                 }
1471         }
1472         
1473         if (folder->finished_copy)
1474                 folder->finished_copy(folder, dest);
1475
1476         return num;
1477 }
1478
1479 /*
1480 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1481 {
1482         Folder *folder;
1483         gint num;
1484
1485         g_return_val_if_fail(dest != NULL, -1);
1486         g_return_val_if_fail(msglist != NULL, -1);
1487
1488         folder = dest->folder;
1489         if (dest->last_num < 0) folder->scan(folder, dest);
1490
1491         num = folder->move_msgs_with_dest(folder, dest, msglist);
1492         if (num > 0) dest->last_num = num;
1493         else dest->op_count = 0;
1494
1495         return num;
1496 }
1497 */
1498
1499 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1500 {
1501         Folder *folder;
1502         FolderItem *item;
1503         GSList *newmsgnums = NULL;
1504         GSList *l, *l2;
1505         gint num;
1506
1507         g_return_val_if_fail(dest != NULL, -1);
1508         g_return_val_if_fail(msglist != NULL, -1);
1509
1510         folder = dest->folder;
1511
1512         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1513         g_return_val_if_fail(folder->remove_msg != NULL, -1);
1514
1515         /* 
1516          * Copy messages to destination folder and 
1517          * store new message numbers in newmsgnums
1518          */
1519         item = NULL;
1520         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
1521                 MsgInfo * msginfo = (MsgInfo *) l->data;
1522
1523                 if (!item && msginfo->folder != NULL)
1524                         item = msginfo->folder;
1525
1526                 num = folder->copy_msg(folder, dest, msginfo);
1527                 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1528         }
1529
1530         /* Read cache for dest folder */
1531         if (!dest->cache) folder_item_read_cache(dest);
1532         
1533         /* 
1534          * Fetch new MsgInfos for new messages in dest folder,
1535          * add them to the msgcache and update folder message counts
1536          */
1537         l2 = newmsgnums;
1538         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1539                 MsgInfo *msginfo = (MsgInfo *) l->data;
1540
1541                 num = GPOINTER_TO_INT(l2->data);
1542
1543                 if (num != -1) {
1544                         MsgInfo *newmsginfo;
1545
1546                         newmsginfo = folder->fetch_msginfo(folder, dest, num);
1547                         if (newmsginfo) {
1548                                 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1549                                 if (dest->stype == F_OUTBOX ||
1550                                     dest->stype == F_QUEUE  ||
1551                                     dest->stype == F_DRAFT  ||
1552                                     dest->stype == F_TRASH)
1553                                         MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1554                                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
1555                                 msgcache_add_msg(dest->cache, newmsginfo);
1556
1557                                 if (MSG_IS_NEW(newmsginfo->flags))
1558                                         dest->new++;
1559                                 if (MSG_IS_UNREAD(newmsginfo->flags))
1560                                         dest->unread++;
1561                                 dest->total++;
1562                                 dest->need_update = TRUE;
1563
1564                                 procmsg_msginfo_free(newmsginfo);
1565                         }
1566                 }
1567                 l2 = g_slist_next(l2);
1568         }
1569
1570         /*
1571          * Remove source messages from their folders if
1572          * copying was successfull and update folder
1573          * message counts
1574          */
1575         l2 = newmsgnums;
1576         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1577                 MsgInfo *msginfo = (MsgInfo *) l->data;
1578
1579                 num = GPOINTER_TO_INT(l2->data);
1580                 
1581                 if (num != -1) {
1582                         item->folder->remove_msg(item->folder,
1583                                                  msginfo->folder,
1584                                                  msginfo->msgnum);
1585                         if (!item->cache)
1586                                 folder_item_read_cache(item);
1587                         msgcache_remove_msg(item->cache, msginfo->msgnum);
1588
1589                         if (MSG_IS_NEW(msginfo->flags))
1590                                 msginfo->folder->new--;
1591                         if (MSG_IS_UNREAD(msginfo->flags))
1592                                 msginfo->folder->unread--;
1593                         msginfo->folder->total--;                       
1594                         msginfo->folder->need_update = TRUE;
1595                 }
1596
1597                 l2 = g_slist_next(l2);
1598         }
1599
1600
1601         if (folder->finished_copy)
1602                 folder->finished_copy(folder, dest);
1603
1604         g_slist_free(newmsgnums);
1605         return dest->last_num;
1606 }
1607
1608 /*
1609 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1610 {
1611         Folder *folder;
1612         gint num;
1613
1614         g_return_val_if_fail(dest != NULL, -1);
1615         g_return_val_if_fail(msginfo != NULL, -1);
1616
1617         folder = dest->folder;
1618         if (dest->last_num < 0) folder->scan(folder, dest);
1619
1620         num = folder->copy_msg(folder, dest, msginfo);
1621         if (num > 0) dest->last_num = num;
1622
1623         return num;
1624 }
1625 */
1626
1627 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1628 {
1629         Folder *folder;
1630         gint num;
1631
1632         g_return_val_if_fail(dest != NULL, -1);
1633         g_return_val_if_fail(msginfo != NULL, -1);
1634
1635         folder = dest->folder;
1636
1637         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1638
1639         if (!dest->cache) folder_item_read_cache(dest);
1640         
1641         num = folder->copy_msg(folder, dest, msginfo);
1642         if (num != -1) {
1643                 MsgInfo *newmsginfo;
1644
1645                 if (NULL != (newmsginfo = folder->fetch_msginfo(folder, dest, num))) {
1646                         newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1647                         if (dest->stype == F_OUTBOX ||
1648                             dest->stype == F_QUEUE  ||
1649                             dest->stype == F_DRAFT  ||
1650                             dest->stype == F_TRASH)
1651                                 MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1652                                                      MSG_NEW|MSG_UNREAD|MSG_DELETED);
1653                         msgcache_add_msg(dest->cache, newmsginfo);
1654
1655                         if (MSG_IS_NEW(newmsginfo->flags))
1656                                 dest->new++;
1657                         if (MSG_IS_UNREAD(newmsginfo->flags))
1658                                 dest->unread++;
1659                         dest->total++;
1660                         dest->need_update = TRUE;
1661
1662                         procmsg_msginfo_free(newmsginfo);
1663                 }                       
1664         }
1665
1666         if (folder->finished_copy)
1667                 folder->finished_copy(folder, dest);
1668
1669         return num;
1670 }
1671
1672 /*
1673 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1674 {
1675         Folder *folder;
1676         gint num;
1677
1678         g_return_val_if_fail(dest != NULL, -1);
1679         g_return_val_if_fail(msglist != NULL, -1);
1680
1681         folder = dest->folder;
1682         if (dest->last_num < 0) folder->scan(folder, dest);
1683
1684         num = folder->copy_msgs_with_dest(folder, dest, msglist);
1685         if (num > 0) dest->last_num = num;
1686         else dest->op_count = 0;
1687
1688         return num;
1689 }
1690 */
1691
1692 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1693 {
1694         Folder *folder;
1695         gint num;
1696         GSList *newmsgnums = NULL;
1697         GSList *l, *l2;
1698
1699         g_return_val_if_fail(dest != NULL, -1);
1700         g_return_val_if_fail(msglist != NULL, -1);
1701
1702         folder = dest->folder;
1703  
1704         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1705
1706         /* 
1707          * Copy messages to destination folder and 
1708          * store new message numbers in newmsgnums
1709          */
1710         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
1711                 MsgInfo * msginfo = (MsgInfo *) l->data;
1712
1713                 num = folder->copy_msg(folder, dest, msginfo);
1714                 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1715         }
1716
1717         /* Read cache for dest folder */
1718         if (!dest->cache) folder_item_read_cache(dest);
1719
1720         /* 
1721          * Fetch new MsgInfos for new messages in dest folder,
1722          * add them to the msgcache and update folder message counts
1723          */
1724         l2 = newmsgnums;
1725         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1726                 MsgInfo *msginfo = (MsgInfo *) l->data;
1727
1728                 num = GPOINTER_TO_INT(l2->data);
1729
1730                 if (num != -1) {
1731                         MsgInfo *newmsginfo;
1732
1733                         newmsginfo = folder->fetch_msginfo(folder, dest, num);
1734                         if (newmsginfo) {
1735                                 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1736                                 if (dest->stype == F_OUTBOX ||
1737                                     dest->stype == F_QUEUE  ||
1738                                     dest->stype == F_DRAFT  ||
1739                                     dest->stype == F_TRASH)
1740                                         MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1741                                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
1742                                 msgcache_add_msg(dest->cache, newmsginfo);
1743
1744                                 if (MSG_IS_NEW(newmsginfo->flags))
1745                                         dest->new++;
1746                                 if (MSG_IS_UNREAD(newmsginfo->flags))
1747                                         dest->unread++;
1748                                 dest->total++;
1749                                 dest->need_update = TRUE;
1750
1751                                 procmsg_msginfo_free(newmsginfo);
1752                         }
1753                 }
1754                 l2 = g_slist_next(l2);
1755         }
1756         
1757         if (folder->finished_copy)
1758                 folder->finished_copy(folder, dest);
1759
1760         g_slist_free(newmsgnums);
1761         return dest->last_num;
1762 }
1763
1764 gint folder_item_remove_msg(FolderItem *item, gint num)
1765 {
1766         Folder *folder;
1767         gint ret;
1768         MsgInfo *msginfo;
1769
1770         g_return_val_if_fail(item != NULL, -1);
1771
1772         folder = item->folder;
1773         if (!item->cache) folder_item_read_cache(item);
1774
1775         ret = folder->remove_msg(folder, item, num);
1776
1777         msginfo = msgcache_get_msg(item->cache, num);
1778         if (msginfo != NULL) {
1779                 if (MSG_IS_NEW(msginfo->flags))
1780                         item->new--;
1781                 if (MSG_IS_UNREAD(msginfo->flags))
1782                         item->unread--;
1783                 procmsg_msginfo_free(msginfo);
1784                 msgcache_remove_msg(item->cache, num);
1785         }
1786         item->total--;
1787         item->need_update = TRUE;
1788
1789         return ret;
1790 }
1791
1792 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
1793 {
1794         gint ret = 0;
1795
1796         g_return_val_if_fail(item != NULL, -1);
1797
1798         if (!item->cache) folder_item_read_cache(item);
1799
1800         while (msglist != NULL) {
1801                 MsgInfo *msginfo = (MsgInfo *)msglist->data;
1802
1803                 ret = folder_item_remove_msg(item, msginfo->msgnum);
1804                 if (ret != 0) break;
1805                 msgcache_remove_msg(item->cache, msginfo->msgnum);
1806                 msglist = msglist->next;
1807         }
1808
1809         return ret;
1810 }
1811
1812 gint folder_item_remove_all_msg(FolderItem *item)
1813 {
1814         Folder *folder;
1815         gint result;
1816
1817         g_return_val_if_fail(item != NULL, -1);
1818
1819         folder = item->folder;
1820
1821         g_return_val_if_fail(folder->remove_all_msg != NULL, -1);
1822
1823         result = folder->remove_all_msg(folder, item);
1824
1825         if (result == 0) {
1826                 if (folder->finished_remove)
1827                         folder->finished_remove(folder, item);
1828
1829                 folder_item_free_cache(item);
1830                 item->cache = msgcache_new();
1831
1832                 item->new = 0;
1833                 item->unread = 0;
1834                 item->total = 0;
1835                 item->need_update = TRUE;
1836         }
1837
1838         return result;
1839 }
1840
1841 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
1842 {
1843         Folder *folder;
1844
1845         g_return_val_if_fail(item != NULL, FALSE);
1846
1847         folder = item->folder;
1848
1849         g_return_val_if_fail(folder->is_msg_changed != NULL, -1);
1850
1851         return folder->is_msg_changed(folder, item, msginfo);
1852 }
1853
1854 gchar *folder_item_get_cache_file(FolderItem *item)
1855 {
1856         gchar *path;
1857         gchar *file;
1858
1859         g_return_val_if_fail(item != NULL, NULL);
1860         g_return_val_if_fail(item->path != NULL, NULL);
1861
1862         path = folder_item_get_path(item);
1863         g_return_val_if_fail(path != NULL, NULL);
1864         if (!is_dir_exist(path))
1865                 make_dir_hier(path);
1866         file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
1867         g_free(path);
1868
1869         return file;
1870 }
1871
1872 gchar *folder_item_get_mark_file(FolderItem *item)
1873 {
1874         gchar *path;
1875         gchar *file;
1876
1877         g_return_val_if_fail(item != NULL, NULL);
1878         g_return_val_if_fail(item->path != NULL, NULL);
1879
1880         path = folder_item_get_path(item);
1881         g_return_val_if_fail(path != NULL, NULL);
1882         if (!is_dir_exist(path))
1883                 make_dir_hier(path);
1884         file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
1885         g_free(path);
1886
1887         return file;
1888 }
1889
1890 static gboolean folder_build_tree(GNode *node, gpointer data)
1891 {
1892         Folder *folder = FOLDER(data);
1893         FolderItem *item;
1894         XMLNode *xmlnode;
1895         GList *list;
1896         SpecialFolderItemType stype = F_NORMAL;
1897         const gchar *name = NULL;
1898         const gchar *path = NULL;
1899         PrefsAccount *account = NULL;
1900         gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE, 
1901                  threaded = TRUE, apply_sub = FALSE;
1902         gboolean ret_rcpt = FALSE, hidereadmsgs = FALSE; /* CLAWS */
1903         FolderSortKey sort_key = SORT_BY_NONE;
1904         FolderSortType sort_type = SORT_ASCENDING;
1905         gint new = 0, unread = 0, total = 0;
1906         time_t mtime = 0;
1907
1908         g_return_val_if_fail(node->data != NULL, FALSE);
1909         if (!node->parent) return FALSE;
1910
1911         xmlnode = node->data;
1912         if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
1913                 g_warning("tag name != \"folderitem\"\n");
1914                 return FALSE;
1915         }
1916
1917         list = xmlnode->tag->attr;
1918         for (; list != NULL; list = list->next) {
1919                 XMLAttr *attr = list->data;
1920
1921                 if (!attr || !attr->name || !attr->value) continue;
1922                 if (!strcmp(attr->name, "type")) {
1923                         if (!strcasecmp(attr->value, "normal"))
1924                                 stype = F_NORMAL;
1925                         else if (!strcasecmp(attr->value, "inbox"))
1926                                 stype = F_INBOX;
1927                         else if (!strcasecmp(attr->value, "outbox"))
1928                                 stype = F_OUTBOX;
1929                         else if (!strcasecmp(attr->value, "draft"))
1930                                 stype = F_DRAFT;
1931                         else if (!strcasecmp(attr->value, "queue"))
1932                                 stype = F_QUEUE;
1933                         else if (!strcasecmp(attr->value, "trash"))
1934                                 stype = F_TRASH;
1935                 } else if (!strcmp(attr->name, "name"))
1936                         name = attr->value;
1937                 else if (!strcmp(attr->name, "path"))
1938                         path = attr->value;
1939                 else if (!strcmp(attr->name, "mtime"))
1940                         mtime = strtoul(attr->value, NULL, 10);
1941                 else if (!strcmp(attr->name, "new"))
1942                         new = atoi(attr->value);
1943                 else if (!strcmp(attr->name, "unread"))
1944                         unread = atoi(attr->value);
1945                 else if (!strcmp(attr->name, "total"))
1946                         total = atoi(attr->value);
1947                 else if (!strcmp(attr->name, "no_sub"))
1948                         no_sub = *attr->value == '1' ? TRUE : FALSE;
1949                 else if (!strcmp(attr->name, "no_select"))
1950                         no_select = *attr->value == '1' ? TRUE : FALSE;
1951                 else if (!strcmp(attr->name, "collapsed"))
1952                         collapsed = *attr->value == '1' ? TRUE : FALSE;
1953                 else if (!strcmp(attr->name, "threaded"))
1954                         threaded =  *attr->value == '1' ? TRUE : FALSE;
1955                 else if (!strcmp(attr->name, "hidereadmsgs"))
1956                         hidereadmsgs =  *attr->value == '1' ? TRUE : FALSE;
1957                 else if (!strcmp(attr->name, "reqretrcpt"))
1958                         ret_rcpt =  *attr->value == '1' ? TRUE : FALSE;
1959                 else if (!strcmp(attr->name, "sort_key")) {
1960                         if (!strcmp(attr->value, "none"))
1961                                 sort_key = SORT_BY_NONE;
1962                         else if (!strcmp(attr->value, "number"))
1963                                 sort_key = SORT_BY_NUMBER;
1964                         else if (!strcmp(attr->value, "size"))
1965                                 sort_key = SORT_BY_SIZE;
1966                         else if (!strcmp(attr->value, "date"))
1967                                 sort_key = SORT_BY_DATE;
1968                         else if (!strcmp(attr->value, "from"))
1969                                 sort_key = SORT_BY_FROM;
1970                         else if (!strcmp(attr->value, "subject"))
1971                                 sort_key = SORT_BY_SUBJECT;
1972                         else if (!strcmp(attr->value, "score"))
1973                                 sort_key = SORT_BY_SCORE;
1974                         else if (!strcmp(attr->value, "label"))
1975                                 sort_key = SORT_BY_LABEL;
1976                         else if (!strcmp(attr->value, "mark"))
1977                                 sort_key = SORT_BY_MARK;
1978                         else if (!strcmp(attr->value, "unread"))
1979                                 sort_key = SORT_BY_UNREAD;
1980                         else if (!strcmp(attr->value, "mime"))
1981                                 sort_key = SORT_BY_MIME;
1982                         else if (!strcmp(attr->value, "locked"))
1983                                 sort_key = SORT_BY_LOCKED;
1984                 } else if (!strcmp(attr->name, "sort_type")) {
1985                         if (!strcmp(attr->value, "ascending"))
1986                                 sort_type = SORT_ASCENDING;
1987                         else
1988                                 sort_type = SORT_DESCENDING;
1989                 } else if (!strcmp(attr->name, "account_id")) {
1990                         account = account_find_from_id(atoi(attr->value));
1991                         if (!account) g_warning("account_id: %s not found\n",
1992                                                 attr->value);
1993                 } else if (!strcmp(attr->name, "apply_sub"))
1994                         apply_sub = *attr->value == '1' ? TRUE : FALSE;
1995         }
1996
1997         item = folder_item_new(folder, name, path);
1998         item->stype = stype;
1999         item->mtime = mtime;
2000         item->new = new;
2001         item->unread = unread;
2002         item->total = total;
2003         item->no_sub = no_sub;
2004         item->no_select = no_select;
2005         item->collapsed = collapsed;
2006         item->threaded  = threaded;
2007         item->hide_read_msgs  = hidereadmsgs;
2008         item->ret_rcpt  = ret_rcpt;
2009         item->sort_key  = sort_key;
2010         item->sort_type = sort_type;
2011         item->parent = FOLDER_ITEM(node->parent->data);
2012         item->folder = folder;
2013         switch (stype) {
2014         case F_INBOX:  folder->inbox  = item; break;
2015         case F_OUTBOX: folder->outbox = item; break;
2016         case F_DRAFT:  folder->draft  = item; break;
2017         case F_QUEUE:  folder->queue  = item; break;
2018         case F_TRASH:  folder->trash  = item; break;
2019         default:       break;
2020         }
2021         item->account = account;
2022         item->apply_sub = apply_sub;
2023         prefs_folder_item_read_config(item);
2024
2025         node->data = item;
2026         xml_free_node(xmlnode);
2027
2028         return FALSE;
2029 }
2030
2031 static gboolean folder_read_folder_func(GNode *node, gpointer data)
2032 {
2033         Folder *folder;
2034         XMLNode *xmlnode;
2035         GList *list;
2036         FolderType type = F_UNKNOWN;
2037         const gchar *name = NULL;
2038         const gchar *path = NULL;
2039         PrefsAccount *account = NULL;
2040         gboolean collapsed = FALSE, threaded = TRUE, apply_sub = FALSE;
2041         gboolean ret_rcpt = FALSE; /* CLAWS */
2042
2043         if (g_node_depth(node) != 2) return FALSE;
2044         g_return_val_if_fail(node->data != NULL, FALSE);
2045
2046         xmlnode = node->data;
2047         if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
2048                 g_warning("tag name != \"folder\"\n");
2049                 return TRUE;
2050         }
2051         g_node_unlink(node);
2052         list = xmlnode->tag->attr;
2053         for (; list != NULL; list = list->next) {
2054                 XMLAttr *attr = list->data;
2055
2056                 if (!attr || !attr->name || !attr->value) continue;
2057                 if (!strcmp(attr->name, "type")) {
2058                         if (!strcasecmp(attr->value, "mh"))
2059                                 type = F_MH;
2060                         else if (!strcasecmp(attr->value, "mbox"))
2061                                 type = F_MBOX;
2062                         else if (!strcasecmp(attr->value, "maildir"))
2063                                 type = F_MAILDIR;
2064                         else if (!strcasecmp(attr->value, "imap"))
2065                                 type = F_IMAP;
2066                         else if (!strcasecmp(attr->value, "news"))
2067                                 type = F_NEWS;
2068                 } else if (!strcmp(attr->name, "name"))
2069                         name = attr->value;
2070                 else if (!strcmp(attr->name, "path"))
2071                         path = attr->value;
2072                 else if (!strcmp(attr->name, "collapsed"))
2073                         collapsed = *attr->value == '1' ? TRUE : FALSE;
2074                 else if (!strcmp(attr->name, "threaded"))
2075                         threaded = *attr->value == '1' ? TRUE : FALSE;
2076                 else if (!strcmp(attr->name, "account_id")) {
2077                         account = account_find_from_id(atoi(attr->value));
2078                         if (!account) g_warning("account_id: %s not found\n",
2079                                                 attr->value);
2080                 } else if (!strcmp(attr->name, "apply_sub"))
2081                         apply_sub = *attr->value == '1' ? TRUE : FALSE;
2082                 else if (!strcmp(attr->name, "reqretrcpt"))
2083                         ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
2084         }
2085
2086         folder = folder_new(type, name, path);
2087         g_return_val_if_fail(folder != NULL, FALSE);
2088         folder->account = account;
2089         if (account && (type == F_IMAP || type == F_NEWS))
2090                 account->folder = REMOTE_FOLDER(folder);
2091         node->data = folder->node->data;
2092         g_node_destroy(folder->node);
2093         folder->node = node;
2094         folder_add(folder);
2095         FOLDER_ITEM(node->data)->collapsed = collapsed;
2096         FOLDER_ITEM(node->data)->threaded  = threaded;
2097         FOLDER_ITEM(node->data)->account   = account;
2098         FOLDER_ITEM(node->data)->apply_sub = apply_sub;
2099         FOLDER_ITEM(node->data)->ret_rcpt  = ret_rcpt;
2100
2101         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2102                         folder_build_tree, folder);
2103
2104         return FALSE;
2105 }
2106
2107 static gchar *folder_get_list_path(void)
2108 {
2109         static gchar *filename = NULL;
2110
2111         if (!filename)
2112                 filename =  g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2113                                         FOLDER_LIST, NULL);
2114
2115         return filename;
2116 }
2117
2118 static void folder_write_list_recursive(GNode *node, gpointer data)
2119 {
2120         FILE *fp = (FILE *)data;
2121         FolderItem *item;
2122         gint i, depth;
2123         static gchar *folder_type_str[] = {"mh", "mbox", "maildir", "imap",
2124                                            "news", "unknown"};
2125         static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
2126                                                  "draft", "queue", "trash"};
2127         static gchar *sort_key_str[] = {"none", "number", "size", "date",
2128                                         "from", "subject", "score", "label",
2129                                         "mark", "unread", "mime", "locked" };
2130         g_return_if_fail(node != NULL);
2131         g_return_if_fail(fp != NULL);
2132
2133         item = FOLDER_ITEM(node->data);
2134         g_return_if_fail(item != NULL);
2135
2136         depth = g_node_depth(node);
2137         for (i = 0; i < depth; i++)
2138                 fputs("    ", fp);
2139         if (depth == 1) {
2140                 Folder *folder = item->folder;
2141
2142                 fprintf(fp, "<folder type=\"%s\"", folder_type_str[folder->type]);
2143                 if (folder->name) {
2144                         fputs(" name=\"", fp);
2145                         xml_file_put_escape_str(fp, folder->name);
2146                         fputs("\"", fp);
2147                 }
2148                 if ((folder->type == F_MH) || (folder->type == F_MBOX)) {
2149                         fputs(" path=\"", fp);
2150                         xml_file_put_escape_str
2151                                 (fp, LOCAL_FOLDER(folder)->rootpath);
2152                         fputs("\"", fp);
2153                 }
2154                 if (item->collapsed && node->children)
2155                         fputs(" collapsed=\"1\"", fp);
2156                 if (folder->account)
2157                         fprintf(fp, " account_id=\"%d\"",
2158                                 folder->account->account_id);
2159                 if (item->apply_sub)
2160                         fputs(" apply_sub=\"1\"", fp);
2161                 if (item->ret_rcpt) 
2162                         fputs(" reqretrcpt=\"1\"", fp);
2163         } else {
2164                 fprintf(fp, "<folderitem type=\"%s\"",
2165                         folder_item_stype_str[item->stype]);
2166                 if (item->name) {
2167                         fputs(" name=\"", fp);
2168                         xml_file_put_escape_str(fp, item->name);
2169                         fputs("\"", fp);
2170                 }
2171                 if (item->path) {
2172                         fputs(" path=\"", fp);
2173                         xml_file_put_escape_str(fp, item->path);
2174                         fputs("\"", fp);
2175                 }
2176                 
2177                 if (item->no_sub)
2178                         fputs(" no_sub=\"1\"", fp);
2179                 if (item->no_select)
2180                         fputs(" no_select=\"1\"", fp);
2181                 if (item->collapsed && node->children)
2182                         fputs(" collapsed=\"1\"", fp);
2183                 if (item->threaded)
2184                         fputs(" threaded=\"1\"", fp);
2185                 else
2186                         fputs(" threaded=\"0\"", fp);
2187                 if (item->hide_read_msgs)
2188                         fputs(" hidereadmsgs=\"1\"", fp);
2189                 else
2190                         fputs(" hidereadmsgs=\"0\"", fp);
2191                 if (item->ret_rcpt)
2192                         fputs(" reqretrcpt=\"1\"", fp);
2193
2194                 if (item->sort_key != SORT_BY_NONE) {
2195                         fprintf(fp, " sort_key=\"%s\"",
2196                                 sort_key_str[item->sort_key]);
2197                         if (item->sort_type == SORT_ASCENDING)
2198                                 fprintf(fp, " sort_type=\"ascending\"");
2199                         else
2200                                 fprintf(fp, " sort_type=\"descending\"");
2201                 }
2202
2203                 fprintf(fp,
2204                         " mtime=\"%lu\" new=\"%d\" unread=\"%d\" total=\"%d\"",
2205                         item->mtime, item->new, item->unread, item->total);
2206                         
2207                 if (item->account)
2208                         fprintf(fp, " account_id=\"%d\"",
2209                                 item->account->account_id);
2210                 if (item->apply_sub)
2211                         fputs(" apply_sub=\"1\"", fp);
2212         }
2213
2214         if (node->children) {
2215                 GNode *child;
2216                 fputs(">\n", fp);
2217
2218                 child = node->children;
2219                 while (child) {
2220                         GNode *cur;
2221
2222                         cur = child;
2223                         child = cur->next;
2224                         folder_write_list_recursive(cur, data);
2225                 }
2226
2227                 for (i = 0; i < depth; i++)
2228                         fputs("    ", fp);
2229                 fprintf(fp, "</%s>\n", depth == 1 ? "folder" : "folderitem");
2230         } else
2231                 fputs(" />\n", fp);
2232 }
2233
2234 static void folder_update_op_count_rec(GNode *node) {
2235         FolderItem *fitem = FOLDER_ITEM(node->data);
2236
2237         if (g_node_depth(node) > 0) {
2238                 if (fitem->op_count > 0) {
2239                         fitem->op_count = 0;
2240                         folderview_update_item(fitem, 0);
2241                 }
2242                 if (node->children) {
2243                         GNode *child;
2244
2245                         child = node->children;
2246                         while (child) {
2247                                 GNode *cur;
2248
2249                                 cur = child;
2250                                 child = cur->next;
2251                                 folder_update_op_count_rec(cur);
2252                         }
2253                 }
2254         }
2255 }
2256
2257 void folder_update_op_count() {
2258         GList *cur;
2259         Folder *folder;
2260
2261         for (cur = folder_list; cur != NULL; cur = cur->next) {
2262                 folder = cur->data;
2263                 folder_update_op_count_rec(folder->node);
2264         }
2265 }
2266
2267 typedef struct _type_str {
2268         gchar * str;
2269         gint type;
2270 } type_str;
2271
2272
2273 /*
2274 static gchar * folder_item_get_tree_identifier(FolderItem * item)
2275 {
2276         if (item->parent != NULL) {
2277                 gchar * path;
2278                 gchar * id;
2279
2280                 path = folder_item_get_tree_identifier(item->parent);
2281                 if (path == NULL)
2282                         return NULL;
2283
2284                 id = g_strconcat(path, "/", item->name, NULL);
2285                 g_free(path);
2286
2287                 return id;
2288         }
2289         else {
2290                 return g_strconcat("/", item->name, NULL);
2291         }
2292 }
2293 */
2294
2295 /* CLAWS: temporary local folder for filtering */
2296 static Folder *processing_folder;
2297 static FolderItem *processing_folder_item;
2298
2299 static void folder_create_processing_folder(void)
2300 {
2301 #define PROCESSING_FOLDER ".processing" 
2302         Folder     *tmpparent;
2303         gchar      *tmpname;
2304
2305         tmpparent = folder_get_default_folder();
2306         g_assert(tmpparent);
2307         debug_print("tmpparentroot %s\n", LOCAL_FOLDER(tmpparent)->rootpath);
2308         if (LOCAL_FOLDER(tmpparent)->rootpath[0] == '/')
2309                 tmpname = g_strconcat(LOCAL_FOLDER(tmpparent)->rootpath,
2310                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2311                                       NULL);
2312         else
2313                 tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2314                                       LOCAL_FOLDER(tmpparent)->rootpath,
2315                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2316                                       NULL);
2317
2318         processing_folder = folder_new(F_MH, "PROCESSING", LOCAL_FOLDER(tmpparent)->rootpath);
2319         g_assert(processing_folder);
2320
2321         if (!is_dir_exist(tmpname)) {
2322                 debug_print("*TMP* creating %s\n", tmpname);
2323                 processing_folder_item = processing_folder->create_folder(processing_folder,
2324                                                                           processing_folder->node->data,
2325                                                                           PROCESSING_FOLDER);
2326                 g_assert(processing_folder_item);                                                                         
2327         }
2328         else {
2329                 debug_print("*TMP* already created\n");
2330                 processing_folder_item = folder_item_new(processing_folder, ".processing", ".processing");
2331                 g_assert(processing_folder_item);
2332                 folder_item_append(processing_folder->node->data, processing_folder_item);
2333         }
2334         g_free(tmpname);
2335 }
2336
2337 FolderItem *folder_get_default_processing(void)
2338 {
2339         if (!processing_folder_item) {
2340                 folder_create_processing_folder();
2341         }
2342         return processing_folder_item;
2343 }
2344
2345 /* folder_persist_prefs_new() - return hash table with persistent
2346  * settings (and folder name as key). 
2347  * (note that in claws other options are in the PREFS_FOLDER_ITEM_RC
2348  * file, so those don't need to be included in PersistPref yet) 
2349  */
2350 GHashTable *folder_persist_prefs_new(Folder *folder)
2351 {
2352         GHashTable *pptable;
2353
2354         g_return_val_if_fail(folder, NULL);
2355         pptable = g_hash_table_new(g_str_hash, g_str_equal);
2356         folder_get_persist_prefs_recursive(folder->node, pptable);
2357         return pptable;
2358 }
2359
2360 void folder_persist_prefs_free(GHashTable *pptable)
2361 {
2362         g_return_if_fail(pptable);
2363         g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
2364         g_hash_table_destroy(pptable);
2365 }
2366
2367 const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
2368 {
2369         if (pptable == NULL || name == NULL) return NULL;
2370         return g_hash_table_lookup(pptable, name);
2371 }
2372
2373 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
2374 {
2375         const PersistPrefs *pp;
2376         gchar *id = folder_item_get_identifier(item);
2377
2378         pp = folder_get_persist_prefs(pptable, id); 
2379         g_free(id);
2380
2381         if (!pp) return;
2382
2383         /* CLAWS: since not all folder properties have been migrated to 
2384          * folderlist.xml, we need to call the old stuff first before
2385          * setting things that apply both to Main and Claws. */
2386         prefs_folder_item_read_config(item); 
2387          
2388         item->collapsed = pp->collapsed;
2389         item->threaded  = pp->threaded;
2390         item->ret_rcpt  = pp->ret_rcpt;
2391         item->hide_read_msgs = pp->hide_read_msgs;
2392         item->sort_key  = pp->sort_key;
2393         item->sort_type = pp->sort_type;
2394 }
2395
2396 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
2397 {
2398         FolderItem *item = FOLDER_ITEM(node->data);
2399         PersistPrefs *pp;
2400         GNode *child, *cur;
2401         gchar *id;
2402
2403         g_return_if_fail(node != NULL);
2404         g_return_if_fail(item != NULL);
2405
2406         /* NOTE: item->path == NULL means top level folder; not interesting
2407          * to store preferences of that one.  */
2408         if (item->path) {
2409                 id = folder_item_get_identifier(item);
2410                 pp = g_new0(PersistPrefs, 1);
2411                 g_return_if_fail(pp != NULL);
2412                 pp->collapsed = item->collapsed;
2413                 pp->threaded  = item->threaded;
2414                 pp->ret_rcpt  = item->ret_rcpt; 
2415                 pp->hide_read_msgs = item->hide_read_msgs;
2416                 pp->sort_key  = item->sort_key;
2417                 pp->sort_type = item->sort_type;
2418                 g_hash_table_insert(pptable, id, pp);
2419         }
2420
2421         if (node->children) {
2422                 child = node->children;
2423                 while (child) {
2424                         cur = child;
2425                         child = cur->next;
2426                         folder_get_persist_prefs_recursive(cur, pptable);
2427                 }
2428         }       
2429 }
2430
2431 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
2432 {
2433         if (key) 
2434                 g_free(key);
2435         if (val) 
2436                 g_free(val);
2437         return TRUE;    
2438 }
2439
2440 void folder_item_apply_processing(FolderItem *item)
2441 {
2442         GSList *processing_list;
2443         GSList *mlist, *cur;
2444         
2445         g_return_if_fail(item != NULL);
2446         
2447         processing_list = item->prefs->processing;
2448         if (processing_list == NULL)
2449                 return;
2450
2451         mlist = folder_item_get_msg_list(item);
2452         
2453         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2454                 MsgInfo * msginfo;
2455
2456                 msginfo = (MsgInfo *) cur->data;
2457                 filter_message_by_msginfo(processing_list, msginfo);
2458                 procmsg_msginfo_free(msginfo);
2459         }
2460         
2461         folderview_update_items_when_required(FALSE);
2462
2463         g_slist_free(mlist);
2464 }