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 gint mh_get_num_list (Folder *folder,
89 void mh_scan_tree (Folder *folder);
91 gint mh_create_tree (Folder *folder);
92 FolderItem *mh_create_folder (Folder *folder,
95 gint mh_rename_folder (Folder *folder,
98 gint mh_remove_folder (Folder *folder,
101 gchar *mh_get_new_msg_filename (FolderItem *dest);
103 static GSList *mh_get_uncached_msgs (GHashTable *msg_table,
105 static MsgInfo *mh_parse_msg (const gchar *file,
107 static void mh_scan_tree_recursive (FolderItem *item);
109 static gboolean mh_rename_folder_func (GNode *node,
113 Folder *mh_folder_new(const gchar *name, const gchar *path)
117 folder = (Folder *)g_new0(MHFolder, 1);
118 mh_folder_init(folder, name, path);
123 void mh_folder_destroy(Folder *folder)
125 folder_local_folder_destroy(LOCAL_FOLDER(folder));
128 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
132 folder_local_folder_init(folder, name, path);
134 /* folder->get_msg_list = mh_get_msg_list; */
135 folder->fetch_msg = mh_fetch_msg;
136 folder->fetch_msginfo = mh_fetch_msginfo;
137 folder->add_msg = mh_add_msg;
138 folder->move_msg = mh_move_msg;
139 folder->move_msgs_with_dest = mh_move_msgs_with_dest;
140 folder->copy_msg = mh_copy_msg;
141 folder->copy_msgs_with_dest = mh_copy_msgs_with_dest;
142 folder->remove_msg = mh_remove_msg;
143 folder->remove_all_msg = mh_remove_all_msg;
144 folder->is_msg_changed = mh_is_msg_changed;
145 /* folder->scan = mh_scan_folder; */
146 folder->get_num_list = mh_get_num_list;
147 folder->scan_tree = mh_scan_tree;
148 folder->create_tree = mh_create_tree;
149 folder->create_folder = mh_create_folder;
150 folder->rename_folder = mh_rename_folder;
151 folder->remove_folder = mh_remove_folder;
152 folder->destroy = mh_folder_destroy;
155 void mh_get_last_num(Folder *folder, FolderItem *item)
164 g_return_if_fail(item != NULL);
166 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
168 path = folder_item_get_path(item);
169 g_return_if_fail(path != NULL);
170 if (change_dir(path) < 0) {
176 if ((dp = opendir(".")) == NULL) {
177 FILE_OP_ERROR(item->path, "opendir");
181 while ((d = readdir(dp)) != NULL) {
182 if ((num = to_number(d->d_name)) >= 0 &&
183 stat(d->d_name, &s) == 0 &&
184 S_ISREG(s.st_mode)) {
191 debug_print("Last number in dir %s = %d\n", item->path, max);
192 item->last_num = max;
195 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list)
202 gint num, nummsgs = 0;
204 g_return_val_if_fail(item != NULL, -1);
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, -1);
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));
234 GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
237 GHashTable *msg_table;
240 gboolean scan_new = TRUE;
242 struct timeval tv_before, tv_after, tv_result;
244 gettimeofday(&tv_before, NULL);
247 g_return_val_if_fail(item != NULL, NULL);
249 path = folder_item_get_path(item);
250 if (stat(path, &s) < 0) {
251 FILE_OP_ERROR(path, "stat");
255 mtime = MAX(s.st_mtime, s.st_ctime);
256 if (item->mtime == mtime) {
257 debug_print("Folder is not modified.\n");
264 if (use_cache && !scan_new) {
265 mlist = procmsg_read_cache(item, FALSE);
267 mlist = mh_get_uncached_msgs(NULL, item);
268 } else if (use_cache) {
271 mlist = procmsg_read_cache(item, TRUE);
272 msg_table = procmsg_msg_hash_table_create(mlist);
274 newlist = mh_get_uncached_msgs(msg_table, item);
276 g_hash_table_destroy(msg_table);
278 mlist = g_slist_concat(mlist, newlist);
280 mlist = mh_get_uncached_msgs(NULL, item);
282 procmsg_set_flags(mlist, item);
285 gettimeofday(&tv_after, NULL);
287 timersub(&tv_after, &tv_before, &tv_result);
288 g_print("mh_get_msg_list: %s: elapsed time: %ld.%06ld sec\n",
289 item->path, tv_result.tv_sec, tv_result.tv_usec);
295 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
300 g_return_val_if_fail(item != NULL, NULL);
301 g_return_val_if_fail(num > 0, NULL);
303 path = folder_item_get_path(item);
304 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
306 if (!is_file_exist(file)) {
314 MsgInfo *mh_fetch_msginfo(Folder *folder, FolderItem *item, gint num)
322 g_return_val_if_fail(item != NULL, NULL);
323 g_return_val_if_fail(num > 0, NULL);
325 path = folder_item_get_path(item);
326 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
328 if (!is_file_exist(file)) {
333 folder_item_set_default_flags(item, &flags);
334 msginfo = procheader_parse_file(file, flags, TRUE, FALSE);
340 msginfo->msgnum = num;
341 msginfo->folder = item;
343 if (stat(file, &s) < 0) {
344 FILE_OP_ERROR(file, "stat");
348 msginfo->size = s.st_size;
349 msginfo->mtime = s.st_mtime;
357 gchar *mh_get_new_msg_filename(FolderItem *dest)
362 destpath = folder_item_get_path(dest);
363 g_return_val_if_fail(destpath != NULL, NULL);
365 if (!is_dir_exist(destpath))
366 make_dir_hier(destpath);
369 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
371 if (is_file_entry_exist(destfile)) {
383 #define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
385 MsgInfo newmsginfo; \
387 newmsginfo.msgnum = dest->last_num; \
388 newmsginfo.flags = msginfo->flags; \
389 if (dest->stype == F_OUTBOX || \
390 dest->stype == F_QUEUE || \
391 dest->stype == F_DRAFT || \
392 dest->stype == F_TRASH) \
393 MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
394 MSG_NEW|MSG_UNREAD|MSG_DELETED); \
396 procmsg_write_flags(&newmsginfo, fp); \
399 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
400 gboolean remove_source)
404 g_return_val_if_fail(dest != NULL, -1);
405 g_return_val_if_fail(file != NULL, -1);
407 if (dest->last_num < 0) {
408 mh_get_last_num(folder, dest);
409 if (dest->last_num < 0) return -1;
412 destfile = mh_get_new_msg_filename(dest);
413 g_return_val_if_fail(destfile != NULL, -1);
415 if (link(file, destfile) < 0) {
416 if (copy_file(file, destfile, TRUE) < 0) {
417 g_warning(_("can't copy message %s to %s\n"),
425 if (unlink(file) < 0)
426 FILE_OP_ERROR(file, "unlink");
431 return dest->last_num;
434 static gint mh_do_move(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
439 PrefsFolderItem *prefs;
441 g_return_val_if_fail(dest != NULL, -1);
442 g_return_val_if_fail(msginfo != NULL, -1);
444 if (msginfo->folder == dest) {
445 g_warning(_("the src folder is identical to the dest.\n"));
449 if (dest->last_num < 0) {
450 mh_get_last_num(folder, dest);
451 if (dest->last_num < 0) return -1;
456 destfile = mh_get_new_msg_filename(dest);
457 if (!destfile) return -1;
459 srcfile = procmsg_get_message_file(msginfo);
461 debug_print("Moving message %s%c%d to %s ...\n",
462 msginfo->folder->path, G_DIR_SEPARATOR,
463 msginfo->msgnum, dest->path);
465 if (move_file(srcfile, destfile, FALSE) < 0) {
471 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
472 if (chmod(destfile, prefs->folder_chmod) < 0)
473 FILE_OP_ERROR(destfile, "chmod");
476 filemode = prefs->folder_chmod;
477 if (filemode & S_IRGRP) filemode |= S_IWGRP;
478 if (filemode & S_IROTH) filemode |= S_IWOTH;
485 return dest->last_num;
488 gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
493 g_return_val_if_fail(folder != NULL, -1);
494 g_return_val_if_fail(dest != NULL, -1);
495 g_return_val_if_fail(msginfo != NULL, -1);
496 g_return_val_if_fail(msginfo->folder != NULL, -1);
498 if (folder == msginfo->folder->folder)
499 return mh_do_move(folder, dest, msginfo);
501 srcfile = procmsg_get_message_file(msginfo);
502 if (!srcfile) return -1;
504 ret = mh_add_msg(folder, dest, srcfile, FALSE);
511 destdir = folder_item_get_path(dest);
512 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
513 g_warning(_("Can't open mark file.\n"));
515 SET_DEST_MSG_FLAGS(fp, dest, msginfo);
520 ret = folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
526 static gint mh_do_move_msgs_with_dest(Folder *folder, FolderItem *dest,
533 PrefsFolderItem *prefs;
535 g_return_val_if_fail(dest != NULL, -1);
536 g_return_val_if_fail(msglist != NULL, -1);
538 if (dest->last_num < 0) {
539 mh_get_last_num(folder, dest);
540 if (dest->last_num < 0) return -1;
545 for (cur = msglist; cur != NULL; cur = cur->next) {
546 msginfo = (MsgInfo *)cur->data;
548 if (msginfo->folder == dest) {
549 g_warning(_("the src folder is identical to the dest.\n"));
552 debug_print("Moving message %s%c%d to %s ...\n",
553 msginfo->folder->path, G_DIR_SEPARATOR,
554 msginfo->msgnum, dest->path);
556 destfile = mh_get_new_msg_filename(dest);
557 if (!destfile) return -1;
558 srcfile = procmsg_get_message_file(msginfo);
560 if (move_file(srcfile, destfile, FALSE) < 0) {
571 return dest->last_num;
574 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
580 msginfo = (MsgInfo *)msglist->data;
581 if (folder == msginfo->folder->folder)
582 return mh_do_move_msgs_with_dest(folder, dest, msglist);
584 for (cur = msglist; cur != NULL; cur = cur->next) {
585 msginfo = (MsgInfo *)cur->data;
586 ret = mh_move_msg(folder, dest, msginfo);
587 if (ret == -1) break;
593 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
598 PrefsFolderItem *prefs;
600 g_return_val_if_fail(dest != NULL, -1);
601 g_return_val_if_fail(msginfo != NULL, -1);
603 if (msginfo->folder == dest) {
604 g_warning(_("the src folder is identical to the dest.\n"));
608 if (dest->last_num < 0) {
609 mh_get_last_num(folder, dest);
610 if (dest->last_num < 0) return -1;
615 srcfile = procmsg_get_message_file(msginfo);
616 destfile = mh_get_new_msg_filename(dest);
622 debug_print("Copying message %s%c%d to %s ...\n",
623 msginfo->folder->path, G_DIR_SEPARATOR,
624 msginfo->msgnum, dest->path);
627 if (copy_file(srcfile, destfile, TRUE) < 0) {
628 FILE_OP_ERROR(srcfile, "copy");
634 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
635 if (chmod(destfile, prefs->folder_chmod) < 0)
636 FILE_OP_ERROR(destfile, "chmod");
639 filemode = prefs->folder_chmod;
640 if (filemode & S_IRGRP) filemode |= S_IWGRP;
641 if (filemode & S_IROTH) filemode |= S_IWOTH;
648 return dest->last_num;
652 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
660 src_folder = msginfo->folder->folder;
662 g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
664 filename = src_folder->fetch_msg(src_folder,
667 if (filename == NULL)
670 num = folder->add_msg(folder, dest, filename, FALSE);
672 destdir = folder_item_get_path(dest);
677 newmsginfo.msgnum = dest->last_num;
678 newmsginfo.flags = msginfo->flags;
679 if (dest->stype == F_OUTBOX ||
680 dest->stype == F_QUEUE ||
681 dest->stype == F_DRAFT ||
682 dest->stype == F_TRASH)
683 MSG_UNSET_FLAGS(newmsginfo.flags,
684 MSG_NEW|MSG_UNREAD|MSG_DELETED);
686 procmsg_write_flags(&newmsginfo, fp);
694 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
701 g_return_val_if_fail(dest != NULL, -1);
702 g_return_val_if_fail(msglist != NULL, -1);
704 if (dest->last_num < 0) {
705 mh_get_last_num(folder, dest);
706 if (dest->last_num < 0) return -1;
709 for (cur = msglist; cur != NULL; cur = cur->next) {
710 msginfo = (MsgInfo *)cur->data;
712 if (msginfo->folder == dest) {
713 g_warning(_("the src folder is identical to the dest.\n"));
716 debug_print("Copying message %s%c%d to %s ...\n",
717 msginfo->folder->path, G_DIR_SEPARATOR,
718 msginfo->msgnum, dest->path);
720 destfile = mh_get_new_msg_filename(dest);
721 if (!destfile) break;
722 srcfile = procmsg_get_message_file(msginfo);
724 if (copy_file(srcfile, destfile, TRUE) < 0) {
725 FILE_OP_ERROR(srcfile, "copy");
736 return dest->last_num;
739 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
743 g_return_val_if_fail(item != NULL, -1);
745 file = mh_fetch_msg(folder, item, num);
746 g_return_val_if_fail(file != NULL, -1);
748 if (unlink(file) < 0) {
749 FILE_OP_ERROR(file, "unlink");
758 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
763 g_return_val_if_fail(item != NULL, -1);
765 path = folder_item_get_path(item);
766 g_return_val_if_fail(path != NULL, -1);
767 val = remove_all_numbered_files(path);
773 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
777 if (stat(itos(msginfo->msgnum), &s) < 0 ||
778 msginfo->size != s.st_size ||
779 msginfo->mtime != s.st_mtime)
785 gint mh_scan_folder(Folder *folder, FolderItem *item)
794 g_return_val_if_fail(item != NULL, -1);
796 debug_print("mh_scan_folder(): Scanning %s ...\n", item->path);
798 path = folder_item_get_path(item);
799 g_return_val_if_fail(path != NULL, -1);
800 if (change_dir(path) < 0) {
806 if ((dp = opendir(".")) == NULL) {
807 FILE_OP_ERROR(item->path, "opendir");
812 folder->ui_func(folder, item, folder->ui_func_data);
814 while ((d = readdir(dp)) != NULL) {
815 if ((num = to_number(d->d_name)) >= 0 &&
816 stat(d->d_name, &s) == 0 &&
817 S_ISREG(s.st_mode)) {
829 item->new = item->unread = item->total = 0;
831 gint new, unread, total, min, max;
833 procmsg_get_mark_sum(".", &new, &unread, &total, &min, &max, 0);
835 new += n_msg - total;
836 unread += n_msg - total;
839 item->unread = unread;
843 debug_print("Last number in dir %s = %d\n", item->path, max);
844 item->last_num = max;
849 void mh_scan_tree(Folder *folder)
854 g_return_if_fail(folder != NULL);
856 item = folder_item_new(folder, folder->name, NULL);
857 item->folder = folder;
858 folder->node = g_node_new(item);
860 rootpath = folder_item_get_path(item);
861 if (change_dir(rootpath) < 0) {
867 mh_create_tree(folder);
868 mh_scan_tree_recursive(item);
871 #define MAKE_DIR_IF_NOT_EXIST(dir) \
873 if (!is_dir_exist(dir)) { \
874 if (is_file_exist(dir)) { \
875 g_warning(_("File `%s' already exists.\n" \
876 "Can't create folder."), dir); \
879 if (make_dir(dir) < 0) \
884 gint mh_create_tree(Folder *folder)
888 g_return_val_if_fail(folder != NULL, -1);
890 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
891 rootpath = LOCAL_FOLDER(folder)->rootpath;
892 MAKE_DIR_IF_NOT_EXIST(rootpath);
893 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
894 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
895 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
896 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
897 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
898 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
903 #undef MAKE_DIR_IF_NOT_EXIST
905 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
910 FolderItem *new_item;
912 g_return_val_if_fail(folder != NULL, NULL);
913 g_return_val_if_fail(parent != NULL, NULL);
914 g_return_val_if_fail(name != NULL, NULL);
916 path = folder_item_get_path(parent);
917 if (!is_dir_exist(path))
920 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
923 if (make_dir(fullpath) < 0) {
931 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
934 path = g_strdup(name);
935 new_item = folder_item_new(folder, name, path);
936 folder_item_append(parent, new_item);
942 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
950 g_return_val_if_fail(folder != NULL, -1);
951 g_return_val_if_fail(item != NULL, -1);
952 g_return_val_if_fail(item->path != NULL, -1);
953 g_return_val_if_fail(name != NULL, -1);
955 oldpath = folder_item_get_path(item);
956 if (!is_dir_exist(oldpath))
957 make_dir_hier(oldpath);
959 dirname = g_dirname(oldpath);
960 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
963 if (rename(oldpath, newpath) < 0) {
964 FILE_OP_ERROR(oldpath, "rename");
973 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
974 dirname = g_dirname(item->path);
975 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
978 newpath = g_strdup(name);
981 item->name = g_strdup(name);
983 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
985 paths[0] = g_strdup(item->path);
987 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
988 mh_rename_folder_func, paths);
995 gint mh_remove_folder(Folder *folder, FolderItem *item)
999 g_return_val_if_fail(folder != NULL, -1);
1000 g_return_val_if_fail(item != NULL, -1);
1001 g_return_val_if_fail(item->path != NULL, -1);
1003 path = folder_item_get_path(item);
1004 if (remove_dir_recursive(path) < 0) {
1005 g_warning("can't remove directory `%s'\n", path);
1011 folder_item_remove(item);
1016 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
1022 GSList *newlist = NULL;
1023 GSList *last = NULL;
1028 g_return_val_if_fail(item != NULL, NULL);
1030 path = folder_item_get_path(item);
1031 g_return_val_if_fail(path != NULL, NULL);
1032 if (change_dir(path) < 0) {
1038 if ((dp = opendir(".")) == NULL) {
1039 FILE_OP_ERROR(item->path, "opendir");
1043 debug_print("\tSearching uncached messages... ");
1046 while ((d = readdir(dp)) != NULL) {
1047 if ((num = to_number(d->d_name)) < 0) continue;
1048 if (stat(d->d_name, &s) < 0) {
1049 FILE_OP_ERROR(d->d_name, "stat");
1052 if (!S_ISREG(s.st_mode)) continue;
1054 msginfo = g_hash_table_lookup
1055 (msg_table, GUINT_TO_POINTER(num));
1058 /* not found in the cache (uncached message) */
1059 msginfo = mh_parse_msg(d->d_name, item);
1060 if (!msginfo) continue;
1064 g_slist_append(NULL, msginfo);
1066 last = g_slist_append(last, msginfo);
1073 /* discard all previous cache */
1074 while ((d = readdir(dp)) != NULL) {
1075 if (to_number(d->d_name) < 0) continue;
1076 if (stat(d->d_name, &s) < 0) {
1077 FILE_OP_ERROR(d->d_name, "stat");
1080 if (!S_ISREG(s.st_mode)) continue;
1082 msginfo = mh_parse_msg(d->d_name, item);
1083 if (!msginfo) continue;
1086 last = newlist = g_slist_append(NULL, msginfo);
1088 last = g_slist_append(last, msginfo);
1098 debug_print("%d uncached message(s) found.\n", n_newmsg);
1100 debug_print("done.\n");
1102 /* sort new messages in numerical order */
1104 debug_print("\tSorting uncached messages in numerical order... ");
1105 newlist = g_slist_sort
1106 (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
1107 debug_print("done.\n");
1113 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1119 flags.perm_flags = MSG_NEW|MSG_UNREAD;
1120 flags.tmp_flags = 0;
1122 g_return_val_if_fail(item != NULL, NULL);
1123 g_return_val_if_fail(file != NULL, NULL);
1125 if (item->stype == F_QUEUE) {
1126 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1127 } else if (item->stype == F_DRAFT) {
1128 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1131 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1132 if (!msginfo) return NULL;
1134 msginfo->msgnum = atoi(file);
1135 msginfo->folder = item;
1137 if (stat(file, &s) < 0) {
1138 FILE_OP_ERROR(file, "stat");
1142 msginfo->size = s.st_size;
1143 msginfo->mtime = s.st_mtime;
1150 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
1155 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
1156 result = is_dir_exist(entry);
1163 * check whether PATH is a Maildir style mailbox.
1164 * This is the case if the 3 subdir: new, cur, tmp are existing.
1165 * This functon assumes that entry is an directory
1167 static gboolean mh_is_maildir(const gchar *path)
1169 return mh_is_maildir_one(path, "new") &&
1170 mh_is_maildir_one(path, "cur") &&
1171 mh_is_maildir_one(path, "tmp");
1175 static void mh_scan_tree_recursive(FolderItem *item)
1183 g_return_if_fail(item != NULL);
1184 g_return_if_fail(item->folder != NULL);
1186 dp = opendir(item->path ? item->path : ".");
1188 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
1192 debug_print("scanning %s ...\n",
1193 item->path ? item->path
1194 : LOCAL_FOLDER(item->folder)->rootpath);
1195 if (item->folder->ui_func)
1196 item->folder->ui_func(item->folder, item,
1197 item->folder->ui_func_data);
1199 while ((d = readdir(dp)) != NULL) {
1200 if (d->d_name[0] == '.') continue;
1203 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1206 entry = g_strdup(d->d_name);
1208 if (stat(entry, &s) < 0) {
1209 FILE_OP_ERROR(entry, "stat");
1214 if (S_ISDIR(s.st_mode)) {
1215 FolderItem *new_item;
1218 if (mh_is_maildir(entry)) {
1224 new_item = folder_item_new(item->folder, d->d_name, entry);
1225 folder_item_append(item, new_item);
1227 if (!strcmp(d->d_name, INBOX_DIR)) {
1228 new_item->stype = F_INBOX;
1229 item->folder->inbox = new_item;
1230 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
1231 new_item->stype = F_OUTBOX;
1232 item->folder->outbox = new_item;
1233 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
1234 new_item->stype = F_DRAFT;
1235 item->folder->draft = new_item;
1236 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
1237 new_item->stype = F_QUEUE;
1238 item->folder->queue = new_item;
1239 } else if (!strcmp(d->d_name, TRASH_DIR)) {
1240 new_item->stype = F_TRASH;
1241 item->folder->trash = new_item;
1244 mh_scan_tree_recursive(new_item);
1245 } else if (to_number(d->d_name) != -1) n_msg++;
1254 gint new, unread, total, min, max;
1256 procmsg_get_mark_sum(item->path, &new, &unread, &total,
1258 if (n_msg > total) {
1259 new += n_msg - total;
1260 unread += n_msg - total;
1263 item->unread = unread;
1264 item->total = n_msg;
1269 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1271 FolderItem *item = node->data;
1272 gchar **paths = data;
1273 const gchar *oldpath = paths[0];
1274 const gchar *newpath = paths[1];
1276 gchar *new_itempath;
1279 oldpathlen = strlen(oldpath);
1280 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1281 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1285 base = item->path + oldpathlen;
1286 while (*base == G_DIR_SEPARATOR) base++;
1288 new_itempath = g_strdup(newpath);
1290 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1293 item->path = new_itempath;