From 946cd4cb73f95526aa09777afc2a493610d9696d Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Fri, 27 May 2022 21:03:31 +0200 Subject: Load and store accounts in the keychain --- lib/accountregistry.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) (limited to 'lib/accountregistry.cpp') diff --git a/lib/accountregistry.cpp b/lib/accountregistry.cpp index 616b54b4..9b6114d9 100644 --- a/lib/accountregistry.cpp +++ b/lib/accountregistry.cpp @@ -5,7 +5,13 @@ #include "accountregistry.h" #include "connection.h" +#include +#if QT_VERSION_MAJOR >= 6 +# include +#else +# include +#endif using namespace Quotient; void AccountRegistry::add(Connection* a) @@ -15,6 +21,7 @@ void AccountRegistry::add(Connection* a) beginInsertRows(QModelIndex(), size(), size()); push_back(a); endInsertRows(); + Q_EMIT accountCountChanged(); } void AccountRegistry::drop(Connection* a) @@ -54,8 +61,6 @@ QHash AccountRegistry::roleNames() const return { { AccountRole, "connection" } }; } - - Connection* AccountRegistry::get(const QString& userId) { for (const auto &connection : *this) { @@ -64,3 +69,61 @@ Connection* AccountRegistry::get(const QString& userId) } return nullptr; } + +QKeychain::ReadPasswordJob* AccountRegistry::loadAccessTokenFromKeychain(const QString& userId) +{ + qCDebug(MAIN) << "Reading access token from keychain for" << userId; + auto job = new QKeychain::ReadPasswordJob(qAppName(), this); + job->setKey(userId); + + connect(job, &QKeychain::Job::finished, this, [job]() { + if (job->error() == QKeychain::Error::NoError) { + return; + } + //TODO error handling + }); + job->start(); + + return job; +} + +void AccountRegistry::invokeLogin() +{ + const auto accounts = SettingsGroup("Accounts").childGroups(); + for (const auto& accountId : accounts) { + AccountSettings account{accountId}; + m_accountsLoading += accountId; + Q_EMIT accountsLoadingChanged(); + + if (!account.homeserver().isEmpty()) { + auto accessTokenLoadingJob = loadAccessTokenFromKeychain(account.userId()); + connect(accessTokenLoadingJob, &QKeychain::Job::finished, this, [accountId, this, accessTokenLoadingJob]() { + AccountSettings account{accountId}; + if (accessTokenLoadingJob->error() != QKeychain::Error::NoError) { + //TODO error handling + return; + } + + auto connection = new Connection(account.homeserver()); + connect(connection, &Connection::connected, this, [connection] { + connection->loadState(); + connection->setLazyLoading(true); + + connection->syncLoop(); + }); + connect(connection, &Connection::loginError, this, [](const QString& error, const QString&) { + //TODO error handling + }); + connect(connection, &Connection::networkError, this, [](const QString& error, const QString&, int, int) { + //TODO error handling + }); + connection->assumeIdentity(account.userId(), accessTokenLoadingJob->binaryData(), account.deviceId()); + }); + } + } +} + +QStringList AccountRegistry::accountsLoading() const +{ + return m_accountsLoading; +} -- cgit v1.2.3