File indexing completed on 2025-02-16 13:00:40
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 <QRegularExpression> 0010 #include <QtGlobal> 0011 0012 class QWidget; 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 QWidget *parent = nullptr; 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 #if KAUTHCORE_BUILD_DEPRECATED_SINCE(5, 71) 0069 Action::Action(const QString &name, const QString &details) 0070 : Action(name, DetailsMap{{AuthDetail::DetailOther, details}}) 0071 { 0072 } 0073 #endif 0074 0075 Action::Action(const QString &name, const DetailsMap &details) 0076 : d(new ActionData()) 0077 { 0078 setName(name); 0079 setDetailsV2(details); 0080 BackendsManager::authBackend()->setupAction(d->name); 0081 } 0082 0083 Action::~Action() 0084 { 0085 } 0086 0087 // Operators 0088 Action &Action::operator=(const Action &action) 0089 { 0090 if (this == &action) { 0091 // Protect against self-assignment 0092 return *this; 0093 } 0094 0095 d = action.d; 0096 return *this; 0097 } 0098 0099 bool Action::operator==(const Action &action) const 0100 { 0101 return d->name == action.d->name; 0102 } 0103 0104 bool Action::operator!=(const Action &action) const 0105 { 0106 return d->name != action.d->name; 0107 } 0108 0109 // Accessors 0110 QString Action::name() const 0111 { 0112 return d->name; 0113 } 0114 0115 void Action::setName(const QString &name) 0116 { 0117 d->name = name; 0118 } 0119 0120 // Accessors 0121 int Action::timeout() const 0122 { 0123 return d->timeout; 0124 } 0125 0126 void Action::setTimeout(int timeout) 0127 { 0128 d->timeout = timeout; 0129 } 0130 0131 #if KAUTHCORE_BUILD_DEPRECATED_SINCE(5, 71) 0132 QString Action::details() const 0133 { 0134 return d->details.value(AuthDetail::DetailOther).toString(); 0135 } 0136 #endif 0137 0138 #if KAUTHCORE_BUILD_DEPRECATED_SINCE(5, 71) 0139 void Action::setDetails(const QString &details) 0140 { 0141 d->details.clear(); 0142 d->details.insert(AuthDetail::DetailOther, details); 0143 } 0144 #endif 0145 0146 Action::DetailsMap Action::detailsV2() const 0147 { 0148 return d->details; 0149 } 0150 0151 void Action::setDetailsV2(const DetailsMap &details) 0152 { 0153 d->details = details; 0154 } 0155 0156 bool Action::isValid() const 0157 { 0158 if (d->name.isEmpty()) { 0159 return false; 0160 } 0161 0162 // Does the backend support checking for known actions? 0163 if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::CheckActionExistenceCapability) { 0164 // In this case, just ask the backend 0165 return BackendsManager::authBackend()->actionExists(name()); 0166 } else { 0167 // Otherwise, check through a regexp 0168 const QRegularExpression re(QRegularExpression::anchoredPattern(QStringLiteral("[0-z]+(\\.[0-z]+)*"))); 0169 return re.match(name()).hasMatch(); 0170 } 0171 } 0172 0173 void Action::setArguments(const QVariantMap &arguments) 0174 { 0175 d->args = arguments; 0176 } 0177 0178 void Action::addArgument(const QString &key, const QVariant &value) 0179 { 0180 d->args.insert(key, value); 0181 } 0182 0183 QVariantMap Action::arguments() const 0184 { 0185 return d->args; 0186 } 0187 0188 QString Action::helperId() const 0189 { 0190 return d->helperId; 0191 } 0192 0193 // TODO: Check for helper id's syntax 0194 void Action::setHelperId(const QString &id) 0195 { 0196 d->helperId = id; 0197 } 0198 0199 void Action::setParentWidget(QWidget *parent) 0200 { 0201 d->parent = parent; 0202 } 0203 0204 QWidget *Action::parentWidget() const 0205 { 0206 return d->parent; 0207 } 0208 0209 Action::AuthStatus Action::status() const 0210 { 0211 if (!isValid()) { 0212 return Action::InvalidStatus; 0213 } 0214 0215 return BackendsManager::authBackend()->actionStatus(d->name); 0216 } 0217 0218 ExecuteJob *Action::execute(ExecutionMode mode) 0219 { 0220 return new ExecuteJob(*this, mode, nullptr); 0221 } 0222 0223 bool Action::hasHelper() const 0224 { 0225 return !d->helperId.isEmpty(); 0226 } 0227 0228 } // namespace Auth 0229 0230 #include "moc_action.cpp"