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