refactor: NetworkConnection util created, migrated all functions
This commit is contained in:
parent
e8fc13630c
commit
70ec8cea65
7 changed files with 144 additions and 143 deletions
|
|
@ -131,27 +131,19 @@ ColumnLayout {
|
||||||
Nmcli.disconnectFromNetwork();
|
Nmcli.disconnectFromNetwork();
|
||||||
} else {
|
} else {
|
||||||
root.connectingToSsid = networkItem.modelData.ssid;
|
root.connectingToSsid = networkItem.modelData.ssid;
|
||||||
// Check if network is secure
|
NetworkConnection.handleConnect(
|
||||||
if (networkItem.modelData.isSecure) {
|
networkItem.modelData,
|
||||||
// Try to connect first - will show password dialog if password is needed
|
null,
|
||||||
Nmcli.connectToNetwork(networkItem.modelData.ssid, "", networkItem.modelData.bssid, result => {
|
(network) => {
|
||||||
if (result && result.needsPassword) {
|
// Password is required - show password dialog
|
||||||
// Password is required - show password dialog
|
root.passwordNetwork = network;
|
||||||
root.passwordNetwork = networkItem.modelData;
|
root.showPasswordDialog = true;
|
||||||
root.showPasswordDialog = true;
|
root.wrapper.currentName = "wirelesspassword";
|
||||||
root.wrapper.currentName = "wirelesspassword";
|
}
|
||||||
} else if (result && result.success) {
|
);
|
||||||
// Connection successful with saved password
|
|
||||||
root.connectingToSsid = "";
|
// Clear connecting state if connection succeeds immediately (saved profile)
|
||||||
} else {
|
// This is handled by the onActiveChanged connection below
|
||||||
// Connection failed for other reasons
|
|
||||||
root.connectingToSsid = "";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// Open network, no password needed
|
|
||||||
Nmcli.connectToNetwork(networkItem.modelData.ssid, "", networkItem.modelData.bssid, null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import qs.components
|
||||||
import qs.components.controls
|
import qs.components.controls
|
||||||
import qs.services
|
import qs.services
|
||||||
import qs.config
|
import qs.config
|
||||||
|
import qs.utils
|
||||||
import Quickshell
|
import Quickshell
|
||||||
import QtQuick
|
import QtQuick
|
||||||
import QtQuick.Layouts
|
import QtQuick.Layouts
|
||||||
|
|
@ -452,7 +453,7 @@ ColumnLayout {
|
||||||
text = qsTr("Connecting...");
|
text = qsTr("Connecting...");
|
||||||
|
|
||||||
// Connect to network
|
// Connect to network
|
||||||
Nmcli.connectToNetwork(root.network.ssid, password, root.network.bssid || "", result => {
|
NetworkConnection.connectWithPassword(root.network, password, result => {
|
||||||
if (result && result.success)
|
if (result && result.success)
|
||||||
// Connection successful, monitor will handle the rest
|
// Connection successful, monitor will handle the rest
|
||||||
{} else if (result && result.needsPassword) {
|
{} else if (result && result.needsPassword) {
|
||||||
|
|
|
||||||
|
|
@ -415,7 +415,7 @@ Item {
|
||||||
if (modelData && modelData.active) {
|
if (modelData && modelData.active) {
|
||||||
Nmcli.disconnectFromNetwork();
|
Nmcli.disconnectFromNetwork();
|
||||||
} else if (modelData) {
|
} else if (modelData) {
|
||||||
handleWirelessConnect(modelData);
|
NetworkConnection.handleConnect(modelData, root.session, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -586,46 +586,5 @@ Item {
|
||||||
Nmcli.loadSavedConnections(() => {});
|
Nmcli.loadSavedConnections(() => {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleWirelessConnect(network): void {
|
|
||||||
if (Nmcli.active && Nmcli.active.ssid !== network.ssid) {
|
|
||||||
Nmcli.disconnectFromNetwork();
|
|
||||||
Qt.callLater(() => {
|
|
||||||
connectToWirelessNetwork(network);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
connectToWirelessNetwork(network);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function connectToWirelessNetwork(network): void {
|
|
||||||
if (network.isSecure) {
|
|
||||||
const hasSavedProfile = Nmcli.hasSavedProfile(network.ssid);
|
|
||||||
|
|
||||||
if (hasSavedProfile) {
|
|
||||||
Nmcli.connectToNetwork(network.ssid, "", network.bssid, null);
|
|
||||||
} else {
|
|
||||||
Nmcli.connectToNetworkWithPasswordCheck(
|
|
||||||
network.ssid,
|
|
||||||
network.isSecure,
|
|
||||||
(result) => {
|
|
||||||
if (result.needsPassword) {
|
|
||||||
if (Nmcli.pendingConnection) {
|
|
||||||
Nmcli.connectionCheckTimer.stop();
|
|
||||||
Nmcli.immediateCheckTimer.stop();
|
|
||||||
Nmcli.immediateCheckTimer.checkCount = 0;
|
|
||||||
Nmcli.pendingConnection = null;
|
|
||||||
}
|
|
||||||
root.session.network.showPasswordDialog = true;
|
|
||||||
root.session.network.pendingNetwork = network;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
network.bssid
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Nmcli.connectToNetwork(network.ssid, "", network.bssid, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import qs.components.effects
|
||||||
import qs.components.containers
|
import qs.components.containers
|
||||||
import qs.services
|
import qs.services
|
||||||
import qs.config
|
import qs.config
|
||||||
|
import qs.utils
|
||||||
import QtQuick
|
import QtQuick
|
||||||
import QtQuick.Layouts
|
import QtQuick.Layouts
|
||||||
|
|
||||||
|
|
@ -125,7 +126,7 @@ Item {
|
||||||
checked: root.network?.active ?? false
|
checked: root.network?.active ?? false
|
||||||
toggle.onToggled: {
|
toggle.onToggled: {
|
||||||
if (checked) {
|
if (checked) {
|
||||||
root.handleConnect();
|
NetworkConnection.handleConnect(root.network, root.session, null);
|
||||||
} else {
|
} else {
|
||||||
Nmcli.disconnectFromNetwork();
|
Nmcli.disconnectFromNetwork();
|
||||||
}
|
}
|
||||||
|
|
@ -207,39 +208,4 @@ Item {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleConnect(): void {
|
|
||||||
if (Nmcli.active && Nmcli.active.ssid !== root.network.ssid) {
|
|
||||||
Nmcli.disconnectFromNetwork();
|
|
||||||
Qt.callLater(() => {
|
|
||||||
connectToNetwork();
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
connectToNetwork();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function connectToNetwork(): void {
|
|
||||||
if (root.network.isSecure) {
|
|
||||||
const hasSavedProfile = Nmcli.hasSavedProfile(root.network.ssid);
|
|
||||||
|
|
||||||
if (hasSavedProfile) {
|
|
||||||
Nmcli.connectToNetwork(root.network.ssid, "", root.network.bssid, null);
|
|
||||||
} else {
|
|
||||||
Nmcli.connectToNetworkWithPasswordCheck(root.network.ssid, root.network.isSecure, result => {
|
|
||||||
if (result.needsPassword) {
|
|
||||||
if (Nmcli.pendingConnection) {
|
|
||||||
Nmcli.connectionCheckTimer.stop();
|
|
||||||
Nmcli.immediateCheckTimer.stop();
|
|
||||||
Nmcli.immediateCheckTimer.checkCount = 0;
|
|
||||||
Nmcli.pendingConnection = null;
|
|
||||||
}
|
|
||||||
root.session.network.showPasswordDialog = true;
|
|
||||||
root.session.network.pendingNetwork = root.network;
|
|
||||||
}
|
|
||||||
}, root.network.bssid);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Nmcli.connectToNetwork(root.network.ssid, "", root.network.bssid, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import qs.components.controls
|
||||||
import qs.components.containers
|
import qs.components.containers
|
||||||
import qs.services
|
import qs.services
|
||||||
import qs.config
|
import qs.config
|
||||||
|
import qs.utils
|
||||||
import QtQuick
|
import QtQuick
|
||||||
import QtQuick.Layouts
|
import QtQuick.Layouts
|
||||||
|
|
||||||
|
|
@ -193,7 +194,7 @@ ColumnLayout {
|
||||||
if (modelData.active) {
|
if (modelData.active) {
|
||||||
Nmcli.disconnectFromNetwork();
|
Nmcli.disconnectFromNetwork();
|
||||||
} else {
|
} else {
|
||||||
handleConnect(modelData);
|
NetworkConnection.handleConnect(modelData, root.session, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -217,45 +218,4 @@ ColumnLayout {
|
||||||
Nmcli.loadSavedConnections(() => {});
|
Nmcli.loadSavedConnections(() => {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleConnect(network): void {
|
|
||||||
if (Nmcli.active && Nmcli.active.ssid !== network.ssid) {
|
|
||||||
Nmcli.disconnectFromNetwork();
|
|
||||||
Qt.callLater(() => {
|
|
||||||
connectToNetwork(network);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
connectToNetwork(network);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function connectToNetwork(network): void {
|
|
||||||
if (network.isSecure) {
|
|
||||||
const hasSavedProfile = Nmcli.hasSavedProfile(network.ssid);
|
|
||||||
|
|
||||||
if (hasSavedProfile) {
|
|
||||||
Nmcli.connectToNetwork(network.ssid, "", network.bssid, null);
|
|
||||||
} else {
|
|
||||||
Nmcli.connectToNetworkWithPasswordCheck(
|
|
||||||
network.ssid,
|
|
||||||
network.isSecure,
|
|
||||||
(result) => {
|
|
||||||
if (result.needsPassword) {
|
|
||||||
if (Nmcli.pendingConnection) {
|
|
||||||
Nmcli.connectionCheckTimer.stop();
|
|
||||||
Nmcli.immediateCheckTimer.stop();
|
|
||||||
Nmcli.immediateCheckTimer.checkCount = 0;
|
|
||||||
Nmcli.pendingConnection = null;
|
|
||||||
}
|
|
||||||
root.session.network.showPasswordDialog = true;
|
|
||||||
root.session.network.pendingNetwork = network;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
network.bssid
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Nmcli.connectToNetwork(network.ssid, "", network.bssid, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
@ -8,6 +8,7 @@ import qs.components.effects
|
||||||
import qs.components.containers
|
import qs.components.containers
|
||||||
import qs.services
|
import qs.services
|
||||||
import qs.config
|
import qs.config
|
||||||
|
import qs.utils
|
||||||
import Quickshell
|
import Quickshell
|
||||||
import QtQuick
|
import QtQuick
|
||||||
import QtQuick.Layouts
|
import QtQuick.Layouts
|
||||||
|
|
@ -391,7 +392,7 @@ Item {
|
||||||
text = qsTr("Connecting...");
|
text = qsTr("Connecting...");
|
||||||
|
|
||||||
// Connect to network
|
// Connect to network
|
||||||
Nmcli.connectToNetwork(root.network.ssid, password, root.network.bssid || "", result => {
|
NetworkConnection.connectWithPassword(root.network, password, result => {
|
||||||
if (result && result.success)
|
if (result && result.success)
|
||||||
// Connection successful, monitor will handle the rest
|
// Connection successful, monitor will handle the rest
|
||||||
{} else if (result && result.needsPassword) {
|
{} else if (result && result.needsPassword) {
|
||||||
|
|
|
||||||
122
utils/NetworkConnection.qml
Normal file
122
utils/NetworkConnection.qml
Normal file
|
|
@ -0,0 +1,122 @@
|
||||||
|
pragma Singleton
|
||||||
|
|
||||||
|
import qs.services
|
||||||
|
import QtQuick
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NetworkConnection
|
||||||
|
*
|
||||||
|
* Centralized utility for network connection logic. Provides a single source of truth
|
||||||
|
* for connecting to wireless networks, eliminating code duplication across
|
||||||
|
* controlcenter components and bar popouts.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* ```qml
|
||||||
|
* import qs.utils
|
||||||
|
*
|
||||||
|
* // With Session object (controlcenter)
|
||||||
|
* NetworkConnection.handleConnect(network, session);
|
||||||
|
*
|
||||||
|
* // Without Session object (bar popouts) - provide password dialog callback
|
||||||
|
* NetworkConnection.handleConnect(network, null, (network) => {
|
||||||
|
* // Show password dialog
|
||||||
|
* root.passwordNetwork = network;
|
||||||
|
* root.showPasswordDialog = true;
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
QtObject {
|
||||||
|
id: root
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle network connection with automatic disconnection if needed.
|
||||||
|
* If there's an active network different from the target, disconnects first,
|
||||||
|
* then connects to the target network.
|
||||||
|
*
|
||||||
|
* @param network The network object to connect to (must have ssid property)
|
||||||
|
* @param session Optional Session object (for controlcenter - must have network property with showPasswordDialog and pendingNetwork)
|
||||||
|
* @param onPasswordNeeded Optional callback function(network) called when password is needed (for bar popouts)
|
||||||
|
*/
|
||||||
|
function handleConnect(network, session, onPasswordNeeded): void {
|
||||||
|
if (!network) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Nmcli.active && Nmcli.active.ssid !== network.ssid) {
|
||||||
|
Nmcli.disconnectFromNetwork();
|
||||||
|
Qt.callLater(() => {
|
||||||
|
root.connectToNetwork(network, session, onPasswordNeeded);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
root.connectToNetwork(network, session, onPasswordNeeded);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect to a wireless network.
|
||||||
|
* Handles both secured and open networks, checks for saved profiles,
|
||||||
|
* and shows password dialog if needed.
|
||||||
|
*
|
||||||
|
* @param network The network object to connect to (must have ssid, isSecure, bssid properties)
|
||||||
|
* @param session Optional Session object (for controlcenter - must have network property with showPasswordDialog and pendingNetwork)
|
||||||
|
* @param onPasswordNeeded Optional callback function(network) called when password is needed (for bar popouts)
|
||||||
|
*/
|
||||||
|
function connectToNetwork(network, session, onPasswordNeeded): void {
|
||||||
|
if (!network) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (network.isSecure) {
|
||||||
|
const hasSavedProfile = Nmcli.hasSavedProfile(network.ssid);
|
||||||
|
|
||||||
|
if (hasSavedProfile) {
|
||||||
|
Nmcli.connectToNetwork(network.ssid, "", network.bssid, null);
|
||||||
|
} else {
|
||||||
|
// Use password check with callback
|
||||||
|
Nmcli.connectToNetworkWithPasswordCheck(
|
||||||
|
network.ssid,
|
||||||
|
network.isSecure,
|
||||||
|
(result) => {
|
||||||
|
if (result.needsPassword) {
|
||||||
|
// Clear pending connection if exists
|
||||||
|
if (Nmcli.pendingConnection) {
|
||||||
|
Nmcli.connectionCheckTimer.stop();
|
||||||
|
Nmcli.immediateCheckTimer.stop();
|
||||||
|
Nmcli.immediateCheckTimer.checkCount = 0;
|
||||||
|
Nmcli.pendingConnection = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle password dialog - use session if available, otherwise use callback
|
||||||
|
if (session && session.network) {
|
||||||
|
session.network.showPasswordDialog = true;
|
||||||
|
session.network.pendingNetwork = network;
|
||||||
|
} else if (onPasswordNeeded) {
|
||||||
|
onPasswordNeeded(network);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
network.bssid
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Nmcli.connectToNetwork(network.ssid, "", network.bssid, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect to a wireless network with a provided password.
|
||||||
|
* Used by password dialogs when the user has already entered a password.
|
||||||
|
*
|
||||||
|
* @param network The network object to connect to (must have ssid, bssid properties)
|
||||||
|
* @param password The password to use for connection
|
||||||
|
* @param onResult Optional callback function(result) called with connection result
|
||||||
|
*/
|
||||||
|
function connectWithPassword(network, password, onResult): void {
|
||||||
|
if (!network) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Nmcli.connectToNetwork(network.ssid, password || "", network.bssid || "", onResult || null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Add table
Reference in a new issue