-
Notifications
You must be signed in to change notification settings - Fork 1
/
notify.go
265 lines (230 loc) · 7.1 KB
/
notify.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package webkitgtk
import (
"context"
"fmt"
"github.com/godbus/dbus/v5"
"sync"
"time"
)
const (
notifierDbusObjectPath = "/org/freedesktop/Notifications"
notifierDbusInterfacePath = "org.freedesktop.Notifications"
)
type notificationAction struct {
label string
callback func()
}
type Notification struct {
notifier *dbusNotify
id uint32 // The notification ID.
replaceID uint32 // The optional notification to replace.
icon []byte // The optional icon data.
title string // The summary text briefly describing the notification.
message string // The optional detailed body text.
actions []notificationAction // The actions send a request message back to the notification client when invoked.
onClose []func() // The optional callback function to be called when the notification is closed.
hints map[string]interface{} // hints are a way to provide extra data to a notification server.
timeout time.Duration // The timeout since the display of the notification at which the notification should
}
func (a *App) Notify(title, message string) *Notification {
return &Notification{
notifier: a.notifier,
message: message,
title: title,
}
}
func (n *Notification) Icon(icon []byte) *Notification {
n.icon = icon
return n
}
func (n *Notification) Timeout(timeout time.Duration) *Notification {
n.timeout = timeout
return n
}
func (n *Notification) Action(label string, callback func()) *Notification {
n.actions = append(n.actions, notificationAction{
label: label,
callback: callback,
})
return n
}
func (n *Notification) Closed(callback func()) *Notification {
n.onClose = append(n.onClose, callback)
return n
}
type dbusNotify struct {
log logFunc
conn *dbus.Conn
appName string
appIcon string
notifications_ sync.Mutex
notifications map[uint32]*Notification
server string // Notification Server Name
vendor string // Notification Server Vendor
version string // Notification Server Version
specification string // Spec Version
actionIcons bool // Supports using icons instead of text for displaying actions.
actions bool // The server will provide any specified actions to the user.
body bool // Supports body text. Some implementations may only show the summary.
bodyHyperlinks bool // The server supports hyperlinks in the notifications.
bodyImages bool // The server supports images in the notifications.
bodyMarkup bool // Supports markup in the body text.
iconMulti bool // The server will render an animation of all the frames in a given image array.
iconStatic bool // Supports display of exactly 1 frame of any given image array.
persistence bool // The server supports persistence of notifications.
sound bool // The server supports sounds on notifications.
}
func (n *dbusNotify) Start(conn *dbus.Conn) error {
n.log = newLogFunc("dbus-notify")
n.conn = conn
n.notifications = make(map[uint32]*Notification)
/////////////
var d = make(chan *dbus.Call, 1)
var o = conn.Object(notifierDbusInterfacePath, notifierDbusObjectPath)
o.GoWithContext(context.Background(),
"org.freedesktop.Notifications.GetServerInformation",
0,
d)
err := (<-d).Store(&n.server,
&n.vendor,
&n.version,
&n.specification)
if err != nil {
return fmt.Errorf("error getting notification server information: %w", err)
}
n.log("notification server information", "server", n.server, "vendor", n.vendor, "version", n.version, "specification", n.specification)
//var d = make(chan *dbus.Call, 1)
//var o = n.dbus.conn.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
var s = make([]string, 0)
o.GoWithContext(context.Background(),
"org.freedesktop.Notifications.GetCapabilities",
0,
d)
err = (<-d).Store(&s)
if err != nil {
return fmt.Errorf("error getting notification server capabilities: %w", err)
}
for _, v := range s {
switch v {
case "action-icons":
n.actionIcons = true
break
case "actions":
n.actions = true
break
case "body":
n.body = true
break
case "body-hyperlinks":
n.bodyHyperlinks = true
break
case "body-images":
n.bodyImages = true
break
case "body-markup":
n.bodyMarkup = true
break
case "icon-multi":
n.iconMulti = true
break
case "icon-static":
n.iconStatic = true
break
case "persistence":
n.persistence = true
break
case "sound":
n.sound = true
break
}
}
n.log("notification server capabilities", "action-icons", n.actionIcons, "actions", n.actions, "body", n.body, "body-hyperlinks", n.bodyHyperlinks, "body-images", n.bodyImages, "body-markup", n.bodyMarkup, "icon-multi", n.iconMulti, "icon-static", n.iconStatic, "persistence", n.persistence, "sound", n.sound)
n.log("started")
return nil
}
// Show sends the information in the notification object to the server to be
// displayed.
func (n *Notification) Show() (uint32, error) {
timeout := int32(-1)
if n.timeout > 0 {
timeout = int32(n.timeout.Milliseconds())
}
// We need to convert the interface type of the map to dbus.Variant as
// people dont want to have to import the dbus package just to make use
// of the notification hints.
hints := make(map[string]interface{})
for k, v := range n.hints {
hints[k] = dbus.MakeVariant(v)
}
var actions []string
if n.notifier.actions {
for _, v := range n.actions {
actions = append(actions, v.label)
actions = append(actions, v.label)
}
}
var appIcon string
var d = make(chan *dbus.Call, 1)
var o = n.notifier.conn.Object(notifierDbusInterfacePath, notifierDbusObjectPath)
o.GoWithContext(context.Background(),
"org.freedesktop.Notifications.Notify",
0,
d,
n.notifier.appName,
n.replaceID,
appIcon,
n.title,
n.message,
actions,
hints,
timeout)
err := (<-d).Store(&n.id)
if err != nil {
return 0, fmt.Errorf("error showing notification: %w", err)
}
n.notifier.notifications_.Lock()
n.notifier.notifications[n.id] = n
n.notifier.notifications_.Unlock()
return n.id, nil
}
func (n *dbusNotify) Signal(sig *dbus.Signal) {
if sig.Path != notifierDbusObjectPath {
return
}
switch sig.Name {
case "org.freedesktop.Notifications.NotificationClosed":
id := sig.Body[0].(uint32)
n.notifications_.Lock()
notification, ok := n.notifications[id]
delete(n.notifications, id)
n.notifications_.Unlock()
n.log("notification closed", "id", id, "reason", sig.Body[1].(uint32))
if !ok {
n.log("notification not found", "id", id)
return
}
for _, onClose := range notification.onClose {
onClose()
}
case "org.freedesktop.Notifications.ActionInvoked":
id := sig.Body[0].(uint32)
n.notifications_.Lock()
notification, ok := n.notifications[id]
n.notifications_.Unlock()
n.log("notification action invoked", "id", sig.Body[0].(uint32), "action", sig.Body[1].(string))
if !ok {
n.log("notification not found", "id", id)
return
}
action := sig.Body[1].(string)
for _, v := range notification.actions {
if v.label == action {
v.callback()
return
}
}
}
}
func (n *dbusNotify) Stop() {
n.log("stopped")
}