KAuth

action.cpp
1 /*
2  SPDX-FileCopyrightText: 2009-2012 Dario Freddi <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6 
7 #include "action.h"
8 
9 #include <QRegularExpression>
10 #include <QtGlobal>
11 
12 class QWidget;
13 
14 #include "executejob.h"
15 
16 #include "BackendsManager.h"
17 
18 namespace KAuth
19 {
20 class ActionData : public QSharedData
21 {
22 public:
23  ActionData()
24  : parent(nullptr)
25  , timeout(-1)
26  {
27  }
28  ActionData(const ActionData &other)
29  : QSharedData(other)
30  , name(other.name)
31  , helperId(other.helperId)
32  , details(other.details)
33  , args(other.args)
34  , parent(other.parent)
35  , timeout(other.timeout)
36  {
37  }
38  ~ActionData()
39  {
40  }
41 
42  QString name;
43  QString helperId;
44  Action::DetailsMap details;
45  QVariantMap args;
46  QWidget *parent = nullptr;
47  int timeout;
48 };
49 
50 // Constructors
52  : d(new ActionData())
53 {
54 }
55 
56 Action::Action(const Action &action)
57  : d(action.d)
58 {
59 }
60 
61 Action::Action(const QString &name)
62  : d(new ActionData())
63 {
64  setName(name);
65  BackendsManager::authBackend()->setupAction(d->name);
66 }
67 
68 #if KAUTHCORE_BUILD_DEPRECATED_SINCE(5, 71)
69 Action::Action(const QString &name, const QString &details)
70  : Action(name, DetailsMap{{AuthDetail::DetailOther, details}})
71 {
72 }
73 #endif
74 
75 Action::Action(const QString &name, const DetailsMap &details)
76  : d(new ActionData())
77 {
78  setName(name);
80  BackendsManager::authBackend()->setupAction(d->name);
81 }
82 
84 {
85 }
86 
87 // Operators
89 {
90  if (this == &action) {
91  // Protect against self-assignment
92  return *this;
93  }
94 
95  d = action.d;
96  return *this;
97 }
98 
99 bool Action::operator==(const Action &action) const
100 {
101  return d->name == action.d->name;
102 }
103 
104 bool Action::operator!=(const Action &action) const
105 {
106  return d->name != action.d->name;
107 }
108 
109 // Accessors
111 {
112  return d->name;
113 }
114 
115 void Action::setName(const QString &name)
116 {
117  d->name = name;
118 }
119 
120 // Accessors
121 int Action::timeout() const
122 {
123  return d->timeout;
124 }
125 
126 void Action::setTimeout(int timeout)
127 {
128  d->timeout = timeout;
129 }
130 
131 #if KAUTHCORE_BUILD_DEPRECATED_SINCE(5, 71)
133 {
134  return d->details.value(AuthDetail::DetailOther).toString();
135 }
136 #endif
137 
138 #if KAUTHCORE_BUILD_DEPRECATED_SINCE(5, 71)
139 void Action::setDetails(const QString &details)
140 {
141  d->details.clear();
142  d->details.insert(AuthDetail::DetailOther, details);
143 }
144 #endif
145 
147 {
148  return d->details;
149 }
150 
151 void Action::setDetailsV2(const DetailsMap &details)
152 {
153  d->details = details;
154 }
155 
156 bool Action::isValid() const
157 {
158  if (d->name.isEmpty()) {
159  return false;
160  }
161 
162  // Does the backend support checking for known actions?
163  if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::CheckActionExistenceCapability) {
164  // In this case, just ask the backend
165  return BackendsManager::authBackend()->actionExists(name());
166  } else {
167  // Otherwise, check through a regexp
168  const QRegularExpression re(QRegularExpression::anchoredPattern(QStringLiteral("[0-z]+(\\.[0-z]+)*")));
169  return re.match(name()).hasMatch();
170  }
171 }
172 
173 void Action::setArguments(const QVariantMap &arguments)
174 {
175  d->args = arguments;
176 }
177 
178 void Action::addArgument(const QString &key, const QVariant &value)
179 {
180  d->args.insert(key, value);
181 }
182 
183 QVariantMap Action::arguments() const
184 {
185  return d->args;
186 }
187 
189 {
190  return d->helperId;
191 }
192 
193 // TODO: Check for helper id's syntax
195 {
196  d->helperId = id;
197 }
198 
200 {
201  d->parent = parent;
202 }
203 
205 {
206  return d->parent;
207 }
208 
210 {
211  if (!isValid()) {
212  return Action::InvalidStatus;
213  }
214 
215  return BackendsManager::authBackend()->actionStatus(d->name);
216 }
217 
218 ExecuteJob *Action::execute(ExecutionMode mode)
219 {
220  return new ExecuteJob(*this, mode, nullptr);
221 }
222 
223 bool Action::hasHelper() const
224 {
225  return !d->helperId.isEmpty();
226 }
227 
228 } // namespace Auth
QString anchoredPattern(const QString &expression)
void setDetailsV2(const DetailsMap &details)
Sets the action's details.
Definition: action.cpp:151
void setArguments(const QVariantMap &arguments)
Sets the map object used to pass arguments to the helper.
Definition: action.cpp:173
void setParentWidget(QWidget *parent)
Sets a parent widget for the authentication dialog.
Definition: action.cpp:199
QWidget * parentWidget() const
Returns the parent widget for the authentication dialog for this action.
Definition: action.cpp:204
AuthStatus status() const
Gets information about the authorization status of an action.
Definition: action.cpp:209
QString name() const
Gets the action's name.
Definition: action.cpp:110
Job for executing an Action.
Definition: executejob.h:39
AuthStatus
The three values set by authorization methods.
Definition: action.h:82
void setName(const QString &name)
Sets the action's name.
Definition: action.cpp:115
ExecuteJob * execute(ExecutionMode mode=ExecuteMode)
Get the job object used to execute the action.
Definition: action.cpp:218
bool isValid() const
Returns if the object represents a valid action.
Definition: action.cpp:156
void setDetails(const QString &details)
Sets the action's details.
Definition: action.cpp:139
QString details() const
Gets the action's details.
Definition: action.cpp:132
QVariantMap arguments() const
Returns map object used to pass arguments to the helper.
Definition: action.cpp:183
bool hasMatch() const const
void setTimeout(int timeout)
Sets the action's timeout.
Definition: action.cpp:126
Action & operator=(const Action &action)
Assignment operator.
Definition: action.cpp:88
QRegularExpressionMatch match(const QString &subject, int offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
DetailsMap detailsV2() const
Gets the action's details.
Definition: action.cpp:146
Definition: action.cpp:18
@ InvalidStatus
An invalid action cannot be authorized.
Definition: action.h:85
bool operator!=(const Action &action) const
Negated comparison operator.
Definition: action.cpp:104
Class to access, authorize and execute actions.
Definition: action.h:75
int timeout() const
Gets the action's timeout.
Definition: action.cpp:121
void addArgument(const QString &key, const QVariant &value)
Convenience method to add an argument.
Definition: action.cpp:178
void setHelperId(const QString &id)
Sets the default helper ID used for actions execution.
Definition: action.cpp:194
bool operator==(const Action &action) const
Comparison operator.
Definition: action.cpp:99
~Action()
Virtual destructor.
Definition: action.cpp:83
bool hasHelper() const
Checks if the action has an helper.
Definition: action.cpp:223
QString helperId() const
Gets the default helper ID used for actions execution.
Definition: action.cpp:188
Action()
Default constructor.
Definition: action.cpp:51
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 04:08:19 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.