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