2
0

DesktopFile.kt 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package eu.mjdev.desktop.data
  2. import org.ini4j.Ini
  3. import java.io.File
  4. @Suppress("ConstPropertyName", "unused")
  5. class DesktopFile(
  6. val file: File
  7. ) {
  8. private val content
  9. get() = runCatching { if (file.exists()) Ini(file) else null }.getOrNull()
  10. private val props
  11. get() = content?.get("Desktop Entry")
  12. val type
  13. get() = props?.get(Prop_Type) ?: ""
  14. val version
  15. get() = props?.get(Prop_Version) ?: ""
  16. val name
  17. get() = props?.get(Prop_Name) ?: ""
  18. val comment
  19. get() = props?.get(Prop_Comment) ?: ""
  20. val path
  21. get() = props?.get(Prop_Path) ?: ""
  22. val exec
  23. get() = props?.get(Prop_Exec) ?: ""
  24. val icon
  25. get() = props?.get(Prop_Icon) ?: ""
  26. val categories
  27. get() = (props?.get(Prop_Categories) ?: "").split(";").toList()
  28. val notifyOnStart
  29. get() = (props?.get(Prop_StartupNotify) ?: "false").asBoolean()
  30. val runInTerminal
  31. get() = (props?.get(Prop_Terminal) ?: "false").asBoolean()
  32. companion object {
  33. const val Prop_Type = "Type" // string "Application"
  34. const val Prop_Version = "Version" // string
  35. const val Prop_Path = "Path" // string
  36. const val Prop_Name = "Name" // string
  37. const val Prop_Comment = "Comment" // string
  38. const val Prop_Exec = "Exec" // string
  39. const val Prop_StartupNotify = "StartupNotify" // boolean
  40. const val Prop_Terminal = "Terminal" // boolean
  41. const val Prop_Icon = "Icon" // string
  42. const val Prop_Categories = "Categories" // string delimited ;
  43. const val Prop_GenericName = "GenericName"
  44. const val Prop_MimeType = "MimeType" // string delimited ;
  45. const val Prop_Actions = "Actions"
  46. private fun String.asBoolean(): Boolean = when (this.lowercase()) {
  47. "true" -> true
  48. "1" -> true
  49. else -> false
  50. }
  51. fun create(path: String): DesktopFile = create(File(path).apply {
  52. if (!exists()) createNewFile()
  53. })
  54. fun create(file: File): DesktopFile = DesktopFile(file)
  55. }
  56. }