Search in sources :

Example 26 with LocalFolder

use of com.fsck.k9.mailstore.LocalFolder in project k-9 by k9mail.

the class MessagingController method listFoldersSynchronous.

/**
     * Lists folders that are available locally and remotely. This method calls
     * listFoldersCallback for local folders before it returns, and then for
     * remote folders at some later point. If there are no local folders
     * includeRemote is forced by this method. This method is called in the
     * foreground.
     * TODO this needs to cache the remote folder list
     */
public void listFoldersSynchronous(final Account account, final boolean refreshRemote, final MessagingListener listener) {
    for (MessagingListener l : getListeners(listener)) {
        l.listFoldersStarted(account);
    }
    List<LocalFolder> localFolders = null;
    if (!account.isAvailable(context)) {
        Timber.i("not listing folders of unavailable account");
    } else {
        try {
            LocalStore localStore = account.getLocalStore();
            localFolders = localStore.getPersonalNamespaces(false);
            if (refreshRemote || localFolders.isEmpty()) {
                doRefreshRemote(account, listener);
                return;
            }
            for (MessagingListener l : getListeners(listener)) {
                l.listFolders(account, localFolders);
            }
        } catch (Exception e) {
            for (MessagingListener l : getListeners(listener)) {
                l.listFoldersFailed(account, e.getMessage());
            }
            addErrorMessage(account, null, e);
            return;
        } finally {
            if (localFolders != null) {
                for (Folder localFolder : localFolders) {
                    closeFolder(localFolder);
                }
            }
        }
    }
    for (MessagingListener l : getListeners(listener)) {
        l.listFoldersFinished(account);
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) LocalStore(com.fsck.k9.mailstore.LocalStore) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException)

Example 27 with LocalFolder

use of com.fsck.k9.mailstore.LocalFolder in project k-9 by k9mail.

the class MessagingController method refreshRemoteSynchronous.

@VisibleForTesting
void refreshRemoteSynchronous(final Account account, final MessagingListener listener) {
    List<LocalFolder> localFolders = null;
    try {
        Store store = account.getRemoteStore();
        List<? extends Folder> remoteFolders = store.getPersonalNamespaces(false);
        LocalStore localStore = account.getLocalStore();
        Set<String> remoteFolderNames = new HashSet<>();
        List<LocalFolder> foldersToCreate = new LinkedList<>();
        localFolders = localStore.getPersonalNamespaces(false);
        Set<String> localFolderNames = new HashSet<>();
        for (Folder localFolder : localFolders) {
            localFolderNames.add(localFolder.getName());
        }
        for (Folder remoteFolder : remoteFolders) {
            if (!localFolderNames.contains(remoteFolder.getName())) {
                LocalFolder localFolder = localStore.getFolder(remoteFolder.getName());
                foldersToCreate.add(localFolder);
            }
            remoteFolderNames.add(remoteFolder.getName());
        }
        localStore.createFolders(foldersToCreate, account.getDisplayCount());
        localFolders = localStore.getPersonalNamespaces(false);
        /*
             * Clear out any folders that are no longer on the remote store.
             */
        for (Folder localFolder : localFolders) {
            String localFolderName = localFolder.getName();
            //        special placeholder folder "-NONE-".
            if (K9.FOLDER_NONE.equals(localFolderName)) {
                localFolder.delete(false);
            }
            if (!account.isSpecialFolder(localFolderName) && !remoteFolderNames.contains(localFolderName)) {
                localFolder.delete(false);
            }
        }
        localFolders = localStore.getPersonalNamespaces(false);
        for (MessagingListener l : getListeners(listener)) {
            l.listFolders(account, localFolders);
        }
        for (MessagingListener l : getListeners(listener)) {
            l.listFoldersFinished(account);
        }
    } catch (Exception e) {
        for (MessagingListener l : getListeners(listener)) {
            l.listFoldersFailed(account, "");
        }
        addErrorMessage(account, null, e);
    } finally {
        if (localFolders != null) {
            for (Folder localFolder : localFolders) {
                closeFolder(localFolder);
            }
        }
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) LocalStore(com.fsck.k9.mailstore.LocalStore) Store(com.fsck.k9.mail.Store) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) LocalStore(com.fsck.k9.mailstore.LocalStore) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) LinkedList(java.util.LinkedList) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException) HashSet(java.util.HashSet) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 28 with LocalFolder

use of com.fsck.k9.mailstore.LocalFolder in project k-9 by k9mail.

the class MessagingController method downloadPartial.

private void downloadPartial(Folder remoteFolder, LocalFolder localFolder, Message message) throws MessagingException {
    /*
         * We have a structure to deal with, from which
         * we can pull down the parts we want to actually store.
         * Build a list of parts we are interested in. Text parts will be downloaded
         * right now, attachments will be left for later.
         */
    Set<Part> viewables = MessageExtractor.collectTextParts(message);
    /*
         * Now download the parts we're interested in storing.
         */
    for (Part part : viewables) {
        remoteFolder.fetchPart(message, part, null);
    }
    // Store the updated message locally
    localFolder.appendMessages(Collections.singletonList(message));
    Message localMessage = localFolder.getMessage(message.getUid());
    // Set a flag indicating this message has been fully downloaded and can be
    // viewed.
    localMessage.setFlag(Flag.X_DOWNLOADED_PARTIAL, true);
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) Part(com.fsck.k9.mail.Part)

Example 29 with LocalFolder

use of com.fsck.k9.mailstore.LocalFolder in project k-9 by k9mail.

the class ReconstructMessageFromDatabaseTest method testThatByteIdenticalCopyOfMessageIsReconstructed.

public void testThatByteIdenticalCopyOfMessageIsReconstructed() throws IOException, MessagingException {
    LocalFolder folder = createFolderInDatabase();
    MimeMessage message = parseMessage();
    saveMessageToDatabase(folder, message);
    LocalMessage localMessage = readMessageFromDatabase(folder, message);
    String reconstructedMessage = writeMessageToString(localMessage);
    assertEquals(MESSAGE_SOURCE, reconstructedMessage);
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage)

Example 30 with LocalFolder

use of com.fsck.k9.mailstore.LocalFolder in project k-9 by k9mail.

the class MessagingControllerTest method synchronizeMailboxSynchronous_withAccountSetToSyncRemoteDeletions_shouldNotDeleteLocalCopiesOfExistingMessagesAfterEarliestPollDate.

@Test
public void synchronizeMailboxSynchronous_withAccountSetToSyncRemoteDeletions_shouldNotDeleteLocalCopiesOfExistingMessagesAfterEarliestPollDate() throws Exception {
    messageCountInRemoteFolder(1);
    Date dateOfEarliestPoll = new Date();
    LocalMessage localMessage = localMessageWithCopyOnServer();
    when(account.syncRemoteDeletions()).thenReturn(true);
    when(account.getEarliestPollDate()).thenReturn(dateOfEarliestPoll);
    when(localMessage.olderThan(dateOfEarliestPoll)).thenReturn(false);
    when(localFolder.getMessages(null)).thenReturn(Collections.singletonList(localMessage));
    controller.synchronizeMailboxSynchronous(account, FOLDER_NAME, listener, remoteFolder);
    verify(localFolder, never()).destroyMessages(messageListCaptor.capture());
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) Date(java.util.Date) Test(org.junit.Test)

Aggregations

LocalFolder (com.fsck.k9.mailstore.LocalFolder)41 MessagingException (com.fsck.k9.mail.MessagingException)31 LocalMessage (com.fsck.k9.mailstore.LocalMessage)30 LocalStore (com.fsck.k9.mailstore.LocalStore)28 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)22 Folder (com.fsck.k9.mail.Folder)20 Message (com.fsck.k9.mail.Message)20 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)13 FetchProfile (com.fsck.k9.mail.FetchProfile)12 Store (com.fsck.k9.mail.Store)12 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)12 IOException (java.io.IOException)11 SuppressLint (android.annotation.SuppressLint)10 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)10 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)10 ArrayList (java.util.ArrayList)10 Date (java.util.Date)10 Test (org.junit.Test)9 VisibleForTesting (android.support.annotation.VisibleForTesting)5 HashMap (java.util.HashMap)5