1/*
2 SPDX-FileCopyrightText: 2009-2012 Dario Freddi <drf@kde.org>
3 SPDX-FileCopyrightText: 2008 Nicola Gigante <nicola.gigante@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7
8#include "actionreply.h"
9
10#include <QDebug>
11#include <QIODevice>
12
13namespace KAuth
14{
15class ActionReplyData : public QSharedData
16{
17public:
18 ActionReplyData()
19 {
20 }
21 ActionReplyData(const ActionReplyData &other)
22 : QSharedData(other)
23 , data(other.data)
24 , errorCode(other.errorCode)
25 , errorDescription(other.errorDescription)
26 , type(other.type)
27 {
28 }
29 ~ActionReplyData()
30 {
31 }
32
33 QVariantMap data; // User-defined data for success and helper error replies, empty for kauth errors
34 uint errorCode;
35 QString errorDescription;
36 ActionReply::Type type;
37};
38
39// Predefined replies
40const ActionReply ActionReply::SuccessReply()
41{
42 return ActionReply();
43}
44const ActionReply ActionReply::HelperErrorReply()
45{
46 ActionReply reply(ActionReply::HelperErrorType);
47 reply.setError(-1);
48 return reply;
49}
50const ActionReply ActionReply::HelperErrorReply(int error)
51{
52 ActionReply reply(ActionReply::HelperErrorType);
53 reply.setError(error);
54 return reply;
55}
56const ActionReply ActionReply::NoResponderReply()
57{
58 return ActionReply(ActionReply::NoResponderError);
59}
60const ActionReply ActionReply::NoSuchActionReply()
61{
62 return ActionReply(ActionReply::NoSuchActionError);
63}
64const ActionReply ActionReply::InvalidActionReply()
65{
66 return ActionReply(ActionReply::InvalidActionError);
67}
68const ActionReply ActionReply::AuthorizationDeniedReply()
69{
70 return ActionReply(ActionReply::AuthorizationDeniedError);
71}
72const ActionReply ActionReply::UserCancelledReply()
73{
74 return ActionReply(ActionReply::UserCancelledError);
75}
76const ActionReply ActionReply::HelperBusyReply()
77{
78 return ActionReply(ActionReply::HelperBusyError);
79}
80const ActionReply ActionReply::AlreadyStartedReply()
81{
82 return ActionReply(ActionReply::AlreadyStartedError);
83}
84const ActionReply ActionReply::DBusErrorReply()
85{
86 return ActionReply(ActionReply::DBusError);
87}
88
89// Constructors
90ActionReply::ActionReply(const ActionReply &reply)
91 : d(reply.d)
92{
93}
94
95ActionReply::ActionReply()
96 : d(new ActionReplyData())
97{
98 d->errorCode = 0;
99 d->type = SuccessType;
100}
101
102ActionReply::ActionReply(ActionReply::Type type)
103 : d(new ActionReplyData())
104{
105 d->errorCode = 0;
106 d->type = type;
107}
108
109ActionReply::ActionReply(int error)
110 : d(new ActionReplyData())
111{
112 d->errorCode = error;
113 d->type = KAuthErrorType;
114}
115
116ActionReply::~ActionReply()
117{
118}
119
120void ActionReply::setData(const QVariantMap &data)
121{
122 d->data = data;
123}
124
125void ActionReply::addData(const QString &key, const QVariant &value)
126{
127 d->data.insert(key, value);
128}
129
130QVariantMap ActionReply::data() const
131{
132 return d->data;
133}
134
135ActionReply::Type ActionReply::type() const
136{
137 return d->type;
138}
139
140void ActionReply::setType(ActionReply::Type type)
141{
142 d->type = type;
143}
144
145bool ActionReply::succeeded() const
146{
147 return d->type == SuccessType;
148}
149
150bool ActionReply::failed() const
151{
152 return !succeeded();
153}
154
155ActionReply::Error ActionReply::errorCode() const
156{
157 return (ActionReply::Error)d->errorCode;
158}
159
160void ActionReply::setErrorCode(Error errorCode)
161{
162 d->errorCode = errorCode;
163 if (d->type != HelperErrorType) {
164 d->type = KAuthErrorType;
165 }
166}
167
168int ActionReply::error() const
169{
170 return d->errorCode;
171}
172
173void ActionReply::setError(int error)
174{
175 d->errorCode = error;
176}
177
178QString ActionReply::errorDescription() const
179{
180 return d->errorDescription;
181}
182
183void ActionReply::setErrorDescription(const QString &error)
184{
185 d->errorDescription = error;
186}
187
188QByteArray ActionReply::serialized() const
189{
190 QByteArray data;
191 QDataStream s(&data, QIODevice::WriteOnly);
192
193 s << d->data << d->errorCode << static_cast<quint32>(d->type) << d->errorDescription;
194
195 return data;
196}
197
198ActionReply ActionReply::deserialize(const QByteArray &data)
199{
200 ActionReply reply;
201 QByteArray a(data);
202 QDataStream s(&a, QIODevice::ReadOnly);
203
204 quint32 i;
205 s >> reply.d->data >> reply.d->errorCode >> i >> reply.d->errorDescription;
206 reply.d->type = static_cast<ActionReply::Type>(i);
207
208 return reply;
209}
210
211// Operators
212ActionReply &ActionReply::operator=(const ActionReply &reply)
213{
214 if (this == &reply) {
215 // Protect against self-assignment
216 return *this;
217 }
218
219 d = reply.d;
220 return *this;
221}
222
223bool ActionReply::operator==(const ActionReply &reply) const
224{
225 return (d->type == reply.d->type && d->errorCode == reply.d->errorCode);
226}
227
228bool ActionReply::operator!=(const ActionReply &reply) const
229{
230 return (d->type != reply.d->type || d->errorCode != reply.d->errorCode);
231}
232
233} // namespace Auth
234

source code of kauth/src/actionreply.cpp