2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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 void mh_folder_init(Folder * folder,
47 const gchar * name, const gchar * path);
49 static Folder *mh_folder_new(const gchar * name, const gchar * path);
50 static void mh_folder_destroy(Folder * folder);
51 static gchar *mh_fetch_msg(Folder * folder, FolderItem * item, gint num);
52 static MsgInfo *mh_get_msginfo(Folder * folder,
53 FolderItem * item, gint num);
54 static gint mh_add_msg(Folder * folder,
56 const gchar * file, gboolean remove_source);
57 static gint mh_copy_msg(Folder * folder,
58 FolderItem * dest, MsgInfo * msginfo);
59 static gint mh_remove_msg(Folder * folder, FolderItem * item, gint num);
60 static gint mh_remove_all_msg(Folder * folder, FolderItem * item);
61 static gboolean mh_is_msg_changed(Folder * folder,
62 FolderItem * item, MsgInfo * msginfo);
64 static gint mh_get_num_list(Folder * folder,
65 FolderItem * item, GSList ** list);
66 static void mh_scan_tree(Folder * folder);
68 static gint mh_create_tree(Folder * folder);
69 static FolderItem *mh_create_folder(Folder * folder,
72 static gint mh_rename_folder(Folder * folder,
73 FolderItem * item, const gchar * name);
74 static gint mh_remove_folder(Folder * folder, FolderItem * item);
76 static gchar *mh_get_new_msg_filename(FolderItem * dest);
78 static MsgInfo *mh_parse_msg(const gchar * file, FolderItem * item);
79 static void mh_scan_tree_recursive(FolderItem * item);
81 static gboolean mh_rename_folder_func(GNode * node, gpointer data);
84 FolderClass mh_class =
90 /* Folder functions */
96 /* FolderItem functions */
108 /* Message functions */
120 FolderClass *mh_get_class(void)
125 Folder *mh_folder_new(const gchar *name, const gchar *path)
129 folder = (Folder *)g_new0(MHFolder, 1);
130 folder->klass = &mh_class;
131 mh_folder_init(folder, name, path);
136 static void mh_folder_destroy(Folder *folder)
138 folder_local_folder_destroy(LOCAL_FOLDER(folder));
141 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
143 folder_local_folder_init(folder, name, path);
147 void mh_get_last_num(Folder *folder, FolderItem *item)
156 g_return_if_fail(item != NULL);
158 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
160 path = folder_item_get_path(item);
161 g_return_if_fail(path != NULL);
162 if (change_dir(path) < 0) {
168 if ((dp = opendir(".")) == NULL) {
169 FILE_OP_ERROR(item->path, "opendir");
173 while ((d = readdir(dp)) != NULL) {
174 if ((num = to_number(d->d_name)) >= 0 &&
175 stat(d->d_name, &s) == 0 &&
176 S_ISREG(s.st_mode)) {
183 debug_print("Last number in dir %s = %d\n", item->path, max);
184 item->last_num = max;
187 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list)
194 gint num, nummsgs = 0;
196 g_return_val_if_fail(item != NULL, -1);
198 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
200 path = folder_item_get_path(item);
201 g_return_val_if_fail(path != NULL, -1);
202 if (change_dir(path) < 0) {
208 if ((dp = opendir(".")) == NULL) {
209 FILE_OP_ERROR(item->path, "opendir");
213 while ((d = readdir(dp)) != NULL) {
214 if ((num = to_number(d->d_name)) >= 0 &&
215 stat(d->d_name, &s) == 0 &&
216 S_ISREG(s.st_mode)) {
217 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
226 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
231 g_return_val_if_fail(item != NULL, NULL);
232 g_return_val_if_fail(num > 0, NULL);
234 path = folder_item_get_path(item);
235 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
237 if (!is_file_exist(file)) {
245 MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
250 g_return_val_if_fail(item != NULL, NULL);
251 g_return_val_if_fail(num > 0, NULL);
253 file = mh_fetch_msg(folder, item, num);
254 if (!file) return NULL;
256 msginfo = mh_parse_msg(file, item);
258 msginfo->msgnum = num;
265 gchar *mh_get_new_msg_filename(FolderItem *dest)
270 destpath = folder_item_get_path(dest);
271 g_return_val_if_fail(destpath != NULL, NULL);
273 if (!is_dir_exist(destpath))
274 make_dir_hier(destpath);
277 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
279 if (is_file_entry_exist(destfile)) {
291 #define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
293 MsgInfo newmsginfo; \
295 newmsginfo.msgnum = dest->last_num; \
296 newmsginfo.flags = msginfo->flags; \
297 if (dest->stype == F_OUTBOX || \
298 dest->stype == F_QUEUE || \
299 dest->stype == F_DRAFT || \
300 dest->stype == F_TRASH) \
301 MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
302 MSG_NEW|MSG_UNREAD|MSG_DELETED); \
304 procmsg_write_flags(&newmsginfo, fp); \
307 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
308 gboolean remove_source)
312 g_return_val_if_fail(dest != NULL, -1);
313 g_return_val_if_fail(file != NULL, -1);
315 if (dest->last_num < 0) {
316 mh_get_last_num(folder, dest);
317 if (dest->last_num < 0) return -1;
320 destfile = mh_get_new_msg_filename(dest);
321 g_return_val_if_fail(destfile != NULL, -1);
323 if (link(file, destfile) < 0) {
324 if (copy_file(file, destfile, TRUE) < 0) {
325 g_warning("can't copy message %s to %s\n",
333 if (unlink(file) < 0)
334 FILE_OP_ERROR(file, "unlink");
339 return dest->last_num;
342 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
347 PrefsFolderItem *prefs;
349 g_return_val_if_fail(dest != NULL, -1);
350 g_return_val_if_fail(msginfo != NULL, -1);
352 if (msginfo->folder == dest) {
353 g_warning("the src folder is identical to the dest.\n");
357 if (dest->last_num < 0) {
358 mh_get_last_num(folder, dest);
359 if (dest->last_num < 0) return -1;
364 srcfile = procmsg_get_message_file(msginfo);
365 destfile = mh_get_new_msg_filename(dest);
371 debug_print("Copying message %s%c%d to %s ...\n",
372 msginfo->folder->path, G_DIR_SEPARATOR,
373 msginfo->msgnum, dest->path);
376 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
377 && dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
378 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
383 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
384 FILE_OP_ERROR(srcfile, "copy");
391 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
392 if (chmod(destfile, prefs->folder_chmod) < 0)
393 FILE_OP_ERROR(destfile, "chmod");
396 filemode = prefs->folder_chmod;
397 if (filemode & S_IRGRP) filemode |= S_IWGRP;
398 if (filemode & S_IROTH) filemode |= S_IWOTH;
405 return dest->last_num;
408 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
412 g_return_val_if_fail(item != NULL, -1);
414 file = mh_fetch_msg(folder, item, num);
415 g_return_val_if_fail(file != NULL, -1);
417 if (unlink(file) < 0) {
418 FILE_OP_ERROR(file, "unlink");
427 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
432 g_return_val_if_fail(item != NULL, -1);
434 path = folder_item_get_path(item);
435 g_return_val_if_fail(path != NULL, -1);
436 val = remove_all_numbered_files(path);
442 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
446 if (stat(itos(msginfo->msgnum), &s) < 0 ||
447 msginfo->size != s.st_size ||
448 msginfo->mtime != s.st_mtime)
454 void mh_scan_tree(Folder *folder)
459 g_return_if_fail(folder != NULL);
461 item = folder_item_new(folder, folder->name, NULL);
462 item->folder = folder;
463 folder->node = g_node_new(item);
465 rootpath = folder_item_get_path(item);
466 if (change_dir(rootpath) < 0) {
472 mh_create_tree(folder);
473 mh_scan_tree_recursive(item);
476 #define MAKE_DIR_IF_NOT_EXIST(dir) \
478 if (!is_dir_exist(dir)) { \
479 if (is_file_exist(dir)) { \
480 g_warning("File `%s' already exists.\n" \
481 "Can't create folder.", dir); \
484 if (make_dir(dir) < 0) \
489 gint mh_create_tree(Folder *folder)
493 g_return_val_if_fail(folder != NULL, -1);
495 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
496 rootpath = LOCAL_FOLDER(folder)->rootpath;
497 MAKE_DIR_IF_NOT_EXIST(rootpath);
498 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
499 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
500 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
501 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
502 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
503 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
508 #undef MAKE_DIR_IF_NOT_EXIST
510 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
515 FolderItem *new_item;
516 gchar *mh_sequences_filename;
517 FILE *mh_sequences_file;
519 g_return_val_if_fail(folder != NULL, NULL);
520 g_return_val_if_fail(parent != NULL, NULL);
521 g_return_val_if_fail(name != NULL, NULL);
523 path = folder_item_get_path(parent);
524 if (!is_dir_exist(path))
525 if (make_dir_hier(path) != 0)
528 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
531 if (make_dir(fullpath) < 0) {
539 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
542 path = g_strdup(name);
543 new_item = folder_item_new(folder, name, path);
544 folder_item_append(parent, new_item);
548 path = folder_item_get_path(new_item);
549 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
550 ".mh_sequences", NULL);
551 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
552 fclose(mh_sequences_file);
554 g_free(mh_sequences_filename);
560 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
568 g_return_val_if_fail(folder != NULL, -1);
569 g_return_val_if_fail(item != NULL, -1);
570 g_return_val_if_fail(item->path != NULL, -1);
571 g_return_val_if_fail(name != NULL, -1);
573 oldpath = folder_item_get_path(item);
574 if (!is_dir_exist(oldpath))
575 make_dir_hier(oldpath);
577 dirname = g_dirname(oldpath);
578 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
581 if (rename(oldpath, newpath) < 0) {
582 FILE_OP_ERROR(oldpath, "rename");
591 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
592 dirname = g_dirname(item->path);
593 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
596 newpath = g_strdup(name);
599 item->name = g_strdup(name);
601 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
603 paths[0] = g_strdup(item->path);
605 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
606 mh_rename_folder_func, paths);
613 gint mh_remove_folder(Folder *folder, FolderItem *item)
617 g_return_val_if_fail(folder != NULL, -1);
618 g_return_val_if_fail(item != NULL, -1);
619 g_return_val_if_fail(item->path != NULL, -1);
621 path = folder_item_get_path(item);
622 if (remove_dir_recursive(path) < 0) {
623 g_warning("can't remove directory `%s'\n", path);
629 folder_item_remove(item);
633 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
639 flags.perm_flags = MSG_NEW|MSG_UNREAD;
642 g_return_val_if_fail(item != NULL, NULL);
643 g_return_val_if_fail(file != NULL, NULL);
645 if (item->stype == F_QUEUE) {
646 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
647 } else if (item->stype == F_DRAFT) {
648 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
651 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
652 if (!msginfo) return NULL;
654 msginfo->msgnum = atoi(file);
655 msginfo->folder = item;
657 if (stat(file, &s) < 0) {
658 FILE_OP_ERROR(file, "stat");
662 msginfo->size = s.st_size;
663 msginfo->mtime = s.st_mtime;
670 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
675 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
676 result = is_dir_exist(entry);
683 * check whether PATH is a Maildir style mailbox.
684 * This is the case if the 3 subdir: new, cur, tmp are existing.
685 * This functon assumes that entry is an directory
687 static gboolean mh_is_maildir(const gchar *path)
689 return mh_is_maildir_one(path, "new") &&
690 mh_is_maildir_one(path, "cur") &&
691 mh_is_maildir_one(path, "tmp");
695 static void mh_scan_tree_recursive(FolderItem *item)
703 g_return_if_fail(item != NULL);
704 g_return_if_fail(item->folder != NULL);
706 dp = opendir(item->path ? item->path : ".");
708 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
712 debug_print("scanning %s ...\n",
713 item->path ? item->path
714 : LOCAL_FOLDER(item->folder)->rootpath);
715 if (item->folder->ui_func)
716 item->folder->ui_func(item->folder, item,
717 item->folder->ui_func_data);
719 while ((d = readdir(dp)) != NULL) {
720 if (d->d_name[0] == '.') continue;
723 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
726 entry = g_strdup(d->d_name);
728 if (stat(entry, &s) < 0) {
729 FILE_OP_ERROR(entry, "stat");
734 if (S_ISDIR(s.st_mode)) {
735 FolderItem *new_item;
738 if (mh_is_maildir(entry)) {
744 new_item = folder_item_new(item->folder, d->d_name, entry);
745 folder_item_append(item, new_item);
747 if (!strcmp(d->d_name, INBOX_DIR)) {
748 new_item->stype = F_INBOX;
749 item->folder->inbox = new_item;
750 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
751 new_item->stype = F_OUTBOX;
752 item->folder->outbox = new_item;
753 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
754 new_item->stype = F_DRAFT;
755 item->folder->draft = new_item;
756 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
757 new_item->stype = F_QUEUE;
758 item->folder->queue = new_item;
759 } else if (!strcmp(d->d_name, TRASH_DIR)) {
760 new_item->stype = F_TRASH;
761 item->folder->trash = new_item;
764 mh_scan_tree_recursive(new_item);
765 } else if (to_number(d->d_name) != -1) n_msg++;
774 gint new, unread, total, min, max;
776 procmsg_get_mark_sum(item->path, &new, &unread, &total,
779 new += n_msg - total;
780 unread += n_msg - total;
783 item->unread = unread;
789 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
791 FolderItem *item = node->data;
792 gchar **paths = data;
793 const gchar *oldpath = paths[0];
794 const gchar *newpath = paths[1];
799 oldpathlen = strlen(oldpath);
800 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
801 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
805 base = item->path + oldpathlen;
806 while (*base == G_DIR_SEPARATOR) base++;
808 new_itempath = g_strdup(newpath);
810 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
813 item->path = new_itempath;