2012-10-09 18:43:53 +00:00
|
|
|
{- git-annex assistant webapp configurator for editing existing repos
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings, RankNTypes #-}
|
|
|
|
|
|
|
|
module Assistant.WebApp.Configurators.Edit where
|
|
|
|
|
|
|
|
import Assistant.Common
|
|
|
|
import Assistant.WebApp
|
|
|
|
import Assistant.WebApp.Types
|
|
|
|
import Assistant.WebApp.SideBar
|
2012-10-11 21:14:42 +00:00
|
|
|
import Assistant.WebApp.Configurators.Local (syncRemote)
|
|
|
|
import Assistant.WebApp.DashBoard (cancelTransfer)
|
|
|
|
import Assistant.DaemonStatus
|
|
|
|
import Assistant.TransferQueue
|
|
|
|
import Assistant.ThreadedMonad
|
2012-10-09 18:43:53 +00:00
|
|
|
import Utility.Yesod
|
2012-10-09 19:11:48 +00:00
|
|
|
import qualified Remote
|
2012-10-11 21:14:42 +00:00
|
|
|
import qualified Remote.List as Remote
|
|
|
|
import qualified Types.Remote as Remote
|
2012-10-09 19:11:48 +00:00
|
|
|
import Logs.UUID
|
2012-10-10 20:04:28 +00:00
|
|
|
import Logs.Group
|
2012-10-10 23:13:49 +00:00
|
|
|
import Logs.PreferredContent
|
2012-10-11 21:14:42 +00:00
|
|
|
import Logs.Transfer
|
2012-10-10 20:04:28 +00:00
|
|
|
import Types.StandardGroups
|
2012-10-11 23:22:29 +00:00
|
|
|
import qualified Config
|
2012-10-11 21:14:42 +00:00
|
|
|
import Annex.UUID
|
2012-10-09 18:43:53 +00:00
|
|
|
|
|
|
|
import Yesod
|
|
|
|
import Data.Text (Text)
|
|
|
|
import qualified Data.Text as T
|
2012-10-09 19:11:48 +00:00
|
|
|
import qualified Data.Map as M
|
2012-10-10 20:23:41 +00:00
|
|
|
import qualified Data.Set as S
|
|
|
|
|
|
|
|
data RepoGroup = RepoGroupCustom String | RepoGroupStandard StandardGroup
|
|
|
|
deriving (Show, Eq)
|
2012-10-09 19:11:48 +00:00
|
|
|
|
|
|
|
data RepoConfig = RepoConfig
|
|
|
|
{ repoDescription :: Text
|
2012-10-10 20:23:41 +00:00
|
|
|
, repoGroup :: RepoGroup
|
2012-10-11 23:22:29 +00:00
|
|
|
, repoSyncable :: Bool
|
2012-10-09 19:11:48 +00:00
|
|
|
}
|
|
|
|
deriving (Show)
|
|
|
|
|
2012-10-11 21:14:42 +00:00
|
|
|
getRepoConfig :: Remote -> Annex RepoConfig
|
|
|
|
getRepoConfig r = RepoConfig
|
2012-10-10 23:13:49 +00:00
|
|
|
<$> (T.pack . fromMaybe "" . M.lookup uuid <$> uuidMap)
|
|
|
|
<*> getrepogroup
|
2012-10-11 23:22:29 +00:00
|
|
|
<*> Config.repoSyncable (Remote.repo r)
|
2012-10-10 23:13:49 +00:00
|
|
|
where
|
|
|
|
getrepogroup = do
|
|
|
|
groups <- lookupGroups uuid
|
|
|
|
return $
|
|
|
|
maybe (RepoGroupCustom $ unwords $ S.toList groups) RepoGroupStandard
|
|
|
|
(getStandardGroup groups)
|
2012-10-11 21:14:42 +00:00
|
|
|
uuid = Remote.uuid r
|
2012-10-10 23:13:49 +00:00
|
|
|
|
2012-10-11 23:22:29 +00:00
|
|
|
{- Returns Just False if syncing has been disabled, or Just True when enabled. -}
|
2012-10-11 21:14:42 +00:00
|
|
|
setRepoConfig :: Remote -> RepoConfig -> Annex (Maybe Bool)
|
|
|
|
setRepoConfig r c = do
|
2012-10-10 23:13:49 +00:00
|
|
|
describeUUID uuid $ T.unpack $ repoDescription c
|
|
|
|
case repoGroup c of
|
|
|
|
RepoGroupStandard g -> setStandardGroup uuid g
|
|
|
|
RepoGroupCustom s -> groupSet uuid $ S.fromList $ words s
|
2012-10-11 21:14:42 +00:00
|
|
|
ifM ((==) uuid <$> getUUID)
|
|
|
|
( return Nothing
|
|
|
|
, do
|
2012-10-11 23:22:29 +00:00
|
|
|
syncable <- Config.repoSyncable $ Remote.repo r
|
|
|
|
if (syncable /= repoSyncable c)
|
2012-10-11 21:14:42 +00:00
|
|
|
then do
|
2012-10-11 23:22:29 +00:00
|
|
|
let key = Config.remoteConfig (Remote.repo r) "sync"
|
|
|
|
Config.setConfig key $
|
|
|
|
if syncable then "false" else "true"
|
2012-10-11 21:14:42 +00:00
|
|
|
void $ Remote.remoteListRefresh
|
2012-10-11 23:22:29 +00:00
|
|
|
return $ Just $ repoSyncable c
|
2012-10-11 21:14:42 +00:00
|
|
|
else return Nothing
|
|
|
|
)
|
|
|
|
where
|
|
|
|
uuid = Remote.uuid r
|
|
|
|
|
2012-10-11 23:22:29 +00:00
|
|
|
changeSyncable :: Remote -> Bool -> Handler ()
|
|
|
|
changeSyncable r True = syncRemote r
|
|
|
|
changeSyncable r False = do
|
2012-10-11 21:14:42 +00:00
|
|
|
webapp <- getYesod
|
|
|
|
let dstatus = daemonStatus webapp
|
|
|
|
let st = fromJust $ threadState webapp
|
|
|
|
liftIO $ runThreadState st $ updateKnownRemotes dstatus
|
2012-10-11 23:22:29 +00:00
|
|
|
{- Stop all transfers to or from this remote.
|
2012-10-11 21:14:42 +00:00
|
|
|
- XXX Can't stop any ongoing scan, or git syncs. -}
|
|
|
|
void $ liftIO $ dequeueTransfers (transferQueue webapp) dstatus tofrom
|
|
|
|
mapM_ (cancelTransfer False) =<<
|
|
|
|
filter tofrom . M.keys <$>
|
|
|
|
liftIO (currentTransfers <$> getDaemonStatus dstatus)
|
|
|
|
where
|
|
|
|
tofrom t = transferUUID t == Remote.uuid r
|
2012-10-10 23:13:49 +00:00
|
|
|
|
2012-10-09 19:11:48 +00:00
|
|
|
editRepositoryAForm :: RepoConfig -> AForm WebApp WebApp RepoConfig
|
|
|
|
editRepositoryAForm def = RepoConfig
|
|
|
|
<$> areq textField "Description" (Just $ repoDescription def)
|
2012-10-10 20:23:41 +00:00
|
|
|
<*> areq (selectFieldList $ customgroups++standardgroups) "Repository group" (Just $ repoGroup def)
|
2012-10-11 23:22:29 +00:00
|
|
|
<*> areq checkBoxField "Syncing enabled" (Just $ repoSyncable def)
|
2012-10-10 20:04:28 +00:00
|
|
|
where
|
2012-10-10 20:23:41 +00:00
|
|
|
standardgroups :: [(Text, RepoGroup)]
|
|
|
|
standardgroups = map (\g -> (T.pack $ descStandardGroup g , RepoGroupStandard g))
|
2012-10-10 20:04:28 +00:00
|
|
|
[minBound :: StandardGroup .. maxBound :: StandardGroup]
|
2012-10-10 20:23:41 +00:00
|
|
|
customgroups :: [(Text, RepoGroup)]
|
|
|
|
customgroups = case repoGroup def of
|
|
|
|
RepoGroupCustom s -> [(T.pack s, RepoGroupCustom s)]
|
|
|
|
_ -> []
|
2012-10-09 19:11:48 +00:00
|
|
|
|
2012-10-09 18:43:53 +00:00
|
|
|
getEditRepositoryR :: UUID -> Handler RepHtml
|
2012-10-11 21:35:08 +00:00
|
|
|
getEditRepositoryR = editForm False
|
|
|
|
|
|
|
|
getEditNewRepositoryR :: UUID -> Handler RepHtml
|
|
|
|
getEditNewRepositoryR = editForm True
|
|
|
|
|
|
|
|
editForm :: Bool -> UUID -> Handler RepHtml
|
|
|
|
editForm new uuid = bootstrap (Just Config) $ do
|
2012-10-09 19:11:48 +00:00
|
|
|
sideBarDisplay
|
|
|
|
setTitle "Configure repository"
|
2012-10-11 21:14:42 +00:00
|
|
|
|
|
|
|
r <- lift $ fromMaybe (error "Unknown UUID") . M.lookup uuid
|
|
|
|
<$> runAnnex M.empty (Remote.remoteMap id)
|
|
|
|
curr <- lift $ runAnnex undefined $ getRepoConfig r
|
2012-10-09 19:11:48 +00:00
|
|
|
((result, form), enctype) <- lift $
|
|
|
|
runFormGet $ renderBootstrap $ editRepositoryAForm curr
|
|
|
|
case result of
|
2012-10-10 23:13:49 +00:00
|
|
|
FormSuccess input -> lift $ do
|
2012-10-11 23:22:29 +00:00
|
|
|
maybe noop (changeSyncable r) =<<
|
2012-10-11 21:14:42 +00:00
|
|
|
runAnnex undefined (setRepoConfig r input)
|
2012-10-10 23:13:49 +00:00
|
|
|
redirect RepositoriesR
|
2012-10-11 21:35:08 +00:00
|
|
|
_ -> showform form enctype curr
|
2012-10-09 19:11:48 +00:00
|
|
|
where
|
2012-10-11 21:35:08 +00:00
|
|
|
showform form enctype curr = do
|
|
|
|
let istransfer = repoGroup curr == RepoGroupStandard TransferGroup
|
2012-10-09 19:11:48 +00:00
|
|
|
let authtoken = webAppFormAuthToken
|
|
|
|
$(widgetFile "configurators/editrepository")
|
2012-10-11 21:35:08 +00:00
|
|
|
|
|
|
|
|