1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 19:52:17 -05:00
This commit is contained in:
Jan Berkel 2014-12-15 13:01:13 +01:00
parent 2a2e18e8b6
commit b443af43ae
4 changed files with 39 additions and 32 deletions

View File

@ -22,6 +22,9 @@ import java.util.regex.Pattern;
import static com.fsck.k9.mail.internet.CharsetSupport.fixupCharset; import static com.fsck.k9.mail.internet.CharsetSupport.fixupCharset;
import static com.fsck.k9.mail.internet.MimeUtility.getHeaderParameter; import static com.fsck.k9.mail.internet.MimeUtility.getHeaderParameter;
import static com.fsck.k9.mail.internet.Viewable.Alternative; import static com.fsck.k9.mail.internet.Viewable.Alternative;
import static com.fsck.k9.mail.internet.Viewable.Html;
import static com.fsck.k9.mail.internet.Viewable.MessageHeader;
import static com.fsck.k9.mail.internet.Viewable.Text;
import static com.fsck.k9.mail.internet.Viewable.Textual; import static com.fsck.k9.mail.internet.Viewable.Textual;
public class MessageExtractor { public class MessageExtractor {
@ -167,7 +170,7 @@ public class MessageExtractor {
Message message = (Message) body; Message message = (Message) body;
// We add the Message object so we can extract the filename later. // We add the Message object so we can extract the filename later.
viewables.add(new Viewable.MessageHeader(part, message)); viewables.add(new MessageHeader(part, message));
// Recurse to grab all viewable parts and attachments from that message. // Recurse to grab all viewable parts and attachments from that message.
viewables.addAll(getViewables(message, attachments)); viewables.addAll(getViewables(message, attachments));
@ -177,10 +180,10 @@ public class MessageExtractor {
*/ */
String mimeType = part.getMimeType(); String mimeType = part.getMimeType();
if (mimeType.equalsIgnoreCase("text/plain")) { if (mimeType.equalsIgnoreCase("text/plain")) {
Viewable.Text text = new Viewable.Text(part); Text text = new Text(part);
viewables.add(text); viewables.add(text);
} else { } else {
Viewable.Html html = new Viewable.Html(part); Html html = new Html(part);
viewables.add(html); viewables.add(html);
} }
} else { } else {
@ -220,7 +223,7 @@ public class MessageExtractor {
* @param directChild If {@code true}, this method will return after the first {@code text/plain} was * @param directChild If {@code true}, this method will return after the first {@code text/plain} was
* found. * found.
* *
* @return A list of {@link Viewable.Text} viewables. * @return A list of {@link Text} viewables.
* *
* @throws MessagingException * @throws MessagingException
* In case of an error. * In case of an error.
@ -254,7 +257,7 @@ public class MessageExtractor {
} }
} }
} else if (isPartTextualBody(part) && part.getMimeType().equalsIgnoreCase("text/plain")) { } else if (isPartTextualBody(part) && part.getMimeType().equalsIgnoreCase("text/plain")) {
Viewable.Text text = new Viewable.Text(part); Text text = new Text(part);
viewables.add(text); viewables.add(text);
if (directChild) { if (directChild) {
break; break;
@ -274,7 +277,7 @@ public class MessageExtractor {
* @param directChild If {@code true}, this method will add all {@code text/html} parts except the first * @param directChild If {@code true}, this method will add all {@code text/html} parts except the first
* found to 'attachments'. * found to 'attachments'.
* *
* @return A list of {@link Viewable.Text} viewables. * @return A list of {@link Text} viewables.
* *
* @throws MessagingException In case of an error. * @throws MessagingException In case of an error.
*/ */
@ -314,7 +317,7 @@ public class MessageExtractor {
} }
} else if (!(directChild && partFound) && isPartTextualBody(part) && } else if (!(directChild && partFound) && isPartTextualBody(part) &&
part.getMimeType().equalsIgnoreCase("text/html")) { part.getMimeType().equalsIgnoreCase("text/html")) {
Viewable.Html html = new Viewable.Html(part); Html html = new Html(part);
viewables.add(html); viewables.add(html);
partFound = true; partFound = true;
} else if (!knownTextParts.contains(part)) { } else if (!knownTextParts.contains(part)) {
@ -359,8 +362,8 @@ public class MessageExtractor {
* *
* @return The set of viewable {@code Part}s. * @return The set of viewable {@code Part}s.
* *
* @see MimeUtility#findHtmlPart(Multipart, Set, List, boolean) * @see MessageExtractor#findHtmlPart(Multipart, Set, List, boolean)
* @see MimeUtility#findAttachments(Multipart, Set, List) * @see MessageExtractor#findAttachments(Multipart, Set, List)
*/ */
private static Set<Part> getParts(List<Viewable> viewables) { private static Set<Part> getParts(List<Viewable> viewables) {
Set<Part> parts = new HashSet<Part>(); Set<Part> parts = new HashSet<Part>();

View File

@ -991,7 +991,7 @@ public class MimeUtility {
*/ */
if (contentTransferEncoding != null) { if (contentTransferEncoding != null) {
contentTransferEncoding = contentTransferEncoding =
MimeUtility.getHeaderParameter(contentTransferEncoding, null); getHeaderParameter(contentTransferEncoding, null);
if (MimeUtil.ENC_QUOTED_PRINTABLE.equalsIgnoreCase(contentTransferEncoding)) { if (MimeUtil.ENC_QUOTED_PRINTABLE.equalsIgnoreCase(contentTransferEncoding)) {
in = new QuotedPrintableInputStream(in); in = new QuotedPrintableInputStream(in);
} else if (MimeUtil.ENC_BASE64.equalsIgnoreCase(contentTransferEncoding)) { } else if (MimeUtil.ENC_BASE64.equalsIgnoreCase(contentTransferEncoding)) {

View File

@ -7,7 +7,7 @@ import java.util.List;
/** /**
* Empty marker class interface the class hierarchy used by * Empty marker class interface the class hierarchy used by
* {@link com.fsck.k9.mailstore.LocalMessageExtractor#extractTextAndAttachments(android.content.Context, com.fsck.k9.mail.Message)}. * {@link MessageExtractor#getViewables(com.fsck.k9.mail.Part, java.util.List)}
* *
* @see Viewable.Text * @see Viewable.Text
* @see Viewable.Html * @see Viewable.Html
@ -20,7 +20,7 @@ public interface Viewable {
* *
* @see com.fsck.k9.mail.internet.MessageExtractor#isPartTextualBody(com.fsck.k9.mail.Part) * @see com.fsck.k9.mail.internet.MessageExtractor#isPartTextualBody(com.fsck.k9.mail.Part)
*/ */
static abstract class Textual implements Viewable { abstract class Textual implements Viewable {
private Part mPart; private Part mPart;
public Textual(Part part) { public Textual(Part part) {
@ -35,7 +35,7 @@ public interface Viewable {
/** /**
* Class representing a {@code text/plain} part of a message. * Class representing a {@code text/plain} part of a message.
*/ */
static class Text extends Textual { class Text extends Textual {
public Text(Part part) { public Text(Part part) {
super(part); super(part);
} }
@ -44,7 +44,7 @@ public interface Viewable {
/** /**
* Class representing a {@code text/html} part of a message. * Class representing a {@code text/html} part of a message.
*/ */
static class Html extends Textual { class Html extends Textual {
public Html(Part part) { public Html(Part part) {
super(part); super(part);
} }
@ -58,7 +58,7 @@ public interface Viewable {
* inline. * inline.
* </p> * </p>
*/ */
static class MessageHeader implements Viewable { class MessageHeader implements Viewable {
private Part mContainerPart; private Part mContainerPart;
private Message mMessage; private Message mMessage;
@ -84,7 +84,7 @@ public interface Viewable {
* class. * class.
* </p> * </p>
*/ */
static class Alternative implements Viewable { class Alternative implements Viewable {
private List<Viewable> mText; private List<Viewable> mText;
private List<Viewable> mHtml; private List<Viewable> mHtml;
@ -101,5 +101,4 @@ public interface Viewable {
return mHtml; return mHtml;
} }
} }
} }

View File

@ -19,6 +19,11 @@ import java.util.Date;
import java.util.List; import java.util.List;
import static com.fsck.k9.mail.internet.MimeUtility.getHeaderParameter; import static com.fsck.k9.mail.internet.MimeUtility.getHeaderParameter;
import static com.fsck.k9.mail.internet.Viewable.Alternative;
import static com.fsck.k9.mail.internet.Viewable.Html;
import static com.fsck.k9.mail.internet.Viewable.MessageHeader;
import static com.fsck.k9.mail.internet.Viewable.Text;
import static com.fsck.k9.mail.internet.Viewable.Textual;
class LocalMessageExtractor { class LocalMessageExtractor {
private static final String TEXT_DIVIDER = private static final String TEXT_DIVIDER =
@ -58,14 +63,14 @@ class LocalMessageExtractor {
StringBuilder html = new StringBuilder(); StringBuilder html = new StringBuilder();
for (Viewable viewable : viewables) { for (Viewable viewable : viewables) {
if (viewable instanceof Viewable.Textual) { if (viewable instanceof Textual) {
// This is either a text/plain or text/html part. Fill the variables 'text' and // This is either a text/plain or text/html part. Fill the variables 'text' and
// 'html', converting between plain text and HTML as necessary. // 'html', converting between plain text and HTML as necessary.
text.append(buildText(viewable, !hideDivider)); text.append(buildText(viewable, !hideDivider));
html.append(buildHtml(viewable, !hideDivider)); html.append(buildHtml(viewable, !hideDivider));
hideDivider = false; hideDivider = false;
} else if (viewable instanceof Viewable.MessageHeader) { } else if (viewable instanceof MessageHeader) {
Viewable.MessageHeader header = (Viewable.MessageHeader) viewable; MessageHeader header = (MessageHeader) viewable;
Part containerPart = header.getContainerPart(); Part containerPart = header.getContainerPart();
Message innerMessage = header.getMessage(); Message innerMessage = header.getMessage();
@ -76,9 +81,9 @@ class LocalMessageExtractor {
addMessageHeaderHtml(context, html, innerMessage); addMessageHeaderHtml(context, html, innerMessage);
hideDivider = true; hideDivider = true;
} else if (viewable instanceof Viewable.Alternative) { } else if (viewable instanceof Alternative) {
// Handle multipart/alternative contents // Handle multipart/alternative contents
Viewable.Alternative alternative = (Viewable.Alternative) viewable; Alternative alternative = (Alternative) viewable;
/* /*
* We made sure at least one of text/plain or text/html is present when * We made sure at least one of text/plain or text/html is present when
@ -161,21 +166,21 @@ class LocalMessageExtractor {
private static StringBuilder buildHtml(Viewable viewable, boolean prependDivider) private static StringBuilder buildHtml(Viewable viewable, boolean prependDivider)
{ {
StringBuilder html = new StringBuilder(); StringBuilder html = new StringBuilder();
if (viewable instanceof Viewable.Textual) { if (viewable instanceof Textual) {
Part part = ((Viewable.Textual)viewable).getPart(); Part part = ((Textual)viewable).getPart();
addHtmlDivider(html, part, prependDivider); addHtmlDivider(html, part, prependDivider);
String t = part.getText(); String t = part.getText();
if (t == null) { if (t == null) {
t = ""; t = "";
} else if (viewable instanceof Viewable.Text) { } else if (viewable instanceof Text) {
t = HtmlConverter.textToHtml(t); t = HtmlConverter.textToHtml(t);
} }
html.append(t); html.append(t);
} else if (viewable instanceof Viewable.Alternative) { } else if (viewable instanceof Alternative) {
// That's odd - an Alternative as child of an Alternative; go ahead and try to use the // That's odd - an Alternative as child of an Alternative; go ahead and try to use the
// text/html child; fall-back to the text/plain part. // text/html child; fall-back to the text/plain part.
Viewable.Alternative alternative = (Viewable.Alternative) viewable; Alternative alternative = (Alternative) viewable;
List<Viewable> htmlAlternative = alternative.getHtml().isEmpty() ? List<Viewable> htmlAlternative = alternative.getHtml().isEmpty() ?
alternative.getText() : alternative.getHtml(); alternative.getText() : alternative.getHtml();
@ -193,21 +198,21 @@ class LocalMessageExtractor {
private static StringBuilder buildText(Viewable viewable, boolean prependDivider) private static StringBuilder buildText(Viewable viewable, boolean prependDivider)
{ {
StringBuilder text = new StringBuilder(); StringBuilder text = new StringBuilder();
if (viewable instanceof Viewable.Textual) { if (viewable instanceof Textual) {
Part part = ((Viewable.Textual)viewable).getPart(); Part part = ((Textual)viewable).getPart();
addTextDivider(text, part, prependDivider); addTextDivider(text, part, prependDivider);
String t = part.getText(); String t = part.getText();
if (t == null) { if (t == null) {
t = ""; t = "";
} else if (viewable instanceof Viewable.Html) { } else if (viewable instanceof Html) {
t = HtmlConverter.htmlToText(t); t = HtmlConverter.htmlToText(t);
} }
text.append(t); text.append(t);
} else if (viewable instanceof Viewable.Alternative) { } else if (viewable instanceof Alternative) {
// That's odd - an Alternative as child of an Alternative; go ahead and try to use the // That's odd - an Alternative as child of an Alternative; go ahead and try to use the
// text/plain child; fall-back to the text/html part. // text/plain child; fall-back to the text/html part.
Viewable.Alternative alternative = (Viewable.Alternative) viewable; Alternative alternative = (Alternative) viewable;
List<Viewable> textAlternative = alternative.getText().isEmpty() ? List<Viewable> textAlternative = alternative.getText().isEmpty() ?
alternative.getHtml() : alternative.getText(); alternative.getHtml() : alternative.getText();