fix crash in folder_destroy
[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                 mlist = folder_item_get_msg_list(item);
996                 for (cur = mlist ; cur != NULL ; cur = cur->next) {
997                         MsgInfo * msginfo;
998
999                         msginfo = (MsgInfo *) cur->data;
1000                         if (MSG_IS_NEW(msginfo->flags))
1001                                 procmsg_msginfo_unset_flags(msginfo, MSG_NEW, 0);
1002                         procmsg_msginfo_free(msginfo);
1003                 }
1004                 g_slist_free(mlist);
1005         }               
1006
1007         folder_item_write_cache(item);
1008         
1009         folder_item_update(item, F_ITEM_UPDATE_MSGCNT);
1010 }
1011
1012 gint folder_item_scan(FolderItem *item)
1013 {
1014         Folder *folder;
1015         GSList *folder_list = NULL, *cache_list = NULL;
1016         GSList *folder_list_cur, *cache_list_cur, *new_list = NULL;
1017         GSList *exists_list = NULL, *elem;
1018         GSList *newmsg_list = NULL;
1019         guint newcnt = 0, unreadcnt = 0, totalcnt = 0, unreadmarkedcnt = 0;
1020         guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
1021         gboolean update_flags = 0;
1022     
1023         g_return_val_if_fail(item != NULL, -1);
1024         if (item->path == NULL) return -1;
1025
1026         folder = item->folder;
1027
1028         g_return_val_if_fail(folder != NULL, -1);
1029         g_return_val_if_fail(folder->klass->get_num_list != NULL, -1);
1030
1031         debug_print("Scanning folder %s for cache changes.\n", item->path);
1032
1033         /* Get list of messages for folder and cache */
1034         if (folder->klass->get_num_list(item->folder, item, &folder_list) < 0) {
1035                 debug_print("Error fetching list of message numbers\n");
1036                 return(-1);
1037         }
1038
1039         if (!folder->klass->check_msgnum_validity || 
1040             folder->klass->check_msgnum_validity(folder, item)) {
1041                 if (!item->cache)
1042                         folder_item_read_cache(item);
1043                 cache_list = msgcache_get_msg_list(item->cache);
1044         } else {
1045                 if (item->cache)
1046                         msgcache_destroy(item->cache);
1047                 item->cache = msgcache_new();
1048                 cache_list = NULL;
1049         }
1050
1051         /* Sort both lists */
1052         cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
1053         folder_list = g_slist_sort(folder_list, folder_sort_folder_list);
1054
1055         cache_list_cur = cache_list;
1056         folder_list_cur = folder_list;
1057
1058         if (cache_list_cur != NULL) {
1059                 GSList *cache_list_last;
1060         
1061                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1062                 cache_list_last = g_slist_last(cache_list);
1063                 cache_max_num = ((MsgInfo *)cache_list_last->data)->msgnum;
1064         } else {
1065                 cache_cur_num = G_MAXINT;
1066                 cache_max_num = 0;
1067         }
1068
1069         if (folder_list_cur != NULL) {
1070                 GSList *folder_list_last;
1071         
1072                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1073                 folder_list_last = g_slist_last(folder_list);
1074                 folder_max_num = GPOINTER_TO_INT(folder_list_last->data);
1075         } else {
1076                 folder_cur_num = G_MAXINT;
1077                 folder_max_num = 0;
1078         }
1079
1080         while ((cache_cur_num != G_MAXINT) || (folder_cur_num != G_MAXINT)) {
1081                 /*
1082                  *  Message only exists in the folder
1083                  *  Remember message for fetching
1084                  */
1085                 if (folder_cur_num < cache_cur_num) {
1086                         gboolean add = FALSE;
1087
1088                         switch(FOLDER_TYPE(folder)) {
1089                                 case F_NEWS:
1090                                         if (folder_cur_num < cache_max_num)
1091                                                 break;
1092                                         
1093                                         if (folder->account->max_articles == 0) {
1094                                                 add = TRUE;
1095                                         }
1096
1097                                         if (folder_max_num <= folder->account->max_articles) {
1098                                                 add = TRUE;
1099                                         } else if (folder_cur_num > (folder_max_num - folder->account->max_articles)) {
1100                                                 add = TRUE;
1101                                         }
1102                                         break;
1103                                 default:
1104                                         add = TRUE;
1105                                         break;
1106                         }
1107                         
1108                         if (add) {
1109                                 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(folder_cur_num));
1110                                 debug_print("Remembered message %d for fetching\n", folder_cur_num);
1111                         }
1112
1113                         /* Move to next folder number */
1114                         folder_list_cur = folder_list_cur->next;
1115
1116                         if (folder_list_cur != NULL)
1117                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1118                         else
1119                                 folder_cur_num = G_MAXINT;
1120
1121                         continue;
1122                 }
1123
1124                 /*
1125                  *  Message only exists in the cache
1126                  *  Remove the message from the cache
1127                  */
1128                 if (cache_cur_num < folder_cur_num) {
1129                         msgcache_remove_msg(item->cache, cache_cur_num);
1130                         debug_print("Removed message %d from cache.\n", cache_cur_num);
1131
1132                         /* Move to next cache number */
1133                         cache_list_cur = cache_list_cur->next;
1134
1135                         if (cache_list_cur != NULL)
1136                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1137                         else
1138                                 cache_cur_num = G_MAXINT;
1139
1140                         update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1141
1142                         continue;
1143                 }
1144
1145                 /*
1146                  *  Message number exists in folder and cache!
1147                  *  Check if the message has been modified
1148                  */
1149                 if (cache_cur_num == folder_cur_num) {
1150                         MsgInfo *msginfo;
1151
1152                         msginfo = msgcache_get_msg(item->cache, folder_cur_num);
1153                         if (folder->klass->is_msg_changed && folder->klass->is_msg_changed(folder, item, msginfo)) {
1154                                 msgcache_remove_msg(item->cache, msginfo->msgnum);
1155                                 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(msginfo->msgnum));
1156                                 procmsg_msginfo_free(msginfo);
1157
1158                                 debug_print("Remembering message %d to update...\n", folder_cur_num);
1159                         } else
1160                                 exists_list = g_slist_prepend(exists_list, msginfo);
1161
1162                         /* Move to next folder and cache number */
1163                         cache_list_cur = cache_list_cur->next;
1164                         folder_list_cur = folder_list_cur->next;
1165
1166                         if (cache_list_cur != NULL)
1167                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1168                         else
1169                                 cache_cur_num = G_MAXINT;
1170
1171                         if (folder_list_cur != NULL)
1172                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1173                         else
1174                                 folder_cur_num = G_MAXINT;
1175
1176                         continue;
1177                 }
1178         }
1179         
1180         for(cache_list_cur = cache_list; cache_list_cur != NULL; cache_list_cur = g_slist_next(cache_list_cur))
1181                 procmsg_msginfo_free((MsgInfo *) cache_list_cur->data);
1182
1183         g_slist_free(cache_list);
1184         g_slist_free(folder_list);
1185
1186         if (new_list != NULL) {
1187                 if (folder->klass->get_msginfos) {
1188                         newmsg_list = folder->klass->get_msginfos(folder, item, new_list);
1189                 } else if (folder->klass->get_msginfo) {
1190                         GSList *elem;
1191         
1192                         for (elem = new_list; elem != NULL; elem = g_slist_next(elem)) {
1193                                 MsgInfo *msginfo;
1194                                 guint num;
1195
1196                                 num = GPOINTER_TO_INT(elem->data);
1197                                 msginfo = folder->klass->get_msginfo(folder, item, num);
1198                                 if (msginfo != NULL) {
1199                                         newmsg_list = g_slist_prepend(newmsg_list, msginfo);
1200                                         debug_print("Added newly found message %d to cache.\n", num);
1201                                 }
1202                         }
1203                 }
1204                 g_slist_free(new_list);
1205         }
1206
1207         if (newmsg_list != NULL) {
1208                 GSList *elem;
1209
1210                 for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
1211                         MsgInfo *msginfo = (MsgInfo *) elem->data;
1212
1213                         msgcache_add_msg(item->cache, msginfo);
1214                         if ((item->stype == F_INBOX) &&
1215                             (item->folder->account != NULL) && 
1216                             (item->folder->account->filter_on_recv) &&
1217                             procmsg_msginfo_filter(msginfo))
1218                                 procmsg_msginfo_free(msginfo);
1219                         else
1220                                 exists_list = g_slist_prepend(exists_list, msginfo);
1221                 }
1222                 g_slist_free(newmsg_list);
1223
1224                 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1225         }
1226
1227         for (elem = exists_list; elem != NULL; elem = g_slist_next(elem)) {
1228                 MsgInfo *msginfo;
1229
1230                 msginfo = elem->data;
1231                 if (MSG_IS_IGNORE_THREAD(msginfo->flags) && (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1232                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1233                 if (!MSG_IS_IGNORE_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD)) {
1234                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1235                         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
1236                 }
1237                 if ((item->stype == F_OUTBOX ||
1238                      item->stype == F_QUEUE  ||
1239                      item->stype == F_DRAFT  ||
1240                      item->stype == F_TRASH) &&
1241                     (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1242                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1243                 if (MSG_IS_NEW(msginfo->flags))
1244                         newcnt++;
1245                 if (MSG_IS_UNREAD(msginfo->flags))
1246                         unreadcnt++;
1247                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1248                         unreadmarkedcnt++;
1249                 totalcnt++;
1250
1251                 procmsg_msginfo_free(msginfo);
1252         }
1253         g_slist_free(exists_list);
1254
1255         item->new_msgs = newcnt;
1256         item->unread_msgs = unreadcnt;
1257         item->total_msgs = totalcnt;
1258         item->unreadmarked_msgs = unreadmarkedcnt;
1259
1260         update_flags |= F_ITEM_UPDATE_MSGCNT;
1261
1262         folder_item_update(item, update_flags);
1263
1264         return 0;
1265 }
1266
1267 static void folder_item_scan_foreach_func(gpointer key, gpointer val,
1268                                           gpointer data)
1269 {
1270         folder_item_scan(FOLDER_ITEM(key));
1271 }
1272
1273 void folder_item_scan_foreach(GHashTable *table)
1274 {
1275         g_hash_table_foreach(table, folder_item_scan_foreach_func, NULL);
1276 }
1277
1278 void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
1279 {
1280         gint *memusage = (gint *)data;
1281
1282         if (item->cache == NULL)
1283                 return;
1284         
1285         *memusage += msgcache_get_memory_usage(item->cache);
1286 }
1287
1288 gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
1289 {
1290         FolderItem *fa = (FolderItem *)a;
1291         FolderItem *fb = (FolderItem *)b;
1292         
1293         return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
1294 }
1295
1296 void folder_find_expired_caches(FolderItem *item, gpointer data)
1297 {
1298         GSList **folder_item_list = (GSList **)data;
1299         gint difftime, expiretime;
1300         
1301         if (item->cache == NULL)
1302                 return;
1303
1304         if (item->opened > 0)
1305                 return;
1306
1307         difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
1308         expiretime = prefs_common.cache_min_keep_time * 60;
1309         debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
1310         if (difftime > expiretime) {
1311                 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
1312         }
1313 }
1314
1315 void folder_item_free_cache(FolderItem *item)
1316 {
1317         g_return_if_fail(item != NULL);
1318         
1319         if (item->cache == NULL)
1320                 return;
1321         
1322         if (item->opened > 0)
1323                 return;
1324
1325         folder_item_write_cache(item);
1326         msgcache_destroy(item->cache);
1327         item->cache = NULL;
1328 }
1329
1330 void folder_clean_cache_memory()
1331 {
1332         gint memusage = 0;
1333
1334         folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);       
1335         debug_print("Total cache memory usage: %d\n", memusage);
1336         
1337         if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
1338                 GSList *folder_item_list = NULL, *listitem;
1339                 
1340                 debug_print("Trying to free cache memory\n");
1341
1342                 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);      
1343                 listitem = folder_item_list;
1344                 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
1345                         FolderItem *item = (FolderItem *)(listitem->data);
1346
1347                         debug_print("Freeing cache memory for %s\n", item->path);
1348                         memusage -= msgcache_get_memory_usage(item->cache);
1349                         folder_item_free_cache(item);
1350                         listitem = listitem->next;
1351                 }
1352                 g_slist_free(folder_item_list);
1353         }
1354 }
1355
1356 void folder_item_read_cache(FolderItem *item)
1357 {
1358         gchar *cache_file, *mark_file;
1359         
1360         g_return_if_fail(item != NULL);
1361
1362         cache_file = folder_item_get_cache_file(item);
1363         mark_file = folder_item_get_mark_file(item);
1364         item->cache = msgcache_read_cache(item, cache_file);
1365         if (!item->cache) {
1366                 item->cache = msgcache_new();
1367                 folder_item_scan(item);
1368         }
1369         msgcache_read_mark(item->cache, mark_file);
1370         g_free(cache_file);
1371         g_free(mark_file);
1372
1373         folder_clean_cache_memory();
1374 }
1375
1376 void folder_item_write_cache(FolderItem *item)
1377 {
1378         gchar *cache_file, *mark_file;
1379         PrefsFolderItem *prefs;
1380         gint filemode = 0;
1381         gchar *id;
1382         
1383         if (!item || !item->path || !item->cache)
1384                 return;
1385
1386         id = folder_item_get_identifier(item);
1387         debug_print("Save cache for folder %s\n", id);
1388         g_free(id);
1389
1390         cache_file = folder_item_get_cache_file(item);
1391         mark_file = folder_item_get_mark_file(item);
1392         if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
1393                 prefs = item->prefs;
1394                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
1395                         /* for cache file */
1396                         filemode = prefs->folder_chmod;
1397                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
1398                         if (filemode & S_IROTH) filemode |= S_IWOTH;
1399                         chmod(cache_file, filemode);
1400                 }
1401         }
1402
1403         g_free(cache_file);
1404         g_free(mark_file);
1405 }
1406
1407 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
1408 {
1409         Folder *folder;
1410         MsgInfo *msginfo;
1411         
1412         g_return_val_if_fail(item != NULL, NULL);
1413         
1414         folder = item->folder;
1415         if (!item->cache)
1416                 folder_item_read_cache(item);
1417         
1418         if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
1419                 return msginfo;
1420         
1421         g_return_val_if_fail(folder->klass->get_msginfo, NULL);
1422         if ((msginfo = folder->klass->get_msginfo(folder, item, num)) != NULL) {
1423                 msgcache_add_msg(item->cache, msginfo);
1424                 return msginfo;
1425         }
1426         
1427         return NULL;
1428 }
1429
1430 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
1431 {
1432         Folder *folder;
1433         MsgInfo *msginfo;
1434         
1435         g_return_val_if_fail(item != NULL, NULL);
1436         
1437         folder = item->folder;
1438         if (!item->cache)
1439                 folder_item_read_cache(item);
1440         
1441         if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
1442                 return msginfo;
1443
1444         return NULL;
1445 }
1446
1447 GSList *folder_item_get_msg_list(FolderItem *item)
1448 {
1449         g_return_val_if_fail(item != NULL, NULL);
1450         
1451         if (item->cache == 0)
1452                 folder_item_read_cache(item);
1453
1454         g_return_val_if_fail(item->cache != NULL, NULL);
1455         
1456         return msgcache_get_msg_list(item->cache);
1457 }
1458
1459 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
1460 {
1461         Folder *folder;
1462
1463         g_return_val_if_fail(item != NULL, NULL);
1464
1465         folder = item->folder;
1466
1467         g_return_val_if_fail(folder->klass->fetch_msg != NULL, NULL);
1468
1469         return folder->klass->fetch_msg(folder, item, num);
1470 }
1471
1472 static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
1473 {
1474         static HeaderEntry hentry[] = {{"Message-ID:",  NULL, TRUE},
1475                                        {NULL,           NULL, FALSE}};
1476         FILE *fp;
1477         MsgInfo *msginfo;
1478         gint msgnum = 0;
1479         gchar buf[BUFFSIZE];
1480
1481         if ((fp = fopen(file, "rb")) == NULL)
1482                 return 0;
1483
1484         if ((dest->stype == F_QUEUE) || (dest->stype == F_DRAFT))
1485                 while (fgets(buf, sizeof(buf), fp) != NULL)
1486                         if (buf[0] == '\r' || buf[0] == '\n') break;
1487
1488         procheader_get_header_fields(fp, hentry);
1489         if (hentry[0].body) {
1490                 extract_parenthesis(hentry[0].body, '<', '>');
1491                 remove_space(hentry[0].body);
1492                 if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
1493                         msgnum = msginfo->msgnum;
1494                         procmsg_msginfo_free(msginfo);
1495
1496                         debug_print("found message as uid %d\n", msgnum);
1497                 }
1498         }
1499         
1500         g_free(hentry[0].body);
1501         hentry[0].body = NULL;
1502         fclose(fp);
1503
1504         return msgnum;
1505 }
1506
1507 static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
1508 {
1509         MsgPermFlags perm_flags = 0;
1510         MsgTmpFlags tmp_flags = 0;
1511
1512         /* create new flags */
1513         if (source != NULL) {
1514                 /* copy original flags */
1515                 perm_flags = source->flags.perm_flags;
1516                 tmp_flags = source->flags.tmp_flags;
1517         } else {
1518                 perm_flags = dest->flags.perm_flags;
1519                 tmp_flags = dest->flags.tmp_flags;
1520         }
1521
1522         /* remove new, unread and deleted in special folders */
1523         if (dest->folder->stype == F_OUTBOX ||
1524             dest->folder->stype == F_QUEUE  ||
1525             dest->folder->stype == F_DRAFT  ||
1526             dest->folder->stype == F_TRASH)
1527                 perm_flags &= ~(MSG_NEW | MSG_UNREAD | MSG_DELETED);
1528
1529         /* set ignore flag of ignored parent exists */
1530         if (procmsg_msg_has_flagged_parent(dest, MSG_IGNORE_THREAD))
1531                 perm_flags |= MSG_IGNORE_THREAD;
1532
1533         /* Unset tmp flags that should not be copied */
1534         tmp_flags &= ~(MSG_MOVE | MSG_COPY);
1535
1536         /* unset flags that are set but should not */
1537         procmsg_msginfo_unset_flags(dest,
1538                                     dest->flags.perm_flags & ~perm_flags,
1539                                     dest->flags.tmp_flags  & ~tmp_flags);
1540         /* set new flags */
1541         procmsg_msginfo_set_flags(dest,
1542                                   ~dest->flags.perm_flags & perm_flags,
1543                                   ~dest->flags.tmp_flags  & tmp_flags);
1544
1545         folder_item_update(dest->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1546 }
1547
1548 static void add_msginfo_to_cache(FolderItem *item, MsgInfo *newmsginfo, MsgInfo *flagsource)
1549 {
1550         /* update folder stats */
1551         if (MSG_IS_NEW(newmsginfo->flags))
1552                 item->new_msgs++;
1553         if (MSG_IS_UNREAD(newmsginfo->flags))
1554                 item->unread_msgs++;
1555         if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
1556                 item->unreadmarked_msgs++;
1557         item->total_msgs++;
1558
1559         copy_msginfo_flags(flagsource, newmsginfo);
1560
1561         msgcache_add_msg(item->cache, newmsginfo);
1562 }
1563
1564 static void remove_msginfo_from_cache(FolderItem *item, MsgInfo *msginfo)
1565 {
1566         if (!item->cache)
1567             folder_item_read_cache(item);
1568
1569         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1570                 msginfo->folder->new_msgs--;
1571         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1572                 msginfo->folder->unread_msgs--;
1573         if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1574                 msginfo->folder->unreadmarked_msgs--;
1575         msginfo->folder->total_msgs--;
1576
1577         msgcache_remove_msg(item->cache, msginfo->msgnum);
1578         folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1579 }
1580
1581 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
1582                          gboolean remove_source)
1583 {
1584         Folder *folder;
1585         gint num;
1586         MsgInfo *msginfo;
1587
1588         g_return_val_if_fail(dest != NULL, -1);
1589         g_return_val_if_fail(file != NULL, -1);
1590
1591         folder = dest->folder;
1592
1593         g_return_val_if_fail(folder->klass->add_msg != NULL, -1);
1594
1595         if (!dest->cache)
1596                 folder_item_read_cache(dest);
1597
1598         num = folder->klass->add_msg(folder, dest, file, FALSE);
1599
1600         if (num > 0) {
1601                 msginfo = folder->klass->get_msginfo(folder, dest, num);
1602
1603                 if (msginfo != NULL) {
1604                         add_msginfo_to_cache(dest, msginfo, NULL);
1605                         procmsg_msginfo_free(msginfo);
1606                 }
1607
1608                 dest->last_num = num;
1609         } else if (num == 0) {
1610                 folder_item_scan(dest);
1611                 num = folder_item_get_msg_num_by_file(dest, file);
1612         }
1613
1614         if (num >= 0 && remove_source) {
1615                 if (unlink(file) < 0)
1616                         FILE_OP_ERROR(file, "unlink");
1617         }
1618
1619         return num;
1620 }
1621
1622 /*
1623 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1624 {
1625         Folder *folder;
1626         gint num;
1627
1628         g_return_val_if_fail(dest != NULL, -1);
1629         g_return_val_if_fail(msginfo != NULL, -1);
1630
1631         folder = dest->folder;
1632         if (dest->last_num < 0) folder->scan(folder, dest);
1633
1634         num = folder->move_msg(folder, dest, msginfo);
1635         if (num > 0) dest->last_num = num;
1636
1637         return num;
1638 }
1639 */
1640                 
1641 FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest) 
1642 {
1643         GSList *mlist;
1644         FolderItem *new_item;
1645         FolderItem *next_item;
1646         GNode *srcnode;
1647         gchar *old_id, *new_id;
1648
1649         mlist = folder_item_get_msg_list(src);
1650
1651         /* move messages */
1652         debug_print("Moving %s to %s\n", src->path, dest->path);
1653         new_item = folder_create_folder(dest, g_basename(src->path));
1654         if (new_item == NULL) {
1655                 printf("Can't create folder\n");
1656                 return NULL;
1657         }
1658         
1659         if (new_item->folder == NULL)
1660                 new_item->folder = dest->folder;
1661
1662         /* move messages */
1663         log_message(_("Moving %s to %s...\n"), 
1664                         src->name, new_item->path);
1665         folder_item_move_msgs_with_dest(new_item, mlist);
1666         
1667         /*copy prefs*/
1668         prefs_folder_item_copy_prefs(src, new_item);
1669         new_item->collapsed = src->collapsed;
1670         new_item->thread_collapsed = src->thread_collapsed;
1671         new_item->threaded  = src->threaded;
1672         new_item->ret_rcpt  = src->ret_rcpt;
1673         new_item->hide_read_msgs = src->hide_read_msgs;
1674         new_item->sort_key  = src->sort_key;
1675         new_item->sort_type = src->sort_type;
1676
1677         prefs_matcher_write_config();
1678         
1679         /* recurse */
1680         srcnode = src->folder->node;    
1681         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
1682         srcnode = srcnode->children;
1683         while (srcnode != NULL) {
1684                 if (srcnode && srcnode->data) {
1685                         next_item = (FolderItem*) srcnode->data;
1686                         srcnode = srcnode->next;
1687                         if (folder_item_move_recursive(next_item, new_item) == NULL)
1688                                 return NULL;
1689                 }
1690         }
1691         old_id = folder_item_get_identifier(src);
1692         new_id = folder_item_get_identifier(new_item);
1693         debug_print("updating rules : %s => %s\n", old_id, new_id);
1694         
1695         src->folder->klass->remove_folder(src->folder, src);
1696         folder_write_list();
1697
1698         if (old_id != NULL && new_id != NULL)
1699                 prefs_filtering_rename_path(old_id, new_id);
1700         g_free(old_id);
1701         g_free(new_id);
1702
1703         return new_item;
1704 }
1705
1706 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item)
1707 {
1708         FolderItem *tmp = dest->parent;
1709         gchar * src_identifier, * dst_identifier;
1710         gchar * phys_srcpath, * phys_dstpath;
1711         GNode *src_node;
1712         
1713         while (tmp) {
1714                 if (tmp == src) {
1715                         return F_MOVE_FAILED_DEST_IS_CHILD;
1716                 }
1717                 tmp = tmp->parent;
1718         }
1719         
1720         tmp = src->parent;
1721         
1722         src_identifier = folder_item_get_identifier(src);
1723         dst_identifier = folder_item_get_identifier(dest);
1724         
1725         if(dst_identifier == NULL && dest->folder && dest->parent == NULL) {
1726                 /* dest can be a root folder */
1727                 dst_identifier = folder_get_identifier(dest->folder);
1728         }
1729         if (src_identifier == NULL || dst_identifier == NULL) {
1730                 debug_print("Can't get identifiers\n");
1731                 return F_MOVE_FAILED;
1732         }
1733
1734         if (src->folder != dest->folder) {
1735                 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
1736         }
1737
1738         phys_srcpath = folder_item_get_path(src);
1739         phys_dstpath = g_strconcat(folder_item_get_path(dest),G_DIR_SEPARATOR_S,g_basename(phys_srcpath),NULL);
1740
1741         if (src->parent == dest || src == dest) {
1742                 g_free(src_identifier);
1743                 g_free(dst_identifier);
1744                 g_free(phys_srcpath);
1745                 g_free(phys_dstpath);
1746                 return F_MOVE_FAILED_DEST_IS_PARENT;
1747         }
1748         debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
1749         if ((tmp = folder_item_move_recursive(src, dest)) == NULL) {
1750                 return F_MOVE_FAILED;
1751         }
1752         
1753         /* update rules */
1754         src_node = g_node_find(src->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, src);
1755         if (src_node) 
1756                 g_node_destroy(src_node);
1757         else
1758                 debug_print("can't remove node: it's null!\n");
1759         /* not to much worry if remove fails, move has been done */
1760         
1761         g_free(src_identifier);
1762         g_free(dst_identifier);
1763         g_free(phys_srcpath);
1764         g_free(phys_dstpath);
1765
1766         *new_item = tmp;
1767
1768         return F_MOVE_OK;
1769 }
1770
1771 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1772 {
1773         GSList *list = NULL;
1774         gint ret;
1775
1776         list = g_slist_append(list, msginfo);
1777         ret = folder_item_move_msgs_with_dest(dest, list);
1778         g_slist_free(list);
1779         
1780         return ret;
1781 }
1782
1783 /*
1784 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1785 {
1786         Folder *folder;
1787         gint num;
1788
1789         g_return_val_if_fail(dest != NULL, -1);
1790         g_return_val_if_fail(msglist != NULL, -1);
1791
1792         folder = dest->folder;
1793         if (dest->last_num < 0) folder->scan(folder, dest);
1794
1795         num = folder->move_msgs_with_dest(folder, dest, msglist);
1796         if (num > 0) dest->last_num = num;
1797         else dest->op_count = 0;
1798
1799         return num;
1800 }
1801 */
1802
1803
1804
1805 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1806 {
1807         Folder *folder;
1808         FolderItem *item;
1809         GSList *newmsgnums = NULL;
1810         GSList *l, *l2;
1811         gint num, lastnum = -1;
1812         gboolean folderscan = FALSE;
1813
1814         g_return_val_if_fail(dest != NULL, -1);
1815         g_return_val_if_fail(msglist != NULL, -1);
1816
1817         folder = dest->folder;
1818
1819         g_return_val_if_fail(folder->klass->copy_msg != NULL, -1);
1820         g_return_val_if_fail(folder->klass->remove_msg != NULL, -1);
1821
1822         /* 
1823          * Copy messages to destination folder and 
1824          * store new message numbers in newmsgnums
1825          */
1826         item = NULL;
1827         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
1828                 MsgInfo * msginfo = (MsgInfo *) l->data;
1829
1830                 if (!item && msginfo->folder != NULL)
1831                         item = msginfo->folder;
1832
1833                 num = folder->klass->copy_msg(folder, dest, msginfo);
1834                 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1835         }
1836
1837         /* Read cache for dest folder */
1838         if (!dest->cache) folder_item_read_cache(dest);
1839
1840         /* 
1841          * Fetch new MsgInfos for new messages in dest folder,
1842          * add them to the msgcache and update folder message counts
1843          */
1844         l2 = newmsgnums;
1845         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1846                 MsgInfo *msginfo = (MsgInfo *) l->data;
1847
1848                 num = GPOINTER_TO_INT(l2->data);
1849                 l2 = g_slist_next(l2);
1850
1851                 if (num >= 0) {
1852                         MsgInfo *newmsginfo;
1853
1854                         if (num == 0) {
1855                                 gchar *file;
1856
1857                                 if (!folderscan) {
1858                                         folder_item_scan(dest);
1859                                         folderscan = TRUE;
1860                                 }
1861                                 file = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
1862                                 num = folder_item_get_msg_num_by_file(dest, file);
1863                                 g_free(file);
1864                         }
1865
1866                         if (num > lastnum)
1867                                 lastnum = num;
1868
1869                         if (num == 0)
1870                                 continue;
1871
1872                         if (!folderscan && 
1873                             ((newmsginfo = folder->klass->get_msginfo(folder, dest, num)) != NULL)) {
1874                                 newmsginfo = folder->klass->get_msginfo(folder, dest, num);
1875                                 add_msginfo_to_cache(dest, newmsginfo, msginfo);
1876                                 procmsg_msginfo_free(newmsginfo);
1877                         } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
1878                                 copy_msginfo_flags(msginfo, newmsginfo);
1879                                 procmsg_msginfo_free(newmsginfo);
1880                         }
1881                 }
1882         }
1883
1884         /*
1885          * Remove source messages from their folders if
1886          * copying was successfull and update folder
1887          * message counts
1888          */
1889         l2 = newmsgnums;
1890         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1891                 MsgInfo *msginfo = (MsgInfo *) l->data;
1892
1893                 num = GPOINTER_TO_INT(l2->data);
1894                 l2 = g_slist_next(l2);
1895                 
1896                 if (num >= 0) {
1897                         item->folder->klass->remove_msg(item->folder,
1898                                                         msginfo->folder,
1899                                                         msginfo->msgnum);
1900                         remove_msginfo_from_cache(item, msginfo);
1901                 }
1902         }
1903
1904
1905         if (folder->klass->finished_copy)
1906                 folder->klass->finished_copy(folder, dest);
1907
1908         g_slist_free(newmsgnums);
1909         return lastnum;
1910 }
1911
1912 /*
1913 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1914 {
1915         Folder *folder;
1916         gint num;
1917
1918         g_return_val_if_fail(dest != NULL, -1);
1919         g_return_val_if_fail(msginfo != NULL, -1);
1920
1921         folder = dest->folder;
1922         if (dest->last_num < 0) folder->scan(folder, dest);
1923
1924         num = folder->copy_msg(folder, dest, msginfo);
1925         if (num > 0) dest->last_num = num;
1926
1927         return num;
1928 }
1929 */
1930
1931 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1932 {
1933         GSList *list = NULL;
1934         gint ret;
1935
1936         list = g_slist_append(list, msginfo);
1937         ret = folder_item_copy_msgs_with_dest(dest, list);
1938         g_slist_free(list);
1939         
1940         return ret;
1941 }
1942
1943 /*
1944 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1945 {
1946         Folder *folder;
1947         gint num;
1948
1949         g_return_val_if_fail(dest != NULL, -1);
1950         g_return_val_if_fail(msglist != NULL, -1);
1951
1952         folder = dest->folder;
1953         if (dest->last_num < 0) folder->scan(folder, dest);
1954
1955         num = folder->copy_msgs_with_dest(folder, dest, msglist);
1956         if (num > 0) dest->last_num = num;
1957         else dest->op_count = 0;
1958
1959         return num;
1960 }
1961 */
1962
1963 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1964 {
1965         Folder *folder;
1966         gint num, lastnum = -1;
1967         GSList *newmsgnums = NULL;
1968         GSList *l, *l2;
1969         gboolean folderscan = FALSE;
1970
1971         g_return_val_if_fail(dest != NULL, -1);
1972         g_return_val_if_fail(msglist != NULL, -1);
1973
1974         folder = dest->folder;
1975  
1976         g_return_val_if_fail(folder->klass->copy_msg != NULL, -1);
1977
1978         /* 
1979          * Copy messages to destination folder and 
1980          * store new message numbers in newmsgnums
1981          */
1982         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
1983                 MsgInfo * msginfo = (MsgInfo *) l->data;
1984
1985                 num = folder->klass->copy_msg(folder, dest, msginfo);
1986                 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1987         }
1988
1989         /* Read cache for dest folder */
1990         if (!dest->cache) folder_item_read_cache(dest);
1991
1992         /* 
1993          * Fetch new MsgInfos for new messages in dest folder,
1994          * add them to the msgcache and update folder message counts
1995          */
1996         l2 = newmsgnums;
1997         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1998                 MsgInfo *msginfo = (MsgInfo *) l->data;
1999
2000                 num = GPOINTER_TO_INT(l2->data);
2001                 l2 = g_slist_next(l2);
2002
2003                 if (num >= 0) {
2004                         MsgInfo *newmsginfo;
2005
2006                         if (num == 0) {
2007                                 gchar *file;
2008
2009                                 if (!folderscan) {
2010                                         folder_item_scan(dest);
2011                                         folderscan = TRUE;
2012                                 }
2013                                 file = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
2014                                 num = folder_item_get_msg_num_by_file(dest, file);
2015                                 g_free(file);
2016                         }
2017         
2018                         if (num > lastnum)
2019                                 lastnum = num;
2020
2021                         if (num == 0)
2022                                 continue;
2023
2024                         if (!folderscan && 
2025                             ((newmsginfo = folder->klass->get_msginfo(folder, dest, num)) != NULL)) {
2026                                 newmsginfo = folder->klass->get_msginfo(folder, dest, num);
2027                                 add_msginfo_to_cache(dest, newmsginfo, msginfo);
2028                                 procmsg_msginfo_free(newmsginfo);
2029                         } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
2030                                 copy_msginfo_flags(msginfo, newmsginfo);
2031                                 procmsg_msginfo_free(newmsginfo);
2032                         }
2033                 }
2034         }
2035         
2036         if (folder->klass->finished_copy)
2037                 folder->klass->finished_copy(folder, dest);
2038
2039         g_slist_free(newmsgnums);
2040         return lastnum;
2041 }
2042
2043 gint folder_item_remove_msg(FolderItem *item, gint num)
2044 {
2045         Folder *folder;
2046         gint ret;
2047         MsgInfo *msginfo;
2048
2049         g_return_val_if_fail(item != NULL, -1);
2050
2051         folder = item->folder;
2052         if (!item->cache) folder_item_read_cache(item);
2053
2054         ret = folder->klass->remove_msg(folder, item, num);
2055
2056         msginfo = msgcache_get_msg(item->cache, num);
2057         if (msginfo != NULL) {
2058                 remove_msginfo_from_cache(item, msginfo);
2059                 procmsg_msginfo_free(msginfo);
2060         }
2061         folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2062
2063         return ret;
2064 }
2065
2066 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
2067 {
2068         Folder *folder;
2069         gint ret = 0;
2070
2071         g_return_val_if_fail(item != NULL, -1);
2072         folder = item->folder;
2073         g_return_val_if_fail(folder != NULL, -1);
2074
2075         if (!item->cache) folder_item_read_cache(item);
2076
2077         while (msglist != NULL) {
2078                 MsgInfo *msginfo = (MsgInfo *)msglist->data;
2079
2080                 ret = folder_item_remove_msg(item, msginfo->msgnum);
2081                 if (ret != 0) break;
2082                 msgcache_remove_msg(item->cache, msginfo->msgnum);
2083                 msglist = msglist->next;
2084         }
2085
2086         return ret;
2087 }
2088
2089 gint folder_item_remove_all_msg(FolderItem *item)
2090 {
2091         Folder *folder;
2092         gint result;
2093
2094         g_return_val_if_fail(item != NULL, -1);
2095
2096         folder = item->folder;
2097
2098         g_return_val_if_fail(folder->klass->remove_all_msg != NULL, -1);
2099
2100         result = folder->klass->remove_all_msg(folder, item);
2101
2102         if (result == 0) {
2103                 if (folder->klass->finished_remove)
2104                         folder->klass->finished_remove(folder, item);
2105
2106                 folder_item_free_cache(item);
2107                 item->cache = msgcache_new();
2108
2109                 item->new_msgs = 0;
2110                 item->unread_msgs = 0;
2111                 item->unreadmarked_msgs = 0;
2112                 item->total_msgs = 0;
2113                 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2114         }
2115
2116         return result;
2117 }
2118
2119 void folder_item_change_msg_flags(FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
2120 {
2121         g_return_if_fail(item != NULL);
2122         g_return_if_fail(msginfo != NULL);
2123         
2124         if (item->folder->klass->change_flags != NULL) {
2125                 item->folder->klass->change_flags(item->folder, item, msginfo, newflags);
2126         } else {
2127                 msginfo->flags.perm_flags = newflags;
2128         }
2129 }
2130
2131 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
2132 {
2133         Folder *folder;
2134
2135         g_return_val_if_fail(item != NULL, FALSE);
2136
2137         folder = item->folder;
2138
2139         g_return_val_if_fail(folder->klass->is_msg_changed != NULL, -1);
2140
2141         return folder->klass->is_msg_changed(folder, item, msginfo);
2142 }
2143
2144 gchar *folder_item_get_cache_file(FolderItem *item)
2145 {
2146         gchar *path;
2147         gchar *file;
2148
2149         g_return_val_if_fail(item != NULL, NULL);
2150         g_return_val_if_fail(item->path != NULL, NULL);
2151
2152         path = folder_item_get_path(item);
2153         g_return_val_if_fail(path != NULL, NULL);
2154         if (!is_dir_exist(path))
2155                 make_dir_hier(path);
2156         file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
2157         g_free(path);
2158
2159         return file;
2160 }
2161
2162 gchar *folder_item_get_mark_file(FolderItem *item)
2163 {
2164         gchar *path;
2165         gchar *file;
2166
2167         g_return_val_if_fail(item != NULL, NULL);
2168         g_return_val_if_fail(item->path != NULL, NULL);
2169
2170         path = folder_item_get_path(item);
2171         g_return_val_if_fail(path != NULL, NULL);
2172         if (!is_dir_exist(path))
2173                 make_dir_hier(path);
2174         file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
2175         g_free(path);
2176
2177         return file;
2178 }
2179
2180 static gboolean folder_build_tree(GNode *node, gpointer data)
2181 {
2182         Folder *folder = FOLDER(data);
2183         FolderItem *item;
2184         XMLNode *xmlnode;
2185         GList *list;
2186         SpecialFolderItemType stype = F_NORMAL;
2187         const gchar *name = NULL;
2188         const gchar *path = NULL;
2189         PrefsAccount *account = NULL;
2190         gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE, 
2191                  threaded = TRUE, apply_sub = FALSE;
2192         gboolean ret_rcpt = FALSE, hidereadmsgs = FALSE,
2193                  thread_collapsed = FALSE; /* CLAWS */
2194         FolderSortKey sort_key = SORT_BY_NONE;
2195         FolderSortType sort_type = SORT_ASCENDING;
2196         gint new = 0, unread = 0, total = 0, unreadmarked = 0;
2197         time_t mtime = 0;
2198
2199         g_return_val_if_fail(node->data != NULL, FALSE);
2200         if (!node->parent) return FALSE;
2201
2202         xmlnode = node->data;
2203         if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
2204                 g_warning("tag name != \"folderitem\"\n");
2205                 return FALSE;
2206         }
2207
2208         list = xmlnode->tag->attr;
2209         for (; list != NULL; list = list->next) {
2210                 XMLAttr *attr = list->data;
2211
2212                 if (!attr || !attr->name || !attr->value) continue;
2213                 if (!strcmp(attr->name, "type")) {
2214                         if (!strcasecmp(attr->value, "normal"))
2215                                 stype = F_NORMAL;
2216                         else if (!strcasecmp(attr->value, "inbox"))
2217                                 stype = F_INBOX;
2218                         else if (!strcasecmp(attr->value, "outbox"))
2219                                 stype = F_OUTBOX;
2220                         else if (!strcasecmp(attr->value, "draft"))
2221                                 stype = F_DRAFT;
2222                         else if (!strcasecmp(attr->value, "queue"))
2223                                 stype = F_QUEUE;
2224                         else if (!strcasecmp(attr->value, "trash"))
2225                                 stype = F_TRASH;
2226                 } else if (!strcmp(attr->name, "name"))
2227                         name = attr->value;
2228                 else if (!strcmp(attr->name, "path"))
2229                         path = attr->value;
2230                 else if (!strcmp(attr->name, "mtime"))
2231                         mtime = strtoul(attr->value, NULL, 10);
2232                 else if (!strcmp(attr->name, "new"))
2233                         new = atoi(attr->value);
2234                 else if (!strcmp(attr->name, "unread"))
2235                         unread = atoi(attr->value);
2236                 else if (!strcmp(attr->name, "unreadmarked"))
2237                         unreadmarked = atoi(attr->value);
2238                 else if (!strcmp(attr->name, "total"))
2239                         total = atoi(attr->value);
2240                 else if (!strcmp(attr->name, "no_sub"))
2241                         no_sub = *attr->value == '1' ? TRUE : FALSE;
2242                 else if (!strcmp(attr->name, "no_select"))
2243                         no_select = *attr->value == '1' ? TRUE : FALSE;
2244                 else if (!strcmp(attr->name, "collapsed"))
2245                         collapsed = *attr->value == '1' ? TRUE : FALSE;
2246                 else if (!strcmp(attr->name, "thread_collapsed"))
2247                         thread_collapsed =  *attr->value == '1' ? TRUE : FALSE;
2248                 else if (!strcmp(attr->name, "threaded"))
2249                         threaded =  *attr->value == '1' ? TRUE : FALSE;
2250                 else if (!strcmp(attr->name, "hidereadmsgs"))
2251                         hidereadmsgs =  *attr->value == '1' ? TRUE : FALSE;
2252                 else if (!strcmp(attr->name, "reqretrcpt"))
2253                         ret_rcpt =  *attr->value == '1' ? TRUE : FALSE;
2254                 else if (!strcmp(attr->name, "sort_key")) {
2255                         if (!strcmp(attr->value, "none"))
2256                                 sort_key = SORT_BY_NONE;
2257                         else if (!strcmp(attr->value, "number"))
2258                                 sort_key = SORT_BY_NUMBER;
2259                         else if (!strcmp(attr->value, "size"))
2260                                 sort_key = SORT_BY_SIZE;
2261                         else if (!strcmp(attr->value, "date"))
2262                                 sort_key = SORT_BY_DATE;
2263                         else if (!strcmp(attr->value, "from"))
2264                                 sort_key = SORT_BY_FROM;
2265                         else if (!strcmp(attr->value, "subject"))
2266                                 sort_key = SORT_BY_SUBJECT;
2267                         else if (!strcmp(attr->value, "score"))
2268                                 sort_key = SORT_BY_SCORE;
2269                         else if (!strcmp(attr->value, "label"))
2270                                 sort_key = SORT_BY_LABEL;
2271                         else if (!strcmp(attr->value, "mark"))
2272                                 sort_key = SORT_BY_MARK;
2273                         else if (!strcmp(attr->value, "unread"))
2274                                 sort_key = SORT_BY_STATUS;
2275                         else if (!strcmp(attr->value, "mime"))
2276                                 sort_key = SORT_BY_MIME;
2277                         else if (!strcmp(attr->value, "to"))
2278                                 sort_key = SORT_BY_TO;
2279                         else if (!strcmp(attr->value, "locked"))
2280                                 sort_key = SORT_BY_LOCKED;
2281                 } else if (!strcmp(attr->name, "sort_type")) {
2282                         if (!strcmp(attr->value, "ascending"))
2283                                 sort_type = SORT_ASCENDING;
2284                         else
2285                                 sort_type = SORT_DESCENDING;
2286                 } else if (!strcmp(attr->name, "account_id")) {
2287                         account = account_find_from_id(atoi(attr->value));
2288                         if (!account) g_warning("account_id: %s not found\n",
2289                                                 attr->value);
2290                 } else if (!strcmp(attr->name, "apply_sub"))
2291                         apply_sub = *attr->value == '1' ? TRUE : FALSE;
2292         }
2293
2294         item = folder_item_new(folder, name, path);
2295         item->stype = stype;
2296         item->mtime = mtime;
2297         item->new_msgs = new;
2298         item->unread_msgs = unread;
2299         item->unreadmarked_msgs = unreadmarked;
2300         item->total_msgs = total;
2301         item->no_sub = no_sub;
2302         item->no_select = no_select;
2303         item->collapsed = collapsed;
2304         item->thread_collapsed = thread_collapsed;
2305         item->threaded  = threaded;
2306         item->hide_read_msgs  = hidereadmsgs;
2307         item->ret_rcpt  = ret_rcpt;
2308         item->sort_key  = sort_key;
2309         item->sort_type = sort_type;
2310         item->parent = FOLDER_ITEM(node->parent->data);
2311         item->folder = folder;
2312         switch (stype) {
2313         case F_INBOX:  folder->inbox  = item; break;
2314         case F_OUTBOX: folder->outbox = item; break;
2315         case F_DRAFT:  folder->draft  = item; break;
2316         case F_QUEUE:  folder->queue  = item; break;
2317         case F_TRASH:  folder->trash  = item; break;
2318         default:       break;
2319         }
2320         item->account = account;
2321         item->apply_sub = apply_sub;
2322         prefs_folder_item_read_config(item);
2323
2324         node->data = item;
2325         xml_free_node(xmlnode);
2326
2327         return FALSE;
2328 }
2329
2330 static gboolean folder_read_folder_func(GNode *node, gpointer data)
2331 {
2332         Folder *folder;
2333         XMLNode *xmlnode;
2334         GList *list;
2335         FolderClass *class = NULL;
2336         const gchar *name = NULL;
2337         const gchar *path = NULL;
2338         PrefsAccount *account = NULL;
2339         gboolean collapsed = FALSE, threaded = TRUE, apply_sub = FALSE;
2340         gboolean ret_rcpt = FALSE, thread_collapsed = FALSE; /* CLAWS */
2341
2342         if (g_node_depth(node) != 2) return FALSE;
2343         g_return_val_if_fail(node->data != NULL, FALSE);
2344
2345         xmlnode = node->data;
2346         if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
2347                 g_warning("tag name != \"folder\"\n");
2348                 return TRUE;
2349         }
2350         g_node_unlink(node);
2351         list = xmlnode->tag->attr;
2352         for (; list != NULL; list = list->next) {
2353                 XMLAttr *attr = list->data;
2354
2355                 if (!attr || !attr->name || !attr->value) continue;
2356                 if (!strcmp(attr->name, "type"))
2357                         class = folder_get_class_from_string(attr->value);
2358                 else if (!strcmp(attr->name, "name"))
2359                         name = attr->value;
2360                 else if (!strcmp(attr->name, "path"))
2361                         path = attr->value;
2362                 else if (!strcmp(attr->name, "collapsed"))
2363                         collapsed = *attr->value == '1' ? TRUE : FALSE;
2364                 else if (!strcmp(attr->name, "thread_collapsed"))
2365                         thread_collapsed = *attr->value == '1' ? TRUE : FALSE;
2366                 else if (!strcmp(attr->name, "threaded"))
2367                         threaded = *attr->value == '1' ? TRUE : FALSE;
2368                 else if (!strcmp(attr->name, "account_id")) {
2369                         account = account_find_from_id(atoi(attr->value));
2370                         if (!account) g_warning("account_id: %s not found\n",
2371                                                 attr->value);
2372                 } else if (!strcmp(attr->name, "apply_sub"))
2373                         apply_sub = *attr->value == '1' ? TRUE : FALSE;
2374                 else if (!strcmp(attr->name, "reqretrcpt"))
2375                         ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
2376         }
2377
2378         folder = folder_new(class, name, path);
2379         g_return_val_if_fail(folder != NULL, FALSE);
2380         folder->account = account;
2381         if (account != NULL)
2382                 account->folder = REMOTE_FOLDER(folder);
2383         node->data = folder->node->data;
2384         g_node_destroy(folder->node);
2385         folder->node = node;
2386         folder_add(folder);
2387         FOLDER_ITEM(node->data)->collapsed = collapsed;
2388         FOLDER_ITEM(node->data)->thread_collapsed = thread_collapsed;
2389         FOLDER_ITEM(node->data)->threaded  = threaded;
2390         FOLDER_ITEM(node->data)->account   = account;
2391         FOLDER_ITEM(node->data)->apply_sub = apply_sub;
2392         FOLDER_ITEM(node->data)->ret_rcpt  = ret_rcpt;
2393
2394         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2395                         folder_build_tree, folder);
2396
2397         return FALSE;
2398 }
2399
2400 static gchar *folder_get_list_path(void)
2401 {
2402         static gchar *filename = NULL;
2403
2404         if (!filename)
2405                 filename =  g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2406                                         FOLDER_LIST, NULL);
2407
2408         return filename;
2409 }
2410
2411 #define PUT_ESCAPE_STR(fp, attr, str)                   \
2412 {                                                       \
2413         fputs(" " attr "=\"", fp);                      \
2414         xml_file_put_escape_str(fp, str);               \
2415         fputs("\"", fp);                                \
2416 }
2417
2418 static void folder_write_list_recursive(GNode *node, gpointer data)
2419 {
2420         FILE *fp = (FILE *)data;
2421         FolderItem *item;
2422         gint i, depth;
2423         static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
2424                                                  "draft", "queue", "trash"};
2425         static gchar *sort_key_str[] = {"none", "number", "size", "date",
2426                                         "from", "subject", "score", "label",
2427                                         "mark", "unread", "mime", "to", 
2428                                         "locked"};
2429         g_return_if_fail(node != NULL);
2430         g_return_if_fail(fp != NULL);
2431
2432         item = FOLDER_ITEM(node->data);
2433         g_return_if_fail(item != NULL);
2434
2435         depth = g_node_depth(node);
2436         for (i = 0; i < depth; i++)
2437                 fputs("    ", fp);
2438         if (depth == 1) {
2439                 Folder *folder = item->folder;
2440
2441                 fprintf(fp, "<folder type=\"%s\"", folder->klass->idstr);
2442                 if (folder->name)
2443                         PUT_ESCAPE_STR(fp, "name", folder->name);
2444                 if (FOLDER_TYPE(folder) == F_MH || FOLDER_TYPE(folder) == F_MBOX)
2445                         PUT_ESCAPE_STR(fp, "path",
2446                                        LOCAL_FOLDER(folder)->rootpath);
2447                 if (item->collapsed && node->children)
2448                         fputs(" collapsed=\"1\"", fp);
2449                 if (folder->account)
2450                         fprintf(fp, " account_id=\"%d\"",
2451                                 folder->account->account_id);
2452                 if (item->apply_sub)
2453                         fputs(" apply_sub=\"1\"", fp);
2454                 if (item->ret_rcpt) 
2455                         fputs(" reqretrcpt=\"1\"", fp);
2456         } else {
2457                 fprintf(fp, "<folderitem type=\"%s\"",
2458                         folder_item_stype_str[item->stype]);
2459                 if (item->name)
2460                         PUT_ESCAPE_STR(fp, "name", item->name);
2461                 if (item->path)
2462                         PUT_ESCAPE_STR(fp, "path", item->path);
2463
2464                 if (item->no_sub)
2465                         fputs(" no_sub=\"1\"", fp);
2466                 if (item->no_select)
2467                         fputs(" no_select=\"1\"", fp);
2468                 if (item->collapsed && node->children)
2469                         fputs(" collapsed=\"1\"", fp);
2470                 else
2471                         fputs(" collapsed=\"0\"", fp);
2472                 if (item->thread_collapsed)
2473                         fputs(" thread_collapsed=\"1\"", fp);
2474                 else
2475                         fputs(" thread_collapsed=\"0\"", fp);
2476                 if (item->threaded)
2477                         fputs(" threaded=\"1\"", fp);
2478                 else
2479                         fputs(" threaded=\"0\"", fp);
2480                 if (item->hide_read_msgs)
2481                         fputs(" hidereadmsgs=\"1\"", fp);
2482                 else
2483                         fputs(" hidereadmsgs=\"0\"", fp);
2484                 if (item->ret_rcpt)
2485                         fputs(" reqretrcpt=\"1\"", fp);
2486
2487                 if (item->sort_key != SORT_BY_NONE) {
2488                         fprintf(fp, " sort_key=\"%s\"",
2489                                 sort_key_str[item->sort_key]);
2490                         if (item->sort_type == SORT_ASCENDING)
2491                                 fprintf(fp, " sort_type=\"ascending\"");
2492                         else
2493                                 fprintf(fp, " sort_type=\"descending\"");
2494                 }
2495
2496                 fprintf(fp,
2497                         " mtime=\"%lu\" new=\"%d\" unread=\"%d\" unreadmarked=\"%d\" total=\"%d\"",
2498                         item->mtime, item->new_msgs, item->unread_msgs, item->unreadmarked_msgs, item->total_msgs);
2499
2500                 if (item->account)
2501                         fprintf(fp, " account_id=\"%d\"",
2502                                 item->account->account_id);
2503                 if (item->apply_sub)
2504                         fputs(" apply_sub=\"1\"", fp);
2505         }
2506
2507         if (node->children) {
2508                 GNode *child;
2509                 fputs(">\n", fp);
2510
2511                 child = node->children;
2512                 while (child) {
2513                         GNode *cur;
2514
2515                         cur = child;
2516                         child = cur->next;
2517                         folder_write_list_recursive(cur, data);
2518                 }
2519
2520                 for (i = 0; i < depth; i++)
2521                         fputs("    ", fp);
2522                 fprintf(fp, "</%s>\n", depth == 1 ? "folder" : "folderitem");
2523         } else
2524                 fputs(" />\n", fp);
2525 }
2526
2527 static void folder_update_op_count_rec(GNode *node)
2528 {
2529         FolderItem *fitem = FOLDER_ITEM(node->data);
2530
2531         if (g_node_depth(node) > 0) {
2532                 if (fitem->op_count > 0) {
2533                         fitem->op_count = 0;
2534                         folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
2535                 }
2536                 if (node->children) {
2537                         GNode *child;
2538
2539                         child = node->children;
2540                         while (child) {
2541                                 GNode *cur;
2542
2543                                 cur = child;
2544                                 child = cur->next;
2545                                 folder_update_op_count_rec(cur);
2546                         }
2547                 }
2548         }
2549 }
2550
2551 void folder_update_op_count() {
2552         GList *cur;
2553         Folder *folder;
2554
2555         for (cur = folder_list; cur != NULL; cur = cur->next) {
2556                 folder = cur->data;
2557                 folder_update_op_count_rec(folder->node);
2558         }
2559 }
2560
2561 typedef struct _type_str {
2562         gchar * str;
2563         gint type;
2564 } type_str;
2565
2566
2567 /*
2568 static gchar * folder_item_get_tree_identifier(FolderItem * item)
2569 {
2570         if (item->parent != NULL) {
2571                 gchar * path;
2572                 gchar * id;
2573
2574                 path = folder_item_get_tree_identifier(item->parent);
2575                 if (path == NULL)
2576                         return NULL;
2577
2578                 id = g_strconcat(path, "/", item->name, NULL);
2579                 g_free(path);
2580
2581                 return id;
2582         }
2583         else {
2584                 return g_strconcat("/", item->name, NULL);
2585         }
2586 }
2587 */
2588
2589 /* CLAWS: temporary local folder for filtering */
2590 #define TEMP_FOLDER "TEMP_FOLDER"
2591 #define PROCESSING_FOLDER_ITEM "processing"     
2592
2593 static FolderItem *processing_folder_item;
2594
2595 static void folder_create_processing_folder(void)
2596 {
2597         Folder *processing_folder;
2598         gchar      *tmpname;
2599
2600         if ((processing_folder = folder_find_from_name(TEMP_FOLDER, mh_get_class())) == NULL) {
2601                 gchar *tmppath;
2602
2603                 tmppath =
2604                     g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2605                                 "tempfolder", NULL);
2606                 processing_folder =
2607                     folder_new(mh_get_class(), TEMP_FOLDER, tmppath);
2608                 g_free(tmppath);
2609         }
2610         g_assert(processing_folder != NULL);
2611
2612         debug_print("tmpparentroot %s\n", LOCAL_FOLDER(processing_folder)->rootpath);
2613         if (LOCAL_FOLDER(processing_folder)->rootpath[0] == '/')
2614                 tmpname = g_strconcat(LOCAL_FOLDER(processing_folder)->rootpath,
2615                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
2616                                       NULL);
2617         else
2618                 tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2619                                       LOCAL_FOLDER(processing_folder)->rootpath,
2620                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
2621                                       NULL);
2622
2623         if (!is_dir_exist(tmpname)) {
2624                 debug_print("*TMP* creating %s\n", tmpname);
2625                 processing_folder_item = processing_folder->klass->create_folder(processing_folder,
2626                                                                                  processing_folder->node->data,
2627                                                                                  PROCESSING_FOLDER_ITEM);
2628         } else {
2629                 debug_print("*TMP* already created\n");
2630                 processing_folder_item = folder_item_new(processing_folder, PROCESSING_FOLDER_ITEM, PROCESSING_FOLDER_ITEM);
2631                 g_assert(processing_folder_item);
2632                 folder_item_append(processing_folder->node->data, processing_folder_item);
2633         }
2634         g_assert(processing_folder_item != NULL);
2635         g_free(tmpname);
2636 }
2637
2638 FolderItem *folder_get_default_processing(void)
2639 {
2640         if (!processing_folder_item) {
2641                 folder_create_processing_folder();
2642         }
2643         return processing_folder_item;
2644 }
2645
2646 /* folder_persist_prefs_new() - return hash table with persistent
2647  * settings (and folder name as key). 
2648  * (note that in claws other options are in the PREFS_FOLDER_ITEM_RC
2649  * file, so those don't need to be included in PersistPref yet) 
2650  */
2651 GHashTable *folder_persist_prefs_new(Folder *folder)
2652 {
2653         GHashTable *pptable;
2654
2655         g_return_val_if_fail(folder, NULL);
2656         pptable = g_hash_table_new(g_str_hash, g_str_equal);
2657         folder_get_persist_prefs_recursive(folder->node, pptable);
2658         return pptable;
2659 }
2660
2661 void folder_persist_prefs_free(GHashTable *pptable)
2662 {
2663         g_return_if_fail(pptable);
2664         g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
2665         g_hash_table_destroy(pptable);
2666 }
2667
2668 const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
2669 {
2670         if (pptable == NULL || name == NULL) return NULL;
2671         return g_hash_table_lookup(pptable, name);
2672 }
2673
2674 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
2675 {
2676         const PersistPrefs *pp;
2677         gchar *id = folder_item_get_identifier(item);
2678
2679         pp = folder_get_persist_prefs(pptable, id); 
2680         g_free(id);
2681
2682         if (!pp) return;
2683
2684         /* CLAWS: since not all folder properties have been migrated to 
2685          * folderlist.xml, we need to call the old stuff first before
2686          * setting things that apply both to Main and Claws. */
2687         prefs_folder_item_read_config(item); 
2688          
2689         item->collapsed = pp->collapsed;
2690         item->thread_collapsed = pp->thread_collapsed;
2691         item->threaded  = pp->threaded;
2692         item->ret_rcpt  = pp->ret_rcpt;
2693         item->hide_read_msgs = pp->hide_read_msgs;
2694         item->sort_key  = pp->sort_key;
2695         item->sort_type = pp->sort_type;
2696 }
2697
2698 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
2699 {
2700         FolderItem *item = FOLDER_ITEM(node->data);
2701         PersistPrefs *pp;
2702         GNode *child, *cur;
2703         gchar *id;
2704
2705         g_return_if_fail(node != NULL);
2706         g_return_if_fail(item != NULL);
2707
2708         /* NOTE: item->path == NULL means top level folder; not interesting
2709          * to store preferences of that one.  */
2710         if (item->path) {
2711                 id = folder_item_get_identifier(item);
2712                 pp = g_new0(PersistPrefs, 1);
2713                 g_return_if_fail(pp != NULL);
2714                 pp->collapsed = item->collapsed;
2715                 pp->thread_collapsed = item->thread_collapsed;
2716                 pp->threaded  = item->threaded;
2717                 pp->ret_rcpt  = item->ret_rcpt; 
2718                 pp->hide_read_msgs = item->hide_read_msgs;
2719                 pp->sort_key  = item->sort_key;
2720                 pp->sort_type = item->sort_type;
2721                 g_hash_table_insert(pptable, id, pp);
2722         }
2723
2724         if (node->children) {
2725                 child = node->children;
2726                 while (child) {
2727                         cur = child;
2728                         child = cur->next;
2729                         folder_get_persist_prefs_recursive(cur, pptable);
2730                 }
2731         }       
2732 }
2733
2734 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
2735 {
2736         if (key) 
2737                 g_free(key);
2738         if (val) 
2739                 g_free(val);
2740         return TRUE;    
2741 }
2742
2743 void folder_item_apply_processing(FolderItem *item)
2744 {
2745         GSList *processing_list;
2746         GSList *mlist, *cur;
2747         
2748         g_return_if_fail(item != NULL);
2749         
2750         processing_list = item->prefs->processing;
2751         if (processing_list == NULL)
2752                 return;
2753
2754         folder_item_update_freeze();
2755
2756         mlist = folder_item_get_msg_list(item);
2757         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2758                 MsgInfo * msginfo;
2759
2760                 msginfo = (MsgInfo *) cur->data;
2761                 filter_message_by_msginfo(processing_list, msginfo);
2762                 procmsg_msginfo_free(msginfo);
2763         }
2764         g_slist_free(mlist);
2765
2766         folder_item_update_thaw();
2767 }
2768
2769 /*
2770  *  functions for handling FolderItem content changes
2771  */
2772 static gint folder_item_update_freeze_cnt = 0;
2773
2774 /**
2775  * Notify the folder system about changes to a folder. If the
2776  * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
2777  * be invoked, otherwise the changes will be remebered until
2778  * the folder system is thawed.
2779  *
2780  * \param item The FolderItem that was changed
2781  * \param update_flags Type of changed that was made
2782  */
2783 void folder_item_update(FolderItem *item, FolderItemUpdateFlags update_flags)
2784 {
2785         if (folder_item_update_freeze_cnt == 0) {
2786                 FolderItemUpdateData source;
2787         
2788                 source.item = item;
2789                 source.update_flags = update_flags;
2790                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
2791         } else {
2792                 item->update_flags |= update_flags;
2793         }
2794 }
2795
2796 void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
2797 {
2798         GNode *node = item->folder->node;       
2799
2800         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
2801         node = node->children;
2802
2803         folder_item_update(item, update_flags);
2804         while (node != NULL) {
2805                 if (node && node->data) {
2806                         FolderItem *next_item = (FolderItem*) node->data;
2807
2808                         folder_item_update(next_item, update_flags);
2809                 }
2810                 node = node->next;
2811         }
2812 }
2813
2814 void folder_item_update_freeze()
2815 {
2816         folder_item_update_freeze_cnt++;
2817 }
2818
2819 static void folder_item_update_func(FolderItem *item, gpointer data)
2820 {
2821         FolderItemUpdateData source;
2822     
2823         if (item->update_flags) {
2824                 source.item = item;
2825                 source.update_flags = item->update_flags;
2826                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);                             
2827                 item->update_flags = 0;
2828         }
2829 }
2830
2831 void folder_item_update_thaw()
2832 {
2833         if (folder_item_update_freeze_cnt > 0)
2834                 folder_item_update_freeze_cnt--;
2835         if (folder_item_update_freeze_cnt == 0) {
2836                 /* Update all folders */
2837                 folder_func_to_all_folders(folder_item_update_func, NULL);
2838         }
2839 }
2840
2841 #undef PUT_ESCAPE_STR