代码之家  ›  专栏  ›  技术社区  ›  George Sofianos

Qt DBus监视器方法调用

  •  5
  • George Sofianos  · 技术社区  · 10 年前

    我想知道是否有一个简单的方法 通过QtDbus“监控”方法调用 某项服务。例如,我希望当对org.freedesktop.Notifications有Notify方法调用时,能够“捕获”它并读取其参数。

    注释*

    我可能已经找到了一个解决方案,那就是使用dbus监视器应用程序,但我想知道是否有更好的方法通过Qt dbus库。

    1 回复  |  直到 10 年前
        1
  •  7
  •   Mohammad Kanan    3 年前

    是的,您应该能够通过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 Specification 列出了可以进入的各种匹配字段 matchString .

    为了更好地了解情况, the QDBus documentation 建议设置环境变量 QDBUS_DEBUG=1 以使应用程序记录有关其dbus消息传递的信息。