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