Search in sources :

Example 31 with Html

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

the class MessageViewInfoExtractor method buildHtml.

/**
 * Use the contents of a {@link com.fsck.k9.mail.internet.Viewable} to create the HTML to be displayed.
 *
 * <p>
 * This will use {@link HtmlConverter#textToHtml(String)} to convert plain text parts
 * to HTML if necessary.
 * </p>
 *
 * @param viewable
 *         The viewable part to build the HTML from.
 * @param prependDivider
 *         {@code true}, if the HTML divider should be inserted as first element.
 *         {@code false}, otherwise.
 *
 * @return The contents of the supplied viewable instance as HTML.
 */
private StringBuilder buildHtml(Viewable viewable, boolean prependDivider) {
    StringBuilder html = new StringBuilder();
    if (viewable instanceof Textual) {
        Part part = ((Textual) viewable).getPart();
        addHtmlDivider(html, part, prependDivider);
        String t = getTextFromPart(part);
        if (t == null) {
            t = "";
        } else if (viewable instanceof Text) {
            t = HtmlConverter.textToHtml(t);
        } else if (!(viewable instanceof Html)) {
            throw new IllegalStateException("unhandled case!");
        }
        html.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/html child; fall-back to the text/plain part.
        Alternative alternative = (Alternative) viewable;
        List<Viewable> htmlAlternative = alternative.getHtml().isEmpty() ? alternative.getText() : alternative.getHtml();
        boolean divider = prependDivider;
        for (Viewable htmlViewable : htmlAlternative) {
            html.append(buildHtml(htmlViewable, divider));
            divider = true;
        }
    }
    return html;
}
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 32 with Html

use of com.fsck.k9.mail.internet.Viewable.Html 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 = 0;
    // 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.isDebugLoggingEnabled()) {
            Timber.d("insertable: %s", quotedHtmlContent.toDebugString());
        }
        // Convert the text to HTML
        text = textToHtmlFragment(text);
        // Save length of the body. This is used when thawing drafts.
        composedMessageLength = text.length();
        // Append signature to the reply
        if (mAppendSignature && (mReplyAfterQuote || mSignatureBeforeQuotedText)) {
            text += getSignatureHtml();
        }
        /*
             * 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) {
                final String separator = "<br clear=\"all\">";
                text = separator + text;
                composedMessageOffset = separator.length();
            }
        } else {
            quotedHtmlContent.setInsertionLocation(InsertableHtmlContent.InsertionLocation.BEFORE_QUOTE);
            if (mInsertSeparator) {
                text += "<br><br>";
            }
        }
        // Place signature immediately after the quoted text
        if (mAppendSignature && !(mReplyAfterQuote || mSignatureBeforeQuotedText)) {
            quotedHtmlContent.insertIntoQuotedFooter(getSignatureHtml());
        }
        quotedHtmlContent.setUserContent(text);
        // Save the body's offset. This is used when thawing drafts.
        composedMessageOffset += quotedHtmlContent.getInsertionPoint();
        text = quotedHtmlContent.toString();
    } else {
        // Convert the text to HTML
        text = textToHtmlFragment(text);
        composedMessageLength = text.length();
        composedMessageOffset = HTML_AND_BODY_START.length();
        // There is no text to quote so simply append the signature if available
        if (mAppendSignature) {
            text += getSignatureHtml();
        }
        text = HTML_AND_BODY_START + text + HTML_AND_BODY_END;
    }
    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)

Example 33 with Html

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

the class MessageViewInfoExtractor method addMessageHeaderHtml.

/**
 * Extract important header values from a message to display inline (HTML version).
 *
 * @param html
 *         The {@link StringBuilder} that will receive the (HTML) output.
 * @param message
 *         The message to extract the header values from.
 *
 * @throws com.fsck.k9.mail.MessagingException
 *          In case of an error.
 */
private void addMessageHeaderHtml(StringBuilder html, Message message) throws MessagingException {
    html.append("<table style=\"border: 0\">");
    // From: <sender>
    Address[] from = message.getFrom();
    if (from != null && from.length > 0) {
        addTableRow(html, resourceProvider.messageHeaderFrom(), Address.toString(from));
    }
    // To: <recipients>
    Address[] to = message.getRecipients(Message.RecipientType.TO);
    if (to != null && to.length > 0) {
        addTableRow(html, resourceProvider.messageHeaderTo(), Address.toString(to));
    }
    // Cc: <recipients>
    Address[] cc = message.getRecipients(Message.RecipientType.CC);
    if (cc != null && cc.length > 0) {
        addTableRow(html, resourceProvider.messageHeaderCc(), Address.toString(cc));
    }
    // Date: <date>
    Date date = message.getSentDate();
    if (date != null) {
        addTableRow(html, resourceProvider.messageHeaderDate(), date.toString());
    }
    // Subject: <subject>
    String subject = message.getSubject();
    addTableRow(html, resourceProvider.messageHeaderSubject(), (subject == null) ? resourceProvider.noSubject() : subject);
    html.append("</table>");
}
Also used : Address(com.fsck.k9.mail.Address) Date(java.util.Date)

Example 34 with Html

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

the class MessageBuilder method buildBody.

private void buildBody(MimeMessage message) throws MessagingException {
    // Build the body.
    // TODO FIXME - body can be either an HTML or Text part, depending on whether we're in
    // HTML mode or not.  Should probably fix this so we don't mix up html and text parts.
    TextBody body = buildText(isDraft);
    // text/plain part when messageFormat == MessageFormat.HTML
    TextBody bodyPlain = null;
    final boolean hasAttachments = !attachments.isEmpty();
    if (messageFormat == SimpleMessageFormat.HTML) {
        // HTML message (with alternative text part)
        // This is the compiled MIME part for an HTML message.
        MimeMultipart composedMimeMessage = createMimeMultipart();
        composedMimeMessage.setSubType("alternative");
        // Let the receiver select either the text or the HTML part.
        bodyPlain = buildText(isDraft, SimpleMessageFormat.TEXT);
        composedMimeMessage.addBodyPart(MimeBodyPart.create(bodyPlain, "text/plain"));
        MimeBodyPart htmlPart = MimeBodyPart.create(body, "text/html");
        if (inlineAttachments != null && inlineAttachments.size() > 0) {
            MimeMultipart htmlPartWithInlineImages = new MimeMultipart("multipart/related", boundaryGenerator.generateBoundary());
            htmlPartWithInlineImages.addBodyPart(htmlPart);
            addInlineAttachmentsToMessage(htmlPartWithInlineImages);
            composedMimeMessage.addBodyPart(MimeBodyPart.create(htmlPartWithInlineImages));
        } else {
            composedMimeMessage.addBodyPart(htmlPart);
        }
        if (hasAttachments) {
            // If we're HTML and have attachments, we have a MimeMultipart container to hold the
            // whole message (mp here), of which one part is a MimeMultipart container
            // (composedMimeMessage) with the user's composed messages, and subsequent parts for
            // the attachments.
            MimeMultipart mp = createMimeMultipart();
            mp.addBodyPart(MimeBodyPart.create(composedMimeMessage));
            addAttachmentsToMessage(mp);
            MimeMessageHelper.setBody(message, mp);
        } else {
            // If no attachments, our multipart/alternative part is the only one we need.
            MimeMessageHelper.setBody(message, composedMimeMessage);
        }
    } else if (messageFormat == SimpleMessageFormat.TEXT) {
        // Text-only message.
        if (hasAttachments) {
            MimeMultipart mp = createMimeMultipart();
            mp.addBodyPart(MimeBodyPart.create(body, "text/plain"));
            addAttachmentsToMessage(mp);
            MimeMessageHelper.setBody(message, mp);
        } else {
            // No attachments to include, just stick the text body in the message and call it good.
            MimeMessageHelper.setBody(message, body);
        }
    }
    // If this is a draft, add metadata for thawing.
    if (isDraft) {
        // Add the identity to the message.
        message.addHeader(K9.IDENTITY_HEADER, buildIdentityHeader(body, bodyPlain));
    }
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 35 with Html

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

the class HtmlQuoteCreator method quoteOriginalHtmlMessage.

/**
 * Add quoting markup to a HTML message.
 * @param originalMessage Metadata for message being quoted.
 * @param messageBody Text of the message to be quoted.
 * @param quoteStyle Style of quoting.
 * @return Modified insertable message.
 */
public static InsertableHtmlContent quoteOriginalHtmlMessage(Message originalMessage, String messageBody, QuoteStyle quoteStyle) {
    CoreResourceProvider resourceProvider = DI.get(CoreResourceProvider.class);
    InsertableHtmlContent insertable = findInsertionPoints(messageBody);
    String sentDate = new QuoteDateFormatter().format(originalMessage.getSentDate());
    String fromAddress = Address.toString(originalMessage.getFrom());
    if (quoteStyle == QuoteStyle.PREFIX) {
        StringBuilder header = new StringBuilder();
        header.append("<div class=\"gmail_quote\">");
        if (sentDate.length() != 0) {
            String replyHeader = resourceProvider.replyHeader(fromAddress, sentDate);
            header.append(HtmlConverter.textToHtmlFragment(replyHeader));
        } else {
            String replyHeader = resourceProvider.replyHeader(fromAddress);
            header.append(HtmlConverter.textToHtmlFragment(replyHeader));
        }
        header.append("<blockquote class=\"gmail_quote\" " + "style=\"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;\">\r\n");
        String footer = "</blockquote></div>";
        insertable.insertIntoQuotedHeader(header.toString());
        insertable.insertIntoQuotedFooter(footer);
    } else if (quoteStyle == QuoteStyle.HEADER) {
        StringBuilder header = new StringBuilder();
        header.append("<div style='font-size:10.0pt;font-family:\"Tahoma\",\"sans-serif\";padding:3.0pt 0in 0in 0in'>\r\n");
        // This gets converted into a horizontal line during html to text conversion.
        header.append("<hr style='border:none;border-top:solid #E1E1E1 1.0pt'>\r\n");
        if (originalMessage.getFrom() != null && fromAddress.length() != 0) {
            header.append("<b>").append(resourceProvider.messageHeaderFrom()).append("</b> ").append(HtmlConverter.textToHtmlFragment(fromAddress)).append("<br>\r\n");
        }
        if (sentDate.length() != 0) {
            header.append("<b>").append(resourceProvider.messageHeaderDate()).append("</b> ").append(sentDate).append("<br>\r\n");
        }
        if (originalMessage.getRecipients(RecipientType.TO) != null && originalMessage.getRecipients(RecipientType.TO).length != 0) {
            header.append("<b>").append(resourceProvider.messageHeaderTo()).append("</b> ").append(HtmlConverter.textToHtmlFragment(Address.toString(originalMessage.getRecipients(RecipientType.TO)))).append("<br>\r\n");
        }
        if (originalMessage.getRecipients(RecipientType.CC) != null && originalMessage.getRecipients(RecipientType.CC).length != 0) {
            header.append("<b>").append(resourceProvider.messageHeaderCc()).append("</b> ").append(HtmlConverter.textToHtmlFragment(Address.toString(originalMessage.getRecipients(RecipientType.CC)))).append("<br>\r\n");
        }
        if (originalMessage.getSubject() != null) {
            header.append("<b>").append(resourceProvider.messageHeaderSubject()).append("</b> ").append(HtmlConverter.textToHtmlFragment(originalMessage.getSubject())).append("<br>\r\n");
        }
        header.append("</div>\r\n");
        header.append("<br>\r\n");
        insertable.insertIntoQuotedHeader(header.toString());
    }
    return insertable;
}
Also used : CoreResourceProvider(com.fsck.k9.CoreResourceProvider)

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