use of com.fsck.k9.mail.internet.Viewable.Html in project k-9 by k9mail.
the class MigrationTo51 method insertTextualPartIntoDatabase.
private static MimeStructureState insertTextualPartIntoDatabase(SQLiteDatabase db, MimeStructureState structureState, MimeHeader mimeHeader, String content, boolean isHtml) throws IOException {
if (mimeHeader == null) {
mimeHeader = new MimeHeader();
}
mimeHeader.setHeader(MimeHeader.HEADER_CONTENT_TYPE, isHtml ? "text/html; charset=\"utf-8\"" : "text/plain; charset=\"utf-8\"");
mimeHeader.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, MimeUtil.ENC_QUOTED_PRINTABLE);
byte[] contentBytes;
int decodedBodySize;
int dataLocation;
if (content != null) {
ByteArrayOutputStream contentOutputStream = new ByteArrayOutputStream();
QuotedPrintableOutputStream quotedPrintableOutputStream = new QuotedPrintableOutputStream(contentOutputStream, false);
quotedPrintableOutputStream.write(content.getBytes());
quotedPrintableOutputStream.flush();
dataLocation = DATA_LOCATION__IN_DATABASE;
contentBytes = contentOutputStream.toByteArray();
decodedBodySize = content.length();
} else {
dataLocation = DATA_LOCATION__MISSING;
contentBytes = null;
decodedBodySize = 0;
}
ContentValues cv = new ContentValues();
cv.put("type", MESSAGE_PART_TYPE__UNKNOWN);
cv.put("data_location", dataLocation);
cv.put("mime_type", isHtml ? "text/html" : "text/plain");
cv.put("header", mimeHeader.toString());
cv.put("data", contentBytes);
cv.put("decoded_body_size", decodedBodySize);
cv.put("encoding", MimeUtil.ENC_QUOTED_PRINTABLE);
cv.put("charset", "utf-8");
structureState.applyValues(cv);
long partId = db.insertOrThrow("message_parts", null, cv);
return structureState.nextChild(partId);
}
use of com.fsck.k9.mail.internet.Viewable.Html in project k-9 by k9mail.
the class TextBodyBuilderTest method testBuildTextHtml.
@Theory
public void testBuildTextHtml(boolean includeQuotedText, QuoteStyle quoteStyle, boolean isReplyAfterQuote, boolean isSignatureUse, boolean isSignatureBeforeQuotedText, boolean isDraft) {
String expectedText;
int expectedMessageLength;
int expectedMessagePosition = 0;
String expectedHtmlContent;
String expectedPrefix = "";
if (includeQuotedText && quoteStyle == QuoteStyle.PREFIX && isReplyAfterQuote && !isDraft) {
expectedPrefix = "<br clear=\"all\">";
}
String expectedPostfix = "";
if (!isDraft && includeQuotedText) {
expectedPostfix = "<br><br>";
}
// 3.signature
if (quoteStyle == QuoteStyle.PREFIX && isReplyAfterQuote) {
expectedText = expectedPrefix + "<html>message content";
if (!isDraft && isSignatureUse) {
expectedText += "\r\n" + "signature";
}
expectedText += "</html>";
expectedMessageLength = expectedText.length();
String quotedContent = "quoted text";
if (isDraft || includeQuotedText) {
expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent, 0, false, expectedText, expectedText + quotedContent);
expectedText += quotedContent;
} else {
expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent, 0, true, "", quotedContent);
// expectedText += quotedContent;
}
} else // 3.quoted text
if (isSignatureBeforeQuotedText) {
expectedText = expectedPrefix + "<html>message content";
if (!isDraft && isSignatureUse) {
expectedText += "\r\n" + "signature";
}
expectedText += "</html>";
expectedText += expectedPostfix;
expectedMessageLength = expectedText.length();
String quotedContent = "quoted text";
if (isDraft || includeQuotedText) {
expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent, 0, true, expectedText, expectedText + quotedContent);
expectedText += quotedContent;
} else {
expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent, 0, true, "", quotedContent);
// expectedText += quotedContent;
}
} else // 1.message content
// 2.quoted text
// 3.signature
{
String expectedSignature = "";
expectedText = expectedPrefix + "<html>message content";
if (!isDraft && isSignatureUse) {
if (!includeQuotedText) {
expectedText += "\r\n" + "signature";
} else {
expectedSignature = "<html>\r\nsignature</html>";
}
}
expectedText += "</html>";
expectedText += expectedPostfix;
expectedMessageLength = expectedText.length();
String quotedContent = "quoted text";
if (isDraft || includeQuotedText) {
expectedHtmlContent = makeExpectedHtmlContent(expectedText, expectedSignature + quotedContent, expectedSignature.length(), true, expectedText, expectedText + expectedSignature + quotedContent);
expectedText += expectedSignature + quotedContent;
} else {
expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent, 0, true, "", quotedContent);
// expectedText += quotedContent;
}
}
InsertableHtmlContent insertableHtmlContent = new InsertableHtmlContent();
String quotedText = "quoted text";
insertableHtmlContent.setQuotedContent(new StringBuilder(quotedText));
String messageText = "message content";
String signatureText = "signature";
TestingTextBodyBuilder textBodyBuilder = new TestingTextBodyBuilder(includeQuotedText, isDraft, quoteStyle, isReplyAfterQuote, isSignatureBeforeQuotedText, isSignatureUse, messageText, signatureText);
textBodyBuilder.setQuotedTextHtml(insertableHtmlContent);
TextBody textBody = textBodyBuilder.buildTextHtml();
assertThat(textBody, instanceOf(TextBody.class));
assertThat(textBody.getRawText(), is(expectedText));
assertThat(textBody.getComposedMessageLength(), is(expectedMessageLength));
assertThat(textBody.getComposedMessageOffset(), is(expectedMessagePosition));
assertThat(insertableHtmlContent.toDebugString(), is(expectedHtmlContent));
}
use of com.fsck.k9.mail.internet.Viewable.Html in project k-9 by k9mail.
the class PreviewTextExtractorTest method extractPreview_withSimpleTextHtml.
@Test
public void extractPreview_withSimpleTextHtml() throws Exception {
String text = "<b>The quick brown fox jumps over the lazy dog</b>";
Part part = createTextPart("text/html", text);
String preview = previewTextExtractor.extractPreview(part);
assertEquals("The quick brown fox jumps over the lazy dog", preview);
}
use of com.fsck.k9.mail.internet.Viewable.Html in project sms-backup-plus by jberkel.
the class Attachment method createPart.
private static MimeBodyPart createPart(Body body, final String filename, final String contentType) throws MessagingException {
MimeBodyPart part = new MimeBodyPart(body, contentType);
String contentTypeHeader = TextUtils.isEmpty(contentType) ? "application/octet-stream" : contentType;
String disposition = "attachment";
if (!TextUtils.isEmpty(filename)) {
// should set both name and filename parameters
// http://www.imc.org/ietf-smtp/mail-archive/msg05023.html
disposition += encodeRFC2231("filename", filename);
contentTypeHeader += encodeRFC2231("name", filename);
}
part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentTypeHeader);
part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, disposition);
part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64");
return part;
}
use of com.fsck.k9.mail.internet.Viewable.Html 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 sanitizedHtml = htmlProcessor.processForDisplay(html.toString());
return new ViewableExtractedText(text.toString(), sanitizedHtml);
} catch (Exception e) {
throw new MessagingException("Couldn't extract viewable parts", e);
}
}
Aggregations