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