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)
import Data.ByteString.Char8 (ByteString)
getResponse_ :: MonadMPD m => String -> m ()
getResponse_ x = getResponse x >> return ()
getResponses :: MonadMPD m => [String] -> m [ByteString]
getResponses cmds = getResponse . concat $ intersperse "\n" cmds'
where cmds' = "command_list_begin" : cmds ++ ["command_list_end"]
failOnEmpty :: MonadMPD m => [ByteString] -> m [ByteString]
failOnEmpty [] = throwError $ Unexpected "Non-empty response expected."
failOnEmpty xs = return xs
getResponse1 :: MonadMPD m => String -> m [ByteString]
getResponse1 x = getResponse x >>= failOnEmpty
takeValues :: [ByteString] -> [ByteString]
takeValues = snd . unzip . toAssocList
takeEntries :: MonadMPD m => [ByteString] -> m [LsResult]
takeEntries = mapM toEntry . splitGroups groupHeads . toAssocList
where
toEntry xs@(("file",_):_) = LsSong `liftM` runParser parseSong xs
toEntry (("directory",d):_) = (return . LsDirectory . Path) d
toEntry (("playlist",pl):_) = (return . LsPlaylist . PlaylistName) pl
toEntry _ = error "takeEntries: splitGroups is broken"
groupHeads = ["file", "directory", "playlist"]
extractEntries :: (Song -> Maybe a, PlaylistName -> Maybe a, Path -> Maybe a)
-> [LsResult] -> [a]
extractEntries (fSong,fPlayList,fDir) = mapMaybe f
where
f (LsSong s) = fSong s
f (LsPlaylist pl) = fPlayList pl
f (LsDirectory d) = fDir d
takeSongs :: MonadMPD m => [ByteString] -> m [Song]
takeSongs = mapM (runParser parseSong)
. splitGroups ["file"]
. toAssocList