22ae8821ede2ea584050b468dd7d7c8870b22831
[claws.git] / src / mh.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 GSList  *mh_get_uncached_msgs            (GHashTable     *msg_table,
47                                                  FolderItem     *item);
48 static MsgInfo *mh_parse_msg                    (const gchar    *file,
49                                                  FolderItem     *item);
50 static void     mh_scan_tree_recursive          (FolderItem     *item);
51
52 static gboolean mh_rename_folder_func           (GNode          *node,
53                                                  gpointer        data);
54
55
56 GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
57 {
58         GSList *mlist;
59         GHashTable *msg_table;
60         gchar *path;
61         struct stat s;
62         gboolean scan_new = TRUE;
63 #ifdef MEASURE_TIME
64         struct timeval tv_before, tv_after, tv_result;
65
66         gettimeofday(&tv_before, NULL);
67 #endif
68
69         g_return_val_if_fail(item != NULL, NULL);
70
71         path = folder_item_get_path(item);
72
73         if (stat(path, &s) < 0) {
74                 FILE_OP_ERROR(path, "stat");
75         } else {
76                 if (item->mtime == s.st_mtime) {
77                         debug_print("Folder is not modified.\n");
78                         scan_new = FALSE;
79                 } else
80                         item->mtime = s.st_mtime;
81         }
82         g_free(path);
83
84         if (use_cache && !scan_new) {
85                 mlist = procmsg_read_cache(item, FALSE);
86                 if (!mlist)
87                         mlist = mh_get_uncached_msgs(NULL, item);
88         } else if (use_cache) {
89                 GSList *newlist;
90
91                 mlist = procmsg_read_cache(item, TRUE);
92                 msg_table = procmsg_msg_hash_table_create(mlist);
93
94                 newlist = mh_get_uncached_msgs(msg_table, item);
95                 if (msg_table)
96                         g_hash_table_destroy(msg_table);
97
98                 mlist = g_slist_concat(mlist, newlist);
99         } else
100                 mlist = mh_get_uncached_msgs(NULL, item);
101
102         procmsg_set_flags(mlist, item);
103
104 #ifdef MEASURE_TIME
105         gettimeofday(&tv_after, NULL);
106
107         timersub(&tv_after, &tv_before, &tv_result);
108         g_print("mh_get_msg_list: %s: elapsed time: %ld.%06ld sec\n",
109                 item->path, tv_result.tv_sec, tv_result.tv_usec);
110 #endif
111
112         return mlist;
113 }
114
115 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
116 {
117         gchar *path;
118         gchar *file;
119
120         g_return_val_if_fail(item != NULL, NULL);
121         g_return_val_if_fail(num > 0 && num <= item->last_num, NULL);
122
123         path = folder_item_get_path(item);
124
125         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
126         g_free(path);
127         if (!is_file_exist(file)) {
128                 g_free(file);
129                 return NULL;
130         }
131
132         return file;
133 }
134
135 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
136                 gboolean remove_source)
137 {
138         gchar *destpath;
139         gchar *destfile;
140
141         g_return_val_if_fail(dest != NULL, -1);
142         g_return_val_if_fail(file != NULL, -1);
143
144         if (dest->last_num < 0) {
145                 mh_scan_folder(folder, dest);
146                 if (dest->last_num < 0) return -1;
147         }
148
149         destpath = folder_item_get_path(dest);
150         g_return_val_if_fail(destpath != NULL, -1);
151         if (!is_dir_exist(destpath))
152                 make_dir_hier(destpath);
153
154         destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
155                                    dest->last_num + 1);
156
157         if (link(file, destfile) < 0) {
158                 if (EXDEV == errno) {
159                         if (copy_file(file, destfile) < 0) {
160                                 g_warning(_("can't copy message %s to %s\n"),
161                                           file, destfile);
162                                 g_free(destfile);
163                                 return -1;
164                         }
165                 } else {
166                         FILE_OP_ERROR(file, "link");
167                         g_free(destfile);
168                         return -1;
169                 }
170         }
171
172         if (remove_source) {
173                 if (unlink(file) < 0)
174                         FILE_OP_ERROR(file, "unlink");
175         }
176
177         g_free(destfile);
178         dest->last_num++;
179         return dest->last_num;
180 }
181
182 gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
183 {
184         gchar *destdir;
185         gchar *srcfile;
186         gchar *destfile;
187         FILE *fp;
188
189         g_return_val_if_fail(dest != NULL, -1);
190         g_return_val_if_fail(msginfo != NULL, -1);
191
192         if (msginfo->folder == dest) {
193                 g_warning(_("the src folder is identical to the dest.\n"));
194                 return -1;
195         }
196
197         if (dest->last_num < 0) {
198                 mh_scan_folder(folder, dest);
199                 if (dest->last_num < 0) return -1;
200         }
201
202         destdir = folder_item_get_path(dest);
203
204         if (!is_dir_exist(destdir))
205                 make_dir_hier(destdir);
206
207         if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
208                 g_warning(_("Can't open mark file.\n"));
209
210         debug_print(_("Moving message %s%c%d to %s ...\n"),
211                     msginfo->folder->path, G_DIR_SEPARATOR,
212                     msginfo->msgnum, dest->path);
213         srcfile = procmsg_get_message_file_path(msginfo);
214         destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
215                                    dest->last_num + 1);
216         g_free(destdir);
217
218         if (is_file_exist(destfile)) {
219                 g_warning(_("%s already exists."), destfile);
220                 g_free(srcfile);
221                 g_free(destfile);
222                 if (fp) fclose(fp);
223                 return -1;
224         }
225
226         if (rename(srcfile, destfile) < 0) {
227                 if (EXDEV == errno) {
228                         if (copy_file(srcfile, destfile) < 0) {
229                                 g_free(srcfile);
230                                 g_free(destfile);
231                                 return -1;
232                         }
233                         unlink(srcfile);
234                 } else {
235                         FILE_OP_ERROR(srcfile, "rename");
236                         g_free(srcfile);
237                         g_free(destfile);
238                         if (fp) fclose(fp);
239                         return -1;
240                 }
241         }
242
243         g_free(srcfile);
244         g_free(destfile);
245         dest->last_num++;
246
247         if (fp) {
248                 MsgInfo newmsginfo;
249
250                 newmsginfo.msgnum = dest->last_num;
251                 newmsginfo.flags = msginfo->flags;
252                 if (dest->stype == F_OUTBOX ||
253                     dest->stype == F_QUEUE  ||
254                     dest->stype == F_DRAFT  ||
255                     dest->stype == F_TRASH)
256                         MSG_UNSET_FLAGS(newmsginfo.flags,
257                                         MSG_NEW|MSG_UNREAD|MSG_DELETED);
258
259                 procmsg_write_flags(&newmsginfo, fp);
260                 fclose(fp);
261         }
262
263         return dest->last_num;
264 }
265
266 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
267 {
268         gchar *destdir;
269         gchar *srcfile;
270         gchar *destfile;
271         FILE *fp;
272         GSList *cur;
273         MsgInfo *msginfo;
274
275         g_return_val_if_fail(dest != NULL, -1);
276         g_return_val_if_fail(msglist != NULL, -1);
277
278         if (dest->last_num < 0) {
279                 mh_scan_folder(folder, dest);
280                 if (dest->last_num < 0) return -1;
281         }
282
283         destdir = folder_item_get_path(dest);
284         if (!is_dir_exist(destdir))
285                 make_dir_hier(destdir);
286
287         if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
288                 g_warning(_("Can't open mark file.\n"));
289
290         for (cur = msglist; cur != NULL; cur = cur->next) {
291                 msginfo = (MsgInfo *)cur->data;
292
293                 if (msginfo->folder == dest) {
294                         g_warning(_("the src folder is identical to the dest.\n"));
295                         continue;
296                 }
297                 debug_print(_("Moving message %s%c%d to %s ...\n"),
298                             msginfo->folder->path, G_DIR_SEPARATOR,
299                             msginfo->msgnum, dest->path);
300
301                 srcfile = procmsg_get_message_file_path(msginfo);
302                 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
303                                            dest->last_num + 1);
304
305                 if (is_file_exist(destfile)) {
306                         g_warning(_("%s already exists."), destfile);
307                         g_free(srcfile);
308                         g_free(destfile);
309                         break;
310                 }
311
312                 if (rename(srcfile, destfile) < 0) {
313                         if (EXDEV == errno) {
314                                 if (copy_file(srcfile, destfile) < 0) {
315                                         g_free(srcfile);
316                                         g_free(destfile);
317                                         break;
318                                 }
319                                 unlink(srcfile);
320                         } else {
321                                 FILE_OP_ERROR(srcfile, "rename");
322                                 g_free(srcfile);
323                                 g_free(destfile);
324                                 break;
325                         }
326                 }
327
328                 g_free(srcfile);
329                 g_free(destfile);
330                 dest->last_num++;
331
332                 if (fp) {
333                         MsgInfo newmsginfo;
334
335                         newmsginfo.msgnum = dest->last_num;
336                         newmsginfo.flags = msginfo->flags;
337                         if (dest->stype == F_OUTBOX ||
338                             dest->stype == F_QUEUE  ||
339                             dest->stype == F_DRAFT  ||
340                             dest->stype == F_TRASH)
341                                 MSG_UNSET_FLAGS(newmsginfo.flags,
342                                                 MSG_NEW|MSG_UNREAD|MSG_DELETED);
343
344                         procmsg_write_flags(&newmsginfo, fp);
345                 }
346         }
347
348         g_free(destdir);
349         if (fp) fclose(fp);
350
351         return dest->last_num;
352 }
353
354 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
355 {
356         gchar *destdir;
357         gchar *srcfile;
358         gchar *destfile;
359         FILE *fp;
360
361         g_return_val_if_fail(dest != NULL, -1);
362         g_return_val_if_fail(msginfo != NULL, -1);
363
364         if (msginfo->folder == dest) {
365                 g_warning(_("the src folder is identical to the dest.\n"));
366                 return -1;
367         }
368
369         if (dest->last_num < 0) {
370                 mh_scan_folder(folder, dest);
371                 if (dest->last_num < 0) return -1;
372         }
373
374         destdir = folder_item_get_path(dest);
375         if (!is_dir_exist(destdir))
376                 make_dir_hier(destdir);
377
378         if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
379                 g_warning(_("Can't open mark file.\n"));
380
381         debug_print(_("Copying message %s%c%d to %s ...\n"),
382                     msginfo->folder->path, G_DIR_SEPARATOR,
383                     msginfo->msgnum, dest->path);
384         srcfile = procmsg_get_message_file_path(msginfo);
385         destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
386                                    dest->last_num + 1);
387         g_free(destdir);
388
389         if (is_file_exist(destfile)) {
390                 g_warning(_("%s already exists."), destfile);
391                 g_free(srcfile);
392                 g_free(destfile);
393                 if (fp) fclose(fp);
394                 return -1;
395         }
396
397         if (copy_file(srcfile, destfile) < 0) {
398                 FILE_OP_ERROR(srcfile, "copy");
399                 g_free(srcfile);
400                 g_free(destfile);
401                 if (fp) fclose(fp);
402                 return -1;
403         }
404
405         g_free(srcfile);
406         g_free(destfile);
407         dest->last_num++;
408
409         if (fp) {
410                 MsgInfo newmsginfo;
411
412                 newmsginfo.msgnum = dest->last_num;
413                 newmsginfo.flags = msginfo->flags;
414                 if (dest->stype == F_OUTBOX ||
415                     dest->stype == F_QUEUE  ||
416                     dest->stype == F_DRAFT  ||
417                     dest->stype == F_TRASH)
418                         MSG_UNSET_FLAGS(newmsginfo.flags,
419                                         MSG_NEW|MSG_UNREAD|MSG_DELETED);
420                 procmsg_write_flags(&newmsginfo, fp);
421                 fclose(fp);
422         }
423
424         return dest->last_num;
425 }
426
427 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
428 {
429         gchar *destdir;
430         gchar *srcfile;
431         gchar *destfile;
432         FILE *fp;
433         GSList *cur;
434         MsgInfo *msginfo;
435
436         g_return_val_if_fail(dest != NULL, -1);
437         g_return_val_if_fail(msglist != NULL, -1);
438
439         if (dest->last_num < 0) {
440                 mh_scan_folder(folder, dest);
441                 if (dest->last_num < 0) return -1;
442         }
443
444         destdir = folder_item_get_path(dest);
445         if (!is_dir_exist(destdir))
446                 make_dir_hier(destdir);
447
448         if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
449                 g_warning(_("Can't open mark file.\n"));
450
451         for (cur = msglist; cur != NULL; cur = cur->next) {
452                 msginfo = (MsgInfo *)cur->data;
453
454                 if (msginfo->folder == dest) {
455                         g_warning(_("the src folder is identical to the dest.\n"));
456                         continue;
457                 }
458                 debug_print(_("Copying message %s%c%d to %s ...\n"),
459                             msginfo->folder->path, G_DIR_SEPARATOR,
460                             msginfo->msgnum, dest->path);
461
462                 srcfile = procmsg_get_message_file_path(msginfo);
463                 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
464                                            dest->last_num + 1);
465
466                 if (is_file_exist(destfile)) {
467                         g_warning(_("%s already exists."), destfile);
468                         g_free(srcfile);
469                         g_free(destfile);
470                         break;
471                 }
472
473                 if (copy_file(srcfile, destfile) < 0) {
474                         FILE_OP_ERROR(srcfile, "copy");
475                         g_free(srcfile);
476                         g_free(destfile);
477                         break;
478                 }
479
480                 g_free(srcfile);
481                 g_free(destfile);
482                 dest->last_num++;
483
484                 if (fp) {
485                         MsgInfo newmsginfo;
486
487                         newmsginfo.msgnum = dest->last_num;
488                         newmsginfo.flags = msginfo->flags;
489                         if (dest->stype == F_OUTBOX ||
490                             dest->stype == F_QUEUE  ||
491                             dest->stype == F_DRAFT  ||
492                             dest->stype == F_TRASH)
493                                 MSG_UNSET_FLAGS(newmsginfo.flags,
494                                                 MSG_NEW|MSG_UNREAD|MSG_DELETED);
495                         procmsg_write_flags(&newmsginfo, fp);
496                 }
497         }
498
499         g_free(destdir);
500         if (fp) fclose(fp);
501
502         return dest->last_num;
503 }
504
505 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
506 {
507         gchar *file;
508
509         g_return_val_if_fail(item != NULL, -1);
510
511         file = mh_fetch_msg(folder, item, num);
512         g_return_val_if_fail(file != NULL, -1);
513
514         if (unlink(file) < 0) {
515                 FILE_OP_ERROR(file, "unlink");
516                 g_free(file);
517                 return -1;
518         }
519
520         g_free(file);
521         return 0;
522 }
523
524 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
525 {
526         gchar *path;
527         DIR *dp;
528         struct dirent *d;
529
530         g_return_val_if_fail(item != NULL, -1);
531
532         path = folder_item_get_path(item);
533         g_return_val_if_fail(path != NULL, -1);
534         if (change_dir(path) < 0) {
535                 g_free(path);
536                 return -1;
537         }
538         g_free(path);
539
540         if ((dp = opendir(".")) == NULL) {
541                 FILE_OP_ERROR(item->path, "opendir");
542                 return -1;
543         }
544
545         while ((d = readdir(dp)) != NULL) {
546                 if (to_number(d->d_name) < 0) continue;
547                 if (unlink(d->d_name) < 0)
548                         FILE_OP_ERROR(d->d_name, "unlink");
549         }
550
551         closedir(dp);
552         return 0;
553 }
554
555 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
556 {
557         struct stat s;
558
559         if (stat(itos(msginfo->msgnum), &s) < 0 ||
560             msginfo->size  != s.st_size ||
561             msginfo->mtime != s.st_mtime)
562                 return TRUE;
563
564         return FALSE;
565 }
566
567 void mh_scan_folder(Folder *folder, FolderItem *item)
568 {
569         gchar *path;
570         DIR *dp;
571         struct dirent *d;
572         struct stat s;
573         gint max = 0;
574         gint num;
575         gint n_msg = 0;
576
577         g_return_if_fail(item != NULL);
578
579         path = folder_item_get_path(item);
580         g_return_if_fail(path != NULL);
581         if (change_dir(path) < 0) {
582                 g_free(path);
583                 return;
584         }
585         g_free(path);
586
587         if ((dp = opendir(".")) == NULL) {
588                 FILE_OP_ERROR(item->path, "opendir");
589                 return;
590         }
591
592         if (folder->ui_func)
593                 folder->ui_func(folder, item, folder->ui_func_data);
594
595         while ((d = readdir(dp)) != NULL) {
596                 if ((num = to_number(d->d_name)) >= 0 &&
597                     stat(d->d_name, &s) == 0 &&
598                     S_ISREG(s.st_mode)) {
599                         n_msg++;
600                         if (max < num)
601                                 max = num;
602                 }
603         }
604
605         closedir(dp);
606
607         if (n_msg == 0)
608                 item->new = item->unread = item->total = 0;
609         else {
610                 gint new, unread, total;
611
612                 procmsg_get_mark_sum(".", &new, &unread, &total);
613                 if (n_msg > total) {
614                         new += n_msg - total;
615                         unread += n_msg - total;
616                 }
617                 item->new = new;
618                 item->unread = unread;
619                 item->total = n_msg;
620         }
621
622         debug_print(_("Last number in dir %s = %d\n"), item->path, max);
623         item->last_num = max;
624 }
625
626 void mh_scan_tree(Folder *folder)
627 {
628         FolderItem *item;
629         gchar *rootpath;
630
631         g_return_if_fail(folder != NULL);
632
633         folder_tree_destroy(folder);
634         item = folder_item_new(folder->name, NULL);
635         item->folder = folder;
636         folder->node = g_node_new(item);
637
638         rootpath = folder_item_get_path(item);
639         if (change_dir(rootpath) < 0) {
640                 g_free(rootpath);
641                 return;
642         }
643         g_free(rootpath);
644
645         mh_scan_tree_recursive(item);
646 }
647
648 #define MAKE_DIR_IF_NOT_EXIST(dir) \
649 { \
650         if (!is_dir_exist(dir)) { \
651                 if (is_file_exist(dir)) { \
652                         g_warning(_("File `%s' already exists.\n" \
653                                     "Can't create folder."), dir); \
654                         return -1; \
655                 } \
656                 if (mkdir(dir, S_IRWXU) < 0) { \
657                         FILE_OP_ERROR(dir, "mkdir"); \
658                         return -1; \
659                 } \
660                 if (chmod(dir, S_IRWXU) < 0) \
661                         FILE_OP_ERROR(dir, "chmod"); \
662         } \
663 }
664
665 gint mh_create_tree(Folder *folder)
666 {
667         gchar *rootpath;
668
669         g_return_val_if_fail(folder != NULL, -1);
670
671         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
672         rootpath = LOCAL_FOLDER(folder)->rootpath;
673         MAKE_DIR_IF_NOT_EXIST(rootpath);
674         CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
675         MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
676         MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
677         MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
678         MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
679         MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
680
681         return 0;
682 }
683
684 #undef MAKE_DIR_IF_NOT_EXIST
685
686 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
687                              const gchar *name)
688 {
689         gchar *path;
690         gchar *fullpath;
691         FolderItem *new_item;
692
693         g_return_val_if_fail(folder != NULL, NULL);
694         g_return_val_if_fail(parent != NULL, NULL);
695         g_return_val_if_fail(name != NULL, NULL);
696
697         path = folder_item_get_path(parent);
698         if (!is_dir_exist(path))
699                 make_dir_hier(path);
700
701         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
702         g_free(path);
703
704         if (mkdir(fullpath, S_IRWXU) < 0) {
705                 FILE_OP_ERROR(fullpath, "mkdir");
706                 g_free(fullpath);
707                 return NULL;
708         }
709         if (chmod(fullpath, S_IRWXU) < 0)
710                 FILE_OP_ERROR(fullpath, "chmod");
711
712         g_free(fullpath);
713
714         if (parent->path)
715                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
716                                    NULL);
717         else
718                 path = g_strdup(name);
719         new_item = folder_item_new(name, path);
720         folder_item_append(parent, new_item);
721         g_free(path);
722
723         return new_item;
724 }
725
726 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
727 {
728         gchar *oldpath;
729         gchar *dirname;
730         gchar *newpath;
731         GNode *node;
732         gchar *paths[2];
733
734         g_return_val_if_fail(folder != NULL, -1);
735         g_return_val_if_fail(item != NULL, -1);
736         g_return_val_if_fail(item->path != NULL, -1);
737         g_return_val_if_fail(name != NULL, -1);
738
739         oldpath = folder_item_get_path(item);
740         if (!is_dir_exist(oldpath))
741                 make_dir_hier(oldpath);
742
743         dirname = g_dirname(oldpath);
744         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
745         g_free(dirname);
746
747         if (rename(oldpath, newpath) < 0) {
748                 FILE_OP_ERROR(oldpath, "rename");
749                 g_free(oldpath);
750                 g_free(newpath);
751                 return -1;
752         }
753
754         g_free(oldpath);
755         g_free(newpath);
756
757         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
758                 dirname = g_dirname(item->path);
759                 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
760                 g_free(dirname);
761         } else
762                 newpath = g_strdup(name);
763
764         g_free(item->name);
765         item->name = g_strdup(name);
766
767         node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
768                            item);
769         paths[0] = g_strdup(item->path);
770         paths[1] = newpath;
771         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
772                         mh_rename_folder_func, paths);
773
774         g_free(paths[0]);
775         g_free(paths[1]);
776         return 0;
777 }
778
779 gint mh_remove_folder(Folder *folder, FolderItem *item)
780 {
781         gchar *path;
782
783         g_return_val_if_fail(folder != NULL, -1);
784         g_return_val_if_fail(item != NULL, -1);
785         g_return_val_if_fail(item->path != NULL, -1);
786
787         path = folder_item_get_path(item);
788         if (remove_dir_recursive(path) < 0) {
789                 g_warning("can't remove directory `%s'\n", path);
790                 g_free(path);
791                 return -1;
792         }
793
794         g_free(path);
795         folder_item_remove(item);
796         return 0;
797 }
798
799
800 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
801 {
802         gchar *path;
803         DIR *dp;
804         struct dirent *d;
805         struct stat s;
806         GSList *newlist = NULL;
807         GSList *last = NULL;
808         MsgInfo *msginfo;
809         gint n_newmsg = 0;
810         gint num;
811
812         g_return_val_if_fail(item != NULL, NULL);
813
814         path = folder_item_get_path(item);
815         g_return_val_if_fail(path != NULL, NULL);
816         if (change_dir(path) < 0) {
817                 g_free(path);
818                 return NULL;
819         }
820         g_free(path);
821
822         if ((dp = opendir(".")) == NULL) {
823                 FILE_OP_ERROR(item->path, "opendir");
824                 return NULL;
825         }
826
827         debug_print(_("\tSearching uncached messages... "));
828
829         if (msg_table) {
830                 while ((d = readdir(dp)) != NULL) {
831                         if ((num = to_number(d->d_name)) < 0) continue;
832                         if (stat(d->d_name, &s) < 0) {
833                                 FILE_OP_ERROR(d->d_name, "stat");
834                                 continue;
835                         }
836                         if (!S_ISREG(s.st_mode)) continue;
837
838                         msginfo = g_hash_table_lookup
839                                 (msg_table, GUINT_TO_POINTER(num));
840
841                         if (!msginfo) {
842                                 /* not found in the cache (uncached message) */
843                                 msginfo = mh_parse_msg(d->d_name, item);
844                                 if (!msginfo) continue;
845
846                                 if (!newlist)
847                                         last = newlist =
848                                                 g_slist_append(NULL, msginfo);
849                                 else {
850                                         last = g_slist_append(last, msginfo);
851                                         last = last->next;
852                                 }
853                                 n_newmsg++;
854                         }
855                 }
856         } else {
857                 /* discard all previous cache */
858                 while ((d = readdir(dp)) != NULL) {
859                         if (to_number(d->d_name) < 0) continue;
860                         if (stat(d->d_name, &s) < 0) {
861                                 FILE_OP_ERROR(d->d_name, "stat");
862                                 continue;
863                         }
864                         if (!S_ISREG(s.st_mode)) continue;
865
866                         msginfo = mh_parse_msg(d->d_name, item);
867                         if (!msginfo) continue;
868
869                         if (!newlist)
870                                 last = newlist = g_slist_append(NULL, msginfo);
871                         else {
872                                 last = g_slist_append(last, msginfo);
873                                 last = last->next;
874                         }
875                         n_newmsg++;
876                 }
877         }
878
879         closedir(dp);
880
881         if (n_newmsg)
882                 debug_print(_("%d uncached message(s) found.\n"), n_newmsg);
883         else
884                 debug_print(_("done.\n"));
885
886         /* sort new messages in numerical order */
887         if (newlist) {
888                 debug_print(_("\tSorting uncached messages in numerical order... "));
889                 newlist = g_slist_sort
890                         (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
891                 debug_print(_("done.\n"));
892         }
893
894         return newlist;
895 }
896
897 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
898 {
899         struct stat s;
900         MsgInfo *msginfo;
901         MsgFlags flags = MSG_NEW|MSG_UNREAD;
902
903         g_return_val_if_fail(item != NULL, NULL);
904         g_return_val_if_fail(file != NULL, NULL);
905
906         if (item->stype == F_QUEUE) {
907                 MSG_SET_FLAGS(flags, MSG_QUEUED);
908         } else if (item->stype == F_DRAFT) {
909                 MSG_SET_FLAGS(flags, MSG_DRAFT);
910         }
911
912         msginfo = procheader_parse(file, flags, FALSE);
913         if (!msginfo) return NULL;
914
915         msginfo->msgnum = atoi(file);
916         msginfo->folder = item;
917
918         if (stat(file, &s) < 0) {
919                 FILE_OP_ERROR(file, "stat");
920                 msginfo->size = 0;
921                 msginfo->mtime = 0;
922         } else {
923                 msginfo->size = s.st_size;
924                 msginfo->mtime = s.st_mtime;
925         }
926
927         return msginfo;
928 }
929
930 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
931 {
932         gchar *entry;
933         gboolean result;
934
935         entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
936         result = is_dir_exist(entry);
937         g_free(entry);
938
939         return result;
940 }
941
942 /*
943  * check whether PATH is a Maildir style mailbox.
944  * This is the case if the 3 subdir: new, cur, tmp are existing.
945  * This functon assumes that entry is an directory
946  */
947 static gboolean mh_is_maildir(const gchar *path)
948 {
949         return mh_is_maildir_one(path, "new") &&
950                mh_is_maildir_one(path, "cur") &&
951                mh_is_maildir_one(path, "tmp");
952 }
953
954 static void mh_scan_tree_recursive(FolderItem *item)
955 {
956         DIR *dp;
957         struct dirent *d;
958         struct stat s;
959         gchar *entry;
960         gint n_msg = 0;
961
962         g_return_if_fail(item != NULL);
963         g_return_if_fail(item->folder != NULL);
964
965         dp = opendir(item->path ? item->path : ".");
966         if (!dp) {
967                 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
968                 return;
969         }
970
971         debug_print("scanning %s ...\n",
972                     item->path ? item->path
973                     : LOCAL_FOLDER(item->folder)->rootpath);
974         if (item->folder->ui_func)
975                 item->folder->ui_func(item->folder, item,
976                                       item->folder->ui_func_data);
977
978         while ((d = readdir(dp)) != NULL) {
979                 if (d->d_name[0] == '.') continue;
980
981                 if (item->path)
982                         entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
983                                             d->d_name, NULL);
984                 else
985                         entry = g_strdup(d->d_name);
986
987                 if (stat(entry, &s) < 0) {
988                         FILE_OP_ERROR(entry, "stat");
989                         g_free(entry);
990                         continue;
991                 }
992
993                 if (S_ISDIR(s.st_mode)) {
994                         FolderItem *new_item;
995
996                         if (mh_is_maildir(entry)) {
997                                 g_free(entry);
998                                 continue;
999                         }
1000
1001                         new_item = folder_item_new(d->d_name, entry);
1002                         folder_item_append(item, new_item);
1003                         if (!item->path) {
1004                                 if (!strcmp(d->d_name, "inbox")) {
1005                                         new_item->stype = F_INBOX;
1006                                         item->folder->inbox = new_item;
1007                                 } else if (!strcmp(d->d_name, "outbox")) {
1008                                         new_item->stype = F_OUTBOX;
1009                                         item->folder->outbox = new_item;
1010                                 } else if (!strcmp(d->d_name, "draft")) {
1011                                         new_item->stype = F_DRAFT;
1012                                         item->folder->draft = new_item;
1013                                 } else if (!strcmp(d->d_name, "queue")) {
1014                                         new_item->stype = F_QUEUE;
1015                                         item->folder->queue = new_item;
1016                                 } else if (!strcmp(d->d_name, "trash")) {
1017                                         new_item->stype = F_TRASH;
1018                                         item->folder->trash = new_item;
1019                                 }
1020                         }
1021                         mh_scan_tree_recursive(new_item);
1022                 } else if (to_number(d->d_name) != -1) n_msg++;
1023
1024                 g_free(entry);
1025         }
1026
1027         closedir(dp);
1028
1029         if (item->path) {
1030                 gint new, unread, total;
1031
1032                 procmsg_get_mark_sum(item->path, &new, &unread, &total);
1033                 if (n_msg > total) {
1034                         new += n_msg - total;
1035                         unread += n_msg - total;
1036                 }
1037                 item->new = new;
1038                 item->unread = unread;
1039                 item->total = n_msg;
1040         }
1041 }
1042
1043 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1044 {
1045         FolderItem *item = node->data;
1046         gchar **paths = data;
1047         const gchar *oldpath = paths[0];
1048         const gchar *newpath = paths[1];
1049         gchar *base;
1050         gchar *new_itempath;
1051         gint oldpathlen;
1052
1053         oldpathlen = strlen(oldpath);
1054         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1055                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1056                 return TRUE;
1057         }
1058
1059         base = item->path + oldpathlen;
1060         while (*base == G_DIR_SEPARATOR) base++;
1061         if (*base == '\0')
1062                 new_itempath = g_strdup(newpath);
1063         else
1064                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1065                                            NULL);
1066         g_free(item->path);
1067         item->path = new_itempath;
1068
1069         return FALSE;
1070 }