App.kt 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package eu.mjdev.desktop.provider.data
  2. import androidx.compose.ui.graphics.Color
  3. import java.io.File
  4. @Suppress("unused", "MemberVisibilityCanBePrivate")
  5. class App(
  6. val file: File? = null,
  7. val desktopFile: DesktopFile? = file?.let { DesktopFile(file) }
  8. ) {
  9. val type
  10. get() = desktopFile?.type ?: "Application"
  11. val version
  12. get() = desktopFile?.version ?: ""
  13. val name
  14. get() = desktopFile?.name ?: "-"
  15. val comment
  16. get() = desktopFile?.comment ?: ""
  17. val path
  18. get() = desktopFile?.path ?: ""
  19. val exec
  20. get() = desktopFile?.exec ?: ""
  21. val iconName
  22. get() = desktopFile?.icon ?: ""
  23. val categories
  24. get() = desktopFile?.categories.ifEmptyCategories { listOf(Category.UNCATEGORIZED) }
  25. val notifyOnStart
  26. get() = desktopFile?.notifyOnStart ?: false
  27. val runInTerminal
  28. get() = desktopFile?.runInTerminal ?: false
  29. var enabled: Boolean = true
  30. var iconTint: Color? = null
  31. fun start() = runCatching {
  32. ProcessBuilder("/bin/bash", "-c", exec).start()
  33. }.onFailure { error -> error.printStackTrace() }
  34. companion object {
  35. val Empty: App = App()
  36. fun List<String>?.ifEmptyCategories(block: () -> List<String>) = this?.filter { s ->
  37. s.isNotEmpty()
  38. }.let { list ->
  39. when {
  40. list == null -> block()
  41. list.isEmpty() -> block()
  42. else -> list
  43. }
  44. }
  45. }
  46. }