Skip to content

Commit

Permalink
[vpnmodel] Cleanup pending connection / disconnect handling. Contribu…
Browse files Browse the repository at this point in the history
…tes to JB#42710
  • Loading branch information
rainemak committed Aug 22, 2018
1 parent 0575c5c commit 462dbe6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 66 deletions.
81 changes: 20 additions & 61 deletions src/vpnmodel.cpp
Expand Up @@ -359,10 +359,6 @@ 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 @@ -375,8 +371,6 @@ 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 @@ -490,9 +484,8 @@ void VpnModel::deleteConnection(const QString &path)
{
if (VpnConnection *conn = connection(path)) {
if (conn->state() == VpnModel::Ready || conn->state() == VpnModel::Configuration) {
auto it = connections_.find(path);
if (it != connections_.end()) {
ConnmanVpnConnectionProxy *proxy(*it);
ConnmanServiceProxy* proxy = vpnServices_.value(path);
if (proxy) {
QDBusPendingCall call = proxy->Disconnect();
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, path](QDBusPendingCallWatcher *watcher) {
Expand Down Expand Up @@ -525,37 +518,33 @@ void VpnModel::activateConnection(const QString &path)
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)) {
if (otherPath != path && (connection->state() == VpnModel::Ready ||
connection->state() == VpnModel::Configuration)) {
deactivateConnection(otherPath);
qCDebug(lcVpnLog) << "Adding pending vpn disconnect" << otherPath << connection->state() << "when connecting to vpn";
}
}

qCDebug(lcVpnLog) << "About to connect has pending disconnects:" << !pendingDisconnects_.isEmpty() << pendingDisconnects_.keys() << "connecting:" << path;
qCDebug(lcVpnLog) << "About to connect path:" << path;

if (pendingDisconnects_.isEmpty()) {
ConnmanServiceProxy* proxy = vpnServices_.value(path);
if (proxy) {
// TODO: Maybe possible to remove after Sailfish OS 2.2.1 release.
proxy->SetProperty(autoConnectKey, QDBusVariant(true));
QDBusPendingCall call = proxy->Connect();
qCDebug(lcVpnLog) << "Connect to vpn" << path;
ConnmanServiceProxy* proxy = vpnServices_.value(path);
if (proxy) {
// TODO: Maybe possible to remove after Sailfish OS 2.2.1 release.
proxy->SetProperty(autoConnectKey, QDBusVariant(true));
QDBusPendingCall call = proxy->Connect();
qCDebug(lcVpnLog) << "Connect to vpn" << path;

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

Expand All @@ -564,17 +553,8 @@ void VpnModel::deactivateConnection(const QString &path)
qCInfo(lcVpnLog) << "Disconnect" << path;
ConnmanServiceProxy* proxy = vpnServices_.value(path);
if (proxy) {
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);
connect(connection, &VpnConnection::stateChanged, this, &VpnModel::updatePendingDisconnectState, Qt::UniqueConnection);
}

proxy->SetProperty(autoConnectKey, QDBusVariant(false));
QDBusPendingCall call = proxy->Disconnect();

QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, path](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<void> reply = *watcher;
Expand Down Expand Up @@ -739,27 +719,6 @@ 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();

disconnect(pendingDisconnect, &VpnConnection::stateChanged, this, &VpnModel::updatePendingDisconnectState);

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);
Expand Down
5 changes: 0 additions & 5 deletions src/vpnmodel.h
Expand Up @@ -90,9 +90,6 @@ 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 @@ -130,8 +127,6 @@ private slots:
QHash<QString, ConnmanVpnConnectionProxy *> connections_;
QHash<QString, ConnmanServiceProxy *> vpnServices_;
QSet<QString> defaultDomains_;
QMap<QString, VpnConnection*> pendingDisconnects_;
QString pendingConnect_;
CredentialsRepository credentials_;
ConnectionState bestState_;
};
Expand Down

0 comments on commit 462dbe6

Please sign in to comment.