2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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>
37 # include <sys/time.h>
43 #include "procheader.h"
46 #include "statusbar.h"
49 /* Define possible missing constants for Windows. */
62 static void mh_folder_init (Folder *folder,
66 static Folder *mh_folder_new (const gchar *name,
68 static void mh_folder_destroy (Folder *folder);
69 static gchar *mh_fetch_msg (Folder *folder,
72 static MsgInfo *mh_get_msginfo (Folder *folder,
75 static gint mh_add_msg (Folder *folder,
79 static gint mh_add_msgs (Folder *folder,
83 static gint mh_copy_msg (Folder *folder,
86 static gint mh_copy_msgs (Folder *folder,
90 static gint mh_remove_msg (Folder *folder,
93 static gint mh_remove_msgs (Folder *folder,
97 static gint mh_remove_all_msg (Folder *folder,
99 static gboolean mh_is_msg_changed (Folder *folder,
103 static gint mh_get_num_list (Folder *folder,
106 gboolean *old_uids_valid);
107 static gint mh_scan_tree (Folder *folder);
109 static gint mh_create_tree (Folder *folder);
110 static FolderItem *mh_create_folder (Folder *folder,
113 static gint mh_rename_folder (Folder *folder,
116 static gint mh_remove_folder (Folder *folder,
119 static gchar *mh_get_new_msg_filename (FolderItem *dest);
121 static MsgInfo *mh_parse_msg (const gchar *file,
123 static void mh_remove_missing_folder_items (Folder *folder);
124 static gchar *mh_filename_from_utf8 (const gchar *path);
125 static gchar *mh_filename_to_utf8 (const gchar *path);
126 static void mh_scan_tree_recursive (FolderItem *item);
128 static gboolean mh_rename_folder_func (GNode *node,
130 static gchar *mh_item_get_path (Folder *folder,
133 static gboolean mh_scan_required (Folder *folder,
135 static int mh_item_close (Folder *folder,
138 static gint mh_get_flags (Folder *folder, FolderItem *item,
139 MsgInfoList *msginfo_list, GRelation *msgflags);
141 static void mh_write_sequences (FolderItem *item, gboolean remove_unseen);
143 static FolderClass mh_class;
145 FolderClass *mh_get_class(void)
147 if (mh_class.idstr == NULL) {
148 mh_class.type = F_MH;
149 mh_class.idstr = "mh";
150 mh_class.uistr = "MH";
152 /* Folder functions */
153 mh_class.new_folder = mh_folder_new;
154 mh_class.destroy_folder = mh_folder_destroy;
155 mh_class.set_xml = folder_local_set_xml;
156 mh_class.get_xml = folder_local_get_xml;
157 mh_class.scan_tree = mh_scan_tree;
158 mh_class.create_tree = mh_create_tree;
160 /* FolderItem functions */
161 mh_class.item_get_path = mh_item_get_path;
162 mh_class.create_folder = mh_create_folder;
163 mh_class.rename_folder = mh_rename_folder;
164 mh_class.remove_folder = mh_remove_folder;
165 mh_class.get_num_list = mh_get_num_list;
166 mh_class.scan_required = mh_scan_required;
167 mh_class.close = mh_item_close;
168 mh_class.get_flags = NULL; /*mh_get_flags */;
170 /* Message functions */
171 mh_class.get_msginfo = mh_get_msginfo;
172 mh_class.fetch_msg = mh_fetch_msg;
173 mh_class.add_msg = mh_add_msg;
174 mh_class.add_msgs = mh_add_msgs;
175 mh_class.copy_msg = mh_copy_msg;
176 mh_class.copy_msgs = mh_copy_msgs;
177 mh_class.remove_msg = mh_remove_msg;
178 mh_class.remove_msgs = mh_remove_msgs;
179 mh_class.remove_all_msg = mh_remove_all_msg;
180 mh_class.is_msg_changed = mh_is_msg_changed;
186 static Folder *mh_folder_new(const gchar *name, const gchar *path)
190 folder = (Folder *)g_new0(MHFolder, 1);
191 folder->klass = &mh_class;
192 mh_folder_init(folder, name, path);
197 static void mh_folder_destroy(Folder *folder)
199 folder_local_folder_destroy(LOCAL_FOLDER(folder));
202 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
204 folder_local_folder_init(folder, name, path);
208 gboolean mh_scan_required(Folder *folder, FolderItem *item)
213 path = folder_item_get_path(item);
214 g_return_val_if_fail(path != NULL, FALSE);
216 if (stat(path, &s) < 0) {
217 FILE_OP_ERROR(path, "stat");
222 if ((s.st_mtime > item->mtime) &&
223 (s.st_mtime - 3600 != item->mtime)) {
224 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
226 (long int) s.st_mtime,
227 (long int) item->mtime);
232 debug_print("MH scan not required: %s (%ld <= %ld)\n",
234 (long int) s.st_mtime,
235 (long int) item->mtime);
240 void mh_get_last_num(Folder *folder, FolderItem *item)
248 g_return_if_fail(item != NULL);
250 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
252 path = folder_item_get_path(item);
253 g_return_if_fail(path != NULL);
254 if (change_dir(path) < 0) {
260 if ((dp = opendir(".")) == NULL) {
261 FILE_OP_ERROR(item->path, "opendir");
265 while ((d = readdir(dp)) != NULL) {
266 if ((num = to_number(d->d_name)) > 0 &&
267 dirent_is_regular_file(d)) {
274 debug_print("Last number in dir %s = %d\n", item->path, max);
275 item->last_num = max;
278 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
284 gint num, nummsgs = 0;
286 g_return_val_if_fail(item != NULL, -1);
288 debug_print("mh_get_num_list(): Scanning %s ...\n", item->path);
290 *old_uids_valid = TRUE;
292 path = folder_item_get_path(item);
293 g_return_val_if_fail(path != NULL, -1);
294 if (change_dir(path) < 0) {
300 if ((dp = opendir(".")) == NULL) {
301 FILE_OP_ERROR(item->path, "opendir");
305 while ((d = readdir(dp)) != NULL) {
306 if ((num = to_number(d->d_name)) > 0) {
307 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
313 item->mtime = time(NULL);
317 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
322 g_return_val_if_fail(item != NULL, NULL);
323 g_return_val_if_fail(num > 0, NULL);
325 path = folder_item_get_path(item);
326 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
328 if (!is_file_exist(file)) {
337 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
342 g_return_val_if_fail(item != NULL, NULL);
346 file = mh_fetch_msg(folder, item, num);
347 if (!file) return NULL;
349 msginfo = mh_parse_msg(file, item);
351 msginfo->msgnum = num;
358 static gchar *mh_get_new_msg_filename(FolderItem *dest)
363 destpath = folder_item_get_path(dest);
364 g_return_val_if_fail(destpath != NULL, NULL);
366 if (!is_dir_exist(destpath))
367 make_dir_hier(destpath);
370 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
372 if (is_file_entry_exist(destfile)) {
384 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
388 MsgFileInfo fileinfo;
390 g_return_val_if_fail(file != NULL, -1);
392 fileinfo.msginfo = NULL;
393 fileinfo.file = (gchar *)file;
394 fileinfo.flags = flags;
395 file_list.data = &fileinfo;
396 file_list.next = NULL;
398 ret = mh_add_msgs(folder, dest, &file_list, NULL);
402 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
407 MsgFileInfo *fileinfo;
409 g_return_val_if_fail(dest != NULL, -1);
410 g_return_val_if_fail(file_list != NULL, -1);
412 if (dest->last_num < 0) {
413 mh_get_last_num(folder, dest);
414 if (dest->last_num < 0) return -1;
417 for (cur = file_list; cur != NULL; cur = cur->next) {
418 fileinfo = (MsgFileInfo *)cur->data;
420 destfile = mh_get_new_msg_filename(dest);
421 if (destfile == NULL) return -1;
424 if (link(fileinfo->file, destfile) < 0) {
426 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
427 g_warning(_("can't copy message %s to %s\n"),
428 fileinfo->file, destfile);
436 if (relation != NULL)
437 g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
441 mh_write_sequences(dest, TRUE);
442 return dest->last_num;
445 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
449 g_return_val_if_fail(msginfo != NULL, -1);
451 msglist.data = msginfo;
454 return mh_copy_msgs(folder, dest, &msglist, NULL);
457 static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist,
460 gboolean dest_need_scan = FALSE;
464 FolderItemPrefs *prefs;
465 MsgInfo *msginfo = NULL;
466 MsgInfoList *cur = NULL;
467 gint curnum = 0, total = 0;
468 gchar *srcpath = NULL;
469 gboolean full_fetch = FALSE;
470 time_t last_mtime = (time_t)0;
472 g_return_val_if_fail(dest != NULL, -1);
473 g_return_val_if_fail(msglist != NULL, -1);
475 msginfo = (MsgInfo *)msglist->data;
477 g_return_val_if_fail(msginfo != NULL, -1);
479 if (msginfo->folder == dest) {
480 g_warning("the src folder is identical to the dest.\n");
484 if (msginfo->folder->folder != dest->folder)
487 if (dest->last_num < 0) {
488 mh_get_last_num(folder, dest);
489 if (dest->last_num < 0) return -1;
494 srcpath = folder_item_get_path(msginfo->folder);
496 dest_need_scan = mh_scan_required(dest->folder, dest);
497 last_mtime = dest->mtime;
499 total = g_slist_length(msglist);
501 if (MSG_IS_MOVE(msginfo->flags))
502 statusbar_print_all(_("Moving messages..."));
504 statusbar_print_all(_("Copying messages..."));
506 for (cur = msglist; cur; cur = cur->next) {
507 msginfo = (MsgInfo *)cur->data;
509 goto err_reset_status;
512 srcfile = g_strconcat(srcpath,
514 itos(msginfo->msgnum), NULL);
516 srcfile = procmsg_get_message_file(msginfo);
519 goto err_reset_status;
521 destfile = mh_get_new_msg_filename(dest);
524 goto err_reset_status;
528 statusbar_progress_all(curnum, total, 100);
529 if (curnum % 100 == 0)
534 debug_print("Copying message %s%c%d to %s ...\n",
535 msginfo->folder->path, G_DIR_SEPARATOR,
536 msginfo->msgnum, dest->path);
539 if (MSG_IS_MOVE(msginfo->flags)) {
540 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
541 if (move_file(srcfile, destfile, TRUE) < 0) {
542 FILE_OP_ERROR(srcfile, "move");
543 if (copy_file(srcfile, destfile, TRUE) < 0) {
544 FILE_OP_ERROR(srcfile, "copy");
547 goto err_reset_status;
550 /* say unlinking's not necessary */
551 msginfo->flags.tmp_flags |= MSG_MOVE_DONE;
553 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
554 FILE_OP_ERROR(srcfile, "copy");
557 goto err_reset_status;
559 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
560 if (chmod(destfile, prefs->folder_chmod) < 0)
561 FILE_OP_ERROR(destfile, "chmod");
564 filemode = prefs->folder_chmod;
565 if (filemode & S_IRGRP) filemode |= S_IWGRP;
566 if (filemode & S_IROTH) filemode |= S_IWOTH;
569 g_relation_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
576 mh_write_sequences(dest, TRUE);
578 if (dest->mtime == last_mtime && !dest_need_scan)
579 dest->mtime = time(NULL);
582 statusbar_progress_all(0,0,0);
585 return dest->last_num;
588 mh_write_sequences(dest, TRUE);
590 statusbar_progress_all(0,0,0);
597 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
599 gboolean need_scan = FALSE;
600 time_t last_mtime = (time_t)0;
603 g_return_val_if_fail(item != NULL, -1);
605 file = mh_fetch_msg(folder, item, num);
606 g_return_val_if_fail(file != NULL, -1);
608 need_scan = mh_scan_required(folder, item);
609 last_mtime = item->mtime;
611 if (g_unlink(file) < 0) {
612 FILE_OP_ERROR(file, "unlink");
617 if (item->mtime == last_mtime && !need_scan)
618 item->mtime = time(NULL);
624 static gint mh_remove_msgs(Folder *folder, FolderItem *item,
625 MsgInfoList *msglist, GRelation *relation)
627 gboolean need_scan = FALSE;
629 time_t last_mtime = (time_t)0;
631 gint total = 0, curnum = 0;
633 g_return_val_if_fail(item != NULL, -1);
635 path = folder_item_get_path(item);
637 need_scan = mh_scan_required(folder, item);
638 last_mtime = item->mtime;
640 total = g_slist_length(msglist);
642 statusbar_print_all(_("Deleting messages..."));
645 for (cur = msglist; cur; cur = cur->next) {
646 MsgInfo *msginfo = (MsgInfo *)cur->data;
651 if (MSG_IS_MOVE(msginfo->flags) && MSG_IS_MOVE_DONE(msginfo->flags)) {
652 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
656 statusbar_progress_all(curnum, total, 100);
657 if (curnum % 100 == 0)
663 tuples = g_relation_select(relation, msginfo, 0);
664 num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
665 g_tuples_destroy(tuples);
669 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
673 if (g_unlink(file) < 0) {
682 statusbar_progress_all(0,0,0);
685 if (item->mtime == last_mtime && !need_scan)
686 item->mtime = time(NULL);
692 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
697 g_return_val_if_fail(item != NULL, -1);
699 path = folder_item_get_path(item);
700 g_return_val_if_fail(path != NULL, -1);
701 val = remove_all_numbered_files(path);
704 mh_write_sequences(item, TRUE);
709 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
714 if (g_stat(itos(msginfo->msgnum), &s) < 0 ||
715 msginfo->size != s.st_size || (
716 (msginfo->mtime - s.st_mtime != 0) &&
717 (msginfo->mtime - s.st_mtime != 3600) &&
718 (msginfo->mtime - s.st_mtime != -3600)))
724 static gint mh_scan_tree(Folder *folder)
729 g_return_val_if_fail(folder != NULL, -1);
732 item = folder_item_new(folder, folder->name, NULL);
733 item->folder = folder;
734 folder->node = item->node = g_node_new(item);
736 item = FOLDER_ITEM(folder->node->data);
738 rootpath = folder_item_get_path(item);
739 if (change_dir(rootpath) < 0) {
745 mh_create_tree(folder);
746 mh_remove_missing_folder_items(folder);
747 mh_scan_tree_recursive(item);
752 #define MAKE_DIR_IF_NOT_EXIST(dir) \
754 if (!is_dir_exist(dir)) { \
755 if (is_file_exist(dir)) { \
756 g_warning("File `%s' already exists.\n" \
757 "Can't create folder.", dir); \
760 if (make_dir(dir) < 0) \
765 static gint mh_create_tree(Folder *folder)
769 g_return_val_if_fail(folder != NULL, -1);
771 CHDIR_RETURN_VAL_IF_FAIL(get_mail_base_dir(), -1);
772 rootpath = LOCAL_FOLDER(folder)->rootpath;
773 MAKE_DIR_IF_NOT_EXIST(rootpath);
774 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
775 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
776 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
777 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
778 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
779 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
784 #undef MAKE_DIR_IF_NOT_EXIST
786 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
788 gchar *folder_path, *path;
790 g_return_val_if_fail(folder != NULL, NULL);
791 g_return_val_if_fail(item != NULL, NULL);
793 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
794 g_return_val_if_fail(folder_path != NULL, NULL);
796 /* FIXME: [W32] The code below does not correctly merge
797 relative filenames; there should be a function to handle
799 if ( !is_relative_filename (folder_path) ) {
801 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
804 path = g_strdup(folder_path);
807 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
808 folder_path, G_DIR_SEPARATOR_S,
811 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
815 real_path = mh_filename_from_utf8(path);
816 if (!is_dir_exist(real_path) && is_dir_exist(path)) {
817 /* mmh, older version did put utf8 filenames instead of
818 * the correct encoding */
819 rename(path, real_path);
820 folder_item_scan(item);
827 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
830 gchar *path, *real_name;
832 FolderItem *new_item;
833 gchar *mh_sequences_filename;
834 FILE *mh_sequences_file;
836 g_return_val_if_fail(folder != NULL, NULL);
837 g_return_val_if_fail(parent != NULL, NULL);
838 g_return_val_if_fail(name != NULL, NULL);
840 path = folder_item_get_path(parent);
841 if (!is_dir_exist(path))
842 if (make_dir_hier(path) != 0)
845 real_name = mh_filename_from_utf8(name);
846 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
850 if (make_dir(fullpath) < 0) {
858 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
861 path = g_strdup(name);
862 new_item = folder_item_new(folder, name, path);
863 folder_item_append(parent, new_item);
867 path = folder_item_get_path(new_item);
868 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
869 ".mh_sequences", NULL);
870 if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
871 fclose(mh_sequences_file);
873 g_free(mh_sequences_filename);
879 static gint mh_rename_folder(Folder *folder, FolderItem *item,
885 gchar *newpath, *utf8newpath;
888 g_return_val_if_fail(folder != NULL, -1);
889 g_return_val_if_fail(item != NULL, -1);
890 g_return_val_if_fail(item->path != NULL, -1);
891 g_return_val_if_fail(name != NULL, -1);
893 oldpath = folder_item_get_path(item);
894 if (!is_dir_exist(oldpath))
895 make_dir_hier(oldpath);
897 dirname = g_path_get_dirname(oldpath);
898 real_name = mh_filename_from_utf8(name);
899 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
902 if (g_rename(oldpath, newpath) < 0) {
903 FILE_OP_ERROR(oldpath, "rename");
912 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
913 dirname = g_path_get_dirname(item->path);
914 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
918 utf8newpath = g_strdup(name);
921 item->name = g_strdup(name);
923 paths[0] = g_strdup(item->path);
924 paths[1] = utf8newpath;
925 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
926 mh_rename_folder_func, paths);
933 static gint mh_remove_folder(Folder *folder, FolderItem *item)
937 g_return_val_if_fail(folder != NULL, -1);
938 g_return_val_if_fail(item != NULL, -1);
939 g_return_val_if_fail(item->path != NULL, -1);
941 path = folder_item_get_path(item);
942 if (remove_dir_recursive(path) < 0) {
943 g_warning("can't remove directory `%s'\n", path);
949 folder_item_remove(item);
953 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
958 g_return_val_if_fail(item != NULL, NULL);
959 g_return_val_if_fail(file != NULL, NULL);
961 flags.perm_flags = MSG_NEW|MSG_UNREAD;
964 if (folder_has_parent_of_type(item, F_QUEUE)) {
965 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
966 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
967 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
970 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
971 if (!msginfo) return NULL;
973 msginfo->msgnum = atoi(file);
974 msginfo->folder = item;
979 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
984 g_return_val_if_fail(node->data != NULL, FALSE);
986 if (G_NODE_IS_ROOT(node))
989 item = FOLDER_ITEM(node->data);
991 path = folder_item_get_path(item);
992 if (!is_dir_exist(path)) {
993 debug_print("folder '%s' not found. removing...\n", path);
994 folder_item_remove(item);
1001 static void mh_remove_missing_folder_items(Folder *folder)
1003 g_return_if_fail(folder != NULL);
1005 debug_print("searching missing folders...\n");
1007 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1008 mh_remove_missing_folder_items_func, folder);
1011 static void mh_scan_tree_recursive(FolderItem *item)
1020 const gchar *dir_name;
1022 gchar *real_path, *entry, *utf8entry, *utf8name;
1025 g_return_if_fail(item != NULL);
1026 g_return_if_fail(item->folder != NULL);
1028 folder = item->folder;
1030 real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
1032 dir = g_dir_open(real_path, 0, NULL);
1034 g_warning("failed to open directory: %s\n", real_path);
1039 dp = opendir(real_path);
1041 FILE_OP_ERROR(real_path, "opendir");
1047 debug_print("scanning %s ...\n",
1048 item->path ? item->path
1049 : LOCAL_FOLDER(item->folder)->rootpath);
1050 if (folder->ui_func)
1051 folder->ui_func(folder, item, folder->ui_func_data);
1054 while ((dir_name = g_dir_read_name(dir)) != NULL) {
1056 while ((d = readdir(dp)) != NULL) {
1057 dir_name = d->d_name;
1059 if (dir_name[0] == '.') continue;
1061 utf8name = mh_filename_to_utf8(dir_name);
1063 utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1066 utf8entry = g_strdup(utf8name);
1067 entry = mh_filename_from_utf8(utf8entry);
1070 #if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
1071 d->d_type == DT_DIR ||
1072 (d->d_type == DT_UNKNOWN &&
1074 g_stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
1075 #if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
1079 FolderItem *new_item = NULL;
1083 for (node = node->children; node != NULL; node = node->next) {
1084 FolderItem *cur_item = FOLDER_ITEM(node->data);
1085 if (!strcmp2(cur_item->path, entry)) {
1086 new_item = cur_item;
1091 debug_print("new folder '%s' found.\n", entry);
1092 new_item = folder_item_new(folder, utf8name, utf8entry);
1093 folder_item_append(item, new_item);
1097 if (!folder->inbox &&
1098 !strcmp(dir_name, INBOX_DIR)) {
1099 new_item->stype = F_INBOX;
1100 folder->inbox = new_item;
1101 } else if (!folder->outbox &&
1102 !strcmp(dir_name, OUTBOX_DIR)) {
1103 new_item->stype = F_OUTBOX;
1104 folder->outbox = new_item;
1105 } else if (!folder->draft &&
1106 !strcmp(dir_name, DRAFT_DIR)) {
1107 new_item->stype = F_DRAFT;
1108 folder->draft = new_item;
1109 } else if (!folder->queue &&
1110 !strcmp(dir_name, QUEUE_DIR)) {
1111 new_item->stype = F_QUEUE;
1112 folder->queue = new_item;
1113 } else if (!folder->trash &&
1114 !strcmp(dir_name, TRASH_DIR)) {
1115 new_item->stype = F_TRASH;
1116 folder->trash = new_item;
1120 mh_scan_tree_recursive(new_item);
1121 } else if (to_number(dir_name) > 0) n_msg++;
1134 item->mtime = time(NULL);
1137 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1139 FolderItem *item = node->data;
1140 gchar **paths = data;
1141 const gchar *oldpath = paths[0];
1142 const gchar *newpath = paths[1];
1144 gchar *new_itempath;
1147 oldpathlen = strlen(oldpath);
1148 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1149 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1153 base = item->path + oldpathlen;
1154 while (*base == G_DIR_SEPARATOR) base++;
1156 new_itempath = g_strdup(newpath);
1158 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1161 item->path = new_itempath;
1166 static gchar *mh_filename_from_utf8(const gchar *path)
1168 gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1171 g_warning("mh_filename_from_utf8: faild to convert character set\n");
1172 real_path = g_strdup(path);
1178 static gchar *mh_filename_to_utf8(const gchar *path)
1180 gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1182 g_warning("mh_filename_to_utf8: faild to convert character set\n");
1183 utf8path = g_strdup(path);
1189 static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1191 MsgInfo *msginfo_a = (MsgInfo *) a;
1192 MsgInfo *msginfo_b = (MsgInfo *) b;
1194 return (msginfo_a->msgnum - msginfo_b->msgnum);
1197 static gchar *get_unseen_seq_name(void)
1199 static gchar *seq_name = NULL;
1201 gchar buf[BUFFSIZE];
1203 gchar *profile_path = g_strconcat(
1204 get_home_dir(), G_DIR_SEPARATOR_S,
1205 ".mh_profile", NULL);
1206 FILE *fp = g_fopen(profile_path, "r");
1208 while (fgets(buf, sizeof(buf), fp) != NULL) {
1209 if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
1210 gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
1211 while (*seq_tmp == ' ')
1213 seq_name = g_strdup(seq_tmp);
1214 seq_name = strretchomp(seq_name);
1221 seq_name = g_strdup("unseen");
1222 tmp = g_strdup_printf("%s:", seq_name);
1230 static gint mh_get_flags(Folder *folder, FolderItem *item,
1231 MsgInfoList *msginfo_list, GRelation *msgflags)
1233 gchar *mh_sequences_filename;
1234 FILE *mh_sequences_file;
1235 gchar buf[BUFFSIZE];
1236 gchar *unseen_list = NULL;
1238 MsgInfoList *mcur = NULL;
1240 GTimer *timer = g_timer_new();
1241 g_timer_start(timer);
1246 /* don't update from .mh_sequences if the item's opened: mails may have
1247 * been marked read/unread and it's not yet written in the file. */
1251 path = folder_item_get_path(item);
1253 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
1254 ".mh_sequences", NULL);
1256 if ((mh_sequences_file = g_fopen(mh_sequences_filename, "r+b")) != NULL) {
1257 while (fgets(buf, sizeof(buf), mh_sequences_file) != NULL) {
1258 if (!strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name()))) {
1259 unseen_list = g_strdup(buf+strlen(get_unseen_seq_name()));
1263 fclose(mh_sequences_file);
1266 g_free(mh_sequences_filename);
1270 gchar *token = NULL, *next = NULL, *boundary = NULL;
1272 GHashTable *unseen_table = g_hash_table_new(g_direct_hash, g_direct_equal);
1274 cur = unseen_list = strretchomp(unseen_list);
1275 debug_print("found unseen list in .mh_sequences: %s\n", unseen_list);
1277 while (*cur && *cur == ' ')
1280 if ((next = strchr(cur, ' ')) != NULL) {
1289 if ((boundary = strchr(token, '-')) != NULL) {
1295 for (i = atoi(start); i <= atoi(end); i++) {
1296 g_hash_table_insert(unseen_table, GINT_TO_POINTER(i), GINT_TO_POINTER(1));
1298 } else if ((num = atoi(token)) > 0) {
1299 g_hash_table_insert(unseen_table, GINT_TO_POINTER(num), GINT_TO_POINTER(1));
1304 for (mcur = msginfo_list; mcur; mcur = mcur->next) {
1305 MsgInfo *msginfo = (MsgInfo *)mcur->data;
1306 MsgPermFlags flags = msginfo->flags.perm_flags;
1307 if (g_hash_table_lookup(unseen_table, GINT_TO_POINTER(msginfo->msgnum))) {
1308 flags |= MSG_UNREAD;
1309 } else if (!(flags & MSG_NEW)) { /* don't mark new msgs as read */
1310 flags &= ~(MSG_UNREAD);
1312 if (flags != msginfo->flags.perm_flags)
1313 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
1315 g_hash_table_destroy(unseen_table);
1316 g_free(unseen_list);
1319 g_timer_stop(timer);
1320 printf("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
1321 g_timer_destroy(timer);
1327 static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
1329 gchar *mh_sequences_old, *mh_sequences_new;
1330 FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
1331 gchar buf[BUFFSIZE];
1334 GTimer *timer = g_timer_new();
1335 g_timer_start(timer);
1340 path = folder_item_get_path(item);
1342 mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
1343 ".mh_sequences", NULL);
1344 mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
1345 ".mh_sequences.new", NULL);
1346 if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
1347 GSList *msglist = folder_item_get_msg_list(item);
1349 MsgInfo *info = NULL;
1350 gint start = -1, end = -1;
1351 gchar *sequence = g_strdup("");
1352 msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
1355 /* write the unseen sequence if we don't have to scrap it */
1356 if (!remove_unseen) do {
1357 info = (MsgInfo *)(cur ? cur->data:NULL);
1358 if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
1360 start = end = info->msgnum;
1364 if (start > 0 && end > 0) {
1365 gchar *tmp = sequence;
1367 sequence = g_strdup_printf("%s %d-%d ", tmp, start, end);
1369 sequence = g_strdup_printf("%s %d ", tmp, start);
1374 cur = cur ? cur->next:NULL;
1375 } while (cur || (start > 0 && end > 0));
1376 if (sequence && strlen(sequence)) {
1377 fprintf(mh_sequences_new_fp, "%s%s\n",
1378 get_unseen_seq_name(), sequence);
1379 debug_print("wrote unseen sequence: '%s%s'\n",
1380 get_unseen_seq_name(), sequence);
1382 /* rewrite the rest of the file */
1383 if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
1384 while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
1385 if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
1386 fprintf(mh_sequences_new_fp, "%s", buf);
1388 fclose(mh_sequences_old_fp);
1391 fclose(mh_sequences_new_fp);
1392 g_rename(mh_sequences_new, mh_sequences_old);
1394 procmsg_msg_list_free(msglist);
1396 g_free(mh_sequences_old);
1397 g_free(mh_sequences_new);
1400 g_timer_stop(timer);
1401 printf("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
1402 g_timer_destroy(timer);
1406 static int mh_item_close(Folder *folder, FolderItem *item)
1408 mh_write_sequences(item, FALSE);