Search in sources :

Example 61 with Text

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

the class BodyTextExtractor method getBodyTextFromMessage.

/** Fetch the body text from a messagePart in the desired messagePart format. This method handles
     * conversions between formats (html to text and vice versa) if necessary.
     */
public static String getBodyTextFromMessage(Part messagePart, SimpleMessageFormat format) {
    Part part;
    if (format == SimpleMessageFormat.HTML) {
        // HTML takes precedence, then text.
        part = MimeUtility.findFirstPartByMimeType(messagePart, "text/html");
        if (part != null) {
            Timber.d("getBodyTextFromMessage: HTML requested, HTML found.");
            return MessageExtractor.getTextFromPart(part);
        }
        part = MimeUtility.findFirstPartByMimeType(messagePart, "text/plain");
        if (part != null) {
            Timber.d("getBodyTextFromMessage: HTML requested, text found.");
            String text = MessageExtractor.getTextFromPart(part);
            return HtmlConverter.textToHtml(text);
        }
    } else if (format == SimpleMessageFormat.TEXT) {
        // Text takes precedence, then html.
        part = MimeUtility.findFirstPartByMimeType(messagePart, "text/plain");
        if (part != null) {
            Timber.d("getBodyTextFromMessage: Text requested, text found.");
            return MessageExtractor.getTextFromPart(part);
        }
        part = MimeUtility.findFirstPartByMimeType(messagePart, "text/html");
        if (part != null) {
            Timber.d("getBodyTextFromMessage: Text requested, HTML found.");
            String text = MessageExtractor.getTextFromPart(part);
            return HtmlConverter.htmlToText(text);
        }
    }
    // If we had nothing interesting, return an empty string.
    return "";
}
Also used : Part(com.fsck.k9.mail.Part)

Example 62 with Text

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

the class MessageViewInfoExtractor method addMessageHeaderText.

/**
     * Extract important header values from a message to display inline (plain text version).
     *
     * @param text
     *         The {@link StringBuilder} that will receive the (plain text) output.
     * @param message
     *         The message to extract the header values from.
     *
     * @throws com.fsck.k9.mail.MessagingException
     *          In case of an error.
     */
private void addMessageHeaderText(StringBuilder text, Message message) throws MessagingException {
    // From: <sender>
    Address[] from = message.getFrom();
    if (from != null && from.length > 0) {
        text.append(context.getString(R.string.message_compose_quote_header_from));
        text.append(' ');
        text.append(Address.toString(from));
        text.append("\r\n");
    }
    // To: <recipients>
    Address[] to = message.getRecipients(Message.RecipientType.TO);
    if (to != null && to.length > 0) {
        text.append(context.getString(R.string.message_compose_quote_header_to));
        text.append(' ');
        text.append(Address.toString(to));
        text.append("\r\n");
    }
    // Cc: <recipients>
    Address[] cc = message.getRecipients(Message.RecipientType.CC);
    if (cc != null && cc.length > 0) {
        text.append(context.getString(R.string.message_compose_quote_header_cc));
        text.append(' ');
        text.append(Address.toString(cc));
        text.append("\r\n");
    }
    // Date: <date>
    Date date = message.getSentDate();
    if (date != null) {
        text.append(context.getString(R.string.message_compose_quote_header_send_date));
        text.append(' ');
        text.append(date.toString());
        text.append("\r\n");
    }
    // Subject: <subject>
    String subject = message.getSubject();
    text.append(context.getString(R.string.message_compose_quote_header_subject));
    text.append(' ');
    if (subject == null) {
        text.append(context.getString(R.string.general_no_subject));
    } else {
        text.append(subject);
    }
    text.append("\r\n\r\n");
}
Also used : Address(com.fsck.k9.mail.Address) Date(java.util.Date)

Example 63 with Text

use of com.fsck.k9.mail.internet.Viewable.Text 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)

Example 64 with Text

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

the class MessageFulltextCreator method extractText.

private String extractText(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 65 with Text

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

the class TextPartFinder method findFirstTextPart.

@Nullable
public Part findFirstTextPart(@NonNull Part part) {
    String mimeType = part.getMimeType();
    Body body = part.getBody();
    if (body instanceof Multipart) {
        Multipart multipart = (Multipart) body;
        if (isSameMimeType(mimeType, "multipart/alternative")) {
            return findTextPartInMultipartAlternative(multipart);
        } else {
            return findTextPartInMultipart(multipart);
        }
    } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) {
        return part;
    }
    return null;
}
Also used : Multipart(com.fsck.k9.mail.Multipart) Body(com.fsck.k9.mail.Body) Nullable(android.support.annotation.Nullable)

Aggregations

Test (org.junit.Test)74 Part (com.fsck.k9.mail.Part)64 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)37 BodyPart (com.fsck.k9.mail.BodyPart)34 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)33 MessageCreationHelper.createTextPart (com.fsck.k9.message.MessageCreationHelper.createTextPart)27 Message (com.fsck.k9.mail.Message)22 TextBody (com.fsck.k9.mail.internet.TextBody)18 MessageCreationHelper.createEmptyPart (com.fsck.k9.message.MessageCreationHelper.createEmptyPart)16 ArrayList (java.util.ArrayList)16 Body (com.fsck.k9.mail.Body)13 MessagingException (com.fsck.k9.mail.MessagingException)13 MessageCreationHelper.createPart (com.fsck.k9.message.MessageCreationHelper.createPart)13 Multipart (com.fsck.k9.mail.Multipart)10 Viewable (com.fsck.k9.mail.internet.Viewable)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)8 Intent (android.content.Intent)6 BinaryMemoryBody (com.fsck.k9.mailstore.BinaryMemoryBody)6 ViewableExtractedText (com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText)6