From 7e958abb1d6dcba8bf4634677672c6c5c7039460 Mon Sep 17 00:00:00 2001 From: cketti Date: Mon, 4 May 2020 15:47:25 +0200 Subject: [PATCH] Fix exporting folder names for special folders --- .../com/fsck/k9/preferences/KoinModule.kt | 3 +- .../fsck/k9/preferences/SettingsExporter.kt | 81 +++++++++++++++---- .../k9/preferences/SettingsExporterTest.kt | 5 +- 3 files changed, 72 insertions(+), 17 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/preferences/KoinModule.kt b/app/core/src/main/java/com/fsck/k9/preferences/KoinModule.kt index 0f09967c3..a89f71de0 100644 --- a/app/core/src/main/java/com/fsck/k9/preferences/KoinModule.kt +++ b/app/core/src/main/java/com/fsck/k9/preferences/KoinModule.kt @@ -8,7 +8,8 @@ val preferencesModule = module { contentResolver = get(), backendManager = get(), preferences = get(), - folderSettingsProvider = get() + folderSettingsProvider = get(), + folderRepositoryManager = get() ) } factory { FolderSettingsProvider(folderRepositoryManager = get()) } diff --git a/app/core/src/main/java/com/fsck/k9/preferences/SettingsExporter.kt b/app/core/src/main/java/com/fsck/k9/preferences/SettingsExporter.kt index 1c2c069e7..29d6a5c55 100644 --- a/app/core/src/main/java/com/fsck/k9/preferences/SettingsExporter.kt +++ b/app/core/src/main/java/com/fsck/k9/preferences/SettingsExporter.kt @@ -10,6 +10,8 @@ import com.fsck.k9.AccountPreferenceSerializer.Companion.IDENTITY_EMAIL_KEY import com.fsck.k9.AccountPreferenceSerializer.Companion.IDENTITY_NAME_KEY import com.fsck.k9.Preferences import com.fsck.k9.backend.BackendManager +import com.fsck.k9.mailstore.FolderRepository +import com.fsck.k9.mailstore.FolderRepositoryManager import com.fsck.k9.preferences.ServerTypeConverter.fromServerSettingsType import com.fsck.k9.preferences.Settings.InvalidSettingValueException import com.fsck.k9.preferences.Settings.SettingsDescription @@ -23,7 +25,8 @@ class SettingsExporter( private val contentResolver: ContentResolver, private val backendManager: BackendManager, private val preferences: Preferences, - private val folderSettingsProvider: FolderSettingsProvider + private val folderSettingsProvider: FolderSettingsProvider, + private val folderRepositoryManager: FolderRepositoryManager ) { @Throws(SettingsImportExportException::class) fun exportToUri(includeGlobals: Boolean, accountUuids: Set, uri: Uri) { @@ -210,23 +213,14 @@ class SettingsExporter( } } - val versionedSetting = AccountSettingsDescriptions.SETTINGS[keyPart] - if (versionedSetting != null) { - val highestVersion = versionedSetting.lastKey() - - val setting = versionedSetting[highestVersion] - if (setting != null) { - // Only export account settings that can be found in AccountSettings.SETTINGS - try { - writeKeyAndPrettyValueFromSetting(serializer, keyPart, setting, valueString) - } catch (e: InvalidSettingValueException) { - Timber.w("Account setting \"%s\" (%s) has invalid value \"%s\" in preference storage. " + - "This shouldn't happen!", keyPart, account.description, valueString) - } - } + if (keyPart !in FOLDER_NAME_KEYS) { + writeAccountSettingIfValid(serializer, keyPart, valueString, account) } } + val folderRepository = folderRepositoryManager.getFolderRepository(account) + writeFolderNameSettings(account, folderRepository, serializer) + serializer.endTag(null, SETTINGS_ELEMENT) if (identities.isNotEmpty()) { @@ -252,6 +246,54 @@ class SettingsExporter( serializer.endTag(null, ACCOUNT_ELEMENT) } + private fun writeAccountSettingIfValid( + serializer: XmlSerializer, + keyPart: String, + valueString: String, + account: Account + ) { + val versionedSetting = AccountSettingsDescriptions.SETTINGS[keyPart] + if (versionedSetting != null) { + val highestVersion = versionedSetting.lastKey() + + val setting = versionedSetting[highestVersion] + if (setting != null) { + // Only export account settings that can be found in AccountSettings.SETTINGS + try { + writeKeyAndPrettyValueFromSetting(serializer, keyPart, setting, valueString) + } catch (e: InvalidSettingValueException) { + Timber.w( + "Account setting \"%s\" (%s) has invalid value \"%s\" in preference storage. " + + "This shouldn't happen!", keyPart, account.description, valueString + ) + } + } + } + } + + private fun writeFolderNameSettings( + account: Account, + folderRepository: FolderRepository, + serializer: XmlSerializer + ) { + fun writeFolderNameSetting(key: String, folderId: Long?, importedFolderServerId: String?) { + val folderServerId = folderId?.let { + folderRepository.getFolderServerId(folderId) + } ?: importedFolderServerId + + if (folderServerId != null) { + writeAccountSettingIfValid(serializer, key, folderServerId, account) + } + } + + writeFolderNameSetting("autoExpandFolderName", account.autoExpandFolderId, account.importedAutoExpandFolder) + writeFolderNameSetting("archiveFolderName", account.archiveFolderId, account.importedArchiveFolder) + writeFolderNameSetting("draftsFolderName", account.draftsFolderId, account.importedDraftsFolder) + writeFolderNameSetting("sentFolderName", account.sentFolderId, account.importedSentFolder) + writeFolderNameSetting("spamFolderName", account.spamFolderId, account.importedSpamFolder) + writeFolderNameSetting("trashFolderName", account.trashFolderId, account.importedTrashFolder) + } + private fun writeIdentity( serializer: XmlSerializer, accountUuid: String, @@ -441,5 +483,14 @@ class SettingsExporter( const val NAME_ELEMENT = "name" const val EMAIL_ELEMENT = "email" const val DESCRIPTION_ELEMENT = "description" + + private val FOLDER_NAME_KEYS = setOf( + "autoExpandFolderName", + "archiveFolderName", + "draftsFolderName", + "sentFolderName", + "spamFolderName", + "trashFolderName" + ) } } diff --git a/app/core/src/test/java/com/fsck/k9/preferences/SettingsExporterTest.kt b/app/core/src/test/java/com/fsck/k9/preferences/SettingsExporterTest.kt index 952166e03..e9694668a 100644 --- a/app/core/src/test/java/com/fsck/k9/preferences/SettingsExporterTest.kt +++ b/app/core/src/test/java/com/fsck/k9/preferences/SettingsExporterTest.kt @@ -3,6 +3,7 @@ package com.fsck.k9.preferences import com.fsck.k9.K9RobolectricTest import com.fsck.k9.Preferences import com.fsck.k9.backend.BackendManager +import com.fsck.k9.mailstore.FolderRepositoryManager import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import org.jdom2.Document @@ -19,11 +20,13 @@ class SettingsExporterTest : K9RobolectricTest() { private val preferences: Preferences by inject() private val backendManager: BackendManager by inject() private val folderSettingsProvider: FolderSettingsProvider by inject() + private val folderRepositoryManager: FolderRepositoryManager by inject() private val settingsExporter = SettingsExporter( contentResolver, backendManager, preferences, - folderSettingsProvider + folderSettingsProvider, + folderRepositoryManager ) @Test