1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-26 01:28:50 -05:00

Delete all messages in a thread when 'delete' is used on the placeholder

This commit is contained in:
cketti 2012-11-03 01:52:45 +01:00
parent 966794f169
commit e339dd1f10
5 changed files with 82 additions and 16 deletions

View File

@ -3560,6 +3560,47 @@ public class MessagingController implements Runnable {
} }
} }
public void deleteThreads(final Message[] messages) {
actOnMessages(messages, new MessageActor() {
@Override
public void act(final Account account, final Folder folder,
final List<Message> accountMessages) {
for (Message message : accountMessages) {
suppressMessage(account, folder.getName(), message);
}
putBackground("deleteThreads", null, new Runnable() {
@Override
public void run() {
deleteThreadsSynchronous(account, folder.getName(), accountMessages);
}
});
}
});
}
public void deleteThreadsSynchronous(Account account, String folderName,
List<Message> messages) {
try {
LocalStore localStore = account.getLocalStore();
List<Message> messagesToDelete = new ArrayList<Message>();
for (Message message : messages) {
long rootId = ((LocalMessage) message).getRootId();
Message[] messagesInThread = localStore.getMessagesInThread(rootId);
Collections.addAll(messagesToDelete, messagesInThread);
}
deleteMessagesSynchronous(account, folderName,
messagesToDelete.toArray(EMPTY_MESSAGE_ARRAY), null);
} catch (MessagingException e) {
Log.e(K9.LOG_TAG, "Something went wrong while deleting threads", e);
}
}
public void deleteMessages(final Message[] messages, final MessagingListener listener) { public void deleteMessages(final Message[] messages, final MessagingListener listener) {
actOnMessages(messages, new MessageActor() { actOnMessages(messages, new MessageActor() {

View File

@ -1068,7 +1068,11 @@ public class MessageListFragment extends SherlockFragment implements OnItemClick
} }
private void onDelete(Message[] messages) { private void onDelete(Message[] messages) {
mController.deleteMessages(messages, null); if (mThreadedList) {
mController.deleteThreads(messages);
} else {
mController.deleteMessages(messages, null);
}
} }
@Override @Override
@ -2563,7 +2567,7 @@ public class MessageListFragment extends SherlockFragment implements OnItemClick
} }
private Message[] getCheckedMessages() { private Message[] getCheckedMessages() {
Message[] messages = new Message[mSelectedCount]; Message[] messages = new Message[mSelected.size()];
int out = 0; int out = 0;
for (int position = 0, end = mAdapter.getCount(); position < end; position++) { for (int position = 0, end = mAdapter.getCount(); position < end; position++) {
if (mSelected.get(position, false)) { if (mSelected.get(position, false)) {

View File

@ -73,6 +73,9 @@ import com.fsck.k9.mail.store.StorageManager.StorageProvider;
import com.fsck.k9.provider.AttachmentProvider; import com.fsck.k9.provider.AttachmentProvider;
import com.fsck.k9.provider.EmailProvider; import com.fsck.k9.provider.EmailProvider;
import com.fsck.k9.search.LocalSearch; import com.fsck.k9.search.LocalSearch;
import com.fsck.k9.search.SearchSpecification.Attribute;
import com.fsck.k9.search.SearchSpecification.SearchCondition;
import com.fsck.k9.search.SearchSpecification.Searchfield;
import com.fsck.k9.search.SqlQueryBuilder; import com.fsck.k9.search.SqlQueryBuilder;
/** /**
@ -963,7 +966,10 @@ public class LocalStore extends Store implements Serializable {
List<String> queryArgs = new ArrayList<String>(); List<String> queryArgs = new ArrayList<String>();
SqlQueryBuilder.buildWhereClause(mAccount, search.getConditions(), query, queryArgs); SqlQueryBuilder.buildWhereClause(mAccount, search.getConditions(), query, queryArgs);
String where = query.toString(); // Avoid "ambiguous column name" error by prefixing "id" with the message table name
String where = SqlQueryBuilder.addPrefixToSelection(new String[] { "id" },
"messages.", query.toString());
String[] selectionArgs = queryArgs.toArray(EMPTY_STRING_ARRAY); String[] selectionArgs = queryArgs.toArray(EMPTY_STRING_ARRAY);
String sqlQuery = "SELECT " + GET_MESSAGES_COLS + "FROM messages " + String sqlQuery = "SELECT " + GET_MESSAGES_COLS + "FROM messages " +
@ -1036,6 +1042,16 @@ public class LocalStore extends Store implements Serializable {
} }
public Message[] getMessagesInThread(final long rootId) throws MessagingException {
String rootIdString = Long.toString(rootId);
LocalSearch search = new LocalSearch();
search.and(Searchfield.THREAD_ROOT, rootIdString, Attribute.EQUALS);
search.or(new SearchCondition(Searchfield.ID, Attribute.EQUALS, rootIdString));
return searchForMessages(null, search);
}
public AttachmentInfo getAttachmentInfo(final String attachmentId) throws UnavailableStorageException { public AttachmentInfo getAttachmentInfo(final String attachmentId) throws UnavailableStorageException {
return database.execute(false, new DbCallback<AttachmentInfo>() { return database.execute(false, new DbCallback<AttachmentInfo>() {
@Override @Override

View File

@ -15,6 +15,7 @@ import com.fsck.k9.mail.store.LockableDatabase;
import com.fsck.k9.mail.store.LockableDatabase.DbCallback; import com.fsck.k9.mail.store.LockableDatabase.DbCallback;
import com.fsck.k9.mail.store.LockableDatabase.WrappedException; import com.fsck.k9.mail.store.LockableDatabase.WrappedException;
import com.fsck.k9.mail.store.UnavailableStorageException; import com.fsck.k9.mail.store.UnavailableStorageException;
import com.fsck.k9.search.SqlQueryBuilder;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.content.ContentProvider; import android.content.ContentProvider;
@ -257,9 +258,11 @@ public class EmailProvider extends ContentProvider {
query.append(" FROM messages m " + query.append(" FROM messages m " +
"LEFT JOIN folders f ON (m.folder_id = f.id) " + "LEFT JOIN folders f ON (m.folder_id = f.id) " +
"WHERE "); "WHERE ");
query.append(addPrefixToSelection(MESSAGES_COLUMNS, "m.", where)); query.append(SqlQueryBuilder.addPrefixToSelection(MESSAGES_COLUMNS,
"m.", where));
query.append(" ORDER BY "); query.append(" ORDER BY ");
query.append(addPrefixToSelection(MESSAGES_COLUMNS, "m.", sortOrder)); query.append(SqlQueryBuilder.addPrefixToSelection(MESSAGES_COLUMNS,
"m.", sortOrder));
cursor = db.rawQuery(query.toString(), selectionArgs); cursor = db.rawQuery(query.toString(), selectionArgs);
} else { } else {
@ -332,7 +335,8 @@ public class EmailProvider extends ContentProvider {
if (!StringUtils.isNullOrEmpty(selection)) { if (!StringUtils.isNullOrEmpty(selection)) {
query.append("AND ("); query.append("AND (");
query.append(addPrefixToSelection(MESSAGES_COLUMNS, "h.", selection)); query.append(SqlQueryBuilder.addPrefixToSelection(MESSAGES_COLUMNS,
"h.", selection));
query.append(") "); query.append(") ");
} }
@ -340,7 +344,8 @@ public class EmailProvider extends ContentProvider {
if (!StringUtils.isNullOrEmpty(sortOrder)) { if (!StringUtils.isNullOrEmpty(sortOrder)) {
query.append(" ORDER BY "); query.append(" ORDER BY ");
query.append(addPrefixToSelection(MESSAGES_COLUMNS, "m.", sortOrder)); query.append(SqlQueryBuilder.addPrefixToSelection(MESSAGES_COLUMNS,
"m.", sortOrder));
} }
return db.rawQuery(query.toString(), selectionArgs); return db.rawQuery(query.toString(), selectionArgs);
@ -351,15 +356,6 @@ public class EmailProvider extends ContentProvider {
} }
} }
private String addPrefixToSelection(String[] columnNames, String prefix, String selection) {
String result = selection;
for (String columnName : columnNames) {
result = result.replaceAll("\\b" + columnName + "\\b", prefix + columnName);
}
return result;
}
private Account getAccount(String accountUuid) { private Account getAccount(String accountUuid) {
if (mPreferences == null) { if (mPreferences == null) {
Context appContext = getContext().getApplicationContext(); Context appContext = getContext().getApplicationContext();

View File

@ -220,4 +220,13 @@ public class SqlQueryBuilder {
} }
} }
} }
public static String addPrefixToSelection(String[] columnNames, String prefix, String selection) {
String result = selection;
for (String columnName : columnNames) {
result = result.replaceAll("\\b" + columnName + "\\b", prefix + columnName);
}
return result;
}
} }