{-# LANGUAGE OverloadedStrings #-}

{- |
Module      : Network.MPD.Commands.Database
Copyright   : (c) Ben Sinclair 2005-2009, Joachim Fasting 2012
License     : MIT (see LICENSE)

Maintainer  : joachifm@fastmail.fm
Stability   : stable
Portability : unportable

The music database.
-}

module Network.MPD.Commands.Database
    ( count
    , find
    , findAdd
    , list
    , listAll
    , listAllInfo
    , lsInfo
    , readComments
    , search
    , searchAdd
    , searchAddPl
    , update
    , rescan
    ) where

import qualified Network.MPD.Applicative.Internal as A
import qualified Network.MPD.Applicative.Database as A
import           Network.MPD.Commands.Query
import           Network.MPD.Commands.Types
import           Network.MPD.Core

-- | Count the number of entries matching a query.
count :: MonadMPD m => Query -> m Count
count :: forall (m :: * -> *). MonadMPD m => Query -> m Count
count = Command Count -> m Count
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand (Command Count -> m Count)
-> (Query -> Command Count) -> Query -> m Count
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> Command Count
A.count

-- | Search the database for entries exactly matching a query.
find :: MonadMPD m => Query -> m [Song]
find :: forall (m :: * -> *). MonadMPD m => Query -> m [Song]
find = Command [Song] -> m [Song]
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand (Command [Song] -> m [Song])
-> (Query -> Command [Song]) -> Query -> m [Song]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> Command [Song]
A.find

-- | Adds songs matching a query to the current playlist.
findAdd :: MonadMPD m => Query -> m ()
findAdd :: forall (m :: * -> *). MonadMPD m => Query -> m ()
findAdd = Command () -> m ()
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand (Command () -> m ()) -> (Query -> Command ()) -> Query -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> Command ()
A.findAdd

-- | List all tags of the specified type of songs that that satisfy the query.
--
-- @since 0.10.0.0
list :: MonadMPD m
     => Metadata -- ^ Metadata to list
     -> Query -> m [Value]
list :: forall (m :: * -> *). MonadMPD m => Metadata -> Query -> m [Value]
list Metadata
m = Command [Value] -> m [Value]
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand (Command [Value] -> m [Value])
-> (Query -> Command [Value]) -> Query -> m [Value]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Metadata -> Query -> Command [Value]
A.list Metadata
m

-- | List the songs (without metadata) in a database directory recursively.
listAll :: MonadMPD m => Path -> m [Path]
listAll :: forall (m :: * -> *). MonadMPD m => Path -> m [Path]
listAll = Command [Path] -> m [Path]
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand (Command [Path] -> m [Path])
-> (Path -> Command [Path]) -> Path -> m [Path]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path -> Command [Path]
A.listAll

-- | Recursive 'lsInfo'.
listAllInfo :: MonadMPD m => Path -> m [LsResult]
listAllInfo :: forall (m :: * -> *). MonadMPD m => Path -> m [LsResult]
listAllInfo = Command [LsResult] -> m [LsResult]
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand (Command [LsResult] -> m [LsResult])
-> (Path -> Command [LsResult]) -> Path -> m [LsResult]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path -> Command [LsResult]
A.listAllInfo

-- | Non-recursively list the contents of a database directory.
lsInfo :: MonadMPD m => Path -> m [LsResult]
lsInfo :: forall (m :: * -> *). MonadMPD m => Path -> m [LsResult]
lsInfo = Command [LsResult] -> m [LsResult]
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand (Command [LsResult] -> m [LsResult])
-> (Path -> Command [LsResult]) -> Path -> m [LsResult]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path -> Command [LsResult]
A.lsInfo

-- | Read comments from file at path.
readComments :: MonadMPD m => Path -> m [(String, String)]
readComments :: forall (m :: * -> *). MonadMPD m => Path -> m [(String, String)]
readComments = Command [(String, String)] -> m [(String, String)]
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand (Command [(String, String)] -> m [(String, String)])
-> (Path -> Command [(String, String)])
-> Path
-> m [(String, String)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path -> Command [(String, String)]
A.readComments

-- | Search the database using case insensitive matching.
search :: MonadMPD m => Query -> m [Song]
search :: forall (m :: * -> *). MonadMPD m => Query -> m [Song]
search = Command [Song] -> m [Song]
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand (Command [Song] -> m [Song])
-> (Query -> Command [Song]) -> Query -> m [Song]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> Command [Song]
A.search

-- | Like 'search' but adds the results to the current playlist.
--
-- @since 0.10.0.0
searchAdd :: MonadMPD m => Query -> m ()
searchAdd :: forall (m :: * -> *). MonadMPD m => Query -> m ()
searchAdd = Command () -> m ()
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand (Command () -> m ()) -> (Query -> Command ()) -> Query -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Query -> Command ()
A.searchAdd

-- | Like 'searchAdd' but adds results to the named playlist.
--
-- @since 0.10.0.0
searchAddPl :: MonadMPD m => PlaylistName -> Query -> m ()
searchAddPl :: forall (m :: * -> *). MonadMPD m => PlaylistName -> Query -> m ()
searchAddPl PlaylistName
pl = Command () -> m ()
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand (Command () -> m ()) -> (Query -> Command ()) -> Query -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PlaylistName -> Query -> Command ()
A.searchAddPl PlaylistName
pl

-- | Update the server's database.
--
-- If no path is given, the whole library will be scanned.  Unreadable or
-- non-existent paths are silently ignored.
--
-- The update job id is returned.
update :: MonadMPD m => Maybe Path -> m Integer
update :: forall (m :: * -> *). MonadMPD m => Maybe Path -> m Integer
update = Command Integer -> m Integer
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand (Command Integer -> m Integer)
-> (Maybe Path -> Command Integer) -> Maybe Path -> m Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Path -> Command Integer
A.update

-- | Like 'update' but also rescans unmodified files.
rescan :: MonadMPD m => Maybe Path -> m Integer
rescan :: forall (m :: * -> *). MonadMPD m => Maybe Path -> m Integer
rescan = Command Integer -> m Integer
forall (m :: * -> *) a. MonadMPD m => Command a -> m a
A.runCommand (Command Integer -> m Integer)
-> (Maybe Path -> Command Integer) -> Maybe Path -> m Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Path -> Command Integer
A.rescan