2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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.
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.
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/>.
27 #include <glib/gi18n.h>
38 #include "procheader.h"
41 #include "statusbar.h"
45 /* Define possible missing constants for Windows. */
58 static void mh_folder_init (Folder *folder,
62 static Folder *mh_folder_new (const gchar *name,
64 static void mh_folder_destroy (Folder *folder);
65 static gchar *mh_fetch_msg (Folder *folder,
68 static MsgInfo *mh_get_msginfo (Folder *folder,
71 static gint mh_add_msg (Folder *folder,
75 static gint mh_add_msgs (Folder *folder,
79 static gint mh_copy_msg (Folder *folder,
82 static gint mh_copy_msgs (Folder *folder,
86 static gint mh_remove_msg (Folder *folder,
89 static gint mh_remove_msgs (Folder *folder,
93 static gint mh_remove_all_msg (Folder *folder,
95 static gboolean mh_is_msg_changed (Folder *folder,
99 static gint mh_get_num_list (Folder *folder,
102 gboolean *old_uids_valid);
103 static gint mh_scan_tree (Folder *folder);
105 static gint mh_create_tree (Folder *folder);
106 static FolderItem *mh_create_folder (Folder *folder,
109 static gint mh_rename_folder (Folder *folder,
112 static gint mh_remove_folder (Folder *folder,
115 static gchar *mh_get_new_msg_filename (FolderItem *dest);
117 static MsgInfo *mh_parse_msg (const gchar *file,
119 static void mh_remove_missing_folder_items (Folder *folder);
120 static gchar *mh_filename_from_utf8 (const gchar *path);
121 static gchar *mh_filename_to_utf8 (const gchar *path);
122 static void mh_scan_tree_recursive (FolderItem *item);
124 static gboolean mh_rename_folder_func (GNode *node,
126 static gchar *mh_item_get_path (Folder *folder,
129 static gboolean mh_scan_required (Folder *folder,
131 static int mh_item_close (Folder *folder,
134 static gint mh_get_flags (Folder *folder, FolderItem *item,
135 MsgInfoList *msginfo_list, GRelation *msgflags);
137 static void mh_write_sequences (FolderItem *item, gboolean remove_unseen);
139 static FolderClass mh_class;
141 FolderClass *mh_get_class(void)
143 if (mh_class.idstr == NULL) {
144 mh_class.type = F_MH;
145 mh_class.idstr = "mh";
146 mh_class.uistr = "MH";
148 /* Folder functions */
149 mh_class.new_folder = mh_folder_new;
150 mh_class.destroy_folder = mh_folder_destroy;
151 mh_class.set_xml = folder_local_set_xml;
152 mh_class.get_xml = folder_local_get_xml;
153 mh_class.scan_tree = mh_scan_tree;
154 mh_class.create_tree = mh_create_tree;
156 /* FolderItem functions */
157 mh_class.item_get_path = mh_item_get_path;
158 mh_class.create_folder = mh_create_folder;
159 mh_class.rename_folder = mh_rename_folder;
160 mh_class.remove_folder = mh_remove_folder;
161 mh_class.get_num_list = mh_get_num_list;
162 mh_class.scan_required = mh_scan_required;
163 mh_class.close = mh_item_close;
164 mh_class.get_flags = NULL; /*mh_get_flags */;
166 /* Message functions */
167 mh_class.get_msginfo = mh_get_msginfo;
168 mh_class.fetch_msg = mh_fetch_msg;
169 mh_class.add_msg = mh_add_msg;
170 mh_class.add_msgs = mh_add_msgs;
171 mh_class.copy_msg = mh_copy_msg;
172 mh_class.copy_msgs = mh_copy_msgs;
173 mh_class.remove_msg = mh_remove_msg;
174 mh_class.remove_msgs = mh_remove_msgs;
175 mh_class.remove_all_msg = mh_remove_all_msg;
176 mh_class.is_msg_changed = mh_is_msg_changed;
182 static Folder *mh_folder_new(const gchar *name, const gchar *path)
186 folder = (Folder *)g_new0(MHFolder, 1);
187 folder->klass = &mh_class;
188 mh_folder_init(folder, name, path);
193 static void mh_folder_destroy(Folder *folder)
195 folder_local_folder_destroy(LOCAL_FOLDER(folder));
198 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
200 folder_local_folder_init(folder, name, path);
204 gboolean mh_scan_required(Folder *folder, FolderItem *item)
209 path = folder_item_get_path(item);
210 g_return_val_if_fail(path != NULL, FALSE);
212 if (stat(path, &s) < 0) {
213 FILE_OP_ERROR(path, "stat");
218 if ((s.st_mtime > item->mtime) &&
219 (s.st_mtime - 3600 != item->mtime)) {
220 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
222 (long int) s.st_mtime,
223 (long int) item->mtime);
228 debug_print("MH scan not required: %s (%ld <= %ld)\n",
230 (long int) s.st_mtime,
231 (long int) item->mtime);
236 static void mh_get_last_num(Folder *folder, FolderItem *item)
244 g_return_if_fail(item != NULL);
246 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
248 path = folder_item_get_path(item);
249 g_return_if_fail(path != NULL);
250 if (change_dir(path) < 0) {
256 if ((dp = opendir(".")) == NULL) {
257 FILE_OP_ERROR(item->path, "opendir");
261 while ((d = readdir(dp)) != NULL) {
262 if ((num = to_number(d->d_name)) > 0 &&
263 dirent_is_regular_file(d)) {
270 debug_print("Last number in dir %s = %d\n", item->path, max);
271 item->last_num = max;
274 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
280 gint num, nummsgs = 0;
282 g_return_val_if_fail(item != NULL, -1);
284 debug_print("mh_get_num_list(): Scanning %s ...\n", item->path);
286 *old_uids_valid = TRUE;
288 path = folder_item_get_path(item);
289 g_return_val_if_fail(path != NULL, -1);
290 if (change_dir(path) < 0) {
296 if ((dp = opendir(".")) == NULL) {
297 FILE_OP_ERROR(item->path, "opendir");
301 while ((d = readdir(dp)) != NULL) {
302 if ((num = to_number(d->d_name)) > 0) {
303 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
313 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
318 g_return_val_if_fail(item != NULL, NULL);
319 g_return_val_if_fail(num > 0, NULL);
321 path = folder_item_get_path(item);
322 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
324 if (!is_file_exist(file)) {
333 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
338 g_return_val_if_fail(item != NULL, NULL);
342 file = mh_fetch_msg(folder, item, num);
343 if (!file) return NULL;
345 msginfo = mh_parse_msg(file, item);
347 msginfo->msgnum = num;
354 static gchar *mh_get_new_msg_filename(FolderItem *dest)
359 destpath = folder_item_get_path(dest);
360 g_return_val_if_fail(destpath != NULL, NULL);
362 if (!is_dir_exist(destpath))
363 make_dir_hier(destpath);
366 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
368 if (is_file_entry_exist(destfile)) {
380 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
384 MsgFileInfo fileinfo;
386 g_return_val_if_fail(file != NULL, -1);
388 fileinfo.msginfo = NULL;
389 fileinfo.file = (gchar *)file;
390 fileinfo.flags = flags;
391 file_list.data = &fileinfo;
392 file_list.next = NULL;
394 ret = mh_add_msgs(folder, dest, &file_list, NULL);
398 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
403 MsgFileInfo *fileinfo;
405 g_return_val_if_fail(dest != NULL, -1);
406 g_return_val_if_fail(file_list != NULL, -1);
408 if (dest->last_num < 0) {
409 mh_get_last_num(folder, dest);
410 if (dest->last_num < 0) return -1;
413 for (cur = file_list; cur != NULL; cur = cur->next) {
414 fileinfo = (MsgFileInfo *)cur->data;
416 destfile = mh_get_new_msg_filename(dest);
417 if (destfile == NULL) return -1;
420 if (link(fileinfo->file, destfile) < 0) {
422 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
423 g_warning(_("can't copy message %s to %s\n"),
424 fileinfo->file, destfile);
432 if (relation != NULL)
433 g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
437 mh_write_sequences(dest, TRUE);
438 return dest->last_num;
441 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
445 g_return_val_if_fail(msginfo != NULL, -1);
447 msglist.data = msginfo;
450 return mh_copy_msgs(folder, dest, &msglist, NULL);
453 static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist,
456 gboolean dest_need_scan = FALSE;
457 gboolean src_need_scan = FALSE;
458 FolderItem *src = NULL;
462 FolderItemPrefs *prefs;
463 MsgInfo *msginfo = NULL;
464 MsgInfoList *cur = NULL;
465 gint curnum = 0, total = 0;
466 gchar *srcpath = NULL;
467 gboolean full_fetch = FALSE;
468 time_t last_dest_mtime = (time_t)0;
469 time_t last_src_mtime = (time_t)0;
471 g_return_val_if_fail(dest != NULL, -1);
472 g_return_val_if_fail(msglist != NULL, -1);
474 msginfo = (MsgInfo *)msglist->data;
476 g_return_val_if_fail(msginfo != NULL, -1);
478 if (msginfo->folder == dest) {
479 g_warning("the src folder is identical to the dest.\n");
483 if (msginfo->folder->folder != dest->folder)
486 if (FOLDER_TYPE(msginfo->folder->folder) == F_MH) {
487 src = msginfo->folder;
490 if (dest->last_num < 0) {
491 mh_get_last_num(folder, dest);
492 if (dest->last_num < 0) return -1;
497 srcpath = folder_item_get_path(msginfo->folder);
499 dest_need_scan = mh_scan_required(dest->folder, dest);
500 last_dest_mtime = dest->mtime;
503 src_need_scan = mh_scan_required(src->folder, src);
504 last_src_mtime = src->mtime;
507 total = g_slist_length(msglist);
509 if (MSG_IS_MOVE(msginfo->flags))
510 statusbar_print_all(_("Moving messages..."));
512 statusbar_print_all(_("Copying messages..."));
514 for (cur = msglist; cur; cur = cur->next) {
515 msginfo = (MsgInfo *)cur->data;
517 goto err_reset_status;
520 srcfile = g_strconcat(srcpath,
522 itos(msginfo->msgnum), NULL);
524 srcfile = procmsg_get_message_file(msginfo);
527 goto err_reset_status;
529 destfile = mh_get_new_msg_filename(dest);
532 goto err_reset_status;
536 statusbar_progress_all(curnum, total, 100);
537 if (curnum % 100 == 0)
542 debug_print("Copying message %s%c%d to %s ...\n",
543 msginfo->folder->path, G_DIR_SEPARATOR,
544 msginfo->msgnum, dest->path);
547 if (MSG_IS_MOVE(msginfo->flags)) {
548 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
549 if (move_file(srcfile, destfile, TRUE) < 0) {
550 FILE_OP_ERROR(srcfile, "move");
551 if (copy_file(srcfile, destfile, TRUE) < 0) {
552 FILE_OP_ERROR(srcfile, "copy");
555 goto err_reset_status;
558 /* say unlinking's not necessary */
559 msginfo->flags.tmp_flags |= MSG_MOVE_DONE;
561 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
562 FILE_OP_ERROR(srcfile, "copy");
565 goto err_reset_status;
567 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
568 if (chmod(destfile, prefs->folder_chmod) < 0)
569 FILE_OP_ERROR(destfile, "chmod");
572 filemode = prefs->folder_chmod;
573 if (filemode & S_IRGRP) filemode |= S_IWGRP;
574 if (filemode & S_IROTH) filemode |= S_IWOTH;
577 g_relation_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
584 mh_write_sequences(dest, TRUE);
586 if (dest->mtime == last_dest_mtime && !dest_need_scan) {
590 if (src && src->mtime == last_src_mtime && !src_need_scan) {
595 statusbar_progress_all(0,0,0);
598 return dest->last_num;
601 mh_write_sequences(dest, TRUE);
603 statusbar_progress_all(0,0,0);
610 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
612 gboolean need_scan = FALSE;
613 time_t last_mtime = (time_t)0;
616 g_return_val_if_fail(item != NULL, -1);
618 file = mh_fetch_msg(folder, item, num);
619 g_return_val_if_fail(file != NULL, -1);
621 need_scan = mh_scan_required(folder, item);
622 last_mtime = item->mtime;
624 if (g_unlink(file) < 0) {
625 FILE_OP_ERROR(file, "unlink");
630 if (item->mtime == last_mtime && !need_scan) {
637 static gint mh_remove_msgs(Folder *folder, FolderItem *item,
638 MsgInfoList *msglist, GRelation *relation)
640 gboolean need_scan = FALSE;
642 time_t last_mtime = (time_t)0;
644 gint total = 0, curnum = 0;
646 g_return_val_if_fail(item != NULL, -1);
648 path = folder_item_get_path(item);
650 need_scan = mh_scan_required(folder, item);
651 last_mtime = item->mtime;
653 total = g_slist_length(msglist);
655 statusbar_print_all(_("Deleting messages..."));
658 for (cur = msglist; cur; cur = cur->next) {
659 MsgInfo *msginfo = (MsgInfo *)cur->data;
662 if (MSG_IS_MOVE(msginfo->flags) && MSG_IS_MOVE_DONE(msginfo->flags)) {
663 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
667 statusbar_progress_all(curnum, total, 100);
668 if (curnum % 100 == 0)
673 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
677 if (g_unlink(file) < 0) {
686 statusbar_progress_all(0,0,0);
689 if (item->mtime == last_mtime && !need_scan) {
697 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
702 g_return_val_if_fail(item != NULL, -1);
704 path = folder_item_get_path(item);
705 g_return_val_if_fail(path != NULL, -1);
706 val = remove_all_numbered_files(path);
709 mh_write_sequences(item, TRUE);
714 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
719 if (g_stat(itos(msginfo->msgnum), &s) < 0 ||
720 msginfo->size != s.st_size || (
721 (msginfo->mtime - s.st_mtime != 0) &&
722 (msginfo->mtime - s.st_mtime != 3600) &&
723 (msginfo->mtime - s.st_mtime != -3600)))
729 static gint mh_scan_tree(Folder *folder)
734 g_return_val_if_fail(folder != NULL, -1);
737 item = folder_item_new(folder, folder->name, NULL);
738 item->folder = folder;
739 folder->node = item->node = g_node_new(item);
741 item = FOLDER_ITEM(folder->node->data);
743 rootpath = folder_item_get_path(item);
744 if (change_dir(rootpath) < 0) {
750 mh_create_tree(folder);
751 mh_remove_missing_folder_items(folder);
752 mh_scan_tree_recursive(item);
757 #define MAKE_DIR_IF_NOT_EXIST(dir) \
759 if (!is_dir_exist(dir)) { \
760 if (is_file_exist(dir)) { \
761 g_warning("File `%s' already exists.\n" \
762 "Can't create folder.", dir); \
765 if (make_dir_hier(dir) < 0) \
770 static gint mh_create_tree(Folder *folder)
774 g_return_val_if_fail(folder != NULL, -1);
776 CHDIR_RETURN_VAL_IF_FAIL(get_mail_base_dir(), -1);
777 rootpath = LOCAL_FOLDER(folder)->rootpath;
778 MAKE_DIR_IF_NOT_EXIST(rootpath);
779 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
780 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
781 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
782 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
783 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
784 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
789 #undef MAKE_DIR_IF_NOT_EXIST
791 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
793 gchar *folder_path, *path;
795 g_return_val_if_fail(folder != NULL, NULL);
796 g_return_val_if_fail(item != NULL, NULL);
798 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
799 g_return_val_if_fail(folder_path != NULL, NULL);
801 /* FIXME: [W32] The code below does not correctly merge
802 relative filenames; there should be a function to handle
804 if ( !is_relative_filename (folder_path) ) {
806 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
809 path = g_strdup(folder_path);
812 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
813 folder_path, G_DIR_SEPARATOR_S,
816 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
820 real_path = mh_filename_from_utf8(path);
821 if (!is_dir_exist(real_path) && is_dir_exist(path)) {
822 /* mmh, older version did put utf8 filenames instead of
823 * the correct encoding */
824 rename(path, real_path);
825 folder_item_scan(item);
832 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
835 gchar *path, *real_name;
837 FolderItem *new_item;
838 gchar *mh_sequences_filename;
839 FILE *mh_sequences_file;
841 g_return_val_if_fail(folder != NULL, NULL);
842 g_return_val_if_fail(parent != NULL, NULL);
843 g_return_val_if_fail(name != NULL, NULL);
845 path = folder_item_get_path(parent);
846 if (!is_dir_exist(path))
847 if (make_dir_hier(path) != 0)
850 real_name = mh_filename_from_utf8(name);
851 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
855 if (make_dir(fullpath) < 0) {
863 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
866 path = g_strdup(name);
867 new_item = folder_item_new(folder, name, path);
868 folder_item_append(parent, new_item);
872 path = folder_item_get_path(new_item);
873 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
874 ".mh_sequences", NULL);
875 if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
876 fclose(mh_sequences_file);
878 g_free(mh_sequences_filename);
884 static gint mh_rename_folder(Folder *folder, FolderItem *item,
890 gchar *newpath, *utf8newpath;
893 g_return_val_if_fail(folder != NULL, -1);
894 g_return_val_if_fail(item != NULL, -1);
895 g_return_val_if_fail(item->path != NULL, -1);
896 g_return_val_if_fail(name != NULL, -1);
898 oldpath = folder_item_get_path(item);
899 if (!is_dir_exist(oldpath))
900 make_dir_hier(oldpath);
902 dirname = g_path_get_dirname(oldpath);
903 real_name = mh_filename_from_utf8(name);
904 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
907 if (g_rename(oldpath, newpath) < 0) {
908 FILE_OP_ERROR(oldpath, "rename");
917 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
918 dirname = g_path_get_dirname(item->path);
919 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
923 utf8newpath = g_strdup(name);
926 item->name = g_strdup(name);
928 paths[0] = g_strdup(item->path);
929 paths[1] = utf8newpath;
930 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
931 mh_rename_folder_func, paths);
938 static gint mh_remove_folder(Folder *folder, FolderItem *item)
942 g_return_val_if_fail(folder != NULL, -1);
943 g_return_val_if_fail(item != NULL, -1);
944 g_return_val_if_fail(item->path != NULL, -1);
946 path = folder_item_get_path(item);
947 if (remove_dir_recursive(path) < 0) {
948 g_warning("can't remove directory `%s'\n", path);
954 folder_item_remove(item);
958 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
963 g_return_val_if_fail(item != NULL, NULL);
964 g_return_val_if_fail(file != NULL, NULL);
966 flags.perm_flags = MSG_NEW|MSG_UNREAD;
969 if (folder_has_parent_of_type(item, F_QUEUE)) {
970 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
971 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
972 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
975 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
976 if (!msginfo) return NULL;
978 msginfo->msgnum = atoi(file);
979 msginfo->folder = item;
984 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
989 g_return_val_if_fail(node->data != NULL, FALSE);
991 if (G_NODE_IS_ROOT(node))
994 item = FOLDER_ITEM(node->data);
996 path = folder_item_get_path(item);
997 if (!is_dir_exist(path)) {
998 debug_print("folder '%s' not found. removing...\n", path);
999 folder_item_remove(item);
1006 static void mh_remove_missing_folder_items(Folder *folder)
1008 g_return_if_fail(folder != NULL);
1010 debug_print("searching missing folders...\n");
1012 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1013 mh_remove_missing_folder_items_func, folder);
1016 static void mh_scan_tree_recursive(FolderItem *item)
1025 const gchar *dir_name;
1027 gchar *real_path, *entry, *utf8entry, *utf8name;
1030 g_return_if_fail(item != NULL);
1031 g_return_if_fail(item->folder != NULL);
1033 folder = item->folder;
1035 real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
1037 dir = g_dir_open(real_path, 0, NULL);
1039 g_warning("failed to open directory: %s\n", real_path);
1044 dp = opendir(real_path);
1046 FILE_OP_ERROR(real_path, "opendir");
1052 debug_print("scanning %s ...\n",
1053 item->path ? item->path
1054 : LOCAL_FOLDER(item->folder)->rootpath);
1055 if (folder->ui_func)
1056 folder->ui_func(folder, item, folder->ui_func_data);
1059 while ((dir_name = g_dir_read_name(dir)) != NULL) {
1061 while ((d = readdir(dp)) != NULL) {
1062 dir_name = d->d_name;
1064 if (dir_name[0] == '.') continue;
1066 utf8name = mh_filename_to_utf8(dir_name);
1068 utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1071 utf8entry = g_strdup(utf8name);
1072 entry = mh_filename_from_utf8(utf8entry);
1075 #if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
1076 d->d_type == DT_DIR ||
1077 (d->d_type == DT_UNKNOWN &&
1079 g_stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
1080 #if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
1084 FolderItem *new_item = NULL;
1088 for (node = node->children; node != NULL; node = node->next) {
1089 FolderItem *cur_item = FOLDER_ITEM(node->data);
1090 gchar *curpath = mh_filename_from_utf8(cur_item->path);
1091 if (!strcmp2(curpath, entry)) {
1092 new_item = cur_item;
1099 debug_print("new folder '%s' found.\n", entry);
1100 new_item = folder_item_new(folder, utf8name, utf8entry);
1101 folder_item_append(item, new_item);
1105 if (!folder->inbox &&
1106 !strcmp(dir_name, INBOX_DIR)) {
1107 new_item->stype = F_INBOX;
1108 folder->inbox = new_item;
1109 } else if (!folder->outbox &&
1110 !strcmp(dir_name, OUTBOX_DIR)) {
1111 new_item->stype = F_OUTBOX;
1112 folder->outbox = new_item;
1113 } else if (!folder->draft &&
1114 !strcmp(dir_name, DRAFT_DIR)) {
1115 new_item->stype = F_DRAFT;
1116 folder->draft = new_item;
1117 } else if (!folder->queue &&
1118 !strcmp(dir_name, QUEUE_DIR)) {
1119 new_item->stype = F_QUEUE;
1120 folder->queue = new_item;
1121 } else if (!folder->trash &&
1122 !strcmp(dir_name, TRASH_DIR)) {
1123 new_item->stype = F_TRASH;
1124 folder->trash = new_item;
1128 mh_scan_tree_recursive(new_item);
1129 } else if (to_number(dir_name) > 0) n_msg++;
1145 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1147 FolderItem *item = node->data;
1148 gchar **paths = data;
1149 const gchar *oldpath = paths[0];
1150 const gchar *newpath = paths[1];
1152 gchar *new_itempath;
1155 oldpathlen = strlen(oldpath);
1156 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1157 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1161 base = item->path + oldpathlen;
1162 while (*base == G_DIR_SEPARATOR) base++;
1164 new_itempath = g_strdup(newpath);
1166 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1169 item->path = new_itempath;
1174 static gchar *mh_filename_from_utf8(const gchar *path)
1176 gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1179 g_warning("mh_filename_from_utf8: failed to convert character set\n");
1180 real_path = g_strdup(path);
1186 static gchar *mh_filename_to_utf8(const gchar *path)
1188 gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1190 g_warning("mh_filename_to_utf8: failed to convert character set\n");
1191 utf8path = g_strdup(path);
1197 static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1199 MsgInfo *msginfo_a = (MsgInfo *) a;
1200 MsgInfo *msginfo_b = (MsgInfo *) b;
1202 return (msginfo_a->msgnum - msginfo_b->msgnum);
1205 static gchar *get_unseen_seq_name(void)
1207 static gchar *seq_name = NULL;
1209 gchar buf[BUFFSIZE];
1211 gchar *profile_path = g_strconcat(
1212 get_home_dir(), G_DIR_SEPARATOR_S,
1213 ".mh_profile", NULL);
1214 FILE *fp = g_fopen(profile_path, "r");
1216 while (fgets(buf, sizeof(buf), fp) != NULL) {
1217 if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
1218 gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
1219 while (*seq_tmp == ' ')
1221 seq_name = g_strdup(seq_tmp);
1222 seq_name = strretchomp(seq_name);
1229 seq_name = g_strdup("unseen");
1230 tmp = g_strdup_printf("%s:", seq_name);
1238 static gint mh_get_flags(Folder *folder, FolderItem *item,
1239 MsgInfoList *msginfo_list, GRelation *msgflags)
1241 gchar *mh_sequences_filename;
1242 FILE *mh_sequences_file;
1243 gchar buf[BUFFSIZE];
1244 gchar *unseen_list = NULL;
1246 MsgInfoList *mcur = NULL;
1248 GTimer *timer = g_timer_new();
1249 g_timer_start(timer);
1254 /* don't update from .mh_sequences if the item's opened: mails may have
1255 * been marked read/unread and it's not yet written in the file. */
1259 path = folder_item_get_path(item);
1261 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
1262 ".mh_sequences", NULL);
1264 if ((mh_sequences_file = g_fopen(mh_sequences_filename, "r+b")) != NULL) {
1265 while (fgets(buf, sizeof(buf), mh_sequences_file) != NULL) {
1266 if (!strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name()))) {
1267 unseen_list = g_strdup(buf+strlen(get_unseen_seq_name()));
1271 fclose(mh_sequences_file);
1274 g_free(mh_sequences_filename);
1278 gchar *token = NULL, *next = NULL, *boundary = NULL;
1280 GHashTable *unseen_table = g_hash_table_new(g_direct_hash, g_direct_equal);
1282 cur = unseen_list = strretchomp(unseen_list);
1283 debug_print("found unseen list in .mh_sequences: %s\n", unseen_list);
1285 while (*cur && *cur == ' ')
1288 if ((next = strchr(cur, ' ')) != NULL) {
1297 if ((boundary = strchr(token, '-')) != NULL) {
1303 for (i = atoi(start); i <= atoi(end); i++) {
1304 g_hash_table_insert(unseen_table, GINT_TO_POINTER(i), GINT_TO_POINTER(1));
1306 } else if ((num = atoi(token)) > 0) {
1307 g_hash_table_insert(unseen_table, GINT_TO_POINTER(num), GINT_TO_POINTER(1));
1312 for (mcur = msginfo_list; mcur; mcur = mcur->next) {
1313 MsgInfo *msginfo = (MsgInfo *)mcur->data;
1314 MsgPermFlags flags = msginfo->flags.perm_flags;
1315 if (g_hash_table_lookup(unseen_table, GINT_TO_POINTER(msginfo->msgnum))) {
1316 flags |= MSG_UNREAD;
1317 } else if (!(flags & MSG_NEW)) { /* don't mark new msgs as read */
1318 flags &= ~(MSG_UNREAD);
1320 if (flags != msginfo->flags.perm_flags)
1321 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
1323 g_hash_table_destroy(unseen_table);
1324 g_free(unseen_list);
1327 g_timer_stop(timer);
1328 printf("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
1329 g_timer_destroy(timer);
1335 static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
1337 gchar *mh_sequences_old, *mh_sequences_new;
1338 FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
1339 gchar buf[BUFFSIZE];
1346 path = folder_item_get_path(item);
1348 mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
1349 ".mh_sequences", NULL);
1350 mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
1351 ".mh_sequences.new", NULL);
1352 if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
1353 GSList *msglist = folder_item_get_msg_list(item);
1355 MsgInfo *info = NULL;
1356 gint start = -1, end = -1;
1357 gchar *sequence = g_strdup("");
1359 msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
1362 /* write the unseen sequence if we don't have to scrap it */
1363 if (!remove_unseen) do {
1364 info = (MsgInfo *)(cur ? cur->data:NULL);
1365 if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
1367 start = end = info->msgnum;
1371 if (start > 0 && end > 0) {
1375 snprintf(tmp, 31, " %d-%d", start, end);
1377 snprintf(tmp, 31, " %d", start);
1379 tmp_len = strlen(tmp);
1380 sequence = g_realloc(sequence, seq_len+tmp_len+1);
1381 strcpy(sequence+seq_len, tmp);
1387 cur = cur ? cur->next:NULL;
1388 } while (cur || (start > 0 && end > 0));
1389 if (sequence && *sequence) {
1390 fprintf(mh_sequences_new_fp, "%s%s\n",
1391 get_unseen_seq_name(), sequence);
1392 debug_print("wrote unseen sequence: '%s%s'\n",
1393 get_unseen_seq_name(), sequence);
1395 /* rewrite the rest of the file */
1396 if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
1397 while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
1398 if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
1399 fprintf(mh_sequences_new_fp, "%s", buf);
1401 fclose(mh_sequences_old_fp);
1404 fflush(mh_sequences_new_fp);
1406 fsync(fileno(mh_sequences_new_fp));
1408 fclose(mh_sequences_new_fp);
1410 g_rename(mh_sequences_new, mh_sequences_old);
1412 procmsg_msg_list_free(msglist);
1414 g_free(mh_sequences_old);
1415 g_free(mh_sequences_new);
1421 static int mh_item_close(Folder *folder, FolderItem *item)
1423 time_t last_mtime = (time_t)0;
1424 gboolean need_scan = mh_scan_required(item->folder, item);
1425 last_mtime = item->mtime;
1427 mh_write_sequences(item, FALSE);
1429 if (item->mtime == last_mtime && !need_scan) {
1436 void mh_set_mtime(FolderItem *item)
1439 gchar *path = folder_item_get_path(item);
1441 g_return_if_fail(path != NULL);
1443 if (stat(path, &s) < 0) {
1444 FILE_OP_ERROR(path, "stat");
1449 item->mtime = s.st_mtime;
1450 debug_print("MH: forced mtime of %s to %ld\n", item->name, item->mtime);