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