diff options
| author | Caroline Larimore <caroline@larimo.re> | 2025-01-30 22:26:55 -0800 |
|---|---|---|
| committer | Caroline Larimore <caroline@larimo.re> | 2025-01-30 22:26:55 -0800 |
| commit | 8fcb05ee54e250ae23113549e09fc11410829272 (patch) | |
| tree | 273c26a838973fdb448134be8ef34b791722338a /srv/server.go | |
| parent | d9ddbfa1b7e697884f2be1ea9f11f8ecb5a2027d (diff) | |
Add dismissal commands
Diffstat (limited to 'srv/server.go')
| -rw-r--r-- | srv/server.go | 88 |
1 files changed, 76 insertions, 12 deletions
diff --git a/srv/server.go b/srv/server.go index 673081b..27ced3f 100644 --- a/srv/server.go +++ b/srv/server.go @@ -1,8 +1,11 @@ package srv import ( + "encoding/json" "fmt" "log" + "os" + "slices" "sync" "github.com/godbus/dbus/v5" @@ -11,10 +14,70 @@ import ( const DEFAULT_EXPIRATION = 5000 const SORT_DIRECTION = 1 // 1 = newest first, -1 = oldest first -type server = struct { - conn *dbus.Conn - object dbus.ObjectPath - name string +type server struct { + notifications *notificationStack + conn *dbus.Conn + object dbus.ObjectPath + name string +} + +func (s server) close(id uint32, reason closeReason) { + s.notifications.mutex.Lock() + defer s.notifications.mutex.Unlock() + + n, ok := s.notifications.notifications[id] + if !ok { + return + } + + if n.timer != nil { + n.timer.Stop() + } + + if n.Image != "" { + os.Remove(n.Image) + } + + delete(s.notifications.notifications, n.Id) + + err := s.conn.Emit(s.object, s.name+".NotificationClosed", n.Id, reason) + if err != nil { + log.Print(err) + } +} + +// TODO: relocate to cmd/corvid +func (s server) output() { + arr := make([]notification, len(s.notifications.notifications)) + + i := 0 + for _, notification := range s.notifications.notifications { + arr[i] = notification + i++ + } + + slices.SortFunc(arr, func(a, b notification) int { + if a.Timestamp > b.Timestamp { + return SORT_DIRECTION + } else if a.Timestamp < b.Timestamp { + return -SORT_DIRECTION + } else { + if a.Id > b.Id { + return SORT_DIRECTION + } else if a.Id < b.Id { + return -SORT_DIRECTION + } + } + + return 0 + }) + + j, err := json.Marshal(arr) + if err != nil { + log.Fatalln(err) + } + + fmt.Println(string(j)) } func Start() { @@ -37,9 +100,10 @@ func Start() { err = startDBusServer( conn, corvidServer{ - conn: conn, - object: CORVID_DBUS_OBJECT, - name: CORVID_DBUS_NAME, + notifications: ¬ifications, + conn: conn, + object: CORVID_DBUS_OBJECT, + name: CORVID_DBUS_NAME, }, CORVID_DBUS_OBJECT, CORVID_DBUS_NAME, @@ -52,11 +116,9 @@ func Start() { conn, notifServer{ notifications: ¬ifications, - server: server{ - conn: conn, - object: NOTIF_DBUS_OBJECT, - name: NOTIF_DBUS_NAME, - }, + conn: conn, + object: NOTIF_DBUS_OBJECT, + name: NOTIF_DBUS_NAME, }, NOTIF_DBUS_OBJECT, NOTIF_DBUS_NAME, @@ -64,6 +126,8 @@ func Start() { if err != nil { log.Fatal(err) } + + fmt.Println("[]") } func startDBusServer(conn *dbus.Conn, v interface{}, object dbus.ObjectPath, name string) error { |