2005-01-03 [colin] 0.9.13cvs25
[claws.git] / src / mh.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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 <dirent.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #undef MEASURE_TIME
34
35 #ifdef MEASURE_TIME
36 #  include <sys/time.h>
37 #endif
38
39 #include "intl.h"
40 #include "folder.h"
41 #include "mh.h"
42 #include "procmsg.h"
43 #include "procheader.h"
44 #include "utils.h"
45
46 static void     mh_folder_init          (Folder         *folder,
47                                          const gchar    *name,
48                                          const gchar    *path);
49
50 static Folder   *mh_folder_new          (const gchar    *name,
51                                          const gchar    *path);
52 static void     mh_folder_destroy       (Folder         *folder);
53 static gchar   *mh_fetch_msg            (Folder         *folder,
54                                          FolderItem     *item,
55                                          gint            num);
56 static MsgInfo *mh_get_msginfo          (Folder         *folder,
57                                          FolderItem     *item,
58                                          gint            num);
59 static gint     mh_add_msg              (Folder         *folder,
60                                          FolderItem     *dest,
61                                          const gchar    *file,
62                                          MsgFlags       *flags);
63 static gint     mh_add_msgs             (Folder         *folder,
64                                          FolderItem     *dest,
65                                          GSList         *file_list,
66                                          GRelation      *relation);
67 static gint     mh_copy_msg             (Folder         *folder,
68                                          FolderItem     *dest,
69                                          MsgInfo        *msginfo);
70 static gint     mh_remove_msg           (Folder         *folder,
71                                          FolderItem     *item,
72                                          gint            num);
73 static gint     mh_remove_all_msg       (Folder         *folder,
74                                          FolderItem     *item);
75 static gboolean mh_is_msg_changed       (Folder         *folder,
76                                          FolderItem     *item,
77                                          MsgInfo        *msginfo);
78
79 static gint     mh_get_num_list         (Folder         *folder,
80                                          FolderItem     *item, 
81                                          GSList         **list, 
82                                          gboolean       *old_uids_valid);
83 static gint     mh_scan_tree            (Folder         *folder);
84
85 static gint    mh_create_tree           (Folder         *folder);
86 static FolderItem *mh_create_folder     (Folder         *folder,
87                                          FolderItem     *parent,
88                                          const gchar    *name);
89 static gint    mh_rename_folder         (Folder         *folder,
90                                          FolderItem     *item,
91                                          const gchar    *name);
92 static gint    mh_remove_folder         (Folder         *folder,
93                                          FolderItem     *item);
94
95 static gchar   *mh_get_new_msg_filename         (FolderItem     *dest);
96
97 static MsgInfo *mh_parse_msg                    (const gchar    *file,
98                                                  FolderItem     *item);
99 static void     mh_remove_missing_folder_items  (Folder         *folder);
100 static void     mh_scan_tree_recursive          (FolderItem     *item);
101
102 static gboolean mh_rename_folder_func           (GNode          *node,
103                                                  gpointer        data);
104 static gchar   *mh_item_get_path                (Folder *folder, 
105                                                  FolderItem *item);
106
107 static FolderClass mh_class;
108
109 FolderClass *mh_get_class(void)
110 {
111         if (mh_class.idstr == NULL) {
112                 mh_class.type = F_MH;
113                 mh_class.idstr = "mh";
114                 mh_class.uistr = "MH";
115                 
116                 /* Folder functions */
117                 mh_class.new_folder = mh_folder_new;
118                 mh_class.destroy_folder = mh_folder_destroy;
119                 mh_class.set_xml = folder_local_set_xml;
120                 mh_class.get_xml = folder_local_get_xml;
121                 mh_class.scan_tree = mh_scan_tree;
122                 mh_class.create_tree = mh_create_tree;
123
124                 /* FolderItem functions */
125                 mh_class.item_get_path = mh_item_get_path;
126                 mh_class.create_folder = mh_create_folder;
127                 mh_class.rename_folder = mh_rename_folder;
128                 mh_class.remove_folder = mh_remove_folder;
129                 mh_class.get_num_list = mh_get_num_list;
130                 
131                 /* Message functions */
132                 mh_class.get_msginfo = mh_get_msginfo;
133                 mh_class.fetch_msg = mh_fetch_msg;
134                 mh_class.add_msg = mh_add_msg;
135                 mh_class.add_msgs = mh_add_msgs;
136                 mh_class.copy_msg = mh_copy_msg;
137                 mh_class.remove_msg = mh_remove_msg;
138                 mh_class.remove_all_msg = mh_remove_all_msg;
139                 mh_class.is_msg_changed = mh_is_msg_changed;
140         }
141
142         return &mh_class;
143 }
144
145 static Folder *mh_folder_new(const gchar *name, const gchar *path)
146 {
147         Folder *folder;
148
149         folder = (Folder *)g_new0(MHFolder, 1);
150         folder->klass = &mh_class;
151         mh_folder_init(folder, name, path);
152
153         return folder;
154 }
155
156 static void mh_folder_destroy(Folder *folder)
157 {
158         folder_local_folder_destroy(LOCAL_FOLDER(folder));
159 }
160
161 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
162 {
163         folder_local_folder_init(folder, name, path);
164
165 }
166
167 void mh_get_last_num(Folder *folder, FolderItem *item)
168 {
169         gchar *path;
170         DIR *dp;
171         struct dirent *d;
172         gint max = 0;
173         gint num;
174
175         g_return_if_fail(item != NULL);
176
177         debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
178
179         path = folder_item_get_path(item);
180         g_return_if_fail(path != NULL);
181         if (change_dir(path) < 0) {
182                 g_free(path);
183                 return;
184         }
185         g_free(path);
186
187         if ((dp = opendir(".")) == NULL) {
188                 FILE_OP_ERROR(item->path, "opendir");
189                 return;
190         }
191
192         while ((d = readdir(dp)) != NULL) {
193                 if ((num = to_number(d->d_name)) >= 0 &&
194                     dirent_is_regular_file(d)) {
195                         if (max < num)
196                                 max = num;
197                 }
198         }
199         closedir(dp);
200
201         debug_print("Last number in dir %s = %d\n", item->path, max);
202         item->last_num = max;
203 }
204
205 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
206 {
207
208         gchar *path;
209         DIR *dp;
210         struct dirent *d;
211         gint num, nummsgs = 0;
212
213         g_return_val_if_fail(item != NULL, -1);
214
215         debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
216
217         *old_uids_valid = TRUE;
218
219         path = folder_item_get_path(item);
220         g_return_val_if_fail(path != NULL, -1);
221         if (change_dir(path) < 0) {
222                 g_free(path);
223                 return -1;
224         }
225         g_free(path);
226
227         if ((dp = opendir(".")) == NULL) {
228                 FILE_OP_ERROR(item->path, "opendir");
229                 return -1;
230         }
231
232         while ((d = readdir(dp)) != NULL) {
233                 if ((num = to_number(d->d_name)) >= 0) {
234                         *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
235                         nummsgs++;
236                 }
237         }
238         closedir(dp);
239
240         return nummsgs;
241 }
242
243 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
244 {
245         gchar *path;
246         gchar *file;
247
248         g_return_val_if_fail(item != NULL, NULL);
249         g_return_val_if_fail(num > 0, NULL);
250
251         path = folder_item_get_path(item);
252         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
253         g_free(path);
254         if (!is_file_exist(file)) {
255                 g_free(file);
256                 return NULL;
257         }
258
259         return file;
260 }
261
262 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
263 {
264         MsgInfo *msginfo;
265         gchar *file;
266
267         g_return_val_if_fail(item != NULL, NULL);
268         g_return_val_if_fail(num > 0, NULL);
269
270         file = mh_fetch_msg(folder, item, num);
271         if (!file) return NULL;
272
273         msginfo = mh_parse_msg(file, item);
274         if (msginfo)
275                 msginfo->msgnum = num;
276
277         g_free(file);
278
279         return msginfo;
280 }
281
282 static gchar *mh_get_new_msg_filename(FolderItem *dest)
283 {
284         gchar *destfile;
285         gchar *destpath;
286
287         destpath = folder_item_get_path(dest);
288         g_return_val_if_fail(destpath != NULL, NULL);
289
290         if (!is_dir_exist(destpath))
291                 make_dir_hier(destpath);
292
293         for (;;) {
294                 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
295                                            dest->last_num + 1);
296                 if (is_file_entry_exist(destfile)) {
297                         dest->last_num++;
298                         g_free(destfile);
299                 } else
300                         break;
301         }
302
303         g_free(destpath);
304
305         return destfile;
306 }
307
308 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
309 {
310         gint ret;
311         GSList file_list;
312         MsgFileInfo fileinfo;
313
314         g_return_val_if_fail(file != NULL, -1);
315
316         fileinfo.msginfo = NULL;
317         fileinfo.file = (gchar *)file;
318         fileinfo.flags = flags;
319         file_list.data = &fileinfo;
320         file_list.next = NULL;
321
322         ret = mh_add_msgs(folder, dest, &file_list, NULL);
323         return ret;
324
325  
326 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list, 
327                  GRelation *relation)
328
329         gchar *destfile;
330         GSList *cur;
331         MsgFileInfo *fileinfo;
332
333         g_return_val_if_fail(dest != NULL, -1);
334         g_return_val_if_fail(file_list != NULL, -1);
335
336         if (dest->last_num < 0) {
337                 mh_get_last_num(folder, dest);
338                 if (dest->last_num < 0) return -1;
339         }
340
341         for (cur = file_list; cur != NULL; cur = cur->next) {
342                 fileinfo = (MsgFileInfo *)cur->data;
343
344                 destfile = mh_get_new_msg_filename(dest);
345                 if (destfile == NULL) return -1;
346
347                 if (link(fileinfo->file, destfile) < 0) {
348                         if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
349                                 g_warning(_("can't copy message %s to %s\n"),
350                                           fileinfo->file, destfile);
351                                 g_free(destfile);
352                                 return -1;
353                         }
354                 }
355                 if (relation != NULL)
356                         g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
357                 g_free(destfile);
358                 dest->last_num++;
359         }
360
361         return dest->last_num;
362 }
363
364 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
365 {
366         gchar *srcfile;
367         gchar *destfile;
368         gint filemode = 0;
369         FolderItemPrefs *prefs;
370
371         g_return_val_if_fail(dest != NULL, -1);
372         g_return_val_if_fail(msginfo != NULL, -1);
373
374         if (msginfo->folder == dest) {
375                 g_warning("the src folder is identical to the dest.\n");
376                 return -1;
377         }
378
379         if (dest->last_num < 0) {
380                 mh_get_last_num(folder, dest);
381                 if (dest->last_num < 0) return -1;
382         }
383
384         prefs = dest->prefs;
385
386         srcfile = procmsg_get_message_file(msginfo);
387         destfile = mh_get_new_msg_filename(dest);
388         if (!destfile) {
389                 g_free(srcfile);
390                 return -1;
391         }
392         
393         debug_print("Copying message %s%c%d to %s ...\n",
394                     msginfo->folder->path, G_DIR_SEPARATOR,
395                     msginfo->msgnum, dest->path);
396         
397
398         if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
399         &&  dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
400                 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
401                         g_free(srcfile);
402                         g_free(destfile);
403                         return -1;
404                 }
405         } else if (copy_file(srcfile, destfile, TRUE) < 0) {
406                 FILE_OP_ERROR(srcfile, "copy");
407                 g_free(srcfile);
408                 g_free(destfile);
409                 return -1;
410         }
411
412
413         if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
414                 if (chmod(destfile, prefs->folder_chmod) < 0)
415                         FILE_OP_ERROR(destfile, "chmod");
416
417                 /* for mark file */
418                 filemode = prefs->folder_chmod;
419                 if (filemode & S_IRGRP) filemode |= S_IWGRP;
420                 if (filemode & S_IROTH) filemode |= S_IWOTH;
421         }
422
423         g_free(srcfile);
424         g_free(destfile);
425         dest->last_num++;
426
427         return dest->last_num;
428 }
429
430 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
431 {
432         gchar *file;
433
434         g_return_val_if_fail(item != NULL, -1);
435
436         file = mh_fetch_msg(folder, item, num);
437         g_return_val_if_fail(file != NULL, -1);
438
439         if (unlink(file) < 0) {
440                 FILE_OP_ERROR(file, "unlink");
441                 g_free(file);
442                 return -1;
443         }
444
445         g_free(file);
446         return 0;
447 }
448
449 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
450 {
451         gchar *path;
452         gint val;
453
454         g_return_val_if_fail(item != NULL, -1);
455
456         path = folder_item_get_path(item);
457         g_return_val_if_fail(path != NULL, -1);
458         val = remove_all_numbered_files(path);
459         g_free(path);
460
461         return val;
462 }
463
464 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
465                                   MsgInfo *msginfo)
466 {
467         struct stat s;
468
469         if (stat(itos(msginfo->msgnum), &s) < 0 ||
470             msginfo->size  != s.st_size ||
471             msginfo->mtime != s.st_mtime)
472                 return TRUE;
473
474         return FALSE;
475 }
476
477 static gint mh_scan_tree(Folder *folder)
478 {
479         FolderItem *item;
480         gchar *rootpath;
481
482         g_return_val_if_fail(folder != NULL, -1);
483
484         if (!folder->node) {
485                 item = folder_item_new(folder, folder->name, NULL);
486                 item->folder = folder;
487                 folder->node = item->node = g_node_new(item);
488         } else
489                 item = FOLDER_ITEM(folder->node->data);
490
491         rootpath = folder_item_get_path(item);
492         if (change_dir(rootpath) < 0) {
493                 g_free(rootpath);
494                 return -1;
495         }
496         g_free(rootpath);
497
498         mh_create_tree(folder);
499         mh_remove_missing_folder_items(folder);
500         mh_scan_tree_recursive(item);
501
502         return 0;
503 }
504
505 #define MAKE_DIR_IF_NOT_EXIST(dir) \
506 { \
507         if (!is_dir_exist(dir)) { \
508                 if (is_file_exist(dir)) { \
509                         g_warning("File `%s' already exists.\n" \
510                                     "Can't create folder.", dir); \
511                         return -1; \
512                 } \
513                 if (make_dir(dir) < 0) \
514                         return -1; \
515         } \
516 }
517
518 static gint mh_create_tree(Folder *folder)
519 {
520         gchar *rootpath;
521
522         g_return_val_if_fail(folder != NULL, -1);
523
524         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
525         rootpath = LOCAL_FOLDER(folder)->rootpath;
526         MAKE_DIR_IF_NOT_EXIST(rootpath);
527         CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
528         MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
529         MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
530         MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
531         MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
532         MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
533
534         return 0;
535 }
536
537 #undef MAKE_DIR_IF_NOT_EXIST
538
539 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
540 {
541         gchar *folder_path, *path;
542
543         g_return_val_if_fail(folder != NULL, NULL);
544         g_return_val_if_fail(item != NULL, NULL);
545
546         folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
547         g_return_val_if_fail(folder_path != NULL, NULL);
548
549         if (folder_path[0] == G_DIR_SEPARATOR) {
550                 if (item->path)
551                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
552                                            item->path, NULL);
553                 else
554                         path = g_strdup(folder_path);
555         } else {
556                 if (item->path)
557                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
558                                            folder_path, G_DIR_SEPARATOR_S,
559                                            item->path, NULL);
560                 else
561                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
562                                            folder_path, NULL);
563         }
564         g_free(folder_path);
565
566         return path;
567 }
568
569 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
570                                     const gchar *name)
571 {
572         gchar *path;
573         gchar *fullpath;
574         FolderItem *new_item;
575         gchar *mh_sequences_filename;
576         FILE *mh_sequences_file;
577
578         g_return_val_if_fail(folder != NULL, NULL);
579         g_return_val_if_fail(parent != NULL, NULL);
580         g_return_val_if_fail(name != NULL, NULL);
581
582         path = folder_item_get_path(parent);
583         if (!is_dir_exist(path)) 
584                 if (make_dir_hier(path) != 0)
585                         return NULL;
586                 
587         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
588         g_free(path);
589
590         if (make_dir(fullpath) < 0) {
591                 g_free(fullpath);
592                 return NULL;
593         }
594
595         g_free(fullpath);
596
597         if (parent->path)
598                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
599                                    NULL);
600         else
601                 path = g_strdup(name);
602         new_item = folder_item_new(folder, name, path);
603         folder_item_append(parent, new_item);
604
605         g_free(path);
606
607         path = folder_item_get_path(new_item);
608         mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
609                                             ".mh_sequences", NULL);
610         if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
611                 fclose(mh_sequences_file);
612         }
613         g_free(mh_sequences_filename);
614         g_free(path);
615
616         return new_item;
617 }
618
619 static gint mh_rename_folder(Folder *folder, FolderItem *item,
620                              const gchar *name)
621 {
622         gchar *oldpath;
623         gchar *dirname;
624         gchar *newpath;
625         gchar *paths[2];
626
627         g_return_val_if_fail(folder != NULL, -1);
628         g_return_val_if_fail(item != NULL, -1);
629         g_return_val_if_fail(item->path != NULL, -1);
630         g_return_val_if_fail(name != NULL, -1);
631
632         oldpath = folder_item_get_path(item);
633         if (!is_dir_exist(oldpath))
634                 make_dir_hier(oldpath);
635
636         dirname = g_dirname(oldpath);
637         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
638         g_free(dirname);
639
640         if (rename(oldpath, newpath) < 0) {
641                 FILE_OP_ERROR(oldpath, "rename");
642                 g_free(oldpath);
643                 g_free(newpath);
644                 return -1;
645         }
646
647         g_free(oldpath);
648         g_free(newpath);
649
650         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
651                 dirname = g_dirname(item->path);
652                 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
653                 g_free(dirname);
654         } else
655                 newpath = g_strdup(name);
656
657         g_free(item->name);
658         item->name = g_strdup(name);
659
660         paths[0] = g_strdup(item->path);
661         paths[1] = newpath;
662         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
663                         mh_rename_folder_func, paths);
664
665         g_free(paths[0]);
666         g_free(paths[1]);
667         return 0;
668 }
669
670 static gint mh_remove_folder(Folder *folder, FolderItem *item)
671 {
672         gchar *path;
673
674         g_return_val_if_fail(folder != NULL, -1);
675         g_return_val_if_fail(item != NULL, -1);
676         g_return_val_if_fail(item->path != NULL, -1);
677
678         path = folder_item_get_path(item);
679         if (remove_dir_recursive(path) < 0) {
680                 g_warning("can't remove directory `%s'\n", path);
681                 g_free(path);
682                 return -1;
683         }
684
685         g_free(path);
686         folder_item_remove(item);
687         return 0;
688 }
689
690 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
691 {
692         MsgInfo *msginfo;
693         MsgFlags flags;
694
695         g_return_val_if_fail(item != NULL, NULL);
696         g_return_val_if_fail(file != NULL, NULL);
697
698         flags.perm_flags = MSG_NEW|MSG_UNREAD;
699         flags.tmp_flags = 0;
700
701         if (item->stype == F_QUEUE) {
702                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
703         } else if (item->stype == F_DRAFT) {
704                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
705         }
706
707         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
708         if (!msginfo) return NULL;
709
710         msginfo->msgnum = atoi(file);
711         msginfo->folder = item;
712
713         return msginfo;
714 }
715
716 #if 0
717 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
718 {
719         gchar *entry;
720         gboolean result;
721
722         entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
723         result = is_dir_exist(entry);
724         g_free(entry);
725
726         return result;
727 }
728
729 /*
730  * check whether PATH is a Maildir style mailbox.
731  * This is the case if the 3 subdir: new, cur, tmp are existing.
732  * This functon assumes that entry is an directory
733  */
734 static gboolean mh_is_maildir(const gchar *path)
735 {
736         return mh_is_maildir_one(path, "new") &&
737                mh_is_maildir_one(path, "cur") &&
738                mh_is_maildir_one(path, "tmp");
739 }
740 #endif
741
742 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
743 {
744         FolderItem *item;
745         gchar *path;
746
747         g_return_val_if_fail(node->data != NULL, FALSE);
748
749         if (G_NODE_IS_ROOT(node))
750                 return FALSE;
751
752         item = FOLDER_ITEM(node->data);
753
754         path = folder_item_get_path(item);
755         if (!is_dir_exist(path)) {
756                 debug_print("folder '%s' not found. removing...\n", path);
757                 folder_item_remove(item);
758         }
759         g_free(path);
760
761         return FALSE;
762 }
763
764 static void mh_remove_missing_folder_items(Folder *folder)
765 {
766         g_return_if_fail(folder != NULL);
767
768         debug_print("searching missing folders...\n");
769
770         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
771                         mh_remove_missing_folder_items_func, folder);
772 }
773
774 static void mh_scan_tree_recursive(FolderItem *item)
775 {
776         Folder *folder;
777         DIR *dp;
778         struct dirent *d;
779         struct stat s;
780         gchar *entry;
781         gint n_msg = 0;
782
783         g_return_if_fail(item != NULL);
784         g_return_if_fail(item->folder != NULL);
785
786         folder = item->folder;
787
788         dp = opendir(item->path ? item->path : ".");
789         if (!dp) {
790                 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
791                 return;
792         }
793
794         debug_print("scanning %s ...\n",
795                     item->path ? item->path
796                     : LOCAL_FOLDER(item->folder)->rootpath);
797         if (folder->ui_func)
798                 folder->ui_func(folder, item, folder->ui_func_data);
799
800         while ((d = readdir(dp)) != NULL) {
801                 if (d->d_name[0] == '.') continue;
802
803                 if (item->path)
804                         entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
805                                             d->d_name, NULL);
806                 else
807                         entry = g_strdup(d->d_name);
808
809                 if (
810 #ifdef HAVE_DIRENT_D_TYPE
811                         d->d_type == DT_DIR ||
812                         (d->d_type == DT_UNKNOWN &&
813 #endif
814                         stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
815 #ifdef HAVE_DIRENT_D_TYPE
816                         )
817 #endif
818                    ) {
819                         FolderItem *new_item = NULL;
820                         GNode *node;
821
822 #if 0
823                         if (mh_is_maildir(entry)) {
824                                 g_free(entry);
825                                 continue;
826                         }
827 #endif
828
829                         node = item->node;
830                         for (node = node->children; node != NULL; node = node->next) {
831                                 FolderItem *cur_item = FOLDER_ITEM(node->data);
832                                 if (!strcmp2(cur_item->path, entry)) {
833                                         new_item = cur_item;
834                                         break;
835                                 }
836                         }
837                         if (!new_item) {
838                                 debug_print("new folder '%s' found.\n", entry);
839                                 new_item = folder_item_new(folder, d->d_name, entry);
840                                 folder_item_append(item, new_item);
841                         }
842
843                         if (!item->path) {
844                                 if (!folder->inbox &&
845                                     !strcmp(d->d_name, INBOX_DIR)) {
846                                         new_item->stype = F_INBOX;
847                                         folder->inbox = new_item;
848                                 } else if (!folder->outbox &&
849                                            !strcmp(d->d_name, OUTBOX_DIR)) {
850                                         new_item->stype = F_OUTBOX;
851                                         folder->outbox = new_item;
852                                 } else if (!folder->draft &&
853                                            !strcmp(d->d_name, DRAFT_DIR)) {
854                                         new_item->stype = F_DRAFT;
855                                         folder->draft = new_item;
856                                 } else if (!folder->queue &&
857                                            !strcmp(d->d_name, QUEUE_DIR)) {
858                                         new_item->stype = F_QUEUE;
859                                         folder->queue = new_item;
860                                 } else if (!folder->trash &&
861                                            !strcmp(d->d_name, TRASH_DIR)) {
862                                         new_item->stype = F_TRASH;
863                                         folder->trash = new_item;
864                                 }
865                         }
866
867                         mh_scan_tree_recursive(new_item);
868                 } else if (to_number(d->d_name) != -1) n_msg++;
869
870                 g_free(entry);
871         }
872
873         closedir(dp);
874
875 /*
876         if (item->path) {
877                 gint new, unread, total, min, max;
878
879                 procmsg_get_mark_sum(item->path, &new, &unread, &total,
880                                      &min, &max, 0);
881                 if (n_msg > total) {
882                         new += n_msg - total;
883                         unread += n_msg - total;
884                 }
885                 item->new = new;
886                 item->unread = unread;
887                 item->total = n_msg;
888         }
889 */
890 }
891
892 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
893 {
894         FolderItem *item = node->data;
895         gchar **paths = data;
896         const gchar *oldpath = paths[0];
897         const gchar *newpath = paths[1];
898         gchar *base;
899         gchar *new_itempath;
900         gint oldpathlen;
901
902         oldpathlen = strlen(oldpath);
903         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
904                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
905                 return TRUE;
906         }
907
908         base = item->path + oldpathlen;
909         while (*base == G_DIR_SEPARATOR) base++;
910         if (*base == '\0')
911                 new_itempath = g_strdup(newpath);
912         else
913                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
914                                            NULL);
915         g_free(item->path);
916         item->path = new_itempath;
917
918         return FALSE;
919 }