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