Debian Codegraph @

bus/notifications/raw.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 // Package notifications wraps a couple of Notifications's DBus API points:
18 // the org.freedesktop.Notifications.Notify call, and listening for the
19 // ActionInvoked signal.
20 package notifications
21
22 // this is the lower-level api
23
24 import (
25 "encoding/json"
26 "errors"
27
28 "github.com/godbus/dbus/v5"
29
30 "gitlab.com/ubports/development/core/lomiri-push-service/bus"
31 "gitlab.com/ubports/development/core/lomiri-push-service/click"
32 "gitlab.com/ubports/development/core/lomiri-push-service/click/cnotificationsettings"
33 "gitlab.com/ubports/development/core/lomiri-push-service/launch_helper"
34 "gitlab.com/ubports/development/core/lomiri-push-service/logger"
35 "gitlab.com/ubports/development/core/lomiri-push-service/sounds"
36 )
37
38 // Notifications lives on a well-knwon bus.Address
39 var BusAddress bus.Address = bus.Address{
40 Interface: "org.freedesktop.Notifications",
41 Path: "/org/freedesktop/Notifications",
42 Name: "org.freedesktop.Notifications",
43 }
44
45 /*****************************************************************
46 * RawNotifications
47 */
48
49 // convenience type for the (uint32, string) ActionInvoked signal data
50 type RawAction struct {
51 App *click.AppId `json:"app,omitempty"`
52 Action string `json:"act,omitempty"`
53 ActionId int `json:"aid,omitempty"`
54 Nid string `json:"nid,omitempty"`
55 RawId uint32 `json:"-"`
56 }
57
58 // a raw notification provides a low-level interface to the f.d.o. dbus
59 // notifications api
60 type RawNotifications struct {
61 bus bus.Endpoint
62 log logger.Logger
63 sound sounds.Sound
64 }
65
66 // Raw returns a new RawNotifications that'll use the provided bus.Endpoint
67 func Raw(endp bus.Endpoint, log logger.Logger, sound sounds.Sound) *RawNotifications {
68 return &RawNotifications{endp, log, sound}
69 }
70
71 /*
72 public methods
73 */
74
75 // Notify fires a notification
76 func (raw *RawNotifications) Notify(
77 app_name string, reuse_id uint32,
78 icon, summary, body string,
79 actions []string, hints map[string]dbus.Variant,
80 timeout int32) (uint32, error) {
81 // that's a long argument list! Take a breather.
82 //
83 if raw.bus == nil {
84 return 0, errors.New("unconfigured (missing bus)")
85 }
86 var res uint32
87 err := raw.bus.Call("Notify", bus.Args(app_name, reuse_id, icon,
88 summary, body, actions, hints, timeout), &res)
89 if err != nil {
90 return 0, err
91 }
92 return res, nil
93 }
94
95 // WatchActions listens for ActionInvoked signals from the notification daemon
96 // and sends them over the channel provided
97 func (raw *RawNotifications) WatchActions() (<-chan *RawAction, error) {
98 ch := make(chan *RawAction)
99 _, err := raw.bus.WatchSignal("ActionInvoked",
100 func(ns ...interface{}) {
101 if len(ns) != 2 {
102 raw.log.Debugf("ActionInvoked delivered %d things instead of 2", len(ns))
103 return
104 }
105 rawId, ok := ns[0].(uint32)
106 if !ok {
107 raw.log.Debugf("ActionInvoked's 1st param not a uint32")
108 return
109 }
110 encodedAction, ok := ns[1].(string)
111 if !ok {
112 raw.log.Debugf("ActionInvoked's 2nd param not a string")
113 return
114 }
115 var action *RawAction
116 err := json.Unmarshal([]byte(encodedAction), &action)
117 if err != nil {
118 raw.log.Debugf("ActionInvoked's 2nd param not a json-encoded RawAction")
119 return
120 }
121 action.RawId = rawId
122 ch <- action
123 }, func() { close(ch) })
124 if err != nil {
125 raw.log.Debugf("failed to set up the watch: %s", err)
126 return nil, err
127 }
128 return ch, nil
129 }
130
131 var canUseBubblesNotify = cnotificationsettings.CanUseBubblesNotify
132
133 // Present displays a given card.
134 //
135 // If card.Actions is empty it's a plain, noninteractive bubble notification.
136 // If card.Actions has 1 action, it's an interactive notification.
137 // If card.Actions has 2 actions, it will show as a snap decision.
138 // If it has more actions, who knows (good luck).
139 func (raw *RawNotifications) Present(app *click.AppId, nid string, notification *launch_helper.Notification) bool {
140 if notification == nil {
141 panic("please check notification is not nil before calling present")
142 }
143
144 if !canUseBubblesNotify(app) {
145 raw.log.Debugf("[%s] bubbles disabled by user for this app.", nid)
146
147 if raw.sound != nil {
148 return raw.sound.Present(app, nid, notification)
149 } else {
150 return false
151 }
152 }
153
154 card := notification.Card
155
156 if card == nil || !card.Popup || card.Summary == "" {
157 raw.log.Debugf("[%s] notification has no popup card: %#v", nid, card)
158 return false
159 }
160
161 hints := make(map[string]dbus.Variant)
162 hints["x-lomiri-secondary-icon"] = dbus.MakeVariant(app.SymbolicIcon())
163
164 if raw.sound != nil {
165 soundFile := raw.sound.GetSound(app, nid, notification)
166 if soundFile != "" {
167 hints["sound-file"] = dbus.MakeVariant(soundFile)
168 raw.log.Debugf("[%s] notification will play sound: %s", nid, soundFile)
169 }
170 }
171
172 appId := app.Original()
173 actions := make([]string, 2*len(card.Actions))
174 for i, action := range card.Actions {
175 act, err := json.Marshal(&RawAction{
176 App: app,
177 Nid: nid,
178 ActionId: i,
179 Action: action,
180 })
181 if err != nil {
182 raw.log.Errorf("[%s] while marshaling %#v to json: %v", nid, action, err)
183 return false
184 }
185 actions[2*i] = string(act)
186 actions[2*i+1] = action
187 }
188 switch len(card.Actions) {
189 case 0:
190 // nothing
191 default:
192 raw.log.Errorf("[%s] don't know what to do with %d actions; ignoring the rest", nid, len(card.Actions))
193 actions = actions[:2]
194 fallthrough
195 case 1:
196 hints["x-lomiri-switch-to-application"] = dbus.MakeVariant("true")
197 }
198
199 raw.log.Debugf("[%s] creating popup (or snap decision) for %s (summary: %s)", nid, app.Base(), card.Summary)
200
201 _, err := raw.Notify(appId, 0, card.Icon, card.Summary, card.Body, actions, hints, 30*1000)
202
203 if err != nil {
204 raw.log.Errorf("[%s] call to Notify failed: %v", nid, err)
205 return false
206 }
207
208 return true
209 }