* src/folder.c
[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                         MsgInfo *msginfo;
961
962                         msginfo = msgcache_get_msg(item->cache, num);
963                         if(folder->is_msg_changed && folder->is_msg_changed(folder, item, msginfo)) {
964                                 MsgInfo *newmsginfo;
965
966                                 msgcache_remove_msg(item->cache, msginfo->msgnum);
967
968                                 newmsginfo = folder->fetch_msginfo(folder, item, num);
969                                 msgcache_add_msg(item->cache, newmsginfo);
970                                 if(MSG_IS_NEW(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
971                                         newcnt++;
972                                 if(MSG_IS_UNREAD(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
973                                         unreadcnt++;
974                                 procmsg_msginfo_free(newmsginfo);
975
976                                 debug_print(_("Updated msginfo for message %d.\n"), num);
977                         } else {
978                                 if(MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
979                                         newcnt++;
980                                 if(MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
981                                         unreadcnt++;
982                         }
983                         totalcnt++;
984                         procmsg_msginfo_free(msginfo);
985                 }
986         }
987
988         if(folder->fetch_msginfos) {
989                 GSList *newmsg_list;
990                 MsgInfo *msginfo;
991                 
992                 if(new_list) {
993                         newmsg_list = folder->fetch_msginfos(folder, item, new_list);
994                         for(elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
995                                 msginfo = (MsgInfo *) elem->data;
996                                 msgcache_add_msg(item->cache, msginfo);
997                                 if(MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
998                                         newcnt++;
999                                 if(MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1000                                         unreadcnt++;
1001                                 totalcnt++;
1002                                 procmsg_msginfo_free(msginfo);
1003                         }
1004                         g_slist_free(newmsg_list);
1005                         folderview_update_item(item, FALSE);
1006                 }
1007         } else if (folder->fetch_msginfo) {
1008                 for(elem = new_list; elem != NULL; elem = g_slist_next(elem)) {
1009                         MsgInfo *msginfo;
1010                         guint num;
1011
1012                         num = GPOINTER_TO_INT(elem->data);
1013                         msginfo = folder->fetch_msginfo(folder, item, num);
1014                         if(msginfo != NULL) {
1015                                 msgcache_add_msg(item->cache, msginfo);
1016                                 if(MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1017                                     newcnt++;
1018                                 if(MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1019                                     unreadcnt++;
1020                                 totalcnt++;
1021                                 procmsg_msginfo_free(msginfo);
1022                                 debug_print(_("Added newly found message %d to cache.\n"), num);
1023                         }
1024                 }
1025                 folderview_update_item(item, FALSE);
1026         }
1027
1028         item->new = newcnt;
1029         item->unread = unreadcnt;
1030         item->total = totalcnt;
1031         
1032         g_slist_free(folder_list);
1033         g_slist_free(cache_list);
1034         g_slist_free(new_list);
1035         g_free(folderscaninfo);
1036
1037         return 0;
1038 }
1039
1040 static void folder_item_scan_foreach_func(gpointer key, gpointer val,
1041                                           gpointer data)
1042 {
1043         folder_item_scan(FOLDER_ITEM(key));
1044 }
1045
1046 void folder_item_scan_foreach(GHashTable *table)
1047 {
1048         g_hash_table_foreach(table, folder_item_scan_foreach_func, NULL);
1049 }
1050
1051 void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
1052 {
1053         gint *memusage = (gint *)data;
1054
1055         if(item->cache == NULL)
1056                 return;
1057         
1058         *memusage += msgcache_get_memory_usage(item->cache);
1059 }
1060
1061 gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
1062 {
1063         FolderItem *fa = (FolderItem *)a;
1064         FolderItem *fb = (FolderItem *)b;
1065         
1066         return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
1067 }
1068
1069 void folder_find_expired_caches(FolderItem *item, gpointer data)
1070 {
1071         GSList **folder_item_list = (GSList **)data;
1072         gint difftime, expiretime;
1073         
1074         if(item->cache == NULL)
1075                 return;
1076
1077         difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
1078         expiretime = prefs_common.cache_min_keep_time * 60;
1079         debug_print(_("Cache unused time: %d (Expire time: %d)\n"), difftime, expiretime);
1080         if(difftime > expiretime) {
1081                 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
1082         }
1083 }
1084
1085 void folder_item_free_cache(FolderItem *item)
1086 {
1087         g_return_if_fail(item != NULL);
1088         
1089         if(item->cache == NULL)
1090                 return;
1091         
1092         folder_item_write_cache(item);
1093         msgcache_destroy(item->cache);
1094         item->cache = NULL;
1095 }
1096
1097 void folder_clean_cache_memory()
1098 {
1099         gint memusage = 0;
1100
1101         folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);       
1102         debug_print(_("Total cache memory usage: %d\n"), memusage);
1103         
1104         if(memusage > (prefs_common.cache_max_mem_usage * 1024)) {
1105                 GSList *folder_item_list = NULL, *listitem;
1106                 
1107                 debug_print(_("Trying to free cache memory\n"));
1108
1109                 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);      
1110                 listitem = folder_item_list;
1111                 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
1112                         FolderItem *item = (FolderItem *)(listitem->data);
1113
1114                         debug_print(_("Freeing cache memory for %s\n"), item->path);
1115                         memusage -= msgcache_get_memory_usage(item->cache);
1116                         folder_item_free_cache(item);
1117                         listitem = listitem->next;
1118                 }
1119                 g_slist_free(folder_item_list);
1120         }
1121 }
1122
1123 void folder_item_read_cache(FolderItem *item)
1124 {
1125         gchar *cache_file, *mark_file;
1126         
1127         g_return_if_fail(item != NULL);
1128
1129         cache_file = folder_item_get_cache_file(item);
1130         mark_file = folder_item_get_mark_file(item);
1131         item->cache = msgcache_read_cache(item, cache_file);
1132         if(!item->cache) {
1133                 item->cache = msgcache_new();
1134                 folder_item_scan(item);
1135         }
1136         msgcache_read_mark(item->cache, mark_file);
1137         g_free(cache_file);
1138         g_free(mark_file);
1139
1140         folder_clean_cache_memory();
1141 }
1142
1143 void folder_item_write_cache(FolderItem *item)
1144 {
1145         gchar *cache_file, *mark_file;
1146         PrefsFolderItem *prefs;
1147         gint filemode = 0;
1148         gchar *id;
1149         
1150         if (!item || !item->path || !item->cache)
1151                 return;
1152
1153         id = folder_item_get_identifier(item);
1154         debug_print(_("Save cache for folder %s\n"), id);
1155         g_free(id);
1156
1157         cache_file = folder_item_get_cache_file(item);
1158         mark_file = folder_item_get_mark_file(item);
1159         if(msgcache_write(cache_file, mark_file, item->cache) < 0) {
1160                 prefs = item->prefs;
1161                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
1162                         /* for cache file */
1163                         filemode = prefs->folder_chmod;
1164                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
1165                         if (filemode & S_IROTH) filemode |= S_IWOTH;
1166                         chmod(cache_file, filemode);
1167                 }
1168         }
1169
1170         g_free(cache_file);
1171         g_free(mark_file);
1172 }
1173
1174 MsgInfo *folder_item_fetch_msginfo(FolderItem *item, gint num)
1175 {
1176         Folder *folder;
1177         MsgInfo *msginfo;
1178         
1179         g_return_val_if_fail(item != NULL, NULL);
1180         
1181         folder = item->folder;
1182         if(!item->cache)
1183                 folder_item_read_cache(item);
1184         
1185         if((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
1186                 return msginfo;
1187         
1188         g_return_val_if_fail(folder->fetch_msginfo, NULL);
1189         if((msginfo = folder->fetch_msginfo(folder, item, num)) != NULL) {
1190                 msgcache_add_msg(item->cache, msginfo);
1191                 return msginfo;
1192         }
1193         
1194         return NULL;
1195 }
1196
1197 MsgInfo *folder_item_fetch_msginfo_by_id(FolderItem *item, const gchar *msgid)
1198 {
1199         Folder *folder;
1200         MsgInfo *msginfo;
1201         
1202         g_return_val_if_fail(item != NULL, NULL);
1203         
1204         folder = item->folder;
1205         if(!item->cache)
1206                 folder_item_read_cache(item);
1207         
1208         if((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
1209                 return msginfo;
1210
1211         return NULL;
1212 }
1213
1214 GSList *folder_item_get_msg_list(FolderItem *item)
1215 {
1216         g_return_val_if_fail(item != NULL, NULL);
1217         
1218         if(item->cache == 0)
1219                 folder_item_read_cache(item);
1220
1221         g_return_val_if_fail(item->cache != NULL, NULL);
1222         
1223         return msgcache_get_msg_list(item->cache);
1224 }
1225
1226 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
1227 {
1228         Folder *folder;
1229
1230         g_return_val_if_fail(item != NULL, NULL);
1231
1232         folder = item->folder;
1233
1234         g_return_val_if_fail(folder->fetch_msg != NULL, NULL);
1235
1236         return folder->fetch_msg(folder, item, num);
1237 }
1238
1239 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
1240                          gboolean remove_source)
1241 {
1242         Folder *folder;
1243         gint num;
1244         MsgInfo *msginfo;
1245
1246         g_return_val_if_fail(dest != NULL, -1);
1247         g_return_val_if_fail(file != NULL, -1);
1248
1249         folder = dest->folder;
1250
1251         g_return_val_if_fail(folder->add_msg != NULL, -1);
1252
1253         if (!dest->cache)
1254                 folder_item_read_cache(dest);
1255
1256         num = folder->add_msg(folder, dest, file, remove_source);
1257
1258         if (num > 0) {
1259                 msginfo = folder->fetch_msginfo(folder, dest, num);
1260
1261                 if(MSG_IS_NEW(msginfo->flags))
1262                         dest->new++;
1263                 if(MSG_IS_UNREAD(msginfo->flags))
1264                         dest->unread++;
1265                 dest->total++;
1266
1267                 dest->last_num = num;
1268                 msgcache_add_msg(dest->cache, msginfo);
1269                 procmsg_msginfo_free(msginfo);
1270         }
1271
1272         return num;
1273 }
1274
1275 /*
1276 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1277 {
1278         Folder *folder;
1279         gint num;
1280
1281         g_return_val_if_fail(dest != NULL, -1);
1282         g_return_val_if_fail(msginfo != NULL, -1);
1283
1284         folder = dest->folder;
1285         if (dest->last_num < 0) folder->scan(folder, dest);
1286
1287         num = folder->move_msg(folder, dest, msginfo);
1288         if (num > 0) dest->last_num = num;
1289
1290         return num;
1291 }
1292 */
1293
1294 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1295 {
1296         Folder *folder;
1297         gint num;
1298         Folder * src_folder;
1299
1300         g_return_val_if_fail(dest != NULL, -1);
1301         g_return_val_if_fail(msginfo != NULL, -1);
1302
1303         folder = dest->folder;
1304
1305         g_return_val_if_fail(folder->remove_msg != NULL, -1);
1306         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1307
1308         if (!dest->cache) folder_item_read_cache(dest);
1309
1310         src_folder = msginfo->folder->folder;
1311
1312         num = folder->copy_msg(folder, dest, msginfo);
1313         
1314         if (num != -1) {
1315                 MsgInfo *newmsginfo;
1316
1317                 newmsginfo = folder->fetch_msginfo(folder, dest, num);
1318                 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1319                 if (dest->stype == F_OUTBOX ||
1320                     dest->stype == F_QUEUE  ||
1321                     dest->stype == F_DRAFT  ||
1322                     dest->stype == F_TRASH)
1323                         MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1324                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
1325                 msgcache_add_msg(dest->cache, newmsginfo);
1326
1327                 /* CLAWS */
1328                 if(src_folder->remove_msg) {
1329                         src_folder->remove_msg(src_folder,
1330                                                msginfo->folder,
1331                                                msginfo->msgnum);
1332                 }
1333                 msgcache_remove_msg(msginfo->folder->cache, msginfo->msgnum);
1334
1335                 if (MSG_IS_NEW(msginfo->flags))
1336                         msginfo->folder->new--;
1337                 if (MSG_IS_NEW(newmsginfo->flags))
1338                         dest->new++;
1339                 if (MSG_IS_UNREAD(msginfo->flags))
1340                         msginfo->folder->unread--;
1341                 if (MSG_IS_UNREAD(newmsginfo->flags))
1342                         dest->unread++;
1343                 msginfo->folder->total--;
1344                 dest->total++;
1345
1346                 procmsg_msginfo_free(newmsginfo);
1347         }
1348         
1349         if (folder->finished_copy)
1350                 folder->finished_copy(folder, dest);
1351
1352         return num;
1353 }
1354
1355 /*
1356 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1357 {
1358         Folder *folder;
1359         gint num;
1360
1361         g_return_val_if_fail(dest != NULL, -1);
1362         g_return_val_if_fail(msglist != NULL, -1);
1363
1364         folder = dest->folder;
1365         if (dest->last_num < 0) folder->scan(folder, dest);
1366
1367         num = folder->move_msgs_with_dest(folder, dest, msglist);
1368         if (num > 0) dest->last_num = num;
1369         else dest->op_count = 0;
1370
1371         return num;
1372 }
1373 */
1374
1375 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1376 {
1377         Folder *folder;
1378         FolderItem *item;
1379         GSList *newmsgnums = NULL;
1380         GSList *l, *l2;
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         /* 
1392          * Copy messages to destination folder and 
1393          * store new message numbers in newmsgnums
1394          */
1395         item = NULL;
1396         for(l = msglist ; l != NULL ; l = g_slist_next(l)) {
1397                 MsgInfo * msginfo = (MsgInfo *) l->data;
1398
1399                 if (!item && msginfo->folder != NULL)
1400                         item = msginfo->folder;
1401
1402                 num = folder->copy_msg(folder, dest, msginfo);
1403                 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1404         }
1405
1406         /* Read cache for dest folder */
1407         if (!dest->cache) folder_item_read_cache(dest);
1408         
1409         /* 
1410          * Fetch new MsgInfos for new messages in dest folder,
1411          * add them to the msgcache and update folder message counts
1412          */
1413         l2 = newmsgnums;
1414         for(l = msglist; l != NULL; l = g_slist_next(l)) {
1415                 MsgInfo *msginfo = (MsgInfo *) l->data;
1416
1417                 num = GPOINTER_TO_INT(l2->data);
1418
1419                 if (num != -1) {
1420                         MsgInfo *newmsginfo;
1421
1422                         newmsginfo = folder->fetch_msginfo(folder, dest, num);
1423                         if(newmsginfo) {
1424                                 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1425                                 if (dest->stype == F_OUTBOX ||
1426                                     dest->stype == F_QUEUE  ||
1427                                     dest->stype == F_DRAFT  ||
1428                                     dest->stype == F_TRASH)
1429                                         MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1430                                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
1431                                 msgcache_add_msg(dest->cache, newmsginfo);
1432
1433                                 if (MSG_IS_NEW(newmsginfo->flags))
1434                                         dest->new++;
1435                                 if (MSG_IS_UNREAD(newmsginfo->flags))
1436                                         dest->unread++;
1437                                 dest->total++;
1438
1439                                 procmsg_msginfo_free(newmsginfo);
1440                         }
1441                 }
1442                 l2 = g_slist_next(l2);
1443         }
1444
1445         /*
1446          * Remove source messages from their folders if
1447          * copying was successfull and update folder
1448          * message counts
1449          */
1450         l2 = newmsgnums;
1451         for(l = msglist; l != NULL; l = g_slist_next(l)) {
1452                 MsgInfo *msginfo = (MsgInfo *) l->data;
1453
1454                 num = GPOINTER_TO_INT(l2->data);
1455                 
1456                 if(num != -1) {
1457                         item->folder->remove_msg(item->folder,
1458                                                  msginfo->folder,
1459                                                  msginfo->msgnum);
1460                         if(!item->cache)
1461                                 folder_item_read_cache(item);
1462                         msgcache_remove_msg(item->cache, msginfo->msgnum);
1463
1464                         if (MSG_IS_NEW(msginfo->flags))
1465                                 msginfo->folder->new--;
1466                         if (MSG_IS_UNREAD(msginfo->flags))
1467                                 msginfo->folder->unread--;
1468                         msginfo->folder->total--;                       
1469                 }
1470
1471                 l2 = g_slist_next(l2);
1472         }
1473
1474
1475         if (folder->finished_copy)
1476                 folder->finished_copy(folder, dest);
1477
1478         g_slist_free(newmsgnums);
1479         return dest->last_num;
1480 }
1481
1482 /*
1483 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1484 {
1485         Folder *folder;
1486         gint num;
1487
1488         g_return_val_if_fail(dest != NULL, -1);
1489         g_return_val_if_fail(msginfo != NULL, -1);
1490
1491         folder = dest->folder;
1492         if (dest->last_num < 0) folder->scan(folder, dest);
1493
1494         num = folder->copy_msg(folder, dest, msginfo);
1495         if (num > 0) dest->last_num = num;
1496
1497         return num;
1498 }
1499 */
1500
1501 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1502 {
1503         Folder *folder;
1504         gint num;
1505
1506         g_return_val_if_fail(dest != NULL, -1);
1507         g_return_val_if_fail(msginfo != NULL, -1);
1508
1509         folder = dest->folder;
1510
1511         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1512
1513         if (!dest->cache) folder_item_read_cache(dest);
1514         
1515         num = folder->copy_msg(folder, dest, msginfo);
1516         if (num != -1) {
1517                 MsgInfo *newmsginfo;
1518
1519                 newmsginfo = folder->fetch_msginfo(folder, dest, num);
1520                 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1521                 if (dest->stype == F_OUTBOX ||
1522                     dest->stype == F_QUEUE  ||
1523                     dest->stype == F_DRAFT  ||
1524                     dest->stype == F_TRASH)
1525                         MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1526                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
1527                 msgcache_add_msg(dest->cache, newmsginfo);
1528
1529                 if (MSG_IS_NEW(newmsginfo->flags))
1530                         dest->new++;
1531                 if (MSG_IS_UNREAD(newmsginfo->flags))
1532                         dest->unread++;
1533                 dest->total++;
1534
1535                 procmsg_msginfo_free(newmsginfo);
1536         }
1537
1538         if (folder->finished_copy)
1539                 folder->finished_copy(folder, dest);
1540
1541         return num;
1542 }
1543
1544 /*
1545 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1546 {
1547         Folder *folder;
1548         gint num;
1549
1550         g_return_val_if_fail(dest != NULL, -1);
1551         g_return_val_if_fail(msglist != NULL, -1);
1552
1553         folder = dest->folder;
1554         if (dest->last_num < 0) folder->scan(folder, dest);
1555
1556         num = folder->copy_msgs_with_dest(folder, dest, msglist);
1557         if (num > 0) dest->last_num = num;
1558         else dest->op_count = 0;
1559
1560         return num;
1561 }
1562 */
1563
1564 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1565 {
1566         Folder *folder;
1567         gint num;
1568         GSList *newmsgnums = NULL;
1569         GSList *l, *l2;
1570
1571         g_return_val_if_fail(dest != NULL, -1);
1572         g_return_val_if_fail(msglist != NULL, -1);
1573
1574         folder = dest->folder;
1575  
1576         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1577
1578         /* 
1579          * Copy messages to destination folder and 
1580          * store new message numbers in newmsgnums
1581          */
1582         for(l = msglist ; l != NULL ; l = g_slist_next(l)) {
1583                 MsgInfo * msginfo = (MsgInfo *) l->data;
1584
1585                 num = folder->copy_msg(folder, dest, msginfo);
1586                 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1587         }
1588
1589         /* Read cache for dest folder */
1590         if (!dest->cache) folder_item_read_cache(dest);
1591         
1592         /* 
1593          * Fetch new MsgInfos for new messages in dest folder,
1594          * add them to the msgcache and update folder message counts
1595          */
1596         l2 = newmsgnums;
1597         for(l = msglist; l != NULL; l = g_slist_next(l)) {
1598                 MsgInfo *msginfo = (MsgInfo *) l->data;
1599
1600                 num = GPOINTER_TO_INT(l2->data);
1601
1602                 if (num != -1) {
1603                         MsgInfo *newmsginfo;
1604
1605                         newmsginfo = folder->fetch_msginfo(folder, dest, num);
1606                         if(newmsginfo) {
1607                                 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1608                                 if (dest->stype == F_OUTBOX ||
1609                                     dest->stype == F_QUEUE  ||
1610                                     dest->stype == F_DRAFT  ||
1611                                     dest->stype == F_TRASH)
1612                                         MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1613                                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
1614                                 msgcache_add_msg(dest->cache, newmsginfo);
1615
1616                                 if (MSG_IS_NEW(newmsginfo->flags))
1617                                         dest->new++;
1618                                 if (MSG_IS_UNREAD(newmsginfo->flags))
1619                                         dest->unread++;
1620                                 dest->total++;
1621
1622                                 procmsg_msginfo_free(newmsginfo);
1623                         }
1624                 }
1625                 l2 = g_slist_next(l2);
1626         }
1627         
1628         if (folder->finished_copy)
1629                 folder->finished_copy(folder, dest);
1630
1631         g_slist_free(newmsgnums);
1632         return dest->last_num;
1633 }
1634
1635 gint folder_item_remove_msg(FolderItem *item, gint num)
1636 {
1637         Folder *folder;
1638         gint ret;
1639         MsgInfo *msginfo;
1640
1641         g_return_val_if_fail(item != NULL, -1);
1642
1643         folder = item->folder;
1644         if (!item->cache) folder_item_read_cache(item);
1645
1646         ret = folder->remove_msg(folder, item, num);
1647
1648         msginfo = msgcache_get_msg(item->cache, num);
1649         if(MSG_IS_NEW(msginfo->flags))
1650                 item->new--;
1651         if(MSG_IS_UNREAD(msginfo->flags))
1652                 item->unread--;
1653         item->total--;
1654         procmsg_msginfo_free(msginfo);
1655         msgcache_remove_msg(item->cache, num);
1656
1657         return ret;
1658 }
1659
1660 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
1661 {
1662         gint ret = 0;
1663
1664         g_return_val_if_fail(item != NULL, -1);
1665
1666         if (!item->cache) folder_item_read_cache(item);
1667
1668         while (msglist != NULL) {
1669                 MsgInfo *msginfo = (MsgInfo *)msglist->data;
1670
1671                 ret = folder_item_remove_msg(item, msginfo->msgnum);
1672                 if (ret != 0) break;
1673                 msgcache_remove_msg(item->cache, msginfo->msgnum);
1674                 msglist = msglist->next;
1675         }
1676
1677         return ret;
1678 }
1679
1680 gint folder_item_remove_all_msg(FolderItem *item)
1681 {
1682         Folder *folder;
1683         gint result;
1684
1685         g_return_val_if_fail(item != NULL, -1);
1686
1687         folder = item->folder;
1688
1689         g_return_val_if_fail(folder->remove_all_msg != NULL, -1);
1690
1691         result = folder->remove_all_msg(folder, item);
1692
1693         if (result == 0) {
1694                 if (folder->finished_remove)
1695                         folder->finished_remove(folder, item);
1696
1697                 folder_item_free_cache(item);
1698                 item->cache = msgcache_new();
1699
1700                 item->new = 0;
1701                 item->unread = 0;
1702                 item->total = 0;
1703         }
1704
1705         return result;
1706 }
1707
1708 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
1709 {
1710         Folder *folder;
1711
1712         g_return_val_if_fail(item != NULL, FALSE);
1713
1714         folder = item->folder;
1715
1716         g_return_val_if_fail(folder->is_msg_changed != NULL, -1);
1717
1718         return folder->is_msg_changed(folder, item, msginfo);
1719 }
1720
1721 gchar *folder_item_get_cache_file(FolderItem *item)
1722 {
1723         gchar *path;
1724         gchar *file;
1725
1726         g_return_val_if_fail(item != NULL, NULL);
1727         g_return_val_if_fail(item->path != NULL, NULL);
1728
1729         path = folder_item_get_path(item);
1730         g_return_val_if_fail(path != NULL, NULL);
1731         if (!is_dir_exist(path))
1732                 make_dir_hier(path);
1733         file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
1734         g_free(path);
1735
1736         return file;
1737 }
1738
1739 gchar *folder_item_get_mark_file(FolderItem *item)
1740 {
1741         gchar *path;
1742         gchar *file;
1743
1744         g_return_val_if_fail(item != NULL, NULL);
1745         g_return_val_if_fail(item->path != NULL, NULL);
1746
1747         path = folder_item_get_path(item);
1748         g_return_val_if_fail(path != NULL, NULL);
1749         if (!is_dir_exist(path))
1750                 make_dir_hier(path);
1751         file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
1752         g_free(path);
1753
1754         return file;
1755 }
1756
1757 static gboolean folder_build_tree(GNode *node, gpointer data)
1758 {
1759         Folder *folder = FOLDER(data);
1760         FolderItem *item;
1761         XMLNode *xmlnode;
1762         GList *list;
1763         SpecialFolderItemType stype = F_NORMAL;
1764         const gchar *name = NULL;
1765         const gchar *path = NULL;
1766         PrefsAccount *account = NULL;
1767         gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE, 
1768                  threaded = TRUE, ret_rcpt = FALSE, hidereadmsgs = FALSE;
1769         FolderSortKey sort_key = SORT_BY_NONE;
1770         FolderSortType sort_type = SORT_ASCENDING;
1771         gint new = 0, unread = 0, total = 0;
1772         time_t mtime = 0;
1773
1774         g_return_val_if_fail(node->data != NULL, FALSE);
1775         if (!node->parent) return FALSE;
1776
1777         xmlnode = node->data;
1778         if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
1779                 g_warning("tag name != \"folderitem\"\n");
1780                 return FALSE;
1781         }
1782
1783         list = xmlnode->tag->attr;
1784         for (; list != NULL; list = list->next) {
1785                 XMLAttr *attr = list->data;
1786
1787                 if (!attr || !attr->name || !attr->value) continue;
1788                 if (!strcmp(attr->name, "type")) {
1789                         if (!strcasecmp(attr->value, "normal"))
1790                                 stype = F_NORMAL;
1791                         else if (!strcasecmp(attr->value, "inbox"))
1792                                 stype = F_INBOX;
1793                         else if (!strcasecmp(attr->value, "outbox"))
1794                                 stype = F_OUTBOX;
1795                         else if (!strcasecmp(attr->value, "draft"))
1796                                 stype = F_DRAFT;
1797                         else if (!strcasecmp(attr->value, "queue"))
1798                                 stype = F_QUEUE;
1799                         else if (!strcasecmp(attr->value, "trash"))
1800                                 stype = F_TRASH;
1801                 } else if (!strcmp(attr->name, "name"))
1802                         name = attr->value;
1803                 else if (!strcmp(attr->name, "path"))
1804                         path = attr->value;
1805                 else if (!strcmp(attr->name, "account_id")) {
1806                         account = account_find_from_id(atoi(attr->value));
1807                         if (!account) g_warning("account_id: %s not found\n",
1808                                                 attr->value);
1809                 } else if (!strcmp(attr->name, "mtime"))
1810                         mtime = strtoul(attr->value, NULL, 10);
1811                 else if (!strcmp(attr->name, "new"))
1812                         new = atoi(attr->value);
1813                 else if (!strcmp(attr->name, "unread"))
1814                         unread = atoi(attr->value);
1815                 else if (!strcmp(attr->name, "total"))
1816                         total = atoi(attr->value);
1817                 else if (!strcmp(attr->name, "no_sub"))
1818                         no_sub = *attr->value == '1' ? TRUE : FALSE;
1819                 else if (!strcmp(attr->name, "no_select"))
1820                         no_select = *attr->value == '1' ? TRUE : FALSE;
1821                 else if (!strcmp(attr->name, "collapsed"))
1822                         collapsed = *attr->value == '1' ? TRUE : FALSE;
1823                 else if (!strcmp(attr->name, "threaded"))
1824                         threaded =  *attr->value == '1' ? TRUE : FALSE;
1825                 else if (!strcmp(attr->name, "hidereadmsgs"))
1826                         hidereadmsgs =  *attr->value == '1' ? TRUE : FALSE;
1827                 else if (!strcmp(attr->name, "reqretrcpt"))
1828                         ret_rcpt =  *attr->value == '1' ? TRUE : FALSE;
1829                 else if (!strcmp(attr->name, "sort_key")) {
1830                         if (!strcmp(attr->value, "none"))
1831                                 sort_key = SORT_BY_NONE;
1832                         else if (!strcmp(attr->value, "number"))
1833                                 sort_key = SORT_BY_NUMBER;
1834                         else if (!strcmp(attr->value, "size"))
1835                                 sort_key = SORT_BY_SIZE;
1836                         else if (!strcmp(attr->value, "date"))
1837                                 sort_key = SORT_BY_DATE;
1838                         else if (!strcmp(attr->value, "from"))
1839                                 sort_key = SORT_BY_FROM;
1840                         else if (!strcmp(attr->value, "subject"))
1841                                 sort_key = SORT_BY_SUBJECT;
1842                         else if (!strcmp(attr->value, "score"))
1843                                 sort_key = SORT_BY_SCORE;
1844                         else if (!strcmp(attr->value, "label"))
1845                                 sort_key = SORT_BY_LABEL;
1846                         else if (!strcmp(attr->value, "mark"))
1847                                 sort_key = SORT_BY_MARK;
1848                         else if (!strcmp(attr->value, "unread"))
1849                                 sort_key = SORT_BY_UNREAD;
1850                         else if (!strcmp(attr->value, "mime"))
1851                                 sort_key = SORT_BY_MIME;
1852                         else if (!strcmp(attr->value, "locked"))
1853                                 sort_key = SORT_BY_LOCKED;
1854                 } else if (!strcmp(attr->name, "sort_type")) {
1855                         if (!strcmp(attr->value, "ascending"))
1856                                 sort_type = SORT_ASCENDING;
1857                         else
1858                                 sort_type = SORT_DESCENDING;
1859                 }
1860         }
1861
1862         item = folder_item_new(name, path);
1863         item->stype = stype;
1864         item->account = account;
1865         item->mtime = mtime;
1866         item->new = new;
1867         item->unread = unread;
1868         item->total = total;
1869         item->no_sub = no_sub;
1870         item->no_select = no_select;
1871         item->collapsed = collapsed;
1872         item->threaded  = threaded;
1873         item->hide_read_msgs  = hidereadmsgs;
1874         item->ret_rcpt  = ret_rcpt;
1875         item->sort_key  = sort_key;
1876         item->sort_type = sort_type;
1877         item->parent = FOLDER_ITEM(node->parent->data);
1878         item->folder = folder;
1879         switch (stype) {
1880         case F_INBOX:  folder->inbox  = item; break;
1881         case F_OUTBOX: folder->outbox = item; break;
1882         case F_DRAFT:  folder->draft  = item; break;
1883         case F_QUEUE:  folder->queue  = item; break;
1884         case F_TRASH:  folder->trash  = item; break;
1885         default:       break;
1886         }
1887
1888         prefs_folder_item_read_config(item);
1889
1890         node->data = item;
1891         xml_free_node(xmlnode);
1892
1893         return FALSE;
1894 }
1895
1896 static gboolean folder_read_folder_func(GNode *node, gpointer data)
1897 {
1898         Folder *folder;
1899         XMLNode *xmlnode;
1900         GList *list;
1901         FolderType type = F_UNKNOWN;
1902         const gchar *name = NULL;
1903         const gchar *path = NULL;
1904         PrefsAccount *account = NULL;
1905         gboolean collapsed = FALSE, threaded = TRUE, ret_rcpt = FALSE;
1906
1907         if (g_node_depth(node) != 2) return FALSE;
1908         g_return_val_if_fail(node->data != NULL, FALSE);
1909
1910         xmlnode = node->data;
1911         if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
1912                 g_warning("tag name != \"folder\"\n");
1913                 return TRUE;
1914         }
1915         g_node_unlink(node);
1916         list = xmlnode->tag->attr;
1917         for (; list != NULL; list = list->next) {
1918                 XMLAttr *attr = list->data;
1919
1920                 if (!attr || !attr->name || !attr->value) continue;
1921                 if (!strcmp(attr->name, "type")) {
1922                         if (!strcasecmp(attr->value, "mh"))
1923                                 type = F_MH;
1924                         else if (!strcasecmp(attr->value, "mbox"))
1925                                 type = F_MBOX;
1926                         else if (!strcasecmp(attr->value, "maildir"))
1927                                 type = F_MAILDIR;
1928                         else if (!strcasecmp(attr->value, "imap"))
1929                                 type = F_IMAP;
1930                         else if (!strcasecmp(attr->value, "news"))
1931                                 type = F_NEWS;
1932                 } else if (!strcmp(attr->name, "name"))
1933                         name = attr->value;
1934                 else if (!strcmp(attr->name, "path"))
1935                         path = attr->value;
1936                 else if (!strcmp(attr->name, "account_id")) {
1937                         account = account_find_from_id(atoi(attr->value));
1938                         if (!account) g_warning("account_id: %s not found\n",
1939                                                 attr->value);
1940                 } else if (!strcmp(attr->name, "collapsed"))
1941                         collapsed = *attr->value == '1' ? TRUE : FALSE;
1942                 else if (!strcmp(attr->name, "threaded"))
1943                         threaded = *attr->value == '1' ? TRUE : FALSE;
1944                 else if (!strcmp(attr->name, "reqretrcpt"))
1945                         ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
1946         }
1947
1948         folder = folder_new(type, name, path);
1949         g_return_val_if_fail(folder != NULL, FALSE);
1950         folder->account = account;
1951         if (account && (type == F_IMAP || type == F_NEWS))
1952                 account->folder = REMOTE_FOLDER(folder);
1953         node->data = folder->node->data;
1954         g_node_destroy(folder->node);
1955         folder->node = node;
1956         folder_add(folder);
1957         FOLDER_ITEM(node->data)->collapsed = collapsed;
1958         FOLDER_ITEM(node->data)->threaded  = threaded;
1959         FOLDER_ITEM(node->data)->ret_rcpt  = ret_rcpt;
1960
1961         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1962                         folder_build_tree, folder);
1963
1964         return FALSE;
1965 }
1966
1967 static gchar *folder_get_list_path(void)
1968 {
1969         static gchar *filename = NULL;
1970
1971         if (!filename)
1972                 filename =  g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1973                                         FOLDER_LIST, NULL);
1974
1975         return filename;
1976 }
1977
1978 static void folder_write_list_recursive(GNode *node, gpointer data)
1979 {
1980         FILE *fp = (FILE *)data;
1981         FolderItem *item = FOLDER_ITEM(node->data);
1982         gint i, depth;
1983         static gchar *folder_type_str[] = {"mh", "mbox", "maildir", "imap",
1984                                            "news", "unknown"};
1985         static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
1986                                                  "draft", "queue", "trash"};
1987         static gchar *sort_key_str[] = {"none", "number", "size", "date",
1988                                         "from", "subject", "score", "label",
1989                                         "mark", "unread", "mime", "locked" };
1990
1991         g_return_if_fail(item != NULL);
1992
1993         depth = g_node_depth(node);
1994         for (i = 0; i < depth; i++)
1995                 fputs("    ", fp);
1996         if (depth == 1) {
1997                 Folder *folder = item->folder;
1998
1999                 fprintf(fp, "<folder type=\"%s\"", folder_type_str[folder->type]);
2000                 if (folder->name) {
2001                         fputs(" name=\"", fp);
2002                         xml_file_put_escape_str(fp, folder->name);
2003                         fputs("\"", fp);
2004                 }
2005                 if ((folder->type == F_MH) || (folder->type == F_MBOX)) {
2006                         fputs(" path=\"", fp);
2007                         xml_file_put_escape_str
2008                                 (fp, LOCAL_FOLDER(folder)->rootpath);
2009                         fputs("\"", fp);
2010                 }
2011                 if (folder->account)
2012                         fprintf(fp, " account_id=\"%d\"",
2013                                 folder->account->account_id);
2014                 if (item->collapsed && node->children)
2015                         fputs(" collapsed=\"1\"", fp);
2016                 if (item->ret_rcpt) 
2017                         fputs(" reqretrcpt=\"1\"", fp);
2018         } else {
2019                 fprintf(fp, "<folderitem type=\"%s\"",
2020                         folder_item_stype_str[item->stype]);
2021                 if (item->name) {
2022                         fputs(" name=\"", fp);
2023                         xml_file_put_escape_str(fp, item->name);
2024                         fputs("\"", fp);
2025                 }
2026                 if (item->path) {
2027                         fputs(" path=\"", fp);
2028                         xml_file_put_escape_str(fp, item->path);
2029                         fputs("\"", fp);
2030                 }
2031                 if (item->account)
2032                         fprintf(fp, " account_id=\"%d\"",
2033                                 item->account->account_id);
2034                 if (item->no_sub)
2035                         fputs(" no_sub=\"1\"", fp);
2036                 if (item->no_select)
2037                         fputs(" no_select=\"1\"", fp);
2038                 if (item->collapsed && node->children)
2039                         fputs(" collapsed=\"1\"", fp);
2040                 if (item->threaded)
2041                         fputs(" threaded=\"1\"", fp);
2042                 else
2043                         fputs(" threaded=\"0\"", fp);
2044                 if (item->hide_read_msgs)
2045                         fputs(" hidereadmsgs=\"1\"", fp);
2046                 else
2047                         fputs(" hidereadmsgs=\"0\"", fp);
2048                 if (item->ret_rcpt)
2049                         fputs(" reqretrcpt=\"1\"", fp);
2050
2051                 if (item->sort_key != SORT_BY_NONE) {
2052                         fprintf(fp, " sort_key=\"%s\"",
2053                                 sort_key_str[item->sort_key]);
2054                         if (item->sort_type == SORT_ASCENDING)
2055                                 fprintf(fp, " sort_type=\"ascending\"");
2056                         else
2057                                 fprintf(fp, " sort_type=\"descending\"");
2058                 }
2059
2060                 fprintf(fp,
2061                         " mtime=\"%lu\" new=\"%d\" unread=\"%d\" total=\"%d\"",
2062                         item->mtime, item->new, item->unread, item->total);
2063         }
2064
2065         if (node->children) {
2066                 GNode *child;
2067                 fputs(">\n", fp);
2068
2069                 child = node->children;
2070                 while (child) {
2071                         GNode *cur;
2072
2073                         cur = child;
2074                         child = cur->next;
2075                         folder_write_list_recursive(cur, data);
2076                 }
2077
2078                 for (i = 0; i < depth; i++)
2079                         fputs("    ", fp);
2080                 fprintf(fp, "</%s>\n", depth == 1 ? "folder" : "folderitem");
2081         } else
2082                 fputs(" />\n", fp);
2083 }
2084
2085 static void folder_update_op_count_rec(GNode *node) {
2086         FolderItem *fitem = FOLDER_ITEM(node->data);
2087
2088         if (g_node_depth(node) > 0) {
2089                 if (fitem->op_count > 0) {
2090                         fitem->op_count = 0;
2091                         folderview_update_item(fitem, 0);
2092                 }
2093                 if (node->children) {
2094                         GNode *child;
2095
2096                         child = node->children;
2097                         while (child) {
2098                                 GNode *cur;
2099
2100                                 cur = child;
2101                                 child = cur->next;
2102                                 folder_update_op_count_rec(cur);
2103                         }
2104                 }
2105         }
2106 }
2107
2108 void folder_update_op_count() {
2109         GList *cur;
2110         Folder *folder;
2111
2112         for (cur = folder_list; cur != NULL; cur = cur->next) {
2113                 folder = cur->data;
2114                 folder_update_op_count_rec(folder->node);
2115         }
2116 }
2117
2118 typedef struct _type_str {
2119         gchar * str;
2120         gint type;
2121 } type_str;
2122
2123
2124 /*
2125 static gchar * folder_item_get_tree_identifier(FolderItem * item)
2126 {
2127         if (item->parent != NULL) {
2128                 gchar * path;
2129                 gchar * id;
2130
2131                 path = folder_item_get_tree_identifier(item->parent);
2132                 if (path == NULL)
2133                         return NULL;
2134
2135                 id = g_strconcat(path, "/", item->name, NULL);
2136                 g_free(path);
2137
2138                 return id;
2139         }
2140         else {
2141                 return g_strconcat("/", item->name, NULL);
2142         }
2143 }
2144 */
2145
2146 /* CLAWS: temporary local folder for filtering */
2147 static Folder *processing_folder;
2148 static FolderItem *processing_folder_item;
2149
2150 static void folder_create_processing_folder(void)
2151 {
2152 #define PROCESSING_FOLDER ".processing" 
2153         Folder     *tmpparent;
2154         gchar      *tmpname;
2155
2156         tmpparent = folder_get_default_folder();
2157         g_assert(tmpparent);
2158         debug_print("tmpparentroot %s\n", LOCAL_FOLDER(tmpparent)->rootpath);
2159         if (LOCAL_FOLDER(tmpparent)->rootpath[0] == '/')
2160                 tmpname = g_strconcat(LOCAL_FOLDER(tmpparent)->rootpath,
2161                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2162                                       NULL);
2163         else
2164                 tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2165                                       LOCAL_FOLDER(tmpparent)->rootpath,
2166                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2167                                       NULL);
2168
2169         processing_folder = folder_new(F_MH, "PROCESSING", LOCAL_FOLDER(tmpparent)->rootpath);
2170         g_assert(processing_folder);
2171
2172         if (!is_dir_exist(tmpname)) {
2173                 debug_print("*TMP* creating %s\n", tmpname);
2174                 processing_folder_item = processing_folder->create_folder(processing_folder,
2175                                                                           processing_folder->node->data,
2176                                                                           PROCESSING_FOLDER);
2177                 g_assert(processing_folder_item);                                                                         
2178         }
2179         else {
2180                 debug_print("*TMP* already created\n");
2181                 processing_folder_item = folder_item_new(".processing", ".processing");
2182                 g_assert(processing_folder_item);
2183                 folder_item_append(processing_folder->node->data, processing_folder_item);
2184         }
2185         g_free(tmpname);
2186 }
2187
2188 FolderItem *folder_get_default_processing(void)
2189 {
2190         if (!processing_folder_item) {
2191                 folder_create_processing_folder();
2192         }
2193         return processing_folder_item;
2194 }
2195
2196 /* folder_persist_prefs_new() - return hash table with persistent
2197  * settings (and folder name as key). 
2198  * (note that in claws other options are in the PREFS_FOLDER_ITEM_RC
2199  * file, so those don't need to be included in PersistPref yet) 
2200  */
2201 GHashTable *folder_persist_prefs_new(Folder *folder)
2202 {
2203         GHashTable *pptable;
2204
2205         g_return_val_if_fail(folder, NULL);
2206         pptable = g_hash_table_new(g_str_hash, g_str_equal);
2207         folder_get_persist_prefs_recursive(folder->node, pptable);
2208         return pptable;
2209 }
2210
2211 void folder_persist_prefs_free(GHashTable *pptable)
2212 {
2213         g_return_if_fail(pptable);
2214         g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
2215         g_hash_table_destroy(pptable);
2216 }
2217
2218 const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
2219 {
2220         if (pptable == NULL || name == NULL) return NULL;
2221         return g_hash_table_lookup(pptable, name);
2222 }
2223
2224 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
2225 {
2226         const PersistPrefs *pp;
2227
2228         pp = folder_get_persist_prefs(pptable, item->path); 
2229         if (!pp) return;
2230
2231         /* CLAWS: since not all folder properties have been migrated to 
2232          * folderlist.xml, we need to call the old stuff first before
2233          * setting things that apply both to Main and Claws. */
2234         prefs_folder_item_read_config(item); 
2235          
2236         item->collapsed = pp->collapsed;
2237         item->threaded  = pp->threaded;
2238         item->ret_rcpt  = pp->ret_rcpt;
2239         item->hide_read_msgs = pp->hide_read_msgs;
2240         item->sort_key  = pp->sort_key;
2241         item->sort_type = pp->sort_type;
2242 }
2243
2244 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
2245 {
2246         FolderItem *item = FOLDER_ITEM(node->data);
2247         PersistPrefs *pp;
2248         GNode *child, *cur;
2249
2250         g_return_if_fail(node != NULL);
2251         g_return_if_fail(item != NULL);
2252
2253         /* FIXME: item->path == NULL for top level folder, so this means that 
2254          * properties of MH folder root will not be stored. Not quite important, 
2255          * because the top level folder properties are not special anyway. */
2256         if (item->path) {
2257                 pp = g_new0(PersistPrefs, 1);
2258                 g_return_if_fail(pp != NULL);
2259                 pp->collapsed = item->collapsed;
2260                 pp->threaded  = item->threaded;
2261                 pp->ret_rcpt  = item->ret_rcpt; 
2262                 pp->hide_read_msgs = item->hide_read_msgs;
2263                 pp->sort_key  = item->sort_key;
2264                 pp->sort_type = item->sort_type;
2265                 g_hash_table_insert(pptable, g_strdup(item->path), pp);
2266         }               
2267
2268         if (node->children) {
2269                 child = node->children;
2270                 while (child) {
2271                         cur = child;
2272                         child = cur->next;
2273                         folder_get_persist_prefs_recursive(cur, pptable);
2274                 }
2275         }       
2276 }
2277
2278 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
2279 {
2280         if (key) 
2281                 g_free(key);
2282         if (val) 
2283                 g_free(val);
2284         return TRUE;    
2285 }
2286
2287 void folder_item_apply_processing(FolderItem *item)
2288 {
2289         GSList *processing_list;
2290         GSList *mlist, *cur;
2291         GHashTable *folder_table;
2292         
2293         g_return_if_fail(item != NULL);
2294         
2295         processing_list = item->prefs->processing;
2296         if (processing_list == NULL)
2297                 return;
2298         folder_table = g_hash_table_new(NULL, NULL);
2299
2300         mlist = folder_item_get_msg_list(item);
2301         
2302         for(cur = mlist ; cur != NULL ; cur = cur->next) {
2303                 MsgInfo * msginfo;
2304
2305                 msginfo = (MsgInfo *) cur->data;
2306                 filter_message_by_msginfo(processing_list, msginfo,
2307                                           folder_table);
2308                 procmsg_msginfo_free(msginfo);
2309         }
2310         
2311         /* folder_item_scan_foreach(summaryview->folder_table); */
2312         folderview_update_item_foreach(folder_table);
2313
2314         g_slist_free(mlist);
2315         
2316         g_hash_table_destroy(folder_table);
2317 }