* src/folder.[ch]
[claws.git] / src / folder.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33
34 #include "intl.h"
35 #include "folder.h"
36 #include "folderview.h"
37 #include "session.h"
38 #include "imap.h"
39 #include "news.h"
40 #include "mh.h"
41 #include "mbox_folder.h"
42 #include "utils.h"
43 #include "xml.h"
44 #include "codeconv.h"
45 #include "prefs.h"
46 #include "prefs_common.h"
47 #include "account.h"
48 #include "prefs_account.h"
49 #include "filtering.h"
50 #include "scoring.h"
51 #include "prefs_folder_item.h"
52 #include "procheader.h"
53
54 static GList *folder_list = NULL;
55
56 static void folder_init         (Folder         *folder,
57                                  const gchar    *name);
58
59 static gboolean folder_read_folder_func (GNode          *node,
60                                          gpointer        data);
61 static gchar *folder_get_list_path      (void);
62 static void folder_write_list_recursive (GNode          *node,
63                                          gpointer        data);
64 static void folder_update_op_count_rec  (GNode          *node);
65
66
67 static void folder_get_persist_prefs_recursive
68                                         (GNode *node, GHashTable *pptable);
69 static gboolean persist_prefs_free      (gpointer key, gpointer val, gpointer data);
70 void folder_item_read_cache             (FolderItem *item);
71 void folder_item_free_cache             (FolderItem *item);
72
73 Folder *folder_new(FolderType type, const gchar *name, const gchar *path)
74 {
75         Folder *folder = NULL;
76
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         if((msginfo = folder->fetch_msginfo(folder, item, num)) != NULL) {
1191                 msgcache_add_msg(item->cache, msginfo);
1192                 return msginfo;
1193         }
1194         
1195         return NULL;
1196 }
1197
1198 MsgInfo *folder_item_fetch_msginfo_by_id(FolderItem *item, const gchar *msgid)
1199 {
1200         Folder *folder;
1201         MsgInfo *msginfo;
1202         
1203         g_return_val_if_fail(item != NULL, NULL);
1204         
1205         folder = item->folder;
1206         if(!item->cache)
1207                 folder_item_read_cache(item);
1208         
1209         if((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
1210                 return msginfo;
1211
1212         return NULL;
1213 }
1214
1215 GSList *folder_item_get_msg_list(FolderItem *item)
1216 {
1217         g_return_val_if_fail(item != NULL, NULL);
1218         
1219         if(item->cache == 0)
1220                 folder_item_read_cache(item);
1221
1222         g_return_val_if_fail(item->cache != NULL, NULL);
1223         
1224         return msgcache_get_msg_list(item->cache);
1225 }
1226
1227 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
1228 {
1229         Folder *folder;
1230
1231         g_return_val_if_fail(item != NULL, NULL);
1232
1233         folder = item->folder;
1234
1235         g_return_val_if_fail(folder->fetch_msg != NULL, NULL);
1236
1237         return folder->fetch_msg(folder, item, num);
1238 }
1239
1240 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
1241                          gboolean remove_source)
1242 {
1243         Folder *folder;
1244         gint num;
1245         MsgInfo *msginfo;
1246
1247         g_return_val_if_fail(dest != NULL, -1);
1248         g_return_val_if_fail(file != NULL, -1);
1249
1250         folder = dest->folder;
1251
1252         g_return_val_if_fail(folder->add_msg != NULL, -1);
1253
1254         if (!dest->cache)
1255                 folder_item_read_cache(dest);
1256
1257         num = folder->add_msg(folder, dest, file, remove_source);
1258
1259         if (num > 0) {
1260                 msginfo = folder->fetch_msginfo(folder, dest, num);
1261
1262                 if(MSG_IS_NEW(msginfo->flags))
1263                         dest->new++;
1264                 if(MSG_IS_UNREAD(msginfo->flags))
1265                         dest->unread++;
1266                 dest->total++;
1267
1268                 dest->last_num = num;
1269                 msgcache_add_msg(dest->cache, msginfo);
1270                 procmsg_msginfo_free(msginfo);
1271         }
1272
1273         return num;
1274 }
1275
1276 /*
1277 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1278 {
1279         Folder *folder;
1280         gint num;
1281
1282         g_return_val_if_fail(dest != NULL, -1);
1283         g_return_val_if_fail(msginfo != NULL, -1);
1284
1285         folder = dest->folder;
1286         if (dest->last_num < 0) folder->scan(folder, dest);
1287
1288         num = folder->move_msg(folder, dest, msginfo);
1289         if (num > 0) dest->last_num = num;
1290
1291         return num;
1292 }
1293 */
1294
1295 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1296 {
1297         Folder *folder;
1298         gint num;
1299         Folder * src_folder;
1300
1301         g_return_val_if_fail(dest != NULL, -1);
1302         g_return_val_if_fail(msginfo != NULL, -1);
1303
1304         folder = dest->folder;
1305
1306         g_return_val_if_fail(folder->remove_msg != NULL, -1);
1307         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1308
1309         if (!dest->cache) folder_item_read_cache(dest);
1310
1311         src_folder = msginfo->folder->folder;
1312
1313         num = folder->copy_msg(folder, dest, msginfo);
1314         
1315         if (num != -1) {
1316                 MsgInfo *newmsginfo;
1317
1318                 newmsginfo = folder->fetch_msginfo(folder, dest, num);
1319                 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1320                 if (dest->stype == F_OUTBOX ||
1321                     dest->stype == F_QUEUE  ||
1322                     dest->stype == F_DRAFT  ||
1323                     dest->stype == F_TRASH)
1324                         MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1325                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
1326                 msgcache_add_msg(dest->cache, newmsginfo);
1327
1328                 /* CLAWS */
1329                 if(src_folder->remove_msg) {
1330                         src_folder->remove_msg(src_folder,
1331                                                msginfo->folder,
1332                                                msginfo->msgnum);
1333                 }
1334                 msgcache_remove_msg(msginfo->folder->cache, msginfo->msgnum);
1335
1336                 if (MSG_IS_NEW(msginfo->flags))
1337                         msginfo->folder->new--;
1338                 if (MSG_IS_NEW(newmsginfo->flags))
1339                         dest->new++;
1340                 if (MSG_IS_UNREAD(msginfo->flags))
1341                         msginfo->folder->unread--;
1342                 if (MSG_IS_UNREAD(newmsginfo->flags))
1343                         dest->unread++;
1344                 msginfo->folder->total--;
1345                 dest->total++;
1346
1347                 procmsg_msginfo_free(newmsginfo);
1348         }
1349         
1350         if (folder->finished_copy)
1351                 folder->finished_copy(folder, dest);
1352
1353         return num;
1354 }
1355
1356 /*
1357 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1358 {
1359         Folder *folder;
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         if (dest->last_num < 0) folder->scan(folder, dest);
1367
1368         num = folder->move_msgs_with_dest(folder, dest, msglist);
1369         if (num > 0) dest->last_num = num;
1370         else dest->op_count = 0;
1371
1372         return num;
1373 }
1374 */
1375
1376 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1377 {
1378         Folder *folder;
1379         FolderItem * item;
1380         GSList * l;
1381         gint num;
1382
1383         g_return_val_if_fail(dest != NULL, -1);
1384         g_return_val_if_fail(msglist != NULL, -1);
1385
1386         folder = dest->folder;
1387
1388         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1389         g_return_val_if_fail(folder->remove_msg != NULL, -1);
1390
1391         if (!dest->cache) folder_item_read_cache(dest);
1392
1393         item = NULL;
1394         for(l = msglist ; l != NULL ; l = g_slist_next(l)) {
1395                 MsgInfo * msginfo = (MsgInfo *) l->data;
1396
1397                 if (!item && msginfo->folder != NULL)
1398                         item = msginfo->folder;
1399                 if (!item->cache) folder_item_read_cache(dest);
1400
1401                 num = folder->copy_msg(folder, dest, msginfo);
1402                 if (num != -1) {
1403                         MsgInfo *newmsginfo;
1404
1405                         newmsginfo = folder->fetch_msginfo(folder, dest, num);
1406                         if(newmsginfo) {
1407                                 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1408                                 if (dest->stype == F_OUTBOX ||
1409                                     dest->stype == F_QUEUE  ||
1410                                     dest->stype == F_DRAFT  ||
1411                                     dest->stype == F_TRASH)
1412                                         MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1413                                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
1414                                 msgcache_add_msg(dest->cache, newmsginfo);
1415
1416                                 if (MSG_IS_NEW(msginfo->flags))
1417                                         msginfo->folder->new--;
1418                                 if (MSG_IS_NEW(newmsginfo->flags))
1419                                         dest->new++;
1420                                 if (MSG_IS_UNREAD(msginfo->flags))
1421                                         msginfo->folder->unread--;
1422                                 if (MSG_IS_UNREAD(newmsginfo->flags))
1423                                         dest->unread++;
1424                                 msginfo->folder->total--;
1425                                 dest->total++;
1426
1427                                 procmsg_msginfo_free(newmsginfo);
1428                         }
1429                         item->folder->remove_msg(item->folder,
1430                                                  msginfo->folder,
1431                                                  msginfo->msgnum);
1432                         msgcache_remove_msg(item->cache, msginfo->msgnum);
1433                 }
1434         }
1435
1436         if (folder->finished_copy)
1437                 folder->finished_copy(folder, dest);
1438
1439         return dest->last_num;
1440 }
1441
1442 /*
1443 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1444 {
1445         Folder *folder;
1446         gint num;
1447
1448         g_return_val_if_fail(dest != NULL, -1);
1449         g_return_val_if_fail(msginfo != NULL, -1);
1450
1451         folder = dest->folder;
1452         if (dest->last_num < 0) folder->scan(folder, dest);
1453
1454         num = folder->copy_msg(folder, dest, msginfo);
1455         if (num > 0) dest->last_num = num;
1456
1457         return num;
1458 }
1459 */
1460
1461 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1462 {
1463         Folder *folder;
1464         gint num;
1465
1466         g_return_val_if_fail(dest != NULL, -1);
1467         g_return_val_if_fail(msginfo != NULL, -1);
1468
1469         folder = dest->folder;
1470
1471         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1472
1473         if (!dest->cache) folder_item_read_cache(dest);
1474         
1475         num = folder->copy_msg(folder, dest, msginfo);
1476         if (num != -1) {
1477                 MsgInfo *newmsginfo;
1478
1479                 newmsginfo = folder->fetch_msginfo(folder, dest, num);
1480                 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1481                 if (dest->stype == F_OUTBOX ||
1482                     dest->stype == F_QUEUE  ||
1483                     dest->stype == F_DRAFT  ||
1484                     dest->stype == F_TRASH)
1485                         MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1486                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
1487                 msgcache_add_msg(dest->cache, newmsginfo);
1488
1489                 if (MSG_IS_NEW(newmsginfo->flags))
1490                         dest->new++;
1491                 if (MSG_IS_UNREAD(newmsginfo->flags))
1492                         dest->unread++;
1493                 dest->total++;
1494
1495                 procmsg_msginfo_free(newmsginfo);
1496         }
1497
1498         if (folder->finished_copy)
1499                 folder->finished_copy(folder, dest);
1500
1501         return num;
1502 }
1503
1504 /*
1505 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1506 {
1507         Folder *folder;
1508         gint num;
1509
1510         g_return_val_if_fail(dest != NULL, -1);
1511         g_return_val_if_fail(msglist != NULL, -1);
1512
1513         folder = dest->folder;
1514         if (dest->last_num < 0) folder->scan(folder, dest);
1515
1516         num = folder->copy_msgs_with_dest(folder, dest, msglist);
1517         if (num > 0) dest->last_num = num;
1518         else dest->op_count = 0;
1519
1520         return num;
1521 }
1522 */
1523
1524 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1525 {
1526         Folder *folder;
1527         gint num;
1528         GSList * l;
1529
1530         g_return_val_if_fail(dest != NULL, -1);
1531         g_return_val_if_fail(msglist != NULL, -1);
1532
1533         folder = dest->folder;
1534  
1535         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1536
1537         if (!dest->cache) folder_item_read_cache(dest);
1538
1539         for(l = msglist ; l != NULL ; l = g_slist_next(l)) {
1540                 MsgInfo * msginfo = (MsgInfo *) l->data;
1541
1542                 num = folder->copy_msg(folder, dest, msginfo);
1543                 if (num != -1) {
1544                         MsgInfo *newmsginfo;
1545
1546                         newmsginfo = folder->fetch_msginfo(folder, dest, num);
1547                         if(newmsginfo) {
1548                                 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1549                                 if (dest->stype == F_OUTBOX ||
1550                                     dest->stype == F_QUEUE  ||
1551                                     dest->stype == F_DRAFT  ||
1552                                     dest->stype == F_TRASH)
1553                                         MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1554                                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
1555                                 msgcache_add_msg(dest->cache, newmsginfo);
1556
1557                                 if (MSG_IS_NEW(newmsginfo->flags))
1558                                         dest->new++;
1559                                 if (MSG_IS_UNREAD(newmsginfo->flags))
1560                                         dest->unread++;
1561                                 dest->total++;
1562
1563                                 procmsg_msginfo_free(newmsginfo);
1564                         }
1565                 }
1566         }
1567
1568         if (folder->finished_copy)
1569                 folder->finished_copy(folder, dest);
1570
1571         return dest->last_num;
1572 }
1573
1574 gint folder_item_remove_msg(FolderItem *item, gint num)
1575 {
1576         Folder *folder;
1577         gint ret;
1578         MsgInfo *msginfo;
1579
1580         g_return_val_if_fail(item != NULL, -1);
1581
1582         folder = item->folder;
1583         if (!item->cache) folder_item_read_cache(item);
1584
1585         ret = folder->remove_msg(folder, item, num);
1586
1587         msginfo = msgcache_get_msg(item->cache, num);
1588         if(MSG_IS_NEW(msginfo->flags))
1589                 item->new--;
1590         if(MSG_IS_UNREAD(msginfo->flags))
1591                 item->unread--;
1592         item->total--;
1593         procmsg_msginfo_free(msginfo);
1594         msgcache_remove_msg(item->cache, num);
1595
1596         return ret;
1597 }
1598
1599 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
1600 {
1601         gint ret = 0;
1602
1603         g_return_val_if_fail(item != NULL, -1);
1604
1605         if (!item->cache) folder_item_read_cache(item);
1606
1607         while (msglist != NULL) {
1608                 MsgInfo *msginfo = (MsgInfo *)msglist->data;
1609
1610                 ret = folder_item_remove_msg(item, msginfo->msgnum);
1611                 if (ret != 0) break;
1612                 msgcache_remove_msg(item->cache, msginfo->msgnum);
1613                 msglist = msglist->next;
1614         }
1615
1616         return ret;
1617 }
1618
1619 gint folder_item_remove_all_msg(FolderItem *item)
1620 {
1621         Folder *folder;
1622         gint result;
1623
1624         g_return_val_if_fail(item != NULL, -1);
1625
1626         folder = item->folder;
1627
1628         g_return_val_if_fail(folder->remove_all_msg != NULL, -1);
1629
1630         result = folder->remove_all_msg(folder, item);
1631
1632         if (result == 0) {
1633                 if (folder->finished_remove)
1634                         folder->finished_remove(folder, item);
1635
1636                 folder_item_free_cache(item);
1637                 item->cache = msgcache_new();
1638
1639                 item->new = 0;
1640                 item->unread = 0;
1641                 item->total = 0;
1642         }
1643
1644         return result;
1645 }
1646
1647 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
1648 {
1649         Folder *folder;
1650
1651         g_return_val_if_fail(item != NULL, FALSE);
1652
1653         folder = item->folder;
1654
1655         g_return_val_if_fail(folder->is_msg_changed != NULL, -1);
1656
1657         return folder->is_msg_changed(folder, item, msginfo);
1658 }
1659
1660 gchar *folder_item_get_cache_file(FolderItem *item)
1661 {
1662         gchar *path;
1663         gchar *file;
1664
1665         g_return_val_if_fail(item != NULL, NULL);
1666         g_return_val_if_fail(item->path != NULL, NULL);
1667
1668         path = folder_item_get_path(item);
1669         g_return_val_if_fail(path != NULL, NULL);
1670         if (!is_dir_exist(path))
1671                 make_dir_hier(path);
1672         file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
1673         g_free(path);
1674
1675         return file;
1676 }
1677
1678 gchar *folder_item_get_mark_file(FolderItem *item)
1679 {
1680         gchar *path;
1681         gchar *file;
1682
1683         g_return_val_if_fail(item != NULL, NULL);
1684         g_return_val_if_fail(item->path != NULL, NULL);
1685
1686         path = folder_item_get_path(item);
1687         g_return_val_if_fail(path != NULL, NULL);
1688         if (!is_dir_exist(path))
1689                 make_dir_hier(path);
1690         file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
1691         g_free(path);
1692
1693         return file;
1694 }
1695
1696 static gboolean folder_build_tree(GNode *node, gpointer data)
1697 {
1698         Folder *folder = FOLDER(data);
1699         FolderItem *item;
1700         XMLNode *xmlnode;
1701         GList *list;
1702         SpecialFolderItemType stype = F_NORMAL;
1703         const gchar *name = NULL;
1704         const gchar *path = NULL;
1705         PrefsAccount *account = NULL;
1706         gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE, 
1707                  threaded = TRUE, ret_rcpt = FALSE, hidereadmsgs = FALSE;
1708         FolderSortKey sort_key = SORT_BY_NONE;
1709         FolderSortType sort_type = SORT_ASCENDING;
1710         gint new = 0, unread = 0, total = 0;
1711         time_t mtime = 0;
1712
1713         g_return_val_if_fail(node->data != NULL, FALSE);
1714         if (!node->parent) return FALSE;
1715
1716         xmlnode = node->data;
1717         if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
1718                 g_warning("tag name != \"folderitem\"\n");
1719                 return FALSE;
1720         }
1721
1722         list = xmlnode->tag->attr;
1723         for (; list != NULL; list = list->next) {
1724                 XMLAttr *attr = list->data;
1725
1726                 if (!attr || !attr->name || !attr->value) continue;
1727                 if (!strcmp(attr->name, "type")) {
1728                         if (!strcasecmp(attr->value, "normal"))
1729                                 stype = F_NORMAL;
1730                         else if (!strcasecmp(attr->value, "inbox"))
1731                                 stype = F_INBOX;
1732                         else if (!strcasecmp(attr->value, "outbox"))
1733                                 stype = F_OUTBOX;
1734                         else if (!strcasecmp(attr->value, "draft"))
1735                                 stype = F_DRAFT;
1736                         else if (!strcasecmp(attr->value, "queue"))
1737                                 stype = F_QUEUE;
1738                         else if (!strcasecmp(attr->value, "trash"))
1739                                 stype = F_TRASH;
1740                 } else if (!strcmp(attr->name, "name"))
1741                         name = attr->value;
1742                 else if (!strcmp(attr->name, "path"))
1743                         path = attr->value;
1744                 else if (!strcmp(attr->name, "account_id")) {
1745                         account = account_find_from_id(atoi(attr->value));
1746                         if (!account) g_warning("account_id: %s not found\n",
1747                                                 attr->value);
1748                 } else if (!strcmp(attr->name, "mtime"))
1749                         mtime = strtoul(attr->value, NULL, 10);
1750                 else if (!strcmp(attr->name, "new"))
1751                         new = atoi(attr->value);
1752                 else if (!strcmp(attr->name, "unread"))
1753                         unread = atoi(attr->value);
1754                 else if (!strcmp(attr->name, "total"))
1755                         total = atoi(attr->value);
1756                 else if (!strcmp(attr->name, "no_sub"))
1757                         no_sub = *attr->value == '1' ? TRUE : FALSE;
1758                 else if (!strcmp(attr->name, "no_select"))
1759                         no_select = *attr->value == '1' ? TRUE : FALSE;
1760                 else if (!strcmp(attr->name, "collapsed"))
1761                         collapsed = *attr->value == '1' ? TRUE : FALSE;
1762                 else if (!strcmp(attr->name, "threaded"))
1763                         threaded =  *attr->value == '1' ? TRUE : FALSE;
1764                 else if (!strcmp(attr->name, "hidereadmsgs"))
1765                         hidereadmsgs =  *attr->value == '1' ? TRUE : FALSE;
1766                 else if (!strcmp(attr->name, "reqretrcpt"))
1767                         ret_rcpt =  *attr->value == '1' ? TRUE : FALSE;
1768                 else if (!strcmp(attr->name, "sort_key")) {
1769                         if (!strcmp(attr->value, "none"))
1770                                 sort_key = SORT_BY_NONE;
1771                         else if (!strcmp(attr->value, "number"))
1772                                 sort_key = SORT_BY_NUMBER;
1773                         else if (!strcmp(attr->value, "size"))
1774                                 sort_key = SORT_BY_SIZE;
1775                         else if (!strcmp(attr->value, "date"))
1776                                 sort_key = SORT_BY_DATE;
1777                         else if (!strcmp(attr->value, "from"))
1778                                 sort_key = SORT_BY_FROM;
1779                         else if (!strcmp(attr->value, "subject"))
1780                                 sort_key = SORT_BY_SUBJECT;
1781                         else if (!strcmp(attr->value, "score"))
1782                                 sort_key = SORT_BY_SCORE;
1783                         else if (!strcmp(attr->value, "label"))
1784                                 sort_key = SORT_BY_LABEL;
1785                         else if (!strcmp(attr->value, "mark"))
1786                                 sort_key = SORT_BY_MARK;
1787                         else if (!strcmp(attr->value, "unread"))
1788                                 sort_key = SORT_BY_UNREAD;
1789                         else if (!strcmp(attr->value, "mime"))
1790                                 sort_key = SORT_BY_MIME;
1791                         else if (!strcmp(attr->value, "locked"))
1792                                 sort_key = SORT_BY_LOCKED;
1793                 } else if (!strcmp(attr->name, "sort_type")) {
1794                         if (!strcmp(attr->value, "ascending"))
1795                                 sort_type = SORT_ASCENDING;
1796                         else
1797                                 sort_type = SORT_DESCENDING;
1798                 }
1799         }
1800
1801         item = folder_item_new(name, path);
1802         item->stype = stype;
1803         item->account = account;
1804         item->mtime = mtime;
1805         item->new = new;
1806         item->unread = unread;
1807         item->total = total;
1808         item->no_sub = no_sub;
1809         item->no_select = no_select;
1810         item->collapsed = collapsed;
1811         item->threaded  = threaded;
1812         item->hide_read_msgs  = hidereadmsgs;
1813         item->ret_rcpt  = ret_rcpt;
1814         item->sort_key  = sort_key;
1815         item->sort_type = sort_type;
1816         item->parent = FOLDER_ITEM(node->parent->data);
1817         item->folder = folder;
1818         switch (stype) {
1819         case F_INBOX:  folder->inbox  = item; break;
1820         case F_OUTBOX: folder->outbox = item; break;
1821         case F_DRAFT:  folder->draft  = item; break;
1822         case F_QUEUE:  folder->queue  = item; break;
1823         case F_TRASH:  folder->trash  = item; break;
1824         default:       break;
1825         }
1826
1827         prefs_folder_item_read_config(item);
1828
1829         node->data = item;
1830         xml_free_node(xmlnode);
1831
1832         return FALSE;
1833 }
1834
1835 static gboolean folder_read_folder_func(GNode *node, gpointer data)
1836 {
1837         Folder *folder;
1838         XMLNode *xmlnode;
1839         GList *list;
1840         FolderType type = F_UNKNOWN;
1841         const gchar *name = NULL;
1842         const gchar *path = NULL;
1843         PrefsAccount *account = NULL;
1844         gboolean collapsed = FALSE, threaded = TRUE, ret_rcpt = FALSE;
1845
1846         if (g_node_depth(node) != 2) return FALSE;
1847         g_return_val_if_fail(node->data != NULL, FALSE);
1848
1849         xmlnode = node->data;
1850         if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
1851                 g_warning("tag name != \"folder\"\n");
1852                 return TRUE;
1853         }
1854         g_node_unlink(node);
1855         list = xmlnode->tag->attr;
1856         for (; list != NULL; list = list->next) {
1857                 XMLAttr *attr = list->data;
1858
1859                 if (!attr || !attr->name || !attr->value) continue;
1860                 if (!strcmp(attr->name, "type")) {
1861                         if (!strcasecmp(attr->value, "mh"))
1862                                 type = F_MH;
1863                         else if (!strcasecmp(attr->value, "mbox"))
1864                                 type = F_MBOX;
1865                         else if (!strcasecmp(attr->value, "maildir"))
1866                                 type = F_MAILDIR;
1867                         else if (!strcasecmp(attr->value, "imap"))
1868                                 type = F_IMAP;
1869                         else if (!strcasecmp(attr->value, "news"))
1870                                 type = F_NEWS;
1871                 } else if (!strcmp(attr->name, "name"))
1872                         name = attr->value;
1873                 else if (!strcmp(attr->name, "path"))
1874                         path = attr->value;
1875                 else if (!strcmp(attr->name, "account_id")) {
1876                         account = account_find_from_id(atoi(attr->value));
1877                         if (!account) g_warning("account_id: %s not found\n",
1878                                                 attr->value);
1879                 } else if (!strcmp(attr->name, "collapsed"))
1880                         collapsed = *attr->value == '1' ? TRUE : FALSE;
1881                 else if (!strcmp(attr->name, "threaded"))
1882                         threaded = *attr->value == '1' ? TRUE : FALSE;
1883                 else if (!strcmp(attr->name, "reqretrcpt"))
1884                         ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
1885         }
1886
1887         folder = folder_new(type, name, path);
1888         g_return_val_if_fail(folder != NULL, FALSE);
1889         folder->account = account;
1890         if (account && (type == F_IMAP || type == F_NEWS))
1891                 account->folder = REMOTE_FOLDER(folder);
1892         node->data = folder->node->data;
1893         g_node_destroy(folder->node);
1894         folder->node = node;
1895         folder_add(folder);
1896         FOLDER_ITEM(node->data)->collapsed = collapsed;
1897         FOLDER_ITEM(node->data)->threaded  = threaded;
1898         FOLDER_ITEM(node->data)->ret_rcpt  = ret_rcpt;
1899
1900         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1901                         folder_build_tree, folder);
1902
1903         return FALSE;
1904 }
1905
1906 static gchar *folder_get_list_path(void)
1907 {
1908         static gchar *filename = NULL;
1909
1910         if (!filename)
1911                 filename =  g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1912                                         FOLDER_LIST, NULL);
1913
1914         return filename;
1915 }
1916
1917 static void folder_write_list_recursive(GNode *node, gpointer data)
1918 {
1919         FILE *fp = (FILE *)data;
1920         FolderItem *item = FOLDER_ITEM(node->data);
1921         gint i, depth;
1922         static gchar *folder_type_str[] = {"mh", "mbox", "maildir", "imap",
1923                                            "news", "unknown"};
1924         static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
1925                                                  "draft", "queue", "trash"};
1926         static gchar *sort_key_str[] = {"none", "number", "size", "date",
1927                                         "from", "subject", "score", "label",
1928                                         "mark", "unread", "mime", "locked" };
1929
1930         g_return_if_fail(item != NULL);
1931
1932         depth = g_node_depth(node);
1933         for (i = 0; i < depth; i++)
1934                 fputs("    ", fp);
1935         if (depth == 1) {
1936                 Folder *folder = item->folder;
1937
1938                 fprintf(fp, "<folder type=\"%s\"", folder_type_str[folder->type]);
1939                 if (folder->name) {
1940                         fputs(" name=\"", fp);
1941                         xml_file_put_escape_str(fp, folder->name);
1942                         fputs("\"", fp);
1943                 }
1944                 if ((folder->type == F_MH) || (folder->type == F_MBOX)) {
1945                         fputs(" path=\"", fp);
1946                         xml_file_put_escape_str
1947                                 (fp, LOCAL_FOLDER(folder)->rootpath);
1948                         fputs("\"", fp);
1949                 }
1950                 if (folder->account)
1951                         fprintf(fp, " account_id=\"%d\"",
1952                                 folder->account->account_id);
1953                 if (item->collapsed && node->children)
1954                         fputs(" collapsed=\"1\"", fp);
1955                 if (item->ret_rcpt) 
1956                         fputs(" reqretrcpt=\"1\"", fp);
1957         } else {
1958                 fprintf(fp, "<folderitem type=\"%s\"",
1959                         folder_item_stype_str[item->stype]);
1960                 if (item->name) {
1961                         fputs(" name=\"", fp);
1962                         xml_file_put_escape_str(fp, item->name);
1963                         fputs("\"", fp);
1964                 }
1965                 if (item->path) {
1966                         fputs(" path=\"", fp);
1967                         xml_file_put_escape_str(fp, item->path);
1968                         fputs("\"", fp);
1969                 }
1970                 if (item->account)
1971                         fprintf(fp, " account_id=\"%d\"",
1972                                 item->account->account_id);
1973                 if (item->no_sub)
1974                         fputs(" no_sub=\"1\"", fp);
1975                 if (item->no_select)
1976                         fputs(" no_select=\"1\"", fp);
1977                 if (item->collapsed && node->children)
1978                         fputs(" collapsed=\"1\"", fp);
1979                 if (item->threaded)
1980                         fputs(" threaded=\"1\"", fp);
1981                 else
1982                         fputs(" threaded=\"0\"", fp);
1983                 if (item->hide_read_msgs)
1984                         fputs(" hidereadmsgs=\"1\"", fp);
1985                 else
1986                         fputs(" hidereadmsgs=\"0\"", fp);
1987                 if (item->ret_rcpt)
1988                         fputs(" reqretrcpt=\"1\"", fp);
1989
1990                 if (item->sort_key != SORT_BY_NONE) {
1991                         fprintf(fp, " sort_key=\"%s\"",
1992                                 sort_key_str[item->sort_key]);
1993                         if (item->sort_type == SORT_ASCENDING)
1994                                 fprintf(fp, " sort_type=\"ascending\"");
1995                         else
1996                                 fprintf(fp, " sort_type=\"descending\"");
1997                 }
1998
1999                 fprintf(fp,
2000                         " mtime=\"%lu\" new=\"%d\" unread=\"%d\" total=\"%d\"",
2001                         item->mtime, item->new, item->unread, item->total);
2002         }
2003
2004         if (node->children) {
2005                 GNode *child;
2006                 fputs(">\n", fp);
2007
2008                 child = node->children;
2009                 while (child) {
2010                         GNode *cur;
2011
2012                         cur = child;
2013                         child = cur->next;
2014                         folder_write_list_recursive(cur, data);
2015                 }
2016
2017                 for (i = 0; i < depth; i++)
2018                         fputs("    ", fp);
2019                 fprintf(fp, "</%s>\n", depth == 1 ? "folder" : "folderitem");
2020         } else
2021                 fputs(" />\n", fp);
2022 }
2023
2024 static void folder_update_op_count_rec(GNode *node) {
2025         FolderItem *fitem = FOLDER_ITEM(node->data);
2026
2027         if (g_node_depth(node) > 0) {
2028                 if (fitem->op_count > 0) {
2029                         fitem->op_count = 0;
2030                         folderview_update_item(fitem, 0);
2031                 }
2032                 if (node->children) {
2033                         GNode *child;
2034
2035                         child = node->children;
2036                         while (child) {
2037                                 GNode *cur;
2038
2039                                 cur = child;
2040                                 child = cur->next;
2041                                 folder_update_op_count_rec(cur);
2042                         }
2043                 }
2044         }
2045 }
2046
2047 void folder_update_op_count() {
2048         GList *cur;
2049         Folder *folder;
2050
2051         for (cur = folder_list; cur != NULL; cur = cur->next) {
2052                 folder = cur->data;
2053                 folder_update_op_count_rec(folder->node);
2054         }
2055 }
2056
2057 typedef struct _type_str {
2058         gchar * str;
2059         gint type;
2060 } type_str;
2061
2062
2063 /*
2064 static gchar * folder_item_get_tree_identifier(FolderItem * item)
2065 {
2066         if (item->parent != NULL) {
2067                 gchar * path;
2068                 gchar * id;
2069
2070                 path = folder_item_get_tree_identifier(item->parent);
2071                 if (path == NULL)
2072                         return NULL;
2073
2074                 id = g_strconcat(path, "/", item->name, NULL);
2075                 g_free(path);
2076
2077                 return id;
2078         }
2079         else {
2080                 return g_strconcat("/", item->name, NULL);
2081         }
2082 }
2083 */
2084
2085 /* CLAWS: temporary local folder for filtering */
2086 static Folder *processing_folder;
2087 static FolderItem *processing_folder_item;
2088
2089 static void folder_create_processing_folder(void)
2090 {
2091 #define PROCESSING_FOLDER ".processing" 
2092         Folder     *tmpparent;
2093         gchar      *tmpname;
2094
2095         tmpparent = folder_get_default_folder();
2096         g_assert(tmpparent);
2097         debug_print("tmpparentroot %s\n", LOCAL_FOLDER(tmpparent)->rootpath);
2098         if (LOCAL_FOLDER(tmpparent)->rootpath[0] == '/')
2099                 tmpname = g_strconcat(LOCAL_FOLDER(tmpparent)->rootpath,
2100                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2101                                       NULL);
2102         else
2103                 tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2104                                       LOCAL_FOLDER(tmpparent)->rootpath,
2105                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2106                                       NULL);
2107
2108         processing_folder = folder_new(F_MH, "PROCESSING", LOCAL_FOLDER(tmpparent)->rootpath);
2109         g_assert(processing_folder);
2110
2111         if (!is_dir_exist(tmpname)) {
2112                 debug_print("*TMP* creating %s\n", tmpname);
2113                 processing_folder_item = processing_folder->create_folder(processing_folder,
2114                                                                           processing_folder->node->data,
2115                                                                           PROCESSING_FOLDER);
2116                 g_assert(processing_folder_item);                                                                         
2117         }
2118         else {
2119                 debug_print("*TMP* already created\n");
2120                 processing_folder_item = folder_item_new(".processing", ".processing");
2121                 g_assert(processing_folder_item);
2122                 folder_item_append(processing_folder->node->data, processing_folder_item);
2123         }
2124         g_free(tmpname);
2125 }
2126
2127 FolderItem *folder_get_default_processing(void)
2128 {
2129         if (!processing_folder_item) {
2130                 folder_create_processing_folder();
2131         }
2132         return processing_folder_item;
2133 }
2134
2135 /* folder_persist_prefs_new() - return hash table with persistent
2136  * settings (and folder name as key). 
2137  * (note that in claws other options are in the PREFS_FOLDER_ITEM_RC
2138  * file, so those don't need to be included in PersistPref yet) 
2139  */
2140 GHashTable *folder_persist_prefs_new(Folder *folder)
2141 {
2142         GHashTable *pptable;
2143
2144         g_return_val_if_fail(folder, NULL);
2145         pptable = g_hash_table_new(g_str_hash, g_str_equal);
2146         folder_get_persist_prefs_recursive(folder->node, pptable);
2147         return pptable;
2148 }
2149
2150 void folder_persist_prefs_free(GHashTable *pptable)
2151 {
2152         g_return_if_fail(pptable);
2153         g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
2154         g_hash_table_destroy(pptable);
2155 }
2156
2157 const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
2158 {
2159         if (pptable == NULL || name == NULL) return NULL;
2160         return g_hash_table_lookup(pptable, name);
2161 }
2162
2163 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
2164 {
2165         const PersistPrefs *pp;
2166
2167         pp = folder_get_persist_prefs(pptable, item->path); 
2168         if (!pp) return;
2169
2170         /* CLAWS: since not all folder properties have been migrated to 
2171          * folderlist.xml, we need to call the old stuff first before
2172          * setting things that apply both to Main and Claws. */
2173         prefs_folder_item_read_config(item); 
2174          
2175         item->collapsed = pp->collapsed;
2176         item->threaded  = pp->threaded;
2177         item->ret_rcpt  = pp->ret_rcpt;
2178         item->hide_read_msgs = pp->hide_read_msgs;
2179         item->sort_key  = pp->sort_key;
2180         item->sort_type = pp->sort_type;
2181 }
2182
2183 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
2184 {
2185         FolderItem *item = FOLDER_ITEM(node->data);
2186         PersistPrefs *pp;
2187         GNode *child, *cur;
2188
2189         g_return_if_fail(node != NULL);
2190         g_return_if_fail(item != NULL);
2191
2192         /* FIXME: item->path == NULL for top level folder, so this means that 
2193          * properties of MH folder root will not be stored. Not quite important, 
2194          * because the top level folder properties are not special anyway. */
2195         if (item->path) {
2196                 pp = g_new0(PersistPrefs, 1);
2197                 g_return_if_fail(pp != NULL);
2198                 pp->collapsed = item->collapsed;
2199                 pp->threaded  = item->threaded;
2200                 pp->ret_rcpt  = item->ret_rcpt; 
2201                 pp->hide_read_msgs = item->hide_read_msgs;
2202                 pp->sort_key  = item->sort_key;
2203                 pp->sort_type = item->sort_type;
2204                 g_hash_table_insert(pptable, g_strdup(item->path), pp);
2205         }               
2206
2207         if (node->children) {
2208                 child = node->children;
2209                 while (child) {
2210                         cur = child;
2211                         child = cur->next;
2212                         folder_get_persist_prefs_recursive(cur, pptable);
2213                 }
2214         }       
2215 }
2216
2217 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
2218 {
2219         if (key) 
2220                 g_free(key);
2221         if (val) 
2222                 g_free(val);
2223         return TRUE;    
2224 }
2225