Bluetooth.qml 4.92 KB
Newer Older
Nicolas Fella's avatar
Nicolas Fella committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * SPDX-FileCopyrightText: 2020 Nicolas Fella <nicolas.fella@gmx.de>
 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
 */

import QtQuick 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.10 as QQC2

import org.kde.kirigami 2.12 as Kirigami
import org.kde.kcm 1.2

import org.kde.bluezqt 1.0 as BluezQt

import org.kde.plasma.private.bluetooth 1.0

ScrollViewKCM {

    id: root

Nicolas Fella's avatar
Nicolas Fella committed
21
22
23
    implicitHeight: Kirigami.Units.gridUnit * 28
    implicitWidth: Kirigami.Units.gridUnit * 28

Nicolas Fella's avatar
Nicolas Fella committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    function setBluetoothEnabled(enabled) {
        BluezQt.Manager.bluetoothBlocked = !enabled

        for (var i = 0; i < BluezQt.Manager.adapters.length; ++i) {
            var adapter = BluezQt.Manager.adapters[i];
            adapter.powered = enabled;
        }
    }

    header: Kirigami.InlineMessage {
        id: errorMessage
        type: Kirigami.MessageType.Error
        showCloseButton: true
    }

    view: ListView {
        clip: true

        Kirigami.PlaceholderMessage {
43
44
45
46
47
48
49
50
            id: noBluetoothMessage
            visible: !BluezQt.Manager.rfkill.state === BluezQt.Rfkill.Unknown && BluezQt.Manager.adapters.length === 0
            text: i18n("No Bluetooth adapters found")
            width: parent.width - (Kirigami.Units.largeSpacing * 4)
            anchors.centerIn: parent
        }

        Kirigami.PlaceholderMessage {
51
            visible: BluezQt.Manager.operational && !BluezQt.Manager.bluetoothOperational && !noBluetoothMessage.visible
Nicolas Fella's avatar
Nicolas Fella committed
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
            text: i18n("Bluetooth is disabled")
            width: parent.width - (Kirigami.Units.largeSpacing * 4)
            anchors.centerIn: parent

            helpfulAction: Kirigami.Action {
                iconName: "network-bluetooth"
                text: i18n("Enable")
                onTriggered: {
                    root.setBluetoothEnabled(true)
                }
            }
        }

        model: BluezQt.Manager.bluetoothOperational ? devicesModel : []

        QQC2.BusyIndicator {
            id: busyIndicator
            running: false
            anchors.centerIn: parent
        }


        DevicesProxyModel {
            id: devicesModel
            sourceModel: BluezQt.DevicesModel { }
        }

        section.property: "Connected"
        section.delegate: Kirigami.ListSectionHeader {
            text: section === "true" ? i18n("Connected") : i18n("Available")
        }

        delegate: Kirigami.SwipeListItem {

            contentItem: Kirigami.BasicListItem {
                // The parent item already has a highlight
                activeBackgroundColor: "transparent"
                // Otherwise there are unnecessary margins
                anchors.top: parent.top
                anchors.bottom: parent.bottom
                anchors.left: parent.left
                // No right anchor so text can be elided by actions

                text: model.Name
                icon: model.Icon
                onClicked: kcm.push("Device.qml", {device: model.Device})
            }

            actions: [
                Kirigami.Action {
                    text: model.Connected ? i18n("Disconnect") : i18n("Connect")
                    icon.name: model.Connected ? "network-disconnect" : "network-connect"
                    onTriggered: {
                        if (model.Connected) {
                            makeCall(model.Device.disconnectFromDevice())
                        } else {
                            makeCall(model.Device.connectToDevice())
                        }
                    }
                },
                Kirigami.Action {
                    text: i18n("Remove")
                    icon.name: "list-remove"
                    onTriggered: {
                        makeCall(model.Adapter.removeDevice(model.Device))
                    }
                }
            ]
            function makeCall(call) {
                busyIndicator.running = true
                call.finished.connect(call => {
                    busyIndicator.running = false
                    if (call.error) {
                        errorMessage.text = call.errorText
                        errorMessage.visible = true
                    }
                })
            }
        }
    }

    footer: RowLayout {

        QQC2.Button {
            text: i18n("Add...")
            visible: BluezQt.Manager.bluetoothOperational
            icon.name: "list-add"
            onClicked: kcm.runWizard()
        }

        QQC2.Button {
            visible: BluezQt.Manager.bluetoothOperational
            text: i18n("Disable Bluetooth")
            icon.name: "network-bluetooth"
            onClicked: {
                root.setBluetoothEnabled(false)
            }
        }

        Item {
            Layout.fillWidth: true
        }

        QQC2.Button {
            visible: BluezQt.Manager.bluetoothOperational
            text: i18n("Configure...")
            icon.name: "configure"
            onClicked: kcm.push("General.qml")
        }
    }
}