For background:
I’m writing a program that listens for the creation of new IPv4 Config objects in NetworkManager and gets the DHCP-provided gateway and nameservers from the properties of the object. Eventually this code should create host routes to the DNS servers for a little more robustness in some complicated lab setups.
I can get the Gateway property easily enough, but the program crashes as soon as it would reach the line to read the property for Nameservers.
Header:
#ifndef NETMONITOR_H
#define NETMONITOR_H
#include <QtCore>
#include <QtDBus/QDBusInterface>
#define NM_SVC "org.freedesktop.NetworkManager"
#define NM_OBJ_MAIN "/org/freedesktop/NetworkManager"
#define NM_OBJ_MANAGER "/org/freedesktop"
#define NM_IF_MAIN "org.freedesktop.NetworkManager"
#define DBUS_IF_MANAGER "org.freedesktop.DBus.ObjectManager"
#define NM_IF_IPV4CFG "org.freedesktop.NetworkManager.IP4Config"
class NetMonitor : public QObject
{
Q_OBJECT
private:
public:
NetMonitor(QObject* parent );
private slots:
void handleNewInterfaces( QDBusObjectPath f_newObject, QVariantMap f_newInterfaces );
};
#endif
Implementation:
#include <QtCore>
#include <QtDBus/QDBusConnection>
#include <QDBusMetaType>
#include "NetMonitor.h"
typedef QList<uint> NameserverList;
Q_DECLARE_METATYPE(NameserverList)
NetMonitor::NetMonitor( QObject *parent ) : QObject( parent )
{
qDBusRegisterMetaType<NameserverList>();
if( ! QDBusConnection::systemBus().connect( QStringLiteral(NM_SVC), QStringLiteral( NM_OBJ_MANAGER ),
QStringLiteral(DBUS_IF_MANAGER), QStringLiteral("InterfacesAdded"),
this, SLOT(handleNewInterfaces( QDBusObjectPath, QVariantMap )) ) )
{
qInfo() << "Failed to connect InterfacesAdded signal.";
}
}
void NetMonitor::handleNewInterfaces( QDBusObjectPath f_ipconfig, QVariantMap f_settingsmap ){
Q_UNUSED(f_settingsmap);
if( ! f_ipconfig.path().startsWith( QStringLiteral("/org/freedesktop/NetworkManager/IP4Config/") ) ) return;
qInfo( "New NM Object=%s", qPrintable(f_ipconfig.path()) );
QDBusInterface newIP4conf ( QStringLiteral(NM_SVC), f_ipconfig.path(), QStringLiteral(NM_IF_IPV4CFG), QDBusConnection::systemBus() );
qInfo( "Gateway is %s", qPrintable( newIP4conf.property("Gateway").toString() ) );
NameserverList nameservers = newIP4conf.property("Nameservers").value<NameserverList>();
}
When I run this in the background with Qt DBus Debug messages enabled and turn on networking I get:
QDBusConnectionPrivate(0xf6604750) sending message: QDBusMessage(type=MethodCall, service="org.freedesktop.NetworkManager", path="/org/freedesktop/NetworkManager/IP4Config/69", interface="org.freedesktop.DBus.Properties", member="Get", signature="", contents=("org.freedesktop.NetworkManager.IP4Config", "Gateway") )
QDBusConnectionPrivate(0xf6604750) got message reply: QDBusMessage(type=MethodReturn, service=":1.5", signature="v", contents=([Variant(QString): "192.168.0.1"]) )
Gateway is 192.168.0.1
Cannot construct placeholder type QDBusRawType
[1]+ Aborted QDBUS_DEBUG=1 ./objmonitor
So the type system doesn’t even know how to prepare to run this call despite my efforts to declare a metatype and just gives up. But it can retrieve the string-type Gateway just fine. And this is the simple example. The non-deprecated property for NameserverData is of type aa{sv}
, or a list of dictionaries of variants.
I’ve seen various questions like this before but most seem to end in workarounds or partial self-answers. What I want to know is: in Qt 5, and later 6, what strictly is needed to add each of the complex types to the type system so you can read them from properties, connect to signals that return them, and write them to properties, or use them as the arguments of calls.
Do you need to define new classes for structs?
If you need a list do you define the meta object for the list type or for the contents of the list?
How do you deal with recursive complex types, like the actually VERY common double-string-indexed map of variants a{sa{sv}}
?
Am I going to be stuck manually calling org.freedesktop.DBus.Properties.Get()
and then parsing the results out of a QDBusArgument?