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);
202 if (!is_dir_exist(destdir))
203 make_dir_hier(destdir);
205 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
206 g_warning(_("Can't open mark file.\n"));
208 debug_print(_("Moving message %s%c%d to %s ...\n"),
209 msginfo->folder->path, G_DIR_SEPARATOR,
210 msginfo->msgnum, dest->path);
211 srcfile = procmsg_get_message_file_path(msginfo);
212 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
216 if (is_file_exist(destfile)) {
217 g_warning(_("%s already exists."), destfile);
224 if (rename(srcfile, destfile) < 0) {
225 if (EXDEV == errno) {
226 if (copy_file(srcfile, destfile) < 0) {
233 FILE_OP_ERROR(srcfile, "rename");
248 newmsginfo.msgnum = dest->last_num;
249 newmsginfo.flags = msginfo->flags;
250 if (dest->stype == F_OUTBOX ||
251 dest->stype == F_QUEUE ||
252 dest->stype == F_DRAFT ||
253 dest->stype == F_TRASH)
254 MSG_UNSET_FLAGS(newmsginfo.flags,
255 MSG_NEW|MSG_UNREAD|MSG_DELETED);
257 procmsg_write_flags(&newmsginfo, fp);
261 return dest->last_num;
264 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
273 g_return_val_if_fail(dest != NULL, -1);
274 g_return_val_if_fail(msglist != NULL, -1);
276 if (dest->last_num < 0) {
277 mh_scan_folder(folder, dest);
278 if (dest->last_num < 0) return -1;
281 destdir = folder_item_get_path(dest);
282 if (!is_dir_exist(destdir))
283 make_dir_hier(destdir);
285 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
286 g_warning(_("Can't open mark file.\n"));
288 for (cur = msglist; cur != NULL; cur = cur->next) {
289 msginfo = (MsgInfo *)cur->data;
291 if (msginfo->folder == dest) {
292 g_warning(_("the src folder is identical to the dest.\n"));
295 debug_print(_("Moving message %s%c%d to %s ...\n"),
296 msginfo->folder->path, G_DIR_SEPARATOR,
297 msginfo->msgnum, dest->path);
299 srcfile = procmsg_get_message_file_path(msginfo);
300 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
303 if (is_file_exist(destfile)) {
304 g_warning(_("%s already exists."), destfile);
310 if (rename(srcfile, destfile) < 0) {
311 if (EXDEV == errno) {
312 if (copy_file(srcfile, destfile) < 0) {
319 FILE_OP_ERROR(srcfile, "rename");
333 newmsginfo.msgnum = dest->last_num;
334 newmsginfo.flags = msginfo->flags;
335 if (dest->stype == F_OUTBOX ||
336 dest->stype == F_QUEUE ||
337 dest->stype == F_DRAFT ||
338 dest->stype == F_TRASH)
339 MSG_UNSET_FLAGS(newmsginfo.flags,
340 MSG_NEW|MSG_UNREAD|MSG_DELETED);
342 procmsg_write_flags(&newmsginfo, fp);
349 return dest->last_num;
352 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
359 g_return_val_if_fail(dest != NULL, -1);
360 g_return_val_if_fail(msginfo != NULL, -1);
362 if (msginfo->folder == dest) {
363 g_warning(_("the src folder is identical to the dest.\n"));
367 if (dest->last_num < 0) {
368 mh_scan_folder(folder, dest);
369 if (dest->last_num < 0) return -1;
372 destdir = folder_item_get_path(dest);
373 if (!is_dir_exist(destdir))
374 make_dir_hier(destdir);
376 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
377 g_warning(_("Can't open mark file.\n"));
379 debug_print(_("Copying message %s%c%d to %s ...\n"),
380 msginfo->folder->path, G_DIR_SEPARATOR,
381 msginfo->msgnum, dest->path);
382 srcfile = procmsg_get_message_file_path(msginfo);
383 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
387 if (is_file_exist(destfile)) {
388 g_warning(_("%s already exists."), destfile);
395 if (copy_file(srcfile, destfile) < 0) {
396 FILE_OP_ERROR(srcfile, "copy");
410 newmsginfo.msgnum = dest->last_num;
411 newmsginfo.flags = msginfo->flags;
412 if (dest->stype == F_OUTBOX ||
413 dest->stype == F_QUEUE ||
414 dest->stype == F_DRAFT ||
415 dest->stype == F_TRASH)
416 MSG_UNSET_FLAGS(newmsginfo.flags,
417 MSG_NEW|MSG_UNREAD|MSG_DELETED);
418 procmsg_write_flags(&newmsginfo, fp);
422 return dest->last_num;
425 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
434 g_return_val_if_fail(dest != NULL, -1);
435 g_return_val_if_fail(msglist != NULL, -1);
437 if (dest->last_num < 0) {
438 mh_scan_folder(folder, dest);
439 if (dest->last_num < 0) return -1;
442 destdir = folder_item_get_path(dest);
443 if (!is_dir_exist(destdir))
444 make_dir_hier(destdir);
446 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
447 g_warning(_("Can't open mark file.\n"));
449 for (cur = msglist; cur != NULL; cur = cur->next) {
450 msginfo = (MsgInfo *)cur->data;
452 if (msginfo->folder == dest) {
453 g_warning(_("the src folder is identical to the dest.\n"));
456 debug_print(_("Copying message %s%c%d to %s ...\n"),
457 msginfo->folder->path, G_DIR_SEPARATOR,
458 msginfo->msgnum, dest->path);
460 srcfile = procmsg_get_message_file_path(msginfo);
461 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
464 if (is_file_exist(destfile)) {
465 g_warning(_("%s already exists."), destfile);
471 if (copy_file(srcfile, destfile) < 0) {
472 FILE_OP_ERROR(srcfile, "copy");
485 newmsginfo.msgnum = dest->last_num;
486 newmsginfo.flags = msginfo->flags;
487 if (dest->stype == F_OUTBOX ||
488 dest->stype == F_QUEUE ||
489 dest->stype == F_DRAFT ||
490 dest->stype == F_TRASH)
491 MSG_UNSET_FLAGS(newmsginfo.flags,
492 MSG_NEW|MSG_UNREAD|MSG_DELETED);
493 procmsg_write_flags(&newmsginfo, fp);
500 return dest->last_num;
503 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
507 g_return_val_if_fail(item != NULL, -1);
509 file = mh_fetch_msg(folder, item, num);
510 g_return_val_if_fail(file != NULL, -1);
512 if (unlink(file) < 0) {
513 FILE_OP_ERROR(file, "unlink");
522 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
528 g_return_val_if_fail(item != NULL, -1);
530 path = folder_item_get_path(item);
531 g_return_val_if_fail(path != NULL, -1);
532 if (change_dir(path) < 0) {
538 if ((dp = opendir(".")) == NULL) {
539 FILE_OP_ERROR(item->path, "opendir");
543 while ((d = readdir(dp)) != NULL) {
544 if (to_number(d->d_name) < 0) continue;
545 if (unlink(d->d_name) < 0)
546 FILE_OP_ERROR(d->d_name, "unlink");
553 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
557 if (stat(itos(msginfo->msgnum), &s) < 0 ||
558 msginfo->size != s.st_size ||
559 msginfo->mtime != s.st_mtime)
565 void mh_scan_folder(Folder *folder, FolderItem *item)
575 g_return_if_fail(item != NULL);
577 path = folder_item_get_path(item);
578 g_return_if_fail(path != NULL);
579 if (change_dir(path) < 0) {
585 if ((dp = opendir(".")) == NULL) {
586 FILE_OP_ERROR(item->path, "opendir");
591 folder->ui_func(folder, item, folder->ui_func_data);
593 while ((d = readdir(dp)) != NULL) {
594 if ((num = to_number(d->d_name)) >= 0 &&
595 stat(d->d_name, &s) == 0 &&
596 S_ISREG(s.st_mode)) {
606 item->new = item->unread = item->total = 0;
608 gint new, unread, total;
610 procmsg_get_mark_sum(".", &new, &unread, &total);
612 new += n_msg - total;
613 unread += n_msg - total;
616 item->unread = unread;
620 debug_print(_("Last number in dir %s = %d\n"), item->path, max);
621 item->last_num = max;
624 void mh_scan_tree(Folder *folder)
629 g_return_if_fail(folder != NULL);
631 folder_tree_destroy(folder);
632 item = folder_item_new(folder->name, NULL);
633 item->folder = folder;
634 folder->node = g_node_new(item);
636 rootpath = folder_item_get_path(item);
637 if (change_dir(rootpath) < 0) {
643 mh_scan_tree_recursive(item);
646 #define MAKE_DIR_IF_NOT_EXIST(dir) \
648 if (!is_dir_exist(dir)) { \
649 if (is_file_exist(dir)) { \
650 g_warning(_("File `%s' already exists.\n" \
651 "Can't create folder."), dir); \
654 if (mkdir(dir, S_IRWXU) < 0) { \
655 FILE_OP_ERROR(dir, "mkdir"); \
658 if (chmod(dir, S_IRWXU) < 0) \
659 FILE_OP_ERROR(dir, "chmod"); \
663 gint mh_create_tree(Folder *folder)
667 g_return_val_if_fail(folder != NULL, -1);
669 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
670 rootpath = LOCAL_FOLDER(folder)->rootpath;
671 MAKE_DIR_IF_NOT_EXIST(rootpath);
672 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
673 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
674 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
675 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
676 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
677 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
682 #undef MAKE_DIR_IF_NOT_EXIST
684 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
689 FolderItem *new_item;
691 g_return_val_if_fail(folder != NULL, NULL);
692 g_return_val_if_fail(parent != NULL, NULL);
693 g_return_val_if_fail(name != NULL, NULL);
695 path = folder_item_get_path(parent);
696 if (!is_dir_exist(path))
699 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
702 if (mkdir(fullpath, S_IRWXU) < 0) {
703 FILE_OP_ERROR(fullpath, "mkdir");
707 if (chmod(fullpath, S_IRWXU) < 0)
708 FILE_OP_ERROR(fullpath, "chmod");
713 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
716 path = g_strdup(name);
717 new_item = folder_item_new(name, path);
718 folder_item_append(parent, new_item);
724 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
732 g_return_val_if_fail(folder != NULL, -1);
733 g_return_val_if_fail(item != NULL, -1);
734 g_return_val_if_fail(item->path != NULL, -1);
735 g_return_val_if_fail(name != NULL, -1);
737 oldpath = folder_item_get_path(item);
738 if (!is_dir_exist(oldpath))
739 make_dir_hier(oldpath);
741 dirname = g_dirname(oldpath);
742 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
745 if (rename(oldpath, newpath) < 0) {
746 FILE_OP_ERROR(oldpath, "rename");
755 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
756 dirname = g_dirname(item->path);
757 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
760 newpath = g_strdup(name);
763 item->name = g_strdup(name);
765 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
767 paths[0] = g_strdup(item->path);
769 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
770 mh_rename_folder_func, paths);
777 gint mh_remove_folder(Folder *folder, FolderItem *item)
781 g_return_val_if_fail(folder != NULL, -1);
782 g_return_val_if_fail(item != NULL, -1);
783 g_return_val_if_fail(item->path != NULL, -1);
785 path = folder_item_get_path(item);
786 if (remove_dir_recursive(path) < 0) {
787 g_warning("can't remove directory `%s'\n", path);
793 folder_item_remove(item);
798 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
804 GSList *newlist = NULL;
810 g_return_val_if_fail(item != NULL, NULL);
812 path = folder_item_get_path(item);
813 g_return_val_if_fail(path != NULL, NULL);
814 if (change_dir(path) < 0) {
820 if ((dp = opendir(".")) == NULL) {
821 FILE_OP_ERROR(item->path, "opendir");
825 debug_print(_("\tSearching uncached messages... "));
828 while ((d = readdir(dp)) != NULL) {
829 if ((num = to_number(d->d_name)) < 0) continue;
830 if (stat(d->d_name, &s) < 0) {
831 FILE_OP_ERROR(d->d_name, "stat");
834 if (!S_ISREG(s.st_mode)) continue;
836 msginfo = g_hash_table_lookup
837 (msg_table, GUINT_TO_POINTER(num));
840 /* not found in the cache (uncached message) */
841 msginfo = mh_parse_msg(d->d_name, item);
842 if (!msginfo) continue;
846 g_slist_append(NULL, msginfo);
848 last = g_slist_append(last, msginfo);
855 /* discard all previous cache */
856 while ((d = readdir(dp)) != NULL) {
857 if (to_number(d->d_name) < 0) continue;
858 if (stat(d->d_name, &s) < 0) {
859 FILE_OP_ERROR(d->d_name, "stat");
862 if (!S_ISREG(s.st_mode)) continue;
864 msginfo = mh_parse_msg(d->d_name, item);
865 if (!msginfo) continue;
868 last = newlist = g_slist_append(NULL, msginfo);
870 last = g_slist_append(last, msginfo);
880 debug_print(_("%d uncached message(s) found.\n"), n_newmsg);
882 debug_print(_("done.\n"));
884 /* sort new messages in numerical order */
886 debug_print(_("\tSorting uncached messages in numerical order... "));
887 newlist = g_slist_sort
888 (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
889 debug_print(_("done.\n"));
895 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
899 MsgFlags flags = MSG_NEW|MSG_UNREAD;
901 g_return_val_if_fail(item != NULL, NULL);
902 g_return_val_if_fail(file != NULL, NULL);
904 if (item->stype == F_QUEUE) {
905 MSG_SET_FLAGS(flags, MSG_QUEUED);
906 } else if (item->stype == F_DRAFT) {
907 MSG_SET_FLAGS(flags, MSG_DRAFT);
910 msginfo = procheader_parse(file, flags, FALSE);
911 if (!msginfo) return NULL;
913 msginfo->msgnum = atoi(file);
914 msginfo->folder = item;
916 if (stat(file, &s) < 0) {
917 FILE_OP_ERROR(file, "stat");
921 msginfo->size = s.st_size;
922 msginfo->mtime = s.st_mtime;
928 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
933 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
934 result = is_dir_exist(entry);
941 * check whether PATH is a Maildir style mailbox.
942 * This is the case if the 3 subdir: new, cur, tmp are existing.
943 * This functon assumes that entry is an directory
945 static gboolean mh_is_maildir(const gchar *path)
947 return mh_is_maildir_one(path, "new") &&
948 mh_is_maildir_one(path, "cur") &&
949 mh_is_maildir_one(path, "tmp");
952 static void mh_scan_tree_recursive(FolderItem *item)
960 g_return_if_fail(item != NULL);
961 g_return_if_fail(item->folder != NULL);
963 dp = opendir(item->path ? item->path : ".");
965 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
969 debug_print("scanning %s ...\n",
970 item->path ? item->path
971 : LOCAL_FOLDER(item->folder)->rootpath);
972 if (item->folder->ui_func)
973 item->folder->ui_func(item->folder, item,
974 item->folder->ui_func_data);
976 while ((d = readdir(dp)) != NULL) {
977 if (d->d_name[0] == '.') continue;
980 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
983 entry = g_strdup(d->d_name);
985 if (stat(entry, &s) < 0) {
986 FILE_OP_ERROR(entry, "stat");
991 if (S_ISDIR(s.st_mode)) {
992 FolderItem *new_item;
994 if (mh_is_maildir(entry)) {
999 new_item = folder_item_new(d->d_name, entry);
1000 folder_item_append(item, new_item);
1002 if (!strcmp(d->d_name, "inbox")) {
1003 new_item->stype = F_INBOX;
1004 item->folder->inbox = new_item;
1005 } else if (!strcmp(d->d_name, "outbox")) {
1006 new_item->stype = F_OUTBOX;
1007 item->folder->outbox = new_item;
1008 } else if (!strcmp(d->d_name, "draft")) {
1009 new_item->stype = F_DRAFT;
1010 item->folder->draft = new_item;
1011 } else if (!strcmp(d->d_name, "queue")) {
1012 new_item->stype = F_QUEUE;
1013 item->folder->queue = new_item;
1014 } else if (!strcmp(d->d_name, "trash")) {
1015 new_item->stype = F_TRASH;
1016 item->folder->trash = new_item;
1019 mh_scan_tree_recursive(new_item);
1020 } else if (to_number(d->d_name) != -1) n_msg++;
1028 gint new, unread, total;
1030 procmsg_get_mark_sum(item->path, &new, &unread, &total);
1031 if (n_msg > total) {
1032 new += n_msg - total;
1033 unread += n_msg - total;
1036 item->unread = unread;
1037 item->total = n_msg;
1041 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1043 FolderItem *item = node->data;
1044 gchar **paths = data;
1045 const gchar *oldpath = paths[0];
1046 const gchar *newpath = paths[1];
1048 gchar *new_itempath;
1051 oldpathlen = strlen(oldpath);
1052 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1053 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1057 base = item->path + oldpathlen;
1058 while (*base == G_DIR_SEPARATOR) base++;
1060 new_itempath = g_strdup(newpath);
1062 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1065 item->path = new_itempath;