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