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