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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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;
460 FolderItemPrefs *prefs;
461 MsgInfo *msginfo = NULL;
462 MsgInfoList *cur = NULL;
463 gint curnum = 0, total = 0;
464 gchar *srcpath = NULL;
465 gboolean full_fetch = FALSE;
466 time_t last_mtime = (time_t)0;
468 g_return_val_if_fail(dest != NULL, -1);
469 g_return_val_if_fail(msglist != NULL, -1);
471 msginfo = (MsgInfo *)msglist->data;
473 g_return_val_if_fail(msginfo != NULL, -1);
475 if (msginfo->folder == dest) {
476 g_warning("the src folder is identical to the dest.\n");
480 if (msginfo->folder->folder != dest->folder)
483 if (dest->last_num < 0) {
484 mh_get_last_num(folder, dest);
485 if (dest->last_num < 0) return -1;
490 srcpath = folder_item_get_path(msginfo->folder);
492 dest_need_scan = mh_scan_required(dest->folder, dest);
493 last_mtime = dest->mtime;
495 total = g_slist_length(msglist);
497 if (MSG_IS_MOVE(msginfo->flags))
498 statusbar_print_all(_("Moving messages..."));
500 statusbar_print_all(_("Copying messages..."));
502 for (cur = msglist; cur; cur = cur->next) {
503 msginfo = (MsgInfo *)cur->data;
505 goto err_reset_status;
508 srcfile = g_strconcat(srcpath,
510 itos(msginfo->msgnum), NULL);
512 srcfile = procmsg_get_message_file(msginfo);
515 goto err_reset_status;
517 destfile = mh_get_new_msg_filename(dest);
520 goto err_reset_status;
524 statusbar_progress_all(curnum, total, 100);
525 if (curnum % 100 == 0)
530 debug_print("Copying message %s%c%d to %s ...\n",
531 msginfo->folder->path, G_DIR_SEPARATOR,
532 msginfo->msgnum, dest->path);
535 if (MSG_IS_MOVE(msginfo->flags)) {
536 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
537 if (move_file(srcfile, destfile, TRUE) < 0) {
538 FILE_OP_ERROR(srcfile, "move");
539 if (copy_file(srcfile, destfile, TRUE) < 0) {
540 FILE_OP_ERROR(srcfile, "copy");
543 goto err_reset_status;
546 /* say unlinking's not necessary */
547 msginfo->flags.tmp_flags |= MSG_MOVE_DONE;
549 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
550 FILE_OP_ERROR(srcfile, "copy");
553 goto err_reset_status;
555 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
556 if (chmod(destfile, prefs->folder_chmod) < 0)
557 FILE_OP_ERROR(destfile, "chmod");
560 filemode = prefs->folder_chmod;
561 if (filemode & S_IRGRP) filemode |= S_IWGRP;
562 if (filemode & S_IROTH) filemode |= S_IWOTH;
565 g_relation_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
572 mh_write_sequences(dest, TRUE);
574 if (dest->mtime == last_mtime && !dest_need_scan) {
578 statusbar_progress_all(0,0,0);
581 return dest->last_num;
584 mh_write_sequences(dest, TRUE);
586 statusbar_progress_all(0,0,0);
593 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
595 gboolean need_scan = FALSE;
596 time_t last_mtime = (time_t)0;
599 g_return_val_if_fail(item != NULL, -1);
601 file = mh_fetch_msg(folder, item, num);
602 g_return_val_if_fail(file != NULL, -1);
604 need_scan = mh_scan_required(folder, item);
605 last_mtime = item->mtime;
607 if (g_unlink(file) < 0) {
608 FILE_OP_ERROR(file, "unlink");
613 if (item->mtime == last_mtime && !need_scan) {
620 static gint mh_remove_msgs(Folder *folder, FolderItem *item,
621 MsgInfoList *msglist, GRelation *relation)
623 gboolean need_scan = FALSE;
625 time_t last_mtime = (time_t)0;
627 gint total = 0, curnum = 0;
629 g_return_val_if_fail(item != NULL, -1);
631 path = folder_item_get_path(item);
633 need_scan = mh_scan_required(folder, item);
634 last_mtime = item->mtime;
636 total = g_slist_length(msglist);
638 statusbar_print_all(_("Deleting messages..."));
641 for (cur = msglist; cur; cur = cur->next) {
642 MsgInfo *msginfo = (MsgInfo *)cur->data;
645 if (MSG_IS_MOVE(msginfo->flags) && MSG_IS_MOVE_DONE(msginfo->flags)) {
646 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
650 statusbar_progress_all(curnum, total, 100);
651 if (curnum % 100 == 0)
656 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
660 if (g_unlink(file) < 0) {
669 statusbar_progress_all(0,0,0);
672 if (item->mtime == last_mtime && !need_scan) {
680 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
685 g_return_val_if_fail(item != NULL, -1);
687 path = folder_item_get_path(item);
688 g_return_val_if_fail(path != NULL, -1);
689 val = remove_all_numbered_files(path);
692 mh_write_sequences(item, TRUE);
697 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
702 if (g_stat(itos(msginfo->msgnum), &s) < 0 ||
703 msginfo->size != s.st_size || (
704 (msginfo->mtime - s.st_mtime != 0) &&
705 (msginfo->mtime - s.st_mtime != 3600) &&
706 (msginfo->mtime - s.st_mtime != -3600)))
712 static gint mh_scan_tree(Folder *folder)
717 g_return_val_if_fail(folder != NULL, -1);
720 item = folder_item_new(folder, folder->name, NULL);
721 item->folder = folder;
722 folder->node = item->node = g_node_new(item);
724 item = FOLDER_ITEM(folder->node->data);
726 rootpath = folder_item_get_path(item);
727 if (change_dir(rootpath) < 0) {
733 mh_create_tree(folder);
734 mh_remove_missing_folder_items(folder);
735 mh_scan_tree_recursive(item);
740 #define MAKE_DIR_IF_NOT_EXIST(dir) \
742 if (!is_dir_exist(dir)) { \
743 if (is_file_exist(dir)) { \
744 g_warning("File `%s' already exists.\n" \
745 "Can't create folder.", dir); \
748 if (make_dir_hier(dir) < 0) \
753 static gint mh_create_tree(Folder *folder)
757 g_return_val_if_fail(folder != NULL, -1);
759 CHDIR_RETURN_VAL_IF_FAIL(get_mail_base_dir(), -1);
760 rootpath = LOCAL_FOLDER(folder)->rootpath;
761 MAKE_DIR_IF_NOT_EXIST(rootpath);
762 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
763 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
764 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
765 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
766 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
767 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
772 #undef MAKE_DIR_IF_NOT_EXIST
774 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
776 gchar *folder_path, *path;
778 g_return_val_if_fail(folder != NULL, NULL);
779 g_return_val_if_fail(item != NULL, NULL);
781 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
782 g_return_val_if_fail(folder_path != NULL, NULL);
784 /* FIXME: [W32] The code below does not correctly merge
785 relative filenames; there should be a function to handle
787 if ( !is_relative_filename (folder_path) ) {
789 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
792 path = g_strdup(folder_path);
795 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
796 folder_path, G_DIR_SEPARATOR_S,
799 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
803 real_path = mh_filename_from_utf8(path);
804 if (!is_dir_exist(real_path) && is_dir_exist(path)) {
805 /* mmh, older version did put utf8 filenames instead of
806 * the correct encoding */
807 rename(path, real_path);
808 folder_item_scan(item);
815 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
818 gchar *path, *real_name;
820 FolderItem *new_item;
821 gchar *mh_sequences_filename;
822 FILE *mh_sequences_file;
824 g_return_val_if_fail(folder != NULL, NULL);
825 g_return_val_if_fail(parent != NULL, NULL);
826 g_return_val_if_fail(name != NULL, NULL);
828 path = folder_item_get_path(parent);
829 if (!is_dir_exist(path))
830 if (make_dir_hier(path) != 0)
833 real_name = mh_filename_from_utf8(name);
834 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
838 if (make_dir(fullpath) < 0) {
846 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
849 path = g_strdup(name);
850 new_item = folder_item_new(folder, name, path);
851 folder_item_append(parent, new_item);
855 path = folder_item_get_path(new_item);
856 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
857 ".mh_sequences", NULL);
858 if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
859 fclose(mh_sequences_file);
861 g_free(mh_sequences_filename);
867 static gint mh_rename_folder(Folder *folder, FolderItem *item,
873 gchar *newpath, *utf8newpath;
876 g_return_val_if_fail(folder != NULL, -1);
877 g_return_val_if_fail(item != NULL, -1);
878 g_return_val_if_fail(item->path != NULL, -1);
879 g_return_val_if_fail(name != NULL, -1);
881 oldpath = folder_item_get_path(item);
882 if (!is_dir_exist(oldpath))
883 make_dir_hier(oldpath);
885 dirname = g_path_get_dirname(oldpath);
886 real_name = mh_filename_from_utf8(name);
887 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
890 if (g_rename(oldpath, newpath) < 0) {
891 FILE_OP_ERROR(oldpath, "rename");
900 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
901 dirname = g_path_get_dirname(item->path);
902 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
906 utf8newpath = g_strdup(name);
909 item->name = g_strdup(name);
911 paths[0] = g_strdup(item->path);
912 paths[1] = utf8newpath;
913 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
914 mh_rename_folder_func, paths);
921 static gint mh_remove_folder(Folder *folder, FolderItem *item)
925 g_return_val_if_fail(folder != NULL, -1);
926 g_return_val_if_fail(item != NULL, -1);
927 g_return_val_if_fail(item->path != NULL, -1);
929 path = folder_item_get_path(item);
930 if (remove_dir_recursive(path) < 0) {
931 g_warning("can't remove directory `%s'\n", path);
937 folder_item_remove(item);
941 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
946 g_return_val_if_fail(item != NULL, NULL);
947 g_return_val_if_fail(file != NULL, NULL);
949 flags.perm_flags = MSG_NEW|MSG_UNREAD;
952 if (folder_has_parent_of_type(item, F_QUEUE)) {
953 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
954 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
955 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
958 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
959 if (!msginfo) return NULL;
961 msginfo->msgnum = atoi(file);
962 msginfo->folder = item;
967 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
972 g_return_val_if_fail(node->data != NULL, FALSE);
974 if (G_NODE_IS_ROOT(node))
977 item = FOLDER_ITEM(node->data);
979 path = folder_item_get_path(item);
980 if (!is_dir_exist(path)) {
981 debug_print("folder '%s' not found. removing...\n", path);
982 folder_item_remove(item);
989 static void mh_remove_missing_folder_items(Folder *folder)
991 g_return_if_fail(folder != NULL);
993 debug_print("searching missing folders...\n");
995 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
996 mh_remove_missing_folder_items_func, folder);
999 static void mh_scan_tree_recursive(FolderItem *item)
1008 const gchar *dir_name;
1010 gchar *real_path, *entry, *utf8entry, *utf8name;
1013 g_return_if_fail(item != NULL);
1014 g_return_if_fail(item->folder != NULL);
1016 folder = item->folder;
1018 real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
1020 dir = g_dir_open(real_path, 0, NULL);
1022 g_warning("failed to open directory: %s\n", real_path);
1027 dp = opendir(real_path);
1029 FILE_OP_ERROR(real_path, "opendir");
1035 debug_print("scanning %s ...\n",
1036 item->path ? item->path
1037 : LOCAL_FOLDER(item->folder)->rootpath);
1038 if (folder->ui_func)
1039 folder->ui_func(folder, item, folder->ui_func_data);
1042 while ((dir_name = g_dir_read_name(dir)) != NULL) {
1044 while ((d = readdir(dp)) != NULL) {
1045 dir_name = d->d_name;
1047 if (dir_name[0] == '.') continue;
1049 utf8name = mh_filename_to_utf8(dir_name);
1051 utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1054 utf8entry = g_strdup(utf8name);
1055 entry = mh_filename_from_utf8(utf8entry);
1058 #if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
1059 d->d_type == DT_DIR ||
1060 (d->d_type == DT_UNKNOWN &&
1062 g_stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
1063 #if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
1067 FolderItem *new_item = NULL;
1071 for (node = node->children; node != NULL; node = node->next) {
1072 FolderItem *cur_item = FOLDER_ITEM(node->data);
1073 gchar *curpath = mh_filename_from_utf8(cur_item->path);
1074 if (!strcmp2(curpath, entry)) {
1075 new_item = cur_item;
1082 debug_print("new folder '%s' found.\n", entry);
1083 new_item = folder_item_new(folder, utf8name, utf8entry);
1084 folder_item_append(item, new_item);
1088 if (!folder->inbox &&
1089 !strcmp(dir_name, INBOX_DIR)) {
1090 new_item->stype = F_INBOX;
1091 folder->inbox = new_item;
1092 } else if (!folder->outbox &&
1093 !strcmp(dir_name, OUTBOX_DIR)) {
1094 new_item->stype = F_OUTBOX;
1095 folder->outbox = new_item;
1096 } else if (!folder->draft &&
1097 !strcmp(dir_name, DRAFT_DIR)) {
1098 new_item->stype = F_DRAFT;
1099 folder->draft = new_item;
1100 } else if (!folder->queue &&
1101 !strcmp(dir_name, QUEUE_DIR)) {
1102 new_item->stype = F_QUEUE;
1103 folder->queue = new_item;
1104 } else if (!folder->trash &&
1105 !strcmp(dir_name, TRASH_DIR)) {
1106 new_item->stype = F_TRASH;
1107 folder->trash = new_item;
1111 mh_scan_tree_recursive(new_item);
1112 } else if (to_number(dir_name) > 0) n_msg++;
1128 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1130 FolderItem *item = node->data;
1131 gchar **paths = data;
1132 const gchar *oldpath = paths[0];
1133 const gchar *newpath = paths[1];
1135 gchar *new_itempath;
1138 oldpathlen = strlen(oldpath);
1139 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1140 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1144 base = item->path + oldpathlen;
1145 while (*base == G_DIR_SEPARATOR) base++;
1147 new_itempath = g_strdup(newpath);
1149 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1152 item->path = new_itempath;
1157 static gchar *mh_filename_from_utf8(const gchar *path)
1159 gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1162 g_warning("mh_filename_from_utf8: faild to convert character set\n");
1163 real_path = g_strdup(path);
1169 static gchar *mh_filename_to_utf8(const gchar *path)
1171 gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1173 g_warning("mh_filename_to_utf8: faild to convert character set\n");
1174 utf8path = g_strdup(path);
1180 static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1182 MsgInfo *msginfo_a = (MsgInfo *) a;
1183 MsgInfo *msginfo_b = (MsgInfo *) b;
1185 return (msginfo_a->msgnum - msginfo_b->msgnum);
1188 static gchar *get_unseen_seq_name(void)
1190 static gchar *seq_name = NULL;
1192 gchar buf[BUFFSIZE];
1194 gchar *profile_path = g_strconcat(
1195 get_home_dir(), G_DIR_SEPARATOR_S,
1196 ".mh_profile", NULL);
1197 FILE *fp = g_fopen(profile_path, "r");
1199 while (fgets(buf, sizeof(buf), fp) != NULL) {
1200 if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
1201 gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
1202 while (*seq_tmp == ' ')
1204 seq_name = g_strdup(seq_tmp);
1205 seq_name = strretchomp(seq_name);
1212 seq_name = g_strdup("unseen");
1213 tmp = g_strdup_printf("%s:", seq_name);
1221 static gint mh_get_flags(Folder *folder, FolderItem *item,
1222 MsgInfoList *msginfo_list, GRelation *msgflags)
1224 gchar *mh_sequences_filename;
1225 FILE *mh_sequences_file;
1226 gchar buf[BUFFSIZE];
1227 gchar *unseen_list = NULL;
1229 MsgInfoList *mcur = NULL;
1231 GTimer *timer = g_timer_new();
1232 g_timer_start(timer);
1237 /* don't update from .mh_sequences if the item's opened: mails may have
1238 * been marked read/unread and it's not yet written in the file. */
1242 path = folder_item_get_path(item);
1244 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
1245 ".mh_sequences", NULL);
1247 if ((mh_sequences_file = g_fopen(mh_sequences_filename, "r+b")) != NULL) {
1248 while (fgets(buf, sizeof(buf), mh_sequences_file) != NULL) {
1249 if (!strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name()))) {
1250 unseen_list = g_strdup(buf+strlen(get_unseen_seq_name()));
1254 fclose(mh_sequences_file);
1257 g_free(mh_sequences_filename);
1261 gchar *token = NULL, *next = NULL, *boundary = NULL;
1263 GHashTable *unseen_table = g_hash_table_new(g_direct_hash, g_direct_equal);
1265 cur = unseen_list = strretchomp(unseen_list);
1266 debug_print("found unseen list in .mh_sequences: %s\n", unseen_list);
1268 while (*cur && *cur == ' ')
1271 if ((next = strchr(cur, ' ')) != NULL) {
1280 if ((boundary = strchr(token, '-')) != NULL) {
1286 for (i = atoi(start); i <= atoi(end); i++) {
1287 g_hash_table_insert(unseen_table, GINT_TO_POINTER(i), GINT_TO_POINTER(1));
1289 } else if ((num = atoi(token)) > 0) {
1290 g_hash_table_insert(unseen_table, GINT_TO_POINTER(num), GINT_TO_POINTER(1));
1295 for (mcur = msginfo_list; mcur; mcur = mcur->next) {
1296 MsgInfo *msginfo = (MsgInfo *)mcur->data;
1297 MsgPermFlags flags = msginfo->flags.perm_flags;
1298 if (g_hash_table_lookup(unseen_table, GINT_TO_POINTER(msginfo->msgnum))) {
1299 flags |= MSG_UNREAD;
1300 } else if (!(flags & MSG_NEW)) { /* don't mark new msgs as read */
1301 flags &= ~(MSG_UNREAD);
1303 if (flags != msginfo->flags.perm_flags)
1304 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
1306 g_hash_table_destroy(unseen_table);
1307 g_free(unseen_list);
1310 g_timer_stop(timer);
1311 printf("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
1312 g_timer_destroy(timer);
1318 static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
1320 gchar *mh_sequences_old, *mh_sequences_new;
1321 FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
1322 gchar buf[BUFFSIZE];
1329 path = folder_item_get_path(item);
1331 mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
1332 ".mh_sequences", NULL);
1333 mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
1334 ".mh_sequences.new", NULL);
1335 if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
1336 GSList *msglist = folder_item_get_msg_list(item);
1338 MsgInfo *info = NULL;
1339 gint start = -1, end = -1;
1340 gchar *sequence = g_strdup("");
1342 msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
1345 /* write the unseen sequence if we don't have to scrap it */
1346 if (!remove_unseen) do {
1347 info = (MsgInfo *)(cur ? cur->data:NULL);
1348 if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
1350 start = end = info->msgnum;
1354 if (start > 0 && end > 0) {
1358 snprintf(tmp, 31, " %d-%d", start, end);
1360 snprintf(tmp, 31, " %d", start);
1362 tmp_len = strlen(tmp);
1363 sequence = g_realloc(sequence, seq_len+tmp_len+1);
1364 strcpy(sequence+seq_len, tmp);
1370 cur = cur ? cur->next:NULL;
1371 } while (cur || (start > 0 && end > 0));
1372 if (sequence && *sequence) {
1373 fprintf(mh_sequences_new_fp, "%s%s\n",
1374 get_unseen_seq_name(), sequence);
1375 debug_print("wrote unseen sequence: '%s%s'\n",
1376 get_unseen_seq_name(), sequence);
1378 /* rewrite the rest of the file */
1379 if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
1380 while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
1381 if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
1382 fprintf(mh_sequences_new_fp, "%s", buf);
1384 fclose(mh_sequences_old_fp);
1387 fflush(mh_sequences_new_fp);
1389 fsync(fileno(mh_sequences_new_fp));
1391 fclose(mh_sequences_new_fp);
1393 g_rename(mh_sequences_new, mh_sequences_old);
1395 procmsg_msg_list_free(msglist);
1397 g_free(mh_sequences_old);
1398 g_free(mh_sequences_new);
1404 static int mh_item_close(Folder *folder, FolderItem *item)
1406 time_t last_mtime = (time_t)0;
1407 gboolean need_scan = mh_scan_required(item->folder, item);
1408 last_mtime = item->mtime;
1410 mh_write_sequences(item, FALSE);
1412 if (item->mtime == last_mtime && !need_scan) {
1419 void mh_set_mtime(FolderItem *item)
1422 gchar *path = folder_item_get_path(item);
1424 g_return_if_fail(path != NULL);
1426 if (stat(path, &s) < 0) {
1427 FILE_OP_ERROR(path, "stat");
1432 item->mtime = s.st_mtime;
1433 debug_print("MH: forced mtime of %s to %ld\n", item->name, item->mtime);