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