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