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