Debian Codegraph @

bus/accounts/accounts.go

raw
1 /*
2 Copyright 2013-2015 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 // accounts exposes some properties that're stored in org.freedesktop.Accounts
17 // (specifically, the ones that we need are all under
18 // com.lomiri.touch.AccountsService.Sound).
19 package accounts
20
21 import (
22 "fmt"
23 "os/user"
24 "strings"
25 "sync"
26
27 "github.com/godbus/dbus/v5"
28 "github.com/adrg/xdg"
29
30 "gitlab.com/ubports/development/core/lomiri-push-service/bus"
31 "gitlab.com/ubports/development/core/lomiri-push-service/logger"
32 )
33
34 // accounts lives on a well-known bus.Address.
35 //
36 // Note this one isn't it: the interface is for dbus.properties, and the path
37 // is missing the UID.
38 var BusAddress bus.Address = bus.Address{
39 Interface: "org.freedesktop.DBus.Properties",
40 Path: "/org/freedesktop/Accounts/User",
41 Name: "org.freedesktop.Accounts",
42 }
43
44 const accountsSoundIface = "com.lomiri.touch.AccountsService.Sound"
45
46 type Accounts interface {
47 // Start() sets up the asynchronous updating of properties, and does the first update.
48 Start() error
49 // Cancel() stops the asynchronous updating of properties.
50 Cancel()
51 // SilentMode() tells you whether the device is in silent mode.
52 SilentMode() bool
53 // Vibrate() tells you whether the device is allowed to vibrate.
54 Vibrate() bool
55 // MessageSoundFile() tells you the default sound filename.
56 MessageSoundFile() string
57 String() string
58 }
59
60 // Accounts tracks the relevant bits of configuration. Nothing directly
61 // accessible because it is updated asynchronously, so use the accessors.
62 type accounts struct {
63 endp bus.Endpoint
64 log logger.Logger
65 silent bool
66 vibrate bool
67 vibrateSilentMode bool
68 messageSound string
69 cancellable bus.Cancellable
70 lck sync.Mutex
71 updaters map[string]func(dbus.Variant)
72 }
73
74 // sets up a new Accounts structure, ready to be Start()ed.
75 func New(endp bus.Endpoint, log logger.Logger) Accounts {
76 a := &accounts{
77 endp: endp,
78 log: log,
79 }
80
81 a.updaters = map[string]func(dbus.Variant){
82 "SilentMode": a.updateSilentMode,
83 "IncomingMessageVibrate": a.updateVibrate,
84 "IncomingMessageVibrateSilentMode": a.updateVibrateSilentMode,
85 "IncomingMessageSound": a.updateMessageSound,
86 }
87
88 return a
89 }
90
91 // sets up the asynchronous updating of properties, and does the first update.
92 func (a *accounts) Start() error {
93 err := a.startWatch()
94 if err != nil {
95 return err
96 }
97 a.update()
98 return nil
99 }
100
101 // does sets up the watch on the PropertiesChanged signal. Separate from Start
102 // because it holds a lock.
103 func (a *accounts) startWatch() error {
104 cancellable, err := a.endp.WatchSignal("PropertiesChanged", a.propsHandler, a.bailoutHandler)
105 if err != nil {
106 a.log.Errorf("unable to watch for property changes: %v", err)
107 return err
108 }
109
110 a.lck.Lock()
111 defer a.lck.Unlock()
112 if a.cancellable != nil {
113 panic("tried to start Accounts twice?")
114 }
115 a.cancellable = cancellable
116
117 return nil
118 }
119
120 // cancel the asynchronous updating of properties.
121 func (a *accounts) Cancel() {
122 a.cancellable.Cancel()
123 }
124
125 // slightly shorter than %#v
126 func (a *accounts) String() string {
127 return fmt.Sprintf("&accounts{silent: %t, vibrate: %t, vibratesilent: %t, messageSound: %q}",
128 a.silent, a.vibrate, a.vibrateSilentMode, a.messageSound)
129 }
130
131 // merely log that the watch loop has bailed; not much we can do.
132 func (a *accounts) bailoutHandler() {
133 a.log.Debugf("loop bailed out")
134 }
135
136 // handle PropertiesChanged, which is described in
137 // http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties
138 func (a *accounts) propsHandler(ns ...interface{}) {
139 if len(ns) != 3 {
140 a.log.Errorf("PropertiesChanged delivered %d things instead of 3.", len(ns))
141 return
142 }
143
144 iface, ok := ns[0].(string)
145 if !ok {
146 a.log.Errorf("PropertiesChanged 1st param not a string: %#v.", ns[0])
147 return
148 }
149 if iface != accountsSoundIface {
150 a.log.Debugf("PropertiesChanged for %#v, ignoring.", iface)
151 return
152 }
153 changed, ok := ns[1].(map[interface{}]interface{})
154 if !ok {
155 a.log.Errorf("PropertiesChanged 2nd param not a map: %#v.", ns[1])
156 return
157 }
158 if len(changed) != 0 {
159 // not seen in the wild, but easy to implement properly (ie
160 // using the values we're given) if it starts to
161 // happen. Meanwhile just do a full update.
162 a.log.Infof("PropertiesChanged provided 'changed'; reverting to full update.")
163 a.update()
164 return
165 }
166 invalid, ok := ns[2].([]interface{})
167 if !ok {
168 a.log.Errorf("PropertiesChanged 3rd param not a list of properties: %#v.", ns[2])
169 return
170 }
171 a.log.Debugf("props changed: %#v.", invalid)
172 switch len(invalid) {
173 case 0:
174 // nothing to do?
175 a.log.Debugf("PropertiesChanged 3rd param is empty; doing nothing.")
176 case 1:
177 // the common case right now
178 k, ok := invalid[0].(string)
179 if !ok {
180 a.log.Errorf("PropertiesChanged 3rd param's only entry not a string: %#v.", invalid[0])
181 return
182 }
183 updater, ok := a.updaters[k]
184 if ok {
185 var v dbus.Variant
186 err := a.endp.Call("Get", []interface{}{accountsSoundIface, k}, &v)
187 if err != nil {
188 a.log.Errorf("when calling Get for %s: %v", k, err)
189 return
190 }
191 a.log.Debugf("Get for %s got %#v.", k, v)
192 // updaters must be called with the lock held
193 a.lck.Lock()
194 defer a.lck.Unlock()
195 updater(v)
196 a.log.Debugf("updated %s.", k)
197 }
198 default:
199 // not seen in the wild, but we probably want to drop to a
200 // full update if getting more than one change anyway.
201 a.log.Infof("PropertiesChanged provided more than one 'invalid'; reverting to full update.")
202 a.update()
203 }
204 }
205
206 func (a *accounts) updateSilentMode(vsilent dbus.Variant) {
207 silent, ok := vsilent.Value().(bool)
208 if !ok {
209 a.log.Errorf("SilentMode needed a bool.")
210 return
211 }
212
213 a.silent = silent
214 }
215
216 func (a *accounts) updateVibrate(vvibrate dbus.Variant) {
217 vibrate, ok := vvibrate.Value().(bool)
218 if !ok {
219 a.log.Errorf("IncomingMessageVibrate needed a bool.")
220 return
221 }
222
223 a.vibrate = vibrate
224 }
225
226 func (a *accounts) updateVibrateSilentMode(vvibrateSilentMode dbus.Variant) {
227 vibrateSilentMode, ok := vvibrateSilentMode.Value().(bool)
228 if !ok {
229 a.log.Errorf("IncomingMessageVibrateSilentMode needed a bool.")
230 return
231 }
232
233 a.vibrateSilentMode = vibrateSilentMode
234 }
235
236 func (a *accounts) updateMessageSound(vsnd dbus.Variant) {
237 snd, ok := vsnd.Value().(string)
238 if !ok {
239 a.log.Errorf("IncomingMessageSound needed a string.")
240 return
241 }
242
243 for _, dir := range xdg.DataDirs {
244 if dir[len(dir)-1] != '/' {
245 dir += "/"
246 }
247 if strings.HasPrefix(snd, dir) {
248 snd = snd[len(dir):]
249 break
250 }
251 }
252
253 a.messageSound = snd
254 }
255
256 func (a *accounts) update() {
257 props := make(map[string]dbus.Variant)
258 err := a.endp.Call("GetAll", []interface{}{accountsSoundIface}, &props)
259 if err != nil {
260 a.log.Errorf("when calling GetAll: %v", err)
261 return
262 }
263 a.log.Debugf("GetAll got: %#v", props)
264
265 a.lck.Lock()
266 defer a.lck.Unlock()
267
268 for name, updater := range a.updaters {
269 updater(props[name])
270 }
271 }
272
273 // is the device in silent mode?
274 func (a *accounts) SilentMode() bool {
275 a.lck.Lock()
276 defer a.lck.Unlock()
277
278 return a.silent
279 }
280
281 // should notifications vibrate?
282 func (a *accounts) Vibrate() bool {
283 a.lck.Lock()
284 defer a.lck.Unlock()
285
286 if a.silent {
287 return a.vibrateSilentMode
288 } else {
289 return a.vibrate
290 }
291 }
292
293 // what is the default sound file?
294 func (a *accounts) MessageSoundFile() string {
295 a.lck.Lock()
296 defer a.lck.Unlock()
297
298 return a.messageSound
299 }
300
301 // the BusAddress should actually end with the UID of the user in question;
302 // here we do what's needed to get that.
303 func init() {
304 u, err := user.Current()
305 if err != nil {
306 panic(err)
307 }
308
309 BusAddress.Path += u.Uid
310 }