Search in sources :

Example 6 with UnavailableStorageException

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

the class MessagingController method moveOrCopyMessageSynchronous.

private void moveOrCopyMessageSynchronous(final Account account, final String srcFolder, final List<? extends Message> inMessages, final String destFolder, final boolean isCopy) {
    try {
        LocalStore localStore = account.getLocalStore();
        Store remoteStore = account.getRemoteStore();
        if (!isCopy && (!remoteStore.isMoveCapable() || !localStore.isMoveCapable())) {
            return;
        }
        if (isCopy && (!remoteStore.isCopyCapable() || !localStore.isCopyCapable())) {
            return;
        }
        LocalFolder localSrcFolder = localStore.getFolder(srcFolder);
        Folder localDestFolder = localStore.getFolder(destFolder);
        boolean unreadCountAffected = false;
        List<String> uids = new LinkedList<>();
        for (Message message : inMessages) {
            String uid = message.getUid();
            if (!uid.startsWith(K9.LOCAL_UID_PREFIX)) {
                uids.add(uid);
            }
            if (!unreadCountAffected && !message.isSet(Flag.SEEN)) {
                unreadCountAffected = true;
            }
        }
        List<LocalMessage> messages = localSrcFolder.getMessagesByUids(uids);
        if (messages.size() > 0) {
            Map<String, Message> origUidMap = new HashMap<>();
            for (Message message : messages) {
                origUidMap.put(message.getUid(), message);
            }
            Timber.i("moveOrCopyMessageSynchronous: source folder = %s, %d messages, destination folder = %s, " + "isCopy = %s", srcFolder, messages.size(), destFolder, isCopy);
            Map<String, String> uidMap;
            if (isCopy) {
                FetchProfile fp = new FetchProfile();
                fp.add(Item.ENVELOPE);
                fp.add(Item.BODY);
                localSrcFolder.fetch(messages, fp, null);
                uidMap = localSrcFolder.copyMessages(messages, localDestFolder);
                if (unreadCountAffected) {
                    // If this copy operation changes the unread count in the destination
                    // folder, notify the listeners.
                    int unreadMessageCount = localDestFolder.getUnreadMessageCount();
                    for (MessagingListener l : getListeners()) {
                        l.folderStatusChanged(account, destFolder, unreadMessageCount);
                    }
                }
            } else {
                uidMap = localSrcFolder.moveMessages(messages, localDestFolder);
                for (Entry<String, Message> entry : origUidMap.entrySet()) {
                    String origUid = entry.getKey();
                    Message message = entry.getValue();
                    for (MessagingListener l : getListeners()) {
                        l.messageUidChanged(account, srcFolder, origUid, message.getUid());
                    }
                }
                unsuppressMessages(account, messages);
                if (unreadCountAffected) {
                    // If this move operation changes the unread count, notify the listeners
                    // that the unread count changed in both the source and destination folder.
                    int unreadMessageCountSrc = localSrcFolder.getUnreadMessageCount();
                    int unreadMessageCountDest = localDestFolder.getUnreadMessageCount();
                    for (MessagingListener l : getListeners()) {
                        l.folderStatusChanged(account, srcFolder, unreadMessageCountSrc);
                        l.folderStatusChanged(account, destFolder, unreadMessageCountDest);
                    }
                }
            }
            List<String> origUidKeys = new ArrayList<>(origUidMap.keySet());
            queueMoveOrCopy(account, srcFolder, destFolder, isCopy, origUidKeys, uidMap);
        }
        processPendingCommands(account);
    } catch (UnavailableStorageException e) {
        Timber.i("Failed to move/copy message because storage is not available - trying again later.");
        throw new UnavailableAccountException(e);
    } catch (MessagingException me) {
        addErrorMessage(account, null, me);
        throw new RuntimeException("Error moving message", me);
    }
}
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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MessagingException(com.fsck.k9.mail.MessagingException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) 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) LinkedList(java.util.LinkedList) SuppressLint(android.annotation.SuppressLint) LocalFolder(com.fsck.k9.mailstore.LocalFolder)

Aggregations

MessagingException (com.fsck.k9.mail.MessagingException)5 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)5 Message (com.fsck.k9.mail.Message)3 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)3 LocalFolder (com.fsck.k9.mailstore.LocalFolder)3 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 Folder (com.fsck.k9.mail.Folder)2 Store (com.fsck.k9.mail.Store)2 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)2 LocalMessage (com.fsck.k9.mailstore.LocalMessage)2 LocalStore (com.fsck.k9.mailstore.LocalStore)2 WrappedException (com.fsck.k9.mailstore.LockableDatabase.WrappedException)2 HashMap (java.util.HashMap)2 SuppressLint (android.annotation.SuppressLint)1 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 VisibleForTesting (android.support.annotation.VisibleForTesting)1 Account (com.fsck.k9.Account)1 EmailProviderCacheCursor (com.fsck.k9.cache.EmailProviderCacheCursor)1 PendingCommand (com.fsck.k9.controller.MessagingControllerCommands.PendingCommand)1