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

Replace CRLF with LF when loading drafts

This is necessary because we save the offset and length of the user-
supplied text in the identity header. These values are then later used
to split the draft in user text and quoted message.
When calculating these values we operate on a string with LF line
endings. Ideally we want to do the reverse operation on the same
string, but when saving the message to the server LF is converted to
CRLF to create RFC-conforming messages.

This is only a hack and will probably be the cause of more trouble in
the future. A better solution would be to make the identity header more
robust or get rid of it entirely.
This commit is contained in:
cketti 2012-03-17 04:15:30 +01:00
parent 3fa8081e88
commit f9a35aeaee

View File

@ -3296,9 +3296,11 @@ public class MimeUtility {
List<Part> attachments = new ArrayList<Part>();
Body firstBody = part.getBody();
if (part.isMimeType("text/plain") &&
firstBody instanceof TextBody) {
text = ((TextBody) firstBody).getText();
if (part.isMimeType("text/plain")) {
String bodyText = getTextFromPart(part);
if (bodyText != null) {
text = fixDraftTextBody(bodyText);
}
} else if (part.isMimeType("multipart/alternative") &&
firstBody instanceof MimeMultipart) {
MimeMultipart multipart = (MimeMultipart) firstBody;
@ -3307,9 +3309,9 @@ public class MimeUtility {
String bodyText = getTextFromPart(bodyPart);
if (bodyText != null) {
if (text.length() == 0 && bodyPart.isMimeType("text/plain")) {
text = bodyText;
text = fixDraftTextBody(bodyText);
} else if (html.length() == 0 && bodyPart.isMimeType("text/html")) {
html = bodyText;
html = fixDraftTextBody(bodyText);
}
}
}
@ -3317,4 +3319,21 @@ public class MimeUtility {
return new ViewableContainer(text, html, attachments);
}
/**
* Fix line endings of text bodies in draft messages.
*
* <p>
* We create drafts with LF line endings. The values in the identity header are based on that.
* So we replace CRLF with LF when loading messages (from the server).
* </p>
*
* @param text
* The body text with CRLF line endings
*
* @return The text with LF line endings
*/
private static String fixDraftTextBody(String text) {
return text.replace("\r\n", "\n");
}
}