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