rendered paste body#include <stdlib.h>
#include <QtDBus>
#include <QCoreApplication>
#include <QDebug>
#include <QObject>
#define DBUS_SERVICE "org.gnome.Xchat"
#define DBUS_REMOTE "/org/gnome/Xchat/Remote"
#define DBUS_REMOTE_CONNECTION_INTERFACE "org.gnome.Xchat.Connection"
#define DBUS_REMOTE_PLUGIN_INTERFACE "org.gnome.Xchat.Plugin"
#define DBUS_NAME "org.freedesktop.DBus"
#define DBUS_PATH "/org/freedesktop/DBus"
#define DBUS_INTERFACE "org.freedesktop.DBus"
uint command_id;
uint server_id;
class Receiver: public QObject, public QDBusContext
{
Q_OBJECT
public slots:
void test_server_cb(const QStringList &word, const QStringList &word_eol,
uint hook_id, uint context_id)
{
if (hook_id == server_id)
qDebug() << "message:" << word_eol.at(0);
}
void test_command_cb(const QStringList &word, const QStringList &word_eol,
uint hook_id, uint context_id)
{
if (hook_id == command_id) {
// sorry, I don't know what "proxy" is in dbus-glib here
qDebug() << "test command received:" << word_eol.at(1);
}
}
void unload_cb()
{
qDebug() << "Good bye !";
QCoreApplication::instance()->quit();
}
void name_owner_changed_callback(const QString &arg1, const QString &arg2,
const QString &arg3)
{
qDebug() << "Got a Signal!";
qDebug() << "Signal's args:" << arg1 << arg2 << arg3;
}
};
static Receiver receiver;
static void
write_error(const char *message, const QDBusError &error)
{
qDebug().nospace() << message << ": " << error.message();
}
static QDBusAbstractInterface *get_another_dbus_proxy()
{
QDBusConnection connection = QDBusConnection::sessionBus();
if (!connection.isConnected()) {
qDebug() << "Failed to get connection";
return NULL;
}
QDBusConnectionInterface *proxy = connection.interface();
receiver.connect(proxy, SIGNAL(serviceOwnerChanged(QString,QString,QString)),
SLOT(name_owner_changed_callback(QString,QString,QString)));
qDebug() << "Signals connected...";
return proxy;
}
int
main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
// WARNING: this leaks!
get_another_dbus_proxy();
QDBusConnection connection = QDBusConnection::sessionBus();
QDBusInterface *remote_object = new QDBusInterface(DBUS_SERVICE,
DBUS_REMOTE,
DBUS_REMOTE_CONNECTION_INTERFACE,
connection);
QDBusReply<QString> reply = remote_object->call("Connect", argv[0],
"example",
"Example of a D-Bus client",
"1.0");
QString path;
if (!reply.isValid()) {
write_error("Failed to complete Connect", reply.error());
return EXIT_FAILURE;
} else {
path = reply;
}
delete remote_object;
QDBusReply<uint> reply2;
remote_object = new QDBusInterface(DBUS_SERVICE, path,
DBUS_REMOTE_PLUGIN_INTERFACE, connection);
reply2 = remote_object->call("HookCommand", "test", 0, "Simple D-Bus example", 1);
if (!reply2.isValid()) {
write_error("Failed to complete HookCommand", reply2.error());
return EXIT_FAILURE;
} else {
command_id = reply2;
}
qDebug() << "Command hook id =" << command_id;
reply2 = remote_object->call("HookServer", "RAW LINE", 0, 0);
if (!reply2.isValid()) {
write_error("Failed to complete HookServer", reply2.error());
return EXIT_FAILURE;
} else {
server_id = reply2;
}
qDebug() << "Server hook id =" << server_id;
receiver.connect(remote_object, SIGNAL(CommandSignal(QStringList,QStringList,uint,uint)),
SLOT(test_command_cb(QStringList,QStringList,uint,uint)));
receiver.connect(remote_object, SIGNAL(ServerSignal(QStringList,QStringList,uint,uint)),
SLOT(test_server_cb(QStringList,QStringList,uint,uint)));
receiver.connect(remote_object, SIGNAL(UnloadSignal()),
SLOT(unload_cb()));
return app.exec();
}
#include "example.moc"