this type_hint not wanted or needed here
[claws.git] / src / compose.c
index ade8db94a1594df143364720d718e6a274620093..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 "claws_io.h"
+#include "file-utils.h"
 
 #ifdef USE_LDAP
 #include "password.h"
@@ -1010,6 +1010,13 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
        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) {
                gtk_entry_set_text(GTK_ENTRY(compose->from_name), mailto_from);
@@ -1201,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);
@@ -1273,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))
@@ -1310,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;
@@ -1580,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);
@@ -1679,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)
@@ -1896,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);
@@ -2386,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);
                }
@@ -2433,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);
@@ -2526,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);
@@ -2871,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') {
@@ -2996,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) {
@@ -3672,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;
        }
 
@@ -3692,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)
@@ -3736,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;
 }
@@ -3788,11 +3811,11 @@ static gboolean compose_attach_append(Compose *compose, const gchar *file,
                        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
@@ -4233,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;
@@ -4254,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)
@@ -4308,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;
        }
@@ -4951,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);
-                                      
-       compose_activate_privacy_system(compose, account, FALSE);
 
        if (!init && compose->mode != COMPOSE_REDIRECT) {
                undo_block(compose->undostruct);
@@ -5532,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);
 }
@@ -5565,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;
        }
 
@@ -5584,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;
@@ -5593,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];
@@ -5609,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;
        }
 
@@ -5625,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;
 }
@@ -5897,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)
@@ -5948,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;
        }
 
@@ -5969,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 (safe_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;
        }
@@ -6116,8 +6146,8 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
        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 COMPOSE_QUEUE_ERROR_WITH_ERRNO;
        }
@@ -6179,7 +6209,7 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
                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 COMPOSE_QUEUE_ERROR_NO_MSG;
@@ -6204,7 +6234,7 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
                                 * key selection */
                                if (err == TRUE)
                                        g_warning("failed to write queue message");
-                               fclose(fp);
+                               claws_fclose(fp);
                                claws_unlink(tmp);
                                g_free(tmp);
                                return COMPOSE_QUEUE_ERROR_NO_ENCRYPTION_KEY;
@@ -6257,7 +6287,7 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
 
        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 COMPOSE_QUEUE_ERROR_WITH_ERRNO;
@@ -6265,7 +6295,7 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
        } 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;
@@ -6273,13 +6303,13 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
        }
        if (err == TRUE) {
                g_warning("failed to write queue message");
-               fclose(fp);
+               claws_fclose(fp);
                claws_unlink(tmp);
                g_free(tmp);
                return COMPOSE_QUEUE_ERROR_WITH_ERRNO;
        }
-       if (safe_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 COMPOSE_QUEUE_ERROR_WITH_ERRNO;
@@ -6428,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,
@@ -6439,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)
@@ -7050,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';
@@ -7072,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 */
@@ -7470,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)
@@ -7497,8 +7526,6 @@ 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);
 
        savemsg_combo = gtk_combo_box_text_new_with_entry();
        compose->savemsg_checkbtn = savemsg_checkbtn;
@@ -7513,6 +7540,11 @@ static GtkWidget *compose_create_others(Compose *compose)
        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);
@@ -7529,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;
@@ -9160,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);
@@ -9427,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);
@@ -9459,11 +9487,11 @@ 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; 
@@ -10115,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);
@@ -10260,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);       
@@ -10306,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;
        }
 
@@ -10373,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 (safe_fclose(fp) == EOF) {
+       if (claws_safe_fclose(fp) == EOF) {
                goto warn_err;
        }
        
@@ -10551,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;
@@ -10568,7 +10601,7 @@ void compose_reopen_exit_drafts(void)
                        }
                        g_strfreev(parts);
                }       
-               fclose(fp);
+               claws_fclose(fp);
        }       
        g_free(filepath);
        compose_clear_exit_drafts();
@@ -10865,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)
@@ -11539,11 +11698,13 @@ static void compose_activate_privacy_system(Compose *compose, PrefsAccount *acco
 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) {
+               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) {
+               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);
                }
@@ -11574,25 +11735,13 @@ 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_full(list);
+               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 */
@@ -11674,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);