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"
47 static void mh_folder_init (Folder *folder,
51 static Folder *mh_folder_new (const gchar *name,
53 static void mh_folder_destroy (Folder *folder);
54 static gchar *mh_fetch_msg (Folder *folder,
57 static MsgInfo *mh_get_msginfo (Folder *folder,
60 static gint mh_add_msg (Folder *folder,
64 static gint mh_add_msgs (Folder *folder,
68 static gint mh_copy_msg (Folder *folder,
71 static gint mh_remove_msg (Folder *folder,
74 static gint mh_remove_all_msg (Folder *folder,
76 static gboolean mh_is_msg_changed (Folder *folder,
80 static gint mh_get_num_list (Folder *folder,
83 gboolean *old_uids_valid);
84 static gint mh_scan_tree (Folder *folder);
86 static gint mh_create_tree (Folder *folder);
87 static FolderItem *mh_create_folder (Folder *folder,
90 static gint mh_rename_folder (Folder *folder,
93 static gint mh_remove_folder (Folder *folder,
96 static gchar *mh_get_new_msg_filename (FolderItem *dest);
98 static MsgInfo *mh_parse_msg (const gchar *file,
100 static void mh_remove_missing_folder_items (Folder *folder);
101 static gchar *mh_filename_from_utf8 (const gchar *path);
102 static gchar *mh_filename_to_utf8 (const gchar *path);
103 static void mh_scan_tree_recursive (FolderItem *item);
105 static gboolean mh_rename_folder_func (GNode *node,
107 static gchar *mh_item_get_path (Folder *folder,
110 static FolderClass mh_class =
116 /* Folder functions */
119 folder_local_set_xml,
120 folder_local_get_xml,
124 /* FolderItem functions */
140 /* Message functions */
154 FolderClass *mh_get_class(void)
159 static Folder *mh_folder_new(const gchar *name, const gchar *path)
163 folder = (Folder *)g_new0(MHFolder, 1);
164 folder->klass = &mh_class;
165 mh_folder_init(folder, name, path);
170 static void mh_folder_destroy(Folder *folder)
172 folder_local_folder_destroy(LOCAL_FOLDER(folder));
175 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
177 folder_local_folder_init(folder, name, path);
181 void mh_get_last_num(Folder *folder, FolderItem *item)
189 g_return_if_fail(item != NULL);
191 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
193 path = folder_item_get_path(item);
194 g_return_if_fail(path != NULL);
195 if (change_dir(path) < 0) {
201 if ((dp = opendir(".")) == NULL) {
202 FILE_OP_ERROR(item->path, "opendir");
206 while ((d = readdir(dp)) != NULL) {
207 if ((num = to_number(d->d_name)) >= 0 &&
208 dirent_is_regular_file(d)) {
215 debug_print("Last number in dir %s = %d\n", item->path, max);
216 item->last_num = max;
219 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
225 gint num, nummsgs = 0;
227 g_return_val_if_fail(item != NULL, -1);
229 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
231 *old_uids_valid = TRUE;
233 path = folder_item_get_path(item);
234 g_return_val_if_fail(path != NULL, -1);
235 if (change_dir(path) < 0) {
241 if ((dp = opendir(".")) == NULL) {
242 FILE_OP_ERROR(item->path, "opendir");
246 while ((d = readdir(dp)) != NULL) {
247 if ((num = to_number(d->d_name)) >= 0) {
248 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
257 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
262 g_return_val_if_fail(item != NULL, NULL);
263 g_return_val_if_fail(num > 0, NULL);
265 path = folder_item_get_path(item);
266 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
268 if (!is_file_exist(file)) {
276 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
281 g_return_val_if_fail(item != NULL, NULL);
282 g_return_val_if_fail(num > 0, NULL);
284 file = mh_fetch_msg(folder, item, num);
285 if (!file) return NULL;
287 msginfo = mh_parse_msg(file, item);
289 msginfo->msgnum = num;
296 static gchar *mh_get_new_msg_filename(FolderItem *dest)
301 destpath = folder_item_get_path(dest);
302 g_return_val_if_fail(destpath != NULL, NULL);
304 if (!is_dir_exist(destpath))
305 make_dir_hier(destpath);
308 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
310 if (is_file_entry_exist(destfile)) {
322 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
326 MsgFileInfo fileinfo;
328 g_return_val_if_fail(file != NULL, -1);
330 fileinfo.msginfo = NULL;
331 fileinfo.file = (gchar *)file;
332 fileinfo.flags = flags;
333 file_list.data = &fileinfo;
334 file_list.next = NULL;
336 ret = mh_add_msgs(folder, dest, &file_list, NULL);
340 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
345 MsgFileInfo *fileinfo;
347 g_return_val_if_fail(dest != NULL, -1);
348 g_return_val_if_fail(file_list != NULL, -1);
350 if (dest->last_num < 0) {
351 mh_get_last_num(folder, dest);
352 if (dest->last_num < 0) return -1;
355 for (cur = file_list; cur != NULL; cur = cur->next) {
356 fileinfo = (MsgFileInfo *)cur->data;
358 destfile = mh_get_new_msg_filename(dest);
359 if (destfile == NULL) return -1;
361 if (link(fileinfo->file, destfile) < 0) {
362 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
363 g_warning(_("can't copy message %s to %s\n"),
364 fileinfo->file, destfile);
369 if (relation != NULL)
370 g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
375 return dest->last_num;
378 static 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 static 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 static 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 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
483 if (stat(itos(msginfo->msgnum), &s) < 0 ||
484 msginfo->size != s.st_size ||
485 msginfo->mtime != s.st_mtime)
491 static gint mh_scan_tree(Folder *folder)
496 g_return_val_if_fail(folder != NULL, -1);
499 item = folder_item_new(folder, folder->name, NULL);
500 item->folder = folder;
501 folder->node = item->node = g_node_new(item);
503 item = FOLDER_ITEM(folder->node->data);
505 rootpath = folder_item_get_path(item);
506 if (change_dir(rootpath) < 0) {
512 mh_create_tree(folder);
513 mh_remove_missing_folder_items(folder);
514 mh_scan_tree_recursive(item);
519 #define MAKE_DIR_IF_NOT_EXIST(dir) \
521 if (!is_dir_exist(dir)) { \
522 if (is_file_exist(dir)) { \
523 g_warning("File `%s' already exists.\n" \
524 "Can't create folder.", dir); \
527 if (make_dir(dir) < 0) \
532 static gint mh_create_tree(Folder *folder)
536 g_return_val_if_fail(folder != NULL, -1);
538 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
539 rootpath = LOCAL_FOLDER(folder)->rootpath;
540 MAKE_DIR_IF_NOT_EXIST(rootpath);
541 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
542 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
543 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
544 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
545 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
546 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
551 #undef MAKE_DIR_IF_NOT_EXIST
553 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
555 gchar *folder_path, *path;
557 g_return_val_if_fail(folder != NULL, NULL);
558 g_return_val_if_fail(item != NULL, NULL);
560 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
561 g_return_val_if_fail(folder_path != NULL, NULL);
563 if (folder_path[0] == G_DIR_SEPARATOR) {
565 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
568 path = g_strdup(folder_path);
571 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
572 folder_path, G_DIR_SEPARATOR_S,
575 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
583 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
586 gchar *path, *real_name;
588 FolderItem *new_item;
589 gchar *mh_sequences_filename;
590 FILE *mh_sequences_file;
592 g_return_val_if_fail(folder != NULL, NULL);
593 g_return_val_if_fail(parent != NULL, NULL);
594 g_return_val_if_fail(name != NULL, NULL);
596 path = folder_item_get_path(parent);
597 if (!is_dir_exist(path))
598 if (make_dir_hier(path) != 0)
601 real_name = mh_filename_from_utf8(name);
602 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
606 if (make_dir(fullpath) < 0) {
614 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
617 path = g_strdup(name);
618 new_item = folder_item_new(folder, name, path);
619 folder_item_append(parent, new_item);
623 path = folder_item_get_path(new_item);
624 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
625 ".mh_sequences", NULL);
626 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
627 fclose(mh_sequences_file);
629 g_free(mh_sequences_filename);
635 static gint mh_rename_folder(Folder *folder, FolderItem *item,
641 gchar *newpath, *utf8newpath;
644 g_return_val_if_fail(folder != NULL, -1);
645 g_return_val_if_fail(item != NULL, -1);
646 g_return_val_if_fail(item->path != NULL, -1);
647 g_return_val_if_fail(name != NULL, -1);
649 oldpath = folder_item_get_path(item);
650 if (!is_dir_exist(oldpath))
651 make_dir_hier(oldpath);
653 dirname = g_dirname(oldpath);
654 real_name = mh_filename_from_utf8(name);
655 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
658 if (rename(oldpath, newpath) < 0) {
659 FILE_OP_ERROR(oldpath, "rename");
668 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
669 dirname = g_dirname(item->path);
670 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
674 utf8newpath = g_strdup(name);
677 item->name = g_strdup(name);
679 paths[0] = g_strdup(item->path);
680 paths[1] = utf8newpath;
681 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
682 mh_rename_folder_func, paths);
689 static gint mh_remove_folder(Folder *folder, FolderItem *item)
693 g_return_val_if_fail(folder != NULL, -1);
694 g_return_val_if_fail(item != NULL, -1);
695 g_return_val_if_fail(item->path != NULL, -1);
697 path = folder_item_get_path(item);
698 if (remove_dir_recursive(path) < 0) {
699 g_warning("can't remove directory `%s'\n", path);
705 folder_item_remove(item);
709 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
715 g_return_val_if_fail(item != NULL, NULL);
716 g_return_val_if_fail(file != NULL, NULL);
718 flags.perm_flags = MSG_NEW|MSG_UNREAD;
721 if (item->stype == F_QUEUE) {
722 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
723 } else if (item->stype == F_DRAFT) {
724 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
727 if (stat(file, &s) < 0) {
728 FILE_OP_ERROR(file, "stat");
731 if (!S_ISREG(s.st_mode))
734 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
735 if (!msginfo) return NULL;
737 msginfo->msgnum = atoi(file);
738 msginfo->folder = item;
739 msginfo->size = s.st_size;
740 msginfo->mtime = s.st_mtime;
746 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
751 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
752 result = is_dir_exist(entry);
759 * check whether PATH is a Maildir style mailbox.
760 * This is the case if the 3 subdir: new, cur, tmp are existing.
761 * This functon assumes that entry is an directory
763 static gboolean mh_is_maildir(const gchar *path)
765 return mh_is_maildir_one(path, "new") &&
766 mh_is_maildir_one(path, "cur") &&
767 mh_is_maildir_one(path, "tmp");
771 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
776 g_return_val_if_fail(node->data != NULL, FALSE);
778 if (G_NODE_IS_ROOT(node))
781 item = FOLDER_ITEM(node->data);
783 path = folder_item_get_path(item);
784 if (!is_dir_exist(path)) {
785 debug_print("folder '%s' not found. removing...\n", path);
786 folder_item_remove(item);
793 static void mh_remove_missing_folder_items(Folder *folder)
795 g_return_if_fail(folder != NULL);
797 debug_print("searching missing folders...\n");
799 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
800 mh_remove_missing_folder_items_func, folder);
803 static void mh_scan_tree_recursive(FolderItem *item)
809 gchar *real_path, *entry, *utf8entry, *utf8name;
812 g_return_if_fail(item != NULL);
813 g_return_if_fail(item->folder != NULL);
815 folder = item->folder;
817 real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
818 dp = opendir(real_path);
820 FILE_OP_ERROR(real_path, "opendir");
825 debug_print("scanning %s ...\n",
826 item->path ? item->path
827 : LOCAL_FOLDER(item->folder)->rootpath);
829 folder->ui_func(folder, item, folder->ui_func_data);
831 while ((d = readdir(dp)) != NULL) {
832 if (d->d_name[0] == '.') continue;
834 utf8name = mh_filename_to_utf8(d->d_name);
836 utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
839 utf8entry = g_strdup(utf8name);
840 entry = mh_filename_from_utf8(utf8entry);
843 #ifdef HAVE_DIRENT_D_TYPE
844 d->d_type == DT_DIR ||
845 (d->d_type == DT_UNKNOWN &&
847 stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
848 #ifdef HAVE_DIRENT_D_TYPE
852 FolderItem *new_item = NULL;
856 if (mh_is_maildir(entry)) {
865 for (node = node->children; node != NULL; node = node->next) {
866 FolderItem *cur_item = FOLDER_ITEM(node->data);
867 if (!strcmp2(cur_item->path, entry)) {
873 debug_print("new folder '%s' found.\n", entry);
874 new_item = folder_item_new(folder, utf8name, utf8entry);
875 folder_item_append(item, new_item);
879 if (!folder->inbox &&
880 !strcmp(d->d_name, INBOX_DIR)) {
881 new_item->stype = F_INBOX;
882 folder->inbox = new_item;
883 } else if (!folder->outbox &&
884 !strcmp(d->d_name, OUTBOX_DIR)) {
885 new_item->stype = F_OUTBOX;
886 folder->outbox = new_item;
887 } else if (!folder->draft &&
888 !strcmp(d->d_name, DRAFT_DIR)) {
889 new_item->stype = F_DRAFT;
890 folder->draft = new_item;
891 } else if (!folder->queue &&
892 !strcmp(d->d_name, QUEUE_DIR)) {
893 new_item->stype = F_QUEUE;
894 folder->queue = new_item;
895 } else if (!folder->trash &&
896 !strcmp(d->d_name, TRASH_DIR)) {
897 new_item->stype = F_TRASH;
898 folder->trash = new_item;
902 mh_scan_tree_recursive(new_item);
903 } else if (to_number(d->d_name) != -1) n_msg++;
914 gint new, unread, total, min, max;
916 procmsg_get_mark_sum(item->path, &new, &unread, &total,
919 new += n_msg - total;
920 unread += n_msg - total;
923 item->unread = unread;
929 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
931 FolderItem *item = node->data;
932 gchar **paths = data;
933 const gchar *oldpath = paths[0];
934 const gchar *newpath = paths[1];
939 oldpathlen = strlen(oldpath);
940 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
941 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
945 base = item->path + oldpathlen;
946 while (*base == G_DIR_SEPARATOR) base++;
948 new_itempath = g_strdup(newpath);
950 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
953 item->path = new_itempath;
958 #warning FIXME_GTK2 /* should we use g_filename_from_utf8()? */
959 static gchar *mh_filename_from_utf8(const gchar *path)
961 const gchar *src_codeset = CS_UTF_8;
962 const gchar *dest_codeset = conv_get_current_charset_str();
965 real_path = conv_codeset_strdup(path, src_codeset, dest_codeset);
967 g_warning("mh_filename_from_utf8: faild to convert character set\n");
968 /* FIXME: show dialog? */
969 real_path = g_strdup(path);
975 #warning FIXME_GTK2 /* should we use g_filename_to_utf8()? */
976 static gchar *mh_filename_to_utf8(const gchar *path)
978 const gchar *src_codeset = conv_get_current_charset_str();
979 const gchar *dest_codeset = CS_UTF_8;
982 utf8path = conv_codeset_strdup(path, src_codeset, dest_codeset);
984 g_warning("mh_filename_to_utf8: faild to convert character set\n");
985 /* FIXME: show dialog? */
986 utf8path = g_strdup(path);