Qt 4.8
qdbuserror.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDBus module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
51 #ifndef QT_NO_DBUS
52 
54 
55 /*
56  * Use the following Perl script to generate the error string index list:
57 ===== PERL SCRIPT ====
58 print "static const char errorMessages_string[] =\n";
59 $counter = 0;
60 $i = 0;
61 while (<STDIN>) {
62  chomp;
63  print " \"$_\\0\"\n";
64  $sizes[$i++] = $counter;
65  $counter += 1 + length $_;
66 }
67 print " \"\\0\";\n\nstatic const int errorMessages_indices[] = {\n ";
68 for ($j = 0; $j < $i; ++$j) {
69  printf "$sizes[$j], ";
70 }
71 print "0\n};\n";
72 ===== PERL SCRIPT ====
73 
74  * The input data is as follows:
75 other
76 org.freedesktop.DBus.Error.Failed
77 org.freedesktop.DBus.Error.NoMemory
78 org.freedesktop.DBus.Error.ServiceUnknown
79 org.freedesktop.DBus.Error.NoReply
80 org.freedesktop.DBus.Error.BadAddress
81 org.freedesktop.DBus.Error.NotSupported
82 org.freedesktop.DBus.Error.LimitsExceeded
83 org.freedesktop.DBus.Error.AccessDenied
84 org.freedesktop.DBus.Error.NoServer
85 org.freedesktop.DBus.Error.Timeout
86 org.freedesktop.DBus.Error.NoNetwork
87 org.freedesktop.DBus.Error.AddressInUse
88 org.freedesktop.DBus.Error.Disconnected
89 org.freedesktop.DBus.Error.InvalidArgs
90 org.freedesktop.DBus.Error.UnknownMethod
91 org.freedesktop.DBus.Error.TimedOut
92 org.freedesktop.DBus.Error.InvalidSignature
93 org.freedesktop.DBus.Error.UnknownInterface
94 com.trolltech.QtDBus.Error.InternalError
95 org.freedesktop.DBus.Error.UnknownObject
96 com.trolltech.QtDBus.Error.InvalidService
97 com.trolltech.QtDBus.Error.InvalidObjectPath
98 com.trolltech.QtDBus.Error.InvalidInterface
99 com.trolltech.QtDBus.Error.InvalidMember
100 */
101 
102 // in the same order as KnownErrors!
103 static const char errorMessages_string[] =
104  "other\0"
105  "org.freedesktop.DBus.Error.Failed\0"
106  "org.freedesktop.DBus.Error.NoMemory\0"
107  "org.freedesktop.DBus.Error.ServiceUnknown\0"
108  "org.freedesktop.DBus.Error.NoReply\0"
109  "org.freedesktop.DBus.Error.BadAddress\0"
110  "org.freedesktop.DBus.Error.NotSupported\0"
111  "org.freedesktop.DBus.Error.LimitsExceeded\0"
112  "org.freedesktop.DBus.Error.AccessDenied\0"
113  "org.freedesktop.DBus.Error.NoServer\0"
114  "org.freedesktop.DBus.Error.Timeout\0"
115  "org.freedesktop.DBus.Error.NoNetwork\0"
116  "org.freedesktop.DBus.Error.AddressInUse\0"
117  "org.freedesktop.DBus.Error.Disconnected\0"
118  "org.freedesktop.DBus.Error.InvalidArgs\0"
119  "org.freedesktop.DBus.Error.UnknownMethod\0"
120  "org.freedesktop.DBus.Error.TimedOut\0"
121  "org.freedesktop.DBus.Error.InvalidSignature\0"
122  "org.freedesktop.DBus.Error.UnknownInterface\0"
123  "com.trolltech.QtDBus.Error.InternalError\0"
124  "org.freedesktop.DBus.Error.UnknownObject\0"
125  "com.trolltech.QtDBus.Error.InvalidService\0"
126  "com.trolltech.QtDBus.Error.InvalidObjectPath\0"
127  "com.trolltech.QtDBus.Error.InvalidInterface\0"
128  "com.trolltech.QtDBus.Error.InvalidMember\0"
129  "\0";
130 
131 static const int errorMessages_indices[] = {
132  0, 6, 40, 76, 118, 153, 191, 231,
133  273, 313, 349, 384, 421, 461, 501, 540,
134  581, 617, 661, 705, 746, 787, 829, 874,
135  918, 0
136 };
137 
138 static const int errorMessages_count = sizeof errorMessages_indices /
139  sizeof errorMessages_indices[0];
140 
141 static inline const char *get(QDBusError::ErrorType code)
142 {
143  int intcode = qBound(0, int(code) - int(QDBusError::Other), errorMessages_count);
145 }
146 
147 static inline QDBusError::ErrorType get(const char *name)
148 {
149  if (!name || !*name)
150  return QDBusError::NoError;
151  for (int i = 0; i < errorMessages_count; ++i)
152  if (strcmp(name, errorMessages_string + errorMessages_indices[i]) == 0)
153  return QDBusError::ErrorType(i + int(QDBusError::Other));
154  return QDBusError::Other;
155 }
156 
255 QDBusError::QDBusError(const DBusError *error)
256  : code(NoError)
257 {
258  if (!error || !q_dbus_error_is_set(error))
259  return;
260 
261  code = ::get(error->name);
262  msg = QString::fromUtf8(error->message);
263  nm = QString::fromUtf8(error->name);
264 }
265 
274  : code(NoError)
275 {
276  if (qdmsg.type() != QDBusMessage::ErrorMessage)
277  return;
278 
279  code = ::get(qdmsg.errorName().toUtf8().constData());
280  nm = qdmsg.errorName();
281  msg = qdmsg.errorMessage();
282 }
283 
289  : code(error)
290 {
291  nm = QLatin1String(::get(error));
292  msg = mess;
293 }
294 
300  : code(other.code), msg(other.msg), nm(other.nm)
301 {
302 }
303 
310 {
311  code = other.code;
312  msg = other.msg;
313  nm = other.nm;
314  return *this;
315 }
316 
324 {
325  return code;
326 }
327 
336 {
337  return nm;
338 }
339 
347 {
348  return msg;
349 }
350 
357 {
358  return (code != NoError);
359 }
360 
369 {
370  return QLatin1String(::get(error));
371 }
372 
373 #ifndef QT_NO_DEBUG_STREAM
375 {
376  dbg.nospace() << "QDBusError(" << msg.name() << ", " << msg.message() << ')';
377  return dbg.space();
378 }
379 #endif
380 
382 
383 #endif // QT_NO_DBUS
QDebug operator<<(QDebug dbg, const QDBusError &msg)
Definition: qdbuserror.cpp:374
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
bool isValid() const
Returns true if this is a valid error condition (i.e., if there was an error), otherwise false...
Definition: qdbuserror.cpp:356
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
static const int errorMessages_count
Definition: qdbuserror.cpp:138
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
#define error(msg)
The QDBusError class represents an error received from the D-Bus bus or from remote applications foun...
Definition: qdbuserror.h:60
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString msg
Definition: qdbuserror.h:112
ErrorType
In order to facilitate verification of the most common D-Bus errors generated by the D-Bus implementa...
Definition: qdbuserror.h:63
ErrorType type() const
Returns this error&#39;s ErrorType.
Definition: qdbuserror.cpp:323
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QString errorMessage() const
Returns the human-readable message associated with the error that was received.
const char * name
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
QString name() const
Returns this error&#39;s name.
Definition: qdbuserror.cpp:335
QString nm
Definition: qdbuserror.h:113
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QString errorName() const
Returns the name of the error that was received.
Q_DECL_CONSTEXPR const T & qBound(const T &min, const T &val, const T &max)
Definition: qglobal.h:1219
static const char * get(QDBusError::ErrorType code)
Definition: qdbuserror.cpp:141
QDBusError & operator=(const QDBusError &other)
Definition: qdbuserror.cpp:309
The QDBusMessage class represents one message sent or received over the D-Bus bus.
Definition: qdbusmessage.h:59
MessageType type() const
Returns the message type.
QDBusError(const DBusError *error=0)
Constructs a QDBusError from a DBusError structure.
Definition: qdbuserror.cpp:255
ErrorType code
Definition: qdbuserror.h:111
static const int errorMessages_indices[]
Definition: qdbuserror.cpp:131
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
static QString errorString(ErrorType error)
Returns the error name associated with error condition error.
Definition: qdbuserror.cpp:368
static const char errorMessages_string[]
Definition: qdbuserror.cpp:103
QString message() const
Returns the message that the callee associated with this error.
Definition: qdbuserror.cpp:346