Search in sources :

Example 41 with LocalFolder

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

the class MessagingController method moveOrDeleteSentMessage.

private void moveOrDeleteSentMessage(Account account, LocalStore localStore, LocalFolder localFolder, LocalMessage message) throws MessagingException {
    if (!account.hasSentFolder()) {
        Timber.i("Account does not have a sent mail folder; deleting sent message");
        message.setFlag(Flag.DELETED, true);
    } else {
        LocalFolder localSentFolder = localStore.getFolder(account.getSentFolderName());
        Timber.i("Moving sent message to folder '%s' (%d)", account.getSentFolderName(), localSentFolder.getId());
        localFolder.moveMessages(Collections.singletonList(message), localSentFolder);
        Timber.i("Moved sent message to folder '%s' (%d)", account.getSentFolderName(), localSentFolder.getId());
        PendingCommand command = PendingAppend.create(localSentFolder.getName(), message.getUid());
        queuePendingCommand(account, command);
        processPendingCommands(account);
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) PendingCommand(com.fsck.k9.controller.MessagingControllerCommands.PendingCommand)

Example 42 with LocalFolder

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

the class MessagingController method searchRemoteMessagesSynchronous.

@VisibleForTesting
void searchRemoteMessagesSynchronous(final String acctUuid, final String folderName, final String query, final Set<Flag> requiredFlags, final Set<Flag> forbiddenFlags, final MessagingListener listener) {
    final Account acct = Preferences.getPreferences(context).getAccount(acctUuid);
    if (listener != null) {
        listener.remoteSearchStarted(folderName);
    }
    List<Message> extraResults = new ArrayList<>();
    try {
        Store remoteStore = acct.getRemoteStore();
        LocalStore localStore = acct.getLocalStore();
        if (remoteStore == null || localStore == null) {
            throw new MessagingException("Could not get store");
        }
        Folder remoteFolder = remoteStore.getFolder(folderName);
        LocalFolder localFolder = localStore.getFolder(folderName);
        if (remoteFolder == null || localFolder == null) {
            throw new MessagingException("Folder not found");
        }
        List<Message> messages = remoteFolder.search(query, requiredFlags, forbiddenFlags);
        Timber.i("Remote search got %d results", messages.size());
        // There's no need to fetch messages already completely downloaded
        List<Message> remoteMessages = localFolder.extractNewMessages(messages);
        messages.clear();
        if (listener != null) {
            listener.remoteSearchServerQueryComplete(folderName, remoteMessages.size(), acct.getRemoteSearchNumResults());
        }
        Collections.sort(remoteMessages, new UidReverseComparator());
        int resultLimit = acct.getRemoteSearchNumResults();
        if (resultLimit > 0 && remoteMessages.size() > resultLimit) {
            extraResults = remoteMessages.subList(resultLimit, remoteMessages.size());
            remoteMessages = remoteMessages.subList(0, resultLimit);
        }
        loadSearchResultsSynchronous(remoteMessages, localFolder, remoteFolder, listener);
    } catch (Exception e) {
        if (Thread.currentThread().isInterrupted()) {
            Timber.i(e, "Caught exception on aborted remote search; safe to ignore.");
        } else {
            Timber.e(e, "Could not complete remote search");
            if (listener != null) {
                listener.remoteSearchFailed(null, e.getMessage());
            }
            addErrorMessage(acct, null, e);
        }
    } finally {
        if (listener != null) {
            listener.remoteSearchFinished(folderName, 0, acct.getRemoteSearchNumResults(), extraResults);
        }
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) SearchAccount(com.fsck.k9.search.SearchAccount) Account(com.fsck.k9.Account) 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) LocalStore(com.fsck.k9.mailstore.LocalStore) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) SuppressLint(android.annotation.SuppressLint) 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 43 with LocalFolder

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

the class MessagingController method loadMessageRemoteSynchronous.

private boolean loadMessageRemoteSynchronous(final Account account, final String folder, final String uid, final MessagingListener listener, final boolean loadPartialFromSearch) {
    Folder remoteFolder = null;
    LocalFolder localFolder = null;
    try {
        LocalStore localStore = account.getLocalStore();
        localFolder = localStore.getFolder(folder);
        localFolder.open(Folder.OPEN_MODE_RW);
        LocalMessage message = localFolder.getMessage(uid);
        if (uid.startsWith(K9.LOCAL_UID_PREFIX)) {
            Timber.w("Message has local UID so cannot download fully.");
            // ASH move toast
            android.widget.Toast.makeText(context, "Message has local UID so cannot download fully", android.widget.Toast.LENGTH_LONG).show();
            // TODO: Using X_DOWNLOADED_FULL is wrong because it's only a partial message. But
            // one we can't download completely. Maybe add a new flag; X_PARTIAL_MESSAGE ?
            message.setFlag(Flag.X_DOWNLOADED_FULL, true);
            message.setFlag(Flag.X_DOWNLOADED_PARTIAL, false);
        }
        /* commented out because this was pulled from another unmerged branch:
            } else if (localFolder.isLocalOnly() && !force) {
                Log.w(K9.LOG_TAG, "Message in local-only folder so cannot download fully.");
                // ASH move toast
                android.widget.Toast.makeText(mApplication,
                        "Message in local-only folder so cannot download fully",
                        android.widget.Toast.LENGTH_LONG).show();
                message.setFlag(Flag.X_DOWNLOADED_FULL, true);
                message.setFlag(Flag.X_DOWNLOADED_PARTIAL, false);
            }*/
        /*if (!message.isSet(Flag.X_DOWNLOADED_FULL)) */
        {
            /*
                 * At this point the message is not available, so we need to download it
                 * fully if possible.
                 */
            Store remoteStore = account.getRemoteStore();
            remoteFolder = remoteStore.getFolder(folder);
            remoteFolder.open(Folder.OPEN_MODE_RW);
            // Get the remote message and fully download it
            Message remoteMessage = remoteFolder.getMessage(uid);
            if (loadPartialFromSearch) {
                downloadMessages(account, remoteFolder, localFolder, Collections.singletonList(remoteMessage), false, false);
            } else {
                FetchProfile fp = new FetchProfile();
                fp.add(FetchProfile.Item.BODY);
                remoteFolder.fetch(Collections.singletonList(remoteMessage), fp, null);
                localFolder.appendMessages(Collections.singletonList(remoteMessage));
            }
            message = localFolder.getMessage(uid);
            if (!loadPartialFromSearch) {
                message.setFlag(Flag.X_DOWNLOADED_FULL, true);
            }
        }
        // Mark that this message is now fully synched
        if (account.isMarkMessageAsReadOnView()) {
            message.setFlag(Flag.SEEN, true);
        }
        // now that we have the full message, refresh the headers
        for (MessagingListener l : getListeners(listener)) {
            l.loadMessageRemoteFinished(account, folder, uid);
        }
        return true;
    } catch (Exception e) {
        for (MessagingListener l : getListeners(listener)) {
            l.loadMessageRemoteFailed(account, folder, uid, e);
        }
        notifyUserIfCertificateProblem(account, e, true);
        addErrorMessage(account, null, e);
        return false;
    } finally {
        closeFolder(remoteFolder);
        closeFolder(localFolder);
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) 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) 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) 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 44 with LocalFolder

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

the class LocalFolder method getMessagesByReference.

public List<LocalMessage> getMessagesByReference(@NonNull List<MessageReference> messageReferences) throws MessagingException {
    open(OPEN_MODE_RW);
    String accountUuid = getAccountUuid();
    String folderName = getName();
    List<LocalMessage> messages = new ArrayList<>();
    for (MessageReference messageReference : messageReferences) {
        if (!accountUuid.equals(messageReference.getAccountUuid())) {
            throw new IllegalArgumentException("all message references must belong to this Account!");
        }
        if (!folderName.equals(messageReference.getFolderName())) {
            throw new IllegalArgumentException("all message references must belong to this LocalFolder!");
        }
        LocalMessage message = getMessage(messageReference.getUid());
        if (message != null) {
            messages.add(message);
        }
    }
    return messages;
}
Also used : ArrayList(java.util.ArrayList) MessageReference(com.fsck.k9.activity.MessageReference)

Example 45 with LocalFolder

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

the class LocalStore method loadLocalMessageByMessageId.

@Nullable
private LocalMessage loadLocalMessageByMessageId(long messageId) throws MessagingException {
    Map<String, List<String>> foldersAndUids = getFoldersAndUids(Collections.singletonList(messageId), false);
    if (foldersAndUids.isEmpty()) {
        return null;
    }
    Map.Entry<String, List<String>> entry = foldersAndUids.entrySet().iterator().next();
    String folderName = entry.getKey();
    String uid = entry.getValue().get(0);
    LocalFolder folder = getFolder(folderName);
    LocalMessage localMessage = folder.getMessage(uid);
    FetchProfile fp = new FetchProfile();
    fp.add(Item.BODY);
    folder.fetch(Collections.singletonList(localMessage), fp, null);
    return localMessage;
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Nullable(android.support.annotation.Nullable)

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