a888a88e048ba00309f180f2f5117c993840cde2
[claws.git] / src / mh.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <dirent.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #undef MEASURE_TIME
34
35 #ifdef MEASURE_TIME
36 #  include <sys/time.h>
37 #endif
38
39 #include "intl.h"
40 #include "folder.h"
41 #include "mh.h"
42 #include "procmsg.h"
43 #include "procheader.h"
44 #include "utils.h"
45
46 static void     mh_folder_init                  (Folder         *folder,
47                                                  const gchar    *name,
48                                                  const gchar    *path);
49
50 GSList  *mh_get_msg_list        (Folder         *folder,
51                                  FolderItem     *item,
52                                  gboolean        use_cache);
53 gchar   *mh_fetch_msg           (Folder         *folder,
54                                  FolderItem     *item,
55                                  gint            num);
56 MsgInfo   *mh_fetch_msginfo     (Folder         *folder,
57                                  FolderItem     *item,
58                                  gint            num);
59 gint     mh_add_msg             (Folder         *folder,
60                                  FolderItem     *dest,
61                                  const gchar    *file,
62                                  gboolean        remove_source);
63 gint     mh_move_msg            (Folder         *folder,
64                                  FolderItem     *dest,
65                                  MsgInfo        *msginfo);
66 gint     mh_move_msgs_with_dest (Folder         *folder,
67                                  FolderItem     *dest,
68                                  GSList         *msglist);
69 gint     mh_copy_msg            (Folder         *folder,
70                                  FolderItem     *dest,
71                                  MsgInfo        *msginfo);
72 gint     mh_copy_msgs_with_dest (Folder         *folder,
73                                  FolderItem     *dest,
74                                  GSList         *msglist);
75 gint     mh_remove_msg          (Folder         *folder,
76                                  FolderItem     *item,
77                                  gint            num);
78 gint     mh_remove_all_msg      (Folder         *folder,
79                                  FolderItem     *item);
80 gboolean mh_is_msg_changed      (Folder         *folder,
81                                  FolderItem     *item,
82                                  MsgInfo        *msginfo);
83
84 gint    mh_scan_folder          (Folder         *folder,
85                                  FolderItem     *item);
86 GSList *mh_get_num_list         (Folder         *folder,
87                                  FolderItem     *item);
88 void    mh_scan_tree            (Folder         *folder);
89
90 gint    mh_create_tree          (Folder         *folder);
91 FolderItem *mh_create_folder    (Folder         *folder,
92                                  FolderItem     *parent,
93                                  const gchar    *name);
94 gint    mh_rename_folder        (Folder         *folder,
95                                  FolderItem     *item,
96                                  const gchar    *name);
97 gint    mh_remove_folder        (Folder         *folder,
98                                  FolderItem     *item);
99
100 gchar   *mh_get_new_msg_filename                (FolderItem     *dest);
101
102 static GSList  *mh_get_uncached_msgs            (GHashTable     *msg_table,
103                                                  FolderItem     *item);
104 static MsgInfo *mh_parse_msg                    (const gchar    *file,
105                                                  FolderItem     *item);
106 static void     mh_scan_tree_recursive          (FolderItem     *item, 
107                                                  GHashTable *pptable);
108
109 static gboolean mh_rename_folder_func           (GNode          *node,
110                                                  gpointer        data);
111
112
113 Folder *mh_folder_new(const gchar *name, const gchar *path)
114 {
115         Folder *folder;
116
117         folder = (Folder *)g_new0(MHFolder, 1);
118         mh_folder_init(folder, name, path);
119
120         return folder;
121 }
122
123 void mh_folder_destroy(MHFolder *folder)
124 {
125         folder_local_folder_destroy(LOCAL_FOLDER(folder));
126 }
127
128 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
129 {
130         folder_local_folder_init(folder, name, path);
131
132         folder->type = F_MH;
133
134 /*
135         folder->get_msg_list        = mh_get_msg_list;
136 */
137         folder->fetch_msg           = mh_fetch_msg;
138         folder->fetch_msginfo       = mh_fetch_msginfo;
139         folder->add_msg             = mh_add_msg;
140         folder->move_msg            = mh_move_msg;
141         folder->move_msgs_with_dest = mh_move_msgs_with_dest;
142         folder->copy_msg            = mh_copy_msg;
143         folder->copy_msgs_with_dest = mh_copy_msgs_with_dest;
144         folder->remove_msg          = mh_remove_msg;
145         folder->remove_all_msg      = mh_remove_all_msg;
146         folder->is_msg_changed      = mh_is_msg_changed;
147 /*
148         folder->scan                = mh_scan_folder;
149 */
150         folder->get_num_list        = mh_get_num_list;
151         folder->scan_tree           = mh_scan_tree;
152         folder->create_tree         = mh_create_tree;
153         folder->create_folder       = mh_create_folder;
154         folder->rename_folder       = mh_rename_folder;
155         folder->remove_folder       = mh_remove_folder;
156 }
157
158 void mh_get_last_num(Folder *folder, FolderItem *item)
159 {
160         gchar *path;
161         DIR *dp;
162         struct dirent *d;
163         struct stat s;
164         gint max = 0;
165         gint num;
166
167         g_return_if_fail(item != NULL);
168
169         debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
170
171         path = folder_item_get_path(item);
172         g_return_if_fail(path != NULL);
173         if (change_dir(path) < 0) {
174                 g_free(path);
175                 return;
176         }
177         g_free(path);
178
179         if ((dp = opendir(".")) == NULL) {
180                 FILE_OP_ERROR(item->path, "opendir");
181                 return;
182         }
183
184         while ((d = readdir(dp)) != NULL) {
185                 if ((num = to_number(d->d_name)) >= 0 &&
186                     stat(d->d_name, &s) == 0 &&
187                     S_ISREG(s.st_mode)) {
188                         if (max < num)
189                                 max = num;
190                 }
191         }
192         closedir(dp);
193
194         debug_print(_("Last number in dir %s = %d\n"), item->path, max);
195         item->last_num = max;
196 }
197
198 GSList *mh_get_num_list(Folder *folder, FolderItem *item)
199 {
200
201         gchar *path;
202         DIR *dp;
203         struct dirent *d;
204         struct stat s;
205         gint num;
206         GSList *list = NULL;
207
208         g_return_val_if_fail(item != NULL, NULL);
209
210         debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
211
212         path = folder_item_get_path(item);
213         g_return_val_if_fail(path != NULL, NULL);
214         if (change_dir(path) < 0) {
215                 g_free(path);
216                 return NULL;
217         }
218         g_free(path);
219
220         if ((dp = opendir(".")) == NULL) {
221                 FILE_OP_ERROR(item->path, "opendir");
222                 return NULL;
223         }
224
225         while ((d = readdir(dp)) != NULL) {
226                 if ((num = to_number(d->d_name)) >= 0 &&
227                     stat(d->d_name, &s) == 0 &&
228                     S_ISREG(s.st_mode)) {
229                         list = g_slist_prepend(list, GINT_TO_POINTER(num));
230                 }
231         }
232         closedir(dp);
233
234         return list;
235 }
236
237 GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
238 {
239         GSList *mlist;
240         GHashTable *msg_table;
241         gchar *path;
242         struct stat s;
243         gboolean scan_new = TRUE;
244 #ifdef MEASURE_TIME
245         struct timeval tv_before, tv_after, tv_result;
246
247         gettimeofday(&tv_before, NULL);
248 #endif
249
250         g_return_val_if_fail(item != NULL, NULL);
251
252         path = folder_item_get_path(item);
253         if (stat(path, &s) < 0) {
254                 FILE_OP_ERROR(path, "stat");
255         } else {
256                 time_t mtime;
257
258                 mtime = MAX(s.st_mtime, s.st_ctime);
259                 if (item->mtime == mtime) {
260                         debug_print("Folder is not modified.\n");
261                         scan_new = FALSE;
262                 } else
263                         item->mtime = mtime;
264         }
265         g_free(path);
266
267         if (use_cache && !scan_new) {
268                 mlist = procmsg_read_cache(item, FALSE);
269                 if (!mlist)
270                         mlist = mh_get_uncached_msgs(NULL, item);
271         } else if (use_cache) {
272                 GSList *newlist;
273
274                 mlist = procmsg_read_cache(item, TRUE);
275                 msg_table = procmsg_msg_hash_table_create(mlist);
276
277                 newlist = mh_get_uncached_msgs(msg_table, item);
278                 if (msg_table)
279                         g_hash_table_destroy(msg_table);
280
281                 mlist = g_slist_concat(mlist, newlist);
282         } else
283                 mlist = mh_get_uncached_msgs(NULL, item);
284
285         procmsg_set_flags(mlist, item);
286
287 #ifdef MEASURE_TIME
288         gettimeofday(&tv_after, NULL);
289
290         timersub(&tv_after, &tv_before, &tv_result);
291         g_print("mh_get_msg_list: %s: elapsed time: %ld.%06ld sec\n",
292                 item->path, tv_result.tv_sec, tv_result.tv_usec);
293 #endif
294
295         return mlist;
296 }
297
298 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
299 {
300         gchar *path;
301         gchar *file;
302
303         g_return_val_if_fail(item != NULL, NULL);
304         g_return_val_if_fail(num > 0, NULL);
305
306         path = folder_item_get_path(item);
307         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
308         g_free(path);
309         if (!is_file_exist(file)) {
310                 g_free(file);
311                 return NULL;
312         }
313
314         return file;
315 }
316
317 MsgInfo *mh_fetch_msginfo(Folder *folder, FolderItem *item, gint num)
318 {
319         gchar *path;
320         gchar *file;
321         MsgFlags flags;
322         MsgInfo *msginfo;
323         struct stat s;
324
325         g_return_val_if_fail(item != NULL, NULL);
326         g_return_val_if_fail(num > 0, NULL);
327
328         path = folder_item_get_path(item);
329         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
330         g_free(path);
331         if (!is_file_exist(file)) {
332                 g_free(file);
333                 return NULL;
334         }
335
336         folder_item_set_default_flags(item, &flags);
337         msginfo = procheader_parse_file(file, flags, TRUE, FALSE);
338         msginfo->msgnum = num;
339         msginfo->folder = item;
340
341         if (stat(file, &s) < 0) {
342                 FILE_OP_ERROR(file, "stat");
343                 msginfo->size = 0;
344                 msginfo->mtime = 0;
345         } else {
346                 msginfo->size = s.st_size;
347                 msginfo->mtime = s.st_mtime;
348         }
349
350         return msginfo;
351 }
352
353 gchar *mh_get_new_msg_filename(FolderItem *dest)
354 {
355         gchar *destfile;
356         gchar *destpath;
357
358         destpath = folder_item_get_path(dest);
359         g_return_val_if_fail(destpath != NULL, NULL);
360
361         if (!is_dir_exist(destpath))
362                 make_dir_hier(destpath);
363
364         for (;;) {
365                 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
366                                            dest->last_num + 1);
367                 if (is_file_entry_exist(destfile)) {
368                         dest->last_num++;
369                         g_free(destfile);
370                 } else
371                         break;
372         }
373
374         g_free(destpath);
375
376         return destfile;
377 }
378
379 #define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
380 { \
381         MsgInfo newmsginfo; \
382  \
383         newmsginfo.msgnum = dest->last_num; \
384         newmsginfo.flags = msginfo->flags; \
385         if (dest->stype == F_OUTBOX || \
386             dest->stype == F_QUEUE  || \
387             dest->stype == F_DRAFT  || \
388             dest->stype == F_TRASH) \
389                 MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
390                                      MSG_NEW|MSG_UNREAD|MSG_DELETED); \
391  \
392         procmsg_write_flags(&newmsginfo, fp); \
393 }
394
395 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
396                 gboolean remove_source)
397 {
398         gchar *destfile;
399
400         g_return_val_if_fail(dest != NULL, -1);
401         g_return_val_if_fail(file != NULL, -1);
402
403         if (dest->last_num < 0) {
404                 mh_get_last_num(folder, dest);
405                 if (dest->last_num < 0) return -1;
406         }
407
408         destfile = mh_get_new_msg_filename(dest);
409         g_return_val_if_fail(destfile != NULL, -1);
410
411         if (link(file, destfile) < 0) {
412                 if (copy_file(file, destfile) < 0) {
413                         g_warning(_("can't copy message %s to %s\n"),
414                                   file, destfile);
415                         g_free(destfile);
416                         return -1;
417                 }
418         }
419
420         if (remove_source) {
421                 if (unlink(file) < 0)
422                         FILE_OP_ERROR(file, "unlink");
423         }
424
425         g_free(destfile);
426         dest->last_num++;
427         return dest->last_num;
428 }
429
430 static gint mh_do_move(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
431 {
432         gchar *destdir;
433         gchar *srcfile;
434         gchar *destfile;
435         FILE *fp;
436         gint filemode = 0;
437         PrefsFolderItem *prefs;
438
439         g_return_val_if_fail(dest != NULL, -1);
440         g_return_val_if_fail(msginfo != NULL, -1);
441
442         if (msginfo->folder == dest) {
443                 g_warning(_("the src folder is identical to the dest.\n"));
444                 return -1;
445         }
446
447         if (dest->last_num < 0) {
448                 mh_get_last_num(folder, dest);
449                 if (dest->last_num < 0) return -1;
450         }
451
452         prefs = dest->prefs;
453
454         destfile = mh_get_new_msg_filename(dest);
455         g_return_val_if_fail(destfile != NULL, -1);
456
457         debug_print(_("Moving message %s%c%d to %s ...\n"),
458                     msginfo->folder->path, G_DIR_SEPARATOR,
459                     msginfo->msgnum, dest->path);
460         srcfile = procmsg_get_message_file(msginfo);
461
462         destfile = mh_get_new_msg_filename(dest);
463         if(!destfile) return -1;
464
465         srcfile = procmsg_get_message_file(msginfo);
466
467         if (move_file(srcfile, destfile) < 0) {
468                 g_free(srcfile);
469                 g_free(destfile);
470                 return -1;
471         }
472
473         if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
474                 if (chmod(destfile, prefs->folder_chmod) < 0)
475                         FILE_OP_ERROR(destfile, "chmod");
476
477                 /* for mark file */
478                 filemode = prefs->folder_chmod;
479                 if (filemode & S_IRGRP) filemode |= S_IWGRP;
480                 if (filemode & S_IROTH) filemode |= S_IWOTH;
481         }
482
483         g_free(srcfile);
484         g_free(destfile);
485         dest->last_num++;
486
487         g_free(destdir);
488
489         return dest->last_num;
490 }
491
492 gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
493 {
494         gchar *srcfile;
495         gint ret = 0;
496  
497         g_return_val_if_fail(folder != NULL, -1);
498         g_return_val_if_fail(dest != NULL, -1);
499         g_return_val_if_fail(msginfo != NULL, -1);
500         g_return_val_if_fail(msginfo->folder != NULL, -1);
501  
502         if (folder == msginfo->folder->folder)
503                 return mh_do_move(folder, dest, msginfo);
504  
505         srcfile = procmsg_get_message_file(msginfo);
506         if (!srcfile) return -1;
507  
508         ret = mh_add_msg(folder, dest, srcfile, FALSE);
509         g_free(srcfile);
510  
511         if (ret != -1) {
512                 gchar *destdir;
513                 FILE *fp;
514  
515                 destdir = folder_item_get_path(dest);
516                 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
517                         g_warning(_("Can't open mark file.\n"));
518                 else {
519                         SET_DEST_MSG_FLAGS(fp, dest, msginfo);
520                         fclose(fp);
521                 }
522                 g_free(destdir);
523  
524                 ret = folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
525         }
526  
527         return ret;
528 }
529
530 static gint mh_do_move_msgs_with_dest(Folder *folder, FolderItem *dest,
531                                       GSList *msglist)
532 {
533         gchar *srcfile;
534         gchar *destfile;
535         FILE *fp;
536         GSList *cur;
537         MsgInfo *msginfo;
538         PrefsFolderItem *prefs;
539
540         g_return_val_if_fail(dest != NULL, -1);
541         g_return_val_if_fail(msglist != NULL, -1);
542
543         if (dest->last_num < 0) {
544                 mh_get_last_num(folder, dest);
545                 if (dest->last_num < 0) return -1;
546         }
547
548         prefs = dest->prefs;
549
550         for (cur = msglist; cur != NULL; cur = cur->next) {
551                 msginfo = (MsgInfo *)cur->data;
552
553                 if (msginfo->folder == dest) {
554                         g_warning(_("the src folder is identical to the dest.\n"));
555                         continue;
556                 }
557                 debug_print(_("Moving message %s%c%d to %s ...\n"),
558                             msginfo->folder->path, G_DIR_SEPARATOR,
559                             msginfo->msgnum, dest->path);
560
561                 destfile = mh_get_new_msg_filename(dest);
562                 if (!destfile) break;
563                 srcfile = procmsg_get_message_file(msginfo);
564                 destfile = mh_get_new_msg_filename(dest);
565                 if(!destfile) return -1;
566
567                 if (move_file(srcfile, destfile) < 0) {
568                         g_free(srcfile);
569                         g_free(destfile);
570                         break;
571                 }
572
573                 g_free(srcfile);
574                 g_free(destfile);
575                 dest->last_num++;
576         }
577
578         return dest->last_num;
579 }
580
581 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
582 {
583         MsgInfo *msginfo;
584         GSList *cur;
585         gint ret = 0;
586
587         msginfo = (MsgInfo *)msglist->data;
588         if (folder == msginfo->folder->folder)
589                 return mh_do_move_msgs_with_dest(folder, dest, msglist);
590
591         for (cur = msglist; cur != NULL; cur = cur->next) {
592                 msginfo = (MsgInfo *)cur->data;
593                 ret = mh_move_msg(folder, dest, msginfo);
594                 if (ret == -1) break;
595         }
596
597         return ret;
598 }
599
600 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
601 {
602         gchar *srcfile;
603         gchar *destfile;
604         gchar *destdir;
605         gint filemode = 0;
606         PrefsFolderItem *prefs;
607
608         g_return_val_if_fail(dest != NULL, -1);
609         g_return_val_if_fail(msginfo != NULL, -1);
610
611         if (msginfo->folder == dest) {
612                 g_warning(_("the src folder is identical to the dest.\n"));
613                 return -1;
614         }
615
616         if (dest->last_num < 0) {
617                 mh_get_last_num(folder, dest);
618                 if (dest->last_num < 0) return -1;
619         }
620
621         prefs = dest->prefs;
622
623         destfile = mh_get_new_msg_filename(dest);
624         g_return_val_if_fail(destfile != NULL, -1);
625
626         debug_print(_("Copying message %s%c%d to %s ...\n"),
627                     msginfo->folder->path, G_DIR_SEPARATOR,
628                     msginfo->msgnum, dest->path);
629
630         srcfile = procmsg_get_message_file(msginfo);
631         destfile = mh_get_new_msg_filename(dest);
632         if(!destfile) {
633                 g_free(srcfile);
634                 return -1;
635         }
636         
637         dest->op_count--;
638
639         if (copy_file(srcfile, destfile) < 0) {
640                 FILE_OP_ERROR(srcfile, "copy");
641                 g_free(srcfile);
642                 g_free(destfile);
643                 return -1;
644         }
645
646         if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
647                 if (chmod(destfile, prefs->folder_chmod) < 0)
648                         FILE_OP_ERROR(destfile, "chmod");
649
650                 /* for mark file */
651                 filemode = prefs->folder_chmod;
652                 if (filemode & S_IRGRP) filemode |= S_IWGRP;
653                 if (filemode & S_IROTH) filemode |= S_IWOTH;
654         }
655
656         g_free(srcfile);
657         g_free(destfile);
658         dest->last_num++;
659
660         return dest->last_num;
661 }
662
663 /*
664 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
665 {
666         Folder * src_folder;
667         gchar * filename;
668         gint num;
669         gchar * destdir;
670         FILE * fp;
671
672         src_folder = msginfo->folder->folder;
673         
674         g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
675         
676         filename = src_folder->fetch_msg(src_folder,
677                                          msginfo->folder,
678                                          msginfo->msgnum);
679         if (filename == NULL)
680                 return -1;
681
682         num = folder->add_msg(folder, dest, filename, FALSE);
683
684         destdir = folder_item_get_path(dest);
685
686         if (fp) {
687                 MsgInfo newmsginfo;
688
689                 newmsginfo.msgnum = dest->last_num;
690                 newmsginfo.flags = msginfo->flags;
691                 if (dest->stype == F_OUTBOX ||
692                     dest->stype == F_QUEUE  ||
693                     dest->stype == F_DRAFT  ||
694                     dest->stype == F_TRASH)
695                         MSG_UNSET_FLAGS(newmsginfo.flags,
696                                         MSG_NEW|MSG_UNREAD|MSG_DELETED);
697
698                 procmsg_write_flags(&newmsginfo, fp);
699                 fclose(fp);
700         }
701         
702         return num;
703 }
704 */
705
706 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
707 {
708         gchar *srcfile;
709         gchar *destfile;
710         GSList *cur;
711         MsgInfo *msginfo;
712
713         g_return_val_if_fail(dest != NULL, -1);
714         g_return_val_if_fail(msglist != NULL, -1);
715
716         if (dest->last_num < 0) {
717                 mh_get_last_num(folder, dest);
718                 if (dest->last_num < 0) return -1;
719         }
720
721         for (cur = msglist; cur != NULL; cur = cur->next) {
722                 msginfo = (MsgInfo *)cur->data;
723
724                 if (msginfo->folder == dest) {
725                         g_warning(_("the src folder is identical to the dest.\n"));
726                         continue;
727                 }
728                 debug_print(_("Copying message %s%c%d to %s ...\n"),
729                             msginfo->folder->path, G_DIR_SEPARATOR,
730                             msginfo->msgnum, dest->path);
731
732                 destfile = mh_get_new_msg_filename(dest);
733                 if (!destfile) break;
734                 srcfile = procmsg_get_message_file(msginfo);
735
736                 if (copy_file(srcfile, destfile) < 0) {
737                         FILE_OP_ERROR(srcfile, "copy");
738                         g_free(srcfile);
739                         g_free(destfile);
740                         break;
741                 }
742
743                 g_free(srcfile);
744                 g_free(destfile);
745                 dest->last_num++;
746         }
747
748         return dest->last_num;
749 }
750
751 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
752 {
753         gchar *file;
754         MsgInfo *msginfo;
755
756         g_return_val_if_fail(item != NULL, -1);
757
758         file = mh_fetch_msg(folder, item, num);
759         g_return_val_if_fail(file != NULL, -1);
760
761         if (unlink(file) < 0) {
762                 FILE_OP_ERROR(file, "unlink");
763                 g_free(file);
764                 return -1;
765         }
766
767         g_free(file);
768         return 0;
769 }
770
771 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
772 {
773         gchar *path;
774         gint val;
775
776         g_return_val_if_fail(item != NULL, -1);
777
778         path = folder_item_get_path(item);
779         g_return_val_if_fail(path != NULL, -1);
780         val = remove_all_numbered_files(path);
781         g_free(path);
782
783         return val;
784 }
785
786 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
787 {
788         struct stat s;
789
790         if (stat(itos(msginfo->msgnum), &s) < 0 ||
791             msginfo->size  != s.st_size ||
792             msginfo->mtime != s.st_mtime)
793                 return TRUE;
794
795         return FALSE;
796 }
797
798 gint mh_scan_folder(Folder *folder, FolderItem *item)
799 {
800         gchar *path;
801         DIR *dp;
802         struct dirent *d;
803         struct stat s;
804         gint max = 0;
805         gint num;
806         gint n_msg = 0;
807
808         g_return_val_if_fail(item != NULL, -1);
809
810         debug_print("mh_scan_folder(): Scanning %s ...\n", item->path);
811
812         path = folder_item_get_path(item);
813         g_return_val_if_fail(path != NULL, -1);
814         if (change_dir(path) < 0) {
815                 g_free(path);
816                 return -1;
817         }
818         g_free(path);
819
820         if ((dp = opendir(".")) == NULL) {
821                 FILE_OP_ERROR(item->path, "opendir");
822                 return -1;
823         }
824
825         if (folder->ui_func)
826                 folder->ui_func(folder, item, folder->ui_func_data);
827
828         while ((d = readdir(dp)) != NULL) {
829                 if ((num = to_number(d->d_name)) >= 0 &&
830                     stat(d->d_name, &s) == 0 &&
831                     S_ISREG(s.st_mode)) {
832 /*
833                         n_msg++;
834 */
835                         if (max < num)
836                                 max = num;
837                 }
838         }
839         closedir(dp);
840
841 /*
842         if (n_msg == 0)
843                 item->new = item->unread = item->total = 0;
844         else {
845                 gint new, unread, total, min, max;
846
847                 procmsg_get_mark_sum(".", &new, &unread, &total, &min, &max, 0);
848                 if (n_msg > total) {
849                         new += n_msg - total;
850                         unread += n_msg - total;
851                 }
852                 item->new = new;
853                 item->unread = unread;
854                 item->total = n_msg;
855         }
856 */
857         debug_print(_("Last number in dir %s = %d\n"), item->path, max);
858         item->last_num = max;
859
860         return 0;
861 }
862
863 void mh_scan_tree(Folder *folder)
864 {
865         FolderItem *item;
866         gchar *rootpath;
867         GHashTable *pptable;
868
869         g_return_if_fail(folder != NULL);
870
871         pptable = folder_persist_prefs_new(folder);
872
873         prefs_scoring_clear();
874         prefs_filtering_clear();
875         folder_tree_destroy(folder);
876         item = folder_item_new(folder->name, NULL);
877         item->folder = folder;
878         folder->node = g_node_new(item);
879
880         rootpath = folder_item_get_path(item);
881         if (change_dir(rootpath) < 0) {
882                 g_free(rootpath);
883                 return;
884         }
885         g_free(rootpath);
886
887         mh_create_tree(folder);
888         mh_scan_tree_recursive(item, pptable);
889         
890         folder_persist_prefs_free(pptable);
891
892         prefs_matcher_read_config();
893 }
894
895 #define MAKE_DIR_IF_NOT_EXIST(dir) \
896 { \
897         if (!is_dir_exist(dir)) { \
898                 if (is_file_exist(dir)) { \
899                         g_warning(_("File `%s' already exists.\n" \
900                                     "Can't create folder."), dir); \
901                         return -1; \
902                 } \
903                 if (make_dir(dir) < 0) \
904                         return -1; \
905         } \
906 }
907
908 gint mh_create_tree(Folder *folder)
909 {
910         gchar *rootpath;
911
912         g_return_val_if_fail(folder != NULL, -1);
913
914         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
915         rootpath = LOCAL_FOLDER(folder)->rootpath;
916         MAKE_DIR_IF_NOT_EXIST(rootpath);
917         CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
918         MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
919         MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
920         MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
921         MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
922         MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
923
924         return 0;
925 }
926
927 #undef MAKE_DIR_IF_NOT_EXIST
928
929 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
930                              const gchar *name)
931 {
932         gchar *path;
933         gchar *fullpath;
934         FolderItem *new_item;
935
936         g_return_val_if_fail(folder != NULL, NULL);
937         g_return_val_if_fail(parent != NULL, NULL);
938         g_return_val_if_fail(name != NULL, NULL);
939
940         path = folder_item_get_path(parent);
941         if (!is_dir_exist(path))
942                 make_dir_hier(path);
943
944         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
945         g_free(path);
946
947         if (make_dir(fullpath) < 0) {
948                 g_free(fullpath);
949                 return NULL;
950         }
951
952         g_free(fullpath);
953
954         if (parent->path)
955                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
956                                    NULL);
957         else
958                 path = g_strdup(name);
959         new_item = folder_item_new(name, path);
960         folder_item_append(parent, new_item);
961         g_free(path);
962
963         return new_item;
964 }
965
966 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
967 {
968         gchar *oldpath;
969         gchar *dirname;
970         gchar *newpath;
971         GNode *node;
972         gchar *paths[2];
973
974         g_return_val_if_fail(folder != NULL, -1);
975         g_return_val_if_fail(item != NULL, -1);
976         g_return_val_if_fail(item->path != NULL, -1);
977         g_return_val_if_fail(name != NULL, -1);
978
979         oldpath = folder_item_get_path(item);
980         if (!is_dir_exist(oldpath))
981                 make_dir_hier(oldpath);
982
983         dirname = g_dirname(oldpath);
984         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
985         g_free(dirname);
986
987         if (rename(oldpath, newpath) < 0) {
988                 FILE_OP_ERROR(oldpath, "rename");
989                 g_free(oldpath);
990                 g_free(newpath);
991                 return -1;
992         }
993
994         g_free(oldpath);
995         g_free(newpath);
996
997         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
998                 dirname = g_dirname(item->path);
999                 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
1000                 g_free(dirname);
1001         } else
1002                 newpath = g_strdup(name);
1003
1004         g_free(item->name);
1005         item->name = g_strdup(name);
1006
1007         node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
1008                            item);
1009         paths[0] = g_strdup(item->path);
1010         paths[1] = newpath;
1011         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1012                         mh_rename_folder_func, paths);
1013
1014         g_free(paths[0]);
1015         g_free(paths[1]);
1016         return 0;
1017 }
1018
1019 gint mh_remove_folder(Folder *folder, FolderItem *item)
1020 {
1021         gchar *path;
1022
1023         g_return_val_if_fail(folder != NULL, -1);
1024         g_return_val_if_fail(item != NULL, -1);
1025         g_return_val_if_fail(item->path != NULL, -1);
1026
1027         path = folder_item_get_path(item);
1028         if (remove_dir_recursive(path) < 0) {
1029                 g_warning("can't remove directory `%s'\n", path);
1030                 g_free(path);
1031                 return -1;
1032         }
1033
1034         g_free(path);
1035         folder_item_remove(item);
1036         return 0;
1037 }
1038
1039
1040 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
1041 {
1042         gchar *path;
1043         DIR *dp;
1044         struct dirent *d;
1045         struct stat s;
1046         GSList *newlist = NULL;
1047         GSList *last = NULL;
1048         MsgInfo *msginfo;
1049         gint n_newmsg = 0;
1050         gint num;
1051
1052         g_return_val_if_fail(item != NULL, NULL);
1053
1054         path = folder_item_get_path(item);
1055         g_return_val_if_fail(path != NULL, NULL);
1056         if (change_dir(path) < 0) {
1057                 g_free(path);
1058                 return NULL;
1059         }
1060         g_free(path);
1061
1062         if ((dp = opendir(".")) == NULL) {
1063                 FILE_OP_ERROR(item->path, "opendir");
1064                 return NULL;
1065         }
1066
1067         debug_print(_("\tSearching uncached messages... "));
1068
1069         if (msg_table) {
1070                 while ((d = readdir(dp)) != NULL) {
1071                         if ((num = to_number(d->d_name)) < 0) continue;
1072                         if (stat(d->d_name, &s) < 0) {
1073                                 FILE_OP_ERROR(d->d_name, "stat");
1074                                 continue;
1075                         }
1076                         if (!S_ISREG(s.st_mode)) continue;
1077
1078                         msginfo = g_hash_table_lookup
1079                                 (msg_table, GUINT_TO_POINTER(num));
1080
1081                         if (!msginfo) {
1082                                 /* not found in the cache (uncached message) */
1083                                 msginfo = mh_parse_msg(d->d_name, item);
1084                                 if (!msginfo) continue;
1085
1086                                 if (!newlist)
1087                                         last = newlist =
1088                                                 g_slist_append(NULL, msginfo);
1089                                 else {
1090                                         last = g_slist_append(last, msginfo);
1091                                         last = last->next;
1092                                 }
1093                                 n_newmsg++;
1094                         }
1095                 }
1096         } else {
1097                 /* discard all previous cache */
1098                 while ((d = readdir(dp)) != NULL) {
1099                         if (to_number(d->d_name) < 0) continue;
1100                         if (stat(d->d_name, &s) < 0) {
1101                                 FILE_OP_ERROR(d->d_name, "stat");
1102                                 continue;
1103                         }
1104                         if (!S_ISREG(s.st_mode)) continue;
1105
1106                         msginfo = mh_parse_msg(d->d_name, item);
1107                         if (!msginfo) continue;
1108
1109                         if (!newlist)
1110                                 last = newlist = g_slist_append(NULL, msginfo);
1111                         else {
1112                                 last = g_slist_append(last, msginfo);
1113                                 last = last->next;
1114                         }
1115                         n_newmsg++;
1116                 }
1117         }
1118
1119         closedir(dp);
1120
1121         if (n_newmsg)
1122                 debug_print(_("%d uncached message(s) found.\n"), n_newmsg);
1123         else
1124                 debug_print(_("done.\n"));
1125
1126         /* sort new messages in numerical order */
1127         if (newlist) {
1128                 debug_print(_("\tSorting uncached messages in numerical order... "));
1129                 newlist = g_slist_sort
1130                         (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
1131                 debug_print(_("done.\n"));
1132         }
1133
1134         return newlist;
1135 }
1136
1137 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1138 {
1139         struct stat s;
1140         MsgInfo *msginfo;
1141         MsgFlags flags;
1142
1143         flags.perm_flags = MSG_NEW|MSG_UNREAD;
1144         flags.tmp_flags = 0;
1145
1146         g_return_val_if_fail(item != NULL, NULL);
1147         g_return_val_if_fail(file != NULL, NULL);
1148
1149         if (item->stype == F_QUEUE) {
1150                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1151         } else if (item->stype == F_DRAFT) {
1152                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1153         }
1154
1155         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1156         if (!msginfo) return NULL;
1157
1158         msginfo->msgnum = atoi(file);
1159         msginfo->folder = item;
1160
1161         if (stat(file, &s) < 0) {
1162                 FILE_OP_ERROR(file, "stat");
1163                 msginfo->size = 0;
1164                 msginfo->mtime = 0;
1165         } else {
1166                 msginfo->size = s.st_size;
1167                 msginfo->mtime = s.st_mtime;
1168         }
1169
1170         return msginfo;
1171 }
1172
1173 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
1174 {
1175         gchar *entry;
1176         gboolean result;
1177
1178         entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
1179         result = is_dir_exist(entry);
1180         g_free(entry);
1181
1182         return result;
1183 }
1184
1185 /*
1186  * check whether PATH is a Maildir style mailbox.
1187  * This is the case if the 3 subdir: new, cur, tmp are existing.
1188  * This functon assumes that entry is an directory
1189  */
1190 static gboolean mh_is_maildir(const gchar *path)
1191 {
1192         return mh_is_maildir_one(path, "new") &&
1193                mh_is_maildir_one(path, "cur") &&
1194                mh_is_maildir_one(path, "tmp");
1195 }
1196
1197 static void mh_scan_tree_recursive(FolderItem *item, GHashTable *pptable)
1198 {
1199         DIR *dp;
1200         struct dirent *d;
1201         struct stat s;
1202         gchar *entry;
1203         gint n_msg = 0;
1204
1205         g_return_if_fail(item != NULL);
1206         g_return_if_fail(item->folder != NULL);
1207
1208         dp = opendir(item->path ? item->path : ".");
1209         if (!dp) {
1210                 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
1211                 return;
1212         }
1213
1214         debug_print("scanning %s ...\n",
1215                     item->path ? item->path
1216                     : LOCAL_FOLDER(item->folder)->rootpath);
1217         if (item->folder->ui_func)
1218                 item->folder->ui_func(item->folder, item,
1219                                       item->folder->ui_func_data);
1220
1221         while ((d = readdir(dp)) != NULL) {
1222                 if (d->d_name[0] == '.') continue;
1223
1224                 if (item->path)
1225                         entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1226                                             d->d_name, NULL);
1227                 else
1228                         entry = g_strdup(d->d_name);
1229
1230                 if (stat(entry, &s) < 0) {
1231                         FILE_OP_ERROR(entry, "stat");
1232                         g_free(entry);
1233                         continue;
1234                 }
1235
1236                 if (S_ISDIR(s.st_mode)) {
1237                         FolderItem *new_item;
1238
1239                         if (mh_is_maildir(entry)) {
1240                                 g_free(entry);
1241                                 continue;
1242                         }
1243
1244                         new_item = folder_item_new(d->d_name, entry);
1245                         folder_item_append(item, new_item);
1246                         if (!item->path) {
1247                                 if (!strcmp(d->d_name, INBOX_DIR)) {
1248                                         new_item->stype = F_INBOX;
1249                                         item->folder->inbox = new_item;
1250                                 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
1251                                         new_item->stype = F_OUTBOX;
1252                                         item->folder->outbox = new_item;
1253                                 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
1254                                         new_item->stype = F_DRAFT;
1255                                         item->folder->draft = new_item;
1256                                 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
1257                                         new_item->stype = F_QUEUE;
1258                                         item->folder->queue = new_item;
1259                                 } else if (!strcmp(d->d_name, TRASH_DIR)) {
1260                                         new_item->stype = F_TRASH;
1261                                         item->folder->trash = new_item;
1262                                 }
1263                         }
1264                         folder_item_restore_persist_prefs(new_item, pptable);
1265                         mh_scan_tree_recursive(new_item, pptable);
1266                 } else if (to_number(d->d_name) != -1) n_msg++;
1267
1268                 g_free(entry);
1269         }
1270
1271         closedir(dp);
1272
1273         if (item->path) {
1274                 gint new, unread, total, min, max;
1275
1276                 procmsg_get_mark_sum(item->path, &new, &unread, &total,
1277                                      &min, &max, 0);
1278                 if (n_msg > total) {
1279                         new += n_msg - total;
1280                         unread += n_msg - total;
1281                 }
1282                 item->new = new;
1283                 item->unread = unread;
1284                 item->total = n_msg;
1285         }
1286 }
1287
1288 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1289 {
1290         FolderItem *item = node->data;
1291         gchar **paths = data;
1292         const gchar *oldpath = paths[0];
1293         const gchar *newpath = paths[1];
1294         gchar *base;
1295         gchar *new_itempath;
1296         gint oldpathlen;
1297
1298         oldpathlen = strlen(oldpath);
1299         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1300                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1301                 return TRUE;
1302         }
1303
1304         base = item->path + oldpathlen;
1305         while (*base == G_DIR_SEPARATOR) base++;
1306         if (*base == '\0')
1307                 new_itempath = g_strdup(newpath);
1308         else
1309                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1310                                            NULL);
1311         g_free(item->path);
1312         item->path = new_itempath;
1313
1314         return FALSE;
1315 }