0
0
mirror of https://github.com/thunderbird/thunderbird-android.git synced 2024-09-20 04:02:14 +02:00

Add support for UIDVALIDITY to ImapFolder

This commit is contained in:
cketti 2019-12-18 00:55:10 +01:00
parent 073a00fcb2
commit 5791253122
5 changed files with 120 additions and 0 deletions

View File

@ -67,6 +67,7 @@ public class ImapFolder {
private volatile boolean exists;
private boolean inSearch = false;
private boolean canCreateKeywords = false;
private Long uidValidity = null;
public ImapFolder(ImapStore store, String name) {
@ -88,6 +89,14 @@ public class ImapFolder {
this.type = type;
}
public Long getUidValidity() {
if (!isOpen()) {
throw new IllegalStateException("ImapFolder needs to be open");
}
return uidValidity;
}
private String getPrefixedName() throws MessagingException {
String prefixedName = "";
@ -164,6 +173,7 @@ public class ImapFolder {
this.mode = mode;
for (ImapResponse response : responses) {
extractUidValidity(response);
handlePermanentFlags(response);
}
@ -180,6 +190,13 @@ public class ImapFolder {
}
}
private void extractUidValidity(ImapResponse response) {
UidValidityResponse uidValidityResponse = UidValidityResponse.parse(response);
if (uidValidityResponse != null) {
uidValidity = uidValidityResponse.getUidValidity();
}
}
private void handlePermanentFlags(ImapResponse response) {
PermanentFlagsResponse permanentFlagsResponse = PermanentFlagsResponse.parse(response);
if (permanentFlagsResponse == null) {

View File

@ -42,6 +42,24 @@ class ImapList extends ArrayList<Object> {
return inRange(index) && get(index) instanceof String;
}
public boolean isLong(int index) {
if (!inRange(index)) {
return false;
}
Object value = get(index);
if (!(value instanceof String)) {
return false;
}
try {
Long.parseLong((String) value);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public long getLong(int index) {
return Long.parseLong(getString(index));
}

View File

@ -16,4 +16,5 @@ class Responses {
public static final String PERMANENTFLAGS = "PERMANENTFLAGS";
public static final String COPYUID = "COPYUID";
public static final String SEARCH = "SEARCH";
public static final String UIDVALIDITY = "UIDVALIDITY";
}

View File

@ -0,0 +1,21 @@
package com.fsck.k9.mail.store.imap
import com.fsck.k9.mail.store.imap.ImapResponseParser.equalsIgnoreCase
internal class UidValidityResponse private constructor(val uidValidity: Long) {
companion object {
@JvmStatic
fun parse(response: ImapResponse): UidValidityResponse? {
if (response.isTagged || !equalsIgnoreCase(response[0], Responses.OK) || !response.isList(1)) return null
val responseTextList = response.getList(1)
if (responseTextList.size < 2 || !equalsIgnoreCase(responseTextList[0], Responses.UIDVALIDITY) ||
!responseTextList.isLong(1)) return null
val uidValidity = responseTextList.getLong(1)
if (uidValidity !in 0L..0xFFFFFFFFL) return null
return UidValidityResponse(uidValidity)
}
}
}

View File

@ -0,0 +1,63 @@
package com.fsck.k9.mail.store.imap
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Test
class UidValidityResponseTest {
@Test
fun validResponseWithText() {
val response = ImapResponseHelper.createImapResponse("* OK [UIDVALIDITY 23] UIDs valid")
val result = UidValidityResponse.parse(response)
assertNotNull(result)
assertEquals(23, result!!.uidValidity)
}
@Test
fun validResponseWithoutText() {
val response = ImapResponseHelper.createImapResponse("* OK [UIDVALIDITY 42]")
val result = UidValidityResponse.parse(response)
assertNotNull(result)
assertEquals(42, result!!.uidValidity)
}
@Test
fun taggedResponse_shouldReturnNull() {
assertNotValid("99 OK [UIDVALIDITY 42]")
}
@Test
fun noResponse_shouldReturnNull() {
assertNotValid("* NO [UIDVALIDITY 42]")
}
@Test
fun responseTextWithOnlyOneItem_shouldReturnNull() {
assertNotValid("* OK [UIDVALIDITY]")
}
@Test
fun uidValidityIsNotANumber_shouldReturnNull() {
assertNotValid("* OK [UIDVALIDITY fourtytwo]")
}
@Test
fun negativeUidValidity_shouldReturnNull() {
assertNotValid("* OK [UIDVALIDITY -1]")
}
@Test
fun uidValidityOutsideRange_shouldReturnNull() {
assertNotValid("* OK [UIDVALIDITY 4294967296]")
}
private fun assertNotValid(response: String) {
val result = UidValidityResponse.parse(ImapResponseHelper.createImapResponse(response))
assertNull(result)
}
}