我想知道是否有通过QtDbus来“监视”特定服务的方法调用的简便方法。例如,我希望在对org.freedesktop.Notifications进行Notify方法调用时能够“捕获”它并读取其参数。
注意*
我可能找到了使用dbus-monitor应用程序的解决方案,但我想知道是否有更好的方法来访问Qt Dbus库。
是的,您应该能够通过QtDBus进行这项工作(需要一些工作)。从根本上讲,消息总线上的任何客户端都可以订阅任何消息-仅受总线安全策略的限制。(因此,除非您具有调试访问权限的应用程序或消息总线的调试访问权限,否则就无法监视它。)关键是您要org.freedesktop.DBus.AddMatch
在总线本身上使用该方法:
// first connect our handler object to the QDBusConnection so that it knows what
// to do with the incoming Notify calls
// slotNotifyObserved() must have a compatible signature to the DBus call
QDBusConnection::sessionBus().connect("", // any service name
"", // any object path
"org.freedesktop.Notifications",
"Notify",
myImplementingQObject,
SLOT(slotNotifyObserved(...));
// then ask the bus to send us a copy of each Notify call message
QString matchString = "interface='org.freedesktop.Notifications',member='Notify',type='method_call',eavesdrop='true'";
QDBusInterface busInterface("org.freedesktop.DBus", "/org/freedesktop/DBus",
"org.freedesktop.DBus");
busInterface.call("AddMatch", matchString);
// once we get back to the event loop our object should be called as other programs
// make Notify() calls
该的DBus规范给出了各种匹配字段就可以去参加的上市matchString
。
为了更好地了解发生了什么,QDBus文档建议设置环境变量,QDBUS_DEBUG=1
以使您的应用程序记录有关其dbus消息传递的信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句