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