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