Search in sources :

Example 31 with LocalStore

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

the class MigrationTo43 method fixOutboxFolders.

public static void fixOutboxFolders(SQLiteDatabase db, MigrationsHelper migrationsHelper) {
    try {
        LocalStore localStore = migrationsHelper.getLocalStore();
        Account account = migrationsHelper.getAccount();
        Context context = migrationsHelper.getContext();
        // If folder "OUTBOX" (old, v3.800 - v3.802) exists, rename it to
        // "K9MAIL_INTERNAL_OUTBOX" (new)
        LocalFolder oldOutbox = new LocalFolder(localStore, "OUTBOX");
        if (oldOutbox.exists()) {
            ContentValues cv = new ContentValues();
            cv.put("name", Account.OUTBOX);
            db.update("folders", cv, "name = ?", new String[] { "OUTBOX" });
            Timber.i("Renamed folder OUTBOX to %s", OUTBOX);
        }
        // Check if old (pre v3.800) localized outbox folder exists
        String localizedOutbox = context.getString(R.string.special_mailbox_name_outbox);
        LocalFolder obsoleteOutbox = new LocalFolder(localStore, localizedOutbox);
        if (obsoleteOutbox.exists()) {
            // Get all messages from the localized outbox ...
            List<? extends Message> messages = obsoleteOutbox.getMessages(null, false);
            if (messages.size() > 0) {
                // ... and move them to the drafts folder (we don't want to
                // surprise the user by sending potentially very old messages)
                LocalFolder drafts = new LocalFolder(localStore, account.getDraftsFolderName());
                obsoleteOutbox.moveMessages(messages, drafts);
            }
            // Now get rid of the localized outbox
            obsoleteOutbox.delete();
            obsoleteOutbox.delete(true);
        }
    } catch (Exception e) {
        Timber.e(e, "Error trying to fix the outbox folders");
    }
}
Also used : Context(android.content.Context) LocalFolder(com.fsck.k9.mailstore.LocalFolder) ContentValues(android.content.ContentValues) Account(com.fsck.k9.Account) LocalStore(com.fsck.k9.mailstore.LocalStore)

Example 32 with LocalStore

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

the class MessagingController method checkMailForAccount.

private void checkMailForAccount(final Context context, final Account account, final boolean ignoreLastCheckedTime, final MessagingListener listener) {
    if (!account.isAvailable(context)) {
        Timber.i("Skipping synchronizing unavailable account %s", account.getDescription());
        return;
    }
    final long accountInterval = account.getAutomaticCheckIntervalMinutes() * 60 * 1000;
    if (!ignoreLastCheckedTime && accountInterval <= 0) {
        Timber.i("Skipping synchronizing account %s", account.getDescription());
        return;
    }
    Timber.i("Synchronizing account %s", account.getDescription());
    account.setRingNotified(false);
    sendPendingMessages(account, listener);
    try {
        Account.FolderMode aDisplayMode = account.getFolderDisplayMode();
        Account.FolderMode aSyncMode = account.getFolderSyncMode();
        Store localStore = account.getLocalStore();
        for (final Folder folder : localStore.getPersonalNamespaces(false)) {
            folder.open(Folder.OPEN_MODE_RW);
            Folder.FolderClass fDisplayClass = folder.getDisplayClass();
            Folder.FolderClass fSyncClass = folder.getSyncClass();
            if (modeMismatch(aDisplayMode, fDisplayClass)) {
                continue;
            }
            if (modeMismatch(aSyncMode, fSyncClass)) {
                continue;
            }
            synchronizeFolder(account, folder, ignoreLastCheckedTime, accountInterval, listener);
        }
    } catch (MessagingException e) {
        Timber.e(e, "Unable to synchronize account %s", account.getName());
        addErrorMessage(account, null, e);
    } finally {
        putBackground("clear notification flag for " + account.getDescription(), null, new Runnable() {

            @Override
            public void run() {
                Timber.v("Clearing notification flag for %s", account.getDescription());
                account.setRingNotified(false);
                try {
                    AccountStats stats = account.getStats(context);
                    if (stats == null || stats.unreadMessageCount == 0) {
                        notificationController.clearNewMailNotifications(account);
                    }
                } catch (MessagingException e) {
                    Timber.e(e, "Unable to getUnreadMessageCount for account: %s", account);
                }
            }
        });
    }
}
Also used : SearchAccount(com.fsck.k9.search.SearchAccount) Account(com.fsck.k9.Account) MessagingException(com.fsck.k9.mail.MessagingException) LocalStore(com.fsck.k9.mailstore.LocalStore) Store(com.fsck.k9.mail.Store) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) AccountStats(com.fsck.k9.AccountStats)

Example 33 with LocalStore

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

the class MessagingController method isMoveCapable.

public boolean isMoveCapable(final Account account) {
    try {
        Store localStore = account.getLocalStore();
        Store remoteStore = account.getRemoteStore();
        return localStore.isMoveCapable() && remoteStore.isMoveCapable();
    } catch (MessagingException me) {
        Timber.e(me, "Exception while ascertaining move capability");
        return false;
    }
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) LocalStore(com.fsck.k9.mailstore.LocalStore) Store(com.fsck.k9.mail.Store) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store)

Example 34 with LocalStore

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

the class MessagingController method moveOrDeleteSentMessage.

private void moveOrDeleteSentMessage(Account account, LocalStore localStore, LocalFolder localFolder, LocalMessage message) throws MessagingException {
    if (!account.hasSentFolder()) {
        Timber.i("Account does not have a sent mail folder; deleting sent message");
        message.setFlag(Flag.DELETED, true);
    } else {
        LocalFolder localSentFolder = localStore.getFolder(account.getSentFolderName());
        Timber.i("Moving sent message to folder '%s' (%d)", account.getSentFolderName(), localSentFolder.getId());
        localFolder.moveMessages(Collections.singletonList(message), localSentFolder);
        Timber.i("Moved sent message to folder '%s' (%d)", account.getSentFolderName(), localSentFolder.getId());
        PendingCommand command = PendingAppend.create(localSentFolder.getName(), message.getUid());
        queuePendingCommand(account, command);
        processPendingCommands(account);
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) PendingCommand(com.fsck.k9.controller.MessagingControllerCommands.PendingCommand)

Example 35 with LocalStore

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

the class MessagingController method setupPushing.

public boolean setupPushing(final Account account) {
    try {
        Pusher previousPusher = pushers.remove(account);
        if (previousPusher != null) {
            previousPusher.stop();
        }
        Account.FolderMode aDisplayMode = account.getFolderDisplayMode();
        Account.FolderMode aPushMode = account.getFolderPushMode();
        List<String> names = new ArrayList<>();
        Store localStore = account.getLocalStore();
        for (final Folder folder : localStore.getPersonalNamespaces(false)) {
            if (folder.getName().equals(account.getErrorFolderName()) || folder.getName().equals(account.getOutboxFolderName())) {
                continue;
            }
            folder.open(Folder.OPEN_MODE_RW);
            Folder.FolderClass fDisplayClass = folder.getDisplayClass();
            Folder.FolderClass fPushClass = folder.getPushClass();
            if (modeMismatch(aDisplayMode, fDisplayClass)) {
                continue;
            }
            if (modeMismatch(aPushMode, fPushClass)) {
                continue;
            }
            Timber.i("Starting pusher for %s:%s", account.getDescription(), folder.getName());
            names.add(folder.getName());
        }
        if (!names.isEmpty()) {
            PushReceiver receiver = new MessagingControllerPushReceiver(context, account, this);
            int maxPushFolders = account.getMaxPushFolders();
            if (names.size() > maxPushFolders) {
                Timber.i("Count of folders to push for account %s is %d, greater than limit of %d, truncating", account.getDescription(), names.size(), maxPushFolders);
                names = names.subList(0, maxPushFolders);
            }
            try {
                Store store = account.getRemoteStore();
                if (!store.isPushCapable()) {
                    Timber.i("Account %s is not push capable, skipping", account.getDescription());
                    return false;
                }
                Pusher pusher = store.getPusher(receiver);
                if (pusher != null) {
                    Pusher oldPusher = pushers.putIfAbsent(account, pusher);
                    if (oldPusher == null) {
                        pusher.start(names);
                    }
                }
            } catch (Exception e) {
                Timber.e(e, "Could not get remote store");
                return false;
            }
            return true;
        } else {
            Timber.i("No folders are configured for pushing in account %s", account.getDescription());
            return false;
        }
    } catch (Exception e) {
        Timber.e(e, "Got exception while setting up pushing");
    }
    return false;
}
Also used : SearchAccount(com.fsck.k9.search.SearchAccount) Account(com.fsck.k9.Account) ArrayList(java.util.ArrayList) LocalStore(com.fsck.k9.mailstore.LocalStore) Store(com.fsck.k9.mail.Store) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) Pusher(com.fsck.k9.mail.Pusher) SuppressLint(android.annotation.SuppressLint) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException) PushReceiver(com.fsck.k9.mail.PushReceiver)

Aggregations

LocalStore (com.fsck.k9.mailstore.LocalStore)41 MessagingException (com.fsck.k9.mail.MessagingException)33 LocalFolder (com.fsck.k9.mailstore.LocalFolder)33 Store (com.fsck.k9.mail.Store)18 LocalMessage (com.fsck.k9.mailstore.LocalMessage)18 Folder (com.fsck.k9.mail.Folder)17 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)17 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)15 FetchProfile (com.fsck.k9.mail.FetchProfile)14 Message (com.fsck.k9.mail.Message)13 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)13 IOException (java.io.IOException)12 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)11 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)11 Test (org.junit.Test)10 Account (com.fsck.k9.Account)9 ArrayList (java.util.ArrayList)9 SuppressLint (android.annotation.SuppressLint)8 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)8 VisibleForTesting (android.support.annotation.VisibleForTesting)5