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