{-# LANGUAGE OverloadedStrings #-}
module Network.MPD.Commands.Extensions where
import Network.MPD.Core
import Network.MPD.Commands
import qualified Network.MPD.Applicative.Internal as A
import qualified Network.MPD.Applicative.CurrentPlaylist as A
import qualified Network.MPD.Applicative.StoredPlaylists as A
import Control.Monad (liftM)
import Data.Traversable (for)
import Data.Foldable (for_)
addMany :: MonadMPD m => PlaylistName -> [Path] -> m ()
addMany :: forall (m :: * -> *). MonadMPD m => PlaylistName -> [Path] -> m ()
addMany PlaylistName
plname [Path]
xs = Command () -> m ()
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand ([Path] -> (Path -> Command ()) -> Command ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Path]
xs Path -> Command ()
cmd)
where cmd :: Path -> Command ()
cmd | PlaylistName
plname PlaylistName -> PlaylistName -> Bool
forall a. Eq a => a -> a -> Bool
== PlaylistName
"" = Path -> Command ()
A.add
| Bool
otherwise = PlaylistName -> Path -> Command ()
A.playlistAdd PlaylistName
plname
addIdMany :: MonadMPD m => Path -> Maybe Position -> m [Id]
addIdMany :: forall (m :: * -> *).
MonadMPD m =>
Path -> Maybe Position -> m [Id]
addIdMany Path
x (Just Position
p) = do
[Path]
fs <- Path -> m [Path]
forall (m :: * -> *). MonadMPD m => Path -> m [Path]
listAll Path
x
let fs' :: [(Path, Maybe Position)]
fs' = ((Path, Position) -> (Path, Maybe Position))
-> [(Path, Position)] -> [(Path, Maybe Position)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Path
a, Position
b) -> (Path
a, Position -> Maybe Position
forall a. a -> Maybe a
Just Position
b)) ([(Path, Position)] -> [(Path, Maybe Position)])
-> [(Path, Position)] -> [(Path, Maybe Position)]
forall a b. (a -> b) -> a -> b
$ [Path] -> [Position] -> [(Path, Position)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Path]
fs [Position
p ..]
Command [Id] -> m [Id]
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand (Command [Id] -> m [Id]) -> Command [Id] -> m [Id]
forall a b. (a -> b) -> a -> b
$ [(Path, Maybe Position)]
-> ((Path, Maybe Position) -> Command Id) -> Command [Id]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for [(Path, Maybe Position)]
fs' ((Path -> Maybe Position -> Command Id)
-> (Path, Maybe Position) -> Command Id
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Path -> Maybe Position -> Command Id
A.addId)
addIdMany Path
x Maybe Position
Nothing = do
[Path]
fs <- Path -> m [Path]
forall (m :: * -> *). MonadMPD m => Path -> m [Path]
listAll Path
x
Command [Id] -> m [Id]
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand (Command [Id] -> m [Id]) -> Command [Id] -> m [Id]
forall a b. (a -> b) -> a -> b
$ [Path] -> (Path -> Command Id) -> Command [Id]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for [Path]
fs (Path -> Maybe Position -> Command Id
`A.addId` Maybe Position
forall a. Maybe a
Nothing)
listArtists :: MonadMPD m => m [Artist]
listArtists :: forall (m :: * -> *). MonadMPD m => m [Artist]
listArtists = Metadata -> Query -> m [Artist]
forall (m :: * -> *). MonadMPD m => Metadata -> Query -> m [Artist]
list Metadata
Artist Query
forall a. Monoid a => a
mempty
listAlbums :: MonadMPD m => Maybe Artist -> m [Album]
listAlbums :: forall (m :: * -> *). MonadMPD m => Maybe Artist -> m [Artist]
listAlbums Maybe Artist
ma = Metadata -> Query -> m [Artist]
forall (m :: * -> *). MonadMPD m => Metadata -> Query -> m [Artist]
list Metadata
Album (case Maybe Artist
ma of
Maybe Artist
Nothing -> Query
forall a. Monoid a => a
mempty
Just Artist
a -> Metadata
Artist Metadata -> Artist -> Query
=? Artist
a)
listAlbum :: MonadMPD m => Artist -> Album -> m [Song]
listAlbum :: forall (m :: * -> *). MonadMPD m => Artist -> Artist -> m [Song]
listAlbum Artist
artist Artist
album = Query -> m [Song]
forall (m :: * -> *). MonadMPD m => Query -> m [Song]
find (Metadata
Artist Metadata -> Artist -> Query
=? Artist
artist Query -> Query -> Query
forall a. Semigroup a => a -> a -> a
<> Metadata
Album Metadata -> Artist -> Query
=? Artist
album)
getPlaylist :: MonadMPD m => m [Song]
getPlaylist :: forall (m :: * -> *). MonadMPD m => m [Song]
getPlaylist = Maybe Position -> m [Song]
forall (m :: * -> *). MonadMPD m => Maybe Position -> m [Song]
playlistInfo Maybe Position
forall a. Maybe a
Nothing
volume :: MonadMPD m => Int -> m ()
volume :: forall (m :: * -> *). MonadMPD m => Position -> m ()
volume Position
n = do
Maybe Volume
cur <- Status -> Maybe Volume
stVolume (Status -> Maybe Volume) -> m Status -> m (Maybe Volume)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` m Status
forall (m :: * -> *). MonadMPD m => m Status
status
case Maybe Volume
cur of
Maybe Volume
Nothing -> () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just Volume
v -> Volume -> m ()
forall (m :: * -> *). MonadMPD m => Volume -> m ()
setVolume (Volume -> Volume
forall {b} {p}. (Integral b, Integral p) => p -> b
adjust Volume
v)
where
adjust :: p -> b
adjust p
x = Double -> b
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (Double -> b) -> Double -> b
forall a b. (a -> b) -> a -> b
$ (Position -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Position
n Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
100 :: Double)) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x' Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
x'
where x' :: Double
x' = p -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral p
x