2 * Copyright (C) 2013,2015-2016 Canonical, Ltd.
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.
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.
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/>.
18#include "UsersModel.h"
19#include <QIdentityProxyModel>
20#include <QLightDM/UsersModel>
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
35 explicit MangleModel(QObject* parent=0);
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;
47 int sourceRowCount() const;
49 void updateGuestRow();
50 void updateManualRow();
51 void updateCustomRows();
53 void addCustomRow(const CustomRow &newRow);
54 void removeCustomRow(const QString &rowName);
56 QList<CustomRow> m_customRows;
57 bool m_updatingCustomRows;
60MangleModel::MangleModel(QObject* parent)
61 : QIdentityProxyModel(parent)
62 , m_updatingCustomRows(false)
64 setSourceModel(new QLightDM::UsersModel(this));
68 // Would be nice if there were a rowCountChanged signal in the base class.
69 // We redo all custom rows on any row count change, because (A) some of
70 // custom rows (manual login) use row count information and (B) when
71 // testing, we use a modelReset signal as a way to indicate that a custom
72 // row has been toggled off or on.
73 connect(this, &QIdentityProxyModel::modelReset,
74 this, &MangleModel::updateCustomRows);
75 connect(this, &QIdentityProxyModel::rowsInserted,
76 this, &MangleModel::updateCustomRows);
77 connect(this, &QIdentityProxyModel::rowsRemoved,
78 this, &MangleModel::updateCustomRows);
81QVariant MangleModel::data(const QModelIndex &index, int role) const
85 if (index.row() >= rowCount())
88 bool isCustomRow = index.row() >= sourceRowCount();
89 if (isCustomRow && index.column() == 0) {
90 int customIndex = index.row() - sourceRowCount();
91 if (role == QLightDM::UsersModel::NameRole) {
92 variantData = m_customRows[customIndex].name;
93 } else if (role == QLightDM::UsersModel::RealNameRole) {
94 variantData = m_customRows[customIndex].realName;
95 } else if (role == QLightDM::UsersModel::LoggedInRole) {
97 } else if (role == QLightDM::UsersModel::SessionRole) {
98 variantData = Greeter::instance()->defaultSessionHint();
101 variantData = QIdentityProxyModel::data(index, role);
104 // If user's real name is empty, switch to unix name
105 if (role == QLightDM::UsersModel::RealNameRole && variantData.toString().isEmpty()) {
106 variantData = data(index, QLightDM::UsersModel::NameRole);
107 } else if (role == QLightDM::UsersModel::BackgroundPathRole && variantData.toString().startsWith('#')) {
108 const QString stringData = "data:image/svg+xml,<svg><rect width='100%' height='100%' fill='" + variantData.toString() + "'/></svg>";
109 variantData = stringData;
115void MangleModel::addCustomRow(const CustomRow &newRow)
117 for (int i = 0; i < m_customRows.size(); i++) {
118 if (m_customRows[i].name == newRow.name) {
119 return; // we don't have custom rows that change content yet
123 beginInsertRows(QModelIndex(), rowCount(), rowCount());
124 m_customRows << newRow;
128void MangleModel::removeCustomRow(const QString &rowName)
130 for (int i = 0; i < m_customRows.size(); i++) {
131 if (m_customRows[i].name == rowName) {
132 int rowNum = sourceRowCount() + i;
133 beginRemoveRows(QModelIndex(), rowNum, rowNum);
134 m_customRows.removeAt(i);
141void MangleModel::updateManualRow()
143 bool hasAnotherEntry = sourceRowCount() > 0;
144 for (int i = 0; !hasAnotherEntry && i < m_customRows.size(); i++) {
145 if (m_customRows[i].name != QStringLiteral("*other")) {
146 hasAnotherEntry = true;
150 // Show manual login if we are asked to OR if no other entry exists
151 if (Greeter::instance()->showManualLoginHint() || !hasAnotherEntry)
152 addCustomRow({QStringLiteral("*other"), gettext("Login")});
154 removeCustomRow(QStringLiteral("*other"));
157void MangleModel::updateGuestRow()
159 if (Greeter::instance()->hasGuestAccount())
160 addCustomRow({QStringLiteral("*guest"), gettext("Guest Session")});
162 removeCustomRow(QStringLiteral("*guest"));
165void MangleModel::updateCustomRows()
167 // We update when rowCount changes, but we also insert/remove rows here.
168 // So guard this function to avoid recursion.
169 if (m_updatingCustomRows)
172 m_updatingCustomRows = true;
175 m_updatingCustomRows = false;
178int MangleModel::rowCount(const QModelIndex &parent) const
180 if (parent.isValid())
183 return sourceRowCount() + m_customRows.size();
186int MangleModel::sourceRowCount() const
188 return Greeter::instance()->hideUsersHint() ? 0 : sourceModel()->rowCount();
191QModelIndex MangleModel::index(int row, int column, const QModelIndex &parent) const
193 if (row >= rowCount())
194 return QModelIndex();
196 bool isCustomRow = row >= sourceRowCount();
197 if (isCustomRow && !parent.isValid()) {
198 return createIndex(row, column);
200 return QIdentityProxyModel::index(row, column, parent);
204// **** Now we continue with actual UsersModel class ****
206UsersModel::UsersModel(QObject* parent)
207 : UnitySortFilterProxyModelQML(parent)
209 setModel(new MangleModel(this));
210 setSortCaseSensitivity(Qt::CaseInsensitive);
211 setSortLocaleAware(true);
212 setSortRole(QLightDM::UsersModel::RealNameRole);
216bool UsersModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
218 auto leftName = source_left.data(QLightDM::UsersModel::NameRole);
219 auto rightName = source_right.data(QLightDM::UsersModel::NameRole);
221 if (leftName == QStringLiteral("*guest"))
223 if (rightName == QStringLiteral("*guest"))
225 if (leftName == QStringLiteral("*other"))
227 if (rightName == QStringLiteral("*other"))
230 return UnitySortFilterProxyModelQML::lessThan(source_left, source_right);
233#include "UsersModel.moc"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list