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