-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet_funcs.go
480 lines (376 loc) · 8.67 KB
/
wallet_funcs.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package gokwallet
import (
"github.com/godbus/dbus/v5"
)
/*
NewWallet returns a Wallet. It requires a RecurseOpts
(you can use DefaultRecurseOpts, call NewRecurseOpts, or provide your own RecurseOpts struct).
It also requires a WalletManager and wallet name.
*/
func NewWallet(wm *WalletManager, name string, recursion *RecurseOpts) (wallet *Wallet, err error) {
if !wm.isInit {
err = ErrInitWM
return
}
wallet = &Wallet{
DbusObject: wm.DbusObject,
Name: name,
Folders: nil,
Recurse: recursion,
wm: wm,
// handle: 0,
isInit: false,
}
wallet.isInit = true
// TODO: remove this and leave to caller, since it might use PamOpen instead? Fail back to it?
if err = wallet.walletCheck(); err != nil {
return
}
if wallet.Recurse.All || wallet.Recurse.Folders {
if err = wallet.Update(); err != nil {
return
}
}
wallet.isInit = true
return
}
/*
Disconnect disconnects this Wallet from its parent WalletManager.
You may need to run WalletManager.Update after this to clear the WalletManager cache.
*/
func (w *Wallet) Disconnect() (err error) {
var call *dbus.Call
var ok bool
if err = w.walletCheck(); err != nil {
return
}
if call = w.Dbus.Call(
DbusWMDisconnectApp, 0, w.Name, w.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&ok); err != nil {
return
}
if !ok {
err = ErrNoDisconnect
}
return
}
// DisconnectApplication disconnects this Wallet from a specified WalletManager/application (see Wallet.Connections).
func (w *Wallet) DisconnectApplication(appName string) (err error) {
var call *dbus.Call
var ok bool
if err = w.walletCheck(); err != nil {
return
}
if call = w.Dbus.Call(
DbusWMDisconnectApp, 0, w.Name, appName,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&ok); err != nil {
return
}
if !ok {
err = ErrNoDisconnect
}
return
}
/*
ChangePassword will change (or set) the password for a Wallet.
Note that this *must* be done via the windowing/graphical layer.
There is no way to change a Wallet's password via the Dbus API.
*/
func (w *Wallet) ChangePassword() (err error) {
var call *dbus.Call
if err = w.walletCheck(); err != nil {
return
}
if call = w.Dbus.Call(
DbusWMChangePassword, 0, w.Name, DefaultWindowID, w.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
return
}
// Close closes a Wallet.
func (w *Wallet) Close() (err error) {
var call *dbus.Call
var rslt int32
if err = w.walletCheck(); err != nil {
return
}
// Using a handler allows us to close access for this particular parent WalletManager.
if call = w.Dbus.Call(
DbusWMClose, 0, w.handle, false, w.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&rslt); err != nil {
return
}
err = resultCheck(rslt)
return
}
// Connections lists the application names for connections to ("users of") this Wallet.
func (w *Wallet) Connections() (connList []string, err error) {
var call *dbus.Call
if err = w.walletCheck(); err != nil {
return
}
if call = w.Dbus.Call(
DbusWMUsers, 0, w.Name,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&connList); err != nil {
return
}
return
}
/*
CreateFolder creates a new Folder in a Wallet.
If you need a Folder object, use NewFolder instead.
*/
func (w *Wallet) CreateFolder(name string) (err error) {
var call *dbus.Call
var ok bool
if err = w.walletCheck(); err != nil {
return
}
if call = w.Dbus.Call(
DbusWMCreateFolder, 0, w.handle, name, w.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&ok); err != nil {
return
}
if !ok {
err = ErrNoCreate
}
return
}
// Delete deletes a Wallet.
func (w *Wallet) Delete() (err error) {
var call *dbus.Call
var rslt int32
if err = w.walletCheck(); err != nil {
return
}
if call = w.Dbus.Call(
DbusWMDeleteWallet, 0, w.Name,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&rslt); err != nil {
return
}
err = resultCheck(rslt)
w = nil
return
}
/*
FolderExists indicates if a Folder exists in a Wallet or not.
Similar to Wallet.HasFolder but does not need the Wallet to be opened/unlocked.
*/
func (w *Wallet) FolderExists(folderName string) (exists bool, err error) {
var call *dbus.Call
var notExists bool
// We don't need a walletcheck here since we don't need a handle.
if call = w.Dbus.Call(
DbusWMFolderNotExist, 0, w.Name, folderName,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(¬Exists); err != nil {
return
}
exists = !notExists
return
}
/*
ForceClose is like Close but will still close a Wallet even if currently in use by the WalletManager.
(Despite the insinuation by the name, this should be a relatively safe operation).
*/
func (w *Wallet) ForceClose() (err error) {
var call *dbus.Call
var rslt int32
if err = w.walletCheck(); err != nil {
return
}
// Using a handler allows us to close access for this particular parent WalletManager.
if call = w.Dbus.Call(
DbusWMClose, 0, w.handle, true, w.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&rslt); err != nil {
return
}
err = resultCheck(rslt)
return
}
// HasFolder indicates if a Wallet has a Folder in it named folderName.
func (w *Wallet) HasFolder(folderName string) (hasFolder bool, err error) {
var call *dbus.Call
if err = w.walletCheck(); err != nil {
return
}
if call = w.Dbus.Call(
DbusWMHasFolder, 0, w.handle, folderName, w.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&hasFolder); err != nil {
return
}
return
}
// IsOpen returns whether a Wallet is open ("unlocked") or not (as well as updates Wallet.IsOpen).
func (w *Wallet) IsOpen() (isOpen bool, err error) {
var call *dbus.Call
// We don't call walletcheck here because this method is called by a walletcheck.
if !w.isInit {
err = ErrInitWallet
return
}
// We can call the same method with w.handle instead of w.Name. We don't have a handler yet though.
if call = w.Dbus.Call(
DbusWMIsOpen, 0, w.Name,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&w.IsUnlocked); err != nil {
return
}
isOpen = w.IsUnlocked
return
}
// ListFolders lists all Folder names in a Wallet.
func (w *Wallet) ListFolders() (folderList []string, err error) {
var call *dbus.Call
if err = w.walletCheck(); err != nil {
return
}
if call = w.Dbus.Call(
DbusWMFolderList, 0, w.handle, w.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&folderList); err != nil {
return
}
return
}
/*
Open will open ("unlock") a Wallet.
It will no-op if the Wallet is already open.
*/
func (w *Wallet) Open() (err error) {
var call *dbus.Call
var handler *int32 = new(int32)
// We don't call walletcheck here because this method is called by a walletcheck.
if !w.isInit {
err = ErrInitWallet
return
}
if !w.IsUnlocked || !w.hasHandle {
if call = w.Dbus.Call(
DbusWMOpen, 0, w.Name, DefaultWindowID, w.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(handler); err != nil {
return
}
}
if handler == nil {
err = ErrDbusOpfailNoHandle
return
} else {
w.handle = *handler
}
w.hasHandle = true
w.IsUnlocked = true
return
}
/*
RemoveFolder removes a Folder folderName from a Wallet.
Note that this will also remove all WalletItems in the given Folder.
*/
func (w *Wallet) RemoveFolder(folderName string) (err error) {
var call *dbus.Call
var success bool
if err = w.walletCheck(); err != nil {
return
}
if call = w.Dbus.Call(
DbusWMRemoveFolder, 0, w.handle, folderName, w.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&success); err != nil {
return
}
if !success {
err = ErrDbusOpfailRemoveFolder
return
}
return
}
// Update fetches/updates all Folder objects in a Wallet.
func (w *Wallet) Update() (err error) {
var folderNames []string
var errs []error = make([]error, 0)
if err = w.walletCheck(); err != nil {
return
}
if folderNames, err = w.ListFolders(); err != nil {
return
}
w.Folders = make(map[string]*Folder)
for _, fn := range folderNames {
if w.Folders[fn], err = NewFolder(w, fn, w.Recurse); err != nil {
errs = append(errs, err)
err = nil
continue
}
}
if errs != nil && len(errs) > 0 {
err = NewErrors(errs...)
return
}
return
}
// walletCheck will check if a Wallet is (initialized and) opened and, if not, attempt to open it.
func (w *Wallet) walletCheck() (err error) {
if !w.isInit {
err = ErrInitWallet
return
}
if _, err = w.IsOpen(); err != nil {
return
}
if !w.IsUnlocked || !w.hasHandle {
if err = w.Open(); err != nil {
return
}
}
return
}