-
Notifications
You must be signed in to change notification settings - Fork 78
/
desktop_notifications_linux.go
99 lines (79 loc) · 2.37 KB
/
desktop_notifications_linux.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
package gui
import (
"fmt"
"log"
"time"
"github.com/twstrike/coyim/Godeps/_workspace/src/github.com/TheCreeper/go-notify"
"github.com/twstrike/coyim/Godeps/_workspace/src/github.com/godbus/dbus"
"github.com/twstrike/coyim/ui"
)
const notificationFeaturesSupported = notificationStyles | notificationUrgency | notificationExpiry
type desktopNotifications struct {
notifications map[string]uint32
notification notify.Notification
notificationStyle string
notificationUrgent bool
notificationExpires bool
supported bool
}
func (dn *desktopNotifications) hints() map[string]interface{} {
if !dn.supported {
return nil
}
hints := make(map[string]interface{})
hints[notify.HintTransient] = false
hints[notify.HintActionIcons] = "coyim"
hints[notify.HintDesktopEntry] = "coyim.desktop"
hints[notify.HintCategory] = notify.ClassImReceived
if dn.notificationUrgent {
hints[notify.HintUrgency] = notify.UrgencyCritical
} else {
hints[notify.HintUrgency] = notify.UrgencyNormal
}
return hints
}
func newDesktopNotifications() *desktopNotifications {
if _, err := dbus.SessionBus(); err != nil {
log.Printf("Error enabling dbus based notifications! %+v\n", err)
return createDesktopNotifications()
}
dn := createDesktopNotifications()
dn.supported = true
dn.notifications = make(map[string]uint32)
return dn
}
const defaultExpirationMs = 5000
func (dn *desktopNotifications) expiration() int32 {
if dn.notificationExpires {
return defaultExpirationMs
}
return notify.ExpiresNever
}
func (dn *desktopNotifications) show(jid, from, message string) error {
if dn.notificationStyle == "off" || !dn.supported {
return nil
}
notification := notify.Notification{
AppName: "CoyIM",
AppIcon: "coyim",
Timeout: dn.expiration(),
Hints: dn.hints(),
ReplacesID: dn.notifications[jid],
}
from = ui.EscapeAllHTMLTags(string(ui.StripSomeHTML([]byte(from))))
notification.Summary, notification.Body = dn.format(from, message, true)
nid, err := notification.Show()
if err != nil {
return fmt.Errorf("Error showing notification: %v", err)
}
if dn.notificationExpires {
go expireNotification(nid, defaultExpirationMs)
}
dn.notifications[jid] = nid
dn.notification = notification
return nil
}
func expireNotification(id uint32, expiry int) {
time.Sleep(time.Duration(expiry) * time.Millisecond)
notify.CloseNotification(id)
}