2021-12-20 04:02:48 -05:00
|
|
|
package gokwallet
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
import (
|
|
|
|
"github.com/godbus/dbus/v5"
|
|
|
|
)
|
|
|
|
|
2021-12-20 04:02:48 -05:00
|
|
|
/*
|
|
|
|
NewMap returns a Map. It requires a RecurseOpts
|
|
|
|
(you can use DefaultRecurseOpts, call NewRecurseOpts, or provide your own RecurseOpts struct).
|
|
|
|
It also requires a Folder.
|
|
|
|
*/
|
|
|
|
func NewMap(f *Folder, keyName string, recursion *RecurseOpts) (m *Map, err error) {
|
|
|
|
|
|
|
|
if !f.isInit {
|
2021-12-25 02:54:18 -05:00
|
|
|
err = ErrInitFolder
|
2021-12-20 04:02:48 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m = &Map{
|
|
|
|
DbusObject: f.DbusObject,
|
|
|
|
Name: keyName,
|
|
|
|
// Value: "",
|
|
|
|
Recurse: recursion,
|
|
|
|
wm: f.wallet.wm,
|
|
|
|
wallet: f.wallet,
|
|
|
|
folder: f,
|
|
|
|
isInit: false,
|
|
|
|
}
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
m.isInit = true
|
|
|
|
|
2021-12-20 04:02:48 -05:00
|
|
|
if m.Recurse.AllWalletItems || m.Recurse.Maps {
|
|
|
|
if err = m.Update(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.isInit = true
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-23 04:21:21 -05:00
|
|
|
// Delete will delete this Map from its parent Folder. You may want to run Folder.UpdateMaps to update the existing map of Map items.
|
|
|
|
func (m *Map) Delete() (err error) {
|
|
|
|
|
|
|
|
if err = m.folder.RemoveEntry(m.Name); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m = nil
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetValue will replace this Map's Map.Value.
|
|
|
|
func (m *Map) SetValue(newValue map[string]string) (err error) {
|
|
|
|
|
|
|
|
if _, err = m.folder.WriteMap(m.Name, newValue); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Value = newValue
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-20 04:02:48 -05:00
|
|
|
// Update fetches a Map's Map.Value.
|
|
|
|
func (m *Map) Update() (err error) {
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
var call *dbus.Call
|
2021-12-22 03:20:08 -05:00
|
|
|
var b []byte
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
if call = m.Dbus.Call(
|
2021-12-22 03:20:08 -05:00
|
|
|
DbusWMReadMap, 0, m.folder.wallet.handle, m.folder.Name, m.Name, m.folder.wallet.wm.AppID,
|
2021-12-25 02:54:18 -05:00
|
|
|
); call.Err != nil {
|
|
|
|
err = call.Err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = call.Store(&b); err != nil {
|
2021-12-22 03:20:08 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.Value, _, err = bytesToMap(b); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-12-20 04:02:48 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// isWalletItem is needed for interface membership.
|
|
|
|
func (m *Map) isWalletItem() (isWalletItem bool) {
|
|
|
|
|
|
|
|
isWalletItem = true
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|