From 9d90c530504d2418b4be59ac54396f1f7e8fa9ee Mon Sep 17 00:00:00 2001 From: m5w6 <35432036+m5w6@users.noreply.github.com> Date: Sat, 20 Jan 2018 21:22:16 +0100 Subject: [PATCH] Remove namespace prefix from auto-configured special folder names Fixes #701 --- .../fsck/k9/mail/store/imap/ImapStore.java | 9 +++++ .../k9/mail/store/imap/ImapStoreTest.java | 35 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java index 6ec05a1da..096e6a219 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java @@ -267,6 +267,15 @@ public class ImapStore extends RemoteStore { combinedPrefix = null; } + String prefix = getCombinedPrefix(); + int prefixLength = prefix.length(); + if (prefixLength > 0 && decodedFolderName.startsWith(prefix)) { + if (K9MailLib.isDebug()) { + Timber.d("Folder auto-configuration stripping prefix (%s) from folder name: %s", prefix, decodedFolderName); + } + decodedFolderName = decodedFolderName.substring(prefixLength); + } + if (listResponse.hasAttribute("\\Archive") || listResponse.hasAttribute("\\All")) { mStoreConfig.setArchiveFolderName(decodedFolderName); if (K9MailLib.isDebug()) { diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapStoreTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapStoreTest.java index 30462d392..b0e393575 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapStoreTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapStoreTest.java @@ -131,6 +131,31 @@ public class ImapStoreTest { verifyNoMoreInteractions(imapConnection); } + @Test + public void autoconfigureFolders_removeNamespacePrefix() throws Exception { + ImapConnection imapConnection = mock(ImapConnection.class); + when(imapConnection.hasCapability(Capabilities.SPECIAL_USE)).thenReturn(true); + List imapResponses = Arrays.asList( + createImapResponse("* LIST (\\Drafts) \"/\" \"INBOX.Drafts\""), + createImapResponse("* LIST (\\Sent) \"/\" \"INBOX.Sent\""), + createImapResponse("* LIST (\\Junk) \"/\" \"INBOX.Spam\""), + createImapResponse("* LIST (\\Trash) \"/\" \"INBOX.Trash\""), + createImapResponse("* LIST (\\Archive) \"/\" \"INBOX.Archive\""), + createImapResponse("5 OK Success") + ); + when(imapConnection.executeSimpleCommand("LIST (SPECIAL-USE) \"\" \"INBOX.*\"")).thenReturn(imapResponses); + + imapStore.setTestCombinedPrefix("INBOX."); + imapStore.autoconfigureFolders(imapConnection); + + assertEquals("INBOX.", imapStore.getCombinedPrefix()); + verify(storeConfig).setDraftsFolderName("Drafts"); + verify(storeConfig).setSentFolderName("Sent"); + verify(storeConfig).setSpamFolderName("Spam"); + verify(storeConfig).setTrashFolderName("Trash"); + verify(storeConfig).setArchiveFolderName("Archive"); + } + @Test public void getPersonalNamespaces_withForceListAll() throws Exception { when(storeConfig.subscribedFoldersOnly()).thenReturn(true); @@ -313,6 +338,7 @@ public class ImapStoreTest { static class TestImapStore extends ImapStore { private Deque imapConnections = new ArrayDeque<>(); + private String testCombinedPrefix; public TestImapStore(StoreConfig storeConfig, TrustedSocketFactory trustedSocketFactory, ConnectivityManager connectivityManager, OAuth2TokenProvider oauth2TokenProvider) throws MessagingException { @@ -330,5 +356,14 @@ public class ImapStoreTest { public void enqueueImapConnection(ImapConnection imapConnection) { imapConnections.add(imapConnection); } + + @Override + String getCombinedPrefix() { + return testCombinedPrefix != null ? testCombinedPrefix : super.getCombinedPrefix(); + } + + void setTestCombinedPrefix(String prefix) { + testCombinedPrefix = prefix; + } } }