我想知道是否有一种简单的方法可以通过QtDbus来"监控"某个服务的方法调用.例如,当有一个Notify方法调用org.freedesktop.Notifications时,我希望能够"捕获"它并读取它的参数.
注意*
我可能找到了一个使用dbus-monitor应用程序的解决方案,但我想知道是否有更好的方法通过Qt Dbus库.
小智 6
是的,您应该可以通过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
Run Code Online (Sandbox Code Playgroud)
该的DBus规范给出了各种匹配字段就可以去参加的上市matchString.
为了更好地了解发生的情况,QDBus文档建议设置环境变量QDBUS_DEBUG=1以使应用程序记录有关其dbus消息传递的信息.
| 归档时间: |
|
| 查看次数: |
1914 次 |
| 最近记录: |