0.9.3claws64
[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                        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 #define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
299 { \
300         MsgInfo newmsginfo; \
301  \
302         newmsginfo.msgnum = dest->last_num; \
303         newmsginfo.flags = msginfo->flags; \
304         if (dest->stype == F_OUTBOX || \
305             dest->stype == F_QUEUE  || \
306             dest->stype == F_DRAFT  || \
307             dest->stype == F_TRASH) \
308                 MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
309                                      MSG_NEW|MSG_UNREAD|MSG_DELETED); \
310  \
311         procmsg_write_flags(&newmsginfo, fp); \
312 }
313
314 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
315 {
316         gint ret;
317         GSList file_list;
318         MsgFileInfo fileinfo;
319
320         g_return_val_if_fail(file != NULL, -1);
321
322         fileinfo.file = (gchar *)file;
323         fileinfo.flags = flags;
324         file_list.data = &fileinfo;
325         file_list.next = NULL;
326
327         ret = mh_add_msgs(folder, dest, &file_list, NULL);
328         return ret;
329
330  
331 gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list, 
332                  GRelation *relation)
333
334         gchar *destfile;
335         GSList *cur;
336         MsgFileInfo *fileinfo;
337
338         g_return_val_if_fail(dest != NULL, -1);
339         g_return_val_if_fail(file_list != NULL, -1);
340
341         if (dest->last_num < 0) {
342                 mh_get_last_num(folder, dest);
343                 if (dest->last_num < 0) return -1;
344         }
345
346         for (cur = file_list; cur != NULL; cur = cur->next) {
347                 fileinfo = (MsgFileInfo *)cur->data;
348
349                 destfile = mh_get_new_msg_filename(dest);
350                 if (destfile == NULL) return -1;
351
352                 if (link(fileinfo->file, destfile) < 0) {
353                         if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
354                                 g_warning(_("can't copy message %s to %s\n"),
355                                           fileinfo->file, destfile);
356                                 g_free(destfile);
357                                 return -1;
358                         }
359                 }
360                 if (relation != NULL)
361                         g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
362
363                 g_free(destfile);
364                 dest->last_num++;
365         }
366
367         return dest->last_num;
368 }
369
370 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
371 {
372         gchar *srcfile;
373         gchar *destfile;
374         gint filemode = 0;
375         FolderItemPrefs *prefs;
376
377         g_return_val_if_fail(dest != NULL, -1);
378         g_return_val_if_fail(msginfo != NULL, -1);
379
380         if (msginfo->folder == dest) {
381                 g_warning("the src folder is identical to the dest.\n");
382                 return -1;
383         }
384
385         if (dest->last_num < 0) {
386                 mh_get_last_num(folder, dest);
387                 if (dest->last_num < 0) return -1;
388         }
389
390         prefs = dest->prefs;
391
392         srcfile = procmsg_get_message_file(msginfo);
393         destfile = mh_get_new_msg_filename(dest);
394         if (!destfile) {
395                 g_free(srcfile);
396                 return -1;
397         }
398         
399         debug_print("Copying message %s%c%d to %s ...\n",
400                     msginfo->folder->path, G_DIR_SEPARATOR,
401                     msginfo->msgnum, dest->path);
402         
403
404         if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
405         &&  dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
406                 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
407                         g_free(srcfile);
408                         g_free(destfile);
409                         return -1;
410                 }
411         } else if (copy_file(srcfile, destfile, TRUE) < 0) {
412                 FILE_OP_ERROR(srcfile, "copy");
413                 g_free(srcfile);
414                 g_free(destfile);
415                 return -1;
416         }
417
418
419         if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
420                 if (chmod(destfile, prefs->folder_chmod) < 0)
421                         FILE_OP_ERROR(destfile, "chmod");
422
423                 /* for mark file */
424                 filemode = prefs->folder_chmod;
425                 if (filemode & S_IRGRP) filemode |= S_IWGRP;
426                 if (filemode & S_IROTH) filemode |= S_IWOTH;
427         }
428
429         g_free(srcfile);
430         g_free(destfile);
431         dest->last_num++;
432
433         return dest->last_num;
434 }
435
436 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
437 {
438         gchar *file;
439
440         g_return_val_if_fail(item != NULL, -1);
441
442         file = mh_fetch_msg(folder, item, num);
443         g_return_val_if_fail(file != NULL, -1);
444
445         if (unlink(file) < 0) {
446                 FILE_OP_ERROR(file, "unlink");
447                 g_free(file);
448                 return -1;
449         }
450
451         g_free(file);
452         return 0;
453 }
454
455 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
456 {
457         gchar *path;
458         gint val;
459
460         g_return_val_if_fail(item != NULL, -1);
461
462         path = folder_item_get_path(item);
463         g_return_val_if_fail(path != NULL, -1);
464         val = remove_all_numbered_files(path);
465         g_free(path);
466
467         return val;
468 }
469
470 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
471 {
472         struct stat s;
473
474         if (stat(itos(msginfo->msgnum), &s) < 0 ||
475             msginfo->size  != s.st_size ||
476             msginfo->mtime != s.st_mtime)
477                 return TRUE;
478
479         return FALSE;
480 }
481
482 void mh_scan_tree(Folder *folder)
483 {
484         FolderItem *item;
485         gchar *rootpath;
486
487         g_return_if_fail(folder != NULL);
488
489         item = folder_item_new(folder, folder->name, NULL);
490         item->folder = folder;
491         folder->node = g_node_new(item);
492
493         rootpath = folder_item_get_path(item);
494         if (change_dir(rootpath) < 0) {
495                 g_free(rootpath);
496                 return;
497         }
498         g_free(rootpath);
499
500         mh_create_tree(folder);
501         mh_scan_tree_recursive(item);
502 }
503
504 #define MAKE_DIR_IF_NOT_EXIST(dir) \
505 { \
506         if (!is_dir_exist(dir)) { \
507                 if (is_file_exist(dir)) { \
508                         g_warning("File `%s' already exists.\n" \
509                                     "Can't create folder.", dir); \
510                         return -1; \
511                 } \
512                 if (make_dir(dir) < 0) \
513                         return -1; \
514         } \
515 }
516
517 gint mh_create_tree(Folder *folder)
518 {
519         gchar *rootpath;
520
521         g_return_val_if_fail(folder != NULL, -1);
522
523         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
524         rootpath = LOCAL_FOLDER(folder)->rootpath;
525         MAKE_DIR_IF_NOT_EXIST(rootpath);
526         CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
527         MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
528         MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
529         MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
530         MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
531         MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
532
533         return 0;
534 }
535
536 #undef MAKE_DIR_IF_NOT_EXIST
537
538 gchar *mh_item_get_path(Folder *folder, FolderItem *item)
539 {
540         gchar *folder_path, *path;
541
542         g_return_val_if_fail(folder != NULL, NULL);
543         g_return_val_if_fail(item != NULL, NULL);
544
545         folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
546         g_return_val_if_fail(folder_path != NULL, NULL);
547
548         if (folder_path[0] == G_DIR_SEPARATOR) {
549                 if (item->path)
550                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
551                                            item->path, NULL);
552                 else
553                         path = g_strdup(folder_path);
554         } else {
555                 if (item->path)
556                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
557                                            folder_path, G_DIR_SEPARATOR_S,
558                                            item->path, NULL);
559                 else
560                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
561                                            folder_path, NULL);
562         }
563         g_free(folder_path);
564
565         return path;
566 }
567
568 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
569                              const gchar *name)
570 {
571         gchar *path;
572         gchar *fullpath;
573         FolderItem *new_item;
574         gchar *mh_sequences_filename;
575         FILE *mh_sequences_file;
576
577         g_return_val_if_fail(folder != NULL, NULL);
578         g_return_val_if_fail(parent != NULL, NULL);
579         g_return_val_if_fail(name != NULL, NULL);
580
581         path = folder_item_get_path(parent);
582         if (!is_dir_exist(path)) 
583                 if (make_dir_hier(path) != 0)
584                         return NULL;
585                 
586         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
587         g_free(path);
588
589         if (make_dir(fullpath) < 0) {
590                 g_free(fullpath);
591                 return NULL;
592         }
593
594         g_free(fullpath);
595
596         if (parent->path)
597                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
598                                    NULL);
599         else
600                 path = g_strdup(name);
601         new_item = folder_item_new(folder, name, path);
602         folder_item_append(parent, new_item);
603
604         g_free(path);
605
606         path = folder_item_get_path(new_item);
607         mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
608                                             ".mh_sequences", NULL);
609         if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
610                 fclose(mh_sequences_file);
611         }
612         g_free(mh_sequences_filename);
613         g_free(path);
614
615         return new_item;
616 }
617
618 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
619 {
620         gchar *oldpath;
621         gchar *dirname;
622         gchar *newpath;
623         GNode *node;
624         gchar *paths[2];
625
626         g_return_val_if_fail(folder != NULL, -1);
627         g_return_val_if_fail(item != NULL, -1);
628         g_return_val_if_fail(item->path != NULL, -1);
629         g_return_val_if_fail(name != NULL, -1);
630
631         oldpath = folder_item_get_path(item);
632         if (!is_dir_exist(oldpath))
633                 make_dir_hier(oldpath);
634
635         dirname = g_dirname(oldpath);
636         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
637         g_free(dirname);
638
639         if (rename(oldpath, newpath) < 0) {
640                 FILE_OP_ERROR(oldpath, "rename");
641                 g_free(oldpath);
642                 g_free(newpath);
643                 return -1;
644         }
645
646         g_free(oldpath);
647         g_free(newpath);
648
649         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
650                 dirname = g_dirname(item->path);
651                 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
652                 g_free(dirname);
653         } else
654                 newpath = g_strdup(name);
655
656         g_free(item->name);
657         item->name = g_strdup(name);
658
659         node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
660                            item);
661         paths[0] = g_strdup(item->path);
662         paths[1] = newpath;
663         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
664                         mh_rename_folder_func, paths);
665
666         g_free(paths[0]);
667         g_free(paths[1]);
668         return 0;
669 }
670
671 gint mh_remove_folder(Folder *folder, FolderItem *item)
672 {
673         gchar *path;
674
675         g_return_val_if_fail(folder != NULL, -1);
676         g_return_val_if_fail(item != NULL, -1);
677         g_return_val_if_fail(item->path != NULL, -1);
678
679         path = folder_item_get_path(item);
680         if (remove_dir_recursive(path) < 0) {
681                 g_warning("can't remove directory `%s'\n", path);
682                 g_free(path);
683                 return -1;
684         }
685
686         g_free(path);
687         folder_item_remove(item);
688         return 0;
689 }
690
691 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
692 {
693         struct stat s;
694         MsgInfo *msginfo;
695         MsgFlags flags;
696
697         flags.perm_flags = MSG_NEW|MSG_UNREAD;
698         flags.tmp_flags = 0;
699
700         g_return_val_if_fail(item != NULL, NULL);
701         g_return_val_if_fail(file != NULL, NULL);
702
703         if (item->stype == F_QUEUE) {
704                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
705         } else if (item->stype == F_DRAFT) {
706                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
707         }
708
709         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
710         if (!msginfo) return NULL;
711
712         msginfo->msgnum = atoi(file);
713         msginfo->folder = item;
714
715         if (stat(file, &s) < 0) {
716                 FILE_OP_ERROR(file, "stat");
717                 msginfo->size = 0;
718                 msginfo->mtime = 0;
719         } else {
720                 msginfo->size = s.st_size;
721                 msginfo->mtime = s.st_mtime;
722         }
723
724         return msginfo;
725 }
726
727 #if 0
728 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
729 {
730         gchar *entry;
731         gboolean result;
732
733         entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
734         result = is_dir_exist(entry);
735         g_free(entry);
736
737         return result;
738 }
739
740 /*
741  * check whether PATH is a Maildir style mailbox.
742  * This is the case if the 3 subdir: new, cur, tmp are existing.
743  * This functon assumes that entry is an directory
744  */
745 static gboolean mh_is_maildir(const gchar *path)
746 {
747         return mh_is_maildir_one(path, "new") &&
748                mh_is_maildir_one(path, "cur") &&
749                mh_is_maildir_one(path, "tmp");
750 }
751 #endif
752
753 static void mh_scan_tree_recursive(FolderItem *item)
754 {
755         DIR *dp;
756         struct dirent *d;
757         struct stat s;
758         gchar *entry;
759         gint n_msg = 0;
760
761         g_return_if_fail(item != NULL);
762         g_return_if_fail(item->folder != NULL);
763
764         dp = opendir(item->path ? item->path : ".");
765         if (!dp) {
766                 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
767                 return;
768         }
769
770         debug_print("scanning %s ...\n",
771                     item->path ? item->path
772                     : LOCAL_FOLDER(item->folder)->rootpath);
773         if (item->folder->ui_func)
774                 item->folder->ui_func(item->folder, item,
775                                       item->folder->ui_func_data);
776
777         while ((d = readdir(dp)) != NULL) {
778                 if (d->d_name[0] == '.') continue;
779
780                 if (item->path)
781                         entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
782                                             d->d_name, NULL);
783                 else
784                         entry = g_strdup(d->d_name);
785
786                 if (stat(entry, &s) < 0) {
787                         FILE_OP_ERROR(entry, "stat");
788                         g_free(entry);
789                         continue;
790                 }
791
792                 if (S_ISDIR(s.st_mode)) {
793                         FolderItem *new_item;
794
795 #if 0
796                         if (mh_is_maildir(entry)) {
797                                 g_free(entry);
798                                 continue;
799                         }
800 #endif
801
802                         new_item = folder_item_new(item->folder, d->d_name, entry);
803                         folder_item_append(item, new_item);
804                         if (!item->path) {
805                                 if (!strcmp(d->d_name, INBOX_DIR)) {
806                                         new_item->stype = F_INBOX;
807                                         item->folder->inbox = new_item;
808                                 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
809                                         new_item->stype = F_OUTBOX;
810                                         item->folder->outbox = new_item;
811                                 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
812                                         new_item->stype = F_DRAFT;
813                                         item->folder->draft = new_item;
814                                 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
815                                         new_item->stype = F_QUEUE;
816                                         item->folder->queue = new_item;
817                                 } else if (!strcmp(d->d_name, TRASH_DIR)) {
818                                         new_item->stype = F_TRASH;
819                                         item->folder->trash = new_item;
820                                 }
821                         }
822                         mh_scan_tree_recursive(new_item);
823                 } else if (to_number(d->d_name) != -1) n_msg++;
824
825                 g_free(entry);
826         }
827
828         closedir(dp);
829
830 /*
831         if (item->path) {
832                 gint new, unread, total, min, max;
833
834                 procmsg_get_mark_sum(item->path, &new, &unread, &total,
835                                      &min, &max, 0);
836                 if (n_msg > total) {
837                         new += n_msg - total;
838                         unread += n_msg - total;
839                 }
840                 item->new = new;
841                 item->unread = unread;
842                 item->total = n_msg;
843         }
844 */
845 }
846
847 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
848 {
849         FolderItem *item = node->data;
850         gchar **paths = data;
851         const gchar *oldpath = paths[0];
852         const gchar *newpath = paths[1];
853         gchar *base;
854         gchar *new_itempath;
855         gint oldpathlen;
856
857         oldpathlen = strlen(oldpath);
858         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
859                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
860                 return TRUE;
861         }
862
863         base = item->path + oldpathlen;
864         while (*base == G_DIR_SEPARATOR) base++;
865         if (*base == '\0')
866                 new_itempath = g_strdup(newpath);
867         else
868                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
869                                            NULL);
870         g_free(item->path);
871         item->path = new_itempath;
872
873         return FALSE;
874 }