-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
175 lines (152 loc) · 3.84 KB
/
main.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
package main
import (
"fmt"
"image"
"image/draw"
_ "image/jpeg"
_ "image/png"
"log"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/godbus/dbus/v5"
"github.com/esiqveland/notify"
)
func main() {
err := runMain()
if err != nil {
log.Printf("\nerror: %v\n", err)
os.Exit(1)
}
}
func runMain() error {
wg := &sync.WaitGroup{}
conn, err := dbus.SessionBusPrivate()
if err != nil {
panic(err)
}
defer conn.Close()
if err = conn.Auth(nil); err != nil {
panic(err)
}
if err = conn.Hello(); err != nil {
panic(err)
}
DebugServerFeatures(conn)
// Basic usage
soundHint := notify.HintSoundWithName(
//"message-new-instant",
"trash-empty",
)
// Create a Notification to send
iconName := "mail-unread"
n := notify.Notification{
AppName: "Test GO App",
ReplacesID: uint32(0),
AppIcon: iconName,
Summary: "Test",
Body: "This is a test of the DBus bindings for go with sound.",
Actions: []notify.Action{
{Key: "cancel", Label: "Cancel"},
{Key: "open", Label: "Open"},
},
Hints: map[string]dbus.Variant{
soundHint.ID: soundHint.Variant,
},
ExpireTimeout: time.Second * 5,
}
n.SetUrgency(notify.UrgencyCritical)
counter := int32(0)
// Listen for actions invoked!
onAction := func(action *notify.ActionInvokedSignal) {
atomic.AddInt32(&counter, 1)
log.Printf("ActionInvoked: %v Key: %v", action.ID, action.ActionKey)
wg.Done()
}
onClosed := func(closer *notify.NotificationClosedSignal) {
atomic.AddInt32(&counter, 1)
log.Printf("NotificationClosed: %v Reason: %v", closer.ID, closer.Reason)
wg.Done()
}
// Notifier instance with event delivery:
notifier, err := notify.New(
conn,
// action event handler
notify.WithOnAction(onAction),
// closed event handler
notify.WithOnClosed(onClosed),
// override with custom logger
notify.WithLogger(log.New(os.Stdout, "notify: ", log.Flags())),
)
if err != nil {
return err
}
defer notifier.Close()
rgbaSample, err := readImage("./big.jpg")
if err != nil {
return err
}
absFilePath, err := filepath.Abs("./small.png")
if err != nil {
return err
}
hintProfileImage := notify.HintImageDataRGBA(rgbaSample)
hintImageByPath := notify.HintImageFilePath(absFilePath)
urgencyHint := notify.HintUrgency(notify.UrgencyCritical)
n.AddHint(urgencyHint)
n.AddHint(hintImageByPath)
// according to spec, image-data hint should have precedence:
n.AddHint(hintProfileImage)
wg.Add(1)
id, err := notifier.SendNotification(n)
if err != nil {
log.Printf("error sending notification: %v", err)
}
log.Printf("sent notification id: %v", id)
// add two extra so the process should block forever:
// this is due to a bug in Gnome that delivers multiple copies of the action signal...
wg.Add(2)
wg.Wait()
log.Printf("total signal count received: %d", atomic.LoadInt32(&counter))
return nil
}
func DebugServerFeatures(conn *dbus.Conn) {
// List server features!
caps, err := notify.GetCapabilities(conn)
if err != nil {
log.Printf("error fetching capabilities: %v", err)
}
for x := range caps {
fmt.Printf("Registered capability: %v\n", caps[x])
}
info, err := notify.GetServerInformation(conn)
if err != nil {
log.Printf("error getting server information: %v", err)
}
fmt.Printf("Name: %v\n", info.Name)
fmt.Printf("Vendor: %v\n", info.Vendor)
fmt.Printf("Version: %v\n", info.Version)
fmt.Printf("Spec: %v\n", info.SpecVersion)
}
func readImage(path string) (*image.RGBA, error) {
fd, err := os.Open(path)
if err != nil {
return nil, err
}
defer fd.Close()
decode, _, err := image.Decode(fd)
if err != nil {
return nil, err
}
img, ok := decode.(*image.RGBA)
if !ok {
b := decode.Bounds()
m := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(m, m.Bounds(), decode, b.Min, draw.Src)
return m, nil
//return nil, fmt.Errorf("decode RGBA failed of: %v", path)
}
return img, err
}