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