diff --git a/src/com/fsck/k9/activity/MessageInfoHolder.java b/src/com/fsck/k9/activity/MessageInfoHolder.java index 4669daec6..56db97ced 100644 --- a/src/com/fsck/k9/activity/MessageInfoHolder.java +++ b/src/com/fsck/k9/activity/MessageInfoHolder.java @@ -24,6 +24,7 @@ public class MessageInfoHolder { public boolean selected; public String account; public String uri; + public int threadCount; // Empty constructor for comparison public MessageInfoHolder() { diff --git a/src/com/fsck/k9/fragment/MessageListFragment.java b/src/com/fsck/k9/fragment/MessageListFragment.java index 87510e5a2..4bd5ba88f 100644 --- a/src/com/fsck/k9/fragment/MessageListFragment.java +++ b/src/com/fsck/k9/fragment/MessageListFragment.java @@ -687,7 +687,7 @@ public class MessageListFragment extends SherlockFragment implements OnItemClick final MessageInfoHolder message = (MessageInfoHolder) parent.getItemAtPosition(position); if (mSelectedCount > 0) { toggleMessageSelect(message); - } else if (((LocalMessage) message.message).getThreadCount() > 1) { + } else if (message.threadCount > 1) { Folder folder = message.message.getFolder(); long rootId = ((LocalMessage) message.message).getRootId(); mFragmentListener.showThread(folder.getAccount(), folder.getName(), rootId); @@ -1767,7 +1767,36 @@ public class MessageListFragment extends SherlockFragment implements OnItemClick FolderInfoHolder folderInfoHolder = new FolderInfoHolder( getActivity(), messageFolder, messageAccount); messageHelper.populate(m, message, folderInfoHolder, messageAccount); - messagesToAdd.add(m); + + if (verifyAgainstSearch) { + LocalMessage localMessage = (LocalMessage) message; + + if (mThreadId != -1) { + if (localMessage.getRootId() == mThreadId || + localMessage.getId() == mThreadId) { + messagesToAdd.add(m); + } + } else if (mThreadViewEnabled) { + long threadId = localMessage.getRootId(); + if (threadId == -1) { + threadId = localMessage.getId(); + } + + MessageInfoHolder threadPlaceHolder = getThread(threadId); + if (threadPlaceHolder == null) { + messagesToAdd.add(m); + } else if (m.compareDate.after(threadPlaceHolder.compareDate)) { + messagesToRemove.add(threadPlaceHolder); + messagesToAdd.add(m); + } else { + threadPlaceHolder.threadCount = m.threadCount; + } + } else { + messagesToAdd.add(m); + } + } else { + messagesToAdd.add(m); + } } else { if (mQueryString != null) { if (verifyAgainstSearch) { @@ -1876,6 +1905,17 @@ public class MessageListFragment extends SherlockFragment implements OnItemClick return null; } + private MessageInfoHolder getThread(long threadId) { + for (MessageInfoHolder holder : mMessages) { + LocalMessage localMessage = (LocalMessage) holder.message; + if (localMessage.getId() == threadId || localMessage.getRootId() == threadId) { + return holder; + } + } + + return null; + } + public FolderInfoHolder getFolder(String folder, Account account) { LocalFolder local_folder = null; try { @@ -2056,7 +2096,7 @@ public class MessageListFragment extends SherlockFragment implements OnItemClick subject = message.message.getSubject(); } - int threadCount = ((LocalMessage) message.message).getThreadCount(); + int threadCount = message.threadCount; if (threadCount > 1) { holder.threadCount.setText(Integer.toString(threadCount)); holder.threadCount.setVisibility(View.VISIBLE); diff --git a/src/com/fsck/k9/helper/MessageHelper.java b/src/com/fsck/k9/helper/MessageHelper.java index 9c2abb491..ae708f224 100644 --- a/src/com/fsck/k9/helper/MessageHelper.java +++ b/src/com/fsck/k9/helper/MessageHelper.java @@ -17,6 +17,7 @@ import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.Message; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.Message.RecipientType; +import com.fsck.k9.mail.store.LocalStore.LocalMessage; import com.fsck.k9.helper.DateFormatter; public class MessageHelper { @@ -86,6 +87,8 @@ public class MessageHelper { target.account = account.getUuid(); target.uri = "email://messages/" + account.getAccountNumber() + "/" + message.getFolder().getName() + "/" + message.getUid(); + target.threadCount = ((LocalMessage) message).getThreadCount(); + } catch (MessagingException me) { Log.w(K9.LOG_TAG, "Unable to load message info", me); } diff --git a/src/com/fsck/k9/mail/store/LocalStore.java b/src/com/fsck/k9/mail/store/LocalStore.java index fe69d8af3..8c42b6f48 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -93,6 +93,10 @@ public class LocalStore extends Store implements Serializable { "subject, sender_list, date, uid, flags, id, to_list, cc_list, " + "bcc_list, reply_to_list, attachment_count, internal_date, message_id, folder_id, preview, thread_root, thread_parent, empty "; + static private String GET_MESSAGES_COLS_PREFIX = + "m.subject, m.sender_list, m.date, m.uid, m.flags, m.id, m.to_list, m.cc_list, " + + "m.bcc_list, m.reply_to_list, m.attachment_count, m.internal_date, m.message_id, " + + "m.folder_id, m.preview, m.thread_root, m.thread_parent, m.empty "; static private String GET_FOLDER_COLS = "id, name, unread_count, visible_limit, last_updated, status, push_state, last_pushed, flagged_count, integrate, top_group, poll_class, push_class, display_class"; @@ -1891,13 +1895,23 @@ public class LocalStore extends Store implements Serializable { try { cursor = db.rawQuery( - "SELECT " - + GET_MESSAGES_COLS - + "FROM messages WHERE uid = ? AND folder_id = ?", + "SELECT " + + GET_MESSAGES_COLS_PREFIX + ", COUNT(c.id) " + + "FROM messages m LEFT JOIN messages c " + + "ON (" + + "(" + + "m.thread_root IN (c.thread_root, c.id)" + + " OR " + + "(m.thread_root IS NULL AND m.id IN (c.thread_root, c.id))" + + ") AND c.empty != 1) " + + "WHERE m.uid = ? AND m.folder_id = ?", new String[] { message.getUid(), Long.toString(mFolderId) }); - if (!cursor.moveToNext()) { + + // Note: Because of the COUNT(c.id) we will always get one result + // row even if nothing was found. So we check if 'id' is NULL + if (!cursor.moveToNext() || cursor.isNull(0)) { return null; } message.populateFromGetMessageCursor(cursor);