diff options
author | Alexey Rusakov <Kitsune-Ral@users.sf.net> | 2021-08-26 21:36:43 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-26 21:36:43 +0200 |
commit | 6d55a2c27852f9285aa1b435a1fd56ba08aa83e5 (patch) | |
tree | f31b2f3e7217302e4dd37e288be6fa6d219401d1 | |
parent | 6953e55361f600a591c08b9cd287a350230b3ef8 (diff) | |
parent | 8d21c8a6579ad9c63e331ffb42e3ed81b9c73caf (diff) | |
download | libquotient-6d55a2c27852f9285aa1b435a1fd56ba08aa83e5.tar.gz libquotient-6d55a2c27852f9285aa1b435a1fd56ba08aa83e5.zip |
Merge pull request #497 from TobiasFella/accountregistry
Add AccountRegistry
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | lib/accountregistry.cpp | 98 | ||||
-rw-r--r-- | lib/accountregistry.h | 45 |
3 files changed, 144 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 49105389..3c6e7548 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,6 +140,7 @@ list(APPEND lib_SRCS lib/util.cpp lib/encryptionmanager.cpp lib/eventitem.cpp + lib/accountregistry.cpp lib/events/event.cpp lib/events/roomevent.cpp lib/events/stateevent.cpp diff --git a/lib/accountregistry.cpp b/lib/accountregistry.cpp new file mode 100644 index 00000000..3a022f14 --- /dev/null +++ b/lib/accountregistry.cpp @@ -0,0 +1,98 @@ +// 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() +{} + +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; +}
\ No newline at end of file diff --git a/lib/accountregistry.h b/lib/accountregistry.h new file mode 100644 index 00000000..e87da3e8 --- /dev/null +++ b/lib/accountregistry.h @@ -0,0 +1,45 @@ +// SPDX-FileCopyrightText: 2020 Kitsune Ral <Kitsune-Ral@users.sf.net> +// SPDX-FileCopyrightText: Tobias Fella <fella@posteo.de> +// SPDX-License-Identifier: LGPL-2.1-or-later + +#pragma once + +#include <QtCore/QObject> +#include <QtCore/QList> +#include <QtCore/QAbstractListModel> + +namespace Quotient { +class Connection; + +class AccountRegistry : public QAbstractListModel { + Q_OBJECT +public: + enum EventRoles { + ConnectionRole = Qt::UserRole + 1, + }; + + static AccountRegistry &instance() { + static AccountRegistry _instance; + return _instance; + } + + const QVector<Connection*> accounts() const; + void add(Connection* a); + void drop(Connection* a); + bool isLoggedIn(const QString& userId) const; + bool isEmpty() const; + int count() const; + bool contains(Connection*) const; + Connection* get(const QString& userId); + + [[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override; + + [[nodiscard]] QHash<int, QByteArray> roleNames() const override; + +private: + AccountRegistry(); + + QVector<Connection *> m_accounts; +}; +}
\ No newline at end of file |