File: UsersModel.cpp

package info (click to toggle)
lomiri 0.5.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (248 lines) | stat: -rw-r--r-- 8,184 bytes parent folder | download | duplicates (2)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * Copyright (C) 2013, 2015-2016 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Greeter.h"
#include "UsersModel.h"
#include <QIdentityProxyModel>
#include <QLightDM/UsersModel>

#include <libintl.h>

// First, we define an internal class that wraps LightDM's UsersModel.  This
// class will modify some of the data coming from LightDM.  For example, we
// modify any empty Real Names into just normal Names.  We also add optional
// rows, depending on configuration.
// (We can't modify the data directly in UsersModel below because it won't sort
// using the modified data.)
class MangleModel : public QIdentityProxyModel
{
    Q_OBJECT

public:
    explicit MangleModel(QObject* parent=0);

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;

private:
    struct CustomRow {
        QString name;
        QString realName;
    };

    int sourceRowCount() const;

    void updateGuestRow();
    void updateManualRow();
    void updateCustomRows();
    void resetCustomRows();

    void addCustomRow(const CustomRow &newRow);
    void removeCustomRow(const QString &rowName);

    QList<CustomRow> m_customRows;
    bool m_updatingCustomRows;
};

MangleModel::MangleModel(QObject* parent)
  : QIdentityProxyModel(parent)
  , m_updatingCustomRows(false)
{
    setSourceModel(new QLightDM::UsersModel(this));

    updateCustomRows();

    // Would be nice if there were a rowCountChanged signal in the base class.
    // We redo all custom rows on any row count change, because (A) some of
    // custom rows (manual login) use row count information and (B) when
    // testing, we use a modelReset signal as a way to indicate that a custom
    // row has been toggled off or on.
    connect(this, &QIdentityProxyModel::modelReset,
            this, &MangleModel::resetCustomRows);
    connect(this, &QIdentityProxyModel::rowsInserted,
            this, &MangleModel::updateCustomRows);
    connect(this, &QIdentityProxyModel::rowsRemoved,
            this, &MangleModel::updateCustomRows);
}

QVariant MangleModel::data(const QModelIndex &index, int role) const
{
    QVariant variantData;

    if (index.row() >= rowCount())
        return QVariant();

    bool isCustomRow = index.row() >= sourceRowCount();
    if (isCustomRow && index.column() == 0) {
        int customIndex = index.row() - sourceRowCount();
        if (role == QLightDM::UsersModel::NameRole) {
            variantData = m_customRows[customIndex].name;
        } else if (role == QLightDM::UsersModel::RealNameRole) {
            variantData = m_customRows[customIndex].realName;
        } else if (role == QLightDM::UsersModel::LoggedInRole) {
            variantData = false;
        } else if (role == QLightDM::UsersModel::SessionRole) {
            variantData = Greeter::instance()->defaultSessionHint();
        }
    } else {
        variantData = QIdentityProxyModel::data(index, role);
    }

    // If user's real name is empty, switch to unix name
    if (role == QLightDM::UsersModel::RealNameRole && variantData.toString().isEmpty()) {
        variantData = data(index, QLightDM::UsersModel::NameRole);
    } else if (role == QLightDM::UsersModel::BackgroundPathRole && variantData.toString().startsWith('#')) {
        const QString stringData = "data:image/svg+xml,<svg><rect width='100%' height='100%' fill='" + variantData.toString() + "'/></svg>";
        variantData = stringData;
    }

    // Workaround for liblightdm returning "" when a user has no default session
    if (Q_UNLIKELY(role == QLightDM::UsersModel::SessionRole && variantData.toString().isEmpty())) {
        variantData = Greeter::instance()->defaultSessionHint();
    }

    return variantData;
}

void MangleModel::addCustomRow(const CustomRow &newRow)
{
    for (int i = 0; i < m_customRows.size(); i++) {
        if (m_customRows[i].name == newRow.name) {
            return; // we don't have custom rows that change content yet
        }
    }

    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_customRows << newRow;
    endInsertRows();
}

void MangleModel::removeCustomRow(const QString &rowName)
{
    for (int i = 0; i < m_customRows.size(); i++) {
        if (m_customRows[i].name == rowName) {
            int rowNum = sourceRowCount() + i;
            beginRemoveRows(QModelIndex(), rowNum, rowNum);
            m_customRows.removeAt(i);
            endRemoveRows();
            break;
        }
    }
}

void MangleModel::updateManualRow()
{
    bool hasAnotherEntry = sourceRowCount() > 0;
    for (int i = 0; !hasAnotherEntry && i < m_customRows.size(); i++) {
        if (m_customRows[i].name != QStringLiteral("*other")) {
            hasAnotherEntry = true;
        }
    }

    // Show manual login if we are asked to OR if no other entry exists
    if (Greeter::instance()->showManualLoginHint() || !hasAnotherEntry)
        addCustomRow({QStringLiteral("*other"), gettext("Login")});
    else
        removeCustomRow(QStringLiteral("*other"));
}

void MangleModel::updateGuestRow()
{
    if (Greeter::instance()->hasGuestAccount())
        addCustomRow({QStringLiteral("*guest"), gettext("Guest Session")});
    else
        removeCustomRow(QStringLiteral("*guest"));
}

void MangleModel::updateCustomRows()
{
    // We update when rowCount changes, but we also insert/remove rows here.
    // So guard this function to avoid recursion.
    if (m_updatingCustomRows)
        return;

    m_updatingCustomRows = true;
    updateGuestRow();
    updateManualRow();
    m_updatingCustomRows = false;
}

void MangleModel::resetCustomRows()
{
    // If our parent model is reset, then this model itself is also reset. That
    // means any of our custom rows "no longer exists". Reset it, then proceed
    // to re-populate it if needed.
    m_customRows.clear();
    updateCustomRows();
}

int MangleModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    else
        return sourceRowCount() + m_customRows.size();
}

int MangleModel::sourceRowCount() const
{
    return Greeter::instance()->hideUsersHint() ? 0 : sourceModel()->rowCount();
}

QModelIndex MangleModel::index(int row, int column, const QModelIndex &parent) const
{
    if (row >= rowCount())
        return QModelIndex();

    bool isCustomRow = row >= sourceRowCount();
    if (isCustomRow && !parent.isValid()) {
        return createIndex(row, column);
    } else {
        return QIdentityProxyModel::index(row, column, parent);
    }
}

// **** Now we continue with actual UsersModel class ****

UsersModel::UsersModel(QObject* parent)
  : LomiriSortFilterProxyModelQML(parent)
{
    setModel(new MangleModel(this));
    setSortCaseSensitivity(Qt::CaseInsensitive);
    setSortLocaleAware(true);
    setSortRole(QLightDM::UsersModel::RealNameRole);
    sort(0);
}

bool UsersModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
{
    auto leftName = source_left.data(QLightDM::UsersModel::NameRole);
    auto rightName = source_right.data(QLightDM::UsersModel::NameRole);

    if (leftName == QStringLiteral("*guest"))
        return false;
    if (rightName == QStringLiteral("*guest"))
        return true;
    if (leftName == QStringLiteral("*other"))
        return false;
    if (rightName == QStringLiteral("*other"))
        return true;

    return LomiriSortFilterProxyModelQML::lessThan(source_left, source_right);
}

#include "UsersModel.moc"