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