-
Notifications
You must be signed in to change notification settings - Fork 4
/
daemon.go
89 lines (76 loc) · 1.79 KB
/
daemon.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
// main package
package main
import (
"fmt"
"log"
"time"
"github.com/godbus/dbus/v5"
)
func notify(input string) {
log.Printf("Update finished: %s", input)
// Customize message based on success state
message := "Updates successfully installed"
submessage := "System has been upgraded, on " +
time.Now().Format(time.RFC1123) +
" please reboot to take effect."
icon := "appointment-soon"
if input == "failure" {
message = "Update process failed"
submessage = "An error was encountered while upgrading on " +
time.Now().Format(time.RFC1123)
icon = "appointment-missed"
}
conn, err := dbus.ConnectSessionBus()
if err != nil {
panic(err)
}
defer conn.Close()
obj := conn.Object(
"org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
)
// Set hints of the notification, in this case we set it to
// an urgent notification, so we're sure that it will stick
// in the tray, and the user is notified.
hints := map[string]dbus.Variant{
"urgency": dbus.MakeVariant(byte(1)),
"category": dbus.MakeVariant("device"),
}
call := obj.Call(
"org.freedesktop.Notifications.Notify",
0,
"",
uint32(0),
icon,
message,
submessage,
[]string{},
hints,
int32(-1),
)
if call.Err != nil {
panic(call.Err)
}
}
// NotifyDaemon will wait for a message on org.opensuse.tukit.Updated and trigger
// a graphical notification accordingly.
func NotifyDaemon() {
conn, err := dbus.SystemBus()
if err != nil {
panic(err)
}
if err = conn.AddMatchSignal(
dbus.WithMatchSender(Iface),
dbus.WithMatchObjectPath(dbus.ObjectPath(FullPath)),
dbus.WithMatchInterface(Iface),
dbus.WithMatchMember(Member),
); err != nil {
panic(err)
}
c := make(chan *dbus.Signal, 10)
conn.Signal(c)
for v := range c {
body := fmt.Sprintf("%s", v.Body...)
notify(body)
}
}