Commit a8c6a12e authored by Raine Makelainen's avatar Raine Makelainen

[settings-vpn] When VPN doesn't provide domain create one. Fixes JB#41638

Do not use fixed defaultDomain for VPNs that do not provide
domain. Due this it was it possible to import same VPN configuration multiple
times and alter their settings.

This also introduces a new default domain naming.
parent 91627230
......@@ -44,7 +44,8 @@
namespace {
const QString defaultDomain(QStringLiteral("merproject.org"));
const auto defaultDomain = QStringLiteral("sailfishos.org");
const auto legacyDefaultDomain(QStringLiteral("merproject.org"));
const auto connmanService = QStringLiteral("net.connman");
const auto connmanVpnService = QStringLiteral("net.connman.vpn");
const auto autoConnectKey = QStringLiteral("AutoConnect");
......@@ -405,7 +406,7 @@ void VpnModel::createConnection(const QVariantMap &createProperties)
QVariantMap properties(createProperties);
const QString domain(properties.value(QString("domain")).toString());
if (domain.isEmpty()) {
properties.insert(QString("domain"), QVariant::fromValue(defaultDomain));
properties.insert(QString("domain"), QVariant::fromValue(createDefaultDomain()));
}
QDBusPendingCall call = connmanVpn_.Create(propertiesToDBus(properties));
......@@ -450,7 +451,7 @@ void VpnModel::modifyConnection(const QString &path, const QVariantMap &properti
const QString domain(updatedProperties.value(QString("domain")).toString());
if (domain.isEmpty()) {
updatedProperties.insert(QString("domain"), QVariant::fromValue(defaultDomain));
updatedProperties.insert(QString("domain"), QVariant::fromValue(createDefaultDomain()));
}
const QString location(CredentialsRepository::locationForObjectPath(path));
......@@ -830,7 +831,11 @@ void VpnModel::updateConnection(VpnConnection *conn, const QVariantMap &updatePr
ppit = properties.find(QStringLiteral("domain"));
if (ppit != properties.end()) {
if ((*ppit).value<QString>() == defaultDomain) {
QString domain = (*ppit).value<QString>();
if (isDefaultDomain(domain)) {
// Default domains are dropped from the model data but
// let's track what default domains we have seen.
defaultDomains_.insert(domain);
properties.erase(ppit);
}
}
......@@ -1098,6 +1103,42 @@ QVariantMap VpnModel::processOpenVpnProvisioningFile(QFile &provisioningFile)
return rv;
}
bool VpnModel::domainInUse(const QString &domain) const
{
if (defaultDomains_.contains(domain)) {
return true;
}
const int itemCount(count());
for (int index = 0; index < itemCount; ++index) {
const VpnConnection *connection = get<VpnConnection>(index);
if (connection->domain() == domain) {
return true;
}
}
return false;
}
QString VpnModel::createDefaultDomain() const
{
QString newDomain = defaultDomain;
int index = 1;
while (domainInUse(newDomain)) {
newDomain = defaultDomain + QString(".%1").arg(index);
++index;
}
return newDomain;
}
bool VpnModel::isDefaultDomain(const QString &domain) const
{
if (domain == legacyDefaultDomain)
return true;
static const QRegularExpression domainPattern(QStringLiteral("^%1(\\.\\d+)?$").arg(defaultDomain));
return domainPattern.match(domain).hasMatch();
}
VpnConnection::VpnConnection(const QString &path)
: QObject(0)
......
......@@ -101,6 +101,10 @@ private:
QVariantMap processOpenVpnProvisioningFile(QFile &provisioningFile);
bool domainInUse(const QString &domain) const;
QString createDefaultDomain() const;
bool isDefaultDomain(const QString &domain) const;
class CredentialsRepository
{
public:
......@@ -125,6 +129,7 @@ private:
ConnmanVpnProxy connmanVpn_;
QHash<QString, ConnmanVpnConnectionProxy *> connections_;
QHash<QString, ConnmanServiceProxy *> vpnServices_;
QSet<QString> defaultDomains_;
QMap<QString, VpnConnection*> pendingDisconnects_;
QString pendingConnect_;
CredentialsRepository credentials_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment