@@ -44,41 +44,50 @@ private:
44 QString realName;
44 QString realName;
47− void addCustomRow(const CustomRow &newRow);
47+ int sourceRowCount() const;
48− void removeCustomRow(const QString &rowName);
48+
49 void updateGuestRow();
49 void updateGuestRow();
50 void updateManualRow();
50 void updateManualRow();
51+ void updateCustomRows();
53+ void addCustomRow(const CustomRow &newRow);
54+ void removeCustomRow(const QString &rowName);
52 QList<CustomRow> m_customRows;
56 QList<CustomRow> m_customRows;
57+ bool m_updatingCustomRows;
55MangleModel::MangleModel(QObject* parent)
60MangleModel::MangleModel(QObject* parent)
56 : QIdentityProxyModel(parent)
61 : QIdentityProxyModel(parent)
62+ , m_updatingCustomRows(false)
58− if (!Greeter::instance()->hideUsersHint()) {
59− setSourceModel(new QLightDM::UsersModel(this));
64+ setSourceModel(new QLightDM::UsersModel(this));
62− updateGuestRow();
66+ updateCustomRows();
65− // Would be nice if there were a rowCountChanged signal in the base class
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.
66 connect(this, &QIdentityProxyModel::modelReset,
73 connect(this, &QIdentityProxyModel::modelReset,
67− this, &MangleModel::updateManualRow);
74+ this, &MangleModel::updateCustomRows);
68 connect(this, &QIdentityProxyModel::rowsInserted,
75 connect(this, &QIdentityProxyModel::rowsInserted,
69− this, &MangleModel::updateManualRow);
76+ this, &MangleModel::updateCustomRows);
70 connect(this, &QIdentityProxyModel::rowsRemoved,
77 connect(this, &QIdentityProxyModel::rowsRemoved,
71− this, &MangleModel::updateManualRow);
78+ this, &MangleModel::updateCustomRows);
74QVariant MangleModel::data(const QModelIndex &index, int role) const
81QVariant MangleModel::data(const QModelIndex &index, int role) const
76 QVariant variantData;
83 QVariant variantData;
85+ if (index.row() >= rowCount())
78− bool isCustomRow = index.row() >= sourceModel()->rowCount() &&
88+ bool isCustomRow = index.row() >= sourceRowCount();
79− index.row() < rowCount();
80 if (isCustomRow && index.column() == 0) {
89 if (isCustomRow && index.column() == 0) {
81− int customIndex = index.row() - sourceModel()->rowCount();
90+ int customIndex = index.row() - sourceRowCount();
82 if (role == QLightDM::UsersModel::NameRole) {
91 if (role == QLightDM::UsersModel::NameRole) {
83 variantData = m_customRows[customIndex].name;
92 variantData = m_customRows[customIndex].name;
84 } else if (role == QLightDM::UsersModel::RealNameRole) {
93 } else if (role == QLightDM::UsersModel::RealNameRole) {
@@ -120,7 +129,7 @@ void MangleModel::removeCustomRow(const QString &rowName)
121 for (int i = 0; i < m_customRows.size(); i++) {
130 for (int i = 0; i < m_customRows.size(); i++) {
122 if (m_customRows[i].name == rowName) {
131 if (m_customRows[i].name == rowName) {
123− int rowNum = sourceModel()->rowCount() + i;
132+ int rowNum = sourceRowCount() + i;
124 beginRemoveRows(QModelIndex(), rowNum, rowNum);
133 beginRemoveRows(QModelIndex(), rowNum, rowNum);
125 m_customRows.removeAt(i);
134 m_customRows.removeAt(i);
126 endRemoveRows();
135 endRemoveRows();
@@ -131,7 +140,7 @@ void MangleModel::removeCustomRow(const QString &rowName)
132void MangleModel::updateManualRow()
141void MangleModel::updateManualRow()
134− bool hasAnotherEntry = sourceModel()->rowCount() > 0;
143+ bool hasAnotherEntry = sourceRowCount() > 0;
135 for (int i = 0; !hasAnotherEntry && i < m_customRows.size(); i++) {
144 for (int i = 0; !hasAnotherEntry && i < m_customRows.size(); i++) {
136 if (m_customRows[i].name != QStringLiteral("*other")) {
145 if (m_customRows[i].name != QStringLiteral("*other")) {
137 hasAnotherEntry = true;
146 hasAnotherEntry = true;
@@ -153,16 +162,38 @@ void MangleModel::updateGuestRow()
153 removeCustomRow(QStringLiteral("*guest"));
162 removeCustomRow(QStringLiteral("*guest"));
165+void 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;
156int MangleModel::rowCount(const QModelIndex &parent) const
178int MangleModel::rowCount(const QModelIndex &parent) const
158− return QIdentityProxyModel::rowCount(parent) +
180+ if (parent.isValid())
159− (parent.isValid() ? 0 : m_customRows.size());
181+ return 0;
183+ return sourceRowCount() + m_customRows.size();
186+int MangleModel::sourceRowCount() const
188+ return Greeter::instance()->hideUsersHint() ? 0 : sourceModel()->rowCount();
162QModelIndex MangleModel::index(int row, int column, const QModelIndex &parent) const
191QModelIndex MangleModel::index(int row, int column, const QModelIndex &parent) const
193+ if (row >= rowCount())
194+ return QModelIndex();
164− bool isCustomRow = row >= sourceModel()->rowCount() &&
196+ bool isCustomRow = row >= sourceRowCount();
166 if (isCustomRow && !parent.isValid()) {
197 if (isCustomRow && !parent.isValid()) {
167 return createIndex(row, column);
198 return createIndex(row, column);