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