aboutsummaryrefslogtreecommitdiffstats
path: root/internal/notify/policy.go
blob: c1dc647db560044d5ae25421826589f7a3e5f5bb (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
87
88
89
90
91
92
93
94
95
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

package notify

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

// Urgency is the freedesktop urgency level.
type Urgency string

const (
	Low      Urgency = "low"
	Normal   Urgency = "normal"
	Critical Urgency = "critical"
)

// DefaultTimeoutMS is the balloon lifetime for an urgency when the client sends
// expire_timeout 0. Critical never expires, which is 0 here.
func DefaultTimeoutMS(u Urgency) int64 {
	switch u {
	case Low, Normal:
		return 10_000
	default:
		return 0
	}
}

// EffectiveTimeoutMS resolves the client's expire_timeout: 0 means the urgency
// default, -1 means never, anything positive is milliseconds and wins.
func EffectiveTimeoutMS(expire int32, u Urgency) int64 {
	switch {
	case expire == -1:
		return 0
	case expire == 0:
		return DefaultTimeoutMS(u)
	default:
		return int64(expire)
	}
}

// UrgencyFromHints reads the urgency byte: 0 low, 1 normal, 2 critical. A
// missing or malformed value is normal, the same default libnotify uses.
func UrgencyFromHints(hints map[string]dbus.Variant) Urgency {
	v, ok := hints["urgency"]
	if !ok {
		return Normal
	}
	switch n := v.Value().(type) {
	case uint8:
		switch n {
		case 0:
			return Low
		case 2:
			return Critical
		}
	case int32:
		switch n {
		case 0:
			return Low
		case 2:
			return Critical
		}
	}
	return Normal
}

// StackTagFromHints reads the dunst stack tag, then the danix spelling. Both
// mean the same thing and dunst wins when a client sends both.
func StackTagFromHints(hints map[string]dbus.Variant) string {
	for _, key := range []string{"x-dunst-stack-tag", "x-danix-stack-tag"} {
		if v, ok := hints[key]; ok {
			if s, ok := v.Value().(string); ok && s != "" {
				return s
			}
		}
	}
	return ""
}

// ParseActions turns the spec's flat [key, label, key, label] array into pairs.
func ParseActions(flat []string) [][2]string {
	out := make([][2]string, 0, len(flat)/2)
	for i := 0; i+1 < len(flat); i += 2 {
		out = append(out, [2]string{flat[i], flat[i+1]})
	}
	return out
}