Question

I would like to know if there is an easy way via QtDbus to "monitor" method calls of a certain service. For example I would like when there is a Notify method call to org.freedesktop.Notifications to be able to "catch" it and read its arguments.

Note*

I may have find a solution, which is using the dbus-monitor application but I would like to know if there is a better way through Qt Dbus library.

Was it helpful?

Solution

Yes, you should be able to do this (with a bit of work) through QtDBus. Fundamentally, any client on a message bus can subscribe to any message -- limited only by the bus's security policy. (So there is no way to monitor an explicitly uncooperative application, unless you have debugging access to it or to the message bus.) The key is that you will want to use the org.freedesktop.DBus.AddMatch method on the bus itself:

// 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

The DBus Specification gives a listing of the various matching fields that could go in matchString.

To better see what is going on, the QDBus documentation suggests setting the environment variable QDBUS_DEBUG=1 to cause your application to log information about its dbus messaging.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top