Search in sources :

Example 26 with TextBody

use of com.fsck.k9.mail.internet.TextBody 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 27 with TextBody

use of com.fsck.k9.mail.internet.TextBody 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 28 with TextBody

use of com.fsck.k9.mail.internet.TextBody 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(new MimeBodyPart(bodyPlain, "text/plain"));
        composedMimeMessage.addBodyPart(new MimeBodyPart(body, "text/html"));
        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(new MimeBodyPart(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(new MimeBodyPart(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 29 with TextBody

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

the class MessageCompose method processDraftMessage.

private void processDraftMessage(MessageViewInfo messageViewInfo) {
    Message message = messageViewInfo.message;
    draftId = MessagingController.getInstance(getApplication()).getId(message);
    subjectView.setText(message.getSubject());
    recipientPresenter.initFromDraftMessage(message);
    // Read In-Reply-To header from draft
    final String[] inReplyTo = message.getHeader("In-Reply-To");
    if (inReplyTo.length >= 1) {
        repliedToMessageId = inReplyTo[0];
    }
    // Read References header from draft
    final String[] references = message.getHeader("References");
    if (references.length >= 1) {
        referencedMessageIds = references[0];
    }
    if (!relatedMessageProcessed) {
        attachmentPresenter.loadNonInlineAttachments(messageViewInfo);
    }
    // Decode the identity header when loading a draft.
    // See buildIdentityHeader(TextBody) for a detailed description of the composition of this blob.
    Map<IdentityField, String> k9identity = new HashMap<>();
    String[] identityHeaders = message.getHeader(K9.IDENTITY_HEADER);
    if (identityHeaders.length > 0 && identityHeaders[0] != null) {
        k9identity = IdentityHeaderParser.parse(identityHeaders[0]);
    }
    Identity newIdentity = new Identity();
    if (k9identity.containsKey(IdentityField.SIGNATURE)) {
        newIdentity.setSignatureUse(true);
        newIdentity.setSignature(k9identity.get(IdentityField.SIGNATURE));
        signatureChanged = true;
    } else {
        if (message instanceof LocalMessage) {
            newIdentity.setSignatureUse(((LocalMessage) message).getFolder().getSignatureUse());
        }
        newIdentity.setSignature(identity.getSignature());
    }
    if (k9identity.containsKey(IdentityField.NAME)) {
        newIdentity.setName(k9identity.get(IdentityField.NAME));
        identityChanged = true;
    } else {
        newIdentity.setName(identity.getName());
    }
    if (k9identity.containsKey(IdentityField.EMAIL)) {
        newIdentity.setEmail(k9identity.get(IdentityField.EMAIL));
        identityChanged = true;
    } else {
        newIdentity.setEmail(identity.getEmail());
    }
    if (k9identity.containsKey(IdentityField.ORIGINAL_MESSAGE)) {
        relatedMessageReference = null;
        String originalMessage = k9identity.get(IdentityField.ORIGINAL_MESSAGE);
        MessageReference messageReference = MessageReference.parse(originalMessage);
        if (messageReference != null) {
            // Check if this is a valid account in our database
            Preferences prefs = Preferences.getPreferences(getApplicationContext());
            Account account = prefs.getAccount(messageReference.getAccountUuid());
            if (account != null) {
                relatedMessageReference = messageReference;
            }
        }
    }
    identity = newIdentity;
    updateSignature();
    updateFrom();
    quotedMessagePresenter.processDraftMessage(messageViewInfo, k9identity);
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) Account(com.fsck.k9.Account) LocalMessage(com.fsck.k9.mailstore.LocalMessage) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) HashMap(java.util.HashMap) Identity(com.fsck.k9.Identity) Preferences(com.fsck.k9.Preferences) IdentityField(com.fsck.k9.message.IdentityField)

Example 30 with TextBody

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

the class MessageTest method textBodyPart.

private MimeBodyPart textBodyPart() throws MessagingException {
    TextBody textBody = new TextBody("Testing.\r\n" + "This is a text body with some greek characters.\r\n" + "αβγδεζηθ\r\n" + "End of test.\r\n");
    textBody.setCharset("utf-8");
    MimeBodyPart bodyPart = new MimeBodyPart();
    MimeMessageHelper.setBody(bodyPart, textBody);
    CharsetSupport.setCharset("utf-8", bodyPart);
    return bodyPart;
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Aggregations

TextBody (com.fsck.k9.mail.internet.TextBody)27 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)16 Test (org.junit.Test)15 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)13 Part (com.fsck.k9.mail.Part)7 ArrayList (java.util.ArrayList)7 Viewable (com.fsck.k9.mail.internet.Viewable)6 ViewableExtractedText (com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText)6 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)5 BodyPart (com.fsck.k9.mail.BodyPart)3 Message (com.fsck.k9.mail.Message)3 MessagingException (com.fsck.k9.mail.MessagingException)3 Address (com.fsck.k9.mail.Address)2 InsertableHtmlContent (com.fsck.k9.message.quote.InsertableHtmlContent)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Date (java.util.Date)2 Theory (org.junit.experimental.theories.Theory)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 Account (com.fsck.k9.Account)1