Fix exporting folder names for special folders

This commit is contained in:
cketti 2020-05-04 15:47:25 +02:00
parent d503935ac5
commit 7e958abb1d
3 changed files with 72 additions and 17 deletions

View file

@ -8,7 +8,8 @@ val preferencesModule = module {
contentResolver = get(),
backendManager = get(),
preferences = get(),
folderSettingsProvider = get()
folderSettingsProvider = get(),
folderRepositoryManager = get()
)
}
factory { FolderSettingsProvider(folderRepositoryManager = get()) }

View file

@ -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<String>, 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"
)
}
}

View file

@ -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