Warning, /plasma/plasma-mobile/quicksettings/bluetooth/contents/ui/main.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 import QtQuick 2.15
0005 
0006 import org.kde.bluezqt 1.0 as BluezQt
0007 import org.kde.plasma.private.mobileshell.quicksettingsplugin as QS
0008 
0009 QS.QuickSetting {
0010     property QtObject btManager: BluezQt.Manager
0011     property var connectedDevices: []
0012 
0013     id: root
0014 
0015     text: i18n("Bluetooth")
0016     icon: "network-bluetooth"
0017     settingsCommand: "plasma-open-settings kcm_bluetooth"
0018     function toggle() {
0019         const enable = !btManager.bluetoothOperational;
0020         btManager.bluetoothBlocked = !enable;
0021 
0022         for (var i = 0; i < btManager.adapters.length; ++i) {
0023             btManager.adapters[i].powered = enable;
0024         }
0025     }
0026     enabled: btManager.bluetoothOperational
0027 
0028     Connections {
0029         target: btManager
0030 
0031         function onDeviceAdded() {
0032             updateConnectedDevices();
0033         }
0034         function onDeviceRemoved() {
0035             updateConnectedDevices();
0036         }
0037         function onDeviceChanged() {
0038             updateConnectedDevices();
0039         }
0040         function onBluetoothBlockedChanged() {
0041             updateConnectedDevices();
0042         }
0043         function onBluetoothOperationalChanged() {
0044             updateConnectedDevices();
0045         }
0046     }
0047 
0048     function updateConnectedDevices() {
0049         let _connectedDevices = [];
0050         for (let i = 0; i < btManager.devices.length; ++i) {
0051             const device = btManager.devices[i];
0052             if (device.connected) {
0053                 _connectedDevices.push(device);
0054             }
0055         }
0056 
0057         if (connectedDevices != _connectedDevices) {
0058             connectedDevices = _connectedDevices;
0059 
0060             if (connectedDevices.length === 0) {
0061                 root.status = ""
0062             } else if (connectedDevices.length === 1) {
0063                 root.status = formatDevice(0);
0064             } else {
0065                 let text = "";
0066                 for (let i = 0; i < connectedDevices.length; i++) {
0067                     const device = connectedDevices[i];
0068                     const battery = device.battery;
0069                     text += formatDevice(i) + " \u2022 ";
0070                 }
0071 
0072                 // trims until the last dot
0073                 text = text.substring(0, text.length - 2);
0074 
0075                 root.status = text;
0076             }
0077         }
0078     }
0079 
0080     function formatDevice(deviceIndex) {
0081         const device = connectedDevices[deviceIndex];
0082         const battery = device.battery;
0083         const name = device.name;
0084 
0085         return battery
0086             ? "%1 ยท %2".arg(name).arg(battery.percentage)
0087             : name;
0088     }
0089 }