2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 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 GSList *mh_get_uncached_msgs (GHashTable *msg_table,
48 static MsgInfo *mh_parse_msg (const gchar *file,
50 static void mh_scan_tree_recursive (FolderItem *item);
52 static gboolean mh_rename_folder_func (GNode *node,
56 GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
59 GHashTable *msg_table;
62 gboolean scan_new = TRUE;
64 struct timeval tv_before, tv_after, tv_result;
66 gettimeofday(&tv_before, NULL);
69 g_return_val_if_fail(item != NULL, NULL);
71 path = folder_item_get_path(item);
72 if (stat(path, &s) < 0) {
73 FILE_OP_ERROR(path, "stat");
75 if (item->mtime == s.st_mtime) {
76 debug_print("Folder is not modified.\n");
79 item->mtime = s.st_mtime;
83 if (use_cache && !scan_new) {
84 mlist = procmsg_read_cache(item, FALSE);
86 mlist = mh_get_uncached_msgs(NULL, item);
87 } else if (use_cache) {
90 mlist = procmsg_read_cache(item, TRUE);
91 msg_table = procmsg_msg_hash_table_create(mlist);
93 newlist = mh_get_uncached_msgs(msg_table, item);
95 g_hash_table_destroy(msg_table);
97 mlist = g_slist_concat(mlist, newlist);
99 mlist = mh_get_uncached_msgs(NULL, item);
101 procmsg_set_flags(mlist, item);
104 gettimeofday(&tv_after, NULL);
106 timersub(&tv_after, &tv_before, &tv_result);
107 g_print("mh_get_msg_list: %s: elapsed time: %ld.%06ld sec\n",
108 item->path, tv_result.tv_sec, tv_result.tv_usec);
114 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
119 g_return_val_if_fail(item != NULL, NULL);
120 g_return_val_if_fail(num > 0 && num <= item->last_num, NULL);
122 path = folder_item_get_path(item);
123 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
125 if (!is_file_exist(file)) {
133 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
134 gboolean remove_source)
139 g_return_val_if_fail(dest != NULL, -1);
140 g_return_val_if_fail(file != NULL, -1);
142 if (dest->last_num < 0) {
143 mh_scan_folder(folder, dest);
144 if (dest->last_num < 0) return -1;
147 destpath = folder_item_get_path(dest);
148 g_return_val_if_fail(destpath != NULL, -1);
149 if (!is_dir_exist(destpath))
150 make_dir_hier(destpath);
152 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
155 if (link(file, destfile) < 0) {
156 if (EXDEV == errno) {
157 if (copy_file(file, destfile) < 0) {
158 g_warning(_("can't copy message %s to %s\n"),
164 FILE_OP_ERROR(file, "link");
171 if (unlink(file) < 0)
172 FILE_OP_ERROR(file, "unlink");
177 return dest->last_num;
180 gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
187 g_return_val_if_fail(dest != NULL, -1);
188 g_return_val_if_fail(msginfo != NULL, -1);
190 if (msginfo->folder == dest) {
191 g_warning(_("the src folder is identical to the dest.\n"));
195 if (dest->last_num < 0) {
196 mh_scan_folder(folder, dest);
197 if (dest->last_num < 0) return -1;
200 destdir = folder_item_get_path(dest);
201 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
202 g_warning(_("Can't open mark file.\n"));
204 debug_print(_("Moving message %s%c%d to %s ...\n"),
205 msginfo->folder->path, G_DIR_SEPARATOR,
206 msginfo->msgnum, dest->path);
207 srcfile = procmsg_get_message_file_path(msginfo);
208 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
212 if (move_file(srcfile, destfile) < 0) {
226 newmsginfo.msgnum = dest->last_num;
227 newmsginfo.flags = msginfo->flags;
228 if (dest->stype == F_OUTBOX ||
229 dest->stype == F_QUEUE ||
230 dest->stype == F_DRAFT ||
231 dest->stype == F_TRASH)
232 MSG_UNSET_FLAGS(newmsginfo.flags,
233 MSG_NEW|MSG_UNREAD|MSG_DELETED);
235 procmsg_write_flags(&newmsginfo, fp);
239 return dest->last_num;
242 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
251 g_return_val_if_fail(dest != NULL, -1);
252 g_return_val_if_fail(msglist != NULL, -1);
254 if (dest->last_num < 0) {
255 mh_scan_folder(folder, dest);
256 if (dest->last_num < 0) return -1;
259 destdir = folder_item_get_path(dest);
260 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
261 g_warning(_("Can't open mark file.\n"));
263 for (cur = msglist; cur != NULL; cur = cur->next) {
264 msginfo = (MsgInfo *)cur->data;
266 if (msginfo->folder == dest) {
267 g_warning(_("the src folder is identical to the dest.\n"));
270 debug_print(_("Moving message %s%c%d to %s ...\n"),
271 msginfo->folder->path, G_DIR_SEPARATOR,
272 msginfo->msgnum, dest->path);
274 srcfile = procmsg_get_message_file_path(msginfo);
275 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
278 if (move_file(srcfile, destfile) < 0) {
291 newmsginfo.msgnum = dest->last_num;
292 newmsginfo.flags = msginfo->flags;
293 if (dest->stype == F_OUTBOX ||
294 dest->stype == F_QUEUE ||
295 dest->stype == F_DRAFT ||
296 dest->stype == F_TRASH)
297 MSG_UNSET_FLAGS(newmsginfo.flags,
298 MSG_NEW|MSG_UNREAD|MSG_DELETED);
300 procmsg_write_flags(&newmsginfo, fp);
307 return dest->last_num;
310 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
317 g_return_val_if_fail(dest != NULL, -1);
318 g_return_val_if_fail(msginfo != NULL, -1);
320 if (msginfo->folder == dest) {
321 g_warning(_("the src folder is identical to the dest.\n"));
325 if (dest->last_num < 0) {
326 mh_scan_folder(folder, dest);
327 if (dest->last_num < 0) return -1;
330 destdir = folder_item_get_path(dest);
331 if (!is_dir_exist(destdir))
332 make_dir_hier(destdir);
334 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
335 g_warning(_("Can't open mark file.\n"));
337 debug_print(_("Copying message %s%c%d to %s ...\n"),
338 msginfo->folder->path, G_DIR_SEPARATOR,
339 msginfo->msgnum, dest->path);
340 srcfile = procmsg_get_message_file_path(msginfo);
341 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
345 if (is_file_exist(destfile)) {
346 g_warning(_("%s already exists."), destfile);
353 if (copy_file(srcfile, destfile) < 0) {
354 FILE_OP_ERROR(srcfile, "copy");
368 newmsginfo.msgnum = dest->last_num;
369 newmsginfo.flags = msginfo->flags;
370 if (dest->stype == F_OUTBOX ||
371 dest->stype == F_QUEUE ||
372 dest->stype == F_DRAFT ||
373 dest->stype == F_TRASH)
374 MSG_UNSET_FLAGS(newmsginfo.flags,
375 MSG_NEW|MSG_UNREAD|MSG_DELETED);
376 procmsg_write_flags(&newmsginfo, fp);
380 return dest->last_num;
384 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
392 src_folder = msginfo->folder->folder;
394 g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
396 filename = src_folder->fetch_msg(src_folder,
399 if (filename == NULL)
402 num = folder->add_msg(folder, dest, filename, FALSE);
404 destdir = folder_item_get_path(dest);
405 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
406 g_warning(_("Can't open mark file.\n"));
411 newmsginfo.msgnum = dest->last_num;
412 newmsginfo.flags = msginfo->flags;
413 if (dest->stype == F_OUTBOX ||
414 dest->stype == F_QUEUE ||
415 dest->stype == F_DRAFT ||
416 dest->stype == F_TRASH)
417 MSG_UNSET_FLAGS(newmsginfo.flags,
418 MSG_NEW|MSG_UNREAD|MSG_DELETED);
420 procmsg_write_flags(&newmsginfo, fp);
428 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
437 g_return_val_if_fail(dest != NULL, -1);
438 g_return_val_if_fail(msglist != NULL, -1);
440 if (dest->last_num < 0) {
441 mh_scan_folder(folder, dest);
442 if (dest->last_num < 0) return -1;
445 destdir = folder_item_get_path(dest);
446 if (!is_dir_exist(destdir))
447 make_dir_hier(destdir);
449 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
450 g_warning(_("Can't open mark file.\n"));
452 for (cur = msglist; cur != NULL; cur = cur->next) {
453 msginfo = (MsgInfo *)cur->data;
455 if (msginfo->folder == dest) {
456 g_warning(_("the src folder is identical to the dest.\n"));
459 debug_print(_("Copying message %s%c%d to %s ...\n"),
460 msginfo->folder->path, G_DIR_SEPARATOR,
461 msginfo->msgnum, dest->path);
463 srcfile = procmsg_get_message_file_path(msginfo);
464 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
467 if (is_file_exist(destfile)) {
468 g_warning(_("%s already exists."), destfile);
474 if (copy_file(srcfile, destfile) < 0) {
475 FILE_OP_ERROR(srcfile, "copy");
488 newmsginfo.msgnum = dest->last_num;
489 newmsginfo.flags = msginfo->flags;
490 if (dest->stype == F_OUTBOX ||
491 dest->stype == F_QUEUE ||
492 dest->stype == F_DRAFT ||
493 dest->stype == F_TRASH)
494 MSG_UNSET_FLAGS(newmsginfo.flags,
495 MSG_NEW|MSG_UNREAD|MSG_DELETED);
496 procmsg_write_flags(&newmsginfo, fp);
503 return dest->last_num;
506 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
510 g_return_val_if_fail(item != NULL, -1);
512 file = mh_fetch_msg(folder, item, num);
513 g_return_val_if_fail(file != NULL, -1);
515 if (unlink(file) < 0) {
516 FILE_OP_ERROR(file, "unlink");
525 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
530 g_return_val_if_fail(item != NULL, -1);
532 path = folder_item_get_path(item);
533 g_return_val_if_fail(path != NULL, -1);
534 val = remove_all_numbered_files(path);
540 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
544 if (stat(itos(msginfo->msgnum), &s) < 0 ||
545 msginfo->size != s.st_size ||
546 msginfo->mtime != s.st_mtime)
552 void mh_scan_folder(Folder *folder, FolderItem *item)
562 g_return_if_fail(item != NULL);
564 path = folder_item_get_path(item);
565 g_return_if_fail(path != NULL);
566 if (change_dir(path) < 0) {
572 if ((dp = opendir(".")) == NULL) {
573 FILE_OP_ERROR(item->path, "opendir");
578 folder->ui_func(folder, item, folder->ui_func_data);
580 while ((d = readdir(dp)) != NULL) {
581 if ((num = to_number(d->d_name)) >= 0 &&
582 stat(d->d_name, &s) == 0 &&
583 S_ISREG(s.st_mode)) {
593 item->new = item->unread = item->total = 0;
595 gint new, unread, total;
597 procmsg_get_mark_sum(".", &new, &unread, &total);
599 new += n_msg - total;
600 unread += n_msg - total;
603 item->unread = unread;
607 debug_print(_("Last number in dir %s = %d\n"), item->path, max);
608 item->last_num = max;
611 void mh_scan_tree(Folder *folder)
616 g_return_if_fail(folder != NULL);
618 folder_tree_destroy(folder);
619 item = folder_item_new(folder->name, NULL);
620 item->folder = folder;
621 folder->node = g_node_new(item);
623 rootpath = folder_item_get_path(item);
624 if (change_dir(rootpath) < 0) {
630 mh_scan_tree_recursive(item);
633 #define MAKE_DIR_IF_NOT_EXIST(dir) \
635 if (!is_dir_exist(dir)) { \
636 if (is_file_exist(dir)) { \
637 g_warning(_("File `%s' already exists.\n" \
638 "Can't create folder."), dir); \
641 if (mkdir(dir, S_IRWXU) < 0) { \
642 FILE_OP_ERROR(dir, "mkdir"); \
645 if (chmod(dir, S_IRWXU) < 0) \
646 FILE_OP_ERROR(dir, "chmod"); \
650 gint mh_create_tree(Folder *folder)
654 g_return_val_if_fail(folder != NULL, -1);
656 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
657 rootpath = LOCAL_FOLDER(folder)->rootpath;
658 MAKE_DIR_IF_NOT_EXIST(rootpath);
659 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
660 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
661 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
662 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
663 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
664 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
669 #undef MAKE_DIR_IF_NOT_EXIST
671 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
676 FolderItem *new_item;
678 g_return_val_if_fail(folder != NULL, NULL);
679 g_return_val_if_fail(parent != NULL, NULL);
680 g_return_val_if_fail(name != NULL, NULL);
682 path = folder_item_get_path(parent);
683 if (!is_dir_exist(path))
686 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
689 if (mkdir(fullpath, S_IRWXU) < 0) {
690 FILE_OP_ERROR(fullpath, "mkdir");
694 if (chmod(fullpath, S_IRWXU) < 0)
695 FILE_OP_ERROR(fullpath, "chmod");
700 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
703 path = g_strdup(name);
704 new_item = folder_item_new(name, path);
705 folder_item_append(parent, new_item);
711 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
719 g_return_val_if_fail(folder != NULL, -1);
720 g_return_val_if_fail(item != NULL, -1);
721 g_return_val_if_fail(item->path != NULL, -1);
722 g_return_val_if_fail(name != NULL, -1);
724 oldpath = folder_item_get_path(item);
725 if (!is_dir_exist(oldpath))
726 make_dir_hier(oldpath);
728 dirname = g_dirname(oldpath);
729 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
732 if (rename(oldpath, newpath) < 0) {
733 FILE_OP_ERROR(oldpath, "rename");
742 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
743 dirname = g_dirname(item->path);
744 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
747 newpath = g_strdup(name);
750 item->name = g_strdup(name);
752 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
754 paths[0] = g_strdup(item->path);
756 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
757 mh_rename_folder_func, paths);
764 gint mh_remove_folder(Folder *folder, FolderItem *item)
768 g_return_val_if_fail(folder != NULL, -1);
769 g_return_val_if_fail(item != NULL, -1);
770 g_return_val_if_fail(item->path != NULL, -1);
772 path = folder_item_get_path(item);
773 if (remove_dir_recursive(path) < 0) {
774 g_warning("can't remove directory `%s'\n", path);
780 folder_item_remove(item);
785 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
791 GSList *newlist = NULL;
797 g_return_val_if_fail(item != NULL, NULL);
799 path = folder_item_get_path(item);
800 g_return_val_if_fail(path != NULL, NULL);
801 if (change_dir(path) < 0) {
807 if ((dp = opendir(".")) == NULL) {
808 FILE_OP_ERROR(item->path, "opendir");
812 debug_print(_("\tSearching uncached messages... "));
815 while ((d = readdir(dp)) != NULL) {
816 if ((num = to_number(d->d_name)) < 0) continue;
817 if (stat(d->d_name, &s) < 0) {
818 FILE_OP_ERROR(d->d_name, "stat");
821 if (!S_ISREG(s.st_mode)) continue;
823 msginfo = g_hash_table_lookup
824 (msg_table, GUINT_TO_POINTER(num));
827 /* not found in the cache (uncached message) */
828 msginfo = mh_parse_msg(d->d_name, item);
829 if (!msginfo) continue;
833 g_slist_append(NULL, msginfo);
835 last = g_slist_append(last, msginfo);
842 /* discard all previous cache */
843 while ((d = readdir(dp)) != NULL) {
844 if (to_number(d->d_name) < 0) continue;
845 if (stat(d->d_name, &s) < 0) {
846 FILE_OP_ERROR(d->d_name, "stat");
849 if (!S_ISREG(s.st_mode)) continue;
851 msginfo = mh_parse_msg(d->d_name, item);
852 if (!msginfo) continue;
855 last = newlist = g_slist_append(NULL, msginfo);
857 last = g_slist_append(last, msginfo);
867 debug_print(_("%d uncached message(s) found.\n"), n_newmsg);
869 debug_print(_("done.\n"));
871 /* sort new messages in numerical order */
873 debug_print(_("\tSorting uncached messages in numerical order... "));
874 newlist = g_slist_sort
875 (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
876 debug_print(_("done.\n"));
882 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
886 MsgFlags flags = MSG_NEW|MSG_UNREAD;
888 g_return_val_if_fail(item != NULL, NULL);
889 g_return_val_if_fail(file != NULL, NULL);
891 if (item->stype == F_QUEUE) {
892 MSG_SET_FLAGS(flags, MSG_QUEUED);
893 } else if (item->stype == F_DRAFT) {
894 MSG_SET_FLAGS(flags, MSG_DRAFT);
897 msginfo = procheader_parse(file, flags, FALSE);
898 if (!msginfo) return NULL;
900 msginfo->msgnum = atoi(file);
901 msginfo->folder = item;
903 if (stat(file, &s) < 0) {
904 FILE_OP_ERROR(file, "stat");
908 msginfo->size = s.st_size;
909 msginfo->mtime = s.st_mtime;
915 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
920 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
921 result = is_dir_exist(entry);
928 * check whether PATH is a Maildir style mailbox.
929 * This is the case if the 3 subdir: new, cur, tmp are existing.
930 * This functon assumes that entry is an directory
932 static gboolean mh_is_maildir(const gchar *path)
934 return mh_is_maildir_one(path, "new") &&
935 mh_is_maildir_one(path, "cur") &&
936 mh_is_maildir_one(path, "tmp");
939 static void mh_scan_tree_recursive(FolderItem *item)
947 g_return_if_fail(item != NULL);
948 g_return_if_fail(item->folder != NULL);
950 dp = opendir(item->path ? item->path : ".");
952 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
956 debug_print("scanning %s ...\n",
957 item->path ? item->path
958 : LOCAL_FOLDER(item->folder)->rootpath);
959 if (item->folder->ui_func)
960 item->folder->ui_func(item->folder, item,
961 item->folder->ui_func_data);
963 while ((d = readdir(dp)) != NULL) {
964 if (d->d_name[0] == '.') continue;
967 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
970 entry = g_strdup(d->d_name);
972 if (stat(entry, &s) < 0) {
973 FILE_OP_ERROR(entry, "stat");
978 if (S_ISDIR(s.st_mode)) {
979 FolderItem *new_item;
981 if (mh_is_maildir(entry)) {
986 new_item = folder_item_new(d->d_name, entry);
987 folder_item_append(item, new_item);
989 if (!strcmp(d->d_name, "inbox")) {
990 new_item->stype = F_INBOX;
991 item->folder->inbox = new_item;
992 } else if (!strcmp(d->d_name, "outbox")) {
993 new_item->stype = F_OUTBOX;
994 item->folder->outbox = new_item;
995 } else if (!strcmp(d->d_name, "draft")) {
996 new_item->stype = F_DRAFT;
997 item->folder->draft = new_item;
998 } else if (!strcmp(d->d_name, "queue")) {
999 new_item->stype = F_QUEUE;
1000 item->folder->queue = new_item;
1001 } else if (!strcmp(d->d_name, "trash")) {
1002 new_item->stype = F_TRASH;
1003 item->folder->trash = new_item;
1006 mh_scan_tree_recursive(new_item);
1007 } else if (to_number(d->d_name) != -1) n_msg++;
1015 gint new, unread, total;
1017 procmsg_get_mark_sum(item->path, &new, &unread, &total);
1018 if (n_msg > total) {
1019 new += n_msg - total;
1020 unread += n_msg - total;
1023 item->unread = unread;
1024 item->total = n_msg;
1028 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1030 FolderItem *item = node->data;
1031 gchar **paths = data;
1032 const gchar *oldpath = paths[0];
1033 const gchar *newpath = paths[1];
1035 gchar *new_itempath;
1038 oldpathlen = strlen(oldpath);
1039 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1040 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1044 base = item->path + oldpathlen;
1045 while (*base == G_DIR_SEPARATOR) base++;
1047 new_itempath = g_strdup(newpath);
1049 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1052 item->path = new_itempath;