forked from muesli/beehive
/
notificationbee.go
92 lines (78 loc) · 2.33 KB
/
notificationbee.go
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
// +build dragonfly freebsd linux netbsd openbsd solaris darwin
/*
* Copyright (C) 2014 Daniel 'grindhold' Brendle
* 2014-2017 Christian Muehlhaeuser
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Daniel 'grindhold' Brendle <grindhold@skarphed.org>
* Christian Muehlhaeuser <muesli@gmail.com>
*/
// Package notificationbee is a Bee that can trigger desktop notifications.
package notificationbee
import (
"strings"
"github.com/muesli/beehive/bees"
)
// Urgency level iota
const (
UrgencyLow = uint32(iota)
UrgencyNormal
UrgencyCritical
)
var (
urgencyMap = map[string]uint32{
"": UrgencyNormal,
"normal": UrgencyNormal,
"low": UrgencyLow,
"critical": UrgencyCritical,
}
)
// NotificationBee is a Bee that can trigger freedesktop.org DBus
// notifications.
type NotificationBee struct {
bees.Bee
}
// Run executes the Bee's event loop.
func (mod *NotificationBee) Run(cin chan bees.Event) {
select {
case <-mod.SigChan:
return
}
}
// Action triggers the action passed to it.
func (mod *NotificationBee) Action(action bees.Action) []bees.Placeholder {
outs := []bees.Placeholder{}
switch action.Name {
case "notify":
text := ""
u := ""
urgency := UrgencyNormal
action.Options.Bind("text", &text)
action.Options.Bind("urgency", &u)
text = strings.TrimSpace(text)
urgency, _ = urgencyMap[u]
if len(text) > 0 {
mod.execAction(text, urgency)
}
default:
panic("Unknown action triggered in " + mod.Name() + ": " + action.Name)
}
return outs
}
// ReloadOptions parses the config options and initializes the Bee.
func (mod *NotificationBee) ReloadOptions(options bees.BeeOptions) {
mod.SetOptions(options)
}