Search in sources :

Example 6 with WebDavFolder

use of com.fsck.k9.mail.store.webdav.WebDavFolder 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);
}
Also used : WebDavMessage(com.fsck.k9.mail.store.webdav.WebDavMessage) Flag(com.fsck.k9.mail.Flag)

Example 7 with WebDavFolder

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

the class WebDavSync method refreshLocalMessageFlags.

private void refreshLocalMessageFlags(final SyncConfig syncConfig, final WebDavFolder remoteFolder, final BackendFolder backendFolder, List<WebDavMessage> syncFlagMessages, final AtomicInteger progress, final int todo, SyncListener listener) throws MessagingException {
    final String folder = remoteFolder.getServerId();
    Timber.d("SYNC: About to sync flags for %d remote messages for folder %s", syncFlagMessages.size(), folder);
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.FLAGS);
    List<WebDavMessage> undeletedMessages = new LinkedList<>();
    for (WebDavMessage message : syncFlagMessages) {
        if (!message.isSet(Flag.DELETED)) {
            undeletedMessages.add(message);
        }
    }
    int maxDownloadSize = syncConfig.getMaximumAutoDownloadMessageSize();
    remoteFolder.fetch(undeletedMessages, fp, null, maxDownloadSize);
    for (WebDavMessage remoteMessage : syncFlagMessages) {
        boolean messageChanged = syncFlags(syncConfig, backendFolder, remoteMessage);
        if (messageChanged) {
            listener.syncFlagChanged(folder, remoteMessage.getUid());
        }
        progress.incrementAndGet();
        listener.syncProgress(folder, progress.get(), todo);
    }
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) WebDavMessage(com.fsck.k9.mail.store.webdav.WebDavMessage) LinkedList(java.util.LinkedList)

Example 8 with WebDavFolder

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

the class WebDavStore method createFolder.

/**
 * Creates a folder using the URL passed as parameter (only if it has not been already created) and adds this to
 * our store folder map.
 */
private WebDavFolder createFolder(String folderUrl, Map<String, String> specialFoldersMap) {
    if (folderUrl == null) {
        return null;
    }
    WebDavFolder wdFolder = null;
    String folderName = getFolderName(folderUrl);
    if (folderName != null) {
        wdFolder = getFolder(folderName);
        if (wdFolder != null) {
            wdFolder.setUrl(folderUrl);
            FolderType type = getFolderType(folderName, specialFoldersMap);
            wdFolder.setType(type);
        }
    }
    return wdFolder;
}
Also used : FolderType(com.fsck.k9.mail.FolderType)

Example 9 with WebDavFolder

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

the class WebDavSync method downloadSaneBody.

private void downloadSaneBody(SyncConfig syncConfig, WebDavFolder remoteFolder, BackendFolder backendFolder, WebDavMessage message) throws MessagingException {
    /*
         * The provider was unable to get the structure of the message, so
         * we'll download a reasonable portion of the messge and mark it as
         * incomplete so the entire thing can be downloaded later if the user
         * wishes to download it.
         */
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.BODY_SANE);
    /*
         *  TODO a good optimization here would be to make sure that all Stores set
         *  the proper size after this fetch and compare the before and after size. If
         *  they equal we can mark this SYNCHRONIZED instead of PARTIALLY_SYNCHRONIZED
         */
    int maxDownloadSize = syncConfig.getMaximumAutoDownloadMessageSize();
    remoteFolder.fetch(Collections.singletonList(message), fp, null, maxDownloadSize);
    boolean completeMessage = false;
    // Certain (POP3) servers give you the whole message even when you ask for only the first x Kb
    if (!message.isSet(Flag.X_DOWNLOADED_FULL)) {
        /*
             * Mark the message as fully downloaded if the message size is smaller than
             * the account's autodownload size limit, otherwise mark as only a partial
             * download.  This will prevent the system from downloading the same message
             * twice.
             *
             * If there is no limit on autodownload size, that's the same as the message
             * being smaller than the max size
             */
        if (syncConfig.getMaximumAutoDownloadMessageSize() == 0 || message.getSize() < syncConfig.getMaximumAutoDownloadMessageSize()) {
            completeMessage = true;
        }
    }
    // Store the updated message locally
    if (completeMessage) {
        backendFolder.saveMessage(message, MessageDownloadState.FULL);
    } else {
        backendFolder.saveMessage(message, MessageDownloadState.PARTIAL);
    }
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile)

Example 10 with WebDavFolder

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

the class WebDavSync method downloadMessages.

private int downloadMessages(final SyncConfig syncConfig, final WebDavFolder remoteFolder, final BackendFolder backendFolder, List<WebDavMessage> inputMessages, final SyncListener listener) throws MessagingException {
    final Date earliestDate = syncConfig.getEarliestPollDate();
    // now
    Date downloadStarted = new Date();
    if (earliestDate != null) {
        Timber.d("Only syncing messages after %s", earliestDate);
    }
    final String folder = remoteFolder.getServerId();
    List<WebDavMessage> syncFlagMessages = new ArrayList<>();
    List<WebDavMessage> unsyncedMessages = new ArrayList<>();
    final AtomicInteger newMessages = new AtomicInteger(0);
    List<WebDavMessage> messages = new ArrayList<>(inputMessages);
    for (WebDavMessage message : messages) {
        evaluateMessageForDownload(message, folder, backendFolder, unsyncedMessages, syncFlagMessages, listener);
    }
    final AtomicInteger progress = new AtomicInteger(0);
    final int todo = unsyncedMessages.size() + syncFlagMessages.size();
    listener.syncProgress(folder, progress.get(), todo);
    Timber.d("SYNC: Have %d unsynced messages", unsyncedMessages.size());
    messages.clear();
    final List<WebDavMessage> largeMessages = new ArrayList<>();
    final List<WebDavMessage> smallMessages = new ArrayList<>();
    if (!unsyncedMessages.isEmpty()) {
        int visibleLimit = backendFolder.getVisibleLimit();
        int listSize = unsyncedMessages.size();
        if ((visibleLimit > 0) && (listSize > visibleLimit)) {
            unsyncedMessages = unsyncedMessages.subList(0, visibleLimit);
        }
        FetchProfile fp = new FetchProfile();
        fp.add(FetchProfile.Item.FLAGS);
        fp.add(FetchProfile.Item.ENVELOPE);
        Timber.d("SYNC: About to fetch %d unsynced messages for folder %s", unsyncedMessages.size(), folder);
        fetchUnsyncedMessages(syncConfig, remoteFolder, unsyncedMessages, smallMessages, largeMessages, progress, todo, fp, listener);
        Timber.d("SYNC: Synced unsynced messages for folder %s", folder);
    }
    Timber.d("SYNC: Have %d large messages and %d small messages out of %d unsynced messages", largeMessages.size(), smallMessages.size(), unsyncedMessages.size());
    unsyncedMessages.clear();
    /*
         * Grab the content of the small messages first. This is going to
         * be very fast and at very worst will be a single up of a few bytes and a single
         * download of 625k.
         */
    FetchProfile fp = new FetchProfile();
    // TODO: Only fetch small and large messages if we have some
    fp.add(FetchProfile.Item.BODY);
    // fp.add(FetchProfile.Item.FLAGS);
    // fp.add(FetchProfile.Item.ENVELOPE);
    downloadSmallMessages(remoteFolder, backendFolder, smallMessages, progress, newMessages, todo, fp, listener);
    smallMessages.clear();
    /*
         * Now do the large messages that require more round trips.
         */
    fp = new FetchProfile();
    fp.add(FetchProfile.Item.STRUCTURE);
    downloadLargeMessages(syncConfig, remoteFolder, backendFolder, largeMessages, progress, newMessages, todo, fp, listener);
    largeMessages.clear();
    /*
         * Refresh the flags for any messages in the local store that we didn't just
         * download.
         */
    refreshLocalMessageFlags(syncConfig, remoteFolder, backendFolder, syncFlagMessages, progress, todo, listener);
    Timber.d("SYNC: Synced remote messages for folder %s, %d new messages", folder, newMessages.get());
    return newMessages.get();
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) WebDavMessage(com.fsck.k9.mail.store.webdav.WebDavMessage) Date(java.util.Date)

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