Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
ParameterWakeOnLanUI.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include <QtGlobal>
4#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
5#include <QRegExpValidator>
6#include <QRegExp>
7#else
8#include <QRegularExpression>
9#include <QRegularExpressionValidator>
10#endif
11#include <QMessageBox>
12#include <QNetworkInterface>
13#include <QLoggingCategory>
14#include "ParameterWakeOnLanUI.h"
15#include "ui_ParameterWakeOnLanUI.h"
16
17static Q_LOGGING_CATEGORY(log, "WOL.Parameter.UI")
18
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)
23 : QDialog(parent)
24 , ui(new Ui::CParameterWakeOnLanUI)
25{
26 ui->setupUi(this);
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);
34#else
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);
41#endif
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);
46
47 ui->leBroadcastAddress->setValidator(ipValidator);
48#if QT_VERSION > QT_VERSION_CHECK(6, 0, 0)
49 ui->leBroadcastAddress->setPlaceholderText("255.255.255.255");
50#endif
51
52 foreach(auto iface, QNetworkInterface::allInterfaces()) {
53 qDebug(log) << iface;
54 auto entry = iface.addressEntries();
55 if(iface.flags() & QNetworkInterface::IsLoopBack)
56 continue;
57 if(!(iface.flags() & QNetworkInterface::CanBroadcast))
58 continue;
59 foreach(auto e, entry) {
60 if(!e.broadcast().isNull()) {
61 ui->cbNetworkInterface->addItem(
62 e.ip().toString(),
63 QStringList() << e.broadcast().toString()
64 << e.netmask().toString());
65 }
66 }
67 }
68}
69
70CParameterWakeOnLanUI::~CParameterWakeOnLanUI()
71{
72 delete ui;
73}
74
76{
77 qDebug(log) << Q_FUNC_INFO;
78 m_pWakeOnLan = qobject_cast<CParameterWakeOnLan*>(pParameter);
79 if(!m_pWakeOnLan) return -1;
80
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());
85 ui->cbNetworkInterface->setCurrentText(m_pWakeOnLan->GetNetworkInterface());
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());
93 on_pbSave_clicked();
94 ui->pbShow->setEnabled(m_pWakeOnLan->GetParameterClient()->GetViewPassowrd());
95 ui->sbRepeat->setValue(m_pWakeOnLan->GetRepeat());
96 ui->sbInterval->setValue(m_pWakeOnLan->GetInterval());
97 ui->sbTimeOut->setValue(m_pWakeOnLan->GetTimeOut());
98 return 0;
99}
100
101bool CParameterWakeOnLanUI::CheckValidity(bool validity)
102{
103 if(!validity) return true;
104
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);
112#else
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);
119#endif
120
121 int pos = 0;
122 QString szIp = ui->leIP->text();
123 if(QValidator::Acceptable != ipValidator.validate(szIp, pos))
124 {
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();
129 return false;
130 }
131 QString szBroadAddress = ui->leBroadcastAddress->text();
132 if(QValidator::Acceptable != ipValidator.validate(szBroadAddress, pos))
133 {
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();
138 return false;
139 }
140 pos = 0;
141 QString szPassword = ui->lePassword->text();
142 if(QValidator::Acceptable != passwordValidator.validate(szPassword, pos))
143 {
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();
150 return false;
151 }
152 pos = 0;
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();
159 return false;
160 }
161 return true;
162}
163
165{
166 if(!m_pWakeOnLan) return -1;
167
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());
175 m_pWakeOnLan->SetSavePassword(ui->pbSave->isChecked());
176 m_pWakeOnLan->SetRepeat(ui->sbRepeat->value());
177 m_pWakeOnLan->SetInterval(ui->sbInterval->value());
178 m_pWakeOnLan->SetTimeOut(ui->sbTimeOut->value());
179
180 return 0;
181}
182
183void CParameterWakeOnLanUI::on_pbShow_clicked()
184{
185 switch(ui->lePassword->echoMode())
186 {
187 case QLineEdit::Password:
188 ui->lePassword->setEchoMode(QLineEdit::Normal);
189 ui->pbShow->setIcon(QIcon::fromTheme("eye-off"));
190 break;
191 case QLineEdit::Normal:
192 ui->lePassword->setEchoMode(QLineEdit::Password);
193 ui->pbShow->setIcon(QIcon::fromTheme("eye-on"));
194 break;
195 default:
196 ui->pbShow->setIcon(QIcon::fromTheme("eye-on"));
197 }
198}
199
200void CParameterWakeOnLanUI::on_pbSave_clicked()
201{
202 QString szText;
203 if(ui->pbSave->isChecked())
204 {
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);
211 } else {
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);
217 }
218}
219
220void CParameterWakeOnLanUI::on_cbNetworkInterface_currentIndexChanged(int index)
221{
222 qDebug(log) << Q_FUNC_INFO << index;
223 ui->leBroadcastAddress->setText(ui->cbNetworkInterface->itemData(index).toStringList().at(0));
224}
225
226void CParameterWakeOnLanUI::slotHostChanged(const QString& szHost)
227{
228 QStringList lstMask = ui->cbNetworkInterface->currentData().toStringList();
229 if(lstMask.length() != 2)
230 return;
231 QString szMask = lstMask[1];
232 QString szIP = ui->cbNetworkInterface->currentText();
233 if(szMask.isEmpty()) {
234 qCritical(log) << "The mask is empty";
235 return;
236 }
237 if(GetSubNet(szHost, szMask) == GetSubNet(szIP, szMask))
238 return;
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;
243 szMask = lstMask[1];
244 if(GetSubNet(szIP, szMask) == GetSubNet(szHost, szMask))
245 {
246 ui->cbNetworkInterface->setCurrentIndex(i);
247 return;
248 }
249 }
250}
251
252#ifdef WIN32
253#include <winsock2.h>
254typedef unsigned long in_addr_t;
255#else
256#include <arpa/inet.h>
257#endif
258
259QString CParameterWakeOnLanUI::GetSubNet(const QString& szIP, const QString& szMask)
260{
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();
272}
273
274void CParameterWakeOnLanUI::on_pbOK_clicked()
275{
276 if(!CheckValidity(false))
277 return;
278 int nRet = Accept();
279 if(nRet) return;
280 accept();
281}
282
283void CParameterWakeOnLanUI::on_pbCancel_clicked()
284{
285 reject();
286}
287
288void CParameterWakeOnLanUI::on_leIP_editingFinished()
289{
290 slotHostChanged(ui->leIP->text());
291}
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.
Parameter interface.
Definition Parameter.h:169