* src/compose.[ch]
[claws.git] / src / compose.c
index 6f74b4b16fc3b553382c375a1c0943426fa93665..8910d1043c6c9251d2dda2c79763e91bcc19abaf 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
 #include "procmsg.h"
 #include "menu.h"
 #include "stock_pixmap.h"
-#include "send.h"
+#include "send_message.h"
 #include "imap.h"
 #include "news.h"
 #include "customheader.h"
 #include "prefs_common.h"
 #include "prefs_account.h"
-#include "prefs_actions.h"
+#include "action.h"
 #include "account.h"
 #include "filesel.h"
 #include "procheader.h"
@@ -92,6 +92,7 @@
 #include "statusbar.h"
 #include "about.h"
 #include "base64.h"
+#include "quoted-printable.h"
 #include "codeconv.h"
 #include "utils.h"
 #include "gtkutils.h"
@@ -164,11 +165,6 @@ Compose *compose_generic_new                       (PrefsAccount   *account,
 
 static Compose *compose_create                 (PrefsAccount   *account,
                                                 ComposeMode     mode);
-static void compose_toolbar_create             (Compose        *compose,
-                                                GtkWidget      *container);
-static void compose_toolbar_update              (Compose        *compose);
-static void compose_toolbar_set_sensitive       (Compose        *compose,
-                                                gboolean        sensitive);
 
 static GtkWidget *compose_account_option_menu_create
                                                (Compose        *compose);
@@ -200,7 +196,9 @@ static void compose_reply_set_entry         (Compose        *compose,
                                                 followup_and_reply_to);
 static void compose_reedit_set_entry           (Compose        *compose,
                                                 MsgInfo        *msginfo);
-static void compose_insert_sig                 (Compose        *compose);
+static void compose_insert_sig                 (Compose        *compose,
+                                                gboolean        replace);
+static gchar *compose_get_signature_str                (Compose        *compose);
 static void compose_insert_file                        (Compose        *compose,
                                                 const gchar    *file);
 static void compose_attach_append              (Compose        *compose,
@@ -215,7 +213,8 @@ static void compose_wrap_line_all_full              (Compose        *compose,
                                                 gboolean        autowrap);
 static void compose_set_title                  (Compose        *compose);
 static void compose_select_account             (Compose        *compose,
-                                                PrefsAccount   *account);
+                                                PrefsAccount   *account,
+                                                gboolean        init);
 
 static PrefsAccount *compose_current_mail_account(void);
 /* static gint compose_send                    (Compose        *compose); */
@@ -248,7 +247,8 @@ static gint compose_write_headers           (Compose        *compose,
 static void compose_convert_header             (gchar          *dest,
                                                 gint            len,
                                                 gchar          *src,
-                                                gint            header_len);
+                                                gint            header_len,
+                                                gboolean        addr_field);
 static void compose_generate_msgid             (Compose        *compose,
                                                 gchar          *buf,
                                                 gint            len);
@@ -296,31 +296,8 @@ static void compose_update_priority_menu_item(Compose * compose);
 static gboolean compose_edit_size_alloc (GtkEditable   *widget,
                                         GtkAllocation  *allocation,
                                         GtkSHRuler     *shruler);
-
-static void toolbar_send_cb            (GtkWidget      *widget,
-                                        gpointer        data);
-static void toolbar_send_later_cb      (GtkWidget      *widget,
-                                        gpointer        data);
-static void toolbar_draft_cb           (GtkWidget      *widget,
-                                        gpointer        data);
-static void toolbar_insert_cb          (GtkWidget      *widget,
-                                        gpointer        data);
-static void toolbar_attach_cb          (GtkWidget      *widget,
-                                        gpointer        data);
-static void toolbar_sig_cb             (GtkWidget      *widget,
-                                        gpointer        data);
-static void toolbar_ext_editor_cb      (GtkWidget      *widget,
-                                        gpointer        data);
-static void toolbar_linewrap_cb                (GtkWidget      *widget,
-                                        gpointer        data);
-static void toolbar_address_cb         (GtkWidget      *widget,
-                                        gpointer        data);
-static void toolbar_compose_buttons_cb  (GtkWidget      *widget, 
-
-                                        ToolbarItem    *item);
 static void account_activated          (GtkMenuItem    *menuitem,
                                         gpointer        data);
-
 static void attach_selected            (GtkCList       *clist,
                                         gint            row,
                                         gint            column,
@@ -350,6 +327,9 @@ static void compose_attach_cb               (gpointer        data,
 static void compose_insert_file_cb     (gpointer        data,
                                         guint           action,
                                         GtkWidget      *widget);
+static void compose_insert_sig_cb      (gpointer        data,
+                                        guint           action,
+                                        GtkWidget      *widget);
 
 static void compose_close_cb           (gpointer        data,
                                         guint           action,
@@ -482,7 +462,7 @@ static void text_inserted           (GtkWidget      *widget,
                                         Compose        *compose);
 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
                                  gboolean to_all, gboolean to_ml,
-                                 gboolean ignore_replyto,
+                                 gboolean to_sender,
                                  gboolean followup_and_reply_to,
                                  const gchar *body);
 
@@ -508,7 +488,7 @@ static GtkItemFactoryEntry compose_popup_entries[] =
        {N_("/_Add..."),        NULL, compose_attach_cb, 0, NULL},
        {N_("/_Remove"),        NULL, compose_attach_remove_selected, 0, NULL},
        {N_("/---"),            NULL, NULL, 0, "<Separator>"},
-       {N_("/_Property..."),   NULL, compose_attach_property, 0, NULL}
+       {N_("/_Properties..."), NULL, compose_attach_property, 0, NULL}
 };
 
 static GtkItemFactoryEntry compose_entries[] =
@@ -516,7 +496,7 @@ static GtkItemFactoryEntry compose_entries[] =
        {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
        {N_("/_File/_Attach file"),             "<control>M", compose_attach_cb,      0, NULL},
        {N_("/_File/_Insert file"),             "<control>I", compose_insert_file_cb, 0, NULL},
-       {N_("/_File/Insert si_gnature"),        "<control>G", compose_insert_sig,     0, NULL},
+       {N_("/_File/Insert si_gnature"),        "<control>G", compose_insert_sig_cb,  0, NULL},
        {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
        {N_("/_File/_Close"),                   "<control>W", compose_close_cb, 0, NULL},
 
@@ -720,12 +700,13 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
        ifactory = gtk_item_factory_from_widget(compose->menubar);
 
        compose->replyinfo = NULL;
+       compose->fwdinfo   = NULL;
 
        text = GTK_STEXT(compose->text);
        gtk_stext_freeze(text);
 
-       if (prefs_common.auto_sig)
-               compose_insert_sig(compose);
+       if (account->auto_sig)
+               compose_insert_sig(compose, FALSE);
        gtk_editable_set_position(GTK_EDITABLE(text), 0);
        gtk_stext_set_point(text, 0);
 
@@ -811,71 +792,127 @@ Compose *compose_new_followup_and_replyto(PrefsAccount *account,
 }
 */
 
+void compose_reply_mode(ComposeMode mode, GSList *msginfo_list, gchar *body)
+{
+       MsgInfo *msginfo;
+       guint list_len;
+
+       g_return_if_fail(msginfo_list != NULL);
+
+       msginfo = (MsgInfo*)g_slist_nth_data(msginfo_list, 0);
+       g_return_if_fail(msginfo != NULL);
+
+       list_len = g_slist_length(msginfo_list);
+
+       switch (mode) {
+       case COMPOSE_REPLY:
+               compose_reply(msginfo, prefs_common.reply_with_quote,
+                             FALSE, prefs_common.default_reply_list, FALSE, body);
+               break;
+       case COMPOSE_REPLY_WITH_QUOTE:
+               compose_reply(msginfo, TRUE, FALSE, prefs_common.default_reply_list, FALSE, body);
+               break;
+       case COMPOSE_REPLY_WITHOUT_QUOTE:
+               compose_reply(msginfo, FALSE, FALSE, prefs_common.default_reply_list, FALSE, NULL);
+               break;
+       case COMPOSE_REPLY_TO_SENDER:
+               compose_reply(msginfo, prefs_common.reply_with_quote,
+                             FALSE, FALSE, TRUE, body);
+               break;
+       case COMPOSE_FOLLOWUP_AND_REPLY_TO:
+               compose_followup_and_reply_to(msginfo,
+                                             prefs_common.reply_with_quote,
+                                             FALSE, FALSE, body);
+               break;
+       case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
+               compose_reply(msginfo, TRUE, FALSE, FALSE, TRUE, body);
+               break;
+       case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
+               compose_reply(msginfo, FALSE, FALSE, FALSE, TRUE, NULL);
+               break;
+       case COMPOSE_REPLY_TO_ALL:
+               compose_reply(msginfo, prefs_common.reply_with_quote,
+                             TRUE, FALSE, FALSE, body);
+               break;
+       case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
+               compose_reply(msginfo, TRUE, TRUE, FALSE, FALSE, body);
+               break;
+       case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
+               compose_reply(msginfo, FALSE, TRUE, FALSE, FALSE, NULL);
+               break;
+       case COMPOSE_REPLY_TO_LIST:
+               compose_reply(msginfo, prefs_common.reply_with_quote,
+                             FALSE, TRUE, FALSE, body);
+               break;
+       case COMPOSE_REPLY_TO_LIST_WITH_QUOTE:
+               compose_reply(msginfo, TRUE, FALSE, TRUE, FALSE, body);
+               break;
+       case COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE:
+               compose_reply(msginfo, FALSE, FALSE, TRUE, FALSE, NULL);
+               break;
+       case COMPOSE_FORWARD:
+               if (prefs_common.forward_as_attachment) {
+                       compose_reply_mode(COMPOSE_FORWARD_AS_ATTACH, msginfo_list, body);
+                       return;
+               } else {
+                       compose_reply_mode(COMPOSE_FORWARD_INLINE, msginfo_list, body);
+                       return;
+               }
+               break;
+       case COMPOSE_FORWARD_INLINE:
+               /* check if we reply to more than one Message */
+               if (list_len == 1) {
+                       compose_forward(NULL, msginfo, FALSE, body);
+                       break;
+               } 
+               /* more messages FALL THROUGH */
+       case COMPOSE_FORWARD_AS_ATTACH:
+               compose_forward_multiple(NULL, msginfo_list);
+               break;
+       case COMPOSE_REDIRECT:
+               compose_redirect(NULL, msginfo);
+               break;
+       default:
+               g_warning("compose_reply(): invalid Compose Mode: %d\n", mode);
+       }
+}
+
 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
-                  gboolean to_ml, gboolean ignore_replyto
+                  gboolean to_ml, gboolean to_sender
                   const gchar *body)
 {
        compose_generic_reply(msginfo, quote, to_all, to_ml, 
-                             ignore_replyto, FALSE, body);
+                             to_sender, FALSE, body);
 }
 
 void compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
                                   gboolean to_all,
-                                  gboolean ignore_replyto,
+                                  gboolean to_sender,
                                   const gchar *body)
 {
        compose_generic_reply(msginfo, quote, to_all, FALSE, 
-                             ignore_replyto, TRUE, body);
+                             to_sender, TRUE, body);
 }
 
 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
                                  gboolean to_all, gboolean to_ml,
-                                 gboolean ignore_replyto,
+                                 gboolean to_sender,
                                  gboolean followup_and_reply_to,
                                  const gchar *body)
 {
        Compose *compose;
-       PrefsAccount *account;
+       PrefsAccount *account = NULL;
        PrefsAccount *reply_account;
        GtkSText *text;
 
        g_return_if_fail(msginfo != NULL);
        g_return_if_fail(msginfo->folder != NULL);
 
-       account = NULL;
-       /* select the account set in folderitem's property (if enabled) */
-       if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
-               account = account_find_from_id(msginfo->folder->prefs->default_account);
+       account = account_get_reply_account(msginfo, prefs_common.reply_account_autosel);
        
-       /* select the account for the whole folder (IMAP / NNTP) */
-       if (!account)
-               /* FIXME: this is not right, because folder may be nested. we should
-                * ascend the tree until we find a parent with proper account 
-                * information */
-               account = msginfo->folder->folder->account;
-
-       /* select account by to: and cc: header if enabled */
-       if (prefs_common.reply_account_autosel) {
-               if (!account && msginfo->to) {
-                       gchar *to;
-                       Xstrdup_a(to, msginfo->to, return);
-                       extract_address(to);
-                       account = account_find_from_address(to);
-               }
-               if (!account) {
-                       gchar cc[BUFFSIZE];
-                       if (!get_header_from_msginfo(msginfo, cc, sizeof(cc), "CC:")) { /* Found a CC header */
-                               extract_address(cc);
-                               account = account_find_from_address(cc);
-                       }        
-               }
-       }
-
-       /* select current account */
-       if (!account) account = cur_account;
        g_return_if_fail(account != NULL);
 
-       if (ignore_replyto && account->protocol == A_NNTP &&
+       if (to_sender && account->protocol == A_NNTP &&
            !followup_and_reply_to) {
                reply_account =
                        account_find_from_address(account->address);
@@ -911,7 +948,7 @@ static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
 
        if (compose_parse_header(compose, msginfo) < 0) return;
        compose_reply_set_entry(compose, msginfo, to_all, to_ml, 
-                               ignore_replyto, followup_and_reply_to);
+                               to_sender, followup_and_reply_to);
        compose_show_first_last_header(compose, TRUE);
 
        text = GTK_STEXT(compose->text);
@@ -931,8 +968,8 @@ static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
                                              qmark, body);
        }
 
-       if (prefs_common.auto_sig)
-               compose_insert_sig(compose);
+       if (account->auto_sig)
+               compose_insert_sig(compose, FALSE);
 
        if (quote && prefs_common.linewrap_quote)
                compose_wrap_line_all(compose);
@@ -991,13 +1028,12 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
        }
        g_return_val_if_fail(account != NULL, NULL);
 
-       MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
-       MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
-       if (MSG_IS_IMAP(msginfo->flags))
-               imap_msg_unset_perm_flags(msginfo, MSG_REPLIED);
-
        compose = compose_create(account, COMPOSE_FORWARD);
 
+       compose->fwdinfo = procmsg_msginfo_get_full_info(msginfo);
+       if (!compose->fwdinfo)
+               compose->fwdinfo = procmsg_msginfo_copy(msginfo);
+
        if (msginfo->subject && *msginfo->subject) {
                gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Fw: ");
                gtk_entry_append_text(GTK_ENTRY(compose->subject_entry),
@@ -1042,8 +1078,8 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
                        procmsg_msginfo_free(full_msginfo);
                }
 
-       if (prefs_common.auto_sig)
-               compose_insert_sig(compose);
+       if (account->auto_sig)
+               compose_insert_sig(compose, FALSE);
 
        if (prefs_common.linewrap_quote)
                compose_wrap_line_all(compose);
@@ -1121,8 +1157,8 @@ Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
                g_free(msgfile);
        }
 
-       if (prefs_common.auto_sig)
-               compose_insert_sig(compose);
+       if (account->auto_sig)
+               compose_insert_sig(compose, FALSE);
 
        if (prefs_common.linewrap_quote)
                compose_wrap_line_all(compose);
@@ -1248,6 +1284,7 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo)
        ifactory = gtk_item_factory_from_widget(compose->menubar);
 
        compose->replyinfo = NULL;
+       compose->fwdinfo = NULL;
 
        compose_show_first_last_header(compose, TRUE);
 
@@ -1379,6 +1416,46 @@ void compose_entry_select (Compose *compose, const gchar *mailto)
        }
 }
 
+void compose_toolbar_cb(gint action, gpointer data)
+{
+       ToolbarItem *toolbar_item = (ToolbarItem*)data;
+       Compose *compose = (Compose*)toolbar_item->parent;
+       
+       g_return_if_fail(compose != NULL);
+
+       switch(action) {
+       case A_SEND:
+               compose_send_cb(compose, 0, NULL);
+               break;
+       case A_SENDL:
+               compose_send_later_cb(compose, 0, NULL);
+               break;
+       case A_DRAFT:
+               compose_draft_cb(compose, 0, NULL);
+               break;
+       case A_INSERT:
+               compose_insert_file_cb(compose, 0, NULL);
+               break;
+       case A_ATTACH:
+               compose_attach_cb(compose, 0, NULL);
+               break;
+       case A_SIG:
+               compose_insert_sig(compose, FALSE);
+               break;
+       case A_EXTEDITOR:
+               compose_ext_editor_cb(compose, 0, NULL);
+               break;
+       case A_LINEWRAP:
+               compose_wrap_line(compose);
+               break;
+       case A_ADDRBOOK:
+               compose_address_cb(compose, 0, NULL);
+               break;
+       default:
+               break;
+       }
+}
+
 static void compose_entries_set(Compose *compose, const gchar *mailto)
 {
        gchar *to = NULL;
@@ -1414,11 +1491,11 @@ static void compose_entries_set(Compose *compose, const gchar *mailto)
 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
 {
        static HeaderEntry hentry[] = {{"Reply-To:",    NULL, TRUE},
-                                      {"Cc:",          NULL, FALSE},
+                                      {"Cc:",          NULL, TRUE},
                                       {"References:",  NULL, FALSE},
-                                      {"Bcc:",         NULL, FALSE},
-                                      {"Newsgroups:",  NULL, FALSE},
-                                      {"Followup-To:", NULL, FALSE},
+                                      {"Bcc:",         NULL, TRUE},
+                                      {"Newsgroups:",  NULL, TRUE},
+                                      {"Followup-To:", NULL, TRUE},
                                       {"List-Post:",   NULL, FALSE},
                                       {"X-Priority:",  NULL, FALSE},
                                       {NULL,           NULL, FALSE}};
@@ -1472,6 +1549,7 @@ static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
                hentry[H_BCC].body = NULL;
        }
        if (hentry[H_NEWSGROUPS].body != NULL) {
+               conv_unmime_header_overwrite(hentry[H_NEWSGROUPS].body);
                compose->newsgroups = hentry[H_NEWSGROUPS].body;
                hentry[H_NEWSGROUPS].body = NULL;
        }
@@ -1595,7 +1673,8 @@ static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
        gchar *buf;
        gchar *p, *lastp;
        gint len;
-
+       gchar *trimmed_body = body;
+       
        if (!msginfo)
                msginfo = &dummyinfo;
 
@@ -1612,7 +1691,11 @@ static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
        }
 
        if (fmt && *fmt != '\0') {
-               quote_fmt_init(msginfo, quote_str, body);
+               while (trimmed_body && strlen(trimmed_body) > 1
+                       && trimmed_body[0]=='\n')
+                       *trimmed_body++;
+
+               quote_fmt_init(msginfo, quote_str, trimmed_body);
                quote_fmt_scan_string(fmt);
                quote_fmt_parse();
 
@@ -1643,7 +1726,7 @@ static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
 
 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
                                    gboolean to_all, gboolean to_ml,
-                                   gboolean ignore_replyto,
+                                   gboolean to_sender,
                                    gboolean followup_and_reply_to)
 {
        GSList *cc_list = NULL;
@@ -1655,13 +1738,13 @@ static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
        g_return_if_fail(compose->account != NULL);
        g_return_if_fail(msginfo != NULL);
 
-       if (compose->account->protocol != A_NNTP || followup_and_reply_to) {
+       if (compose->account->protocol != A_NNTP) {
                if (!compose->replyto && to_ml && compose->ml_post
                    && !(msginfo->folder && msginfo->folder->prefs->enable_default_reply_to))
                        compose_entry_append(compose,
                                           compose->ml_post,
                                           COMPOSE_TO);
-               else if (!(to_all || ignore_replyto)
+               else if (!(to_all || to_sender)
                         && msginfo->folder
                         && msginfo->folder->prefs->enable_default_reply_to) {
                        compose_entry_append(compose,
@@ -1669,32 +1752,38 @@ static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
                            COMPOSE_TO);
                } else
                        compose_entry_append(compose,
-                                (compose->replyto && !ignore_replyto)
-                                ? compose->replyto
-                                : msginfo->from ? msginfo->from : "",
-                                COMPOSE_TO);
+                                (compose->replyto && !to_sender)
+                                 ? compose->replyto :
+                                 msginfo->from ? msginfo->from : "",
+                                 COMPOSE_TO);
        } else {
-               if (ignore_replyto)
+               if (to_sender || (compose->followup_to && 
+                       !strncmp(compose->followup_to, "poster", 6)))
                        compose_entry_append
-                               (compose, msginfo->from ? msginfo->from : "",
+                               (compose, 
+                                (compose->replyto ? compose->replyto :
+                                       msginfo->from ? msginfo->from : ""),
                                 COMPOSE_TO);
-               else {
-                       if (compose->followup_to && !strncmp(compose->followup_to, "poster", 6)) {
-                               compose_entry_append
-                                       (compose,
-                                       ((compose->replyto && !ignore_replyto)
-                                       ? compose->replyto
-                                       : msginfo->from ? msginfo->from : ""),
-                                       COMPOSE_TO);                            
-                       } else {
-                               compose_entry_append
-                                       (compose,
-                                        compose->followup_to ? compose->followup_to
-                                        : compose->newsgroups ? compose->newsgroups
-                                        : "",
-                                        COMPOSE_NEWSGROUPS);
-                       }
-               }
+                                
+               else if (followup_and_reply_to || to_all) {
+                       compose_entry_append
+                               (compose,
+                                (compose->replyto ? compose->replyto :
+                                msginfo->from ? msginfo->from : ""),
+                                COMPOSE_TO);                           
+               
+                       compose_entry_append
+                               (compose,
+                                compose->followup_to ? compose->followup_to :
+                                compose->newsgroups ? compose->newsgroups : "",
+                                COMPOSE_NEWSGROUPS);
+               } 
+               else 
+                       compose_entry_append
+                               (compose,
+                                compose->followup_to ? compose->followup_to :
+                                compose->newsgroups ? compose->newsgroups : "",
+                                COMPOSE_NEWSGROUPS);
        }
 
        if (msginfo->subject && *msginfo->subject) {
@@ -1727,28 +1816,33 @@ static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
        }
 
        if (replyto && from)
-               cc_list = address_list_append(cc_list, from);
-       cc_list = address_list_append(cc_list, msginfo->to);
-       cc_list = address_list_append(cc_list, compose->cc);
+               cc_list = address_list_append_with_comments(cc_list, from);
+       cc_list = address_list_append_with_comments(cc_list, msginfo->to);
+       cc_list = address_list_append_with_comments(cc_list, compose->cc);
 
        to_table = g_hash_table_new(g_str_hash, g_str_equal);
        if (replyto)
-               g_hash_table_insert(to_table, replyto, GINT_TO_POINTER(1));
+               g_hash_table_insert(to_table, g_strdup(replyto), GINT_TO_POINTER(1));
        if (compose->account)
-               g_hash_table_insert(to_table, compose->account->address,
+               g_hash_table_insert(to_table, g_strdup(compose->account->address),
                                    GINT_TO_POINTER(1));
 
        /* remove address on To: and that of current account */
        for (cur = cc_list; cur != NULL; ) {
                GSList *next = cur->next;
+               gchar *addr;
+
+               addr = g_strdup(cur->data);
+               extract_address(addr);
 
-               if (g_hash_table_lookup(to_table, cur->data) != NULL)
+               if (GPOINTER_TO_INT(g_hash_table_lookup(to_table, addr)) == 1)
                        cc_list = g_slist_remove(cc_list, cur->data);
                else
-                       g_hash_table_insert(to_table, cur->data, cur);
+                       g_hash_table_insert(to_table, addr, GINT_TO_POINTER(1));
 
                cur = next;
        }
+       hash_free_strings(to_table);
        g_hash_table_destroy(to_table);
 
        if (cc_list) {
@@ -1781,6 +1875,8 @@ static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
        SET_ADDRESS(COMPOSE_CC, compose->cc);
        SET_ADDRESS(COMPOSE_BCC, compose->bcc);
        SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
+       SET_ADDRESS(COMPOSE_NEWSGROUPS, compose->newsgroups);
+       SET_ADDRESS(COMPOSE_FOLLOWUPTO, compose->followup_to);
 
        compose_update_priority_menu_item(compose);
 #if USE_GPGME  
@@ -1793,74 +1889,100 @@ static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
 #undef SET_ENTRY
 #undef SET_ADDRESS
 
-static void compose_exec_sig(Compose *compose, gchar *sigfile)
+static void compose_insert_sig(Compose *compose, gboolean replace)
 {
-       FILE  *sigprg;
-       gchar  *buf;
-       size_t buf_len = 128;
-       if (strlen(sigfile) < 2)
-         return;
-       sigprg = popen(sigfile+1, "r");
-       if (sigprg) {
+       GtkSText *text = GTK_STEXT(compose->text);
+       gint cur_pos;
 
-               buf = g_malloc(buf_len);
+       g_return_if_fail(compose->account != NULL);
 
-               if (!buf) {
-                       gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
-                       "Unable to insert signature (malloc failed)\n", -1);
+       cur_pos = gtk_editable_get_position(GTK_EDITABLE(text));
 
-                       pclose(sigprg);
-                       return;
-               }
+       gtk_stext_freeze(text);
 
-               while (!feof(sigprg)) {
-                       bzero(buf, buf_len);
-                       fread(buf, buf_len-1, 1, sigprg);
-                       gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, buf, -1);
+       if (replace && compose->sig_str) {
+               gchar *tmp;
+               gint pos;
+               gint len;
+
+               if (compose->account->sig_sep)
+                       tmp = g_strconcat(compose->account->sig_sep, "\n",
+                                         compose->sig_str, NULL);
+               else
+                       tmp = g_strdup(compose->sig_str);
+
+               pos = gtkut_stext_find(text, 0, tmp, TRUE);
+               if (pos != -1) {
+                       gtk_stext_set_point(text, pos);
+                       len = get_wcs_len(tmp);
+                       gtk_stext_forward_delete(text, len);
+               } else {
+                       len = gtk_stext_get_length(text);
+                       gtk_stext_set_point(text, len);
                }
 
-               g_free(buf);
-               pclose(sigprg);
-       }
-       else
-       {
-               gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
-               "Can't exec file: ", -1);
-               gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
-               sigfile+1, -1);
+               g_free(tmp);
+       } else
+               gtk_stext_insert(text, NULL, NULL, NULL, "\n\n", 2);
+
+       if (compose->account->sig_sep) {
+               gtk_stext_insert(text, NULL, NULL, NULL, 
+                                compose->account->sig_sep, -1);
+               gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
        }
+
+       g_free(compose->sig_str);
+       compose->sig_str = compose_get_signature_str(compose);
+
+       gtk_stext_insert(text, NULL, NULL, NULL, compose->sig_str, -1);
+
+       gtk_stext_thaw(text);
+
+       if (cur_pos > gtk_stext_get_length(text))
+               cur_pos = gtk_stext_get_length(text);
+
+       gtk_editable_set_position(GTK_EDITABLE(text), cur_pos);
+       gtk_stext_set_point(text, cur_pos);
 }
 
-static void compose_insert_sig(Compose *compose)
+static gchar *compose_get_signature_str(Compose *compose)
 {
-       gchar *sigfile;
+       static gchar *default_sigfile;
+       gchar *sig_file = NULL;
+       gchar *sig_str = NULL;
 
-       if (compose->account && compose->account->sig_path)
-               sigfile = g_strdup(compose->account->sig_path);
-       else
-               sigfile = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
-                                     DEFAULT_SIGNATURE, NULL);
+       g_return_val_if_fail(compose->account != NULL, NULL);
 
-       if (!is_file_or_fifo_exist(sigfile) && sigfile[0] != '|') {
-               g_free(sigfile);
-               return;
+       if (compose->account->sig_type == SIG_FILE) {
+               if (compose->account->sig_path)
+                       sig_file = compose->account->sig_path;
+               else {
+                       if (!default_sigfile)
+                               default_sigfile = g_strconcat
+                                       (get_home_dir(), G_DIR_SEPARATOR_S,
+                                        DEFAULT_SIGNATURE, NULL);
+                       sig_file = default_sigfile;
+               }
+
+               if (!is_file_or_fifo_exist(sig_file)) {
+                       g_warning("can't open signature file: %s\n", sig_file);
+                       return NULL;
+               }
        }
 
-       gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, "\n\n", 2);
-       if (prefs_common.sig_sep) {
-               gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
-                               prefs_common.sig_sep, -1);
-               gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
-                               "\n", 1);
+       if (compose->account->sig_type == SIG_COMMAND) {
+               if (compose->account->sig_path)
+                       sig_str = get_command_output
+                               (compose->account->sig_path);
+       } else {
+               gchar *tmp;
+
+               tmp = file_read_to_str(sig_file);
+               sig_str = normalize_newlines(tmp);
+               g_free(tmp);
        }
 
-       if (sigfile[0] == '|')
-               compose_exec_sig(compose, sigfile);
-       else
-               compose_insert_file(compose, sigfile);
-       g_free(sigfile);
+       return sig_str;
 }
 
 static void compose_insert_file(Compose *compose, const gchar *file)
@@ -1942,7 +2064,10 @@ static void compose_attach_append(Compose *compose, const gchar *file,
        if (content_type) {
                ainfo->content_type = g_strdup(content_type);
                if (!strcasecmp(content_type, "message/rfc822")) {
-                       ainfo->encoding = ENC_7BIT;
+                       if (procmime_get_encoding_for_file(file) == ENC_7BIT)
+                               ainfo->encoding = ENC_7BIT;
+                       else
+                               ainfo->encoding = ENC_8BIT;
                        ainfo->name = g_strdup_printf
                                (_("Message: %s"),
                                 g_basename(filename ? filename : file));
@@ -2049,6 +2174,10 @@ static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
        }                                                                    \
 }
 
+#define DISP_WIDTH(len) \
+       ((len > 2 && conv_get_current_charset() == C_UTF_8) ? 2 : \
+        (len == 2 && conv_get_current_charset() == C_UTF_8) ? 1 : len)
+
 #define INDENT_CHARS   ">|#"
 #define SPACE_CHARS    " \t"
 
@@ -2176,7 +2305,7 @@ static void compose_wrap_line(Compose *compose)
                        line_len = cur_len + ch_len;
                }
 
-               if (cur_len + ch_len > prefs_common.linewrap_len &&
+               if (cur_len + DISP_WIDTH(ch_len) > prefs_common.linewrap_len &&
                    line_len > 0) {
                        gint tlen = ch_len;
 
@@ -2197,16 +2326,16 @@ static void compose_wrap_line(Compose *compose)
                        p_end++;
                        cur_pos++;
                        line_pos++;
-                       cur_len = cur_len - line_len + ch_len;
+                       cur_len = cur_len - line_len + DISP_WIDTH(ch_len);
                        line_len = 0;
                        continue;
                }
 
                if (ch_len > 1) {
                        line_pos = cur_pos + 1;
-                       line_len = cur_len + ch_len;
+                       line_len = cur_len + DISP_WIDTH(ch_len);
                }
-               cur_len += ch_len;
+               cur_len += DISP_WIDTH(ch_len);
        }
 
 compose_end:
@@ -2480,6 +2609,14 @@ static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
                                g_print("after delete l_pos=");
                                dump_text(text, line_pos, tlen, 1);
 #endif
+                               /* move beginning of line if we are on LF */
+                               GET_CHAR(line_pos, cb, clen);
+                               if (clen == 1 && *cb == '\n')
+                                       line_pos++;
+#ifdef WRAP_DEBUG
+                               g_print("new line_pos=%d\n", line_pos);
+#endif
+
                                continue;
                        }
 
@@ -2503,7 +2640,7 @@ static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
                }
 
                /* are we over wrapping length set in preferences ? */
-               if (cur_len + ch_len > linewrap_len) {
+               if (cur_len + DISP_WIDTH(ch_len) > linewrap_len) {
                        gint clen;
 
 #ifdef WRAP_DEBUG
@@ -2601,10 +2738,10 @@ static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
 
                if (ch_len > 1) {
                        line_pos = cur_pos + 1;
-                       line_len = cur_len + ch_len;
+                       line_len = cur_len + DISP_WIDTH(ch_len);
                }
                /* advance to next character in buffer */
-               cur_len += ch_len;
+               cur_len += DISP_WIDTH(ch_len);
        }
 
        if (frozen)
@@ -2659,7 +2796,8 @@ compose_current_mail_account(void)
        return ac;
 }
 
-static void compose_select_account(Compose *compose, PrefsAccount *account)
+static void compose_select_account(Compose *compose, PrefsAccount *account,
+                                  gboolean init)
 {
        GtkWidget *menuitem;
        GtkItemFactory *ifactory;
@@ -2744,6 +2882,9 @@ static void compose_select_account(Compose *compose, PrefsAccount *account)
                                       
        activate_gnupg_mode(compose, account);          
 #endif /* USE_GPGME */
+
+       if (!init && account->auto_sig)
+               compose_insert_sig(compose, TRUE);
 }
 
 gboolean compose_check_for_valid_recipient(Compose *compose) {
@@ -2827,13 +2968,17 @@ gint compose_send(Compose *compose)
                alertpanel_error(_("Could not queue message for sending"));
                return -1;
        }
+
+       if (msgnum == 0) {
+               alertpanel_error(_("The message was queued but could not be sent.\nUse \"Send queued messages\" from the main window to retry."));
+               return 0;
+       }
        
        msgpath = folder_item_fetch_msg(folder, msgnum);
        val = procmsg_send_message_queue(msgpath);
        g_free(msgpath);
 
        folder_item_remove_msg(folder, msgnum);
-       folder_update_item(folder, TRUE);
 
        return val;
 }
@@ -2930,13 +3075,10 @@ gint compose_send(Compose *compose)
                                        alertpanel_error(_("Can't queue the message."));
                        }
                } else
-                       alertpanel_error(_("Error occurred while sending the message."));
+                       alertpanel_error_log(_("Error occurred while sending the message."));
        } else {
                if (compose->mode == COMPOSE_REEDIT) {
                        compose_remove_reedit_target(compose);
-                       if (compose->targetinfo)
-                               folder_update_item
-                                       (compose->targetinfo->folder, TRUE);
                }
                /* save message to outbox */
                if (prefs_common.savemsg) {
@@ -2947,8 +3089,6 @@ gint compose_send(Compose *compose)
                        if (procmsg_save_to_outbox(outbox, tmp, FALSE) < 0)
                                alertpanel_error
                                        (_("Can't save the message to Sent."));
-                       else
-                               folder_update_item(outbox, TRUE);
                }
        }
 
@@ -2992,7 +3132,7 @@ static gint compose_redirect_write_headers_from_headerlist(Compose *compose,
                        if (str[0] != '\0') {
                                compose_convert_header
                                        (buf, sizeof(buf), str,
-                                       strlen("Resent-To") + 2);
+                                       strlen("Resent-To") + 2, TRUE);
                                if (first_address) {
                                        fprintf(fp, "Resent-To: ");
                                        first_address = FALSE;
@@ -3028,7 +3168,7 @@ static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
        if (compose->account->name && *compose->account->name) {
                compose_convert_header
                        (buf, sizeof(buf), compose->account->name,
-                        strlen("From: "));
+                        strlen("From: "), TRUE);
                fprintf(fp, "Resent-From: %s <%s>\n",
                        buf, compose->account->address);
        } else
@@ -3041,7 +3181,7 @@ static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
                g_strstrip(str);
                if (*str != '\0') {
                        compose_convert_header(buf, sizeof(buf), str,
-                                              strlen("Subject: "));
+                                              strlen("Subject: "), FALSE);
                        fprintf(fp, "Subject: %s\n", buf);
                }
        }
@@ -3111,7 +3251,8 @@ static gint compose_redirect_write_to_file(Compose *compose, const gchar *file)
                                        compose_convert_header
                                                (buf, sizeof(buf),
                                                 compose->account->name,
-                                                strlen("From: "));
+                                                strlen("From: "),
+                                                FALSE);
                                        fprintf(fdest, "%s <%s>",
                                                buf,
                                                compose->account->address);
@@ -3198,8 +3339,7 @@ static gint compose_clearsign_text(Compose *compose, gchar **text)
                return -1;
        }
 
-       if (canonicalize_file_replace(tmp_file) < 0 ||
-           compose_create_signers_list(compose, &key_list) < 0 ||
+       if (compose_create_signers_list(compose, &key_list) < 0 ||
            rfc2015_clearsign(tmp_file, key_list) < 0) {
                unlink(tmp_file);
                g_free(tmp_file);
@@ -3224,6 +3364,7 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
        size_t len;
        gchar *chars;
        gchar *buf;
+       gchar *canon_buf;
        const gchar *out_codeset;
        EncodingType encoding;
 
@@ -3252,7 +3393,15 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
                out_codeset = conv_get_outgoing_charset_str();
                if (!strcasecmp(out_codeset, CS_US_ASCII))
                        out_codeset = CS_ISO_8859_1;
-               encoding = procmime_get_encoding_for_charset(out_codeset);
+
+               if (prefs_common.encoding_method == CTE_BASE64)
+                       encoding = ENC_BASE64;
+               else if (prefs_common.encoding_method == CTE_QUOTED_PRINTABLE)
+                       encoding = ENC_QUOTED_PRINTABLE;
+               else if (prefs_common.encoding_method == CTE_8BIT)
+                       encoding = ENC_8BIT;
+               else
+                       encoding = procmime_get_encoding_for_charset(out_codeset);
 
 #if USE_GPGME
                if (!is_draft &&
@@ -3273,11 +3422,15 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
                buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
                if (!buf) {
                        AlertValue aval;
+                       gchar *msg;
 
+                       msg = g_strdup_printf(_("Can't convert the character encoding of the message from\n"
+                                               "%s to %s.\n"
+                                               "Send it anyway?"), src_codeset, out_codeset);
                        aval = alertpanel
-                               (_("Error"),
-                                _("Can't convert the character encoding of the message.\n"
-                                  "Send it anyway?"), _("Yes"), _("+No"), NULL);
+                               (_("Error"), msg, _("Yes"), _("+No"), NULL);
+                       g_free(msg);
+
                        if (aval != G_ALERTDEFAULT) {
                                g_free(chars);
                                fclose(fp);
@@ -3285,39 +3438,16 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
                                return -1;
                        } else {
                                buf = chars;
+                               out_codeset = src_codeset;
                                chars = NULL;
                        }
                }
        }
        g_free(chars);
 
-       /* Canonicalize line endings in the message text */
-       {
-               gchar *canon_buf, *out;
-               const gchar *p;
-               guint new_len = 0;
-
-               for (p = buf ; *p; ++p) {
-                       if (*p != '\r') {
-                               ++new_len;
-                               if (*p == '\n')
-                                       ++new_len;
-                       }
-               }
-
-               out = canon_buf = g_new(gchar, new_len + 1);
-               for (p = buf; *p; ++p) {
-                       if (*p != '\r') {
-                               if (*p == '\n')
-                                       *out++ = '\r';
-                               *out++ = *p;
-                       }
-               }
-               *out = '\0';
-
-               free(buf);
-               buf = canon_buf;
-       }
+       canon_buf = canonicalize_str(buf);
+       g_free(buf);
+       buf = canon_buf;
 
 #if USE_GPGME
        if (!is_draft && compose->use_signing && compose->gnupg_mode) {
@@ -3370,6 +3500,22 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
                        fputs(outbuf, fp);
                        fputc('\n', fp);
                }
+       } else if (encoding == ENC_QUOTED_PRINTABLE) {
+               gchar *outbuf;
+               size_t outlen;
+
+               outbuf = g_malloc(len * 4);
+               qp_encode_line(outbuf, buf);
+               outlen = strlen(outbuf);
+               if (fwrite(outbuf, sizeof(gchar), outlen, fp) != outlen) {
+                       FILE_OP_ERROR(file, "fwrite");
+                       fclose(fp);
+                       unlink(file);
+                       g_free(outbuf);
+                       g_free(buf);
+                       return -1;
+               }
+               g_free(outbuf);
        } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
                FILE_OP_ERROR(file, "fwrite");
                fclose(fp);
@@ -3389,8 +3535,10 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
        }
 
 #if USE_GPGME
-       if (is_draft)
+       if (is_draft) {
+               uncanonicalize_file_replace(file);
                return 0;
+       }
 
        if ((compose->use_signing && !compose->gnupg_mode) ||
            compose->use_encryption) {
@@ -3418,6 +3566,8 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
        }
 #endif /* USE_GPGME */
 
+       uncanonicalize_file_replace(file);
+
        return 0;
 }
 
@@ -3472,7 +3622,8 @@ static gint compose_remove_reedit_target(Compose *compose)
        g_return_val_if_fail(item != NULL, -1);
 
        if (procmsg_msg_exist(msginfo) &&
-           (item->stype == F_DRAFT || item->stype == F_QUEUE)) {
+           (item->stype == F_DRAFT || item->stype == F_QUEUE 
+            || msginfo == compose->autosaved_draft)) {
                if (folder_item_remove_msg(item, msginfo->msgnum) < 0) {
                        g_warning("can't remove the old message\n");
                        return -1;
@@ -3490,7 +3641,6 @@ void compose_remove_draft(Compose *compose)
 
        if (procmsg_msg_exist(msginfo)) {
                folder_item_remove_msg(drafts, msginfo->msgnum);
-               folder_update_item(drafts, TRUE);
        }
 
 }
@@ -3655,6 +3805,14 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                fprintf(fp, "RMID:%s\x7f%d\x7f%s\n", folderid, compose->replyinfo->msgnum, compose->replyinfo->msgid);
                g_free(folderid);
        }
+       /* Message-ID of message forwarding to */
+       if ((compose->fwdinfo != NULL) && (compose->fwdinfo->msgid != NULL)) {
+               gchar *folderid;
+               
+               folderid = folder_item_get_identifier(compose->fwdinfo->folder);
+               fprintf(fp, "FMID:%s\x7f%d\x7f%s\n", folderid, compose->fwdinfo->msgnum, compose->fwdinfo->msgid);
+               g_free(folderid);
+       }
        fprintf(fp, "\n");
 
        while (fgets(buf, sizeof(buf), src_fp) != NULL) {
@@ -3700,14 +3858,8 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
 
        if (compose->mode == COMPOSE_REEDIT) {
                compose_remove_reedit_target(compose);
-               if (compose->targetinfo &&
-                   compose->targetinfo->folder != queue)
-                       folder_update_item
-                               (compose->targetinfo->folder, TRUE);
        }
 
-       folder_update_item(queue, TRUE);
-
        if ((msgnum != NULL) && (item != NULL)) {
                *msgnum = num;
                *item = queue;
@@ -3727,9 +3879,6 @@ static void compose_write_attach(Compose *compose, FILE *fp)
 
        for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
             row++) {
-               gchar buf[BUFFSIZE];
-               gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
-
                if ((attach_fp = fopen(ainfo->file, "rb")) == NULL) {
                        g_warning("Can't open file %s\n", ainfo->file);
                        continue;
@@ -3742,7 +3891,7 @@ static void compose_write_attach(Compose *compose, FILE *fp)
                        fprintf(fp, "Content-Disposition: inline\n");
                } else {
                        compose_convert_header(filename, sizeof(filename),
-                                              ainfo->name, 12);
+                                              ainfo->name, 12, FALSE);
                        fprintf(fp, "Content-Type: %s;\n"
                                    " name=\"%s\"\n",
                                ainfo->content_type, filename);
@@ -3753,19 +3902,8 @@ static void compose_write_attach(Compose *compose, FILE *fp)
                fprintf(fp, "Content-Transfer-Encoding: %s\n\n",
                        procmime_get_encoding_str(ainfo->encoding));
 
-               switch (ainfo->encoding) {
-
-               case ENC_7BIT:
-               case ENC_8BIT:
-                       /* if (ainfo->encoding == ENC_7BIT) { */
-
-                       while (fgets(buf, sizeof(buf), attach_fp) != NULL) {
-                               strcrchomp(buf);
-                               fputs(buf, fp);
-                       }
-                       break;
-                       /* } else { */
-               case ENC_BASE64:
+               if (ainfo->encoding == ENC_BASE64) {
+                       gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
 
                        while ((len = fread(inbuf, sizeof(gchar),
                                            B64_LINE_SIZE, attach_fp))
@@ -3779,10 +3917,20 @@ static void compose_write_attach(Compose *compose, FILE *fp)
                                fputs(outbuf, fp);
                                fputc('\n', fp);
                        }
-                       break;
-               default:
-                       debug_print("Tried to write attachment in unsupported encoding type\n");
-                       break;
+               } else if (ainfo->encoding == ENC_QUOTED_PRINTABLE) {
+                       gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
+
+                       while (fgets(inbuf, sizeof(inbuf), attach_fp) != NULL) {
+                               qp_encode_line(outbuf, inbuf);
+                               fputs(outbuf, fp);
+                       }
+               } else {
+                       gchar buf[BUFFSIZE];
+
+                       while (fgets(buf, sizeof(buf), attach_fp) != NULL) {
+                               strcrchomp(buf);
+                               fputs(buf, fp);
+                       }
                }
 
                fclose(attach_fp);
@@ -3816,7 +3964,8 @@ static void compose_write_attach(Compose *compose, FILE *fp)
                        compose->to_list = address_list_append               \
                                (compose->to_list, str);                     \
                        compose_convert_header                               \
-                               (buf, sizeof(buf), str, strlen(header) + 2); \
+                               (buf, sizeof(buf), str, strlen(header) + 2,  \
+                                TRUE);                                      \
                        fprintf(fp, "%s: %s\n", header, buf);                \
                }                                                            \
        }                                                                    \
@@ -3858,7 +4007,7 @@ static gint compose_write_headers_from_headerlist(Compose *compose,
                        if (str[0] != '\0') {
                                compose_convert_header
                                        (buf, sizeof(buf), str,
-                                       strlen(header) + 2);
+                                       strlen(header) + 2, TRUE);
                                if (first_address) {
                                        fprintf(fp, "%s: ", header);
                                        first_address = FALSE;
@@ -3920,7 +4069,7 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                if (compose->account->name && *compose->account->name) {
                        compose_convert_header
                                (buf, sizeof(buf), compose->account->name,
-                                strlen("From: "));
+                                strlen("From: "), TRUE);
                        QUOTE_IF_REQUIRED(name, buf);
                        fprintf(fp, "From: %s <%s>\n",
                                name, compose->account->address);
@@ -3951,7 +4100,8 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                                        newsgroup_list_append
                                                (compose->newsgroup_list, str);
                                compose_convert_header(buf, sizeof(buf), str,
-                                                      strlen("Newsgroups: "));
+                                                      strlen("Newsgroups: "),
+                                                      TRUE);
                                fprintf(fp, "Newsgroups: %s\n", buf);
                        }
                }
@@ -3981,7 +4131,7 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                g_strstrip(str);
                if (*str != '\0') {
                        compose_convert_header(buf, sizeof(buf), str,
-                                              strlen("Subject: "));
+                                              strlen("Subject: "), FALSE);
                        fprintf(fp, "Subject: %s\n", buf);
                }
        }
@@ -4012,7 +4162,8 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                        remove_space(str);
                        if (*str != '\0') {
                                compose_convert_header(buf, sizeof(buf), str,
-                                                      strlen("Followup-To: "));
+                                                      strlen("Followup-To: "),
+                                                      TRUE);
                                fprintf(fp, "Followup-To: %s\n", buf);
                        }
                }
@@ -4028,7 +4179,8 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                        g_strstrip(str);
                        if (*str != '\0') {
                                compose_convert_header(buf, sizeof(buf), str,
-                                                      strlen("Reply-To: "));
+                                                      strlen("Reply-To: "),
+                                                      TRUE);
                                fprintf(fp, "Reply-To: %s\n", buf);
                        }
                }
@@ -4039,13 +4191,14 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
            !IS_IN_CUSTOM_HEADER("Organization")) {
                compose_convert_header(buf, sizeof(buf),
                                       compose->account->organization,
-                                      strlen("Organization: "));
+                                      strlen("Organization: "), FALSE);
                fprintf(fp, "Organization: %s\n", buf);
        }
 
        /* Program version and system info */
        /* uname(&utsbuf); */
-       if (g_slist_length(compose->to_list) && !IS_IN_CUSTOM_HEADER("X-Mailer")) {
+       if (g_slist_length(compose->to_list) && !IS_IN_CUSTOM_HEADER("X-Mailer") &&
+           !compose->newsgroup_list) {
                fprintf(fp, "X-Mailer: %s (GTK+ %d.%d.%d; %s)\n",
                        prog_version,
                        gtk_major_version, gtk_minor_version, gtk_micro_version,
@@ -4082,7 +4235,7 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                                compose_convert_header
                                        (buf, sizeof(buf),
                                         chdr->value ? chdr->value : "",
-                                        strlen(chdr->name) + 2);
+                                        strlen(chdr->name) + 2, FALSE);
                                fprintf(fp, "%s: %s\n", chdr->name, buf);
                        }
                }
@@ -4129,7 +4282,10 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                if (compose->return_receipt) {
                        if (compose->account->name
                            && *compose->account->name) {
-                               compose_convert_header(buf, sizeof(buf), compose->account->name, strlen("Disposition-Notification-To: "));
+                               compose_convert_header(buf, sizeof(buf), 
+                                                      compose->account->name, 
+                                                      strlen("Disposition-Notification-To: "),
+                                                      TRUE);
                                fprintf(fp, "Disposition-Notification-To: %s <%s>\n", buf, compose->account->address);
                        } else
                                fprintf(fp, "Disposition-Notification-To: %s\n", compose->account->address);
@@ -4184,16 +4340,16 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
 #undef IS_IN_CUSTOM_HEADER
 
 static void compose_convert_header(gchar *dest, gint len, gchar *src,
-                                  gint header_len)
+                                  gint header_len, gboolean addr_field)
 {
        g_return_if_fail(src != NULL);
        g_return_if_fail(dest != NULL);
 
        if (len < 1) return;
 
-       remove_return(src);
+       g_strchomp(src);
 
-       conv_encode_header(dest, len, src, header_len);
+       conv_encode_header(dest, len, src, header_len, addr_field);
 }
 
 static void compose_generate_msgid(Compose *compose, gchar *buf, gint len)
@@ -4658,7 +4814,8 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        handlebox = gtk_handle_box_new();
        gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
 
-       compose_toolbar_create(compose, handlebox);
+       compose->toolbar = toolbar_create(TOOLBAR_COMPOSE, handlebox,
+                                         (gpointer)compose);
 
        vbox2 = gtk_vbox_new(FALSE, 2);
        gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
@@ -4849,7 +5006,7 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        }
 #endif
 
-       update_compose_actions_menu(ifactory, "/Tools/Actions", compose);
+       action_update_compose_menu(ifactory, compose);
 
 
        undostruct = undo_init(text);
@@ -4884,6 +5041,7 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
 
        compose->targetinfo = NULL;
        compose->replyinfo  = NULL;
+       compose->fwdinfo    = NULL;
 
        compose->replyto     = NULL;
        compose->cc          = NULL;
@@ -4905,18 +5063,19 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        compose->modified = FALSE;
 
        compose->return_receipt = FALSE;
-       compose->paste_as_quotation = FALSE;
 
        compose->to_list        = NULL;
        compose->newsgroup_list = NULL;
 
+       compose->undostruct = undostruct;
+
+       compose->sig_str = NULL;
+
        compose->exteditor_file    = NULL;
        compose->exteditor_pid     = -1;
        compose->exteditor_readdes = -1;
        compose->exteditor_tag     = -1;
 
-       compose->redirect_filename = NULL;
-       compose->undostruct = undostruct;
 #if USE_ASPELL
        menu_set_sensitive(ifactory, "/Spelling", FALSE);
        if (mode != COMPOSE_REDIRECT) {
@@ -4949,7 +5108,7 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        }
 #endif
 
-       compose_select_account(compose, account);
+       compose_select_account(compose, account, TRUE);
 
 #if USE_ASPELL
         compose->gtkaspell      = gtkaspell;
@@ -4971,7 +5130,7 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
                gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(compose->header_last->combo)->entry), prefs_common.trans_hdr ? _("Newsgroups:") : "Newsgroups:");
 
        addressbook_set_target_compose(compose);
-       update_compose_actions_menu(ifactory, "/Tools/Actions", compose);
+       action_update_compose_menu(ifactory, compose);
        
        if (mode != COMPOSE_REDIRECT)
                compose_set_template_menu(compose);
@@ -4993,230 +5152,12 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
 #if USE_GPGME
        activate_gnupg_mode(compose, account);
 #endif 
-
-       common_toolbar_set_style(compose, TOOLBAR_COMPOSE);
-
+       toolbar_set_style(compose->toolbar->toolbar, compose->handlebox, prefs_common.toolbar_style);
        gtk_widget_show(window);
        
        return compose;
 }
 
-static void toolbar_compose_buttons_cb(GtkWidget   *widget, 
-                                      ToolbarItem *item)
-{
-       struct {
-               gint   index;
-               void (*func)(GtkWidget *widget, gpointer data);
-       } compose_action[] = {
-               { A_SEND,        toolbar_send_cb                        },
-               { A_SENDL,       toolbar_send_later_cb                  },
-               { A_DRAFT,       toolbar_draft_cb                       },
-               { A_INSERT,      toolbar_insert_cb                      },
-               { A_ATTACH,      toolbar_attach_cb                      },
-               { A_SIG,         toolbar_sig_cb                         },
-               { A_EXTEDITOR,   toolbar_ext_editor_cb                  },
-               { A_LINEWRAP,    toolbar_linewrap_cb                    },
-               { A_ADDRBOOK,    toolbar_address_cb                     },
-               { A_SYL_ACTIONS, common_toolbar_actions_execute_cb      }};
-
-
-       gint num_items = sizeof(compose_action)/sizeof(compose_action[0]);
-       gint i;
-       for (i = 0; i < num_items; i++) {
-               
-               if (compose_action[i].index == item->index) {
-                       if (item->index == A_SYL_ACTIONS)
-                               compose_action[i].func(widget, (gpointer)item->parent);
-                       else /* this won`t be necessary if everything is moved to toolbar.c */
-                               compose_action[i].func(widget, (gpointer)item->parent->data);
-                       break;
-               }
-       }
-}
-
-static void compose_toolbar_update(Compose *compose)
-{
-       gtk_container_remove(GTK_CONTAINER(compose->handlebox), 
-                            GTK_WIDGET(compose->toolbar->toolbar));
-       
-       compose->toolbar->toolbar       = NULL;
-       compose->toolbar->send_btn      = NULL;
-       compose->toolbar->sendl_btn     = NULL;
-       compose->toolbar->draft_btn     = NULL;
-       compose->toolbar->insert_btn    = NULL;
-       compose->toolbar->attach_btn    = NULL;
-       compose->toolbar->sig_btn       = NULL; 
-       compose->toolbar->exteditor_btn = NULL; 
-       compose->toolbar->linewrap_btn  = NULL; 
-       compose->toolbar->addrbook_btn  = NULL; 
-
-       TOOLBAR_DESTROY_ITEMS(compose->toolbar->item_list);     
-       TOOLBAR_DESTROY_ACTIONS(compose->toolbar->action_list);
-       compose_toolbar_create(compose, compose->handlebox);    
-}
-
-static void compose_toolbar_create(Compose   *compose, 
-                                  GtkWidget *container)
-{
-       ToolbarItem *toolbar_item;
-
-       GtkWidget *toolbar;
-       GtkWidget *icon_wid  = NULL;
-       GtkWidget *item;
-       GtkTooltips *toolbar_tips;
-       ToolbarSylpheedActions *action_item;
-       GSList *cur;
-       GSList *toolbar_list;
-       toolbar_tips = gtk_tooltips_new();
-       
-       toolbar_read_config_file(TOOLBAR_COMPOSE);
-       toolbar_list = toolbar_get_list(TOOLBAR_COMPOSE);
-
-       compose->toolbar = g_new0(Toolbar, 1); 
-       
-       toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
-                                 GTK_TOOLBAR_BOTH);
-       gtk_container_add(GTK_CONTAINER(container), toolbar);
-       gtk_container_set_border_width(GTK_CONTAINER(container), 2);
-       gtk_toolbar_set_button_relief(GTK_TOOLBAR(toolbar), GTK_RELIEF_NONE);
-       gtk_toolbar_set_space_style(GTK_TOOLBAR(toolbar),
-                                   GTK_TOOLBAR_SPACE_LINE);
-       
-       for (cur = toolbar_list; cur != NULL; cur = cur->next) {
-
-               if (g_strcasecmp(((ToolbarItem*)cur->data)->file, SEPARATOR) == 0) {
-                       gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
-                       continue;
-               }
-               
-               toolbar_item = g_new0(ToolbarItem, 1); 
-               toolbar_item->file = g_strdup(((ToolbarItem*)cur->data)->file);
-               toolbar_item->text = g_strdup(((ToolbarItem*)cur->data)->text);
-               toolbar_item->index = ((ToolbarItem*)cur->data)->index;
-
-               toolbar_item->parent = g_new0(ToolbarParent, 1);
-               toolbar_item->parent->data = (gpointer)compose;
-               toolbar_item->parent->type = TOOLBAR_COMPOSE;
-
-               /* collect toolbar items in list to keep track */
-               compose->toolbar->item_list = g_slist_append(compose->toolbar->item_list, 
-                                                            toolbar_item);
-
-               icon_wid = stock_pixmap_widget(container, stock_pixmap_get_icon(toolbar_item->file));
-               item  = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
-                                               toolbar_item->text,
-                                               (""),
-                                               (""),
-                                               icon_wid, toolbar_compose_buttons_cb, 
-                                               toolbar_item);
-               
-               switch (toolbar_item->index) {
-               case A_SEND:
-                       compose->toolbar->send_btn = item;
-                       gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
-                                            compose->toolbar->send_btn, 
-                                            _("Send Message"), NULL);
-                       break;
-               case A_SENDL:
-                       compose->toolbar->sendl_btn = item;
-                       gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
-                                            compose->toolbar->sendl_btn,
-                                            _("Put into queue folder and send later"), NULL);
-                       break;
-               case A_DRAFT:
-                       compose->toolbar->draft_btn = item; 
-                       gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
-                                            compose->toolbar->draft_btn,
-                                            _("Save to draft folder"), NULL);
-                       break;
-               case A_INSERT:
-                       compose->toolbar->insert_btn = item; 
-                       gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
-                                            compose->toolbar->insert_btn,
-                                            _("Insert file"), NULL);
-                       break;
-               case A_ATTACH:
-                       compose->toolbar->attach_btn = item;
-                       gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
-                                            compose->toolbar->attach_btn,
-                                            _("Attach file"), NULL);
-                       break;
-               case A_SIG:
-                       compose->toolbar->sig_btn = item;
-                       gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
-                                            compose->toolbar->sig_btn,
-                                            _("Insert signature"), NULL);
-                       break;
-               case A_EXTEDITOR:
-                       compose->toolbar->exteditor_btn = item;
-                       gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
-                                            compose->toolbar->exteditor_btn,
-                                            _("Edit with external editor"), NULL);
-                       break;
-               case A_LINEWRAP:
-                       compose->toolbar->linewrap_btn = item;
-                       gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
-                                            compose->toolbar->linewrap_btn,
-                                            _("Wrap all long lines"), NULL);
-                       break;
-               case A_ADDRBOOK:
-                       compose->toolbar->addrbook_btn = item;
-                       gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
-                                            compose->toolbar->addrbook_btn,
-                                            _("Address book"), NULL);
-                       break;
-               case A_SYL_ACTIONS:
-                       action_item = g_new0(ToolbarSylpheedActions, 1);
-                       action_item->widget = item;
-                       action_item->name   = g_strdup(toolbar_item->text);
-
-                       compose->toolbar->action_list = 
-                               g_slist_append(compose->toolbar->action_list,
-                                              action_item);
-
-                       gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
-                                            item,
-                                            action_item->name, NULL);
-
-                       gtk_widget_show(item);
-                       break;
-               default:
-                       break;
-               }
-       }
-
-       compose->toolbar->toolbar = toolbar;
-
-       gtk_widget_show_all(toolbar);
-}
-
-static void compose_toolbar_set_sensitive (Compose * compose, gboolean sensitive)
-{
-       GSList *items = compose->toolbar->action_list;
-       if (compose->toolbar->send_btn)
-               gtk_widget_set_sensitive(compose->toolbar->send_btn, sensitive);
-       if (compose->toolbar->sendl_btn)
-               gtk_widget_set_sensitive(compose->toolbar->sendl_btn, sensitive);
-       if (compose->toolbar->draft_btn )
-               gtk_widget_set_sensitive(compose->toolbar->draft_btn , sensitive);
-       if (compose->toolbar->insert_btn )
-               gtk_widget_set_sensitive(compose->toolbar->insert_btn , sensitive);
-       if (compose->toolbar->attach_btn)
-               gtk_widget_set_sensitive(compose->toolbar->attach_btn, sensitive);
-       if (compose->toolbar->sig_btn)
-               gtk_widget_set_sensitive(compose->toolbar->sig_btn, sensitive);
-       if (compose->toolbar->exteditor_btn)
-               gtk_widget_set_sensitive(compose->toolbar->exteditor_btn, sensitive);
-       if (compose->toolbar->linewrap_btn)
-               gtk_widget_set_sensitive(compose->toolbar->linewrap_btn, sensitive);
-       if (compose->toolbar->addrbook_btn)
-               gtk_widget_set_sensitive(compose->toolbar->addrbook_btn, sensitive);
-       for (; items != NULL; items = g_slist_next(items)) {
-               ToolbarSylpheedActions *item = (ToolbarSylpheedActions *)items->data;
-               gtk_widget_set_sensitive(item->widget, sensitive);
-       }
-}
-
 static GtkWidget *compose_account_option_menu_create(Compose *compose)
 {
        GList *accounts;
@@ -5247,7 +5188,7 @@ static GtkWidget *compose_account_option_menu_create(Compose *compose)
                else
                        name = g_strdup_printf("%s: %s",
                                               ac->account_name, ac->address);
-               MENUITEM_ADD(menu, menuitem, name, ac);
+               MENUITEM_ADD(menu, menuitem, name, ac->account_id);
                g_free(name);
                gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
                                   GTK_SIGNAL_FUNC(account_activated),
@@ -5379,8 +5320,7 @@ void compose_reflect_prefs_pixmap_theme(void)
 
        for (cur = compose_list; cur != NULL; cur = cur->next) {
                compose = (Compose *)cur->data;
-               compose_toolbar_update(compose);
-               common_toolbar_set_style(compose, TOOLBAR_COMPOSE);
+               toolbar_update(TOOLBAR_COMPOSE, compose);
        }
 }
 
@@ -5406,9 +5346,9 @@ static void compose_template_apply(Compose *compose, Template *tmpl,
                compose_entry_append(compose, tmpl->bcc, COMPOSE_BCC);
 
        if (replace)
-               gtk_stext_clear(GTK_STEXT(compose->text));
+               gtkut_stext_clear(GTK_STEXT(compose->text));
 
-       if (compose->replyinfo == NULL) {
+       if ((compose->replyinfo == NULL) && (compose->fwdinfo == NULL)) {
                parsed_str = compose_quote_fmt(compose, NULL, tmpl->value,
                                               NULL, NULL);
        } else {
@@ -5417,12 +5357,18 @@ static void compose_template_apply(Compose *compose, Template *tmpl,
                else
                        qmark = "> ";
 
-               parsed_str = compose_quote_fmt(compose, compose->replyinfo,
-                                              tmpl->value, qmark, NULL);
+               if (compose->replyinfo != NULL)
+                       parsed_str = compose_quote_fmt(compose, compose->replyinfo,
+                                                      tmpl->value, qmark, NULL);
+               else if (compose->fwdinfo != NULL)
+                       parsed_str = compose_quote_fmt(compose, compose->fwdinfo,
+                                                      tmpl->value, qmark, NULL);
+               else
+                       parsed_str = NULL;
        }
 
-       if (replace && parsed_str && prefs_common.auto_sig)
-               compose_insert_sig(compose);
+       if (replace && parsed_str && compose->account->auto_sig)
+               compose_insert_sig(compose, FALSE);
 
        if (replace && parsed_str) {
                gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
@@ -5454,6 +5400,7 @@ static void compose_destroy(Compose *compose)
 
        procmsg_msginfo_free(compose->targetinfo);
        procmsg_msginfo_free(compose->replyinfo);
+       procmsg_msginfo_free(compose->fwdinfo);
 
        g_free(compose->replyto);
        g_free(compose->cc);
@@ -5470,6 +5417,10 @@ static void compose_destroy(Compose *compose)
 
        if (compose->redirect_filename)
                g_free(compose->redirect_filename);
+       if (compose->undostruct)
+               undo_destroy(compose->undostruct);
+
+       g_free(compose->sig_str);
 
        g_free(compose->exteditor_file);
 
@@ -5491,8 +5442,7 @@ static void compose_destroy(Compose *compose)
 
        gtk_widget_destroy(compose->paned);
 
-       TOOLBAR_DESTROY_ITEMS(compose->toolbar->item_list);
-       TOOLBAR_DESTROY_ACTIONS(compose->toolbar->action_list);
+       toolbar_destroy(compose->toolbar);
        g_free(compose->toolbar);
        g_free(compose);
 
@@ -5554,9 +5504,11 @@ static void compose_attach_property(Compose *compose)
 
        optmenu = GTK_OPTION_MENU(attach_prop.encoding_optmenu);
        if (ainfo->encoding == ENC_UNKNOWN)
-               gtk_option_menu_set_history(optmenu, ENC_BASE64);
+               menu_select_by_data(GTK_MENU(gtk_option_menu_get_menu(optmenu)),
+                                   GINT_TO_POINTER(ENC_BASE64));
        else
-               gtk_option_menu_set_history(optmenu, ainfo->encoding);
+               menu_select_by_data(GTK_MENU(gtk_option_menu_get_menu(optmenu)),
+                                   GINT_TO_POINTER(ainfo->encoding));
 
        gtk_entry_set_text(GTK_ENTRY(attach_prop.mimetype_entry),
                           ainfo->content_type ? ainfo->content_type : "");
@@ -5677,7 +5629,7 @@ static void compose_attach_property_create(gboolean *cancelled)
        window = gtk_window_new(GTK_WINDOW_DIALOG);
        gtk_widget_set_usize(window, 480, -1);
        gtk_container_set_border_width(GTK_CONTAINER(window), 8);
-       gtk_window_set_title(GTK_WINDOW(window), _("Property"));
+       gtk_window_set_title(GTK_WINDOW(window), _("Properties"));
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);
        gtk_signal_connect(GTK_OBJECT(window), "delete_event",
@@ -5745,8 +5697,10 @@ static void compose_attach_property_create(gboolean *cancelled)
 #if 0
        gtk_widget_set_sensitive(menuitem, FALSE);
 #endif
-       MENUITEM_ADD(optmenu_menu, menuitem, "quoted-printable", ENC_QUOTED_PRINTABLE);
-       gtk_widget_set_sensitive(menuitem, FALSE);
+       MENUITEM_ADD(optmenu_menu, menuitem, "quoted-printable",
+                    ENC_QUOTED_PRINTABLE);
+       gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), optmenu_menu);
+
        MENUITEM_ADD(optmenu_menu, menuitem, "base64", ENC_BASE64);
 
        gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), optmenu_menu);
@@ -5853,20 +5807,20 @@ static void compose_exec_ext_editor(Compose *compose)
                close(pipe_fds[0]);
 
                if (compose_write_body_to_file(compose, tmp) < 0) {
-                       fd_write(pipe_fds[1], "2\n", 2);
+                       fd_write_all(pipe_fds[1], "2\n", 2);
                        _exit(1);
                }
 
                pid_ed = compose_exec_ext_editor_real(tmp);
                if (pid_ed < 0) {
-                       fd_write(pipe_fds[1], "1\n", 2);
+                       fd_write_all(pipe_fds[1], "1\n", 2);
                        _exit(1);
                }
 
                /* wait until editor is terminated */
                waitpid(pid_ed, NULL, 0);
 
-               fd_write(pipe_fds[1], "0\n", 2);
+               fd_write_all(pipe_fds[1], "0\n", 2);
 
                close(pipe_fds[1]);
                _exit(0);
@@ -6156,68 +6110,18 @@ static gboolean compose_edit_size_alloc(GtkEditable *widget,
        return TRUE;
 }
 
-static void toolbar_send_cb(GtkWidget *widget, gpointer data)
-{
-       compose_send_cb(data, 0, NULL);
-}
-
-static void toolbar_send_later_cb(GtkWidget *widget, gpointer data)
-{
-       compose_send_later_cb(data, 0, NULL);
-}
-
-static void toolbar_draft_cb(GtkWidget *widget, gpointer data)
-{
-       compose_draft_cb(data, 0, NULL);
-}
-
-static void toolbar_insert_cb(GtkWidget *widget, gpointer data)
-{
-       compose_insert_file_cb(data, 0, NULL);
-}
-
-static void toolbar_attach_cb(GtkWidget *widget, gpointer data)
-{
-       compose_attach_cb(data, 0, NULL);
-}
-
-static void toolbar_sig_cb(GtkWidget *widget, gpointer data)
-{
-       Compose *compose = (Compose *)data;
-
-       compose_insert_sig(compose);
-}
-
-static void toolbar_ext_editor_cb(GtkWidget *widget, gpointer data)
-{
-       Compose *compose = (Compose *)data;
-
-       compose_exec_ext_editor(compose);
-}
-
-static void toolbar_linewrap_cb(GtkWidget *widget, gpointer data)
-{
-       Compose *compose = (Compose *)data;
-
-       compose_wrap_line(compose);
-}
-
-static void toolbar_address_cb(GtkWidget *widget, gpointer data)
-{
-       compose_address_cb(data, 0, NULL);
-}
-
 static void account_activated(GtkMenuItem *menuitem, gpointer data)
 {
        Compose *compose = (Compose *)data;
 
        PrefsAccount *ac;
 
-       ac = (PrefsAccount *)gtk_object_get_user_data(GTK_OBJECT(menuitem));
+       ac = account_find_from_id(
+               GPOINTER_TO_INT(gtk_object_get_user_data(GTK_OBJECT(menuitem))));
        g_return_if_fail(ac != NULL);
 
        if (ac != compose->account)
-               compose_select_account(compose, ac);
+               compose_select_account(compose, ac, FALSE);
 }
 
 static void attach_selected(GtkCList *clist, gint row, gint column,
@@ -6271,7 +6175,7 @@ static void attach_key_pressed(GtkWidget *widget, GdkEventKey *event,
 static void compose_allow_user_actions (Compose *compose, gboolean allow)
 {
        GtkItemFactory *ifactory = gtk_item_factory_from_widget(compose->menubar);
-       compose_toolbar_set_sensitive(compose, allow);
+       toolbar_comp_set_sensitive(compose, allow);
        menu_set_sensitive(ifactory, "/File", allow);
        menu_set_sensitive(ifactory, "/Edit", allow);
        menu_set_sensitive(ifactory, "/Spelling", allow);
@@ -6351,23 +6255,18 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
 
        if (compose->mode == COMPOSE_REEDIT) {
                compose_remove_reedit_target(compose);
-               if (compose->targetinfo &&
-                   compose->targetinfo->folder != draft)
-                       folder_update_item(compose->targetinfo->folder,
-                                              TRUE);
        }
 
        newmsginfo = folder_item_get_msginfo(draft, msgnum);
        if (newmsginfo) {
                procmsg_msginfo_unset_flags(newmsginfo, ~0, ~0);
                procmsg_msginfo_set_flags(newmsginfo, 0, MSG_DRAFT);
-               folder_update_item(draft, TRUE);
                procmsg_msginfo_free(newmsginfo);
        }
        
        lock = FALSE;
 
-       /* 0: quit editing  1: keep editing */
+       /* 0: quit editing  1: keep editing  2: keep editing (autosave) */
        if (action == 0)
                gtk_widget_destroy(compose->window);
        else {
@@ -6391,6 +6290,10 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
                compose->targetinfo->mtime = s.st_mtime;
                compose->targetinfo->folder = draft;
                compose->mode = COMPOSE_REEDIT;
+               
+               if (action == 2) {
+                       compose->autosaved_draft = compose->targetinfo;
+               }
        }
 }
 
@@ -6437,6 +6340,14 @@ static void compose_insert_file_cb(gpointer data, guint action,
        }
 }
 
+static void compose_insert_sig_cb(gpointer data, guint action,
+                                 GtkWidget *widget)
+{
+       Compose *compose = (Compose *)data;
+
+       compose_insert_sig(compose, FALSE);
+}
+
 static gint compose_delete_cb(GtkWidget *widget, GdkEventAny *event,
                              gpointer data)
 {
@@ -6560,10 +6471,18 @@ static void compose_paste_as_quote_cb(Compose *compose)
 {
        if (compose->focused_editable &&
            GTK_WIDGET_HAS_FOCUS(compose->focused_editable)) {
-               compose->paste_as_quotation = TRUE;
+               /* let text_insert() (called directly or at a later time
+                * after the gtk_editable_paste_clipboard) know that 
+                * text is to be inserted as a quotation. implemented
+                * by using a simple refcount... */
+               gint paste_as_quotation = GPOINTER_TO_INT(gtk_object_get_data(
+                                               GTK_OBJECT(compose->focused_editable),
+                                               "paste_as_quotation"));
+               gtk_object_set_data(GTK_OBJECT(compose->focused_editable),
+                                   "paste_as_quotation",
+                                   GINT_TO_POINTER(paste_as_quotation + 1));
                gtk_editable_paste_clipboard
                        (GTK_EDITABLE(compose->focused_editable));
-               compose->paste_as_quotation = FALSE;
        }
 }
 
@@ -7017,11 +6936,13 @@ static void text_inserted(GtkWidget *widget, const gchar *text,
                          gint length, gint *position, Compose *compose)
 {
        GtkEditable *editable = GTK_EDITABLE(widget);
+       gint paste_as_quotation = GPOINTER_TO_INT(gtk_object_get_data
+                               (GTK_OBJECT(widget), "paste_as_quotation"));
 
        gtk_signal_handler_block_by_func(GTK_OBJECT(widget),
                                         GTK_SIGNAL_FUNC(text_inserted),
                                         compose);
-       if (compose->paste_as_quotation) {
+       if (paste_as_quotation) {
                gchar *new_text;
                gchar *qmark;
                gint pos;
@@ -7037,6 +6958,8 @@ static void text_inserted(GtkWidget *widget, const gchar *text,
                gtk_editable_set_position(editable, pos);
                *position = pos;
                g_free(new_text);
+               gtk_object_set_data(GTK_OBJECT(widget), "paste_as_quotation",
+                                   GINT_TO_POINTER(paste_as_quotation - 1));
        } else
                gtk_editable_insert_text(editable, text, length, position);
 
@@ -7051,7 +6974,7 @@ static void text_inserted(GtkWidget *widget, const gchar *text,
        
        if (prefs_common.autosave && 
            gtk_stext_get_length(GTK_STEXT(widget)) % prefs_common.autosave_length == 0)
-               compose_draft_cb((gpointer)compose, 1, NULL);
+               compose_draft_cb((gpointer)compose, 2, NULL);
 }
 
 static gboolean compose_send_control_enter(Compose *compose)