Search in sources :

Example 36 with FetchProfile

use of com.fsck.k9.mail.FetchProfile in project k-9 by k9mail.

the class MessagingController method downloadSmallMessages.

private <T extends Message> void downloadSmallMessages(final Account account, final Folder<T> remoteFolder, final LocalFolder localFolder, List<T> smallMessages, final AtomicInteger progress, final int unreadBeforeStart, final AtomicInteger newMessages, final int todo, FetchProfile fp) throws MessagingException {
    final String folder = remoteFolder.getName();
    final Date earliestDate = account.getEarliestPollDate();
    Timber.d("SYNC: Fetching %d small messages for folder %s", smallMessages.size(), folder);
    remoteFolder.fetch(smallMessages, fp, new MessageRetrievalListener<T>() {

        @Override
        public void messageFinished(final T message, int number, int ofTotal) {
            try {
                if (!shouldImportMessage(account, message, earliestDate)) {
                    progress.incrementAndGet();
                    return;
                }
                // Store the updated message locally
                final LocalMessage localMessage = localFolder.storeSmallMessage(message, new Runnable() {

                    @Override
                    public void run() {
                        progress.incrementAndGet();
                    }
                });
                // not marked as read.
                if (!localMessage.isSet(Flag.SEEN)) {
                    newMessages.incrementAndGet();
                }
                Timber.v("About to notify listeners that we got a new small message %s:%s:%s", account, folder, message.getUid());
                // Update the listener with what we've found
                for (MessagingListener l : getListeners()) {
                    l.synchronizeMailboxProgress(account, folder, progress.get(), todo);
                    if (!localMessage.isSet(Flag.SEEN)) {
                        l.synchronizeMailboxNewMessage(account, folder, localMessage);
                    }
                }
                if (shouldNotifyForMessage(account, localFolder, message)) {
                    // Notify with the localMessage so that we don't have to recalculate the content preview.
                    notificationController.addNewMailNotification(account, localMessage, unreadBeforeStart);
                }
            } catch (MessagingException me) {
                addErrorMessage(account, null, me);
                Timber.e(me, "SYNC: fetch small messages");
            }
        }

        @Override
        public void messageStarted(String uid, int number, int ofTotal) {
        }

        @Override
        public void messagesFinished(int total) {
        }
    });
    Timber.d("SYNC: Done fetching small messages for folder %s", folder);
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) MessagingException(com.fsck.k9.mail.MessagingException) Date(java.util.Date) SuppressLint(android.annotation.SuppressLint)

Example 37 with FetchProfile

use of com.fsck.k9.mail.FetchProfile in project k-9 by k9mail.

the class MessagingController method fetchUnsyncedMessages.

private <T extends Message> void fetchUnsyncedMessages(final Account account, final Folder<T> remoteFolder, List<T> unsyncedMessages, final List<Message> smallMessages, final List<Message> largeMessages, final AtomicInteger progress, final int todo, FetchProfile fp) throws MessagingException {
    final String folder = remoteFolder.getName();
    final Date earliestDate = account.getEarliestPollDate();
    remoteFolder.fetch(unsyncedMessages, fp, new MessageRetrievalListener<T>() {

        @Override
        public void messageFinished(T message, int number, int ofTotal) {
            try {
                if (message.isSet(Flag.DELETED) || message.olderThan(earliestDate)) {
                    if (K9.isDebug()) {
                        if (message.isSet(Flag.DELETED)) {
                            Timber.v("Newly downloaded message %s:%s:%s was marked deleted on server, " + "skipping", account, folder, message.getUid());
                        } else {
                            Timber.d("Newly downloaded message %s is older than %s, skipping", message.getUid(), earliestDate);
                        }
                    }
                    progress.incrementAndGet();
                    for (MessagingListener l : getListeners()) {
                        //TODO: This might be the source of poll count errors in the UI. Is todo always the same as ofTotal
                        l.synchronizeMailboxProgress(account, folder, progress.get(), todo);
                    }
                    return;
                }
                if (account.getMaximumAutoDownloadMessageSize() > 0 && message.getSize() > account.getMaximumAutoDownloadMessageSize()) {
                    largeMessages.add(message);
                } else {
                    smallMessages.add(message);
                }
            } catch (Exception e) {
                Timber.e(e, "Error while storing downloaded message.");
                addErrorMessage(account, null, e);
            }
        }

        @Override
        public void messageStarted(String uid, int number, int ofTotal) {
        }

        @Override
        public void messagesFinished(int total) {
        // FIXME this method is almost never invoked by various Stores! Don't rely on it unless fixed!!
        }
    });
}
Also used : Date(java.util.Date) 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)

Example 38 with FetchProfile

use of com.fsck.k9.mail.FetchProfile in project k-9 by k9mail.

the class ImapFolderTest method fetch_withBodySaneFetchProfile_shouldIssueRespectiveCommand.

@Test
public void fetch_withBodySaneFetchProfile_shouldIssueRespectiveCommand() throws Exception {
    ImapFolder folder = createFolder("Folder");
    prepareImapFolderForOpen(OPEN_MODE_RO);
    folder.open(OPEN_MODE_RO);
    when(imapConnection.readResponse(any(ImapResponseCallback.class))).thenReturn(createImapResponse("x OK"));
    List<ImapMessage> messages = createImapMessages("1");
    FetchProfile fetchProfile = createFetchProfile(Item.BODY_SANE);
    when(storeConfig.getMaximumAutoDownloadMessageSize()).thenReturn(4096);
    folder.fetch(messages, fetchProfile, null);
    verify(imapConnection).sendCommand("UID FETCH 1 (UID BODY.PEEK[]<0.4096>)", false);
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) Test(org.junit.Test)

Example 39 with FetchProfile

use of com.fsck.k9.mail.FetchProfile in project k-9 by k9mail.

the class ImapFolderTest method fetch_withFlagsFetchProfile_shouldSetFlags.

@Test
public void fetch_withFlagsFetchProfile_shouldSetFlags() throws Exception {
    ImapFolder folder = createFolder("Folder");
    prepareImapFolderForOpen(OPEN_MODE_RO);
    folder.open(OPEN_MODE_RO);
    List<ImapMessage> messages = createImapMessages("1");
    FetchProfile fetchProfile = createFetchProfile(Item.FLAGS);
    when(imapConnection.readResponse(any(ImapResponseCallback.class))).thenReturn(createImapResponse("* 1 FETCH (FLAGS (\\Seen) UID 1)")).thenReturn(createImapResponse("x OK"));
    folder.fetch(messages, fetchProfile, null);
    ImapMessage imapMessage = messages.get(0);
    verify(imapMessage).setFlagInternal(Flag.SEEN, true);
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) Test(org.junit.Test)

Example 40 with FetchProfile

use of com.fsck.k9.mail.FetchProfile in project k-9 by k9mail.

the class ImapFolderTest method fetch_withStructureFetchProfile_shouldIssueRespectiveCommand.

@Test
public void fetch_withStructureFetchProfile_shouldIssueRespectiveCommand() throws Exception {
    ImapFolder folder = createFolder("Folder");
    prepareImapFolderForOpen(OPEN_MODE_RO);
    folder.open(OPEN_MODE_RO);
    when(imapConnection.readResponse(any(ImapResponseCallback.class))).thenReturn(createImapResponse("x OK"));
    List<ImapMessage> messages = createImapMessages("1");
    FetchProfile fetchProfile = createFetchProfile(Item.STRUCTURE);
    folder.fetch(messages, fetchProfile, null);
    verify(imapConnection).sendCommand("UID FETCH 1 (UID BODYSTRUCTURE)", false);
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) Test(org.junit.Test)

Aggregations

FetchProfile (com.fsck.k9.mail.FetchProfile)49 Test (org.junit.Test)27 LocalMessage (com.fsck.k9.mailstore.LocalMessage)15 MessagingException (com.fsck.k9.mail.MessagingException)12 ArrayList (java.util.ArrayList)10 LocalFolder (com.fsck.k9.mailstore.LocalFolder)9 LocalStore (com.fsck.k9.mailstore.LocalStore)9 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)8 Date (java.util.Date)8 Message (com.fsck.k9.mail.Message)7 SuppressLint (android.annotation.SuppressLint)6 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)6 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)5 Multipart (com.fsck.k9.mail.Multipart)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 WebDavMessage (com.fsck.k9.mail.store.webdav.WebDavMessage)4 LinkedList (java.util.LinkedList)4 HttpResponse (org.apache.http.HttpResponse)4 StatusLine (org.apache.http.StatusLine)4