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);
73 if (stat(path, &s) < 0) {
74 FILE_OP_ERROR(path, "stat");
76 if (item->mtime == s.st_mtime) {
77 debug_print("Folder is not modified.\n");
80 item->mtime = s.st_mtime;
84 if (use_cache && !scan_new) {
85 mlist = procmsg_read_cache(item, FALSE);
87 mlist = mh_get_uncached_msgs(NULL, item);
88 } else if (use_cache) {
91 mlist = procmsg_read_cache(item, TRUE);
92 msg_table = procmsg_msg_hash_table_create(mlist);
94 newlist = mh_get_uncached_msgs(msg_table, item);
96 g_hash_table_destroy(msg_table);
98 mlist = g_slist_concat(mlist, newlist);
100 mlist = mh_get_uncached_msgs(NULL, item);
102 procmsg_set_flags(mlist, item);
105 gettimeofday(&tv_after, NULL);
107 timersub(&tv_after, &tv_before, &tv_result);
108 g_print("mh_get_msg_list: %s: elapsed time: %ld.%06ld sec\n",
109 item->path, tv_result.tv_sec, tv_result.tv_usec);
115 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
120 g_return_val_if_fail(item != NULL, NULL);
121 g_return_val_if_fail(num > 0 && num <= item->last_num, NULL);
123 path = folder_item_get_path(item);
125 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
127 if (!is_file_exist(file)) {
135 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
136 gboolean remove_source)
141 g_return_val_if_fail(dest != NULL, -1);
142 g_return_val_if_fail(file != NULL, -1);
144 if (dest->last_num < 0) {
145 mh_scan_folder(folder, dest);
146 if (dest->last_num < 0) return -1;
149 destpath = folder_item_get_path(dest);
150 g_return_val_if_fail(destpath != NULL, -1);
151 if (!is_dir_exist(destpath))
152 make_dir_hier(destpath);
154 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
157 if (link(file, destfile) < 0) {
158 if (EXDEV == errno) {
159 if (copy_file(file, destfile) < 0) {
160 g_warning(_("can't copy message %s to %s\n"),
166 FILE_OP_ERROR(file, "link");
173 if (unlink(file) < 0)
174 FILE_OP_ERROR(file, "unlink");
179 return dest->last_num;
182 gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
189 g_return_val_if_fail(dest != NULL, -1);
190 g_return_val_if_fail(msginfo != NULL, -1);
192 if (msginfo->folder == dest) {
193 g_warning(_("the src folder is identical to the dest.\n"));
197 if (dest->last_num < 0) {
198 mh_scan_folder(folder, dest);
199 if (dest->last_num < 0) return -1;
202 destdir = folder_item_get_path(dest);
204 if (!is_dir_exist(destdir))
205 make_dir_hier(destdir);
207 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
208 g_warning(_("Can't open mark file.\n"));
210 debug_print(_("Moving message %s%c%d to %s ...\n"),
211 msginfo->folder->path, G_DIR_SEPARATOR,
212 msginfo->msgnum, dest->path);
213 srcfile = procmsg_get_message_file_path(msginfo);
214 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
218 if (is_file_exist(destfile)) {
219 g_warning(_("%s already exists."), destfile);
226 if (rename(srcfile, destfile) < 0) {
227 if (EXDEV == errno) {
228 if (copy_file(srcfile, destfile) < 0) {
235 FILE_OP_ERROR(srcfile, "rename");
250 newmsginfo.msgnum = dest->last_num;
251 newmsginfo.flags = msginfo->flags;
252 if (dest->stype == F_OUTBOX ||
253 dest->stype == F_QUEUE ||
254 dest->stype == F_DRAFT ||
255 dest->stype == F_TRASH)
256 MSG_UNSET_FLAGS(newmsginfo.flags,
257 MSG_NEW|MSG_UNREAD|MSG_DELETED);
259 procmsg_write_flags(&newmsginfo, fp);
263 return dest->last_num;
266 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
275 g_return_val_if_fail(dest != NULL, -1);
276 g_return_val_if_fail(msglist != NULL, -1);
278 if (dest->last_num < 0) {
279 mh_scan_folder(folder, dest);
280 if (dest->last_num < 0) return -1;
283 destdir = folder_item_get_path(dest);
284 if (!is_dir_exist(destdir))
285 make_dir_hier(destdir);
287 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
288 g_warning(_("Can't open mark file.\n"));
290 for (cur = msglist; cur != NULL; cur = cur->next) {
291 msginfo = (MsgInfo *)cur->data;
293 if (msginfo->folder == dest) {
294 g_warning(_("the src folder is identical to the dest.\n"));
297 debug_print(_("Moving message %s%c%d to %s ...\n"),
298 msginfo->folder->path, G_DIR_SEPARATOR,
299 msginfo->msgnum, dest->path);
301 srcfile = procmsg_get_message_file_path(msginfo);
302 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
305 if (is_file_exist(destfile)) {
306 g_warning(_("%s already exists."), destfile);
312 if (rename(srcfile, destfile) < 0) {
313 if (EXDEV == errno) {
314 if (copy_file(srcfile, destfile) < 0) {
321 FILE_OP_ERROR(srcfile, "rename");
335 newmsginfo.msgnum = dest->last_num;
336 newmsginfo.flags = msginfo->flags;
337 if (dest->stype == F_OUTBOX ||
338 dest->stype == F_QUEUE ||
339 dest->stype == F_DRAFT ||
340 dest->stype == F_TRASH)
341 MSG_UNSET_FLAGS(newmsginfo.flags,
342 MSG_NEW|MSG_UNREAD|MSG_DELETED);
344 procmsg_write_flags(&newmsginfo, fp);
351 return dest->last_num;
354 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
361 g_return_val_if_fail(dest != NULL, -1);
362 g_return_val_if_fail(msginfo != NULL, -1);
364 if (msginfo->folder == dest) {
365 g_warning(_("the src folder is identical to the dest.\n"));
369 if (dest->last_num < 0) {
370 mh_scan_folder(folder, dest);
371 if (dest->last_num < 0) return -1;
374 destdir = folder_item_get_path(dest);
375 if (!is_dir_exist(destdir))
376 make_dir_hier(destdir);
378 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
379 g_warning(_("Can't open mark file.\n"));
381 debug_print(_("Copying message %s%c%d to %s ...\n"),
382 msginfo->folder->path, G_DIR_SEPARATOR,
383 msginfo->msgnum, dest->path);
384 srcfile = procmsg_get_message_file_path(msginfo);
385 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
389 if (is_file_exist(destfile)) {
390 g_warning(_("%s already exists."), destfile);
397 if (copy_file(srcfile, destfile) < 0) {
398 FILE_OP_ERROR(srcfile, "copy");
412 newmsginfo.msgnum = dest->last_num;
413 newmsginfo.flags = msginfo->flags;
414 if (dest->stype == F_OUTBOX ||
415 dest->stype == F_QUEUE ||
416 dest->stype == F_DRAFT ||
417 dest->stype == F_TRASH)
418 MSG_UNSET_FLAGS(newmsginfo.flags,
419 MSG_NEW|MSG_UNREAD|MSG_DELETED);
420 procmsg_write_flags(&newmsginfo, fp);
424 return dest->last_num;
427 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
436 g_return_val_if_fail(dest != NULL, -1);
437 g_return_val_if_fail(msglist != NULL, -1);
439 if (dest->last_num < 0) {
440 mh_scan_folder(folder, dest);
441 if (dest->last_num < 0) return -1;
444 destdir = folder_item_get_path(dest);
445 if (!is_dir_exist(destdir))
446 make_dir_hier(destdir);
448 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
449 g_warning(_("Can't open mark file.\n"));
451 for (cur = msglist; cur != NULL; cur = cur->next) {
452 msginfo = (MsgInfo *)cur->data;
454 if (msginfo->folder == dest) {
455 g_warning(_("the src folder is identical to the dest.\n"));
458 debug_print(_("Copying message %s%c%d to %s ...\n"),
459 msginfo->folder->path, G_DIR_SEPARATOR,
460 msginfo->msgnum, dest->path);
462 srcfile = procmsg_get_message_file_path(msginfo);
463 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
466 if (is_file_exist(destfile)) {
467 g_warning(_("%s already exists."), destfile);
473 if (copy_file(srcfile, destfile) < 0) {
474 FILE_OP_ERROR(srcfile, "copy");
487 newmsginfo.msgnum = dest->last_num;
488 newmsginfo.flags = msginfo->flags;
489 if (dest->stype == F_OUTBOX ||
490 dest->stype == F_QUEUE ||
491 dest->stype == F_DRAFT ||
492 dest->stype == F_TRASH)
493 MSG_UNSET_FLAGS(newmsginfo.flags,
494 MSG_NEW|MSG_UNREAD|MSG_DELETED);
495 procmsg_write_flags(&newmsginfo, fp);
502 return dest->last_num;
505 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
509 g_return_val_if_fail(item != NULL, -1);
511 file = mh_fetch_msg(folder, item, num);
512 g_return_val_if_fail(file != NULL, -1);
514 if (unlink(file) < 0) {
515 FILE_OP_ERROR(file, "unlink");
524 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 if (change_dir(path) < 0) {
540 if ((dp = opendir(".")) == NULL) {
541 FILE_OP_ERROR(item->path, "opendir");
545 while ((d = readdir(dp)) != NULL) {
546 if (to_number(d->d_name) < 0) continue;
547 if (unlink(d->d_name) < 0)
548 FILE_OP_ERROR(d->d_name, "unlink");
555 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
559 if (stat(itos(msginfo->msgnum), &s) < 0 ||
560 msginfo->size != s.st_size ||
561 msginfo->mtime != s.st_mtime)
567 void mh_scan_folder(Folder *folder, FolderItem *item)
577 g_return_if_fail(item != NULL);
579 path = folder_item_get_path(item);
580 g_return_if_fail(path != NULL);
581 if (change_dir(path) < 0) {
587 if ((dp = opendir(".")) == NULL) {
588 FILE_OP_ERROR(item->path, "opendir");
593 folder->ui_func(folder, item, folder->ui_func_data);
595 while ((d = readdir(dp)) != NULL) {
596 if ((num = to_number(d->d_name)) >= 0 &&
597 stat(d->d_name, &s) == 0 &&
598 S_ISREG(s.st_mode)) {
608 item->new = item->unread = item->total = 0;
610 gint new, unread, total;
612 procmsg_get_mark_sum(".", &new, &unread, &total);
614 new += n_msg - total;
615 unread += n_msg - total;
618 item->unread = unread;
622 debug_print(_("Last number in dir %s = %d\n"), item->path, max);
623 item->last_num = max;
626 void mh_scan_tree(Folder *folder)
631 g_return_if_fail(folder != NULL);
633 folder_tree_destroy(folder);
634 item = folder_item_new(folder->name, NULL);
635 item->folder = folder;
636 folder->node = g_node_new(item);
638 rootpath = folder_item_get_path(item);
639 if (change_dir(rootpath) < 0) {
645 mh_scan_tree_recursive(item);
648 #define MAKE_DIR_IF_NOT_EXIST(dir) \
650 if (!is_dir_exist(dir)) { \
651 if (is_file_exist(dir)) { \
652 g_warning(_("File `%s' already exists.\n" \
653 "Can't create folder."), dir); \
656 if (mkdir(dir, S_IRWXU) < 0) { \
657 FILE_OP_ERROR(dir, "mkdir"); \
660 if (chmod(dir, S_IRWXU) < 0) \
661 FILE_OP_ERROR(dir, "chmod"); \
665 gint mh_create_tree(Folder *folder)
669 g_return_val_if_fail(folder != NULL, -1);
671 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
672 rootpath = LOCAL_FOLDER(folder)->rootpath;
673 MAKE_DIR_IF_NOT_EXIST(rootpath);
674 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
675 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
676 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
677 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
678 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
679 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
684 #undef MAKE_DIR_IF_NOT_EXIST
686 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
691 FolderItem *new_item;
693 g_return_val_if_fail(folder != NULL, NULL);
694 g_return_val_if_fail(parent != NULL, NULL);
695 g_return_val_if_fail(name != NULL, NULL);
697 path = folder_item_get_path(parent);
698 if (!is_dir_exist(path))
701 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
704 if (mkdir(fullpath, S_IRWXU) < 0) {
705 FILE_OP_ERROR(fullpath, "mkdir");
709 if (chmod(fullpath, S_IRWXU) < 0)
710 FILE_OP_ERROR(fullpath, "chmod");
715 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
718 path = g_strdup(name);
719 new_item = folder_item_new(name, path);
720 folder_item_append(parent, new_item);
726 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
734 g_return_val_if_fail(folder != NULL, -1);
735 g_return_val_if_fail(item != NULL, -1);
736 g_return_val_if_fail(item->path != NULL, -1);
737 g_return_val_if_fail(name != NULL, -1);
739 oldpath = folder_item_get_path(item);
740 if (!is_dir_exist(oldpath))
741 make_dir_hier(oldpath);
743 dirname = g_dirname(oldpath);
744 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
747 if (rename(oldpath, newpath) < 0) {
748 FILE_OP_ERROR(oldpath, "rename");
757 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
758 dirname = g_dirname(item->path);
759 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
762 newpath = g_strdup(name);
765 item->name = g_strdup(name);
767 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
769 paths[0] = g_strdup(item->path);
771 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
772 mh_rename_folder_func, paths);
779 gint mh_remove_folder(Folder *folder, FolderItem *item)
783 g_return_val_if_fail(folder != NULL, -1);
784 g_return_val_if_fail(item != NULL, -1);
785 g_return_val_if_fail(item->path != NULL, -1);
787 path = folder_item_get_path(item);
788 if (remove_dir_recursive(path) < 0) {
789 g_warning("can't remove directory `%s'\n", path);
795 folder_item_remove(item);
800 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
806 GSList *newlist = NULL;
812 g_return_val_if_fail(item != NULL, NULL);
814 path = folder_item_get_path(item);
815 g_return_val_if_fail(path != NULL, NULL);
816 if (change_dir(path) < 0) {
822 if ((dp = opendir(".")) == NULL) {
823 FILE_OP_ERROR(item->path, "opendir");
827 debug_print(_("\tSearching uncached messages... "));
830 while ((d = readdir(dp)) != NULL) {
831 if ((num = to_number(d->d_name)) < 0) continue;
832 if (stat(d->d_name, &s) < 0) {
833 FILE_OP_ERROR(d->d_name, "stat");
836 if (!S_ISREG(s.st_mode)) continue;
838 msginfo = g_hash_table_lookup
839 (msg_table, GUINT_TO_POINTER(num));
842 /* not found in the cache (uncached message) */
843 msginfo = mh_parse_msg(d->d_name, item);
844 if (!msginfo) continue;
848 g_slist_append(NULL, msginfo);
850 last = g_slist_append(last, msginfo);
857 /* discard all previous cache */
858 while ((d = readdir(dp)) != NULL) {
859 if (to_number(d->d_name) < 0) continue;
860 if (stat(d->d_name, &s) < 0) {
861 FILE_OP_ERROR(d->d_name, "stat");
864 if (!S_ISREG(s.st_mode)) continue;
866 msginfo = mh_parse_msg(d->d_name, item);
867 if (!msginfo) continue;
870 last = newlist = g_slist_append(NULL, msginfo);
872 last = g_slist_append(last, msginfo);
882 debug_print(_("%d uncached message(s) found.\n"), n_newmsg);
884 debug_print(_("done.\n"));
886 /* sort new messages in numerical order */
888 debug_print(_("\tSorting uncached messages in numerical order... "));
889 newlist = g_slist_sort
890 (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
891 debug_print(_("done.\n"));
897 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
901 MsgFlags flags = MSG_NEW|MSG_UNREAD;
903 g_return_val_if_fail(item != NULL, NULL);
904 g_return_val_if_fail(file != NULL, NULL);
906 if (item->stype == F_QUEUE) {
907 MSG_SET_FLAGS(flags, MSG_QUEUED);
908 } else if (item->stype == F_DRAFT) {
909 MSG_SET_FLAGS(flags, MSG_DRAFT);
912 msginfo = procheader_parse(file, flags, FALSE);
913 if (!msginfo) return NULL;
915 msginfo->msgnum = atoi(file);
916 msginfo->folder = item;
918 if (stat(file, &s) < 0) {
919 FILE_OP_ERROR(file, "stat");
923 msginfo->size = s.st_size;
924 msginfo->mtime = s.st_mtime;
930 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
935 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
936 result = is_dir_exist(entry);
943 * check whether PATH is a Maildir style mailbox.
944 * This is the case if the 3 subdir: new, cur, tmp are existing.
945 * This functon assumes that entry is an directory
947 static gboolean mh_is_maildir(const gchar *path)
949 return mh_is_maildir_one(path, "new") &&
950 mh_is_maildir_one(path, "cur") &&
951 mh_is_maildir_one(path, "tmp");
954 static void mh_scan_tree_recursive(FolderItem *item)
962 g_return_if_fail(item != NULL);
963 g_return_if_fail(item->folder != NULL);
965 dp = opendir(item->path ? item->path : ".");
967 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
971 debug_print("scanning %s ...\n",
972 item->path ? item->path
973 : LOCAL_FOLDER(item->folder)->rootpath);
974 if (item->folder->ui_func)
975 item->folder->ui_func(item->folder, item,
976 item->folder->ui_func_data);
978 while ((d = readdir(dp)) != NULL) {
979 if (d->d_name[0] == '.') continue;
982 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
985 entry = g_strdup(d->d_name);
987 if (stat(entry, &s) < 0) {
988 FILE_OP_ERROR(entry, "stat");
993 if (S_ISDIR(s.st_mode)) {
994 FolderItem *new_item;
996 if (mh_is_maildir(entry)) {
1001 new_item = folder_item_new(d->d_name, entry);
1002 folder_item_append(item, new_item);
1004 if (!strcmp(d->d_name, "inbox")) {
1005 new_item->stype = F_INBOX;
1006 item->folder->inbox = new_item;
1007 } else if (!strcmp(d->d_name, "outbox")) {
1008 new_item->stype = F_OUTBOX;
1009 item->folder->outbox = new_item;
1010 } else if (!strcmp(d->d_name, "draft")) {
1011 new_item->stype = F_DRAFT;
1012 item->folder->draft = new_item;
1013 } else if (!strcmp(d->d_name, "queue")) {
1014 new_item->stype = F_QUEUE;
1015 item->folder->queue = new_item;
1016 } else if (!strcmp(d->d_name, "trash")) {
1017 new_item->stype = F_TRASH;
1018 item->folder->trash = new_item;
1021 mh_scan_tree_recursive(new_item);
1022 } else if (to_number(d->d_name) != -1) n_msg++;
1030 gint new, unread, total;
1032 procmsg_get_mark_sum(item->path, &new, &unread, &total);
1033 if (n_msg > total) {
1034 new += n_msg - total;
1035 unread += n_msg - total;
1038 item->unread = unread;
1039 item->total = n_msg;
1043 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1045 FolderItem *item = node->data;
1046 gchar **paths = data;
1047 const gchar *oldpath = paths[0];
1048 const gchar *newpath = paths[1];
1050 gchar *new_itempath;
1053 oldpathlen = strlen(oldpath);
1054 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1055 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1059 base = item->path + oldpathlen;
1060 while (*base == G_DIR_SEPARATOR) base++;
1062 new_itempath = g_strdup(newpath);
1064 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1067 item->path = new_itempath;