KAuth

actionreply.cpp
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;
37};
38
39// Predefined replies
45{
47 reply.setError(-1);
48 return reply;
49}
51{
53 reply.setError(error);
54 return reply;
55}
88
89// Constructors
91 : d(reply.d)
92{
93}
94
96 : d(new ActionReplyData())
97{
98 d->errorCode = 0;
99 d->type = SuccessType;
100}
101
103 : d(new ActionReplyData())
104{
105 d->errorCode = 0;
106 d->type = type;
107}
108
110 : d(new ActionReplyData())
111{
112 d->errorCode = error;
113 d->type = KAuthErrorType;
114}
115
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
136{
137 return d->type;
138}
139
141{
142 d->type = type;
143}
144
146{
147 return d->type == SuccessType;
148}
149
151{
152 return !succeeded();
153}
154
156{
157 return (ActionReply::Error)d->errorCode;
158}
159
161{
162 d->errorCode = errorCode;
163 if (d->type != HelperErrorType) {
164 d->type = KAuthErrorType;
165 }
166}
167
169{
170 return d->errorCode;
171}
172
174{
175 d->errorCode = error;
176}
177
179{
180 return d->errorDescription;
181}
182
184{
185 d->errorDescription = error;
186}
187
189{
192
193 s << d->data << d->errorCode << static_cast<quint32>(d->type) << d->errorDescription;
194
195 return data;
196}
197
199{
200 ActionReply reply;
201 QByteArray a(data);
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
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
Class that encapsulates a reply coming from the helper after executing an action.
Error errorCode() const
Returns the error code of an error reply.
static const ActionReply SuccessReply()
An empty successful reply. Same as using the default constructor.
static const ActionReply InvalidActionReply()
errorCode() == InvalidAction
bool operator!=(const ActionReply &reply) const
Negated comparison operator.
virtual ~ActionReply()
Virtual destructor.
ActionReply & operator=(const ActionReply &reply)
Assignment operator.
void addData(const QString &key, const QVariant &value)
Convenience method to add some data to the reply.
static const ActionReply NoSuchActionReply()
errorCode() == NoSuchAction
bool operator==(const ActionReply &reply) const
Comparison operator.
void setType(Type type)
Sets the reply type.
void setErrorDescription(const QString &error)
Sets a human-readble description of the error.
static const ActionReply HelperErrorReply()
An empty reply with type() == HelperError and errorCode() == -1.
QString errorDescription() const
Gets a human-readble description of the error, if available.
static const ActionReply DBusErrorReply()
errorCode() == DBusError
Error
The enumeration of the possible values of errorCode() when type() is ActionReply::KAuthError.
@ DBusError
An error from D-Bus occurred.
@ AuthorizationDeniedError
You don't have the authorization to execute the action.
@ InvalidActionError
You tried to execute an invalid action object.
@ NoResponderError
The helper responder object hasn't been set. This shouldn't happen if you use the KAUTH_HELPER macro ...
@ HelperBusyError
The helper is busy executing another action (or group of actions). Try later.
@ NoSuchActionError
The action you tried to execute doesn't exist.
@ UserCancelledError
Action execution has been cancelled by the user.
@ AlreadyStartedError
The action was already started and is currently running.
Type type() const
Returns the reply's type.
void setErrorCode(Error errorCode)
Sets the error code of an error reply.
ActionReply()
Default constructor. Sets type() to Success and errorCode() to zero.
static const ActionReply NoResponderReply()
errorCode() == NoResponder
bool succeeded() const
Returns true if type() == Success.
static const ActionReply HelperBusyReply()
errorCode() == HelperBusy
static ActionReply deserialize(const QByteArray &data)
Deserialize a reply from a QByteArray.
static const ActionReply UserCancelledReply()
errorCode() == UserCancelled
Type
Enumeration of the different kinds of replies.
@ KAuthErrorType
An error reply generated by the library itself.
@ HelperErrorType
An error reply generated by the helper.
@ SuccessType
The action has been completed successfully.
static const ActionReply AuthorizationDeniedReply()
errorCode() == AuthorizationDenied
void setError(int error)
Sets the error code of an error reply.
int error() const
Returns the error code of an error reply.
static const ActionReply AlreadyStartedReply()
errorCode() == AlreadyStartedError
void setData(const QVariantMap &data)
Sets the custom data to send back to the application.
QByteArray serialized() const
Serialize the reply into a QByteArray.
QVariantMap data() const
Returns the custom data coming from the helper.
bool failed() const
Returns true if type() != Success.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.