summaryrefslogtreecommitdiff
path: root/Annex/Notification.hs
blob: 4793c9537c36fcbfc557cdcad9be14e15a0772e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{- git-annex desktop notifications
 -
 - Copyright 2014 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

{-# LANGUAGE CPP #-}

module Annex.Notification where

import Common.Annex
import Logs.Transfer
#ifdef WITH_DBUS_NOTIFICATIONS
import qualified Annex
import Types.DesktopNotify
import qualified DBus.Notify as Notify
import qualified DBus.Client
#endif

-- Witness that notification has happened.
data NotifyWitness = NotifyWitness

{- 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
notifyTransfer direction (Just f) a = do
	wanted <- Annex.getState Annex.desktopnotify
	let action = if direction == Upload then "uploading" else "downloading"
	let basedesc = action ++ " " ++ f
	let startdesc = "started " ++ basedesc
	let enddesc ok = if ok
		then "finished " ++ basedesc
		else basedesc ++ " failed"
	if (notifyStart wanted || notifyFinish wanted)
		then do
			client <- liftIO DBus.Client.connectSession
			startnotification <- liftIO $ if notifyStart wanted
				then Just <$> Notify.notify client (mkNote startdesc)
				else pure Nothing
			ok <- a NotifyWitness
			when (notifyFinish wanted) $ liftIO $ void $ maybe 
				(Notify.notify client $ mkNote $ enddesc ok)
				(\n -> Notify.replace client n $ mkNote $ enddesc ok)
				startnotification
			return ok
		else a NotifyWitness
#else
notifyTransfer _ (Just _) a = do a NotifyWitness
#endif

notifyDrop :: Maybe FilePath -> Bool -> Annex ()
notifyDrop Nothing _ = noop
#ifdef WITH_DBUS_NOTIFICATIONS
notifyDrop (Just f) ok = do
	wanted <- Annex.getState Annex.desktopnotify
	when (notifyFinish wanted) $ liftIO $ do
		client <- DBus.Client.connectSession
		let msg = if ok
			then "dropped " ++ f
			else "failed to drop" ++ f
		void $ Notify.notify client (mkNote msg)
#else
notifyDrop (Just _) _ = noop
#endif

#ifdef WITH_DBUS_NOTIFICATIONS
mkNote :: String -> Notify.Note
mkNote desc = Notify.blankNote
	{ Notify.appName = "git-annex"
	, Notify.summary = desc
	, Notify.hints =
		[ Notify.Category Notify.Transfer
		, Notify.Urgency Notify.Low
		, Notify.SuppressSound True
		]
	}
#endif