0.8.10claws45
[claws.git] / src / procmsg.c
index df2ee1396f2f17f06255edce3cb68f0a507f12d3..2d51337501d76d205a4dd015fd62bb1bec35da05 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2002 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2003 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
@@ -39,6 +39,8 @@
 #endif
 #include "alertpanel.h"
 #include "news.h"
+#include "imap.h"
+#include "hooks.h"
 
 typedef struct _FlagInfo       FlagInfo;
 
@@ -48,16 +50,8 @@ struct _FlagInfo
        MsgFlags flags;
 };
 
-static void mark_sum_func                      (gpointer        key,
-                                                gpointer        value,
-                                                gpointer        data);
-
 static GHashTable *procmsg_read_mark_file      (const gchar    *folder);
-static gint procmsg_cmp_msgnum                 (gconstpointer   a,
-                                                gconstpointer   b);
-static gint procmsg_cmp_flag_msgnum            (gconstpointer   a,
-                                                gconstpointer   b);
-
+void   procmsg_msginfo_write_flags             (MsgInfo        *msginfo);
 
 GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
 {
@@ -142,7 +136,7 @@ static gint procmsg_read_cache_data_str(FILE *fp, gchar **str)
                ret = -1;
 
        if (ret < 0)
-               g_warning(_("Cache data is corrupted\n"));
+               g_warning("Cache data is corrupted\n");
 
        return ret;
 }
@@ -158,7 +152,7 @@ static gint procmsg_read_cache_data_str(FILE *fp, gchar **str)
 #define READ_CACHE_DATA_INT(n, fp) \
 { \
        if (fread(&n, sizeof(n), 1, fp) != 1) { \
-               g_warning(_("Cache data is corrupted\n")); \
+               g_warning("Cache data is corrupted\n"); \
                procmsg_msginfo_free(msginfo); \
                break; \
        } \
@@ -208,19 +202,19 @@ GSList *procmsg_read_cache(FolderItem *item, gboolean scan_file)
        }
        cache_file = folder_item_get_cache_file(item);
        if ((fp = fopen(cache_file, "rb")) == NULL) {
-               debug_print(_("\tNo cache file\n"));
+               debug_print("\tNo cache file\n");
                g_free(cache_file);
                return NULL;
        }
        setvbuf(fp, file_buf, _IOFBF, sizeof(file_buf));
        g_free(cache_file);
 
-       debug_print(_("\tReading summary cache...\n"));
+       debug_print("\tReading summary cache...\n");
 
        /* compare cache version */
        if (fread(&ver, sizeof(ver), 1, fp) != 1 ||
            CACHE_VERSION != ver) {
-               debug_print(_("Cache version is different. Discarding it.\n"));
+               debug_print("Cache version is different. Discarding it.\n");
                fclose(fp);
                return NULL;
        }
@@ -268,7 +262,7 @@ GSList *procmsg_read_cache(FolderItem *item, gboolean scan_file)
        }
 
        fclose(fp);
-       debug_print(_("done.\n"));
+       debug_print("done.\n");
 
        return mlist;
 }
@@ -290,7 +284,7 @@ void procmsg_set_flags(GSList *mlist, FolderItem *item)
        g_return_if_fail(item != NULL);
        g_return_if_fail(item->folder != NULL);
 
-       debug_print(_("\tMarking the messages...\n"));
+       debug_print("\tMarking the messages...\n");
 
        markdir = folder_item_get_path(item);
        if (!is_dir_exist(markdir))
@@ -332,22 +326,20 @@ void procmsg_set_flags(GSList *mlist, FolderItem *item)
 
        item->last_num = lastnum;
 
-       debug_print(_("done.\n"));
+       debug_print("done.\n");
        if (newmsg)
-               debug_print(_("\t%d new message(s)\n"), newmsg);
+               debug_print("\t%d new message(s)\n", newmsg);
 
        hash_free_value_mem(mark_table);
        g_hash_table_destroy(mark_table);
 }
 
-gint procmsg_get_last_num_in_cache(GSList *mlist)
+gint procmsg_get_last_num_in_msg_list(GSList *mlist)
 {
        GSList *cur;
        MsgInfo *msginfo;
        gint last = 0;
 
-       if (mlist == NULL) return 0;
-
        for (cur = mlist; cur != NULL; cur = cur->next) {
                msginfo = (MsgInfo *)cur->data;
                if (msginfo && msginfo->msgnum > last)
@@ -440,7 +432,7 @@ void procmsg_add_flags(FolderItem *item, gint num, MsgFlags flags)
        g_return_if_fail(path != NULL);
 
        if ((fp = procmsg_open_mark_file(path, TRUE)) == NULL) {
-               g_warning(_("can't open mark file\n"));
+               g_warning("can't open mark file\n");
                g_free(path);
                return;
        }
@@ -462,51 +454,6 @@ struct MarkSum {
        gint first;
 };
 
-static void mark_sum_func(gpointer key, gpointer value, gpointer data)
-{
-       MsgFlags *flags = value;
-       gint num = GPOINTER_TO_INT(key);
-       struct MarkSum *marksum = data;
-
-       if (marksum->first <= num) {
-               if (MSG_IS_NEW(*flags) && !MSG_IS_IGNORE_THREAD(*flags)) (*marksum->new)++;
-               if (MSG_IS_UNREAD(*flags) && !MSG_IS_IGNORE_THREAD(*flags)) (*marksum->unread)++;
-               if (num > *marksum->max) *marksum->max = num;
-               if (num < *marksum->min || *marksum->min == 0) *marksum->min = num;
-               (*marksum->total)++;
-       }
-
-       g_free(flags);
-}
-
-#if 0 /* NEW CACHE DOES NOT ALLOW ACCESS TO THE MARK FILE */
-void procmsg_get_mark_sum(const gchar *folder,
-                         gint *new, gint *unread, gint *total,
-                         gint *min, gint *max,
-                         gint first)
-{
-       GHashTable *mark_table;
-       struct MarkSum marksum;
-
-       *new = *unread = *total = *min = *max = 0;
-       marksum.new    = new;
-       marksum.unread = unread;
-       marksum.total  = total;
-       marksum.min    = min;
-       marksum.max    = max;
-       marksum.first  = first;
-
-       mark_table = procmsg_read_mark_file(folder);
-
-       if (mark_table) {
-               g_hash_table_foreach(mark_table, mark_sum_func, &marksum);
-               g_hash_table_destroy(mark_table);
-       }
-       debug_print("mark->new = %d, mark->unread = %d, mark->total = %d\n",
-                   *(marksum.new), *(marksum.unread), *(marksum.total));
-}
-#endif
-
 static GHashTable *procmsg_read_mark_file(const gchar *folder)
 {
        FILE *fp;
@@ -523,10 +470,14 @@ static GHashTable *procmsg_read_mark_file(const gchar *folder)
        while (fread(&num, sizeof(num), 1, fp) == 1) {
                if (fread(&perm_flags, sizeof(perm_flags), 1, fp) != 1) break;
 
+               flags = g_hash_table_lookup(mark_table, GUINT_TO_POINTER(num));
+               if (flags != NULL)
+                       g_free(flags);
+
                flags = g_new0(MsgFlags, 1);
                flags->perm_flags = perm_flags;
     
-               if(!MSG_IS_REALLY_DELETED(*flags)) {
+               if (!MSG_IS_REALLY_DELETED(*flags)) {
                        g_hash_table_insert(mark_table, GUINT_TO_POINTER(num), flags);
                } else {
                        g_hash_table_remove(mark_table, GUINT_TO_POINTER(num));
@@ -546,10 +497,10 @@ FILE *procmsg_open_mark_file(const gchar *folder, gboolean append)
        markfile = g_strconcat(folder, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
 
        if ((fp = fopen(markfile, "rb")) == NULL)
-               debug_print(_("Mark file not found.\n"));
+               debug_print("Mark file not found.\n");
        else if (fread(&ver, sizeof(ver), 1, fp) != 1 || MARK_VERSION != ver) {
-               debug_print(_("Mark version is different (%d != %d). "
-                             "Discarding it.\n"), ver, MARK_VERSION);
+               debug_print("Mark version is different (%d != %d). "
+                             "Discarding it.\n", ver, MARK_VERSION);
                fclose(fp);
                fp = NULL;
        }
@@ -564,12 +515,12 @@ FILE *procmsg_open_mark_file(const gchar *folder, gboolean append)
                /* reopen with append mode */
                fclose(fp);
                if ((fp = fopen(markfile, "ab")) == NULL)
-                       g_warning(_("Can't open mark file with append mode.\n"));
+                       g_warning("Can't open mark file with append mode.\n");
        } else {
                /* open with overwrite mode if mark file doesn't exist or
                   version is different */
                if ((fp = fopen(markfile, "wb")) == NULL)
-                       g_warning(_("Can't open mark file with write mode.\n"));
+                       g_warning("Can't open mark file with write mode.\n");
                else {
                        ver = MARK_VERSION;
                        WRITE_CACHE_DATA_INT(ver, fp);
@@ -580,6 +531,15 @@ FILE *procmsg_open_mark_file(const gchar *folder, gboolean append)
        return fp;
 }
 
+static gboolean procmsg_ignore_node(GNode *node, gpointer data)
+{
+       MsgInfo *msginfo = (MsgInfo *)node->data;
+       
+       procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
+
+       return FALSE;
+}
+
 /* return the reversed thread tree */
 GNode *procmsg_get_thread_tree(GSList *mlist)
 {
@@ -589,6 +549,7 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
        MsgInfo *msginfo;
        const gchar *msgid;
        const gchar *subject;
+       GNode *found_subject;
 
        root = g_node_new(NULL);
        msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
@@ -603,7 +564,7 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
                        if (parent == NULL) {
                                parent = root;
                        } else {
-                               if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags)) {
+                               if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                                        procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
                                }
                        }
@@ -615,11 +576,23 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
                    g_hash_table_lookup(msgid_table, msgid) == NULL)
                        g_hash_table_insert(msgid_table, (gchar *)msgid, node);
 
-               subject = msginfo->subject;
-               if (subject_table_lookup(subject_table,
-                                        (gchar *) subject) == NULL)
-                       subject_table_insert(subject_table, (gchar *)subject,
-                                            node);
+               if (prefs_common.thread_by_subject) {
+                       subject = msginfo->subject;
+                       found_subject = subject_table_lookup(subject_table,
+                                                            (gchar *) subject);
+                       if (found_subject == NULL)
+                               subject_table_insert(subject_table, (gchar *) subject,
+                                                    node);
+                       else {
+                               /* replace if msg in table is older than current one 
+                                * can add here more stuff. */
+                               if ( ((MsgInfo*)(found_subject->data))->date_t >
+                                    ((MsgInfo*)(node->data))->date_t )  {
+                                       subject_table_remove(subject_table, (gchar *) subject);
+                                       subject_table_insert(subject_table, (gchar *) subject, node);
+                               }       
+                       }
+               }
        }
 
        /* complete the unfinished threads */
@@ -634,8 +607,8 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
                        g_node_insert_before
                                (parent, parent->children, node);
                        /* CLAWS: ignore thread */
-                       if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags)) {
-                               procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
+                       if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
+                               g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
                        }
                }
                node = next;
@@ -645,34 +618,36 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
         * threaded by subject line. we need to handle this in a special way to prevent
         * circular reference from a node that has already been threaded by IN-REPLY-TO
         * but is also in the subject line hash table */
-       for (node = root->children; node != NULL; ) {
-               next = node->next;
-               msginfo = (MsgInfo *) node->data;
-               parent = NULL;
-               if (subject_is_reply(msginfo->subject)) {
-                       parent = subject_table_lookup(subject_table,
-                                                     msginfo->subject);
-                       /* the node may already be threaded by IN-REPLY-TO,
-                          so go up in the tree to find the parent node */
-                       if (parent != NULL) {
-                               if (g_node_is_ancestor(node, parent))
-                                       parent = NULL;
-                               if (parent == node)
-                                       parent = NULL;
-                       }
+       if (prefs_common.thread_by_subject) {
+               for (node = root->children; node != NULL; ) {
+                       next = node->next;
+                       msginfo = (MsgInfo *) node->data;
+                       parent = NULL;
+                       if (subject_is_reply(msginfo->subject)) {
+                               parent = subject_table_lookup(subject_table,
+                                                             msginfo->subject);
+                               /* the node may already be threaded by IN-REPLY-TO,
+                                  so go up in the tree to find the parent node */
+                               if (parent != NULL) {
+                                       if (g_node_is_ancestor(node, parent))
+                                               parent = NULL;
+                                       if (parent == node)
+                                               parent = NULL;
+                               }
 
-                       if (parent) {
-                               g_node_unlink(node);
-                               g_node_append(parent, node);
-                               /* CLAWS: ignore thread */
-                               if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags)) {
-                                       procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
+                               if (parent) {
+                                       g_node_unlink(node);
+                                       g_node_append(parent, node);
+                                       /* CLAWS: ignore thread */
+                                       if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
+                                               g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
+                                       }
                                }
-                       }
-               }                                       
-               node = next;
-       }               
-               
+                       }                                       
+                       node = next;
+               }       
+       }
+       
        g_hash_table_destroy(subject_table);
        g_hash_table_destroy(msgid_table);
 
@@ -684,10 +659,11 @@ void procmsg_move_messages(GSList *mlist)
        GSList *cur, *movelist = NULL;
        MsgInfo *msginfo;
        FolderItem *dest = NULL;
-       GHashTable *hash;
 
        if (!mlist) return;
 
+       folder_item_update_freeze();
+
        for (cur = mlist; cur != NULL; cur = cur->next) {
                msginfo = (MsgInfo *)cur->data;
                if (!dest) {
@@ -702,12 +678,15 @@ void procmsg_move_messages(GSList *mlist)
                        dest = msginfo->to_folder;
                        movelist = g_slist_append(movelist, msginfo);
                }
+               procmsg_msginfo_set_to_folder(msginfo, NULL);
        }
 
        if (movelist) {
                folder_item_move_msgs_with_dest(dest, movelist);
                g_slist_free(movelist);
        }
+
+       folder_item_update_thaw();
 }
 
 void procmsg_copy_messages(GSList *mlist)
@@ -715,18 +694,10 @@ void procmsg_copy_messages(GSList *mlist)
        GSList *cur, *copylist = NULL;
        MsgInfo *msginfo;
        FolderItem *dest = NULL;
-       GHashTable *hash;
 
        if (!mlist) return;
 
-       /* 
-       
-       Horrible: Scanning 2 times for every copy!
-
-       hash = procmsg_to_folder_hash_table_create(mlist);
-       folder_item_scan_foreach(hash);
-       g_hash_table_destroy(hash);
-       */
+       folder_item_update_freeze();
 
        for (cur = mlist; cur != NULL; cur = cur->next) {
                msginfo = (MsgInfo *)cur->data;
@@ -742,12 +713,15 @@ void procmsg_copy_messages(GSList *mlist)
                        dest = msginfo->to_folder;
                        copylist = g_slist_append(copylist, msginfo);
                }
+               procmsg_msginfo_set_to_folder(msginfo, NULL);
        }
 
        if (copylist) {
                folder_item_copy_msgs_with_dest(dest, copylist);
                g_slist_free(copylist);
        }
+
+       folder_item_update_thaw();
 }
 
 gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
@@ -776,7 +750,7 @@ gchar *procmsg_get_message_file(MsgInfo *msginfo)
 
        filename = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum);
        if (!filename)
-               g_warning(_("can't fetch message %d\n"), msginfo->msgnum);
+               g_warning("can't fetch message %d\n", msginfo->msgnum);
 
        return filename;
 }
@@ -805,7 +779,7 @@ FILE *procmsg_open_message(MsgInfo *msginfo)
 
        g_free(file);
 
-       if (MSG_IS_QUEUED(msginfo->flags)) {
+       if (MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags)) {
                gchar buf[BUFFSIZE];
 
                while (fgets(buf, sizeof(buf), fp) != NULL)
@@ -839,8 +813,7 @@ FILE *procmsg_open_message_decrypted(MsgInfo *msginfo, MimeInfo **mimeinfo)
        }
 
        if (MSG_IS_ENCRYPTED(msginfo->flags) &&
-           !msginfo->plaintext_file &&
-           !msginfo->decryption_failed) {
+           (!msginfo->plaintext_file || msginfo->decryption_failed)) {
                rfc2015_decrypt_message(msginfo, mimeinfo_, fp);
                if (msginfo->plaintext_file &&
                    !msginfo->decryption_failed) {
@@ -890,7 +863,6 @@ void procmsg_empty_trash(void)
 
 gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
 {
-       gint i;
        gint ret = 0;
        GSList *list, *elem;
 
@@ -902,7 +874,7 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
        list = folder_item_get_msg_list(queue);
 
 
-       for(elem = list; elem != NULL; elem = elem->next) {
+       for (elem = list; elem != NULL; elem = elem->next) {
                gchar *file;
                MsgInfo *msginfo;
                
@@ -911,7 +883,7 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
                file = folder_item_fetch_msg(queue, msginfo->msgnum);
                if (file) {
                        if (procmsg_send_message_queue(file) < 0) {
-                               g_warning(_("Sending queued message %d failed.\n"), msginfo->msgnum);
+                               g_warning("Sending queued message %d failed.\n", msginfo->msgnum);
                                ret = -1;
                        } else {
                        /* CLAWS: 
@@ -931,20 +903,39 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
                procmsg_msginfo_free(msginfo);
        }
 
-       folderview_update_item(queue, FALSE);
-
        return ret;
 }
 
+gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
+{
+       FILE *fp, *outfp;
+       gchar buf[BUFFSIZE];
+       
+       if ((fp = fopen(in, "rb")) == NULL) {
+               FILE_OP_ERROR(in, "fopen");
+               return -1;
+       }
+       if ((outfp = fopen(out, "wb")) == NULL) {
+               FILE_OP_ERROR(out, "fopen");
+               fclose(fp);
+               return -1;
+       }
+       while (fgets(buf, sizeof(buf), fp) != NULL)
+               if (buf[0] == '\r' || buf[0] == '\n') break;
+       while (fgets(buf, sizeof(buf), fp) != NULL)
+               fputs(buf, outfp);
+       fclose(outfp);
+       fclose(fp);
+       return 0;
+
+}
 gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
                            gboolean is_queued)
 {
        gint num;
-       FILE *fp;
-       MsgFlags flag = {0, 0};
        MsgInfo *msginfo;
 
-       debug_print(_("saving sent message...\n"));
+       debug_print("saving sent message...\n");
 
        if (!outbox)
                outbox = folder_get_default_outbox();
@@ -953,43 +944,33 @@ gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
        /* remove queueing headers */
        if (is_queued) {
                gchar tmp[MAXPATHLEN + 1];
-               gchar buf[BUFFSIZE];
-               FILE *outfp;
 
                g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.out.%08x",
                           get_rc_dir(), G_DIR_SEPARATOR, (guint)random());
-               if ((fp = fopen(file, "rb")) == NULL) {
-                       FILE_OP_ERROR(file, "fopen");
+               
+               if (procmsg_remove_special_headers(file, tmp) !=0)
                        return -1;
-               }
-               if ((outfp = fopen(tmp, "wb")) == NULL) {
-                       FILE_OP_ERROR(tmp, "fopen");
-                       fclose(fp);
+
+               folder_item_scan(outbox);
+               if ((num = folder_item_add_msg(outbox, tmp, TRUE)) < 0) {
+                       g_warning("can't save message\n");
+                       unlink(tmp);
                        return -1;
                }
-               while (fgets(buf, sizeof(buf), fp) != NULL)
-                       if (buf[0] == '\r' || buf[0] == '\n') break;
-               while (fgets(buf, sizeof(buf), fp) != NULL)
-                       fputs(buf, outfp);
-               fclose(outfp);
-               fclose(fp);
-               Xstrdup_a(file, tmp, return -1);
-       }
-
-       if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
-               g_warning(_("can't save message\n"));
-               if(is_queued) {
-                       unlink(file);
+       } else {
+               folder_item_scan(outbox);
+               if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
+                       g_warning("can't save message\n");
+                       return -1;
                }
                return -1;
        }
-       msginfo = folder_item_fetch_msginfo(outbox, num);
-       procmsg_msginfo_unset_flags(msginfo, ~0, ~0);
-       procmsg_msginfo_free(msginfo);
-
-       if(is_queued) {
-               unlink(file);
+       msginfo = folder_item_get_msginfo(outbox, num);
+       if (msginfo != NULL) {
+           procmsg_msginfo_unset_flags(msginfo, ~0, 0);
+           procmsg_msginfo_free(msginfo);
        }
+       folder_item_update(outbox, TRUE);
 
        return 0;
 }
@@ -1006,7 +987,7 @@ void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
        g_return_if_fail(msginfo);
 
        if ((tmpfp = procmime_get_first_text_content(msginfo)) == NULL) {
-               g_warning(_("Can't get text part\n"));
+               g_warning("Can't get text part\n");
                return;
        }
 
@@ -1040,7 +1021,7 @@ void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline)
                g_snprintf(buf, sizeof(buf) - 1, cmdline, prtmp);
        else {
                if (cmdline)
-                       g_warning(_("Print command line is invalid: `%s'\n"),
+                       g_warning("Print command line is invalid: `%s'\n",
                                  cmdline);
                g_snprintf(buf, sizeof(buf) - 1, def_cmd, prtmp);
        }
@@ -1115,14 +1096,51 @@ MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
        return newmsginfo;
 }
 
+MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
+{
+       MsgInfo *full_msginfo;
+       gchar *file;
+
+       if (msginfo == NULL) return NULL;
+
+       file = procmsg_get_message_file(msginfo);
+       if (!file) {
+               g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n");
+               return NULL;
+       }
+
+       full_msginfo = procheader_parse_file(file, msginfo->flags, TRUE, FALSE);
+       g_free(file);
+       if (!full_msginfo) return NULL;
+
+       full_msginfo->msgnum = msginfo->msgnum;
+       full_msginfo->size = msginfo->size;
+       full_msginfo->mtime = msginfo->mtime;
+       full_msginfo->folder = msginfo->folder;
+#if USE_GPGME
+       full_msginfo->plaintext_file = g_strdup(msginfo->plaintext_file);
+       full_msginfo->decryption_failed = msginfo->decryption_failed;
+#endif
+       procmsg_msginfo_set_to_folder(full_msginfo, msginfo->to_folder);
+
+       return full_msginfo;
+}
+
 void procmsg_msginfo_free(MsgInfo *msginfo)
 {
        if (msginfo == NULL) return;
 
        msginfo->refcnt--;
-       if(msginfo->refcnt > 0)
+       if (msginfo->refcnt > 0)
                return;
 
+       debug_print("freeing msginfo %d is %s\n", msginfo->msgnum, msginfo->folder ? msginfo->folder->path : "(nil)");
+
+       if (msginfo->to_folder) {
+               msginfo->to_folder->op_count--;
+               folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
+       }
+
        g_free(msginfo->fromspace);
        g_free(msginfo->references);
        g_free(msginfo->returnreceiptto);
@@ -1149,49 +1167,38 @@ guint procmsg_msginfo_memusage(MsgInfo *msginfo)
        guint memusage = 0;
        
        memusage += sizeof(MsgInfo);
-       if(msginfo->fromname)
+       if (msginfo->fromname)
                memusage += strlen(msginfo->fromname);
-       if(msginfo->date)
+       if (msginfo->date)
                memusage += strlen(msginfo->date);
-       if(msginfo->from)
+       if (msginfo->from)
                memusage += strlen(msginfo->from);
-       if(msginfo->to)
+       if (msginfo->to)
                memusage += strlen(msginfo->to);
-       if(msginfo->cc)
+       if (msginfo->cc)
                memusage += strlen(msginfo->cc);
-       if(msginfo->newsgroups)
+       if (msginfo->newsgroups)
                memusage += strlen(msginfo->newsgroups);
-       if(msginfo->subject)
+       if (msginfo->subject)
                memusage += strlen(msginfo->subject);
-       if(msginfo->msgid)
+       if (msginfo->msgid)
                memusage += strlen(msginfo->msgid);
-       if(msginfo->inreplyto)
+       if (msginfo->inreplyto)
                memusage += strlen(msginfo->inreplyto);
-       if(msginfo->xface)
+       if (msginfo->xface)
                memusage += strlen(msginfo->xface);
-       if(msginfo->dispositionnotificationto)
+       if (msginfo->dispositionnotificationto)
                memusage += strlen(msginfo->dispositionnotificationto);
-       if(msginfo->returnreceiptto)
+       if (msginfo->returnreceiptto)
                memusage += strlen(msginfo->returnreceiptto);
-       if(msginfo->references)
+       if (msginfo->references)
                memusage += strlen(msginfo->references);
-       if(msginfo->fromspace)
+       if (msginfo->fromspace)
                memusage += strlen(msginfo->fromspace);
 
        return memusage;
 }
 
-static gint procmsg_cmp_msgnum(gconstpointer a, gconstpointer b)
-{
-       const MsgInfo *msginfo = a;
-       const guint msgnum = GPOINTER_TO_UINT(b);
-
-       if (!msginfo)
-               return -1;
-
-       return msginfo->msgnum - msgnum;
-}
-
 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
 {
        const MsgInfo *msginfo1 = a;
@@ -1205,17 +1212,6 @@ gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
        return msginfo1->msgnum - msginfo2->msgnum;
 }
 
-static gint procmsg_cmp_flag_msgnum(gconstpointer a, gconstpointer b)
-{
-       const FlagInfo *finfo = a;
-       const guint msgnum = GPOINTER_TO_UINT(b);
-
-       if (!finfo)
-               return -1;
-
-       return finfo->msgnum - msgnum;
-}
-
 enum
 {
        Q_SENDER           = 0,
@@ -1224,7 +1220,9 @@ enum
        Q_NEWSGROUPS       = 3,
        Q_MAIL_ACCOUNT_ID  = 4,
        Q_NEWS_ACCOUNT_ID  = 5,
-       Q_SAVE_COPY_FOLDER = 6
+       Q_SAVE_COPY_FOLDER = 6,
+       Q_REPLY_MESSAGE_ID = 7,
+       Q_FWD_MESSAGE_ID   = 8
 };
 
 gint procmsg_send_message_queue(const gchar *file)
@@ -1236,6 +1234,8 @@ gint procmsg_send_message_queue(const gchar *file)
                                       {"MAID:", NULL, FALSE},
                                       {"NAID:", NULL, FALSE},
                                       {"SCF:",  NULL, FALSE},
+                                      {"RMID:", NULL, FALSE},
+                                      {"FMID:", NULL, FALSE},
                                       {NULL,    NULL, FALSE}};
        FILE *fp;
        gint filepos;
@@ -1245,6 +1245,8 @@ gint procmsg_send_message_queue(const gchar *file)
        GSList *to_list = NULL;
        GSList *newsgroup_list = NULL;
        gchar *savecopyfolder = NULL;
+       gchar *replymessageid = NULL;
+       gchar *fwdmessageid = NULL;
        gchar buf[BUFFSIZE];
        gint hnum;
        PrefsAccount *mailac = NULL, *newsac = NULL;
@@ -1283,15 +1285,20 @@ gint procmsg_send_message_queue(const gchar *file)
                case Q_SAVE_COPY_FOLDER:
                        if (!savecopyfolder) savecopyfolder = g_strdup(p);
                        break;
+               case Q_REPLY_MESSAGE_ID:
+                       if (!replymessageid) replymessageid = g_strdup(p);
+                       break;
+               case Q_FWD_MESSAGE_ID:
+                       if (!fwdmessageid) fwdmessageid = g_strdup(p);
+                       break;
                }
        }
        filepos = ftell(fp);
 
-       fseek(fp, filepos, SEEK_SET);
        if (to_list) {
-               debug_print(_("Sending message by mail\n"));
-               if(!from) {
-                       g_warning(_("Queued message header is broken.\n"));
+               debug_print("Sending message by mail\n");
+               if (!from) {
+                       g_warning("Queued message header is broken.\n");
                        mailval = -1;
                } else if (mailac && mailac->use_mail_command &&
                           mailac->mail_command && (* mailac->mail_command)) {
@@ -1304,8 +1311,8 @@ gint procmsg_send_message_queue(const gchar *file)
                        if (!mailac) {
                                mailac = account_find_from_smtp_server(from, smtpserver);
                                if (!mailac) {
-                                       g_warning(_("Account not found. "
-                                                   "Using current account...\n"));
+                                       g_warning("Account not found. "
+                                                   "Using current account...\n");
                                        mailac = cur_account;
                                }
                        }
@@ -1315,7 +1322,7 @@ gint procmsg_send_message_queue(const gchar *file)
                        else {
                                PrefsAccount tmp_ac;
 
-                               g_warning(_("Account not found.\n"));
+                               g_warning("Account not found.\n");
 
                                memset(&tmp_ac, 0, sizeof(PrefsAccount));
                                tmp_ac.address = from;
@@ -1326,11 +1333,11 @@ gint procmsg_send_message_queue(const gchar *file)
                }
                if (mailval < 0) {
                        if (!local)
-                               alertpanel_error(
+                               alertpanel_error_log(
                                        _("Error occurred while sending the message to `%s'."),
                                        mailac ? mailac->smtp_server : smtpserver);
                        else
-                               alertpanel_error(
+                               alertpanel_error_log(
                                        _("Error occurred while sending the message with command `%s'."),
                                        (mailac && mailac->use_mail_command && 
                                         mailac->mail_command && (*mailac->mail_command)) ? 
@@ -1338,7 +1345,8 @@ gint procmsg_send_message_queue(const gchar *file)
                }
        }
 
-       if(newsgroup_list && (newsval == 0)) {
+       fseek(fp, filepos, SEEK_SET);
+       if (newsgroup_list && (newsval == 0)) {
                Folder *folder;
                gchar *tmp = NULL;
                FILE *tmpfp;
@@ -1353,7 +1361,7 @@ gint procmsg_send_message_queue(const gchar *file)
                } else {
                        if (change_file_mode_rw(tmpfp, tmp) < 0) {
                                FILE_OP_ERROR(tmp, "chmod");
-                               g_warning(_("can't change file mode\n"));
+                               g_warning("can't change file mode\n");
                        }
 
                        while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) {
@@ -1365,8 +1373,8 @@ gint procmsg_send_message_queue(const gchar *file)
                        }
                        fclose(tmpfp);
 
-                       if(newsval == 0) {
-                               debug_print(_("Sending message by news\n"));
+                       if (newsval == 0) {
+                               debug_print("Sending message by news\n");
 
                                folder = FOLDER(newsac->folder);
 
@@ -1393,15 +1401,56 @@ gint procmsg_send_message_queue(const gchar *file)
        if (mailval == 0 && newsval == 0 && savecopyfolder) {
                FolderItem *outbox;
 
-               debug_print(_("saving sent message...\n"));
+               debug_print("saving sent message...\n");
 
                outbox = folder_find_item_from_identifier(savecopyfolder);
-               if(!outbox)
+               if (!outbox)
                        outbox = folder_get_default_outbox();
 
                procmsg_save_to_outbox(outbox, file, TRUE);
        }
 
+       if (replymessageid != NULL || fwdmessageid != NULL) {
+               gchar **tokens;
+               FolderItem *item;
+               
+               if (replymessageid != NULL)
+                       tokens = g_strsplit(replymessageid, "\x7f", 0);
+               else
+                       tokens = g_strsplit(fwdmessageid, "\x7f", 0);
+               item = folder_find_item_from_identifier(tokens[0]);
+               if (item != NULL) {
+                       MsgInfo *msginfo;
+                       
+                       msginfo = folder_item_get_msginfo(item, atoi(tokens[1]));
+                       if ((msginfo != NULL) && (strcmp(msginfo->msgid, tokens[2]) != 0)) {
+                               procmsg_msginfo_free(msginfo);
+                               msginfo = NULL;
+                       }
+                       
+                       if (msginfo == NULL) {
+                               msginfo = folder_item_get_msginfo_by_msgid(item, tokens[2]);
+                       }
+                       
+                       if (msginfo != NULL) {
+                               if (replymessageid != NULL) {
+                                       procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
+                                       procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
+                               } 
+                               else {
+                                       procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED, 0);
+                                       procmsg_msginfo_set_flags(msginfo, MSG_FORWARDED, 0);
+                               }
+                               procmsg_msginfo_free(msginfo);
+                       }
+               }
+               g_strfreev(tokens);
+       }
+
+       g_free(savecopyfolder);
+       g_free(replymessageid);
+       g_free(fwdmessageid);
+       
        return (newsval != 0 ? newsval : mailval);
 }
 
@@ -1415,92 +1464,132 @@ msginfo->folder->folder->change_flags(msginfo->folder->folder, \
 
 void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
 {
-       gboolean changed = FALSE;
-       FolderItem *item = msginfo->folder;
+       FolderItem *item;
+       MsgInfoUpdate msginfo_update;
 
-       debug_print(_("Setting flags for message %d in folder %s\n"), msginfo->msgnum, item->path);
+       g_return_if_fail(msginfo != NULL);
+       item = msginfo->folder;
+       g_return_if_fail(item != NULL);
+       
+       debug_print("Setting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
 
        /* if new flag is set */
-       if((perm_flags & MSG_NEW) && !MSG_IS_NEW(msginfo->flags) &&
+       if ((perm_flags & MSG_NEW) && !MSG_IS_NEW(msginfo->flags) &&
           !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                item->new++;
-               changed = TRUE;
        }
 
        /* if unread flag is set */
-       if((perm_flags & MSG_UNREAD) && !MSG_IS_UNREAD(msginfo->flags) &&
+       if ((perm_flags & MSG_UNREAD) && !MSG_IS_UNREAD(msginfo->flags) &&
           !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                item->unread++;
-               changed = TRUE;
        }
 
+       if (!MSG_IS_UNREAD(msginfo->flags) &&(perm_flags & MSG_UNREAD)
+       && procmsg_msg_has_marked_parent(msginfo)) {
+               item->unreadmarked++;
+       }
+       
+       if (!MSG_IS_MARKED(msginfo->flags) && (perm_flags & MSG_MARKED)) {
+               procmsg_update_unread_children(msginfo, TRUE);
+       }
+
+
        /* if ignore thread flag is set */
-       if((perm_flags & MSG_IGNORE_THREAD) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
-               if(MSG_IS_NEW(msginfo->flags) || (perm_flags & MSG_NEW)) {
+       if ((perm_flags & MSG_IGNORE_THREAD) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
+               if (MSG_IS_NEW(msginfo->flags) || (perm_flags & MSG_NEW)) {
                        item->new--;
-                       changed = TRUE;
                }
-               if(MSG_IS_UNREAD(msginfo->flags) || (perm_flags & MSG_UNREAD)) {
+               if (MSG_IS_UNREAD(msginfo->flags) || (perm_flags & MSG_UNREAD)) {
                        item->unread--;
-                       changed = TRUE;
                }
+               if ((perm_flags & MSG_UNREAD) || (MSG_IS_UNREAD(msginfo->flags)
+               && procmsg_msg_has_marked_parent(msginfo))) {
+                       item->unreadmarked--;
+               }
+               if ((perm_flags & MSG_MARKED) || (MSG_IS_MARKED(msginfo->flags)
+               && !MSG_IS_IGNORE_THREAD(msginfo->flags))) {
+                       procmsg_update_unread_children(msginfo, FALSE);
+               }
+
        }
 
-       if (MSG_IS_IMAP(msginfo->flags))
+       if (msginfo->folder->folder->type == F_IMAP)
                imap_msg_set_perm_flags(msginfo, perm_flags);
 
        msginfo->flags.perm_flags |= perm_flags;
        msginfo->flags.tmp_flags |= tmp_flags;
 
-       if(changed) {
-               folderview_update_item(item, FALSE);
-       }
+       msginfo_update.msginfo = msginfo;
+       hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
+       folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
+
        CHANGE_FLAGS(msginfo);
        procmsg_msginfo_write_flags(msginfo);
 }
 
 void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmpFlags tmp_flags)
 {
-       gboolean changed = FALSE;
-       FolderItem *item = msginfo->folder;
+       FolderItem *item;
+       MsgInfoUpdate msginfo_update;
+
+       g_return_if_fail(msginfo != NULL);
+       item = msginfo->folder;
+       g_return_if_fail(item != NULL); 
        
-       debug_print(_("Unsetting flags for message %d in folder %s\n"), msginfo->msgnum, item->path);
+       debug_print("Unsetting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
 
        /* if new flag is unset */
-       if((perm_flags & MSG_NEW) && MSG_IS_NEW(msginfo->flags) &&
+       if ((perm_flags & MSG_NEW) && MSG_IS_NEW(msginfo->flags) &&
           !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                item->new--;
-               changed = TRUE;
        }
 
        /* if unread flag is unset */
-       if((perm_flags & MSG_UNREAD) && MSG_IS_UNREAD(msginfo->flags) &&
+       if ((perm_flags & MSG_UNREAD) && MSG_IS_UNREAD(msginfo->flags) &&
           !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                item->unread--;
-               changed = TRUE;
+       }
+       
+       if (MSG_IS_UNREAD(msginfo->flags) && (perm_flags & MSG_UNREAD)
+       && !MSG_IS_IGNORE_THREAD(msginfo->flags)
+       && procmsg_msg_has_marked_parent(msginfo)) {
+               item->unreadmarked--;
+       }
+
+       if (MSG_IS_MARKED(msginfo->flags) && (perm_flags & MSG_MARKED)
+       && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
+               procmsg_update_unread_children(msginfo, FALSE);
        }
 
        /* if ignore thread flag is unset */
-       if((perm_flags & MSG_IGNORE_THREAD) && MSG_IS_IGNORE_THREAD(msginfo->flags)) {
-               if(MSG_IS_NEW(msginfo->flags) && !(perm_flags & MSG_NEW)) {
+       if ((perm_flags & MSG_IGNORE_THREAD) && MSG_IS_IGNORE_THREAD(msginfo->flags)) {
+               if (MSG_IS_NEW(msginfo->flags) && !(perm_flags & MSG_NEW)) {
                        item->new++;
-                       changed = TRUE;
                }
-               if(MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)) {
+               if (MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)) {
                        item->unread++;
-                       changed = TRUE;
                }
+               if (MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)
+               && procmsg_msg_has_marked_parent(msginfo)) {
+                       item->unreadmarked++;
+               }
+               if (MSG_IS_MARKED(msginfo->flags) && !(perm_flags & MSG_MARKED)) {
+                       procmsg_update_unread_children(msginfo, TRUE);
+               }
+
        }
 
-       if (MSG_IS_IMAP(msginfo->flags))
+       if (msginfo->folder->folder->type == F_IMAP)
                imap_msg_unset_perm_flags(msginfo, perm_flags);
 
        msginfo->flags.perm_flags &= ~perm_flags;
        msginfo->flags.tmp_flags &= ~tmp_flags;
 
-       if(changed) {
-               folderview_update_item(item, FALSE);
-       }
+       msginfo_update.msginfo = msginfo;
+       hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
+       folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
+
        CHANGE_FLAGS(msginfo);
        procmsg_msginfo_write_flags(msginfo);
 }
@@ -1518,8 +1607,170 @@ void procmsg_msginfo_write_flags(MsgInfo *msginfo)
                procmsg_write_flags(msginfo, fp);
                fclose(fp);
        } else {
-               g_warning(_("Can't open mark file.\n"));
+               g_warning("Can't open mark file.\n");
        }
        
        g_free(destdir);
 }
+
+/*!
+ *\brief       check for flags (e.g. mark) in prior msgs of current thread
+ *
+ *\param       info Current message
+ *\param       perm_flags Flags to be checked
+ *\param       parentmsgs Hash of prior msgs to avoid loops
+ *
+ *\return      gboolean TRUE if perm_flags are found
+ */
+gboolean procmsg_msg_has_flagged_parent_real(MsgInfo *info,
+               MsgPermFlags perm_flags, GHashTable *parentmsgs)
+{
+       MsgInfo *tmp;
+
+       g_return_val_if_fail(info != NULL, FALSE);
+
+       if (info != NULL && info->folder != NULL && info->inreplyto != NULL) {
+               tmp = folder_item_get_msginfo_by_msgid(info->folder,
+                               info->inreplyto);
+               if (tmp && (tmp->flags.perm_flags & perm_flags)) {
+                       procmsg_msginfo_free(tmp);
+                       return TRUE;
+               } else if (tmp != NULL) {
+                       gboolean result;
+
+                       if (g_hash_table_lookup(parentmsgs, info)) {
+                               debug_print("loop detected: %s%c%d\n",
+                                       folder_item_get_path(info->folder),
+                                       G_DIR_SEPARATOR, info->msgnum);
+                               result = FALSE;
+                       } else {
+                               g_hash_table_insert(parentmsgs, info, "1");
+                               result = procmsg_msg_has_flagged_parent_real(
+                                   tmp, perm_flags, parentmsgs);
+                       }
+                       procmsg_msginfo_free(tmp);
+                       return result;
+               } else {
+                       return FALSE;
+               }
+       } else
+               return FALSE;
+}
+
+/*!
+ *\brief       Callback for cleaning up hash of parentmsgs
+ */
+gboolean parentmsgs_hash_remove(gpointer key,
+                            gpointer value,
+                            gpointer user_data)
+{
+       return TRUE;
+}
+
+/*!
+ *\brief       Set up list of parentmsgs
+ *             See procmsg_msg_has_flagged_parent_real()
+ */
+gboolean procmsg_msg_has_flagged_parent(MsgInfo *info, MsgPermFlags perm_flags)
+{
+       gboolean result;
+       GHashTable *parentmsgs = g_hash_table_new(NULL, NULL); 
+
+       result = procmsg_msg_has_flagged_parent_real(info, perm_flags, parentmsgs);
+       g_hash_table_foreach_remove(parentmsgs, parentmsgs_hash_remove, NULL);
+       g_hash_table_destroy(parentmsgs);
+       return result;
+}
+
+/*!
+ *\brief       Check if msgs prior in thread are marked
+ *             See procmsg_msg_has_flagged_parent_real()
+ */
+gboolean procmsg_msg_has_marked_parent(MsgInfo *info)
+{
+       return procmsg_msg_has_flagged_parent(info, MSG_MARKED);
+}
+
+
+GSList *procmsg_find_children_func(MsgInfo *info, 
+                                  GSList *children, GSList *all)
+{
+       GSList *cur;
+
+       g_return_val_if_fail(info!=NULL, children);
+       if (info->msgid == NULL)
+               return children;
+
+       for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
+               MsgInfo *tmp = (MsgInfo *)cur->data;
+               if (tmp->inreplyto && !strcmp(tmp->inreplyto, info->msgid)) {
+                       /* Check if message is already in the list */
+                       if ((children == NULL) || 
+                           (g_slist_index(children, tmp) == -1)) {
+                               children = g_slist_prepend(children,
+                                               procmsg_msginfo_new_ref(tmp));
+                               children = procmsg_find_children_func(tmp, 
+                                                       children, 
+                                                       all);
+                       }
+               }
+       }
+       return children;
+}
+
+GSList *procmsg_find_children (MsgInfo *info)
+{
+       GSList *children;
+       GSList *all, *cur;
+
+       g_return_val_if_fail(info!=NULL, NULL);
+       all = folder_item_get_msg_list(info->folder);
+       children = procmsg_find_children_func(info, NULL, all);
+       if (children != NULL) {
+               for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
+                       /* this will not free the used pointers
+                          created with procmsg_msginfo_new_ref */
+                       procmsg_msginfo_free((MsgInfo *)cur->data);
+               }
+       }
+       g_slist_free(all);
+
+       return children;
+}
+
+void procmsg_update_unread_children(MsgInfo *info, gboolean newly_marked)
+{
+       GSList *children = procmsg_find_children(info);
+       GSList *cur;
+       for (cur = children; cur != NULL; cur = g_slist_next(cur)) {
+               MsgInfo *tmp = (MsgInfo *)cur->data;
+               if(MSG_IS_UNREAD(tmp->flags) && !MSG_IS_IGNORE_THREAD(tmp->flags)) {
+                       if(newly_marked) 
+                               info->folder->unreadmarked++;
+                       else
+                               info->folder->unreadmarked--;
+                       folder_item_update(info->folder, F_ITEM_UPDATE_MSGCNT);
+               }
+               procmsg_msginfo_free(tmp);
+       }
+       g_slist_free(children);
+}
+
+/**
+ * Set the destination folder for a copy or move operation
+ *
+ * \param msginfo The message which's destination folder is changed
+ * \param to_folder The destination folder for the operation
+ */
+void procmsg_msginfo_set_to_folder(MsgInfo *msginfo, FolderItem *to_folder)
+{
+       if(msginfo->to_folder != NULL) {
+               msginfo->to_folder->op_count--;
+               folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
+       }
+       msginfo->to_folder = to_folder;
+       if(to_folder != NULL) {
+               to_folder->op_count++;
+               folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
+       }
+}