module Configuration (
Cfg(..),
+ cj_empty,
get_cfg,
use_account
)
where
-import Control.Monad (mplus)
import Data.ConfigFile
import Data.Maybe (isJust)
import Data.Monoid (Monoid(..))
+import Network.HTTP.Conduit (CookieJar, createCookieJar, destroyCookieJar)
import System.Directory (
doesFileExist,
getAppUserDataDirectory
-- | Contains all of our configurable options.
data Cfg = Cfg {
article :: String,
- cookie_jar :: Maybe FilePath,
+ cookie_jar :: CookieJar,
full_stories :: Bool,
output :: FilePath,
password :: Maybe String,
username :: Maybe String }
+-- An empty CookieJar. See cj_append for rationale.
+cj_empty :: CookieJar
+cj_empty = createCookieJar []
+
+
+-- Defined for convenience; I would really like to use mappend but GHC
+-- bitches about the orphan instance.
+cj_append :: CookieJar -> CookieJar -> CookieJar
+cj_append cj1 cj2 =
+ createCookieJar (cookies1 ++ cookies2)
+ where
+ -- Decompose the cookie jars into lists.
+ cookies1 = destroyCookieJar cj1
+ cookies2 = destroyCookieJar cj2
+
instance Monoid Cfg where
- mempty = Cfg { article = "",
- cookie_jar = Nothing,
+ mempty = Cfg { article = mempty,
+ cookie_jar = cj_empty,
full_stories = False,
- output = "",
+ output = mempty,
password = Nothing,
username = Nothing }
mappend c1 c2 =
let article' = (if null article1 then article2 else article1)
- cookie_jar' = cookie_jar1 `mplus` cookie_jar2
+ cookie_jar' = cookie_jar1 `cj_append` cookie_jar2
full_stories' = full_stories1 || full_stories2
output' = (if null output1 then output2 else output1)
- password' = password1 `mplus` password2
- username' = username1 `mplus` username2
+ password' = password1 `mappend` password2
+ username' = username1 `mappend` username2
in
Cfg { article = article',
cookie_jar = cookie_jar',