Configured OS specific directories for user data

This commit is contained in:
dankito 2024-09-27 03:31:49 +02:00
parent 0dac13dc43
commit b802f5b48f
1 changed files with 33 additions and 2 deletions

View File

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