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,
50 static Folder *mh_folder_new (const gchar *name,
52 static void mh_folder_destroy (Folder *folder);
53 static gchar *mh_fetch_msg (Folder *folder,
56 static MsgInfo *mh_get_msginfo (Folder *folder,
59 static gint mh_add_msg (Folder *folder,
63 static gint mh_add_msgs (Folder *folder,
67 static gint mh_copy_msg (Folder *folder,
70 static gint mh_remove_msg (Folder *folder,
73 static gint mh_remove_all_msg (Folder *folder,
75 static gboolean mh_is_msg_changed (Folder *folder,
79 static gint mh_get_num_list (Folder *folder,
82 gboolean *old_uids_valid);
83 static gint mh_scan_tree (Folder *folder);
85 static gint mh_create_tree (Folder *folder);
86 static FolderItem *mh_create_folder (Folder *folder,
89 static gint mh_rename_folder (Folder *folder,
92 static gint mh_remove_folder (Folder *folder,
95 static gchar *mh_get_new_msg_filename (FolderItem *dest);
97 static MsgInfo *mh_parse_msg (const gchar *file,
99 static void mh_remove_missing_folder_items (Folder *folder);
100 static void mh_scan_tree_recursive (FolderItem *item);
102 static gboolean mh_rename_folder_func (GNode *node,
104 static gchar *mh_item_get_path (Folder *folder,
107 static FolderClass mh_class =
113 /* Folder functions */
116 folder_local_set_xml,
117 folder_local_get_xml,
121 /* FolderItem functions */
137 /* Message functions */
151 FolderClass *mh_get_class(void)
156 static Folder *mh_folder_new(const gchar *name, const gchar *path)
160 folder = (Folder *)g_new0(MHFolder, 1);
161 folder->klass = &mh_class;
162 mh_folder_init(folder, name, path);
167 static void mh_folder_destroy(Folder *folder)
169 folder_local_folder_destroy(LOCAL_FOLDER(folder));
172 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
174 folder_local_folder_init(folder, name, path);
178 void mh_get_last_num(Folder *folder, FolderItem *item)
186 g_return_if_fail(item != NULL);
188 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
190 path = folder_item_get_path(item);
191 g_return_if_fail(path != NULL);
192 if (change_dir(path) < 0) {
198 if ((dp = opendir(".")) == NULL) {
199 FILE_OP_ERROR(item->path, "opendir");
203 while ((d = readdir(dp)) != NULL) {
204 if ((num = to_number(d->d_name)) >= 0 &&
205 dirent_is_regular_file(d)) {
212 debug_print("Last number in dir %s = %d\n", item->path, max);
213 item->last_num = max;
216 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
222 gint num, nummsgs = 0;
224 g_return_val_if_fail(item != NULL, -1);
226 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
228 *old_uids_valid = TRUE;
230 path = folder_item_get_path(item);
231 g_return_val_if_fail(path != NULL, -1);
232 if (change_dir(path) < 0) {
238 if ((dp = opendir(".")) == NULL) {
239 FILE_OP_ERROR(item->path, "opendir");
243 while ((d = readdir(dp)) != NULL) {
244 if ((num = to_number(d->d_name)) >= 0) {
245 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
254 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
259 g_return_val_if_fail(item != NULL, NULL);
260 g_return_val_if_fail(num > 0, NULL);
262 path = folder_item_get_path(item);
263 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
265 if (!is_file_exist(file)) {
273 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
278 g_return_val_if_fail(item != NULL, NULL);
279 g_return_val_if_fail(num > 0, NULL);
281 file = mh_fetch_msg(folder, item, num);
282 if (!file) return NULL;
284 msginfo = mh_parse_msg(file, item);
286 msginfo->msgnum = num;
293 static gchar *mh_get_new_msg_filename(FolderItem *dest)
298 destpath = folder_item_get_path(dest);
299 g_return_val_if_fail(destpath != NULL, NULL);
301 if (!is_dir_exist(destpath))
302 make_dir_hier(destpath);
305 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
307 if (is_file_entry_exist(destfile)) {
319 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
323 MsgFileInfo fileinfo;
325 g_return_val_if_fail(file != NULL, -1);
327 fileinfo.msginfo = NULL;
328 fileinfo.file = (gchar *)file;
329 fileinfo.flags = flags;
330 file_list.data = &fileinfo;
331 file_list.next = NULL;
333 ret = mh_add_msgs(folder, dest, &file_list, NULL);
337 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
342 MsgFileInfo *fileinfo;
344 g_return_val_if_fail(dest != NULL, -1);
345 g_return_val_if_fail(file_list != NULL, -1);
347 if (dest->last_num < 0) {
348 mh_get_last_num(folder, dest);
349 if (dest->last_num < 0) return -1;
352 for (cur = file_list; cur != NULL; cur = cur->next) {
353 fileinfo = (MsgFileInfo *)cur->data;
355 destfile = mh_get_new_msg_filename(dest);
356 if (destfile == NULL) return -1;
358 if (link(fileinfo->file, destfile) < 0) {
359 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
360 g_warning(_("can't copy message %s to %s\n"),
361 fileinfo->file, destfile);
366 if (relation != NULL)
367 g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
372 return dest->last_num;
375 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
380 FolderItemPrefs *prefs;
382 g_return_val_if_fail(dest != NULL, -1);
383 g_return_val_if_fail(msginfo != NULL, -1);
385 if (msginfo->folder == dest) {
386 g_warning("the src folder is identical to the dest.\n");
390 if (dest->last_num < 0) {
391 mh_get_last_num(folder, dest);
392 if (dest->last_num < 0) return -1;
397 srcfile = procmsg_get_message_file(msginfo);
398 destfile = mh_get_new_msg_filename(dest);
404 debug_print("Copying message %s%c%d to %s ...\n",
405 msginfo->folder->path, G_DIR_SEPARATOR,
406 msginfo->msgnum, dest->path);
409 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
410 && dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
411 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
416 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
417 FILE_OP_ERROR(srcfile, "copy");
424 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
425 if (chmod(destfile, prefs->folder_chmod) < 0)
426 FILE_OP_ERROR(destfile, "chmod");
429 filemode = prefs->folder_chmod;
430 if (filemode & S_IRGRP) filemode |= S_IWGRP;
431 if (filemode & S_IROTH) filemode |= S_IWOTH;
438 return dest->last_num;
441 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
445 g_return_val_if_fail(item != NULL, -1);
447 file = mh_fetch_msg(folder, item, num);
448 g_return_val_if_fail(file != NULL, -1);
450 if (unlink(file) < 0) {
451 FILE_OP_ERROR(file, "unlink");
460 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
465 g_return_val_if_fail(item != NULL, -1);
467 path = folder_item_get_path(item);
468 g_return_val_if_fail(path != NULL, -1);
469 val = remove_all_numbered_files(path);
475 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
480 if (stat(itos(msginfo->msgnum), &s) < 0 ||
481 msginfo->size != s.st_size ||
482 msginfo->mtime != s.st_mtime)
488 static gint mh_scan_tree(Folder *folder)
493 g_return_val_if_fail(folder != NULL, -1);
496 item = folder_item_new(folder, folder->name, NULL);
497 item->folder = folder;
498 folder->node = item->node = g_node_new(item);
500 item = FOLDER_ITEM(folder->node->data);
502 rootpath = folder_item_get_path(item);
503 if (change_dir(rootpath) < 0) {
509 mh_create_tree(folder);
510 mh_remove_missing_folder_items(folder);
511 mh_scan_tree_recursive(item);
516 #define MAKE_DIR_IF_NOT_EXIST(dir) \
518 if (!is_dir_exist(dir)) { \
519 if (is_file_exist(dir)) { \
520 g_warning("File `%s' already exists.\n" \
521 "Can't create folder.", dir); \
524 if (make_dir(dir) < 0) \
529 static gint mh_create_tree(Folder *folder)
533 g_return_val_if_fail(folder != NULL, -1);
535 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
536 rootpath = LOCAL_FOLDER(folder)->rootpath;
537 MAKE_DIR_IF_NOT_EXIST(rootpath);
538 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
539 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
540 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
541 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
542 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
543 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
548 #undef MAKE_DIR_IF_NOT_EXIST
550 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
552 gchar *folder_path, *path;
554 g_return_val_if_fail(folder != NULL, NULL);
555 g_return_val_if_fail(item != NULL, NULL);
557 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
558 g_return_val_if_fail(folder_path != NULL, NULL);
560 if (folder_path[0] == G_DIR_SEPARATOR) {
562 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
565 path = g_strdup(folder_path);
568 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
569 folder_path, G_DIR_SEPARATOR_S,
572 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
580 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
585 FolderItem *new_item;
586 gchar *mh_sequences_filename;
587 FILE *mh_sequences_file;
589 g_return_val_if_fail(folder != NULL, NULL);
590 g_return_val_if_fail(parent != NULL, NULL);
591 g_return_val_if_fail(name != NULL, NULL);
593 path = folder_item_get_path(parent);
594 if (!is_dir_exist(path))
595 if (make_dir_hier(path) != 0)
598 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
601 if (make_dir(fullpath) < 0) {
609 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
612 path = g_strdup(name);
613 new_item = folder_item_new(folder, name, path);
614 folder_item_append(parent, new_item);
618 path = folder_item_get_path(new_item);
619 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
620 ".mh_sequences", NULL);
621 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
622 fclose(mh_sequences_file);
624 g_free(mh_sequences_filename);
630 static gint mh_rename_folder(Folder *folder, FolderItem *item,
638 g_return_val_if_fail(folder != NULL, -1);
639 g_return_val_if_fail(item != NULL, -1);
640 g_return_val_if_fail(item->path != NULL, -1);
641 g_return_val_if_fail(name != NULL, -1);
643 oldpath = folder_item_get_path(item);
644 if (!is_dir_exist(oldpath))
645 make_dir_hier(oldpath);
647 dirname = g_dirname(oldpath);
648 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
651 if (rename(oldpath, newpath) < 0) {
652 FILE_OP_ERROR(oldpath, "rename");
661 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
662 dirname = g_dirname(item->path);
663 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
666 newpath = g_strdup(name);
669 item->name = g_strdup(name);
671 paths[0] = g_strdup(item->path);
673 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
674 mh_rename_folder_func, paths);
681 static gint mh_remove_folder(Folder *folder, FolderItem *item)
685 g_return_val_if_fail(folder != NULL, -1);
686 g_return_val_if_fail(item != NULL, -1);
687 g_return_val_if_fail(item->path != NULL, -1);
689 path = folder_item_get_path(item);
690 if (remove_dir_recursive(path) < 0) {
691 g_warning("can't remove directory `%s'\n", path);
697 folder_item_remove(item);
701 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
707 g_return_val_if_fail(item != NULL, NULL);
708 g_return_val_if_fail(file != NULL, NULL);
710 flags.perm_flags = MSG_NEW|MSG_UNREAD;
713 if (item->stype == F_QUEUE) {
714 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
715 } else if (item->stype == F_DRAFT) {
716 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
719 if (stat(file, &s) < 0) {
720 FILE_OP_ERROR(file, "stat");
723 if (!S_ISREG(s.st_mode))
726 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
727 if (!msginfo) return NULL;
729 msginfo->msgnum = atoi(file);
730 msginfo->folder = item;
731 msginfo->size = s.st_size;
732 msginfo->mtime = s.st_mtime;
738 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
743 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
744 result = is_dir_exist(entry);
751 * check whether PATH is a Maildir style mailbox.
752 * This is the case if the 3 subdir: new, cur, tmp are existing.
753 * This functon assumes that entry is an directory
755 static gboolean mh_is_maildir(const gchar *path)
757 return mh_is_maildir_one(path, "new") &&
758 mh_is_maildir_one(path, "cur") &&
759 mh_is_maildir_one(path, "tmp");
763 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
768 g_return_val_if_fail(node->data != NULL, FALSE);
770 if (G_NODE_IS_ROOT(node))
773 item = FOLDER_ITEM(node->data);
775 path = folder_item_get_path(item);
776 if (!is_dir_exist(path)) {
777 debug_print("folder '%s' not found. removing...\n", path);
778 folder_item_remove(item);
785 static void mh_remove_missing_folder_items(Folder *folder)
787 g_return_if_fail(folder != NULL);
789 debug_print("searching missing folders...\n");
791 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
792 mh_remove_missing_folder_items_func, folder);
795 static void mh_scan_tree_recursive(FolderItem *item)
804 g_return_if_fail(item != NULL);
805 g_return_if_fail(item->folder != NULL);
807 folder = item->folder;
809 dp = opendir(item->path ? item->path : ".");
811 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
815 debug_print("scanning %s ...\n",
816 item->path ? item->path
817 : LOCAL_FOLDER(item->folder)->rootpath);
819 folder->ui_func(folder, item, folder->ui_func_data);
821 while ((d = readdir(dp)) != NULL) {
822 if (d->d_name[0] == '.') continue;
825 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
828 entry = g_strdup(d->d_name);
831 #ifdef HAVE_DIRENT_D_TYPE
832 d->d_type == DT_DIR ||
833 (d->d_type == DT_UNKNOWN &&
835 stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
836 #ifdef HAVE_DIRENT_D_TYPE
840 FolderItem *new_item = NULL;
844 if (mh_is_maildir(entry)) {
851 for (node = node->children; node != NULL; node = node->next) {
852 FolderItem *cur_item = FOLDER_ITEM(node->data);
853 if (!strcmp2(cur_item->path, entry)) {
859 debug_print("new folder '%s' found.\n", entry);
860 new_item = folder_item_new(folder, d->d_name, entry);
861 folder_item_append(item, new_item);
865 if (!folder->inbox &&
866 !strcmp(d->d_name, INBOX_DIR)) {
867 new_item->stype = F_INBOX;
868 folder->inbox = new_item;
869 } else if (!folder->outbox &&
870 !strcmp(d->d_name, OUTBOX_DIR)) {
871 new_item->stype = F_OUTBOX;
872 folder->outbox = new_item;
873 } else if (!folder->draft &&
874 !strcmp(d->d_name, DRAFT_DIR)) {
875 new_item->stype = F_DRAFT;
876 folder->draft = new_item;
877 } else if (!folder->queue &&
878 !strcmp(d->d_name, QUEUE_DIR)) {
879 new_item->stype = F_QUEUE;
880 folder->queue = new_item;
881 } else if (!folder->trash &&
882 !strcmp(d->d_name, TRASH_DIR)) {
883 new_item->stype = F_TRASH;
884 folder->trash = new_item;
888 mh_scan_tree_recursive(new_item);
889 } else if (to_number(d->d_name) != -1) n_msg++;
898 gint new, unread, total, min, max;
900 procmsg_get_mark_sum(item->path, &new, &unread, &total,
903 new += n_msg - total;
904 unread += n_msg - total;
907 item->unread = unread;
913 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
915 FolderItem *item = node->data;
916 gchar **paths = data;
917 const gchar *oldpath = paths[0];
918 const gchar *newpath = paths[1];
923 oldpathlen = strlen(oldpath);
924 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
925 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
929 base = item->path + oldpathlen;
930 while (*base == G_DIR_SEPARATOR) base++;
932 new_itempath = g_strdup(newpath);
934 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
937 item->path = new_itempath;