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