Search in sources :

Example 51 with LocalFolder

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

the class MessagingController method moveOrDeleteSentMessage.

private void moveOrDeleteSentMessage(Account account, LocalStore localStore, LocalMessage message) throws MessagingException {
    if (!account.hasSentFolder() || !account.isUploadSentMessages()) {
        Timber.i("Not uploading sent message; deleting local message");
        message.destroy();
    } else {
        long sentFolderId = account.getSentFolderId();
        LocalFolder sentFolder = localStore.getFolder(sentFolderId);
        sentFolder.open();
        String sentFolderServerId = sentFolder.getServerId();
        Timber.i("Moving sent message to folder '%s' (%d)", sentFolderServerId, sentFolderId);
        MessageStore messageStore = messageStoreManager.getMessageStore(account);
        long destinationMessageId = messageStore.moveMessage(message.getDatabaseId(), sentFolderId);
        Timber.i("Moved sent message to folder '%s' (%d)", sentFolderServerId, sentFolderId);
        if (!sentFolder.isLocalOnly()) {
            String destinationUid = messageStore.getMessageServerId(destinationMessageId);
            PendingCommand command = PendingAppend.create(sentFolderId, destinationUid);
            queuePendingCommand(account, command);
            processPendingCommands(account);
        }
    }
    for (MessagingListener listener : getListeners()) {
        listener.folderStatusChanged(account, account.getOutboxFolderId());
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) MessageStore(com.fsck.k9.mailstore.MessageStore) PendingCommand(com.fsck.k9.controller.MessagingControllerCommands.PendingCommand)

Example 52 with LocalFolder

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

the class MessagingController method checkMailForAccount.

private void checkMailForAccount(Account account, boolean ignoreLastCheckedTime, boolean notify, MessagingListener listener) {
    Timber.i("Synchronizing account %s", account);
    NotificationState notificationState = new NotificationState();
    sendPendingMessages(account, listener);
    refreshFolderListIfStale(account);
    try {
        Account.FolderMode aDisplayMode = account.getFolderDisplayMode();
        Account.FolderMode aSyncMode = account.getFolderSyncMode();
        LocalStore localStore = localStoreProvider.getInstance(account);
        for (final LocalFolder folder : localStore.getPersonalNamespaces(false)) {
            folder.open();
            FolderClass fDisplayClass = folder.getDisplayClass();
            FolderClass fSyncClass = folder.getSyncClass();
            if (LocalFolder.isModeMismatch(aDisplayMode, fDisplayClass)) {
                // Never sync a folder that isn't displayed
                continue;
            }
            if (LocalFolder.isModeMismatch(aSyncMode, fSyncClass)) {
                // Do not sync folders in the wrong class
                continue;
            }
            synchronizeFolder(account, folder, ignoreLastCheckedTime, notify, listener, notificationState);
        }
    } catch (MessagingException e) {
        Timber.e(e, "Unable to synchronize account %s", account);
    } finally {
        putBackground("clear notification flag for " + account, null, new Runnable() {

            @Override
            public void run() {
                Timber.v("Clearing notification flag for %s", account);
                clearFetchingMailNotification(account);
                if (getUnreadMessageCount(account) == 0) {
                    notificationController.clearNewMailNotifications(account, false);
                }
            }
        });
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) SearchAccount(com.fsck.k9.search.SearchAccount) Account(com.fsck.k9.Account) MessagingException(com.fsck.k9.mail.MessagingException) LocalStore(com.fsck.k9.mailstore.LocalStore) FolderClass(com.fsck.k9.mail.FolderClass)

Example 53 with LocalFolder

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

the class MessagingControllerTest method searchLocalMessagesSynchronous_shouldNotifyWhenStoreFinishesRetrievingAMessage.

@Test
public void searchLocalMessagesSynchronous_shouldNotifyWhenStoreFinishesRetrievingAMessage() throws Exception {
    LocalMessage localMessage = mock(LocalMessage.class);
    when(localMessage.getFolder()).thenReturn(localFolder);
    when(search.searchAllAccounts()).thenReturn(true);
    when(search.getAccountUuids()).thenReturn(new String[0]);
    when(localStore.searchForMessages(nullable(MessageRetrievalListener.class), eq(search))).thenThrow(new MessagingException("Test"));
    controller.searchLocalMessagesSynchronous(search, listener);
    verify(localStore).searchForMessages(messageRetrievalListenerCaptor.capture(), eq(search));
    messageRetrievalListenerCaptor.getValue().messageFinished(localMessage, 1, 1);
    verify(listener).listLocalMessagesAddMessages(eq(account), eq((String) null), eq(Collections.singletonList(localMessage)));
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) MessagingException(com.fsck.k9.mail.MessagingException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MessageRetrievalListener(com.fsck.k9.mail.MessageRetrievalListener) K9RobolectricTest(com.fsck.k9.K9RobolectricTest) Test(org.junit.Test)

Example 54 with LocalFolder

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

the class MessagingController method moveOrDeleteSentMessage.

private void moveOrDeleteSentMessage(Account account, LocalStore localStore, LocalFolder localFolder, LocalMessage message) throws MessagingException {
    if (!account.hasSentFolder()) {
        Timber.i("Account does not have a sent mail folder; deleting sent message");
        message.setFlag(Flag.DELETED, true);
    } else {
        LocalFolder localSentFolder = localStore.getFolder(account.getSentFolderName());
        Timber.i("Moving sent message to folder '%s' (%d)", account.getSentFolderName(), localSentFolder.getId());
        localFolder.moveMessages(Collections.singletonList(message), localSentFolder);
        Timber.i("Moved sent message to folder '%s' (%d)", account.getSentFolderName(), localSentFolder.getId());
        PendingCommand command = PendingAppend.create(localSentFolder.getName(), message.getUid());
        queuePendingCommand(account, command);
        processPendingCommands(account);
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) PendingCommand(com.fsck.k9.controller.MessagingControllerCommands.PendingCommand)

Example 55 with LocalFolder

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

the class MessagingController method searchRemoteMessagesSynchronous.

@VisibleForTesting
void searchRemoteMessagesSynchronous(final String acctUuid, final String folderName, final String query, final Set<Flag> requiredFlags, final Set<Flag> forbiddenFlags, final MessagingListener listener) {
    final Account acct = Preferences.getPreferences(context).getAccount(acctUuid);
    if (listener != null) {
        listener.remoteSearchStarted(folderName);
    }
    List<Message> extraResults = new ArrayList<>();
    try {
        Store remoteStore = acct.getRemoteStore();
        LocalStore localStore = acct.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");
        }
        List<Message> messages = remoteFolder.search(query, requiredFlags, forbiddenFlags);
        Timber.i("Remote search got %d results", messages.size());
        // There's no need to fetch messages already completely downloaded
        List<Message> remoteMessages = localFolder.extractNewMessages(messages);
        messages.clear();
        if (listener != null) {
            listener.remoteSearchServerQueryComplete(folderName, remoteMessages.size(), acct.getRemoteSearchNumResults());
        }
        Collections.sort(remoteMessages, new UidReverseComparator());
        int resultLimit = acct.getRemoteSearchNumResults();
        if (resultLimit > 0 && remoteMessages.size() > resultLimit) {
            extraResults = remoteMessages.subList(resultLimit, remoteMessages.size());
            remoteMessages = remoteMessages.subList(0, resultLimit);
        }
        loadSearchResultsSynchronous(remoteMessages, localFolder, remoteFolder, listener);
    } catch (Exception e) {
        if (Thread.currentThread().isInterrupted()) {
            Timber.i(e, "Caught exception on aborted remote search; safe to ignore.");
        } else {
            Timber.e(e, "Could not complete remote search");
            if (listener != null) {
                listener.remoteSearchFailed(null, e.getMessage());
            }
            addErrorMessage(acct, null, e);
        }
    } finally {
        if (listener != null) {
            listener.remoteSearchFinished(folderName, 0, acct.getRemoteSearchNumResults(), extraResults);
        }
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) SearchAccount(com.fsck.k9.search.SearchAccount) Account(com.fsck.k9.Account) LocalMessage(com.fsck.k9.mailstore.LocalMessage) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MessagingException(com.fsck.k9.mail.MessagingException) ArrayList(java.util.ArrayList) 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) SuppressLint(android.annotation.SuppressLint) 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) VisibleForTesting(android.support.annotation.VisibleForTesting)

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