alerttonotify/bus/bus.go

77 lines
1.8 KiB
Go
Raw Normal View History

2024-12-02 23:47:40 +00:00
package bus
import (
"time"
"github.com/godbus/dbus/v5"
)
type NotifyBus struct {
conn *dbus.Conn
}
type BusNotification struct {
ID uint32
Summary string
Body string
Timeout time.Duration
}
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{}
call.Store(&srvInfo.Name, &srvInfo.Vendor, &srvInfo.Version, &srvInfo.SpecVersion)
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
"alerttonotify", // 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
}
call.Store(&ret)
return ret, nil
}