Commit 09ed9433 authored by Raine Makelainen's avatar Raine Makelainen

[nemo-settings-system] Cleanup VPN automaticUpDown. Contributes to JB#41460

parent 4ce4dfb4
......@@ -189,80 +189,7 @@ int numericValue(VpnModel::ConnectionState state)
return (state == VpnModel::Ready ? 3 : (state == VpnModel::Configuration ? 2 : (state == VpnModel::Failure ? 1 : 0)));
}
}
VpnModel::TokenFileRepository::TokenFileRepository(const QString &path)
: baseDir_(path)
{
if (!baseDir_.exists() && !baseDir_.mkpath(path)) {
qCWarning(lcVpnLog) << "Unable to create base directory for VPN token files:" << path;
} else {
foreach (const QFileInfo &info, baseDir_.entryInfoList()) {
if (info.isFile() && info.size() == 0) {
// This is a token file
tokens_.append(info.fileName());
}
}
}
}
QString VpnModel::TokenFileRepository::tokenForObjectPath(const QString &path)
{
int index = path.lastIndexOf(QChar('/'));
if (index != -1) {
return path.mid(index + 1);
}
return QString();
}
bool VpnModel::TokenFileRepository::tokenExists(const QString &token) const
{
return tokens_.contains(token);
}
void VpnModel::TokenFileRepository::ensureToken(const QString &token)
{
if (!tokens_.contains(token)) {
QFile tokenFile(baseDir_.absoluteFilePath(token));
if (!tokenFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
qCWarning(lcVpnLog) << "Unable to write token file:" << tokenFile.fileName();
} else {
tokenFile.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ReadOther | QFileDevice::WriteOther);
tokenFile.close();
tokens_.append(token);
}
}
}
void VpnModel::TokenFileRepository::removeToken(const QString &token)
{
QStringList::iterator it = std::find(tokens_.begin(), tokens_.end(), token);
if (it != tokens_.end()) {
if (!baseDir_.remove(token)) {
qCWarning(lcVpnLog) << "Unable to delete token file:" << token;
} else {
tokens_.erase(it);
}
}
}
void VpnModel::TokenFileRepository::removeUnknownTokens(const QStringList &knownConnections)
{
for (QStringList::iterator it = tokens_.begin(); it != tokens_.end(); ) {
const QString &token(*it);
if (knownConnections.contains(token)) {
// This token pertains to an extant connection
++it;
} else {
// Remove this token
baseDir_.remove(token);
it = tokens_.erase(it);
}
}
}
} // end anonymous namespace
VpnModel::CredentialsRepository::CredentialsRepository(const QString &path)
: baseDir_(path)
......@@ -387,7 +314,6 @@ QVariantMap VpnModel::CredentialsRepository::decodeCredentials(const QByteArray
VpnModel::VpnModel(QObject *parent)
: ObjectListModel(parent, true, false)
, connmanVpn_(connmanVpnService, "/", QDBusConnection::systemBus(), this)
, tokenFiles_("/home/nemo/.local/share/system/vpn")
, credentials_("/home/nemo/.local/share/system/vpn-data")
, bestState_(VpnModel::Idle)
{
......@@ -403,7 +329,6 @@ VpnModel::VpnModel(QObject *parent)
}
QVariantMap qmlProperties(propertiesToQml(properties));
qmlProperties.insert(QStringLiteral("automaticUpDown"), tokenFiles_.tokenExists(TokenFileRepository::tokenForObjectPath(path)));
qmlProperties.insert(QStringLiteral("storeCredentials"), credentials_.credentialsExist(CredentialsRepository::locationForObjectPath(path)));
updateConnection(conn, qmlProperties);
});
......@@ -521,7 +446,6 @@ void VpnModel::modifyConnection(const QString &path, const QVariantMap &properti
updatedProperties.remove(QString("state"));
updatedProperties.remove(QString("index"));
updatedProperties.remove(QString("immutable"));
updatedProperties.remove(QString("automaticUpDown"));
updatedProperties.remove(QString("storeCredentials"));
const QString domain(updatedProperties.value(QString("domain")).toString());
......@@ -529,10 +453,6 @@ void VpnModel::modifyConnection(const QString &path, const QVariantMap &properti
updatedProperties.insert(QString("domain"), QVariant::fromValue(defaultDomain));
}
const QString token(TokenFileRepository::tokenForObjectPath(path));
const bool wasAutomatic(tokenFiles_.tokenExists(token));
const bool automatic(properties.value(QString("automaticUpDown")).toBool());
const QString location(CredentialsRepository::locationForObjectPath(path));
const bool couldStoreCredentials(credentials_.credentialsExist(location));
const bool canStoreCredentials(properties.value(QString("storeCredentials")).toBool());
......@@ -540,7 +460,7 @@ void VpnModel::modifyConnection(const QString &path, const QVariantMap &properti
QDBusPendingCall call = connmanVpn_.Create(propertiesToDBus(updatedProperties));
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, conn, token, automatic, wasAutomatic, location, canStoreCredentials, couldStoreCredentials](QDBusPendingCallWatcher *watcher) {
connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, conn, location, canStoreCredentials, couldStoreCredentials](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<QDBusObjectPath> reply = *watcher;
watcher->deleteLater();
......@@ -550,14 +470,6 @@ void VpnModel::modifyConnection(const QString &path, const QVariantMap &properti
const QDBusObjectPath &objectPath(reply.value());
qCWarning(lcVpnLog) << "Modified VPN connection:" << objectPath.path();
if (automatic != wasAutomatic) {
if (automatic) {
tokenFiles_.ensureToken(token);
} else {
tokenFiles_.removeToken(token);
}
}
if (canStoreCredentials != couldStoreCredentials) {
if (canStoreCredentials ) {
credentials_.storeCredentials(location, QVariantMap());
......@@ -663,26 +575,6 @@ void VpnModel::deactivateConnection(const QString &path)
}
}
void VpnModel::setAutomaticConnection(const QString &path, bool enabled)
{
if (VpnConnection *conn = connection(path)) {
const QString token(TokenFileRepository::tokenForObjectPath(path));
const bool wasEnabled(tokenFiles_.tokenExists(token));
if (enabled != wasEnabled) {
if (enabled) {
tokenFiles_.ensureToken(token);
} else {
tokenFiles_.removeToken(token);
}
conn->setAutomaticUpDown(enabled);
itemChanged(conn);
}
} else {
qCWarning(lcVpnLog) << "Unable to set automatic connection for unknown VPN connection:" << path;
}
}
QVariantMap VpnModel::connectionCredentials(const QString &path)
{
QVariantMap rv;
......@@ -806,22 +698,16 @@ void VpnModel::fetchVpnList()
} else {
const PathPropertiesArray &connections(reply.value());
QStringList tokens;
for (const PathProperties &connection : connections) {
const QString &path(connection.first.path());
const QVariantMap &properties(connection.second);
QVariantMap qmlProperties(propertiesToQml(properties));
qmlProperties.insert(QStringLiteral("automaticUpDown"), tokenFiles_.tokenExists(TokenFileRepository::tokenForObjectPath(path)));
qmlProperties.insert(QStringLiteral("storeCredentials"), credentials_.credentialsExist(CredentialsRepository::locationForObjectPath(path)));
VpnConnection *conn = newConnection(path);
updateConnection(conn, qmlProperties);
tokens.append(TokenFileRepository::tokenForObjectPath(path));
}
tokenFiles_.removeUnknownTokens(tokens);
}
setPopulated(true);
......@@ -1206,7 +1092,6 @@ VpnConnection::VpnConnection(const QString &path)
, state_(static_cast<int>(VpnModel::Disconnect))
, type_("openvpn")
, autoConnect_(false)
, automaticUpDown_(false)
, storeCredentials_(false)
, immutable_(false)
, index_(-1)
......
......@@ -74,8 +74,6 @@ public:
Q_INVOKABLE void activateConnection(const QString &path);
Q_INVOKABLE void deactivateConnection(const QString &path);
Q_INVOKABLE void setAutomaticConnection(const QString &path, bool enabled);
Q_INVOKABLE QVariantMap connectionCredentials(const QString &path);
Q_INVOKABLE void setConnectionCredentials(const QString &path, const QVariantMap &credentials);
......@@ -103,25 +101,6 @@ private:
QVariantMap processOpenVpnProvisioningFile(QFile &provisioningFile);
class TokenFileRepository
{
public:
TokenFileRepository(const QString &path);
static QString tokenForObjectPath(const QString &path);
bool tokenExists(const QString &token) const;
void ensureToken(const QString &token);
void removeToken(const QString &token);
void removeUnknownTokens(const QStringList &knownConnections);
private:
QDir baseDir_;
QStringList tokens_;
};
class CredentialsRepository
{
public:
......@@ -148,7 +127,6 @@ private:
QHash<QString, ConnmanServiceProxy *> vpnServices_;
QMap<QString, VpnConnection*> pendingDisconnects_;
QString pendingConnect_;
TokenFileRepository tokenFiles_;
CredentialsRepository credentials_;
ConnectionState bestState_;
};
......@@ -162,7 +140,6 @@ class SYSTEMSETTINGS_EXPORT VpnConnection : public QObject
Q_PROPERTY(QString domain READ domain WRITE setDomain NOTIFY domainChanged)
Q_PROPERTY(QString networks READ networks WRITE setNetworks NOTIFY networksChanged)
Q_PROPERTY(bool autoConnect READ autoConnect WRITE setAutoConnect NOTIFY autoConnectChanged)
Q_PROPERTY(bool automaticUpDown READ automaticUpDown WRITE setAutomaticUpDown NOTIFY automaticUpDownChanged)
Q_PROPERTY(bool storeCredentials READ storeCredentials WRITE setStoreCredentials NOTIFY storeCredentialsChanged)
Q_PROPERTY(int state READ state WRITE setState NOTIFY stateChanged)
Q_PROPERTY(QString type READ type WRITE setType NOTIFY typeChanged)
......@@ -195,9 +172,6 @@ public:
bool autoConnect() const { return autoConnect_; }
void setAutoConnect(bool autoConnect) { updateMember(&VpnConnection::autoConnect_, autoConnect, &VpnConnection::autoConnectChanged); }
bool automaticUpDown() const { return automaticUpDown_; }
void setAutomaticUpDown(bool automaticUpDown) { updateMember(&VpnConnection::automaticUpDown_, automaticUpDown, &VpnConnection::automaticUpDownChanged); }
bool storeCredentials() const { return storeCredentials_; }
void setStoreCredentials(bool storeCredentials) { updateMember(&VpnConnection::storeCredentials_, storeCredentials, &VpnConnection::storeCredentialsChanged); }
......@@ -239,7 +213,6 @@ signals:
void domainChanged();
void networksChanged();
void autoConnectChanged();
void automaticUpDownChanged();
void storeCredentialsChanged();
void immutableChanged();
void indexChanged();
......@@ -269,7 +242,6 @@ private:
QString domain_;
QString networks_;
bool autoConnect_;
bool automaticUpDown_;
bool storeCredentials_;
bool immutable_;
int index_;
......
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