-
Notifications
You must be signed in to change notification settings - Fork 1
/
notify_linux.go
106 lines (86 loc) · 2.01 KB
/
notify_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
100
101
102
103
104
105
106
package main
import (
"html"
"log"
"path"
notify "github.com/esiqveland/notify"
"github.com/godbus/dbus"
"github.com/jangxx/go-poclient"
"github.com/skratchdot/open-golang/open"
)
type NotificationActions struct {
url string
}
var notifier notify.Notifier
var notifications map[uint32]NotificationActions
func initNotifications() {
notifications = make(map[uint32]NotificationActions)
conn, err := dbus.SessionBus()
if err != nil {
log.Panicln("Error while connecting to session dbus for notifications")
}
notifier, err = notify.New(conn)
if err != nil {
log.Fatalln(err.Error())
}
actions := notifier.ActionInvoked()
go func() {
for {
action := <-actions
switch action.ActionKey {
case "default":
//do nothing, notification was closed
case "openurl":
open.Run(notifications[action.ID].url)
}
delete(notifications, action.ID)
}
}()
}
func sendNotification(message poclient.Message) {
title := message.AppName
if message.Title != "" {
title = message.Title
}
body := message.Text
actions := []string{}
actionData := NotificationActions{}
if message.URL != "" {
label := "Open URL"
if message.URLTitle != "" {
label = message.URLTitle
}
actions = append(actions, "openurl", label)
actionData.url = message.URL
}
n := notify.Notification{
AppName: "SchubsiGo",
ReplacesID: 0,
Summary: title,
Body: html.EscapeString(body),
Actions: actions,
Hints: map[string]dbus.Variant{},
ExpireTimeout: int32(5000),
}
if config.cache.Exists(message.IconID + ".png") {
iconPath := path.Join(config.cache.Path, message.IconID+".png")
n.AppIcon = iconPath
}
id, err := notifier.SendNotification(n)
if err != nil {
log.Println(err.Error())
} else {
notifications[id] = actionData
}
}
func sendStatusNotification(status string) {
n := notify.Notification{
AppName: "SchubsiGo",
ReplacesID: 0,
Body: status,
}
_, err := notifier.SendNotification(n)
if err != nil {
log.Println(err.Error())
}
}