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