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