2005-07-20 [colin] 1.9.12cvs87
[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                 int total = g_slist_length(newmsg_list), cur = 0;
1784                 
1785                 if ((filtering == TRUE) &&
1786                     (item->stype == F_INBOX) &&
1787                     (item->folder->account != NULL) && 
1788                     (item->folder->account->filter_on_recv)) 
1789                         statusbar_print_all(_("Filtering messages...\n"));
1790                 
1791                 for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
1792                         MsgInfo *msginfo = (MsgInfo *) elem->data;
1793
1794                         statusbar_progress_all(cur++,total, 10);
1795
1796                         msgcache_add_msg(item->cache, msginfo);
1797                         if ((filtering == TRUE) &&
1798                             (item->stype == F_INBOX) &&
1799                             (item->folder->account != NULL) && 
1800                             (item->folder->account->filter_on_recv) &&
1801                             procmsg_msginfo_filter(msginfo))
1802                                 procmsg_msginfo_free(msginfo);
1803                         else
1804                                 exists_list = g_slist_prepend(exists_list, msginfo);
1805                 }
1806                 g_slist_free(newmsg_list);
1807
1808                 statusbar_progress_all(0,0,0);
1809                 statusbar_pop_all();
1810                 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1811         }
1812
1813         for (elem = exists_list; elem != NULL; elem = g_slist_next(elem)) {
1814                 MsgInfo *msginfo;
1815
1816                 msginfo = elem->data;
1817                 if (MSG_IS_IGNORE_THREAD(msginfo->flags) && (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1818                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1819                 if (!MSG_IS_IGNORE_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD)) {
1820                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1821                         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
1822                 }
1823                 if ((item->stype == F_OUTBOX ||
1824                      item->stype == F_QUEUE  ||
1825                      item->stype == F_DRAFT  ||
1826                      item->stype == F_TRASH) &&
1827                     (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1828                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1829                 if (MSG_IS_NEW(msginfo->flags))
1830                         newcnt++;
1831                 if (MSG_IS_UNREAD(msginfo->flags))
1832                         unreadcnt++;
1833                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1834                         unreadmarkedcnt++;
1835                 if (MSG_IS_MARKED(msginfo->flags))
1836                         markedcnt++;
1837
1838                 totalcnt++;
1839
1840                 procmsg_msginfo_free(msginfo);
1841         }
1842         g_slist_free(exists_list);
1843
1844         item->new_msgs = newcnt;
1845         item->unread_msgs = unreadcnt;
1846         item->total_msgs = totalcnt;
1847         item->unreadmarked_msgs = unreadmarkedcnt;
1848         item->marked_msgs = markedcnt;
1849
1850         update_flags |= F_ITEM_UPDATE_MSGCNT;
1851
1852         folder_item_update(item, update_flags);
1853         folder_item_update_thaw();
1854
1855         return 0;
1856 }
1857
1858 gint folder_item_scan(FolderItem *item)
1859 {
1860         return folder_item_scan_full(item, TRUE);
1861 }
1862
1863 static gboolean folder_scan_all_items_func(GNode *node, gpointer data)
1864 {
1865         FolderItem *item = node->data;
1866
1867         folder_item_scan(item);
1868
1869         return FALSE;
1870 }
1871
1872 void folder_scan_all_items(Folder * folder)
1873 {
1874         g_node_traverse(folder->node, G_PRE_ORDER,
1875                         G_TRAVERSE_ALL, -1, folder_scan_all_items_func, NULL);
1876 }
1877
1878 static void folder_item_scan_foreach_func(gpointer key, gpointer val,
1879                                           gpointer data)
1880 {
1881         folder_item_scan(FOLDER_ITEM(key));
1882 }
1883
1884 void folder_item_scan_foreach(GHashTable *table)
1885 {
1886         g_hash_table_foreach(table, folder_item_scan_foreach_func, NULL);
1887 }
1888
1889 void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
1890 {
1891         gint *memusage = (gint *)data;
1892
1893         if (item->cache == NULL)
1894                 return;
1895         
1896         *memusage += msgcache_get_memory_usage(item->cache);
1897 }
1898
1899 gint folder_item_syncronize_flags(FolderItem *item)
1900 {
1901         MsgInfoList *msglist = NULL;
1902         GSList *cur;
1903         gint ret = 0;
1904         
1905         g_return_val_if_fail(item != NULL, -1);
1906         g_return_val_if_fail(item->folder != NULL, -1);
1907         g_return_val_if_fail(item->folder->klass != NULL, -1);
1908         g_return_val_if_fail(item->no_select == FALSE, -1);     
1909
1910         if (item->cache == NULL)
1911                 folder_item_read_cache(item);
1912         
1913         msglist = msgcache_get_msg_list(item->cache);
1914         
1915         ret = syncronize_flags(item, msglist);
1916
1917         for (cur = msglist; cur != NULL; cur = g_slist_next(cur))
1918                 procmsg_msginfo_free((MsgInfo *) cur->data);
1919         
1920         return ret;
1921 }
1922
1923 gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
1924 {
1925         FolderItem *fa = (FolderItem *)a;
1926         FolderItem *fb = (FolderItem *)b;
1927         
1928         return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
1929 }
1930
1931 void folder_find_expired_caches(FolderItem *item, gpointer data)
1932 {
1933         GSList **folder_item_list = (GSList **)data;
1934         gint difftime, expiretime;
1935         
1936         if (item->cache == NULL)
1937                 return;
1938
1939         if (item->opened > 0)
1940                 return;
1941
1942         difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
1943         expiretime = prefs_common.cache_min_keep_time * 60;
1944         debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
1945         if (difftime > expiretime) {
1946                 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
1947         }
1948 }
1949
1950 void folder_item_free_cache(FolderItem *item)
1951 {
1952         g_return_if_fail(item != NULL);
1953         
1954         if (item->cache == NULL)
1955                 return;
1956         
1957         if (item->opened > 0)
1958                 return;
1959
1960         folder_item_write_cache(item);
1961         msgcache_destroy(item->cache);
1962         item->cache = NULL;
1963 }
1964
1965 void folder_clean_cache_memory(void)
1966 {
1967         gint memusage = 0;
1968
1969         folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);       
1970         debug_print("Total cache memory usage: %d\n", memusage);
1971         
1972         if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
1973                 GSList *folder_item_list = NULL, *listitem;
1974                 
1975                 debug_print("Trying to free cache memory\n");
1976
1977                 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);      
1978                 listitem = folder_item_list;
1979                 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
1980                         FolderItem *item = (FolderItem *)(listitem->data);
1981
1982                         debug_print("Freeing cache memory for %s\n", item->path ? item->path : item->name);
1983                         memusage -= msgcache_get_memory_usage(item->cache);
1984                         folder_item_free_cache(item);
1985                         listitem = listitem->next;
1986                 }
1987                 g_slist_free(folder_item_list);
1988         }
1989 }
1990
1991 void folder_item_read_cache(FolderItem *item)
1992 {
1993         gchar *cache_file, *mark_file;
1994         
1995         g_return_if_fail(item != NULL);
1996
1997         if (item->path != NULL) {
1998                 cache_file = folder_item_get_cache_file(item);
1999                 mark_file = folder_item_get_mark_file(item);
2000                 item->cache = msgcache_read_cache(item, cache_file);
2001                 if (!item->cache) {
2002                         MsgInfoList *list, *cur;
2003                         guint newcnt = 0, unreadcnt = 0;
2004                         guint markedcnt = 0, unreadmarkedcnt = 0;
2005                         MsgInfo *msginfo;
2006
2007                         item->cache = msgcache_new();
2008                         folder_item_scan_full(item, TRUE);
2009
2010                         msgcache_read_mark(item->cache, mark_file);
2011
2012                         list = msgcache_get_msg_list(item->cache);
2013                         for (cur = list; cur != NULL; cur = g_slist_next(cur)) {
2014                                 msginfo = cur->data;
2015
2016                                 if (MSG_IS_NEW(msginfo->flags))
2017                                         newcnt++;
2018                                 if (MSG_IS_UNREAD(msginfo->flags))
2019                                         unreadcnt++;
2020                                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2021                                         unreadmarkedcnt++;
2022                                 if (MSG_IS_MARKED(msginfo->flags))
2023                                         markedcnt++;
2024                         }
2025                         item->new_msgs = newcnt;
2026                         item->unread_msgs = unreadcnt;
2027                         item->unreadmarked_msgs = unreadmarkedcnt;
2028                         item->marked_msgs = markedcnt;
2029                         procmsg_msg_list_free(list);
2030                 } else
2031                         msgcache_read_mark(item->cache, mark_file);
2032
2033                 g_free(cache_file);
2034                 g_free(mark_file);
2035         } else {
2036                 item->cache = msgcache_new();
2037         }
2038
2039         folder_clean_cache_memory();
2040 }
2041
2042 void folder_item_write_cache(FolderItem *item)
2043 {
2044         gchar *cache_file, *mark_file;
2045         FolderItemPrefs *prefs;
2046         gint filemode = 0;
2047         gchar *id;
2048         
2049         if (!item || !item->path || !item->cache)
2050                 return;
2051
2052         id = folder_item_get_identifier(item);
2053         debug_print("Save cache for folder %s\n", id);
2054         g_free(id);
2055
2056         cache_file = folder_item_get_cache_file(item);
2057         mark_file = folder_item_get_mark_file(item);
2058         if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
2059                 prefs = item->prefs;
2060                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
2061                         /* for cache file */
2062                         filemode = prefs->folder_chmod;
2063                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
2064                         if (filemode & S_IROTH) filemode |= S_IWOTH;
2065                         chmod(cache_file, filemode);
2066                 }
2067         }
2068
2069         g_free(cache_file);
2070         g_free(mark_file);
2071 }
2072
2073 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
2074 {
2075         Folder *folder;
2076         MsgInfo *msginfo = NULL;
2077         
2078         g_return_val_if_fail(item != NULL, NULL);
2079         g_return_val_if_fail(item->no_select == FALSE, NULL);   
2080         folder = item->folder;
2081         if (!item->cache)
2082                 folder_item_read_cache(item);
2083         
2084         if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
2085                 return msginfo;
2086         
2087         msginfo = get_msginfo(item, num);
2088         if (msginfo != NULL) {
2089                 msgcache_add_msg(item->cache, msginfo);
2090                 return msginfo;
2091         }
2092         
2093         return NULL;
2094 }
2095
2096 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
2097 {
2098         Folder *folder;
2099         MsgInfo *msginfo;
2100         
2101         g_return_val_if_fail(item != NULL, NULL);
2102         g_return_val_if_fail(msgid != NULL, NULL);
2103         g_return_val_if_fail(item->no_select == FALSE, FALSE);
2104         
2105         folder = item->folder;
2106         if (!item->cache)
2107                 folder_item_read_cache(item);
2108         
2109         if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
2110                 return msginfo;
2111
2112         return NULL;
2113 }
2114
2115 GSList *folder_item_get_msg_list(FolderItem *item)
2116 {
2117         g_return_val_if_fail(item != NULL, NULL);
2118         g_return_val_if_fail(item->no_select == FALSE, FALSE);
2119         
2120         if (item->cache == 0)
2121                 folder_item_read_cache(item);
2122
2123         g_return_val_if_fail(item->cache != NULL, NULL);
2124         
2125         return msgcache_get_msg_list(item->cache);
2126 }
2127
2128 void msginfo_set_mime_flags(GNode *node, gpointer data)
2129 {
2130         MsgInfo *msginfo = data;
2131         MimeInfo *mimeinfo = node->data;
2132         
2133         if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT) {
2134                 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2135         } else if (mimeinfo->disposition == DISPOSITIONTYPE_UNKNOWN && 
2136                  mimeinfo->type != MIMETYPE_TEXT &&
2137                  mimeinfo->type != MIMETYPE_MULTIPART) {
2138                 if (!mimeinfo->subtype 
2139                 ||  strcmp(mimeinfo->subtype, "pgp-signature"))
2140                         procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2141         }
2142
2143         /* don't descend below top level message for signed and encrypted info */
2144         if (mimeinfo->type == MIMETYPE_MESSAGE)
2145                 return;
2146
2147         if (privacy_mimeinfo_is_signed(mimeinfo)) {
2148                 procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
2149         }
2150
2151         if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
2152                 procmsg_msginfo_set_flags(msginfo, 0, MSG_ENCRYPTED);
2153         } else {
2154                 /* searching inside encrypted parts doesn't really make sense */
2155                 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2156         }
2157 }
2158
2159 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
2160 {
2161         Folder *folder;
2162         gchar *msgfile;
2163         MsgInfo *msginfo;
2164
2165         g_return_val_if_fail(item != NULL, NULL);
2166
2167         folder = item->folder;
2168
2169         g_return_val_if_fail(folder->klass->fetch_msg != NULL, NULL);
2170         g_return_val_if_fail(item->no_select == FALSE, NULL);
2171
2172         msgfile = folder->klass->fetch_msg(folder, item, num);
2173
2174         if (msgfile != NULL) {
2175                 msginfo = folder_item_get_msginfo(item, num);
2176                 if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
2177                         MimeInfo *mimeinfo;
2178
2179                         if (msginfo->folder->stype != F_QUEUE && 
2180                             msginfo->folder->stype != F_DRAFT)
2181                                 mimeinfo = procmime_scan_file(msgfile);
2182                         else
2183                                 mimeinfo = procmime_scan_queue_file(msgfile);
2184                         /* check for attachments */
2185                         if (mimeinfo != NULL) { 
2186                                 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2187                                 procmime_mimeinfo_free_all(mimeinfo);
2188
2189                                 procmsg_msginfo_set_flags(msginfo, 0, MSG_SCANNED);
2190                         }
2191                 }
2192         }
2193
2194         return msgfile;
2195 }
2196
2197 gchar *folder_item_fetch_msg_full(FolderItem *item, gint num, gboolean headers,
2198                                   gboolean body)
2199 {
2200         Folder *folder;
2201         gchar *msgfile;
2202         MsgInfo *msginfo;
2203
2204         g_return_val_if_fail(item != NULL, NULL);
2205         g_return_val_if_fail(item->no_select == FALSE, NULL);
2206         
2207         folder = item->folder;
2208
2209         if (folder->klass->fetch_msg_full == NULL)
2210                 return folder_item_fetch_msg(item, num);
2211
2212         msgfile = folder->klass->fetch_msg_full(folder, item, num, 
2213                                                 headers, body);
2214
2215         if (msgfile != NULL) {
2216                 msginfo = folder_item_get_msginfo(item, num);
2217                 if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
2218                         MimeInfo *mimeinfo;
2219
2220                         if (msginfo->folder->stype != F_QUEUE && 
2221                             msginfo->folder->stype != F_DRAFT)
2222                                 mimeinfo = procmime_scan_file(msgfile);
2223                         else
2224                                 mimeinfo = procmime_scan_queue_file(msgfile);
2225                         /* check for attachments */
2226                         if (mimeinfo != NULL) { 
2227                                 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2228                                 procmime_mimeinfo_free_all(mimeinfo);
2229
2230                                 procmsg_msginfo_set_flags(msginfo, 0, MSG_SCANNED);
2231                         }
2232                 }
2233         }
2234
2235         return msgfile;
2236 }
2237
2238
2239 gint folder_item_fetch_all_msg(FolderItem *item)
2240 {
2241         Folder *folder;
2242         GSList *mlist;
2243         GSList *cur;
2244         gint num = 0;
2245         gint ret = 0;
2246         gint total = 0;
2247
2248         g_return_val_if_fail(item != NULL, -1);
2249         g_return_val_if_fail(item->no_select == FALSE, -1);
2250
2251         debug_print("fetching all messages in %s ...\n", item->path);
2252         statusbar_print_all(_("Fetching all messages in %s ...\n"), item->path);
2253
2254         folder = item->folder;
2255
2256         if (folder->ui_func)
2257                 folder->ui_func(folder, item, folder->ui_func_data ?
2258                                 folder->ui_func_data : GINT_TO_POINTER(num));
2259
2260         mlist = folder_item_get_msg_list(item);
2261
2262         total = g_slist_length(mlist);
2263
2264         for (cur = mlist; cur != NULL; cur = cur->next) {
2265                 MsgInfo *msginfo = (MsgInfo *)cur->data;
2266                 gchar *msg;
2267
2268                 statusbar_progress_all(num++,total, 10);
2269
2270                 if (folder->ui_func)
2271                         folder->ui_func(folder, item,
2272                                         folder->ui_func_data ?
2273                                         folder->ui_func_data :
2274                                         GINT_TO_POINTER(num));
2275
2276                 msg = folder_item_fetch_msg(item, msginfo->msgnum);
2277                 if (!msg) {
2278                         g_warning("Can't fetch message %d. Aborting.\n",
2279                                   msginfo->msgnum);
2280                         ret = -1;
2281                         break;
2282                 }
2283                 g_free(msg);
2284         }
2285         
2286         statusbar_progress_all(0,0,0);
2287         statusbar_pop_all();
2288         procmsg_msg_list_free(mlist);
2289
2290         return ret;
2291 }
2292
2293 static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
2294 {
2295         static HeaderEntry hentry[] = {{"Message-ID:",  NULL, TRUE},
2296                                        {NULL,           NULL, FALSE}};
2297         FILE *fp;
2298         MsgInfo *msginfo;
2299         gint msgnum = 0;
2300         gchar buf[BUFFSIZE];
2301
2302         if ((fp = fopen(file, "rb")) == NULL)
2303                 return 0;
2304
2305         if ((dest->stype == F_QUEUE) || (dest->stype == F_DRAFT))
2306                 while (fgets(buf, sizeof(buf), fp) != NULL)
2307                         if (buf[0] == '\r' || buf[0] == '\n') break;
2308
2309         procheader_get_header_fields(fp, hentry);
2310         if (hentry[0].body) {
2311                 extract_parenthesis(hentry[0].body, '<', '>');
2312                 remove_space(hentry[0].body);
2313                 if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
2314                         msgnum = msginfo->msgnum;
2315                         procmsg_msginfo_free(msginfo);
2316
2317                         debug_print("found message as uid %d\n", msgnum);
2318                 }
2319         }
2320         
2321         g_free(hentry[0].body);
2322         hentry[0].body = NULL;
2323         fclose(fp);
2324
2325         return msgnum;
2326 }
2327
2328 static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
2329 {
2330         MsgPermFlags perm_flags = 0;
2331         MsgTmpFlags tmp_flags = 0;
2332
2333         /* create new flags */
2334         if (source != NULL) {
2335                 /* copy original flags */
2336                 perm_flags = source->flags.perm_flags;
2337                 tmp_flags = source->flags.tmp_flags;
2338         } else {
2339                 perm_flags = dest->flags.perm_flags;
2340                 tmp_flags = dest->flags.tmp_flags;
2341         }
2342
2343         /* remove new, unread and deleted in special folders */
2344         if (dest->folder->stype == F_OUTBOX ||
2345             dest->folder->stype == F_QUEUE  ||
2346             dest->folder->stype == F_DRAFT  ||
2347             dest->folder->stype == F_TRASH)
2348                 perm_flags &= ~(MSG_NEW | MSG_UNREAD | MSG_DELETED);
2349
2350         /* set ignore flag of ignored parent exists */
2351         if (procmsg_msg_has_flagged_parent(dest, MSG_IGNORE_THREAD))
2352                 perm_flags |= MSG_IGNORE_THREAD;
2353
2354         /* Unset tmp flags that should not be copied */
2355         tmp_flags &= ~(MSG_MOVE | MSG_COPY);
2356
2357         /* unset flags that are set but should not */
2358         procmsg_msginfo_unset_flags(dest,
2359                                     dest->flags.perm_flags & ~perm_flags,
2360                                     dest->flags.tmp_flags  & ~tmp_flags);
2361         /* set new flags */
2362         procmsg_msginfo_set_flags(dest,
2363                                   ~dest->flags.perm_flags & perm_flags,
2364                                   ~dest->flags.tmp_flags  & tmp_flags);
2365 }
2366
2367 static void add_msginfo_to_cache(FolderItem *item, MsgInfo *newmsginfo, MsgInfo *flagsource)
2368 {
2369         /* update folder stats */
2370         if (MSG_IS_NEW(newmsginfo->flags))
2371                 item->new_msgs++;
2372         if (MSG_IS_UNREAD(newmsginfo->flags))
2373                 item->unread_msgs++;
2374         if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
2375                 item->unreadmarked_msgs++;
2376         if (MSG_IS_MARKED(newmsginfo->flags))
2377                 item->marked_msgs++;
2378         item->total_msgs++;
2379
2380         folder_item_update_freeze();
2381         msgcache_add_msg(item->cache, newmsginfo);
2382         copy_msginfo_flags(flagsource, newmsginfo);
2383         folder_item_update_with_msg(item,  F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_ADDMSG, newmsginfo);
2384         folder_item_update_thaw();
2385 }
2386
2387 static void remove_msginfo_from_cache(FolderItem *item, MsgInfo *msginfo)
2388 {
2389         MsgInfoUpdate msginfo_update;
2390
2391         if (!item->cache)
2392                 folder_item_read_cache(item);
2393
2394         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2395                 msginfo->folder->new_msgs--;
2396         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2397                 msginfo->folder->unread_msgs--;
2398         if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2399                 msginfo->folder->unreadmarked_msgs--;
2400         if (MSG_IS_MARKED(msginfo->flags))
2401                 item->marked_msgs--;
2402
2403         msginfo->folder->total_msgs--;
2404
2405         msginfo_update.msginfo = msginfo;
2406         msginfo_update.flags = MSGINFO_UPDATE_DELETED;
2407         hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
2408
2409         msgcache_remove_msg(item->cache, msginfo->msgnum);
2410         folder_item_update_with_msg(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_REMOVEMSG, msginfo);
2411 }
2412
2413 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
2414                          MsgFlags *flags, gboolean remove_source)
2415 {
2416         GSList file_list;
2417         MsgFileInfo fileinfo;
2418
2419         g_return_val_if_fail(dest != NULL, -1);
2420         g_return_val_if_fail(file != NULL, -1);
2421  
2422         fileinfo.msginfo = NULL;
2423         fileinfo.file = (gchar *)file;
2424         fileinfo.flags = flags;
2425         file_list.data = &fileinfo;
2426         file_list.next = NULL;
2427
2428         return folder_item_add_msgs(dest, &file_list, remove_source);
2429 }
2430
2431 gint folder_item_add_msgs(FolderItem *dest, GSList *file_list,
2432                           gboolean remove_source)
2433 {
2434         Folder *folder;
2435         gint ret, num, lastnum = -1;
2436         GSList *file_cur;
2437         GRelation *relation;
2438         MsgFileInfo *fileinfo = NULL;
2439         gboolean folderscan = FALSE;
2440
2441         g_return_val_if_fail(dest != NULL, -1);
2442         g_return_val_if_fail(file_list != NULL, -1);
2443         g_return_val_if_fail(dest->folder != NULL, -1);
2444         g_return_val_if_fail(dest->no_select == FALSE, -1);
2445
2446         folder = dest->folder;
2447
2448         relation = g_relation_new(2);
2449         g_relation_index(relation, 0, g_direct_hash, g_direct_equal);
2450
2451         if (folder->klass->add_msgs != NULL) {
2452                 ret = folder->klass->add_msgs(folder, dest, file_list, relation);
2453                 if (ret < 0) {
2454                         g_relation_destroy(relation);
2455                         return ret;
2456                 }
2457         } else {
2458                 for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
2459                         fileinfo = (MsgFileInfo *) file_cur->data;
2460
2461                         ret = folder->klass->add_msg(folder, dest, fileinfo->file, fileinfo->flags);
2462                         if (ret < 0) {
2463                                 g_relation_destroy(relation);
2464                                 return ret;
2465                         }
2466                         g_relation_insert(relation, fileinfo, GINT_TO_POINTER(ret));
2467                 }
2468         }
2469
2470         for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
2471                 GTuples *tuples;
2472
2473                 fileinfo = (MsgFileInfo *) file_cur->data;
2474                 tuples = g_relation_select(relation, fileinfo, 0);
2475                 num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
2476                 g_tuples_destroy(tuples);
2477
2478                 if (num >= 0) {
2479                         MsgInfo *newmsginfo;
2480
2481                         if (num == 0) {
2482                                 if (!folderscan) {
2483                                         folder_item_scan_full(dest, FALSE);
2484                                         folderscan = TRUE;
2485                                 }
2486                                 num = folder_item_get_msg_num_by_file(dest, fileinfo->file);
2487                         }
2488
2489                         if (num > lastnum)
2490                                 lastnum = num;
2491
2492                         if (num >= 0 && remove_source) {
2493                                 if (unlink(fileinfo->file) < 0)
2494                                         FILE_OP_ERROR(fileinfo->file, "unlink");
2495                         }
2496
2497                         if (num == 0)
2498                                 continue;
2499
2500                         if (!folderscan && 
2501                             ((newmsginfo = get_msginfo(dest, num)) != NULL)) {
2502                                 add_msginfo_to_cache(dest, newmsginfo, NULL);
2503                                 procmsg_msginfo_free(newmsginfo);
2504                         } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
2505                                 /* TODO: set default flags */
2506                                 procmsg_msginfo_free(newmsginfo);
2507                         }
2508                 }
2509         }
2510
2511         g_relation_destroy(relation);
2512
2513         return lastnum;
2514 }
2515                 
2516 FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest) 
2517 {
2518         GSList *mlist;
2519         FolderItem *new_item;
2520         FolderItem *next_item;
2521         GNode *srcnode;
2522         gchar *old_id, *new_id;
2523
2524         mlist = folder_item_get_msg_list(src);
2525         
2526         /* move messages */
2527         debug_print("Moving %s to %s\n", src->path, dest->path);
2528         new_item = folder_create_folder(dest, src->name);
2529         if (new_item == NULL) {
2530                 printf("Can't create folder\n");
2531                 return NULL;
2532         }
2533         
2534         if (new_item->folder == NULL)
2535                 new_item->folder = dest->folder;
2536
2537         /* move messages */
2538         log_message(_("Moving %s to %s...\n"), 
2539                         src->name, new_item->path);
2540         if (mlist != NULL)
2541                 folder_item_move_msgs(new_item, mlist);
2542         
2543         /*copy prefs*/
2544         folder_item_prefs_copy_prefs(src, new_item);
2545         new_item->collapsed = src->collapsed;
2546         new_item->thread_collapsed = src->thread_collapsed;
2547         new_item->threaded  = src->threaded;
2548         new_item->ret_rcpt  = src->ret_rcpt;
2549         new_item->hide_read_msgs = src->hide_read_msgs;
2550         new_item->sort_key  = src->sort_key;
2551         new_item->sort_type = src->sort_type;
2552
2553         prefs_matcher_write_config();
2554         
2555         /* recurse */
2556         srcnode = src->folder->node;    
2557         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
2558         srcnode = srcnode->children;
2559         while (srcnode != NULL) {
2560                 if (srcnode && srcnode->data) {
2561                         next_item = (FolderItem*) srcnode->data;
2562                         srcnode = srcnode->next;
2563                         if (folder_item_move_recursive(next_item, new_item) == NULL)
2564                                 return NULL;
2565                 }
2566         }
2567         old_id = folder_item_get_identifier(src);
2568         new_id = folder_item_get_identifier(new_item);
2569         debug_print("updating rules : %s => %s\n", old_id, new_id);
2570
2571         /* if src supports removing, otherwise only copy folder */
2572         if (src->folder->klass->remove_folder != NULL)  
2573                 src->folder->klass->remove_folder(src->folder, src);
2574         folder_write_list();
2575
2576         if (old_id != NULL && new_id != NULL)
2577                 prefs_filtering_rename_path(old_id, new_id);
2578         g_free(old_id);
2579         g_free(new_id);
2580
2581         return new_item;
2582 }
2583
2584 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item)
2585 {
2586         FolderItem *tmp = folder_item_parent(dest);
2587         gchar * src_identifier, * dst_identifier;
2588         gchar * phys_srcpath, * phys_dstpath;
2589         
2590         while (tmp) {
2591                 if (tmp == src) {
2592                         return F_MOVE_FAILED_DEST_IS_CHILD;
2593                 }
2594                 tmp = folder_item_parent(tmp);
2595         }
2596         
2597         tmp = folder_item_parent(src);
2598         
2599         src_identifier = folder_item_get_identifier(src);
2600         dst_identifier = folder_item_get_identifier(dest);
2601         
2602         if(dst_identifier == NULL && dest->folder && folder_item_parent(dest) == NULL) {
2603                 /* dest can be a root folder */
2604                 dst_identifier = folder_get_identifier(dest->folder);
2605         }
2606         if (src_identifier == NULL || dst_identifier == NULL) {
2607                 debug_print("Can't get identifiers\n");
2608                 return F_MOVE_FAILED;
2609         }
2610
2611         if (src->folder != dest->folder) {
2612                 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
2613         }
2614
2615         phys_srcpath = folder_item_get_path(src);
2616         phys_dstpath = g_strconcat(folder_item_get_path(dest),
2617                        G_DIR_SEPARATOR_S,
2618                        g_path_get_basename(phys_srcpath),
2619                        NULL);
2620
2621         if (folder_item_parent(src) == dest || src == dest) {
2622                 g_free(src_identifier);
2623                 g_free(dst_identifier);
2624                 g_free(phys_srcpath);
2625                 g_free(phys_dstpath);
2626                 return F_MOVE_FAILED_DEST_IS_PARENT;
2627         }
2628         debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
2629         if ((tmp = folder_item_move_recursive(src, dest)) == NULL) {
2630                 return F_MOVE_FAILED;
2631         }
2632         
2633         g_free(src_identifier);
2634         g_free(dst_identifier);
2635         g_free(phys_srcpath);
2636         g_free(phys_dstpath);
2637
2638         *new_item = tmp;
2639
2640         return F_MOVE_OK;
2641 }
2642
2643 /**
2644  * Copy a list of message to a new folder and remove
2645  * source messages if wanted
2646  */
2647 static gint do_copy_msgs(FolderItem *dest, GSList *msglist, gboolean remove_source)
2648 {
2649         Folder *folder;
2650         GSList *l;
2651         gint num, lastnum = -1;
2652         gboolean folderscan = FALSE;
2653         GRelation *relation;
2654         GSList *not_moved = NULL;
2655
2656         g_return_val_if_fail(dest != NULL, -1);
2657         g_return_val_if_fail(msglist != NULL, -1);
2658
2659         folder = dest->folder;
2660
2661         g_return_val_if_fail(folder->klass->copy_msg != NULL, -1);
2662         g_return_val_if_fail(dest->no_select == FALSE, -1);
2663
2664         relation = g_relation_new(2);
2665         g_relation_index(relation, 0, g_direct_hash, g_direct_equal);
2666         g_relation_index(relation, 1, g_direct_hash, g_direct_equal);
2667
2668         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
2669                 MsgInfo * msginfo = (MsgInfo *) l->data;
2670
2671                 if (msginfo->planned_download != 0) {
2672                         int old_planned = msginfo->planned_download;
2673                         partial_unmark(msginfo);
2674                         /* little hack to reenable after */
2675                         msginfo->planned_download = old_planned;
2676                 }
2677         }
2678
2679         /* 
2680          * Copy messages to destination folder and 
2681          * store new message numbers in newmsgnums
2682          */
2683         if (folder->klass->copy_msgs != NULL) {
2684                 if (folder->klass->copy_msgs(folder, dest, msglist, relation) < 0) {
2685                         g_relation_destroy(relation);
2686                         return -1;
2687                 }
2688         } else {
2689                 for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
2690                         MsgInfo * msginfo = (MsgInfo *) l->data;
2691
2692                         num = folder->klass->copy_msg(folder, dest, msginfo);
2693                         if (num > 0)
2694                                 g_relation_insert(relation, msginfo, GINT_TO_POINTER(num));
2695                         else
2696                                 not_moved = g_slist_prepend(not_moved, msginfo);
2697                 }
2698         }
2699
2700         /* Read cache for dest folder */
2701         if (!dest->cache) folder_item_read_cache(dest);
2702
2703         /* 
2704          * Fetch new MsgInfos for new messages in dest folder,
2705          * add them to the msgcache and update folder message counts
2706          */
2707         if (g_relation_count(relation, GINT_TO_POINTER(0), 1) > 0) {
2708                 folder_item_scan_full(dest, FALSE);
2709                 folderscan = TRUE;
2710         }
2711
2712         for (l = msglist; l != NULL; l = g_slist_next(l)) {
2713                 MsgInfo *msginfo = (MsgInfo *) l->data;
2714                 GTuples *tuples;
2715
2716                 tuples = g_relation_select(relation, msginfo, 0);
2717                 num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
2718                 g_tuples_destroy(tuples);
2719
2720                 if (num >= 0) {
2721                         MsgInfo *newmsginfo = NULL;
2722
2723                         if (folderscan) {
2724                                 if (msginfo->msgid != NULL) {
2725                                         newmsginfo = folder_item_get_msginfo_by_msgid(dest, msginfo->msgid);
2726                                         if (newmsginfo != NULL) {
2727                                                 copy_msginfo_flags(msginfo, newmsginfo);
2728                                                 num = newmsginfo->msgnum;
2729                                         }
2730                                 }
2731                         } else {
2732                                 newmsginfo = get_msginfo(dest, num);
2733                                 if (newmsginfo != NULL) {
2734                                         add_msginfo_to_cache(dest, newmsginfo, msginfo);
2735                                 }
2736                         }
2737
2738                         if (msginfo->planned_download 
2739                             == POP3_PARTIAL_DLOAD_DELE) {
2740                                 partial_mark_for_delete(newmsginfo);
2741                         }
2742                         if (msginfo->planned_download 
2743                             == POP3_PARTIAL_DLOAD_DLOAD) {
2744                                 partial_mark_for_download(newmsginfo);
2745                         }
2746                         procmsg_msginfo_free(newmsginfo);
2747
2748
2749                         if (num > lastnum)
2750                                 lastnum = num;
2751                 }
2752         }
2753
2754         if (remove_source) {
2755                 MsgInfo *msginfo = (MsgInfo *) msglist->data;
2756                 FolderItem *item = msginfo->folder;
2757                 /*
2758                  * Remove source messages from their folders if
2759                  * copying was successfull and update folder
2760                  * message counts
2761                  */
2762                 if (item->folder->klass->remove_msgs) {
2763                         item->folder->klass->remove_msgs(item->folder,
2764                                                                 msginfo->folder,
2765                                                                 msglist,
2766                                                                 relation);
2767                 }
2768                 for (l = msglist; l != NULL; l = g_slist_next(l)) {
2769                         GTuples *tuples;
2770                         msginfo = (MsgInfo *) l->data;
2771                         item = msginfo->folder;
2772
2773                         tuples = g_relation_select(relation, msginfo, 0);
2774                         num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
2775                         g_tuples_destroy(tuples);
2776
2777                         if (g_slist_find(not_moved, msginfo))
2778                                 continue;
2779
2780                         if ((num >= 0) && (item->folder->klass->remove_msg != NULL)) {
2781                                 if (!item->folder->klass->remove_msgs)
2782                                         item->folder->klass->remove_msg(item->folder,
2783                                                                 msginfo->folder,
2784                                                                 msginfo->msgnum);
2785                                 remove_msginfo_from_cache(item, msginfo);
2786                         }
2787                 }
2788         }
2789
2790         g_relation_destroy(relation);
2791         return lastnum;
2792 }
2793
2794 /**
2795  * Move a message to a new folder.
2796  *
2797  * \param dest Destination folder
2798  * \param msginfo The message
2799  */
2800 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
2801 {
2802         GSList list;
2803
2804         g_return_val_if_fail(dest != NULL, -1);
2805         g_return_val_if_fail(msginfo != NULL, -1);
2806
2807         list.data = msginfo;
2808         list.next = NULL;
2809
2810         return do_copy_msgs(dest, &list, TRUE);
2811 }
2812
2813 /**
2814  * Move a list of messages to a new folder.
2815  *
2816  * \param dest Destination folder
2817  * \param msglist List of messages
2818  */
2819 gint folder_item_move_msgs(FolderItem *dest, GSList *msglist)
2820 {
2821         gint result = -1;
2822         g_return_val_if_fail(dest != NULL, -1);
2823         g_return_val_if_fail(msglist != NULL, -1);
2824         inc_lock();
2825         result = do_copy_msgs(dest, msglist, TRUE);
2826         inc_unlock();
2827         return result;
2828 }
2829
2830 /**
2831  * Copy a message to a new folder.
2832  *
2833  * \param dest Destination folder
2834  * \param msginfo The message
2835  */
2836 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
2837 {
2838         GSList list;
2839
2840         g_return_val_if_fail(dest != NULL, -1);
2841         g_return_val_if_fail(msginfo != NULL, -1);
2842     
2843         list.data = msginfo;
2844         list.next = NULL;
2845
2846         return do_copy_msgs(dest, &list, FALSE);
2847 }
2848
2849 /**
2850  * Copy a list of messages to a new folder.
2851  *
2852  * \param dest Destination folder
2853  * \param msglist List of messages
2854  */
2855 gint folder_item_copy_msgs(FolderItem *dest, GSList *msglist)
2856 {
2857         gint result;
2858         g_return_val_if_fail(dest != NULL, -1);
2859         g_return_val_if_fail(msglist != NULL, -1);
2860
2861         inc_lock();
2862         result = do_copy_msgs(dest, msglist, FALSE);
2863         inc_unlock();
2864         
2865         return result;
2866 }
2867
2868 gint folder_item_remove_msg(FolderItem *item, gint num)
2869 {
2870         Folder *folder;
2871         gint ret;
2872         MsgInfo *msginfo;
2873
2874         g_return_val_if_fail(item != NULL, -1);
2875         folder = item->folder;
2876         g_return_val_if_fail(folder->klass->remove_msg != NULL, -1);
2877         g_return_val_if_fail(item->no_select == FALSE, -1);
2878
2879         if (!item->cache) folder_item_read_cache(item);
2880
2881         ret = folder->klass->remove_msg(folder, item, num);
2882
2883         msginfo = msgcache_get_msg(item->cache, num);
2884         if (msginfo != NULL) {
2885                 remove_msginfo_from_cache(item, msginfo);
2886                 procmsg_msginfo_free(msginfo);
2887         }
2888
2889         return ret;
2890 }
2891
2892 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
2893 {
2894         Folder *folder;
2895         gint ret = 0;
2896
2897         g_return_val_if_fail(item != NULL, -1);
2898         folder = item->folder;
2899         g_return_val_if_fail(folder != NULL, -1);
2900         g_return_val_if_fail(item->no_select == FALSE, -1);
2901         inc_lock();
2902         if (!item->cache) folder_item_read_cache(item);
2903
2904         folder_item_update_freeze();
2905         if (item->folder->klass->remove_msgs) {
2906                 ret = item->folder->klass->remove_msgs(item->folder,
2907                                                         item,
2908                                                         msglist,
2909                                                         NULL);
2910         }
2911         while (ret == 0 && msglist != NULL) {
2912                 MsgInfo *msginfo = (MsgInfo *)msglist->data;
2913                 if (MSG_IS_LOCKED(msginfo->flags)) {
2914                         msglist = msglist->next;
2915                         continue;
2916                 }
2917                 if (!item->folder->klass->remove_msgs)
2918                         ret = folder_item_remove_msg(item, msginfo->msgnum);
2919                 if (ret != 0) break;
2920                 msgcache_remove_msg(item->cache, msginfo->msgnum);
2921                 msglist = msglist->next;
2922         }
2923         folder_item_scan_full(item, FALSE);
2924         folder_item_update_thaw();
2925         inc_unlock();
2926         return ret;
2927 }
2928
2929 gint folder_item_remove_all_msg(FolderItem *item)
2930 {
2931         Folder *folder;
2932         gint result;
2933
2934         g_return_val_if_fail(item != NULL, -1);
2935         g_return_val_if_fail(item->no_select == FALSE, -1);
2936
2937         folder = item->folder;
2938
2939         inc_lock();
2940         if (folder->klass->remove_all_msg != NULL) {
2941                 result = folder->klass->remove_all_msg(folder, item);
2942
2943                 if (result == 0) {
2944                         folder_item_free_cache(item);
2945                         item->cache = msgcache_new();
2946                 }
2947         } else {
2948                 MsgInfoList *msglist;
2949
2950                 msglist = folder_item_get_msg_list(item);
2951                 result = folder_item_remove_msgs(item, msglist);
2952                 procmsg_msg_list_free(msglist);
2953         }
2954
2955         if (result == 0) {
2956                 item->new_msgs = 0;
2957                 item->unread_msgs = 0;
2958                 item->unreadmarked_msgs = 0;
2959                 item->marked_msgs = 0;
2960                 item->total_msgs = 0;
2961                 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2962         }
2963
2964         inc_unlock();
2965         return result;
2966 }
2967
2968 void folder_item_change_msg_flags(FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
2969 {
2970         g_return_if_fail(item != NULL);
2971         g_return_if_fail(msginfo != NULL);
2972         g_return_if_fail(item->no_select == FALSE);
2973         
2974         if (item->folder->klass->change_flags != NULL) {
2975                 item->folder->klass->change_flags(item->folder, item, msginfo, newflags);
2976         } else {
2977                 msginfo->flags.perm_flags = newflags;
2978         }
2979 }
2980
2981 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
2982 {
2983         Folder *folder;
2984
2985         g_return_val_if_fail(item != NULL, FALSE);
2986         g_return_val_if_fail(item->no_select == FALSE, FALSE);
2987
2988         folder = item->folder;
2989
2990         g_return_val_if_fail(folder->klass->is_msg_changed != NULL, -1);
2991
2992         return folder->klass->is_msg_changed(folder, item, msginfo);
2993 }
2994
2995 gchar *folder_item_get_cache_file(FolderItem *item)
2996 {
2997         gchar *path;
2998         gchar *file;
2999
3000         g_return_val_if_fail(item != NULL, NULL);
3001         g_return_val_if_fail(item->path != NULL, NULL);
3002
3003         path = folder_item_get_path(item);
3004         g_return_val_if_fail(path != NULL, NULL);
3005         if (!is_dir_exist(path))
3006                 make_dir_hier(path);
3007         file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
3008         g_free(path);
3009
3010         return file;
3011 }
3012
3013 gchar *folder_item_get_mark_file(FolderItem *item)
3014 {
3015         gchar *path;
3016         gchar *file;
3017
3018         g_return_val_if_fail(item != NULL, NULL);
3019         g_return_val_if_fail(item->path != NULL, NULL);
3020
3021         path = folder_item_get_path(item);
3022         g_return_val_if_fail(path != NULL, NULL);
3023         if (!is_dir_exist(path))
3024                 make_dir_hier(path);
3025         file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
3026         g_free(path);
3027
3028         return file;
3029 }
3030
3031 static gpointer xml_to_folder_item(gpointer nodedata, gpointer data)
3032 {
3033         XMLNode *xmlnode = (XMLNode *) nodedata;
3034         Folder *folder = (Folder *) data;
3035         FolderItem *item;
3036
3037         g_return_val_if_fail(xmlnode != NULL, NULL);
3038         g_return_val_if_fail(folder != NULL, NULL);
3039
3040         if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
3041                 g_warning("tag name != \"folderitem\"\n");
3042                 return NULL;
3043         }
3044
3045         item = folder_item_new(folder, "", "");
3046         if (folder->klass->item_set_xml != NULL)
3047                 folder->klass->item_set_xml(folder, item, xmlnode->tag);
3048         else
3049                 folder_item_set_xml(folder, item, xmlnode->tag);
3050
3051         item->folder = folder;
3052
3053         switch (item->stype) {
3054         case F_INBOX:  folder->inbox  = item; break;
3055         case F_OUTBOX: folder->outbox = item; break;
3056         case F_DRAFT:  folder->draft  = item; break;
3057         case F_QUEUE:  folder->queue  = item; break;
3058         case F_TRASH:  folder->trash  = item; break;
3059         default:       break;
3060         }
3061         folder_item_prefs_read_config(item);
3062
3063         return item;
3064 }
3065
3066 static gboolean folder_item_set_node(GNode *node, gpointer data)
3067 {
3068         FolderItem *item = (FolderItem *) node->data;
3069         item->node = node;
3070
3071         return FALSE;
3072 }
3073
3074 static Folder *folder_get_from_xml(GNode *node)
3075 {
3076         Folder *folder;
3077         XMLNode *xmlnode;
3078         GList *list;
3079         FolderClass *klass = NULL;
3080         GNode *cur;
3081
3082         g_return_val_if_fail(node->data != NULL, NULL);
3083
3084         xmlnode = node->data;
3085         if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
3086                 g_warning("tag name != \"folder\"\n");
3087                 return NULL;
3088         }
3089         list = xmlnode->tag->attr;
3090         for (; list != NULL; list = list->next) {
3091                 XMLAttr *attr = list->data;
3092
3093                 if (!attr || !attr->name || !attr->value) continue;
3094                 if (!strcmp(attr->name, "type"))
3095                         klass = folder_get_class_from_string(attr->value);
3096         }
3097         if (klass == NULL)
3098                 return NULL;
3099
3100         folder = folder_new(klass, "", "");
3101         g_return_val_if_fail(folder != NULL, NULL);
3102
3103         if (klass->set_xml)
3104                 klass->set_xml(folder, xmlnode->tag);
3105         else
3106                 folder_set_xml(folder, xmlnode->tag);
3107
3108         cur = node->children;
3109         while (cur != NULL) {
3110                 GNode *itemnode;
3111
3112                 itemnode = g_node_map(cur, xml_to_folder_item, (gpointer) folder);
3113                 g_node_append(folder->node, itemnode);
3114                 cur = cur->next;
3115         }
3116         g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, folder_item_set_node, NULL);
3117         
3118         return folder;
3119 }
3120
3121 static gchar *folder_get_list_path(void)
3122 {
3123         static gchar *filename = NULL;
3124
3125         if (!filename)
3126                 filename =  g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
3127                                         FOLDER_LIST, NULL);
3128
3129         return filename;
3130 }
3131
3132 #define PUT_ESCAPE_STR(fp, attr, str)                   \
3133 {                                                       \
3134         fputs(" " attr "=\"", fp);                      \
3135         xml_file_put_escape_str(fp, str);               \
3136         fputs("\"", fp);                                \
3137 }
3138
3139 static gpointer folder_item_to_xml(gpointer nodedata, gpointer data)
3140 {
3141         FolderItem *item = (FolderItem *) nodedata;
3142         XMLTag *tag;
3143
3144         g_return_val_if_fail(item != NULL, NULL);
3145
3146         if (item->folder->klass->item_get_xml != NULL)
3147                 tag = item->folder->klass->item_get_xml(item->folder, item);
3148         else
3149                 tag = folder_item_get_xml(item->folder, item);
3150
3151         return xml_node_new(tag, NULL);;
3152 }
3153
3154 static GNode *folder_get_xml_node(Folder *folder)
3155 {
3156         GNode *node;
3157         XMLNode *xmlnode;
3158         XMLTag *tag;
3159
3160         g_return_val_if_fail(folder != NULL, NULL);
3161
3162         if (folder->klass->get_xml != NULL)
3163                 tag = folder->klass->get_xml(folder);
3164         else
3165                 tag = folder_get_xml(folder);
3166
3167         xml_tag_add_attr(tag, xml_attr_new("type", folder->klass->idstr));
3168
3169         xmlnode = xml_node_new(tag, NULL);
3170
3171         node = g_node_new(xmlnode);
3172         if (folder->node->children) {
3173                 GNode *cur;
3174
3175                 cur = folder->node->children;
3176                 while (cur) {
3177                         GNode *xmlnode;
3178
3179                         xmlnode = g_node_map(cur, folder_item_to_xml, (gpointer) folder);
3180                         g_node_append(node, xmlnode);
3181                         cur = cur->next;
3182                 }
3183         }
3184
3185         return node;
3186 }
3187
3188 static void folder_update_op_count_rec(GNode *node)
3189 {
3190         FolderItem *fitem = FOLDER_ITEM(node->data);
3191
3192         if (g_node_depth(node) > 0) {
3193                 if (fitem->op_count > 0) {
3194                         fitem->op_count = 0;
3195                         folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
3196                 }
3197                 if (node->children) {
3198                         GNode *child;
3199
3200                         child = node->children;
3201                         while (child) {
3202                                 GNode *cur;
3203
3204                                 cur = child;
3205                                 child = cur->next;
3206                                 folder_update_op_count_rec(cur);
3207                         }
3208                 }
3209         }
3210 }
3211
3212 void folder_update_op_count(void) 
3213 {
3214         GList *cur;
3215         Folder *folder;
3216
3217         for (cur = folder_list; cur != NULL; cur = cur->next) {
3218                 folder = cur->data;
3219                 folder_update_op_count_rec(folder->node);
3220         }
3221 }
3222
3223 typedef struct _type_str {
3224         gchar * str;
3225         gint type;
3226 } type_str;
3227
3228
3229 /*
3230 static gchar * folder_item_get_tree_identifier(FolderItem * item)
3231 {
3232         if (item->parent != NULL) {
3233                 gchar * path;
3234                 gchar * id;
3235
3236                 path = folder_item_get_tree_identifier(item->parent);
3237                 if (path == NULL)
3238                         return NULL;
3239
3240                 id = g_strconcat(path, "/", item->name, NULL);
3241                 g_free(path);
3242
3243                 return id;
3244         }
3245         else {
3246                 return g_strconcat("/", item->name, NULL);
3247         }
3248 }
3249 */
3250
3251 /* CLAWS: temporary local folder for filtering */
3252 #define TEMP_FOLDER "TEMP_FOLDER"
3253 #define PROCESSING_FOLDER_ITEM "processing"     
3254
3255 static FolderItem *processing_folder_item;
3256
3257 static void folder_create_processing_folder(void)
3258 {
3259         Folder *processing_folder;
3260         gchar      *tmpname;
3261
3262         if ((processing_folder = folder_find_from_name(TEMP_FOLDER, mh_get_class())) == NULL) {
3263                 gchar *tmppath;
3264
3265                 tmppath =
3266                     g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
3267                                 "tempfolder", NULL);
3268                 processing_folder =
3269                     folder_new(mh_get_class(), TEMP_FOLDER, tmppath);
3270                 g_free(tmppath);
3271         }
3272         g_assert(processing_folder != NULL);
3273
3274         debug_print("tmpparentroot %s\n", LOCAL_FOLDER(processing_folder)->rootpath);
3275         if (LOCAL_FOLDER(processing_folder)->rootpath[0] == '/')
3276                 tmpname = g_strconcat(LOCAL_FOLDER(processing_folder)->rootpath,
3277                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
3278                                       NULL);
3279         else
3280                 tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
3281                                       LOCAL_FOLDER(processing_folder)->rootpath,
3282                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
3283                                       NULL);
3284
3285         if (!is_dir_exist(tmpname)) {
3286                 debug_print("*TMP* creating %s\n", tmpname);
3287                 processing_folder_item = processing_folder->klass->create_folder(processing_folder,
3288                                                                                  processing_folder->node->data,
3289                                                                                  PROCESSING_FOLDER_ITEM);
3290         } else {
3291                 debug_print("*TMP* already created\n");
3292                 processing_folder_item = folder_item_new(processing_folder, PROCESSING_FOLDER_ITEM, PROCESSING_FOLDER_ITEM);
3293                 g_assert(processing_folder_item);
3294                 folder_item_append(processing_folder->node->data, processing_folder_item);
3295         }
3296         g_assert(processing_folder_item != NULL);
3297         g_free(tmpname);
3298 }
3299
3300 FolderItem *folder_get_default_processing(void)
3301 {
3302         if (!processing_folder_item) {
3303                 folder_create_processing_folder();
3304         }
3305         return processing_folder_item;
3306 }
3307
3308 /* folder_persist_prefs_new() - return hash table with persistent
3309  * settings (and folder name as key). 
3310  * (note that in claws other options are in the folder_item_prefs_RC
3311  * file, so those don't need to be included in PersistPref yet) 
3312  */
3313 GHashTable *folder_persist_prefs_new(Folder *folder)
3314 {
3315         GHashTable *pptable;
3316
3317         g_return_val_if_fail(folder, NULL);
3318         pptable = g_hash_table_new(g_str_hash, g_str_equal);
3319         folder_get_persist_prefs_recursive(folder->node, pptable);
3320         return pptable;
3321 }
3322
3323 void folder_persist_prefs_free(GHashTable *pptable)
3324 {
3325         g_return_if_fail(pptable);
3326         g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
3327         g_hash_table_destroy(pptable);
3328 }
3329
3330 const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
3331 {
3332         if (pptable == NULL || name == NULL) return NULL;
3333         return g_hash_table_lookup(pptable, name);
3334 }
3335
3336 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
3337 {
3338         const PersistPrefs *pp;
3339         gchar *id = folder_item_get_identifier(item);
3340
3341         pp = folder_get_persist_prefs(pptable, id); 
3342         g_free(id);
3343
3344         if (!pp) return;
3345
3346         /* CLAWS: since not all folder properties have been migrated to 
3347          * folderlist.xml, we need to call the old stuff first before
3348          * setting things that apply both to Main and Claws. */
3349         folder_item_prefs_read_config(item); 
3350
3351         item->collapsed = pp->collapsed;
3352         item->thread_collapsed = pp->thread_collapsed;
3353         item->threaded  = pp->threaded;
3354         item->ret_rcpt  = pp->ret_rcpt;
3355         item->hide_read_msgs = pp->hide_read_msgs;
3356         item->sort_key  = pp->sort_key;
3357         item->sort_type = pp->sort_type;
3358 }
3359
3360 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
3361 {
3362         FolderItem *item = FOLDER_ITEM(node->data);
3363         PersistPrefs *pp;
3364         GNode *child, *cur;
3365         gchar *id;
3366
3367         g_return_if_fail(node != NULL);
3368         g_return_if_fail(item != NULL);
3369
3370         /* NOTE: item->path == NULL means top level folder; not interesting
3371          * to store preferences of that one.  */
3372         if (item->path) {
3373                 id = folder_item_get_identifier(item);
3374                 pp = g_new0(PersistPrefs, 1);
3375                 g_return_if_fail(pp != NULL);
3376                 pp->collapsed = item->collapsed;
3377                 pp->thread_collapsed = item->thread_collapsed;
3378                 pp->threaded  = item->threaded;
3379                 pp->ret_rcpt  = item->ret_rcpt; 
3380                 pp->hide_read_msgs = item->hide_read_msgs;
3381                 pp->sort_key  = item->sort_key;
3382                 pp->sort_type = item->sort_type;
3383                 g_hash_table_insert(pptable, id, pp);
3384         }
3385
3386         if (node->children) {
3387                 child = node->children;
3388                 while (child) {
3389                         cur = child;
3390                         child = cur->next;
3391                         folder_get_persist_prefs_recursive(cur, pptable);
3392                 }
3393         }       
3394 }
3395
3396 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
3397 {
3398         if (key) 
3399                 g_free(key);
3400         if (val) 
3401                 g_free(val);
3402         return TRUE;    
3403 }
3404
3405 void folder_item_apply_processing(FolderItem *item)
3406 {
3407         GSList *processing_list;
3408         GSList *mlist, *cur;
3409         guint total = 0, curmsg = 0;
3410
3411         g_return_if_fail(item != NULL);
3412         g_return_if_fail(item->no_select == FALSE);     
3413         processing_list = item->prefs->processing;
3414
3415         folder_item_update_freeze();
3416
3417         mlist = folder_item_get_msg_list(item);
3418         total = g_slist_length(mlist);
3419         statusbar_print_all(_("Processing messages..."));
3420         for (cur = mlist ; cur != NULL ; cur = cur->next) {
3421                 MsgInfo * msginfo;
3422
3423                 msginfo = (MsgInfo *) cur->data;
3424                 
3425                 /* reset parameters that can be modified by processing */
3426                 msginfo->hidden = 0;
3427                 msginfo->score = 0;
3428
3429                 statusbar_progress_all(curmsg++,total, 10);
3430
3431                 /* apply pre global rules */
3432                 filter_message_by_msginfo(pre_global_processing, msginfo);
3433                 
3434                 /* apply rules of the folder */
3435                 filter_message_by_msginfo(processing_list, msginfo);
3436
3437                 /* apply post global rules */
3438                 filter_message_by_msginfo(post_global_processing, msginfo);
3439                 
3440                 procmsg_msginfo_free(msginfo);
3441         }
3442         g_slist_free(mlist);
3443         
3444         statusbar_progress_all(0,0,0);
3445         statusbar_pop_all();
3446
3447         folder_item_update_thaw();
3448 }
3449
3450 /*
3451  *  functions for handling FolderItem content changes
3452  */
3453 static gint folder_item_update_freeze_cnt = 0;
3454
3455 static void folder_item_update_with_msg(FolderItem *item, FolderItemUpdateFlags update_flags, MsgInfo *msg)
3456 {
3457         if (folder_item_update_freeze_cnt == 0 /* || (msg != NULL && item->opened) */) {
3458                 FolderItemUpdateData source;
3459         
3460                 source.item = item;
3461                 source.update_flags = update_flags;
3462                 source.msg = msg;
3463                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
3464         } else {
3465                 item->update_flags |= update_flags & ~(F_ITEM_UPDATE_ADDMSG | F_ITEM_UPDATE_REMOVEMSG);
3466         }
3467 }
3468
3469 /**
3470  * Notify the folder system about changes to a folder. If the
3471  * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
3472  * be invoked, otherwise the changes will be remebered until
3473  * the folder system is thawed.
3474  *
3475  * \param item The FolderItem that was changed
3476  * \param update_flags Type of changed that was made
3477  */
3478 void folder_item_update(FolderItem *item, FolderItemUpdateFlags update_flags)
3479 {
3480         folder_item_update_with_msg(item, update_flags, NULL);
3481 }
3482
3483 void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
3484 {
3485         GNode *node = item->folder->node;       
3486
3487         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
3488         node = node->children;
3489
3490         folder_item_update(item, update_flags);
3491         while (node != NULL) {
3492                 if (node && node->data) {
3493                         FolderItem *next_item = (FolderItem*) node->data;
3494
3495                         folder_item_update(next_item, update_flags);
3496                 }
3497                 node = node->next;
3498         }
3499 }
3500
3501 void folder_item_update_freeze(void)
3502 {
3503         folder_item_update_freeze_cnt++;
3504 }
3505
3506 static void folder_item_update_func(FolderItem *item, gpointer data)
3507 {
3508         FolderItemUpdateData source;
3509     
3510         if (item->update_flags) {
3511                 source.item = item;
3512                 source.update_flags = item->update_flags;
3513                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);                             
3514                 item->update_flags = 0;
3515         }
3516 }
3517
3518 void folder_item_update_thaw(void)
3519 {
3520         if (folder_item_update_freeze_cnt > 0)
3521                 folder_item_update_freeze_cnt--;
3522         if (folder_item_update_freeze_cnt == 0) {
3523                 /* Update all folders */
3524                 folder_func_to_all_folders(folder_item_update_func, NULL);
3525         }
3526 }
3527
3528 void folder_item_set_batch (FolderItem *item, gboolean batch)
3529 {
3530         if (item->folder->klass->set_batch) {
3531                 item->folder->klass->set_batch(item->folder, item, batch);
3532         }
3533 }
3534
3535 #undef PUT_ESCAPE_STR