diff --git a/src/com/fsck/k9/mail/internet/MimeUtility.java b/src/com/fsck/k9/mail/internet/MimeUtility.java index 0b8caf71f..0405fd567 100644 --- a/src/com/fsck/k9/mail/internet/MimeUtility.java +++ b/src/com/fsck/k9/mail/internet/MimeUtility.java @@ -3260,4 +3260,61 @@ public class MimeUtility { return charset; } + + public static ViewableContainer extractPartsFromDraft(Message message) + throws MessagingException { + + Body body = message.getBody(); + if (message.isMimeType("multipart/mixed") && body instanceof MimeMultipart) { + MimeMultipart multipart = (MimeMultipart) body; + + ViewableContainer container; + int count = multipart.getCount(); + if (count >= 1) { + // The first part is either a text/plain or a multipart/alternative + BodyPart firstPart = multipart.getBodyPart(0); + container = extractTextual(firstPart); + + // The rest should be attachments + for (int i = 1; i < count; i++) { + BodyPart bodyPart = multipart.getBodyPart(i); + container.attachments.add(bodyPart); + } + } else { + container = new ViewableContainer(null, null, new ArrayList()); + } + + return container; + } + + return extractTextual(message); + } + + private static ViewableContainer extractTextual(Part part) throws MessagingException { + String text = null; + String html = null; + List attachments = new ArrayList(); + + Body firstBody = part.getBody(); + if (part.isMimeType("text/plain") && + firstBody instanceof TextBody) { + text = ((TextBody) firstBody).getText(); + } else if (part.isMimeType("multipart/alternative") && + firstBody instanceof MimeMultipart) { + MimeMultipart multipart = (MimeMultipart) firstBody; + for (int i = 0, count = multipart.getCount(); i < count; i++) { + BodyPart bodyPart = multipart.getBodyPart(i); + if (bodyPart.getBody() instanceof TextBody) { + TextBody textBody = (TextBody) bodyPart.getBody(); + if (text == null && bodyPart.isMimeType("text/plain")) { + text = textBody.getText(); + } else if (html == null && bodyPart.isMimeType("text/html")) { + html = textBody.getText(); + } + } + } + } + + return new ViewableContainer(text, html, attachments); + } } diff --git a/src/com/fsck/k9/mail/store/LocalStore.java b/src/com/fsck/k9/mail/store/LocalStore.java index f9d769511..64d89610c 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -2134,12 +2134,29 @@ public class LocalStore extends Store implements Serializable { deleteAttachments(message.getUid()); } - ViewableContainer container = - MimeUtility.extractTextAndAttachments(mApplication, message); + boolean isDraft = (message.getHeader(K9.IDENTITY_HEADER) != null); - List attachments = container.attachments; - String text = container.text; - String html = HtmlConverter.convertEmoji2Img(container.html); + List attachments; + String text; + String html; + if (isDraft) { + // Don't modify the text/plain or text/html part of our own + // draft messages because this will cause the values stored in + // the identity header to be wrong. + ViewableContainer container = + MimeUtility.extractPartsFromDraft(message); + + text = container.text; + html = container.html; + attachments = container.attachments; + } else { + ViewableContainer container = + MimeUtility.extractTextAndAttachments(mApplication, message); + + attachments = container.attachments; + text = container.text; + html = HtmlConverter.convertEmoji2Img(container.html); + } String preview = calculateContentPreview(text);