initial message templates support
[claws.git] / src / mh.c
blob0ff17bec267ead28cc7c114f08cfbe8c5be6347a
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 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.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <dirent.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
33 #undef MEASURE_TIME
35 #ifdef MEASURE_TIME
36 # include <sys/time.h>
37 #endif
39 #include "intl.h"
40 #include "folder.h"
41 #include "mh.h"
42 #include "procmsg.h"
43 #include "procheader.h"
44 #include "utils.h"
46 static GSList *mh_get_uncached_msgs (GHashTable *msg_table,
47 FolderItem *item);
48 static MsgInfo *mh_parse_msg (const gchar *file,
49 FolderItem *item);
50 static void mh_scan_tree_recursive (FolderItem *item);
52 static gboolean mh_rename_folder_func (GNode *node,
53 gpointer data);
56 GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
58 GSList *mlist;
59 GHashTable *msg_table;
60 gchar *path;
61 struct stat s;
62 gboolean scan_new = TRUE;
63 #ifdef MEASURE_TIME
64 struct timeval tv_before, tv_after, tv_result;
66 gettimeofday(&tv_before, NULL);
67 #endif
69 g_return_val_if_fail(item != NULL, NULL);
71 path = folder_item_get_path(item);
72 if (stat(path, &s) < 0) {
73 FILE_OP_ERROR(path, "stat");
74 } else {
75 if (item->mtime == s.st_mtime) {
76 debug_print("Folder is not modified.\n");
77 scan_new = FALSE;
78 } else
79 item->mtime = s.st_mtime;
81 g_free(path);
83 if (use_cache && !scan_new) {
84 mlist = procmsg_read_cache(item, FALSE);
85 if (!mlist)
86 mlist = mh_get_uncached_msgs(NULL, item);
87 } else if (use_cache) {
88 GSList *newlist;
90 mlist = procmsg_read_cache(item, TRUE);
91 msg_table = procmsg_msg_hash_table_create(mlist);
93 newlist = mh_get_uncached_msgs(msg_table, item);
94 if (msg_table)
95 g_hash_table_destroy(msg_table);
97 mlist = g_slist_concat(mlist, newlist);
98 } else
99 mlist = mh_get_uncached_msgs(NULL, item);
101 procmsg_set_flags(mlist, item);
103 #ifdef MEASURE_TIME
104 gettimeofday(&tv_after, NULL);
106 timersub(&tv_after, &tv_before, &tv_result);
107 g_print("mh_get_msg_list: %s: elapsed time: %ld.%06ld sec\n",
108 item->path, tv_result.tv_sec, tv_result.tv_usec);
109 #endif
111 return mlist;
114 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
116 gchar *path;
117 gchar *file;
119 g_return_val_if_fail(item != NULL, NULL);
120 g_return_val_if_fail(num > 0 && num <= item->last_num, NULL);
122 path = folder_item_get_path(item);
123 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
124 g_free(path);
125 if (!is_file_exist(file)) {
126 g_free(file);
127 return NULL;
130 return file;
133 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
134 gboolean remove_source)
136 gchar *destpath;
137 gchar *destfile;
139 g_return_val_if_fail(dest != NULL, -1);
140 g_return_val_if_fail(file != NULL, -1);
142 if (dest->last_num < 0) {
143 mh_scan_folder(folder, dest);
144 if (dest->last_num < 0) return -1;
147 destpath = folder_item_get_path(dest);
148 g_return_val_if_fail(destpath != NULL, -1);
149 if (!is_dir_exist(destpath))
150 make_dir_hier(destpath);
152 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
153 dest->last_num + 1);
155 if (link(file, destfile) < 0) {
156 if (EXDEV == errno) {
157 if (copy_file(file, destfile) < 0) {
158 g_warning(_("can't copy message %s to %s\n"),
159 file, destfile);
160 g_free(destfile);
161 return -1;
163 } else {
164 FILE_OP_ERROR(file, "link");
165 g_free(destfile);
166 return -1;
170 if (remove_source) {
171 if (unlink(file) < 0)
172 FILE_OP_ERROR(file, "unlink");
175 g_free(destfile);
176 dest->last_num++;
177 return dest->last_num;
180 gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
182 gchar *destdir;
183 gchar *srcfile;
184 gchar *destfile;
185 FILE *fp;
186 gint filemode = 0;
187 PrefsFolderItem *prefs;
189 g_return_val_if_fail(dest != NULL, -1);
190 g_return_val_if_fail(msginfo != NULL, -1);
192 if (msginfo->folder == dest) {
193 g_warning(_("the src folder is identical to the dest.\n"));
194 return -1;
197 if (dest->last_num < 0) {
198 mh_scan_folder(folder, dest);
199 if (dest->last_num < 0) return -1;
202 prefs = dest->prefs;
204 destdir = folder_item_get_path(dest);
205 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
206 g_warning(_("Can't open mark file.\n"));
208 debug_print(_("Moving message %s%c%d to %s ...\n"),
209 msginfo->folder->path, G_DIR_SEPARATOR,
210 msginfo->msgnum, dest->path);
211 srcfile = procmsg_get_message_file_path(msginfo);
212 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
213 dest->last_num + 1);
214 g_free(destdir);
216 if (move_file(srcfile, destfile) < 0) {
217 g_free(srcfile);
218 g_free(destfile);
219 if (fp) fclose(fp);
220 return -1;
223 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
224 if (chmod(destfile, prefs->folder_chmod) < 0)
225 FILE_OP_ERROR(destfile, "chmod");
227 /* for mark file */
228 filemode = prefs->folder_chmod;
229 if (filemode & S_IRGRP) filemode |= S_IWGRP;
230 if (filemode & S_IROTH) filemode |= S_IWOTH;
233 g_free(srcfile);
234 g_free(destfile);
235 dest->last_num++;
237 if (fp) {
238 MsgInfo newmsginfo;
240 newmsginfo.msgnum = dest->last_num;
241 newmsginfo.flags = msginfo->flags;
242 if (dest->stype == F_OUTBOX ||
243 dest->stype == F_QUEUE ||
244 dest->stype == F_DRAFT ||
245 dest->stype == F_TRASH)
246 MSG_UNSET_PERM_FLAGS(newmsginfo.flags,
247 MSG_NEW|MSG_UNREAD|MSG_DELETED);
249 procmsg_write_flags(&newmsginfo, fp);
251 if (filemode) {
252 #if HAVE_FCHMOD
253 fchmod(fileno(fp), filemode);
254 #else
255 markfile = folder_item_get_mark_file(dest);
256 if (markfile) {
257 chmod(markfile, filemode);
258 g_free(markfile);
260 #endif
263 fclose(fp);
266 return dest->last_num;
269 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
271 gchar *destdir;
272 gchar *srcfile;
273 gchar *destfile;
274 FILE *fp;
275 GSList *cur;
276 MsgInfo *msginfo;
277 PrefsFolderItem *prefs;
279 g_return_val_if_fail(dest != NULL, -1);
280 g_return_val_if_fail(msglist != NULL, -1);
282 if (dest->last_num < 0) {
283 mh_scan_folder(folder, dest);
284 if (dest->last_num < 0) return -1;
287 prefs = dest->prefs;
289 destdir = folder_item_get_path(dest);
290 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
291 g_warning(_("Can't open mark file.\n"));
293 for (cur = msglist; cur != NULL; cur = cur->next) {
294 msginfo = (MsgInfo *)cur->data;
296 if (msginfo->folder == dest) {
297 g_warning(_("the src folder is identical to the dest.\n"));
298 continue;
300 debug_print(_("Moving message %s%c%d to %s ...\n"),
301 msginfo->folder->path, G_DIR_SEPARATOR,
302 msginfo->msgnum, dest->path);
304 srcfile = procmsg_get_message_file_path(msginfo);
305 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
306 dest->last_num + 1);
308 if (move_file(srcfile, destfile) < 0) {
309 g_free(srcfile);
310 g_free(destfile);
311 break;
314 g_free(srcfile);
315 g_free(destfile);
316 dest->last_num++;
318 if (fp) {
319 MsgInfo newmsginfo;
321 newmsginfo.msgnum = dest->last_num;
322 newmsginfo.flags = msginfo->flags;
323 if (dest->stype == F_OUTBOX ||
324 dest->stype == F_QUEUE ||
325 dest->stype == F_DRAFT ||
326 dest->stype == F_TRASH)
327 MSG_UNSET_PERM_FLAGS
328 (newmsginfo.flags,
329 MSG_NEW|MSG_UNREAD|MSG_DELETED);
331 procmsg_write_flags(&newmsginfo, fp);
335 g_free(destdir);
336 if (fp) fclose(fp);
338 return dest->last_num;
341 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
343 gchar *destdir;
344 gchar *srcfile;
345 gchar *destfile;
346 FILE *fp;
347 gint filemode = 0;
348 PrefsFolderItem *prefs;
350 g_return_val_if_fail(dest != NULL, -1);
351 g_return_val_if_fail(msginfo != NULL, -1);
353 if (msginfo->folder == dest) {
354 g_warning(_("the src folder is identical to the dest.\n"));
355 return -1;
358 if (dest->last_num < 0) {
359 mh_scan_folder(folder, dest);
360 if (dest->last_num < 0) return -1;
363 prefs = dest->prefs;
365 destdir = folder_item_get_path(dest);
366 if (!is_dir_exist(destdir))
367 make_dir_hier(destdir);
369 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
370 g_warning(_("Can't open mark file.\n"));
372 debug_print(_("Copying message %s%c%d to %s ...\n"),
373 msginfo->folder->path, G_DIR_SEPARATOR,
374 msginfo->msgnum, dest->path);
375 srcfile = procmsg_get_message_file_path(msginfo);
376 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
377 dest->last_num + 1);
378 g_free(destdir);
380 dest->op_count--;
382 if (is_file_exist(destfile)) {
383 g_warning(_("%s already exists."), destfile);
384 g_free(srcfile);
385 g_free(destfile);
386 if (fp) fclose(fp);
387 return -1;
390 if (copy_file(srcfile, destfile) < 0) {
391 FILE_OP_ERROR(srcfile, "copy");
392 g_free(srcfile);
393 g_free(destfile);
394 if (fp) fclose(fp);
395 return -1;
398 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
399 if (chmod(destfile, prefs->folder_chmod) < 0)
400 FILE_OP_ERROR(destfile, "chmod");
402 /* for mark file */
403 filemode = prefs->folder_chmod;
404 if (filemode & S_IRGRP) filemode |= S_IWGRP;
405 if (filemode & S_IROTH) filemode |= S_IWOTH;
408 g_free(srcfile);
409 g_free(destfile);
410 dest->last_num++;
412 if (fp) {
413 MsgInfo newmsginfo;
415 newmsginfo.msgnum = dest->last_num;
416 newmsginfo.flags = msginfo->flags;
417 if (dest->stype == F_OUTBOX ||
418 dest->stype == F_QUEUE ||
419 dest->stype == F_DRAFT ||
420 dest->stype == F_TRASH)
421 MSG_UNSET_PERM_FLAGS(newmsginfo.flags,
422 MSG_NEW|MSG_UNREAD|MSG_DELETED);
423 procmsg_write_flags(&newmsginfo, fp);
425 if (filemode) {
426 #if HAVE_FCHMOD
427 fchmod(fileno(fp), filemode);
428 #else
429 markfile = folder_item_get_mark_file(dest);
430 if (markfile) {
431 chmod(markfile, filemode);
432 g_free(markfile);
434 #endif
437 fclose(fp);
440 return dest->last_num;
444 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
446 Folder * src_folder;
447 gchar * filename;
448 gint num;
449 gchar * destdir;
450 FILE * fp;
452 src_folder = msginfo->folder->folder;
454 g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
456 filename = src_folder->fetch_msg(src_folder,
457 msginfo->folder,
458 msginfo->msgnum);
459 if (filename == NULL)
460 return -1;
462 num = folder->add_msg(folder, dest, filename, FALSE);
464 destdir = folder_item_get_path(dest);
465 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
466 g_warning(_("Can't open mark file.\n"));
468 if (fp) {
469 MsgInfo newmsginfo;
471 newmsginfo.msgnum = dest->last_num;
472 newmsginfo.flags = msginfo->flags;
473 if (dest->stype == F_OUTBOX ||
474 dest->stype == F_QUEUE ||
475 dest->stype == F_DRAFT ||
476 dest->stype == F_TRASH)
477 MSG_UNSET_FLAGS(newmsginfo.flags,
478 MSG_NEW|MSG_UNREAD|MSG_DELETED);
480 procmsg_write_flags(&newmsginfo, fp);
481 fclose(fp);
484 return num;
488 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
490 gchar *destdir;
491 gchar *srcfile;
492 gchar *destfile;
493 FILE *fp;
494 GSList *cur;
495 MsgInfo *msginfo;
497 g_return_val_if_fail(dest != NULL, -1);
498 g_return_val_if_fail(msglist != NULL, -1);
500 if (dest->last_num < 0) {
501 mh_scan_folder(folder, dest);
502 if (dest->last_num < 0) return -1;
505 destdir = folder_item_get_path(dest);
506 if (!is_dir_exist(destdir))
507 make_dir_hier(destdir);
509 if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
510 g_warning(_("Can't open mark file.\n"));
512 for (cur = msglist; cur != NULL; cur = cur->next) {
513 msginfo = (MsgInfo *)cur->data;
515 if (msginfo->folder == dest) {
516 g_warning(_("the src folder is identical to the dest.\n"));
517 continue;
519 debug_print(_("Copying message %s%c%d to %s ...\n"),
520 msginfo->folder->path, G_DIR_SEPARATOR,
521 msginfo->msgnum, dest->path);
523 srcfile = procmsg_get_message_file_path(msginfo);
524 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
525 dest->last_num + 1);
527 if (is_file_exist(destfile)) {
528 g_warning(_("%s already exists."), destfile);
529 g_free(srcfile);
530 g_free(destfile);
531 break;
534 if (copy_file(srcfile, destfile) < 0) {
535 FILE_OP_ERROR(srcfile, "copy");
536 g_free(srcfile);
537 g_free(destfile);
538 break;
541 g_free(srcfile);
542 g_free(destfile);
543 dest->last_num++;
545 if (fp) {
546 MsgInfo newmsginfo;
548 newmsginfo.msgnum = dest->last_num;
549 newmsginfo.flags = msginfo->flags;
550 if (dest->stype == F_OUTBOX ||
551 dest->stype == F_QUEUE ||
552 dest->stype == F_DRAFT ||
553 dest->stype == F_TRASH)
554 MSG_UNSET_PERM_FLAGS
555 (newmsginfo.flags,
556 MSG_NEW|MSG_UNREAD|MSG_DELETED);
557 procmsg_write_flags(&newmsginfo, fp);
561 g_free(destdir);
562 if (fp) fclose(fp);
564 return dest->last_num;
567 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
569 gchar *file;
571 g_return_val_if_fail(item != NULL, -1);
573 file = mh_fetch_msg(folder, item, num);
574 g_return_val_if_fail(file != NULL, -1);
576 if (unlink(file) < 0) {
577 FILE_OP_ERROR(file, "unlink");
578 g_free(file);
579 return -1;
582 g_free(file);
583 return 0;
586 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
588 gchar *path;
589 gint val;
591 g_return_val_if_fail(item != NULL, -1);
593 path = folder_item_get_path(item);
594 g_return_val_if_fail(path != NULL, -1);
595 val = remove_all_numbered_files(path);
596 g_free(path);
598 return val;
601 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
603 struct stat s;
605 if (stat(itos(msginfo->msgnum), &s) < 0 ||
606 msginfo->size != s.st_size ||
607 msginfo->mtime != s.st_mtime)
608 return TRUE;
610 return FALSE;
613 void mh_scan_folder(Folder *folder, FolderItem *item)
615 gchar *path;
616 DIR *dp;
617 struct dirent *d;
618 struct stat s;
619 gint max = 0;
620 gint num;
621 gint n_msg = 0;
623 g_return_if_fail(item != NULL);
625 path = folder_item_get_path(item);
626 g_return_if_fail(path != NULL);
627 if (change_dir(path) < 0) {
628 g_free(path);
629 return;
631 g_free(path);
633 if ((dp = opendir(".")) == NULL) {
634 FILE_OP_ERROR(item->path, "opendir");
635 return;
638 if (folder->ui_func)
639 folder->ui_func(folder, item, folder->ui_func_data);
641 while ((d = readdir(dp)) != NULL) {
642 if ((num = to_number(d->d_name)) >= 0 &&
643 stat(d->d_name, &s) == 0 &&
644 S_ISREG(s.st_mode)) {
645 n_msg++;
646 if (max < num)
647 max = num;
651 closedir(dp);
653 if (n_msg == 0)
654 item->new = item->unread = item->total = 0;
655 else {
656 gint new, unread, total;
658 procmsg_get_mark_sum(".", &new, &unread, &total);
659 if (n_msg > total) {
660 new += n_msg - total;
661 unread += n_msg - total;
663 item->new = new;
664 item->unread = unread;
665 item->total = n_msg;
668 debug_print(_("Last number in dir %s = %d\n"), item->path, max);
669 item->last_num = max;
672 void mh_scan_tree(Folder *folder)
674 FolderItem *item;
675 gchar *rootpath;
677 g_return_if_fail(folder != NULL);
679 folder_tree_destroy(folder);
680 item = folder_item_new(folder->name, NULL);
681 item->folder = folder;
682 folder->node = g_node_new(item);
684 rootpath = folder_item_get_path(item);
685 if (change_dir(rootpath) < 0) {
686 g_free(rootpath);
687 return;
689 g_free(rootpath);
691 mh_scan_tree_recursive(item);
694 #define MAKE_DIR_IF_NOT_EXIST(dir) \
696 if (!is_dir_exist(dir)) { \
697 if (is_file_exist(dir)) { \
698 g_warning(_("File `%s' already exists.\n" \
699 "Can't create folder."), dir); \
700 return -1; \
702 if (mkdir(dir, S_IRWXU) < 0) { \
703 FILE_OP_ERROR(dir, "mkdir"); \
704 return -1; \
706 if (chmod(dir, S_IRWXU) < 0) \
707 FILE_OP_ERROR(dir, "chmod"); \
711 gint mh_create_tree(Folder *folder)
713 gchar *rootpath;
715 g_return_val_if_fail(folder != NULL, -1);
717 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
718 rootpath = LOCAL_FOLDER(folder)->rootpath;
719 MAKE_DIR_IF_NOT_EXIST(rootpath);
720 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
721 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
722 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
723 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
724 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
725 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
727 return 0;
730 #undef MAKE_DIR_IF_NOT_EXIST
732 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
733 const gchar *name)
735 gchar *path;
736 gchar *fullpath;
737 FolderItem *new_item;
739 g_return_val_if_fail(folder != NULL, NULL);
740 g_return_val_if_fail(parent != NULL, NULL);
741 g_return_val_if_fail(name != NULL, NULL);
743 path = folder_item_get_path(parent);
744 if (!is_dir_exist(path))
745 make_dir_hier(path);
747 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
748 g_free(path);
750 if (mkdir(fullpath, S_IRWXU) < 0) {
751 FILE_OP_ERROR(fullpath, "mkdir");
752 g_free(fullpath);
753 return NULL;
755 if (chmod(fullpath, S_IRWXU) < 0)
756 FILE_OP_ERROR(fullpath, "chmod");
758 g_free(fullpath);
760 if (parent->path)
761 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
762 NULL);
763 else
764 path = g_strdup(name);
765 new_item = folder_item_new(name, path);
766 folder_item_append(parent, new_item);
767 g_free(path);
769 return new_item;
772 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
774 gchar *oldpath;
775 gchar *dirname;
776 gchar *newpath;
777 GNode *node;
778 gchar *paths[2];
780 g_return_val_if_fail(folder != NULL, -1);
781 g_return_val_if_fail(item != NULL, -1);
782 g_return_val_if_fail(item->path != NULL, -1);
783 g_return_val_if_fail(name != NULL, -1);
785 oldpath = folder_item_get_path(item);
786 if (!is_dir_exist(oldpath))
787 make_dir_hier(oldpath);
789 dirname = g_dirname(oldpath);
790 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
791 g_free(dirname);
793 if (rename(oldpath, newpath) < 0) {
794 FILE_OP_ERROR(oldpath, "rename");
795 g_free(oldpath);
796 g_free(newpath);
797 return -1;
800 g_free(oldpath);
801 g_free(newpath);
803 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
804 dirname = g_dirname(item->path);
805 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
806 g_free(dirname);
807 } else
808 newpath = g_strdup(name);
810 g_free(item->name);
811 item->name = g_strdup(name);
813 node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
814 item);
815 paths[0] = g_strdup(item->path);
816 paths[1] = newpath;
817 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
818 mh_rename_folder_func, paths);
820 g_free(paths[0]);
821 g_free(paths[1]);
822 return 0;
825 gint mh_remove_folder(Folder *folder, FolderItem *item)
827 gchar *path;
829 g_return_val_if_fail(folder != NULL, -1);
830 g_return_val_if_fail(item != NULL, -1);
831 g_return_val_if_fail(item->path != NULL, -1);
833 path = folder_item_get_path(item);
834 if (remove_dir_recursive(path) < 0) {
835 g_warning("can't remove directory `%s'\n", path);
836 g_free(path);
837 return -1;
840 g_free(path);
841 folder_item_remove(item);
842 return 0;
846 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
848 gchar *path;
849 DIR *dp;
850 struct dirent *d;
851 struct stat s;
852 GSList *newlist = NULL;
853 GSList *last = NULL;
854 MsgInfo *msginfo;
855 gint n_newmsg = 0;
856 gint num;
858 g_return_val_if_fail(item != NULL, NULL);
860 path = folder_item_get_path(item);
861 g_return_val_if_fail(path != NULL, NULL);
862 if (change_dir(path) < 0) {
863 g_free(path);
864 return NULL;
866 g_free(path);
868 if ((dp = opendir(".")) == NULL) {
869 FILE_OP_ERROR(item->path, "opendir");
870 return NULL;
873 debug_print(_("\tSearching uncached messages... "));
875 if (msg_table) {
876 while ((d = readdir(dp)) != NULL) {
877 if ((num = to_number(d->d_name)) < 0) continue;
878 if (stat(d->d_name, &s) < 0) {
879 FILE_OP_ERROR(d->d_name, "stat");
880 continue;
882 if (!S_ISREG(s.st_mode)) continue;
884 msginfo = g_hash_table_lookup
885 (msg_table, GUINT_TO_POINTER(num));
887 if (!msginfo) {
888 /* not found in the cache (uncached message) */
889 msginfo = mh_parse_msg(d->d_name, item);
890 if (!msginfo) continue;
892 if (!newlist)
893 last = newlist =
894 g_slist_append(NULL, msginfo);
895 else {
896 last = g_slist_append(last, msginfo);
897 last = last->next;
899 n_newmsg++;
902 } else {
903 /* discard all previous cache */
904 while ((d = readdir(dp)) != NULL) {
905 if (to_number(d->d_name) < 0) continue;
906 if (stat(d->d_name, &s) < 0) {
907 FILE_OP_ERROR(d->d_name, "stat");
908 continue;
910 if (!S_ISREG(s.st_mode)) continue;
912 msginfo = mh_parse_msg(d->d_name, item);
913 if (!msginfo) continue;
915 if (!newlist)
916 last = newlist = g_slist_append(NULL, msginfo);
917 else {
918 last = g_slist_append(last, msginfo);
919 last = last->next;
921 n_newmsg++;
925 closedir(dp);
927 if (n_newmsg)
928 debug_print(_("%d uncached message(s) found.\n"), n_newmsg);
929 else
930 debug_print(_("done.\n"));
932 /* sort new messages in numerical order */
933 if (newlist) {
934 debug_print(_("\tSorting uncached messages in numerical order... "));
935 newlist = g_slist_sort
936 (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
937 debug_print(_("done.\n"));
940 return newlist;
943 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
945 struct stat s;
946 MsgInfo *msginfo;
947 MsgFlags flags;
949 flags.perm_flags = MSG_NEW|MSG_UNREAD;
950 flags.tmp_flags = 0;
952 g_return_val_if_fail(item != NULL, NULL);
953 g_return_val_if_fail(file != NULL, NULL);
955 if (item->stype == F_QUEUE) {
956 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
957 } else if (item->stype == F_DRAFT) {
958 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
961 msginfo = procheader_parse(file, flags, FALSE);
962 if (!msginfo) return NULL;
964 msginfo->msgnum = atoi(file);
965 msginfo->folder = item;
967 if (stat(file, &s) < 0) {
968 FILE_OP_ERROR(file, "stat");
969 msginfo->size = 0;
970 msginfo->mtime = 0;
971 } else {
972 msginfo->size = s.st_size;
973 msginfo->mtime = s.st_mtime;
976 return msginfo;
979 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
981 gchar *entry;
982 gboolean result;
984 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
985 result = is_dir_exist(entry);
986 g_free(entry);
988 return result;
992 * check whether PATH is a Maildir style mailbox.
993 * This is the case if the 3 subdir: new, cur, tmp are existing.
994 * This functon assumes that entry is an directory
996 static gboolean mh_is_maildir(const gchar *path)
998 return mh_is_maildir_one(path, "new") &&
999 mh_is_maildir_one(path, "cur") &&
1000 mh_is_maildir_one(path, "tmp");
1003 static void mh_scan_tree_recursive(FolderItem *item)
1005 DIR *dp;
1006 struct dirent *d;
1007 struct stat s;
1008 gchar *entry;
1009 gint n_msg = 0;
1011 g_return_if_fail(item != NULL);
1012 g_return_if_fail(item->folder != NULL);
1014 dp = opendir(item->path ? item->path : ".");
1015 if (!dp) {
1016 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
1017 return;
1020 debug_print("scanning %s ...\n",
1021 item->path ? item->path
1022 : LOCAL_FOLDER(item->folder)->rootpath);
1023 if (item->folder->ui_func)
1024 item->folder->ui_func(item->folder, item,
1025 item->folder->ui_func_data);
1027 while ((d = readdir(dp)) != NULL) {
1028 if (d->d_name[0] == '.') continue;
1030 if (item->path)
1031 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1032 d->d_name, NULL);
1033 else
1034 entry = g_strdup(d->d_name);
1036 if (stat(entry, &s) < 0) {
1037 FILE_OP_ERROR(entry, "stat");
1038 g_free(entry);
1039 continue;
1042 if (S_ISDIR(s.st_mode)) {
1043 FolderItem *new_item;
1045 if (mh_is_maildir(entry)) {
1046 g_free(entry);
1047 continue;
1050 new_item = folder_item_new(d->d_name, entry);
1051 folder_item_append(item, new_item);
1052 if (!item->path) {
1053 if (!strcmp(d->d_name, "inbox")) {
1054 new_item->stype = F_INBOX;
1055 item->folder->inbox = new_item;
1056 } else if (!strcmp(d->d_name, "outbox")) {
1057 new_item->stype = F_OUTBOX;
1058 item->folder->outbox = new_item;
1059 } else if (!strcmp(d->d_name, "draft")) {
1060 new_item->stype = F_DRAFT;
1061 item->folder->draft = new_item;
1062 } else if (!strcmp(d->d_name, "queue")) {
1063 new_item->stype = F_QUEUE;
1064 item->folder->queue = new_item;
1065 } else if (!strcmp(d->d_name, "trash")) {
1066 new_item->stype = F_TRASH;
1067 item->folder->trash = new_item;
1070 mh_scan_tree_recursive(new_item);
1071 } else if (to_number(d->d_name) != -1) n_msg++;
1073 g_free(entry);
1076 closedir(dp);
1078 if (item->path) {
1079 gint new, unread, total;
1081 procmsg_get_mark_sum(item->path, &new, &unread, &total);
1082 if (n_msg > total) {
1083 new += n_msg - total;
1084 unread += n_msg - total;
1086 item->new = new;
1087 item->unread = unread;
1088 item->total = n_msg;
1092 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1094 FolderItem *item = node->data;
1095 gchar **paths = data;
1096 const gchar *oldpath = paths[0];
1097 const gchar *newpath = paths[1];
1098 gchar *base;
1099 gchar *new_itempath;
1100 gint oldpathlen;
1102 oldpathlen = strlen(oldpath);
1103 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1104 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1105 return TRUE;
1108 base = item->path + oldpathlen;
1109 while (*base == G_DIR_SEPARATOR) base++;
1110 if (*base == '\0')
1111 new_itempath = g_strdup(newpath);
1112 else
1113 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1114 NULL);
1115 g_free(item->path);
1116 item->path = new_itempath;
1118 return FALSE;