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;
112 FolderClass *mh_get_class(void)
114 if (mh_class.idstr == NULL) {
115 mh_class.type = F_MH;
116 mh_class.idstr = "mh";
117 mh_class.uistr = "MH";
119 /* Folder functions */
120 mh_class.new_folder = mh_folder_new;
121 mh_class.destroy_folder = mh_folder_destroy;
122 mh_class.set_xml = folder_local_set_xml;
123 mh_class.get_xml = folder_local_get_xml;
124 mh_class.scan_tree = mh_scan_tree;
125 mh_class.create_tree = mh_create_tree;
127 /* FolderItem functions */
128 mh_class.item_get_path = mh_item_get_path;
129 mh_class.create_folder = mh_create_folder;
130 mh_class.rename_folder = mh_rename_folder;
131 mh_class.remove_folder = mh_remove_folder;
132 mh_class.get_num_list = mh_get_num_list;
134 /* Message functions */
135 mh_class.get_msginfo = mh_get_msginfo;
136 mh_class.fetch_msg = mh_fetch_msg;
137 mh_class.add_msg = mh_add_msg;
138 mh_class.add_msgs = mh_add_msgs;
139 mh_class.copy_msg = mh_copy_msg;
140 mh_class.remove_msg = mh_remove_msg;
141 mh_class.remove_all_msg = mh_remove_all_msg;
142 mh_class.is_msg_changed = mh_is_msg_changed;
148 static Folder *mh_folder_new(const gchar *name, const gchar *path)
152 folder = (Folder *)g_new0(MHFolder, 1);
153 folder->klass = &mh_class;
154 mh_folder_init(folder, name, path);
159 static void mh_folder_destroy(Folder *folder)
161 folder_local_folder_destroy(LOCAL_FOLDER(folder));
164 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
166 folder_local_folder_init(folder, name, path);
170 void mh_get_last_num(Folder *folder, FolderItem *item)
178 g_return_if_fail(item != NULL);
180 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
182 path = folder_item_get_path(item);
183 g_return_if_fail(path != NULL);
184 if (change_dir(path) < 0) {
190 if ((dp = opendir(".")) == NULL) {
191 FILE_OP_ERROR(item->path, "opendir");
195 while ((d = readdir(dp)) != NULL) {
196 if ((num = to_number(d->d_name)) >= 0 &&
197 dirent_is_regular_file(d)) {
204 debug_print("Last number in dir %s = %d\n", item->path, max);
205 item->last_num = max;
208 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
214 gint num, nummsgs = 0;
216 g_return_val_if_fail(item != NULL, -1);
218 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
220 *old_uids_valid = TRUE;
222 path = folder_item_get_path(item);
223 g_return_val_if_fail(path != NULL, -1);
224 if (change_dir(path) < 0) {
230 if ((dp = opendir(".")) == NULL) {
231 FILE_OP_ERROR(item->path, "opendir");
235 while ((d = readdir(dp)) != NULL) {
236 if ((num = to_number(d->d_name)) >= 0) {
237 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
246 static gchar *mh_fetch_msg(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 path = folder_item_get_path(item);
255 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
257 if (!is_file_exist(file)) {
265 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
270 g_return_val_if_fail(item != NULL, NULL);
271 g_return_val_if_fail(num > 0, NULL);
273 file = mh_fetch_msg(folder, item, num);
274 if (!file) return NULL;
276 msginfo = mh_parse_msg(file, item);
278 msginfo->msgnum = num;
285 static gchar *mh_get_new_msg_filename(FolderItem *dest)
290 destpath = folder_item_get_path(dest);
291 g_return_val_if_fail(destpath != NULL, NULL);
293 if (!is_dir_exist(destpath))
294 make_dir_hier(destpath);
297 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
299 if (is_file_entry_exist(destfile)) {
311 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
315 MsgFileInfo fileinfo;
317 g_return_val_if_fail(file != NULL, -1);
319 fileinfo.msginfo = NULL;
320 fileinfo.file = (gchar *)file;
321 fileinfo.flags = flags;
322 file_list.data = &fileinfo;
323 file_list.next = NULL;
325 ret = mh_add_msgs(folder, dest, &file_list, NULL);
329 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
334 MsgFileInfo *fileinfo;
336 g_return_val_if_fail(dest != NULL, -1);
337 g_return_val_if_fail(file_list != NULL, -1);
339 if (dest->last_num < 0) {
340 mh_get_last_num(folder, dest);
341 if (dest->last_num < 0) return -1;
344 for (cur = file_list; cur != NULL; cur = cur->next) {
345 fileinfo = (MsgFileInfo *)cur->data;
347 destfile = mh_get_new_msg_filename(dest);
348 if (destfile == NULL) return -1;
350 if (link(fileinfo->file, destfile) < 0) {
351 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
352 g_warning(_("can't copy message %s to %s\n"),
353 fileinfo->file, destfile);
358 if (relation != NULL)
359 g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
364 return dest->last_num;
367 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
372 FolderItemPrefs *prefs;
374 g_return_val_if_fail(dest != NULL, -1);
375 g_return_val_if_fail(msginfo != NULL, -1);
377 if (msginfo->folder == dest) {
378 g_warning("the src folder is identical to the dest.\n");
382 if (dest->last_num < 0) {
383 mh_get_last_num(folder, dest);
384 if (dest->last_num < 0) return -1;
389 srcfile = procmsg_get_message_file(msginfo);
390 destfile = mh_get_new_msg_filename(dest);
396 debug_print("Copying message %s%c%d to %s ...\n",
397 msginfo->folder->path, G_DIR_SEPARATOR,
398 msginfo->msgnum, dest->path);
401 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
402 && dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
403 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
408 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
409 FILE_OP_ERROR(srcfile, "copy");
416 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
417 if (chmod(destfile, prefs->folder_chmod) < 0)
418 FILE_OP_ERROR(destfile, "chmod");
421 filemode = prefs->folder_chmod;
422 if (filemode & S_IRGRP) filemode |= S_IWGRP;
423 if (filemode & S_IROTH) filemode |= S_IWOTH;
430 return dest->last_num;
433 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
437 g_return_val_if_fail(item != NULL, -1);
439 file = mh_fetch_msg(folder, item, num);
440 g_return_val_if_fail(file != NULL, -1);
442 if (unlink(file) < 0) {
443 FILE_OP_ERROR(file, "unlink");
452 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
457 g_return_val_if_fail(item != NULL, -1);
459 path = folder_item_get_path(item);
460 g_return_val_if_fail(path != NULL, -1);
461 val = remove_all_numbered_files(path);
467 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
472 if (stat(itos(msginfo->msgnum), &s) < 0 ||
473 msginfo->size != s.st_size ||
474 msginfo->mtime != s.st_mtime)
480 static gint mh_scan_tree(Folder *folder)
485 g_return_val_if_fail(folder != NULL, -1);
488 item = folder_item_new(folder, folder->name, NULL);
489 item->folder = folder;
490 folder->node = item->node = g_node_new(item);
492 item = FOLDER_ITEM(folder->node->data);
494 rootpath = folder_item_get_path(item);
495 if (change_dir(rootpath) < 0) {
501 mh_create_tree(folder);
502 mh_remove_missing_folder_items(folder);
503 mh_scan_tree_recursive(item);
508 #define MAKE_DIR_IF_NOT_EXIST(dir) \
510 if (!is_dir_exist(dir)) { \
511 if (is_file_exist(dir)) { \
512 g_warning("File `%s' already exists.\n" \
513 "Can't create folder.", dir); \
516 if (make_dir(dir) < 0) \
521 static gint mh_create_tree(Folder *folder)
525 g_return_val_if_fail(folder != NULL, -1);
527 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
528 rootpath = LOCAL_FOLDER(folder)->rootpath;
529 MAKE_DIR_IF_NOT_EXIST(rootpath);
530 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
531 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
532 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
533 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
534 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
535 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
540 #undef MAKE_DIR_IF_NOT_EXIST
542 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
544 gchar *folder_path, *path;
546 g_return_val_if_fail(folder != NULL, NULL);
547 g_return_val_if_fail(item != NULL, NULL);
549 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
550 g_return_val_if_fail(folder_path != NULL, NULL);
552 if (folder_path[0] == G_DIR_SEPARATOR) {
554 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
557 path = g_strdup(folder_path);
560 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
561 folder_path, G_DIR_SEPARATOR_S,
564 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
572 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
575 gchar *path, *real_name;
577 FolderItem *new_item;
578 gchar *mh_sequences_filename;
579 FILE *mh_sequences_file;
581 g_return_val_if_fail(folder != NULL, NULL);
582 g_return_val_if_fail(parent != NULL, NULL);
583 g_return_val_if_fail(name != NULL, NULL);
585 path = folder_item_get_path(parent);
586 if (!is_dir_exist(path))
587 if (make_dir_hier(path) != 0)
590 real_name = mh_filename_from_utf8(name);
591 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
595 if (make_dir(fullpath) < 0) {
603 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
606 path = g_strdup(name);
607 new_item = folder_item_new(folder, name, path);
608 folder_item_append(parent, new_item);
612 path = folder_item_get_path(new_item);
613 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
614 ".mh_sequences", NULL);
615 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
616 fclose(mh_sequences_file);
618 g_free(mh_sequences_filename);
624 static gint mh_rename_folder(Folder *folder, FolderItem *item,
630 gchar *newpath, *utf8newpath;
633 g_return_val_if_fail(folder != NULL, -1);
634 g_return_val_if_fail(item != NULL, -1);
635 g_return_val_if_fail(item->path != NULL, -1);
636 g_return_val_if_fail(name != NULL, -1);
638 oldpath = folder_item_get_path(item);
639 if (!is_dir_exist(oldpath))
640 make_dir_hier(oldpath);
642 dirname = g_path_get_dirname(oldpath);
643 real_name = mh_filename_from_utf8(name);
644 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_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_path_get_dirname(item->path);
659 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
663 utf8newpath = g_strdup(name);
666 item->name = g_strdup(name);
668 paths[0] = g_strdup(item->path);
669 paths[1] = utf8newpath;
670 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
671 mh_rename_folder_func, paths);
678 static gint mh_remove_folder(Folder *folder, FolderItem *item)
682 g_return_val_if_fail(folder != NULL, -1);
683 g_return_val_if_fail(item != NULL, -1);
684 g_return_val_if_fail(item->path != NULL, -1);
686 path = folder_item_get_path(item);
687 if (remove_dir_recursive(path) < 0) {
688 g_warning("can't remove directory `%s'\n", path);
694 folder_item_remove(item);
698 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
703 g_return_val_if_fail(item != NULL, NULL);
704 g_return_val_if_fail(file != NULL, NULL);
706 flags.perm_flags = MSG_NEW|MSG_UNREAD;
709 if (item->stype == F_QUEUE) {
710 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
711 } else if (item->stype == F_DRAFT) {
712 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
715 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
716 if (!msginfo) return NULL;
718 msginfo->msgnum = atoi(file);
719 msginfo->folder = item;
725 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
730 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
731 result = is_dir_exist(entry);
738 * check whether PATH is a Maildir style mailbox.
739 * This is the case if the 3 subdir: new, cur, tmp are existing.
740 * This functon assumes that entry is an directory
742 static gboolean mh_is_maildir(const gchar *path)
744 return mh_is_maildir_one(path, "new") &&
745 mh_is_maildir_one(path, "cur") &&
746 mh_is_maildir_one(path, "tmp");
750 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
755 g_return_val_if_fail(node->data != NULL, FALSE);
757 if (G_NODE_IS_ROOT(node))
760 item = FOLDER_ITEM(node->data);
762 path = folder_item_get_path(item);
763 if (!is_dir_exist(path)) {
764 debug_print("folder '%s' not found. removing...\n", path);
765 folder_item_remove(item);
772 static void mh_remove_missing_folder_items(Folder *folder)
774 g_return_if_fail(folder != NULL);
776 debug_print("searching missing folders...\n");
778 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
779 mh_remove_missing_folder_items_func, folder);
782 static void mh_scan_tree_recursive(FolderItem *item)
788 gchar *real_path, *entry, *utf8entry, *utf8name;
791 g_return_if_fail(item != NULL);
792 g_return_if_fail(item->folder != NULL);
794 folder = item->folder;
796 real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
797 dp = opendir(real_path);
799 FILE_OP_ERROR(real_path, "opendir");
804 debug_print("scanning %s ...\n",
805 item->path ? item->path
806 : LOCAL_FOLDER(item->folder)->rootpath);
808 folder->ui_func(folder, item, folder->ui_func_data);
810 while ((d = readdir(dp)) != NULL) {
811 if (d->d_name[0] == '.') continue;
813 utf8name = mh_filename_to_utf8(d->d_name);
815 utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
818 utf8entry = g_strdup(utf8name);
819 entry = mh_filename_from_utf8(utf8entry);
822 #ifdef HAVE_DIRENT_D_TYPE
823 d->d_type == DT_DIR ||
824 (d->d_type == DT_UNKNOWN &&
826 stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
827 #ifdef HAVE_DIRENT_D_TYPE
831 FolderItem *new_item = NULL;
835 if (mh_is_maildir(entry)) {
844 for (node = node->children; node != NULL; node = node->next) {
845 FolderItem *cur_item = FOLDER_ITEM(node->data);
846 if (!strcmp2(cur_item->path, entry)) {
852 debug_print("new folder '%s' found.\n", entry);
853 new_item = folder_item_new(folder, utf8name, utf8entry);
854 folder_item_append(item, new_item);
858 if (!folder->inbox &&
859 !strcmp(d->d_name, INBOX_DIR)) {
860 new_item->stype = F_INBOX;
861 folder->inbox = new_item;
862 } else if (!folder->outbox &&
863 !strcmp(d->d_name, OUTBOX_DIR)) {
864 new_item->stype = F_OUTBOX;
865 folder->outbox = new_item;
866 } else if (!folder->draft &&
867 !strcmp(d->d_name, DRAFT_DIR)) {
868 new_item->stype = F_DRAFT;
869 folder->draft = new_item;
870 } else if (!folder->queue &&
871 !strcmp(d->d_name, QUEUE_DIR)) {
872 new_item->stype = F_QUEUE;
873 folder->queue = new_item;
874 } else if (!folder->trash &&
875 !strcmp(d->d_name, TRASH_DIR)) {
876 new_item->stype = F_TRASH;
877 folder->trash = new_item;
881 mh_scan_tree_recursive(new_item);
882 } else if (to_number(d->d_name) != -1) n_msg++;
893 gint new, unread, total, min, max;
895 procmsg_get_mark_sum(item->path, &new, &unread, &total,
898 new += n_msg - total;
899 unread += n_msg - total;
902 item->unread = unread;
908 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
910 FolderItem *item = node->data;
911 gchar **paths = data;
912 const gchar *oldpath = paths[0];
913 const gchar *newpath = paths[1];
918 oldpathlen = strlen(oldpath);
919 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
920 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
924 base = item->path + oldpathlen;
925 while (*base == G_DIR_SEPARATOR) base++;
927 new_itempath = g_strdup(newpath);
929 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
932 item->path = new_itempath;
937 #warning FIXME_GTK2 /* should we use g_filename_from_utf8()? */
938 static gchar *mh_filename_from_utf8(const gchar *path)
940 const gchar *src_codeset = CS_UTF_8;
941 const gchar *dest_codeset = conv_get_current_charset_str();
944 real_path = conv_codeset_strdup(path, src_codeset, dest_codeset);
946 g_warning("mh_filename_from_utf8: faild to convert character set\n");
947 /* FIXME: show dialog? */
948 real_path = g_strdup(path);
954 #warning FIXME_GTK2 /* should we use g_filename_to_utf8()? */
955 static gchar *mh_filename_to_utf8(const gchar *path)
957 const gchar *src_codeset = conv_get_current_charset_str();
958 const gchar *dest_codeset = CS_UTF_8;
961 utf8path = conv_codeset_strdup(path, src_codeset, dest_codeset);
963 g_warning("mh_filename_to_utf8: faild to convert character set\n");
964 /* FIXME: show dialog? */
965 utf8path = g_strdup(path);