2020-06-20 16:00:04 -04:00
|
|
|
package niotify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/esiqveland/notify"
|
|
|
|
dbus "github.com/godbus/dbus/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
type linuxManager struct {
|
|
|
|
notify.Notifier
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ managerInterface = &linuxManager{}
|
|
|
|
|
|
|
|
func newManager() (Manager, error) {
|
|
|
|
conn, err := dbus.SessionBus()
|
|
|
|
if err != nil {
|
|
|
|
return Manager{}, fmt.Errorf("failed connecting to dbus: %w", err)
|
|
|
|
}
|
|
|
|
notifier, err := notify.New(conn)
|
|
|
|
if err != nil {
|
|
|
|
return Manager{}, fmt.Errorf("failed creating notifier: %w", err)
|
|
|
|
}
|
|
|
|
return Manager{
|
|
|
|
impl: &linuxManager{
|
|
|
|
Notifier: notifier,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type linuxNotification struct {
|
|
|
|
id uint32
|
|
|
|
*linuxManager
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ notificationInterface = linuxNotification{}
|
|
|
|
|
|
|
|
func (l *linuxManager) CreateNotification(title, text string) (*Notification, error) {
|
2020-06-20 16:04:00 -04:00
|
|
|
id, err := l.Notifier.SendNotification(notify.Notification{
|
|
|
|
Summary: title,
|
|
|
|
Body: text,
|
|
|
|
})
|
2020-06-20 16:00:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Notification{
|
|
|
|
linuxNotification{
|
|
|
|
id: id,
|
|
|
|
linuxManager: l,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l linuxNotification) Cancel() error {
|
|
|
|
_, err := l.linuxManager.CloseNotification(l.id)
|
|
|
|
return err
|
|
|
|
}
|