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