Search in sources :

Example 21 with Flag

use of com.fsck.k9.mail.Flag 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 22 with Flag

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

the class MessagingController method searchRemoteMessagesSynchronous.

@VisibleForTesting
void searchRemoteMessagesSynchronous(final String acctUuid, final String folderName, final String query, final Set<Flag> requiredFlags, final Set<Flag> forbiddenFlags, final MessagingListener listener) {
    final Account acct = Preferences.getPreferences(context).getAccount(acctUuid);
    if (listener != null) {
        listener.remoteSearchStarted(folderName);
    }
    List<Message> extraResults = new ArrayList<>();
    try {
        Store remoteStore = acct.getRemoteStore();
        LocalStore localStore = acct.getLocalStore();
        if (remoteStore == null || localStore == null) {
            throw new MessagingException("Could not get store");
        }
        Folder remoteFolder = remoteStore.getFolder(folderName);
        LocalFolder localFolder = localStore.getFolder(folderName);
        if (remoteFolder == null || localFolder == null) {
            throw new MessagingException("Folder not found");
        }
        List<Message> messages = remoteFolder.search(query, requiredFlags, forbiddenFlags);
        Timber.i("Remote search got %d results", messages.size());
        // There's no need to fetch messages already completely downloaded
        List<Message> remoteMessages = localFolder.extractNewMessages(messages);
        messages.clear();
        if (listener != null) {
            listener.remoteSearchServerQueryComplete(folderName, remoteMessages.size(), acct.getRemoteSearchNumResults());
        }
        Collections.sort(remoteMessages, new UidReverseComparator());
        int resultLimit = acct.getRemoteSearchNumResults();
        if (resultLimit > 0 && remoteMessages.size() > resultLimit) {
            extraResults = remoteMessages.subList(resultLimit, remoteMessages.size());
            remoteMessages = remoteMessages.subList(0, resultLimit);
        }
        loadSearchResultsSynchronous(remoteMessages, localFolder, remoteFolder, listener);
    } catch (Exception e) {
        if (Thread.currentThread().isInterrupted()) {
            Timber.i(e, "Caught exception on aborted remote search; safe to ignore.");
        } else {
            Timber.e(e, "Could not complete remote search");
            if (listener != null) {
                listener.remoteSearchFailed(null, e.getMessage());
            }
            addErrorMessage(acct, null, e);
        }
    } finally {
        if (listener != null) {
            listener.remoteSearchFinished(folderName, 0, acct.getRemoteSearchNumResults(), extraResults);
        }
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) SearchAccount(com.fsck.k9.search.SearchAccount) Account(com.fsck.k9.Account) LocalMessage(com.fsck.k9.mailstore.LocalMessage) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MessagingException(com.fsck.k9.mail.MessagingException) ArrayList(java.util.ArrayList) LocalStore(com.fsck.k9.mailstore.LocalStore) Store(com.fsck.k9.mail.Store) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) LocalStore(com.fsck.k9.mailstore.LocalStore) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) 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) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 23 with Flag

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

the class MessagingController method loadMessageRemoteSynchronous.

private boolean loadMessageRemoteSynchronous(final Account account, final String folder, final String uid, final MessagingListener listener, final boolean loadPartialFromSearch) {
    Folder remoteFolder = null;
    LocalFolder localFolder = null;
    try {
        LocalStore localStore = account.getLocalStore();
        localFolder = localStore.getFolder(folder);
        localFolder.open(Folder.OPEN_MODE_RW);
        LocalMessage message = localFolder.getMessage(uid);
        if (uid.startsWith(K9.LOCAL_UID_PREFIX)) {
            Timber.w("Message has local UID so cannot download fully.");
            // ASH move toast
            android.widget.Toast.makeText(context, "Message has local UID so cannot download fully", android.widget.Toast.LENGTH_LONG).show();
            // TODO: Using X_DOWNLOADED_FULL is wrong because it's only a partial message. But
            // one we can't download completely. Maybe add a new flag; X_PARTIAL_MESSAGE ?
            message.setFlag(Flag.X_DOWNLOADED_FULL, true);
            message.setFlag(Flag.X_DOWNLOADED_PARTIAL, false);
        }
        /* commented out because this was pulled from another unmerged branch:
            } else if (localFolder.isLocalOnly() && !force) {
                Log.w(K9.LOG_TAG, "Message in local-only folder so cannot download fully.");
                // ASH move toast
                android.widget.Toast.makeText(mApplication,
                        "Message in local-only folder so cannot download fully",
                        android.widget.Toast.LENGTH_LONG).show();
                message.setFlag(Flag.X_DOWNLOADED_FULL, true);
                message.setFlag(Flag.X_DOWNLOADED_PARTIAL, false);
            }*/
        /*if (!message.isSet(Flag.X_DOWNLOADED_FULL)) */
        {
            /*
                 * At this point the message is not available, so we need to download it
                 * fully if possible.
                 */
            Store remoteStore = account.getRemoteStore();
            remoteFolder = remoteStore.getFolder(folder);
            remoteFolder.open(Folder.OPEN_MODE_RW);
            // Get the remote message and fully download it
            Message remoteMessage = remoteFolder.getMessage(uid);
            if (loadPartialFromSearch) {
                downloadMessages(account, remoteFolder, localFolder, Collections.singletonList(remoteMessage), false, false);
            } else {
                FetchProfile fp = new FetchProfile();
                fp.add(FetchProfile.Item.BODY);
                remoteFolder.fetch(Collections.singletonList(remoteMessage), fp, null);
                localFolder.appendMessages(Collections.singletonList(remoteMessage));
            }
            message = localFolder.getMessage(uid);
            if (!loadPartialFromSearch) {
                message.setFlag(Flag.X_DOWNLOADED_FULL, true);
            }
        }
        // Mark that this message is now fully synched
        if (account.isMarkMessageAsReadOnView()) {
            message.setFlag(Flag.SEEN, true);
        }
        // now that we have the full message, refresh the headers
        for (MessagingListener l : getListeners(listener)) {
            l.loadMessageRemoteFinished(account, folder, uid);
        }
        return true;
    } catch (Exception e) {
        for (MessagingListener l : getListeners(listener)) {
            l.loadMessageRemoteFailed(account, folder, uid, e);
        }
        notifyUserIfCertificateProblem(account, e, true);
        addErrorMessage(account, null, e);
        return false;
    } finally {
        closeFolder(remoteFolder);
        closeFolder(localFolder);
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) LocalMessage(com.fsck.k9.mailstore.LocalMessage) FetchProfile(com.fsck.k9.mail.FetchProfile) LocalMessage(com.fsck.k9.mailstore.LocalMessage) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) LocalStore(com.fsck.k9.mailstore.LocalStore) Store(com.fsck.k9.mail.Store) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) LocalStore(com.fsck.k9.mailstore.LocalStore) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) 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)

Example 24 with Flag

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

the class MessageListFragment method setFlagForSelected.

private void setFlagForSelected(final Flag flag, final boolean newState) {
    if (selected.isEmpty()) {
        return;
    }
    Map<Account, List<Long>> messageMap = new HashMap<>();
    Map<Account, List<Long>> threadMap = new HashMap<>();
    Set<Account> accounts = new HashSet<>();
    for (int position = 0, end = adapter.getCount(); position < end; position++) {
        Cursor cursor = (Cursor) adapter.getItem(position);
        long uniqueId = cursor.getLong(uniqueIdColumn);
        if (selected.contains(uniqueId)) {
            String uuid = cursor.getString(ACCOUNT_UUID_COLUMN);
            Account account = preferences.getAccount(uuid);
            accounts.add(account);
            if (showingThreadedList && cursor.getInt(THREAD_COUNT_COLUMN) > 1) {
                List<Long> threadRootIdList = threadMap.get(account);
                if (threadRootIdList == null) {
                    threadRootIdList = new ArrayList<>();
                    threadMap.put(account, threadRootIdList);
                }
                threadRootIdList.add(cursor.getLong(THREAD_ROOT_COLUMN));
            } else {
                List<Long> messageIdList = messageMap.get(account);
                if (messageIdList == null) {
                    messageIdList = new ArrayList<>();
                    messageMap.put(account, messageIdList);
                }
                messageIdList.add(cursor.getLong(ID_COLUMN));
            }
        }
    }
    for (Account account : accounts) {
        List<Long> messageIds = messageMap.get(account);
        List<Long> threadRootIds = threadMap.get(account);
        if (messageIds != null) {
            messagingController.setFlag(account, messageIds, flag, newState);
        }
        if (threadRootIds != null) {
            messagingController.setFlagForThreads(account, threadRootIds, flag, newState);
        }
    }
    computeBatchDirection();
}
Also used : Account(com.fsck.k9.Account) HashMap(java.util.HashMap) Cursor(android.database.Cursor) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 25 with Flag

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

the class MigrationTo60Test method migrateSetFlag.

@Test
public void migrateSetFlag() {
    OldPendingCommand command = queueSetFlagBulk(SOURCE_FOLDER, FLAG_STATE, FLAG, UID_ARRAY);
    PendingSetFlag pendingCommand = (PendingSetFlag) MigrationTo60.migratePendingCommand(command);
    assertEquals(SOURCE_FOLDER, pendingCommand.folder);
    assertEquals(FLAG_STATE, pendingCommand.newState);
    assertEquals(FLAG, pendingCommand.flag);
    assertEquals(asList(UID_ARRAY), pendingCommand.uids);
}
Also used : PendingSetFlag(com.fsck.k9.controller.MessagingControllerCommands.PendingSetFlag) OldPendingCommand(com.fsck.k9.mailstore.migrations.MigrationTo60.OldPendingCommand) Test(org.junit.Test)

Aggregations

MessagingException (com.fsck.k9.mail.MessagingException)10 Message (com.fsck.k9.mail.Message)9 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)9 LocalMessage (com.fsck.k9.mailstore.LocalMessage)9 Flag (com.fsck.k9.mail.Flag)8 LocalFolder (com.fsck.k9.mailstore.LocalFolder)8 LocalStore (com.fsck.k9.mailstore.LocalStore)8 ArrayList (java.util.ArrayList)8 Folder (com.fsck.k9.mail.Folder)7 PendingSetFlag (com.fsck.k9.controller.MessagingControllerCommands.PendingSetFlag)6 Store (com.fsck.k9.mail.Store)6 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)6 Account (com.fsck.k9.Account)5 IOException (java.io.IOException)5 SuppressLint (android.annotation.SuppressLint)4 Cursor (android.database.Cursor)4 EmailProviderCache (com.fsck.k9.cache.EmailProviderCache)4 FetchProfile (com.fsck.k9.mail.FetchProfile)4 OldPendingCommand (com.fsck.k9.mailstore.migrations.MigrationTo60.OldPendingCommand)4 Date (java.util.Date)4