2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2005 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.
27 #include <glib/gi18n.h>
37 # 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 gboolean mh_scan_required (Folder *folder,
113 static FolderClass mh_class;
115 FolderClass *mh_get_class(void)
117 if (mh_class.idstr == NULL) {
118 mh_class.type = F_MH;
119 mh_class.idstr = "mh";
120 mh_class.uistr = "MH";
122 /* Folder functions */
123 mh_class.new_folder = mh_folder_new;
124 mh_class.destroy_folder = mh_folder_destroy;
125 mh_class.set_xml = folder_local_set_xml;
126 mh_class.get_xml = folder_local_get_xml;
127 mh_class.scan_tree = mh_scan_tree;
128 mh_class.create_tree = mh_create_tree;
130 /* FolderItem functions */
131 mh_class.item_get_path = mh_item_get_path;
132 mh_class.create_folder = mh_create_folder;
133 mh_class.rename_folder = mh_rename_folder;
134 mh_class.remove_folder = mh_remove_folder;
135 mh_class.get_num_list = mh_get_num_list;
136 mh_class.scan_required = mh_scan_required;
138 /* Message functions */
139 mh_class.get_msginfo = mh_get_msginfo;
140 mh_class.fetch_msg = mh_fetch_msg;
141 mh_class.add_msg = mh_add_msg;
142 mh_class.add_msgs = mh_add_msgs;
143 mh_class.copy_msg = mh_copy_msg;
144 mh_class.remove_msg = mh_remove_msg;
145 mh_class.remove_all_msg = mh_remove_all_msg;
146 mh_class.is_msg_changed = mh_is_msg_changed;
152 static Folder *mh_folder_new(const gchar *name, const gchar *path)
156 folder = (Folder *)g_new0(MHFolder, 1);
157 folder->klass = &mh_class;
158 mh_folder_init(folder, name, path);
163 static void mh_folder_destroy(Folder *folder)
165 folder_local_folder_destroy(LOCAL_FOLDER(folder));
168 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
170 folder_local_folder_init(folder, name, path);
174 gboolean mh_scan_required(Folder *folder, FolderItem *item)
179 path = folder_item_get_path(item);
180 g_return_val_if_fail(path != NULL, FALSE);
182 if (stat(path, &s) < 0) {
183 FILE_OP_ERROR(path, "stat");
188 if (s.st_mtime > item->mtime) {
189 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
197 debug_print("MH scan not required: %s (%ld <= %ld)\n",
205 void mh_get_last_num(Folder *folder, FolderItem *item)
213 g_return_if_fail(item != NULL);
215 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
217 path = folder_item_get_path(item);
218 g_return_if_fail(path != NULL);
219 if (change_dir(path) < 0) {
225 if ((dp = opendir(".")) == NULL) {
226 FILE_OP_ERROR(item->path, "opendir");
230 while ((d = readdir(dp)) != NULL) {
231 if ((num = to_number(d->d_name)) > 0 &&
232 dirent_is_regular_file(d)) {
239 debug_print("Last number in dir %s = %d\n", item->path, max);
240 item->last_num = max;
243 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
249 gint num, nummsgs = 0;
251 g_return_val_if_fail(item != NULL, -1);
253 debug_print("mh_get_num_list(): Scanning %s ...\n", item->path);
255 *old_uids_valid = TRUE;
257 path = folder_item_get_path(item);
258 g_return_val_if_fail(path != NULL, -1);
259 if (change_dir(path) < 0) {
265 if ((dp = opendir(".")) == NULL) {
266 FILE_OP_ERROR(item->path, "opendir");
270 while ((d = readdir(dp)) != NULL) {
271 if ((num = to_number(d->d_name)) > 0) {
272 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
278 item->mtime = time(NULL);
282 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
287 g_return_val_if_fail(item != NULL, NULL);
288 g_return_val_if_fail(num > 0, NULL);
290 path = folder_item_get_path(item);
291 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
293 if (!is_file_exist(file)) {
301 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
306 g_return_val_if_fail(item != NULL, NULL);
307 g_return_val_if_fail(num > 0, NULL);
309 file = mh_fetch_msg(folder, item, num);
310 if (!file) return NULL;
312 msginfo = mh_parse_msg(file, item);
314 msginfo->msgnum = num;
321 static gchar *mh_get_new_msg_filename(FolderItem *dest)
326 destpath = folder_item_get_path(dest);
327 g_return_val_if_fail(destpath != NULL, NULL);
329 if (!is_dir_exist(destpath))
330 make_dir_hier(destpath);
333 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
335 if (is_file_entry_exist(destfile)) {
347 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
351 MsgFileInfo fileinfo;
353 g_return_val_if_fail(file != NULL, -1);
355 fileinfo.msginfo = NULL;
356 fileinfo.file = (gchar *)file;
357 fileinfo.flags = flags;
358 file_list.data = &fileinfo;
359 file_list.next = NULL;
361 ret = mh_add_msgs(folder, dest, &file_list, NULL);
365 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
370 MsgFileInfo *fileinfo;
372 g_return_val_if_fail(dest != NULL, -1);
373 g_return_val_if_fail(file_list != NULL, -1);
375 if (dest->last_num < 0) {
376 mh_get_last_num(folder, dest);
377 if (dest->last_num < 0) return -1;
380 for (cur = file_list; cur != NULL; cur = cur->next) {
381 fileinfo = (MsgFileInfo *)cur->data;
383 destfile = mh_get_new_msg_filename(dest);
384 if (destfile == NULL) return -1;
386 if (link(fileinfo->file, destfile) < 0) {
387 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
388 g_warning(_("can't copy message %s to %s\n"),
389 fileinfo->file, destfile);
394 if (relation != NULL)
395 g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
400 return dest->last_num;
403 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
405 gboolean dest_need_scan = FALSE;
409 FolderItemPrefs *prefs;
411 g_return_val_if_fail(dest != NULL, -1);
412 g_return_val_if_fail(msginfo != NULL, -1);
414 if (msginfo->folder == dest) {
415 g_warning("the src folder is identical to the dest.\n");
419 if (dest->last_num < 0) {
420 mh_get_last_num(folder, dest);
421 if (dest->last_num < 0) return -1;
426 srcfile = procmsg_get_message_file(msginfo);
427 destfile = mh_get_new_msg_filename(dest);
433 debug_print("Copying message %s%c%d to %s ...\n",
434 msginfo->folder->path, G_DIR_SEPARATOR,
435 msginfo->msgnum, dest->path);
438 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
439 && dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
440 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
445 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
446 FILE_OP_ERROR(srcfile, "copy");
452 dest_need_scan = mh_scan_required(dest->folder, dest);
454 dest->mtime = time(NULL);
457 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
458 if (chmod(destfile, prefs->folder_chmod) < 0)
459 FILE_OP_ERROR(destfile, "chmod");
462 filemode = prefs->folder_chmod;
463 if (filemode & S_IRGRP) filemode |= S_IWGRP;
464 if (filemode & S_IROTH) filemode |= S_IWOTH;
471 return dest->last_num;
474 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
476 gboolean need_scan = FALSE;
479 g_return_val_if_fail(item != NULL, -1);
481 file = mh_fetch_msg(folder, item, num);
482 g_return_val_if_fail(file != NULL, -1);
484 need_scan = mh_scan_required(folder, item);
486 if (unlink(file) < 0) {
487 FILE_OP_ERROR(file, "unlink");
493 item->mtime = time(NULL);
499 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
504 g_return_val_if_fail(item != NULL, -1);
506 path = folder_item_get_path(item);
507 g_return_val_if_fail(path != NULL, -1);
508 val = remove_all_numbered_files(path);
514 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
519 if (stat(itos(msginfo->msgnum), &s) < 0 ||
520 msginfo->size != s.st_size ||
521 msginfo->mtime != s.st_mtime)
527 static gint mh_scan_tree(Folder *folder)
532 g_return_val_if_fail(folder != NULL, -1);
535 item = folder_item_new(folder, folder->name, NULL);
536 item->folder = folder;
537 folder->node = item->node = g_node_new(item);
539 item = FOLDER_ITEM(folder->node->data);
541 rootpath = folder_item_get_path(item);
542 if (change_dir(rootpath) < 0) {
548 mh_create_tree(folder);
549 mh_remove_missing_folder_items(folder);
550 mh_scan_tree_recursive(item);
555 #define MAKE_DIR_IF_NOT_EXIST(dir) \
557 if (!is_dir_exist(dir)) { \
558 if (is_file_exist(dir)) { \
559 g_warning("File `%s' already exists.\n" \
560 "Can't create folder.", dir); \
563 if (make_dir(dir) < 0) \
568 static gint mh_create_tree(Folder *folder)
572 g_return_val_if_fail(folder != NULL, -1);
574 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
575 rootpath = LOCAL_FOLDER(folder)->rootpath;
576 MAKE_DIR_IF_NOT_EXIST(rootpath);
577 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
578 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
579 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
580 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
581 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
582 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
587 #undef MAKE_DIR_IF_NOT_EXIST
589 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
591 gchar *folder_path, *path;
593 g_return_val_if_fail(folder != NULL, NULL);
594 g_return_val_if_fail(item != NULL, NULL);
596 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
597 g_return_val_if_fail(folder_path != NULL, NULL);
599 if (folder_path[0] == G_DIR_SEPARATOR) {
601 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
604 path = g_strdup(folder_path);
607 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
608 folder_path, G_DIR_SEPARATOR_S,
611 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
619 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
622 gchar *path, *real_name;
624 FolderItem *new_item;
625 gchar *mh_sequences_filename;
626 FILE *mh_sequences_file;
628 g_return_val_if_fail(folder != NULL, NULL);
629 g_return_val_if_fail(parent != NULL, NULL);
630 g_return_val_if_fail(name != NULL, NULL);
632 path = folder_item_get_path(parent);
633 if (!is_dir_exist(path))
634 if (make_dir_hier(path) != 0)
637 real_name = mh_filename_from_utf8(name);
638 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
642 if (make_dir(fullpath) < 0) {
650 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
653 path = g_strdup(name);
654 new_item = folder_item_new(folder, name, path);
655 folder_item_append(parent, new_item);
659 path = folder_item_get_path(new_item);
660 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
661 ".mh_sequences", NULL);
662 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
663 fclose(mh_sequences_file);
665 g_free(mh_sequences_filename);
671 static gint mh_rename_folder(Folder *folder, FolderItem *item,
677 gchar *newpath, *utf8newpath;
680 g_return_val_if_fail(folder != NULL, -1);
681 g_return_val_if_fail(item != NULL, -1);
682 g_return_val_if_fail(item->path != NULL, -1);
683 g_return_val_if_fail(name != NULL, -1);
685 oldpath = folder_item_get_path(item);
686 if (!is_dir_exist(oldpath))
687 make_dir_hier(oldpath);
689 dirname = g_path_get_dirname(oldpath);
690 real_name = mh_filename_from_utf8(name);
691 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
694 if (rename(oldpath, newpath) < 0) {
695 FILE_OP_ERROR(oldpath, "rename");
704 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
705 dirname = g_path_get_dirname(item->path);
706 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
710 utf8newpath = g_strdup(name);
713 item->name = g_strdup(name);
715 paths[0] = g_strdup(item->path);
716 paths[1] = utf8newpath;
717 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
718 mh_rename_folder_func, paths);
725 static gint mh_remove_folder(Folder *folder, FolderItem *item)
729 g_return_val_if_fail(folder != NULL, -1);
730 g_return_val_if_fail(item != NULL, -1);
731 g_return_val_if_fail(item->path != NULL, -1);
733 path = folder_item_get_path(item);
734 if (remove_dir_recursive(path) < 0) {
735 g_warning("can't remove directory `%s'\n", path);
741 folder_item_remove(item);
745 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
750 g_return_val_if_fail(item != NULL, NULL);
751 g_return_val_if_fail(file != NULL, NULL);
753 flags.perm_flags = MSG_NEW|MSG_UNREAD;
756 if (item->stype == F_QUEUE) {
757 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
758 } else if (item->stype == F_DRAFT) {
759 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
762 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
763 if (!msginfo) return NULL;
765 msginfo->msgnum = atoi(file);
766 msginfo->folder = item;
772 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
777 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
778 result = is_dir_exist(entry);
785 * check whether PATH is a Maildir style mailbox.
786 * This is the case if the 3 subdir: new, cur, tmp are existing.
787 * This functon assumes that entry is an directory
789 static gboolean mh_is_maildir(const gchar *path)
791 return mh_is_maildir_one(path, "new") &&
792 mh_is_maildir_one(path, "cur") &&
793 mh_is_maildir_one(path, "tmp");
797 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
802 g_return_val_if_fail(node->data != NULL, FALSE);
804 if (G_NODE_IS_ROOT(node))
807 item = FOLDER_ITEM(node->data);
809 path = folder_item_get_path(item);
810 if (!is_dir_exist(path)) {
811 debug_print("folder '%s' not found. removing...\n", path);
812 folder_item_remove(item);
819 static void mh_remove_missing_folder_items(Folder *folder)
821 g_return_if_fail(folder != NULL);
823 debug_print("searching missing folders...\n");
825 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
826 mh_remove_missing_folder_items_func, folder);
829 static void mh_scan_tree_recursive(FolderItem *item)
835 gchar *real_path, *entry, *utf8entry, *utf8name;
838 g_return_if_fail(item != NULL);
839 g_return_if_fail(item->folder != NULL);
841 folder = item->folder;
843 real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
844 dp = opendir(real_path);
846 FILE_OP_ERROR(real_path, "opendir");
851 debug_print("scanning %s ...\n",
852 item->path ? item->path
853 : LOCAL_FOLDER(item->folder)->rootpath);
855 folder->ui_func(folder, item, folder->ui_func_data);
857 while ((d = readdir(dp)) != NULL) {
858 if (d->d_name[0] == '.') continue;
860 utf8name = mh_filename_to_utf8(d->d_name);
862 utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
865 utf8entry = g_strdup(utf8name);
866 entry = mh_filename_from_utf8(utf8entry);
869 #ifdef HAVE_DIRENT_D_TYPE
870 d->d_type == DT_DIR ||
871 (d->d_type == DT_UNKNOWN &&
873 stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
874 #ifdef HAVE_DIRENT_D_TYPE
878 FolderItem *new_item = NULL;
882 if (mh_is_maildir(entry)) {
891 for (node = node->children; node != NULL; node = node->next) {
892 FolderItem *cur_item = FOLDER_ITEM(node->data);
893 if (!strcmp2(cur_item->path, entry)) {
899 debug_print("new folder '%s' found.\n", entry);
900 new_item = folder_item_new(folder, utf8name, utf8entry);
901 folder_item_append(item, new_item);
905 if (!folder->inbox &&
906 !strcmp(d->d_name, INBOX_DIR)) {
907 new_item->stype = F_INBOX;
908 folder->inbox = new_item;
909 } else if (!folder->outbox &&
910 !strcmp(d->d_name, OUTBOX_DIR)) {
911 new_item->stype = F_OUTBOX;
912 folder->outbox = new_item;
913 } else if (!folder->draft &&
914 !strcmp(d->d_name, DRAFT_DIR)) {
915 new_item->stype = F_DRAFT;
916 folder->draft = new_item;
917 } else if (!folder->queue &&
918 !strcmp(d->d_name, QUEUE_DIR)) {
919 new_item->stype = F_QUEUE;
920 folder->queue = new_item;
921 } else if (!folder->trash &&
922 !strcmp(d->d_name, TRASH_DIR)) {
923 new_item->stype = F_TRASH;
924 folder->trash = new_item;
928 mh_scan_tree_recursive(new_item);
929 } else if (to_number(d->d_name) != -1) n_msg++;
938 item->mtime = time(NULL);
942 gint new, unread, total, min, max;
944 procmsg_get_mark_sum(item->path, &new, &unread, &total,
947 new += n_msg - total;
948 unread += n_msg - total;
951 item->unread = unread;
957 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
959 FolderItem *item = node->data;
960 gchar **paths = data;
961 const gchar *oldpath = paths[0];
962 const gchar *newpath = paths[1];
967 oldpathlen = strlen(oldpath);
968 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
969 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
973 base = item->path + oldpathlen;
974 while (*base == G_DIR_SEPARATOR) base++;
976 new_itempath = g_strdup(newpath);
978 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
981 item->path = new_itempath;
986 #warning FIXME_GTK2 /* should we use g_filename_from_utf8()? */
987 static gchar *mh_filename_from_utf8(const gchar *path)
989 const gchar *src_codeset = CS_UTF_8;
990 const gchar *dest_codeset = conv_get_locale_charset_str();
993 real_path = conv_codeset_strdup(path, src_codeset, dest_codeset);
995 g_warning("mh_filename_from_utf8: faild to convert character set\n");
996 /* FIXME: show dialog? */
997 real_path = g_strdup(path);
1003 #warning FIXME_GTK2 /* should we use g_filename_to_utf8()? */
1004 static gchar *mh_filename_to_utf8(const gchar *path)
1006 const gchar *src_codeset = conv_get_locale_charset_str();
1007 const gchar *dest_codeset = CS_UTF_8;
1010 utf8path = conv_codeset_strdup(path, src_codeset, dest_codeset);
1012 g_warning("mh_filename_to_utf8: faild to convert character set\n");
1013 /* FIXME: show dialog? */
1014 utf8path = g_strdup(path);