2008-07-05 [colin] 3.5.0cvs7
[claws.git] / src / mh.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
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 <glib/gi18n.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <time.h>
34
35 #include "folder.h"
36 #include "mh.h"
37 #include "procmsg.h"
38 #include "procheader.h"
39 #include "utils.h"
40 #include "codeconv.h"
41 #include "statusbar.h"
42 #include "gtkutils.h"
43 #include "timing.h"
44
45 /* Define possible missing constants for Windows. */
46 #ifdef G_OS_WIN32
47 # ifndef S_IRGRP
48 # define S_IRGRP 0
49 # define S_IWGRP 0
50 # endif
51 # ifndef S_IROTH
52 # define S_IROTH 0
53 # define S_IWOTH 0
54 # endif
55 #endif
56
57
58 static void     mh_folder_init          (Folder         *folder,
59                                          const gchar    *name,
60                                          const gchar    *path);
61
62 static Folder   *mh_folder_new          (const gchar    *name,
63                                          const gchar    *path);
64 static void     mh_folder_destroy       (Folder         *folder);
65 static gchar   *mh_fetch_msg            (Folder         *folder,
66                                          FolderItem     *item,
67                                          gint            num);
68 static MsgInfo *mh_get_msginfo          (Folder         *folder,
69                                          FolderItem     *item,
70                                          gint            num);
71 static gint     mh_add_msg              (Folder         *folder,
72                                          FolderItem     *dest,
73                                          const gchar    *file,
74                                          MsgFlags       *flags);
75 static gint     mh_add_msgs             (Folder         *folder,
76                                          FolderItem     *dest,
77                                          GSList         *file_list,
78                                          GRelation      *relation);
79 static gint     mh_copy_msg             (Folder         *folder,
80                                          FolderItem     *dest,
81                                          MsgInfo        *msginfo);
82 static gint     mh_copy_msgs            (Folder         *folder, 
83                                          FolderItem     *dest, 
84                                          MsgInfoList    *msglist, 
85                                          GRelation      *relation);
86 static gint     mh_remove_msg           (Folder         *folder,
87                                          FolderItem     *item,
88                                          gint            num);
89 static gint     mh_remove_msgs          (Folder         *folder, 
90                                          FolderItem     *item, 
91                                          MsgInfoList    *msglist, 
92                                          GRelation      *relation);
93 static gint     mh_remove_all_msg       (Folder         *folder,
94                                          FolderItem     *item);
95 static gboolean mh_is_msg_changed       (Folder         *folder,
96                                          FolderItem     *item,
97                                          MsgInfo        *msginfo);
98
99 static gint     mh_get_num_list         (Folder         *folder,
100                                          FolderItem     *item, 
101                                          GSList         **list, 
102                                          gboolean       *old_uids_valid);
103 static gint     mh_scan_tree            (Folder         *folder);
104
105 static gint    mh_create_tree           (Folder         *folder);
106 static FolderItem *mh_create_folder     (Folder         *folder,
107                                          FolderItem     *parent,
108                                          const gchar    *name);
109 static gint    mh_rename_folder         (Folder         *folder,
110                                          FolderItem     *item,
111                                          const gchar    *name);
112 static gint    mh_remove_folder         (Folder         *folder,
113                                          FolderItem     *item);
114
115 static gchar   *mh_get_new_msg_filename         (FolderItem     *dest);
116
117 static MsgInfo *mh_parse_msg                    (const gchar    *file,
118                                                  FolderItem     *item);
119 static void     mh_remove_missing_folder_items  (Folder         *folder);
120 static gchar    *mh_filename_from_utf8          (const gchar    *path);
121 static gchar    *mh_filename_to_utf8            (const gchar    *path);
122 static void     mh_scan_tree_recursive          (FolderItem     *item);
123
124 static gboolean mh_rename_folder_func           (GNode          *node,
125                                                  gpointer        data);
126 static gchar   *mh_item_get_path                (Folder *folder, 
127                                                  FolderItem *item);
128
129 static gboolean mh_scan_required        (Folder         *folder,
130                                          FolderItem     *item);
131 static void mh_set_mtime                (Folder         *folder,
132                                          FolderItem *item);
133 static int mh_item_close                (Folder         *folder,
134                                          FolderItem     *item);
135 #if 0
136 static gint mh_get_flags                (Folder *folder, FolderItem *item,
137                                          MsgInfoList *msginfo_list, GRelation *msgflags);
138 #endif
139 static void mh_write_sequences          (FolderItem     *item, gboolean remove_unseen);
140
141 static FolderClass mh_class;
142
143 FolderClass *mh_get_class(void)
144 {
145         if (mh_class.idstr == NULL) {
146                 mh_class.type = F_MH;
147                 mh_class.idstr = "mh";
148                 mh_class.uistr = "MH";
149                 
150                 /* Folder functions */
151                 mh_class.new_folder = mh_folder_new;
152                 mh_class.destroy_folder = mh_folder_destroy;
153                 mh_class.set_xml = folder_local_set_xml;
154                 mh_class.get_xml = folder_local_get_xml;
155                 mh_class.scan_tree = mh_scan_tree;
156                 mh_class.create_tree = mh_create_tree;
157
158                 /* FolderItem functions */
159                 mh_class.item_get_path = mh_item_get_path;
160                 mh_class.create_folder = mh_create_folder;
161                 mh_class.rename_folder = mh_rename_folder;
162                 mh_class.remove_folder = mh_remove_folder;
163                 mh_class.get_num_list = mh_get_num_list;
164                 mh_class.scan_required = mh_scan_required;
165                 mh_class.set_mtime = mh_set_mtime;
166                 mh_class.close = mh_item_close;
167                 mh_class.get_flags = NULL; /*mh_get_flags */;
168
169                 /* Message functions */
170                 mh_class.get_msginfo = mh_get_msginfo;
171                 mh_class.fetch_msg = mh_fetch_msg;
172                 mh_class.add_msg = mh_add_msg;
173                 mh_class.add_msgs = mh_add_msgs;
174                 mh_class.copy_msg = mh_copy_msg;
175                 mh_class.copy_msgs = mh_copy_msgs;
176                 mh_class.remove_msg = mh_remove_msg;
177                 mh_class.remove_msgs = mh_remove_msgs;
178                 mh_class.remove_all_msg = mh_remove_all_msg;
179                 mh_class.is_msg_changed = mh_is_msg_changed;
180         }
181
182         return &mh_class;
183 }
184
185 static Folder *mh_folder_new(const gchar *name, const gchar *path)
186 {
187         Folder *folder;
188
189         folder = (Folder *)g_new0(MHFolder, 1);
190         folder->klass = &mh_class;
191         mh_folder_init(folder, name, path);
192
193         return folder;
194 }
195
196 static void mh_folder_destroy(Folder *folder)
197 {
198         folder_local_folder_destroy(LOCAL_FOLDER(folder));
199 }
200
201 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
202 {
203         folder_local_folder_init(folder, name, path);
204
205 }
206
207 gboolean mh_scan_required(Folder *folder, FolderItem *item)
208 {
209         gchar *path;
210         struct stat s;
211
212         path = folder_item_get_path(item);
213         g_return_val_if_fail(path != NULL, FALSE);
214
215         if (stat(path, &s) < 0) {
216                 FILE_OP_ERROR(path, "stat");
217                 g_free(path);
218                 return FALSE;
219         }
220
221         if ((s.st_mtime > item->mtime) &&
222                 (s.st_mtime - 3600 != item->mtime)) {
223                 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
224                             path?path:"(null)",
225                             (long int) s.st_mtime,
226                             (long int) item->mtime);
227                 g_free(path);
228                 return TRUE;
229         }
230
231         debug_print("MH scan not required: %s (%ld <= %ld)\n",
232                     path?path:"(null)",
233                     (long int) s.st_mtime,
234                     (long int) item->mtime);
235         g_free(path);
236         return FALSE;
237 }
238
239 static void mh_get_last_num(Folder *folder, FolderItem *item)
240 {
241         gchar *path;
242         DIR *dp;
243         struct dirent *d;
244         gint max = 0;
245         gint num;
246
247         g_return_if_fail(item != NULL);
248
249         debug_print("mh_get_last_num(): Scanning %s ...\n", item->path?item->path:"(null)");
250
251         path = folder_item_get_path(item);
252         g_return_if_fail(path != NULL);
253         if (change_dir(path) < 0) {
254                 g_free(path);
255                 return;
256         }
257         g_free(path);
258
259         if ((dp = opendir(".")) == NULL) {
260                 FILE_OP_ERROR(item->path, "opendir");
261                 return;
262         }
263
264         while ((d = readdir(dp)) != NULL) {
265                 if ((num = to_number(d->d_name)) > 0 &&
266                     dirent_is_regular_file(d)) {
267                         if (max < num)
268                                 max = num;
269                 }
270                 if (num % 2000 == 0)
271                         GTK_EVENTS_FLUSH();
272         }
273         closedir(dp);
274
275         debug_print("Last number in dir %s = %d\n", item->path?item->path:"(null)", max);
276         item->last_num = max;
277 }
278
279 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
280 {
281
282         gchar *path;
283         DIR *dp;
284         struct dirent *d;
285         gint num, nummsgs = 0;
286
287         g_return_val_if_fail(item != NULL, -1);
288
289         debug_print("mh_get_num_list(): Scanning %s ...\n", item->path?item->path:"(null)");
290
291         *old_uids_valid = TRUE;
292
293         path = folder_item_get_path(item);
294         g_return_val_if_fail(path != NULL, -1);
295         if (change_dir(path) < 0) {
296                 g_free(path);
297                 return -1;
298         }
299         g_free(path);
300
301         if ((dp = opendir(".")) == NULL) {
302                 FILE_OP_ERROR(item->path, "opendir");
303                 return -1;
304         }
305
306         while ((d = readdir(dp)) != NULL) {
307                 if ((num = to_number(d->d_name)) > 0) {
308                         *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
309                         nummsgs++;
310                 }
311         }
312         closedir(dp);
313
314         mh_set_mtime(folder, item);
315         return nummsgs;
316 }
317
318 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
319 {
320         gchar *path;
321         gchar *file;
322
323         g_return_val_if_fail(item != NULL, NULL);
324         g_return_val_if_fail(num > 0, NULL);
325
326         path = folder_item_get_path(item);
327         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
328
329         if (!is_file_exist(file)) {
330                 g_free(file);
331                 g_free(path);
332                 return NULL;
333         }
334         g_free(path);
335         return file;
336 }
337
338 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
339 {
340         MsgInfo *msginfo;
341         gchar *file;
342
343         g_return_val_if_fail(item != NULL, NULL);
344         if (num <= 0)
345                 return NULL;
346
347         file = mh_fetch_msg(folder, item, num);
348         if (!file) return NULL;
349
350         msginfo = mh_parse_msg(file, item);
351         if (msginfo)
352                 msginfo->msgnum = num;
353
354         g_free(file);
355
356         return msginfo;
357 }
358
359 static gchar *mh_get_new_msg_filename(FolderItem *dest)
360 {
361         gchar *destfile;
362         gchar *destpath;
363
364         destpath = folder_item_get_path(dest);
365         g_return_val_if_fail(destpath != NULL, NULL);
366
367         if (!is_dir_exist(destpath))
368                 make_dir_hier(destpath);
369
370         for (;;) {
371                 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
372                                            dest->last_num + 1);
373                 if (is_file_entry_exist(destfile)) {
374                         dest->last_num++;
375                         g_free(destfile);
376                 } else
377                         break;
378         }
379
380         g_free(destpath);
381
382         return destfile;
383 }
384
385 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
386 {
387         gint ret;
388         GSList file_list;
389         MsgFileInfo fileinfo;
390
391         g_return_val_if_fail(file != NULL, -1);
392
393         fileinfo.msginfo = NULL;
394         fileinfo.file = (gchar *)file;
395         fileinfo.flags = flags;
396         file_list.data = &fileinfo;
397         file_list.next = NULL;
398
399         ret = mh_add_msgs(folder, dest, &file_list, NULL);
400         return ret;
401
402  
403 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list, 
404                  GRelation *relation)
405
406         gchar *destfile;
407         GSList *cur;
408         MsgFileInfo *fileinfo;
409
410         g_return_val_if_fail(dest != NULL, -1);
411         g_return_val_if_fail(file_list != NULL, -1);
412
413         if (dest->last_num < 0) {
414                 mh_get_last_num(folder, dest);
415                 if (dest->last_num < 0) return -1;
416         }
417
418         for (cur = file_list; cur != NULL; cur = cur->next) {
419                 fileinfo = (MsgFileInfo *)cur->data;
420
421                 destfile = mh_get_new_msg_filename(dest);
422                 if (destfile == NULL) return -1;
423
424 #ifdef G_OS_UNIX
425                 if (link(fileinfo->file, destfile) < 0) {
426 #endif
427                         if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
428                                 g_warning(_("can't copy message %s to %s\n"),
429                                           fileinfo->file, destfile);
430                                 g_free(destfile);
431                                 return -1;
432                         }
433 #ifdef G_OS_UNIX
434                 }
435 #endif
436
437                 if (relation != NULL)
438                         g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
439                 g_free(destfile);
440                 dest->last_num++;
441         }
442         mh_write_sequences(dest, TRUE);
443         return dest->last_num;
444 }
445
446 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
447 {
448         GSList msglist;
449
450         g_return_val_if_fail(msginfo != NULL, -1);
451
452         msglist.data = msginfo;
453         msglist.next = NULL;
454
455         return mh_copy_msgs(folder, dest, &msglist, NULL);      
456 }
457
458 static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist, 
459                          GRelation *relation)
460 {
461         gboolean dest_need_scan = FALSE;
462         gboolean src_need_scan = FALSE;
463         FolderItem *src = NULL;
464         gchar *srcfile;
465         gchar *destfile;
466         gint filemode = 0;
467         FolderItemPrefs *prefs;
468         MsgInfo *msginfo = NULL;
469         MsgInfoList *cur = NULL;
470         gint curnum = 0, total = 0;
471         gchar *srcpath = NULL;
472         gboolean full_fetch = FALSE;
473         time_t last_dest_mtime = (time_t)0;
474         time_t last_src_mtime = (time_t)0;
475
476         g_return_val_if_fail(dest != NULL, -1);
477         g_return_val_if_fail(msglist != NULL, -1);
478         
479         msginfo = (MsgInfo *)msglist->data;
480
481         g_return_val_if_fail(msginfo != NULL, -1);
482
483         if (msginfo->folder == dest) {
484                 g_warning("the src folder is identical to the dest.\n");
485                 return -1;
486         }
487
488         if (msginfo->folder->folder != dest->folder)
489                 full_fetch = TRUE;
490         
491         if (FOLDER_TYPE(msginfo->folder->folder) == F_MH) {
492                 src = msginfo->folder;
493         }
494
495         if (dest->last_num < 0) {
496                 mh_get_last_num(folder, dest);
497                 if (dest->last_num < 0) return -1;
498         }
499
500         prefs = dest->prefs;
501
502         srcpath = folder_item_get_path(msginfo->folder);
503
504         dest_need_scan = mh_scan_required(dest->folder, dest);
505         last_dest_mtime = dest->mtime;
506
507         if (src) {
508                 src_need_scan = mh_scan_required(src->folder, src);
509                 last_src_mtime = src->mtime;
510         }
511
512         total = g_slist_length(msglist);
513         if (total > 100) {
514                 if (MSG_IS_MOVE(msginfo->flags))
515                         statusbar_print_all(_("Moving messages..."));
516                 else
517                         statusbar_print_all(_("Copying messages..."));
518         }
519         for (cur = msglist; cur; cur = cur->next) {
520                 msginfo = (MsgInfo *)cur->data;
521                 if (!msginfo) {
522                         goto err_reset_status;
523                 }
524                 if (!full_fetch) {
525                         srcfile = g_strconcat(srcpath, 
526                                 G_DIR_SEPARATOR_S, 
527                                 itos(msginfo->msgnum), NULL);
528                 } else {
529                         srcfile = procmsg_get_message_file(msginfo);
530                 }
531                 if (!srcfile) {
532                         goto err_reset_status;
533                 }
534                 destfile = mh_get_new_msg_filename(dest);
535                 if (!destfile) {
536                         g_free(srcfile);
537                         goto err_reset_status;
538                 }
539
540                 if (total > 100) {
541                         statusbar_progress_all(curnum, total, 100);
542                         if (curnum % 100 == 0)
543                                 GTK_EVENTS_FLUSH();
544                         curnum++;
545                 }
546
547                 debug_print("Copying message %s%c%d to %s ...\n",
548                             msginfo->folder->path, G_DIR_SEPARATOR,
549                             msginfo->msgnum, dest->path);
550
551
552                 if (MSG_IS_MOVE(msginfo->flags)) {
553                         msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
554                         if (move_file(srcfile, destfile, TRUE) < 0) {
555                                 FILE_OP_ERROR(srcfile, "move");
556                                 if (copy_file(srcfile, destfile, TRUE) < 0) {
557                                         FILE_OP_ERROR(srcfile, "copy");
558                                         g_free(srcfile);
559                                         g_free(destfile);
560                                         goto err_reset_status;
561                                 }
562                         } else {
563                                 /* say unlinking's not necessary */
564                                 msginfo->flags.tmp_flags |= MSG_MOVE_DONE;
565                         }
566                 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
567                         FILE_OP_ERROR(srcfile, "copy");
568                         g_free(srcfile);
569                         g_free(destfile);
570                         goto err_reset_status;
571                 } 
572                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
573                         if (chmod(destfile, prefs->folder_chmod) < 0)
574                                 FILE_OP_ERROR(destfile, "chmod");
575
576                         /* for mark file */
577                         filemode = prefs->folder_chmod;
578                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
579                         if (filemode & S_IROTH) filemode |= S_IWOTH;
580                 }
581                 if (relation)
582                         g_relation_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
583                 g_free(srcfile);
584                 g_free(destfile);
585                 dest->last_num++;
586         }
587
588         g_free(srcpath);
589         mh_write_sequences(dest, TRUE);
590
591         if (dest->mtime == last_dest_mtime && !dest_need_scan) {
592                 mh_set_mtime(folder, dest);
593         }
594
595         if (src && src->mtime == last_src_mtime && !src_need_scan) {
596                 mh_set_mtime(folder, src);
597         }
598
599         if (total > 100) {
600                 statusbar_progress_all(0,0,0);
601                 statusbar_pop_all();
602         }
603         return dest->last_num;
604 err_reset_status:
605         g_free(srcpath);
606         mh_write_sequences(dest, TRUE);
607         if (total > 100) {
608                 statusbar_progress_all(0,0,0);
609                 statusbar_pop_all();
610         }
611         return -1;
612
613 }
614
615 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
616 {
617         gboolean need_scan = FALSE;
618         time_t last_mtime = (time_t)0;
619         gchar *file;
620
621         g_return_val_if_fail(item != NULL, -1);
622
623         file = mh_fetch_msg(folder, item, num);
624         g_return_val_if_fail(file != NULL, -1);
625
626         need_scan = mh_scan_required(folder, item);
627         last_mtime = item->mtime;
628
629         if (claws_unlink(file) < 0) {
630                 FILE_OP_ERROR(file, "unlink");
631                 g_free(file);
632                 return -1;
633         }
634
635         if (item->mtime == last_mtime && !need_scan) {
636                 mh_set_mtime(folder, item);
637         }
638         g_free(file);
639         return 0;
640 }
641
642 static gint mh_remove_msgs(Folder *folder, FolderItem *item, 
643                     MsgInfoList *msglist, GRelation *relation)
644 {
645         gboolean need_scan = FALSE;
646         gchar *path, *file;
647         time_t last_mtime = (time_t)0;
648         MsgInfoList *cur;
649         gint total = 0, curnum = 0;
650
651         g_return_val_if_fail(item != NULL, -1);
652
653         path = folder_item_get_path(item);
654         
655         need_scan = mh_scan_required(folder, item);
656         last_mtime = item->mtime;
657
658         total = g_slist_length(msglist);
659         if (total > 100) {
660                 statusbar_print_all(_("Deleting messages..."));
661         }
662
663         for (cur = msglist; cur; cur = cur->next) {
664                 MsgInfo *msginfo = (MsgInfo *)cur->data;
665                 if (msginfo == NULL)
666                         continue;
667                 if (MSG_IS_MOVE(msginfo->flags) && MSG_IS_MOVE_DONE(msginfo->flags)) {
668                         msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
669                         continue;
670                 }
671                 if (total > 100) {
672                         statusbar_progress_all(curnum, total, 100);
673                         if (curnum % 100 == 0)
674                                 GTK_EVENTS_FLUSH();
675                         curnum++;
676                 }
677
678                 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
679                 if (file == NULL)
680                         continue;
681                 
682                 if (claws_unlink(file) < 0) {
683                         g_free(file);
684                         continue;
685                 }
686                 
687                 g_free(file);
688         }
689
690         if (total > 100) {
691                 statusbar_progress_all(0,0,0);
692                 statusbar_pop_all();
693         }
694         if (item->mtime == last_mtime && !need_scan) {
695                 mh_set_mtime(folder, item);
696         }
697
698         g_free(path);
699         return 0;
700 }
701
702 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
703 {
704         gchar *path;
705         gint val;
706
707         g_return_val_if_fail(item != NULL, -1);
708
709         path = folder_item_get_path(item);
710         g_return_val_if_fail(path != NULL, -1);
711         val = remove_all_numbered_files(path);
712         g_free(path);
713
714         mh_write_sequences(item, TRUE);
715
716         return val;
717 }
718
719 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
720                                   MsgInfo *msginfo)
721 {
722         struct stat s;
723
724         if (g_stat(itos(msginfo->msgnum), &s) < 0 ||
725             msginfo->size  != s.st_size || (
726                 (msginfo->mtime - s.st_mtime != 0) &&
727                 (msginfo->mtime - s.st_mtime != 3600) &&
728                 (msginfo->mtime - s.st_mtime != -3600)))
729                 return TRUE;
730
731         return FALSE;
732 }
733
734 static gint mh_scan_tree(Folder *folder)
735 {
736         FolderItem *item;
737         gchar *rootpath;
738
739         g_return_val_if_fail(folder != NULL, -1);
740
741         if (!folder->node) {
742                 item = folder_item_new(folder, folder->name, NULL);
743                 item->folder = folder;
744                 folder->node = item->node = g_node_new(item);
745         } else
746                 item = FOLDER_ITEM(folder->node->data);
747
748         rootpath = folder_item_get_path(item);
749         if (change_dir(rootpath) < 0) {
750                 g_free(rootpath);
751                 return -1;
752         }
753         g_free(rootpath);
754
755         mh_create_tree(folder);
756         mh_remove_missing_folder_items(folder);
757         mh_scan_tree_recursive(item);
758
759         return 0;
760 }
761
762 #define MAKE_DIR_IF_NOT_EXIST(dir) \
763 { \
764         if (!is_dir_exist(dir)) { \
765                 if (is_file_exist(dir)) { \
766                         g_warning("File `%s' already exists.\n" \
767                                     "Can't create folder.", dir); \
768                         return -1; \
769                 } \
770                 if (make_dir_hier(dir) < 0) \
771                         return -1; \
772         } \
773 }
774
775 static gint mh_create_tree(Folder *folder)
776 {
777         gchar *rootpath;
778
779         g_return_val_if_fail(folder != NULL, -1);
780
781         CHDIR_RETURN_VAL_IF_FAIL(get_mail_base_dir(), -1);
782         rootpath = LOCAL_FOLDER(folder)->rootpath;
783         MAKE_DIR_IF_NOT_EXIST(rootpath);
784         CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
785         MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
786         MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
787         MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
788         MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
789         MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
790
791         return 0;
792 }
793
794 #undef MAKE_DIR_IF_NOT_EXIST
795
796 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
797 {
798         gchar *folder_path, *path;
799         gchar *real_path;
800         g_return_val_if_fail(folder != NULL, NULL);
801         g_return_val_if_fail(item != NULL, NULL);
802
803         folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
804         g_return_val_if_fail(folder_path != NULL, NULL);
805
806         /* FIXME: [W32] The code below does not correctly merge
807            relative filenames; there should be a function to handle
808            this.  */
809         if ( !is_relative_filename (folder_path) ) {
810                 if (item->path)
811                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
812                                            item->path, NULL);
813                 else
814                         path = g_strdup(folder_path);
815         } else {
816                 if (item->path)
817                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
818                                            folder_path, G_DIR_SEPARATOR_S,
819                                            item->path, NULL);
820                 else
821                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
822                                            folder_path, NULL);
823         }
824         g_free(folder_path);
825         real_path = mh_filename_from_utf8(path);
826         if (!is_dir_exist(real_path) && is_dir_exist(path)) {
827                 /* mmh, older version did put utf8 filenames instead of
828                  * the correct encoding */
829                 rename(path, real_path);
830                 folder_item_scan(item);
831         }
832
833         g_free(path);
834         return real_path;
835 }
836
837 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
838                                     const gchar *name)
839 {
840         gchar *path, *real_name;
841         gchar *fullpath;
842         FolderItem *new_item;
843         gchar *mh_sequences_filename;
844         FILE *mh_sequences_file;
845
846         g_return_val_if_fail(folder != NULL, NULL);
847         g_return_val_if_fail(parent != NULL, NULL);
848         g_return_val_if_fail(name != NULL, NULL);
849
850         path = folder_item_get_path(parent);
851         if (!is_dir_exist(path)) 
852                 if (make_dir_hier(path) != 0)
853                         return NULL;
854                 
855         real_name = mh_filename_from_utf8(name);
856         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
857         g_free(real_name);
858         g_free(path);
859
860         if (make_dir(fullpath) < 0) {
861                 g_free(fullpath);
862                 return NULL;
863         }
864
865         g_free(fullpath);
866
867         if (parent->path)
868                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
869                                    NULL);
870         else
871                 path = g_strdup(name);
872         new_item = folder_item_new(folder, name, path);
873         folder_item_append(parent, new_item);
874
875         g_free(path);
876
877         path = folder_item_get_path(new_item);
878         mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
879                                             ".mh_sequences", NULL);
880         if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
881                 fclose(mh_sequences_file);
882         }
883         g_free(mh_sequences_filename);
884         g_free(path);
885
886         return new_item;
887 }
888
889 static gint mh_rename_folder(Folder *folder, FolderItem *item,
890                              const gchar *name)
891 {
892         gchar *real_name;
893         gchar *oldpath;
894         gchar *dirname;
895         gchar *newpath, *utf8newpath;
896         gchar *paths[2];
897
898         g_return_val_if_fail(folder != NULL, -1);
899         g_return_val_if_fail(item != NULL, -1);
900         g_return_val_if_fail(item->path != NULL, -1);
901         g_return_val_if_fail(name != NULL, -1);
902
903         oldpath = folder_item_get_path(item);
904         if (!is_dir_exist(oldpath))
905                 make_dir_hier(oldpath);
906
907         dirname = g_path_get_dirname(oldpath);
908         real_name = mh_filename_from_utf8(name);
909         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
910         g_free(real_name);
911
912         if (g_rename(oldpath, newpath) < 0) {
913                 FILE_OP_ERROR(oldpath, "rename");
914                 g_free(oldpath);
915                 g_free(newpath);
916                 return -1;
917         }
918
919         g_free(oldpath);
920         g_free(newpath);
921
922         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
923                 dirname = g_path_get_dirname(item->path);
924                 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
925                                           name, NULL);
926                 g_free(dirname);
927         } else
928                 utf8newpath = g_strdup(name);
929
930         g_free(item->name);
931         item->name = g_strdup(name);
932
933         paths[0] = g_strdup(item->path);
934         paths[1] = utf8newpath;
935         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
936                         mh_rename_folder_func, paths);
937
938         g_free(paths[0]);
939         g_free(paths[1]);
940         return 0;
941 }
942
943 static gint mh_remove_folder(Folder *folder, FolderItem *item)
944 {
945         gchar *path;
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
951         path = folder_item_get_path(item);
952         if (remove_dir_recursive(path) < 0) {
953                 g_warning("can't remove directory `%s'\n", path);
954                 g_free(path);
955                 return -1;
956         }
957
958         g_free(path);
959         folder_item_remove(item);
960         return 0;
961 }
962
963 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
964 {
965         MsgInfo *msginfo;
966         MsgFlags flags;
967
968         g_return_val_if_fail(item != NULL, NULL);
969         g_return_val_if_fail(file != NULL, NULL);
970
971         flags.perm_flags = MSG_NEW|MSG_UNREAD;
972         flags.tmp_flags = 0;
973
974         if (folder_has_parent_of_type(item, F_QUEUE)) {
975                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
976         } else if (folder_has_parent_of_type(item, F_DRAFT)) {
977                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
978         }
979
980         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
981         if (!msginfo) return NULL;
982
983         msginfo->msgnum = atoi(file);
984         msginfo->folder = item;
985
986         return msginfo;
987 }
988
989 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
990 {
991         FolderItem *item;
992         gchar *path;
993
994         g_return_val_if_fail(node->data != NULL, FALSE);
995
996         if (G_NODE_IS_ROOT(node))
997                 return FALSE;
998
999         item = FOLDER_ITEM(node->data);
1000
1001         path = folder_item_get_path(item);
1002         if (!is_dir_exist(path)) {
1003                 debug_print("folder '%s' not found. removing...\n", path?path:"(null)");
1004                 folder_item_remove(item);
1005         }
1006         g_free(path);
1007
1008         return FALSE;
1009 }
1010
1011 static void mh_remove_missing_folder_items(Folder *folder)
1012 {
1013         g_return_if_fail(folder != NULL);
1014
1015         debug_print("searching missing folders...\n");
1016
1017         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1018                         mh_remove_missing_folder_items_func, folder);
1019 }
1020
1021 static void mh_scan_tree_recursive(FolderItem *item)
1022 {
1023         Folder *folder;
1024 #ifdef G_OS_WIN32
1025         GDir *dir;
1026 #else
1027         DIR *dp;
1028         struct dirent *d;
1029 #endif
1030         const gchar *dir_name;
1031         struct stat s;
1032         gchar *real_path, *entry, *utf8entry, *utf8name;
1033         gint n_msg = 0;
1034
1035         g_return_if_fail(item != NULL);
1036         g_return_if_fail(item->folder != NULL);
1037
1038         folder = item->folder;
1039
1040         real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
1041 #ifdef G_OS_WIN32
1042         dir = g_dir_open(real_path, 0, NULL);
1043         if (!dir) {
1044                 g_warning("failed to open directory: %s\n", real_path);
1045                 g_free(real_path);
1046                 return;
1047         }
1048 #else
1049         dp = opendir(real_path);
1050         if (!dp) {
1051                 FILE_OP_ERROR(real_path, "opendir");
1052                 return;
1053         }
1054 #endif
1055         g_free(real_path);
1056
1057         debug_print("scanning %s ...\n",
1058                     item->path ? item->path
1059                     : LOCAL_FOLDER(item->folder)->rootpath);
1060         if (folder->ui_func)
1061                 folder->ui_func(folder, item, folder->ui_func_data);
1062
1063 #ifdef G_OS_WIN32
1064         while ((dir_name = g_dir_read_name(dir)) != NULL) {
1065 #else
1066         while ((d = readdir(dp)) != NULL) {
1067                 dir_name = d->d_name;
1068 #endif
1069                 if (dir_name[0] == '.') continue;
1070
1071                 utf8name = mh_filename_to_utf8(dir_name);
1072                 if (item->path)
1073                         utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1074                                                 utf8name, NULL);
1075                 else
1076                         utf8entry = g_strdup(utf8name);
1077                 entry = mh_filename_from_utf8(utf8entry);
1078
1079                 if (
1080 #if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
1081                         d->d_type == DT_DIR ||
1082                         (d->d_type == DT_UNKNOWN &&
1083 #endif
1084                         g_stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
1085 #if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
1086                         )
1087 #endif
1088                    ) {
1089                         FolderItem *new_item = NULL;
1090                         GNode *node;
1091
1092                         node = item->node;
1093                         for (node = node->children; node != NULL; node = node->next) {
1094                                 FolderItem *cur_item = FOLDER_ITEM(node->data);
1095                                 gchar *curpath = mh_filename_from_utf8(cur_item->path);
1096                                 if (!strcmp2(curpath, entry)) {
1097                                         new_item = cur_item;
1098                                         g_free(curpath);
1099                                         break;
1100                                 }
1101                                 g_free(curpath);
1102                         }
1103                         if (!new_item) {
1104                                 debug_print("new folder '%s' found.\n", entry);
1105                                 new_item = folder_item_new(folder, utf8name, utf8entry);
1106                                 folder_item_append(item, new_item);
1107                         }
1108
1109                         if (!item->path) {
1110                                 if (!folder->inbox &&
1111                                     !strcmp(dir_name, INBOX_DIR)) {
1112                                         new_item->stype = F_INBOX;
1113                                         folder->inbox = new_item;
1114                                 } else if (!folder->outbox &&
1115                                            !strcmp(dir_name, OUTBOX_DIR)) {
1116                                         new_item->stype = F_OUTBOX;
1117                                         folder->outbox = new_item;
1118                                 } else if (!folder->draft &&
1119                                            !strcmp(dir_name, DRAFT_DIR)) {
1120                                         new_item->stype = F_DRAFT;
1121                                         folder->draft = new_item;
1122                                 } else if (!folder->queue &&
1123                                            !strcmp(dir_name, QUEUE_DIR)) {
1124                                         new_item->stype = F_QUEUE;
1125                                         folder->queue = new_item;
1126                                 } else if (!folder->trash &&
1127                                            !strcmp(dir_name, TRASH_DIR)) {
1128                                         new_item->stype = F_TRASH;
1129                                         folder->trash = new_item;
1130                                 }
1131                         }
1132
1133                         mh_scan_tree_recursive(new_item);
1134                 } else if (to_number(dir_name) > 0) n_msg++;
1135
1136                 g_free(entry);
1137                 g_free(utf8entry);
1138                 g_free(utf8name);
1139         }
1140
1141 #ifdef G_OS_WIN32
1142         g_dir_close(dir);
1143 #else
1144         closedir(dp);
1145 #endif
1146
1147         mh_set_mtime(folder, item);
1148 }
1149
1150 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1151 {
1152         FolderItem *item = node->data;
1153         gchar **paths = data;
1154         const gchar *oldpath = paths[0];
1155         const gchar *newpath = paths[1];
1156         gchar *base;
1157         gchar *new_itempath;
1158         gint oldpathlen;
1159
1160         oldpathlen = strlen(oldpath);
1161         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1162                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1163                 return TRUE;
1164         }
1165
1166         base = item->path + oldpathlen;
1167         while (*base == G_DIR_SEPARATOR) base++;
1168         if (*base == '\0')
1169                 new_itempath = g_strdup(newpath);
1170         else
1171                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1172                                            NULL);
1173         g_free(item->path);
1174         item->path = new_itempath;
1175
1176         return FALSE;
1177 }
1178
1179 static gchar *mh_filename_from_utf8(const gchar *path)
1180 {
1181         gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1182
1183         if (!real_path) {
1184                 g_warning("mh_filename_from_utf8: failed to convert character set\n");
1185                 real_path = g_strdup(path);
1186         }
1187
1188         return real_path;
1189 }
1190
1191 static gchar *mh_filename_to_utf8(const gchar *path)
1192 {
1193         gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1194         if (!utf8path) {
1195                 g_warning("mh_filename_to_utf8: failed to convert character set\n");
1196                 utf8path = g_strdup(path);
1197         }
1198
1199         return utf8path;
1200 }
1201
1202 static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1203 {
1204         MsgInfo *msginfo_a = (MsgInfo *) a;
1205         MsgInfo *msginfo_b = (MsgInfo *) b;
1206
1207         return (msginfo_a->msgnum - msginfo_b->msgnum);
1208 }
1209
1210 static gchar *get_unseen_seq_name(void)
1211 {
1212         static gchar *seq_name = NULL;
1213         if (!seq_name) {
1214                 gchar buf[BUFFSIZE];
1215                 gchar *tmp;
1216                 gchar *profile_path = g_strconcat(
1217                         get_home_dir(), G_DIR_SEPARATOR_S,
1218                         ".mh_profile", NULL);
1219                 FILE *fp = g_fopen(profile_path, "r");
1220                 if (fp) {
1221                         while (fgets(buf, sizeof(buf), fp) != NULL) {
1222                                 if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
1223                                         gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
1224                                         while (*seq_tmp == ' ')
1225                                                 seq_tmp++;
1226                                         seq_name = g_strdup(seq_tmp);
1227                                         seq_name = strretchomp(seq_name);
1228                                         break;
1229                                 }
1230                         }
1231                         fclose(fp);
1232                 }
1233                 if (!seq_name)
1234                         seq_name = g_strdup("unseen");
1235                 tmp = g_strdup_printf("%s:", seq_name);
1236                 g_free(seq_name);
1237                 seq_name = tmp;
1238         }
1239         return seq_name;        
1240 }
1241
1242 #if 0
1243 static gint mh_get_flags(Folder *folder, FolderItem *item,
1244                            MsgInfoList *msginfo_list, GRelation *msgflags)
1245 {
1246         gchar *mh_sequences_filename;
1247         FILE *mh_sequences_file;
1248         gchar buf[BUFFSIZE];
1249         gchar *unseen_list = NULL;
1250         gchar *path;
1251         MsgInfoList *mcur = NULL;
1252 /*
1253         GTimer *timer = g_timer_new();
1254         g_timer_start(timer);
1255 */
1256         if (!item)
1257                 return 0;
1258
1259         /* don't update from .mh_sequences if the item's opened: mails may have
1260          * been marked read/unread and it's not yet written in the file. */     
1261         if (item->opened)
1262                 return 0;
1263
1264         path = folder_item_get_path(item);
1265
1266         mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
1267                                             ".mh_sequences", NULL);
1268         g_free(path);
1269         if ((mh_sequences_file = g_fopen(mh_sequences_filename, "r+b")) != NULL) {
1270                 while (fgets(buf, sizeof(buf), mh_sequences_file) != NULL) {
1271                         if (!strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name()))) {
1272                                 unseen_list = g_strdup(buf+strlen(get_unseen_seq_name()));
1273                                 break;
1274                         }
1275                 }
1276                 fclose(mh_sequences_file);
1277         }
1278         
1279         g_free(mh_sequences_filename);
1280         
1281         if (unseen_list) {
1282                 gchar *cur = NULL;
1283                 gchar *token = NULL, *next = NULL, *boundary = NULL;
1284                 gint num = 0;
1285                 GHashTable *unseen_table = g_hash_table_new(g_direct_hash, g_direct_equal);
1286
1287                 cur = unseen_list = strretchomp(unseen_list);
1288                 debug_print("found unseen list in .mh_sequences: %s\n", unseen_list);
1289 next_token:
1290                 while (*cur && *cur == ' ')
1291                         cur++;
1292                 
1293                 if ((next = strchr(cur, ' ')) != NULL) {
1294                         token = cur;
1295                         cur = next+1;
1296                         *next = '\0';
1297                 } else {
1298                         token = cur;
1299                         cur = NULL;
1300                 }
1301                 
1302                 if ((boundary = strchr(token, '-')) != NULL) {
1303                         gchar *start, *end;
1304                         int i;
1305                         start = token;
1306                         end = boundary+1;
1307                         *boundary='\0';
1308                         for (i = atoi(start); i <= atoi(end); i++) {
1309                                 g_hash_table_insert(unseen_table, GINT_TO_POINTER(i), GINT_TO_POINTER(1));
1310                         }
1311                 } else if ((num = atoi(token)) > 0) {
1312                         g_hash_table_insert(unseen_table, GINT_TO_POINTER(num), GINT_TO_POINTER(1));
1313                 }
1314                 
1315                 if (cur)
1316                         goto next_token;
1317                 for (mcur = msginfo_list; mcur; mcur = mcur->next) {
1318                         MsgInfo *msginfo = (MsgInfo *)mcur->data;
1319                         MsgPermFlags flags = msginfo->flags.perm_flags;
1320                         if (g_hash_table_lookup(unseen_table, GINT_TO_POINTER(msginfo->msgnum))) {
1321                                 flags |= MSG_UNREAD;
1322                         } else if (!(flags & MSG_NEW)) { /* don't mark new msgs as read */
1323                                 flags &= ~(MSG_UNREAD);
1324                         }
1325                         if (flags != msginfo->flags.perm_flags)
1326                                 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
1327                 }
1328                 g_hash_table_destroy(unseen_table);
1329                 g_free(unseen_list);
1330         }
1331 /*
1332         g_timer_stop(timer);
1333         g_print("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
1334         g_timer_destroy(timer);
1335 */
1336         return 0;
1337 }
1338 #endif
1339
1340 static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
1341 {
1342         gchar *mh_sequences_old, *mh_sequences_new;
1343         FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
1344         gchar buf[BUFFSIZE];
1345         gchar *path = NULL;
1346         gboolean err = FALSE;
1347         START_TIMING("");
1348
1349         if (!item)
1350                 return;
1351         
1352         path = folder_item_get_path(item);
1353
1354         mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
1355                                             ".mh_sequences", NULL);
1356         mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
1357                                             ".mh_sequences.new", NULL);
1358         if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
1359                 GSList *msglist = folder_item_get_msg_list(item);
1360                 GSList *cur;
1361                 MsgInfo *info = NULL;
1362                 gint start = -1, end = -1;
1363                 gchar *sequence = g_strdup("");
1364                 gint seq_len = 0;
1365                 msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
1366                 cur = msglist;
1367                 
1368                 /* write the unseen sequence if we don't have to scrap it */
1369                 if (!remove_unseen) do {
1370                         info = (MsgInfo *)(cur ? cur->data:NULL);
1371                         if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
1372                                 if (start < 0)
1373                                         start = end = info->msgnum;
1374                                 else
1375                                         end = info->msgnum;
1376                         } else {
1377                                 if (start > 0 && end > 0) {
1378                                         gchar tmp[32];
1379                                         gint tmp_len = 0;
1380                                         if (start != end)
1381                                                 snprintf(tmp, 31, " %d-%d", start, end);
1382                                         else
1383                                                 snprintf(tmp, 31, " %d", start);
1384                                         
1385                                         tmp_len = strlen(tmp);
1386                                         sequence = g_realloc(sequence, seq_len+tmp_len+1);
1387                                         strcpy(sequence+seq_len, tmp);
1388                                         seq_len += tmp_len;
1389
1390                                         start = end = -1;
1391                                 }
1392                         }
1393                         cur = cur ? cur->next:NULL;
1394                 } while (cur || (start > 0 && end > 0));
1395                 if (sequence && *sequence) {
1396                         if (fprintf(mh_sequences_new_fp, "%s%s\n", 
1397                                         get_unseen_seq_name(), sequence) < 0)
1398                                 err = TRUE;
1399                         else
1400                                 debug_print("wrote unseen sequence: '%s%s'\n", 
1401                                         get_unseen_seq_name(), sequence);
1402                 }
1403                 /* rewrite the rest of the file */
1404                 if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
1405                         while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
1406                                 if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
1407                                         if (fprintf(mh_sequences_new_fp, "%s", buf) < 0) {
1408                                                 err = TRUE;
1409                                                 break;
1410                                         }
1411                         }
1412                         fclose(mh_sequences_old_fp);
1413                 }
1414                 
1415                 fflush(mh_sequences_new_fp);
1416 #if 0
1417                 fsync(fileno(mh_sequences_new_fp));
1418 #endif
1419                 if (fclose(mh_sequences_new_fp) == EOF)
1420                         err = TRUE;
1421
1422                 if (!err)
1423                         g_rename(mh_sequences_new, mh_sequences_old);
1424                 g_free(sequence);
1425                 procmsg_msg_list_free(msglist);
1426         }
1427         g_free(mh_sequences_old);
1428         g_free(mh_sequences_new);
1429         g_free(path);
1430
1431         END_TIMING();
1432 }
1433
1434 static int mh_item_close(Folder *folder, FolderItem *item)
1435 {
1436         time_t last_mtime = (time_t)0;
1437         gboolean need_scan = mh_scan_required(item->folder, item);
1438         last_mtime = item->mtime;
1439
1440         mh_write_sequences(item, FALSE);
1441
1442         if (item->mtime == last_mtime && !need_scan) {
1443                 mh_set_mtime(folder, item);
1444         }
1445
1446         return 0;
1447 }
1448
1449 static void mh_set_mtime(Folder *folder, FolderItem *item)
1450 {
1451         struct stat s;
1452         gchar *path = folder_item_get_path(item);
1453
1454         g_return_if_fail(path != NULL);
1455
1456         if (stat(path, &s) < 0) {
1457                 FILE_OP_ERROR(path, "stat");
1458                 g_free(path);
1459                 return;
1460         }
1461
1462         item->mtime = s.st_mtime;
1463         debug_print("MH: forced mtime of %s to %ld\n", item->name?item->name:"(null)", item->mtime);
1464         g_free(path);
1465 }