Improve some sieve-related translations
[claws.git] / src / mh.c
blob1b46ad4d046f30f74ef1e57758a5704e668d9ea5
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2013 Hiroyuki Yamamoto and the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
25 #include "defs.h"
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <dirent.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <time.h>
36 #include "folder.h"
37 #include "folder_item_prefs.h"
38 #include "mh.h"
39 #include "procmsg.h"
40 #include "procheader.h"
41 #include "utils.h"
42 #include "codeconv.h"
43 #include "statusbar.h"
44 #include "gtkutils.h"
45 #include "timing.h"
46 #include "msgcache.h"
47 #include "file-utils.h"
49 /* Define possible missing constants for Windows. */
50 #ifdef G_OS_WIN32
51 # ifndef S_IRGRP
52 # define S_IRGRP 0
53 # define S_IWGRP 0
54 # endif
55 # ifndef S_IROTH
56 # define S_IROTH 0
57 # define S_IWOTH 0
58 # endif
59 #endif
62 static void mh_folder_init (Folder *folder,
63 const gchar *name,
64 const gchar *path);
66 static Folder *mh_folder_new (const gchar *name,
67 const gchar *path);
68 static void mh_folder_destroy (Folder *folder);
69 static gchar *mh_fetch_msg (Folder *folder,
70 FolderItem *item,
71 gint num);
72 static MsgInfo *mh_get_msginfo (Folder *folder,
73 FolderItem *item,
74 gint num);
75 static gint mh_add_msg (Folder *folder,
76 FolderItem *dest,
77 const gchar *file,
78 MsgFlags *flags);
79 static gint mh_add_msgs (Folder *folder,
80 FolderItem *dest,
81 GSList *file_list,
82 GHashTable *relation);
83 static gint mh_copy_msg (Folder *folder,
84 FolderItem *dest,
85 MsgInfo *msginfo);
86 static gint mh_copy_msgs (Folder *folder,
87 FolderItem *dest,
88 MsgInfoList *msglist,
89 GHashTable *relation);
90 static gint mh_remove_msg (Folder *folder,
91 FolderItem *item,
92 gint num);
93 static gint mh_remove_msgs (Folder *folder,
94 FolderItem *item,
95 MsgInfoList *msglist,
96 GHashTable *relation);
97 static gint mh_remove_all_msg (Folder *folder,
98 FolderItem *item);
99 static gboolean mh_is_msg_changed (Folder *folder,
100 FolderItem *item,
101 MsgInfo *msginfo);
103 static gint mh_get_num_list (Folder *folder,
104 FolderItem *item,
105 GSList **list,
106 gboolean *old_uids_valid);
107 static gint mh_scan_tree (Folder *folder);
109 static gint mh_create_tree (Folder *folder);
110 static FolderItem *mh_create_folder (Folder *folder,
111 FolderItem *parent,
112 const gchar *name);
113 static gint mh_rename_folder (Folder *folder,
114 FolderItem *item,
115 const gchar *name);
116 static gint mh_remove_folder (Folder *folder,
117 FolderItem *item);
119 static gchar *mh_get_new_msg_filename (FolderItem *dest);
121 static MsgInfo *mh_parse_msg (const gchar *file,
122 FolderItem *item);
123 static void mh_remove_missing_folder_items (Folder *folder);
124 static gchar *mh_filename_from_utf8 (const gchar *path);
125 static gchar *mh_filename_to_utf8 (const gchar *path);
126 static void mh_scan_tree_recursive (FolderItem *item);
128 static gboolean mh_rename_folder_func (GNode *node,
129 gpointer data);
130 static gchar *mh_item_get_path (Folder *folder,
131 FolderItem *item);
133 static gboolean mh_scan_required (Folder *folder,
134 FolderItem *item);
135 static void mh_set_mtime (Folder *folder,
136 FolderItem *item);
137 static int mh_item_close (Folder *folder,
138 FolderItem *item);
139 #if 0
140 static gint mh_get_flags (Folder *folder, FolderItem *item,
141 MsgInfoList *msginfo_list, GHashTable *msgflags);
142 #endif
143 static void mh_write_sequences (FolderItem *item, gboolean remove_unseen);
145 static FolderClass mh_class;
147 FolderClass *mh_get_class(void)
149 if (mh_class.idstr == NULL) {
150 mh_class.type = F_MH;
151 mh_class.idstr = "mh";
152 mh_class.uistr = "MH";
153 mh_class.supports_server_search = FALSE;
155 /* Folder functions */
156 mh_class.new_folder = mh_folder_new;
157 mh_class.destroy_folder = mh_folder_destroy;
158 mh_class.set_xml = folder_local_set_xml;
159 mh_class.get_xml = folder_local_get_xml;
160 mh_class.scan_tree = mh_scan_tree;
161 mh_class.create_tree = mh_create_tree;
163 /* FolderItem functions */
164 mh_class.item_get_path = mh_item_get_path;
165 mh_class.create_folder = mh_create_folder;
166 mh_class.rename_folder = mh_rename_folder;
167 mh_class.remove_folder = mh_remove_folder;
168 mh_class.get_num_list = mh_get_num_list;
169 mh_class.scan_required = mh_scan_required;
170 mh_class.set_mtime = mh_set_mtime;
171 mh_class.close = mh_item_close;
172 mh_class.get_flags = NULL; /*mh_get_flags */;
174 /* Message functions */
175 mh_class.get_msginfo = mh_get_msginfo;
176 mh_class.fetch_msg = mh_fetch_msg;
177 mh_class.add_msg = mh_add_msg;
178 mh_class.add_msgs = mh_add_msgs;
179 mh_class.copy_msg = mh_copy_msg;
180 mh_class.copy_msgs = mh_copy_msgs;
181 mh_class.search_msgs = folder_item_search_msgs_local;
182 mh_class.remove_msg = mh_remove_msg;
183 mh_class.remove_msgs = mh_remove_msgs;
184 mh_class.remove_all_msg = mh_remove_all_msg;
185 mh_class.is_msg_changed = mh_is_msg_changed;
188 return &mh_class;
191 static Folder *mh_folder_new(const gchar *name, const gchar *path)
193 Folder *folder;
195 folder = (Folder *)g_new0(MHFolder, 1);
196 folder->klass = &mh_class;
197 mh_folder_init(folder, name, path);
199 return folder;
202 static void mh_folder_destroy(Folder *folder)
204 folder_local_folder_destroy(LOCAL_FOLDER(folder));
207 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
209 folder_local_folder_init(folder, name, path);
213 gboolean mh_scan_required(Folder *folder, FolderItem *item)
215 gchar *path;
216 GStatBuf s;
218 path = folder_item_get_path(item);
219 cm_return_val_if_fail(path != NULL, FALSE);
221 if (g_stat(path, &s) < 0) {
222 FILE_OP_ERROR(path, "stat");
223 g_free(path);
224 return FALSE;
227 if ((s.st_mtime > item->mtime) &&
228 (s.st_mtime - 3600 != item->mtime)) {
229 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
230 path,
231 (long int) s.st_mtime,
232 (long int) item->mtime);
233 g_free(path);
234 return TRUE;
237 debug_print("MH scan not required: %s (%ld <= %ld)\n",
238 path,
239 (long int) s.st_mtime,
240 (long int) item->mtime);
241 g_free(path);
242 return FALSE;
245 static void mh_get_last_num(Folder *folder, FolderItem *item)
247 gchar *path, *fullpath;
248 GDir *dp;
249 const gchar *d;
250 GError *error = NULL;
251 gint max = 0;
252 gint num;
254 cm_return_if_fail(item != NULL);
256 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path?item->path:"(null)");
258 path = folder_item_get_path(item);
259 cm_return_if_fail(path != NULL);
261 if ((dp = g_dir_open(path, 0, &error)) == NULL) {
262 g_warning("couldn't open directory '%s': %s (%d)",
263 path, error->message, error->code);
264 g_error_free(error);
265 g_free(path);
266 return;
269 while ((d = g_dir_read_name(dp)) != NULL) {
270 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, d, NULL);
271 if ((num = to_number(d)) > 0 &&
272 g_file_test(fullpath, G_FILE_TEST_IS_REGULAR)) {
273 if (max < num)
274 max = num;
276 g_free(fullpath);
278 if (num % 2000 == 0)
279 GTK_EVENTS_FLUSH();
281 g_dir_close(dp);
282 g_free(path);
284 debug_print("Last number in dir %s = %d\n", item->path?item->path:"(null)", max);
285 item->last_num = max;
288 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
291 gchar *path;
292 GDir *dp;
293 const gchar *d;
294 GError *error = NULL;
295 gint num, nummsgs = 0;
297 cm_return_val_if_fail(item != NULL, -1);
299 debug_print("mh_get_num_list(): Scanning %s ...\n", item->path?item->path:"(null)");
301 *old_uids_valid = TRUE;
303 path = folder_item_get_path(item);
304 cm_return_val_if_fail(path != NULL, -1);
306 if ((dp = g_dir_open(path, 0, &error)) == NULL) {
307 g_message("Couldn't open current directory: %s (%d).\n",
308 error->message, error->code);
309 g_error_free(error);
310 g_free(path);
311 return -1;
313 g_free(path);
315 while ((d = g_dir_read_name(dp)) != NULL) {
316 if ((num = to_number(d)) > 0) {
317 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
318 nummsgs++;
321 g_dir_close(dp);
323 mh_set_mtime(folder, item);
324 return nummsgs;
327 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
329 gchar *path;
330 gchar *file;
332 cm_return_val_if_fail(item != NULL, NULL);
333 cm_return_val_if_fail(num > 0, NULL);
335 path = folder_item_get_path(item);
336 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
338 if (!is_file_exist(file)) {
339 g_free(file);
340 g_free(path);
341 return NULL;
343 g_free(path);
344 return file;
347 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
349 MsgInfo *msginfo;
350 gchar *file;
352 cm_return_val_if_fail(item != NULL, NULL);
353 if (num <= 0)
354 return NULL;
356 file = mh_fetch_msg(folder, item, num);
357 if (!file) return NULL;
359 msginfo = mh_parse_msg(file, item);
360 if (msginfo)
361 msginfo->msgnum = num;
363 g_free(file);
365 return msginfo;
368 static gchar *mh_get_new_msg_filename(FolderItem *dest)
370 gchar *destfile;
371 gchar *destpath;
373 destpath = folder_item_get_path(dest);
374 cm_return_val_if_fail(destpath != NULL, NULL);
376 if (dest->last_num < 0) {
377 mh_get_last_num(dest->folder, dest);
378 if (dest->last_num < 0) return NULL;
381 if (!is_dir_exist(destpath))
382 make_dir_hier(destpath);
384 for (;;) {
385 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
386 dest->last_num + 1);
387 if (is_file_entry_exist(destfile)) {
388 dest->last_num++;
389 g_free(destfile);
390 } else
391 break;
394 g_free(destpath);
396 return destfile;
399 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
401 gint ret;
402 GSList file_list;
403 MsgFileInfo fileinfo;
405 cm_return_val_if_fail(file != NULL, -1);
407 fileinfo.msginfo = NULL;
408 fileinfo.file = (gchar *)file;
409 fileinfo.flags = flags;
410 file_list.data = &fileinfo;
411 file_list.next = NULL;
413 ret = mh_add_msgs(folder, dest, &file_list, NULL);
414 return ret;
417 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
418 GHashTable *relation)
420 gchar *destfile;
421 GSList *cur;
422 MsgFileInfo *fileinfo;
424 cm_return_val_if_fail(dest != NULL, -1);
425 cm_return_val_if_fail(file_list != NULL, -1);
427 if (dest->last_num < 0) {
428 mh_get_last_num(folder, dest);
429 if (dest->last_num < 0) return -1;
432 for (cur = file_list; cur != NULL; cur = cur->next) {
433 fileinfo = (MsgFileInfo *)cur->data;
435 destfile = mh_get_new_msg_filename(dest);
436 if (destfile == NULL) return -1;
438 #ifdef G_OS_UNIX
439 if (link(fileinfo->file, destfile) < 0) {
440 #endif
441 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
442 g_warning("can't copy message %s to %s",
443 fileinfo->file, destfile);
444 g_free(destfile);
445 return -1;
447 #ifdef G_OS_UNIX
449 #endif
451 if (relation != NULL)
452 g_hash_table_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
453 g_free(destfile);
454 dest->last_num++;
456 mh_write_sequences(dest, TRUE);
457 return dest->last_num;
460 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
462 GSList msglist;
464 cm_return_val_if_fail(msginfo != NULL, -1);
466 msglist.data = msginfo;
467 msglist.next = NULL;
469 return mh_copy_msgs(folder, dest, &msglist, NULL);
472 static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist,
473 GHashTable *relation)
475 gboolean dest_need_scan = FALSE;
476 gboolean src_need_scan = FALSE;
477 FolderItem *src = NULL;
478 gchar *srcfile;
479 gchar *destfile;
480 FolderItemPrefs *prefs;
481 MsgInfo *msginfo = NULL;
482 MsgInfoList *cur = NULL;
483 gint curnum = 0, total = 0;
484 gchar *srcpath = NULL;
485 gboolean full_fetch = FALSE;
486 time_t last_dest_mtime = (time_t)0;
487 time_t last_src_mtime = (time_t)0;
489 cm_return_val_if_fail(dest != NULL, -1);
490 cm_return_val_if_fail(msglist != NULL, -1);
492 msginfo = (MsgInfo *)msglist->data;
494 cm_return_val_if_fail(msginfo != NULL, -1);
496 if (msginfo->folder == dest) {
497 g_warning("the src folder is identical to the dest");
498 return -1;
501 if (msginfo->folder->folder != dest->folder)
502 full_fetch = TRUE;
504 if (FOLDER_TYPE(msginfo->folder->folder) == F_MH) {
505 src = msginfo->folder;
508 if (dest->last_num < 0) {
509 mh_get_last_num(folder, dest);
510 if (dest->last_num < 0) return -1;
513 prefs = dest->prefs;
515 srcpath = folder_item_get_path(msginfo->folder);
517 dest_need_scan = mh_scan_required(dest->folder, dest);
518 last_dest_mtime = dest->mtime;
520 if (src) {
521 src_need_scan = mh_scan_required(src->folder, src);
522 last_src_mtime = src->mtime;
525 total = g_slist_length(msglist);
526 if (total > 100) {
527 if (MSG_IS_MOVE(msginfo->flags))
528 statusbar_print_all(_("Moving messages..."));
529 else
530 statusbar_print_all(_("Copying messages..."));
532 for (cur = msglist; cur; cur = cur->next) {
533 msginfo = (MsgInfo *)cur->data;
534 if (!msginfo) {
535 goto err_reset_status;
537 if (!full_fetch) {
538 srcfile = g_strconcat(srcpath,
539 G_DIR_SEPARATOR_S,
540 itos(msginfo->msgnum), NULL);
541 } else {
542 srcfile = procmsg_get_message_file(msginfo);
544 if (!srcfile) {
545 goto err_reset_status;
547 destfile = mh_get_new_msg_filename(dest);
548 if (!destfile) {
549 g_free(srcfile);
550 goto err_reset_status;
553 if (total > 100) {
554 statusbar_progress_all(curnum, total, 100);
555 if (curnum % 100 == 0)
556 GTK_EVENTS_FLUSH();
557 curnum++;
560 debug_print("Copying message %s%c%d to %s ...\n",
561 msginfo->folder->path, G_DIR_SEPARATOR,
562 msginfo->msgnum, dest->path);
565 if (MSG_IS_MOVE(msginfo->flags)) {
566 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
567 if (move_file(srcfile, destfile, TRUE) < 0) {
568 FILE_OP_ERROR(srcfile, "move");
569 if (copy_file(srcfile, destfile, TRUE) < 0) {
570 FILE_OP_ERROR(srcfile, "copy");
571 g_free(srcfile);
572 g_free(destfile);
573 goto err_reset_status;
575 } else {
576 /* say unlinking's not necessary */
577 msginfo->flags.tmp_flags |= MSG_MOVE_DONE;
579 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
580 FILE_OP_ERROR(srcfile, "copy");
581 g_free(srcfile);
582 g_free(destfile);
583 goto err_reset_status;
585 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
586 if (chmod(destfile, prefs->folder_chmod) < 0)
587 FILE_OP_ERROR(destfile, "chmod");
589 if (relation) {
590 if (g_hash_table_lookup(relation, msginfo) != NULL)
591 g_warning("already in: %p", msginfo);
593 g_hash_table_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
595 g_free(srcfile);
596 g_free(destfile);
597 dest->last_num++;
600 g_free(srcpath);
601 mh_write_sequences(dest, TRUE);
603 if (dest->mtime == last_dest_mtime && !dest_need_scan) {
604 mh_set_mtime(folder, dest);
607 if (src && src->mtime == last_src_mtime && !src_need_scan) {
608 mh_set_mtime(folder, src);
611 if (total > 100) {
612 statusbar_progress_all(0,0,0);
613 statusbar_pop_all();
615 return dest->last_num;
616 err_reset_status:
617 g_free(srcpath);
618 mh_write_sequences(dest, TRUE);
619 if (total > 100) {
620 statusbar_progress_all(0,0,0);
621 statusbar_pop_all();
623 return -1;
627 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
629 gboolean need_scan = FALSE;
630 time_t last_mtime = (time_t)0;
631 gchar *file;
633 cm_return_val_if_fail(item != NULL, -1);
635 file = mh_fetch_msg(folder, item, num);
636 cm_return_val_if_fail(file != NULL, -1);
638 need_scan = mh_scan_required(folder, item);
639 last_mtime = item->mtime;
641 if (claws_unlink(file) < 0) {
642 FILE_OP_ERROR(file, "unlink");
643 g_free(file);
644 return -1;
647 if (item->mtime == last_mtime && !need_scan) {
648 mh_set_mtime(folder, item);
650 g_free(file);
651 return 0;
654 static gint mh_remove_msgs(Folder *folder, FolderItem *item,
655 MsgInfoList *msglist, GHashTable *relation)
657 gboolean need_scan = FALSE;
658 gchar *path, *file;
659 time_t last_mtime = (time_t)0;
660 MsgInfoList *cur;
661 gint total = 0, curnum = 0;
663 cm_return_val_if_fail(item != NULL, -1);
665 path = folder_item_get_path(item);
667 need_scan = mh_scan_required(folder, item);
668 last_mtime = item->mtime;
670 total = g_slist_length(msglist);
671 if (total > 100) {
672 statusbar_print_all(_("Deleting messages..."));
675 for (cur = msglist; cur; cur = cur->next) {
676 MsgInfo *msginfo = (MsgInfo *)cur->data;
677 if (msginfo == NULL)
678 continue;
679 if (MSG_IS_MOVE(msginfo->flags) && MSG_IS_MOVE_DONE(msginfo->flags)) {
680 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
681 continue;
683 if (total > 100) {
684 statusbar_progress_all(curnum, total, 100);
685 if (curnum % 100 == 0)
686 GTK_EVENTS_FLUSH();
687 curnum++;
690 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
691 if (file == NULL)
692 continue;
694 if (claws_unlink(file) < 0) {
695 g_free(file);
696 continue;
699 g_free(file);
702 if (total > 100) {
703 statusbar_progress_all(0,0,0);
704 statusbar_pop_all();
706 if (item->mtime == last_mtime && !need_scan) {
707 mh_set_mtime(folder, item);
710 g_free(path);
711 return 0;
714 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
716 gchar *path;
717 gint val;
719 cm_return_val_if_fail(item != NULL, -1);
721 path = folder_item_get_path(item);
722 cm_return_val_if_fail(path != NULL, -1);
723 val = remove_all_numbered_files(path);
724 g_free(path);
726 mh_write_sequences(item, TRUE);
728 return val;
731 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
732 MsgInfo *msginfo)
734 #ifdef G_OS_WIN32
735 GFile *f;
736 GFileInfo *fi;
737 GTimeVal tv;
738 GError *error = NULL;
739 #else
740 GStatBuf s;
741 int r;
742 #endif
743 gchar *path;
744 gchar *parent_path;
746 parent_path = folder_item_get_path(item);
747 path = g_strdup_printf("%s%c%d", parent_path,
748 G_DIR_SEPARATOR, msginfo->msgnum);
749 g_free(parent_path);
751 #ifdef G_OS_WIN32
752 f = g_file_new_for_path(path);
753 g_free(path);
754 fi = g_file_query_info(f, "standard::size,time::modified",
755 G_FILE_QUERY_INFO_NONE, NULL, &error);
756 if (error != NULL) {
757 g_warning(error->message);
758 g_error_free(error);
759 g_object_unref(f);
760 return TRUE;
763 g_file_info_get_modification_time(fi, &tv);
764 if (msginfo->size != g_file_info_get_size(fi) || (
765 (msginfo->mtime - tv.tv_sec != 0) &&
766 abs(msginfo->mtime - tv.tv_sec) != 3600)) {
767 g_error_free(error);
768 g_object_unref(f);
769 return TRUE;
771 #else
772 r = g_stat(path, &s);
773 g_free(path);
774 if (r < 0 ||
775 msginfo->size != s.st_size || (
776 (msginfo->mtime - s.st_mtime != 0) &&
777 (msginfo->mtime - s.st_mtime != 3600) &&
778 (msginfo->mtime - s.st_mtime != -3600))) {
779 return TRUE;
781 #endif
783 return FALSE;
786 static gint mh_scan_tree(Folder *folder)
788 FolderItem *item;
790 cm_return_val_if_fail(folder != NULL, -1);
792 if (!folder->node) {
793 item = folder_item_new(folder, folder->name, NULL);
794 item->folder = folder;
795 folder->node = item->node = g_node_new(item);
796 } else
797 item = FOLDER_ITEM(folder->node->data);
799 mh_create_tree(folder);
800 mh_remove_missing_folder_items(folder);
801 mh_scan_tree_recursive(item);
803 return 0;
806 #define MAKE_DIR_IF_NOT_EXIST(dir) \
808 if (!is_dir_exist(dir)) { \
809 if (is_file_exist(dir)) { \
810 g_warning("file '%s' already exists, " \
811 "can't create folder", dir); \
812 if (rootpath) \
813 g_free(rootpath); \
814 if (path) \
815 g_free(path); \
816 return -1; \
818 if (make_dir_hier(dir) < 0) { \
819 if (rootpath) \
820 g_free(rootpath); \
821 if (path) \
822 g_free(path); \
823 return -1; \
825 debug_print("Created dir '%s'\n", dir); \
829 static gint mh_create_tree(Folder *folder)
831 gchar *rootpath, *f, *path = NULL;
833 cm_return_val_if_fail(folder != NULL, -1);
835 rootpath = LOCAL_FOLDER(folder)->rootpath;
836 #ifdef G_OS_UNIX
837 if (*rootpath == '/') {
838 #elif defined G_OS_WIN32
839 if (g_ascii_isalpha(*rootpath) && !strncmp(rootpath + 1, ":\\", 2)) {
840 #endif
841 /* Folder path is absolute. */
842 rootpath = g_strdup(rootpath);
843 } else {
844 /* Folder path is relative, using mail base dir. */
845 rootpath = g_strconcat(get_mail_base_dir(), G_DIR_SEPARATOR_S,
846 rootpath, NULL);
849 MAKE_DIR_IF_NOT_EXIST(rootpath);
851 /* Create special directories as needed */
852 if (folder->inbox != NULL &&
853 folder->inbox->path != NULL)
854 f = folder->inbox->path;
855 else
856 f = INBOX_DIR;
857 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
858 MAKE_DIR_IF_NOT_EXIST(path);
859 g_free(path);
861 if (folder->outbox != NULL &&
862 folder->outbox->path != NULL)
863 f = folder->outbox->path;
864 else
865 f = OUTBOX_DIR;
866 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
867 MAKE_DIR_IF_NOT_EXIST(path);
868 g_free(path);
870 if (folder->draft != NULL &&
871 folder->draft->path != NULL)
872 f = folder->draft->path;
873 else
874 f = DRAFT_DIR;
875 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
876 MAKE_DIR_IF_NOT_EXIST(path);
877 g_free(path);
879 if (folder->queue != NULL &&
880 folder->queue->path != NULL)
881 f = folder->queue->path;
882 else
883 f = QUEUE_DIR;
884 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
885 MAKE_DIR_IF_NOT_EXIST(path);
886 g_free(path);
888 if (folder->trash != NULL &&
889 folder->trash->path != NULL)
890 f = folder->trash->path;
891 else
892 f = TRASH_DIR;
893 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
894 MAKE_DIR_IF_NOT_EXIST(path);
895 g_free(path);
897 g_free(rootpath);
898 return 0;
901 #undef MAKE_DIR_IF_NOT_EXIST
903 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
905 gchar *folder_path, *path;
906 gchar *real_path;
907 cm_return_val_if_fail(folder != NULL, NULL);
908 cm_return_val_if_fail(item != NULL, NULL);
910 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
911 cm_return_val_if_fail(folder_path != NULL, NULL);
913 /* FIXME: [W32] The code below does not correctly merge
914 relative filenames; there should be a function to handle
915 this. */
916 if ( !is_relative_filename (folder_path) ) {
917 if (item->path)
918 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
919 item->path, NULL);
920 else
921 path = g_strdup(folder_path);
922 } else {
923 if (item->path)
924 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
925 folder_path, G_DIR_SEPARATOR_S,
926 item->path, NULL);
927 else
928 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
929 folder_path, NULL);
931 g_free(folder_path);
932 real_path = mh_filename_from_utf8(path);
933 if (!is_dir_exist(real_path) && is_dir_exist(path)) {
934 /* mmh, older version did put utf8 filenames instead of
935 * the correct encoding */
936 if (g_rename(path, real_path) == 0)
937 folder_item_scan(item);
940 g_free(path);
941 return real_path;
944 static gboolean mh_renumber_msg(MsgInfo *info)
946 gchar *src, *dest;
947 gboolean result = FALSE;
948 guint num;
949 cm_return_val_if_fail(info != NULL, FALSE);
951 src = folder_item_fetch_msg(info->folder, info->msgnum);
952 dest = mh_get_new_msg_filename(info->folder);
953 num = info->folder->last_num + 1;
955 if (move_file(src, dest, FALSE) == 0) {
956 msgcache_remove_msg(info->folder->cache, info->msgnum);
957 info->msgnum = num;
958 msgcache_add_msg(info->folder->cache, info);
959 result = TRUE;
962 g_free(src);
963 g_free(dest);
965 return result;
968 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
969 const gchar *name)
971 gchar *path, *real_name;
972 gchar *fullpath;
973 FolderItem *new_item;
974 gchar *mh_sequences_filename;
975 FILE *mh_sequences_file;
977 cm_return_val_if_fail(folder != NULL, NULL);
978 cm_return_val_if_fail(parent != NULL, NULL);
979 cm_return_val_if_fail(name != NULL, NULL);
981 path = folder_item_get_path(parent);
982 if (!is_dir_exist(path))
983 if (make_dir_hier(path) != 0)
984 return NULL;
986 real_name = mh_filename_from_utf8(name);
987 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
988 g_free(real_name);
989 g_free(path);
991 if (to_number(name) > 0) {
992 MsgInfo *info = folder_item_get_msginfo(parent, to_number(name));
993 if (info != NULL) {
994 gboolean ok = mh_renumber_msg(info);
995 procmsg_msginfo_free(&info);
996 if (!ok) {
997 g_free(fullpath);
998 return NULL;
1003 if (make_dir(fullpath) < 0) {
1004 g_free(fullpath);
1005 return NULL;
1008 g_free(fullpath);
1010 if (parent->path)
1011 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
1012 NULL);
1013 else
1014 path = g_strdup(name);
1015 new_item = folder_item_new(folder, name, path);
1016 folder_item_append(parent, new_item);
1018 g_free(path);
1020 path = folder_item_get_path(new_item);
1021 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
1022 ".mh_sequences", NULL);
1023 if ((mh_sequences_file = claws_fopen(mh_sequences_filename, "a+b")) != NULL) {
1024 claws_fclose(mh_sequences_file);
1026 g_free(mh_sequences_filename);
1027 g_free(path);
1029 return new_item;
1032 static gint mh_rename_folder(Folder *folder, FolderItem *item,
1033 const gchar *name)
1035 gchar *real_name;
1036 gchar *oldpath;
1037 gchar *dirname;
1038 gchar *newpath, *utf8newpath;
1039 gchar *paths[2];
1041 cm_return_val_if_fail(folder != NULL, -1);
1042 cm_return_val_if_fail(item != NULL, -1);
1043 cm_return_val_if_fail(item->path != NULL, -1);
1044 cm_return_val_if_fail(name != NULL, -1);
1046 oldpath = folder_item_get_path(item);
1047 if (!is_dir_exist(oldpath))
1048 make_dir_hier(oldpath);
1050 dirname = g_path_get_dirname(oldpath);
1051 real_name = mh_filename_from_utf8(name);
1052 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
1053 g_free(real_name);
1054 g_free(dirname);
1056 if (g_rename(oldpath, newpath) < 0) {
1057 FILE_OP_ERROR(oldpath, "rename");
1058 g_free(oldpath);
1059 g_free(newpath);
1060 return -1;
1063 g_free(oldpath);
1064 g_free(newpath);
1066 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
1067 dirname = g_path_get_dirname(item->path);
1068 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
1069 name, NULL);
1070 g_free(dirname);
1071 } else
1072 utf8newpath = g_strdup(name);
1074 g_free(item->name);
1075 item->name = g_strdup(name);
1077 paths[0] = g_strdup(item->path);
1078 paths[1] = utf8newpath;
1079 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1080 mh_rename_folder_func, paths);
1082 g_free(paths[0]);
1083 g_free(paths[1]);
1084 return 0;
1087 static gint mh_remove_folder(Folder *folder, FolderItem *item)
1089 gchar *path;
1090 gint ret;
1092 cm_return_val_if_fail(folder != NULL, -1);
1093 cm_return_val_if_fail(item != NULL, -1);
1094 cm_return_val_if_fail(item->path != NULL, -1);
1096 path = folder_item_get_path(item);
1097 if ((ret = remove_dir_recursive(path)) < 0) {
1098 g_warning("can't remove directory '%s'", path);
1099 g_free(path);
1100 return ret;
1103 g_free(path);
1104 folder_item_remove(item);
1105 return 0;
1108 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1110 MsgInfo *msginfo;
1111 MsgFlags flags;
1113 cm_return_val_if_fail(item != NULL, NULL);
1114 cm_return_val_if_fail(file != NULL, NULL);
1116 flags.perm_flags = MSG_NEW|MSG_UNREAD;
1117 flags.tmp_flags = 0;
1119 if (folder_has_parent_of_type(item, F_QUEUE)) {
1120 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1121 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
1122 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1125 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1126 if (!msginfo) return NULL;
1128 msginfo->msgnum = atoi(file);
1129 msginfo->folder = item;
1131 return msginfo;
1134 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
1136 FolderItem *item;
1137 gchar *path;
1139 cm_return_val_if_fail(node->data != NULL, FALSE);
1141 if (G_NODE_IS_ROOT(node))
1142 return FALSE;
1144 item = FOLDER_ITEM(node->data);
1146 path = folder_item_get_path(item);
1147 if (!is_dir_exist(path)) {
1148 debug_print("folder '%s' not found. removing...\n", path?path:"(null)");
1149 folder_item_remove(item);
1151 g_free(path);
1153 return FALSE;
1156 static void mh_remove_missing_folder_items(Folder *folder)
1158 cm_return_if_fail(folder != NULL);
1160 debug_print("searching missing folders...\n");
1162 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1163 mh_remove_missing_folder_items_func, folder);
1166 static void mh_scan_tree_recursive(FolderItem *item)
1168 Folder *folder;
1169 GDir *dir;
1170 const gchar *dir_name;
1171 gchar *entry, *utf8entry, *utf8name, *path;
1172 gint n_msg = 0;
1173 GError *error = NULL;
1175 cm_return_if_fail(item != NULL);
1176 cm_return_if_fail(item->folder != NULL);
1178 folder = item->folder;
1180 path = folder_item_get_path(item);
1181 debug_print("mh_scan_tree_recursive() opening '%s'\n", path);
1182 dir = g_dir_open(path, 0, &error);
1183 if (!dir) {
1184 g_warning("failed to open directory '%s': %s (%d)",
1185 path, error->message, error->code);
1186 g_error_free(error);
1187 g_free(path);
1188 return;
1191 debug_print("scanning %s ...\n",
1192 item->path ? item->path
1193 : LOCAL_FOLDER(item->folder)->rootpath);
1194 if (folder->ui_func)
1195 folder->ui_func(folder, item, folder->ui_func_data);
1197 while ((dir_name = g_dir_read_name(dir)) != NULL) {
1198 if (dir_name[0] == '.') continue;
1200 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir_name, NULL);
1202 utf8name = mh_filename_to_utf8(dir_name);
1203 if (item->path)
1204 utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1205 utf8name, NULL);
1206 else
1207 utf8entry = g_strdup(utf8name);
1209 if (g_file_test(entry, G_FILE_TEST_IS_DIR)) {
1210 FolderItem *new_item = NULL;
1211 GNode *node;
1213 node = item->node;
1214 for (node = node->children; node != NULL; node = node->next) {
1215 FolderItem *cur_item = FOLDER_ITEM(node->data);
1216 gchar *curpath = folder_item_get_path(cur_item);
1217 if (!g_strcmp0(curpath, entry)) {
1218 new_item = cur_item;
1219 g_free(curpath);
1220 break;
1222 g_free(curpath);
1224 if (!new_item) {
1225 debug_print("new folder '%s' found.\n", entry);
1226 new_item = folder_item_new(folder, utf8name, utf8entry);
1227 folder_item_append(item, new_item);
1230 if (!item->path) {
1231 if (!folder->inbox &&
1232 !strcmp(dir_name, INBOX_DIR)) {
1233 new_item->stype = F_INBOX;
1234 folder->inbox = new_item;
1235 } else if (!folder->outbox &&
1236 !strcmp(dir_name, OUTBOX_DIR)) {
1237 new_item->stype = F_OUTBOX;
1238 folder->outbox = new_item;
1239 } else if (!folder->draft &&
1240 !strcmp(dir_name, DRAFT_DIR)) {
1241 new_item->stype = F_DRAFT;
1242 folder->draft = new_item;
1243 } else if (!folder->queue &&
1244 !strcmp(dir_name, QUEUE_DIR)) {
1245 new_item->stype = F_QUEUE;
1246 folder->queue = new_item;
1247 } else if (!folder->trash &&
1248 !strcmp(dir_name, TRASH_DIR)) {
1249 new_item->stype = F_TRASH;
1250 folder->trash = new_item;
1254 mh_scan_tree_recursive(new_item);
1255 } else if (to_number(dir_name) > 0) n_msg++;
1257 g_free(entry);
1258 g_free(utf8entry);
1259 g_free(utf8name);
1262 g_dir_close(dir);
1263 g_free(path);
1265 mh_set_mtime(folder, item);
1268 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1270 FolderItem *item = node->data;
1271 gchar **paths = data;
1272 const gchar *oldpath = paths[0];
1273 const gchar *newpath = paths[1];
1274 gchar *base;
1275 gchar *new_itempath;
1276 gint oldpathlen;
1278 oldpathlen = strlen(oldpath);
1279 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1280 g_warning("path doesn't match: %s, %s", oldpath, item->path);
1281 return TRUE;
1284 base = item->path + oldpathlen;
1285 while (*base == G_DIR_SEPARATOR) base++;
1286 if (*base == '\0')
1287 new_itempath = g_strdup(newpath);
1288 else
1289 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1290 NULL);
1291 g_free(item->path);
1292 item->path = new_itempath;
1294 return FALSE;
1297 static gchar *mh_filename_from_utf8(const gchar *path)
1299 gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1301 if (!real_path) {
1302 g_warning("mh_filename_from_utf8: failed to convert character set");
1303 real_path = g_strdup(path);
1306 return real_path;
1309 static gchar *mh_filename_to_utf8(const gchar *path)
1311 gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1312 if (!utf8path) {
1313 g_warning("mh_filename_to_utf8: failed to convert character set");
1314 utf8path = g_strdup(path);
1317 return utf8path;
1320 static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1322 MsgInfo *msginfo_a = (MsgInfo *) a;
1323 MsgInfo *msginfo_b = (MsgInfo *) b;
1325 return (msginfo_a->msgnum - msginfo_b->msgnum);
1328 static gchar *get_unseen_seq_name(void)
1330 static gchar *seq_name = NULL;
1331 if (!seq_name) {
1332 gchar buf[BUFFSIZE];
1333 gchar *tmp;
1334 gchar *profile_path = g_strconcat(
1335 get_home_dir(), G_DIR_SEPARATOR_S,
1336 ".mh_profile", NULL);
1337 FILE *fp = claws_fopen(profile_path, "r");
1338 g_free(profile_path);
1339 if (fp) {
1340 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
1341 if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
1342 gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
1343 while (*seq_tmp == ' ')
1344 seq_tmp++;
1345 seq_name = g_strdup(seq_tmp);
1346 seq_name = strretchomp(seq_name);
1347 break;
1350 claws_fclose(fp);
1352 if (!seq_name)
1353 seq_name = g_strdup("unseen");
1354 tmp = g_strdup_printf("%s:", seq_name);
1355 g_free(seq_name);
1356 seq_name = tmp;
1358 return seq_name;
1361 static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
1363 gchar *mh_sequences_old, *mh_sequences_new;
1364 FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
1365 gchar buf[BUFFSIZE];
1366 gchar *path = NULL;
1367 gboolean err = FALSE;
1368 START_TIMING("");
1370 if (!item)
1371 return;
1373 path = folder_item_get_path(item);
1375 mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
1376 ".mh_sequences", NULL);
1377 mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
1378 ".mh_sequences.new", NULL);
1379 if ((mh_sequences_new_fp = claws_fopen(mh_sequences_new, "w+b")) != NULL) {
1380 GSList *msglist = folder_item_get_msg_list(item);
1381 GSList *cur;
1382 MsgInfo *info = NULL;
1383 gint start = -1, end = -1;
1384 gchar *sequence = g_strdup("");
1385 gint seq_len = 0;
1386 msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
1387 cur = msglist;
1389 /* write the unseen sequence if we don't have to scrap it */
1390 if (!remove_unseen) do {
1391 info = (MsgInfo *)(cur ? cur->data:NULL);
1392 if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
1393 if (start < 0)
1394 start = end = info->msgnum;
1395 else
1396 end = info->msgnum;
1397 } else {
1398 if (start > 0 && end > 0) {
1399 gchar tmp[32];
1400 gint tmp_len = 0;
1401 if (start != end)
1402 snprintf(tmp, 31, " %d-%d", start, end);
1403 else
1404 snprintf(tmp, 31, " %d", start);
1406 tmp_len = strlen(tmp);
1407 sequence = g_realloc(sequence, seq_len+tmp_len+1);
1408 strcpy(sequence+seq_len, tmp);
1409 seq_len += tmp_len;
1411 start = end = -1;
1414 cur = cur ? cur->next:NULL;
1415 } while (cur || (start > 0 && end > 0));
1416 if (sequence && *sequence) {
1417 if (fprintf(mh_sequences_new_fp, "%s%s\n",
1418 get_unseen_seq_name(), sequence) < 0)
1419 err = TRUE;
1420 else
1421 debug_print("wrote unseen sequence: '%s%s'\n",
1422 get_unseen_seq_name(), sequence);
1424 /* rewrite the rest of the file */
1425 if ((mh_sequences_old_fp = claws_fopen(mh_sequences_old, "r+b")) != NULL) {
1426 while (claws_fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
1427 if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
1428 if (fprintf(mh_sequences_new_fp, "%s", buf) < 0) {
1429 err = TRUE;
1430 break;
1433 claws_fclose(mh_sequences_old_fp);
1436 if (claws_safe_fclose(mh_sequences_new_fp) == EOF)
1437 err = TRUE;
1439 if (!err) {
1440 if (g_rename(mh_sequences_new, mh_sequences_old) < 0)
1441 FILE_OP_ERROR(mh_sequences_new, "rename");
1443 g_free(sequence);
1444 procmsg_msg_list_free(msglist);
1446 g_free(mh_sequences_old);
1447 g_free(mh_sequences_new);
1448 g_free(path);
1450 END_TIMING();
1453 static int mh_item_close(Folder *folder, FolderItem *item)
1455 time_t last_mtime = (time_t)0;
1456 gboolean need_scan = mh_scan_required(item->folder, item);
1457 last_mtime = item->mtime;
1459 mh_write_sequences(item, FALSE);
1461 if (item->mtime == last_mtime && !need_scan) {
1462 mh_set_mtime(folder, item);
1465 return 0;
1468 static void mh_set_mtime(Folder *folder, FolderItem *item)
1470 GStatBuf s;
1471 gchar *path = folder_item_get_path(item);
1473 cm_return_if_fail(path != NULL);
1475 if (g_stat(path, &s) < 0) {
1476 FILE_OP_ERROR(path, "stat");
1477 g_free(path);
1478 return;
1481 item->mtime = s.st_mtime;
1482 debug_print("MH: forced mtime of %s to %"CM_TIME_FORMAT"\n", item->name?item->name:"(null)", item->mtime);
1483 g_free(path);