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();
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));
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);
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;
113 if (Q_UNLIKELY(role == QLightDM::UsersModel::SessionRole && variantData.toString().isEmpty())) {
114 variantData = Greeter::instance()->defaultSessionHint();
120void MangleModel::addCustomRow(
const CustomRow &newRow)
122 for (
int i = 0; i < m_customRows.size(); i++) {
123 if (m_customRows[i].name == newRow.name) {
128 beginInsertRows(QModelIndex(), rowCount(), rowCount());
129 m_customRows << newRow;
133void MangleModel::removeCustomRow(
const QString &rowName)
135 for (
int i = 0; i < m_customRows.size(); i++) {
136 if (m_customRows[i].name == rowName) {
137 int rowNum = sourceRowCount() + i;
138 beginRemoveRows(QModelIndex(), rowNum, rowNum);
139 m_customRows.removeAt(i);
146void MangleModel::updateManualRow()
148 bool hasAnotherEntry = sourceRowCount() > 0;
149 for (
int i = 0; !hasAnotherEntry && i < m_customRows.size(); i++) {
150 if (m_customRows[i].name != QStringLiteral(
"*other")) {
151 hasAnotherEntry =
true;
156 if (Greeter::instance()->showManualLoginHint() || !hasAnotherEntry)
157 addCustomRow({QStringLiteral(
"*other"), gettext(
"Login")});
159 removeCustomRow(QStringLiteral(
"*other"));
162void MangleModel::updateGuestRow()
164 if (Greeter::instance()->hasGuestAccount())
165 addCustomRow({QStringLiteral(
"*guest"), gettext(
"Guest Session")});
167 removeCustomRow(QStringLiteral(
"*guest"));
170void MangleModel::updateCustomRows()
174 if (m_updatingCustomRows)
177 m_updatingCustomRows =
true;
180 m_updatingCustomRows =
false;
183int MangleModel::rowCount(
const QModelIndex &parent)
const
185 if (parent.isValid())
188 return sourceRowCount() + m_customRows.size();
191int MangleModel::sourceRowCount()
const
193 return Greeter::instance()->hideUsersHint() ? 0 : sourceModel()->rowCount();
196QModelIndex MangleModel::index(
int row,
int column,
const QModelIndex &parent)
const
198 if (row >= rowCount())
199 return QModelIndex();
201 bool isCustomRow = row >= sourceRowCount();
202 if (isCustomRow && !parent.isValid()) {
203 return createIndex(row, column);
205 return QIdentityProxyModel::index(row, column, parent);
211UsersModel::UsersModel(QObject* parent)
212 : LomiriSortFilterProxyModelQML(parent)
214 setModel(
new MangleModel(
this));
215 setSortCaseSensitivity(Qt::CaseInsensitive);
216 setSortLocaleAware(
true);
217 setSortRole(QLightDM::UsersModel::RealNameRole);
221bool UsersModel::lessThan(
const QModelIndex &source_left,
const QModelIndex &source_right)
const
223 auto leftName = source_left.data(QLightDM::UsersModel::NameRole);
224 auto rightName = source_right.data(QLightDM::UsersModel::NameRole);
226 if (leftName == QStringLiteral(
"*guest"))
228 if (rightName == QStringLiteral(
"*guest"))
230 if (leftName == QStringLiteral(
"*other"))
232 if (rightName == QStringLiteral(
"*other"))
235 return LomiriSortFilterProxyModelQML::lessThan(source_left, source_right);
238#include "UsersModel.moc"