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