4#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
5#include <QRegExpValidator>
8#include <QRegularExpression>
9#include <QRegularExpressionValidator>
12#include <QNetworkInterface>
13#include <QLoggingCategory>
14#include "ParameterWakeOnLanUI.h"
15#include "ui_ParameterWakeOnLanUI.h"
17static Q_LOGGING_CATEGORY(log,
"WOL.Parameter.UI")
19#define RegularIp "^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$"
20#define RegularMac "^[A-Fa-f0-9]{2}(-[A-Fa-f0-9]{2}){5}$|^[A-Fa-f0-9]{2}(:[A-Fa-f0-9]{2}){5}$"
21#define RegularPassword "(.{6})?|^[A-Fa-f0-9]{2}(-[A-Fa-f0-9]{2}){5}$|^[A-Fa-f0-9]{2}(:[A-Fa-f0-9]{2}){5}$"
22CParameterWakeOnLanUI::CParameterWakeOnLanUI(QWidget *parent)
27#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
28 QRegExp rxMac(RegularMac);
29 QRegExpValidator* macValidator =
new QRegExpValidator(rxMac,
this);
30 QRegExp rxIP(RegularIp);
31 QRegExpValidator* ipValidator =
new QRegExpValidator(rxIP,
this);
32 QRegExp rxPassword(RegularPassword);
33 QRegExpValidator* passwordValidator =
new QRegExpValidator(rxPassword,
this);
35 QRegularExpression rxMac(RegularMac);
36 QRegularExpressionValidator* macValidator =
new QRegularExpressionValidator(rxMac,
this);
37 QRegularExpression rxIP(RegularIp);
38 QRegularExpressionValidator* ipValidator =
new QRegularExpressionValidator(rxIP,
this);
39 QRegularExpression rxPassword(RegularPassword);
40 QRegularExpressionValidator* passwordValidator =
new QRegularExpressionValidator(rxPassword,
this);
42 ui->leIP->setValidator(ipValidator);
43 ui->leMac->setValidator(macValidator);
44 ui->leMac->setPlaceholderText(
"FF:FF:FF:FF:FF:FF");
45 ui->lePassword->setValidator(passwordValidator);
47 ui->leBroadcastAddress->setValidator(ipValidator);
48#if QT_VERSION > QT_VERSION_CHECK(6, 0, 0)
49 ui->leBroadcastAddress->setPlaceholderText(
"255.255.255.255");
52 foreach(
auto iface, QNetworkInterface::allInterfaces()) {
54 auto entry = iface.addressEntries();
55 if(iface.flags() & QNetworkInterface::IsLoopBack)
57 if(!(iface.flags() & QNetworkInterface::CanBroadcast))
59 foreach(
auto e, entry) {
60 if(!e.broadcast().isNull()) {
61 ui->cbNetworkInterface->addItem(
63 QStringList() << e.broadcast().toString()
64 << e.netmask().toString());
70CParameterWakeOnLanUI::~CParameterWakeOnLanUI()
77 qDebug(log) << Q_FUNC_INFO;
78 m_pWakeOnLan = qobject_cast<CParameterWakeOnLan*>(pParameter);
79 if(!m_pWakeOnLan)
return -1;
81 ui->leIP->setText(m_pWakeOnLan->m_Net.GetHost());
82 ui->gbWakeOnLan->setChecked(m_pWakeOnLan->GetEnable());
83 ui->leMac->setText(m_pWakeOnLan->GetMac());
84 ui->leBroadcastAddress->setText(m_pWakeOnLan->GetBroadcastAddress());
86 if(m_pWakeOnLan->GetBroadcastAddress().isEmpty()
87 && ui->cbNetworkInterface->count() > 0)
88 on_cbNetworkInterface_currentIndexChanged(
89 ui->cbNetworkInterface->currentIndex());
90 ui->sbPort->setValue(m_pWakeOnLan->GetPort());
91 ui->lePassword->setText(m_pWakeOnLan->GetPassword());
92 ui->pbSave->setChecked(m_pWakeOnLan->GetSavePassword());
95 ui->sbRepeat->setValue(m_pWakeOnLan->GetRepeat());
96 ui->sbInterval->setValue(m_pWakeOnLan->
GetInterval());
97 ui->sbTimeOut->setValue(m_pWakeOnLan->
GetTimeOut());
101bool CParameterWakeOnLanUI::CheckValidity(
bool validity)
103 if(!validity)
return true;
105#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
106 QRegExp rxMac(RegularMac);
107 QRegExpValidator macValidator(rxMac,
this);
108 QRegExp rxIP(RegularIp);
109 QRegExpValidator ipValidator(rxIP,
this);
110 QRegExp rxPassword(RegularPassword);
111 QRegExpValidator passwordValidator(rxPassword,
this);
113 QRegularExpression rxMac(RegularMac);
114 QRegularExpressionValidator macValidator(rxMac,
this);
115 QRegularExpression rxIP(RegularIp);
116 QRegularExpressionValidator ipValidator(rxIP,
this);
117 QRegularExpression rxPassword(RegularPassword);
118 QRegularExpressionValidator passwordValidator(rxPassword,
this);
122 QString szIp = ui->leIP->text();
123 if(QValidator::Acceptable != ipValidator.validate(szIp, pos))
125 QMessageBox::critical(
this, tr(
"Error"),
126 tr(
"The ip address is error"));
127 qCritical(log) <<
"The ip address is error";
128 ui->leIP->setFocus();
131 QString szBroadAddress = ui->leBroadcastAddress->text();
132 if(QValidator::Acceptable != ipValidator.validate(szBroadAddress, pos))
134 QMessageBox::critical(
this, tr(
"Error"),
135 tr(
"The broadcast address is error"));
136 qCritical(log) <<
"The broadcast address is error";
137 ui->leBroadcastAddress->setFocus();
141 QString szPassword = ui->lePassword->text();
142 if(QValidator::Acceptable != passwordValidator.validate(szPassword, pos))
144 QMessageBox::critical(
this, tr(
"Error"),
145 tr(
"The password is error."
146 " A password string of length 6. "
147 "or Hexadecimal representation of 6 bytes"));
148 qCritical(log) <<
"The password is error";
149 ui->lePassword->setFocus();
153 QString szMac = ui->leMac->text();
154 if(QValidator::Acceptable != macValidator.validate(szMac, pos)) {
155 QMessageBox::critical(
this, tr(
"Error"),
156 tr(
"The mac address is error"));
157 qCritical(log) <<
"The mac address is error";
158 ui->leMac->setFocus();
166 if(!m_pWakeOnLan)
return -1;
168 m_pWakeOnLan->m_Net.SetHost(ui->leIP->text());
169 m_pWakeOnLan->SetEnable(ui->gbWakeOnLan->isChecked());
170 m_pWakeOnLan->SetMac(ui->leMac->text());
171 m_pWakeOnLan->SetNetworkInterface(ui->cbNetworkInterface->currentText());
172 m_pWakeOnLan->SetBroadcastAddress(ui->leBroadcastAddress->text());
173 m_pWakeOnLan->SetPort(ui->sbPort->value());
174 m_pWakeOnLan->SetPassword(ui->lePassword->text());
176 m_pWakeOnLan->SetRepeat(ui->sbRepeat->value());
177 m_pWakeOnLan->
SetInterval(ui->sbInterval->value());
178 m_pWakeOnLan->
SetTimeOut(ui->sbTimeOut->value());
183void CParameterWakeOnLanUI::on_pbShow_clicked()
185 switch(ui->lePassword->echoMode())
187 case QLineEdit::Password:
188 ui->lePassword->setEchoMode(QLineEdit::Normal);
189 ui->pbShow->setIcon(QIcon::fromTheme(
"eye-off"));
191 case QLineEdit::Normal:
192 ui->lePassword->setEchoMode(QLineEdit::Password);
193 ui->pbShow->setIcon(QIcon::fromTheme(
"eye-on"));
196 ui->pbShow->setIcon(QIcon::fromTheme(
"eye-on"));
200void CParameterWakeOnLanUI::on_pbSave_clicked()
203 if(ui->pbSave->isChecked())
205 ui->lePassword->setEnabled(
true);
206 szText = tr(
"A password string of length 6."
207 " or Hexadecimal representation of 6 bytes");
208 ui->lePassword->setToolTip(szText);
209 ui->lePassword->setStatusTip(szText);
210 ui->lePassword->setPlaceholderText(szText);
212 szText = tr(
"Please checked save password to enable");
213 ui->lePassword->setToolTip(szText);
214 ui->lePassword->setStatusTip(szText);
215 ui->lePassword->setPlaceholderText(szText);
216 ui->lePassword->setEnabled(
false);
220void CParameterWakeOnLanUI::on_cbNetworkInterface_currentIndexChanged(
int index)
222 qDebug(log) << Q_FUNC_INFO << index;
223 ui->leBroadcastAddress->setText(ui->cbNetworkInterface->itemData(index).toStringList().at(0));
226void CParameterWakeOnLanUI::slotHostChanged(
const QString& szHost)
228 QStringList lstMask = ui->cbNetworkInterface->currentData().toStringList();
229 if(lstMask.length() != 2)
231 QString szMask = lstMask[1];
232 QString szIP = ui->cbNetworkInterface->currentText();
233 if(szMask.isEmpty()) {
234 qCritical(log) <<
"The mask is empty";
237 if(GetSubNet(szHost, szMask) == GetSubNet(szIP, szMask))
239 for(
int i = 0; i < ui->cbNetworkInterface->count(); i++) {
240 szIP = ui->cbNetworkInterface->itemText(i);
241 lstMask = ui->cbNetworkInterface->itemData(i).toStringList();
242 if(lstMask.length() != 2)
continue;
244 if(GetSubNet(szIP, szMask) == GetSubNet(szHost, szMask))
246 ui->cbNetworkInterface->setCurrentIndex(i);
254typedef unsigned long in_addr_t;
256#include <arpa/inet.h>
259QString CParameterWakeOnLanUI::GetSubNet(
const QString& szIP,
const QString& szMask)
261 in_addr_t ip = inet_addr(szIP.toStdString().c_str());
262 in_addr_t mask = inet_addr(szMask.toStdString().c_str());
263 in_addr_t subNet = 0;
264 char* pIP = (
char*)&ip;
265 char* pMask = (
char*)&mask;
266 char* pSubNet = (
char*)&subNet;
267 for (
int i = 0; i < 4; i++)
268 pSubNet[i] = pMask[i] & pIP[i];
269 std::string szSubNet = inet_ntoa((
struct in_addr&)subNet);
270 qDebug(log) <<
"SubNet:" << szSubNet.c_str();
271 return szSubNet.c_str();
274void CParameterWakeOnLanUI::on_pbOK_clicked()
276 if(!CheckValidity(
false))
283void CParameterWakeOnLanUI::on_pbCancel_clicked()
288void CParameterWakeOnLanUI::on_leIP_editingFinished()
290 slotHostChanged(ui->leIP->text());
CParameterClient * GetParameterClient()
Get CParameterClient.
The wake on lan parameters UI.
int SetParameter(CParameter *pParameter)
Set the parameters and initialize the user interface.
int Accept()
Accept parameters.
int SetTimeOut(int nTimeout)
Unit: ms.
int SetSavePassword(bool save)
Set save password.
int SetInterval(int nInterval)
Unit: ms.
const QString GetNetworkInterface() const
Get network interface.
const int GetTimeOut() const
Unit: ms.
const int GetInterval() const
Unit: ms.