sync with 0.7.4cvs49
[claws.git] / src / mh.c
index b9996810f6a34443649599d82f659862d2629ceb..bd9f632b8ff0997525c0947b14936b920fc6a8c5 100644 (file)
--- a/src/mh.c
+++ b/src/mh.c
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2001 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2002 Hiroyuki Yamamoto
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include "procheader.h"
 #include "utils.h"
 
+static void    mh_folder_init                  (Folder         *folder,
+                                                const gchar    *name,
+                                                const gchar    *path);
+
+static gchar   *mh_get_new_msg_filename                (FolderItem     *dest);
+
 static GSList  *mh_get_uncached_msgs           (GHashTable     *msg_table,
                                                 FolderItem     *item);
 static MsgInfo *mh_parse_msg                   (const gchar    *file,
                                                 FolderItem     *item);
-static void    mh_scan_tree_recursive          (FolderItem     *item);
+static void    mh_scan_tree_recursive          (FolderItem     *item, 
+                                                GHashTable *pptable);
 
 static gboolean mh_rename_folder_func          (GNode          *node,
                                                 gpointer        data);
 
 
+Folder *mh_folder_new(const gchar *name, const gchar *path)
+{
+       Folder *folder;
+
+       folder = (Folder *)g_new0(MHFolder, 1);
+       mh_folder_init(folder, name, path);
+
+       return folder;
+}
+
+void mh_folder_destroy(MHFolder *folder)
+{
+       folder_local_folder_destroy(LOCAL_FOLDER(folder));
+}
+
+static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
+{
+       folder_local_folder_init(folder, name, path);
+
+       folder->type = F_MH;
+
+       folder->get_msg_list        = mh_get_msg_list;
+       folder->fetch_msg           = mh_fetch_msg;
+       folder->add_msg             = mh_add_msg;
+       folder->move_msg            = mh_move_msg;
+       folder->move_msgs_with_dest = mh_move_msgs_with_dest;
+       folder->copy_msg            = mh_copy_msg;
+       folder->copy_msgs_with_dest = mh_copy_msgs_with_dest;
+       folder->remove_msg          = mh_remove_msg;
+       folder->remove_all_msg      = mh_remove_all_msg;
+       folder->is_msg_changed      = mh_is_msg_changed;
+       folder->scan                = mh_scan_folder;
+       folder->scan_tree           = mh_scan_tree;
+       folder->create_tree         = mh_create_tree;
+       folder->create_folder       = mh_create_folder;
+       folder->rename_folder       = mh_rename_folder;
+       folder->remove_folder       = mh_remove_folder;
+}
+
 GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
 {
        GSList *mlist;
@@ -72,11 +118,14 @@ GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
        if (stat(path, &s) < 0) {
                FILE_OP_ERROR(path, "stat");
        } else {
-               if (item->mtime == s.st_mtime) {
+               time_t mtime;
+
+               mtime = MAX(s.st_mtime, s.st_ctime);
+               if (item->mtime == mtime) {
                        debug_print("Folder is not modified.\n");
                        scan_new = FALSE;
                } else
-                       item->mtime = s.st_mtime;
+                       item->mtime = mtime;
        }
        g_free(path);
 
@@ -130,10 +179,51 @@ gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
        return file;
 }
 
+static gchar *mh_get_new_msg_filename(FolderItem *dest)
+{
+       gchar *destfile;
+       gchar *destpath;
+
+       destpath = folder_item_get_path(dest);
+       g_return_val_if_fail(destpath != NULL, NULL);
+
+       if (!is_dir_exist(destpath))
+               make_dir_hier(destpath);
+
+       for (;;) {
+               destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
+                                          dest->last_num + 1);
+               if (is_file_entry_exist(destfile)) {
+                       dest->last_num++;
+                       g_free(destfile);
+               } else
+                       break;
+       }
+
+       g_free(destpath);
+
+       return destfile;
+}
+
+#define SET_DEST_MSG_FLAGS(fp, dest, msginfo) \
+{ \
+       MsgInfo newmsginfo; \
+ \
+       newmsginfo.msgnum = dest->last_num; \
+       newmsginfo.flags = msginfo->flags; \
+       if (dest->stype == F_OUTBOX || \
+           dest->stype == F_QUEUE  || \
+           dest->stype == F_DRAFT  || \
+           dest->stype == F_TRASH) \
+               MSG_UNSET_PERM_FLAGS(newmsginfo.flags, \
+                                    MSG_NEW|MSG_UNREAD|MSG_DELETED); \
+ \
+       procmsg_write_flags(&newmsginfo, fp); \
+}
+
 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
                gboolean remove_source)
 {
-       gchar *destpath;
        gchar *destfile;
 
        g_return_val_if_fail(dest != NULL, -1);
@@ -144,13 +234,8 @@ gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
                if (dest->last_num < 0) return -1;
        }
 
-       destpath = folder_item_get_path(dest);
-       g_return_val_if_fail(destpath != NULL, -1);
-       if (!is_dir_exist(destpath))
-               make_dir_hier(destpath);
-
-       destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
-                                  dest->last_num + 1);
+       destfile = mh_get_new_msg_filename(dest);
+       g_return_val_if_fail(destfile != NULL, -1);
 
        if (link(file, destfile) < 0) {
                if (copy_file(file, destfile) < 0) {
@@ -171,7 +256,7 @@ gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
        return dest->last_num;
 }
 
-gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
+static gint mh_do_move(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
 {
        gchar *destdir;
        gchar *srcfile;
@@ -195,22 +280,22 @@ gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
 
        prefs = dest->prefs;
 
-       destdir = folder_item_get_path(dest);
-       if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
-               g_warning(_("Can't open mark file.\n"));
+       destfile = mh_get_new_msg_filename(dest);
+       g_return_val_if_fail(destfile != NULL, -1);
 
        debug_print(_("Moving message %s%c%d to %s ...\n"),
                    msginfo->folder->path, G_DIR_SEPARATOR,
                    msginfo->msgnum, dest->path);
-       srcfile = procmsg_get_message_file_path(msginfo);
-       destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
-                                  dest->last_num + 1);
-       g_free(destdir);
+       srcfile = procmsg_get_message_file(msginfo);
+
+       destfile = mh_get_new_msg_filename(dest);
+       if(!destfile) return -1;
+
+       srcfile = procmsg_get_message_file(msginfo);
 
        if (move_file(srcfile, destfile) < 0) {
                g_free(srcfile);
                g_free(destfile);
-               if (fp) fclose(fp);
                return -1;
        }
 
@@ -228,20 +313,11 @@ gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
        g_free(destfile);
        dest->last_num++;
 
-       if (fp) {
-               MsgInfo newmsginfo;
-
-               newmsginfo.msgnum = dest->last_num;
-               newmsginfo.flags = msginfo->flags;
-               if (dest->stype == F_OUTBOX ||
-                   dest->stype == F_QUEUE  ||
-                   dest->stype == F_DRAFT  ||
-                   dest->stype == F_TRASH)
-                       MSG_UNSET_PERM_FLAGS(newmsginfo.flags,
-                                            MSG_NEW|MSG_UNREAD|MSG_DELETED);
-
-               procmsg_write_flags(&newmsginfo, fp);
-
+       destdir = folder_item_get_path(dest);
+       if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
+               g_warning(_("Can't open mark file.\n"));
+       else {
+               SET_DEST_MSG_FLAGS(fp, dest, msginfo);
                if (filemode) {
 #if HAVE_FCHMOD
                        fchmod(fileno(fp), filemode);
@@ -258,11 +334,51 @@ gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
 
                fclose(fp);
        }
+       g_free(destdir);
 
        return dest->last_num;
 }
 
-gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
+gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
+{
+       gchar *srcfile;
+       gint ret = 0;
+       g_return_val_if_fail(folder != NULL, -1);
+       g_return_val_if_fail(dest != NULL, -1);
+       g_return_val_if_fail(msginfo != NULL, -1);
+       g_return_val_if_fail(msginfo->folder != NULL, -1);
+       if (folder == msginfo->folder->folder)
+               return mh_do_move(folder, dest, msginfo);
+       srcfile = procmsg_get_message_file(msginfo);
+       if (!srcfile) return -1;
+       ret = mh_add_msg(folder, dest, srcfile, FALSE);
+       g_free(srcfile);
+       if (ret != -1) {
+               gchar *destdir;
+               FILE *fp;
+               destdir = folder_item_get_path(dest);
+               if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
+                       g_warning(_("Can't open mark file.\n"));
+               else {
+                       SET_DEST_MSG_FLAGS(fp, dest, msginfo);
+                       fclose(fp);
+               }
+               g_free(destdir);
+               ret = folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
+       }
+       return ret;
+}
+
+static gint mh_do_move_msgs_with_dest(Folder *folder, FolderItem *dest,
+                                     GSList *msglist)
 {
        gchar *destdir;
        gchar *srcfile;
@@ -297,9 +413,11 @@ gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
                            msginfo->folder->path, G_DIR_SEPARATOR,
                            msginfo->msgnum, dest->path);
 
-               srcfile = procmsg_get_message_file_path(msginfo);
-               destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
-                                          dest->last_num + 1);
+               destfile = mh_get_new_msg_filename(dest);
+               if (!destfile) break;
+               srcfile = procmsg_get_message_file(msginfo);
+               destfile = mh_get_new_msg_filename(dest);
+               if(!destfile) return -1;
 
                if (move_file(srcfile, destfile) < 0) {
                        g_free(srcfile);
@@ -312,19 +430,7 @@ gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
                dest->last_num++;
 
                if (fp) {
-                       MsgInfo newmsginfo;
-
-                       newmsginfo.msgnum = dest->last_num;
-                       newmsginfo.flags = msginfo->flags;
-                       if (dest->stype == F_OUTBOX ||
-                           dest->stype == F_QUEUE  ||
-                           dest->stype == F_DRAFT  ||
-                           dest->stype == F_TRASH)
-                               MSG_UNSET_PERM_FLAGS
-                                       (newmsginfo.flags,
-                                        MSG_NEW|MSG_UNREAD|MSG_DELETED);
-
-                       procmsg_write_flags(&newmsginfo, fp);
+                       SET_DEST_MSG_FLAGS(fp, dest, msginfo);
                }
        }
 
@@ -334,6 +440,25 @@ gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
        return dest->last_num;
 }
 
+gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
+{
+       MsgInfo *msginfo;
+       GSList *cur;
+       gint ret = 0;
+
+       msginfo = (MsgInfo *)msglist->data;
+       if (folder == msginfo->folder->folder)
+               return mh_do_move_msgs_with_dest(folder, dest, msglist);
+
+       for (cur = msglist; cur != NULL; cur = cur->next) {
+               msginfo = (MsgInfo *)cur->data;
+               ret = mh_move_msg(folder, dest, msginfo);
+               if (ret == -1) break;
+       }
+
+       return ret;
+}
+
 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
 {
        gchar *destdir;
@@ -358,36 +483,19 @@ gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
 
        prefs = dest->prefs;
 
-       destdir = folder_item_get_path(dest);
-       if (!is_dir_exist(destdir))
-               make_dir_hier(destdir);
-
-       if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
-               g_warning(_("Can't open mark file.\n"));
+       destfile = mh_get_new_msg_filename(dest);
+       g_return_val_if_fail(destfile != NULL, -1);
 
        debug_print(_("Copying message %s%c%d to %s ...\n"),
                    msginfo->folder->path, G_DIR_SEPARATOR,
                    msginfo->msgnum, dest->path);
-       srcfile = procmsg_get_message_file_path(msginfo);
-       destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
-                                  dest->last_num + 1);
-       g_free(destdir);
 
-       dest->op_count--;
-
-       if (is_file_exist(destfile)) {
-               g_warning(_("%s already exists."), destfile);
-               g_free(srcfile);
-               g_free(destfile);
-               if (fp) fclose(fp);
-               return -1;
-       }
+       srcfile = procmsg_get_message_file(msginfo);
 
        if (copy_file(srcfile, destfile) < 0) {
                FILE_OP_ERROR(srcfile, "copy");
                g_free(srcfile);
                g_free(destfile);
-               if (fp) fclose(fp);
                return -1;
        }
 
@@ -405,18 +513,11 @@ gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
        g_free(destfile);
        dest->last_num++;
 
-       if (fp) {
-               MsgInfo newmsginfo;
-
-               newmsginfo.msgnum = dest->last_num;
-               newmsginfo.flags = msginfo->flags;
-               if (dest->stype == F_OUTBOX ||
-                   dest->stype == F_QUEUE  ||
-                   dest->stype == F_DRAFT  ||
-                   dest->stype == F_TRASH)
-                       MSG_UNSET_PERM_FLAGS(newmsginfo.flags,
-                                            MSG_NEW|MSG_UNREAD|MSG_DELETED);
-               procmsg_write_flags(&newmsginfo, fp);
+       destdir = folder_item_get_path(dest);
+       if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
+               g_warning(_("Can't open mark file.\n"));
+       else {
+               SET_DEST_MSG_FLAGS(fp, dest, msginfo);
 
                if (filemode) {
 #if HAVE_FCHMOD
@@ -434,6 +535,7 @@ gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
 
                fclose(fp);
        }
+       g_free(destdir);
 
        return dest->last_num;
 }
@@ -460,8 +562,6 @@ gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
        num = folder->add_msg(folder, dest, filename, FALSE);
 
        destdir = folder_item_get_path(dest);
-       if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
-               g_warning(_("Can't open mark file.\n"));
 
        if (fp) {
                MsgInfo newmsginfo;
@@ -518,16 +618,9 @@ gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
                            msginfo->folder->path, G_DIR_SEPARATOR,
                            msginfo->msgnum, dest->path);
 
-               srcfile = procmsg_get_message_file_path(msginfo);
-               destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
-                                          dest->last_num + 1);
-
-               if (is_file_exist(destfile)) {
-                       g_warning(_("%s already exists."), destfile);
-                       g_free(srcfile);
-                       g_free(destfile);
-                       break;
-               }
+               destfile = mh_get_new_msg_filename(dest);
+               if (!destfile) break;
+               srcfile = procmsg_get_message_file(msginfo);
 
                if (copy_file(srcfile, destfile) < 0) {
                        FILE_OP_ERROR(srcfile, "copy");
@@ -541,18 +634,7 @@ gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
                dest->last_num++;
 
                if (fp) {
-                       MsgInfo newmsginfo;
-
-                       newmsginfo.msgnum = dest->last_num;
-                       newmsginfo.flags = msginfo->flags;
-                       if (dest->stype == F_OUTBOX ||
-                           dest->stype == F_QUEUE  ||
-                           dest->stype == F_DRAFT  ||
-                           dest->stype == F_TRASH)
-                               MSG_UNSET_PERM_FLAGS
-                                       (newmsginfo.flags,
-                                        MSG_NEW|MSG_UNREAD|MSG_DELETED);
-                       procmsg_write_flags(&newmsginfo, fp);
+                       SET_DEST_MSG_FLAGS(fp, dest, msginfo);
                }
        }
 
@@ -608,7 +690,7 @@ gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
        return FALSE;
 }
 
-void mh_scan_folder(Folder *folder, FolderItem *item)
+gint mh_scan_folder(Folder *folder, FolderItem *item)
 {
        gchar *path;
        DIR *dp;
@@ -618,21 +700,21 @@ void mh_scan_folder(Folder *folder, FolderItem *item)
        gint num;
        gint n_msg = 0;
 
-       g_return_if_fail(item != NULL);
+       g_return_val_if_fail(item != NULL, -1);
 
        debug_print("mh_scan_folder(): Scanning %s ...\n", item->path);
 
        path = folder_item_get_path(item);
-       g_return_if_fail(path != NULL);
+       g_return_val_if_fail(path != NULL, -1);
        if (change_dir(path) < 0) {
                g_free(path);
-               return;
+               return -1;
        }
        g_free(path);
 
        if ((dp = opendir(".")) == NULL) {
                FILE_OP_ERROR(item->path, "opendir");
-               return;
+               return -1;
        }
 
        if (folder->ui_func)
@@ -653,9 +735,9 @@ void mh_scan_folder(Folder *folder, FolderItem *item)
        if (n_msg == 0)
                item->new = item->unread = item->total = 0;
        else {
-               gint new, unread, total;
+               gint new, unread, total, min, max;
 
-               procmsg_get_mark_sum(".", &new, &unread, &total);
+               procmsg_get_mark_sum(".", &new, &unread, &total, &min, &max, 0);
                if (n_msg > total) {
                        new += n_msg - total;
                        unread += n_msg - total;
@@ -667,15 +749,22 @@ void mh_scan_folder(Folder *folder, FolderItem *item)
 
        debug_print(_("Last number in dir %s = %d\n"), item->path, max);
        item->last_num = max;
+
+       return 0;
 }
 
 void mh_scan_tree(Folder *folder)
 {
        FolderItem *item;
        gchar *rootpath;
+       GHashTable *pptable;
 
        g_return_if_fail(folder != NULL);
 
+       pptable = folder_persist_prefs_new(folder);
+
+       prefs_scoring_clear();
+       prefs_filtering_clear();
        folder_tree_destroy(folder);
        item = folder_item_new(folder->name, NULL);
        item->folder = folder;
@@ -688,7 +777,12 @@ void mh_scan_tree(Folder *folder)
        }
        g_free(rootpath);
 
-       mh_scan_tree_recursive(item);
+       mh_create_tree(folder);
+       mh_scan_tree_recursive(item, pptable);
+       
+       folder_persist_prefs_free(pptable);
+
+       prefs_matcher_read_config();
 }
 
 #define MAKE_DIR_IF_NOT_EXIST(dir) \
@@ -958,7 +1052,7 @@ static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
                MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
        }
 
-       msginfo = procheader_parse(file, flags, FALSE);
+       msginfo = procheader_parse(file, flags, FALSE, FALSE);
        if (!msginfo) return NULL;
 
        msginfo->msgnum = atoi(file);
@@ -1000,7 +1094,7 @@ static gboolean mh_is_maildir(const gchar *path)
               mh_is_maildir_one(path, "tmp");
 }
 
-static void mh_scan_tree_recursive(FolderItem *item)
+static void mh_scan_tree_recursive(FolderItem *item, GHashTable *pptable)
 {
        DIR *dp;
        struct dirent *d;
@@ -1050,24 +1144,25 @@ static void mh_scan_tree_recursive(FolderItem *item)
                        new_item = folder_item_new(d->d_name, entry);
                        folder_item_append(item, new_item);
                        if (!item->path) {
-                               if (!strcmp(d->d_name, "inbox")) {
+                               if (!strcmp(d->d_name, INBOX_DIR)) {
                                        new_item->stype = F_INBOX;
                                        item->folder->inbox = new_item;
-                               } else if (!strcmp(d->d_name, "outbox")) {
+                               } else if (!strcmp(d->d_name, OUTBOX_DIR)) {
                                        new_item->stype = F_OUTBOX;
                                        item->folder->outbox = new_item;
-                               } else if (!strcmp(d->d_name, "draft")) {
+                               } else if (!strcmp(d->d_name, DRAFT_DIR)) {
                                        new_item->stype = F_DRAFT;
                                        item->folder->draft = new_item;
-                               } else if (!strcmp(d->d_name, "queue")) {
+                               } else if (!strcmp(d->d_name, QUEUE_DIR)) {
                                        new_item->stype = F_QUEUE;
                                        item->folder->queue = new_item;
-                               } else if (!strcmp(d->d_name, "trash")) {
+                               } else if (!strcmp(d->d_name, TRASH_DIR)) {
                                        new_item->stype = F_TRASH;
                                        item->folder->trash = new_item;
                                }
                        }
-                       mh_scan_tree_recursive(new_item);
+                       folder_item_restore_persist_prefs(new_item, pptable);
+                       mh_scan_tree_recursive(new_item, pptable);
                } else if (to_number(d->d_name) != -1) n_msg++;
 
                g_free(entry);
@@ -1076,9 +1171,10 @@ static void mh_scan_tree_recursive(FolderItem *item)
        closedir(dp);
 
        if (item->path) {
-               gint new, unread, total;
+               gint new, unread, total, min, max;
 
-               procmsg_get_mark_sum(item->path, &new, &unread, &total);
+               procmsg_get_mark_sum(item->path, &new, &unread, &total,
+                                    &min, &max, 0);
                if (n_msg > total) {
                        new += n_msg - total;
                        unread += n_msg - total;