Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/dbus/qdbuscontext.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtDBus module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||
9 | ** Commercial License Usage | - | ||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||
16 | ** | - | ||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||
25 | ** | - | ||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||
29 | ** | - | ||||||
30 | ** $QT_END_LICENSE$ | - | ||||||
31 | ** | - | ||||||
32 | ****************************************************************************/ | - | ||||||
33 | - | |||||||
34 | #include "qdbusmessage.h" | - | ||||||
35 | #include "qdbusconnection.h" | - | ||||||
36 | #include "qdbusabstractadaptor.h" | - | ||||||
37 | - | |||||||
38 | #include "qdbuscontext.h" | - | ||||||
39 | #include "qdbuscontext_p.h" | - | ||||||
40 | - | |||||||
41 | #ifndef QT_NO_DBUS | - | ||||||
42 | - | |||||||
43 | QT_BEGIN_NAMESPACE | - | ||||||
44 | - | |||||||
45 | QDBusContextPrivate *QDBusContextPrivate::set(QObject *obj, QDBusContextPrivate *newContext) | - | ||||||
46 | { | - | ||||||
47 | // determine if this is an adaptor or not | - | ||||||
48 | if (qobject_cast<QDBusAbstractAdaptor *>(obj))
| 302-3534 | ||||||
49 | obj = obj->parent(); executed 302 times by 5 tests: obj = obj->parent(); Executed by:
| 302 | ||||||
50 | - | |||||||
51 | Q_ASSERT(obj); | - | ||||||
52 | - | |||||||
53 | void *ptr = obj->qt_metacast("QDBusContext"); | - | ||||||
54 | QDBusContext *q_ptr = reinterpret_cast<QDBusContext *>(ptr); | - | ||||||
55 | if (q_ptr) {
| 1044-2792 | ||||||
56 | QDBusContextPrivate *old = q_ptr->d_ptr; | - | ||||||
57 | q_ptr->d_ptr = newContext; | - | ||||||
58 | return old; executed 1044 times by 4 tests: return old; Executed by:
| 1044 | ||||||
59 | } | - | ||||||
60 | - | |||||||
61 | return 0; executed 2792 times by 158 tests: return 0; Executed by:
| 2792 | ||||||
62 | } | - | ||||||
63 | - | |||||||
64 | /*! | - | ||||||
65 | \since 4.3 | - | ||||||
66 | \class QDBusContext | - | ||||||
67 | \inmodule QtDBus | - | ||||||
68 | - | |||||||
69 | \brief The QDBusContext class allows slots to determine the D-Bus context of the calls. | - | ||||||
70 | - | |||||||
71 | When a slot is called in an object due to a signal delivery or due | - | ||||||
72 | to a remote method call, it is sometimes necessary to know the | - | ||||||
73 | context in which that happened. In particular, if the slot | - | ||||||
74 | determines that it wants to send the reply at a later opportunity | - | ||||||
75 | or if it wants to reply with an error, the context is needed. | - | ||||||
76 | - | |||||||
77 | The QDBusContext class is an alternative to accessing the context | - | ||||||
78 | that doesn't involve modifying the code generated by the \l | - | ||||||
79 | {Qt D-Bus XML compiler (qdbusxml2cpp)}. | - | ||||||
80 | - | |||||||
81 | QDBusContext is used by subclassing it from the objects being | - | ||||||
82 | exported using QDBusConnection::registerObject(). The following | - | ||||||
83 | example illustrates the usage: | - | ||||||
84 | - | |||||||
85 | \snippet code/src_qdbus_qdbuscontext.cpp 0 | - | ||||||
86 | - | |||||||
87 | The example illustrates the two typical uses, that of sending | - | ||||||
88 | error replies and that of delayed replies. | - | ||||||
89 | - | |||||||
90 | Note: do not subclass QDBusContext and QDBusAbstractAdaptor at the | - | ||||||
91 | same time. QDBusContext should appear in the real object, not the | - | ||||||
92 | adaptor. If it's necessary from the adaptor code to determine the | - | ||||||
93 | context, use a public inheritance and access the functions via | - | ||||||
94 | QObject::parent(). | - | ||||||
95 | */ | - | ||||||
96 | - | |||||||
97 | /*! | - | ||||||
98 | Constructs an empty QDBusContext. | - | ||||||
99 | */ | - | ||||||
100 | QDBusContext::QDBusContext() | - | ||||||
101 | : d_ptr(0) | - | ||||||
102 | { | - | ||||||
103 | } executed 6 times by 4 tests: end of block Executed by:
| 6 | ||||||
104 | - | |||||||
105 | /*! | - | ||||||
106 | An empty destructor. | - | ||||||
107 | */ | - | ||||||
108 | QDBusContext::~QDBusContext() | - | ||||||
109 | { | - | ||||||
110 | } | - | ||||||
111 | - | |||||||
112 | /*! | - | ||||||
113 | Returns \c true if we are processing a D-Bus call. If this function | - | ||||||
114 | returns \c true, the rest of the functions in this class are | - | ||||||
115 | available. | - | ||||||
116 | - | |||||||
117 | Accessing those functions when this function returns \c false is | - | ||||||
118 | undefined and may lead to crashes. | - | ||||||
119 | */ | - | ||||||
120 | bool QDBusContext::calledFromDBus() const | - | ||||||
121 | { | - | ||||||
122 | return d_ptr; executed 20 times by 1 test: return d_ptr; Executed by:
| 20 | ||||||
123 | } | - | ||||||
124 | - | |||||||
125 | /*! | - | ||||||
126 | Returns the connection from which this call was received. | - | ||||||
127 | */ | - | ||||||
128 | QDBusConnection QDBusContext::connection() const | - | ||||||
129 | { | - | ||||||
130 | return d_ptr->connection; executed 21 times by 2 tests: return d_ptr->connection; Executed by:
| 21 | ||||||
131 | } | - | ||||||
132 | - | |||||||
133 | /*! | - | ||||||
134 | Returns the message that generated this call. | - | ||||||
135 | */ | - | ||||||
136 | const QDBusMessage &QDBusContext::message() const | - | ||||||
137 | { | - | ||||||
138 | return d_ptr->message; executed 86 times by 3 tests: return d_ptr->message; Executed by:
| 86 | ||||||
139 | } | - | ||||||
140 | - | |||||||
141 | /*! | - | ||||||
142 | Returns \c true if this call will have a delayed reply. | - | ||||||
143 | - | |||||||
144 | \sa setDelayedReply() | - | ||||||
145 | */ | - | ||||||
146 | bool QDBusContext::isDelayedReply() const | - | ||||||
147 | { | - | ||||||
148 | return message().isDelayedReply(); never executed: return message().isDelayedReply(); | 0 | ||||||
149 | } | - | ||||||
150 | - | |||||||
151 | /*! | - | ||||||
152 | Sets whether this call will have a delayed reply or not. | - | ||||||
153 | - | |||||||
154 | If \a enable is false, Qt D-Bus will automatically generate a reply | - | ||||||
155 | back to the caller, if needed, as soon as the called slot returns. | - | ||||||
156 | - | |||||||
157 | If \a enable is true, Qt D-Bus will not generate automatic | - | ||||||
158 | replies. It will also ignore the return value from the slot and | - | ||||||
159 | any output parameters. Instead, the called object is responsible | - | ||||||
160 | for storing the incoming message and send a reply or error at a | - | ||||||
161 | later time. | - | ||||||
162 | - | |||||||
163 | Failing to send a reply will result in an automatic timeout error | - | ||||||
164 | being generated by D-Bus. | - | ||||||
165 | */ | - | ||||||
166 | void QDBusContext::setDelayedReply(bool enable) const | - | ||||||
167 | { | - | ||||||
168 | message().setDelayedReply(enable); | - | ||||||
169 | } executed 43 times by 3 tests: end of block Executed by:
| 43 | ||||||
170 | - | |||||||
171 | /*! | - | ||||||
172 | Sends an error \a name as a reply to the caller. The optional \a | - | ||||||
173 | msg parameter is a human-readable text explaining the failure. | - | ||||||
174 | - | |||||||
175 | If an error is sent, the return value and any output parameters | - | ||||||
176 | from the called slot will be ignored by Qt D-Bus. | - | ||||||
177 | */ | - | ||||||
178 | void QDBusContext::sendErrorReply(const QString &name, const QString &msg) const | - | ||||||
179 | { | - | ||||||
180 | setDelayedReply(true); | - | ||||||
181 | connection().send(message().createErrorReply(name, msg)); | - | ||||||
182 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||
183 | - | |||||||
184 | /*! | - | ||||||
185 | \overload | - | ||||||
186 | Sends an error \a type as a reply to the caller. The optional \a | - | ||||||
187 | msg parameter is a human-readable text explaining the failure. | - | ||||||
188 | - | |||||||
189 | If an error is sent, the return value and any output parameters | - | ||||||
190 | from the called slot will be ignored by Qt D-Bus. | - | ||||||
191 | */ | - | ||||||
192 | void QDBusContext::sendErrorReply(QDBusError::ErrorType type, const QString &msg) const | - | ||||||
193 | { | - | ||||||
194 | setDelayedReply(true); | - | ||||||
195 | connection().send(message().createErrorReply(type, msg)); | - | ||||||
196 | } never executed: end of block | 0 | ||||||
197 | - | |||||||
198 | QT_END_NAMESPACE | - | ||||||
199 | - | |||||||
200 | #endif // QT_NO_DBUS | - | ||||||
Source code | Switch to Preprocessed file |