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