2022-07-06 18:54:10 -04:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | VirtualBox-specific functions
|
|
|
|
|
|
|
|
module XMonad.Internal.Concurrent.VirtualBox
|
|
|
|
( vmExists
|
|
|
|
) 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
|
|
|
|
|
|
|
|
import System.Directory
|
|
|
|
|
|
|
|
import XMonad.Internal.Shell
|
|
|
|
|
2022-07-07 01:05:17 -04:00
|
|
|
vmExists :: String -> IO (Maybe Msg)
|
2022-07-06 18:54:10 -04:00
|
|
|
vmExists vm = do
|
|
|
|
d <- vmDirectory
|
2022-07-07 01:05:17 -04:00
|
|
|
either (return . Just . Msg Error) findVMDir d
|
2022-07-06 18:54:10 -04:00
|
|
|
where
|
|
|
|
findVMDir vd = do
|
|
|
|
vs <- listDirectory vd
|
|
|
|
return $ if vm `elem` vs then Nothing
|
2022-07-07 01:05:17 -04:00
|
|
|
else Just $ Msg Error $ "could not find " ++ singleQuote vm
|
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
|
|
|
|
qual e n = (elName e) { qName = n }
|
|
|
|
|
|
|
|
vmConfig :: IO FilePath
|
|
|
|
vmConfig = getXdgDirectory XdgConfig "VirtualBox/VirtualBox.xml"
|