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