File: accountregistry.cpp

package info (click to toggle)
libquotient 0.9.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,588 kB
  • sloc: xml: 39,103; cpp: 25,226; sh: 97; makefile: 10
file content (158 lines) | stat: -rw-r--r-- 5,430 bytes parent folder | download
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// 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"
#include "logging_categories_p.h"
#include "settings.h"

#include <QtCore/QCoreApplication>

using namespace Quotient;

struct Q_DECL_HIDDEN AccountRegistry::Private {
    QStringList m_accountsLoading;
};

AccountRegistry::AccountRegistry(QObject* parent)
    : QAbstractListModel(parent), d(makeImpl<Private>())
{}

void AccountRegistry::add(Connection* a)
{
    Q_ASSERT(a != nullptr);
    if (get(a->userId()) != nullptr) {
        qWarning(MAIN) << "Attempt to add another connection for the same user "
                          "id; skipping";
        return;
    }
    beginInsertRows(QModelIndex(), size(), size());
    push_back(a);
    connect(a, &Connection::loggedOut, this, [this, a] { drop(a); });
    qDebug(MAIN) << "Added" << a->objectName() << "to the account registry";
    endInsertRows();
    emit accountCountChanged();
}

void AccountRegistry::drop(Connection* a)
{
    if (const auto idx = indexOf(a); idx != -1) {
        beginRemoveRows(QModelIndex(), idx, idx);
        remove(idx);
        qDebug(MAIN) << "Removed" << a->objectName()
                     << "from the account registry";
        endRemoveRows();
    }
    Q_ASSERT(!contains(a));
}

bool AccountRegistry::isLoggedIn(const QString &userId) const
{
    const auto conn = get(userId);
    return conn != nullptr && conn->isLoggedIn();
}

QVariant AccountRegistry::data(const QModelIndex& index, int role) const
{
    if (!index.isValid() || index.row() >= size())
        return {};

    switch (role) {
        case AccountRole:
            return QVariant::fromValue(at(index.row()));
        case UserIdRole:
            return QVariant::fromValue(at(index.row())->userId());
        default:
            return {};
    }
}

int AccountRegistry::rowCount(const QModelIndex& parent) const
{
    return parent.isValid() ? 0 : size();
}

QHash<int, QByteArray> AccountRegistry::roleNames() const
{
    return { { AccountRole, QByteArrayLiteral("connection") },
             { UserIdRole, QByteArrayLiteral("userId") } };
}

Connection* AccountRegistry::get(const QString& userId) const
{
    for (const auto& connection : accounts()) {
        if (connection->userId() == userId)
            return connection;
    }
    return nullptr;
}

void AccountRegistry::invokeLogin()
{
    const auto accounts = SettingsGroup("Accounts"_L1).childGroups();
    for (const auto& accountId : accounts) {
        AccountSettings account { accountId };

        if (account.homeserver().isEmpty())
            continue;

        d->m_accountsLoading += accountId;
        emit accountsLoadingChanged();

        qCDebug(MAIN) << "Reading access token from keychain for" << accountId;
        auto accessTokenLoadingJob =
            new QKeychain::ReadPasswordJob(qAppName(), this);
        accessTokenLoadingJob->setKey(accountId);
        connect(accessTokenLoadingJob, &QKeychain::Job::finished, this,
                [accountId, this, accessTokenLoadingJob]() {
                    if (accessTokenLoadingJob->error()
                        != QKeychain::Error::NoError) {
                        emit keychainError(accessTokenLoadingJob->error());
                        d->m_accountsLoading.removeAll(accountId);
                        emit accountsLoadingChanged();
                        return;
                    }

                    AccountSettings account { accountId };
                    auto connection = new Connection(account.homeserver());
                    connect(connection, &Connection::connected, this,
                            [connection, this, accountId] {
                                connection->loadState();
                                connection->setLazyLoading(true);

                                connection->syncLoop();

                                d->m_accountsLoading.removeAll(accountId);
                                emit accountsLoadingChanged();
                            });
                    connect(connection, &Connection::loginError, this,
                            [this, connection, accountId](const QString& error,
                                               const QString& details) {
                                emit loginError(connection, error, details);

                                d->m_accountsLoading.removeAll(accountId);
                                emit accountsLoadingChanged();
                            });
                    connect(connection, &Connection::resolveError, this,
                            [this, connection, accountId](const QString& error) {
                                emit resolveError(connection, error);

                                d->m_accountsLoading.removeAll(accountId);
                                emit accountsLoadingChanged();
                            });
                    connection->assumeIdentity(
                        account.userId(),
                        account.deviceId(),
                        QString::fromUtf8(accessTokenLoadingJob->binaryData()));
                    add(connection);
                });
        accessTokenLoadingJob->start();
    }
}

QStringList AccountRegistry::accountsLoading() const
{
    return d->m_accountsLoading;
}