Coverage Report

Created: 2026-04-29 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/dbus/qdbuscontext.cpp
Line
Count
Source
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
0
{
18
    // determine if this is an adaptor or not
19
0
    if (qobject_cast<QDBusAbstractAdaptor *>(obj))
20
0
        obj = obj->parent();
21
22
0
    Q_ASSERT(obj);
23
24
0
    void *ptr = obj->qt_metacast("QDBusContext");
25
0
    QDBusContext *q_ptr = reinterpret_cast<QDBusContext *>(ptr);
26
0
    if (q_ptr) {
27
0
        QDBusContextPrivate *old = q_ptr->d_ptr;
28
0
        q_ptr->d_ptr = newContext;
29
0
        return old;
30
0
    }
31
32
0
    return nullptr;
33
0
}
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
0
    : d_ptr(nullptr)
73
0
{
74
0
}
75
76
/*!
77
  An empty destructor.
78
 */
79
QDBusContext::~QDBusContext()
80
0
{
81
0
}
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
0
{
93
0
    return d_ptr;
94
0
}
95
96
/*!
97
    Returns the connection from which this call was received.
98
*/
99
QDBusConnection QDBusContext::connection() const
100
0
{
101
0
    return d_ptr->connection;
102
0
}
103
104
/*!
105
    Returns the message that generated this call.
106
*/
107
const QDBusMessage &QDBusContext::message() const
108
0
{
109
0
    return d_ptr->message;
110
0
}
111
112
/*!
113
    Returns \c true if this call will have a delayed reply.
114
115
    \sa setDelayedReply()
116
*/
117
bool QDBusContext::isDelayedReply() const
118
0
{
119
0
    return message().isDelayedReply();
120
0
}
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
*/
137
void QDBusContext::setDelayedReply(bool enable) const
138
0
{
139
0
    message().setDelayedReply(enable);
140
0
}
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
*/
149
void QDBusContext::sendErrorReply(const QString &name, const QString &msg) const
150
0
{
151
0
    setDelayedReply(true);
152
0
    connection().send(message().createErrorReply(name, msg));
153
0
}
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
*/
163
void QDBusContext::sendErrorReply(QDBusError::ErrorType type, const QString &msg) const
164
0
{
165
0
    setDelayedReply(true);
166
0
    connection().send(message().createErrorReply(type, msg));
167
0
}
168
169
QT_END_NAMESPACE
170
171
#endif // QT_NO_DBUS