Search in sources :

Example 1 with QuotedTextMode

use of com.fsck.k9.message.QuotedTextMode in project k-9 by k9mail.

the class QuotedMessagePresenter method processDraftMessage.

public void processDraftMessage(MessageViewInfo messageViewInfo, Map<IdentityField, String> k9identity) {
    quoteStyle = k9identity.get(IdentityField.QUOTE_STYLE) != null ? QuoteStyle.valueOf(k9identity.get(IdentityField.QUOTE_STYLE)) : account.getQuoteStyle();
    int cursorPosition = 0;
    if (k9identity.containsKey(IdentityField.CURSOR_POSITION)) {
        try {
            cursorPosition = Integer.parseInt(k9identity.get(IdentityField.CURSOR_POSITION));
        } catch (Exception e) {
            Timber.e(e, "Could not parse cursor position for MessageCompose; continuing.");
        }
    }
    String showQuotedTextMode;
    if (k9identity.containsKey(IdentityField.QUOTED_TEXT_MODE)) {
        showQuotedTextMode = k9identity.get(IdentityField.QUOTED_TEXT_MODE);
    } else {
        showQuotedTextMode = "NONE";
    }
    int bodyLength = k9identity.get(IdentityField.LENGTH) != null ? Integer.valueOf(k9identity.get(IdentityField.LENGTH)) : UNKNOWN_LENGTH;
    int bodyOffset = k9identity.get(IdentityField.OFFSET) != null ? Integer.valueOf(k9identity.get(IdentityField.OFFSET)) : UNKNOWN_LENGTH;
    Integer bodyFooterOffset = k9identity.get(IdentityField.FOOTER_OFFSET) != null ? Integer.valueOf(k9identity.get(IdentityField.FOOTER_OFFSET)) : null;
    Integer bodyPlainLength = k9identity.get(IdentityField.PLAIN_LENGTH) != null ? Integer.valueOf(k9identity.get(IdentityField.PLAIN_LENGTH)) : null;
    Integer bodyPlainOffset = k9identity.get(IdentityField.PLAIN_OFFSET) != null ? Integer.valueOf(k9identity.get(IdentityField.PLAIN_OFFSET)) : null;
    QuotedTextMode quotedMode;
    try {
        quotedMode = QuotedTextMode.valueOf(showQuotedTextMode);
    } catch (Exception e) {
        quotedMode = QuotedTextMode.NONE;
    }
    // Always respect the user's current composition format preference, even if the
    // draft was saved in a different format.
    // TODO - The current implementation doesn't allow a user in HTML mode to edit a draft that wasn't saved with K9mail.
    String messageFormatString = k9identity.get(IdentityField.MESSAGE_FORMAT);
    MessageFormat messageFormat = null;
    if (messageFormatString != null) {
        try {
            messageFormat = MessageFormat.valueOf(messageFormatString);
        } catch (Exception e) {
        /* do nothing */
        }
    }
    if (messageFormat == null) {
        // This message probably wasn't created by us. The exception is legacy
        // drafts created before the advent of HTML composition. In those cases,
        // we'll display the whole message (including the quoted part) in the
        // composition window. If that's the case, try and convert it to text to
        // match the behavior in text mode.
        view.setMessageContentCharacters(BodyTextExtractor.getBodyTextFromMessage(messageViewInfo.message, SimpleMessageFormat.TEXT));
        forcePlainText = true;
        showOrHideQuotedText(quotedMode);
        return;
    }
    if (messageFormat == MessageFormat.HTML) {
        Part part = MimeUtility.findFirstPartByMimeType(messageViewInfo.message, "text/html");
        if (part != null) {
            // Shouldn't happen if we were the one who saved it.
            quotedTextFormat = SimpleMessageFormat.HTML;
            String text = MessageExtractor.getTextFromPart(part);
            Timber.d("Loading message with offset %d, length %d. Text length is %d.", bodyOffset, bodyLength, text.length());
            if (bodyOffset + bodyLength > text.length()) {
                // The draft was edited outside of K-9 Mail?
                Timber.d("The identity field from the draft contains an invalid LENGTH/OFFSET");
                bodyOffset = 0;
                bodyLength = 0;
            }
            // Grab our reply text.
            String bodyText = text.substring(bodyOffset, bodyOffset + bodyLength);
            view.setMessageContentCharacters(HtmlConverter.htmlToText(bodyText));
            // Regenerate the quoted html without our user content in it.
            StringBuilder quotedHTML = new StringBuilder();
            // stuff before the reply
            quotedHTML.append(text.substring(0, bodyOffset));
            quotedHTML.append(text.substring(bodyOffset + bodyLength));
            if (quotedHTML.length() > 0) {
                quotedHtmlContent = new InsertableHtmlContent();
                quotedHtmlContent.setQuotedContent(quotedHTML);
                // We don't know if bodyOffset refers to the header or to the footer
                quotedHtmlContent.setHeaderInsertionPoint(bodyOffset);
                if (bodyFooterOffset != null) {
                    quotedHtmlContent.setFooterInsertionPoint(bodyFooterOffset);
                } else {
                    quotedHtmlContent.setFooterInsertionPoint(bodyOffset);
                }
                // TODO replace with MessageViewInfo data
                view.setQuotedHtml(quotedHtmlContent.getQuotedContent(), AttachmentResolver.createFromPart(messageViewInfo.rootPart));
            }
        }
        if (bodyPlainOffset != null && bodyPlainLength != null) {
            processSourceMessageText(messageViewInfo.rootPart, bodyPlainOffset, bodyPlainLength, false);
        }
    } else if (messageFormat == MessageFormat.TEXT) {
        quotedTextFormat = SimpleMessageFormat.TEXT;
        processSourceMessageText(messageViewInfo.rootPart, bodyOffset, bodyLength, true);
    } else {
        Timber.e("Unhandled message format.");
    }
    // Set the cursor position if we have it.
    try {
        view.setMessageContentCursorPosition(cursorPosition);
    } catch (Exception e) {
        Timber.e(e, "Could not set cursor position in MessageCompose; ignoring.");
    }
    showOrHideQuotedText(quotedMode);
}
Also used : SimpleMessageFormat(com.fsck.k9.message.SimpleMessageFormat) MessageFormat(com.fsck.k9.Account.MessageFormat) Part(com.fsck.k9.mail.Part) QuotedTextMode(com.fsck.k9.message.QuotedTextMode) InsertableHtmlContent(com.fsck.k9.message.quote.InsertableHtmlContent) MessagingException(com.fsck.k9.mail.MessagingException)

Example 2 with QuotedTextMode

use of com.fsck.k9.message.QuotedTextMode in project k-9 by k9mail.

the class MessageBuilder method buildText.

/**
     * Build the {@link Body} that will contain the text of the message.
     *
     * <p>
     * Draft messages are treated somewhat differently in that signatures are not appended and HTML
     * separators between composed text and quoted text are not added.
     * </p>
     *
     * @param isDraft
     *         If {@code true} we build a message that will be saved as a draft (as opposed to
     *         sent).
     * @param simpleMessageFormat
     *         Specifies what type of message to build ({@code text/plain} vs. {@code text/html}).
     *
     * @return {@link TextBody} instance that contains the entered text and possibly the quoted
     *         original message.
     */
private TextBody buildText(boolean isDraft, SimpleMessageFormat simpleMessageFormat) {
    String messageText = text;
    TextBodyBuilder textBodyBuilder = new TextBodyBuilder(messageText);
    /*
         * Find out if we need to include the original message as quoted text.
         *
         * We include the quoted text in the body if the user didn't choose to
         * hide it. We always include the quoted text when we're saving a draft.
         * That's so the user is able to "un-hide" the quoted text if (s)he
         * opens a saved draft.
         */
    boolean includeQuotedText = (isDraft || quotedTextMode == QuotedTextMode.SHOW);
    boolean isReplyAfterQuote = (quoteStyle == QuoteStyle.PREFIX && this.isReplyAfterQuote);
    textBodyBuilder.setIncludeQuotedText(false);
    if (includeQuotedText) {
        if (simpleMessageFormat == SimpleMessageFormat.HTML && quotedHtmlContent != null) {
            textBodyBuilder.setIncludeQuotedText(true);
            textBodyBuilder.setQuotedTextHtml(quotedHtmlContent);
            textBodyBuilder.setReplyAfterQuote(isReplyAfterQuote);
        }
        if (simpleMessageFormat == SimpleMessageFormat.TEXT && quotedText.length() > 0) {
            textBodyBuilder.setIncludeQuotedText(true);
            textBodyBuilder.setQuotedText(quotedText);
            textBodyBuilder.setReplyAfterQuote(isReplyAfterQuote);
        }
    }
    textBodyBuilder.setInsertSeparator(!isDraft);
    boolean useSignature = (!isDraft && identity.getSignatureUse());
    if (useSignature) {
        textBodyBuilder.setAppendSignature(true);
        textBodyBuilder.setSignature(signature);
        textBodyBuilder.setSignatureBeforeQuotedText(isSignatureBeforeQuotedText);
    } else {
        textBodyBuilder.setAppendSignature(false);
    }
    TextBody body;
    if (simpleMessageFormat == SimpleMessageFormat.HTML) {
        body = textBodyBuilder.buildTextHtml();
    } else {
        body = textBodyBuilder.buildTextPlain();
    }
    return body;
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody)

Aggregations

MessageFormat (com.fsck.k9.Account.MessageFormat)1 MessagingException (com.fsck.k9.mail.MessagingException)1 Part (com.fsck.k9.mail.Part)1 TextBody (com.fsck.k9.mail.internet.TextBody)1 QuotedTextMode (com.fsck.k9.message.QuotedTextMode)1 SimpleMessageFormat (com.fsck.k9.message.SimpleMessageFormat)1 InsertableHtmlContent (com.fsck.k9.message.quote.InsertableHtmlContent)1