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