source: trunk/src/dbus/qdbuserror.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 12.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtDBus module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qdbuserror.h"
43
44#include <qdebug.h>
45#include <qvarlengtharray.h>
46
47#include <qdbus_symbols_p.h>
48#include "qdbusmessage.h"
49#include "qdbusmessage_p.h"
50
51QT_BEGIN_NAMESPACE
52
53/*
54 * Use the following Perl script to generate the error string index list:
55===== PERL SCRIPT ====
56print "static const char errorMessages_string[] =\n";
57$counter = 0;
58$i = 0;
59while (<STDIN>) {
60 chomp;
61 print " \"$_\\0\"\n";
62 $sizes[$i++] = $counter;
63 $counter += 1 + length $_;
64}
65print " \"\\0\";\n\nstatic const int errorMessages_indices[] = {\n ";
66for ($j = 0; $j < $i; ++$j) {
67 printf "$sizes[$j], ";
68}
69print "0\n};\n";
70===== PERL SCRIPT ====
71
72 * The input data is as follows:
73other
74org.freedesktop.DBus.Error.Failed
75org.freedesktop.DBus.Error.NoMemory
76org.freedesktop.DBus.Error.ServiceUnknown
77org.freedesktop.DBus.Error.NoReply
78org.freedesktop.DBus.Error.BadAddress
79org.freedesktop.DBus.Error.NotSupported
80org.freedesktop.DBus.Error.LimitsExceeded
81org.freedesktop.DBus.Error.AccessDenied
82org.freedesktop.DBus.Error.NoServer
83org.freedesktop.DBus.Error.Timeout
84org.freedesktop.DBus.Error.NoNetwork
85org.freedesktop.DBus.Error.AddressInUse
86org.freedesktop.DBus.Error.Disconnected
87org.freedesktop.DBus.Error.InvalidArgs
88org.freedesktop.DBus.Error.UnknownMethod
89org.freedesktop.DBus.Error.TimedOut
90org.freedesktop.DBus.Error.InvalidSignature
91org.freedesktop.DBus.Error.UnknownInterface
92com.trolltech.QtDBus.Error.InternalError
93org.freedesktop.DBus.Error.UnknownObject
94*/
95
96// in the same order as KnownErrors!
97static const char errorMessages_string[] =
98 "other\0"
99 "org.freedesktop.DBus.Error.Failed\0"
100 "org.freedesktop.DBus.Error.NoMemory\0"
101 "org.freedesktop.DBus.Error.ServiceUnknown\0"
102 "org.freedesktop.DBus.Error.NoReply\0"
103 "org.freedesktop.DBus.Error.BadAddress\0"
104 "org.freedesktop.DBus.Error.NotSupported\0"
105 "org.freedesktop.DBus.Error.LimitsExceeded\0"
106 "org.freedesktop.DBus.Error.AccessDenied\0"
107 "org.freedesktop.DBus.Error.NoServer\0"
108 "org.freedesktop.DBus.Error.Timeout\0"
109 "org.freedesktop.DBus.Error.NoNetwork\0"
110 "org.freedesktop.DBus.Error.AddressInUse\0"
111 "org.freedesktop.DBus.Error.Disconnected\0"
112 "org.freedesktop.DBus.Error.InvalidArgs\0"
113 "org.freedesktop.DBus.Error.UnknownMethod\0"
114 "org.freedesktop.DBus.Error.TimedOut\0"
115 "org.freedesktop.DBus.Error.InvalidSignature\0"
116 "org.freedesktop.DBus.Error.UnknownInterface\0"
117 "com.trolltech.QtDBus.Error.InternalError\0"
118 "org.freedesktop.DBus.Error.UnknownObject\0"
119 "\0";
120
121static const int errorMessages_indices[] = {
122 0, 6, 40, 76, 118, 153, 191, 231,
123 273, 313, 349, 384, 421, 461, 501, 540,
124 581, 617, 661, 705, 746, 0
125};
126
127static const int errorMessages_count = sizeof errorMessages_indices /
128 sizeof errorMessages_indices[0];
129
130static inline const char *get(QDBusError::ErrorType code)
131{
132 int intcode = qBound(0, int(code) - int(QDBusError::Other), errorMessages_count);
133 return errorMessages_string + errorMessages_indices[intcode];
134}
135
136static inline QDBusError::ErrorType get(const char *name)
137{
138 if (!name || !*name)
139 return QDBusError::NoError;
140 for (int i = 0; i < errorMessages_count; ++i)
141 if (strcmp(name, errorMessages_string + errorMessages_indices[i]) == 0)
142 return QDBusError::ErrorType(i + int(QDBusError::Other));
143 return QDBusError::Other;
144}
145
146/*!
147 \class QDBusError
148 \inmodule QtDBus
149 \since 4.2
150
151 \brief The QDBusError class represents an error received from the
152 D-Bus bus or from remote applications found in the bus.
153
154 When dealing with the D-Bus bus service or with remote
155 applications over D-Bus, a number of error conditions can
156 happen. This error conditions are sometimes signalled by a
157 returned error value or by a QDBusError.
158
159 C++ and Java exceptions are a valid analogy for D-Bus errors:
160 instead of returning normally with a return value, remote
161 applications and the bus may decide to throw an error
162 condition. However, the QtDBus implementation does not use the C++
163 exception-throwing mechanism, so you will receive QDBusErrors in
164 the return reply (see QDBusReply::error()).
165
166 QDBusError objects are used to inspect the error name and message
167 as received from the bus and remote applications. You should not
168 create such objects yourself to signal error conditions when
169 called from D-Bus: instead, use QDBusMessage::createError() and
170 QDBusConnection::send().
171
172 \sa QDBusConnection::send(), QDBusMessage, QDBusReply
173*/
174
175/*!
176 \enum QDBusError::ErrorType
177
178 In order to facilitate verification of the most common D-Bus errors generated by the D-Bus
179 implementation and by the bus daemon itself, QDBusError can be compared to a set of pre-defined
180 values:
181
182 \value NoError QDBusError is invalid (i.e., the call succeeded)
183 \value Other QDBusError contains an error that is one of the well-known ones
184 \value Failed The call failed (\c org.freedesktop.DBus.Error.Failed)
185 \value NoMemory Out of memory (\c org.freedesktop.DBus.Error.NoMemory)
186 \value ServiceUnknown The called service is not known
187 (\c org.freedesktop.DBus.Error.ServiceUnknown)
188 \value NoReply The called method did not reply within the specified timeout
189 (\c org.freedesktop.DBus.Error.NoReply)
190 \value BadAddress The address given is not valid
191 (\c org.freedesktop.DBus.Error.BadAddress)
192 \value NotSupported The call/operation is not supported
193 (\c org.freedesktop.DBus.Error.NotSupported)
194 \value LimitsExceeded The limits allocated to this process/call/connection exceeded the
195 pre-defined values (\c org.freedesktop.DBus.Error.LimitsExceeded)
196 \value AccessDenied The call/operation tried to access a resource it isn't allowed to
197 (\c org.freedesktop.DBus.Error.AccessDenied)
198 \value NoServer \e {Documentation doesn't say what this is for}
199 (\c org.freedesktop.DBus.Error.NoServer)
200 \value Timeout \e {Documentation doesn't say what this is for or how it's used}
201 (\c org.freedesktop.DBus.Error.Timeout)
202 \value NoNetwork \e {Documentation doesn't say what this is for}
203 (\c org.freedesktop.DBus.Error.NoNetwork)
204 \value AddressInUse QDBusServer tried to bind to an address that is already in use
205 (\c org.freedesktop.DBus.Error.AddressInUse)
206 \value Disconnected The call/process/message was sent after QDBusConnection disconnected
207 (\c org.freedesktop.DBus.Error.Disconnected)
208 \value InvalidArgs The arguments passed to this call/operation are not valid
209 (\c org.freedesktop.DBus.Error.InvalidArgs)
210 \value UnknownMethod The method called was not found in this object/interface with the
211 given parameters (\c org.freedesktop.DBus.Error.UnknownMethod)
212 \value TimedOut \e {Documentation doesn't say...}
213 (\c org.freedesktop.DBus.Error.TimedOut)
214 \value InvalidSignature The type signature is not valid or compatible
215 (\c org.freedesktop.DBus.Error.InvalidSignature)
216 \value UnknownInterface The interface is not known
217 \value InternalError An internal error occurred
218 (\c com.trolltech.QtDBus.Error.InternalError)
219 \value UnknownObject The remote object could not be found.
220
221*/
222
223/*!
224 \internal
225 Constructs a QDBusError from a DBusError structure.
226*/
227QDBusError::QDBusError(const DBusError *error)
228 : code(NoError)
229{
230 if (!error || !q_dbus_error_is_set(error))
231 return;
232
233 code = ::get(error->name);
234 msg = QString::fromUtf8(error->message);
235 nm = QString::fromUtf8(error->name);
236}
237
238/*!
239 \internal
240 Constructs a QDBusError from a QDBusMessage.
241*/
242QDBusError::QDBusError(const QDBusMessage &qdmsg)
243 : code(NoError)
244{
245 if (qdmsg.type() != QDBusMessage::ErrorMessage)
246 return;
247
248 code = ::get(qdmsg.errorName().toUtf8().constData());
249 nm = qdmsg.errorName();
250 msg = qdmsg.errorMessage();
251}
252
253/*!
254 \internal
255 Constructs a QDBusError from a well-known error code
256*/
257QDBusError::QDBusError(ErrorType error, const QString &mess)
258 : code(error)
259{
260 nm = QLatin1String(::get(error));
261 msg = mess;
262}
263
264/*!
265 \internal
266 Constructs a QDBusError from another QDBusError object
267*/
268QDBusError::QDBusError(const QDBusError &other)
269 : code(other.code), msg(other.msg), nm(other.nm)
270{
271}
272
273/*!
274 \internal
275 Assignment operator
276*/
277
278QDBusError &QDBusError::operator=(const QDBusError &other)
279{
280 code = other.code;
281 msg = other.msg;
282 nm = other.nm;
283 return *this;
284}
285
286/*!
287 Returns this error's ErrorType.
288
289 \sa ErrorType
290*/
291
292QDBusError::ErrorType QDBusError::type() const
293{
294 return code;
295}
296
297/*!
298 Returns this error's name. Error names are similar to D-Bus Interface names, like
299 \c org.freedesktop.DBus.InvalidArgs.
300
301 \sa type()
302*/
303
304QString QDBusError::name() const
305{
306 return nm;
307}
308
309/*!
310 Returns the message that the callee associated with this error. Error messages are
311 implementation defined and usually contain a human-readable error code, though this does not
312 mean it is suitable for your end-users.
313*/
314
315QString QDBusError::message() const
316{
317 return msg;
318}
319
320/*!
321 Returns true if this is a valid error condition (i.e., if there was an error),
322 otherwise false.
323*/
324
325bool QDBusError::isValid() const
326{
327 return (code != NoError);
328}
329
330/*!
331 \since 4.3
332 Returns the error name associated with error condition \a error.
333*/
334QString QDBusError::errorString(ErrorType error)
335{
336 return QLatin1String(::get(error));
337}
338
339#ifndef QT_NO_DEBUG_STREAM
340QDebug operator<<(QDebug dbg, const QDBusError &msg)
341{
342 dbg.nospace() << "QDBusError(" << msg.name() << ", " << msg.message() << ")";
343 return dbg.space();
344}
345#endif
346
347QT_END_NAMESPACE
348
349
Note: See TracBrowser for help on using the repository browser.