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