gokwallet/blob_funcs.go
2021-12-28 02:50:54 -05:00

120 lines
2.1 KiB
Go

package gokwallet
import (
"github.com/godbus/dbus/v5"
)
/*
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 {
err = ErrInitFolder
return
}
blob = &Blob{
DbusObject: f.DbusObject,
Name: keyName,
// Value: "",
Recurse: recursion,
wm: f.wallet.wm,
wallet: f.wallet,
folder: f,
isInit: false,
}
blob.isInit = true
if blob.Recurse.AllWalletItems || blob.Recurse.Blobs {
if err = blob.Update(); err != nil {
return
}
}
blob.isInit = true
return
}
// 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
}
// 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
}
// 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
}
// Update fetches a Blob's Blob.Value.
func (b *Blob) Update() (err error) {
var call *dbus.Call
var v dbus.Variant
if err = b.folder.wallet.walletCheck(); err != nil {
return
}
if call = b.Dbus.Call(
DbusWMReadEntry, 0, b.folder.wallet.handle, b.folder.Name, b.Name, b.folder.wallet.wm.AppID,
); call.Err != nil {
err = call.Err
return
}
if err = call.Store(&v); err != nil {
return
}
b.Value = v.Value().([]byte)
return
}
// isWalletItem is needed for interface membership.
func (b *Blob) isWalletItem() (isWalletItem bool) {
isWalletItem = true
return
}