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