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