Search in sources :

Example 26 with Html

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

the class MigrationTo51 method insertTextualPartIntoDatabase.

private static MimeStructureState insertTextualPartIntoDatabase(SQLiteDatabase db, MimeStructureState structureState, MimeHeader mimeHeader, String content, boolean isHtml) throws IOException {
    if (mimeHeader == null) {
        mimeHeader = new MimeHeader();
    }
    mimeHeader.setHeader(MimeHeader.HEADER_CONTENT_TYPE, isHtml ? "text/html; charset=\"utf-8\"" : "text/plain; charset=\"utf-8\"");
    mimeHeader.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, MimeUtil.ENC_QUOTED_PRINTABLE);
    byte[] contentBytes;
    int decodedBodySize;
    int dataLocation;
    if (content != null) {
        ByteArrayOutputStream contentOutputStream = new ByteArrayOutputStream();
        QuotedPrintableOutputStream quotedPrintableOutputStream = new QuotedPrintableOutputStream(contentOutputStream, false);
        quotedPrintableOutputStream.write(content.getBytes());
        quotedPrintableOutputStream.flush();
        dataLocation = DATA_LOCATION__IN_DATABASE;
        contentBytes = contentOutputStream.toByteArray();
        decodedBodySize = content.length();
    } else {
        dataLocation = DATA_LOCATION__MISSING;
        contentBytes = null;
        decodedBodySize = 0;
    }
    ContentValues cv = new ContentValues();
    cv.put("type", MESSAGE_PART_TYPE__UNKNOWN);
    cv.put("data_location", dataLocation);
    cv.put("mime_type", isHtml ? "text/html" : "text/plain");
    cv.put("header", mimeHeader.toString());
    cv.put("data", contentBytes);
    cv.put("decoded_body_size", decodedBodySize);
    cv.put("encoding", MimeUtil.ENC_QUOTED_PRINTABLE);
    cv.put("charset", "utf-8");
    structureState.applyValues(cv);
    long partId = db.insertOrThrow("message_parts", null, cv);
    return structureState.nextChild(partId);
}
Also used : ContentValues(android.content.ContentValues) QuotedPrintableOutputStream(org.apache.james.mime4j.codec.QuotedPrintableOutputStream) MimeHeader(com.fsck.k9.mail.internet.MimeHeader) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 27 with Html

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

the class TextBodyBuilderTest method testBuildTextHtml.

@Theory
public void testBuildTextHtml(boolean includeQuotedText, QuoteStyle quoteStyle, boolean isReplyAfterQuote, boolean isSignatureUse, boolean isSignatureBeforeQuotedText, boolean isDraft) {
    String expectedText;
    int expectedMessageLength;
    int expectedMessagePosition = 0;
    String expectedHtmlContent;
    String expectedPrefix = "";
    if (includeQuotedText && quoteStyle == QuoteStyle.PREFIX && isReplyAfterQuote && !isDraft) {
        expectedPrefix = "<br clear=\"all\">";
    }
    String expectedPostfix = "";
    if (!isDraft && includeQuotedText) {
        expectedPostfix = "<br><br>";
    }
    // 3.signature
    if (quoteStyle == QuoteStyle.PREFIX && isReplyAfterQuote) {
        expectedText = expectedPrefix + "<html>message content";
        if (!isDraft && isSignatureUse) {
            expectedText += "\r\n" + "signature";
        }
        expectedText += "</html>";
        expectedMessageLength = expectedText.length();
        String quotedContent = "quoted text";
        if (isDraft || includeQuotedText) {
            expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent, 0, false, expectedText, expectedText + quotedContent);
            expectedText += quotedContent;
        } else {
            expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent, 0, true, "", quotedContent);
        // expectedText += quotedContent;
        }
    } else // 3.quoted text
    if (isSignatureBeforeQuotedText) {
        expectedText = expectedPrefix + "<html>message content";
        if (!isDraft && isSignatureUse) {
            expectedText += "\r\n" + "signature";
        }
        expectedText += "</html>";
        expectedText += expectedPostfix;
        expectedMessageLength = expectedText.length();
        String quotedContent = "quoted text";
        if (isDraft || includeQuotedText) {
            expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent, 0, true, expectedText, expectedText + quotedContent);
            expectedText += quotedContent;
        } else {
            expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent, 0, true, "", quotedContent);
        // expectedText += quotedContent;
        }
    } else // 1.message content
    // 2.quoted text
    // 3.signature
    {
        String expectedSignature = "";
        expectedText = expectedPrefix + "<html>message content";
        if (!isDraft && isSignatureUse) {
            if (!includeQuotedText) {
                expectedText += "\r\n" + "signature";
            } else {
                expectedSignature = "<html>\r\nsignature</html>";
            }
        }
        expectedText += "</html>";
        expectedText += expectedPostfix;
        expectedMessageLength = expectedText.length();
        String quotedContent = "quoted text";
        if (isDraft || includeQuotedText) {
            expectedHtmlContent = makeExpectedHtmlContent(expectedText, expectedSignature + quotedContent, expectedSignature.length(), true, expectedText, expectedText + expectedSignature + quotedContent);
            expectedText += expectedSignature + quotedContent;
        } else {
            expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent, 0, true, "", quotedContent);
        // expectedText += quotedContent;
        }
    }
    InsertableHtmlContent insertableHtmlContent = new InsertableHtmlContent();
    String quotedText = "quoted text";
    insertableHtmlContent.setQuotedContent(new StringBuilder(quotedText));
    String messageText = "message content";
    String signatureText = "signature";
    TestingTextBodyBuilder textBodyBuilder = new TestingTextBodyBuilder(includeQuotedText, isDraft, quoteStyle, isReplyAfterQuote, isSignatureBeforeQuotedText, isSignatureUse, messageText, signatureText);
    textBodyBuilder.setQuotedTextHtml(insertableHtmlContent);
    TextBody textBody = textBodyBuilder.buildTextHtml();
    assertThat(textBody, instanceOf(TextBody.class));
    assertThat(textBody.getRawText(), is(expectedText));
    assertThat(textBody.getComposedMessageLength(), is(expectedMessageLength));
    assertThat(textBody.getComposedMessageOffset(), is(expectedMessagePosition));
    assertThat(insertableHtmlContent.toDebugString(), is(expectedHtmlContent));
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) InsertableHtmlContent(com.fsck.k9.message.quote.InsertableHtmlContent) Theory(org.junit.experimental.theories.Theory)

Example 28 with Html

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

the class PreviewTextExtractorTest method extractPreview_withSimpleTextHtml.

@Test
public void extractPreview_withSimpleTextHtml() throws Exception {
    String text = "<b>The quick brown fox jumps over the lazy dog</b>";
    Part part = createTextPart("text/html", text);
    String preview = previewTextExtractor.extractPreview(part);
    assertEquals("The quick brown fox jumps over the lazy dog", preview);
}
Also used : Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) MessageCreationHelper.createTextPart(com.fsck.k9.message.MessageCreationHelper.createTextPart) Test(org.junit.Test)

Example 29 with Html

use of com.fsck.k9.mail.internet.Viewable.Html in project sms-backup-plus by jberkel.

the class Attachment method createPart.

private static MimeBodyPart createPart(Body body, final String filename, final String contentType) throws MessagingException {
    MimeBodyPart part = new MimeBodyPart(body, contentType);
    String contentTypeHeader = TextUtils.isEmpty(contentType) ? "application/octet-stream" : contentType;
    String disposition = "attachment";
    if (!TextUtils.isEmpty(filename)) {
        // should set both name and filename parameters
        // http://www.imc.org/ietf-smtp/mail-archive/msg05023.html
        disposition += encodeRFC2231("filename", filename);
        contentTypeHeader += encodeRFC2231("name", filename);
    }
    part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentTypeHeader);
    part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, disposition);
    part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64");
    return part;
}
Also used : MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 30 with Html

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

the class MessageViewInfoExtractor method extractTextFromViewables.

/**
 * Extract the viewable textual parts of a message and return the rest as attachments.
 *
 * @return A {@link ViewableExtractedText} instance containing the textual parts of the message as
 *         plain text and HTML, and a list of message parts considered attachments.
 *
 * @throws com.fsck.k9.mail.MessagingException
 *          In case of an error.
 */
@VisibleForTesting
ViewableExtractedText extractTextFromViewables(List<Viewable> viewables) throws MessagingException {
    try {
        // Collect all viewable parts
        /*
             * Convert the tree of viewable parts into text and HTML
             */
        // Used to suppress the divider for the first viewable part
        boolean hideDivider = true;
        StringBuilder text = new StringBuilder();
        StringBuilder html = new StringBuilder();
        for (Viewable viewable : viewables) {
            if (viewable instanceof Textual) {
                // This is either a text/plain or text/html part. Fill the variables 'text' and
                // 'html', converting between plain text and HTML as necessary.
                text.append(buildText(viewable, !hideDivider));
                html.append(buildHtml(viewable, !hideDivider));
                hideDivider = false;
            } else if (viewable instanceof MessageHeader) {
                MessageHeader header = (MessageHeader) viewable;
                Part containerPart = header.getContainerPart();
                Message innerMessage = header.getMessage();
                addTextDivider(text, containerPart, !hideDivider);
                addMessageHeaderText(text, innerMessage);
                addHtmlDivider(html, containerPart, !hideDivider);
                addMessageHeaderHtml(html, innerMessage);
                hideDivider = true;
            } else if (viewable instanceof Alternative) {
                // Handle multipart/alternative contents
                Alternative alternative = (Alternative) viewable;
                /*
                     * We made sure at least one of text/plain or text/html is present when
                     * creating the Alternative object. If one part is not present we convert the
                     * other one to make sure 'text' and 'html' always contain the same text.
                     */
                List<Viewable> textAlternative = alternative.getText().isEmpty() ? alternative.getHtml() : alternative.getText();
                List<Viewable> htmlAlternative = alternative.getHtml().isEmpty() ? alternative.getText() : alternative.getHtml();
                // Fill the 'text' variable
                boolean divider = !hideDivider;
                for (Viewable textViewable : textAlternative) {
                    text.append(buildText(textViewable, divider));
                    divider = true;
                }
                // Fill the 'html' variable
                divider = !hideDivider;
                for (Viewable htmlViewable : htmlAlternative) {
                    html.append(buildHtml(htmlViewable, divider));
                    divider = true;
                }
                hideDivider = false;
            }
        }
        String sanitizedHtml = htmlProcessor.processForDisplay(html.toString());
        return new ViewableExtractedText(text.toString(), sanitizedHtml);
    } catch (Exception e) {
        throw new MessagingException("Couldn't extract viewable parts", e);
    }
}
Also used : Alternative(com.fsck.k9.mail.internet.Viewable.Alternative) Message(com.fsck.k9.mail.Message) Textual(com.fsck.k9.mail.internet.Viewable.Textual) MessagingException(com.fsck.k9.mail.MessagingException) MessagingException(com.fsck.k9.mail.MessagingException) Part(com.fsck.k9.mail.Part) Viewable(com.fsck.k9.mail.internet.Viewable) MessageHeader(com.fsck.k9.mail.internet.Viewable.MessageHeader) VisibleForTesting(androidx.annotation.VisibleForTesting)

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