1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-25 00:58:50 -05:00

Don't modify draft messages when storing them in the database

This commit is contained in:
cketti 2012-03-15 21:21:00 +01:00
parent aeb0220e56
commit f181e923ca
2 changed files with 79 additions and 5 deletions

View File

@ -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<Part>());
}
return container;
}
return extractTextual(message);
}
private static ViewableContainer extractTextual(Part part) throws MessagingException {
String text = null;
String html = null;
List<Part> attachments = new ArrayList<Part>();
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);
}
}

View File

@ -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<Part> attachments = container.attachments;
String text = container.text;
String html = HtmlConverter.convertEmoji2Img(container.html);
List<Part> 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);