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