2013-03-03 21:07:27 +00:00
|
|
|
{- git-annex assistant general preferences
|
|
|
|
-
|
2013-03-04 03:20:47 +00:00
|
|
|
- Copyright 2013 Joey Hess <joey@kitenet.net>
|
2013-03-03 21:07:27 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-03-04 03:27:17 +00:00
|
|
|
{-# LANGUAGE QuasiQuotes, TemplateHaskell, OverloadedStrings #-}
|
2013-03-03 21:07:27 +00:00
|
|
|
|
|
|
|
module Assistant.WebApp.Configurators.Preferences (
|
2013-03-16 22:48:23 +00:00
|
|
|
getPreferencesR,
|
|
|
|
postPreferencesR
|
2013-03-03 21:07:27 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Assistant.WebApp.Common
|
|
|
|
import qualified Annex
|
|
|
|
import qualified Git
|
|
|
|
import Config
|
2013-04-23 15:38:52 +00:00
|
|
|
import Config.Files
|
2013-03-03 21:07:27 +00:00
|
|
|
import Utility.DataUnits
|
2013-06-18 00:41:17 +00:00
|
|
|
import Git.Config
|
2013-03-03 21:07:27 +00:00
|
|
|
|
|
|
|
import qualified Data.Text as T
|
|
|
|
|
|
|
|
data PrefsForm = PrefsForm
|
|
|
|
{ diskReserve :: Text
|
|
|
|
, numCopies :: Int
|
|
|
|
, autoStart :: Bool
|
|
|
|
, debugEnabled :: Bool
|
|
|
|
}
|
|
|
|
|
2013-06-03 20:33:05 +00:00
|
|
|
prefsAForm :: PrefsForm -> MkAForm PrefsForm
|
2013-03-03 21:07:27 +00:00
|
|
|
prefsAForm def = PrefsForm
|
|
|
|
<$> areq (storageField `withNote` diskreservenote)
|
|
|
|
"Disk reserve" (Just $ diskReserve def)
|
|
|
|
<*> areq (positiveIntField `withNote` numcopiesnote)
|
|
|
|
"Number of copies" (Just $ numCopies def)
|
|
|
|
<*> areq (checkBoxField `withNote` autostartnote)
|
|
|
|
"Auto start" (Just $ autoStart def)
|
|
|
|
<*> areq (checkBoxField `withNote` debugnote)
|
|
|
|
"Enable debug logging" (Just $ debugEnabled def)
|
|
|
|
where
|
|
|
|
diskreservenote = [whamlet|<br>Avoid downloading files from other repositories when there is too little free disk space.|]
|
|
|
|
numcopiesnote = [whamlet|<br>Only drop a file after verifying that other repositories contain this many copies.|]
|
|
|
|
debugnote = [whamlet|<a href="@{LogR}">View Log</a>|]
|
|
|
|
autostartnote = [whamlet|Start the git-annex assistant at boot or on login.|]
|
|
|
|
|
|
|
|
positiveIntField = check isPositive intField
|
|
|
|
where
|
|
|
|
isPositive i
|
|
|
|
| i > 0 = Right i
|
|
|
|
| otherwise = Left notPositive
|
|
|
|
notPositive :: Text
|
|
|
|
notPositive = "This should be 1 or more!"
|
|
|
|
|
|
|
|
storageField = check validStorage textField
|
|
|
|
where
|
|
|
|
validStorage t
|
|
|
|
| T.null t = Right t
|
|
|
|
| otherwise = case readSize dataUnits $ T.unpack t of
|
|
|
|
Nothing -> Left badParse
|
|
|
|
Just _ -> Right t
|
|
|
|
badParse :: Text
|
|
|
|
badParse = "Parse error. Expected something like \"100 megabytes\" or \"2 gb\""
|
|
|
|
|
|
|
|
getPrefs :: Annex PrefsForm
|
|
|
|
getPrefs = PrefsForm
|
|
|
|
<$> (T.pack . roughSize storageUnits False . annexDiskReserve <$> Annex.getGitConfig)
|
|
|
|
<*> (annexNumCopies <$> Annex.getGitConfig)
|
|
|
|
<*> inAutoStartFile
|
2013-06-18 00:41:17 +00:00
|
|
|
<*> (annexDebug <$> Annex.getGitConfig)
|
2013-03-03 21:07:27 +00:00
|
|
|
|
|
|
|
storePrefs :: PrefsForm -> Annex ()
|
|
|
|
storePrefs p = do
|
|
|
|
setConfig (annexConfig "diskreserve") (T.unpack $ diskReserve p)
|
|
|
|
setConfig (annexConfig "numcopies") (show $ numCopies p)
|
|
|
|
unlessM ((==) <$> pure (autoStart p) <*> inAutoStartFile) $ do
|
|
|
|
here <- fromRepo Git.repoPath
|
|
|
|
liftIO $ if autoStart p
|
|
|
|
then addAutoStartFile here
|
|
|
|
else removeAutoStartFile here
|
2013-06-18 00:41:17 +00:00
|
|
|
setConfig (annexConfig "debug") (boolConfig $ debugEnabled p)
|
|
|
|
liftIO $ if debugEnabled p
|
|
|
|
then enableDebugOutput
|
|
|
|
else disableDebugOutput
|
2013-03-03 21:07:27 +00:00
|
|
|
|
|
|
|
getPreferencesR :: Handler RepHtml
|
2013-03-16 22:48:23 +00:00
|
|
|
getPreferencesR = postPreferencesR
|
|
|
|
postPreferencesR :: Handler RepHtml
|
|
|
|
postPreferencesR = page "Preferences" (Just Configuration) $ do
|
2013-06-03 17:51:54 +00:00
|
|
|
((result, form), enctype) <- liftH $ do
|
2013-03-04 20:36:38 +00:00
|
|
|
current <- liftAnnex getPrefs
|
2013-03-16 22:48:23 +00:00
|
|
|
runFormPost $ renderBootstrap $ prefsAForm current
|
2013-03-03 21:07:27 +00:00
|
|
|
case result of
|
2013-06-03 17:51:54 +00:00
|
|
|
FormSuccess new -> liftH $ do
|
2013-03-04 20:36:38 +00:00
|
|
|
liftAnnex $ storePrefs new
|
2013-03-03 21:07:27 +00:00
|
|
|
redirect ConfigurationR
|
|
|
|
_ -> $(widgetFile "configurators/preferences")
|
|
|
|
|
|
|
|
inAutoStartFile :: Annex Bool
|
|
|
|
inAutoStartFile = do
|
|
|
|
here <- fromRepo Git.repoPath
|
|
|
|
any (`equalFilePath` here) <$> liftIO readAutoStartFile
|