| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2009-2012 Dario Freddi <drf@kde.org> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "action.h" |
| 8 | |
| 9 | #include <QPointer> |
| 10 | #include <QRegularExpression> |
| 11 | #include <QWindow> |
| 12 | #include <QtGlobal> |
| 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 | QPointer<QWindow> parent; |
| 47 | int timeout; |
| 48 | }; |
| 49 | |
| 50 | // Constructors |
| 51 | Action::Action() |
| 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 | Action::Action(const QString &name, const DetailsMap &details) |
| 69 | : d(new ActionData()) |
| 70 | { |
| 71 | setName(name); |
| 72 | setDetailsV2(details); |
| 73 | BackendsManager::authBackend()->setupAction(d->name); |
| 74 | } |
| 75 | |
| 76 | Action::~Action() |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | // Operators |
| 81 | Action &Action::operator=(const Action &action) |
| 82 | { |
| 83 | if (this == &action) { |
| 84 | // Protect against self-assignment |
| 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | d = action.d; |
| 89 | return *this; |
| 90 | } |
| 91 | |
| 92 | bool Action::operator==(const Action &action) const |
| 93 | { |
| 94 | return d->name == action.d->name; |
| 95 | } |
| 96 | |
| 97 | bool Action::operator!=(const Action &action) const |
| 98 | { |
| 99 | return d->name != action.d->name; |
| 100 | } |
| 101 | |
| 102 | // Accessors |
| 103 | QString Action::name() const |
| 104 | { |
| 105 | return d->name; |
| 106 | } |
| 107 | |
| 108 | void Action::setName(const QString &name) |
| 109 | { |
| 110 | d->name = name; |
| 111 | } |
| 112 | |
| 113 | // Accessors |
| 114 | int Action::timeout() const |
| 115 | { |
| 116 | return d->timeout; |
| 117 | } |
| 118 | |
| 119 | void Action::setTimeout(int timeout) |
| 120 | { |
| 121 | d->timeout = timeout; |
| 122 | } |
| 123 | |
| 124 | Action::DetailsMap Action::detailsV2() const |
| 125 | { |
| 126 | return d->details; |
| 127 | } |
| 128 | |
| 129 | void Action::setDetailsV2(const DetailsMap &details) |
| 130 | { |
| 131 | d->details = details; |
| 132 | } |
| 133 | |
| 134 | bool Action::isValid() const |
| 135 | { |
| 136 | return !d->name.isEmpty(); |
| 137 | } |
| 138 | |
| 139 | void Action::setArguments(const QVariantMap &arguments) |
| 140 | { |
| 141 | d->args = arguments; |
| 142 | } |
| 143 | |
| 144 | void Action::addArgument(const QString &key, const QVariant &value) |
| 145 | { |
| 146 | d->args.insert(key, value); |
| 147 | } |
| 148 | |
| 149 | QVariantMap Action::arguments() const |
| 150 | { |
| 151 | return d->args; |
| 152 | } |
| 153 | |
| 154 | QString Action::helperId() const |
| 155 | { |
| 156 | return d->helperId; |
| 157 | } |
| 158 | |
| 159 | // TODO: Check for helper id's syntax |
| 160 | void Action::setHelperId(const QString &id) |
| 161 | { |
| 162 | d->helperId = id; |
| 163 | } |
| 164 | |
| 165 | void Action::setParentWindow(QWindow *parent) |
| 166 | { |
| 167 | d->parent = parent; |
| 168 | } |
| 169 | |
| 170 | QWindow *Action::parentWindow() const |
| 171 | { |
| 172 | return d->parent.data(); |
| 173 | } |
| 174 | |
| 175 | Action::AuthStatus Action::status() const |
| 176 | { |
| 177 | if (!isValid()) { |
| 178 | return Action::InvalidStatus; |
| 179 | } |
| 180 | |
| 181 | return BackendsManager::authBackend()->actionStatus(action: d->name); |
| 182 | } |
| 183 | |
| 184 | ExecuteJob *Action::execute(ExecutionMode mode) |
| 185 | { |
| 186 | return new ExecuteJob(*this, mode, nullptr); |
| 187 | } |
| 188 | |
| 189 | bool Action::hasHelper() const |
| 190 | { |
| 191 | return !d->helperId.isEmpty(); |
| 192 | } |
| 193 | |
| 194 | } // namespace Auth |
| 195 | |
| 196 | #include "moc_action.cpp" |
| 197 | |