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 gint mh_add_msgs (Folder *folder,
60 gboolean remove_source,
62 static gint mh_copy_msg(Folder * folder,
63 FolderItem * dest, MsgInfo * msginfo);
64 static gint mh_remove_msg(Folder * folder, FolderItem * item, gint num);
65 static gint mh_remove_all_msg(Folder * folder, FolderItem * item);
66 static gboolean mh_is_msg_changed(Folder * folder,
67 FolderItem * item, MsgInfo * msginfo);
69 static gint mh_get_num_list(Folder * folder,
70 FolderItem * item, GSList ** list);
71 static void mh_scan_tree(Folder * folder);
73 static gint mh_create_tree(Folder * folder);
74 static FolderItem *mh_create_folder(Folder * folder,
77 static gint mh_rename_folder(Folder * folder,
78 FolderItem * item, const gchar * name);
79 static gint mh_remove_folder(Folder * folder, FolderItem * item);
81 static gchar *mh_get_new_msg_filename(FolderItem * dest);
83 static MsgInfo *mh_parse_msg(const gchar * file, FolderItem * item);
84 static void mh_scan_tree_recursive(FolderItem * item);
86 static gboolean mh_rename_folder_func(GNode * node, gpointer data);
87 static gchar *mh_item_get_path(Folder *folder, FolderItem *item);
89 FolderClass mh_class =
95 /* Folder functions */
101 /* FolderItem functions */
114 /* 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,
315 gboolean remove_source)
319 g_return_val_if_fail(file != NULL, -1);
321 file_list.data = (gpointer) file;
322 file_list.next = NULL;
324 return mh_add_msgs(folder, dest, &file_list, remove_source, NULL);
327 gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
328 gboolean remove_source, gint *first)
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;
348 if (first_ == 0 || first_ > dest->last_num + 1)
349 first_ = dest->last_num + 1;
351 if (link(file, destfile) < 0) {
352 if (copy_file(file, destfile, TRUE) < 0) {
353 g_warning(_("can't copy message %s to %s\n"),
368 for (cur = file_list; cur != NULL; cur = cur->next) {
369 file = (gchar *)cur->data;
370 if (unlink(file) < 0)
371 FILE_OP_ERROR(file, "unlink");
375 return dest->last_num;
378 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
383 FolderItemPrefs *prefs;
385 g_return_val_if_fail(dest != NULL, -1);
386 g_return_val_if_fail(msginfo != NULL, -1);
388 if (msginfo->folder == dest) {
389 g_warning("the src folder is identical to the dest.\n");
393 if (dest->last_num < 0) {
394 mh_get_last_num(folder, dest);
395 if (dest->last_num < 0) return -1;
400 srcfile = procmsg_get_message_file(msginfo);
401 destfile = mh_get_new_msg_filename(dest);
407 debug_print("Copying message %s%c%d to %s ...\n",
408 msginfo->folder->path, G_DIR_SEPARATOR,
409 msginfo->msgnum, dest->path);
412 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
413 && dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
414 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
419 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
420 FILE_OP_ERROR(srcfile, "copy");
427 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
428 if (chmod(destfile, prefs->folder_chmod) < 0)
429 FILE_OP_ERROR(destfile, "chmod");
432 filemode = prefs->folder_chmod;
433 if (filemode & S_IRGRP) filemode |= S_IWGRP;
434 if (filemode & S_IROTH) filemode |= S_IWOTH;
441 return dest->last_num;
444 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
448 g_return_val_if_fail(item != NULL, -1);
450 file = mh_fetch_msg(folder, item, num);
451 g_return_val_if_fail(file != NULL, -1);
453 if (unlink(file) < 0) {
454 FILE_OP_ERROR(file, "unlink");
463 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
468 g_return_val_if_fail(item != NULL, -1);
470 path = folder_item_get_path(item);
471 g_return_val_if_fail(path != NULL, -1);
472 val = remove_all_numbered_files(path);
478 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
482 if (stat(itos(msginfo->msgnum), &s) < 0 ||
483 msginfo->size != s.st_size ||
484 msginfo->mtime != s.st_mtime)
490 void mh_scan_tree(Folder *folder)
495 g_return_if_fail(folder != NULL);
497 item = folder_item_new(folder, folder->name, NULL);
498 item->folder = folder;
499 folder->node = g_node_new(item);
501 rootpath = folder_item_get_path(item);
502 if (change_dir(rootpath) < 0) {
508 mh_create_tree(folder);
509 mh_scan_tree_recursive(item);
512 #define MAKE_DIR_IF_NOT_EXIST(dir) \
514 if (!is_dir_exist(dir)) { \
515 if (is_file_exist(dir)) { \
516 g_warning("File `%s' already exists.\n" \
517 "Can't create folder.", dir); \
520 if (make_dir(dir) < 0) \
525 gint mh_create_tree(Folder *folder)
529 g_return_val_if_fail(folder != NULL, -1);
531 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
532 rootpath = LOCAL_FOLDER(folder)->rootpath;
533 MAKE_DIR_IF_NOT_EXIST(rootpath);
534 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
535 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
536 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
537 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
538 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
539 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
544 #undef MAKE_DIR_IF_NOT_EXIST
546 gchar *mh_item_get_path(Folder *folder, FolderItem *item)
548 gchar *folder_path, *path;
550 g_return_val_if_fail(folder != NULL, NULL);
551 g_return_val_if_fail(item != NULL, NULL);
553 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
554 g_return_val_if_fail(folder_path != NULL, NULL);
556 if (folder_path[0] == G_DIR_SEPARATOR) {
558 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
561 path = g_strdup(folder_path);
564 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
565 folder_path, G_DIR_SEPARATOR_S,
568 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
576 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
581 FolderItem *new_item;
582 gchar *mh_sequences_filename;
583 FILE *mh_sequences_file;
585 g_return_val_if_fail(folder != NULL, NULL);
586 g_return_val_if_fail(parent != NULL, NULL);
587 g_return_val_if_fail(name != NULL, NULL);
589 path = folder_item_get_path(parent);
590 if (!is_dir_exist(path))
591 if (make_dir_hier(path) != 0)
594 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
597 if (make_dir(fullpath) < 0) {
605 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
608 path = g_strdup(name);
609 new_item = folder_item_new(folder, name, path);
610 folder_item_append(parent, new_item);
614 path = folder_item_get_path(new_item);
615 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
616 ".mh_sequences", NULL);
617 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
618 fclose(mh_sequences_file);
620 g_free(mh_sequences_filename);
626 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
634 g_return_val_if_fail(folder != NULL, -1);
635 g_return_val_if_fail(item != NULL, -1);
636 g_return_val_if_fail(item->path != NULL, -1);
637 g_return_val_if_fail(name != NULL, -1);
639 oldpath = folder_item_get_path(item);
640 if (!is_dir_exist(oldpath))
641 make_dir_hier(oldpath);
643 dirname = g_dirname(oldpath);
644 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
647 if (rename(oldpath, newpath) < 0) {
648 FILE_OP_ERROR(oldpath, "rename");
657 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
658 dirname = g_dirname(item->path);
659 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
662 newpath = g_strdup(name);
665 item->name = g_strdup(name);
667 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
669 paths[0] = g_strdup(item->path);
671 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
672 mh_rename_folder_func, paths);
679 gint mh_remove_folder(Folder *folder, FolderItem *item)
683 g_return_val_if_fail(folder != NULL, -1);
684 g_return_val_if_fail(item != NULL, -1);
685 g_return_val_if_fail(item->path != NULL, -1);
687 path = folder_item_get_path(item);
688 if (remove_dir_recursive(path) < 0) {
689 g_warning("can't remove directory `%s'\n", path);
695 folder_item_remove(item);
699 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
705 flags.perm_flags = MSG_NEW|MSG_UNREAD;
708 g_return_val_if_fail(item != NULL, NULL);
709 g_return_val_if_fail(file != NULL, NULL);
711 if (item->stype == F_QUEUE) {
712 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
713 } else if (item->stype == F_DRAFT) {
714 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
717 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
718 if (!msginfo) return NULL;
720 msginfo->msgnum = atoi(file);
721 msginfo->folder = item;
723 if (stat(file, &s) < 0) {
724 FILE_OP_ERROR(file, "stat");
728 msginfo->size = s.st_size;
729 msginfo->mtime = s.st_mtime;
736 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
741 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
742 result = is_dir_exist(entry);
749 * check whether PATH is a Maildir style mailbox.
750 * This is the case if the 3 subdir: new, cur, tmp are existing.
751 * This functon assumes that entry is an directory
753 static gboolean mh_is_maildir(const gchar *path)
755 return mh_is_maildir_one(path, "new") &&
756 mh_is_maildir_one(path, "cur") &&
757 mh_is_maildir_one(path, "tmp");
761 static void mh_scan_tree_recursive(FolderItem *item)
769 g_return_if_fail(item != NULL);
770 g_return_if_fail(item->folder != NULL);
772 dp = opendir(item->path ? item->path : ".");
774 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
778 debug_print("scanning %s ...\n",
779 item->path ? item->path
780 : LOCAL_FOLDER(item->folder)->rootpath);
781 if (item->folder->ui_func)
782 item->folder->ui_func(item->folder, item,
783 item->folder->ui_func_data);
785 while ((d = readdir(dp)) != NULL) {
786 if (d->d_name[0] == '.') continue;
789 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
792 entry = g_strdup(d->d_name);
794 if (stat(entry, &s) < 0) {
795 FILE_OP_ERROR(entry, "stat");
800 if (S_ISDIR(s.st_mode)) {
801 FolderItem *new_item;
804 if (mh_is_maildir(entry)) {
810 new_item = folder_item_new(item->folder, d->d_name, entry);
811 folder_item_append(item, new_item);
813 if (!strcmp(d->d_name, INBOX_DIR)) {
814 new_item->stype = F_INBOX;
815 item->folder->inbox = new_item;
816 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
817 new_item->stype = F_OUTBOX;
818 item->folder->outbox = new_item;
819 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
820 new_item->stype = F_DRAFT;
821 item->folder->draft = new_item;
822 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
823 new_item->stype = F_QUEUE;
824 item->folder->queue = new_item;
825 } else if (!strcmp(d->d_name, TRASH_DIR)) {
826 new_item->stype = F_TRASH;
827 item->folder->trash = new_item;
830 mh_scan_tree_recursive(new_item);
831 } else if (to_number(d->d_name) != -1) n_msg++;
840 gint new, unread, total, min, max;
842 procmsg_get_mark_sum(item->path, &new, &unread, &total,
845 new += n_msg - total;
846 unread += n_msg - total;
849 item->unread = unread;
855 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
857 FolderItem *item = node->data;
858 gchar **paths = data;
859 const gchar *oldpath = paths[0];
860 const gchar *newpath = paths[1];
865 oldpathlen = strlen(oldpath);
866 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
867 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
871 base = item->path + oldpathlen;
872 while (*base == G_DIR_SEPARATOR) base++;
874 new_itempath = g_strdup(newpath);
876 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
879 item->path = new_itempath;