2005-10-04 [colin] 1.9.15cvs9
[claws.git] / src / folder.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <glib/gi18n.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34
35 #include "folder.h"
36 #include "session.h"
37 #include "inc.h"
38 #include "imap.h"
39 #include "news.h"
40 #include "mh.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 "procheader.h"
48 #include "hooks.h"
49 #include "log.h"
50 #include "folder_item_prefs.h"
51 #include "remotefolder.h"
52 #include "partial_download.h"
53 #include "statusbar.h"
54
55 /* Dependecies to be removed ?! */
56 #include "prefs_common.h"
57 #include "prefs_account.h"
58
59 static GList *folder_list = NULL;
60 static GSList *class_list = NULL;
61 static GSList *folder_unloaded_list = NULL;
62
63 void folder_init                (Folder         *folder,
64                                  const gchar    *name);
65
66 static gchar *folder_get_list_path      (void);
67 static GNode *folder_get_xml_node       (Folder         *folder);
68 static Folder *folder_get_from_xml      (GNode          *node);
69 static void folder_update_op_count_rec  (GNode          *node);
70
71
72 static void folder_get_persist_prefs_recursive
73                                         (GNode *node, GHashTable *pptable);
74 static gboolean persist_prefs_free      (gpointer key, gpointer val, gpointer data);
75 void folder_item_read_cache             (FolderItem *item);
76 void folder_item_free_cache             (FolderItem *item);
77 gint folder_item_scan_full              (FolderItem *item, gboolean filtering);
78 static void folder_item_update_with_msg (FolderItem *item, FolderItemUpdateFlags update_flags,
79                                          MsgInfo *msg);
80
81 void folder_system_init(void)
82 {
83         folder_register_class(mh_get_class());
84         folder_register_class(imap_get_class());
85         folder_register_class(news_get_class());
86 }
87
88 GSList *folder_get_class_list(void)
89 {
90         return class_list;
91 }
92
93 void folder_register_class(FolderClass *klass)
94 {
95         GSList *xmllist, *cur;
96
97         debug_print("registering folder class %s\n", klass->idstr);
98
99         class_list = g_slist_append(class_list, klass);
100
101         xmllist = g_slist_copy(folder_unloaded_list);
102         for (cur = xmllist; cur != NULL; cur = g_slist_next(cur)) {
103                 GNode *node = (GNode *) cur->data;
104                 XMLNode *xmlnode = (XMLNode *) node->data;
105                 GList *cur = xmlnode->tag->attr;
106
107                 for (; cur != NULL; cur = g_list_next(cur)) {
108                         XMLAttr *attr = (XMLAttr *) cur->data;
109
110                         if (!attr || !attr->name || !attr->value) continue;
111                         if (!strcmp(attr->name, "type") && !strcmp(attr->value, klass->idstr)) {
112                                 Folder *folder;
113
114                                 folder = folder_get_from_xml(node);
115                                 folder_add(folder);
116                                 folder_unloaded_list = g_slist_remove(folder_unloaded_list, node);
117
118                                 cur = NULL;
119                                 continue;
120                         }
121                 }
122         }
123         g_slist_free(xmllist);
124 }
125
126 void folder_unregister_class(FolderClass *klass)
127 {
128         GList *folderlist, *cur;
129
130         debug_print("unregistering folder class %s\n", klass->idstr);
131
132         class_list = g_slist_remove(class_list, klass);
133
134         folderlist = g_list_copy(folder_get_list());
135         for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
136                 Folder *folder = (Folder *) cur->data;
137
138                 if (folder->klass == klass) {
139                         GNode *xmlnode = folder_get_xml_node(folder);
140                         folder_unloaded_list = g_slist_append(folder_unloaded_list, xmlnode);
141                         folder_destroy(folder);
142                 }
143         }
144         g_list_free(folderlist);
145 }
146
147 Folder *folder_new(FolderClass *klass, const gchar *name, const gchar *path)
148 {
149         Folder *folder = NULL;
150         FolderItem *item;
151
152         g_return_val_if_fail(klass != NULL, NULL);
153
154         name = name ? name : path;
155         folder = klass->new_folder(name, path);
156
157         /* Create root folder item */
158         item = folder_item_new(folder, name, NULL);
159         if (item == NULL) {
160                 return NULL;
161         }
162         item->folder = folder;
163         folder->node = item->node = g_node_new(item);
164         folder->data = NULL;
165
166         return folder;
167 }
168
169 void folder_init(Folder *folder, const gchar *name)
170 {
171         g_return_if_fail(folder != NULL);
172
173         folder_set_name(folder, name);
174
175         /* Init folder data */
176         folder->account = NULL;
177         folder->sort = 0;
178         folder->inbox = NULL;
179         folder->outbox = NULL;
180         folder->draft = NULL;
181         folder->queue = NULL;
182         folder->trash = NULL;
183 }
184
185 void folder_destroy(Folder *folder)
186 {
187         g_return_if_fail(folder != NULL);
188         g_return_if_fail(folder->klass->destroy_folder != NULL);
189
190         folder_remove(folder);
191
192         folder_tree_destroy(folder);
193
194         folder->klass->destroy_folder(folder);
195
196         g_free(folder->name);
197         g_free(folder);
198 }
199
200 void folder_set_xml(Folder *folder, XMLTag *tag)
201 {
202         GList *cur;
203         FolderItem *rootitem = NULL;
204
205         if ((folder->node != NULL) && (folder->node->data != NULL))
206                 rootitem = (FolderItem *) folder->node->data;
207
208         for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
209                 XMLAttr *attr = (XMLAttr *) cur->data;
210
211                 if (!attr || !attr->name || !attr->value) continue;
212                 if (!strcmp(attr->name, "name")) {
213                         if (folder->name != NULL)
214                                 g_free(folder->name);
215                         folder->name = g_strdup(attr->value);
216                         if (rootitem != NULL) {
217                                 if (rootitem->name != NULL)
218                                         g_free(rootitem->name);
219                                 rootitem->name = g_strdup(attr->value);
220                         }
221                 } else if (!strcmp(attr->name, "account_id")) {
222                         PrefsAccount *account;
223
224                         account = account_find_from_id(atoi(attr->value));
225                         if (!account)
226                                 g_warning("account_id: %s not found\n", attr->value);
227                         else {
228                                 folder->account = account;
229                                 account->folder = folder;
230                         }
231                 } else if (!strcmp(attr->name, "collapsed")) {
232                         if (rootitem != NULL)
233                                 rootitem->collapsed = *attr->value == '1' ? TRUE : FALSE;
234                 } else if (!strcmp(attr->name, "sort")) {
235                         folder->sort = atoi(attr->value);
236                 }
237         }
238 }
239
240 XMLTag *folder_get_xml(Folder *folder)
241 {
242         XMLTag *tag;
243
244         tag = xml_tag_new("folder");
245
246         if (folder->name)
247                 xml_tag_add_attr(tag, xml_attr_new("name", folder->name));
248         if (folder->account)
249                 xml_tag_add_attr(tag, xml_attr_new_int("account_id", folder->account->account_id));
250         if (folder->node && folder->node->data) {
251                 FolderItem *rootitem = (FolderItem *) folder->node->data;
252
253                 xml_tag_add_attr(tag, xml_attr_new("collapsed", rootitem->collapsed ? "1" : "0"));
254         }
255         xml_tag_add_attr(tag, xml_attr_new_int("sort", folder->sort));
256
257         return tag;
258 }
259
260 FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path)
261 {
262         FolderItem *item = NULL;
263         
264         g_return_val_if_fail(folder != NULL, NULL);
265         
266         if (folder->klass->item_new) {
267                 item = folder->klass->item_new(folder);
268         } else {
269                 item = g_new0(FolderItem, 1);
270         }
271
272         g_return_val_if_fail(item != NULL, NULL);
273
274         item->stype = F_NORMAL;
275         item->name = g_strdup(name);
276         item->path = g_strdup(path);
277         item->mtime = 0;
278         item->new_msgs = 0;
279         item->unread_msgs = 0;
280         item->unreadmarked_msgs = 0;
281         item->marked_msgs = 0;
282         item->total_msgs = 0;
283         item->last_num = -1;
284         item->cache = NULL;
285         item->no_sub = FALSE;
286         item->no_select = FALSE;
287         item->collapsed = FALSE;
288         item->thread_collapsed = FALSE;
289         item->threaded  = TRUE;
290         item->ret_rcpt  = FALSE;
291         item->opened    = FALSE;
292         item->node = g_node_new(item);
293         item->folder = NULL;
294         item->account = NULL;
295         item->apply_sub = FALSE;
296         item->mark_queue = NULL;
297         item->data = NULL;
298         item->parent_stype = -1;
299
300         item->sort_key = SORT_BY_DATE;
301         item->sort_type = SORT_ASCENDING;
302
303         item->prefs = folder_item_prefs_new();
304
305         return item;
306 }
307
308 void folder_item_append(FolderItem *parent, FolderItem *item)
309 {
310         g_return_if_fail(parent != NULL);
311         g_return_if_fail(parent->folder != NULL);
312         g_return_if_fail(parent->node != NULL);
313         g_return_if_fail(item != NULL);
314
315         item->folder = parent->folder;
316         g_node_append(parent->node, item->node);
317 }
318
319 void folder_item_remove(FolderItem *item)
320 {
321         GNode *node, *start_node;
322         FolderUpdateData hookdata;
323
324         g_return_if_fail(item != NULL);
325         g_return_if_fail(item->folder != NULL);
326         g_return_if_fail(item->folder->node != NULL);
327
328         start_node = node = item->node;
329         
330         node = item->folder->node;
331         
332         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
333         node = node->children;
334
335         /* remove my children */
336         while (node != NULL) {
337                 if (node && node->data) {
338                         FolderItem *sub_item = (FolderItem*) node->data;
339                         node = node->next;
340                         folder_item_remove(sub_item);
341                 }
342         }
343         
344         /* remove myself */
345         if (item->cache != NULL) {
346                 msgcache_destroy(item->cache);
347                 item->cache = NULL;
348         }
349
350         hookdata.folder = item->folder;
351         hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_REMOVE_FOLDERITEM;
352         hookdata.item = item;
353         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
354
355         node = start_node;
356
357         if (item->folder->node == node)
358                 item->folder->node = NULL;
359
360         folder_item_destroy(item);
361
362         g_node_destroy(node);
363 }
364
365 void folder_item_remove_children(FolderItem *item)
366 {
367         GNode *node, *next;
368
369         g_return_if_fail(item != NULL);
370         g_return_if_fail(item->folder != NULL);
371         g_return_if_fail(item->node != NULL);
372
373         node = item->node->children;
374         while (node != NULL) {
375                 next = node->next;
376                 folder_item_remove(FOLDER_ITEM(node->data));
377                 node = next;
378         }
379 }
380
381 void folder_item_destroy(FolderItem *item)
382 {
383         Folder *folder;
384
385         g_return_if_fail(item != NULL);
386
387         folder = item->folder;
388         if (folder) {
389                 if (folder->inbox == item)
390                         folder->inbox = NULL;
391                 else if (folder->outbox == item)
392                         folder->outbox = NULL;
393                 else if (folder->draft == item)
394                         folder->draft = NULL;
395                 else if (folder->queue == item)
396                         folder->queue = NULL;
397                 else if (folder->trash == item)
398                         folder->trash = NULL;
399         }
400
401         if (item->cache)
402                 folder_item_free_cache(item);
403         if (item->prefs)
404                 folder_item_prefs_free(item->prefs);
405         g_free(item->name);
406         g_free(item->path);
407
408         if (item->folder != NULL) {
409                 if(item->folder->klass->item_destroy) {
410                         item->folder->klass->item_destroy(item->folder, item);
411                 } else {
412                         g_free(item);
413                 }
414         }
415 }
416
417 FolderItem *folder_item_parent(FolderItem *item)
418 {
419         g_return_val_if_fail(item != NULL, NULL);
420         g_return_val_if_fail(item->node != NULL, NULL);
421
422         if (item->node->parent == NULL)
423                 return NULL;
424         return (FolderItem *) item->node->parent->data;
425 }
426
427 void folder_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
428 {
429         GList *cur;
430
431         for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
432                 XMLAttr *attr = (XMLAttr *) cur->data;
433
434                 if (!attr || !attr->name || !attr->value) continue;
435                 if (!strcmp(attr->name, "type")) {
436                         if (!g_ascii_strcasecmp(attr->value, "normal"))
437                                 item->stype = F_NORMAL;
438                         else if (!g_ascii_strcasecmp(attr->value, "inbox"))
439                                 item->stype = F_INBOX;
440                         else if (!g_ascii_strcasecmp(attr->value, "outbox"))
441                                 item->stype = F_OUTBOX;
442                         else if (!g_ascii_strcasecmp(attr->value, "draft"))
443                                 item->stype = F_DRAFT;
444                         else if (!g_ascii_strcasecmp(attr->value, "queue"))
445                                 item->stype = F_QUEUE;
446                         else if (!g_ascii_strcasecmp(attr->value, "trash"))
447                                 item->stype = F_TRASH;
448                 } else if (!strcmp(attr->name, "name")) {
449                         if (item->name != NULL)
450                                 g_free(item->name);
451                         item->name = g_strdup(attr->value);
452                 } else if (!strcmp(attr->name, "path")) {
453                         if (item->path != NULL)
454                                 g_free(item->path);
455                         item->path = g_strdup(attr->value);
456                 } else if (!strcmp(attr->name, "mtime"))
457                         item->mtime = strtoul(attr->value, NULL, 10);
458                 else if (!strcmp(attr->name, "new"))
459                         item->new_msgs = atoi(attr->value);
460                 else if (!strcmp(attr->name, "unread"))
461                         item->unread_msgs = atoi(attr->value);
462                 else if (!strcmp(attr->name, "unreadmarked"))
463                         item->unreadmarked_msgs = atoi(attr->value);
464                 else if (!strcmp(attr->name, "marked"))
465                         item->marked_msgs = atoi(attr->value);
466                 else if (!strcmp(attr->name, "total"))
467                         item->total_msgs = atoi(attr->value);
468                 else if (!strcmp(attr->name, "no_sub"))
469                         item->no_sub = *attr->value == '1' ? TRUE : FALSE;
470                 else if (!strcmp(attr->name, "no_select"))
471                         item->no_select = *attr->value == '1' ? TRUE : FALSE;
472                 else if (!strcmp(attr->name, "collapsed"))
473                         item->collapsed = *attr->value == '1' ? TRUE : FALSE;
474                 else if (!strcmp(attr->name, "thread_collapsed"))
475                         item->thread_collapsed =  *attr->value == '1' ? TRUE : FALSE;
476                 else if (!strcmp(attr->name, "threaded"))
477                         item->threaded =  *attr->value == '1' ? TRUE : FALSE;
478                 else if (!strcmp(attr->name, "hidereadmsgs"))
479                         item->hide_read_msgs =  *attr->value == '1' ? TRUE : FALSE;
480                 else if (!strcmp(attr->name, "reqretrcpt"))
481                         item->ret_rcpt =  *attr->value == '1' ? TRUE : FALSE;
482                 else if (!strcmp(attr->name, "sort_key")) {
483                         if (!strcmp(attr->value, "none"))
484                                 item->sort_key = SORT_BY_NONE;
485                         else if (!strcmp(attr->value, "number"))
486                                 item->sort_key = SORT_BY_NUMBER;
487                         else if (!strcmp(attr->value, "size"))
488                                 item->sort_key = SORT_BY_SIZE;
489                         else if (!strcmp(attr->value, "date"))
490                                 item->sort_key = SORT_BY_DATE;
491                         else if (!strcmp(attr->value, "from"))
492                                 item->sort_key = SORT_BY_FROM;
493                         else if (!strcmp(attr->value, "subject"))
494                                 item->sort_key = SORT_BY_SUBJECT;
495                         else if (!strcmp(attr->value, "score"))
496                                 item->sort_key = SORT_BY_SCORE;
497                         else if (!strcmp(attr->value, "label"))
498                                 item->sort_key = SORT_BY_LABEL;
499                         else if (!strcmp(attr->value, "mark"))
500                                 item->sort_key = SORT_BY_MARK;
501                         else if (!strcmp(attr->value, "unread"))
502                                 item->sort_key = SORT_BY_STATUS;
503                         else if (!strcmp(attr->value, "mime"))
504                                 item->sort_key = SORT_BY_MIME;
505                         else if (!strcmp(attr->value, "to"))
506                                 item->sort_key = SORT_BY_TO;
507                         else if (!strcmp(attr->value, "locked"))
508                                 item->sort_key = SORT_BY_LOCKED;
509                 } else if (!strcmp(attr->name, "sort_type")) {
510                         if (!strcmp(attr->value, "ascending"))
511                                 item->sort_type = SORT_ASCENDING;
512                         else
513                                 item->sort_type = SORT_DESCENDING;
514                 } else if (!strcmp(attr->name, "account_id")) {
515                         PrefsAccount *account;
516
517                         account = account_find_from_id(atoi(attr->value));
518                         if (!account)
519                                 g_warning("account_id: %s not found\n", attr->value);
520                         else
521                                 item->account = account;
522                 } else if (!strcmp(attr->name, "apply_sub"))
523                         item->apply_sub = *attr->value == '1' ? TRUE : FALSE;
524         }
525 }
526
527 XMLTag *folder_item_get_xml(Folder *folder, FolderItem *item)
528 {
529         static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
530                                                  "draft", "queue", "trash"};
531         static gchar *sort_key_str[] = {"none", "number", "size", "date",
532                                         "from", "subject", "score", "label",
533                                         "mark", "unread", "mime", "to", 
534                                         "locked"};
535         XMLTag *tag;
536         gchar *value;
537
538         tag = xml_tag_new("folderitem");
539
540         xml_tag_add_attr(tag, xml_attr_new("type", folder_item_stype_str[item->stype]));
541         if (item->name)
542                 xml_tag_add_attr(tag, xml_attr_new("name", item->name));
543         if (item->path)
544                 xml_tag_add_attr(tag, xml_attr_new("path", item->path));
545         if (item->no_sub)
546                 xml_tag_add_attr(tag, xml_attr_new("no_sub", "1"));
547         if (item->no_select)
548                 xml_tag_add_attr(tag, xml_attr_new("no_select", "1"));
549         xml_tag_add_attr(tag, xml_attr_new("collapsed", item->collapsed && item->node->children ? "1" : "0"));
550         xml_tag_add_attr(tag, xml_attr_new("thread_collapsed", item->thread_collapsed ? "1" : "0"));
551         xml_tag_add_attr(tag, xml_attr_new("threaded", item->threaded ? "1" : "0"));
552         xml_tag_add_attr(tag, xml_attr_new("hidereadmsgs", item->hide_read_msgs ? "1" : "0"));
553         if (item->ret_rcpt)
554                 xml_tag_add_attr(tag, xml_attr_new("reqretrcpt", "1"));
555
556         if (item->sort_key != SORT_BY_NONE) {
557                 xml_tag_add_attr(tag, xml_attr_new("sort_key", sort_key_str[item->sort_key]));
558                 xml_tag_add_attr(tag, xml_attr_new("sort_type", item->sort_type == SORT_ASCENDING ? "ascending" : "descending"));
559         }
560
561         value = g_strdup_printf("%ld", (unsigned long int) item->mtime);
562         xml_tag_add_attr(tag, xml_attr_new("mtime", value));
563         g_free(value);
564         xml_tag_add_attr(tag, xml_attr_new_int("new", item->new_msgs));
565         xml_tag_add_attr(tag, xml_attr_new_int("unread", item->unread_msgs));
566         xml_tag_add_attr(tag, xml_attr_new_int("unreadmarked", item->unreadmarked_msgs));
567         xml_tag_add_attr(tag, xml_attr_new_int("marked", item->marked_msgs));
568         xml_tag_add_attr(tag, xml_attr_new_int("total", item->total_msgs));
569
570         if (item->account)
571                 xml_tag_add_attr(tag, xml_attr_new_int("account_id", item->account->account_id));
572         if (item->apply_sub)
573                 xml_tag_add_attr(tag, xml_attr_new("apply_sub", "1"));
574
575         return tag;
576 }
577
578 void folder_set_ui_func(Folder *folder, FolderUIFunc func, gpointer data)
579 {
580         g_return_if_fail(folder != NULL);
581
582         folder->ui_func = func;
583         folder->ui_func_data = data;
584 }
585
586 void folder_set_name(Folder *folder, const gchar *name)
587 {
588         g_return_if_fail(folder != NULL);
589
590         g_free(folder->name);
591         folder->name = name ? g_strdup(name) : NULL;
592         if (folder->node && folder->node->data) {
593                 FolderItem *item = (FolderItem *)folder->node->data;
594
595                 g_free(item->name);
596                 item->name = name ? g_strdup(name) : NULL;
597         }
598 }
599
600 void folder_set_sort(Folder *folder, guint sort)
601 {
602         g_return_if_fail(folder != NULL);
603
604         if (folder->sort != sort) {
605                 folder_remove(folder);
606                 folder->sort = sort;
607                 folder_add(folder);
608         }
609 }
610
611 gboolean folder_tree_destroy_func(GNode *node, gpointer data) {
612         FolderItem *item = (FolderItem *) node->data;
613
614         folder_item_destroy(item);
615         return FALSE;
616 }
617
618 void folder_tree_destroy(Folder *folder)
619 {
620         GNode *node;
621
622         g_return_if_fail(folder != NULL);
623
624         node = folder->node;
625         
626         prefs_filtering_clear_folder(folder);
627
628         if (node != NULL) {
629                 g_node_traverse(node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
630                                 folder_tree_destroy_func, NULL);
631                 g_node_destroy(node);
632                 folder->node = NULL;
633         }
634 }
635
636 void folder_add(Folder *folder)
637 {
638         Folder *cur_folder;
639         GList *cur;
640         gint i;
641         FolderUpdateData hookdata;
642
643         g_return_if_fail(folder != NULL);
644
645         for (i = 0, cur = folder_list; cur != NULL; cur = cur->next, i++) {
646                 cur_folder = FOLDER(cur->data);
647                 if (cur_folder->sort < folder->sort)
648                         break;
649         }
650
651         folder_list = g_list_insert(folder_list, folder, i);
652
653         hookdata.folder = folder;
654         hookdata.update_flags = FOLDER_ADD_FOLDER;
655         hookdata.item = NULL;
656         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
657 }
658
659 void folder_remove(Folder *folder)
660 {
661         FolderUpdateData hookdata;
662
663         g_return_if_fail(folder != NULL);
664
665         folder_list = g_list_remove(folder_list, folder);
666
667         hookdata.folder = folder;
668         hookdata.update_flags = FOLDER_REMOVE_FOLDER;
669         hookdata.item = NULL;
670         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
671 }
672
673 GList *folder_get_list(void)
674 {
675         return folder_list;
676 }
677
678 gint folder_read_list(void)
679 {
680         GNode *node, *cur;
681         XMLNode *xmlnode;
682         gchar *path;
683
684         path = folder_get_list_path();
685         if (!is_file_exist(path)) return -1;
686         node = xml_parse_file(path);
687         if (!node) return -1;
688
689         xmlnode = node->data;
690         if (strcmp2(xmlnode->tag->tag, "folderlist") != 0) {
691                 g_warning("wrong folder list\n");
692                 xml_free_tree(node);
693                 return -1;
694         }
695
696         cur = node->children;
697         while (cur != NULL) {
698                 Folder *folder;
699
700                 folder = folder_get_from_xml(cur);
701                 if (folder != NULL)
702                         folder_add(folder);
703                 else
704                         folder_unloaded_list = g_slist_append(folder_unloaded_list,
705                                 (gpointer) xml_copy_tree(cur));
706                 cur = cur->next;
707         }
708
709         xml_free_tree(node);
710         if (folder_list)
711                 return 0;
712         else
713                 return -1;
714 }
715
716 void folder_write_list(void)
717 {
718         GList *list;
719         GSList *slist;
720         Folder *folder;
721         gchar *path;
722         PrefFile *pfile;
723         GNode *rootnode;
724         XMLNode *xmlnode;
725         XMLTag *tag;
726
727         path = folder_get_list_path();
728         if ((pfile = prefs_write_open(path)) == NULL) return;
729
730         xml_file_put_xml_decl(pfile->fp);
731         tag = xml_tag_new("folderlist");
732
733         xmlnode = xml_node_new(tag, NULL);
734
735         rootnode = g_node_new(xmlnode);
736
737         for (list = folder_list; list != NULL; list = list->next) {
738                 GNode *node;
739
740                 folder = list->data;
741                 node = folder_get_xml_node(folder);
742                 if (node != NULL)
743                         g_node_append(rootnode, node);
744         }
745
746         for (slist = folder_unloaded_list; slist != NULL; slist = g_slist_next(slist)) {
747                 GNode *node = (GNode *) slist->data;
748
749                 g_node_append(rootnode, (gpointer) xml_copy_tree(node));
750         }
751
752         xml_write_tree(rootnode, pfile->fp);
753
754         if (prefs_file_close(pfile) < 0)
755                 g_warning("failed to write folder list.\n");
756
757         xml_free_tree(rootnode);
758 }
759
760 gboolean folder_scan_tree_func(GNode *node, gpointer data)
761 {
762         GHashTable *pptable = (GHashTable *)data;
763         FolderItem *item = (FolderItem *)node->data;
764         
765         folder_item_restore_persist_prefs(item, pptable);
766         folder_item_scan_full(item, FALSE);
767
768         return FALSE;
769 }
770
771 void folder_scan_tree(Folder *folder)
772 {
773         GHashTable *pptable;
774         FolderUpdateData hookdata;
775         
776         if (!folder->klass->scan_tree)
777                 return;
778         
779         pptable = folder_persist_prefs_new(folder);
780
781         /*
782          * should be changed and tree update should be done without 
783          * destroying the tree first
784          */
785         folder_tree_destroy(folder);
786         folder->klass->scan_tree(folder);
787
788         hookdata.folder = folder;
789         hookdata.update_flags = FOLDER_TREE_CHANGED;
790         hookdata.item = NULL;
791         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
792
793         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_scan_tree_func, pptable);
794         folder_persist_prefs_free(pptable);
795
796         prefs_matcher_read_config();
797
798         folder_write_list();
799 }
800
801 FolderItem *folder_create_folder(FolderItem *parent, const gchar *name)
802 {
803         FolderItem *new_item;
804
805         new_item = parent->folder->klass->create_folder(parent->folder, parent, name);
806         if (new_item) {
807                 FolderUpdateData hookdata;
808
809                 new_item->cache = msgcache_new();
810
811                 hookdata.folder = new_item->folder;
812                 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDERITEM;
813                 hookdata.item = new_item;
814                 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
815         }
816
817         return new_item;
818 }
819
820 gint folder_item_rename(FolderItem *item, gchar *newname)
821 {
822         gint retval;
823
824         g_return_val_if_fail(item != NULL, -1);
825         g_return_val_if_fail(newname != NULL, -1);
826
827         retval = item->folder->klass->rename_folder(item->folder, item, newname);
828
829         if (retval >= 0) {
830                 FolderItemUpdateData hookdata;
831
832                 hookdata.item = item;
833                 hookdata.update_flags = F_ITEM_UPDATE_NAME;
834                 hookdata.msg = NULL;
835                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &hookdata);
836         }
837
838         return retval;
839 }
840
841 struct TotalMsgCount
842 {
843         guint new_msgs;
844         guint unread_msgs;
845         guint unreadmarked_msgs;
846         guint marked_msgs;
847         guint total_msgs;
848 };
849
850 struct FuncToAllFoldersData
851 {
852         FolderItemFunc  function;
853         gpointer        data;
854 };
855
856 static gboolean folder_func_to_all_folders_func(GNode *node, gpointer data)
857 {
858         FolderItem *item;
859         struct FuncToAllFoldersData *function_data = (struct FuncToAllFoldersData *) data;
860
861         g_return_val_if_fail(node->data != NULL, FALSE);
862
863         item = FOLDER_ITEM(node->data);
864         g_return_val_if_fail(item != NULL, FALSE);
865
866         function_data->function(item, function_data->data);
867
868         return FALSE;
869 }
870
871 void folder_func_to_all_folders(FolderItemFunc function, gpointer data)
872 {
873         GList *list;
874         Folder *folder;
875         struct FuncToAllFoldersData function_data;
876         
877         function_data.function = function;
878         function_data.data = data;
879
880         for (list = folder_list; list != NULL; list = list->next) {
881                 folder = FOLDER(list->data);
882                 if (folder->node)
883                         g_node_traverse(folder->node, G_PRE_ORDER,
884                                         G_TRAVERSE_ALL, -1,
885                                         folder_func_to_all_folders_func,
886                                         &function_data);
887         }
888 }
889
890 static void folder_count_total_msgs_func(FolderItem *item, gpointer data)
891 {
892         struct TotalMsgCount *count = (struct TotalMsgCount *)data;
893
894         count->new_msgs += item->new_msgs;
895         count->unread_msgs += item->unread_msgs;
896         count->unreadmarked_msgs += item->unreadmarked_msgs;
897         count->marked_msgs += item->marked_msgs;
898         count->total_msgs += item->total_msgs;
899 }
900
901 struct TotalMsgStatus
902 {
903         guint new;
904         guint unread;
905         guint total;
906         GString *str;
907 };
908
909 static gboolean folder_get_status_full_all_func(GNode *node, gpointer data)
910 {
911         FolderItem *item;
912         struct TotalMsgStatus *status = (struct TotalMsgStatus *)data;
913         gchar *id;
914  
915         g_return_val_if_fail(node->data != NULL, FALSE);
916  
917         item = FOLDER_ITEM(node->data);
918
919         if (!item->path) return FALSE;
920
921         status->new += item->new_msgs;
922         status->unread += item->unread_msgs;
923         status->total += item->total_msgs;
924
925         if (status->str) {
926                 id = folder_item_get_identifier(item);
927                 g_string_append_printf(status->str, "%5d %5d %5d %s\n",
928                                   item->new_msgs, item->unread_msgs,
929                                   item->total_msgs, id);
930                 g_free(id);
931         }
932  
933         return FALSE;
934  }
935  
936 static void folder_get_status_full_all(GString *str, guint *new, guint *unread,
937                                        guint *total)
938 {
939         GList *list;
940         Folder *folder;
941         struct TotalMsgStatus status;
942  
943         status.new = status.unread = status.total = 0;
944         status.str = str;
945  
946         debug_print("Counting total number of messages...\n");
947  
948         for (list = folder_list; list != NULL; list = list->next) {
949                 folder = FOLDER(list->data);
950                 if (folder->node)
951                         g_node_traverse(folder->node, G_PRE_ORDER,
952                                         G_TRAVERSE_ALL, -1,
953                                         folder_get_status_full_all_func,
954                                         &status);
955         }
956  
957         *new = status.new;
958         *unread = status.unread;
959         *total = status.total;
960 }
961
962 gchar *folder_get_status(GPtrArray *folders, gboolean full)
963 {
964         guint new, unread, total;
965         GString *str;
966         gint i;
967         gchar *ret;
968
969         new = unread = total = 0;
970
971         str = g_string_new(NULL);
972
973         if (folders) {
974                 for (i = 0; i < folders->len; i++) {
975                         FolderItem *item;
976
977                         item = g_ptr_array_index(folders, i);
978                         new += item->new_msgs;
979                         unread += item->unread_msgs;
980                         total += item->total_msgs;
981
982                         if (full) {
983                                 gchar *id;
984
985                                 id = folder_item_get_identifier(item);
986                                 g_string_append_printf(str, "%5d %5d %5d %s\n",
987                                                   item->new_msgs, item->unread_msgs,
988                                                   item->total_msgs, id);
989                                 g_free(id);
990                         }
991                 }
992         } else {
993                 folder_get_status_full_all(full ? str : NULL,
994                                            &new, &unread, &total);
995         }
996
997         if (full)
998                 g_string_append_printf(str, "%5d %5d %5d\n", new, unread, total);
999         else
1000                 g_string_append_printf(str, "%d %d %d\n", new, unread, total);
1001
1002         ret = str->str;
1003         g_string_free(str, FALSE);
1004  
1005         return ret;
1006 }
1007
1008 void folder_count_total_msgs(guint *new_msgs, guint *unread_msgs, 
1009                              guint *unreadmarked_msgs, guint *marked_msgs,
1010                              guint *total_msgs)
1011 {
1012         struct TotalMsgCount count;
1013
1014         count.new_msgs = count.unread_msgs = count.unreadmarked_msgs = count.total_msgs = 0;
1015
1016         debug_print("Counting total number of messages...\n");
1017
1018         folder_func_to_all_folders(folder_count_total_msgs_func, &count);
1019
1020         *new_msgs = count.new_msgs;
1021         *unread_msgs = count.unread_msgs;
1022         *unreadmarked_msgs = count.unreadmarked_msgs;
1023         *marked_msgs = count.marked_msgs;
1024         *total_msgs = count.total_msgs;
1025 }
1026
1027 Folder *folder_find_from_path(const gchar *path)
1028 {
1029         GList *list;
1030         Folder *folder;
1031
1032         for (list = folder_list; list != NULL; list = list->next) {
1033                 folder = list->data;
1034                 if ((FOLDER_TYPE(folder) == F_MH || 
1035                      FOLDER_TYPE(folder) == F_MBOX) &&
1036                     !path_cmp(LOCAL_FOLDER(folder)->rootpath, path))
1037                         return folder;
1038         }
1039
1040         return NULL;
1041 }
1042
1043 Folder *folder_find_from_name(const gchar *name, FolderClass *klass)
1044 {
1045         GList *list;
1046         Folder *folder;
1047
1048         for (list = folder_list; list != NULL; list = list->next) {
1049                 folder = list->data;
1050                 if (folder->klass == klass && 
1051                     strcmp2(name, folder->name) == 0)
1052                         return folder;
1053         }
1054
1055         return NULL;
1056 }
1057
1058 static gboolean folder_item_find_func(GNode *node, gpointer data)
1059 {
1060         FolderItem *item = node->data;
1061         gpointer *d = data;
1062         const gchar *path = d[0];
1063
1064         if (path_cmp(path, item->path) != 0)
1065                 return FALSE;
1066
1067         d[1] = item;
1068
1069         return TRUE;
1070 }
1071
1072 FolderItem *folder_find_item_from_path(const gchar *path)
1073 {
1074         Folder *folder;
1075         gpointer d[2];
1076
1077         folder = folder_get_default_folder();
1078         g_return_val_if_fail(folder != NULL, NULL);
1079
1080         d[0] = (gpointer)path;
1081         d[1] = NULL;
1082         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1083                         folder_item_find_func, d);
1084         return d[1];
1085 }
1086
1087 FolderItem *folder_find_child_item_by_name(FolderItem *item, const gchar *name)
1088 {
1089         GNode *node;
1090         FolderItem *child;
1091
1092         for (node = item->node->children; node != NULL; node = node->next) {
1093                 gchar *base;
1094                 child = FOLDER_ITEM(node->data);
1095                 base = g_path_get_basename(child->path);
1096                 if (strcmp2(base, name) == 0) {
1097                         g_free(base);
1098                         return child;
1099                 }
1100                 g_free(base);
1101         }
1102
1103         return NULL;
1104 }
1105
1106 FolderClass *folder_get_class_from_string(const gchar *str)
1107 {
1108         GSList *classlist;
1109
1110         classlist = folder_get_class_list();
1111         for (; classlist != NULL; classlist = g_slist_next(classlist)) {
1112                 FolderClass *class = (FolderClass *) classlist->data;
1113                 if (g_ascii_strcasecmp(class->idstr, str) == 0)
1114                         return class;
1115         }
1116
1117         return NULL;
1118 }
1119
1120 gchar *folder_get_identifier(Folder *folder)
1121 {
1122         gchar *type_str;
1123
1124         g_return_val_if_fail(folder != NULL, NULL);
1125
1126         type_str = folder->klass->idstr;
1127         return g_strconcat("#", type_str, "/", folder->name, NULL);
1128 }
1129
1130 gchar *folder_item_get_identifier(FolderItem *item)
1131 {
1132         gchar *id = NULL;
1133         gchar *folder_id = NULL;
1134
1135         g_return_val_if_fail(item != NULL, NULL);
1136         g_return_val_if_fail(item->path != NULL, NULL);
1137
1138         folder_id = folder_get_identifier(item->folder);
1139         id = g_strconcat(folder_id, "/", item->path, NULL);
1140         g_free(folder_id);
1141
1142         return id;
1143 }
1144
1145 FolderItem *folder_find_item_from_identifier(const gchar *identifier)
1146 {
1147         Folder *folder;
1148         gpointer d[2];
1149         gchar *str;
1150         gchar *p;
1151         gchar *name;
1152         gchar *path;
1153         FolderClass *class;
1154
1155         g_return_val_if_fail(identifier != NULL, NULL);
1156
1157         if (*identifier != '#')
1158                 return folder_find_item_from_path(identifier);
1159
1160         Xstrdup_a(str, identifier, return NULL);
1161
1162         p = strchr(str, '/');
1163         if (!p)
1164                 return folder_find_item_from_path(identifier);
1165         *p = '\0';
1166         p++;
1167         class = folder_get_class_from_string(&str[1]);
1168         if (class == NULL)
1169                 return folder_find_item_from_path(identifier);
1170
1171         name = p;
1172         p = strchr(p, '/');
1173         if (!p)
1174                 return folder_find_item_from_path(identifier);
1175         *p = '\0';
1176         p++;
1177
1178         folder = folder_find_from_name(name, class);
1179         if (!folder)
1180                 return folder_find_item_from_path(identifier);
1181
1182         path = p;
1183
1184         d[0] = (gpointer)path;
1185         d[1] = NULL;
1186         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1187                         folder_item_find_func, d);
1188         return d[1];
1189 }
1190
1191 /**
1192  * Get a displayable name for a FolderItem
1193  *
1194  * \param item FolderItem for that a name should be created
1195  * \return Displayable name for item, returned string has to
1196  *         be freed
1197  */
1198 gchar *folder_item_get_name(FolderItem *item)
1199 {
1200         gchar *name = NULL;
1201
1202         g_return_val_if_fail(item != NULL, g_strdup(""));
1203
1204         switch (item->stype) {
1205         case F_INBOX:
1206                 name = g_strdup(!strcmp2(item->name, INBOX_DIR) ? _("Inbox") :
1207                                 item->name);
1208                 break;
1209         case F_OUTBOX:
1210                 name = g_strdup(!strcmp2(item->name, OUTBOX_DIR) ? _("Sent") :
1211                                 item->name);
1212                 break;
1213         case F_QUEUE:
1214                 name = g_strdup(!strcmp2(item->name, QUEUE_DIR) ? _("Queue") :
1215                                 item->name);
1216                 break;
1217         case F_TRASH:
1218                 name = g_strdup(!strcmp2(item->name, TRASH_DIR) ? _("Trash") :
1219                                 item->name);
1220                 break;
1221         case F_DRAFT:
1222                 name = g_strdup(!strcmp2(item->name, DRAFT_DIR) ? _("Drafts") :
1223                                 item->name);
1224                 break;
1225         default:
1226                 break;
1227         }
1228
1229         if (name == NULL) {
1230                 /*
1231                  * should probably be done by a virtual function,
1232                  * the folder knows the ui string and how to abbrev
1233                 */
1234                 if (folder_item_parent(item) == NULL) {
1235                         name = g_strconcat(item->name, " (", item->folder->klass->uistr, ")", NULL);
1236                 } else {
1237                         if (FOLDER_CLASS(item->folder) == news_get_class() &&
1238                             item->path && !strcmp2(item->name, item->path))
1239                                 name = get_abbrev_newsgroup_name
1240                                         (item->path,
1241                                          prefs_common.ng_abbrev_len);
1242                         else
1243                                 name = g_strdup(item->name);
1244                 }
1245         }
1246
1247         if (name == NULL)
1248                 name = g_strdup("");
1249
1250         return name;
1251 }
1252
1253 Folder *folder_get_default_folder(void)
1254 {
1255         return folder_list ? FOLDER(folder_list->data) : NULL;
1256 }
1257
1258 FolderItem *folder_get_default_inbox(void)
1259 {
1260         GList *flist;
1261
1262         for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1263                 Folder * folder = FOLDER(flist->data);
1264
1265                 if (folder == NULL)
1266                         continue;
1267                 if (folder->inbox == NULL)
1268                         continue;
1269
1270                 return folder->inbox;
1271         }
1272
1273         return NULL;
1274 }
1275
1276 FolderItem *folder_get_default_outbox(void)
1277 {
1278         GList *flist;
1279
1280         for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1281                 Folder * folder = FOLDER(flist->data);
1282
1283                 if (folder == NULL)
1284                         continue;
1285                 if (folder->outbox == NULL)
1286                         continue;
1287
1288                 return folder->outbox;
1289         }
1290
1291         return NULL;
1292 }
1293
1294 FolderItem *folder_get_default_draft(void)
1295 {
1296         GList *flist;
1297
1298         for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1299                 Folder * folder = FOLDER(flist->data);
1300
1301                 if (folder == NULL)
1302                         continue;
1303                 if (folder->draft == NULL)
1304                         continue;
1305
1306                 return folder->draft;
1307         }
1308
1309         return NULL;
1310 }
1311
1312 FolderItem *folder_get_default_queue(void)
1313 {
1314         GList *flist;
1315
1316         for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1317                 Folder * folder = FOLDER(flist->data);
1318
1319                 if (folder == NULL)
1320                         continue;
1321                 if (folder->queue == NULL)
1322                         continue;
1323
1324                 return folder->queue;
1325         }
1326
1327         return NULL;
1328 }
1329
1330 FolderItem *folder_get_default_trash(void)
1331 {
1332         GList *flist;
1333
1334         for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1335                 Folder * folder = FOLDER(flist->data);
1336
1337                 if (folder == NULL)
1338                         continue;
1339                 if (folder->trash == NULL)
1340                         continue;
1341
1342                 return folder->trash;
1343         }
1344
1345         return NULL;
1346 }
1347
1348 #define CREATE_FOLDER_IF_NOT_EXIST(member, dir, type)           \
1349 {                                                               \
1350         if (!folder->member) {                                  \
1351                 item = folder_item_new(folder, dir, dir);       \
1352                 item->stype = type;                             \
1353                 folder_item_append(rootitem, item);             \
1354                 folder->member = item;                          \
1355         }                                                       \
1356 }
1357
1358 void folder_set_missing_folders(void)
1359 {
1360         Folder *folder;
1361         FolderItem *rootitem;
1362         FolderItem *item;
1363         GList *list;
1364
1365         for (list = folder_list; list != NULL; list = list->next) {
1366                 folder = list->data;
1367                 if (FOLDER_TYPE(folder) != F_MH) continue;
1368                 rootitem = FOLDER_ITEM(folder->node->data);
1369                 g_return_if_fail(rootitem != NULL);
1370
1371                 if (folder->inbox && folder->outbox && folder->draft &&
1372                     folder->queue && folder->trash)
1373                         continue;
1374
1375                 if (folder->klass->create_tree(folder) < 0) {
1376                         g_warning("%s: can't create the folder tree.\n",
1377                                   LOCAL_FOLDER(folder)->rootpath);
1378                         continue;
1379                 }
1380
1381                 CREATE_FOLDER_IF_NOT_EXIST(inbox,  INBOX_DIR,  F_INBOX);
1382                 CREATE_FOLDER_IF_NOT_EXIST(outbox, OUTBOX_DIR, F_OUTBOX);
1383                 CREATE_FOLDER_IF_NOT_EXIST(draft,  DRAFT_DIR,  F_DRAFT);
1384                 CREATE_FOLDER_IF_NOT_EXIST(queue,  QUEUE_DIR,  F_QUEUE);
1385                 CREATE_FOLDER_IF_NOT_EXIST(trash,  TRASH_DIR,  F_TRASH);
1386         }
1387 }
1388
1389 static gboolean folder_unref_account_func(GNode *node, gpointer data)
1390 {
1391         FolderItem *item = node->data;
1392         PrefsAccount *account = data;
1393
1394         if (item->account == account)
1395                 item->account = NULL;
1396
1397         return FALSE;
1398 }
1399
1400 void folder_unref_account_all(PrefsAccount *account)
1401 {
1402         Folder *folder;
1403         GList *list;
1404
1405         if (!account) return;
1406
1407         for (list = folder_list; list != NULL; list = list->next) {
1408                 folder = list->data;
1409                 if (folder->account == account)
1410                         folder->account = NULL;
1411                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1412                                 folder_unref_account_func, account);
1413         }
1414 }
1415
1416 #undef CREATE_FOLDER_IF_NOT_EXIST
1417
1418 gchar *folder_item_get_path(FolderItem *item)
1419 {
1420         Folder *folder;
1421
1422         g_return_val_if_fail(item != NULL, NULL);
1423         folder = item->folder;
1424         g_return_val_if_fail(folder != NULL, NULL);
1425
1426         return folder->klass->item_get_path(folder, item);
1427 }
1428
1429 void folder_item_set_default_flags(FolderItem *dest, MsgFlags *flags)
1430 {
1431         if (!(folder_has_parent_of_type(dest, F_OUTBOX) ||
1432               folder_has_parent_of_type(dest, F_QUEUE)  ||
1433               folder_has_parent_of_type(dest, F_DRAFT) ||
1434               folder_has_parent_of_type(dest, F_TRASH))) {
1435                 flags->perm_flags = MSG_NEW|MSG_UNREAD;
1436         } else {
1437                 flags->perm_flags = 0;
1438         }
1439         if (FOLDER_TYPE(dest->folder) == F_MH) {
1440                 if (folder_has_parent_of_type(dest, F_QUEUE)) {
1441                         MSG_SET_TMP_FLAGS(*flags, MSG_QUEUED);
1442                 } else if (folder_has_parent_of_type(dest, F_DRAFT)) {
1443                         MSG_SET_TMP_FLAGS(*flags, MSG_DRAFT);
1444                 }
1445         }
1446 }
1447
1448 static gint folder_sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1449 {
1450         MsgInfo *msginfo_a = (MsgInfo *) a;
1451         MsgInfo *msginfo_b = (MsgInfo *) b;
1452
1453         return (msginfo_a->msgnum - msginfo_b->msgnum);
1454 }
1455
1456 static gint folder_sort_folder_list(gconstpointer a, gconstpointer b)
1457 {
1458         guint gint_a = GPOINTER_TO_INT(a);
1459         guint gint_b = GPOINTER_TO_INT(b);
1460         
1461         return (gint_a - gint_b);
1462 }
1463
1464 void folder_item_process_open(FolderItem *item)
1465 {
1466         gchar *buf;
1467         if (item == NULL)
1468                 return;
1469         if((item->folder->klass->scan_required != NULL) &&
1470            (item->folder->klass->scan_required(item->folder, item))) {
1471                 folder_item_scan_full(item, TRUE);
1472         } else {
1473                 folder_item_syncronize_flags(item);
1474         }
1475         
1476         /* Processing */
1477         buf = g_strdup_printf(_("Processing (%s)...\n"), 
1478                               item->path ? item->path : item->name);
1479         debug_print("%s\n", buf);
1480         g_free(buf);
1481         
1482         folder_item_apply_processing(item);
1483
1484         debug_print("done.\n");
1485         return; 
1486 }
1487
1488 gint folder_item_open(FolderItem *item)
1489 {
1490         g_return_val_if_fail(item->no_select == FALSE, -1);
1491
1492         item->opened = TRUE;
1493
1494         return 0;
1495 }
1496
1497 gint folder_item_close(FolderItem *item)
1498 {
1499         GSList *mlist, *cur;
1500         Folder *folder;
1501         
1502         g_return_val_if_fail(item != NULL, -1);
1503         g_return_val_if_fail(item->no_select == FALSE, -1);
1504
1505         if (item->new_msgs) {
1506                 folder_item_update_freeze();
1507                 mlist = folder_item_get_msg_list(item);
1508                 for (cur = mlist ; cur != NULL ; cur = cur->next) {
1509                         MsgInfo * msginfo;
1510
1511                         msginfo = (MsgInfo *) cur->data;
1512                         if (MSG_IS_NEW(msginfo->flags))
1513                                 procmsg_msginfo_unset_flags(msginfo, MSG_NEW, 0);
1514                         procmsg_msginfo_free(msginfo);
1515                 }
1516                 g_slist_free(mlist);
1517                 folder_item_update_thaw();
1518         }               
1519
1520         folder_item_write_cache(item);
1521         
1522         folder_item_update(item, F_ITEM_UPDATE_MSGCNT);
1523
1524         item->opened = FALSE;
1525         folder = item->folder;
1526
1527         if (folder->klass->close == NULL)
1528                 return 0;
1529
1530         return folder->klass->close(folder, item);
1531 }
1532
1533 static MsgInfoList *get_msginfos(FolderItem *item, MsgNumberList *numlist)
1534 {
1535         MsgInfoList *msglist = NULL;
1536         Folder *folder = item->folder;
1537         g_return_val_if_fail(item->no_select == FALSE, NULL);
1538         
1539         if (folder->klass->get_msginfos != NULL)
1540                 msglist = folder->klass->get_msginfos(folder, item, numlist);
1541         else {
1542                 MsgNumberList *elem;
1543
1544                 for (elem = numlist; elem != NULL; elem = g_slist_next(elem)) {
1545                         MsgInfo *msginfo;
1546                         guint num;
1547
1548                         num = GPOINTER_TO_INT(elem->data);
1549                         msginfo = folder->klass->get_msginfo(folder, item, num);
1550                         if (msginfo != NULL)
1551                                 msglist = g_slist_prepend(msglist, msginfo);
1552                 }               
1553         }
1554
1555         return msglist;
1556 }
1557
1558 static MsgInfo *get_msginfo(FolderItem *item, guint num)
1559 {
1560         MsgNumberList numlist;
1561         MsgInfoList *msglist;
1562         MsgInfo *msginfo = NULL;
1563
1564         numlist.data = GINT_TO_POINTER(num);
1565         numlist.next = NULL;
1566         msglist = get_msginfos(item, &numlist);
1567         if (msglist != NULL)
1568                 msginfo = procmsg_msginfo_new_ref(msglist->data);
1569         procmsg_msg_list_free(msglist);
1570
1571         return msginfo;
1572 }
1573
1574 static gint syncronize_flags(FolderItem *item, MsgInfoList *msglist)
1575 {
1576         GRelation *relation;
1577         gint ret = 0;
1578         GSList *cur;
1579
1580         if(msglist == NULL)
1581                 return 0;
1582         if(item->folder->klass->get_flags == NULL)
1583                 return 0;
1584         g_return_val_if_fail(item->no_select == FALSE, 0);
1585
1586         relation = g_relation_new(2);
1587         g_relation_index(relation, 0, g_direct_hash, g_direct_equal);
1588         if ((ret = item->folder->klass->get_flags(
1589             item->folder, item, msglist, relation)) == 0) {
1590                 GTuples *tuples;
1591                 MsgInfo *msginfo;
1592                 MsgPermFlags permflags = 0;
1593                 gboolean skip;
1594
1595                 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1596                         msginfo = (MsgInfo *) cur->data;
1597                 
1598                         tuples = g_relation_select(relation, msginfo, 0);
1599                         skip = tuples->len < 1;
1600                         if (!skip)
1601                                 permflags = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1602                         g_tuples_destroy(tuples);
1603                         if (skip)
1604                                 continue;
1605                         
1606                         if (msginfo->flags.perm_flags != permflags) {
1607                                 procmsg_msginfo_change_flags(msginfo,
1608                                         permflags & ~msginfo->flags.perm_flags, 0,
1609                                         ~permflags & msginfo->flags.perm_flags, 0);
1610                         }
1611                 }
1612         }
1613         g_relation_destroy(relation);   
1614
1615         return ret;
1616 }
1617
1618 gint folder_item_scan_full(FolderItem *item, gboolean filtering)
1619 {
1620         Folder *folder;
1621         GSList *folder_list = NULL, *cache_list = NULL;
1622         GSList *folder_list_cur, *cache_list_cur, *new_list = NULL;
1623         GSList *exists_list = NULL, *elem;
1624         GSList *newmsg_list = NULL;
1625         guint newcnt = 0, unreadcnt = 0, totalcnt = 0;
1626         guint markedcnt = 0, unreadmarkedcnt = 0;
1627         guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
1628         gboolean update_flags = 0, old_uids_valid = FALSE;
1629     
1630         g_return_val_if_fail(item != NULL, -1);
1631         if (item->path == NULL) return -1;
1632
1633         folder = item->folder;
1634
1635         g_return_val_if_fail(folder != NULL, -1);
1636         g_return_val_if_fail(folder->klass->get_num_list != NULL, -1);
1637
1638         debug_print("Scanning folder %s for cache changes.\n", item->path);
1639
1640         /* Get list of messages for folder and cache */
1641         if (folder->klass->get_num_list(item->folder, item, &folder_list, &old_uids_valid) < 0) {
1642                 debug_print("Error fetching list of message numbers\n");
1643                 return(-1);
1644         }
1645
1646         if (old_uids_valid) {
1647                 if (!item->cache)
1648                         folder_item_read_cache(item);
1649                 cache_list = msgcache_get_msg_list(item->cache);
1650         } else {
1651                 if (item->cache)
1652                         msgcache_destroy(item->cache);
1653                 item->cache = msgcache_new();
1654                 cache_list = NULL;
1655         }
1656
1657         /* Sort both lists */
1658         cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
1659         folder_list = g_slist_sort(folder_list, folder_sort_folder_list);
1660
1661         cache_list_cur = cache_list;
1662         folder_list_cur = folder_list;
1663
1664         if (cache_list_cur != NULL) {
1665                 GSList *cache_list_last;
1666         
1667                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1668                 cache_list_last = g_slist_last(cache_list);
1669                 cache_max_num = ((MsgInfo *)cache_list_last->data)->msgnum;
1670         } else {
1671                 cache_cur_num = G_MAXINT;
1672                 cache_max_num = 0;
1673         }
1674
1675         if (folder_list_cur != NULL) {
1676                 GSList *folder_list_last;
1677         
1678                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1679                 folder_list_last = g_slist_last(folder_list);
1680                 folder_max_num = GPOINTER_TO_INT(folder_list_last->data);
1681         } else {
1682                 folder_cur_num = G_MAXINT;
1683                 folder_max_num = 0;
1684         }
1685
1686         while ((cache_cur_num != G_MAXINT) || (folder_cur_num != G_MAXINT)) {
1687                 /*
1688                  *  Message only exists in the folder
1689                  *  Remember message for fetching
1690                  */
1691                 if (folder_cur_num < cache_cur_num) {
1692                         gboolean add = FALSE;
1693
1694                         switch(FOLDER_TYPE(folder)) {
1695                                 case F_NEWS:
1696                                         if (folder_cur_num < cache_max_num)
1697                                                 break;
1698                                         
1699                                         if (folder->account->max_articles == 0) {
1700                                                 add = TRUE;
1701                                         }
1702
1703                                         if (folder_max_num <= folder->account->max_articles) {
1704                                                 add = TRUE;
1705                                         } else if (folder_cur_num > (folder_max_num - folder->account->max_articles)) {
1706                                                 add = TRUE;
1707                                         }
1708                                         break;
1709                                 default:
1710                                         add = TRUE;
1711                                         break;
1712                         }
1713                         
1714                         if (add) {
1715                                 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(folder_cur_num));
1716                                 debug_print("Remembered message %d for fetching\n", folder_cur_num);
1717                         }
1718
1719                         /* Move to next folder number */
1720                         folder_list_cur = folder_list_cur->next;
1721
1722                         if (folder_list_cur != NULL)
1723                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1724                         else
1725                                 folder_cur_num = G_MAXINT;
1726
1727                         continue;
1728                 }
1729
1730                 /*
1731                  *  Message only exists in the cache
1732                  *  Remove the message from the cache
1733                  */
1734                 if (cache_cur_num < folder_cur_num) {
1735                         msgcache_remove_msg(item->cache, cache_cur_num);
1736                         debug_print("Removed message %d from cache.\n", cache_cur_num);
1737
1738                         /* Move to next cache number */
1739                         cache_list_cur = cache_list_cur->next;
1740
1741                         if (cache_list_cur != NULL)
1742                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1743                         else
1744                                 cache_cur_num = G_MAXINT;
1745
1746                         update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1747
1748                         continue;
1749                 }
1750
1751                 /*
1752                  *  Message number exists in folder and cache!
1753                  *  Check if the message has been modified
1754                  */
1755                 if (cache_cur_num == folder_cur_num) {
1756                         MsgInfo *msginfo;
1757
1758                         msginfo = msgcache_get_msg(item->cache, folder_cur_num);
1759                         if (folder->klass->is_msg_changed && folder->klass->is_msg_changed(folder, item, msginfo)) {
1760                                 msgcache_remove_msg(item->cache, msginfo->msgnum);
1761                                 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(msginfo->msgnum));
1762                                 procmsg_msginfo_free(msginfo);
1763
1764                                 debug_print("Remembering message %d to update...\n", folder_cur_num);
1765                         } else
1766                                 exists_list = g_slist_prepend(exists_list, msginfo);
1767
1768                         /* Move to next folder and cache number */
1769                         cache_list_cur = cache_list_cur->next;
1770                         folder_list_cur = folder_list_cur->next;
1771
1772                         if (cache_list_cur != NULL)
1773                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1774                         else
1775                                 cache_cur_num = G_MAXINT;
1776
1777                         if (folder_list_cur != NULL)
1778                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1779                         else
1780                                 folder_cur_num = G_MAXINT;
1781
1782                         continue;
1783                 }
1784         }
1785         
1786         for(cache_list_cur = cache_list; cache_list_cur != NULL; cache_list_cur = g_slist_next(cache_list_cur))
1787                 procmsg_msginfo_free((MsgInfo *) cache_list_cur->data);
1788
1789         g_slist_free(cache_list);
1790         g_slist_free(folder_list);
1791
1792         if (new_list != NULL) {
1793                 GSList *tmp_list = NULL;
1794                 newmsg_list = get_msginfos(item, new_list);
1795                 g_slist_free(new_list);
1796                 tmp_list = g_slist_concat(g_slist_copy(exists_list), g_slist_copy(newmsg_list));
1797                 syncronize_flags(item, tmp_list);
1798                 g_slist_free(tmp_list);
1799         } else {
1800                 syncronize_flags(item, exists_list);
1801         }
1802
1803         folder_item_update_freeze();
1804         if (newmsg_list != NULL) {
1805                 GSList *elem;
1806                 int total = g_slist_length(newmsg_list), cur = 0;
1807                 
1808                 if ((filtering == TRUE) &&
1809                     (item->stype == F_INBOX) &&
1810                     (item->folder->account != NULL) && 
1811                     (item->folder->account->filter_on_recv)) 
1812                         statusbar_print_all(_("Filtering messages...\n"));
1813                 
1814                 for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
1815                         MsgInfo *msginfo = (MsgInfo *) elem->data;
1816
1817                         statusbar_progress_all(cur++,total, 10);
1818
1819                         msgcache_add_msg(item->cache, msginfo);
1820                         if ((filtering == TRUE) &&
1821                             (item->stype == F_INBOX) &&
1822                             (item->folder->account != NULL) && 
1823                             (item->folder->account->filter_on_recv) &&
1824                             procmsg_msginfo_filter(msginfo))
1825                                 procmsg_msginfo_free(msginfo);
1826                         else
1827                                 exists_list = g_slist_prepend(exists_list, msginfo);
1828                 }
1829                 g_slist_free(newmsg_list);
1830
1831                 statusbar_progress_all(0,0,0);
1832                 statusbar_pop_all();
1833                 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1834         }
1835
1836         folder_item_set_batch(item, TRUE);
1837         for (elem = exists_list; elem != NULL; elem = g_slist_next(elem)) {
1838                 MsgInfo *msginfo;
1839
1840                 msginfo = elem->data;
1841                 if (MSG_IS_IGNORE_THREAD(msginfo->flags) && (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1842                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1843                 if (!MSG_IS_IGNORE_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD)) {
1844                         procmsg_msginfo_change_flags(msginfo, MSG_IGNORE_THREAD, 0, MSG_NEW | MSG_UNREAD, 0);
1845                 }
1846                 if ((folder_has_parent_of_type(item, F_OUTBOX) ||
1847                      folder_has_parent_of_type(item, F_QUEUE)  ||
1848                      folder_has_parent_of_type(item, F_DRAFT)  ||
1849                      folder_has_parent_of_type(item, F_TRASH)) &&
1850                     (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1851                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1852                 if (MSG_IS_NEW(msginfo->flags))
1853                         newcnt++;
1854                 if (MSG_IS_UNREAD(msginfo->flags))
1855                         unreadcnt++;
1856                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1857                         unreadmarkedcnt++;
1858                 if (MSG_IS_MARKED(msginfo->flags))
1859                         markedcnt++;
1860
1861                 totalcnt++;
1862
1863                 procmsg_msginfo_free(msginfo);
1864         }
1865         folder_item_set_batch(item, FALSE);
1866         g_slist_free(exists_list);
1867
1868         item->new_msgs = newcnt;
1869         item->unread_msgs = unreadcnt;
1870         item->total_msgs = totalcnt;
1871         item->unreadmarked_msgs = unreadmarkedcnt;
1872         item->marked_msgs = markedcnt;
1873
1874         update_flags |= F_ITEM_UPDATE_MSGCNT;
1875
1876         folder_item_update(item, update_flags);
1877         folder_item_update_thaw();
1878
1879         return 0;
1880 }
1881
1882 gint folder_item_scan(FolderItem *item)
1883 {
1884         return folder_item_scan_full(item, TRUE);
1885 }
1886
1887 static gboolean folder_scan_all_items_func(GNode *node, gpointer data)
1888 {
1889         FolderItem *item = node->data;
1890
1891         folder_item_scan(item);
1892
1893         return FALSE;
1894 }
1895
1896 void folder_scan_all_items(Folder * folder)
1897 {
1898         g_node_traverse(folder->node, G_PRE_ORDER,
1899                         G_TRAVERSE_ALL, -1, folder_scan_all_items_func, NULL);
1900 }
1901
1902 static void folder_item_scan_foreach_func(gpointer key, gpointer val,
1903                                           gpointer data)
1904 {
1905         folder_item_scan(FOLDER_ITEM(key));
1906 }
1907
1908 void folder_item_scan_foreach(GHashTable *table)
1909 {
1910         g_hash_table_foreach(table, folder_item_scan_foreach_func, NULL);
1911 }
1912
1913 void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
1914 {
1915         gint *memusage = (gint *)data;
1916
1917         if (item->cache == NULL)
1918                 return;
1919         
1920         *memusage += msgcache_get_memory_usage(item->cache);
1921 }
1922
1923 gint folder_item_syncronize_flags(FolderItem *item)
1924 {
1925         MsgInfoList *msglist = NULL;
1926         GSList *cur;
1927         gint ret = 0;
1928         
1929         g_return_val_if_fail(item != NULL, -1);
1930         g_return_val_if_fail(item->folder != NULL, -1);
1931         g_return_val_if_fail(item->folder->klass != NULL, -1);
1932         g_return_val_if_fail(item->no_select == FALSE, -1);     
1933
1934         if (item->cache == NULL)
1935                 folder_item_read_cache(item);
1936         
1937         msglist = msgcache_get_msg_list(item->cache);
1938         
1939         ret = syncronize_flags(item, msglist);
1940
1941         for (cur = msglist; cur != NULL; cur = g_slist_next(cur))
1942                 procmsg_msginfo_free((MsgInfo *) cur->data);
1943         
1944         g_slist_free(msglist);
1945
1946         return ret;
1947 }
1948
1949 gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
1950 {
1951         FolderItem *fa = (FolderItem *)a;
1952         FolderItem *fb = (FolderItem *)b;
1953         
1954         return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
1955 }
1956
1957 void folder_find_expired_caches(FolderItem *item, gpointer data)
1958 {
1959         GSList **folder_item_list = (GSList **)data;
1960         gint difftime, expiretime;
1961         
1962         if (item->cache == NULL)
1963                 return;
1964
1965         if (item->opened > 0)
1966                 return;
1967
1968         difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
1969         expiretime = prefs_common.cache_min_keep_time * 60;
1970         debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
1971         if (difftime > expiretime) {
1972                 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
1973         }
1974 }
1975
1976 void folder_item_free_cache(FolderItem *item)
1977 {
1978         g_return_if_fail(item != NULL);
1979         
1980         if (item->cache == NULL)
1981                 return;
1982         
1983         if (item->opened > 0)
1984                 return;
1985
1986         folder_item_write_cache(item);
1987         msgcache_destroy(item->cache);
1988         item->cache = NULL;
1989 }
1990
1991 void folder_clean_cache_memory_force(void)
1992 {
1993         int old_cache_max_mem_usage = prefs_common.cache_max_mem_usage;
1994         int old_cache_min_keep_time = prefs_common.cache_min_keep_time;
1995
1996         prefs_common.cache_max_mem_usage = 0;
1997         prefs_common.cache_min_keep_time = 0;
1998
1999         folder_clean_cache_memory();
2000
2001         prefs_common.cache_max_mem_usage = old_cache_max_mem_usage;
2002         prefs_common.cache_min_keep_time = old_cache_min_keep_time;
2003 }
2004
2005 void folder_clean_cache_memory(void)
2006 {
2007         gint memusage = 0;
2008
2009         folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);       
2010         debug_print("Total cache memory usage: %d\n", memusage);
2011         
2012         if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
2013                 GSList *folder_item_list = NULL, *listitem;
2014                 
2015                 debug_print("Trying to free cache memory\n");
2016
2017                 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);      
2018                 listitem = folder_item_list;
2019                 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
2020                         FolderItem *item = (FolderItem *)(listitem->data);
2021
2022                         debug_print("Freeing cache memory for %s\n", item->path ? item->path : item->name);
2023                         memusage -= msgcache_get_memory_usage(item->cache);
2024                         folder_item_free_cache(item);
2025                         listitem = listitem->next;
2026                 }
2027                 g_slist_free(folder_item_list);
2028         }
2029 }
2030
2031 void folder_item_read_cache(FolderItem *item)
2032 {
2033         gchar *cache_file, *mark_file;
2034         
2035         g_return_if_fail(item != NULL);
2036
2037         if (item->path != NULL) {
2038                 cache_file = folder_item_get_cache_file(item);
2039                 mark_file = folder_item_get_mark_file(item);
2040                 item->cache = msgcache_read_cache(item, cache_file);
2041                 if (!item->cache) {
2042                         MsgInfoList *list, *cur;
2043                         guint newcnt = 0, unreadcnt = 0;
2044                         guint markedcnt = 0, unreadmarkedcnt = 0;
2045                         MsgInfo *msginfo;
2046
2047                         item->cache = msgcache_new();
2048                         folder_item_scan_full(item, TRUE);
2049
2050                         msgcache_read_mark(item->cache, mark_file);
2051
2052                         list = msgcache_get_msg_list(item->cache);
2053                         for (cur = list; cur != NULL; cur = g_slist_next(cur)) {
2054                                 msginfo = cur->data;
2055
2056                                 if (MSG_IS_NEW(msginfo->flags))
2057                                         newcnt++;
2058                                 if (MSG_IS_UNREAD(msginfo->flags))
2059                                         unreadcnt++;
2060                                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2061                                         unreadmarkedcnt++;
2062                                 if (MSG_IS_MARKED(msginfo->flags))
2063                                         markedcnt++;
2064                         }
2065                         item->new_msgs = newcnt;
2066                         item->unread_msgs = unreadcnt;
2067                         item->unreadmarked_msgs = unreadmarkedcnt;
2068                         item->marked_msgs = markedcnt;
2069                         procmsg_msg_list_free(list);
2070                 } else
2071                         msgcache_read_mark(item->cache, mark_file);
2072
2073                 g_free(cache_file);
2074                 g_free(mark_file);
2075         } else {
2076                 item->cache = msgcache_new();
2077         }
2078
2079         folder_clean_cache_memory();
2080 }
2081
2082 void folder_item_write_cache(FolderItem *item)
2083 {
2084         gchar *cache_file, *mark_file;
2085         FolderItemPrefs *prefs;
2086         gint filemode = 0;
2087         gchar *id;
2088         
2089         if (!item || !item->path || !item->cache)
2090                 return;
2091
2092         id = folder_item_get_identifier(item);
2093         debug_print("Save cache for folder %s\n", id);
2094         g_free(id);
2095
2096         cache_file = folder_item_get_cache_file(item);
2097         mark_file = folder_item_get_mark_file(item);
2098         if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
2099                 prefs = item->prefs;
2100                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
2101                         /* for cache file */
2102                         filemode = prefs->folder_chmod;
2103                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
2104                         if (filemode & S_IROTH) filemode |= S_IWOTH;
2105                         chmod(cache_file, filemode);
2106                 }
2107         }
2108
2109         g_free(cache_file);
2110         g_free(mark_file);
2111 }
2112
2113 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
2114 {
2115         Folder *folder;
2116         MsgInfo *msginfo = NULL;
2117         
2118         g_return_val_if_fail(item != NULL, NULL);
2119         g_return_val_if_fail(item->no_select == FALSE, NULL);   
2120         folder = item->folder;
2121         if (!item->cache)
2122                 folder_item_read_cache(item);
2123         
2124         if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
2125                 return msginfo;
2126         
2127         msginfo = get_msginfo(item, num);
2128         if (msginfo != NULL) {
2129                 msgcache_add_msg(item->cache, msginfo);
2130                 return msginfo;
2131         }
2132         
2133         return NULL;
2134 }
2135
2136 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
2137 {
2138         Folder *folder;
2139         MsgInfo *msginfo;
2140         
2141         g_return_val_if_fail(item != NULL, NULL);
2142         g_return_val_if_fail(msgid != NULL, NULL);
2143         g_return_val_if_fail(item->no_select == FALSE, FALSE);
2144         
2145         folder = item->folder;
2146         if (!item->cache)
2147                 folder_item_read_cache(item);
2148         
2149         if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
2150                 return msginfo;
2151
2152         return NULL;
2153 }
2154
2155 GSList *folder_item_get_msg_list(FolderItem *item)
2156 {
2157         g_return_val_if_fail(item != NULL, NULL);
2158         g_return_val_if_fail(item->no_select == FALSE, FALSE);
2159         
2160         if (item->cache == 0)
2161                 folder_item_read_cache(item);
2162
2163         g_return_val_if_fail(item->cache != NULL, NULL);
2164         
2165         return msgcache_get_msg_list(item->cache);
2166 }
2167
2168 void msginfo_set_mime_flags(GNode *node, gpointer data)
2169 {
2170         MsgInfo *msginfo = data;
2171         MimeInfo *mimeinfo = node->data;
2172         
2173         if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT) {
2174                 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2175         } else if (mimeinfo->disposition == DISPOSITIONTYPE_UNKNOWN && 
2176                  mimeinfo->type != MIMETYPE_TEXT &&
2177                  mimeinfo->type != MIMETYPE_MULTIPART) {
2178                 if (!mimeinfo->subtype 
2179                 ||  strcmp(mimeinfo->subtype, "pgp-signature"))
2180                         procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2181         }
2182
2183         /* don't descend below top level message for signed and encrypted info */
2184         if (mimeinfo->type == MIMETYPE_MESSAGE)
2185                 return;
2186
2187         if (privacy_mimeinfo_is_signed(mimeinfo)) {
2188                 procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
2189         }
2190
2191         if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
2192                 procmsg_msginfo_set_flags(msginfo, 0, MSG_ENCRYPTED);
2193         } else {
2194                 /* searching inside encrypted parts doesn't really make sense */
2195                 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2196         }
2197 }
2198
2199 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
2200 {
2201         Folder *folder;
2202         gchar *msgfile;
2203         MsgInfo *msginfo;
2204
2205         g_return_val_if_fail(item != NULL, NULL);
2206
2207         folder = item->folder;
2208
2209         g_return_val_if_fail(folder->klass->fetch_msg != NULL, NULL);
2210         g_return_val_if_fail(item->no_select == FALSE, NULL);
2211
2212         msgfile = folder->klass->fetch_msg(folder, item, num);
2213
2214         if (msgfile != NULL) {
2215                 msginfo = folder_item_get_msginfo(item, num);
2216                 if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
2217                         MimeInfo *mimeinfo;
2218
2219                         if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) && 
2220                             !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
2221                                 mimeinfo = procmime_scan_file(msgfile);
2222                         else
2223                                 mimeinfo = procmime_scan_queue_file(msgfile);
2224                         /* check for attachments */
2225                         if (mimeinfo != NULL) { 
2226                                 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2227                                 procmime_mimeinfo_free_all(mimeinfo);
2228
2229                                 procmsg_msginfo_set_flags(msginfo, 0, MSG_SCANNED);
2230                         }
2231                 }
2232         }
2233
2234         return msgfile;
2235 }
2236
2237 gchar *folder_item_fetch_msg_full(FolderItem *item, gint num, gboolean headers,
2238                                   gboolean body)
2239 {
2240         Folder *folder;
2241         gchar *msgfile;
2242         MsgInfo *msginfo;
2243
2244         g_return_val_if_fail(item != NULL, NULL);
2245         g_return_val_if_fail(item->no_select == FALSE, NULL);
2246         
2247         folder = item->folder;
2248
2249         if (folder->klass->fetch_msg_full == NULL)
2250                 return folder_item_fetch_msg(item, num);
2251
2252         msgfile = folder->klass->fetch_msg_full(folder, item, num, 
2253                                                 headers, body);
2254
2255         if (msgfile != NULL) {
2256                 msginfo = folder_item_get_msginfo(item, num);
2257                 if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
2258                         MimeInfo *mimeinfo;
2259
2260                         if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
2261                             !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
2262                                 mimeinfo = procmime_scan_file(msgfile);
2263                         else
2264                                 mimeinfo = procmime_scan_queue_file(msgfile);
2265                         /* check for attachments */
2266                         if (mimeinfo != NULL) { 
2267                                 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2268                                 procmime_mimeinfo_free_all(mimeinfo);
2269
2270                                 procmsg_msginfo_set_flags(msginfo, 0, MSG_SCANNED);
2271                         }
2272                 }
2273         }
2274
2275         return msgfile;
2276 }
2277
2278
2279 gint folder_item_fetch_all_msg(FolderItem *item)
2280 {
2281         Folder *folder;
2282         GSList *mlist;
2283         GSList *cur;
2284         gint num = 0;
2285         gint ret = 0;
2286         gint total = 0;
2287
2288         g_return_val_if_fail(item != NULL, -1);
2289         g_return_val_if_fail(item->no_select == FALSE, -1);
2290
2291         debug_print("fetching all messages in %s ...\n", item->path);
2292         statusbar_print_all(_("Fetching all messages in %s ...\n"), item->path);
2293
2294         folder = item->folder;
2295
2296         if (folder->ui_func)
2297                 folder->ui_func(folder, item, folder->ui_func_data ?
2298                                 folder->ui_func_data : GINT_TO_POINTER(num));
2299
2300         mlist = folder_item_get_msg_list(item);
2301
2302         total = g_slist_length(mlist);
2303
2304         for (cur = mlist; cur != NULL; cur = cur->next) {
2305                 MsgInfo *msginfo = (MsgInfo *)cur->data;
2306                 gchar *msg;
2307
2308                 statusbar_progress_all(num++,total, 10);
2309
2310                 if (folder->ui_func)
2311                         folder->ui_func(folder, item,
2312                                         folder->ui_func_data ?
2313                                         folder->ui_func_data :
2314                                         GINT_TO_POINTER(num));
2315
2316                 msg = folder_item_fetch_msg(item, msginfo->msgnum);
2317                 if (!msg) {
2318                         g_warning("Can't fetch message %d. Aborting.\n",
2319                                   msginfo->msgnum);
2320                         ret = -1;
2321                         break;
2322                 }
2323                 g_free(msg);
2324         }
2325         
2326         statusbar_progress_all(0,0,0);
2327         statusbar_pop_all();
2328         procmsg_msg_list_free(mlist);
2329
2330         return ret;
2331 }
2332
2333 static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
2334 {
2335         static HeaderEntry hentry[] = {{"Message-ID:",  NULL, TRUE},
2336                                        {NULL,           NULL, FALSE}};
2337         FILE *fp;
2338         MsgInfo *msginfo;
2339         gint msgnum = 0;
2340         gchar buf[BUFFSIZE];
2341
2342         if ((fp = g_fopen(file, "rb")) == NULL)
2343                 return 0;
2344
2345         if ((folder_has_parent_of_type(dest, F_QUEUE)) || 
2346             (folder_has_parent_of_type(dest, F_DRAFT)))
2347                 while (fgets(buf, sizeof(buf), fp) != NULL)
2348                         if (buf[0] == '\r' || buf[0] == '\n') break;
2349
2350         procheader_get_header_fields(fp, hentry);
2351         if (hentry[0].body) {
2352                 extract_parenthesis(hentry[0].body, '<', '>');
2353                 remove_space(hentry[0].body);
2354                 if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
2355                         msgnum = msginfo->msgnum;
2356                         procmsg_msginfo_free(msginfo);
2357
2358                         debug_print("found message as uid %d\n", msgnum);
2359                 }
2360         }
2361         
2362         g_free(hentry[0].body);
2363         hentry[0].body = NULL;
2364         fclose(fp);
2365
2366         return msgnum;
2367 }
2368
2369 static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
2370 {
2371         MsgPermFlags perm_flags = 0;
2372         MsgTmpFlags tmp_flags = 0;
2373
2374         /* create new flags */
2375         if (source != NULL) {
2376                 /* copy original flags */
2377                 perm_flags = source->flags.perm_flags;
2378                 tmp_flags = source->flags.tmp_flags;
2379         } else {
2380                 perm_flags = dest->flags.perm_flags;
2381                 tmp_flags = dest->flags.tmp_flags;
2382         }
2383
2384         /* remove new, unread and deleted in special folders */
2385         if (folder_has_parent_of_type(dest->folder, F_OUTBOX) || 
2386             folder_has_parent_of_type(dest->folder, F_QUEUE) || 
2387             folder_has_parent_of_type(dest->folder, F_DRAFT) || 
2388             folder_has_parent_of_type(dest->folder, F_TRASH))
2389                 perm_flags &= ~(MSG_NEW | MSG_UNREAD | MSG_DELETED);
2390
2391         /* set ignore flag of ignored parent exists */
2392         if (procmsg_msg_has_flagged_parent(dest, MSG_IGNORE_THREAD))
2393                 perm_flags |= MSG_IGNORE_THREAD;
2394
2395         /* Unset tmp flags that should not be copied */
2396         tmp_flags &= ~(MSG_MOVE | MSG_COPY);
2397
2398         /* unset flags that are set but should not */
2399         /* and set new flags */
2400         procmsg_msginfo_change_flags(dest,
2401                                   ~dest->flags.perm_flags & perm_flags,
2402                                   ~dest->flags.tmp_flags  & tmp_flags,
2403                                    dest->flags.perm_flags & ~perm_flags,
2404                                    dest->flags.tmp_flags  & ~tmp_flags);
2405 }
2406
2407 static void add_msginfo_to_cache(FolderItem *item, MsgInfo *newmsginfo, MsgInfo *flagsource)
2408 {
2409         /* update folder stats */
2410         if (MSG_IS_NEW(newmsginfo->flags))
2411                 item->new_msgs++;
2412         if (MSG_IS_UNREAD(newmsginfo->flags))
2413                 item->unread_msgs++;
2414         if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
2415                 item->unreadmarked_msgs++;
2416         if (MSG_IS_MARKED(newmsginfo->flags))
2417                 item->marked_msgs++;
2418         item->total_msgs++;
2419
2420         folder_item_update_freeze();
2421         msgcache_add_msg(item->cache, newmsginfo);
2422         copy_msginfo_flags(flagsource, newmsginfo);
2423         folder_item_update_with_msg(item,  F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_ADDMSG, newmsginfo);
2424         folder_item_update_thaw();
2425 }
2426
2427 static void remove_msginfo_from_cache(FolderItem *item, MsgInfo *msginfo)
2428 {
2429         MsgInfoUpdate msginfo_update;
2430
2431         if (!item->cache)
2432                 folder_item_read_cache(item);
2433
2434         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2435                 msginfo->folder->new_msgs--;
2436         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2437                 msginfo->folder->unread_msgs--;
2438         if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2439                 msginfo->folder->unreadmarked_msgs--;
2440         if (MSG_IS_MARKED(msginfo->flags))
2441                 item->marked_msgs--;
2442
2443         msginfo->folder->total_msgs--;
2444
2445         msginfo_update.msginfo = msginfo;
2446         msginfo_update.flags = MSGINFO_UPDATE_DELETED;
2447         hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
2448
2449         msgcache_remove_msg(item->cache, msginfo->msgnum);
2450         folder_item_update_with_msg(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_REMOVEMSG, msginfo);
2451 }
2452
2453 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
2454                          MsgFlags *flags, gboolean remove_source)
2455 {
2456         GSList file_list;
2457         MsgFileInfo fileinfo;
2458
2459         g_return_val_if_fail(dest != NULL, -1);
2460         g_return_val_if_fail(file != NULL, -1);
2461  
2462         fileinfo.msginfo = NULL;
2463         fileinfo.file = (gchar *)file;
2464         fileinfo.flags = flags;
2465         file_list.data = &fileinfo;
2466         file_list.next = NULL;
2467
2468         return folder_item_add_msgs(dest, &file_list, remove_source);
2469 }
2470
2471 gint folder_item_add_msgs(FolderItem *dest, GSList *file_list,
2472                           gboolean remove_source)
2473 {
2474         Folder *folder;
2475         gint ret, num, lastnum = -1;
2476         GSList *file_cur;
2477         GRelation *relation;
2478         MsgFileInfo *fileinfo = NULL;
2479         gboolean folderscan = FALSE;
2480
2481         g_return_val_if_fail(dest != NULL, -1);
2482         g_return_val_if_fail(file_list != NULL, -1);
2483         g_return_val_if_fail(dest->folder != NULL, -1);
2484         g_return_val_if_fail(dest->no_select == FALSE, -1);
2485
2486         folder = dest->folder;
2487
2488         relation = g_relation_new(2);
2489         g_relation_index(relation, 0, g_direct_hash, g_direct_equal);
2490
2491         if (folder->klass->add_msgs != NULL) {
2492                 ret = folder->klass->add_msgs(folder, dest, file_list, relation);
2493                 if (ret < 0) {
2494                         g_relation_destroy(relation);
2495                         return ret;
2496                 }
2497         } else {
2498                 for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
2499                         fileinfo = (MsgFileInfo *) file_cur->data;
2500
2501                         ret = folder->klass->add_msg(folder, dest, fileinfo->file, fileinfo->flags);
2502                         if (ret < 0) {
2503                                 g_relation_destroy(relation);
2504                                 return ret;
2505                         }
2506                         g_relation_insert(relation, fileinfo, GINT_TO_POINTER(ret));
2507                 }
2508         }
2509
2510         for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
2511                 GTuples *tuples;
2512
2513                 fileinfo = (MsgFileInfo *) file_cur->data;
2514                 tuples = g_relation_select(relation, fileinfo, 0);
2515                 num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
2516                 g_tuples_destroy(tuples);
2517
2518                 if (num >= 0) {
2519                         MsgInfo *newmsginfo;
2520
2521                         if (num == 0) {
2522                                 if (!folderscan) {
2523                                         folder_item_scan_full(dest, FALSE);
2524                                         folderscan = TRUE;
2525                                 }
2526                                 num = folder_item_get_msg_num_by_file(dest, fileinfo->file);
2527                         }
2528
2529                         if (num > lastnum)
2530                                 lastnum = num;
2531
2532                         if (num >= 0 && remove_source) {
2533                                 if (g_unlink(fileinfo->file) < 0)
2534                                         FILE_OP_ERROR(fileinfo->file, "unlink");
2535                         }
2536
2537                         if (num == 0)
2538                                 continue;
2539
2540                         if (!folderscan && 
2541                             ((newmsginfo = get_msginfo(dest, num)) != NULL)) {
2542                                 add_msginfo_to_cache(dest, newmsginfo, NULL);
2543                                 procmsg_msginfo_free(newmsginfo);
2544                         } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
2545                                 /* TODO: set default flags */
2546                                 procmsg_msginfo_free(newmsginfo);
2547                         }
2548                 }
2549         }
2550
2551         g_relation_destroy(relation);
2552
2553         return lastnum;
2554 }
2555                 
2556 FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest) 
2557 {
2558         GSList *mlist;
2559         FolderItem *new_item;
2560         FolderItem *next_item;
2561         GNode *srcnode;
2562         gchar *old_id, *new_id;
2563
2564         /* move messages */
2565         debug_print("Moving %s to %s\n", src->path, dest->path);
2566         new_item = folder_create_folder(dest, src->name);
2567         if (new_item == NULL) {
2568                 printf("Can't create folder\n");
2569                 return NULL;
2570         }
2571         
2572         if (new_item->folder == NULL)
2573                 new_item->folder = dest->folder;
2574
2575         /* move messages */
2576         log_message(_("Moving %s to %s...\n"), 
2577                         src->name, new_item->path);
2578
2579         mlist = folder_item_get_msg_list(src);
2580         
2581         if (mlist != NULL) {
2582                 folder_item_move_msgs(new_item, mlist);
2583                 procmsg_msg_list_free(mlist);
2584         }
2585         
2586         /*copy prefs*/
2587         folder_item_prefs_copy_prefs(src, new_item);
2588         new_item->collapsed = src->collapsed;
2589         new_item->thread_collapsed = src->thread_collapsed;
2590         new_item->threaded  = src->threaded;
2591         new_item->ret_rcpt  = src->ret_rcpt;
2592         new_item->hide_read_msgs = src->hide_read_msgs;
2593         new_item->sort_key  = src->sort_key;
2594         new_item->sort_type = src->sort_type;
2595
2596         prefs_matcher_write_config();
2597         
2598         /* recurse */
2599         srcnode = src->folder->node;    
2600         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
2601         srcnode = srcnode->children;
2602         while (srcnode != NULL) {
2603                 if (srcnode && srcnode->data) {
2604                         next_item = (FolderItem*) srcnode->data;
2605                         srcnode = srcnode->next;
2606                         if (folder_item_move_recursive(next_item, new_item) == NULL) {
2607                                 return NULL;
2608                         }
2609                 }
2610         }
2611         old_id = folder_item_get_identifier(src);
2612         new_id = folder_item_get_identifier(new_item);
2613         debug_print("updating rules : %s => %s\n", old_id, new_id);
2614
2615         /* if src supports removing, otherwise only copy folder */
2616         if (src->folder->klass->remove_folder != NULL)  
2617                 src->folder->klass->remove_folder(src->folder, src);
2618         folder_write_list();
2619
2620         if (old_id != NULL && new_id != NULL)
2621                 prefs_filtering_rename_path(old_id, new_id);
2622         g_free(old_id);
2623         g_free(new_id);
2624         
2625         return new_item;
2626 }
2627
2628 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item)
2629 {
2630         FolderItem *tmp = folder_item_parent(dest);
2631         gchar * src_identifier, * dst_identifier;
2632         gchar * phys_srcpath, * phys_dstpath;
2633         
2634         while (tmp) {
2635                 if (tmp == src) {
2636                         return F_MOVE_FAILED_DEST_IS_CHILD;
2637                 }
2638                 tmp = folder_item_parent(tmp);
2639         }
2640         
2641         tmp = folder_item_parent(src);
2642         
2643         src_identifier = folder_item_get_identifier(src);
2644         dst_identifier = folder_item_get_identifier(dest);
2645         
2646         if(dst_identifier == NULL && dest->folder && folder_item_parent(dest) == NULL) {
2647                 /* dest can be a root folder */
2648                 dst_identifier = folder_get_identifier(dest->folder);
2649         }
2650         if (src_identifier == NULL || dst_identifier == NULL) {
2651                 debug_print("Can't get identifiers\n");
2652                 return F_MOVE_FAILED;
2653         }
2654
2655         if (src->folder != dest->folder) {
2656                 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
2657         }
2658
2659         phys_srcpath = folder_item_get_path(src);
2660         phys_dstpath = g_strconcat(folder_item_get_path(dest),
2661                        G_DIR_SEPARATOR_S,
2662                        g_path_get_basename(phys_srcpath),
2663                        NULL);
2664
2665         if (folder_item_parent(src) == dest || src == dest) {
2666                 g_free(src_identifier);
2667                 g_free(dst_identifier);
2668                 g_free(phys_srcpath);
2669                 g_free(phys_dstpath);
2670                 return F_MOVE_FAILED_DEST_IS_PARENT;
2671         }
2672         debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
2673         if ((tmp = folder_item_move_recursive(src, dest)) == NULL) {
2674                 return F_MOVE_FAILED;
2675         }
2676         
2677         g_free(src_identifier);
2678         g_free(dst_identifier);
2679         g_free(phys_srcpath);
2680         g_free(phys_dstpath);
2681
2682         *new_item = tmp;
2683
2684         return F_MOVE_OK;
2685 }
2686
2687 /**
2688  * Copy a list of message to a new folder and remove
2689  * source messages if wanted
2690  */
2691 static gint do_copy_msgs(FolderItem *dest, GSList *msglist, gboolean remove_source)
2692 {
2693         Folder *folder;
2694         GSList *l;
2695         gint num, lastnum = -1;
2696         gboolean folderscan = FALSE;
2697         GRelation *relation;
2698         GSList *not_moved = NULL;
2699
2700         g_return_val_if_fail(dest != NULL, -1);
2701         g_return_val_if_fail(msglist != NULL, -1);
2702
2703         folder = dest->folder;
2704
2705         g_return_val_if_fail(folder->klass->copy_msg != NULL, -1);
2706         g_return_val_if_fail(dest->no_select == FALSE, -1);
2707
2708         relation = g_relation_new(2);
2709         g_relation_index(relation, 0, g_direct_hash, g_direct_equal);
2710         g_relation_index(relation, 1, g_direct_hash, g_direct_equal);
2711
2712         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
2713                 MsgInfo * msginfo = (MsgInfo *) l->data;
2714
2715                 if (msginfo->planned_download != 0) {
2716                         int old_planned = msginfo->planned_download;
2717                         partial_unmark(msginfo);
2718                         /* little hack to reenable after */
2719                         msginfo->planned_download = old_planned;
2720                 }
2721         }
2722
2723         /* 
2724          * Copy messages to destination folder and 
2725          * store new message numbers in newmsgnums
2726          */
2727         if (folder->klass->copy_msgs != NULL) {
2728                 if (folder->klass->copy_msgs(folder, dest, msglist, relation) < 0) {
2729                         g_relation_destroy(relation);
2730                         return -1;
2731                 }
2732         } else {
2733                 for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
2734                         MsgInfo * msginfo = (MsgInfo *) l->data;
2735
2736                         num = folder->klass->copy_msg(folder, dest, msginfo);
2737                         if (num > 0)
2738                                 g_relation_insert(relation, msginfo, GINT_TO_POINTER(num));
2739                         else
2740                                 not_moved = g_slist_prepend(not_moved, msginfo);
2741                 }
2742         }
2743
2744         /* Read cache for dest folder */
2745         if (!dest->cache) folder_item_read_cache(dest);
2746
2747         /* 
2748          * Fetch new MsgInfos for new messages in dest folder,
2749          * add them to the msgcache and update folder message counts
2750          */
2751         if (g_relation_count(relation, GINT_TO_POINTER(0), 1) > 0) {
2752                 folder_item_scan_full(dest, FALSE);
2753                 folderscan = TRUE;
2754         }
2755
2756         for (l = msglist; l != NULL; l = g_slist_next(l)) {
2757                 MsgInfo *msginfo = (MsgInfo *) l->data;
2758                 GTuples *tuples;
2759
2760                 tuples = g_relation_select(relation, msginfo, 0);
2761                 num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
2762                 g_tuples_destroy(tuples);
2763
2764                 if (num >= 0) {
2765                         MsgInfo *newmsginfo = NULL;
2766
2767                         if (folderscan) {
2768                                 if (msginfo->msgid != NULL) {
2769                                         newmsginfo = folder_item_get_msginfo_by_msgid(dest, msginfo->msgid);
2770                                         if (newmsginfo != NULL) {
2771                                                 copy_msginfo_flags(msginfo, newmsginfo);
2772                                                 num = newmsginfo->msgnum;
2773                                         }
2774                                 }
2775                         } else {
2776                                 newmsginfo = get_msginfo(dest, num);
2777                                 if (newmsginfo != NULL) {
2778                                         add_msginfo_to_cache(dest, newmsginfo, msginfo);
2779                                 }
2780                         }
2781
2782                         if (msginfo->planned_download 
2783                             == POP3_PARTIAL_DLOAD_DELE) {
2784                                 partial_mark_for_delete(newmsginfo);
2785                         }
2786                         if (msginfo->planned_download 
2787                             == POP3_PARTIAL_DLOAD_DLOAD) {
2788                                 partial_mark_for_download(newmsginfo);
2789                         }
2790                         procmsg_msginfo_free(newmsginfo);
2791
2792
2793                         if (num > lastnum)
2794                                 lastnum = num;
2795                 }
2796         }
2797
2798         if (remove_source) {
2799                 MsgInfo *msginfo = (MsgInfo *) msglist->data;
2800                 FolderItem *item = msginfo->folder;
2801                 /*
2802                  * Remove source messages from their folders if
2803                  * copying was successfull and update folder
2804                  * message counts
2805                  */
2806                 if (item->folder->klass->remove_msgs) {
2807                         item->folder->klass->remove_msgs(item->folder,
2808                                                                 msginfo->folder,
2809                                                                 msglist,
2810                                                                 relation);
2811                 }
2812                 for (l = msglist; l != NULL; l = g_slist_next(l)) {
2813                         GTuples *tuples;
2814                         msginfo = (MsgInfo *) l->data;
2815                         item = msginfo->folder;
2816
2817                         tuples = g_relation_select(relation, msginfo, 0);
2818                         num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
2819                         g_tuples_destroy(tuples);
2820
2821                         if (g_slist_find(not_moved, msginfo))
2822                                 continue;
2823
2824                         if ((num >= 0) && (item->folder->klass->remove_msg != NULL)) {
2825                                 if (!item->folder->klass->remove_msgs)
2826                                         item->folder->klass->remove_msg(item->folder,
2827                                                                 msginfo->folder,
2828                                                                 msginfo->msgnum);
2829                                 remove_msginfo_from_cache(item, msginfo);
2830                         }
2831                 }
2832         }
2833
2834         g_relation_destroy(relation);
2835         if (not_moved != NULL) {
2836                 g_slist_free(not_moved);
2837                 return -1;
2838         } else
2839                 return lastnum;
2840 }
2841
2842 /**
2843  * Move a message to a new folder.
2844  *
2845  * \param dest Destination folder
2846  * \param msginfo The message
2847  */
2848 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
2849 {
2850         GSList list;
2851
2852         g_return_val_if_fail(dest != NULL, -1);
2853         g_return_val_if_fail(msginfo != NULL, -1);
2854
2855         list.data = msginfo;
2856         list.next = NULL;
2857
2858         return do_copy_msgs(dest, &list, TRUE);
2859 }
2860
2861 /**
2862  * Move a list of messages to a new folder.
2863  *
2864  * \param dest Destination folder
2865  * \param msglist List of messages
2866  */
2867 gint folder_item_move_msgs(FolderItem *dest, GSList *msglist)
2868 {
2869         gint result = -1;
2870         g_return_val_if_fail(dest != NULL, -1);
2871         g_return_val_if_fail(msglist != NULL, -1);
2872         inc_lock();
2873         result = do_copy_msgs(dest, msglist, TRUE);
2874         inc_unlock();
2875         return result;
2876 }
2877
2878 /**
2879  * Copy a message to a new folder.
2880  *
2881  * \param dest Destination folder
2882  * \param msginfo The message
2883  */
2884 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
2885 {
2886         GSList list;
2887
2888         g_return_val_if_fail(dest != NULL, -1);
2889         g_return_val_if_fail(msginfo != NULL, -1);
2890     
2891         list.data = msginfo;
2892         list.next = NULL;
2893
2894         return do_copy_msgs(dest, &list, FALSE);
2895 }
2896
2897 /**
2898  * Copy a list of messages to a new folder.
2899  *
2900  * \param dest Destination folder
2901  * \param msglist List of messages
2902  */
2903 gint folder_item_copy_msgs(FolderItem *dest, GSList *msglist)
2904 {
2905         gint result;
2906         g_return_val_if_fail(dest != NULL, -1);
2907         g_return_val_if_fail(msglist != NULL, -1);
2908
2909         inc_lock();
2910         result = do_copy_msgs(dest, msglist, FALSE);
2911         inc_unlock();
2912         
2913         return result;
2914 }
2915
2916 gint folder_item_remove_msg(FolderItem *item, gint num)
2917 {
2918         Folder *folder;
2919         gint ret;
2920         MsgInfo *msginfo;
2921
2922         g_return_val_if_fail(item != NULL, -1);
2923         folder = item->folder;
2924         g_return_val_if_fail(folder->klass->remove_msg != NULL, -1);
2925         g_return_val_if_fail(item->no_select == FALSE, -1);
2926
2927         if (!item->cache) folder_item_read_cache(item);
2928
2929         ret = folder->klass->remove_msg(folder, item, num);
2930
2931         msginfo = msgcache_get_msg(item->cache, num);
2932         if (msginfo != NULL) {
2933                 remove_msginfo_from_cache(item, msginfo);
2934                 procmsg_msginfo_free(msginfo);
2935         }
2936
2937         return ret;
2938 }
2939
2940 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
2941 {
2942         Folder *folder;
2943         gint ret = 0;
2944
2945         g_return_val_if_fail(item != NULL, -1);
2946         folder = item->folder;
2947         g_return_val_if_fail(folder != NULL, -1);
2948         g_return_val_if_fail(item->no_select == FALSE, -1);
2949         inc_lock();
2950         if (!item->cache) folder_item_read_cache(item);
2951
2952         folder_item_update_freeze();
2953         if (item->folder->klass->remove_msgs) {
2954                 ret = item->folder->klass->remove_msgs(item->folder,
2955                                                         item,
2956                                                         msglist,
2957                                                         NULL);
2958         }
2959         while (ret == 0 && msglist != NULL) {
2960                 MsgInfo *msginfo = (MsgInfo *)msglist->data;
2961                 if (MSG_IS_LOCKED(msginfo->flags)) {
2962                         msglist = msglist->next;
2963                         continue;
2964                 }
2965                 if (!item->folder->klass->remove_msgs)
2966                         ret = folder_item_remove_msg(item, msginfo->msgnum);
2967                 if (ret != 0) break;
2968                 msgcache_remove_msg(item->cache, msginfo->msgnum);
2969                 msglist = msglist->next;
2970         }
2971         folder_item_scan_full(item, FALSE);
2972         folder_item_update_thaw();
2973         inc_unlock();
2974         return ret;
2975 }
2976
2977 gint folder_item_remove_all_msg(FolderItem *item)
2978 {
2979         Folder *folder;
2980         gint result;
2981
2982         g_return_val_if_fail(item != NULL, -1);
2983         g_return_val_if_fail(item->no_select == FALSE, -1);
2984
2985         folder = item->folder;
2986
2987         inc_lock();
2988         if (folder->klass->remove_all_msg != NULL) {
2989                 result = folder->klass->remove_all_msg(folder, item);
2990
2991                 if (result == 0) {
2992                         folder_item_free_cache(item);
2993                         item->cache = msgcache_new();
2994                 }
2995         } else {
2996                 MsgInfoList *msglist;
2997
2998                 msglist = folder_item_get_msg_list(item);
2999                 result = folder_item_remove_msgs(item, msglist);
3000                 procmsg_msg_list_free(msglist);
3001         }
3002
3003         if (result == 0) {
3004                 item->new_msgs = 0;
3005                 item->unread_msgs = 0;
3006                 item->unreadmarked_msgs = 0;
3007                 item->marked_msgs = 0;
3008                 item->total_msgs = 0;
3009                 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
3010         }
3011
3012         inc_unlock();
3013         return result;
3014 }
3015
3016 void folder_item_change_msg_flags(FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
3017 {
3018         g_return_if_fail(item != NULL);
3019         g_return_if_fail(msginfo != NULL);
3020         g_return_if_fail(item->no_select == FALSE);
3021         
3022         if (item->folder->klass->change_flags != NULL) {
3023                 item->folder->klass->change_flags(item->folder, item, msginfo, newflags);
3024         } else {
3025                 msginfo->flags.perm_flags = newflags;
3026         }
3027 }
3028
3029 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
3030 {
3031         Folder *folder;
3032
3033         g_return_val_if_fail(item != NULL, FALSE);
3034         g_return_val_if_fail(item->no_select == FALSE, FALSE);
3035
3036         folder = item->folder;
3037
3038         g_return_val_if_fail(folder->klass->is_msg_changed != NULL, -1);
3039
3040         return folder->klass->is_msg_changed(folder, item, msginfo);
3041 }
3042
3043 gchar *folder_item_get_cache_file(FolderItem *item)
3044 {
3045         gchar *path;
3046         gchar *file;
3047
3048         g_return_val_if_fail(item != NULL, NULL);
3049         g_return_val_if_fail(item->path != NULL, NULL);
3050
3051         path = folder_item_get_path(item);
3052         g_return_val_if_fail(path != NULL, NULL);
3053         if (!is_dir_exist(path))
3054                 make_dir_hier(path);
3055         file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
3056         g_free(path);
3057
3058         return file;
3059 }
3060
3061 gchar *folder_item_get_mark_file(FolderItem *item)
3062 {
3063         gchar *path;
3064         gchar *file;
3065
3066         g_return_val_if_fail(item != NULL, NULL);
3067         g_return_val_if_fail(item->path != NULL, NULL);
3068
3069         path = folder_item_get_path(item);
3070         g_return_val_if_fail(path != NULL, NULL);
3071         if (!is_dir_exist(path))
3072                 make_dir_hier(path);
3073         file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
3074         g_free(path);
3075
3076         return file;
3077 }
3078
3079 static gpointer xml_to_folder_item(gpointer nodedata, gpointer data)
3080 {
3081         XMLNode *xmlnode = (XMLNode *) nodedata;
3082         Folder *folder = (Folder *) data;
3083         FolderItem *item;
3084
3085         g_return_val_if_fail(xmlnode != NULL, NULL);
3086         g_return_val_if_fail(folder != NULL, NULL);
3087
3088         if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
3089                 g_warning("tag name != \"folderitem\"\n");
3090                 return NULL;
3091         }
3092
3093         item = folder_item_new(folder, "", "");
3094         if (folder->klass->item_set_xml != NULL)
3095                 folder->klass->item_set_xml(folder, item, xmlnode->tag);
3096         else
3097                 folder_item_set_xml(folder, item, xmlnode->tag);
3098
3099         item->folder = folder;
3100
3101         switch (item->stype) {
3102         case F_INBOX:  folder->inbox  = item; break;
3103         case F_OUTBOX: folder->outbox = item; break;
3104         case F_DRAFT:  folder->draft  = item; break;
3105         case F_QUEUE:  folder->queue  = item; break;
3106         case F_TRASH:  folder->trash  = item; break;
3107         default:       break;
3108         }
3109         folder_item_prefs_read_config(item);
3110
3111         return item;
3112 }
3113
3114 static gboolean folder_item_set_node(GNode *node, gpointer data)
3115 {
3116         FolderItem *item = (FolderItem *) node->data;
3117         item->node = node;
3118
3119         return FALSE;
3120 }
3121
3122 static Folder *folder_get_from_xml(GNode *node)
3123 {
3124         Folder *folder;
3125         XMLNode *xmlnode;
3126         GList *list;
3127         FolderClass *klass = NULL;
3128         GNode *cur;
3129
3130         g_return_val_if_fail(node->data != NULL, NULL);
3131
3132         xmlnode = node->data;
3133         if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
3134                 g_warning("tag name != \"folder\"\n");
3135                 return NULL;
3136         }
3137         list = xmlnode->tag->attr;
3138         for (; list != NULL; list = list->next) {
3139                 XMLAttr *attr = list->data;
3140
3141                 if (!attr || !attr->name || !attr->value) continue;
3142                 if (!strcmp(attr->name, "type"))
3143                         klass = folder_get_class_from_string(attr->value);
3144         }
3145         if (klass == NULL)
3146                 return NULL;
3147
3148         folder = folder_new(klass, "", "");
3149         g_return_val_if_fail(folder != NULL, NULL);
3150
3151         if (klass->set_xml)
3152                 klass->set_xml(folder, xmlnode->tag);
3153         else
3154                 folder_set_xml(folder, xmlnode->tag);
3155
3156         cur = node->children;
3157         while (cur != NULL) {
3158                 GNode *itemnode;
3159
3160                 itemnode = g_node_map(cur, xml_to_folder_item, (gpointer) folder);
3161                 g_node_append(folder->node, itemnode);
3162                 cur = cur->next;
3163         }
3164         g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, folder_item_set_node, NULL);
3165         
3166         return folder;
3167 }
3168
3169 static gchar *folder_get_list_path(void)
3170 {
3171         static gchar *filename = NULL;
3172
3173         if (!filename)
3174                 filename =  g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
3175                                         FOLDER_LIST, NULL);
3176
3177         return filename;
3178 }
3179
3180 #define PUT_ESCAPE_STR(fp, attr, str)                   \
3181 {                                                       \
3182         fputs(" " attr "=\"", fp);                      \
3183         xml_file_put_escape_str(fp, str);               \
3184         fputs("\"", fp);                                \
3185 }
3186
3187 static gpointer folder_item_to_xml(gpointer nodedata, gpointer data)
3188 {
3189         FolderItem *item = (FolderItem *) nodedata;
3190         XMLTag *tag;
3191
3192         g_return_val_if_fail(item != NULL, NULL);
3193
3194         if (item->folder->klass->item_get_xml != NULL)
3195                 tag = item->folder->klass->item_get_xml(item->folder, item);
3196         else
3197                 tag = folder_item_get_xml(item->folder, item);
3198
3199         return xml_node_new(tag, NULL);
3200 }
3201
3202 static GNode *folder_get_xml_node(Folder *folder)
3203 {
3204         GNode *node;
3205         XMLNode *xmlnode;
3206         XMLTag *tag;
3207
3208         g_return_val_if_fail(folder != NULL, NULL);
3209
3210         if (folder->klass->get_xml != NULL)
3211                 tag = folder->klass->get_xml(folder);
3212         else
3213                 tag = folder_get_xml(folder);
3214
3215         xml_tag_add_attr(tag, xml_attr_new("type", folder->klass->idstr));
3216
3217         xmlnode = xml_node_new(tag, NULL);
3218
3219         node = g_node_new(xmlnode);
3220         if (folder->node->children) {
3221                 GNode *cur;
3222
3223                 cur = folder->node->children;
3224                 while (cur) {
3225                         GNode *xmlnode;
3226
3227                         xmlnode = g_node_map(cur, folder_item_to_xml, (gpointer) folder);
3228                         g_node_append(node, xmlnode);
3229                         cur = cur->next;
3230                 }
3231         }
3232
3233         return node;
3234 }
3235
3236 static void folder_update_op_count_rec(GNode *node)
3237 {
3238         FolderItem *fitem = FOLDER_ITEM(node->data);
3239
3240         if (g_node_depth(node) > 0) {
3241                 if (fitem->op_count > 0) {
3242                         fitem->op_count = 0;
3243                         folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
3244                 }
3245                 if (node->children) {
3246                         GNode *child;
3247
3248                         child = node->children;
3249                         while (child) {
3250                                 GNode *cur;
3251
3252                                 cur = child;
3253                                 child = cur->next;
3254                                 folder_update_op_count_rec(cur);
3255                         }
3256                 }
3257         }
3258 }
3259
3260 void folder_update_op_count(void) 
3261 {
3262         GList *cur;
3263         Folder *folder;
3264
3265         for (cur = folder_list; cur != NULL; cur = cur->next) {
3266                 folder = cur->data;
3267                 folder_update_op_count_rec(folder->node);
3268         }
3269 }
3270
3271 typedef struct _type_str {
3272         gchar * str;
3273         gint type;
3274 } type_str;
3275
3276
3277 /*
3278 static gchar * folder_item_get_tree_identifier(FolderItem * item)
3279 {
3280         if (item->parent != NULL) {
3281                 gchar * path;
3282                 gchar * id;
3283
3284                 path = folder_item_get_tree_identifier(item->parent);
3285                 if (path == NULL)
3286                         return NULL;
3287
3288                 id = g_strconcat(path, "/", item->name, NULL);
3289                 g_free(path);
3290
3291                 return id;
3292         }
3293         else {
3294                 return g_strconcat("/", item->name, NULL);
3295         }
3296 }
3297 */
3298
3299 /* CLAWS: temporary local folder for filtering */
3300 #define TEMP_FOLDER "TEMP_FOLDER"
3301 #define PROCESSING_FOLDER_ITEM "processing"     
3302
3303 static FolderItem *processing_folder_item;
3304
3305 static void folder_create_processing_folder(void)
3306 {
3307         Folder *processing_folder;
3308         gchar      *tmpname;
3309
3310         if ((processing_folder = folder_find_from_name(TEMP_FOLDER, mh_get_class())) == NULL) {
3311                 gchar *tmppath;
3312
3313                 tmppath =
3314                     g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
3315                                 "tempfolder", NULL);
3316                 processing_folder =
3317                     folder_new(mh_get_class(), TEMP_FOLDER, tmppath);
3318                 g_free(tmppath);
3319         }
3320         g_assert(processing_folder != NULL);
3321
3322         debug_print("tmpparentroot %s\n", LOCAL_FOLDER(processing_folder)->rootpath);
3323         if (LOCAL_FOLDER(processing_folder)->rootpath[0] == '/')
3324                 tmpname = g_strconcat(LOCAL_FOLDER(processing_folder)->rootpath,
3325                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
3326                                       NULL);
3327         else
3328                 tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
3329                                       LOCAL_FOLDER(processing_folder)->rootpath,
3330                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
3331                                       NULL);
3332
3333         if (!is_dir_exist(tmpname)) {
3334                 debug_print("*TMP* creating %s\n", tmpname);
3335                 processing_folder_item = processing_folder->klass->create_folder(processing_folder,
3336                                                                                  processing_folder->node->data,
3337                                                                                  PROCESSING_FOLDER_ITEM);
3338         } else {
3339                 debug_print("*TMP* already created\n");
3340                 processing_folder_item = folder_item_new(processing_folder, PROCESSING_FOLDER_ITEM, PROCESSING_FOLDER_ITEM);
3341                 g_assert(processing_folder_item);
3342                 folder_item_append(processing_folder->node->data, processing_folder_item);
3343         }
3344         g_assert(processing_folder_item != NULL);
3345         g_free(tmpname);
3346 }
3347
3348 FolderItem *folder_get_default_processing(void)
3349 {
3350         if (!processing_folder_item) {
3351                 folder_create_processing_folder();
3352         }
3353         return processing_folder_item;
3354 }
3355
3356 /* folder_persist_prefs_new() - return hash table with persistent
3357  * settings (and folder name as key). 
3358  * (note that in claws other options are in the folder_item_prefs_RC
3359  * file, so those don't need to be included in PersistPref yet) 
3360  */
3361 GHashTable *folder_persist_prefs_new(Folder *folder)
3362 {
3363         GHashTable *pptable;
3364
3365         g_return_val_if_fail(folder, NULL);
3366         pptable = g_hash_table_new(g_str_hash, g_str_equal);
3367         folder_get_persist_prefs_recursive(folder->node, pptable);
3368         return pptable;
3369 }
3370
3371 void folder_persist_prefs_free(GHashTable *pptable)
3372 {
3373         g_return_if_fail(pptable);
3374         g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
3375         g_hash_table_destroy(pptable);
3376 }
3377
3378 const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
3379 {
3380         if (pptable == NULL || name == NULL) return NULL;
3381         return g_hash_table_lookup(pptable, name);
3382 }
3383
3384 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
3385 {
3386         const PersistPrefs *pp;
3387         gchar *id = folder_item_get_identifier(item);
3388
3389         pp = folder_get_persist_prefs(pptable, id); 
3390         g_free(id);
3391
3392         if (!pp) return;
3393
3394         /* CLAWS: since not all folder properties have been migrated to 
3395          * folderlist.xml, we need to call the old stuff first before
3396          * setting things that apply both to Main and Claws. */
3397         folder_item_prefs_read_config(item); 
3398
3399         item->collapsed = pp->collapsed;
3400         item->thread_collapsed = pp->thread_collapsed;
3401         item->threaded  = pp->threaded;
3402         item->ret_rcpt  = pp->ret_rcpt;
3403         item->hide_read_msgs = pp->hide_read_msgs;
3404         item->sort_key  = pp->sort_key;
3405         item->sort_type = pp->sort_type;
3406 }
3407
3408 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
3409 {
3410         FolderItem *item = FOLDER_ITEM(node->data);
3411         PersistPrefs *pp;
3412         GNode *child, *cur;
3413         gchar *id;
3414
3415         g_return_if_fail(node != NULL);
3416         g_return_if_fail(item != NULL);
3417
3418         /* NOTE: item->path == NULL means top level folder; not interesting
3419          * to store preferences of that one.  */
3420         if (item->path) {
3421                 id = folder_item_get_identifier(item);
3422                 pp = g_new0(PersistPrefs, 1);
3423                 g_return_if_fail(pp != NULL);
3424                 pp->collapsed = item->collapsed;
3425                 pp->thread_collapsed = item->thread_collapsed;
3426                 pp->threaded  = item->threaded;
3427                 pp->ret_rcpt  = item->ret_rcpt; 
3428                 pp->hide_read_msgs = item->hide_read_msgs;
3429                 pp->sort_key  = item->sort_key;
3430                 pp->sort_type = item->sort_type;
3431                 g_hash_table_insert(pptable, id, pp);
3432         }
3433
3434         if (node->children) {
3435                 child = node->children;
3436                 while (child) {
3437                         cur = child;
3438                         child = cur->next;
3439                         folder_get_persist_prefs_recursive(cur, pptable);
3440                 }
3441         }       
3442 }
3443
3444 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
3445 {
3446         if (key) 
3447                 g_free(key);
3448         if (val) 
3449                 g_free(val);
3450         return TRUE;    
3451 }
3452
3453 void folder_item_apply_processing(FolderItem *item)
3454 {
3455         GSList *processing_list;
3456         GSList *mlist, *cur;
3457         guint total = 0, curmsg = 0;
3458
3459         g_return_if_fail(item != NULL);
3460         g_return_if_fail(item->no_select == FALSE);     
3461         processing_list = item->prefs->processing;
3462
3463         folder_item_update_freeze();
3464
3465         mlist = folder_item_get_msg_list(item);
3466         total = g_slist_length(mlist);
3467         statusbar_print_all(_("Processing messages..."));
3468         for (cur = mlist ; cur != NULL ; cur = cur->next) {
3469                 MsgInfo * msginfo;
3470
3471                 msginfo = (MsgInfo *) cur->data;
3472                 
3473                 /* reset parameters that can be modified by processing */
3474                 msginfo->hidden = 0;
3475                 msginfo->score = 0;
3476
3477                 statusbar_progress_all(curmsg++,total, 10);
3478
3479                 /* apply pre global rules */
3480                 filter_message_by_msginfo(pre_global_processing, msginfo);
3481                 
3482                 /* apply rules of the folder */
3483                 filter_message_by_msginfo(processing_list, msginfo);
3484
3485                 /* apply post global rules */
3486                 filter_message_by_msginfo(post_global_processing, msginfo);
3487                 
3488                 procmsg_msginfo_free(msginfo);
3489         }
3490         g_slist_free(mlist);
3491         
3492         statusbar_progress_all(0,0,0);
3493         statusbar_pop_all();
3494
3495         folder_item_update_thaw();
3496 }
3497
3498 /*
3499  *  functions for handling FolderItem content changes
3500  */
3501 static gint folder_item_update_freeze_cnt = 0;
3502
3503 static void folder_item_update_with_msg(FolderItem *item, FolderItemUpdateFlags update_flags, MsgInfo *msg)
3504 {
3505         if (folder_item_update_freeze_cnt == 0 /* || (msg != NULL && item->opened) */) {
3506                 FolderItemUpdateData source;
3507         
3508                 source.item = item;
3509                 source.update_flags = update_flags;
3510                 source.msg = msg;
3511                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
3512         } else {
3513                 item->update_flags |= update_flags & ~(F_ITEM_UPDATE_ADDMSG | F_ITEM_UPDATE_REMOVEMSG);
3514         }
3515 }
3516
3517 /**
3518  * Notify the folder system about changes to a folder. If the
3519  * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
3520  * be invoked, otherwise the changes will be remebered until
3521  * the folder system is thawed.
3522  *
3523  * \param item The FolderItem that was changed
3524  * \param update_flags Type of changed that was made
3525  */
3526 void folder_item_update(FolderItem *item, FolderItemUpdateFlags update_flags)
3527 {
3528         folder_item_update_with_msg(item, update_flags, NULL);
3529 }
3530
3531 void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
3532 {
3533         GNode *node = item->folder->node;       
3534
3535         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
3536         node = node->children;
3537
3538         folder_item_update(item, update_flags);
3539         while (node != NULL) {
3540                 if (node && node->data) {
3541                         FolderItem *next_item = (FolderItem*) node->data;
3542
3543                         folder_item_update(next_item, update_flags);
3544                 }
3545                 node = node->next;
3546         }
3547 }
3548
3549 void folder_item_update_freeze(void)
3550 {
3551         folder_item_update_freeze_cnt++;
3552 }
3553
3554 static void folder_item_update_func(FolderItem *item, gpointer data)
3555 {
3556         FolderItemUpdateData source;
3557     
3558         if (item->update_flags) {
3559                 source.item = item;
3560                 source.update_flags = item->update_flags;
3561                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);                             
3562                 item->update_flags = 0;
3563         }
3564 }
3565
3566 void folder_item_update_thaw(void)
3567 {
3568         if (folder_item_update_freeze_cnt > 0)
3569                 folder_item_update_freeze_cnt--;
3570         if (folder_item_update_freeze_cnt == 0) {
3571                 /* Update all folders */
3572                 folder_func_to_all_folders(folder_item_update_func, NULL);
3573         }
3574 }
3575
3576 static void folder_item_synchronise_func(FolderItem *item, gpointer data)
3577 {
3578         Folder *folder = (Folder *)data;
3579         if (folder == NULL || item->folder == folder) {
3580                 if(item->prefs->offlinesync && item->folder->klass->synchronise)
3581                         item->folder->klass->synchronise(item);
3582         }
3583 }
3584
3585 void folder_synchronise(Folder *folder)
3586 {
3587         folder_func_to_all_folders(folder_item_synchronise_func, folder);
3588 }
3589
3590 typedef struct _WantSyncData {
3591         Folder *folder;
3592         gboolean want_sync;
3593 } WantSyncData;
3594
3595 static void folder_item_want_synchronise_func(FolderItem *item, gpointer data)
3596 {