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