git-annex/Assistant/Alert.hs

312 lines
9.1 KiB
Haskell
Raw Normal View History

2012-07-29 13:35:01 +00:00
{- git-annex assistant alerts
-
2013-04-04 05:48:26 +00:00
- Copyright 2012, 2013 Joey Hess <joey@kitenet.net>
2012-07-29 13:35:01 +00:00
-
- Licensed under the GNU GPL version 3 or higher.
-}
2013-04-04 05:48:26 +00:00
{-# LANGUAGE OverloadedStrings, CPP #-}
2012-07-29 13:35:01 +00:00
module Assistant.Alert where
import Common.Annex
2013-04-04 05:48:26 +00:00
import Assistant.Types.Alert
import Assistant.Alert.Utility
import qualified Remote
import Utility.Tense
2012-08-06 21:09:23 +00:00
import Logs.Transfer
import Data.String
2013-04-04 05:48:26 +00:00
import qualified Data.Text as T
2012-07-29 13:35:01 +00:00
2013-04-04 05:48:26 +00:00
#ifdef WITH_WEBAPP
import Assistant.Monad
import Assistant.DaemonStatus
import Assistant.WebApp.Types
import Assistant.WebApp
2013-04-04 05:50:36 +00:00
import Yesod
2013-04-04 05:48:26 +00:00
#endif
{- Makes a button for an alert that opens a Route. The button will
- close the alert it's attached to when clicked. -}
#ifdef WITH_WEBAPP
mkAlertButton :: T.Text -> UrlRenderer -> Route WebApp -> Assistant AlertButton
mkAlertButton label urlrenderer route = do
close <- asIO1 removeAlert
url <- liftIO $ renderUrl urlrenderer route []
return $ AlertButton
{ buttonLabel = label
, buttonUrl = url
, buttonAction = Just close
}
2013-04-04 05:48:26 +00:00
#endif
2012-07-29 23:05:51 +00:00
2013-07-10 19:37:40 +00:00
renderData :: Alert -> TenseText
renderData = tenseWords . alertData
baseActivityAlert :: Alert
baseActivityAlert = Alert
{ alertClass = Activity
, alertHeader = Nothing
2013-07-10 19:37:40 +00:00
, alertMessageRender = renderData
2012-08-06 19:41:42 +00:00
, alertData = []
2013-07-10 19:37:40 +00:00
, alertCounter = 0
, alertBlockDisplay = False
, alertClosable = False
2012-07-29 23:05:51 +00:00
, alertPriority = Medium
, alertIcon = Just ActivityIcon
, alertCombiner = Nothing
, alertName = Nothing
, alertButton = Nothing
2012-07-29 13:35:01 +00:00
}
warningAlert :: String -> String -> Alert
warningAlert name msg = Alert
{ alertClass = Warning
, alertHeader = Just $ tenseWords ["warning"]
2013-07-10 19:37:40 +00:00
, alertMessageRender = renderData
, alertData = [UnTensed $ T.pack msg]
2013-07-10 19:37:40 +00:00
, alertCounter = 0
, alertBlockDisplay = True
, alertClosable = True
, alertPriority = High
, alertIcon = Just ErrorIcon
, alertCombiner = Just $ dataCombiner $ \_old new -> new
, alertName = Just $ WarningAlert name
, alertButton = Nothing
}
2012-08-06 19:41:42 +00:00
activityAlert :: Maybe TenseText -> [TenseChunk] -> Alert
activityAlert header dat = baseActivityAlert
{ alertHeader = header
2012-08-06 19:41:42 +00:00
, alertData = dat
}
startupScanAlert :: Alert
2012-09-13 04:57:52 +00:00
startupScanAlert = activityAlert Nothing
2012-08-06 19:41:42 +00:00
[Tensed "Performing" "Performed", "startup scan"]
{- Displayed when a shutdown is occurring, so will be seen after shutdown
- has happened. -}
shutdownAlert :: Alert
shutdownAlert = warningAlert "shutdown" "git-annex has been shut down"
2012-08-02 18:02:35 +00:00
commitAlert :: Alert
2012-09-13 04:57:52 +00:00
commitAlert = activityAlert Nothing
[Tensed "Committing" "Committed", "changes to git"]
showRemotes :: [Remote] -> TenseChunk
2013-03-18 23:11:46 +00:00
showRemotes = UnTensed . T.intercalate ", " . map (T.pack . Remote.name)
2012-08-02 18:02:35 +00:00
2012-08-06 21:09:23 +00:00
syncAlert :: [Remote] -> Alert
syncAlert rs = baseActivityAlert
{ alertName = Just SyncAlert
, alertHeader = Just $ tenseWords
2012-08-06 21:09:23 +00:00
[Tensed "Syncing" "Synced", "with", showRemotes rs]
2012-07-29 23:05:51 +00:00
, alertPriority = Low
, alertIcon = Just SyncIcon
2012-12-13 04:45:27 +00:00
}
syncResultAlert :: [Remote] -> [Remote] -> Alert
syncResultAlert succeeded failed = makeAlertFiller (not $ null succeeded) $
baseActivityAlert
{ alertName = Just SyncAlert
, alertHeader = Just $ tenseWords msg
}
where
msg
| null succeeded = ["Failed to sync with", showRemotes failed]
| null failed = ["Synced with", showRemotes succeeded]
| otherwise =
[ "Synced with", showRemotes succeeded
, "but not with", showRemotes failed
]
sanityCheckAlert :: Alert
sanityCheckAlert = activityAlert
(Just $ tenseWords [Tensed "Running" "Ran", "daily sanity check"])
2012-08-06 19:41:42 +00:00
["to make sure everything is ok."]
sanityCheckFixAlert :: String -> Alert
sanityCheckFixAlert msg = Alert
{ alertClass = Warning
, alertHeader = Just $ tenseWords ["Fixed a problem"]
2012-08-06 19:41:42 +00:00
, alertMessageRender = render
, alertData = [UnTensed $ T.pack msg]
2013-07-10 19:37:40 +00:00
, alertCounter = 0
, alertBlockDisplay = True
2012-07-29 23:05:51 +00:00
, alertPriority = High
, alertClosable = True
, alertIcon = Just ErrorIcon
, alertName = Just SanityCheckFixAlert
2012-08-06 19:41:42 +00:00
, alertCombiner = Just $ dataCombiner (++)
, alertButton = Nothing
}
2012-10-31 06:34:03 +00:00
where
2013-07-10 19:37:40 +00:00
render alert = tenseWords $ alerthead : alertData alert ++ [alertfoot]
2012-10-31 06:34:03 +00:00
alerthead = "The daily sanity check found and fixed a problem:"
alertfoot = "If these problems persist, consider filing a bug report."
pairingAlert :: AlertButton -> Alert
pairingAlert button = baseActivityAlert
{ alertData = [ UnTensed "Pairing in progress" ]
, alertPriority = High
, alertButton = Just button
}
pairRequestReceivedAlert :: String -> AlertButton -> Alert
pairRequestReceivedAlert who button = Alert
2012-09-11 20:11:28 +00:00
{ alertClass = Message
, alertHeader = Nothing
2013-07-10 19:37:40 +00:00
, alertMessageRender = renderData
, alertData = [UnTensed $ T.pack $ who ++ " is sending a pair request."]
2013-07-10 19:37:40 +00:00
, alertCounter = 0
2012-09-11 20:11:28 +00:00
, alertBlockDisplay = False
2012-09-08 19:07:44 +00:00
, alertPriority = High
, alertClosable = True
, alertIcon = Just InfoIcon
, alertName = Just $ PairAlert who
, alertCombiner = Just $ dataCombiner $ \_old new -> new
, alertButton = Just button
}
2012-09-13 04:57:52 +00:00
pairRequestAcknowledgedAlert :: String -> Maybe AlertButton -> Alert
pairRequestAcknowledgedAlert who button = baseActivityAlert
{ alertData = ["Pairing with", UnTensed (T.pack who), Tensed "in progress" "complete"]
, alertPriority = High
, alertName = Just $ PairAlert who
, alertCombiner = Just $ dataCombiner $ \_old new -> new
2012-09-11 19:43:33 +00:00
, alertButton = button
2012-09-08 19:07:44 +00:00
}
xmppNeededAlert :: AlertButton -> Alert
xmppNeededAlert button = Alert
2012-11-11 00:52:46 +00:00
{ alertHeader = Just "Share with friends, and keep your devices in sync across the cloud."
, alertIcon = Just TheCloud
, alertPriority = High
, alertButton = Just button
, alertClosable = True
, alertClass = Message
2013-07-10 19:37:40 +00:00
, alertMessageRender = renderData
, alertCounter = 0
, alertBlockDisplay = True
, alertName = Just $ XMPPNeededAlert
, alertCombiner = Just $ dataCombiner $ \_old new -> new
, alertData = []
}
cloudRepoNeededAlert :: Maybe String -> AlertButton -> Alert
cloudRepoNeededAlert friendname button = Alert
{ alertHeader = Just $ fromString $ unwords
[ "Unable to download files from"
, (fromMaybe "your other devices" friendname) ++ "."
]
, alertIcon = Just ErrorIcon
, alertPriority = High
, alertButton = Just button
, alertClosable = True
, alertClass = Message
2013-07-10 19:37:40 +00:00
, alertMessageRender = renderData
, alertCounter = 0
, alertBlockDisplay = True
, alertName = Just $ CloudRepoNeededAlert
, alertCombiner = Just $ dataCombiner $ \_old new -> new
, alertData = []
}
remoteRemovalAlert :: String -> AlertButton -> Alert
remoteRemovalAlert desc button = Alert
{ alertHeader = Just $ fromString $
"The repository \"" ++ desc ++
"\" has been emptied, and can now be removed."
, alertIcon = Just InfoIcon
, alertPriority = High
, alertButton = Just button
, alertClosable = True
, alertClass = Message
2013-07-10 19:37:40 +00:00
, alertMessageRender = renderData
, alertCounter = 0
, alertBlockDisplay = True
, alertName = Just $ RemoteRemovalAlert desc
, alertCombiner = Just $ dataCombiner $ \_old new -> new
, alertData = []
}
2013-07-10 19:37:40 +00:00
{- Show a message that relates to a list of files.
-
- The most recent several files are shown, and a count of any others. -}
fileAlert :: TenseChunk -> [FilePath] -> Alert
fileAlert msg files = (activityAlert Nothing shortfiles)
2012-08-06 21:09:23 +00:00
{ alertName = Just $ FileAlert msg
2013-07-10 19:37:40 +00:00
, alertMessageRender = renderer
, alertCounter = counter
, alertCombiner = Just $ fullCombiner combiner
}
2012-10-31 06:34:03 +00:00
where
2013-07-10 19:37:40 +00:00
maxfilesshown = 10
(somefiles, counter) = splitcounter (dedupadjacent files)
shortfiles = map (fromString . shortFile . takeFileName) somefiles
renderer alert = tenseWords $ msg : alertData alert ++ showcounter
where
showcounter = case alertCounter alert of
0 -> []
_ -> [fromString $ "and " ++ show (alertCounter alert) ++ " other files"]
2012-08-06 19:41:42 +00:00
2013-07-10 19:37:40 +00:00
dedupadjacent (x:y:rest)
| x == y = dedupadjacent (y:rest)
| otherwise = x : dedupadjacent (y:rest)
dedupadjacent (x:[]) = [x]
dedupadjacent [] = []
{- Note that this ensures the counter is never 1; no need to say
- "1 file" when the filename could be shown. -}
splitcounter l
| length l <= maxfilesshown = (l, 0)
| otherwise =
let (keep, rest) = splitAt (maxfilesshown - 1) l
in (keep, length rest)
combiner new old =
let (fs, n) = splitcounter $
dedupadjacent $ alertData new ++ alertData old
cnt = n + alertCounter new + alertCounter old
in old
{ alertData = fs
, alertCounter = cnt
}
addFileAlert :: [FilePath] -> Alert
2012-08-06 21:09:23 +00:00
addFileAlert = fileAlert (Tensed "Adding" "Added")
{- This is only used as a success alert after a transfer, not during it. -}
transferFileAlert :: Direction -> Bool -> FilePath -> Alert
2013-07-10 19:37:40 +00:00
transferFileAlert direction True file
| direction == Upload = fileAlert "Uploaded" [file]
| otherwise = fileAlert "Downloaded" [file]
transferFileAlert direction False file
| direction == Upload = fileAlert "Upload failed" [file]
| otherwise = fileAlert "Download failed" [file]
2012-08-06 21:09:23 +00:00
2012-08-06 19:41:42 +00:00
dataCombiner :: ([TenseChunk] -> [TenseChunk] -> [TenseChunk]) -> AlertCombiner
2013-07-10 19:37:40 +00:00
dataCombiner combiner = fullCombiner $
\new old -> old { alertData = alertData new `combiner` alertData old }
fullCombiner :: (Alert -> Alert -> Alert) -> AlertCombiner
fullCombiner combiner new old
2012-08-06 19:41:42 +00:00
| alertClass new /= alertClass old = Nothing
| alertName new == alertName old =
2013-07-10 19:37:40 +00:00
Just $! new `combiner` old
2012-08-06 19:41:42 +00:00
| otherwise = Nothing
2012-08-02 17:47:26 +00:00
shortFile :: FilePath -> String
shortFile f
| len < maxlen = f
| otherwise = take half f ++ ".." ++ drop (len - half) f
2012-10-31 06:34:03 +00:00
where
len = length f
maxlen = 20
half = (maxlen - 2) `div` 2
2012-08-02 17:47:26 +00:00