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 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
574 PrefsFolderItem *prefs;
576 g_return_val_if_fail(dest != NULL, -1);
577 g_return_val_if_fail(msginfo != NULL, -1);
579 if (msginfo->folder == dest) {
580 g_warning(_("the src folder is identical to the dest.\n"));
584 if (dest->last_num < 0) {
585 mh_get_last_num(folder, dest);
586 if (dest->last_num < 0) return -1;
591 srcfile = procmsg_get_message_file(msginfo);
592 destfile = mh_get_new_msg_filename(dest);
598 debug_print("Copying message %s%c%d to %s ...\n",
599 msginfo->folder->path, G_DIR_SEPARATOR,
600 msginfo->msgnum, dest->path);
603 if (copy_file(srcfile, destfile, TRUE) < 0) {
604 FILE_OP_ERROR(srcfile, "copy");
610 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
611 if (chmod(destfile, prefs->folder_chmod) < 0)
612 FILE_OP_ERROR(destfile, "chmod");
615 filemode = prefs->folder_chmod;
616 if (filemode & S_IRGRP) filemode |= S_IWGRP;
617 if (filemode & S_IROTH) filemode |= S_IWOTH;
624 return dest->last_num;
628 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
636 src_folder = msginfo->folder->folder;
638 g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
640 filename = src_folder->fetch_msg(src_folder,
643 if (filename == NULL)
646 num = folder->add_msg(folder, dest, filename, FALSE);
648 destdir = folder_item_get_path(dest);
653 newmsginfo.msgnum = dest->last_num;
654 newmsginfo.flags = msginfo->flags;
655 if (dest->stype == F_OUTBOX ||
656 dest->stype == F_QUEUE ||
657 dest->stype == F_DRAFT ||
658 dest->stype == F_TRASH)
659 MSG_UNSET_FLAGS(newmsginfo.flags,
660 MSG_NEW|MSG_UNREAD|MSG_DELETED);
662 procmsg_write_flags(&newmsginfo, fp);
670 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
677 g_return_val_if_fail(dest != NULL, -1);
678 g_return_val_if_fail(msglist != NULL, -1);
680 if (dest->last_num < 0) {
681 mh_get_last_num(folder, dest);
682 if (dest->last_num < 0) return -1;
685 for (cur = msglist; cur != NULL; cur = cur->next) {
686 msginfo = (MsgInfo *)cur->data;
688 if (msginfo->folder == dest) {
689 g_warning(_("the src folder is identical to the dest.\n"));
692 debug_print("Copying message %s%c%d to %s ...\n",
693 msginfo->folder->path, G_DIR_SEPARATOR,
694 msginfo->msgnum, dest->path);
696 destfile = mh_get_new_msg_filename(dest);
697 if (!destfile) break;
698 srcfile = procmsg_get_message_file(msginfo);
700 if (copy_file(srcfile, destfile, TRUE) < 0) {
701 FILE_OP_ERROR(srcfile, "copy");
712 return dest->last_num;
715 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
719 g_return_val_if_fail(item != NULL, -1);
721 file = mh_fetch_msg(folder, item, num);
722 g_return_val_if_fail(file != NULL, -1);
724 if (unlink(file) < 0) {
725 FILE_OP_ERROR(file, "unlink");
734 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
739 g_return_val_if_fail(item != NULL, -1);
741 path = folder_item_get_path(item);
742 g_return_val_if_fail(path != NULL, -1);
743 val = remove_all_numbered_files(path);
749 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
753 if (stat(itos(msginfo->msgnum), &s) < 0 ||
754 msginfo->size != s.st_size ||
755 msginfo->mtime != s.st_mtime)
761 gint mh_scan_folder(Folder *folder, FolderItem *item)
770 g_return_val_if_fail(item != NULL, -1);
772 debug_print("mh_scan_folder(): Scanning %s ...\n", item->path);
774 path = folder_item_get_path(item);
775 g_return_val_if_fail(path != NULL, -1);
776 if (change_dir(path) < 0) {
782 if ((dp = opendir(".")) == NULL) {
783 FILE_OP_ERROR(item->path, "opendir");
788 folder->ui_func(folder, item, folder->ui_func_data);
790 while ((d = readdir(dp)) != NULL) {
791 if ((num = to_number(d->d_name)) >= 0 &&
792 stat(d->d_name, &s) == 0 &&
793 S_ISREG(s.st_mode)) {
805 item->new = item->unread = item->total = 0;
807 gint new, unread, total, min, max;
809 procmsg_get_mark_sum(".", &new, &unread, &total, &min, &max, 0);
811 new += n_msg - total;
812 unread += n_msg - total;
815 item->unread = unread;
819 debug_print("Last number in dir %s = %d\n", item->path, max);
820 item->last_num = max;
825 void mh_scan_tree(Folder *folder)
830 g_return_if_fail(folder != NULL);
832 item = folder_item_new(folder, folder->name, NULL);
833 item->folder = folder;
834 folder->node = g_node_new(item);
836 rootpath = folder_item_get_path(item);
837 if (change_dir(rootpath) < 0) {
843 mh_create_tree(folder);
844 mh_scan_tree_recursive(item);
847 #define MAKE_DIR_IF_NOT_EXIST(dir) \
849 if (!is_dir_exist(dir)) { \
850 if (is_file_exist(dir)) { \
851 g_warning(_("File `%s' already exists.\n" \
852 "Can't create folder."), dir); \
855 if (make_dir(dir) < 0) \
860 gint mh_create_tree(Folder *folder)
864 g_return_val_if_fail(folder != NULL, -1);
866 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
867 rootpath = LOCAL_FOLDER(folder)->rootpath;
868 MAKE_DIR_IF_NOT_EXIST(rootpath);
869 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
870 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
871 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
872 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
873 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
874 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
879 #undef MAKE_DIR_IF_NOT_EXIST
881 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
886 FolderItem *new_item;
888 g_return_val_if_fail(folder != NULL, NULL);
889 g_return_val_if_fail(parent != NULL, NULL);
890 g_return_val_if_fail(name != NULL, NULL);
892 path = folder_item_get_path(parent);
893 if (!is_dir_exist(path))
894 if (make_dir_hier(path) != 0)
897 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
900 if (make_dir(fullpath) < 0) {
908 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
911 path = g_strdup(name);
912 new_item = folder_item_new(folder, name, path);
913 folder_item_append(parent, new_item);
919 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
927 g_return_val_if_fail(folder != NULL, -1);
928 g_return_val_if_fail(item != NULL, -1);
929 g_return_val_if_fail(item->path != NULL, -1);
930 g_return_val_if_fail(name != NULL, -1);
932 oldpath = folder_item_get_path(item);
933 if (!is_dir_exist(oldpath))
934 make_dir_hier(oldpath);
936 dirname = g_dirname(oldpath);
937 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
940 if (rename(oldpath, newpath) < 0) {
941 FILE_OP_ERROR(oldpath, "rename");
950 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
951 dirname = g_dirname(item->path);
952 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
955 newpath = g_strdup(name);
958 item->name = g_strdup(name);
960 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
962 paths[0] = g_strdup(item->path);
964 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
965 mh_rename_folder_func, paths);
972 gint mh_remove_folder(Folder *folder, FolderItem *item)
976 g_return_val_if_fail(folder != NULL, -1);
977 g_return_val_if_fail(item != NULL, -1);
978 g_return_val_if_fail(item->path != NULL, -1);
980 path = folder_item_get_path(item);
981 if (remove_dir_recursive(path) < 0) {
982 g_warning("can't remove directory `%s'\n", path);
988 folder_item_remove(item);
993 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
999 GSList *newlist = NULL;
1000 GSList *last = NULL;
1005 g_return_val_if_fail(item != NULL, NULL);
1007 path = folder_item_get_path(item);
1008 g_return_val_if_fail(path != NULL, NULL);
1009 if (change_dir(path) < 0) {
1015 if ((dp = opendir(".")) == NULL) {
1016 FILE_OP_ERROR(item->path, "opendir");
1020 debug_print("\tSearching uncached messages... ");
1023 while ((d = readdir(dp)) != NULL) {
1024 if ((num = to_number(d->d_name)) < 0) continue;
1025 if (stat(d->d_name, &s) < 0) {
1026 FILE_OP_ERROR(d->d_name, "stat");
1029 if (!S_ISREG(s.st_mode)) continue;
1031 msginfo = g_hash_table_lookup
1032 (msg_table, GUINT_TO_POINTER(num));
1035 /* not found in the cache (uncached message) */
1036 msginfo = mh_parse_msg(d->d_name, item);
1037 if (!msginfo) continue;
1041 g_slist_append(NULL, msginfo);
1043 last = g_slist_append(last, msginfo);
1050 /* discard all previous cache */
1051 while ((d = readdir(dp)) != NULL) {
1052 if (to_number(d->d_name) < 0) continue;
1053 if (stat(d->d_name, &s) < 0) {
1054 FILE_OP_ERROR(d->d_name, "stat");
1057 if (!S_ISREG(s.st_mode)) continue;
1059 msginfo = mh_parse_msg(d->d_name, item);
1060 if (!msginfo) continue;
1063 last = newlist = g_slist_append(NULL, msginfo);
1065 last = g_slist_append(last, msginfo);
1075 debug_print("%d uncached message(s) found.\n", n_newmsg);
1077 debug_print("done.\n");
1079 /* sort new messages in numerical order */
1081 debug_print("\tSorting uncached messages in numerical order... ");
1082 newlist = g_slist_sort
1083 (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
1084 debug_print("done.\n");
1090 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1096 flags.perm_flags = MSG_NEW|MSG_UNREAD;
1097 flags.tmp_flags = 0;
1099 g_return_val_if_fail(item != NULL, NULL);
1100 g_return_val_if_fail(file != NULL, NULL);
1102 if (item->stype == F_QUEUE) {
1103 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1104 } else if (item->stype == F_DRAFT) {
1105 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1108 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1109 if (!msginfo) return NULL;
1111 msginfo->msgnum = atoi(file);
1112 msginfo->folder = item;
1114 if (stat(file, &s) < 0) {
1115 FILE_OP_ERROR(file, "stat");
1119 msginfo->size = s.st_size;
1120 msginfo->mtime = s.st_mtime;
1127 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
1132 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
1133 result = is_dir_exist(entry);
1140 * check whether PATH is a Maildir style mailbox.
1141 * This is the case if the 3 subdir: new, cur, tmp are existing.
1142 * This functon assumes that entry is an directory
1144 static gboolean mh_is_maildir(const gchar *path)
1146 return mh_is_maildir_one(path, "new") &&
1147 mh_is_maildir_one(path, "cur") &&
1148 mh_is_maildir_one(path, "tmp");
1152 static void mh_scan_tree_recursive(FolderItem *item)
1160 g_return_if_fail(item != NULL);
1161 g_return_if_fail(item->folder != NULL);
1163 dp = opendir(item->path ? item->path : ".");
1165 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
1169 debug_print("scanning %s ...\n",
1170 item->path ? item->path
1171 : LOCAL_FOLDER(item->folder)->rootpath);
1172 if (item->folder->ui_func)
1173 item->folder->ui_func(item->folder, item,
1174 item->folder->ui_func_data);
1176 while ((d = readdir(dp)) != NULL) {
1177 if (d->d_name[0] == '.') continue;
1180 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1183 entry = g_strdup(d->d_name);
1185 if (stat(entry, &s) < 0) {
1186 FILE_OP_ERROR(entry, "stat");
1191 if (S_ISDIR(s.st_mode)) {
1192 FolderItem *new_item;
1195 if (mh_is_maildir(entry)) {
1201 new_item = folder_item_new(item->folder, d->d_name, entry);
1202 folder_item_append(item, new_item);
1204 if (!strcmp(d->d_name, INBOX_DIR)) {
1205 new_item->stype = F_INBOX;
1206 item->folder->inbox = new_item;
1207 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
1208 new_item->stype = F_OUTBOX;
1209 item->folder->outbox = new_item;
1210 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
1211 new_item->stype = F_DRAFT;
1212 item->folder->draft = new_item;
1213 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
1214 new_item->stype = F_QUEUE;
1215 item->folder->queue = new_item;
1216 } else if (!strcmp(d->d_name, TRASH_DIR)) {
1217 new_item->stype = F_TRASH;
1218 item->folder->trash = new_item;
1221 mh_scan_tree_recursive(new_item);
1222 } else if (to_number(d->d_name) != -1) n_msg++;
1231 gint new, unread, total, min, max;
1233 procmsg_get_mark_sum(item->path, &new, &unread, &total,
1235 if (n_msg > total) {
1236 new += n_msg - total;
1237 unread += n_msg - total;
1240 item->unread = unread;
1241 item->total = n_msg;
1246 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1248 FolderItem *item = node->data;
1249 gchar **paths = data;
1250 const gchar *oldpath = paths[0];
1251 const gchar *newpath = paths[1];
1253 gchar *new_itempath;
1256 oldpathlen = strlen(oldpath);
1257 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1258 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1262 base = item->path + oldpathlen;
1263 while (*base == G_DIR_SEPARATOR) base++;
1265 new_itempath = g_strdup(newpath);
1267 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1270 item->path = new_itempath;