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