18#include "UsersModel.h"
19#include <QIdentityProxyModel>
20#include <QLightDM/UsersModel>
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();
52 void resetCustomRows();
54 void addCustomRow(
const CustomRow &newRow);
55 void removeCustomRow(
const QString &rowName);
57 QList<CustomRow> m_customRows;
58 bool m_updatingCustomRows;
61MangleModel::MangleModel(QObject* parent)
62 : QIdentityProxyModel(parent)
63 , m_updatingCustomRows(false)
65 setSourceModel(
new QLightDM::UsersModel(
this));
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);
82QVariant MangleModel::data(
const QModelIndex &index,
int role)
const
86 if (index.row() >= rowCount())
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) {
98 }
else if (role == QLightDM::UsersModel::SessionRole) {
99 variantData = Greeter::instance()->defaultSessionHint();
102 variantData = QIdentityProxyModel::data(index, role);
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;
114 if (Q_UNLIKELY(role == QLightDM::UsersModel::SessionRole && variantData.toString().isEmpty())) {
115 variantData = Greeter::instance()->defaultSessionHint();
121void MangleModel::addCustomRow(
const CustomRow &newRow)
123 for (
int i = 0; i < m_customRows.size(); i++) {
124 if (m_customRows[i].name == newRow.name) {
129 beginInsertRows(QModelIndex(), rowCount(), rowCount());
130 m_customRows << newRow;
134void MangleModel::removeCustomRow(
const QString &rowName)
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);
147void MangleModel::updateManualRow()
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;
157 if (Greeter::instance()->showManualLoginHint() || !hasAnotherEntry)
158 addCustomRow({QStringLiteral(
"*other"), gettext(
"Login")});
160 removeCustomRow(QStringLiteral(
"*other"));
163void MangleModel::updateGuestRow()
165 if (Greeter::instance()->hasGuestAccount())
166 addCustomRow({QStringLiteral(
"*guest"), gettext(
"Guest Session")});
168 removeCustomRow(QStringLiteral(
"*guest"));
171void MangleModel::updateCustomRows()
175 if (m_updatingCustomRows)
178 m_updatingCustomRows =
true;
181 m_updatingCustomRows =
false;
184void MangleModel::resetCustomRows()
189 m_customRows.clear();
193int MangleModel::rowCount(
const QModelIndex &parent)
const
195 if (parent.isValid())
198 return sourceRowCount() + m_customRows.size();
201int MangleModel::sourceRowCount()
const
203 return Greeter::instance()->hideUsersHint() ? 0 : sourceModel()->rowCount();
206QModelIndex MangleModel::index(
int row,
int column,
const QModelIndex &parent)
const
208 if (row >= rowCount())
209 return QModelIndex();
211 bool isCustomRow = row >= sourceRowCount();
212 if (isCustomRow && !parent.isValid()) {
213 return createIndex(row, column);
215 return QIdentityProxyModel::index(row, column, parent);
221UsersModel::UsersModel(QObject* parent)
222 : LomiriSortFilterProxyModelQML(parent)
224 setModel(
new MangleModel(
this));
225 setSortCaseSensitivity(Qt::CaseInsensitive);
226 setSortLocaleAware(
true);
227 setSortRole(QLightDM::UsersModel::RealNameRole);
231bool UsersModel::lessThan(
const QModelIndex &source_left,
const QModelIndex &source_right)
const
233 auto leftName = source_left.data(QLightDM::UsersModel::NameRole);
234 auto rightName = source_right.data(QLightDM::UsersModel::NameRole);
236 if (leftName == QStringLiteral(
"*guest"))
238 if (rightName == QStringLiteral(
"*guest"))
240 if (leftName == QStringLiteral(
"*other"))
242 if (rightName == QStringLiteral(
"*other"))
245 return LomiriSortFilterProxyModelQML::lessThan(source_left, source_right);
248#include "UsersModel.moc"