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