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.
34 # include <sys/time.h>
41 #include "procheader.h"
44 static GSList *mh_get_uncached_msgs (GHashTable *msg_table,
46 static MsgInfo *mh_parse_msg (const gchar *file,
48 static void mh_scan_tree_recursive (FolderItem *item);
50 static gboolean mh_rename_folder_func (GNode *node,
54 GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
57 GHashTable *msg_table;
60 gboolean scan_new = TRUE;
62 struct timeval tv_before, tv_after, tv_result;
64 gettimeofday(&tv_before, NULL);
67 g_return_val_if_fail(item != NULL, NULL);
69 path = folder_item_get_path(item);
70 if (stat(path, &s) < 0) {
71 FILE_OP_ERROR(path, "stat");
73 if (item->mtime == s.st_mtime) {
74 debug_print("Folder is not modified.\n");
77 item->mtime = s.st_mtime;
81 if (use_cache && !scan_new) {
82 mlist = procmsg_read_cache(item, FALSE);
84 mlist = mh_get_uncached_msgs(NULL, item);
85 } else if (use_cache) {
88 mlist = procmsg_read_cache(item, TRUE);
89 msg_table = procmsg_msg_hash_table_create(mlist);
91 newlist = mh_get_uncached_msgs(msg_table, item);
93 g_hash_table_destroy(msg_table);
95 mlist = g_slist_concat(mlist, newlist);
97 mlist = mh_get_uncached_msgs(NULL, item);
99 procmsg_set_flags(mlist, item);
102 gettimeofday(&tv_after, NULL);
104 timersub(&tv_after, &tv_before, &tv_result);
105 g_print("mh_get_msg_list: %s: elapsed time: %ld.%06ld sec\n",
106 item->path, tv_result.tv_sec, tv_result.tv_usec);
112 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
117 g_return_val_if_fail(item != NULL, NULL);
118 g_return_val_if_fail(num > 0 && num <= item->last_num, NULL);
120 path = folder_item_get_path(item);
121 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
123 if (!is_file_exist(file)) {
131 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file)
136 g_return_val_if_fail(dest != NULL, -1);
137 g_return_val_if_fail(file != NULL, -1);
139 if (dest->last_num < 0) {
140 mh_scan_folder(folder, dest);
141 if (dest->last_num < 0) return -1;
144 destpath = folder_item_get_path(dest);
145 g_return_val_if_fail(destpath != NULL, -1);
146 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
148 if (copy_file(file, destfile) < 0) {
149 g_warning(_("can't copy message %s to %s\n"), file, destfile);
156 return dest->last_num;
159 gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
166 g_return_val_if_fail(dest != NULL, -1);
167 g_return_val_if_fail(msginfo != NULL, -1);
169 if (msginfo->folder == dest) {
170 g_warning(_("the src folder is identical to the dest.\n"));
174 if (dest->last_num < 0) {
175 mh_scan_folder(folder, dest);
176 if (dest->last_num < 0) return -1;
179 destdir = folder_item_get_path(dest);
180 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
181 g_warning(_("Can't open mark file.\n"));
183 debug_print(_("Moving message %s%c%d to %s ...\n"),
184 msginfo->folder->path, G_DIR_SEPARATOR,
185 msginfo->msgnum, dest->path);
186 srcfile = procmsg_get_message_file_path(msginfo);
187 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
191 if (is_file_exist(destfile)) {
192 g_warning(_("%s already exists."), destfile);
199 if (rename(srcfile, destfile) < 0) {
200 if (EXDEV == errno) {
201 if (copy_file(srcfile, destfile) < 0) {
208 FILE_OP_ERROR(srcfile, "rename");
223 newmsginfo.msgnum = dest->last_num;
224 newmsginfo.flags = msginfo->flags;
225 if (dest->stype == F_OUTBOX ||
226 dest->stype == F_QUEUE ||
227 dest->stype == F_DRAFT ||
228 dest->stype == F_TRASH)
229 MSG_UNSET_FLAGS(newmsginfo.flags,
230 MSG_NEW|MSG_UNREAD|MSG_DELETED);
232 procmsg_write_flags(&newmsginfo, fp);
236 return dest->last_num;
239 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
248 g_return_val_if_fail(dest != NULL, -1);
249 g_return_val_if_fail(msglist != NULL, -1);
251 if (dest->last_num < 0) {
252 mh_scan_folder(folder, dest);
253 if (dest->last_num < 0) return -1;
256 destdir = folder_item_get_path(dest);
257 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
258 g_warning(_("Can't open mark file.\n"));
260 for (cur = msglist; cur != NULL; cur = cur->next) {
261 msginfo = (MsgInfo *)cur->data;
263 if (msginfo->folder == dest) {
264 g_warning(_("the src folder is identical to the dest.\n"));
267 debug_print(_("Moving message %s%c%d to %s ...\n"),
268 msginfo->folder->path, G_DIR_SEPARATOR,
269 msginfo->msgnum, dest->path);
271 srcfile = procmsg_get_message_file_path(msginfo);
272 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
275 if (is_file_exist(destfile)) {
276 g_warning(_("%s already exists."), destfile);
282 if (rename(srcfile, destfile) < 0) {
283 if (EXDEV == errno) {
284 if (copy_file(srcfile, destfile) < 0) {
291 FILE_OP_ERROR(srcfile, "rename");
305 newmsginfo.msgnum = dest->last_num;
306 newmsginfo.flags = msginfo->flags;
307 if (dest->stype == F_OUTBOX ||
308 dest->stype == F_QUEUE ||
309 dest->stype == F_DRAFT ||
310 dest->stype == F_TRASH)
311 MSG_UNSET_FLAGS(newmsginfo.flags,
312 MSG_NEW|MSG_UNREAD|MSG_DELETED);
314 procmsg_write_flags(&newmsginfo, fp);
321 return dest->last_num;
324 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
331 g_return_val_if_fail(dest != NULL, -1);
332 g_return_val_if_fail(msginfo != NULL, -1);
334 if (msginfo->folder == dest) {
335 g_warning(_("the src folder is identical to the dest.\n"));
339 if (dest->last_num < 0) {
340 mh_scan_folder(folder, dest);
341 if (dest->last_num < 0) return -1;
344 destdir = folder_item_get_path(dest);
345 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
346 g_warning(_("Can't open mark file.\n"));
348 debug_print(_("Copying message %s%c%d to %s ...\n"),
349 msginfo->folder->path, G_DIR_SEPARATOR,
350 msginfo->msgnum, dest->path);
351 srcfile = procmsg_get_message_file_path(msginfo);
352 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
356 if (is_file_exist(destfile)) {
357 g_warning(_("%s already exists."), destfile);
364 if (copy_file(srcfile, destfile) < 0) {
365 FILE_OP_ERROR(srcfile, "copy");
379 newmsginfo.msgnum = dest->last_num;
380 newmsginfo.flags = msginfo->flags;
381 if (dest->stype == F_OUTBOX ||
382 dest->stype == F_QUEUE ||
383 dest->stype == F_DRAFT ||
384 dest->stype == F_TRASH)
385 MSG_UNSET_FLAGS(newmsginfo.flags,
386 MSG_NEW|MSG_UNREAD|MSG_DELETED);
387 procmsg_write_flags(&newmsginfo, fp);
391 return dest->last_num;
394 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
403 g_return_val_if_fail(dest != NULL, -1);
404 g_return_val_if_fail(msglist != NULL, -1);
406 if (dest->last_num < 0) {
407 mh_scan_folder(folder, dest);
408 if (dest->last_num < 0) return -1;
411 destdir = folder_item_get_path(dest);
412 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
413 g_warning(_("Can't open mark file.\n"));
415 for (cur = msglist; cur != NULL; cur = cur->next) {
416 msginfo = (MsgInfo *)cur->data;
418 if (msginfo->folder == dest) {
419 g_warning(_("the src folder is identical to the dest.\n"));
422 debug_print(_("Copying message %s%c%d to %s ...\n"),
423 msginfo->folder->path, G_DIR_SEPARATOR,
424 msginfo->msgnum, dest->path);
426 srcfile = procmsg_get_message_file_path(msginfo);
427 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
430 if (is_file_exist(destfile)) {
431 g_warning(_("%s already exists."), destfile);
437 if (copy_file(srcfile, destfile) < 0) {
438 FILE_OP_ERROR(srcfile, "copy");
451 newmsginfo.msgnum = dest->last_num;
452 newmsginfo.flags = msginfo->flags;
453 if (dest->stype == F_OUTBOX ||
454 dest->stype == F_QUEUE ||
455 dest->stype == F_DRAFT ||
456 dest->stype == F_TRASH)
457 MSG_UNSET_FLAGS(newmsginfo.flags,
458 MSG_NEW|MSG_UNREAD|MSG_DELETED);
459 procmsg_write_flags(&newmsginfo, fp);
466 return dest->last_num;
469 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
473 g_return_val_if_fail(item != NULL, -1);
475 file = mh_fetch_msg(folder, item, num);
476 g_return_val_if_fail(file != NULL, -1);
478 if (unlink(file) < 0) {
479 FILE_OP_ERROR(file, "unlink");
488 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
494 g_return_val_if_fail(item != NULL, -1);
496 path = folder_item_get_path(item);
497 g_return_val_if_fail(path != NULL, -1);
498 if (change_dir(path) < 0) {
504 if ((dp = opendir(".")) == NULL) {
505 FILE_OP_ERROR(item->path, "opendir");
509 while ((d = readdir(dp)) != NULL) {
510 if (to_number(d->d_name) < 0) continue;
511 if (unlink(d->d_name) < 0)
512 FILE_OP_ERROR(d->d_name, "unlink");
519 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
523 if (stat(itos(msginfo->msgnum), &s) < 0 ||
524 msginfo->size != s.st_size ||
525 msginfo->mtime != s.st_mtime)
531 void mh_scan_folder(Folder *folder, FolderItem *item)
541 g_return_if_fail(item != NULL);
543 path = folder_item_get_path(item);
544 g_return_if_fail(path != NULL);
545 if (change_dir(path) < 0) {
551 if ((dp = opendir(".")) == NULL) {
552 FILE_OP_ERROR(item->path, "opendir");
557 folder->ui_func(folder, item, folder->ui_func_data);
559 while ((d = readdir(dp)) != NULL) {
560 if ((num = to_number(d->d_name)) >= 0 &&
561 stat(d->d_name, &s) == 0 &&
562 S_ISREG(s.st_mode)) {
572 item->new = item->unread = item->total = 0;
574 gint new, unread, total;
576 procmsg_get_mark_sum(".", &new, &unread, &total);
578 new += n_msg - total;
579 unread += n_msg - total;
582 item->unread = unread;
586 debug_print(_("Last number in dir %s = %d\n"), item->path, max);
587 item->last_num = max;
590 void mh_scan_tree(Folder *folder)
595 g_return_if_fail(folder != NULL);
597 folder_tree_destroy(folder);
598 item = folder_item_new(folder->name, NULL);
599 item->folder = folder;
600 folder->node = g_node_new(item);
602 rootpath = folder_item_get_path(item);
603 if (change_dir(rootpath) < 0) {
609 mh_scan_tree_recursive(item);
612 #define MAKE_DIR_IF_NOT_EXIST(dir) \
614 if (!is_dir_exist(dir)) { \
615 if (is_file_exist(dir)) { \
616 g_warning(_("File `%s' already exists.\n" \
617 "Can't create folder."), dir); \
620 if (mkdir(dir, S_IRWXU) < 0) { \
621 FILE_OP_ERROR(dir, "mkdir"); \
624 if (chmod(dir, S_IRWXU) < 0) \
625 FILE_OP_ERROR(dir, "chmod"); \
629 gint mh_create_tree(Folder *folder)
633 g_return_val_if_fail(folder != NULL, -1);
635 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
636 rootpath = LOCAL_FOLDER(folder)->rootpath;
637 MAKE_DIR_IF_NOT_EXIST(rootpath);
638 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
639 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
640 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
641 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
642 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
643 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
648 #undef MAKE_DIR_IF_NOT_EXIST
650 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
655 FolderItem *new_item;
657 g_return_val_if_fail(folder != NULL, NULL);
658 g_return_val_if_fail(parent != NULL, NULL);
659 g_return_val_if_fail(name != NULL, NULL);
661 path = folder_item_get_path(parent);
662 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
665 if (mkdir(fullpath, S_IRWXU) < 0) {
666 FILE_OP_ERROR(fullpath, "mkdir");
670 if (chmod(fullpath, S_IRWXU) < 0)
671 FILE_OP_ERROR(fullpath, "chmod");
676 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
679 path = g_strdup(name);
680 new_item = folder_item_new(name, path);
681 folder_item_append(parent, new_item);
687 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
695 g_return_val_if_fail(folder != NULL, -1);
696 g_return_val_if_fail(item != NULL, -1);
697 g_return_val_if_fail(item->path != NULL, -1);
698 g_return_val_if_fail(name != NULL, -1);
700 oldpath = folder_item_get_path(item);
701 dirname = g_dirname(oldpath);
702 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
705 if (rename(oldpath, newpath) < 0) {
706 FILE_OP_ERROR(oldpath, "rename");
715 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
716 dirname = g_dirname(item->path);
717 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
720 newpath = g_strdup(name);
723 item->name = g_strdup(name);
725 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
727 paths[0] = g_strdup(item->path);
729 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
730 mh_rename_folder_func, paths);
737 gint mh_remove_folder(Folder *folder, FolderItem *item)
741 g_return_val_if_fail(folder != NULL, -1);
742 g_return_val_if_fail(item != NULL, -1);
743 g_return_val_if_fail(item->path != NULL, -1);
745 path = folder_item_get_path(item);
746 if (remove_dir_recursive(path) < 0) {
747 g_warning("can't remove directory `%s'\n", path);
753 folder_item_remove(item);
758 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
764 GSList *newlist = NULL;
770 g_return_val_if_fail(item != NULL, NULL);
772 path = folder_item_get_path(item);
773 g_return_val_if_fail(path != NULL, NULL);
774 if (change_dir(path) < 0) {
780 if ((dp = opendir(".")) == NULL) {
781 FILE_OP_ERROR(item->path, "opendir");
785 debug_print(_("\tSearching uncached messages... "));
788 while ((d = readdir(dp)) != NULL) {
789 if ((num = to_number(d->d_name)) < 0) continue;
790 if (stat(d->d_name, &s) < 0) {
791 FILE_OP_ERROR(d->d_name, "stat");
794 if (!S_ISREG(s.st_mode)) continue;
796 msginfo = g_hash_table_lookup
797 (msg_table, GUINT_TO_POINTER(num));
800 /* not found in the cache (uncached message) */
801 msginfo = mh_parse_msg(d->d_name, item);
802 if (!msginfo) continue;
806 g_slist_append(NULL, msginfo);
808 last = g_slist_append(last, msginfo);
815 /* discard all previous cache */
816 while ((d = readdir(dp)) != NULL) {
817 if (to_number(d->d_name) < 0) continue;
818 if (stat(d->d_name, &s) < 0) {
819 FILE_OP_ERROR(d->d_name, "stat");
822 if (!S_ISREG(s.st_mode)) continue;
824 msginfo = mh_parse_msg(d->d_name, item);
825 if (!msginfo) continue;
828 last = newlist = g_slist_append(NULL, msginfo);
830 last = g_slist_append(last, msginfo);
840 debug_print(_("%d uncached message(s) found.\n"), n_newmsg);
842 debug_print(_("done.\n"));
844 /* sort new messages in numerical order */
846 debug_print(_("\tSorting uncached messages in numerical order... "));
847 newlist = g_slist_sort
848 (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
849 debug_print(_("done.\n"));
855 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
859 MsgFlags flags = MSG_NEW|MSG_UNREAD;
861 g_return_val_if_fail(item != NULL, NULL);
862 g_return_val_if_fail(file != NULL, NULL);
864 if (item->stype == F_QUEUE) {
865 MSG_SET_FLAGS(flags, MSG_QUEUED);
866 } else if (item->stype == F_DRAFT) {
867 MSG_SET_FLAGS(flags, MSG_DRAFT);
870 msginfo = procheader_parse(file, flags, FALSE);
871 if (!msginfo) return NULL;
873 msginfo->msgnum = atoi(file);
874 msginfo->folder = item;
876 if (stat(file, &s) < 0) {
877 FILE_OP_ERROR(file, "stat");
881 msginfo->size = s.st_size;
882 msginfo->mtime = s.st_mtime;
888 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
893 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
894 result = is_dir_exist(entry);
901 * check whether PATH is a Maildir style mailbox.
902 * This is the case if the 3 subdir: new, cur, tmp are existing.
903 * This functon assumes that entry is an directory
905 static gboolean mh_is_maildir(const gchar *path)
907 return mh_is_maildir_one(path, "new") &&
908 mh_is_maildir_one(path, "cur") &&
909 mh_is_maildir_one(path, "tmp");
912 static void mh_scan_tree_recursive(FolderItem *item)
920 g_return_if_fail(item != NULL);
921 g_return_if_fail(item->folder != NULL);
923 dp = opendir(item->path ? item->path : ".");
925 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
929 debug_print("scanning %s ...\n",
930 item->path ? item->path
931 : LOCAL_FOLDER(item->folder)->rootpath);
932 if (item->folder->ui_func)
933 item->folder->ui_func(item->folder, item,
934 item->folder->ui_func_data);
936 while ((d = readdir(dp)) != NULL) {
937 if (d->d_name[0] == '.') continue;
940 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
943 entry = g_strdup(d->d_name);
945 if (stat(entry, &s) < 0) {
946 FILE_OP_ERROR(entry, "stat");
951 if (S_ISDIR(s.st_mode)) {
952 FolderItem *new_item;
954 if (mh_is_maildir(entry)) {
959 new_item = folder_item_new(d->d_name, entry);
960 folder_item_append(item, new_item);
962 if (!strcmp(d->d_name, "inbox")) {
963 new_item->stype = F_INBOX;
964 item->folder->inbox = new_item;
965 } else if (!strcmp(d->d_name, "outbox")) {
966 new_item->stype = F_OUTBOX;
967 item->folder->outbox = new_item;
968 } else if (!strcmp(d->d_name, "draft")) {
969 new_item->stype = F_DRAFT;
970 item->folder->draft = new_item;
971 } else if (!strcmp(d->d_name, "queue")) {
972 new_item->stype = F_QUEUE;
973 item->folder->queue = new_item;
974 } else if (!strcmp(d->d_name, "trash")) {
975 new_item->stype = F_TRASH;
976 item->folder->trash = new_item;
979 mh_scan_tree_recursive(new_item);
980 } else if (to_number(d->d_name) != -1) n_msg++;
988 gint new, unread, total;
990 procmsg_get_mark_sum(item->path, &new, &unread, &total);
992 new += n_msg - total;
993 unread += n_msg - total;
996 item->unread = unread;
1001 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1003 FolderItem *item = node->data;
1004 gchar **paths = data;
1005 const gchar *oldpath = paths[0];
1006 const gchar *newpath = paths[1];
1008 gchar *new_itempath;
1011 oldpathlen = strlen(oldpath);
1012 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1013 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1017 base = item->path + oldpathlen;
1018 while (*base == G_DIR_SEPARATOR) base++;
1020 new_itempath = g_strdup(newpath);
1022 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1025 item->path = new_itempath;