diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/AttachmentViewInfo.java b/k9mail/src/main/java/com/fsck/k9/mailstore/AttachmentViewInfo.java index 8b0bb3050..f969a3c00 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/AttachmentViewInfo.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/AttachmentViewInfo.java @@ -7,6 +7,8 @@ import com.fsck.k9.mail.Part; public class AttachmentViewInfo { + public static final long UNKNOWN_SIZE = -1; + public final String mimeType; public final String displayName; public final long size; diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalBodyPart.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalBodyPart.java index 3ea9820a6..ebab22c60 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalBodyPart.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalBodyPart.java @@ -8,11 +8,18 @@ import com.fsck.k9.mail.internet.MimeBodyPart; public class LocalBodyPart extends MimeBodyPart implements LocalPart { private final String accountUuid; private final long messagePartId; + private final String displayName; + private final long size; + private final boolean firstClassAttachment; - public LocalBodyPart(String accountUuid, long messagePartId) throws MessagingException { + public LocalBodyPart(String accountUuid, long messagePartId, String displayName, long size, + boolean firstClassAttachment) throws MessagingException { super(); this.accountUuid = accountUuid; this.messagePartId = messagePartId; + this.displayName = displayName; + this.size = size; + this.firstClassAttachment = firstClassAttachment; } @Override @@ -24,4 +31,19 @@ public class LocalBodyPart extends MimeBodyPart implements LocalPart { public long getId() { return messagePartId; } + + @Override + public String getDisplayName() { + return displayName; + } + + @Override + public long getSize() { + return size; + } + + @Override + public boolean isFirstClassAttachment() { + return firstClassAttachment; + } } diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java index 708681241..1d2b8c3b9 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java @@ -24,6 +24,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; +import android.net.Uri; import android.util.Log; import com.fsck.k9.Account; @@ -42,7 +43,6 @@ import com.fsck.k9.mail.MessageRetrievalListener; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.Multipart; import com.fsck.k9.mail.Part; -import com.fsck.k9.mail.internet.MimeBodyPart; import com.fsck.k9.mail.internet.MimeHeader; import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.internet.MimeMultipart; @@ -61,6 +61,7 @@ import org.apache.james.mime4j.util.MimeUtil; public class LocalFolder extends Folder implements Serializable { private static final long serialVersionUID = -1973296520918624767L; + private static final Uri PLACEHOLDER_URI = Uri.EMPTY; private final LocalStore localStore; @@ -669,11 +670,15 @@ public class LocalFolder extends Folder implements Serializable { throws MessagingException { long id = cursor.getLong(0); + int type = cursor.getInt(1); long parentId = cursor.getLong(2); String mimeType = cursor.getString(3); + long size = cursor.getLong(4); + String displayName = cursor.getString(5); byte[] header = cursor.getBlob(6); int dataLocation = cursor.getInt(9); String serverExtra = cursor.getString(15); + boolean firstClassAttachment = (type != MessagePartType.HIDDEN_ATTACHMENT); final Part part; if (id == message.getMessagePartId()) { @@ -686,7 +691,7 @@ public class LocalFolder extends Folder implements Serializable { String parentMimeType = parentPart.getMimeType(); if (parentMimeType.startsWith("multipart/")) { - BodyPart bodyPart = new LocalBodyPart(getAccountUuid(), id); + BodyPart bodyPart = new LocalBodyPart(getAccountUuid(), id, displayName, size, firstClassAttachment); ((Multipart) parentPart.getBody()).addBodyPart(bodyPart); part = bodyPart; } else if (parentMimeType.startsWith("message/")) { @@ -1353,18 +1358,32 @@ public class LocalFolder extends Folder implements Serializable { cv.put("preamble", multipart.getPreamble()); cv.put("epilogue", multipart.getEpilogue()); cv.put("boundary", multipart.getBoundary()); - } else if (body == null) { - //TODO: deal with missing parts - cv.put("data_location", DataLocation.MISSING); } else { - cv.put("data_location", DataLocation.IN_DATABASE); + AttachmentViewInfo attachment = LocalMessageExtractor.extractAttachmentInfo(part, PLACEHOLDER_URI); - byte[] bodyData = getBodyBytes(body); - String encoding = getTransferEncoding(part); + cv.put("display_name", attachment.displayName); - cv.put("encoding", encoding); - cv.put("data", bodyData); - cv.put("content_id", part.getContentId()); + if (body == null) { + //TODO: deal with missing parts + cv.put("data_location", DataLocation.MISSING); + cv.put("decoded_body_size", attachment.size); + } else { + cv.put("data_location", DataLocation.IN_DATABASE); + + byte[] bodyData = getBodyBytes(body); + String encoding = getTransferEncoding(part); + + if (attachment.size == AttachmentViewInfo.UNKNOWN_SIZE) { + //FIXME: Use size of content when transfer encoding is stripped + cv.put("decoded_body_size", bodyData.length); + } else { + cv.put("decoded_body_size", attachment.size); + } + + cv.put("encoding", encoding); + cv.put("data", bodyData); + cv.put("content_id", part.getContentId()); + } } } @@ -1941,6 +1960,7 @@ public class LocalFolder extends Folder implements Serializable { static final int TEXT = 3; static final int RELATED = 4; static final int ATTACHMENT = 5; + static final int HIDDEN_ATTACHMENT = 6; } // Note: The contents of the 'message_parts' table depend on these values. diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java index a7d5bc59b..f0ab12dd7 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java @@ -462,6 +462,23 @@ public class LocalMessageExtractor { } private static AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { + if (part instanceof LocalPart) { + LocalPart localPart = (LocalPart) part; + String accountUuid = localPart.getAccountUuid(); + long messagePartId = localPart.getId(); + String mimeType = part.getMimeType(); + String displayName = localPart.getDisplayName(); + long size = localPart.getSize(); + boolean firstClassAttachment = localPart.isFirstClassAttachment(); + Uri uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); + + return new AttachmentViewInfo(mimeType, displayName, size, uri, firstClassAttachment, part); + } else { + throw new IllegalStateException("Not supported yet"); + } + } + + public static AttachmentViewInfo extractAttachmentInfo(Part part, Uri uri) throws MessagingException { boolean firstClassAttachment = true; String mimeType = part.getMimeType(); @@ -488,7 +505,7 @@ public class LocalMessageExtractor { firstClassAttachment = false; } - long size = 0; + long size = AttachmentViewInfo.UNKNOWN_SIZE; String sizeParam = MimeUtility.getHeaderParameter(contentDisposition, "size"); if (sizeParam != null) { try { @@ -496,11 +513,6 @@ public class LocalMessageExtractor { } catch (NumberFormatException e) { /* ignore */ } } - LocalPart localPart = (LocalPart) part; - String accountUuid = localPart.getAccountUuid(); - long messagePartId = localPart.getId(); - Uri uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); - return new AttachmentViewInfo(mimeType, name, size, uri, firstClassAttachment, part); } } diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalPart.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalPart.java index 239595716..6fb9c50fd 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalPart.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalPart.java @@ -4,4 +4,7 @@ package com.fsck.k9.mailstore; public interface LocalPart { String getAccountUuid(); long getId(); + String getDisplayName(); + long getSize(); + boolean isFirstClassAttachment(); }