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