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