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