Search in sources :

Example 1 with WebDavFolder

use of com.fsck.k9.mail.store.webdav.WebDavFolder in project k-9 by k9mail.

the class WebDavStoreTest method getFolder_calledTwice_shouldReturnFirstInstance.

@Test
public void getFolder_calledTwice_shouldReturnFirstInstance() throws Exception {
    WebDavStore webDavStore = createDefaultWebDavStore();
    String folderName = "Trash";
    Folder webDavFolder = webDavStore.getFolder(folderName);
    Folder result = webDavStore.getFolder(folderName);
    assertSame(webDavFolder, result);
}
Also used : Folder(com.fsck.k9.mail.Folder) Test(org.junit.Test)

Example 2 with WebDavFolder

use of com.fsck.k9.mail.store.webdav.WebDavFolder in project k-9 by k9mail.

the class CommandSetFlag method setFlag.

void setFlag(@NotNull String folderServerId, @NotNull List<String> messageServerIds, @NotNull Flag flag, boolean newState) throws MessagingException {
    WebDavFolder remoteFolder = webDavStore.getFolder(folderServerId);
    try {
        remoteFolder.open();
        List<WebDavMessage> messages = new ArrayList<>();
        for (String uid : messageServerIds) {
            messages.add(remoteFolder.getMessage(uid));
        }
        if (messages.isEmpty()) {
            return;
        }
        remoteFolder.setFlags(messages, Collections.singleton(flag), newState);
    } finally {
        remoteFolder.close();
    }
}
Also used : WebDavFolder(com.fsck.k9.mail.store.webdav.WebDavFolder) ArrayList(java.util.ArrayList) WebDavMessage(com.fsck.k9.mail.store.webdav.WebDavMessage)

Example 3 with WebDavFolder

use of com.fsck.k9.mail.store.webdav.WebDavFolder in project k-9 by k9mail.

the class CommandMoveOrCopyMessages method moveOrCopyMessages.

private Map<String, String> moveOrCopyMessages(String srcFolder, String destFolder, Collection<String> uids, boolean isCopy) throws MessagingException {
    WebDavFolder remoteSrcFolder = null;
    WebDavFolder remoteDestFolder = null;
    try {
        remoteSrcFolder = webDavStore.getFolder(srcFolder);
        List<WebDavMessage> messages = new ArrayList<>();
        for (String uid : uids) {
            messages.add(remoteSrcFolder.getMessage(uid));
        }
        if (messages.isEmpty()) {
            Timber.i("processingPendingMoveOrCopy: no remote messages to move, skipping");
            return null;
        }
        remoteSrcFolder.open();
        Timber.d("processingPendingMoveOrCopy: source folder = %s, %d messages, " + "destination folder = %s, isCopy = %s", srcFolder, messages.size(), destFolder, isCopy);
        remoteDestFolder = webDavStore.getFolder(destFolder);
        if (isCopy) {
            return remoteSrcFolder.copyMessages(messages, remoteDestFolder);
        } else {
            return remoteSrcFolder.moveMessages(messages, remoteDestFolder);
        }
    } finally {
        closeFolder(remoteSrcFolder);
        closeFolder(remoteDestFolder);
    }
}
Also used : WebDavFolder(com.fsck.k9.mail.store.webdav.WebDavFolder) ArrayList(java.util.ArrayList) WebDavMessage(com.fsck.k9.mail.store.webdav.WebDavMessage)

Example 4 with WebDavFolder

use of com.fsck.k9.mail.store.webdav.WebDavFolder 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();
        }
    }
}
Also used : WebDavFolder(com.fsck.k9.mail.store.webdav.WebDavFolder) HashMap(java.util.HashMap) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException) ArrayList(java.util.ArrayList) WebDavMessage(com.fsck.k9.mail.store.webdav.WebDavMessage) Date(java.util.Date) MessagingException(com.fsck.k9.mail.MessagingException) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException) BackendFolder(com.fsck.k9.backend.api.BackendFolder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MoreMessages(com.fsck.k9.backend.api.BackendFolder.MoreMessages)

Example 5 with WebDavFolder

use of com.fsck.k9.mail.store.webdav.WebDavFolder in project k-9 by k9mail.

the class WebDavSync method updateMoreMessages.

private void updateMoreMessages(WebDavFolder remoteFolder, BackendFolder backendFolder, Date earliestDate, int remoteStart) {
    if (remoteStart == 1) {
        backendFolder.setMoreMessages(MoreMessages.FALSE);
    } else {
        boolean moreMessagesAvailable = remoteFolder.areMoreMessagesAvailable(remoteStart, earliestDate);
        MoreMessages newMoreMessages = (moreMessagesAvailable) ? MoreMessages.TRUE : MoreMessages.FALSE;
        backendFolder.setMoreMessages(newMoreMessages);
    }
}
Also used : MoreMessages(com.fsck.k9.backend.api.BackendFolder.MoreMessages)

Aggregations

WebDavMessage (com.fsck.k9.mail.store.webdav.WebDavMessage)7 ArrayList (java.util.ArrayList)4 FetchProfile (com.fsck.k9.mail.FetchProfile)3 WebDavFolder (com.fsck.k9.mail.store.webdav.WebDavFolder)3 Date (java.util.Date)3 MoreMessages (com.fsck.k9.backend.api.BackendFolder.MoreMessages)2 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)2 MessagingException (com.fsck.k9.mail.MessagingException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 BackendFolder (com.fsck.k9.backend.api.BackendFolder)1 Flag (com.fsck.k9.mail.Flag)1 Folder (com.fsck.k9.mail.Folder)1 FolderType (com.fsck.k9.mail.FolderType)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1