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 static GSList *mh_get_uncached_msgs (GHashTable *msg_table,
52 static MsgInfo *mh_parse_msg (const gchar *file,
54 static void mh_scan_tree_recursive (FolderItem *item,
57 static gboolean mh_rename_folder_func (GNode *node,
61 Folder *mh_folder_new(const gchar *name, const gchar *path)
65 folder = (Folder *)g_new0(MHFolder, 1);
66 mh_folder_init(folder, name, path);
71 void mh_folder_destroy(MHFolder *folder)
73 folder_local_folder_destroy(LOCAL_FOLDER(folder));
76 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
78 folder_local_folder_init(folder, name, path);
82 folder->get_msg_list = mh_get_msg_list;
83 folder->fetch_msg = mh_fetch_msg;
84 folder->add_msg = mh_add_msg;
85 folder->move_msg = mh_move_msg;
86 folder->move_msgs_with_dest = mh_move_msgs_with_dest;
87 folder->copy_msg = mh_copy_msg;
88 folder->copy_msgs_with_dest = mh_copy_msgs_with_dest;
89 folder->remove_msg = mh_remove_msg;
90 folder->remove_all_msg = mh_remove_all_msg;
91 folder->is_msg_changed = mh_is_msg_changed;
92 folder->scan = mh_scan_folder;
93 folder->scan_tree = mh_scan_tree;
94 folder->create_tree = mh_create_tree;
95 folder->create_folder = mh_create_folder;
96 folder->rename_folder = mh_rename_folder;
97 folder->remove_folder = mh_remove_folder;
100 GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
103 GHashTable *msg_table;
106 gboolean scan_new = TRUE;
108 struct timeval tv_before, tv_after, tv_result;
110 gettimeofday(&tv_before, NULL);
113 g_return_val_if_fail(item != NULL, NULL);
115 path = folder_item_get_path(item);
116 if (stat(path, &s) < 0) {
117 FILE_OP_ERROR(path, "stat");
121 mtime = MAX(s.st_mtime, s.st_ctime);
122 if (item->mtime == mtime) {
123 debug_print("Folder is not modified.\n");
130 if (use_cache && !scan_new) {
131 mlist = procmsg_read_cache(item, FALSE);
133 mlist = mh_get_uncached_msgs(NULL, item);
134 } else if (use_cache) {
137 mlist = procmsg_read_cache(item, TRUE);
138 msg_table = procmsg_msg_hash_table_create(mlist);
140 newlist = mh_get_uncached_msgs(msg_table, item);
142 g_hash_table_destroy(msg_table);
144 mlist = g_slist_concat(mlist, newlist);
146 mlist = mh_get_uncached_msgs(NULL, item);
148 procmsg_set_flags(mlist, item);
151 gettimeofday(&tv_after, NULL);
153 timersub(&tv_after, &tv_before, &tv_result);
154 g_print("mh_get_msg_list: %s: elapsed time: %ld.%06ld sec\n",
155 item->path, tv_result.tv_sec, tv_result.tv_usec);
161 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
166 g_return_val_if_fail(item != NULL, NULL);
167 g_return_val_if_fail(num > 0 && num <= item->last_num, NULL);
169 path = folder_item_get_path(item);
170 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
172 if (!is_file_exist(file)) {
180 gchar *mh_get_newmsg_filename(FolderItem *dest)
184 gboolean found = FALSE;
186 destpath = folder_item_get_path(dest);
187 g_return_val_if_fail(destpath != NULL, NULL);
188 if (!is_dir_exist(destpath))
189 make_dir_hier(destpath);
192 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
194 if(is_file_exist(destfile)) {
206 #define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
208 MsgInfo newmsginfo; \
210 newmsginfo.msgnum = dest->last_num; \
211 newmsginfo.flags = msginfo->flags; \
212 if (dest->stype == F_OUTBOX || \
213 dest->stype == F_QUEUE || \
214 dest->stype == F_DRAFT || \
215 dest->stype == F_TRASH) \
216 MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
217 MSG_NEW|MSG_UNREAD|MSG_DELETED); \
219 procmsg_write_flags(&newmsginfo, fp); \
222 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
223 gboolean remove_source)
227 g_return_val_if_fail(dest != NULL, -1);
228 g_return_val_if_fail(file != NULL, -1);
230 if (dest->last_num < 0) {
231 mh_scan_folder(folder, dest);
232 if (dest->last_num < 0) return -1;
235 destfile = mh_get_newmsg_filename(dest);
236 if(!destfile) return -1;
238 if (link(file, destfile) < 0) {
239 if (copy_file(file, destfile) < 0) {
240 g_warning(_("can't copy message %s to %s\n"),
248 if (unlink(file) < 0)
249 FILE_OP_ERROR(file, "unlink");
254 return dest->last_num;
257 static gint mh_do_move(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
264 PrefsFolderItem *prefs;
266 g_return_val_if_fail(dest != NULL, -1);
267 g_return_val_if_fail(msginfo != NULL, -1);
269 if (msginfo->folder == dest) {
270 g_warning(_("the src folder is identical to the dest.\n"));
274 if (dest->last_num < 0) {
275 mh_scan_folder(folder, dest);
276 if (dest->last_num < 0) return -1;
281 destdir = folder_item_get_path(dest);
283 debug_print(_("Moving message %s%c%d to %s ...\n"),
284 msginfo->folder->path, G_DIR_SEPARATOR,
285 msginfo->msgnum, dest->path);
286 srcfile = procmsg_get_message_file(msginfo);
288 destfile = mh_get_newmsg_filename(dest);
289 if(!destfile) return -1;
292 if (move_file(srcfile, destfile) < 0) {
299 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
300 if (chmod(destfile, prefs->folder_chmod) < 0)
301 FILE_OP_ERROR(destfile, "chmod");
304 filemode = prefs->folder_chmod;
305 if (filemode & S_IRGRP) filemode |= S_IWGRP;
306 if (filemode & S_IROTH) filemode |= S_IWOTH;
313 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
314 g_warning(_("Can't open mark file.\n"));
316 SET_DEST_MSG_FLAGS(fp, dest, msginfo);
319 fchmod(fileno(fp), filemode);
323 markfile = folder_item_get_mark_file(dest);
325 chmod(markfile, filemode);
335 return dest->last_num;
338 gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
343 g_return_val_if_fail(folder != NULL, -1);
344 g_return_val_if_fail(dest != NULL, -1);
345 g_return_val_if_fail(msginfo != NULL, -1);
346 g_return_val_if_fail(msginfo->folder != NULL, -1);
348 if (folder == msginfo->folder->folder)
349 return mh_do_move(folder, dest, msginfo);
351 srcfile = procmsg_get_message_file(msginfo);
352 if (!srcfile) return -1;
354 ret = mh_add_msg(folder, dest, srcfile, FALSE);
361 destdir = folder_item_get_path(dest);
362 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
363 g_warning(_("Can't open mark file.\n"));
365 SET_DEST_MSG_FLAGS(fp, dest, msginfo);
370 ret = folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
376 static gint mh_do_move_msgs_with_dest(Folder *folder, FolderItem *dest,
385 PrefsFolderItem *prefs;
387 g_return_val_if_fail(dest != NULL, -1);
388 g_return_val_if_fail(msglist != NULL, -1);
390 if (dest->last_num < 0) {
391 mh_scan_folder(folder, dest);
392 if (dest->last_num < 0) return -1;
397 destdir = folder_item_get_path(dest);
398 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
399 g_warning(_("Can't open mark file.\n"));
401 for (cur = msglist; cur != NULL; cur = cur->next) {
402 msginfo = (MsgInfo *)cur->data;
404 if (msginfo->folder == dest) {
405 g_warning(_("the src folder is identical to the dest.\n"));
408 debug_print(_("Moving message %s%c%d to %s ...\n"),
409 msginfo->folder->path, G_DIR_SEPARATOR,
410 msginfo->msgnum, dest->path);
412 srcfile = procmsg_get_message_file(msginfo);
413 destfile = mh_get_newmsg_filename(dest);
414 if(!destfile) return -1;
416 if (move_file(srcfile, destfile) < 0) {
427 SET_DEST_MSG_FLAGS(fp, dest, msginfo);
434 return dest->last_num;
437 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
443 msginfo = (MsgInfo *)msglist->data;
444 if (folder == msginfo->folder->folder)
445 return mh_do_move_msgs_with_dest(folder, dest, msglist);
447 for (cur = msglist; cur != NULL; cur = cur->next) {
448 msginfo = (MsgInfo *)cur->data;
449 ret = mh_move_msg(folder, dest, msginfo);
450 if (ret == -1) break;
456 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
463 PrefsFolderItem *prefs;
465 g_return_val_if_fail(dest != NULL, -1);
466 g_return_val_if_fail(msginfo != NULL, -1);
468 if (msginfo->folder == dest) {
469 g_warning(_("the src folder is identical to the dest.\n"));
473 if (dest->last_num < 0) {
474 mh_scan_folder(folder, dest);
475 if (dest->last_num < 0) return -1;
480 destdir = folder_item_get_path(dest);
481 if (!is_dir_exist(destdir))
482 make_dir_hier(destdir);
484 debug_print(_("Copying message %s%c%d to %s ...\n"),
485 msginfo->folder->path, G_DIR_SEPARATOR,
486 msginfo->msgnum, dest->path);
487 srcfile = procmsg_get_message_file(msginfo);
488 destfile = mh_get_newmsg_filename(dest);
497 if (copy_file(srcfile, destfile) < 0) {
498 FILE_OP_ERROR(srcfile, "copy");
505 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
506 if (chmod(destfile, prefs->folder_chmod) < 0)
507 FILE_OP_ERROR(destfile, "chmod");
510 filemode = prefs->folder_chmod;
511 if (filemode & S_IRGRP) filemode |= S_IWGRP;
512 if (filemode & S_IROTH) filemode |= S_IWOTH;
519 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
520 g_warning(_("Can't open mark file.\n"));
522 SET_DEST_MSG_FLAGS(fp, dest, msginfo);
526 fchmod(fileno(fp), filemode);
530 markfile = folder_item_get_mark_file(dest);
532 chmod(markfile, filemode);
542 return dest->last_num;
546 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
554 src_folder = msginfo->folder->folder;
556 g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
558 filename = src_folder->fetch_msg(src_folder,
561 if (filename == NULL)
564 num = folder->add_msg(folder, dest, filename, FALSE);
566 destdir = folder_item_get_path(dest);
571 newmsginfo.msgnum = dest->last_num;
572 newmsginfo.flags = msginfo->flags;
573 if (dest->stype == F_OUTBOX ||
574 dest->stype == F_QUEUE ||
575 dest->stype == F_DRAFT ||
576 dest->stype == F_TRASH)
577 MSG_UNSET_FLAGS(newmsginfo.flags,
578 MSG_NEW|MSG_UNREAD|MSG_DELETED);
580 procmsg_write_flags(&newmsginfo, fp);
588 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
597 g_return_val_if_fail(dest != NULL, -1);
598 g_return_val_if_fail(msglist != NULL, -1);
600 if (dest->last_num < 0) {
601 mh_scan_folder(folder, dest);
602 if (dest->last_num < 0) return -1;
605 destdir = folder_item_get_path(dest);
606 if (!is_dir_exist(destdir))
607 make_dir_hier(destdir);
609 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
610 g_warning(_("Can't open mark file.\n"));
612 for (cur = msglist; cur != NULL; cur = cur->next) {
613 msginfo = (MsgInfo *)cur->data;
615 if (msginfo->folder == dest) {
616 g_warning(_("the src folder is identical to the dest.\n"));
619 debug_print(_("Copying message %s%c%d to %s ...\n"),
620 msginfo->folder->path, G_DIR_SEPARATOR,
621 msginfo->msgnum, dest->path);
623 srcfile = procmsg_get_message_file(msginfo);
624 destfile = mh_get_newmsg_filename(dest);
630 if (copy_file(srcfile, destfile) < 0) {
631 FILE_OP_ERROR(srcfile, "copy");
642 SET_DEST_MSG_FLAGS(fp, dest, msginfo);
649 return dest->last_num;
652 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
657 g_return_val_if_fail(item != NULL, -1);
659 file = mh_fetch_msg(folder, item, num);
660 g_return_val_if_fail(file != NULL, -1);
662 if (unlink(file) < 0) {
663 FILE_OP_ERROR(file, "unlink");
668 if(msginfo = g_new0(MsgInfo, 1)) {
669 msginfo->msgnum = num;
670 msginfo->flags.perm_flags = MSG_REALLY_DELETED;
671 msginfo->folder = item;
673 procmsg_msginfo_write_flags(msginfo);
681 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
686 g_return_val_if_fail(item != NULL, -1);
688 path = folder_item_get_path(item);
689 g_return_val_if_fail(path != NULL, -1);
690 val = remove_all_numbered_files(path);
696 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
700 if (stat(itos(msginfo->msgnum), &s) < 0 ||
701 msginfo->size != s.st_size ||
702 msginfo->mtime != s.st_mtime)
708 void mh_scan_folder(Folder *folder, FolderItem *item)
718 g_return_if_fail(item != NULL);
720 debug_print("mh_scan_folder(): Scanning %s ...\n", item->path);
722 path = folder_item_get_path(item);
723 g_return_if_fail(path != NULL);
724 if (change_dir(path) < 0) {
730 if ((dp = opendir(".")) == NULL) {
731 FILE_OP_ERROR(item->path, "opendir");
736 folder->ui_func(folder, item, folder->ui_func_data);
738 while ((d = readdir(dp)) != NULL) {
739 if ((num = to_number(d->d_name)) >= 0 &&
740 stat(d->d_name, &s) == 0 &&
741 S_ISREG(s.st_mode)) {
751 item->new = item->unread = item->total = 0;
753 gint new, unread, total, min, max;
755 procmsg_get_mark_sum(".", &new, &unread, &total, &min, &max, 0);
757 new += n_msg - total;
758 unread += n_msg - total;
761 item->unread = unread;
765 debug_print(_("Last number in dir %s = %d\n"), item->path, max);
766 item->last_num = max;
769 void mh_scan_tree(Folder *folder)
775 g_return_if_fail(folder != NULL);
777 pptable = folder_persist_prefs_new(folder);
779 folder_tree_destroy(folder);
780 item = folder_item_new(folder->name, NULL);
781 item->folder = folder;
782 folder->node = g_node_new(item);
784 rootpath = folder_item_get_path(item);
785 if (change_dir(rootpath) < 0) {
791 mh_scan_tree_recursive(item, pptable);
793 folder_persist_prefs_free(pptable);
796 #define MAKE_DIR_IF_NOT_EXIST(dir) \
798 if (!is_dir_exist(dir)) { \
799 if (is_file_exist(dir)) { \
800 g_warning(_("File `%s' already exists.\n" \
801 "Can't create folder."), dir); \
804 if (mkdir(dir, S_IRWXU) < 0) { \
805 FILE_OP_ERROR(dir, "mkdir"); \
808 if (chmod(dir, S_IRWXU) < 0) \
809 FILE_OP_ERROR(dir, "chmod"); \
813 gint mh_create_tree(Folder *folder)
817 g_return_val_if_fail(folder != NULL, -1);
819 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
820 rootpath = LOCAL_FOLDER(folder)->rootpath;
821 MAKE_DIR_IF_NOT_EXIST(rootpath);
822 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
823 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
824 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
825 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
826 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
827 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
832 #undef MAKE_DIR_IF_NOT_EXIST
834 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
839 FolderItem *new_item;
841 g_return_val_if_fail(folder != NULL, NULL);
842 g_return_val_if_fail(parent != NULL, NULL);
843 g_return_val_if_fail(name != NULL, NULL);
845 path = folder_item_get_path(parent);
846 if (!is_dir_exist(path))
849 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
852 if (mkdir(fullpath, S_IRWXU) < 0) {
853 FILE_OP_ERROR(fullpath, "mkdir");
857 if (chmod(fullpath, S_IRWXU) < 0)
858 FILE_OP_ERROR(fullpath, "chmod");
863 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
866 path = g_strdup(name);
867 new_item = folder_item_new(name, path);
868 folder_item_append(parent, new_item);
874 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
882 g_return_val_if_fail(folder != NULL, -1);
883 g_return_val_if_fail(item != NULL, -1);
884 g_return_val_if_fail(item->path != NULL, -1);
885 g_return_val_if_fail(name != NULL, -1);
887 oldpath = folder_item_get_path(item);
888 if (!is_dir_exist(oldpath))
889 make_dir_hier(oldpath);
891 dirname = g_dirname(oldpath);
892 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
895 if (rename(oldpath, newpath) < 0) {
896 FILE_OP_ERROR(oldpath, "rename");
905 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
906 dirname = g_dirname(item->path);
907 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
910 newpath = g_strdup(name);
913 item->name = g_strdup(name);
915 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
917 paths[0] = g_strdup(item->path);
919 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
920 mh_rename_folder_func, paths);
927 gint mh_remove_folder(Folder *folder, FolderItem *item)
931 g_return_val_if_fail(folder != NULL, -1);
932 g_return_val_if_fail(item != NULL, -1);
933 g_return_val_if_fail(item->path != NULL, -1);
935 path = folder_item_get_path(item);
936 if (remove_dir_recursive(path) < 0) {
937 g_warning("can't remove directory `%s'\n", path);
943 folder_item_remove(item);
948 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
954 GSList *newlist = NULL;
960 g_return_val_if_fail(item != NULL, NULL);
962 path = folder_item_get_path(item);
963 g_return_val_if_fail(path != NULL, NULL);
964 if (change_dir(path) < 0) {
970 if ((dp = opendir(".")) == NULL) {
971 FILE_OP_ERROR(item->path, "opendir");
975 debug_print(_("\tSearching uncached messages... "));
978 while ((d = readdir(dp)) != NULL) {
979 if ((num = to_number(d->d_name)) < 0) continue;
980 if (stat(d->d_name, &s) < 0) {
981 FILE_OP_ERROR(d->d_name, "stat");
984 if (!S_ISREG(s.st_mode)) continue;
986 msginfo = g_hash_table_lookup
987 (msg_table, GUINT_TO_POINTER(num));
990 /* not found in the cache (uncached message) */
991 msginfo = mh_parse_msg(d->d_name, item);
992 if (!msginfo) continue;
996 g_slist_append(NULL, msginfo);
998 last = g_slist_append(last, msginfo);
1005 /* discard all previous cache */
1006 while ((d = readdir(dp)) != NULL) {
1007 if (to_number(d->d_name) < 0) continue;
1008 if (stat(d->d_name, &s) < 0) {
1009 FILE_OP_ERROR(d->d_name, "stat");
1012 if (!S_ISREG(s.st_mode)) continue;
1014 msginfo = mh_parse_msg(d->d_name, item);
1015 if (!msginfo) continue;
1018 last = newlist = g_slist_append(NULL, msginfo);
1020 last = g_slist_append(last, msginfo);
1030 debug_print(_("%d uncached message(s) found.\n"), n_newmsg);
1032 debug_print(_("done.\n"));
1034 /* sort new messages in numerical order */
1036 debug_print(_("\tSorting uncached messages in numerical order... "));
1037 newlist = g_slist_sort
1038 (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
1039 debug_print(_("done.\n"));
1045 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1051 flags.perm_flags = MSG_NEW|MSG_UNREAD;
1052 flags.tmp_flags = 0;
1054 g_return_val_if_fail(item != NULL, NULL);
1055 g_return_val_if_fail(file != NULL, NULL);
1057 if (item->stype == F_QUEUE) {
1058 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1059 } else if (item->stype == F_DRAFT) {
1060 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1063 msginfo = procheader_parse(file, flags, FALSE, FALSE);
1064 if (!msginfo) return NULL;
1066 msginfo->msgnum = atoi(file);
1067 msginfo->folder = item;
1069 if (stat(file, &s) < 0) {
1070 FILE_OP_ERROR(file, "stat");
1074 msginfo->size = s.st_size;
1075 msginfo->mtime = s.st_mtime;
1081 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
1086 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
1087 result = is_dir_exist(entry);
1094 * check whether PATH is a Maildir style mailbox.
1095 * This is the case if the 3 subdir: new, cur, tmp are existing.
1096 * This functon assumes that entry is an directory
1098 static gboolean mh_is_maildir(const gchar *path)
1100 return mh_is_maildir_one(path, "new") &&
1101 mh_is_maildir_one(path, "cur") &&
1102 mh_is_maildir_one(path, "tmp");
1105 static void mh_scan_tree_recursive(FolderItem *item, GHashTable *pptable)
1113 g_return_if_fail(item != NULL);
1114 g_return_if_fail(item->folder != NULL);
1116 dp = opendir(item->path ? item->path : ".");
1118 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
1122 debug_print("scanning %s ...\n",
1123 item->path ? item->path
1124 : LOCAL_FOLDER(item->folder)->rootpath);
1125 if (item->folder->ui_func)
1126 item->folder->ui_func(item->folder, item,
1127 item->folder->ui_func_data);
1129 while ((d = readdir(dp)) != NULL) {
1130 if (d->d_name[0] == '.') continue;
1133 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1136 entry = g_strdup(d->d_name);
1138 if (stat(entry, &s) < 0) {
1139 FILE_OP_ERROR(entry, "stat");
1144 if (S_ISDIR(s.st_mode)) {
1145 FolderItem *new_item;
1147 if (mh_is_maildir(entry)) {
1152 new_item = folder_item_new(d->d_name, entry);
1153 folder_item_append(item, new_item);
1155 if (!strcmp(d->d_name, INBOX_DIR)) {
1156 new_item->stype = F_INBOX;
1157 item->folder->inbox = new_item;
1158 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
1159 new_item->stype = F_OUTBOX;
1160 item->folder->outbox = new_item;
1161 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
1162 new_item->stype = F_DRAFT;
1163 item->folder->draft = new_item;
1164 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
1165 new_item->stype = F_QUEUE;
1166 item->folder->queue = new_item;
1167 } else if (!strcmp(d->d_name, TRASH_DIR)) {
1168 new_item->stype = F_TRASH;
1169 item->folder->trash = new_item;
1172 folder_item_restore_persist_prefs(new_item, pptable);
1173 mh_scan_tree_recursive(new_item, pptable);
1174 } else if (to_number(d->d_name) != -1) n_msg++;
1182 gint new, unread, total, min, max;
1184 procmsg_get_mark_sum(item->path, &new, &unread, &total,
1186 if (n_msg > total) {
1187 new += n_msg - total;
1188 unread += n_msg - total;
1191 item->unread = unread;
1192 item->total = n_msg;
1196 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1198 FolderItem *item = node->data;
1199 gchar **paths = data;
1200 const gchar *oldpath = paths[0];
1201 const gchar *newpath = paths[1];
1203 gchar *new_itempath;
1206 oldpathlen = strlen(oldpath);
1207 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1208 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1212 base = item->path + oldpathlen;
1213 while (*base == G_DIR_SEPARATOR) base++;
1215 new_itempath = g_strdup(newpath);
1217 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1220 item->path = new_itempath;