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