Skip to content

Commit

Permalink
[nemo-settings-system] Cleanup VPN automaticUpDown. Contributes to JB…
Browse files Browse the repository at this point in the history
…#41460
  • Loading branch information
rainemak committed Apr 11, 2018
1 parent 4ce4dfb commit 09ed943
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 145 deletions.
119 changes: 2 additions & 117 deletions src/vpnmodel.cpp
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
});
Expand Down Expand Up @@ -521,26 +446,21 @@ 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());
if (domain.isEmpty()) {
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());

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();

Expand All @@ -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());
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 0 additions & 28 deletions src/vpnmodel.h
Expand Up @@ -74,8 +74,6 @@ class SYSTEMSETTINGS_EXPORT VpnModel : public ObjectListModel
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);

Expand Down Expand Up @@ -103,25 +101,6 @@ private slots:

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:
Expand All @@ -148,7 +127,6 @@ private slots:
QHash<QString, ConnmanServiceProxy *> vpnServices_;
QMap<QString, VpnConnection*> pendingDisconnects_;
QString pendingConnect_;
TokenFileRepository tokenFiles_;
CredentialsRepository credentials_;
ConnectionState bestState_;
};
Expand All @@ -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)
Expand Down Expand Up @@ -195,9 +172,6 @@ class SYSTEMSETTINGS_EXPORT VpnConnection : public QObject
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); }

Expand Down Expand Up @@ -239,7 +213,6 @@ class SYSTEMSETTINGS_EXPORT VpnConnection : public QObject
void domainChanged();
void networksChanged();
void autoConnectChanged();
void automaticUpDownChanged();
void storeCredentialsChanged();
void immutableChanged();
void indexChanged();
Expand Down Expand Up @@ -269,7 +242,6 @@ class SYSTEMSETTINGS_EXPORT VpnConnection : public QObject
QString domain_;
QString networks_;
bool autoConnect_;
bool automaticUpDown_;
bool storeCredentials_;
bool immutable_;
int index_;
Expand Down

0 comments on commit 09ed943

Please sign in to comment.