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