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