re-implement per-folder message threading
[claws.git] / src / folder.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <stdio.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33
34 #include "intl.h"
35 #include "folder.h"
36 #include "session.h"
37 #include "imap.h"
38 #include "news.h"
39 #include "mh.h"
40 #include "mbox_folder.h"
41 #include "utils.h"
42 #include "xml.h"
43 #include "codeconv.h"
44 #include "prefs_gtk.h"
45 #include "account.h"
46 #include "filtering.h"
47 #include "scoring.h"
48 #include "prefs_folder_item.h"
49 #include "procheader.h"
50 #include "hooks.h"
51 #include "log.h"
52
53 /* Dependecies to be removed ?! */
54 #include "prefs_common.h"
55 #include "prefs_account.h"
56 #include "prefs_folder_item.h"
57
58 static GList *folder_list = NULL;
59
60 static void folder_init         (Folder         *folder,
61                                  const gchar    *name);
62
63 static gboolean folder_read_folder_func (GNode          *node,
64                                          gpointer        data);
65 static gchar *folder_get_list_path      (void);
66 static void folder_write_list_recursive (GNode          *node,
67                                          gpointer        data);
68 static void folder_update_op_count_rec  (GNode          *node);
69
70
71 static void folder_get_persist_prefs_recursive
72                                         (GNode *node, GHashTable *pptable);
73 static gboolean persist_prefs_free      (gpointer key, gpointer val, gpointer data);
74 void folder_item_read_cache             (FolderItem *item);
75 void folder_item_free_cache             (FolderItem *item);
76
77 Folder *folder_new(FolderType type, const gchar *name, const gchar *path)
78 {
79         Folder *folder = NULL;
80         FolderItem *item;
81
82         name = name ? name : path;
83         switch (type) {
84         case F_MBOX:
85                 folder = mbox_folder_new(name, path);
86                 break;
87         case F_MH:
88                 folder = mh_folder_new(name, path);
89                 break;
90         case F_IMAP:
91                 folder = imap_folder_new(name, path);
92                 break;
93         case F_NEWS:
94                 folder = news_folder_new(name, path);
95                 break;
96         default:
97                 return NULL;
98         }
99
100         /* Create root folder item */
101         item = folder_item_new(folder, name, NULL);
102         item->folder = folder;
103         folder->node = g_node_new(item);
104         folder->data = NULL;
105
106         return folder;
107 }
108
109 static void folder_init(Folder *folder, const gchar *name)
110 {
111         g_return_if_fail(folder != NULL);
112
113         folder_set_name(folder, name);
114
115         /* Init folder data */
116         folder->account = NULL;
117         folder->inbox = NULL;
118         folder->outbox = NULL;
119         folder->draft = NULL;
120         folder->queue = NULL;
121         folder->trash = NULL;
122
123         /* Init Folder functions */
124         folder->item_new = NULL;
125         folder->item_destroy = NULL;
126         folder->fetch_msg = NULL;
127         folder->get_msginfo = NULL;
128         folder->get_msginfos = NULL;
129         folder->get_num_list = NULL;
130         folder->ui_func = NULL;
131         folder->ui_func_data = NULL;
132         folder->change_flags = NULL;
133         folder->check_msgnum_validity = NULL;
134 }
135
136 void folder_local_folder_init(Folder *folder, const gchar *name,
137                               const gchar *path)
138 {
139         folder_init(folder, name);
140         LOCAL_FOLDER(folder)->rootpath = g_strdup(path);
141 }
142
143 void folder_remote_folder_init(Folder *folder, const gchar *name,
144                                const gchar *path)
145 {
146         folder_init(folder, name);
147         REMOTE_FOLDER(folder)->session = NULL;
148 }
149
150 void folder_destroy(Folder *folder)
151 {
152         g_return_if_fail(folder != NULL);
153         g_return_if_fail(folder->destroy != NULL);
154
155         folder->destroy(folder);
156
157         folder_list = g_list_remove(folder_list, folder);
158
159         folder_tree_destroy(folder);
160         g_free(folder->name);
161         g_free(folder);
162 }
163
164 void folder_local_folder_destroy(LocalFolder *lfolder)
165 {
166         g_return_if_fail(lfolder != NULL);
167
168         g_free(lfolder->rootpath);
169 }
170
171 void folder_remote_folder_destroy(RemoteFolder *rfolder)
172 {
173         g_return_if_fail(rfolder != NULL);
174
175         if (rfolder->session)
176                 session_destroy(rfolder->session);
177 }
178
179 #if 0
180 Folder *mbox_folder_new(const gchar *name, const gchar *path)
181 {
182         /* not yet implemented */
183         return NULL;
184 }
185
186 Folder *maildir_folder_new(const gchar *name, const gchar *path)
187 {
188         /* not yet implemented */
189         return NULL;
190 }
191 #endif
192
193 FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path)
194 {
195         FolderItem *item = NULL;
196
197         if (folder->item_new) {
198                 item = folder->item_new(folder);
199         } else {
200                 item = g_new0(FolderItem, 1);
201         }
202
203         g_return_val_if_fail(item != NULL, NULL);
204
205         item->stype = F_NORMAL;
206         item->name = g_strdup(name);
207         item->path = g_strdup(path);
208         item->mtime = 0;
209         item->new = 0;
210         item->unread = 0;
211         item->unreadmarked = 0;
212         item->total = 0;
213         item->last_num = -1;
214         item->cache = NULL;
215         item->no_sub = FALSE;
216         item->no_select = FALSE;
217         item->collapsed = FALSE;
218         item->thread_collapsed = FALSE;
219         item->threaded  = TRUE;
220         item->ret_rcpt  = FALSE;
221         item->opened    = FALSE;
222         item->parent = NULL;
223         item->folder = NULL;
224         item->account = NULL;
225         item->apply_sub = FALSE;
226         item->mark_queue = NULL;
227         item->data = NULL;
228
229         item->prefs = prefs_folder_item_new();
230
231         return item;
232 }
233
234 void folder_item_append(FolderItem *parent, FolderItem *item)
235 {
236         GNode *node;
237
238         g_return_if_fail(parent != NULL);
239         g_return_if_fail(parent->folder != NULL);
240         g_return_if_fail(item != NULL);
241
242         node = parent->folder->node;
243         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, parent);
244         g_return_if_fail(node != NULL);
245
246         item->parent = parent;
247         item->folder = parent->folder;
248         g_node_append_data(node, item);
249 }
250
251 void folder_item_remove(FolderItem *item)
252 {
253         GNode *node;
254
255         g_return_if_fail(item != NULL);
256         g_return_if_fail(item->folder != NULL);
257
258         node = item->folder->node;
259         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
260         g_return_if_fail(node != NULL);
261
262         /* TODO: free all FolderItem's first */
263         if (item->folder->node == node)
264                 item->folder->node = NULL;
265         g_node_destroy(node);
266 }
267
268 void folder_item_destroy(FolderItem *item)
269 {
270         g_return_if_fail(item != NULL);
271
272         debug_print("Destroying folder item %s\n", item->path);
273
274         if (item->cache)
275                 folder_item_free_cache(item);
276         g_free(item->name);
277         g_free(item->path);
278
279         if (item->folder != NULL) {
280                 if(item->folder->item_destroy) {
281                         item->folder->item_destroy(item->folder, item);
282                 } else {
283                         g_free(item);
284                 }
285         }
286 }
287
288 void folder_set_ui_func(Folder *folder, FolderUIFunc func, gpointer data)
289 {
290         g_return_if_fail(folder != NULL);
291
292         folder->ui_func = func;
293         folder->ui_func_data = data;
294 }
295
296 void folder_set_name(Folder *folder, const gchar *name)
297 {
298         g_return_if_fail(folder != NULL);
299
300         g_free(folder->name);
301         folder->name = name ? g_strdup(name) : NULL;
302         if (folder->node && folder->node->data) {
303                 FolderItem *item = (FolderItem *)folder->node->data;
304
305                 g_free(item->name);
306                 item->name = name ? g_strdup(name) : NULL;
307         }
308 }
309
310 gboolean folder_tree_destroy_func(GNode *node, gpointer data) {
311         FolderItem *item = (FolderItem *) node->data;
312
313         folder_item_destroy(item);
314         return FALSE;
315 }
316
317 void folder_tree_destroy(Folder *folder)
318 {
319         g_return_if_fail(folder != NULL);
320         g_return_if_fail(folder->node != NULL);
321         
322         prefs_scoring_clear();
323         prefs_filtering_clear();
324
325         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_tree_destroy_func, NULL);
326         if (folder->node)
327                 g_node_destroy(folder->node);
328
329         folder->inbox = NULL;
330         folder->outbox = NULL;
331         folder->draft = NULL;
332         folder->queue = NULL;
333         folder->trash = NULL;
334         folder->node = NULL;
335 }
336
337 void folder_add(Folder *folder)
338 {
339         Folder *cur_folder;
340         GList *cur;
341         gint i;
342
343         g_return_if_fail(folder != NULL);
344
345         for (i = 0, cur = folder_list; cur != NULL; cur = cur->next, i++) {
346                 cur_folder = FOLDER(cur->data);
347                 if (folder->type == F_MH) {
348                         if (cur_folder->type != F_MH) break;
349                 } else if (folder->type == F_MBOX) {
350                         if (cur_folder->type != F_MH &&
351                             cur_folder->type != F_MBOX) break;
352                 } else if (folder->type == F_IMAP) {
353                         if (cur_folder->type != F_MH &&
354                             cur_folder->type != F_MBOX &&
355                             cur_folder->type != F_IMAP) break;
356                 } else if (folder->type == F_NEWS) {
357                         if (cur_folder->type != F_MH &&
358                             cur_folder->type != F_MBOX &&
359                             cur_folder->type != F_IMAP &&
360                             cur_folder->type != F_NEWS) break;
361                 }
362         }
363
364         folder_list = g_list_insert(folder_list, folder, i);
365 }
366
367 GList *folder_get_list(void)
368 {
369         return folder_list;
370 }
371
372 gint folder_read_list(void)
373 {
374         GNode *node;
375         XMLNode *xmlnode;
376         gchar *path;
377
378         path = folder_get_list_path();
379         if (!is_file_exist(path)) return -1;
380         node = xml_parse_file(path);
381         if (!node) return -1;
382
383         xmlnode = node->data;
384         if (strcmp2(xmlnode->tag->tag, "folderlist") != 0) {
385                 g_warning("wrong folder list\n");
386                 xml_free_tree(node);
387                 return -1;
388         }
389
390         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, 2,
391                         folder_read_folder_func, NULL);
392
393         xml_free_tree(node);
394         if (folder_list)
395                 return 0;
396         else
397                 return -1;
398 }
399
400 void folder_write_list(void)
401 {
402         GList *list;
403         Folder *folder;
404         gchar *path;
405         PrefFile *pfile;
406
407         path = folder_get_list_path();
408         if ((pfile = prefs_write_open(path)) == NULL) return;
409
410         fprintf(pfile->fp, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
411                 conv_get_current_charset_str());
412         fputs("\n<folderlist>\n", pfile->fp);
413
414         for (list = folder_list; list != NULL; list = list->next) {
415                 folder = list->data;
416                 folder_write_list_recursive(folder->node, pfile->fp);
417         }
418
419         fputs("</folderlist>\n", pfile->fp);
420
421         if (prefs_file_close(pfile) < 0)
422                 g_warning("failed to write folder list.\n");
423 }
424
425 gboolean folder_scan_tree_func(GNode *node, gpointer data)
426 {
427         GHashTable *pptable = (GHashTable *)data;
428         FolderItem *item = (FolderItem *)node->data;
429         
430         folder_item_restore_persist_prefs(item, pptable);
431
432         return FALSE;
433 }
434
435 void folder_scan_tree(Folder *folder)
436 {
437         GHashTable *pptable;
438         
439         if (!folder->scan_tree)
440                 return;
441         
442         pptable = folder_persist_prefs_new(folder);
443         folder_tree_destroy(folder);
444
445         folder->scan_tree(folder);
446
447         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_scan_tree_func, pptable);
448         folder_persist_prefs_free(pptable);
449
450         prefs_matcher_read_config();
451
452         folder_write_list();
453 }
454
455 FolderItem *folder_create_folder(FolderItem *parent, const gchar *name)
456 {
457         FolderItem *new_item;
458
459         new_item = parent->folder->create_folder(parent->folder, parent, name);
460         if (new_item)
461                 new_item->cache = msgcache_new();
462
463         return new_item;
464 }
465
466 struct TotalMsgCount
467 {
468         guint new;
469         guint unread;
470         guint unreadmarked;
471         guint total;
472 };
473
474 struct FuncToAllFoldersData
475 {
476         FolderItemFunc  function;
477         gpointer        data;
478 };
479
480 static gboolean folder_func_to_all_folders_func(GNode *node, gpointer data)
481 {
482         FolderItem *item;
483         struct FuncToAllFoldersData *function_data = (struct FuncToAllFoldersData *) data;
484
485         g_return_val_if_fail(node->data != NULL, FALSE);
486
487         item = FOLDER_ITEM(node->data);
488         g_return_val_if_fail(item != NULL, FALSE);
489
490         function_data->function(item, function_data->data);
491
492         return FALSE;
493 }
494
495 void folder_func_to_all_folders(FolderItemFunc function, gpointer data)
496 {
497         GList *list;
498         Folder *folder;
499         struct FuncToAllFoldersData function_data;
500         
501         function_data.function = function;
502         function_data.data = data;
503
504         for (list = folder_list; list != NULL; list = list->next) {
505                 folder = FOLDER(list->data);
506                 if (folder->node)
507                         g_node_traverse(folder->node, G_PRE_ORDER,
508                                         G_TRAVERSE_ALL, -1,
509                                         folder_func_to_all_folders_func,
510                                         &function_data);
511         }
512 }
513
514 static void folder_count_total_msgs_func(FolderItem *item, gpointer data)
515 {
516         struct TotalMsgCount *count = (struct TotalMsgCount *)data;
517
518         count->new += item->new;
519         count->unread += item->unread;
520         count->unreadmarked += item->unreadmarked;
521         count->total += item->total;
522 }
523
524 void folder_count_total_msgs(guint *new, guint *unread, guint *unreadmarked, guint *total)
525 {
526         struct TotalMsgCount count;
527
528         count.new = count.unread = count.unreadmarked = count.total = 0;
529
530         debug_print("Counting total number of messages...\n");
531
532         folder_func_to_all_folders(folder_count_total_msgs_func, &count);
533
534         *new = count.new;
535         *unread = count.unread;
536         *unreadmarked = count.unreadmarked;
537         *total = count.total;
538 }
539
540 Folder *folder_find_from_path(const gchar *path)
541 {
542         GList *list;
543         Folder *folder;
544
545         for (list = folder_list; list != NULL; list = list->next) {
546                 folder = list->data;
547                 if ((folder->type == F_MH || folder->type == F_MBOX) &&
548                     !path_cmp(LOCAL_FOLDER(folder)->rootpath, path))
549                         return folder;
550         }
551
552         return NULL;
553 }
554
555 Folder *folder_find_from_name(const gchar *name, FolderType type)
556 {
557         GList *list;
558         Folder *folder;
559
560         for (list = folder_list; list != NULL; list = list->next) {
561                 folder = list->data;
562                 if (folder->type == type && strcmp2(name, folder->name) == 0)
563                         return folder;
564         }
565
566         return NULL;
567 }
568
569 static gboolean folder_item_find_func(GNode *node, gpointer data)
570 {
571         FolderItem *item = node->data;
572         gpointer *d = data;
573         const gchar *path = d[0];
574
575         if (path_cmp(path, item->path) != 0)
576                 return FALSE;
577
578         d[1] = item;
579
580         return TRUE;
581 }
582
583 FolderItem *folder_find_item_from_path(const gchar *path)
584 {
585         Folder *folder;
586         gpointer d[2];
587
588         folder = folder_get_default_folder();
589         g_return_val_if_fail(folder != NULL, NULL);
590
591         d[0] = (gpointer)path;
592         d[1] = NULL;
593         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
594                         folder_item_find_func, d);
595         return d[1];
596 }
597
598 static const struct {
599         gchar *str;
600         FolderType type;
601 } type_str_table[] = {
602         {"#mh"     , F_MH},
603         {"#mbox"   , F_MBOX},
604         {"#maildir", F_MAILDIR},
605         {"#imap"   , F_IMAP},
606         {"#news"   , F_NEWS}
607 };
608
609 static gchar *folder_get_type_string(FolderType type)
610 {
611         gint i;
612
613         for (i = 0; i < sizeof(type_str_table) / sizeof(type_str_table[0]);
614              i++) {
615                 if (type_str_table[i].type == type)
616                         return type_str_table[i].str;
617         }
618
619         return NULL;
620 }
621
622 static FolderType folder_get_type_from_string(const gchar *str)
623 {
624         gint i;
625
626         for (i = 0; i < sizeof(type_str_table) / sizeof(type_str_table[0]);
627              i++) {
628                 if (g_strcasecmp(type_str_table[i].str, str) == 0)
629                         return type_str_table[i].type;
630         }
631
632         return F_UNKNOWN;
633 }
634
635 gchar *folder_get_identifier(Folder *folder)
636 {
637         gchar *type_str;
638
639         g_return_val_if_fail(folder != NULL, NULL);
640
641         type_str = folder_get_type_string(folder->type);
642         return g_strconcat(type_str, "/", folder->name, NULL);
643 }
644
645 gchar *folder_item_get_identifier(FolderItem *item)
646 {
647         gchar *id;
648         gchar *folder_id;
649
650         g_return_val_if_fail(item != NULL, NULL);
651         g_return_val_if_fail(item->path != NULL, NULL);
652
653         folder_id = folder_get_identifier(item->folder);
654         id = g_strconcat(folder_id, "/", item->path, NULL);
655         g_free(folder_id);
656
657         return id;
658 }
659
660 FolderItem *folder_find_item_from_identifier(const gchar *identifier)
661 {
662         Folder *folder;
663         gpointer d[2];
664         gchar *str;
665         gchar *p;
666         gchar *name;
667         gchar *path;
668         FolderType type;
669
670         g_return_val_if_fail(identifier != NULL, NULL);
671
672         if (*identifier != '#')
673                 return folder_find_item_from_path(identifier);
674
675         Xstrdup_a(str, identifier, return NULL);
676
677         p = strchr(str, '/');
678         if (!p)
679                 return folder_find_item_from_path(identifier);
680         *p = '\0';
681         p++;
682         type = folder_get_type_from_string(str);
683         if (type == F_UNKNOWN)
684                 return folder_find_item_from_path(identifier);
685
686         name = p;
687         p = strchr(p, '/');
688         if (!p)
689                 return folder_find_item_from_path(identifier);
690         *p = '\0';
691         p++;
692
693         folder = folder_find_from_name(name, type);
694         if (!folder)
695                 return folder_find_item_from_path(identifier);
696
697         path = p;
698
699         d[0] = (gpointer)path;
700         d[1] = NULL;
701         g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
702                         folder_item_find_func, d);
703         return d[1];
704 }
705
706 Folder *folder_get_default_folder(void)
707 {
708         return folder_list ? FOLDER(folder_list->data) : NULL;
709 }
710
711 FolderItem *folder_get_default_inbox(void)
712 {
713         Folder *folder;
714
715         if (!folder_list) return NULL;
716         folder = FOLDER(folder_list->data);
717         g_return_val_if_fail(folder != NULL, NULL);
718         return folder->inbox;
719 }
720
721 FolderItem *folder_get_default_outbox(void)
722 {
723         Folder *folder;
724
725         if (!folder_list) return NULL;
726         folder = FOLDER(folder_list->data);
727         g_return_val_if_fail(folder != NULL, NULL);
728         return folder->outbox;
729 }
730
731 FolderItem *folder_get_default_draft(void)
732 {
733         Folder *folder;
734
735         if (!folder_list) return NULL;
736         folder = FOLDER(folder_list->data);
737         g_return_val_if_fail(folder != NULL, NULL);
738         return folder->draft;
739 }
740
741 FolderItem *folder_get_default_queue(void)
742 {
743         Folder *folder;
744
745         if (!folder_list) return NULL;
746         folder = FOLDER(folder_list->data);
747         g_return_val_if_fail(folder != NULL, NULL);
748         return folder->queue;
749 }
750
751 FolderItem *folder_get_default_trash(void)
752 {
753         Folder *folder;
754
755         if (!folder_list) return NULL;
756         folder = FOLDER(folder_list->data);
757         g_return_val_if_fail(folder != NULL, NULL);
758         return folder->trash;
759 }
760
761 #define CREATE_FOLDER_IF_NOT_EXIST(member, dir, type)           \
762 {                                                               \
763         if (!folder->member) {                                  \
764                 item = folder_item_new(folder, dir, dir);       \
765                 item->stype = type;                             \
766                 folder_item_append(rootitem, item);             \
767                 folder->member = item;                          \
768         }                                                       \
769 }
770
771 void folder_set_missing_folders(void)
772 {
773         Folder *folder;
774         FolderItem *rootitem;
775         FolderItem *item;
776         GList *list;
777
778         for (list = folder_list; list != NULL; list = list->next) {
779                 folder = list->data;
780                 if (folder->type != F_MH) continue;
781                 rootitem = FOLDER_ITEM(folder->node->data);
782                 g_return_if_fail(rootitem != NULL);
783
784                 if (folder->inbox && folder->outbox && folder->draft &&
785                     folder->queue && folder->trash)
786                         continue;
787
788                 if (folder->create_tree(folder) < 0) {
789                         g_warning("%s: can't create the folder tree.\n",
790                                   LOCAL_FOLDER(folder)->rootpath);
791                         continue;
792                 }
793
794                 CREATE_FOLDER_IF_NOT_EXIST(inbox,  INBOX_DIR,  F_INBOX);
795                 CREATE_FOLDER_IF_NOT_EXIST(outbox, OUTBOX_DIR, F_OUTBOX);
796                 CREATE_FOLDER_IF_NOT_EXIST(draft,  DRAFT_DIR,  F_DRAFT);
797                 CREATE_FOLDER_IF_NOT_EXIST(queue,  QUEUE_DIR,  F_QUEUE);
798                 CREATE_FOLDER_IF_NOT_EXIST(trash,  TRASH_DIR,  F_TRASH);
799         }
800 }
801
802 static gboolean folder_unref_account_func(GNode *node, gpointer data)
803 {
804         FolderItem *item = node->data;
805         PrefsAccount *account = data;
806
807         if (item->account == account)
808                 item->account = NULL;
809
810         return FALSE;
811 }
812
813 void folder_unref_account_all(PrefsAccount *account)
814 {
815         Folder *folder;
816         GList *list;
817
818         if (!account) return;
819
820         for (list = folder_list; list != NULL; list = list->next) {
821                 folder = list->data;
822                 if (folder->account == account)
823                         folder->account = NULL;
824                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
825                                 folder_unref_account_func, account);
826         }
827 }
828
829 #undef CREATE_FOLDER_IF_NOT_EXIST
830
831 gchar *folder_get_path(Folder *folder)
832 {
833         gchar *path;
834
835         g_return_val_if_fail(folder != NULL, NULL);
836
837         switch(FOLDER_TYPE(folder)) {
838
839                 case F_MH:
840                         path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
841                         break;
842
843                 case F_IMAP:
844                         g_return_val_if_fail(folder->account != NULL, NULL);
845                         path = g_strconcat(get_imap_cache_dir(),
846                                            G_DIR_SEPARATOR_S,
847                                            folder->account->recv_server,
848                                            G_DIR_SEPARATOR_S,
849                                            folder->account->userid,
850                                            NULL);
851                         break;
852
853                 case F_NEWS:
854                         g_return_val_if_fail(folder->account != NULL, NULL);
855                         path = g_strconcat(get_news_cache_dir(),
856                                            G_DIR_SEPARATOR_S,
857                                            folder->account->nntp_server,
858                                            NULL);
859                         break;
860
861                 default:
862                         path = NULL;
863                         break;
864         }
865         
866         return path;
867 }
868
869 gchar *folder_item_get_path(FolderItem *item)
870 {
871         gchar *folder_path;
872         gchar *path;
873
874         g_return_val_if_fail(item != NULL, NULL);
875
876         if(FOLDER_TYPE(item->folder) != F_MBOX) {
877                 folder_path = folder_get_path(item->folder);
878                 g_return_val_if_fail(folder_path != NULL, NULL);
879
880                 if (folder_path[0] == G_DIR_SEPARATOR) {
881                         if (item->path)
882                                 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
883                                                    item->path, NULL);
884                         else
885                                 path = g_strdup(folder_path);
886                 } else {
887                         if (item->path)
888                                 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
889                                                    folder_path, G_DIR_SEPARATOR_S,
890                                                    item->path, NULL);
891                         else
892                                 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
893                                                    folder_path, NULL);
894                 }
895
896                 g_free(folder_path);
897         } else {
898                 gchar *itempath;
899
900                 itempath = mbox_get_virtual_path(item);
901                 if (itempath == NULL)
902                         return NULL;
903                 path = g_strconcat(get_mbox_cache_dir(),
904                                           G_DIR_SEPARATOR_S, itempath, NULL);
905                 g_free(itempath);
906         }
907         return path;
908 }
909
910 void folder_item_set_default_flags(FolderItem *dest, MsgFlags *flags)
911 {
912         if (!(dest->stype == F_OUTBOX ||
913               dest->stype == F_QUEUE  ||
914               dest->stype == F_DRAFT  ||
915               dest->stype == F_TRASH)) {
916                 flags->perm_flags = MSG_NEW|MSG_UNREAD;
917         } else {
918                 flags->perm_flags = 0;
919         }
920         flags->tmp_flags = MSG_CACHED;
921         if (dest->folder->type == F_MH) {
922                 if (dest->stype == F_QUEUE) {
923                         MSG_SET_TMP_FLAGS(*flags, MSG_QUEUED);
924                 } else if (dest->stype == F_DRAFT) {
925                         MSG_SET_TMP_FLAGS(*flags, MSG_DRAFT);
926                 }
927         } else if (dest->folder->type == F_IMAP) {
928                 MSG_SET_TMP_FLAGS(*flags, MSG_IMAP);
929         } else if (dest->folder->type == F_NEWS) {
930                 MSG_SET_TMP_FLAGS(*flags, MSG_NEWS);
931         }
932 }
933
934 static gint folder_sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
935 {
936         MsgInfo *msginfo_a = (MsgInfo *) a;
937         MsgInfo *msginfo_b = (MsgInfo *) b;
938
939         return (msginfo_a->msgnum - msginfo_b->msgnum);
940 }
941
942 static gint folder_sort_folder_list(gconstpointer a, gconstpointer b)
943 {
944         guint gint_a = GPOINTER_TO_INT(a);
945         guint gint_b = GPOINTER_TO_INT(b);
946         
947         return (gint_a - gint_b);
948 }
949
950 gint folder_item_open(FolderItem *item)
951 {
952         if(((item->folder->type == F_IMAP) && !item->no_select) || (item->folder->type == F_NEWS)) {
953                 folder_item_scan(item);
954         }
955
956         /* Processing */
957         if(item->prefs->processing != NULL) {
958                 gchar *buf;
959                 
960                 buf = g_strdup_printf(_("Processing (%s)...\n"), item->path);
961                 debug_print("%s\n", buf);
962                 g_free(buf);
963         
964                 folder_item_apply_processing(item);
965
966                 debug_print("done.\n");
967         }
968
969         return 0;
970 }
971
972 void folder_item_close(FolderItem *item)
973 {
974         GSList *mlist, *cur;
975         
976         g_return_if_fail(item != NULL);
977
978         if (item->new) {
979                 mlist = folder_item_get_msg_list(item);
980                 for (cur = mlist ; cur != NULL ; cur = cur->next) {
981                         MsgInfo * msginfo;
982
983                         msginfo = (MsgInfo *) cur->data;
984                         if (MSG_IS_NEW(msginfo->flags))
985                                 procmsg_msginfo_unset_flags(msginfo, MSG_NEW, 0);
986                         procmsg_msginfo_free(msginfo);
987                 }
988                 g_slist_free(mlist);
989         }               
990
991         folder_item_write_cache(item);
992         
993         folder_item_update(item, F_ITEM_UPDATE_MSGCNT);
994 }
995
996 gint folder_item_scan(FolderItem *item)
997 {
998         Folder *folder;
999         GSList *folder_list = NULL, *cache_list = NULL, *folder_list_cur, *cache_list_cur, *new_list = NULL;
1000         guint newcnt = 0, unreadcnt = 0, totalcnt = 0, unreadmarkedcnt = 0;
1001         guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
1002         gboolean update_flags = 0;
1003     
1004         g_return_val_if_fail(item != NULL, -1);
1005         if (item->path == NULL) return -1;
1006
1007         folder = item->folder;
1008
1009         g_return_val_if_fail(folder != NULL, -1);
1010         g_return_val_if_fail(folder->get_num_list != NULL, -1);
1011
1012         debug_print("Scanning folder %s for cache changes.\n", item->path);
1013
1014         /* Get list of messages for folder and cache */
1015         if (folder->get_num_list(item->folder, item, &folder_list) < 0) {
1016                 debug_print("Error fetching list of message numbers\n");
1017                 return(-1);
1018         }
1019
1020         if (!folder->check_msgnum_validity || 
1021             folder->check_msgnum_validity(folder, item)) {
1022                 if (!item->cache)
1023                         folder_item_read_cache(item);
1024                 cache_list = msgcache_get_msg_list(item->cache);
1025         } else {
1026                 if (item->cache)
1027                         msgcache_destroy(item->cache);
1028                 item->cache = msgcache_new();
1029                 cache_list = NULL;
1030         }
1031
1032         /* Sort both lists */
1033         cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
1034         folder_list = g_slist_sort(folder_list, folder_sort_folder_list);
1035
1036         cache_list_cur = cache_list;
1037         folder_list_cur = folder_list;
1038
1039         if (cache_list_cur != NULL) {
1040                 GSList *cache_list_last;
1041         
1042                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1043                 cache_list_last = g_slist_last(cache_list);
1044                 cache_max_num = ((MsgInfo *)cache_list_last->data)->msgnum;
1045         } else {
1046                 cache_cur_num = G_MAXINT;
1047                 cache_max_num = 0;
1048         }
1049
1050         if (folder_list_cur != NULL) {
1051                 GSList *folder_list_last;
1052         
1053                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1054                 folder_list_last = g_slist_last(folder_list);
1055                 folder_max_num = GPOINTER_TO_INT(folder_list_last->data);
1056         } else {
1057                 folder_cur_num = G_MAXINT;
1058                 folder_max_num = 0;
1059         }
1060
1061         while ((cache_cur_num != G_MAXINT) || (folder_cur_num != G_MAXINT)) {
1062                 /*
1063                  *  Message only exists in the folder
1064                  *  Remember message for fetching
1065                  */
1066                 if (folder_cur_num < cache_cur_num) {
1067                         gboolean add = FALSE;
1068
1069                         switch(folder->type) {
1070                                 case F_NEWS:
1071                                         if (folder_cur_num < cache_max_num)
1072                                                 break;
1073                                         
1074                                         if (prefs_common.max_articles == 0) {
1075                                                 add = TRUE;
1076                                         }
1077
1078                                         if (folder_max_num <= prefs_common.max_articles) {
1079                                                 add = TRUE;
1080                                         } else if (folder_cur_num > (folder_max_num - prefs_common.max_articles)) {
1081                                                 add = TRUE;
1082                                         }
1083                                         break;
1084                                 default:
1085                                         add = TRUE;
1086                                         break;
1087                         }
1088                         
1089                         if (add) {
1090                                 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(folder_cur_num));
1091                                 debug_print("Remembered message %d for fetching\n", folder_cur_num);
1092                         }
1093
1094                         /* Move to next folder number */
1095                         folder_list_cur = folder_list_cur->next;
1096
1097                         if (folder_list_cur != NULL)
1098                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1099                         else
1100                                 folder_cur_num = G_MAXINT;
1101
1102                         continue;
1103                 }
1104
1105                 /*
1106                  *  Message only exists in the cache
1107                  *  Remove the message from the cache
1108                  */
1109                 if (cache_cur_num < folder_cur_num) {
1110                         msgcache_remove_msg(item->cache, cache_cur_num);
1111                         debug_print("Removed message %d from cache.\n", cache_cur_num);
1112
1113                         /* Move to next cache number */
1114                         cache_list_cur = cache_list_cur->next;
1115
1116                         if (cache_list_cur != NULL)
1117                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1118                         else
1119                                 cache_cur_num = G_MAXINT;
1120
1121                         update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1122
1123                         continue;
1124                 }
1125
1126                 /*
1127                  *  Message number exists in folder and cache!
1128                  *  Check if the message has been modified
1129                  */
1130                 if (cache_cur_num == folder_cur_num) {
1131                         MsgInfo *msginfo;
1132
1133                         msginfo = msgcache_get_msg(item->cache, folder_cur_num);
1134                         if (folder->is_msg_changed && folder->is_msg_changed(folder, item, msginfo)) {
1135                                 MsgInfo *newmsginfo;
1136
1137                                 msgcache_remove_msg(item->cache, msginfo->msgnum);
1138
1139                                 if (NULL != (newmsginfo = folder->get_msginfo(folder, item, folder_cur_num))) {
1140                                         msgcache_add_msg(item->cache, newmsginfo);
1141                                         if (MSG_IS_NEW(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
1142                                                 newcnt++;
1143                                         if (MSG_IS_UNREAD(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
1144                                                 unreadcnt++;
1145                                         if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
1146                                                 unreadmarkedcnt++;
1147                                         if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
1148                                                 procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
1149                                         procmsg_msginfo_free(newmsginfo);
1150                                 }                                       
1151
1152                                 debug_print("Updated msginfo for message %d.\n", folder_cur_num);
1153                         } else {
1154                                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1155                                         newcnt++;
1156                                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1157                                         unreadcnt++;
1158                                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1159                                         unreadmarkedcnt++;
1160                                 if (!MSG_IS_IGNORE_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD))
1161                                         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
1162                         }
1163                         totalcnt++;
1164                         procmsg_msginfo_free(msginfo);
1165
1166                         /* Move to next folder and cache number */
1167                         cache_list_cur = cache_list_cur->next;
1168                         folder_list_cur = folder_list_cur->next;
1169
1170                         if (cache_list_cur != NULL)
1171                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1172                         else
1173                                 cache_cur_num = G_MAXINT;
1174
1175                         if (folder_list_cur != NULL)
1176                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1177                         else
1178                                 folder_cur_num = G_MAXINT;
1179
1180                         update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1181
1182                         continue;
1183                 }
1184         }
1185
1186         for(cache_list_cur = cache_list; cache_list_cur != NULL; cache_list_cur = g_slist_next(cache_list_cur)) {
1187                 procmsg_msginfo_free((MsgInfo *) cache_list_cur->data);
1188         }
1189
1190         g_slist_free(cache_list);
1191         g_slist_free(folder_list);
1192
1193         if (folder->get_msginfos) {
1194                 GSList *elem;
1195                 GSList *newmsg_list;
1196                 MsgInfo *msginfo;
1197                 
1198                 if (new_list) {
1199                         newmsg_list = folder->get_msginfos(folder, item, new_list);
1200                         for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
1201                                 msginfo = (MsgInfo *) elem->data;
1202                                 msgcache_add_msg(item->cache, msginfo);
1203                                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1204                                         newcnt++;
1205                                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1206                                         unreadcnt++;
1207                                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1208                                         unreadmarkedcnt++;
1209                                 if (procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD))
1210                                         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
1211                                 totalcnt++;
1212                                 procmsg_msginfo_free(msginfo);
1213
1214                                 update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1215                         }
1216                         g_slist_free(newmsg_list);
1217                 }
1218         } else if (folder->get_msginfo) {
1219                 GSList *elem;
1220         
1221                 for (elem = new_list; elem != NULL; elem = g_slist_next(elem)) {
1222                         MsgInfo *msginfo;
1223                         guint num;
1224
1225                         num = GPOINTER_TO_INT(elem->data);
1226                         msginfo = folder->get_msginfo(folder, item, num);
1227                         if (msginfo != NULL) {
1228                                 msgcache_add_msg(item->cache, msginfo);
1229                                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1230                                     newcnt++;
1231                                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1232                                     unreadcnt++;
1233                                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1234                                         unreadmarkedcnt++;
1235                                 if (procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD))
1236                                         procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
1237                                 totalcnt++;
1238                                 procmsg_msginfo_free(msginfo);
1239                                 debug_print("Added newly found message %d to cache.\n", num);
1240                         }
1241
1242                         update_flags |= F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT;
1243                 }
1244         }
1245
1246         item->new = newcnt;
1247         item->unread = unreadcnt;
1248         item->total = totalcnt;
1249         item->unreadmarked = unreadmarkedcnt;
1250         g_slist_free(new_list);
1251
1252         folder_item_update(item, update_flags);
1253
1254         return 0;
1255 }
1256
1257 static void folder_item_scan_foreach_func(gpointer key, gpointer val,
1258                                           gpointer data)
1259 {
1260         folder_item_scan(FOLDER_ITEM(key));
1261 }
1262
1263 void folder_item_scan_foreach(GHashTable *table)
1264 {
1265         g_hash_table_foreach(table, folder_item_scan_foreach_func, NULL);
1266 }
1267
1268 void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
1269 {
1270         gint *memusage = (gint *)data;
1271
1272         if (item->cache == NULL)
1273                 return;
1274         
1275         *memusage += msgcache_get_memory_usage(item->cache);
1276 }
1277
1278 gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
1279 {
1280         FolderItem *fa = (FolderItem *)a;
1281         FolderItem *fb = (FolderItem *)b;
1282         
1283         return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
1284 }
1285
1286 void folder_find_expired_caches(FolderItem *item, gpointer data)
1287 {
1288         GSList **folder_item_list = (GSList **)data;
1289         gint difftime, expiretime;
1290         
1291         if (item->cache == NULL)
1292                 return;
1293
1294         if (item->opened > 0)
1295                 return;
1296
1297         difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
1298         expiretime = prefs_common.cache_min_keep_time * 60;
1299         debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
1300         if (difftime > expiretime) {
1301                 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
1302         }
1303 }
1304
1305 void folder_item_free_cache(FolderItem *item)
1306 {
1307         g_return_if_fail(item != NULL);
1308         
1309         if (item->cache == NULL)
1310                 return;
1311         
1312         if (item->opened > 0)
1313                 return;
1314
1315         folder_item_write_cache(item);
1316         msgcache_destroy(item->cache);
1317         item->cache = NULL;
1318 }
1319
1320 void folder_clean_cache_memory()
1321 {
1322         gint memusage = 0;
1323
1324         folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);       
1325         debug_print("Total cache memory usage: %d\n", memusage);
1326         
1327         if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
1328                 GSList *folder_item_list = NULL, *listitem;
1329                 
1330                 debug_print("Trying to free cache memory\n");
1331
1332                 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);      
1333                 listitem = folder_item_list;
1334                 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
1335                         FolderItem *item = (FolderItem *)(listitem->data);
1336
1337                         debug_print("Freeing cache memory for %s\n", item->path);
1338                         memusage -= msgcache_get_memory_usage(item->cache);
1339                         folder_item_free_cache(item);
1340                         listitem = listitem->next;
1341                 }
1342                 g_slist_free(folder_item_list);
1343         }
1344 }
1345
1346 void folder_item_read_cache(FolderItem *item)
1347 {
1348         gchar *cache_file, *mark_file;
1349         
1350         g_return_if_fail(item != NULL);
1351
1352         cache_file = folder_item_get_cache_file(item);
1353         mark_file = folder_item_get_mark_file(item);
1354         item->cache = msgcache_read_cache(item, cache_file);
1355         if (!item->cache) {
1356                 item->cache = msgcache_new();
1357                 folder_item_scan(item);
1358         }
1359         msgcache_read_mark(item->cache, mark_file);
1360         g_free(cache_file);
1361         g_free(mark_file);
1362
1363         folder_clean_cache_memory();
1364 }
1365
1366 void folder_item_write_cache(FolderItem *item)
1367 {
1368         gchar *cache_file, *mark_file;
1369         PrefsFolderItem *prefs;
1370         gint filemode = 0;
1371         gchar *id;
1372         
1373         if (!item || !item->path || !item->cache)
1374                 return;
1375
1376         id = folder_item_get_identifier(item);
1377         debug_print("Save cache for folder %s\n", id);
1378         g_free(id);
1379
1380         cache_file = folder_item_get_cache_file(item);
1381         mark_file = folder_item_get_mark_file(item);
1382         if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
1383                 prefs = item->prefs;
1384                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
1385                         /* for cache file */
1386                         filemode = prefs->folder_chmod;
1387                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
1388                         if (filemode & S_IROTH) filemode |= S_IWOTH;
1389                         chmod(cache_file, filemode);
1390                 }
1391         }
1392
1393         g_free(cache_file);
1394         g_free(mark_file);
1395 }
1396
1397 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
1398 {
1399         Folder *folder;
1400         MsgInfo *msginfo;
1401         
1402         g_return_val_if_fail(item != NULL, NULL);
1403         
1404         folder = item->folder;
1405         if (!item->cache)
1406                 folder_item_read_cache(item);
1407         
1408         if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
1409                 return msginfo;
1410         
1411         g_return_val_if_fail(folder->get_msginfo, NULL);
1412         if ((msginfo = folder->get_msginfo(folder, item, num)) != NULL) {
1413                 msgcache_add_msg(item->cache, msginfo);
1414                 return msginfo;
1415         }
1416         
1417         return NULL;
1418 }
1419
1420 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
1421 {
1422         Folder *folder;
1423         MsgInfo *msginfo;
1424         
1425         g_return_val_if_fail(item != NULL, NULL);
1426         
1427         folder = item->folder;
1428         if (!item->cache)
1429                 folder_item_read_cache(item);
1430         
1431         if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
1432                 return msginfo;
1433
1434         return NULL;
1435 }
1436
1437 GSList *folder_item_get_msg_list(FolderItem *item)
1438 {
1439         g_return_val_if_fail(item != NULL, NULL);
1440         
1441         if (item->cache == 0)
1442                 folder_item_read_cache(item);
1443
1444         g_return_val_if_fail(item->cache != NULL, NULL);
1445         
1446         return msgcache_get_msg_list(item->cache);
1447 }
1448
1449 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
1450 {
1451         Folder *folder;
1452
1453         g_return_val_if_fail(item != NULL, NULL);
1454
1455         folder = item->folder;
1456
1457         g_return_val_if_fail(folder->fetch_msg != NULL, NULL);
1458
1459         return folder->fetch_msg(folder, item, num);
1460 }
1461
1462 static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
1463 {
1464         static HeaderEntry hentry[] = {{"Message-ID:",  NULL, TRUE},
1465                                        {NULL,           NULL, FALSE}};
1466         FILE *fp;
1467         MsgInfo *msginfo;
1468         gint msgnum = 0;
1469         gchar buf[BUFFSIZE];
1470
1471         if ((fp = fopen(file, "rb")) == NULL)
1472                 return 0;
1473
1474         if ((dest->stype == F_QUEUE) || (dest->stype == F_DRAFT))
1475                 while (fgets(buf, sizeof(buf), fp) != NULL)
1476                         if (buf[0] == '\r' || buf[0] == '\n') break;
1477
1478         procheader_get_header_fields(fp, hentry);
1479         if (hentry[0].body) {
1480                 extract_parenthesis(hentry[0].body, '<', '>');
1481                 remove_space(hentry[0].body);
1482                 if ((msginfo = msgcache_get_msg_by_id(dest->cache, hentry[0].body)) != NULL) {
1483                         msgnum = msginfo->msgnum;
1484                         procmsg_msginfo_free(msginfo);
1485
1486                         debug_print("found message as uid %d\n", msgnum);
1487                 }
1488         }
1489         
1490         g_free(hentry[0].body);
1491         fclose(fp);
1492
1493         return msgnum;
1494 }
1495
1496 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
1497                          gboolean remove_source)
1498 {
1499         Folder *folder;
1500         gint num;
1501         MsgInfo *msginfo;
1502
1503         g_return_val_if_fail(dest != NULL, -1);
1504         g_return_val_if_fail(file != NULL, -1);
1505
1506         folder = dest->folder;
1507
1508         g_return_val_if_fail(folder->add_msg != NULL, -1);
1509
1510         if (!dest->cache)
1511                 folder_item_read_cache(dest);
1512
1513         num = folder->add_msg(folder, dest, file, FALSE);
1514
1515         if (num > 0) {
1516                 msginfo = folder->get_msginfo(folder, dest, num);
1517
1518                 if (msginfo != NULL) {
1519                         if (MSG_IS_NEW(msginfo->flags))
1520                                 dest->new++;
1521                         if (MSG_IS_UNREAD(msginfo->flags))
1522                                 dest->unread++;
1523                         if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1524                                 dest->unreadmarked++;
1525                         if (procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD))
1526                                 procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
1527                         dest->total++;
1528                         folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1529
1530                         msgcache_add_msg(dest->cache, msginfo);
1531
1532                         procmsg_msginfo_free(msginfo);
1533                 }
1534
1535                 dest->last_num = num;
1536         } else if (num == 0) {
1537                 folder_item_scan(dest);
1538                 num = folder_item_get_msg_num_by_file(dest, file);
1539         }
1540
1541         if (num >= 0 && remove_source) {
1542                 if (unlink(file) < 0)
1543                         FILE_OP_ERROR(file, "unlink");
1544         }
1545
1546         return num;
1547 }
1548
1549 /*
1550 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1551 {
1552         Folder *folder;
1553         gint num;
1554
1555         g_return_val_if_fail(dest != NULL, -1);
1556         g_return_val_if_fail(msginfo != NULL, -1);
1557
1558         folder = dest->folder;
1559         if (dest->last_num < 0) folder->scan(folder, dest);
1560
1561         num = folder->move_msg(folder, dest, msginfo);
1562         if (num > 0) dest->last_num = num;
1563
1564         return num;
1565 }
1566 */
1567                 
1568 FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest) 
1569 {
1570         GSList *mlist;
1571         GSList *cur;
1572         FolderItem *new_item;
1573         FolderItem *next_item;
1574         GNode *srcnode;
1575         int cnt = 0;
1576         gchar *old_id, *new_id;
1577
1578         mlist = folder_item_get_msg_list(src);
1579
1580         /* move messages */
1581         debug_print("Moving %s to %s\n", src->path, dest->path);
1582         new_item = folder_create_folder(dest, g_basename(src->path));
1583         if (new_item == NULL) {
1584                 printf("Can't create folder\n");
1585                 return NULL;
1586         }
1587         
1588         if (new_item->folder == NULL)
1589                 new_item->folder = dest->folder;
1590
1591         /* move messages */
1592         for (cur = mlist ; cur != NULL ; cur = cur->next) {
1593                 MsgInfo * msginfo;
1594                 cnt++;
1595                 if (cnt%500)
1596                         log_message(_("Moving %s to %s (%d%%)...\n"), src->name, 
1597                                         new_item->path,
1598                                         100*cnt/g_slist_length(mlist));
1599                 msginfo = (MsgInfo *) cur->data;
1600                 folder_item_move_msg(new_item, msginfo);
1601
1602                 procmsg_msginfo_free(msginfo);
1603         }
1604         
1605         /*copy prefs*/
1606         prefs_folder_item_copy_prefs(src, new_item);
1607         new_item->collapsed = src->collapsed;
1608         new_item->thread_collapsed = src->thread_collapsed;
1609         new_item->threaded  = src->threaded;
1610         new_item->ret_rcpt  = src->ret_rcpt;
1611         new_item->hide_read_msgs = src->hide_read_msgs;
1612         new_item->sort_key  = src->sort_key;
1613         new_item->sort_type = src->sort_type;
1614
1615         prefs_matcher_write_config();
1616         
1617         /* recurse */
1618         srcnode = src->folder->node;    
1619         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
1620         srcnode = srcnode->children;
1621         while (srcnode != NULL) {
1622                 if (srcnode && srcnode->data) {
1623                         next_item = (FolderItem*) srcnode->data;
1624                         srcnode = srcnode->next;
1625                         if (folder_item_move_recursive(next_item, new_item) == NULL)
1626                                 return NULL;
1627                 }
1628         }
1629         old_id = folder_item_get_identifier(src);
1630         new_id = folder_item_get_identifier(new_item);
1631         debug_print("updating rules : %s => %s\n", old_id, new_id);
1632         
1633         src->folder->remove_folder(src->folder, src);
1634         folder_write_list();
1635
1636         if (old_id != NULL && new_id != NULL)
1637                 prefs_filtering_rename_path(old_id, new_id);
1638         g_free(old_id);
1639         g_free(new_id);
1640
1641         return new_item;
1642 }
1643
1644 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item)
1645 {
1646         FolderItem *tmp = dest->parent;
1647         gchar * src_identifier, * dst_identifier;
1648         gchar * phys_srcpath, * phys_dstpath;
1649         GNode *src_node;
1650         
1651         while (tmp) {
1652                 if (tmp == src) {
1653                         return F_MOVE_FAILED_DEST_IS_CHILD;
1654                 }
1655                 tmp = tmp->parent;
1656         }
1657         
1658         tmp = src->parent;
1659         
1660         src_identifier = folder_item_get_identifier(src);
1661         dst_identifier = folder_item_get_identifier(dest);
1662         
1663         if(dst_identifier == NULL && dest->folder && dest->parent == NULL) {
1664                 /* dest can be a root folder */
1665                 dst_identifier = folder_get_identifier(dest->folder);
1666         }
1667         if (src_identifier == NULL || dst_identifier == NULL) {
1668                 printf("Can't get identifiers\n");
1669                 return F_MOVE_FAILED;
1670         }
1671
1672         if (src->folder != dest->folder) {
1673                 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
1674         }
1675
1676         phys_srcpath = folder_item_get_path(src);
1677         phys_dstpath = g_strconcat(folder_item_get_path(dest),G_DIR_SEPARATOR_S,g_basename(phys_srcpath),NULL);
1678
1679         if (src->parent == dest || src == dest) {
1680                 g_free(src_identifier);
1681                 g_free(dst_identifier);
1682                 g_free(phys_srcpath);
1683                 g_free(phys_dstpath);
1684                 return F_MOVE_FAILED_DEST_IS_PARENT;
1685         }
1686         debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
1687         if ((tmp = folder_item_move_recursive(src, dest)) == NULL) {
1688                 return F_MOVE_FAILED;
1689         }
1690         
1691         /* update rules */
1692         src_node = g_node_find(src->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, src);
1693         if (src_node) 
1694                 g_node_destroy(src_node);
1695         else
1696                 debug_print("can't remove node: it's null!\n");
1697         /* not to much worry if remove fails, move has been done */
1698         
1699         g_free(src_identifier);
1700         g_free(dst_identifier);
1701         g_free(phys_srcpath);
1702         g_free(phys_dstpath);
1703
1704         *new_item = tmp;
1705
1706         return F_MOVE_OK;
1707 }
1708
1709 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1710 {
1711         Folder *folder;
1712         gint num;
1713         Folder *src_folder;
1714
1715         g_return_val_if_fail(dest != NULL, -1);
1716         g_return_val_if_fail(msginfo != NULL, -1);
1717
1718         folder = dest->folder;
1719
1720         g_return_val_if_fail(folder->remove_msg != NULL, -1);
1721         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1722
1723         if (!dest->cache) folder_item_read_cache(dest);
1724
1725         src_folder = msginfo->folder->folder;
1726
1727         num = folder->copy_msg(folder, dest, msginfo);
1728         
1729         if (num > 0) {
1730                 MsgInfo *newmsginfo;
1731     
1732                 /* Add new msginfo to dest folder */
1733                 if (NULL != (newmsginfo = folder->get_msginfo(folder, dest, num))) {
1734                         newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1735                         
1736                         if (dest->stype == F_OUTBOX || dest->stype == F_QUEUE  ||
1737                             dest->stype == F_DRAFT  || dest->stype == F_TRASH)
1738                                 MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1739                                                      MSG_NEW|MSG_UNREAD|MSG_DELETED);
1740                         msgcache_add_msg(dest->cache, newmsginfo);
1741
1742                         if (MSG_IS_NEW(newmsginfo->flags))
1743                                 dest->new++;
1744                         if (MSG_IS_UNREAD(newmsginfo->flags))
1745                                 dest->unread++;
1746                         if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
1747                                 dest->unreadmarked++;
1748                         if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
1749                                 procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
1750                         dest->total++;
1751                         folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1752
1753                         procmsg_msginfo_free(newmsginfo);
1754                 }
1755
1756                 /* remove source message from it's folder */
1757                 if (src_folder->remove_msg) {
1758                         src_folder->remove_msg(src_folder, msginfo->folder,
1759                                                msginfo->msgnum);
1760                         msgcache_remove_msg(msginfo->folder->cache, msginfo->msgnum);
1761
1762                         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1763                                 msginfo->folder->new--;
1764                         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1765                                 msginfo->folder->unread--;
1766                         if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1767                                 msginfo->folder->unreadmarked--;
1768                         msginfo->folder->total--;
1769                         folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1770                 }
1771         }
1772         
1773         if (folder->finished_copy)
1774                 folder->finished_copy(folder, dest);
1775
1776         return num;
1777 }
1778
1779 /*
1780 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1781 {
1782         Folder *folder;
1783         gint num;
1784
1785         g_return_val_if_fail(dest != NULL, -1);
1786         g_return_val_if_fail(msglist != NULL, -1);
1787
1788         folder = dest->folder;
1789         if (dest->last_num < 0) folder->scan(folder, dest);
1790
1791         num = folder->move_msgs_with_dest(folder, dest, msglist);
1792         if (num > 0) dest->last_num = num;
1793         else dest->op_count = 0;
1794
1795         return num;
1796 }
1797 */
1798
1799 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1800 {
1801         Folder *folder;
1802         FolderItem *item;
1803         GSList *newmsgnums = NULL;
1804         GSList *l, *l2;
1805         gint num;
1806
1807         g_return_val_if_fail(dest != NULL, -1);
1808         g_return_val_if_fail(msglist != NULL, -1);
1809
1810         folder = dest->folder;
1811
1812         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1813         g_return_val_if_fail(folder->remove_msg != NULL, -1);
1814
1815         /* 
1816          * Copy messages to destination folder and 
1817          * store new message numbers in newmsgnums
1818          */
1819         item = NULL;
1820         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
1821                 MsgInfo * msginfo = (MsgInfo *) l->data;
1822
1823                 if (!item && msginfo->folder != NULL)
1824                         item = msginfo->folder;
1825
1826                 num = folder->copy_msg(folder, dest, msginfo);
1827                 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1828         }
1829
1830         /* Read cache for dest folder */
1831         if (!dest->cache) folder_item_read_cache(dest);
1832         
1833         /* 
1834          * Fetch new MsgInfos for new messages in dest folder,
1835          * add them to the msgcache and update folder message counts
1836          */
1837         l2 = newmsgnums;
1838         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1839                 MsgInfo *msginfo = (MsgInfo *) l->data;
1840
1841                 num = GPOINTER_TO_INT(l2->data);
1842
1843                 if (num > 0) {
1844                         MsgInfo *newmsginfo;
1845
1846                         newmsginfo = folder->get_msginfo(folder, dest, num);
1847                         if (newmsginfo) {
1848                                 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1849                                 if (dest->stype == F_OUTBOX ||
1850                                     dest->stype == F_QUEUE  ||
1851                                     dest->stype == F_DRAFT  ||
1852                                     dest->stype == F_TRASH)
1853                                         MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1854                                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
1855                                 msgcache_add_msg(dest->cache, newmsginfo);
1856
1857                                 if (MSG_IS_NEW(newmsginfo->flags))
1858                                         dest->new++;
1859                                 if (MSG_IS_UNREAD(newmsginfo->flags))
1860                                         dest->unread++;
1861                                 if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
1862                                         dest->unreadmarked++;
1863                                 if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
1864                                         procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
1865                                 dest->total++;
1866                                 folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1867
1868                                 procmsg_msginfo_free(newmsginfo);
1869                         }
1870                 }
1871                 l2 = g_slist_next(l2);
1872         }
1873
1874         /*
1875          * Remove source messages from their folders if
1876          * copying was successfull and update folder
1877          * message counts
1878          */
1879         l2 = newmsgnums;
1880         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1881                 MsgInfo *msginfo = (MsgInfo *) l->data;
1882
1883                 num = GPOINTER_TO_INT(l2->data);
1884                 
1885                 if (num > 0) {
1886                         item->folder->remove_msg(item->folder,
1887                                                  msginfo->folder,
1888                                                  msginfo->msgnum);
1889                         if (!item->cache)
1890                                 folder_item_read_cache(item);
1891                         msgcache_remove_msg(item->cache, msginfo->msgnum);
1892
1893                         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1894                                 msginfo->folder->new--;
1895                         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1896                                 msginfo->folder->unread--;
1897                         if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1898                                 msginfo->folder->unreadmarked--;
1899                         msginfo->folder->total--;                       
1900                         folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1901                 }
1902
1903                 l2 = g_slist_next(l2);
1904         }
1905
1906
1907         if (folder->finished_copy)
1908                 folder->finished_copy(folder, dest);
1909
1910         g_slist_free(newmsgnums);
1911         return dest->last_num;
1912 }
1913
1914 /*
1915 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1916 {
1917         Folder *folder;
1918         gint num;
1919
1920         g_return_val_if_fail(dest != NULL, -1);
1921         g_return_val_if_fail(msginfo != NULL, -1);
1922
1923         folder = dest->folder;
1924         if (dest->last_num < 0) folder->scan(folder, dest);
1925
1926         num = folder->copy_msg(folder, dest, msginfo);
1927         if (num > 0) dest->last_num = num;
1928
1929         return num;
1930 }
1931 */
1932
1933 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1934 {
1935         Folder *folder;
1936         gint num;
1937
1938         g_return_val_if_fail(dest != NULL, -1);
1939         g_return_val_if_fail(msginfo != NULL, -1);
1940
1941         folder = dest->folder;
1942
1943         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1944
1945         if (!dest->cache) folder_item_read_cache(dest);
1946         
1947         num = folder->copy_msg(folder, dest, msginfo);
1948         if (num > 0) {
1949                 MsgInfo *newmsginfo;
1950
1951                 if (NULL != (newmsginfo = folder->get_msginfo(folder, dest, num))) {
1952                         newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1953                         if (dest->stype == F_OUTBOX ||
1954                             dest->stype == F_QUEUE  ||
1955                             dest->stype == F_DRAFT  ||
1956                             dest->stype == F_TRASH)
1957                                 MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1958                                                      MSG_NEW|MSG_UNREAD|MSG_DELETED);
1959                         msgcache_add_msg(dest->cache, newmsginfo);
1960
1961                         if (MSG_IS_NEW(newmsginfo->flags))
1962                                 dest->new++;
1963                         if (MSG_IS_UNREAD(newmsginfo->flags))
1964                                 dest->unread++;
1965                         if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
1966                                 dest->unreadmarked++;
1967                         if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
1968                                 procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
1969                         dest->total++;
1970                         folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
1971
1972                         procmsg_msginfo_free(newmsginfo);
1973                 }                       
1974         }
1975
1976         if (folder->finished_copy)
1977                 folder->finished_copy(folder, dest);
1978
1979         return num;
1980 }
1981
1982 /*
1983 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1984 {
1985         Folder *folder;
1986         gint num;
1987
1988         g_return_val_if_fail(dest != NULL, -1);
1989         g_return_val_if_fail(msglist != NULL, -1);
1990
1991         folder = dest->folder;
1992         if (dest->last_num < 0) folder->scan(folder, dest);
1993
1994         num = folder->copy_msgs_with_dest(folder, dest, msglist);
1995         if (num > 0) dest->last_num = num;
1996         else dest->op_count = 0;
1997
1998         return num;
1999 }
2000 */
2001
2002 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
2003 {
2004         Folder *folder;
2005         gint num;
2006         GSList *newmsgnums = NULL;
2007         GSList *l, *l2;
2008
2009         g_return_val_if_fail(dest != NULL, -1);
2010         g_return_val_if_fail(msglist != NULL, -1);
2011
2012         folder = dest->folder;
2013  
2014         g_return_val_if_fail(folder->copy_msg != NULL, -1);
2015
2016         /* 
2017          * Copy messages to destination folder and 
2018          * store new message numbers in newmsgnums
2019          */
2020         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
2021                 MsgInfo * msginfo = (MsgInfo *) l->data;
2022
2023                 num = folder->copy_msg(folder, dest, msginfo);
2024                 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
2025         }
2026
2027         /* Read cache for dest folder */
2028         if (!dest->cache) folder_item_read_cache(dest);
2029
2030         /* 
2031          * Fetch new MsgInfos for new messages in dest folder,
2032          * add them to the msgcache and update folder message counts
2033          */
2034         l2 = newmsgnums;
2035         for (l = msglist; l != NULL; l = g_slist_next(l)) {
2036                 MsgInfo *msginfo = (MsgInfo *) l->data;
2037
2038                 num = GPOINTER_TO_INT(l2->data);
2039
2040                 if (num > 0) {
2041                         MsgInfo *newmsginfo;
2042
2043                         newmsginfo = folder->get_msginfo(folder, dest, num);
2044                         if (newmsginfo) {
2045                                 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
2046                                 if (dest->stype == F_OUTBOX ||
2047                                     dest->stype == F_QUEUE  ||
2048                                     dest->stype == F_DRAFT  ||
2049                                     dest->stype == F_TRASH)
2050                                         MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
2051                                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
2052                                 msgcache_add_msg(dest->cache, newmsginfo);
2053
2054                                 if (MSG_IS_NEW(newmsginfo->flags))
2055                                         dest->new++;
2056                                 if (MSG_IS_UNREAD(newmsginfo->flags))
2057                                         dest->unread++;
2058                                 if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
2059                                         dest->unreadmarked++;
2060                                 if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
2061                                         procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
2062                                 dest->total++;
2063                                 folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2064
2065                                 procmsg_msginfo_free(newmsginfo);
2066                         }
2067                 }
2068                 l2 = g_slist_next(l2);
2069         }
2070         
2071         if (folder->finished_copy)
2072                 folder->finished_copy(folder, dest);
2073
2074         g_slist_free(newmsgnums);
2075         return dest->last_num;
2076 }
2077
2078 gint folder_item_remove_msg(FolderItem *item, gint num)
2079 {
2080         Folder *folder;
2081         gint ret;
2082         MsgInfo *msginfo;
2083
2084         g_return_val_if_fail(item != NULL, -1);
2085
2086         folder = item->folder;
2087         if (!item->cache) folder_item_read_cache(item);
2088
2089         ret = folder->remove_msg(folder, item, num);
2090
2091         msginfo = msgcache_get_msg(item->cache, num);
2092         if (msginfo != NULL) {
2093                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2094                         item->new--;
2095                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2096                         item->unread--;
2097                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2098                         item->unreadmarked--;
2099                 procmsg_msginfo_free(msginfo);
2100                 msgcache_remove_msg(item->cache, num);
2101         }
2102         item->total--;
2103         folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2104
2105         return ret;
2106 }
2107
2108 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
2109 {
2110         Folder *folder;
2111         gint ret = 0;
2112
2113         g_return_val_if_fail(item != NULL, -1);
2114         folder = item->folder;
2115         g_return_val_if_fail(folder != NULL, -1);
2116
2117         if (!item->cache) folder_item_read_cache(item);
2118
2119         if (folder->remove_msgs) {
2120                 ret = folder->remove_msgs(folder, item, msglist);
2121                 if (ret == 0)
2122                         folder_item_scan(item);
2123                 return ret;
2124         }
2125
2126         while (msglist != NULL) {
2127                 MsgInfo *msginfo = (MsgInfo *)msglist->data;
2128
2129                 ret = folder_item_remove_msg(item, msginfo->msgnum);
2130                 if (ret != 0) break;
2131                 msgcache_remove_msg(item->cache, msginfo->msgnum);
2132                 msglist = msglist->next;
2133         }
2134
2135         return ret;
2136 }
2137
2138 gint folder_item_remove_all_msg(FolderItem *item)
2139 {
2140         Folder *folder;
2141         gint result;
2142
2143         g_return_val_if_fail(item != NULL, -1);
2144
2145         folder = item->folder;
2146
2147         g_return_val_if_fail(folder->remove_all_msg != NULL, -1);
2148
2149         result = folder->remove_all_msg(folder, item);
2150
2151         if (result == 0) {
2152                 if (folder->finished_remove)
2153                         folder->finished_remove(folder, item);
2154
2155                 folder_item_free_cache(item);
2156                 item->cache = msgcache_new();
2157
2158                 item->new = 0;
2159                 item->unread = 0;
2160                 item->unreadmarked = 0;
2161                 item->total = 0;
2162                 folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
2163         }
2164
2165         return result;
2166 }
2167
2168 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
2169 {
2170         Folder *folder;
2171
2172         g_return_val_if_fail(item != NULL, FALSE);
2173
2174         folder = item->folder;
2175
2176         g_return_val_if_fail(folder->is_msg_changed != NULL, -1);
2177
2178         return folder->is_msg_changed(folder, item, msginfo);
2179 }
2180
2181 gchar *folder_item_get_cache_file(FolderItem *item)
2182 {
2183         gchar *path;
2184         gchar *file;
2185
2186         g_return_val_if_fail(item != NULL, NULL);
2187         g_return_val_if_fail(item->path != NULL, NULL);
2188
2189         path = folder_item_get_path(item);
2190         g_return_val_if_fail(path != NULL, NULL);
2191         if (!is_dir_exist(path))
2192                 make_dir_hier(path);
2193         file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
2194         g_free(path);
2195
2196         return file;
2197 }
2198
2199 gchar *folder_item_get_mark_file(FolderItem *item)
2200 {
2201         gchar *path;
2202         gchar *file;
2203
2204         g_return_val_if_fail(item != NULL, NULL);
2205         g_return_val_if_fail(item->path != NULL, NULL);
2206
2207         path = folder_item_get_path(item);
2208         g_return_val_if_fail(path != NULL, NULL);
2209         if (!is_dir_exist(path))
2210                 make_dir_hier(path);
2211         file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
2212         g_free(path);
2213
2214         return file;
2215 }
2216
2217 static gboolean folder_build_tree(GNode *node, gpointer data)
2218 {
2219         Folder *folder = FOLDER(data);
2220         FolderItem *item;
2221         XMLNode *xmlnode;
2222         GList *list;
2223         SpecialFolderItemType stype = F_NORMAL;
2224         const gchar *name = NULL;
2225         const gchar *path = NULL;
2226         PrefsAccount *account = NULL;
2227         gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE, 
2228                  threaded = TRUE, apply_sub = FALSE;
2229         gboolean ret_rcpt = FALSE, hidereadmsgs = FALSE,
2230                  thread_collapsed = FALSE; /* CLAWS */
2231         FolderSortKey sort_key = SORT_BY_NONE;
2232         FolderSortType sort_type = SORT_ASCENDING;
2233         gint new = 0, unread = 0, total = 0, unreadmarked = 0;
2234         time_t mtime = 0;
2235
2236         g_return_val_if_fail(node->data != NULL, FALSE);
2237         if (!node->parent) return FALSE;
2238
2239         xmlnode = node->data;
2240         if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
2241                 g_warning("tag name != \"folderitem\"\n");
2242                 return FALSE;
2243         }
2244
2245         list = xmlnode->tag->attr;
2246         for (; list != NULL; list = list->next) {
2247                 XMLAttr *attr = list->data;
2248
2249                 if (!attr || !attr->name || !attr->value) continue;
2250                 if (!strcmp(attr->name, "type")) {
2251                         if (!strcasecmp(attr->value, "normal"))
2252                                 stype = F_NORMAL;
2253                         else if (!strcasecmp(attr->value, "inbox"))
2254                                 stype = F_INBOX;
2255                         else if (!strcasecmp(attr->value, "outbox"))
2256                                 stype = F_OUTBOX;
2257                         else if (!strcasecmp(attr->value, "draft"))
2258                                 stype = F_DRAFT;
2259                         else if (!strcasecmp(attr->value, "queue"))
2260                                 stype = F_QUEUE;
2261                         else if (!strcasecmp(attr->value, "trash"))
2262                                 stype = F_TRASH;
2263                 } else if (!strcmp(attr->name, "name"))
2264                         name = attr->value;
2265                 else if (!strcmp(attr->name, "path"))
2266                         path = attr->value;
2267                 else if (!strcmp(attr->name, "mtime"))
2268                         mtime = strtoul(attr->value, NULL, 10);
2269                 else if (!strcmp(attr->name, "new"))
2270                         new = atoi(attr->value);
2271                 else if (!strcmp(attr->name, "unread"))
2272                         unread = atoi(attr->value);
2273                 else if (!strcmp(attr->name, "unreadmarked"))
2274                         unreadmarked = atoi(attr->value);
2275                 else if (!strcmp(attr->name, "total"))
2276                         total = atoi(attr->value);
2277                 else if (!strcmp(attr->name, "no_sub"))
2278                         no_sub = *attr->value == '1' ? TRUE : FALSE;
2279                 else if (!strcmp(attr->name, "no_select"))
2280                         no_select = *attr->value == '1' ? TRUE : FALSE;
2281                 else if (!strcmp(attr->name, "collapsed"))
2282                         collapsed = *attr->value == '1' ? TRUE : FALSE;
2283                 else if (!strcmp(attr->name, "thread_collapsed"))
2284                         thread_collapsed =  *attr->value == '1' ? TRUE : FALSE;
2285                 else if (!strcmp(attr->name, "threaded"))
2286                         threaded =  *attr->value == '1' ? TRUE : FALSE;
2287                 else if (!strcmp(attr->name, "hidereadmsgs"))
2288                         hidereadmsgs =  *attr->value == '1' ? TRUE : FALSE;
2289                 else if (!strcmp(attr->name, "reqretrcpt"))
2290                         ret_rcpt =  *attr->value == '1' ? TRUE : FALSE;
2291                 else if (!strcmp(attr->name, "sort_key")) {
2292                         if (!strcmp(attr->value, "none"))
2293                                 sort_key = SORT_BY_NONE;
2294                         else if (!strcmp(attr->value, "number"))
2295                                 sort_key = SORT_BY_NUMBER;
2296                         else if (!strcmp(attr->value, "size"))
2297                                 sort_key = SORT_BY_SIZE;
2298                         else if (!strcmp(attr->value, "date"))
2299                                 sort_key = SORT_BY_DATE;
2300                         else if (!strcmp(attr->value, "from"))
2301                                 sort_key = SORT_BY_FROM;
2302                         else if (!strcmp(attr->value, "subject"))
2303                                 sort_key = SORT_BY_SUBJECT;
2304                         else if (!strcmp(attr->value, "score"))
2305                                 sort_key = SORT_BY_SCORE;
2306                         else if (!strcmp(attr->value, "label"))
2307                                 sort_key = SORT_BY_LABEL;
2308                         else if (!strcmp(attr->value, "mark"))
2309                                 sort_key = SORT_BY_MARK;
2310                         else if (!strcmp(attr->value, "unread"))
2311                                 sort_key = SORT_BY_UNREAD;
2312                         else if (!strcmp(attr->value, "mime"))
2313                                 sort_key = SORT_BY_MIME;
2314                         else if (!strcmp(attr->value, "to"))
2315                                 sort_key = SORT_BY_TO;
2316                         else if (!strcmp(attr->value, "locked"))
2317                                 sort_key = SORT_BY_LOCKED;
2318                 } else if (!strcmp(attr->name, "sort_type")) {
2319                         if (!strcmp(attr->value, "ascending"))
2320                                 sort_type = SORT_ASCENDING;
2321                         else
2322                                 sort_type = SORT_DESCENDING;
2323                 } else if (!strcmp(attr->name, "account_id")) {
2324                         account = account_find_from_id(atoi(attr->value));
2325                         if (!account) g_warning("account_id: %s not found\n",
2326                                                 attr->value);
2327                 } else if (!strcmp(attr->name, "apply_sub"))
2328                         apply_sub = *attr->value == '1' ? TRUE : FALSE;
2329         }
2330
2331         item = folder_item_new(folder, name, path);
2332         item->stype = stype;
2333         item->mtime = mtime;
2334         item->new = new;
2335         item->unread = unread;
2336         item->unreadmarked = unreadmarked;
2337         item->total = total;
2338         item->no_sub = no_sub;
2339         item->no_select = no_select;
2340         item->collapsed = collapsed;
2341         item->thread_collapsed = thread_collapsed;
2342         item->threaded  = threaded;
2343         item->hide_read_msgs  = hidereadmsgs;
2344         item->ret_rcpt  = ret_rcpt;
2345         item->sort_key  = sort_key;
2346         item->sort_type = sort_type;
2347         item->parent = FOLDER_ITEM(node->parent->data);
2348         item->folder = folder;
2349         switch (stype) {
2350         case F_INBOX:  folder->inbox  = item; break;
2351         case F_OUTBOX: folder->outbox = item; break;
2352         case F_DRAFT:  folder->draft  = item; break;
2353         case F_QUEUE:  folder->queue  = item; break;
2354         case F_TRASH:  folder->trash  = item; break;
2355         default:       break;
2356         }
2357         item->account = account;
2358         item->apply_sub = apply_sub;
2359         prefs_folder_item_read_config(item);
2360
2361         node->data = item;
2362         xml_free_node(xmlnode);
2363
2364         return FALSE;
2365 }
2366
2367 static gboolean folder_read_folder_func(GNode *node, gpointer data)
2368 {
2369         Folder *folder;
2370         XMLNode *xmlnode;
2371         GList *list;
2372         FolderType type = F_UNKNOWN;
2373         const gchar *name = NULL;
2374         const gchar *path = NULL;
2375         PrefsAccount *account = NULL;
2376         gboolean collapsed = FALSE, threaded = TRUE, apply_sub = FALSE;
2377         gboolean ret_rcpt = FALSE, thread_collapsed = FALSE; /* CLAWS */
2378
2379         if (g_node_depth(node) != 2) return FALSE;
2380         g_return_val_if_fail(node->data != NULL, FALSE);
2381
2382         xmlnode = node->data;
2383         if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
2384                 g_warning("tag name != \"folder\"\n");
2385                 return TRUE;
2386         }
2387         g_node_unlink(node);
2388         list = xmlnode->tag->attr;
2389         for (; list != NULL; list = list->next) {
2390                 XMLAttr *attr = list->data;
2391
2392                 if (!attr || !attr->name || !attr->value) continue;
2393                 if (!strcmp(attr->name, "type")) {
2394                         if (!strcasecmp(attr->value, "mh"))
2395                                 type = F_MH;
2396                         else if (!strcasecmp(attr->value, "mbox"))
2397                                 type = F_MBOX;
2398                         else if (!strcasecmp(attr->value, "maildir"))
2399                                 type = F_MAILDIR;
2400                         else if (!strcasecmp(attr->value, "imap"))
2401                                 type = F_IMAP;
2402                         else if (!strcasecmp(attr->value, "news"))
2403                                 type = F_NEWS;
2404                 } else if (!strcmp(attr->name, "name"))
2405                         name = attr->value;
2406                 else if (!strcmp(attr->name, "path"))
2407                         path = attr->value;
2408                 else if (!strcmp(attr->name, "collapsed"))
2409                         collapsed = *attr->value == '1' ? TRUE : FALSE;
2410                 else if (!strcmp(attr->name, "thread_collapsed"))
2411                         thread_collapsed = *attr->value == '1' ? TRUE : FALSE;
2412                 else if (!strcmp(attr->name, "threaded"))
2413                         threaded = *attr->value == '1' ? TRUE : FALSE;
2414                 else if (!strcmp(attr->name, "account_id")) {
2415                         account = account_find_from_id(atoi(attr->value));
2416                         if (!account) g_warning("account_id: %s not found\n",
2417                                                 attr->value);
2418                 } else if (!strcmp(attr->name, "apply_sub"))
2419                         apply_sub = *attr->value == '1' ? TRUE : FALSE;
2420                 else if (!strcmp(attr->name, "reqretrcpt"))
2421                         ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
2422         }
2423
2424         folder = folder_new(type, name, path);
2425         g_return_val_if_fail(folder != NULL, FALSE);
2426         folder->account = account;
2427         if (account && (type == F_IMAP || type == F_NEWS))
2428                 account->folder = REMOTE_FOLDER(folder);
2429         node->data = folder->node->data;
2430         g_node_destroy(folder->node);
2431         folder->node = node;
2432         folder_add(folder);
2433         FOLDER_ITEM(node->data)->collapsed = collapsed;
2434         FOLDER_ITEM(node->data)->thread_collapsed = thread_collapsed;
2435         FOLDER_ITEM(node->data)->threaded  = threaded;
2436         FOLDER_ITEM(node->data)->account   = account;
2437         FOLDER_ITEM(node->data)->apply_sub = apply_sub;
2438         FOLDER_ITEM(node->data)->ret_rcpt  = ret_rcpt;
2439
2440         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2441                         folder_build_tree, folder);
2442
2443         return FALSE;
2444 }
2445
2446 static gchar *folder_get_list_path(void)
2447 {
2448         static gchar *filename = NULL;
2449
2450         if (!filename)
2451                 filename =  g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2452                                         FOLDER_LIST, NULL);
2453
2454         return filename;
2455 }
2456
2457 #define PUT_ESCAPE_STR(fp, attr, str)                   \
2458 {                                                       \
2459         fputs(" " attr "=\"", fp);                      \
2460         xml_file_put_escape_str(fp, str);               \
2461         fputs("\"", fp);                                \
2462 }
2463
2464 static void folder_write_list_recursive(GNode *node, gpointer data)
2465 {
2466         FILE *fp = (FILE *)data;
2467         FolderItem *item;
2468         gint i, depth;
2469         static gchar *folder_type_str[] = {"mh", "mbox", "maildir", "imap",
2470                                            "news", "unknown"};
2471         static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
2472                                                  "draft", "queue", "trash"};
2473         static gchar *sort_key_str[] = {"none", "number", "size", "date",
2474                                         "from", "subject", "score", "label",
2475                                         "mark", "unread", "mime", "to", 
2476                                         "locked"};
2477         g_return_if_fail(node != NULL);
2478         g_return_if_fail(fp != NULL);
2479
2480         item = FOLDER_ITEM(node->data);
2481         g_return_if_fail(item != NULL);
2482
2483         depth = g_node_depth(node);
2484         for (i = 0; i < depth; i++)
2485                 fputs("    ", fp);
2486         if (depth == 1) {
2487                 Folder *folder = item->folder;
2488
2489                 fprintf(fp, "<folder type=\"%s\"", folder_type_str[folder->type]);
2490                 if (folder->name)
2491                         PUT_ESCAPE_STR(fp, "name", folder->name);
2492                 if (folder->type == F_MH || folder->type == F_MBOX)
2493                         PUT_ESCAPE_STR(fp, "path",
2494                                        LOCAL_FOLDER(folder)->rootpath);
2495                 if (item->collapsed && node->children)
2496                         fputs(" collapsed=\"1\"", fp);
2497                 if (folder->account)
2498                         fprintf(fp, " account_id=\"%d\"",
2499                                 folder->account->account_id);
2500                 if (item->apply_sub)
2501                         fputs(" apply_sub=\"1\"", fp);
2502                 if (item->ret_rcpt) 
2503                         fputs(" reqretrcpt=\"1\"", fp);
2504         } else {
2505                 fprintf(fp, "<folderitem type=\"%s\"",
2506                         folder_item_stype_str[item->stype]);
2507                 if (item->name)
2508                         PUT_ESCAPE_STR(fp, "name", item->name);
2509                 if (item->path)
2510                         PUT_ESCAPE_STR(fp, "path", item->path);
2511
2512                 if (item->no_sub)
2513                         fputs(" no_sub=\"1\"", fp);
2514                 if (item->no_select)
2515                         fputs(" no_select=\"1\"", fp);
2516                 if (item->collapsed && node->children)
2517                         fputs(" collapsed=\"1\"", fp);
2518                 else
2519                         fputs(" collapsed=\"0\"", fp);
2520                 if (item->thread_collapsed)
2521                         fputs(" thread_collapsed=\"1\"", fp);
2522                 else
2523                         fputs(" thread_collapsed=\"0\"", fp);
2524                 if (item->threaded)
2525                         fputs(" threaded=\"1\"", fp);
2526                 else
2527                         fputs(" threaded=\"0\"", fp);
2528                 if (item->hide_read_msgs)
2529                         fputs(" hidereadmsgs=\"1\"", fp);
2530                 else
2531                         fputs(" hidereadmsgs=\"0\"", fp);
2532                 if (item->ret_rcpt)
2533                         fputs(" reqretrcpt=\"1\"", fp);
2534
2535                 if (item->sort_key != SORT_BY_NONE) {
2536                         fprintf(fp, " sort_key=\"%s\"",
2537                                 sort_key_str[item->sort_key]);
2538                         if (item->sort_type == SORT_ASCENDING)
2539                                 fprintf(fp, " sort_type=\"ascending\"");
2540                         else
2541                                 fprintf(fp, " sort_type=\"descending\"");
2542                 }
2543
2544                 fprintf(fp,
2545                         " mtime=\"%lu\" new=\"%d\" unread=\"%d\" unreadmarked=\"%d\" total=\"%d\"",
2546                         item->mtime, item->new, item->unread, item->unreadmarked, item->total);
2547
2548                 if (item->account)
2549                         fprintf(fp, " account_id=\"%d\"",
2550                                 item->account->account_id);
2551                 if (item->apply_sub)
2552                         fputs(" apply_sub=\"1\"", fp);
2553         }
2554
2555         if (node->children) {
2556                 GNode *child;
2557                 fputs(">\n", fp);
2558
2559                 child = node->children;
2560                 while (child) {
2561                         GNode *cur;
2562
2563                         cur = child;
2564                         child = cur->next;
2565                         folder_write_list_recursive(cur, data);
2566                 }
2567
2568                 for (i = 0; i < depth; i++)
2569                         fputs("    ", fp);
2570                 fprintf(fp, "</%s>\n", depth == 1 ? "folder" : "folderitem");
2571         } else
2572                 fputs(" />\n", fp);
2573 }
2574
2575 static void folder_update_op_count_rec(GNode *node)
2576 {
2577         FolderItem *fitem = FOLDER_ITEM(node->data);
2578
2579         if (g_node_depth(node) > 0) {
2580                 if (fitem->op_count > 0) {
2581                         fitem->op_count = 0;
2582                         folder_item_update(fitem, F_ITEM_UPDATE_MSGCNT);
2583                 }
2584                 if (node->children) {
2585                         GNode *child;
2586
2587                         child = node->children;
2588                         while (child) {
2589                                 GNode *cur;
2590
2591                                 cur = child;
2592                                 child = cur->next;
2593                                 folder_update_op_count_rec(cur);
2594                         }
2595                 }
2596         }
2597 }
2598
2599 void folder_update_op_count() {
2600         GList *cur;
2601         Folder *folder;
2602
2603         for (cur = folder_list; cur != NULL; cur = cur->next) {
2604                 folder = cur->data;
2605                 folder_update_op_count_rec(folder->node);
2606         }
2607 }
2608
2609 typedef struct _type_str {
2610         gchar * str;
2611         gint type;
2612 } type_str;
2613
2614
2615 /*
2616 static gchar * folder_item_get_tree_identifier(FolderItem * item)
2617 {
2618         if (item->parent != NULL) {
2619                 gchar * path;
2620                 gchar * id;
2621
2622                 path = folder_item_get_tree_identifier(item->parent);
2623                 if (path == NULL)
2624                         return NULL;
2625
2626                 id = g_strconcat(path, "/", item->name, NULL);
2627                 g_free(path);
2628
2629                 return id;
2630         }
2631         else {
2632                 return g_strconcat("/", item->name, NULL);
2633         }
2634 }
2635 */
2636
2637 /* CLAWS: temporary local folder for filtering */
2638 static Folder *processing_folder;
2639 static FolderItem *processing_folder_item;
2640
2641 static void folder_create_processing_folder(void)
2642 {
2643 #define PROCESSING_FOLDER ".processing" 
2644         Folder     *tmpparent;
2645         gchar      *tmpname;
2646
2647         tmpparent = folder_get_default_folder();
2648         g_assert(tmpparent);
2649         debug_print("tmpparentroot %s\n", LOCAL_FOLDER(tmpparent)->rootpath);
2650         if (LOCAL_FOLDER(tmpparent)->rootpath[0] == '/')
2651                 tmpname = g_strconcat(LOCAL_FOLDER(tmpparent)->rootpath,
2652                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2653                                       NULL);
2654         else
2655                 tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2656                                       LOCAL_FOLDER(tmpparent)->rootpath,
2657                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2658                                       NULL);
2659
2660         processing_folder = folder_new(F_MH, "PROCESSING", LOCAL_FOLDER(tmpparent)->rootpath);
2661         g_assert(processing_folder);
2662
2663         if (!is_dir_exist(tmpname)) {
2664                 debug_print("*TMP* creating %s\n", tmpname);
2665                 processing_folder_item = processing_folder->create_folder(processing_folder,
2666                                                                           processing_folder->node->data,
2667                                                                           PROCESSING_FOLDER);
2668                 g_assert(processing_folder_item);                                                                         
2669         }
2670         else {
2671                 debug_print("*TMP* already created\n");
2672                 processing_folder_item = folder_item_new(processing_folder, ".processing", ".processing");
2673                 g_assert(processing_folder_item);
2674                 folder_item_append(processing_folder->node->data, processing_folder_item);
2675         }
2676         g_free(tmpname);
2677 }
2678
2679 FolderItem *folder_get_default_processing(void)
2680 {
2681         if (!processing_folder_item) {
2682                 folder_create_processing_folder();
2683         }
2684         return processing_folder_item;
2685 }
2686
2687 /* folder_persist_prefs_new() - return hash table with persistent
2688  * settings (and folder name as key). 
2689  * (note that in claws other options are in the PREFS_FOLDER_ITEM_RC
2690  * file, so those don't need to be included in PersistPref yet) 
2691  */
2692 GHashTable *folder_persist_prefs_new(Folder *folder)
2693 {
2694         GHashTable *pptable;
2695
2696         g_return_val_if_fail(folder, NULL);
2697         pptable = g_hash_table_new(g_str_hash, g_str_equal);
2698         folder_get_persist_prefs_recursive(folder->node, pptable);
2699         return pptable;
2700 }
2701
2702 void folder_persist_prefs_free(GHashTable *pptable)
2703 {
2704         g_return_if_fail(pptable);
2705         g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
2706         g_hash_table_destroy(pptable);
2707 }
2708
2709 const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
2710 {
2711         if (pptable == NULL || name == NULL) return NULL;
2712         return g_hash_table_lookup(pptable, name);
2713 }
2714
2715 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
2716 {
2717         const PersistPrefs *pp;
2718         gchar *id = folder_item_get_identifier(item);
2719
2720         pp = folder_get_persist_prefs(pptable, id); 
2721         g_free(id);
2722
2723         if (!pp) return;
2724
2725         /* CLAWS: since not all folder properties have been migrated to 
2726          * folderlist.xml, we need to call the old stuff first before
2727          * setting things that apply both to Main and Claws. */
2728         prefs_folder_item_read_config(item); 
2729          
2730         item->collapsed = pp->collapsed;
2731         item->thread_collapsed = pp->thread_collapsed;
2732         item->threaded  = pp->threaded;
2733         item->ret_rcpt  = pp->ret_rcpt;
2734         item->hide_read_msgs = pp->hide_read_msgs;
2735         item->sort_key  = pp->sort_key;
2736         item->sort_type = pp->sort_type;
2737 }
2738
2739 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
2740 {
2741         FolderItem *item = FOLDER_ITEM(node->data);
2742         PersistPrefs *pp;
2743         GNode *child, *cur;
2744         gchar *id;
2745
2746         g_return_if_fail(node != NULL);
2747         g_return_if_fail(item != NULL);
2748
2749         /* NOTE: item->path == NULL means top level folder; not interesting
2750          * to store preferences of that one.  */
2751         if (item->path) {
2752                 id = folder_item_get_identifier(item);
2753                 pp = g_new0(PersistPrefs, 1);
2754                 g_return_if_fail(pp != NULL);
2755                 pp->collapsed = item->collapsed;
2756                 pp->thread_collapsed = item->thread_collapsed;
2757                 pp->threaded  = item->threaded;
2758                 pp->ret_rcpt  = item->ret_rcpt; 
2759                 pp->hide_read_msgs = item->hide_read_msgs;
2760                 pp->sort_key  = item->sort_key;
2761                 pp->sort_type = item->sort_type;
2762                 g_hash_table_insert(pptable, id, pp);
2763         }
2764
2765         if (node->children) {
2766                 child = node->children;
2767                 while (child) {
2768                         cur = child;
2769                         child = cur->next;
2770                         folder_get_persist_prefs_recursive(cur, pptable);
2771                 }
2772         }       
2773 }
2774
2775 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
2776 {
2777         if (key) 
2778                 g_free(key);
2779         if (val) 
2780                 g_free(val);
2781         return TRUE;    
2782 }
2783
2784 void folder_item_apply_processing(FolderItem *item)
2785 {
2786         GSList *processing_list;
2787         GSList *mlist, *cur;
2788         
2789         g_return_if_fail(item != NULL);
2790         
2791         processing_list = item->prefs->processing;
2792         if (processing_list == NULL)
2793                 return;
2794
2795         folder_item_update_freeze();
2796
2797         mlist = folder_item_get_msg_list(item);
2798         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2799                 MsgInfo * msginfo;
2800
2801                 msginfo = (MsgInfo *) cur->data;
2802                 filter_message_by_msginfo(processing_list, msginfo);
2803                 procmsg_msginfo_free(msginfo);
2804         }
2805         g_slist_free(mlist);
2806
2807         folder_item_update_thaw();
2808 }
2809
2810 /*
2811  *  functions for handling FolderItem content changes
2812  */
2813 static gint folder_item_update_freeze_cnt = 0;
2814
2815 /**
2816  * Notify the folder system about changes to a folder. If the
2817  * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
2818  * be invoked, otherwise the changes will be remebered until
2819  * the folder system is thawed.
2820  *
2821  * \param item The FolderItem that was changed
2822  * \param update_flags Type of changed that was made
2823  */
2824 void folder_item_update(FolderItem *item, FolderItemUpdateFlags update_flags)
2825 {
2826         if (folder_item_update_freeze_cnt == 0) {
2827                 FolderItemUpdateData source;
2828         
2829                 source.item = item;
2830                 source.update_flags = update_flags;
2831                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
2832         } else {
2833                 item->update_flags |= update_flags;
2834         }
2835 }
2836
2837 void folder_item_update_recursive(FolderItem *item, FolderItemUpdateFlags update_flags)
2838 {
2839         GNode *node = item->folder->node;       
2840
2841         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
2842         node = node->children;
2843
2844         folder_item_update(item, update_flags);
2845         while (node != NULL) {
2846                 if (node && node->data) {
2847                         FolderItem *next_item = (FolderItem*) node->data;
2848
2849                         folder_item_update(next_item, update_flags);
2850                 }
2851                 node = node->next;
2852         }
2853 }
2854
2855 void folder_item_update_freeze()
2856 {
2857         folder_item_update_freeze_cnt++;
2858 }
2859
2860 static void folder_item_update_func(FolderItem *item, gpointer data)
2861 {
2862         FolderItemUpdateData source;
2863     
2864         if (item->update_flags) {
2865                 source.item = item;
2866                 source.update_flags = item->update_flags;
2867                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);                             
2868                 item->update_flags = 0;
2869         }
2870 }
2871
2872 void folder_item_update_thaw()
2873 {
2874         if (folder_item_update_freeze_cnt > 0)
2875                 folder_item_update_freeze_cnt--;
2876         if (folder_item_update_freeze_cnt == 0) {
2877                 /* Update all folders */
2878                 folder_func_to_all_folders(folder_item_update_func, NULL);
2879         }
2880 }
2881
2882 #undef PUT_ESCAPE_STR