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