Cleaned up ImapStore.ImapFolder.appendMessages()

This commit is contained in:
cketti 2012-02-16 21:33:53 +01:00
parent 8e1627e1b9
commit 396005974a

View file

@ -54,7 +54,6 @@ import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.os.PowerManager; import android.os.PowerManager;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.beetstra.jutf7.CharsetProvider; import com.beetstra.jutf7.CharsetProvider;
@ -62,6 +61,7 @@ import com.fsck.k9.Account;
import com.fsck.k9.K9; import com.fsck.k9.K9;
import com.fsck.k9.R; import com.fsck.k9.R;
import com.fsck.k9.controller.MessageRetrievalListener; import com.fsck.k9.controller.MessageRetrievalListener;
import com.fsck.k9.helper.StringUtils;
import com.fsck.k9.helper.Utility; import com.fsck.k9.helper.Utility;
import com.fsck.k9.helper.power.TracingPowerManager; import com.fsck.k9.helper.power.TracingPowerManager;
import com.fsck.k9.helper.power.TracingPowerManager.TracingWakeLock; import com.fsck.k9.helper.power.TracingPowerManager.TracingWakeLock;
@ -1099,7 +1099,6 @@ public class ImapStore extends Store {
do { do {
response = mConnection.readResponse(); response = mConnection.readResponse();
handleUntaggedResponse(response); handleUntaggedResponse(response);
while (response.more());
} while (response.mTag == null); } while (response.mTag == null);
/* /*
@ -1953,21 +1952,30 @@ public class ImapStore extends Store {
} }
/** /**
* Appends the given messages to the selected folder. This implementation also determines * Appends the given messages to the selected folder.
* the new UID of the given message on the IMAP server and sets the Message's UID to the *
* new server UID. * <p>
* This implementation also determines the new UIDs of the given messages on the IMAP
* server and changes the messages' UIDs to the new server UIDs.
* </p>
*
* @param messages
* The messages to append to the folder.
*
* @return The mapping of original message UIDs to the new server UIDs.
*/ */
@Override @Override
public Map<String, String> appendMessages(Message[] messages) throws MessagingException { public Map<String, String> appendMessages(Message[] messages) throws MessagingException {
checkOpen(); checkOpen();
try { try {
Map<String, String> uidMap = null; Map<String, String> uidMap = new HashMap<String, String>();
for (Message message : messages) { for (Message message : messages) {
mConnection.sendCommand( mConnection.sendCommand(
String.format("APPEND %s (%s) {%d}", String.format("APPEND %s (%s) {%d}",
encodeString(encodeFolderName(getPrefixedName())), encodeString(encodeFolderName(getPrefixedName())),
combineFlags(message.getFlags()), combineFlags(message.getFlags()),
message.calculateSize()), false); message.calculateSize()), false);
ImapResponse response; ImapResponse response;
do { do {
response = mConnection.readResponse(); response = mConnection.readResponse();
@ -1981,53 +1989,54 @@ public class ImapStore extends Store {
} }
} while (response.mTag == null); } while (response.mTag == null);
/* if (response.size() > 1) {
* If the server supports UIDPLUS, then along with the APPEND response it will return an APPENDUID response code. /*
* e.g. 11 OK [APPENDUID 2 238268] APPEND completed * If the server supports UIDPLUS, then along with the APPEND response it
* * will return an APPENDUID response code, e.g.
* We can use the UID included in this response to update our records. *
* * 11 OK [APPENDUID 2 238268] APPEND completed
* Code imported from AOSP Email as of git rev a5c3744a247e432acf9f571a9dfb55321c4baa1a *
*/ * We can use the UID included in this response to update our records.
Object responseList = response.get(1); */
Object responseList = response.get(1);
if (responseList instanceof ImapList) { if (responseList instanceof ImapList) {
final ImapList appendList = (ImapList) responseList; ImapList appendList = (ImapList) responseList;
if ((appendList.size() >= 3) && appendList.getString(0).equals("APPENDUID")) { if (appendList.size() >= 3 &&
String newUid = appendList.getString(2); appendList.getString(0).equals("APPENDUID")) {
/* String newUid = appendList.getString(2);
* We need uidMap to be null initially to maintain consistency with the behavior of other similar methods (copyMessages, moveMessages) which
* return null if new UIDs are not available. Therefore, we initialize uidMap over here. if (!StringUtils.isNullOrEmpty(newUid)) {
*/ message.setUid(newUid);
if (uidMap == null) { uidMap.put(message.getUid(), newUid);
uidMap = new HashMap<String, String>(); continue;
} }
uidMap.put(message.getUid(), newUid);
if (!TextUtils.isEmpty(newUid)) {
message.setUid(newUid);
continue;
} }
} }
} }
/* /*
* This part is executed in case the server does not support UIDPLUS or does not implement the APPENDUID response code. * This part is executed in case the server does not support UIDPLUS or does
* not implement the APPENDUID response code.
*/ */
String newUid = getUidFromMessageId(message); String newUid = getUidFromMessageId(message);
if (K9.DEBUG) if (K9.DEBUG) {
Log.d(K9.LOG_TAG, "Got UID " + newUid + " for message for " + getLogId()); Log.d(K9.LOG_TAG, "Got UID " + newUid + " for message for " + getLogId());
}
if (newUid != null) { if (!StringUtils.isNullOrEmpty(newUid)) {
if (uidMap == null) {
uidMap = new HashMap<String, String>();
}
uidMap.put(message.getUid(), newUid); uidMap.put(message.getUid(), newUid);
message.setUid(newUid); message.setUid(newUid);
} }
} }
return uidMap;
/*
* We need uidMap to be null if new UIDs are not available to maintain consistency
* with the behavior of other similar methods (copyMessages, moveMessages) which
* return null.
*/
return (uidMap.size() == 0) ? null : uidMap;
} catch (IOException ioe) { } catch (IOException ioe) {
throw ioExceptionHandler(mConnection, ioe); throw ioExceptionHandler(mConnection, ioe);
} }