File indexing completed on 2024-04-28 05:34:17

0001 // SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
0002 //
0003 // SPDX-License-Identifier: LGPL-2.1-or-later
0004 
0005 #ifndef PROVIDERBASE_H_
0006 #define PROVIDERBASE_H_
0007 
0008 #include <QObject>
0009 #include <QTimer>
0010 
0011 #include "klipperutils.h"
0012 
0013 #include <memory>
0014 
0015 class QDBusPendingCallWatcher;
0016 class KJob;
0017 class QMimeData;
0018 
0019 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0020 namespace Plasma
0021 #else
0022 namespace Plasma5Support
0023 #endif
0024 {
0025 class DataEngineConsumer;
0026 }
0027 
0028 namespace PlasmaPass
0029 {
0030 class PasswordsModel;
0031 
0032 class ProviderBase : public QObject
0033 {
0034     Q_OBJECT
0035 
0036     Q_PROPERTY(bool valid READ isValid NOTIFY validChanged)
0037     Q_PROPERTY(int timeout READ timeout NOTIFY timeoutChanged)
0038     Q_PROPERTY(int defaultTimeout READ defaultTimeout CONSTANT)
0039     Q_PROPERTY(QString secret READ secret NOTIFY secretChanged)
0040     Q_PROPERTY(bool hasError READ hasError NOTIFY errorChanged)
0041     Q_PROPERTY(QString error READ error NOTIFY errorChanged)
0042 
0043     friend class PasswordsModel;
0044 public:
0045     ~ProviderBase() override;
0046 
0047     QString secret() const;
0048     bool isValid() const;
0049     int timeout() const;
0050     int defaultTimeout() const; // in milliseconds
0051     bool hasError() const;
0052     QString error() const;
0053 
0054 public Q_SLOTS:
0055     void reset();
0056 
0057 Q_SIGNALS:
0058     void secretChanged();
0059     void validChanged();
0060     void timeoutChanged();
0061     void errorChanged();
0062 
0063 protected:
0064     explicit ProviderBase(const QString &path, QObject *parent = nullptr);
0065 
0066     void setSecret(const QString &secret);
0067     void setSecretTimeout(std::chrono::seconds timeout);
0068     void setError(const QString &error);
0069 
0070     enum class HandlingResult {
0071         Continue,
0072         Stop
0073     };
0074     virtual HandlingResult handleSecret(QStringView secret) = 0;
0075 
0076 private Q_SLOTS:
0077     void start();
0078     void onPlasmaServiceRemovePasswordResult(KJob *job);
0079 
0080 private:
0081     void expireSecret();
0082 
0083     void removePasswordFromClipboard(const QString &password);
0084     static void clearClipboard();
0085 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0086     std::unique_ptr<Plasma::DataEngineConsumer> mEngineConsumer;
0087 #else
0088     std::unique_ptr<Plasma5Support::DataEngineConsumer> mEngineConsumer;
0089 #endif
0090     QString mPath;
0091     QString mError;
0092     QString mSecret;
0093     QTimer mTimer;
0094     int mTimeout = 0;
0095     std::chrono::seconds mSecretTimeout;
0096 
0097     static KlipperUtils::State sKlipperState;
0098 };
0099 
0100 }
0101 #endif