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