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