UserDirs.kt 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package eu.mjdev.desktop.helpers.system
  2. import java.io.File
  3. @Suppress("unused")
  4. class UserDirs(
  5. homeDir: File,
  6. configFile: File = File(homeDir, "/.config/user-dirs.dirs"),
  7. configFileContent: List<String> = runCatching { configFile.readLines().filter {
  8. (!it.startsWith("#")) && (!it.trim().isEmpty())
  9. }.map {
  10. it.replace("\$HOME", homeDir.absolutePath)
  11. .replace("\"", "")
  12. } }.getOrElse { emptyList() }
  13. ) : HashMap<String, File>() {
  14. init {
  15. configFileContent.map {
  16. it.split("=").let { Pair(it[0], it.getOrNull(1)) }
  17. }.forEach {
  18. put(it.first, File(it.second ?: ""))
  19. }
  20. }
  21. val desktopDirectory
  22. get() = this[XDG_DESKTOP_DIR]
  23. val downloadDirectory
  24. get() = this[XDG_DOWNLOAD_DIR]
  25. val templatesDirectory
  26. get() = this[XDG_TEMPLATES_DIR]
  27. val publicShareDirectory
  28. get() = this[XDG_PUBLICSHARE_DIR]
  29. val documentsDirectory
  30. get() = this[XDG_DOCUMENTS_DIR]
  31. val musicDirectory
  32. get() = this[XDG_MUSIC_DIR]
  33. val picturesDirectory
  34. get() = this[XDG_PICTURES_DIR]
  35. val videosDirectory
  36. get() = this[XDG_VIDEOS_DIR]
  37. companion object {
  38. const val XDG_DESKTOP_DIR = "XDG_DESKTOP_DIR"
  39. const val XDG_DOWNLOAD_DIR = "XDG_DOWNLOAD_DIR"
  40. const val XDG_TEMPLATES_DIR = "XDG_TEMPLATES_DIR"
  41. const val XDG_PUBLICSHARE_DIR = "XDG_PUBLICSHARE_DIR"
  42. const val XDG_DOCUMENTS_DIR = "XDG_DOCUMENTS_DIR"
  43. const val XDG_MUSIC_DIR = "XDG_MUSIC_DIR"
  44. const val XDG_PICTURES_DIR = "XDG_PICTURES_DIR"
  45. const val XDG_VIDEOS_DIR = "XDG_VIDEOS_DIR"
  46. }
  47. }