mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2025-12-22 16:37:21 -06:00
Simplify functions
This commit is contained in:
@@ -548,10 +548,9 @@ QVector<Utils::Net::Subnet> Preferences::getWebUiAuthSubnetWhitelist() const
|
||||
|
||||
for (const QString &rawSubnet : subnets)
|
||||
{
|
||||
bool ok = false;
|
||||
const Utils::Net::Subnet subnet = Utils::Net::parseSubnet(rawSubnet.trimmed(), &ok);
|
||||
if (ok)
|
||||
ret.append(subnet);
|
||||
const std::optional<Utils::Net::Subnet> subnet = Utils::Net::parseSubnet(rawSubnet.trimmed());
|
||||
if (subnet)
|
||||
ret.append(subnet.value());
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -561,9 +560,7 @@ void Preferences::setWebUiAuthSubnetWhitelist(QStringList subnets)
|
||||
{
|
||||
Algorithm::removeIf(subnets, [](const QString &subnet)
|
||||
{
|
||||
bool ok = false;
|
||||
Utils::Net::parseSubnet(subnet.trimmed(), &ok);
|
||||
return !ok;
|
||||
return !Utils::Net::parseSubnet(subnet.trimmed()).has_value();
|
||||
});
|
||||
|
||||
setValue(u"Preferences/WebUI/AuthSubnetWhitelist"_qs, subnets);
|
||||
|
||||
@@ -47,22 +47,15 @@ namespace Utils
|
||||
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);
|
||||
if (ok)
|
||||
*ok = (subnet != invalid);
|
||||
const Subnet invalid = {QHostAddress(), -1};
|
||||
if (subnet == invalid)
|
||||
return std::nullopt;
|
||||
return subnet;
|
||||
}
|
||||
|
||||
bool canParseSubnet(const QString &subnetStr)
|
||||
{
|
||||
bool ok = false;
|
||||
parseSubnet(subnetStr, &ok);
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool isLoopbackAddress(const QHostAddress &addr)
|
||||
{
|
||||
return (addr == QHostAddress::LocalHost)
|
||||
|
||||
@@ -28,8 +28,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QHostAddress>
|
||||
#include <optional>
|
||||
|
||||
#include <QtContainerFwd>
|
||||
#include <QHostAddress>
|
||||
|
||||
class QSslCertificate;
|
||||
class QSslKey;
|
||||
@@ -37,11 +39,11 @@ class QString;
|
||||
|
||||
namespace Utils::Net
|
||||
{
|
||||
// alias for `QHostAddress::parseSubnet()` return type
|
||||
using Subnet = QPair<QHostAddress, int>;
|
||||
|
||||
bool isValidIP(const QString &ip);
|
||||
Subnet parseSubnet(const QString &subnetStr, bool *ok = nullptr);
|
||||
bool canParseSubnet(const QString &subnetStr);
|
||||
std::optional<Subnet> parseSubnet(const QString &subnetStr);
|
||||
bool isLoopbackAddress(const QHostAddress &addr);
|
||||
bool isIPInRange(const QHostAddress &addr, const QVector<Subnet> &subnets);
|
||||
QString subnetToString(const Subnet &subnet);
|
||||
|
||||
Reference in New Issue
Block a user