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