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