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