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_get_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->get_msginfo = mh_get_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_get_msginfo(Folder *folder, FolderItem *item, gint num)
319 g_return_val_if_fail(item != NULL, NULL);
320 g_return_val_if_fail(num > 0, NULL);
322 file = mh_fetch_msg(folder, item, num);
323 if (!file) return NULL;
325 msginfo = mh_parse_msg(file, item);
326 msginfo->msgnum = num;
333 gchar *mh_get_new_msg_filename(FolderItem *dest)
338 destpath = folder_item_get_path(dest);
339 g_return_val_if_fail(destpath != NULL, NULL);
341 if (!is_dir_exist(destpath))
342 make_dir_hier(destpath);
345 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
347 if (is_file_entry_exist(destfile)) {
359 #define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
361 MsgInfo newmsginfo; \
363 newmsginfo.msgnum = dest->last_num; \
364 newmsginfo.flags = msginfo->flags; \
365 if (dest->stype == F_OUTBOX || \
366 dest->stype == F_QUEUE || \
367 dest->stype == F_DRAFT || \
368 dest->stype == F_TRASH) \
369 MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
370 MSG_NEW|MSG_UNREAD|MSG_DELETED); \
372 procmsg_write_flags(&newmsginfo, fp); \
375 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
376 gboolean remove_source)
380 g_return_val_if_fail(dest != NULL, -1);
381 g_return_val_if_fail(file != NULL, -1);
383 if (dest->last_num < 0) {
384 mh_get_last_num(folder, dest);
385 if (dest->last_num < 0) return -1;
388 destfile = mh_get_new_msg_filename(dest);
389 g_return_val_if_fail(destfile != NULL, -1);
391 if (link(file, destfile) < 0) {
392 if (copy_file(file, destfile, TRUE) < 0) {
393 g_warning(_("can't copy message %s to %s\n"),
401 if (unlink(file) < 0)
402 FILE_OP_ERROR(file, "unlink");
407 return dest->last_num;
410 static gint mh_do_move(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
415 PrefsFolderItem *prefs;
417 g_return_val_if_fail(dest != NULL, -1);
418 g_return_val_if_fail(msginfo != NULL, -1);
420 if (msginfo->folder == dest) {
421 g_warning(_("the src folder is identical to the dest.\n"));
425 if (dest->last_num < 0) {
426 mh_get_last_num(folder, dest);
427 if (dest->last_num < 0) return -1;
432 destfile = mh_get_new_msg_filename(dest);
433 if (!destfile) return -1;
435 srcfile = procmsg_get_message_file(msginfo);
437 debug_print("Moving message %s%c%d to %s ...\n",
438 msginfo->folder->path, G_DIR_SEPARATOR,
439 msginfo->msgnum, dest->path);
441 if (move_file(srcfile, destfile, FALSE) < 0) {
447 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
448 if (chmod(destfile, prefs->folder_chmod) < 0)
449 FILE_OP_ERROR(destfile, "chmod");
452 filemode = prefs->folder_chmod;
453 if (filemode & S_IRGRP) filemode |= S_IWGRP;
454 if (filemode & S_IROTH) filemode |= S_IWOTH;
461 return dest->last_num;
464 gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
469 g_return_val_if_fail(folder != NULL, -1);
470 g_return_val_if_fail(dest != NULL, -1);
471 g_return_val_if_fail(msginfo != NULL, -1);
472 g_return_val_if_fail(msginfo->folder != NULL, -1);
474 if (folder == msginfo->folder->folder)
475 return mh_do_move(folder, dest, msginfo);
477 srcfile = procmsg_get_message_file(msginfo);
478 if (!srcfile) return -1;
480 ret = mh_add_msg(folder, dest, srcfile, FALSE);
487 destdir = folder_item_get_path(dest);
488 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
489 g_warning(_("Can't open mark file.\n"));
491 SET_DEST_MSG_FLAGS(fp, dest, msginfo);
496 ret = folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
502 static gint mh_do_move_msgs_with_dest(Folder *folder, FolderItem *dest,
509 PrefsFolderItem *prefs;
511 g_return_val_if_fail(dest != NULL, -1);
512 g_return_val_if_fail(msglist != NULL, -1);
514 if (dest->last_num < 0) {
515 mh_get_last_num(folder, dest);
516 if (dest->last_num < 0) return -1;
521 for (cur = msglist; cur != NULL; cur = cur->next) {
522 msginfo = (MsgInfo *)cur->data;
524 if (msginfo->folder == dest) {
525 g_warning(_("the src folder is identical to the dest.\n"));
528 debug_print("Moving message %s%c%d to %s ...\n",
529 msginfo->folder->path, G_DIR_SEPARATOR,
530 msginfo->msgnum, dest->path);
532 destfile = mh_get_new_msg_filename(dest);
533 if (!destfile) return -1;
534 srcfile = procmsg_get_message_file(msginfo);
536 if (move_file(srcfile, destfile, FALSE) < 0) {
547 return dest->last_num;
550 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
556 msginfo = (MsgInfo *)msglist->data;
557 if (folder == msginfo->folder->folder)
558 return mh_do_move_msgs_with_dest(folder, dest, msglist);
560 for (cur = msglist; cur != NULL; cur = cur->next) {
561 msginfo = (MsgInfo *)cur->data;
562 ret = mh_move_msg(folder, dest, msginfo);
563 if (ret == -1) break;
569 static void mh_remove_queue_headers (const gchar *file)
573 fp = fopen(file, "rb");
574 tmp = get_tmp_file();
576 fp2 = fopen(tmp, "wb");
580 while (fgets(buf, sizeof(buf), fp) != NULL)
581 if (buf[0] == '\r' || buf[0] == '\n')
584 while ((len=fread(buf, sizeof(char), sizeof(buf), fp)) > 0) {
585 fwrite(buf, len, 1, fp2);
589 move_file(tmp, file, TRUE);
594 g_warning (_("Couldn't fopen(\"%s\",\"rb\")\n"), file);
598 g_warning (_("Couldn't fopen(\"%s\",\"wb\")\n"), tmp);
603 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
608 PrefsFolderItem *prefs;
610 g_return_val_if_fail(dest != NULL, -1);
611 g_return_val_if_fail(msginfo != NULL, -1);
613 if (msginfo->folder == dest) {
614 g_warning(_("the src folder is identical to the dest.\n"));
618 if (dest->last_num < 0) {
619 mh_get_last_num(folder, dest);
620 if (dest->last_num < 0) return -1;
625 srcfile = procmsg_get_message_file(msginfo);
626 destfile = mh_get_new_msg_filename(dest);
632 debug_print("Copying message %s%c%d to %s ...\n",
633 msginfo->folder->path, G_DIR_SEPARATOR,
634 msginfo->msgnum, dest->path);
637 if (copy_file(srcfile, destfile, TRUE) < 0) {
638 FILE_OP_ERROR(srcfile, "copy");
644 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
645 && dest->stype != F_QUEUE && dest->stype != F_DRAFT)
646 mh_remove_queue_headers(destfile);
648 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
649 if (chmod(destfile, prefs->folder_chmod) < 0)
650 FILE_OP_ERROR(destfile, "chmod");
653 filemode = prefs->folder_chmod;
654 if (filemode & S_IRGRP) filemode |= S_IWGRP;
655 if (filemode & S_IROTH) filemode |= S_IWOTH;
662 return dest->last_num;
666 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
674 src_folder = msginfo->folder->folder;
676 g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
678 filename = src_folder->fetch_msg(src_folder,
681 if (filename == NULL)
684 num = folder->add_msg(folder, dest, filename, FALSE);
686 destdir = folder_item_get_path(dest);
691 newmsginfo.msgnum = dest->last_num;
692 newmsginfo.flags = msginfo->flags;
693 if (dest->stype == F_OUTBOX ||
694 dest->stype == F_QUEUE ||
695 dest->stype == F_DRAFT ||
696 dest->stype == F_TRASH)
697 MSG_UNSET_FLAGS(newmsginfo.flags,
698 MSG_NEW|MSG_UNREAD|MSG_DELETED);
700 procmsg_write_flags(&newmsginfo, fp);
708 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
715 g_return_val_if_fail(dest != NULL, -1);
716 g_return_val_if_fail(msglist != NULL, -1);
718 if (dest->last_num < 0) {
719 mh_get_last_num(folder, dest);
720 if (dest->last_num < 0) return -1;
723 for (cur = msglist; cur != NULL; cur = cur->next) {
724 msginfo = (MsgInfo *)cur->data;
726 if (msginfo->folder == dest) {
727 g_warning(_("the src folder is identical to the dest.\n"));
730 debug_print("Copying message %s%c%d to %s ...\n",
731 msginfo->folder->path, G_DIR_SEPARATOR,
732 msginfo->msgnum, dest->path);
734 destfile = mh_get_new_msg_filename(dest);
735 if (!destfile) break;
736 srcfile = procmsg_get_message_file(msginfo);
738 if (copy_file(srcfile, destfile, TRUE) < 0) {
739 FILE_OP_ERROR(srcfile, "copy");
750 return dest->last_num;
753 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
757 g_return_val_if_fail(item != NULL, -1);
759 file = mh_fetch_msg(folder, item, num);
760 g_return_val_if_fail(file != NULL, -1);
762 if (unlink(file) < 0) {
763 FILE_OP_ERROR(file, "unlink");
772 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
777 g_return_val_if_fail(item != NULL, -1);
779 path = folder_item_get_path(item);
780 g_return_val_if_fail(path != NULL, -1);
781 val = remove_all_numbered_files(path);
787 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
791 if (stat(itos(msginfo->msgnum), &s) < 0 ||
792 msginfo->size != s.st_size ||
793 msginfo->mtime != s.st_mtime)
799 gint mh_scan_folder(Folder *folder, FolderItem *item)
808 g_return_val_if_fail(item != NULL, -1);
810 debug_print("mh_scan_folder(): Scanning %s ...\n", item->path);
812 path = folder_item_get_path(item);
813 g_return_val_if_fail(path != NULL, -1);
814 if (change_dir(path) < 0) {
820 if ((dp = opendir(".")) == NULL) {
821 FILE_OP_ERROR(item->path, "opendir");
826 folder->ui_func(folder, item, folder->ui_func_data);
828 while ((d = readdir(dp)) != NULL) {
829 if ((num = to_number(d->d_name)) >= 0 &&
830 stat(d->d_name, &s) == 0 &&
831 S_ISREG(s.st_mode)) {
843 item->new = item->unread = item->total = 0;
845 gint new, unread, total, min, max;
847 procmsg_get_mark_sum(".", &new, &unread, &total, &min, &max, 0);
849 new += n_msg - total;
850 unread += n_msg - total;
853 item->unread = unread;
857 debug_print("Last number in dir %s = %d\n", item->path, max);
858 item->last_num = max;
863 void mh_scan_tree(Folder *folder)
868 g_return_if_fail(folder != NULL);
870 item = folder_item_new(folder, folder->name, NULL);
871 item->folder = folder;
872 folder->node = g_node_new(item);
874 rootpath = folder_item_get_path(item);
875 if (change_dir(rootpath) < 0) {
881 mh_create_tree(folder);
882 mh_scan_tree_recursive(item);
885 #define MAKE_DIR_IF_NOT_EXIST(dir) \
887 if (!is_dir_exist(dir)) { \
888 if (is_file_exist(dir)) { \
889 g_warning(_("File `%s' already exists.\n" \
890 "Can't create folder."), dir); \
893 if (make_dir(dir) < 0) \
898 gint mh_create_tree(Folder *folder)
902 g_return_val_if_fail(folder != NULL, -1);
904 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
905 rootpath = LOCAL_FOLDER(folder)->rootpath;
906 MAKE_DIR_IF_NOT_EXIST(rootpath);
907 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
908 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
909 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
910 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
911 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
912 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
917 #undef MAKE_DIR_IF_NOT_EXIST
919 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
924 FolderItem *new_item;
926 g_return_val_if_fail(folder != NULL, NULL);
927 g_return_val_if_fail(parent != NULL, NULL);
928 g_return_val_if_fail(name != NULL, NULL);
930 path = folder_item_get_path(parent);
931 if (!is_dir_exist(path))
932 if (make_dir_hier(path) != 0)
935 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
938 if (make_dir(fullpath) < 0) {
946 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
949 path = g_strdup(name);
950 new_item = folder_item_new(folder, name, path);
951 folder_item_append(parent, new_item);
957 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
965 g_return_val_if_fail(folder != NULL, -1);
966 g_return_val_if_fail(item != NULL, -1);
967 g_return_val_if_fail(item->path != NULL, -1);
968 g_return_val_if_fail(name != NULL, -1);
970 oldpath = folder_item_get_path(item);
971 if (!is_dir_exist(oldpath))
972 make_dir_hier(oldpath);
974 dirname = g_dirname(oldpath);
975 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
978 if (rename(oldpath, newpath) < 0) {
979 FILE_OP_ERROR(oldpath, "rename");
988 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
989 dirname = g_dirname(item->path);
990 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
993 newpath = g_strdup(name);
996 item->name = g_strdup(name);
998 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
1000 paths[0] = g_strdup(item->path);
1002 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1003 mh_rename_folder_func, paths);
1010 gint mh_remove_folder(Folder *folder, FolderItem *item)
1014 g_return_val_if_fail(folder != NULL, -1);
1015 g_return_val_if_fail(item != NULL, -1);
1016 g_return_val_if_fail(item->path != NULL, -1);
1018 path = folder_item_get_path(item);
1019 if (remove_dir_recursive(path) < 0) {
1020 g_warning("can't remove directory `%s'\n", path);
1026 folder_item_remove(item);
1031 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
1037 GSList *newlist = NULL;
1038 GSList *last = NULL;
1043 g_return_val_if_fail(item != NULL, NULL);
1045 path = folder_item_get_path(item);
1046 g_return_val_if_fail(path != NULL, NULL);
1047 if (change_dir(path) < 0) {
1053 if ((dp = opendir(".")) == NULL) {
1054 FILE_OP_ERROR(item->path, "opendir");
1058 debug_print("\tSearching uncached messages... ");
1061 while ((d = readdir(dp)) != NULL) {
1062 if ((num = to_number(d->d_name)) < 0) continue;
1063 if (stat(d->d_name, &s) < 0) {
1064 FILE_OP_ERROR(d->d_name, "stat");
1067 if (!S_ISREG(s.st_mode)) continue;
1069 msginfo = g_hash_table_lookup
1070 (msg_table, GUINT_TO_POINTER(num));
1073 /* not found in the cache (uncached message) */
1074 msginfo = mh_parse_msg(d->d_name, item);
1075 if (!msginfo) continue;
1079 g_slist_append(NULL, msginfo);
1081 last = g_slist_append(last, msginfo);
1088 /* discard all previous cache */
1089 while ((d = readdir(dp)) != NULL) {
1090 if (to_number(d->d_name) < 0) continue;
1091 if (stat(d->d_name, &s) < 0) {
1092 FILE_OP_ERROR(d->d_name, "stat");
1095 if (!S_ISREG(s.st_mode)) continue;
1097 msginfo = mh_parse_msg(d->d_name, item);
1098 if (!msginfo) continue;
1101 last = newlist = g_slist_append(NULL, msginfo);
1103 last = g_slist_append(last, msginfo);
1113 debug_print("%d uncached message(s) found.\n", n_newmsg);
1115 debug_print("done.\n");
1117 /* sort new messages in numerical order */
1119 debug_print("\tSorting uncached messages in numerical order... ");
1120 newlist = g_slist_sort
1121 (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
1122 debug_print("done.\n");
1128 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1134 flags.perm_flags = MSG_NEW|MSG_UNREAD;
1135 flags.tmp_flags = 0;
1137 g_return_val_if_fail(item != NULL, NULL);
1138 g_return_val_if_fail(file != NULL, NULL);
1140 if (item->stype == F_QUEUE) {
1141 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1142 } else if (item->stype == F_DRAFT) {
1143 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1146 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1147 if (!msginfo) return NULL;
1149 msginfo->msgnum = atoi(file);
1150 msginfo->folder = item;
1152 if (stat(file, &s) < 0) {
1153 FILE_OP_ERROR(file, "stat");
1157 msginfo->size = s.st_size;
1158 msginfo->mtime = s.st_mtime;
1165 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
1170 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
1171 result = is_dir_exist(entry);
1178 * check whether PATH is a Maildir style mailbox.
1179 * This is the case if the 3 subdir: new, cur, tmp are existing.
1180 * This functon assumes that entry is an directory
1182 static gboolean mh_is_maildir(const gchar *path)
1184 return mh_is_maildir_one(path, "new") &&
1185 mh_is_maildir_one(path, "cur") &&
1186 mh_is_maildir_one(path, "tmp");
1190 static void mh_scan_tree_recursive(FolderItem *item)
1198 g_return_if_fail(item != NULL);
1199 g_return_if_fail(item->folder != NULL);
1201 dp = opendir(item->path ? item->path : ".");
1203 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
1207 debug_print("scanning %s ...\n",
1208 item->path ? item->path
1209 : LOCAL_FOLDER(item->folder)->rootpath);
1210 if (item->folder->ui_func)
1211 item->folder->ui_func(item->folder, item,
1212 item->folder->ui_func_data);
1214 while ((d = readdir(dp)) != NULL) {
1215 if (d->d_name[0] == '.') continue;
1218 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1221 entry = g_strdup(d->d_name);
1223 if (stat(entry, &s) < 0) {
1224 FILE_OP_ERROR(entry, "stat");
1229 if (S_ISDIR(s.st_mode)) {
1230 FolderItem *new_item;
1233 if (mh_is_maildir(entry)) {
1239 new_item = folder_item_new(item->folder, d->d_name, entry);
1240 folder_item_append(item, new_item);
1242 if (!strcmp(d->d_name, INBOX_DIR)) {
1243 new_item->stype = F_INBOX;
1244 item->folder->inbox = new_item;
1245 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
1246 new_item->stype = F_OUTBOX;
1247 item->folder->outbox = new_item;
1248 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
1249 new_item->stype = F_DRAFT;
1250 item->folder->draft = new_item;
1251 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
1252 new_item->stype = F_QUEUE;
1253 item->folder->queue = new_item;
1254 } else if (!strcmp(d->d_name, TRASH_DIR)) {
1255 new_item->stype = F_TRASH;
1256 item->folder->trash = new_item;
1259 mh_scan_tree_recursive(new_item);
1260 } else if (to_number(d->d_name) != -1) n_msg++;
1269 gint new, unread, total, min, max;
1271 procmsg_get_mark_sum(item->path, &new, &unread, &total,
1273 if (n_msg > total) {
1274 new += n_msg - total;
1275 unread += n_msg - total;
1278 item->unread = unread;
1279 item->total = n_msg;
1284 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1286 FolderItem *item = node->data;
1287 gchar **paths = data;
1288 const gchar *oldpath = paths[0];
1289 const gchar *newpath = paths[1];
1291 gchar *new_itempath;
1294 oldpathlen = strlen(oldpath);
1295 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1296 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1300 base = item->path + oldpathlen;
1301 while (*base == G_DIR_SEPARATOR) base++;
1303 new_itempath = g_strdup(newpath);
1305 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1308 item->path = new_itempath;