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