Skip to content

Commit

Permalink
settings: Let ReadOne fall back to Read on error
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Aug 9, 2024
1 parent 2653b8d commit f01d29f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 54 deletions.
28 changes: 0 additions & 28 deletions settings/all.go

This file was deleted.

26 changes: 0 additions & 26 deletions settings/one.go

This file was deleted.

59 changes: 59 additions & 0 deletions settings/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package settings

import (
"github.com/godbus/dbus/v5"
"github.com/rymdport/portal/internal/apis"
)

const (
readCallPath = settingsCallPath + ".Read"
readOneCallPath = settingsCallPath + ".ReadOne"
readAllCallPath = settingsCallPath + ".ReadAll"
)

// ReadAll returns all values for the corresponding namespaces passed.
// If namespaces is an empty array or contains an empty string it matches all.
// Globbing is supported but only for trailing sections, e.g. “org.example.*”.
func ReadAll(namespaces []string) (map[string](map[string]dbus.Variant), error) {
conn, err := dbus.SessionBus() // Shared connection, don't close.
if err != nil {
return nil, err
}

obj := conn.Object(apis.ObjectName, apis.ObjectPath)
call := obj.Call(readAllCallPath, 0, namespaces)
if call.Err != nil {
return nil, call.Err
}

var value map[string](map[string]dbus.Variant)
err = call.Store(&value)
return value, err
}

// ReadOne reads a single value which may be any valid DBus type. Returns an error on any unknown namespace or key.
func ReadOne(namespace, key string) (any, error) {
value, err := read(readOneCallPath, namespace, key)
if err != nil {
return read(readCallPath, namespace, key) // Use deprecated fallback if new interface does not exist.
}

return value, err
}

func read(callPath, namespace, key string) (any, error) {
conn, err := dbus.SessionBus() // Shared connection, don't close.
if err != nil {
return nil, err
}

obj := conn.Object(apis.ObjectName, apis.ObjectPath)
call := obj.Call(callPath, 0, namespace, key)
if call.Err != nil {
return nil, call.Err
}

var value any
err = call.Store(&value)
return value, err
}

0 comments on commit f01d29f

Please sign in to comment.