{- * 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.Configuration.Configuration ( configDirFilePath, playlistDirFilePath, readConfiguration, getConfiguration, getMusicDirectory, getConfigurationColor, makeDefaultConfiguration ) where import Control.Monad import System.Environment import System.Directory import Data.Either.Utils import Data.ConfigFile hiding (content) import Graphics.Vty import System.FilePath.Posix import Mp.Utils.Utils import Mp.Configuration.ConfigurationFile configDir :: String configDir = ".config/mp" configFile :: String configFile = "mp.config" configDirFilePath :: IO FilePath configDirFilePath = do homeDir <- getEnv "HOME" return $ homeDir configDir playlistDirFilePath :: IO FilePath playlistDirFilePath = do cfg <- configDirFilePath return $ cfg "playlists" readConfiguration :: IO ConfigParser readConfiguration = do confDir <- configDirFilePath let file = confDir configFile conf <- readfile emptyCP file return $ forceEither conf getConfiguration :: ConfigParser -> SectionSpec -> OptionSpec -> String getConfiguration parser section option = forceEither $ get parser section option getMusicDirectory :: ConfigParser -> String getMusicDirectory conf = getConfiguration conf "general" "music_dir" getConfigurationColor :: ConfigParser -> OptionSpec -> (Color, Style) getConfigurationColor parser option = toVtyColor $ getConfiguration parser "colors" option makeDefaultConfiguration :: IO () makeDefaultConfiguration = do confDir <- configDirFilePath playlistDir <- playlistDirFilePath createDirectoryIfMissing True confDir createDirectoryIfMissing True playlistDir makeDefaultConfigurationFile confDir makeDefaultConfigurationFile :: String -> IO () makeDefaultConfigurationFile confDir = do homeDir <- getEnv "HOME" exist <- doesFileExist file unless exist $ writeFile file $ defaultConfiguration homeDir where file = confDir configFile