Add set ip with new IpSettingsDialog.
This commit is contained in:
@@ -130,12 +130,12 @@ bool NetworkManager::setDHCP()
|
||||
JsonObject::Instance()->autoDHCP(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 ipaddr = NewExp(addr.ip, addr.mask);
|
||||
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;
|
||||
args << "-c" << setIpCommand;
|
||||
settingProcess.start("/bin/sh", args);
|
||||
|
||||
@@ -24,7 +24,7 @@ public:
|
||||
//static void setInterfaceName(const QString& name);
|
||||
static bool checkPassWord(const QString& pwd, QString& err_info);
|
||||
bool setDHCP();
|
||||
bool setIpAddr(const IpAddr& addr, const QString& aGateWay, QString& err_info);
|
||||
bool setIpAddr(const IpAddr& addr, QString& err_info);
|
||||
void initNetworkInfo();
|
||||
static bool setIpAddrList(const QList<QStringList>& list, QString& err_info);
|
||||
static bool setDefaultGateway(const QString& gw, QString& err_info);
|
||||
|
||||
@@ -3,9 +3,12 @@
|
||||
#include "components/ImageSwitch.h"
|
||||
#include "components/ULineEdit.h"
|
||||
#include "utilities/InputFormatValidator.h"
|
||||
#include "device/networkmanager.h"
|
||||
#include "DialogManager.h"
|
||||
|
||||
#include <QLabel>
|
||||
#include <QVBoxLayout>
|
||||
#include <QThread>
|
||||
|
||||
IpSettingsDialog::IpSettingsDialog(QWidget* aParent, Qt::WindowFlags aFlag)
|
||||
: GUIFormBaseDialog(aParent, aFlag)
|
||||
@@ -14,12 +17,20 @@ IpSettingsDialog::IpSettingsDialog(QWidget* aParent, Qt::WindowFlags aFlag)
|
||||
, mIpAddress(new ULineEdit(this))
|
||||
, mSubnetMask(new ULineEdit(this))
|
||||
, mGateway(new ULineEdit(this))
|
||||
, mThread(nullptr)
|
||||
{
|
||||
IpAddr addr = JsonObject::Instance()->getDefaultIpAddr();
|
||||
mDhcpButton->setChecked(addr.dhcp);
|
||||
mIpAddress->setText(addr.ip);
|
||||
mSubnetMask->setText(addr.mask);
|
||||
mGateway->setText(addr.gateway);
|
||||
if(addr.dhcp)
|
||||
{
|
||||
mIpAddress->setEnabled(false);
|
||||
mSubnetMask->setEnabled(false);
|
||||
mGateway->setEnabled(false);
|
||||
}
|
||||
connect(mDhcpButton, &ImageSwitch::clicked, this, &IpSettingsDialog::handleDhcpClicked);
|
||||
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()
|
||||
{
|
||||
if(!InputFormatValidator::ValidateIpAddressFormat(mIpAddress->text()))
|
||||
{
|
||||
mErrorText->show();
|
||||
mErrorText->setText("Ip Address is not valid");
|
||||
mErrorText->setText(tr("Ip Address is not valid"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!InputFormatValidator::ValidateIpAddressFormat(mSubnetMask->text()))
|
||||
{
|
||||
mErrorText->show();
|
||||
mErrorText->setText("Subnet Mask is not valid");
|
||||
mErrorText->setText(tr("Subnet Mask is not valid"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!InputFormatValidator::ValidateIpAddressFormat(mGateway->text()))
|
||||
{
|
||||
mErrorText->show();
|
||||
mErrorText->setText("Gateway is not valid");
|
||||
mErrorText->setText(tr("Gateway is not valid"));
|
||||
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()
|
||||
@@ -97,3 +140,17 @@ void IpSettingsDialog::init()
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -20,12 +20,18 @@ protected:
|
||||
private:
|
||||
void init();
|
||||
|
||||
private slots:
|
||||
void handleDhcpClicked();
|
||||
void handleThreadStart();
|
||||
void handleThreadExit();
|
||||
|
||||
private:
|
||||
QLabel* mErrorText;
|
||||
ImageSwitch* mDhcpButton;
|
||||
ULineEdit* mIpAddress;
|
||||
ULineEdit* mSubnetMask;
|
||||
ULineEdit* mGateway;
|
||||
QThread* mThread;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -58,7 +58,6 @@ void JsonObject::init()
|
||||
mLockScreenTime = QString(getJsonString("general", "lockscreen")).toInt();
|
||||
mStorageAlarmSize = getJsonString("storagepolicy", "mininum");
|
||||
mInterfaceName = QString(getJsonString("address", "device"));
|
||||
mDhcp = QVariant(QString(getJsonString("address", "dhcp"))).toBool();
|
||||
mGateway = QString(getJsonString("routing", "defaultgateway"));
|
||||
mScanConfirm = getBool("general","ScanConfirm");
|
||||
mCompleteNotify = getBool("general","CompleteNotify");
|
||||
@@ -476,14 +475,13 @@ void JsonObject::setInterfaceName(const QString& name)
|
||||
|
||||
bool JsonObject::isDHCP()
|
||||
{
|
||||
return mDhcp;
|
||||
return mDefaultIpAddress.dhcp;
|
||||
}
|
||||
|
||||
void JsonObject::autoDHCP(bool ena)
|
||||
{
|
||||
QString str = QVariant(ena).toString();
|
||||
setJsonString("address", "dhcp", str.toStdString().c_str());
|
||||
mDhcp = ena;
|
||||
setBool("address","Dhcp", ena, true);
|
||||
mDefaultIpAddress.dhcp = ena;
|
||||
}
|
||||
|
||||
IpAddr JsonObject::getDefaultIpAddr()
|
||||
|
||||
@@ -173,7 +173,6 @@ private:
|
||||
host mMppsHost;
|
||||
IpAddr mDefaultIpAddress;
|
||||
|
||||
bool mDhcp;
|
||||
bool mDmsSimulator;
|
||||
bool mScanConfirm;
|
||||
bool mCompleteNotify;
|
||||
|
||||
@@ -323,9 +323,9 @@ void NetworkCfgDialog::applyData()
|
||||
IpAddr deIpAddress;
|
||||
deIpAddress.ip = mUi->addr_ip->text();
|
||||
deIpAddress.mask = mUi->addr_mask->text();
|
||||
QString gateway = mUi->led_gw->text();
|
||||
deIpAddress.gateway = mUi->led_gw->text();
|
||||
mError.append("network settings\t");
|
||||
bool result = NetworkManager::getInstance()->setIpAddr(deIpAddress, gateway, mError);
|
||||
bool result = NetworkManager::getInstance()->setIpAddr(deIpAddress, mError);
|
||||
mError.append(boolToStr(result));
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user