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