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