package at.hannibal2.skyhanni.utils import at.hannibal2.skyhanni.test.command.ErrorManager import java.awt.Desktop import java.io.IOException import java.net.URI object OSUtils { enum class OperatingSystem { LINUX, SOLARIS, WINDOWS, MACOS, UNKNOWN, } fun getOperatingSystemRaw(): String = System.getProperty("os.name") fun getOperatingSystem(): OperatingSystem { val osName = getOperatingSystemRaw().lowercase() return when { osName.contains("win") -> OperatingSystem.WINDOWS osName.contains("mac") -> OperatingSystem.MACOS osName.contains("solaris") || osName.contains("sunos") -> OperatingSystem.SOLARIS osName.contains("linux") || osName.contains("unix") -> OperatingSystem.LINUX else -> OperatingSystem.UNKNOWN } } val isWindows: Boolean val isMac: Boolean val isLinux: Boolean init { val os = getOperatingSystem() isWindows = os == OperatingSystem.WINDOWS isMac = os == OperatingSystem.MACOS isLinux = os == OperatingSystem.LINUX } @JvmStatic fun openBrowser(url: String) { val desktopSupported = Desktop.isDesktopSupported() val supportedActionBrowse = Desktop.getDesktop().isSupported(Desktop.Action.BROWSE) if (desktopSupported && supportedActionBrowse) { try { Desktop.getDesktop().browse(URI(url)) } catch (e: IOException) { ErrorManager.logErrorWithData( e, "Error while opening website.", "url" to url, ) } } else { copyToClipboard(url) ErrorManager.logErrorStateWithData( "Cannot open website! Copied url to clipboard instead", "Web browser is not supported", "url" to url, "desktopSupported" to desktopSupported, "supportedActionBrowse" to supportedActionBrowse, ) } } fun copyToClipboard(text: String) { ClipboardUtils.copyToClipboard(text) } suspend fun readFromClipboard() = ClipboardUtils.readFromClipboard() }