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