summaryrefslogtreecommitdiff
path: root/qt/src/qdbuserror.cpp
blob: ee6ce12c38fab193e3babfaddc588941dec890a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* qdbuserror.h QDBusError object
 *
 * Copyright (C) 2005 Harald Fernengel <harry@kdevelop.org>
 * Copyright (C) 2006 Trolltech AS. All rights reserved.
 *    Author: Thiago Macieira <thiago.macieira@trolltech.com>
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "qdbuserror.h"

#include <qdebug.h>
#include <qvarlengtharray.h>

#include <dbus/dbus.h>
#include "qdbusmessage.h"

struct ErrorMessageMapping
{
    ErrorMessageMapping();
    QVarLengthArray<const char*, QDBusError::qKnownErrorsMax> messages;

    inline const char *get(QDBusError::KnownErrors code) const
    {
        if (code <= QDBusError::Other || code > QDBusError::qKnownErrorsMax)
            return messages[int(QDBusError::Other) - 1];
        return messages[int(code) - 1];
    }

    inline QDBusError::KnownErrors get(const char *name) const
    {
        if (!name || !*name)
            return QDBusError::NoError;
        for (int i = QDBusError::Other; i <= QDBusError::qKnownErrorsMax; ++i)
            if (strcmp(name, messages[i - 1]) == 0)
                return QDBusError::KnownErrors(i);
        return QDBusError::Other;
    }
};

static const char errorMessages_string[] =
    // in the same order as KnownErrors!
    "other\0"                           // Other -- shouldn't happen
    DBUS_ERROR_FAILED "\0"              // Failed
    DBUS_ERROR_NO_MEMORY "\0"           // NoMemory
    DBUS_ERROR_SERVICE_UNKNOWN "\0"     // ServiceUnknown
    DBUS_ERROR_NO_REPLY "\0"            // NoReply
    DBUS_ERROR_BAD_ADDRESS "\0"         // BadAddress
    DBUS_ERROR_NOT_SUPPORTED "\0"       // NotSupported
    DBUS_ERROR_LIMITS_EXCEEDED "\0"     // LimitsExceeded
    DBUS_ERROR_ACCESS_DENIED  "\0"      // AccessDenied
    DBUS_ERROR_NO_SERVER "\0"           // NoServer
    DBUS_ERROR_TIMEOUT "\0"             // Timeout
    DBUS_ERROR_NO_NETWORK "\0"          // NoNetwork
    DBUS_ERROR_ADDRESS_IN_USE "\0"      // AddressInUse
    DBUS_ERROR_DISCONNECTED "\0"        // Disconnected
    DBUS_ERROR_INVALID_ARGS "\0"        // InvalidArgs
    DBUS_ERROR_UNKNOWN_METHOD "\0"      // UnknownMethod
    DBUS_ERROR_TIMED_OUT "\0"           // TimedOut
    DBUS_ERROR_INVALID_SIGNATURE "\0"   // InvalidSignature
    "com.trolltech.QtDBus.Error.UnknownInterface\0" // UnknownInterface
    "com.trolltech.QtDBus.Error.InternalError\0" // InternalError
    "\0";

ErrorMessageMapping::ErrorMessageMapping()
    : messages(int(QDBusError::qKnownErrorsMax))
{
    // create the list:
    const char *p = errorMessages_string;
    int i = 0;
    while (*p) {
        messages[i] = p;
        p += strlen(p) + 1;
        ++i;
    }
}

Q_GLOBAL_STATIC(ErrorMessageMapping, errorMessages)    

/*!
    \class QDBusError
    \brief Represents an error received from the D-Bus bus or from remote applications found in the bus.

    When dealing with the D-Bus bus service or with remote applications over D-Bus, a number of
    error conditions can happen. This error conditions are sometimes signalled by a returned error
    value or by a QDBusError.

    C++ and Java exceptions are a valid analogy for D-Bus errors: instead of returning normally with
    a return value, remote applications and the bus may decide to throw an error condition. However,
    the QtDBus implementation does not use the C++ exception-throwing mechanism, so you will receive
    QDBusErrors in the return reply (see QDBusReply::error()).

    QDBusError objects are used to inspect the error name and message as received from the bus and
    remote applications. You should not create such objects yourself to signal error conditions when
    called from D-Bus: instead, use QDBusMessage::error and QDBusConnection::send.

    \sa QDBusConnection::send(), QDBusMessage, QDBusReply
*/

/*!
    \enum QDBusError::KnownErrors

    In order to facilitate verification of the most common D-Bus errors generated by the D-Bus
    implementation and by the bus daemon itself, QDBusError can be compared to a set of pre-defined
    values:

    \value NoError              QDBusError is invalid (i.e., the call succeeded)
    \value Other                QDBusError contains an error that is one of the well-known ones
    \value Failed               The call failed (\c org.freedesktop.DBus.Error.Failed)
    \value NoMemory             Out of memory (\c org.freedesktop.DBus.Error.NoMemory)
    \value ServiceUnknown       The called service is not known
                                (\c org.freedesktop.DBus.Error.ServiceUnknown)
    \value NoReply              The called method did not reply within the specified timeout
                                (\c org.freedesktop.DBus.Error.NoReply)
    \value BadAddress           The address given is not valid
                                (\c org.freedesktop.DBus.Error.BadAddress)
    \value NotSupported         The call/operation is not supported
                                (\c org.freedesktop.DBus.Error.NotSupported)
    \value LimitsExceeded       The limits allocated to this process/call/connection exceeded the
                                pre-defined values (\c org.freedesktop.DBus.Error.LimitsExceeded)
    \value AccessDenied         The call/operation tried to access a resource it isn't allowed to
                                (\c org.freedesktop.DBus.Error.AccessDenied)
    \value NoServer             \e {Documentation doesn't say what this is for}
                                (\c org.freedesktop.DBus.Error.NoServer)
    \value Timeout              \e {Documentation doesn't say what this is for or how it's used}
                                (\c org.freedesktop.DBus.Error.Timeout)
    \value NoNetwork            \e {Documentation doesn't say what this is for}
                                (\c org.freedesktop.DBus.Error.NoNetwork)
    \value AddressInUse         QDBusServer tried to bind to an address that is already in use
                                (\c org.freedesktop.DBus.Error.AddressInUse)
    \value Disconnected         The call/process/message was sent after QDBusConnection disconnected
                                (\c org.freedesktop.DBus.Error.Disconnected)
    \value InvalidArgs          The arguments passed to this call/operation are not valid
                                (\c org.freedesktop.DBus.Error.InvalidArgs)
    \value UnknownMethod        The method called was not found in this object/interface with the
                                given parameters (\c org.freedesktop.DBus.Error.UnknownMethod)
    \value TimedOut             \e {Documentation doesn't say...}
                                (\c org.freedesktop.DBus.Error.TimedOut)
    \value InvalidSignature     The type signature is not valid or compatible
                                (\c org.freedesktop.DBus.Error.InvalidSignature)
    \value UnknownInterface     The interface is not known
    \value InternalError        An internal error occurred
                                (\c com.trolltech.QtDBus.Error.InternalError)

*/

/*!
    \internal
    Constructs a QDBusError from a DBusError structure.
*/
QDBusError::QDBusError(const DBusError *error)
    : code(NoError)
{
    if (!error || !dbus_error_is_set(error))
        return;

    code = errorMessages()->get(error->name);
    nm = QString::fromUtf8(error->name);
    msg = QString::fromUtf8(error->message);
}

/*!
    \internal
    Constructs a QDBusError from a QDBusMessage.
*/
QDBusError::QDBusError(const QDBusMessage &qdmsg)
    : code(Other)
{
    if (qdmsg.type() != QDBusMessage::ErrorMessage)
        return;

    nm = qdmsg.name();
    if (qdmsg.count())
        msg = qdmsg[0].toString();
    code = errorMessages()->get(nm.toUtf8().constData());
}

/*!
    \internal
    Constructs a QDBusError from a well-known error code
*/
QDBusError::QDBusError(KnownErrors error, const QString &mess)
    : code(error)
{
    nm = QLatin1String(errorMessages()->get(error));
    msg = mess;
}

/*!
    \fn QDBusError::name() const
    Returns this error's name. Error names are similar to D-Bus Interface names, like
    "org.freedesktop.DBus.InvalidArgs".
*/

/*!
    \fn QDBusError::message() const
    Returns the message that the callee associated with this error. Error messages are
    implementation defined and usually contain a human-readable error code, though this does not
    mean it is suitable for your end-users.
*/

/*!
    \fn QDBusError::isValid() const
    Returns true if this is a valid error condition (i.e., if there was an error), false otherwise.
*/

/*!
    \fn QDBusError::operator==(KnownErrors error) const
    Compares this QDBusError against the well-known error code \a error and returns true if they
    match.
*/

/*!
    \fn operator==(QDBusError::KnownErrors p1, const QDBusError &p2)
    \relates QDBusError

    Compares the QDBusError \a p2 against the well-known error code \a p1 and returns true if they
    match.
*/

#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QDBusError &msg)
{
    dbg.nospace() << "QDBusError(" << msg.name() << ", " << msg.message() << ")";
    return dbg.space();
}
#endif