b41ab8b8309e09accc785ff9673aec6ee8b25a26
[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                 return NULL;
297         }
298
299         return file;
300 }
301
302 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
303 {
304         MsgInfo *msginfo;
305         gchar *file;
306
307         g_return_val_if_fail(item != NULL, NULL);
308         g_return_val_if_fail(num > 0, NULL);
309
310         file = mh_fetch_msg(folder, item, num);
311         if (!file) return NULL;
312
313         msginfo = mh_parse_msg(file, item);
314         if (msginfo)
315                 msginfo->msgnum = num;
316
317         g_free(file);
318
319         return msginfo;
320 }
321
322 static gchar *mh_get_new_msg_filename(FolderItem *dest)
323 {
324         gchar *destfile;
325         gchar *destpath;
326
327         destpath = folder_item_get_path(dest);
328         g_return_val_if_fail(destpath != NULL, NULL);
329
330         if (!is_dir_exist(destpath))
331                 make_dir_hier(destpath);
332
333         for (;;) {
334                 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
335                                            dest->last_num + 1);
336                 if (is_file_entry_exist(destfile)) {
337                         dest->last_num++;
338                         g_free(destfile);
339                 } else
340                         break;
341         }
342
343         g_free(destpath);
344
345         return destfile;
346 }
347
348 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
349 {
350         gint ret;
351         GSList file_list;
352         MsgFileInfo fileinfo;
353
354         g_return_val_if_fail(file != NULL, -1);
355
356         fileinfo.msginfo = NULL;
357         fileinfo.file = (gchar *)file;
358         fileinfo.flags = flags;
359         file_list.data = &fileinfo;
360         file_list.next = NULL;
361
362         ret = mh_add_msgs(folder, dest, &file_list, NULL);
363         return ret;
364
365  
366 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list, 
367                  GRelation *relation)
368
369         gchar *destfile;
370         GSList *cur;
371         MsgFileInfo *fileinfo;
372
373         g_return_val_if_fail(dest != NULL, -1);
374         g_return_val_if_fail(file_list != NULL, -1);
375
376         if (dest->last_num < 0) {
377                 mh_get_last_num(folder, dest);
378                 if (dest->last_num < 0) return -1;
379         }
380
381         for (cur = file_list; cur != NULL; cur = cur->next) {
382                 fileinfo = (MsgFileInfo *)cur->data;
383
384                 destfile = mh_get_new_msg_filename(dest);
385                 if (destfile == NULL) return -1;
386
387                 if (link(fileinfo->file, destfile) < 0) {
388                         if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
389                                 g_warning(_("can't copy message %s to %s\n"),
390                                           fileinfo->file, destfile);
391                                 g_free(destfile);
392                                 return -1;
393                         }
394                 }
395                 if (relation != NULL)
396                         g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
397                 g_free(destfile);
398                 dest->last_num++;
399         }
400
401         return dest->last_num;
402 }
403
404 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
405 {
406         gboolean dest_need_scan = FALSE;
407         gchar *srcfile;
408         gchar *destfile;
409         gint filemode = 0;
410         FolderItemPrefs *prefs;
411
412         g_return_val_if_fail(dest != NULL, -1);
413         g_return_val_if_fail(msginfo != NULL, -1);
414
415         if (msginfo->folder == dest) {
416                 g_warning("the src folder is identical to the dest.\n");
417                 return -1;
418         }
419
420         if (dest->last_num < 0) {
421                 mh_get_last_num(folder, dest);
422                 if (dest->last_num < 0) return -1;
423         }
424
425         prefs = dest->prefs;
426
427         srcfile = procmsg_get_message_file(msginfo);
428         destfile = mh_get_new_msg_filename(dest);
429         if (!destfile) {
430                 g_free(srcfile);
431                 return -1;
432         }
433         
434         debug_print("Copying message %s%c%d to %s ...\n",
435                     msginfo->folder->path, G_DIR_SEPARATOR,
436                     msginfo->msgnum, dest->path);
437         
438
439         if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
440         &&  dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
441                 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
442                         g_free(srcfile);
443                         g_free(destfile);
444                         return -1;
445                 }
446         } else if (copy_file(srcfile, destfile, TRUE) < 0) {
447                 FILE_OP_ERROR(srcfile, "copy");
448                 g_free(srcfile);
449                 g_free(destfile);
450                 return -1;
451         }
452
453         dest_need_scan = mh_scan_required(dest->folder, dest);
454         if (!dest_need_scan)
455                 dest->mtime = time(NULL);
456
457
458         if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
459                 if (chmod(destfile, prefs->folder_chmod) < 0)
460                         FILE_OP_ERROR(destfile, "chmod");
461
462                 /* for mark file */
463                 filemode = prefs->folder_chmod;
464                 if (filemode & S_IRGRP) filemode |= S_IWGRP;
465                 if (filemode & S_IROTH) filemode |= S_IWOTH;
466         }
467
468         g_free(srcfile);
469         g_free(destfile);
470         dest->last_num++;
471
472         return dest->last_num;
473 }
474
475 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
476 {
477         gboolean need_scan = FALSE;
478         gchar *file;
479
480         g_return_val_if_fail(item != NULL, -1);
481
482         file = mh_fetch_msg(folder, item, num);
483         g_return_val_if_fail(file != NULL, -1);
484
485         need_scan = mh_scan_required(folder, item);
486
487         if (unlink(file) < 0) {
488                 FILE_OP_ERROR(file, "unlink");
489                 g_free(file);
490                 return -1;
491         }
492
493         if (!need_scan)
494                 item->mtime = time(NULL);
495
496         g_free(file);
497         return 0;
498 }
499
500 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
501 {
502         gchar *path;
503         gint val;
504
505         g_return_val_if_fail(item != NULL, -1);
506
507         path = folder_item_get_path(item);
508         g_return_val_if_fail(path != NULL, -1);
509         val = remove_all_numbered_files(path);
510         g_free(path);
511
512         return val;
513 }
514
515 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
516                                   MsgInfo *msginfo)
517 {
518         struct stat s;
519
520         if (stat(itos(msginfo->msgnum), &s) < 0 ||
521             msginfo->size  != s.st_size || (
522                 (msginfo->mtime - s.st_mtime != 0) &&
523                 (msginfo->mtime - s.st_mtime != 3600) &&
524                 (msginfo->mtime - s.st_mtime != -3600)))
525                 return TRUE;
526
527         return FALSE;
528 }
529
530 static gint mh_scan_tree(Folder *folder)
531 {
532         FolderItem *item;
533         gchar *rootpath;
534
535         g_return_val_if_fail(folder != NULL, -1);
536
537         if (!folder->node) {
538                 item = folder_item_new(folder, folder->name, NULL);
539                 item->folder = folder;
540                 folder->node = item->node = g_node_new(item);
541         } else
542                 item = FOLDER_ITEM(folder->node->data);
543
544         rootpath = folder_item_get_path(item);
545         if (change_dir(rootpath) < 0) {
546                 g_free(rootpath);
547                 return -1;
548         }
549         g_free(rootpath);
550
551         mh_create_tree(folder);
552         mh_remove_missing_folder_items(folder);
553         mh_scan_tree_recursive(item);
554
555         return 0;
556 }
557
558 #define MAKE_DIR_IF_NOT_EXIST(dir) \
559 { \
560         if (!is_dir_exist(dir)) { \
561                 if (is_file_exist(dir)) { \
562                         g_warning("File `%s' already exists.\n" \
563                                     "Can't create folder.", dir); \
564                         return -1; \
565                 } \
566                 if (make_dir(dir) < 0) \
567                         return -1; \
568         } \
569 }
570
571 static gint mh_create_tree(Folder *folder)
572 {
573         gchar *rootpath;
574
575         g_return_val_if_fail(folder != NULL, -1);
576
577         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
578         rootpath = LOCAL_FOLDER(folder)->rootpath;
579         MAKE_DIR_IF_NOT_EXIST(rootpath);
580         CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
581         MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
582         MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
583         MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
584         MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
585         MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
586
587         return 0;
588 }
589
590 #undef MAKE_DIR_IF_NOT_EXIST
591
592 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
593 {
594         gchar *folder_path, *path;
595
596         g_return_val_if_fail(folder != NULL, NULL);
597         g_return_val_if_fail(item != NULL, NULL);
598
599         folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
600         g_return_val_if_fail(folder_path != NULL, NULL);
601
602         if (folder_path[0] == G_DIR_SEPARATOR) {
603                 if (item->path)
604                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
605                                            item->path, NULL);
606                 else
607                         path = g_strdup(folder_path);
608         } else {
609                 if (item->path)
610                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
611                                            folder_path, G_DIR_SEPARATOR_S,
612                                            item->path, NULL);
613                 else
614                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
615                                            folder_path, NULL);
616         }
617         g_free(folder_path);
618
619         return path;
620 }
621
622 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
623                                     const gchar *name)
624 {
625         gchar *path, *real_name;
626         gchar *fullpath;
627         FolderItem *new_item;
628         gchar *mh_sequences_filename;
629         FILE *mh_sequences_file;
630
631         g_return_val_if_fail(folder != NULL, NULL);
632         g_return_val_if_fail(parent != NULL, NULL);
633         g_return_val_if_fail(name != NULL, NULL);
634
635         path = folder_item_get_path(parent);
636         if (!is_dir_exist(path)) 
637                 if (make_dir_hier(path) != 0)
638                         return NULL;
639                 
640         real_name = mh_filename_from_utf8(name);
641         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
642         g_free(real_name);
643         g_free(path);
644
645         if (make_dir(fullpath) < 0) {
646                 g_free(fullpath);
647                 return NULL;
648         }
649
650         g_free(fullpath);
651
652         if (parent->path)
653                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
654                                    NULL);
655         else
656                 path = g_strdup(name);
657         new_item = folder_item_new(folder, name, path);
658         folder_item_append(parent, new_item);
659
660         g_free(path);
661
662         path = folder_item_get_path(new_item);
663         mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
664                                             ".mh_sequences", NULL);
665         if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
666                 fclose(mh_sequences_file);
667         }
668         g_free(mh_sequences_filename);
669         g_free(path);
670
671         return new_item;
672 }
673
674 static gint mh_rename_folder(Folder *folder, FolderItem *item,
675                              const gchar *name)
676 {
677         gchar *real_name;
678         gchar *oldpath;
679         gchar *dirname;
680         gchar *newpath, *utf8newpath;
681         gchar *paths[2];
682
683         g_return_val_if_fail(folder != NULL, -1);
684         g_return_val_if_fail(item != NULL, -1);
685         g_return_val_if_fail(item->path != NULL, -1);
686         g_return_val_if_fail(name != NULL, -1);
687
688         oldpath = folder_item_get_path(item);
689         if (!is_dir_exist(oldpath))
690                 make_dir_hier(oldpath);
691
692         dirname = g_path_get_dirname(oldpath);
693         real_name = mh_filename_from_utf8(name);
694         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
695         g_free(real_name);
696
697         if (rename(oldpath, newpath) < 0) {
698                 FILE_OP_ERROR(oldpath, "rename");
699                 g_free(oldpath);
700                 g_free(newpath);
701                 return -1;
702         }
703
704         g_free(oldpath);
705         g_free(newpath);
706
707         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
708                 dirname = g_path_get_dirname(item->path);
709                 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
710                                           name, NULL);
711                 g_free(dirname);
712         } else
713                 utf8newpath = g_strdup(name);
714
715         g_free(item->name);
716         item->name = g_strdup(name);
717
718         paths[0] = g_strdup(item->path);
719         paths[1] = utf8newpath;
720         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
721                         mh_rename_folder_func, paths);
722
723         g_free(paths[0]);
724         g_free(paths[1]);
725         return 0;
726 }
727
728 static gint mh_remove_folder(Folder *folder, FolderItem *item)
729 {
730         gchar *path;
731
732         g_return_val_if_fail(folder != NULL, -1);
733         g_return_val_if_fail(item != NULL, -1);
734         g_return_val_if_fail(item->path != NULL, -1);
735
736         path = folder_item_get_path(item);
737         if (remove_dir_recursive(path) < 0) {
738                 g_warning("can't remove directory `%s'\n", path);
739                 g_free(path);
740                 return -1;
741         }
742
743         g_free(path);
744         folder_item_remove(item);
745         return 0;
746 }
747
748 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
749 {
750         MsgInfo *msginfo;
751         MsgFlags flags;
752
753         g_return_val_if_fail(item != NULL, NULL);
754         g_return_val_if_fail(file != NULL, NULL);
755
756         flags.perm_flags = MSG_NEW|MSG_UNREAD;
757         flags.tmp_flags = 0;
758
759         if (item->stype == F_QUEUE) {
760                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
761         } else if (item->stype == F_DRAFT) {
762                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
763         }
764
765         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
766         if (!msginfo) return NULL;
767
768         msginfo->msgnum = atoi(file);
769         msginfo->folder = item;
770
771         return msginfo;
772 }
773
774 #if 0
775 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
776 {
777         gchar *entry;
778         gboolean result;
779
780         entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
781         result = is_dir_exist(entry);
782         g_free(entry);
783
784         return result;
785 }
786
787 /*
788  * check whether PATH is a Maildir style mailbox.
789  * This is the case if the 3 subdir: new, cur, tmp are existing.
790  * This functon assumes that entry is an directory
791  */
792 static gboolean mh_is_maildir(const gchar *path)
793 {
794         return mh_is_maildir_one(path, "new") &&
795                mh_is_maildir_one(path, "cur") &&
796                mh_is_maildir_one(path, "tmp");
797 }
798 #endif
799
800 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
801 {
802         FolderItem *item;
803         gchar *path;
804
805         g_return_val_if_fail(node->data != NULL, FALSE);
806
807         if (G_NODE_IS_ROOT(node))
808                 return FALSE;
809
810         item = FOLDER_ITEM(node->data);
811
812         path = folder_item_get_path(item);
813         if (!is_dir_exist(path)) {
814                 debug_print("folder '%s' not found. removing...\n", path);
815                 folder_item_remove(item);
816         }
817         g_free(path);
818
819         return FALSE;
820 }
821
822 static void mh_remove_missing_folder_items(Folder *folder)
823 {
824         g_return_if_fail(folder != NULL);
825
826         debug_print("searching missing folders...\n");
827
828         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
829                         mh_remove_missing_folder_items_func, folder);
830 }
831
832 static void mh_scan_tree_recursive(FolderItem *item)
833 {
834         Folder *folder;
835         DIR *dp;
836         struct dirent *d;
837         struct stat s;
838         gchar *real_path, *entry, *utf8entry, *utf8name;
839         gint n_msg = 0;
840
841         g_return_if_fail(item != NULL);
842         g_return_if_fail(item->folder != NULL);
843
844         folder = item->folder;
845
846         real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
847         dp = opendir(real_path);
848         if (!dp) {
849                 FILE_OP_ERROR(real_path, "opendir");
850                 return;
851         }
852         g_free(real_path);
853
854         debug_print("scanning %s ...\n",
855                     item->path ? item->path
856                     : LOCAL_FOLDER(item->folder)->rootpath);
857         if (folder->ui_func)
858                 folder->ui_func(folder, item, folder->ui_func_data);
859
860         while ((d = readdir(dp)) != NULL) {
861                 if (d->d_name[0] == '.') continue;
862
863                 utf8name = mh_filename_to_utf8(d->d_name);
864                 if (item->path)
865                         utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
866                                                 utf8name, NULL);
867                 else
868                         utf8entry = g_strdup(utf8name);
869                 entry = mh_filename_from_utf8(utf8entry);
870
871                 if (
872 #ifdef HAVE_DIRENT_D_TYPE
873                         d->d_type == DT_DIR ||
874                         (d->d_type == DT_UNKNOWN &&
875 #endif
876                         stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
877 #ifdef HAVE_DIRENT_D_TYPE
878                         )
879 #endif
880                    ) {
881                         FolderItem *new_item = NULL;
882                         GNode *node;
883
884 #if 0
885                         if (mh_is_maildir(entry)) {
886                                 g_free(entry);
887                                 g_free(utf8entry);
888                                 g_free(utf8name);
889                                 continue;
890                         }
891 #endif
892
893                         node = item->node;
894                         for (node = node->children; node != NULL; node = node->next) {
895                                 FolderItem *cur_item = FOLDER_ITEM(node->data);
896                                 if (!strcmp2(cur_item->path, entry)) {
897                                         new_item = cur_item;
898                                         break;
899                                 }
900                         }
901                         if (!new_item) {
902                                 debug_print("new folder '%s' found.\n", entry);
903                                 new_item = folder_item_new(folder, utf8name, utf8entry);
904                                 folder_item_append(item, new_item);
905                         }
906
907                         if (!item->path) {
908                                 if (!folder->inbox &&
909                                     !strcmp(d->d_name, INBOX_DIR)) {
910                                         new_item->stype = F_INBOX;
911                                         folder->inbox = new_item;
912                                 } else if (!folder->outbox &&
913                                            !strcmp(d->d_name, OUTBOX_DIR)) {
914                                         new_item->stype = F_OUTBOX;
915                                         folder->outbox = new_item;
916                                 } else if (!folder->draft &&
917                                            !strcmp(d->d_name, DRAFT_DIR)) {
918                                         new_item->stype = F_DRAFT;
919                                         folder->draft = new_item;
920                                 } else if (!folder->queue &&
921                                            !strcmp(d->d_name, QUEUE_DIR)) {
922                                         new_item->stype = F_QUEUE;
923                                         folder->queue = new_item;
924                                 } else if (!folder->trash &&
925                                            !strcmp(d->d_name, TRASH_DIR)) {
926                                         new_item->stype = F_TRASH;
927                                         folder->trash = new_item;
928                                 }
929                         }
930
931                         mh_scan_tree_recursive(new_item);
932                 } else if (to_number(d->d_name) != -1) n_msg++;
933
934                 g_free(entry);
935                 g_free(utf8entry);
936                 g_free(utf8name);
937         }
938
939         closedir(dp);
940
941         item->mtime = time(NULL);
942
943 /*
944         if (item->path) {
945                 gint new, unread, total, min, max;
946
947                 procmsg_get_mark_sum(item->path, &new, &unread, &total,
948                                      &min, &max, 0);
949                 if (n_msg > total) {
950                         new += n_msg - total;
951                         unread += n_msg - total;
952                 }
953                 item->new = new;
954                 item->unread = unread;
955                 item->total = n_msg;
956         }
957 */
958 }
959
960 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
961 {
962         FolderItem *item = node->data;
963         gchar **paths = data;
964         const gchar *oldpath = paths[0];
965         const gchar *newpath = paths[1];
966         gchar *base;
967         gchar *new_itempath;
968         gint oldpathlen;
969
970         oldpathlen = strlen(oldpath);
971         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
972                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
973                 return TRUE;
974         }
975
976         base = item->path + oldpathlen;
977         while (*base == G_DIR_SEPARATOR) base++;
978         if (*base == '\0')
979                 new_itempath = g_strdup(newpath);
980         else
981                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
982                                            NULL);
983         g_free(item->path);
984         item->path = new_itempath;
985
986         return FALSE;
987 }
988
989 static gchar *mh_filename_from_utf8(const gchar *path)
990 {
991         const gchar *src_codeset = CS_UTF_8;
992         const gchar *dest_codeset = conv_get_locale_charset_str();
993         gchar *real_path;
994
995         real_path = conv_codeset_strdup(path, src_codeset, dest_codeset);
996         if (!real_path) {
997                 g_warning("mh_filename_from_utf8: faild to convert character set\n");
998                 /* FIXME: show dialog? */
999                 real_path = g_strdup(path);
1000         }
1001
1002         return real_path;
1003 }
1004
1005 static gchar *mh_filename_to_utf8(const gchar *path)
1006 {
1007         const gchar *src_codeset = conv_get_locale_charset_str();
1008         const gchar *dest_codeset = CS_UTF_8;
1009         gchar *utf8path;
1010
1011         utf8path = conv_codeset_strdup(path, src_codeset, dest_codeset);
1012         if (!utf8path) {
1013                 g_warning("mh_filename_to_utf8: faild to convert character set\n");
1014                 /* FIXME: show dialog? */
1015                 utf8path = g_strdup(path);
1016         }
1017
1018         return utf8path;
1019 }