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