Qt 4.8
Public Functions | Properties | Friends | List of all members
QDBusContext Class Reference

The QDBusContext class allows slots to determine the D-Bus context of the calls. More...

#include <qdbuscontext.h>

Inheritance diagram for QDBusContext:
QConnmanDBusHelper QNmDBusHelper QOfonoDBusHelper

Public Functions

bool calledFromDBus () const
 Returns true if we are processing a D-Bus call. More...
 
QDBusConnection connection () const
 Returns the connection from which this call was received. More...
 
bool isDelayedReply () const
 Returns true if this call will have a delayed reply. More...
 
const QDBusMessagemessage () const
 Returns the message that generated this call. More...
 
 QDBusContext ()
 Constructs an empty QDBusContext. More...
 
void sendErrorReply (const QString &name, const QString &msg=QString()) const
 Sends an error name as a reply to the caller. More...
 
void sendErrorReply (QDBusError::ErrorType type, const QString &msg=QString()) const
 Sends an error type as a reply to the caller. More...
 
void setDelayedReply (bool enable) const
 Sets whether this call will have a delayed reply or not. More...
 
 ~QDBusContext ()
 An empty destructor. More...
 

Properties

QDBusContextPrivated_ptr
 

Friends

class QDBusContextPrivate
 

Detailed Description

The QDBusContext class allows slots to determine the D-Bus context of the calls.

Since
4.3
Attention
Module: QtDBus

When a slot is called in an object due to a signal delivery or due to a remote method call, it is sometimes necessary to know the context in which that happened. In particular, if the slot determines that it wants to send the reply at a later opportunity or if it wants to reply with an error, the context is needed.

The QDBusContext class is an alternative to accessing the context that doesn't involve modifying the code generated by the QtDBus XML Compiler (qdbusxml2cpp).

QDBusContext is used by subclassing it from the objects being exported using QDBusConnection::registerObject(). The following example illustrates the usage:

class MyObject: public QObject,
protected QDBusContext
{
...
protected slots:
void process();
public slots:
void methodWithError();
QString methodWithDelayedReply();
};
void MyObject::methodWithError()
{
"The method call 'methodWithError()' is not supported");
}
QString MyObject::methodWithDelayedReply()
{
conn = connection();
msg = message();
return QString();
}

The example illustrates the two typical uses, that of sending error replies and that of delayed replies.

Note: do not subclass QDBusContext and QDBusAbstractAdaptor at the same time. QDBusContext should appear in the real object, not the adaptor. If it's necessary from the adaptor code to determine the context, use a public inheritance and access the functions via QObject::parent().

Definition at line 60 of file qdbuscontext.h.

Constructors and Destructors

◆ QDBusContext()

QDBusContext::QDBusContext ( )

Constructs an empty QDBusContext.

Definition at line 110 of file qdbuscontext.cpp.

111  : d_ptr(0)
112 {
113 }
QDBusContextPrivate * d_ptr
Definition: qdbuscontext.h:78

◆ ~QDBusContext()

QDBusContext::~QDBusContext ( )

An empty destructor.

Definition at line 118 of file qdbuscontext.cpp.

119 {
120 }

Functions

◆ calledFromDBus()

bool QDBusContext::calledFromDBus ( ) const

Returns true if we are processing a D-Bus call.

If this function returns true, the rest of the functions in this class are available.

Accessing those functions when this function returns false is undefined and may lead to crashes.

Definition at line 130 of file qdbuscontext.cpp.

131 {
132  return d_ptr;
133 }
QDBusContextPrivate * d_ptr
Definition: qdbuscontext.h:78

◆ connection()

QDBusConnection QDBusContext::connection ( ) const

Returns the connection from which this call was received.

Definition at line 138 of file qdbuscontext.cpp.

Referenced by sendErrorReply().

139 {
140  return d_ptr->connection;
141 }
QDBusConnection connection
QDBusContextPrivate * d_ptr
Definition: qdbuscontext.h:78

◆ isDelayedReply()

bool QDBusContext::isDelayedReply ( ) const

Returns true if this call will have a delayed reply.

See also
setDelayedReply()

Definition at line 156 of file qdbuscontext.cpp.

157 {
158  return message().isDelayedReply();
159 }
const QDBusMessage & message() const
Returns the message that generated this call.
bool isDelayedReply() const
Returns the delayed reply flag, as set by setDelayedReply().

◆ message()

const QDBusMessage & QDBusContext::message ( ) const

◆ sendErrorReply() [1/2]

void QDBusContext::sendErrorReply ( const QString name,
const QString msg = QString() 
) const

Sends an error name as a reply to the caller.

The optional msg parameter is a human-readable text explaining the failure.

If an error is sent, the return value and any output parameters from the called slot will be ignored by QtDBus.

Definition at line 188 of file qdbuscontext.cpp.

189 {
190  setDelayedReply(true);
191  connection().send(message().createErrorReply(name, msg));
192 }
const QDBusMessage & message() const
Returns the message that generated this call.
bool send(const QDBusMessage &message) const
Sends the message over this connection, without waiting for a reply.
void setDelayedReply(bool enable) const
Sets whether this call will have a delayed reply or not.
QDBusConnection connection() const
Returns the connection from which this call was received.

◆ sendErrorReply() [2/2]

void QDBusContext::sendErrorReply ( QDBusError::ErrorType  type,
const QString msg = QString() 
) const

Sends an error type as a reply to the caller.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. The optional msg parameter is a human-readable text explaining the failure.

If an error is sent, the return value and any output parameters from the called slot will be ignored by QtDBus.

Definition at line 205 of file qdbuscontext.cpp.

206 {
207  setDelayedReply(true);
208  connection().send(message().createErrorReply(type, msg));
209 }
int type
Definition: qmetatype.cpp:239
const QDBusMessage & message() const
Returns the message that generated this call.
bool send(const QDBusMessage &message) const
Sends the message over this connection, without waiting for a reply.
void setDelayedReply(bool enable) const
Sets whether this call will have a delayed reply or not.
QDBusConnection connection() const
Returns the connection from which this call was received.

◆ setDelayedReply()

void QDBusContext::setDelayedReply ( bool  enable) const

Sets whether this call will have a delayed reply or not.

If enable is false, QtDBus will automatically generate a reply back to the caller, if needed, as soon as the called slot returns.

If enable is true, QtDBus will not generate automatic replies. It will also ignore the return value from the slot and any output parameters. Instead, the called object is responsible for storing the incoming message and send a reply or error at a later time.

Failing to send a reply will result in an automatic timeout error being generated by D-Bus.

Definition at line 176 of file qdbuscontext.cpp.

Referenced by sendErrorReply().

177 {
178  message().setDelayedReply(enable);
179 }
const QDBusMessage & message() const
Returns the message that generated this call.
void setDelayedReply(bool enable) const
Sets whether the message will be replied later (if enable is true) or if an automatic reply should be...

Friends and Related Functions

◆ QDBusContextPrivate

friend class QDBusContextPrivate
friend

Definition at line 79 of file qdbuscontext.h.

Properties

◆ d_ptr

QDBusContextPrivate* QDBusContext::d_ptr
private

Definition at line 78 of file qdbuscontext.h.

Referenced by calledFromDBus(), connection(), message(), and QDBusContextPrivate::set().


The documentation for this class was generated from the following files: