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