git-annex/Assistant/WebApp/Configurators.hs

203 lines
5.8 KiB
Haskell
Raw Normal View History

2012-07-31 05:11:32 +00:00
{- git-annex assistant webapp configurators
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU AGPL version 3 or higher.
2012-07-31 05:11:32 +00:00
-}
2012-10-18 14:14:49 +00:00
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings, RankNTypes, CPP #-}
2012-07-31 05:11:32 +00:00
module Assistant.WebApp.Configurators where
2012-11-25 04:26:46 +00:00
import Assistant.WebApp.Common
2012-10-30 18:44:18 +00:00
import Assistant.DaemonStatus
2012-11-13 21:50:54 +00:00
import Assistant.WebApp.Notifications
import Assistant.WebApp.Utility
2012-08-31 19:17:12 +00:00
import Assistant.WebApp.Configurators.Local
import qualified Annex
2012-07-31 05:11:32 +00:00
import qualified Remote
import qualified Types.Remote as Remote
2012-07-31 05:11:32 +00:00
import Annex.UUID (getUUID)
import Logs.Remote
import Logs.Trust
import qualified Git
2012-11-02 16:59:31 +00:00
#ifdef WITH_XMPP
import Assistant.XMPP.Client
#endif
2012-07-31 05:11:32 +00:00
import qualified Data.Map as M
2012-07-31 05:11:32 +00:00
{- The main configuration screen. -}
getConfigurationR :: Handler RepHtml
getConfigurationR = ifM (inFirstRun)
( getFirstRepositoryR
, page "Configuration" (Just Configuration) $ do
2012-11-02 16:59:31 +00:00
#ifdef WITH_XMPP
2012-10-26 21:13:30 +00:00
xmppconfigured <- lift $ runAnnex False $ isJust <$> getXMPPCreds
2012-11-02 16:59:31 +00:00
#else
let xmppconfigured = False
#endif
$(widgetFile "configurators/main")
)
{- An intro message, list of repositories, and nudge to make more. -}
introDisplay :: Text -> Widget
introDisplay ident = do
webapp <- lift getYesod
2012-11-13 21:50:54 +00:00
repolist <- lift $ repoList $ RepoSelector
{ onlyCloud = False
, onlyConfigured = True
, includeHere = False
}
let n = length repolist
let numrepos = show n
$(widgetFile "configurators/intro")
lift $ modifyWebAppState $ \s -> s { showIntro = False }
makeMiscRepositories :: Widget
makeMiscRepositories = $(widgetFile "configurators/repositories/misc")
makeCloudRepositories :: Widget
makeCloudRepositories = $(widgetFile "configurators/repositories/cloud")
2012-08-05 23:55:06 +00:00
{- Lists known repositories, followed by options to add more. -}
getRepositoriesR :: Handler RepHtml
getRepositoriesR = page "Repositories" (Just Configuration) $ do
2012-11-13 21:50:54 +00:00
let repolist = repoListDisplay $ RepoSelector
{ onlyCloud = False
, onlyConfigured = False
, includeHere = True
}
2012-08-05 23:55:06 +00:00
$(widgetFile "configurators/repositories")
2012-08-04 00:40:34 +00:00
data Actions
= DisabledRepoActions
{ setupRepoLink :: Route WebApp }
| SyncingRepoActions
{ setupRepoLink :: Route WebApp
, syncToggleLink :: Route WebApp
}
| NotSyncingRepoActions
{ setupRepoLink :: Route WebApp
, syncToggleLink :: Route WebApp
}
mkSyncingRepoActions :: UUID -> Actions
mkSyncingRepoActions u = SyncingRepoActions
{ setupRepoLink = EditRepositoryR u
, syncToggleLink = DisableSyncR u
}
mkNotSyncingRepoActions :: UUID -> Actions
mkNotSyncingRepoActions u = NotSyncingRepoActions
{ setupRepoLink = EditRepositoryR u
, syncToggleLink = EnableSyncR u
}
needsEnabled :: Actions -> Bool
needsEnabled (DisabledRepoActions _) = True
needsEnabled _ = False
notSyncing :: Actions -> Bool
notSyncing (SyncingRepoActions _ _) = False
notSyncing _ = True
2012-11-13 21:50:54 +00:00
{- Called by client to get a list of repos, that refreshes
- when new repos as added.
-
- Returns a div, which will be inserted into the calling page.
-}
getRepoListR :: RepoListNotificationId -> Handler RepHtml
getRepoListR (RepoListNotificationId nid reposelector) = do
waitNotifier getRepoListBroadcaster nid
2012-11-25 04:26:46 +00:00
p <- widgetToPageContent $ repoListDisplay reposelector
hamletToRepHtml $ [hamlet|^{pageBody p}|]
2012-11-13 21:50:54 +00:00
repoListDisplay :: RepoSelector -> Widget
repoListDisplay reposelector = do
autoUpdate ident (NotifierRepoListR reposelector) (10 :: Int) (10 :: Int)
repolist <- lift $ repoList reposelector
$(widgetFile "configurators/repositories/list")
where
ident = "repolist"
type RepoList = [(String, String, Actions)]
{- A numbered list of known repositories,
- with actions that can be taken on them. -}
2012-11-13 21:50:54 +00:00
repoList :: RepoSelector -> Handler RepoList
repoList reposelector
| onlyConfigured reposelector = list =<< configured
| otherwise = list =<< (++) <$> configured <*> rest
2012-10-31 06:34:03 +00:00
where
configured = do
rs <- filter wantedrepo . syncRemotes
2012-10-31 06:34:03 +00:00
<$> liftAssistant getDaemonStatus
runAnnex [] $ do
let us = map Remote.uuid rs
let l = zip us $ map mkSyncingRepoActions us
if includeHere reposelector
then do
u <- getUUID
autocommit <- annexAutoCommit <$> Annex.getGitConfig
let hereactions = if autocommit
then mkSyncingRepoActions u
else mkNotSyncingRepoActions u
let here = (u, hereactions)
return $ here : l
else return l
2012-10-31 06:34:03 +00:00
rest = runAnnex [] $ do
m <- readRemoteLog
unconfigured <- map snd . catMaybes . filter wantedremote
2012-11-11 04:26:29 +00:00
. map (findinfo m)
<$> (trustExclude DeadTrusted $ M.keys m)
unsyncable <- map Remote.uuid . filter wantedrepo .
filter (not . remoteAnnexSync . Remote.gitconfig)
<$> Remote.enabledRemoteList
2012-10-31 06:34:03 +00:00
return $ zip unsyncable (map mkNotSyncingRepoActions unsyncable) ++ unconfigured
wantedrepo r
| Remote.readonly r = False
2012-11-13 21:50:54 +00:00
| onlyCloud reposelector = Git.repoIsUrl (Remote.repo r) && not (isXMPPRemote r)
| otherwise = True
wantedremote Nothing = False
wantedremote (Just (iscloud, _))
2012-11-13 21:50:54 +00:00
| onlyCloud reposelector = iscloud
| otherwise = True
findinfo m u = case M.lookup u m of
2012-10-31 06:34:03 +00:00
Nothing -> Nothing
Just c -> case M.lookup "type" c of
Just "rsync" -> val True EnableRsyncR
Just "directory" -> val False EnableDirectoryR
2012-10-18 14:14:49 +00:00
#ifdef WITH_S3
Just "S3" -> val True EnableS3R
#endif
2012-11-24 20:30:15 +00:00
Just "glacier" -> val True EnableGlacierR
#ifdef WITH_WEBDAV
Just "webdav" -> val True EnableWebDAVR
2012-10-18 14:14:49 +00:00
#endif
2012-10-31 06:34:03 +00:00
_ -> Nothing
where
val iscloud r = Just (iscloud, (u, DisabledRepoActions $ r u))
2012-10-31 06:34:03 +00:00
list l = runAnnex [] $ do
let l' = nubBy (\x y -> fst x == fst y) l
zip3
<$> pure counter
<*> Remote.prettyListUUIDs (map fst l')
<*> pure (map snd l')
counter = map show ([1..] :: [Int])
getEnableSyncR :: UUID -> Handler ()
getEnableSyncR = flipSync True
getDisableSyncR :: UUID -> Handler ()
getDisableSyncR = flipSync False
flipSync :: Bool -> UUID -> Handler ()
flipSync enable uuid = do
mremote <- runAnnex undefined $ Remote.remoteFromUUID uuid
changeSyncable mremote enable
redirect RepositoriesR