Search in sources :

Example 21 with Account

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

the class MessagingController method actOnMessagesGroupedByAccountAndFolder.

private void actOnMessagesGroupedByAccountAndFolder(List<MessageReference> messages, MessageActor actor) {
    Map<String, Map<String, List<MessageReference>>> accountMap = groupMessagesByAccountAndFolder(messages);
    for (Map.Entry<String, Map<String, List<MessageReference>>> entry : accountMap.entrySet()) {
        String accountUuid = entry.getKey();
        Account account = Preferences.getPreferences(context).getAccount(accountUuid);
        Map<String, List<MessageReference>> folderMap = entry.getValue();
        for (Map.Entry<String, List<MessageReference>> folderEntry : folderMap.entrySet()) {
            String folderName = folderEntry.getKey();
            List<MessageReference> messageList = folderEntry.getValue();
            actOnMessageGroup(account, folderName, messageList, actor);
        }
    }
}
Also used : SearchAccount(com.fsck.k9.search.SearchAccount) Account(com.fsck.k9.Account) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) MessageReference(com.fsck.k9.activity.MessageReference) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 22 with Account

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

the class MessagingController method processPendingSetFlag.

/**
     * Processes a pending mark read or unread command.
     */
void processPendingSetFlag(PendingSetFlag command, Account account) throws MessagingException {
    String folder = command.folder;
    if (account.getErrorFolderName().equals(folder) || account.getOutboxFolderName().equals(folder)) {
        return;
    }
    boolean newState = command.newState;
    Flag flag = command.flag;
    Store remoteStore = account.getRemoteStore();
    Folder remoteFolder = remoteStore.getFolder(folder);
    if (!remoteFolder.exists() || !remoteFolder.isFlagSupported(flag)) {
        return;
    }
    try {
        remoteFolder.open(Folder.OPEN_MODE_RW);
        if (remoteFolder.getMode() != Folder.OPEN_MODE_RW) {
            return;
        }
        List<Message> messages = new ArrayList<>();
        for (String uid : command.uids) {
            if (!uid.startsWith(K9.LOCAL_UID_PREFIX)) {
                messages.add(remoteFolder.getMessage(uid));
            }
        }
        if (messages.isEmpty()) {
            return;
        }
        remoteFolder.setFlags(messages, Collections.singleton(flag), newState);
    } finally {
        closeFolder(remoteFolder);
    }
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) ArrayList(java.util.ArrayList) 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) PendingSetFlag(com.fsck.k9.controller.MessagingControllerCommands.PendingSetFlag) Flag(com.fsck.k9.mail.Flag)

Example 23 with Account

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

the class MessagingController method queuePendingCommand.

private void queuePendingCommand(Account account, PendingCommand command) {
    try {
        LocalStore localStore = account.getLocalStore();
        localStore.addPendingCommand(command);
    } catch (Exception e) {
        addErrorMessage(account, null, e);
        throw new RuntimeException("Unable to enqueue pending command", e);
    }
}
Also used : LocalStore(com.fsck.k9.mailstore.LocalStore) 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 24 with Account

use of com.fsck.k9.Account 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 25 with Account

use of com.fsck.k9.Account 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)

Aggregations

Account (com.fsck.k9.Account)122 Test (org.junit.Test)81 MessagingException (com.fsck.k9.mail.MessagingException)53 LocalFolder (com.fsck.k9.mailstore.LocalFolder)44 LocalMessage (com.fsck.k9.mailstore.LocalMessage)43 LocalStore (com.fsck.k9.mailstore.LocalStore)42 ArrayList (java.util.ArrayList)34 MessageReference (com.fsck.k9.activity.MessageReference)32 Message (com.fsck.k9.mail.Message)29 Folder (com.fsck.k9.mail.Folder)27 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)25 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)22 IOException (java.io.IOException)22 Cursor (android.database.Cursor)21 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)20 Store (com.fsck.k9.mail.Store)20 SearchAccount (com.fsck.k9.search.SearchAccount)20 PendingIntent (android.app.PendingIntent)19 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)19 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)18