File indexing completed on 2024-06-16 06:54:13

0001 /*
0002     SPDX-FileCopyrightText: 2009-2012 Dario Freddi <drf@kde.org>
0003     SPDX-FileCopyrightText: 2008 Nicola Gigante <nicola.gigante@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "actionreply.h"
0009 
0010 #include <QDebug>
0011 #include <QIODevice>
0012 
0013 namespace KAuth
0014 {
0015 class ActionReplyData : public QSharedData
0016 {
0017 public:
0018     ActionReplyData()
0019     {
0020     }
0021     ActionReplyData(const ActionReplyData &other)
0022         : QSharedData(other)
0023         , data(other.data)
0024         , errorCode(other.errorCode)
0025         , errorDescription(other.errorDescription)
0026         , type(other.type)
0027     {
0028     }
0029     ~ActionReplyData()
0030     {
0031     }
0032 
0033     QVariantMap data; // User-defined data for success and helper error replies, empty for kauth errors
0034     uint errorCode;
0035     QString errorDescription;
0036     ActionReply::Type type;
0037 };
0038 
0039 // Predefined replies
0040 const ActionReply ActionReply::SuccessReply()
0041 {
0042     return ActionReply();
0043 }
0044 const ActionReply ActionReply::HelperErrorReply()
0045 {
0046     ActionReply reply(ActionReply::HelperErrorType);
0047     reply.setError(-1);
0048     return reply;
0049 }
0050 const ActionReply ActionReply::HelperErrorReply(int error)
0051 {
0052     ActionReply reply(ActionReply::HelperErrorType);
0053     reply.setError(error);
0054     return reply;
0055 }
0056 const ActionReply ActionReply::NoResponderReply()
0057 {
0058     return ActionReply(ActionReply::NoResponderError);
0059 }
0060 const ActionReply ActionReply::NoSuchActionReply()
0061 {
0062     return ActionReply(ActionReply::NoSuchActionError);
0063 }
0064 const ActionReply ActionReply::InvalidActionReply()
0065 {
0066     return ActionReply(ActionReply::InvalidActionError);
0067 }
0068 const ActionReply ActionReply::AuthorizationDeniedReply()
0069 {
0070     return ActionReply(ActionReply::AuthorizationDeniedError);
0071 }
0072 const ActionReply ActionReply::UserCancelledReply()
0073 {
0074     return ActionReply(ActionReply::UserCancelledError);
0075 }
0076 const ActionReply ActionReply::HelperBusyReply()
0077 {
0078     return ActionReply(ActionReply::HelperBusyError);
0079 }
0080 const ActionReply ActionReply::AlreadyStartedReply()
0081 {
0082     return ActionReply(ActionReply::AlreadyStartedError);
0083 }
0084 const ActionReply ActionReply::DBusErrorReply()
0085 {
0086     return ActionReply(ActionReply::DBusError);
0087 }
0088 
0089 // Constructors
0090 ActionReply::ActionReply(const ActionReply &reply)
0091     : d(reply.d)
0092 {
0093 }
0094 
0095 ActionReply::ActionReply()
0096     : d(new ActionReplyData())
0097 {
0098     d->errorCode = 0;
0099     d->type = SuccessType;
0100 }
0101 
0102 ActionReply::ActionReply(ActionReply::Type type)
0103     : d(new ActionReplyData())
0104 {
0105     d->errorCode = 0;
0106     d->type = type;
0107 }
0108 
0109 ActionReply::ActionReply(int error)
0110     : d(new ActionReplyData())
0111 {
0112     d->errorCode = error;
0113     d->type = KAuthErrorType;
0114 }
0115 
0116 ActionReply::~ActionReply()
0117 {
0118 }
0119 
0120 void ActionReply::setData(const QVariantMap &data)
0121 {
0122     d->data = data;
0123 }
0124 
0125 void ActionReply::addData(const QString &key, const QVariant &value)
0126 {
0127     d->data.insert(key, value);
0128 }
0129 
0130 QVariantMap ActionReply::data() const
0131 {
0132     return d->data;
0133 }
0134 
0135 ActionReply::Type ActionReply::type() const
0136 {
0137     return d->type;
0138 }
0139 
0140 void ActionReply::setType(ActionReply::Type type)
0141 {
0142     d->type = type;
0143 }
0144 
0145 bool ActionReply::succeeded() const
0146 {
0147     return d->type == SuccessType;
0148 }
0149 
0150 bool ActionReply::failed() const
0151 {
0152     return !succeeded();
0153 }
0154 
0155 ActionReply::Error ActionReply::errorCode() const
0156 {
0157     return (ActionReply::Error)d->errorCode;
0158 }
0159 
0160 void ActionReply::setErrorCode(Error errorCode)
0161 {
0162     d->errorCode = errorCode;
0163     if (d->type != HelperErrorType) {
0164         d->type = KAuthErrorType;
0165     }
0166 }
0167 
0168 int ActionReply::error() const
0169 {
0170     return d->errorCode;
0171 }
0172 
0173 void ActionReply::setError(int error)
0174 {
0175     d->errorCode = error;
0176 }
0177 
0178 QString ActionReply::errorDescription() const
0179 {
0180     return d->errorDescription;
0181 }
0182 
0183 void ActionReply::setErrorDescription(const QString &error)
0184 {
0185     d->errorDescription = error;
0186 }
0187 
0188 QByteArray ActionReply::serialized() const
0189 {
0190     QByteArray data;
0191     QDataStream s(&data, QIODevice::WriteOnly);
0192 
0193     s << d->data << d->errorCode << static_cast<quint32>(d->type) << d->errorDescription;
0194 
0195     return data;
0196 }
0197 
0198 ActionReply ActionReply::deserialize(const QByteArray &data)
0199 {
0200     ActionReply reply;
0201     QByteArray a(data);
0202     QDataStream s(&a, QIODevice::ReadOnly);
0203 
0204     quint32 i;
0205     s >> reply.d->data >> reply.d->errorCode >> i >> reply.d->errorDescription;
0206     reply.d->type = static_cast<ActionReply::Type>(i);
0207 
0208     return reply;
0209 }
0210 
0211 // Operators
0212 ActionReply &ActionReply::operator=(const ActionReply &reply)
0213 {
0214     if (this == &reply) {
0215         // Protect against self-assignment
0216         return *this;
0217     }
0218 
0219     d = reply.d;
0220     return *this;
0221 }
0222 
0223 bool ActionReply::operator==(const ActionReply &reply) const
0224 {
0225     return (d->type == reply.d->type && d->errorCode == reply.d->errorCode);
0226 }
0227 
0228 bool ActionReply::operator!=(const ActionReply &reply) const
0229 {
0230     return (d->type != reply.d->type || d->errorCode != reply.d->errorCode);
0231 }
0232 
0233 } // namespace Auth