Skip to content

Commit

Permalink
feat: Add network configuration error handling
Browse files Browse the repository at this point in the history
Add network configuration error handling

Log:
Task: https://pms.uniontech.com/task-view-361719.html
  • Loading branch information
caixr23 committed Oct 31, 2024
1 parent a8700a1 commit d5defe8
Show file tree
Hide file tree
Showing 23 changed files with 1,585 additions and 675 deletions.
1 change: 1 addition & 0 deletions dcc-network/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ set(DccNetwork_Libraries
# Qt6::Quick
Dtk6::Core
KF6::NetworkManagerQt
Dde::Control-Center
dde-network-core6
)

Expand Down
5 changes: 5 additions & 0 deletions dcc-network/operation/dccnetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ NetItem *DccNetwork::root() const
return m_manager->root();
}

bool DccNetwork::CheckPasswordValid(const QString &key, const QString &password)
{
return NetManager::CheckPasswordValid(key, password);
}

void DccNetwork::exec(NetManager::CmdType cmd, const QString &id, const QVariantMap &param)
{
switch (cmd) {
Expand Down
1 change: 1 addition & 0 deletions dcc-network/operation/dccnetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class DccNetwork : public QObject
public:
explicit DccNetwork(QObject *parent = nullptr);
NetItem *root() const;
Q_INVOKABLE static bool CheckPasswordValid(const QString &key, const QString &password);

NetManager *manager() const { return m_manager; }

Expand Down
59 changes: 59 additions & 0 deletions dcc-network/qml/NetFileChooseEdit.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later
import QtQuick 2.15
import QtQuick.Layouts 1.15
import Qt.labs.platform 1.1

import org.deepin.dtk 1.0 as D

RowLayout {
id: root
property var dataItem
property var nameFilters: [qsTr("All files (*)")]
property alias placeholderText: edit.placeholderText
property alias text: edit.text
property bool initialized: false
signal textUpdated

Component {
id: fileDialog
FileDialog {
visible: false
nameFilters: root.nameFilters
onAccepted: {
edit.text = currentFile.toString().replace("file://", "")
dataItem.editClicked()
this.destroy(10)
}
onRejected: this.destroy(10)
}
}
D.LineEdit {
id: edit
onTextChanged: {
if (!initialized) {
return
}
if (showAlert) {
dataItem.errorKey = ""
}
root.textUpdated()
dataItem.editClicked()
}
showAlert: dataItem.errorKey === dccObj.name
alertDuration: 2000
onShowAlertChanged: {
if (showAlert) {
dccObj.trigger()
forceActiveFocus()
}
}
Component.onCompleted: initialized = true
}
NetButton {
text: "..."
onClicked: {
fileDialog.createObject(this).open()
}
}
}
40 changes: 40 additions & 0 deletions dcc-network/qml/NetPasswordEdit.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later
import QtQuick 2.15

import org.deepin.dtk 1.0 as D

D.PasswordEdit {
property var dataItem
property bool newInput: false
property bool initialized: false
signal textUpdated

placeholderText: qsTr("Required")
echoButtonVisible: newInput
showAlert: dataItem.errorKey === dccObj.name
onTextChanged: {
if (!initialized) {
return
}
if (showAlert) {
dataItem.errorKey = ""
}
if (text.length === 0) {
newInput = true
}
textUpdated()
dataItem.editClicked()
}
alertDuration: 2000
onShowAlertChanged: {
if (showAlert) {
dccObj.trigger()
forceActiveFocus()
}
}
Component.onCompleted: {
newInput = text.length === 0
initialized = true
}
}
117 changes: 117 additions & 0 deletions dcc-network/qml/NetUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ const VpnTypeEnum = Object.freeze({
"openconnect": 0x20,
"sstp": 0x40
})
// ip正则表达式
const ipRegExp = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
const ipv6RegExp = /^((([0-9A-Fa-f]{1,4}:){7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.0\.0|255\.(254|252|248|240|224|192|128|0)\.0\.0|255\.255\.(254|252|248|240|224|192|128|0)\.0|255\.255\.255\.(254|252|248|240|224|192|128|0)/
// MAC正则表达式
const macRegExp = /([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})/

function toVpnTypeEnum(vpnKey) {
let key = vpnKey.substring(31)
console.log("toVpnTypeEnum", vpnKey, key)
Expand All @@ -29,3 +37,112 @@ function toVpnKey(vpnType) {
function removeTrailingNull(str) {
return str ? str.toString().replace(/\0+$/, '') : ""
}

function numToIp(num) {
let ips = [0, 0, 0, 0]
for (var i = 0; i < 4; i++) {
ips[i] = (num >> (i * 8)) & 255
}
return ips.join('.')
}

function ipToNum(ip) {
console.log("ipToNum----", ip, typeof ip)
let ips = ip.split('.')
let cidr = 0
let ipNum = 0
if (ips.length !== 4) {
return 0
}
for (let ipStr of ips) {
let num = parseInt(ipStr, 10)
ipNum |= ((num & 255) << cidr)
cidr += 8
}
return ipNum
}

function prefixToIp(subnetMask) {
if (subnetMask < 0 || subnetMask > 32) {
throw new Error("Subnet mask must be between 0 and 32")
}

let maskArray = [255, 255, 255, 255]

for (var i = 0; i < 4; i++) {
let byteBits = i * 8 + 8 - subnetMask
if (byteBits > 0) {
maskArray[i] = (255 << byteBits) & 255
}
}

return maskArray.join('.')
}

function ipToPrefix(decimalSubnet) {
let octets = decimalSubnet.split('.')
let cidr = 0

for (let octet of octets) {
let num = parseInt(octet, 10)
for (var i = 0; i < 8; i++) {
if ((num & (1 << (7 - i))) !== 0) {
cidr++
} else {
break
}
}
if (cidr === 32)
break
}
return cidr
}

function macToStr(mac) {
return Array.prototype.map.call(new Uint8Array(mac), x => ('00' + x.toString(16)).toUpperCase().slice(-2)).join(':')
}
function strToMac(str) {
if (str.length === 0)
return new Uint8Array()
let arr = str.split(":")
let hexArr = arr.join("")
return new Uint8Array(hexArr.match(/[\da-f]{2}/gi).map(bb => {
return parseInt(bb, 16)
})).buffer
}
// 转为ByteArray并以\0结尾
function strToByteArray(data) {
if (typeof data === 'string') {
var arr = []
for (var i = 0; i < data.length; ++i) {
let charcode = data.charCodeAt(i)
if (charcode < 0x80) {
arr.push(charcode)
} else if (charcode < 0x800) {
arr.push(0xc0 | (charcode >> 6), 0x80 | (charcode & 0x3f))
} else if (charcode < 0xd800 || charcode >= 0xe000) {
arr.push(0xe0 | (charcode >> 12), 0x80 | ((charcode >> 6) & 0x3f), 0x80 | (charcode & 0x3f))
} else {
// Handle surrogate pairs (U+10000 to U+10FFFF)
i++
charcode = 0x10000 + (((charcode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff))
arr.push(0xf0 | (charcode >> 18), 0x80 | ((charcode >> 12) & 0x3f), 0x80 | ((charcode >> 6) & 0x3f), 0x80 | (charcode & 0x3f))
}
}
if (arr[arr.length - 1] !== 0) {
arr.push(0)
}
return new Uint8Array(arr).buffer
} else if (data instanceof ArrayBuffer) {
const uint8Array = new Uint8Array(data)
if (uint8Array[uint8Array.length - 1] !== 0) {
const newUint8Array = new Uint8Array(uint8Array.length + 1)
newUint8Array.set(uint8Array)
newUint8Array[newUint8Array.length - 1] = 0
return newUint8Array.buffer
} else {
return data
}
}
return undefined
}
11 changes: 10 additions & 1 deletion dcc-network/qml/PageDSLSettings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ DccObject {
property var config: null
property var item: null
property int type: NetType.WiredItem
property bool modified: false
signal finished
readonly property string parentUrl: parentName + "/" + name

Expand All @@ -34,27 +35,32 @@ DccObject {
id: sectionGeneric
parentName: root.parentUrl + "/body"
weight: 100
onEditClicked: modified = true
}
SectionPPPOE {
id: sectionPPPOE
parentName: root.parentUrl + "/body"
weight: 200
onEditClicked: modified = true
}
SectionIPv4 {
id: sectionIPv4
parentName: root.parentUrl + "/body"
weight: 300
onEditClicked: modified = true
}
SectionDNS {
id: sectionDNS
parentName: root.parentUrl + "/body"
weight: 400
onEditClicked: modified = true
}
SectionDevice {
id: sectionDevice
parentName: root.parentUrl + "/body"
weight: 500
canNotBind: false
onEditClicked: modified = true
onSsidChanged: {
if (root.type === NetType.WirelessItem && !root.config[root.config.connection.type].hasOwnProperty("id")) {
sectionGeneric.settingsID = sectionDevice.ssid
Expand All @@ -65,6 +71,7 @@ DccObject {
id: sectionPPP
parentName: root.parentUrl + "/body"
weight: 600
onEditClicked: modified = true
}
}
onConfigChanged: {
Expand All @@ -74,6 +81,7 @@ DccObject {
sectionDNS.setConfig((config.hasOwnProperty("ipv4") && config.ipv4.hasOwnProperty("dns")) ? config.ipv4.dns : null)
sectionDevice.setConfig(config["802-3-ethernet"])
sectionPPP.setConfig(config["ppp"])
modified = config.connection.uuid === "{00000000-0000-0000-0000-000000000000}"
}

DccObject {
Expand Down Expand Up @@ -174,6 +182,7 @@ DccObject {
name: "save"
parentName: root.parentUrl + "/footer"
weight: 40
enabled: root.modified
pageType: DccObject.Item
page: NetButton {
text: qsTr("Save")
Expand Down Expand Up @@ -202,7 +211,7 @@ DccObject {
} else {
dccData.exec(NetManager.SetConnectInfo, "", nConfig)
}

root.modified = false
root.finished()
}
}
Expand Down
Loading

0 comments on commit d5defe8

Please sign in to comment.