sync with 0.9.2cvs7
[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         folder_item_update_freeze();
1242         for (elem = exists_list; elem != NULL; elem = g_slist_next(elem)) {
1243                 MsgInfo *msginfo;
1244
1245                 msginfo = elem->data;
1246                 if (MSG_IS_IGNORE_THREAD(msginfo->flags) && (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1247                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1248                 if (!MSG_IS_IGNORE_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD)) {
1249                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1250                         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
1251                 }
1252                 if ((item->stype == F_OUTBOX ||
1253                      item->stype == F_QUEUE  ||
1254                      item->stype == F_DRAFT  ||
1255                      item->stype == F_TRASH) &&
1256                     (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1257                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1258                 if (MSG_IS_NEW(msginfo->flags))
1259                         newcnt++;
1260                 if (MSG_IS_UNREAD(msginfo->flags))
1261                         unreadcnt++;
1262                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1263                         unreadmarkedcnt++;
1264                 totalcnt++;
1265
1266                 procmsg_msginfo_free(msginfo);
1267         }
1268         g_slist_free(exists_list);
1269
1270         item->new_msgs = newcnt;
1271         item->unread_msgs = unreadcnt;
1272         item->total_msgs = totalcnt;
1273         item->unreadmarked_msgs = unreadmarkedcnt;
1274
1275         update_flags |= F_ITEM_UPDATE_MSGCNT;
1276
1277         folder_item_update(item, update_flags);
1278         folder_item_update_thaw();
1279
1280         return 0;
1281 }
1282
1283 gint folder_item_scan(FolderItem *item)
1284 {
1285         return folder_item_scan_full(item, TRUE);
1286 }
1287
1288 static gboolean folder_scan_all_items_func(GNode *node, gpointer data)
1289 {
1290         FolderItem *item = node->data;
1291
1292         folder_item_scan(item);
1293
1294         return FALSE;
1295 }
1296
1297 void folder_scan_all_items(Folder * folder)
1298 {
1299         g_node_traverse(folder->node, G_PRE_ORDER,
1300                         G_TRAVERSE_ALL, -1, folder_scan_all_items_func, NULL);
1301 }
1302
1303 static void folder_item_scan_foreach_func(gpointer key, gpointer val,
1304                                           gpointer data)
1305 {
1306         folder_item_scan(FOLDER_ITEM(key));
1307 }
1308
1309 void folder_item_scan_foreach(GHashTable *table)
1310 {
1311         g_hash_table_foreach(table, folder_item_scan_foreach_func, NULL);
1312 }
1313
1314 void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
1315 {
1316         gint *memusage = (gint *)data;
1317
1318         if (item->cache == NULL)
1319                 return;
1320         
1321         *memusage += msgcache_get_memory_usage(item->cache);
1322 }
1323
1324 gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
1325 {
1326         FolderItem *fa = (FolderItem *)a;
1327         FolderItem *fb = (FolderItem *)b;
1328         
1329         return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
1330 }
1331
1332 void folder_find_expired_caches(FolderItem *item, gpointer data)
1333 {
1334         GSList **folder_item_list = (GSList **)data;
1335         gint difftime, expiretime;
1336         
1337         if (item->cache == NULL)
1338                 return;
1339
1340         if (item->opened > 0)
1341                 return;
1342
1343         difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
1344         expiretime = prefs_common.cache_min_keep_time * 60;
1345         debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
1346         if (difftime > expiretime) {
1347                 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
1348         }
1349 }
1350
1351 void folder_item_free_cache(FolderItem *item)
1352 {
1353         g_return_if_fail(item != NULL);
1354         
1355         if (item->cache == NULL)
1356                 return;
1357         
1358         if (item->opened > 0)
1359                 return;
1360
1361         folder_item_write_cache(item);
1362         msgcache_destroy(item->cache);
1363         item->cache = NULL;
1364 }
1365
1366 void folder_clean_cache_memory(void)
1367 {
1368         gint memusage = 0;
1369
1370         folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);       
1371         debug_print("Total cache memory usage: %d\n", memusage);
1372         
1373         if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
1374                 GSList *folder_item_list = NULL, *listitem;
1375                 
1376                 debug_print("Trying to free cache memory\n");
1377
1378                 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);      
1379                 listitem = folder_item_list;
1380                 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
1381                         FolderItem *item = (FolderItem *)(listitem->data);
1382
1383                         debug_print("Freeing cache memory for %s\n", item->path);
1384                         memusage -= msgcache_get_memory_usage(item->cache);
1385                         folder_item_free_cache(item);
1386                         listitem = listitem->next;
1387                 }
1388                 g_slist_free(folder_item_list);
1389         }
1390 }
1391
1392 void folder_item_read_cache(FolderItem *item)
1393 {
1394         gchar *cache_file, *mark_file;
1395         
1396         g_return_if_fail(item != NULL);
1397
1398         cache_file = folder_item_get_cache_file(item);
1399         mark_file = folder_item_get_mark_file(item);
1400         item->cache = msgcache_read_cache(item, cache_file);
1401         if (!item->cache) {
1402                 item->cache = msgcache_new();
1403                 folder_item_scan_full(item, TRUE);
1404         }
1405         msgcache_read_mark(item->cache, mark_file);
1406         g_free(cache_file);
1407         g_free(mark_file);
1408
1409         folder_clean_cache_memory();
1410 }
1411
1412 void folder_item_write_cache(FolderItem *item)
1413 {
1414         gchar *cache_file, *mark_file;
1415         PrefsFolderItem *prefs;
1416         gint filemode = 0;
1417         gchar *id;
1418         
1419         if (!item || !item->path || !item->cache)
1420                 return;
1421
1422         id = folder_item_get_identifier(item);
1423         debug_print("Save cache for folder %s\n", id);
1424         g_free(id);
1425
1426         cache_file = folder_item_get_cache_file(item);
1427         mark_file = folder_item_get_mark_file(item);
1428         if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
1429                 prefs = item->prefs;
1430                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
1431                         /* for cache file */
1432                         filemode = prefs->folder_chmod;
1433                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
1434                         if (filemode & S_IROTH) filemode |= S_IWOTH;
1435                         chmod(cache_file, filemode);
1436                 }
1437         }
1438
1439         g_free(cache_file);
1440         g_free(mark_file);
1441 }
1442
1443 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
1444 {
1445         Folder *folder;
1446         MsgInfo *msginfo;
1447         
1448         g_return_val_if_fail(item != NULL, NULL);
1449         
1450         folder = item->folder;
1451         if (!item->cache)
1452                 folder_item_read_cache(item);
1453         
1454         if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
1455                 return msginfo;
1456         
1457         g_return_val_if_fail(folder->klass->get_msginfo, NULL);
1458         if ((msginfo = folder->klass->get_msginfo(folder, item, num)) != NULL) {
1459                 msgcache_add_msg(item->cache, msginfo);
1460                 return msginfo;
1461         }
1462         
1463         return NULL;
1464 }
1465
1466 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
1467 {
1468         Folder *folder;
1469         MsgInfo *msginfo;
1470         
1471         g_return_val_if_fail(item != NULL, NULL);
1472         
1473         folder = item->folder;
1474         if (!item->cache)
1475                 folder_item_read_cache(item);
1476         
1477         if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
1478                 return msginfo;
1479
1480         return NULL;
1481 }
1482
1483 GSList *folder_item_get_msg_list(FolderItem *item)
1484 {
1485         g_return_val_if_fail(item != NULL, NULL);
1486         
1487         if (item->cache == 0)
1488                 folder_item_read_cache(item);
1489
1490         g_return_val_if_fail(item->cache != NULL, NULL);
1491         
1492         return msgcache_get_msg_list(item->cache);
1493 }
1494
1495 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
1496 {
1497         Folder *folder;
1498
1499         g_return_val_if_fail(item != NULL, NULL);
1500
1501         folder = item->folder;
1502
1503         g_return_val_if_fail(folder->klass->fetch_msg != NULL, NULL);
1504
1505         return folder->klass->fetch_msg(folder, item, num);
1506 }
1507
1508 static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
1509 {
1510         static HeaderEntry hentry[] = {{"Message-ID:",  NULL, TRUE},
1511                                        {NULL,           NULL, FALSE}};
1512         FILE *fp;
1513         MsgInfo *msginfo;
1514         gint msgnum = 0;
1515         gchar buf[BUFFSIZE];
1516
1517         if ((fp = fopen(file, "rb")) == NULL)
1518                 return 0;
1519
1520         if ((dest->stype == F_QUEUE) || (dest->stype == F_DRAFT))
1521                 while (fgets(buf, sizeof(buf), fp) != NULL)
1522                         if (buf[0] == '\r' || buf[0] == '\n') break;
1523
1524         procheader_get_header_fields(fp, hentry);
1525         if (hentry[0].body) {
1526                 extract_parenthesis(hentry[0].body, '<', '>');
1527                 remove_space(hentry[0].body);
1528                 if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
1529                         msgnum = msginfo->msgnum;
1530                         procmsg_msginfo_free(msginfo);
1531
1532                         debug_print("found message as uid %d\n", msgnum);
1533                 }
1534         }
1535         
1536         g_free(hentry[0].body);
1537         hentry[0].body = NULL;
1538         fclose(fp);
1539
1540         return msgnum;
1541 }
1542
1543 static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
1544 {
1545         MsgPermFlags perm_flags = 0;
1546         MsgTmpFlags tmp_flags = 0;
1547
1548         /* create new flags */
1549         if (source != NULL) {
1550                 /* copy original flags */
1551                 perm_flags = source->flags.perm_flags;
1552                 tmp_flags = source->flags.tmp_flags;
1553         } else {
1554                 perm_flags = dest->flags.perm_flags;
1555                 tmp_flags = dest->flags.tmp_flags;
1556         }
1557
1558         /* remove new, unread and deleted in special folders */
1559         if (dest->folder->stype == F_OUTBOX ||
1560             dest->folder->stype == F_QUEUE  ||
1561             dest->folder->stype == F_DRAFT  ||
1562             dest->folder->stype == F_TRASH)
1563                 perm_flags &= ~(MSG_NEW | MSG_UNREAD | MSG_DELETED);
1564
1565         /* set ignore flag of ignored parent exists */
1566         if (procmsg_msg_has_flagged_parent(dest, MSG_IGNORE_THREAD))
1567                 perm_flags |= MSG_IGNORE_THREAD;
1568
1569         /* Unset tmp flags that should not be copied */
1570         tmp_flags &= ~(MSG_MOVE | MSG_COPY);
1571
1572         /* unset flags that are set but should not */
1573         procmsg_msginfo_unset_flags(dest,
1574                                     dest->flags.perm_flags & ~perm_flags,
1575                                     dest->flags.tmp_flags  & ~tmp_flags);
1576         /* set new flags */
1577         procmsg_msginfo_set_flags(dest,
1578                                   ~dest->flags.perm_flags & perm_flags,
1579                                   ~dest->flags.tmp_flags  & tmp_flags);
1580
1581         folder_item_update(dest->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1582 }
1583
1584 static void add_msginfo_to_cache(FolderItem *item, MsgInfo *newmsginfo, MsgInfo *flagsource)
1585 {
1586         /* update folder stats */
1587         if (MSG_IS_NEW(newmsginfo->flags))
1588                 item->new_msgs++;
1589         if (MSG_IS_UNREAD(newmsginfo->flags))
1590                 item->unread_msgs++;
1591         if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
1592                 item->unreadmarked_msgs++;
1593         item->total_msgs++;
1594
1595         copy_msginfo_flags(flagsource, newmsginfo);
1596
1597         msgcache_add_msg(item->cache, newmsginfo);
1598 }
1599
1600 static void remove_msginfo_from_cache(FolderItem *item, MsgInfo *msginfo)
1601 {
1602         if (!item->cache)
1603             folder_item_read_cache(item);
1604
1605         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1606                 msginfo->folder->new_msgs--;
1607         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1608                 msginfo->folder->unread_msgs--;
1609         if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1610                 msginfo->folder->unreadmarked_msgs--;
1611         msginfo->folder->total_msgs--;
1612
1613         msgcache_remove_msg(item->cache, msginfo->msgnum);
1614         folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1615 }
1616
1617 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
1618                          gboolean remove_source)
1619 {
1620         Folder *folder;
1621         gint num;
1622         MsgInfo *msginfo;
1623
1624         g_return_val_if_fail(dest != NULL, -1);
1625         g_return_val_if_fail(file != NULL, -1);
1626
1627         folder = dest->folder;
1628
1629         g_return_val_if_fail(folder->klass->add_msg != NULL, -1);
1630
1631         if (!dest->cache)
1632                 folder_item_read_cache(dest);
1633
1634         num = folder->klass->add_msg(folder, dest, file, FALSE);
1635
1636         if (num > 0) {
1637                 msginfo = folder->klass->get_msginfo(folder, dest, num);
1638
1639                 if (msginfo != NULL) {
1640                         add_msginfo_to_cache(dest, msginfo, NULL);
1641                         procmsg_msginfo_free(msginfo);
1642                         folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1643                 }
1644
1645                 dest->last_num = num;
1646         } else if (num == 0) {
1647                 folder_item_scan_full(dest, FALSE);
1648                 num = folder_item_get_msg_num_by_file(dest, file);
1649         }
1650
1651         if (num >= 0 && remove_source) {
1652                 if (unlink(file) < 0)
1653                         FILE_OP_ERROR(file, "unlink");
1654         }
1655
1656         return num;
1657 }
1658
1659 /*
1660 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1661 {
1662         Folder *folder;
1663         gint num;
1664
1665         g_return_val_if_fail(dest != NULL, -1);
1666         g_return_val_if_fail(msginfo != NULL, -1);
1667
1668         folder = dest->folder;
1669         if (dest->last_num < 0) folder->scan(folder, dest);
1670
1671         num = folder->move_msg(folder, dest, msginfo);
1672         if (num > 0) dest->last_num = num;
1673
1674         return num;
1675 }
1676 */
1677                 
1678 FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest) 
1679 {
1680         GSList *mlist;
1681         FolderItem *new_item;
1682         FolderItem *next_item;
1683         GNode *srcnode;
1684         gchar *old_id, *new_id;
1685
1686         mlist = folder_item_get_msg_list(src);
1687
1688         /* move messages */
1689         debug_print("Moving %s to %s\n", src->path, dest->path);
1690         new_item = folder_create_folder(dest, g_basename(src->path));
1691         if (new_item == NULL) {
1692                 printf("Can't create folder\n");
1693                 return NULL;
1694         }
1695         
1696         if (new_item->folder == NULL)
1697                 new_item->folder = dest->folder;
1698
1699         /* move messages */
1700         log_message(_("Moving %s to %s...\n"), 
1701                         src->name, new_item->path);
1702         folder_item_move_msgs_with_dest(new_item, mlist);
1703         
1704         /*copy prefs*/
1705         prefs_folder_item_copy_prefs(src, new_item);
1706         new_item->collapsed = src->collapsed;
1707         new_item->thread_collapsed = src->thread_collapsed;
1708         new_item->threaded  = src->threaded;
1709         new_item->ret_rcpt  = src->ret_rcpt;
1710         new_item->hide_read_msgs = src->hide_read_msgs;
1711         new_item->sort_key  = src->sort_key;
1712         new_item->sort_type = src->sort_type;
1713
1714         prefs_matcher_write_config();
1715         
1716         /* recurse */
1717         srcnode = src->folder->node;    
1718         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
1719         srcnode = srcnode->children;
1720         while (srcnode != NULL) {
1721                 if (srcnode && srcnode->data) {
1722                         next_item = (FolderItem*) srcnode->data;
1723                         srcnode = srcnode->next;
1724                         if (folder_item_move_recursive(next_item, new_item) == NULL)
1725                                 return NULL;
1726                 }
1727         }
1728         old_id = folder_item_get_identifier(src);
1729         new_id = folder_item_get_identifier(new_item);
1730         debug_print("updating rules : %s => %s\n", old_id, new_id);
1731         
1732         src->folder->klass->remove_folder(src->folder, src);
1733         folder_write_list();
1734
1735         if (old_id != NULL && new_id != NULL)
1736                 prefs_filtering_rename_path(old_id, new_id);
1737         g_free(old_id);
1738         g_free(new_id);
1739
1740         return new_item;
1741 }
1742
1743 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item)
1744 {
1745         FolderItem *tmp = dest->parent;
1746         gchar * src_identifier, * dst_identifier;
1747         gchar * phys_srcpath, * phys_dstpath;
1748         GNode *src_node;
1749         
1750         while (tmp) {
1751                 if (tmp == src) {
1752                         return F_MOVE_FAILED_DEST_IS_CHILD;
1753                 }
1754                 tmp = tmp->parent;
1755         }
1756         
1757         tmp = src->parent;
1758         
1759         src_identifier = folder_item_get_identifier(src);
1760         dst_identifier = folder_item_get_identifier(dest);
1761         
1762         if(dst_identifier == NULL && dest->folder && dest->parent == NULL) {
1763                 /* dest can be a root folder */
1764                 dst_identifier = folder_get_identifier(dest->folder);
1765         }
1766         if (src_identifier == NULL || dst_identifier == NULL) {
1767                 debug_print("Can't get identifiers\n");
1768                 return F_MOVE_FAILED;
1769         }
1770
1771         if (src->folder != dest->folder) {
1772                 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
1773         }
1774
1775         phys_srcpath = folder_item_get_path(src);
1776         phys_dstpath = g_strconcat(folder_item_get_path(dest),G_DIR_SEPARATOR_S,g_basename(phys_srcpath),NULL);
1777
1778         if (src->parent == dest || src == dest) {
1779                 g_free(src_identifier);
1780                 g_free(dst_identifier);
1781                 g_free(phys_srcpath);
1782                 g_free(phys_dstpath);
1783                 return F_MOVE_FAILED_DEST_IS_PARENT;
1784         }
1785         debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
1786         if ((tmp = folder_item_move_recursive(src, dest)) == NULL) {
1787                 return F_MOVE_FAILED;
1788         }
1789         
1790         /* update rules */
1791         src_node = g_node_find(src->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, src);
1792         if (src_node) 
1793                 g_node_destroy(src_node);
1794         else
1795                 debug_print("can't remove node: it's null!\n");
1796         /* not to much worry if remove fails, move has been done */
1797         
1798         g_free(src_identifier);
1799         g_free(dst_identifier);
1800         g_free(phys_srcpath);
1801         g_free(phys_dstpath);
1802
1803         *new_item = tmp;
1804
1805         return F_MOVE_OK;
1806 }
1807
1808 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1809 {
1810         GSList *list = NULL;
1811         gint ret;
1812
1813         list = g_slist_append(list, msginfo);
1814         ret = folder_item_move_msgs_with_dest(dest, list);
1815         g_slist_free(list);
1816         
1817         return ret;
1818 }
1819
1820 /*
1821 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1822 {
1823         Folder *folder;
1824         gint num;
1825
1826         g_return_val_if_fail(dest != NULL, -1);
1827         g_return_val_if_fail(msglist != NULL, -1);
1828
1829         folder = dest->folder;
1830         if (dest->last_num < 0) folder->scan(folder, dest);
1831
1832         num = folder->move_msgs_with_dest(folder, dest, msglist);
1833         if (num > 0) dest->last_num = num;
1834         else dest->op_count = 0;
1835
1836         return num;
1837 }
1838 */
1839
1840
1841
1842 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1843 {
1844         Folder *folder;
1845         FolderItem *item;
1846         GSList *newmsgnums = NULL;
1847         GSList *l, *l2;
1848         gint num, lastnum = -1;
1849         gboolean folderscan = FALSE;
1850
1851         g_return_val_if_fail(dest != NULL, -1);
1852         g_return_val_if_fail(msglist != NULL, -1);
1853
1854         folder = dest->folder;
1855
1856         g_return_val_if_fail(folder->klass->copy_msg != NULL, -1);
1857         g_return_val_if_fail(folder->klass->remove_msg != NULL, -1);
1858
1859         /* 
1860          * Copy messages to destination folder and 
1861          * store new message numbers in newmsgnums
1862          */
1863         item = NULL;
1864         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
1865                 MsgInfo * msginfo = (MsgInfo *) l->data;
1866
1867                 if (!item && msginfo->folder != NULL)
1868                         item = msginfo->folder;
1869
1870                 num = folder->klass->copy_msg(folder, dest, msginfo);
1871                 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1872         }
1873
1874         /* Read cache for dest folder */
1875         if (!dest->cache) folder_item_read_cache(dest);
1876
1877         /* 
1878          * Fetch new MsgInfos for new messages in dest folder,
1879          * add them to the msgcache and update folder message counts
1880          */
1881         l2 = newmsgnums;
1882         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1883                 MsgInfo *msginfo = (MsgInfo *) l->data;
1884
1885                 num = GPOINTER_TO_INT(l2->data);
1886                 l2 = g_slist_next(l2);
1887
1888                 if (num >= 0) {
1889                         MsgInfo *newmsginfo;
1890
1891                         if (num == 0) {
1892                                 gchar *file;
1893
1894                                 if (!folderscan) {
1895                                         folder_item_scan_full(dest, FALSE);
1896                                         folderscan = TRUE;
1897                                 }
1898                                 file = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
1899                                 num = folder_item_get_msg_num_by_file(dest, file);
1900                                 g_free(file);
1901                         }
1902
1903                         if (num > lastnum)
1904                                 lastnum = num;
1905
1906                         if (num == 0)
1907                                 continue;
1908
1909                         if (!folderscan && 
1910                             ((newmsginfo = folder->klass->get_msginfo(folder, dest, num)) != NULL)) {
1911                                 add_msginfo_to_cache(dest, newmsginfo, msginfo);
1912                                 procmsg_msginfo_free(newmsginfo);
1913                         } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
1914                                 copy_msginfo_flags(msginfo, newmsginfo);
1915                                 procmsg_msginfo_free(newmsginfo);
1916                         }
1917                 }
1918         }
1919
1920         /*
1921          * Remove source messages from their folders if
1922          * copying was successfull and update folder
1923          * message counts
1924          */
1925         l2 = newmsgnums;
1926         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1927                 MsgInfo *msginfo = (MsgInfo *) l->data;
1928
1929                 num = GPOINTER_TO_INT(l2->data);
1930                 l2 = g_slist_next(l2);
1931                 
1932                 if (num >= 0) {
1933                         item->folder->klass->remove_msg(item->folder,
1934                                                         msginfo->folder,
1935                                                         msginfo->msgnum);
1936                         remove_msginfo_from_cache(item, msginfo);
1937                 }
1938         }
1939
1940
1941         if (folder->klass->finished_copy)
1942                 folder->klass->finished_copy(folder, dest);
1943
1944         g_slist_free(newmsgnums);
1945         return lastnum;
1946 }
1947
1948 /*
1949 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1950 {
1951         Folder *folder;
1952         gint num;
1953
1954         g_return_val_if_fail(dest != NULL, -1);
1955         g_return_val_if_fail(msginfo != NULL, -1);
1956
1957         folder = dest->folder;
1958         if (dest->last_num < 0) folder->scan(folder, dest);
1959
1960         num = folder->copy_msg(folder, dest, msginfo);
1961         if (num > 0) dest->last_num = num;
1962
1963         return num;
1964 }
1965 */
1966
1967 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1968 {
1969         GSList *list = NULL;
1970         gint ret;
1971
1972         list = g_slist_append(list, msginfo);
1973         ret = folder_item_copy_msgs_with_dest(dest, list);
1974         g_slist_free(list);
1975         
1976         return ret;
1977 }
1978
1979 /*
1980 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1981 {
1982         Folder *folder;
1983         gint num;
1984
1985         g_return_val_if_fail(dest != NULL, -1);
1986         g_return_val_if_fail(msglist != NULL, -1);
1987
1988         folder = dest->folder;
1989         if (dest->last_num < 0) folder->scan(folder, dest);
1990
1991         num = folder->copy_msgs_with_dest(folder, dest, msglist);
1992         if (num > 0) dest->last_num = num;
1993         else dest->op_count = 0;
1994
1995         return num;
1996 }
1997 */
1998
1999 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
2000 {
2001         Folder *folder;
2002         gint num, lastnum = -1;
2003         GSList *newmsgnums = NULL;
2004         GSList *l, *l2;
2005         gboolean folderscan = FALSE;
2006
2007         g_return_val_if_fail(dest != NULL, -1);
2008         g_return_val_if_fail(msglist != NULL, -1);
2009
2010         folder = dest->folder;
2011  
2012         g_return_val_if_fail(folder->klass->copy_msg != NULL, -1);
2013
2014         /* 
2015          * Copy messages to destination folder and 
2016          * store new message numbers in newmsgnums
2017          */
2018         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
2019                 MsgInfo * msginfo = (MsgInfo *) l->data;
2020
2021                 num = folder->klass->copy_msg(folder, dest, msginfo);
2022                 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
2023         }
2024
2025         /* Read cache for dest folder */
2026         if (!dest->cache) folder_item_read_cache(dest);
2027
2028         /* 
2029          * Fetch new MsgInfos for new messages in dest folder,
2030          * add them to the msgcache and update folder message counts
2031          */
2032         l2 = newmsgnums;
2033         for (l = msglist; l != NULL; l = g_slist_next(l)) {
2034                 MsgInfo *msginfo = (MsgInfo *) l->data;
2035
2036                 num = GPOINTER_TO_INT(l2->data);
2037                 l2 = g_slist_next(l2);
2038
2039                 if (num >= 0) {
2040                         MsgInfo *newmsginfo;
2041
2042                         if (num == 0) {
2043                                 gchar *file;
2044
2045                                 if (!folderscan) {
2046                                         folder_item_scan_full(dest, FALSE);
2047                                         folderscan = TRUE;
2048                                 }
2049                                 file = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
2050                                 num = folder_item_get_msg_num_by_file(dest, file);
2051                                 g_free(file);
2052                         }
2053         
2054                         if (num > lastnum)
2055                                 lastnum = num;
2056
2057                         if (num == 0)
2058                                 continue;
2059
2060                         if (!folderscan && 
2061                             ((newmsginfo = folder->klass->get_msginfo(folder, dest, num)) != NULL)) {
2062                                 newmsginfo = folder->klass->get_msginfo(folder, dest, num);
2063                                 add_msginfo_to_cache(dest, newmsginfo, msginfo);
2064                                 procmsg_msginfo_free(newmsginfo);
2065                         } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
2066                                 copy_msginfo_flags(msginfo, newmsginfo);
2067                                 procmsg_msginfo_free(newmsginfo);
2068                         }
2069                 }
2070         }
2071         
2072         if (folder->klass->finished_copy)
2073                 folder->klass->finished_copy(folder, dest);
2074
2075         g_slist_free(newmsgnums);
2076         return lastnum;
2077 }
2078
2079 gint folder_item_remove_msg(FolderItem *item, gint num)
2080 {
2081         Folder *folder;
2082         gint ret;
2083         MsgInfo *msginfo;
2084
2085         g_return_val_if_fail(item != NULL, -1);
2086         folder = item->folder;
2087         g_return_val_if_fail(folder->klass->remove_msg != NULL, -1);
2088
2089         if (!item->cache) folder_item_read_cache(item);
2090
2091         ret = folder->klass->remove_msg(folder, item, num);
2092
2093         msginfo = msgcache_get_msg(item->cache, num);
2094         if (msginfo != NULL) {
2095                 remove_msginfo_from_cache(item, msginfo);
2096                 procmsg_msginfo_free(msginfo);
2097         }
2098         folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2099
2100         return ret;
2101 }
2102
2103 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
2104 {
2105         Folder *folder;
2106         gint ret = 0;
2107
2108         g_return_val_if_fail(item != NULL, -1);
2109         folder = item->folder;
2110         g_return_val_if_fail(folder != NULL, -1);
2111
2112         if (!item->cache) folder_item_read_cache(item);
2113
2114         while (msglist != NULL) {
2115                 MsgInfo *msginfo = (MsgInfo *)msglist->data;
2116
2117                 ret = folder_item_remove_msg(item, msginfo->msgnum);
2118                 if (ret != 0) break;
2119                 msgcache_remove_msg(item->cache, msginfo->msgnum);
2120                 msglist = msglist->next;
2121         }
2122
2123         return ret;
2124 }
2125
2126 gint folder_item_remove_all_msg(FolderItem *item)
2127 {
2128         Folder *folder;
2129         gint result;
2130
2131         g_return_val_if_fail(item != NULL, -1);
2132
2133         folder = item->folder;
2134
2135         g_return_val_if_fail(folder->klass->remove_all_msg != NULL, -1);
2136
2137         result = folder->klass->remove_all_msg(folder, item);
2138
2139         if (result == 0) {
2140                 if (folder->klass->finished_remove)
2141                         folder->klass->finished_remove(folder, item);
2142
2143                 folder_item_free_cache(item);
2144                 item->cache = msgcache_new();
2145
2146                 item->new_msgs = 0;
2147                 item->unread_msgs = 0;
2148                 item->unreadmarked_msgs = 0;
2149                 item->total_msgs = 0;
2150                 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2151         }
2152
2153         return result;
2154 }
2155
2156 void folder_item_change_msg_flags(FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
2157 {
2158         g_return_if_fail(item != NULL);
2159         g_return_if_fail(msginfo != NULL);
2160         
2161         if (item->folder->klass->change_flags != NULL) {
2162                 item->folder->klass->change_flags(item->folder, item, msginfo, newflags);
2163         } else {
2164                 msginfo->flags.perm_flags = newflags;
2165         }
2166 }
2167
2168 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
2169 {
2170         Folder *folder;
2171
2172         g_return_val_if_fail(item != NULL, FALSE);
2173
2174         folder = item->folder;
2175
2176         g_return_val_if_fail(folder->klass->is_msg_changed != NULL, -1);
2177
2178         return folder->klass->is_msg_changed(folder, item, msginfo);
2179 }
2180
2181 gchar *folder_item_get_cache_file(FolderItem *item)
2182 {
2183         gchar *path;
2184         gchar *file;
2185
2186         g_return_val_if_fail(item != NULL, NULL);
2187         g_return_val_if_fail(item->path != NULL, NULL);
2188
2189         path = folder_item_get_path(item);
2190         g_return_val_if_fail(path != NULL, NULL);
2191         if (!is_dir_exist(path))
2192                 make_dir_hier(path);
2193         file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
2194         g_free(path);
2195
2196         return file;
2197 }
2198
2199 gchar *folder_item_get_mark_file(FolderItem *item)
2200 {
2201         gchar *path;
2202         gchar *file;
2203
2204         g_return_val_if_fail(item != NULL, NULL);
2205         g_return_val_if_fail(item->path != NULL, NULL);
2206
2207         path = folder_item_get_path(item);
2208         g_return_val_if_fail(path != NULL, NULL);
2209         if (!is_dir_exist(path))
2210                 make_dir_hier(path);
2211         file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
2212         g_free(path);
2213
2214         return file;
2215 }
2216
2217 static gboolean folder_build_tree(GNode *node, gpointer data)
2218 {
2219         Folder *folder = FOLDER(data);
2220         FolderItem *item;
2221         XMLNode *xmlnode;
2222         GList *list;
2223         SpecialFolderItemType stype = F_NORMAL;
2224         const gchar *name = NULL;
2225         const gchar *path = NULL;
2226         PrefsAccount *account = NULL;
2227         gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE, 
2228                  threaded = TRUE, apply_sub = FALSE;
2229         gboolean ret_rcpt = FALSE, hidereadmsgs = FALSE,
2230                  thread_collapsed = FALSE; /* CLAWS */
2231         FolderSortKey sort_key = SORT_BY_NONE;
2232         FolderSortType sort_type = SORT_ASCENDING;
2233         gint new = 0, unread = 0, total = 0, unreadmarked = 0;
2234         time_t mtime = 0;
2235
2236         g_return_val_if_fail(node->data != NULL, FALSE);
2237         if (!node->parent) return FALSE;
2238
2239         xmlnode = node->data;
2240         if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
2241                 g_warning("tag name != \"folderitem\"\n");
2242                 return FALSE;
2243         }
2244
2245         list = xmlnode->tag->attr;
2246         for (; list != NULL; list = list->next) {
2247                 XMLAttr *attr = list->data;
2248
2249                 if (!attr || !attr->name || !attr->value) continue;
2250                 if (!strcmp(attr->name, "type")) {
2251                         if (!strcasecmp(attr->value, "normal"))
2252                                 stype = F_NORMAL;
2253                         else if (!strcasecmp(attr->value, "inbox"))
2254                                 stype = F_INBOX;
2255                         else if (!strcasecmp(attr->value, "outbox"))
2256                                 stype = F_OUTBOX;
2257                         else if (!strcasecmp(attr->value, "draft"))
2258                                 stype = F_DRAFT;
2259                         else if (!strcasecmp(attr->value, "queue"))
2260                                 stype = F_QUEUE;
2261                         else if (!strcasecmp(attr->value, "trash"))
2262                                 stype = F_TRASH;
2263                 } else if (!strcmp(attr->name, "name"))
2264                         name = attr->value;
2265                 else if (!strcmp(attr->name, "path"))
2266                         path = attr->value;
2267                 else if (!strcmp(attr->name, "mtime"))
2268                         mtime = strtoul(attr->value, NULL, 10);
2269                 else if (!strcmp(attr->name, "new"))
2270                         new = atoi(attr->value);
2271                 else if (!strcmp(attr->name, "unread"))
2272                         unread = atoi(attr->value);
2273                 else if (!strcmp(attr->name, "unreadmarked"))
2274                         unreadmarked = atoi(attr->value);
2275                 else if (!strcmp(attr->name, "total"))
2276                         total = atoi(attr->value);
2277                 else if (!strcmp(attr->name, "no_sub"))
2278                         no_sub = *attr->value == '1' ? TRUE : FALSE;
2279                 else if (!strcmp(attr->name, "no_select"))
2280                         no_select = *attr->value == '1' ? TRUE : FALSE;
2281                 else if (!strcmp(attr->name, "collapsed"))
2282                         collapsed = *attr->value == '1' ? TRUE : FALSE;
2283                 else if (!strcmp(attr->name, "thread_collapsed"))
2284                         thread_collapsed =  *attr->value == '1' ? TRUE : FALSE;
2285                 else if (!strcmp(attr->name, "threaded"))
2286                         threaded =  *attr->value == '1' ? TRUE : FALSE;
2287                 else if (!strcmp(attr->name, "hidereadmsgs"))
2288                         hidereadmsgs =  *attr->value == '1' ? TRUE : FALSE;
2289                 else if (!strcmp(attr->name, "reqretrcpt"))
2290                         ret_rcpt =  *attr->value == '1' ? TRUE : FALSE;
2291                 else if (!strcmp(attr->name, "sort_key")) {
2292                         if (!strcmp(attr->value, "none"))
2293                                 sort_key = SORT_BY_NONE;
2294                         else if (!strcmp(attr->value, "number"))
2295                                 sort_key = SORT_BY_NUMBER;
2296                         else if (!strcmp(attr->value, "size"))
2297                                 sort_key = SORT_BY_SIZE;
2298                         else if (!strcmp(attr->value, "date"))
2299                                 sort_key = SORT_BY_DATE;
2300                         else if (!strcmp(attr->value, "from"))
2301                                 sort_key = SORT_BY_FROM;
2302                         else if (!strcmp(attr->value, "subject"))
2303                                 sort_key = SORT_BY_SUBJECT;
2304                         else if (!strcmp(attr->value, "score"))
2305                                 sort_key = SORT_BY_SCORE;
2306                         else if (!strcmp(attr->value, "label"))
2307                                 sort_key = SORT_BY_LABEL;
2308                         else if (!strcmp(attr->value, "mark"))
2309                                 sort_key = SORT_BY_MARK;
2310                         else if (!strcmp(attr->value, "unread"))
2311                                 sort_key = SORT_BY_STATUS;
2312                         else if (!strcmp(attr->value, "mime"))
2313                                 sort_key = SORT_BY_MIME;
2314                         else if (!strcmp(attr->value, "to"))
2315                                 sort_key = SORT_BY_TO;
2316                         else if (!strcmp(attr->value, "locked"))
2317                                 sort_key = SORT_BY_LOCKED;
2318                 } else if (!strcmp(attr->name, "sort_type")) {
2319                         if (!strcmp(attr->value, "ascending"))
2320                                 sort_type = SORT_ASCENDING;
2321                         else
2322                                 sort_type = SORT_DESCENDING;
2323                 } else if (!strcmp(attr->name, "account_id")) {
2324                         account = account_find_from_id(atoi(attr->value));
2325                         if (!account) g_warning("account_id: %s not found\n",
2326                                                 attr->value);
2327                 } else if (!strcmp(attr->name, "apply_sub"))
2328                         apply_sub = *attr->value == '1' ? TRUE : FALSE;
2329         }
2330
2331         item = folder_item_new(folder, name, path);
2332         item->stype = stype;
2333         item->mtime = mtime;
2334         item->new_msgs = new;
2335         item->unread_msgs = unread;
2336         item->unreadmarked_msgs = unreadmarked;
2337         item->total_msgs = total;
2338         item->no_sub = no_sub;
2339         item->no_select = no_select;
2340         item->collapsed = collapsed;
2341         item->thread_collapsed = thread_collapsed;
2342         item->threaded  = threaded;
2343         item->hide_read_msgs  = hidereadmsgs;
2344         item->ret_rcpt  = ret_rcpt;
2345         item->sort_key  = sort_key;
2346         item->sort_type = sort_type;
2347         item->parent = FOLDER_ITEM(node->parent->data);
2348         item->folder = folder;
2349         switch (stype) {
2350         case F_INBOX:  folder->inbox  = item; break;
2351         case F_OUTBOX: folder->outbox = item; break;
2352         case F_DRAFT:  folder->draft  = item; break;
2353         case F_QUEUE:  folder->queue  = item; break;
2354         case F_TRASH:  folder->trash  = item; break;
2355         default:       break;
2356         }
2357         item->account = account;
2358         item->apply_sub = apply_sub;
2359         prefs_folder_item_read_config(item);
2360
2361         node->data = item;
2362         xml_free_node(xmlnode);
2363
2364         return FALSE;
2365 }
2366
2367 static gboolean folder_read_folder_func(GNode *node, gpointer data)
2368 {
2369         Folder *folder;
2370         XMLNode *xmlnode;
2371         GList *list;
2372         FolderClass *class = NULL;
2373         const gchar *name = NULL;
2374         const gchar *path = NULL;
2375         PrefsAccount *account = NULL;
2376         gboolean collapsed = FALSE, threaded = TRUE, apply_sub = FALSE;
2377         gboolean ret_rcpt = FALSE, thread_collapsed = FALSE; /* CLAWS */
2378
2379         if (g_node_depth(node) != 2) return FALSE;
2380         g_return_val_if_fail(node->data != NULL, FALSE);
2381
2382         xmlnode = node->data;
2383         if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
2384                 g_warning("tag name != \"folder\"\n");
2385                 return TRUE;
2386         }
2387         g_node_unlink(node);
2388         list = xmlnode->tag->attr;
2389         for (; list != NULL; list = list->next) {
2390                 XMLAttr *attr = list->data;
2391
2392                 if (!attr || !attr->name || !attr->value) continue;
2393                 if (!strcmp(attr->name, "type"))
2394                         class = folder_get_class_from_string(attr->value);
2395                 else if (!strcmp(attr->name, "name"))
2396                         name = attr->value;
2397                 else if (!strcmp(attr->name, "path"))
2398                         path = attr->value;
2399                 else if (!strcmp(attr->name, "collapsed"))
2400                         collapsed = *attr->value == '1' ? TRUE : FALSE;
2401                 else if (!strcmp(attr->name, "thread_collapsed"))
2402                         thread_collapsed = *attr->value == '1' ? TRUE : FALSE;
2403                 else if (!strcmp(attr->name, "threaded"))
2404                         threaded = *attr->value == '1' ? TRUE : FALSE;
2405                 else if (!strcmp(attr->name, "account_id")) {
2406                         account = account_find_from_id(atoi(attr->value));
2407                         if (!account) g_warning("account_id: %s not found\n",
2408                                                 attr->value);
2409                 } else if (!strcmp(attr->name, "apply_sub"))
2410                         apply_sub = *attr->value == '1' ? TRUE : FALSE;
2411                 else if (!strcmp(attr->name, "reqretrcpt"))
2412                         ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
2413         }
2414
2415         folder = folder_new(class, name, path);
2416         g_return_val_if_fail(folder != NULL, FALSE);
2417         folder->account = account;
2418         if (account != NULL)
2419                 account->folder = REMOTE_FOLDER(folder);
2420         node->data = folder->node->data;
2421         g_node_destroy(folder->node);
2422         folder->node = node;
2423         folder_add(folder);
2424         FOLDER_ITEM(node->data)->collapsed = collapsed;
2425         FOLDER_ITEM(node->data)->thread_collapsed = thread_collapsed;
2426         FOLDER_ITEM(node->data)->threaded  = threaded;
2427         FOLDER_ITEM(node->data)->account   = account;
2428         FOLDER_ITEM(node->data)->apply_sub = apply_sub;
2429         FOLDER_ITEM(node->data)->ret_rcpt  = ret_rcpt;
2430
2431         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2432                         folder_build_tree, folder);
2433
2434         return FALSE;
2435 }
2436
2437 static gchar *folder_get_list_path(void)
2438 {
2439         static gchar *filename = NULL;
2440
2441         if (!filename)
2442                 filename =  g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2443                                         FOLDER_LIST, NULL);
2444
2445         return filename;
2446 }
2447
2448 #define PUT_ESCAPE_STR(fp, attr, str)                   \
2449 {                                                       \
2450         fputs(" " attr "=\"", fp);                      \
2451         xml_file_put_escape_str(fp, str);               \
2452         fputs("\"", fp);                                \
2453 }
2454
2455 static void folder_write_list_recursive(GNode *node, gpointer data)
2456 {
2457         FILE *fp = (FILE *)data;
2458         FolderItem *item;
2459         gint i, depth;
2460         static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
2461                                                  "draft", "queue", "trash"};
2462         static gchar *sort_key_str[] = {"none", "number", "size", "date",
2463                                         "from", "subject", "score", "label",
2464                                         "mark", "unread", "mime", "to", 
2465                                         "locked"};
2466         g_return_if_fail(node != NULL);
2467         g_return_if_fail(fp != NULL);
2468
2469         item = FOLDER_ITEM(node->data);
2470         g_return_if_fail(item != NULL);
2471
2472         depth = g_node_depth(node);
2473         for (i = 0; i < depth; i++)
2474                 fputs("    ", fp);
2475         if (depth == 1) {
2476                 Folder *folder = item->folder;
2477
2478                 fprintf(fp, "<folder type=\"%s\"", folder->klass->idstr);
2479                 if (folder->name)
2480                         PUT_ESCAPE_STR(fp, "name", folder->name);
2481                 if (FOLDER_TYPE(folder) == F_MH || FOLDER_TYPE(folder) == F_MBOX)
2482                         PUT_ESCAPE_STR(fp, "path",
2483                                        LOCAL_FOLDER(folder)->rootpath);
2484                 if (item->collapsed && node->children)
2485                         fputs(" collapsed=\"1\"", fp);
2486                 if (folder->account)
2487                         fprintf(fp, " account_id=\"%d\"",
2488                                 folder->account->account_id);
2489                 if (item->apply_sub)
2490                         fputs(" apply_sub=\"1\"", fp);
2491                 if (item->ret_rcpt) 
2492                         fputs(" reqretrcpt=\"1\"", fp);
2493         } else {
2494                 fprintf(fp, "<folderitem type=\"%s\"",
2495                         folder_item_stype_str[item->stype]);
2496                 if (item->name)
2497                         PUT_ESCAPE_STR(fp, "name", item->name);
2498                 if (item->path)
2499                         PUT_ESCAPE_STR(fp, "path", item->path);
2500
2501                 if (item->no_sub)
2502                         fputs(" no_sub=\"1\"", fp);
2503                 if (item->no_select)
2504                         fputs(" no_select=\"1\"", fp);
2505                 if (item->collapsed && node->children)
2506                         fputs(" collapsed=\"1\"", fp);
2507                 else
2508                         fputs(" collapsed=\"0\"", fp);
2509                 if (item->thread_collapsed)
2510                         fputs(" thread_collapsed=\"1\"", fp);
2511                 else
2512                         fputs(" thread_collapsed=\"0\"", fp);
2513                 if (item->threaded)
2514                         fputs(" threaded=\"1\"", fp);
2515                 else
2516                         fputs(" threaded=\"0\"", fp);
2517                 if (item->hide_read_msgs)
2518                         fputs(" hidereadmsgs=\"1\"", fp);
2519                 else
2520                         fputs(" hidereadmsgs=\"0\"", fp);
2521                 if (item->ret_rcpt)
2522                         fputs(" reqretrcpt=\"1\"", fp);
2523
2524                 if (item->sort_key != SORT_BY_NONE) {
2525                         fprintf(fp, " sort_key=\"%s\"",
2526                                 sort_key_str[item->sort_key]);
2527                         if (item->sort_type == SORT_ASCENDING)
2528                                 fprintf(fp, " sort_type=\"ascending\"");
2529                         else
2530                                 fprintf(fp, " sort_type=\"descending\"");
2531                 }
2532
2533                 fprintf(fp,
2534                         " mtime=\"%lu\" new=\"%d\" unread=\"%d\" unreadmarked=\"%d\" total=\"%d\"",
2535                         item->mtime, item->new_msgs, item->unread_msgs, item->unreadmarked_msgs, item->total_msgs);
2536
2537                 if (item->account)
2538                         fprintf(fp, " account_id=\"%d\"",
2539                                 item->account->account_id);
2540                 if (item->apply_sub)
2541                         fputs(" apply_sub=\"1\"", fp);
2542         }
2543
2544         if (node->children) {
2545                 GNode *child;
2546                 fputs(">\n", fp);
2547
2548                 child = node->children;
2549                 while (child) {
2550                         GNode *cur;
2551
2552                         cur = child;
2553                         child = cur->next;
2554                         folder_write_list_recursive(cur, data);
2555                 }
2556
2557                 for (i = 0; i < depth; i++)
2558                         fputs("    ", fp);
2559                 fprintf(fp, "</%s>\n", depth == 1 ? "folder" : "folderitem");
2560         } else
2561                 fputs(" />\n", fp);
2562 }
2563
2564 static void folder_update_op_count_rec(GNode *node)
2565 {
2566         FolderItem *fitem = FOLDER_ITEM(node->data);
2567
2568         if (g_node_depth(node) > 0) {
2569                 if (fitem->op_count > 0) {
2570                         fitem->op_count = 0;
2571                         folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
2572                 }
2573                 if (node->children) {
2574                         GNode *child;
2575
2576                         child = node->children;
2577                         while (child) {
2578                                 GNode *cur;
2579
2580                                 cur = child;
2581                                 child = cur->next;
2582                                 folder_update_op_count_rec(cur);
2583                         }
2584                 }
2585         }
2586 }
2587
2588 void folder_update_op_count(void) 
2589 {
2590         GList *cur;
2591         Folder *folder;
2592
2593         for (cur = folder_list; cur != NULL; cur = cur->next) {
2594                 folder = cur->data;
2595                 folder_update_op_count_rec(folder->node);
2596         }
2597 }
2598
2599 typedef struct _type_str {
2600         gchar * str;
2601         gint type;
2602 } type_str;
2603
2604
2605 /*
2606 static gchar * folder_item_get_tree_identifier(FolderItem * item)
2607 {
2608         if (item->parent != NULL) {
2609                 gchar * path;
2610                 gchar * id;
2611
2612                 path = folder_item_get_tree_identifier(item->parent);
2613                 if (path == NULL)
2614                         return NULL;
2615
2616                 id = g_strconcat(path, "/", item->name, NULL);
2617                 g_free(path);
2618
2619                 return id;
2620         }
2621         else {
2622                 return g_strconcat("/", item->name, NULL);
2623         }
2624 }
2625 */
2626
2627 /* CLAWS: temporary local folder for filtering */
2628 #define TEMP_FOLDER "TEMP_FOLDER"
2629 #define PROCESSING_FOLDER_ITEM "processing"     
2630
2631 static FolderItem *processing_folder_item;
2632
2633 static void folder_create_processing_folder(void)
2634 {
2635         Folder *processing_folder;
2636         gchar      *tmpname;
2637
2638         if ((processing_folder = folder_find_from_name(TEMP_FOLDER, mh_get_class())) == NULL) {
2639                 gchar *tmppath;
2640
2641                 tmppath =
2642                     g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2643                                 "tempfolder", NULL);
2644                 processing_folder =
2645                     folder_new(mh_get_class(), TEMP_FOLDER, tmppath);
2646                 g_free(tmppath);
2647         }
2648         g_assert(processing_folder != NULL);
2649
2650         debug_print("tmpparentroot %s\n", LOCAL_FOLDER(processing_folder)->rootpath);
2651         if (LOCAL_FOLDER(processing_folder)->rootpath[0] == '/')
2652                 tmpname = g_strconcat(LOCAL_FOLDER(processing_folder)->rootpath,
2653                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
2654                                       NULL);
2655         else
2656                 tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2657                                       LOCAL_FOLDER(processing_folder)->rootpath,
2658                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
2659                                       NULL);
2660
2661         if (!is_dir_exist(tmpname)) {
2662                 debug_print("*TMP* creating %s\n", tmpname);
2663                 processing_folder_item = processing_folder->klass->create_folder(processing_folder,
2664                                                                                  processing_folder->node->data,
2665                                                                                  PROCESSING_FOLDER_ITEM);
2666         } else {
2667                 debug_print("*TMP* already created\n");
2668                 processing_folder_item = folder_item_new(processing_folder, PROCESSING_FOLDER_ITEM, PROCESSING_FOLDER_ITEM);
2669                 g_assert(processing_folder_item);
2670                 folder_item_append(processing_folder->node->data, processing_folder_item);
2671         }
2672         g_assert(processing_folder_item != NULL);
2673         g_free(tmpname);
2674 }
2675
2676 FolderItem *folder_get_default_processing(void)
2677 {
2678         if (!processing_folder_item) {
2679                 folder_create_processing_folder();
2680         }
2681         return processing_folder_item;
2682 }
2683
2684 /* folder_persist_prefs_new() - return hash table with persistent
2685  * settings (and folder name as key). 
2686  * (note that in claws other options are in the PREFS_FOLDER_ITEM_RC
2687  * file, so those don't need to be included in PersistPref yet) 
2688  */
2689 GHashTable *folder_persist_prefs_new(Folder *folder)
2690 {
2691         GHashTable *pptable;
2692
2693         g_return_val_if_fail(folder, NULL);
2694         pptable = g_hash_table_new(g_str_hash, g_str_equal);
2695         folder_get_persist_prefs_recursive(folder->node, pptable);
2696         return pptable;
2697 }
2698
2699 void folder_persist_prefs_free(GHashTable *pptable)
2700 {
2701         g_return_if_fail(pptable);
2702         g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
2703         g_hash_table_destroy(pptable);
2704 }
2705
2706 const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
2707 {
2708         if (pptable == NULL || name == NULL) return NULL;
2709         return g_hash_table_lookup(pptable, name);
2710 }
2711
2712 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
2713 {
2714         const PersistPrefs *pp;
2715         gchar *id = folder_item_get_identifier(item);
2716
2717         pp = folder_get_persist_prefs(pptable, id); 
2718         g_free(id);
2719
2720         if (!pp) return;
2721
2722         /* CLAWS: since not all folder properties have been migrated to 
2723          * folderlist.xml, we need to call the old stuff first before
2724          * setting things that apply both to Main and Claws. */
2725         prefs_folder_item_read_config(item); 
2726          
2727         item->collapsed = pp->collapsed;
2728         item->thread_collapsed = pp->thread_collapsed;
2729         item->threaded  = pp->threaded;
2730         item->ret_rcpt  = pp->ret_rcpt;
2731         item->hide_read_msgs = pp->hide_read_msgs;
2732         item->sort_key  = pp->sort_key;
2733         item->sort_type = pp->sort_type;
2734 }
2735
2736 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
2737 {
2738         FolderItem *item = FOLDER_ITEM(node->data);
2739         PersistPrefs *pp;
2740         GNode *child, *cur;
2741         gchar *id;
2742
2743         g_return_if_fail(node != NULL);
2744         g_return_if_fail(item != NULL);
2745
2746         /* NOTE: item->path == NULL means top level folder; not interesting
2747          * to store preferences of that one.  */
2748         if (item->path) {
2749                 id = folder_item_get_identifier(item);
2750                 pp = g_new0(PersistPrefs, 1);
2751                 g_return_if_fail(pp != NULL);
2752                 pp->collapsed = item->collapsed;
2753                 pp->thread_collapsed = item->thread_collapsed;
2754                 pp->threaded  = item->threaded;
2755                 pp->ret_rcpt  = item->ret_rcpt; 
2756                 pp->hide_read_msgs = item->hide_read_msgs;
2757                 pp->sort_key  = item->sort_key;
2758                 pp->sort_type = item->sort_type;
2759                 g_hash_table_insert(pptable, id, pp);
2760         }
2761
2762         if (node->children) {
2763                 child = node->children;
2764                 while (child) {
2765                         cur = child;
2766                         child = cur->next;
2767                         folder_get_persist_prefs_recursive(cur, pptable);
2768                 }
2769         }       
2770 }
2771
2772 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
2773 {
2774         if (key) 
2775                 g_free(key);
2776         if (val) 
2777                 g_free(val);
2778         return TRUE;    
2779 }
2780
2781 void folder_item_apply_processing(FolderItem *item)
2782 {
2783         GSList *processing_list;
2784         GSList *mlist, *cur;
2785         
2786         g_return_if_fail(item != NULL);
2787         
2788         processing_list = item->prefs->processing;
2789         if (processing_list == NULL)
2790                 return;
2791
2792         folder_item_update_freeze();
2793
2794         mlist = folder_item_get_msg_list(item);
2795         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2796                 MsgInfo * msginfo;
2797
2798                 msginfo = (MsgInfo *) cur->data;
2799                 filter_message_by_msginfo(processing_list, msginfo);
2800                 procmsg_msginfo_free(msginfo);
2801         }
2802         g_slist_free(mlist);
2803
2804         folder_item_update_thaw();
2805 }
2806
2807 /*
2808  *  functions for handling FolderItem content changes
2809  */
2810 static gint folder_item_update_freeze_cnt = 0;
2811
2812 /**
2813  * Notify the folder system about changes to a folder. If the
2814  * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
2815  * be invoked, otherwise the changes will be remebered until
2816  * the folder system is thawed.
2817  *
2818  * \param item The FolderItem that was changed
2819  * \param update_flags Type of changed that was made
2820  */
2821 void folder_item_update(FolderItem *item, FolderItemUpdateFlags update_flags)
2822 {
2823         if (folder_item_update_freeze_cnt == 0) {
2824                 FolderItemUpdateData source;
2825         
2826                 source.item = item;
2827                 source.update_flags = update_flags;
2828                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
2829         } else {
2830                 item->update_flags |= update_flags;
2831         }
2832 }
2833
2834 void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
2835 {
2836         GNode *node = item->folder->node;       
2837
2838         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
2839         node = node->children;
2840
2841         folder_item_update(item, update_flags);
2842         while (node != NULL) {
2843                 if (node && node->data) {
2844                         FolderItem *next_item = (FolderItem*) node->data;
2845
2846                         folder_item_update(next_item, update_flags);
2847                 }
2848                 node = node->next;
2849         }
2850 }
2851
2852 void folder_item_update_freeze(void)
2853 {
2854         folder_item_update_freeze_cnt++;
2855 }
2856
2857 static void folder_item_update_func(FolderItem *item, gpointer data)
2858 {
2859         FolderItemUpdateData source;
2860     
2861         if (item->update_flags) {
2862                 source.item = item;
2863                 source.update_flags = item->update_flags;
2864                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);                             
2865                 item->update_flags = 0;
2866         }
2867 }
2868
2869 void folder_item_update_thaw(void)
2870 {
2871         if (folder_item_update_freeze_cnt > 0)
2872                 folder_item_update_freeze_cnt--;
2873         if (folder_item_update_freeze_cnt == 0) {
2874                 /* Update all folders */
2875                 folder_func_to_all_folders(folder_item_update_func, NULL);
2876         }
2877 }
2878
2879 #undef PUT_ESCAPE_STR