Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit f01b6e29 authored by Wolf-Martell Montwé's avatar Wolf-Martell Montwé
Browse files

refactor(core-preference): change AccountPreferenceSerializer to LegacyAccountStorageHandler

parent fd2dae86
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
package net.thunderbird.feature.account.storage.legacy

import net.thunderbird.feature.account.storage.legacy.AccountPreferenceSerializer
import net.thunderbird.feature.account.storage.legacy.mapper.DefaultAccountAvatarDataMapper
import net.thunderbird.feature.account.storage.legacy.mapper.DefaultAccountProfileDataMapper
import net.thunderbird.feature.account.storage.legacy.mapper.DefaultLegacyAccountWrapperDataMapper
@@ -27,7 +26,7 @@ val featureAccountStorageLegacyModule = module {
    factory { ServerSettingsDtoSerializer() }

    single {
        AccountPreferenceSerializer(
        LegacyAccountStorageHandler(
            serverSettingsDtoSerializer = get(),
            logger = get(),
        )
+32 −32
Original line number Diff line number Diff line
@@ -22,18 +22,19 @@ import net.thunderbird.feature.notification.NotificationSettings
import net.thunderbird.feature.notification.NotificationVibration
import net.thunderbird.feature.notification.VibratePattern

class AccountPreferenceSerializer(
class LegacyAccountStorageHandler(
    private val serverSettingsDtoSerializer: ServerSettingsDtoSerializer,
    private val logger: Logger,
) {
) : StorageHandler<LegacyAccount> {

    @Suppress("LongMethod", "MagicNumber")
    @Synchronized
    fun loadAccount(account: LegacyAccount, storage: Storage) {
        val accountUuid = account.uuid
        val keyGen = AccountKeyGenerator(account.id)
    override fun load(data: LegacyAccount, storage: Storage) {
        val keyGen = AccountKeyGenerator(data.id)

        with(account) {
        profileDtoStorageHandler.load(data, storage)

        with(data) {
            incomingServerSettings = serverSettingsDtoSerializer.deserialize(
                storage.getStringOrDefault(keyGen.create(INCOMING_SERVER_SETTINGS_KEY), ""),
            )
@@ -187,7 +188,8 @@ class AccountPreferenceSerializer(

            setSortAscending(sortType, storage.getBoolean(keyGen.create("sortAscending"), false))

            showPictures = getEnumStringPref<ShowPictures>(storage, keyGen.create("showPicturesEnum"), ShowPictures.NEVER)
            showPictures =
                getEnumStringPref<ShowPictures>(storage, keyGen.create("showPicturesEnum"), ShowPictures.NEVER)

            updateNotificationSettings {
                NotificationSettings(
@@ -223,7 +225,7 @@ class AccountPreferenceSerializer(
            folderPushMode = getEnumStringPref<FolderMode>(storage, keyGen.create("folderPushMode"), FolderMode.NONE)

            isSignatureBeforeQuotedText = storage.getBoolean(keyGen.create("signatureBeforeQuotedText"), false)
            replaceIdentities(loadIdentities(account.id, storage))
            replaceIdentities(loadIdentities(data.id, storage))

            openPgpProvider = storage.getStringOrDefault(keyGen.create("openPgpProvider"), "")
            openPgpKey = storage.getLong(keyGen.create("cryptoKey"), AccountDefaultsProvider.Companion.NO_OPENPGP_KEY)
@@ -304,16 +306,16 @@ class AccountPreferenceSerializer(

    @Suppress("LongMethod")
    @Synchronized
    fun save(editor: StorageEditor, storage: Storage, account: LegacyAccount) {
        val keyGen = AccountKeyGenerator(account.id)
    override fun save(data: LegacyAccount, storage: Storage, editor: StorageEditor) {
        val keyGen = AccountKeyGenerator(data.id)

        if (!storage.getStringOrDefault("accountUuids", "").contains(account.uuid)) {
        if (!storage.getStringOrDefault("accountUuids", "").contains(data.uuid)) {
            var accountUuids = storage.getStringOrDefault("accountUuids", "")
            accountUuids += (if (accountUuids.isNotEmpty()) "," else "") + account.uuid
            accountUuids += (if (accountUuids.isNotEmpty()) "," else "") + data.uuid
            editor.putString("accountUuids", accountUuids)
        }

        with(account) {
        with(data) {
            editor.putString(
                keyGen.create(INCOMING_SERVER_SETTINGS_KEY),
                serverSettingsDtoSerializer.serialize(incomingServerSettings),
@@ -414,14 +416,14 @@ class AccountPreferenceSerializer(
            editor.putBoolean(keyGen.create("migrateToOAuth"), shouldMigrateToOAuth)
        }

        saveIdentities(account, storage, editor)
        saveIdentities(data, storage, editor)
    }

    @Suppress("LongMethod")
    @Synchronized
    fun delete(editor: StorageEditor, storage: Storage, account: LegacyAccount) {
        val keyGen = AccountKeyGenerator(account.id)
        val accountUuid = account.uuid
    override fun delete(data: LegacyAccount, storage: Storage, editor: StorageEditor) {
        val keyGen = AccountKeyGenerator(data.id)
        val accountUuid = data.uuid

        // Get the list of account UUIDs
        val uuids = storage
@@ -535,17 +537,17 @@ class AccountPreferenceSerializer(
        editor.remove(keyGen.create("sendClientInfo"))
        editor.remove(keyGen.create("migrateToOAuth"))

        deleteIdentities(account, storage, editor)
        deleteIdentities(data, storage, editor)
        // TODO: Remove preference settings that may exist for individual folders in the account.
    }

    @Synchronized
    private fun saveIdentities(account: LegacyAccount, storage: Storage, editor: StorageEditor) {
        deleteIdentities(account, storage, editor)
    private fun saveIdentities(data: LegacyAccount, storage: Storage, editor: StorageEditor) {
        deleteIdentities(data, storage, editor)
        var ident = 0
        val keyGen = AccountKeyGenerator(account.id)
        val keyGen = AccountKeyGenerator(data.id)

        with(account) {
        with(data) {
            for (identity in identities) {
                editor.putString(keyGen.create("$IDENTITY_NAME_KEY.$ident"), identity.name)
                editor.putString(keyGen.create("$IDENTITY_EMAIL_KEY.$ident"), identity.email)
@@ -559,8 +561,8 @@ class AccountPreferenceSerializer(
    }

    @Synchronized
    private fun deleteIdentities(account: LegacyAccount, storage: Storage, editor: StorageEditor) {
        val keyGen = AccountKeyGenerator(account.id)
    private fun deleteIdentities(data: LegacyAccount, storage: Storage, editor: StorageEditor) {
        val keyGen = AccountKeyGenerator(data.id)

        var identityIndex = 0
        var gotOne: Boolean
@@ -580,15 +582,15 @@ class AccountPreferenceSerializer(
        } while (gotOne)
    }

    fun move(editor: StorageEditor, account: LegacyAccount, storage: Storage, newPosition: Int) {
    fun move(data: LegacyAccount, storage: Storage, editor: StorageEditor, newPosition: Int) {
        val accountUuids = storage.getStringOrDefault("accountUuids", "").split(",").filter { it.isNotEmpty() }
        val oldPosition = accountUuids.indexOf(account.uuid)
        val oldPosition = accountUuids.indexOf(data.uuid)
        if (oldPosition == -1 || oldPosition == newPosition) return

        val newAccountUuidsString = accountUuids.toMutableList()
            .apply {
                removeAt(oldPosition)
                add(newPosition, account.uuid)
                add(newPosition, data.uuid)
            }
            .joinToString(separator = ",")

@@ -599,11 +601,9 @@ class AccountPreferenceSerializer(
        return try {
            storage.getEnumOrDefault<T>(key, defaultEnum)
        } catch (ex: IllegalArgumentException) {
            logger.warn(
                null,
                "Unable to convert preference key [$key] to enum of type defaultEnum: $defaultEnum",
                ex,
            )
            logger.warn(throwable = ex) {
                "Unable to convert preference key [$key] to enum of type defaultEnum: $defaultEnum"
            }

            defaultEnum
        }
+43 −0
Original line number Diff line number Diff line
package net.thunderbird.feature.account.storage.legacy

import androidx.annotation.Discouraged
import net.thunderbird.core.preference.storage.Storage
import net.thunderbird.core.preference.storage.StorageEditor

/**
 * Represents a storage handler for a specific data type.
 *
 * @param T The type of data that this handler can handle.
 */
@Discouraged(
    message = "This interface is only used to encapsulate the [LegacyAccount] storage handling.",
)
interface StorageHandler<T> {

    /**
     * Loads the data from the storage into the provided object.
     *
     * @param data The object to load the data into.
     * @param storage The storage to load the data from.
     */
    fun load(data: T, storage: Storage)

    /**
     * Saves the data from the provided object to the storage.
     *
     * @param data The object to save the data from.
     * @param storage The storage to save the data to.
     * @param editor The storage editor to use for saving the data.
     */
    fun save(data: T, storage: Storage, editor: StorageEditor)

    /**
     * Deletes the data from the storage.
     *
     * @param data The data to delete.
     * @param storage The storage to delete the data from.
     * @param editor The storage editor to use for deleting the data.
     */
    fun delete(data: T, storage: Storage, editor: StorageEditor)
}
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ val mainModule = module {
        Preferences(
            storagePersister = get(),
            localStoreProvider = get(),
            accountPreferenceSerializer = get(),
            legacyAccountStorageHandler = get(),
            accountDefaultsProvider = get(),
        )
    }
+6 −6
Original line number Diff line number Diff line
@@ -28,13 +28,13 @@ import net.thunderbird.core.logging.legacy.Log
import net.thunderbird.core.preference.storage.Storage
import net.thunderbird.core.preference.storage.StorageEditor
import net.thunderbird.core.preference.storage.StoragePersister
import net.thunderbird.feature.account.storage.legacy.AccountPreferenceSerializer
import net.thunderbird.feature.account.storage.legacy.LegacyAccountStorageHandler

@Suppress("MaxLineLength")
class Preferences internal constructor(
    private val storagePersister: StoragePersister,
    private val localStoreProvider: LocalStoreProvider,
    private val accountPreferenceSerializer: AccountPreferenceSerializer,
    private val legacyAccountStorageHandler: LegacyAccountStorageHandler,
    private val backgroundDispatcher: CoroutineDispatcher = Dispatchers.IO,
    private val accountDefaultsProvider: AccountDefaultsProvider,
) : AccountManager {
@@ -91,7 +91,7 @@ class Preferences internal constructor(
                        uuid,
                        K9::isSensitiveDebugLoggingEnabled,
                    )
                    accountPreferenceSerializer.loadAccount(account, storage)
                    legacyAccountStorageHandler.load(account, storage)

                    accounts[uuid] = account
                    accountsInOrder.add(account)
@@ -204,7 +204,7 @@ class Preferences internal constructor(
            accountsInOrder.remove(account)

            val storageEditor = createStorageEditor()
            accountPreferenceSerializer.delete(storageEditor, storage, account)
            legacyAccountStorageHandler.delete(account, storage, storageEditor)
            storageEditor.commit()

            if (account === newAccount) {
@@ -225,7 +225,7 @@ class Preferences internal constructor(

        synchronized(accountLock) {
            val editor = createStorageEditor()
            accountPreferenceSerializer.save(editor, storage, account)
            legacyAccountStorageHandler.save(account, storage, editor)
            editor.commit()

            loadAccounts()
@@ -272,7 +272,7 @@ class Preferences internal constructor(
    override fun moveAccount(account: LegacyAccount, newPosition: Int) {
        synchronized(accountLock) {
            val storageEditor = createStorageEditor()
            accountPreferenceSerializer.move(storageEditor, account, storage, newPosition)
            legacyAccountStorageHandler.move(account, storage, storageEditor, newPosition)
            storageEditor.commit()

            loadAccounts()
Loading