diff --git a/backend/api/src/main/java/com/fsck/k9/backend/api/Backend.kt b/backend/api/src/main/java/com/fsck/k9/backend/api/Backend.kt index d3fea5ac7..3d9820fa5 100644 --- a/backend/api/src/main/java/com/fsck/k9/backend/api/Backend.kt +++ b/backend/api/src/main/java/com/fsck/k9/backend/api/Backend.kt @@ -10,6 +10,9 @@ interface Backend { val supportsSeenFlag: Boolean val supportsExpunge: Boolean + @Throws(MessagingException::class) + fun getFolders(forceListAll: Boolean): List + // TODO: Add a way to cancel the sync process fun sync(folder: String, syncConfig: SyncConfig, listener: SyncListener, providedRemoteFolder: Folder<*>?) diff --git a/backend/api/src/main/java/com/fsck/k9/backend/api/FolderInfo.kt b/backend/api/src/main/java/com/fsck/k9/backend/api/FolderInfo.kt new file mode 100644 index 000000000..089b765dc --- /dev/null +++ b/backend/api/src/main/java/com/fsck/k9/backend/api/FolderInfo.kt @@ -0,0 +1,3 @@ +package com.fsck.k9.backend.api + +data class FolderInfo(val serverId: String, val name: String) diff --git a/backend/imap/src/main/java/com/fsck/k9/backend/imap/CommandGetFolders.kt b/backend/imap/src/main/java/com/fsck/k9/backend/imap/CommandGetFolders.kt new file mode 100644 index 000000000..b45185693 --- /dev/null +++ b/backend/imap/src/main/java/com/fsck/k9/backend/imap/CommandGetFolders.kt @@ -0,0 +1,14 @@ +package com.fsck.k9.backend.imap + + +import com.fsck.k9.backend.api.FolderInfo +import com.fsck.k9.mail.store.imap.ImapStore + + +internal class CommandGetFolders(private val imapStore: ImapStore) { + fun getFolders(forceListAll: Boolean): List { + return imapStore.getPersonalNamespaces(forceListAll).map { + FolderInfo(it.serverId, it.name) + } + } +} diff --git a/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapBackend.java b/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapBackend.java index 96f59e89d..6589915f0 100644 --- a/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapBackend.java +++ b/backend/imap/src/main/java/com/fsck/k9/backend/imap/ImapBackend.java @@ -6,6 +6,7 @@ import java.util.Map; import com.fsck.k9.backend.api.Backend; import com.fsck.k9.backend.api.BackendStorage; +import com.fsck.k9.backend.api.FolderInfo; import com.fsck.k9.backend.api.SyncConfig; import com.fsck.k9.backend.api.SyncListener; import com.fsck.k9.mail.Flag; @@ -18,6 +19,7 @@ import org.jetbrains.annotations.Nullable; public class ImapBackend implements Backend { private final ImapSync imapSync; + private final CommandGetFolders commandGetFolders; private final CommandSetFlag commandSetFlag; private final CommandMarkAllAsRead commandMarkAllAsRead; private final CommandExpunge commandExpunge; @@ -30,6 +32,7 @@ public class ImapBackend implements Backend { commandMarkAllAsRead = new CommandMarkAllAsRead(imapStore); commandExpunge = new CommandExpunge(imapStore); commandMoveOrCopyMessages = new CommandMoveOrCopyMessages(imapStore); + commandGetFolders = new CommandGetFolders(imapStore); } @Override @@ -42,6 +45,12 @@ public class ImapBackend implements Backend { return true; } + @NotNull + @Override + public List getFolders(boolean forceListAll) { + return commandGetFolders.getFolders(forceListAll); + } + @Override public void sync(@NotNull String folder, @NotNull SyncConfig syncConfig, @NotNull SyncListener listener, Folder providedRemoteFolder) { diff --git a/backend/pop3/src/main/java/com/fsck/k9/backend/pop3/CommandGetFolders.kt b/backend/pop3/src/main/java/com/fsck/k9/backend/pop3/CommandGetFolders.kt new file mode 100644 index 000000000..94f05e7c0 --- /dev/null +++ b/backend/pop3/src/main/java/com/fsck/k9/backend/pop3/CommandGetFolders.kt @@ -0,0 +1,12 @@ +package com.fsck.k9.backend.pop3 + + +import com.fsck.k9.backend.api.FolderInfo +import com.fsck.k9.mail.store.pop3.Pop3Folder + + +internal class CommandGetFolders { + fun getFolders(): List { + return listOf(FolderInfo(Pop3Folder.INBOX, Pop3Folder.INBOX)) + } +} diff --git a/backend/pop3/src/main/java/com/fsck/k9/backend/pop3/Pop3Backend.kt b/backend/pop3/src/main/java/com/fsck/k9/backend/pop3/Pop3Backend.kt index 048830fea..0f6750e00 100644 --- a/backend/pop3/src/main/java/com/fsck/k9/backend/pop3/Pop3Backend.kt +++ b/backend/pop3/src/main/java/com/fsck/k9/backend/pop3/Pop3Backend.kt @@ -2,6 +2,7 @@ package com.fsck.k9.backend.pop3 import com.fsck.k9.backend.api.Backend import com.fsck.k9.backend.api.BackendStorage +import com.fsck.k9.backend.api.FolderInfo import com.fsck.k9.backend.api.SyncConfig import com.fsck.k9.backend.api.SyncListener import com.fsck.k9.mail.Flag @@ -10,11 +11,16 @@ import com.fsck.k9.mail.store.pop3.Pop3Store class Pop3Backend(accountName: String, backendStorage: BackendStorage, pop3Store: Pop3Store) : Backend { private val pop3Sync: Pop3Sync = Pop3Sync(accountName, backendStorage, pop3Store) + private val commandGetFolders = CommandGetFolders() private val commandSetFlag = CommandSetFlag(pop3Store) override val supportsSeenFlag: Boolean = false override val supportsExpunge: Boolean = false + override fun getFolders(forceListAll: Boolean): List { + return commandGetFolders.getFolders() + } + override fun sync(folder: String, syncConfig: SyncConfig, listener: SyncListener, providedRemoteFolder: Folder<*>?) { pop3Sync.sync(folder, syncConfig, listener) } diff --git a/backend/webdav/src/main/java/com/fsck/k9/backend/webdav/CommandGetFolders.kt b/backend/webdav/src/main/java/com/fsck/k9/backend/webdav/CommandGetFolders.kt new file mode 100644 index 000000000..24706c69e --- /dev/null +++ b/backend/webdav/src/main/java/com/fsck/k9/backend/webdav/CommandGetFolders.kt @@ -0,0 +1,14 @@ +package com.fsck.k9.backend.webdav + + +import com.fsck.k9.backend.api.FolderInfo +import com.fsck.k9.mail.store.webdav.WebDavStore + + +internal class CommandGetFolders(private val webDavStore: WebDavStore) { + fun getFolders(forceListAll: Boolean): List { + return webDavStore.getPersonalNamespaces(forceListAll).map { + FolderInfo(it.serverId, it.name) + } + } +} diff --git a/backend/webdav/src/main/java/com/fsck/k9/backend/webdav/WebDavBackend.kt b/backend/webdav/src/main/java/com/fsck/k9/backend/webdav/WebDavBackend.kt index f4b41ad40..5d3c828cb 100644 --- a/backend/webdav/src/main/java/com/fsck/k9/backend/webdav/WebDavBackend.kt +++ b/backend/webdav/src/main/java/com/fsck/k9/backend/webdav/WebDavBackend.kt @@ -2,6 +2,7 @@ package com.fsck.k9.backend.webdav import com.fsck.k9.backend.api.Backend import com.fsck.k9.backend.api.BackendStorage +import com.fsck.k9.backend.api.FolderInfo import com.fsck.k9.backend.api.SyncConfig import com.fsck.k9.backend.api.SyncListener import com.fsck.k9.mail.Flag @@ -11,6 +12,7 @@ import com.fsck.k9.mail.store.webdav.WebDavStore class WebDavBackend(accountName: String, backendStorage: BackendStorage, webDavStore: WebDavStore) : Backend { private val webDavSync: WebDavSync = WebDavSync(accountName, backendStorage, webDavStore) + private val commandGetFolders = CommandGetFolders(webDavStore) private val commandSetFlag = CommandSetFlag(webDavStore) private val commandMarkAllAsRead = CommandMarkAllAsRead(webDavStore) private val commandMoveOrCopyMessages = CommandMoveOrCopyMessages(webDavStore) @@ -19,6 +21,10 @@ class WebDavBackend(accountName: String, backendStorage: BackendStorage, webDavS override val supportsExpunge: Boolean = true + override fun getFolders(forceListAll: Boolean): List { + return commandGetFolders.getFolders(forceListAll) + } + override fun sync(folder: String, syncConfig: SyncConfig, listener: SyncListener, providedRemoteFolder: Folder<*>?) { webDavSync.sync(folder, syncConfig, listener) } diff --git a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java index 34d07987a..7a3835635 100644 --- a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -49,6 +49,7 @@ import com.fsck.k9.activity.MessageReference; import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection; import com.fsck.k9.backend.BackendManager; import com.fsck.k9.backend.api.Backend; +import com.fsck.k9.backend.api.FolderInfo; import com.fsck.k9.backend.api.MessageRemovalListener; import com.fsck.k9.backend.api.SyncConfig; import com.fsck.k9.backend.api.SyncListener; @@ -433,12 +434,11 @@ public class MessagingController { void refreshRemoteSynchronous(final Account account, final MessagingListener listener) { List localFolders = null; try { - RemoteStore store = account.getRemoteStore(); - - List remoteFolders = store.getPersonalNamespaces(false); + Backend backend = getBackend(account); + List folders = backend.getFolders(false); LocalStore localStore = account.getLocalStore(); - Map remoteFolderMap = new HashMap<>(); + Map remoteFolderNameMap = new HashMap<>(); List foldersToCreate = new LinkedList<>(); localFolders = localStore.getPersonalNamespaces(false); @@ -447,12 +447,13 @@ public class MessagingController { localFolderServerIds.add(localFolder.getServerId()); } - for (Folder remoteFolder : remoteFolders) { - if (!localFolderServerIds.contains(remoteFolder.getServerId())) { - LocalFolder localFolder = localStore.getFolder(remoteFolder.getServerId()); + for (FolderInfo folder : folders) { + String folderServerId = folder.getServerId(); + if (!localFolderServerIds.contains(folderServerId)) { + LocalFolder localFolder = localStore.getFolder(folderServerId); foldersToCreate.add(localFolder); } - remoteFolderMap.put(remoteFolder.getServerId(), remoteFolder); + remoteFolderNameMap.put(folderServerId, folder.getName()); } localStore.createFolders(foldersToCreate, account.getDisplayCount()); @@ -470,10 +471,10 @@ public class MessagingController { localFolder.delete(false); } - boolean folderExistsOnServer = remoteFolderMap.containsKey(localFolderServerId); + boolean folderExistsOnServer = remoteFolderNameMap.containsKey(localFolderServerId); if (folderExistsOnServer) { - Folder remoteFolder = remoteFolderMap.get(localFolderServerId); - localFolder.setName(remoteFolder.getName()); + String folderName = remoteFolderNameMap.get(localFolderServerId); + localFolder.setName(folderName); } else if (!account.isSpecialFolder(localFolderServerId)) { localFolder.delete(false); } diff --git a/mail/protocols/pop3/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Folder.java b/mail/protocols/pop3/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Folder.java index ef32c1eee..022b71511 100644 --- a/mail/protocols/pop3/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Folder.java +++ b/mail/protocols/pop3/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Folder.java @@ -31,7 +31,7 @@ import static com.fsck.k9.mail.store.pop3.Pop3Commands.*; * POP3 only supports one folder, "Inbox". So the folder name is the ID here. */ public class Pop3Folder extends Folder { - static final String INBOX = "INBOX"; + public static final String INBOX = "INBOX"; private Pop3Store pop3Store;