Commit 574114f2 authored by Ismael Asensio's avatar Ismael Asensio Committed by Nicolas Fella
Browse files

[applet] Refactor bluetooth state properties

Remove the `logic.js` helper file and prefer to set the plasmoid properties
declaratively. Along with some javascript modernizing.
parent 84c3bbe4
Pipeline #186127 passed with stage
in 2 minutes and 8 seconds
......@@ -83,7 +83,7 @@ PlasmaExtras.Representation {
// group because it's unnecessary; all we want to do here is
// separate the connected devices from the available ones
section.delegate: Loader {
active: section !== "Connected" && Logic.conectedDevicesCount() > 0
active: section !== "Connected" && bluetoothApplet.connectedDevices.length > 0
// Need to manually set the height or else the loader takes up
// space after the first time it unloads a previously-loaded item
height: active ? PlasmaCore.Units.gridUnit : 0
......
/*
SPDX-FileCopyrightText: 2014-2015 David Rosca <nowrep@gmail.com>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
function updateStatus() {
var connectedDevices = [];
for (var i = 0; i < btManager.devices.length; ++i) {
var device = btManager.devices[i];
if (device.connected) {
connectedDevices.push(device);
}
}
var text = "";
var bullet = "\u2022";
if (btManager.bluetoothBlocked) {
text = i18n("Bluetooth is disabled");
} else if (!btManager.bluetoothOperational) {
if (!btManager.adapters.length) {
text = i18n("No adapters available");
} else {
text = i18n("Bluetooth is offline");
}
} else if (connectedDevices.length === 1) {
text = i18n("%1 connected", connectedDevices[0].name);
} else if (connectedDevices.length > 1) {
text = i18ncp("Number of connected devices", "%1 connected device", "%1 connected devices", connectedDevices.length);
for (var i = 0; i < connectedDevices.length; ++i) {
var device = connectedDevices[i];
text += "\n %1 %2".arg(bullet).arg(device.name);
}
} else {
text = i18n("No connected devices");
}
plasmoid.toolTipSubText = text;
deviceConnected = connectedDevices.length;
if (btManager.bluetoothOperational) {
plasmoid.status = PlasmaCore.Types.ActiveStatus;
} else {
plasmoid.status = PlasmaCore.Types.PassiveStatus;
}
}
function icon()
{
if (deviceConnected) {
return "preferences-system-bluetooth-activated";
} else if (!btManager.bluetoothOperational) {
return "preferences-system-bluetooth-inactive";
}
return "preferences-system-bluetooth";
}
function conectedDevicesCount() {
var connectedDevices = [];
for (var i = 0; i < btManager.devices.length; ++i) {
var device = btManager.devices[i];
if (device.connected) {
connectedDevices.push(device);
}
}
return connectedDevices.length
}
......@@ -12,51 +12,97 @@ import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.kquickcontrolsaddons 2.0
import org.kde.plasma.private.bluetooth 1.0 as PlasmaBt
import "logic.js" as Logic
Item {
id: bluetoothApplet
property bool deviceConnected : false
property var connectedDevices : []
property int runningActions : 0
property QtObject btManager : BluezQt.Manager
Plasmoid.toolTipMainText: i18n("Bluetooth")
Plasmoid.icon: Logic.icon()
Plasmoid.switchWidth: PlasmaCore.Units.gridUnit * 15
Plasmoid.switchHeight: PlasmaCore.Units.gridUnit * 10
Plasmoid.compactRepresentation: CompactRepresentation { }
Plasmoid.fullRepresentation: FullRepresentation { }
Plasmoid.status: (btManager.bluetoothOperational) ? PlasmaCore.Types.ActiveStatus : PlasmaCore.Types.PassiveStatus
Plasmoid.icon: {
if (connectedDevices.length > 0) {
return "preferences-system-bluetooth-activated";
}
if (!btManager.bluetoothOperational) {
return "preferences-system-bluetooth-inactive";
}
return "preferences-system-bluetooth";
}
Plasmoid.toolTipMainText: i18n("Bluetooth")
Plasmoid.toolTipSubText: {
if (btManager.bluetoothBlocked) {
return i18n("Bluetooth is disabled");
}
if (!btManager.bluetoothOperational) {
if (btManager.adapters.length === 0) {
return i18n("No adapters available");
}
return i18n("Bluetooth is offline");
}
if (connectedDevices.length === 0) {
return i18n("No connected devices");
}
if (connectedDevices.length === 1) {
return i18n("%1 connected", connectedDevices[0].name);
}
let text = i18ncp("Number of connected devices", "%1 connected device", "%1 connected devices", connectedDevices.length);
for (let i = 0; i < connectedDevices.length; ++i) {
const device = connectedDevices[i];
text += "\n \u2022 %1".arg(device.name);
}
return text;
}
Connections {
target: btManager
function onDeviceAdded() {
Logic.updateStatus();
updateConnectedDevices();
}
function onDeviceRemoved() {
Logic.updateStatus();
updateConnectedDevices();
}
function onDeviceChanged() {
Logic.updateStatus();
updateConnectedDevices();
}
function onBluetoothBlockedChanged() {
Logic.updateStatus();
updateConnectedDevices();
}
function onBluetoothOperationalChanged() {
Logic.updateStatus();
updateConnectedDevices();
}
}
function updateConnectedDevices() {
let _connectedDevices = [];
for (let i = 0; i < btManager.devices.length; ++i) {
const device = btManager.devices[i];
if (device.connected) {
_connectedDevices.push(device);
}
}
if (connectedDevices != _connectedDevices) {
connectedDevices = _connectedDevices;
connectedDevicesChanged();
}
}
function toggleBluetooth()
{
var enable = !btManager.bluetoothOperational;
const enable = !btManager.bluetoothOperational;
btManager.bluetoothBlocked = !enable;
for (var i = 0; i < btManager.adapters.length; ++i) {
var adapter = btManager.adapters[i];
for (let i = 0; i < btManager.adapters.length; ++i) {
let adapter = btManager.adapters[i];
adapter.powered = enable;
}
}
......@@ -76,6 +122,6 @@ Item {
plasmoid.setAction("addNewDevice", i18n("Add New Device…"), "list-add");
plasmoid.action("addNewDevice").visible = Qt.binding(() => {return !btManager.bluetoothBlocked;});
Logic.updateStatus();
updateConnectedDevices();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment