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,
58 static gint mh_add_msgs(Folder * folder,
59 FolderItem * dest, GSList * file_list, GRelation *relation);
60 static gint mh_copy_msg(Folder * folder,
61 FolderItem * dest, MsgInfo * msginfo);
62 static gint mh_remove_msg(Folder * folder, FolderItem * item, gint num);
63 static gint mh_remove_all_msg(Folder * folder, FolderItem * item);
64 static gboolean mh_is_msg_changed(Folder * folder,
65 FolderItem * item, MsgInfo * msginfo);
67 static gint mh_get_num_list(Folder * folder,
68 FolderItem * item, GSList ** list);
69 static void mh_scan_tree(Folder * folder);
71 static gint mh_create_tree(Folder * folder);
72 static FolderItem *mh_create_folder(Folder * folder,
75 static gint mh_rename_folder(Folder * folder,
76 FolderItem * item, const gchar * name);
77 static gint mh_remove_folder(Folder * folder, FolderItem * item);
79 static gchar *mh_get_new_msg_filename(FolderItem * dest);
81 static MsgInfo *mh_parse_msg(const gchar * file, FolderItem * item);
82 static void mh_scan_tree_recursive(FolderItem * item);
84 static gboolean mh_rename_folder_func(GNode * node, gpointer data);
85 static gchar *mh_item_get_path(Folder *folder, FolderItem *item);
87 FolderClass mh_class =
93 /* Folder functions */
99 /* FolderItem functions */
113 /* Message functions */
127 FolderClass *mh_get_class(void)
132 Folder *mh_folder_new(const gchar *name, const gchar *path)
136 folder = (Folder *)g_new0(MHFolder, 1);
137 folder->klass = &mh_class;
138 mh_folder_init(folder, name, path);
143 static void mh_folder_destroy(Folder *folder)
145 folder_local_folder_destroy(LOCAL_FOLDER(folder));
148 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
150 folder_local_folder_init(folder, name, path);
154 void mh_get_last_num(Folder *folder, FolderItem *item)
163 g_return_if_fail(item != NULL);
165 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
167 path = folder_item_get_path(item);
168 g_return_if_fail(path != NULL);
169 if (change_dir(path) < 0) {
175 if ((dp = opendir(".")) == NULL) {
176 FILE_OP_ERROR(item->path, "opendir");
180 while ((d = readdir(dp)) != NULL) {
181 if ((num = to_number(d->d_name)) >= 0 &&
182 stat(d->d_name, &s) == 0 &&
183 S_ISREG(s.st_mode)) {
190 debug_print("Last number in dir %s = %d\n", item->path, max);
191 item->last_num = max;
194 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list)
201 gint num, nummsgs = 0;
203 g_return_val_if_fail(item != NULL, -1);
205 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
207 path = folder_item_get_path(item);
208 g_return_val_if_fail(path != NULL, -1);
209 if (change_dir(path) < 0) {
215 if ((dp = opendir(".")) == NULL) {
216 FILE_OP_ERROR(item->path, "opendir");
220 while ((d = readdir(dp)) != NULL) {
221 if ((num = to_number(d->d_name)) >= 0 &&
222 stat(d->d_name, &s) == 0 &&
223 S_ISREG(s.st_mode)) {
224 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
233 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
238 g_return_val_if_fail(item != NULL, NULL);
239 g_return_val_if_fail(num > 0, NULL);
241 path = folder_item_get_path(item);
242 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
244 if (!is_file_exist(file)) {
252 MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
257 g_return_val_if_fail(item != NULL, NULL);
258 g_return_val_if_fail(num > 0, NULL);
260 file = mh_fetch_msg(folder, item, num);
261 if (!file) return NULL;
263 msginfo = mh_parse_msg(file, item);
265 msginfo->msgnum = num;
272 gchar *mh_get_new_msg_filename(FolderItem *dest)
277 destpath = folder_item_get_path(dest);
278 g_return_val_if_fail(destpath != NULL, NULL);
280 if (!is_dir_exist(destpath))
281 make_dir_hier(destpath);
284 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
286 if (is_file_entry_exist(destfile)) {
298 #define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
300 MsgInfo newmsginfo; \
302 newmsginfo.msgnum = dest->last_num; \
303 newmsginfo.flags = msginfo->flags; \
304 if (dest->stype == F_OUTBOX || \
305 dest->stype == F_QUEUE || \
306 dest->stype == F_DRAFT || \
307 dest->stype == F_TRASH) \
308 MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
309 MSG_NEW|MSG_UNREAD|MSG_DELETED); \
311 procmsg_write_flags(&newmsginfo, fp); \
314 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
318 MsgFileInfo fileinfo;
320 g_return_val_if_fail(file != NULL, -1);
322 fileinfo.file = (gchar *)file;
323 fileinfo.flags = flags;
324 file_list.data = &fileinfo;
325 file_list.next = NULL;
327 ret = mh_add_msgs(folder, dest, &file_list, NULL);
331 gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
336 MsgFileInfo *fileinfo;
338 g_return_val_if_fail(dest != NULL, -1);
339 g_return_val_if_fail(file_list != NULL, -1);
341 if (dest->last_num < 0) {
342 mh_get_last_num(folder, dest);
343 if (dest->last_num < 0) return -1;
346 for (cur = file_list; cur != NULL; cur = cur->next) {
347 fileinfo = (MsgFileInfo *)cur->data;
349 destfile = mh_get_new_msg_filename(dest);
350 if (destfile == NULL) return -1;
352 if (link(fileinfo->file, destfile) < 0) {
353 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
354 g_warning(_("can't copy message %s to %s\n"),
355 fileinfo->file, destfile);
360 if (relation != NULL)
361 g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
367 return dest->last_num;
370 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
375 FolderItemPrefs *prefs;
377 g_return_val_if_fail(dest != NULL, -1);
378 g_return_val_if_fail(msginfo != NULL, -1);
380 if (msginfo->folder == dest) {
381 g_warning("the src folder is identical to the dest.\n");
385 if (dest->last_num < 0) {
386 mh_get_last_num(folder, dest);
387 if (dest->last_num < 0) return -1;
392 srcfile = procmsg_get_message_file(msginfo);
393 destfile = mh_get_new_msg_filename(dest);
399 debug_print("Copying message %s%c%d to %s ...\n",
400 msginfo->folder->path, G_DIR_SEPARATOR,
401 msginfo->msgnum, dest->path);
404 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
405 && dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
406 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
411 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
412 FILE_OP_ERROR(srcfile, "copy");
419 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
420 if (chmod(destfile, prefs->folder_chmod) < 0)
421 FILE_OP_ERROR(destfile, "chmod");
424 filemode = prefs->folder_chmod;
425 if (filemode & S_IRGRP) filemode |= S_IWGRP;
426 if (filemode & S_IROTH) filemode |= S_IWOTH;
433 return dest->last_num;
436 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
440 g_return_val_if_fail(item != NULL, -1);
442 file = mh_fetch_msg(folder, item, num);
443 g_return_val_if_fail(file != NULL, -1);
445 if (unlink(file) < 0) {
446 FILE_OP_ERROR(file, "unlink");
455 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
460 g_return_val_if_fail(item != NULL, -1);
462 path = folder_item_get_path(item);
463 g_return_val_if_fail(path != NULL, -1);
464 val = remove_all_numbered_files(path);
470 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
474 if (stat(itos(msginfo->msgnum), &s) < 0 ||
475 msginfo->size != s.st_size ||
476 msginfo->mtime != s.st_mtime)
482 void mh_scan_tree(Folder *folder)
487 g_return_if_fail(folder != NULL);
489 item = folder_item_new(folder, folder->name, NULL);
490 item->folder = folder;
491 folder->node = g_node_new(item);
493 rootpath = folder_item_get_path(item);
494 if (change_dir(rootpath) < 0) {
500 mh_create_tree(folder);
501 mh_scan_tree_recursive(item);
504 #define MAKE_DIR_IF_NOT_EXIST(dir) \
506 if (!is_dir_exist(dir)) { \
507 if (is_file_exist(dir)) { \
508 g_warning("File `%s' already exists.\n" \
509 "Can't create folder.", dir); \
512 if (make_dir(dir) < 0) \
517 gint mh_create_tree(Folder *folder)
521 g_return_val_if_fail(folder != NULL, -1);
523 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
524 rootpath = LOCAL_FOLDER(folder)->rootpath;
525 MAKE_DIR_IF_NOT_EXIST(rootpath);
526 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
527 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
528 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
529 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
530 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
531 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
536 #undef MAKE_DIR_IF_NOT_EXIST
538 gchar *mh_item_get_path(Folder *folder, FolderItem *item)
540 gchar *folder_path, *path;
542 g_return_val_if_fail(folder != NULL, NULL);
543 g_return_val_if_fail(item != NULL, NULL);
545 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
546 g_return_val_if_fail(folder_path != NULL, NULL);
548 if (folder_path[0] == G_DIR_SEPARATOR) {
550 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
553 path = g_strdup(folder_path);
556 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
557 folder_path, G_DIR_SEPARATOR_S,
560 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
568 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
573 FolderItem *new_item;
574 gchar *mh_sequences_filename;
575 FILE *mh_sequences_file;
577 g_return_val_if_fail(folder != NULL, NULL);
578 g_return_val_if_fail(parent != NULL, NULL);
579 g_return_val_if_fail(name != NULL, NULL);
581 path = folder_item_get_path(parent);
582 if (!is_dir_exist(path))
583 if (make_dir_hier(path) != 0)
586 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
589 if (make_dir(fullpath) < 0) {
597 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
600 path = g_strdup(name);
601 new_item = folder_item_new(folder, name, path);
602 folder_item_append(parent, new_item);
606 path = folder_item_get_path(new_item);
607 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
608 ".mh_sequences", NULL);
609 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
610 fclose(mh_sequences_file);
612 g_free(mh_sequences_filename);
618 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
626 g_return_val_if_fail(folder != NULL, -1);
627 g_return_val_if_fail(item != NULL, -1);
628 g_return_val_if_fail(item->path != NULL, -1);
629 g_return_val_if_fail(name != NULL, -1);
631 oldpath = folder_item_get_path(item);
632 if (!is_dir_exist(oldpath))
633 make_dir_hier(oldpath);
635 dirname = g_dirname(oldpath);
636 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
639 if (rename(oldpath, newpath) < 0) {
640 FILE_OP_ERROR(oldpath, "rename");
649 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
650 dirname = g_dirname(item->path);
651 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
654 newpath = g_strdup(name);
657 item->name = g_strdup(name);
659 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
661 paths[0] = g_strdup(item->path);
663 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
664 mh_rename_folder_func, paths);
671 gint mh_remove_folder(Folder *folder, FolderItem *item)
675 g_return_val_if_fail(folder != NULL, -1);
676 g_return_val_if_fail(item != NULL, -1);
677 g_return_val_if_fail(item->path != NULL, -1);
679 path = folder_item_get_path(item);
680 if (remove_dir_recursive(path) < 0) {
681 g_warning("can't remove directory `%s'\n", path);
687 folder_item_remove(item);
691 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
697 flags.perm_flags = MSG_NEW|MSG_UNREAD;
700 g_return_val_if_fail(item != NULL, NULL);
701 g_return_val_if_fail(file != NULL, NULL);
703 if (item->stype == F_QUEUE) {
704 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
705 } else if (item->stype == F_DRAFT) {
706 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
709 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
710 if (!msginfo) return NULL;
712 msginfo->msgnum = atoi(file);
713 msginfo->folder = item;
715 if (stat(file, &s) < 0) {
716 FILE_OP_ERROR(file, "stat");
720 msginfo->size = s.st_size;
721 msginfo->mtime = s.st_mtime;
728 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
733 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
734 result = is_dir_exist(entry);
741 * check whether PATH is a Maildir style mailbox.
742 * This is the case if the 3 subdir: new, cur, tmp are existing.
743 * This functon assumes that entry is an directory
745 static gboolean mh_is_maildir(const gchar *path)
747 return mh_is_maildir_one(path, "new") &&
748 mh_is_maildir_one(path, "cur") &&
749 mh_is_maildir_one(path, "tmp");
753 static void mh_scan_tree_recursive(FolderItem *item)
761 g_return_if_fail(item != NULL);
762 g_return_if_fail(item->folder != NULL);
764 dp = opendir(item->path ? item->path : ".");
766 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
770 debug_print("scanning %s ...\n",
771 item->path ? item->path
772 : LOCAL_FOLDER(item->folder)->rootpath);
773 if (item->folder->ui_func)
774 item->folder->ui_func(item->folder, item,
775 item->folder->ui_func_data);
777 while ((d = readdir(dp)) != NULL) {
778 if (d->d_name[0] == '.') continue;
781 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
784 entry = g_strdup(d->d_name);
786 if (stat(entry, &s) < 0) {
787 FILE_OP_ERROR(entry, "stat");
792 if (S_ISDIR(s.st_mode)) {
793 FolderItem *new_item;
796 if (mh_is_maildir(entry)) {
802 new_item = folder_item_new(item->folder, d->d_name, entry);
803 folder_item_append(item, new_item);
805 if (!strcmp(d->d_name, INBOX_DIR)) {
806 new_item->stype = F_INBOX;
807 item->folder->inbox = new_item;
808 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
809 new_item->stype = F_OUTBOX;
810 item->folder->outbox = new_item;
811 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
812 new_item->stype = F_DRAFT;
813 item->folder->draft = new_item;
814 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
815 new_item->stype = F_QUEUE;
816 item->folder->queue = new_item;
817 } else if (!strcmp(d->d_name, TRASH_DIR)) {
818 new_item->stype = F_TRASH;
819 item->folder->trash = new_item;
822 mh_scan_tree_recursive(new_item);
823 } else if (to_number(d->d_name) != -1) n_msg++;
832 gint new, unread, total, min, max;
834 procmsg_get_mark_sum(item->path, &new, &unread, &total,
837 new += n_msg - total;
838 unread += n_msg - total;
841 item->unread = unread;
847 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
849 FolderItem *item = node->data;
850 gchar **paths = data;
851 const gchar *oldpath = paths[0];
852 const gchar *newpath = paths[1];
857 oldpathlen = strlen(oldpath);
858 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
859 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
863 base = item->path + oldpathlen;
864 while (*base == G_DIR_SEPARATOR) base++;
866 new_itempath = g_strdup(newpath);
868 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
871 item->path = new_itempath;