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