Search in sources :

Example 31 with PendingCommand

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

the class MessagingController method deleteMessagesSynchronous.

private void deleteMessagesSynchronous(Account account, long folderId, List<LocalMessage> messages) {
    try {
        List<LocalMessage> localOnlyMessages = new ArrayList<>();
        List<LocalMessage> syncedMessages = new ArrayList<>();
        List<String> syncedMessageUids = new ArrayList<>();
        for (LocalMessage message : messages) {
            notificationController.removeNewMailNotification(account, message.makeMessageReference());
            String uid = message.getUid();
            if (uid.startsWith(K9.LOCAL_UID_PREFIX)) {
                localOnlyMessages.add(message);
            } else {
                syncedMessages.add(message);
                syncedMessageUids.add(uid);
            }
        }
        Backend backend = getBackend(account);
        LocalStore localStore = localStoreProvider.getInstance(account);
        LocalFolder localFolder = localStore.getFolder(folderId);
        localFolder.open();
        Map<String, String> uidMap = null;
        Long trashFolderId = account.getTrashFolderId();
        LocalFolder localTrashFolder = null;
        if (!account.hasTrashFolder() || folderId == trashFolderId || (backend.getSupportsTrashFolder() && !backend.isDeleteMoveToTrash())) {
            Timber.d("Not moving deleted messages to local Trash folder. Removing local copies.");
            if (!localOnlyMessages.isEmpty()) {
                localFolder.destroyMessages(localOnlyMessages);
            }
            if (!syncedMessages.isEmpty()) {
                localFolder.setFlags(syncedMessages, Collections.singleton(Flag.DELETED), true);
            }
        } else {
            Timber.d("Deleting messages in normal folder, moving");
            localTrashFolder = localStore.getFolder(trashFolderId);
            MessageStore messageStore = messageStoreManager.getMessageStore(account);
            List<Long> messageIds = new ArrayList<>();
            Map<Long, String> messageIdToUidMapping = new HashMap<>();
            for (LocalMessage message : messages) {
                long messageId = message.getDatabaseId();
                messageIds.add(messageId);
                messageIdToUidMapping.put(messageId, message.getUid());
            }
            Map<Long, Long> moveMessageIdMapping = messageStore.moveMessages(messageIds, trashFolderId);
            Map<Long, String> destinationMapping = messageStore.getMessageServerIds(moveMessageIdMapping.values());
            uidMap = new HashMap<>();
            for (Entry<Long, Long> entry : moveMessageIdMapping.entrySet()) {
                long sourceMessageId = entry.getKey();
                long destinationMessageId = entry.getValue();
                String sourceUid = messageIdToUidMapping.get(sourceMessageId);
                String destinationUid = destinationMapping.get(destinationMessageId);
                uidMap.put(sourceUid, destinationUid);
            }
            if (account.isMarkMessageAsReadOnDelete()) {
                Collection<Long> destinationMessageIds = moveMessageIdMapping.values();
                messageStore.setFlag(destinationMessageIds, Flag.SEEN, true);
            }
        }
        for (MessagingListener l : getListeners()) {
            l.folderStatusChanged(account, folderId);
            if (localTrashFolder != null) {
                l.folderStatusChanged(account, trashFolderId);
            }
        }
        Timber.d("Delete policy for account %s is %s", account, account.getDeletePolicy());
        Long outboxFolderId = account.getOutboxFolderId();
        if (outboxFolderId != null && folderId == outboxFolderId && supportsUpload(account)) {
            for (String destinationUid : uidMap.values()) {
                // If the message was in the Outbox, then it has been copied to local Trash, and has
                // to be copied to remote trash
                PendingCommand command = PendingAppend.create(trashFolderId, destinationUid);
                queuePendingCommand(account, command);
            }
            processPendingCommands(account);
        } else if (localFolder.isLocalOnly()) {
        // Nothing to do on the remote side
        } else if (!syncedMessageUids.isEmpty()) {
            if (account.getDeletePolicy() == DeletePolicy.ON_DELETE) {
                if (!account.hasTrashFolder() || folderId == trashFolderId || !backend.isDeleteMoveToTrash()) {
                    queueDelete(account, folderId, syncedMessageUids);
                } else if (account.isMarkMessageAsReadOnDelete()) {
                    queueMoveOrCopy(account, folderId, trashFolderId, MoveOrCopyFlavor.MOVE_AND_MARK_AS_READ, uidMap);
                } else {
                    queueMoveOrCopy(account, folderId, trashFolderId, MoveOrCopyFlavor.MOVE, uidMap);
                }
                processPendingCommands(account);
            } else if (account.getDeletePolicy() == DeletePolicy.MARK_AS_READ) {
                queueSetFlag(account, localFolder.getDatabaseId(), true, Flag.SEEN, syncedMessageUids);
                processPendingCommands(account);
            } else {
                Timber.d("Delete policy %s prevents delete from server", account.getDeletePolicy());
            }
        }
        unsuppressMessages(account, messages);
    } catch (MessagingException me) {
        throw new RuntimeException("Error deleting message from local store.", me);
    }
}
Also used : MessageStore(com.fsck.k9.mailstore.MessageStore) LocalMessage(com.fsck.k9.mailstore.LocalMessage) HashMap(java.util.HashMap) MessagingException(com.fsck.k9.mail.MessagingException) ArrayList(java.util.ArrayList) LocalStore(com.fsck.k9.mailstore.LocalStore) LocalFolder(com.fsck.k9.mailstore.LocalFolder) Backend(com.fsck.k9.backend.api.Backend) PendingCommand(com.fsck.k9.controller.MessagingControllerCommands.PendingCommand)

Example 32 with PendingCommand

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

the class PendingCommandSerializerTest method testSerializeDeserialize__withArguments.

@Test
public void testSerializeDeserialize__withArguments() {
    PendingCommand pendingCommand = PendingAppend.create(SOURCE_FOLDER_ID, UID);
    String serializedCommand = pendingCommandSerializer.serialize(pendingCommand);
    PendingAppend unserializedCommand = (PendingAppend) pendingCommandSerializer.unserialize(DATABASE_ID, pendingCommand.getCommandName(), serializedCommand);
    assertEquals(DATABASE_ID, unserializedCommand.databaseId);
    assertEquals(SOURCE_FOLDER_ID, unserializedCommand.folderId);
    assertEquals(UID, unserializedCommand.uid);
}
Also used : PendingAppend(com.fsck.k9.controller.MessagingControllerCommands.PendingAppend) PendingCommand(com.fsck.k9.controller.MessagingControllerCommands.PendingCommand) Test(org.junit.Test)

Example 33 with PendingCommand

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

the class PendingCommandSerializerTest method testSerializeDeserialize__withComplexArguments.

@Test
public void testSerializeDeserialize__withComplexArguments() {
    PendingCommand pendingCommand = PendingMoveOrCopy.create(SOURCE_FOLDER_ID, DEST_FOLDER_ID, IS_COPY, UID_MAP);
    String serializedCommand = pendingCommandSerializer.serialize(pendingCommand);
    PendingMoveOrCopy unserializedCommand = (PendingMoveOrCopy) pendingCommandSerializer.unserialize(DATABASE_ID, pendingCommand.getCommandName(), serializedCommand);
    assertEquals(DATABASE_ID, unserializedCommand.databaseId);
    assertEquals(SOURCE_FOLDER_ID, unserializedCommand.srcFolderId);
    assertEquals(DEST_FOLDER_ID, unserializedCommand.destFolderId);
    assertEquals(UID_MAP, unserializedCommand.newUidMap);
}
Also used : PendingMoveOrCopy(com.fsck.k9.controller.MessagingControllerCommands.PendingMoveOrCopy) PendingCommand(com.fsck.k9.controller.MessagingControllerCommands.PendingCommand) Test(org.junit.Test)

Aggregations

PendingCommand (com.fsck.k9.controller.MessagingControllerCommands.PendingCommand)21 Test (org.junit.Test)14 OldPendingCommand (com.fsck.k9.mailstore.migrations.MigrationTo60.OldPendingCommand)10 PendingMoveOrCopy (com.fsck.k9.controller.MessagingControllerCommands.PendingMoveOrCopy)5 MessagingException (com.fsck.k9.mail.MessagingException)5 LocalFolder (com.fsck.k9.mailstore.LocalFolder)5 LocalStore (com.fsck.k9.mailstore.LocalStore)5 PendingSetFlag (com.fsck.k9.controller.MessagingControllerCommands.PendingSetFlag)4 LocalMessage (com.fsck.k9.mailstore.LocalMessage)3 ArrayList (java.util.ArrayList)3 PendingAppend (com.fsck.k9.controller.MessagingControllerCommands.PendingAppend)2 PendingEmptyTrash (com.fsck.k9.controller.MessagingControllerCommands.PendingEmptyTrash)2 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)2 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)2 Flag (com.fsck.k9.mail.Flag)2 Message (com.fsck.k9.mail.Message)2 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)2 MessageStore (com.fsck.k9.mailstore.MessageStore)2 ContentValues (android.content.ContentValues)1 Backend (com.fsck.k9.backend.api.Backend)1