Search in sources :

Example 91 with Account

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

the class EmailProvider method getAccountStats.

private Cursor getAccountStats(String accountUuid, String[] columns, final String selection, final String[] selectionArgs) {
    Account account = getAccount(accountUuid);
    LockableDatabase database = getDatabase(account);
    // Use default projection if none was given
    String[] sourceProjection = (columns == null) ? STATS_DEFAULT_PROJECTION : columns;
    // Create SQL query string
    final StringBuilder sql = new StringBuilder();
    sql.append("SELECT ");
    // Append projection for the database query
    // e.g. "SUM(read=0) AS unread_count, SUM(flagged) AS flagged_count"
    boolean first = true;
    for (String columnName : sourceProjection) {
        if (!first) {
            sql.append(',');
        } else {
            first = false;
        }
        if (StatsColumns.UNREAD_COUNT.equals(columnName)) {
            sql.append("SUM(" + MessageColumns.READ + "=0) AS " + StatsColumns.UNREAD_COUNT);
        } else if (StatsColumns.FLAGGED_COUNT.equals(columnName)) {
            sql.append("SUM(" + MessageColumns.FLAGGED + ") AS " + StatsColumns.FLAGGED_COUNT);
        } else {
            throw new IllegalArgumentException("Column name not allowed: " + columnName);
        }
    }
    // Table selection
    sql.append(" FROM messages");
    if (containsAny(selection, FOLDERS_COLUMNS)) {
        sql.append(" JOIN folders ON (folders.id = messages.folder_id)");
    }
    // WHERE clause
    sql.append(" WHERE (deleted = 0 AND empty = 0)");
    if (!TextUtils.isEmpty(selection)) {
        sql.append(" AND (");
        sql.append(selection);
        sql.append(")");
    }
    // Query the database and return the result cursor
    try {
        return database.execute(false, new DbCallback<Cursor>() {

            @Override
            public Cursor doDbWork(SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
                return db.rawQuery(sql.toString(), selectionArgs);
            }
        });
    } catch (UnavailableStorageException e) {
        throw new RuntimeException("Storage not available", e);
    } catch (MessagingException e) {
        throw new RuntimeException("messaging exception", e);
    }
}
Also used : Account(com.fsck.k9.Account) WrappedException(com.fsck.k9.mailstore.LockableDatabase.WrappedException) MessagingException(com.fsck.k9.mail.MessagingException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) EmailProviderCacheCursor(com.fsck.k9.cache.EmailProviderCacheCursor) Cursor(android.database.Cursor) LockableDatabase(com.fsck.k9.mailstore.LockableDatabase) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 92 with Account

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

the class EmailProvider method getMessages.

protected Cursor getMessages(String accountUuid, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder) {
    Account account = getAccount(accountUuid);
    LockableDatabase database = getDatabase(account);
    try {
        return database.execute(false, new DbCallback<Cursor>() {

            @Override
            public Cursor doDbWork(SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
                String where;
                if (TextUtils.isEmpty(selection)) {
                    where = InternalMessageColumns.DELETED + " = 0 AND " + InternalMessageColumns.EMPTY + " = 0";
                } else {
                    where = "(" + selection + ") AND " + InternalMessageColumns.DELETED + " = 0 AND " + InternalMessageColumns.EMPTY + " = 0";
                }
                final Cursor cursor;
                if (Utility.arrayContainsAny(projection, (Object[]) FOLDERS_COLUMNS)) {
                    StringBuilder query = new StringBuilder();
                    query.append("SELECT ");
                    boolean first = true;
                    for (String columnName : projection) {
                        if (!first) {
                            query.append(",");
                        } else {
                            first = false;
                        }
                        if (MessageColumns.ID.equals(columnName)) {
                            query.append("m.");
                            query.append(MessageColumns.ID);
                            query.append(" AS ");
                            query.append(MessageColumns.ID);
                        } else {
                            query.append(columnName);
                        }
                    }
                    query.append(" FROM messages m " + "JOIN threads t ON (t.message_id = m.id) " + "LEFT JOIN folders f ON (m.folder_id = f.id) " + "WHERE ");
                    query.append(SqlQueryBuilder.addPrefixToSelection(FIXUP_MESSAGES_COLUMNS, "m.", where));
                    query.append(" ORDER BY ");
                    query.append(SqlQueryBuilder.addPrefixToSelection(FIXUP_MESSAGES_COLUMNS, "m.", sortOrder));
                    cursor = db.rawQuery(query.toString(), selectionArgs);
                } else {
                    cursor = db.query(MESSAGES_TABLE, projection, where, selectionArgs, null, null, sortOrder);
                }
                return cursor;
            }
        });
    } catch (UnavailableStorageException e) {
        throw new RuntimeException("Storage not available", e);
    } catch (MessagingException e) {
        throw new RuntimeException("messaging exception", e);
    }
}
Also used : Account(com.fsck.k9.Account) WrappedException(com.fsck.k9.mailstore.LockableDatabase.WrappedException) LockableDatabase(com.fsck.k9.mailstore.LockableDatabase) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) MessagingException(com.fsck.k9.mail.MessagingException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) EmailProviderCacheCursor(com.fsck.k9.cache.EmailProviderCacheCursor) Cursor(android.database.Cursor)

Example 93 with Account

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

the class EmailProvider method getThread.

protected Cursor getThread(String accountUuid, final String[] projection, final String threadId, final String sortOrder) {
    Account account = getAccount(accountUuid);
    LockableDatabase database = getDatabase(account);
    try {
        return database.execute(false, new DbCallback<Cursor>() {

            @Override
            public Cursor doDbWork(SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
                StringBuilder query = new StringBuilder();
                query.append("SELECT ");
                boolean first = true;
                for (String columnName : projection) {
                    if (!first) {
                        query.append(",");
                    } else {
                        first = false;
                    }
                    if (MessageColumns.ID.equals(columnName)) {
                        query.append("m." + MessageColumns.ID + " AS " + MessageColumns.ID);
                    } else {
                        query.append(columnName);
                    }
                }
                query.append(" FROM " + THREADS_TABLE + " t JOIN " + MESSAGES_TABLE + " m " + "ON (m." + MessageColumns.ID + " = t." + ThreadColumns.MESSAGE_ID + ") ");
                if (Utility.arrayContainsAny(projection, (Object[]) FOLDERS_COLUMNS)) {
                    query.append("LEFT JOIN " + FOLDERS_TABLE + " f " + "ON (m." + MessageColumns.FOLDER_ID + " = f." + FolderColumns.ID + ") ");
                }
                query.append("WHERE " + ThreadColumns.ROOT + " = ? AND " + InternalMessageColumns.DELETED + " = 0 AND " + InternalMessageColumns.EMPTY + " = 0");
                query.append(" ORDER BY ");
                query.append(SqlQueryBuilder.addPrefixToSelection(FIXUP_MESSAGES_COLUMNS, "m.", sortOrder));
                return db.rawQuery(query.toString(), new String[] { threadId });
            }
        });
    } catch (UnavailableStorageException e) {
        throw new RuntimeException("Storage not available", e);
    } catch (MessagingException e) {
        throw new RuntimeException("messaging exception", e);
    }
}
Also used : Account(com.fsck.k9.Account) WrappedException(com.fsck.k9.mailstore.LockableDatabase.WrappedException) LockableDatabase(com.fsck.k9.mailstore.LockableDatabase) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) MessagingException(com.fsck.k9.mail.MessagingException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) EmailProviderCacheCursor(com.fsck.k9.cache.EmailProviderCacheCursor) Cursor(android.database.Cursor)

Example 94 with Account

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

the class WearNotifications method addSummaryActions.

public void addSummaryActions(Builder builder, NotificationData notificationData) {
    NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();
    addMarkAllAsReadAction(wearableExtender, notificationData);
    if (isDeleteActionAvailableForWear()) {
        addDeleteAllAction(wearableExtender, notificationData);
    }
    Account account = notificationData.getAccount();
    if (isArchiveActionAvailableForWear(account)) {
        addArchiveAllAction(wearableExtender, notificationData);
    }
    builder.extend(wearableExtender);
}
Also used : Account(com.fsck.k9.Account) WearableExtender(android.support.v4.app.NotificationCompat.WearableExtender) NotificationCompat(android.support.v4.app.NotificationCompat) WearableExtender(android.support.v4.app.NotificationCompat.WearableExtender)

Example 95 with Account

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

the class SettingsExporter method writeAccount.

private static void writeAccount(XmlSerializer serializer, Account account, Map<String, Object> prefs) throws IOException {
    Set<Integer> identities = new HashSet<>();
    Set<String> folders = new HashSet<>();
    String accountUuid = account.getUuid();
    serializer.startTag(null, ACCOUNT_ELEMENT);
    serializer.attribute(null, UUID_ATTRIBUTE, accountUuid);
    String name = (String) prefs.get(accountUuid + "." + Account.ACCOUNT_DESCRIPTION_KEY);
    if (name != null) {
        serializer.startTag(null, NAME_ELEMENT);
        serializer.text(name);
        serializer.endTag(null, NAME_ELEMENT);
    }
    // Write incoming server settings
    ServerSettings incoming = RemoteStore.decodeStoreUri(account.getStoreUri());
    serializer.startTag(null, INCOMING_SERVER_ELEMENT);
    serializer.attribute(null, TYPE_ATTRIBUTE, incoming.type.name());
    writeElement(serializer, HOST_ELEMENT, incoming.host);
    if (incoming.port != -1) {
        writeElement(serializer, PORT_ELEMENT, Integer.toString(incoming.port));
    }
    if (incoming.connectionSecurity != null) {
        writeElement(serializer, CONNECTION_SECURITY_ELEMENT, incoming.connectionSecurity.name());
    }
    if (incoming.authenticationType != null) {
        writeElement(serializer, AUTHENTICATION_TYPE_ELEMENT, incoming.authenticationType.name());
    }
    writeElement(serializer, USERNAME_ELEMENT, incoming.username);
    writeElement(serializer, CLIENT_CERTIFICATE_ALIAS_ELEMENT, incoming.clientCertificateAlias);
    // XXX For now we don't export the password
    //writeElement(serializer, PASSWORD_ELEMENT, incoming.password);
    Map<String, String> extras = incoming.getExtra();
    if (extras != null && extras.size() > 0) {
        serializer.startTag(null, EXTRA_ELEMENT);
        for (Entry<String, String> extra : extras.entrySet()) {
            writeKeyAndPrettyValueFromSetting(serializer, extra.getKey(), extra.getValue());
        }
        serializer.endTag(null, EXTRA_ELEMENT);
    }
    serializer.endTag(null, INCOMING_SERVER_ELEMENT);
    // Write outgoing server settings
    ServerSettings outgoing = Transport.decodeTransportUri(account.getTransportUri());
    serializer.startTag(null, OUTGOING_SERVER_ELEMENT);
    serializer.attribute(null, TYPE_ATTRIBUTE, outgoing.type.name());
    writeElement(serializer, HOST_ELEMENT, outgoing.host);
    if (outgoing.port != -1) {
        writeElement(serializer, PORT_ELEMENT, Integer.toString(outgoing.port));
    }
    if (outgoing.connectionSecurity != null) {
        writeElement(serializer, CONNECTION_SECURITY_ELEMENT, outgoing.connectionSecurity.name());
    }
    if (outgoing.authenticationType != null) {
        writeElement(serializer, AUTHENTICATION_TYPE_ELEMENT, outgoing.authenticationType.name());
    }
    writeElement(serializer, USERNAME_ELEMENT, outgoing.username);
    writeElement(serializer, CLIENT_CERTIFICATE_ALIAS_ELEMENT, outgoing.clientCertificateAlias);
    // XXX For now we don't export the password
    //writeElement(serializer, PASSWORD_ELEMENT, outgoing.password);
    extras = outgoing.getExtra();
    if (extras != null && extras.size() > 0) {
        serializer.startTag(null, EXTRA_ELEMENT);
        for (Entry<String, String> extra : extras.entrySet()) {
            writeKeyAndPrettyValueFromSetting(serializer, extra.getKey(), extra.getValue());
        }
        serializer.endTag(null, EXTRA_ELEMENT);
    }
    serializer.endTag(null, OUTGOING_SERVER_ELEMENT);
    // Write account settings
    serializer.startTag(null, SETTINGS_ELEMENT);
    for (Map.Entry<String, Object> entry : prefs.entrySet()) {
        String key = entry.getKey();
        String valueString = entry.getValue().toString();
        String[] comps = key.split("\\.", 2);
        if (comps.length < 2) {
            // Skip global settings
            continue;
        }
        String keyUuid = comps[0];
        String keyPart = comps[1];
        if (!keyUuid.equals(accountUuid)) {
            // Setting doesn't belong to the account we're currently writing.
            continue;
        }
        int indexOfLastDot = keyPart.lastIndexOf(".");
        boolean hasThirdPart = indexOfLastDot != -1 && indexOfLastDot < keyPart.length() - 1;
        if (hasThirdPart) {
            String secondPart = keyPart.substring(0, indexOfLastDot);
            String thirdPart = keyPart.substring(indexOfLastDot + 1);
            if (Account.IDENTITY_DESCRIPTION_KEY.equals(secondPart)) {
                // This is an identity key. Save identity index for later...
                try {
                    identities.add(Integer.parseInt(thirdPart));
                } catch (NumberFormatException e) {
                /* ignore */
                }
                // ... but don't write it now.
                continue;
            }
            if (FolderSettings.SETTINGS.containsKey(thirdPart)) {
                // This is a folder key. Save folder name for later...
                folders.add(secondPart);
                // ... but don't write it now.
                continue;
            }
        }
        TreeMap<Integer, SettingsDescription> versionedSetting = AccountSettings.SETTINGS.get(keyPart);
        if (versionedSetting != null) {
            Integer highestVersion = versionedSetting.lastKey();
            SettingsDescription setting = versionedSetting.get(highestVersion);
            if (setting != null) {
                // Only export account settings that can be found in AccountSettings.SETTINGS
                try {
                    writeKeyAndPrettyValueFromSetting(serializer, keyPart, setting, valueString);
                } catch (InvalidSettingValueException e) {
                    Timber.w("Account setting \"%s\" (%s) has invalid value \"%s\" in preference storage. " + "This shouldn't happen!", keyPart, account.getDescription(), valueString);
                }
            }
        }
    }
    serializer.endTag(null, SETTINGS_ELEMENT);
    if (identities.size() > 0) {
        serializer.startTag(null, IDENTITIES_ELEMENT);
        // Sort identity indices (that's why we store them as Integers)
        List<Integer> sortedIdentities = new ArrayList<>(identities);
        Collections.sort(sortedIdentities);
        for (Integer identityIndex : sortedIdentities) {
            writeIdentity(serializer, accountUuid, identityIndex.toString(), prefs);
        }
        serializer.endTag(null, IDENTITIES_ELEMENT);
    }
    if (folders.size() > 0) {
        serializer.startTag(null, FOLDERS_ELEMENT);
        for (String folder : folders) {
            writeFolder(serializer, accountUuid, folder, prefs);
        }
        serializer.endTag(null, FOLDERS_ELEMENT);
    }
    serializer.endTag(null, ACCOUNT_ELEMENT);
}
Also used : ArrayList(java.util.ArrayList) SettingsDescription(com.fsck.k9.preferences.Settings.SettingsDescription) InvalidSettingValueException(com.fsck.k9.preferences.Settings.InvalidSettingValueException) ServerSettings(com.fsck.k9.mail.ServerSettings) Map(java.util.Map) TreeMap(java.util.TreeMap) HashSet(java.util.HashSet)

Aggregations

Account (com.fsck.k9.Account)122 Test (org.junit.Test)81 MessagingException (com.fsck.k9.mail.MessagingException)53 LocalFolder (com.fsck.k9.mailstore.LocalFolder)44 LocalMessage (com.fsck.k9.mailstore.LocalMessage)43 LocalStore (com.fsck.k9.mailstore.LocalStore)42 ArrayList (java.util.ArrayList)34 MessageReference (com.fsck.k9.activity.MessageReference)32 Message (com.fsck.k9.mail.Message)29 Folder (com.fsck.k9.mail.Folder)27 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)25 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)22 IOException (java.io.IOException)22 Cursor (android.database.Cursor)21 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)20 Store (com.fsck.k9.mail.Store)20 SearchAccount (com.fsck.k9.search.SearchAccount)20 PendingIntent (android.app.PendingIntent)19 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)19 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)18