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