Search in sources :

Example 16 with LocalFolder

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

the class EmailProviderCache method unhideMessages.

public void unhideMessages(List<? extends Message> messages) {
    synchronized (mHiddenMessageCache) {
        for (Message message : messages) {
            LocalMessage localMessage = (LocalMessage) message;
            long messageId = localMessage.getId();
            long folderId = ((LocalFolder) localMessage.getFolder()).getId();
            Long hiddenInFolder = mHiddenMessageCache.get(messageId);
            if (hiddenInFolder != null && hiddenInFolder.longValue() == folderId) {
                mHiddenMessageCache.remove(messageId);
            }
        }
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) LocalMessage(com.fsck.k9.mailstore.LocalMessage) LocalMessage(com.fsck.k9.mailstore.LocalMessage) Message(com.fsck.k9.mail.Message)

Example 17 with LocalFolder

use of com.fsck.k9.mailstore.LocalFolder 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 18 with LocalFolder

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

Example 19 with LocalFolder

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

the class MessagingController method processPendingMoveOrCopy.

void processPendingMoveOrCopy(PendingMoveOrCopy command, Account account) throws MessagingException {
    Folder remoteSrcFolder = null;
    Folder remoteDestFolder = null;
    LocalFolder localDestFolder;
    try {
        String srcFolder = command.srcFolder;
        if (account.getErrorFolderName().equals(srcFolder)) {
            return;
        }
        String destFolder = command.destFolder;
        boolean isCopy = command.isCopy;
        Store remoteStore = account.getRemoteStore();
        remoteSrcFolder = remoteStore.getFolder(srcFolder);
        Store localStore = account.getLocalStore();
        localDestFolder = (LocalFolder) localStore.getFolder(destFolder);
        List<Message> messages = new ArrayList<>();
        Collection<String> uids = command.newUidMap != null ? command.newUidMap.keySet() : command.uids;
        for (String uid : uids) {
            if (!uid.startsWith(K9.LOCAL_UID_PREFIX)) {
                messages.add(remoteSrcFolder.getMessage(uid));
            }
        }
        if (!remoteSrcFolder.exists()) {
            throw new MessagingException("processingPendingMoveOrCopy: remoteFolder " + srcFolder + " does not exist", true);
        }
        remoteSrcFolder.open(Folder.OPEN_MODE_RW);
        if (remoteSrcFolder.getMode() != Folder.OPEN_MODE_RW) {
            throw new MessagingException("processingPendingMoveOrCopy: could not open remoteSrcFolder " + srcFolder + " read/write", true);
        }
        Timber.d("processingPendingMoveOrCopy: source folder = %s, %d messages, destination folder = %s, " + "isCopy = %s", srcFolder, messages.size(), destFolder, isCopy);
        Map<String, String> remoteUidMap = null;
        if (!isCopy && destFolder.equals(account.getTrashFolderName())) {
            Timber.d("processingPendingMoveOrCopy doing special case for deleting message");
            String destFolderName = destFolder;
            if (K9.FOLDER_NONE.equals(destFolderName)) {
                destFolderName = null;
            }
            remoteSrcFolder.delete(messages, destFolderName);
        } else {
            remoteDestFolder = remoteStore.getFolder(destFolder);
            if (isCopy) {
                remoteUidMap = remoteSrcFolder.copyMessages(messages, remoteDestFolder);
            } else {
                remoteUidMap = remoteSrcFolder.moveMessages(messages, remoteDestFolder);
            }
        }
        if (!isCopy && Expunge.EXPUNGE_IMMEDIATELY == account.getExpungePolicy()) {
            Timber.i("processingPendingMoveOrCopy expunging folder %s:%s", account.getDescription(), srcFolder);
            remoteSrcFolder.expunge();
        }
        /*
             * This next part is used to bring the local UIDs of the local destination folder
             * upto speed with the remote UIDs of remote destination folder.
             */
        if (command.newUidMap != null && remoteUidMap != null && !remoteUidMap.isEmpty()) {
            for (Map.Entry<String, String> entry : remoteUidMap.entrySet()) {
                String remoteSrcUid = entry.getKey();
                String newUid = entry.getValue();
                String localDestUid = command.newUidMap.get(remoteSrcUid);
                if (localDestUid == null) {
                    continue;
                }
                Message localDestMessage = localDestFolder.getMessage(localDestUid);
                if (localDestMessage != null) {
                    localDestMessage.setUid(newUid);
                    localDestFolder.changeUid((LocalMessage) localDestMessage);
                    for (MessagingListener l : getListeners()) {
                        l.messageUidChanged(account, destFolder, localDestUid, newUid);
                    }
                }
            }
        }
    } finally {
        closeFolder(remoteSrcFolder);
        closeFolder(remoteDestFolder);
    }
}
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) 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) LocalFolder(com.fsck.k9.mailstore.LocalFolder) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 20 with LocalFolder

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

the class MessagingController method messagesArrived.

public void messagesArrived(final Account account, final Folder remoteFolder, final List<Message> messages, final boolean flagSyncOnly) {
    Timber.i("Got new pushed email messages for account %s, folder %s", account.getDescription(), remoteFolder.getName());
    final CountDownLatch latch = new CountDownLatch(1);
    putBackground("Push messageArrived of account " + account.getDescription() + ", folder " + remoteFolder.getName(), null, new Runnable() {

        @Override
        public void run() {
            LocalFolder localFolder = null;
            try {
                LocalStore localStore = account.getLocalStore();
                localFolder = localStore.getFolder(remoteFolder.getName());
                localFolder.open(Folder.OPEN_MODE_RW);
                account.setRingNotified(false);
                int newCount = downloadMessages(account, remoteFolder, localFolder, messages, flagSyncOnly, true);
                int unreadMessageCount = localFolder.getUnreadMessageCount();
                localFolder.setLastPush(System.currentTimeMillis());
                localFolder.setStatus(null);
                Timber.i("messagesArrived newCount = %d, unread count = %d", newCount, unreadMessageCount);
                if (unreadMessageCount == 0) {
                    notificationController.clearNewMailNotifications(account);
                }
                for (MessagingListener l : getListeners()) {
                    l.folderStatusChanged(account, remoteFolder.getName(), unreadMessageCount);
                }
            } catch (Exception e) {
                String rootMessage = getRootCauseMessage(e);
                String errorMessage = "Push failed: " + rootMessage;
                try {
                    localFolder.setStatus(errorMessage);
                } catch (Exception se) {
                    Timber.e(se, "Unable to set failed status on localFolder");
                }
                for (MessagingListener l : getListeners()) {
                    l.synchronizeMailboxFailed(account, remoteFolder.getName(), errorMessage);
                }
                addErrorMessage(account, null, e);
            } finally {
                closeFolder(localFolder);
                latch.countDown();
            }
        }
    });
    try {
        latch.await();
    } catch (Exception e) {
        Timber.e(e, "Interrupted while awaiting latch release");
    }
    Timber.i("MessagingController.messagesArrivedLatch released");
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) LocalStore(com.fsck.k9.mailstore.LocalStore) CountDownLatch(java.util.concurrent.CountDownLatch) 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)

Aggregations

LocalFolder (com.fsck.k9.mailstore.LocalFolder)41 MessagingException (com.fsck.k9.mail.MessagingException)31 LocalMessage (com.fsck.k9.mailstore.LocalMessage)30 LocalStore (com.fsck.k9.mailstore.LocalStore)28 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)22 Folder (com.fsck.k9.mail.Folder)20 Message (com.fsck.k9.mail.Message)20 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)13 FetchProfile (com.fsck.k9.mail.FetchProfile)12 Store (com.fsck.k9.mail.Store)12 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)12 IOException (java.io.IOException)11 SuppressLint (android.annotation.SuppressLint)10 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)10 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)10 ArrayList (java.util.ArrayList)10 Date (java.util.Date)10 Test (org.junit.Test)9 VisibleForTesting (android.support.annotation.VisibleForTesting)5 HashMap (java.util.HashMap)5