2022-12-26 14:45:49 -05:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2022-07-06 18:54:10 -04:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2022-12-26 14:45:49 -05:00
|
|
|
|
2022-07-06 18:54:10 -04:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | VirtualBox-specific functions
|
|
|
|
|
|
|
|
module XMonad.Internal.Concurrent.VirtualBox
|
|
|
|
( vmExists
|
2022-08-30 00:21:21 -04:00
|
|
|
, vmInstanceConfig
|
|
|
|
, qual
|
2022-07-06 18:54:10 -04:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Exception
|
|
|
|
|
2022-07-09 17:44:14 -04:00
|
|
|
import Data.Internal.Dependency
|
|
|
|
|
2022-07-06 18:54:10 -04:00
|
|
|
import Text.XML.Light
|
|
|
|
|
2022-12-26 17:56:55 -05:00
|
|
|
import RIO hiding (try)
|
2022-12-26 14:45:49 -05:00
|
|
|
import RIO.FilePath
|
|
|
|
import qualified RIO.Text as T
|
|
|
|
|
2022-07-06 18:54:10 -04:00
|
|
|
import System.Directory
|
|
|
|
|
|
|
|
import XMonad.Internal.Shell
|
|
|
|
|
2022-12-26 14:45:49 -05:00
|
|
|
vmExists :: T.Text -> IO (Maybe Msg)
|
2022-12-26 17:56:55 -05:00
|
|
|
vmExists vm = either (Just . Msg LevelError) (const Nothing) <$> vmInstanceConfig vm
|
2022-08-30 00:21:21 -04:00
|
|
|
|
2022-12-26 14:45:49 -05:00
|
|
|
vmInstanceConfig :: T.Text -> IO (Either T.Text FilePath)
|
2022-08-30 00:21:21 -04:00
|
|
|
vmInstanceConfig vmName = do
|
|
|
|
either (return . Right) findInstance =<< vmDirectory
|
2022-07-06 18:54:10 -04:00
|
|
|
where
|
2022-12-26 14:45:49 -05:00
|
|
|
path = T.unpack vmName </> addExtension (T.unpack vmName) "vbox"
|
2022-08-30 00:21:21 -04:00
|
|
|
findInstance dir = do
|
|
|
|
res <- findFile [dir] path
|
|
|
|
return $ case res of
|
|
|
|
Just p -> Right p
|
2022-12-26 14:45:49 -05:00
|
|
|
Nothing -> Left $ T.append "could not find VM instance: " $ singleQuote vmName
|
2022-07-06 18:54:10 -04:00
|
|
|
|
|
|
|
vmDirectory :: IO (Either String String)
|
|
|
|
vmDirectory = do
|
|
|
|
p <- vmConfig
|
|
|
|
(s :: Either IOException String) <- try $ readFile p
|
|
|
|
return $ case s of
|
|
|
|
(Left _) -> Left "could not read VirtualBox config file"
|
|
|
|
(Right x) -> maybe (Left "Could not parse VirtualBox config file") Right
|
|
|
|
$ findDir =<< parseXMLDoc x
|
|
|
|
where
|
|
|
|
findDir e = findAttr (unqual "defaultMachineFolder")
|
|
|
|
=<< findChild (qual e "SystemProperties")
|
|
|
|
=<< findChild (qual e "Global") e
|
2022-08-30 00:21:21 -04:00
|
|
|
|
|
|
|
qual :: Element -> String -> QName
|
|
|
|
qual e n = (elName e) { qName = n }
|
2022-07-06 18:54:10 -04:00
|
|
|
|
|
|
|
vmConfig :: IO FilePath
|
|
|
|
vmConfig = getXdgDirectory XdgConfig "VirtualBox/VirtualBox.xml"
|