Search in sources :

Example 11 with TextBody

use of com.fsck.k9.mail.internet.TextBody in project k-9 by k9mail.

the class MessageCryptoHelper method extractClearsignedTextReplacementPart.

private static MimeBodyPart extractClearsignedTextReplacementPart(Part part) {
    try {
        String clearsignedText = MessageExtractor.getTextFromPart(part);
        String replacementText = OpenPgpUtils.extractClearsignedMessage(clearsignedText);
        if (replacementText == null) {
            Timber.e("failed to extract clearsigned text for replacement part");
            return NO_REPLACEMENT_PART;
        }
        return new MimeBodyPart(new TextBody(replacementText), "text/plain");
    } catch (MessagingException e) {
        Timber.e(e, "failed to create clearsigned text replacement part");
        return NO_REPLACEMENT_PART;
    }
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MessagingException(com.fsck.k9.mail.MessagingException) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 12 with TextBody

use of com.fsck.k9.mail.internet.TextBody in project k-9 by k9mail.

the class MessageExtractor method getTextFromPart.

public static String getTextFromPart(Part part, long textSizeLimit) {
    try {
        if ((part != null) && (part.getBody() != null)) {
            final Body body = part.getBody();
            if (body instanceof TextBody) {
                return ((TextBody) body).getRawText();
            }
            final String mimeType = part.getMimeType();
            if (mimeType != null && MimeUtility.mimeTypeMatches(mimeType, "text/*") || part.isMimeType("application/pgp")) {
                return getTextFromTextPart(part, body, mimeType, textSizeLimit);
            } else {
                throw new MessagingException("Provided non-text part: " + mimeType);
            }
        } else {
            throw new MessagingException("Provided invalid part");
        }
    } catch (IOException e) {
        Log.e(LOG_TAG, "Unable to getTextFromPart", e);
    } catch (MessagingException e) {
        Log.e(LOG_TAG, "Unable to getTextFromPart", e);
    }
    return null;
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) IOException(java.io.IOException) Body(com.fsck.k9.mail.Body)

Example 13 with TextBody

use of com.fsck.k9.mail.internet.TextBody in project k-9 by k9mail.

the class Multipart method setCharset.

public void setCharset(String charset) throws MessagingException {
    if (mParts.isEmpty())
        return;
    BodyPart part = mParts.get(0);
    Body body = part.getBody();
    if (body instanceof TextBody) {
        CharsetSupport.setCharset(charset, part);
        ((TextBody) body).setCharset(charset);
    }
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) TextBody(com.fsck.k9.mail.internet.TextBody)

Example 14 with TextBody

use of com.fsck.k9.mail.internet.TextBody in project k-9 by k9mail.

the class TextBodyBuilder method buildTextPlain.

/**
     * Build the {@link Body} that will contain the text of the message.
     *
     * @return {@link TextBody} instance that contains the entered text and
     *         possibly the quoted original message.
     */
public TextBody buildTextPlain() {
    // The length of the formatted version of the user-supplied text/reply
    int composedMessageLength;
    // The offset of the user-supplied text/reply in the final text body
    int composedMessageOffset;
    // Get the user-supplied text
    String text = mMessageContent;
    // Capture composed message length before we start attaching quoted parts and signatures.
    composedMessageLength = text.length();
    composedMessageOffset = 0;
    // Do we have to modify an existing message to include our reply?
    if (mIncludeQuotedText) {
        String quotedText = getQuotedText();
        if (mAppendSignature) {
            // Append signature to the text/reply
            if (mReplyAfterQuote || mSignatureBeforeQuotedText) {
                text += getSignature();
            }
        }
        if (mReplyAfterQuote) {
            composedMessageOffset = quotedText.length() + "\r\n".length();
            text = quotedText + "\r\n" + text;
        } else {
            text += "\r\n\r\n" + quotedText;
        }
        if (mAppendSignature) {
            // Place signature immediately after the quoted text
            if (!(mReplyAfterQuote || mSignatureBeforeQuotedText)) {
                text += getSignature();
            }
        }
    } else {
        // There is no text to quote so simply append the signature if available
        if (mAppendSignature) {
            // Append signature to the text/reply
            text += getSignature();
        }
    }
    TextBody body = new TextBody(text);
    body.setComposedMessageLength(composedMessageLength);
    body.setComposedMessageOffset(composedMessageOffset);
    return body;
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody)

Example 15 with TextBody

use of com.fsck.k9.mail.internet.TextBody in project k-9 by k9mail.

the class TextBodyBuilder method buildTextHtml.

/**
     * Build the {@link Body} that will contain the text of the message.
     *
     * @return {@link com.fsck.k9.mail.internet.TextBody} instance that contains the entered text and
     *         possibly the quoted original message.
     */
public TextBody buildTextHtml() {
    // The length of the formatted version of the user-supplied text/reply
    int composedMessageLength;
    // The offset of the user-supplied text/reply in the final text body
    int composedMessageOffset;
    // Get the user-supplied text
    String text = mMessageContent;
    // Do we have to modify an existing message to include our reply?
    if (mIncludeQuotedText) {
        InsertableHtmlContent quotedHtmlContent = getQuotedTextHtml();
        if (K9.isDebug()) {
            Timber.d("insertable: %s", quotedHtmlContent.toDebugString());
        }
        if (mAppendSignature) {
            // Append signature to the reply
            if (mReplyAfterQuote || mSignatureBeforeQuotedText) {
                text += getSignature();
            }
        }
        // Convert the text to HTML
        text = textToHtmlFragment(text);
        /*
             * Set the insertion location based upon our reply after quote
             * setting. Additionally, add some extra separators between the
             * composed message and quoted message depending on the quote
             * location. We only add the extra separators when we're
             * sending, that way when we load a draft, we don't have to know
             * the length of the separators to remove them before editing.
             */
        if (mReplyAfterQuote) {
            quotedHtmlContent.setInsertionLocation(InsertableHtmlContent.InsertionLocation.AFTER_QUOTE);
            if (mInsertSeparator) {
                text = "<br clear=\"all\">" + text;
            }
        } else {
            quotedHtmlContent.setInsertionLocation(InsertableHtmlContent.InsertionLocation.BEFORE_QUOTE);
            if (mInsertSeparator) {
                text += "<br><br>";
            }
        }
        if (mAppendSignature) {
            // Place signature immediately after the quoted text
            if (!(mReplyAfterQuote || mSignatureBeforeQuotedText)) {
                quotedHtmlContent.insertIntoQuotedFooter(getSignatureHtml());
            }
        }
        quotedHtmlContent.setUserContent(text);
        // Save length of the body and its offset.  This is used when thawing drafts.
        composedMessageLength = text.length();
        composedMessageOffset = quotedHtmlContent.getInsertionPoint();
        text = quotedHtmlContent.toString();
    } else {
        // There is no text to quote so simply append the signature if available
        if (mAppendSignature) {
            text += getSignature();
        }
        // Convert the text to HTML
        text = textToHtmlFragment(text);
        //TODO: Wrap this in proper HTML tags
        composedMessageLength = text.length();
        composedMessageOffset = 0;
    }
    TextBody body = new TextBody(text);
    body.setComposedMessageLength(composedMessageLength);
    body.setComposedMessageOffset(composedMessageOffset);
    return body;
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) InsertableHtmlContent(com.fsck.k9.message.quote.InsertableHtmlContent)

Aggregations

TextBody (com.fsck.k9.mail.internet.TextBody)27 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)16 Test (org.junit.Test)15 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)13 Part (com.fsck.k9.mail.Part)7 ArrayList (java.util.ArrayList)7 Viewable (com.fsck.k9.mail.internet.Viewable)6 ViewableExtractedText (com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText)6 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)5 BodyPart (com.fsck.k9.mail.BodyPart)3 Message (com.fsck.k9.mail.Message)3 MessagingException (com.fsck.k9.mail.MessagingException)3 Address (com.fsck.k9.mail.Address)2 InsertableHtmlContent (com.fsck.k9.message.quote.InsertableHtmlContent)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Date (java.util.Date)2 Theory (org.junit.experimental.theories.Theory)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 Account (com.fsck.k9.Account)1