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