aboutsummaryrefslogtreecommitdiff
path: root/lib/accountregistry.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/accountregistry.cpp')
-rw-r--r--lib/accountregistry.cpp67
1 files changed, 65 insertions, 2 deletions
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 <QtCore/QCoreApplication>
+#if QT_VERSION_MAJOR >= 6
+# include <qt6keychain/keychain.h>
+#else
+# include <qt5keychain/keychain.h>
+#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<int, QByteArray> 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;
+}