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);
108 static gboolean mh_rename_folder_func (GNode *node,
112 Folder *mh_folder_new(const gchar *name, const gchar *path)
116 folder = (Folder *)g_new0(MHFolder, 1);
117 mh_folder_init(folder, name, path);
122 void mh_folder_destroy(MHFolder *folder)
124 folder_local_folder_destroy(LOCAL_FOLDER(folder));
127 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
131 folder_local_folder_init(folder, name, path);
134 folder->get_msg_list = mh_get_msg_list;
136 folder->fetch_msg = mh_fetch_msg;
137 folder->fetch_msginfo = mh_fetch_msginfo;
138 folder->add_msg = mh_add_msg;
139 folder->move_msg = mh_move_msg;
140 folder->move_msgs_with_dest = mh_move_msgs_with_dest;
141 folder->copy_msg = mh_copy_msg;
142 folder->copy_msgs_with_dest = mh_copy_msgs_with_dest;
143 folder->remove_msg = mh_remove_msg;
144 folder->remove_all_msg = mh_remove_all_msg;
145 folder->is_msg_changed = mh_is_msg_changed;
147 folder->scan = mh_scan_folder;
149 folder->get_num_list = mh_get_num_list;
150 folder->scan_tree = mh_scan_tree;
151 folder->create_tree = mh_create_tree;
152 folder->create_folder = mh_create_folder;
153 folder->rename_folder = mh_rename_folder;
154 folder->remove_folder = mh_remove_folder;
157 void mh_get_last_num(Folder *folder, FolderItem *item)
166 g_return_if_fail(item != NULL);
168 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
170 path = folder_item_get_path(item);
171 g_return_if_fail(path != NULL);
172 if (change_dir(path) < 0) {
178 if ((dp = opendir(".")) == NULL) {
179 FILE_OP_ERROR(item->path, "opendir");
183 while ((d = readdir(dp)) != NULL) {
184 if ((num = to_number(d->d_name)) >= 0 &&
185 stat(d->d_name, &s) == 0 &&
186 S_ISREG(s.st_mode)) {
193 debug_print("Last number in dir %s = %d\n", item->path, max);
194 item->last_num = max;
197 GSList *mh_get_num_list(Folder *folder, FolderItem *item)
207 g_return_val_if_fail(item != NULL, NULL);
209 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
211 path = folder_item_get_path(item);
212 g_return_val_if_fail(path != NULL, NULL);
213 if (change_dir(path) < 0) {
219 if ((dp = opendir(".")) == NULL) {
220 FILE_OP_ERROR(item->path, "opendir");
224 while ((d = readdir(dp)) != NULL) {
225 if ((num = to_number(d->d_name)) >= 0 &&
226 stat(d->d_name, &s) == 0 &&
227 S_ISREG(s.st_mode)) {
228 list = g_slist_prepend(list, GINT_TO_POINTER(num));
236 GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
239 GHashTable *msg_table;
242 gboolean scan_new = TRUE;
244 struct timeval tv_before, tv_after, tv_result;
246 gettimeofday(&tv_before, NULL);
249 g_return_val_if_fail(item != NULL, NULL);
251 path = folder_item_get_path(item);
252 if (stat(path, &s) < 0) {
253 FILE_OP_ERROR(path, "stat");
257 mtime = MAX(s.st_mtime, s.st_ctime);
258 if (item->mtime == mtime) {
259 debug_print("Folder is not modified.\n");
266 if (use_cache && !scan_new) {
267 mlist = procmsg_read_cache(item, FALSE);
269 mlist = mh_get_uncached_msgs(NULL, item);
270 } else if (use_cache) {
273 mlist = procmsg_read_cache(item, TRUE);
274 msg_table = procmsg_msg_hash_table_create(mlist);
276 newlist = mh_get_uncached_msgs(msg_table, item);
278 g_hash_table_destroy(msg_table);
280 mlist = g_slist_concat(mlist, newlist);
282 mlist = mh_get_uncached_msgs(NULL, item);
284 procmsg_set_flags(mlist, item);
287 gettimeofday(&tv_after, NULL);
289 timersub(&tv_after, &tv_before, &tv_result);
290 g_print("mh_get_msg_list: %s: elapsed time: %ld.%06ld sec\n",
291 item->path, tv_result.tv_sec, tv_result.tv_usec);
297 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
302 g_return_val_if_fail(item != NULL, NULL);
303 g_return_val_if_fail(num > 0, NULL);
305 path = folder_item_get_path(item);
306 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
308 if (!is_file_exist(file)) {
316 MsgInfo *mh_fetch_msginfo(Folder *folder, FolderItem *item, gint num)
324 g_return_val_if_fail(item != NULL, NULL);
325 g_return_val_if_fail(num > 0, NULL);
327 path = folder_item_get_path(item);
328 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
330 if (!is_file_exist(file)) {
335 folder_item_set_default_flags(item, &flags);
336 msginfo = procheader_parse_file(file, flags, TRUE, FALSE);
342 msginfo->msgnum = num;
343 msginfo->folder = item;
345 if (stat(file, &s) < 0) {
346 FILE_OP_ERROR(file, "stat");
350 msginfo->size = s.st_size;
351 msginfo->mtime = s.st_mtime;
359 gchar *mh_get_new_msg_filename(FolderItem *dest)
364 destpath = folder_item_get_path(dest);
365 g_return_val_if_fail(destpath != NULL, NULL);
367 if (!is_dir_exist(destpath))
368 make_dir_hier(destpath);
371 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
373 if (is_file_entry_exist(destfile)) {
385 #define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
387 MsgInfo newmsginfo; \
389 newmsginfo.msgnum = dest->last_num; \
390 newmsginfo.flags = msginfo->flags; \
391 if (dest->stype == F_OUTBOX || \
392 dest->stype == F_QUEUE || \
393 dest->stype == F_DRAFT || \
394 dest->stype == F_TRASH) \
395 MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
396 MSG_NEW|MSG_UNREAD|MSG_DELETED); \
398 procmsg_write_flags(&newmsginfo, fp); \
401 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
402 gboolean remove_source)
406 g_return_val_if_fail(dest != NULL, -1);
407 g_return_val_if_fail(file != NULL, -1);
409 if (dest->last_num < 0) {
410 mh_get_last_num(folder, dest);
411 if (dest->last_num < 0) return -1;
414 destfile = mh_get_new_msg_filename(dest);
415 g_return_val_if_fail(destfile != NULL, -1);
417 if (link(file, destfile) < 0) {
418 if (copy_file(file, destfile, TRUE) < 0) {
419 g_warning(_("can't copy message %s to %s\n"),
427 if (unlink(file) < 0)
428 FILE_OP_ERROR(file, "unlink");
433 return dest->last_num;
436 static gint mh_do_move(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
441 PrefsFolderItem *prefs;
443 g_return_val_if_fail(dest != NULL, -1);
444 g_return_val_if_fail(msginfo != NULL, -1);
446 if (msginfo->folder == dest) {
447 g_warning(_("the src folder is identical to the dest.\n"));
451 if (dest->last_num < 0) {
452 mh_get_last_num(folder, dest);
453 if (dest->last_num < 0) return -1;
458 destfile = mh_get_new_msg_filename(dest);
459 g_return_val_if_fail(destfile != NULL, -1);
461 debug_print("Moving message %s%c%d to %s ...\n",
462 msginfo->folder->path, G_DIR_SEPARATOR,
463 msginfo->msgnum, dest->path);
464 srcfile = procmsg_get_message_file(msginfo);
466 destfile = mh_get_new_msg_filename(dest);
467 if(!destfile) return -1;
469 srcfile = procmsg_get_message_file(msginfo);
471 if (move_file(srcfile, destfile, FALSE) < 0) {
477 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
478 if (chmod(destfile, prefs->folder_chmod) < 0)
479 FILE_OP_ERROR(destfile, "chmod");
482 filemode = prefs->folder_chmod;
483 if (filemode & S_IRGRP) filemode |= S_IWGRP;
484 if (filemode & S_IROTH) filemode |= S_IWOTH;
491 return dest->last_num;
494 gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
499 g_return_val_if_fail(folder != NULL, -1);
500 g_return_val_if_fail(dest != NULL, -1);
501 g_return_val_if_fail(msginfo != NULL, -1);
502 g_return_val_if_fail(msginfo->folder != NULL, -1);
504 if (folder == msginfo->folder->folder)
505 return mh_do_move(folder, dest, msginfo);
507 srcfile = procmsg_get_message_file(msginfo);
508 if (!srcfile) return -1;
510 ret = mh_add_msg(folder, dest, srcfile, FALSE);
517 destdir = folder_item_get_path(dest);
518 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
519 g_warning(_("Can't open mark file.\n"));
521 SET_DEST_MSG_FLAGS(fp, dest, msginfo);
526 ret = folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
532 static gint mh_do_move_msgs_with_dest(Folder *folder, FolderItem *dest,
539 PrefsFolderItem *prefs;
541 g_return_val_if_fail(dest != NULL, -1);
542 g_return_val_if_fail(msglist != NULL, -1);
544 if (dest->last_num < 0) {
545 mh_get_last_num(folder, dest);
546 if (dest->last_num < 0) return -1;
551 for (cur = msglist; cur != NULL; cur = cur->next) {
552 msginfo = (MsgInfo *)cur->data;
554 if (msginfo->folder == dest) {
555 g_warning(_("the src folder is identical to the dest.\n"));
558 debug_print("Moving message %s%c%d to %s ...\n",
559 msginfo->folder->path, G_DIR_SEPARATOR,
560 msginfo->msgnum, dest->path);
562 destfile = mh_get_new_msg_filename(dest);
563 if (!destfile) break;
564 srcfile = procmsg_get_message_file(msginfo);
565 destfile = mh_get_new_msg_filename(dest);
566 if(!destfile) return -1;
568 if (move_file(srcfile, destfile, FALSE) < 0) {
579 return dest->last_num;
582 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
588 msginfo = (MsgInfo *)msglist->data;
589 if (folder == msginfo->folder->folder)
590 return mh_do_move_msgs_with_dest(folder, dest, msglist);
592 for (cur = msglist; cur != NULL; cur = cur->next) {
593 msginfo = (MsgInfo *)cur->data;
594 ret = mh_move_msg(folder, dest, msginfo);
595 if (ret == -1) break;
601 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 srcfile = procmsg_get_message_file(msginfo);
624 destfile = mh_get_new_msg_filename(dest);
630 debug_print("Copying message %s%c%d to %s ...\n",
631 msginfo->folder->path, G_DIR_SEPARATOR,
632 msginfo->msgnum, dest->path);
635 if (copy_file(srcfile, destfile, TRUE) < 0) {
636 FILE_OP_ERROR(srcfile, "copy");
642 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
643 if (chmod(destfile, prefs->folder_chmod) < 0)
644 FILE_OP_ERROR(destfile, "chmod");
647 filemode = prefs->folder_chmod;
648 if (filemode & S_IRGRP) filemode |= S_IWGRP;
649 if (filemode & S_IROTH) filemode |= S_IWOTH;
656 return dest->last_num;
660 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
668 src_folder = msginfo->folder->folder;
670 g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
672 filename = src_folder->fetch_msg(src_folder,
675 if (filename == NULL)
678 num = folder->add_msg(folder, dest, filename, FALSE);
680 destdir = folder_item_get_path(dest);
685 newmsginfo.msgnum = dest->last_num;
686 newmsginfo.flags = msginfo->flags;
687 if (dest->stype == F_OUTBOX ||
688 dest->stype == F_QUEUE ||
689 dest->stype == F_DRAFT ||
690 dest->stype == F_TRASH)
691 MSG_UNSET_FLAGS(newmsginfo.flags,
692 MSG_NEW|MSG_UNREAD|MSG_DELETED);
694 procmsg_write_flags(&newmsginfo, fp);
702 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
709 g_return_val_if_fail(dest != NULL, -1);
710 g_return_val_if_fail(msglist != NULL, -1);
712 if (dest->last_num < 0) {
713 mh_get_last_num(folder, dest);
714 if (dest->last_num < 0) return -1;
717 for (cur = msglist; cur != NULL; cur = cur->next) {
718 msginfo = (MsgInfo *)cur->data;
720 if (msginfo->folder == dest) {
721 g_warning(_("the src folder is identical to the dest.\n"));
724 debug_print("Copying message %s%c%d to %s ...\n",
725 msginfo->folder->path, G_DIR_SEPARATOR,
726 msginfo->msgnum, dest->path);
728 destfile = mh_get_new_msg_filename(dest);
729 if (!destfile) break;
730 srcfile = procmsg_get_message_file(msginfo);
732 if (copy_file(srcfile, destfile, TRUE) < 0) {
733 FILE_OP_ERROR(srcfile, "copy");
744 return dest->last_num;
747 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
751 g_return_val_if_fail(item != NULL, -1);
753 file = mh_fetch_msg(folder, item, num);
754 g_return_val_if_fail(file != NULL, -1);
756 if (unlink(file) < 0) {
757 FILE_OP_ERROR(file, "unlink");
766 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
771 g_return_val_if_fail(item != NULL, -1);
773 path = folder_item_get_path(item);
774 g_return_val_if_fail(path != NULL, -1);
775 val = remove_all_numbered_files(path);
781 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
785 if (stat(itos(msginfo->msgnum), &s) < 0 ||
786 msginfo->size != s.st_size ||
787 msginfo->mtime != s.st_mtime)
793 gint mh_scan_folder(Folder *folder, FolderItem *item)
802 g_return_val_if_fail(item != NULL, -1);
804 debug_print("mh_scan_folder(): Scanning %s ...\n", item->path);
806 path = folder_item_get_path(item);
807 g_return_val_if_fail(path != NULL, -1);
808 if (change_dir(path) < 0) {
814 if ((dp = opendir(".")) == NULL) {
815 FILE_OP_ERROR(item->path, "opendir");
820 folder->ui_func(folder, item, folder->ui_func_data);
822 while ((d = readdir(dp)) != NULL) {
823 if ((num = to_number(d->d_name)) >= 0 &&
824 stat(d->d_name, &s) == 0 &&
825 S_ISREG(s.st_mode)) {
837 item->new = item->unread = item->total = 0;
839 gint new, unread, total, min, max;
841 procmsg_get_mark_sum(".", &new, &unread, &total, &min, &max, 0);
843 new += n_msg - total;
844 unread += n_msg - total;
847 item->unread = unread;
851 debug_print("Last number in dir %s = %d\n", item->path, max);
852 item->last_num = max;
857 void mh_scan_tree(Folder *folder)
862 g_return_if_fail(folder != NULL);
864 item = folder_item_new(folder, folder->name, NULL);
865 item->folder = folder;
866 folder->node = g_node_new(item);
868 rootpath = folder_item_get_path(item);
869 if (change_dir(rootpath) < 0) {
875 mh_create_tree(folder);
876 mh_scan_tree_recursive(item);
879 #define MAKE_DIR_IF_NOT_EXIST(dir) \
881 if (!is_dir_exist(dir)) { \
882 if (is_file_exist(dir)) { \
883 g_warning(_("File `%s' already exists.\n" \
884 "Can't create folder."), dir); \
887 if (make_dir(dir) < 0) \
892 gint mh_create_tree(Folder *folder)
896 g_return_val_if_fail(folder != NULL, -1);
898 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
899 rootpath = LOCAL_FOLDER(folder)->rootpath;
900 MAKE_DIR_IF_NOT_EXIST(rootpath);
901 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
902 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
903 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
904 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
905 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
906 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
911 #undef MAKE_DIR_IF_NOT_EXIST
913 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
918 FolderItem *new_item;
920 g_return_val_if_fail(folder != NULL, NULL);
921 g_return_val_if_fail(parent != NULL, NULL);
922 g_return_val_if_fail(name != NULL, NULL);
924 path = folder_item_get_path(parent);
925 if (!is_dir_exist(path))
928 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
931 if (make_dir(fullpath) < 0) {
939 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
942 path = g_strdup(name);
943 new_item = folder_item_new(folder, name, path);
944 folder_item_append(parent, new_item);
950 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
958 g_return_val_if_fail(folder != NULL, -1);
959 g_return_val_if_fail(item != NULL, -1);
960 g_return_val_if_fail(item->path != NULL, -1);
961 g_return_val_if_fail(name != NULL, -1);
963 oldpath = folder_item_get_path(item);
964 if (!is_dir_exist(oldpath))
965 make_dir_hier(oldpath);
967 dirname = g_dirname(oldpath);
968 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
971 if (rename(oldpath, newpath) < 0) {
972 FILE_OP_ERROR(oldpath, "rename");
981 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
982 dirname = g_dirname(item->path);
983 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
986 newpath = g_strdup(name);
989 item->name = g_strdup(name);
991 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
993 paths[0] = g_strdup(item->path);
995 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
996 mh_rename_folder_func, paths);
1003 gint mh_remove_folder(Folder *folder, FolderItem *item)
1007 g_return_val_if_fail(folder != NULL, -1);
1008 g_return_val_if_fail(item != NULL, -1);
1009 g_return_val_if_fail(item->path != NULL, -1);
1011 path = folder_item_get_path(item);
1012 if (remove_dir_recursive(path) < 0) {
1013 g_warning("can't remove directory `%s'\n", path);
1019 folder_item_remove(item);
1024 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
1030 GSList *newlist = NULL;
1031 GSList *last = NULL;
1036 g_return_val_if_fail(item != NULL, NULL);
1038 path = folder_item_get_path(item);
1039 g_return_val_if_fail(path != NULL, NULL);
1040 if (change_dir(path) < 0) {
1046 if ((dp = opendir(".")) == NULL) {
1047 FILE_OP_ERROR(item->path, "opendir");
1051 debug_print("\tSearching uncached messages... ");
1054 while ((d = readdir(dp)) != NULL) {
1055 if ((num = to_number(d->d_name)) < 0) continue;
1056 if (stat(d->d_name, &s) < 0) {
1057 FILE_OP_ERROR(d->d_name, "stat");
1060 if (!S_ISREG(s.st_mode)) continue;
1062 msginfo = g_hash_table_lookup
1063 (msg_table, GUINT_TO_POINTER(num));
1066 /* not found in the cache (uncached message) */
1067 msginfo = mh_parse_msg(d->d_name, item);
1068 if (!msginfo) continue;
1072 g_slist_append(NULL, msginfo);
1074 last = g_slist_append(last, msginfo);
1081 /* discard all previous cache */
1082 while ((d = readdir(dp)) != NULL) {
1083 if (to_number(d->d_name) < 0) continue;
1084 if (stat(d->d_name, &s) < 0) {
1085 FILE_OP_ERROR(d->d_name, "stat");
1088 if (!S_ISREG(s.st_mode)) continue;
1090 msginfo = mh_parse_msg(d->d_name, item);
1091 if (!msginfo) continue;
1094 last = newlist = g_slist_append(NULL, msginfo);
1096 last = g_slist_append(last, msginfo);
1106 debug_print("%d uncached message(s) found.\n", n_newmsg);
1108 debug_print("done.\n");
1110 /* sort new messages in numerical order */
1112 debug_print("\tSorting uncached messages in numerical order... ");
1113 newlist = g_slist_sort
1114 (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
1115 debug_print("done.\n");
1121 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1127 flags.perm_flags = MSG_NEW|MSG_UNREAD;
1128 flags.tmp_flags = 0;
1130 g_return_val_if_fail(item != NULL, NULL);
1131 g_return_val_if_fail(file != NULL, NULL);
1133 if (item->stype == F_QUEUE) {
1134 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1135 } else if (item->stype == F_DRAFT) {
1136 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1139 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1140 if (!msginfo) return NULL;
1142 msginfo->msgnum = atoi(file);
1143 msginfo->folder = item;
1145 if (stat(file, &s) < 0) {
1146 FILE_OP_ERROR(file, "stat");
1150 msginfo->size = s.st_size;
1151 msginfo->mtime = s.st_mtime;
1158 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
1163 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
1164 result = is_dir_exist(entry);
1171 * check whether PATH is a Maildir style mailbox.
1172 * This is the case if the 3 subdir: new, cur, tmp are existing.
1173 * This functon assumes that entry is an directory
1175 static gboolean mh_is_maildir(const gchar *path)
1177 return mh_is_maildir_one(path, "new") &&
1178 mh_is_maildir_one(path, "cur") &&
1179 mh_is_maildir_one(path, "tmp");
1183 static void mh_scan_tree_recursive(FolderItem *item)
1191 g_return_if_fail(item != NULL);
1192 g_return_if_fail(item->folder != NULL);
1194 dp = opendir(item->path ? item->path : ".");
1196 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
1200 debug_print("scanning %s ...\n",
1201 item->path ? item->path
1202 : LOCAL_FOLDER(item->folder)->rootpath);
1203 if (item->folder->ui_func)
1204 item->folder->ui_func(item->folder, item,
1205 item->folder->ui_func_data);
1207 while ((d = readdir(dp)) != NULL) {
1208 if (d->d_name[0] == '.') continue;
1211 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1214 entry = g_strdup(d->d_name);
1216 if (stat(entry, &s) < 0) {
1217 FILE_OP_ERROR(entry, "stat");
1222 if (S_ISDIR(s.st_mode)) {
1223 FolderItem *new_item;
1226 if (mh_is_maildir(entry)) {
1232 new_item = folder_item_new(item->folder, d->d_name, entry);
1233 folder_item_append(item, new_item);
1235 if (!strcmp(d->d_name, INBOX_DIR)) {
1236 new_item->stype = F_INBOX;
1237 item->folder->inbox = new_item;
1238 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
1239 new_item->stype = F_OUTBOX;
1240 item->folder->outbox = new_item;
1241 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
1242 new_item->stype = F_DRAFT;
1243 item->folder->draft = new_item;
1244 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
1245 new_item->stype = F_QUEUE;
1246 item->folder->queue = new_item;
1247 } else if (!strcmp(d->d_name, TRASH_DIR)) {
1248 new_item->stype = F_TRASH;
1249 item->folder->trash = new_item;
1252 mh_scan_tree_recursive(new_item);
1253 } else if (to_number(d->d_name) != -1) n_msg++;
1262 gint new, unread, total, min, max;
1264 procmsg_get_mark_sum(item->path, &new, &unread, &total,
1266 if (n_msg > total) {
1267 new += n_msg - total;
1268 unread += n_msg - total;
1271 item->unread = unread;
1272 item->total = n_msg;
1277 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1279 FolderItem *item = node->data;
1280 gchar **paths = data;
1281 const gchar *oldpath = paths[0];
1282 const gchar *newpath = paths[1];
1284 gchar *new_itempath;
1287 oldpathlen = strlen(oldpath);
1288 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1289 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1293 base = item->path + oldpathlen;
1294 while (*base == G_DIR_SEPARATOR) base++;
1296 new_itempath = g_strdup(newpath);
1298 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1301 item->path = new_itempath;