Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove folder notification class #8127

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions config/detekt/detekt-baseline-legacy-common.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<SmellBaseline>
<ManuallySuppressedIssues></ManuallySuppressedIssues>
<CurrentIssues>
<ID>CyclomaticComplexMethod:K9NotificationStrategy.kt$K9NotificationStrategy$override fun shouldNotifyForMessage( account: Account, localFolder: LocalFolder, message: LocalMessage, isOldMessage: Boolean, ): Boolean</ID>
<ID>LongMethod:K9NotificationStrategy.kt$K9NotificationStrategy$override fun shouldNotifyForMessage( account: Account, localFolder: LocalFolder, message: LocalMessage, isOldMessage: Boolean, ): Boolean</ID>
<ID>ReturnCount:K9NotificationStrategy.kt$K9NotificationStrategy$override fun shouldNotifyForMessage( account: Account, localFolder: LocalFolder, message: LocalMessage, isOldMessage: Boolean, ): Boolean</ID>
<ID>TooManyFunctions:K9CoreResourceProvider.kt$K9CoreResourceProvider : CoreResourceProvider</ID>
<ID>TooManyFunctions:K9NotificationActionCreator.kt$K9NotificationActionCreator : NotificationActionCreator</ID>
Expand Down
21 changes: 0 additions & 21 deletions config/detekt/detekt-baseline-legacy-storage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,6 @@
<ID>MagicNumber:MigrationTo84.kt$MigrationTo84$3</ID>
<ID>MagicNumber:MigrationTo84.kt$MigrationTo84$4</ID>
<ID>MagicNumber:MigrationTo84.kt$MigrationTo84$5</ID>
<ID>MagicNumber:Migrations.kt$Migrations$62</ID>
<ID>MagicNumber:Migrations.kt$Migrations$64</ID>
<ID>MagicNumber:Migrations.kt$Migrations$65</ID>
<ID>MagicNumber:Migrations.kt$Migrations$66</ID>
<ID>MagicNumber:Migrations.kt$Migrations$67</ID>
<ID>MagicNumber:Migrations.kt$Migrations$68</ID>
<ID>MagicNumber:Migrations.kt$Migrations$69</ID>
<ID>MagicNumber:Migrations.kt$Migrations$70</ID>
<ID>MagicNumber:Migrations.kt$Migrations$71</ID>
<ID>MagicNumber:Migrations.kt$Migrations$72</ID>
<ID>MagicNumber:Migrations.kt$Migrations$73</ID>
<ID>MagicNumber:Migrations.kt$Migrations$74</ID>
<ID>MagicNumber:Migrations.kt$Migrations$75</ID>
<ID>MagicNumber:Migrations.kt$Migrations$76</ID>
<ID>MagicNumber:Migrations.kt$Migrations$78</ID>
<ID>MagicNumber:Migrations.kt$Migrations$79</ID>
<ID>MagicNumber:Migrations.kt$Migrations$80</ID>
<ID>MagicNumber:Migrations.kt$Migrations$81</ID>
<ID>MagicNumber:Migrations.kt$Migrations$82</ID>
<ID>MagicNumber:Migrations.kt$Migrations$83</ID>
<ID>MagicNumber:Migrations.kt$Migrations$84</ID>
<ID>MagicNumber:RetrieveFolderOperations.kt$CursorFolderAccessor$10</ID>
<ID>MagicNumber:RetrieveFolderOperations.kt$CursorFolderAccessor$11</ID>
<ID>MagicNumber:RetrieveFolderOperations.kt$CursorFolderAccessor$12</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,13 @@ class K9NotificationStrategy(
return false
}

val folder = message.folder
if (folder != null) {
when (folder.databaseId) {
account.inboxFolderId -> {
// Don't skip notifications if the Inbox folder is also configured as another special folder
}
account.trashFolderId -> {
Timber.v("No notification: Message is in Trash folder")
return false
}
account.draftsFolderId -> {
Timber.v("No notification: Message is in Drafts folder")
return false
}
account.spamFolderId -> {
Timber.v("No notification: Message is in Spam folder")
return false
}
account.sentFolderId -> {
Timber.v("No notification: Message is in Sent folder")
return false
}
}
}

if (isModeMismatch(account.folderDisplayMode, localFolder.displayClass)) {
Timber.v("No notification: Message is in folder not being displayed")
return false
}

if (isModeMismatch(account.folderNotifyNewMailMode, localFolder.notifyClass)) {
Timber.v("No notification: Notifications are disabled for this folder class")
if (!localFolder.isNotificationsEnabled) {
Timber.v("No notification: Notifications are not enabled for this folder")
return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FolderSettingsProvider(val preferences: Preferences, val account: Account)
visibleLimit = account.displayCount,
displayClass = storage.getString("$prefix.displayMode", null).toFolderClass(FolderClass.NO_CLASS),
syncClass = storage.getString("$prefix.syncMode", null).toFolderClass(FolderClass.INHERITED),
notifyClass = storage.getString("$prefix.notifyMode", null).toFolderClass(FolderClass.INHERITED),
isNotificationsEnabled = storage.getBoolean("$prefix.notificationsEnabled", false),
pushClass = storage.getString("$prefix.pushMode", null).toFolderClass(FolderClass.SECOND_CLASS),
inTopGroup = storage.getBoolean("$prefix.inTopGroup", false),
integrate = storage.getBoolean("$prefix.integrate", false),
Expand All @@ -31,7 +31,7 @@ class FolderSettingsProvider(val preferences: Preferences, val account: Account)

editor.remove("$prefix.displayMode")
editor.remove("$prefix.syncMode")
editor.remove("$prefix.notifyMode")
editor.remove("$prefix.notificationsEnabled")
editor.remove("$prefix.pushMode")
editor.remove("$prefix.inTopGroup")
editor.remove("$prefix.integrate")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class LocalFolder {
private FolderClass displayClass = FolderClass.NO_CLASS;
private FolderClass syncClass = FolderClass.INHERITED;
private FolderClass pushClass = FolderClass.SECOND_CLASS;
private FolderClass notifyClass = FolderClass.INHERITED;
private boolean notificationsEnabled = false;

private boolean isInTopGroup = false;
private boolean isIntegrate = false;
Expand Down Expand Up @@ -177,8 +177,7 @@ void open(Cursor cursor) throws MessagingException {
String noClass = FolderClass.NO_CLASS.toString();
String displayClass = cursor.getString(LocalStore.FOLDER_DISPLAY_CLASS_INDEX);
this.displayClass = FolderClass.valueOf((displayClass == null) ? noClass : displayClass);
String notifyClass = cursor.getString(LocalStore.FOLDER_NOTIFY_CLASS_INDEX);
this.notifyClass = FolderClass.valueOf((notifyClass == null) ? noClass : notifyClass);
this.notificationsEnabled = cursor.getInt(LocalStore.FOLDER_NOTIFICATIONS_ENABLED_INDEX) == 1;
String pushClass = cursor.getString(LocalStore.FOLDER_PUSH_CLASS_INDEX);
this.pushClass = FolderClass.valueOf((pushClass == null) ? noClass : pushClass);
String syncClass = cursor.getString(LocalStore.FOLDER_SYNC_CLASS_INDEX);
Expand Down Expand Up @@ -308,10 +307,6 @@ public FolderClass getSyncClass() {
return (FolderClass.INHERITED == syncClass) ? getDisplayClass() : syncClass;
}

public FolderClass getNotifyClass() {
return (FolderClass.INHERITED == notifyClass) ? getPushClass() : notifyClass;
}

public FolderClass getPushClass() {
return (FolderClass.INHERITED == pushClass) ? getSyncClass() : pushClass;
}
Expand All @@ -331,9 +326,8 @@ public void setPushClass(FolderClass pushClass) throws MessagingException {
updateFolderColumn("push_class", this.pushClass.name());
}

public void setNotifyClass(FolderClass notifyClass) throws MessagingException {
this.notifyClass = notifyClass;
updateFolderColumn("notify_class", this.notifyClass.name());
public boolean isNotificationsEnabled() {
return notificationsEnabled;
}

public boolean isIntegrate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public class LocalStore {

static final String GET_FOLDER_COLS =
"folders.id, name, visible_limit, last_updated, status, " +
"integrate, top_group, poll_class, push_class, display_class, notify_class, more_messages, server_id, " +
"integrate, top_group, poll_class, push_class, display_class, notifications_enabled, more_messages, server_id, " +
"local_only, type";

static final int FOLDER_ID_INDEX = 0;
Expand All @@ -125,7 +125,7 @@ public class LocalStore {
static final int FOLDER_SYNC_CLASS_INDEX = 7;
static final int FOLDER_PUSH_CLASS_INDEX = 8;
static final int FOLDER_DISPLAY_CLASS_INDEX = 9;
static final int FOLDER_NOTIFY_CLASS_INDEX = 10;
static final int FOLDER_NOTIFICATIONS_ENABLED_INDEX = 10;
static final int MORE_MESSAGES_INDEX = 11;
static final int FOLDER_SERVER_ID_INDEX = 12;
static final int LOCAL_ONLY_INDEX = 13;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SpecialFolderUpdater(
folderRepository.setDisplayClass(account, newInboxId, FolderClass.FIRST_CLASS)
folderRepository.setSyncClass(account, newInboxId, FolderClass.FIRST_CLASS)
folderRepository.setPushClass(account, newInboxId, FolderClass.FIRST_CLASS)
folderRepository.setNotificationClass(account, newInboxId, FolderClass.FIRST_CLASS)
folderRepository.setNotificationsEnabled(account, newInboxId, true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ class AccountSettingsDescriptions {
new V(1, new BooleanSetting(false))
));
s.put("folderNotifyNewMailMode", Settings.versions(
new V(34, new EnumSetting<>(FolderMode.class, FolderMode.ALL))
new V(34, new EnumSetting<>(FolderMode.class, FolderMode.ALL)),
new V(96, null)
));
s.put("notifySelfNewMail", Settings.versions(
new V(1, new BooleanSetting(true))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.fsck.k9.preferences

import com.fsck.k9.preferences.upgrader.CombinedSettingsUpgraderTo96

internal typealias CombinedSettingsUpgraderFactory = () -> CombinedSettingsUpgrader

@Suppress("MagicNumber")
internal object CombinedSettingsUpgraders {
val UPGRADERS = emptyMap<Int, CombinedSettingsUpgraderFactory>()
val UPGRADERS = mapOf<Int, CombinedSettingsUpgraderFactory>(
96 to ::CombinedSettingsUpgraderTo96,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class FolderSettingsDescriptions {
new V(1, new EnumSetting<>(FolderClass.class, FolderClass.NO_CLASS))
));
s.put("notifyMode", Settings.versions(
new V(34, new EnumSetting<>(FolderClass.class, FolderClass.INHERITED))
new V(34, new EnumSetting<>(FolderClass.class, FolderClass.INHERITED)),
new V(96, null)
));
s.put("syncMode", Settings.versions(
new V(1, new EnumSetting<>(FolderClass.class, FolderClass.INHERITED))
Expand All @@ -45,6 +46,9 @@ class FolderSettingsDescriptions {
s.put("integrate", Settings.versions(
new V(1, new BooleanSetting(false))
));
s.put("notificationsEnabled", Settings.versions(
new V(96, new BooleanSetting(false))
));

SETTINGS = Collections.unmodifiableMap(s);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FolderSettingsProvider(private val folderRepository: FolderRepository) {
isIntegrate == getDefaultValue("integrate") &&
syncClass == getDefaultValue("syncMode") &&
displayClass == getDefaultValue("displayMode") &&
notifyClass == getDefaultValue("notifyMode") &&
isNotificationsEnabled == getDefaultValue("notificationsEnabled") &&
pushClass == getDefaultValue("pushMode")
}

Expand All @@ -35,7 +35,7 @@ class FolderSettingsProvider(private val folderRepository: FolderRepository) {
isIntegrate,
syncClass,
displayClass,
notifyClass,
isNotificationsEnabled,
pushClass,
)
}
Expand All @@ -47,6 +47,6 @@ data class FolderSettings(
val isIntegrate: Boolean,
val syncClass: FolderClass,
val displayClass: FolderClass,
val notifyClass: FolderClass,
val isNotificationsEnabled: Boolean,
val pushClass: FolderClass,
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Settings {
*
* @see SettingsExporter
*/
public static final int VERSION = 95;
public static final int VERSION = 96;

static Map<String, Object> validate(int version, Map<String, TreeMap<Integer, SettingsDescription<?>>> settings,
Map<String, String> importedSettings, boolean useDefaultValues) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ class SettingsExporter(
writeFolderSetting(serializer, "inTopGroup", folder.isInTopGroup.toString())
writeFolderSetting(serializer, "syncMode", folder.syncClass.name)
writeFolderSetting(serializer, "displayMode", folder.displayClass.name)
writeFolderSetting(serializer, "notifyMode", folder.notifyClass.name)
writeFolderSetting(serializer, "notificationsEnabled", folder.isNotificationsEnabled.toString())
writeFolderSetting(serializer, "pushMode", folder.pushClass.name)

serializer.endTag(null, FOLDER_ELEMENT)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.fsck.k9.preferences.upgrader

import app.k9mail.legacy.account.Account.FolderMode
import com.fsck.k9.mail.FolderClass
import com.fsck.k9.preferences.CombinedSettingsUpgrader
import com.fsck.k9.preferences.InternalSettingsMap
import com.fsck.k9.preferences.ValidatedSettings

class CombinedSettingsUpgraderTo96 : CombinedSettingsUpgrader {
override fun upgrade(account: ValidatedSettings.Account): ValidatedSettings.Account {
val notifyFolderMode = account.settings["folderNotifyNewMailMode"] as? FolderMode ?: FolderMode.NONE
val ignoredFolders = getNotificationIgnoredFolders(account)

val newFolders = account.folders.map { folder ->
val notificationsEnabled = if (folder.name in ignoredFolders) {
false
} else {
folder.isNotificationEnabled(notifyFolderMode)
}

val newFolderSettings = folder.settings.toMutableMap().apply {
this["notificationsEnabled"] = notificationsEnabled
}

folder.copy(settings = newFolderSettings)
}

return account.copy(folders = newFolders)
}

private fun getNotificationIgnoredFolders(account: ValidatedSettings.Account): Set<String> {
return buildSet {
account.settings.getFolderNameUnlessInbox("trashFolderName")?.let { add(it) }
account.settings.getFolderNameUnlessInbox("draftsFolderName")?.let { add(it) }
account.settings.getFolderNameUnlessInbox("spamFolderName")?.let { add(it) }
account.settings.getFolderNameUnlessInbox("sentFolderName")?.let { add(it) }
}
}

private fun InternalSettingsMap.getFolderNameUnlessInbox(key: String): String? {
return (this[key] as? String)?.takeIf { !it.equals("INBOX", ignoreCase = true) }
}

private fun ValidatedSettings.Folder.isNotificationEnabled(
notifyFolderMode: FolderMode,
): Boolean {
val notifyClass = getEffectiveNotifyClass(settings)

return when (notifyFolderMode) {
FolderMode.NONE -> {
false
}

FolderMode.ALL -> {
true
}

FolderMode.FIRST_CLASS -> {
notifyClass == FolderClass.FIRST_CLASS
}

FolderMode.FIRST_AND_SECOND_CLASS -> {
notifyClass == FolderClass.FIRST_CLASS || notifyClass == FolderClass.SECOND_CLASS
}

FolderMode.NOT_SECOND_CLASS -> {
notifyClass != FolderClass.SECOND_CLASS
}
}
}

private fun getEffectiveNotifyClass(folderSettings: InternalSettingsMap): FolderClass {
return folderSettings.getFolderClass("notifyMode").takeIf { it != FolderClass.INHERITED }
?: folderSettings.getFolderClass("pushMode").takeIf { it != FolderClass.INHERITED }
?: folderSettings.getFolderClass("syncMode").takeIf { it != FolderClass.INHERITED }
?: folderSettings.getFolderClass("displayMode").takeIf { it != FolderClass.INHERITED }
?: FolderClass.NO_CLASS
}

private fun InternalSettingsMap.getFolderClass(key: String): FolderClass {
return this[key] as? FolderClass ?: FolderClass.NO_CLASS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ internal fun createFolderSettingsProvider(): FolderSettingsProvider {
visibleLimit = 25,
displayClass = FolderClass.NO_CLASS,
syncClass = FolderClass.INHERITED,
notifyClass = FolderClass.INHERITED,
isNotificationsEnabled = false,
pushClass = FolderClass.SECOND_CLASS,
inTopGroup = false,
integrate = false,
Expand Down
Loading