aboutsummaryrefslogtreecommitdiff
path: root/notification.go
blob: 2d5625df826f8888680b071f0d5f73ccc46bb3c4 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main

import (
	"encoding/json"
	"log"
	"os"
	"time"

	"github.com/godbus/dbus/v5"
)

type hint struct {
	dbus.Variant
}

func (h hint) MarshalJSON() ([]byte, error) {
	//TODO: find a better way lol
	switch h.Signature().String()[0] {
	case 'y':
		return json.Marshal(h.Value().(uint8))
	case 'b':
		return json.Marshal(h.Value().(bool))
	case 'n':
		return json.Marshal(h.Value().(int16))
	case 'q':
		return json.Marshal(h.Value().(uint16))
	case 'i':
		return json.Marshal(h.Value().(int32))
	case 'u':
		return json.Marshal(h.Value().(uint32))
	case 'x':
		return json.Marshal(h.Value().(int64))
	case 't':
		return json.Marshal(h.Value().(uint64))
	case 'd':
		return json.Marshal(h.Value().(float64))
	case 's':
		return json.Marshal(h.Value().(string))
	default:
		panic("Impossible type")
	}
}

type closeReason uint32

const (
	CloseReasonExpire    closeReason = 1
	CloseReasonDismissed             = iota
	CloseReasonClosed                = iota
	CloseReasonOther                 = iota
)

type notification struct {
	Id         uint32            `json:"id"`
	AppName    string            `json:"app_name"`
	AppIcon    string            `json:"app_icon"`
	Summary    string            `json:"summary"`
	Body       string            `json:"body"`
	Actions    map[string]string `json:"actions"`
	Hints      map[string]hint   `json:"hints"`
	Timestamp  int64             `json:"timestamp"`
	Expiration int32             `json:"expiration"`
	Image      string            `json:"image"`
	timer      *time.Timer
}

func (n notification) close(reason closeReason) {
	notifications.mutex.Lock()
	defer notifications.mutex.Unlock()

	if n.timer != nil {
		n.timer.Stop()
	}

	if n.Image != "" {
		os.Remove(n.Image)
	}

	delete(notifications.notifications, n.Id)
	output()

	err := conn.Emit(DBUS_OBJECT, DBUS_NAME+".NotificationClosed", n.Id, reason)
	if err != nil {
		log.Print(err)
	}
}