aboutsummaryrefslogtreecommitdiff
path: root/lib/accountregistry.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/accountregistry.cpp')
-rw-r--r--lib/accountregistry.cpp83
1 files changed, 77 insertions, 6 deletions
diff --git a/lib/accountregistry.cpp b/lib/accountregistry.cpp
index 616b54b4..ad7c5f99 100644
--- a/lib/accountregistry.cpp
+++ b/lib/accountregistry.cpp
@@ -5,6 +5,7 @@
#include "accountregistry.h"
#include "connection.h"
+#include <QtCore/QCoreApplication>
using namespace Quotient;
@@ -15,14 +16,16 @@ void AccountRegistry::add(Connection* a)
beginInsertRows(QModelIndex(), size(), size());
push_back(a);
endInsertRows();
+ emit accountCountChanged();
}
void AccountRegistry::drop(Connection* a)
{
- const auto idx = indexOf(a);
- beginRemoveRows(QModelIndex(), idx, idx);
- remove(idx);
- endRemoveRows();
+ if (const auto idx = indexOf(a); idx != -1) {
+ beginRemoveRows(QModelIndex(), idx, idx);
+ remove(idx);
+ endRemoveRows();
+ }
Q_ASSERT(!contains(a));
}
@@ -54,8 +57,6 @@ QHash<int, QByteArray> AccountRegistry::roleNames() const
return { { AccountRole, "connection" } };
}
-
-
Connection* AccountRegistry::get(const QString& userId)
{
for (const auto &connection : *this) {
@@ -64,3 +65,73 @@ 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);
+ job->start();
+
+ return job;
+}
+
+void AccountRegistry::invokeLogin()
+{
+ const auto accounts = SettingsGroup("Accounts").childGroups();
+ for (const auto& accountId : accounts) {
+ AccountSettings account { accountId };
+ m_accountsLoading += accountId;
+ emit accountsLoadingChanged();
+
+ if (account.homeserver().isEmpty())
+ continue;
+
+ auto accessTokenLoadingJob =
+ loadAccessTokenFromKeychain(account.userId());
+ connect(accessTokenLoadingJob, &QKeychain::Job::finished, this,
+ [accountId, this, accessTokenLoadingJob]() {
+ if (accessTokenLoadingJob->error()
+ != QKeychain::Error::NoError) {
+ emit keychainError(accessTokenLoadingJob->error());
+ return;
+ }
+
+ AccountSettings account { accountId };
+ auto connection = new Connection(account.homeserver());
+ connect(connection, &Connection::connected, this,
+ [connection, this, accountId] {
+ connection->loadState();
+ connection->setLazyLoading(true);
+
+ connection->syncLoop();
+
+ m_accountsLoading.removeAll(accountId);
+ emit accountsLoadingChanged();
+ });
+ connect(connection, &Connection::loginError, this,
+ [this, connection, accountId](const QString& error,
+ const QString& details) {
+ emit loginError(connection, error, details);
+
+ m_accountsLoading.removeAll(accountId);
+ emit accountsLoadingChanged();
+ });
+ connect(connection, &Connection::resolveError, this,
+ [this, connection, accountId](const QString& error) {
+ emit resolveError(connection, error);
+
+ m_accountsLoading.removeAll(accountId);
+ emit accountsLoadingChanged();
+ });
+ connection->assumeIdentity(
+ account.userId(), accessTokenLoadingJob->binaryData(),
+ account.deviceId());
+ });
+ }
+}
+
+QStringList AccountRegistry::accountsLoading() const
+{
+ return m_accountsLoading;
+}