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