Move code to download (partial) message to Backend implementations
This commit is contained in:
parent
455315f62e
commit
a57a4f6df3
7 changed files with 74 additions and 19 deletions
|
@ -18,6 +18,9 @@ interface Backend {
|
|||
// TODO: Add a way to cancel the sync process
|
||||
fun sync(folder: String, syncConfig: SyncConfig, listener: SyncListener, providedRemoteFolder: Folder<*>?)
|
||||
|
||||
@Throws(MessagingException::class)
|
||||
fun downloadMessage(syncConfig: SyncConfig, folderServerId: String, messageServerId: String)
|
||||
|
||||
@Throws(MessagingException::class)
|
||||
fun setFlag(folderServerId: String, messageServerIds: List<String>, flag: Flag, newState: Boolean)
|
||||
|
||||
|
|
|
@ -70,6 +70,12 @@ public class ImapBackend implements Backend {
|
|||
imapSync.sync(folder, syncConfig, listener, providedRemoteFolder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void downloadMessage(@NotNull SyncConfig syncConfig, @NotNull String folderServerId,
|
||||
@NotNull String messageServerId) throws MessagingException {
|
||||
imapSync.downloadMessage(syncConfig, folderServerId, messageServerId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFlag(@NotNull String folderServerId, @NotNull List<String> messageServerIds, @NotNull Flag flag,
|
||||
boolean newState) throws MessagingException {
|
||||
|
|
|
@ -31,6 +31,7 @@ import com.fsck.k9.mail.MessageRetrievalListener;
|
|||
import com.fsck.k9.mail.MessagingException;
|
||||
import com.fsck.k9.mail.Part;
|
||||
import com.fsck.k9.mail.internet.MessageExtractor;
|
||||
import com.fsck.k9.mail.store.imap.ImapFolder;
|
||||
import com.fsck.k9.mail.store.imap.ImapStore;
|
||||
import timber.log.Timber;
|
||||
|
||||
|
@ -256,6 +257,28 @@ class ImapSync {
|
|||
|
||||
}
|
||||
|
||||
void downloadMessage(SyncConfig syncConfig, String folderServerId, String messageServerId)
|
||||
throws MessagingException {
|
||||
BackendFolder backendFolder = backendStorage.getFolder(folderServerId);
|
||||
ImapFolder remoteFolder = imapStore.getFolder(folderServerId);
|
||||
try {
|
||||
remoteFolder.open(Folder.OPEN_MODE_RO);
|
||||
Message remoteMessage = remoteFolder.getMessage(messageServerId);
|
||||
|
||||
downloadMessages(
|
||||
syncConfig,
|
||||
remoteFolder,
|
||||
backendFolder,
|
||||
Collections.singletonList(remoteMessage),
|
||||
false,
|
||||
false,
|
||||
null,
|
||||
new SimpleSyncListener());
|
||||
} finally {
|
||||
remoteFolder.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the messages described by inputMessages from the remote store and writes them to
|
||||
* local storage.
|
||||
|
@ -275,7 +298,7 @@ class ImapSync {
|
|||
*
|
||||
* @throws MessagingException
|
||||
*/
|
||||
int downloadMessages(SyncConfig syncConfig, Folder remoteFolder, BackendFolder backendFolder,
|
||||
private int downloadMessages(SyncConfig syncConfig, Folder remoteFolder, BackendFolder backendFolder,
|
||||
List<Message> inputMessages, boolean flagSyncOnly, boolean purgeToVisibleLimit, Long lastUid,
|
||||
final SyncListener listener) throws MessagingException {
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package com.fsck.k9.backend.imap
|
||||
|
||||
import com.fsck.k9.backend.api.SyncListener
|
||||
|
||||
class SimpleSyncListener : SyncListener {
|
||||
override fun syncStarted(folderServerId: String, folderName: String) = Unit
|
||||
override fun syncAuthenticationSuccess() = Unit
|
||||
override fun syncHeadersStarted(folderServerId: String, folderName: String) = Unit
|
||||
override fun syncHeadersProgress(folderServerId: String, completed: Int, total: Int) = Unit
|
||||
override fun syncHeadersFinished(folderServerId: String, totalMessagesInMailbox: Int, numNewMessages: Int) = Unit
|
||||
override fun syncProgress(folderServerId: String, completed: Int, total: Int) = Unit
|
||||
override fun syncNewMessage(folderServerId: String, messageServerId: String, isOldMessage: Boolean) = Unit
|
||||
override fun syncRemovedMessage(folderServerId: String, messageServerId: String) = Unit
|
||||
override fun syncFlagChanged(folderServerId: String, messageServerId: String) = Unit
|
||||
override fun syncFinished(folderServerId: String, totalMessagesInMailbox: Int, numNewMessages: Int) = Unit
|
||||
override fun syncFailed(folderServerId: String, message: String, exception: Exception?) = Unit
|
||||
override fun folderStatusChanged(folderServerId: String, unreadMessageCount: Int) = Unit
|
||||
}
|
|
@ -29,6 +29,10 @@ class Pop3Backend(accountName: String, backendStorage: BackendStorage, pop3Store
|
|||
pop3Sync.sync(folder, syncConfig, listener)
|
||||
}
|
||||
|
||||
override fun downloadMessage(syncConfig: SyncConfig, folderServerId: String, messageServerId: String) {
|
||||
throw UnsupportedOperationException("not implemented")
|
||||
}
|
||||
|
||||
override fun setFlag(folderServerId: String, messageServerIds: List<String>, flag: Flag, newState: Boolean) {
|
||||
commandSetFlag.setFlag(folderServerId, messageServerIds, flag, newState)
|
||||
}
|
||||
|
|
|
@ -34,6 +34,10 @@ class WebDavBackend(accountName: String, backendStorage: BackendStorage, webDavS
|
|||
webDavSync.sync(folder, syncConfig, listener)
|
||||
}
|
||||
|
||||
override fun downloadMessage(syncConfig: SyncConfig, folderServerId: String, messageServerId: String) {
|
||||
throw UnsupportedOperationException("not implemented")
|
||||
}
|
||||
|
||||
@Throws(MessagingException::class)
|
||||
override fun setFlag(folderServerId: String, messageServerIds: List<String>, flag: Flag, newState: Boolean) {
|
||||
commandSetFlag.setFlag(folderServerId, messageServerIds, flag, newState)
|
||||
|
|
|
@ -747,13 +747,7 @@ public class MessagingController {
|
|||
return;
|
||||
}
|
||||
|
||||
SyncConfig syncConfig = new SyncConfig(
|
||||
account.getExpungePolicy().toBackendExpungePolicy(),
|
||||
account.getEarliestPollDate(),
|
||||
account.syncRemoteDeletions(),
|
||||
account.getMaximumAutoDownloadMessageSize(),
|
||||
K9.DEFAULT_VISIBLE_LIMIT,
|
||||
SYNC_FLAGS);
|
||||
SyncConfig syncConfig = createSyncConfig(account);
|
||||
|
||||
ControllerSyncListener syncListener = new ControllerSyncListener(account, listener);
|
||||
remoteMessageStore.sync(folder, syncConfig, syncListener, providedRemoteFolder);
|
||||
|
@ -766,6 +760,16 @@ public class MessagingController {
|
|||
}
|
||||
}
|
||||
|
||||
private SyncConfig createSyncConfig(Account account) {
|
||||
return new SyncConfig(
|
||||
account.getExpungePolicy().toBackendExpungePolicy(),
|
||||
account.getEarliestPollDate(),
|
||||
account.syncRemoteDeletions(),
|
||||
account.getMaximumAutoDownloadMessageSize(),
|
||||
K9.DEFAULT_VISIBLE_LIMIT,
|
||||
SYNC_FLAGS);
|
||||
}
|
||||
|
||||
private void updateFolderStatus(Account account, String folderServerId, String status) {
|
||||
try {
|
||||
LocalStore localStore = account.getLocalStore();
|
||||
|
@ -1858,7 +1862,6 @@ public class MessagingController {
|
|||
|
||||
private boolean loadMessageRemoteSynchronous(final Account account, final String folder,
|
||||
final String uid, final MessagingListener listener, final boolean loadPartialFromSearch) {
|
||||
Folder remoteFolder = null;
|
||||
LocalFolder localFolder = null;
|
||||
try {
|
||||
LocalStore localStore = account.getLocalStore();
|
||||
|
@ -1878,21 +1881,16 @@ public class MessagingController {
|
|||
message.setFlag(Flag.X_DOWNLOADED_FULL, true);
|
||||
message.setFlag(Flag.X_DOWNLOADED_PARTIAL, false);
|
||||
} else {
|
||||
RemoteStore remoteStore = account.getRemoteStore();
|
||||
remoteFolder = remoteStore.getFolder(folder);
|
||||
remoteFolder.open(Folder.OPEN_MODE_RW);
|
||||
|
||||
// Get the remote message and fully download it
|
||||
Message remoteMessage = remoteFolder.getMessage(uid);
|
||||
Backend backend = getBackend(account);
|
||||
|
||||
if (loadPartialFromSearch) {
|
||||
downloadMessages(account, remoteFolder, localFolder,
|
||||
Collections.singletonList(remoteMessage), false, false);
|
||||
SyncConfig syncConfig = createSyncConfig(account);
|
||||
backend.downloadMessage(syncConfig, folder, uid);
|
||||
} else {
|
||||
FetchProfile fp = new FetchProfile();
|
||||
fp.add(FetchProfile.Item.BODY);
|
||||
fp.add(FetchProfile.Item.FLAGS);
|
||||
remoteFolder.fetch(Collections.singletonList(remoteMessage), fp, null);
|
||||
Message remoteMessage = backend.fetchMessage(folder, uid, fp);
|
||||
localFolder.appendMessages(Collections.singletonList(remoteMessage));
|
||||
}
|
||||
|
||||
|
@ -1917,7 +1915,6 @@ public class MessagingController {
|
|||
Timber.e(e, "Error while loading remote message");
|
||||
return false;
|
||||
} finally {
|
||||
closeFolder(remoteFolder);
|
||||
closeFolder(localFolder);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue