Search in sources :

Example 6 with Html

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

the class MessageExtractorTest method getTextFromPart_withHtmlWithCharsetInHtmlRawDataBody_shouldReturnHtmlText.

@Test
public void getTextFromPart_withHtmlWithCharsetInHtmlRawDataBody_shouldReturnHtmlText() throws Exception {
    String bodyText = "<html><head>" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + "</head><body>Sample text body</body></html>";
    BinaryMemoryBody body = new BinaryMemoryBody(bodyText.getBytes(), MimeUtil.ENC_8BIT);
    part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html");
    part.setBody(body);
    String result = MessageExtractor.getTextFromPart(part);
    assertNotNull(result);
    assertEquals(bodyText, result);
}
Also used : BinaryMemoryBody(com.fsck.k9.mailstore.BinaryMemoryBody) Test(org.junit.Test)

Example 7 with Html

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

the class MessageViewInfoExtractor method buildText.

private StringBuilder buildText(Viewable viewable, boolean prependDivider) {
    StringBuilder text = new StringBuilder();
    if (viewable instanceof Textual) {
        Part part = ((Textual) viewable).getPart();
        addTextDivider(text, part, prependDivider);
        String t = getTextFromPart(part);
        if (t == null) {
            t = "";
        } else if (viewable instanceof Html) {
            t = HtmlConverter.htmlToText(t);
        } else if (!(viewable instanceof Text)) {
            throw new IllegalStateException("unhandled case!");
        }
        text.append(t);
    } else if (viewable instanceof Alternative) {
        // That's odd - an Alternative as child of an Alternative; go ahead and try to use the
        // text/plain child; fall-back to the text/html part.
        Alternative alternative = (Alternative) viewable;
        List<Viewable> textAlternative = alternative.getText().isEmpty() ? alternative.getHtml() : alternative.getText();
        boolean divider = prependDivider;
        for (Viewable textViewable : textAlternative) {
            text.append(buildText(textViewable, divider));
            divider = true;
        }
    }
    return text;
}
Also used : Alternative(com.fsck.k9.mail.internet.Viewable.Alternative) Textual(com.fsck.k9.mail.internet.Viewable.Textual) Part(com.fsck.k9.mail.Part) Viewable(com.fsck.k9.mail.internet.Viewable) Html(com.fsck.k9.mail.internet.Viewable.Html) Text(com.fsck.k9.mail.internet.Viewable.Text) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with Html

use of com.fsck.k9.mail.internet.Viewable.Html 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) {
    TextBodyBuilder textBodyBuilder = new TextBodyBuilder(text);
    /*
         * 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)

Example 9 with Html

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

the class MessageFulltextCreator method createFulltext.

public String createFulltext(@NonNull Message message) {
    Part textPart = textPartFinder.findFirstTextPart(message);
    if (textPart == null || hasEmptyBody(textPart)) {
        return null;
    }
    String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_FTS);
    String mimeType = textPart.getMimeType();
    if (!MimeUtility.isSameMimeType(mimeType, "text/html")) {
        return text;
    }
    return HtmlConverter.htmlToText(text);
}
Also used : Part(com.fsck.k9.mail.Part)

Example 10 with Html

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

the class TextPartFinder method findTextPartInMultipart.

private Part findTextPartInMultipart(Multipart multipart) {
    for (BodyPart bodyPart : multipart.getBodyParts()) {
        String mimeType = bodyPart.getMimeType();
        Body body = bodyPart.getBody();
        if (body instanceof Multipart) {
            Part candidatePart = findFirstTextPart(bodyPart);
            if (candidatePart != null) {
                return candidatePart;
            }
        } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) {
            return bodyPart;
        }
    }
    return null;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) Multipart(com.fsck.k9.mail.Multipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) Body(com.fsck.k9.mail.Body)

Aggregations

Part (com.fsck.k9.mail.Part)31 BodyPart (com.fsck.k9.mail.BodyPart)24 Test (org.junit.Test)24 MessageCreationHelper.createTextPart (com.fsck.k9.message.MessageCreationHelper.createTextPart)12 MessageCreationHelper.createEmptyPart (com.fsck.k9.message.MessageCreationHelper.createEmptyPart)11 MessageCreationHelper.createPart (com.fsck.k9.message.MessageCreationHelper.createPart)11 ArrayList (java.util.ArrayList)11 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)9 Body (com.fsck.k9.mail.Body)7 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)7 Message (com.fsck.k9.mail.Message)6 Multipart (com.fsck.k9.mail.Multipart)6 TextBody (com.fsck.k9.mail.internet.TextBody)6 Viewable (com.fsck.k9.mail.internet.Viewable)6 Alternative (com.fsck.k9.mail.internet.Viewable.Alternative)4 Html (com.fsck.k9.mail.internet.Viewable.Html)4 Text (com.fsck.k9.mail.internet.Viewable.Text)4 MessageFormat (com.fsck.k9.Account.MessageFormat)3 K9RobolectricTest (com.fsck.k9.K9RobolectricTest)3 MessagingException (com.fsck.k9.mail.MessagingException)3