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);
82 static gchar *mh_item_get_path(Folder *folder, FolderItem *item);
84 FolderClass mh_class =
90 /* Folder functions */
96 /* FolderItem functions */
109 /* Message functions */
121 FolderClass *mh_get_class(void)
126 Folder *mh_folder_new(const gchar *name, const gchar *path)
130 folder = (Folder *)g_new0(MHFolder, 1);
131 folder->klass = &mh_class;
132 mh_folder_init(folder, name, path);
137 static void mh_folder_destroy(Folder *folder)
139 folder_local_folder_destroy(LOCAL_FOLDER(folder));
142 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
144 folder_local_folder_init(folder, name, path);
148 void mh_get_last_num(Folder *folder, FolderItem *item)
157 g_return_if_fail(item != NULL);
159 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
161 path = folder_item_get_path(item);
162 g_return_if_fail(path != NULL);
163 if (change_dir(path) < 0) {
169 if ((dp = opendir(".")) == NULL) {
170 FILE_OP_ERROR(item->path, "opendir");
174 while ((d = readdir(dp)) != NULL) {
175 if ((num = to_number(d->d_name)) >= 0 &&
176 stat(d->d_name, &s) == 0 &&
177 S_ISREG(s.st_mode)) {
184 debug_print("Last number in dir %s = %d\n", item->path, max);
185 item->last_num = max;
188 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list)
195 gint num, nummsgs = 0;
197 g_return_val_if_fail(item != NULL, -1);
199 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
201 path = folder_item_get_path(item);
202 g_return_val_if_fail(path != NULL, -1);
203 if (change_dir(path) < 0) {
209 if ((dp = opendir(".")) == NULL) {
210 FILE_OP_ERROR(item->path, "opendir");
214 while ((d = readdir(dp)) != NULL) {
215 if ((num = to_number(d->d_name)) >= 0 &&
216 stat(d->d_name, &s) == 0 &&
217 S_ISREG(s.st_mode)) {
218 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
227 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
232 g_return_val_if_fail(item != NULL, NULL);
233 g_return_val_if_fail(num > 0, NULL);
235 path = folder_item_get_path(item);
236 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
238 if (!is_file_exist(file)) {
246 MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
251 g_return_val_if_fail(item != NULL, NULL);
252 g_return_val_if_fail(num > 0, NULL);
254 file = mh_fetch_msg(folder, item, num);
255 if (!file) return NULL;
257 msginfo = mh_parse_msg(file, item);
259 msginfo->msgnum = num;
266 gchar *mh_get_new_msg_filename(FolderItem *dest)
271 destpath = folder_item_get_path(dest);
272 g_return_val_if_fail(destpath != NULL, NULL);
274 if (!is_dir_exist(destpath))
275 make_dir_hier(destpath);
278 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
280 if (is_file_entry_exist(destfile)) {
292 #define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
294 MsgInfo newmsginfo; \
296 newmsginfo.msgnum = dest->last_num; \
297 newmsginfo.flags = msginfo->flags; \
298 if (dest->stype == F_OUTBOX || \
299 dest->stype == F_QUEUE || \
300 dest->stype == F_DRAFT || \
301 dest->stype == F_TRASH) \
302 MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
303 MSG_NEW|MSG_UNREAD|MSG_DELETED); \
305 procmsg_write_flags(&newmsginfo, fp); \
308 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
309 gboolean remove_source)
313 g_return_val_if_fail(dest != NULL, -1);
314 g_return_val_if_fail(file != NULL, -1);
316 if (dest->last_num < 0) {
317 mh_get_last_num(folder, dest);
318 if (dest->last_num < 0) return -1;
321 destfile = mh_get_new_msg_filename(dest);
322 g_return_val_if_fail(destfile != NULL, -1);
324 if (link(file, destfile) < 0) {
325 if (copy_file(file, destfile, TRUE) < 0) {
326 g_warning("can't copy message %s to %s\n",
334 if (unlink(file) < 0)
335 FILE_OP_ERROR(file, "unlink");
340 return dest->last_num;
343 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
348 PrefsFolderItem *prefs;
350 g_return_val_if_fail(dest != NULL, -1);
351 g_return_val_if_fail(msginfo != NULL, -1);
353 if (msginfo->folder == dest) {
354 g_warning("the src folder is identical to the dest.\n");
358 if (dest->last_num < 0) {
359 mh_get_last_num(folder, dest);
360 if (dest->last_num < 0) return -1;
365 srcfile = procmsg_get_message_file(msginfo);
366 destfile = mh_get_new_msg_filename(dest);
372 debug_print("Copying message %s%c%d to %s ...\n",
373 msginfo->folder->path, G_DIR_SEPARATOR,
374 msginfo->msgnum, dest->path);
377 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
378 && dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
379 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
384 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
385 FILE_OP_ERROR(srcfile, "copy");
392 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
393 if (chmod(destfile, prefs->folder_chmod) < 0)
394 FILE_OP_ERROR(destfile, "chmod");
397 filemode = prefs->folder_chmod;
398 if (filemode & S_IRGRP) filemode |= S_IWGRP;
399 if (filemode & S_IROTH) filemode |= S_IWOTH;
406 return dest->last_num;
409 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
413 g_return_val_if_fail(item != NULL, -1);
415 file = mh_fetch_msg(folder, item, num);
416 g_return_val_if_fail(file != NULL, -1);
418 if (unlink(file) < 0) {
419 FILE_OP_ERROR(file, "unlink");
428 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
433 g_return_val_if_fail(item != NULL, -1);
435 path = folder_item_get_path(item);
436 g_return_val_if_fail(path != NULL, -1);
437 val = remove_all_numbered_files(path);
443 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
447 if (stat(itos(msginfo->msgnum), &s) < 0 ||
448 msginfo->size != s.st_size ||
449 msginfo->mtime != s.st_mtime)
455 void mh_scan_tree(Folder *folder)
460 g_return_if_fail(folder != NULL);
462 item = folder_item_new(folder, folder->name, NULL);
463 item->folder = folder;
464 folder->node = g_node_new(item);
466 rootpath = folder_item_get_path(item);
467 if (change_dir(rootpath) < 0) {
473 mh_create_tree(folder);
474 mh_scan_tree_recursive(item);
477 #define MAKE_DIR_IF_NOT_EXIST(dir) \
479 if (!is_dir_exist(dir)) { \
480 if (is_file_exist(dir)) { \
481 g_warning("File `%s' already exists.\n" \
482 "Can't create folder.", dir); \
485 if (make_dir(dir) < 0) \
490 gint mh_create_tree(Folder *folder)
494 g_return_val_if_fail(folder != NULL, -1);
496 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
497 rootpath = LOCAL_FOLDER(folder)->rootpath;
498 MAKE_DIR_IF_NOT_EXIST(rootpath);
499 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
500 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
501 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
502 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
503 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
504 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
509 #undef MAKE_DIR_IF_NOT_EXIST
511 gchar *mh_item_get_path(Folder *folder, FolderItem *item)
513 gchar *folder_path, *path;
515 g_return_val_if_fail(folder != NULL, NULL);
516 g_return_val_if_fail(item != NULL, NULL);
518 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
519 g_return_val_if_fail(folder_path != NULL, NULL);
521 if (folder_path[0] == G_DIR_SEPARATOR) {
523 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
526 path = g_strdup(folder_path);
529 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
530 folder_path, G_DIR_SEPARATOR_S,
533 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
541 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
546 FolderItem *new_item;
547 gchar *mh_sequences_filename;
548 FILE *mh_sequences_file;
550 g_return_val_if_fail(folder != NULL, NULL);
551 g_return_val_if_fail(parent != NULL, NULL);
552 g_return_val_if_fail(name != NULL, NULL);
554 path = folder_item_get_path(parent);
555 if (!is_dir_exist(path))
556 if (make_dir_hier(path) != 0)
559 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
562 if (make_dir(fullpath) < 0) {
570 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
573 path = g_strdup(name);
574 new_item = folder_item_new(folder, name, path);
575 folder_item_append(parent, new_item);
579 path = folder_item_get_path(new_item);
580 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
581 ".mh_sequences", NULL);
582 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
583 fclose(mh_sequences_file);
585 g_free(mh_sequences_filename);
591 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
599 g_return_val_if_fail(folder != NULL, -1);
600 g_return_val_if_fail(item != NULL, -1);
601 g_return_val_if_fail(item->path != NULL, -1);
602 g_return_val_if_fail(name != NULL, -1);
604 oldpath = folder_item_get_path(item);
605 if (!is_dir_exist(oldpath))
606 make_dir_hier(oldpath);
608 dirname = g_dirname(oldpath);
609 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
612 if (rename(oldpath, newpath) < 0) {
613 FILE_OP_ERROR(oldpath, "rename");
622 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
623 dirname = g_dirname(item->path);
624 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
627 newpath = g_strdup(name);
630 item->name = g_strdup(name);
632 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
634 paths[0] = g_strdup(item->path);
636 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
637 mh_rename_folder_func, paths);
644 gint mh_remove_folder(Folder *folder, FolderItem *item)
648 g_return_val_if_fail(folder != NULL, -1);
649 g_return_val_if_fail(item != NULL, -1);
650 g_return_val_if_fail(item->path != NULL, -1);
652 path = folder_item_get_path(item);
653 if (remove_dir_recursive(path) < 0) {
654 g_warning("can't remove directory `%s'\n", path);
660 folder_item_remove(item);
664 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
670 flags.perm_flags = MSG_NEW|MSG_UNREAD;
673 g_return_val_if_fail(item != NULL, NULL);
674 g_return_val_if_fail(file != NULL, NULL);
676 if (item->stype == F_QUEUE) {
677 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
678 } else if (item->stype == F_DRAFT) {
679 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
682 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
683 if (!msginfo) return NULL;
685 msginfo->msgnum = atoi(file);
686 msginfo->folder = item;
688 if (stat(file, &s) < 0) {
689 FILE_OP_ERROR(file, "stat");
693 msginfo->size = s.st_size;
694 msginfo->mtime = s.st_mtime;
701 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
706 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
707 result = is_dir_exist(entry);
714 * check whether PATH is a Maildir style mailbox.
715 * This is the case if the 3 subdir: new, cur, tmp are existing.
716 * This functon assumes that entry is an directory
718 static gboolean mh_is_maildir(const gchar *path)
720 return mh_is_maildir_one(path, "new") &&
721 mh_is_maildir_one(path, "cur") &&
722 mh_is_maildir_one(path, "tmp");
726 static void mh_scan_tree_recursive(FolderItem *item)
734 g_return_if_fail(item != NULL);
735 g_return_if_fail(item->folder != NULL);
737 dp = opendir(item->path ? item->path : ".");
739 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
743 debug_print("scanning %s ...\n",
744 item->path ? item->path
745 : LOCAL_FOLDER(item->folder)->rootpath);
746 if (item->folder->ui_func)
747 item->folder->ui_func(item->folder, item,
748 item->folder->ui_func_data);
750 while ((d = readdir(dp)) != NULL) {
751 if (d->d_name[0] == '.') continue;
754 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
757 entry = g_strdup(d->d_name);
759 if (stat(entry, &s) < 0) {
760 FILE_OP_ERROR(entry, "stat");
765 if (S_ISDIR(s.st_mode)) {
766 FolderItem *new_item;
769 if (mh_is_maildir(entry)) {
775 new_item = folder_item_new(item->folder, d->d_name, entry);
776 folder_item_append(item, new_item);
778 if (!strcmp(d->d_name, INBOX_DIR)) {
779 new_item->stype = F_INBOX;
780 item->folder->inbox = new_item;
781 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
782 new_item->stype = F_OUTBOX;
783 item->folder->outbox = new_item;
784 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
785 new_item->stype = F_DRAFT;
786 item->folder->draft = new_item;
787 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
788 new_item->stype = F_QUEUE;
789 item->folder->queue = new_item;
790 } else if (!strcmp(d->d_name, TRASH_DIR)) {
791 new_item->stype = F_TRASH;
792 item->folder->trash = new_item;
795 mh_scan_tree_recursive(new_item);
796 } else if (to_number(d->d_name) != -1) n_msg++;
805 gint new, unread, total, min, max;
807 procmsg_get_mark_sum(item->path, &new, &unread, &total,
810 new += n_msg - total;
811 unread += n_msg - total;
814 item->unread = unread;
820 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
822 FolderItem *item = node->data;
823 gchar **paths = data;
824 const gchar *oldpath = paths[0];
825 const gchar *newpath = paths[1];
830 oldpathlen = strlen(oldpath);
831 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
832 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
836 base = item->path + oldpathlen;
837 while (*base == G_DIR_SEPARATOR) base++;
839 new_itempath = g_strdup(newpath);
841 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
844 item->path = new_itempath;