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