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