File indexing completed on 2024-03-24 03:55:22

0001 /*
0002     SPDX-FileCopyrightText: 2009-2012 Dario Freddi <drf@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "action.h"
0008 
0009 #include <QPointer>
0010 #include <QRegularExpression>
0011 #include <QWindow>
0012 #include <QtGlobal>
0013 
0014 #include "executejob.h"
0015 
0016 #include "BackendsManager.h"
0017 
0018 namespace KAuth
0019 {
0020 class ActionData : public QSharedData
0021 {
0022 public:
0023     ActionData()
0024         : parent(nullptr)
0025         , timeout(-1)
0026     {
0027     }
0028     ActionData(const ActionData &other)
0029         : QSharedData(other)
0030         , name(other.name)
0031         , helperId(other.helperId)
0032         , details(other.details)
0033         , args(other.args)
0034         , parent(other.parent)
0035         , timeout(other.timeout)
0036     {
0037     }
0038     ~ActionData()
0039     {
0040     }
0041 
0042     QString name;
0043     QString helperId;
0044     Action::DetailsMap details;
0045     QVariantMap args;
0046     QPointer<QWindow> parent;
0047     int timeout;
0048 };
0049 
0050 // Constructors
0051 Action::Action()
0052     : d(new ActionData())
0053 {
0054 }
0055 
0056 Action::Action(const Action &action)
0057     : d(action.d)
0058 {
0059 }
0060 
0061 Action::Action(const QString &name)
0062     : d(new ActionData())
0063 {
0064     setName(name);
0065     BackendsManager::authBackend()->setupAction(d->name);
0066 }
0067 
0068 Action::Action(const QString &name, const DetailsMap &details)
0069     : d(new ActionData())
0070 {
0071     setName(name);
0072     setDetailsV2(details);
0073     BackendsManager::authBackend()->setupAction(d->name);
0074 }
0075 
0076 Action::~Action()
0077 {
0078 }
0079 
0080 // Operators
0081 Action &Action::operator=(const Action &action)
0082 {
0083     if (this == &action) {
0084         // Protect against self-assignment
0085         return *this;
0086     }
0087 
0088     d = action.d;
0089     return *this;
0090 }
0091 
0092 bool Action::operator==(const Action &action) const
0093 {
0094     return d->name == action.d->name;
0095 }
0096 
0097 bool Action::operator!=(const Action &action) const
0098 {
0099     return d->name != action.d->name;
0100 }
0101 
0102 // Accessors
0103 QString Action::name() const
0104 {
0105     return d->name;
0106 }
0107 
0108 void Action::setName(const QString &name)
0109 {
0110     d->name = name;
0111 }
0112 
0113 // Accessors
0114 int Action::timeout() const
0115 {
0116     return d->timeout;
0117 }
0118 
0119 void Action::setTimeout(int timeout)
0120 {
0121     d->timeout = timeout;
0122 }
0123 
0124 Action::DetailsMap Action::detailsV2() const
0125 {
0126     return d->details;
0127 }
0128 
0129 void Action::setDetailsV2(const DetailsMap &details)
0130 {
0131     d->details = details;
0132 }
0133 
0134 bool Action::isValid() const
0135 {
0136     return !d->name.isEmpty();
0137 }
0138 
0139 void Action::setArguments(const QVariantMap &arguments)
0140 {
0141     d->args = arguments;
0142 }
0143 
0144 void Action::addArgument(const QString &key, const QVariant &value)
0145 {
0146     d->args.insert(key, value);
0147 }
0148 
0149 QVariantMap Action::arguments() const
0150 {
0151     return d->args;
0152 }
0153 
0154 QString Action::helperId() const
0155 {
0156     return d->helperId;
0157 }
0158 
0159 // TODO: Check for helper id's syntax
0160 void Action::setHelperId(const QString &id)
0161 {
0162     d->helperId = id;
0163 }
0164 
0165 void Action::setParentWindow(QWindow *parent)
0166 {
0167     d->parent = parent;
0168 }
0169 
0170 QWindow *Action::parentWindow() const
0171 {
0172     return d->parent.data();
0173 }
0174 
0175 Action::AuthStatus Action::status() const
0176 {
0177     if (!isValid()) {
0178         return Action::InvalidStatus;
0179     }
0180 
0181     return BackendsManager::authBackend()->actionStatus(d->name);
0182 }
0183 
0184 ExecuteJob *Action::execute(ExecutionMode mode)
0185 {
0186     return new ExecuteJob(*this, mode, nullptr);
0187 }
0188 
0189 bool Action::hasHelper() const
0190 {
0191     return !d->helperId.isEmpty();
0192 }
0193 
0194 } // namespace Auth
0195 
0196 #include "moc_action.cpp"