Search in sources :

Example 31 with MessagingListener

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

the class MessagingController method fetchUnsyncedMessages.

private <T extends Message> void fetchUnsyncedMessages(final Account account, final Folder<T> remoteFolder, List<T> unsyncedMessages, final List<Message> smallMessages, final List<Message> largeMessages, final AtomicInteger progress, final int todo, FetchProfile fp) throws MessagingException {
    final String folder = remoteFolder.getName();
    final Date earliestDate = account.getEarliestPollDate();
    remoteFolder.fetch(unsyncedMessages, fp, new MessageRetrievalListener<T>() {

        @Override
        public void messageFinished(T message, int number, int ofTotal) {
            try {
                if (message.isSet(Flag.DELETED) || message.olderThan(earliestDate)) {
                    if (K9.isDebug()) {
                        if (message.isSet(Flag.DELETED)) {
                            Timber.v("Newly downloaded message %s:%s:%s was marked deleted on server, " + "skipping", account, folder, message.getUid());
                        } else {
                            Timber.d("Newly downloaded message %s is older than %s, skipping", message.getUid(), earliestDate);
                        }
                    }
                    progress.incrementAndGet();
                    for (MessagingListener l : getListeners()) {
                        //TODO: This might be the source of poll count errors in the UI. Is todo always the same as ofTotal
                        l.synchronizeMailboxProgress(account, folder, progress.get(), todo);
                    }
                    return;
                }
                if (account.getMaximumAutoDownloadMessageSize() > 0 && message.getSize() > account.getMaximumAutoDownloadMessageSize()) {
                    largeMessages.add(message);
                } else {
                    smallMessages.add(message);
                }
            } catch (Exception e) {
                Timber.e(e, "Error while storing downloaded message.");
                addErrorMessage(account, null, e);
            }
        }

        @Override
        public void messageStarted(String uid, int number, int ofTotal) {
        }

        @Override
        public void messagesFinished(int total) {
        // FIXME this method is almost never invoked by various Stores! Don't rely on it unless fixed!!
        }
    });
}
Also used : Date(java.util.Date) 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)

Example 32 with MessagingListener

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

the class MessagingController method getSearchAccountStatsSynchronous.

public AccountStats getSearchAccountStatsSynchronous(final SearchAccount searchAccount, final MessagingListener listener) {
    Preferences preferences = Preferences.getPreferences(context);
    LocalSearch search = searchAccount.getRelatedSearch();
    // Collect accounts that belong to the search
    String[] accountUuids = search.getAccountUuids();
    List<Account> accounts;
    if (search.searchAllAccounts()) {
        accounts = preferences.getAccounts();
    } else {
        accounts = new ArrayList<>(accountUuids.length);
        for (int i = 0, len = accountUuids.length; i < len; i++) {
            String accountUuid = accountUuids[i];
            accounts.set(i, preferences.getAccount(accountUuid));
        }
    }
    ContentResolver cr = context.getContentResolver();
    int unreadMessageCount = 0;
    int flaggedMessageCount = 0;
    String[] projection = { StatsColumns.UNREAD_COUNT, StatsColumns.FLAGGED_COUNT };
    for (Account account : accounts) {
        StringBuilder query = new StringBuilder();
        List<String> queryArgs = new ArrayList<>();
        ConditionsTreeNode conditions = search.getConditions();
        SqlQueryBuilder.buildWhereClause(account, conditions, query, queryArgs);
        String selection = query.toString();
        String[] selectionArgs = queryArgs.toArray(new String[queryArgs.size()]);
        Uri uri = Uri.withAppendedPath(EmailProvider.CONTENT_URI, "account/" + account.getUuid() + "/stats");
        // Query content provider to get the account stats
        Cursor cursor = cr.query(uri, projection, selection, selectionArgs, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                unreadMessageCount += cursor.getInt(0);
                flaggedMessageCount += cursor.getInt(1);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    // Create AccountStats instance...
    AccountStats stats = new AccountStats();
    stats.unreadMessageCount = unreadMessageCount;
    stats.flaggedMessageCount = flaggedMessageCount;
    // ...and notify the listener
    if (listener != null) {
        listener.accountStatusChanged(searchAccount, stats);
    }
    return stats;
}
Also used : ConditionsTreeNode(com.fsck.k9.search.ConditionsTreeNode) SearchAccount(com.fsck.k9.search.SearchAccount) Account(com.fsck.k9.Account) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Uri(android.net.Uri) SuppressLint(android.annotation.SuppressLint) ContentResolver(android.content.ContentResolver) LocalSearch(com.fsck.k9.search.LocalSearch) Preferences(com.fsck.k9.Preferences) AccountStats(com.fsck.k9.AccountStats)

Example 33 with MessagingListener

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

the class MessagingController method getFolderUnreadMessageCount.

public void getFolderUnreadMessageCount(final Account account, final String folderName, final MessagingListener l) {
    Runnable unreadRunnable = new Runnable() {

        @Override
        public void run() {
            int unreadMessageCount = 0;
            try {
                Folder localFolder = account.getLocalStore().getFolder(folderName);
                unreadMessageCount = localFolder.getUnreadMessageCount();
            } catch (MessagingException me) {
                Timber.e(me, "Count not get unread count for account %s", account.getDescription());
            }
            l.folderStatusChanged(account, folderName, unreadMessageCount);
        }
    };
    put("getFolderUnread:" + account.getDescription() + ":" + folderName, l, unreadRunnable);
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) SuppressLint(android.annotation.SuppressLint)

Aggregations

MessagingException (com.fsck.k9.mail.MessagingException)25 LocalFolder (com.fsck.k9.mailstore.LocalFolder)20 LocalStore (com.fsck.k9.mailstore.LocalStore)20 LocalMessage (com.fsck.k9.mailstore.LocalMessage)18 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)15 SuppressLint (android.annotation.SuppressLint)14 Folder (com.fsck.k9.mail.Folder)14 Message (com.fsck.k9.mail.Message)14 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)14 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)13 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)13 IOException (java.io.IOException)13 Store (com.fsck.k9.mail.Store)12 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)12 ArrayList (java.util.ArrayList)9 FetchProfile (com.fsck.k9.mail.FetchProfile)7 VisibleForTesting (android.support.annotation.VisibleForTesting)6 Account (com.fsck.k9.Account)6 Date (java.util.Date)6 SearchAccount (com.fsck.k9.search.SearchAccount)5