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"
6
#
include
"qdbusconnection.h"
7
#
include
"qdbusabstractadaptor.h"
8
9
#
include
"qdbuscontext.h"
10
#
include
"qdbuscontext_p.h"
11
12
#
ifndef
QT_NO_DBUS
13
14
QT_BEGIN_NAMESPACE
15
16
QDBusContextPrivate
*
QDBusContextPrivate
::
set
(QObject *obj,
QDBusContextPrivate
*newContext)
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
*/
71
QDBusContext::QDBusContext()
72
: d_ptr(
nullptr
)
73
{
74
}
75
76
/*!
77
An empty destructor.
78
*/
79
QDBusContext::~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
*/
91
bool
QDBusContext::calledFromDBus()
const
92
{
93
return
d_ptr;
94
}
95
96
/*!
97
Returns the connection from which this call was received.
98
*/
99
QDBusConnection QDBusContext::connection()
const
100
{
101
Q_ASSERT(calledFromDBus());
102
return
d_ptr->connection;
103
}
104
105
/*!
106
Returns the message that generated this call.
107
*/
108
const
QDBusMessage &QDBusContext::message()
const
109
{
110
Q_ASSERT(calledFromDBus());
111
return
d_ptr->message;
112
}
113
114
/*!
115
Returns \c true if this call will have a delayed reply.
116
117
\sa setDelayedReply()
118
*/
119
bool
QDBusContext::isDelayedReply()
const
120
{
121
Q_ASSERT(calledFromDBus());
122
return
message().isDelayedReply();
123
}
124
125
/*!
126
Sets whether this call will have a delayed reply or not.
127
128
If \a enable is false, Qt D-Bus will automatically generate a reply
129
back to the caller, if needed, as soon as the called slot returns.
130
131
If \a enable is true, Qt D-Bus will not generate automatic
132
replies. It will also ignore the return value from the slot and
133
any output parameters. Instead, the called object is responsible
134
for storing the incoming message and send a reply or error at a
135
later time.
136
137
Failing to send a reply will result in an automatic timeout error
138
being generated by D-Bus.
139
*/
140
void
QDBusContext::setDelayedReply(
bool
enable)
const
141
{
142
Q_ASSERT(calledFromDBus());
143
message().setDelayedReply(enable);
144
}
145
146
/*!
147
Sends an error \a name as a reply to the caller. The optional \a
148
msg parameter is a human-readable text explaining the failure.
149
150
If an error is sent, the return value and any output parameters
151
from the called slot will be ignored by Qt D-Bus.
152
*/
153
void
QDBusContext::sendErrorReply(
const
QString &name,
const
QString &msg)
const
154
{
155
Q_ASSERT(calledFromDBus());
156
setDelayedReply(
true
);
157
connection().send(message().createErrorReply(name, msg));
158
}
159
160
/*!
161
\overload
162
Sends an error \a type as a reply to the caller. The optional \a
163
msg parameter is a human-readable text explaining the failure.
164
165
If an error is sent, the return value and any output parameters
166
from the called slot will be ignored by Qt D-Bus.
167
*/
168
void
QDBusContext::sendErrorReply(QDBusError::ErrorType type,
const
QString &msg)
const
169
{
170
Q_ASSERT(calledFromDBus());
171
setDelayedReply(
true
);
172
connection().send(message().createErrorReply(type, msg));
173
}
174
175
QT_END_NAMESPACE
176
177
#
endif
// QT_NO_DBUS
QDBusContextPrivate
Definition
qdbuscontext_p.h:31
QDBusContextPrivate::set
static QDBusContextPrivate * set(QObject *obj, QDBusContextPrivate *newContext)
Definition
qdbuscontext.cpp:16
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtbase
src
dbus
qdbuscontext.cpp
Generated on
for Qt by
1.16.1