2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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,
78 GHashTable *relation);
79 static gint mh_copy_msg (Folder *folder,
82 static gint mh_copy_msgs (Folder *folder,
85 GHashTable *relation);
86 static gint mh_remove_msg (Folder *folder,
89 static gint mh_remove_msgs (Folder *folder,
92 GHashTable *relation);
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 void mh_set_mtime (Folder *folder,
133 static int mh_item_close (Folder *folder,
136 static gint mh_get_flags (Folder *folder, FolderItem *item,
137 MsgInfoList *msginfo_list, GHashTable *msgflags);
139 static void mh_write_sequences (FolderItem *item, gboolean remove_unseen);
141 static FolderClass mh_class;
143 FolderClass *mh_get_class(void)
145 if (mh_class.idstr == NULL) {
146 mh_class.type = F_MH;
147 mh_class.idstr = "mh";
148 mh_class.uistr = "MH";
150 /* Folder functions */
151 mh_class.new_folder = mh_folder_new;
152 mh_class.destroy_folder = mh_folder_destroy;
153 mh_class.set_xml = folder_local_set_xml;
154 mh_class.get_xml = folder_local_get_xml;
155 mh_class.scan_tree = mh_scan_tree;
156 mh_class.create_tree = mh_create_tree;
158 /* FolderItem functions */
159 mh_class.item_get_path = mh_item_get_path;
160 mh_class.create_folder = mh_create_folder;
161 mh_class.rename_folder = mh_rename_folder;
162 mh_class.remove_folder = mh_remove_folder;
163 mh_class.get_num_list = mh_get_num_list;
164 mh_class.scan_required = mh_scan_required;
165 mh_class.set_mtime = mh_set_mtime;
166 mh_class.close = mh_item_close;
167 mh_class.get_flags = NULL; /*mh_get_flags */;
169 /* Message functions */
170 mh_class.get_msginfo = mh_get_msginfo;
171 mh_class.fetch_msg = mh_fetch_msg;
172 mh_class.add_msg = mh_add_msg;
173 mh_class.add_msgs = mh_add_msgs;
174 mh_class.copy_msg = mh_copy_msg;
175 mh_class.copy_msgs = mh_copy_msgs;
176 mh_class.remove_msg = mh_remove_msg;
177 mh_class.remove_msgs = mh_remove_msgs;
178 mh_class.remove_all_msg = mh_remove_all_msg;
179 mh_class.is_msg_changed = mh_is_msg_changed;
185 static Folder *mh_folder_new(const gchar *name, const gchar *path)
189 folder = (Folder *)g_new0(MHFolder, 1);
190 folder->klass = &mh_class;
191 mh_folder_init(folder, name, path);
196 static void mh_folder_destroy(Folder *folder)
198 folder_local_folder_destroy(LOCAL_FOLDER(folder));
201 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
203 folder_local_folder_init(folder, name, path);
207 gboolean mh_scan_required(Folder *folder, FolderItem *item)
212 path = folder_item_get_path(item);
213 cm_return_val_if_fail(path != NULL, FALSE);
215 if (g_stat(path, &s) < 0) {
216 FILE_OP_ERROR(path, "stat");
221 if ((s.st_mtime > item->mtime) &&
222 (s.st_mtime - 3600 != item->mtime)) {
223 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
225 (long int) s.st_mtime,
226 (long int) item->mtime);
231 debug_print("MH scan not required: %s (%ld <= %ld)\n",
233 (long int) s.st_mtime,
234 (long int) item->mtime);
239 static void mh_get_last_num(Folder *folder, FolderItem *item)
247 cm_return_if_fail(item != NULL);
249 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path?item->path:"(null)");
251 path = folder_item_get_path(item);
252 cm_return_if_fail(path != NULL);
253 if (change_dir(path) < 0) {
259 if ((dp = opendir(".")) == NULL) {
260 FILE_OP_ERROR(item->path, "opendir");
264 while ((d = readdir(dp)) != NULL) {
265 if ((num = to_number(d->d_name)) > 0 &&
266 dirent_is_regular_file(d)) {
275 debug_print("Last number in dir %s = %d\n", item->path?item->path:"(null)", max);
276 item->last_num = max;
279 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
285 gint num, nummsgs = 0;
287 cm_return_val_if_fail(item != NULL, -1);
289 debug_print("mh_get_num_list(): Scanning %s ...\n", item->path?item->path:"(null)");
291 *old_uids_valid = TRUE;
293 path = folder_item_get_path(item);
294 cm_return_val_if_fail(path != NULL, -1);
295 if (change_dir(path) < 0) {
301 if ((dp = opendir(".")) == NULL) {
302 FILE_OP_ERROR(item->path, "opendir");
306 while ((d = readdir(dp)) != NULL) {
307 if ((num = to_number(d->d_name)) > 0) {
308 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
314 mh_set_mtime(folder, item);
318 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
323 cm_return_val_if_fail(item != NULL, NULL);
324 cm_return_val_if_fail(num > 0, NULL);
326 path = folder_item_get_path(item);
327 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
329 if (!is_file_exist(file)) {
338 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
343 cm_return_val_if_fail(item != NULL, NULL);
347 file = mh_fetch_msg(folder, item, num);
348 if (!file) return NULL;
350 msginfo = mh_parse_msg(file, item);
352 msginfo->msgnum = num;
359 static gchar *mh_get_new_msg_filename(FolderItem *dest)
364 destpath = folder_item_get_path(dest);
365 cm_return_val_if_fail(destpath != NULL, NULL);
367 if (!is_dir_exist(destpath))
368 make_dir_hier(destpath);
371 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
373 if (is_file_entry_exist(destfile)) {
385 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
389 MsgFileInfo fileinfo;
391 cm_return_val_if_fail(file != NULL, -1);
393 fileinfo.msginfo = NULL;
394 fileinfo.file = (gchar *)file;
395 fileinfo.flags = flags;
396 file_list.data = &fileinfo;
397 file_list.next = NULL;
399 ret = mh_add_msgs(folder, dest, &file_list, NULL);
403 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
404 GHashTable *relation)
408 MsgFileInfo *fileinfo;
410 cm_return_val_if_fail(dest != NULL, -1);
411 cm_return_val_if_fail(file_list != NULL, -1);
413 if (dest->last_num < 0) {
414 mh_get_last_num(folder, dest);
415 if (dest->last_num < 0) return -1;
418 for (cur = file_list; cur != NULL; cur = cur->next) {
419 fileinfo = (MsgFileInfo *)cur->data;
421 destfile = mh_get_new_msg_filename(dest);
422 if (destfile == NULL) return -1;
425 if (link(fileinfo->file, destfile) < 0) {
427 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
428 g_warning(_("can't copy message %s to %s\n"),
429 fileinfo->file, destfile);
437 if (relation != NULL)
438 g_hash_table_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
442 mh_write_sequences(dest, TRUE);
443 return dest->last_num;
446 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
450 cm_return_val_if_fail(msginfo != NULL, -1);
452 msglist.data = msginfo;
455 return mh_copy_msgs(folder, dest, &msglist, NULL);
458 static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist,
459 GHashTable *relation)
461 gboolean dest_need_scan = FALSE;
462 gboolean src_need_scan = FALSE;
463 FolderItem *src = NULL;
467 FolderItemPrefs *prefs;
468 MsgInfo *msginfo = NULL;
469 MsgInfoList *cur = NULL;
470 gint curnum = 0, total = 0;
471 gchar *srcpath = NULL;
472 gboolean full_fetch = FALSE;
473 time_t last_dest_mtime = (time_t)0;
474 time_t last_src_mtime = (time_t)0;
476 cm_return_val_if_fail(dest != NULL, -1);
477 cm_return_val_if_fail(msglist != NULL, -1);
479 msginfo = (MsgInfo *)msglist->data;
481 cm_return_val_if_fail(msginfo != NULL, -1);
483 if (msginfo->folder == dest) {
484 g_warning("the src folder is identical to the dest.\n");
488 if (msginfo->folder->folder != dest->folder)
491 if (FOLDER_TYPE(msginfo->folder->folder) == F_MH) {
492 src = msginfo->folder;
495 if (dest->last_num < 0) {
496 mh_get_last_num(folder, dest);
497 if (dest->last_num < 0) return -1;
502 srcpath = folder_item_get_path(msginfo->folder);
504 dest_need_scan = mh_scan_required(dest->folder, dest);
505 last_dest_mtime = dest->mtime;
508 src_need_scan = mh_scan_required(src->folder, src);
509 last_src_mtime = src->mtime;
512 total = g_slist_length(msglist);
514 if (MSG_IS_MOVE(msginfo->flags))
515 statusbar_print_all(_("Moving messages..."));
517 statusbar_print_all(_("Copying messages..."));
519 for (cur = msglist; cur; cur = cur->next) {
520 msginfo = (MsgInfo *)cur->data;
522 goto err_reset_status;
525 srcfile = g_strconcat(srcpath,
527 itos(msginfo->msgnum), NULL);
529 srcfile = procmsg_get_message_file(msginfo);
532 goto err_reset_status;
534 destfile = mh_get_new_msg_filename(dest);
537 goto err_reset_status;
541 statusbar_progress_all(curnum, total, 100);
542 if (curnum % 100 == 0)
547 debug_print("Copying message %s%c%d to %s ...\n",
548 msginfo->folder->path, G_DIR_SEPARATOR,
549 msginfo->msgnum, dest->path);
552 if (MSG_IS_MOVE(msginfo->flags)) {
553 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
554 if (move_file(srcfile, destfile, TRUE) < 0) {
555 FILE_OP_ERROR(srcfile, "move");
556 if (copy_file(srcfile, destfile, TRUE) < 0) {
557 FILE_OP_ERROR(srcfile, "copy");
560 goto err_reset_status;
563 /* say unlinking's not necessary */
564 msginfo->flags.tmp_flags |= MSG_MOVE_DONE;
566 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
567 FILE_OP_ERROR(srcfile, "copy");
570 goto err_reset_status;
572 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
573 if (chmod(destfile, prefs->folder_chmod) < 0)
574 FILE_OP_ERROR(destfile, "chmod");
577 filemode = prefs->folder_chmod;
578 if (filemode & S_IRGRP) filemode |= S_IWGRP;
579 if (filemode & S_IROTH) filemode |= S_IWOTH;
582 if (g_hash_table_lookup(relation, msginfo) != NULL)
583 g_warning("already in : %p", msginfo);
585 g_hash_table_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
593 mh_write_sequences(dest, TRUE);
595 if (dest->mtime == last_dest_mtime && !dest_need_scan) {
596 mh_set_mtime(folder, dest);
599 if (src && src->mtime == last_src_mtime && !src_need_scan) {
600 mh_set_mtime(folder, src);
604 statusbar_progress_all(0,0,0);
607 return dest->last_num;
610 mh_write_sequences(dest, TRUE);
612 statusbar_progress_all(0,0,0);
619 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
621 gboolean need_scan = FALSE;
622 time_t last_mtime = (time_t)0;
625 cm_return_val_if_fail(item != NULL, -1);
627 file = mh_fetch_msg(folder, item, num);
628 cm_return_val_if_fail(file != NULL, -1);
630 need_scan = mh_scan_required(folder, item);
631 last_mtime = item->mtime;
633 if (claws_unlink(file) < 0) {
634 FILE_OP_ERROR(file, "unlink");
639 if (item->mtime == last_mtime && !need_scan) {
640 mh_set_mtime(folder, item);
646 static gint mh_remove_msgs(Folder *folder, FolderItem *item,
647 MsgInfoList *msglist, GHashTable *relation)
649 gboolean need_scan = FALSE;
651 time_t last_mtime = (time_t)0;
653 gint total = 0, curnum = 0;
655 cm_return_val_if_fail(item != NULL, -1);
657 path = folder_item_get_path(item);
659 need_scan = mh_scan_required(folder, item);
660 last_mtime = item->mtime;
662 total = g_slist_length(msglist);
664 statusbar_print_all(_("Deleting messages..."));
667 for (cur = msglist; cur; cur = cur->next) {
668 MsgInfo *msginfo = (MsgInfo *)cur->data;
671 if (MSG_IS_MOVE(msginfo->flags) && MSG_IS_MOVE_DONE(msginfo->flags)) {
672 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
676 statusbar_progress_all(curnum, total, 100);
677 if (curnum % 100 == 0)
682 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
686 if (claws_unlink(file) < 0) {
695 statusbar_progress_all(0,0,0);
698 if (item->mtime == last_mtime && !need_scan) {
699 mh_set_mtime(folder, item);
706 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
711 cm_return_val_if_fail(item != NULL, -1);
713 path = folder_item_get_path(item);
714 cm_return_val_if_fail(path != NULL, -1);
715 val = remove_all_numbered_files(path);
718 mh_write_sequences(item, TRUE);
723 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
728 if (g_stat(itos(msginfo->msgnum), &s) < 0 ||
729 msginfo->size != s.st_size || (
730 (msginfo->mtime - s.st_mtime != 0) &&
731 (msginfo->mtime - s.st_mtime != 3600) &&
732 (msginfo->mtime - s.st_mtime != -3600)))
738 static gint mh_scan_tree(Folder *folder)
743 cm_return_val_if_fail(folder != NULL, -1);
746 item = folder_item_new(folder, folder->name, NULL);
747 item->folder = folder;
748 folder->node = item->node = g_node_new(item);
750 item = FOLDER_ITEM(folder->node->data);
752 rootpath = folder_item_get_path(item);
753 if (change_dir(rootpath) < 0) {
759 mh_create_tree(folder);
760 mh_remove_missing_folder_items(folder);
761 mh_scan_tree_recursive(item);
766 #define MAKE_DIR_IF_NOT_EXIST(dir) \
768 if (!is_dir_exist(dir)) { \
769 if (is_file_exist(dir)) { \
770 g_warning("File `%s' already exists.\n" \
771 "Can't create folder.", dir); \
774 if (make_dir_hier(dir) < 0) \
779 static gint mh_create_tree(Folder *folder)
783 cm_return_val_if_fail(folder != NULL, -1);
785 CHDIR_RETURN_VAL_IF_FAIL(get_mail_base_dir(), -1);
786 rootpath = LOCAL_FOLDER(folder)->rootpath;
787 MAKE_DIR_IF_NOT_EXIST(rootpath);
788 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
790 /* Create special directories as needed */
791 if (folder->inbox != NULL &&
792 folder->inbox->path != NULL)
793 f = folder->inbox->path;
796 MAKE_DIR_IF_NOT_EXIST(f);
798 if (folder->outbox != NULL &&
799 folder->outbox->path != NULL)
800 f = folder->outbox->path;
803 MAKE_DIR_IF_NOT_EXIST(f);
805 if (folder->draft != NULL &&
806 folder->draft->path != NULL)
807 f = folder->draft->path;
810 MAKE_DIR_IF_NOT_EXIST(f);
812 if (folder->queue != NULL &&
813 folder->queue->path != NULL)
814 f = folder->queue->path;
817 MAKE_DIR_IF_NOT_EXIST(f);
819 if (folder->trash != NULL &&
820 folder->trash->path != NULL)
821 f = folder->trash->path;
824 MAKE_DIR_IF_NOT_EXIST(f);
829 #undef MAKE_DIR_IF_NOT_EXIST
831 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
833 gchar *folder_path, *path;
835 cm_return_val_if_fail(folder != NULL, NULL);
836 cm_return_val_if_fail(item != NULL, NULL);
838 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
839 cm_return_val_if_fail(folder_path != NULL, NULL);
841 /* FIXME: [W32] The code below does not correctly merge
842 relative filenames; there should be a function to handle
844 if ( !is_relative_filename (folder_path) ) {
846 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
849 path = g_strdup(folder_path);
852 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
853 folder_path, G_DIR_SEPARATOR_S,
856 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
860 real_path = mh_filename_from_utf8(path);
861 if (!is_dir_exist(real_path) && is_dir_exist(path)) {
862 /* mmh, older version did put utf8 filenames instead of
863 * the correct encoding */
864 g_rename(path, real_path);
865 folder_item_scan(item);
872 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
875 gchar *path, *real_name;
877 FolderItem *new_item;
878 gchar *mh_sequences_filename;
879 FILE *mh_sequences_file;
881 cm_return_val_if_fail(folder != NULL, NULL);
882 cm_return_val_if_fail(parent != NULL, NULL);
883 cm_return_val_if_fail(name != NULL, NULL);
885 path = folder_item_get_path(parent);
886 if (!is_dir_exist(path))
887 if (make_dir_hier(path) != 0)
890 real_name = mh_filename_from_utf8(name);
891 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
895 if (make_dir(fullpath) < 0) {
903 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
906 path = g_strdup(name);
907 new_item = folder_item_new(folder, name, path);
908 folder_item_append(parent, new_item);
912 path = folder_item_get_path(new_item);
913 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
914 ".mh_sequences", NULL);
915 if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
916 fclose(mh_sequences_file);
918 g_free(mh_sequences_filename);
924 static gint mh_rename_folder(Folder *folder, FolderItem *item,
930 gchar *newpath, *utf8newpath;
933 cm_return_val_if_fail(folder != NULL, -1);
934 cm_return_val_if_fail(item != NULL, -1);
935 cm_return_val_if_fail(item->path != NULL, -1);
936 cm_return_val_if_fail(name != NULL, -1);
938 oldpath = folder_item_get_path(item);
939 if (!is_dir_exist(oldpath))
940 make_dir_hier(oldpath);
942 dirname = g_path_get_dirname(oldpath);
943 real_name = mh_filename_from_utf8(name);
944 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
947 if (g_rename(oldpath, newpath) < 0) {
948 FILE_OP_ERROR(oldpath, "rename");
957 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
958 dirname = g_path_get_dirname(item->path);
959 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
963 utf8newpath = g_strdup(name);
966 item->name = g_strdup(name);
968 paths[0] = g_strdup(item->path);
969 paths[1] = utf8newpath;
970 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
971 mh_rename_folder_func, paths);
978 static gint mh_remove_folder(Folder *folder, FolderItem *item)
982 cm_return_val_if_fail(folder != NULL, -1);
983 cm_return_val_if_fail(item != NULL, -1);
984 cm_return_val_if_fail(item->path != NULL, -1);
986 path = folder_item_get_path(item);
987 if (remove_dir_recursive(path) < 0) {
988 g_warning("can't remove directory `%s'\n", path);
994 folder_item_remove(item);
998 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1003 cm_return_val_if_fail(item != NULL, NULL);
1004 cm_return_val_if_fail(file != NULL, NULL);
1006 flags.perm_flags = MSG_NEW|MSG_UNREAD;
1007 flags.tmp_flags = 0;
1009 if (folder_has_parent_of_type(item, F_QUEUE)) {
1010 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1011 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
1012 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1015 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1016 if (!msginfo) return NULL;
1018 msginfo->msgnum = atoi(file);
1019 msginfo->folder = item;
1024 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
1029 cm_return_val_if_fail(node->data != NULL, FALSE);
1031 if (G_NODE_IS_ROOT(node))
1034 item = FOLDER_ITEM(node->data);
1036 path = folder_item_get_path(item);
1037 if (!is_dir_exist(path)) {
1038 debug_print("folder '%s' not found. removing...\n", path?path:"(null)");
1039 folder_item_remove(item);
1046 static void mh_remove_missing_folder_items(Folder *folder)
1048 cm_return_if_fail(folder != NULL);
1050 debug_print("searching missing folders...\n");
1052 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1053 mh_remove_missing_folder_items_func, folder);
1056 static void mh_scan_tree_recursive(FolderItem *item)
1065 const gchar *dir_name;
1067 gchar *real_path, *entry, *utf8entry, *utf8name;
1070 cm_return_if_fail(item != NULL);
1071 cm_return_if_fail(item->folder != NULL);
1073 folder = item->folder;
1075 real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
1077 dir = g_dir_open(real_path, 0, NULL);
1079 g_warning("failed to open directory: %s\n", real_path);
1084 dp = opendir(real_path);
1086 FILE_OP_ERROR(real_path, "opendir");
1092 debug_print("scanning %s ...\n",
1093 item->path ? item->path
1094 : LOCAL_FOLDER(item->folder)->rootpath);
1095 if (folder->ui_func)
1096 folder->ui_func(folder, item, folder->ui_func_data);
1099 while ((dir_name = g_dir_read_name(dir)) != NULL) {
1101 while ((d = readdir(dp)) != NULL) {
1102 dir_name = d->d_name;
1104 if (dir_name[0] == '.') continue;
1106 utf8name = mh_filename_to_utf8(dir_name);
1108 utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1111 utf8entry = g_strdup(utf8name);
1112 entry = mh_filename_from_utf8(utf8entry);
1115 #if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
1116 d->d_type == DT_DIR ||
1117 (d->d_type == DT_UNKNOWN &&
1119 g_stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
1120 #if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
1124 FolderItem *new_item = NULL;
1128 for (node = node->children; node != NULL; node = node->next) {
1129 FolderItem *cur_item = FOLDER_ITEM(node->data);
1130 gchar *curpath = mh_filename_from_utf8(cur_item->path);
1131 if (!strcmp2(curpath, entry)) {
1132 new_item = cur_item;
1139 debug_print("new folder '%s' found.\n", entry);
1140 new_item = folder_item_new(folder, utf8name, utf8entry);
1141 folder_item_append(item, new_item);
1145 if (!folder->inbox &&
1146 !strcmp(dir_name, INBOX_DIR)) {
1147 new_item->stype = F_INBOX;
1148 folder->inbox = new_item;
1149 } else if (!folder->outbox &&
1150 !strcmp(dir_name, OUTBOX_DIR)) {
1151 new_item->stype = F_OUTBOX;
1152 folder->outbox = new_item;
1153 } else if (!folder->draft &&
1154 !strcmp(dir_name, DRAFT_DIR)) {
1155 new_item->stype = F_DRAFT;
1156 folder->draft = new_item;
1157 } else if (!folder->queue &&
1158 !strcmp(dir_name, QUEUE_DIR)) {
1159 new_item->stype = F_QUEUE;
1160 folder->queue = new_item;
1161 } else if (!folder->trash &&
1162 !strcmp(dir_name, TRASH_DIR)) {
1163 new_item->stype = F_TRASH;
1164 folder->trash = new_item;
1168 mh_scan_tree_recursive(new_item);
1169 } else if (to_number(dir_name) > 0) n_msg++;
1182 mh_set_mtime(folder, item);
1185 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1187 FolderItem *item = node->data;
1188 gchar **paths = data;
1189 const gchar *oldpath = paths[0];
1190 const gchar *newpath = paths[1];
1192 gchar *new_itempath;
1195 oldpathlen = strlen(oldpath);
1196 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1197 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1201 base = item->path + oldpathlen;
1202 while (*base == G_DIR_SEPARATOR) base++;
1204 new_itempath = g_strdup(newpath);
1206 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1209 item->path = new_itempath;
1214 static gchar *mh_filename_from_utf8(const gchar *path)
1216 gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1219 g_warning("mh_filename_from_utf8: failed to convert character set\n");
1220 real_path = g_strdup(path);
1226 static gchar *mh_filename_to_utf8(const gchar *path)
1228 gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1230 g_warning("mh_filename_to_utf8: failed to convert character set\n");
1231 utf8path = g_strdup(path);
1237 static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1239 MsgInfo *msginfo_a = (MsgInfo *) a;
1240 MsgInfo *msginfo_b = (MsgInfo *) b;
1242 return (msginfo_a->msgnum - msginfo_b->msgnum);
1245 static gchar *get_unseen_seq_name(void)
1247 static gchar *seq_name = NULL;
1249 gchar buf[BUFFSIZE];
1251 gchar *profile_path = g_strconcat(
1252 get_home_dir(), G_DIR_SEPARATOR_S,
1253 ".mh_profile", NULL);
1254 FILE *fp = g_fopen(profile_path, "r");
1256 while (fgets(buf, sizeof(buf), fp) != NULL) {
1257 if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
1258 gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
1259 while (*seq_tmp == ' ')
1261 seq_name = g_strdup(seq_tmp);
1262 seq_name = strretchomp(seq_name);
1269 seq_name = g_strdup("unseen");
1270 tmp = g_strdup_printf("%s:", seq_name);
1277 static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
1279 gchar *mh_sequences_old, *mh_sequences_new;
1280 FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
1281 gchar buf[BUFFSIZE];
1283 gboolean err = FALSE;
1289 path = folder_item_get_path(item);
1291 mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
1292 ".mh_sequences", NULL);
1293 mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
1294 ".mh_sequences.new", NULL);
1295 if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
1296 GSList *msglist = folder_item_get_msg_list(item);
1298 MsgInfo *info = NULL;
1299 gint start = -1, end = -1;
1300 gchar *sequence = g_strdup("");
1302 msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
1305 /* write the unseen sequence if we don't have to scrap it */
1306 if (!remove_unseen) do {
1307 info = (MsgInfo *)(cur ? cur->data:NULL);
1308 if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
1310 start = end = info->msgnum;
1314 if (start > 0 && end > 0) {
1318 snprintf(tmp, 31, " %d-%d", start, end);
1320 snprintf(tmp, 31, " %d", start);
1322 tmp_len = strlen(tmp);
1323 sequence = g_realloc(sequence, seq_len+tmp_len+1);
1324 strcpy(sequence+seq_len, tmp);
1330 cur = cur ? cur->next:NULL;
1331 } while (cur || (start > 0 && end > 0));
1332 if (sequence && *sequence) {
1333 if (fprintf(mh_sequences_new_fp, "%s%s\n",
1334 get_unseen_seq_name(), sequence) < 0)
1337 debug_print("wrote unseen sequence: '%s%s'\n",
1338 get_unseen_seq_name(), sequence);
1340 /* rewrite the rest of the file */
1341 if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
1342 while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
1343 if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
1344 if (fprintf(mh_sequences_new_fp, "%s", buf) < 0) {
1349 fclose(mh_sequences_old_fp);
1352 fflush(mh_sequences_new_fp);
1354 fsync(fileno(mh_sequences_new_fp));
1356 if (fclose(mh_sequences_new_fp) == EOF)
1360 g_rename(mh_sequences_new, mh_sequences_old);
1362 procmsg_msg_list_free(msglist);
1364 g_free(mh_sequences_old);
1365 g_free(mh_sequences_new);
1371 static int mh_item_close(Folder *folder, FolderItem *item)
1373 time_t last_mtime = (time_t)0;
1374 gboolean need_scan = mh_scan_required(item->folder, item);
1375 last_mtime = item->mtime;
1377 mh_write_sequences(item, FALSE);
1379 if (item->mtime == last_mtime && !need_scan) {
1380 mh_set_mtime(folder, item);
1386 static void mh_set_mtime(Folder *folder, FolderItem *item)
1389 gchar *path = folder_item_get_path(item);
1391 cm_return_if_fail(path != NULL);
1393 if (g_stat(path, &s) < 0) {
1394 FILE_OP_ERROR(path, "stat");
1399 item->mtime = s.st_mtime;
1400 debug_print("MH: forced mtime of %s to %ld\n", item->name?item->name:"(null)", item->mtime);