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