8#include "accountstorage_kwallet_p.h"
15#include <QJsonDocument>
23static const QString FolderName = QStringLiteral(
"LibKGAPI");
24static const QString AccountNameKey = QStringLiteral(
"name");
25static const QString ScopesKey = QStringLiteral(
"scopes");
26static const QString AccessTokenKey = QStringLiteral(
"accessToken");
27static const QString RefreshTokenKey = QStringLiteral(
"refreshToken");
28static const QString ExpiresKey = QStringLiteral(
"expires");
32AccountStorage *KWalletStorageFactory::create()
const
34 return new KWalletStorage();
37KWalletStorage::KWalletStorage()
41KWalletStorage::~KWalletStorage()
46void KWalletStorage::open(
const std::function<
void(
bool)> &callback)
48 const auto openedCallback = [
this, callback](
bool opened) {
49 mWalletOpening =
false;
51 qCWarning(KGAPIDebug,
"KWallet: failed to open");
52 mWallet->deleteLater();
58 if (mWallet->currentFolder() == FolderName) {
63 if (!mWallet->hasFolder(FolderName)) {
64 if (!mWallet->createFolder(FolderName)) {
65 qCWarning(KGAPIDebug,
"KWallet: failed to create a new folder");
70 if (!mWallet->setFolder(FolderName)) {
71 qCWarning(KGAPIDebug,
"KWallet: failed to change folder");
93 mWalletOpening =
true;
96 qCWarning(KGAPIDebug,
"KWallet: failed to open wallet (maybe it's disabled?");
100 mWalletOpening =
false;
106bool KWalletStorage::opened()
const
114 qCWarning(KGAPIDebug,
"Trying to get an account from a closed wallet!");
119 mWallet->readMap(apiKey, accounts);
120 const auto accountIt = accounts.
constFind(accountName);
121 if (accountIt == accounts.
cend()) {
125 return parseAccount(*accountIt);
128bool KWalletStorage::storeAccount(
const QString &apiKey,
const AccountPtr &account)
131 qCWarning(KGAPIDebug,
"Trying to store an account in a closed wallet!");
136 if (mWallet->readMap(apiKey, accounts) == 0) {
137 accounts[account->accountName()] = serializeAccount(account);
138 if (mWallet->writeMap(apiKey, accounts) != 0) {
139 qCWarning(KGAPIDebug,
"KWallet: failed to write accounts map");
143 qCWarning(KGAPIDebug,
"KWallet: failed to read accounts map");
150void KWalletStorage::removeAccount(
const QString &apiKey,
const QString &accountName)
153 qCWarning(KGAPIDebug,
"Trying to remove an account from a closed wallet!");
158 if (mWallet->readMap(apiKey, accounts) == 0) {
159 if (accounts.
remove(accountName)) {
160 if (!mWallet->writeMap(apiKey, accounts)) {
161 qCWarning(KGAPIDebug,
"KWallet: failed to write accounts map");
165 qCWarning(KGAPIDebug,
"KWallet: failed to read accounts map");
173 qCWarning(KGAPIDebug,
"Failed to parse account returned from KWallet");
176 const auto obj = doc.object();
177 QJsonArray scopesArray = obj.value(ScopesKey).toArray();
181 return QUrl::fromEncoded(val.toString().toUtf8());
183 auto acc =
AccountPtr::create(obj.value(AccountNameKey).toString(), obj.value(AccessTokenKey).toString(), obj.value(RefreshTokenKey).toString(), scopes);
191 const auto scopes = account->scopes();
192 std::transform(scopes.
cbegin(), scopes.
cend(), std::back_inserter(scopesArray), [](
const QUrl &scope) {
193 return scope.toString(QUrl::FullyEncoded);
196 {AccessTokenKey, account->accessToken()},
197 {RefreshTokenKey, account->refreshToken()},
198 {ExpiresKey, account->expireDateTime().toString(
Qt::ISODate)},
199 {ScopesKey, scopesArray}})
static const QString NetworkWallet()
static Wallet * openWallet(const QString &name, WId w, OpenType ot=Synchronous)
void walletOpened(bool success)
A job to fetch a single map tile described by a StaticMapUrl.
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
const_iterator constBegin() const const
const_iterator constEnd() const const
qsizetype size() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QByteArray toJson(JsonFormat format) const const
const_iterator cbegin() const const
const_iterator cend() const const
void reserve(qsizetype size)
const_iterator cend() const const
const_iterator constFind(const Key &key) const const
size_type remove(const Key &key)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QSharedPointer< T > create(Args &&... args)
QString fromUtf8(QByteArrayView str)
QByteArray toUtf8() const const