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,
57 static gint mh_add_msgs(Folder * folder,
58 FolderItem * dest, GSList * file_list, MsgNumberList **newnum_list);
59 static gint mh_copy_msg(Folder * folder,
60 FolderItem * dest, MsgInfo * msginfo);
61 static gint mh_remove_msg(Folder * folder, FolderItem * item, gint num);
62 static gint mh_remove_all_msg(Folder * folder, FolderItem * item);
63 static gboolean mh_is_msg_changed(Folder * folder,
64 FolderItem * item, MsgInfo * msginfo);
66 static gint mh_get_num_list(Folder * folder,
67 FolderItem * item, GSList ** list);
68 static void mh_scan_tree(Folder * folder);
70 static gint mh_create_tree(Folder * folder);
71 static FolderItem *mh_create_folder(Folder * folder,
74 static gint mh_rename_folder(Folder * folder,
75 FolderItem * item, const gchar * name);
76 static gint mh_remove_folder(Folder * folder, FolderItem * item);
78 static gchar *mh_get_new_msg_filename(FolderItem * dest);
80 static MsgInfo *mh_parse_msg(const gchar * file, FolderItem * item);
81 static void mh_scan_tree_recursive(FolderItem * item);
83 static gboolean mh_rename_folder_func(GNode * node, gpointer data);
84 static gchar *mh_item_get_path(Folder *folder, FolderItem *item);
86 FolderClass mh_class =
92 /* Folder functions */
98 /* FolderItem functions */
111 /* Message functions */
125 FolderClass *mh_get_class(void)
130 Folder *mh_folder_new(const gchar *name, const gchar *path)
134 folder = (Folder *)g_new0(MHFolder, 1);
135 folder->klass = &mh_class;
136 mh_folder_init(folder, name, path);
141 static void mh_folder_destroy(Folder *folder)
143 folder_local_folder_destroy(LOCAL_FOLDER(folder));
146 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
148 folder_local_folder_init(folder, name, path);
152 void mh_get_last_num(Folder *folder, FolderItem *item)
161 g_return_if_fail(item != NULL);
163 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
165 path = folder_item_get_path(item);
166 g_return_if_fail(path != NULL);
167 if (change_dir(path) < 0) {
173 if ((dp = opendir(".")) == NULL) {
174 FILE_OP_ERROR(item->path, "opendir");
178 while ((d = readdir(dp)) != NULL) {
179 if ((num = to_number(d->d_name)) >= 0 &&
180 stat(d->d_name, &s) == 0 &&
181 S_ISREG(s.st_mode)) {
188 debug_print("Last number in dir %s = %d\n", item->path, max);
189 item->last_num = max;
192 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list)
199 gint num, nummsgs = 0;
201 g_return_val_if_fail(item != NULL, -1);
203 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
205 path = folder_item_get_path(item);
206 g_return_val_if_fail(path != NULL, -1);
207 if (change_dir(path) < 0) {
213 if ((dp = opendir(".")) == NULL) {
214 FILE_OP_ERROR(item->path, "opendir");
218 while ((d = readdir(dp)) != NULL) {
219 if ((num = to_number(d->d_name)) >= 0 &&
220 stat(d->d_name, &s) == 0 &&
221 S_ISREG(s.st_mode)) {
222 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
231 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
236 g_return_val_if_fail(item != NULL, NULL);
237 g_return_val_if_fail(num > 0, NULL);
239 path = folder_item_get_path(item);
240 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
242 if (!is_file_exist(file)) {
250 MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
255 g_return_val_if_fail(item != NULL, NULL);
256 g_return_val_if_fail(num > 0, NULL);
258 file = mh_fetch_msg(folder, item, num);
259 if (!file) return NULL;
261 msginfo = mh_parse_msg(file, item);
263 msginfo->msgnum = num;
270 gchar *mh_get_new_msg_filename(FolderItem *dest)
275 destpath = folder_item_get_path(dest);
276 g_return_val_if_fail(destpath != NULL, NULL);
278 if (!is_dir_exist(destpath))
279 make_dir_hier(destpath);
282 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
284 if (is_file_entry_exist(destfile)) {
296 #define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
298 MsgInfo newmsginfo; \
300 newmsginfo.msgnum = dest->last_num; \
301 newmsginfo.flags = msginfo->flags; \
302 if (dest->stype == F_OUTBOX || \
303 dest->stype == F_QUEUE || \
304 dest->stype == F_DRAFT || \
305 dest->stype == F_TRASH) \
306 MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
307 MSG_NEW|MSG_UNREAD|MSG_DELETED); \
309 procmsg_write_flags(&newmsginfo, fp); \
312 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file)
316 MsgNumberList *newnum_list = NULL;
318 g_return_val_if_fail(file != NULL, -1);
320 file_list.data = (gpointer) file;
321 file_list.next = NULL;
323 ret = mh_add_msgs(folder, dest, &file_list, NULL);
324 g_slist_free(newnum_list);
328 gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
329 MsgNumberList **newnum_list)
335 g_return_val_if_fail(dest != NULL, -1);
336 g_return_val_if_fail(file_list != NULL, -1);
338 if (dest->last_num < 0) {
339 mh_get_last_num(folder, dest);
340 if (dest->last_num < 0) return -1;
343 for (cur = file_list; cur != NULL; cur = cur->next) {
344 file = (gchar *)cur->data;
346 destfile = mh_get_new_msg_filename(dest);
347 if (destfile == NULL) return -1;
349 if (link(file, destfile) < 0) {
350 if (copy_file(file, destfile, TRUE) < 0) {
351 g_warning(_("can't copy message %s to %s\n"),
357 *newnum_list = g_slist_append(*newnum_list, GINT_TO_POINTER(dest->last_num + 1));
363 return dest->last_num;
366 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
371 FolderItemPrefs *prefs;
373 g_return_val_if_fail(dest != NULL, -1);
374 g_return_val_if_fail(msginfo != NULL, -1);
376 if (msginfo->folder == dest) {
377 g_warning("the src folder is identical to the dest.\n");
381 if (dest->last_num < 0) {
382 mh_get_last_num(folder, dest);
383 if (dest->last_num < 0) return -1;
388 srcfile = procmsg_get_message_file(msginfo);
389 destfile = mh_get_new_msg_filename(dest);
395 debug_print("Copying message %s%c%d to %s ...\n",
396 msginfo->folder->path, G_DIR_SEPARATOR,
397 msginfo->msgnum, dest->path);
400 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
401 && dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
402 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
407 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
408 FILE_OP_ERROR(srcfile, "copy");
415 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
416 if (chmod(destfile, prefs->folder_chmod) < 0)
417 FILE_OP_ERROR(destfile, "chmod");
420 filemode = prefs->folder_chmod;
421 if (filemode & S_IRGRP) filemode |= S_IWGRP;
422 if (filemode & S_IROTH) filemode |= S_IWOTH;
429 return dest->last_num;
432 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
436 g_return_val_if_fail(item != NULL, -1);
438 file = mh_fetch_msg(folder, item, num);
439 g_return_val_if_fail(file != NULL, -1);
441 if (unlink(file) < 0) {
442 FILE_OP_ERROR(file, "unlink");
451 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
456 g_return_val_if_fail(item != NULL, -1);
458 path = folder_item_get_path(item);
459 g_return_val_if_fail(path != NULL, -1);
460 val = remove_all_numbered_files(path);
466 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
470 if (stat(itos(msginfo->msgnum), &s) < 0 ||
471 msginfo->size != s.st_size ||
472 msginfo->mtime != s.st_mtime)
478 void mh_scan_tree(Folder *folder)
483 g_return_if_fail(folder != NULL);
485 item = folder_item_new(folder, folder->name, NULL);
486 item->folder = folder;
487 folder->node = g_node_new(item);
489 rootpath = folder_item_get_path(item);
490 if (change_dir(rootpath) < 0) {
496 mh_create_tree(folder);
497 mh_scan_tree_recursive(item);
500 #define MAKE_DIR_IF_NOT_EXIST(dir) \
502 if (!is_dir_exist(dir)) { \
503 if (is_file_exist(dir)) { \
504 g_warning("File `%s' already exists.\n" \
505 "Can't create folder.", dir); \
508 if (make_dir(dir) < 0) \
513 gint mh_create_tree(Folder *folder)
517 g_return_val_if_fail(folder != NULL, -1);
519 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
520 rootpath = LOCAL_FOLDER(folder)->rootpath;
521 MAKE_DIR_IF_NOT_EXIST(rootpath);
522 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
523 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
524 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
525 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
526 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
527 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
532 #undef MAKE_DIR_IF_NOT_EXIST
534 gchar *mh_item_get_path(Folder *folder, FolderItem *item)
536 gchar *folder_path, *path;
538 g_return_val_if_fail(folder != NULL, NULL);
539 g_return_val_if_fail(item != NULL, NULL);
541 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
542 g_return_val_if_fail(folder_path != NULL, NULL);
544 if (folder_path[0] == G_DIR_SEPARATOR) {
546 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
549 path = g_strdup(folder_path);
552 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
553 folder_path, G_DIR_SEPARATOR_S,
556 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
564 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
569 FolderItem *new_item;
570 gchar *mh_sequences_filename;
571 FILE *mh_sequences_file;
573 g_return_val_if_fail(folder != NULL, NULL);
574 g_return_val_if_fail(parent != NULL, NULL);
575 g_return_val_if_fail(name != NULL, NULL);
577 path = folder_item_get_path(parent);
578 if (!is_dir_exist(path))
579 if (make_dir_hier(path) != 0)
582 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
585 if (make_dir(fullpath) < 0) {
593 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
596 path = g_strdup(name);
597 new_item = folder_item_new(folder, name, path);
598 folder_item_append(parent, new_item);
602 path = folder_item_get_path(new_item);
603 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
604 ".mh_sequences", NULL);
605 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
606 fclose(mh_sequences_file);
608 g_free(mh_sequences_filename);
614 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
622 g_return_val_if_fail(folder != NULL, -1);
623 g_return_val_if_fail(item != NULL, -1);
624 g_return_val_if_fail(item->path != NULL, -1);
625 g_return_val_if_fail(name != NULL, -1);
627 oldpath = folder_item_get_path(item);
628 if (!is_dir_exist(oldpath))
629 make_dir_hier(oldpath);
631 dirname = g_dirname(oldpath);
632 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
635 if (rename(oldpath, newpath) < 0) {
636 FILE_OP_ERROR(oldpath, "rename");
645 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
646 dirname = g_dirname(item->path);
647 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
650 newpath = g_strdup(name);
653 item->name = g_strdup(name);
655 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
657 paths[0] = g_strdup(item->path);
659 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
660 mh_rename_folder_func, paths);
667 gint mh_remove_folder(Folder *folder, FolderItem *item)
671 g_return_val_if_fail(folder != NULL, -1);
672 g_return_val_if_fail(item != NULL, -1);
673 g_return_val_if_fail(item->path != NULL, -1);
675 path = folder_item_get_path(item);
676 if (remove_dir_recursive(path) < 0) {
677 g_warning("can't remove directory `%s'\n", path);
683 folder_item_remove(item);
687 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
693 flags.perm_flags = MSG_NEW|MSG_UNREAD;
696 g_return_val_if_fail(item != NULL, NULL);
697 g_return_val_if_fail(file != NULL, NULL);
699 if (item->stype == F_QUEUE) {
700 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
701 } else if (item->stype == F_DRAFT) {
702 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
705 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
706 if (!msginfo) return NULL;
708 msginfo->msgnum = atoi(file);
709 msginfo->folder = item;
711 if (stat(file, &s) < 0) {
712 FILE_OP_ERROR(file, "stat");
716 msginfo->size = s.st_size;
717 msginfo->mtime = s.st_mtime;
724 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
729 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
730 result = is_dir_exist(entry);
737 * check whether PATH is a Maildir style mailbox.
738 * This is the case if the 3 subdir: new, cur, tmp are existing.
739 * This functon assumes that entry is an directory
741 static gboolean mh_is_maildir(const gchar *path)
743 return mh_is_maildir_one(path, "new") &&
744 mh_is_maildir_one(path, "cur") &&
745 mh_is_maildir_one(path, "tmp");
749 static void mh_scan_tree_recursive(FolderItem *item)
757 g_return_if_fail(item != NULL);
758 g_return_if_fail(item->folder != NULL);
760 dp = opendir(item->path ? item->path : ".");
762 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
766 debug_print("scanning %s ...\n",
767 item->path ? item->path
768 : LOCAL_FOLDER(item->folder)->rootpath);
769 if (item->folder->ui_func)
770 item->folder->ui_func(item->folder, item,
771 item->folder->ui_func_data);
773 while ((d = readdir(dp)) != NULL) {
774 if (d->d_name[0] == '.') continue;
777 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
780 entry = g_strdup(d->d_name);
782 if (stat(entry, &s) < 0) {
783 FILE_OP_ERROR(entry, "stat");
788 if (S_ISDIR(s.st_mode)) {
789 FolderItem *new_item;
792 if (mh_is_maildir(entry)) {
798 new_item = folder_item_new(item->folder, d->d_name, entry);
799 folder_item_append(item, new_item);
801 if (!strcmp(d->d_name, INBOX_DIR)) {
802 new_item->stype = F_INBOX;
803 item->folder->inbox = new_item;
804 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
805 new_item->stype = F_OUTBOX;
806 item->folder->outbox = new_item;
807 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
808 new_item->stype = F_DRAFT;
809 item->folder->draft = new_item;
810 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
811 new_item->stype = F_QUEUE;
812 item->folder->queue = new_item;
813 } else if (!strcmp(d->d_name, TRASH_DIR)) {
814 new_item->stype = F_TRASH;
815 item->folder->trash = new_item;
818 mh_scan_tree_recursive(new_item);
819 } else if (to_number(d->d_name) != -1) n_msg++;
828 gint new, unread, total, min, max;
830 procmsg_get_mark_sum(item->path, &new, &unread, &total,
833 new += n_msg - total;
834 unread += n_msg - total;
837 item->unread = unread;
843 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
845 FolderItem *item = node->data;
846 gchar **paths = data;
847 const gchar *oldpath = paths[0];
848 const gchar *newpath = paths[1];
853 oldpathlen = strlen(oldpath);
854 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
855 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
859 base = item->path + oldpathlen;
860 while (*base == G_DIR_SEPARATOR) base++;
862 new_itempath = g_strdup(newpath);
864 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
867 item->path = new_itempath;