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 ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
604 && dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
605 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
610 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
611 FILE_OP_ERROR(srcfile, "copy");
618 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
619 if (chmod(destfile, prefs->folder_chmod) < 0)
620 FILE_OP_ERROR(destfile, "chmod");
623 filemode = prefs->folder_chmod;
624 if (filemode & S_IRGRP) filemode |= S_IWGRP;
625 if (filemode & S_IROTH) filemode |= S_IWOTH;
632 return dest->last_num;
636 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
644 src_folder = msginfo->folder->folder;
646 g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
648 filename = src_folder->fetch_msg(src_folder,
651 if (filename == NULL)
654 num = folder->add_msg(folder, dest, filename, FALSE);
656 destdir = folder_item_get_path(dest);
661 newmsginfo.msgnum = dest->last_num;
662 newmsginfo.flags = msginfo->flags;
663 if (dest->stype == F_OUTBOX ||
664 dest->stype == F_QUEUE ||
665 dest->stype == F_DRAFT ||
666 dest->stype == F_TRASH)
667 MSG_UNSET_FLAGS(newmsginfo.flags,
668 MSG_NEW|MSG_UNREAD|MSG_DELETED);
670 procmsg_write_flags(&newmsginfo, fp);
678 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
685 g_return_val_if_fail(dest != NULL, -1);
686 g_return_val_if_fail(msglist != NULL, -1);
688 if (dest->last_num < 0) {
689 mh_get_last_num(folder, dest);
690 if (dest->last_num < 0) return -1;
693 for (cur = msglist; cur != NULL; cur = cur->next) {
694 msginfo = (MsgInfo *)cur->data;
696 if (msginfo->folder == dest) {
697 g_warning("the src folder is identical to the dest.\n");
700 debug_print("Copying message %s%c%d to %s ...\n",
701 msginfo->folder->path, G_DIR_SEPARATOR,
702 msginfo->msgnum, dest->path);
704 destfile = mh_get_new_msg_filename(dest);
705 if (!destfile) break;
706 srcfile = procmsg_get_message_file(msginfo);
708 if (copy_file(srcfile, destfile, TRUE) < 0) {
709 FILE_OP_ERROR(srcfile, "copy");
720 return dest->last_num;
723 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
727 g_return_val_if_fail(item != NULL, -1);
729 file = mh_fetch_msg(folder, item, num);
730 g_return_val_if_fail(file != NULL, -1);
732 if (unlink(file) < 0) {
733 FILE_OP_ERROR(file, "unlink");
742 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
747 g_return_val_if_fail(item != NULL, -1);
749 path = folder_item_get_path(item);
750 g_return_val_if_fail(path != NULL, -1);
751 val = remove_all_numbered_files(path);
757 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
761 if (stat(itos(msginfo->msgnum), &s) < 0 ||
762 msginfo->size != s.st_size ||
763 msginfo->mtime != s.st_mtime)
769 gint mh_scan_folder(Folder *folder, FolderItem *item)
778 g_return_val_if_fail(item != NULL, -1);
780 debug_print("mh_scan_folder(): Scanning %s ...\n", item->path);
782 path = folder_item_get_path(item);
783 g_return_val_if_fail(path != NULL, -1);
784 if (change_dir(path) < 0) {
790 if ((dp = opendir(".")) == NULL) {
791 FILE_OP_ERROR(item->path, "opendir");
796 folder->ui_func(folder, item, folder->ui_func_data);
798 while ((d = readdir(dp)) != NULL) {
799 if ((num = to_number(d->d_name)) >= 0 &&
800 stat(d->d_name, &s) == 0 &&
801 S_ISREG(s.st_mode)) {
813 item->new = item->unread = item->total = 0;
815 gint new, unread, total, min, max;
817 procmsg_get_mark_sum(".", &new, &unread, &total, &min, &max, 0);
819 new += n_msg - total;
820 unread += n_msg - total;
823 item->unread = unread;
827 debug_print("Last number in dir %s = %d\n", item->path, max);
828 item->last_num = max;
833 void mh_scan_tree(Folder *folder)
838 g_return_if_fail(folder != NULL);
840 item = folder_item_new(folder, folder->name, NULL);
841 item->folder = folder;
842 folder->node = g_node_new(item);
844 rootpath = folder_item_get_path(item);
845 if (change_dir(rootpath) < 0) {
851 mh_create_tree(folder);
852 mh_scan_tree_recursive(item);
855 #define MAKE_DIR_IF_NOT_EXIST(dir) \
857 if (!is_dir_exist(dir)) { \
858 if (is_file_exist(dir)) { \
859 g_warning("File `%s' already exists.\n" \
860 "Can't create folder.", dir); \
863 if (make_dir(dir) < 0) \
868 gint mh_create_tree(Folder *folder)
872 g_return_val_if_fail(folder != NULL, -1);
874 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
875 rootpath = LOCAL_FOLDER(folder)->rootpath;
876 MAKE_DIR_IF_NOT_EXIST(rootpath);
877 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
878 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
879 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
880 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
881 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
882 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
887 #undef MAKE_DIR_IF_NOT_EXIST
889 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
894 FolderItem *new_item;
895 gchar *mh_sequences_filename;
896 FILE *mh_sequences_file;
898 g_return_val_if_fail(folder != NULL, NULL);
899 g_return_val_if_fail(parent != NULL, NULL);
900 g_return_val_if_fail(name != NULL, NULL);
902 path = folder_item_get_path(parent);
903 if (!is_dir_exist(path))
904 if (make_dir_hier(path) != 0)
907 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
910 if (make_dir(fullpath) < 0) {
918 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
921 path = g_strdup(name);
922 new_item = folder_item_new(folder, name, path);
923 folder_item_append(parent, new_item);
927 path = folder_item_get_path(new_item);
928 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
929 ".mh_sequences", NULL);
930 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
931 fclose(mh_sequences_file);
933 g_free(mh_sequences_filename);
938 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
946 g_return_val_if_fail(folder != NULL, -1);
947 g_return_val_if_fail(item != NULL, -1);
948 g_return_val_if_fail(item->path != NULL, -1);
949 g_return_val_if_fail(name != NULL, -1);
951 oldpath = folder_item_get_path(item);
952 if (!is_dir_exist(oldpath))
953 make_dir_hier(oldpath);
955 dirname = g_dirname(oldpath);
956 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
959 if (rename(oldpath, newpath) < 0) {
960 FILE_OP_ERROR(oldpath, "rename");
969 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
970 dirname = g_dirname(item->path);
971 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
974 newpath = g_strdup(name);
977 item->name = g_strdup(name);
979 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
981 paths[0] = g_strdup(item->path);
983 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
984 mh_rename_folder_func, paths);
991 gint mh_remove_folder(Folder *folder, FolderItem *item)
995 g_return_val_if_fail(folder != NULL, -1);
996 g_return_val_if_fail(item != NULL, -1);
997 g_return_val_if_fail(item->path != NULL, -1);
999 path = folder_item_get_path(item);
1000 if (remove_dir_recursive(path) < 0) {
1001 g_warning("can't remove directory `%s'\n", path);
1007 folder_item_remove(item);
1012 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
1018 GSList *newlist = NULL;
1019 GSList *last = NULL;
1024 g_return_val_if_fail(item != NULL, NULL);
1026 path = folder_item_get_path(item);
1027 g_return_val_if_fail(path != NULL, NULL);
1028 if (change_dir(path) < 0) {
1034 if ((dp = opendir(".")) == NULL) {
1035 FILE_OP_ERROR(item->path, "opendir");
1039 debug_print("\tSearching uncached messages... ");
1042 while ((d = readdir(dp)) != NULL) {
1043 if ((num = to_number(d->d_name)) < 0) continue;
1044 if (stat(d->d_name, &s) < 0) {
1045 FILE_OP_ERROR(d->d_name, "stat");
1048 if (!S_ISREG(s.st_mode)) continue;
1050 msginfo = g_hash_table_lookup
1051 (msg_table, GUINT_TO_POINTER(num));
1054 /* not found in the cache (uncached message) */
1055 msginfo = mh_parse_msg(d->d_name, item);
1056 if (!msginfo) continue;
1060 g_slist_append(NULL, msginfo);
1062 last = g_slist_append(last, msginfo);
1069 /* discard all previous cache */
1070 while ((d = readdir(dp)) != NULL) {
1071 if (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 = mh_parse_msg(d->d_name, item);
1079 if (!msginfo) continue;
1082 last = newlist = g_slist_append(NULL, msginfo);
1084 last = g_slist_append(last, msginfo);
1094 debug_print("%d uncached message(s) found.\n", n_newmsg);
1096 debug_print("done.\n");
1098 /* sort new messages in numerical order */
1100 debug_print("\tSorting uncached messages in numerical order... ");
1101 newlist = g_slist_sort
1102 (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
1103 debug_print("done.\n");
1109 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1115 flags.perm_flags = MSG_NEW|MSG_UNREAD;
1116 flags.tmp_flags = 0;
1118 g_return_val_if_fail(item != NULL, NULL);
1119 g_return_val_if_fail(file != NULL, NULL);
1121 if (item->stype == F_QUEUE) {
1122 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1123 } else if (item->stype == F_DRAFT) {
1124 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1127 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1128 if (!msginfo) return NULL;
1130 msginfo->msgnum = atoi(file);
1131 msginfo->folder = item;
1133 if (stat(file, &s) < 0) {
1134 FILE_OP_ERROR(file, "stat");
1138 msginfo->size = s.st_size;
1139 msginfo->mtime = s.st_mtime;
1146 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
1151 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
1152 result = is_dir_exist(entry);
1159 * check whether PATH is a Maildir style mailbox.
1160 * This is the case if the 3 subdir: new, cur, tmp are existing.
1161 * This functon assumes that entry is an directory
1163 static gboolean mh_is_maildir(const gchar *path)
1165 return mh_is_maildir_one(path, "new") &&
1166 mh_is_maildir_one(path, "cur") &&
1167 mh_is_maildir_one(path, "tmp");
1171 static void mh_scan_tree_recursive(FolderItem *item)
1179 g_return_if_fail(item != NULL);
1180 g_return_if_fail(item->folder != NULL);
1182 dp = opendir(item->path ? item->path : ".");
1184 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
1188 debug_print("scanning %s ...\n",
1189 item->path ? item->path
1190 : LOCAL_FOLDER(item->folder)->rootpath);
1191 if (item->folder->ui_func)
1192 item->folder->ui_func(item->folder, item,
1193 item->folder->ui_func_data);
1195 while ((d = readdir(dp)) != NULL) {
1196 if (d->d_name[0] == '.') continue;
1199 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1202 entry = g_strdup(d->d_name);
1204 if (stat(entry, &s) < 0) {
1205 FILE_OP_ERROR(entry, "stat");
1210 if (S_ISDIR(s.st_mode)) {
1211 FolderItem *new_item;
1214 if (mh_is_maildir(entry)) {
1220 new_item = folder_item_new(item->folder, d->d_name, entry);
1221 folder_item_append(item, new_item);
1223 if (!strcmp(d->d_name, INBOX_DIR)) {
1224 new_item->stype = F_INBOX;
1225 item->folder->inbox = new_item;
1226 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
1227 new_item->stype = F_OUTBOX;
1228 item->folder->outbox = new_item;
1229 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
1230 new_item->stype = F_DRAFT;
1231 item->folder->draft = new_item;
1232 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
1233 new_item->stype = F_QUEUE;
1234 item->folder->queue = new_item;
1235 } else if (!strcmp(d->d_name, TRASH_DIR)) {
1236 new_item->stype = F_TRASH;
1237 item->folder->trash = new_item;
1240 mh_scan_tree_recursive(new_item);
1241 } else if (to_number(d->d_name) != -1) n_msg++;
1250 gint new, unread, total, min, max;
1252 procmsg_get_mark_sum(item->path, &new, &unread, &total,
1254 if (n_msg > total) {
1255 new += n_msg - total;
1256 unread += n_msg - total;
1259 item->unread = unread;
1260 item->total = n_msg;
1265 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1267 FolderItem *item = node->data;
1268 gchar **paths = data;
1269 const gchar *oldpath = paths[0];
1270 const gchar *newpath = paths[1];
1272 gchar *new_itempath;
1275 oldpathlen = strlen(oldpath);
1276 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1277 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1281 base = item->path + oldpathlen;
1282 while (*base == G_DIR_SEPARATOR) base++;
1284 new_itempath = g_strdup(newpath);
1286 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1289 item->path = new_itempath;