Skip to content

Commit

Permalink
[libconnman-qt] Support DefaultRoute VPN property. JB#48797
Browse files Browse the repository at this point in the history
Add support for setting default route property for VPN connections.
ConnMan has this support already, there wasn't any UI or library support
for it.

ConnMan has the "DefaultRoute" option as string with values
"true"|"false". Added conversion of DefaultRoute variant string to bool
when receiving it from ConnMan and doing the vice versa at sending
the content to ConnMan.

Publish the value as boolean for components using the library. Store
the value in m_defaultRoute if the setters and getters are used, which
are also implemented.
  • Loading branch information
LaakkonenJussi committed Jun 5, 2020
1 parent 8a0db35 commit b753f90
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
53 changes: 50 additions & 3 deletions libconnman-qt/vpnconnection.cpp
Expand Up @@ -52,6 +52,10 @@ namespace {
const QString connmanService = QStringLiteral("net.connman");
const QString connmanVpnService = QStringLiteral("net.connman.vpn");
const QString autoConnectKey = QStringLiteral("AutoConnect");
const QString defaultRouteKey = QStringLiteral("DefaultRoute");
const QString defaultRoutePropertyName = QStringLiteral("defaultRoute");
const QString strTrue = QString("true");
const QString strFalse = QString("false");

QString vpnServicePath(const QString &connectionPath)
{
Expand All @@ -65,6 +69,7 @@ VpnConnectionPrivate::VpnConnectionPrivate(VpnConnection &qq, const QString &pat
, m_serviceProxy(connmanService, vpnServicePath(path), QDBusConnection::systemBus(), nullptr)
, m_path(path)
, m_autoConnect(false)
, m_defaultRoute(true)
, m_state(VpnConnection::Idle)
, q_ptr(&qq)
{
Expand All @@ -84,8 +89,16 @@ void VpnConnectionPrivate::init()
QDBusMessage message = reply.reply();
QVariantMap properties = MarshalUtils::demarshallArgument<QVariantMap>(message.arguments().value(0));
bool autoConnect = properties.value(autoConnectKey).toBool();
QString str = properties.value(defaultRouteKey).toString();
properties.clear();
properties.insert(autoConnectKey, autoConnect);

// Don't add the value if it is not set. Undefined value is treated being as the default value (true)
if (!str.isEmpty()) {
bool defaultRoute = str == strTrue;
properties.insert(defaultRouteKey, defaultRoute);
}

q->update(MarshalUtils::propertiesToQml(properties));
} else {
qDebug() << "Error :" << m_path << ":" << reply.error().message();
Expand Down Expand Up @@ -147,14 +160,18 @@ void VpnConnection::modifyConnection(const QVariantMap &properties)

qDebug() << "Updating VPN connection for modification:" << d->m_path;

// Remove properties that connman doesn't know about
// Remove properties that ConnMan doesn't know about
QVariantMap updatedProperties(properties);
updatedProperties.remove(QString("path"));
updatedProperties.remove(QString("state"));
updatedProperties.remove(QString("index"));
updatedProperties.remove(QString("immutable"));
updatedProperties.remove(QString("storeCredentials"));

// Convert defaultRoute bool to string for ConnMan
d->m_defaultRoute = updatedProperties.value(defaultRoutePropertyName).toBool();
updatedProperties.insert(defaultRoutePropertyName, d->m_defaultRoute ? strTrue : strFalse);

// SetProperty supports a single property or an array of properties
d->m_connectionProxy.SetProperty(QString("Properties"),
QDBusVariant(MarshalUtils::propertiesToDBus(updatedProperties)));
Expand Down Expand Up @@ -236,6 +253,7 @@ void VpnConnection::update(const QVariantMap &updateProperties)
d->checkChanged(properties, emissions, "userRoutes", &VpnConnection::userRoutesChanged);
d->checkChanged(properties, emissions, "serverRoutes", &VpnConnection::serverRoutesChanged);

d->updateVariable(properties, emissions, "defaultRoute", &d->m_defaultRoute, &VpnConnection::defaultRouteChanged);
d->updateVariable(properties, emissions, "autoConnect", &d->m_autoConnect, &VpnConnection::autoConnectChanged);
d->updateVariable(properties, emissions, "state", &d->m_state, &VpnConnection::stateChanged);

Expand Down Expand Up @@ -295,6 +313,28 @@ void VpnConnection::setAutoConnect(bool autoConnect)
}
}

bool VpnConnection::defaultRoute() const
{
Q_D(const VpnConnection);

return d->m_defaultRoute;
}

void VpnConnection::setDefaultRoute(bool defaultRoute)
{
Q_D(VpnConnection);

if (d->m_defaultRoute != defaultRoute) {
d->m_defaultRoute = defaultRoute;
qDebug() << "VPN defaultRoute changed:" << d->m_properties.value("name").toString() << defaultRoute;

QString defaultRouteStr = defaultRoute ? strTrue : strFalse;
d->m_serviceProxy.SetProperty(defaultRouteKey, QDBusVariant(defaultRouteStr));

emit defaultRouteChanged();
}
}

VpnConnection::ConnectionState VpnConnection::state() const
{
Q_D(const VpnConnection);
Expand Down Expand Up @@ -355,8 +395,15 @@ inline void VpnConnectionPrivate::updateVariable(QVariantMap &properties, QQueue
{
QVariantMap::const_iterator it = properties.constFind(name);
if ((it != properties.constEnd()) && (*property != qvariant_cast< T >(it.value()))) {
*property = qvariant_cast< T >(it.value());
m_properties.insert(name, it.value());
if (name == defaultRoutePropertyName) {
QString str = it.value().toString();
m_defaultRoute = (str.isEmpty() || str == strTrue);
*property = qvariant_cast< T >(m_defaultRoute);
m_properties.insert(name, QVariant(m_defaultRoute));
} else {
*property = qvariant_cast< T >(it.value());
m_properties.insert(name, it.value());
}
properties.remove(name);
emissions << changedSignal;
}
Expand Down
5 changes: 5 additions & 0 deletions libconnman-qt/vpnconnection.h
Expand Up @@ -71,6 +71,7 @@ class VpnConnection : public QObject
Q_PROPERTY(QStringList nameservers READ nameservers WRITE setNameservers NOTIFY nameserversChanged)
Q_PROPERTY(QVariant userRoutes READ userRoutes WRITE setUserRoutes NOTIFY userRoutesChanged)
Q_PROPERTY(QVariant serverRoutes READ serverRoutes WRITE setServerRoutes NOTIFY serverRoutesChanged)
Q_PROPERTY(bool defaultRoute READ defaultRoute WRITE setDefaultRoute NOTIFY defaultRouteChanged)

Q_PROPERTY(QVariantMap properties READ properties WRITE setProperties NOTIFY propertiesChanged)
Q_PROPERTY(QVariantMap providerProperties READ providerProperties WRITE setProviderProperties NOTIFY providerPropertiesChanged)
Expand Down Expand Up @@ -140,6 +141,9 @@ class VpnConnection : public QObject
QVariant serverRoutes() const;
void setServerRoutes(const QVariant &serverRoutes);

bool defaultRoute() const;
void setDefaultRoute(bool defaultRoute);

QVariantMap properties() const;
void setProperties(const QVariantMap properties);

Expand All @@ -161,6 +165,7 @@ class VpnConnection : public QObject
void nameserversChanged();
void userRoutesChanged();
void serverRoutesChanged();
void defaultRouteChanged();
void propertiesChanged();
void providerPropertiesChanged();
void connectedChanged();
Expand Down
1 change: 1 addition & 0 deletions libconnman-qt/vpnconnection_p.h
Expand Up @@ -55,6 +55,7 @@ class VpnConnectionPrivate
NetConnmanServiceInterface m_serviceProxy;
QString m_path;
bool m_autoConnect;
bool m_defaultRoute;
VpnConnection::ConnectionState m_state;
QVariantMap m_properties;

Expand Down
1 change: 1 addition & 0 deletions plugin/plugins.qmltypes
Expand Up @@ -918,6 +918,7 @@ Module {
Property { name: "nameservers"; type: "QStringList" }
Property { name: "userRoutes"; type: "QVariant" }
Property { name: "serverRoutes"; type: "QVariant" }
Property { name: "defaultRoute"; type: "bool" }
Property { name: "properties"; type: "QVariantMap" }
Property { name: "providerProperties"; type: "QVariantMap" }
Property { name: "connected"; type: "bool"; isReadonly: true }
Expand Down

0 comments on commit b753f90

Please sign in to comment.