kdeconnect-kde/declarativeplugin/qml/DBusProperty.qml

71 lines
1.6 KiB
QML
Raw Normal View History

/**
* SPDX-FileCopyrightText: 2016 Aleix Pol Gonzalez <aleixpol@kde.org>
2024-06-30 23:03:57 +01:00
* SPDX-FileCopyrightText: 2024 ivan tkachenko <me@ratijas.tk>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
2024-06-30 23:03:57 +01:00
pragma ComponentBehavior: Bound
import QtQml
import org.kde.kdeconnect as KDEConnect
QtObject {
id: prop
2024-06-30 23:03:57 +01:00
property QtObject object
property string read
2024-06-30 23:03:57 +01:00
property string change: read + "Changed"
2024-06-30 23:03:57 +01:00
Component.onCompleted: {
get();
}
onChangeChanged: {
if (object) {
2024-06-30 23:03:57 +01:00
const signal = object[change];
if (signal) {
signal.connect(valueReceived);
} else {
2024-06-30 23:03:57 +01:00
console.warn(`couldn't find signal ${change} for ${object}`);
}
}
}
2024-06-30 23:03:57 +01:00
function valueReceived(value: var): void {
if (!value) {
get();
} else {
2024-06-30 23:03:57 +01:00
_value = value;
}
}
property var defaultValue
property var _value: defaultValue
readonly property var value: _value
2024-06-30 23:03:57 +01:00
readonly property KDEConnect.DBusAsyncResponse __response: KDEConnect.DBusAsyncResponse {
id: response
2024-06-30 23:03:57 +01:00
autoDelete: false
2024-06-30 23:03:57 +01:00
onSuccess: result => {
prop._value = result;
}
2024-06-30 23:03:57 +01:00
onError: message => {
2024-06-30 23:03:57 +01:00
console.warn("failed call", prop.object, prop.read, prop.change, message);
}
}
2024-06-30 23:03:57 +01:00
function get(): void {
if (object) {
const method = object[read];
if (method) {
response.setPendingCall(method());
}
}
}
}