1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-30 21:22:26 -05:00

Restructured and documented MessageCompose.buildText()

Also fixed a bug where the signature wasn't appended in the HTML part of
newly composed messages.
This commit is contained in:
cketti 2012-01-10 01:47:23 +01:00
parent a191415860
commit 0d8497b04b

View File

@ -983,125 +983,153 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
return buildText(isDraft, mMessageFormat); return buildText(isDraft, mMessageFormat);
} }
/* /**
* Build the Body that will contain the text of the message. We'll decide where to * Build the {@link Body} that will contain the text of the message.
* include it later. Draft messages are treated somewhat differently in that signatures are not *
* appended and HTML separators between composed text and quoted text are not added. * <p>
* @param isDraft If we should build a message that will be saved as a draft (as opposed to sent). * Draft messages are treated somewhat differently in that signatures are not appended and HTML
* @param messageFormat Set MessageFormat to build. * separators between composed text and quoted text are not added.
* </p>
*
* @param isDraft
* If {@code true} we build a message that will be saved as a draft (as opposed to
* sent).
* @param messageFormat
* Specifies what type of message to build ({@code text/plain} vs. {@code text/html}).
*
* @return {@link TextBody} instance that contains the entered text and possibly the quoted
* original message.
*/ */
private TextBody buildText(boolean isDraft, MessageFormat messageFormat) { private TextBody buildText(boolean isDraft, MessageFormat messageFormat) {
boolean replyAfterQuote = false; // The length of the formatted version of the user-supplied text/reply
String action = getIntent().getAction(); int composedMessageLength;
if (mAccount.isReplyAfterQuote() &&
(ACTION_REPLY.equals(action) || ACTION_REPLY_ALL.equals(action) ||
ACTION_EDIT_DRAFT.equals(action))) {
replyAfterQuote = true;
}
// The offset of the user-supplied text/reply in the final text body
int composedMessageOffset;
/*
* Find out if there is some text to quote.
*
* We include the quoted text in the body if the user didn't choose to hide it. We always
* include the quoted text when we're saving a draft. That's so the user is able to
* "un-hide" the quoted text if (s)he opens a saved draft.
*/
boolean includeQuotedText = (mQuotedHtmlContent != null &&
(mQuotedTextMode.equals(QuotedTextMode.SHOW) || isDraft));
// Reply after quote makes no sense for HEADER style replies
boolean replyAfterQuote = (mQuoteStyle == QuoteStyle.HEADER) ?
false : mAccount.isReplyAfterQuote();
boolean signatureBeforeQuotedText = mAccount.isSignatureBeforeQuotedText();
// Get the user-supplied text
String text = mMessageContentView.getText().toString(); String text = mMessageContentView.getText().toString();
boolean saveQuotedText = false; // Handle HTML separate from the rest of the text content
if (isDraft || mQuotedTextMode.equals(QuotedTextMode.SHOW)) { if (messageFormat == MessageFormat.HTML) {
saveQuotedText = true;
// Do we have to modify an existing message to include our reply?
if (includeQuotedText) {
if (K9.DEBUG) {
Log.d(K9.LOG_TAG, "insertable: " + mQuotedHtmlContent.toDebugString());
} }
// Handle HTML separate from the rest of the text content. HTML mode doesn't allow signature after the quoted
// text, nor does it allow reply after quote. Users who want that functionality will need to stick with text
// mode.
if (messageFormat == MessageFormat.HTML) {
// Place the signature immediately after the reply.
if (!isDraft) { if (!isDraft) {
if (mQuoteStyle == QuoteStyle.HEADER || replyAfterQuote || mAccount.isSignatureBeforeQuotedText()) { // Append signature to the reply
if (replyAfterQuote || signatureBeforeQuotedText) {
text = appendSignature(text); text = appendSignature(text);
} }
} }
text = HtmlConverter.textToHtmlFragment(text);
// Insert it into the existing content object.
if (K9.DEBUG && mQuotedHtmlContent != null)
Log.d(K9.LOG_TAG, "insertable: " + mQuotedHtmlContent.toDebugString());
if (mQuotedHtmlContent != null && saveQuotedText) {
// Set the insertion location based upon our reply after quote setting. Reply after // Convert the text to HTML
// quote makes no sense for HEADER style replies. In addition, add some extra text = HtmlConverter.textToHtmlFragment(text);
// separators between the composed message and quoted message depending on the quote
// location. We only add the extra separators when we're sending, that way when we /*
// load a draft, we don't have to know the length of the separators to remove them * Set the insertion location based upon our reply after quote setting.
// before editing. * Additionally, add some extra separators between the composed message and quoted
if (mQuoteStyle == QuoteStyle.PREFIX && replyAfterQuote) { * message depending on the quote location. We only add the extra separators when
mQuotedHtmlContent.setInsertionLocation(InsertableHtmlContent.InsertionLocation.AFTER_QUOTE); * we're sending, that way when we load a draft, we don't have to know the length
* of the separators to remove them before editing.
*/
if (replyAfterQuote) {
mQuotedHtmlContent.setInsertionLocation(
InsertableHtmlContent.InsertionLocation.AFTER_QUOTE);
if (!isDraft) { if (!isDraft) {
text = "<br clear=\"all\">" + text; text = "<br clear=\"all\">" + text;
} }
} else { } else {
mQuotedHtmlContent.setInsertionLocation(InsertableHtmlContent.InsertionLocation.BEFORE_QUOTE); mQuotedHtmlContent.setInsertionLocation(
InsertableHtmlContent.InsertionLocation.BEFORE_QUOTE);
if (!isDraft) { if (!isDraft) {
text += "<br><br>"; text += "<br><br>";
} }
} }
// Place signature immediately after quote.
if (!isDraft) { if (!isDraft) {
if (mQuoteStyle == QuoteStyle.PREFIX && !replyAfterQuote && !mAccount.isSignatureBeforeQuotedText()) { // Place signature immediately after the quoted text
if (!(replyAfterQuote || signatureBeforeQuotedText)) {
mQuotedHtmlContent.insertIntoQuotedFooter(getSignatureHtml()); mQuotedHtmlContent.insertIntoQuotedFooter(getSignatureHtml());
} }
} }
mQuotedHtmlContent.setUserContent(text); mQuotedHtmlContent.setUserContent(text);
// All done. Build the body.
TextBody body = new TextBody(mQuotedHtmlContent.toString());
// Save length of the body and its offset. This is used when thawing drafts. // Save length of the body and its offset. This is used when thawing drafts.
body.setComposedMessageLength(text.length()); composedMessageLength = text.length();
body.setComposedMessageOffset(mQuotedHtmlContent.getInsertionPoint()); composedMessageOffset = mQuotedHtmlContent.getInsertionPoint();
return body; text = mQuotedHtmlContent.toString();
} else { } else {
TextBody body = new TextBody(text); // There is no text to quote so simply append the signature if available
body.setComposedMessageLength(text.length()); if (!isDraft) {
// Not in reply to anything so the message starts at the beginning (0). text = appendSignature(text);
body.setComposedMessageOffset(0);
return body;
} }
} else if (messageFormat == MessageFormat.TEXT) {
// Convert the text to HTML
text = HtmlConverter.textToHtmlFragment(text);
//TODO: Wrap this in proper HTML tags
composedMessageLength = text.length();
composedMessageOffset = 0;
}
} else {
// Capture composed message length before we start attaching quoted parts and signatures. // Capture composed message length before we start attaching quoted parts and signatures.
Integer composedMessageLength = text.length(); composedMessageLength = text.length();
Integer composedMessageOffset = 0; composedMessageOffset = 0;
// Placing the signature before the quoted text does not make sense if replyAfterQuote is true.
if (!isDraft) { if (!isDraft) {
if (mQuoteStyle == QuoteStyle.HEADER || // Append signature to the text/reply
(!replyAfterQuote && mAccount.isSignatureBeforeQuotedText())) { if (replyAfterQuote || signatureBeforeQuotedText) {
text = appendSignature(text); text = appendSignature(text);
} }
} }
if (saveQuotedText) { if (includeQuotedText) {
if (mQuoteStyle == QuoteStyle.PREFIX && replyAfterQuote) { String quotedText = mQuotedText.getText().toString();
composedMessageOffset = mQuotedText.getText().toString().length() + "\n".length(); if (replyAfterQuote) {
text = mQuotedText.getText().toString() + "\n" + text; composedMessageOffset = quotedText.length() + "\n".length();
text = quotedText + "\n" + text;
} else { } else {
text += "\n\n" + mQuotedText.getText().toString(); text += "\n\n" + quotedText.toString();
} }
} }
// Note: If user has selected reply after quote AND signature before quote, ignore the
// latter setting and append the signature at the end.
if (!isDraft) { if (!isDraft) {
if (mQuoteStyle == QuoteStyle.PREFIX && // Place signature immediately after the quoted text
(replyAfterQuote || !mAccount.isSignatureBeforeQuotedText())) { if (!(replyAfterQuote || signatureBeforeQuotedText)) {
text = appendSignature(text); text = appendSignature(text);
} }
} }
// Build the body. }
TextBody body = new TextBody(text); TextBody body = new TextBody(text);
body.setComposedMessageLength(composedMessageLength); body.setComposedMessageLength(composedMessageLength);
body.setComposedMessageOffset(composedMessageOffset); body.setComposedMessageOffset(composedMessageOffset);
return body; return body;
} else {
// Shouldn't happen.
return new TextBody("");
}
} }
/** /**