Search in sources :

Example 26 with ServerSettings

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

the class ImapStoreUriCreator method create.

/**
     * Creates an ImapStore URI with the supplied settings.
     *
     * @param server
     *         The {@link ServerSettings} object that holds the server settings.
     *
     * @return An ImapStore URI that holds the same information as the {@code server} parameter.
     *
     * @see com.fsck.k9.mail.store.StoreConfig#getStoreUri()
     * @see ImapStore#decodeUri(String)
     */
public static String create(ServerSettings server) {
    String userEnc = encodeUtf8(server.username);
    String passwordEnc = (server.password != null) ? encodeUtf8(server.password) : "";
    String clientCertificateAliasEnc = (server.clientCertificateAlias != null) ? encodeUtf8(server.clientCertificateAlias) : "";
    String scheme;
    switch(server.connectionSecurity) {
        case SSL_TLS_REQUIRED:
            scheme = "imap+ssl+";
            break;
        case STARTTLS_REQUIRED:
            scheme = "imap+tls+";
            break;
        default:
        case NONE:
            scheme = "imap";
            break;
    }
    AuthType authType = server.authenticationType;
    String userInfo;
    if (authType == AuthType.EXTERNAL) {
        userInfo = authType.name() + ":" + userEnc + ":" + clientCertificateAliasEnc;
    } else {
        userInfo = authType.name() + ":" + userEnc + ":" + passwordEnc;
    }
    try {
        Map<String, String> extra = server.getExtra();
        String path;
        if (extra != null) {
            boolean autoDetectNamespace = Boolean.TRUE.toString().equals(extra.get(ImapStoreSettings.AUTODETECT_NAMESPACE_KEY));
            String pathPrefix = (autoDetectNamespace) ? null : extra.get(ImapStoreSettings.PATH_PREFIX_KEY);
            path = "/" + (autoDetectNamespace ? "1" : "0") + "|" + ((pathPrefix == null) ? "" : pathPrefix);
        } else {
            path = "/1|";
        }
        return new URI(scheme, userInfo, server.host, server.port, path, null, null).toString();
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Can't create ImapStore URI", e);
    }
}
Also used : AuthType(com.fsck.k9.mail.AuthType) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 27 with ServerSettings

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

the class AccountSetupBasics method finishAutoSetup.

private void finishAutoSetup() {
    String email = mEmailView.getText().toString();
    String password = mPasswordView.getText().toString();
    String[] emailParts = splitEmail(email);
    String user = emailParts[0];
    String domain = emailParts[1];
    try {
        String userEnc = UrlEncodingHelper.encodeUtf8(user);
        String passwordEnc = UrlEncodingHelper.encodeUtf8(password);
        String incomingUsername = mProvider.incomingUsernameTemplate;
        incomingUsername = incomingUsername.replaceAll("\\$email", email);
        incomingUsername = incomingUsername.replaceAll("\\$user", userEnc);
        incomingUsername = incomingUsername.replaceAll("\\$domain", domain);
        URI incomingUriTemplate = mProvider.incomingUriTemplate;
        URI incomingUri = new URI(incomingUriTemplate.getScheme(), incomingUsername + ":" + passwordEnc, incomingUriTemplate.getHost(), incomingUriTemplate.getPort(), null, null, null);
        String outgoingUsername = mProvider.outgoingUsernameTemplate;
        URI outgoingUriTemplate = mProvider.outgoingUriTemplate;
        URI outgoingUri;
        if (outgoingUsername != null) {
            outgoingUsername = outgoingUsername.replaceAll("\\$email", email);
            outgoingUsername = outgoingUsername.replaceAll("\\$user", userEnc);
            outgoingUsername = outgoingUsername.replaceAll("\\$domain", domain);
            outgoingUri = new URI(outgoingUriTemplate.getScheme(), outgoingUsername + ":" + passwordEnc, outgoingUriTemplate.getHost(), outgoingUriTemplate.getPort(), null, null, null);
        } else {
            outgoingUri = new URI(outgoingUriTemplate.getScheme(), null, outgoingUriTemplate.getHost(), outgoingUriTemplate.getPort(), null, null, null);
        }
        if (mAccount == null) {
            mAccount = Preferences.getPreferences(this).newAccount();
        }
        mAccount.setName(getOwnerName());
        mAccount.setEmail(email);
        mAccount.setStoreUri(incomingUri.toString());
        mAccount.setTransportUri(outgoingUri.toString());
        setupFolderNames(incomingUriTemplate.getHost().toLowerCase(Locale.US));
        ServerSettings incomingSettings = RemoteStore.decodeStoreUri(incomingUri.toString());
        mAccount.setDeletePolicy(AccountCreator.getDefaultDeletePolicy(incomingSettings.type));
        // Check incoming here.  Then check outgoing in onActivityResult()
        AccountSetupCheckSettings.actionCheckSettings(this, mAccount, CheckDirection.INCOMING);
    } catch (URISyntaxException use) {
        /*
             * If there is some problem with the URI we give up and go on to
             * manual setup.
             */
        onManualSetup();
    }
}
Also used : ServerSettings(com.fsck.k9.mail.ServerSettings) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 28 with ServerSettings

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

the class AccountSetupIncoming method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (Intent.ACTION_EDIT.equals(getIntent().getAction())) {
            boolean isPushCapable = false;
            try {
                Store store = mAccount.getRemoteStore();
                isPushCapable = store.isPushCapable();
            } catch (Exception e) {
                Timber.e(e, "Could not get remote store");
            }
            if (isPushCapable && mAccount.getFolderPushMode() != FolderMode.NONE) {
                MailService.actionRestartPushers(this, null);
            }
            mAccount.save(Preferences.getPreferences(this));
            finish();
        } else {
            /*
                 * Set the username and password for the outgoing settings to the username and
                 * password the user just set for incoming.
                 */
            try {
                String username = mUsernameView.getText().toString();
                String password = null;
                String clientCertificateAlias = null;
                AuthType authType = getSelectedAuthType();
                if (AuthType.EXTERNAL == authType) {
                    clientCertificateAlias = mClientCertificateSpinner.getAlias();
                } else {
                    password = mPasswordView.getText().toString();
                }
                URI oldUri = new URI(mAccount.getTransportUri());
                ServerSettings transportServer = new ServerSettings(Type.SMTP, oldUri.getHost(), oldUri.getPort(), ConnectionSecurity.SSL_TLS_REQUIRED, authType, username, password, clientCertificateAlias);
                String transportUri = Transport.createTransportUri(transportServer);
                mAccount.setTransportUri(transportUri);
            } catch (URISyntaxException use) {
            /*
                     * If we can't set up the URL we just continue. It's only for
                     * convenience.
                     */
            }
            AccountSetupOutgoing.actionOutgoingSettings(this, mAccount, mMakeDefault);
            finish();
        }
    }
}
Also used : ServerSettings(com.fsck.k9.mail.ServerSettings) Store(com.fsck.k9.mail.Store) RemoteStore(com.fsck.k9.mail.store.RemoteStore) AuthType(com.fsck.k9.mail.AuthType) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException)

Example 29 with ServerSettings

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

the class AccountSetupIncoming method onNext.

protected void onNext() {
    try {
        ConnectionSecurity connectionSecurity = getSelectedSecurity();
        String username = mUsernameView.getText().toString();
        String password = null;
        String clientCertificateAlias = null;
        AuthType authType = getSelectedAuthType();
        if (authType == AuthType.EXTERNAL) {
            clientCertificateAlias = mClientCertificateSpinner.getAlias();
        } else {
            password = mPasswordView.getText().toString();
        }
        String host = mServerView.getText().toString();
        int port = Integer.parseInt(mPortView.getText().toString());
        Map<String, String> extra = null;
        if (Type.IMAP == mStoreType) {
            extra = new HashMap<String, String>();
            extra.put(ImapStoreSettings.AUTODETECT_NAMESPACE_KEY, Boolean.toString(mImapAutoDetectNamespaceView.isChecked()));
            extra.put(ImapStoreSettings.PATH_PREFIX_KEY, mImapPathPrefixView.getText().toString());
        } else if (Type.WebDAV == mStoreType) {
            extra = new HashMap<String, String>();
            extra.put(WebDavStoreSettings.PATH_KEY, mWebdavPathPrefixView.getText().toString());
            extra.put(WebDavStoreSettings.AUTH_PATH_KEY, mWebdavAuthPathView.getText().toString());
            extra.put(WebDavStoreSettings.MAILBOX_PATH_KEY, mWebdavMailboxPathView.getText().toString());
        }
        mAccount.deleteCertificate(host, port, CheckDirection.INCOMING);
        ServerSettings settings = new ServerSettings(mStoreType, host, port, connectionSecurity, authType, username, password, clientCertificateAlias, extra);
        mAccount.setStoreUri(RemoteStore.createStoreUri(settings));
        mAccount.setCompression(NetworkType.MOBILE, mCompressionMobile.isChecked());
        mAccount.setCompression(NetworkType.WIFI, mCompressionWifi.isChecked());
        mAccount.setCompression(NetworkType.OTHER, mCompressionOther.isChecked());
        mAccount.setSubscribedFoldersOnly(mSubscribedFoldersOnly.isChecked());
        AccountSetupCheckSettings.actionCheckSettings(this, mAccount, CheckDirection.INCOMING);
    } catch (Exception e) {
        failure(e);
    }
}
Also used : ConnectionSecurity(com.fsck.k9.mail.ConnectionSecurity) HashMap(java.util.HashMap) ServerSettings(com.fsck.k9.mail.ServerSettings) AuthType(com.fsck.k9.mail.AuthType) URISyntaxException(java.net.URISyntaxException)

Example 30 with ServerSettings

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

the class AccountSetupIncoming method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.account_setup_incoming);
    mUsernameView = (EditText) findViewById(R.id.account_username);
    mPasswordView = (EditText) findViewById(R.id.account_password);
    mClientCertificateSpinner = (ClientCertificateSpinner) findViewById(R.id.account_client_certificate_spinner);
    mClientCertificateLabelView = (TextView) findViewById(R.id.account_client_certificate_label);
    mPasswordLabelView = (TextView) findViewById(R.id.account_password_label);
    TextView serverLabelView = (TextView) findViewById(R.id.account_server_label);
    mServerView = (EditText) findViewById(R.id.account_server);
    mPortView = (EditText) findViewById(R.id.account_port);
    mSecurityTypeView = (Spinner) findViewById(R.id.account_security_type);
    mAuthTypeView = (Spinner) findViewById(R.id.account_auth_type);
    mImapAutoDetectNamespaceView = (CheckBox) findViewById(R.id.imap_autodetect_namespace);
    mImapPathPrefixView = (EditText) findViewById(R.id.imap_path_prefix);
    mWebdavPathPrefixView = (EditText) findViewById(R.id.webdav_path_prefix);
    mWebdavAuthPathView = (EditText) findViewById(R.id.webdav_auth_path);
    mWebdavMailboxPathView = (EditText) findViewById(R.id.webdav_mailbox_path);
    mNextButton = (Button) findViewById(R.id.next);
    mCompressionMobile = (CheckBox) findViewById(R.id.compression_mobile);
    mCompressionWifi = (CheckBox) findViewById(R.id.compression_wifi);
    mCompressionOther = (CheckBox) findViewById(R.id.compression_other);
    mSubscribedFoldersOnly = (CheckBox) findViewById(R.id.subscribed_folders_only);
    mNextButton.setOnClickListener(this);
    mImapAutoDetectNamespaceView.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mImapPathPrefixView.setEnabled(!isChecked);
            if (isChecked && mImapPathPrefixView.hasFocus()) {
                mImapPathPrefixView.focusSearch(View.FOCUS_UP).requestFocus();
            } else if (!isChecked) {
                mImapPathPrefixView.requestFocus();
            }
        }
    });
    mAuthTypeAdapter = AuthTypeAdapter.get(this);
    mAuthTypeView.setAdapter(mAuthTypeAdapter);
    /*
         * Only allow digits in the port field.
         */
    mPortView.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
    String accountUuid = getIntent().getStringExtra(EXTRA_ACCOUNT);
    mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
    mMakeDefault = getIntent().getBooleanExtra(EXTRA_MAKE_DEFAULT, false);
    /*
         * If we're being reloaded we override the original account with the one
         * we saved
         */
    if (savedInstanceState != null && savedInstanceState.containsKey(EXTRA_ACCOUNT)) {
        accountUuid = savedInstanceState.getString(EXTRA_ACCOUNT);
        mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
    }
    boolean editSettings = Intent.ACTION_EDIT.equals(getIntent().getAction());
    try {
        ServerSettings settings = RemoteStore.decodeStoreUri(mAccount.getStoreUri());
        if (savedInstanceState == null) {
            // The first item is selected if settings.authenticationType is null or is not in mAuthTypeAdapter
            mCurrentAuthTypeViewPosition = mAuthTypeAdapter.getAuthPosition(settings.authenticationType);
        } else {
            mCurrentAuthTypeViewPosition = savedInstanceState.getInt(STATE_AUTH_TYPE_POSITION);
        }
        mAuthTypeView.setSelection(mCurrentAuthTypeViewPosition, false);
        updateViewFromAuthType();
        if (settings.username != null) {
            mUsernameView.setText(settings.username);
        }
        if (settings.password != null) {
            mPasswordView.setText(settings.password);
        }
        if (settings.clientCertificateAlias != null) {
            mClientCertificateSpinner.setAlias(settings.clientCertificateAlias);
        }
        mStoreType = settings.type;
        if (Type.POP3 == settings.type) {
            serverLabelView.setText(R.string.account_setup_incoming_pop_server_label);
            findViewById(R.id.imap_path_prefix_section).setVisibility(View.GONE);
            findViewById(R.id.webdav_advanced_header).setVisibility(View.GONE);
            findViewById(R.id.webdav_mailbox_alias_section).setVisibility(View.GONE);
            findViewById(R.id.webdav_owa_path_section).setVisibility(View.GONE);
            findViewById(R.id.webdav_auth_path_section).setVisibility(View.GONE);
            findViewById(R.id.compression_section).setVisibility(View.GONE);
            findViewById(R.id.compression_label).setVisibility(View.GONE);
            mSubscribedFoldersOnly.setVisibility(View.GONE);
        } else if (Type.IMAP == settings.type) {
            serverLabelView.setText(R.string.account_setup_incoming_imap_server_label);
            ImapStoreSettings imapSettings = (ImapStoreSettings) settings;
            mImapAutoDetectNamespaceView.setChecked(imapSettings.autoDetectNamespace);
            if (imapSettings.pathPrefix != null) {
                mImapPathPrefixView.setText(imapSettings.pathPrefix);
            }
            findViewById(R.id.webdav_advanced_header).setVisibility(View.GONE);
            findViewById(R.id.webdav_mailbox_alias_section).setVisibility(View.GONE);
            findViewById(R.id.webdav_owa_path_section).setVisibility(View.GONE);
            findViewById(R.id.webdav_auth_path_section).setVisibility(View.GONE);
            if (!editSettings) {
                findViewById(R.id.imap_folder_setup_section).setVisibility(View.GONE);
            }
        } else if (Type.WebDAV == settings.type) {
            serverLabelView.setText(R.string.account_setup_incoming_webdav_server_label);
            mConnectionSecurityChoices = new ConnectionSecurity[] { ConnectionSecurity.NONE, ConnectionSecurity.SSL_TLS_REQUIRED };
            // Hide the unnecessary fields
            findViewById(R.id.imap_path_prefix_section).setVisibility(View.GONE);
            findViewById(R.id.account_auth_type_label).setVisibility(View.GONE);
            findViewById(R.id.account_auth_type).setVisibility(View.GONE);
            findViewById(R.id.compression_section).setVisibility(View.GONE);
            findViewById(R.id.compression_label).setVisibility(View.GONE);
            mSubscribedFoldersOnly.setVisibility(View.GONE);
            WebDavStoreSettings webDavSettings = (WebDavStoreSettings) settings;
            if (webDavSettings.path != null) {
                mWebdavPathPrefixView.setText(webDavSettings.path);
            }
            if (webDavSettings.authPath != null) {
                mWebdavAuthPathView.setText(webDavSettings.authPath);
            }
            if (webDavSettings.mailboxPath != null) {
                mWebdavMailboxPathView.setText(webDavSettings.mailboxPath);
            }
        } else {
            throw new Exception("Unknown account type: " + mAccount.getStoreUri());
        }
        if (!editSettings) {
            mAccount.setDeletePolicy(AccountCreator.getDefaultDeletePolicy(settings.type));
        }
        // Note that mConnectionSecurityChoices is configured above based on server type
        ConnectionSecurityAdapter securityTypesAdapter = ConnectionSecurityAdapter.get(this, mConnectionSecurityChoices);
        mSecurityTypeView.setAdapter(securityTypesAdapter);
        // Select currently configured security type
        if (savedInstanceState == null) {
            mCurrentSecurityTypeViewPosition = securityTypesAdapter.getConnectionSecurityPosition(settings.connectionSecurity);
        } else {
            /*
                 * Restore the spinner state now, before calling
                 * setOnItemSelectedListener(), thus avoiding a call to
                 * onItemSelected(). Then, when the system restores the state
                 * (again) in onRestoreInstanceState(), The system will see that
                 * the new state is the same as the current state (set here), so
                 * once again onItemSelected() will not be called.
                 */
            mCurrentSecurityTypeViewPosition = savedInstanceState.getInt(STATE_SECURITY_TYPE_POSITION);
        }
        mSecurityTypeView.setSelection(mCurrentSecurityTypeViewPosition, false);
        updateAuthPlainTextFromSecurityType(settings.connectionSecurity);
        mCompressionMobile.setChecked(mAccount.useCompression(NetworkType.MOBILE));
        mCompressionWifi.setChecked(mAccount.useCompression(NetworkType.WIFI));
        mCompressionOther.setChecked(mAccount.useCompression(NetworkType.OTHER));
        if (settings.host != null) {
            mServerView.setText(settings.host);
        }
        if (settings.port != -1) {
            mPortView.setText(String.format("%d", settings.port));
        } else {
            updatePortFromSecurityType();
        }
        mCurrentPortViewSetting = mPortView.getText().toString();
        mSubscribedFoldersOnly.setChecked(mAccount.subscribedFoldersOnly());
    } catch (Exception e) {
        failure(e);
    }
}
Also used : WebDavStoreSettings(com.fsck.k9.mail.store.webdav.WebDavStoreSettings) OnCheckedChangeListener(android.widget.CompoundButton.OnCheckedChangeListener) ServerSettings(com.fsck.k9.mail.ServerSettings) ImapStoreSettings(com.fsck.k9.mail.store.imap.ImapStoreSettings) URISyntaxException(java.net.URISyntaxException)

Aggregations

ServerSettings (com.fsck.k9.mail.ServerSettings)45 Test (org.junit.Test)34 URISyntaxException (java.net.URISyntaxException)9 AuthType (com.fsck.k9.mail.AuthType)8 URI (java.net.URI)7 HashMap (java.util.HashMap)5 ConnectionSecurity (com.fsck.k9.mail.ConnectionSecurity)4 InvalidSettingValueException (com.fsck.k9.preferences.Settings.InvalidSettingValueException)2 Map (java.util.Map)2 SharedPreferences (android.content.SharedPreferences)1 OnCheckedChangeListener (android.widget.CompoundButton.OnCheckedChangeListener)1 Account (com.fsck.k9.Account)1 Preferences (com.fsck.k9.Preferences)1 Store (com.fsck.k9.mail.Store)1 RemoteStore (com.fsck.k9.mail.store.RemoteStore)1 StoreConfig (com.fsck.k9.mail.store.StoreConfig)1 ImapStoreSettings (com.fsck.k9.mail.store.imap.ImapStoreSettings)1 WebDavStoreSettings (com.fsck.k9.mail.store.webdav.WebDavStoreSettings)1 SettingsDescription (com.fsck.k9.preferences.Settings.SettingsDescription)1 ArrayList (java.util.ArrayList)1