I'm calling the ListConnections method to list all the connections already configured on the system. I am trying to use the getSettings method of the network manager to retrieve the id of each connection, then delete a specific connection with the Delete method The problem is, I can't get just the id. I took example from: https://cgit.freedesktop.org/NetworkManager/NetworkManager/tree/examples/C/qt/change-ipv4-addresses.cpp
My code :
#include <QtDBus/QtDBus>
typedef QMap<QString, QMap<QString, QVariant> > Connection;
Q_DECLARE_METATYPE(Connection)
Q_DECLARE_METATYPE(QList<uint>);
Q_DECLARE_METATYPE(QList<QList<uint> >);
const QString NM_SETTING_CONNECTION_SETTING_NAME = "connection";
const QString NM_SETTING_CONNECTION_ID = "id";
const QString NM_SETTING_CONNECTION_UUID = "uuid";
QDBusConnection bus = QDBusConnection::systemBus();
QDBusInterface NetworkManager("org.freedesktop.NetworkManager", // service
"/org/freedesktop/NetworkManager/Settings", // path
"org.freedesktop.NetworkManager.Settings", // interface
bus);
if(!NetworkManager.isValid())
{
qDebug()<< "Failed to connect to the system bus" << NetworkManager.lastError() <<endl ;
return ;
}
QDBusReply<QList<QDBusObjectPath> > Connections = NetworkManager.call("ListConnections");
qDebug() << "reading saved networks..." << endl;
foreach (const QDBusObjectPath& connection, Connections.value())
{
qDebug() << "existing connections: " << connection.path() << endl;
QDBusInterface NetworkManagerSettings("org.freedesktop.NetworkManager",
connection.path(),
"org.freedesktop.NetworkManager.Settings.Connection",
QDBusConnection::systemBus());
QDBusMessage Settings = NetworkManagerSettings.call("GetSettings");
QVariant t = Settings.arguments().at(0);
qDebug()<< Settings << endl;
qDebug()<< "T :" << t.value<QDBusArgument>().currentType() << endl ; //is Qmap
qDebug()<< "VALUE :" << t.value<QMap <QString,QVariant>>() << endl ;
Connection settings;
QDBusReply<Connection > result2 = NetworkManagerSettings.call("GetSettings");
qDebug()<< "SETTINGS : " << result2.value().isEmpty() << endl;
}
terminal return:
2021-04-17 15:54:14 : existing connections: "/org/freedesktop/NetworkManager/Settings/9"
2021-04-17 15:54:14 : QDBusMessage(type=MethodReturn, service=":1.13", signature="a{sa{sv}}", contents=([Argument: a{sa{sv}} {"ipv6" = [Argument: a{sv} {"address-data" = [Variant: [Argument: aa{sv} {}]], "addresses" = [Variant: [Argument: a(ayuay) {}]], "dns" = [Variant: [Argument: aay {}]], "dns-search" = [Variant(QStringList): {}], "method" = [Variant(QString): "auto"], "route-data" = [Variant: [Argument: aa{sv} {}]], "routes" = [Variant: [Argument: a(ayuayu) {}]]}], "connection" = [Argument: a{sv} {"id" = [Variant(QString): "Auto SFR_7080"], "interface-name" = [Variant(QString): "wlp8s0"], "permissions" = [Variant(QStringList): {}], "timestamp" = [Variant(qulonglong): 1618667506], "type" = [Variant(QString): "802-11-wireless"], "uuid" = [Variant(QString): "a6a7d882-5483-49d9-84c0-d3de815d5bc7"]}], "proxy" = [Argument: a{sv} {}], "ipv4" = [Argument: a{sv} {"address-data" = [Variant: [Argument: aa{sv} {}]], "addresses" = [Variant: [Argument: aau {}]], "dns" = [Variant: [Argument: au {}]], "dns-search" = [Variant(QStringList): {}], "method" = [Variant(QString): "auto"], "route-data" = [Variant: [Argument: aa{sv} {}]], "routes" = [Variant: [Argument: aau {}]]}], "802-11-wireless-security" = [Argument: a{sv} {"auth-alg" = [Variant(QString): "open"], "key-mgmt" = [Variant(QString): "wpa-psk"]}], "802-11-wireless" = [Argument: a{sv} {"mac-address-blacklist" = [Variant(QStringList): {}], "mode" = [Variant(QString): "infrastructure"], "security" = [Variant(QString): "802-11-wireless-security"], "seen-bssids" = [Variant(QStringList): {"24:95:04:E0:70:84"}], "ssid" = [Variant(QByteArray): {83, 70, 82, 95, 55, 48, 56, 48}]}]}]) )
2021-04-17 15:54:14 : T : 4
2021-04-17 15:54:14 : VALUE : QMap()
2021-04-17 15:54:14 : SETTINGS : true
I don't understand why my QMap is empty? how come it doesn't contain anything at all? How to retrieve the id that corresponds to the ssid
Try using
typedef QMap<QString, QVariantMap> Connection;
instead of
typedef QMap<QString, QMap<QString, QVariant> > Connection;
So the function call would look something like below:
QDBusInterface setting(NETWORK_MANAGER_SERVICE, settingsPath,
NETWORK_MANAGER_SETT_CONN_INTERFACE, QDBusConnection::systemBus());
qDebug() << "settingsPath: " << settingsPath;
QDBusMessage result = setting.call("GetSettings");
const QDBusArgument &dbusArg = result.arguments().at(0).value<QDBusArgument>();
Connection connection;
dbusArg >> connection;
qDebug() << "id: " << connection["connection"]["id"].toString();
And output would look something like below:
settingsPath: "/org/freedesktop/NetworkManager/Settings/3"
id: "enp0s3_dhcp_config"