git-annex/Assistant/WebApp/Configurators/Unused.hs

81 lines
2.3 KiB
Haskell
Raw Normal View History

{- git-annex assistant unused file preferences
-
- Copyright 2014 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU AGPL version 3 or higher.
-}
{-# LANGUAGE QuasiQuotes, TemplateHaskell, OverloadedStrings #-}
module Assistant.WebApp.Configurators.Unused where
import Assistant.WebApp.Common
import qualified Annex
2014-01-23 19:09:43 +00:00
import Utility.HumanTime
import Assistant.Unused
import Config
import Git.Config
2014-01-23 19:09:43 +00:00
import Logs.Unused
import Utility.Tense
2014-01-23 19:09:43 +00:00
import qualified Text.Hamlet as Hamlet
2014-01-23 19:09:43 +00:00
data UnusedForm = UnusedForm
{ enableExpire :: Bool
, expireWhen :: Integer
}
unusedForm :: UnusedForm -> Hamlet.Html -> MkMForm UnusedForm
2015-01-28 20:11:28 +00:00
unusedForm d msg = do
2014-04-18 00:07:09 +00:00
(enableRes, enableView) <- mreq (selectFieldList enabledisable) (bfs "")
2015-01-28 20:11:28 +00:00
(Just $ enableExpire d)
2014-04-18 00:07:09 +00:00
(whenRes, whenView) <- mreq intField (bfs "")
2015-01-28 20:11:28 +00:00
(Just $ expireWhen d)
2014-01-23 19:09:43 +00:00
let form = do
webAppFormAuthToken
$(widgetFile "configurators/unused/form")
return (UnusedForm <$> enableRes <*> whenRes, form)
where
enabledisable :: [(Text, Bool)]
enabledisable = [("Disable expiry", False), ("Enable expiry", True)]
2014-01-23 19:09:43 +00:00
getConfigUnusedR :: Handler Html
getConfigUnusedR = postConfigUnusedR
postConfigUnusedR :: Handler Html
2014-01-23 19:09:43 +00:00
postConfigUnusedR = page "Unused files" (Just Configuration) $ do
current <- liftAnnex getUnused
((res, form), enctype) <- liftH $ runFormPostNoToken $ unusedForm current
case res of
FormSuccess new -> liftH $ do
liftAnnex $ storeUnused new
redirect ConfigurationR
_ -> do
munuseddesc <- liftAssistant describeUnused
ts <- liftAnnex $ dateUnusedLog ""
mlastchecked <- case ts of
Nothing -> pure Nothing
Just t -> Just <$> liftIO (durationSince t)
$(widgetFile "configurators/unused")
getUnused :: Annex UnusedForm
getUnused = convert . annexExpireUnused <$> Annex.getGitConfig
where
convert Nothing = noexpire
convert (Just Nothing) = noexpire
convert (Just (Just n)) = UnusedForm True $ durationToDays n
-- The 7 is so that, if they enable expiry, they have to change
-- it to get faster than a week.
noexpire = UnusedForm False 7
storeUnused :: UnusedForm -> Annex ()
storeUnused f = setConfig (annexConfig "expireunused") $
if not (enableExpire f) || expireWhen f < 0
then boolConfig False
else fromDuration $ daysToDuration $ expireWhen f
2014-01-23 19:09:43 +00:00
getCleanupUnusedR :: Handler Html
getCleanupUnusedR = do
liftAssistant $ expireUnused Nothing
redirect ConfigUnusedR