example-client.cpp
#if !defined(EXAMPLE_CLIENT_H)
#define EXAMPLE_CLIENT_H
#include <qobject.h>
class QDBusMessage;
class QDBusProxy;
class ExampleClient : public QObject
{
Q_OBJECT
public:
ExampleClient();
virtual ~ExampleClient();
private slots:
void slotConnect();
void slotDBusSignal(const QDBusMessage& message);
void slotAsyncReply(int callID, const QDBusMessage& reply);
private:
QDBusProxy* m_proxy;
};
#endif
#include <qapplication.h>
#include <qtimer.h>
#include <dbus/qdbusdatalist.h>
#include <dbus/qdbuserror.h>
#include <dbus/qdbusconnection.h>
#include <dbus/qdbusmessage.h>
#include <dbus/qdbusproxy.h>
#include "example-client.h"
ExampleClient::ExampleClient() : QObject(0, "ExampleClient"), m_proxy(0)
{
QTimer::singleShot(0, this, SLOT(slotConnect()));
}
ExampleClient::~ExampleClient()
{
}
void ExampleClient::slotConnect()
{
QDBusConnection connection = QDBusConnection::sessionBus();
if (!connection.isConnected())
{
qFatal("Cannot connect to session bus");
}
m_proxy = new QDBusProxy(this);
m_proxy->setService("org.freedesktop.DBus");
m_proxy->setPath("/org/freedesktop/DBus");
m_proxy->setInterface("org.freedesktop.DBus");
m_proxy->setConnection(connection);
QObject::connect(m_proxy, SIGNAL(dbusSignal(const QDBusMessage&)),
this, SLOT(slotDBusSignal(const QDBusMessage&)));
qDebug("Calling method 'ListNames' synchronously. Our name: '%s'",
connection.uniqueName().local8Bit().data());
QDBusMessage reply = m_proxy->sendWithReply("ListNames", QValueList<QDBusData>());
if (reply.type() == QDBusMessage::ReplyMessage && reply.count() == 1)
{
if (reply[0].type() == QDBusData::List)
{
QDBusDataList list = reply[0].toList();
if (list.type() != QDBusData::String)
{
qWarning("Reply for 'ListNames' carried unexpected value of"
" type %s-List", QDBusData::typeName(list.type()));
}
QStringList nameList = list.toQStringList();
qDebug("Bus knows %d names:", nameList.count());
QStringList::iterator it = nameList.begin();
for (; it != nameList.end(); ++it)
{
qDebug("%s", (*it).local8Bit().data());
}
}
else
qWarning("Reply for 'ListNames' carried unexpected value of type %s",
reply[0].typeName());
}
else
{
qWarning("Method call 'ListNames' failed");
QDBusError error = m_proxy->lastError();
if (error.isValid())
{
qDebug("Error '%s' says:\n\"%s\"", error.name().local8Bit().data(),
error.message().local8Bit().data());
}
}
qDebug("\n");
QObject::connect(m_proxy, SIGNAL(asyncReply(int, const QDBusMessage&)),
this, SLOT(slotAsyncReply(int, const QDBusMessage&)));
QValueList<QDBusData> parameters;
parameters << QDBusData::fromString(connection.uniqueName());
int id = m_proxy->sendWithAsyncReply("GetConnectionUnixUser", parameters);
if (id > 0)
{
qDebug("Calling method 'GetConnectionUnixUser' asynchronously. Call ID %d\n", id);
}
else
qWarning("Method call 'GetConnectionUnixUser' failed");
}
void ExampleClient::slotDBusSignal(const QDBusMessage& message)
{
qDebug("Received signal '%s' from peer: %d values",
message.member().local8Bit().data(), message.count());
for (QDBusMessage::const_iterator it = message.begin(); it != message.end(); ++it)
{
qDebug("Value of type %s: '%s'", (*it).typeName(), (*it).toString().local8Bit().data());
}
qDebug("\n");
}
void ExampleClient::slotAsyncReply(int callID, const QDBusMessage& reply)
{
qDebug("Received asynchronous reply from peer. Call ID %d", callID);
switch (reply.type())
{
case QDBusMessage::ErrorMessage:
{
qDebug("Reply is an error message");
QDBusError error = reply.error();
if (error.isValid())
{
qDebug("Error '%s' says:\n\"%s\"\n",
error.name().local8Bit().data(),
error.message().local8Bit().data());
}
}
break;
case QDBusMessage::ReplyMessage:
{
qDebug("Reply from peer: %d values", reply.count());
for (QDBusMessage::const_iterator it = reply.begin(); it != reply.end(); ++it)
{
qDebug("Value of type %s: '%d'",
(*it).typeName(), (*it).toUInt32());
}
qDebug("\n");
}
break;
default:
break;
}
}
int main(int argc, char** argv)
{
QApplication app(argc, argv, false);
ExampleClient client;
return app.exec();
}