2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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 GSList *mh_get_msg_list (Folder *folder,
53 gchar *mh_fetch_msg (Folder *folder,
56 MsgInfo *mh_fetch_msginfo (Folder *folder,
59 gint mh_add_msg (Folder *folder,
62 gboolean remove_source);
63 gint mh_move_msg (Folder *folder,
66 gint mh_move_msgs_with_dest (Folder *folder,
69 gint mh_copy_msg (Folder *folder,
72 gint mh_copy_msgs_with_dest (Folder *folder,
75 gint mh_remove_msg (Folder *folder,
78 gint mh_remove_all_msg (Folder *folder,
80 gboolean mh_is_msg_changed (Folder *folder,
84 gint mh_scan_folder (Folder *folder,
86 GSList *mh_get_num_list (Folder *folder,
88 void mh_scan_tree (Folder *folder);
90 gint mh_create_tree (Folder *folder);
91 FolderItem *mh_create_folder (Folder *folder,
94 gint mh_rename_folder (Folder *folder,
97 gint mh_remove_folder (Folder *folder,
100 gchar *mh_get_new_msg_filename (FolderItem *dest);
102 static GSList *mh_get_uncached_msgs (GHashTable *msg_table,
104 static MsgInfo *mh_parse_msg (const gchar *file,
106 static void mh_scan_tree_recursive (FolderItem *item);
108 static gboolean mh_rename_folder_func (GNode *node,
112 Folder *mh_folder_new(const gchar *name, const gchar *path)
116 folder = (Folder *)g_new0(MHFolder, 1);
117 mh_folder_init(folder, name, path);
122 void mh_folder_destroy(MHFolder *folder)
124 folder_local_folder_destroy(LOCAL_FOLDER(folder));
127 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
129 folder_local_folder_init(folder, name, path);
134 folder->get_msg_list = mh_get_msg_list;
136 folder->fetch_msg = mh_fetch_msg;
137 folder->fetch_msginfo = mh_fetch_msginfo;
138 folder->add_msg = mh_add_msg;
139 folder->move_msg = mh_move_msg;
140 folder->move_msgs_with_dest = mh_move_msgs_with_dest;
141 folder->copy_msg = mh_copy_msg;
142 folder->copy_msgs_with_dest = mh_copy_msgs_with_dest;
143 folder->remove_msg = mh_remove_msg;
144 folder->remove_all_msg = mh_remove_all_msg;
145 folder->is_msg_changed = mh_is_msg_changed;
147 folder->scan = mh_scan_folder;
149 folder->get_num_list = mh_get_num_list;
150 folder->scan_tree = mh_scan_tree;
151 folder->create_tree = mh_create_tree;
152 folder->create_folder = mh_create_folder;
153 folder->rename_folder = mh_rename_folder;
154 folder->remove_folder = mh_remove_folder;
157 void mh_get_last_num(Folder *folder, FolderItem *item)
166 g_return_if_fail(item != NULL);
168 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
170 path = folder_item_get_path(item);
171 g_return_if_fail(path != NULL);
172 if (change_dir(path) < 0) {
178 if ((dp = opendir(".")) == NULL) {
179 FILE_OP_ERROR(item->path, "opendir");
183 while ((d = readdir(dp)) != NULL) {
184 if ((num = to_number(d->d_name)) >= 0 &&
185 stat(d->d_name, &s) == 0 &&
186 S_ISREG(s.st_mode)) {
193 debug_print(_("Last number in dir %s = %d\n"), item->path, max);
194 item->last_num = max;
197 GSList *mh_get_num_list(Folder *folder, FolderItem *item)
207 g_return_val_if_fail(item != NULL, NULL);
209 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
211 path = folder_item_get_path(item);
212 g_return_val_if_fail(path != NULL, NULL);
213 if (change_dir(path) < 0) {
219 if ((dp = opendir(".")) == NULL) {
220 FILE_OP_ERROR(item->path, "opendir");
224 while ((d = readdir(dp)) != NULL) {
225 if ((num = to_number(d->d_name)) >= 0 &&
226 stat(d->d_name, &s) == 0 &&
227 S_ISREG(s.st_mode)) {
228 list = g_slist_prepend(list, GINT_TO_POINTER(num));
236 GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
239 GHashTable *msg_table;
242 gboolean scan_new = TRUE;
244 struct timeval tv_before, tv_after, tv_result;
246 gettimeofday(&tv_before, NULL);
249 g_return_val_if_fail(item != NULL, NULL);
251 path = folder_item_get_path(item);
252 if (stat(path, &s) < 0) {
253 FILE_OP_ERROR(path, "stat");
257 mtime = MAX(s.st_mtime, s.st_ctime);
258 if (item->mtime == mtime) {
259 debug_print("Folder is not modified.\n");
266 if (use_cache && !scan_new) {
267 mlist = procmsg_read_cache(item, FALSE);
269 mlist = mh_get_uncached_msgs(NULL, item);
270 } else if (use_cache) {
273 mlist = procmsg_read_cache(item, TRUE);
274 msg_table = procmsg_msg_hash_table_create(mlist);
276 newlist = mh_get_uncached_msgs(msg_table, item);
278 g_hash_table_destroy(msg_table);
280 mlist = g_slist_concat(mlist, newlist);
282 mlist = mh_get_uncached_msgs(NULL, item);
284 procmsg_set_flags(mlist, item);
287 gettimeofday(&tv_after, NULL);
289 timersub(&tv_after, &tv_before, &tv_result);
290 g_print("mh_get_msg_list: %s: elapsed time: %ld.%06ld sec\n",
291 item->path, tv_result.tv_sec, tv_result.tv_usec);
297 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
302 g_return_val_if_fail(item != NULL, NULL);
303 g_return_val_if_fail(num > 0, NULL);
305 path = folder_item_get_path(item);
306 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
308 if (!is_file_exist(file)) {
316 MsgInfo *mh_fetch_msginfo(Folder *folder, FolderItem *item, gint num)
324 g_return_val_if_fail(item != NULL, NULL);
325 g_return_val_if_fail(num > 0, NULL);
327 path = folder_item_get_path(item);
328 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
330 if (!is_file_exist(file)) {
335 folder_item_set_default_flags(item, &flags);
336 msginfo = procheader_parse_file(file, flags, TRUE, FALSE);
337 msginfo->msgnum = num;
338 msginfo->folder = item;
340 if (stat(file, &s) < 0) {
341 FILE_OP_ERROR(file, "stat");
345 msginfo->size = s.st_size;
346 msginfo->mtime = s.st_mtime;
352 gchar *mh_get_new_msg_filename(FolderItem *dest)
357 destpath = folder_item_get_path(dest);
358 g_return_val_if_fail(destpath != NULL, NULL);
360 if (!is_dir_exist(destpath))
361 make_dir_hier(destpath);
364 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
366 if (is_file_entry_exist(destfile)) {
378 #define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
380 MsgInfo newmsginfo; \
382 newmsginfo.msgnum = dest->last_num; \
383 newmsginfo.flags = msginfo->flags; \
384 if (dest->stype == F_OUTBOX || \
385 dest->stype == F_QUEUE || \
386 dest->stype == F_DRAFT || \
387 dest->stype == F_TRASH) \
388 MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
389 MSG_NEW|MSG_UNREAD|MSG_DELETED); \
391 procmsg_write_flags(&newmsginfo, fp); \
394 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
395 gboolean remove_source)
399 g_return_val_if_fail(dest != NULL, -1);
400 g_return_val_if_fail(file != NULL, -1);
402 if (dest->last_num < 0) {
403 mh_get_last_num(folder, dest);
404 if (dest->last_num < 0) return -1;
407 destfile = mh_get_new_msg_filename(dest);
408 g_return_val_if_fail(destfile != NULL, -1);
410 if (link(file, destfile) < 0) {
411 if (copy_file(file, destfile) < 0) {
412 g_warning(_("can't copy message %s to %s\n"),
420 if (unlink(file) < 0)
421 FILE_OP_ERROR(file, "unlink");
426 return dest->last_num;
429 static gint mh_do_move(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
434 PrefsFolderItem *prefs;
436 g_return_val_if_fail(dest != NULL, -1);
437 g_return_val_if_fail(msginfo != NULL, -1);
439 if (msginfo->folder == dest) {
440 g_warning(_("the src folder is identical to the dest.\n"));
444 if (dest->last_num < 0) {
445 mh_get_last_num(folder, dest);
446 if (dest->last_num < 0) return -1;
451 destfile = mh_get_new_msg_filename(dest);
452 g_return_val_if_fail(destfile != NULL, -1);
454 debug_print(_("Moving message %s%c%d to %s ...\n"),
455 msginfo->folder->path, G_DIR_SEPARATOR,
456 msginfo->msgnum, dest->path);
457 srcfile = procmsg_get_message_file(msginfo);
459 destfile = mh_get_new_msg_filename(dest);
460 if(!destfile) return -1;
462 srcfile = procmsg_get_message_file(msginfo);
464 if (move_file(srcfile, destfile) < 0) {
470 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
471 if (chmod(destfile, prefs->folder_chmod) < 0)
472 FILE_OP_ERROR(destfile, "chmod");
475 filemode = prefs->folder_chmod;
476 if (filemode & S_IRGRP) filemode |= S_IWGRP;
477 if (filemode & S_IROTH) filemode |= S_IWOTH;
484 return dest->last_num;
487 gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
492 g_return_val_if_fail(folder != NULL, -1);
493 g_return_val_if_fail(dest != NULL, -1);
494 g_return_val_if_fail(msginfo != NULL, -1);
495 g_return_val_if_fail(msginfo->folder != NULL, -1);
497 if (folder == msginfo->folder->folder)
498 return mh_do_move(folder, dest, msginfo);
500 srcfile = procmsg_get_message_file(msginfo);
501 if (!srcfile) return -1;
503 ret = mh_add_msg(folder, dest, srcfile, FALSE);
510 destdir = folder_item_get_path(dest);
511 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
512 g_warning(_("Can't open mark file.\n"));
514 SET_DEST_MSG_FLAGS(fp, dest, msginfo);
519 ret = folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
525 static gint mh_do_move_msgs_with_dest(Folder *folder, FolderItem *dest,
532 PrefsFolderItem *prefs;
534 g_return_val_if_fail(dest != NULL, -1);
535 g_return_val_if_fail(msglist != NULL, -1);
537 if (dest->last_num < 0) {
538 mh_get_last_num(folder, dest);
539 if (dest->last_num < 0) return -1;
544 for (cur = msglist; cur != NULL; cur = cur->next) {
545 msginfo = (MsgInfo *)cur->data;
547 if (msginfo->folder == dest) {
548 g_warning(_("the src folder is identical to the dest.\n"));
551 debug_print(_("Moving message %s%c%d to %s ...\n"),
552 msginfo->folder->path, G_DIR_SEPARATOR,
553 msginfo->msgnum, dest->path);
555 destfile = mh_get_new_msg_filename(dest);
556 if (!destfile) break;
557 srcfile = procmsg_get_message_file(msginfo);
558 destfile = mh_get_new_msg_filename(dest);
559 if(!destfile) return -1;
561 if (move_file(srcfile, destfile) < 0) {
572 return dest->last_num;
575 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
581 msginfo = (MsgInfo *)msglist->data;
582 if (folder == msginfo->folder->folder)
583 return mh_do_move_msgs_with_dest(folder, dest, msglist);
585 for (cur = msglist; cur != NULL; cur = cur->next) {
586 msginfo = (MsgInfo *)cur->data;
587 ret = mh_move_msg(folder, dest, msginfo);
588 if (ret == -1) break;
594 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
599 PrefsFolderItem *prefs;
601 g_return_val_if_fail(dest != NULL, -1);
602 g_return_val_if_fail(msginfo != NULL, -1);
604 if (msginfo->folder == dest) {
605 g_warning(_("the src folder is identical to the dest.\n"));
609 if (dest->last_num < 0) {
610 mh_get_last_num(folder, dest);
611 if (dest->last_num < 0) return -1;
616 destfile = mh_get_new_msg_filename(dest);
617 g_return_val_if_fail(destfile != NULL, -1);
619 debug_print(_("Copying message %s%c%d to %s ...\n"),
620 msginfo->folder->path, G_DIR_SEPARATOR,
621 msginfo->msgnum, dest->path);
623 srcfile = procmsg_get_message_file(msginfo);
624 destfile = mh_get_new_msg_filename(dest);
632 if (copy_file(srcfile, destfile) < 0) {
633 FILE_OP_ERROR(srcfile, "copy");
639 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
640 if (chmod(destfile, prefs->folder_chmod) < 0)
641 FILE_OP_ERROR(destfile, "chmod");
644 filemode = prefs->folder_chmod;
645 if (filemode & S_IRGRP) filemode |= S_IWGRP;
646 if (filemode & S_IROTH) filemode |= S_IWOTH;
653 return dest->last_num;
657 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
665 src_folder = msginfo->folder->folder;
667 g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
669 filename = src_folder->fetch_msg(src_folder,
672 if (filename == NULL)
675 num = folder->add_msg(folder, dest, filename, FALSE);
677 destdir = folder_item_get_path(dest);
682 newmsginfo.msgnum = dest->last_num;
683 newmsginfo.flags = msginfo->flags;
684 if (dest->stype == F_OUTBOX ||
685 dest->stype == F_QUEUE ||
686 dest->stype == F_DRAFT ||
687 dest->stype == F_TRASH)
688 MSG_UNSET_FLAGS(newmsginfo.flags,
689 MSG_NEW|MSG_UNREAD|MSG_DELETED);
691 procmsg_write_flags(&newmsginfo, fp);
699 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
706 g_return_val_if_fail(dest != NULL, -1);
707 g_return_val_if_fail(msglist != NULL, -1);
709 if (dest->last_num < 0) {
710 mh_get_last_num(folder, dest);
711 if (dest->last_num < 0) return -1;
714 for (cur = msglist; cur != NULL; cur = cur->next) {
715 msginfo = (MsgInfo *)cur->data;
717 if (msginfo->folder == dest) {
718 g_warning(_("the src folder is identical to the dest.\n"));
721 debug_print(_("Copying message %s%c%d to %s ...\n"),
722 msginfo->folder->path, G_DIR_SEPARATOR,
723 msginfo->msgnum, dest->path);
725 destfile = mh_get_new_msg_filename(dest);
726 if (!destfile) break;
727 srcfile = procmsg_get_message_file(msginfo);
729 if (copy_file(srcfile, destfile) < 0) {
730 FILE_OP_ERROR(srcfile, "copy");
741 return dest->last_num;
744 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
748 g_return_val_if_fail(item != NULL, -1);
750 file = mh_fetch_msg(folder, item, num);
751 g_return_val_if_fail(file != NULL, -1);
753 if (unlink(file) < 0) {
754 FILE_OP_ERROR(file, "unlink");
763 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
768 g_return_val_if_fail(item != NULL, -1);
770 path = folder_item_get_path(item);
771 g_return_val_if_fail(path != NULL, -1);
772 val = remove_all_numbered_files(path);
778 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
782 if (stat(itos(msginfo->msgnum), &s) < 0 ||
783 msginfo->size != s.st_size ||
784 msginfo->mtime != s.st_mtime)
790 gint mh_scan_folder(Folder *folder, FolderItem *item)
799 g_return_val_if_fail(item != NULL, -1);
801 debug_print("mh_scan_folder(): Scanning %s ...\n", item->path);
803 path = folder_item_get_path(item);
804 g_return_val_if_fail(path != NULL, -1);
805 if (change_dir(path) < 0) {
811 if ((dp = opendir(".")) == NULL) {
812 FILE_OP_ERROR(item->path, "opendir");
817 folder->ui_func(folder, item, folder->ui_func_data);
819 while ((d = readdir(dp)) != NULL) {
820 if ((num = to_number(d->d_name)) >= 0 &&
821 stat(d->d_name, &s) == 0 &&
822 S_ISREG(s.st_mode)) {
834 item->new = item->unread = item->total = 0;
836 gint new, unread, total, min, max;
838 procmsg_get_mark_sum(".", &new, &unread, &total, &min, &max, 0);
840 new += n_msg - total;
841 unread += n_msg - total;
844 item->unread = unread;
848 debug_print(_("Last number in dir %s = %d\n"), item->path, max);
849 item->last_num = max;
854 void mh_scan_tree(Folder *folder)
859 g_return_if_fail(folder != NULL);
861 item = folder_item_new(folder->name, NULL);
862 item->folder = folder;
863 folder->node = g_node_new(item);
865 rootpath = folder_item_get_path(item);
866 if (change_dir(rootpath) < 0) {
872 mh_create_tree(folder);
873 mh_scan_tree_recursive(item);
876 #define MAKE_DIR_IF_NOT_EXIST(dir) \
878 if (!is_dir_exist(dir)) { \
879 if (is_file_exist(dir)) { \
880 g_warning(_("File `%s' already exists.\n" \
881 "Can't create folder."), dir); \
884 if (make_dir(dir) < 0) \
889 gint mh_create_tree(Folder *folder)
893 g_return_val_if_fail(folder != NULL, -1);
895 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
896 rootpath = LOCAL_FOLDER(folder)->rootpath;
897 MAKE_DIR_IF_NOT_EXIST(rootpath);
898 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
899 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
900 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
901 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
902 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
903 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
908 #undef MAKE_DIR_IF_NOT_EXIST
910 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
915 FolderItem *new_item;
917 g_return_val_if_fail(folder != NULL, NULL);
918 g_return_val_if_fail(parent != NULL, NULL);
919 g_return_val_if_fail(name != NULL, NULL);
921 path = folder_item_get_path(parent);
922 if (!is_dir_exist(path))
925 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
928 if (make_dir(fullpath) < 0) {
936 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
939 path = g_strdup(name);
940 new_item = folder_item_new(name, path);
941 folder_item_append(parent, new_item);
947 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
955 g_return_val_if_fail(folder != NULL, -1);
956 g_return_val_if_fail(item != NULL, -1);
957 g_return_val_if_fail(item->path != NULL, -1);
958 g_return_val_if_fail(name != NULL, -1);
960 oldpath = folder_item_get_path(item);
961 if (!is_dir_exist(oldpath))
962 make_dir_hier(oldpath);
964 dirname = g_dirname(oldpath);
965 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
968 if (rename(oldpath, newpath) < 0) {
969 FILE_OP_ERROR(oldpath, "rename");
978 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
979 dirname = g_dirname(item->path);
980 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
983 newpath = g_strdup(name);
986 item->name = g_strdup(name);
988 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
990 paths[0] = g_strdup(item->path);
992 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
993 mh_rename_folder_func, paths);
1000 gint mh_remove_folder(Folder *folder, FolderItem *item)
1004 g_return_val_if_fail(folder != NULL, -1);
1005 g_return_val_if_fail(item != NULL, -1);
1006 g_return_val_if_fail(item->path != NULL, -1);
1008 path = folder_item_get_path(item);
1009 if (remove_dir_recursive(path) < 0) {
1010 g_warning("can't remove directory `%s'\n", path);
1016 folder_item_remove(item);
1021 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
1027 GSList *newlist = NULL;
1028 GSList *last = NULL;
1033 g_return_val_if_fail(item != NULL, NULL);
1035 path = folder_item_get_path(item);
1036 g_return_val_if_fail(path != NULL, NULL);
1037 if (change_dir(path) < 0) {
1043 if ((dp = opendir(".")) == NULL) {
1044 FILE_OP_ERROR(item->path, "opendir");
1048 debug_print(_("\tSearching uncached messages... "));
1051 while ((d = readdir(dp)) != NULL) {
1052 if ((num = to_number(d->d_name)) < 0) continue;
1053 if (stat(d->d_name, &s) < 0) {
1054 FILE_OP_ERROR(d->d_name, "stat");
1057 if (!S_ISREG(s.st_mode)) continue;
1059 msginfo = g_hash_table_lookup
1060 (msg_table, GUINT_TO_POINTER(num));
1063 /* not found in the cache (uncached message) */
1064 msginfo = mh_parse_msg(d->d_name, item);
1065 if (!msginfo) continue;
1069 g_slist_append(NULL, msginfo);
1071 last = g_slist_append(last, msginfo);
1078 /* discard all previous cache */
1079 while ((d = readdir(dp)) != NULL) {
1080 if (to_number(d->d_name) < 0) continue;
1081 if (stat(d->d_name, &s) < 0) {
1082 FILE_OP_ERROR(d->d_name, "stat");
1085 if (!S_ISREG(s.st_mode)) continue;
1087 msginfo = mh_parse_msg(d->d_name, item);
1088 if (!msginfo) continue;
1091 last = newlist = g_slist_append(NULL, msginfo);
1093 last = g_slist_append(last, msginfo);
1103 debug_print(_("%d uncached message(s) found.\n"), n_newmsg);
1105 debug_print(_("done.\n"));
1107 /* sort new messages in numerical order */
1109 debug_print(_("\tSorting uncached messages in numerical order... "));
1110 newlist = g_slist_sort
1111 (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
1112 debug_print(_("done.\n"));
1118 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1124 flags.perm_flags = MSG_NEW|MSG_UNREAD;
1125 flags.tmp_flags = 0;
1127 g_return_val_if_fail(item != NULL, NULL);
1128 g_return_val_if_fail(file != NULL, NULL);
1130 if (item->stype == F_QUEUE) {
1131 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1132 } else if (item->stype == F_DRAFT) {
1133 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1136 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1137 if (!msginfo) return NULL;
1139 msginfo->msgnum = atoi(file);
1140 msginfo->folder = item;
1142 if (stat(file, &s) < 0) {
1143 FILE_OP_ERROR(file, "stat");
1147 msginfo->size = s.st_size;
1148 msginfo->mtime = s.st_mtime;
1154 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
1159 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
1160 result = is_dir_exist(entry);
1167 * check whether PATH is a Maildir style mailbox.
1168 * This is the case if the 3 subdir: new, cur, tmp are existing.
1169 * This functon assumes that entry is an directory
1171 static gboolean mh_is_maildir(const gchar *path)
1173 return mh_is_maildir_one(path, "new") &&
1174 mh_is_maildir_one(path, "cur") &&
1175 mh_is_maildir_one(path, "tmp");
1178 static void mh_scan_tree_recursive(FolderItem *item)
1186 g_return_if_fail(item != NULL);
1187 g_return_if_fail(item->folder != NULL);
1189 dp = opendir(item->path ? item->path : ".");
1191 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
1195 debug_print("scanning %s ...\n",
1196 item->path ? item->path
1197 : LOCAL_FOLDER(item->folder)->rootpath);
1198 if (item->folder->ui_func)
1199 item->folder->ui_func(item->folder, item,
1200 item->folder->ui_func_data);
1202 while ((d = readdir(dp)) != NULL) {
1203 if (d->d_name[0] == '.') continue;
1206 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1209 entry = g_strdup(d->d_name);
1211 if (stat(entry, &s) < 0) {
1212 FILE_OP_ERROR(entry, "stat");
1217 if (S_ISDIR(s.st_mode)) {
1218 FolderItem *new_item;
1220 if (mh_is_maildir(entry)) {
1225 new_item = folder_item_new(d->d_name, entry);
1226 folder_item_append(item, new_item);
1228 if (!strcmp(d->d_name, INBOX_DIR)) {
1229 new_item->stype = F_INBOX;
1230 item->folder->inbox = new_item;
1231 } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
1232 new_item->stype = F_OUTBOX;
1233 item->folder->outbox = new_item;
1234 } else if (!strcmp(d->d_name, DRAFT_DIR)) {
1235 new_item->stype = F_DRAFT;
1236 item->folder->draft = new_item;
1237 } else if (!strcmp(d->d_name, QUEUE_DIR)) {
1238 new_item->stype = F_QUEUE;
1239 item->folder->queue = new_item;
1240 } else if (!strcmp(d->d_name, TRASH_DIR)) {
1241 new_item->stype = F_TRASH;
1242 item->folder->trash = new_item;
1245 mh_scan_tree_recursive(new_item);
1246 } else if (to_number(d->d_name) != -1) n_msg++;
1255 gint new, unread, total, min, max;
1257 procmsg_get_mark_sum(item->path, &new, &unread, &total,
1259 if (n_msg > total) {
1260 new += n_msg - total;
1261 unread += n_msg - total;
1264 item->unread = unread;
1265 item->total = n_msg;
1270 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1272 FolderItem *item = node->data;
1273 gchar **paths = data;
1274 const gchar *oldpath = paths[0];
1275 const gchar *newpath = paths[1];
1277 gchar *new_itempath;
1280 oldpathlen = strlen(oldpath);
1281 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1282 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1286 base = item->path + oldpathlen;
1287 while (*base == G_DIR_SEPARATOR) base++;
1289 new_itempath = g_strdup(newpath);
1291 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1294 item->path = new_itempath;