Search in sources :

Example 16 with Attachment

use of com.fsck.k9.activity.misc.Attachment in project k-9 by k9mail.

the class AttachmentPresenter method onRestoreInstanceState.

public void onRestoreInstanceState(Bundle savedInstanceState) {
    actionToPerformAfterWaiting = WaitingAction.valueOf(savedInstanceState.getString(STATE_KEY_WAITING_FOR_ATTACHMENTS));
    nextLoaderId = savedInstanceState.getInt(STATE_KEY_NEXT_LOADER_ID);
    ArrayList<Attachment> attachmentList = savedInstanceState.getParcelableArrayList(STATE_KEY_ATTACHMENTS);
    // noinspection ConstantConditions, we know this is set in onSaveInstanceState
    for (Attachment attachment : attachmentList) {
        attachments.put(attachment.uri, attachment);
        attachmentMvpView.addAttachmentView(attachment);
        if (attachment.state == LoadingState.URI_ONLY) {
            initAttachmentInfoLoader(attachment);
        } else if (attachment.state == LoadingState.METADATA) {
            initAttachmentContentLoader(attachment);
        }
    }
}
Also used : Attachment(com.fsck.k9.activity.misc.Attachment)

Example 17 with Attachment

use of com.fsck.k9.activity.misc.Attachment 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)

Example 18 with Attachment

use of com.fsck.k9.activity.misc.Attachment in project k-9 by k9mail.

the class LockableDatabase method switchProvider.

/**
     * @param newProviderId
     *            Never <code>null</code>.
     * @throws MessagingException
     */
public void switchProvider(final String newProviderId) throws MessagingException {
    if (newProviderId.equals(mStorageProviderId)) {
        Timber.v("LockableDatabase: Ignoring provider switch request as they are equal: %s", newProviderId);
        return;
    }
    final String oldProviderId = mStorageProviderId;
    lockWrite(oldProviderId);
    try {
        lockWrite(newProviderId);
        try {
            try {
                mDb.close();
            } catch (Exception e) {
                Timber.i(e, "Unable to close DB on local store migration");
            }
            final StorageManager storageManager = getStorageManager();
            File oldDatabase = storageManager.getDatabase(uUid, oldProviderId);
            // create new path
            prepareStorage(newProviderId);
            // move all database files
            FileHelper.moveRecursive(oldDatabase, storageManager.getDatabase(uUid, newProviderId));
            // move all attachment files
            FileHelper.moveRecursive(storageManager.getAttachmentDirectory(uUid, oldProviderId), storageManager.getAttachmentDirectory(uUid, newProviderId));
            // remove any remaining old journal files
            deleteDatabase(oldDatabase);
            mStorageProviderId = newProviderId;
            // re-initialize this class with the new Uri
            openOrCreateDataspace();
        } finally {
            unlockWrite(newProviderId);
        }
    } finally {
        unlockWrite(oldProviderId);
    }
}
Also used : File(java.io.File) SQLiteException(android.database.sqlite.SQLiteException) MessagingException(com.fsck.k9.mail.MessagingException)

Example 19 with Attachment

use of com.fsck.k9.activity.misc.Attachment in project k-9 by k9mail.

the class MessageExtractor method findViewablesAndAttachments.

/** Traverse the MIME tree of a message and extract viewable parts. */
public static void findViewablesAndAttachments(Part part, @Nullable List<Viewable> outputViewableParts, @Nullable List<Part> outputNonViewableParts) throws MessagingException {
    boolean skipSavingNonViewableParts = outputNonViewableParts == null;
    boolean skipSavingViewableParts = outputViewableParts == null;
    if (skipSavingNonViewableParts && skipSavingViewableParts) {
        throw new IllegalArgumentException("method was called but no output is to be collected - this a bug!");
    }
    Body body = part.getBody();
    if (body instanceof Multipart) {
        Multipart multipart = (Multipart) body;
        if (isSameMimeType(part.getMimeType(), "multipart/alternative")) {
            /*
                 * For multipart/alternative parts we try to find a text/plain and a text/html
                 * child. Everything else we find is put into 'attachments'.
                 */
            List<Viewable> text = findTextPart(multipart, true);
            Set<Part> knownTextParts = getParts(text);
            List<Viewable> html = findHtmlPart(multipart, knownTextParts, outputNonViewableParts, true);
            if (skipSavingViewableParts) {
                return;
            }
            if (!text.isEmpty() || !html.isEmpty()) {
                Alternative alternative = new Alternative(text, html);
                outputViewableParts.add(alternative);
            }
        } else {
            // For all other multipart parts we recurse to grab all viewable children.
            for (Part bodyPart : multipart.getBodyParts()) {
                findViewablesAndAttachments(bodyPart, outputViewableParts, outputNonViewableParts);
            }
        }
    } else if (body instanceof Message && !("attachment".equalsIgnoreCase(getContentDisposition(part)))) {
        if (skipSavingViewableParts) {
            return;
        }
        /*
             * We only care about message/rfc822 parts whose Content-Disposition header has a value
             * other than "attachment".
             */
        Message message = (Message) body;
        // We add the Message object so we can extract the filename later.
        outputViewableParts.add(new MessageHeader(part, message));
        // Recurse to grab all viewable parts and attachments from that message.
        findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
    } else if (isPartTextualBody(part)) {
        if (skipSavingViewableParts) {
            return;
        }
        String mimeType = part.getMimeType();
        Viewable viewable;
        if (isSameMimeType(mimeType, "text/plain")) {
            if (isFormatFlowed(part.getContentType())) {
                viewable = new Flowed(part);
            } else {
                viewable = new Text(part);
            }
        } else {
            viewable = new Html(part);
        }
        outputViewableParts.add(viewable);
    } else if (isSameMimeType(part.getMimeType(), "application/pgp-signature")) {
    // ignore this type explicitly
    } else {
        if (skipSavingNonViewableParts) {
            return;
        }
        // Everything else is treated as attachment.
        outputNonViewableParts.add(part);
    }
}
Also used : Multipart(com.fsck.k9.mail.Multipart) Alternative(com.fsck.k9.mail.internet.Viewable.Alternative) Message(com.fsck.k9.mail.Message) Html(com.fsck.k9.mail.internet.Viewable.Html) Text(com.fsck.k9.mail.internet.Viewable.Text) Flowed(com.fsck.k9.mail.internet.Viewable.Flowed) MimeUtility.isFormatFlowed(com.fsck.k9.mail.internet.MimeUtility.isFormatFlowed) Part(com.fsck.k9.mail.Part) BodyPart(com.fsck.k9.mail.BodyPart) MessageHeader(com.fsck.k9.mail.internet.Viewable.MessageHeader) Body(com.fsck.k9.mail.Body)

Example 20 with Attachment

use of com.fsck.k9.activity.misc.Attachment in project k-9 by k9mail.

the class MessageExtractor method findHtmlPart.

/**
     * Search the children of a {@link Multipart} for {@code text/html} parts.
     * Every part that is not a {@code text/html} we want to display, we add to 'attachments'.
     *
     * @param multipart The {@code Multipart} to search through.
     * @param knownTextParts A set of {@code text/plain} parts that shouldn't be added to 'attachments'.
     * @param outputNonViewableParts A list that will receive the parts that are considered attachments.
     * @param directChild If {@code true}, this method will add all {@code text/html} parts except the first
     *         found to 'attachments'.
     *
     * @return A list of {@link Text} viewables.
     *
     * @throws MessagingException In case of an error.
     */
private static List<Viewable> findHtmlPart(Multipart multipart, Set<Part> knownTextParts, @Nullable List<Part> outputNonViewableParts, boolean directChild) throws MessagingException {
    boolean saveNonViewableParts = outputNonViewableParts != null;
    List<Viewable> viewables = new ArrayList<>();
    boolean partFound = false;
    for (Part part : multipart.getBodyParts()) {
        Body body = part.getBody();
        if (body instanceof Multipart) {
            Multipart innerMultipart = (Multipart) body;
            if (directChild && partFound) {
                if (saveNonViewableParts) {
                    // We already found our text/html part. Now we're only looking for attachments.
                    findAttachments(innerMultipart, knownTextParts, outputNonViewableParts);
                }
            } else {
                /*
                     * Recurse to find HTML parts. Since this is a multipart that is a child of a
                     * multipart/alternative we don't want to stop after the first text/html part
                     * we find. This will allow to get all text parts for constructions like this:
                     *
                     * 1. multipart/alternative
                     * 1.1. text/plain
                     * 1.2. multipart/mixed
                     * 1.2.1. text/html
                     * 1.2.2. text/html
                     * 1.3. image/jpeg
                     */
                List<Viewable> htmlViewables = findHtmlPart(innerMultipart, knownTextParts, outputNonViewableParts, false);
                if (!htmlViewables.isEmpty()) {
                    partFound = true;
                    viewables.addAll(htmlViewables);
                }
            }
        } else if (!(directChild && partFound) && isPartTextualBody(part) && isSameMimeType(part.getMimeType(), "text/html")) {
            Html html = new Html(part);
            viewables.add(html);
            partFound = true;
        } else if (!knownTextParts.contains(part)) {
            if (saveNonViewableParts) {
                // Only add this part as attachment if it's not a viewable text/plain part found earlier
                outputNonViewableParts.add(part);
            }
        }
    }
    return viewables;
}
Also used : Multipart(com.fsck.k9.mail.Multipart) Part(com.fsck.k9.mail.Part) BodyPart(com.fsck.k9.mail.BodyPart) ArrayList(java.util.ArrayList) Html(com.fsck.k9.mail.internet.Viewable.Html) Body(com.fsck.k9.mail.Body)

Aggregations

Attachment (com.fsck.k9.activity.misc.Attachment)8 Test (org.junit.Test)8 MessagingException (com.fsck.k9.mail.MessagingException)7 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)7 Part (com.fsck.k9.mail.Part)6 Body (com.fsck.k9.mail.Body)5 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)5 BodyPart (com.fsck.k9.mail.BodyPart)4 Multipart (com.fsck.k9.mail.Multipart)4 AttachmentViewInfo (com.fsck.k9.mailstore.AttachmentViewInfo)4 File (java.io.File)4 ArrayList (java.util.ArrayList)4 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)3 SQLiteException (android.database.sqlite.SQLiteException)2 Uri (android.net.Uri)2 Address (com.fsck.k9.mail.Address)2 TextBody (com.fsck.k9.mail.internet.TextBody)2 Html (com.fsck.k9.mail.internet.Viewable.Html)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2