Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt DBus Monitor Method Calls

Tags:

c++

qt

dbus

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.

like image 324
George Sofianos Avatar asked Mar 23 '14 14:03

George Sofianos


People also ask

What is D-Bus monitor?

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.

What is D-Bus in Qt?

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.

What is Qdbus?

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.


1 Answers

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.

like image 144
bks Avatar answered Sep 19 '22 06:09

bks