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