From 396005974a269d03f66785f53aa91adbd4c2539d Mon Sep 17 00:00:00 2001 From: cketti Date: Thu, 16 Feb 2012 21:33:53 +0100 Subject: [PATCH] Cleaned up ImapStore.ImapFolder.appendMessages() --- src/com/fsck/k9/mail/store/ImapStore.java | 85 +++++++++++++---------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/src/com/fsck/k9/mail/store/ImapStore.java b/src/com/fsck/k9/mail/store/ImapStore.java index abd8cd2e2..461339d41 100644 --- a/src/com/fsck/k9/mail/store/ImapStore.java +++ b/src/com/fsck/k9/mail/store/ImapStore.java @@ -54,7 +54,6 @@ import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.PowerManager; -import android.text.TextUtils; import android.util.Log; import com.beetstra.jutf7.CharsetProvider; @@ -62,6 +61,7 @@ import com.fsck.k9.Account; import com.fsck.k9.K9; import com.fsck.k9.R; import com.fsck.k9.controller.MessageRetrievalListener; +import com.fsck.k9.helper.StringUtils; import com.fsck.k9.helper.Utility; import com.fsck.k9.helper.power.TracingPowerManager; import com.fsck.k9.helper.power.TracingPowerManager.TracingWakeLock; @@ -1099,7 +1099,6 @@ public class ImapStore extends Store { do { response = mConnection.readResponse(); handleUntaggedResponse(response); - while (response.more()); } 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 - * the new UID of the given message on the IMAP server and sets the Message's UID to the - * new server UID. + * Appends the given messages to the selected folder. + * + *

+ * 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. + *

+ * + * @param messages + * The messages to append to the folder. + * + * @return The mapping of original message UIDs to the new server UIDs. */ @Override public Map appendMessages(Message[] messages) throws MessagingException { checkOpen(); try { - Map uidMap = null; + Map uidMap = new HashMap(); for (Message message : messages) { mConnection.sendCommand( String.format("APPEND %s (%s) {%d}", encodeString(encodeFolderName(getPrefixedName())), combineFlags(message.getFlags()), message.calculateSize()), false); + ImapResponse response; do { response = mConnection.readResponse(); @@ -1981,53 +1989,54 @@ public class ImapStore extends Store { } } while (response.mTag == null); - /* - * 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 - * - * We can use the UID included in this response to update our records. - * - * Code imported from AOSP Email as of git rev a5c3744a247e432acf9f571a9dfb55321c4baa1a - */ - Object responseList = response.get(1); + 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 + * + * We can use the UID included in this response to update our records. + */ + Object responseList = response.get(1); - if (responseList instanceof ImapList) { - final ImapList appendList = (ImapList) responseList; - if ((appendList.size() >= 3) && appendList.getString(0).equals("APPENDUID")) { - String newUid = appendList.getString(2); + if (responseList instanceof ImapList) { + ImapList appendList = (ImapList) responseList; + if (appendList.size() >= 3 && + appendList.getString(0).equals("APPENDUID")) { - /* - * 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 (uidMap == null) { - uidMap = new HashMap(); - } - uidMap.put(message.getUid(), newUid); - if (!TextUtils.isEmpty(newUid)) { - message.setUid(newUid); - continue; + String newUid = appendList.getString(2); + + if (!StringUtils.isNullOrEmpty(newUid)) { + message.setUid(newUid); + uidMap.put(message.getUid(), 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); - if (K9.DEBUG) + if (K9.DEBUG) { Log.d(K9.LOG_TAG, "Got UID " + newUid + " for message for " + getLogId()); + } - if (newUid != null) { - if (uidMap == null) { - uidMap = new HashMap(); - } + if (!StringUtils.isNullOrEmpty(newUid)) { uidMap.put(message.getUid(), 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) { throw ioExceptionHandler(mConnection, ioe); }