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