Use GLib's implementation of Base64 instead of our own.
[claws.git] / src / compose.c
index a5d57acc7dc94afed3ae250ea75b60a8b5333b0f..0e3b9b62aee500a0970274ca98107a7e7a6d4bfc 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2013 Hiroyuki Yamamoto and the Claws Mail team
  *
  * 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
@@ -84,7 +84,6 @@
 #include "procmime.h"
 #include "statusbar.h"
 #include "about.h"
-#include "base64.h"
 #include "quoted-printable.h"
 #include "codeconv.h"
 #include "utils.h"
@@ -182,7 +181,11 @@ typedef enum {
 
 #define MAX_REFERENCES_LEN     999
 
+#define COMPOSE_DRAFT_TIMEOUT_UNSET -1
+#define COMPOSE_DRAFT_TIMEOUT_FORBIDDEN -2
+
 static GList *compose_list = NULL;
+static GSList *extra_headers = NULL;
 
 static Compose *compose_generic_new                    (PrefsAccount   *account,
                                                 const gchar    *to,
@@ -364,7 +367,7 @@ static void compose_add_field_list  ( Compose *compose,
 
 static void compose_notebook_size_alloc (GtkNotebook *notebook,
                                         GtkAllocation *allocation,
-                                        Compose *compose);
+                                        GtkPaned *paned);
 static gboolean compose_edit_size_alloc (GtkEditable   *widget,
                                         GtkAllocation  *allocation,
                                         GtkSHRuler     *shruler);
@@ -392,6 +395,8 @@ static void compose_insert_file_cb  (GtkAction      *action,
                                         gpointer        data);
 static void compose_insert_sig_cb      (GtkAction      *action,
                                         gpointer        data);
+static void compose_replace_sig_cb     (GtkAction      *action,
+                                        gpointer        data);
 
 static void compose_close_cb           (GtkAction      *action,
                                         gpointer        data);
@@ -556,6 +561,7 @@ static void compose_set_dictionaries_from_folder_prefs(Compose *compose,
 static void compose_attach_update_label(Compose *compose);
 static void compose_set_folder_prefs(Compose *compose, FolderItem *folder,
                                     gboolean respect_default_to);
+static void compose_subject_entry_activated(GtkWidget *widget, gpointer data);
 
 static GtkActionEntry compose_popup_entries[] =
 {
@@ -586,6 +592,7 @@ static GtkActionEntry compose_entries[] =
        {"Message/AttachFile",          NULL, N_("_Attach file"), "<control>M", NULL, G_CALLBACK(compose_attach_cb) },
        {"Message/InsertFile",          NULL, N_("_Insert file"), "<control>I", NULL, G_CALLBACK(compose_insert_file_cb) },
        {"Message/InsertSig",           NULL, N_("Insert si_gnature"), "<control>G", NULL, G_CALLBACK(compose_insert_sig_cb) },
+       {"Message/ReplaceSig",          NULL, N_("_Replace signature"), NULL, NULL, G_CALLBACK(compose_replace_sig_cb) },
        /* {"Message/---",              NULL, "---" }, */
        {"Message/Save",                NULL, N_("_Save"), "<control>S", NULL, G_CALLBACK(compose_save_cb) }, /*COMPOSE_KEEP_EDITING*/
        /* {"Message/---",              NULL, "---" }, */
@@ -755,8 +762,8 @@ static GtkTargetEntry compose_mime_types[] =
 
 static gboolean compose_put_existing_to_front(MsgInfo *info)
 {
-       GList *compose_list = compose_get_compose_list();
-       GList *elem = NULL;
+       const GList *compose_list = compose_get_compose_list();
+       const GList *elem = NULL;
        
        if (compose_list) {
                for (elem = compose_list; elem != NULL && elem->data != NULL; 
@@ -812,8 +819,8 @@ static void compose_create_tags(GtkTextView *text, Compose *compose)
        GdkColormap *cmap;
        gboolean success[8];
        int i;
-#endif
        GdkColor color[8];
+#endif
 
        buffer = gtk_text_view_get_buffer(text);
 
@@ -875,6 +882,7 @@ static void compose_create_tags(GtkTextView *text, Compose *compose)
        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;
@@ -883,16 +891,13 @@ static void compose_create_tags(GtkTextView *text, Compose *compose)
        color[5] = quote_bgcolor3;
        color[6] = signature_color;
        color[7] = uri_color;
-#if !GTK_CHECK_VERSION(2, 24, 0)
+
        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) {
-                       GtkStyle *style;
-
                        g_warning("Compose: color allocation failed.\n");
-                       style = gtk_widget_get_style(GTK_WIDGET(text));
                        quote_color1 = quote_color2 = quote_color3 = 
                                quote_bgcolor1 = quote_bgcolor2 = quote_bgcolor3 = 
                                signature_color = uri_color = black;
@@ -930,11 +935,20 @@ 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)));
@@ -952,11 +966,20 @@ 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;
 }
@@ -984,8 +1007,15 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
                /* mailto defines a from, check if we can get account prefs from it,
                   if not, the account prefs will be guessed using other ways, but we'll keep
                   the from anyway */
-               if (mailto_from)
+               if (mailto_from) {
                        mailto_account = account_find_from_address(mailto_from, TRUE);
+                       if (mailto_account == NULL) {
+                               gchar *tmp_from;
+                               Xstrdup_a(tmp_from, mailto_from, return NULL);
+                               extract_address(tmp_from);
+                               mailto_account = account_find_from_address(tmp_from, TRUE);
+                       }
+               }
                if (mailto_account)
                        account = mailto_account;
        }
@@ -1175,7 +1205,7 @@ 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->name,
+                       compose_attach_append(compose, ainfo->file, ainfo->file,
                                        ainfo->content_type, ainfo->charset);
                }
        }
@@ -1238,7 +1268,8 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
        if (prefs_common.auto_exteditor)
                compose_exec_ext_editor(compose);
 
-       compose->draft_timeout_tag = -1;
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET;
+
        SCROLL_TO_CURSOR(compose);
 
        compose->modified = FALSE;
@@ -1260,12 +1291,11 @@ static void compose_force_encryption(Compose *compose, PrefsAccount *account,
        if (override_pref == FALSE && account->default_encrypt_reply == FALSE)
                return;
 
-       if (system)
-               privacy = system;
-       else if (account->default_privacy_system
-       &&  strlen(account->default_privacy_system)) {
+       if (account->default_privacy_system && strlen(account->default_privacy_system))
                privacy = account->default_privacy_system;
-       } else {
+       else if (system)
+               privacy = system;
+       else {
                GSList *privacy_avail = privacy_get_system_ids();
                if (privacy_avail && g_slist_length(privacy_avail)) {
                        privacy = (gchar *)(privacy_avail->data);
@@ -1291,12 +1321,11 @@ static void compose_force_signing(Compose *compose, PrefsAccount *account, const
 {
        const gchar *privacy = NULL;
 
-       if (system)
-               privacy = system;
-       else if (account->default_privacy_system
-       &&  strlen(account->default_privacy_system)) {
+       if (account->default_privacy_system && strlen(account->default_privacy_system))
                privacy = account->default_privacy_system;
-       } else {
+       else if (system)
+               privacy = system;
+       else {
                GSList *privacy_avail = privacy_get_system_ids();
                if (privacy_avail && g_slist_length(privacy_avail)) {
                        privacy = (gchar *)(privacy_avail->data);
@@ -1681,7 +1710,7 @@ static Compose *compose_generic_reply(MsgInfo *msginfo,
        compose_set_title(compose);
 
        compose->updating = FALSE;
-       compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET; /* desinhibit auto-drafting after loading */
        SCROLL_TO_CURSOR(compose);
        
        if (compose->deferred_destroy) {
@@ -1884,7 +1913,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
        compose_set_title(compose);
 
        compose->updating = FALSE;
-       compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET; /* desinhibit auto-drafting after loading */
        SCROLL_TO_CURSOR(compose);
 
        if (compose->deferred_destroy) {
@@ -2032,7 +2061,7 @@ static Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_
        compose_set_title(compose);
 
        compose->updating = FALSE;
-       compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET; /* desinhibit auto-drafting after loading */
        SCROLL_TO_CURSOR(compose);
 
        if (compose->deferred_destroy) {
@@ -2079,6 +2108,44 @@ static gboolean compose_is_sig_separator(Compose *compose, GtkTextBuffer *textbu
        return FALSE;
 }
 
+static gboolean compose_update_folder_hook(gpointer source, gpointer data)
+{
+       FolderUpdateData *hookdata = (FolderUpdateData *)source;
+       Compose *compose = (Compose *)data;
+       FolderItem *old_item = NULL;
+       FolderItem *new_item = NULL;
+       gchar *old_id, *new_id;
+
+       if (!(hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
+        && !(hookdata->update_flags & FOLDER_MOVE_FOLDERITEM))
+               return FALSE;
+
+       old_item = hookdata->item;
+       new_item = hookdata->item2;
+
+       old_id = folder_item_get_identifier(old_item);
+       new_id = new_item ? folder_item_get_identifier(new_item) : g_strdup("NULL");
+
+       if (compose->targetinfo && compose->targetinfo->folder == old_item) {
+               debug_print("updating targetinfo folder: %s -> %s\n", old_id, new_id);
+               compose->targetinfo->folder = new_item;
+       }
+
+       if (compose->replyinfo && compose->replyinfo->folder == old_item) {
+               debug_print("updating replyinfo folder: %s -> %s\n", old_id, new_id);
+               compose->replyinfo->folder = new_item;
+       }
+
+       if (compose->fwdinfo && compose->fwdinfo->folder == old_item) {
+               debug_print("updating fwdinfo folder: %s -> %s\n", old_id, new_id);
+               compose->fwdinfo->folder = new_item;
+       }
+
+       g_free(old_id);
+       g_free(new_id);
+       return FALSE;
+}
+
 static void compose_colorize_signature(Compose *compose)
 {
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(compose->text));
@@ -2105,21 +2172,21 @@ static void compose_colorize_signature(Compose *compose)
                                G_CALLBACK(text_inserted),              \
                                compose);                               \
 }
-#define UNBLOCK_WRAP() {                                               \
-       compose->autowrap = prev_autowrap;                              \
-       if (compose->autowrap) {                                        \
-               gint old = compose->draft_timeout_tag;                  \
-               compose->draft_timeout_tag = -2;                        \
-               compose_wrap_all(compose);                              \
-               compose->draft_timeout_tag = old;                       \
-       }                                                               \
-                                                                       \
-       g_signal_handlers_unblock_by_func(G_OBJECT(buffer),             \
-                               G_CALLBACK(compose_changed_cb),         \
-                               compose);                               \
-       g_signal_handlers_unblock_by_func(G_OBJECT(buffer),             \
-                               G_CALLBACK(text_inserted),              \
-                               compose);                               \
+#define UNBLOCK_WRAP() {                                                       \
+       compose->autowrap = prev_autowrap;                                      \
+       if (compose->autowrap) {                                                \
+               gint old = compose->draft_timeout_tag;                          \
+               compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_FORBIDDEN;   \
+               compose_wrap_all(compose);                                      \
+               compose->draft_timeout_tag = old;                               \
+       }                                                                       \
+                                                                               \
+       g_signal_handlers_unblock_by_func(G_OBJECT(buffer),                     \
+                               G_CALLBACK(compose_changed_cb),                 \
+                               compose);                                       \
+       g_signal_handlers_unblock_by_func(G_OBJECT(buffer),                     \
+                               G_CALLBACK(text_inserted),                      \
+                               compose);                                       \
 }
 
 Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
@@ -2344,8 +2411,8 @@ Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
        }
 
        if (fp != NULL) {
-               gboolean prev_autowrap = compose->autowrap;
-               GtkTextBuffer *buffer = textbuf;
+               gboolean prev_autowrap;
+               GtkTextBuffer *buffer;
                BLOCK_WRAP();
                while (fgets(buf, sizeof(buf), fp) != NULL) {
                        strcrchomp(buf);
@@ -2382,7 +2449,7 @@ Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
        compose_set_title(compose);
 
        compose->updating = FALSE;
-       compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET; /* desinhibit auto-drafting after loading */
        SCROLL_TO_CURSOR(compose);
 
        if (compose->deferred_destroy) {
@@ -2468,6 +2535,7 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo,
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Message/InsertFile", FALSE);
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Message/AttachFile", FALSE);
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Message/InsertSig", FALSE);
+       cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Message/ReplaceSig", FALSE);
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Edit", FALSE);
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Options", FALSE);
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Tools/ShowRuler", FALSE);
@@ -2491,7 +2559,7 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo,
        compose->modified = FALSE;
        compose_set_title(compose);
        compose->updating = FALSE;
-       compose->draft_timeout_tag = -1; /* desinhibit auto-drafting after loading */
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET; /* desinhibit auto-drafting after loading */
        SCROLL_TO_CURSOR(compose);
 
        if (compose->deferred_destroy) {
@@ -2504,7 +2572,7 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo,
        return compose;
 }
 
-GList *compose_get_compose_list(void)
+const GList *compose_get_compose_list(void)
 {
        return compose_list;
 }
@@ -2568,8 +2636,6 @@ void compose_entry_append(Compose *compose, const gchar *address,
                gchar *tmp = g_strdup(begin);
                gchar *o_tmp = tmp;
                tmp[cur-begin]='\0';
-               cur++;
-               begin = cur;
                while (*tmp == ' ' || *tmp == '\t')
                        tmp++;
                compose_add_header_entry(compose, header, tmp, pref_type);
@@ -2647,6 +2713,9 @@ void compose_toolbar_cb(gint action, gpointer data)
        case A_SIG:
                compose_insert_sig(compose, FALSE);
                break;
+       case A_REP_SIG:
+               compose_insert_sig(compose, TRUE);
+               break;
        case A_EXTEDITOR:
                compose_ext_editor_cb(NULL, compose);
                break;
@@ -2966,8 +3035,7 @@ static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
                                ref_id_list = g_slist_remove
                                        (ref_id_list, ref_id_list->next->data);
                        } else {
-                               slist_free_strings(ref_id_list);
-                               g_slist_free(ref_id_list);
+                               slist_free_strings_full(ref_id_list);
                                return NULL;
                        }
                } else
@@ -2981,8 +3049,7 @@ static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
                g_string_append_printf(new_ref, "<%s>", (gchar *)cur->data);
        }
 
-       slist_free_strings(ref_id_list);
-       g_slist_free(ref_id_list);
+       slist_free_strings_full(ref_id_list);
 
        new_ref_str = new_ref->str;
        g_string_free(new_ref, FALSE);
@@ -3139,7 +3206,7 @@ static gboolean is_subscription(const gchar *ml_post, const gchar *from)
                *(strstr(left_from, "@")) = '\0';
        }
        
-       if (left_ml && left_from && right_ml && right_from
+       if (right_ml && right_from
        &&  !strncmp(left_from, left_ml, strlen(left_ml))
        &&  !strcmp(right_from, right_ml)) {
                result = TRUE;
@@ -3256,16 +3323,18 @@ static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
                        gchar *tmp1 = NULL;
                        if (!msginfo->from)
                                return;
-                       Xstrdup_a(tmp1, msginfo->from, return);
-                       extract_address(tmp1);
-                       if (to_all || to_sender ||
-                           !account_find_from_address(tmp1, FALSE))
+                       if (to_sender)
+                               compose_entry_append(compose, msginfo->from,
+                                                    COMPOSE_TO, PREF_NONE);
+                       else if (to_all) {
+                               Xstrdup_a(tmp1, msginfo->from, return);
+                               extract_address(tmp1);
                                compose_entry_append(compose,
-                                (compose->replyto && !to_sender)
-                                         ? compose->replyto :
-                                         msginfo->from ? msginfo->from : "",
+                                (!account_find_from_address(tmp1, FALSE))
+                                         ? msginfo->from :
+                                         msginfo->to,
                                          COMPOSE_TO, PREF_NONE);
-                       else if (!to_all && !to_sender) {
+                       } else {
                                if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
                                    !folder_has_parent_of_type(msginfo->folder, F_OUTBOX) &&
                                    !folder_has_parent_of_type(msginfo->folder, F_DRAFT)) {
@@ -3357,8 +3426,7 @@ static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
                        g_free(addr);
                }
                
-               slist_free_strings(cc_list);
-               g_slist_free(cc_list);
+               slist_free_strings_full(cc_list);
        }
        
        g_free(ac_email);
@@ -3506,9 +3574,9 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
        gint len;
        FILE *fp;
        gboolean prev_autowrap;
-       gboolean badtxt = FALSE;
        struct stat file_stat;
        int ret;
+       GString *file_contents = NULL;
 
        cm_return_val_if_fail(file != NULL, COMPOSE_INSERT_NO_FILE);
 
@@ -3568,6 +3636,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) {
                gchar *str;
 
@@ -3587,10 +3656,14 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
                                if (str[len] == '\r') str[len] = '\n';
                }
 
-               gtk_text_buffer_insert(buffer, &iter, str, -1);
+               file_contents = g_string_append(file_contents, str);
                g_free(str);
        }
 
+       gtk_text_buffer_insert(buffer, &iter, file_contents->str, -1);
+       g_string_free(file_contents, TRUE);
+
+       compose_changed_cb(NULL, compose);
        g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
                                          G_CALLBACK(text_inserted),
                                          compose);
@@ -3600,10 +3673,7 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
 
        fclose(fp);
 
-       if (badtxt)
-               return COMPOSE_INSERT_INVALID_CHARACTER;
-       else 
-               return COMPOSE_INSERT_SUCCESS;
+       return COMPOSE_INSERT_SUCCESS;
 }
 
 static gboolean compose_attach_append(Compose *compose, const gchar *file,
@@ -3640,9 +3710,18 @@ static gboolean compose_attach_append(Compose *compose, const gchar *file,
                alertpanel_error("Can't get file size of %s\n", filename);
                return FALSE;
        }
-       if (size == 0) {
-               alertpanel_error(_("File %s is empty."), filename);
-               return FALSE;
+
+       /* In batch mode, we allow 0-length files to be attached no questions asked */
+       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, _("+_Attach anyway"), NULL, FALSE,
+                               NULL, ALERT_WARNING, G_ALERTDEFAULT);
+               g_free(msg);
+
+               if (aval != G_ALERTALTERNATE) {
+                       return FALSE;
+               }
        }
        if ((fp = g_fopen(file, "rb")) == NULL) {
                alertpanel_error(_("Can't read %s."), filename);
@@ -4251,7 +4330,7 @@ static gboolean compose_beautify_paragraph(Compose *compose, GtkTextIter *par_it
        GtkTextIter iter, break_pos, end_of_line;
        gchar *quote_str = NULL;
        gint quote_len;
-       gboolean wrap_quote = prefs_common.linewrap_quote;
+       gboolean wrap_quote = force || prefs_common.linewrap_quote;
        gboolean prev_autowrap = compose->autowrap;
        gint startq_offset = -1, noq_offset = -1;
        gint uri_start = -1, uri_stop = -1;
@@ -4270,7 +4349,7 @@ static gboolean compose_beautify_paragraph(Compose *compose, GtkTextIter *par_it
        if (force) {
                modified = TRUE;
        }
-       if (compose->draft_timeout_tag == -2) {
+       if (compose->draft_timeout_tag == COMPOSE_DRAFT_TIMEOUT_FORBIDDEN) {
                modified = TRUE;
        }
 
@@ -4287,7 +4366,7 @@ static gboolean compose_beautify_paragraph(Compose *compose, GtkTextIter *par_it
        }
 
 
-       if (compose->draft_timeout_tag == -2) {
+       if (compose->draft_timeout_tag == COMPOSE_DRAFT_TIMEOUT_FORBIDDEN) {
                if (gtk_text_iter_ends_line(&iter)) {
                        while (gtk_text_iter_ends_line(&iter) &&
                               gtk_text_iter_forward_line(&iter))
@@ -4649,9 +4728,14 @@ static void compose_wrap_all_full(Compose *compose, gboolean force)
        buffer = gtk_text_view_get_buffer(text);
 
        gtk_text_buffer_get_start_iter(buffer, &iter);
+
+       undo_wrapping(compose->undostruct, TRUE);
+
        while (!gtk_text_iter_is_end(&iter) && modified)
                modified = compose_beautify_paragraph(compose, &iter, force);
 
+       undo_wrapping(compose->undostruct, FALSE);
+
 }
 
 static void compose_set_title(Compose *compose)
@@ -4709,7 +4793,7 @@ compose_current_mail_account(void)
 
 #define QUOTE_IF_REQUIRED(out, str)                                    \
 {                                                                      \
-       if (*str != '"' && strpbrk(str, ",.[]<>")) {                    \
+       if (*str != '"' && strpbrk(str, ",.:;[]<>()@\\\"")) {           \
                gchar *__tmp;                                           \
                gint len;                                               \
                                                                        \
@@ -4737,7 +4821,7 @@ compose_current_mail_account(void)
 
 #define QUOTE_IF_REQUIRED_NORMAL(out, str, errret)                     \
 {                                                                      \
-       if (*str != '"' && strpbrk(str, ",.[]<>")) {                    \
+       if (*str != '"' && strpbrk(str, ",.:;[]<>()@\\\"")) {           \
                gchar *__tmp;                                           \
                gint len;                                               \
                                                                        \
@@ -4764,17 +4848,23 @@ compose_current_mail_account(void)
 static void compose_select_account(Compose *compose, PrefsAccount *account,
                                   gboolean init)
 {
-       gchar *from = NULL, *header;
+       gchar *from = NULL, *header = NULL;
        ComposeHeaderEntry *header_entry;
+#if GTK_CHECK_VERSION(2, 24, 0)
+       GtkTreeIter iter;
+#endif
 
        cm_return_if_fail(account != NULL);
 
        compose->account = account;
        if (account->name && *account->name) {
-               gchar *buf;
+               gchar *buf, *qbuf;
                QUOTE_IF_REQUIRED_NORMAL(buf, account->name, return);
+               qbuf = escape_internal_quotes(buf, '"');
                from = g_strdup_printf("%s <%s>",
-                                      buf, account->address);
+                                      qbuf, account->address);
+               if (qbuf != buf)
+                       g_free(qbuf);
                gtk_entry_set_text(GTK_ENTRY(compose->from_name), from);
        } else {
                from = g_strdup_printf("<%s>",
@@ -4804,7 +4894,13 @@ 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) {
@@ -4846,12 +4942,10 @@ gboolean compose_check_for_valid_recipient(Compose *compose) {
        gchar **strptr;
 
        /* free to and newsgroup list */
-        slist_free_strings(compose->to_list);
-       g_slist_free(compose->to_list);
+        slist_free_strings_full(compose->to_list);
        compose->to_list = NULL;
                        
-       slist_free_strings(compose->newsgroup_list);
-        g_slist_free(compose->newsgroup_list);
+       slist_free_strings_full(compose->newsgroup_list);
         compose->newsgroup_list = NULL;
 
        /* search header entries for to and newsgroup entries */
@@ -4967,9 +5061,9 @@ static gboolean compose_check_entries(Compose *compose, gboolean check_everythin
                return FALSE;
        }
 
-       if (!compose->batch) {
+       if (!compose->batch && prefs_common.warn_empty_subj == TRUE) {
                str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
-               if (*str == '\0' && check_everything == TRUE && 
+               if (*str == '\0' && check_everything == TRUE &&
                    compose->mode != COMPOSE_REDIRECT) {
                        AlertValue aval;
                        gchar *button_label;
@@ -4983,9 +5077,14 @@ static gboolean compose_check_entries(Compose *compose, gboolean check_everythin
                                        compose->sending?_("Send it anyway?"):
                                        _("Queue it anyway?"));
 
-                       aval = alertpanel(compose->sending?_("Send"):_("Send later"), message,
-                                         GTK_STOCK_CANCEL, button_label, NULL);
+                       aval = alertpanel_full(compose->sending?_("Send"):_("Send later"), message,
+                                              GTK_STOCK_CANCEL, button_label, NULL, TRUE, NULL,
+                                              ALERT_QUESTION, G_ALERTDEFAULT);
                        g_free(message);
+                       if (aval & G_ALERTDISABLE) {
+                               aval &= ~G_ALERTDISABLE;
+                               prefs_common.warn_empty_subj = FALSE;
+                       }
                        if (aval != G_ALERTALTERNATE)
                                return FALSE;
                }
@@ -5291,6 +5390,8 @@ static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
                }
                generate_msgid(buf, sizeof(buf), addr);
                err |= (fprintf(fp, "Resent-Message-ID: <%s>\n", buf) < 0);
+               if (compose->msgid)
+                       g_free(compose->msgid);
                compose->msgid = g_strdup(buf);
        } else {
                compose->msgid = NULL;
@@ -5651,8 +5752,11 @@ static gint compose_write_body_to_file(Compose *compose, const gchar *file)
                (tmp, CS_INTERNAL, conv_get_locale_charset_str());
 
        g_free(tmp);
-       if (!chars) return -1;
-
+       if (!chars) {
+               fclose(fp);
+               claws_unlink(file);
+               return -1;
+       }
        /* write body */
        len = strlen(chars);
        if (fwrite(chars, sizeof(gchar), len, fp) != len) {
@@ -5754,17 +5858,13 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
        FILE *fp;
        GSList *cur;
        gint num;
-        static gboolean lock = FALSE;
        PrefsAccount *mailac = NULL, *newsac = NULL;
        gboolean err = FALSE;
 
        debug_print("queueing message...\n");
        cm_return_val_if_fail(compose->account != NULL, -1);
 
-        lock = TRUE;
-       
        if (compose_check_entries(compose, check_subject) == FALSE) {
-                lock = FALSE;
                if (compose->batch) {
                        gtk_widget_show_all(compose->window);
                }
@@ -5773,7 +5873,6 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
 
        if (!compose->to_list && !compose->newsgroup_list) {
                g_warning("can't get recipient list.");
-               lock = FALSE;
                 return -1;
         }
 
@@ -5783,7 +5882,6 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                else if (cur_account && cur_account->protocol != A_NNTP)
                        mailac = cur_account;
                else if (!(mailac = compose_current_mail_account())) {
-                       lock = FALSE;
                        alertpanel_error(_("No account for sending mails available!"));
                        return -1;
                }
@@ -5793,7 +5891,6 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                 if (compose->account->protocol == A_NNTP)
                         newsac = compose->account;
                 else {
-                       lock = FALSE;
                        alertpanel_error(_("Selected account isn't NNTP: Posting is impossible."));
                        return -1;
                }                       
@@ -5806,7 +5903,6 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
        if ((fp = g_fopen(tmp, "wb")) == NULL) {
                FILE_OP_ERROR(tmp, "fopen");
                g_free(tmp);
-               lock = FALSE;
                return -2;
        }
 
@@ -5868,7 +5964,6 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                if (compose->use_encryption) {
                        gchar *encdata;
                        if (!compose_warn_encryption(compose)) {
-                               lock = FALSE;
                                fclose(fp);
                                claws_unlink(tmp);
                                g_free(tmp);
@@ -5894,7 +5989,6 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                                 * key selection */
                                if (err == TRUE)
                                        g_warning("failed to write queue message");
-                               lock = FALSE;
                                fclose(fp);
                                claws_unlink(tmp);
                                g_free(tmp);
@@ -5918,17 +6012,25 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
        }
        /* Message-ID of message replying to */
        if ((compose->replyinfo != NULL) && (compose->replyinfo->msgid != NULL)) {
-               gchar *folderid;
-               
-               folderid = folder_item_get_identifier(compose->replyinfo->folder);
+               gchar *folderid = NULL;
+
+               if (compose->replyinfo->folder)
+                       folderid = folder_item_get_identifier(compose->replyinfo->folder);
+               if (folderid == NULL)
+                       folderid = g_strdup("NULL");
+
                err |= (fprintf(fp, "RMID:%s\t%d\t%s\n", folderid, compose->replyinfo->msgnum, compose->replyinfo->msgid) < 0);
                g_free(folderid);
        }
        /* Message-ID of message forwarding to */
        if ((compose->fwdinfo != NULL) && (compose->fwdinfo->msgid != NULL)) {
-               gchar *folderid;
+               gchar *folderid = NULL;
                
-               folderid = folder_item_get_identifier(compose->fwdinfo->folder);
+               if (compose->fwdinfo->folder)
+                       folderid = folder_item_get_identifier(compose->fwdinfo->folder);
+               if (folderid == NULL)
+                       folderid = g_strdup("NULL");
+
                err |= (fprintf(fp, "FMID:%s\t%d\t%s\n", folderid, compose->fwdinfo->msgnum, compose->fwdinfo->msgid) < 0);
                g_free(folderid);
        }
@@ -5941,7 +6043,6 @@ 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) {
-                       lock = FALSE;
                        fclose(fp);
                        claws_unlink(tmp);
                        g_free(tmp);
@@ -5950,7 +6051,6 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
        } else {
                gint result = 0;
                if ((result = compose_write_to_file(compose, fp, COMPOSE_WRITE_FOR_SEND, TRUE)) < 0) {
-                       lock = FALSE;
                        fclose(fp);
                        claws_unlink(tmp);
                        g_free(tmp);
@@ -5962,14 +6062,12 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                fclose(fp);
                claws_unlink(tmp);
                g_free(tmp);
-               lock = FALSE;
                return -2;
        }
        if (fclose(fp) == EOF) {
                FILE_OP_ERROR(tmp, "fclose");
                claws_unlink(tmp);
                g_free(tmp);
-               lock = FALSE;
                return -2;
        }
 
@@ -5982,7 +6080,6 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                g_warning("can't find queue folder\n");
                claws_unlink(tmp);
                g_free(tmp);
-               lock = FALSE;
                return -1;
        }
        folder_item_scan(queue);
@@ -5990,7 +6087,6 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                g_warning("can't queue the message\n");
                claws_unlink(tmp);
                g_free(tmp);
-               lock = FALSE;
                return -1;
        }
        
@@ -6041,13 +6137,14 @@ static int compose_add_attachments(Compose *compose, MimeInfo *parent)
                        }
                        continue;
                }
+               if (g_stat(ainfo->file, &statbuf) < 0)
+                       return -1;
+
                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;
-
-               g_stat(ainfo->file, &statbuf);
                mimepart->length = statbuf.st_size;
 
                type = g_strdup(ainfo->content_type);
@@ -6109,6 +6206,48 @@ static int compose_add_attachments(Compose *compose, MimeInfo *parent)
        return 0;
 }
 
+static gchar *compose_quote_list_of_addresses(gchar *str)
+{
+       GSList *list = NULL, *item = NULL;
+       gchar *qname = NULL, *faddr = NULL, *result = NULL;
+
+       list = address_list_append_with_comments(list, str);
+       for (item = list; item != NULL; item = item->next) {
+               gchar *spec = item->data;
+               gchar *endofname = strstr(spec, " <");
+               if (endofname != NULL) {
+                       gchar * qqname;
+                       *endofname = '\0';
+                       QUOTE_IF_REQUIRED_NORMAL(qname, spec, return NULL);
+                       qqname = escape_internal_quotes(qname, '"');
+                       *endofname = ' ';
+                       if (*qname != *spec || qqname != qname) { /* has been quoted, compute new */
+                               gchar *addr = g_strdup(endofname);
+                               gchar *name = (qqname != qname)? qqname: g_strdup(qname);
+                               faddr = g_strconcat(name, addr, NULL);
+                               g_free(name);
+                               g_free(addr);
+                               debug_print("new auto-quoted address: '%s'", faddr);
+                       }
+               }
+               if (result == NULL)
+                       result = g_strdup((faddr != NULL)? faddr: spec);
+               else {
+                       result = g_strconcat(result,
+                                            ", ",
+                                            (faddr != NULL)? faddr: spec,
+                                            NULL);
+               }
+               if (faddr != NULL) {
+                       g_free(faddr);
+                       faddr = NULL;
+               }
+       }
+       slist_free_strings_full(list);
+
+       return result;
+}
+
 #define IS_IN_CUSTOM_HEADER(header) \
        (compose->account->add_customhdr && \
         custom_header_find(compose->account->customhdr_list, header) != NULL)
@@ -6141,9 +6280,11 @@ static void compose_add_headerfield_from_headerlist(Compose *compose,
                headerentryname = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((headerentry->combo)))));
 
                if (!g_utf8_collate(trans_fieldname, headerentryname)) {
-                       str = gtk_editable_get_chars(GTK_EDITABLE(headerentry->entry), 0, -1);
-                       g_strstrip(str);
-                       if (str[0] != '\0') {
+                       gchar * ustr = gtk_editable_get_chars(GTK_EDITABLE(headerentry->entry), 0, -1);
+                       g_strstrip(ustr);
+                       str = compose_quote_list_of_addresses(ustr);
+                       g_free(ustr);
+                       if (str != NULL && str[0] != '\0') {
                                if (add_field)
                                        g_string_append(fieldstr, seperator);
                                g_string_append(fieldstr, str);
@@ -6273,13 +6414,17 @@ static gchar *compose_get_header(Compose *compose)
        
        
        if (from_name && *from_name) {
+               gchar *qname;
                compose_convert_header
                        (compose, buf, sizeof(buf), from_name,
                         strlen("From: "), TRUE);
                QUOTE_IF_REQUIRED(name, buf);
+               qname = escape_internal_quotes(name, '"');
                
                g_string_append_printf(header, "From: %s <%s>\n",
-                       name, from_address);
+                       qname, from_address);
+               if (qname != name)
+                       g_free(qname);
        } else
                g_string_append_printf(header, "From: %s\n", from_address);
        
@@ -6335,6 +6480,8 @@ static gchar *compose_get_header(Compose *compose)
                }
                generate_msgid(buf, sizeof(buf), addr);
                g_string_append_printf(header, "Message-ID: <%s>\n", buf);
+               if (compose->msgid)
+                       g_free(compose->msgid);
                compose->msgid = g_strdup(buf);
        } else {
                compose->msgid = NULL;
@@ -6375,7 +6522,8 @@ static gchar *compose_get_header(Compose *compose)
                        gtk_major_version, gtk_minor_version, gtk_micro_version,
                        TARGET_ALIAS);
        }
-       if (g_slist_length(compose->newsgroup_list) && !IS_IN_CUSTOM_HEADER("X-Newsreader")) {
+       if (compose->account->gen_xmailer &&
+           g_slist_length(compose->newsgroup_list) && !IS_IN_CUSTOM_HEADER("X-Newsreader")) {
                g_string_append_printf(header, "X-Newsreader: %s (GTK+ %d.%d.%d; %s)\n",
                        prog_version,
                        gtk_major_version, gtk_minor_version, gtk_micro_version,
@@ -6617,6 +6765,54 @@ static void compose_entry_popup_extend(GtkEntry *entry, GtkMenu *menu, gpointer
        gtk_widget_show(menuitem);
 }
 
+void compose_add_extra_header(gchar *header, GtkListStore *model)
+{
+       GtkTreeIter iter;
+       if (strcmp(header, "")) {
+               COMBOBOX_ADD(model, header, COMPOSE_TO);
+       }
+}
+
+void compose_add_extra_header_entries(GtkListStore *model)
+{
+       FILE *exh;
+       gchar *exhrc;
+       gchar buf[BUFFSIZE];
+       gint lastc;
+
+       if (extra_headers == NULL) {
+               exhrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "extraheaderrc", NULL);
+               if ((exh = g_fopen(exhrc, "rb")) == NULL) {
+                       debug_print("extra headers file not found\n");
+                       goto extra_headers_done;
+               }
+               while (fgets(buf, BUFFSIZE, exh) != NULL) {
+                       lastc = strlen(buf) - 1;        /* remove trailing control chars */
+                       while (lastc >= 0 && buf[lastc] != ':')
+                               buf[lastc--] = '\0';
+                       if (lastc > 0 && buf[0] != '#' && buf[lastc] == ':') {
+                               buf[lastc] = '\0'; /* remove trailing : for comparison */
+                               if (custom_header_is_allowed(buf)) {
+                                       buf[lastc] = ':';
+                                       extra_headers = g_slist_prepend(extra_headers, g_strdup(buf));
+                               }
+                               else
+                                       g_message("disallowed extra header line: %s\n", buf);
+                       }
+                       else {
+                               if (buf[0] != '#')
+                                       g_message("invalid extra header line: %s\n", buf);
+                       }
+               }
+               fclose(exh);
+extra_headers_done:
+               g_free(exhrc);
+               extra_headers = g_slist_prepend(extra_headers, g_strdup("")); /* end of list */
+               extra_headers = g_slist_reverse(extra_headers);
+       }
+       g_slist_foreach(extra_headers, (GFunc)compose_add_extra_header, (gpointer)model);
+}
+
 static void compose_create_header_entry(Compose *compose) 
 {
        gchar *headers[] = {"To:", "Cc:", "Bcc:", "Newsgroups:", "Reply-To:", "Followup-To:", NULL};
@@ -6631,15 +6827,14 @@ static void compose_create_header_entry(Compose *compose)
        gboolean standard_header = FALSE;
        GtkListStore *model;
        GtkTreeIter iter;
-#if !(GTK_CHECK_VERSION(2,12,0))
-       GtkTooltips *tips = compose->tooltips;
-#endif
        
        headerentry = g_new0(ComposeHeaderEntry, 1);
 
-       /* Combo box */
+       /* 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:"),
@@ -6652,12 +6847,25 @@ static void compose_create_header_entry(Compose *compose)
                        COMPOSE_REPLYTO);
        COMBOBOX_ADD(model, prefs_common_translated_header_name("Followup-To:"),
                        COMPOSE_FOLLOWUPTO);
+       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);
        gtk_widget_show(combo);
-       
+
+       /* Putting only the combobox child into focus chain of its parent causes
+        * the parent to be skipped when changing focus via Tab or Shift+Tab.
+        * This eliminates need to pres Tab twice in order to really get from the
+        * combobox to next widget. */
        GList *l = NULL;
        l = g_list_prepend(l, gtk_bin_get_child(GTK_BIN(combo)));
        gtk_container_set_focus_chain(GTK_CONTAINER(combo), l);
@@ -6666,7 +6874,7 @@ static void compose_create_header_entry(Compose *compose)
        gtk_table_attach(GTK_TABLE(compose->header_table), combo, 0, 1,
                        compose->header_nextrow, compose->header_nextrow+1,
                        GTK_SHRINK, GTK_FILL, 0, 0);
-       if (compose->header_last && (compose->draft_timeout_tag != -2)) {
+       if (compose->header_last && (compose->draft_timeout_tag != COMPOSE_DRAFT_TIMEOUT_FORBIDDEN)) {
                const gchar *last_header_entry = gtk_entry_get_text(
                                GTK_ENTRY(gtk_bin_get_child(GTK_BIN((compose->header_last->combo)))));
                string = headers;
@@ -6949,15 +7157,8 @@ static GtkWidget *compose_create_attach(Compose *compose)
                         G_CALLBACK(attach_selected), compose);
        g_signal_connect(G_OBJECT(attach_clist), "button_press_event",
                         G_CALLBACK(attach_button_pressed), compose);
-#ifndef MAEMO
        g_signal_connect(G_OBJECT(attach_clist), "popup-menu",
                         G_CALLBACK(popup_attach_button_pressed), compose);
-#else
-       gtk_widget_tap_and_hold_setup(GTK_WIDGET(attach_clist), NULL, NULL,
-                       GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
-       g_signal_connect(G_OBJECT(attach_clist), "tap-and-hold",
-                        G_CALLBACK(popup_attach_button_pressed), compose);
-#endif
        g_signal_connect(G_OBJECT(attach_clist), "key_press_event",
                         G_CALLBACK(attach_key_pressed), compose);
 
@@ -7009,15 +7210,23 @@ static GtkWidget *compose_create_others(Compose *compose)
        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",
@@ -7036,8 +7245,6 @@ static GtkWidget *compose_create_others(Compose *compose)
                         G_CALLBACK(compose_savemsg_select_cb),
                         compose);
 
-       rowcount++;
-
        return table;   
 }
 
@@ -7168,8 +7375,9 @@ static void compose_dict_changed(void *data)
 {
        Compose *compose = (Compose *) data;
 
-       if(compose->gtkaspell && 
-                  compose->gtkaspell->recheck_when_changing_dict == FALSE)
+       if(!compose->gtkaspell)
+               return; 
+       if(compose->gtkaspell->recheck_when_changing_dict == FALSE)
                return;
 
        gtkaspell_highlight_all(compose->gtkaspell);
@@ -7227,7 +7435,6 @@ static Compose *compose_create(PrefsAccount *account,
 
        UndoMain *undostruct;
 
-       gchar *titles[N_ATTACH_COLS];
        GtkWidget *popupmenu;
        GtkWidget *tmpl_menu;
        GtkActionGroup *action_group = NULL;
@@ -7243,22 +7450,13 @@ static Compose *compose_create(PrefsAccount *account,
        debug_print("Creating compose window...\n");
        compose = g_new0(Compose, 1);
 
-       titles[COL_MIMETYPE] = _("MIME type");
-       titles[COL_SIZE]     = _("Size");
-       titles[COL_NAME]     = _("Name");
-       titles[COL_CHARSET]  = _("Charset");
-
        compose->batch = batch;
        compose->account = account;
        compose->folder = folder;
        
-       compose->mutex = g_mutex_new();
+       compose->mutex = cm_mutex_new();
        compose->set_cursor_pos = -1;
 
-#if !(GTK_CHECK_VERSION(2,12,0))
-       compose->tooltips = tips;
-#endif
-
        window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "compose");
 
        gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
@@ -7306,11 +7504,7 @@ static Compose *compose_create(PrefsAccount *account,
        gtk_action_group_add_radio_actions(action_group, compose_radio_enc_entries,
                        G_N_ELEMENTS(compose_radio_enc_entries), C_AUTO, G_CALLBACK(compose_set_encoding_cb), (gpointer)compose);
 
-#ifndef MAEMO
        MENUITEM_ADDUI_MANAGER(compose->ui_manager, "/", "Menu", NULL, GTK_UI_MANAGER_MENUBAR)
-#else
-       MENUITEM_ADDUI_MANAGER(compose->ui_manager, "/", "Menu", NULL, GTK_UI_MANAGER_POPUP)
-#endif
 
        MENUITEM_ADDUI_MANAGER(compose->ui_manager, "/Menu", "Message", "Message", GTK_UI_MANAGER_MENU)
        MENUITEM_ADDUI_MANAGER(compose->ui_manager, "/Menu", "Edit", "Edit", GTK_UI_MANAGER_MENU)
@@ -7328,6 +7522,7 @@ static Compose *compose_create(PrefsAccount *account,
        MENUITEM_ADDUI_MANAGER(compose->ui_manager, "/Menu/Message", "AttachFile", "Message/AttachFile", GTK_UI_MANAGER_MENUITEM)
        MENUITEM_ADDUI_MANAGER(compose->ui_manager, "/Menu/Message", "InsertFile", "Message/InsertFile", GTK_UI_MANAGER_MENUITEM)
        MENUITEM_ADDUI_MANAGER(compose->ui_manager, "/Menu/Message", "InsertSig", "Message/InsertSig", GTK_UI_MANAGER_MENUITEM)
+       MENUITEM_ADDUI_MANAGER(compose->ui_manager, "/Menu/Message", "ReplaceSig", "Message/ReplaceSig", GTK_UI_MANAGER_MENUITEM)
        MENUITEM_ADDUI_MANAGER(compose->ui_manager, "/Menu/Message", "Separator2", "Message/---", GTK_UI_MANAGER_SEPARATOR)
        MENUITEM_ADDUI_MANAGER(compose->ui_manager, "/Menu/Message", "Save", "Message/Save", GTK_UI_MANAGER_MENUITEM)
        MENUITEM_ADDUI_MANAGER(compose->ui_manager, "/Menu/Message", "Separator3", "Message/---", GTK_UI_MANAGER_SEPARATOR)
@@ -7491,11 +7686,7 @@ static Compose *compose_create(PrefsAccount *account,
        gtk_widget_show_all(menubar);
 
        gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(compose->ui_manager));
-#ifndef MAEMO
        gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
-#else
-       hildon_window_set_menu(HILDON_WINDOW(window), GTK_MENU(menubar));
-#endif
 
        if (prefs_common.toolbar_detachable) {
                handlebox = gtk_handle_box_new();
@@ -7505,13 +7696,8 @@ static Compose *compose_create(PrefsAccount *account,
        gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
 
        gtk_widget_realize(handlebox);
-#ifdef MAEMO
-       compose->toolbar = toolbar_create(TOOLBAR_COMPOSE, window,
-                                         (gpointer)compose);
-#else
        compose->toolbar = toolbar_create(TOOLBAR_COMPOSE, handlebox,
                                          (gpointer)compose);
-#endif
 
        vbox2 = gtk_vbox_new(FALSE, 2);
        gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
@@ -7519,7 +7705,6 @@ static Compose *compose_create(PrefsAccount *account,
        
        /* Notebook */
        notebook = gtk_notebook_new();
-       gtk_widget_set_size_request(notebook, -1, prefs_common.compose_notebook_height);
        gtk_widget_show(notebook);
 
        /* header labels and entries */
@@ -7559,7 +7744,7 @@ static Compose *compose_create(PrefsAccount *account,
        gtk_container_set_border_width(GTK_CONTAINER(subject), 0);
        gtk_widget_show(subject);
 
-       label = gtk_label_new(_("Subject:"));
+       label = gtk_label_new_with_mnemonic(_("_Subject:"));
        gtk_box_pack_start(GTK_BOX(subject), label, FALSE, FALSE, 0);
        gtk_widget_show(label);
 
@@ -7571,6 +7756,7 @@ static Compose *compose_create(PrefsAccount *account,
        gtk_box_pack_start(GTK_BOX(subject), subject_entry, TRUE, TRUE, 0);
        g_signal_connect_after(G_OBJECT(subject_entry), "grab_focus",
                         G_CALLBACK(compose_grab_focus_cb), compose);
+       gtk_label_set_mnemonic_widget(GTK_LABEL(label), subject_entry);
        gtk_widget_show(subject_entry);
        compose->subject_entry = subject_entry;
        gtk_container_add(GTK_CONTAINER(subject_frame), subject);
@@ -7609,8 +7795,6 @@ static Compose *compose_create(PrefsAccount *account,
        gtk_text_buffer_add_selection_clipboard(buffer, clipboard);
        
        gtk_container_add(GTK_CONTAINER(scrolledwin), text);
-       g_signal_connect(G_OBJECT(notebook), "size_allocate",
-                        G_CALLBACK(compose_notebook_size_alloc), compose);     
        g_signal_connect_after(G_OBJECT(text), "size_allocate",
                               G_CALLBACK(compose_edit_size_alloc),
                               ruler);
@@ -7622,17 +7806,12 @@ static Compose *compose_create(PrefsAccount *account,
                         G_CALLBACK(text_inserted), compose);
        g_signal_connect(G_OBJECT(text), "button_press_event",
                         G_CALLBACK(text_clicked), compose);
-#ifndef MAEMO
        g_signal_connect(G_OBJECT(text), "popup-menu",
                         G_CALLBACK(compose_popup_menu), compose);
-#else
-       gtk_widget_tap_and_hold_setup(GTK_WIDGET(text), NULL, NULL,
-                       GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
-       g_signal_connect(G_OBJECT(text), "tap-and-hold",
-                        G_CALLBACK(compose_popup_menu), compose);
-#endif
        g_signal_connect(G_OBJECT(subject_entry), "changed",
-                        G_CALLBACK(compose_changed_cb), compose);
+                       G_CALLBACK(compose_changed_cb), compose);
+       g_signal_connect(G_OBJECT(subject_entry), "activate",
+                       G_CALLBACK(compose_subject_entry_activated), compose);
 
        /* drag and drop */
        gtk_drag_dest_set(text, GTK_DEST_DEFAULT_ALL, compose_mime_types, 
@@ -7652,14 +7831,12 @@ static Compose *compose_create(PrefsAccount *account,
        /* pane between attach clist and text */
        paned = gtk_vpaned_new();
        gtk_container_add(GTK_CONTAINER(vbox2), paned);
-#ifdef MAEMO
-       if( maemo_mainwindow_is_fullscreen(mainwindow_get_mainwindow()->window) )
-               gtk_widget_set_size_request(edit_vbox, -1, mode == COMPOSE_NEW ? 300 : 280);
-       else
-               gtk_widget_set_size_request(edit_vbox, -1, mode == COMPOSE_NEW ? 250 : 230);
-#endif
-       gtk_paned_add1(GTK_PANED(paned), notebook);
-       gtk_paned_add2(GTK_PANED(paned), edit_vbox);
+       gtk_paned_pack1(GTK_PANED(paned), notebook, FALSE, FALSE);
+       gtk_paned_pack2(GTK_PANED(paned), edit_vbox, TRUE, FALSE);
+       gtk_paned_set_position(GTK_PANED(paned), prefs_common.compose_notebook_height);
+       g_signal_connect(G_OBJECT(notebook), "size_allocate",
+                        G_CALLBACK(compose_notebook_size_alloc), paned);
+
        gtk_widget_show_all(paned);
 
 
@@ -7763,7 +7940,12 @@ static Compose *compose_create(PrefsAccount *account,
        compose->exteditor_file    = NULL;
        compose->exteditor_pid     = -1;
        compose->exteditor_tag     = -1;
-       compose->draft_timeout_tag = -2; /* inhibit auto-drafting while loading */
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_FORBIDDEN; /* inhibit auto-drafting while loading */
+
+       compose->folder_update_callback_id =
+               hooks_register_hook(FOLDER_UPDATE_HOOKLIST,
+                               compose_update_folder_hook,
+                               (gpointer) compose);
 
 #if USE_ENCHANT
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Spelling", FALSE);
@@ -7854,10 +8036,6 @@ static Compose *compose_create(PrefsAccount *account,
                gtk_widget_realize(window);
        } else {
                gtk_widget_show(window);
-#ifdef MAEMO
-               maemo_window_full_screen_if_needed(GTK_WINDOW(window));
-               maemo_connect_key_press_to_mainwindow(GTK_WINDOW(window));
-#endif
        }
        
        return compose;
@@ -7872,9 +8050,6 @@ static GtkWidget *compose_account_option_menu_create(Compose *compose)
        GtkListStore *menu;
        GtkTreeIter iter;
        GtkWidget *from_name = NULL;
-#if !(GTK_CHECK_VERSION(2,12,0))
-       GtkTooltips *tips = compose->tooltips;
-#endif
 
        gint num = 0, def_menu = 0;
        
@@ -7929,6 +8104,15 @@ static GtkWidget *compose_account_option_menu_create(Compose *compose)
 
        gtk_box_pack_start(GTK_BOX(hbox), optmenubox, FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(hbox), from_name, TRUE, TRUE, 0);
+
+       /* Putting only the GtkEntry into focus chain of parent hbox causes
+        * the account selector combobox next to it to be unreachable when
+        * navigating widgets in GtkTable with up/down arrow keys.
+        * Note: gtk_widget_set_can_focus() was not enough. */
+       GList *l = NULL;
+       l = g_list_prepend(l, from_name);
+       gtk_container_set_focus_chain(GTK_CONTAINER(hbox), l);
+       g_list_free(l);
        
        CLAWS_SET_TIP(optmenubox,
                _("Account to use for this email"));
@@ -8474,6 +8658,24 @@ static void compose_template_apply_fields(Compose *compose, Template *tmpl)
                }
        }
 
+       if (tmpl->replyto && *tmpl->replyto != '\0') {
+#ifdef USE_ENCHANT
+               quote_fmt_init(msginfo, NULL, NULL, FALSE, compose->account, FALSE,
+                               compose->gtkaspell);
+#else
+               quote_fmt_init(msginfo, NULL, NULL, FALSE, compose->account, FALSE);
+#endif
+               quote_fmt_scan_string(tmpl->replyto);
+               quote_fmt_parse();
+
+               buf = quote_fmt_get_buffer();
+               if (buf == NULL) {
+                       alertpanel_error(_("Template Reply-To format error."));
+               } else {
+                       compose_entry_append(compose, buf, COMPOSE_REPLYTO, PREF_TEMPLATE);
+               }
+       }
+
        /* process the subject */
        if (tmpl->subject && *tmpl->subject != '\0') {
 #ifdef USE_ENCHANT
@@ -8509,21 +8711,25 @@ static void compose_destroy(Compose *compose)
                compose->deferred_destroy = TRUE;
                return;
        }
+
        /* NOTE: address_completion_end() does nothing with the window
         * however this may change. */
        address_completion_end(compose->window);
 
-       slist_free_strings(compose->to_list);
-       g_slist_free(compose->to_list);
-       slist_free_strings(compose->newsgroup_list);
-       g_slist_free(compose->newsgroup_list);
-       slist_free_strings(compose->header_list);
-       g_slist_free(compose->header_list);
+       slist_free_strings_full(compose->to_list);
+       slist_free_strings_full(compose->newsgroup_list);
+       slist_free_strings_full(compose->header_list);
+
+       slist_free_strings_full(extra_headers);
+       extra_headers = NULL;
 
        compose->header_list = compose->newsgroup_list = compose->to_list = NULL;
 
        g_hash_table_destroy(compose->email_hashtable);
 
+       hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST,
+                       compose->folder_update_callback_id);
+
        procmsg_msginfo_free(compose->targetinfo);
        procmsg_msginfo_free(compose->replyinfo);
        procmsg_msginfo_free(compose->fwdinfo);
@@ -8581,7 +8787,7 @@ static void compose_destroy(Compose *compose)
        gtk_widget_destroy(compose->window);
        toolbar_destroy(compose->toolbar);
        g_free(compose->toolbar);
-       g_mutex_free(compose->mutex);
+       cm_mutex_free(compose->mutex);
        g_free(compose);
 }
 
@@ -8739,7 +8945,7 @@ static void compose_attach_property(GtkAction *action, gpointer data)
                gtk_widget_hide(attach_prop.window);
                gtk_window_set_modal(GTK_WINDOW(attach_prop.window), FALSE);
                
-               if (cancelled) 
+               if (cancelled)
                        break;
 
                entry_text = gtk_entry_get_text(GTK_ENTRY(attach_prop.mimetype_entry));
@@ -8861,7 +9067,11 @@ 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);
                         
@@ -8883,7 +9093,11 @@ static void compose_attach_property_create(gboolean *cancelled)
 
        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);
@@ -9193,6 +9407,7 @@ static void compose_set_ext_editor_sensitive(Compose *compose,
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Message/SendLater", sensitive);
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Message/InsertFile", sensitive);
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Message/InsertSig", sensitive);
+       cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Message/ReplaceSig", sensitive);
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Edit/WrapPara", sensitive);
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Edit/WrapAllLines", sensitive);
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Edit/ExtEditor", sensitive);
@@ -9278,9 +9493,9 @@ static void compose_undo_state_changed(UndoMain *undostruct, gint undo_state,
 
 static void compose_notebook_size_alloc(GtkNotebook *notebook,
                                        GtkAllocation *allocation,
-                                       Compose *compose)
+                                       GtkPaned *paned)
 {
-       prefs_common.compose_notebook_height = allocation->height;
+       prefs_common.compose_notebook_height = gtk_paned_get_position(paned);
 }
 
 /* compose_edit_size_alloc() - called when resized. don't know whether Gtk
@@ -9327,12 +9542,16 @@ static void account_activated(GtkComboBox *optmenu, gpointer data)
        GSList *list, *saved_list = NULL;
        HeaderEntryState *state;
        GtkRcStyle *style = NULL;
+#if !GTK_CHECK_VERSION(3, 0, 0)
        static GdkColor yellow;
        static gboolean color_set = FALSE;
+#else
+       static GdkColor yellow = { (guint32)0, (guint32)0xf5, (guint32)0xf6, (guint32)0xbe };
+#endif
 
        /* Get ID of active account in the combo box */
        menu = gtk_combo_box_get_model(optmenu);
-       gtk_combo_box_get_active_iter(optmenu, &iter);
+       cm_return_if_fail(gtk_combo_box_get_active_iter(optmenu, &iter));
        gtk_tree_model_get(menu, &iter, 1, &account_id, -1);
 
        ac = account_find_from_id(account_id);
@@ -9356,12 +9575,14 @@ static void account_activated(GtkComboBox *optmenu, gpointer data)
                                        GTK_EDITABLE(hentry->entry), 0, -1);
                        state->type = hentry->type;
                                
+#if !GTK_CHECK_VERSION(3, 0, 0)
                        if (!color_set) {
                                gdk_color_parse("#f5f6be", &yellow);
                                color_set = gdk_colormap_alloc_color(
                                                        gdk_colormap_get_system(),
                                                        &yellow, FALSE, TRUE);
                        }
+#endif
                                
                        style = gtk_widget_get_modifier_style(hentry->entry);
                        state->entry_marked = gdk_color_equal(&yellow,
@@ -9497,7 +9718,7 @@ static void compose_send_cb(GtkAction *action, gpointer data)
        
        if (compose->draft_timeout_tag >= 0) { /* CLAWS: disable draft timeout */
                g_source_remove(compose->draft_timeout_tag);
-               compose->draft_timeout_tag = -1;
+               compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET;
        }
 
        compose_send(compose);
@@ -9582,7 +9803,7 @@ gboolean compose_draft (gpointer data, guint action)
                debug_print("couldn't lock mutex, probably sending\n");
                return FALSE;
        }
-       
+
        lock = TRUE;
 
        tmp = g_strdup_printf("%s%cdraft.%p", get_tmp_dir(),
@@ -9620,17 +9841,25 @@ gboolean compose_draft (gpointer data, guint action)
 
        /* Message-ID of message replying to */
        if ((compose->replyinfo != NULL) && (compose->replyinfo->msgid != NULL)) {
-               gchar *folderid;
-               
-               folderid = folder_item_get_identifier(compose->replyinfo->folder);
+               gchar *folderid = NULL;
+
+               if (compose->replyinfo->folder)
+                       folderid = folder_item_get_identifier(compose->replyinfo->folder);
+               if (folderid == NULL)
+                       folderid = g_strdup("NULL");
+
                err |= (fprintf(fp, "RMID:%s\t%d\t%s\n", folderid, compose->replyinfo->msgnum, compose->replyinfo->msgid) < 0);
                g_free(folderid);
        }
        /* Message-ID of message forwarding to */
        if ((compose->fwdinfo != NULL) && (compose->fwdinfo->msgid != NULL)) {
-               gchar *folderid;
-               
-               folderid = folder_item_get_identifier(compose->fwdinfo->folder);
+               gchar *folderid = NULL;
+
+               if (compose->fwdinfo->folder)
+                       folderid = folder_item_get_identifier(compose->fwdinfo->folder);
+               if (folderid == NULL)
+                       folderid = g_strdup("NULL");
+
                err |= (fprintf(fp, "FMID:%s\t%d\t%s\n", folderid, compose->fwdinfo->msgnum, compose->fwdinfo->msgid) < 0);
                g_free(folderid);
        }
@@ -9658,9 +9887,11 @@ gboolean compose_draft (gpointer data, guint action)
                goto warn_err;
        }
        
+       flag.perm_flags = MSG_NEW|MSG_UNREAD;
        if (compose->targetinfo) {
                target_locked = MSG_IS_LOCKED(compose->targetinfo->flags);
-               flag.perm_flags = target_locked?MSG_LOCKED:0;
+               if (target_locked) 
+                       flag.perm_flags |= MSG_LOCKED;
        }
        flag.tmp_flags = MSG_DRAFT;
 
@@ -9721,9 +9952,9 @@ warn_err:
        if (newmsginfo) {
                procmsg_msginfo_unset_flags(newmsginfo, ~0, ~0);
                if (target_locked)
-                       procmsg_msginfo_set_flags(newmsginfo, MSG_LOCKED, MSG_DRAFT);
+                       procmsg_msginfo_set_flags(newmsginfo, MSG_NEW|MSG_UNREAD|MSG_LOCKED, MSG_DRAFT);
                else
-                       procmsg_msginfo_set_flags(newmsginfo, 0, MSG_DRAFT);
+                       procmsg_msginfo_set_flags(newmsginfo, MSG_NEW|MSG_UNREAD, MSG_DRAFT);
                if (compose_use_attach(compose) && action != COMPOSE_AUTO_SAVE)
                        procmsg_msginfo_set_flags(newmsginfo, 0,
                                                  MSG_HAS_ATTACHMENT);
@@ -9911,6 +10142,13 @@ static void compose_insert_sig_cb(GtkAction *action, gpointer data)
        compose_insert_sig(compose, FALSE);
 }
 
+static void compose_replace_sig_cb(GtkAction *action, gpointer data)
+{
+       Compose *compose = (Compose *)data;
+
+       compose_insert_sig(compose, TRUE);
+}
+
 static gint compose_delete_cb(GtkWidget *widget, GdkEventAny *event,
                              gpointer data)
 {
@@ -9933,6 +10171,14 @@ void compose_close_toolbar(Compose *compose)
        compose_close_cb(NULL, compose);
 }
 
+static gboolean compose_can_autosave(Compose *compose)
+{
+       if (compose->privacy_system && compose->use_encryption)
+               return prefs_common.autosave && prefs_common.autosave_encrypted;
+       else
+               return prefs_common.autosave;
+}
+
 static void compose_close_cb(GtkAction *action, gpointer data)
 {
        Compose *compose = (Compose *)data;
@@ -9967,8 +10213,8 @@ static void compose_close_cb(GtkAction *action, gpointer data)
                g_mutex_unlock(compose->mutex);
                switch (val) {
                case G_ALERTDEFAULT:
-                       if (prefs_common.autosave && !reedit)
-                               compose_remove_draft(compose);                  
+                       if (compose_can_autosave(compose) && !reedit)
+                               compose_remove_draft(compose);
                        break;
                case G_ALERTALTERNATE:
                        compose_draft(data, COMPOSE_QUIT_EDITING);
@@ -10032,7 +10278,7 @@ static void compose_template_activate_cb(GtkWidget *widget, gpointer data)
        tmpl = g_object_get_data(G_OBJECT(widget), "template");
        cm_return_if_fail(tmpl != NULL);
 
-       msg = g_strdup_printf(_("Do you want to apply the template '%s' ?"),
+       msg = g_strdup_printf(_("Do you want to apply the template '%s'?"),
                              tmpl->name);
        val = alertpanel(_("Apply template"), msg,
                         _("_Replace"), _("_Insert"), GTK_STOCK_CANCEL);
@@ -10373,14 +10619,12 @@ static void textview_move_backward_word (GtkTextView *text)
        GtkTextBuffer *buffer;
        GtkTextMark *mark;
        GtkTextIter ins;
-       gint count;
 
        cm_return_if_fail(GTK_IS_TEXT_VIEW(text));
 
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
        mark = gtk_text_buffer_get_insert(buffer);
        gtk_text_buffer_get_iter_at_mark(buffer, &ins, mark);
-       count = gtk_text_iter_inside_word (&ins) ? 2 : 1;
        if (gtk_text_iter_backward_word_starts(&ins, 1))
                gtk_text_buffer_place_cursor(buffer, &ins);
 }
@@ -10946,10 +11190,7 @@ static void compose_insert_drag_received_cb (GtkWidget         *widget,
                g_list_free(list);
                gtk_drag_finish(drag_context, TRUE, FALSE, time);
                return;
-       } else {
-               return;
        }
-       gtk_drag_finish(drag_context, TRUE, FALSE, time);
 }
 
 static void compose_header_drag_received_cb (GtkWidget         *widget,
@@ -11044,7 +11285,8 @@ static gboolean scroll_postpone(gpointer data)
 {
        Compose *compose = (Compose *)data;
 
-       cm_return_val_if_fail(!compose->batch, FALSE);
+       if (compose->batch)
+               return FALSE;
 
        GTK_EVENTS_FLUSH();
        compose_show_first_last_header(compose, FALSE);
@@ -11067,7 +11309,7 @@ static void compose_headerentry_changed_cb(GtkWidget *entry,
 
 static gboolean compose_defer_auto_save_draft(Compose *compose)
 {
-       compose->draft_timeout_tag = -1;
+       compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_UNSET;
        compose_draft((gpointer)compose, COMPOSE_AUTO_SAVE);
        return FALSE;
 }
@@ -11077,7 +11319,10 @@ static void compose_show_first_last_header(Compose *compose, gboolean show_first
        GtkAdjustment *vadj;
 
        cm_return_if_fail(compose);
-       cm_return_if_fail(!compose->batch);
+
+       if(compose->batch)
+               return;
+
        cm_return_if_fail(GTK_IS_WIDGET(compose->header_table));
        cm_return_if_fail(GTK_IS_VIEWPORT(gtk_widget_get_parent(compose->header_table)));
        vadj = gtk_viewport_get_vadjustment(GTK_VIEWPORT(
@@ -11186,9 +11431,9 @@ static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
                                          compose);
        g_signal_stop_emission_by_name(G_OBJECT(buffer), "insert-text");
 
-       if (prefs_common.autosave && 
+       if (compose_can_autosave(compose) && 
            gtk_text_buffer_get_char_count(buffer) % prefs_common.autosave_length == 0 &&
-           compose->draft_timeout_tag != -2 /* disabled while loading */)
+           compose->draft_timeout_tag != COMPOSE_DRAFT_TIMEOUT_FORBIDDEN /* disabled while loading */)
                compose->draft_timeout_tag = g_timeout_add
                        (500, (GSourceFunc) compose_defer_auto_save_draft, compose);
 }
@@ -11300,16 +11545,27 @@ gboolean compose_close(Compose *compose)
 {
        gint x, y;
 
+       cm_return_val_if_fail(compose, FALSE);
+
        if (!g_mutex_trylock(compose->mutex)) {
                /* we have to wait for the (possibly deferred by auto-save)
                 * drafting to be done, before destroying the compose under
                 * it. */
                debug_print("waiting for drafting to finish...\n");
                compose_allow_user_actions(compose, FALSE);
-               g_timeout_add (500, (GSourceFunc) compose_close, compose);
-               return FALSE;
+               if (compose->close_timeout_tag == 0) {
+                       compose->close_timeout_tag = 
+                               g_timeout_add (500, (GSourceFunc) compose_close,
+                               compose);
+               }
+               return TRUE;
        }
-       cm_return_val_if_fail(compose, FALSE);
+
+       if (compose->draft_timeout_tag >= 0) {
+               g_source_remove(compose->draft_timeout_tag);
+               compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_FORBIDDEN;
+       }
+
        gtkut_widget_get_uposition(compose->window, &x, &y);
        if (!compose->batch) {
                prefs_common.compose_x = x;
@@ -11595,6 +11851,15 @@ static void compose_set_dictionaries_from_folder_prefs(Compose *compose,
 }
 #endif
 
+static void compose_subject_entry_activated(GtkWidget *widget, gpointer data)
+{
+       Compose *compose = (Compose *)data;
+
+       cm_return_if_fail(compose != NULL);
+
+       gtk_widget_grab_focus(compose->text);
+}
+
 /*
  * End of Source.
  */