Search in sources :

Example 56 with LocalFolder

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

the class MessagingController method loadMessageRemoteSynchronous.

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();
        localFolder = localStore.getFolder(folder);
        localFolder.open(Folder.OPEN_MODE_RW);
        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);
        }
        /* commented out because this was pulled from another unmerged branch:
            } else if (localFolder.isLocalOnly() && !force) {
                Log.w(K9.LOG_TAG, "Message in local-only folder so cannot download fully.");
                // ASH move toast
                android.widget.Toast.makeText(mApplication,
                        "Message in local-only folder so cannot download fully",
                        android.widget.Toast.LENGTH_LONG).show();
                message.setFlag(Flag.X_DOWNLOADED_FULL, true);
                message.setFlag(Flag.X_DOWNLOADED_PARTIAL, false);
            }*/
        /*if (!message.isSet(Flag.X_DOWNLOADED_FULL)) */
        {
            /*
                 * At this point the message is not available, so we need to download it
                 * fully if possible.
                 */
            Store 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);
            if (loadPartialFromSearch) {
                downloadMessages(account, remoteFolder, localFolder, Collections.singletonList(remoteMessage), false, false);
            } else {
                FetchProfile fp = new FetchProfile();
                fp.add(FetchProfile.Item.BODY);
                remoteFolder.fetch(Collections.singletonList(remoteMessage), fp, null);
                localFolder.appendMessages(Collections.singletonList(remoteMessage));
            }
            message = localFolder.getMessage(uid);
            if (!loadPartialFromSearch) {
                message.setFlag(Flag.X_DOWNLOADED_FULL, true);
            }
        }
        // Mark that this message is now fully synched
        if (account.isMarkMessageAsReadOnView()) {
            message.setFlag(Flag.SEEN, true);
        }
        // now that we have the full message, refresh the headers
        for (MessagingListener l : getListeners(listener)) {
            l.loadMessageRemoteFinished(account, folder, uid);
        }
        return true;
    } catch (Exception e) {
        for (MessagingListener l : getListeners(listener)) {
            l.loadMessageRemoteFailed(account, folder, uid, e);
        }
        notifyUserIfCertificateProblem(account, e, true);
        addErrorMessage(account, null, e);
        return false;
    } finally {
        closeFolder(remoteFolder);
        closeFolder(localFolder);
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) LocalMessage(com.fsck.k9.mailstore.LocalMessage) FetchProfile(com.fsck.k9.mail.FetchProfile) LocalMessage(com.fsck.k9.mailstore.LocalMessage) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) 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) 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 57 with LocalFolder

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

the class MlfUtils method setLastSelectedFolderName.

static void setLastSelectedFolderName(Preferences preferences, List<MessageReference> messages, String destFolderName) {
    try {
        MessageReference firstMsg = messages.get(0);
        Account account = preferences.getAccount(firstMsg.getAccountUuid());
        LocalFolder firstMsgFolder = MlfUtils.getOpenFolder(firstMsg.getFolderName(), account);
        firstMsgFolder.setLastSelectedFolderName(destFolderName);
    } catch (MessagingException e) {
        Timber.e(e, "Error getting folder for setLastSelectedFolderName()");
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) Account(com.fsck.k9.Account) MessagingException(com.fsck.k9.mail.MessagingException) MessageReference(com.fsck.k9.activity.MessageReference)

Example 58 with LocalFolder

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

the class MessagingControllerTest method synchronizeMailboxSynchronous_withAccountSetToSyncRemoteDeletions_shouldDeleteLocalCopiesOfExistingMessagesBeforeEarliestPollDate.

@Test
public void synchronizeMailboxSynchronous_withAccountSetToSyncRemoteDeletions_shouldDeleteLocalCopiesOfExistingMessagesBeforeEarliestPollDate() throws Exception {
    messageCountInRemoteFolder(1);
    LocalMessage localMessage = localMessageWithCopyOnServer();
    Date dateOfEarliestPoll = new Date();
    when(account.syncRemoteDeletions()).thenReturn(true);
    when(account.getEarliestPollDate()).thenReturn(dateOfEarliestPoll);
    when(localMessage.olderThan(dateOfEarliestPoll)).thenReturn(true);
    when(localFolder.getAllMessagesAndEffectiveDates()).thenReturn(Collections.singletonMap(MESSAGE_UID1, 0L));
    when(localFolder.getMessagesByUids(any(List.class))).thenReturn(Collections.singletonList(localMessage));
    controller.synchronizeMailboxSynchronous(account, FOLDER_NAME, listener, remoteFolder);
    verify(localFolder).destroyMessages(messageListCaptor.capture());
    assertEquals(localMessage, messageListCaptor.getValue().get(0));
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) List(java.util.List) ArrayList(java.util.ArrayList) Date(java.util.Date) Test(org.junit.Test)

Example 59 with LocalFolder

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

the class MessagingControllerTest method synchronizeMailboxSynchronous_withAccountSetNotToSyncRemoteDeletions_shouldNotDeleteLocalCopiesOfMessages.

@Test
public void synchronizeMailboxSynchronous_withAccountSetNotToSyncRemoteDeletions_shouldNotDeleteLocalCopiesOfMessages() throws Exception {
    messageCountInRemoteFolder(0);
    LocalMessage remoteDeletedMessage = mock(LocalMessage.class);
    when(account.syncRemoteDeletions()).thenReturn(false);
    when(localFolder.getMessages(null)).thenReturn(Collections.singletonList(remoteDeletedMessage));
    controller.synchronizeMailboxSynchronous(account, FOLDER_NAME, listener, remoteFolder);
    verify(localFolder, never()).destroyMessages(messageListCaptor.capture());
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) Test(org.junit.Test)

Example 60 with LocalFolder

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

the class MessagingController method loadSearchResults.

public void loadSearchResults(final Account account, final String folderName, final List<Message> messages, final MessagingListener listener) {
    threadPool.execute(new Runnable() {

        @Override
        public void run() {
            if (listener != null) {
                listener.enableProgressIndicator(true);
            }
            try {
                Store remoteStore = account.getRemoteStore();
                LocalStore localStore = account.getLocalStore();
                if (remoteStore == null || localStore == null) {
                    throw new MessagingException("Could not get store");
                }
                Folder remoteFolder = remoteStore.getFolder(folderName);
                LocalFolder localFolder = localStore.getFolder(folderName);
                if (remoteFolder == null || localFolder == null) {
                    throw new MessagingException("Folder not found");
                }
                loadSearchResultsSynchronous(messages, localFolder, remoteFolder, listener);
            } catch (MessagingException e) {
                Timber.e(e, "Exception in loadSearchResults");
                addErrorMessage(account, null, e);
            } finally {
                if (listener != null) {
                    listener.enableProgressIndicator(false);
                }
            }
        }
    });
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) MessagingException(com.fsck.k9.mail.MessagingException) 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)

Aggregations

LocalFolder (com.fsck.k9.mailstore.LocalFolder)62 LocalStore (com.fsck.k9.mailstore.LocalStore)47 MessagingException (com.fsck.k9.mail.MessagingException)44 LocalMessage (com.fsck.k9.mailstore.LocalMessage)40 Message (com.fsck.k9.mail.Message)20 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)20 Folder (com.fsck.k9.mail.Folder)17 FetchProfile (com.fsck.k9.mail.FetchProfile)15 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)13 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)13 ArrayList (java.util.ArrayList)13 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)11 SuppressLint (android.annotation.SuppressLint)10 Backend (com.fsck.k9.backend.api.Backend)10 Store (com.fsck.k9.mail.Store)10 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)10 IOException (java.io.IOException)9 Date (java.util.Date)9 Test (org.junit.Test)9 Account (com.fsck.k9.Account)7