2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
302 MsgFileInfo fileinfo;
304 g_return_val_if_fail(file != NULL, -1);
306 fileinfo.msginfo = NULL;
307 fileinfo.file = (gchar *)file;
308 fileinfo.flags = flags;
309 file_list.data = &fileinfo;
310 file_list.next = NULL;
312 ret = mh_add_msgs(folder, dest, &file_list, NULL);
316 gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
321 MsgFileInfo *fileinfo;
323 g_return_val_if_fail(dest != NULL, -1);
324 g_return_val_if_fail(file_list != NULL, -1);
326 if (dest->last_num < 0) {
327 mh_get_last_num(folder, dest);
328 if (dest->last_num < 0) return -1;
331 for (cur = file_list; cur != NULL; cur = cur->next) {
332 fileinfo = (MsgFileInfo *)cur->data;
334 destfile = mh_get_new_msg_filename(dest);
335 if (destfile == NULL) return -1;
337 if (link(fileinfo->file, destfile) < 0) {
338 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
339 g_warning(_("can't copy message %s to %s\n"),
340 fileinfo->file, destfile);
345 if (relation != NULL)
346 g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
351 return dest->last_num;
354 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
359 FolderItemPrefs *prefs;
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_get_last_num(folder, dest);
371 if (dest->last_num < 0) return -1;
376 srcfile = procmsg_get_message_file(msginfo);
377 destfile = mh_get_new_msg_filename(dest);
383 debug_print("Copying message %s%c%d to %s ...\n",
384 msginfo->folder->path, G_DIR_SEPARATOR,
385 msginfo->msgnum, dest->path);
388 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
389 && dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
390 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
395 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
396 FILE_OP_ERROR(srcfile, "copy");
403 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
404 if (chmod(destfile, prefs->folder_chmod) < 0)
405 FILE_OP_ERROR(destfile, "chmod");
408 filemode = prefs->folder_chmod;
409 if (filemode & S_IRGRP) filemode |= S_IWGRP;
410 if (filemode & S_IROTH) filemode |= S_IWOTH;
417 return dest->last_num;
420 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
424 g_return_val_if_fail(item != NULL, -1);
426 file = mh_fetch_msg(folder, item, num);
427 g_return_val_if_fail(file != NULL, -1);
429 if (unlink(file) < 0) {
430 FILE_OP_ERROR(file, "unlink");
439 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
444 g_return_val_if_fail(item != NULL, -1);
446 path = folder_item_get_path(item);
447 g_return_val_if_fail(path != NULL, -1);
448 val = remove_all_numbered_files(path);
454 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
458 if (stat(itos(msginfo->msgnum), &s) < 0 ||
459 msginfo->size != s.st_size ||
460 msginfo->mtime != s.st_mtime)
466 void mh_scan_tree(Folder *folder)
471 g_return_if_fail(folder != NULL);
473 item = folder_item_new(folder, folder->name, NULL);
474 item->folder = folder;
475 folder->node = g_node_new(item);
477 rootpath = folder_item_get_path(item);
478 if (change_dir(rootpath) < 0) {
484 mh_create_tree(folder);
485 mh_scan_tree_recursive(item);
488 #define MAKE_DIR_IF_NOT_EXIST(dir) \
490 if (!is_dir_exist(dir)) { \
491 if (is_file_exist(dir)) { \
492 g_warning("File `%s' already exists.\n" \
493 "Can't create folder.", dir); \
496 if (make_dir(dir) < 0) \
501 gint mh_create_tree(Folder *folder)
505 g_return_val_if_fail(folder != NULL, -1);
507 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
508 rootpath = LOCAL_FOLDER(folder)->rootpath;
509 MAKE_DIR_IF_NOT_EXIST(rootpath);
510 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
511 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
512 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
513 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
514 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
515 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
520 #undef MAKE_DIR_IF_NOT_EXIST
522 gchar *mh_item_get_path(Folder *folder, FolderItem *item)
524 gchar *folder_path, *path;
526 g_return_val_if_fail(folder != NULL, NULL);
527 g_return_val_if_fail(item != NULL, NULL);
529 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
530 g_return_val_if_fail(folder_path != NULL, NULL);
532 if (folder_path[0] == G_DIR_SEPARATOR) {
534 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
537 path = g_strdup(folder_path);
540 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
541 folder_path, G_DIR_SEPARATOR_S,
544 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
552 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
557 FolderItem *new_item;
558 gchar *mh_sequences_filename;
559 FILE *mh_sequences_file;
561 g_return_val_if_fail(folder != NULL, NULL);
562 g_return_val_if_fail(parent != NULL, NULL);
563 g_return_val_if_fail(name != NULL, NULL);
565 path = folder_item_get_path(parent);
566 if (!is_dir_exist(path))
567 if (make_dir_hier(path) != 0)
570 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
573 if (make_dir(fullpath) < 0) {
581 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
584 path = g_strdup(name);
585 new_item = folder_item_new(folder, name, path);
586 folder_item_append(parent, new_item);
590 path = folder_item_get_path(new_item);
591 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
592 ".mh_sequences", NULL);
593 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
594 fclose(mh_sequences_file);
596 g_free(mh_sequences_filename);
602 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
610 g_return_val_if_fail(folder != NULL, -1);
611 g_return_val_if_fail(item != NULL, -1);
612 g_return_val_if_fail(item->path != NULL, -1);
613 g_return_val_if_fail(name != NULL, -1);
615 oldpath = folder_item_get_path(item);
616 if (!is_dir_exist(oldpath))
617 make_dir_hier(oldpath);
619 dirname = g_dirname(oldpath);
620 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
623 if (rename(oldpath, newpath) < 0) {
624 FILE_OP_ERROR(oldpath, "rename");
633 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
634 dirname = g_dirname(item->path);
635 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
638 newpath = g_strdup(name);
641 item->name = g_strdup(name);
643 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
645 paths[0] = g_strdup(item->path);
647 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
648 mh_rename_folder_func, paths);
655 gint mh_remove_folder(Folder *folder, FolderItem *item)
659 g_return_val_if_fail(folder != NULL, -1);
660 g_return_val_if_fail(item != NULL, -1);
661 g_return_val_if_fail(item->path != NULL, -1);
663 path = folder_item_get_path(item);
664 if (remove_dir_recursive(path) < 0) {
665 g_warning("can't remove directory `%s'\n", path);
671 folder_item_remove(item);
675 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
681 flags.perm_flags = MSG_NEW|MSG_UNREAD;
684 g_return_val_if_fail(item != NULL, NULL);
685 g_return_val_if_fail(file != NULL, NULL);
687 if (item->stype == F_QUEUE) {
688 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
689 } else if (item->stype == F_DRAFT) {
690 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
693 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
694 if (!msginfo) return NULL;
696 msginfo->msgnum = atoi(file);
697 msginfo->folder = item;
699 if (stat(file, &s) < 0) {
700 FILE_OP_ERROR(file, "stat");
704 msginfo->size = s.st_size;
705 msginfo->mtime = s.st_mtime;
712 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
717 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
718 result = is_dir_exist(entry);
725 * check whether PATH is a Maildir style mailbox.
726 * This is the case if the 3 subdir: new, cur, tmp are existing.
727 * This functon assumes that entry is an directory
729 static gboolean mh_is_maildir(const gchar *path)
731 return mh_is_maildir_one(path, "new") &&
732 mh_is_maildir_one(path, "cur") &&
733 mh_is_maildir_one(path, "tmp");
737 static void mh_scan_tree_recursive(FolderItem *item)
745 g_return_if_fail(item != NULL);
746 g_return_if_fail(item->folder != NULL);
748 dp = opendir(item->path ? item->path : ".");
750 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
754 debug_print("scanning %s ...\n",
755 item->path ? item->path
756 : LOCAL_FOLDER(item->folder)->rootpath);
757 if (item->folder->ui_func)
758 item->folder->ui_func(item->folder, item,
759 item->folder->ui_func_data);
761 while ((d = readdir(dp)) != NULL) {
762 if (d->d_name[0] == '.') continue;
765 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
768 entry = g_strdup(d->d_name);
770 if (stat(entry, &s) < 0) {
771 FILE_OP_ERROR(entry, "stat");
776 if (S_ISDIR(s.st_mode)) {
777 FolderItem *new_item;
780 if (mh_is_maildir(entry)) {
786 new_item = folder_item_new(item->folder, d->d_name, entry);
787 folder_item_append(item, new_item);
789 if (!strcmp(d->d_name, INBOX_DIR)) {
790 new_item->stype = F_INBOX;
791 item->folder->inbox = new_item;
792 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
793 new_item->stype = F_OUTBOX;
794 item->folder->outbox = new_item;
795 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
796 new_item->stype = F_DRAFT;
797 item->folder->draft = new_item;
798 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
799 new_item->stype = F_QUEUE;
800 item->folder->queue = new_item;
801 } else if (!strcmp(d->d_name, TRASH_DIR)) {
802 new_item->stype = F_TRASH;
803 item->folder->trash = new_item;
806 mh_scan_tree_recursive(new_item);
807 } else if (to_number(d->d_name) != -1) n_msg++;
816 gint new, unread, total, min, max;
818 procmsg_get_mark_sum(item->path, &new, &unread, &total,
821 new += n_msg - total;
822 unread += n_msg - total;
825 item->unread = unread;
831 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
833 FolderItem *item = node->data;
834 gchar **paths = data;
835 const gchar *oldpath = paths[0];
836 const gchar *newpath = paths[1];
841 oldpathlen = strlen(oldpath);
842 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
843 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
847 base = item->path + oldpathlen;
848 while (*base == G_DIR_SEPARATOR) base++;
850 new_itempath = g_strdup(newpath);
852 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
855 item->path = new_itempath;