Lomiri
Loading...
Searching...
No Matches
UsersModel.cpp
1/*
2 * Copyright (C) 2013, 2015-2016 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "Greeter.h"
18#include "UsersModel.h"
19#include <QIdentityProxyModel>
20#include <QLightDM/UsersModel>
21
22#include <libintl.h>
23
24// First, we define an internal class that wraps LightDM's UsersModel. This
25// class will modify some of the data coming from LightDM. For example, we
26// modify any empty Real Names into just normal Names. We also add optional
27// rows, depending on configuration.
28// (We can't modify the data directly in UsersModel below because it won't sort
29// using the modified data.)
30class MangleModel : public QIdentityProxyModel
31{
32 Q_OBJECT
33
34public:
35 explicit MangleModel(QObject* parent=0);
36
37 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
38 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
39 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
40
41private:
42 struct CustomRow {
43 QString name;
44 QString realName;
45 };
46
47 int sourceRowCount() const;
48
49 void updateGuestRow();
50 void updateManualRow();
51 void updateCustomRows();
52 void resetCustomRows();
53
54 void addCustomRow(const CustomRow &newRow);
55 void removeCustomRow(const QString &rowName);
56
57 QList<CustomRow> m_customRows;
58 bool m_updatingCustomRows;
59};
60
61MangleModel::MangleModel(QObject* parent)
62 : QIdentityProxyModel(parent)
63 , m_updatingCustomRows(false)
64{
65 setSourceModel(new QLightDM::UsersModel(this));
66
67 updateCustomRows();
68
69 // Would be nice if there were a rowCountChanged signal in the base class.
70 // We redo all custom rows on any row count change, because (A) some of
71 // custom rows (manual login) use row count information and (B) when
72 // testing, we use a modelReset signal as a way to indicate that a custom
73 // row has been toggled off or on.
74 connect(this, &QIdentityProxyModel::modelReset,
75 this, &MangleModel::resetCustomRows);
76 connect(this, &QIdentityProxyModel::rowsInserted,
77 this, &MangleModel::updateCustomRows);
78 connect(this, &QIdentityProxyModel::rowsRemoved,
79 this, &MangleModel::updateCustomRows);
80}
81
82QVariant MangleModel::data(const QModelIndex &index, int role) const
83{
84 QVariant variantData;
85
86 if (index.row() >= rowCount())
87 return QVariant();
88
89 bool isCustomRow = index.row() >= sourceRowCount();
90 if (isCustomRow && index.column() == 0) {
91 int customIndex = index.row() - sourceRowCount();
92 if (role == QLightDM::UsersModel::NameRole) {
93 variantData = m_customRows[customIndex].name;
94 } else if (role == QLightDM::UsersModel::RealNameRole) {
95 variantData = m_customRows[customIndex].realName;
96 } else if (role == QLightDM::UsersModel::LoggedInRole) {
97 variantData = false;
98 } else if (role == QLightDM::UsersModel::SessionRole) {
99 variantData = Greeter::instance()->defaultSessionHint();
100 }
101 } else {
102 variantData = QIdentityProxyModel::data(index, role);
103 }
104
105 // If user's real name is empty, switch to unix name
106 if (role == QLightDM::UsersModel::RealNameRole && variantData.toString().isEmpty()) {
107 variantData = data(index, QLightDM::UsersModel::NameRole);
108 } else if (role == QLightDM::UsersModel::BackgroundPathRole && variantData.toString().startsWith('#')) {
109 const QString stringData = "data:image/svg+xml,<svg><rect width='100%' height='100%' fill='" + variantData.toString() + "'/></svg>";
110 variantData = stringData;
111 }
112
113 // Workaround for liblightdm returning "" when a user has no default session
114 if (Q_UNLIKELY(role == QLightDM::UsersModel::SessionRole && variantData.toString().isEmpty())) {
115 variantData = Greeter::instance()->defaultSessionHint();
116 }
117
118 return variantData;
119}
120
121void MangleModel::addCustomRow(const CustomRow &newRow)
122{
123 for (int i = 0; i < m_customRows.size(); i++) {
124 if (m_customRows[i].name == newRow.name) {
125 return; // we don't have custom rows that change content yet
126 }
127 }
128
129 beginInsertRows(QModelIndex(), rowCount(), rowCount());
130 m_customRows << newRow;
131 endInsertRows();
132}
133
134void MangleModel::removeCustomRow(const QString &rowName)
135{
136 for (int i = 0; i < m_customRows.size(); i++) {
137 if (m_customRows[i].name == rowName) {
138 int rowNum = sourceRowCount() + i;
139 beginRemoveRows(QModelIndex(), rowNum, rowNum);
140 m_customRows.removeAt(i);
141 endRemoveRows();
142 break;
143 }
144 }
145}
146
147void MangleModel::updateManualRow()
148{
149 bool hasAnotherEntry = sourceRowCount() > 0;
150 for (int i = 0; !hasAnotherEntry && i < m_customRows.size(); i++) {
151 if (m_customRows[i].name != QStringLiteral("*other")) {
152 hasAnotherEntry = true;
153 }
154 }
155
156 // Show manual login if we are asked to OR if no other entry exists
157 if (Greeter::instance()->showManualLoginHint() || !hasAnotherEntry)
158 addCustomRow({QStringLiteral("*other"), gettext("Login")});
159 else
160 removeCustomRow(QStringLiteral("*other"));
161}
162
163void MangleModel::updateGuestRow()
164{
165 if (Greeter::instance()->hasGuestAccount())
166 addCustomRow({QStringLiteral("*guest"), gettext("Guest Session")});
167 else
168 removeCustomRow(QStringLiteral("*guest"));
169}
170
171void MangleModel::updateCustomRows()
172{
173 // We update when rowCount changes, but we also insert/remove rows here.
174 // So guard this function to avoid recursion.
175 if (m_updatingCustomRows)
176 return;
177
178 m_updatingCustomRows = true;
179 updateGuestRow();
180 updateManualRow();
181 m_updatingCustomRows = false;
182}
183
184void MangleModel::resetCustomRows()
185{
186 // If our parent model is reset, then this model itself is also reset. That
187 // means any of our custom rows "no longer exists". Reset it, then proceed
188 // to re-populate it if needed.
189 m_customRows.clear();
190 updateCustomRows();
191}
192
193int MangleModel::rowCount(const QModelIndex &parent) const
194{
195 if (parent.isValid())
196 return 0;
197 else
198 return sourceRowCount() + m_customRows.size();
199}
200
201int MangleModel::sourceRowCount() const
202{
203 return Greeter::instance()->hideUsersHint() ? 0 : sourceModel()->rowCount();
204}
205
206QModelIndex MangleModel::index(int row, int column, const QModelIndex &parent) const
207{
208 if (row >= rowCount())
209 return QModelIndex();
210
211 bool isCustomRow = row >= sourceRowCount();
212 if (isCustomRow && !parent.isValid()) {
213 return createIndex(row, column);
214 } else {
215 return QIdentityProxyModel::index(row, column, parent);
216 }
217}
218
219// **** Now we continue with actual UsersModel class ****
220
221UsersModel::UsersModel(QObject* parent)
222 : LomiriSortFilterProxyModelQML(parent)
223{
224 setModel(new MangleModel(this));
225 setSortCaseSensitivity(Qt::CaseInsensitive);
226 setSortLocaleAware(true);
227 setSortRole(QLightDM::UsersModel::RealNameRole);
228 sort(0);
229}
230
231bool UsersModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
232{
233 auto leftName = source_left.data(QLightDM::UsersModel::NameRole);
234 auto rightName = source_right.data(QLightDM::UsersModel::NameRole);
235
236 if (leftName == QStringLiteral("*guest"))
237 return false;
238 if (rightName == QStringLiteral("*guest"))
239 return true;
240 if (leftName == QStringLiteral("*other"))
241 return false;
242 if (rightName == QStringLiteral("*other"))
243 return true;
244
245 return LomiriSortFilterProxyModelQML::lessThan(source_left, source_right);
246}
247
248#include "UsersModel.moc"