0.9.3claws56
[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, MsgNumberList **newnum_list);
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         mh_get_num_list,
107         NULL,
108         NULL,
109         NULL,
110         NULL,
111         
112         /* Message functions */
113         mh_get_msginfo,
114         NULL,
115         mh_fetch_msg,
116         mh_add_msg,
117         mh_add_msgs,
118         mh_copy_msg,
119         NULL,
120         mh_remove_msg,
121         mh_remove_all_msg,
122         mh_is_msg_changed,
123         NULL,
124 };
125
126 FolderClass *mh_get_class(void)
127 {
128         return &mh_class;
129 }
130
131 Folder *mh_folder_new(const gchar *name, const gchar *path)
132 {
133         Folder *folder;
134
135         folder = (Folder *)g_new0(MHFolder, 1);
136         folder->klass = &mh_class;
137         mh_folder_init(folder, name, path);
138
139         return folder;
140 }
141
142 static void mh_folder_destroy(Folder *folder)
143 {
144         folder_local_folder_destroy(LOCAL_FOLDER(folder));
145 }
146
147 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
148 {
149         folder_local_folder_init(folder, name, path);
150
151 }
152
153 void mh_get_last_num(Folder *folder, FolderItem *item)
154 {
155         gchar *path;
156         DIR *dp;
157         struct dirent *d;
158         struct stat s;
159         gint max = 0;
160         gint num;
161
162         g_return_if_fail(item != NULL);
163
164         debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
165
166         path = folder_item_get_path(item);
167         g_return_if_fail(path != NULL);
168         if (change_dir(path) < 0) {
169                 g_free(path);
170                 return;
171         }
172         g_free(path);
173
174         if ((dp = opendir(".")) == NULL) {
175                 FILE_OP_ERROR(item->path, "opendir");
176                 return;
177         }
178
179         while ((d = readdir(dp)) != NULL) {
180                 if ((num = to_number(d->d_name)) >= 0 &&
181                     stat(d->d_name, &s) == 0 &&
182                     S_ISREG(s.st_mode)) {
183                         if (max < num)
184                                 max = num;
185                 }
186         }
187         closedir(dp);
188
189         debug_print("Last number in dir %s = %d\n", item->path, max);
190         item->last_num = max;
191 }
192
193 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list)
194 {
195
196         gchar *path;
197         DIR *dp;
198         struct dirent *d;
199         struct stat s;
200         gint num, nummsgs = 0;
201
202         g_return_val_if_fail(item != NULL, -1);
203
204         debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
205
206         path = folder_item_get_path(item);
207         g_return_val_if_fail(path != NULL, -1);
208         if (change_dir(path) < 0) {
209                 g_free(path);
210                 return -1;
211         }
212         g_free(path);
213
214         if ((dp = opendir(".")) == NULL) {
215                 FILE_OP_ERROR(item->path, "opendir");
216                 return -1;
217         }
218
219         while ((d = readdir(dp)) != NULL) {
220                 if ((num = to_number(d->d_name)) >= 0 &&
221                     stat(d->d_name, &s) == 0 &&
222                     S_ISREG(s.st_mode)) {
223                         *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
224                     nummsgs++;
225                 }
226         }
227         closedir(dp);
228
229         return nummsgs;
230 }
231
232 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
233 {
234         gchar *path;
235         gchar *file;
236
237         g_return_val_if_fail(item != NULL, NULL);
238         g_return_val_if_fail(num > 0, NULL);
239
240         path = folder_item_get_path(item);
241         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
242         g_free(path);
243         if (!is_file_exist(file)) {
244                 g_free(file);
245                 return NULL;
246         }
247
248         return file;
249 }
250
251 MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
252 {
253         MsgInfo *msginfo;
254         gchar *file;
255
256         g_return_val_if_fail(item != NULL, NULL);
257         g_return_val_if_fail(num > 0, NULL);
258
259         file = mh_fetch_msg(folder, item, num);
260         if (!file) return NULL;
261
262         msginfo = mh_parse_msg(file, item);
263         if (msginfo)
264                 msginfo->msgnum = num;
265
266         g_free(file);
267
268         return msginfo;
269 }
270
271 gchar *mh_get_new_msg_filename(FolderItem *dest)
272 {
273         gchar *destfile;
274         gchar *destpath;
275
276         destpath = folder_item_get_path(dest);
277         g_return_val_if_fail(destpath != NULL, NULL);
278
279         if (!is_dir_exist(destpath))
280                 make_dir_hier(destpath);
281
282         for (;;) {
283                 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
284                                            dest->last_num + 1);
285                 if (is_file_entry_exist(destfile)) {
286                         dest->last_num++;
287                         g_free(destfile);
288                 } else
289                         break;
290         }
291
292         g_free(destpath);
293
294         return destfile;
295 }
296
297 #define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
298 { \
299         MsgInfo newmsginfo; \
300  \
301         newmsginfo.msgnum = dest->last_num; \
302         newmsginfo.flags = msginfo->flags; \
303         if (dest->stype == F_OUTBOX || \
304             dest->stype == F_QUEUE  || \
305             dest->stype == F_DRAFT  || \
306             dest->stype == F_TRASH) \
307                 MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
308                                      MSG_NEW|MSG_UNREAD|MSG_DELETED); \
309  \
310         procmsg_write_flags(&newmsginfo, fp); \
311 }
312
313 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
314 {
315         gint ret;
316         GSList file_list;
317         MsgFileInfo fileinfo;
318         MsgNumberList *newnum_list = NULL;
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, &newnum_list);
328         g_slist_free(newnum_list);
329         return ret;
330
331  
332 gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list, 
333                  MsgNumberList **newnum_list) 
334
335         gchar *destfile;
336         GSList *cur;
337         MsgFileInfo *fileinfo;
338
339         g_return_val_if_fail(dest != NULL, -1);
340         g_return_val_if_fail(file_list != NULL, -1);
341
342         if (dest->last_num < 0) {
343                 mh_get_last_num(folder, dest);
344                 if (dest->last_num < 0) return -1;
345         }
346
347         for (cur = file_list; cur != NULL; cur = cur->next) {
348                 fileinfo = (MsgFileInfo *)cur->data;
349
350                 destfile = mh_get_new_msg_filename(dest);
351                 if (destfile == NULL) return -1;
352
353                 if (link(fileinfo->file, destfile) < 0) {
354                         if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
355                                 g_warning(_("can't copy message %s to %s\n"),
356                                           fileinfo->file, destfile);
357                                 g_free(destfile);
358                                 return -1;
359                         }
360                 }
361                 *newnum_list = g_slist_append(*newnum_list, 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 }