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