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