KAuth

actionreply.cpp
1 /*
2  SPDX-FileCopyrightText: 2009-2012 Dario Freddi <[email protected]>
3  SPDX-FileCopyrightText: 2008 Nicola Gigante <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7 
8 #include "actionreply.h"
9 
10 #include <QDebug>
11 #include <QIODevice>
12 
13 namespace KAuth
14 {
15 class ActionReplyData : public QSharedData
16 {
17 public:
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
41 {
42  return ActionReply();
43 }
45 {
47  reply.setError(-1);
48  return reply;
49 }
51 {
53  reply.setError(error);
54  return reply;
55 }
57 {
59 }
61 {
63 }
65 {
67 }
69 {
71 }
73 {
75 }
77 {
79 }
81 {
83 }
85 {
87 }
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 
117 {
118 }
119 
120 void ActionReply::setData(const QVariantMap &data)
121 {
122  d->data = data;
123 }
124 
125 void ActionReply::addData(const QString &key, const QVariant &value)
126 {
127  d->data.insert(key, value);
128 }
129 
130 QVariantMap 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 
173 void ActionReply::setError(int error)
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 
223 bool ActionReply::operator==(const ActionReply &reply) const
224 {
225  return (d->type == reply.d->type && d->errorCode == reply.d->errorCode);
226 }
227 
228 bool ActionReply::operator!=(const ActionReply &reply) const
229 {
230  return (d->type != reply.d->type || d->errorCode != reply.d->errorCode);
231 }
232 
233 } // namespace Auth
@ AuthorizationDeniedError
You don't have the authorization to execute the action.
Definition: actionreply.h:367
static const ActionReply HelperErrorReply()
An empty reply with type() == HelperError and errorCode() == -1.
Definition: actionreply.cpp:44
QString errorDescription() const
Gets a human-readble description of the error, if available.
void setType(Type type)
Sets the reply type.
Type type(const QSqlDatabase &db)
static const ActionReply AlreadyStartedReply()
errorCode() == AlreadyStartedError
Definition: actionreply.cpp:80
void addData(const QString &key, const QVariant &value)
Convenience method to add some data to the reply.
bool operator==(const ActionReply &reply) const
Comparison operator.
static const ActionReply NoResponderReply()
errorCode() == NoResponder
Definition: actionreply.cpp:56
static ActionReply deserialize(const QByteArray &data)
Deserialize a reply from a QByteArray.
Error
The enumeration of the possible values of errorCode() when type() is ActionReply::KAuthError.
Definition: actionreply.h:362
Error errorCode() const
Returns the error code of an error reply.
@ NoResponderError
The helper responder object hasn't been set. This shouldn't happen if you use the KAUTH_HELPER macro ...
Definition: actionreply.h:364
Class that encapsulates a reply coming from the helper after executing an action.
Definition: actionreply.h:334
static const ActionReply UserCancelledReply()
errorCode() == UserCancelled
Definition: actionreply.cpp:72
void setErrorCode(Error errorCode)
Sets the error code of an error reply.
bool operator!=(const ActionReply &reply) const
Negated comparison operator.
void setData(const QVariantMap &data)
Sets the custom data to send back to the application.
void setErrorDescription(const QString &error)
Sets a human-readble description of the error.
static const ActionReply HelperBusyReply()
errorCode() == HelperBusy
Definition: actionreply.cpp:76
bool succeeded() const
Returns true if type() == Success.
@ NoSuchActionError
The action you tried to execute doesn't exist.
Definition: actionreply.h:365
static const ActionReply AuthorizationDeniedReply()
errorCode() == AuthorizationDenied
Definition: actionreply.cpp:68
bool failed() const
Returns true if type() != Success.
Type type() const
Returns the reply's type.
QVariantMap data() const
Returns the custom data coming from the helper.
@ DBusError
An error from D-Bus occurred.
Definition: actionreply.h:371
static const ActionReply InvalidActionReply()
errorCode() == InvalidAction
Definition: actionreply.cpp:64
Definition: action.cpp:18
static const ActionReply DBusErrorReply()
errorCode() == DBusError
Definition: actionreply.cpp:84
@ UserCancelledError
Action execution has been cancelled by the user.
Definition: actionreply.h:368
QByteArray serialized() const
Serialize the reply into a QByteArray.
virtual ~ActionReply()
Virtual destructor.
static const ActionReply SuccessReply()
An empty successful reply. Same as using the default constructor.
Definition: actionreply.cpp:40
@ InvalidActionError
You tried to execute an invalid action object.
Definition: actionreply.h:366
@ KAuthErrorType
An error reply generated by the library itself.
Definition: actionreply.h:341
@ SuccessType
The action has been completed successfully.
Definition: actionreply.h:343
int error() const
Returns the error code of an error reply.
static const ActionReply NoSuchActionReply()
errorCode() == NoSuchAction
Definition: actionreply.cpp:60
ActionReply & operator=(const ActionReply &reply)
Assignment operator.
@ HelperBusyError
The helper is busy executing another action (or group of actions). Try later.
Definition: actionreply.h:369
@ HelperErrorType
An error reply generated by the helper.
Definition: actionreply.h:342
Type
Enumeration of the different kinds of replies.
Definition: actionreply.h:340
void setError(int error)
Sets the error code of an error reply.
@ AlreadyStartedError
The action was already started and is currently running.
Definition: actionreply.h:370
ActionReply()
Default constructor. Sets type() to Success and errorCode() to zero.
Definition: actionreply.cpp:95
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Feb 5 2023 04:14:58 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.