xmonad-config/lib/XMonad/Internal/Concurrent/VirtualBox.hs

54 lines
1.6 KiB
Haskell
Raw Normal View History

{-# LANGUAGE ScopedTypeVariables #-}
--------------------------------------------------------------------------------
-- | VirtualBox-specific functions
module XMonad.Internal.Concurrent.VirtualBox
( vmExists
2022-08-30 00:21:21 -04:00
, vmInstanceConfig
, qual
) where
import Control.Exception
2022-07-09 17:44:14 -04:00
import Data.Internal.Dependency
import Text.XML.Light
import System.Directory
2022-08-30 00:21:21 -04:00
import System.FilePath
import XMonad.Internal.Shell
2022-07-07 01:05:17 -04:00
vmExists :: String -> IO (Maybe Msg)
2022-09-01 18:27:53 -04:00
vmExists vm = either (Just . Msg Error) (const Nothing) <$> vmInstanceConfig vm
2022-08-30 00:21:21 -04:00
vmInstanceConfig :: String -> IO (Either String FilePath)
vmInstanceConfig vmName = do
either (return . Right) findInstance =<< vmDirectory
where
2022-08-30 00:21:21 -04:00
path = vmName </> (vmName ++ ".vbox")
findInstance dir = do
res <- findFile [dir] path
return $ case res of
Just p -> Right p
Nothing -> Left $ "could not find VM instance: " ++ singleQuote vmName
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 }
vmConfig :: IO FilePath
vmConfig = getXdgDirectory XdgConfig "VirtualBox/VirtualBox.xml"