aboutsummaryrefslogtreecommitdiff
path: root/srv/server.go
diff options
context:
space:
mode:
Diffstat (limited to 'srv/server.go')
-rw-r--r--srv/server.go88
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: &notifications,
+ 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: &notifications,
- 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 {