Skip to content

Commit

Permalink
[nemo-systemsettings] Allow only one active vpn at once. Contributes …
Browse files Browse the repository at this point in the history
…to JB#41460
  • Loading branch information
rainemak committed Apr 4, 2018
1 parent 3db1ea1 commit f44b489
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
76 changes: 63 additions & 13 deletions src/vpnmodel.cpp
Expand Up @@ -432,6 +432,10 @@ VpnModel::VpnModel(QObject *parent)
vpnServices_.erase(vpnServiceIterator);
delete proxy;
}
pendingDisconnects_.remove(path);
if (pendingConnect_ == path) {
pendingConnect_.clear();
}
});

// If connman-vpn restarts, we need to discard and re-read the state
Expand All @@ -444,6 +448,8 @@ VpnModel::VpnModel(QObject *parent)
setPopulated(false);
qDeleteAll(connections_);
qDeleteAll(vpnServices_);
pendingDisconnects_.clear();
pendingConnect_.clear();
});
connect(watcher, &QDBusServiceWatcher::serviceRegistered, this, [this](const QString &) {
fetchVpnList();
Expand Down Expand Up @@ -591,30 +597,54 @@ void VpnModel::deleteConnection(const QString &path)

void VpnModel::activateConnection(const QString &path)
{
auto it = connections_.find(path);
if (it != connections_.end()) {
ConnmanVpnConnectionProxy *proxy(*it);
for (int i = 0, n = count(); i < n; ++i) {
VpnConnection *connection = qobject_cast<VpnConnection *>(get(i));
QString otherPath = connection->path();
if (otherPath != path && !pendingDisconnects_.contains(otherPath) && (connection->state() == VpnModel::Ready ||
connection->state() == VpnModel::Configuration)) {
pendingDisconnects_.insert(otherPath, connection);
deactivateConnection(otherPath);
qCDebug(lcVpnLog) << "Adding pending vpn disconnect" << otherPath << connection->state() << "when connecting to vpn";
}
}

QDBusPendingCall call = proxy->Connect();
qCDebug(lcVpnLog) << "About to connect has pending:" << !pendingDisconnects_.isEmpty() << pendingDisconnects_.keys();

QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, path](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<void> reply = *watcher;
watcher->deleteLater();
if (pendingDisconnects_.isEmpty()) {
auto it = connections_.find(path);
if (it != connections_.end()) {
ConnmanVpnConnectionProxy *proxy(*it);
QDBusPendingCall call = proxy->Connect();
qCDebug(lcVpnLog) << "Connect to vpn" << path;

if (reply.isError()) {
qCWarning(lcVpnLog) << "Unable to activate Connman VPN connection:" << path << ":" << reply.error().message();
}
});
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, path](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<void> reply = *watcher;
watcher->deleteLater();

if (reply.isError()) {
qCWarning(lcVpnLog) << "Unable to activate Connman VPN connection:" << path << ":" << reply.error().message();
}
});
} else {
qCWarning(lcVpnLog) << "Unable to activate VPN connection without proxy:" << path;
}
} else {
qCWarning(lcVpnLog) << "Unable to activate VPN connection without proxy:" << path;
pendingConnect_ = path;
}
}

void VpnModel::deactivateConnection(const QString &path)
{
auto it = connections_.find(path);
if (it != connections_.end()) {
VpnConnection *connection = this->connection(path);
if (connection && !pendingDisconnects_.contains(path) && (connection->state() == VpnModel::Ready ||
connection->state() == VpnModel::Configuration)) {
qCDebug(lcVpnLog) << "Adding pending vpn disconnect" << path << connection->state() << "when disconnecting from a vpn";
pendingDisconnects_.insert(path, connection);
}

ConnmanVpnConnectionProxy *proxy(*it);

QDBusPendingCall call = proxy->Disconnect();
Expand Down Expand Up @@ -810,9 +840,29 @@ VpnConnection *VpnModel::connection(const QString &path) const
return nullptr;
}

void VpnModel::updatePendingDisconnectState()
{
VpnConnection *pendingDisconnect = qobject_cast<VpnConnection *>(sender());
qCDebug(lcVpnLog) << "Pending disconnect state changed" << pendingDisconnect->state() << pendingDisconnect->path() << pendingDisconnect->name();
if (pendingDisconnect->state() == VpnModel::Idle || pendingDisconnect->state() == VpnModel::Failure) {
ConnmanServiceProxy *serviceProxy = vpnServices_.value(pendingDisconnect->path());
serviceProxy->SetProperty(autoConnectKey, QDBusVariant(false));

pendingDisconnects_.remove(pendingDisconnect->path());
qCDebug(lcVpnLog) << "Pending disconnect is idle" << pendingDisconnect->path() << pendingDisconnect->name();

if (pendingDisconnects_.isEmpty() && !pendingConnect_.isEmpty()) {
qCDebug(lcVpnLog) << "Will activate vpn" << pendingConnect_;
activateConnection(pendingConnect_);
pendingConnect_.clear();
}
}
}

VpnConnection *VpnModel::newConnection(const QString &path)
{
VpnConnection *conn = new VpnConnection(path);
connect(conn, &VpnConnection::stateChanged, this, &VpnModel::updatePendingDisconnectState, Qt::UniqueConnection);
appendItem(conn);

// Create a vpn and a connman service proxies for this connection
Expand Down
5 changes: 5 additions & 0 deletions src/vpnmodel.h
Expand Up @@ -92,6 +92,9 @@ class SYSTEMSETTINGS_EXPORT VpnModel : public ObjectListModel
void bestStateChanged();
void connectionStateChanged(const QString &path, int state);

private slots:
void updatePendingDisconnectState();

private:
void fetchVpnList();

Expand Down Expand Up @@ -143,6 +146,8 @@ class SYSTEMSETTINGS_EXPORT VpnModel : public ObjectListModel
ConnmanVpnProxy connmanVpn_;
QHash<QString, ConnmanVpnConnectionProxy *> connections_;
QHash<QString, ConnmanServiceProxy *> vpnServices_;
QMap<QString, VpnConnection*> pendingDisconnects_;
QString pendingConnect_;
TokenFileRepository tokenFiles_;
CredentialsRepository credentials_;
ConnectionState bestState_;
Expand Down

0 comments on commit f44b489

Please sign in to comment.