From d7558a13139607e9aa34f49aab816b4dff718f5d Mon Sep 17 00:00:00 2001 From: cketti Date: Sun, 17 Jun 2018 04:13:17 +0200 Subject: [PATCH] Move code to empty the trash folder to Backend implementations --- .../java/com/fsck/k9/backend/api/Backend.kt | 3 ++ .../fsck/k9/backend/imap/CommandDeleteAll.kt | 26 ++++++++++++ .../com/fsck/k9/backend/imap/ImapBackend.java | 7 ++++ .../fsck/k9/backend/pop3/CommandDeleteAll.kt | 26 ++++++++++++ .../com/fsck/k9/backend/pop3/Pop3Backend.kt | 5 +++ .../k9/backend/webdav/CommandDeleteAll.kt | 26 ++++++++++++ .../fsck/k9/backend/webdav/WebDavBackend.kt | 5 +++ .../k9/controller/MessagingController.java | 40 +++++++++---------- 8 files changed, 118 insertions(+), 20 deletions(-) create mode 100644 backend/imap/src/main/java/com/fsck/k9/backend/imap/CommandDeleteAll.kt create mode 100644 backend/pop3/src/main/java/com/fsck/k9/backend/pop3/CommandDeleteAll.kt create mode 100644 backend/webdav/src/main/java/com/fsck/k9/backend/webdav/CommandDeleteAll.kt 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 3d9820fa5..e54724976 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 @@ -28,6 +28,9 @@ interface Backend { @Throws(MessagingException::class) fun expungeMessages(folderServerId: String, messageServerIds: List) + @Throws(MessagingException::class) + fun deleteAllMessages(folderServerId: String) + @Throws(MessagingException::class) fun moveMessages( sourceFolderServerId: String, diff --git a/backend/imap/src/main/java/com/fsck/k9/backend/imap/CommandDeleteAll.kt b/backend/imap/src/main/java/com/fsck/k9/backend/imap/CommandDeleteAll.kt new file mode 100644 index 000000000..335b95d63 --- /dev/null +++ b/backend/imap/src/main/java/com/fsck/k9/backend/imap/CommandDeleteAll.kt @@ -0,0 +1,26 @@ +package com.fsck.k9.backend.imap + + +import com.fsck.k9.mail.Flag +import com.fsck.k9.mail.Folder +import com.fsck.k9.mail.MessagingException +import com.fsck.k9.mail.store.imap.ImapStore + + +internal class CommandDeleteAll(private val imapStore: ImapStore) { + + @Throws(MessagingException::class) + fun deleteAll(folderServerId: String) { + val remoteFolder = imapStore.getFolder(folderServerId) + if (!remoteFolder.exists()) { + return + } + + try { + remoteFolder.open(Folder.OPEN_MODE_RW) + remoteFolder.setFlags(setOf(Flag.DELETED), true) + } finally { + remoteFolder.close() + } + } +} 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 6589915f0..af9de2b47 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 @@ -24,6 +24,7 @@ public class ImapBackend implements Backend { private final CommandMarkAllAsRead commandMarkAllAsRead; private final CommandExpunge commandExpunge; private final CommandMoveOrCopyMessages commandMoveOrCopyMessages; + private final CommandDeleteAll commandDeleteAll; public ImapBackend(String accountName, BackendStorage backendStorage, ImapStore imapStore) { @@ -33,6 +34,7 @@ public class ImapBackend implements Backend { commandExpunge = new CommandExpunge(imapStore); commandMoveOrCopyMessages = new CommandMoveOrCopyMessages(imapStore); commandGetFolders = new CommandGetFolders(imapStore); + commandDeleteAll = new CommandDeleteAll(imapStore); } @Override @@ -79,6 +81,11 @@ public class ImapBackend implements Backend { commandExpunge.expungeMessages(folderServerId, messageServerIds); } + @Override + public void deleteAllMessages(@NotNull String folderServerId) throws MessagingException { + commandDeleteAll.deleteAll(folderServerId); + } + @Nullable @Override public Map moveMessages(@NotNull String sourceFolderServerId, @NotNull String targetFolderServerId, diff --git a/backend/pop3/src/main/java/com/fsck/k9/backend/pop3/CommandDeleteAll.kt b/backend/pop3/src/main/java/com/fsck/k9/backend/pop3/CommandDeleteAll.kt new file mode 100644 index 000000000..35a98f40e --- /dev/null +++ b/backend/pop3/src/main/java/com/fsck/k9/backend/pop3/CommandDeleteAll.kt @@ -0,0 +1,26 @@ +package com.fsck.k9.backend.pop3 + + +import com.fsck.k9.mail.Flag +import com.fsck.k9.mail.Folder +import com.fsck.k9.mail.MessagingException +import com.fsck.k9.mail.store.pop3.Pop3Store + + +internal class CommandDeleteAll(private val pop3Store: Pop3Store) { + + @Throws(MessagingException::class) + fun deleteAll(folderServerId: String) { + val remoteFolder = pop3Store.getFolder(folderServerId) + if (!remoteFolder.exists()) { + return + } + + try { + remoteFolder.open(Folder.OPEN_MODE_RW) + remoteFolder.setFlags(setOf(Flag.DELETED), true) + } finally { + remoteFolder.close() + } + } +} 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 0f6750e00..0c3b39b4d 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 @@ -13,6 +13,7 @@ class Pop3Backend(accountName: String, backendStorage: BackendStorage, pop3Store private val pop3Sync: Pop3Sync = Pop3Sync(accountName, backendStorage, pop3Store) private val commandGetFolders = CommandGetFolders() private val commandSetFlag = CommandSetFlag(pop3Store) + private val commandDeleteAll = CommandDeleteAll(pop3Store) override val supportsSeenFlag: Boolean = false override val supportsExpunge: Boolean = false @@ -41,6 +42,10 @@ class Pop3Backend(accountName: String, backendStorage: BackendStorage, pop3Store throw UnsupportedOperationException("not supported") } + override fun deleteAllMessages(folderServerId: String) { + commandDeleteAll.deleteAll(folderServerId) + } + override fun moveMessages( sourceFolderServerId: String, targetFolderServerId: String, diff --git a/backend/webdav/src/main/java/com/fsck/k9/backend/webdav/CommandDeleteAll.kt b/backend/webdav/src/main/java/com/fsck/k9/backend/webdav/CommandDeleteAll.kt new file mode 100644 index 000000000..db25db87a --- /dev/null +++ b/backend/webdav/src/main/java/com/fsck/k9/backend/webdav/CommandDeleteAll.kt @@ -0,0 +1,26 @@ +package com.fsck.k9.backend.webdav + + +import com.fsck.k9.mail.Flag +import com.fsck.k9.mail.Folder +import com.fsck.k9.mail.MessagingException +import com.fsck.k9.mail.store.webdav.WebDavStore + + +internal class CommandDeleteAll(private val webDavStore: WebDavStore) { + + @Throws(MessagingException::class) + fun deleteAll(folderServerId: String) { + val remoteFolder = webDavStore.getFolder(folderServerId) + if (!remoteFolder.exists()) { + return + } + + try { + remoteFolder.open(Folder.OPEN_MODE_RW) + remoteFolder.setFlags(setOf(Flag.DELETED), true) + } finally { + remoteFolder.close() + } + } +} 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 5d3c828cb..d2d2a171f 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 @@ -16,6 +16,7 @@ class WebDavBackend(accountName: String, backendStorage: BackendStorage, webDavS private val commandSetFlag = CommandSetFlag(webDavStore) private val commandMarkAllAsRead = CommandMarkAllAsRead(webDavStore) private val commandMoveOrCopyMessages = CommandMoveOrCopyMessages(webDavStore) + private val commandDeleteAll = CommandDeleteAll(webDavStore) override val supportsSeenFlag: Boolean = true override val supportsExpunge: Boolean = true @@ -46,6 +47,10 @@ class WebDavBackend(accountName: String, backendStorage: BackendStorage, webDavS throw UnsupportedOperationException("not supported") } + override fun deleteAllMessages(folderServerId: String) { + commandDeleteAll.deleteAll(folderServerId) + } + override fun moveMessages( sourceFolderServerId: String, targetFolderServerId: String, 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 7a3835635..ad888bbfb 100644 --- a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -2877,27 +2877,27 @@ public class MessagingController { } void processPendingEmptyTrash(Account account) throws MessagingException { - RemoteStore remoteStore = account.getRemoteStore(); - - Folder remoteFolder = remoteStore.getFolder(account.getTrashFolder()); - try { - if (remoteFolder.exists()) { - remoteFolder.open(Folder.OPEN_MODE_RW); - remoteFolder.setFlags(Collections.singleton(Flag.DELETED), true); - if (Expunge.EXPUNGE_IMMEDIATELY == account.getExpungePolicy()) { - remoteFolder.expunge(); - } - - // When we empty trash, we need to actually synchronize the folder - // or local deletes will never get cleaned up - synchronizeFolder(account, remoteFolder, true, 0, null); - compact(account, null); - - - } - } finally { - closeFolder(remoteFolder); + if (!account.hasTrashFolder()) { + return; } + + Backend backend = getBackend(account); + + String trashFolderServerId = account.getTrashFolder(); + backend.deleteAllMessages(trashFolderServerId); + + if (account.getExpungePolicy() == Expunge.EXPUNGE_IMMEDIATELY) { + backend.expunge(trashFolderServerId); + } + + // When we empty trash, we need to actually synchronize the folder + // or local deletes will never get cleaned up + LocalStore localStore = account.getLocalStore(); + LocalFolder folder = localStore.getFolder(trashFolderServerId); + folder.open(Folder.OPEN_MODE_RW); + synchronizeFolder(account, folder, true, 0, null); + + compact(account, null); } public void emptyTrash(final Account account, MessagingListener listener) {