use of com.fsck.k9.backend.api.SyncConfig in project k-9 by k9mail.
the class MessagingController method loadMessageRemoteSynchronous.
private void loadMessageRemoteSynchronous(Account account, long folderId, String uid, MessagingListener listener, boolean loadPartialFromSearch) {
try {
LocalStore localStore = localStoreProvider.getInstance(account);
LocalFolder localFolder = localStore.getFolder(folderId);
localFolder.open();
String folderServerId = localFolder.getServerId();
LocalMessage message = localFolder.getMessage(uid);
if (uid.startsWith(K9.LOCAL_UID_PREFIX)) {
Timber.w("Message has local UID so cannot download fully.");
// ASH move toast
android.widget.Toast.makeText(context, "Message has local UID so cannot download fully", android.widget.Toast.LENGTH_LONG).show();
// TODO: Using X_DOWNLOADED_FULL is wrong because it's only a partial message. But
// one we can't download completely. Maybe add a new flag; X_PARTIAL_MESSAGE ?
message.setFlag(Flag.X_DOWNLOADED_FULL, true);
message.setFlag(Flag.X_DOWNLOADED_PARTIAL, false);
} else {
Backend backend = getBackend(account);
if (loadPartialFromSearch) {
SyncConfig syncConfig = createSyncConfig(account);
backend.downloadMessage(syncConfig, folderServerId, uid);
} else {
backend.downloadCompleteMessage(folderServerId, uid);
}
message = localFolder.getMessage(uid);
if (!loadPartialFromSearch) {
message.setFlag(Flag.X_DOWNLOADED_FULL, true);
}
}
// now that we have the full message, refresh the headers
for (MessagingListener l : getListeners(listener)) {
l.loadMessageRemoteFinished(account, folderId, uid);
}
} catch (Exception e) {
for (MessagingListener l : getListeners(listener)) {
l.loadMessageRemoteFailed(account, folderId, uid, e);
}
notifyUserIfCertificateProblem(account, e, true);
Timber.e(e, "Error while loading remote message");
}
}
use of com.fsck.k9.backend.api.SyncConfig in project k-9 by k9mail.
the class MessagingController method syncFolder.
private void syncFolder(Account account, long folderId, boolean notify, MessagingListener listener, Backend backend, NotificationState notificationState) {
ServerSettings serverSettings = account.getIncomingServerSettings();
if (serverSettings.isMissingCredentials()) {
handleAuthenticationFailure(account, true);
return;
}
Exception commandException = null;
try {
processPendingCommandsSynchronous(account);
} catch (Exception e) {
Timber.e(e, "Failure processing command, but allow message sync attempt");
commandException = e;
}
LocalFolder localFolder;
try {
LocalStore localStore = localStoreProvider.getInstance(account);
localFolder = localStore.getFolder(folderId);
localFolder.open();
} catch (MessagingException e) {
Timber.e(e, "syncFolder: Couldn't load local folder %d", folderId);
return;
}
// We can't sync local folders
if (localFolder.isLocalOnly()) {
return;
}
final boolean suppressNotifications;
if (notify) {
MessageStore messageStore = messageStoreManager.getMessageStore(account);
Long lastChecked = messageStore.getFolder(folderId, FolderDetailsAccessor::getLastChecked);
suppressNotifications = lastChecked == null;
} else {
suppressNotifications = true;
}
String folderServerId = localFolder.getServerId();
SyncConfig syncConfig = createSyncConfig(account);
ControllerSyncListener syncListener = new ControllerSyncListener(account, listener, suppressNotifications, notificationState);
backend.sync(folderServerId, syncConfig, syncListener);
if (commandException != null && !syncListener.syncFailed) {
String rootMessage = getRootCauseMessage(commandException);
Timber.e("Root cause failure in %s:%s was '%s'", account, folderServerId, rootMessage);
updateFolderStatus(account, folderServerId, rootMessage);
listener.synchronizeMailboxFailed(account, folderId, rootMessage);
}
}
use of com.fsck.k9.backend.api.SyncConfig in project k-9 by k9mail.
the class WebDavSync method syncFlags.
private boolean syncFlags(SyncConfig syncConfig, BackendFolder backendFolder, WebDavMessage remoteMessage) {
String messageServerId = remoteMessage.getUid();
if (!backendFolder.isMessagePresent(messageServerId)) {
return false;
}
Set<Flag> localMessageFlags = backendFolder.getMessageFlags(messageServerId);
if (localMessageFlags.contains(Flag.DELETED)) {
return false;
}
boolean messageChanged = false;
if (remoteMessage.isSet(Flag.DELETED)) {
if (syncConfig.getSyncRemoteDeletions()) {
backendFolder.setMessageFlag(messageServerId, Flag.DELETED, true);
messageChanged = true;
}
} else {
for (Flag flag : syncConfig.getSyncFlags()) {
if (remoteMessage.isSet(flag) != localMessageFlags.contains(flag)) {
backendFolder.setMessageFlag(messageServerId, flag, remoteMessage.isSet(flag));
messageChanged = true;
}
}
}
return messageChanged;
}
use of com.fsck.k9.backend.api.SyncConfig in project k-9 by k9mail.
the class WebDavSync method synchronizeMailboxSynchronous.
void synchronizeMailboxSynchronous(String folder, SyncConfig syncConfig, SyncListener listener) {
WebDavFolder remoteFolder = null;
Timber.i("Synchronizing folder %s:%s", accountName, folder);
BackendFolder backendFolder = null;
try {
Timber.d("SYNC: About to process pending commands for account %s", accountName);
Timber.v("SYNC: About to get local folder %s", folder);
backendFolder = backendStorage.getFolder(folder);
listener.syncStarted(folder);
/*
* Get the message list from the local store and create an index of
* the uids within the list.
*/
Map<String, Long> localUidMap = backendFolder.getAllMessagesAndEffectiveDates();
Timber.v("SYNC: About to get remote folder %s", folder);
remoteFolder = remoteStore.getFolder(folder);
/*
* Synchronization process:
*
Open the folder
Upload any local messages that are marked as PENDING_UPLOAD (Drafts, Sent, Trash)
Get the message count
Get the list of the newest K9.DEFAULT_VISIBLE_LIMIT messages
getMessages(messageCount - K9.DEFAULT_VISIBLE_LIMIT, messageCount)
See if we have each message locally, if not fetch it's flags and envelope
Get and update the unread count for the folder
Update the remote flags of any messages we have locally with an internal date newer than the remote message.
Get the current flags for any messages we have locally but did not just download
Update local flags
For any message we have locally but not remotely, delete the local message to keep cache clean.
Download larger parts of any new messages.
(Optional) Download small attachments in the background.
*/
/*
* Open the remote folder. This pre-loads certain metadata like message count.
*/
Timber.v("SYNC: About to open remote folder %s", folder);
remoteFolder.open();
listener.syncAuthenticationSuccess();
/*
* Get the remote message count.
*/
int remoteMessageCount = remoteFolder.getMessageCount();
int visibleLimit = backendFolder.getVisibleLimit();
if (visibleLimit < 0) {
visibleLimit = syncConfig.getDefaultVisibleLimit();
}
final List<WebDavMessage> remoteMessages = new ArrayList<>();
Map<String, WebDavMessage> remoteUidMap = new HashMap<>();
Timber.v("SYNC: Remote message count for folder %s is %d", folder, remoteMessageCount);
final Date earliestDate = syncConfig.getEarliestPollDate();
long earliestTimestamp = earliestDate != null ? earliestDate.getTime() : 0L;
int remoteStart = 1;
if (remoteMessageCount > 0) {
/* Message numbers start at 1. */
if (visibleLimit > 0) {
remoteStart = Math.max(0, remoteMessageCount - visibleLimit) + 1;
} else {
remoteStart = 1;
}
Timber.v("SYNC: About to get messages %d through %d for folder %s", remoteStart, remoteMessageCount, folder);
final AtomicInteger headerProgress = new AtomicInteger(0);
listener.syncHeadersStarted(folder);
List<WebDavMessage> remoteMessageArray = remoteFolder.getMessages(remoteStart, remoteMessageCount, null);
int messageCount = remoteMessageArray.size();
for (WebDavMessage thisMess : remoteMessageArray) {
headerProgress.incrementAndGet();
listener.syncHeadersProgress(folder, headerProgress.get(), messageCount);
Long localMessageTimestamp = localUidMap.get(thisMess.getUid());
if (localMessageTimestamp == null || localMessageTimestamp >= earliestTimestamp) {
remoteMessages.add(thisMess);
remoteUidMap.put(thisMess.getUid(), thisMess);
}
}
Timber.v("SYNC: Got %d messages for folder %s", remoteUidMap.size(), folder);
listener.syncHeadersFinished(folder, headerProgress.get(), remoteUidMap.size());
} else if (remoteMessageCount < 0) {
throw new Exception("Message count " + remoteMessageCount + " for folder " + folder);
}
/*
* Remove any messages that are in the local store but no longer on the remote store or are too old
*/
MoreMessages moreMessages = backendFolder.getMoreMessages();
if (syncConfig.getSyncRemoteDeletions()) {
List<String> destroyMessageUids = new ArrayList<>();
for (String localMessageUid : localUidMap.keySet()) {
if (remoteUidMap.get(localMessageUid) == null) {
destroyMessageUids.add(localMessageUid);
}
}
if (!destroyMessageUids.isEmpty()) {
moreMessages = MoreMessages.UNKNOWN;
backendFolder.destroyMessages(destroyMessageUids);
for (String uid : destroyMessageUids) {
listener.syncRemovedMessage(folder, uid);
}
}
}
// noinspection UnusedAssignment, free memory early? (better break up the method!)
localUidMap = null;
if (moreMessages == MoreMessages.UNKNOWN) {
updateMoreMessages(remoteFolder, backendFolder, earliestDate, remoteStart);
}
/*
* Now we download the actual content of messages.
*/
int newMessages = downloadMessages(syncConfig, remoteFolder, backendFolder, remoteMessages, listener);
listener.folderStatusChanged(folder);
/* Notify listeners that we're finally done. */
backendFolder.setLastChecked(System.currentTimeMillis());
backendFolder.setStatus(null);
Timber.d("Done synchronizing folder %s:%s @ %tc with %d new messages", accountName, folder, System.currentTimeMillis(), newMessages);
listener.syncFinished(folder);
Timber.i("Done synchronizing folder %s:%s", accountName, folder);
} catch (AuthenticationFailedException e) {
listener.syncFailed(folder, "Authentication failure", e);
} catch (Exception e) {
Timber.e(e, "synchronizeMailbox");
// If we don't set the last checked, it can try too often during
// failure conditions
String rootMessage = ExceptionHelper.getRootCauseMessage(e);
if (backendFolder != null) {
try {
backendFolder.setStatus(rootMessage);
backendFolder.setLastChecked(System.currentTimeMillis());
} catch (Exception e1) {
Timber.e(e1, "Could not set last checked on folder %s:%s", accountName, folder);
}
}
listener.syncFailed(folder, rootMessage, e);
Timber.e("Failed synchronizing folder %s:%s @ %tc", accountName, folder, System.currentTimeMillis());
} finally {
if (remoteFolder != null) {
remoteFolder.close();
}
}
}
use of com.fsck.k9.backend.api.SyncConfig in project k-9 by k9mail.
the class WebDavSync method downloadLargeMessages.
private void downloadLargeMessages(final SyncConfig syncConfig, final WebDavFolder remoteFolder, final BackendFolder backendFolder, List<WebDavMessage> largeMessages, final AtomicInteger progress, final AtomicInteger newMessages, final int todo, FetchProfile fp, SyncListener listener) throws MessagingException {
final String folder = remoteFolder.getServerId();
Timber.d("SYNC: Fetching large messages for folder %s", folder);
int maxDownloadSize = syncConfig.getMaximumAutoDownloadMessageSize();
remoteFolder.fetch(largeMessages, fp, null, maxDownloadSize);
for (WebDavMessage message : largeMessages) {
downloadSaneBody(syncConfig, remoteFolder, backendFolder, message);
String messageServerId = message.getUid();
Timber.v("About to notify listeners that we got a new large message %s:%s:%s", accountName, folder, messageServerId);
// Update the listener with what we've found
progress.incrementAndGet();
// TODO do we need to re-fetch this here?
Set<Flag> flags = backendFolder.getMessageFlags(messageServerId);
// not marked as read.
if (!flags.contains(Flag.SEEN)) {
newMessages.incrementAndGet();
}
listener.syncProgress(folder, progress.get(), todo);
listener.syncNewMessage(folder, messageServerId, false);
}
Timber.d("SYNC: Done fetching large messages for folder %s", folder);
}
Aggregations