1 /*
2  * SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@kde.org>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5  */
6 
7 #include "fakeaccountstorage.h"
8 #include "account.h"
9 
10 #include <QUuid>
11 #include <QDateTime>
12 
13 using namespace KGAPI2;
14 
FakeAccountStorage()15 FakeAccountStorage::FakeAccountStorage()
16 {
17 }
18 
open(const std::function<void (bool)> & callback)19 void FakeAccountStorage::open(const std::function<void (bool)> &callback)
20 {
21     callback(true);
22 }
23 
opened() const24 bool FakeAccountStorage::opened() const
25 {
26     return true;
27 }
28 
getAccount(const QString & apiKey,const QString & accountName)29 AccountPtr FakeAccountStorage::getAccount(const QString &apiKey, const QString &accountName)
30 {
31     return mStore.value(apiKey + accountName, {});
32 }
33 
storeAccount(const QString & apiKey,const KGAPI2::AccountPtr & account)34 bool FakeAccountStorage::storeAccount(const QString &apiKey, const KGAPI2::AccountPtr &account)
35 {
36     mStore[apiKey + account->accountName()] = account;
37     return true;
38 }
39 
removeAccount(const QString & apiKey,const QString & accountName)40 void FakeAccountStorage::removeAccount(const QString &apiKey, const QString &accountName)
41 {
42     mStore.remove(apiKey + accountName);
43 }
44 
generateAccount(const QString & apiKey,const QString & accountName,const QList<QUrl> & scopes)45 AccountPtr FakeAccountStorage::generateAccount(const QString &apiKey, const QString &accountName, const QList<QUrl> &scopes)
46 {
47     const auto acc = AccountPtr::create(accountName, QUuid::createUuid().toString(), QUuid::createUuid().toString(), scopes);
48     acc->setExpireDateTime(QDateTime::currentDateTime().addDays(1));
49     storeAccount(apiKey, acc);
50     return acc;
51 }
52 
53 
FakeAccountStorageFactory()54 FakeAccountStorageFactory::FakeAccountStorageFactory()
55 {
56     mStore = new FakeAccountStorage();
57     sFactory = this;
58 }
59 
~FakeAccountStorageFactory()60 FakeAccountStorageFactory::~FakeAccountStorageFactory()
61 {
62     delete mStore;
63     sFactory = nullptr;
64 }
65 
create() const66 KGAPI2::AccountStorage *FakeAccountStorageFactory::create() const
67 {
68     return mStore;
69 }
70 
fakeStore() const71 FakeAccountStorage *FakeAccountStorageFactory::fakeStore() const
72 {
73     return mStore;
74 }
75 
76