* src/folder.c
[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.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_write_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_update_item(item, FALSE);
993
994 }
995
996 gint folder_item_scan(FolderItem *item)
997 {
998         Folder *folder;
999         GSList *folder_list = NULL, *cache_list = NULL, *folder_list_cur, *cache_list_cur, *new_list = NULL;
1000         guint newcnt = 0, unreadcnt = 0, totalcnt = 0, unreadmarkedcnt = 0;
1001         guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
1002         gboolean contentchange = FALSE;
1003     
1004         g_return_val_if_fail(item != NULL, -1);
1005         if (item->path == NULL) return -1;
1006
1007         folder = item->folder;
1008
1009         g_return_val_if_fail(folder != NULL, -1);
1010         g_return_val_if_fail(folder->get_num_list != NULL, -1);
1011
1012         debug_print("Scanning folder %s for cache changes.\n", item->path);
1013
1014         /* Get list of messages for folder and cache */
1015         if (folder->get_num_list(item->folder, item, &folder_list) < 0) {
1016                 debug_print("Error fetching list of message numbers\n");
1017                 return(-1);
1018         }
1019
1020         if (!folder->check_msgnum_validity || 
1021             folder->check_msgnum_validity(folder, item)) {
1022                 if (!item->cache)
1023                         folder_item_read_cache(item);
1024                 cache_list = msgcache_get_msg_list(item->cache);
1025         } else {
1026                 if (item->cache)
1027                         msgcache_destroy(item->cache);
1028                 item->cache = msgcache_new();
1029                 cache_list = NULL;
1030         }
1031
1032         /* Sort both lists */
1033         cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
1034         folder_list = g_slist_sort(folder_list, folder_sort_folder_list);
1035
1036         cache_list_cur = cache_list;
1037         folder_list_cur = folder_list;
1038
1039         if (cache_list_cur != NULL) {
1040                 GSList *cache_list_last;
1041         
1042                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1043                 cache_list_last = g_slist_last(cache_list);
1044                 cache_max_num = ((MsgInfo *)cache_list_last->data)->msgnum;
1045         } else {
1046                 cache_cur_num = G_MAXINT;
1047                 cache_max_num = 0;
1048         }
1049
1050         if (folder_list_cur != NULL) {
1051                 GSList *folder_list_last;
1052         
1053                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1054                 folder_list_last = g_slist_last(folder_list);
1055                 folder_max_num = GPOINTER_TO_INT(folder_list_last->data);
1056         } else {
1057                 folder_cur_num = G_MAXINT;
1058                 folder_max_num = 0;
1059         }
1060
1061         while ((cache_cur_num != G_MAXINT) || (folder_cur_num != G_MAXINT)) {
1062                 /*
1063                  *  Message only exists in the folder
1064                  *  Remember message for fetching
1065                  */
1066                 if (folder_cur_num < cache_cur_num) {
1067                         gboolean add = FALSE;
1068
1069                         switch(folder->type) {
1070                                 case F_NEWS:
1071                                         if (folder_cur_num < cache_max_num)
1072                                                 break;
1073                                         
1074                                         if (prefs_common.max_articles == 0) {
1075                                                 add = TRUE;
1076                                         }
1077
1078                                         if (folder_max_num <= prefs_common.max_articles) {
1079                                                 add = TRUE;
1080                                         } else if (folder_cur_num > (folder_max_num - prefs_common.max_articles)) {
1081                                                 add = TRUE;
1082                                         }
1083                                         break;
1084                                 default:
1085                                         add = TRUE;
1086                                         break;
1087                         }
1088                         
1089                         if (add) {
1090                                 new_list = g_slist_prepend(new_list, GINT_TO_POINTER(folder_cur_num));
1091                                 debug_print("Remembered message %d for fetching\n", folder_cur_num);
1092                         }
1093
1094                         /* Move to next folder number */
1095                         folder_list_cur = folder_list_cur->next;
1096
1097                         if (folder_list_cur != NULL)
1098                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1099                         else
1100                                 folder_cur_num = G_MAXINT;
1101
1102                         contentchange = TRUE;
1103
1104                         continue;
1105                 }
1106
1107                 /*
1108                  *  Message only exists in the cache
1109                  *  Remove the message from the cache
1110                  */
1111                 if (cache_cur_num < folder_cur_num) {
1112                         msgcache_remove_msg(item->cache, cache_cur_num);
1113                         debug_print("Removed message %d from cache.\n", cache_cur_num);
1114
1115                         /* Move to next cache number */
1116                         cache_list_cur = cache_list_cur->next;
1117
1118                         if (cache_list_cur != NULL)
1119                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1120                         else
1121                                 cache_cur_num = G_MAXINT;
1122
1123                         contentchange = TRUE;
1124
1125                         continue;
1126                 }
1127
1128                 /*
1129                  *  Message number exists in folder and cache!
1130                  *  Check if the message has been modified
1131                  */
1132                 if (cache_cur_num == folder_cur_num) {
1133                         MsgInfo *msginfo;
1134
1135                         msginfo = msgcache_get_msg(item->cache, folder_cur_num);
1136                         if (folder->is_msg_changed && folder->is_msg_changed(folder, item, msginfo)) {
1137                                 MsgInfo *newmsginfo;
1138
1139                                 msgcache_remove_msg(item->cache, msginfo->msgnum);
1140
1141                                 if (NULL != (newmsginfo = folder->get_msginfo(folder, item, folder_cur_num))) {
1142                                         msgcache_add_msg(item->cache, newmsginfo);
1143                                         if (MSG_IS_NEW(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
1144                                                 newcnt++;
1145                                         if (MSG_IS_UNREAD(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
1146                                                 unreadcnt++;
1147                                         if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
1148                                                 unreadmarkedcnt++;
1149                                         procmsg_msginfo_free(newmsginfo);
1150                                 }                                       
1151
1152                                 debug_print("Updated msginfo for message %d.\n", folder_cur_num);
1153                         } else {
1154                                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1155                                         newcnt++;
1156                                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1157                                         unreadcnt++;
1158                                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1159                                         unreadmarkedcnt++;
1160                         }
1161                         totalcnt++;
1162                         procmsg_msginfo_free(msginfo);
1163
1164                         /* Move to next folder and cache number */
1165                         cache_list_cur = cache_list_cur->next;
1166                         folder_list_cur = folder_list_cur->next;
1167
1168                         if (cache_list_cur != NULL)
1169                                 cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
1170                         else
1171                                 cache_cur_num = G_MAXINT;
1172
1173                         if (folder_list_cur != NULL)
1174                                 folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
1175                         else
1176                                 folder_cur_num = G_MAXINT;
1177
1178                         contentchange = TRUE;
1179
1180                         continue;
1181                 }
1182         }
1183
1184         for(cache_list_cur = cache_list; cache_list_cur != NULL; cache_list_cur = g_slist_next(cache_list_cur)) {
1185                 procmsg_msginfo_free((MsgInfo *) cache_list_cur->data);
1186         }
1187
1188         g_slist_free(cache_list);
1189         g_slist_free(folder_list);
1190
1191         if (folder->get_msginfos) {
1192                 GSList *elem;
1193                 GSList *newmsg_list;
1194                 MsgInfo *msginfo;
1195                 
1196                 if (new_list) {
1197                         newmsg_list = folder->get_msginfos(folder, item, new_list);
1198                         for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
1199                                 msginfo = (MsgInfo *) elem->data;
1200                                 msgcache_add_msg(item->cache, msginfo);
1201                                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1202                                         newcnt++;
1203                                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1204                                         unreadcnt++;
1205                                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1206                                         unreadmarkedcnt++;
1207                                 totalcnt++;
1208                                 procmsg_msginfo_free(msginfo);
1209                         }
1210                         g_slist_free(newmsg_list);
1211                 }
1212         } else if (folder->get_msginfo) {
1213                 GSList *elem;
1214         
1215                 for (elem = new_list; elem != NULL; elem = g_slist_next(elem)) {
1216                         MsgInfo *msginfo;
1217                         guint num;
1218
1219                         num = GPOINTER_TO_INT(elem->data);
1220                         msginfo = folder->get_msginfo(folder, item, num);
1221                         if (msginfo != NULL) {
1222                                 msgcache_add_msg(item->cache, msginfo);
1223                                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1224                                     newcnt++;
1225                                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1226                                     unreadcnt++;
1227                                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1228                                         unreadmarkedcnt++;
1229                                 totalcnt++;
1230                                 procmsg_msginfo_free(msginfo);
1231                                 debug_print("Added newly found message %d to cache.\n", num);
1232                         }
1233                 }
1234         }
1235
1236         item->new = newcnt;
1237         item->unread = unreadcnt;
1238         item->total = totalcnt;
1239         item->unreadmarked = unreadmarkedcnt;
1240         g_slist_free(new_list);
1241
1242         folder_update_item(item, contentchange);
1243
1244         return 0;
1245 }
1246
1247 static void folder_item_scan_foreach_func(gpointer key, gpointer val,
1248                                           gpointer data)
1249 {
1250         folder_item_scan(FOLDER_ITEM(key));
1251 }
1252
1253 void folder_item_scan_foreach(GHashTable *table)
1254 {
1255         g_hash_table_foreach(table, folder_item_scan_foreach_func, NULL);
1256 }
1257
1258 void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
1259 {
1260         gint *memusage = (gint *)data;
1261
1262         if (item->cache == NULL)
1263                 return;
1264         
1265         *memusage += msgcache_get_memory_usage(item->cache);
1266 }
1267
1268 gint folder_cache_time_compare_func(gconstpointer a, gconstpointer b)
1269 {
1270         FolderItem *fa = (FolderItem *)a;
1271         FolderItem *fb = (FolderItem *)b;
1272         
1273         return (gint) (msgcache_get_last_access_time(fa->cache) - msgcache_get_last_access_time(fb->cache));
1274 }
1275
1276 void folder_find_expired_caches(FolderItem *item, gpointer data)
1277 {
1278         GSList **folder_item_list = (GSList **)data;
1279         gint difftime, expiretime;
1280         
1281         if (item->cache == NULL)
1282                 return;
1283
1284         if (item->opened > 0)
1285                 return;
1286
1287         difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
1288         expiretime = prefs_common.cache_min_keep_time * 60;
1289         debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
1290         if (difftime > expiretime) {
1291                 *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
1292         }
1293 }
1294
1295 void folder_item_free_cache(FolderItem *item)
1296 {
1297         g_return_if_fail(item != NULL);
1298         
1299         if (item->cache == NULL)
1300                 return;
1301         
1302         if (item->opened > 0)
1303                 return;
1304
1305         folder_item_write_cache(item);
1306         msgcache_destroy(item->cache);
1307         item->cache = NULL;
1308 }
1309
1310 void folder_clean_cache_memory()
1311 {
1312         gint memusage = 0;
1313
1314         folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);       
1315         debug_print("Total cache memory usage: %d\n", memusage);
1316         
1317         if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
1318                 GSList *folder_item_list = NULL, *listitem;
1319                 
1320                 debug_print("Trying to free cache memory\n");
1321
1322                 folder_func_to_all_folders(folder_find_expired_caches, &folder_item_list);      
1323                 listitem = folder_item_list;
1324                 while((listitem != NULL) && (memusage > (prefs_common.cache_max_mem_usage * 1024))) {
1325                         FolderItem *item = (FolderItem *)(listitem->data);
1326
1327                         debug_print("Freeing cache memory for %s\n", item->path);
1328                         memusage -= msgcache_get_memory_usage(item->cache);
1329                         folder_item_free_cache(item);
1330                         listitem = listitem->next;
1331                 }
1332                 g_slist_free(folder_item_list);
1333         }
1334 }
1335
1336 void folder_item_read_cache(FolderItem *item)
1337 {
1338         gchar *cache_file, *mark_file;
1339         
1340         g_return_if_fail(item != NULL);
1341
1342         cache_file = folder_item_get_cache_file(item);
1343         mark_file = folder_item_get_mark_file(item);
1344         item->cache = msgcache_read_cache(item, cache_file);
1345         if (!item->cache) {
1346                 item->cache = msgcache_new();
1347                 folder_item_scan(item);
1348         }
1349         msgcache_read_mark(item->cache, mark_file);
1350         g_free(cache_file);
1351         g_free(mark_file);
1352
1353         folder_clean_cache_memory();
1354 }
1355
1356 void folder_item_write_cache(FolderItem *item)
1357 {
1358         gchar *cache_file, *mark_file;
1359         PrefsFolderItem *prefs;
1360         gint filemode = 0;
1361         gchar *id;
1362         
1363         if (!item || !item->path || !item->cache)
1364                 return;
1365
1366         id = folder_item_get_identifier(item);
1367         debug_print("Save cache for folder %s\n", id);
1368         g_free(id);
1369
1370         cache_file = folder_item_get_cache_file(item);
1371         mark_file = folder_item_get_mark_file(item);
1372         if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
1373                 prefs = item->prefs;
1374                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
1375                         /* for cache file */
1376                         filemode = prefs->folder_chmod;
1377                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
1378                         if (filemode & S_IROTH) filemode |= S_IWOTH;
1379                         chmod(cache_file, filemode);
1380                 }
1381         }
1382
1383         g_free(cache_file);
1384         g_free(mark_file);
1385 }
1386
1387 MsgInfo *folder_item_get_msginfo(FolderItem *item, gint num)
1388 {
1389         Folder *folder;
1390         MsgInfo *msginfo;
1391         
1392         g_return_val_if_fail(item != NULL, NULL);
1393         
1394         folder = item->folder;
1395         if (!item->cache)
1396                 folder_item_read_cache(item);
1397         
1398         if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
1399                 return msginfo;
1400         
1401         g_return_val_if_fail(folder->get_msginfo, NULL);
1402         if ((msginfo = folder->get_msginfo(folder, item, num)) != NULL) {
1403                 msgcache_add_msg(item->cache, msginfo);
1404                 return msginfo;
1405         }
1406         
1407         return NULL;
1408 }
1409
1410 MsgInfo *folder_item_get_msginfo_by_msgid(FolderItem *item, const gchar *msgid)
1411 {
1412         Folder *folder;
1413         MsgInfo *msginfo;
1414         
1415         g_return_val_if_fail(item != NULL, NULL);
1416         
1417         folder = item->folder;
1418         if (!item->cache)
1419                 folder_item_read_cache(item);
1420         
1421         if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
1422                 return msginfo;
1423
1424         return NULL;
1425 }
1426
1427 GSList *folder_item_get_msg_list(FolderItem *item)
1428 {
1429         g_return_val_if_fail(item != NULL, NULL);
1430         
1431         if (item->cache == 0)
1432                 folder_item_read_cache(item);
1433
1434         g_return_val_if_fail(item->cache != NULL, NULL);
1435         
1436         return msgcache_get_msg_list(item->cache);
1437 }
1438
1439 gchar *folder_item_fetch_msg(FolderItem *item, gint num)
1440 {
1441         Folder *folder;
1442
1443         g_return_val_if_fail(item != NULL, NULL);
1444
1445         folder = item->folder;
1446
1447         g_return_val_if_fail(folder->fetch_msg != NULL, NULL);
1448
1449         return folder->fetch_msg(folder, item, num);
1450 }
1451
1452 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
1453                          gboolean remove_source)
1454 {
1455         Folder *folder;
1456         gint num;
1457         MsgInfo *msginfo;
1458
1459         g_return_val_if_fail(dest != NULL, -1);
1460         g_return_val_if_fail(file != NULL, -1);
1461
1462         folder = dest->folder;
1463
1464         g_return_val_if_fail(folder->add_msg != NULL, -1);
1465
1466         if (!dest->cache)
1467                 folder_item_read_cache(dest);
1468
1469         num = folder->add_msg(folder, dest, file, remove_source);
1470
1471         if (num > 0) {
1472                 msginfo = folder->get_msginfo(folder, dest, num);
1473
1474                 if (msginfo != NULL) {
1475                         if (MSG_IS_NEW(msginfo->flags))
1476                                 dest->new++;
1477                         if (MSG_IS_UNREAD(msginfo->flags))
1478                                 dest->unread++;
1479                         if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1480                                 dest->unreadmarked++;
1481                         dest->total++;
1482                         dest->need_update = TRUE;
1483
1484                         msgcache_add_msg(dest->cache, msginfo);
1485
1486                         procmsg_msginfo_free(msginfo);
1487                 }
1488
1489                 dest->last_num = num;
1490         }
1491
1492         return num;
1493 }
1494
1495 /*
1496 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1497 {
1498         Folder *folder;
1499         gint num;
1500
1501         g_return_val_if_fail(dest != NULL, -1);
1502         g_return_val_if_fail(msginfo != NULL, -1);
1503
1504         folder = dest->folder;
1505         if (dest->last_num < 0) folder->scan(folder, dest);
1506
1507         num = folder->move_msg(folder, dest, msginfo);
1508         if (num > 0) dest->last_num = num;
1509
1510         return num;
1511 }
1512 */
1513                 
1514 FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest) 
1515 {
1516         GSList *mlist;
1517         GSList *cur;
1518         FolderItem *new_item;
1519         FolderItem *next_item;
1520         GNode *srcnode;
1521         int cnt = 0;
1522         gchar *old_id, *new_id;
1523
1524         mlist = folder_item_get_msg_list(src);
1525
1526         /* move messages */
1527         debug_print("Moving %s to %s\n", src->path, dest->path);
1528         new_item = folder_create_folder(dest, g_basename(src->path));
1529         if (new_item == NULL) {
1530                 printf("Can't create folder\n");
1531                 return NULL;
1532         }
1533         
1534         if (new_item->folder == NULL)
1535                 new_item->folder = dest->folder;
1536
1537         /* move messages */
1538         for (cur = mlist ; cur != NULL ; cur = cur->next) {
1539                 MsgInfo * msginfo;
1540                 cnt++;
1541                 if (cnt%500)
1542                         log_message(_("Moving %s to %s (%d%%)...\n"), src->name, 
1543                                         new_item->path,
1544                                         100*cnt/g_slist_length(mlist));
1545                 msginfo = (MsgInfo *) cur->data;
1546                 folder_item_move_msg(new_item, msginfo);
1547
1548                 procmsg_msginfo_free(msginfo);
1549         }
1550         
1551         /*copy prefs*/
1552         prefs_folder_item_copy_prefs(src, new_item);
1553         new_item->collapsed = src->collapsed;
1554         new_item->threaded  = src->threaded;
1555         new_item->ret_rcpt  = src->ret_rcpt;
1556         new_item->hide_read_msgs = src->hide_read_msgs;
1557         new_item->sort_key  = src->sort_key;
1558         new_item->sort_type = src->sort_type;
1559
1560         prefs_matcher_write_config();
1561         
1562         /* recurse */
1563         srcnode = src->folder->node;    
1564         srcnode = g_node_find(srcnode, G_PRE_ORDER, G_TRAVERSE_ALL, src);
1565         srcnode = srcnode->children;
1566         while (srcnode != NULL) {
1567                 if (srcnode && srcnode->data) {
1568                         next_item = (FolderItem*) srcnode->data;
1569                         srcnode = srcnode->next;
1570                         if (folder_item_move_recursive(next_item, new_item) == NULL)
1571                                 return NULL;
1572                 }
1573         }
1574         old_id = folder_item_get_identifier(src);
1575         new_id = folder_item_get_identifier(new_item);
1576         debug_print("updating rules : %s => %s\n", old_id, new_id);
1577         
1578         src->folder->remove_folder(src->folder, src);
1579         folder_write_list();
1580
1581         if (old_id != NULL && new_id != NULL)
1582                 prefs_filtering_rename_path(old_id, new_id);
1583         g_free(old_id);
1584         g_free(new_id);
1585
1586         return new_item;
1587 }
1588
1589 gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item)
1590 {
1591         FolderItem *tmp = dest->parent;
1592         gchar * src_identifier, * dst_identifier;
1593         gchar * phys_srcpath, * phys_dstpath;
1594         GNode *src_node;
1595         
1596         while (tmp) {
1597                 if (tmp == src) {
1598                         return F_MOVE_FAILED_DEST_IS_CHILD;
1599                 }
1600                 tmp = tmp->parent;
1601         }
1602         
1603         tmp = src->parent;
1604         
1605         src_identifier = folder_item_get_identifier(src);
1606         dst_identifier = folder_item_get_identifier(dest);
1607         
1608         if(dst_identifier == NULL && dest->folder && dest->parent == NULL) {
1609                 /* dest can be a root folder */
1610                 dst_identifier = folder_get_identifier(dest->folder);
1611         }
1612         if (src_identifier == NULL || dst_identifier == NULL) {
1613                 printf("Can't get identifiers\n");
1614                 return F_MOVE_FAILED;
1615         }
1616
1617         if (src->folder != dest->folder) {
1618                 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
1619         }
1620
1621         phys_srcpath = folder_item_get_path(src);
1622         phys_dstpath = g_strconcat(folder_item_get_path(dest),G_DIR_SEPARATOR_S,g_basename(phys_srcpath),NULL);
1623
1624         if (src->parent == dest || src == dest) {
1625                 g_free(src_identifier);
1626                 g_free(dst_identifier);
1627                 g_free(phys_srcpath);
1628                 g_free(phys_dstpath);
1629                 return F_MOVE_FAILED_DEST_IS_PARENT;
1630         }
1631         debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
1632         if ((tmp = folder_item_move_recursive(src, dest)) == NULL) {
1633                 return F_MOVE_FAILED;
1634         }
1635         
1636         /* update rules */
1637         src_node = g_node_find(src->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, src);
1638         if (src_node) 
1639                 g_node_destroy(src_node);
1640         else
1641                 debug_print("can't remove node: it's null!\n");
1642         /* not to much worry if remove fails, move has been done */
1643         
1644         g_free(src_identifier);
1645         g_free(dst_identifier);
1646         g_free(phys_srcpath);
1647         g_free(phys_dstpath);
1648
1649         *new_item = tmp;
1650
1651         return F_MOVE_OK;
1652 }
1653
1654 gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
1655 {
1656         Folder *folder;
1657         gint num;
1658         Folder *src_folder;
1659
1660         g_return_val_if_fail(dest != NULL, -1);
1661         g_return_val_if_fail(msginfo != NULL, -1);
1662
1663         folder = dest->folder;
1664
1665         g_return_val_if_fail(folder->remove_msg != NULL, -1);
1666         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1667
1668         if (!dest->cache) folder_item_read_cache(dest);
1669
1670         src_folder = msginfo->folder->folder;
1671
1672         num = folder->copy_msg(folder, dest, msginfo);
1673         
1674         if (num != -1) {
1675                 MsgInfo *newmsginfo;
1676     
1677                 /* Add new msginfo to dest folder */
1678                 if (NULL != (newmsginfo = folder->get_msginfo(folder, dest, num))) {
1679                         newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1680                         
1681                         if (dest->stype == F_OUTBOX || dest->stype == F_QUEUE  ||
1682                             dest->stype == F_DRAFT  || dest->stype == F_TRASH)
1683                                 MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1684                                                      MSG_NEW|MSG_UNREAD|MSG_DELETED);
1685                         msgcache_add_msg(dest->cache, newmsginfo);
1686
1687                         if (MSG_IS_NEW(newmsginfo->flags))
1688                                 dest->new++;
1689                         if (MSG_IS_UNREAD(newmsginfo->flags))
1690                                 dest->unread++;
1691                         if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
1692                                 dest->unreadmarked++;
1693                         dest->total++;
1694                         dest->need_update = TRUE;
1695
1696                         procmsg_msginfo_free(newmsginfo);
1697                 }
1698
1699                 /* remove source message from it's folder */
1700                 if (src_folder->remove_msg) {
1701                         src_folder->remove_msg(src_folder, msginfo->folder,
1702                                                msginfo->msgnum);
1703                         msgcache_remove_msg(msginfo->folder->cache, msginfo->msgnum);
1704
1705                         if (MSG_IS_NEW(msginfo->flags))
1706                                 msginfo->folder->new--;
1707                         if (MSG_IS_UNREAD(msginfo->flags))
1708                                 msginfo->folder->unread--;
1709                         if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1710                                 msginfo->folder->unreadmarked--;
1711                         msginfo->folder->total--;
1712                         msginfo->folder->need_update = TRUE;
1713                 }
1714         }
1715         
1716         if (folder->finished_copy)
1717                 folder->finished_copy(folder, dest);
1718
1719         return num;
1720 }
1721
1722 /*
1723 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1724 {
1725         Folder *folder;
1726         gint num;
1727
1728         g_return_val_if_fail(dest != NULL, -1);
1729         g_return_val_if_fail(msglist != NULL, -1);
1730
1731         folder = dest->folder;
1732         if (dest->last_num < 0) folder->scan(folder, dest);
1733
1734         num = folder->move_msgs_with_dest(folder, dest, msglist);
1735         if (num > 0) dest->last_num = num;
1736         else dest->op_count = 0;
1737
1738         return num;
1739 }
1740 */
1741
1742 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
1743 {
1744         Folder *folder;
1745         FolderItem *item;
1746         GSList *newmsgnums = NULL;
1747         GSList *l, *l2;
1748         gint num;
1749
1750         g_return_val_if_fail(dest != NULL, -1);
1751         g_return_val_if_fail(msglist != NULL, -1);
1752
1753         folder = dest->folder;
1754
1755         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1756         g_return_val_if_fail(folder->remove_msg != NULL, -1);
1757
1758         /* 
1759          * Copy messages to destination folder and 
1760          * store new message numbers in newmsgnums
1761          */
1762         item = NULL;
1763         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
1764                 MsgInfo * msginfo = (MsgInfo *) l->data;
1765
1766                 if (!item && msginfo->folder != NULL)
1767                         item = msginfo->folder;
1768
1769                 num = folder->copy_msg(folder, dest, msginfo);
1770                 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1771         }
1772
1773         /* Read cache for dest folder */
1774         if (!dest->cache) folder_item_read_cache(dest);
1775         
1776         /* 
1777          * Fetch new MsgInfos for new messages in dest folder,
1778          * add them to the msgcache and update folder message counts
1779          */
1780         l2 = newmsgnums;
1781         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1782                 MsgInfo *msginfo = (MsgInfo *) l->data;
1783
1784                 num = GPOINTER_TO_INT(l2->data);
1785
1786                 if (num != -1) {
1787                         MsgInfo *newmsginfo;
1788
1789                         newmsginfo = folder->get_msginfo(folder, dest, num);
1790                         if (newmsginfo) {
1791                                 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1792                                 if (dest->stype == F_OUTBOX ||
1793                                     dest->stype == F_QUEUE  ||
1794                                     dest->stype == F_DRAFT  ||
1795                                     dest->stype == F_TRASH)
1796                                         MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1797                                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
1798                                 msgcache_add_msg(dest->cache, newmsginfo);
1799
1800                                 if (MSG_IS_NEW(newmsginfo->flags))
1801                                         dest->new++;
1802                                 if (MSG_IS_UNREAD(newmsginfo->flags))
1803                                         dest->unread++;
1804                                 if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
1805                                         dest->unreadmarked++;
1806                                 dest->total++;
1807                                 dest->need_update = TRUE;
1808
1809                                 procmsg_msginfo_free(newmsginfo);
1810                         }
1811                 }
1812                 l2 = g_slist_next(l2);
1813         }
1814
1815         /*
1816          * Remove source messages from their folders if
1817          * copying was successfull and update folder
1818          * message counts
1819          */
1820         l2 = newmsgnums;
1821         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1822                 MsgInfo *msginfo = (MsgInfo *) l->data;
1823
1824                 num = GPOINTER_TO_INT(l2->data);
1825                 
1826                 if (num != -1) {
1827                         item->folder->remove_msg(item->folder,
1828                                                  msginfo->folder,
1829                                                  msginfo->msgnum);
1830                         if (!item->cache)
1831                                 folder_item_read_cache(item);
1832                         msgcache_remove_msg(item->cache, msginfo->msgnum);
1833
1834                         if (MSG_IS_NEW(msginfo->flags))
1835                                 msginfo->folder->new--;
1836                         if (MSG_IS_UNREAD(msginfo->flags))
1837                                 msginfo->folder->unread--;
1838                         if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
1839                                 msginfo->folder->unreadmarked--;
1840                         msginfo->folder->total--;                       
1841                         msginfo->folder->need_update = TRUE;
1842                 }
1843
1844                 l2 = g_slist_next(l2);
1845         }
1846
1847
1848         if (folder->finished_copy)
1849                 folder->finished_copy(folder, dest);
1850
1851         g_slist_free(newmsgnums);
1852         return dest->last_num;
1853 }
1854
1855 /*
1856 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1857 {
1858         Folder *folder;
1859         gint num;
1860
1861         g_return_val_if_fail(dest != NULL, -1);
1862         g_return_val_if_fail(msginfo != NULL, -1);
1863
1864         folder = dest->folder;
1865         if (dest->last_num < 0) folder->scan(folder, dest);
1866
1867         num = folder->copy_msg(folder, dest, msginfo);
1868         if (num > 0) dest->last_num = num;
1869
1870         return num;
1871 }
1872 */
1873
1874 gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
1875 {
1876         Folder *folder;
1877         gint num;
1878
1879         g_return_val_if_fail(dest != NULL, -1);
1880         g_return_val_if_fail(msginfo != NULL, -1);
1881
1882         folder = dest->folder;
1883
1884         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1885
1886         if (!dest->cache) folder_item_read_cache(dest);
1887         
1888         num = folder->copy_msg(folder, dest, msginfo);
1889         if (num != -1) {
1890                 MsgInfo *newmsginfo;
1891
1892                 if (NULL != (newmsginfo = folder->get_msginfo(folder, dest, num))) {
1893                         newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1894                         if (dest->stype == F_OUTBOX ||
1895                             dest->stype == F_QUEUE  ||
1896                             dest->stype == F_DRAFT  ||
1897                             dest->stype == F_TRASH)
1898                                 MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1899                                                      MSG_NEW|MSG_UNREAD|MSG_DELETED);
1900                         msgcache_add_msg(dest->cache, newmsginfo);
1901
1902                         if (MSG_IS_NEW(newmsginfo->flags))
1903                                 dest->new++;
1904                         if (MSG_IS_UNREAD(newmsginfo->flags))
1905                                 dest->unread++;
1906                         if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
1907                                 dest->unreadmarked++;
1908                         dest->total++;
1909                         dest->need_update = TRUE;
1910
1911                         procmsg_msginfo_free(newmsginfo);
1912                 }                       
1913         }
1914
1915         if (folder->finished_copy)
1916                 folder->finished_copy(folder, dest);
1917
1918         return num;
1919 }
1920
1921 /*
1922 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1923 {
1924         Folder *folder;
1925         gint num;
1926
1927         g_return_val_if_fail(dest != NULL, -1);
1928         g_return_val_if_fail(msglist != NULL, -1);
1929
1930         folder = dest->folder;
1931         if (dest->last_num < 0) folder->scan(folder, dest);
1932
1933         num = folder->copy_msgs_with_dest(folder, dest, msglist);
1934         if (num > 0) dest->last_num = num;
1935         else dest->op_count = 0;
1936
1937         return num;
1938 }
1939 */
1940
1941 gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
1942 {
1943         Folder *folder;
1944         gint num;
1945         GSList *newmsgnums = NULL;
1946         GSList *l, *l2;
1947
1948         g_return_val_if_fail(dest != NULL, -1);
1949         g_return_val_if_fail(msglist != NULL, -1);
1950
1951         folder = dest->folder;
1952  
1953         g_return_val_if_fail(folder->copy_msg != NULL, -1);
1954
1955         /* 
1956          * Copy messages to destination folder and 
1957          * store new message numbers in newmsgnums
1958          */
1959         for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
1960                 MsgInfo * msginfo = (MsgInfo *) l->data;
1961
1962                 num = folder->copy_msg(folder, dest, msginfo);
1963                 newmsgnums = g_slist_append(newmsgnums, GINT_TO_POINTER(num));
1964         }
1965
1966         /* Read cache for dest folder */
1967         if (!dest->cache) folder_item_read_cache(dest);
1968
1969         /* 
1970          * Fetch new MsgInfos for new messages in dest folder,
1971          * add them to the msgcache and update folder message counts
1972          */
1973         l2 = newmsgnums;
1974         for (l = msglist; l != NULL; l = g_slist_next(l)) {
1975                 MsgInfo *msginfo = (MsgInfo *) l->data;
1976
1977                 num = GPOINTER_TO_INT(l2->data);
1978
1979                 if (num != -1) {
1980                         MsgInfo *newmsginfo;
1981
1982                         newmsginfo = folder->get_msginfo(folder, dest, num);
1983                         if (newmsginfo) {
1984                                 newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
1985                                 if (dest->stype == F_OUTBOX ||
1986                                     dest->stype == F_QUEUE  ||
1987                                     dest->stype == F_DRAFT  ||
1988                                     dest->stype == F_TRASH)
1989                                         MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
1990                                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
1991                                 msgcache_add_msg(dest->cache, newmsginfo);
1992
1993                                 if (MSG_IS_NEW(newmsginfo->flags))
1994                                         dest->new++;
1995                                 if (MSG_IS_UNREAD(newmsginfo->flags))
1996                                         dest->unread++;
1997                                 if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
1998                                         dest->unreadmarked++;
1999                                 dest->total++;
2000                                 dest->need_update = TRUE;
2001
2002                                 procmsg_msginfo_free(newmsginfo);
2003                         }
2004                 }
2005                 l2 = g_slist_next(l2);
2006         }
2007         
2008         if (folder->finished_copy)
2009                 folder->finished_copy(folder, dest);
2010
2011         g_slist_free(newmsgnums);
2012         return dest->last_num;
2013 }
2014
2015 gint folder_item_remove_msg(FolderItem *item, gint num)
2016 {
2017         Folder *folder;
2018         gint ret;
2019         MsgInfo *msginfo;
2020
2021         g_return_val_if_fail(item != NULL, -1);
2022
2023         folder = item->folder;
2024         if (!item->cache) folder_item_read_cache(item);
2025
2026         ret = folder->remove_msg(folder, item, num);
2027
2028         msginfo = msgcache_get_msg(item->cache, num);
2029         if (msginfo != NULL) {
2030                 if (MSG_IS_NEW(msginfo->flags))
2031                         item->new--;
2032                 if (MSG_IS_UNREAD(msginfo->flags))
2033                         item->unread--;
2034                 if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
2035                         item->unreadmarked--;
2036                 procmsg_msginfo_free(msginfo);
2037                 msgcache_remove_msg(item->cache, num);
2038         }
2039         item->total--;
2040         item->need_update = TRUE;
2041
2042         return ret;
2043 }
2044
2045 gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
2046 {
2047         Folder *folder;
2048         gint ret = 0;
2049
2050         g_return_val_if_fail(item != NULL, -1);
2051         folder = item->folder;
2052         g_return_val_if_fail(folder != NULL, -1);
2053
2054         if (!item->cache) folder_item_read_cache(item);
2055
2056         if (folder->remove_msgs) {
2057                 ret = folder->remove_msgs(folder, item, msglist);
2058                 if (ret == 0)
2059                         folder_item_scan(item);
2060                 return ret;
2061         }
2062
2063         while (msglist != NULL) {
2064                 MsgInfo *msginfo = (MsgInfo *)msglist->data;
2065
2066                 ret = folder_item_remove_msg(item, msginfo->msgnum);
2067                 if (ret != 0) break;
2068                 msgcache_remove_msg(item->cache, msginfo->msgnum);
2069                 msglist = msglist->next;
2070         }
2071
2072         return ret;
2073 }
2074
2075 gint folder_item_remove_all_msg(FolderItem *item)
2076 {
2077         Folder *folder;
2078         gint result;
2079
2080         g_return_val_if_fail(item != NULL, -1);
2081
2082         folder = item->folder;
2083
2084         g_return_val_if_fail(folder->remove_all_msg != NULL, -1);
2085
2086         result = folder->remove_all_msg(folder, item);
2087
2088         if (result == 0) {
2089                 if (folder->finished_remove)
2090                         folder->finished_remove(folder, item);
2091
2092                 folder_item_free_cache(item);
2093                 item->cache = msgcache_new();
2094
2095                 item->new = 0;
2096                 item->unread = 0;
2097                 item->unreadmarked = 0;
2098                 item->total = 0;
2099                 item->need_update = TRUE;
2100         }
2101
2102         return result;
2103 }
2104
2105 gboolean folder_item_is_msg_changed(FolderItem *item, MsgInfo *msginfo)
2106 {
2107         Folder *folder;
2108
2109         g_return_val_if_fail(item != NULL, FALSE);
2110
2111         folder = item->folder;
2112
2113         g_return_val_if_fail(folder->is_msg_changed != NULL, -1);
2114
2115         return folder->is_msg_changed(folder, item, msginfo);
2116 }
2117
2118 gchar *folder_item_get_cache_file(FolderItem *item)
2119 {
2120         gchar *path;
2121         gchar *file;
2122
2123         g_return_val_if_fail(item != NULL, NULL);
2124         g_return_val_if_fail(item->path != NULL, NULL);
2125
2126         path = folder_item_get_path(item);
2127         g_return_val_if_fail(path != NULL, NULL);
2128         if (!is_dir_exist(path))
2129                 make_dir_hier(path);
2130         file = g_strconcat(path, G_DIR_SEPARATOR_S, CACHE_FILE, NULL);
2131         g_free(path);
2132
2133         return file;
2134 }
2135
2136 gchar *folder_item_get_mark_file(FolderItem *item)
2137 {
2138         gchar *path;
2139         gchar *file;
2140
2141         g_return_val_if_fail(item != NULL, NULL);
2142         g_return_val_if_fail(item->path != NULL, NULL);
2143
2144         path = folder_item_get_path(item);
2145         g_return_val_if_fail(path != NULL, NULL);
2146         if (!is_dir_exist(path))
2147                 make_dir_hier(path);
2148         file = g_strconcat(path, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
2149         g_free(path);
2150
2151         return file;
2152 }
2153
2154 static gboolean folder_build_tree(GNode *node, gpointer data)
2155 {
2156         Folder *folder = FOLDER(data);
2157         FolderItem *item;
2158         XMLNode *xmlnode;
2159         GList *list;
2160         SpecialFolderItemType stype = F_NORMAL;
2161         const gchar *name = NULL;
2162         const gchar *path = NULL;
2163         PrefsAccount *account = NULL;
2164         gboolean no_sub = FALSE, no_select = FALSE, collapsed = FALSE, 
2165                  threaded = TRUE, apply_sub = FALSE;
2166         gboolean ret_rcpt = FALSE, hidereadmsgs = FALSE; /* CLAWS */
2167         FolderSortKey sort_key = SORT_BY_NONE;
2168         FolderSortType sort_type = SORT_ASCENDING;
2169         gint new = 0, unread = 0, total = 0, unreadmarked = 0;
2170         time_t mtime = 0;
2171
2172         g_return_val_if_fail(node->data != NULL, FALSE);
2173         if (!node->parent) return FALSE;
2174
2175         xmlnode = node->data;
2176         if (strcmp2(xmlnode->tag->tag, "folderitem") != 0) {
2177                 g_warning("tag name != \"folderitem\"\n");
2178                 return FALSE;
2179         }
2180
2181         list = xmlnode->tag->attr;
2182         for (; list != NULL; list = list->next) {
2183                 XMLAttr *attr = list->data;
2184
2185                 if (!attr || !attr->name || !attr->value) continue;
2186                 if (!strcmp(attr->name, "type")) {
2187                         if (!strcasecmp(attr->value, "normal"))
2188                                 stype = F_NORMAL;
2189                         else if (!strcasecmp(attr->value, "inbox"))
2190                                 stype = F_INBOX;
2191                         else if (!strcasecmp(attr->value, "outbox"))
2192                                 stype = F_OUTBOX;
2193                         else if (!strcasecmp(attr->value, "draft"))
2194                                 stype = F_DRAFT;
2195                         else if (!strcasecmp(attr->value, "queue"))
2196                                 stype = F_QUEUE;
2197                         else if (!strcasecmp(attr->value, "trash"))
2198                                 stype = F_TRASH;
2199                 } else if (!strcmp(attr->name, "name"))
2200                         name = attr->value;
2201                 else if (!strcmp(attr->name, "path"))
2202                         path = attr->value;
2203                 else if (!strcmp(attr->name, "mtime"))
2204                         mtime = strtoul(attr->value, NULL, 10);
2205                 else if (!strcmp(attr->name, "new"))
2206                         new = atoi(attr->value);
2207                 else if (!strcmp(attr->name, "unread"))
2208                         unread = atoi(attr->value);
2209                 else if (!strcmp(attr->name, "unreadmarked"))
2210                         unreadmarked = atoi(attr->value);
2211                 else if (!strcmp(attr->name, "total"))
2212                         total = atoi(attr->value);
2213                 else if (!strcmp(attr->name, "no_sub"))
2214                         no_sub = *attr->value == '1' ? TRUE : FALSE;
2215                 else if (!strcmp(attr->name, "no_select"))
2216                         no_select = *attr->value == '1' ? TRUE : FALSE;
2217                 else if (!strcmp(attr->name, "collapsed"))
2218                         collapsed = *attr->value == '1' ? TRUE : FALSE;
2219                 else if (!strcmp(attr->name, "threaded"))
2220                         threaded =  *attr->value == '1' ? TRUE : FALSE;
2221                 else if (!strcmp(attr->name, "hidereadmsgs"))
2222                         hidereadmsgs =  *attr->value == '1' ? TRUE : FALSE;
2223                 else if (!strcmp(attr->name, "reqretrcpt"))
2224                         ret_rcpt =  *attr->value == '1' ? TRUE : FALSE;
2225                 else if (!strcmp(attr->name, "sort_key")) {
2226                         if (!strcmp(attr->value, "none"))
2227                                 sort_key = SORT_BY_NONE;
2228                         else if (!strcmp(attr->value, "number"))
2229                                 sort_key = SORT_BY_NUMBER;
2230                         else if (!strcmp(attr->value, "size"))
2231                                 sort_key = SORT_BY_SIZE;
2232                         else if (!strcmp(attr->value, "date"))
2233                                 sort_key = SORT_BY_DATE;
2234                         else if (!strcmp(attr->value, "from"))
2235                                 sort_key = SORT_BY_FROM;
2236                         else if (!strcmp(attr->value, "subject"))
2237                                 sort_key = SORT_BY_SUBJECT;
2238                         else if (!strcmp(attr->value, "score"))
2239                                 sort_key = SORT_BY_SCORE;
2240                         else if (!strcmp(attr->value, "label"))
2241                                 sort_key = SORT_BY_LABEL;
2242                         else if (!strcmp(attr->value, "mark"))
2243                                 sort_key = SORT_BY_MARK;
2244                         else if (!strcmp(attr->value, "unread"))
2245                                 sort_key = SORT_BY_UNREAD;
2246                         else if (!strcmp(attr->value, "mime"))
2247                                 sort_key = SORT_BY_MIME;
2248                         else if (!strcmp(attr->value, "locked"))
2249                                 sort_key = SORT_BY_LOCKED;
2250                 } else if (!strcmp(attr->name, "sort_type")) {
2251                         if (!strcmp(attr->value, "ascending"))
2252                                 sort_type = SORT_ASCENDING;
2253                         else
2254                                 sort_type = SORT_DESCENDING;
2255                 } else if (!strcmp(attr->name, "account_id")) {
2256                         account = account_find_from_id(atoi(attr->value));
2257                         if (!account) g_warning("account_id: %s not found\n",
2258                                                 attr->value);
2259                 } else if (!strcmp(attr->name, "apply_sub"))
2260                         apply_sub = *attr->value == '1' ? TRUE : FALSE;
2261         }
2262
2263         item = folder_item_new(folder, name, path);
2264         item->stype = stype;
2265         item->mtime = mtime;
2266         item->new = new;
2267         item->unread = unread;
2268         item->unreadmarked = unreadmarked;
2269         item->total = total;
2270         item->no_sub = no_sub;
2271         item->no_select = no_select;
2272         item->collapsed = collapsed;
2273         item->threaded  = threaded;
2274         item->hide_read_msgs  = hidereadmsgs;
2275         item->ret_rcpt  = ret_rcpt;
2276         item->sort_key  = sort_key;
2277         item->sort_type = sort_type;
2278         item->parent = FOLDER_ITEM(node->parent->data);
2279         item->folder = folder;
2280         switch (stype) {
2281         case F_INBOX:  folder->inbox  = item; break;
2282         case F_OUTBOX: folder->outbox = item; break;
2283         case F_DRAFT:  folder->draft  = item; break;
2284         case F_QUEUE:  folder->queue  = item; break;
2285         case F_TRASH:  folder->trash  = item; break;
2286         default:       break;
2287         }
2288         item->account = account;
2289         item->apply_sub = apply_sub;
2290         prefs_folder_item_read_config(item);
2291
2292         node->data = item;
2293         xml_free_node(xmlnode);
2294
2295         return FALSE;
2296 }
2297
2298 static gboolean folder_read_folder_func(GNode *node, gpointer data)
2299 {
2300         Folder *folder;
2301         XMLNode *xmlnode;
2302         GList *list;
2303         FolderType type = F_UNKNOWN;
2304         const gchar *name = NULL;
2305         const gchar *path = NULL;
2306         PrefsAccount *account = NULL;
2307         gboolean collapsed = FALSE, threaded = TRUE, apply_sub = FALSE;
2308         gboolean ret_rcpt = FALSE; /* CLAWS */
2309
2310         if (g_node_depth(node) != 2) return FALSE;
2311         g_return_val_if_fail(node->data != NULL, FALSE);
2312
2313         xmlnode = node->data;
2314         if (strcmp2(xmlnode->tag->tag, "folder") != 0) {
2315                 g_warning("tag name != \"folder\"\n");
2316                 return TRUE;
2317         }
2318         g_node_unlink(node);
2319         list = xmlnode->tag->attr;
2320         for (; list != NULL; list = list->next) {
2321                 XMLAttr *attr = list->data;
2322
2323                 if (!attr || !attr->name || !attr->value) continue;
2324                 if (!strcmp(attr->name, "type")) {
2325                         if (!strcasecmp(attr->value, "mh"))
2326                                 type = F_MH;
2327                         else if (!strcasecmp(attr->value, "mbox"))
2328                                 type = F_MBOX;
2329                         else if (!strcasecmp(attr->value, "maildir"))
2330                                 type = F_MAILDIR;
2331                         else if (!strcasecmp(attr->value, "imap"))
2332                                 type = F_IMAP;
2333                         else if (!strcasecmp(attr->value, "news"))
2334                                 type = F_NEWS;
2335                 } else if (!strcmp(attr->name, "name"))
2336                         name = attr->value;
2337                 else if (!strcmp(attr->name, "path"))
2338                         path = attr->value;
2339                 else if (!strcmp(attr->name, "collapsed"))
2340                         collapsed = *attr->value == '1' ? TRUE : FALSE;
2341                 else if (!strcmp(attr->name, "threaded"))
2342                         threaded = *attr->value == '1' ? TRUE : FALSE;
2343                 else if (!strcmp(attr->name, "account_id")) {
2344                         account = account_find_from_id(atoi(attr->value));
2345                         if (!account) g_warning("account_id: %s not found\n",
2346                                                 attr->value);
2347                 } else if (!strcmp(attr->name, "apply_sub"))
2348                         apply_sub = *attr->value == '1' ? TRUE : FALSE;
2349                 else if (!strcmp(attr->name, "reqretrcpt"))
2350                         ret_rcpt = *attr->value == '1' ? TRUE : FALSE;
2351         }
2352
2353         folder = folder_new(type, name, path);
2354         g_return_val_if_fail(folder != NULL, FALSE);
2355         folder->account = account;
2356         if (account && (type == F_IMAP || type == F_NEWS))
2357                 account->folder = REMOTE_FOLDER(folder);
2358         node->data = folder->node->data;
2359         g_node_destroy(folder->node);
2360         folder->node = node;
2361         folder_add(folder);
2362         FOLDER_ITEM(node->data)->collapsed = collapsed;
2363         FOLDER_ITEM(node->data)->threaded  = threaded;
2364         FOLDER_ITEM(node->data)->account   = account;
2365         FOLDER_ITEM(node->data)->apply_sub = apply_sub;
2366         FOLDER_ITEM(node->data)->ret_rcpt  = ret_rcpt;
2367
2368         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2369                         folder_build_tree, folder);
2370
2371         return FALSE;
2372 }
2373
2374 static gchar *folder_get_list_path(void)
2375 {
2376         static gchar *filename = NULL;
2377
2378         if (!filename)
2379                 filename =  g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2380                                         FOLDER_LIST, NULL);
2381
2382         return filename;
2383 }
2384
2385 #define PUT_ESCAPE_STR(fp, attr, str)                   \
2386 {                                                       \
2387         fputs(" " attr "=\"", fp);                      \
2388         xml_file_put_escape_str(fp, str);               \
2389         fputs("\"", fp);                                \
2390 }
2391
2392 static void folder_write_list_recursive(GNode *node, gpointer data)
2393 {
2394         FILE *fp = (FILE *)data;
2395         FolderItem *item;
2396         gint i, depth;
2397         static gchar *folder_type_str[] = {"mh", "mbox", "maildir", "imap",
2398                                            "news", "unknown"};
2399         static gchar *folder_item_stype_str[] = {"normal", "inbox", "outbox",
2400                                                  "draft", "queue", "trash"};
2401         static gchar *sort_key_str[] = {"none", "number", "size", "date",
2402                                         "from", "subject", "score", "label",
2403                                         "mark", "unread", "mime", "locked" };
2404         g_return_if_fail(node != NULL);
2405         g_return_if_fail(fp != NULL);
2406
2407         item = FOLDER_ITEM(node->data);
2408         g_return_if_fail(item != NULL);
2409
2410         depth = g_node_depth(node);
2411         for (i = 0; i < depth; i++)
2412                 fputs("    ", fp);
2413         if (depth == 1) {
2414                 Folder *folder = item->folder;
2415
2416                 fprintf(fp, "<folder type=\"%s\"", folder_type_str[folder->type]);
2417                 if (folder->name)
2418                         PUT_ESCAPE_STR(fp, "name", folder->name);
2419                 if (folder->type == F_MH || folder->type == F_MBOX)
2420                         PUT_ESCAPE_STR(fp, "path",
2421                                        LOCAL_FOLDER(folder)->rootpath);
2422                 if (item->collapsed && node->children)
2423                         fputs(" collapsed=\"1\"", fp);
2424                 if (folder->account)
2425                         fprintf(fp, " account_id=\"%d\"",
2426                                 folder->account->account_id);
2427                 if (item->apply_sub)
2428                         fputs(" apply_sub=\"1\"", fp);
2429                 if (item->ret_rcpt) 
2430                         fputs(" reqretrcpt=\"1\"", fp);
2431         } else {
2432                 fprintf(fp, "<folderitem type=\"%s\"",
2433                         folder_item_stype_str[item->stype]);
2434                 if (item->name)
2435                         PUT_ESCAPE_STR(fp, "name", item->name);
2436                 if (item->path)
2437                         PUT_ESCAPE_STR(fp, "path", item->path);
2438
2439                 if (item->no_sub)
2440                         fputs(" no_sub=\"1\"", fp);
2441                 if (item->no_select)
2442                         fputs(" no_select=\"1\"", fp);
2443                 if (item->collapsed && node->children)
2444                         fputs(" collapsed=\"1\"", fp);
2445                 if (item->threaded)
2446                         fputs(" threaded=\"1\"", fp);
2447                 else
2448                         fputs(" threaded=\"0\"", fp);
2449                 if (item->hide_read_msgs)
2450                         fputs(" hidereadmsgs=\"1\"", fp);
2451                 else
2452                         fputs(" hidereadmsgs=\"0\"", fp);
2453                 if (item->ret_rcpt)
2454                         fputs(" reqretrcpt=\"1\"", fp);
2455
2456                 if (item->sort_key != SORT_BY_NONE) {
2457                         fprintf(fp, " sort_key=\"%s\"",
2458                                 sort_key_str[item->sort_key]);
2459                         if (item->sort_type == SORT_ASCENDING)
2460                                 fprintf(fp, " sort_type=\"ascending\"");
2461                         else
2462                                 fprintf(fp, " sort_type=\"descending\"");
2463                 }
2464
2465                 fprintf(fp,
2466                         " mtime=\"%lu\" new=\"%d\" unread=\"%d\" unreadmarked=\"%d\" total=\"%d\"",
2467                         item->mtime, item->new, item->unread, item->unreadmarked, item->total);
2468
2469                 if (item->account)
2470                         fprintf(fp, " account_id=\"%d\"",
2471                                 item->account->account_id);
2472                 if (item->apply_sub)
2473                         fputs(" apply_sub=\"1\"", fp);
2474         }
2475
2476         if (node->children) {
2477                 GNode *child;
2478                 fputs(">\n", fp);
2479
2480                 child = node->children;
2481                 while (child) {
2482                         GNode *cur;
2483
2484                         cur = child;
2485                         child = cur->next;
2486                         folder_write_list_recursive(cur, data);
2487                 }
2488
2489                 for (i = 0; i < depth; i++)
2490                         fputs("    ", fp);
2491                 fprintf(fp, "</%s>\n", depth == 1 ? "folder" : "folderitem");
2492         } else
2493                 fputs(" />\n", fp);
2494 }
2495
2496 static void folder_update_op_count_rec(GNode *node)
2497 {
2498         FolderItem *fitem = FOLDER_ITEM(node->data);
2499
2500         if (g_node_depth(node) > 0) {
2501                 if (fitem->op_count > 0) {
2502                         fitem->op_count = 0;
2503                         folder_update_item(fitem, FALSE);
2504                 }
2505                 if (node->children) {
2506                         GNode *child;
2507
2508                         child = node->children;
2509                         while (child) {
2510                                 GNode *cur;
2511
2512                                 cur = child;
2513                                 child = cur->next;
2514                                 folder_update_op_count_rec(cur);
2515                         }
2516                 }
2517         }
2518 }
2519
2520 void folder_update_op_count() {
2521         GList *cur;
2522         Folder *folder;
2523
2524         for (cur = folder_list; cur != NULL; cur = cur->next) {
2525                 folder = cur->data;
2526                 folder_update_op_count_rec(folder->node);
2527         }
2528 }
2529
2530 typedef struct _type_str {
2531         gchar * str;
2532         gint type;
2533 } type_str;
2534
2535
2536 /*
2537 static gchar * folder_item_get_tree_identifier(FolderItem * item)
2538 {
2539         if (item->parent != NULL) {
2540                 gchar * path;
2541                 gchar * id;
2542
2543                 path = folder_item_get_tree_identifier(item->parent);
2544                 if (path == NULL)
2545                         return NULL;
2546
2547                 id = g_strconcat(path, "/", item->name, NULL);
2548                 g_free(path);
2549
2550                 return id;
2551         }
2552         else {
2553                 return g_strconcat("/", item->name, NULL);
2554         }
2555 }
2556 */
2557
2558 /* CLAWS: temporary local folder for filtering */
2559 static Folder *processing_folder;
2560 static FolderItem *processing_folder_item;
2561
2562 static void folder_create_processing_folder(void)
2563 {
2564 #define PROCESSING_FOLDER ".processing" 
2565         Folder     *tmpparent;
2566         gchar      *tmpname;
2567
2568         tmpparent = folder_get_default_folder();
2569         g_assert(tmpparent);
2570         debug_print("tmpparentroot %s\n", LOCAL_FOLDER(tmpparent)->rootpath);
2571         if (LOCAL_FOLDER(tmpparent)->rootpath[0] == '/')
2572                 tmpname = g_strconcat(LOCAL_FOLDER(tmpparent)->rootpath,
2573                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2574                                       NULL);
2575         else
2576                 tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
2577                                       LOCAL_FOLDER(tmpparent)->rootpath,
2578                                       G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
2579                                       NULL);
2580
2581         processing_folder = folder_new(F_MH, "PROCESSING", LOCAL_FOLDER(tmpparent)->rootpath);
2582         g_assert(processing_folder);
2583
2584         if (!is_dir_exist(tmpname)) {
2585                 debug_print("*TMP* creating %s\n", tmpname);
2586                 processing_folder_item = processing_folder->create_folder(processing_folder,
2587                                                                           processing_folder->node->data,
2588                                                                           PROCESSING_FOLDER);
2589                 g_assert(processing_folder_item);                                                                         
2590         }
2591         else {
2592                 debug_print("*TMP* already created\n");
2593                 processing_folder_item = folder_item_new(processing_folder, ".processing", ".processing");
2594                 g_assert(processing_folder_item);
2595                 folder_item_append(processing_folder->node->data, processing_folder_item);
2596         }
2597         g_free(tmpname);
2598 }
2599
2600 FolderItem *folder_get_default_processing(void)
2601 {
2602         if (!processing_folder_item) {
2603                 folder_create_processing_folder();
2604         }
2605         return processing_folder_item;
2606 }
2607
2608 /* folder_persist_prefs_new() - return hash table with persistent
2609  * settings (and folder name as key). 
2610  * (note that in claws other options are in the PREFS_FOLDER_ITEM_RC
2611  * file, so those don't need to be included in PersistPref yet) 
2612  */
2613 GHashTable *folder_persist_prefs_new(Folder *folder)
2614 {
2615         GHashTable *pptable;
2616
2617         g_return_val_if_fail(folder, NULL);
2618         pptable = g_hash_table_new(g_str_hash, g_str_equal);
2619         folder_get_persist_prefs_recursive(folder->node, pptable);
2620         return pptable;
2621 }
2622
2623 void folder_persist_prefs_free(GHashTable *pptable)
2624 {
2625         g_return_if_fail(pptable);
2626         g_hash_table_foreach_remove(pptable, persist_prefs_free, NULL);
2627         g_hash_table_destroy(pptable);
2628 }
2629
2630 const PersistPrefs *folder_get_persist_prefs(GHashTable *pptable, const char *name)
2631 {
2632         if (pptable == NULL || name == NULL) return NULL;
2633         return g_hash_table_lookup(pptable, name);
2634 }
2635
2636 void folder_item_restore_persist_prefs(FolderItem *item, GHashTable *pptable)
2637 {
2638         const PersistPrefs *pp;
2639         gchar *id = folder_item_get_identifier(item);
2640
2641         pp = folder_get_persist_prefs(pptable, id); 
2642         g_free(id);
2643
2644         if (!pp) return;
2645
2646         /* CLAWS: since not all folder properties have been migrated to 
2647          * folderlist.xml, we need to call the old stuff first before
2648          * setting things that apply both to Main and Claws. */
2649         prefs_folder_item_read_config(item); 
2650          
2651         item->collapsed = pp->collapsed;
2652         item->threaded  = pp->threaded;
2653         item->ret_rcpt  = pp->ret_rcpt;
2654         item->hide_read_msgs = pp->hide_read_msgs;
2655         item->sort_key  = pp->sort_key;
2656         item->sort_type = pp->sort_type;
2657 }
2658
2659 static void folder_get_persist_prefs_recursive(GNode *node, GHashTable *pptable)
2660 {
2661         FolderItem *item = FOLDER_ITEM(node->data);
2662         PersistPrefs *pp;
2663         GNode *child, *cur;
2664         gchar *id;
2665
2666         g_return_if_fail(node != NULL);
2667         g_return_if_fail(item != NULL);
2668
2669         /* NOTE: item->path == NULL means top level folder; not interesting
2670          * to store preferences of that one.  */
2671         if (item->path) {
2672                 id = folder_item_get_identifier(item);
2673                 pp = g_new0(PersistPrefs, 1);
2674                 g_return_if_fail(pp != NULL);
2675                 pp->collapsed = item->collapsed;
2676                 pp->threaded  = item->threaded;
2677                 pp->ret_rcpt  = item->ret_rcpt; 
2678                 pp->hide_read_msgs = item->hide_read_msgs;
2679                 pp->sort_key  = item->sort_key;
2680                 pp->sort_type = item->sort_type;
2681                 g_hash_table_insert(pptable, id, pp);
2682         }
2683
2684         if (node->children) {
2685                 child = node->children;
2686                 while (child) {
2687                         cur = child;
2688                         child = cur->next;
2689                         folder_get_persist_prefs_recursive(cur, pptable);
2690                 }
2691         }       
2692 }
2693
2694 static gboolean persist_prefs_free(gpointer key, gpointer val, gpointer data)
2695 {
2696         if (key) 
2697                 g_free(key);
2698         if (val) 
2699                 g_free(val);
2700         return TRUE;    
2701 }
2702
2703 void folder_item_apply_processing(FolderItem *item)
2704 {
2705         GSList *processing_list;
2706         GSList *mlist, *cur;
2707         
2708         g_return_if_fail(item != NULL);
2709         
2710         processing_list = item->prefs->processing;
2711         if (processing_list == NULL)
2712                 return;
2713
2714         mlist = folder_item_get_msg_list(item);
2715         
2716         for (cur = mlist ; cur != NULL ; cur = cur->next) {
2717                 MsgInfo * msginfo;
2718
2719                 msginfo = (MsgInfo *) cur->data;
2720                 filter_message_by_msginfo(processing_list, msginfo);
2721                 procmsg_msginfo_free(msginfo);
2722         }
2723         
2724         folder_update_items_when_required(FALSE);
2725
2726         g_slist_free(mlist);
2727 }
2728
2729 /*
2730  *  Callback handling for FolderItem content changes
2731  */
2732 void folder_update_item(FolderItem *item, gboolean contentchange)
2733 {
2734         FolderItemUpdateData source;
2735         
2736         source.item = item;
2737         source.content_change = contentchange;
2738         hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
2739 }
2740
2741 static void folder_update_item_func(FolderItem *item, gpointer contentchange)
2742 {
2743         FolderItemUpdateData source;
2744
2745         if (item->need_update) {
2746                 source.item = item;
2747                 source.content_change = GPOINTER_TO_INT(contentchange);
2748                 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
2749
2750                 item->need_update = FALSE;
2751         }
2752 }
2753
2754 void folder_update_items_when_required(gboolean contentchange)
2755 {
2756         folder_func_to_all_folders(folder_update_item_func, GINT_TO_POINTER(contentchange));
2757 }
2758
2759 void folder_update_item_recursive(FolderItem *item, gboolean update_summary)
2760 {
2761         GNode *node = item->folder->node;       
2762         FolderItemUpdateData source;
2763
2764         node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
2765         node = node->children;
2766
2767         source.item = item;
2768         source.content_change = update_summary; 
2769         hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
2770         while (node != NULL) {
2771                 if (node && node->data) {
2772                         FolderItem *next_item = (FolderItem*) node->data;
2773
2774                         source.item = next_item;
2775                         source.content_change = update_summary; 
2776                         hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST, &source);
2777                 }
2778                 node = node->next;
2779         }
2780 }
2781
2782 #undef PUT_ESCAPE_STR