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