Search in sources :

Example 6 with MessagingListener

use of com.fsck.k9.controller.MessagingListener in project k-9 by k9mail.

the class MessagingController method setFlag.

/**
     * Set or remove a flag for a set of messages in a specific folder.
     * <p>
     * <p>
     * The {@link Message} objects passed in are updated to reflect the new flag state.
     * </p>
     *
     * @param account
     *         The account the folder containing the messages belongs to.
     * @param folderName
     *         The name of the folder.
     * @param messages
     *         The messages to change the flag for.
     * @param flag
     *         The flag to change.
     * @param newState
     *         {@code true}, if the flag should be set. {@code false} if it should be removed.
     */
public void setFlag(Account account, String folderName, List<? extends Message> messages, Flag flag, boolean newState) {
    // TODO: Put this into the background, but right now some callers depend on the message
    //       objects being modified right after this method returns.
    Folder localFolder = null;
    try {
        Store localStore = account.getLocalStore();
        localFolder = localStore.getFolder(folderName);
        localFolder.open(Folder.OPEN_MODE_RW);
        // Allows for re-allowing sending of messages that could not be sent
        if (flag == Flag.FLAGGED && !newState && account.getOutboxFolderName().equals(folderName)) {
            for (Message message : messages) {
                String uid = message.getUid();
                if (uid != null) {
                    sendCount.remove(uid);
                }
            }
        }
        // Update the messages in the local store
        localFolder.setFlags(messages, Collections.singleton(flag), newState);
        int unreadMessageCount = localFolder.getUnreadMessageCount();
        for (MessagingListener l : getListeners()) {
            l.folderStatusChanged(account, folderName, unreadMessageCount);
        }
        // TODO: Skip the remote part for all local-only folders
        if (account.getErrorFolderName().equals(folderName)) {
            return;
        }
        List<String> uids = getUidsFromMessages(messages);
        queueSetFlag(account, folderName, newState, flag, uids);
        processPendingCommands(account);
    } catch (MessagingException me) {
        addErrorMessage(account, null, me);
        throw new RuntimeException(me);
    } finally {
        closeFolder(localFolder);
    }
}
Also used : 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) LocalStore(com.fsck.k9.mailstore.LocalStore) Store(com.fsck.k9.mail.Store) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) SuppressLint(android.annotation.SuppressLint)

Example 7 with MessagingListener

use of com.fsck.k9.controller.MessagingListener in project k-9 by k9mail.

the class MessagingController method processPendingCommandsSynchronous.

private void processPendingCommandsSynchronous(Account account) throws MessagingException {
    LocalStore localStore = account.getLocalStore();
    List<PendingCommand> commands = localStore.getPendingCommands();
    int progress = 0;
    int todo = commands.size();
    if (todo == 0) {
        return;
    }
    for (MessagingListener l : getListeners()) {
        l.pendingCommandsProcessing(account);
        l.synchronizeMailboxProgress(account, null, progress, todo);
    }
    PendingCommand processingCommand = null;
    try {
        for (PendingCommand command : commands) {
            processingCommand = command;
            Timber.d("Processing pending command '%s'", command);
            for (MessagingListener l : getListeners()) {
                l.pendingCommandStarted(account, command.getCommandName());
            }
            /*
                 * We specifically do not catch any exceptions here. If a command fails it is
                 * most likely due to a server or IO error and it must be retried before any
                 * other command processes. This maintains the order of the commands.
                 */
            try {
                command.execute(this, account);
                localStore.removePendingCommand(command);
                Timber.d("Done processing pending command '%s'", command);
            } catch (MessagingException me) {
                if (me.isPermanentFailure()) {
                    addErrorMessage(account, null, me);
                    Timber.e("Failure of command '%s' was permanent, removing command from queue", command);
                    localStore.removePendingCommand(processingCommand);
                } else {
                    throw me;
                }
            } finally {
                progress++;
                for (MessagingListener l : getListeners()) {
                    l.synchronizeMailboxProgress(account, null, progress, todo);
                    l.pendingCommandCompleted(account, command.getCommandName());
                }
            }
        }
    } catch (MessagingException me) {
        notifyUserIfCertificateProblem(account, me, true);
        addErrorMessage(account, null, me);
        Timber.e(me, "Could not process command '%s'", processingCommand);
        throw me;
    } finally {
        for (MessagingListener l : getListeners()) {
            l.pendingCommandsFinished(account);
        }
    }
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) LocalStore(com.fsck.k9.mailstore.LocalStore) PendingCommand(com.fsck.k9.controller.MessagingControllerCommands.PendingCommand) SuppressLint(android.annotation.SuppressLint)

Example 8 with MessagingListener

use of com.fsck.k9.controller.MessagingListener in project k-9 by k9mail.

the class MessagingController method evaluateMessageForDownload.

private void evaluateMessageForDownload(final Message message, final String folder, final LocalFolder localFolder, final Folder remoteFolder, final Account account, final List<Message> unsyncedMessages, final List<Message> syncFlagMessages, boolean flagSyncOnly) throws MessagingException {
    if (message.isSet(Flag.DELETED)) {
        Timber.v("Message with uid %s is marked as deleted", message.getUid());
        syncFlagMessages.add(message);
        return;
    }
    Message localMessage = localFolder.getMessage(message.getUid());
    if (localMessage == null) {
        if (!flagSyncOnly) {
            if (!message.isSet(Flag.X_DOWNLOADED_FULL) && !message.isSet(Flag.X_DOWNLOADED_PARTIAL)) {
                Timber.v("Message with uid %s has not yet been downloaded", message.getUid());
                unsyncedMessages.add(message);
            } else {
                Timber.v("Message with uid %s is partially or fully downloaded", message.getUid());
                // Store the updated message locally
                localFolder.appendMessages(Collections.singletonList(message));
                localMessage = localFolder.getMessage(message.getUid());
                localMessage.setFlag(Flag.X_DOWNLOADED_FULL, message.isSet(Flag.X_DOWNLOADED_FULL));
                localMessage.setFlag(Flag.X_DOWNLOADED_PARTIAL, message.isSet(Flag.X_DOWNLOADED_PARTIAL));
                for (MessagingListener l : getListeners()) {
                    if (!localMessage.isSet(Flag.SEEN)) {
                        l.synchronizeMailboxNewMessage(account, folder, localMessage);
                    }
                }
            }
        }
    } else if (!localMessage.isSet(Flag.DELETED)) {
        Timber.v("Message with uid %s is present in the local store", message.getUid());
        if (!localMessage.isSet(Flag.X_DOWNLOADED_FULL) && !localMessage.isSet(Flag.X_DOWNLOADED_PARTIAL)) {
            Timber.v("Message with uid %s is not downloaded, even partially; trying again", message.getUid());
            unsyncedMessages.add(message);
        } else {
            String newPushState = remoteFolder.getNewPushState(localFolder.getPushState(), message);
            if (newPushState != null) {
                localFolder.setPushState(newPushState);
            }
            syncFlagMessages.add(message);
        }
    } else {
        Timber.v("Local copy of message with uid %s is marked as deleted", message.getUid());
    }
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message)

Example 9 with MessagingListener

use of com.fsck.k9.controller.MessagingListener in project k-9 by k9mail.

the class MessagingController method clearFolderSynchronous.

@VisibleForTesting
protected void clearFolderSynchronous(Account account, String folderName, MessagingListener listener) {
    LocalFolder localFolder = null;
    try {
        localFolder = account.getLocalStore().getFolder(folderName);
        localFolder.open(Folder.OPEN_MODE_RW);
        localFolder.clearAllMessages();
    } catch (UnavailableStorageException e) {
        Timber.i("Failed to clear folder because storage is not available - trying again later.");
        throw new UnavailableAccountException(e);
    } catch (Exception e) {
        Timber.e(e, "clearFolder failed");
        addErrorMessage(account, null, e);
    } finally {
        closeFolder(localFolder);
    }
    listFoldersSynchronous(account, false, listener);
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) 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)

Example 10 with MessagingListener

use of com.fsck.k9.controller.MessagingListener in project k-9 by k9mail.

the class MessagingController method refreshLocalMessageFlags.

private void refreshLocalMessageFlags(final Account account, final Folder remoteFolder, final LocalFolder localFolder, List<Message> syncFlagMessages, final AtomicInteger progress, final int todo) throws MessagingException {
    final String folder = remoteFolder.getName();
    if (remoteFolder.supportsFetchingFlags()) {
        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<Message> undeletedMessages = new LinkedList<>();
        for (Message message : syncFlagMessages) {
            if (!message.isSet(Flag.DELETED)) {
                undeletedMessages.add(message);
            }
        }
        remoteFolder.fetch(undeletedMessages, fp, null);
        for (Message remoteMessage : syncFlagMessages) {
            LocalMessage localMessage = localFolder.getMessage(remoteMessage.getUid());
            boolean messageChanged = syncFlags(localMessage, remoteMessage);
            if (messageChanged) {
                boolean shouldBeNotifiedOf = false;
                if (localMessage.isSet(Flag.DELETED) || isMessageSuppressed(localMessage)) {
                    for (MessagingListener l : getListeners()) {
                        l.synchronizeMailboxRemovedMessage(account, folder, localMessage);
                    }
                } else {
                    if (shouldNotifyForMessage(account, localFolder, localMessage)) {
                        shouldBeNotifiedOf = true;
                    }
                }
                // we're only interested in messages that need removing
                if (!shouldBeNotifiedOf) {
                    MessageReference messageReference = localMessage.makeMessageReference();
                    notificationController.removeNewMailNotification(account, messageReference);
                }
            }
            progress.incrementAndGet();
            for (MessagingListener l : getListeners()) {
                l.synchronizeMailboxProgress(account, folder, progress.get(), todo);
            }
        }
    }
}
Also used : 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) MessageReference(com.fsck.k9.activity.MessageReference) LinkedList(java.util.LinkedList)

Aggregations

MessagingException (com.fsck.k9.mail.MessagingException)25 LocalFolder (com.fsck.k9.mailstore.LocalFolder)20 LocalStore (com.fsck.k9.mailstore.LocalStore)20 LocalMessage (com.fsck.k9.mailstore.LocalMessage)18 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)15 SuppressLint (android.annotation.SuppressLint)14 Folder (com.fsck.k9.mail.Folder)14 Message (com.fsck.k9.mail.Message)14 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)14 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)13 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)13 IOException (java.io.IOException)13 Store (com.fsck.k9.mail.Store)12 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)12 ArrayList (java.util.ArrayList)9 FetchProfile (com.fsck.k9.mail.FetchProfile)7 VisibleForTesting (android.support.annotation.VisibleForTesting)6 Account (com.fsck.k9.Account)6 Date (java.util.Date)6 SearchAccount (com.fsck.k9.search.SearchAccount)5