0c6bae35084ca74b6c1f933652cbc85dfdddc745
[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                 child = FOLDER_ITEM(node->data);
1066                 if (strcmp2(g_path_get_basename(child->path), name) == 0) {
1067                         g_free(child->path);
1068                         return child;
1069                 }
1070         }
1071
1072         return NULL;
1073 }
1074
1075 FolderClass *folder_get_class_from_string(const gchar *str)
1076 {
1077         GSList *classlist;
1078
1079         classlist = folder_get_class_list();
1080         for (; classlist != NULL; classlist = g_slist_next(classlist)) {
1081                 FolderClass *class = (FolderClass *) classlist->data;
1082                 if (g_ascii_strcasecmp(class->idstr, str) == 0)
1083                         return class;
1084         }
1085
1086         return NULL;
1087 }
1088
1089 gchar *folder_get_identifier(Folder *folder)
1090 {
1091         gchar *type_str;
1092
1093         g_return_val_if_fail(folder != NULL, NULL);
1094
1095         type_str = folder->klass->idstr;
1096         return g_strconcat("#", type_str, "/", folder->name, NULL);
1097 }
1098
1099 gchar *folder_item_get_identifier(FolderItem *item)
1100 {
1101         gchar *id = NULL;
1102         gchar *folder_id = NULL;
1103
1104         g_return_val_if_fail(item != NULL, NULL);
1105         g_return_val_if_fail(item->path != NULL, NULL);
1106
1107         folder_id = folder_get_identifier(item->folder);
1108         id = g_strconcat(folder_id, "/", item->path, NULL);
1109         g_free(folder_id);
1110
1111         return id;
1112 }
1113
1114 FolderItem *folder_find_item_from_identifier(const gchar *identifier)
1115 {
1116         Folder *folder;
1117         gpointer d[2];
1118         gchar *str;
1119         gchar *p;
1120         gchar *name;
1121         gchar *path;
1122         FolderClass *class;
1123
1124         g_return_val_if_fail(identifier != NULL, NULL);
1125
1126         if (*identifier != '#')
1127                 return folder_find_item_from_path(identifier);
1128
1129         Xstrdup_a(str, identifier, return NULL);
1130
1131         p = strchr(str, '/');
1132         if (!p)
1133                 return folder_find_item_from_path(identifier);
1134         *p = '\0';
1135         p++;
1136         class = folder_get_class_from_string(&str[1]);
1137         if (class == NULL)
1138                 return folder_find_item_from_path(identifier);
1139
1140         name = p;
1141         p = strchr(p, '/');
1142         if (!p)
1143                 return folder_find_item_from_path(identifier);
1144         *p = '\0';
1145         p++;
1146
1147         folder = folder_find_from_name(name, class);
1148         if (!folder)
1149                 return folder_find_item_from_path(identifier);
1150
1151         path = p;
1152
1153         d[0] = (gpointer)path;
1154         d[1] = NULL;
1155         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1156                         folder_item_find_func, d);
1157         return d[1];
1158 }
1159
1160 /**
1161  * Get a displayable name for a FolderItem
1162  *
1163  * \param item FolderItem for that a name should be created
1164  * \return Displayable name for item, returned string has to
1165  *         be freed
1166  */
1167 gchar *folder_item_get_name(FolderItem *item)
1168 {
1169         gchar *name = NULL;
1170
1171         g_return_val_if_fail(item != NULL, g_strdup(""));
1172
1173         switch (item->stype) {
1174         case F_INBOX:
1175                 name = g_strdup(!strcmp2(item->name, INBOX_DIR) ? _("Inbox") :
1176                                 item->name);
1177                 break;
1178         case F_OUTBOX:
1179                 name = g_strdup(!strcmp2(item->name, OUTBOX_DIR) ? _("Sent") :
1180                                 item->name);
1181                 break;
1182         case F_QUEUE:
1183                 name = g_strdup(!strcmp2(item->name, QUEUE_DIR) ? _("Queue") :
1184                                 item->name);
1185                 break;
1186         case F_TRASH:
1187                 name = g_strdup(!strcmp2(item->name, TRASH_DIR) ? _("Trash") :
1188                                 item->name);
1189                 break;
1190         case F_DRAFT:
1191                 name = g_strdup(!strcmp2(item->name, DRAFT_DIR) ? _("Drafts") :
1192                                 item->name);
1193                 break;
1194         default:
1195                 break;
1196         }
1197
1198         if (name == NULL) {
1199                 /*
1200                  * should probably be done by a virtual function,
1201                  * the folder knows the ui string and how to abbrev
1202                 */
1203                 if (folder_item_parent(item) == NULL) {
1204                         name = g_strconcat(item->name, " (", item->folder->klass->uistr, ")", NULL);
1205                 } else {
1206                         if (FOLDER_CLASS(item->folder) == news_get_class() &&
1207                             item->path && !strcmp2(item->name, item->path))
1208                                 name = get_abbrev_newsgroup_name
1209                                         (item->path,
1210                                          prefs_common.ng_abbrev_len);
1211                         else
1212                                 name = g_strdup(item->name);
1213                 }
1214         }
1215
1216         if (name == NULL)
1217                 name = g_strdup("");
1218
1219         return name;
1220 }
1221
1222 Folder *folder_get_default_folder(void)
1223 {
1224         return folder_list ? FOLDER(folder_list->data) : NULL;
1225 }
1226
1227 FolderItem *folder_get_default_inbox(void)
1228 {
1229         GList *flist;
1230
1231         for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1232                 Folder * folder = FOLDER(flist->data);
1233
1234                 if (folder == NULL)
1235                         continue;
1236                 if (folder->inbox == NULL)
1237                         continue;
1238
1239                 return folder->inbox;
1240         }
1241
1242         return NULL;
1243 }
1244
1245 FolderItem *folder_get_default_outbox(void)
1246 {
1247         GList *flist;
1248
1249         for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1250                 Folder * folder = FOLDER(flist->data);
1251
1252                 if (folder == NULL)
1253                         continue;
1254                 if (folder->outbox == NULL)
1255                         continue;
1256
1257                 return folder->outbox;
1258         }
1259
1260         return NULL;
1261 }
1262
1263 FolderItem *folder_get_default_draft(void)
1264 {
1265         GList *flist;
1266
1267         for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1268                 Folder * folder = FOLDER(flist->data);
1269
1270                 if (folder == NULL)
1271                         continue;
1272                 if (folder->draft == NULL)
1273                         continue;
1274
1275                 return folder->draft;
1276         }
1277
1278         return NULL;
1279 }
1280
1281 FolderItem *folder_get_default_queue(void)
1282 {
1283         GList *flist;
1284
1285         for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1286                 Folder * folder = FOLDER(flist->data);
1287
1288                 if (folder == NULL)
1289                         continue;
1290                 if (folder->queue == NULL)
1291                         continue;
1292
1293                 return folder->queue;
1294         }
1295
1296         return NULL;
1297 }
1298
1299 FolderItem *folder_get_default_trash(void)
1300 {
1301         GList *flist;
1302
1303         for (flist = folder_list; flist != NULL; flist = g_list_next(flist)) {
1304                 Folder * folder = FOLDER(flist->data);
1305
1306                 if (folder == NULL)
1307                         continue;
1308                 if (folder->trash == NULL)
1309                         continue;
1310
1311                 return folder->trash;
1312         }
1313
1314         return NULL;
1315 }
1316
1317 #define CREATE_FOLDER_IF_NOT_EXIST(member, dir, type)           \
1318 {                                                               \
1319         if (!folder->member) {                                  \
1320                 item = folder_item_new(folder, dir, dir);       \
1321                 item->stype = type;                             \
1322                 folder_item_append(rootitem, item);             \
1323                 folder->member = item;                          \
1324         }                                                       \
1325 }
1326
1327 void folder_set_missing_folders(void)
1328 {
1329         Folder *folder;
1330         FolderItem *rootitem;
1331         FolderItem *item;
1332         GList *list;
1333
1334         for (list = folder_list; list != NULL; list = list->next) {
1335                 folder = list->data;
1336                 if (FOLDER_TYPE(folder) != F_MH) continue;
1337                 rootitem = FOLDER_ITEM(folder->node->data);
1338                 g_return_if_fail(rootitem != NULL);
1339
1340                 if (folder->inbox && folder->outbox && folder->draft &&
1341                     folder->queue && folder->trash)
1342                         continue;
1343
1344                 if (folder->klass->create_tree(folder) < 0) {
1345                         g_warning("%s: can't create the folder tree.\n",
1346                                   LOCAL_FOLDER(folder)->rootpath);
1347                         continue;
1348                 }
1349
1350                 CREATE_FOLDER_IF_NOT_EXIST(inbox,  INBOX_DIR,  F_INBOX);
1351                 CREATE_FOLDER_IF_NOT_EXIST(outbox, OUTBOX_DIR, F_OUTBOX);
1352                 CREATE_FOLDER_IF_NOT_EXIST(draft,  DRAFT_DIR,  F_DRAFT);
1353                 CREATE_FOLDER_IF_NOT_EXIST(queue,  QUEUE_DIR,  F_QUEUE);
1354                 CREATE_FOLDER_IF_NOT_EXIST(trash,  TRASH_DIR,  F_TRASH);
1355         }
1356 }
1357
1358 static gboolean folder_unref_account_func(GNode *node, gpointer data)
1359 {
1360         FolderItem *item = node->data;
1361         PrefsAccount *account = data;
1362
1363         if (item->account == account)
1364                 item->account = NULL;
1365
1366         return FALSE;
1367 }
1368
1369 void folder_unref_account_all(PrefsAccount *account)
1370 {
1371         Folder *folder;
1372         GList *list;
1373
1374         if (!account) return;
1375
1376         for (list = folder_list; list != NULL; list = list->next) {
1377                 folder = list->data;
1378                 if (folder->account == account)
1379                         folder->account = NULL;
1380                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1381                                 folder_unref_account_func, account);
1382         }
1383 }
1384
1385 #undef CREATE_FOLDER_IF_NOT_EXIST
1386
1387 gchar *folder_item_get_path(FolderItem *item)
1388 {
1389         Folder *folder;
1390
1391         g_return_val_if_fail(item != NULL, NULL);
1392         folder = item->folder;
1393         g_return_val_if_fail(folder != NULL, NULL);
1394
1395         return folder->klass->item_get_path(folder, item);
1396 }
1397
1398 void folder_item_set_default_flags(FolderItem *dest, MsgFlags *flags)
1399 {
1400         if (!(dest->stype == F_OUTBOX ||
1401               dest->stype == F_QUEUE  ||
1402               dest->stype == F_DRAFT  ||
1403               dest->stype == F_TRASH)) {
1404                 flags->perm_flags = MSG_NEW|MSG_UNREAD;
1405         } else {
1406                 flags->perm_flags = 0;
1407         }
1408         if (FOLDER_TYPE(dest->folder) == F_MH) {
1409                 if (dest->stype == F_QUEUE) {
1410                         MSG_SET_TMP_FLAGS(*flags, MSG_QUEUED);
1411                 } else if (dest->stype == F_DRAFT) {
1412                         MSG_SET_TMP_FLAGS(*flags, MSG_DRAFT);
1413                 }
1414         }
1415 }
1416
1417 static gint folder_sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1418 {
1419         MsgInfo *msginfo_a = (MsgInfo *) a;
1420         MsgInfo *msginfo_b = (MsgInfo *) b;
1421
1422         return (msginfo_a->msgnum - msginfo_b->msgnum);
1423 }
1424
1425 static gint folder_sort_folder_list(gconstpointer a, gconstpointer b)
1426 {
1427         guint gint_a = GPOINTER_TO_INT(a);
1428         guint gint_b = GPOINTER_TO_INT(b);
1429         
1430         return (gint_a - gint_b);
1431 }
1432
1433 gint folder_item_open(FolderItem *item)
1434 {
1435         gchar *buf;
1436         if((item->folder->klass->scan_required != NULL) && (item->folder->klass->scan_required(item->folder, item))) {
1437                 folder_item_scan_full(item, TRUE);
1438         }
1439         folder_item_syncronize_flags(item);
1440         
1441         /* Processing */
1442         buf = g_strdup_printf(_("Processing (%s)...\n"), 
1443                               item->path ? item->path : item->name);
1444         debug_print("%s\n", buf);
1445         g_free(buf);
1446         
1447         folder_item_apply_processing(item);
1448
1449         item->opened = TRUE;
1450
1451         debug_print("done.\n");
1452
1453         return 0;
1454 }
1455
1456 gint folder_item_close(FolderItem *item)
1457 {
1458         GSList *mlist, *cur;
1459         Folder *folder;
1460         
1461         g_return_val_if_fail(item != NULL, -1);
1462
1463         if (item->new_msgs) {
1464                 folder_item_update_freeze();
1465                 mlist = folder_item_get_msg_list(item);
1466                 for (cur = mlist ; cur != NULL ; cur = cur->next) {
1467                         MsgInfo * msginfo;
1468
1469                         msginfo = (MsgInfo *) cur->data;
1470                         if (MSG_IS_NEW(msginfo->flags))
1471                                 procmsg_msginfo_unset_flags(msginfo, MSG_NEW, 0);
1472                         procmsg_msginfo_free(msginfo);
1473                 }
1474                 g_slist_free(mlist);
1475                 folder_item_update_thaw();
1476         }               
1477
1478         folder_item_write_cache(item);
1479         
1480         folder_item_update(item, F_ITEM_UPDATE_MSGCNT);
1481
1482         item->opened = FALSE;
1483         folder = item->folder;
1484
1485         if (folder->klass->close == NULL)
1486                 return 0;
1487
1488         return folder->klass->close(folder, item);
1489 }
1490
1491 static MsgInfoList *get_msginfos(FolderItem *item, MsgNumberList *numlist)
1492 {
1493         MsgInfoList *msglist = NULL;
1494         Folder *folder = item->folder;
1495
1496         if (folder->klass->get_msginfos != NULL)
1497                 msglist = folder->klass->get_msginfos(folder, item, numlist);
1498         else {
1499                 MsgNumberList *elem;
1500
1501                 for (elem = numlist; elem != NULL; elem = g_slist_next(elem)) {
1502                         MsgInfo *msginfo;
1503                         guint num;
1504
1505                         num = GPOINTER_TO_INT(elem->data);
1506                         msginfo = folder->klass->get_msginfo(folder, item, num);
1507                         if (msginfo != NULL)
1508                                 msglist = g_slist_prepend(msglist, msginfo);
1509                 }               
1510         }
1511
1512         return msglist;
1513 }
1514
1515 static MsgInfo *get_msginfo(FolderItem *item, guint num)
1516 {
1517         MsgNumberList numlist;
1518         MsgInfoList *msglist;
1519         MsgInfo *msginfo = NULL;
1520
1521         numlist.data = GINT_TO_POINTER(num);
1522         numlist.next = NULL;
1523         msglist = get_msginfos(item, &numlist);
1524         if (msglist != NULL)
1525                 msginfo = procmsg_msginfo_new_ref(msglist->data);
1526         procmsg_msg_list_free(msglist);
1527
1528         return msginfo;
1529 }
1530
1531 static gint syncronize_flags(FolderItem *item, MsgInfoList *msglist)
1532 {
1533         GRelation *relation;
1534         gint ret = 0;
1535         GSList *cur;
1536
1537         if(msglist == NULL)
1538                 return 0;
1539         if(item->folder->klass->get_flags == NULL)
1540                 return 0;
1541
1542         relation = g_relation_new(2);
1543         g_relation_index(relation, 0, g_direct_hash, g_direct_equal);
1544         if ((ret = item->folder->klass->get_flags(
1545             item->folder, item, msglist, relation)) == 0) {
1546                 GTuples *tuples;
1547                 MsgInfo *msginfo;
1548                 MsgPermFlags permflags;
1549                 gboolean skip;
1550
1551                 for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
1552                         msginfo = (MsgInfo *) cur->data;
1553                 
1554                         tuples = g_relation_select(relation, msginfo, 0);
1555                         skip = tuples->len < 1;
1556                         if (!skip)
1557                                 permflags = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
1558                         g_tuples_destroy(tuples);
1559                         if (skip)
1560                                 continue;
1561                         
1562                         if (msginfo->flags.perm_flags != permflags) {
1563                                 procmsg_msginfo_set_flags(msginfo,
1564                                         permflags & ~msginfo->flags.perm_flags, 0);
1565                                 procmsg_msginfo_unset_flags(msginfo,
1566                                         ~permflags & msginfo->flags.perm_flags, 0);
1567                         }
1568                 }
1569         }
1570         g_relation_destroy(relation);   
1571
1572         return ret;
1573 }
1574
1575 gint folder_item_scan_full(FolderItem *item, gboolean filtering)
1576 {
1577         Folder *folder;
1578         GSList *folder_list = NULL, *cache_list = NULL;
1579         GSList *folder_list_cur, *cache_list_cur, *new_list = NULL;
1580         GSList *exists_list = NULL, *elem;
1581         GSList *newmsg_list = NULL;
1582         guint newcnt = 0, unreadcnt = 0, totalcnt = 0, unreadmarkedcnt = 0;
1583         guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
1584         gboolean update_flags = 0, old_uids_valid = FALSE;
1585     
1586         g_return_val_if_fail(item != NULL, -1);
1587         if (item->path == NULL) return -1;
1588
1589         folder = item->folder;
1590
1591         g_return_val_if_fail(folder != NULL, -1);
1592         g_return_val_if_fail(folder->klass->get_num_list != NULL, -1);
1593
1594         debug_print("Scanning folder %s for cache changes.\n", item->path);
1595
1596         /* Get list of messages for folder and cache */
1597         if (folder->klass->get_num_list(item->folder, item, &folder_list, &old_uids_valid) < 0) {
1598                 debug_print("Error fetching list of message numbers\n");
1599                 return(-1);
1600         }
1601
1602         if (old_uids_valid) {
1603                 if (!item->cache)
1604                         folder_item_read_cache(item);
1605                 cache_list = msgcache_get_msg_list(item->cache);
1606         } else {
1607                 if (item->cache)
1608                         msgcache_destroy(item->cache);
1609                 item->cache = msgcache_new();
1610                 cache_list = NULL;
1611         }
1612
1613         /* Sort both lists */
1614         cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
1615         folder_list = g_slist_sort(folder_list, folder_sort_folder_list);
1616
1617         cache_list_cur = cache_list;
1618         folder_list_cur = folder_list;
1619
1620         if (cache_list_cur != NULL) {
1621                 GSList *cache_list_last;
1622         
1623                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1624                 cache_list_last = g_slist_last(cache_list);
1625                 cache_max_num = ((MsgInfo *)cache_list_last->data)->msgnum;
1626         } else {
1627                 cache_cur_num = G_MAXINT;
1628                 cache_max_num = 0;
1629         }
1630
1631         if (folder_list_cur != NULL) {
1632                 GSList *folder_list_last;
1633         
1634                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1635                 folder_list_last = g_slist_last(folder_list);
1636                 folder_max_num = GPOINTER_TO_INT(folder_list_last->data);
1637         } else {
1638                 folder_cur_num = G_MAXINT;
1639                 folder_max_num = 0;
1640         }
1641
1642         while ((cache_cur_num != G_MAXINT) || (folder_cur_num != G_MAXINT)) {
1643                 /*
1644                  *  Message only exists in the folder
1645                  *  Remember message for fetching
1646                  */
1647                 if (folder_cur_num < cache_cur_num) {
1648                         gboolean add = FALSE;
1649
1650                         switch(FOLDER_TYPE(folder)) {
1651                                 case F_NEWS:
1652                                         if (folder_cur_num < cache_max_num)
1653                                                 break;
1654                                         
1655                                         if (folder->account->max_articles == 0) {
1656                                                 add = TRUE;
1657                                         }
1658
1659                                         if (folder_max_num <= folder->account->max_articles) {
1660                                                 add = TRUE;
1661                                         } else if (folder_cur_num > (folder_max_num - folder->account->max_articles)) {
1662                                                 add = TRUE;
1663                                         }
1664                                         break;
1665                                 default:
1666                                         add = TRUE;
1667                                         break;
1668                         }
1669                         
1670                         if (add) {
1671                                 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(folder_cur_num));
1672                                 debug_print("Remembered message %d for fetching\n", folder_cur_num);
1673                         }
1674
1675                         /* Move to next folder number */
1676                         folder_list_cur = folder_list_cur->next;
1677
1678                         if (folder_list_cur != NULL)
1679                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1680                         else
1681                                 folder_cur_num = G_MAXINT;
1682
1683                         continue;
1684                 }
1685
1686                 /*
1687                  *  Message only exists in the cache
1688                  *  Remove the message from the cache
1689                  */
1690                 if (cache_cur_num < folder_cur_num) {
1691                         msgcache_remove_msg(item->cache, cache_cur_num);
1692                         debug_print("Removed message %d from cache.\n", cache_cur_num);
1693
1694                         /* Move to next cache number */
1695                         cache_list_cur = cache_list_cur->next;
1696
1697                         if (cache_list_cur != NULL)
1698                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1699                         else
1700                                 cache_cur_num = G_MAXINT;
1701
1702                         update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1703
1704                         continue;
1705                 }
1706
1707                 /*
1708                  *  Message number exists in folder and cache!
1709                  *  Check if the message has been modified
1710                  */
1711                 if (cache_cur_num == folder_cur_num) {
1712                         MsgInfo *msginfo;
1713
1714                         msginfo = msgcache_get_msg(item->cache, folder_cur_num);
1715                         if (folder->klass->is_msg_changed && folder->klass->is_msg_changed(folder, item, msginfo)) {
1716                                 msgcache_remove_msg(item->cache, msginfo->msgnum);
1717                                 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(msginfo->msgnum));
1718                                 procmsg_msginfo_free(msginfo);
1719
1720                                 debug_print("Remembering message %d to update...\n", folder_cur_num);
1721                         } else
1722                                 exists_list = g_slist_prepend(exists_list, msginfo);
1723
1724                         /* Move to next folder and cache number */
1725                         cache_list_cur = cache_list_cur->next;
1726                         folder_list_cur = folder_list_cur->next;
1727
1728                         if (cache_list_cur != NULL)
1729                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1730                         else
1731                                 cache_cur_num = G_MAXINT;
1732
1733                         if (folder_list_cur != NULL)
1734                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1735                         else
1736                                 folder_cur_num = G_MAXINT;
1737
1738                         continue;
1739                 }
1740         }
1741         
1742         for(cache_list_cur = cache_list; cache_list_cur != NULL; cache_list_cur = g_slist_next(cache_list_cur))
1743                 procmsg_msginfo_free((MsgInfo *) cache_list_cur->data);
1744
1745         g_slist_free(cache_list);
1746         g_slist_free(folder_list);
1747
1748         if (new_list != NULL) {
1749                 newmsg_list = get_msginfos(item, new_list);
1750                 g_slist_free(new_list);
1751         }
1752
1753         syncronize_flags(item, exists_list);
1754
1755         folder_item_update_freeze();
1756         if (newmsg_list != NULL) {
1757                 GSList *elem;
1758
1759                 for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
1760                         MsgInfo *msginfo = (MsgInfo *) elem->data;
1761
1762                         msgcache_add_msg(item->cache, msginfo);
1763                         if ((filtering == TRUE) &&
1764                             (item->stype == F_INBOX) &&
1765                             (item->folder->account != NULL) && 
1766                             (item->folder->account->filter_on_recv) &&
1767                             procmsg_msginfo_filter(msginfo))
1768                                 procmsg_msginfo_free(msginfo);
1769                         else
1770                                 exists_list = g_slist_prepend(exists_list, msginfo);
1771                 }
1772                 g_slist_free(newmsg_list);
1773
1774                 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1775         }
1776
1777         for (elem = exists_list; elem != NULL; elem = g_slist_next(elem)) {
1778                 MsgInfo *msginfo;
1779
1780                 msginfo = elem->data;
1781                 if (MSG_IS_IGNORE_THREAD(msginfo->flags) && (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1782                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1783                 if (!MSG_IS_IGNORE_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD)) {
1784                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1785                         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
1786                 }
1787                 if ((item->stype == F_OUTBOX ||
1788                      item->stype == F_QUEUE  ||
1789                      item->stype == F_DRAFT  ||
1790                      item->stype == F_TRASH) &&
1791                     (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
1792                         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
1793                 if (MSG_IS_NEW(msginfo->flags))
1794                         newcnt++;
1795                 if (MSG_IS_UNREAD(msginfo->flags))
1796                         unreadcnt++;
1797                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1798                         unreadmarkedcnt++;
1799                 totalcnt++;
1800
1801                 procmsg_msginfo_free(msginfo);
1802         }
1803         g_slist_free(exists_list);
1804
1805         item->new_msgs = newcnt;
1806         item->unread_msgs = unreadcnt;
1807         item->total_msgs = totalcnt;
1808         item->unreadmarked_msgs = unreadmarkedcnt;
1809
1810         update_flags |= F_ITEM_UPDATE_MSGCNT;
1811
1812         folder_item_update(item, update_flags);
1813         folder_item_update_thaw();
1814
1815         return 0;
1816 }
1817
1818 gint folder_item_scan(FolderItem *item)
1819 {
1820         return folder_item_scan_full(item, TRUE);
1821 }
1822
1823 static gboolean folder_scan_all_items_func(GNode *node, gpointer data)
1824 {
1825         FolderItem *item = node->data;
1826
1827         folder_item_scan(item);
1828
1829         return FALSE;
1830 }
1831
1832 void folder_scan_all_items(Folder * folder)
1833 {
1834         g_node_traverse(folder->node, G_PRE_ORDER,
1835                         G_TRAVERSE_ALL, -1, folder_scan_all_items_func, NULL);
1836 }
1837
1838 static void folder_item_scan_foreach_func(gpointer key, gpointer val,
1839                                           gpointer data)
1840 {
1841         folder_item_scan(FOLDER_ITEM(key));
1842 }
1843
1844 void folder_item_scan_foreach(GHashTable *table)
1845 {
1846         g_hash_table_foreach(table, folder_item_scan_foreach_func, NULL);
1847 }
1848
1849 void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
1850 {
1851         gint *memusage = (gint *)data;
1852
1853         if (item->cache == NULL)
1854                 return;
1855         
1856         *memusage += msgcache_get_memory_usage(item->cache);
1857 }
1858
1859 gint folder_item_syncronize_flags(FolderItem *item)
1860 {
1861         MsgInfoList *msglist = NULL;
1862         GSList *cur;
1863         gint ret = 0;
1864         
1865         g_return_val_if_fail(item != NULL, -1);
1866         g_return_val_if_fail(item->folder != NULL, -1);
1867         g_return_val_if_fail(item->folder->klass != NULL, -1);
1868         
1869         if (item->cache == NULL)
1870                 folder_item_read_cache(item);
1871         
1872         msglist = msgcache_get_msg_list(item->cache);
1873         
1874         ret = syncronize_flags(item, msglist);
1875
1876         for (cur = msglist; cur != NULL; cur = g_slist_next(cur))
1877                 procmsg_msginfo_free((MsgInfo *) cur->data);
1878         
1879         return ret;
1880 }
1881
1882 gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
1883 {
1884         FolderItem *fa = (FolderItem *)a;
1885         FolderItem *fb = (FolderItem *)b;
1886         
1887         return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
1888 }
1889
1890 void folder_find_expired_caches(FolderItem *item, gpointer data)
1891 {
1892         GSList **folder_item_list = (GSList **)data;
1893         gint difftime, expiretime;
1894         
1895         if (item->cache == NULL)
1896                 return;
1897
1898         if (item->opened > 0)
1899                 return;
1900
1901         difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
1902         expiretime = prefs_common.cache_min_keep_time * 60;
1903         debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
1904         if (difftime > expiretime) {
1905                 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
1906         }
1907 }
1908
1909 void folder_item_free_cache(FolderItem *item)
1910 {
1911         g_return_if_fail(item != NULL);
1912         
1913         if (item->cache == NULL)
1914                 return;
1915         
1916         if (item->opened > 0)
1917                 return;
1918
1919         folder_item_write_cache(item);
1920         msgcache_destroy(item->cache);
1921         item->cache = NULL;
1922 }
1923
1924 void folder_clean_cache_memory(void)
1925 {
1926         gint memusage = 0;
1927
1928         folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);       
1929         debug_print("Total cache memory usage: %d\n", memusage);
1930         
1931         if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
1932                 GSList *folder_item_list = NULL, *listitem;
1933                 
1934                 debug_print("Trying to free cache memory\n");
1935
1936                 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);      
1937                 listitem = folder_item_list;
1938                 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
1939                         FolderItem *item = (FolderItem *)(listitem->data);
1940
1941                         debug_print("Freeing cache memory for %s\n", item->path);
1942                         memusage -= msgcache_get_memory_usage(item->cache);
1943                         folder_item_free_cache(item);
1944                         listitem = listitem->next;
1945                 }
1946                 g_slist_free(folder_item_list);
1947         }
1948 }
1949
1950 void folder_item_read_cache(FolderItem *item)
1951 {
1952         gchar *cache_file, *mark_file;
1953         
1954         g_return_if_fail(item != NULL);
1955
1956         if (item->path != NULL) {
1957                 cache_file = folder_item_get_cache_file(item);
1958                 mark_file = folder_item_get_mark_file(item);
1959                 item->cache = msgcache_read_cache(item, cache_file);
1960                 if (!item->cache) {
1961                         MsgInfoList *list, *cur;
1962                         guint newcnt = 0, unreadcnt = 0, unreadmarkedcnt = 0;
1963                         MsgInfo *msginfo;
1964
1965                         item->cache = msgcache_new();
1966                         folder_item_scan_full(item, TRUE);
1967
1968                         msgcache_read_mark(item->cache, mark_file);
1969
1970                         list = msgcache_get_msg_list(item->cache);
1971                         for (cur = list; cur != NULL; cur = g_slist_next(cur)) {
1972                                 msginfo = cur->data;
1973
1974                                 if (MSG_IS_NEW(msginfo->flags))
1975                                         newcnt++;
1976                                 if (MSG_IS_UNREAD(msginfo->flags))
1977                                         unreadcnt++;
1978                                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1979                                         unreadmarkedcnt++;
1980                         }
1981                         item->new_msgs = newcnt;
1982                         item->unread_msgs = unreadcnt;
1983                         item->unreadmarked_msgs = unreadmarkedcnt;
1984                         procmsg_msg_list_free(list);
1985                 } else
1986                         msgcache_read_mark(item->cache, mark_file);
1987
1988                 g_free(cache_file);
1989                 g_free(mark_file);
1990         } else {
1991                 item->cache = msgcache_new();
1992         }
1993
1994         folder_clean_cache_memory();
1995 }
1996
1997 void folder_item_write_cache(FolderItem *item)
1998 {
1999         gchar *cache_file, *mark_file;
2000         FolderItemPrefs *prefs;
2001         gint filemode = 0;
2002         gchar *id;
2003         
2004         if (!item || !item->path || !item->cache)
2005                 return;
2006
2007         id = folder_item_get_identifier(item);
2008         debug_print("Save cache for folder %s\n", id);
2009         g_free(id);
2010
2011         cache_file = folder_item_get_cache_file(item);
2012         mark_file = folder_item_get_mark_file(item);
2013         if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
2014                 prefs = item->prefs;
2015                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
2016                         /* for cache file */
2017                         filemode = prefs->folder_chmod;
2018                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
2019                         if (filemode & S_IROTH) filemode |= S_IWOTH;
2020                         chmod(cache_file, filemode);
2021                 }
2022         }
2023
2024         g_free(cache_file);
2025         g_free(mark_file);
2026 }
2027
2028 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
2029 {
2030         Folder *folder;
2031         MsgInfo *msginfo = NULL;
2032         
2033         g_return_val_if_fail(item != NULL, NULL);
2034         
2035         folder = item->folder;
2036         if (!item->cache)
2037                 folder_item_read_cache(item);
2038         
2039         if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
2040                 return msginfo;
2041         
2042         msginfo = get_msginfo(item, num);
2043         if (msginfo != NULL) {
2044                 msgcache_add_msg(item->cache, msginfo);
2045                 return msginfo;
2046         }
2047         
2048         return NULL;
2049 }
2050
2051 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
2052 {
2053         Folder *folder;
2054         MsgInfo *msginfo;
2055         
2056         g_return_val_if_fail(item != NULL, NULL);
2057         g_return_val_if_fail(msgid != NULL, NULL);
2058         
2059         folder = item->folder;
2060         if (!item->cache)
2061                 folder_item_read_cache(item);
2062         
2063         if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
2064                 return msginfo;
2065
2066         return NULL;
2067 }
2068
2069 GSList *folder_item_get_msg_list(FolderItem *item)
2070 {
2071         g_return_val_if_fail(item != NULL, NULL);
2072         
2073         if (item->cache == 0)
2074                 folder_item_read_cache(item);
2075
2076         g_return_val_if_fail(item->cache != NULL, NULL);
2077         
2078         return msgcache_get_msg_list(item->cache);
2079 }
2080
2081 static void msginfo_set_mime_flags(GNode *node, gpointer data)
2082 {
2083         MsgInfo *msginfo = data;
2084         MimeInfo *mimeinfo = node->data;
2085
2086         if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT) {
2087                 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2088         } else if (mimeinfo->disposition == DISPOSITIONTYPE_UNKNOWN && 
2089                  mimeinfo->type != MIMETYPE_TEXT &&
2090                  mimeinfo->type != MIMETYPE_MULTIPART) {
2091                 procmsg_msginfo_set_flags(msginfo, 0, MSG_HAS_ATTACHMENT);
2092         }
2093
2094         /* don't descend below top level message for signed and encrypted info */
2095         if (mimeinfo->type == MIMETYPE_MESSAGE)
2096                 return;
2097
2098         if (privacy_mimeinfo_is_signed(mimeinfo)) {
2099                 procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
2100         }
2101
2102         if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
2103                 procmsg_msginfo_set_flags(msginfo, 0, MSG_ENCRYPTED);
2104         } else {
2105                 /* searching inside encrypted parts doesn't really make sense */
2106                 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2107         }
2108 }
2109
2110 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
2111 {
2112         Folder *folder;
2113         gchar *msgfile;
2114         MsgInfo *msginfo;
2115
2116         g_return_val_if_fail(item != NULL, NULL);
2117
2118         folder = item->folder;
2119
2120         g_return_val_if_fail(folder->klass->fetch_msg != NULL, NULL);
2121
2122         msgfile = folder->klass->fetch_msg(folder, item, num);
2123
2124         if (msgfile != NULL) {
2125                 msginfo = folder_item_get_msginfo(item, num);
2126                 if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
2127                         MimeInfo *mimeinfo;
2128
2129                         if (msginfo->folder->stype != F_QUEUE && 
2130                             msginfo->folder->stype != F_DRAFT)
2131                                 mimeinfo = procmime_scan_file(msgfile);
2132                         else
2133                                 mimeinfo = procmime_scan_queue_file(msgfile);
2134                         /* check for attachments */
2135                         if (mimeinfo != NULL) { 
2136                                 g_node_children_foreach(mimeinfo->node, G_TRAVERSE_ALL, msginfo_set_mime_flags, msginfo);
2137                                 procmime_mimeinfo_free_all(mimeinfo);
2138
2139                                 procmsg_msginfo_set_flags(msginfo, 0, MSG_SCANNED);
2140                         }
2141                 }
2142         }
2143
2144         return msgfile;
2145 }
2146
2147 gint folder_item_fetch_all_msg(FolderItem *item)
2148 {
2149         Folder *folder;
2150         GSList *mlist;
2151         GSList *cur;
2152         gint num = 0;
2153         gint ret = 0;
2154
2155         g_return_val_if_fail(item != NULL, -1);
2156
2157         debug_print("fetching all messages in %s ...\n", item->path);
2158
2159         folder = item->folder;
2160
2161         if (folder->ui_func)
2162                 folder->ui_func(folder, item, folder->ui_func_data ?
2163                                 folder->ui_func_data : GINT_TO_POINTER(num));
2164
2165         mlist = folder_item_get_msg_list(item);
2166
2167         for (cur = mlist; cur != NULL; cur = cur->next) {
2168                 MsgInfo *msginfo = (MsgInfo *)cur->data;
2169                 gchar *msg;
2170
2171                 num++;
2172                 if (folder->ui_func)
2173                         folder->ui_func(folder, item,
2174                                         folder->ui_func_data ?
2175                                         folder->ui_func_data :
2176                                         GINT_TO_POINTER(num));
2177
2178                 msg = folder_item_fetch_msg(item, msginfo->msgnum);
2179                 if (!msg) {
2180                         g_warning("Can't fetch message %d. Aborting.\n",
2181                                   msginfo->msgnum);
2182                         ret = -1;
2183                         break;
2184                 }
2185                 g_free(msg);
2186         }
2187
2188         procmsg_msg_list_free(mlist);
2189
2190         return ret;
2191 }
2192
2193 static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
2194 {
2195         static HeaderEntry hentry[] = {{"Message-ID:",  NULL, TRUE},
2196                                        {NULL,           NULL, FALSE}};
2197         FILE *fp;
2198         MsgInfo *msginfo;
2199         gint msgnum = 0;
2200         gchar buf[BUFFSIZE];
2201
2202         if ((fp = fopen(file, "rb")) == NULL)
2203                 return 0;
2204
2205         if ((dest->stype == F_QUEUE) || (dest->stype == F_DRAFT))
2206                 while (fgets(buf, sizeof(buf), fp) != NULL)
2207                         if (buf[0] == '\r' || buf[0] == '\n') break;
2208
2209         procheader_get_header_fields(fp, hentry);
2210         if (hentry[0].body) {
2211                 extract_parenthesis(hentry[0].body, '<', '>');
2212                 remove_space(hentry[0].body);
2213                 if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
2214                         msgnum = msginfo->msgnum;
2215                         procmsg_msginfo_free(msginfo);
2216
2217                         debug_print("found message as uid %d\n", msgnum);
2218                 }
2219         }
2220         
2221         g_free(hentry[0].body);
2222         hentry[0].body = NULL;
2223         fclose(fp);
2224
2225         return msgnum;
2226 }
2227
2228 static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
2229 {
2230         MsgPermFlags perm_flags = 0;
2231         MsgTmpFlags tmp_flags = 0;
2232
2233         /* create new flags */
2234         if (source != NULL) {
2235                 /* copy original flags */
2236                 perm_flags = source->flags.perm_flags;
2237                 tmp_flags = source->flags.tmp_flags;
2238         } else {
2239                 perm_flags = dest->flags.perm_flags;
2240                 tmp_flags = dest->flags.tmp_flags;
2241         }
2242
2243         /* remove new, unread and deleted in special folders */
2244         if (dest->folder->stype == F_OUTBOX ||
2245             dest->folder->stype == F_QUEUE  ||
2246             dest->folder->stype == F_DRAFT  ||
2247             dest->folder->stype == F_TRASH)
2248                 perm_flags &= ~(MSG_NEW | MSG_UNREAD | MSG_DELETED);
2249
2250         /* set ignore flag of ignored parent exists */
2251         if (procmsg_msg_has_flagged_parent(dest, MSG_IGNORE_THREAD))
2252                 perm_flags |= MSG_IGNORE_THREAD;
2253
2254         /* Unset tmp flags that should not be copied */
2255         tmp_flags &= ~(MSG_MOVE | MSG_COPY);
2256
2257         /* unset flags that are set but should not */
2258         procmsg_msginfo_unset_flags(dest,
2259                                     dest->flags.perm_flags & ~perm_flags,
2260                                     dest->flags.tmp_flags  & ~tmp_flags);
2261         /* set new flags */
2262         procmsg_msginfo_set_flags(dest,
2263                                   ~dest->flags.perm_flags & perm_flags,
2264                                   ~dest->flags.tmp_flags  & tmp_flags);
2265 }
2266
2267 static void add_msginfo_to_cache(FolderItem *item, MsgInfo *newmsginfo, MsgInfo *flagsource)
2268 {
2269         /* update folder stats */
2270         if (MSG_IS_NEW(newmsginfo->flags))
2271                 item->new_msgs++;
2272         if (MSG_IS_UNREAD(newmsginfo->flags))
2273                 item->unread_msgs++;
2274         if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
2275                 item->unreadmarked_msgs++;
2276         item->total_msgs++;
2277
2278         folder_item_update_freeze();
2279         msgcache_add_msg(item->cache, newmsginfo);
2280         copy_msginfo_flags(flagsource, newmsginfo);
2281         folder_item_update_with_msg(item,  F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_ADDMSG, newmsginfo);
2282         folder_item_update_thaw();
2283 }
2284
2285 static void remove_msginfo_from_cache(FolderItem *item, MsgInfo *msginfo)
2286 {
2287         MsgInfoUpdate msginfo_update;
2288
2289         if (!item->cache)
2290                 folder_item_read_cache(item);
2291
2292         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2293                 msginfo->folder->new_msgs--;
2294         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2295                 msginfo->folder->unread_msgs--;
2296         if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2297                 msginfo->folder->unreadmarked_msgs--;
2298         msginfo->folder->total_msgs--;
2299
2300         msginfo_update.msginfo = msginfo;
2301         msginfo_update.flags = MSGINFO_UPDATE_DELETED;
2302         hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
2303
2304         msgcache_remove_msg(item->cache, msginfo->msgnum);
2305         folder_item_update_with_msg(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT | F_ITEM_UPDATE_REMOVEMSG, msginfo);
2306 }
2307
2308 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
2309                          MsgFlags *flags, gboolean remove_source)
2310 {
2311         GSList file_list;
2312         MsgFileInfo fileinfo;
2313
2314         g_return_val_if_fail(dest != NULL, -1);
2315         g_return_val_if_fail(file != NULL, -1);
2316  
2317         fileinfo.msginfo = NULL;
2318         fileinfo.file = (gchar *)file;
2319         fileinfo.flags = flags;
2320         file_list.data = &fileinfo;
2321         file_list.next = NULL;
2322
2323         return folder_item_add_msgs(dest, &file_list, remove_source);
2324 }
2325
2326 gint folder_item_add_msgs(FolderItem *dest, GSList *file_list,
2327                           gboolean remove_source)
2328 {
2329         Folder *folder;
2330         gint ret, num, lastnum = -1;
2331         GSList *file_cur;
2332         GRelation *relation;
2333         MsgFileInfo *fileinfo = NULL;
2334         gboolean folderscan = FALSE;
2335
2336         g_return_val_if_fail(dest != NULL, -1);
2337         g_return_val_if_fail(file_list != NULL, -1);
2338         g_return_val_if_fail(dest->folder != NULL, -1);
2339
2340         folder = dest->folder;
2341
2342         relation = g_relation_new(2);
2343         g_relation_index(relation, 0, g_direct_hash, g_direct_equal);
2344
2345         if (folder->klass->add_msgs != NULL) {
2346                 ret = folder->klass->add_msgs(folder, dest, file_list, relation);
2347                 if (ret < 0) {
2348                         g_relation_destroy(relation);
2349                         return ret;
2350                 }
2351         } else {
2352                 for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
2353                         fileinfo = (MsgFileInfo *) file_cur->data;
2354
2355                         ret = folder->klass->add_msg(folder, dest, fileinfo->file, fileinfo->flags);
2356                         if (ret < 0) {
2357                                 g_relation_destroy(relation);
2358                                 return ret;
2359                         }
2360                         g_relation_insert(relation, fileinfo, GINT_TO_POINTER(ret));
2361                 }
2362         }
2363
2364         for (file_cur = file_list; file_cur != NULL; file_cur = g_slist_next(file_cur)) {
2365                 GTuples *tuples;
2366
2367                 fileinfo = (MsgFileInfo *) file_cur->data;
2368                 tuples = g_relation_select(relation, fileinfo, 0);
2369                 num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
2370                 g_tuples_destroy(tuples);
2371
2372                 if (num >= 0) {
2373                         MsgInfo *newmsginfo;
2374
2375                         if (num == 0) {
2376                                 if (!folderscan) {
2377                                         folder_item_scan_full(dest, FALSE);
2378                                         folderscan = TRUE;
2379                                 }
2380                                 num = folder_item_get_msg_num_by_file(dest, fileinfo->file);
2381                         }
2382
2383                         if (num > lastnum)
2384                                 lastnum = num;
2385
2386                         if (num >= 0 && remove_source) {
2387                                 if (unlink(fileinfo->file) < 0)
2388                                         FILE_OP_ERROR(fileinfo->file, "unlink");
2389                         }
2390
2391                         if (num == 0)
2392                                 continue;
2393
2394                         if (!folderscan && 
2395                             ((newmsginfo = get_msginfo(dest, num)) != NULL)) {
2396                                 add_msginfo_to_cache(dest, newmsginfo, NULL);
2397                                 procmsg_msginfo_free(newmsginfo);
2398                         } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
2399                                 /* TODO: set default flags */
2400                                 procmsg_msginfo_free(newmsginfo);
2401                         }
2402                 }
2403         }
2404
2405         g_relation_destroy(relation);
2406
2407         return lastnum;
2408 }
2409
2410 /*
2411 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
2412 {
2413         Folder *folder;
2414         gint num;
2415
2416         g_return_val_if_fail(dest != NULL, -1);
2417         g_return_val_if_fail(msginfo != NULL, -1);
2418
2419         folder = dest->folder;
2420         if (dest->last_num < 0) folder->scan(folder, dest);
2421
2422         num = folder->move_msg(folder, dest, msginfo);
2423         if (num > 0) dest->last_num = num;
2424
2425         return num;
2426 }
2427 */
2428                 
2429 FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest) 
2430 {
2431         GSList *mlist;
2432         FolderItem *new_item;
2433         FolderItem *next_item;
2434         GNode *srcnode;
2435         gchar *old_id, *new_id;
2436
2437         mlist = folder_item_get_msg_list(src);
2438
2439         /* move messages */
2440         debug_print("Moving %s to %s\n", src->path, dest->path);
2441         new_item = folder_create_folder(dest, src->name);
2442         if (new_item == NULL) {
2443                 printf("Can't create folder\n");
2444                 return NULL;
2445         }
2446         
2447         if (new_item->folder == NULL)
2448                 new_item->folder = dest->folder;
2449
2450         /* move messages */
2451         log_message(_("Moving %s to %s...\n"), 
2452                         src->name, new_item->path);
2453         if (mlist != NULL)
2454                 folder_item_move_msgs(new_item, mlist);
2455         
2456         /*copy prefs*/
2457         folder_item_prefs_copy_prefs(src, new_item);
2458         new_item->collapsed = src->collapsed;
2459         new_item->thread_collapsed = src->thread_collapsed;
2460         new_item->threaded  = src->threaded;
2461         new_item->ret_rcpt  = src->ret_rcpt;
2462         new_item->hide_read_msgs = src->hide_read_msgs;
2463         new_item->sort_key  = src->sort_key;
2464         new_item->sort_type = src->sort_type;
2465
2466         prefs_matcher_write_config();
2467         
2468         /* recurse */
2469         srcnode = src->folder->node;    
2470         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
2471         srcnode = srcnode->children;
2472         while (srcnode != NULL) {
2473                 if (srcnode && srcnode->data) {
2474                         next_item = (FolderItem*) srcnode->data;
2475                         srcnode = srcnode->next;
2476                         if (folder_item_move_recursive(next_item, new_item) == NULL)
2477                                 return NULL;
2478                 }
2479         }
2480         old_id = folder_item_get_identifier(src);
2481         new_id = folder_item_get_identifier(new_item);
2482         debug_print("updating rules : %s => %s\n", old_id, new_id);
2483
2484         /* if src supports removing, otherwise only copy folder */
2485         if (src->folder->klass->remove_folder != NULL)  
2486                 src->folder->klass->remove_folder(src->folder, src);
2487         folder_write_list();
2488
2489         if (old_id != NULL && new_id != NULL)
2490                 prefs_filtering_rename_path(old_id, new_id);
2491         g_free(old_id);
2492         g_free(new_id);
2493
2494         return new_item;
2495 }
2496
2497 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item)
2498 {
2499         FolderItem *tmp = folder_item_parent(dest);
2500         gchar * src_identifier, * dst_identifier;
2501         gchar * phys_srcpath, * phys_dstpath;
2502         
2503         while (tmp) {
2504                 if (tmp == src) {
2505                         return F_MOVE_FAILED_DEST_IS_CHILD;
2506                 }
2507                 tmp = folder_item_parent(tmp);;
2508         }
2509         
2510         tmp = folder_item_parent(src);
2511         
2512         src_identifier = folder_item_get_identifier(src);
2513         dst_identifier = folder_item_get_identifier(dest);
2514         
2515         if(dst_identifier == NULL && dest->folder && folder_item_parent(dest) == NULL) {
2516                 /* dest can be a root folder */
2517                 dst_identifier = folder_get_identifier(dest->folder);
2518         }
2519         if (src_identifier == NULL || dst_identifier == NULL) {
2520                 debug_print("Can't get identifiers\n");
2521                 return F_MOVE_FAILED;
2522         }
2523
2524         if (src->folder != dest->folder) {
2525                 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
2526         }
2527
2528         phys_srcpath = folder_item_get_path(src);
2529         phys_dstpath = g_strconcat(folder_item_get_path(dest),
2530                        G_DIR_SEPARATOR_S,
2531                        g_path_get_basename(phys_srcpath),
2532                        NULL);
2533
2534         if (folder_item_parent(src) == dest || src == dest) {
2535                 g_free(src_identifier);
2536                 g_free(dst_identifier);
2537                 g_free(phys_srcpath);
2538                 g_free(phys_dstpath);
2539                 return F_MOVE_FAILED_DEST_IS_PARENT;
2540         }
2541         debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
2542         if ((tmp = folder_item_move_recursive(src, dest)) == NULL) {
2543                 return F_MOVE_FAILED;
2544         }
2545         
2546         g_free(src_identifier);
2547         g_free(dst_identifier);
2548         g_free(phys_srcpath);
2549         g_free(phys_dstpath);
2550
2551         *new_item = tmp;
2552
2553         return F_MOVE_OK;
2554 }
2555
2556 /**
2557  * Copy a list of message to a new folder and remove
2558  * source messages if wanted
2559  */
2560 static gint do_copy_msgs(FolderItem *dest, GSList *msglist, gboolean remove_source)
2561 {
2562         Folder *folder;
2563         GSList *l;
2564         gint num, lastnum = -1;
2565         gboolean folderscan = FALSE;
2566         GRelation *relation;
2567
2568         g_return_val_if_fail(dest != NULL, -1);
2569         g_return_val_if_fail(msglist != NULL, -1);
2570
2571         folder = dest->folder;
2572
2573         g_return_val_if_fail(folder->klass->copy_msg != NULL, -1);
2574
2575         relation = g_relation_new(2);
2576         g_relation_index(relation, 0, g_direct_hash, g_direct_equal);
2577         g_relation_index(relation, 1, g_direct_hash, g_direct_equal);
2578
2579         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
2580                 MsgInfo * msginfo = (MsgInfo *) l->data;
2581
2582                 if (msginfo->planned_download != 0) {
2583                         int old_planned = msginfo->planned_download;
2584                         partial_unmark(msginfo);
2585                         /* little hack to reenable after */
2586                         msginfo->planned_download = old_planned;
2587                 }
2588         }
2589
2590         /* 
2591          * Copy messages to destination folder and 
2592          * store new message numbers in newmsgnums
2593          */
2594         if (folder->klass->copy_msgs != NULL) {
2595                 if (folder->klass->copy_msgs(folder, dest, msglist, relation) < 0) {
2596                         g_relation_destroy(relation);
2597                         return -1;
2598                 }
2599         } else {
2600                 for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
2601                         MsgInfo * msginfo = (MsgInfo *) l->data;
2602
2603                         num = folder->klass->copy_msg(folder, dest, msginfo);
2604                         g_relation_insert(relation, msginfo, GINT_TO_POINTER(num));
2605                 }
2606         }
2607
2608         /* Read cache for dest folder */
2609         if (!dest->cache) folder_item_read_cache(dest);
2610
2611         /* 
2612          * Fetch new MsgInfos for new messages in dest folder,
2613          * add them to the msgcache and update folder message counts
2614          */
2615         if (g_relation_count(relation, GINT_TO_POINTER(0), 1) > 0) {
2616                 folder_item_scan_full(dest, FALSE);
2617                 folderscan = TRUE;
2618         }
2619
2620         for (l = msglist; l != NULL; l = g_slist_next(l)) {
2621                 MsgInfo *msginfo = (MsgInfo *) l->data;
2622                 GTuples *tuples;
2623
2624                 tuples = g_relation_select(relation, msginfo, 0);
2625                 num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
2626                 g_tuples_destroy(tuples);
2627
2628                 if (num >= 0) {
2629                         MsgInfo *newmsginfo = NULL;
2630
2631                         if (folderscan) {
2632                                 if (msginfo->msgid != NULL) {
2633                                         newmsginfo = folder_item_get_msginfo_by_msgid(dest, msginfo->msgid);
2634                                         if (newmsginfo != NULL) {
2635                                                 copy_msginfo_flags(msginfo, newmsginfo);
2636                                                 num = newmsginfo->msgnum;
2637                                         }
2638                                 }
2639                         } else {
2640                                 newmsginfo = get_msginfo(dest, num);
2641                                 if (newmsginfo != NULL) {
2642                                         add_msginfo_to_cache(dest, newmsginfo, msginfo);
2643                                 }
2644                         }
2645
2646                         if (msginfo->planned_download 
2647                             == POP3_PARTIAL_DLOAD_DELE) {
2648                                 partial_mark_for_delete(newmsginfo);
2649                         }
2650                         if (msginfo->planned_download 
2651                             == POP3_PARTIAL_DLOAD_DLOAD) {
2652                                 partial_mark_for_download(newmsginfo);
2653                         }
2654                         procmsg_msginfo_free(newmsginfo);
2655
2656
2657                         if (num > lastnum)
2658                                 lastnum = num;
2659                 }
2660         }
2661
2662         if (remove_source) {
2663                 /*
2664                  * Remove source messages from their folders if
2665                  * copying was successfull and update folder
2666                  * message counts
2667                  */
2668                 for (l = msglist; l != NULL; l = g_slist_next(l)) {
2669                         MsgInfo *msginfo = (MsgInfo *) l->data;
2670                         FolderItem *item = msginfo->folder;
2671                         GTuples *tuples;
2672
2673                         tuples = g_relation_select(relation, msginfo, 0);
2674                         num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
2675                         g_tuples_destroy(tuples);
2676
2677                         if ((num >= 0) && (item->folder->klass->remove_msg != NULL)) {
2678                                 item->folder->klass->remove_msg(item->folder,
2679                                                                 msginfo->folder,
2680                                                                 msginfo->msgnum);
2681                                 remove_msginfo_from_cache(item, msginfo);
2682                         }
2683                 }
2684         }
2685
2686         g_relation_destroy(relation);
2687         return lastnum;
2688 }
2689
2690 /**
2691  * Move a message to a new folder.
2692  *
2693  * \param dest Destination folder
2694  * \param msginfo The message
2695  */
2696 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
2697 {
2698         GSList list;
2699
2700         g_return_val_if_fail(dest != NULL, -1);
2701         g_return_val_if_fail(msginfo != NULL, -1);
2702
2703         list.data = msginfo;
2704         list.next = NULL;
2705
2706         return do_copy_msgs(dest, &list, TRUE);
2707 }
2708
2709 /**
2710  * Move a list of messages to a new folder.
2711  *
2712  * \param dest Destination folder
2713  * \param msglist List of messages
2714  */
2715 gint folder_item_move_msgs(FolderItem *dest, GSList *msglist)
2716 {
2717         g_return_val_if_fail(dest != NULL, -1);
2718         g_return_val_if_fail(msglist != NULL, -1);
2719
2720         return do_copy_msgs(dest, msglist, TRUE);
2721 }
2722
2723 /**
2724  * Copy a message to a new folder.
2725  *
2726  * \param dest Destination folder
2727  * \param msginfo The message
2728  */
2729 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
2730 {
2731         GSList list;
2732
2733         g_return_val_if_fail(dest != NULL, -1);
2734         g_return_val_if_fail(msginfo != NULL, -1);
2735     
2736         list.data = msginfo;
2737         list.next = NULL;
2738         
2739         return do_copy_msgs(dest, &list, FALSE);
2740 }
2741
2742 /**
2743  * Copy a list of messages to a new folder.
2744  *
2745  * \param dest Destination folder
2746  * \param msglist List of messages
2747  */
2748 gint folder_item_copy_msgs(FolderItem *dest, GSList *msglist)
2749 {
2750         g_return_val_if_fail(dest != NULL, -1);
2751         g_return_val_if_fail(msglist != NULL, -1);
2752
2753         return do_copy_msgs(dest, msglist, FALSE);
2754 }
2755
2756 gint folder_item_remove_msg(FolderItem *item, gint num)
2757 {
2758         Folder *folder;
2759         gint ret;
2760         MsgInfo *msginfo;
2761
2762         g_return_val_if_fail(item != NULL, -1);
2763         folder = item->folder;
2764         g_return_val_if_fail(folder->klass->remove_msg != NULL, -1);
2765
2766         if (!item->cache) folder_item_read_cache(item);
2767
2768         ret = folder->klass->remove_msg(folder, item, num);
2769
2770         msginfo = msgcache_get_msg(item->cache, num);
2771         if (msginfo != NULL) {
2772                 remove_msginfo_from_cache(item, msginfo);
2773                 procmsg_msginfo_free(msginfo);
2774         }
2775
2776         return ret;
2777 }
2778
2779 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
2780 {
2781         Folder *folder;
2782         gint ret = 0;
2783
2784         g_return_val_if_fail(item != NULL, -1);
2785         folder = item->folder;
2786         g_return_val_if_fail(folder != NULL, -1);
2787
2788         if (!item->cache) folder_item_read_cache(item);
2789
2790         folder_item_update_freeze();
2791         while (msglist != NULL) {
2792                 MsgInfo *msginfo = (MsgInfo *)msglist->data;
2793
2794                 ret = folder_item_remove_msg(item, msginfo->msgnum);
2795                 if (ret != 0) break;
2796                 msgcache_remove_msg(item->cache, msginfo->msgnum);
2797                 msglist = msglist->next;
2798         }
2799         folder_item_update_thaw();
2800
2801         return ret;
2802 }
2803
2804 gint folder_item_remove_all_msg(FolderItem *item)
2805 {
2806         Folder *folder;
2807         gint result;
2808
2809         g_return_val_if_fail(item != NULL, -1);
2810
2811         folder = item->folder;
2812
2813         g_return_val_if_fail(folder->klass->remove_all_msg != NULL, -1);
2814
2815         result = folder->klass->remove_all_msg(folder, item);
2816
2817         if (result == 0) {
2818                 folder_item_free_cache(item);
2819                 item->cache = msgcache_new();
2820
2821                 item->new_msgs = 0;
2822                 item->unread_msgs = 0;
2823                 item->unreadmarked_msgs = 0;
2824                 item->total_msgs = 0;
2825                 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2826         }
2827
2828         return result;
2829 }
2830
2831 void folder_item_change_msg_flags(FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
2832 {
2833         g_return_if_fail(item != NULL);
2834         g_return_if_fail(msginfo != NULL);
2835         
2836         if (item->folder->klass->change_flags != NULL) {
2837                 item->folder->klass->change_flags(item->folder, item, msginfo, newflags);
2838         } else {
2839                 msginfo->flags.perm_flags = newflags;
2840         }
2841 }
2842
2843 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
2844 {
2845         Folder *folder;
2846
2847         g_return_val_if_fail(item != NULL, FALSE);
2848
2849         folder = item->folder;
2850
2851         g_return_val_if_fail(folder->klass->is_msg_changed != NULL, -1);
2852
2853         return folder->klass->is_msg_changed(folder, item, msginfo);
2854 }
2855
2856 gchar *folder_item_get_cache_file(FolderItem *item)
2857 {
2858         gchar *path;
2859         gchar *file;
2860
2861         g_return_val_if_fail(item != NULL, NULL);
2862         g_return_val_if_fail(item->path != NULL, NULL);
2863
2864         path = folder_item_get_path(item);
2865         g_return_val_if_fail(path != NULL, NULL);
2866         if (!is_dir_exist(path))
2867                 make_dir_hier(path);
2868         file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
2869         g_free(path);
2870
2871         return file;
2872 }
2873
2874 gchar *folder_item_get_mark_file(FolderItem *item)
2875 {
2876         gchar *path;
2877         gchar *file;
2878
2879         g_return_val_if_fail(item != NULL, NULL);
2880         g_return_val_if_fail(item->path != NULL, NULL);
2881
2882         path = folder_item_get_path(item);
2883         g_return_val_if_fail(path != NULL, NULL);
2884         if (!is_dir_exist(path))
2885                 make_dir_hier(path);
2886         file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
2887         g_free(path);
2888
2889         return file;
2890 }
2891
2892 static gpointer xml_to_folder_item(gpointer nodedata, gpointer data)
2893 {
2894         XMLNode *xmlnode = (XMLNode *) nodedata;
2895         Folder *folder = (Folder *) data;
2896         FolderItem *item;
2897
2898         g_return_val_if_fail(xmlnode != NULL, NULL);
2899         g_return_val_if_fail(folder != NULL, NULL);
2900
2901         if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
2902                 g_warning("tag name != \"folderitem\"\n");
2903                 return NULL;
2904         }
2905
2906         item = folder_item_new(folder, "", "");
2907         if (folder->klass->item_set_xml != NULL)
2908                 folder->klass->item_set_xml(folder, item, xmlnode->tag);
2909         else
2910                 folder_item_set_xml(folder, item, xmlnode->tag);
2911
2912         item->folder = folder;
2913
2914         switch (item->stype) {
2915         case F_INBOX:  folder->inbox  = item; break;
2916         case F_OUTBOX: folder->outbox = item; break;
2917         case F_DRAFT:  folder->draft  = item; break;
2918         case F_QUEUE:  folder->queue  = item; break;
2919         case F_TRASH:  folder->trash  = item; break;
2920         default:       break;
2921         }
2922         folder_item_prefs_read_config(item);
2923
2924         return item;
2925 }
2926
2927 static gboolean folder_item_set_node(GNode *node, gpointer data)
2928 {
2929         FolderItem *item = (FolderItem *) node->data;
2930         item->node = node;
2931
2932         return FALSE;
2933 }
2934
2935 static Folder *folder_get_from_xml(GNode *node)
2936 {
2937         Folder *folder;
2938         XMLNode *xmlnode;
2939         GList *list;
2940         FolderClass *klass = NULL;
2941         GNode *cur;
2942
2943         g_return_val_if_fail(node->data != NULL, NULL);
2944
2945         xmlnode = node->data;
2946         if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
2947                 g_warning("tag name != \"folder\"\n");
2948                 return NULL;
2949         }
2950         list = xmlnode->tag->attr;
2951         for (; list != NULL; list = list->next) {
2952                 XMLAttr *attr = list->data;
2953
2954                 if (!attr || !attr->name || !attr->value) continue;
2955                 if (!strcmp(attr->name, "type"))
2956                         klass = folder_get_class_from_string(attr->value);
2957         }
2958         if (klass == NULL)
2959                 return NULL;
2960
2961         folder = folder_new(klass, "", "");
2962         g_return_val_if_fail(folder != NULL, NULL);
2963
2964         if (klass->set_xml)
2965                 klass->set_xml(folder, xmlnode->tag);
2966         else
2967                 folder_set_xml(folder, xmlnode->tag);
2968
2969         cur = node->children;
2970         while (cur != NULL) {
2971                 GNode *itemnode;
2972
2973                 itemnode = g_node_map(cur, xml_to_folder_item, (gpointer) folder);
2974                 g_node_append(folder->node, itemnode);
2975                 cur = cur->next;
2976         }
2977         g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, folder_item_set_node, NULL);
2978         
2979         return folder;
2980 }
2981
2982 static gchar *folder_get_list_path(void)
2983 {
2984         static gchar *filename = NULL;
2985
2986         if (!filename)
2987                 filename =  g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2988                                         FOLDER_LIST, NULL);
2989
2990         return filename;
2991 }
2992
2993 #define PUT_ESCAPE_STR(fp, attr, str)                   \
2994 {                                                       \
2995         fputs(" " attr "=\"", fp);                      \
2996         xml_file_put_escape_str(fp, str);               \
2997         fputs("\"", fp);                                \
2998 }
2999
3000 static gpointer folder_item_to_xml(gpointer nodedata, gpointer data)
3001 {
3002         FolderItem *item = (FolderItem *) nodedata;
3003         XMLTag *tag;
3004
3005         g_return_val_if_fail(item != NULL, NULL);
3006
3007         if (item->folder->klass->item_get_xml != NULL)
3008                 tag = item->folder->klass->item_get_xml(item->folder, item);
3009         else
3010                 tag = folder_item_get_xml(item->folder, item);
3011
3012         return xml_node_new(tag, NULL);;
3013 }
3014
3015 static GNode *folder_get_xml_node(Folder *folder)
3016 {
3017         GNode *node;
3018         XMLNode *xmlnode;
3019         XMLTag *tag;
3020
3021         g_return_val_if_fail(folder != NULL, NULL);
3022
3023         if (folder->klass->get_xml != NULL)
3024                 tag = folder->klass->get_xml(folder);
3025         else
3026                 tag = folder_get_xml(folder);
3027
3028         xml_tag_add_attr(tag, xml_attr_new("type", folder->klass->idstr));
3029
3030         xmlnode = xml_node_new(tag, NULL);
3031
3032         node = g_node_new(xmlnode);
3033         if (folder->node->children) {
3034                 GNode *cur;
3035
3036                 cur = folder->node->children;
3037                 while (cur) {
3038                         GNode *xmlnode;
3039
3040                         xmlnode = g_node_map(cur, folder_item_to_xml, (gpointer) folder);
3041                         g_node_append(node, xmlnode);
3042                         cur = cur->next;
3043                 }
3044         }
3045
3046         return node;
3047 }
3048
3049 static void folder_update_op_count_rec(GNode *node)
3050 {
3051         FolderItem *fitem = FOLDER_ITEM(node->data);
3052
3053         if (g_node_depth(node) > 0) {
3054                 if (fitem->op_count > 0) {
3055                         fitem->op_count = 0;
3056                         folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
3057                 }
3058                 if (node->children) {
3059                         GNode *child;
3060
3061                         child = node->children;
3062                         while (child) {
3063                                 GNode *cur;
3064
3065                                 cur = child;
3066                                 child = cur->next;
3067                                 folder_update_op_count_rec(cur);
3068                         }
3069                 }
3070         }
3071 }
3072
3073 void folder_update_op_count(void) 
3074 {
3075         GList *cur;
3076         Folder *folder;
3077
3078         for (cur = folder_list; cur != NULL; cur = cur->next) {
3079                 folder = cur->data;
3080                 folder_update_op_count_rec(folder->node);
3081         }
3082 }
3083
3084 typedef struct _type_str {
3085         gchar * str;
3086         gint type;
3087 } type_str;
3088
3089
3090 /*
3091 static gchar * folder_item_get_tree_identifier(FolderItem * item)
3092 {
3093         if (item->parent != NULL) {
3094                 gchar * path;
3095                 gchar * id;
3096
3097                 path = folder_item_get_tree_identifier(item->parent);
3098                 if (path == NULL)
3099                         return NULL;
3100
3101                 id = g_strconcat(path, "/", item->name, NULL);
3102                 g_free(path);
3103
3104                 return id;
3105         }
3106         else {
3107                 return g_strconcat("/", item->name, NULL);
3108         }
3109 }
3110 */
3111
3112 /* CLAWS: temporary local folder for filtering */
3113 #define TEMP_FOLDER "TEMP_FOLDER"
3114 #define PROCESSING_FOLDER_ITEM "processing"     
3115
3116 static FolderItem *processing_folder_item;
3117
3118 static void folder_create_processing_folder(void)
3119 {
3120         Folder *processing_folder;
3121         gchar      *tmpname;
3122
3123         if ((processing_folder = folder_find_from_name(TEMP_FOLDER, mh_get_class())) == NULL) {
3124                 gchar *tmppath;
3125
3126                 tmppath =
3127                     g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
3128                                 "tempfolder", NULL);
3129                 processing_folder =
3130                     folder_new(mh_get_class(), TEMP_FOLDER, tmppath);
3131                 g_free(tmppath);
3132         }
3133         g_assert(processing_folder != NULL);
3134
3135         debug_print("tmpparentroot %s\n", LOCAL_FOLDER(processing_folder)->rootpath);
3136         if (LOCAL_FOLDER(processing_folder)->rootpath[0] == '/')
3137                 tmpname = g_strconcat(LOCAL_FOLDER(processing_folder)->rootpath,
3138                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
3139                                       NULL);
3140         else
3141                 tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
3142                                       LOCAL_FOLDER(processing_folder)->rootpath,
3143                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
3144                                       NULL);
3145
3146         if (!is_dir_exist(tmpname)) {
3147                 debug_print("*TMP* creating %s\n", tmpname);
3148                 processing_folder_item = processing_folder->klass->create_folder(processing_folder,
3149                                                                                  processing_folder->node->data,
3150                                                                                  PROCESSING_FOLDER_ITEM);
3151         } else {
3152                 debug_print("*TMP* already created\n");
3153                 processing_folder_item = folder_item_new(processing_folder, PROCESSING_FOLDER_ITEM, PROCESSING_FOLDER_ITEM);
3154                 g_assert(processing_folder_item);
3155                 folder_item_append(processing_folder->node->data, processing_folder_item);
3156         }
3157         g_assert(processing_folder_item != NULL);
3158         g_free(tmpname);
3159 }
3160
3161 FolderItem *folder_get_default_processing(void)
3162 {
3163         if (!processing_folder_item) {
3164                 folder_create_processing_folder();
3165         }
3166         return processing_folder_item;
3167 }
3168
3169 /* folder_persist_prefs_new() - return hash table with persistent
3170  * settings (and folder name as key). 
3171  * (note that in claws other options are in the folder_item_prefs_RC
3172  * file, so those don't need to be included in PersistPref yet) 
3173  */
3174 GHashTable *folder_persist_prefs_new(Folder *folder)
3175 {
3176         GHashTable *pptable;
3177
3178         g_return_val_if_fail(folder, NULL);
3179         pptable = g_hash_table_new(g_str_hash, g_str_equal);
3180         folder_get_persist_prefs_recursive(folder->node, pptable);
3181         return pptable;
3182 }
3183
3184 void folder_persist_prefs_free(GHashTable *pptable)
3185 {
3186         g_return_if_fail(pptable);
3187         g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
3188         g_hash_table_destroy(pptable);
3189 }
3190
3191 const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
3192 {
3193         if (pptable == NULL || name == NULL) return NULL;
3194         return g_hash_table_lookup(pptable, name);
3195 }
3196
3197 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
3198 {
3199         const PersistPrefs *pp;
3200         gchar *id = folder_item_get_identifier(item);
3201
3202         pp = folder_get_persist_prefs(pptable, id); 
3203         g_free(id);
3204
3205         if (!pp) return;
3206
3207         /* CLAWS: since not all folder properties have been migrated to 
3208          * folderlist.xml, we need to call the old stuff first before
3209          * setting things that apply both to Main and Claws. */
3210         folder_item_prefs_read_config(item); 
3211
3212         item->collapsed = pp->collapsed;
3213         item->thread_collapsed = pp->thread_collapsed;
3214         item->threaded  = pp->threaded;
3215         item->ret_rcpt  = pp->ret_rcpt;
3216         item->hide_read_msgs = pp->hide_read_msgs;
3217         item->sort_key  = pp->sort_key;
3218         item->sort_type = pp->sort_type;
3219 }
3220
3221 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
3222 {
3223         FolderItem *item = FOLDER_ITEM(node->data);
3224         PersistPrefs *pp;
3225         GNode *child, *cur;
3226         gchar *id;
3227
3228         g_return_if_fail(node != NULL);
3229         g_return_if_fail(item != NULL);
3230
3231         /* NOTE: item->path == NULL means top level folder; not interesting
3232          * to store preferences of that one.  */
3233         if (item->path) {
3234                 id = folder_item_get_identifier(item);
3235                 pp = g_new0(PersistPrefs, 1);
3236                 g_return_if_fail(pp != NULL);
3237                 pp->collapsed = item->collapsed;
3238                 pp->thread_collapsed = item->thread_collapsed;
3239                 pp->threaded  = item->threaded;
3240                 pp->ret_rcpt  = item->ret_rcpt; 
3241                 pp->hide_read_msgs = item->hide_read_msgs;
3242                 pp->sort_key  = item->sort_key;
3243                 pp->sort_type = item->sort_type;
3244                 g_hash_table_insert(pptable, id, pp);
3245         }
3246
3247         if (node->children) {
3248                 child = node->children;
3249                 while (child) {
3250                         cur = child;
3251                         child = cur->next;
3252                         folder_get_persist_prefs_recursive(cur, pptable);
3253                 }
3254         }       
3255 }
3256
3257 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
3258 {
3259         if (key) 
3260                 g_free(key);
3261         if (val) 
3262                 g_free(val);
3263         return TRUE;    
3264 }
3265
3266 void folder_item_apply_processing(FolderItem *item)
3267 {
3268         GSList *processing_list;
3269         GSList *mlist, *cur;
3270         
3271         g_return_if_fail(item != NULL);
3272         
3273         processing_list = item->prefs->processing;
3274
3275         folder_item_update_freeze();
3276
3277         mlist = folder_item_get_msg_list(item);
3278         for (cur = mlist ; cur != NULL ; cur = cur->next) {
3279                 MsgInfo * msginfo;
3280
3281                 msginfo = (MsgInfo *) cur->data;
3282                 
3283                 /* reset parameters that can be modified by processing */
3284                 msginfo->hidden = 0;
3285                 msginfo->score = 0;
3286                 
3287                 /* apply pre global rules */
3288                 filter_message_by_msginfo(pre_global_processing, msginfo);
3289                 
3290                 /* apply rules of the folder */
3291                 filter_message_by_msginfo(processing_list, msginfo);
3292
3293                 /* apply post global rules */
3294                 filter_message_by_msginfo(post_global_processing, msginfo);
3295                 
3296                 procmsg_msginfo_free(msginfo);
3297         }
3298         g_slist_free(mlist);
3299
3300         folder_item_update_thaw();
3301 }
3302
3303 /*
3304  *  functions for handling FolderItem content changes
3305  */
3306 static gint folder_item_update_freeze_cnt = 0;
3307
3308 static void folder_item_update_with_msg(FolderItem *item, FolderItemUpdateFlags update_flags, MsgInfo *msg)
3309 {
3310         if (folder_item_update_freeze_cnt == 0 /* || (msg != NULL && item->opened) */) {
3311                 FolderItemUpdateData source;
3312         
3313                 source.item = item;
3314                 source.update_flags = update_flags;
3315                 source.msg = msg;
3316                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
3317         } else {
3318                 item->update_flags |= update_flags & ~(F_ITEM_UPDATE_ADDMSG | F_ITEM_UPDATE_REMOVEMSG);
3319         }
3320 }
3321
3322 /**
3323  * Notify the folder system about changes to a folder. If the
3324  * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
3325  * be invoked, otherwise the changes will be remebered until
3326  * the folder system is thawed.
3327  *
3328  * \param item The FolderItem that was changed
3329  * \param update_flags Type of changed that was made
3330  */
3331 void folder_item_update(FolderItem *item, FolderItemUpdateFlags update_flags)
3332 {
3333         folder_item_update_with_msg(item, update_flags, NULL);
3334 }
3335
3336 void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
3337 {
3338         GNode *node = item->folder->node;       
3339
3340         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
3341         node = node->children;
3342
3343         folder_item_update(item, update_flags);
3344         while (node != NULL) {
3345                 if (node && node->data) {
3346                         FolderItem *next_item = (FolderItem*) node->data;
3347
3348                         folder_item_update(next_item, update_flags);
3349                 }
3350                 node = node->next;
3351         }
3352 }
3353
3354 void folder_item_update_freeze(void)
3355 {
3356         folder_item_update_freeze_cnt++;
3357 }
3358
3359 static void folder_item_update_func(FolderItem *item, gpointer data)
3360 {
3361         FolderItemUpdateData source;
3362     
3363         if (item->update_flags) {
3364                 source.item = item;
3365                 source.update_flags = item->update_flags;
3366                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);                             
3367                 item->update_flags = 0;
3368         }
3369 }
3370
3371 void folder_item_update_thaw(void)
3372 {
3373         if (folder_item_update_freeze_cnt > 0)
3374                 folder_item_update_freeze_cnt--;
3375         if (folder_item_update_freeze_cnt == 0) {
3376                 /* Update all folders */
3377                 folder_func_to_all_folders(folder_item_update_func, NULL);
3378         }
3379 }
3380
3381 #undef PUT_ESCAPE_STR