aboutsummaryrefslogtreecommitdiff
path: root/lib/accountregistry.cpp
diff options
context:
space:
mode:
authorarawaaa <77910862+arawaaa@users.noreply.github.com>2021-12-27 17:35:28 -0600
committerGitHub <noreply@github.com>2021-12-27 17:35:28 -0600
commit7ec3ba834dd313c4408622da30e04cdc6f4cf7c7 (patch)
tree679d7dc5939a229ad46676cd1d7aeaea7a25abce /lib/accountregistry.cpp
parent17bf4d180297c7e87363e179b8afa79ddb15dca7 (diff)
parent674e984e459375974f619d0e778d43a2cc928dc3 (diff)
downloadlibquotient-7ec3ba834dd313c4408622da30e04cdc6f4cf7c7.tar.gz
libquotient-7ec3ba834dd313c4408622da30e04cdc6f4cf7c7.zip
Merge branch 'dev' into pinned
Diffstat (limited to 'lib/accountregistry.cpp')
-rw-r--r--lib/accountregistry.cpp97
1 files changed, 97 insertions, 0 deletions
diff --git a/lib/accountregistry.cpp b/lib/accountregistry.cpp
new file mode 100644
index 00000000..a292ed45
--- /dev/null
+++ b/lib/accountregistry.cpp
@@ -0,0 +1,97 @@
+// SPDX-FileCopyrightText: Kitsune Ral <Kitsune-Ral@users.sf.net>
+// SPDX-FileCopyrightText: Tobias Fella <fella@posteo.de>
+// SPDX-License-Identifier: LGPL-2.1-or-later
+
+#include "accountregistry.h"
+
+#include "connection.h"
+
+using namespace Quotient;
+
+void AccountRegistry::add(Connection* c)
+{
+ if (m_accounts.contains(c))
+ return;
+ beginInsertRows(QModelIndex(), m_accounts.size(), m_accounts.size());
+ m_accounts += c;
+ endInsertRows();
+}
+
+void AccountRegistry::drop(Connection* c)
+{
+ beginRemoveRows(QModelIndex(), m_accounts.indexOf(c), m_accounts.indexOf(c));
+ m_accounts.removeOne(c);
+ endRemoveRows();
+ Q_ASSERT(!m_accounts.contains(c));
+}
+
+bool AccountRegistry::isLoggedIn(const QString &userId) const
+{
+ return std::any_of(m_accounts.cbegin(), m_accounts.cend(),
+ [&userId](Connection* a) { return a->userId() == userId; });
+}
+
+bool AccountRegistry::contains(Connection *c) const
+{
+ return m_accounts.contains(c);
+}
+
+AccountRegistry::AccountRegistry() = default;
+
+QVariant AccountRegistry::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid()) {
+ return {};
+ }
+
+ if (index.row() >= m_accounts.count()) {
+ return {};
+ }
+
+ auto account = m_accounts[index.row()];
+
+ if (role == ConnectionRole) {
+ return QVariant::fromValue(account);
+ }
+
+ return {};
+}
+
+int AccountRegistry::rowCount(const QModelIndex &parent) const
+{
+ if (parent.isValid()) {
+ return 0;
+ }
+
+ return m_accounts.count();
+}
+
+QHash<int, QByteArray> AccountRegistry::roleNames() const
+{
+ return {{ConnectionRole, "connection"}};
+}
+
+bool AccountRegistry::isEmpty() const
+{
+ return m_accounts.isEmpty();
+}
+
+int AccountRegistry::count() const
+{
+ return m_accounts.count();
+}
+
+const QVector<Connection*> AccountRegistry::accounts() const
+{
+ return m_accounts;
+}
+
+Connection* AccountRegistry::get(const QString& userId)
+{
+ for (const auto &connection : m_accounts) {
+ if(connection->userId() == userId) {
+ return connection;
+ }
+ }
+ return nullptr;
+}