2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
37 # include <sys/time.h>
43 #include "procheader.h"
47 /* Define possible missing constants for Windows. */
60 static void mh_folder_init (Folder *folder,
64 static Folder *mh_folder_new (const gchar *name,
66 static void mh_folder_destroy (Folder *folder);
67 static gchar *mh_fetch_msg (Folder *folder,
70 static MsgInfo *mh_get_msginfo (Folder *folder,
73 static gint mh_add_msg (Folder *folder,
77 static gint mh_add_msgs (Folder *folder,
81 static gint mh_copy_msg (Folder *folder,
84 static gint mh_copy_msgs (Folder *folder,
88 static gint mh_remove_msg (Folder *folder,
91 static gint mh_remove_all_msg (Folder *folder,
93 static gboolean mh_is_msg_changed (Folder *folder,
97 static gint mh_get_num_list (Folder *folder,
100 gboolean *old_uids_valid);
101 static gint mh_scan_tree (Folder *folder);
103 static gint mh_create_tree (Folder *folder);
104 static FolderItem *mh_create_folder (Folder *folder,
107 static gint mh_rename_folder (Folder *folder,
110 static gint mh_remove_folder (Folder *folder,
113 static gchar *mh_get_new_msg_filename (FolderItem *dest);
115 static MsgInfo *mh_parse_msg (const gchar *file,
117 static void mh_remove_missing_folder_items (Folder *folder);
118 static gchar *mh_filename_from_utf8 (const gchar *path);
119 static gchar *mh_filename_to_utf8 (const gchar *path);
120 static void mh_scan_tree_recursive (FolderItem *item);
122 static gboolean mh_rename_folder_func (GNode *node,
124 static gchar *mh_item_get_path (Folder *folder,
127 static gboolean mh_scan_required (Folder *folder,
129 static int mh_item_close (Folder *folder,
131 static gint mh_get_flags (Folder *folder, FolderItem *item,
132 MsgInfoList *msginfo_list, GRelation *msgflags);
133 static void mh_write_sequences (FolderItem *item);
135 static FolderClass mh_class;
137 FolderClass *mh_get_class(void)
139 if (mh_class.idstr == NULL) {
140 mh_class.type = F_MH;
141 mh_class.idstr = "mh";
142 mh_class.uistr = "MH";
144 /* Folder functions */
145 mh_class.new_folder = mh_folder_new;
146 mh_class.destroy_folder = mh_folder_destroy;
147 mh_class.set_xml = folder_local_set_xml;
148 mh_class.get_xml = folder_local_get_xml;
149 mh_class.scan_tree = mh_scan_tree;
150 mh_class.create_tree = mh_create_tree;
152 /* FolderItem functions */
153 mh_class.item_get_path = mh_item_get_path;
154 mh_class.create_folder = mh_create_folder;
155 mh_class.rename_folder = mh_rename_folder;
156 mh_class.remove_folder = mh_remove_folder;
157 mh_class.get_num_list = mh_get_num_list;
158 mh_class.scan_required = mh_scan_required;
159 mh_class.close = mh_item_close;
160 mh_class.get_flags = mh_get_flags;
162 /* Message functions */
163 mh_class.get_msginfo = mh_get_msginfo;
164 mh_class.fetch_msg = mh_fetch_msg;
165 mh_class.add_msg = mh_add_msg;
166 mh_class.add_msgs = mh_add_msgs;
167 mh_class.copy_msg = mh_copy_msg;
168 mh_class.copy_msgs = mh_copy_msgs;
169 mh_class.remove_msg = mh_remove_msg;
170 mh_class.remove_all_msg = mh_remove_all_msg;
171 mh_class.is_msg_changed = mh_is_msg_changed;
177 static Folder *mh_folder_new(const gchar *name, const gchar *path)
181 folder = (Folder *)g_new0(MHFolder, 1);
182 folder->klass = &mh_class;
183 mh_folder_init(folder, name, path);
188 static void mh_folder_destroy(Folder *folder)
190 folder_local_folder_destroy(LOCAL_FOLDER(folder));
193 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
195 folder_local_folder_init(folder, name, path);
199 gboolean mh_scan_required(Folder *folder, FolderItem *item)
204 path = folder_item_get_path(item);
205 g_return_val_if_fail(path != NULL, FALSE);
207 if (stat(path, &s) < 0) {
208 FILE_OP_ERROR(path, "stat");
213 if ((s.st_mtime > item->mtime) &&
214 (s.st_mtime - 3600 != item->mtime)) {
215 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
217 (long int) s.st_mtime,
218 (long int) item->mtime);
223 debug_print("MH scan not required: %s (%ld <= %ld)\n",
225 (long int) s.st_mtime,
226 (long int) item->mtime);
231 void mh_get_last_num(Folder *folder, FolderItem *item)
239 g_return_if_fail(item != NULL);
241 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
243 path = folder_item_get_path(item);
244 g_return_if_fail(path != NULL);
245 if (change_dir(path) < 0) {
251 if ((dp = opendir(".")) == NULL) {
252 FILE_OP_ERROR(item->path, "opendir");
256 while ((d = readdir(dp)) != NULL) {
257 if ((num = to_number(d->d_name)) > 0 &&
258 dirent_is_regular_file(d)) {
265 debug_print("Last number in dir %s = %d\n", item->path, max);
266 item->last_num = max;
269 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
275 gint num, nummsgs = 0;
277 g_return_val_if_fail(item != NULL, -1);
279 debug_print("mh_get_num_list(): Scanning %s ...\n", item->path);
281 *old_uids_valid = TRUE;
283 path = folder_item_get_path(item);
284 g_return_val_if_fail(path != NULL, -1);
285 if (change_dir(path) < 0) {
291 if ((dp = opendir(".")) == NULL) {
292 FILE_OP_ERROR(item->path, "opendir");
296 while ((d = readdir(dp)) != NULL) {
297 if ((num = to_number(d->d_name)) > 0) {
298 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
304 item->mtime = time(NULL);
308 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
313 g_return_val_if_fail(item != NULL, NULL);
314 g_return_val_if_fail(num > 0, NULL);
316 path = folder_item_get_path(item);
317 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
319 if (!is_file_exist(file)) {
328 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
333 g_return_val_if_fail(item != NULL, NULL);
334 g_return_val_if_fail(num > 0, NULL);
336 file = mh_fetch_msg(folder, item, num);
337 if (!file) return NULL;
339 msginfo = mh_parse_msg(file, item);
341 msginfo->msgnum = num;
348 static gchar *mh_get_new_msg_filename(FolderItem *dest)
353 destpath = folder_item_get_path(dest);
354 g_return_val_if_fail(destpath != NULL, NULL);
356 if (!is_dir_exist(destpath))
357 make_dir_hier(destpath);
360 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
362 if (is_file_entry_exist(destfile)) {
374 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
378 MsgFileInfo fileinfo;
380 g_return_val_if_fail(file != NULL, -1);
382 fileinfo.msginfo = NULL;
383 fileinfo.file = (gchar *)file;
384 fileinfo.flags = flags;
385 file_list.data = &fileinfo;
386 file_list.next = NULL;
388 ret = mh_add_msgs(folder, dest, &file_list, NULL);
392 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
397 MsgFileInfo *fileinfo;
399 g_return_val_if_fail(dest != NULL, -1);
400 g_return_val_if_fail(file_list != NULL, -1);
402 if (dest->last_num < 0) {
403 mh_get_last_num(folder, dest);
404 if (dest->last_num < 0) return -1;
407 for (cur = file_list; cur != NULL; cur = cur->next) {
408 fileinfo = (MsgFileInfo *)cur->data;
410 destfile = mh_get_new_msg_filename(dest);
411 if (destfile == NULL) return -1;
414 if (link(fileinfo->file, destfile) < 0) {
416 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
417 g_warning(_("can't copy message %s to %s\n"),
418 fileinfo->file, destfile);
426 if (relation != NULL)
427 g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
431 mh_write_sequences(dest);
432 return dest->last_num;
435 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
439 g_return_val_if_fail(msginfo != NULL, -1);
441 msglist.data = msginfo;
444 return mh_copy_msgs(folder, dest, &msglist, NULL);
447 static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist,
450 gboolean dest_need_scan = FALSE;
454 FolderItemPrefs *prefs;
455 MsgInfo *msginfo = NULL;
456 gboolean remove_special_headers = FALSE;
457 MsgInfoList *cur = NULL;
458 g_return_val_if_fail(dest != NULL, -1);
459 g_return_val_if_fail(msglist != NULL, -1);
461 msginfo = (MsgInfo *)msglist->data;
463 g_return_val_if_fail(msginfo != NULL, -1);
465 if (msginfo->folder == dest) {
466 g_warning("the src folder is identical to the dest.\n");
470 if (dest->last_num < 0) {
471 mh_get_last_num(folder, dest);
472 if (dest->last_num < 0) return -1;
475 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
476 && !folder_has_parent_of_type(dest, F_QUEUE)
477 && !folder_has_parent_of_type(dest, F_DRAFT)) {
478 /* as every msginfo comes from the same folder, it is supposed they
479 * will either match the preceding condition either all or none.
481 remove_special_headers = TRUE;
482 } else if (!(MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
483 && (folder_has_parent_of_type(dest, F_QUEUE)
484 || folder_has_parent_of_type(dest, F_DRAFT))) {
490 for (cur = msglist; cur; cur = cur->next) {
491 msginfo = (MsgInfo *)cur->data;
494 srcfile = procmsg_get_message_file(msginfo);
495 destfile = mh_get_new_msg_filename(dest);
501 debug_print("Copying message %s%c%d to %s ...\n",
502 msginfo->folder->path, G_DIR_SEPARATOR,
503 msginfo->msgnum, dest->path);
506 if (remove_special_headers) {
507 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
512 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
513 FILE_OP_ERROR(srcfile, "copy");
518 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
519 if (chmod(destfile, prefs->folder_chmod) < 0)
520 FILE_OP_ERROR(destfile, "chmod");
523 filemode = prefs->folder_chmod;
524 if (filemode & S_IRGRP) filemode |= S_IWGRP;
525 if (filemode & S_IROTH) filemode |= S_IWOTH;
528 g_relation_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
534 dest_need_scan = mh_scan_required(dest->folder, dest);
536 dest->mtime = time(NULL);
538 mh_write_sequences(dest);
540 return dest->last_num;
543 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
545 gboolean need_scan = FALSE;
548 g_return_val_if_fail(item != NULL, -1);
550 file = mh_fetch_msg(folder, item, num);
551 g_return_val_if_fail(file != NULL, -1);
553 need_scan = mh_scan_required(folder, item);
555 if (g_unlink(file) < 0) {
556 FILE_OP_ERROR(file, "unlink");
562 item->mtime = time(NULL);
568 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
573 g_return_val_if_fail(item != NULL, -1);
575 path = folder_item_get_path(item);
576 g_return_val_if_fail(path != NULL, -1);
577 val = remove_all_numbered_files(path);
580 mh_write_sequences(item);
585 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
590 if (g_stat(itos(msginfo->msgnum), &s) < 0 ||
591 msginfo->size != s.st_size || (
592 (msginfo->mtime - s.st_mtime != 0) &&
593 (msginfo->mtime - s.st_mtime != 3600) &&
594 (msginfo->mtime - s.st_mtime != -3600)))
600 static gint mh_scan_tree(Folder *folder)
605 g_return_val_if_fail(folder != NULL, -1);
608 item = folder_item_new(folder, folder->name, NULL);
609 item->folder = folder;
610 folder->node = item->node = g_node_new(item);
612 item = FOLDER_ITEM(folder->node->data);
614 rootpath = folder_item_get_path(item);
615 if (change_dir(rootpath) < 0) {
621 mh_create_tree(folder);
622 mh_remove_missing_folder_items(folder);
623 mh_scan_tree_recursive(item);
628 #define MAKE_DIR_IF_NOT_EXIST(dir) \
630 if (!is_dir_exist(dir)) { \
631 if (is_file_exist(dir)) { \
632 g_warning("File `%s' already exists.\n" \
633 "Can't create folder.", dir); \
636 if (make_dir(dir) < 0) \
641 static gint mh_create_tree(Folder *folder)
645 g_return_val_if_fail(folder != NULL, -1);
647 CHDIR_RETURN_VAL_IF_FAIL(get_mail_base_dir(), -1);
648 rootpath = LOCAL_FOLDER(folder)->rootpath;
649 MAKE_DIR_IF_NOT_EXIST(rootpath);
650 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
651 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
652 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
653 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
654 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
655 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
660 #undef MAKE_DIR_IF_NOT_EXIST
662 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
664 gchar *folder_path, *path;
666 g_return_val_if_fail(folder != NULL, NULL);
667 g_return_val_if_fail(item != NULL, NULL);
669 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
670 g_return_val_if_fail(folder_path != NULL, NULL);
672 /* FIXME: [W32] The code below does not correctly merge
673 relative filenames; there should be a function to handle
675 if ( !is_relative_filename (folder_path) ) {
677 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
680 path = g_strdup(folder_path);
683 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
684 folder_path, G_DIR_SEPARATOR_S,
687 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
691 real_path = mh_filename_from_utf8(path);
692 if (!is_dir_exist(real_path) && is_dir_exist(path)) {
693 /* mmh, older version did put utf8 filenames instead of
694 * the correct encoding */
695 rename(path, real_path);
696 folder_item_scan(item);
703 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
706 gchar *path, *real_name;
708 FolderItem *new_item;
709 gchar *mh_sequences_filename;
710 FILE *mh_sequences_file;
712 g_return_val_if_fail(folder != NULL, NULL);
713 g_return_val_if_fail(parent != NULL, NULL);
714 g_return_val_if_fail(name != NULL, NULL);
716 path = folder_item_get_path(parent);
717 if (!is_dir_exist(path))
718 if (make_dir_hier(path) != 0)
721 real_name = mh_filename_from_utf8(name);
722 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
726 if (make_dir(fullpath) < 0) {
734 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
737 path = g_strdup(name);
738 new_item = folder_item_new(folder, name, path);
739 folder_item_append(parent, new_item);
743 path = folder_item_get_path(new_item);
744 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
745 ".mh_sequences", NULL);
746 if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
747 fclose(mh_sequences_file);
749 g_free(mh_sequences_filename);
755 static gint mh_rename_folder(Folder *folder, FolderItem *item,
761 gchar *newpath, *utf8newpath;
764 g_return_val_if_fail(folder != NULL, -1);
765 g_return_val_if_fail(item != NULL, -1);
766 g_return_val_if_fail(item->path != NULL, -1);
767 g_return_val_if_fail(name != NULL, -1);
769 oldpath = folder_item_get_path(item);
770 if (!is_dir_exist(oldpath))
771 make_dir_hier(oldpath);
773 dirname = g_path_get_dirname(oldpath);
774 real_name = mh_filename_from_utf8(name);
775 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
778 if (g_rename(oldpath, newpath) < 0) {
779 FILE_OP_ERROR(oldpath, "rename");
788 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
789 dirname = g_path_get_dirname(item->path);
790 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
794 utf8newpath = g_strdup(name);
797 item->name = g_strdup(name);
799 paths[0] = g_strdup(item->path);
800 paths[1] = utf8newpath;
801 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
802 mh_rename_folder_func, paths);
809 static gint mh_remove_folder(Folder *folder, FolderItem *item)
813 g_return_val_if_fail(folder != NULL, -1);
814 g_return_val_if_fail(item != NULL, -1);
815 g_return_val_if_fail(item->path != NULL, -1);
817 path = folder_item_get_path(item);
818 if (remove_dir_recursive(path) < 0) {
819 g_warning("can't remove directory `%s'\n", path);
825 folder_item_remove(item);
829 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
834 g_return_val_if_fail(item != NULL, NULL);
835 g_return_val_if_fail(file != NULL, NULL);
837 flags.perm_flags = MSG_NEW|MSG_UNREAD;
840 if (folder_has_parent_of_type(item, F_QUEUE)) {
841 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
842 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
843 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
846 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
847 if (!msginfo) return NULL;
849 msginfo->msgnum = atoi(file);
850 msginfo->folder = item;
855 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
860 g_return_val_if_fail(node->data != NULL, FALSE);
862 if (G_NODE_IS_ROOT(node))
865 item = FOLDER_ITEM(node->data);
867 path = folder_item_get_path(item);
868 if (!is_dir_exist(path)) {
869 debug_print("folder '%s' not found. removing...\n", path);
870 folder_item_remove(item);
877 static void mh_remove_missing_folder_items(Folder *folder)
879 g_return_if_fail(folder != NULL);
881 debug_print("searching missing folders...\n");
883 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
884 mh_remove_missing_folder_items_func, folder);
887 static void mh_scan_tree_recursive(FolderItem *item)
896 const gchar *dir_name;
898 gchar *real_path, *entry, *utf8entry, *utf8name;
901 g_return_if_fail(item != NULL);
902 g_return_if_fail(item->folder != NULL);
904 folder = item->folder;
906 real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
908 dir = g_dir_open(real_path, 0, NULL);
910 g_warning("failed to open directory: %s\n", real_path);
915 dp = opendir(real_path);
917 FILE_OP_ERROR(real_path, "opendir");
923 debug_print("scanning %s ...\n",
924 item->path ? item->path
925 : LOCAL_FOLDER(item->folder)->rootpath);
927 folder->ui_func(folder, item, folder->ui_func_data);
930 while ((dir_name = g_dir_read_name(dir)) != NULL) {
932 while ((d = readdir(dp)) != NULL) {
933 dir_name = d->d_name;
935 if (dir_name[0] == '.') continue;
937 utf8name = mh_filename_to_utf8(dir_name);
939 utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
942 utf8entry = g_strdup(utf8name);
943 entry = mh_filename_from_utf8(utf8entry);
946 #if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
947 d->d_type == DT_DIR ||
948 (d->d_type == DT_UNKNOWN &&
950 g_stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
951 #if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
955 FolderItem *new_item = NULL;
959 for (node = node->children; node != NULL; node = node->next) {
960 FolderItem *cur_item = FOLDER_ITEM(node->data);
961 if (!strcmp2(cur_item->path, entry)) {
967 debug_print("new folder '%s' found.\n", entry);
968 new_item = folder_item_new(folder, utf8name, utf8entry);
969 folder_item_append(item, new_item);
973 if (!folder->inbox &&
974 !strcmp(dir_name, INBOX_DIR)) {
975 new_item->stype = F_INBOX;
976 folder->inbox = new_item;
977 } else if (!folder->outbox &&
978 !strcmp(dir_name, OUTBOX_DIR)) {
979 new_item->stype = F_OUTBOX;
980 folder->outbox = new_item;
981 } else if (!folder->draft &&
982 !strcmp(dir_name, DRAFT_DIR)) {
983 new_item->stype = F_DRAFT;
984 folder->draft = new_item;
985 } else if (!folder->queue &&
986 !strcmp(dir_name, QUEUE_DIR)) {
987 new_item->stype = F_QUEUE;
988 folder->queue = new_item;
989 } else if (!folder->trash &&
990 !strcmp(dir_name, TRASH_DIR)) {
991 new_item->stype = F_TRASH;
992 folder->trash = new_item;
996 mh_scan_tree_recursive(new_item);
997 } else if (to_number(dir_name) > 0) n_msg++;
1010 item->mtime = time(NULL);
1013 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1015 FolderItem *item = node->data;
1016 gchar **paths = data;
1017 const gchar *oldpath = paths[0];
1018 const gchar *newpath = paths[1];
1020 gchar *new_itempath;
1023 oldpathlen = strlen(oldpath);
1024 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1025 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1029 base = item->path + oldpathlen;
1030 while (*base == G_DIR_SEPARATOR) base++;
1032 new_itempath = g_strdup(newpath);
1034 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1037 item->path = new_itempath;
1042 static gchar *mh_filename_from_utf8(const gchar *path)
1044 gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1047 g_warning("mh_filename_from_utf8: faild to convert character set\n");
1048 real_path = g_strdup(path);
1054 static gchar *mh_filename_to_utf8(const gchar *path)
1056 gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1058 g_warning("mh_filename_to_utf8: faild to convert character set\n");
1059 utf8path = g_strdup(path);
1065 static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1067 MsgInfo *msginfo_a = (MsgInfo *) a;
1068 MsgInfo *msginfo_b = (MsgInfo *) b;
1070 return (msginfo_a->msgnum - msginfo_b->msgnum);
1073 static gchar *get_unseen_seq_name(void)
1075 static gchar *seq_name = NULL;
1077 gchar buf[BUFFSIZE];
1079 gchar *profile_path = g_strconcat(
1080 g_get_home_dir(), G_DIR_SEPARATOR_S,
1081 ".mh_profile", NULL);
1082 FILE *fp = g_fopen(profile_path, "r");
1084 while (fgets(buf, sizeof(buf), fp) != NULL) {
1085 if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
1086 gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
1087 while (*seq_tmp == ' ')
1089 seq_name = g_strdup(seq_tmp);
1090 seq_name = strretchomp(seq_name);
1097 seq_name = g_strdup("unseen");
1098 tmp = g_strdup_printf("%s:", seq_name);
1105 static gint mh_get_flags(Folder *folder, FolderItem *item,
1106 MsgInfoList *msginfo_list, GRelation *msgflags)
1108 gchar *mh_sequences_filename;
1109 FILE *mh_sequences_file;
1110 gchar buf[BUFFSIZE];
1111 gchar *unseen_list = NULL;
1113 MsgInfoList *mcur = NULL;
1115 GTimer *timer = g_timer_new();
1116 g_timer_start(timer);
1120 path = folder_item_get_path(item);
1122 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
1123 ".mh_sequences", NULL);
1125 if ((mh_sequences_file = g_fopen(mh_sequences_filename, "r+b")) != NULL) {
1126 while (fgets(buf, sizeof(buf), mh_sequences_file) != NULL) {
1127 if (!strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name()))) {
1128 unseen_list = g_strdup(buf+strlen(get_unseen_seq_name()));
1132 fclose(mh_sequences_file);
1136 gchar *token = NULL, *next = NULL, *boundary = NULL;
1138 GHashTable *unseen_table = g_hash_table_new(g_direct_hash, g_direct_equal);
1140 cur = unseen_list = strretchomp(unseen_list);
1141 debug_print("found unseen list in .mh_sequences: %s\n", unseen_list);
1143 while (*cur && *cur == ' ')
1146 if ((next = strchr(cur, ' ')) != NULL) {
1155 if ((boundary = strchr(token, '-')) != NULL) {
1161 for (i = atoi(start); i <= atoi(end); i++) {
1162 g_hash_table_insert(unseen_table, GINT_TO_POINTER(i), GINT_TO_POINTER(1));
1164 } else if ((num = atoi(token)) > 0) {
1165 g_hash_table_insert(unseen_table, GINT_TO_POINTER(num), GINT_TO_POINTER(1));
1170 for (mcur = msginfo_list; mcur; mcur = mcur->next) {
1171 MsgInfo *msginfo = (MsgInfo *)mcur->data;
1172 MsgPermFlags flags = msginfo->flags.perm_flags;
1173 if (g_hash_table_lookup(unseen_table, GINT_TO_POINTER(msginfo->msgnum))) {
1174 flags |= MSG_UNREAD;
1175 } else if (!(flags & MSG_NEW)) { /* don't mark new msgs as read */
1176 flags &= ~(MSG_UNREAD);
1178 if (flags != msginfo->flags.perm_flags)
1179 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
1181 g_hash_table_destroy(unseen_table);
1182 g_free(unseen_list);
1185 g_timer_stop(timer);
1186 printf("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
1187 g_timer_destroy(timer);
1192 static void mh_write_sequences(FolderItem *item)
1194 gchar *mh_sequences_old, *mh_sequences_new;
1195 FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
1196 gchar buf[BUFFSIZE];
1199 GTimer *timer = g_timer_new();
1200 g_timer_start(timer);
1205 path = folder_item_get_path(item);
1207 mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
1208 ".mh_sequences", NULL);
1209 mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
1210 ".mh_sequences.new", NULL);
1211 if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
1212 GSList *msglist = folder_item_get_msg_list(item);
1214 MsgInfo *info = NULL;
1215 gint start = -1, end = -1;
1216 gchar *sequence = g_strdup("");
1217 msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
1220 /* write the unseen sequence */
1222 info = (MsgInfo *)(cur ? cur->data:NULL);
1223 if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
1225 start = end = info->msgnum;
1229 if (start > 0 && end > 0) {
1230 gchar *tmp = sequence;
1232 sequence = g_strdup_printf("%s %d-%d ", tmp, start, end);
1234 sequence = g_strdup_printf("%s %d ", tmp, start);
1239 cur = cur ? cur->next:NULL;
1240 } while (cur || (start > 0 && end > 0));
1241 if (sequence && strlen(sequence)) {
1242 fprintf(mh_sequences_new_fp, "%s%s\n",
1243 get_unseen_seq_name(), sequence);
1244 debug_print("wrote unseen sequence: '%s%s'\n",
1245 get_unseen_seq_name(), sequence);
1247 /* rewrite the rest of the file */
1248 if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
1249 while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
1250 if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
1251 fprintf(mh_sequences_new_fp, "%s", buf);
1253 fclose(mh_sequences_old_fp);
1256 fclose(mh_sequences_new_fp);
1257 g_rename(mh_sequences_new, mh_sequences_old);
1259 procmsg_msg_list_free(msglist);
1261 g_free(mh_sequences_old);
1262 g_free(mh_sequences_new);
1265 g_timer_stop(timer);
1266 printf("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
1267 g_timer_destroy(timer);
1271 static int mh_item_close(Folder *folder, FolderItem *item)
1273 mh_write_sequences(item);