Search in sources :

Example 26 with Preferences

use of org.orcid.jaxb.model.record_rc3.Preferences 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 27 with Preferences

use of org.orcid.jaxb.model.record_rc3.Preferences in project k-9 by k9mail.

the class MessageCompose method processDraftMessage.

private void processDraftMessage(MessageViewInfo messageViewInfo) {
    Message message = messageViewInfo.message;
    draftId = MessagingController.getInstance(getApplication()).getId(message);
    subjectView.setText(message.getSubject());
    recipientPresenter.initFromDraftMessage(message);
    // Read In-Reply-To header from draft
    final String[] inReplyTo = message.getHeader("In-Reply-To");
    if (inReplyTo.length >= 1) {
        repliedToMessageId = inReplyTo[0];
    }
    // Read References header from draft
    final String[] references = message.getHeader("References");
    if (references.length >= 1) {
        referencedMessageIds = references[0];
    }
    if (!relatedMessageProcessed) {
        attachmentPresenter.loadNonInlineAttachments(messageViewInfo);
    }
    // Decode the identity header when loading a draft.
    // See buildIdentityHeader(TextBody) for a detailed description of the composition of this blob.
    Map<IdentityField, String> k9identity = new HashMap<>();
    String[] identityHeaders = message.getHeader(K9.IDENTITY_HEADER);
    if (identityHeaders.length > 0 && identityHeaders[0] != null) {
        k9identity = IdentityHeaderParser.parse(identityHeaders[0]);
    }
    Identity newIdentity = new Identity();
    if (k9identity.containsKey(IdentityField.SIGNATURE)) {
        newIdentity.setSignatureUse(true);
        newIdentity.setSignature(k9identity.get(IdentityField.SIGNATURE));
        signatureChanged = true;
    } else {
        if (message instanceof LocalMessage) {
            newIdentity.setSignatureUse(((LocalMessage) message).getFolder().getSignatureUse());
        }
        newIdentity.setSignature(identity.getSignature());
    }
    if (k9identity.containsKey(IdentityField.NAME)) {
        newIdentity.setName(k9identity.get(IdentityField.NAME));
        identityChanged = true;
    } else {
        newIdentity.setName(identity.getName());
    }
    if (k9identity.containsKey(IdentityField.EMAIL)) {
        newIdentity.setEmail(k9identity.get(IdentityField.EMAIL));
        identityChanged = true;
    } else {
        newIdentity.setEmail(identity.getEmail());
    }
    if (k9identity.containsKey(IdentityField.ORIGINAL_MESSAGE)) {
        relatedMessageReference = null;
        String originalMessage = k9identity.get(IdentityField.ORIGINAL_MESSAGE);
        MessageReference messageReference = MessageReference.parse(originalMessage);
        if (messageReference != null) {
            // Check if this is a valid account in our database
            Preferences prefs = Preferences.getPreferences(getApplicationContext());
            Account account = prefs.getAccount(messageReference.getAccountUuid());
            if (account != null) {
                relatedMessageReference = messageReference;
            }
        }
    }
    identity = newIdentity;
    updateSignature();
    updateFrom();
    quotedMessagePresenter.processDraftMessage(messageViewInfo, k9identity);
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) Account(com.fsck.k9.Account) LocalMessage(com.fsck.k9.mailstore.LocalMessage) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) HashMap(java.util.HashMap) Identity(com.fsck.k9.Identity) Preferences(com.fsck.k9.Preferences) IdentityField(com.fsck.k9.message.IdentityField)

Example 28 with Preferences

use of org.orcid.jaxb.model.record_rc3.Preferences in project k-9 by k9mail.

the class MessageProviderTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    mMockResolver = getMockContentResolver();
    mContext = K9.app;
    Preferences preferences = Preferences.getPreferences(getMockContext());
    List<Account> accountList = preferences.getAccounts();
    for (Account account : accountList) {
        preferences.deleteAccount(account);
    }
}
Also used : Account(com.fsck.k9.Account) Preferences(com.fsck.k9.Preferences) Before(org.junit.Before)

Example 29 with Preferences

use of org.orcid.jaxb.model.record_rc3.Preferences in project k-9 by k9mail.

the class MessageList method onToggleTheme.

private void onToggleTheme() {
    if (K9.getK9MessageViewTheme() == K9.Theme.DARK) {
        K9.setK9MessageViewThemeSetting(K9.Theme.LIGHT);
    } else {
        K9.setK9MessageViewThemeSetting(K9.Theme.DARK);
    }
    new Thread(new Runnable() {

        @Override
        public void run() {
            Context appContext = getApplicationContext();
            Preferences prefs = Preferences.getPreferences(appContext);
            StorageEditor editor = prefs.getStorage().edit();
            K9.save(editor);
            editor.commit();
        }
    }).start();
    recreate();
}
Also used : Context(android.content.Context) Preferences(com.fsck.k9.Preferences) StorageEditor(com.fsck.k9.preferences.StorageEditor)

Example 30 with Preferences

use of org.orcid.jaxb.model.record_rc3.Preferences in project k-9 by k9mail.

the class SettingsImporter method importAccount.

private static AccountDescriptionPair importAccount(Context context, StorageEditor editor, int contentVersion, ImportedAccount account, boolean overwrite) throws InvalidSettingValueException {
    AccountDescription original = new AccountDescription(account.name, account.uuid);
    Preferences prefs = Preferences.getPreferences(context);
    List<Account> accounts = prefs.getAccounts();
    String uuid = account.uuid;
    Account existingAccount = prefs.getAccount(uuid);
    boolean mergeImportedAccount = (overwrite && existingAccount != null);
    if (!overwrite && existingAccount != null) {
        // An account with this UUID already exists, but we're not allowed to overwrite it.
        // So generate a new UUID.
        uuid = UUID.randomUUID().toString();
    }
    // Make sure the account name is unique
    String accountName = account.name;
    if (isAccountNameUsed(accountName, accounts)) {
        // number >= 1 that results in an unused account name.
        for (int i = 1; i <= accounts.size(); i++) {
            accountName = account.name + " (" + i + ")";
            if (!isAccountNameUsed(accountName, accounts)) {
                break;
            }
        }
    }
    // Write account name
    String accountKeyPrefix = uuid + ".";
    putString(editor, accountKeyPrefix + Account.ACCOUNT_DESCRIPTION_KEY, accountName);
    if (account.incoming == null) {
        // We don't import accounts without incoming server settings
        throw new InvalidSettingValueException();
    }
    // Write incoming server settings (storeUri)
    ServerSettings incoming = new ImportedServerSettings(account.incoming);
    String storeUri = RemoteStore.createStoreUri(incoming);
    putString(editor, accountKeyPrefix + Account.STORE_URI_KEY, Base64.encode(storeUri));
    // Mark account as disabled if the AuthType isn't EXTERNAL and the
    // settings file didn't contain a password
    boolean createAccountDisabled = AuthType.EXTERNAL != incoming.authenticationType && (incoming.password == null || incoming.password.isEmpty());
    if (account.outgoing == null && !ServerSettings.Type.WebDAV.name().equals(account.incoming.type)) {
        // All account types except WebDAV need to provide outgoing server settings
        throw new InvalidSettingValueException();
    }
    if (account.outgoing != null) {
        // Write outgoing server settings (transportUri)
        ServerSettings outgoing = new ImportedServerSettings(account.outgoing);
        String transportUri = Transport.createTransportUri(outgoing);
        putString(editor, accountKeyPrefix + Account.TRANSPORT_URI_KEY, Base64.encode(transportUri));
        /*
             * Mark account as disabled if the settings file contained a username but no password. However, no password
             * is required for the outgoing server for WebDAV accounts, because incoming and outgoing servers are 
             * identical for this account type. Nor is a password required if the AuthType is EXTERNAL.
             */
        boolean outgoingPasswordNeeded = AuthType.EXTERNAL != outgoing.authenticationType && !(ServerSettings.Type.WebDAV == outgoing.type) && outgoing.username != null && !outgoing.username.isEmpty() && (outgoing.password == null || outgoing.password.isEmpty());
        createAccountDisabled = outgoingPasswordNeeded || createAccountDisabled;
    }
    // Write key to mark account as disabled if necessary
    if (createAccountDisabled) {
        editor.putBoolean(accountKeyPrefix + "enabled", false);
    }
    // Validate account settings
    Map<String, Object> validatedSettings = AccountSettings.validate(contentVersion, account.settings.settings, !mergeImportedAccount);
    // Upgrade account settings to current content version
    if (contentVersion != Settings.VERSION) {
        AccountSettings.upgrade(contentVersion, validatedSettings);
    }
    // Convert account settings to the string representation used in preference storage
    Map<String, String> stringSettings = AccountSettings.convert(validatedSettings);
    // Merge account settings if necessary
    Map<String, String> writeSettings;
    if (mergeImportedAccount) {
        writeSettings = new HashMap<>(AccountSettings.getAccountSettings(prefs.getStorage(), uuid));
        writeSettings.putAll(stringSettings);
    } else {
        writeSettings = stringSettings;
    }
    // Write account settings
    for (Map.Entry<String, String> setting : writeSettings.entrySet()) {
        String key = accountKeyPrefix + setting.getKey();
        String value = setting.getValue();
        putString(editor, key, value);
    }
    // If it's a new account generate and write a new "accountNumber"
    if (!mergeImportedAccount) {
        int newAccountNumber = Account.generateAccountNumber(prefs);
        putString(editor, accountKeyPrefix + "accountNumber", Integer.toString(newAccountNumber));
    }
    // Write identities
    if (account.identities != null) {
        importIdentities(editor, contentVersion, uuid, account, overwrite, existingAccount, prefs);
    } else if (!mergeImportedAccount) {
        // Require accounts to at least have one identity
        throw new InvalidSettingValueException();
    }
    // Write folder settings
    if (account.folders != null) {
        for (ImportedFolder folder : account.folders) {
            importFolder(editor, contentVersion, uuid, folder, mergeImportedAccount, prefs);
        }
    }
    //TODO: sync folder settings with localstore?
    AccountDescription imported = new AccountDescription(accountName, uuid);
    return new AccountDescriptionPair(original, imported, mergeImportedAccount);
}
Also used : Account(com.fsck.k9.Account) InvalidSettingValueException(com.fsck.k9.preferences.Settings.InvalidSettingValueException) ServerSettings(com.fsck.k9.mail.ServerSettings) SharedPreferences(android.content.SharedPreferences) Preferences(com.fsck.k9.Preferences) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Preferences (com.fsck.k9.Preferences)17 Account (com.fsck.k9.Account)13 Test (org.junit.Test)12 Preferences (org.apereo.portal.soffit.model.v1_0.Preferences)5 Preferences (org.orcid.jaxb.model.record_v2.Preferences)5 StorageEditor (com.fsck.k9.preferences.StorageEditor)4 SearchAccount (com.fsck.k9.search.SearchAccount)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 InvalidSettingValueException (com.fsck.k9.preferences.Settings.InvalidSettingValueException)3 Storage (com.fsck.k9.preferences.Storage)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Bearer (org.apereo.portal.soffit.model.v1_0.Bearer)3 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 Uri (android.net.Uri)2 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)2 LocalSearch (com.fsck.k9.search.LocalSearch)2 Method (java.lang.reflect.Method)2