2004-09-29 [colin] 0.9.12cvs114.1
[claws.git] / src / compose.c
index e1adb887f42fdd69bc54acb7e87e6583463b2dcb..f3c620fdc8d6d474c136428e50baf88db5a2128e 100644 (file)
@@ -248,8 +248,7 @@ static gint compose_queue_sub                       (Compose        *compose,
                                                 gboolean       check_subject);
 static void compose_write_attach               (Compose        *compose,
                                                 FILE           *fp);
-static gint compose_write_headers              (Compose        *compose,
-                                                FILE           *fp,
+static gchar *compose_get_header               (Compose        *compose,
                                                 const gchar    *charset,
                                                 EncodingType    encoding,
                                                 gboolean        is_draft);
@@ -422,6 +421,8 @@ static void compose_set_gnupg_mode_cb       (gpointer        data,
 static void compose_update_gnupg_mode_menu_item(Compose * compose);
 static void activate_gnupg_mode        (Compose *compose, 
                                         PrefsAccount *account);
+static void compose_use_signing(Compose *compose, gboolean use_signing);
+static void compose_use_encryption(Compose *compose, gboolean use_encryption);
 #endif
 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
                                             GtkWidget *widget);
@@ -483,12 +484,11 @@ gboolean compose_headerentry_changed_cb      (GtkWidget          *entry,
 gboolean compose_headerentry_key_press_event_cb(GtkWidget             *entry,
                                            GdkEventKey        *event,
                                            ComposeHeaderEntry *headerentry);
-static gboolean compose_headerentry_button_pressed (GtkWidget *entry, 
-                                                   GdkEventButton *event,
-                                                   gpointer data);
 
 static void compose_show_first_last_header (Compose *compose, gboolean show_first);
 
+static void compose_allow_user_actions (Compose *compose, gboolean allow);
+
 #if USE_ASPELL
 static void compose_check_all             (Compose *compose);
 static void compose_highlight_all         (Compose *compose);
@@ -511,6 +511,9 @@ static GtkItemFactoryEntry compose_popup_entries[] =
 static GtkItemFactoryEntry compose_entries[] =
 {
        {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
+       {N_("/_File/_Save"),
+                                               "<control>S", compose_draft_cb, 1, NULL},
+       {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
        {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_cb,  0, NULL},
@@ -644,11 +647,6 @@ static GtkItemFactoryEntry compose_entries[] =
                                        compose_send_cb, 0, NULL},
        {N_("/_Message/Send _later"),   "<shift><control>S",
                                        compose_send_later_cb,  0, NULL},
-       {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
-       {N_("/_Message/Save to _draft folder"),
-                                       "<shift><control>D", compose_draft_cb, 0, NULL},
-       {N_("/_Message/Save and _keep editing"),
-                                       "<control>S", compose_draft_cb, 1, NULL},
 #if 0 /* NEW COMPOSE GUI */
        {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
        {N_("/_Message/_To"),           NULL, compose_toggle_to_cb     , 0, "<ToggleItem>"},
@@ -1227,34 +1225,62 @@ void compose_reedit(MsgInfo *msginfo)
        GtkTextIter iter;
        FILE *fp;
        gchar buf[BUFFSIZE];
+#ifdef USE_GPGME
+       gboolean use_signing = FALSE;
+       gboolean use_encryption = FALSE;
+       gboolean gnupg_mode = FALSE;
+#endif
 
        g_return_if_fail(msginfo != NULL);
        g_return_if_fail(msginfo->folder != NULL);
 
         if (msginfo->folder->stype == F_QUEUE || msginfo->folder->stype == F_DRAFT) {
                gchar queueheader_buf[BUFFSIZE];
-               gint id;
+               gint id, param;
 
                /* Select Account from queue headers */
                if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
                                             sizeof(queueheader_buf), "X-Sylpheed-Account-Id:")) {
-                       id = atoi(&queueheader_buf[22]);
+                       id = atoi(&queueheader_buf[strlen("X-Sylpheed-Account-Id:")]);
                        account = account_find_from_id(id);
                }
                if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
                                             sizeof(queueheader_buf), "NAID:")) {
-                       id = atoi(&queueheader_buf[5]);
+                       id = atoi(&queueheader_buf[strlen("NAID:")]);
                        account = account_find_from_id(id);
                }
                if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
                                                    sizeof(queueheader_buf), "MAID:")) {
-                       id = atoi(&queueheader_buf[5]);
+                       id = atoi(&queueheader_buf[strlen("MAID:")]);
                        account = account_find_from_id(id);
                }
                if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
                                                                sizeof(queueheader_buf), "S:")) {
                        account = account_find_from_address(queueheader_buf);
                }
+#ifdef USE_GPGME               
+               if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
+                                            sizeof(queueheader_buf), "X-Sylpheed-Sign:")) {
+                       param = atoi(&queueheader_buf[strlen("X-Sylpheed-Sign:")]);
+                       use_signing = param;
+                       
+               }
+               if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
+                                            sizeof(queueheader_buf), "X-Sylpheed-Encrypt:")) {
+                       param = atoi(&queueheader_buf[strlen("X-Sylpheed-Encrypt:")]);
+                       use_encryption = param;
+               }
+               if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
+                                            sizeof(queueheader_buf), "X-Sylpheed-Gnupg-Mode:")) {
+                       param = atoi(&queueheader_buf[strlen("X-Sylpheed-Gnupg-Mode:")]);
+                       gnupg_mode = param;
+               }
+#endif         
+               if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
+                                            sizeof(queueheader_buf), "X-Priority: ")) {
+                       param = atoi(&queueheader_buf[strlen("X-Priority: ")]); /* mind the space */
+                       compose->priority = param;
+               }
        } else 
                account = msginfo->folder->folder->account;
 
@@ -1269,6 +1295,11 @@ void compose_reedit(MsgInfo *msginfo)
        g_return_if_fail(account != NULL);
 
        compose = compose_create(account, COMPOSE_REEDIT);
+#ifdef USE_GPGME
+       compose->gnupg_mode = gnupg_mode;
+       compose_use_signing(compose, use_signing);
+       compose_use_encryption(compose, use_encryption);
+#endif
        compose->targetinfo = procmsg_msginfo_copy(msginfo);
 
         if (msginfo->folder->stype == F_QUEUE
@@ -1376,12 +1407,11 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo)
        menu_set_sensitive(ifactory, "/Property...", FALSE);
 
        ifactory = gtk_item_factory_from_widget(compose->menubar);
+       menu_set_sensitive(ifactory, "/File/Save", FALSE);
        menu_set_sensitive(ifactory, "/File/Insert file", FALSE);
        menu_set_sensitive(ifactory, "/File/Attach file", FALSE);
        menu_set_sensitive(ifactory, "/File/Insert signature", FALSE);
        menu_set_sensitive(ifactory, "/Edit", FALSE);
-       menu_set_sensitive(ifactory, "/Message/Save to draft folder", FALSE);
-       menu_set_sensitive(ifactory, "/Message/Save and keep editing", FALSE);
 #if USE_GPGME
        menu_set_sensitive(ifactory, "/Message/Sign", FALSE);
        menu_set_sensitive(ifactory, "/Message/Encrypt", FALSE);
@@ -2140,7 +2170,11 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
 
        while (fgets(buf, sizeof(buf), fp) != NULL) {
                const gchar *cur_encoding = conv_get_current_charset_str();
-               gchar *str = conv_codeset_strdup(buf, cur_encoding, CS_UTF_8);
+               gchar *str = NULL;
+               if (!g_utf8_validate(buf, -1, NULL))
+                       str = conv_codeset_strdup(buf, cur_encoding, CS_UTF_8);
+               else
+                       str = g_strdup(buf);
 
                if (!str) continue;
 
@@ -2275,6 +2309,21 @@ static void compose_use_signing(Compose *compose, gboolean use_signing)
                                       use_signing);
        compose_update_gnupg_mode_menu_item(compose);
 }
+
+static void compose_use_encryption(Compose *compose, gboolean use_encryption)
+{
+       GtkItemFactory *ifactory;
+       GtkWidget *menuitem = NULL;
+
+       compose->use_encryption = use_encryption;
+       ifactory = gtk_item_factory_from_widget(compose->menubar);
+       menuitem = gtk_item_factory_get_item
+               (ifactory, "/Message/Encrypt");
+
+       gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), 
+                                      use_encryption);
+       compose_update_gnupg_mode_menu_item(compose);
+}
 #endif /* USE_GPGME */
 
 #define NEXT_PART_NOT_CHILD(info)  \
@@ -3261,20 +3310,38 @@ gint compose_send(Compose *compose)
 {
        gint msgnum;
        FolderItem *folder;
-       gint val;
+       gint val = -1;
        gchar *msgpath;
 
+       compose_allow_user_actions (compose, FALSE);
+       compose->sending = TRUE;
+
        if (compose_check_entries(compose, TRUE) == FALSE)
-               return -1;
+               goto bail;
 
        val = compose_queue(compose, &msgnum, &folder);
+
        if (val) {
                alertpanel_error(_("Could not queue message for sending"));
-               return -1;
+               goto bail;
+       }
+
+
+       if (prefs_common.send_dialog_mode != SEND_DIALOG_ALWAYS) {
+               compose->sending = FALSE;
+               gtk_widget_destroy(compose->window);
+               /* No more compose access in the normal codepath 
+                * after this point! */
        }
 
        if (msgnum == 0) {
-               alertpanel_error(_("The message was queued but could not be sent.\nUse \"Send queued messages\" from the main window to retry."));
+               alertpanel_error(_("The message was queued but could not be "
+                                  "sent.\nUse \"Send queued messages\" from "
+                                  "the main window to retry."));
+               if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
+                       compose->sending = FALSE;
+                       compose_allow_user_actions (compose, TRUE);
+               }
                return 0;
        }
        
@@ -3282,11 +3349,29 @@ gint compose_send(Compose *compose)
        val = procmsg_send_message_queue(msgpath);
        g_free(msgpath);
 
-       folder_item_remove_msg(folder, msgnum);
-       
-       folder_item_scan(folder);
+       if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
+               compose->sending = FALSE;
+               compose_allow_user_actions (compose, TRUE);
+               if (val != 0) {
+                       folder_item_remove_msg(folder, msgnum);
+                       folder_item_scan(folder);
+               }
+       }
+
+       if (val == 0) {
+               folder_item_remove_msg(folder, msgnum);
+               folder_item_scan(folder);
+               if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS)
+                       gtk_widget_destroy(compose->window);
+       }
+
+       return 0;
 
-       return val;
+bail:
+       compose_allow_user_actions (compose, TRUE);
+       compose->sending = FALSE;
+
+       return -1;
 }
 
 #if 0 /* compose restructure */
@@ -3512,6 +3597,7 @@ static gint compose_redirect_write_to_file(Compose *compose, const gchar *file)
        FILE *fdest;
        size_t len;
        gchar buf[BUFFSIZE];
+       gchar *header;
 
        if ((fp = fopen(compose->redirect_filename, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
@@ -3674,6 +3760,8 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
        gchar *canon_buf;
        const gchar *out_codeset;
        EncodingType encoding;
+       gboolean already_encoded = FALSE;
+       gchar *header;
 
        if ((fp = fopen(file, "wb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
@@ -3717,6 +3805,9 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
                    compose->use_signing && !compose->gnupg_mode &&
                    encoding == ENC_8BIT)
                        encoding = ENC_BASE64;
+               
+               if (compose->use_encryption && compose->gnupg_mode)
+                       encoding = ENC_8BIT; /* this will be encrypted to a 7bit string */
 #endif
 
                src_codeset = CS_UTF_8;
@@ -3726,16 +3817,18 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
 
                buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
                if (!buf) {
-                       AlertValue aval;
+                       AlertValue aval = G_ALERTDEFAULT;
                        gchar *msg;
 
-                       msg = g_strdup_printf(_("Can't convert the character encoding of the message from\n"
+                       if (!is_draft) {
+                               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"), msg, _("Yes"), _("+No"), NULL);
-                       g_free(msg);
-
+                               aval = alertpanel_with_type
+                                       (_("Error"), msg, _("Yes"), _("+No"), NULL, NULL, ALERT_ERROR);
+                               g_free(msg);
+                       }
+                       
                        if (aval != G_ALERTDEFAULT) {
                                g_free(chars);
                                fclose(fp);
@@ -3756,6 +3849,8 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
 
 #if USE_GPGME
        if (!is_draft && compose->use_signing && compose->gnupg_mode) {
+               gchar *tmpbuf;
+
                if (compose_clearsign_text(compose, &buf) < 0) {
                        g_warning("clearsign failed\n");
                        fclose(fp);
@@ -3763,18 +3858,25 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
                        g_free(buf);
                        return -1;
                }
+               tmpbuf = conv_codeset_strdup(buf, CS_UTF_8, out_codeset);
+               if (tmpbuf) {
+                       g_free(buf);
+                       buf = tmpbuf;
+               }
        }
 #endif
 
        /* write headers */
-       if (compose_write_headers
-               (compose, fp, out_codeset, encoding, is_draft) < 0) {
+       header = compose_get_header(compose, out_codeset, encoding, is_draft);
+       if (header == NULL || fwrite(header, sizeof(gchar), strlen(header), fp) < strlen(header)) {
                g_warning("can't write headers\n");
                fclose(fp);
                /* unlink(file); */
+               g_free(header);
                g_free(buf);
                return -1;
        }
+       g_free(header);
 
        if (compose_use_attach(compose)) {
 #if USE_GPGME
@@ -3812,10 +3914,14 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
        } 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 (!already_encoded) {
+                       outbuf = g_malloc(len * 4);
+                       qp_encode_line(outbuf, buf);
+                       outlen = strlen(outbuf);
+               } else {
+                       outbuf = g_strdup(buf);
+                       outlen = len;
+               }
                if (fwrite(outbuf, sizeof(gchar), outlen, fp) != outlen) {
                        FILE_OP_ERROR(file, "fwrite");
                        fclose(fp);
@@ -3868,7 +3974,8 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
        }
        if (compose->use_encryption) {
                if (rfc2015_encrypt(file, compose->to_list,
-                                   compose->gnupg_mode) < 0) {
+                                   compose->gnupg_mode,
+                                   out_codeset) < 0) {
                        unlink(file);
                        return -1;
                }
@@ -4110,6 +4217,12 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
        if (newsac) {
                fprintf(fp, "NAID:%d\n", newsac->account_id);
        }
+#ifdef USE_GPGME
+       fprintf(fp, "X-Sylpheed-Sign:%d\n", compose->use_signing);
+       fprintf(fp, "X-Sylpheed-Encrypt:%d\n", compose->use_encryption);
+       fprintf(fp, "X-Sylpheed-Gnupg-Mode:%d\n", compose->gnupg_mode);
+#endif
+
        /* Save copy folder */
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn))) {
                gchar *savefolderid;
@@ -4260,103 +4373,94 @@ static void compose_write_attach(Compose *compose, FILE *fp)
        fprintf(fp, "\n--%s--\n", compose->boundary);
 }
 
-#define QUOTE_IF_REQUIRED(out, str)                    \
-{                                                      \
-       if (*str != '"' && strpbrk(str, ",.[]<>")) {    \
-               gchar *__tmp;                           \
-               gint len;                               \
-                                                       \
-               len = strlen(str) + 3;                  \
-               Xalloca(__tmp, len, return -1);         \
-               g_snprintf(__tmp, len, "\"%s\"", str);  \
-               out = __tmp;                            \
-       } else {                                        \
-               Xstrdup_a(out, str, return -1);         \
-       }                                               \
-}
-  
-#define PUT_RECIPIENT_HEADER(header, str)                                    \
-{                                                                            \
-       if (*str != '\0') {                                                   \
-               gchar *dest;                                                  \
-               Xstrdup_a(dest, str, return -1);                              \
-               g_strstrip(dest);                                             \
-               if (*dest != '\0') {                                          \
-                       compose->to_list = address_list_append                \
-                               (compose->to_list, dest);                     \
-                       compose_convert_header                                \
-                               (buf, sizeof(buf), dest, strlen(header) + 2,  \
-                                TRUE);                                       \
-                       fprintf(fp, "%s: %s\n", header, buf);                 \
-               }                                                             \
-       }                                                                     \
+#define QUOTE_IF_REQUIRED(out, str)                                    \
+{                                                                      \
+       if (*str != '"' && strpbrk(str, ",.[]<>")) {                    \
+               gchar *__tmp;                                           \
+               gint len;                                               \
+                                                                       \
+               len = strlen(str) + 3;                                  \
+               if ((__tmp = alloca(len)) == NULL) {                    \
+                       g_warning("can't allocate memory\n");           \
+                       g_string_free(header, TRUE);                    \
+                       return NULL;                                    \
+               }                                                       \
+               g_snprintf(__tmp, len, "\"%s\"", str);                  \
+               out = __tmp;                                            \
+       } else {                                                        \
+               gchar *__tmp;                                           \
+                                                                       \
+               if ((__tmp = alloca(strlen(str) + 1)) == NULL) {        \
+                       g_warning("can't allocate memory\n");           \
+                       g_string_free(header, TRUE);                    \
+                       return NULL;                                    \
+               } else                                                  \
+                       strcpy(__tmp, str);                             \
+                                                                       \
+               out = __tmp;                                            \
+       }                                                               \
 }
 
 #define IS_IN_CUSTOM_HEADER(header) \
        (compose->account->add_customhdr && \
         custom_header_find(compose->account->customhdr_list, header) != NULL)
 
-static gint compose_write_headers_from_headerlist(Compose *compose, 
-                                                 FILE *fp
-                                                 const gchar *header,
-                                                 const gchar *seperator)
+static void compose_add_headerfield_from_headerlist(Compose *compose, 
+                                                   GString *header
+                                                   const gchar *fieldname,
+                                                   const gchar *seperator)
 {
-       gchar *str, *header_w_colon, *trans_hdr;
-       gboolean write_header = FALSE;
+       gchar *str, *fieldname_w_colon, *trans_fieldname;
+       gboolean add_field = FALSE;
        GSList *list;
        ComposeHeaderEntry *headerentry;
        const gchar * headerentryname;
-       GString *headerstr;
+       GString *fieldstr;
 
-       if (IS_IN_CUSTOM_HEADER(header)) {
-               return 0;
-       }
+       if (IS_IN_CUSTOM_HEADER(fieldname))
+               return;
 
-       debug_print("Writing %s-header\n", header);
+       debug_print("Adding %s-fields\n", fieldname);
 
-       headerstr = g_string_sized_new(64);
+       fieldstr = g_string_sized_new(64);
 
-       header_w_colon = g_strconcat(header, ":", NULL);
-       trans_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
+       fieldname_w_colon = g_strconcat(fieldname, ":", NULL);
+       trans_fieldname = (prefs_common.trans_hdr ? gettext(fieldname_w_colon) : fieldname_w_colon);
 
        for (list = compose->header_list; list; list = list->next) {
                headerentry = ((ComposeHeaderEntry *)list->data);
                headerentryname = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry));
 
-               if (!g_strcasecmp(trans_hdr, headerentryname)) {
-                       const gchar *entstr = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
-                       gchar *tmpstr = conv_codeset_strdup(entstr, CS_UTF_8, conv_get_current_charset_str());
-                       Xstrdup_a(str, tmpstr, return -1);
+               if (!g_strcasecmp(trans_fieldname, headerentryname)) {
+                       str = gtk_editable_get_chars(GTK_EDITABLE(headerentry->entry), 0, -1);
                        g_strstrip(str);
                        if (str[0] != '\0') {
-                               if (write_header)
-                                       g_string_append(headerstr, seperator);
-                               g_string_append(headerstr, str);
-                               write_header = TRUE;
+                               if (add_field)
+                                       g_string_append(fieldstr, seperator);
+                               g_string_append(fieldstr, str);
+                               add_field = TRUE;
                        }
-                       g_free(tmpstr);
                }
        }
-       if (write_header) {
+       if (add_field) {
                gchar *buf;
 
-               buf = g_new0(gchar, headerstr->len * 4 + 256);
+               buf = g_new0(gchar, fieldstr->len * 4 + 256);
                compose_convert_header
-                       (buf, headerstr->len * 4  + 256, headerstr->str,
-                       strlen(header) + 2, TRUE);
-               fprintf(fp, "%s: %s\n", header, buf);
+                       (buf, fieldstr->len * 4  + 256, fieldstr->str,
+                       strlen(fieldname) + 2, TRUE);
+               g_string_sprintfa(header, "%s: %s\n", fieldname, buf);
                g_free(buf);
        }
 
-       g_free(header_w_colon);
-       g_string_free(headerstr, TRUE);
+       g_free(fieldname_w_colon);
+       g_string_free(fieldstr, TRUE);
 
-       return(0);
+       return;
 }
 
-static gint compose_write_headers(Compose *compose, FILE *fp,
-                                 const gchar *charset, EncodingType encoding,
-                                 gboolean is_draft)
+static gchar *compose_get_header(Compose *compose, const gchar *charset,
+                                EncodingType encoding, gboolean is_draft)
 {
        gchar buf[BUFFSIZE];
        const gchar *entry_str;
@@ -4364,32 +4468,39 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
        gchar *name;
        GSList *list;
        gchar *std_headers[] = {"To:", "Cc:", "Bcc:", "Newsgroups:", "Reply-To:", "Followup-To:", NULL};
+       GString *header;
 
        /* struct utsname utsbuf; */
 
-       g_return_val_if_fail(fp != NULL, -1);
-       g_return_val_if_fail(charset != NULL, -1);
-       g_return_val_if_fail(compose->account != NULL, -1);
-       g_return_val_if_fail(compose->account->address != NULL, -1);
+       g_return_val_if_fail(charset != NULL, NULL);
+       g_return_val_if_fail(compose->account != NULL, NULL);
+       g_return_val_if_fail(compose->account->address != NULL, NULL);
+
+       header = g_string_sized_new(64);
 
        /* Save draft infos */
        if (is_draft) {
-               fprintf(fp, "X-Sylpheed-Account-Id:%d\n", compose->account->account_id);
-               fprintf(fp, "S:%s\n", compose->account->address);
+               g_string_sprintfa(header, "X-Sylpheed-Account-Id:%d\n", compose->account->account_id);
+               g_string_sprintfa(header, "S:%s\n", compose->account->address);
                if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn))) {
                        gchar *savefolderid;
 
                        savefolderid = gtk_editable_get_chars(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
-                       fprintf(fp, "SCF:%s\n", savefolderid);
+                       g_string_sprintfa(header, "SCF:%s\n", savefolderid);
                        g_free(savefolderid);
                }
-               fprintf(fp, "\n");
+#ifdef USE_GPGME
+               g_string_sprintfa(header, "X-Sylpheed-Sign:%d\n", compose->use_signing);
+               g_string_sprintfa(header, "X-Sylpheed-Encrypt:%d\n", compose->use_encryption);
+               g_string_sprintfa(header, "X-Sylpheed-Gnupg-Mode:%d\n", compose->gnupg_mode);
+#endif
+               g_string_sprintfa(header, "\n");
        }
 
        /* Date */
        if (compose->account->add_date) {
                get_rfc822_date(buf, sizeof(buf));
-               fprintf(fp, "Date: %s\n", buf);
+               g_string_sprintfa(header, "Date: %s\n", buf);
        }
 
        /* From */
@@ -4398,122 +4509,64 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                        (buf, sizeof(buf), compose->account->name,
                         strlen("From: "), TRUE);
                QUOTE_IF_REQUIRED(name, buf);
-               fprintf(fp, "From: %s <%s>\n",
+               g_string_sprintfa(header, "From: %s <%s>\n",
                        name, compose->account->address);
        } else
-               fprintf(fp, "From: %s\n", compose->account->address);
+               g_string_sprintfa(header, "From: %s\n", compose->account->address);
        
        /* To */
-       compose_write_headers_from_headerlist(compose, fp, "To", ", ");
-#if 0 /* NEW COMPOSE GUI */
-       if (compose->use_to) {
-               entry_str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
-               PUT_RECIPIENT_HEADER("To", entry_str);
-       }
-#endif
+       compose_add_headerfield_from_headerlist(compose, header, "To", ", ");
 
        /* Newsgroups */
-       compose_write_headers_from_headerlist(compose, fp, "Newsgroups", ",");
-#if 0 /* NEW COMPOSE GUI */
-       if (compose->use_newsgroups) {
-               entry_str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
-               if (*entry_str != '\0') {
-                       Xstrdup_a(str, entry_str, return -1);
-                       g_strstrip(str);
-                       remove_space(str);
-                       if (*str != '\0') {
-                               compose->newsgroup_list =
-                                       newsgroup_list_append
-                                               (compose->newsgroup_list, str);
-                               compose_convert_header(buf, sizeof(buf), str,
-                                                      strlen("Newsgroups: "),
-                                                      TRUE);
-                               fprintf(fp, "Newsgroups: %s\n", buf);
-                       }
-               }
-       }
-#endif
+       compose_add_headerfield_from_headerlist(compose, header, "Newsgroups", ",");
+
        /* Cc */
-       compose_write_headers_from_headerlist(compose, fp, "Cc", ", ");
-#if 0 /* NEW COMPOSE GUI */
-       if (compose->use_cc) {
-               str = gtk_entry_get_text(GTK_ENTRY(compose->cc_entry));
-               PUT_RECIPIENT_HEADER("Cc", str);
-       }
-#endif
+       compose_add_headerfield_from_headerlist(compose, header, "Cc", ", ");
+
        /* Bcc */
-       compose_write_headers_from_headerlist(compose, fp, "Bcc", ", ");
-#if 0 /* NEW COMPOSE GUI */
-       if (compose->use_bcc) {
-               str = gtk_entry_get_text(GTK_ENTRY(compose->bcc_entry));
-               PUT_RECIPIENT_HEADER("Bcc", str);
-       }
-#endif
+       compose_add_headerfield_from_headerlist(compose, header, "Bcc", ", ");
 
        /* Subject */
-       entry_str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
-       if (*entry_str != '\0' && !IS_IN_CUSTOM_HEADER("Subject")) {
-               Xstrdup_a(str, entry_str, return -1);
+       str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
+       if (*str != '\0' && !IS_IN_CUSTOM_HEADER("Subject")) {
+               gchar *tmpstr;
+
+               tmpstr = g_strdup(str);
+               if (tmpstr == NULL) {
+                       g_string_free(header, TRUE);
+                       return NULL;
+               }
                g_strstrip(str);
                if (*str != '\0') {
                        compose_convert_header(buf, sizeof(buf), str,
                                               strlen("Subject: "), FALSE);
-                       fprintf(fp, "Subject: %s\n", buf);
+                       g_string_sprintfa(header, "Subject: %s\n", buf);
                }
        }
 
        /* Message-ID */
        if (compose->account->gen_msgid) {
                compose_generate_msgid(buf, sizeof(buf));
-               fprintf(fp, "Message-ID: <%s>\n", buf);
+               g_string_sprintfa(header, "Message-ID: <%s>\n", buf);
                compose->msgid = g_strdup(buf);
        }
 
        if (compose->remove_references == FALSE) {
                /* In-Reply-To */
                if (compose->inreplyto && compose->to_list)
-                       fprintf(fp, "In-Reply-To: <%s>\n", compose->inreplyto);
+                       g_string_sprintfa(header, "In-Reply-To: <%s>\n", compose->inreplyto);
        
                /* References */
                if (compose->references)
-                       fprintf(fp, "References: %s\n", compose->references);
+                       g_string_sprintfa(header, "References: %s\n", compose->references);
        }
 
        /* Followup-To */
-       compose_write_headers_from_headerlist(compose, fp, "Followup-To", ",");
-#if 0 /* NEW COMPOSE GUI */
-       if (compose->use_followupto && !IS_IN_CUSTOM_HEADER("Followup-To")) {
-               entry_str = gtk_entry_get_text(GTK_ENTRY(compose->followup_entry));
-               if (*entry_str != '\0') {
-                       Xstrdup_a(str, entry_str, return -1);
-                       g_strstrip(str);
-                       remove_space(str);
-                       if (*str != '\0') {
-                               compose_convert_header(buf, sizeof(buf), str,
-                                                      strlen("Followup-To: "),
-                                                      TRUE);
-                               fprintf(fp, "Followup-To: %s\n", buf);
-                       }
-               }
-       }
-#endif
+       compose_add_headerfield_from_headerlist(compose, header, "Followup-To", ",");
+
        /* Reply-To */
-       compose_write_headers_from_headerlist(compose, fp, "Reply-To", ", ");
-#if 0 /* NEW COMPOSE GUI */
-       if (compose->use_replyto && !IS_IN_CUSTOM_HEADER("Reply-To")) {
-               entry_str = gtk_entry_get_text(GTK_ENTRY(compose->reply_entry));
-               if (*entry_str != '\0') {
-                       Xstrdup_a(str, entry_str, return -1);
-                       g_strstrip(str);
-                       if (*str != '\0') {
-                               compose_convert_header(buf, sizeof(buf), str,
-                                                      strlen("Reply-To: "),
-                                                      TRUE);
-                               fprintf(fp, "Reply-To: %s\n", buf);
-                       }
-               }
-       }
-#endif
+       compose_add_headerfield_from_headerlist(compose, header, "Reply-To", ", ");
+
        /* Organization */
        if (compose->account->organization &&
            strlen(compose->account->organization) &&
@@ -4521,21 +4574,21 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                compose_convert_header(buf, sizeof(buf),
                                       compose->account->organization,
                                       strlen("Organization: "), FALSE);
-               fprintf(fp, "Organization: %s\n", buf);
+               g_string_sprintfa(header, "Organization: %s\n", buf);
        }
 
        /* Program version and system info */
        /* uname(&utsbuf); */
        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",
+               g_string_sprintfa(header, "X-Mailer: %s (GTK+ %d.%d.%d; %s)\n",
                        prog_version,
                        gtk_major_version, gtk_minor_version, gtk_micro_version,
                        TARGET_ALIAS);
                        /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
        }
        if (g_slist_length(compose->newsgroup_list) && !IS_IN_CUSTOM_HEADER("X-Newsreader")) {
-               fprintf(fp, "X-Newsreader: %s (GTK+ %d.%d.%d; %s)\n",
+               g_string_sprintfa(header, "X-Newsreader: %s (GTK+ %d.%d.%d; %s)\n",
                        prog_version,
                        gtk_major_version, gtk_minor_version, gtk_micro_version,
                        TARGET_ALIAS);
@@ -4555,41 +4608,41 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                                        (buf, sizeof(buf),
                                         chdr->value ? chdr->value : "",
                                         strlen(chdr->name) + 2, FALSE);
-                               fprintf(fp, "%s: %s\n", chdr->name, buf);
+                               g_string_sprintfa(header, "%s: %s\n", chdr->name, buf);
                        }
                }
        }
 
        /* MIME */
-       fprintf(fp, "Mime-Version: 1.0\n");
+       g_string_sprintfa(header, "Mime-Version: 1.0\n");
        if (compose_use_attach(compose)) {
                compose->boundary = generate_mime_boundary(NULL);
-               fprintf(fp,
+               g_string_sprintfa(header,
                        "Content-Type: multipart/mixed;\n"
                        " boundary=\"%s\"\n", compose->boundary);
        } else {
-               fprintf(fp, "Content-Type: text/plain; charset=%s\n", charset);
+               g_string_sprintfa(header, "Content-Type: text/plain; charset=%s\n", charset);
 #if USE_GPGME
                if (compose->use_signing && !compose->gnupg_mode)
-                       fprintf(fp, "Content-Disposition: inline\n");
+                       g_string_sprintfa(header, "Content-Disposition: inline\n");
 #endif
-               fprintf(fp, "Content-Transfer-Encoding: %s\n",
+               g_string_sprintfa(header, "Content-Transfer-Encoding: %s\n",
                        procmime_get_encoding_str(encoding));
        }
 
        /* PRIORITY */
        switch (compose->priority) {
-               case PRIORITY_HIGHEST: fprintf(fp, "Importance: high\n"
+               case PRIORITY_HIGHEST: g_string_sprintfa(header, "Importance: high\n"
                                                   "X-Priority: 1 (Highest)\n");
                        break;
-               case PRIORITY_HIGH: fprintf(fp, "Importance: high\n"
+               case PRIORITY_HIGH: g_string_sprintfa(header, "Importance: high\n"
                                                "X-Priority: 2 (High)\n");
                        break;
                case PRIORITY_NORMAL: break;
-               case PRIORITY_LOW: fprintf(fp, "Importance: low\n"
+               case PRIORITY_LOW: g_string_sprintfa(header, "Importance: low\n"
                                               "X-Priority: 4 (Low)\n");
                        break;
-               case PRIORITY_LOWEST: fprintf(fp, "Importance: low\n"
+               case PRIORITY_LOWEST: g_string_sprintfa(header, "Importance: low\n"
                                                  "X-Priority: 5 (Lowest)\n");
                        break;
                default: debug_print("compose: priority unknown : %d\n",
@@ -4605,9 +4658,9 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                                                       compose->account->name, 
                                                       strlen("Disposition-Notification-To: "),
                                                       TRUE);
-                               fprintf(fp, "Disposition-Notification-To: %s <%s>\n", buf, compose->account->address);
+                               g_string_sprintfa(header, "Disposition-Notification-To: %s <%s>\n", buf, compose->account->address);
                        } else
-                               fprintf(fp, "Disposition-Notification-To: %s\n", compose->account->address);
+                               g_string_sprintfa(header, "Disposition-Notification-To: %s\n", compose->account->address);
                }
        }
 
@@ -4651,16 +4704,19 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                        string++;
                }
                if (!standard_header && !IS_IN_CUSTOM_HEADER(headername))
-                       fprintf(fp, "%s %s\n", headername_wcolon, headervalue);
+                       g_string_sprintfa(header, "%s %s\n", headername_wcolon, headervalue);
                                
                g_free(headername);
                g_free(headername_wcolon);              
        }
 
        /* separator between header and body */
-       fputs("\n", fp);
+       g_string_sprintfa(header, "\n");
 
-       return 0;
+       str = header->str;
+       g_string_free(header, FALSE);
+
+       return str;
 }
 
 #undef IS_IN_CUSTOM_HEADER
@@ -4758,6 +4814,7 @@ static void compose_create_header_entry(Compose *compose)
        }
        if (header)
                gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), header);
+
        g_signal_connect(G_OBJECT(GTK_COMBO(combo)->entry), "grab_focus",
                         G_CALLBACK(compose_grab_focus_before_cb), compose);
        g_signal_connect_after(G_OBJECT(GTK_COMBO(combo)->entry), "grab_focus",
@@ -4780,10 +4837,6 @@ static void compose_create_header_entry(Compose *compose)
                         G_CALLBACK(compose_grab_focus_before_cb), compose);
        g_signal_connect_after(G_OBJECT(entry), "grab_focus",
                         G_CALLBACK(compose_grab_focus_cb), compose);
-       g_signal_connect(G_OBJECT(entry), "button-press-event", 
-                        G_CALLBACK(compose_headerentry_button_pressed),
-                        NULL);
-
        address_completion_register_entry(GTK_ENTRY(entry));
 
         headerentry->compose = compose;
@@ -4957,7 +5010,7 @@ GtkWidget *compose_create_attach(Compose *compose)
        attach_scrwin = gtk_scrolled_window_new(NULL, NULL);
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(attach_scrwin),
                                       GTK_POLICY_AUTOMATIC,
-                                      GTK_POLICY_ALWAYS);
+                                      GTK_POLICY_AUTOMATIC);
        gtk_widget_set_size_request(attach_scrwin, -1, 80);
 
        attach_clist = gtk_clist_new_with_titles(N_ATTACH_COLS, titles);
@@ -5134,7 +5187,6 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
        gtk_widget_set_size_request(window, -1, prefs_common.compose_height);
-       gtk_window_set_wmclass(GTK_WINDOW(window), "compose window", "Sylpheed");
 
        if (!geometry.max_width) {
                geometry.max_width = gdk_screen_width();
@@ -5233,7 +5285,7 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        /* text widget */
        scrolledwin = gtk_scrolled_window_new(NULL, NULL);
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
-                                      GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
+                                      GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
        gtk_box_pack_start(GTK_BOX(edit_vbox), scrolledwin, TRUE, TRUE, 0);
        gtk_widget_set_size_request(scrolledwin, prefs_common.compose_width, -1);
 
@@ -5454,7 +5506,7 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        if (account->set_autocc && account->auto_cc && mode != COMPOSE_REEDIT)
                compose_entry_append(compose, account->auto_cc, COMPOSE_CC);
 
-       if (account->set_autobcc) 
+       if (account->set_autobcc && account->auto_bcc && mode != COMPOSE_REEDIT
                compose_entry_append(compose, account->auto_bcc, COMPOSE_BCC);
        
        if (account->set_autoreplyto && account->auto_replyto && mode != COMPOSE_REEDIT)
@@ -6337,8 +6389,6 @@ static void compose_set_ext_editor_sensitive(Compose *compose,
 
        menu_set_sensitive(ifactory, "/Message/Send", sensitive);
        menu_set_sensitive(ifactory, "/Message/Send later", sensitive);
-       menu_set_sensitive(ifactory, "/Message/Save to draft folder",
-                          sensitive);
        menu_set_sensitive(ifactory, "/File/Insert file", sensitive);
        menu_set_sensitive(ifactory, "/File/Insert signature", sensitive);
        menu_set_sensitive(ifactory, "/Edit/Wrap current paragraph", sensitive);
@@ -6556,7 +6606,6 @@ static void compose_allow_user_actions (Compose *compose, gboolean allow)
 static void compose_send_cb(gpointer data, guint action, GtkWidget *widget)
 {
        Compose *compose = (Compose *)data;
-       gint val;
        
        if (prefs_common.work_offline)
                if (alertpanel(_("Offline warning"), 
@@ -6569,13 +6618,7 @@ static void compose_send_cb(gpointer data, guint action, GtkWidget *widget)
                compose->draft_timeout_tag = -1;
        }
 
-       compose_allow_user_actions (compose, FALSE);
-       compose->sending = TRUE;
-       val = compose_send(compose);
-       compose_allow_user_actions (compose, TRUE);
-       compose->sending = FALSE;
-
-       if (val == 0) gtk_widget_destroy(compose->window);
+       compose_send(compose);
 }
 
 static void compose_send_later_cb(gpointer data, guint action,
@@ -6714,11 +6757,10 @@ static void compose_insert_file_cb(gpointer data, guint action,
                for ( tmp = file_list; tmp; tmp = tmp->next) {
                        gchar *file = (gchar *) tmp->data;
                        gchar *filedup = g_strdup(file);
-                       gchar *shortfile;
+                       const gchar *shortfile = g_basename(filedup);
                        ComposeInsertResult res;
 
                        res = compose_insert_file(compose, file);
-                       shortfile = g_basename(filedup);
                        if (res == COMPOSE_INSERT_READ_ERROR) {
                                alertpanel_error(_("File '%s' could not be read."), shortfile);
                        } else if (res == COMPOSE_INSERT_INVALID_CHARACTER) {
@@ -7650,15 +7692,6 @@ gboolean compose_headerentry_changed_cb(GtkWidget *entry,
        return FALSE;
 }
 
-static gboolean compose_headerentry_button_pressed
-       (GtkWidget *entry, GdkEventButton *event, gpointer data)
-{
-       /* if this is a lclick, grab the focus */
-       if (event->button == 1)
-               gtk_widget_grab_focus(entry);
-       return FALSE;
-}
-
 static void compose_show_first_last_header(Compose *compose, gboolean show_first)
 {
        GtkAdjustment *vadj;