81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
package bus
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/godbus/dbus/v5"
|
|
)
|
|
|
|
type NotifyBus struct {
|
|
conn *dbus.Conn
|
|
}
|
|
|
|
type BusNotification struct {
|
|
ID uint32 `json:"id,omitempty"`
|
|
Summary string `json:"summary"`
|
|
Body string `json:"body,omitempty"`
|
|
Timeout time.Duration `json:"timeout,omitempty"`
|
|
}
|
|
|
|
type NotifyServerInfo struct {
|
|
Name string
|
|
Vendor string
|
|
Version string
|
|
SpecVersion string
|
|
}
|
|
|
|
func NewNotifyBus() (*NotifyBus, error) {
|
|
conn, err := dbus.ConnectSessionBus()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &NotifyBus{conn: conn}, nil
|
|
}
|
|
|
|
func (n *NotifyBus) Close() {
|
|
n.conn.Close()
|
|
}
|
|
|
|
func (n *NotifyBus) ServerInfo() (*NotifyServerInfo, error) {
|
|
obj := n.conn.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
|
|
call := obj.Call(
|
|
"org.freedesktop.Notifications.GetServerInformation", // Method
|
|
0, // Flags
|
|
)
|
|
if call.Err != nil {
|
|
return nil, call.Err
|
|
}
|
|
|
|
srvInfo := &NotifyServerInfo{}
|
|
if err := call.Store(&srvInfo.Name, &srvInfo.Vendor, &srvInfo.Version, &srvInfo.SpecVersion); err != nil {
|
|
return nil, err
|
|
}
|
|
return srvInfo, nil
|
|
}
|
|
|
|
func (n *NotifyBus) Notify(notification BusNotification) (uint32, error) {
|
|
obj := n.conn.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
|
|
var ret uint32
|
|
call := obj.Call(
|
|
"org.freedesktop.Notifications.Notify", // Method
|
|
0, // Flags
|
|
"natstonotify", // App name
|
|
notification.ID, // Notification ID
|
|
"", // Icon
|
|
notification.Summary, // Summary
|
|
notification.Body, // Body
|
|
[]string{}, // Actions
|
|
map[string]dbus.Variant{}, // Hints
|
|
int32(notification.Timeout.Milliseconds()), // Timeout
|
|
)
|
|
if call.Err != nil {
|
|
return ret, call.Err
|
|
}
|
|
|
|
if err := call.Store(&ret); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return ret, nil
|
|
}
|