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