From 96f827d2911916bf8260da1eeb7705fbe6dad3e4 Mon Sep 17 00:00:00 2001 From: cketti Date: Sun, 22 Jan 2012 05:04:15 +0100 Subject: [PATCH] Modified LocalStore to use UPDATE when replacing existing messages This way we can later use the database ID to keep track of drafts. --- src/com/fsck/k9/mail/store/LocalStore.java | 39 +++++++++++++--------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/com/fsck/k9/mail/store/LocalStore.java b/src/com/fsck/k9/mail/store/LocalStore.java index fbcfafa21..21d2865fb 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -2019,9 +2019,9 @@ public class LocalStore extends Store implements Serializable { /** * The method differs slightly from the contract; If an incoming message already has a uid - * assigned and it matches the uid of an existing message then this message will replace the - * old message. It is implemented as a delete/insert. This functionality is used in saving - * of drafts and re-synchronization of updated server messages. + * assigned and it matches the uid of an existing message then this message will replace + * the old message. This functionality is used in saving of drafts and re-synchronization + * of updated server messages. * * NOTE that although this method is located in the LocalStore class, it is not guaranteed * that the messages supplied as parameters are actually {@link LocalMessage} instances (in @@ -2042,6 +2042,7 @@ public class LocalStore extends Store implements Serializable { throw new Error("LocalStore can only store Messages that extend MimeMessage"); } + long oldMessageId = -1; String uid = message.getUid(); if (uid == null || copy) { uid = K9.LOCAL_UID_PREFIX + UUID.randomUUID().toString(); @@ -2049,20 +2050,20 @@ public class LocalStore extends Store implements Serializable { message.setUid(uid); } } else { - Message oldMessage = getMessage(uid); - if (oldMessage != null && !oldMessage.isSet(Flag.SEEN)) { - setUnreadMessageCount(getUnreadMessageCount() - 1); + LocalMessage oldMessage = (LocalMessage) getMessage(uid); + + if (oldMessage != null) { + oldMessageId = oldMessage.getId(); + + if (!oldMessage.isSet(Flag.SEEN)) { + setUnreadMessageCount(getUnreadMessageCount() - 1); + } + if (oldMessage.isSet(Flag.FLAGGED)) { + setFlaggedMessageCount(getFlaggedMessageCount() - 1); + } } - if (oldMessage != null && oldMessage.isSet(Flag.FLAGGED)) { - setFlaggedMessageCount(getFlaggedMessageCount() - 1); - } - /* - * The message may already exist in this Folder, so delete it first. - */ + deleteAttachments(message.getUid()); - db.execSQL("DELETE FROM messages WHERE folder_id = ? AND uid = ?", - new Object[] - { mFolderId, message.getUid() }); } ArrayList viewables = new ArrayList(); @@ -2132,7 +2133,13 @@ public class LocalStore extends Store implements Serializable { cv.put("message_id", messageId); } long messageUid; - messageUid = db.insert("messages", "uid", cv); + + if (oldMessageId == -1) { + messageUid = db.insert("messages", "uid", cv); + } else { + db.update("messages", cv, "id = ?", new String[] { Long.toString(oldMessageId) }); + messageUid = oldMessageId; + } for (Part attachment : attachments) { saveAttachment(messageUid, attachment, copy); }