2013-03-13 01:51:03 +00:00
|
|
|
{- git-annex assistant webapp repository list
|
|
|
|
-
|
2013-03-15 02:10:51 +00:00
|
|
|
- Copyright 2012,2013 Joey Hess <joey@kitenet.net>
|
2013-03-13 01:51:03 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings, RankNTypes, CPP #-}
|
|
|
|
|
|
|
|
module Assistant.WebApp.RepoList where
|
|
|
|
|
|
|
|
import Assistant.WebApp.Common
|
|
|
|
import Assistant.DaemonStatus
|
|
|
|
import Assistant.WebApp.Notifications
|
|
|
|
import Assistant.WebApp.Utility
|
|
|
|
import qualified Annex
|
|
|
|
import qualified Remote
|
|
|
|
import qualified Types.Remote as Remote
|
2013-03-13 21:59:33 +00:00
|
|
|
import Remote.List (remoteListRefresh)
|
2013-03-13 01:51:03 +00:00
|
|
|
import Annex.UUID (getUUID)
|
|
|
|
import Logs.Remote
|
|
|
|
import Logs.Trust
|
2013-03-13 21:59:33 +00:00
|
|
|
import Config
|
|
|
|
import Config.Cost
|
2013-03-13 01:51:03 +00:00
|
|
|
import qualified Git
|
|
|
|
#ifdef WITH_XMPP
|
|
|
|
#endif
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
2013-03-14 17:33:30 +00:00
|
|
|
import qualified Data.Set as S
|
2013-03-14 17:12:27 +00:00
|
|
|
import qualified Data.Text as T
|
2013-03-13 01:51:03 +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
|
2013-03-14 15:55:36 +00:00
|
|
|
notSyncing (SyncingRepoActions _ _) = False
|
2013-03-13 01:51:03 +00:00
|
|
|
notSyncing _ = True
|
|
|
|
|
|
|
|
{- Called by client to get a list of repos, that refreshes
|
2013-03-15 04:34:42 +00:00
|
|
|
- when new repos are added.
|
2013-03-13 01:51:03 +00:00
|
|
|
-
|
|
|
|
- Returns a div, which will be inserted into the calling page.
|
|
|
|
-}
|
|
|
|
getRepoListR :: RepoListNotificationId -> Handler RepHtml
|
|
|
|
getRepoListR (RepoListNotificationId nid reposelector) = do
|
|
|
|
waitNotifier getRepoListBroadcaster nid
|
|
|
|
p <- widgetToPageContent $ repoListDisplay reposelector
|
|
|
|
hamletToRepHtml $ [hamlet|^{pageBody p}|]
|
|
|
|
|
2013-03-15 04:34:42 +00:00
|
|
|
mainRepoSelector :: RepoSelector
|
|
|
|
mainRepoSelector = RepoSelector
|
|
|
|
{ onlyCloud = False
|
|
|
|
, onlyConfigured = False
|
|
|
|
, includeHere = True
|
|
|
|
, nudgeAddMore = False
|
|
|
|
}
|
|
|
|
|
2013-03-15 21:52:41 +00:00
|
|
|
{- List of cloud repositories, configured and not. -}
|
|
|
|
cloudRepoList :: Widget
|
|
|
|
cloudRepoList = repoListDisplay $ RepoSelector
|
|
|
|
{ onlyCloud = True
|
|
|
|
, onlyConfigured = False
|
|
|
|
, includeHere = False
|
|
|
|
, nudgeAddMore = False
|
|
|
|
}
|
|
|
|
|
2013-03-13 01:51:03 +00:00
|
|
|
repoListDisplay :: RepoSelector -> Widget
|
|
|
|
repoListDisplay reposelector = do
|
|
|
|
autoUpdate ident (NotifierRepoListR reposelector) (10 :: Int) (10 :: Int)
|
2013-03-14 02:47:16 +00:00
|
|
|
addScript $ StaticR jquery_ui_core_js
|
|
|
|
addScript $ StaticR jquery_ui_widget_js
|
|
|
|
addScript $ StaticR jquery_ui_mouse_js
|
|
|
|
addScript $ StaticR jquery_ui_sortable_js
|
2013-03-13 01:51:03 +00:00
|
|
|
|
|
|
|
repolist <- lift $ repoList reposelector
|
2013-03-15 04:34:42 +00:00
|
|
|
let addmore = nudgeAddMore reposelector
|
|
|
|
let nootherrepos = length repolist < 2
|
2013-03-13 01:51:03 +00:00
|
|
|
|
2013-03-15 02:10:51 +00:00
|
|
|
$(widgetFile "repolist")
|
2013-03-13 01:51:03 +00:00
|
|
|
where
|
|
|
|
ident = "repolist"
|
|
|
|
|
2013-03-15 04:34:42 +00:00
|
|
|
type RepoList = [(String, UUID, Actions)]
|
2013-03-13 01:51:03 +00:00
|
|
|
|
2013-03-15 04:34:42 +00:00
|
|
|
{- A list of known repositories, with actions that can be taken on them. -}
|
2013-03-13 01:51:03 +00:00
|
|
|
repoList :: RepoSelector -> Handler RepoList
|
|
|
|
repoList reposelector
|
|
|
|
| onlyConfigured reposelector = list =<< configured
|
2013-03-14 17:33:30 +00:00
|
|
|
| otherwise = list =<< (++) <$> configured <*> unconfigured
|
2013-03-13 01:51:03 +00:00
|
|
|
where
|
|
|
|
configured = do
|
2013-03-14 17:33:30 +00:00
|
|
|
syncing <- S.fromList . syncRemotes
|
2013-03-13 01:51:03 +00:00
|
|
|
<$> liftAssistant getDaemonStatus
|
|
|
|
liftAnnex $ do
|
2013-03-14 18:15:35 +00:00
|
|
|
rs <- filter wantedrepo . concat . Remote.byCost
|
|
|
|
<$> Remote.enabledRemoteList
|
2013-03-13 01:51:03 +00:00
|
|
|
let us = map Remote.uuid rs
|
2013-03-14 17:33:30 +00:00
|
|
|
let make r = if r `S.member` syncing
|
|
|
|
then mkSyncingRepoActions $ Remote.uuid r
|
|
|
|
else mkNotSyncingRepoActions $ Remote.uuid r
|
|
|
|
let l = zip us $ map make rs
|
2013-03-13 01:51:03 +00:00
|
|
|
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
|
2013-03-14 17:33:30 +00:00
|
|
|
unconfigured = liftAnnex $ do
|
2013-03-13 01:51:03 +00:00
|
|
|
m <- readRemoteLog
|
2013-03-14 17:33:30 +00:00
|
|
|
map snd . catMaybes . filter wantedremote
|
2013-03-13 01:51:03 +00:00
|
|
|
. map (findinfo m)
|
|
|
|
<$> (trustExclude DeadTrusted $ M.keys m)
|
|
|
|
wantedrepo r
|
|
|
|
| Remote.readonly r = False
|
|
|
|
| onlyCloud reposelector = Git.repoIsUrl (Remote.repo r) && not (isXMPPRemote r)
|
|
|
|
| otherwise = True
|
|
|
|
wantedremote Nothing = False
|
|
|
|
wantedremote (Just (iscloud, _))
|
|
|
|
| onlyCloud reposelector = iscloud
|
|
|
|
| otherwise = True
|
|
|
|
findinfo m u = case M.lookup u m of
|
|
|
|
Nothing -> Nothing
|
|
|
|
Just c -> case M.lookup "type" c of
|
|
|
|
Just "rsync" -> val True EnableRsyncR
|
|
|
|
Just "directory" -> val False EnableDirectoryR
|
|
|
|
#ifdef WITH_S3
|
|
|
|
Just "S3" -> val True EnableS3R
|
|
|
|
#endif
|
|
|
|
Just "glacier" -> val True EnableGlacierR
|
|
|
|
#ifdef WITH_WEBDAV
|
|
|
|
Just "webdav" -> val True EnableWebDAVR
|
|
|
|
#endif
|
|
|
|
_ -> Nothing
|
|
|
|
where
|
|
|
|
val iscloud r = Just (iscloud, (u, DisabledRepoActions $ r u))
|
|
|
|
list l = liftAnnex $ do
|
|
|
|
let l' = nubBy (\x y -> fst x == fst y) l
|
2013-03-15 04:34:42 +00:00
|
|
|
l'' <- zip
|
|
|
|
<$> Remote.prettyListUUIDs (map fst l')
|
2013-03-14 15:55:36 +00:00
|
|
|
<*> pure l'
|
2013-03-15 04:34:42 +00:00
|
|
|
return $ map (\(name, (uuid, actions)) -> (name, uuid, actions)) l''
|
2013-03-13 01:51:03 +00:00
|
|
|
|
|
|
|
getEnableSyncR :: UUID -> Handler ()
|
|
|
|
getEnableSyncR = flipSync True
|
|
|
|
|
|
|
|
getDisableSyncR :: UUID -> Handler ()
|
|
|
|
getDisableSyncR = flipSync False
|
|
|
|
|
|
|
|
flipSync :: Bool -> UUID -> Handler ()
|
|
|
|
flipSync enable uuid = do
|
|
|
|
mremote <- liftAnnex $ Remote.remoteFromUUID uuid
|
|
|
|
changeSyncable mremote enable
|
2013-03-15 04:34:42 +00:00
|
|
|
redirectBack
|
2013-03-13 21:59:33 +00:00
|
|
|
|
2013-03-14 15:55:36 +00:00
|
|
|
getRepositoriesReorderR :: Handler ()
|
|
|
|
getRepositoriesReorderR = do
|
2013-03-14 17:12:27 +00:00
|
|
|
{- Get uuid of the moved item, and the list it was moved within. -}
|
|
|
|
moved <- fromjs <$> runInputGet (ireq textField "moved")
|
|
|
|
list <- map fromjs <$> lookupGetParams "list[]"
|
2013-03-14 02:47:16 +00:00
|
|
|
void $ liftAnnex $ do
|
2013-03-13 21:59:33 +00:00
|
|
|
remote <- fromMaybe (error "Unknown UUID") <$>
|
2013-03-14 17:12:27 +00:00
|
|
|
Remote.remoteFromUUID moved
|
2013-03-14 19:23:45 +00:00
|
|
|
rs <- catMaybes <$> mapM Remote.remoteFromUUID list
|
|
|
|
forM_ (reorderCosts remote rs) $ \(r, newcost) ->
|
2013-03-14 17:12:27 +00:00
|
|
|
when (Remote.cost r /= newcost) $
|
|
|
|
setRemoteCost r newcost
|
2013-03-13 21:59:33 +00:00
|
|
|
remoteListRefresh
|
|
|
|
liftAssistant updateSyncRemotes
|
|
|
|
where
|
2013-03-14 20:22:18 +00:00
|
|
|
fromjs = toUUID . T.unpack
|
2013-03-14 17:12:27 +00:00
|
|
|
|
2013-03-14 19:23:45 +00:00
|
|
|
reorderCosts :: Remote -> [Remote] -> [(Remote, Cost)]
|
|
|
|
reorderCosts remote rs = zip rs'' (insertCostAfter costs i)
|
2013-03-14 17:12:27 +00:00
|
|
|
where
|
2013-03-14 19:23:45 +00:00
|
|
|
{- Find the index of the remote in the list that the remote
|
2013-03-14 17:12:27 +00:00
|
|
|
- was moved to be after.
|
|
|
|
- If it was moved to the start of the list, -1 -}
|
2013-03-14 19:23:45 +00:00
|
|
|
i = fromMaybe 0 (elemIndex remote rs) - 1
|
|
|
|
rs' = filter (\r -> Remote.uuid r /= Remote.uuid remote) rs
|
2013-03-14 17:12:27 +00:00
|
|
|
costs = map Remote.cost rs'
|
|
|
|
rs'' = (\(x, y) -> x ++ [remote] ++ y) $ splitAt (i + 1) rs'
|