7#include "accountmanager.h"
8#include "accountstorage_p.h"
20AccountManager *AccountManager::sInstance =
nullptr;
22class AccountPromise::Private
31 void setError(
const QString &error)
39 this->account = account;
48 bool isRunning()
const
65 bool mRunning =
false;
69class AccountManager::Private
72 Private(AccountManager *q)
79 if (!requestedScopes.
isEmpty()) {
80 auto currentScopes = account->scopes();
81 for (
const auto &requestedScope : requestedScopes) {
82 if (!currentScopes.contains(requestedScope)) {
83 currentScopes.push_back(requestedScope);
86 if (currentScopes != account->scopes()) {
87 account->setScopes(currentScopes);
90 auto job =
new AuthJob(account, apiKey, apiSecret);
91 job->setUsername(account->accountName());
94 promise->d->setError(
tr(
"Failed to authenticate additional scopes"));
98 mStore->storeAccount(apiKey, job->account());
99 promise->d->setAccount(job->account());
106 updateAccount(promise, apiKey, apiSecret, account, {});
111 for (
const auto &scope : std::as_const(requestedScopes)) {
112 if (!currentScopes.
contains(scope)) {
119 void ensureStore(
const std::function<
void(
bool)> &callback)
122 mStore = AccountStorageFactory::instance()->create();
124 if (!mStore->opened()) {
125 mStore->open(callback);
133 const QString key = apiKey + accountName;
134 auto promise = mPendingPromises.value(key,
nullptr);
138 mPendingPromises.remove(key);
140 mPendingPromises.insert(key, promise);
146 AccountStorage *mStore =
nullptr;
151 AccountManager *
const q;
158AccountPromise::AccountPromise(
QObject *parent)
160 , d(new Private(this))
164AccountPromise::~AccountPromise()
173bool AccountPromise::hasError()
const
175 return !d->error.isNull();
178QString AccountPromise::errorText()
const
183AccountManager::AccountManager(
QObject *parent)
185 , d(new Private(this))
189AccountManager::~AccountManager()
193AccountManager *AccountManager::instance()
196 sInstance =
new AccountManager;
203 auto promise = d->createPromise(apiKey, accountName);
204 if (!promise->d->isRunning()) {
207 QTimer::singleShot(0,
this, [
this, promise, apiKey, apiSecret, accountName, scopes]() {
208 d->ensureStore([
this, promise, apiKey, apiSecret, accountName, scopes](
bool storeOpened) {
210 promise->d->setError(
tr(
"Failed to open account store"));
214 const auto account = d->mStore->getAccount(apiKey, accountName);
216 d->createAccount(promise, apiKey, apiSecret, accountName, scopes);
218 if (d->compareScopes(account->scopes(), scopes)) {
221 d->updateAccount(promise, apiKey, apiSecret, account, scopes);
223 promise->d->setAccount(account);
229 account->setAccessToken({});
230 account->setRefreshToken({});
231 account->setExpireDateTime({});
232 d->updateAccount(promise, apiKey, apiSecret, account, scopes);
237 promise->d->setRunning();
244 auto promise = d->createPromise(apiKey, accountName);
245 if (!promise->d->isRunning()) {
247 d->ensureStore([
this, apiKey, promise, apiSecret, accountName](
bool storeOpened) {
249 promise->d->setError(
tr(
"Failed to open account store"));
253 const auto account = d->mStore->getAccount(apiKey, accountName);
255 promise->d->setAccount({});
257 d->updateAccount(promise, apiKey, apiSecret, account, {});
261 promise->d->setRunning();
270 d->ensureStore([
this, promise, apiKey, accountName, scopes](
bool storeOpened) {
272 promise->d->setError(
tr(
"Failed to open account store"));
276 const auto account = d->mStore->getAccount(apiKey, accountName);
278 promise->d->setAccount({});
280 const auto currentScopes = account->scopes();
281 if (scopes.
isEmpty() || d->compareScopes(currentScopes, scopes)) {
282 promise->d->setAccount(account);
284 promise->d->setAccount({});
289 promise->d->setRunning();
295 d->ensureStore([
this, apiKey, accountName, removedScopes](
bool storeOpened) {
300 const auto account = d->mStore->getAccount(apiKey, accountName);
305 for (
const auto &scope : removedScopes) {
306 account->removeScope(scope);
308 if (account->scopes().isEmpty()) {
309 d->mStore->removeAccount(apiKey, account->accountName());
314 account->setAccessToken({});
315 account->setRefreshToken({});
316 account->setExpireDateTime({});
317 d->mStore->storeAccount(apiKey, account);
322#include "moc_accountmanager.cpp"
AccountPromise is a result of asynchronous operations of AccountManager.
void finished(KGAPI2::AccountPromise *self)
The retrieval has finished and the Account can be retrieved.
A job to authenticate against Google and fetch tokens.
void finished(KGAPI2::Job *job)
Emitted when job has finished.
A job to fetch a single map tile described by a StaticMapUrl.
@ NoError
LibKGAPI error - no error.
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QDateTime currentDateTime()
bool contains(const AT &value) const const
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString tr(const char *sourceText, const char *disambiguation, int n)
QSharedPointer< T > create(Args &&... args)