File: DBusProperty.qml

package info (click to toggle)
kdeconnect 25.04.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: cpp: 22,922; xml: 520; python: 92; sh: 25; makefile: 5
file content (70 lines) | stat: -rw-r--r-- 1,599 bytes parent folder | download
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
/**
 * SPDX-FileCopyrightText: 2016 Aleix Pol Gonzalez <aleixpol@kde.org>
 * 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
 */

pragma ComponentBehavior: Bound

import QtQml

import org.kde.kdeconnect as KDEConnect

QtObject {
    id: prop

    property QtObject object
    property string read
    property string change: read + "Changed"

    Component.onCompleted: {
        get();
    }

    onChangeChanged: {
        if (object) {
            const signal = object[change];
            if (signal) {
                signal.connect(valueReceived);
            } else {
                console.warn(`couldn't find signal ${change} for ${object}`);
            }
        }
    }

    function valueReceived(value: var): void {
        if (!value) {
            get();
        } else {
            _value = value;
        }
    }

    property var defaultValue
    property var _value: defaultValue
    readonly property var value: _value

    readonly property KDEConnect.DBusAsyncResponse __response: KDEConnect.DBusAsyncResponse {
        id: response

        autoDelete: false

        onSuccess: result => {
            prop._value = result;
        }

        onError: message => {
            console.warn("failed call", prop.object, prop.read, prop.change, message);
        }
    }

    function get(): void {
        if (object) {
            const method = object[read];
            if (method) {
                response.setPendingCall(method());
            }
        }
    }
}