Search in sources :

Example 91 with Text

use of com.fsck.k9.mail.internet.Viewable.Text 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 content = HtmlConverter.wrapMessageContent(html);
        String sanitizedHtml = htmlSanitizer.sanitize(content);
        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(android.support.annotation.VisibleForTesting)

Example 92 with Text

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

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

the class TextBodyBuilder method buildTextPlain.

/**
     * Build the {@link Body} that will contain the text of the message.
     *
     * @return {@link TextBody} instance that contains the entered text and
     *         possibly the quoted original message.
     */
public TextBody buildTextPlain() {
    // 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;
    // Get the user-supplied text
    String text = mMessageContent;
    // Capture composed message length before we start attaching quoted parts and signatures.
    composedMessageLength = text.length();
    composedMessageOffset = 0;
    // Do we have to modify an existing message to include our reply?
    if (mIncludeQuotedText) {
        String quotedText = getQuotedText();
        if (mAppendSignature) {
            // Append signature to the text/reply
            if (mReplyAfterQuote || mSignatureBeforeQuotedText) {
                text += getSignature();
            }
        }
        if (mReplyAfterQuote) {
            composedMessageOffset = quotedText.length() + "\r\n".length();
            text = quotedText + "\r\n" + text;
        } else {
            text += "\r\n\r\n" + quotedText;
        }
        if (mAppendSignature) {
            // Place signature immediately after the quoted text
            if (!(mReplyAfterQuote || mSignatureBeforeQuotedText)) {
                text += getSignature();
            }
        }
    } else {
        // There is no text to quote so simply append the signature if available
        if (mAppendSignature) {
            // Append signature to the text/reply
            text += getSignature();
        }
    }
    TextBody body = new TextBody(text);
    body.setComposedMessageLength(composedMessageLength);
    body.setComposedMessageOffset(composedMessageOffset);
    return body;
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody)

Example 94 with Text

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

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

the class MigrationTo50 method foldersAddNotifyClassColumn.

public static void foldersAddNotifyClassColumn(SQLiteDatabase db, MigrationsHelper migrationsHelper) {
    try {
        db.execSQL("ALTER TABLE folders ADD notify_class TEXT default '" + Folder.FolderClass.INHERITED.name() + "'");
    } catch (SQLiteException e) {
        if (!e.getMessage().startsWith("duplicate column name:")) {
            throw e;
        }
    }
    ContentValues cv = new ContentValues();
    cv.put("notify_class", Folder.FolderClass.FIRST_CLASS.name());
    Account account = migrationsHelper.getAccount();
    db.update("folders", cv, "name = ?", new String[] { account.getInboxFolderName() });
}
Also used : ContentValues(android.content.ContentValues) Account(com.fsck.k9.Account) SQLiteException(android.database.sqlite.SQLiteException)

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