Files
QtPotato/src/translate/apikeystore.h
T

58 lines
1.4 KiB
C++

#pragma once
#include <QCoroTask>
#include <QObject>
#include <QtQml/qqmlregistration.h>
// Keeps the translation API key in the system keychain (KWallet / Secret Service)
// instead of the plain-text config file.
class ApiKeyStore : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_SINGLETON
Q_PROPERTY(bool hasKey READ hasKey NOTIFY hasKeyChanged)
Q_PROPERTY(QString error READ error NOTIFY errorChanged)
Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
public:
struct ReadResult {
QString key;
QString error;
};
explicit ApiKeyStore(QObject *parent = nullptr);
bool hasKey() const;
QString error() const;
bool busy() const;
// Stores \a key, or removes the stored key when it is empty.
Q_INVOKABLE void setKey(const QString &key);
// Repeats the failed read, write, or removal without asking for the key again.
Q_INVOKABLE void retry();
// An empty key without error means none is stored.
static QCoro::Task<ReadResult> readKey();
Q_SIGNALS:
void hasKeyChanged();
void errorChanged();
void busyChanged();
private:
QCoro::Task<> refresh();
QCoro::Task<> store(QString key);
void setHasKey(bool hasKey);
void setError(const QString &error);
void setBusy(bool busy);
bool m_hasKey = false;
QString m_error;
bool m_busy = false;
bool m_retryStore = false;
QString m_pendingKey;
};