forked from TheCreeper/go-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.go
285 lines (243 loc) · 6.92 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Package notify provides an implementation of the Freedesktop Notifications
// Specification using the DBus API.
package notify
import "github.com/godbus/dbus"
// Notification object paths and interfaces.
const (
objectPath = "/org/freedesktop/Notifications"
interfacePath = "org.freedesktop.Notifications"
getCapabilities = "org.freedesktop.Notifications.GetCapabilities"
closeNotification = "org.freedesktop.Notifications.CloseNotification"
notify = "org.freedesktop.Notifications.Notify"
gsInformation = "org.freedesktop.Notifications.GetServerInformation"
)
// Notification expire timeout
const (
ExpiresDefault = -1
ExpiresNever = 0
)
// Notification Categories
const (
ClassDevice = "device"
ClassDeviceAdded = "device.added"
ClassDeviceError = "device.error"
ClassDeviceRemoved = "device.removed"
ClassEmail = "email"
ClassEmailArrived = "email.arrived"
ClassEmailBounced = "email.bounced"
ClassIm = "im"
ClassImError = "im.error"
ClassImReceived = "im.received"
ClassNetwork = "network"
ClassNetworkConnected = "network.connected"
ClassNetworkDisconnected = "network.disconnected"
ClassNetworkError = "network.error"
ClassPresence = "presence"
ClassPresenceOffline = "presence.offline"
ClassPresenceOnline = "presence.online"
ClassTransfer = "transfer"
ClassTransferComplete = "transfer.complete"
ClassTransferError = "transfer.error"
)
// Urgency Levels
const (
UrgencyLow = byte(0)
UrgencyNormal = byte(1)
UrgencyCritical = byte(2)
)
// Hints
const (
HintActionIcons = "action-icons"
HintCategory = "category"
HintDesktopEntry = "desktop-entry"
HintImageData = "image-data"
HintImagePath = "image-path"
HintResident = "resident"
HintSoundFile = "sound-file"
HintSoundName = "sound-name"
HintSuppressSound = "suppress-sound"
HintTransient = "transient"
HintX = "x"
HintY = "y"
HintUrgency = "urgency"
)
// Capabilities is a struct containing the capabilities of the notification
// server.
type Capabilities struct {
// Supports using icons instead of text for displaying actions.
ActionIcons bool
// The server will provide any specified actions to the user.
Actions bool
// Supports body text. Some implementations may only show the summary.
Body bool
// The server supports hyperlinks in the notifications.
BodyHyperlinks bool
// The server supports images in the notifications.
BodyImages bool
// Supports markup in the body text.
BodyMarkup bool
// The server will render an animation of all the frames in a given
// image array.
IconMulti bool
// Supports display of exactly 1 frame of any given image array.
IconStatic bool
// The server supports persistence of notifications. Notifications will
// be retained until they are acknowledged or removed by the user or
// recalled by the sender.
Persistence bool
// The server supports sounds on notifications.
Sound bool
}
// GetCapabilities returns the capabilities of the notification server.
func GetCapabilities() (c Capabilities, err error) {
conn, err := dbus.SessionBus()
if err != nil {
return
}
obj := conn.Object(interfacePath, objectPath)
call := obj.Call(getCapabilities, 0)
if err = call.Err; err != nil {
return
}
s := []string{}
if err = call.Store(&s); err != nil {
return
}
for _, v := range s {
switch v {
case "action-icons":
c.ActionIcons = true
break
case "actions":
c.Actions = true
break
case "body":
c.Body = true
break
case "body-hyperlinks":
c.BodyHyperlinks = true
break
case "body-images":
c.BodyImages = true
break
case "body-markup":
c.BodyMarkup = true
break
case "icon-multi":
c.IconMulti = true
break
case "icon-static":
c.IconStatic = true
break
case "persistence":
c.Persistence = true
break
case "sound":
c.Sound = true
break
}
}
return
}
// ServerInformation is a struct containing information about the server such
// as its name and version.
type ServerInformation struct {
// The name of the notification server daemon
Name string
// The vendor of the notification server
Vendor string
// Version of the notification server
Version string
// Spec version the notification server conforms to
SpecVersion string
}
// GetServerInformation returns information about the notification server such
// as its name and version.
func GetServerInformation() (i ServerInformation, err error) {
conn, err := dbus.SessionBus()
if err != nil {
return
}
obj := conn.Object(interfacePath, objectPath)
call := obj.Call(gsInformation, 0)
if err = call.Err; err != nil {
return
}
err = call.Store(&i.Name, &i.Vendor, &i.Version, &i.SpecVersion)
return
}
// Notification is a struct which describes the notification to be displayed
// by the notification server.
type Notification struct {
// The optional name of the application sending the notification.
// Can be blank.
AppName string
// The optional notification ID that this notification replaces.
ReplacesID uint32
// The optional program icon of the calling application.
AppIcon string
// The summary text briefly describing the notification.
Summary string
// The optional detailed body text.
Body string
// The actions send a request message back to the notification client
// when invoked.
Actions []string
// Hints are a way to provide extra data to a notification server.
Hints map[string]interface{}
// The timeout time in milliseconds since the display of the
// notification at which the notification should automatically close.
Timeout int32
}
// NewNotification creates a new notification object with some basic
// information.
func NewNotification(summary, body string) Notification {
return Notification{
Summary: summary,
Body: body,
Timeout: ExpiresDefault,
}
}
// Show sends the information in the notification object to the server to be
// displayed.
func (n Notification) Show() (id uint32, err error) {
conn, err := dbus.SessionBus()
if err != nil {
return
}
// 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 := map[string]dbus.Variant{}
for k, v := range n.Hints {
hints[k] = dbus.MakeVariant(v)
}
obj := conn.Object(interfacePath, objectPath)
call := obj.Call(
notify,
0,
n.AppName,
n.ReplacesID,
n.AppIcon,
n.Summary,
n.Body,
n.Actions,
hints,
n.Timeout)
if err = call.Err; err != nil {
return
}
err = call.Store(&id)
return
}
// CloseNotification closes the notification if it exists using its id.
func CloseNotification(id uint32) (err error) {
conn, err := dbus.SessionBus()
if err != nil {
return
}
obj := conn.Object(interfacePath, objectPath)
call := obj.Call(closeNotification, 0, id)
err = call.Err
return
}