Search in sources :

Example 1 with K9

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

the class MessagingController method addErrorMessage.

private void addErrorMessage(Account account, String subject, String body) {
    if (!K9.isDebug()) {
        return;
    }
    if (!loopCatch.compareAndSet(false, true)) {
        return;
    }
    try {
        if (body == null || body.length() < 1) {
            return;
        }
        Store localStore = account.getLocalStore();
        LocalFolder localFolder = (LocalFolder) localStore.getFolder(account.getErrorFolderName());
        MimeMessage message = new MimeMessage();
        MimeMessageHelper.setBody(message, new TextBody(body));
        message.setFlag(Flag.X_DOWNLOADED_FULL, true);
        message.setSubject(subject);
        long nowTime = System.currentTimeMillis();
        Date nowDate = new Date(nowTime);
        message.setInternalDate(nowDate);
        message.addSentDate(nowDate, K9.hideTimeZone());
        message.setFrom(new Address(account.getEmail(), "K9mail internal"));
        localFolder.appendMessages(Collections.singletonList(message));
        localFolder.clearMessagesOlderThan(nowTime - (15 * 60 * 1000));
    } catch (Throwable it) {
        Timber.e(it, "Could not save error message to %s", account.getErrorFolderName());
    } finally {
        loopCatch.set(false);
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) TextBody(com.fsck.k9.mail.internet.TextBody) Address(com.fsck.k9.mail.Address) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) LocalStore(com.fsck.k9.mailstore.LocalStore) Store(com.fsck.k9.mail.Store) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) Date(java.util.Date)

Example 2 with K9

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

the class MessagingController method sendPendingMessagesSynchronous.

/**
     * Attempt to send any messages that are sitting in the Outbox.
     */
@VisibleForTesting
protected void sendPendingMessagesSynchronous(final Account account) {
    LocalFolder localFolder = null;
    Exception lastFailure = null;
    boolean wasPermanentFailure = false;
    try {
        LocalStore localStore = account.getLocalStore();
        localFolder = localStore.getFolder(account.getOutboxFolderName());
        if (!localFolder.exists()) {
            Timber.v("Outbox does not exist");
            return;
        }
        for (MessagingListener l : getListeners()) {
            l.sendPendingMessagesStarted(account);
        }
        localFolder.open(Folder.OPEN_MODE_RW);
        List<LocalMessage> localMessages = localFolder.getMessages(null);
        int progress = 0;
        int todo = localMessages.size();
        for (MessagingListener l : getListeners()) {
            l.synchronizeMailboxProgress(account, account.getSentFolderName(), progress, todo);
        }
        /*
             * The profile we will use to pull all of the content
             * for a given local message into memory for sending.
             */
        FetchProfile fp = new FetchProfile();
        fp.add(FetchProfile.Item.ENVELOPE);
        fp.add(FetchProfile.Item.BODY);
        Timber.i("Scanning folder '%s' (%d) for messages to send", account.getOutboxFolderName(), localFolder.getId());
        Transport transport = transportProvider.getTransport(K9.app, account);
        for (LocalMessage message : localMessages) {
            if (message.isSet(Flag.DELETED)) {
                message.destroy();
                continue;
            }
            try {
                AtomicInteger count = new AtomicInteger(0);
                AtomicInteger oldCount = sendCount.putIfAbsent(message.getUid(), count);
                if (oldCount != null) {
                    count = oldCount;
                }
                Timber.i("Send count for message %s is %d", message.getUid(), count.get());
                if (count.incrementAndGet() > K9.MAX_SEND_ATTEMPTS) {
                    Timber.e("Send count for message %s can't be delivered after %d attempts. " + "Giving up until the user restarts the device", message.getUid(), MAX_SEND_ATTEMPTS);
                    notificationController.showSendFailedNotification(account, new MessagingException(message.getSubject()));
                    continue;
                }
                localFolder.fetch(Collections.singletonList(message), fp, null);
                try {
                    if (message.getHeader(K9.IDENTITY_HEADER).length > 0) {
                        Timber.v("The user has set the Outbox and Drafts folder to the same thing. " + "This message appears to be a draft, so K-9 will not send it");
                        continue;
                    }
                    message.setFlag(Flag.X_SEND_IN_PROGRESS, true);
                    Timber.i("Sending message with UID %s", message.getUid());
                    transport.sendMessage(message);
                    message.setFlag(Flag.X_SEND_IN_PROGRESS, false);
                    message.setFlag(Flag.SEEN, true);
                    progress++;
                    for (MessagingListener l : getListeners()) {
                        l.synchronizeMailboxProgress(account, account.getSentFolderName(), progress, todo);
                    }
                    moveOrDeleteSentMessage(account, localStore, localFolder, message);
                } catch (AuthenticationFailedException e) {
                    lastFailure = e;
                    wasPermanentFailure = false;
                    handleAuthenticationFailure(account, false);
                    handleSendFailure(account, localStore, localFolder, message, e, wasPermanentFailure);
                } catch (CertificateValidationException e) {
                    lastFailure = e;
                    wasPermanentFailure = false;
                    notifyUserIfCertificateProblem(account, e, false);
                    handleSendFailure(account, localStore, localFolder, message, e, wasPermanentFailure);
                } catch (MessagingException e) {
                    lastFailure = e;
                    wasPermanentFailure = e.isPermanentFailure();
                    handleSendFailure(account, localStore, localFolder, message, e, wasPermanentFailure);
                } catch (Exception e) {
                    lastFailure = e;
                    wasPermanentFailure = true;
                    handleSendFailure(account, localStore, localFolder, message, e, wasPermanentFailure);
                }
            } catch (Exception e) {
                lastFailure = e;
                wasPermanentFailure = false;
                Timber.e(e, "Failed to fetch message for sending");
                addErrorMessage(account, "Failed to fetch message for sending", e);
                notifySynchronizeMailboxFailed(account, localFolder, e);
            }
        }
        for (MessagingListener l : getListeners()) {
            l.sendPendingMessagesCompleted(account);
        }
        if (lastFailure != null) {
            if (wasPermanentFailure) {
                notificationController.showSendFailedNotification(account, lastFailure);
            } else {
                notificationController.showSendFailedNotification(account, lastFailure);
            }
        }
    } catch (UnavailableStorageException e) {
        Timber.i("Failed to send pending messages because storage is not available - trying again later.");
        throw new UnavailableAccountException(e);
    } catch (Exception e) {
        Timber.v(e, "Failed to send pending messages");
        for (MessagingListener l : getListeners()) {
            l.sendPendingMessagesFailed(account);
        }
        addErrorMessage(account, null, e);
    } finally {
        if (lastFailure == null) {
            notificationController.clearSendFailedNotification(account);
        }
        closeFolder(localFolder);
    }
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) FetchProfile(com.fsck.k9.mail.FetchProfile) MessagingException(com.fsck.k9.mail.MessagingException) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) 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) SuppressLint(android.annotation.SuppressLint) LocalFolder(com.fsck.k9.mailstore.LocalFolder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) Transport(com.fsck.k9.mail.Transport) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 3 with K9

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

the class MessageListFragment method onArchive.

private void onArchive(final List<MessageReference> messages) {
    Map<Account, List<MessageReference>> messagesByAccount = groupMessagesByAccount(messages);
    for (Entry<Account, List<MessageReference>> entry : messagesByAccount.entrySet()) {
        Account account = entry.getKey();
        String archiveFolder = account.getArchiveFolderName();
        if (!K9.FOLDER_NONE.equals(archiveFolder)) {
            move(entry.getValue(), archiveFolder);
        }
    }
}
Also used : Account(com.fsck.k9.Account) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with K9

use of com.fsck.k9.K9 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 5 with K9

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

the class ContactPicture method getContactPictureLoader.

public static ContactPictureLoader getContactPictureLoader(Context context) {
    final int defaultBgColor;
    if (!K9.isColorizeMissingContactPictures()) {
        TypedValue outValue = new TypedValue();
        context.getTheme().resolveAttribute(R.attr.contactPictureFallbackDefaultBackgroundColor, outValue, true);
        defaultBgColor = outValue.data;
    } else {
        defaultBgColor = 0;
    }
    return new ContactPictureLoader(context, defaultBgColor);
}
Also used : ContactPictureLoader(com.fsck.k9.activity.misc.ContactPictureLoader) TypedValue(android.util.TypedValue)

Aggregations

Account (com.fsck.k9.Account)20 MessagingException (com.fsck.k9.mail.MessagingException)13 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)10 LocalStore (com.fsck.k9.mailstore.LocalStore)10 Message (com.fsck.k9.mail.Message)9 Store (com.fsck.k9.mail.Store)9 LocalFolder (com.fsck.k9.mailstore.LocalFolder)9 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)9 Intent (android.content.Intent)8 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)8 LocalMessage (com.fsck.k9.mailstore.LocalMessage)8 SuppressLint (android.annotation.SuppressLint)7 BaseAccount (com.fsck.k9.BaseAccount)7 Preferences (com.fsck.k9.Preferences)7 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)7 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)7 Folder (com.fsck.k9.mail.Folder)7 SearchAccount (com.fsck.k9.search.SearchAccount)7