Skip to content

Commit

Permalink
Merge pull request #8750 from cketti/empty-spam
Browse files Browse the repository at this point in the history
Add Empty Spam command
  • Loading branch information
cketti authored Jan 21, 2025
2 parents 6f3f5d2 + 22598b9 commit db82787
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.fsck.k9.controller.MessagingControllerCommands.PendingAppend;
import com.fsck.k9.controller.MessagingControllerCommands.PendingCommand;
import com.fsck.k9.controller.MessagingControllerCommands.PendingDelete;
import com.fsck.k9.controller.MessagingControllerCommands.PendingEmptySpam;
import com.fsck.k9.controller.MessagingControllerCommands.PendingEmptyTrash;
import com.fsck.k9.controller.MessagingControllerCommands.PendingExpunge;
import com.fsck.k9.controller.MessagingControllerCommands.PendingMarkAllAsRead;
Expand Down Expand Up @@ -2099,6 +2100,58 @@ private static List<String> getUidsFromMessages(List<LocalMessage> messages) {
return uids;
}

void processPendingEmptySpam(Account account) throws MessagingException {
if (!account.hasSpamFolder()) {
return;
}

long spamFolderId = account.getSpamFolderId();
LocalStore localStore = localStoreProvider.getInstance(account);
LocalFolder folder = localStore.getFolder(spamFolderId);
folder.open();
String spamFolderServerId = folder.getServerId();

Backend backend = getBackend(account);
backend.deleteAllMessages(spamFolderServerId);

// Remove all messages marked as deleted
folder.destroyDeletedMessages();

compact(account);
}

public void emptySpam(final Account account, MessagingListener listener) {
putBackground("emptySpam", listener, new Runnable() {
@Override
public void run() {
try {
Long spamFolderId = account.getSpamFolderId();
if (spamFolderId == null) {
Timber.w("No Spam folder configured. Can't empty spam.");
return;
}

LocalStore localStore = localStoreProvider.getInstance(account);
LocalFolder localFolder = localStore.getFolder(spamFolderId);
localFolder.open();

localFolder.destroyLocalOnlyMessages();
localFolder.setFlags(Collections.singleton(Flag.DELETED), true);

for (MessagingListener l : getListeners()) {
l.folderStatusChanged(account, spamFolderId);
}

PendingCommand command = PendingEmptySpam.create();
queuePendingCommand(account, command);
processPendingCommands(account);
} catch (Exception e) {
Timber.e(e, "emptySpam failed");
}
}
});
}

void processPendingEmptyTrash(Account account) throws MessagingException {
if (!account.hasTrashFolder()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class MessagingControllerCommands {
static final String COMMAND_EXPUNGE = "expunge";
static final String COMMAND_MOVE_OR_COPY = "move_or_copy";
static final String COMMAND_MOVE_AND_MARK_AS_READ = "move_and_mark_as_read";
static final String COMMAND_EMPTY_SPAM = "empty_spam";
static final String COMMAND_EMPTY_TRASH = "empty_trash";

public abstract static class PendingCommand {
Expand Down Expand Up @@ -95,6 +96,22 @@ public void execute(MessagingController controller, Account account) throws Mess
}
}

public static class PendingEmptySpam extends PendingCommand {
public static PendingEmptySpam create() {
return new PendingEmptySpam();
}

@Override
public String getCommandName() {
return COMMAND_EMPTY_SPAM;
}

@Override
public void execute(MessagingController controller, Account account) throws MessagingException {
controller.processPendingEmptySpam(account);
}
}

public static class PendingEmptyTrash extends PendingCommand {
public static PendingEmptyTrash create() {
return new PendingEmptyTrash();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.fsck.k9.controller.MessagingControllerCommands.PendingAppend;
import com.fsck.k9.controller.MessagingControllerCommands.PendingCommand;
import com.fsck.k9.controller.MessagingControllerCommands.PendingDelete;
import com.fsck.k9.controller.MessagingControllerCommands.PendingEmptySpam;
import com.fsck.k9.controller.MessagingControllerCommands.PendingEmptyTrash;
import com.fsck.k9.controller.MessagingControllerCommands.PendingExpunge;
import com.fsck.k9.controller.MessagingControllerCommands.PendingMarkAllAsRead;
Expand Down Expand Up @@ -37,6 +38,7 @@ private PendingCommandSerializer() {
moshi.adapter(PendingMoveAndMarkAsRead.class));
adapters.put(MessagingControllerCommands.COMMAND_APPEND, moshi.adapter(PendingAppend.class));
adapters.put(MessagingControllerCommands.COMMAND_REPLACE, moshi.adapter(PendingReplace.class));
adapters.put(MessagingControllerCommands.COMMAND_EMPTY_SPAM, moshi.adapter(PendingEmptySpam.class));
adapters.put(MessagingControllerCommands.COMMAND_EMPTY_TRASH, moshi.adapter(PendingEmptyTrash.class));
adapters.put(MessagingControllerCommands.COMMAND_EXPUNGE, moshi.adapter(PendingExpunge.class));
adapters.put(MessagingControllerCommands.COMMAND_MARK_ALL_AS_READ, moshi.adapter(PendingMarkAllAsRead.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,18 @@ class MessageListFragment :
}
}

private fun onEmptySpam() {
if (isShowingSpamFolder) {
showDialog(R.id.dialog_confirm_empty_spam)
}
}

private val isShowingSpamFolder: Boolean
get() {
if (!isSingleFolderMode) return false
return currentFolder!!.databaseId == account!!.spamFolderId
}

private fun onEmptyTrash() {
if (isShowingTrashFolder) {
showDialog(R.id.dialog_confirm_empty_trash)
Expand Down Expand Up @@ -786,6 +798,14 @@ class MessageListFragment :
ConfirmationDialogFragment.newInstance(dialogId, title, message, confirmText, cancelText)
}

R.id.dialog_confirm_empty_spam -> {
val title = getString(R.string.dialog_confirm_empty_spam_title)
val message = getString(R.string.dialog_confirm_empty_spam_message)
val confirmText = getString(R.string.dialog_confirm_delete_confirm_button)
val cancelText = getString(R.string.dialog_confirm_delete_cancel_button)
ConfirmationDialogFragment.newInstance(dialogId, title, message, confirmText, cancelText)
}

R.id.dialog_confirm_empty_trash -> {
val title = getString(R.string.dialog_confirm_empty_trash_title)
val message = getString(R.string.dialog_confirm_empty_trash_message)
Expand Down Expand Up @@ -820,6 +840,7 @@ class MessageListFragment :
menu.findItem(R.id.set_sort).isVisible = true
menu.findItem(R.id.select_all).isVisible = true
menu.findItem(R.id.mark_all_as_read).isVisible = isMarkAllAsReadSupported
menu.findItem(R.id.empty_spam).isVisible = isShowingSpamFolder
menu.findItem(R.id.empty_trash).isVisible = isShowingTrashFolder

if (isSingleAccountMode) {
Expand All @@ -843,6 +864,7 @@ class MessageListFragment :
menu.findItem(R.id.select_all).isVisible = false
menu.findItem(R.id.mark_all_as_read).isVisible = false
menu.findItem(R.id.send_messages).isVisible = false
menu.findItem(R.id.empty_spam).isVisible = false
menu.findItem(R.id.empty_trash).isVisible = false
menu.findItem(R.id.expunge).isVisible = false
menu.findItem(R.id.search_everywhere).isVisible = false
Expand All @@ -862,6 +884,7 @@ class MessageListFragment :
R.id.select_all -> selectAll()
R.id.mark_all_as_read -> confirmMarkAllAsRead()
R.id.send_messages -> onSendPendingMessages()
R.id.empty_spam -> onEmptySpam()
R.id.empty_trash -> onEmptyTrash()
R.id.expunge -> onExpunge()
R.id.search_everywhere -> onSearchEverywhere()
Expand Down Expand Up @@ -1227,6 +1250,10 @@ class MessageListFragment :
markAllAsRead()
}

R.id.dialog_confirm_empty_spam -> {
messagingController.emptySpam(account, null)
}

R.id.dialog_confirm_empty_trash -> {
messagingController.emptyTrash(account, null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@
app:showAsAction="never"
/>

<!-- MessageList -->
<item
android:id="@+id/empty_spam"
android:title="@string/empty_spam_action"
app:showAsAction="never"
/>

<!-- MessageList -->
<item
android:id="@+id/empty_trash"
Expand Down
1 change: 1 addition & 0 deletions legacy/ui/legacy/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<resources>

<item type="id" name="dialog_confirm_mark_all_as_read" />
<item type="id" name="dialog_confirm_empty_spam" />
<item type="id" name="dialog_confirm_empty_trash" />
<item type="id" name="dialog_confirm_delete" />
<item type="id" name="dialog_confirm_spam" />
Expand Down
4 changes: 4 additions & 0 deletions legacy/ui/legacy/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<string name="read_receipt_enabled">Will request read receipt</string>
<string name="read_receipt_disabled">Will not request read receipt</string>
<string name="add_attachment_action">Add attachment</string>
<string name="empty_spam_action">Empty Spam</string>
<string name="empty_trash_action">Empty Trash</string>
<string name="expunge_action">Expunge</string>
<string name="about_action">About</string>
Expand Down Expand Up @@ -721,6 +722,9 @@
<string name="dialog_confirm_mark_all_as_read_title">Confirm mark all as read</string>
<string name="dialog_confirm_mark_all_as_read_message">Do you want to mark all messages as read?</string>

<string name="dialog_confirm_empty_spam_title">Confirm empty spam</string>
<string name="dialog_confirm_empty_spam_message">Do you want to empty the spam folder?</string>

<string name="dialog_confirm_empty_trash_title">Confirm empty trash</string>
<string name="dialog_confirm_empty_trash_message">Do you want to empty the trash folder?</string>

Expand Down

0 comments on commit db82787

Please sign in to comment.