module Network.MPD.Commands.Util where
import Network.MPD.Commands.Parse
import Network.MPD.Commands.Types
import Network.MPD.Core
import Network.MPD.Util
import Control.Monad.Error
import Data.List (intersperse)
import Data.Maybe (mapMaybe)
getResponse_ :: MonadMPD m => String -> m ()
getResponse_ x = getResponse x >> return ()
getResponses :: MonadMPD m => [String] -> m [String]
getResponses cmds = getResponse . concat $ intersperse "\n" cmds'
where cmds' = "command_list_begin" : cmds ++ ["command_list_end"]
failOnEmpty :: MonadMPD m => [String] -> m [String]
failOnEmpty [] = throwError $ Unexpected "Non-empty response expected."
failOnEmpty xs = return xs
getResponse1 :: MonadMPD m => String -> m [String]
getResponse1 x = getResponse x >>= failOnEmpty
takeValues :: [String] -> [String]
takeValues = snd . unzip . toAssocList
data EntryType
= SongEntry Song
| PLEntry String
| DirEntry String
deriving (Eq, Show)
takeEntries :: MonadMPD m => [String] -> m [EntryType]
takeEntries = mapM toEntry . splitGroups wrappers . toAssocList
where
toEntry xs@(("file",_):_) = liftM SongEntry $ runParser parseSong xs
toEntry (("directory",d):_) = return $ DirEntry d
toEntry (("playlist",pl):_) = return $ PLEntry pl
toEntry _ = error "takeEntries: splitGroups is broken"
wrappers = [("file",id), ("directory",id), ("playlist",id)]
extractEntries :: (Song -> Maybe a, String -> Maybe a, String -> Maybe a)
-> [EntryType] -> [a]
extractEntries (fSong,fPlayList,fDir) = mapMaybe f
where
f (SongEntry s) = fSong s
f (PLEntry pl) = fPlayList pl
f (DirEntry d) = fDir d
takeSongs :: MonadMPD m => [String] -> m [Song]
takeSongs = mapM (runParser parseSong)
. splitGroups [("file",id)]
. toAssocList