~whereswaldon/gio-x

ref: 25b1f7920092ac9d03657ab3cfbf6c9460b075e1 gio-x/notify/notify_dbus.go -rw-r--r-- 1.1 KiB
25b1f792Chris Waldon notify: remove dangling golang references to niotify 4 months ago
                                                                                
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
//+build linux,!android openbsd freebsd netbsd

package notify

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) {
	id, err := l.Notifier.SendNotification(notify.Notification{
		Summary: title,
		Body:    text,
	})
	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
}