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