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