Search in sources :

Example 16 with Store

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

the class MessagingController method downloadPartial.

private void downloadPartial(Folder remoteFolder, LocalFolder localFolder, Message message) throws MessagingException {
    /*
         * We have a structure to deal with, from which
         * we can pull down the parts we want to actually store.
         * Build a list of parts we are interested in. Text parts will be downloaded
         * right now, attachments will be left for later.
         */
    Set<Part> viewables = MessageExtractor.collectTextParts(message);
    /*
         * Now download the parts we're interested in storing.
         */
    for (Part part : viewables) {
        remoteFolder.fetchPart(message, part, null);
    }
    // Store the updated message locally
    localFolder.appendMessages(Collections.singletonList(message));
    Message localMessage = localFolder.getMessage(message.getUid());
    // Set a flag indicating this message has been fully downloaded and can be
    // viewed.
    localMessage.setFlag(Flag.X_DOWNLOADED_PARTIAL, true);
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) Part(com.fsck.k9.mail.Part)

Example 17 with Store

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

the class FolderSettings method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String folderName = (String) getIntent().getSerializableExtra(EXTRA_FOLDER_NAME);
    String accountUuid = getIntent().getStringExtra(EXTRA_ACCOUNT);
    Account mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
    try {
        LocalStore localStore = mAccount.getLocalStore();
        mFolder = localStore.getFolder(folderName);
        mFolder.open(Folder.OPEN_MODE_RW);
    } catch (MessagingException me) {
        Timber.e(me, "Unable to edit folder %s preferences", folderName);
        return;
    }
    boolean isPushCapable = false;
    try {
        Store store = mAccount.getRemoteStore();
        isPushCapable = store.isPushCapable();
    } catch (Exception e) {
        Timber.e(e, "Could not get remote store");
    }
    addPreferencesFromResource(R.xml.folder_settings_preferences);
    String displayName = FolderInfoHolder.getDisplayName(this, mAccount, mFolder.getName());
    Preference category = findPreference(PREFERENCE_TOP_CATERGORY);
    category.setTitle(displayName);
    mInTopGroup = (CheckBoxPreference) findPreference(PREFERENCE_IN_TOP_GROUP);
    mInTopGroup.setChecked(mFolder.isInTopGroup());
    mIntegrate = (CheckBoxPreference) findPreference(PREFERENCE_INTEGRATE);
    mIntegrate.setChecked(mFolder.isIntegrate());
    mDisplayClass = (ListPreference) findPreference(PREFERENCE_DISPLAY_CLASS);
    mDisplayClass.setValue(mFolder.getDisplayClass().name());
    mDisplayClass.setSummary(mDisplayClass.getEntry());
    mDisplayClass.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        public boolean onPreferenceChange(Preference preference, Object newValue) {
            final String summary = newValue.toString();
            int index = mDisplayClass.findIndexOfValue(summary);
            mDisplayClass.setSummary(mDisplayClass.getEntries()[index]);
            mDisplayClass.setValue(summary);
            return false;
        }
    });
    mSyncClass = (ListPreference) findPreference(PREFERENCE_SYNC_CLASS);
    mSyncClass.setValue(mFolder.getRawSyncClass().name());
    mSyncClass.setSummary(mSyncClass.getEntry());
    mSyncClass.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        public boolean onPreferenceChange(Preference preference, Object newValue) {
            final String summary = newValue.toString();
            int index = mSyncClass.findIndexOfValue(summary);
            mSyncClass.setSummary(mSyncClass.getEntries()[index]);
            mSyncClass.setValue(summary);
            return false;
        }
    });
    mPushClass = (ListPreference) findPreference(PREFERENCE_PUSH_CLASS);
    mPushClass.setEnabled(isPushCapable);
    mPushClass.setValue(mFolder.getRawPushClass().name());
    mPushClass.setSummary(mPushClass.getEntry());
    mPushClass.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        public boolean onPreferenceChange(Preference preference, Object newValue) {
            final String summary = newValue.toString();
            int index = mPushClass.findIndexOfValue(summary);
            mPushClass.setSummary(mPushClass.getEntries()[index]);
            mPushClass.setValue(summary);
            return false;
        }
    });
    mNotifyClass = (ListPreference) findPreference(PREFERENCE_NOTIFY_CLASS);
    mNotifyClass.setValue(mFolder.getRawNotifyClass().name());
    mNotifyClass.setSummary(mNotifyClass.getEntry());
    mNotifyClass.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        public boolean onPreferenceChange(Preference preference, Object newValue) {
            final String summary = newValue.toString();
            int index = mNotifyClass.findIndexOfValue(summary);
            mNotifyClass.setSummary(mNotifyClass.getEntries()[index]);
            mNotifyClass.setValue(summary);
            return false;
        }
    });
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) CheckBoxPreference(android.preference.CheckBoxPreference) ListPreference(android.preference.ListPreference) Preference(android.preference.Preference) Store(com.fsck.k9.mail.Store) LocalStore(com.fsck.k9.mailstore.LocalStore) LocalStore(com.fsck.k9.mailstore.LocalStore) MessagingException(com.fsck.k9.mail.MessagingException)

Example 18 with Store

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

the class WebDavStore method getHttpClient.

public WebDavHttpClient getHttpClient() throws MessagingException {
    if (httpClient == null) {
        httpClient = httpClientFactory.create();
        // Disable automatic redirects on the http client.
        httpClient.getParams().setBooleanParameter("http.protocol.handle-redirects", false);
        // Setup a cookie store for forms-based authentication.
        httpContext = new BasicHttpContext();
        authCookies = new BasicCookieStore();
        httpContext.setAttribute(ClientContext.COOKIE_STORE, authCookies);
        SchemeRegistry reg = httpClient.getConnectionManager().getSchemeRegistry();
        try {
            Scheme s = new Scheme("https", new WebDavSocketFactory(hostname, 443), 443);
            reg.register(s);
        } catch (NoSuchAlgorithmException nsa) {
            Log.e(LOG_TAG, "NoSuchAlgorithmException in getHttpClient: " + nsa);
            throw new MessagingException("NoSuchAlgorithmException in getHttpClient: " + nsa);
        } catch (KeyManagementException kme) {
            Log.e(LOG_TAG, "KeyManagementException in getHttpClient: " + kme);
            throw new MessagingException("KeyManagementException in getHttpClient: " + kme);
        }
    }
    return httpClient;
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Scheme(org.apache.http.conn.scheme.Scheme) MessagingException(com.fsck.k9.mail.MessagingException) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException)

Example 19 with Store

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

the class ImapStoreUriDecoder method decode.

/**
     * Decodes an ImapStore URI.
     *
     * <p>Possible forms:</p>
     * <pre>
     * imap://auth:user:password@server:port ConnectionSecurity.NONE
     * imap+tls+://auth:user:password@server:port ConnectionSecurity.STARTTLS_REQUIRED
     * imap+ssl+://auth:user:password@server:port ConnectionSecurity.SSL_TLS_REQUIRED
     * </pre>
     *
     * NOTE: this method expects the userinfo part of the uri to be encoded twice, due to a bug in
     * {@link ImapStoreUriCreator#create(ServerSettings)}.
     *
     * @param uri the store uri.
     */
public static ImapStoreSettings decode(String uri) {
    String host;
    int port;
    ConnectionSecurity connectionSecurity;
    AuthType authenticationType = null;
    String username = null;
    String password = null;
    String clientCertificateAlias = null;
    String pathPrefix = null;
    boolean autoDetectNamespace = true;
    URI imapUri;
    try {
        imapUri = new URI(uri);
    } catch (URISyntaxException use) {
        throw new IllegalArgumentException("Invalid ImapStore URI", use);
    }
    String scheme = imapUri.getScheme();
    /*
         * Currently available schemes are:
         * imap
         * imap+tls+
         * imap+ssl+
         *
         * The following are obsolete schemes that may be found in pre-existing
         * settings from earlier versions or that may be found when imported. We
         * continue to recognize them and re-map them appropriately:
         * imap+tls
         * imap+ssl
         */
    if (scheme.equals("imap")) {
        connectionSecurity = ConnectionSecurity.NONE;
        port = Type.IMAP.defaultPort;
    } else if (scheme.startsWith("imap+tls")) {
        connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED;
        port = Type.IMAP.defaultPort;
    } else if (scheme.startsWith("imap+ssl")) {
        connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED;
        port = Type.IMAP.defaultTlsPort;
    } else {
        throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")");
    }
    host = imapUri.getHost();
    if (imapUri.getPort() != -1) {
        port = imapUri.getPort();
    }
    if (imapUri.getUserInfo() != null) {
        String userinfo = imapUri.getUserInfo();
        String[] userInfoParts = userinfo.split(":");
        if (userinfo.endsWith(":")) {
            // Or XOAUTH2 where it's a valid config - XOAUTH:username:
            if (userInfoParts.length > 1) {
                authenticationType = AuthType.valueOf(userInfoParts[0]);
                username = decodeUtf8(userInfoParts[1]);
            } else {
                authenticationType = AuthType.PLAIN;
                username = decodeUtf8(userInfoParts[0]);
            }
        } else if (userInfoParts.length == 2) {
            // Old/standard style of encoding - PLAIN auth only:
            // username:password
            authenticationType = AuthType.PLAIN;
            username = decodeUtf8(userInfoParts[0]);
            password = decodeUtf8(userInfoParts[1]);
        } else if (userInfoParts.length == 3) {
            // Standard encoding
            // PLAIN:username:password
            // EXTERNAL:username:certAlias
            authenticationType = AuthType.valueOf(userInfoParts[0]);
            username = decodeUtf8(userInfoParts[1]);
            if (AuthType.EXTERNAL == authenticationType) {
                clientCertificateAlias = decodeUtf8(userInfoParts[2]);
            } else {
                password = decodeUtf8(userInfoParts[2]);
            }
        }
    }
    String path = imapUri.getPath();
    if (path != null && path.length() > 1) {
        // Strip off the leading "/"
        String cleanPath = path.substring(1);
        if (cleanPath.length() >= 2 && cleanPath.charAt(1) == '|') {
            autoDetectNamespace = cleanPath.charAt(0) == '1';
            if (!autoDetectNamespace) {
                pathPrefix = cleanPath.substring(2);
            }
        } else {
            if (cleanPath.length() > 0) {
                pathPrefix = cleanPath;
                autoDetectNamespace = false;
            }
        }
    }
    return new ImapStoreSettings(host, port, connectionSecurity, authenticationType, username, password, clientCertificateAlias, autoDetectNamespace, pathPrefix);
}
Also used : ConnectionSecurity(com.fsck.k9.mail.ConnectionSecurity) AuthType(com.fsck.k9.mail.AuthType) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 20 with Store

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

the class RemoteStore method getInstance.

/**
     * Get an instance of a remote mail store.
     */
public static synchronized Store getInstance(Context context, StoreConfig storeConfig) throws MessagingException {
    String uri = storeConfig.getStoreUri();
    if (uri.startsWith("local")) {
        throw new RuntimeException("Asked to get non-local Store object but given LocalStore URI");
    }
    Store store = sStores.get(uri);
    if (store == null) {
        if (uri.startsWith("imap")) {
            OAuth2TokenProvider oAuth2TokenProvider = null;
            store = new ImapStore(storeConfig, new DefaultTrustedSocketFactory(context), (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE), oAuth2TokenProvider);
        } else if (uri.startsWith("pop3")) {
            store = new Pop3Store(storeConfig, new DefaultTrustedSocketFactory(context));
        } else if (uri.startsWith("webdav")) {
            store = new WebDavStore(storeConfig, new WebDavHttpClient.WebDavHttpClientFactory());
        }
        if (store != null) {
            sStores.put(uri, store);
        }
    }
    if (store == null) {
        throw new MessagingException("Unable to locate an applicable Store for " + uri);
    }
    return store;
}
Also used : Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) DefaultTrustedSocketFactory(com.fsck.k9.mail.ssl.DefaultTrustedSocketFactory) MessagingException(com.fsck.k9.mail.MessagingException) OAuth2TokenProvider(com.fsck.k9.mail.oauth.OAuth2TokenProvider) ConnectivityManager(android.net.ConnectivityManager) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) Store(com.fsck.k9.mail.Store) WebDavStore(com.fsck.k9.mail.store.webdav.WebDavStore) ImapStore(com.fsck.k9.mail.store.imap.ImapStore) ImapStore(com.fsck.k9.mail.store.imap.ImapStore) WebDavStore(com.fsck.k9.mail.store.webdav.WebDavStore)

Aggregations

Store (com.fsck.k9.mail.Store)24 MessagingException (com.fsck.k9.mail.MessagingException)23 LocalStore (com.fsck.k9.mailstore.LocalStore)21 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)20 LocalFolder (com.fsck.k9.mailstore.LocalFolder)19 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)17 Folder (com.fsck.k9.mail.Folder)16 Message (com.fsck.k9.mail.Message)16 LocalMessage (com.fsck.k9.mailstore.LocalMessage)16 ArrayList (java.util.ArrayList)10 SuppressLint (android.annotation.SuppressLint)7 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)7 IOException (java.io.IOException)6 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)5 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)5 FetchProfile (com.fsck.k9.mail.FetchProfile)5 Date (java.util.Date)5 Account (com.fsck.k9.Account)4 VisibleForTesting (android.support.annotation.VisibleForTesting)3 SearchAccount (com.fsck.k9.search.SearchAccount)3