Search in sources :

Example 1 with ConditionsTreeNode

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

the class Account method getStats.

/**
     * @return <code>null</code> if not available
     * @throws MessagingException
     * @see {@link #isAvailable(Context)}
     */
public AccountStats getStats(Context context) throws MessagingException {
    if (!isAvailable(context)) {
        return null;
    }
    AccountStats stats = new AccountStats();
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(EmailProvider.CONTENT_URI, "account/" + getUuid() + "/stats");
    String[] projection = { StatsColumns.UNREAD_COUNT, StatsColumns.FLAGGED_COUNT };
    // Create LocalSearch instance to exclude special folders (Trash, Drafts, Spam, Outbox,
    // Sent) and limit the search to displayable folders.
    LocalSearch search = new LocalSearch();
    excludeSpecialFolders(search);
    limitToDisplayableFolders(search);
    // Use the LocalSearch instance to create a WHERE clause to query the content provider
    StringBuilder query = new StringBuilder();
    List<String> queryArgs = new ArrayList<>();
    ConditionsTreeNode conditions = search.getConditions();
    SqlQueryBuilder.buildWhereClause(this, conditions, query, queryArgs);
    String selection = query.toString();
    String[] selectionArgs = queryArgs.toArray(new String[0]);
    Cursor cursor = cr.query(uri, projection, selection, selectionArgs, null);
    try {
        if (cursor != null && cursor.moveToFirst()) {
            stats.unreadMessageCount = cursor.getInt(0);
            stats.flaggedMessageCount = cursor.getInt(1);
        }
    } finally {
        Utility.closeQuietly(cursor);
    }
    LocalStore localStore = getLocalStore();
    if (K9.measureAccounts()) {
        stats.size = localStore.getSize();
    }
    return stats;
}
Also used : ConditionsTreeNode(com.fsck.k9.search.ConditionsTreeNode) LocalSearch(com.fsck.k9.search.LocalSearch) ArrayList(java.util.ArrayList) LocalStore(com.fsck.k9.mailstore.LocalStore) Cursor(android.database.Cursor) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver)

Example 2 with ConditionsTreeNode

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

the class SqlQueryBuilder method buildWhereClauseInternal.

private static void buildWhereClauseInternal(Account account, ConditionsTreeNode node, StringBuilder query, List<String> selectionArgs) {
    if (node == null) {
        query.append("1");
        return;
    }
    if (node.mLeft == null && node.mRight == null) {
        AccountSearchConditions accountSearchConditions = DI.get(AccountSearchConditions.class);
        SearchCondition condition = node.mCondition;
        switch(condition.field) {
            case SEARCHABLE:
                {
                    switch(account.getSearchableFolders()) {
                        case ALL:
                            {
                                // Create temporary LocalSearch object so we can use...
                                LocalSearch tempSearch = new LocalSearch();
                                // ...the helper methods in Account to create the necessary conditions
                                // to exclude "unwanted" folders.
                                accountSearchConditions.excludeUnwantedFolders(account, tempSearch);
                                buildWhereClauseInternal(account, tempSearch.getConditions(), query, selectionArgs);
                                break;
                            }
                        case DISPLAYABLE:
                            {
                                // Create temporary LocalSearch object so we can use...
                                LocalSearch tempSearch = new LocalSearch();
                                // ...the helper methods in Account to create the necessary conditions
                                // to limit the selection to displayable, non-special folders.
                                accountSearchConditions.excludeSpecialFolders(account, tempSearch);
                                accountSearchConditions.limitToDisplayableFolders(account, tempSearch);
                                buildWhereClauseInternal(account, tempSearch.getConditions(), query, selectionArgs);
                                break;
                            }
                        case NONE:
                            {
                                // Dummy condition, never select
                                query.append("0");
                                break;
                            }
                    }
                    break;
                }
            case MESSAGE_CONTENTS:
                {
                    String fulltextQueryString = condition.value;
                    if (condition.attribute != Attribute.CONTAINS) {
                        Timber.e("message contents can only be matched!");
                    }
                    query.append("m.id IN (SELECT docid FROM messages_fulltext WHERE fulltext MATCH ?)");
                    selectionArgs.add(fulltextQueryString);
                    break;
                }
            default:
                {
                    appendCondition(condition, query, selectionArgs);
                }
        }
    } else {
        query.append("(");
        buildWhereClauseInternal(account, node.mLeft, query, selectionArgs);
        query.append(") ");
        query.append(node.mValue.name());
        query.append(" (");
        buildWhereClauseInternal(account, node.mRight, query, selectionArgs);
        query.append(")");
    }
}
Also used : SearchCondition(com.fsck.k9.search.SearchSpecification.SearchCondition)

Example 3 with ConditionsTreeNode

use of com.fsck.k9.search.ConditionsTreeNode 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 4 with ConditionsTreeNode

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

the class ConditionsTreeNode method buildNodeFromRow.

/**
 * Converts a single database row to a single condition node.
 *
 * @param cursor Cursor pointing to the row we want to convert.
 * @return A single ConditionsTreeNode
 */
private static ConditionsTreeNode buildNodeFromRow(Cursor cursor) {
    ConditionsTreeNode result = null;
    SearchCondition condition = null;
    Operator tmpValue = ConditionsTreeNode.Operator.valueOf(cursor.getString(5));
    if (tmpValue == Operator.CONDITION) {
        condition = new SearchCondition(SearchField.valueOf(cursor.getString(0)), Attribute.valueOf(cursor.getString(2)), cursor.getString(1));
    }
    result = new ConditionsTreeNode(condition);
    result.mValue = tmpValue;
    result.mLeftMPTTMarker = cursor.getInt(3);
    result.mRightMPTTMarker = cursor.getInt(4);
    return result;
}
Also used : SearchCondition(com.fsck.k9.search.SearchSpecification.SearchCondition)

Aggregations

ContentResolver (android.content.ContentResolver)2 Cursor (android.database.Cursor)2 Uri (android.net.Uri)2 ConditionsTreeNode (com.fsck.k9.search.ConditionsTreeNode)2 LocalSearch (com.fsck.k9.search.LocalSearch)2 SearchCondition (com.fsck.k9.search.SearchSpecification.SearchCondition)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 Account (com.fsck.k9.Account)1 AccountStats (com.fsck.k9.AccountStats)1 Preferences (com.fsck.k9.Preferences)1 LocalStore (com.fsck.k9.mailstore.LocalStore)1 SearchAccount (com.fsck.k9.search.SearchAccount)1