use of com.fsck.k9.mail.Body in project k-9 by k9mail.
the class LocalStore method writeRawBodyToStream.
private void writeRawBodyToStream(Cursor cursor, SQLiteDatabase db, OutputStream outputStream) throws IOException, MessagingException {
long partId = cursor.getLong(ATTACH_PART_ID_INDEX);
String rootPart = cursor.getString(ATTACH_ROOT_INDEX);
LocalMessage message = loadLocalMessageByRootPartId(db, rootPart);
if (message == null) {
throw new MessagingException("Unable to find message for attachment!");
}
Part part = findPartById(message, partId);
if (part == null) {
throw new MessagingException("Unable to find attachment part in associated message (db integrity error?)");
}
Body body = part.getBody();
if (body == null) {
throw new MessagingException("Attachment part isn't available!");
}
body.writeTo(outputStream);
}
use of com.fsck.k9.mail.Body in project k-9 by k9mail.
the class LocalStore method findPartById.
static Part findPartById(Part searchRoot, long partId) {
if (searchRoot instanceof LocalMessage) {
LocalMessage localMessage = (LocalMessage) searchRoot;
if (localMessage.getMessagePartId() == partId) {
return localMessage;
}
}
Stack<Part> partStack = new Stack<>();
partStack.add(searchRoot);
while (!partStack.empty()) {
Part part = partStack.pop();
if (part instanceof LocalPart) {
LocalPart localBodyPart = (LocalPart) part;
if (localBodyPart.getPartId() == partId) {
return part;
}
}
Body body = part.getBody();
if (body instanceof Multipart) {
Multipart innerMultipart = (Multipart) body;
for (BodyPart innerPart : innerMultipart.getBodyParts()) {
partStack.add(innerPart);
}
}
if (body instanceof Part) {
partStack.add((Part) body);
}
}
return null;
}
use of com.fsck.k9.mail.Body 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));
}
}
use of com.fsck.k9.mail.Body in project k-9 by k9mail.
the class AttachmentInfoExtractor method extractAttachmentInfo.
@WorkerThread
public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException {
Uri uri;
long size;
boolean isContentAvailable;
if (part instanceof LocalPart) {
LocalPart localPart = (LocalPart) part;
String accountUuid = localPart.getAccountUuid();
long messagePartId = localPart.getPartId();
size = localPart.getSize();
isContentAvailable = part.getBody() != null;
uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId);
} else if (part instanceof LocalMessage) {
LocalMessage localMessage = (LocalMessage) part;
String accountUuid = localMessage.getAccount().getUuid();
long messagePartId = localMessage.getMessagePartId();
size = localMessage.getSize();
isContentAvailable = part.getBody() != null;
uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId);
} else {
Body body = part.getBody();
if (body instanceof DeferredFileBody) {
DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body;
size = decryptedTempFileBody.getSize();
uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType());
isContentAvailable = true;
} else {
throw new IllegalArgumentException("Unsupported part type provided");
}
}
return extractAttachmentInfo(part, uri, size, isContentAvailable);
}
use of com.fsck.k9.mail.Body in project k-9 by k9mail.
the class TextPartFinder method findTextPartInMultipartAlternative.
private Part findTextPartInMultipartAlternative(Multipart multipart) {
Part htmlPart = null;
for (BodyPart bodyPart : multipart.getBodyParts()) {
String mimeType = bodyPart.getMimeType();
Body body = bodyPart.getBody();
if (body instanceof Multipart) {
Part candidatePart = findFirstTextPart(bodyPart);
if (candidatePart != null) {
if (isSameMimeType(candidatePart.getMimeType(), "text/html")) {
htmlPart = candidatePart;
} else {
return candidatePart;
}
}
} else if (isSameMimeType(mimeType, "text/plain")) {
return bodyPart;
} else if (isSameMimeType(mimeType, "text/html") && htmlPart == null) {
htmlPart = bodyPart;
}
}
if (htmlPart != null) {
return htmlPart;
}
return null;
}
Aggregations