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