66 lines
3.1 KiB
QML
66 lines
3.1 KiB
QML
pragma ComponentBehavior: Bound
|
|
import QtQuick
|
|
import Quickshell.Networking
|
|
|
|
QtObject {
|
|
id: root
|
|
property var device: null
|
|
property var pendingNetwork: null
|
|
property string operation: ""
|
|
property bool busy: false
|
|
property string error: ""
|
|
property int timeoutMs: 45000
|
|
signal needsSecret(var network)
|
|
signal succeeded(string action)
|
|
function fail(message) { busy = false; pendingNetwork = null; error = message; }
|
|
function checkResult() {
|
|
if (!busy) return;
|
|
if (!device || device.nmManaged !== true) { fail("Интерфейс недоступен для управления"); return; }
|
|
if ((operation === "connect" && pendingNetwork?.connected) || (operation === "disconnect" && !device.connected)) {
|
|
const action = operation;
|
|
busy = false; pendingNetwork = null; succeeded(action);
|
|
}
|
|
}
|
|
function requestConnect(network, psk) {
|
|
if (busy) return false;
|
|
if (!device || !device.nmManaged || !network || network.device !== device) { error = "Подключение недоступно"; return false; }
|
|
error = ""; operation = "connect"; pendingNetwork = network; busy = true;
|
|
try {
|
|
if (psk !== undefined && psk !== "") network.connectWithPsk(psk);
|
|
else network.connect();
|
|
checkResult();
|
|
return true;
|
|
} catch (e) { fail(String(e)); return false; }
|
|
}
|
|
function requestDisconnect() {
|
|
if (busy) return false;
|
|
if (!device || !device.nmManaged || !device.connected) { error = "Интерфейс уже отключён или недоступен"; return false; }
|
|
error = ""; operation = "disconnect"; pendingNetwork = null; busy = true;
|
|
try { device.disconnect(); checkResult(); return true; }
|
|
catch (e) { fail(String(e)); return false; }
|
|
}
|
|
onDeviceChanged: if (busy) fail("Интерфейс был отключён")
|
|
onPendingNetworkChanged: if (busy && operation === "connect" && !pendingNetwork) fail("Сеть больше недоступна")
|
|
property Connections deviceEvents: Connections {
|
|
target: root.device
|
|
function onConnectedChanged() { root.checkResult(); }
|
|
function onStateChanged() { root.checkResult(); }
|
|
function onNmManagedChanged() { root.checkResult(); }
|
|
}
|
|
property Connections networkEvents: Connections {
|
|
target: root.pendingNetwork
|
|
function onConnectedChanged() { root.checkResult(); }
|
|
function onStateChanged() { root.checkResult(); }
|
|
function onConnectionFailed(reason) {
|
|
const network = root.pendingNetwork;
|
|
root.fail("Не удалось подключиться: " + ConnectionFailReason.toString(reason));
|
|
if (reason === ConnectionFailReason.NoSecrets) root.needsSecret(network);
|
|
}
|
|
}
|
|
property Timer watchdog: Timer {
|
|
interval: root.timeoutMs
|
|
running: root.busy
|
|
onTriggered: root.fail("Время ожидания истекло. Результат подключения не подтверждён.")
|
|
}
|
|
}
|