2021-12-20 04:02:48 -05:00
|
|
|
package gokwallet
|
|
|
|
|
2021-12-22 03:20:08 -05:00
|
|
|
import (
|
|
|
|
"github.com/godbus/dbus/v5"
|
|
|
|
)
|
|
|
|
|
2021-12-20 04:02:48 -05:00
|
|
|
/*
|
|
|
|
NewBlob returns a Blob. It requires a RecurseOpts
|
|
|
|
(you can use DefaultRecurseOpts, call NewRecurseOpts, or provide your own RecurseOpts struct).
|
|
|
|
It also requires a Folder.
|
|
|
|
*/
|
|
|
|
func NewBlob(f *Folder, keyName string, recursion *RecurseOpts) (blob *Blob, err error) {
|
|
|
|
|
|
|
|
if !f.isInit {
|
2021-12-25 02:54:18 -05:00
|
|
|
err = ErrInitFolder
|
2021-12-20 04:02:48 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
blob = &Blob{
|
|
|
|
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
|
|
|
blob.isInit = true
|
2021-12-20 04:02:48 -05:00
|
|
|
|
|
|
|
if blob.Recurse.AllWalletItems || blob.Recurse.Blobs {
|
|
|
|
if err = blob.Update(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blob.isInit = true
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-23 04:21:21 -05:00
|
|
|
// Delete will delete this Blob from its parent Folder. You may want to run Folder.UpdateBlobs to update the existing map of Blob items.
|
|
|
|
func (b *Blob) Delete() (err error) {
|
|
|
|
|
|
|
|
if err = b.folder.RemoveEntry(b.Name); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b = nil
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-28 02:50:54 -05:00
|
|
|
// Exists returns true if this Blob actually exists.
|
|
|
|
func (b *Blob) Exists() (exists bool, err error) {
|
|
|
|
|
|
|
|
if exists, err = b.folder.HasEntry(b.Name); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename renames this Blob (changes its key).
|
|
|
|
func (b *Blob) Rename(newName string) (err error) {
|
|
|
|
|
|
|
|
if err = b.folder.RenameEntry(b.Name, newName); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Name = newName
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-23 04:21:21 -05:00
|
|
|
// SetValue will replace this Blob's Blob.Value.
|
|
|
|
func (b *Blob) SetValue(newValue []byte) (err error) {
|
|
|
|
|
|
|
|
if _, err = b.folder.WriteBlob(b.Name, newValue); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Value = newValue
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-20 04:02:48 -05:00
|
|
|
// Update fetches a Blob's Blob.Value.
|
|
|
|
func (b *Blob) Update() (err error) {
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
var call *dbus.Call
|
2021-12-22 03:20:08 -05:00
|
|
|
var v dbus.Variant
|
|
|
|
|
2021-12-28 02:50:54 -05:00
|
|
|
if err = b.folder.wallet.walletCheck(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-25 02:54:18 -05:00
|
|
|
if call = b.Dbus.Call(
|
2021-12-22 03:20:08 -05:00
|
|
|
DbusWMReadEntry, 0, b.folder.wallet.handle, b.folder.Name, b.Name, b.folder.wallet.wm.AppID,
|
2021-12-25 02:54:18 -05:00
|
|
|
); call.Err != nil {
|
|
|
|
err = call.Err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = call.Store(&v); err != nil {
|
2021-12-22 03:20:08 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Value = v.Value().([]byte)
|
2021-12-20 04:02:48 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// isWalletItem is needed for interface membership.
|
|
|
|
func (b *Blob) isWalletItem() (isWalletItem bool) {
|
|
|
|
|
|
|
|
isWalletItem = true
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|