CLI: Add a --insert option to --compose, allowing to insert files.
[claws.git] / src / compose.c
index 4a29775d4c257da93882fca6938a1cf0ccf2a716..5aad2778780b0a04b1cb35418bdad97ba2b0643b 100644 (file)
@@ -490,8 +490,6 @@ static void compose_update_privacy_system_menu_item(Compose * compose, gboolean
 static void activate_privacy_system     (Compose *compose, 
                                          PrefsAccount *account,
                                         gboolean warn);
-static void compose_use_signing(Compose *compose, gboolean use_signing);
-static void compose_use_encryption(Compose *compose, gboolean use_encryption);
 static void compose_toggle_return_receipt_cb(GtkToggleAction *action,
                                         gpointer        data);
 static void compose_toggle_remove_refs_cb(GtkToggleAction *action,
@@ -1086,6 +1084,7 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
                        else
                                gtk_entry_set_text(GTK_ENTRY(compose->from_name), buf);
                        quote_fmt_reset_vartable();
+                       quote_fmtlex_destroy();
 
                        g_free(tmp);
                }
@@ -1179,6 +1178,7 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
                                gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf);
                        compose_attach_from_list(compose, quote_fmt_get_attachments_list(), FALSE);
                        quote_fmt_reset_vartable();
+                       quote_fmtlex_destroy();
 
                        g_free(subject);
                        g_free(tmp);
@@ -1226,7 +1226,10 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
 
                for (curr = attach_files ; curr != NULL ; curr = curr->next) {
                        ainfo = (AttachInfo *) curr->data;
-                       compose_attach_append(compose, ainfo->file, ainfo->file,
+                       if (ainfo->insert)
+                               compose_insert_file(compose, ainfo->file);
+                       else
+                               compose_attach_append(compose, ainfo->file, ainfo->file,
                                        ainfo->content_type, ainfo->charset);
                }
        }
@@ -1321,6 +1324,7 @@ static void compose_force_encryption(Compose *compose, PrefsAccount *account,
                if (privacy_avail && g_slist_length(privacy_avail)) {
                        privacy = (gchar *)(privacy_avail->data);
                }
+               g_slist_free_full(privacy_avail, g_free);
        }
        if (privacy != NULL) {
                if (system) {
@@ -1653,6 +1657,7 @@ static Compose *compose_generic_reply(MsgInfo *msginfo,
                else
                        gtk_entry_set_text(GTK_ENTRY(compose->from_name), buf);
                quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
 
                g_free(tmp);
        }
@@ -1835,6 +1840,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
                else
                        gtk_entry_set_text(GTK_ENTRY(compose->from_name), buf);
                quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
 
                g_free(tmp);
                procmsg_msginfo_free(&full_msginfo);
@@ -2026,6 +2032,7 @@ static Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_
                        else
                                gtk_entry_set_text(GTK_ENTRY(compose->from_name), buf);
                        quote_fmt_reset_vartable();
+                       quote_fmtlex_destroy();
 
                        g_free(tmp);
                }
@@ -2756,6 +2763,10 @@ void compose_toolbar_cb(gint action, gpointer data)
                compose_check_all(NULL, compose);
                break;
 #endif
+       case A_PRIVACY_SIGN:
+               break;
+       case A_PRIVACY_ENCRYPT:
+               break;
        default:
                break;
        }
@@ -2981,7 +2992,9 @@ static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
                if (msginfo->inreplyto && *msginfo->inreplyto)
                        compose->inreplyto = g_strdup(msginfo->inreplyto);
 
-               if (msginfo->msgid && *msginfo->msgid)
+               if (msginfo->msgid && *msginfo->msgid &&
+                               compose->folder != NULL &&
+                               compose->folder->stype ==  F_DRAFT)
                        compose->msgid = g_strdup(msginfo->msgid);
        } else {
                if (msginfo->msgid && *msginfo->msgid)
@@ -3120,6 +3133,7 @@ static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
                quote_fmt_parse();
 
                buf = quote_fmt_get_buffer();
+
                if (buf == NULL)
                        alertpanel_error(_("The \"Quotation mark\" of the template is invalid."));
                else
@@ -3153,11 +3167,14 @@ static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
                }
 
                buf = quote_fmt_get_buffer();
+
                if (buf == NULL) {
                        gint line = quote_fmt_get_line();
                        alertpanel_error(err_msg, line);
+
                        goto error;
                }
+
        } else
                buf = "";
 
@@ -3868,13 +3885,13 @@ static gboolean compose_attach_append(Compose *compose, const gchar *file,
        return TRUE;
 }
 
-static void compose_use_signing(Compose *compose, gboolean use_signing)
+void compose_use_signing(Compose *compose, gboolean use_signing)
 {
        compose->use_signing = use_signing;
        cm_toggle_menu_set_active_full(compose->ui_manager, "Menu/Options/Sign", use_signing);
 }
 
-static void compose_use_encryption(Compose *compose, gboolean use_encryption)
+void compose_use_encryption(Compose *compose, gboolean use_encryption)
 {
        compose->use_encryption = use_encryption;
        cm_toggle_menu_set_active_full(compose->ui_manager, "Menu/Options/Encrypt", use_encryption);
@@ -4082,10 +4099,22 @@ static int compose_itemized_length(GtkTextBuffer *buffer,
        }
 
        clen = g_unichar_to_utf8(wc, ch);
-       if (clen != 1)
-               return 0;
-
-       if (!strchr("*-+", ch[0]))
+       if (!((clen == 1 && strchr("*-+", ch[0])) ||
+           (clen == 3 && (
+               wc == 0x2022 || /* BULLET */
+               wc == 0x2023 || /* TRIANGULAR BULLET */
+               wc == 0x2043 || /* HYPHEN BULLET */
+               wc == 0x204c || /* BLACK LEFTWARDS BULLET */
+               wc == 0x204d || /* BLACK RIGHTWARDS BULLET */
+               wc == 0x2219 || /* BULLET OPERATOR */
+               wc == 0x25d8 || /* INVERSE BULLET */
+               wc == 0x25e6 || /* WHITE BULLET */
+               wc == 0x2619 || /* REVERSED ROTATED FLORAL HEART BULLET */
+               wc == 0x2765 || /* ROTATED HEAVY BLACK HEART BULLET */
+               wc == 0x2767 || /* ROTATED FLORAL HEART BULLET */
+               wc == 0x29be || /* CIRCLED WHITE BULLET */
+               wc == 0x29bf    /* CIRCLED BULLET */
+               ))))
                return 0;
 
        gtk_text_iter_forward_char(&iter);
@@ -5143,6 +5172,7 @@ static gboolean compose_check_entries(Compose *compose, gboolean check_everythin
                                               GTK_STOCK_CANCEL, button_label, NULL, TRUE, NULL,
                                               ALERT_QUESTION, G_ALERTDEFAULT);
                        g_free(message);
+                       g_free(button_label);
                        if (aval & G_ALERTDISABLE) {
                                aval &= ~G_ALERTDISABLE;
                                prefs_common.warn_empty_subj = FALSE;
@@ -5166,9 +5196,9 @@ static gboolean compose_check_entries(Compose *compose, gboolean check_everythin
                        entry = gtk_editable_get_chars(GTK_EDITABLE(((ComposeHeaderEntry *)list->data)->entry), 0, -1);
                        g_strstrip(header);
                        g_strstrip(entry);
-                       if ((entry[0] != '\0')
-                       &&      (strcmp(header, prefs_common_translated_header_name("To:"))
-                       ||  strcmp(header, prefs_common_translated_header_name("Cc:")))) {
+                       if ((entry[0] != '\0') &&
+                           (!strcmp(header, prefs_common_translated_header_name("To:")) ||
+                            !strcmp(header, prefs_common_translated_header_name("Cc:")))) {
                                cnt++;
                        }
                        g_free(header);
@@ -5461,7 +5491,7 @@ static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
                get_rfc822_date_hide_tz(date, sizeof(date));
        else
                get_rfc822_date(date, sizeof(date));
-       err |= (fprintf(fp, "Resent-Date: %s\n", buf) < 0);
+       err |= (fprintf(fp, "Resent-Date: %s\n", date) < 0);
 
        /* Resent-From */
        if (compose->account->name && *compose->account->name) {
@@ -5611,7 +5641,7 @@ error:
 static gint compose_write_to_file(Compose *compose, FILE *fp, gint action, gboolean attach_parts)
 {
        GtkTextBuffer *buffer;
-       GtkTextIter start, end;
+       GtkTextIter start, end, tmp;
        gchar *chars, *tmp_enc_file, *content;
        gchar *buf, *msg;
        const gchar *out_codeset;
@@ -5643,10 +5673,21 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action, gbool
        mimemsg->data.mem = compose_get_header(compose);
 
        /* Create text part MimeInfo */
-       /* get all composed text */
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(compose->text));
-       gtk_text_buffer_get_start_iter(buffer, &start);
        gtk_text_buffer_get_end_iter(buffer, &end);
+       tmp = end;
+
+       /* We make sure that there is a newline at the end. */
+       if (action == COMPOSE_WRITE_FOR_SEND && gtk_text_iter_backward_char(&tmp)) {
+               chars = gtk_text_buffer_get_text(buffer, &tmp, &end, FALSE);
+               if (*chars != '\n') {
+                       gtk_text_buffer_insert(buffer, &end, "\n", 1);
+               }
+               g_free(chars);
+       }
+
+       /* get all composed text */
+       gtk_text_buffer_get_start_iter(buffer, &start);
        chars = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
 
        out_codeset = conv_get_charset_str(compose->out_encoding);
@@ -8445,6 +8486,22 @@ static void compose_set_privacy_system_cb(GtkWidget *widget, gpointer data)
 
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Options/Sign", can_sign);
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Options/Encrypt", can_encrypt);
+       if (compose->toolbar->privacy_sign_btn != NULL) {
+               gtk_widget_set_sensitive(
+                       GTK_WIDGET(compose->toolbar->privacy_sign_btn),
+                       can_sign);
+               gtk_toggle_tool_button_set_active(
+                       GTK_TOGGLE_TOOL_BUTTON(compose->toolbar->privacy_sign_btn),
+                       can_sign ? compose->use_signing : FALSE);
+       }
+       if (compose->toolbar->privacy_encrypt_btn != NULL) {
+               gtk_widget_set_sensitive(
+                       GTK_WIDGET(compose->toolbar->privacy_encrypt_btn),
+                       can_encrypt);
+               gtk_toggle_tool_button_set_active(
+                       GTK_TOGGLE_TOOL_BUTTON(compose->toolbar->privacy_encrypt_btn),
+                       can_encrypt ? compose->use_encryption : FALSE);
+       }
 }
 
 static void compose_update_privacy_system_menu_item(Compose * compose, gboolean warn)
@@ -8501,8 +8558,18 @@ static void compose_update_privacy_system_menu_item(Compose * compose, gboolean
 
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Options/Sign", can_sign);
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Options/Encrypt", can_encrypt);
-}      
+       if (compose->toolbar->privacy_sign_btn != NULL) {
+               gtk_widget_set_sensitive(
+                       GTK_WIDGET(compose->toolbar->privacy_sign_btn),
+                       can_sign);
+       }
+       if (compose->toolbar->privacy_encrypt_btn != NULL) {
+               gtk_widget_set_sensitive(
+                       GTK_WIDGET(compose->toolbar->privacy_encrypt_btn),
+                       can_encrypt);
+       }
+}
+
 static void compose_set_out_encoding(Compose *compose)
 {
        CharSet out_encoding;
@@ -8776,6 +8843,8 @@ static void compose_template_apply(Compose *compose, Template *tmpl,
        compose_template_apply_fields(compose, tmpl);
        compose_attach_from_list(compose, quote_fmt_get_attachments_list(), FALSE);
        quote_fmt_reset_vartable();
+       quote_fmtlex_destroy();
+
        compose_changed_cb(NULL, compose);
 
 #ifdef USE_ENCHANT
@@ -8792,7 +8861,7 @@ static void compose_template_apply_fields_error(const gchar *header)
        tr = g_strdup(C_("'%s' stands for a header name",
                                  "Template '%s' format error."));
        text = g_strdup_printf(tr, prefs_common_translated_header_name(header));
-       alertpanel_error(text);
+       alertpanel_error("%s", text);
 
        g_free(text);
        g_free(tr);
@@ -8829,6 +8898,9 @@ static void compose_template_apply_fields(Compose *compose, Template *tmpl)
                } else {
                        gtk_entry_set_text(GTK_ENTRY(compose->from_name), buf);
                }
+
+               quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
        }
 
        if (tmpl->to && *tmpl->to != '\0') {
@@ -8847,6 +8919,9 @@ static void compose_template_apply_fields(Compose *compose, Template *tmpl)
                } else {
                        compose_entry_append(compose, buf, COMPOSE_TO, PREF_TEMPLATE);
                }
+
+               quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
        }
 
        if (tmpl->cc && *tmpl->cc != '\0') {
@@ -8865,6 +8940,9 @@ static void compose_template_apply_fields(Compose *compose, Template *tmpl)
                } else {
                        compose_entry_append(compose, buf, COMPOSE_CC, PREF_TEMPLATE);
                }
+
+               quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
        }
 
        if (tmpl->bcc && *tmpl->bcc != '\0') {
@@ -8883,6 +8961,9 @@ static void compose_template_apply_fields(Compose *compose, Template *tmpl)
                } else {
                        compose_entry_append(compose, buf, COMPOSE_BCC, PREF_TEMPLATE);
                }
+
+               quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
        }
 
        if (tmpl->replyto && *tmpl->replyto != '\0') {
@@ -8901,6 +8982,9 @@ static void compose_template_apply_fields(Compose *compose, Template *tmpl)
                } else {
                        compose_entry_append(compose, buf, COMPOSE_REPLYTO, PREF_TEMPLATE);
                }
+
+               quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
        }
 
        /* process the subject */
@@ -8920,6 +9004,9 @@ static void compose_template_apply_fields(Compose *compose, Template *tmpl)
                } else {
                        gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf);
                }
+
+               quote_fmt_reset_vartable();
+               quote_fmtlex_destroy();
        }
 
        procmsg_msginfo_free( &dummyinfo );
@@ -11361,6 +11448,7 @@ static void compose_toggle_sign_cb(GtkToggleAction *action, gpointer data)
        Compose *compose = (Compose *)data;
 
        compose->use_signing = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
+       gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(compose->toolbar->privacy_sign_btn), compose->use_signing);
 }
 
 static void compose_toggle_encrypt_cb(GtkToggleAction *action, gpointer data)
@@ -11368,6 +11456,7 @@ static void compose_toggle_encrypt_cb(GtkToggleAction *action, gpointer data)
        Compose *compose = (Compose *)data;
 
        compose->use_encryption = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
+       gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(compose->toolbar->privacy_encrypt_btn), compose->use_encryption);
 }
 
 static void activate_privacy_system(Compose *compose, PrefsAccount *account, gboolean warn) 
@@ -11622,6 +11711,8 @@ static gboolean compose_headerentry_button_clicked_cb (GtkWidget *button,
                                         ComposeHeaderEntry *headerentry)
 {
        gtk_entry_set_text(GTK_ENTRY(headerentry->entry), "");
+       gtk_widget_modify_base(GTK_WIDGET(headerentry->entry), GTK_STATE_NORMAL, NULL);
+       gtk_widget_modify_text(GTK_WIDGET(headerentry->entry), GTK_STATE_NORMAL, NULL);
        return FALSE;
 }