servicesmodel.cpp 4.98 KB
Newer Older
1
2
3
4
5
6
/*
 *  SPDX-FileCopyrightText: 2012 Alejandro Fiestas Olivares <afiestas@kde.org>
 *  SPDX-FileCopyrightText: 2020 Dan Leinir Turthra Jensen <admin@leinir.dk>
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 */
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include "servicesmodel.h"

#include "core.h"

#include <QDebug>

#include <QIcon>

#include <SignOn/Identity>

#include <Accounts/Account>
#include <Accounts/Manager>

class ServicesModel::Private : public QObject
{
public:
    Private(ServicesModel *model)
        : q(model)
26
27
    {
    }
28
    virtual ~Private()
29
30
    {
    }
31
32

    Accounts::ServiceList services;
33
    Accounts::Account *account{nullptr};
34
35

private:
36
    ServicesModel *q;
37
38
};

39
40
41
ServicesModel::ServicesModel(QObject *parent)
    : QAbstractListModel(parent)
    , d(new ServicesModel::Private(this))
42
43
44
45
46
47
48
49
50
51
{
}

ServicesModel::~ServicesModel()
{
    delete d;
}

QHash<int, QByteArray> ServicesModel::roleNames() const
{
Nicolas Fella's avatar
Nicolas Fella committed
52
53
54
55
56
57
58
59
    static QHash<int, QByteArray> roles{{NameRole, "name"},
                                        {DescriptionRole, "description"},
                                        {DisplayNameRole, "displayName"},
                                        {ServiceTypeRole, "servieType"},
                                        {ProviderNameRole, "providerName"},
                                        {IconNameRole, "iconName"},
                                        {TagsRole, "tags"},
                                        {EnabledRole, "enabled"}};
60
61
62
    return roles;
}

63
int ServicesModel::rowCount(const QModelIndex &parent) const
64
65
66
67
68
69
70
71
{
    if (parent.isValid()) {
        return 0;
    }

    return d->services.count();
}

72
QVariant ServicesModel::data(const QModelIndex &index, int role) const
73
74
{
    QVariant data;
75
76
    if (checkIndex(index)) {
        const Accounts::Service &service = d->services.value(index.row());
77
78
        if (service.isValid()) {
            switch (role) {
79
80
81
82
83
84
85
86
            case NameRole:
                data.setValue(service.name());
                break;
            case DescriptionRole:
#if ACCOUNTSQT5_VERSION_MAJOR == 1 && ACCOUNTSQT5_VERSION_MINOR >= 16
                // Not all services have descriptions and UIs should be designed with that in mind.
                // Consequently, we can accept not having a fallback for this.
                data.setValue(service.description());
87
#endif
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
                break;
            case DisplayNameRole:
                data.setValue(service.displayName());
                break;
            case ServiceTypeRole:
                data.setValue(service.serviceType());
                break;
            case ProviderNameRole:
                data.setValue(service.provider());
                break;
            case IconNameRole:
                data.setValue(service.iconName());
                break;
            case TagsRole:
                data.setValue(service.tags().values());
                break;
            case EnabledRole:
                data.setValue(d->account->enabledServices().contains(service));
                break;
107
108
109
110
111
112
            }
        }
    }
    return data;
}

113
void ServicesModel::setAccount(QObject *account)
114
115
116
117
118
119
120
{
    if (d->account != account) {
        beginResetModel();
        d->services.clear();
        if (d->account) {
            disconnect(d->account, nullptr, this, nullptr);
        }
121
        d->account = qobject_cast<Accounts::Account *>(account);
122
        if (d->account) {
123
            connect(d->account, &Accounts::Account::displayNameChanged, this, &ServicesModel::accountChanged);
124
            connect(d->account, &Accounts::Account::enabledChanged, this, [this](const QString &serviceName, bool /*enabled*/) {
125
                int i{0};
126
                for (const Accounts::Service &service : qAsConst(d->services)) {
127
128
129
130
131
                    if (service.name() == serviceName) {
                        break;
                    }
                    ++i;
                }
Nicolas Fella's avatar
Nicolas Fella committed
132
                Q_EMIT dataChanged(index(i), index(i));
133
            });
134
            connect(d->account, &QObject::destroyed, this, [this]() {
135
136
137
138
139
140
141
142
143
144
145
146
147
                beginResetModel();
                d->account = nullptr;
                Q_EMIT accountChanged();
                d->services.clear();
                endResetModel();
            });
            d->services = d->account->services();
        }
        endResetModel();
        Q_EMIT accountChanged();
    }
}

148
QObject *ServicesModel::account() const
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{
    return d->account;
}

quint32 ServicesModel::accountId() const
{
    if (d->account) {
        return d->account->id();
    }
    return -1;
}

QString ServicesModel::accountDisplayName() const
{
    if (d->account) {
        return d->account->displayName();
    }
    return QString{};
}

QString ServicesModel::accountProviderName() const
{
    if (d->account) {
        return d->account->providerName();
    }
    return QString{};
}

QString ServicesModel::accountIconName() const
{
179
    if (d->account && d->account->provider().isValid() && !d->account->provider().iconName().isEmpty()) {
180
181
        return d->account->provider().iconName();
    }
182
    return QString::fromLatin1("user-identity");
183
}