Add set ip with new IpSettingsDialog.

This commit is contained in:
sunwen
2024-05-08 15:20:28 +08:00
parent eb041549aa
commit 638a40343f
7 changed files with 75 additions and 15 deletions

View File

@@ -130,12 +130,12 @@ bool NetworkManager::setDHCP()
JsonObject::Instance()->autoDHCP(true); JsonObject::Instance()->autoDHCP(true);
return true; return true;
} }
bool NetworkManager::setIpAddr(const IpAddr& addr, const QString& aGateWay, QString& err_info) bool NetworkManager::setIpAddr(const IpAddr& addr, QString& err_info)
{ {
QString pwd = JsonObject::Instance()->passWord(); QString pwd = JsonObject::Instance()->passWord();
QString ipaddr = NewExp(addr.ip, addr.mask); QString ipaddr = NewExp(addr.ip, addr.mask);
QProcess settingProcess; QProcess settingProcess;
QString setIpCommand = QString("echo %1 | sudo -S nmcli con mod usct ipv4.method manual ipv4.address %2 ipv4.gateway %3").arg(pwd).arg(ipaddr).arg(aGateWay); QString setIpCommand = QString("echo %1 | sudo -S nmcli con mod usct ipv4.method manual ipv4.address %2 ipv4.gateway %3").arg(pwd).arg(ipaddr).arg(addr.gateway);
QStringList args; QStringList args;
args << "-c" << setIpCommand; args << "-c" << setIpCommand;
settingProcess.start("/bin/sh", args); settingProcess.start("/bin/sh", args);

View File

@@ -24,7 +24,7 @@ public:
//static void setInterfaceName(const QString& name); //static void setInterfaceName(const QString& name);
static bool checkPassWord(const QString& pwd, QString& err_info); static bool checkPassWord(const QString& pwd, QString& err_info);
bool setDHCP(); bool setDHCP();
bool setIpAddr(const IpAddr& addr, const QString& aGateWay, QString& err_info); bool setIpAddr(const IpAddr& addr, QString& err_info);
void initNetworkInfo(); void initNetworkInfo();
static bool setIpAddrList(const QList<QStringList>& list, QString& err_info); static bool setIpAddrList(const QList<QStringList>& list, QString& err_info);
static bool setDefaultGateway(const QString& gw, QString& err_info); static bool setDefaultGateway(const QString& gw, QString& err_info);

View File

@@ -3,9 +3,12 @@
#include "components/ImageSwitch.h" #include "components/ImageSwitch.h"
#include "components/ULineEdit.h" #include "components/ULineEdit.h"
#include "utilities/InputFormatValidator.h" #include "utilities/InputFormatValidator.h"
#include "device/networkmanager.h"
#include "DialogManager.h"
#include <QLabel> #include <QLabel>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QThread>
IpSettingsDialog::IpSettingsDialog(QWidget* aParent, Qt::WindowFlags aFlag) IpSettingsDialog::IpSettingsDialog(QWidget* aParent, Qt::WindowFlags aFlag)
: GUIFormBaseDialog(aParent, aFlag) : GUIFormBaseDialog(aParent, aFlag)
@@ -14,12 +17,20 @@ IpSettingsDialog::IpSettingsDialog(QWidget* aParent, Qt::WindowFlags aFlag)
, mIpAddress(new ULineEdit(this)) , mIpAddress(new ULineEdit(this))
, mSubnetMask(new ULineEdit(this)) , mSubnetMask(new ULineEdit(this))
, mGateway(new ULineEdit(this)) , mGateway(new ULineEdit(this))
, mThread(nullptr)
{ {
IpAddr addr = JsonObject::Instance()->getDefaultIpAddr(); IpAddr addr = JsonObject::Instance()->getDefaultIpAddr();
mDhcpButton->setChecked(addr.dhcp); mDhcpButton->setChecked(addr.dhcp);
mIpAddress->setText(addr.ip); mIpAddress->setText(addr.ip);
mSubnetMask->setText(addr.mask); mSubnetMask->setText(addr.mask);
mGateway->setText(addr.gateway); mGateway->setText(addr.gateway);
if(addr.dhcp)
{
mIpAddress->setEnabled(false);
mSubnetMask->setEnabled(false);
mGateway->setEnabled(false);
}
connect(mDhcpButton, &ImageSwitch::clicked, this, &IpSettingsDialog::handleDhcpClicked);
init(); init();
} }
@@ -28,29 +39,61 @@ IpSettingsDialog::~IpSettingsDialog()
} }
void IpSettingsDialog::handleDhcpClicked()
{
bool isChecked = mDhcpButton->getChecked();
mIpAddress->setEnabled(!isChecked);
mSubnetMask->setEnabled(!isChecked);
mGateway->setEnabled(!isChecked);
}
bool IpSettingsDialog::updateReferenceData() bool IpSettingsDialog::updateReferenceData()
{ {
if(!InputFormatValidator::ValidateIpAddressFormat(mIpAddress->text())) if(!InputFormatValidator::ValidateIpAddressFormat(mIpAddress->text()))
{ {
mErrorText->show(); mErrorText->show();
mErrorText->setText("Ip Address is not valid"); mErrorText->setText(tr("Ip Address is not valid"));
return false; return false;
} }
if(!InputFormatValidator::ValidateIpAddressFormat(mSubnetMask->text())) if(!InputFormatValidator::ValidateIpAddressFormat(mSubnetMask->text()))
{ {
mErrorText->show(); mErrorText->show();
mErrorText->setText("Subnet Mask is not valid"); mErrorText->setText(tr("Subnet Mask is not valid"));
return false; return false;
} }
if(!InputFormatValidator::ValidateIpAddressFormat(mGateway->text())) if(!InputFormatValidator::ValidateIpAddressFormat(mGateway->text()))
{ {
mErrorText->show(); mErrorText->show();
mErrorText->setText("Gateway is not valid"); mErrorText->setText(tr("Gateway is not valid"));
return false; return false;
} }
return true;
IpAddr ip;
ip.ip = mIpAddress->text();
ip.dhcp = mDhcpButton->getChecked();
ip.mask = mSubnetMask->text();
ip.gateway = mGateway->text();
JsonObject::Instance()->setDefaultIpAddr(ip);
mThread = QThread::create([ip]()
{
if (ip.dhcp)
{
bool result = NetworkManager::getInstance()->setDHCP();
}
else
{
QString error;
bool result = NetworkManager::getInstance()->setIpAddr(ip, error);
}
});
connect(mThread, &QThread::started, this, &IpSettingsDialog::handleThreadStart);
connect(mThread, &QThread::finished, this, &IpSettingsDialog::handleThreadExit);
mThread->start();
return false;
} }
void IpSettingsDialog::init() void IpSettingsDialog::init()
@@ -97,3 +140,17 @@ void IpSettingsDialog::init()
mErrorText->hide(); mErrorText->hide();
} }
void IpSettingsDialog::handleThreadStart()
{
DialogManager::Default()->raiseSyncDialog("Saving Network Configuration...");
}
void IpSettingsDialog::handleThreadExit()
{
DialogManager::Default()->hideTopSyncDialog();
NetworkManager::getInstance()->initNetworkInfo();
disconnect(mThread, &QThread::started, this, &IpSettingsDialog::handleThreadStart);
disconnect(mThread, &QThread::finished, this, &IpSettingsDialog::handleThreadExit);
mThread->deleteLater();
accept();
}

View File

@@ -20,12 +20,18 @@ protected:
private: private:
void init(); void init();
private slots:
void handleDhcpClicked();
void handleThreadStart();
void handleThreadExit();
private: private:
QLabel* mErrorText; QLabel* mErrorText;
ImageSwitch* mDhcpButton; ImageSwitch* mDhcpButton;
ULineEdit* mIpAddress; ULineEdit* mIpAddress;
ULineEdit* mSubnetMask; ULineEdit* mSubnetMask;
ULineEdit* mGateway; ULineEdit* mGateway;
QThread* mThread;
}; };

View File

@@ -58,7 +58,6 @@ void JsonObject::init()
mLockScreenTime = QString(getJsonString("general", "lockscreen")).toInt(); mLockScreenTime = QString(getJsonString("general", "lockscreen")).toInt();
mStorageAlarmSize = getJsonString("storagepolicy", "mininum"); mStorageAlarmSize = getJsonString("storagepolicy", "mininum");
mInterfaceName = QString(getJsonString("address", "device")); mInterfaceName = QString(getJsonString("address", "device"));
mDhcp = QVariant(QString(getJsonString("address", "dhcp"))).toBool();
mGateway = QString(getJsonString("routing", "defaultgateway")); mGateway = QString(getJsonString("routing", "defaultgateway"));
mScanConfirm = getBool("general","ScanConfirm"); mScanConfirm = getBool("general","ScanConfirm");
mCompleteNotify = getBool("general","CompleteNotify"); mCompleteNotify = getBool("general","CompleteNotify");
@@ -476,14 +475,13 @@ void JsonObject::setInterfaceName(const QString& name)
bool JsonObject::isDHCP() bool JsonObject::isDHCP()
{ {
return mDhcp; return mDefaultIpAddress.dhcp;
} }
void JsonObject::autoDHCP(bool ena) void JsonObject::autoDHCP(bool ena)
{ {
QString str = QVariant(ena).toString(); setBool("address","Dhcp", ena, true);
setJsonString("address", "dhcp", str.toStdString().c_str()); mDefaultIpAddress.dhcp = ena;
mDhcp = ena;
} }
IpAddr JsonObject::getDefaultIpAddr() IpAddr JsonObject::getDefaultIpAddr()

View File

@@ -173,7 +173,6 @@ private:
host mMppsHost; host mMppsHost;
IpAddr mDefaultIpAddress; IpAddr mDefaultIpAddress;
bool mDhcp;
bool mDmsSimulator; bool mDmsSimulator;
bool mScanConfirm; bool mScanConfirm;
bool mCompleteNotify; bool mCompleteNotify;

View File

@@ -323,9 +323,9 @@ void NetworkCfgDialog::applyData()
IpAddr deIpAddress; IpAddr deIpAddress;
deIpAddress.ip = mUi->addr_ip->text(); deIpAddress.ip = mUi->addr_ip->text();
deIpAddress.mask = mUi->addr_mask->text(); deIpAddress.mask = mUi->addr_mask->text();
QString gateway = mUi->led_gw->text(); deIpAddress.gateway = mUi->led_gw->text();
mError.append("network settings\t"); mError.append("network settings\t");
bool result = NetworkManager::getInstance()->setIpAddr(deIpAddress, gateway, mError); bool result = NetworkManager::getInstance()->setIpAddr(deIpAddress, mError);
mError.append(boolToStr(result)); mError.append(boolToStr(result));
} }
}); });