-
Notifications
You must be signed in to change notification settings - Fork 3
/
notify.go
118 lines (98 loc) · 2.9 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
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
"github.com/adrg/xdg"
"github.com/esiqveland/notify"
"github.com/godbus/dbus"
"golang.org/x/xerrors"
)
var defaultNotificationClient = ¬ificationClient{
store: ¬ificationStoreClient{},
}
type notificationClient struct {
store notificationStore
}
func (c *notificationClient) showVolumeNotification(volume int, mute bool) error {
conn, err := dbus.SessionBus()
if err != nil {
return xerrors.Errorf("error connecting to DBus: %w", err)
}
n := notify.Notification{
AppName: "volumectl",
AppIcon: c.notificationVolumeIcon(volume, mute),
ReplacesID: c.store.LastNoficationID(),
ExpireTimeout: int32(3000),
Hints: map[string]dbus.Variant{
"value": dbus.MakeVariant(volume),
"synchronous": dbus.MakeVariant("volume"),
},
}
notificationID, err := notify.SendNotification(conn, n)
if err != nil {
return xerrors.Errorf("error sending notification: %w", err)
}
return c.store.WriteNotificationID(notificationID)
}
func (c *notificationClient) notificationVolumeIcon(volume int, mute bool) string {
iconName := "notification-audio-volume-medium"
if mute {
iconName = "notification-audio-volume-muted"
} else if volume == 0 {
iconName = "notification-audio-volume-off"
} else if volume > 70 {
iconName = "notification-audio-volume-high"
} else if volume < 30 {
iconName = "notification-audio-volume-low"
}
return iconName
}
func showVolumeNotification(volume int, mute bool) error {
return defaultNotificationClient.showVolumeNotification(volume, mute)
}
type notificationStore interface {
LastNoficationID() uint32
WriteNotificationID(uint32) error
}
type notificationStoreClient struct{}
var _ notificationStore = ¬ificationStoreClient{}
func (s *notificationStoreClient) LastNoficationID() uint32 {
storeFilePath, err := xdg.SearchRuntimeFile(s.storeFilePath())
if err != nil {
return 0
}
storeFileContent, err := ioutil.ReadFile(storeFilePath)
if err != nil {
return 0
}
lastNotificationID := strings.TrimSpace(string(storeFileContent))
notificationID, _ := strconv.ParseUint(lastNotificationID, 10, 32)
return uint32(notificationID)
}
func (s *notificationStoreClient) WriteNotificationID(id uint32) error {
if err := ensureDirExists(path.Join(xdg.RuntimeDir, "volumectl")); err != nil {
return xerrors.Errorf("unable to create XDG runtime directory: %w", err)
}
storeFilePath := path.Join(xdg.RuntimeDir, s.storeFilePath())
return ioutil.WriteFile(storeFilePath, []byte(fmt.Sprintf("%d", id)), 0600)
}
func (s *notificationStoreClient) storeFilePath() string {
return path.Join("volumectl", "last_notification_id")
}
func ensureDirExists(dir string) error {
fi, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
return os.Mkdir(dir, os.ModeDir|0700)
}
return err
}
if !fi.IsDir() {
return xerrors.Errorf("%d is not a directory", dir)
}
return nil
}