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/>.
22 #include "claws-features.h"
28 #include <glib/gi18n.h>
39 #include "procheader.h"
42 #include "statusbar.h"
46 /* Define possible missing constants for Windows. */
59 static void mh_folder_init (Folder *folder,
63 static Folder *mh_folder_new (const gchar *name,
65 static void mh_folder_destroy (Folder *folder);
66 static gchar *mh_fetch_msg (Folder *folder,
69 static MsgInfo *mh_get_msginfo (Folder *folder,
72 static gint mh_add_msg (Folder *folder,
76 static gint mh_add_msgs (Folder *folder,
79 GHashTable *relation);
80 static gint mh_copy_msg (Folder *folder,
83 static gint mh_copy_msgs (Folder *folder,
86 GHashTable *relation);
87 static gint mh_remove_msg (Folder *folder,
90 static gint mh_remove_msgs (Folder *folder,
93 GHashTable *relation);
94 static gint mh_remove_all_msg (Folder *folder,
96 static gboolean mh_is_msg_changed (Folder *folder,
100 static gint mh_get_num_list (Folder *folder,
103 gboolean *old_uids_valid);
104 static gint mh_scan_tree (Folder *folder);
106 static gint mh_create_tree (Folder *folder);
107 static FolderItem *mh_create_folder (Folder *folder,
110 static gint mh_rename_folder (Folder *folder,
113 static gint mh_remove_folder (Folder *folder,
116 static gchar *mh_get_new_msg_filename (FolderItem *dest);
118 static MsgInfo *mh_parse_msg (const gchar *file,
120 static void mh_remove_missing_folder_items (Folder *folder);
121 static gchar *mh_filename_from_utf8 (const gchar *path);
122 static gchar *mh_filename_to_utf8 (const gchar *path);
123 static void mh_scan_tree_recursive (FolderItem *item);
125 static gboolean mh_rename_folder_func (GNode *node,
127 static gchar *mh_item_get_path (Folder *folder,
130 static gboolean mh_scan_required (Folder *folder,
132 static void mh_set_mtime (Folder *folder,
134 static int mh_item_close (Folder *folder,
137 static gint mh_get_flags (Folder *folder, FolderItem *item,
138 MsgInfoList *msginfo_list, GHashTable *msgflags);
140 static void mh_write_sequences (FolderItem *item, gboolean remove_unseen);
142 static FolderClass mh_class;
144 FolderClass *mh_get_class(void)
146 if (mh_class.idstr == NULL) {
147 mh_class.type = F_MH;
148 mh_class.idstr = "mh";
149 mh_class.uistr = "MH";
151 /* Folder functions */
152 mh_class.new_folder = mh_folder_new;
153 mh_class.destroy_folder = mh_folder_destroy;
154 mh_class.set_xml = folder_local_set_xml;
155 mh_class.get_xml = folder_local_get_xml;
156 mh_class.scan_tree = mh_scan_tree;
157 mh_class.create_tree = mh_create_tree;
159 /* FolderItem functions */
160 mh_class.item_get_path = mh_item_get_path;
161 mh_class.create_folder = mh_create_folder;
162 mh_class.rename_folder = mh_rename_folder;
163 mh_class.remove_folder = mh_remove_folder;
164 mh_class.get_num_list = mh_get_num_list;
165 mh_class.scan_required = mh_scan_required;
166 mh_class.set_mtime = mh_set_mtime;
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 cm_return_val_if_fail(path != NULL, FALSE);
216 if (g_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 static void mh_get_last_num(Folder *folder, FolderItem *item)
248 cm_return_if_fail(item != NULL);
250 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path?item->path:"(null)");
252 path = folder_item_get_path(item);
253 cm_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)) {
276 debug_print("Last number in dir %s = %d\n", item->path?item->path:"(null)", max);
277 item->last_num = max;
280 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
286 gint num, nummsgs = 0;
288 cm_return_val_if_fail(item != NULL, -1);
290 debug_print("mh_get_num_list(): Scanning %s ...\n", item->path?item->path:"(null)");
292 *old_uids_valid = TRUE;
294 path = folder_item_get_path(item);
295 cm_return_val_if_fail(path != NULL, -1);
296 if (change_dir(path) < 0) {
302 if ((dp = opendir(".")) == NULL) {
303 FILE_OP_ERROR(item->path, "opendir");
307 while ((d = readdir(dp)) != NULL) {
308 if ((num = to_number(d->d_name)) > 0) {
309 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
315 mh_set_mtime(folder, item);
319 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
324 cm_return_val_if_fail(item != NULL, NULL);
325 cm_return_val_if_fail(num > 0, NULL);
327 path = folder_item_get_path(item);
328 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
330 if (!is_file_exist(file)) {
339 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
344 cm_return_val_if_fail(item != NULL, NULL);
348 file = mh_fetch_msg(folder, item, num);
349 if (!file) return NULL;
351 msginfo = mh_parse_msg(file, item);
353 msginfo->msgnum = num;
360 static gchar *mh_get_new_msg_filename(FolderItem *dest)
365 destpath = folder_item_get_path(dest);
366 cm_return_val_if_fail(destpath != NULL, NULL);
368 if (!is_dir_exist(destpath))
369 make_dir_hier(destpath);
372 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
374 if (is_file_entry_exist(destfile)) {
386 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
390 MsgFileInfo fileinfo;
392 cm_return_val_if_fail(file != NULL, -1);
394 fileinfo.msginfo = NULL;
395 fileinfo.file = (gchar *)file;
396 fileinfo.flags = flags;
397 file_list.data = &fileinfo;
398 file_list.next = NULL;
400 ret = mh_add_msgs(folder, dest, &file_list, NULL);
404 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
405 GHashTable *relation)
409 MsgFileInfo *fileinfo;
411 cm_return_val_if_fail(dest != NULL, -1);
412 cm_return_val_if_fail(file_list != NULL, -1);
414 if (dest->last_num < 0) {
415 mh_get_last_num(folder, dest);
416 if (dest->last_num < 0) return -1;
419 for (cur = file_list; cur != NULL; cur = cur->next) {
420 fileinfo = (MsgFileInfo *)cur->data;
422 destfile = mh_get_new_msg_filename(dest);
423 if (destfile == NULL) return -1;
426 if (link(fileinfo->file, destfile) < 0) {
428 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
429 g_warning(_("can't copy message %s to %s\n"),
430 fileinfo->file, destfile);
438 if (relation != NULL)
439 g_hash_table_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
443 mh_write_sequences(dest, TRUE);
444 return dest->last_num;
447 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
451 cm_return_val_if_fail(msginfo != NULL, -1);
453 msglist.data = msginfo;
456 return mh_copy_msgs(folder, dest, &msglist, NULL);
459 static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist,
460 GHashTable *relation)
462 gboolean dest_need_scan = FALSE;
463 gboolean src_need_scan = FALSE;
464 FolderItem *src = NULL;
468 FolderItemPrefs *prefs;
469 MsgInfo *msginfo = NULL;
470 MsgInfoList *cur = NULL;
471 gint curnum = 0, total = 0;
472 gchar *srcpath = NULL;
473 gboolean full_fetch = FALSE;
474 time_t last_dest_mtime = (time_t)0;
475 time_t last_src_mtime = (time_t)0;
477 cm_return_val_if_fail(dest != NULL, -1);
478 cm_return_val_if_fail(msglist != NULL, -1);
480 msginfo = (MsgInfo *)msglist->data;
482 cm_return_val_if_fail(msginfo != NULL, -1);
484 if (msginfo->folder == dest) {
485 g_warning("the src folder is identical to the dest.\n");
489 if (msginfo->folder->folder != dest->folder)
492 if (FOLDER_TYPE(msginfo->folder->folder) == F_MH) {
493 src = msginfo->folder;
496 if (dest->last_num < 0) {
497 mh_get_last_num(folder, dest);
498 if (dest->last_num < 0) return -1;
503 srcpath = folder_item_get_path(msginfo->folder);
505 dest_need_scan = mh_scan_required(dest->folder, dest);
506 last_dest_mtime = dest->mtime;
509 src_need_scan = mh_scan_required(src->folder, src);
510 last_src_mtime = src->mtime;
513 total = g_slist_length(msglist);
515 if (MSG_IS_MOVE(msginfo->flags))
516 statusbar_print_all(_("Moving messages..."));
518 statusbar_print_all(_("Copying messages..."));
520 for (cur = msglist; cur; cur = cur->next) {
521 msginfo = (MsgInfo *)cur->data;
523 goto err_reset_status;
526 srcfile = g_strconcat(srcpath,
528 itos(msginfo->msgnum), NULL);
530 srcfile = procmsg_get_message_file(msginfo);
533 goto err_reset_status;
535 destfile = mh_get_new_msg_filename(dest);
538 goto err_reset_status;
542 statusbar_progress_all(curnum, total, 100);
543 if (curnum % 100 == 0)
548 debug_print("Copying message %s%c%d to %s ...\n",
549 msginfo->folder->path, G_DIR_SEPARATOR,
550 msginfo->msgnum, dest->path);
553 if (MSG_IS_MOVE(msginfo->flags)) {
554 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
555 if (move_file(srcfile, destfile, TRUE) < 0) {
556 FILE_OP_ERROR(srcfile, "move");
557 if (copy_file(srcfile, destfile, TRUE) < 0) {
558 FILE_OP_ERROR(srcfile, "copy");
561 goto err_reset_status;
564 /* say unlinking's not necessary */
565 msginfo->flags.tmp_flags |= MSG_MOVE_DONE;
567 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
568 FILE_OP_ERROR(srcfile, "copy");
571 goto err_reset_status;
573 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
574 if (chmod(destfile, prefs->folder_chmod) < 0)
575 FILE_OP_ERROR(destfile, "chmod");
578 filemode = prefs->folder_chmod;
579 if (filemode & S_IRGRP) filemode |= S_IWGRP;
580 if (filemode & S_IROTH) filemode |= S_IWOTH;
583 if (g_hash_table_lookup(relation, msginfo) != NULL)
584 g_warning("already in : %p", msginfo);
586 g_hash_table_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
594 mh_write_sequences(dest, TRUE);
596 if (dest->mtime == last_dest_mtime && !dest_need_scan) {
597 mh_set_mtime(folder, dest);
600 if (src && src->mtime == last_src_mtime && !src_need_scan) {
601 mh_set_mtime(folder, src);
605 statusbar_progress_all(0,0,0);
608 return dest->last_num;
611 mh_write_sequences(dest, TRUE);
613 statusbar_progress_all(0,0,0);
620 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
622 gboolean need_scan = FALSE;
623 time_t last_mtime = (time_t)0;
626 cm_return_val_if_fail(item != NULL, -1);
628 file = mh_fetch_msg(folder, item, num);
629 cm_return_val_if_fail(file != NULL, -1);
631 need_scan = mh_scan_required(folder, item);
632 last_mtime = item->mtime;
634 if (claws_unlink(file) < 0) {
635 FILE_OP_ERROR(file, "unlink");
640 if (item->mtime == last_mtime && !need_scan) {
641 mh_set_mtime(folder, item);
647 static gint mh_remove_msgs(Folder *folder, FolderItem *item,
648 MsgInfoList *msglist, GHashTable *relation)
650 gboolean need_scan = FALSE;
652 time_t last_mtime = (time_t)0;
654 gint total = 0, curnum = 0;
656 cm_return_val_if_fail(item != NULL, -1);
658 path = folder_item_get_path(item);
660 need_scan = mh_scan_required(folder, item);
661 last_mtime = item->mtime;
663 total = g_slist_length(msglist);
665 statusbar_print_all(_("Deleting messages..."));
668 for (cur = msglist; cur; cur = cur->next) {
669 MsgInfo *msginfo = (MsgInfo *)cur->data;
672 if (MSG_IS_MOVE(msginfo->flags) && MSG_IS_MOVE_DONE(msginfo->flags)) {
673 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
677 statusbar_progress_all(curnum, total, 100);
678 if (curnum % 100 == 0)
683 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
687 if (claws_unlink(file) < 0) {
696 statusbar_progress_all(0,0,0);
699 if (item->mtime == last_mtime && !need_scan) {
700 mh_set_mtime(folder, item);
707 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
712 cm_return_val_if_fail(item != NULL, -1);
714 path = folder_item_get_path(item);
715 cm_return_val_if_fail(path != NULL, -1);
716 val = remove_all_numbered_files(path);
719 mh_write_sequences(item, TRUE);
724 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
729 if (g_stat(itos(msginfo->msgnum), &s) < 0 ||
730 msginfo->size != s.st_size || (
731 (msginfo->mtime - s.st_mtime != 0) &&
732 (msginfo->mtime - s.st_mtime != 3600) &&
733 (msginfo->mtime - s.st_mtime != -3600)))
739 static gint mh_scan_tree(Folder *folder)
744 cm_return_val_if_fail(folder != NULL, -1);
747 item = folder_item_new(folder, folder->name, NULL);
748 item->folder = folder;
749 folder->node = item->node = g_node_new(item);
751 item = FOLDER_ITEM(folder->node->data);
753 rootpath = folder_item_get_path(item);
754 if (change_dir(rootpath) < 0) {
760 mh_create_tree(folder);
761 mh_remove_missing_folder_items(folder);
762 mh_scan_tree_recursive(item);
767 #define MAKE_DIR_IF_NOT_EXIST(dir) \
769 if (!is_dir_exist(dir)) { \
770 if (is_file_exist(dir)) { \
771 g_warning("File `%s' already exists.\n" \
772 "Can't create folder.", dir); \
775 if (make_dir_hier(dir) < 0) \
780 static gint mh_create_tree(Folder *folder)
784 cm_return_val_if_fail(folder != NULL, -1);
786 CHDIR_RETURN_VAL_IF_FAIL(get_mail_base_dir(), -1);
787 rootpath = LOCAL_FOLDER(folder)->rootpath;
788 MAKE_DIR_IF_NOT_EXIST(rootpath);
789 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
791 /* Create special directories as needed */
792 if (folder->inbox != NULL &&
793 folder->inbox->path != NULL)
794 f = folder->inbox->path;
797 MAKE_DIR_IF_NOT_EXIST(f);
799 if (folder->outbox != NULL &&
800 folder->outbox->path != NULL)
801 f = folder->outbox->path;
804 MAKE_DIR_IF_NOT_EXIST(f);
806 if (folder->draft != NULL &&
807 folder->draft->path != NULL)
808 f = folder->draft->path;
811 MAKE_DIR_IF_NOT_EXIST(f);
813 if (folder->queue != NULL &&
814 folder->queue->path != NULL)
815 f = folder->queue->path;
818 MAKE_DIR_IF_NOT_EXIST(f);
820 if (folder->trash != NULL &&
821 folder->trash->path != NULL)
822 f = folder->trash->path;
825 MAKE_DIR_IF_NOT_EXIST(f);
830 #undef MAKE_DIR_IF_NOT_EXIST
832 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
834 gchar *folder_path, *path;
836 cm_return_val_if_fail(folder != NULL, NULL);
837 cm_return_val_if_fail(item != NULL, NULL);
839 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
840 cm_return_val_if_fail(folder_path != NULL, NULL);
842 /* FIXME: [W32] The code below does not correctly merge
843 relative filenames; there should be a function to handle
845 if ( !is_relative_filename (folder_path) ) {
847 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
850 path = g_strdup(folder_path);
853 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
854 folder_path, G_DIR_SEPARATOR_S,
857 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
861 real_path = mh_filename_from_utf8(path);
862 if (!is_dir_exist(real_path) && is_dir_exist(path)) {
863 /* mmh, older version did put utf8 filenames instead of
864 * the correct encoding */
865 g_rename(path, real_path);
866 folder_item_scan(item);
873 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
876 gchar *path, *real_name;
878 FolderItem *new_item;
879 gchar *mh_sequences_filename;
880 FILE *mh_sequences_file;
882 cm_return_val_if_fail(folder != NULL, NULL);
883 cm_return_val_if_fail(parent != NULL, NULL);
884 cm_return_val_if_fail(name != NULL, NULL);
886 path = folder_item_get_path(parent);
887 if (!is_dir_exist(path))
888 if (make_dir_hier(path) != 0)
891 real_name = mh_filename_from_utf8(name);
892 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
896 if (make_dir(fullpath) < 0) {
904 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
907 path = g_strdup(name);
908 new_item = folder_item_new(folder, name, path);
909 folder_item_append(parent, new_item);
913 path = folder_item_get_path(new_item);
914 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
915 ".mh_sequences", NULL);
916 if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
917 fclose(mh_sequences_file);
919 g_free(mh_sequences_filename);
925 static gint mh_rename_folder(Folder *folder, FolderItem *item,
931 gchar *newpath, *utf8newpath;
934 cm_return_val_if_fail(folder != NULL, -1);
935 cm_return_val_if_fail(item != NULL, -1);
936 cm_return_val_if_fail(item->path != NULL, -1);
937 cm_return_val_if_fail(name != NULL, -1);
939 oldpath = folder_item_get_path(item);
940 if (!is_dir_exist(oldpath))
941 make_dir_hier(oldpath);
943 dirname = g_path_get_dirname(oldpath);
944 real_name = mh_filename_from_utf8(name);
945 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
948 if (g_rename(oldpath, newpath) < 0) {
949 FILE_OP_ERROR(oldpath, "rename");
958 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
959 dirname = g_path_get_dirname(item->path);
960 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
964 utf8newpath = g_strdup(name);
967 item->name = g_strdup(name);
969 paths[0] = g_strdup(item->path);
970 paths[1] = utf8newpath;
971 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
972 mh_rename_folder_func, paths);
979 static gint mh_remove_folder(Folder *folder, FolderItem *item)
983 cm_return_val_if_fail(folder != NULL, -1);
984 cm_return_val_if_fail(item != NULL, -1);
985 cm_return_val_if_fail(item->path != NULL, -1);
987 path = folder_item_get_path(item);
988 if (remove_dir_recursive(path) < 0) {
989 g_warning("can't remove directory `%s'\n", path);
995 folder_item_remove(item);
999 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1004 cm_return_val_if_fail(item != NULL, NULL);
1005 cm_return_val_if_fail(file != NULL, NULL);
1007 flags.perm_flags = MSG_NEW|MSG_UNREAD;
1008 flags.tmp_flags = 0;
1010 if (folder_has_parent_of_type(item, F_QUEUE)) {
1011 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1012 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
1013 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1016 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1017 if (!msginfo) return NULL;
1019 msginfo->msgnum = atoi(file);
1020 msginfo->folder = item;
1025 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
1030 cm_return_val_if_fail(node->data != NULL, FALSE);
1032 if (G_NODE_IS_ROOT(node))
1035 item = FOLDER_ITEM(node->data);
1037 path = folder_item_get_path(item);
1038 if (!is_dir_exist(path)) {
1039 debug_print("folder '%s' not found. removing...\n", path?path:"(null)");
1040 folder_item_remove(item);
1047 static void mh_remove_missing_folder_items(Folder *folder)
1049 cm_return_if_fail(folder != NULL);
1051 debug_print("searching missing folders...\n");
1053 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1054 mh_remove_missing_folder_items_func, folder);
1057 static void mh_scan_tree_recursive(FolderItem *item)
1066 const gchar *dir_name;
1068 gchar *real_path, *entry, *utf8entry, *utf8name;
1071 cm_return_if_fail(item != NULL);
1072 cm_return_if_fail(item->folder != NULL);
1074 folder = item->folder;
1076 real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
1078 dir = g_dir_open(real_path, 0, NULL);
1080 g_warning("failed to open directory: %s\n", real_path);
1085 dp = opendir(real_path);
1087 FILE_OP_ERROR(real_path, "opendir");
1093 debug_print("scanning %s ...\n",
1094 item->path ? item->path
1095 : LOCAL_FOLDER(item->folder)->rootpath);
1096 if (folder->ui_func)
1097 folder->ui_func(folder, item, folder->ui_func_data);
1100 while ((dir_name = g_dir_read_name(dir)) != NULL) {
1102 while ((d = readdir(dp)) != NULL) {
1103 dir_name = d->d_name;
1105 if (dir_name[0] == '.') continue;
1107 utf8name = mh_filename_to_utf8(dir_name);
1109 utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1112 utf8entry = g_strdup(utf8name);
1113 entry = mh_filename_from_utf8(utf8entry);
1116 #if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
1117 d->d_type == DT_DIR ||
1118 (d->d_type == DT_UNKNOWN &&
1120 g_stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
1121 #if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
1125 FolderItem *new_item = NULL;
1129 for (node = node->children; node != NULL; node = node->next) {
1130 FolderItem *cur_item = FOLDER_ITEM(node->data);
1131 gchar *curpath = mh_filename_from_utf8(cur_item->path);
1132 if (!strcmp2(curpath, entry)) {
1133 new_item = cur_item;
1140 debug_print("new folder '%s' found.\n", entry);
1141 new_item = folder_item_new(folder, utf8name, utf8entry);
1142 folder_item_append(item, new_item);
1146 if (!folder->inbox &&
1147 !strcmp(dir_name, INBOX_DIR)) {
1148 new_item->stype = F_INBOX;
1149 folder->inbox = new_item;
1150 } else if (!folder->outbox &&
1151 !strcmp(dir_name, OUTBOX_DIR)) {
1152 new_item->stype = F_OUTBOX;
1153 folder->outbox = new_item;
1154 } else if (!folder->draft &&
1155 !strcmp(dir_name, DRAFT_DIR)) {
1156 new_item->stype = F_DRAFT;
1157 folder->draft = new_item;
1158 } else if (!folder->queue &&
1159 !strcmp(dir_name, QUEUE_DIR)) {
1160 new_item->stype = F_QUEUE;
1161 folder->queue = new_item;
1162 } else if (!folder->trash &&
1163 !strcmp(dir_name, TRASH_DIR)) {
1164 new_item->stype = F_TRASH;
1165 folder->trash = new_item;
1169 mh_scan_tree_recursive(new_item);
1170 } else if (to_number(dir_name) > 0) n_msg++;
1183 mh_set_mtime(folder, item);
1186 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1188 FolderItem *item = node->data;
1189 gchar **paths = data;
1190 const gchar *oldpath = paths[0];
1191 const gchar *newpath = paths[1];
1193 gchar *new_itempath;
1196 oldpathlen = strlen(oldpath);
1197 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1198 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1202 base = item->path + oldpathlen;
1203 while (*base == G_DIR_SEPARATOR) base++;
1205 new_itempath = g_strdup(newpath);
1207 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1210 item->path = new_itempath;
1215 static gchar *mh_filename_from_utf8(const gchar *path)
1217 gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1220 g_warning("mh_filename_from_utf8: failed to convert character set\n");
1221 real_path = g_strdup(path);
1227 static gchar *mh_filename_to_utf8(const gchar *path)
1229 gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1231 g_warning("mh_filename_to_utf8: failed to convert character set\n");
1232 utf8path = g_strdup(path);
1238 static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1240 MsgInfo *msginfo_a = (MsgInfo *) a;
1241 MsgInfo *msginfo_b = (MsgInfo *) b;
1243 return (msginfo_a->msgnum - msginfo_b->msgnum);
1246 static gchar *get_unseen_seq_name(void)
1248 static gchar *seq_name = NULL;
1250 gchar buf[BUFFSIZE];
1252 gchar *profile_path = g_strconcat(
1253 get_home_dir(), G_DIR_SEPARATOR_S,
1254 ".mh_profile", NULL);
1255 FILE *fp = g_fopen(profile_path, "r");
1257 while (fgets(buf, sizeof(buf), fp) != NULL) {
1258 if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
1259 gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
1260 while (*seq_tmp == ' ')
1262 seq_name = g_strdup(seq_tmp);
1263 seq_name = strretchomp(seq_name);
1270 seq_name = g_strdup("unseen");
1271 tmp = g_strdup_printf("%s:", seq_name);
1278 static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
1280 gchar *mh_sequences_old, *mh_sequences_new;
1281 FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
1282 gchar buf[BUFFSIZE];
1284 gboolean err = FALSE;
1290 path = folder_item_get_path(item);
1292 mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
1293 ".mh_sequences", NULL);
1294 mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
1295 ".mh_sequences.new", NULL);
1296 if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
1297 GSList *msglist = folder_item_get_msg_list(item);
1299 MsgInfo *info = NULL;
1300 gint start = -1, end = -1;
1301 gchar *sequence = g_strdup("");
1303 msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
1306 /* write the unseen sequence if we don't have to scrap it */
1307 if (!remove_unseen) do {
1308 info = (MsgInfo *)(cur ? cur->data:NULL);
1309 if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
1311 start = end = info->msgnum;
1315 if (start > 0 && end > 0) {
1319 snprintf(tmp, 31, " %d-%d", start, end);
1321 snprintf(tmp, 31, " %d", start);
1323 tmp_len = strlen(tmp);
1324 sequence = g_realloc(sequence, seq_len+tmp_len+1);
1325 strcpy(sequence+seq_len, tmp);
1331 cur = cur ? cur->next:NULL;
1332 } while (cur || (start > 0 && end > 0));
1333 if (sequence && *sequence) {
1334 if (fprintf(mh_sequences_new_fp, "%s%s\n",
1335 get_unseen_seq_name(), sequence) < 0)
1338 debug_print("wrote unseen sequence: '%s%s'\n",
1339 get_unseen_seq_name(), sequence);
1341 /* rewrite the rest of the file */
1342 if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
1343 while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
1344 if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
1345 if (fprintf(mh_sequences_new_fp, "%s", buf) < 0) {
1350 fclose(mh_sequences_old_fp);
1353 fflush(mh_sequences_new_fp);
1355 fsync(fileno(mh_sequences_new_fp));
1357 if (fclose(mh_sequences_new_fp) == EOF)
1361 g_rename(mh_sequences_new, mh_sequences_old);
1363 procmsg_msg_list_free(msglist);
1365 g_free(mh_sequences_old);
1366 g_free(mh_sequences_new);
1372 static int mh_item_close(Folder *folder, FolderItem *item)
1374 time_t last_mtime = (time_t)0;
1375 gboolean need_scan = mh_scan_required(item->folder, item);
1376 last_mtime = item->mtime;
1378 mh_write_sequences(item, FALSE);
1380 if (item->mtime == last_mtime && !need_scan) {
1381 mh_set_mtime(folder, item);
1387 static void mh_set_mtime(Folder *folder, FolderItem *item)
1390 gchar *path = folder_item_get_path(item);
1392 cm_return_if_fail(path != NULL);
1394 if (g_stat(path, &s) < 0) {
1395 FILE_OP_ERROR(path, "stat");
1400 item->mtime = s.st_mtime;
1401 debug_print("MH: forced mtime of %s to %ld\n", item->name?item->name:"(null)", item->mtime);