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