sycn with 0.8.8cvs11
[claws.git] / src / procmsg.c
index 61efa879f3137f117f59a5073c876bce17b33e88..11180f2b4033553b48e19fa7afe54e4a6f5903a2 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
@@ -40,6 +40,7 @@
 #include "alertpanel.h"
 #include "news.h"
 #include "imap.h"
+#include "hooks.h"
 
 typedef struct _FlagInfo       FlagInfo;
 
@@ -51,7 +52,7 @@ struct _FlagInfo
 
 static GHashTable *procmsg_read_mark_file      (const gchar    *folder);
 void   procmsg_msginfo_write_flags             (MsgInfo        *msginfo);
-
+static void procmsg_update_unread_children (MsgInfo *info, gboolean newly_marked);
 
 GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
 {
@@ -136,7 +137,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;
 }
@@ -152,7 +153,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; \
        } \
@@ -432,7 +433,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;
        }
@@ -477,7 +478,7 @@ static GHashTable *procmsg_read_mark_file(const gchar *folder)
                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));
@@ -515,12 +516,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);
@@ -536,6 +537,8 @@ 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 */
@@ -562,7 +565,7 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
                        if (parent == NULL) {
                                parent = root;
                        } else {
-                               if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->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);
                                }
                        }
@@ -603,7 +606,7 @@ 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) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
+                       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);
                        }
                }
@@ -634,7 +637,7 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
                                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)) {
+                               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);
                                }
                        }
@@ -656,6 +659,8 @@ void procmsg_move_messages(GSList *mlist)
 
        if (!mlist) return;
 
+       folder_item_update_freeze();
+
        for (cur = mlist; cur != NULL; cur = cur->next) {
                msginfo = (MsgInfo *)cur->data;
                if (!dest) {
@@ -670,12 +675,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)
@@ -686,14 +694,7 @@ void procmsg_copy_messages(GSList *mlist)
 
        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;
@@ -709,12 +710,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)
@@ -743,7 +747,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;
 }
@@ -772,7 +776,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)
@@ -806,8 +810,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) {
@@ -868,7 +871,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;
                
@@ -877,7 +880,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: 
@@ -897,16 +900,36 @@ 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;
        MsgInfo *msginfo;
 
        debug_print("saving sent message...\n");
@@ -918,26 +941,12 @@ 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");
-                       return -1;
-               }
-               if ((outfp = fopen(tmp, "wb")) == NULL) {
-                       FILE_OP_ERROR(tmp, "fopen");
-                       fclose(fp);
+               
+               if (procmsg_remove_special_headers(file, tmp) !=0)
                        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);
 
                folder_item_scan(outbox);
                if ((num = folder_item_add_msg(outbox, tmp, TRUE)) < 0) {
@@ -953,15 +962,12 @@ gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
                }
                return -1;
        }
-       msginfo = folder_item_fetch_msginfo(outbox, num);
-       if(msginfo != NULL) {
-           procmsg_msginfo_unset_flags(msginfo, ~0, ~0);
+       msginfo = folder_item_get_msginfo(outbox, num);
+       if (msginfo != NULL) {
+           procmsg_msginfo_unset_flags(msginfo, ~0, 0);
            procmsg_msginfo_free(msginfo);
        }
-
-       if(is_queued) {
-               unlink(file);
-       }
+       folder_item_update(outbox, TRUE);
 
        return 0;
 }
@@ -978,7 +984,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;
        }
 
@@ -1012,7 +1018,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);
        }
@@ -1087,14 +1093,49 @@ 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;
 
+       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);
@@ -1121,33 +1162,33 @@ 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;
@@ -1176,6 +1217,7 @@ enum
        Q_NEWS_ACCOUNT_ID  = 5,
        Q_SAVE_COPY_FOLDER = 6,
        Q_REPLY_MESSAGE_ID = 7,
+       Q_FWD_MESSAGE_ID   = 8
 };
 
 gint procmsg_send_message_queue(const gchar *file)
@@ -1188,6 +1230,7 @@ gint procmsg_send_message_queue(const gchar *file)
                                       {"NAID:", NULL, FALSE},
                                       {"SCF:",  NULL, FALSE},
                                       {"RMID:", NULL, FALSE},
+                                      {"FMID:", NULL, FALSE},
                                       {NULL,    NULL, FALSE}};
        FILE *fp;
        gint filepos;
@@ -1198,6 +1241,7 @@ gint procmsg_send_message_queue(const gchar *file)
        GSList *newsgroup_list = NULL;
        gchar *savecopyfolder = NULL;
        gchar *replymessageid = NULL;
+       gchar *fwdmessageid = NULL;
        gchar buf[BUFFSIZE];
        gint hnum;
        PrefsAccount *mailac = NULL, *newsac = NULL;
@@ -1239,15 +1283,17 @@ gint procmsg_send_message_queue(const gchar *file)
                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"));
+               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)) {
@@ -1260,8 +1306,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;
                                }
                        }
@@ -1271,7 +1317,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;
@@ -1282,11 +1328,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)) ? 
@@ -1294,7 +1340,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;
@@ -1309,7 +1356,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) {
@@ -1321,7 +1368,7 @@ gint procmsg_send_message_queue(const gchar *file)
                        }
                        fclose(tmpfp);
 
-                       if(newsval == 0) {
+                       if (newsval == 0) {
                                debug_print("Sending message by news\n");
 
                                folder = FOLDER(newsac->folder);
@@ -1352,35 +1399,43 @@ gint procmsg_send_message_queue(const gchar *file)
                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) {
+       if (replymessageid != NULL || fwdmessageid != NULL) {
                gchar **tokens;
                FolderItem *item;
                
-               tokens = g_strsplit(replymessageid, "\x7f", 0);
+               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) {
+               if (item != NULL) {
                        MsgInfo *msginfo;
                        
-                       msginfo = folder_item_fetch_msginfo(item, atoi(tokens[1]));
-                       if((msginfo != NULL) && (strcmp(msginfo->msgid, tokens[2]) != 0)) {
+                       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_fetch_msginfo_by_id(item, tokens[2]);
+                       if (msginfo == NULL) {
+                               msginfo = folder_item_get_msginfo_by_msgid(item, tokens[2]);
                        }
                        
-                       if(msginfo != NULL) {
-                               procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
-                               procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
-
+                       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);
                        }
                }
@@ -1389,6 +1444,7 @@ gint procmsg_send_message_queue(const gchar *file)
 
        g_free(savecopyfolder);
        g_free(replymessageid);
+       g_free(fwdmessageid);
        
        return (newsval != 0 ? newsval : mailval);
 }
@@ -1403,35 +1459,54 @@ 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;
 
+       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))
@@ -1440,44 +1515,64 @@ void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmp
        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);
 
        /* 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))
@@ -1486,9 +1581,10 @@ void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgT
        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);
 }
@@ -1506,8 +1602,148 @@ 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 (MsgInfo *info)
+{
+       GSList *children = NULL;
+       GSList *all, *cur;
+       
+       g_return_val_if_fail(info!=NULL, NULL);
+       if (info->msgid == NULL)
+               return NULL;
+       all = folder_item_get_msg_list(info->folder);
+       for (cur = all; cur != NULL; cur = g_slist_next(cur)) {
+               MsgInfo *tmp = (MsgInfo *)cur->data;
+               if (tmp->inreplyto && !strcmp(tmp->inreplyto, info->msgid)) {
+                       GSList *grand_children;
+                       children = g_slist_prepend(children, tmp);
+                       grand_children = procmsg_find_children(tmp);
+                       children = slist_concat_unique(children, grand_children);
+                       g_slist_free(grand_children);
+               }
+               if (tmp && tmp != info)
+                       procmsg_msginfo_free(tmp);
+       }
+       
+       return children;
+}
+
+static 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);
+       }
+}
+
+/**
+ * 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);
+       }
+}