Search in sources :

Example 6 with K9

use of com.fsck.k9.K9 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 7 with K9

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

the class ContactPicture method getContactPictureLoader.

public static ContactPictureLoader getContactPictureLoader(Context context) {
    final int defaultBgColor;
    if (!K9.isColorizeMissingContactPictures()) {
        TypedValue outValue = new TypedValue();
        context.getTheme().resolveAttribute(R.attr.contactPictureFallbackDefaultBackgroundColor, outValue, true);
        defaultBgColor = outValue.data;
    } else {
        defaultBgColor = 0;
    }
    return new ContactPictureLoader(context, defaultBgColor);
}
Also used : ContactPictureLoader(com.fsck.k9.activity.misc.ContactPictureLoader) TypedValue(android.util.TypedValue)

Example 8 with K9

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

the class MessageListFragment method changeSort.

/**
     * Change the sort type and sort order used for the message list.
     *
     * @param sortType
     *         Specifies which field to use for sorting the message list.
     * @param sortAscending
     *         Specifies the sort order. If this argument is {@code null} the default search order
     *         for the sort type is used.
     */
// FIXME: Don't save the changes in the UI thread
private void changeSort(SortType sortType, Boolean sortAscending) {
    this.sortType = sortType;
    Account account = this.account;
    if (account != null) {
        account.setSortType(this.sortType);
        if (sortAscending == null) {
            this.sortAscending = account.isSortAscending(this.sortType);
        } else {
            this.sortAscending = sortAscending;
        }
        account.setSortAscending(this.sortType, this.sortAscending);
        sortDateAscending = account.isSortAscending(SortType.SORT_DATE);
        account.save(preferences);
    } else {
        K9.setSortType(this.sortType);
        if (sortAscending == null) {
            this.sortAscending = K9.isSortAscending(this.sortType);
        } else {
            this.sortAscending = sortAscending;
        }
        K9.setSortAscending(this.sortType, this.sortAscending);
        sortDateAscending = K9.isSortAscending(SortType.SORT_DATE);
        StorageEditor editor = preferences.getStorage().edit();
        K9.save(editor);
        editor.commit();
    }
    reSort();
}
Also used : Account(com.fsck.k9.Account) StorageEditor(com.fsck.k9.preferences.StorageEditor)

Example 9 with K9

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

the class LocalFolder method moveMessages.

@Override
public Map<String, String> moveMessages(final List<? extends Message> msgs, final Folder destFolder) throws MessagingException {
    if (!(destFolder instanceof LocalFolder)) {
        throw new MessagingException("moveMessages called with non-LocalFolder");
    }
    final LocalFolder lDestFolder = (LocalFolder) destFolder;
    final Map<String, String> uidMap = new HashMap<>();
    try {
        this.localStore.database.execute(false, new DbCallback<Void>() {

            @Override
            public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
                try {
                    lDestFolder.open(OPEN_MODE_RW);
                    for (Message message : msgs) {
                        LocalMessage lMessage = (LocalMessage) message;
                        String oldUID = message.getUid();
                        Timber.d("Updating folder_id to %s for message with UID %s, " + "id %d currently in folder %s", lDestFolder.getId(), message.getUid(), lMessage.getId(), getName());
                        String newUid = K9.LOCAL_UID_PREFIX + UUID.randomUUID().toString();
                        message.setUid(newUid);
                        uidMap.put(oldUID, newUid);
                        // Message threading in the target folder
                        ThreadInfo threadInfo = lDestFolder.doMessageThreading(db, message);
                        /*
                             * "Move" the message into the new folder
                             */
                        long msgId = lMessage.getId();
                        String[] idArg = new String[] { Long.toString(msgId) };
                        ContentValues cv = new ContentValues();
                        cv.put("folder_id", lDestFolder.getId());
                        cv.put("uid", newUid);
                        db.update("messages", cv, "id = ?", idArg);
                        // Create/update entry in 'threads' table for the message in the
                        // target folder
                        cv.clear();
                        cv.put("message_id", msgId);
                        if (threadInfo.threadId == -1) {
                            if (threadInfo.rootId != -1) {
                                cv.put("root", threadInfo.rootId);
                            }
                            if (threadInfo.parentId != -1) {
                                cv.put("parent", threadInfo.parentId);
                            }
                            db.insert("threads", null, cv);
                        } else {
                            db.update("threads", cv, "id = ?", new String[] { Long.toString(threadInfo.threadId) });
                        }
                        /*
                             * Add a placeholder message so we won't download the original
                             * message again if we synchronize before the remote move is
                             * complete.
                             */
                        // We need to open this folder to get the folder id
                        open(OPEN_MODE_RW);
                        cv.clear();
                        cv.put("uid", oldUID);
                        cv.putNull("flags");
                        cv.put("read", 1);
                        cv.put("deleted", 1);
                        cv.put("folder_id", mFolderId);
                        cv.put("empty", 0);
                        String messageId = message.getMessageId();
                        if (messageId != null) {
                            cv.put("message_id", messageId);
                        }
                        final long newId;
                        if (threadInfo.msgId != -1) {
                            // There already existed an empty message in the target folder.
                            // Let's use it as placeholder.
                            newId = threadInfo.msgId;
                            db.update("messages", cv, "id = ?", new String[] { Long.toString(newId) });
                        } else {
                            newId = db.insert("messages", null, cv);
                        }
                        /*
                             * Update old entry in 'threads' table to point to the newly
                             * created placeholder.
                             */
                        cv.clear();
                        cv.put("message_id", newId);
                        db.update("threads", cv, "id = ?", new String[] { Long.toString(lMessage.getThreadId()) });
                    }
                } catch (MessagingException e) {
                    throw new WrappedException(e);
                }
                return null;
            }
        });
        this.localStore.notifyChange();
        return uidMap;
    } catch (WrappedException e) {
        throw (MessagingException) e.getCause();
    }
}
Also used : ContentValues(android.content.ContentValues) WrappedException(com.fsck.k9.mailstore.LockableDatabase.WrappedException) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MessagingException(com.fsck.k9.mail.MessagingException) HashMap(java.util.HashMap) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 10 with K9

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

the class MessagingController method checkMail.

/**
     * Checks mail for one or multiple accounts. If account is null all accounts
     * are checked.
     */
public void checkMail(final Context context, final Account account, final boolean ignoreLastCheckedTime, final boolean useManualWakeLock, final MessagingListener listener) {
    TracingWakeLock twakeLock = null;
    if (useManualWakeLock) {
        TracingPowerManager pm = TracingPowerManager.getPowerManager(context);
        twakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "K9 MessagingController.checkMail");
        twakeLock.setReferenceCounted(false);
        twakeLock.acquire(K9.MANUAL_WAKE_LOCK_TIMEOUT);
    }
    final TracingWakeLock wakeLock = twakeLock;
    for (MessagingListener l : getListeners()) {
        l.checkMailStarted(context, account);
    }
    putBackground("checkMail", listener, new Runnable() {

        @Override
        public void run() {
            try {
                Timber.i("Starting mail check");
                Preferences prefs = Preferences.getPreferences(context);
                Collection<Account> accounts;
                if (account != null) {
                    accounts = new ArrayList<>(1);
                    accounts.add(account);
                } else {
                    accounts = prefs.getAvailableAccounts();
                }
                for (final Account account : accounts) {
                    checkMailForAccount(context, account, ignoreLastCheckedTime, listener);
                }
            } catch (Exception e) {
                Timber.e(e, "Unable to synchronize mail");
                addErrorMessage(account, null, e);
            }
            putBackground("finalize sync", null, new Runnable() {

                @Override
                public void run() {
                    Timber.i("Finished mail sync");
                    if (wakeLock != null) {
                        wakeLock.release();
                    }
                    for (MessagingListener l : getListeners()) {
                        l.checkMailFinished(context, account);
                    }
                }
            });
        }
    });
}
Also used : SearchAccount(com.fsck.k9.search.SearchAccount) Account(com.fsck.k9.Account) TracingPowerManager(com.fsck.k9.mail.power.TracingPowerManager) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Preferences(com.fsck.k9.Preferences) TracingWakeLock(com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock) 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)

Aggregations

Account (com.fsck.k9.Account)20 MessagingException (com.fsck.k9.mail.MessagingException)13 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)10 LocalStore (com.fsck.k9.mailstore.LocalStore)10 Message (com.fsck.k9.mail.Message)9 Store (com.fsck.k9.mail.Store)9 LocalFolder (com.fsck.k9.mailstore.LocalFolder)9 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)9 Intent (android.content.Intent)8 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)8 LocalMessage (com.fsck.k9.mailstore.LocalMessage)8 SuppressLint (android.annotation.SuppressLint)7 BaseAccount (com.fsck.k9.BaseAccount)7 Preferences (com.fsck.k9.Preferences)7 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)7 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)7 Folder (com.fsck.k9.mail.Folder)7 SearchAccount (com.fsck.k9.search.SearchAccount)7