aboutsummaryrefslogtreecommitdiff
path: root/lib/accountregistry.h
blob: 2f6dffdf5154d44a7264a319b14c1bd6c6789329 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 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 "quotient_export.h"

#include <QtCore/QAbstractListModel>

namespace Quotient {
class Connection;

class QUOTIENT_API AccountRegistry : public QAbstractListModel,
                                     private QVector<Connection*> {
    Q_OBJECT
public:
    using const_iterator = QVector::const_iterator;
    using const_reference = QVector::const_reference;

    enum EventRoles {
        AccountRole = Qt::UserRole + 1,
        ConnectionRole = AccountRole
    };

    [[deprecated("Use Accounts variable instead")]] //
    static AccountRegistry& instance();

    // Expose most of QVector's const-API but only provide add() and drop()
    // for changing it. In theory other changing operations could be supported
    // too; but then boilerplate begin/end*() calls has to be tucked into each
    // and this class gives no guarantees on the order of entries, so why care.

    const QVector<Connection*>& accounts() const { return *this; }
    void add(Connection* a);
    void drop(Connection* a);
    const_iterator begin() const { return QVector::begin(); }
    const_iterator end() const { return QVector::end(); }
    const_reference front() const { return QVector::front(); }
    const_reference back() const { return QVector::back(); }
    bool isLoggedIn(const QString& userId) const;
    Connection* get(const QString& userId);

    using QVector::isEmpty, QVector::empty;
    using QVector::size, QVector::count, QVector::capacity;
    using QVector::cbegin, QVector::cend, QVector::contains;

    // QAbstractItemModel interface implementation

    [[nodiscard]] QVariant data(const QModelIndex& index,
                                int role) const override;
    [[nodiscard]] int rowCount(
        const QModelIndex& parent = QModelIndex()) const override;
    [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
};

inline QUOTIENT_API AccountRegistry Accounts {};

inline AccountRegistry& AccountRegistry::instance() { return Accounts; }
}