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