PDA

View Full Version : DBus connection



Nitz
24th January 2009, 21:32
Hi,

I'm trying to create two Qt applications and connect them through DBus system.

The first application, the receiver, has a slot named "reload". The other application, the sender, must call this slot.

The receiver

The main.cpp:

MainWindow is a class that inherits from QMainWindow.

#include <QApplication>
#include <QTextCodec>
#include <QDBusConnection>

#include "MainWindow.hpp"
#include "DBusManager.hpp"

int main(int argc, char *argv[]) {
QTextCodec::setCodecForLocale(QTextCodec::codecFor Name("UTF-8"));
QApplication app(argc, argv);

MainWindow *mainWindow = new MainWindow();
new DBusManager(mainWindow, &app);
QDBusConnection::sessionBus().registerObject("/", &app);

mainWindow->show();

return app.exec();
}


The DBusManager.hpp:

#ifndef COMPONENT_DBUSMANAGER
#define COMPONENT_DBUSMANAGER

#include <QDBusAbstractAdaptor>
#include <QApplication>

#include "MainWindow.hpp"

class DBusManager : public QDBusAbstractAdaptor {
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.simple-environment.sdesktop")

private:
MainWindow *mainWindow;

public:
DBusManager(MainWindow *mainWindow, QApplication *parent);

public slots:
Q_NOREPLY void reload();
};

#endif

The DBusManager.cpp:

#include "DBusManager.hpp"

/* Constructor */

DBusManager::DBusManager(MainWindow *mainWindow, QApplication *parent) : QDBusAbstractAdaptor(parent) {
this->mainWindow = mainWindow;
}

/* Public slots */

void DBusManager::reload() {
this->mainWindow->reload();
}


The sender

The sender, executes this code:

QDBusMessage message = QDBusMessage::createMethodCall("org.simple-environment.sdesktop", "/", "sdesktop", "reload");
QDBusConnection::sessionBus().call(message);

I can compile both applications without problems, but when I execute them, the sender writes this through stderr:

process 14899: arguments to dbus_message_new_method_call() were incorrect, assertion "interface == NULL || _dbus_check_is_valid_interface (interface)" failed in file dbus-message.c line 1076.
This is normally a bug in some application using the D-Bus library.
QDBusConnection: error: could not send message to service "org.simple-environment.sdesktop" path "/" interface "sdesktop" member "reload"

What is wrong with this code?

Nitz
24th January 2009, 23:04
Ok, now it doesn't write the error through stderr. The problem was that I have specified bad dbus inteface at sender.

The new sender code:

QDBusMessage message = QDBusMessage::createMethodCall("org.simple-environment.sdesktop", "/", "", "reload");
QDBusConnection::sessionBus().call(message);

I run both applications but the receiver doesn't execute the "reload" slot :(

Nitz
25th January 2009, 14:43
The error is that I'm registering the object but not the service at receiver.

The solution is, at main.cpp:

#include <QApplication>
#include <QTextCodec>
#include <QDBusConnection>

#include "MainWindow.hpp"
#include "DBusManager.hpp"

int main(int argc, char *argv[]) {
QTextCodec::setCodecForLocale(QTextCodec::codecFor Name("UTF-8"));
QApplication app(argc, argv);

MainWindow *mainWindow = new MainWindow();
new DBusManager(mainWindow, &app);
QDBusConnection bus = QDBusConnection::sessionBus();
bus.registerObject("/", &app);
bus.registerService("org.simple-environment.sdesktop");

mainWindow->show();

return app.exec();
}