aboutsummaryrefslogtreecommitdiff
path: root/lib/accountregistry.cpp
blob: a292ed45597b138af31980164d9c5e1a32465df1 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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;
}