2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 # include <sys/time.h>
43 #include "procheader.h"
46 static void mh_folder_init (Folder *folder,
50 GSList *mh_get_msg_list (Folder *folder,
53 gchar *mh_fetch_msg (Folder *folder,
56 MsgInfo *mh_fetch_msginfo (Folder *folder,
59 gint mh_add_msg (Folder *folder,
62 gboolean remove_source);
63 gint mh_move_msg (Folder *folder,
66 gint mh_move_msgs_with_dest (Folder *folder,
69 gint mh_copy_msg (Folder *folder,
72 gint mh_copy_msgs_with_dest (Folder *folder,
75 gint mh_remove_msg (Folder *folder,
78 gint mh_remove_all_msg (Folder *folder,
80 gboolean mh_is_msg_changed (Folder *folder,
84 gint mh_scan_folder (Folder *folder,
86 GSList *mh_get_num_list (Folder *folder,
88 void mh_scan_tree (Folder *folder);
90 gint mh_create_tree (Folder *folder);
91 FolderItem *mh_create_folder (Folder *folder,
94 gint mh_rename_folder (Folder *folder,
97 gint mh_remove_folder (Folder *folder,
100 gchar *mh_get_new_msg_filename (FolderItem *dest);
102 static GSList *mh_get_uncached_msgs (GHashTable *msg_table,
104 static MsgInfo *mh_parse_msg (const gchar *file,
106 static void mh_scan_tree_recursive (FolderItem *item);
108 static gboolean mh_rename_folder_func (GNode *node,
112 Folder *mh_folder_new(const gchar *name, const gchar *path)
116 folder = (Folder *)g_new0(MHFolder, 1);
117 mh_folder_init(folder, name, path);
122 void mh_folder_destroy(MHFolder *folder)
124 folder_local_folder_destroy(LOCAL_FOLDER(folder));
127 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
131 folder_local_folder_init(folder, name, path);
133 /* folder->get_msg_list = mh_get_msg_list; */
134 folder->fetch_msg = mh_fetch_msg;
135 folder->fetch_msginfo = mh_fetch_msginfo;
136 folder->add_msg = mh_add_msg;
137 folder->move_msg = mh_move_msg;
138 folder->move_msgs_with_dest = mh_move_msgs_with_dest;
139 folder->copy_msg = mh_copy_msg;
140 folder->copy_msgs_with_dest = mh_copy_msgs_with_dest;
141 folder->remove_msg = mh_remove_msg;
142 folder->remove_all_msg = mh_remove_all_msg;
143 folder->is_msg_changed = mh_is_msg_changed;
144 /* folder->scan = mh_scan_folder; */
145 folder->get_num_list = mh_get_num_list;
146 folder->scan_tree = mh_scan_tree;
147 folder->create_tree = mh_create_tree;
148 folder->create_folder = mh_create_folder;
149 folder->rename_folder = mh_rename_folder;
150 folder->remove_folder = mh_remove_folder;
151 folder->destroy = mh_folder_destroy;
154 void mh_get_last_num(Folder *folder, FolderItem *item)
163 g_return_if_fail(item != NULL);
165 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
167 path = folder_item_get_path(item);
168 g_return_if_fail(path != NULL);
169 if (change_dir(path) < 0) {
175 if ((dp = opendir(".")) == NULL) {
176 FILE_OP_ERROR(item->path, "opendir");
180 while ((d = readdir(dp)) != NULL) {
181 if ((num = to_number(d->d_name)) >= 0 &&
182 stat(d->d_name, &s) == 0 &&
183 S_ISREG(s.st_mode)) {
190 debug_print("Last number in dir %s = %d\n", item->path, max);
191 item->last_num = max;
194 GSList *mh_get_num_list(Folder *folder, FolderItem *item)
204 g_return_val_if_fail(item != NULL, NULL);
206 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
208 path = folder_item_get_path(item);
209 g_return_val_if_fail(path != NULL, NULL);
210 if (change_dir(path) < 0) {
216 if ((dp = opendir(".")) == NULL) {
217 FILE_OP_ERROR(item->path, "opendir");
221 while ((d = readdir(dp)) != NULL) {
222 if ((num = to_number(d->d_name)) >= 0 &&
223 stat(d->d_name, &s) == 0 &&
224 S_ISREG(s.st_mode)) {
225 list = g_slist_prepend(list, GINT_TO_POINTER(num));
233 GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
236 GHashTable *msg_table;
239 gboolean scan_new = TRUE;
241 struct timeval tv_before, tv_after, tv_result;
243 gettimeofday(&tv_before, NULL);
246 g_return_val_if_fail(item != NULL, NULL);
248 path = folder_item_get_path(item);
249 if (stat(path, &s) < 0) {
250 FILE_OP_ERROR(path, "stat");
254 mtime = MAX(s.st_mtime, s.st_ctime);
255 if (item->mtime == mtime) {
256 debug_print("Folder is not modified.\n");
263 if (use_cache && !scan_new) {
264 mlist = procmsg_read_cache(item, FALSE);
266 mlist = mh_get_uncached_msgs(NULL, item);
267 } else if (use_cache) {
270 mlist = procmsg_read_cache(item, TRUE);
271 msg_table = procmsg_msg_hash_table_create(mlist);
273 newlist = mh_get_uncached_msgs(msg_table, item);
275 g_hash_table_destroy(msg_table);
277 mlist = g_slist_concat(mlist, newlist);
279 mlist = mh_get_uncached_msgs(NULL, item);
281 procmsg_set_flags(mlist, item);
284 gettimeofday(&tv_after, NULL);
286 timersub(&tv_after, &tv_before, &tv_result);
287 g_print("mh_get_msg_list: %s: elapsed time: %ld.%06ld sec\n",
288 item->path, tv_result.tv_sec, tv_result.tv_usec);
294 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
299 g_return_val_if_fail(item != NULL, NULL);
300 g_return_val_if_fail(num > 0, NULL);
302 path = folder_item_get_path(item);
303 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
305 if (!is_file_exist(file)) {
313 MsgInfo *mh_fetch_msginfo(Folder *folder, FolderItem *item, gint num)
321 g_return_val_if_fail(item != NULL, NULL);
322 g_return_val_if_fail(num > 0, NULL);
324 path = folder_item_get_path(item);
325 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
327 if (!is_file_exist(file)) {
332 folder_item_set_default_flags(item, &flags);
333 msginfo = procheader_parse_file(file, flags, TRUE, FALSE);
339 msginfo->msgnum = num;
340 msginfo->folder = item;
342 if (stat(file, &s) < 0) {
343 FILE_OP_ERROR(file, "stat");
347 msginfo->size = s.st_size;
348 msginfo->mtime = s.st_mtime;
356 gchar *mh_get_new_msg_filename(FolderItem *dest)
361 destpath = folder_item_get_path(dest);
362 g_return_val_if_fail(destpath != NULL, NULL);
364 if (!is_dir_exist(destpath))
365 make_dir_hier(destpath);
368 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
370 if (is_file_entry_exist(destfile)) {
382 #define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
384 MsgInfo newmsginfo; \
386 newmsginfo.msgnum = dest->last_num; \
387 newmsginfo.flags = msginfo->flags; \
388 if (dest->stype == F_OUTBOX || \
389 dest->stype == F_QUEUE || \
390 dest->stype == F_DRAFT || \
391 dest->stype == F_TRASH) \
392 MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
393 MSG_NEW|MSG_UNREAD|MSG_DELETED); \
395 procmsg_write_flags(&newmsginfo, fp); \
398 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
399 gboolean remove_source)
403 g_return_val_if_fail(dest != NULL, -1);
404 g_return_val_if_fail(file != NULL, -1);
406 if (dest->last_num < 0) {
407 mh_get_last_num(folder, dest);
408 if (dest->last_num < 0) return -1;
411 destfile = mh_get_new_msg_filename(dest);
412 g_return_val_if_fail(destfile != NULL, -1);
414 if (link(file, destfile) < 0) {
415 if (copy_file(file, destfile, TRUE) < 0) {
416 g_warning(_("can't copy message %s to %s\n"),
424 if (unlink(file) < 0)
425 FILE_OP_ERROR(file, "unlink");
430 return dest->last_num;
433 static gint mh_do_move(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
438 PrefsFolderItem *prefs;
440 g_return_val_if_fail(dest != NULL, -1);
441 g_return_val_if_fail(msginfo != NULL, -1);
443 if (msginfo->folder == dest) {
444 g_warning(_("the src folder is identical to the dest.\n"));
448 if (dest->last_num < 0) {
449 mh_get_last_num(folder, dest);
450 if (dest->last_num < 0) return -1;
455 destfile = mh_get_new_msg_filename(dest);
456 if (!destfile) return -1;
458 srcfile = procmsg_get_message_file(msginfo);
460 debug_print("Moving message %s%c%d to %s ...\n",
461 msginfo->folder->path, G_DIR_SEPARATOR,
462 msginfo->msgnum, dest->path);
464 if (move_file(srcfile, destfile, FALSE) < 0) {
470 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
471 if (chmod(destfile, prefs->folder_chmod) < 0)
472 FILE_OP_ERROR(destfile, "chmod");
475 filemode = prefs->folder_chmod;
476 if (filemode & S_IRGRP) filemode |= S_IWGRP;
477 if (filemode & S_IROTH) filemode |= S_IWOTH;
484 return dest->last_num;
487 gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
492 g_return_val_if_fail(folder != NULL, -1);
493 g_return_val_if_fail(dest != NULL, -1);
494 g_return_val_if_fail(msginfo != NULL, -1);
495 g_return_val_if_fail(msginfo->folder != NULL, -1);
497 if (folder == msginfo->folder->folder)
498 return mh_do_move(folder, dest, msginfo);
500 srcfile = procmsg_get_message_file(msginfo);
501 if (!srcfile) return -1;
503 ret = mh_add_msg(folder, dest, srcfile, FALSE);
510 destdir = folder_item_get_path(dest);
511 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
512 g_warning(_("Can't open mark file.\n"));
514 SET_DEST_MSG_FLAGS(fp, dest, msginfo);
519 ret = folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
525 static gint mh_do_move_msgs_with_dest(Folder *folder, FolderItem *dest,
532 PrefsFolderItem *prefs;
534 g_return_val_if_fail(dest != NULL, -1);
535 g_return_val_if_fail(msglist != NULL, -1);
537 if (dest->last_num < 0) {
538 mh_get_last_num(folder, dest);
539 if (dest->last_num < 0) return -1;
544 for (cur = msglist; cur != NULL; cur = cur->next) {
545 msginfo = (MsgInfo *)cur->data;
547 if (msginfo->folder == dest) {
548 g_warning(_("the src folder is identical to the dest.\n"));
551 debug_print("Moving message %s%c%d to %s ...\n",
552 msginfo->folder->path, G_DIR_SEPARATOR,
553 msginfo->msgnum, dest->path);
555 destfile = mh_get_new_msg_filename(dest);
556 if (!destfile) return -1;
557 srcfile = procmsg_get_message_file(msginfo);
559 if (move_file(srcfile, destfile, FALSE) < 0) {
570 return dest->last_num;
573 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
579 msginfo = (MsgInfo *)msglist->data;
580 if (folder == msginfo->folder->folder)
581 return mh_do_move_msgs_with_dest(folder, dest, msglist);
583 for (cur = msglist; cur != NULL; cur = cur->next) {
584 msginfo = (MsgInfo *)cur->data;
585 ret = mh_move_msg(folder, dest, msginfo);
586 if (ret == -1) break;
592 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
597 PrefsFolderItem *prefs;
599 g_return_val_if_fail(dest != NULL, -1);
600 g_return_val_if_fail(msginfo != NULL, -1);
602 if (msginfo->folder == dest) {
603 g_warning(_("the src folder is identical to the dest.\n"));
607 if (dest->last_num < 0) {
608 mh_get_last_num(folder, dest);
609 if (dest->last_num < 0) return -1;
614 srcfile = procmsg_get_message_file(msginfo);
615 destfile = mh_get_new_msg_filename(dest);
621 debug_print("Copying message %s%c%d to %s ...\n",
622 msginfo->folder->path, G_DIR_SEPARATOR,
623 msginfo->msgnum, dest->path);
626 if (copy_file(srcfile, destfile, TRUE) < 0) {
627 FILE_OP_ERROR(srcfile, "copy");
633 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
634 if (chmod(destfile, prefs->folder_chmod) < 0)
635 FILE_OP_ERROR(destfile, "chmod");
638 filemode = prefs->folder_chmod;
639 if (filemode & S_IRGRP) filemode |= S_IWGRP;
640 if (filemode & S_IROTH) filemode |= S_IWOTH;
647 return dest->last_num;
651 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
659 src_folder = msginfo->folder->folder;
661 g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
663 filename = src_folder->fetch_msg(src_folder,
666 if (filename == NULL)
669 num = folder->add_msg(folder, dest, filename, FALSE);
671 destdir = folder_item_get_path(dest);
676 newmsginfo.msgnum = dest->last_num;
677 newmsginfo.flags = msginfo->flags;
678 if (dest->stype == F_OUTBOX ||
679 dest->stype == F_QUEUE ||
680 dest->stype == F_DRAFT ||
681 dest->stype == F_TRASH)
682 MSG_UNSET_FLAGS(newmsginfo.flags,
683 MSG_NEW|MSG_UNREAD|MSG_DELETED);
685 procmsg_write_flags(&newmsginfo, fp);
693 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
700 g_return_val_if_fail(dest != NULL, -1);
701 g_return_val_if_fail(msglist != NULL, -1);
703 if (dest->last_num < 0) {
704 mh_get_last_num(folder, dest);
705 if (dest->last_num < 0) return -1;
708 for (cur = msglist; cur != NULL; cur = cur->next) {
709 msginfo = (MsgInfo *)cur->data;
711 if (msginfo->folder == dest) {
712 g_warning(_("the src folder is identical to the dest.\n"));
715 debug_print("Copying message %s%c%d to %s ...\n",
716 msginfo->folder->path, G_DIR_SEPARATOR,
717 msginfo->msgnum, dest->path);
719 destfile = mh_get_new_msg_filename(dest);
720 if (!destfile) break;
721 srcfile = procmsg_get_message_file(msginfo);
723 if (copy_file(srcfile, destfile, TRUE) < 0) {
724 FILE_OP_ERROR(srcfile, "copy");
735 return dest->last_num;
738 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
742 g_return_val_if_fail(item != NULL, -1);
744 file = mh_fetch_msg(folder, item, num);
745 g_return_val_if_fail(file != NULL, -1);
747 if (unlink(file) < 0) {
748 FILE_OP_ERROR(file, "unlink");
757 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
762 g_return_val_if_fail(item != NULL, -1);
764 path = folder_item_get_path(item);
765 g_return_val_if_fail(path != NULL, -1);
766 val = remove_all_numbered_files(path);
772 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
776 if (stat(itos(msginfo->msgnum), &s) < 0 ||
777 msginfo->size != s.st_size ||
778 msginfo->mtime != s.st_mtime)
784 gint mh_scan_folder(Folder *folder, FolderItem *item)
793 g_return_val_if_fail(item != NULL, -1);
795 debug_print("mh_scan_folder(): Scanning %s ...\n", item->path);
797 path = folder_item_get_path(item);
798 g_return_val_if_fail(path != NULL, -1);
799 if (change_dir(path) < 0) {
805 if ((dp = opendir(".")) == NULL) {
806 FILE_OP_ERROR(item->path, "opendir");
811 folder->ui_func(folder, item, folder->ui_func_data);
813 while ((d = readdir(dp)) != NULL) {
814 if ((num = to_number(d->d_name)) >= 0 &&
815 stat(d->d_name, &s) == 0 &&
816 S_ISREG(s.st_mode)) {
828 item->new = item->unread = item->total = 0;
830 gint new, unread, total, min, max;
832 procmsg_get_mark_sum(".", &new, &unread, &total, &min, &max, 0);
834 new += n_msg - total;
835 unread += n_msg - total;
838 item->unread = unread;
842 debug_print("Last number in dir %s = %d\n", item->path, max);
843 item->last_num = max;
848 void mh_scan_tree(Folder *folder)
853 g_return_if_fail(folder != NULL);
855 item = folder_item_new(folder, folder->name, NULL);
856 item->folder = folder;
857 folder->node = g_node_new(item);
859 rootpath = folder_item_get_path(item);
860 if (change_dir(rootpath) < 0) {
866 mh_create_tree(folder);
867 mh_scan_tree_recursive(item);
870 #define MAKE_DIR_IF_NOT_EXIST(dir) \
872 if (!is_dir_exist(dir)) { \
873 if (is_file_exist(dir)) { \
874 g_warning(_("File `%s' already exists.\n" \
875 "Can't create folder."), dir); \
878 if (make_dir(dir) < 0) \
883 gint mh_create_tree(Folder *folder)
887 g_return_val_if_fail(folder != NULL, -1);
889 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
890 rootpath = LOCAL_FOLDER(folder)->rootpath;
891 MAKE_DIR_IF_NOT_EXIST(rootpath);
892 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
893 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
894 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
895 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
896 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
897 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
902 #undef MAKE_DIR_IF_NOT_EXIST
904 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
909 FolderItem *new_item;
911 g_return_val_if_fail(folder != NULL, NULL);
912 g_return_val_if_fail(parent != NULL, NULL);
913 g_return_val_if_fail(name != NULL, NULL);
915 path = folder_item_get_path(parent);
916 if (!is_dir_exist(path))
919 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
922 if (make_dir(fullpath) < 0) {
930 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
933 path = g_strdup(name);
934 new_item = folder_item_new(folder, name, path);
935 folder_item_append(parent, new_item);
941 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
949 g_return_val_if_fail(folder != NULL, -1);
950 g_return_val_if_fail(item != NULL, -1);
951 g_return_val_if_fail(item->path != NULL, -1);
952 g_return_val_if_fail(name != NULL, -1);
954 oldpath = folder_item_get_path(item);
955 if (!is_dir_exist(oldpath))
956 make_dir_hier(oldpath);
958 dirname = g_dirname(oldpath);
959 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
962 if (rename(oldpath, newpath) < 0) {
963 FILE_OP_ERROR(oldpath, "rename");
972 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
973 dirname = g_dirname(item->path);
974 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
977 newpath = g_strdup(name);
980 item->name = g_strdup(name);
982 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
984 paths[0] = g_strdup(item->path);
986 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
987 mh_rename_folder_func, paths);
994 gint mh_remove_folder(Folder *folder, FolderItem *item)
998 g_return_val_if_fail(folder != NULL, -1);
999 g_return_val_if_fail(item != NULL, -1);
1000 g_return_val_if_fail(item->path != NULL, -1);
1002 path = folder_item_get_path(item);
1003 if (remove_dir_recursive(path) < 0) {
1004 g_warning("can't remove directory `%s'\n", path);
1010 folder_item_remove(item);
1015 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
1021 GSList *newlist = NULL;
1022 GSList *last = NULL;
1027 g_return_val_if_fail(item != NULL, NULL);
1029 path = folder_item_get_path(item);
1030 g_return_val_if_fail(path != NULL, NULL);
1031 if (change_dir(path) < 0) {
1037 if ((dp = opendir(".")) == NULL) {
1038 FILE_OP_ERROR(item->path, "opendir");
1042 debug_print("\tSearching uncached messages... ");
1045 while ((d = readdir(dp)) != NULL) {
1046 if ((num = to_number(d->d_name)) < 0) continue;
1047 if (stat(d->d_name, &s) < 0) {
1048 FILE_OP_ERROR(d->d_name, "stat");
1051 if (!S_ISREG(s.st_mode)) continue;
1053 msginfo = g_hash_table_lookup
1054 (msg_table, GUINT_TO_POINTER(num));
1057 /* not found in the cache (uncached message) */
1058 msginfo = mh_parse_msg(d->d_name, item);
1059 if (!msginfo) continue;
1063 g_slist_append(NULL, msginfo);
1065 last = g_slist_append(last, msginfo);
1072 /* discard all previous cache */
1073 while ((d = readdir(dp)) != NULL) {
1074 if (to_number(d->d_name) < 0) continue;
1075 if (stat(d->d_name, &s) < 0) {
1076 FILE_OP_ERROR(d->d_name, "stat");
1079 if (!S_ISREG(s.st_mode)) continue;
1081 msginfo = mh_parse_msg(d->d_name, item);
1082 if (!msginfo) continue;
1085 last = newlist = g_slist_append(NULL, msginfo);
1087 last = g_slist_append(last, msginfo);
1097 debug_print("%d uncached message(s) found.\n", n_newmsg);
1099 debug_print("done.\n");
1101 /* sort new messages in numerical order */
1103 debug_print("\tSorting uncached messages in numerical order... ");
1104 newlist = g_slist_sort
1105 (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
1106 debug_print("done.\n");
1112 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1118 flags.perm_flags = MSG_NEW|MSG_UNREAD;
1119 flags.tmp_flags = 0;
1121 g_return_val_if_fail(item != NULL, NULL);
1122 g_return_val_if_fail(file != NULL, NULL);
1124 if (item->stype == F_QUEUE) {
1125 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1126 } else if (item->stype == F_DRAFT) {
1127 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1130 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1131 if (!msginfo) return NULL;
1133 msginfo->msgnum = atoi(file);
1134 msginfo->folder = item;
1136 if (stat(file, &s) < 0) {
1137 FILE_OP_ERROR(file, "stat");
1141 msginfo->size = s.st_size;
1142 msginfo->mtime = s.st_mtime;
1149 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
1154 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
1155 result = is_dir_exist(entry);
1162 * check whether PATH is a Maildir style mailbox.
1163 * This is the case if the 3 subdir: new, cur, tmp are existing.
1164 * This functon assumes that entry is an directory
1166 static gboolean mh_is_maildir(const gchar *path)
1168 return mh_is_maildir_one(path, "new") &&
1169 mh_is_maildir_one(path, "cur") &&
1170 mh_is_maildir_one(path, "tmp");
1174 static void mh_scan_tree_recursive(FolderItem *item)
1182 g_return_if_fail(item != NULL);
1183 g_return_if_fail(item->folder != NULL);
1185 dp = opendir(item->path ? item->path : ".");
1187 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
1191 debug_print("scanning %s ...\n",
1192 item->path ? item->path
1193 : LOCAL_FOLDER(item->folder)->rootpath);
1194 if (item->folder->ui_func)
1195 item->folder->ui_func(item->folder, item,
1196 item->folder->ui_func_data);
1198 while ((d = readdir(dp)) != NULL) {
1199 if (d->d_name[0] == '.') continue;
1202 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1205 entry = g_strdup(d->d_name);
1207 if (stat(entry, &s) < 0) {
1208 FILE_OP_ERROR(entry, "stat");
1213 if (S_ISDIR(s.st_mode)) {
1214 FolderItem *new_item;
1217 if (mh_is_maildir(entry)) {
1223 new_item = folder_item_new(item->folder, d->d_name, entry);
1224 folder_item_append(item, new_item);
1226 if (!strcmp(d->d_name, INBOX_DIR)) {
1227 new_item->stype = F_INBOX;
1228 item->folder->inbox = new_item;
1229 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
1230 new_item->stype = F_OUTBOX;
1231 item->folder->outbox = new_item;
1232 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
1233 new_item->stype = F_DRAFT;
1234 item->folder->draft = new_item;
1235 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
1236 new_item->stype = F_QUEUE;
1237 item->folder->queue = new_item;
1238 } else if (!strcmp(d->d_name, TRASH_DIR)) {
1239 new_item->stype = F_TRASH;
1240 item->folder->trash = new_item;
1243 mh_scan_tree_recursive(new_item);
1244 } else if (to_number(d->d_name) != -1) n_msg++;
1253 gint new, unread, total, min, max;
1255 procmsg_get_mark_sum(item->path, &new, &unread, &total,
1257 if (n_msg > total) {
1258 new += n_msg - total;
1259 unread += n_msg - total;
1262 item->unread = unread;
1263 item->total = n_msg;
1268 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1270 FolderItem *item = node->data;
1271 gchar **paths = data;
1272 const gchar *oldpath = paths[0];
1273 const gchar *newpath = paths[1];
1275 gchar *new_itempath;
1278 oldpathlen = strlen(oldpath);
1279 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1280 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1284 base = item->path + oldpathlen;
1285 while (*base == G_DIR_SEPARATOR) base++;
1287 new_itempath = g_strdup(newpath);
1289 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1292 item->path = new_itempath;