0
0
mirror of https://github.com/yse/easy_profiler.git synced 2024-12-28 01:04:41 +08:00

Validating ip address

This commit is contained in:
Victor Zarubkin 2016-09-27 23:45:33 +03:00
parent 66a8f7804c
commit d099aa28bb

View File

@ -134,7 +134,7 @@ EasyMainWindow::EasyMainWindow() : Parent()
QRegExp rx("^0*(2(5[0-5]|[0-4]\\d)|1?\\d{1,2})(\\.0*(2(5[0-5]|[0-4]\\d)|1?\\d{1,2})){3}$"); QRegExp rx("^0*(2(5[0-5]|[0-4]\\d)|1?\\d{1,2})(\\.0*(2(5[0-5]|[0-4]\\d)|1?\\d{1,2})){3}$");
m_ipEdit->setValidator(new QRegExpValidator(rx, m_ipEdit)); m_ipEdit->setValidator(new QRegExpValidator(rx, m_ipEdit));
m_ipEdit->setText("127.0.0.1"); m_ipEdit->setText("127.0.0.1");
m_ipEdit->setFixedWidth(m_ipEdit->fontMetrics().width(QString("255.255.255.255")) + 10); m_ipEdit->setFixedWidth(m_ipEdit->fontMetrics().width(QString("255.255.255.255")) + 20);
toolbar->addWidget(m_ipEdit); toolbar->addWidget(m_ipEdit);
toolbar->addWidget(new QLabel(" Port:")); toolbar->addWidget(new QLabel(" Port:"));
@ -144,6 +144,9 @@ EasyMainWindow::EasyMainWindow() : Parent()
m_portEdit->setFixedWidth(m_portEdit->fontMetrics().width(QString("000000")) + 10); m_portEdit->setFixedWidth(m_portEdit->fontMetrics().width(QString("000000")) + 10);
toolbar->addWidget(m_portEdit); toolbar->addWidget(m_portEdit);
connect(m_ipEdit, &QLineEdit::returnPressed, [this](){ onConnectClicked(true); });
connect(m_portEdit, &QLineEdit::returnPressed, [this](){ onConnectClicked(true); });
loadSettings(); loadSettings();
if (!m_lastAddress.isEmpty()) if (!m_lastAddress.isEmpty())
@ -850,14 +853,31 @@ void EasyMainWindow::onConnectClicked(bool)
return; return;
auto text = m_ipEdit->text(); auto text = m_ipEdit->text();
if (text.split(QChar('.')).size() != 4) auto parts = text.split(QChar('.'));
if (parts.size() != 4)
{ {
QMessageBox::warning(this, "Warning", "Invalid IP-Address", QMessageBox::Close); QMessageBox::warning(this, "Warning", "Invalid IP-Address", QMessageBox::Close);
return; return;
} }
m_lastAddress = text; for (auto& part : parts)
{
int i = 0;
for (; i < part.size(); ++i)
{
if (part[i] != QChar('0'))
break;
}
if (i < part.size())
part = part.mid(i);
else
part = "0";
}
m_lastAddress = parts.join(QChar('.'));
m_lastPort = m_portEdit->text().toUShort(); m_lastPort = m_portEdit->text().toUShort();
m_ipEdit->setText(m_lastAddress);
if (!m_listener.connect(m_lastAddress.toStdString().c_str(), m_lastPort)) if (!m_listener.connect(m_lastAddress.toStdString().c_str(), m_lastPort))
{ {
QMessageBox::warning(this, "Warning", "Cannot connect with application", QMessageBox::Close); QMessageBox::warning(this, "Warning", "Cannot connect with application", QMessageBox::Close);