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