1 | // SPDX-FileCopyrightText: 2020 Kitsune Ral <Kitsune-Ral@users.sf.net> |
2 | // SPDX-FileCopyrightText: Tobias Fella <fella@posteo.de> |
3 | // SPDX-License-Identifier: LGPL-2.1-or-later |
4 | |
5 | #pragma once |
6 | |
7 | #include "util.h" |
8 | |
9 | #include <QtCore/QAbstractListModel> |
10 | |
11 | #if QT_VERSION_MAJOR >= 6 |
12 | # include <qt6keychain/keychain.h> |
13 | #else |
14 | # include <qt5keychain/keychain.h> |
15 | #endif |
16 | |
17 | namespace Quotient { |
18 | class Connection; |
19 | |
20 | class QUOTIENT_API AccountRegistry : public QAbstractListModel, |
21 | private QVector<Connection*> { |
22 | Q_OBJECT |
23 | /// Number of accounts that are currently fully loaded |
24 | Q_PROPERTY(int accountCount READ rowCount NOTIFY accountCountChanged) |
25 | /// List of accounts that are currently in some stage of being loaded (Reading token from keychain, trying to contact server, etc). |
26 | /// Can be used to inform the user or to show a login screen if size() == 0 and no accounts are loaded |
27 | Q_PROPERTY(QStringList accountsLoading READ accountsLoading NOTIFY accountsLoadingChanged) |
28 | public: |
29 | using vector_t = QVector<Connection*>; |
30 | using const_iterator = vector_t::const_iterator; |
31 | using const_reference = vector_t::const_reference; |
32 | |
33 | enum EventRoles { |
34 | AccountRole = Qt::UserRole + 1, |
35 | ConnectionRole = AccountRole, |
36 | UserIdRole = Qt::DisplayRole |
37 | }; |
38 | |
39 | explicit AccountRegistry(QObject* parent = nullptr); |
40 | |
41 | // Expose most of vector_t's const-API but only provide add() and drop() |
42 | // for changing it. In theory other changing operations could be supported |
43 | // too; but then boilerplate begin/end*() calls has to be tucked into each |
44 | // and this class gives no guarantees on the order of entries, so why care. |
45 | |
46 | const vector_t& accounts() const { return *this; } |
47 | void add(Connection* a); |
48 | void drop(Connection* a); |
49 | const_iterator begin() const { return vector_t::begin(); } |
50 | const_iterator end() const { return vector_t::end(); } |
51 | const_reference front() const { return vector_t::front(); } |
52 | const_reference back() const { return vector_t::back(); } |
53 | bool isLoggedIn(const QString& userId) const; |
54 | Connection* get(const QString& userId) const; |
55 | |
56 | using vector_t::isEmpty, vector_t::empty; |
57 | using vector_t::size, vector_t::count, vector_t::capacity; |
58 | using vector_t::cbegin, vector_t::cend, vector_t::contains; |
59 | |
60 | // QAbstractItemModel interface implementation |
61 | |
62 | [[nodiscard]] QVariant data(const QModelIndex& index, |
63 | int role) const override; |
64 | [[nodiscard]] int rowCount( |
65 | const QModelIndex& parent = QModelIndex()) const override; |
66 | [[nodiscard]] QHash<int, QByteArray> roleNames() const override; |
67 | |
68 | QStringList accountsLoading() const; |
69 | |
70 | [[deprecated("This may leak Connection objects when failing and cannot be" |
71 | "fixed without breaking the API; do not use it" )]] // |
72 | void invokeLogin(); |
73 | |
74 | Q_SIGNALS: |
75 | void accountCountChanged(); |
76 | void accountsLoadingChanged(); |
77 | |
78 | void keychainError(QKeychain::Error error); |
79 | void loginError(Connection* connection, QString message, QString details); |
80 | void resolveError(Connection* connection, QString error); |
81 | |
82 | private: |
83 | struct Private; |
84 | ImplPtr<Private> d; |
85 | }; |
86 | |
87 | [[deprecated("Make and use an application-scope instance instead of a singleton" )]] |
88 | extern QUOTIENT_API AccountRegistry Accounts; |
89 | } // namespace Quotient |
90 | |