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