Move code to set message flags to Backend implementations
This commit is contained in:
parent
80e884cd52
commit
294a678b16
10 changed files with 202 additions and 35 deletions
|
@ -1,10 +1,15 @@
|
|||
package com.fsck.k9.backend.api
|
||||
|
||||
|
||||
import com.fsck.k9.mail.Flag
|
||||
import com.fsck.k9.mail.Folder
|
||||
import com.fsck.k9.mail.MessagingException
|
||||
|
||||
|
||||
interface Backend {
|
||||
// TODO: Add a way to cancel the sync process
|
||||
fun sync(folder: String, syncConfig: SyncConfig, listener: SyncListener, providedRemoteFolder: Folder<*>)
|
||||
fun sync(folder: String, syncConfig: SyncConfig, listener: SyncListener, providedRemoteFolder: Folder<*>?)
|
||||
|
||||
@Throws(MessagingException::class)
|
||||
fun setFlag(folderServerId: String, messageServerIds: List<String>, flag: Flag, newState: Boolean)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package com.fsck.k9.backend.imap;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import com.fsck.k9.backend.api.BackendFolder;
|
||||
import com.fsck.k9.mail.Flag;
|
||||
import com.fsck.k9.mail.Folder;
|
||||
import com.fsck.k9.mail.Message;
|
||||
import com.fsck.k9.mail.MessagingException;
|
||||
import com.fsck.k9.mail.store.imap.ImapFolder;
|
||||
import com.fsck.k9.mail.store.imap.ImapStore;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
class CommandSetFlag {
|
||||
private final ImapStore imapStore;
|
||||
|
||||
|
||||
CommandSetFlag(ImapStore imapStore) {
|
||||
this.imapStore = imapStore;
|
||||
}
|
||||
|
||||
void setFlag(@NotNull String folderServerId, @NotNull List<String> messageServerIds, @NotNull Flag flag,
|
||||
boolean newState) throws MessagingException {
|
||||
|
||||
ImapFolder remoteFolder = imapStore.getFolder(folderServerId);
|
||||
if (!remoteFolder.exists() || !remoteFolder.isFlagSupported(flag)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
remoteFolder.open(Folder.OPEN_MODE_RW);
|
||||
if (remoteFolder.getMode() != Folder.OPEN_MODE_RW) {
|
||||
return;
|
||||
}
|
||||
List<Message> messages = new ArrayList<>();
|
||||
for (String uid : messageServerIds) {
|
||||
if (!uid.startsWith(BackendFolder.LOCAL_UID_PREFIX)) {
|
||||
messages.add(remoteFolder.getMessage(uid));
|
||||
}
|
||||
}
|
||||
|
||||
if (messages.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
remoteFolder.setFlags(messages, Collections.singleton(flag), newState);
|
||||
} finally {
|
||||
remoteFolder.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,24 +1,38 @@
|
|||
package com.fsck.k9.backend.imap;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fsck.k9.backend.api.Backend;
|
||||
import com.fsck.k9.backend.api.BackendStorage;
|
||||
import com.fsck.k9.backend.api.SyncConfig;
|
||||
import com.fsck.k9.backend.api.SyncListener;
|
||||
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;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
public class ImapBackend implements Backend {
|
||||
private final ImapSync imapSync;
|
||||
private final CommandSetFlag commandSetFlag;
|
||||
|
||||
|
||||
public ImapBackend(String accountName, BackendStorage backendStorage, ImapStore imapStore) {
|
||||
this.imapSync = new ImapSync(accountName, backendStorage, imapStore);
|
||||
imapSync = new ImapSync(accountName, backendStorage, imapStore);
|
||||
commandSetFlag = new CommandSetFlag(imapStore);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sync(String folder, SyncConfig syncConfig, SyncListener listener, Folder providedRemoteFolder) {
|
||||
public void sync(@NotNull String folder, @NotNull SyncConfig syncConfig, @NotNull SyncListener listener,
|
||||
Folder providedRemoteFolder) {
|
||||
imapSync.sync(folder, syncConfig, listener, providedRemoteFolder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFlag(@NotNull String folderServerId, @NotNull List<String> messageServerIds, @NotNull Flag flag,
|
||||
boolean newState) throws MessagingException {
|
||||
commandSetFlag.setFlag(folderServerId, messageServerIds, flag, newState);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package com.fsck.k9.backend.pop3;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import com.fsck.k9.backend.api.BackendFolder;
|
||||
import com.fsck.k9.mail.Flag;
|
||||
import com.fsck.k9.mail.Folder;
|
||||
import com.fsck.k9.mail.Message;
|
||||
import com.fsck.k9.mail.MessagingException;
|
||||
import com.fsck.k9.mail.store.pop3.Pop3Folder;
|
||||
import com.fsck.k9.mail.store.pop3.Pop3Store;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
class CommandSetFlag {
|
||||
private final Pop3Store pop3Store;
|
||||
|
||||
|
||||
CommandSetFlag(Pop3Store pop3Store) {
|
||||
this.pop3Store = pop3Store;
|
||||
}
|
||||
|
||||
void setFlag(@NotNull String folderServerId, @NotNull List<String> messageServerIds, @NotNull Flag flag,
|
||||
boolean newState) throws MessagingException {
|
||||
|
||||
Pop3Folder remoteFolder = pop3Store.getFolder(folderServerId);
|
||||
if (!remoteFolder.exists() || !remoteFolder.isFlagSupported(flag)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
remoteFolder.open(Folder.OPEN_MODE_RW);
|
||||
if (remoteFolder.getMode() != Folder.OPEN_MODE_RW) {
|
||||
return;
|
||||
}
|
||||
List<Message> messages = new ArrayList<>();
|
||||
for (String uid : messageServerIds) {
|
||||
if (!uid.startsWith(BackendFolder.LOCAL_UID_PREFIX)) {
|
||||
messages.add(remoteFolder.getMessage(uid));
|
||||
}
|
||||
}
|
||||
|
||||
if (messages.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
remoteFolder.setFlags(messages, Collections.singleton(flag), newState);
|
||||
} finally {
|
||||
remoteFolder.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,13 +4,19 @@ import com.fsck.k9.backend.api.Backend
|
|||
import com.fsck.k9.backend.api.BackendStorage
|
||||
import com.fsck.k9.backend.api.SyncConfig
|
||||
import com.fsck.k9.backend.api.SyncListener
|
||||
import com.fsck.k9.mail.Flag
|
||||
import com.fsck.k9.mail.Folder
|
||||
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 commandSetFlag = CommandSetFlag(pop3Store)
|
||||
|
||||
override fun sync(folder: String, syncConfig: SyncConfig, listener: SyncListener, providedRemoteFolder: Folder<*>?) {
|
||||
pop3Sync.sync(folder, syncConfig, listener)
|
||||
}
|
||||
|
||||
override fun setFlag(folderServerId: String, messageServerIds: List<String>, flag: Flag, newState: Boolean) {
|
||||
commandSetFlag.setFlag(folderServerId, messageServerIds, flag, newState)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package com.fsck.k9.backend.webdav;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import com.fsck.k9.backend.api.BackendFolder;
|
||||
import com.fsck.k9.mail.Flag;
|
||||
import com.fsck.k9.mail.Folder;
|
||||
import com.fsck.k9.mail.Message;
|
||||
import com.fsck.k9.mail.MessagingException;
|
||||
import com.fsck.k9.mail.store.webdav.WebDavFolder;
|
||||
import com.fsck.k9.mail.store.webdav.WebDavStore;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
class CommandSetFlag {
|
||||
private final WebDavStore webDavStore;
|
||||
|
||||
|
||||
CommandSetFlag(WebDavStore webDavStore) {
|
||||
this.webDavStore = webDavStore;
|
||||
}
|
||||
|
||||
void setFlag(@NotNull String folderServerId, @NotNull List<String> messageServerIds, @NotNull Flag flag,
|
||||
boolean newState) throws MessagingException {
|
||||
|
||||
WebDavFolder remoteFolder = webDavStore.getFolder(folderServerId);
|
||||
if (!remoteFolder.exists() || !remoteFolder.isFlagSupported(flag)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
remoteFolder.open(Folder.OPEN_MODE_RW);
|
||||
if (remoteFolder.getMode() != Folder.OPEN_MODE_RW) {
|
||||
return;
|
||||
}
|
||||
List<Message> messages = new ArrayList<>();
|
||||
for (String uid : messageServerIds) {
|
||||
if (!uid.startsWith(BackendFolder.LOCAL_UID_PREFIX)) {
|
||||
messages.add(remoteFolder.getMessage(uid));
|
||||
}
|
||||
}
|
||||
|
||||
if (messages.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
remoteFolder.setFlags(messages, Collections.singleton(flag), newState);
|
||||
} finally {
|
||||
remoteFolder.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,13 +4,21 @@ import com.fsck.k9.backend.api.Backend
|
|||
import com.fsck.k9.backend.api.BackendStorage
|
||||
import com.fsck.k9.backend.api.SyncConfig
|
||||
import com.fsck.k9.backend.api.SyncListener
|
||||
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
|
||||
|
||||
class WebDavBackend(accountName: String, backendStorage: BackendStorage, webDavStore: WebDavStore) : Backend {
|
||||
private val webDavSync: WebDavSync = WebDavSync(accountName, backendStorage, webDavStore)
|
||||
private val commandSetFlag = CommandSetFlag(webDavStore)
|
||||
|
||||
override fun sync(folder: String, syncConfig: SyncConfig, listener: SyncListener, providedRemoteFolder: Folder<*>?) {
|
||||
webDavSync.sync(folder, syncConfig, listener)
|
||||
}
|
||||
|
||||
@Throws(MessagingException::class)
|
||||
override fun setFlag(folderServerId: String, messageServerIds: List<String>, flag: Flag, newState: Boolean) {
|
||||
commandSetFlag.setFlag(folderServerId, messageServerIds, flag, newState)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1707,36 +1707,8 @@ public class MessagingController {
|
|||
* Processes a pending mark read or unread command.
|
||||
*/
|
||||
void processPendingSetFlag(PendingSetFlag command, Account account) throws MessagingException {
|
||||
String folder = command.folder;
|
||||
|
||||
boolean newState = command.newState;
|
||||
Flag flag = command.flag;
|
||||
|
||||
RemoteStore remoteStore = account.getRemoteStore();
|
||||
Folder remoteFolder = remoteStore.getFolder(folder);
|
||||
if (!remoteFolder.exists() || !remoteFolder.isFlagSupported(flag)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
remoteFolder.open(Folder.OPEN_MODE_RW);
|
||||
if (remoteFolder.getMode() != Folder.OPEN_MODE_RW) {
|
||||
return;
|
||||
}
|
||||
List<Message> messages = new ArrayList<>();
|
||||
for (String uid : command.uids) {
|
||||
if (!uid.startsWith(K9.LOCAL_UID_PREFIX)) {
|
||||
messages.add(remoteFolder.getMessage(uid));
|
||||
}
|
||||
}
|
||||
|
||||
if (messages.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
remoteFolder.setFlags(messages, Collections.singleton(flag), newState);
|
||||
} finally {
|
||||
closeFolder(remoteFolder);
|
||||
}
|
||||
Backend backend = getBackend(account);
|
||||
backend.setFlag(command.folder, command.uids, command.flag, command.newState);
|
||||
}
|
||||
|
||||
private void queueExpunge(final Account account, final String folderServerId) {
|
||||
|
|
|
@ -30,7 +30,7 @@ import static com.fsck.k9.mail.store.pop3.Pop3Commands.*;
|
|||
/**
|
||||
* POP3 only supports one folder, "Inbox". So the folder name is the ID here.
|
||||
*/
|
||||
class Pop3Folder extends Folder<Pop3Message> {
|
||||
public class Pop3Folder extends Folder<Pop3Message> {
|
||||
static final String INBOX = "INBOX";
|
||||
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ import static com.fsck.k9.mail.helper.UrlEncodingHelper.encodeUtf8;
|
|||
/**
|
||||
* A WebDav Folder
|
||||
*/
|
||||
class WebDavFolder extends Folder<WebDavMessage> {
|
||||
public class WebDavFolder extends Folder<WebDavMessage> {
|
||||
private String mName;
|
||||
private String mFolderUrl;
|
||||
private boolean mIsOpen = false;
|
||||
|
|
Loading…
Reference in a new issue