packages feed

mp-0.1.3: src/Mp/Player/ServerState.hs

{-  
 *  Programmer:	Piotr Borek
 *  E-mail:     piotrborek@op.pl
 *  Copyright 2014 Piotr Borek
 *
 *  Distributed under the terms of the GPL (GNU Public License)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-}

module Mp.Player.ServerState where

import qualified Control.Monad.State as State

import Data.Binary
import System.Random

import Mp.Utils.Shuffle
import Mp.Utils.Exception

data ServerState = ServerState {
    getPlaylist        :: ![String],
    getCurrentPosition :: !Integer,
    getCurrentDuration :: !Integer,
    getStatus          :: !String,
    getPlaying         :: !Int,
    getVolume          :: !Double,
    isRepeatMode       :: !Bool,
    isShuffleMode      :: !Bool,
    randomInitializer  :: !Int,
    shuffleFunc        :: Int -> Int,
    shuffleData        :: !ShuffleData
}

instance Binary ServerState where
    put st = do
        put (1113 :: Word32)
        put (0 :: Int)
        put $ getPlaying st
        if getStatus st == "Paused"
            then put "Playing"
            else put $ getStatus st
        put $ getVolume st
        put $ getPlaylist st
        put $ isRepeatMode st
        put $ isShuffleMode st
        put $ randomInitializer st

    get = do
        magic <- get :: Get Word32
        if magic /= 1113
            then return defaultServerState
            else do
                version <- get :: Get Int
                case version of
                    0 -> getVersion0
                    _ -> return defaultServerState
        where
            getVersion0 = do
                playing     <- get
                status      <- get
                volume      <- get
                playlist    <- get
                repeatMode  <- get
                shuffleMode <- get
                r           <- get

                let g = mkStdGen r
                let (f, d) = if shuffleMode then
                                 State.runState (shuffleFilled $ length playlist) $ shuffleInitial g
                             else
                                 (id, shuffleInitial g)

                return ServerState {
                    getPlaylist = playlist,
                    getCurrentPosition = 0,
                    getCurrentDuration = 0,
                    getStatus = status,
                    getPlaying = playing,
                    getVolume = volume,
                    isRepeatMode = repeatMode,
                    isShuffleMode = shuffleMode,
                    randomInitializer = r,
                    shuffleFunc = f,
                    shuffleData = d
                }

defaultServerState :: ServerState
defaultServerState = ServerState {
    getPlaylist = [],
    getCurrentPosition = 0,
    getCurrentDuration = 0,
    getStatus = "Stopped",
    getPlaying = -1,
    getVolume = 1.0,
    isRepeatMode = False,
    isShuffleMode = False,
    randomInitializer = 0,
    shuffleFunc = id,
    shuffleData = shuffleInitial $ mkStdGen 0
}

readStateFromFile :: String -> IO ServerState
readStateFromFile file = decodeFile file `catchAny` (\_ -> return defaultServerState)