Search in sources :

Example 91 with Part

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

the class AttachmentController method downloadAttachment.

private void downloadAttachment(LocalPart localPart, final Runnable attachmentDownloadedCallback) {
    String accountUuid = localPart.getAccountUuid();
    Account account = Preferences.getPreferences(context).getAccount(accountUuid);
    LocalMessage message = localPart.getMessage();
    messageViewFragment.showAttachmentLoadingDialog();
    controller.loadAttachment(account, message, attachment.part, new SimpleMessagingListener() {

        @Override
        public void loadAttachmentFinished(Account account, Message message, Part part) {
            messageViewFragment.hideAttachmentLoadingDialogOnMainThread();
            messageViewFragment.runOnMainThread(attachmentDownloadedCallback);
        }

        @Override
        public void loadAttachmentFailed(Account account, Message message, Part part, String reason) {
            messageViewFragment.hideAttachmentLoadingDialogOnMainThread();
        }
    });
}
Also used : Account(com.fsck.k9.Account) LocalMessage(com.fsck.k9.mailstore.LocalMessage) LocalMessage(com.fsck.k9.mailstore.LocalMessage) Message(com.fsck.k9.mail.Message) Part(com.fsck.k9.mail.Part) LocalPart(com.fsck.k9.mailstore.LocalPart) SimpleMessagingListener(com.fsck.k9.controller.SimpleMessagingListener)

Example 92 with Part

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

the class MessagingController method sendAlternate.

public void sendAlternate(Context context, Account account, LocalMessage message) {
    Timber.d("Got message %s:%s:%s for sendAlternate", account.getDescription(), message.getFolder(), message.getUid());
    Intent msg = new Intent(Intent.ACTION_SEND);
    String quotedText = null;
    Part part = MimeUtility.findFirstPartByMimeType(message, "text/plain");
    if (part == null) {
        part = MimeUtility.findFirstPartByMimeType(message, "text/html");
    }
    if (part != null) {
        quotedText = MessageExtractor.getTextFromPart(part);
    }
    if (quotedText != null) {
        msg.putExtra(Intent.EXTRA_TEXT, quotedText);
    }
    msg.putExtra(Intent.EXTRA_SUBJECT, message.getSubject());
    Address[] from = message.getFrom();
    String[] senders = new String[from.length];
    for (int i = 0; i < from.length; i++) {
        senders[i] = from[i].toString();
    }
    msg.putExtra(Intents.Share.EXTRA_FROM, senders);
    Address[] to = message.getRecipients(RecipientType.TO);
    String[] recipientsTo = new String[to.length];
    for (int i = 0; i < to.length; i++) {
        recipientsTo[i] = to[i].toString();
    }
    msg.putExtra(Intent.EXTRA_EMAIL, recipientsTo);
    Address[] cc = message.getRecipients(RecipientType.CC);
    String[] recipientsCc = new String[cc.length];
    for (int i = 0; i < cc.length; i++) {
        recipientsCc[i] = cc[i].toString();
    }
    msg.putExtra(Intent.EXTRA_CC, recipientsCc);
    msg.setType("text/plain");
    context.startActivity(Intent.createChooser(msg, context.getString(R.string.send_alternate_chooser_title)));
}
Also used : Address(com.fsck.k9.mail.Address) Part(com.fsck.k9.mail.Part) Intent(android.content.Intent) SuppressLint(android.annotation.SuppressLint)

Example 93 with Part

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

the class LocalFolder method loadMessagePart.

private void loadMessagePart(LocalMessage message, Map<Long, Part> partById, Cursor cursor) throws MessagingException {
    long id = cursor.getLong(0);
    long parentId = cursor.getLong(2);
    String mimeType = cursor.getString(3);
    long size = cursor.getLong(4);
    byte[] header = cursor.getBlob(6);
    int dataLocation = cursor.getInt(9);
    String serverExtra = cursor.getString(15);
    // TODO we don't currently cache much of the part data which is computed with AttachmentInfoExtractor,
    // TODO might want to do that at a later point?
    // String displayName = cursor.getString(5);
    // int type = cursor.getInt(1);
    // boolean inlineAttachment = (type == MessagePartType.HIDDEN_ATTACHMENT);
    final Part part;
    if (id == message.getMessagePartId()) {
        part = message;
    } else {
        Part parentPart = partById.get(parentId);
        if (parentPart == null) {
            throw new IllegalStateException("Parent part not found");
        }
        String parentMimeType = parentPart.getMimeType();
        if (MimeUtility.isMultipart(parentMimeType)) {
            BodyPart bodyPart = new LocalBodyPart(getAccountUuid(), message, id, size);
            ((Multipart) parentPart.getBody()).addBodyPart(bodyPart);
            part = bodyPart;
        } else if (MimeUtility.isMessage(parentMimeType)) {
            Message innerMessage = new LocalMimeMessage(getAccountUuid(), message, id);
            parentPart.setBody(innerMessage);
            part = innerMessage;
        } else {
            throw new IllegalStateException("Parent is neither a multipart nor a message");
        }
        parseHeaderBytes(part, header);
    }
    partById.put(id, part);
    part.setServerExtra(serverExtra);
    if (MimeUtility.isMultipart(mimeType)) {
        byte[] preamble = cursor.getBlob(11);
        byte[] epilogue = cursor.getBlob(12);
        String boundary = cursor.getString(13);
        MimeMultipart multipart = new MimeMultipart(mimeType, boundary);
        part.setBody(multipart);
        multipart.setPreamble(preamble);
        multipart.setEpilogue(epilogue);
    } else if (dataLocation == DataLocation.IN_DATABASE) {
        String encoding = cursor.getString(7);
        byte[] data = cursor.getBlob(10);
        Body body = new BinaryMemoryBody(data, encoding);
        part.setBody(body);
    } else if (dataLocation == DataLocation.ON_DISK) {
        String encoding = cursor.getString(7);
        File file = localStore.getAttachmentFile(Long.toString(id));
        if (file.exists()) {
            Body body = new FileBackedBody(file, encoding);
            part.setBody(body);
        }
    }
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) 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) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) Body(com.fsck.k9.mail.Body) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) File(java.io.File)

Example 94 with Part

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

the class AttachmentResolver method createFromPart.

@WorkerThread
public static AttachmentResolver createFromPart(Part part) {
    AttachmentInfoExtractor attachmentInfoExtractor = AttachmentInfoExtractor.getInstance();
    Map<String, Uri> contentIdToAttachmentUriMap = buildCidToAttachmentUriMap(attachmentInfoExtractor, part);
    return new AttachmentResolver(contentIdToAttachmentUriMap);
}
Also used : AttachmentInfoExtractor(com.fsck.k9.message.extractors.AttachmentInfoExtractor) Uri(android.net.Uri) WorkerThread(android.support.annotation.WorkerThread)

Example 95 with Part

use of com.fsck.k9.mail.Part 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);
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) Body(com.fsck.k9.mail.Body)

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