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) {
170 g_warning(_("the folder of the message is not defined\n"));
174 if (msginfo->folder == dest) {
175 g_warning(_("the src folder is identical to the dest.\n"));
179 if (dest->last_num < 0) {
180 mh_scan_folder(folder, dest);
181 if (dest->last_num < 0) return -1;
184 destdir = folder_item_get_path(dest);
185 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
186 g_warning(_("Can't open mark file.\n"));
188 debug_print(_("Moving message %s%c%d to %s ...\n"),
189 msginfo->folder->path, G_DIR_SEPARATOR,
190 msginfo->msgnum, dest->path);
191 srcfile = procmsg_get_message_file_path(msginfo);
192 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
196 if (is_file_exist(destfile)) {
197 g_warning(_("%s already exists."), destfile);
204 if (rename(srcfile, destfile) < 0) {
205 if (EXDEV == errno) {
206 if (copy_file(srcfile, destfile) < 0) {
213 FILE_OP_ERROR(srcfile, "rename");
228 newmsginfo.msgnum = dest->last_num;
229 newmsginfo.flags = msginfo->flags;
230 if (dest->stype == F_OUTBOX ||
231 dest->stype == F_QUEUE ||
232 dest->stype == F_DRAFT ||
233 dest->stype == F_TRASH)
234 MSG_UNSET_FLAGS(newmsginfo.flags,
235 MSG_NEW|MSG_UNREAD|MSG_DELETED);
237 procmsg_write_flags(&newmsginfo, fp);
241 return dest->last_num;
244 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
253 g_return_val_if_fail(dest != NULL, -1);
254 g_return_val_if_fail(msglist != NULL, -1);
256 if (dest->last_num < 0) {
257 mh_scan_folder(folder, dest);
258 if (dest->last_num < 0) return -1;
261 destdir = folder_item_get_path(dest);
262 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
263 g_warning(_("Can't open mark file.\n"));
265 for (cur = msglist; cur != NULL; cur = cur->next) {
266 msginfo = (MsgInfo *)cur->data;
268 if (msginfo->folder == dest) {
269 g_warning(_("the src folder is identical to the dest.\n"));
272 debug_print(_("Moving message %s%c%d to %s ...\n"),
273 msginfo->folder->path, G_DIR_SEPARATOR,
274 msginfo->msgnum, dest->path);
276 srcfile = procmsg_get_message_file_path(msginfo);
277 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
280 if (is_file_exist(destfile)) {
281 g_warning(_("%s already exists."), destfile);
287 if (rename(srcfile, destfile) < 0) {
288 if (EXDEV == errno) {
289 if (copy_file(srcfile, destfile) < 0) {
296 FILE_OP_ERROR(srcfile, "rename");
310 newmsginfo.msgnum = dest->last_num;
311 newmsginfo.flags = msginfo->flags;
312 if (dest->stype == F_OUTBOX ||
313 dest->stype == F_QUEUE ||
314 dest->stype == F_DRAFT ||
315 dest->stype == F_TRASH)
316 MSG_UNSET_FLAGS(newmsginfo.flags,
317 MSG_NEW|MSG_UNREAD|MSG_DELETED);
319 procmsg_write_flags(&newmsginfo, fp);
326 return dest->last_num;
329 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
336 g_return_val_if_fail(dest != NULL, -1);
337 g_return_val_if_fail(msginfo != NULL, -1);
339 if (!msginfo->folder) {
340 g_warning(_("the folder of the message is not defined\n"));
344 if (msginfo->folder == dest) {
345 g_warning(_("the src folder is identical to the dest.\n"));
349 if (dest->last_num < 0) {
350 mh_scan_folder(folder, dest);
351 if (dest->last_num < 0) return -1;
354 destdir = folder_item_get_path(dest);
355 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
356 g_warning(_("Can't open mark file.\n"));
358 debug_print(_("Copying message %s%c%d to %s ...\n"),
359 msginfo->folder->path, G_DIR_SEPARATOR,
360 msginfo->msgnum, dest->path);
361 srcfile = procmsg_get_message_file_path(msginfo);
362 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
366 if (is_file_exist(destfile)) {
367 g_warning(_("%s already exists."), destfile);
374 if (copy_file(srcfile, destfile) < 0) {
375 FILE_OP_ERROR(srcfile, "copy");
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_FLAGS(newmsginfo.flags,
396 MSG_NEW|MSG_UNREAD|MSG_DELETED);
397 procmsg_write_flags(&newmsginfo, fp);
401 return dest->last_num;
404 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
413 g_return_val_if_fail(dest != NULL, -1);
414 g_return_val_if_fail(msglist != NULL, -1);
416 if (dest->last_num < 0) {
417 mh_scan_folder(folder, dest);
418 if (dest->last_num < 0) return -1;
421 destdir = folder_item_get_path(dest);
422 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
423 g_warning(_("Can't open mark file.\n"));
425 for (cur = msglist; cur != NULL; cur = cur->next) {
426 msginfo = (MsgInfo *)cur->data;
428 if (msginfo->folder == dest) {
429 g_warning(_("the src folder is identical to the dest.\n"));
432 debug_print(_("Copying message %s%c%d to %s ...\n"),
433 msginfo->folder->path, G_DIR_SEPARATOR,
434 msginfo->msgnum, dest->path);
436 srcfile = procmsg_get_message_file_path(msginfo);
437 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
440 if (is_file_exist(destfile)) {
441 g_warning(_("%s already exists."), destfile);
447 if (copy_file(srcfile, destfile) < 0) {
448 FILE_OP_ERROR(srcfile, "copy");
461 newmsginfo.msgnum = dest->last_num;
462 newmsginfo.flags = msginfo->flags;
463 if (dest->stype == F_OUTBOX ||
464 dest->stype == F_QUEUE ||
465 dest->stype == F_DRAFT ||
466 dest->stype == F_TRASH)
467 MSG_UNSET_FLAGS(newmsginfo.flags,
468 MSG_NEW|MSG_UNREAD|MSG_DELETED);
469 procmsg_write_flags(&newmsginfo, fp);
476 return dest->last_num;
479 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
483 g_return_val_if_fail(item != NULL, -1);
485 file = mh_fetch_msg(folder, item, num);
486 g_return_val_if_fail(file != NULL, -1);
488 if (unlink(file) < 0) {
489 FILE_OP_ERROR(file, "unlink");
498 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
504 g_return_val_if_fail(item != NULL, -1);
506 path = folder_item_get_path(item);
507 g_return_val_if_fail(path != NULL, -1);
508 if (change_dir(path) < 0) {
514 if ((dp = opendir(".")) == NULL) {
515 FILE_OP_ERROR(item->path, "opendir");
519 while ((d = readdir(dp)) != NULL) {
520 if (to_number(d->d_name) < 0) continue;
521 if (unlink(d->d_name) < 0)
522 FILE_OP_ERROR(d->d_name, "unlink");
529 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
533 if (stat(itos(msginfo->msgnum), &s) < 0 ||
534 msginfo->size != s.st_size ||
535 msginfo->mtime != s.st_mtime)
541 void mh_scan_folder(Folder *folder, FolderItem *item)
551 g_return_if_fail(item != NULL);
553 path = folder_item_get_path(item);
554 g_return_if_fail(path != NULL);
555 if (change_dir(path) < 0) {
561 if ((dp = opendir(".")) == NULL) {
562 FILE_OP_ERROR(item->path, "opendir");
567 folder->ui_func(folder, item, folder->ui_func_data);
569 while ((d = readdir(dp)) != NULL) {
570 if ((num = to_number(d->d_name)) >= 0 &&
571 stat(d->d_name, &s) == 0 &&
572 S_ISREG(s.st_mode)) {
582 item->new = item->unread = item->total = 0;
584 gint new, unread, total;
586 procmsg_get_mark_sum(".", &new, &unread, &total);
588 new += n_msg - total;
589 unread += n_msg - total;
592 item->unread = unread;
596 debug_print(_("Last number in dir %s = %d\n"), item->path, max);
597 item->last_num = max;
600 void mh_scan_tree(Folder *folder)
605 g_return_if_fail(folder != NULL);
607 folder_tree_destroy(folder);
608 item = folder_item_new(folder->name, NULL);
609 item->folder = folder;
610 folder->node = g_node_new(item);
612 rootpath = folder_item_get_path(item);
613 if (change_dir(rootpath) < 0) {
619 mh_scan_tree_recursive(item);
622 #define MAKE_DIR_IF_NOT_EXIST(dir) \
624 if (!is_dir_exist(dir)) { \
625 if (is_file_exist(dir)) { \
626 g_warning(_("File `%s' already exists.\n" \
627 "Can't create folder."), dir); \
630 if (mkdir(dir, S_IRWXU) < 0) { \
631 FILE_OP_ERROR(dir, "mkdir"); \
634 if (chmod(dir, S_IRWXU) < 0) \
635 FILE_OP_ERROR(dir, "chmod"); \
639 gint mh_create_tree(Folder *folder)
643 g_return_val_if_fail(folder != NULL, -1);
645 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
646 rootpath = LOCAL_FOLDER(folder)->rootpath;
647 MAKE_DIR_IF_NOT_EXIST(rootpath);
648 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
649 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
650 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
651 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
652 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
653 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
658 #undef MAKE_DIR_IF_NOT_EXIST
660 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
665 FolderItem *new_item;
667 g_return_val_if_fail(folder != NULL, NULL);
668 g_return_val_if_fail(parent != NULL, NULL);
669 g_return_val_if_fail(name != NULL, NULL);
671 path = folder_item_get_path(parent);
672 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
675 if (mkdir(fullpath, S_IRWXU) < 0) {
676 FILE_OP_ERROR(fullpath, "mkdir");
680 if (chmod(fullpath, S_IRWXU) < 0)
681 FILE_OP_ERROR(fullpath, "chmod");
686 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
689 path = g_strdup(name);
690 new_item = folder_item_new(name, path);
691 folder_item_append(parent, new_item);
697 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
705 g_return_val_if_fail(folder != NULL, -1);
706 g_return_val_if_fail(item != NULL, -1);
707 g_return_val_if_fail(item->path != NULL, -1);
708 g_return_val_if_fail(name != NULL, -1);
710 oldpath = folder_item_get_path(item);
711 dirname = g_dirname(oldpath);
712 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
715 if (rename(oldpath, newpath) < 0) {
716 FILE_OP_ERROR(oldpath, "rename");
725 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
726 dirname = g_dirname(item->path);
727 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
730 newpath = g_strdup(name);
733 item->name = g_strdup(name);
735 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
737 paths[0] = g_strdup(item->path);
739 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
740 mh_rename_folder_func, paths);
747 gint mh_remove_folder(Folder *folder, FolderItem *item)
751 g_return_val_if_fail(folder != NULL, -1);
752 g_return_val_if_fail(item != NULL, -1);
753 g_return_val_if_fail(item->path != NULL, -1);
755 path = folder_item_get_path(item);
756 if (remove_dir_recursive(path) < 0) {
757 g_warning("can't remove directory `%s'\n", path);
763 folder_item_remove(item);
768 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
774 GSList *newlist = NULL;
780 g_return_val_if_fail(item != NULL, NULL);
782 path = folder_item_get_path(item);
783 g_return_val_if_fail(path != NULL, NULL);
784 if (change_dir(path) < 0) {
790 if ((dp = opendir(".")) == NULL) {
791 FILE_OP_ERROR(item->path, "opendir");
795 debug_print(_("\tSearching uncached messages... "));
798 while ((d = readdir(dp)) != NULL) {
799 if ((num = to_number(d->d_name)) < 0) continue;
800 if (stat(d->d_name, &s) < 0) {
801 FILE_OP_ERROR(d->d_name, "stat");
804 if (!S_ISREG(s.st_mode)) continue;
806 msginfo = g_hash_table_lookup
807 (msg_table, GUINT_TO_POINTER(num));
810 /* not found in the cache (uncached message) */
811 msginfo = mh_parse_msg(d->d_name, item);
812 if (!msginfo) continue;
816 g_slist_append(NULL, msginfo);
818 last = g_slist_append(last, msginfo);
825 /* discard all previous cache */
826 while ((d = readdir(dp)) != NULL) {
827 if (to_number(d->d_name) < 0) continue;
828 if (stat(d->d_name, &s) < 0) {
829 FILE_OP_ERROR(d->d_name, "stat");
832 if (!S_ISREG(s.st_mode)) continue;
834 msginfo = mh_parse_msg(d->d_name, item);
835 if (!msginfo) continue;
838 last = newlist = g_slist_append(NULL, msginfo);
840 last = g_slist_append(last, msginfo);
850 debug_print(_("%d uncached message(s) found.\n"), n_newmsg);
852 debug_print(_("done.\n"));
854 /* sort new messages in numerical order */
856 debug_print(_("\tSorting uncached messages in numerical order... "));
857 newlist = g_slist_sort
858 (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
859 debug_print(_("done.\n"));
865 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
869 MsgFlags flags = MSG_NEW|MSG_UNREAD;
871 g_return_val_if_fail(item != NULL, NULL);
872 g_return_val_if_fail(file != NULL, NULL);
874 if (item->stype == F_QUEUE) {
875 MSG_SET_FLAGS(flags, MSG_QUEUED);
876 } else if (item->stype == F_DRAFT) {
877 MSG_SET_FLAGS(flags, MSG_DRAFT);
880 msginfo = procheader_parse(file, flags, FALSE);
881 if (!msginfo) return NULL;
883 msginfo->msgnum = atoi(file);
884 msginfo->folder = item;
886 if (stat(file, &s) < 0) {
887 FILE_OP_ERROR(file, "stat");
891 msginfo->size = s.st_size;
892 msginfo->mtime = s.st_mtime;
898 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
903 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
904 result = is_dir_exist(entry);
911 * check whether PATH is a Maildir style mailbox.
912 * This is the case if the 3 subdir: new, cur, tmp are existing.
913 * This functon assumes that entry is an directory
915 static gboolean mh_is_maildir(const gchar *path)
917 return mh_is_maildir_one(path, "new") &&
918 mh_is_maildir_one(path, "cur") &&
919 mh_is_maildir_one(path, "tmp");
922 static void mh_scan_tree_recursive(FolderItem *item)
930 g_return_if_fail(item != NULL);
931 g_return_if_fail(item->folder != NULL);
933 dp = opendir(item->path ? item->path : ".");
935 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
939 debug_print("scanning %s ...\n",
940 item->path ? item->path
941 : LOCAL_FOLDER(item->folder)->rootpath);
942 if (item->folder->ui_func)
943 item->folder->ui_func(item->folder, item,
944 item->folder->ui_func_data);
946 while ((d = readdir(dp)) != NULL) {
947 if (d->d_name[0] == '.') continue;
950 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
953 entry = g_strdup(d->d_name);
955 if (stat(entry, &s) < 0) {
956 FILE_OP_ERROR(entry, "stat");
961 if (S_ISDIR(s.st_mode)) {
962 FolderItem *new_item;
964 if (mh_is_maildir(entry)) {
969 new_item = folder_item_new(d->d_name, entry);
970 folder_item_append(item, new_item);
972 if (!strcmp(d->d_name, "inbox")) {
973 new_item->stype = F_INBOX;
974 item->folder->inbox = new_item;
975 } else if (!strcmp(d->d_name, "outbox")) {
976 new_item->stype = F_OUTBOX;
977 item->folder->outbox = new_item;
978 } else if (!strcmp(d->d_name, "draft")) {
979 new_item->stype = F_DRAFT;
980 item->folder->draft = new_item;
981 } else if (!strcmp(d->d_name, "queue")) {
982 new_item->stype = F_QUEUE;
983 item->folder->queue = new_item;
984 } else if (!strcmp(d->d_name, "trash")) {
985 new_item->stype = F_TRASH;
986 item->folder->trash = new_item;
989 mh_scan_tree_recursive(new_item);
990 } else if (to_number(d->d_name) != -1) n_msg++;
998 gint new, unread, total;
1000 procmsg_get_mark_sum(item->path, &new, &unread, &total);
1001 if (n_msg > total) {
1002 new += n_msg - total;
1003 unread += n_msg - total;
1006 item->unread = unread;
1007 item->total = n_msg;
1011 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1013 FolderItem *item = node->data;
1014 gchar **paths = data;
1015 const gchar *oldpath = paths[0];
1016 const gchar *newpath = paths[1];
1018 gchar *new_itempath;
1021 oldpathlen = strlen(oldpath);
1022 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1023 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1027 base = item->path + oldpathlen;
1028 while (*base == G_DIR_SEPARATOR) base++;
1030 new_itempath = g_strdup(newpath);
1032 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1035 item->path = new_itempath;