From b802f5b48f29f7e583ef32ce5c485550104ae9e5 Mon Sep 17 00:00:00 2001 From: dankito Date: Fri, 27 Sep 2024 03:31:49 +0200 Subject: [PATCH] Configured OS specific directories for user data --- .../persistence/BankingRepository.jvm.kt | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/BankingPersistence/src/jvmMain/kotlin/net/codinux/banking/persistence/BankingRepository.jvm.kt b/BankingPersistence/src/jvmMain/kotlin/net/codinux/banking/persistence/BankingRepository.jvm.kt index 66e79d7..4c6f2aa 100644 --- a/BankingPersistence/src/jvmMain/kotlin/net/codinux/banking/persistence/BankingRepository.jvm.kt +++ b/BankingPersistence/src/jvmMain/kotlin/net/codinux/banking/persistence/BankingRepository.jvm.kt @@ -36,9 +36,40 @@ private fun determineDataDirectory(): File { val dataDir = if (Files.isWritable(currentDir)) { // couldn't believe it, but java.io.File returned folder is writable for "C:\\Program Files\\" File(currentDir.absolutePathString(), "data") } else { // otherwise use .bankmeister dir in user's home dir (the default for releases) - val userHome = System.getProperty("user.home") - File(userHome, ".bankmeister") + File(determineOsDependentUserDataDir(), ".bankmeister") } return dataDir.also { it.mkdirs() } +} + +private fun determineOsDependentUserDataDir(): File { + val userHomeString = System.getProperty("user.home") + val userHome = File(userHomeString) + val windowsLocalAppDataDir = System.getenv("LOCALAPPDATA")?.takeUnless { it.isBlank() } + + return if (windowsLocalAppDataDir != null) { + File(windowsLocalAppDataDir) + } else if (userHomeString.startsWith("/")) { + val osName = System.getProperty("os.name") + if (osName.contains("mac", true) || osName.contains("darwin", true)) { // macOS + File(userHome, "Library/Application Support") + } else if (osName.contains("nux")) { // Linux + val localShareDirectory = File(userHome, ".local/share") + val configDir = File(userHome, ".config") + + if (localShareDirectory.exists()) { + localShareDirectory + } else if (configDir.exists()) { + configDir + } else { + userHome + } + } else { // unknown + userHome + } + } else if (userHomeString.length > 3 && userHomeString[1] == ':' && userHomeString[2] == '\\') { // Windows, but LOCALAPPDATA is not set + userHome // File(userHome, "AppData\Local") + } else { + userHome + } } \ No newline at end of file