Search in sources :

Example 11 with Alternative

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

the class TextPartFinderTest method findFirstTextPart_withMultipartAlternativeContainingMultipartRelatedContainingTextHtmlFirst.

@Test
public void findFirstTextPart_withMultipartAlternativeContainingMultipartRelatedContainingTextHtmlFirst() throws Exception {
    BodyPart expected = createTextPart("text/plain");
    Part part = createMultipart("multipart/alternative", createMultipart("multipart/related", createTextPart("text/html"), createPart("image/jpeg")), expected);
    Part result = textPartFinder.findFirstTextPart(part);
    assertEquals(expected, result);
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) BodyPart(com.fsck.k9.mail.BodyPart) MessageCreationHelper.createEmptyPart(com.fsck.k9.message.MessageCreationHelper.createEmptyPart) Part(com.fsck.k9.mail.Part) MessageCreationHelper.createPart(com.fsck.k9.message.MessageCreationHelper.createPart) MessageCreationHelper.createTextPart(com.fsck.k9.message.MessageCreationHelper.createTextPart) Test(org.junit.Test)

Example 12 with Alternative

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

the class TextPartFinderTest method findFirstTextPart_withMultipartMixedContainingMultipartAlternative.

@Test
public void findFirstTextPart_withMultipartMixedContainingMultipartAlternative() throws Exception {
    BodyPart expected = createTextPart("text/plain");
    Part part = createMultipart("multipart/mixed", createPart("image/jpeg"), createMultipart("multipart/alternative", expected, createTextPart("text/html")), createTextPart("text/plain"));
    Part result = textPartFinder.findFirstTextPart(part);
    assertEquals(expected, result);
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) BodyPart(com.fsck.k9.mail.BodyPart) MessageCreationHelper.createEmptyPart(com.fsck.k9.message.MessageCreationHelper.createEmptyPart) Part(com.fsck.k9.mail.Part) MessageCreationHelper.createPart(com.fsck.k9.message.MessageCreationHelper.createPart) MessageCreationHelper.createTextPart(com.fsck.k9.message.MessageCreationHelper.createTextPart) Test(org.junit.Test)

Example 13 with Alternative

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

the class TextPartFinderTest method findFirstTextPart_withMultipartAlternativeHtmlPartFirst.

@Test
public void findFirstTextPart_withMultipartAlternativeHtmlPartFirst() throws Exception {
    BodyPart expected = createTextPart("text/plain");
    Part part = createMultipart("multipart/alternative", createTextPart("text/html"), expected);
    Part result = textPartFinder.findFirstTextPart(part);
    assertEquals(expected, result);
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) BodyPart(com.fsck.k9.mail.BodyPart) MessageCreationHelper.createEmptyPart(com.fsck.k9.message.MessageCreationHelper.createEmptyPart) Part(com.fsck.k9.mail.Part) MessageCreationHelper.createPart(com.fsck.k9.message.MessageCreationHelper.createPart) MessageCreationHelper.createTextPart(com.fsck.k9.message.MessageCreationHelper.createTextPart) Test(org.junit.Test)

Example 14 with Alternative

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

the class MigrationTo51 method insertBodyAsMultipartAlternative.

private static MimeStructureState insertBodyAsMultipartAlternative(SQLiteDatabase db, MimeStructureState structureState, MimeHeader mimeHeader, String textContent, String htmlContent) throws IOException {
    if (mimeHeader == null) {
        mimeHeader = new MimeHeader();
    }
    String boundary = MimeUtility.getHeaderParameter(mimeHeader.getFirstHeader(MimeHeader.HEADER_CONTENT_TYPE), "boundary");
    if (TextUtils.isEmpty(boundary)) {
        boundary = MimeUtil.createUniqueBoundary();
    }
    mimeHeader.setHeader(MimeHeader.HEADER_CONTENT_TYPE, String.format("multipart/alternative; boundary=\"%s\";", boundary));
    int dataLocation = textContent != null || htmlContent != null ? DATA_LOCATION__IN_DATABASE : DATA_LOCATION__MISSING;
    ContentValues cv = new ContentValues();
    cv.put("type", MESSAGE_PART_TYPE__UNKNOWN);
    cv.put("data_location", dataLocation);
    cv.put("mime_type", "multipart/alternative");
    cv.put("header", mimeHeader.toString());
    cv.put("boundary", boundary);
    structureState.applyValues(cv);
    long multipartAlternativePartId = db.insertOrThrow("message_parts", null, cv);
    structureState = structureState.nextMultipartChild(multipartAlternativePartId);
    if (textContent != null) {
        structureState = insertTextualPartIntoDatabase(db, structureState, null, textContent, false);
    }
    if (htmlContent != null) {
        structureState = insertTextualPartIntoDatabase(db, structureState, null, htmlContent, true);
    }
    return structureState;
}
Also used : ContentValues(android.content.ContentValues) MimeHeader(com.fsck.k9.mail.internet.MimeHeader)

Example 15 with Alternative

use of com.fsck.k9.mail.internet.Viewable.Alternative 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 = MessageExtractor.getTextFromPart(part);
        if (t == null) {
            t = "";
        } else if (viewable instanceof Html) {
            t = HtmlConverter.htmlToText(t);
        } else if (viewable instanceof Flowed) {
            t = FlowedMessageUtils.deflow(t, false);
        } 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) Flowed(com.fsck.k9.mail.internet.Viewable.Flowed) 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)

Aggregations

Part (com.fsck.k9.mail.Part)20 BodyPart (com.fsck.k9.mail.BodyPart)18 Test (org.junit.Test)18 MessageCreationHelper.createEmptyPart (com.fsck.k9.message.MessageCreationHelper.createEmptyPart)9 MessageCreationHelper.createPart (com.fsck.k9.message.MessageCreationHelper.createPart)9 MessageCreationHelper.createTextPart (com.fsck.k9.message.MessageCreationHelper.createTextPart)9 ArrayList (java.util.ArrayList)9 Multipart (com.fsck.k9.mail.Multipart)8 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)7 Message (com.fsck.k9.mail.Message)6 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)6 Body (com.fsck.k9.mail.Body)5 Viewable (com.fsck.k9.mail.internet.Viewable)4 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 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 FetchProfile (com.fsck.k9.mail.FetchProfile)3 Flowed (com.fsck.k9.mail.internet.Viewable.Flowed)3 MessageHeader (com.fsck.k9.mail.internet.Viewable.MessageHeader)3