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_message("Couldn't open current directory: %s (%d).\n",
262 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)) {
282 debug_print("Last number in dir %s = %d\n", item->path?item->path:"(null)", max);
283 item->last_num = max;
286 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
292 GError *error = NULL;
293 gint num, nummsgs = 0;
295 cm_return_val_if_fail(item != NULL, -1);
297 debug_print("mh_get_num_list(): Scanning %s ...\n", item->path?item->path:"(null)");
299 *old_uids_valid = TRUE;
301 path = folder_item_get_path(item);
302 cm_return_val_if_fail(path != NULL, -1);
304 if ((dp = g_dir_open(path, 0, &error)) == NULL) {
305 g_message("Couldn't open current directory: %s (%d).\n",
306 error->message, error->code);
313 while ((d = g_dir_read_name(dp)) != NULL) {
314 if ((num = to_number(d)) > 0) {
315 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
321 mh_set_mtime(folder, item);
325 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
330 cm_return_val_if_fail(item != NULL, NULL);
331 cm_return_val_if_fail(num > 0, NULL);
333 path = folder_item_get_path(item);
334 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
336 if (!is_file_exist(file)) {
345 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
350 cm_return_val_if_fail(item != NULL, NULL);
354 file = mh_fetch_msg(folder, item, num);
355 if (!file) return NULL;
357 msginfo = mh_parse_msg(file, item);
359 msginfo->msgnum = num;
366 static gchar *mh_get_new_msg_filename(FolderItem *dest)
371 destpath = folder_item_get_path(dest);
372 cm_return_val_if_fail(destpath != NULL, NULL);
374 if (dest->last_num < 0) {
375 mh_get_last_num(dest->folder, dest);
376 if (dest->last_num < 0) return NULL;
379 if (!is_dir_exist(destpath))
380 make_dir_hier(destpath);
383 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
385 if (is_file_entry_exist(destfile)) {
397 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
401 MsgFileInfo fileinfo;
403 cm_return_val_if_fail(file != NULL, -1);
405 fileinfo.msginfo = NULL;
406 fileinfo.file = (gchar *)file;
407 fileinfo.flags = flags;
408 file_list.data = &fileinfo;
409 file_list.next = NULL;
411 ret = mh_add_msgs(folder, dest, &file_list, NULL);
415 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
416 GHashTable *relation)
420 MsgFileInfo *fileinfo;
422 cm_return_val_if_fail(dest != NULL, -1);
423 cm_return_val_if_fail(file_list != NULL, -1);
425 if (dest->last_num < 0) {
426 mh_get_last_num(folder, dest);
427 if (dest->last_num < 0) return -1;
430 for (cur = file_list; cur != NULL; cur = cur->next) {
431 fileinfo = (MsgFileInfo *)cur->data;
433 destfile = mh_get_new_msg_filename(dest);
434 if (destfile == NULL) return -1;
437 if (link(fileinfo->file, destfile) < 0) {
439 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
440 g_warning("can't copy message %s to %s",
441 fileinfo->file, destfile);
449 if (relation != NULL)
450 g_hash_table_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
454 mh_write_sequences(dest, TRUE);
455 return dest->last_num;
458 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
462 cm_return_val_if_fail(msginfo != NULL, -1);
464 msglist.data = msginfo;
467 return mh_copy_msgs(folder, dest, &msglist, NULL);
470 static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist,
471 GHashTable *relation)
473 gboolean dest_need_scan = FALSE;
474 gboolean src_need_scan = FALSE;
475 FolderItem *src = NULL;
478 FolderItemPrefs *prefs;
479 MsgInfo *msginfo = NULL;
480 MsgInfoList *cur = NULL;
481 gint curnum = 0, total = 0;
482 gchar *srcpath = NULL;
483 gboolean full_fetch = FALSE;
484 time_t last_dest_mtime = (time_t)0;
485 time_t last_src_mtime = (time_t)0;
487 cm_return_val_if_fail(dest != NULL, -1);
488 cm_return_val_if_fail(msglist != NULL, -1);
490 msginfo = (MsgInfo *)msglist->data;
492 cm_return_val_if_fail(msginfo != NULL, -1);
494 if (msginfo->folder == dest) {
495 g_warning("the src folder is identical to the dest.");
499 if (msginfo->folder->folder != dest->folder)
502 if (FOLDER_TYPE(msginfo->folder->folder) == F_MH) {
503 src = msginfo->folder;
506 if (dest->last_num < 0) {
507 mh_get_last_num(folder, dest);
508 if (dest->last_num < 0) return -1;
513 srcpath = folder_item_get_path(msginfo->folder);
515 dest_need_scan = mh_scan_required(dest->folder, dest);
516 last_dest_mtime = dest->mtime;
519 src_need_scan = mh_scan_required(src->folder, src);
520 last_src_mtime = src->mtime;
523 total = g_slist_length(msglist);
525 if (MSG_IS_MOVE(msginfo->flags))
526 statusbar_print_all(_("Moving messages..."));
528 statusbar_print_all(_("Copying messages..."));
530 for (cur = msglist; cur; cur = cur->next) {
531 msginfo = (MsgInfo *)cur->data;
533 goto err_reset_status;
536 srcfile = g_strconcat(srcpath,
538 itos(msginfo->msgnum), NULL);
540 srcfile = procmsg_get_message_file(msginfo);
543 goto err_reset_status;
545 destfile = mh_get_new_msg_filename(dest);
548 goto err_reset_status;
552 statusbar_progress_all(curnum, total, 100);
553 if (curnum % 100 == 0)
558 debug_print("Copying message %s%c%d to %s ...\n",
559 msginfo->folder->path, G_DIR_SEPARATOR,
560 msginfo->msgnum, dest->path);
563 if (MSG_IS_MOVE(msginfo->flags)) {
564 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
565 if (move_file(srcfile, destfile, TRUE) < 0) {
566 FILE_OP_ERROR(srcfile, "move");
567 if (copy_file(srcfile, destfile, TRUE) < 0) {
568 FILE_OP_ERROR(srcfile, "copy");
571 goto err_reset_status;
574 /* say unlinking's not necessary */
575 msginfo->flags.tmp_flags |= MSG_MOVE_DONE;
577 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
578 FILE_OP_ERROR(srcfile, "copy");
581 goto err_reset_status;
583 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
584 if (chmod(destfile, prefs->folder_chmod) < 0)
585 FILE_OP_ERROR(destfile, "chmod");
588 if (g_hash_table_lookup(relation, msginfo) != NULL)
589 g_warning("already in: %p", msginfo);
591 g_hash_table_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
599 mh_write_sequences(dest, TRUE);
601 if (dest->mtime == last_dest_mtime && !dest_need_scan) {
602 mh_set_mtime(folder, dest);
605 if (src && src->mtime == last_src_mtime && !src_need_scan) {
606 mh_set_mtime(folder, src);
610 statusbar_progress_all(0,0,0);
613 return dest->last_num;
616 mh_write_sequences(dest, TRUE);
618 statusbar_progress_all(0,0,0);
625 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
627 gboolean need_scan = FALSE;
628 time_t last_mtime = (time_t)0;
631 cm_return_val_if_fail(item != NULL, -1);
633 file = mh_fetch_msg(folder, item, num);
634 cm_return_val_if_fail(file != NULL, -1);
636 need_scan = mh_scan_required(folder, item);
637 last_mtime = item->mtime;
639 if (claws_unlink(file) < 0) {
640 FILE_OP_ERROR(file, "unlink");
645 if (item->mtime == last_mtime && !need_scan) {
646 mh_set_mtime(folder, item);
652 static gint mh_remove_msgs(Folder *folder, FolderItem *item,
653 MsgInfoList *msglist, GHashTable *relation)
655 gboolean need_scan = FALSE;
657 time_t last_mtime = (time_t)0;
659 gint total = 0, curnum = 0;
661 cm_return_val_if_fail(item != NULL, -1);
663 path = folder_item_get_path(item);
665 need_scan = mh_scan_required(folder, item);
666 last_mtime = item->mtime;
668 total = g_slist_length(msglist);
670 statusbar_print_all(_("Deleting messages..."));
673 for (cur = msglist; cur; cur = cur->next) {
674 MsgInfo *msginfo = (MsgInfo *)cur->data;
677 if (MSG_IS_MOVE(msginfo->flags) && MSG_IS_MOVE_DONE(msginfo->flags)) {
678 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
682 statusbar_progress_all(curnum, total, 100);
683 if (curnum % 100 == 0)
688 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
692 if (claws_unlink(file) < 0) {
701 statusbar_progress_all(0,0,0);
704 if (item->mtime == last_mtime && !need_scan) {
705 mh_set_mtime(folder, item);
712 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
717 cm_return_val_if_fail(item != NULL, -1);
719 path = folder_item_get_path(item);
720 cm_return_val_if_fail(path != NULL, -1);
721 val = remove_all_numbered_files(path);
724 mh_write_sequences(item, TRUE);
729 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
735 path = g_strdup_printf("%s%c%d", folder_item_get_path(item),
736 G_DIR_SEPARATOR, msginfo->msgnum);
737 if (g_stat((path), &s) < 0 ||
738 msginfo->size != s.st_size || (
739 (msginfo->mtime - s.st_mtime != 0) &&
740 (msginfo->mtime - s.st_mtime != 3600) &&
741 (msginfo->mtime - s.st_mtime != -3600))) {
750 static gint mh_scan_tree(Folder *folder)
754 cm_return_val_if_fail(folder != NULL, -1);
757 item = folder_item_new(folder, folder->name, NULL);
758 item->folder = folder;
759 folder->node = item->node = g_node_new(item);
761 item = FOLDER_ITEM(folder->node->data);
763 mh_create_tree(folder);
764 mh_remove_missing_folder_items(folder);
765 mh_scan_tree_recursive(item);
770 #define MAKE_DIR_IF_NOT_EXIST(dir) \
772 if (!is_dir_exist(dir)) { \
773 if (is_file_exist(dir)) { \
774 g_warning("File '%s' already exists. " \
775 "Can't create folder.", dir); \
778 if (make_dir_hier(dir) < 0) \
780 debug_print("Created dir '%s'\n", dir); \
784 static gint mh_create_tree(Folder *folder)
786 gchar *rootpath, *f, *path;
788 cm_return_val_if_fail(folder != NULL, -1);
790 rootpath = LOCAL_FOLDER(folder)->rootpath;
792 if (*rootpath == '/') {
794 if (g_ascii_isalpha(*rootpath) && !strncmp(rootpath + 1, "\:", 2)) {
796 /* Folder path is absolute. */
797 rootpath = g_strdup(rootpath);
799 /* Folder path is relative, using mail base dir. */
800 rootpath = g_strconcat(get_mail_base_dir(), G_DIR_SEPARATOR_S,
804 MAKE_DIR_IF_NOT_EXIST(rootpath);
806 /* Create special directories as needed */
807 if (folder->inbox != NULL &&
808 folder->inbox->path != NULL)
809 f = folder->inbox->path;
812 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
813 MAKE_DIR_IF_NOT_EXIST(path);
816 if (folder->outbox != NULL &&
817 folder->outbox->path != NULL)
818 f = folder->outbox->path;
821 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
822 MAKE_DIR_IF_NOT_EXIST(path);
825 if (folder->draft != NULL &&
826 folder->draft->path != NULL)
827 f = folder->draft->path;
830 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
831 MAKE_DIR_IF_NOT_EXIST(path);
834 if (folder->queue != NULL &&
835 folder->queue->path != NULL)
836 f = folder->queue->path;
839 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
840 MAKE_DIR_IF_NOT_EXIST(path);
843 if (folder->trash != NULL &&
844 folder->trash->path != NULL)
845 f = folder->trash->path;
848 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
849 MAKE_DIR_IF_NOT_EXIST(path);
856 #undef MAKE_DIR_IF_NOT_EXIST
858 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
860 gchar *folder_path, *path;
862 cm_return_val_if_fail(folder != NULL, NULL);
863 cm_return_val_if_fail(item != NULL, NULL);
865 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
866 cm_return_val_if_fail(folder_path != NULL, NULL);
868 /* FIXME: [W32] The code below does not correctly merge
869 relative filenames; there should be a function to handle
871 if ( !is_relative_filename (folder_path) ) {
873 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
876 path = g_strdup(folder_path);
879 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
880 folder_path, G_DIR_SEPARATOR_S,
883 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
887 real_path = mh_filename_from_utf8(path);
888 if (!is_dir_exist(real_path) && is_dir_exist(path)) {
889 /* mmh, older version did put utf8 filenames instead of
890 * the correct encoding */
891 if (g_rename(path, real_path) == 0)
892 folder_item_scan(item);
899 static gboolean mh_renumber_msg(MsgInfo *info)
902 gboolean result = FALSE;
904 cm_return_val_if_fail(info != NULL, FALSE);
906 src = folder_item_fetch_msg(info->folder, info->msgnum);
907 dest = mh_get_new_msg_filename(info->folder);
908 num = info->folder->last_num + 1;
910 if (move_file(src, dest, FALSE) == 0) {
911 msgcache_remove_msg(info->folder->cache, info->msgnum);
913 msgcache_add_msg(info->folder->cache, info);
923 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
926 gchar *path, *real_name;
928 FolderItem *new_item;
929 gchar *mh_sequences_filename;
930 FILE *mh_sequences_file;
932 cm_return_val_if_fail(folder != NULL, NULL);
933 cm_return_val_if_fail(parent != NULL, NULL);
934 cm_return_val_if_fail(name != NULL, NULL);
936 path = folder_item_get_path(parent);
937 if (!is_dir_exist(path))
938 if (make_dir_hier(path) != 0)
941 real_name = mh_filename_from_utf8(name);
942 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
946 if (to_number(name) > 0) {
947 MsgInfo *info = folder_item_get_msginfo(parent, to_number(name));
949 gboolean ok = mh_renumber_msg(info);
950 procmsg_msginfo_free(&info);
958 if (make_dir(fullpath) < 0) {
966 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
969 path = g_strdup(name);
970 new_item = folder_item_new(folder, name, path);
971 folder_item_append(parent, new_item);
975 path = folder_item_get_path(new_item);
976 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
977 ".mh_sequences", NULL);
978 if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
979 fclose(mh_sequences_file);
981 g_free(mh_sequences_filename);
987 static gint mh_rename_folder(Folder *folder, FolderItem *item,
993 gchar *newpath, *utf8newpath;
996 cm_return_val_if_fail(folder != NULL, -1);
997 cm_return_val_if_fail(item != NULL, -1);
998 cm_return_val_if_fail(item->path != NULL, -1);
999 cm_return_val_if_fail(name != NULL, -1);
1001 oldpath = folder_item_get_path(item);
1002 if (!is_dir_exist(oldpath))
1003 make_dir_hier(oldpath);
1005 dirname = g_path_get_dirname(oldpath);
1006 real_name = mh_filename_from_utf8(name);
1007 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
1010 if (g_rename(oldpath, newpath) < 0) {
1011 FILE_OP_ERROR(oldpath, "rename");
1020 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
1021 dirname = g_path_get_dirname(item->path);
1022 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
1026 utf8newpath = g_strdup(name);
1029 item->name = g_strdup(name);
1031 paths[0] = g_strdup(item->path);
1032 paths[1] = utf8newpath;
1033 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1034 mh_rename_folder_func, paths);
1041 static gint mh_remove_folder(Folder *folder, FolderItem *item)
1046 cm_return_val_if_fail(folder != NULL, -1);
1047 cm_return_val_if_fail(item != NULL, -1);
1048 cm_return_val_if_fail(item->path != NULL, -1);
1050 path = folder_item_get_path(item);
1051 if ((ret = remove_dir_recursive(path)) < 0) {
1052 g_warning("can't remove directory '%s'", path);
1058 folder_item_remove(item);
1062 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1067 cm_return_val_if_fail(item != NULL, NULL);
1068 cm_return_val_if_fail(file != NULL, NULL);
1070 flags.perm_flags = MSG_NEW|MSG_UNREAD;
1071 flags.tmp_flags = 0;
1073 if (folder_has_parent_of_type(item, F_QUEUE)) {
1074 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1075 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
1076 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1079 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1080 if (!msginfo) return NULL;
1082 msginfo->msgnum = atoi(file);
1083 msginfo->folder = item;
1088 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
1093 cm_return_val_if_fail(node->data != NULL, FALSE);
1095 if (G_NODE_IS_ROOT(node))
1098 item = FOLDER_ITEM(node->data);
1100 path = folder_item_get_path(item);
1101 if (!is_dir_exist(path)) {
1102 debug_print("folder '%s' not found. removing...\n", path?path:"(null)");
1103 folder_item_remove(item);
1110 static void mh_remove_missing_folder_items(Folder *folder)
1112 cm_return_if_fail(folder != NULL);
1114 debug_print("searching missing folders...\n");
1116 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1117 mh_remove_missing_folder_items_func, folder);
1120 static void mh_scan_tree_recursive(FolderItem *item)
1124 const gchar *dir_name;
1125 gchar *entry, *utf8entry, *utf8name, *path;
1127 GError *error = NULL;
1129 cm_return_if_fail(item != NULL);
1130 cm_return_if_fail(item->folder != NULL);
1132 folder = item->folder;
1134 path = folder_item_get_path(item);
1135 debug_print("mh_scan_tree_recursive() opening '%s'\n", path);
1136 dir = g_dir_open(path, 0, &error);
1138 g_warning("failed to open directory '%s': %s (%d)",
1139 path, error->message, error->code);
1140 g_error_free(error);
1145 debug_print("scanning %s ...\n",
1146 item->path ? item->path
1147 : LOCAL_FOLDER(item->folder)->rootpath);
1148 if (folder->ui_func)
1149 folder->ui_func(folder, item, folder->ui_func_data);
1151 while ((dir_name = g_dir_read_name(dir)) != NULL) {
1152 if (dir_name[0] == '.') continue;
1154 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir_name, NULL);
1156 utf8name = mh_filename_to_utf8(dir_name);
1158 utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1161 utf8entry = g_strdup(utf8name);
1163 if (g_file_test(entry, G_FILE_TEST_IS_DIR)) {
1164 FolderItem *new_item = NULL;
1168 for (node = node->children; node != NULL; node = node->next) {
1169 FolderItem *cur_item = FOLDER_ITEM(node->data);
1170 gchar *curpath = folder_item_get_path(cur_item);
1171 if (!strcmp2(curpath, entry)) {
1172 new_item = cur_item;
1179 debug_print("new folder '%s' found.\n", entry);
1180 new_item = folder_item_new(folder, utf8name, utf8entry);
1181 folder_item_append(item, new_item);
1185 if (!folder->inbox &&
1186 !strcmp(dir_name, INBOX_DIR)) {
1187 new_item->stype = F_INBOX;
1188 folder->inbox = new_item;
1189 } else if (!folder->outbox &&
1190 !strcmp(dir_name, OUTBOX_DIR)) {
1191 new_item->stype = F_OUTBOX;
1192 folder->outbox = new_item;
1193 } else if (!folder->draft &&
1194 !strcmp(dir_name, DRAFT_DIR)) {
1195 new_item->stype = F_DRAFT;
1196 folder->draft = new_item;
1197 } else if (!folder->queue &&
1198 !strcmp(dir_name, QUEUE_DIR)) {
1199 new_item->stype = F_QUEUE;
1200 folder->queue = new_item;
1201 } else if (!folder->trash &&
1202 !strcmp(dir_name, TRASH_DIR)) {
1203 new_item->stype = F_TRASH;
1204 folder->trash = new_item;
1208 mh_scan_tree_recursive(new_item);
1209 } else if (to_number(dir_name) > 0) n_msg++;
1219 mh_set_mtime(folder, item);
1222 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1224 FolderItem *item = node->data;
1225 gchar **paths = data;
1226 const gchar *oldpath = paths[0];
1227 const gchar *newpath = paths[1];
1229 gchar *new_itempath;
1232 oldpathlen = strlen(oldpath);
1233 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1234 g_warning("path doesn't match: %s, %s", oldpath, item->path);
1238 base = item->path + oldpathlen;
1239 while (*base == G_DIR_SEPARATOR) base++;
1241 new_itempath = g_strdup(newpath);
1243 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1246 item->path = new_itempath;
1251 static gchar *mh_filename_from_utf8(const gchar *path)
1253 gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1256 g_warning("mh_filename_from_utf8: failed to convert character set");
1257 real_path = g_strdup(path);
1263 static gchar *mh_filename_to_utf8(const gchar *path)
1265 gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1267 g_warning("mh_filename_to_utf8: failed to convert character set");
1268 utf8path = g_strdup(path);
1274 static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1276 MsgInfo *msginfo_a = (MsgInfo *) a;
1277 MsgInfo *msginfo_b = (MsgInfo *) b;
1279 return (msginfo_a->msgnum - msginfo_b->msgnum);
1282 static gchar *get_unseen_seq_name(void)
1284 static gchar *seq_name = NULL;
1286 gchar buf[BUFFSIZE];
1288 gchar *profile_path = g_strconcat(
1289 get_home_dir(), G_DIR_SEPARATOR_S,
1290 ".mh_profile", NULL);
1291 FILE *fp = g_fopen(profile_path, "r");
1293 while (fgets(buf, sizeof(buf), fp) != NULL) {
1294 if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
1295 gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
1296 while (*seq_tmp == ' ')
1298 seq_name = g_strdup(seq_tmp);
1299 seq_name = strretchomp(seq_name);
1306 seq_name = g_strdup("unseen");
1307 tmp = g_strdup_printf("%s:", seq_name);
1314 static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
1316 gchar *mh_sequences_old, *mh_sequences_new;
1317 FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
1318 gchar buf[BUFFSIZE];
1320 gboolean err = FALSE;
1326 path = folder_item_get_path(item);
1328 mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
1329 ".mh_sequences", NULL);
1330 mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
1331 ".mh_sequences.new", NULL);
1332 if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
1333 GSList *msglist = folder_item_get_msg_list(item);
1335 MsgInfo *info = NULL;
1336 gint start = -1, end = -1;
1337 gchar *sequence = g_strdup("");
1339 msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
1342 /* write the unseen sequence if we don't have to scrap it */
1343 if (!remove_unseen) do {
1344 info = (MsgInfo *)(cur ? cur->data:NULL);
1345 if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
1347 start = end = info->msgnum;
1351 if (start > 0 && end > 0) {
1355 snprintf(tmp, 31, " %d-%d", start, end);
1357 snprintf(tmp, 31, " %d", start);
1359 tmp_len = strlen(tmp);
1360 sequence = g_realloc(sequence, seq_len+tmp_len+1);
1361 strcpy(sequence+seq_len, tmp);
1367 cur = cur ? cur->next:NULL;
1368 } while (cur || (start > 0 && end > 0));
1369 if (sequence && *sequence) {
1370 if (fprintf(mh_sequences_new_fp, "%s%s\n",
1371 get_unseen_seq_name(), sequence) < 0)
1374 debug_print("wrote unseen sequence: '%s%s'\n",
1375 get_unseen_seq_name(), sequence);
1377 /* rewrite the rest of the file */
1378 if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
1379 while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
1380 if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
1381 if (fprintf(mh_sequences_new_fp, "%s", buf) < 0) {
1386 fclose(mh_sequences_old_fp);
1389 fflush(mh_sequences_new_fp);
1391 fsync(fileno(mh_sequences_new_fp));
1393 if (fclose(mh_sequences_new_fp) == EOF)
1397 if (g_rename(mh_sequences_new, mh_sequences_old) < 0)
1398 FILE_OP_ERROR(mh_sequences_new, "rename");
1401 procmsg_msg_list_free(msglist);
1403 g_free(mh_sequences_old);
1404 g_free(mh_sequences_new);
1410 static int mh_item_close(Folder *folder, FolderItem *item)
1412 time_t last_mtime = (time_t)0;
1413 gboolean need_scan = mh_scan_required(item->folder, item);
1414 last_mtime = item->mtime;
1416 mh_write_sequences(item, FALSE);
1418 if (item->mtime == last_mtime && !need_scan) {
1419 mh_set_mtime(folder, item);
1425 static void mh_set_mtime(Folder *folder, FolderItem *item)
1428 gchar *path = folder_item_get_path(item);
1430 cm_return_if_fail(path != NULL);
1432 if (g_stat(path, &s) < 0) {
1433 FILE_OP_ERROR(path, "stat");
1438 item->mtime = s.st_mtime;
1439 debug_print("MH: forced mtime of %s to %ld\n", item->name?item->name:"(null)", item->mtime);