如何调用GetSettings qt qdbus网络管理器



我正在调用ListConnections方法来列出系统上已经配置的所有连接。我正在尝试使用网络管理器的getSettings方法来检索每个连接的id,然后使用delete方法删除特定的连接问题是,我不能只得到id。我举了一个例子:https://cgit.freedesktop.org/NetworkManager/NetworkManager/tree/examples/C/qt/change-ipv4-addresses.cpp

我的代码:

#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;
}

终端返回:

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 

我不明白为什么我的QMap是空的?为什么它根本不含任何东西?如何检索与ssid 相对应的id

尝试使用

typedef QMap<QString, QVariantMap> Connection;

而不是

typedef QMap<QString, QMap<QString, QVariant> > Connection;

因此,函数调用如下所示:

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();

输出如下所示:

settingsPath: "/org/freedesktop/NetworkManager/Settings/3"
id:  "enp0s3_dhcp_config"

相关内容

  • 没有找到相关文章

最新更新