* src/procmsg.[ch]
[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
896         g_return_val_if_fail(folder != NULL, NULL);
897         g_return_val_if_fail(parent != NULL, NULL);
898         g_return_val_if_fail(name != NULL, NULL);
899
900         path = folder_item_get_path(parent);
901         if (!is_dir_exist(path)) 
902                 if (make_dir_hier(path) != 0)
903                         return NULL;
904                 
905         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
906         g_free(path);
907
908         if (make_dir(fullpath) < 0) {
909                 g_free(fullpath);
910                 return NULL;
911         }
912
913         g_free(fullpath);
914
915         if (parent->path)
916                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
917                                    NULL);
918         else
919                 path = g_strdup(name);
920         new_item = folder_item_new(folder, name, path);
921         folder_item_append(parent, new_item);
922         g_free(path);
923
924         return new_item;
925 }
926
927 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
928 {
929         gchar *oldpath;
930         gchar *dirname;
931         gchar *newpath;
932         GNode *node;
933         gchar *paths[2];
934
935         g_return_val_if_fail(folder != NULL, -1);
936         g_return_val_if_fail(item != NULL, -1);
937         g_return_val_if_fail(item->path != NULL, -1);
938         g_return_val_if_fail(name != NULL, -1);
939
940         oldpath = folder_item_get_path(item);
941         if (!is_dir_exist(oldpath))
942                 make_dir_hier(oldpath);
943
944         dirname = g_dirname(oldpath);
945         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
946         g_free(dirname);
947
948         if (rename(oldpath, newpath) < 0) {
949                 FILE_OP_ERROR(oldpath, "rename");
950                 g_free(oldpath);
951                 g_free(newpath);
952                 return -1;
953         }
954
955         g_free(oldpath);
956         g_free(newpath);
957
958         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
959                 dirname = g_dirname(item->path);
960                 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
961                 g_free(dirname);
962         } else
963                 newpath = g_strdup(name);
964
965         g_free(item->name);
966         item->name = g_strdup(name);
967
968         node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
969                            item);
970         paths[0] = g_strdup(item->path);
971         paths[1] = newpath;
972         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
973                         mh_rename_folder_func, paths);
974
975         g_free(paths[0]);
976         g_free(paths[1]);
977         return 0;
978 }
979
980 gint mh_remove_folder(Folder *folder, FolderItem *item)
981 {
982         gchar *path;
983
984         g_return_val_if_fail(folder != NULL, -1);
985         g_return_val_if_fail(item != NULL, -1);
986         g_return_val_if_fail(item->path != NULL, -1);
987
988         path = folder_item_get_path(item);
989         if (remove_dir_recursive(path) < 0) {
990                 g_warning("can't remove directory `%s'\n", path);
991                 g_free(path);
992                 return -1;
993         }
994
995         g_free(path);
996         folder_item_remove(item);
997         return 0;
998 }
999
1000
1001 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
1002 {
1003         gchar *path;
1004         DIR *dp;
1005         struct dirent *d;
1006         struct stat s;
1007         GSList *newlist = NULL;
1008         GSList *last = NULL;
1009         MsgInfo *msginfo;
1010         gint n_newmsg = 0;
1011         gint num;
1012
1013         g_return_val_if_fail(item != NULL, NULL);
1014
1015         path = folder_item_get_path(item);
1016         g_return_val_if_fail(path != NULL, NULL);
1017         if (change_dir(path) < 0) {
1018                 g_free(path);
1019                 return NULL;
1020         }
1021         g_free(path);
1022
1023         if ((dp = opendir(".")) == NULL) {
1024                 FILE_OP_ERROR(item->path, "opendir");
1025                 return NULL;
1026         }
1027
1028         debug_print("\tSearching uncached messages... ");
1029
1030         if (msg_table) {
1031                 while ((d = readdir(dp)) != NULL) {
1032                         if ((num = to_number(d->d_name)) < 0) continue;
1033                         if (stat(d->d_name, &s) < 0) {
1034                                 FILE_OP_ERROR(d->d_name, "stat");
1035                                 continue;
1036                         }
1037                         if (!S_ISREG(s.st_mode)) continue;
1038
1039                         msginfo = g_hash_table_lookup
1040                                 (msg_table, GUINT_TO_POINTER(num));
1041
1042                         if (!msginfo) {
1043                                 /* not found in the cache (uncached message) */
1044                                 msginfo = mh_parse_msg(d->d_name, item);
1045                                 if (!msginfo) continue;
1046
1047                                 if (!newlist)
1048                                         last = newlist =
1049                                                 g_slist_append(NULL, msginfo);
1050                                 else {
1051                                         last = g_slist_append(last, msginfo);
1052                                         last = last->next;
1053                                 }
1054                                 n_newmsg++;
1055                         }
1056                 }
1057         } else {
1058                 /* discard all previous cache */
1059                 while ((d = readdir(dp)) != NULL) {
1060                         if (to_number(d->d_name) < 0) continue;
1061                         if (stat(d->d_name, &s) < 0) {
1062                                 FILE_OP_ERROR(d->d_name, "stat");
1063                                 continue;
1064                         }
1065                         if (!S_ISREG(s.st_mode)) continue;
1066
1067                         msginfo = mh_parse_msg(d->d_name, item);
1068                         if (!msginfo) continue;
1069
1070                         if (!newlist)
1071                                 last = newlist = g_slist_append(NULL, msginfo);
1072                         else {
1073                                 last = g_slist_append(last, msginfo);
1074                                 last = last->next;
1075                         }
1076                         n_newmsg++;
1077                 }
1078         }
1079
1080         closedir(dp);
1081
1082         if (n_newmsg)
1083                 debug_print("%d uncached message(s) found.\n", n_newmsg);
1084         else
1085                 debug_print("done.\n");
1086
1087         /* sort new messages in numerical order */
1088         if (newlist) {
1089                 debug_print("\tSorting uncached messages in numerical order... ");
1090                 newlist = g_slist_sort
1091                         (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
1092                 debug_print("done.\n");
1093         }
1094
1095         return newlist;
1096 }
1097
1098 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1099 {
1100         struct stat s;
1101         MsgInfo *msginfo;
1102         MsgFlags flags;
1103
1104         flags.perm_flags = MSG_NEW|MSG_UNREAD;
1105         flags.tmp_flags = 0;
1106
1107         g_return_val_if_fail(item != NULL, NULL);
1108         g_return_val_if_fail(file != NULL, NULL);
1109
1110         if (item->stype == F_QUEUE) {
1111                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1112         } else if (item->stype == F_DRAFT) {
1113                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1114         }
1115
1116         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1117         if (!msginfo) return NULL;
1118
1119         msginfo->msgnum = atoi(file);
1120         msginfo->folder = item;
1121
1122         if (stat(file, &s) < 0) {
1123                 FILE_OP_ERROR(file, "stat");
1124                 msginfo->size = 0;
1125                 msginfo->mtime = 0;
1126         } else {
1127                 msginfo->size = s.st_size;
1128                 msginfo->mtime = s.st_mtime;
1129         }
1130
1131         return msginfo;
1132 }
1133
1134 #if 0
1135 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
1136 {
1137         gchar *entry;
1138         gboolean result;
1139
1140         entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
1141         result = is_dir_exist(entry);
1142         g_free(entry);
1143
1144         return result;
1145 }
1146
1147 /*
1148  * check whether PATH is a Maildir style mailbox.
1149  * This is the case if the 3 subdir: new, cur, tmp are existing.
1150  * This functon assumes that entry is an directory
1151  */
1152 static gboolean mh_is_maildir(const gchar *path)
1153 {
1154         return mh_is_maildir_one(path, "new") &&
1155                mh_is_maildir_one(path, "cur") &&
1156                mh_is_maildir_one(path, "tmp");
1157 }
1158 #endif
1159
1160 static void mh_scan_tree_recursive(FolderItem *item)
1161 {
1162         DIR *dp;
1163         struct dirent *d;
1164         struct stat s;
1165         gchar *entry;
1166         gint n_msg = 0;
1167
1168         g_return_if_fail(item != NULL);
1169         g_return_if_fail(item->folder != NULL);
1170
1171         dp = opendir(item->path ? item->path : ".");
1172         if (!dp) {
1173                 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
1174                 return;
1175         }
1176
1177         debug_print("scanning %s ...\n",
1178                     item->path ? item->path
1179                     : LOCAL_FOLDER(item->folder)->rootpath);
1180         if (item->folder->ui_func)
1181                 item->folder->ui_func(item->folder, item,
1182                                       item->folder->ui_func_data);
1183
1184         while ((d = readdir(dp)) != NULL) {
1185                 if (d->d_name[0] == '.') continue;
1186
1187                 if (item->path)
1188                         entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1189                                             d->d_name, NULL);
1190                 else
1191                         entry = g_strdup(d->d_name);
1192
1193                 if (stat(entry, &s) < 0) {
1194                         FILE_OP_ERROR(entry, "stat");
1195                         g_free(entry);
1196                         continue;
1197                 }
1198
1199                 if (S_ISDIR(s.st_mode)) {
1200                         FolderItem *new_item;
1201
1202 #if 0
1203                         if (mh_is_maildir(entry)) {
1204                                 g_free(entry);
1205                                 continue;
1206                         }
1207 #endif
1208
1209                         new_item = folder_item_new(item->folder, d->d_name, entry);
1210                         folder_item_append(item, new_item);
1211                         if (!item->path) {
1212                                 if (!strcmp(d->d_name, INBOX_DIR)) {
1213                                         new_item->stype = F_INBOX;
1214                                         item->folder->inbox = new_item;
1215                                 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
1216                                         new_item->stype = F_OUTBOX;
1217                                         item->folder->outbox = new_item;
1218                                 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
1219                                         new_item->stype = F_DRAFT;
1220                                         item->folder->draft = new_item;
1221                                 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
1222                                         new_item->stype = F_QUEUE;
1223                                         item->folder->queue = new_item;
1224                                 } else if (!strcmp(d->d_name, TRASH_DIR)) {
1225                                         new_item->stype = F_TRASH;
1226                                         item->folder->trash = new_item;
1227                                 }
1228                         }
1229                         mh_scan_tree_recursive(new_item);
1230                 } else if (to_number(d->d_name) != -1) n_msg++;
1231
1232                 g_free(entry);
1233         }
1234
1235         closedir(dp);
1236
1237 /*
1238         if (item->path) {
1239                 gint new, unread, total, min, max;
1240
1241                 procmsg_get_mark_sum(item->path, &new, &unread, &total,
1242                                      &min, &max, 0);
1243                 if (n_msg > total) {
1244                         new += n_msg - total;
1245                         unread += n_msg - total;
1246                 }
1247                 item->new = new;
1248                 item->unread = unread;
1249                 item->total = n_msg;
1250         }
1251 */
1252 }
1253
1254 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1255 {
1256         FolderItem *item = node->data;
1257         gchar **paths = data;
1258         const gchar *oldpath = paths[0];
1259         const gchar *newpath = paths[1];
1260         gchar *base;
1261         gchar *new_itempath;
1262         gint oldpathlen;
1263
1264         oldpathlen = strlen(oldpath);
1265         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1266                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1267                 return TRUE;
1268         }
1269
1270         base = item->path + oldpathlen;
1271         while (*base == G_DIR_SEPARATOR) base++;
1272         if (*base == '\0')
1273                 new_itempath = g_strdup(newpath);
1274         else
1275                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1276                                            NULL);
1277         g_free(item->path);
1278         item->path = new_itempath;
1279
1280         return FALSE;
1281 }