0
0
mirror of https://github.com/thunderbird/thunderbird-android.git synced 2024-09-19 19:52:14 +02:00

Pass settings descriptions and upgraders via constructor

This commit is contained in:
cketti 2024-09-01 13:25:42 +02:00
parent 6da770f29d
commit d83e2d19b0
15 changed files with 149 additions and 139 deletions

View File

@ -46,7 +46,7 @@ import static com.fsck.k9.preferences.upgrader.AccountSettingsUpgraderTo53.FOLDE
class AccountSettingsDescriptions {
static final Map<String, TreeMap<Integer, SettingsDescription<?>>> SETTINGS;
private static final Map<Integer, SettingsUpgrader> UPGRADERS;
static final Map<Integer, SettingsUpgrader> UPGRADERS;
static {
Map<String, TreeMap<Integer, SettingsDescription<?>>> s = new LinkedHashMap<>();
@ -302,10 +302,6 @@ class AccountSettingsDescriptions {
return Settings.validate(version, SETTINGS, importedSettings, useDefaultValues);
}
public static Map<String, Object> upgrade(int version, Map<String, Object> validatedSettings) {
return SettingsUpgradeHelper.upgrade(version, UPGRADERS, SETTINGS, validatedSettings);
}
public static Map<String, String> convert(Map<String, Object> settings) {
return Settings.convert(settings, SETTINGS);
}

View File

@ -4,15 +4,18 @@ internal class AccountSettingsUpgrader(
private val identitySettingsUpgrader: IdentitySettingsUpgrader,
private val folderSettingsUpgrader: FolderSettingsUpgrader,
private val serverSettingsUpgrader: ServerSettingsUpgrader,
private val latestVersion: Int = Settings.VERSION,
private val settingsDescriptions: SettingsDescriptions = AccountSettingsDescriptions.SETTINGS,
private val upgraders: Map<Int, SettingsUpgrader> = AccountSettingsDescriptions.UPGRADERS,
) {
fun upgrade(contentVersion: Int, account: ValidatedSettings.Account): ValidatedSettings.Account {
if (contentVersion == Settings.VERSION) {
if (contentVersion == latestVersion) {
return account
}
return account.copy(
settings = AccountSettingsDescriptions.upgrade(contentVersion, account.settings),
settings = upgradeAccountSettings(contentVersion, account.settings),
incoming = serverSettingsUpgrader.upgrade(contentVersion, account.incoming),
outgoing = serverSettingsUpgrader.upgrade(contentVersion, account.outgoing),
identities = upgradeIdentities(contentVersion, account.identities),
@ -20,6 +23,13 @@ internal class AccountSettingsUpgrader(
)
}
private fun upgradeAccountSettings(
contentVersion: Int,
settings: InternalSettingsMap,
): InternalSettingsMap {
return SettingsUpgradeHelper.upgrade(contentVersion, upgraders, settingsDescriptions, settings)
}
private fun upgradeIdentities(
contentVersion: Int,
identities: List<ValidatedSettings.Identity>,

View File

@ -16,7 +16,7 @@ import com.fsck.k9.preferences.Settings.V;
class FolderSettingsDescriptions {
static final Map<String, TreeMap<Integer, SettingsDescription<?>>> SETTINGS;
private static final Map<Integer, SettingsUpgrader> UPGRADERS;
static final Map<Integer, SettingsUpgrader> UPGRADERS;
static {
Map<String, TreeMap<Integer, SettingsDescription<?>>> s = new LinkedHashMap<>();
@ -57,10 +57,6 @@ class FolderSettingsDescriptions {
return Settings.validate(version, SETTINGS, importedSettings, useDefaultValues);
}
public static Map<String, Object> upgrade(int version, Map<String, Object> validatedSettings) {
return SettingsUpgradeHelper.upgrade(version, UPGRADERS, SETTINGS, validatedSettings);
}
public static Map<String, String> convert(Map<String, Object> settings) {
return Settings.convert(settings, SETTINGS);
}

View File

@ -1,12 +1,21 @@
package com.fsck.k9.preferences
internal class FolderSettingsUpgrader {
internal class FolderSettingsUpgrader(
private val latestVersion: Int = Settings.VERSION,
private val settingsDescriptions: SettingsDescriptions = FolderSettingsDescriptions.SETTINGS,
private val upgraders: Map<Int, SettingsUpgrader> = FolderSettingsDescriptions.UPGRADERS,
) {
fun upgrade(contentVersion: Int, folder: ValidatedSettings.Folder): ValidatedSettings.Folder {
if (contentVersion == Settings.VERSION) {
if (contentVersion == latestVersion) {
return folder
}
val upgradedSettings = FolderSettingsDescriptions.upgrade(contentVersion, folder.settings)
val upgradedSettings = SettingsUpgradeHelper.upgrade(
contentVersion,
upgraders,
settingsDescriptions,
folder.settings,
)
return folder.copy(settings = upgradedSettings)
}

View File

@ -18,7 +18,7 @@ import com.fsck.k9.preferences.Settings.V;
class IdentitySettingsDescriptions {
static final Map<String, TreeMap<Integer, SettingsDescription<?>>> SETTINGS;
private static final Map<Integer, SettingsUpgrader> UPGRADERS;
static final Map<Integer, SettingsUpgrader> UPGRADERS;
static {
Map<String, TreeMap<Integer, SettingsDescription<?>>> s = new LinkedHashMap<>();
@ -50,10 +50,6 @@ class IdentitySettingsDescriptions {
return Settings.validate(version, SETTINGS, importedSettings, useDefaultValues);
}
public static Map<String, Object> upgrade(int version, Map<String, Object> validatedSettings) {
return SettingsUpgradeHelper.upgrade(version, UPGRADERS, SETTINGS, validatedSettings);
}
public static Map<String, String> convert(Map<String, Object> settings) {
return Settings.convert(settings, SETTINGS);
}

View File

@ -1,12 +1,21 @@
package com.fsck.k9.preferences
internal class IdentitySettingsUpgrader {
internal class IdentitySettingsUpgrader(
private val latestVersion: Int = Settings.VERSION,
private val settingsDescriptions: SettingsDescriptions = IdentitySettingsDescriptions.SETTINGS,
private val upgraders: Map<Int, SettingsUpgrader> = IdentitySettingsDescriptions.UPGRADERS,
) {
fun upgrade(contentVersion: Int, identity: ValidatedSettings.Identity): ValidatedSettings.Identity {
if (contentVersion == Settings.VERSION) {
if (contentVersion == latestVersion) {
return identity
}
val upgradedSettings = IdentitySettingsDescriptions.upgrade(contentVersion, identity.settings)
val upgradedSettings = SettingsUpgradeHelper.upgrade(
contentVersion,
upgraders,
settingsDescriptions,
identity.settings,
)
return identity.copy(settings = upgradedSettings)
}

View File

@ -11,90 +11,84 @@ import com.fsck.k9.preferences.upgrader.ServerSettingsUpgraderTo95
* settings to the latest content version.
*/
@Suppress("MagicNumber")
internal class ServerSettingsDescriptions {
val settings: SettingsDescriptions by lazy {
mapOf(
HOST to versions(
1 to StringSetting(null),
),
PORT to versions(
1 to IntegerRangeSetting(1, 65535, -1),
),
CONNECTION_SECURITY to versions(
1 to StringEnumSetting(
defaultValue = "SSL_TLS_REQUIRED",
values = setOf(
"NONE",
"STARTTLS_OPTIONAL",
"STARTTLS_REQUIRED",
"SSL_TLS_OPTIONAL",
"SSL_TLS_REQUIRED",
),
),
92 to NoDefaultStringEnumSetting(
values = setOf(
"NONE",
"STARTTLS_REQUIRED",
"SSL_TLS_REQUIRED",
),
),
),
AUTHENTICATION_TYPE to versions(
1 to NoDefaultStringEnumSetting(
values = setOf(
"PLAIN",
"CRAM_MD5",
"EXTERNAL",
"XOAUTH2",
"AUTOMATIC",
"LOGIN",
),
),
94 to NoDefaultStringEnumSetting(
values = setOf(
"PLAIN",
"CRAM_MD5",
"EXTERNAL",
"XOAUTH2",
),
),
95 to NoDefaultStringEnumSetting(
values = setOf(
"PLAIN",
"CRAM_MD5",
"EXTERNAL",
"XOAUTH2",
"NONE",
),
),
),
USERNAME to versions(
1 to StringSetting(""),
),
PASSWORD to versions(
1 to StringSetting(null),
),
CLIENT_CERTIFICATE_ALIAS to versions(
1 to StringSetting(null),
),
)
}
internal object ServerSettingsDescriptions {
const val HOST = "host"
const val PORT = "port"
const val CONNECTION_SECURITY = "connectionSecurity"
const val AUTHENTICATION_TYPE = "authenticationType"
const val USERNAME = "username"
const val PASSWORD = "password"
const val CLIENT_CERTIFICATE_ALIAS = "clientCertificateAlias"
val upgraders: Map<Int, SettingsUpgrader> by lazy {
mapOf(
92 to ServerSettingsUpgraderTo92(),
94 to ServerSettingsUpgraderTo94(),
95 to ServerSettingsUpgraderTo95(),
)
}
val SETTINGS: SettingsDescriptions = mapOf(
HOST to versions(
1 to StringSetting(null),
),
PORT to versions(
1 to IntegerRangeSetting(1, 65535, -1),
),
CONNECTION_SECURITY to versions(
1 to StringEnumSetting(
defaultValue = "SSL_TLS_REQUIRED",
values = setOf(
"NONE",
"STARTTLS_OPTIONAL",
"STARTTLS_REQUIRED",
"SSL_TLS_OPTIONAL",
"SSL_TLS_REQUIRED",
),
),
92 to NoDefaultStringEnumSetting(
values = setOf(
"NONE",
"STARTTLS_REQUIRED",
"SSL_TLS_REQUIRED",
),
),
),
AUTHENTICATION_TYPE to versions(
1 to NoDefaultStringEnumSetting(
values = setOf(
"PLAIN",
"CRAM_MD5",
"EXTERNAL",
"XOAUTH2",
"AUTOMATIC",
"LOGIN",
),
),
94 to NoDefaultStringEnumSetting(
values = setOf(
"PLAIN",
"CRAM_MD5",
"EXTERNAL",
"XOAUTH2",
),
),
95 to NoDefaultStringEnumSetting(
values = setOf(
"PLAIN",
"CRAM_MD5",
"EXTERNAL",
"XOAUTH2",
"NONE",
),
),
),
USERNAME to versions(
1 to StringSetting(""),
),
PASSWORD to versions(
1 to StringSetting(null),
),
CLIENT_CERTIFICATE_ALIAS to versions(
1 to StringSetting(null),
),
)
companion object {
const val HOST = "host"
const val PORT = "port"
const val CONNECTION_SECURITY = "connectionSecurity"
const val AUTHENTICATION_TYPE = "authenticationType"
const val USERNAME = "username"
const val PASSWORD = "password"
const val CLIENT_CERTIFICATE_ALIAS = "clientCertificateAlias"
}
val UPGRADERS: Map<Int, SettingsUpgrader> = mapOf(
92 to ServerSettingsUpgraderTo92(),
94 to ServerSettingsUpgraderTo94(),
95 to ServerSettingsUpgraderTo95(),
)
}

View File

@ -1,17 +1,19 @@
package com.fsck.k9.preferences
internal class ServerSettingsUpgrader(
private val serverSettingsDescriptions: ServerSettingsDescriptions = ServerSettingsDescriptions(),
private val latestVersion: Int = Settings.VERSION,
private val settingsDescriptions: SettingsDescriptions = ServerSettingsDescriptions.SETTINGS,
private val upgraders: Map<Int, SettingsUpgrader> = ServerSettingsDescriptions.UPGRADERS,
) {
fun upgrade(contentVersion: Int, server: ValidatedSettings.Server): ValidatedSettings.Server {
if (contentVersion == Settings.VERSION) {
if (contentVersion == latestVersion) {
return server
}
val upgradedSettings = SettingsUpgradeHelper.upgrade(
contentVersion,
serverSettingsDescriptions.upgraders,
serverSettingsDescriptions.settings,
upgraders,
settingsDescriptions,
server.settings,
)

View File

@ -1,22 +1,22 @@
package com.fsck.k9.preferences
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.AUTHENTICATION_TYPE
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.CLIENT_CERTIFICATE_ALIAS
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.CONNECTION_SECURITY
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.HOST
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.PASSWORD
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.PORT
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.USERNAME
import com.fsck.k9.preferences.ServerSettingsDescriptions.AUTHENTICATION_TYPE
import com.fsck.k9.preferences.ServerSettingsDescriptions.CLIENT_CERTIFICATE_ALIAS
import com.fsck.k9.preferences.ServerSettingsDescriptions.CONNECTION_SECURITY
import com.fsck.k9.preferences.ServerSettingsDescriptions.HOST
import com.fsck.k9.preferences.ServerSettingsDescriptions.PASSWORD
import com.fsck.k9.preferences.ServerSettingsDescriptions.PORT
import com.fsck.k9.preferences.ServerSettingsDescriptions.USERNAME
import com.fsck.k9.preferences.ServerTypeConverter.toServerSettingsType
import com.fsck.k9.preferences.Settings.InvalidSettingValueException
internal class ServerSettingsValidator(
private val serverSettingsDescriptions: ServerSettingsDescriptions = ServerSettingsDescriptions(),
private val settingsDescriptions: SettingsDescriptions = ServerSettingsDescriptions.SETTINGS,
) {
fun validate(contentVersion: Int, server: SettingsFile.Server): ValidatedSettings.Server {
val settings = convertServerSettingsToMap(server)
val validatedSettings = Settings.validate(contentVersion, serverSettingsDescriptions.settings, settings, true)
val validatedSettings = Settings.validate(contentVersion, settingsDescriptions, settings, true)
if (validatedSettings[AUTHENTICATION_TYPE] !is String) {
throw InvalidSettingValueException("Missing '$AUTHENTICATION_TYPE' value")

View File

@ -4,13 +4,13 @@ import com.fsck.k9.ServerSettingsSerializer
import com.fsck.k9.mail.AuthType
import com.fsck.k9.mail.ConnectionSecurity
import com.fsck.k9.mail.ServerSettings
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.AUTHENTICATION_TYPE
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.CLIENT_CERTIFICATE_ALIAS
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.CONNECTION_SECURITY
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.HOST
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.PASSWORD
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.PORT
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.USERNAME
import com.fsck.k9.preferences.ServerSettingsDescriptions.AUTHENTICATION_TYPE
import com.fsck.k9.preferences.ServerSettingsDescriptions.CLIENT_CERTIFICATE_ALIAS
import com.fsck.k9.preferences.ServerSettingsDescriptions.CONNECTION_SECURITY
import com.fsck.k9.preferences.ServerSettingsDescriptions.HOST
import com.fsck.k9.preferences.ServerSettingsDescriptions.PASSWORD
import com.fsck.k9.preferences.ServerSettingsDescriptions.PORT
import com.fsck.k9.preferences.ServerSettingsDescriptions.USERNAME
internal class ServerSettingsWriter(
private val serverSettingsSerializer: ServerSettingsSerializer,

View File

@ -1,10 +1,10 @@
package com.fsck.k9.preferences
import com.fsck.k9.helper.mapCollectionToSet
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.AUTHENTICATION_TYPE
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.HOST
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.PASSWORD
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.USERNAME
import com.fsck.k9.preferences.ServerSettingsDescriptions.AUTHENTICATION_TYPE
import com.fsck.k9.preferences.ServerSettingsDescriptions.HOST
import com.fsck.k9.preferences.ServerSettingsDescriptions.PASSWORD
import com.fsck.k9.preferences.ServerSettingsDescriptions.USERNAME
import com.fsck.k9.preferences.Settings.InvalidSettingValueException
import java.io.InputStream
import timber.log.Timber

View File

@ -1,6 +1,6 @@
package com.fsck.k9.preferences.upgrader
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.CONNECTION_SECURITY
import com.fsck.k9.preferences.ServerSettingsDescriptions.CONNECTION_SECURITY
import com.fsck.k9.preferences.SettingsUpgrader
class ServerSettingsUpgraderTo92 : SettingsUpgrader {

View File

@ -1,7 +1,7 @@
package com.fsck.k9.preferences.upgrader
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.AUTHENTICATION_TYPE
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.CONNECTION_SECURITY
import com.fsck.k9.preferences.ServerSettingsDescriptions.AUTHENTICATION_TYPE
import com.fsck.k9.preferences.ServerSettingsDescriptions.CONNECTION_SECURITY
import com.fsck.k9.preferences.SettingsUpgrader
/**

View File

@ -1,8 +1,8 @@
package com.fsck.k9.preferences.upgrader
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.AUTHENTICATION_TYPE
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.PASSWORD
import com.fsck.k9.preferences.ServerSettingsDescriptions.Companion.USERNAME
import com.fsck.k9.preferences.ServerSettingsDescriptions.AUTHENTICATION_TYPE
import com.fsck.k9.preferences.ServerSettingsDescriptions.PASSWORD
import com.fsck.k9.preferences.ServerSettingsDescriptions.USERNAME
import com.fsck.k9.preferences.SettingsUpgrader
/**

View File

@ -8,9 +8,7 @@ import assertk.assertions.isFalse
import assertk.assertions.isInstanceOf
import assertk.fail
import com.fsck.k9.preferences.Settings.BooleanSetting
import com.fsck.k9.preferences.Settings.SettingsDescription
import com.fsck.k9.preferences.Settings.StringSetting
import java.util.TreeMap
import kotlin.test.Test
class SettingsUpgradeHelperTest {