Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdbuscontext.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qdbusmessage.h"
8
9#include "qdbuscontext.h"
10#include "qdbuscontext_p.h"
11
12#ifndef QT_NO_DBUS
13
15
17{
18 // determine if this is an adaptor or not
19 if (qobject_cast<QDBusAbstractAdaptor *>(obj))
20 obj = obj->parent();
21
22 Q_ASSERT(obj);
23
24 void *ptr = obj->qt_metacast("QDBusContext");
25 QDBusContext *q_ptr = reinterpret_cast<QDBusContext *>(ptr);
26 if (q_ptr) {
27 QDBusContextPrivate *old = q_ptr->d_ptr;
28 q_ptr->d_ptr = newContext;
29 return old;
30 }
31
32 return nullptr;
33}
34
35/*!
36 \since 4.3
37 \class QDBusContext
38 \inmodule QtDBus
39
40 \brief The QDBusContext class allows slots to determine the D-Bus context of the calls.
41
42 When a slot is called in an object due to a signal delivery or due
43 to a remote method call, it is sometimes necessary to know the
44 context in which that happened. In particular, if the slot
45 determines that it wants to send the reply at a later opportunity
46 or if it wants to reply with an error, the context is needed.
47
48 The QDBusContext class is an alternative to accessing the context
49 that doesn't involve modifying the code generated by the \l
50 {Qt D-Bus XML compiler (qdbusxml2cpp)}.
51
52 QDBusContext is used by subclassing it from the objects being
53 exported using QDBusConnection::registerObject(). The following
54 example illustrates the usage:
55
56 \snippet code/src_qdbus_qdbuscontext.cpp 0
57
58 The example illustrates the two typical uses, that of sending
59 error replies and that of delayed replies.
60
61 Note: do not subclass QDBusContext and QDBusAbstractAdaptor at the
62 same time. QDBusContext should appear in the real object, not the
63 adaptor. If it's necessary from the adaptor code to determine the
64 context, use a public inheritance and access the functions via
65 QObject::parent().
66*/
67
68/*!
69 Constructs an empty QDBusContext.
70 */
71QDBusContext::QDBusContext()
72 : d_ptr(nullptr)
73{
74}
75
76/*!
77 An empty destructor.
78 */
79QDBusContext::~QDBusContext()
80{
81}
82
83/*!
84 Returns \c true if we are processing a D-Bus call. If this function
85 returns \c true, the rest of the functions in this class are
86 available.
87
88 Accessing those functions when this function returns \c false is
89 undefined and may lead to crashes.
90*/
91bool QDBusContext::calledFromDBus() const
92{
93 return d_ptr;
94}
95
96/*!
97 Returns the connection from which this call was received.
98*/
99QDBusConnection QDBusContext::connection() const
100{
101 return d_ptr->connection;
102}
103
104/*!
105 Returns the message that generated this call.
106*/
107const QDBusMessage &QDBusContext::message() const
108{
109 return d_ptr->message;
110}
111
112/*!
113 Returns \c true if this call will have a delayed reply.
114
115 \sa setDelayedReply()
116*/
117bool QDBusContext::isDelayedReply() const
118{
119 return message().isDelayedReply();
120}
121
122/*!
123 Sets whether this call will have a delayed reply or not.
124
125 If \a enable is false, Qt D-Bus will automatically generate a reply
126 back to the caller, if needed, as soon as the called slot returns.
127
128 If \a enable is true, Qt D-Bus will not generate automatic
129 replies. It will also ignore the return value from the slot and
130 any output parameters. Instead, the called object is responsible
131 for storing the incoming message and send a reply or error at a
132 later time.
133
134 Failing to send a reply will result in an automatic timeout error
135 being generated by D-Bus.
136*/
137void QDBusContext::setDelayedReply(bool enable) const
138{
139 message().setDelayedReply(enable);
140}
141
142/*!
143 Sends an error \a name as a reply to the caller. The optional \a
144 msg parameter is a human-readable text explaining the failure.
145
146 If an error is sent, the return value and any output parameters
147 from the called slot will be ignored by Qt D-Bus.
148*/
149void QDBusContext::sendErrorReply(const QString &name, const QString &msg) const
150{
151 setDelayedReply(true);
152 connection().send(message().createErrorReply(name, msg));
153}
154
155/*!
156 \overload
157 Sends an error \a type as a reply to the caller. The optional \a
158 msg parameter is a human-readable text explaining the failure.
159
160 If an error is sent, the return value and any output parameters
161 from the called slot will be ignored by Qt D-Bus.
162*/
163void QDBusContext::sendErrorReply(QDBusError::ErrorType type, const QString &msg) const
164{
165 setDelayedReply(true);
166 connection().send(message().createErrorReply(type, msg));
167}
168
169QT_END_NAMESPACE
170
171#endif // QT_NO_DBUS
static QDBusContextPrivate * set(QObject *obj, QDBusContextPrivate *newContext)