module Hmpf.MPDSession ( albums , artists , status , M.MPDStatus (..) , currentsong , M.Song (..) , playAlbum , vol , pause , nextSong , previousSong , play , stop , fastforward , rewind , command , updatedb , tracksByArtist , tracks , albumByTrack , addSongs , playlistinfo , playlistinfoAt , clear , loadCache , albumByArtist ) where import qualified Hmpf.MPD as M import Hmpf.ApplicationTypes -- ( lift , get , Session (..) ) import Control.Concurrent.MVar import Hmpf.LCDProc (alert) import Control.Concurrent (threadDelay, forkIO) tracks = do st <- get (as,albs,trks) <- lift ( readMVar . cache $ st ) return trks albums = do st <- get (as,albs,trks) <- lift ( readMVar . cache $ st ) return albs artists = do st <- get (as,albs,trks) <- lift ( readMVar . cache $ st ) return as loadCache :: Session () loadCache = do st <- get (as',albs',trks') <- lift ( readMVar . cache $ st ) let mc = mpdConf st conf <- lift ( readMVar mc ) as <- lift . M.artists $ conf lift( swapMVar (cache st) ( as , albs' , trks' ) ) albs <- lift . M.albums $ conf lift( swapMVar (cache st) ( as , albs , trks' ) ) trks <- lift . M.tracks $ conf lift( swapMVar (cache st) ( as , albs , trks ) ) return () updatedb= do st <- get let mc = mpdConf st lift ( do conf <- readMVar mc M.updatedb conf ) cch <- lower (loadCache >> alert "Cache reloaded" "") let loadCache' = threadDelay 20000000 >> cch lift ( forkIO loadCache' ) albumByTrack = passConfMore M.albumByTrack playlistinfoAt = passConfMore M.playlistinfoAt addSongs = passConfMore M.addSongs clear = passConf M.clear tracksByArtist = passConfMore M.tracksByArtist albumByArtist = passConfMore M.albumByArtist status = passConf M.status playlistinfo = passConf M.playlistinfo currentsong = passConf M.currentsong playAlbum= passConfMore M.playAlbum vol= passConfMore M.vol pause= passConf M.pause nextSong= passConf M.nextSong previousSong= passConf M.previousSong play= passConf M.play stop= passConf M.stop fastforward= passConf M.fastforward rewind= passConf M.rewind command= passConfMore M.command passConfMore f x = do st <- get let mc = mpdConf st lift ( do conf <- takeMVar mc y <- f conf x putMVar mc conf return y ) passConf f = do st <- get let mc = mpdConf st lift ( do conf <- takeMVar mc y <- f conf putMVar mc conf return y )