Search in sources :

Example 6 with Part

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

the class MessageHelper method isCompletePartAvailable.

public static boolean isCompletePartAvailable(Part part) {
    Stack<Part> partsToCheck = new Stack<Part>();
    partsToCheck.push(part);
    while (!partsToCheck.isEmpty()) {
        Part currentPart = partsToCheck.pop();
        Body body = currentPart.getBody();
        boolean isBodyMissing = body == null;
        if (isBodyMissing) {
            return false;
        }
        if (body instanceof Multipart) {
            Multipart multipart = (Multipart) body;
            for (BodyPart bodyPart : multipart.getBodyParts()) {
                partsToCheck.push(bodyPart);
            }
        }
    }
    return true;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Multipart(com.fsck.k9.mail.Multipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Body(com.fsck.k9.mail.Body) Stack(java.util.Stack)

Example 7 with Part

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

the class MessageViewInfoExtractor method buildText.

private StringBuilder buildText(Viewable viewable, boolean prependDivider) {
    StringBuilder text = new StringBuilder();
    if (viewable instanceof Textual) {
        Part part = ((Textual) viewable).getPart();
        addTextDivider(text, part, prependDivider);
        String t = MessageExtractor.getTextFromPart(part);
        if (t == null) {
            t = "";
        } else if (viewable instanceof Html) {
            t = HtmlConverter.htmlToText(t);
        } else if (viewable instanceof Flowed) {
            t = FlowedMessageUtils.deflow(t, false);
        } else if (!(viewable instanceof Text)) {
            throw new IllegalStateException("unhandled case!");
        }
        text.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/plain child; fall-back to the text/html part.
        Alternative alternative = (Alternative) viewable;
        List<Viewable> textAlternative = alternative.getText().isEmpty() ? alternative.getHtml() : alternative.getText();
        boolean divider = prependDivider;
        for (Viewable textViewable : textAlternative) {
            text.append(buildText(textViewable, divider));
            divider = true;
        }
    }
    return text;
}
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 8 with Part

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

the class MessageViewInfoExtractor method extractMessageForView.

@WorkerThread
public MessageViewInfo extractMessageForView(Message message, @Nullable MessageCryptoAnnotations annotations) throws MessagingException {
    Part rootPart;
    CryptoResultAnnotation cryptoResultAnnotation;
    List<Part> extraParts;
    CryptoMessageParts cryptoMessageParts = MessageCryptoSplitter.split(message, annotations);
    if (cryptoMessageParts != null) {
        rootPart = cryptoMessageParts.contentPart;
        cryptoResultAnnotation = cryptoMessageParts.contentCryptoAnnotation;
        extraParts = cryptoMessageParts.extraParts;
    } else {
        if (annotations != null && !annotations.isEmpty()) {
            Timber.e("Got message annotations but no crypto root part!");
        }
        rootPart = message;
        cryptoResultAnnotation = null;
        extraParts = null;
    }
    List<AttachmentViewInfo> attachmentInfos = new ArrayList<>();
    ViewableExtractedText viewable = extractViewableAndAttachments(Collections.singletonList(rootPart), attachmentInfos);
    List<AttachmentViewInfo> extraAttachmentInfos = new ArrayList<>();
    String extraViewableText = null;
    if (extraParts != null) {
        ViewableExtractedText extraViewable = extractViewableAndAttachments(extraParts, extraAttachmentInfos);
        extraViewableText = extraViewable.text;
    }
    AttachmentResolver attachmentResolver = AttachmentResolver.createFromPart(rootPart);
    boolean isMessageIncomplete = !message.isSet(Flag.X_DOWNLOADED_FULL) || MessageExtractor.hasMissingParts(message);
    return MessageViewInfo.createWithExtractedContent(message, isMessageIncomplete, rootPart, viewable.html, attachmentInfos, cryptoResultAnnotation, attachmentResolver, extraViewableText, extraAttachmentInfos);
}
Also used : CryptoMessageParts(com.fsck.k9.ui.crypto.MessageCryptoSplitter.CryptoMessageParts) Part(com.fsck.k9.mail.Part) ArrayList(java.util.ArrayList) WorkerThread(android.support.annotation.WorkerThread)

Example 9 with Part

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

the class LocalFolder method leafPartToContentValues.

private File leafPartToContentValues(ContentValues cv, Part part, Body body) throws MessagingException, IOException {
    AttachmentViewInfo attachment = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part);
    cv.put("display_name", attachment.displayName);
    String encoding = getTransferEncoding(part);
    if (!(body instanceof SizeAware)) {
        throw new IllegalStateException("Body needs to implement SizeAware");
    }
    SizeAware sizeAwareBody = (SizeAware) body;
    long fileSize = sizeAwareBody.getSize();
    File file = null;
    int dataLocation;
    if (fileSize > MAX_BODY_SIZE_FOR_DATABASE) {
        dataLocation = DataLocation.ON_DISK;
        file = writeBodyToDiskIfNecessary(part);
        long size = decodeAndCountBytes(file, encoding, fileSize);
        cv.put("decoded_body_size", size);
    } else {
        dataLocation = DataLocation.IN_DATABASE;
        byte[] bodyData = getBodyBytes(body);
        cv.put("data", bodyData);
        long size = decodeAndCountBytes(bodyData, encoding, bodyData.length);
        cv.put("decoded_body_size", size);
    }
    cv.put("data_location", dataLocation);
    cv.put("encoding", encoding);
    cv.put("content_id", part.getContentId());
    return file;
}
Also used : SizeAware(com.fsck.k9.mail.internet.SizeAware) File(java.io.File)

Example 10 with Part

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

the class LocalFolder method updateOrInsertMessagePart.

private long updateOrInsertMessagePart(SQLiteDatabase db, ContentValues cv, Part part, long existingMessagePartId) throws IOException, MessagingException {
    byte[] headerBytes = getHeaderBytes(part);
    cv.put("mime_type", part.getMimeType());
    cv.put("header", headerBytes);
    cv.put("type", MessagePartType.UNKNOWN);
    File file = null;
    Body body = part.getBody();
    if (body instanceof Multipart) {
        multipartToContentValues(cv, (Multipart) body);
    } else if (body == null) {
        missingPartToContentValues(cv, part);
    } else if (body instanceof Message) {
        messageMarkerToContentValues(cv);
    } else {
        file = leafPartToContentValues(cv, part, body);
    }
    long messagePartId;
    if (existingMessagePartId != INVALID_MESSAGE_PART_ID) {
        messagePartId = existingMessagePartId;
        db.update("message_parts", cv, "id = ?", new String[] { Long.toString(messagePartId) });
    } else {
        messagePartId = db.insertOrThrow("message_parts", null, cv);
    }
    if (file != null) {
        moveTemporaryFile(file, Long.toString(messagePartId));
    }
    return messagePartId;
}
Also used : Multipart(com.fsck.k9.mail.Multipart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) File(java.io.File) Body(com.fsck.k9.mail.Body) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody)

Aggregations

Part (com.fsck.k9.mail.Part)113 Test (org.junit.Test)92 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)78 BodyPart (com.fsck.k9.mail.BodyPart)73 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)39 Message (com.fsck.k9.mail.Message)32 MessageCreationHelper.createTextPart (com.fsck.k9.message.MessageCreationHelper.createTextPart)30 Body (com.fsck.k9.mail.Body)29 Multipart (com.fsck.k9.mail.Multipart)27 ArrayList (java.util.ArrayList)27 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)20 MessageCreationHelper.createEmptyPart (com.fsck.k9.message.MessageCreationHelper.createEmptyPart)19 MessagingException (com.fsck.k9.mail.MessagingException)16 MessageCreationHelper.createPart (com.fsck.k9.message.MessageCreationHelper.createPart)16 TextBody (com.fsck.k9.mail.internet.TextBody)14 AttachmentViewInfo (com.fsck.k9.mailstore.AttachmentViewInfo)13 Viewable (com.fsck.k9.mail.internet.Viewable)10 Uri (android.net.Uri)8 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)6 BinaryMemoryBody (com.fsck.k9.mailstore.BinaryMemoryBody)6