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