{-# LANGUAGE OverloadedStrings #-}

{- |
Module      : Network.MPD.Applicative.Mount
Copyright   : (c) Joachim Fasting 2014
License     : MIT

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

Mounting remote storage.
-}

module Network.MPD.Applicative.Mount
  ( mount
  , unmount
  , listMounts
  , listNeighbors
  ) where

import           Network.MPD.Commands.Arg hiding (Command)
import           Network.MPD.Applicative.Internal
import           Network.MPD.Util

import           Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.UTF8 as UTF8

mount :: String -- Path
      -> String -- Uri
      -> Command ()
mount :: String -> String -> Command ()
mount String
p String
u = forall a. Parser a -> [String] -> Command a
Command Parser ()
emptyResponse [Command
"mount" forall a. MPDArg a => Command -> a -> String
<@> String
p forall a b. (MPDArg a, MPDArg b) => a -> b -> Args
<++> String
u]

unmount :: String -- Path
        -> Command ()
unmount :: String -> Command ()
unmount String
p = forall a. Parser a -> [String] -> Command a
Command Parser ()
emptyResponse [Command
"unmount" forall a. MPDArg a => Command -> a -> String
<@> String
p]

listMounts :: Command [(String, String)] -- (Path, Uri)
listMounts :: Command [(String, String)]
listMounts = forall a. Parser a -> [String] -> Command a
Command (forall a. ([ByteString] -> Either String a) -> Parser a
liftParser [ByteString] -> Either String [(String, String)]
p) [String
"listmounts"]
  where
    p :: [ByteString] -> Either String [(String, String)]
p = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM [(ByteString, ByteString)] -> Either String (String, String)
parseMount forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString]
-> [(ByteString, ByteString)] -> [[(ByteString, ByteString)]]
splitGroups [ByteString
"mount"] forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> [(ByteString, ByteString)]
toAssocList
    parseMount :: [(ByteString, ByteString)] -> Either String (String, String)
    parseMount :: [(ByteString, ByteString)] -> Either String (String, String)
parseMount [(ByteString
"mount", ByteString
mo), (ByteString
"storage", ByteString
st)] = forall a b. b -> Either a b
Right (ByteString -> String
UTF8.toString ByteString
mo, ByteString -> String
UTF8.toString ByteString
st)
    parseMount [(ByteString, ByteString)]
_ = forall a b. a -> Either a b
Left String
"Unexpected result from listMounts"

listNeighbors :: Command [(String, String)] -- (Uri, Name)
listNeighbors :: Command [(String, String)]
listNeighbors = forall a. Parser a -> [String] -> Command a
Command (forall a. ([ByteString] -> Either String a) -> Parser a
liftParser [ByteString] -> Either String [(String, String)]
p) [String
"listneighbors"]
  where
    p :: [ByteString] -> Either String [(String, String)]
p = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM [(ByteString, ByteString)] -> Either String (String, String)
parseNeighbor forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString]
-> [(ByteString, ByteString)] -> [[(ByteString, ByteString)]]
splitGroups [ByteString
"neighbor"] forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> [(ByteString, ByteString)]
toAssocList
    parseNeighbor :: [(ByteString, ByteString)] -> Either String (String, String)
    parseNeighbor :: [(ByteString, ByteString)] -> Either String (String, String)
parseNeighbor [(ByteString
"neighbor", ByteString
ne), (ByteString
"name", ByteString
na)] = forall a b. b -> Either a b
Right (ByteString -> String
UTF8.toString ByteString
ne, ByteString -> String
UTF8.toString ByteString
na)
    parseNeighbor [(ByteString, ByteString)]
_ = forall a b. a -> Either a b
Left String
"Unexpected result from listNeighbors"