2007-01-21 [colin] 2.7.1cvs45
[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_item_get_cache_file        (FolderItem     *item);
85 static gchar *folder_item_get_mark_file (FolderItem     *item);
86 static gchar *folder_get_list_path      (void);
87 static GNode *folder_get_xml_node       (Folder         *folder);
88 static Folder *folder_get_from_xml      (GNode          *node);
89 static void folder_update_op_count_rec  (GNode          *node);
90
91
92 static void folder_get_persist_prefs_recursive
93                                         (GNode *node, GHashTable *pptable);
94 static gboolean persist_prefs_free      (gpointer key, gpointer val, gpointer data);
95 static void folder_item_read_cache              (FolderItem *item);
96 gint folder_item_scan_full              (FolderItem *item, gboolean filtering);
97 static void folder_item_update_with_msg (FolderItem *item, FolderItemUpdateFlags update_flags,
98                                          MsgInfo *msg);
99 static GHashTable *folder_persist_prefs_new     (Folder *folder);
100 static void folder_persist_prefs_free           (GHashTable *pptable);
101 static void folder_item_restore_persist_prefs   (FolderItem *item, GHashTable *pptable);
102
103
104 void folder_system_init(void)
105 {
106         folder_register_class(mh_get_class());
107         folder_register_class(imap_get_class());
108         folder_register_class(news_get_class());
109 }
110
111 static GSList *folder_get_class_list(void)
112 {
113         return class_list;
114 }
115
116 void folder_register_class(FolderClass *klass)
117 {
118         GSList *xmllist, *cur;
119
120         debug_print("registering folder class %s\n", klass->idstr);
121
122         class_list = g_slist_append(class_list, klass);
123
124         xmllist = g_slist_copy(folder_unloaded_list);
125         for (cur = xmllist; cur != NULL; cur = g_slist_next(cur)) {
126                 GNode *node = (GNode *) cur->data;
127                 XMLNode *xmlnode = (XMLNode *) node->data;
128                 GList *cur = xmlnode->tag->attr;
129
130                 for (; cur != NULL; cur = g_list_next(cur)) {
131                         XMLAttr *attr = (XMLAttr *) cur->data;
132
133                         if (!attr || !attr->name || !attr->value) continue;
134                         if (!strcmp(attr->name, "type") && !strcmp(attr->value, klass->idstr)) {
135                                 Folder *folder;
136
137                                 folder = folder_get_from_xml(node);
138                                 folder_add(folder);
139                                 folder_unloaded_list = g_slist_remove(folder_unloaded_list, node);
140
141                                 cur = NULL;
142                                 continue;
143                         }
144                 }
145         }
146         g_slist_free(xmllist);
147 }
148
149 void folder_unregister_class(FolderClass *klass)
150 {
151         GList *folderlist, *cur;
152
153         debug_print("unregistering folder class %s\n", klass->idstr);
154
155         class_list = g_slist_remove(class_list, klass);
156
157         folderlist = g_list_copy(folder_get_list());
158         for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
159                 Folder *folder = (Folder *) cur->data;
160
161                 if (folder->klass == klass) {
162                         GNode *xmlnode = folder_get_xml_node(folder);
163                         folder_unloaded_list = g_slist_append(folder_unloaded_list, xmlnode);
164                         folder_destroy(folder);
165                 }
166         }
167         g_list_free(folderlist);
168 }
169
170 Folder *folder_new(FolderClass *klass, const gchar *name, const gchar *path)
171 {
172         Folder *folder = NULL;
173         FolderItem *item;
174
175         g_return_val_if_fail(klass != NULL, NULL);
176
177         name = name ? name : path;
178         folder = klass->new_folder(name, path);
179
180         /* Create root folder item */
181         item = folder_item_new(folder, name, NULL);
182         if (item == NULL) {
183                 return NULL;
184         }
185         item->folder = folder;
186         folder->node = item->node = g_node_new(item);
187         folder->data = NULL;
188
189         return folder;
190 }
191
192 void folder_init(Folder *folder, const gchar *name)
193 {
194         g_return_if_fail(folder != NULL);
195
196         folder_set_name(folder, name);
197
198         /* Init folder data */
199         folder->account = NULL;
200         folder->sort = 0;
201         folder->inbox = NULL;
202         folder->outbox = NULL;
203         folder->draft = NULL;
204         folder->queue = NULL;
205         folder->trash = NULL;
206 }
207
208 static void reset_parent_type(FolderItem *item, gpointer data) {
209         item->parent_stype = -1;
210 }
211
212 void folder_item_change_type(FolderItem *item, SpecialFolderItemType newtype)
213 {
214         Folder *folder = NULL;
215         FolderUpdateData hookdata;
216
217         folder = item->folder;
218         /* unset previous root of newtype */
219         switch(newtype) {
220         case F_INBOX:
221                 folder_item_change_type(folder->inbox, F_NORMAL);
222                 folder->inbox = item;
223                 break;
224         case F_OUTBOX:
225                 folder_item_change_type(folder->outbox, F_NORMAL);
226                 folder->outbox = item;
227                 break;
228         case F_QUEUE:
229                 folder_item_change_type(folder->queue, F_NORMAL);
230                 folder->queue = item;
231                 break;
232         case F_DRAFT:
233                 folder_item_change_type(folder->draft, F_NORMAL);
234                 folder->draft = item;
235                 break;
236         case F_TRASH:
237                 folder_item_change_type(folder->trash, F_NORMAL);
238                 folder->trash = item;
239                 break;
240         case F_NORMAL:
241         default:
242                 break;
243         }
244         /* set new type for current folder and sons */
245         item->stype = newtype;
246         folder_func_to_all_folders(reset_parent_type, NULL);
247         
248         hookdata.folder = folder;
249         hookdata.update_flags = FOLDER_TREE_CHANGED;
250         hookdata.item = NULL;
251         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
252 }
253
254 void folder_destroy(Folder *folder)
255 {
256         g_return_if_fail(folder != NULL);
257         g_return_if_fail(folder->klass->destroy_folder != NULL);
258
259         folder_remove(folder);
260
261         folder_tree_destroy(folder);
262
263         folder->klass->destroy_folder(folder);
264
265         g_free(folder->name);
266         g_free(folder);
267 }
268
269 void folder_set_xml(Folder *folder, XMLTag *tag)
270 {
271         GList *cur;
272         FolderItem *rootitem = NULL;
273
274         if ((folder->node != NULL) && (folder->node->data != NULL))
275                 rootitem = (FolderItem *) folder->node->data;
276
277         for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
278                 XMLAttr *attr = (XMLAttr *) cur->data;
279
280                 if (!attr || !attr->name || !attr->value) continue;
281                 if (!strcmp(attr->name, "name")) {
282                         g_free(folder->name);
283                         folder->name = g_strdup(attr->value);
284                         if (rootitem != NULL) {
285                                 g_free(rootitem->name);
286                                 rootitem->name = g_strdup(attr->value);
287                         }
288                 } else if (!strcmp(attr->name, "account_id")) {
289                         PrefsAccount *account;
290
291                         account = account_find_from_id(atoi(attr->value));
292                         if (!account)
293                                 g_warning("account_id: %s not found\n", attr->value);
294                         else {
295                                 folder->account = account;
296                                 account->folder = folder;
297                         }
298                 } else if (!strcmp(attr->name, "collapsed")) {
299                         if (rootitem != NULL)
300                                 rootitem->collapsed = *attr->value == '1' ? TRUE : FALSE;
301                 } else if (!strcmp(attr->name, "sort")) {
302                         folder->sort = atoi(attr->value);
303                 }
304         }
305 }
306
307 XMLTag *folder_get_xml(Folder *folder)
308 {
309         XMLTag *tag;
310
311         tag = xml_tag_new("folder");
312
313         if (folder->name)
314                 xml_tag_add_attr(tag, xml_attr_new("name", folder->name));
315         if (folder->account)
316                 xml_tag_add_attr(tag, xml_attr_new_int("account_id", folder->account->account_id));
317         if (folder->node && folder->node->data) {
318                 FolderItem *rootitem = (FolderItem *) folder->node->data;
319
320                 xml_tag_add_attr(tag, xml_attr_new("collapsed", rootitem->collapsed ? "1" : "0"));
321         }
322         xml_tag_add_attr(tag, xml_attr_new_int("sort", folder->sort));
323
324         return tag;
325 }
326
327 FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path)
328 {
329         FolderItem *item = NULL;
330         
331         g_return_val_if_fail(folder != NULL, NULL);
332         
333         if (folder->klass->item_new) {
334                 item = folder->klass->item_new(folder);
335         } else {
336                 item = g_new0(FolderItem, 1);
337         }
338
339         g_return_val_if_fail(item != NULL, NULL);
340
341         item->stype = F_NORMAL;
342         item->name = g_strdup(name);
343         item->path = g_strdup(path);
344         item->mtime = 0;
345         item->new_msgs = 0;
346         item->unread_msgs = 0;
347         item->unreadmarked_msgs = 0;
348         item->marked_msgs = 0;
349         item->total_msgs = 0;
350         item->last_num = -1;
351         item->cache = NULL;
352         item->no_sub = FALSE;
353         item->no_select = FALSE;
354         item->collapsed = FALSE;
355         item->thread_collapsed = FALSE;
356         item->threaded  = TRUE;
357         item->ret_rcpt  = FALSE;
358         item->opened    = FALSE;
359         item->node = g_node_new(item);
360         item->folder = NULL;
361         item->account = NULL;
362         item->apply_sub = FALSE;
363         item->mark_queue = NULL;
364         item->data = NULL;
365         item->parent_stype = -1;
366
367         item->sort_key = SORT_BY_DATE;
368         item->sort_type = SORT_ASCENDING;
369
370         item->prefs = folder_item_prefs_new();
371
372         return item;
373 }
374
375 void folder_item_append(FolderItem *parent, FolderItem *item)
376 {
377         g_return_if_fail(parent != NULL);
378         g_return_if_fail(parent->folder != NULL);
379         g_return_if_fail(parent->node != NULL);
380         g_return_if_fail(item != NULL);
381
382         item->folder = parent->folder;
383         g_node_append(parent->node, item->node);
384 }
385
386 void folder_item_remove(FolderItem *item)
387 {
388         GNode *node, *start_node;
389         FolderUpdateData hookdata;
390
391         g_return_if_fail(item != NULL);
392         g_return_if_fail(item->folder != NULL);
393         g_return_if_fail(item->folder->node != NULL);
394
395         start_node = node = item->node;
396         
397         node = item->folder->node;
398         
399         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
400         node = node->children;
401
402         /* remove my children */
403         while (node != NULL) {
404                 if (node && node->data) {
405                         FolderItem *sub_item = (FolderItem*) node->data;
406                         node = node->next;
407                         folder_item_remove(sub_item);
408                 }
409         }
410         
411         /* remove myself */
412         if (item->cache != NULL) {
413                 msgcache_destroy(item->cache);
414                 item->cache = NULL;
415         }
416
417         hookdata.folder = item->folder;
418         hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_REMOVE_FOLDERITEM;
419         hookdata.item = item;
420         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
421
422         node = start_node;
423
424         if (item->folder->node == node)
425                 item->folder->node = NULL;
426
427         folder_item_destroy(item);
428
429         g_node_destroy(node);
430 }
431
432 void folder_item_remove_children(FolderItem *item)
433 {
434         GNode *node, *next;
435
436         g_return_if_fail(item != NULL);
437         g_return_if_fail(item->folder != NULL);
438         g_return_if_fail(item->node != NULL);
439
440         node = item->node->children;
441         while (node != NULL) {
442                 next = node->next;
443                 folder_item_remove(FOLDER_ITEM(node->data));
444                 node = next;
445         }
446 }
447
448 void folder_item_destroy(FolderItem *item)
449 {
450         Folder *folder;
451
452         g_return_if_fail(item != NULL);
453
454         folder = item->folder;
455         if (folder) {
456                 if (folder->inbox == item)
457                         folder->inbox = NULL;
458                 else if (folder->outbox == item)
459                         folder->outbox = NULL;
460                 else if (folder->draft == item)
461                         folder->draft = NULL;
462                 else if (folder->queue == item)
463                         folder->queue = NULL;
464                 else if (folder->trash == item)
465                         folder->trash = NULL;
466         }
467
468         if (item->cache)
469                 folder_item_free_cache(item, TRUE);
470         if (item->prefs)
471                 folder_item_prefs_free(item->prefs);
472         g_free(item->name);
473         g_free(item->path);
474
475         if (item->folder != NULL) {
476                 if(item->folder->klass->item_destroy) {
477                         item->folder->klass->item_destroy(item->folder, item);
478                 } else {
479                         g_free(item);
480                 }
481         }
482 }
483
484 FolderItem *folder_item_parent(FolderItem *item)
485 {
486         g_return_val_if_fail(item != NULL, NULL);
487         g_return_val_if_fail(item->node != NULL, NULL);
488
489         if (item->node->parent == NULL)
490                 return NULL;
491         return (FolderItem *) item->node->parent->data;
492 }
493
494 void folder_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
495 {
496         GList *cur;
497
498         for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
499                 XMLAttr *attr = (XMLAttr *) cur->data;
500
501                 if (!attr || !attr->name || !attr->value) continue;
502                 if (!strcmp(attr->name, "type")) {
503                         if (!g_ascii_strcasecmp(attr->value, "normal"))
504                                 item->stype = F_NORMAL;
505                         else if (!g_ascii_strcasecmp(attr->value, "inbox"))
506                                 item->stype = F_INBOX;
507                         else if (!g_ascii_strcasecmp(attr->value, "outbox"))
508                                 item->stype = F_OUTBOX;
509                         else if (!g_ascii_strcasecmp(attr->value, "draft"))
510                                 item->stype = F_DRAFT;
511                         else if (!g_ascii_strcasecmp(attr->value, "queue"))
512                                 item->stype = F_QUEUE;
513                         else if (!g_ascii_strcasecmp(attr->value, "trash"))
514                                 item->stype = F_TRASH;
515                 } else if (!strcmp(attr->name, "name")) {
516                         g_free(item->name);
517                         item->name = g_strdup(attr->value);
518                 } else if (!strcmp(attr->name, "path")) {
519                         g_free(item->path);
520                         item->path = g_strdup(attr->value);
521                 } else if (!strcmp(attr->name, "mtime"))
522                         item->mtime = strtoul(attr->value, NULL, 10);
523                 else if (!strcmp(attr->name, "new"))
524                         item->new_msgs = atoi(attr->value);
525                 else if (!strcmp(attr->name, "unread"))
526                         item->unread_msgs = atoi(attr->value);
527                 else if (!strcmp(attr->name, "unreadmarked"))
528                         item->unreadmarked_msgs = atoi(attr->value);
529                 else if (!strcmp(attr->name, "marked"))
530                         item->marked_msgs = atoi(attr->value);
531                 else if (!strcmp(attr->name, "total"))
532                         item->total_msgs = atoi(attr->value);
533                 else if (!strcmp(attr->name, "no_sub"))
534                         item->no_sub = *attr->value == '1' ? TRUE : FALSE;
535                 else if (!strcmp(attr->name, "no_select"))
536                         item->no_select = *attr->value == '1' ? TRUE : FALSE;
537                 else if (!strcmp(attr->name, "collapsed"))
538                         item->collapsed = *attr->value == '1' ? TRUE : FALSE;
539                 else if (!strcmp(attr->name, "thread_collapsed"))
540                         item->thread_collapsed =  *attr->value == '1' ? TRUE : FALSE;
541                 else if (!strcmp(attr->name, "threaded"))
542                         item->threaded =  *attr->value == '1' ? TRUE : FALSE;
543                 else if (!strcmp(attr->name, "hidereadmsgs"))
544                         item->hide_read_msgs =  *attr->value == '1' ? TRUE : FALSE;
545                 else if (!strcmp(attr->name, "reqretrcpt"))
546                         item->ret_rcpt =  *attr->value == '1' ? TRUE : FALSE;
547                 else if (!strcmp(attr->name, "sort_key")) {
548                         if (!strcmp(attr->value, "none"))
549                                 item->sort_key = SORT_BY_NONE;
550                         else if (!strcmp(attr->value, "number"))
551                                 item->sort_key = SORT_BY_NUMBER;
552                         else if (!strcmp(attr->value, "size"))
553                                 item->sort_key = SORT_BY_SIZE;
554                         else if (!strcmp(attr->value, "date"))
555                                 item->sort_key = SORT_BY_DATE;
556                         else if (!strcmp(attr->value, "from"))
557                                 item->sort_key = SORT_BY_FROM;
558                         else if (!strcmp(attr->value, "subject"))
559                                 item->sort_key = SORT_BY_SUBJECT;
560                         else if (!strcmp(attr->value, "score"))
561                                 item->sort_key = SORT_BY_SCORE;
562                         else if (!strcmp(attr->value, "label"))
563                                 item->sort_key = SORT_BY_LABEL;
564                         else if (!strcmp(attr->value, "mark"))
565                                 item->sort_key = SORT_BY_MARK;
566                         else if (!strcmp(attr->value, "unread"))
567                                 item->sort_key = SORT_BY_STATUS;
568                         else if (!strcmp(attr->value, "mime"))
569                                 item->sort_key = SORT_BY_MIME;
570                         else if (!strcmp(attr->value, "to"))
571                                 item->sort_key = SORT_BY_TO;
572                         else if (!strcmp(attr->value, "locked"))
573                                 item->sort_key = SORT_BY_LOCKED;
574                 } else if (!strcmp(attr->name, "sort_type")) {
575                         if (!strcmp(attr->value, "ascending"))
576                                 item->sort_type = SORT_ASCENDING;
577                         else
578                                 item->sort_type = SORT_DESCENDING;
579                 } else if (!strcmp(attr->name, "account_id")) {
580                         PrefsAccount *account;
581
582                         account = account_find_from_id(atoi(attr->value));
583                         if (!account)
584                                 g_warning("account_id: %s not found\n", attr->value);
585                         else
586                                 item->account = account;
587                 } else if (!strcmp(attr->name, "apply_sub"))
588                         item->apply_sub = *attr->value == '1' ? TRUE : FALSE;
589         }
590 }
591
592 XMLTag *folder_item_get_xml(Folder *folder, FolderItem *item)
593 {
594         static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
595                                                  "draft", "queue", "trash"};
596         static gchar *sort_key_str[] = {"none", "number", "size", "date",
597                                         "from", "subject", "score", "label",
598                                         "mark", "unread", "mime", "to", 
599                                         "locked"};
600         XMLTag *tag;
601         gchar *value;
602
603         tag = xml_tag_new("folderitem");
604
605         xml_tag_add_attr(tag, xml_attr_new("type", folder_item_stype_str[item->stype]));
606         if (item->name)
607                 xml_tag_add_attr(tag, xml_attr_new("name", item->name));
608         if (item->path)
609                 xml_tag_add_attr(tag, xml_attr_new("path", item->path));
610         if (item->no_sub)
611                 xml_tag_add_attr(tag, xml_attr_new("no_sub", "1"));
612         if (item->no_select)
613                 xml_tag_add_attr(tag, xml_attr_new("no_select", "1"));
614         xml_tag_add_attr(tag, xml_attr_new("collapsed", item->collapsed && item->node->children ? "1" : "0"));
615         xml_tag_add_attr(tag, xml_attr_new("thread_collapsed", item->thread_collapsed ? "1" : "0"));
616         xml_tag_add_attr(tag, xml_attr_new("threaded", item->threaded ? "1" : "0"));
617         xml_tag_add_attr(tag, xml_attr_new("hidereadmsgs", item->hide_read_msgs ? "1" : "0"));
618         if (item->ret_rcpt)
619                 xml_tag_add_attr(tag, xml_attr_new("reqretrcpt", "1"));
620
621         if (item->sort_key != SORT_BY_NONE) {
622                 xml_tag_add_attr(tag, xml_attr_new("sort_key", sort_key_str[item->sort_key]));
623                 xml_tag_add_attr(tag, xml_attr_new("sort_type", item->sort_type == SORT_ASCENDING ? "ascending" : "descending"));
624         }
625
626         value = g_strdup_printf("%ld", (unsigned long int) item->mtime);
627         xml_tag_add_attr(tag, xml_attr_new("mtime", value));
628         g_free(value);
629         xml_tag_add_attr(tag, xml_attr_new_int("new", item->new_msgs));
630         xml_tag_add_attr(tag, xml_attr_new_int("unread", item->unread_msgs));
631         xml_tag_add_attr(tag, xml_attr_new_int("unreadmarked", item->unreadmarked_msgs));
632         xml_tag_add_attr(tag, xml_attr_new_int("marked", item->marked_msgs));
633         xml_tag_add_attr(tag, xml_attr_new_int("total", item->total_msgs));
634
635         if (item->account)
636                 xml_tag_add_attr(tag, xml_attr_new_int("account_id", item->account->account_id));
637         if (item->apply_sub)
638                 xml_tag_add_attr(tag, xml_attr_new("apply_sub", "1"));
639
640         return tag;
641 }
642
643 void folder_set_ui_func(Folder *folder, FolderUIFunc func, gpointer data)
644 {
645         g_return_if_fail(folder != NULL);
646
647         folder->ui_func = func;
648         folder->ui_func_data = data;
649 }
650
651 void folder_set_name(Folder *folder, const gchar *name)
652 {
653         g_return_if_fail(folder != NULL);
654
655         g_free(folder->name);
656         folder->name = name ? g_strdup(name) : NULL;
657         if (folder->node && folder->node->data) {
658                 FolderItem *item = (FolderItem *)folder->node->data;
659
660                 g_free(item->name);
661                 item->name = name ? g_strdup(name) : NULL;
662         }
663 }
664
665 void folder_set_sort(Folder *folder, guint sort)
666 {
667         g_return_if_fail(folder != NULL);
668
669         if (folder->sort != sort) {
670                 folder_remove(folder);
671                 folder->sort = sort;
672                 folder_add(folder);
673         }
674 }
675
676 static gboolean folder_tree_destroy_func(GNode *node, gpointer data) {
677         FolderItem *item = (FolderItem *) node->data;
678
679         folder_item_destroy(item);
680         return FALSE;
681 }
682
683 void folder_tree_destroy(Folder *folder)
684 {
685         GNode *node;
686
687         g_return_if_fail(folder != NULL);
688
689         node = folder->node;
690         
691         prefs_filtering_clear_folder(folder);
692
693         if (node != NULL) {
694                 g_node_traverse(node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
695                                 folder_tree_destroy_func, NULL);
696                 g_node_destroy(node);
697                 folder->node = NULL;
698         }
699 }
700
701 void folder_add(Folder *folder)
702 {
703         Folder *cur_folder;
704         GList *cur;
705         gint i;
706         FolderUpdateData hookdata;
707
708         g_return_if_fail(folder != NULL);
709
710         for (i = 0, cur = folder_list; cur != NULL; cur = cur->next, i++) {
711                 cur_folder = FOLDER(cur->data);
712                 if (cur_folder->sort < folder->sort)
713                         break;
714         }
715
716         folder_list = g_list_insert(folder_list, folder, i);
717
718         hookdata.folder = folder;
719         hookdata.update_flags = FOLDER_ADD_FOLDER;
720         hookdata.item = NULL;
721         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
722 }
723
724 void folder_remove(Folder *folder)
725 {
726         FolderUpdateData hookdata;
727
728         g_return_if_fail(folder != NULL);
729
730         folder_list = g_list_remove(folder_list, folder);
731
732         hookdata.folder = folder;
733         hookdata.update_flags = FOLDER_REMOVE_FOLDER;
734         hookdata.item = NULL;
735         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
736 }
737
738 GList *folder_get_list(void)
739 {
740         return folder_list;
741 }
742
743 gint folder_read_list(void)
744 {
745         GNode *node, *cur;
746         XMLNode *xmlnode;
747         gchar *path;
748
749         path = folder_get_list_path();
750         if (!is_file_exist(path)) return -1;
751         node = xml_parse_file(path);
752         if (!node) return -1;
753
754         xmlnode = node->data;
755         if (strcmp2(xmlnode->tag->tag, "folderlist") != 0) {
756                 g_warning("wrong folder list\n");
757                 xml_free_tree(node);
758                 return -1;
759         }
760
761         cur = node->children;
762         while (cur != NULL) {
763                 Folder *folder;
764
765                 folder = folder_get_from_xml(cur);
766                 if (folder != NULL)
767                         folder_add(folder);
768                 else
769                         folder_unloaded_list = g_slist_append(folder_unloaded_list,
770                                 (gpointer) xml_copy_tree(cur));
771                 cur = cur->next;
772         }
773
774         xml_free_tree(node);
775         if (folder_list || folder_unloaded_list)
776                 return 0;
777         else
778                 return -1;
779 }
780
781 void folder_write_list(void)
782 {
783         GList *list;
784         GSList *slist;
785         Folder *folder;
786         gchar *path;
787         PrefFile *pfile;
788         GNode *rootnode;
789         XMLNode *xmlnode;
790         XMLTag *tag;
791
792         path = folder_get_list_path();
793         if ((pfile = prefs_write_open(path)) == NULL) return;
794
795         xml_file_put_xml_decl(pfile->fp);
796         tag = xml_tag_new("folderlist");
797
798         xmlnode = xml_node_new(tag, NULL);
799
800         rootnode = g_node_new(xmlnode);
801
802         for (list = folder_list; list != NULL; list = list->next) {
803                 GNode *node;
804
805                 folder = list->data;
806                 node = folder_get_xml_node(folder);
807                 if (node != NULL)
808                         g_node_append(rootnode, node);
809         }
810
811         for (slist = folder_unloaded_list; slist != NULL; slist = g_slist_next(slist)) {
812                 GNode *node = (GNode *) slist->data;
813
814                 g_node_append(rootnode, (gpointer) xml_copy_tree(node));
815         }
816
817         xml_write_tree(rootnode, pfile->fp);
818
819         if (prefs_file_close(pfile) < 0)
820                 g_warning("failed to write folder list.\n");
821
822         xml_free_tree(rootnode);
823 }
824
825 static gboolean folder_scan_tree_func(GNode *node, gpointer data)
826 {
827         GHashTable *pptable = (GHashTable *)data;
828         FolderItem *item = (FolderItem *)node->data;
829         
830         folder_item_restore_persist_prefs(item, pptable);
831         folder_item_scan_full(item, FALSE);
832
833         return FALSE;
834 }
835
836 void folder_scan_tree(Folder *folder, gboolean rebuild)
837 {
838         GHashTable *pptable;
839         FolderUpdateData hookdata;
840         Folder *old_folder = folder;
841
842         if (!folder->klass->scan_tree)
843                 return;
844         
845         pptable = folder_persist_prefs_new(folder);
846
847         if (rebuild)
848                 folder_remove(folder);
849
850         if (folder->klass->scan_tree(folder) < 0) {
851                 if (rebuild)
852                         folder_add(old_folder);
853                 return;
854         } else if (rebuild)
855                 folder_add(folder);
856
857         hookdata.folder = folder;
858         hookdata.update_flags = FOLDER_TREE_CHANGED;
859         hookdata.item = NULL;
860         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
861
862         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_scan_tree_func, pptable);
863         folder_persist_prefs_free(pptable);
864
865         prefs_matcher_read_config();
866
867         folder_write_list();
868 }
869
870 static gboolean folder_restore_prefs_func(GNode *node, gpointer data)
871 {
872         GHashTable *pptable = (GHashTable *)data;
873         FolderItem *item = (FolderItem *)node->data;
874         
875         folder_item_restore_persist_prefs(item, pptable);
876
877         return FALSE;
878 }
879
880 void folder_fast_scan_tree(Folder *folder)
881 {
882         GHashTable *pptable;
883         FolderUpdateData hookdata;
884
885         if (!folder->klass->scan_tree)
886                 return;
887         
888         pptable = folder_persist_prefs_new(folder);
889
890         if (folder->klass->scan_tree(folder) < 0) {
891                 return;
892         } 
893
894         hookdata.folder = folder;
895         hookdata.update_flags = FOLDER_TREE_CHANGED;
896         hookdata.item = NULL;
897         hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
898
899         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_restore_prefs_func, pptable);
900         folder_persist_prefs_free(pptable);
901
902         prefs_matcher_read_config();
903
904         folder_write_list();
905 }
906
907 FolderItem *folder_create_folder(FolderItem *parent, const gchar *name)
908 {
909         FolderItem *new_item;
910
911         new_item = parent->folder->klass->create_folder(parent->folder, parent, name);
912         if (new_item) {
913                 FolderUpdateData hookdata;
914
915                 new_item->cache = msgcache_new();
916
917                 hookdata.folder = new_item->folder;
918                 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDERITEM;
919                 hookdata.item = new_item;
920                 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
921         }
922
923         return new_item;
924 }
925
926 gint folder_item_rename(FolderItem *item, gchar *newname)
927 {
928         gint retval;
929
930         g_return_val_if_fail(item != NULL, -1);
931         g_return_val_if_fail(newname != NULL, -1);
932
933         retval = item->folder->klass->rename_folder(item->folder, item, newname);
934
935         if (retval >= 0) {
936                 FolderItemUpdateData hookdata;
937                 FolderUpdateData hookdata2;
938
939                 hookdata.item = item;
940                 hookdata.update_flags = F_ITEM_UPDATE_NAME;
941                 hookdata.msg = NULL;
942                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &hookdata);
943
944                 hookdata2.folder = item->folder;
945                 hookdata2.item = item;
946                 hookdata2.update_flags = FOLDER_RENAME_FOLDERITEM;
947                 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata2);
948         }
949
950         return retval;
951 }
952
953 struct TotalMsgCount
954 {
955         guint new_msgs;
956         guint unread_msgs;
957         guint unreadmarked_msgs;
958         guint marked_msgs;
959         guint total_msgs;
960 };
961
962 struct FuncToAllFoldersData
963 {
964         FolderItemFunc  function;
965         gpointer        data;
966 };
967
968 static gboolean folder_func_to_all_folders_func(GNode *node, gpointer data)
969 {
970         FolderItem *item;
971         struct FuncToAllFoldersData *function_data = (struct FuncToAllFoldersData *) data;
972
973         g_return_val_if_fail(node->data != NULL, FALSE);
974
975         item = FOLDER_ITEM(node->data);
976         g_return_val_if_fail(item != NULL, FALSE);
977
978         function_data->function(item, function_data->data);
979
980         return FALSE;
981 }
982
983 void folder_func_to_all_folders(FolderItemFunc function, gpointer data)
984 {
985         GList *list;
986         Folder *folder;
987         struct FuncToAllFoldersData function_data;
988         
989         function_data.function = function;
990         function_data.data = data;
991
992         for (list = folder_list; list != NULL; list = list->next) {
993                 folder = FOLDER(list->data);
994                 if (folder->node)
995                         g_node_traverse(folder->node, G_PRE_ORDER,
996                                         G_TRAVERSE_ALL, -1,
997                                         folder_func_to_all_folders_func,
998                                         &function_data);
999         }
1000 }
1001
1002 static void folder_count_total_msgs_func(FolderItem *item, gpointer data)
1003 {
1004         struct TotalMsgCount *count = (struct TotalMsgCount *)data;
1005
1006         count->new_msgs += item->new_msgs;
1007         count->unread_msgs += item->unread_msgs;
1008         count->unreadmarked_msgs += item->unreadmarked_msgs;
1009         count->marked_msgs += item->marked_msgs;
1010         count->total_msgs += item->total_msgs;
1011 }
1012
1013 struct TotalMsgStatus
1014 {
1015         guint new;
1016         guint unread;
1017         guint total;
1018         GString *str;
1019 };
1020
1021 static gboolean folder_get_status_full_all_func(GNode *node, gpointer data)
1022 {
1023         FolderItem *item;
1024         struct TotalMsgStatus *status = (struct TotalMsgStatus *)data;
1025         gchar *id;
1026  
1027         g_return_val_if_fail(node->data != NULL, FALSE);
1028  
1029         item = FOLDER_ITEM(node->data);
1030
1031         if (!item->path) return FALSE;
1032
1033         status->new += item->new_msgs;
1034         status->unread += item->unread_msgs;
1035         status->total += item->total_msgs;
1036
1037         if (status->str) {
1038                 id = folder_item_get_identifier(item);
1039                 g_string_append_printf(status->str, "%5d %5d %5d %s\n",
1040                                   item->new_msgs, item->unread_msgs,
1041                                   item->total_msgs, id);
1042                 g_free(id);
1043         }
1044  
1045         return FALSE;
1046  }
1047  
1048 static void folder_get_status_full_all(GString *str, guint *new, guint *unread,
1049                                        guint *total)
1050 {
1051         GList *list;
1052         Folder *folder;
1053         struct TotalMsgStatus status;
1054  
1055         status.new = status.unread = status.total = 0;
1056         status.str = str;
1057  
1058         debug_print("Counting total number of messages...\n");
1059  
1060         for (list = folder_list; list != NULL; list = list->next) {
1061                 folder = FOLDER(list->data);
1062                 if (folder->node)
1063                         g_node_traverse(folder->node, G_PRE_ORDER,
1064                                         G_TRAVERSE_ALL, -1,
1065                                         folder_get_status_full_all_func,
1066                                         &status);
1067         }
1068  
1069         *new = status.new;
1070         *unread = status.unread;
1071         *total = status.total;
1072 }
1073
1074 gchar *folder_get_status(GPtrArray *folders, gboolean full)
1075 {
1076         guint new, unread, total;
1077         GString *str;
1078         gint i;
1079         gchar *ret;
1080
1081         new = unread = total = 0;
1082
1083         str = g_string_new(NULL);
1084
1085         if (folders) {
1086                 for (i = 0; i < folders->len; i++) {
1087                         FolderItem *item;
1088
1089                         item = g_ptr_array_index(folders, i);
1090                         new += item->new_msgs;
1091                         unread += item->unread_msgs;
1092                         total += item->total_msgs;
1093
1094                         if (full) {
1095                                 gchar *id;
1096
1097                                 id = folder_item_get_identifier(item);
1098                                 g_string_append_printf(str, "%5d %5d %5d %s\n",
1099                                                   item->new_msgs, item->unread_msgs,
1100                                                   item->total_msgs, id);
1101                                 g_free(id);
1102                         }
1103                 }
1104         } else {
1105                 folder_get_status_full_all(full ? str : NULL,
1106                                            &new, &unread, &total);
1107         }
1108
1109         if (full)
1110                 g_string_append_printf(str, "%5d %5d %5d\n", new, unread, total);
1111         else
1112                 g_string_append_printf(str, "%d %d %d\n", new, unread, total);
1113
1114         ret = str->str;
1115         g_string_free(str, FALSE);
1116  
1117         return ret;
1118 }
1119
1120 void folder_count_total_msgs(guint *new_msgs, guint *unread_msgs, 
1121                              guint *unreadmarked_msgs, guint *marked_msgs,
1122                              guint *total_msgs)
1123 {
1124         struct TotalMsgCount count;
1125
1126         count.new_msgs = count.unread_msgs = count.unreadmarked_msgs = count.total_msgs = 0;
1127
1128         debug_print("Counting total number of messages...\n");
1129
1130         folder_func_to_all_folders(folder_count_total_msgs_func, &count);
1131
1132         *new_msgs = count.new_msgs;
1133         *unread_msgs = count.unread_msgs;
1134         *unreadmarked_msgs = count.unreadmarked_msgs;
1135         *marked_msgs = count.marked_msgs;
1136         *total_msgs = count.total_msgs;
1137 }
1138
1139 Folder *folder_find_from_path(const gchar *path)
1140 {
1141         GList *list;
1142         Folder *folder;
1143
1144         for (list = folder_list; list != NULL; list = list->next) {
1145                 folder = list->data;
1146                 if ((FOLDER_TYPE(folder) == F_MH || 
1147                      FOLDER_TYPE(folder) == F_MBOX) &&
1148                     !path_cmp(LOCAL_FOLDER(folder)->rootpath, path))
1149                         return folder;
1150         }
1151
1152         return NULL;
1153 }
1154
1155 Folder *folder_find_from_name(const gchar *name, FolderClass *klass)
1156 {
1157         GList *list;
1158         Folder *folder;
1159
1160         for (list = folder_list; list != NULL; list = list->next) {
1161                 folder = list->data;
1162                 if (folder->klass == klass && 
1163                     strcmp2(name, folder->name) == 0)
1164                         return folder;
1165         }
1166
1167         return NULL;
1168 }
1169
1170 static gboolean folder_item_find_func(GNode *node, gpointer data)
1171 {
1172         FolderItem *item = node->data;
1173         gpointer *d = data;
1174         const gchar *path = d[0];
1175
1176         if (path_cmp(path, item->path) != 0)
1177                 return FALSE;
1178
1179         d[1] = item;
1180
1181         return TRUE;
1182 }
1183
1184 FolderItem *folder_find_item_from_path(const gchar *path)
1185 {
1186         Folder *folder;
1187         gpointer d[2];
1188         GList *list = folder_get_list();
1189         
1190         folder = list ? list->data:NULL;
1191         
1192         g_return_val_if_fail(folder != NULL, NULL);
1193
1194         d[0] = (gpointer)path;
1195         d[1] = NULL;
1196         while (d[1] == NULL && list) {
1197                 folder = FOLDER(list->data);
1198                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1199                         folder_item_find_func, d);
1200                 list = list->next;
1201         }
1202         return d[1];
1203 }
1204
1205 FolderItem *folder_find_child_item_by_name(FolderItem *item, const gchar *name)
1206 {
1207         GNode *node;
1208         FolderItem *child;
1209
1210         for (node = item->node->children; node != NULL; node = node->next) {
1211                 gchar *base;
1212                 child = FOLDER_ITEM(node->data);
1213                 base = g_path_get_basename(child->path);
1214                 if (strcmp2(base, name) == 0) {
1215                         g_free(base);
1216                         return child;
1217                 }
1218                 g_free(base);
1219         }
1220
1221         return NULL;
1222 }
1223
1224 FolderClass *folder_get_class_from_string(const gchar *str)
1225 {
1226         GSList *classlist;
1227
1228         classlist = folder_get_class_list();
1229         for (; classlist != NULL; classlist = g_slist_next(classlist)) {
1230                 FolderClass *class = (FolderClass *) classlist->data;
1231                 if (g_ascii_strcasecmp(class->idstr, str) == 0)
1232                         return class;
1233         }
1234
1235         return NULL;
1236 }
1237
1238 static gchar *folder_get_identifier(Folder *folder)
1239 {
1240         gchar *type_str;
1241
1242         g_return_val_if_fail(folder != NULL, NULL);
1243
1244         type_str = folder->klass->idstr;
1245         return g_strconcat("#", type_str, "/", folder->name, NULL);
1246 }
1247
1248 gchar *folder_item_get_identifier(FolderItem *item)
1249 {
1250         gchar *id = NULL;
1251         gchar *folder_id = NULL;
1252
1253         g_return_val_if_fail(item != NULL, NULL);
1254
1255         if (item->path == NULL)
1256                 return NULL;
1257
1258         folder_id = folder_get_identifier(item->folder);
1259         id = g_strconcat(folder_id, "/", item->path, NULL);
1260         g_free(folder_id);
1261
1262         return id;
1263 }
1264
1265 FolderItem *folder_find_item_from_identifier(const gchar *identifier)
1266 {
1267         Folder *folder;
1268         gpointer d[2];
1269         gchar *str;
1270         gchar *p;
1271         gchar *name;
1272         gchar *path;
1273         FolderClass *class;
1274
1275         g_return_val_if_fail(identifier != NULL, NULL);
1276
1277         if (*identifier != '#')
1278                 return folder_find_item_from_path(identifier);
1279
1280         Xstrdup_a(str, identifier, return NULL);
1281
1282         p = strchr(str, '/');
1283         if (!p)
1284                 return folder_find_item_from_path(identifier);
1285         *p = '\0';
1286         p++;
1287         class = folder_get_class_from_string(&str[1]);
1288         if (class == NULL)
1289                 return folder_find_item_from_path(identifier);
1290
1291         name = p;
1292         p = strchr(p, '/');
1293         if (!p)
1294                 return folder_find_item_from_path(identifier);
1295         *p = '\0';
1296         p++;
1297
1298         folder = folder_find_from_name(name, class);
1299         if (!folder)
1300                 return folder_find_item_from_path(identifier);
1301
1302         path = p;
1303
1304         d[0] = (gpointer)path;
1305         d[1] = NULL;
1306         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1307                         folder_item_find_func, d);
1308         return d[1];
1309 }
1310
1311 /**
1312  * Get a displayable name for a FolderItem
1313  *
1314  * \param item FolderItem for that a name should be created
1315  * \return Displayable name for item, returned string has to
1316  *         be freed
1317  */
1318 gchar *folder_item_get_name(FolderItem *item)
1319 {
1320         gchar *name = NULL;
1321
1322         g_return_val_if_fail(item != NULL, g_strdup(""));
1323
1324         switch (item->stype) {
1325         case F_INBOX:
1326                 name = g_strdup(!strcmp2(item->name, INBOX_DIR) ? _("Inbox") :
1327                                 item->name);
1328                 break;
1329         case F_OUTBOX:
1330                 name = g_strdup(!strcmp2(item->name, OUTBOX_DIR) ? _("Sent") :
1331                                 item->name);
1332                 break;
1333         case F_QUEUE:
1334                 name = g_strdup(!strcmp2(item->name, QUEUE_DIR) ? _("Queue") :
1335                                 item->name);
1336                 break;
1337         case F_TRASH:
1338                 name = g_strdup(!strcmp2(item->name, TRASH_DIR) ? _("Trash") :
1339                                 item->name);
1340                 break;
1341         case F_DRAFT:
1342                 name = g_strdup(!strcmp2(item->name, DRAFT_DIR) ? _("Drafts") :
1343                                 item->name);
1344                 break;
1345         default:
1346                 break;
1347         }
1348
1349         if (name == NULL) {
1350                 /*
1351                  * should probably be done by a virtual function,
1352                  * the folder knows the ui string and how to abbrev
1353                 */
1354                 if (folder_item_parent(item) == NULL) {
1355                         name = g_strconcat(item->name, " (", item->folder->klass->uistr, ")", NULL);
1356                 } else {
1357                         if (FOLDER_CLASS(item->folder) == news_get_class() &&
1358                             item->path && !strcmp2(item->name, item->path))
1359                                 name = get_abbrev_newsgroup_name
1360                                         (item->path,
1361                                          prefs_common.ng_abbrev_len);
1362                         else
1363                                 name = g_strdup(item->name);
1364                 }
1365         }
1366
1367         if (name == NULL)
1368                 name = g_strdup("");
1369
1370         return name;
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 void folder_item_scan_foreach_func(gpointer key, gpointer val,
2108                                           gpointer data)
2109 {
2110         folder_item_scan(FOLDER_ITEM(key));
2111 }
2112
2113 void folder_item_scan_foreach(GHashTable *table)
2114 {
2115         g_hash_table_foreach(table, folder_item_scan_foreach_func, NULL);
2116 }
2117
2118 static void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
2119 {
2120         gint *memusage = (gint *)data;
2121
2122         if (item->cache == NULL)
2123                 return;
2124         
2125         *memusage += msgcache_get_memory_usage(item->cache);
2126 }
2127
2128 gint folder_item_syncronize_flags(FolderItem *item)
2129 {
2130         MsgInfoList *msglist = NULL;
2131         GSList *cur;
2132         gint ret = 0;
2133         
2134         g_return_val_if_fail(item != NULL, -1);
2135         g_return_val_if_fail(item->folder != NULL, -1);
2136         g_return_val_if_fail(item->folder->klass != NULL, -1);
2137         if (item->no_select)
2138                 return -1;
2139
2140         item->scanning = TRUE;
2141
2142         if (item->cache == NULL)
2143                 folder_item_read_cache(item);
2144         
2145         msglist = msgcache_get_msg_list(item->cache);
2146         
2147         ret = syncronize_flags(item, msglist);
2148
2149         for (cur = msglist; cur != NULL; cur = g_slist_next(cur))
2150                 procmsg_msginfo_free((MsgInfo *) cur->data);
2151         
2152         g_slist_free(msglist);
2153
2154         item->scanning = FALSE;
2155
2156         return ret;
2157 }
2158
2159 static gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
2160 {
2161         FolderItem *fa = (FolderItem *)a;
2162         FolderItem *fb = (FolderItem *)b;
2163         
2164         return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
2165 }
2166
2167 static void folder_find_expired_caches(FolderItem *item, gpointer data)
2168 {
2169         GSList **folder_item_list = (GSList **)data;
2170         gint difftime, expiretime;
2171         
2172         if (item->cache == NULL)
2173                 return;
2174
2175         if (item->opened > 0)
2176                 return;
2177
2178         difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
2179         expiretime = prefs_common.cache_min_keep_time * 60;
2180         debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
2181
2182         if (difftime > expiretime && !item->opened && !item->processing_pending) {
2183                 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
2184         }
2185 }
2186
2187 gboolean folder_item_free_cache(FolderItem *item, gboolean force)
2188 {
2189         g_return_val_if_fail(item != NULL, TRUE);
2190         
2191         if (item->cache == NULL)
2192                 return TRUE;
2193         
2194         if (item->opened > 0 && !force)
2195                 return FALSE;
2196
2197         folder_item_write_cache(item);
2198         msgcache_destroy(item->cache);
2199         item->cache = NULL;
2200         return TRUE;
2201 }
2202
2203 void folder_clean_cache_memory_force(void)
2204 {
2205         int old_cache_max_mem_usage = prefs_common.cache_max_mem_usage;
2206         int old_cache_min_keep_time = prefs_common.cache_min_keep_time;
2207
2208         prefs_common.cache_max_mem_usage = 0;
2209         prefs_common.cache_min_keep_time = 0;
2210
2211         folder_clean_cache_memory(NULL);
2212
2213         prefs_common.cache_max_mem_usage = old_cache_max_mem_usage;
2214         prefs_common.cache_min_keep_time = old_cache_min_keep_time;
2215 }
2216
2217 void folder_clean_cache_memory(FolderItem *protected_item)
2218 {
2219         gint memusage = 0;
2220
2221         folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);       
2222         debug_print("Total cache memory usage: %d\n", memusage);
2223         
2224         if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
2225                 GSList *folder_item_list = NULL, *listitem;
2226                 
2227                 debug_print("Trying to free cache memory\n");
2228
2229                 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);      
2230                 listitem = folder_item_list;
2231                 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
2232                         FolderItem *item = (FolderItem *)(listitem->data);
2233                         gint cache_size = 0;
2234                         if (item == protected_item) {
2235                                 listitem = listitem->next;
2236                                 continue;
2237                         }
2238                         debug_print("Freeing cache memory for %s\n", item->path ? item->path : item->name);
2239                         cache_size = msgcache_get_memory_usage(item->cache);
2240                         if (folder_item_free_cache(item, FALSE))
2241                                 memusage -= cache_size;
2242
2243                         listitem = listitem->next;
2244                 }
2245                 g_slist_free(folder_item_list);
2246         }
2247 }
2248
2249 static void folder_item_read_cache(FolderItem *item)
2250 {
2251         gchar *cache_file, *mark_file;
2252         START_TIMING("");
2253         g_return_if_fail(item != NULL);
2254
2255         if (item->path != NULL) {
2256                 cache_file = folder_item_get_cache_file(item);
2257                 mark_file = folder_item_get_mark_file(item);
2258                 item->cache = msgcache_read_cache(item, cache_file);
2259                 if (!item->cache) {
2260                         MsgInfoList *list, *cur;
2261                         guint newcnt = 0, unreadcnt = 0;
2262                         guint markedcnt = 0, unreadmarkedcnt = 0;
2263                         MsgInfo *msginfo;
2264
2265                         item->cache = msgcache_new();
2266                         folder_item_scan_full(item, TRUE);
2267
2268                         msgcache_read_mark(item->cache, mark_file);
2269
2270                         list = msgcache_get_msg_list(item->cache);
2271                         for (cur = list; cur != NULL; cur = g_slist_next(cur)) {
2272                                 msginfo = cur->data;
2273
2274                                 if (MSG_IS_NEW(msginfo->flags))
2275                                         newcnt++;
2276                                 if (MSG_IS_UNREAD(msginfo->flags))
2277                                         unreadcnt++;
2278                                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2279                                         unreadmarkedcnt++;
2280                                 if (MSG_IS_MARKED(msginfo->flags))
2281                                         markedcnt++;
2282                         }
2283                         item->new_msgs = newcnt;
2284                         item->unread_msgs = unreadcnt;
2285                         item->unreadmarked_msgs = unreadmarkedcnt;
2286                         item->marked_msgs = markedcnt;
2287                         procmsg_msg_list_free(list);
2288                 } else
2289                         msgcache_read_mark(item->cache, mark_file);
2290
2291                 g_free(cache_file);
2292                 g_free(mark_file);
2293         } else {
2294                 item->cache = msgcache_new();
2295         }
2296         END_TIMING();
2297         folder_clean_cache_memory(item);
2298 }
2299
2300 void folder_item_write_cache(FolderItem *item)
2301 {
2302         gchar *cache_file, *mark_file;
2303         FolderItemPrefs *prefs;
2304         gint filemode = 0;
2305         gchar *id;
2306         time_t last_mtime = (time_t)0;
2307         gboolean need_scan = FALSE;
2308         
2309         if (!item || !item->path || !item->cache)
2310                 return;
2311
2312         if (FOLDER_TYPE(item->folder) == F_MH) {
2313                 last_mtime = item->mtime;
2314                 need_scan = item->folder->klass->scan_required(item->folder, item);
2315         }
2316
2317         id = folder_item_get_identifier(item);
2318         debug_print("Save cache for folder %s\n", id);
2319         g_free(id);
2320
2321         cache_file = folder_item_get_cache_file(item);
2322         mark_file = folder_item_get_mark_file(item);
2323         if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
2324                 prefs = item->prefs;
2325                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
2326                         /* for cache file */
2327                         filemode = prefs->folder_chmod;
2328                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
2329                         if (filemode & S_IROTH) filemode |= S_IWOTH;
2330                         chmod(cache_file, filemode);
2331                 }
2332         }
2333
2334         if (!need_scan && FOLDER_TYPE(item->folder) == F_MH) {
2335                 if (item->mtime == last_mtime) {
2336                         mh_set_mtime(item);
2337                 }
2338         }
2339
2340         g_free(cache_file);
2341         g_free(mark_file);
2342 }
2343
2344 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
2345 {
2346         Folder *folder;
2347         MsgInfo *msginfo = NULL;
2348         
2349         g_return_val_if_fail(item != NULL, NULL);
2350         if (item->no_select)
2351                 return NULL;
2352         folder = item->folder;
2353         if (!item->cache)
2354                 folder_item_read_cache(item);
2355         
2356         if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
2357                 return msginfo;
2358         
2359         msginfo = get_msginfo(item, num);
2360         if (msginfo != NULL) {
2361                 msgcache_add_msg(item->cache, msginfo);
2362                 return msginfo;
2363         }
2364         
2365         return NULL;
2366 }
2367
2368 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
2369 {
2370         Folder *folder;
2371         MsgInfo *msginfo;
2372         
2373         g_return_val_if_fail(item != NULL, NULL);
2374         g_return_val_if_fail(msgid != NULL, NULL);
2375         if (item->no_select)
2376                 return FALSE;
2377         
2378         folder = item->folder;
2379         if (!item->cache)
2380                 folder_item_read_cache(item);
2381         
2382         if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
2383                 return msginfo;
2384
2385         return NULL;
2386 }
2387
2388 GSList *folder_item_get_msg_list(FolderItem *item)
2389 {
2390         g_return_val_if_fail(item != NULL, NULL);
2391         if (item->no_select)
2392                 return FALSE;
2393         
2394         if (item->cache == 0)
2395                 folder_item_read_cache(item);
2396
2397         g_return_val_if_fail(item->cache != NULL, NULL);
2398         
2399         return msgcache_get_msg_list(item->cache);
2400 }
2401
2402 static void msginfo_set_mime_flags(GNode *node, gpointer data)
2403 {
2404         MsgInfo *msginfo = data;
2405         MimeInfo *mimeinfo = node->data;
2406         
2407         if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT
2408          && (!mimeinfo->subtype ||  strcmp(mimeinfo->subtype, "pgp-signature"))) {
2409                 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2410         } else if (mimeinfo->disposition == DISPOSITIONTYPE_UNKNOWN && 
2411                  mimeinfo->type != MIMETYPE_TEXT &&
2412                  mimeinfo->type != MIMETYPE_MULTIPART) {
2413                 if (!mimeinfo->subtype 
2414                 ||  strcmp(mimeinfo->subtype, "pgp-signature"))
2415                         procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2416         } else if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE &&
2417                 strcmp(mimeinfo->subtype, "pgp-signature") && 
2418                 (procmime_mimeinfo_get_parameter(mimeinfo, "name") != NULL ||
2419                  procmime_mimeinfo_get_parameter(mimeinfo, "filename") != NULL)) {
2420                 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2421         } 
2422
2423         /* don't descend below top level message for signed and encrypted info */
2424         if (mimeinfo->type == MIMETYPE_MESSAGE)
2425                 return;
2426
2427         if (privacy_mimeinfo_is_signed(mimeinfo)) {
2428                 procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
2429         }
2430
2431         if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
2432                 procmsg_msginfo_set_flags(msginfo, 0, MSG_ENCRYPTED);
2433         } else {
2434                 /* searching inside encrypted parts doesn't really make sense */
2435                 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2436         }
2437 }
2438
2439 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
2440 {
2441         Folder *folder;
2442         gchar *msgfile;
2443         MsgInfo *msginfo;
2444
2445         g_return_val_if_fail(item != NULL, NULL);
2446
2447         folder = item->folder;
2448
2449         g_return_val_if_fail(folder->klass->fetch_msg != NULL, NULL);
2450         if (item->no_select)
2451                 return NULL;
2452
2453         msgfile = folder->klass->fetch_msg(folder, item, num);
2454
2455         if (msgfile != NULL) {
2456                 msginfo = folder_item_get_msginfo(item, num);
2457                 if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
2458                         MimeInfo *mimeinfo;
2459
2460                         if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) && 
2461                             !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
2462                                 mimeinfo = procmime_scan_file(msgfile);
2463                         else
2464                                 mimeinfo = procmime_scan_queue_file(msgfile);
2465                         /* check for attachments */
2466                         if (mimeinfo != NULL) { 
2467                                 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2468                                 procmime_mimeinfo_free_all(mimeinfo);
2469
2470                                 procmsg_msginfo_set_flags(msginfo, 0, MSG_SCANNED);
2471                         }
2472                 }
2473                 procmsg_msginfo_free(msginfo);
2474         }
2475
2476         return msgfile;
2477 }
2478
2479 gchar *folder_item_fetch_msg_full(FolderItem *item, gint num, gboolean headers,
2480                                   gboolean body)
2481 {
2482         Folder *folder;
2483         gchar *msgfile;
2484         MsgInfo *msginfo;
2485
2486         g_return_val_if_fail(item != NULL, NULL);
2487         if (item->no_select)
2488                 return NULL;
2489         
2490         folder = item->folder;
2491
2492         if (folder->klass->fetch_msg_full == NULL)
2493                 return folder_item_fetch_msg(item, num);
2494
2495         msgfile = folder->klass->fetch_msg_full(folder, item, num, 
2496                                                 headers, body);
2497
2498         if (msgfile != NULL) {
2499                 msginfo = folder_item_get_msginfo(item, num);
2500                 if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
2501                         MimeInfo *mimeinfo;
2502
2503                         if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
2504                             !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
2505                                 mimeinfo = procmime_scan_file(msgfile);
2506                         else
2507                                 mimeinfo = procmime_scan_queue_file(msgfile);
2508                         /* check for attachments */
2509                         if (mimeinfo != NULL) { 
2510                                 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2511                                 procmime_mimeinfo_free_all(mimeinfo);
2512
2513                                 procmsg_msginfo_set_flags(msginfo, 0, MSG_SCANNED);
2514                         }
2515                 }
2516                 procmsg_msginfo_free(msginfo);
2517         }
2518
2519         return msgfile;
2520 }
2521
2522
2523 gint folder_item_fetch_all_msg(FolderItem *item)
2524 {
2525         Folder *folder;
2526         GSList *mlist;
2527         GSList *cur;
2528         gint num = 0;
2529         gint ret = 0;
2530         gint total = 0;
2531
2532         g_return_val_if_fail(item != NULL, -1);
2533         if (item->no_select)
2534                 return -1;
2535
2536         debug_print("fetching all messages in %s ...\n", item->path ? item->path : "(null)");
2537         statusbar_print_all(_("Fetching all messages in %s ...\n"), item->path ? item->path : "(null)");
2538
2539         folder = item->folder;
2540
2541         if (folder->ui_func)
2542                 folder->ui_func(folder, item, folder->ui_func_data ?
2543                                 folder->ui_func_data : GINT_TO_POINTER(num));
2544
2545         mlist = folder_item_get_msg_list(item);
2546
2547         total = g_slist_length(mlist);
2548
2549         for (cur = mlist; cur != NULL; cur = cur->next) {
2550                 MsgInfo *msginfo = (MsgInfo *)cur->data;
2551                 gchar *msg;
2552
2553                 statusbar_progress_all(num++,total, 10);
2554
2555                 if (folder->ui_func)
2556                         folder->ui_func(folder, item,
2557                                         folder->ui_func_data ?
2558                                         folder->ui_func_data :
2559                                         GINT_TO_POINTER(num));
2560
2561                 msg = folder_item_fetch_msg(item, msginfo->msgnum);
2562                 if (!msg) {
2563                         g_warning("Can't fetch message %d. Aborting.\n",
2564                                   msginfo->msgnum);
2565                         ret = -1;
2566                         break;
2567                 }
2568                 g_free(msg);
2569         }
2570         
2571         statusbar_progress_all(0,0,0);
2572         statusbar_pop_all();
2573         procmsg_msg_list_free(mlist);
2574
2575         return ret;
2576 }
2577
2578 static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
2579 {
2580         static HeaderEntry hentry[] = {{"Message-ID:",  NULL, TRUE},
2581                                        {NULL,           NULL, FALSE}};
2582         FILE *fp;
2583         MsgInfo *msginfo;
2584         gint msgnum = 0;
2585         gchar buf[BUFFSIZE];
2586
2587         if ((fp = g_fopen(file, "rb")) == NULL)
2588                 return 0;
2589
2590         if ((folder_has_parent_of_type(dest, F_QUEUE)) || 
2591             (folder_has_parent_of_type(dest, F_DRAFT)))
2592                 while (fgets(buf, sizeof(buf), fp) != NULL)
2593                         if (buf[0] == '\r' || buf[0] == '\n') break;
2594
2595         procheader_get_header_fields(fp, hentry);
2596         if (hentry[0].body) {
2597                 extract_parenthesis(hentry[0].body, '<', '>');
2598                 remove_space(hentry[0].body);
2599                 if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
2600                         msgnum = msginfo->msgnum;
2601                         procmsg_msginfo_free(msginfo);
2602
2603                         debug_print("found message as uid %d\n", msgnum);
2604                 }
2605         }
2606         
2607         g_free(hentry[0].body);
2608         hentry[0].body = NULL;
2609         fclose(fp);
2610
2611         return msgnum;
2612 }
2613
2614 static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
2615 {
2616         MsgPermFlags perm_flags = 0;
2617         MsgTmpFlags tmp_flags = 0;
2618
2619         /* create new flags */
2620         if (source != NULL) {
2621                 /* copy original flags */
2622                 perm_flags = source->flags.perm_flags;
2623                 tmp_flags = source->flags.tmp_flags;
2624         } else {
2625                 perm_flags = dest->flags.perm_flags;
2626                 tmp_flags = dest->flags.tmp_flags;
2627         }
2628
2629         /* remove new, unread and deleted in special folders */
2630         if (folder_has_parent_of_type(dest->folder, F_OUTBOX) || 
2631             folder_has_parent_of_type(dest->folder, F_QUEUE) || 
2632             folder_has_parent_of_type(dest->folder, F_DRAFT) || 
2633             folder_has_parent_of_type(dest->folder, F_TRASH))
2634                 perm_flags &= ~(MSG_NEW | MSG_UNREAD | MSG_DELETED);
2635
2636         /* set ignore flag of ignored parent exists */
2637         if (procmsg_msg_has_flagged_parent(dest, MSG_IGNORE_THREAD))
2638                 perm_flags |= MSG_IGNORE_THREAD;
2639
2640         /* Unset tmp flags that should not be copied */
2641         tmp_flags &= ~(MSG_MOVE | MSG_COPY | MSG_MOVE_DONE);
2642
2643         /* unset flags that are set but should not */
2644         /* and set new flags */
2645         procmsg_msginfo_change_flags(dest,
2646                                   ~dest->flags.perm_flags & perm_flags,
2647                                   ~dest->flags.tmp_flags  & tmp_flags,
2648                                    dest->flags.perm_flags & ~perm_flags,
2649                                    dest->flags.tmp_flags  & ~tmp_flags);
2650 }
2651
2652 static void add_msginfo_to_cache(FolderItem *item, MsgInfo *newmsginfo, MsgInfo *flagsource)
2653 {
2654         /* update folder stats */
2655         if (MSG_IS_NEW(newmsginfo->flags))
2656                 item->new_msgs++;
2657         if (MSG_IS_UNREAD(newmsginfo->flags))
2658                 item->unread_msgs++;
2659         if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
2660                 item->unreadmarked_msgs++;
2661         if (MSG_IS_MARKED(newmsginfo->flags))
2662                 item->marked_msgs++;
2663         item->total_msgs++;
2664
2665         folder_item_update_freeze();
2666
2667         if (!item->cache)
2668                 folder_item_read_cache(item);
2669
2670         msgcache_add_msg(item->cache, newmsginfo);
2671         copy_msginfo_flags(flagsource, newmsginfo);
2672         folder_item_update_with_msg(item,  F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_ADDMSG, newmsginfo);
2673         folder_item_update_thaw();
2674 }
2675
2676 static void remove_msginfo_from_cache(FolderItem *item, MsgInfo *msginfo)
2677 {
2678         MsgInfoUpdate msginfo_update;
2679
2680         if (!item->cache)
2681                 folder_item_read_cache(item);
2682
2683         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2684                 msginfo->folder->new_msgs--;
2685         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2686                 msginfo->folder->unread_msgs--;
2687         if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2688                 msginfo->folder->unreadmarked_msgs--;
2689         if (MSG_IS_MARKED(msginfo->flags))
2690                 item->marked_msgs--;
2691
2692         msginfo->folder->total_msgs--;
2693
2694         msginfo_update.msginfo = msginfo;
2695         msginfo_update.flags = MSGINFO_UPDATE_DELETED;
2696         hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
2697
2698         msgcache_remove_msg(item->cache, msginfo->msgnum);
2699         folder_item_update_with_msg(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_REMOVEMSG, msginfo);
2700 }
2701
2702 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
2703                          MsgFlags *flags, gboolean remove_source)
2704 {
2705         GSList file_list;
2706         MsgFileInfo fileinfo;
2707
2708         g_return_val_if_fail(dest != NULL, -1);
2709         g_return_val_if_fail(file != NULL, -1);
2710  
2711         fileinfo.msginfo = NULL;
2712         fileinfo.file = (gchar *)file;
2713         fileinfo.flags = flags;
2714         file_list.data = &fileinfo;
2715         file_list.next = NULL;
2716
2717         return folder_item_add_msgs(dest, &file_list, remove_source);
2718 }
2719
2720 gint folder_item_add_msgs(FolderItem *dest, GSList *file_list,
2721                           gboolean remove_source)
2722 {
2723         Folder *folder;
2724         gint ret, num, lastnum = -1;
2725         GSList *file_cur;
2726         GRelation *relation;
2727         MsgFileInfo *fileinfo = NULL;
2728         gboolean folderscan = FALSE;
2729
2730         g_return_val_if_fail(dest != NULL, -1);
2731         g_return_val_if_fail(file_list != NULL, -1);
2732         g_return_val_if_fail(dest->folder != NULL, -1);
2733         if (dest->no_select)
2734                 return -1;
2735
2736         folder = dest->folder;
2737
2738         relation = g_relation_new(2);
2739         g_relation_index(relation, 0, g_direct_hash, g_direct_equal);
2740
2741         if (folder->klass->add_msgs != NULL) {
2742                 ret = folder->klass->add_msgs(folder, dest, file_list, relation);
2743                 if (ret < 0) {
2744                         g_relation_destroy(relation);
2745                         return ret;
2746                 }
2747         } else {
2748                 for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
2749                         fileinfo = (MsgFileInfo *) file_cur->data;
2750
2751                         ret = folder->klass->add_msg(folder, dest, fileinfo->file, fileinfo->flags);
2752                         if (ret < 0) {
2753                                 g_relation_destroy(relation);
2754                                 return ret;
2755                         }
2756                         g_relation_insert(relation, fileinfo, GINT_TO_POINTER(ret));
2757                 }
2758         }
2759
2760         for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
2761                 GTuples *tuples;
2762
2763                 fileinfo = (MsgFileInfo *) file_cur->data;
2764                 tuples = g_relation_select(relation, fileinfo, 0);
2765                 num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
2766                 g_tuples_destroy(tuples);
2767
2768                 if (num >= 0) {
2769                         MsgInfo *newmsginfo;
2770
2771                         if (num == 0) {
2772                                 if (!folderscan) {
2773                                         folder_item_scan_full(dest, FALSE);
2774                                         folderscan = TRUE;
2775                                 }
2776                                 num = folder_item_get_msg_num_by_file(dest, fileinfo->file);
2777                         }
2778
2779                         if (num > lastnum)
2780                                 lastnum = num;
2781
2782                         if (num >= 0 && remove_source) {
2783                                 if (g_unlink(fileinfo->file) < 0)
2784                                         FILE_OP_ERROR(fileinfo->file, "unlink");
2785                         }
2786
2787                         if (num == 0)
2788                                 continue;
2789
2790                         if (!folderscan && 
2791                             ((newmsginfo = get_msginfo(dest, num)) != NULL)) {
2792                                 add_msginfo_to_cache(dest, newmsginfo, NULL);
2793                                 procmsg_msginfo_free(newmsginfo);
2794                         } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
2795                                 /* TODO: set default flags */
2796                                 procmsg_msginfo_free(newmsginfo);
2797                         }
2798                 }
2799         }
2800
2801         g_relation_destroy(relation);
2802
2803         return lastnum;
2804 }
2805
2806 static FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest, gboolean copy) 
2807 {
2808         GSList *mlist;
2809         FolderItem *new_item;
2810         FolderItem *next_item;
2811         GNode *srcnode;
2812         gchar *old_id, *new_id;
2813
2814         /* move messages */
2815         debug_print("%s %s to %s\n", copy?"Copying":"Moving", src->path, dest->path);
2816         new_item = folder_create_folder(dest, src->name);
2817         if (new_item == NULL) {
2818                 printf("Can't create folder\n");
2819                 return NULL;
2820         }
2821         
2822         if (new_item->folder == NULL)
2823                 new_item->folder = dest->folder;
2824
2825         /* move messages */
2826         log_message(copy ?_("Copying %s to %s...\n"):_("Moving %s to %s...\n"), 
2827                         src->name, new_item->path);
2828
2829         mlist = folder_item_get_msg_list(src);
2830         
2831         if (mlist != NULL) {
2832                 if (copy)
2833                         folder_item_copy_msgs(new_item, mlist);
2834                 else
2835                         folder_item_move_msgs(new_item, mlist);
2836                 procmsg_msg_list_free(mlist);
2837         }
2838         
2839         /*copy prefs*/
2840         folder_item_prefs_copy_prefs(src, new_item);
2841         
2842         /* copy internal data */
2843         if (src->folder->klass == new_item->folder->klass &&
2844             src->folder->klass->copy_private_data != NULL)
2845                 src->folder->klass->copy_private_data(src->folder,
2846                                         src, new_item);
2847         new_item->collapsed = src->collapsed;
2848         new_item->thread_collapsed = src->thread_collapsed;
2849         new_item->threaded  = src->threaded;
2850         new_item->ret_rcpt  = src->ret_rcpt;
2851         new_item->hide_read_msgs = src->hide_read_msgs;
2852         new_item->sort_key  = src->sort_key;
2853         new_item->sort_type = src->sort_type;
2854
2855         prefs_matcher_write_config();
2856         
2857         /* recurse */
2858         srcnode = src->folder->node;    
2859         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
2860         srcnode = srcnode->children;
2861         while (srcnode != NULL) {
2862                 if (srcnode && srcnode->data) {
2863                         next_item = (FolderItem*) srcnode->data;
2864                         srcnode = srcnode->next;
2865                         if (folder_item_move_recursive(next_item, new_item, copy) == NULL) {
2866                                 return NULL;
2867                         }
2868                 }
2869         }
2870         old_id = folder_item_get_identifier(src);
2871         new_id = folder_item_get_identifier(new_item);
2872
2873         /* if src supports removing, otherwise only copy folder */
2874         if (src->folder->klass->remove_folder != NULL && !copy) 
2875                 src->folder->klass->remove_folder(src->folder, src);
2876         folder_write_list();
2877
2878         if (!copy) {
2879                 debug_print("updating rules : %s => %s\n", old_id, new_id);
2880                 if (old_id != NULL && new_id != NULL) {
2881                         prefs_filtering_rename_path(old_id, new_id);
2882                         account_rename_path(old_id, new_id);
2883                 }
2884         }
2885         g_free(old_id);
2886         g_free(new_id);
2887         
2888         return new_item;
2889 }
2890
2891 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item, gboolean copy)
2892 {
2893         FolderItem *tmp = folder_item_parent(dest);
2894         gchar * src_identifier, * dst_identifier;
2895         gchar * phys_srcpath, * phys_dstpath, *tmppath;
2896
2897         while (tmp) {
2898                 if (tmp == src) {
2899                         return F_MOVE_FAILED_DEST_IS_CHILD;
2900                 }
2901                 tmp = folder_item_parent(tmp);
2902         }
2903         
2904         tmp = folder_item_parent(src);
2905         
2906         src_identifier = folder_item_get_identifier(src);
2907         dst_identifier = folder_item_get_identifier(dest);
2908         
2909         if(dst_identifier == NULL && dest->folder && folder_item_parent(dest) == NULL) {
2910                 /* dest can be a root folder */
2911                 dst_identifier = folder_get_identifier(dest->folder);
2912         }
2913         if (src_identifier == NULL || dst_identifier == NULL) {
2914                 debug_print("Can't get identifiers\n");
2915                 return F_MOVE_FAILED;
2916         }
2917
2918         if (src->folder != dest->folder && !copy) {
2919                 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
2920         }
2921
2922         phys_srcpath = folder_item_get_path(src);
2923         tmppath = folder_item_get_path(dest);
2924         phys_dstpath = g_strconcat(tmppath,
2925                        G_DIR_SEPARATOR_S,
2926                        g_path_get_basename(phys_srcpath),
2927                        NULL);
2928         g_free(tmppath);
2929
2930         if (folder_item_parent(src) == dest || src == dest) {
2931                 g_free(src_identifier);
2932                 g_free(dst_identifier);
2933                 g_free(phys_srcpath);
2934                 g_free(phys_dstpath);
2935                 return F_MOVE_FAILED_DEST_IS_PARENT;
2936         }
2937         debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
2938         if ((tmp = folder_item_move_recursive(src, dest, copy)) == NULL) {
2939                 return F_MOVE_FAILED;
2940         }
2941         
2942         g_free(src_identifier);
2943         g_free(dst_identifier);
2944         g_free(phys_srcpath);
2945         g_free(phys_dstpath);
2946
2947         *new_item = tmp;
2948
2949         return F_MOVE_OK;
2950 }
2951
2952 /**
2953  * Copy a list of message to a new folder and remove
2954  * source messages if wanted
2955  */
2956 static gint do_copy_msgs(FolderItem *dest, GSList *msglist, gboolean remove_source)
2957 {
2958         Folder *folder;
2959         GSList *l;
2960         gint num, lastnum = -1;
2961         gboolean folderscan = FALSE;
2962         GRelation *relation;
2963         GSList *not_moved = NULL;
2964         gint total = 0, curmsg = 0;
2965         MsgInfo *msginfo = NULL;
2966
2967         g_return_val_if_fail(dest != NULL, -1);
2968         g_return_val_if_fail(msglist != NULL, -1);
2969
2970         folder = dest->folder;
2971
2972         g_return_val_if_fail(folder->klass->copy_msg != NULL, -1);
2973         if (dest->no_select)
2974                 return -1;
2975
2976         msginfo = (MsgInfo *)msglist->data;
2977         
2978         if (!msginfo)
2979                 return -1;
2980         
2981         if (!MSG_IS_QUEUED(msginfo->flags) && 
2982             MSG_IS_DRAFT(msginfo->flags) && 
2983             folder_has_parent_of_type(dest, F_QUEUE)) {
2984                 GSList *cur = msglist;
2985                 gboolean queue_err = FALSE;
2986                 for (; cur; cur = cur->next) {
2987                         Compose *compose = NULL;
2988                         FolderItem *queue = dest;
2989                         int val = 0;
2990                         
2991                         msginfo = (MsgInfo *)cur->data;
2992                         compose = compose_reedit(msginfo, TRUE);
2993                         if (compose == NULL) {
2994                                 queue_err = TRUE;
2995                                 continue;
2996                         }
2997                         val = compose_queue(compose, NULL, &queue, NULL,
2998                                         FALSE);
2999                         if (val < 0) {
3000                                 queue_err = TRUE;
3001                         } else if (remove_source) {
3002                                 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
3003                         }
3004                         if (val == 0)
3005                                 compose_close(compose);
3006                 }
3007                 return queue_err ? -1:0;
3008         }
3009
3010         relation = g_relation_new(2);
3011         g_relation_index(relation, 0, g_direct_hash, g_direct_equal);
3012         g_relation_index(relation, 1, g_direct_hash, g_direct_equal);
3013
3014         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
3015                 MsgInfo * msginfo = (MsgInfo *) l->data;
3016
3017                 if (msginfo->planned_download != 0) {
3018                         int old_planned = msginfo->planned_download;
3019                         partial_unmark(msginfo);
3020                         /* little hack to reenable after */
3021                         msginfo->planned_download = old_planned;
3022                 }
3023         }
3024
3025         /* 
3026          * Copy messages to destination folder and 
3027          * store new message numbers in newmsgnums
3028          */
3029         if (folder->klass->copy_msgs != NULL) {
3030                 if (folder->klass->copy_msgs(folder, dest, msglist, relation) < 0) {
3031                         g_relation_destroy(relation);
3032                         return -1;
3033                 }
3034         } else {
3035                 MsgInfo * msginfo;
3036                 l = msglist;
3037
3038                 /* immediately stop if src and dest folders are identical */
3039                 if (l != NULL) {
3040                         msginfo = (MsgInfo *) l->data;
3041                         if (msginfo != NULL && msginfo->folder == dest) {
3042                                 g_relation_destroy(relation);
3043                                 return -1;
3044                         }
3045                 }
3046
3047                 for (; l != NULL ; l = g_slist_next(l)) {
3048                         msginfo = (MsgInfo *) l->data;
3049
3050                         num = folder->klass->copy_msg(folder, dest, msginfo);
3051                         if (num > 0)
3052                                 g_relation_insert(relation, msginfo, GINT_TO_POINTER(num));
3053                         else
3054                                 not_moved = g_slist_prepend(not_moved, msginfo);
3055                 }
3056         }
3057
3058         if (remove_source) {
3059                 MsgInfo *msginfo = (MsgInfo *) msglist->data;
3060                 FolderItem *item = msginfo->folder;
3061                 /*
3062                  * Remove source messages from their folders if
3063                  * copying was successfull and update folder
3064                  * message counts
3065                  */
3066                 if (not_moved == NULL && item->folder->klass->remove_msgs) {
3067                         item->folder->klass->remove_msgs(item->folder,
3068                                                                 msginfo->folder,
3069                                                                 msglist,
3070                                                                 relation);
3071                 }
3072                 for (l = msglist; l != NULL; l = g_slist_next(l)) {
3073                         GTuples *tuples;
3074                         msginfo = (MsgInfo *) l->data;
3075                         item = msginfo->folder;
3076
3077                         tuples = g_relation_select(relation, msginfo, 0);
3078                         if (tuples) {
3079                                 if (tuples->len)
3080                                         num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
3081                                 else
3082                                         num = 0;
3083                                 g_tuples_destroy(tuples);
3084                         } else {
3085                                 num = -1;
3086                         }
3087
3088                         if (g_slist_find(not_moved, msginfo))
3089                                 continue;
3090
3091                         if ((num >= 0) && (item->folder->klass->remove_msg != NULL)) {
3092                                 if (!item->folder->klass->remove_msgs)
3093                                         item->folder->klass->remove_msg(item->folder,
3094                                                                 msginfo->folder,
3095                                                                 msginfo->msgnum);
3096                                 remove_msginfo_from_cache(item, msginfo);
3097                         }
3098                 }
3099         }
3100
3101         /* Read cache for dest folder */
3102         if (!dest->cache) folder_item_read_cache(dest);
3103
3104         /* 
3105          * Fetch new MsgInfos for new messages in dest folder,
3106          * add them to the msgcache and update folder message counts
3107          */
3108         if (g_relation_count(relation, GINT_TO_POINTER(0), 1) > 0) {
3109                 folder_item_scan_full(dest, FALSE);
3110                 folderscan = TRUE;
3111         }
3112
3113         statusbar_print_all(_("Updating cache for %s..."), dest->path ? dest->path : "(null)");
3114         total = g_slist_length(msglist);
3115         for (l = msglist; l != NULL; l = g_slist_next(l)) {
3116                 MsgInfo *msginfo = (MsgInfo *) l->data;
3117                 GTuples *tuples;
3118
3119                 tuples = g_relation_select(relation, msginfo, 0);
3120                 if (tuples->len > 0) {
3121                         num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
3122                         g_tuples_destroy(tuples);
3123                 } else {
3124                         num = 0;
3125                         if (tuples)
3126                                 g_tuples_destroy(tuples);
3127                 }
3128                 statusbar_progress_all(curmsg++,total, 100);
3129                 if (curmsg % 100 == 0)
3130                         GTK_EVENTS_FLUSH();
3131
3132                 if (num >= 0) {
3133                         MsgInfo *newmsginfo = NULL;
3134
3135                         if (folderscan) {
3136                                 if (msginfo->msgid != NULL) {
3137                                         newmsginfo = folder_item_get_msginfo_by_msgid(dest, msginfo->msgid);
3138                                         if (newmsginfo != NULL) {
3139                                                 copy_msginfo_flags(msginfo, newmsginfo);
3140                                                 num = newmsginfo->msgnum;
3141                                         }
3142                                 }
3143                         } else {
3144                                 newmsginfo = get_msginfo(dest, num);
3145                                 if (newmsginfo != NULL) {
3146                                         add_msginfo_to_cache(dest, newmsginfo, msginfo);
3147                                 }
3148                         }
3149
3150                         if (msginfo->planned_download 
3151                             == POP3_PARTIAL_DLOAD_DELE) {
3152                                 partial_mark_for_delete(newmsginfo);
3153                         }
3154                         if (msginfo->planned_download 
3155                             == POP3_PARTIAL_DLOAD_DLOAD) {
3156                                 partial_mark_for_download(newmsginfo);
3157                         }
3158                         if (!MSG_IS_POSTFILTERED (msginfo->flags)) {
3159                             procmsg_msginfo_set_flags (   msginfo, MSG_POSTFILTERED, 0);
3160                             procmsg_msginfo_set_flags (newmsginfo, MSG_POSTFILTERED, 0);
3161                             hooks_invoke (MAIL_POSTFILTERING_HOOKLIST, newmsginfo);
3162                         }
3163                         procmsg_msginfo_free(newmsginfo);
3164
3165
3166                         if (num > lastnum)
3167                                 lastnum = num;
3168                 }
3169         }
3170         statusbar_progress_all(0,0,0);
3171         statusbar_pop_all();
3172
3173         g_relation_destroy(relation);
3174         if (not_moved != NULL) {
3175                 g_slist_free(not_moved);
3176                 return -1;
3177         } else
3178                 return lastnum;
3179 }
3180
3181 /**
3182  * Move a message to a new folder.
3183  *
3184  * \param dest Destination folder
3185  * \param msginfo The message
3186  */
3187 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
3188 {
3189         GSList list;
3190
3191         g_return_val_if_fail(dest != NULL, -1);
3192         g_return_val_if_fail(msginfo != NULL, -1);
3193
3194         list.data = msginfo;
3195         list.next = NULL;
3196
3197         return do_copy_msgs(dest, &list, TRUE);
3198 }
3199
3200 /**
3201  * Move a list of messages to a new folder.
3202  *
3203  * \param dest Destination folder
3204  * \param msglist List of messages
3205  */
3206 gint folder_item_move_msgs(FolderItem *dest, GSList *msglist)
3207 {
3208         gint result = -1;
3209         g_return_val_if_fail(dest != NULL, -1);
3210         g_return_val_if_fail(msglist != NULL, -1);
3211         inc_lock();
3212         result = do_copy_msgs(dest, msglist, TRUE);
3213         inc_unlock();
3214         return result;
3215 }
3216
3217 /**
3218  * Copy a message to a new folder.
3219  *
3220  * \param dest Destination folder
3221  * \param msginfo The message
3222  */
3223 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
3224 {
3225         GSList list;
3226
3227         g_return_val_if_fail(dest != NULL, -1);
3228         g_return_val_if_fail(msginfo != NULL, -1);
3229     
3230         list.data = msginfo;
3231         list.next = NULL;
3232
3233         return do_copy_msgs(dest, &list, FALSE);
3234 }
3235
3236 /**
3237  * Copy a list of messages to a new folder.
3238  *
3239  * \param dest Destination folder
3240  * \param msglist List of messages
3241  */
3242 gint folder_item_copy_msgs(FolderItem *dest, GSList *msglist)
3243 {
3244         gint result;
3245         g_return_val_if_fail(dest != NULL, -1);
3246         g_return_val_if_fail(msglist != NULL, -1);
3247
3248         inc_lock();
3249         result = do_copy_msgs(dest, msglist, FALSE);
3250         inc_unlock();
3251         
3252         return result;
3253 }
3254
3255 gint folder_item_remove_msg(FolderItem *item, gint num)
3256 {
3257         Folder *folder;
3258         gint ret;
3259         MsgInfo *msginfo;
3260
3261         g_return_val_if_fail(item != NULL, -1);
3262         folder = item->folder;
3263         g_return_val_if_fail(folder->klass->remove_msg != NULL, -1);
3264         if (item->no_select)
3265                 return -1;
3266
3267         if (!item->cache) folder_item_read_cache(item);
3268
3269         ret = folder->klass->remove_msg(folder, item, num);
3270
3271         msginfo = msgcache_get_msg(item->cache, num);
3272         if (msginfo != NULL) {
3273                 remove_msginfo_from_cache(item, msginfo);
3274                 procmsg_msginfo_free(msginfo);
3275         }
3276
3277         return ret;
3278 }
3279
3280 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
3281 {
3282         Folder *folder;
3283         gint ret = 0;
3284
3285         g_return_val_if_fail(item != NULL, -1);
3286         folder = item->folder;
3287         g_return_val_if_fail(folder != NULL, -1);
3288         if (item->no_select)
3289                 return -1;
3290         inc_lock();
3291         if (!item->cache) folder_item_read_cache(item);
3292
3293         folder_item_update_freeze();
3294         if (item->folder->klass->remove_msgs) {
3295                 ret = item->folder->klass->remove_msgs(item->folder,
3296                                                         item,
3297                                                         msglist,
3298                                                         NULL);
3299         }
3300         while (ret == 0 && msglist != NULL) {
3301                 MsgInfo *msginfo = (MsgInfo *)msglist->data;
3302                 if (MSG_IS_LOCKED(msginfo->flags)) {
3303                         msglist = msglist->next;
3304                         continue;
3305                 }
3306                 if (!item->folder->klass->remove_msgs)
3307                         ret = folder_item_remove_msg(item, msginfo->msgnum);
3308                 if (ret != 0) break;
3309                 msgcache_remove_msg(item->cache, msginfo->msgnum);
3310                 msglist = msglist->next;
3311         }
3312         folder_item_scan_full(item, FALSE);
3313         folder_item_update_thaw();
3314         inc_unlock();
3315         return ret;
3316 }
3317
3318 gint folder_item_remove_all_msg(FolderItem *item)
3319 {
3320         Folder *folder;
3321         gint result;
3322
3323         g_return_val_if_fail(item != NULL, -1);
3324         if (item->no_select)
3325                 return -1;
3326
3327         folder = item->folder;
3328
3329         inc_lock();
3330         if (folder->klass->remove_all_msg != NULL) {
3331                 result = folder->klass->remove_all_msg(folder, item);
3332
3333                 if (result == 0) {
3334                         folder_item_free_cache(item, TRUE);
3335                         item->cache = msgcache_new();
3336                 }
3337         } else {
3338                 MsgInfoList *msglist;
3339
3340                 msglist = folder_item_get_msg_list(item);
3341                 result = folder_item_remove_msgs(item, msglist);
3342                 procmsg_msg_list_free(msglist);
3343         }
3344
3345         if (result == 0) {
3346                 item->new_msgs = 0;
3347                 item->unread_msgs = 0;
3348                 item->unreadmarked_msgs = 0;
3349                 item->marked_msgs = 0;
3350                 item->total_msgs = 0;
3351                 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
3352         }
3353
3354         inc_unlock();
3355         return result;
3356 }
3357
3358 void folder_item_change_msg_flags(FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
3359 {
3360         g_return_if_fail(item != NULL);
3361         g_return_if_fail(msginfo != NULL);
3362         if (item->no_select)
3363                 return;
3364         
3365         if (item->folder->klass->change_flags != NULL) {
3366                 item->folder->klass->change_flags(item->folder, item, msginfo, newflags);
3367         } else {
3368                 msginfo->flags.perm_flags = newflags;
3369         }
3370 }
3371
3372 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
3373 {
3374         Folder *folder;
3375
3376         g_return_val_if_fail(item != NULL, FALSE);
3377         if (item->no_select)
3378                 return FALSE;
3379
3380         folder = item->folder;
3381
3382         g_return_val_if_fail(folder->klass->is_msg_changed != NULL, -1);
3383
3384         return folder->klass->is_msg_changed(folder, item, msginfo);
3385 }
3386
3387 static gchar *folder_item_get_cache_file(FolderItem *item)
3388 {
3389         gchar *path;
3390         gchar *file;
3391
3392         g_return_val_if_fail(item != NULL, NULL);
3393         g_return_val_if_fail(item->path != NULL, NULL);
3394
3395         path = folder_item_get_path(item);
3396         g_return_val_if_fail(path != NULL, NULL);
3397         if (!is_dir_exist(path))
3398                 make_dir_hier(path);
3399         file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
3400         g_free(path);
3401
3402         return file;
3403 }
3404
3405 static gchar *folder_item_get_mark_file(FolderItem *item)
3406 {
3407         gchar *path;
3408         gchar *file;
3409
3410         g_return_val_if_fail(item != NULL, NULL);
3411         g_return_val_if_fail(item->path != NULL, NULL);
3412
3413         path = folder_item_get_path(item);
3414         g_return_val_if_fail(path != NULL, NULL);
3415         if (!is_dir_exist(path))
3416                 make_dir_hier(path);
3417         file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
3418         g_free(path);
3419
3420         return file;
3421 }
3422
3423 static gpointer xml_to_folder_item(gpointer nodedata, gpointer data)
3424 {
3425         XMLNode *xmlnode = (XMLNode *) nodedata;
3426         Folder *folder = (Folder *) data;
3427         FolderItem *item;
3428
3429         g_return_val_if_fail(xmlnode != NULL, NULL);
3430         g_return_val_if_fail(folder != NULL, NULL);
3431
3432         if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
3433                 g_warning("tag name != \"folderitem\"\n");
3434                 return NULL;
3435         }
3436
3437         item = folder_item_new(folder, "", "");
3438         if (folder->klass->item_set_xml != NULL)
3439                 folder->klass->item_set_xml(folder, item, xmlnode->tag);
3440         else
3441                 folder_item_set_xml(folder, item, xmlnode->tag);
3442
3443         item->folder = folder;
3444
3445         switch (item->stype) {
3446         case F_INBOX:  folder->inbox  = item; break;
3447         case F_OUTBOX: folder->outbox = item; break;
3448         case F_DRAFT:  folder->draft  = item; break;
3449         case F_QUEUE:  folder->queue  = item; break;
3450         case F_TRASH:  folder->trash  = item; break;
3451         default:       break;
3452         }
3453         folder_item_prefs_read_config(item);
3454
3455         return item;
3456 }
3457
3458 static gboolean folder_item_set_node(GNode *node, gpointer data)
3459 {
3460         FolderItem *item = (FolderItem *) node->data;
3461         item->node = node;
3462
3463         return FALSE;
3464 }
3465
3466 static Folder *folder_get_from_xml(GNode *node)
3467 {
3468         Folder *folder;
3469         XMLNode *xmlnode;
3470         GList *list;
3471         FolderClass *klass = NULL;
3472         GNode *cur;
3473
3474         g_return_val_if_fail(node->data != NULL, NULL);
3475
3476         xmlnode = node->data;
3477         if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
3478                 g_warning("tag name != \"folder\"\n");
3479                 return NULL;
3480         }
3481         list = xmlnode->tag->attr;
3482         for (; list != NULL; list = list->next) {
3483                 XMLAttr *attr = list->data;
3484
3485                 if (!attr || !attr->name || !attr->value) continue;
3486                 if (!strcmp(attr->name, "type"))
3487                         klass = folder_get_class_from_string(attr->value);
3488         }
3489         if (klass == NULL)
3490                 return NULL;
3491
3492         folder = folder_new(klass, "", "");
3493         g_return_val_if_fail(folder != NULL, NULL);
3494
3495         if (klass->set_xml)
3496                 klass->set_xml(folder, xmlnode->tag);
3497         else
3498                 folder_set_xml(folder, xmlnode->tag);
3499
3500         cur = node->children;
3501         while (cur != NULL) {
3502                 GNode *itemnode;
3503
3504                 itemnode = g_node_map(cur, xml_to_folder_item, (gpointer) folder);
3505                 g_node_append(folder->node, itemnode);
3506                 cur = cur->next;
3507         }
3508         g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, folder_item_set_node, NULL);
3509         
3510         return folder;
3511 }
3512
3513 static gchar *folder_get_list_path(void)
3514 {
3515         static gchar *filename = NULL;
3516
3517         if (!filename)
3518                 filename =  g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
3519                                         FOLDER_LIST, NULL);
3520
3521         return filename;
3522 }
3523
3524 #define PUT_ESCAPE_STR(fp, attr, str)                   \
3525 {                                                       \
3526         fputs(" " attr "=\"", fp);                      \
3527         xml_file_put_escape_str(fp, str);               \
3528         fputs("\"", fp);                                \
3529 }
3530
3531 static gpointer folder_item_to_xml(gpointer nodedata, gpointer data)
3532 {
3533         FolderItem *item = (FolderItem *) nodedata;
3534         XMLTag *tag;
3535
3536         g_return_val_if_fail(item != NULL, NULL);
3537
3538         if (item->folder->klass->item_get_xml != NULL)
3539                 tag = item->folder->klass->item_get_xml(item->folder, item);
3540         else
3541                 tag = folder_item_get_xml(item->folder, item);
3542
3543         return xml_node_new(tag, NULL);
3544 }
3545
3546 static GNode *folder_get_xml_node(Folder *folder)