Search in sources :

Example 6 with Flag

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

the class MessagingController method setFlag.

/**
     * Set or remove a flag for a set of messages in a specific folder.
     * <p>
     * <p>
     * The {@link Message} objects passed in are updated to reflect the new flag state.
     * </p>
     *
     * @param account
     *         The account the folder containing the messages belongs to.
     * @param folderName
     *         The name of the folder.
     * @param messages
     *         The messages to change the flag for.
     * @param flag
     *         The flag to change.
     * @param newState
     *         {@code true}, if the flag should be set. {@code false} if it should be removed.
     */
public void setFlag(Account account, String folderName, List<? extends Message> messages, Flag flag, boolean newState) {
    // TODO: Put this into the background, but right now some callers depend on the message
    //       objects being modified right after this method returns.
    Folder localFolder = null;
    try {
        Store localStore = account.getLocalStore();
        localFolder = localStore.getFolder(folderName);
        localFolder.open(Folder.OPEN_MODE_RW);
        // Allows for re-allowing sending of messages that could not be sent
        if (flag == Flag.FLAGGED && !newState && account.getOutboxFolderName().equals(folderName)) {
            for (Message message : messages) {
                String uid = message.getUid();
                if (uid != null) {
                    sendCount.remove(uid);
                }
            }
        }
        // Update the messages in the local store
        localFolder.setFlags(messages, Collections.singleton(flag), newState);
        int unreadMessageCount = localFolder.getUnreadMessageCount();
        for (MessagingListener l : getListeners()) {
            l.folderStatusChanged(account, folderName, unreadMessageCount);
        }
        // TODO: Skip the remote part for all local-only folders
        if (account.getErrorFolderName().equals(folderName)) {
            return;
        }
        List<String> uids = getUidsFromMessages(messages);
        queueSetFlag(account, folderName, newState, flag, uids);
        processPendingCommands(account);
    } catch (MessagingException me) {
        addErrorMessage(account, null, me);
        throw new RuntimeException(me);
    } finally {
        closeFolder(localFolder);
    }
}
Also used : 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) 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) SuppressLint(android.annotation.SuppressLint)

Example 7 with Flag

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

the class MessagingController method processPendingSetFlag.

/**
     * Processes a pending mark read or unread command.
     */
void processPendingSetFlag(PendingSetFlag command, Account account) throws MessagingException {
    String folder = command.folder;
    if (account.getErrorFolderName().equals(folder) || account.getOutboxFolderName().equals(folder)) {
        return;
    }
    boolean newState = command.newState;
    Flag flag = command.flag;
    Store remoteStore = account.getRemoteStore();
    Folder remoteFolder = remoteStore.getFolder(folder);
    if (!remoteFolder.exists() || !remoteFolder.isFlagSupported(flag)) {
        return;
    }
    try {
        remoteFolder.open(Folder.OPEN_MODE_RW);
        if (remoteFolder.getMode() != Folder.OPEN_MODE_RW) {
            return;
        }
        List<Message> messages = new ArrayList<>();
        for (String uid : command.uids) {
            if (!uid.startsWith(K9.LOCAL_UID_PREFIX)) {
                messages.add(remoteFolder.getMessage(uid));
            }
        }
        if (messages.isEmpty()) {
            return;
        }
        remoteFolder.setFlags(messages, Collections.singleton(flag), newState);
    } finally {
        closeFolder(remoteFolder);
    }
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) 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) PendingSetFlag(com.fsck.k9.controller.MessagingControllerCommands.PendingSetFlag) Flag(com.fsck.k9.mail.Flag)

Example 8 with Flag

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

the class MessagingController method setFlagForThreadsInCache.

private void setFlagForThreadsInCache(final Account account, final List<Long> threadRootIds, final Flag flag, final boolean newState) {
    EmailProviderCache cache = EmailProviderCache.getCache(account.getUuid(), context);
    String columnName = LocalStore.getColumnNameForFlag(flag);
    String value = Integer.toString((newState) ? 1 : 0);
    cache.setValueForThreads(threadRootIds, columnName, value);
}
Also used : EmailProviderCache(com.fsck.k9.cache.EmailProviderCache)

Example 9 with Flag

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

the class MigrationTo60Test method queueSetFlagOld.

OldPendingCommand queueSetFlagOld(String folderName, boolean newState, Flag flag, String uid) {
    OldPendingCommand command = new OldPendingCommand();
    command.command = PENDING_COMMAND_SET_FLAG;
    command.arguments = new String[4];
    command.arguments[0] = folderName;
    command.arguments[1] = uid;
    command.arguments[2] = Boolean.toString(newState);
    command.arguments[3] = flag.toString();
    return command;
}
Also used : OldPendingCommand(com.fsck.k9.mailstore.migrations.MigrationTo60.OldPendingCommand)

Example 10 with Flag

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

the class MessagingController method processPendingAppend.

/**
     * Process a pending append message command. This command uploads a local message to the
     * server, first checking to be sure that the server message is not newer than
     * the local message. Once the local message is successfully processed it is deleted so
     * that the server message will be synchronized down without an additional copy being
     * created.
     * TODO update the local message UID instead of deleting it
     */
void processPendingAppend(PendingAppend command, Account account) throws MessagingException {
    Folder remoteFolder = null;
    LocalFolder localFolder = null;
    try {
        String folder = command.folder;
        String uid = command.uid;
        if (account.getErrorFolderName().equals(folder)) {
            return;
        }
        LocalStore localStore = account.getLocalStore();
        localFolder = localStore.getFolder(folder);
        LocalMessage localMessage = localFolder.getMessage(uid);
        if (localMessage == null) {
            return;
        }
        Store remoteStore = account.getRemoteStore();
        remoteFolder = remoteStore.getFolder(folder);
        if (!remoteFolder.exists()) {
            if (!remoteFolder.create(FolderType.HOLDS_MESSAGES)) {
                return;
            }
        }
        remoteFolder.open(Folder.OPEN_MODE_RW);
        if (remoteFolder.getMode() != Folder.OPEN_MODE_RW) {
            return;
        }
        Message remoteMessage = null;
        if (!localMessage.getUid().startsWith(K9.LOCAL_UID_PREFIX)) {
            remoteMessage = remoteFolder.getMessage(localMessage.getUid());
        }
        if (remoteMessage == null) {
            if (localMessage.isSet(Flag.X_REMOTE_COPY_STARTED)) {
                Timber.w("Local message with uid %s has flag %s  already set, checking for remote message with " + "same message id", localMessage.getUid(), X_REMOTE_COPY_STARTED);
                String rUid = remoteFolder.getUidFromMessageId(localMessage);
                if (rUid != null) {
                    Timber.w("Local message has flag %s already set, and there is a remote message with uid %s, " + "assuming message was already copied and aborting this copy", X_REMOTE_COPY_STARTED, rUid);
                    String oldUid = localMessage.getUid();
                    localMessage.setUid(rUid);
                    localFolder.changeUid(localMessage);
                    for (MessagingListener l : getListeners()) {
                        l.messageUidChanged(account, folder, oldUid, localMessage.getUid());
                    }
                    return;
                } else {
                    Timber.w("No remote message with message-id found, proceeding with append");
                }
            }
            /*
                 * If the message does not exist remotely we just upload it and then
                 * update our local copy with the new uid.
                 */
            FetchProfile fp = new FetchProfile();
            fp.add(FetchProfile.Item.BODY);
            localFolder.fetch(Collections.singletonList(localMessage), fp, null);
            String oldUid = localMessage.getUid();
            localMessage.setFlag(Flag.X_REMOTE_COPY_STARTED, true);
            remoteFolder.appendMessages(Collections.singletonList(localMessage));
            localFolder.changeUid(localMessage);
            for (MessagingListener l : getListeners()) {
                l.messageUidChanged(account, folder, oldUid, localMessage.getUid());
            }
        } else {
            /*
                 * If the remote message exists we need to determine which copy to keep.
                 */
            /*
                 * See if the remote message is newer than ours.
                 */
            FetchProfile fp = new FetchProfile();
            fp.add(FetchProfile.Item.ENVELOPE);
            remoteFolder.fetch(Collections.singletonList(remoteMessage), fp, null);
            Date localDate = localMessage.getInternalDate();
            Date remoteDate = remoteMessage.getInternalDate();
            if (remoteDate != null && remoteDate.compareTo(localDate) > 0) {
                /*
                     * If the remote message is newer than ours we'll just
                     * delete ours and move on. A sync will get the server message
                     * if we need to be able to see it.
                     */
                localMessage.destroy();
            } else {
                /*
                     * Otherwise we'll upload our message and then delete the remote message.
                     */
                fp = new FetchProfile();
                fp.add(FetchProfile.Item.BODY);
                localFolder.fetch(Collections.singletonList(localMessage), fp, null);
                String oldUid = localMessage.getUid();
                localMessage.setFlag(Flag.X_REMOTE_COPY_STARTED, true);
                remoteFolder.appendMessages(Collections.singletonList(localMessage));
                localFolder.changeUid(localMessage);
                for (MessagingListener l : getListeners()) {
                    l.messageUidChanged(account, folder, oldUid, localMessage.getUid());
                }
                if (remoteDate != null) {
                    remoteMessage.setFlag(Flag.DELETED, true);
                    if (Expunge.EXPUNGE_IMMEDIATELY == account.getExpungePolicy()) {
                        remoteFolder.expunge();
                    }
                }
            }
        }
    } 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) Date(java.util.Date)

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