e0bc447fc09dd2463a46dcab399d3524bbc078d4
[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 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_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 void mh_scan_tree(Folder *folder)
470 {
471         FolderItem *item;
472         gchar *rootpath;
473
474         g_return_if_fail(folder != NULL);
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;
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
495 #define MAKE_DIR_IF_NOT_EXIST(dir) \
496 { \
497         if (!is_dir_exist(dir)) { \
498                 if (is_file_exist(dir)) { \
499                         g_warning("File `%s' already exists.\n" \
500                                     "Can't create folder.", dir); \
501                         return -1; \
502                 } \
503                 if (make_dir(dir) < 0) \
504                         return -1; \
505         } \
506 }
507
508 gint mh_create_tree(Folder *folder)
509 {
510         gchar *rootpath;
511
512         g_return_val_if_fail(folder != NULL, -1);
513
514         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
515         rootpath = LOCAL_FOLDER(folder)->rootpath;
516         MAKE_DIR_IF_NOT_EXIST(rootpath);
517         CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
518         MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
519         MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
520         MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
521         MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
522         MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
523
524         return 0;
525 }
526
527 #undef MAKE_DIR_IF_NOT_EXIST
528
529 gchar *mh_item_get_path(Folder *folder, FolderItem *item)
530 {
531         gchar *folder_path, *path;
532
533         g_return_val_if_fail(folder != NULL, NULL);
534         g_return_val_if_fail(item != NULL, NULL);
535
536         folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
537         g_return_val_if_fail(folder_path != NULL, NULL);
538
539         if (folder_path[0] == G_DIR_SEPARATOR) {
540                 if (item->path)
541                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
542                                            item->path, NULL);
543                 else
544                         path = g_strdup(folder_path);
545         } else {
546                 if (item->path)
547                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
548                                            folder_path, G_DIR_SEPARATOR_S,
549                                            item->path, NULL);
550                 else
551                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
552                                            folder_path, NULL);
553         }
554         g_free(folder_path);
555
556         return path;
557 }
558
559 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
560                              const gchar *name)
561 {
562         gchar *path;
563         gchar *fullpath;
564         FolderItem *new_item;
565         gchar *mh_sequences_filename;
566         FILE *mh_sequences_file;
567
568         g_return_val_if_fail(folder != NULL, NULL);
569         g_return_val_if_fail(parent != NULL, NULL);
570         g_return_val_if_fail(name != NULL, NULL);
571
572         path = folder_item_get_path(parent);
573         if (!is_dir_exist(path)) 
574                 if (make_dir_hier(path) != 0)
575                         return NULL;
576                 
577         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
578         g_free(path);
579
580         if (make_dir(fullpath) < 0) {
581                 g_free(fullpath);
582                 return NULL;
583         }
584
585         g_free(fullpath);
586
587         if (parent->path)
588                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
589                                    NULL);
590         else
591                 path = g_strdup(name);
592         new_item = folder_item_new(folder, name, path);
593         folder_item_append(parent, new_item);
594
595         g_free(path);
596
597         path = folder_item_get_path(new_item);
598         mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
599                                             ".mh_sequences", NULL);
600         if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
601                 fclose(mh_sequences_file);
602         }
603         g_free(mh_sequences_filename);
604         g_free(path);
605
606         return new_item;
607 }
608
609 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
610 {
611         gchar *oldpath;
612         gchar *dirname;
613         gchar *newpath;
614         gchar *paths[2];
615
616         g_return_val_if_fail(folder != NULL, -1);
617         g_return_val_if_fail(item != NULL, -1);
618         g_return_val_if_fail(item->path != NULL, -1);
619         g_return_val_if_fail(name != NULL, -1);
620
621         oldpath = folder_item_get_path(item);
622         if (!is_dir_exist(oldpath))
623                 make_dir_hier(oldpath);
624
625         dirname = g_dirname(oldpath);
626         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
627         g_free(dirname);
628
629         if (rename(oldpath, newpath) < 0) {
630                 FILE_OP_ERROR(oldpath, "rename");
631                 g_free(oldpath);
632                 g_free(newpath);
633                 return -1;
634         }
635
636         g_free(oldpath);
637         g_free(newpath);
638
639         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
640                 dirname = g_dirname(item->path);
641                 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
642                 g_free(dirname);
643         } else
644                 newpath = g_strdup(name);
645
646         g_free(item->name);
647         item->name = g_strdup(name);
648
649         paths[0] = g_strdup(item->path);
650         paths[1] = newpath;
651         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
652                         mh_rename_folder_func, paths);
653
654         g_free(paths[0]);
655         g_free(paths[1]);
656         return 0;
657 }
658
659 gint mh_remove_folder(Folder *folder, FolderItem *item)
660 {
661         gchar *path;
662
663         g_return_val_if_fail(folder != NULL, -1);
664         g_return_val_if_fail(item != NULL, -1);
665         g_return_val_if_fail(item->path != NULL, -1);
666
667         path = folder_item_get_path(item);
668         if (remove_dir_recursive(path) < 0) {
669                 g_warning("can't remove directory `%s'\n", path);
670                 g_free(path);
671                 return -1;
672         }
673
674         g_free(path);
675         folder_item_remove(item);
676         return 0;
677 }
678
679 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
680 {
681         struct stat s;
682         MsgInfo *msginfo;
683         MsgFlags flags;
684
685         flags.perm_flags = MSG_NEW|MSG_UNREAD;
686         flags.tmp_flags = 0;
687
688         g_return_val_if_fail(item != NULL, NULL);
689         g_return_val_if_fail(file != NULL, NULL);
690
691         if (item->stype == F_QUEUE) {
692                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
693         } else if (item->stype == F_DRAFT) {
694                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
695         }
696
697         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
698         if (!msginfo) return NULL;
699
700         msginfo->msgnum = atoi(file);
701         msginfo->folder = item;
702
703         if (stat(file, &s) < 0) {
704                 FILE_OP_ERROR(file, "stat");
705                 msginfo->size = 0;
706                 msginfo->mtime = 0;
707         } else {
708                 msginfo->size = s.st_size;
709                 msginfo->mtime = s.st_mtime;
710         }
711
712         return msginfo;
713 }
714
715 #if 0
716 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
717 {
718         gchar *entry;
719         gboolean result;
720
721         entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
722         result = is_dir_exist(entry);
723         g_free(entry);
724
725         return result;
726 }
727
728 /*
729  * check whether PATH is a Maildir style mailbox.
730  * This is the case if the 3 subdir: new, cur, tmp are existing.
731  * This functon assumes that entry is an directory
732  */
733 static gboolean mh_is_maildir(const gchar *path)
734 {
735         return mh_is_maildir_one(path, "new") &&
736                mh_is_maildir_one(path, "cur") &&
737                mh_is_maildir_one(path, "tmp");
738 }
739 #endif
740
741 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
742 {
743         FolderItem *item;
744         gchar *path;
745
746         g_return_val_if_fail(node->data != NULL, FALSE);
747
748         if (G_NODE_IS_ROOT(node))
749                 return FALSE;
750
751         item = FOLDER_ITEM(node->data);
752
753         path = folder_item_get_path(item);
754         if (!is_dir_exist(path)) {
755                 debug_print("folder '%s' not found. removing...\n", path);
756                 folder_item_remove(item);
757         }
758         g_free(path);
759
760         return FALSE;
761 }
762
763 static void mh_remove_missing_folder_items(Folder *folder)
764 {
765         g_return_if_fail(folder != NULL);
766
767         debug_print("searching missing folders...\n");
768
769         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
770                         mh_remove_missing_folder_items_func, folder);
771 }
772
773 static void mh_scan_tree_recursive(FolderItem *item)
774 {
775         Folder *folder;
776         DIR *dp;
777         struct dirent *d;
778         struct stat s;
779         gchar *entry;
780         gint n_msg = 0;
781
782         g_return_if_fail(item != NULL);
783         g_return_if_fail(item->folder != NULL);
784
785         folder = item->folder;
786
787         dp = opendir(item->path ? item->path : ".");
788         if (!dp) {
789                 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
790                 return;
791         }
792
793         debug_print("scanning %s ...\n",
794                     item->path ? item->path
795                     : LOCAL_FOLDER(item->folder)->rootpath);
796         if (folder->ui_func)
797                 folder->ui_func(folder, item, folder->ui_func_data);
798
799         while ((d = readdir(dp)) != NULL) {
800                 if (d->d_name[0] == '.') continue;
801
802                 if (item->path)
803                         entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
804                                             d->d_name, NULL);
805                 else
806                         entry = g_strdup(d->d_name);
807
808                 if (stat(entry, &s) < 0) {
809                         FILE_OP_ERROR(entry, "stat");
810                         g_free(entry);
811                         continue;
812                 }
813
814                 if (S_ISDIR(s.st_mode)) {
815                         FolderItem *new_item = NULL;
816                         GNode *node;
817
818 #if 0
819                         if (mh_is_maildir(entry)) {
820                                 g_free(entry);
821                                 continue;
822                         }
823 #endif
824
825                         node = item->node;
826                         for (node = node->children; node != NULL; node = node->next) {
827                                 FolderItem *cur_item = FOLDER_ITEM(node->data);
828                                 if (!strcmp2(cur_item->path, entry)) {
829                                         new_item = cur_item;
830                                         break;
831                                 }
832                         }
833                         if (!new_item) {
834                                 debug_print("new folder '%s' found.\n", entry);
835                                 new_item = folder_item_new(folder, d->d_name, entry);
836                                 folder_item_append(item, new_item);
837                         }
838
839                         if (!item->path) {
840                                 if (!folder->inbox &&
841                                     !strcmp(d->d_name, INBOX_DIR)) {
842                                         new_item->stype = F_INBOX;
843                                         folder->inbox = new_item;
844                                 } else if (!folder->outbox &&
845                                            !strcmp(d->d_name, OUTBOX_DIR)) {
846                                         new_item->stype = F_OUTBOX;
847                                         folder->outbox = new_item;
848                                 } else if (!folder->draft &&
849                                            !strcmp(d->d_name, DRAFT_DIR)) {
850                                         new_item->stype = F_DRAFT;
851                                         folder->draft = new_item;
852                                 } else if (!folder->queue &&
853                                            !strcmp(d->d_name, QUEUE_DIR)) {
854                                         new_item->stype = F_QUEUE;
855                                         folder->queue = new_item;
856                                 } else if (!folder->trash &&
857                                            !strcmp(d->d_name, TRASH_DIR)) {
858                                         new_item->stype = F_TRASH;
859                                         folder->trash = new_item;
860                                 }
861                         }
862
863                         mh_scan_tree_recursive(new_item);
864                 } else if (to_number(d->d_name) != -1) n_msg++;
865
866                 g_free(entry);
867         }
868
869         closedir(dp);
870
871 /*
872         if (item->path) {
873                 gint new, unread, total, min, max;
874
875                 procmsg_get_mark_sum(item->path, &new, &unread, &total,
876                                      &min, &max, 0);
877                 if (n_msg > total) {
878                         new += n_msg - total;
879                         unread += n_msg - total;
880                 }
881                 item->new = new;
882                 item->unread = unread;
883                 item->total = n_msg;
884         }
885 */
886 }
887
888 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
889 {
890         FolderItem *item = node->data;
891         gchar **paths = data;
892         const gchar *oldpath = paths[0];
893         const gchar *newpath = paths[1];
894         gchar *base;
895         gchar *new_itempath;
896         gint oldpathlen;
897
898         oldpathlen = strlen(oldpath);
899         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
900                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
901                 return TRUE;
902         }
903
904         base = item->path + oldpathlen;
905         while (*base == G_DIR_SEPARATOR) base++;
906         if (*base == '\0')
907                 new_itempath = g_strdup(newpath);
908         else
909                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
910                                            NULL);
911         g_free(item->path);
912         item->path = new_itempath;
913
914         return FALSE;
915 }