2014-03-22 19:01:48 +00:00
|
|
|
{- git-annex desktop notifications
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2014 Joey Hess <id@joeyh.name>
|
2014-03-22 19:01:48 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2016-12-02 20:39:01 +00:00
|
|
|
module Annex.Notification (NotifyWitness, noNotification, notifyTransfer, notifyDrop) where
|
2014-03-22 19:01:48 +00:00
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2016-08-03 16:37:12 +00:00
|
|
|
import Types.Transfer
|
2014-03-22 19:01:48 +00:00
|
|
|
#ifdef WITH_DBUS_NOTIFICATIONS
|
2014-04-02 22:09:27 +00:00
|
|
|
import qualified Annex
|
2014-03-22 19:01:48 +00:00
|
|
|
import Types.DesktopNotify
|
|
|
|
import qualified DBus.Notify as Notify
|
|
|
|
import qualified DBus.Client
|
|
|
|
#endif
|
|
|
|
|
|
|
|
-- Witness that notification has happened.
|
|
|
|
data NotifyWitness = NotifyWitness
|
|
|
|
|
2016-12-02 20:39:01 +00:00
|
|
|
-- Only use when no notification should be done.
|
|
|
|
noNotification :: NotifyWitness
|
|
|
|
noNotification = NotifyWitness
|
|
|
|
|
2014-03-22 19:01:48 +00:00
|
|
|
{- Wrap around an action that performs a transfer, which may run multiple
|
|
|
|
- attempts. Displays notification when supported and when the user asked
|
|
|
|
- for it. -}
|
|
|
|
notifyTransfer :: Direction -> Maybe FilePath -> (NotifyWitness -> Annex Bool) -> Annex Bool
|
|
|
|
notifyTransfer _ Nothing a = a NotifyWitness
|
|
|
|
#ifdef WITH_DBUS_NOTIFICATIONS
|
2014-04-02 22:09:27 +00:00
|
|
|
notifyTransfer direction (Just f) a = do
|
2014-03-22 19:01:48 +00:00
|
|
|
wanted <- Annex.getState Annex.desktopnotify
|
|
|
|
if (notifyStart wanted || notifyFinish wanted)
|
|
|
|
then do
|
|
|
|
client <- liftIO DBus.Client.connectSession
|
|
|
|
startnotification <- liftIO $ if notifyStart wanted
|
2014-04-05 13:59:12 +00:00
|
|
|
then Just <$> Notify.notify client (startedTransferNote direction f)
|
2014-03-22 19:01:48 +00:00
|
|
|
else pure Nothing
|
|
|
|
ok <- a NotifyWitness
|
|
|
|
when (notifyFinish wanted) $ liftIO $ void $ maybe
|
2014-04-05 13:59:12 +00:00
|
|
|
(Notify.notify client $ finishedTransferNote ok direction f)
|
|
|
|
(\n -> Notify.replace client n $ finishedTransferNote ok direction f)
|
2014-03-22 19:01:48 +00:00
|
|
|
startnotification
|
|
|
|
return ok
|
|
|
|
else a NotifyWitness
|
|
|
|
#else
|
2015-04-11 04:10:34 +00:00
|
|
|
notifyTransfer _ (Just _) a = a NotifyWitness
|
2014-03-22 19:01:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
notifyDrop :: Maybe FilePath -> Bool -> Annex ()
|
|
|
|
notifyDrop Nothing _ = noop
|
|
|
|
#ifdef WITH_DBUS_NOTIFICATIONS
|
2014-04-02 22:09:27 +00:00
|
|
|
notifyDrop (Just f) ok = do
|
2014-03-22 19:01:48 +00:00
|
|
|
wanted <- Annex.getState Annex.desktopnotify
|
|
|
|
when (notifyFinish wanted) $ liftIO $ do
|
|
|
|
client <- DBus.Client.connectSession
|
2014-04-05 13:59:12 +00:00
|
|
|
void $ Notify.notify client (droppedNote ok f)
|
2014-03-22 19:01:48 +00:00
|
|
|
#else
|
2014-04-02 22:09:27 +00:00
|
|
|
notifyDrop (Just _) _ = noop
|
2014-03-22 19:01:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_DBUS_NOTIFICATIONS
|
2014-04-05 13:59:12 +00:00
|
|
|
startedTransferNote :: Direction -> FilePath -> Notify.Note
|
|
|
|
startedTransferNote Upload = mkNote Notify.Transfer Notify.Low iconUpload
|
|
|
|
"Uploading"
|
|
|
|
startedTransferNote Download = mkNote Notify.Transfer Notify.Low iconDownload
|
|
|
|
"Downloading"
|
|
|
|
|
|
|
|
finishedTransferNote :: Bool -> Direction -> FilePath -> Notify.Note
|
|
|
|
finishedTransferNote False Upload = mkNote Notify.TransferError Notify.Normal iconFailure
|
|
|
|
"Failed to upload"
|
|
|
|
finishedTransferNote False Download = mkNote Notify.TransferError Notify.Normal iconFailure
|
|
|
|
"Failed to download"
|
|
|
|
finishedTransferNote True Upload = mkNote Notify.TransferComplete Notify.Low iconSuccess
|
|
|
|
"Finished uploading"
|
|
|
|
finishedTransferNote True Download = mkNote Notify.TransferComplete Notify.Low iconSuccess
|
|
|
|
"Finished downloading"
|
|
|
|
|
|
|
|
droppedNote :: Bool -> FilePath -> Notify.Note
|
|
|
|
droppedNote False = mkNote Notify.TransferError Notify.Normal iconFailure
|
|
|
|
"Failed to drop"
|
|
|
|
droppedNote True = mkNote Notify.TransferComplete Notify.Low iconSuccess
|
|
|
|
"Dropped"
|
|
|
|
|
|
|
|
iconUpload, iconDownload, iconFailure, iconSuccess :: String
|
|
|
|
iconUpload = "network-transmit"
|
|
|
|
iconDownload = "network-receive"
|
|
|
|
iconFailure = "dialog-error"
|
|
|
|
iconSuccess = "git-annex" -- Is there a standard icon for success/completion?
|
|
|
|
|
|
|
|
mkNote :: Notify.Category -> Notify.UrgencyLevel -> String -> String -> FilePath -> Notify.Note
|
|
|
|
mkNote category urgency icon desc path = Notify.blankNote
|
2014-03-22 19:01:48 +00:00
|
|
|
{ Notify.appName = "git-annex"
|
2014-04-05 13:59:12 +00:00
|
|
|
, Notify.appImage = Just (Notify.Icon icon)
|
|
|
|
, Notify.summary = desc ++ " " ++ path
|
2014-03-22 19:01:48 +00:00
|
|
|
, Notify.hints =
|
2014-04-05 13:59:12 +00:00
|
|
|
[ Notify.Category category
|
|
|
|
, Notify.Urgency urgency
|
2014-03-22 19:01:48 +00:00
|
|
|
, Notify.SuppressSound True
|
|
|
|
]
|
|
|
|
}
|
|
|
|
#endif
|