mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-22 20:44:15 +00:00
Simplify functions
This commit is contained in:
parent
987e1b544a
commit
4094a4c448
@ -548,10 +548,9 @@ QVector<Utils::Net::Subnet> Preferences::getWebUiAuthSubnetWhitelist() const
|
|||||||
|
|
||||||
for (const QString &rawSubnet : subnets)
|
for (const QString &rawSubnet : subnets)
|
||||||
{
|
{
|
||||||
bool ok = false;
|
const std::optional<Utils::Net::Subnet> subnet = Utils::Net::parseSubnet(rawSubnet.trimmed());
|
||||||
const Utils::Net::Subnet subnet = Utils::Net::parseSubnet(rawSubnet.trimmed(), &ok);
|
if (subnet)
|
||||||
if (ok)
|
ret.append(subnet.value());
|
||||||
ret.append(subnet);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@ -561,9 +560,7 @@ void Preferences::setWebUiAuthSubnetWhitelist(QStringList subnets)
|
|||||||
{
|
{
|
||||||
Algorithm::removeIf(subnets, [](const QString &subnet)
|
Algorithm::removeIf(subnets, [](const QString &subnet)
|
||||||
{
|
{
|
||||||
bool ok = false;
|
return !Utils::Net::parseSubnet(subnet.trimmed()).has_value();
|
||||||
Utils::Net::parseSubnet(subnet.trimmed(), &ok);
|
|
||||||
return !ok;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
setValue(u"Preferences/WebUI/AuthSubnetWhitelist"_qs, subnets);
|
setValue(u"Preferences/WebUI/AuthSubnetWhitelist"_qs, subnets);
|
||||||
|
@ -47,22 +47,15 @@ namespace Utils
|
|||||||
return !QHostAddress(ip).isNull();
|
return !QHostAddress(ip).isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
Subnet parseSubnet(const QString &subnetStr, bool *ok)
|
std::optional<Subnet> parseSubnet(const QString &subnetStr)
|
||||||
{
|
{
|
||||||
const Subnet invalid = qMakePair(QHostAddress(), -1);
|
|
||||||
const Subnet subnet = QHostAddress::parseSubnet(subnetStr);
|
const Subnet subnet = QHostAddress::parseSubnet(subnetStr);
|
||||||
if (ok)
|
const Subnet invalid = {QHostAddress(), -1};
|
||||||
*ok = (subnet != invalid);
|
if (subnet == invalid)
|
||||||
|
return std::nullopt;
|
||||||
return subnet;
|
return subnet;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool canParseSubnet(const QString &subnetStr)
|
|
||||||
{
|
|
||||||
bool ok = false;
|
|
||||||
parseSubnet(subnetStr, &ok);
|
|
||||||
return ok;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isLoopbackAddress(const QHostAddress &addr)
|
bool isLoopbackAddress(const QHostAddress &addr)
|
||||||
{
|
{
|
||||||
return (addr == QHostAddress::LocalHost)
|
return (addr == QHostAddress::LocalHost)
|
||||||
|
@ -28,8 +28,10 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QHostAddress>
|
#include <optional>
|
||||||
|
|
||||||
#include <QtContainerFwd>
|
#include <QtContainerFwd>
|
||||||
|
#include <QHostAddress>
|
||||||
|
|
||||||
class QSslCertificate;
|
class QSslCertificate;
|
||||||
class QSslKey;
|
class QSslKey;
|
||||||
@ -37,11 +39,11 @@ class QString;
|
|||||||
|
|
||||||
namespace Utils::Net
|
namespace Utils::Net
|
||||||
{
|
{
|
||||||
|
// alias for `QHostAddress::parseSubnet()` return type
|
||||||
using Subnet = QPair<QHostAddress, int>;
|
using Subnet = QPair<QHostAddress, int>;
|
||||||
|
|
||||||
bool isValidIP(const QString &ip);
|
bool isValidIP(const QString &ip);
|
||||||
Subnet parseSubnet(const QString &subnetStr, bool *ok = nullptr);
|
std::optional<Subnet> parseSubnet(const QString &subnetStr);
|
||||||
bool canParseSubnet(const QString &subnetStr);
|
|
||||||
bool isLoopbackAddress(const QHostAddress &addr);
|
bool isLoopbackAddress(const QHostAddress &addr);
|
||||||
bool isIPInRange(const QHostAddress &addr, const QVector<Subnet> &subnets);
|
bool isIPInRange(const QHostAddress &addr, const QVector<Subnet> &subnets);
|
||||||
QString subnetToString(const Subnet &subnet);
|
QString subnetToString(const Subnet &subnet);
|
||||||
|
@ -90,16 +90,15 @@ void IPSubnetWhitelistOptionsDialog::on_buttonBox_accepted()
|
|||||||
|
|
||||||
void IPSubnetWhitelistOptionsDialog::on_buttonWhitelistIPSubnet_clicked()
|
void IPSubnetWhitelistOptionsDialog::on_buttonWhitelistIPSubnet_clicked()
|
||||||
{
|
{
|
||||||
bool ok = false;
|
const std::optional<Utils::Net::Subnet> subnet = Utils::Net::parseSubnet(m_ui->txtIPSubnet->text());
|
||||||
const Utils::Net::Subnet subnet = Utils::Net::parseSubnet(m_ui->txtIPSubnet->text(), &ok);
|
if (!subnet)
|
||||||
if (!ok)
|
|
||||||
{
|
{
|
||||||
QMessageBox::critical(this, tr("Error"), tr("The entered subnet is invalid."));
|
QMessageBox::critical(this, tr("Error"), tr("The entered subnet is invalid."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_model->insertRow(m_model->rowCount());
|
m_model->insertRow(m_model->rowCount());
|
||||||
m_model->setData(m_model->index(m_model->rowCount() - 1, 0), Utils::Net::subnetToString(subnet));
|
m_model->setData(m_model->index(m_model->rowCount() - 1, 0), Utils::Net::subnetToString(subnet.value()));
|
||||||
m_ui->txtIPSubnet->clear();
|
m_ui->txtIPSubnet->clear();
|
||||||
m_modified = true;
|
m_modified = true;
|
||||||
}
|
}
|
||||||
@ -114,5 +113,5 @@ void IPSubnetWhitelistOptionsDialog::on_buttonDeleteIPSubnet_clicked()
|
|||||||
|
|
||||||
void IPSubnetWhitelistOptionsDialog::on_txtIPSubnet_textChanged(const QString &subnetStr)
|
void IPSubnetWhitelistOptionsDialog::on_txtIPSubnet_textChanged(const QString &subnetStr)
|
||||||
{
|
{
|
||||||
m_ui->buttonWhitelistIPSubnet->setEnabled(Utils::Net::canParseSubnet(subnetStr));
|
m_ui->buttonWhitelistIPSubnet->setEnabled(Utils::Net::parseSubnet(subnetStr).has_value());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user