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.
The dbus-monitor command is used to monitor messages going through a D-Bus message bus. See https://www.freedesktop.org/wiki/Software/dbus/ for more information about the big picture.
D-Bus is an Inter-Process Communication (IPC) and Remote Procedure Calling (RPC) mechanism originally developed for Linux to replace existing and competing IPC solutions with one unified protocol.
qdbus provides an interface to Qt-based applications communicating over D-Bus. See http://www.freedesktop.org/software/dbus/ for more information about the big picture. By default qdbus will list all service names of services that are running and you can manipulate at the moment.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With