2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2013 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>
37 #include "folder_item_prefs.h"
40 #include "procheader.h"
43 #include "statusbar.h"
48 /* Define possible missing constants for Windows. */
61 static void mh_folder_init (Folder *folder,
65 static Folder *mh_folder_new (const gchar *name,
67 static void mh_folder_destroy (Folder *folder);
68 static gchar *mh_fetch_msg (Folder *folder,
71 static MsgInfo *mh_get_msginfo (Folder *folder,
74 static gint mh_add_msg (Folder *folder,
78 static gint mh_add_msgs (Folder *folder,
81 GHashTable *relation);
82 static gint mh_copy_msg (Folder *folder,
85 static gint mh_copy_msgs (Folder *folder,
88 GHashTable *relation);
89 static gint mh_remove_msg (Folder *folder,
92 static gint mh_remove_msgs (Folder *folder,
95 GHashTable *relation);
96 static gint mh_remove_all_msg (Folder *folder,
98 static gboolean mh_is_msg_changed (Folder *folder,
102 static gint mh_get_num_list (Folder *folder,
105 gboolean *old_uids_valid);
106 static gint mh_scan_tree (Folder *folder);
108 static gint mh_create_tree (Folder *folder);
109 static FolderItem *mh_create_folder (Folder *folder,
112 static gint mh_rename_folder (Folder *folder,
115 static gint mh_remove_folder (Folder *folder,
118 static gchar *mh_get_new_msg_filename (FolderItem *dest);
120 static MsgInfo *mh_parse_msg (const gchar *file,
122 static void mh_remove_missing_folder_items (Folder *folder);
123 static gchar *mh_filename_from_utf8 (const gchar *path);
124 static gchar *mh_filename_to_utf8 (const gchar *path);
125 static void mh_scan_tree_recursive (FolderItem *item);
127 static gboolean mh_rename_folder_func (GNode *node,
129 static gchar *mh_item_get_path (Folder *folder,
132 static gboolean mh_scan_required (Folder *folder,
134 static void mh_set_mtime (Folder *folder,
136 static int mh_item_close (Folder *folder,
139 static gint mh_get_flags (Folder *folder, FolderItem *item,
140 MsgInfoList *msginfo_list, GHashTable *msgflags);
142 static void mh_write_sequences (FolderItem *item, gboolean remove_unseen);
144 static FolderClass mh_class;
146 FolderClass *mh_get_class(void)
148 if (mh_class.idstr == NULL) {
149 mh_class.type = F_MH;
150 mh_class.idstr = "mh";
151 mh_class.uistr = "MH";
152 mh_class.supports_server_search = FALSE;
154 /* Folder functions */
155 mh_class.new_folder = mh_folder_new;
156 mh_class.destroy_folder = mh_folder_destroy;
157 mh_class.set_xml = folder_local_set_xml;
158 mh_class.get_xml = folder_local_get_xml;
159 mh_class.scan_tree = mh_scan_tree;
160 mh_class.create_tree = mh_create_tree;
162 /* FolderItem functions */
163 mh_class.item_get_path = mh_item_get_path;
164 mh_class.create_folder = mh_create_folder;
165 mh_class.rename_folder = mh_rename_folder;
166 mh_class.remove_folder = mh_remove_folder;
167 mh_class.get_num_list = mh_get_num_list;
168 mh_class.scan_required = mh_scan_required;
169 mh_class.set_mtime = mh_set_mtime;
170 mh_class.close = mh_item_close;
171 mh_class.get_flags = NULL; /*mh_get_flags */;
173 /* Message functions */
174 mh_class.get_msginfo = mh_get_msginfo;
175 mh_class.fetch_msg = mh_fetch_msg;
176 mh_class.add_msg = mh_add_msg;
177 mh_class.add_msgs = mh_add_msgs;
178 mh_class.copy_msg = mh_copy_msg;
179 mh_class.copy_msgs = mh_copy_msgs;
180 mh_class.search_msgs = folder_item_search_msgs_local;
181 mh_class.remove_msg = mh_remove_msg;
182 mh_class.remove_msgs = mh_remove_msgs;
183 mh_class.remove_all_msg = mh_remove_all_msg;
184 mh_class.is_msg_changed = mh_is_msg_changed;
190 static Folder *mh_folder_new(const gchar *name, const gchar *path)
194 folder = (Folder *)g_new0(MHFolder, 1);
195 folder->klass = &mh_class;
196 mh_folder_init(folder, name, path);
201 static void mh_folder_destroy(Folder *folder)
203 folder_local_folder_destroy(LOCAL_FOLDER(folder));
206 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
208 folder_local_folder_init(folder, name, path);
212 gboolean mh_scan_required(Folder *folder, FolderItem *item)
217 path = folder_item_get_path(item);
218 cm_return_val_if_fail(path != NULL, FALSE);
220 if (g_stat(path, &s) < 0) {
221 FILE_OP_ERROR(path, "stat");
226 if ((s.st_mtime > item->mtime) &&
227 (s.st_mtime - 3600 != item->mtime)) {
228 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
230 (long int) s.st_mtime,
231 (long int) item->mtime);
236 debug_print("MH scan not required: %s (%ld <= %ld)\n",
238 (long int) s.st_mtime,
239 (long int) item->mtime);
244 static void mh_get_last_num(Folder *folder, FolderItem *item)
246 gchar *path, *fullpath;
249 GError *error = NULL;
253 cm_return_if_fail(item != NULL);
255 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path?item->path:"(null)");
257 path = folder_item_get_path(item);
258 cm_return_if_fail(path != NULL);
260 if ((dp = g_dir_open(path, 0, &error)) == NULL) {
261 g_warning("Couldn't open directory '%s': %s (%d)",
262 path, error->message, error->code);
268 while ((d = g_dir_read_name(dp)) != NULL) {
269 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, d, NULL);
270 if ((num = to_number(d)) > 0 &&
271 g_file_test(fullpath, G_FILE_TEST_IS_REGULAR)) {
283 debug_print("Last number in dir %s = %d\n", item->path?item->path:"(null)", max);
284 item->last_num = max;
287 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
293 GError *error = NULL;
294 gint num, nummsgs = 0;
296 cm_return_val_if_fail(item != NULL, -1);
298 debug_print("mh_get_num_list(): Scanning %s ...\n", item->path?item->path:"(null)");
300 *old_uids_valid = TRUE;
302 path = folder_item_get_path(item);
303 cm_return_val_if_fail(path != NULL, -1);
305 if ((dp = g_dir_open(path, 0, &error)) == NULL) {
306 g_message("Couldn't open current directory: %s (%d).\n",
307 error->message, error->code);
314 while ((d = g_dir_read_name(dp)) != NULL) {
315 if ((num = to_number(d)) > 0) {
316 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
322 mh_set_mtime(folder, item);
326 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
331 cm_return_val_if_fail(item != NULL, NULL);
332 cm_return_val_if_fail(num > 0, NULL);
334 path = folder_item_get_path(item);
335 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
337 if (!is_file_exist(file)) {
346 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
351 cm_return_val_if_fail(item != NULL, NULL);
355 file = mh_fetch_msg(folder, item, num);
356 if (!file) return NULL;
358 msginfo = mh_parse_msg(file, item);
360 msginfo->msgnum = num;
367 static gchar *mh_get_new_msg_filename(FolderItem *dest)
372 destpath = folder_item_get_path(dest);
373 cm_return_val_if_fail(destpath != NULL, NULL);
375 if (dest->last_num < 0) {
376 mh_get_last_num(dest->folder, dest);
377 if (dest->last_num < 0) return NULL;
380 if (!is_dir_exist(destpath))
381 make_dir_hier(destpath);
384 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
386 if (is_file_entry_exist(destfile)) {
398 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
402 MsgFileInfo fileinfo;
404 cm_return_val_if_fail(file != NULL, -1);
406 fileinfo.msginfo = NULL;
407 fileinfo.file = (gchar *)file;
408 fileinfo.flags = flags;
409 file_list.data = &fileinfo;
410 file_list.next = NULL;
412 ret = mh_add_msgs(folder, dest, &file_list, NULL);
416 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
417 GHashTable *relation)
421 MsgFileInfo *fileinfo;
423 cm_return_val_if_fail(dest != NULL, -1);
424 cm_return_val_if_fail(file_list != NULL, -1);
426 if (dest->last_num < 0) {
427 mh_get_last_num(folder, dest);
428 if (dest->last_num < 0) return -1;
431 for (cur = file_list; cur != NULL; cur = cur->next) {
432 fileinfo = (MsgFileInfo *)cur->data;
434 destfile = mh_get_new_msg_filename(dest);
435 if (destfile == NULL) return -1;
438 if (link(fileinfo->file, destfile) < 0) {
440 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
441 g_warning("can't copy message %s to %s",
442 fileinfo->file, destfile);
450 if (relation != NULL)
451 g_hash_table_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
455 mh_write_sequences(dest, TRUE);
456 return dest->last_num;
459 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
463 cm_return_val_if_fail(msginfo != NULL, -1);
465 msglist.data = msginfo;
468 return mh_copy_msgs(folder, dest, &msglist, NULL);
471 static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist,
472 GHashTable *relation)
474 gboolean dest_need_scan = FALSE;
475 gboolean src_need_scan = FALSE;
476 FolderItem *src = NULL;
479 FolderItemPrefs *prefs;
480 MsgInfo *msginfo = NULL;
481 MsgInfoList *cur = NULL;
482 gint curnum = 0, total = 0;
483 gchar *srcpath = NULL;
484 gboolean full_fetch = FALSE;
485 time_t last_dest_mtime = (time_t)0;
486 time_t last_src_mtime = (time_t)0;
488 cm_return_val_if_fail(dest != NULL, -1);
489 cm_return_val_if_fail(msglist != NULL, -1);
491 msginfo = (MsgInfo *)msglist->data;
493 cm_return_val_if_fail(msginfo != NULL, -1);
495 if (msginfo->folder == dest) {
496 g_warning("the src folder is identical to the dest.");
500 if (msginfo->folder->folder != dest->folder)
503 if (FOLDER_TYPE(msginfo->folder->folder) == F_MH) {
504 src = msginfo->folder;
507 if (dest->last_num < 0) {
508 mh_get_last_num(folder, dest);
509 if (dest->last_num < 0) return -1;
514 srcpath = folder_item_get_path(msginfo->folder);
516 dest_need_scan = mh_scan_required(dest->folder, dest);
517 last_dest_mtime = dest->mtime;
520 src_need_scan = mh_scan_required(src->folder, src);
521 last_src_mtime = src->mtime;
524 total = g_slist_length(msglist);
526 if (MSG_IS_MOVE(msginfo->flags))
527 statusbar_print_all(_("Moving messages..."));
529 statusbar_print_all(_("Copying messages..."));
531 for (cur = msglist; cur; cur = cur->next) {
532 msginfo = (MsgInfo *)cur->data;
534 goto err_reset_status;
537 srcfile = g_strconcat(srcpath,
539 itos(msginfo->msgnum), NULL);
541 srcfile = procmsg_get_message_file(msginfo);
544 goto err_reset_status;
546 destfile = mh_get_new_msg_filename(dest);
549 goto err_reset_status;
553 statusbar_progress_all(curnum, total, 100);
554 if (curnum % 100 == 0)
559 debug_print("Copying message %s%c%d to %s ...\n",
560 msginfo->folder->path, G_DIR_SEPARATOR,
561 msginfo->msgnum, dest->path);
564 if (MSG_IS_MOVE(msginfo->flags)) {
565 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
566 if (move_file(srcfile, destfile, TRUE) < 0) {
567 FILE_OP_ERROR(srcfile, "move");
568 if (copy_file(srcfile, destfile, TRUE) < 0) {
569 FILE_OP_ERROR(srcfile, "copy");
572 goto err_reset_status;
575 /* say unlinking's not necessary */
576 msginfo->flags.tmp_flags |= MSG_MOVE_DONE;
578 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
579 FILE_OP_ERROR(srcfile, "copy");
582 goto err_reset_status;
584 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
585 if (chmod(destfile, prefs->folder_chmod) < 0)
586 FILE_OP_ERROR(destfile, "chmod");
589 if (g_hash_table_lookup(relation, msginfo) != NULL)
590 g_warning("already in: %p", msginfo);
592 g_hash_table_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
600 mh_write_sequences(dest, TRUE);
602 if (dest->mtime == last_dest_mtime && !dest_need_scan) {
603 mh_set_mtime(folder, dest);
606 if (src && src->mtime == last_src_mtime && !src_need_scan) {
607 mh_set_mtime(folder, src);
611 statusbar_progress_all(0,0,0);
614 return dest->last_num;
617 mh_write_sequences(dest, TRUE);
619 statusbar_progress_all(0,0,0);
626 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
628 gboolean need_scan = FALSE;
629 time_t last_mtime = (time_t)0;
632 cm_return_val_if_fail(item != NULL, -1);
634 file = mh_fetch_msg(folder, item, num);
635 cm_return_val_if_fail(file != NULL, -1);
637 need_scan = mh_scan_required(folder, item);
638 last_mtime = item->mtime;
640 if (claws_unlink(file) < 0) {
641 FILE_OP_ERROR(file, "unlink");
646 if (item->mtime == last_mtime && !need_scan) {
647 mh_set_mtime(folder, item);
653 static gint mh_remove_msgs(Folder *folder, FolderItem *item,
654 MsgInfoList *msglist, GHashTable *relation)
656 gboolean need_scan = FALSE;
658 time_t last_mtime = (time_t)0;
660 gint total = 0, curnum = 0;
662 cm_return_val_if_fail(item != NULL, -1);
664 path = folder_item_get_path(item);
666 need_scan = mh_scan_required(folder, item);
667 last_mtime = item->mtime;
669 total = g_slist_length(msglist);
671 statusbar_print_all(_("Deleting messages..."));
674 for (cur = msglist; cur; cur = cur->next) {
675 MsgInfo *msginfo = (MsgInfo *)cur->data;
678 if (MSG_IS_MOVE(msginfo->flags) && MSG_IS_MOVE_DONE(msginfo->flags)) {
679 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
683 statusbar_progress_all(curnum, total, 100);
684 if (curnum % 100 == 0)
689 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
693 if (claws_unlink(file) < 0) {
702 statusbar_progress_all(0,0,0);
705 if (item->mtime == last_mtime && !need_scan) {
706 mh_set_mtime(folder, item);
713 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
718 cm_return_val_if_fail(item != NULL, -1);
720 path = folder_item_get_path(item);
721 cm_return_val_if_fail(path != NULL, -1);
722 val = remove_all_numbered_files(path);
725 mh_write_sequences(item, TRUE);
730 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
737 parent_path = folder_item_get_path(item);
738 path = g_strdup_printf("%s%c%d", parent_path,
739 G_DIR_SEPARATOR, msginfo->msgnum);
741 if (g_stat((path), &s) < 0 ||
742 msginfo->size != s.st_size || (
743 (msginfo->mtime - s.st_mtime != 0) &&
744 (msginfo->mtime - s.st_mtime != 3600) &&
745 (msginfo->mtime - s.st_mtime != -3600))) {
754 static gint mh_scan_tree(Folder *folder)
758 cm_return_val_if_fail(folder != NULL, -1);
761 item = folder_item_new(folder, folder->name, NULL);
762 item->folder = folder;
763 folder->node = item->node = g_node_new(item);
765 item = FOLDER_ITEM(folder->node->data);
767 mh_create_tree(folder);
768 mh_remove_missing_folder_items(folder);
769 mh_scan_tree_recursive(item);
774 #define MAKE_DIR_IF_NOT_EXIST(dir) \
776 if (!is_dir_exist(dir)) { \
777 if (is_file_exist(dir)) { \
778 g_warning("File '%s' already exists. " \
779 "Can't create folder.", dir); \
782 if (make_dir_hier(dir) < 0) \
784 debug_print("Created dir '%s'\n", dir); \
788 static gint mh_create_tree(Folder *folder)
790 gchar *rootpath, *f, *path;
792 cm_return_val_if_fail(folder != NULL, -1);
794 rootpath = LOCAL_FOLDER(folder)->rootpath;
796 if (*rootpath == '/') {
797 #elif defined G_OS_WIN32
798 if (g_ascii_isalpha(*rootpath) && !strncmp(rootpath + 1, ":\\", 2)) {
800 /* Folder path is absolute. */
801 rootpath = g_strdup(rootpath);
803 /* Folder path is relative, using mail base dir. */
804 rootpath = g_strconcat(get_mail_base_dir(), G_DIR_SEPARATOR_S,
808 MAKE_DIR_IF_NOT_EXIST(rootpath);
810 /* Create special directories as needed */
811 if (folder->inbox != NULL &&
812 folder->inbox->path != NULL)
813 f = folder->inbox->path;
816 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
817 MAKE_DIR_IF_NOT_EXIST(path);
820 if (folder->outbox != NULL &&
821 folder->outbox->path != NULL)
822 f = folder->outbox->path;
825 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
826 MAKE_DIR_IF_NOT_EXIST(path);
829 if (folder->draft != NULL &&
830 folder->draft->path != NULL)
831 f = folder->draft->path;
834 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
835 MAKE_DIR_IF_NOT_EXIST(path);
838 if (folder->queue != NULL &&
839 folder->queue->path != NULL)
840 f = folder->queue->path;
843 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
844 MAKE_DIR_IF_NOT_EXIST(path);
847 if (folder->trash != NULL &&
848 folder->trash->path != NULL)
849 f = folder->trash->path;
852 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
853 MAKE_DIR_IF_NOT_EXIST(path);
860 #undef MAKE_DIR_IF_NOT_EXIST
862 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
864 gchar *folder_path, *path;
866 cm_return_val_if_fail(folder != NULL, NULL);
867 cm_return_val_if_fail(item != NULL, NULL);
869 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
870 cm_return_val_if_fail(folder_path != NULL, NULL);
872 /* FIXME: [W32] The code below does not correctly merge
873 relative filenames; there should be a function to handle
875 if ( !is_relative_filename (folder_path) ) {
877 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
880 path = g_strdup(folder_path);
883 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
884 folder_path, G_DIR_SEPARATOR_S,
887 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
891 real_path = mh_filename_from_utf8(path);
892 if (!is_dir_exist(real_path) && is_dir_exist(path)) {
893 /* mmh, older version did put utf8 filenames instead of
894 * the correct encoding */
895 if (g_rename(path, real_path) == 0)
896 folder_item_scan(item);
903 static gboolean mh_renumber_msg(MsgInfo *info)
906 gboolean result = FALSE;
908 cm_return_val_if_fail(info != NULL, FALSE);
910 src = folder_item_fetch_msg(info->folder, info->msgnum);
911 dest = mh_get_new_msg_filename(info->folder);
912 num = info->folder->last_num + 1;
914 if (move_file(src, dest, FALSE) == 0) {
915 msgcache_remove_msg(info->folder->cache, info->msgnum);
917 msgcache_add_msg(info->folder->cache, info);
927 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
930 gchar *path, *real_name;
932 FolderItem *new_item;
933 gchar *mh_sequences_filename;
934 FILE *mh_sequences_file;
936 cm_return_val_if_fail(folder != NULL, NULL);
937 cm_return_val_if_fail(parent != NULL, NULL);
938 cm_return_val_if_fail(name != NULL, NULL);
940 path = folder_item_get_path(parent);
941 if (!is_dir_exist(path))
942 if (make_dir_hier(path) != 0)
945 real_name = mh_filename_from_utf8(name);
946 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
950 if (to_number(name) > 0) {
951 MsgInfo *info = folder_item_get_msginfo(parent, to_number(name));
953 gboolean ok = mh_renumber_msg(info);
954 procmsg_msginfo_free(&info);
962 if (make_dir(fullpath) < 0) {
970 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
973 path = g_strdup(name);
974 new_item = folder_item_new(folder, name, path);
975 folder_item_append(parent, new_item);
979 path = folder_item_get_path(new_item);
980 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
981 ".mh_sequences", NULL);
982 if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
983 fclose(mh_sequences_file);
985 g_free(mh_sequences_filename);
991 static gint mh_rename_folder(Folder *folder, FolderItem *item,
997 gchar *newpath, *utf8newpath;
1000 cm_return_val_if_fail(folder != NULL, -1);
1001 cm_return_val_if_fail(item != NULL, -1);
1002 cm_return_val_if_fail(item->path != NULL, -1);
1003 cm_return_val_if_fail(name != NULL, -1);
1005 oldpath = folder_item_get_path(item);
1006 if (!is_dir_exist(oldpath))
1007 make_dir_hier(oldpath);
1009 dirname = g_path_get_dirname(oldpath);
1010 real_name = mh_filename_from_utf8(name);
1011 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
1014 if (g_rename(oldpath, newpath) < 0) {
1015 FILE_OP_ERROR(oldpath, "rename");
1024 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
1025 dirname = g_path_get_dirname(item->path);
1026 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
1030 utf8newpath = g_strdup(name);
1033 item->name = g_strdup(name);
1035 paths[0] = g_strdup(item->path);
1036 paths[1] = utf8newpath;
1037 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1038 mh_rename_folder_func, paths);
1045 static gint mh_remove_folder(Folder *folder, FolderItem *item)
1050 cm_return_val_if_fail(folder != NULL, -1);
1051 cm_return_val_if_fail(item != NULL, -1);
1052 cm_return_val_if_fail(item->path != NULL, -1);
1054 path = folder_item_get_path(item);
1055 if ((ret = remove_dir_recursive(path)) < 0) {
1056 g_warning("can't remove directory '%s'", path);
1062 folder_item_remove(item);
1066 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1071 cm_return_val_if_fail(item != NULL, NULL);
1072 cm_return_val_if_fail(file != NULL, NULL);
1074 flags.perm_flags = MSG_NEW|MSG_UNREAD;
1075 flags.tmp_flags = 0;
1077 if (folder_has_parent_of_type(item, F_QUEUE)) {
1078 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1079 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
1080 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1083 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1084 if (!msginfo) return NULL;
1086 msginfo->msgnum = atoi(file);
1087 msginfo->folder = item;
1092 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
1097 cm_return_val_if_fail(node->data != NULL, FALSE);
1099 if (G_NODE_IS_ROOT(node))
1102 item = FOLDER_ITEM(node->data);
1104 path = folder_item_get_path(item);
1105 if (!is_dir_exist(path)) {
1106 debug_print("folder '%s' not found. removing...\n", path?path:"(null)");
1107 folder_item_remove(item);
1114 static void mh_remove_missing_folder_items(Folder *folder)
1116 cm_return_if_fail(folder != NULL);
1118 debug_print("searching missing folders...\n");
1120 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1121 mh_remove_missing_folder_items_func, folder);
1124 static void mh_scan_tree_recursive(FolderItem *item)
1128 const gchar *dir_name;
1129 gchar *entry, *utf8entry, *utf8name, *path;
1131 GError *error = NULL;
1133 cm_return_if_fail(item != NULL);
1134 cm_return_if_fail(item->folder != NULL);
1136 folder = item->folder;
1138 path = folder_item_get_path(item);
1139 debug_print("mh_scan_tree_recursive() opening '%s'\n", path);
1140 dir = g_dir_open(path, 0, &error);
1142 g_warning("failed to open directory '%s': %s (%d)",
1143 path, error->message, error->code);
1144 g_error_free(error);
1149 debug_print("scanning %s ...\n",
1150 item->path ? item->path
1151 : LOCAL_FOLDER(item->folder)->rootpath);
1152 if (folder->ui_func)
1153 folder->ui_func(folder, item, folder->ui_func_data);
1155 while ((dir_name = g_dir_read_name(dir)) != NULL) {
1156 if (dir_name[0] == '.') continue;
1158 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir_name, NULL);
1160 utf8name = mh_filename_to_utf8(dir_name);
1162 utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1165 utf8entry = g_strdup(utf8name);
1167 if (g_file_test(entry, G_FILE_TEST_IS_DIR)) {
1168 FolderItem *new_item = NULL;
1172 for (node = node->children; node != NULL; node = node->next) {
1173 FolderItem *cur_item = FOLDER_ITEM(node->data);
1174 gchar *curpath = folder_item_get_path(cur_item);
1175 if (!strcmp2(curpath, entry)) {
1176 new_item = cur_item;
1183 debug_print("new folder '%s' found.\n", entry);
1184 new_item = folder_item_new(folder, utf8name, utf8entry);
1185 folder_item_append(item, new_item);
1189 if (!folder->inbox &&
1190 !strcmp(dir_name, INBOX_DIR)) {
1191 new_item->stype = F_INBOX;
1192 folder->inbox = new_item;
1193 } else if (!folder->outbox &&
1194 !strcmp(dir_name, OUTBOX_DIR)) {
1195 new_item->stype = F_OUTBOX;
1196 folder->outbox = new_item;
1197 } else if (!folder->draft &&
1198 !strcmp(dir_name, DRAFT_DIR)) {
1199 new_item->stype = F_DRAFT;
1200 folder->draft = new_item;
1201 } else if (!folder->queue &&
1202 !strcmp(dir_name, QUEUE_DIR)) {
1203 new_item->stype = F_QUEUE;
1204 folder->queue = new_item;
1205 } else if (!folder->trash &&
1206 !strcmp(dir_name, TRASH_DIR)) {
1207 new_item->stype = F_TRASH;
1208 folder->trash = new_item;
1212 mh_scan_tree_recursive(new_item);
1213 } else if (to_number(dir_name) > 0) n_msg++;
1223 mh_set_mtime(folder, item);
1226 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1228 FolderItem *item = node->data;
1229 gchar **paths = data;
1230 const gchar *oldpath = paths[0];
1231 const gchar *newpath = paths[1];
1233 gchar *new_itempath;
1236 oldpathlen = strlen(oldpath);
1237 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1238 g_warning("path doesn't match: %s, %s", oldpath, item->path);
1242 base = item->path + oldpathlen;
1243 while (*base == G_DIR_SEPARATOR) base++;
1245 new_itempath = g_strdup(newpath);
1247 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1250 item->path = new_itempath;
1255 static gchar *mh_filename_from_utf8(const gchar *path)
1257 gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1260 g_warning("mh_filename_from_utf8: failed to convert character set");
1261 real_path = g_strdup(path);
1267 static gchar *mh_filename_to_utf8(const gchar *path)
1269 gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1271 g_warning("mh_filename_to_utf8: failed to convert character set");
1272 utf8path = g_strdup(path);
1278 static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1280 MsgInfo *msginfo_a = (MsgInfo *) a;
1281 MsgInfo *msginfo_b = (MsgInfo *) b;
1283 return (msginfo_a->msgnum - msginfo_b->msgnum);
1286 static gchar *get_unseen_seq_name(void)
1288 static gchar *seq_name = NULL;
1290 gchar buf[BUFFSIZE];
1292 gchar *profile_path = g_strconcat(
1293 get_home_dir(), G_DIR_SEPARATOR_S,
1294 ".mh_profile", NULL);
1295 FILE *fp = g_fopen(profile_path, "r");
1297 while (fgets(buf, sizeof(buf), fp) != NULL) {
1298 if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
1299 gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
1300 while (*seq_tmp == ' ')
1302 seq_name = g_strdup(seq_tmp);
1303 seq_name = strretchomp(seq_name);
1310 seq_name = g_strdup("unseen");
1311 tmp = g_strdup_printf("%s:", seq_name);
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];
1324 gboolean err = FALSE;
1330 path = folder_item_get_path(item);
1332 mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
1333 ".mh_sequences", NULL);
1334 mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
1335 ".mh_sequences.new", NULL);
1336 if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
1337 GSList *msglist = folder_item_get_msg_list(item);
1339 MsgInfo *info = NULL;
1340 gint start = -1, end = -1;
1341 gchar *sequence = g_strdup("");
1343 msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
1346 /* write the unseen sequence if we don't have to scrap it */
1347 if (!remove_unseen) do {
1348 info = (MsgInfo *)(cur ? cur->data:NULL);
1349 if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
1351 start = end = info->msgnum;
1355 if (start > 0 && end > 0) {
1359 snprintf(tmp, 31, " %d-%d", start, end);
1361 snprintf(tmp, 31, " %d", start);
1363 tmp_len = strlen(tmp);
1364 sequence = g_realloc(sequence, seq_len+tmp_len+1);
1365 strcpy(sequence+seq_len, tmp);
1371 cur = cur ? cur->next:NULL;
1372 } while (cur || (start > 0 && end > 0));
1373 if (sequence && *sequence) {
1374 if (fprintf(mh_sequences_new_fp, "%s%s\n",
1375 get_unseen_seq_name(), sequence) < 0)
1378 debug_print("wrote unseen sequence: '%s%s'\n",
1379 get_unseen_seq_name(), sequence);
1381 /* rewrite the rest of the file */
1382 if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
1383 while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
1384 if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
1385 if (fprintf(mh_sequences_new_fp, "%s", buf) < 0) {
1390 fclose(mh_sequences_old_fp);
1393 fflush(mh_sequences_new_fp);
1395 fsync(fileno(mh_sequences_new_fp));
1397 if (fclose(mh_sequences_new_fp) == EOF)
1401 if (g_rename(mh_sequences_new, mh_sequences_old) < 0)
1402 FILE_OP_ERROR(mh_sequences_new, "rename");
1405 procmsg_msg_list_free(msglist);
1407 g_free(mh_sequences_old);
1408 g_free(mh_sequences_new);
1414 static int mh_item_close(Folder *folder, FolderItem *item)
1416 time_t last_mtime = (time_t)0;
1417 gboolean need_scan = mh_scan_required(item->folder, item);
1418 last_mtime = item->mtime;
1420 mh_write_sequences(item, FALSE);
1422 if (item->mtime == last_mtime && !need_scan) {
1423 mh_set_mtime(folder, item);
1429 static void mh_set_mtime(Folder *folder, FolderItem *item)
1432 gchar *path = folder_item_get_path(item);
1434 cm_return_if_fail(path != NULL);
1436 if (g_stat(path, &s) < 0) {
1437 FILE_OP_ERROR(path, "stat");
1442 item->mtime = s.st_mtime;
1443 debug_print("MH: forced mtime of %s to %ld\n", item->name?item->name:"(null)", item->mtime);