File: properties.go

package info (click to toggle)
go-dlib 5.6.0.9%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,212 kB
  • sloc: ansic: 4,664; xml: 1,456; makefile: 20; sh: 15
file content (111 lines) | stat: -rw-r--r-- 2,769 bytes parent folder | download | duplicates (3)
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
package dbusutil

import (
	"errors"

	"github.com/godbus/dbus"
	"github.com/godbus/dbus/prop"
)

func (so *ServerObject) propertiesGet(sender dbus.Sender,
	interfaceName, propertyName string) (dbus.Variant, *dbus.Error) {
	so.service.DelayAutoQuit()

	impl := so.getImplementer(interfaceName)
	if impl == nil {
		return dbus.Variant{}, prop.ErrIfaceNotFound
	}
	p := impl.props[propertyName]
	if p == nil {
		return dbus.Variant{}, prop.ErrPropNotFound
	}

	implStatic := impl.getStatic(so.service)
	propStatic := implStatic.props[propertyName]

	if propStatic.access&accessRead == 0 {
		return dbus.Variant{}, dbus.MakeFailedError(errors.New("property can not be read"))
	}

	propRead := newPropertyRead(sender, so, interfaceName, propertyName)

	variant, err := p.GetValueVariant(propRead, propStatic.signature)
	if err != nil {
		return dbus.Variant{}, err
	}
	return variant, nil
}

func (so *ServerObject) propertiesGetAll(sender dbus.Sender, interfaceName string) (map[string]dbus.Variant, *dbus.Error) {
	so.service.DelayAutoQuit()

	impl := so.getImplementer(interfaceName)
	if impl == nil {
		return nil, prop.ErrIfaceNotFound
	}

	implStatic := impl.getStatic(so.service)

	result := make(map[string]dbus.Variant, len(impl.props))
	for propName, p := range impl.props {
		propStatic := implStatic.props[propName]

		if propStatic.access&accessRead != 0 {
			propRead := newPropertyRead(sender, so, interfaceName, propName)
			variant, err := p.GetValueVariant(propRead, propStatic.signature)
			if err != nil {
				// ignore err
				continue
			}
			result[propName] = variant
		}
	}
	return result, nil
}

func (so *ServerObject) propertiesSet(sender dbus.Sender, interfaceName, propertyName string,
	newVar dbus.Variant) *dbus.Error {
	so.service.DelayAutoQuit()

	impl := so.getImplementer(interfaceName)
	if impl == nil {
		return prop.ErrIfaceNotFound
	}

	p := impl.props[propertyName]
	if p == nil {
		return prop.ErrPropNotFound
	}

	implStatic := impl.getStatic(so.service)
	propStatic := implStatic.props[propertyName]

	if propStatic.access&accessWrite == 0 {
		return dbus.MakeFailedError(errors.New("property can not be written"))
	}

	if newVar.Signature() != propStatic.signature {
		return prop.ErrInvalidArg
	}
	newVarValue := newVar.Value()

	//fix newVarValue []interface{} to struct
	if propStatic.hasStruct {
		fixedRV, err := valueFromBus(newVarValue, propStatic.rType)
		if err != nil {
			return dbus.MakeFailedError(err)
		}
		newVarValue = fixedRV.Interface()
	}

	propWrite := newPropertyWrite(sender, so, interfaceName, propertyName, newVarValue)

	changed, setErr := p.SetValue(propWrite)
	if setErr != nil {
		return setErr
	}
	if changed {
		impl.notifyChanged(so.service, so.path, p, propStatic, newVarValue)
	}
	return nil
}