2012-09-02 04:27:48 +00:00
|
|
|
{- git-annex assistant webapp core
|
2012-07-31 05:11:32 +00:00
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
2012-09-24 18:48:47 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-07-31 05:11:32 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings, RankNTypes #-}
|
|
|
|
|
|
|
|
module Assistant.WebApp where
|
|
|
|
|
2012-09-02 04:27:48 +00:00
|
|
|
import Assistant.WebApp.Types
|
2012-07-31 05:11:32 +00:00
|
|
|
import Assistant.Common
|
|
|
|
import Utility.NotificationBroadcaster
|
|
|
|
import Utility.Yesod
|
2012-09-18 21:50:07 +00:00
|
|
|
import Locations.UserConfig
|
2012-07-31 05:11:32 +00:00
|
|
|
|
|
|
|
import Yesod
|
2012-09-02 04:27:48 +00:00
|
|
|
import Data.Text (Text)
|
2012-07-31 05:11:32 +00:00
|
|
|
import Control.Concurrent.STM
|
2012-09-08 23:57:15 +00:00
|
|
|
import Control.Concurrent
|
2012-07-31 05:11:32 +00:00
|
|
|
|
2012-07-31 18:23:17 +00:00
|
|
|
inFirstRun :: Handler Bool
|
2012-08-03 13:44:43 +00:00
|
|
|
inFirstRun = isNothing . relDir <$> getYesod
|
2012-07-31 06:30:26 +00:00
|
|
|
|
2012-07-31 05:11:32 +00:00
|
|
|
newWebAppState :: IO (TMVar WebAppState)
|
2012-09-18 21:50:07 +00:00
|
|
|
newWebAppState = do
|
2013-01-03 19:34:50 +00:00
|
|
|
otherrepos <- listOtherRepos
|
2012-09-18 21:50:07 +00:00
|
|
|
atomically $ newTMVar $ WebAppState
|
|
|
|
{ showIntro = True
|
|
|
|
, otherRepos = otherrepos }
|
2012-07-31 05:11:32 +00:00
|
|
|
|
2012-10-30 21:14:26 +00:00
|
|
|
liftAssistant :: forall sub a. (Assistant a) -> GHandler sub WebApp a
|
2012-11-05 23:39:08 +00:00
|
|
|
liftAssistant a = liftIO . flip runAssistant a =<< assistantData <$> getYesod
|
2012-10-29 18:07:12 +00:00
|
|
|
|
2012-07-31 05:11:32 +00:00
|
|
|
getWebAppState :: forall sub. GHandler sub WebApp WebAppState
|
|
|
|
getWebAppState = liftIO . atomically . readTMVar =<< webAppState <$> getYesod
|
|
|
|
|
|
|
|
modifyWebAppState :: forall sub. (WebAppState -> WebAppState) -> GHandler sub WebApp ()
|
|
|
|
modifyWebAppState a = go =<< webAppState <$> getYesod
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
go s = liftIO $ atomically $ do
|
|
|
|
v <- takeTMVar s
|
|
|
|
putTMVar s $ a v
|
2012-07-31 05:11:32 +00:00
|
|
|
|
2012-07-31 15:19:40 +00:00
|
|
|
{- Runs an Annex action from the webapp.
|
|
|
|
-
|
|
|
|
- When the webapp is run outside a git-annex repository, the fallback
|
|
|
|
- value is returned.
|
|
|
|
-}
|
|
|
|
runAnnex :: forall sub a. a -> Annex a -> GHandler sub WebApp a
|
2012-10-29 04:15:43 +00:00
|
|
|
runAnnex fallback a = ifM (noAnnex <$> getYesod)
|
|
|
|
( return fallback
|
2012-10-30 21:14:26 +00:00
|
|
|
, liftAssistant $ liftAnnex a
|
2012-10-29 04:15:43 +00:00
|
|
|
)
|
2012-07-31 15:19:40 +00:00
|
|
|
|
2012-11-03 01:13:06 +00:00
|
|
|
waitNotifier :: forall sub. (Assistant NotificationBroadcaster) -> NotificationId -> GHandler sub WebApp ()
|
|
|
|
waitNotifier getbroadcaster nid = liftAssistant $ do
|
|
|
|
b <- getbroadcaster
|
|
|
|
liftIO $ waitNotification $ notificationHandleFromId b nid
|
2012-07-31 05:11:32 +00:00
|
|
|
|
2012-11-03 01:13:06 +00:00
|
|
|
newNotifier :: forall sub. (Assistant NotificationBroadcaster) -> GHandler sub WebApp NotificationId
|
|
|
|
newNotifier getbroadcaster = liftAssistant $ do
|
|
|
|
b <- getbroadcaster
|
|
|
|
liftIO $ notificationHandleToId <$> newNotificationHandle b
|
2012-07-31 05:11:32 +00:00
|
|
|
|
2012-07-31 21:57:08 +00:00
|
|
|
{- Adds the auth parameter as a hidden field on a form. Must be put into
|
|
|
|
- every form. -}
|
|
|
|
webAppFormAuthToken :: Widget
|
|
|
|
webAppFormAuthToken = do
|
|
|
|
webapp <- lift getYesod
|
|
|
|
[whamlet|<input type="hidden" name="auth" value="#{secretToken webapp}">|]
|
2012-08-08 21:07:38 +00:00
|
|
|
|
2012-09-27 16:55:00 +00:00
|
|
|
{- A button with an icon, and maybe label or tooltip, that can be
|
|
|
|
- clicked to perform some action.
|
2012-08-08 21:07:38 +00:00
|
|
|
- With javascript, clicking it POSTs the Route, and remains on the same
|
|
|
|
- page.
|
|
|
|
- With noscript, clicking it GETs the Route. -}
|
2012-09-27 16:55:00 +00:00
|
|
|
actionButton :: Route WebApp -> (Maybe String) -> (Maybe String) -> String -> String -> Widget
|
|
|
|
actionButton route label tooltip buttonclass iconclass = $(widgetFile "actionbutton")
|
2012-09-08 23:57:15 +00:00
|
|
|
|
|
|
|
type UrlRenderFunc = Route WebApp -> [(Text, Text)] -> Text
|
|
|
|
type UrlRenderer = MVar (UrlRenderFunc)
|
|
|
|
|
|
|
|
newUrlRenderer :: IO UrlRenderer
|
|
|
|
newUrlRenderer = newEmptyMVar
|
|
|
|
|
|
|
|
setUrlRenderer :: UrlRenderer -> (UrlRenderFunc) -> IO ()
|
|
|
|
setUrlRenderer = putMVar
|
|
|
|
|
|
|
|
{- Blocks until the webapp is running and has called setUrlRenderer. -}
|
|
|
|
renderUrl :: UrlRenderer -> Route WebApp -> [(Text, Text)] -> IO Text
|
|
|
|
renderUrl urlrenderer route params = do
|
|
|
|
r <- readMVar urlrenderer
|
|
|
|
return $ r route params
|
2012-09-09 05:02:44 +00:00
|
|
|
|
|
|
|
{- Redirects back to the referring page, or if there's none, HomeR -}
|
|
|
|
redirectBack :: Handler ()
|
|
|
|
redirectBack = do
|
|
|
|
clearUltDest
|
|
|
|
setUltDestReferer
|
|
|
|
redirectUltDest HomeR
|
2012-09-18 21:50:07 +00:00
|
|
|
|
2013-01-03 19:16:40 +00:00
|
|
|
controlMenu :: Widget
|
|
|
|
controlMenu = do
|
2012-09-18 21:50:07 +00:00
|
|
|
repolist <- lift $ otherRepos <$> getWebAppState
|
2013-01-03 19:16:40 +00:00
|
|
|
$(widgetFile "controlmenu")
|
2012-09-18 21:50:07 +00:00
|
|
|
|
2013-01-03 19:34:50 +00:00
|
|
|
listOtherRepos :: IO [(String, String)]
|
|
|
|
listOtherRepos = do
|
2012-09-18 21:50:07 +00:00
|
|
|
f <- autoStartFile
|
2013-01-03 19:34:50 +00:00
|
|
|
cwd <- getCurrentDirectory
|
|
|
|
dirs <- filter (\d -> not $ d `dirContains` cwd) . nub
|
|
|
|
<$> ifM (doesFileExist f) ( lines <$> readFile f, return [])
|
2012-09-18 21:50:07 +00:00
|
|
|
names <- mapM relHome dirs
|
|
|
|
return $ sort $ zip names dirs
|