this type_hint not wanted or needed here
[claws.git] / src / compose.c
index 4a29775d4c257da93882fca6938a1cf0ccf2a716..b886872f66b2d6bce5613265838a0f2adab5a912 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2016 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2019 the Claws Mail team and 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 "autofaces.h"
 #include "spell_entry.h"
 #include "headers.h"
+#include "file-utils.h"
+
+#ifdef USE_LDAP
+#include "password.h"
+#include "ldapserver.h"
+#endif
 
 enum
 {
@@ -310,7 +316,7 @@ static gint compose_write_body_to_file              (Compose        *compose,
 static gint compose_remove_reedit_target       (Compose        *compose,
                                                 gboolean        force);
 static void compose_remove_draft                       (Compose        *compose);
-static gint compose_queue_sub                  (Compose        *compose,
+static ComposeQueueResult compose_queue_sub                    (Compose        *compose,
                                                 gint           *msgnum,
                                                 FolderItem     **item,
                                                 gchar          **msgpath,
@@ -487,11 +493,10 @@ static void compose_toggle_encrypt_cb     (GtkToggleAction *action,
                                         gpointer        data);
 static void compose_set_privacy_system_cb(GtkWidget *widget, gpointer data);
 static void compose_update_privacy_system_menu_item(Compose * compose, gboolean warn);
-static void activate_privacy_system     (Compose *compose, 
+static void compose_activate_privacy_system     (Compose *compose, 
                                          PrefsAccount *account,
                                         gboolean warn);
-static void compose_use_signing(Compose *compose, gboolean use_signing);
-static void compose_use_encryption(Compose *compose, gboolean use_encryption);
+static void compose_apply_folder_privacy_settings(Compose *compose, FolderItem *folder_item);
 static void compose_toggle_return_receipt_cb(GtkToggleAction *action,
                                         gpointer        data);
 static void compose_toggle_remove_refs_cb(GtkToggleAction *action,
@@ -836,32 +841,26 @@ static void compose_create_tags(GtkTextView *text, Compose *compose)
 {
        GtkTextBuffer *buffer;
        GdkColor black = {(gulong)0, (gushort)0, (gushort)0, (gushort)0};
-#if !GTK_CHECK_VERSION(2, 24, 0)
-       GdkColormap *cmap;
-       gboolean success[8];
-       int i;
-       GdkColor color[8];
-#endif
 
        buffer = gtk_text_view_get_buffer(text);
 
        if (prefs_common.enable_color) {
                /* grab the quote colors, converting from an int to a GdkColor */
-               gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_col,
+               gtkut_convert_int_to_gdk_color(prefs_common.color[COL_QUOTE_LEVEL1],
                                               &quote_color1);
-               gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_col,
+               gtkut_convert_int_to_gdk_color(prefs_common.color[COL_QUOTE_LEVEL2],
                                               &quote_color2);
-               gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_col,
+               gtkut_convert_int_to_gdk_color(prefs_common.color[COL_QUOTE_LEVEL3],
                                               &quote_color3);
-               gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_bgcol,
+               gtkut_convert_int_to_gdk_color(prefs_common.color[COL_QUOTE_LEVEL1_BG],
                                               &quote_bgcolor1);
-               gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_bgcol,
+               gtkut_convert_int_to_gdk_color(prefs_common.color[COL_QUOTE_LEVEL2_BG],
                                               &quote_bgcolor2);
-               gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_bgcol,
+               gtkut_convert_int_to_gdk_color(prefs_common.color[COL_QUOTE_LEVEL3_BG],
                                               &quote_bgcolor3);
-               gtkut_convert_int_to_gdk_color(prefs_common.signature_col,
+               gtkut_convert_int_to_gdk_color(prefs_common.color[COL_SIGNATURE],
                                               &signature_color);
-               gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
+               gtkut_convert_int_to_gdk_color(prefs_common.color[COL_URI],
                                               &uri_color);
        } else {
                signature_color = quote_color1 = quote_color2 = quote_color3 = 
@@ -902,29 +901,6 @@ static void compose_create_tags(GtkTextView *text, Compose *compose)
                                         NULL);
        compose->no_wrap_tag = gtk_text_buffer_create_tag(buffer, "no_wrap", NULL);
        compose->no_join_tag = gtk_text_buffer_create_tag(buffer, "no_join", NULL);
-
-#if !GTK_CHECK_VERSION(2, 24, 0)
-       color[0] = quote_color1;
-       color[1] = quote_color2;
-       color[2] = quote_color3;
-       color[3] = quote_bgcolor1;
-       color[4] = quote_bgcolor2;
-       color[5] = quote_bgcolor3;
-       color[6] = signature_color;
-       color[7] = uri_color;
-
-       cmap = gdk_drawable_get_colormap(gtk_widget_get_window(compose->window));
-       gdk_colormap_alloc_colors(cmap, color, 8, FALSE, TRUE, success);
-
-       for (i = 0; i < 8; i++) {
-               if (success[i] == FALSE) {
-                       g_warning("Compose: color allocation failed.");
-                       quote_color1 = quote_color2 = quote_color3 = 
-                               quote_bgcolor1 = quote_bgcolor2 = quote_bgcolor3 = 
-                               signature_color = uri_color = black;
-               }
-       }
-#endif
 }
 
 Compose *compose_new(PrefsAccount *account, const gchar *mailto,
@@ -956,20 +932,11 @@ static void compose_set_save_to(Compose *compose, const gchar *folderidentifier)
 {
        GtkEditable *entry;
        if (folderidentifier) {
-#if !GTK_CHECK_VERSION(2, 24, 0)
-               combobox_unset_popdown_strings(GTK_COMBO_BOX(compose->savemsg_combo));
-#else
                combobox_unset_popdown_strings(GTK_COMBO_BOX_TEXT(compose->savemsg_combo));
-#endif
                prefs_common.compose_save_to_history = add_history(
                                prefs_common.compose_save_to_history, folderidentifier);
-#if !GTK_CHECK_VERSION(2, 24, 0)
-               combobox_set_popdown_strings(GTK_COMBO_BOX(compose->savemsg_combo),
-                               prefs_common.compose_save_to_history);
-#else
                combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(compose->savemsg_combo),
                                prefs_common.compose_save_to_history);
-#endif
        }
 
        entry = GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(compose->savemsg_combo)));
@@ -987,20 +954,11 @@ static gchar *compose_get_save_to(Compose *compose)
        result = gtk_editable_get_chars(entry, 0, -1);
        
        if (result) {
-#if !GTK_CHECK_VERSION(2, 24, 0)
-               combobox_unset_popdown_strings(GTK_COMBO_BOX(compose->savemsg_combo));
-#else
                combobox_unset_popdown_strings(GTK_COMBO_BOX_TEXT(compose->savemsg_combo));
-#endif
                prefs_common.compose_save_to_history = add_history(
                                prefs_common.compose_save_to_history, result);
-#if !GTK_CHECK_VERSION(2, 24, 0)
-               combobox_set_popdown_strings(GTK_COMBO_BOX(compose->savemsg_combo),
-                               prefs_common.compose_save_to_history);
-#else
                combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(compose->savemsg_combo),
                                prefs_common.compose_save_to_history);
-#endif
        }
        return result;
 }
@@ -1050,6 +1008,14 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
        cm_return_val_if_fail(account != NULL, NULL);
 
        compose = compose_create(account, item, COMPOSE_NEW, FALSE);
+       compose_apply_folder_privacy_settings(compose, item);
+
+       if (privacy_system_can_sign(compose->privacy_system) == FALSE &&
+           (account->default_encrypt || account->default_sign))
+               alertpanel_error(_("You have opted to sign and/or encrypt this "
+                                  "message but have not selected a privacy system.\n\n"
+                                  "Signing and encrypting have been disabled for this "
+                                  "message."));
 
        /* override from name if mailto asked for it */
        if (mailto_from) {
@@ -1086,6 +1052,7 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
                        else
                                gtk_entry_set_text(GTK_ENTRY(compose->from_name), buf);
                        quote_fmt_reset_vartable();
+                       quote_fmtlex_destroy();
 
                        g_free(tmp);
                }
@@ -1179,6 +1146,7 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
                                gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf);
                        compose_attach_from_list(compose, quote_fmt_get_attachments_list(), FALSE);
                        quote_fmt_reset_vartable();
+                       quote_fmtlex_destroy();
 
                        g_free(subject);
                        g_free(tmp);
@@ -1226,7 +1194,10 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
 
                for (curr = attach_files ; curr != NULL ; curr = curr->next) {
                        ainfo = (AttachInfo *) curr->data;
-                       compose_attach_append(compose, ainfo->file, ainfo->file,
+                       if (ainfo->insert)
+                               compose_insert_file(compose, ainfo->file);
+                       else
+                               compose_attach_append(compose, ainfo->file, ainfo->file,
                                        ainfo->content_type, ainfo->charset);
                }
        }
@@ -1237,7 +1208,8 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
        if (item && item->prefs && item->prefs->save_copy_to_folder) {
                gchar *folderidentifier;
 
-               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
+               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
+               gtk_widget_set_sensitive(GTK_WIDGET(compose->savemsg_combo), TRUE);
                folderidentifier = folder_item_get_identifier(item);
                compose_set_save_to(compose, folderidentifier);
                g_free(folderidentifier);
@@ -1309,7 +1281,8 @@ static void compose_force_encryption(Compose *compose, PrefsAccount *account,
        cm_return_if_fail(compose != NULL);
        cm_return_if_fail(account != NULL);
 
-       if (override_pref == FALSE && account->default_encrypt_reply == FALSE)
+       if (privacy_system_can_encrypt(compose->privacy_system) == FALSE ||
+           (override_pref == FALSE && account->default_encrypt_reply == FALSE))
                return;
 
        if (account->default_privacy_system && strlen(account->default_privacy_system))
@@ -1321,6 +1294,7 @@ static void compose_force_encryption(Compose *compose, PrefsAccount *account,
                if (privacy_avail && g_slist_length(privacy_avail)) {
                        privacy = (gchar *)(privacy_avail->data);
                }
+               g_slist_free_full(privacy_avail, g_free);
        }
        if (privacy != NULL) {
                if (system) {
@@ -1345,6 +1319,8 @@ static void compose_force_encryption(Compose *compose, PrefsAccount *account,
 static void compose_force_signing(Compose *compose, PrefsAccount *account, const gchar *system)
 {
        const gchar *privacy = NULL;
+       if (privacy_system_can_sign(compose->privacy_system) == FALSE)
+               return;
 
        if (account->default_privacy_system && strlen(account->default_privacy_system))
                privacy = account->default_privacy_system;
@@ -1594,6 +1570,7 @@ static Compose *compose_generic_reply(MsgInfo *msginfo,
        cm_return_val_if_fail(account != NULL, NULL);
 
        compose = compose_create(account, msginfo->folder, COMPOSE_REPLY, FALSE);
+       compose_apply_folder_privacy_settings(compose, msginfo->folder);
 
        compose->updating = TRUE;
 
@@ -1614,6 +1591,7 @@ static Compose *compose_generic_reply(MsgInfo *msginfo,
                gchar *folderidentifier;
 
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
+               gtk_widget_set_sensitive(GTK_WIDGET(compose->savemsg_combo), TRUE);
                folderidentifier = folder_item_get_identifier(msginfo->folder);
                compose_set_save_to(compose, folderidentifier);
                g_free(folderidentifier);
@@ -1653,6 +1631,7 @@ static Compose *compose_generic_reply(MsgInfo *msginfo,
                else
                        gtk_entry_set_text(GTK_ENTRY(compose->from_name), buf);
                quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
 
                g_free(tmp);
        }
@@ -1712,6 +1691,14 @@ static Compose *compose_generic_reply(MsgInfo *msginfo,
        }
        g_free(s_system);
 
+       if (privacy_system_can_sign(compose->privacy_system) == FALSE &&
+           ((account->default_encrypt_reply && MSG_IS_ENCRYPTED(compose->replyinfo->flags)) ||
+            (account->default_sign_reply && MSG_IS_SIGNED(compose->replyinfo->flags))))
+               alertpanel_error(_("You have opted to sign and/or encrypt this "
+                                  "message but have not selected a privacy system.\n\n"
+                                  "Signing and encrypting have been disabled for this "
+                                  "message."));
+
        SIGNAL_BLOCK(textbuf);
        
        if (account->auto_sig)
@@ -1778,6 +1765,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
        else
                mode = COMPOSE_FORWARD;
        compose = compose_create(account, msginfo->folder, mode, batch);
+       compose_apply_folder_privacy_settings(compose, msginfo->folder);
 
        compose->updating = TRUE;
        compose->fwdinfo = procmsg_msginfo_get_full_info(msginfo);
@@ -1835,6 +1823,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
                else
                        gtk_entry_set_text(GTK_ENTRY(compose->from_name), buf);
                quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
 
                g_free(tmp);
                procmsg_msginfo_free(&full_msginfo);
@@ -1927,6 +1916,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
                gchar *folderidentifier;
 
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
+               gtk_widget_set_sensitive(GTK_WIDGET(compose->savemsg_combo), TRUE);
                folderidentifier = folder_item_get_identifier(msginfo->folder);
                compose_set_save_to(compose, folderidentifier);
                g_free(folderidentifier);
@@ -1948,7 +1938,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
 
        hooks_invoke(COMPOSE_CREATED_HOOKLIST, compose);
 
-        return compose;
+       return compose;
 }
 
 #undef INSERT_FW_HEADER
@@ -1992,6 +1982,7 @@ static Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_
        }
 
        compose = compose_create(account, ((MsgInfo *)msginfo_list->data)->folder, COMPOSE_FORWARD, FALSE);
+       compose_apply_folder_privacy_settings(compose, ((MsgInfo *)msginfo_list->data)->folder);
 
        compose->updating = TRUE;
 
@@ -2026,6 +2017,7 @@ static Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_
                        else
                                gtk_entry_set_text(GTK_ENTRY(compose->from_name), buf);
                        quote_fmt_reset_vartable();
+                       quote_fmtlex_destroy();
 
                        g_free(tmp);
                }
@@ -2398,10 +2390,12 @@ Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
                compose_use_encryption(compose, use_encryption);
                compose_update_privacy_system_menu_item(compose, FALSE);
        } else {
-               activate_privacy_system(compose, account, FALSE);
+               compose_activate_privacy_system(compose, account, FALSE);
        }
+       compose_apply_folder_privacy_settings(compose, msginfo->folder);
 
        compose->targetinfo = procmsg_msginfo_copy(msginfo);
+       compose->targetinfo->tags = g_slist_copy(msginfo->tags);
 
        compose_extract_original_charset(compose);
 
@@ -2413,6 +2407,7 @@ Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
                /* Set message save folder */
                if (!procheader_get_header_from_msginfo(msginfo, &queueheader_buf, "SCF:")) {
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
+                       gtk_widget_set_sensitive(GTK_WIDGET(compose->savemsg_combo), TRUE);
                        compose_set_save_to(compose, &queueheader_buf[4]);
                        g_free(queueheader_buf);
                }
@@ -2460,12 +2455,12 @@ Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
                gboolean prev_autowrap;
                GtkTextBuffer *buffer;
                BLOCK_WRAP();
-               while (fgets(buf, sizeof(buf), fp) != NULL) {
+               while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                        strcrchomp(buf);
                        gtk_text_buffer_insert(textbuf, &iter, buf, -1);
                }
                UNBLOCK_WRAP();
-               fclose(fp);
+               claws_fclose(fp);
        }
        
        compose_attach_parts(compose, msginfo);
@@ -2525,6 +2520,7 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo,
        cm_return_val_if_fail(account != NULL, NULL);
 
        compose = compose_create(account, msginfo->folder, COMPOSE_REDIRECT, batch);
+       compose_apply_folder_privacy_settings(compose, msginfo->folder);
 
        compose->updating = TRUE;
 
@@ -2552,7 +2548,8 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo,
        if (item && item->prefs && item->prefs->save_copy_to_folder) {
                gchar *folderidentifier;
 
-               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
+               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
+               gtk_widget_set_sensitive(GTK_WIDGET(compose->savemsg_combo), TRUE);
                folderidentifier = folder_item_get_identifier(item);
                compose_set_save_to(compose, folderidentifier);
                g_free(folderidentifier);
@@ -2756,6 +2753,10 @@ void compose_toolbar_cb(gint action, gpointer data)
                compose_check_all(NULL, compose);
                break;
 #endif
+       case A_PRIVACY_SIGN:
+               break;
+       case A_PRIVACY_ENCRYPT:
+               break;
        default:
                break;
        }
@@ -2893,9 +2894,9 @@ static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
 
        cm_return_val_if_fail(msginfo != NULL, -1);
 
-       if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
+       if ((fp = procmsg_open_message(msginfo, FALSE)) == NULL) return -1;
        procheader_get_header_fields(fp, hentry);
-       fclose(fp);
+       claws_fclose(fp);
 
        if (hentry[H_REPLY_TO].body != NULL) {
                if (hentry[H_REPLY_TO].body[0] != '\0') {
@@ -2981,7 +2982,9 @@ static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
                if (msginfo->inreplyto && *msginfo->inreplyto)
                        compose->inreplyto = g_strdup(msginfo->inreplyto);
 
-               if (msginfo->msgid && *msginfo->msgid)
+               if (msginfo->msgid && *msginfo->msgid &&
+                               compose->folder != NULL &&
+                               compose->folder->stype ==  F_DRAFT)
                        compose->msgid = g_strdup(msginfo->msgid);
        } else {
                if (msginfo->msgid && *msginfo->msgid)
@@ -3016,9 +3019,9 @@ static gint compose_parse_manual_headers(Compose *compose, MsgInfo *msginfo, Hea
 
        cm_return_val_if_fail(msginfo != NULL, -1);
 
-       if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
+       if ((fp = procmsg_open_message(msginfo, FALSE)) == NULL) return -1;
        procheader_get_header_fields(fp, entries);
-       fclose(fp);
+       claws_fclose(fp);
 
        he = entries;
        while (he != NULL && he->name != NULL) {
@@ -3120,6 +3123,7 @@ static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
                quote_fmt_parse();
 
                buf = quote_fmt_get_buffer();
+
                if (buf == NULL)
                        alertpanel_error(_("The \"Quotation mark\" of the template is invalid."));
                else
@@ -3153,11 +3157,14 @@ static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
                }
 
                buf = quote_fmt_get_buffer();
+
                if (buf == NULL) {
                        gint line = quote_fmt_get_line();
                        alertpanel_error(err_msg, line);
+
                        goto error;
                }
+
        } else
                buf = "";
 
@@ -3616,33 +3623,61 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
        gint len;
        FILE *fp;
        gboolean prev_autowrap;
+#ifdef G_OS_WIN32
+       GFile *f;
+       GFileInfo *fi;
+       GError *error = NULL;
+#else
        GStatBuf file_stat;
+#endif
        int ret;
+       goffset size;
        GString *file_contents = NULL;
        ComposeInsertResult result = COMPOSE_INSERT_SUCCESS;
 
        cm_return_val_if_fail(file != NULL, COMPOSE_INSERT_NO_FILE);
 
        /* get the size of the file we are about to insert */
+#ifdef G_OS_WIN32
+       f = g_file_new_for_path(file);
+       fi = g_file_query_info(f, "standard::size",
+                       G_FILE_QUERY_INFO_NONE, NULL, &error);
+       ret = 0;
+       if (error != NULL) {
+               g_warning(error->message);
+               ret = 1;
+               g_error_free(error);
+               g_object_unref(f);
+       }
+#else
        ret = g_stat(file, &file_stat);
+#endif
        if (ret != 0) {
                gchar *shortfile = g_path_get_basename(file);
                alertpanel_error(_("Could not get size of file '%s'."), shortfile);
                g_free(shortfile);
                return COMPOSE_INSERT_NO_FILE;
        } else if (prefs_common.warn_large_insert == TRUE) {
+#ifdef G_OS_WIN32
+               size = g_file_info_get_size(fi);
+               g_object_unref(fi);
+               g_object_unref(f);
+#else
+               size = file_stat.st_size;
+#endif
 
                /* ask user for confirmation if the file is large */
                if (prefs_common.warn_large_insert_size < 0 ||
-                   file_stat.st_size > (prefs_common.warn_large_insert_size * 1024)) {
+                   size > ((goffset) prefs_common.warn_large_insert_size * 1024)) {
                        AlertValue aval;
                        gchar *msg;
 
                        msg = g_strdup_printf(_("You are about to insert a file of %s "
                                                "in the message body. Are you sure you want to do that?"),
-                                               to_human_readable(file_stat.st_size));
+                                               to_human_readable(size));
                        aval = alertpanel_full(_("Are you sure?"), msg, GTK_STOCK_CANCEL,
-                                       g_strconcat("+", _("_Insert"), NULL), NULL, TRUE, NULL, ALERT_QUESTION, G_ALERTDEFAULT);
+                                       _("_Insert"), NULL, ALERTFOCUS_SECOND, TRUE,
+                                       NULL, ALERT_QUESTION);
                        g_free(msg);
 
                        /* do we ask for confirmation next time? */
@@ -3660,8 +3695,8 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
        }
 
 
-       if ((fp = g_fopen(file, "rb")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
+       if ((fp = claws_fopen(file, "rb")) == NULL) {
+               FILE_OP_ERROR(file, "claws_fopen");
                return COMPOSE_INSERT_READ_ERROR;
        }
 
@@ -3680,7 +3715,7 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
        cur_encoding = conv_get_locale_charset_str_no_utf8();
 
        file_contents = g_string_new("");
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
+       while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                gchar *str;
 
                if (g_utf8_validate(buf, -1, NULL) == TRUE)
@@ -3724,7 +3759,7 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
        }
 
        g_string_free(file_contents, TRUE);
-       fclose(fp);
+       claws_fclose(fp);
 
        return result;
 }
@@ -3768,19 +3803,19 @@ static gboolean compose_attach_append(Compose *compose, const gchar *file,
        if (size == 0 && !compose->batch) {
                gchar * msg = g_strdup_printf(_("File %s is empty."), filename);
                AlertValue aval = alertpanel_full(_("Empty file"), msg, 
-                               GTK_STOCK_CANCEL,  g_strconcat("+", _("_Attach anyway"), NULL), NULL, FALSE,
-                               NULL, ALERT_WARNING, G_ALERTDEFAULT);
+                               GTK_STOCK_CANCEL,  _("_Attach anyway"), NULL,
+                               ALERTFOCUS_SECOND, FALSE, NULL, ALERT_WARNING);
                g_free(msg);
 
                if (aval != G_ALERTALTERNATE) {
                        return FALSE;
                }
        }
-       if ((fp = g_fopen(file, "rb")) == NULL) {
+       if ((fp = claws_fopen(file, "rb")) == NULL) {
                alertpanel_error(_("Can't read %s."), filename);
                return FALSE;
        }
-       fclose(fp);
+       claws_fclose(fp);
 
        ainfo = g_new0(AttachInfo, 1);
        auto_ainfo = g_auto_pointer_new_with_free
@@ -3868,13 +3903,13 @@ static gboolean compose_attach_append(Compose *compose, const gchar *file,
        return TRUE;
 }
 
-static void compose_use_signing(Compose *compose, gboolean use_signing)
+void compose_use_signing(Compose *compose, gboolean use_signing)
 {
        compose->use_signing = use_signing;
        cm_toggle_menu_set_active_full(compose->ui_manager, "Menu/Options/Sign", use_signing);
 }
 
-static void compose_use_encryption(Compose *compose, gboolean use_encryption)
+void compose_use_encryption(Compose *compose, gboolean use_encryption)
 {
        compose->use_encryption = use_encryption;
        cm_toggle_menu_set_active_full(compose->ui_manager, "Menu/Options/Encrypt", use_encryption);
@@ -4082,10 +4117,22 @@ static int compose_itemized_length(GtkTextBuffer *buffer,
        }
 
        clen = g_unichar_to_utf8(wc, ch);
-       if (clen != 1)
-               return 0;
-
-       if (!strchr("*-+", ch[0]))
+       if (!((clen == 1 && strchr("*-+", ch[0])) ||
+           (clen == 3 && (
+               wc == 0x2022 || /* BULLET */
+               wc == 0x2023 || /* TRIANGULAR BULLET */
+               wc == 0x2043 || /* HYPHEN BULLET */
+               wc == 0x204c || /* BLACK LEFTWARDS BULLET */
+               wc == 0x204d || /* BLACK RIGHTWARDS BULLET */
+               wc == 0x2219 || /* BULLET OPERATOR */
+               wc == 0x25d8 || /* INVERSE BULLET */
+               wc == 0x25e6 || /* WHITE BULLET */
+               wc == 0x2619 || /* REVERSED ROTATED FLORAL HEART BULLET */
+               wc == 0x2765 || /* ROTATED HEAVY BLACK HEART BULLET */
+               wc == 0x2767 || /* ROTATED FLORAL HEART BULLET */
+               wc == 0x29be || /* CIRCLED WHITE BULLET */
+               wc == 0x29bf    /* CIRCLED BULLET */
+               ))))
                return 0;
 
        gtk_text_iter_forward_char(&iter);
@@ -4209,10 +4256,16 @@ static gboolean compose_get_line_break_pos(GtkTextBuffer *buffer,
 
        for (; *p != '\0' && i < len; i++) {
                PangoLogAttr *attr = attrs + i;
-               gunichar wc;
+               gunichar wc = g_utf8_get_char(p);
                gint uri_len;
 
-               if (attr->is_line_break && can_break && was_white && !prev_dont_break)
+               /* attr->is_line_break will be false for some characters that
+                * we want to break a line before, like '/' or ':', so we
+                * also allow breaking on any non-wide character. The
+                * mentioned pango attribute is still useful to decide on
+                * line breaks when wide characters are involved. */
+               if ((!g_unichar_iswide(wc) || attr->is_line_break)
+                               && can_break && was_white && !prev_dont_break)
                        pos = i;
                
                was_white = attr->is_white;
@@ -4230,7 +4283,6 @@ static gboolean compose_get_line_break_pos(GtkTextBuffer *buffer,
                        continue;
                }
 
-               wc = g_utf8_get_char(p);
                if (g_unichar_iswide(wc)) {
                        col += 2;
                        if (prev_dont_break && can_break && attr->is_line_break)
@@ -4284,7 +4336,7 @@ static gboolean compose_join_next_line(Compose *compose,
        next_quote_str = compose_get_quote_str(buffer, &iter_, &quote_len);
 
        if ((quote_str || next_quote_str) &&
-           strcmp2(quote_str, next_quote_str) != 0) {
+           g_strcmp0(quote_str, next_quote_str) != 0) {
                g_free(next_quote_str);
                return FALSE;
        }
@@ -4903,9 +4955,7 @@ static void compose_select_account(Compose *compose, PrefsAccount *account,
 {
        gchar *from = NULL, *header = NULL;
        ComposeHeaderEntry *header_entry;
-#if GTK_CHECK_VERSION(2, 24, 0)
        GtkTreeIter iter;
-#endif
 
        cm_return_if_fail(account != NULL);
 
@@ -4929,16 +4979,18 @@ static void compose_select_account(Compose *compose, PrefsAccount *account,
 
        compose_set_title(compose);
 
-       if (account->default_sign && compose->mode != COMPOSE_REDIRECT)
+       compose_activate_privacy_system(compose, account, FALSE);
+
+       if (account->default_sign && privacy_system_can_sign(compose->privacy_system) &&
+           compose->mode != COMPOSE_REDIRECT)
                cm_toggle_menu_set_active_full(compose->ui_manager, "Menu/Options/Sign", TRUE);
        else
                cm_toggle_menu_set_active_full(compose->ui_manager, "Menu/Options/Sign", FALSE);
-       if (account->default_encrypt && compose->mode != COMPOSE_REDIRECT)
+       if (account->default_encrypt && privacy_system_can_encrypt(compose->privacy_system) &&
+           compose->mode != COMPOSE_REDIRECT)
                cm_toggle_menu_set_active_full(compose->ui_manager, "Menu/Options/Encrypt", TRUE);
        else
                cm_toggle_menu_set_active_full(compose->ui_manager, "Menu/Options/Encrypt", FALSE);
-                                      
-       activate_privacy_system(compose, account, FALSE);
 
        if (!init && compose->mode != COMPOSE_REDIRECT) {
                undo_block(compose->undostruct);
@@ -4947,13 +4999,9 @@ static void compose_select_account(Compose *compose, PrefsAccount *account,
        }
        
        header_entry = (ComposeHeaderEntry *) compose->header_list->data;
-#if !GTK_CHECK_VERSION(2, 24, 0)
-       header = gtk_combo_box_get_active_text(GTK_COMBO_BOX(header_entry->combo));
-#else
        if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(header_entry->combo), &iter))
                gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(
                        header_entry->combo)), &iter, COMBOBOX_TEXT, &header, -1);
-#endif
        
        if (header && !strlen(gtk_entry_get_text(GTK_ENTRY(header_entry->entry)))) {
                if (account->protocol == A_NNTP) {
@@ -5061,7 +5109,7 @@ static gboolean compose_check_for_set_recipients(Compose *compose)
                                           prefs_common_translated_header_name("Cc"));
                        aval = alertpanel(_("Send"),
                                          text,
-                                         GTK_STOCK_CANCEL, g_strconcat("+", _("_Send"), NULL), NULL);
+                                         GTK_STOCK_CANCEL, _("_Send"), NULL, ALERTFOCUS_SECOND);
                        g_free(text);
                        if (aval != G_ALERTALTERNATE)
                                return FALSE;
@@ -5098,7 +5146,7 @@ static gboolean compose_check_for_set_recipients(Compose *compose)
                                           prefs_common_translated_header_name("Bcc"));
                        aval = alertpanel(_("Send"),
                                          text,
-                                         GTK_STOCK_CANCEL, g_strconcat("+", _("_Send"), NULL), NULL);
+                                         GTK_STOCK_CANCEL, _("_Send"), NULL, ALERTFOCUS_SECOND);
                        g_free(text);
                        if (aval != G_ALERTALTERNATE)
                                return FALSE;
@@ -5128,20 +5176,15 @@ static gboolean compose_check_entries(Compose *compose, gboolean check_everythin
                if (*str == '\0' && check_everything == TRUE &&
                    compose->mode != COMPOSE_REDIRECT) {
                        AlertValue aval;
-                       gchar *button_label;
                        gchar *message;
 
-                       if (compose->sending)
-                               button_label = g_strconcat("+", _("_Send"), NULL);
-                       else
-                               button_label = g_strconcat("+", _("_Queue"), NULL);
                        message = g_strdup_printf(_("Subject is empty. %s"),
                                        compose->sending?_("Send it anyway?"):
                                        _("Queue it anyway?"));
 
                        aval = alertpanel_full(compose->sending?_("Send"):_("Send later"), message,
-                                              GTK_STOCK_CANCEL, button_label, NULL, TRUE, NULL,
-                                              ALERT_QUESTION, G_ALERTDEFAULT);
+                                              GTK_STOCK_CANCEL, compose->sending?_("_Send"):_("_Queue"), NULL,
+                                              ALERTFOCUS_FIRST, TRUE, NULL, ALERT_QUESTION);
                        g_free(message);
                        if (aval & G_ALERTDISABLE) {
                                aval &= ~G_ALERTDISABLE;
@@ -5166,9 +5209,9 @@ static gboolean compose_check_entries(Compose *compose, gboolean check_everythin
                        entry = gtk_editable_get_chars(GTK_EDITABLE(((ComposeHeaderEntry *)list->data)->entry), 0, -1);
                        g_strstrip(header);
                        g_strstrip(entry);
-                       if ((entry[0] != '\0')
-                       &&      (strcmp(header, prefs_common_translated_header_name("To:"))
-                       ||  strcmp(header, prefs_common_translated_header_name("Cc:")))) {
+                       if ((entry[0] != '\0') &&
+                           (!strcmp(header, prefs_common_translated_header_name("To:")) ||
+                            !strcmp(header, prefs_common_translated_header_name("Cc:")))) {
                                cnt++;
                        }
                        g_free(header);
@@ -5176,20 +5219,15 @@ static gboolean compose_check_entries(Compose *compose, gboolean check_everythin
                }
                if (cnt > prefs_common.warn_sending_many_recipients_num) {
                        AlertValue aval;
-                       gchar *button_label;
                        gchar *message;
 
-                       if (compose->sending)
-                               button_label = g_strconcat("+", _("_Send"), NULL);
-                       else
-                               button_label = g_strconcat("+", _("_Queue"), NULL);
                        message = g_strdup_printf(_("Sending to %d recipients. %s"), cnt,
                                        compose->sending?_("Send it anyway?"):
                                        _("Queue it anyway?"));
 
                        aval = alertpanel_full(compose->sending?_("Send"):_("Send later"), message,
-                                              GTK_STOCK_CANCEL, button_label, NULL, TRUE, NULL,
-                                              ALERT_QUESTION, G_ALERTDEFAULT);
+                                              GTK_STOCK_CANCEL, compose->sending?_("_Send"):_("_Queue"), NULL,
+                                              ALERTFOCUS_FIRST, TRUE, NULL, ALERT_QUESTION);
                        g_free(message);
                        if (aval & G_ALERTDISABLE) {
                                aval &= ~G_ALERTDISABLE;
@@ -5206,11 +5244,49 @@ static gboolean compose_check_entries(Compose *compose, gboolean check_everythin
        return TRUE;
 }
 
+static void _display_queue_error(ComposeQueueResult val)
+{
+       switch (val) {
+               case COMPOSE_QUEUE_SUCCESS:
+                       break;
+               case COMPOSE_QUEUE_ERROR_NO_MSG:
+                       alertpanel_error(_("Could not queue message."));
+                       break;
+               case COMPOSE_QUEUE_ERROR_WITH_ERRNO:
+                       alertpanel_error(_("Could not queue message:\n\n%s."),
+                                       g_strerror(errno));
+                       break;
+               case COMPOSE_QUEUE_ERROR_SIGNING_FAILED:
+                       alertpanel_error(_("Could not queue message for sending:\n\n"
+                                               "Signature failed: %s"),
+                                       privacy_peek_error() ? privacy_get_error() : _("Unknown error"));
+                       break;
+               case COMPOSE_QUEUE_ERROR_ENCRYPT_FAILED:
+                       alertpanel_error(_("Could not queue message for sending:\n\n"
+                                               "Encryption failed: %s"),
+                                       privacy_peek_error() ? privacy_get_error() : _("Unknown error"));
+                       break;
+               case COMPOSE_QUEUE_ERROR_CHAR_CONVERSION:
+                       alertpanel_error(_("Could not queue message for sending:\n\n"
+                                               "Charset conversion failed."));
+                       break;
+               case COMPOSE_QUEUE_ERROR_NO_ENCRYPTION_KEY:
+                       alertpanel_error(_("Could not queue message for sending:\n\n"
+                                               "Couldn't get recipient encryption key."));
+                       break;
+               default:
+                       /* unhandled error */
+                       debug_print("oops, unhandled compose_queue() return value %d\n",
+                                       val);
+                       break;
+       }
+}
+
 gint compose_send(Compose *compose)
 {
        gint msgnum;
        FolderItem *folder = NULL;
-       gint val = -1;
+       ComposeQueueResult val = COMPOSE_QUEUE_ERROR_NO_MSG;
        gchar *msgpath = NULL;
        gboolean discard_window = FALSE;
        gchar *errstr = NULL;
@@ -5235,27 +5311,13 @@ gint compose_send(Compose *compose)
        inc_lock();
        val = compose_queue(compose, &msgnum, &folder, &msgpath, TRUE);
 
-       if (val) {
+       if (val != COMPOSE_QUEUE_SUCCESS) {
                if (compose->batch) {
                        gtk_widget_show_all(compose->window);
                }
-               if (val == -4) {
-                       alertpanel_error(_("Could not queue message for sending:\n\n"
-                                          "Charset conversion failed."));
-               } else if (val == -5) {
-                       alertpanel_error(_("Could not queue message for sending:\n\n"
-                                          "Couldn't get recipient encryption key."));
-               } else if (val == -6) {
-                       /* silent error */
-               } else if (val == -3) {
-                       if (privacy_peek_error())
-                       alertpanel_error(_("Could not queue message for sending:\n\n"
-                                          "Signature failed: %s"), privacy_get_error());
-               } else if (val == -2 && errno != 0) {
-                       alertpanel_error(_("Could not queue message for sending:\n\n%s."), g_strerror(errno));
-               } else {
-                       alertpanel_error(_("Could not queue message for sending."));
-               }
+
+               _display_queue_error(val);
+
                goto bail;
        }
 
@@ -5461,7 +5523,7 @@ static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
                get_rfc822_date_hide_tz(date, sizeof(date));
        else
                get_rfc822_date(date, sizeof(date));
-       err |= (fprintf(fp, "Resent-Date: %s\n", buf) < 0);
+       err |= (fprintf(fp, "Resent-Date: %s\n", date) < 0);
 
        /* Resent-From */
        if (compose->account->name && *compose->account->name) {
@@ -5500,7 +5562,7 @@ static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
                return -1;
 
        /* separator between header and body */
-       err |= (fputs("\n", fp) == EOF);
+       err |= (claws_fputs("\n", fp) == EOF);
 
        return (err ? -1:0);
 }
@@ -5533,8 +5595,8 @@ static gint compose_redirect_write_to_file(Compose *compose, FILE *fdest)
                };
        gint ret = 0;
 
-       if ((fp = g_fopen(compose->redirect_filename, "rb")) == NULL) {
-               FILE_OP_ERROR(compose->redirect_filename, "fopen");
+       if ((fp = claws_fopen(compose->redirect_filename, "rb")) == NULL) {
+               FILE_OP_ERROR(compose->redirect_filename, "claws_fopen");
                return -1;
        }
 
@@ -5552,7 +5614,7 @@ static gint compose_redirect_write_to_file(Compose *compose, FILE *fdest)
                        buf = NULL;
                        continue;
                }
-               if (fputs(buf, fdest) == -1) {
+               if (claws_fputs(buf, fdest) == -1) {
                        g_free(buf);
                        buf = NULL;
                        goto error;
@@ -5561,7 +5623,7 @@ static gint compose_redirect_write_to_file(Compose *compose, FILE *fdest)
                if (!prefs_common.redirect_keep_from) {
                        if (g_ascii_strncasecmp(buf, "From:",
                                          strlen("From:")) == 0) {
-                               err |= (fputs(" (by way of ", fdest) == EOF);
+                               err |= (claws_fputs(" (by way of ", fdest) == EOF);
                                if (compose->account->name
                                    && *compose->account->name) {
                                        gchar buffer[BUFFSIZE];
@@ -5577,13 +5639,13 @@ static gint compose_redirect_write_to_file(Compose *compose, FILE *fdest)
                                } else
                                        err |= (fprintf(fdest, "%s",
                                                compose->account->address) < 0);
-                               err |= (fputs(")", fdest) == EOF);
+                               err |= (claws_fputs(")", fdest) == EOF);
                        }
                }
 
                g_free(buf);
                buf = NULL;
-               if (fputs("\n", fdest) == -1)
+               if (claws_fputs("\n", fdest) == -1)
                        goto error;
        }
 
@@ -5593,17 +5655,17 @@ static gint compose_redirect_write_to_file(Compose *compose, FILE *fdest)
        if (compose_redirect_write_headers(compose, fdest))
                goto error;
 
-       while ((len = fread(rewrite_buf, sizeof(gchar), sizeof(rewrite_buf), fp)) > 0) {
-               if (fwrite(rewrite_buf, sizeof(gchar), len, fdest) != len)
+       while ((len = claws_fread(rewrite_buf, sizeof(gchar), sizeof(rewrite_buf), fp)) > 0) {
+               if (claws_fwrite(rewrite_buf, sizeof(gchar), len, fdest) != len)
                        goto error;
        }
 
-       fclose(fp);
+       claws_fclose(fp);
 
        return 0;
 
 error:
-       fclose(fp);
+       claws_fclose(fp);
 
        return -1;
 }
@@ -5611,7 +5673,7 @@ error:
 static gint compose_write_to_file(Compose *compose, FILE *fp, gint action, gboolean attach_parts)
 {
        GtkTextBuffer *buffer;
-       GtkTextIter start, end;
+       GtkTextIter start, end, tmp;
        gchar *chars, *tmp_enc_file, *content;
        gchar *buf, *msg;
        const gchar *out_codeset;
@@ -5643,10 +5705,21 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action, gbool
        mimemsg->data.mem = compose_get_header(compose);
 
        /* Create text part MimeInfo */
-       /* get all composed text */
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(compose->text));
-       gtk_text_buffer_get_start_iter(buffer, &start);
        gtk_text_buffer_get_end_iter(buffer, &end);
+       tmp = end;
+
+       /* We make sure that there is a newline at the end. */
+       if (action == COMPOSE_WRITE_FOR_SEND && gtk_text_iter_backward_char(&tmp)) {
+               chars = gtk_text_buffer_get_text(buffer, &tmp, &end, FALSE);
+               if (*chars != '\n') {
+                       gtk_text_buffer_insert(buffer, &end, "\n", 1);
+               }
+               g_free(chars);
+       }
+
+       /* get all composed text */
+       gtk_text_buffer_get_start_iter(buffer, &start);
        chars = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
 
        out_codeset = conv_get_charset_str(compose->out_encoding);
@@ -5715,13 +5788,13 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action, gbool
                                                "to the specified %s charset.\n"
                                                "Send it as %s?"), out_codeset, src_codeset);
                        aval = alertpanel_full(_("Error"), msg, GTK_STOCK_CANCEL,
-                                              g_strconcat("+", _("_Send"), NULL), NULL, FALSE,
-                                             NULL, ALERT_ERROR, G_ALERTDEFAULT);
+                                              _("_Send"), NULL, ALERTFOCUS_SECOND, FALSE,
+                                             NULL, ALERT_ERROR);
                        g_free(msg);
 
                        if (aval != G_ALERTALTERNATE) {
                                g_free(chars);
-                               return -3;
+                               return COMPOSE_QUEUE_ERROR_CHAR_CONVERSION;
                        } else {
                                buf = chars;
                                out_codeset = src_codeset;
@@ -5777,11 +5850,12 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action, gbool
                           "The contents of the message might be broken on the way to the delivery.\n"
                           "\n"
                           "Send it anyway?"), line + 1);
-               aval = alertpanel(_("Warning"), msg, GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL);
+               aval = alertpanel(_("Warning"), msg, GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL,
+                               ALERTFOCUS_FIRST);
                g_free(msg);
                if (aval != G_ALERTALTERNATE) {
                        g_free(buf);
-                       return -1;
+                       return COMPOSE_QUEUE_ERROR_NO_MSG;
                }
        }
        
@@ -5811,7 +5885,7 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action, gbool
                g_node_append(mimemsg->node, mimempart->node);
 
                if (compose_add_attachments(compose, mimempart) < 0)
-                       return -1;
+                       return COMPOSE_QUEUE_ERROR_NO_MSG;
        } else
                g_node_append(mimemsg->node, mimetext->node);
 
@@ -5838,7 +5912,7 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action, gbool
                        compose->account, from_addr)) {
                        g_free(from_name);
                        g_free(from_addr);
-                       return -2;
+                       return COMPOSE_QUEUE_ERROR_SIGNING_FAILED;
        }
        g_free(from_name);
        g_free(from_addr);
@@ -5853,20 +5927,20 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action, gbool
                                debug_print("saving sent message unencrypted...\n");
                                FILE *tmpfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmp_enc_file);
                                if (tmpfp) {
-                                       fclose(tmpfp);
+                                       claws_fclose(tmpfp);
 
                                        /* fp now points to a file with headers written,
                                         * let's make a copy. */
                                        rewind(fp);
                                        content = file_read_stream_to_str(fp);
 
-                                       str_write_to_file(content, tmp_enc_file);
+                                       str_write_to_file(content, tmp_enc_file, TRUE);
                                        g_free(content);
 
                                        /* Now write the unencrypted body. */
-                                       if ((tmpfp = g_fopen(tmp_enc_file, "a")) != NULL) {
+                                       if ((tmpfp = claws_fopen(tmp_enc_file, "a")) != NULL) {
                                                procmime_write_mimeinfo(mimemsg, tmpfp);
-                                               fclose(tmpfp);
+                                               claws_fclose(tmpfp);
 
                                                outbox = folder_find_item_from_identifier(compose_get_save_to(compose));
                                                if (!outbox)
@@ -5884,8 +5958,7 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action, gbool
                        if (!privacy_encrypt(compose->privacy_system, mimemsg, compose->encdata)) {
                                debug_print("Couldn't encrypt mime structure: %s.\n",
                                                privacy_get_error());
-                               alertpanel_error(_("Couldn't encrypt the email: %s"),
-                                               privacy_get_error());
+                               return COMPOSE_QUEUE_ERROR_ENCRYPT_FAILED;
                        }
                }
        }
@@ -5905,8 +5978,8 @@ static gint compose_write_body_to_file(Compose *compose, const gchar *file)
        size_t len;
        gchar *chars, *tmp;
 
-       if ((fp = g_fopen(file, "wb")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
+       if ((fp = claws_fopen(file, "wb")) == NULL) {
+               FILE_OP_ERROR(file, "claws_fopen");
                return -1;
        }
 
@@ -5926,24 +5999,24 @@ static gint compose_write_body_to_file(Compose *compose, const gchar *file)
 
        g_free(tmp);
        if (!chars) {
-               fclose(fp);
+               claws_fclose(fp);
                claws_unlink(file);
                return -1;
        }
        /* write body */
        len = strlen(chars);
-       if (fwrite(chars, sizeof(gchar), len, fp) != len) {
-               FILE_OP_ERROR(file, "fwrite");
+       if (claws_fwrite(chars, sizeof(gchar), len, fp) != len) {
+               FILE_OP_ERROR(file, "claws_fwrite");
                g_free(chars);
-               fclose(fp);
+               claws_fclose(fp);
                claws_unlink(file);
                return -1;
        }
 
        g_free(chars);
 
-       if (fclose(fp) == EOF) {
-               FILE_OP_ERROR(file, "fclose");
+       if (claws_safe_fclose(fp) == EOF) {
+               FILE_OP_ERROR(file, "claws_fclose");
                claws_unlink(file);
                return -1;
        }
@@ -5991,7 +6064,7 @@ static void compose_remove_draft(Compose *compose)
 
 }
 
-gint compose_queue(Compose *compose, gint *msgnum, FolderItem **item, gchar **msgpath,
+ComposeQueueResult compose_queue(Compose *compose, gint *msgnum, FolderItem **item, gchar **msgpath,
                   gboolean remove_reedit_target)
 {
        return compose_queue_sub (compose, msgnum, item, msgpath, FALSE, remove_reedit_target);
@@ -6006,8 +6079,8 @@ static gboolean compose_warn_encryption(Compose *compose)
                return TRUE;
 
        val = alertpanel_full(_("Encryption warning"), warning,
-                 GTK_STOCK_CANCEL, g_strconcat("+", _("C_ontinue"), NULL), NULL,
-                 TRUE, NULL, ALERT_WARNING, G_ALERTALTERNATE);
+                 GTK_STOCK_CANCEL, _("C_ontinue"), NULL, ALERTFOCUS_SECOND,
+                 TRUE, NULL, ALERT_WARNING);
        if (val & G_ALERTDISABLE) {
                val &= ~G_ALERTDISABLE;
                if (val == G_ALERTALTERNATE)
@@ -6022,7 +6095,7 @@ static gboolean compose_warn_encryption(Compose *compose)
        } 
 }
 
-static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item, 
+static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item, 
                              gchar **msgpath, gboolean perform_checks,
                              gboolean remove_reedit_target)
 {
@@ -6041,12 +6114,12 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                if (compose->batch) {
                        gtk_widget_show_all(compose->window);
                }
-                return -1;
+                return COMPOSE_QUEUE_ERROR_NO_MSG;
        }
 
        if (!compose->to_list && !compose->newsgroup_list) {
                g_warning("can't get recipient list.");
-                return -1;
+                return COMPOSE_QUEUE_ERROR_NO_MSG;
         }
 
        if (compose->to_list) {
@@ -6056,7 +6129,7 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                        mailac = cur_account;
                else if (!(mailac = compose_current_mail_account())) {
                        alertpanel_error(_("No account for sending mails available!"));
-                       return -1;
+                       return COMPOSE_QUEUE_ERROR_NO_MSG;
                }
        }
 
@@ -6065,7 +6138,7 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                         newsac = compose->account;
                 else {
                        alertpanel_error(_("Selected account isn't NNTP: Posting is impossible."));
-                       return -1;
+                       return COMPOSE_QUEUE_ERROR_NO_MSG;
                }                       
        }
 
@@ -6073,10 +6146,10 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
        tmp = g_strdup_printf("%s%cqueue.%p%08x", get_tmp_dir(),
                              G_DIR_SEPARATOR, compose, (guint) rand());
        debug_print("queuing to %s\n", tmp);
-       if ((fp = g_fopen(tmp, "w+b")) == NULL) {
-               FILE_OP_ERROR(tmp, "fopen");
+       if ((fp = claws_fopen(tmp, "w+b")) == NULL) {
+               FILE_OP_ERROR(tmp, "claws_fopen");
                g_free(tmp);
-               return -2;
+               return COMPOSE_QUEUE_ERROR_WITH_ERRNO;
        }
 
        if (change_file_mode_rw(fp, tmp) < 0) {
@@ -6108,7 +6181,7 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
        else
                err |= (fprintf(fp, "NSV:\n") < 0);
        err |= (fprintf(fp, "SSH:\n") < 0);
-       /* write recepient list */
+       /* write recipient list */
        if (compose->to_list) {
                err |= (fprintf(fp, "R:<%s>", (gchar *)compose->to_list->data) < 0);
                for (cur = compose->to_list->next; cur != NULL;
@@ -6124,7 +6197,7 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                        err |= (fprintf(fp, ",%s", (gchar *)cur->data) < 0);
                err |= (fprintf(fp, "\n") < 0);
        }
-       /* Sylpheed account IDs */
+       /* account IDs */
        if (mailac)
                err |= (fprintf(fp, "MAID:%d\n", mailac->account_id) < 0);
        if (newsac)
@@ -6136,10 +6209,10 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                err |= (fprintf(fp, "X-Claws-Sign:%d\n", compose->use_signing) < 0);
                if (compose->use_encryption) {
                        if (!compose_warn_encryption(compose)) {
-                               fclose(fp);
+                               claws_fclose(fp);
                                claws_unlink(tmp);
                                g_free(tmp);
-                               return -6;
+                               return COMPOSE_QUEUE_ERROR_NO_MSG;
                        }
                        if (mailac && mailac->encrypt_to_self) {
                                GSList *tmp_list = g_slist_copy(compose->to_list);
@@ -6161,10 +6234,10 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                                 * key selection */
                                if (err == TRUE)
                                        g_warning("failed to write queue message");
-                               fclose(fp);
+                               claws_fclose(fp);
                                claws_unlink(tmp);
                                g_free(tmp);
-                               return -5;
+                               return COMPOSE_QUEUE_ERROR_NO_ENCRYPTION_KEY;
                        }
                }
        }
@@ -6214,32 +6287,32 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
 
        if (compose->redirect_filename != NULL) {
                if (compose_redirect_write_to_file(compose, fp) < 0) {
-                       fclose(fp);
+                       claws_fclose(fp);
                        claws_unlink(tmp);
                        g_free(tmp);
-                       return -2;
+                       return COMPOSE_QUEUE_ERROR_WITH_ERRNO;
                }
        } else {
                gint result = 0;
                if ((result = compose_write_to_file(compose, fp, COMPOSE_WRITE_FOR_SEND, TRUE)) < 0) {
-                       fclose(fp);
+                       claws_fclose(fp);
                        claws_unlink(tmp);
                        g_free(tmp);
-                       return result - 1; /* -2 for a generic error, -3 for signing error, -4 for encoding */
+                       return result;
                }
        }
        if (err == TRUE) {
                g_warning("failed to write queue message");
-               fclose(fp);
+               claws_fclose(fp);
                claws_unlink(tmp);
                g_free(tmp);
-               return -2;
+               return COMPOSE_QUEUE_ERROR_WITH_ERRNO;
        }
-       if (fclose(fp) == EOF) {
-               FILE_OP_ERROR(tmp, "fclose");
+       if (claws_safe_fclose(fp) == EOF) {
+               FILE_OP_ERROR(tmp, "claws_fclose");
                claws_unlink(tmp);
                g_free(tmp);
-               return -2;
+               return COMPOSE_QUEUE_ERROR_WITH_ERRNO;
        }
 
        if (item && *item) {
@@ -6251,14 +6324,14 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                g_warning("can't find queue folder");
                claws_unlink(tmp);
                g_free(tmp);
-               return -1;
+               return COMPOSE_QUEUE_ERROR_NO_MSG;
        }
        folder_item_scan(queue);
        if ((num = folder_item_add_msg(queue, tmp, NULL, FALSE)) < 0) {
                g_warning("can't queue the message");
                claws_unlink(tmp);
                g_free(tmp);
-               return -1;
+               return COMPOSE_QUEUE_ERROR_NO_MSG;
        }
        
        if (msgpath == NULL) {
@@ -6267,6 +6340,20 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
        } else
                *msgpath = tmp;
 
+       if (compose->mode == COMPOSE_REEDIT && compose->targetinfo) {
+               MsgInfo *mi = folder_item_get_msginfo(queue, num);
+               if (mi) {
+                       procmsg_msginfo_change_flags(mi,
+                               compose->targetinfo->flags.perm_flags,
+                               compose->targetinfo->flags.tmp_flags & ~(MSG_COPY | MSG_MOVE | MSG_MOVE_DONE),
+                               0, 0);
+
+                       g_slist_free(mi->tags);
+                       mi->tags = g_slist_copy(compose->targetinfo->tags);
+                       procmsg_msginfo_free(&mi);
+               }
+       }
+
        if (compose->mode == COMPOSE_REEDIT && remove_reedit_target) {
                compose_remove_reedit_target(compose, FALSE);
        }
@@ -6276,7 +6363,7 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                *item = queue;
        }
 
-       return 0;
+       return COMPOSE_QUEUE_SUCCESS;
 }
 
 static int compose_add_attachments(Compose *compose, MimeInfo *parent)
@@ -6284,7 +6371,14 @@ static int compose_add_attachments(Compose *compose, MimeInfo *parent)
        AttachInfo *ainfo;
        GtkTreeView *tree_view = GTK_TREE_VIEW(compose->attach_clist);
        MimeInfo *mimepart;
+#ifdef G_OS_WIN32
+       GFile *f;
+       GFileInfo *fi;
+       GError *error = NULL;
+#else
        GStatBuf statbuf;
+#endif
+       goffset size;
        gchar *type, *subtype;
        GtkTreeModel *model;
        GtkTreeIter iter;
@@ -6298,23 +6392,40 @@ static int compose_add_attachments(Compose *compose, MimeInfo *parent)
                
                if (!is_file_exist(ainfo->file)) {
                        gchar *msg = g_strdup_printf(_("Attachment %s doesn't exist anymore. Ignore?"), ainfo->file);
-                       AlertValue val = alertpanel_full(_("Warning"), msg, _("Cancel sending"), _("Ignore attachment"),
-                                     NULL, FALSE, NULL, ALERT_WARNING, G_ALERTDEFAULT);
+                       AlertValue val = alertpanel_full(_("Warning"), msg,
+                                       _("Cancel sending"), _("Ignore attachment"), NULL,
+                                       ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING);
                        g_free(msg);
                        if (val == G_ALERTDEFAULT) {
                                return -1;
                        }
                        continue;
                }
+#ifdef G_OS_WIN32
+               f = g_file_new_for_path(ainfo->file);
+               fi = g_file_query_info(f, "standard::size",
+                               G_FILE_QUERY_INFO_NONE, NULL, &error);
+               if (error != NULL) {
+                       g_warning(error->message);
+                       g_error_free(error);
+                       g_object_unref(f);
+                       return -1;
+               }
+               size = g_file_info_get_size(fi);
+               g_object_unref(fi);
+               g_object_unref(f);
+#else
                if (g_stat(ainfo->file, &statbuf) < 0)
                        return -1;
+               size = statbuf.st_size;
+#endif
 
                mimepart = procmime_mimeinfo_new();
                mimepart->content = MIMECONTENT_FILE;
                mimepart->data.filename = g_strdup(ainfo->file);
                mimepart->tmp = FALSE; /* or we destroy our attachment */
                mimepart->offset = 0;
-               mimepart->length = statbuf.st_size;
+               mimepart->length = size;
 
                type = g_strdup(ainfo->content_type);
 
@@ -6347,7 +6458,7 @@ static int compose_add_attachments(Compose *compose, MimeInfo *parent)
                }
                if (ainfo->name && mimepart->type != MIMETYPE_MESSAGE) {
                        if (mimepart->type == MIMETYPE_APPLICATION && 
-                          !strcmp2(mimepart->subtype, "octet-stream"))
+                          !g_strcmp0(mimepart->subtype, "octet-stream"))
                                g_hash_table_insert(mimepart->typeparameters,
                                                g_strdup("name"), g_strdup(ainfo->name));
                        g_hash_table_insert(mimepart->dispositionparameters,
@@ -6358,7 +6469,7 @@ static int compose_add_attachments(Compose *compose, MimeInfo *parent)
                if (mimepart->type == MIMETYPE_MESSAGE
                    || mimepart->type == MIMETYPE_MULTIPART)
                        ainfo->encoding = ENC_BINARY;
-               else if (compose->use_signing) {
+               else if (compose->use_signing || compose->fwdinfo != NULL) {
                        if (ainfo->encoding == ENC_7BIT)
                                ainfo->encoding = ENC_QUOTED_PRINTABLE;
                        else if (ainfo->encoding == ENC_8BIT)
@@ -6969,11 +7080,11 @@ void compose_add_extra_header_entries(GtkListStore *model)
 
        if (extra_headers == NULL) {
                exhrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "extraheaderrc", NULL);
-               if ((exh = g_fopen(exhrc, "rb")) == NULL) {
+               if ((exh = claws_fopen(exhrc, "rb")) == NULL) {
                        debug_print("extra headers file not found\n");
                        goto extra_headers_done;
                }
-               while (fgets(buf, BUFFSIZE, exh) != NULL) {
+               while (claws_fgets(buf, BUFFSIZE, exh) != NULL) {
                        lastc = strlen(buf) - 1;        /* remove trailing control chars */
                        while (lastc >= 0 && buf[lastc] != ':')
                                buf[lastc--] = '\0';
@@ -6991,7 +7102,7 @@ void compose_add_extra_header_entries(GtkListStore *model)
                                        g_message("invalid extra header line: %s\n", buf);
                        }
                }
-               fclose(exh);
+               claws_fclose(exh);
 extra_headers_done:
                g_free(exhrc);
                extra_headers = g_slist_prepend(extra_headers, g_strdup("")); /* end of list */
@@ -7000,6 +7111,17 @@ extra_headers_done:
        g_slist_foreach(extra_headers, (GFunc)compose_add_extra_header, (gpointer)model);
 }
 
+#ifdef USE_LDAP
+static void _ldap_srv_func(gpointer data, gpointer user_data)
+{
+       LdapServer *server = (LdapServer *)data;
+       gboolean *enable = (gboolean *)user_data;
+
+       debug_print("%s server '%s'\n", (*enable == TRUE ? "enabling" : "disabling"), server->control->hostName);
+       server->searchFlag = *enable;
+}
+#endif
+
 static void compose_create_header_entry(Compose *compose) 
 {
        gchar *headers[] = {"To:", "Cc:", "Bcc:", "Newsgroups:", "Reply-To:", "Followup-To:", NULL};
@@ -7019,9 +7141,6 @@ static void compose_create_header_entry(Compose *compose)
 
        /* Combo box model */
        model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_BOOLEAN);
-#if !GTK_CHECK_VERSION(2, 24, 0)
-       combo = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
-#endif
        COMBOBOX_ADD(model, prefs_common_translated_header_name("To:"),
                        COMPOSE_TO);
        COMBOBOX_ADD(model, prefs_common_translated_header_name("Cc:"),
@@ -7037,13 +7156,11 @@ static void compose_create_header_entry(Compose *compose)
        compose_add_extra_header_entries(model);
 
        /* Combo box */
-#if GTK_CHECK_VERSION(2, 24, 0)
        combo = gtk_combo_box_new_with_model_and_entry(GTK_TREE_MODEL(model));
        GtkCellRenderer *cell = gtk_cell_renderer_text_new();
        gtk_cell_renderer_set_alignment(cell, 0.0, 0.5);
        gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), cell, TRUE);
        gtk_combo_box_set_entry_text_column(GTK_COMBO_BOX(combo), 0);
-#endif
        gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
        g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN(combo))), "grab_focus",
                         G_CALLBACK(compose_grab_focus_cb), compose);
@@ -7138,7 +7255,24 @@ static void compose_create_header_entry(Compose *compose)
        g_signal_connect(G_OBJECT(entry), "populate-popup",
                         G_CALLBACK(compose_entry_popup_extend),
                         NULL);
-       
+
+#ifdef USE_LDAP
+#ifndef PASSWORD_CRYPTO_OLD
+       GSList *pwd_servers = addrindex_get_password_protected_ldap_servers();
+       if (pwd_servers != NULL && master_passphrase() == NULL) {
+               gboolean enable = FALSE;
+               debug_print("Master passphrase not available, disabling password-protected LDAP servers for this compose window.\n");
+               /* Temporarily disable password-protected LDAP servers,
+                * because user did not provide a master passphrase.
+                * We can safely enable searchFlag on all servers in this list
+                * later, since addrindex_get_password_protected_ldap_servers()
+                * includes servers which have it enabled initially. */
+               g_slist_foreach(pwd_servers, _ldap_srv_func, &enable);
+               compose->passworded_ldap_servers = pwd_servers;
+       }
+#endif /* PASSWORD_CRYPTO_OLD */
+#endif /* USE_LDAP */
+
        address_completion_register_entry(GTK_ENTRY(entry), TRUE);
 
         headerentry->compose = compose;
@@ -7366,7 +7500,6 @@ static GtkWidget *compose_create_attach(Compose *compose)
        return attach_scrwin;
 }
 
-static void compose_savemsg_checkbtn_cb(GtkWidget *widget, Compose *compose);
 static void compose_savemsg_select_cb(GtkWidget *widget, Compose *compose);
 
 static GtkWidget *compose_create_others(Compose *compose)
@@ -7393,31 +7526,25 @@ static GtkWidget *compose_create_others(Compose *compose)
        if (account_get_special_folder(compose->account, F_OUTBOX)) {
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(savemsg_checkbtn), prefs_common.savemsg);
        }
-       g_signal_connect(G_OBJECT(savemsg_checkbtn), "toggled",
-                        G_CALLBACK(compose_savemsg_checkbtn_cb), compose);
 
-#if !GTK_CHECK_VERSION(2, 24, 0)
-       savemsg_combo = gtk_combo_box_entry_new_text();
-#else
        savemsg_combo = gtk_combo_box_text_new_with_entry();
-#endif
        compose->savemsg_checkbtn = savemsg_checkbtn;
        compose->savemsg_combo = savemsg_combo;
        gtk_widget_show(savemsg_combo);
 
        if (prefs_common.compose_save_to_history)
-#if !GTK_CHECK_VERSION(2, 24, 0)
-               combobox_set_popdown_strings(GTK_COMBO_BOX(savemsg_combo),
-                               prefs_common.compose_save_to_history);
-#else
                combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(savemsg_combo),
                                prefs_common.compose_save_to_history);
-#endif
        gtk_table_attach(GTK_TABLE(table), savemsg_combo, 1, 2, rowcount, rowcount + 1, GTK_FILL|GTK_EXPAND, GTK_SHRINK, 0, 0);
        gtk_widget_set_sensitive(GTK_WIDGET(savemsg_combo), prefs_common.savemsg);
        g_signal_connect_after(G_OBJECT(savemsg_combo), "grab_focus",
                         G_CALLBACK(compose_grab_focus_cb), compose);
        if (account_get_special_folder(compose->account, F_OUTBOX)) {
+               if (compose->account->set_sent_folder || prefs_common.savemsg)
+                       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(savemsg_checkbtn), TRUE);
+               else
+                       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(savemsg_checkbtn), FALSE);
+               gtk_widget_set_sensitive(GTK_WIDGET(savemsg_combo), TRUE);
                folderidentifier = folder_item_get_identifier(account_get_special_folder
                                  (compose->account, F_OUTBOX));
                compose_set_save_to(compose, folderidentifier);
@@ -7434,12 +7561,6 @@ static GtkWidget *compose_create_others(Compose *compose)
        return table;   
 }
 
-static void compose_savemsg_checkbtn_cb(GtkWidget *widget, Compose *compose) 
-{
-       gtk_widget_set_sensitive(GTK_WIDGET(compose->savemsg_combo),
-               gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn)));
-}
-
 static void compose_savemsg_select_cb(GtkWidget *widget, Compose *compose)
 {
        FolderItem *dest;
@@ -7634,9 +7755,9 @@ static Compose *compose_create(PrefsAccount *account,
 
        cm_return_val_if_fail(account != NULL, NULL);
 
-       gtkut_convert_int_to_gdk_color(prefs_common.default_header_bgcolor,
+       gtkut_convert_int_to_gdk_color(prefs_common.color[COL_DEFAULT_HEADER_BG],
                                           &default_header_bgcolor);
-       gtkut_convert_int_to_gdk_color(prefs_common.default_header_color,
+       gtkut_convert_int_to_gdk_color(prefs_common.color[COL_DEFAULT_HEADER],
                                           &default_header_color);
 
        debug_print("Creating compose window...\n");
@@ -8150,7 +8271,7 @@ static Compose *compose_create(PrefsAccount *account,
                        gtkaspell = gtkaspell_new(prefs_common.dictionary,
                                                  prefs_common.alt_dictionary,
                                                  conv_get_locale_charset_str(),
-                                                 prefs_common.misspelled_col,
+                                                 prefs_common.color[COL_MISSPELLED],
                                                  prefs_common.check_while_typing,
                                                  prefs_common.recheck_when_changing_dict,
                                                  prefs_common.use_alternate,
@@ -8224,8 +8345,8 @@ static Compose *compose_create(PrefsAccount *account,
 
        /* Privacy Systems menu */
        compose_update_privacy_systems_menu(compose);
+       compose_activate_privacy_system(compose, account, TRUE);
 
-       activate_privacy_system(compose, account, TRUE);
        toolbar_set_style(compose->toolbar->toolbar, compose->handlebox, prefs_common.toolbar_style);
        if (batch) {
                gtk_widget_realize(window);
@@ -8445,6 +8566,22 @@ static void compose_set_privacy_system_cb(GtkWidget *widget, gpointer data)
 
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Options/Sign", can_sign);
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Options/Encrypt", can_encrypt);
+       if (compose->toolbar->privacy_sign_btn != NULL) {
+               gtk_widget_set_sensitive(
+                       GTK_WIDGET(compose->toolbar->privacy_sign_btn),
+                       can_sign);
+               gtk_toggle_tool_button_set_active(
+                       GTK_TOGGLE_TOOL_BUTTON(compose->toolbar->privacy_sign_btn),
+                       can_sign ? compose->use_signing : FALSE);
+       }
+       if (compose->toolbar->privacy_encrypt_btn != NULL) {
+               gtk_widget_set_sensitive(
+                       GTK_WIDGET(compose->toolbar->privacy_encrypt_btn),
+                       can_encrypt);
+               gtk_toggle_tool_button_set_active(
+                       GTK_TOGGLE_TOOL_BUTTON(compose->toolbar->privacy_encrypt_btn),
+                       can_encrypt ? compose->use_encryption : FALSE);
+       }
 }
 
 static void compose_update_privacy_system_menu_item(Compose * compose, gboolean warn)
@@ -8501,8 +8638,18 @@ static void compose_update_privacy_system_menu_item(Compose * compose, gboolean
 
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Options/Sign", can_sign);
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Options/Encrypt", can_encrypt);
-}      
+       if (compose->toolbar->privacy_sign_btn != NULL) {
+               gtk_widget_set_sensitive(
+                       GTK_WIDGET(compose->toolbar->privacy_sign_btn),
+                       can_sign);
+       }
+       if (compose->toolbar->privacy_encrypt_btn != NULL) {
+               gtk_widget_set_sensitive(
+                       GTK_WIDGET(compose->toolbar->privacy_encrypt_btn),
+                       can_encrypt);
+       }
+}
+
 static void compose_set_out_encoding(Compose *compose)
 {
        CharSet out_encoding;
@@ -8773,9 +8920,11 @@ static void compose_template_apply(Compose *compose, Template *tmpl,
 
        /* process the other fields */
 
-       compose_template_apply_fields(compose, tmpl);
        compose_attach_from_list(compose, quote_fmt_get_attachments_list(), FALSE);
+       compose_template_apply_fields(compose, tmpl);
        quote_fmt_reset_vartable();
+       quote_fmtlex_destroy();
+
        compose_changed_cb(NULL, compose);
 
 #ifdef USE_ENCHANT
@@ -8792,7 +8941,7 @@ static void compose_template_apply_fields_error(const gchar *header)
        tr = g_strdup(C_("'%s' stands for a header name",
                                  "Template '%s' format error."));
        text = g_strdup_printf(tr, prefs_common_translated_header_name(header));
-       alertpanel_error(text);
+       alertpanel_error("%s", text);
 
        g_free(text);
        g_free(tr);
@@ -8829,6 +8978,9 @@ static void compose_template_apply_fields(Compose *compose, Template *tmpl)
                } else {
                        gtk_entry_set_text(GTK_ENTRY(compose->from_name), buf);
                }
+
+               quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
        }
 
        if (tmpl->to && *tmpl->to != '\0') {
@@ -8847,6 +8999,9 @@ static void compose_template_apply_fields(Compose *compose, Template *tmpl)
                } else {
                        compose_entry_append(compose, buf, COMPOSE_TO, PREF_TEMPLATE);
                }
+
+               quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
        }
 
        if (tmpl->cc && *tmpl->cc != '\0') {
@@ -8865,6 +9020,9 @@ static void compose_template_apply_fields(Compose *compose, Template *tmpl)
                } else {
                        compose_entry_append(compose, buf, COMPOSE_CC, PREF_TEMPLATE);
                }
+
+               quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
        }
 
        if (tmpl->bcc && *tmpl->bcc != '\0') {
@@ -8883,6 +9041,9 @@ static void compose_template_apply_fields(Compose *compose, Template *tmpl)
                } else {
                        compose_entry_append(compose, buf, COMPOSE_BCC, PREF_TEMPLATE);
                }
+
+               quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
        }
 
        if (tmpl->replyto && *tmpl->replyto != '\0') {
@@ -8901,6 +9062,9 @@ static void compose_template_apply_fields(Compose *compose, Template *tmpl)
                } else {
                        compose_entry_append(compose, buf, COMPOSE_REPLYTO, PREF_TEMPLATE);
                }
+
+               quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
        }
 
        /* process the subject */
@@ -8920,6 +9084,9 @@ static void compose_template_apply_fields(Compose *compose, Template *tmpl)
                } else {
                        gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf);
                }
+
+               quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
        }
 
        procmsg_msginfo_free( &dummyinfo );
@@ -8933,6 +9100,13 @@ static void compose_destroy(Compose *compose)
 
        compose_list = g_list_remove(compose_list, compose);
 
+#ifdef USE_LDAP
+       gboolean enable = TRUE;
+       g_slist_foreach(compose->passworded_ldap_servers,
+                       _ldap_srv_func, &enable);
+       g_slist_free(compose->passworded_ldap_servers);
+#endif
+
        if (compose->updating) {
                debug_print("danger, not destroying anything now\n");
                compose->deferred_destroy = TRUE;
@@ -9012,6 +9186,7 @@ static void compose_destroy(Compose *compose)
        clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
        gtk_text_buffer_remove_selection_clipboard(buffer, clipboard);
 
+       message_search_close(compose);
        gtk_widget_destroy(compose->window);
        toolbar_destroy(compose->toolbar);
        g_free(compose->toolbar);
@@ -9068,9 +9243,7 @@ static void compose_attach_remove_selected(GtkAction *action, gpointer data)
 
        selection = gtk_tree_view_get_selection(tree_view);
        sel = gtk_tree_selection_get_selected_rows(selection, &model);
-
-       if (!sel) 
-               return;
+       cm_return_if_fail(sel);
 
        for (cur = sel; cur != NULL; cur = cur->next) {
                GtkTreePath *path = cur->data;
@@ -9131,8 +9304,7 @@ static void compose_attach_property(GtkAction *action, gpointer data)
                return;
 
        sel = gtk_tree_selection_get_selected_rows(selection, &model);
-       if (!sel)
-               return;
+       cm_return_if_fail(sel);
 
        path = (GtkTreePath *) sel->data;
        gtk_tree_model_get_iter(model, &iter, path);
@@ -9282,6 +9454,7 @@ static void compose_attach_property_create(gboolean *cancelled)
        gtk_container_set_border_width(GTK_CONTAINER(window), 8);
        gtk_window_set_title(GTK_WINDOW(window), _("Properties"));
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
+       gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
        g_signal_connect(G_OBJECT(window), "delete_event",
                         G_CALLBACK(attach_property_delete_event),
                         cancelled);
@@ -9301,11 +9474,7 @@ static void compose_attach_property_create(gboolean *cancelled)
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, (0 + 1), 
                         GTK_FILL, 0, 0, 0); 
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); 
-#if !GTK_CHECK_VERSION(2, 24, 0)
-       mimetype_entry = gtk_combo_box_entry_new_text(); 
-#else
        mimetype_entry = gtk_combo_box_text_new_with_entry();
-#endif
        gtk_table_attach(GTK_TABLE(table), mimetype_entry, 1, 2, 0, (0 + 1), 
                         GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
                         
@@ -9318,20 +9487,16 @@ static void compose_attach_property_create(gboolean *cancelled)
 
                tmp = g_strdup_printf("%s/%s", type->type, type->sub_type);
 
-               if (g_list_find_custom(strlist, tmp, (GCompareFunc)strcmp2))
+               if (g_list_find_custom(strlist, tmp, (GCompareFunc)g_strcmp0))
                        g_free(tmp);
                else
                        strlist = g_list_insert_sorted(strlist, (gpointer)tmp,
-                                       (GCompareFunc)strcmp2);
+                                       (GCompareFunc)g_strcmp0);
        }
 
        for (mime_type_list = strlist; mime_type_list != NULL; 
                mime_type_list = mime_type_list->next) {
-#if !GTK_CHECK_VERSION(2, 24, 0)
-               gtk_combo_box_append_text(GTK_COMBO_BOX(mimetype_entry), mime_type_list->data);
-#else
                gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(mimetype_entry), mime_type_list->data);
-#endif
                g_free(mime_type_list->data);
        }
        g_list_free(strlist);
@@ -9622,7 +9787,8 @@ static gboolean compose_ext_editor_kill(Compose *compose)
                           "Force terminating the process?\n"
                           "process group id: %d"), -pgid);
                val = alertpanel_full(_("Notice"), msg, GTK_STOCK_NO, GTK_STOCK_YES,
-                                     NULL, FALSE, NULL, ALERT_WARNING, G_ALERTDEFAULT);
+                                     NULL, ALERTFOCUS_FIRST, FALSE, NULL,
+                                                                               ALERT_WARNING);
                        
                g_free(msg);
 
@@ -9977,14 +10143,19 @@ static void account_activated(GtkComboBox *optmenu, gpointer data)
        }
 
        /* Set message save folder */
-       if (account_get_special_folder(compose->account, F_OUTBOX)) {
-               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
-       }
-       g_signal_connect(G_OBJECT(compose->savemsg_checkbtn), "toggled",
-                        G_CALLBACK(compose_savemsg_checkbtn_cb), compose);
-                          
        compose_set_save_to(compose, NULL);
-       if (account_get_special_folder(compose->account, F_OUTBOX)) {
+       if (compose->folder && compose->folder->prefs && compose->folder->prefs->save_copy_to_folder) {
+               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
+               gtk_widget_set_sensitive(GTK_WIDGET(compose->savemsg_combo), TRUE);
+               folderidentifier = folder_item_get_identifier(compose->folder);
+               compose_set_save_to(compose, folderidentifier);
+               g_free(folderidentifier);
+       } else if (account_get_special_folder(compose->account, F_OUTBOX)) {
+               if (compose->account->set_sent_folder || prefs_common.savemsg)
+                       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
+               else
+                       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), FALSE);
+               gtk_widget_set_sensitive(GTK_WIDGET(compose->savemsg_combo), TRUE);
                folderidentifier = folder_item_get_identifier(account_get_special_folder
                                  (compose->account, F_OUTBOX));
                compose_set_save_to(compose, folderidentifier);
@@ -10100,7 +10271,7 @@ static void compose_send_cb(GtkAction *action, gpointer data)
 static void compose_send_later_cb(GtkAction *action, gpointer data)
 {
        Compose *compose = (Compose *)data;
-       gint val;
+       ComposeQueueResult val;
 
        inc_lock();
        compose_allow_user_actions(compose, FALSE);
@@ -10108,25 +10279,12 @@ static void compose_send_later_cb(GtkAction *action, gpointer data)
        compose_allow_user_actions(compose, TRUE);
        inc_unlock();
 
-       if (!val) {
+       if (val == COMPOSE_QUEUE_SUCCESS) {
                compose_close(compose);
-       } else if (val == -1) {
-               alertpanel_error(_("Could not queue message."));
-       } else if (val == -2) {
-               alertpanel_error(_("Could not queue message:\n\n%s."), g_strerror(errno));
-       } else if (val == -3) {
-               if (privacy_peek_error())
-               alertpanel_error(_("Could not queue message for sending:\n\n"
-                                  "Signature failed: %s"), privacy_get_error());
-       } else if (val == -4) {
-               alertpanel_error(_("Could not queue message for sending:\n\n"
-                                  "Charset conversion failed."));
-       } else if (val == -5) {
-               alertpanel_error(_("Could not queue message for sending:\n\n"
-                                  "Couldn't get recipient encryption key."));
-       } else if (val == -6) {
-               /* silent error */
+       } else {
+               _display_queue_error(val);
        }
+
        toolbar_main_set_sensitive(mainwindow_get_mainwindow());
 }
 
@@ -10135,12 +10293,12 @@ static void compose_register_draft(MsgInfo *info)
 {
        gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                                      DRAFTED_AT_EXIT, NULL);
-       FILE *fp = g_fopen(filepath, "ab");
+       FILE *fp = claws_fopen(filepath, "ab");
        
        if (fp) {
                fprintf(fp, "%s\t%d\n", folder_item_get_identifier(info->folder), 
                                info->msgnum);
-               fclose(fp);
+               claws_fclose(fp);
        }
                
        g_free(filepath);       
@@ -10181,8 +10339,8 @@ gboolean compose_draft (gpointer data, guint action)
 
        tmp = g_strdup_printf("%s%cdraft.%p", get_tmp_dir(),
                              G_DIR_SEPARATOR, compose);
-       if ((fp = g_fopen(tmp, "wb")) == NULL) {
-               FILE_OP_ERROR(tmp, "fopen");
+       if ((fp = claws_fopen(tmp, "wb")) == NULL) {
+               FILE_OP_ERROR(tmp, "claws_fopen");
                goto warn_err;
        }
 
@@ -10248,15 +10406,15 @@ gboolean compose_draft (gpointer data, guint action)
        err |= (fprintf(fp, "X-Claws-End-Special-Headers: 1\n") < 0);
 
        if (err) {
-               fclose(fp);
+               claws_fclose(fp);
                goto warn_err;
        }
 
        if (compose_write_to_file(compose, fp, COMPOSE_WRITE_FOR_STORE, action != COMPOSE_AUTO_SAVE) < 0) {
-               fclose(fp);
+               claws_fclose(fp);
                goto warn_err;
        }
-       if (fclose(fp) == EOF) {
+       if (claws_safe_fclose(fp) == EOF) {
                goto warn_err;
        }
        
@@ -10298,8 +10456,8 @@ warn_err:
                                val = alertpanel_full(_("Could not save draft"),
                                        _("Could not save draft.\n"
                                        "Do you want to cancel exit or discard this email?"),
-                                         _("_Cancel exit"), _("_Discard email"), NULL,
-                                         FALSE, NULL, ALERT_QUESTION, G_ALERTDEFAULT);
+                                         _("_Cancel exit"), _("_Discard email"), NULL, ALERTFOCUS_FIRST,
+                                         FALSE, NULL, ALERT_QUESTION);
                                if (val == G_ALERTALTERNATE) {
                                        lock = FALSE;
                                        g_mutex_unlock(compose->mutex); /* must be done before closing */
@@ -10346,26 +10504,55 @@ warn_err:
                compose_close(compose);
                return TRUE;
        } else {
+#ifdef G_OS_WIN32
+               GFile *f;
+               GFileInfo *fi;
+               GTimeVal tv;
+               GError *error = NULL;
+#else
                GStatBuf s;
+#endif
                gchar *path;
+               goffset size, mtime;
 
                path = folder_item_fetch_msg(draft, msgnum);
                if (path == NULL) {
                        debug_print("can't fetch %s:%d\n", draft->path, msgnum);
                        goto unlock;
                }
+#ifdef G_OS_WIN32
+               f = g_file_new_for_path(path);
+               fi = g_file_query_info(f, "standard::size,time::modified",
+                               G_FILE_QUERY_INFO_NONE, NULL, &error);
+               if (error != NULL) {
+                       debug_print("couldn't query file info for '%s': %s\n",
+                                       path, error->message);
+                       g_error_free(error);
+                       g_free(path);
+                       g_object_unref(f);
+                       goto unlock;
+               }
+               size = g_file_info_get_size(fi);
+               g_file_info_get_modification_time(fi, &tv);
+               mtime = tv.tv_sec;
+               g_object_unref(fi);
+               g_object_unref(f);
+#else
                if (g_stat(path, &s) < 0) {
                        FILE_OP_ERROR(path, "stat");
                        g_free(path);
                        goto unlock;
                }
+               size = s.st_size;
+               mtime = s.st_mtime;
+#endif
                g_free(path);
 
                procmsg_msginfo_free(&(compose->targetinfo));
                compose->targetinfo = procmsg_msginfo_new();
                compose->targetinfo->msgnum = msgnum;
-               compose->targetinfo->size = (goffset)s.st_size;
-               compose->targetinfo->mtime = s.st_mtime;
+               compose->targetinfo->size = size;
+               compose->targetinfo->mtime = mtime;
                compose->targetinfo->folder = draft;
                if (target_locked)
                        procmsg_msginfo_set_flags(compose->targetinfo, MSG_LOCKED, 0);
@@ -10397,11 +10584,11 @@ void compose_reopen_exit_drafts(void)
 {
        gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                                      DRAFTED_AT_EXIT, NULL);
-       FILE *fp = g_fopen(filepath, "rb");
+       FILE *fp = claws_fopen(filepath, "rb");
        gchar buf[1024];
        
        if (fp) {
-               while (fgets(buf, sizeof(buf), fp)) {
+               while (claws_fgets(buf, sizeof(buf), fp)) {
                        gchar **parts = g_strsplit(buf, "\t", 2);
                        const gchar *folder = parts[0];
                        int msgnum = parts[1] ? atoi(parts[1]):-1;
@@ -10414,7 +10601,7 @@ void compose_reopen_exit_drafts(void)
                        }
                        g_strfreev(parts);
                }       
-               fclose(fp);
+               claws_fclose(fp);
        }       
        g_free(filepath);
        compose_clear_exit_drafts();
@@ -10566,15 +10753,16 @@ static void compose_close_cb(GtkAction *action, gpointer data)
                        debug_print("couldn't lock mutex, probably sending\n");
                        return;
                }
-               if (!reedit) {
+               if (!reedit || compose->folder->stype == F_DRAFT) {
                        val = alertpanel(_("Discard message"),
                                 _("This message has been modified. Discard it?"),
-                                _("_Discard"), _("_Save to Drafts"), GTK_STOCK_CANCEL);
+                                _("_Discard"), _("_Save to Drafts"), GTK_STOCK_CANCEL,
+                                ALERTFOCUS_FIRST);
                } else {
                        val = alertpanel(_("Save changes"),
                                 _("This message has been modified. Save the latest changes?"),
-                                _("_Don't save"), g_strconcat("+", _("_Save to Drafts"), NULL),
-                               GTK_STOCK_CANCEL);
+                                _("_Don't save"), _("_Save to Drafts"), GTK_STOCK_CANCEL,
+                                ALERTFOCUS_SECOND);
                }
                g_mutex_unlock(compose->mutex);
                switch (val) {
@@ -10647,7 +10835,7 @@ static void compose_template_activate_cb(GtkWidget *widget, gpointer data)
        msg = g_strdup_printf(_("Do you want to apply the template '%s'?"),
                              tmpl->name);
        val = alertpanel(_("Apply template"), msg,
-                        _("_Replace"), _("_Insert"), GTK_STOCK_CANCEL);
+                        _("_Replace"), _("_Insert"), GTK_STOCK_CANCEL, ALERTFOCUS_FIRST);
        g_free(msg);
 
        if (val == G_ALERTDEFAULT)
@@ -10710,58 +10898,184 @@ static void entry_copy_clipboard(GtkWidget *entry)
                        gtk_clipboard_get(GDK_SELECTION_CLIPBOARD));
 }
 
+static void paste_text(Compose *compose, GtkWidget *entry,
+                      gboolean wrap, GdkAtom clip, GtkTextIter *insert_place,
+                      const gchar *contents)
+{
+       GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(entry));
+       GtkTextMark *mark_start = gtk_text_buffer_get_insert(buffer);
+       GtkTextIter start_iter, end_iter;
+       gint start, end;
+
+       if (contents == NULL)
+               return;
+
+       /* we shouldn't delete the selection when middle-click-pasting, or we
+        * can't mid-click-paste our own selection */
+       if (clip != GDK_SELECTION_PRIMARY) {
+               undo_paste_clipboard(GTK_TEXT_VIEW(compose->text), compose->undostruct);
+               gtk_text_buffer_delete_selection(buffer, FALSE, TRUE);
+       }
+
+       if (insert_place == NULL) {
+               /* if insert_place isn't specified, insert at the cursor.
+                * used for Ctrl-V pasting */
+               gtk_text_buffer_get_iter_at_mark(buffer, &start_iter, mark_start);
+               start = gtk_text_iter_get_offset(&start_iter);
+               gtk_text_buffer_insert(buffer, &start_iter, contents, strlen(contents));
+       } else {
+               /* if insert_place is specified, paste here.
+                * used for mid-click-pasting */
+               start = gtk_text_iter_get_offset(insert_place);
+               gtk_text_buffer_insert(buffer, insert_place, contents, strlen(contents));
+               if (prefs_common.primary_paste_unselects)
+                       gtk_text_buffer_select_range(buffer, insert_place, insert_place);
+       }
+
+       if (!wrap) {
+               /* paste unwrapped: mark the paste so it's not wrapped later */
+               end = start + strlen(contents);
+               gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, start);
+               gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, end);
+               gtk_text_buffer_apply_tag_by_name(buffer, "no_wrap", &start_iter, &end_iter);
+       } else if (wrap && clip == GDK_SELECTION_PRIMARY) {
+               /* rewrap paragraph now (after a mid-click-paste) */
+               mark_start = gtk_text_buffer_get_insert(buffer);
+               gtk_text_buffer_get_iter_at_mark(buffer, &start_iter, mark_start);
+               gtk_text_iter_backward_char(&start_iter);
+               compose_beautify_paragraph(compose, &start_iter, TRUE);
+       }
+       compose->modified = TRUE;
+}
+
+static void attach_uri_list(Compose *compose, GtkSelectionData *data)
+{
+       GList *list, *tmp;
+
+       list = uri_list_extract_filenames(
+               (const gchar *)gtk_selection_data_get_data(data));
+       for (tmp = list; tmp != NULL; tmp = tmp->next) {
+               gchar *utf8_filename = conv_filename_to_utf8((const gchar *)tmp->data);
+               compose_attach_append
+                       (compose, (const gchar *)tmp->data,
+                        utf8_filename, NULL, NULL);
+               g_free(utf8_filename);
+       }
+       if (list)
+               compose_changed_cb(NULL, compose);
+
+       list_free_strings_full(list);
+}
+
+int attach_image(Compose *compose, GtkSelectionData *data, const gchar *subtype)
+{
+       FILE *fp;
+       const guchar *contents;
+       gchar *file;
+       gchar *type;
+       size_t len;
+       int r;
+
+       cm_return_val_if_fail(data != NULL, -1);
+
+       contents = gtk_selection_data_get_data(data);
+       len = gtk_selection_data_get_length(data);
+
+       file = g_strconcat(get_tmp_file(), "-image.", subtype, NULL);
+
+       debug_print("writing image to %s\n", file);
+
+       if ((fp = claws_fopen(file, "wb")) == NULL) {
+               FILE_OP_ERROR(file, "claws_fopen");
+               return -1;
+       }
+
+       if (claws_fwrite(contents, 1, len, fp) != len) {
+               FILE_OP_ERROR(file, "claws_fwrite");
+               claws_fclose(fp);
+               claws_unlink(file);
+               return -1;
+       }
+
+       r = claws_safe_fclose(fp);
+
+       if (r == EOF) {
+               FILE_OP_ERROR(file, "claws_fclose");
+               claws_unlink(file);
+               return -1;
+       }
+
+       type = g_strconcat("image/", subtype, NULL);
+
+       compose_attach_append(compose, (const gchar *)file, 
+               (const gchar *)file, type, NULL);
+
+       alertpanel_notice(_("The pasted image has been attached as: \n%s"), file);
+
+       g_free(file);
+       g_free(type);
+
+       return 0;
+}
+
 static void entry_paste_clipboard(Compose *compose, GtkWidget *entry, 
                                  gboolean wrap, GdkAtom clip, GtkTextIter *insert_place)
 {
        if (GTK_IS_TEXT_VIEW(entry)) {
-               GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(entry));
-               GtkTextMark *mark_start = gtk_text_buffer_get_insert(buffer);
-               GtkTextIter start_iter, end_iter;
-               gint start, end;
-               gchar *contents = gtk_clipboard_wait_for_text(gtk_clipboard_get(clip));
+               GdkAtom types = gdk_atom_intern ("TARGETS", FALSE);
+               GdkAtom *targets = NULL;
+               int n_targets = 0, i;
+               gboolean paste_done = FALSE;
+               GtkClipboard *clipboard = gtk_clipboard_get(clip);
+
+               GtkSelectionData *contents = gtk_clipboard_wait_for_contents(
+                                               clipboard, types);
+
+               if (contents != NULL) {
+                       gtk_selection_data_get_targets(contents, &targets, &n_targets);
+                       gtk_selection_data_free(contents);
+               }
+
+               for (i = 0; i < n_targets; i++) {
+                       GdkAtom atom = targets[i];
+                       gchar *atom_type = gdk_atom_name(atom);
+
+                       if (atom_type != NULL) {
+                               GtkSelectionData *data = gtk_clipboard_wait_for_contents(
+                                               clipboard, atom);
+                               debug_print("got contents of type %s\n", atom_type);
+                               if (!strcmp(atom_type, "text/plain")) {
+                                       /* let the default text handler handle it */
+                    break;
+                               } else if (!strcmp(atom_type, "text/uri-list")) {
+                                       attach_uri_list(compose, data);
+
+                                       paste_done = TRUE;
+                                       break;
+                               } else if (!strncmp(atom_type, "image/", strlen("image/"))) {
+                                       gchar *subtype = g_strdup((gchar *)(strstr(atom_type, "/")+1));
+                                       debug_print("image of type %s\n", subtype);
 
-               if (contents == NULL)
-                       return;
-       
-               /* we shouldn't delete the selection when middle-click-pasting, or we
-                * can't mid-click-paste our own selection */
-               if (clip != GDK_SELECTION_PRIMARY) {
-                       undo_paste_clipboard(GTK_TEXT_VIEW(compose->text), compose->undostruct);
-                       gtk_text_buffer_delete_selection(buffer, FALSE, TRUE);
-               }
-               
-               if (insert_place == NULL) {
-                       /* if insert_place isn't specified, insert at the cursor.
-                        * used for Ctrl-V pasting */
-                       gtk_text_buffer_get_iter_at_mark(buffer, &start_iter, mark_start);
-                       start = gtk_text_iter_get_offset(&start_iter);
-                       gtk_text_buffer_insert(buffer, &start_iter, contents, strlen(contents));
-               } else {
-                       /* if insert_place is specified, paste here.
-                        * used for mid-click-pasting */
-                       start = gtk_text_iter_get_offset(insert_place);
-                       gtk_text_buffer_insert(buffer, insert_place, contents, strlen(contents));
-                       if (prefs_common.primary_paste_unselects)
-                               gtk_text_buffer_select_range(buffer, insert_place, insert_place);
+                                       attach_image(compose, data, subtype);
+                                       g_free(subtype);
+
+                                       paste_done = TRUE;
+                                       break;
+                               }
+                       }
                }
-               
-               if (!wrap) {
-                       /* paste unwrapped: mark the paste so it's not wrapped later */
-                       end = start + strlen(contents);
-                       gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, start);
-                       gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, end);
-                       gtk_text_buffer_apply_tag_by_name(buffer, "no_wrap", &start_iter, &end_iter);
-               } else if (wrap && clip == GDK_SELECTION_PRIMARY) {
-                       /* rewrap paragraph now (after a mid-click-paste) */
-                       mark_start = gtk_text_buffer_get_insert(buffer);
-                       gtk_text_buffer_get_iter_at_mark(buffer, &start_iter, mark_start);
-                       gtk_text_iter_backward_char(&start_iter);
-                       compose_beautify_paragraph(compose, &start_iter, TRUE);
+               if (!paste_done) {
+                       gchar *def_text = gtk_clipboard_wait_for_text(clipboard);
+                       paste_text(compose, entry, wrap, clip,
+                                  insert_place, def_text);
+                       g_free(def_text);
                }
-       } else if (GTK_IS_EDITABLE(entry))
-               gtk_editable_paste_clipboard (GTK_EDITABLE(entry));
+               g_free(targets);
 
-       compose->modified = TRUE;
+       } else if (GTK_IS_EDITABLE(entry)) {
+               gtk_editable_paste_clipboard (GTK_EDITABLE(entry));
+               compose->modified = TRUE;
+       }
 }
 
 static void entry_allsel(GtkWidget *entry)
@@ -10811,9 +11125,9 @@ static void compose_paste_cb(GtkAction *action, gpointer data)
        gint prev_autowrap;
        GtkTextBuffer *buffer;
        BLOCK_WRAP();
-       if (compose->focused_editable &&
+       if (compose->focused_editable
 #ifndef GENERIC_UMPC
-           gtk_widget_has_focus(compose->focused_editable)
+           && gtk_widget_has_focus(compose->focused_editable)
 #endif
                )
                entry_paste_clipboard(compose, compose->focused_editable, 
@@ -11361,6 +11675,7 @@ static void compose_toggle_sign_cb(GtkToggleAction *action, gpointer data)
        Compose *compose = (Compose *)data;
 
        compose->use_signing = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
+       gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(compose->toolbar->privacy_sign_btn), compose->use_signing);
 }
 
 static void compose_toggle_encrypt_cb(GtkToggleAction *action, gpointer data)
@@ -11368,9 +11683,10 @@ static void compose_toggle_encrypt_cb(GtkToggleAction *action, gpointer data)
        Compose *compose = (Compose *)data;
 
        compose->use_encryption = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
+       gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(compose->toolbar->privacy_encrypt_btn), compose->use_encryption);
 }
 
-static void activate_privacy_system(Compose *compose, PrefsAccount *account, gboolean warn) 
+static void compose_activate_privacy_system(Compose *compose, PrefsAccount *account, gboolean warn) 
 {
        g_free(compose->privacy_system);
        g_free(compose->encdata);
@@ -11379,6 +11695,22 @@ static void activate_privacy_system(Compose *compose, PrefsAccount *account, gbo
        compose_update_privacy_system_menu_item(compose, warn);
 }
 
+static void compose_apply_folder_privacy_settings(Compose *compose, FolderItem *folder_item)
+{
+       if (folder_item != NULL) {
+               if (folder_item->prefs->always_sign != SIGN_OR_ENCRYPT_DEFAULT &&
+                   privacy_system_can_sign(compose->privacy_system)) {
+                       compose_use_signing(compose,
+                               (folder_item->prefs->always_sign == SIGN_OR_ENCRYPT_ALWAYS) ? TRUE : FALSE);
+               }
+               if (folder_item->prefs->always_encrypt != SIGN_OR_ENCRYPT_DEFAULT &&
+                   privacy_system_can_encrypt(compose->privacy_system)) {
+                       compose_use_encryption(compose,
+                               (folder_item->prefs->always_encrypt == SIGN_OR_ENCRYPT_ALWAYS) ? TRUE : FALSE);
+               }
+       }
+}
+
 static void compose_toggle_ruler_cb(GtkToggleAction *action, gpointer data)
 {
        Compose *compose = (Compose *)data;
@@ -11403,26 +11735,14 @@ static void compose_attach_drag_received_cb (GtkWidget                *widget,
                                             gpointer            user_data)
 {
        Compose *compose = (Compose *)user_data;
-       GList *list, *tmp;
        GdkAtom type;
 
        type = gtk_selection_data_get_data_type(data);
        if ((gdk_atom_name(type) && !strcmp(gdk_atom_name(type), "text/uri-list"))
           && gtk_drag_get_source_widget(context) !=
                summary_get_main_widget(mainwindow_get_mainwindow()->summaryview)) {
-               list = uri_list_extract_filenames(
-                       (const gchar *)gtk_selection_data_get_data(data));
-               for (tmp = list; tmp != NULL; tmp = tmp->next) {
-                       gchar *utf8_filename = conv_filename_to_utf8((const gchar *)tmp->data);
-                       compose_attach_append
-                               (compose, (const gchar *)tmp->data,
-                                utf8_filename, NULL, NULL);
-                       g_free(utf8_filename);
-               }
-               if (list) compose_changed_cb(NULL, compose);
-               list_free_strings(list);
-               g_list_free(list);
-       } else if (gtk_drag_get_source_widget(context) 
+               attach_uri_list(compose, data);
+       } else if (gtk_drag_get_source_widget(context)
                   == summary_get_main_widget(mainwindow_get_mainwindow()->summaryview)) {
                /* comes from our summaryview */
                SummaryView * summaryview = NULL;
@@ -11503,7 +11823,7 @@ static void compose_insert_drag_received_cb (GtkWidget          *widget,
                        /* Assume a list of no files, and data has ://, is a remote link */
                        gchar *tmpdata = g_strstrip(g_strdup(ddata));
                        gchar *tmpfile = get_tmp_file();
-                       str_write_to_file(tmpdata, tmpfile);
+                       str_write_to_file(tmpdata, tmpfile, TRUE);
                        g_free(tmpdata);  
                        compose_insert_file(compose, tmpfile);
                        claws_unlink(tmpfile);
@@ -11523,8 +11843,9 @@ static void compose_insert_drag_received_cb (GtkWidget          *widget,
                                                        num_files),
                                                num_files);
                                val = alertpanel_full(_("Insert or attach?"), msg,
-                                         GTK_STOCK_CANCEL, g_strconcat("+", _("_Insert"), NULL), _("_Attach"),
-                                         TRUE, NULL, ALERT_QUESTION, G_ALERTALTERNATE);
+                                         GTK_STOCK_CANCEL, _("_Insert"), _("_Attach"),
+                                               ALERTFOCUS_SECOND,
+                                         TRUE, NULL, ALERT_QUESTION);
                                g_free(msg);
                                break;
                        case COMPOSE_DND_INSERT:
@@ -11549,21 +11870,18 @@ static void compose_insert_drag_received_cb (GtkWidget                *widget,
 
                if (val == G_ALERTDEFAULT || val == G_ALERTCANCEL) {
                        gtk_drag_finish(drag_context, FALSE, FALSE, time);
-                       list_free_strings(list);
-                       g_list_free(list);
+                       list_free_strings_full(list);
                        return;
                } else if (val == G_ALERTOTHER) {
                        compose_attach_drag_received_cb(widget, drag_context, x, y, data, info, time, user_data);
-                       list_free_strings(list);
-                       g_list_free(list);
+                       list_free_strings_full(list);
                        return;
                } 
 
                for (tmp = list; tmp != NULL; tmp = tmp->next) {
                        compose_insert_file(compose, (const gchar *)tmp->data);
                }
-               list_free_strings(list);
-               g_list_free(list);
+               list_free_strings_full(list);
                gtk_drag_finish(drag_context, TRUE, FALSE, time);
                return;
        }
@@ -11622,6 +11940,8 @@ static gboolean compose_headerentry_button_clicked_cb (GtkWidget *button,
                                         ComposeHeaderEntry *headerentry)
 {
        gtk_entry_set_text(GTK_ENTRY(headerentry->entry), "");
+       gtk_widget_modify_base(GTK_WIDGET(headerentry->entry), GTK_STATE_NORMAL, NULL);
+       gtk_widget_modify_text(GTK_WIDGET(headerentry->entry), GTK_STATE_NORMAL, NULL);
        return FALSE;
 }
 
@@ -11981,11 +12301,9 @@ static void compose_reply_from_messageview_real(MessageView *msgview, GSList *ms
        Compose *compose = NULL;
        gchar *s_system = NULL;
 
-       cm_return_if_fail(msgview != NULL);
-
        cm_return_if_fail(msginfo_list != NULL);
 
-       if (g_slist_length(msginfo_list) == 1 && !opening_multiple) {
+       if (g_slist_length(msginfo_list) == 1 && !opening_multiple && msgview != NULL) {
                MimeInfo *mimeinfo = messageview_get_selected_mime_part(msgview);
                MsgInfo *orig_msginfo = (MsgInfo *)msginfo_list->data;
 
@@ -12010,7 +12328,7 @@ static void compose_reply_from_messageview_real(MessageView *msgview, GSList *ms
                }
        }
 
-       if (!opening_multiple)
+       if (!opening_multiple && msgview != NULL)
                body = messageview_get_selection(msgview);
 
        if (new_msglist) {
@@ -12036,6 +12354,7 @@ void compose_reply_from_messageview(MessageView *msgview, GSList *msginfo_list,
                                    guint action)
 {
        if ((!prefs_common.forward_as_attachment || action != COMPOSE_FORWARD) 
+       &&  msginfo_list != NULL
        &&  action != COMPOSE_FORWARD_AS_ATTACH && g_slist_length(msginfo_list) > 1) {
                GSList *cur = msginfo_list;
                gchar *msg = g_strdup_printf(_("You are about to reply to %d "
@@ -12044,8 +12363,8 @@ void compose_reply_from_messageview(MessageView *msgview, GSList *msginfo_list,
                                               "want to continue?"), 
                                               g_slist_length(msginfo_list));
                if (g_slist_length(msginfo_list) > 9
-               &&  alertpanel(_("Warning"), msg, GTK_STOCK_CANCEL, "+" GTK_STOCK_YES, NULL)
-                   != G_ALERTALTERNATE) {
+               &&  alertpanel(_("Warning"), msg, GTK_STOCK_CANCEL, GTK_STOCK_YES, NULL,
+                       ALERTFOCUS_SECOND) != G_ALERTALTERNATE) {
                        g_free(msg);
                        return;
                }
@@ -12063,7 +12382,16 @@ void compose_reply_from_messageview(MessageView *msgview, GSList *msginfo_list,
        } else {
                /* forwarding multiple mails as attachments is done via a
                 * single compose window */
-               compose_reply_from_messageview_real(msgview, msginfo_list, action, FALSE);
+               if (msginfo_list != NULL) {
+                       compose_reply_from_messageview_real(msgview, msginfo_list, action, FALSE);
+               } else if (msgview != NULL) {
+                       GSList tmplist;
+                       tmplist.data = msgview->msginfo;
+                       tmplist.next = NULL;
+                       compose_reply_from_messageview_real(msgview, &tmplist, action, FALSE);
+               } else {
+                       debug_print("Nothing to reply to\n");
+               }
        }
 }