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,
107 GHashTable *pptable);
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(MHFolder *folder)
125 folder_local_folder_destroy(LOCAL_FOLDER(folder));
128 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
130 folder_local_folder_init(folder, name, path);
135 folder->get_msg_list = mh_get_msg_list;
137 folder->fetch_msg = mh_fetch_msg;
138 folder->fetch_msginfo = mh_fetch_msginfo;
139 folder->add_msg = mh_add_msg;
140 folder->move_msg = mh_move_msg;
141 folder->move_msgs_with_dest = mh_move_msgs_with_dest;
142 folder->copy_msg = mh_copy_msg;
143 folder->copy_msgs_with_dest = mh_copy_msgs_with_dest;
144 folder->remove_msg = mh_remove_msg;
145 folder->remove_all_msg = mh_remove_all_msg;
146 folder->is_msg_changed = mh_is_msg_changed;
148 folder->scan = mh_scan_folder;
150 folder->get_num_list = mh_get_num_list;
151 folder->scan_tree = mh_scan_tree;
152 folder->create_tree = mh_create_tree;
153 folder->create_folder = mh_create_folder;
154 folder->rename_folder = mh_rename_folder;
155 folder->remove_folder = mh_remove_folder;
158 void mh_get_last_num(Folder *folder, FolderItem *item)
167 g_return_if_fail(item != NULL);
169 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
171 path = folder_item_get_path(item);
172 g_return_if_fail(path != NULL);
173 if (change_dir(path) < 0) {
179 if ((dp = opendir(".")) == NULL) {
180 FILE_OP_ERROR(item->path, "opendir");
184 while ((d = readdir(dp)) != NULL) {
185 if ((num = to_number(d->d_name)) >= 0 &&
186 stat(d->d_name, &s) == 0 &&
187 S_ISREG(s.st_mode)) {
194 debug_print(_("Last number in dir %s = %d\n"), item->path, max);
195 item->last_num = max;
198 GSList *mh_get_num_list(Folder *folder, FolderItem *item)
208 g_return_val_if_fail(item != NULL, NULL);
210 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
212 path = folder_item_get_path(item);
213 g_return_val_if_fail(path != NULL, NULL);
214 if (change_dir(path) < 0) {
220 if ((dp = opendir(".")) == NULL) {
221 FILE_OP_ERROR(item->path, "opendir");
225 while ((d = readdir(dp)) != NULL) {
226 if ((num = to_number(d->d_name)) >= 0 &&
227 stat(d->d_name, &s) == 0 &&
228 S_ISREG(s.st_mode)) {
229 list = g_slist_prepend(list, GINT_TO_POINTER(num));
237 GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
240 GHashTable *msg_table;
243 gboolean scan_new = TRUE;
245 struct timeval tv_before, tv_after, tv_result;
247 gettimeofday(&tv_before, NULL);
250 g_return_val_if_fail(item != NULL, NULL);
252 path = folder_item_get_path(item);
253 if (stat(path, &s) < 0) {
254 FILE_OP_ERROR(path, "stat");
258 mtime = MAX(s.st_mtime, s.st_ctime);
259 if (item->mtime == mtime) {
260 debug_print("Folder is not modified.\n");
267 if (use_cache && !scan_new) {
268 mlist = procmsg_read_cache(item, FALSE);
270 mlist = mh_get_uncached_msgs(NULL, item);
271 } else if (use_cache) {
274 mlist = procmsg_read_cache(item, TRUE);
275 msg_table = procmsg_msg_hash_table_create(mlist);
277 newlist = mh_get_uncached_msgs(msg_table, item);
279 g_hash_table_destroy(msg_table);
281 mlist = g_slist_concat(mlist, newlist);
283 mlist = mh_get_uncached_msgs(NULL, item);
285 procmsg_set_flags(mlist, item);
288 gettimeofday(&tv_after, NULL);
290 timersub(&tv_after, &tv_before, &tv_result);
291 g_print("mh_get_msg_list: %s: elapsed time: %ld.%06ld sec\n",
292 item->path, tv_result.tv_sec, tv_result.tv_usec);
298 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
303 g_return_val_if_fail(item != NULL, NULL);
304 g_return_val_if_fail(num > 0, NULL);
306 path = folder_item_get_path(item);
307 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
309 if (!is_file_exist(file)) {
317 MsgInfo *mh_fetch_msginfo(Folder *folder, FolderItem *item, gint num)
325 g_return_val_if_fail(item != NULL, NULL);
326 g_return_val_if_fail(num > 0, NULL);
328 path = folder_item_get_path(item);
329 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
331 if (!is_file_exist(file)) {
336 folder_item_set_default_flags(item, &flags);
337 msginfo = procheader_parse_file(file, flags, TRUE, FALSE);
338 msginfo->msgnum = num;
339 msginfo->folder = item;
341 if (stat(file, &s) < 0) {
342 FILE_OP_ERROR(file, "stat");
346 msginfo->size = s.st_size;
347 msginfo->mtime = s.st_mtime;
353 gchar *mh_get_new_msg_filename(FolderItem *dest)
358 destpath = folder_item_get_path(dest);
359 g_return_val_if_fail(destpath != NULL, NULL);
361 if (!is_dir_exist(destpath))
362 make_dir_hier(destpath);
365 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
367 if (is_file_entry_exist(destfile)) {
379 #define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
381 MsgInfo newmsginfo; \
383 newmsginfo.msgnum = dest->last_num; \
384 newmsginfo.flags = msginfo->flags; \
385 if (dest->stype == F_OUTBOX || \
386 dest->stype == F_QUEUE || \
387 dest->stype == F_DRAFT || \
388 dest->stype == F_TRASH) \
389 MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
390 MSG_NEW|MSG_UNREAD|MSG_DELETED); \
392 procmsg_write_flags(&newmsginfo, fp); \
395 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
396 gboolean remove_source)
400 g_return_val_if_fail(dest != NULL, -1);
401 g_return_val_if_fail(file != NULL, -1);
403 if (dest->last_num < 0) {
404 mh_get_last_num(folder, dest);
405 if (dest->last_num < 0) return -1;
408 destfile = mh_get_new_msg_filename(dest);
409 g_return_val_if_fail(destfile != NULL, -1);
411 if (link(file, destfile) < 0) {
412 if (copy_file(file, destfile) < 0) {
413 g_warning(_("can't copy message %s to %s\n"),
421 if (unlink(file) < 0)
422 FILE_OP_ERROR(file, "unlink");
427 return dest->last_num;
430 static gint mh_do_move(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
437 PrefsFolderItem *prefs;
439 g_return_val_if_fail(dest != NULL, -1);
440 g_return_val_if_fail(msginfo != NULL, -1);
442 if (msginfo->folder == dest) {
443 g_warning(_("the src folder is identical to the dest.\n"));
447 if (dest->last_num < 0) {
448 mh_get_last_num(folder, dest);
449 if (dest->last_num < 0) return -1;
454 destfile = mh_get_new_msg_filename(dest);
455 g_return_val_if_fail(destfile != NULL, -1);
457 debug_print(_("Moving message %s%c%d to %s ...\n"),
458 msginfo->folder->path, G_DIR_SEPARATOR,
459 msginfo->msgnum, dest->path);
460 srcfile = procmsg_get_message_file(msginfo);
462 destfile = mh_get_new_msg_filename(dest);
463 if(!destfile) return -1;
465 srcfile = procmsg_get_message_file(msginfo);
467 if (move_file(srcfile, destfile) < 0) {
473 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
474 if (chmod(destfile, prefs->folder_chmod) < 0)
475 FILE_OP_ERROR(destfile, "chmod");
478 filemode = prefs->folder_chmod;
479 if (filemode & S_IRGRP) filemode |= S_IWGRP;
480 if (filemode & S_IROTH) filemode |= S_IWOTH;
489 return dest->last_num;
492 gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
497 g_return_val_if_fail(folder != NULL, -1);
498 g_return_val_if_fail(dest != NULL, -1);
499 g_return_val_if_fail(msginfo != NULL, -1);
500 g_return_val_if_fail(msginfo->folder != NULL, -1);
502 if (folder == msginfo->folder->folder)
503 return mh_do_move(folder, dest, msginfo);
505 srcfile = procmsg_get_message_file(msginfo);
506 if (!srcfile) return -1;
508 ret = mh_add_msg(folder, dest, srcfile, FALSE);
515 destdir = folder_item_get_path(dest);
516 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
517 g_warning(_("Can't open mark file.\n"));
519 SET_DEST_MSG_FLAGS(fp, dest, msginfo);
524 ret = folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
530 static gint mh_do_move_msgs_with_dest(Folder *folder, FolderItem *dest,
538 PrefsFolderItem *prefs;
540 g_return_val_if_fail(dest != NULL, -1);
541 g_return_val_if_fail(msglist != NULL, -1);
543 if (dest->last_num < 0) {
544 mh_get_last_num(folder, dest);
545 if (dest->last_num < 0) return -1;
550 for (cur = msglist; cur != NULL; cur = cur->next) {
551 msginfo = (MsgInfo *)cur->data;
553 if (msginfo->folder == dest) {
554 g_warning(_("the src folder is identical to the dest.\n"));
557 debug_print(_("Moving message %s%c%d to %s ...\n"),
558 msginfo->folder->path, G_DIR_SEPARATOR,
559 msginfo->msgnum, dest->path);
561 destfile = mh_get_new_msg_filename(dest);
562 if (!destfile) break;
563 srcfile = procmsg_get_message_file(msginfo);
564 destfile = mh_get_new_msg_filename(dest);
565 if(!destfile) return -1;
567 if (move_file(srcfile, destfile) < 0) {
578 return dest->last_num;
581 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
587 msginfo = (MsgInfo *)msglist->data;
588 if (folder == msginfo->folder->folder)
589 return mh_do_move_msgs_with_dest(folder, dest, msglist);
591 for (cur = msglist; cur != NULL; cur = cur->next) {
592 msginfo = (MsgInfo *)cur->data;
593 ret = mh_move_msg(folder, dest, msginfo);
594 if (ret == -1) break;
600 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
606 PrefsFolderItem *prefs;
608 g_return_val_if_fail(dest != NULL, -1);
609 g_return_val_if_fail(msginfo != NULL, -1);
611 if (msginfo->folder == dest) {
612 g_warning(_("the src folder is identical to the dest.\n"));
616 if (dest->last_num < 0) {
617 mh_get_last_num(folder, dest);
618 if (dest->last_num < 0) return -1;
623 destfile = mh_get_new_msg_filename(dest);
624 g_return_val_if_fail(destfile != NULL, -1);
626 debug_print(_("Copying message %s%c%d to %s ...\n"),
627 msginfo->folder->path, G_DIR_SEPARATOR,
628 msginfo->msgnum, dest->path);
630 srcfile = procmsg_get_message_file(msginfo);
631 destfile = mh_get_new_msg_filename(dest);
639 if (copy_file(srcfile, destfile) < 0) {
640 FILE_OP_ERROR(srcfile, "copy");
646 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
647 if (chmod(destfile, prefs->folder_chmod) < 0)
648 FILE_OP_ERROR(destfile, "chmod");
651 filemode = prefs->folder_chmod;
652 if (filemode & S_IRGRP) filemode |= S_IWGRP;
653 if (filemode & S_IROTH) filemode |= S_IWOTH;
660 return dest->last_num;
664 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
672 src_folder = msginfo->folder->folder;
674 g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
676 filename = src_folder->fetch_msg(src_folder,
679 if (filename == NULL)
682 num = folder->add_msg(folder, dest, filename, FALSE);
684 destdir = folder_item_get_path(dest);
689 newmsginfo.msgnum = dest->last_num;
690 newmsginfo.flags = msginfo->flags;
691 if (dest->stype == F_OUTBOX ||
692 dest->stype == F_QUEUE ||
693 dest->stype == F_DRAFT ||
694 dest->stype == F_TRASH)
695 MSG_UNSET_FLAGS(newmsginfo.flags,
696 MSG_NEW|MSG_UNREAD|MSG_DELETED);
698 procmsg_write_flags(&newmsginfo, fp);
706 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
713 g_return_val_if_fail(dest != NULL, -1);
714 g_return_val_if_fail(msglist != NULL, -1);
716 if (dest->last_num < 0) {
717 mh_get_last_num(folder, dest);
718 if (dest->last_num < 0) return -1;
721 for (cur = msglist; cur != NULL; cur = cur->next) {
722 msginfo = (MsgInfo *)cur->data;
724 if (msginfo->folder == dest) {
725 g_warning(_("the src folder is identical to the dest.\n"));
728 debug_print(_("Copying message %s%c%d to %s ...\n"),
729 msginfo->folder->path, G_DIR_SEPARATOR,
730 msginfo->msgnum, dest->path);
732 destfile = mh_get_new_msg_filename(dest);
733 if (!destfile) break;
734 srcfile = procmsg_get_message_file(msginfo);
736 if (copy_file(srcfile, destfile) < 0) {
737 FILE_OP_ERROR(srcfile, "copy");
748 return dest->last_num;
751 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
756 g_return_val_if_fail(item != NULL, -1);
758 file = mh_fetch_msg(folder, item, num);
759 g_return_val_if_fail(file != NULL, -1);
761 if (unlink(file) < 0) {
762 FILE_OP_ERROR(file, "unlink");
771 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
776 g_return_val_if_fail(item != NULL, -1);
778 path = folder_item_get_path(item);
779 g_return_val_if_fail(path != NULL, -1);
780 val = remove_all_numbered_files(path);
786 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
790 if (stat(itos(msginfo->msgnum), &s) < 0 ||
791 msginfo->size != s.st_size ||
792 msginfo->mtime != s.st_mtime)
798 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)
869 g_return_if_fail(folder != NULL);
871 pptable = folder_persist_prefs_new(folder);
873 prefs_scoring_clear();
874 prefs_filtering_clear();
875 folder_tree_destroy(folder);
876 item = folder_item_new(folder->name, NULL);
877 item->folder = folder;
878 folder->node = g_node_new(item);
880 rootpath = folder_item_get_path(item);
881 if (change_dir(rootpath) < 0) {
887 mh_create_tree(folder);
888 mh_scan_tree_recursive(item, pptable);
890 folder_persist_prefs_free(pptable);
892 prefs_matcher_read_config();
895 #define MAKE_DIR_IF_NOT_EXIST(dir) \
897 if (!is_dir_exist(dir)) { \
898 if (is_file_exist(dir)) { \
899 g_warning(_("File `%s' already exists.\n" \
900 "Can't create folder."), dir); \
903 if (make_dir(dir) < 0) \
908 gint mh_create_tree(Folder *folder)
912 g_return_val_if_fail(folder != NULL, -1);
914 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
915 rootpath = LOCAL_FOLDER(folder)->rootpath;
916 MAKE_DIR_IF_NOT_EXIST(rootpath);
917 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
918 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
919 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
920 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
921 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
922 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
927 #undef MAKE_DIR_IF_NOT_EXIST
929 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
934 FolderItem *new_item;
936 g_return_val_if_fail(folder != NULL, NULL);
937 g_return_val_if_fail(parent != NULL, NULL);
938 g_return_val_if_fail(name != NULL, NULL);
940 path = folder_item_get_path(parent);
941 if (!is_dir_exist(path))
944 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
947 if (make_dir(fullpath) < 0) {
955 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
958 path = g_strdup(name);
959 new_item = folder_item_new(name, path);
960 folder_item_append(parent, new_item);
966 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
974 g_return_val_if_fail(folder != NULL, -1);
975 g_return_val_if_fail(item != NULL, -1);
976 g_return_val_if_fail(item->path != NULL, -1);
977 g_return_val_if_fail(name != NULL, -1);
979 oldpath = folder_item_get_path(item);
980 if (!is_dir_exist(oldpath))
981 make_dir_hier(oldpath);
983 dirname = g_dirname(oldpath);
984 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
987 if (rename(oldpath, newpath) < 0) {
988 FILE_OP_ERROR(oldpath, "rename");
997 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
998 dirname = g_dirname(item->path);
999 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
1002 newpath = g_strdup(name);
1005 item->name = g_strdup(name);
1007 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
1009 paths[0] = g_strdup(item->path);
1011 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1012 mh_rename_folder_func, paths);
1019 gint mh_remove_folder(Folder *folder, FolderItem *item)
1023 g_return_val_if_fail(folder != NULL, -1);
1024 g_return_val_if_fail(item != NULL, -1);
1025 g_return_val_if_fail(item->path != NULL, -1);
1027 path = folder_item_get_path(item);
1028 if (remove_dir_recursive(path) < 0) {
1029 g_warning("can't remove directory `%s'\n", path);
1035 folder_item_remove(item);
1040 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
1046 GSList *newlist = NULL;
1047 GSList *last = NULL;
1052 g_return_val_if_fail(item != NULL, NULL);
1054 path = folder_item_get_path(item);
1055 g_return_val_if_fail(path != NULL, NULL);
1056 if (change_dir(path) < 0) {
1062 if ((dp = opendir(".")) == NULL) {
1063 FILE_OP_ERROR(item->path, "opendir");
1067 debug_print(_("\tSearching uncached messages... "));
1070 while ((d = readdir(dp)) != NULL) {
1071 if ((num = to_number(d->d_name)) < 0) continue;
1072 if (stat(d->d_name, &s) < 0) {
1073 FILE_OP_ERROR(d->d_name, "stat");
1076 if (!S_ISREG(s.st_mode)) continue;
1078 msginfo = g_hash_table_lookup
1079 (msg_table, GUINT_TO_POINTER(num));
1082 /* not found in the cache (uncached message) */
1083 msginfo = mh_parse_msg(d->d_name, item);
1084 if (!msginfo) continue;
1088 g_slist_append(NULL, msginfo);
1090 last = g_slist_append(last, msginfo);
1097 /* discard all previous cache */
1098 while ((d = readdir(dp)) != NULL) {
1099 if (to_number(d->d_name) < 0) continue;
1100 if (stat(d->d_name, &s) < 0) {
1101 FILE_OP_ERROR(d->d_name, "stat");
1104 if (!S_ISREG(s.st_mode)) continue;
1106 msginfo = mh_parse_msg(d->d_name, item);
1107 if (!msginfo) continue;
1110 last = newlist = g_slist_append(NULL, msginfo);
1112 last = g_slist_append(last, msginfo);
1122 debug_print(_("%d uncached message(s) found.\n"), n_newmsg);
1124 debug_print(_("done.\n"));
1126 /* sort new messages in numerical order */
1128 debug_print(_("\tSorting uncached messages in numerical order... "));
1129 newlist = g_slist_sort
1130 (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
1131 debug_print(_("done.\n"));
1137 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1143 flags.perm_flags = MSG_NEW|MSG_UNREAD;
1144 flags.tmp_flags = 0;
1146 g_return_val_if_fail(item != NULL, NULL);
1147 g_return_val_if_fail(file != NULL, NULL);
1149 if (item->stype == F_QUEUE) {
1150 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1151 } else if (item->stype == F_DRAFT) {
1152 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1155 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1156 if (!msginfo) return NULL;
1158 msginfo->msgnum = atoi(file);
1159 msginfo->folder = item;
1161 if (stat(file, &s) < 0) {
1162 FILE_OP_ERROR(file, "stat");
1166 msginfo->size = s.st_size;
1167 msginfo->mtime = s.st_mtime;
1173 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
1178 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
1179 result = is_dir_exist(entry);
1186 * check whether PATH is a Maildir style mailbox.
1187 * This is the case if the 3 subdir: new, cur, tmp are existing.
1188 * This functon assumes that entry is an directory
1190 static gboolean mh_is_maildir(const gchar *path)
1192 return mh_is_maildir_one(path, "new") &&
1193 mh_is_maildir_one(path, "cur") &&
1194 mh_is_maildir_one(path, "tmp");
1197 static void mh_scan_tree_recursive(FolderItem *item, GHashTable *pptable)
1205 g_return_if_fail(item != NULL);
1206 g_return_if_fail(item->folder != NULL);
1208 dp = opendir(item->path ? item->path : ".");
1210 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
1214 debug_print("scanning %s ...\n",
1215 item->path ? item->path
1216 : LOCAL_FOLDER(item->folder)->rootpath);
1217 if (item->folder->ui_func)
1218 item->folder->ui_func(item->folder, item,
1219 item->folder->ui_func_data);
1221 while ((d = readdir(dp)) != NULL) {
1222 if (d->d_name[0] == '.') continue;
1225 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1228 entry = g_strdup(d->d_name);
1230 if (stat(entry, &s) < 0) {
1231 FILE_OP_ERROR(entry, "stat");
1236 if (S_ISDIR(s.st_mode)) {
1237 FolderItem *new_item;
1239 if (mh_is_maildir(entry)) {
1244 new_item = folder_item_new(d->d_name, entry);
1245 folder_item_append(item, new_item);
1247 if (!strcmp(d->d_name, INBOX_DIR)) {
1248 new_item->stype = F_INBOX;
1249 item->folder->inbox = new_item;
1250 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
1251 new_item->stype = F_OUTBOX;
1252 item->folder->outbox = new_item;
1253 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
1254 new_item->stype = F_DRAFT;
1255 item->folder->draft = new_item;
1256 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
1257 new_item->stype = F_QUEUE;
1258 item->folder->queue = new_item;
1259 } else if (!strcmp(d->d_name, TRASH_DIR)) {
1260 new_item->stype = F_TRASH;
1261 item->folder->trash = new_item;
1264 folder_item_restore_persist_prefs(new_item, pptable);
1265 mh_scan_tree_recursive(new_item, pptable);
1266 } else if (to_number(d->d_name) != -1) n_msg++;
1275 gint new, unread, total, min, max;
1277 procmsg_get_mark_sum(item->path, &new, &unread, &total,
1279 if (n_msg > total) {
1280 new += n_msg - total;
1281 unread += n_msg - total;
1284 item->unread = unread;
1285 item->total = n_msg;
1290 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1292 FolderItem *item = node->data;
1293 gchar **paths = data;
1294 const gchar *oldpath = paths[0];
1295 const gchar *newpath = paths[1];
1297 gchar *new_itempath;
1300 oldpathlen = strlen(oldpath);
1301 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1302 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1306 base = item->path + oldpathlen;
1307 while (*base == G_DIR_SEPARATOR) base++;
1309 new_itempath = g_strdup(newpath);
1311 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1314 item->path = new_itempath;