@@ -12,23 +12,22 @@
13 * You should have received a copy of the GNU General Public License
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/>.
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16− * Author: Michael Terry <michael.terry@canonical.com>
19#include "Greeter.h"
17#include "Greeter.h"
20#include "UsersModel.h"
18#include "UsersModel.h"
19+#include <QIdentityProxyModel>
21#include <QLightDM/UsersModel>
20#include <QLightDM/UsersModel>
22−#include <QtCore/QSortFilterProxyModel>
24#include <libintl.h>
22#include <libintl.h>
26// First, we define an internal class that wraps LightDM's UsersModel. This
24// First, we define an internal class that wraps LightDM's UsersModel. This
27// class will modify some of the data coming from LightDM. For example, we
25// class will modify some of the data coming from LightDM. For example, we
28−// modify any empty Real Names into just normal Names.
26+// modify any empty Real Names into just normal Names. We also add optional
27+// rows, depending on configuration.
29// (We can't modify the data directly in UsersModel below because it won't sort
28// (We can't modify the data directly in UsersModel below because it won't sort
30// using the modified data.)
29// using the modified data.)
31−class MangleModel : public QSortFilterProxyModel
30+class MangleModel : public QIdentityProxyModel
@@ -36,21 +35,66 @@ public:
36 explicit MangleModel(QObject* parent=0);
35 explicit MangleModel(QObject* parent=0);
38 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
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;
42+ int manualRow() const;
43+ int guestRow() const;
45+ void updateShowManual();
41MangleModel::MangleModel(QObject* parent)
50MangleModel::MangleModel(QObject* parent)
42− : QSortFilterProxyModel(parent)
51+ : QIdentityProxyModel(parent)
52+ , m_showManual(false)
54+ if (!Greeter::instance()->hideUsersHint()) {
44− setSourceModel(new QLightDM::UsersModel(this));
55+ setSourceModel(new QLightDM::UsersModel(this));
60+ connect(this, &QIdentityProxyModel::modelReset,
61+ this, &MangleModel::updateShowManual);
62+ connect(this, &QIdentityProxyModel::rowsInserted,
63+ this, &MangleModel::updateShowManual);
64+ connect(this, &QIdentityProxyModel::rowsRemoved,
65+ this, &MangleModel::updateShowManual);
47QVariant MangleModel::data(const QModelIndex &index, int role) const
68QVariant MangleModel::data(const QModelIndex &index, int role) const
49− QVariant variantData = QSortFilterProxyModel::data(index, role);
70+ QVariant variantData;
72+ if (index.row() == manualRow() && index.column() == 0) {
74+ case QLightDM::UsersModel::NameRole: return QStringLiteral("*other");
75+ case QLightDM::UsersModel::RealNameRole: return gettext("Login");
76+ case QLightDM::UsersModel::LoggedInRole: return false;
77+ case QLightDM::UsersModel::SessionRole: return Greeter::instance()->defaultSessionHint();
78+ default: return QVariant();
80+ } else if (index.row() == guestRow() && index.column() == 0) {
82+ case QLightDM::UsersModel::NameRole:
83+ variantData = QStringLiteral("*guest"); break;
84+ case QLightDM::UsersModel::RealNameRole:
85+ variantData = gettext("Guest Session"); break;
86+ case QLightDM::UsersModel::LoggedInRole:
87+ variantData = false; break;
88+ case QLightDM::UsersModel::SessionRole:
89+ variantData = Greeter::instance()->defaultSessionHint(); break;
92+ variantData = QIdentityProxyModel::data(index, role);
51 // If user's real name is empty, switch to unix name
95 // If user's real name is empty, switch to unix name
52 if (role == QLightDM::UsersModel::RealNameRole && variantData.toString().isEmpty()) {
96 if (role == QLightDM::UsersModel::RealNameRole && variantData.toString().isEmpty()) {
53− variantData = QSortFilterProxyModel::data(index, QLightDM::UsersModel::NameRole);
97+ variantData = QIdentityProxyModel::data(index, QLightDM::UsersModel::NameRole);
54 } else if (role == QLightDM::UsersModel::BackgroundPathRole && variantData.toString().startsWith('#')) {
98 } else if (role == QLightDM::UsersModel::BackgroundPathRole && variantData.toString().startsWith('#')) {
55 const QString stringData = "data:image/svg+xml,<svg><rect width='100%' height='100%' fill='" + variantData.toString() + "'/></svg>";
99 const QString stringData = "data:image/svg+xml,<svg><rect width='100%' height='100%' fill='" + variantData.toString() + "'/></svg>";
56 variantData = stringData;
100 variantData = stringData;
@@ -59,34 +103,15 @@ QVariant MangleModel::data(const QModelIndex &index, int role) const
59 return variantData;
103 return variantData;
62−// **** Now we continue with actual UsersModel class ****
64−UsersModel::UsersModel(QObject* parent)
65− : UnitySortFilterProxyModelQML(parent)
66− , m_showManual(false)
68− if (!Greeter::instance()->hideUsersHint()) {
69− setModel(new MangleModel(this));
71− setSortCaseSensitivity(Qt::CaseInsensitive);
72− setSortLocaleAware(true);
73− setSortRole(QLightDM::UsersModel::RealNameRole);
76− connect(this, &UnitySortFilterProxyModelQML::countChanged,
77− this, &UsersModel::updateShowManual);
81−void UsersModel::updateShowManual()
106+void MangleModel::updateShowManual()
83 // Show manual login if we are asked to OR if no other entry exists
108 // Show manual login if we are asked to OR if no other entry exists
84 bool showManual = Greeter::instance()->showManualLoginHint() ||
109 bool showManual = Greeter::instance()->showManualLoginHint() ||
85− (QSortFilterProxyModel::rowCount() == 0 &&
110+ (QIdentityProxyModel::rowCount() == 0 &&
86 !Greeter::instance()->hasGuestAccount());
111 !Greeter::instance()->hasGuestAccount());
88 if (m_showManual != showManual) {
113 if (m_showManual != showManual) {
89− int row = QSortFilterProxyModel::rowCount();
114+ int row = QIdentityProxyModel::rowCount();
90 if (showManual)
115 if (showManual)
91 beginInsertRows(QModelIndex(), row, row);
116 beginInsertRows(QModelIndex(), row, row);
@@ -101,29 +126,29 @@ void UsersModel::updateShowManual()
104−int UsersModel::manualRow() const
129+int MangleModel::manualRow() const
106 if (!m_showManual)
131 if (!m_showManual)
107 return -1;
132 return -1;
109− return QSortFilterProxyModel::rowCount();
134+ return QIdentityProxyModel::rowCount();
112−int UsersModel::guestRow() const
137+int MangleModel::guestRow() const
114 if (!Greeter::instance()->hasGuestAccount())
139 if (!Greeter::instance()->hasGuestAccount())
115 return -1;
140 return -1;
117− int row = QSortFilterProxyModel::rowCount();
142+ int row = QIdentityProxyModel::rowCount();
118 if (m_showManual)
143 if (m_showManual)
121 return row;
146 return row;
124−int UsersModel::rowCount(const QModelIndex &parent) const
149+int MangleModel::rowCount(const QModelIndex &parent) const
126− auto count = UnitySortFilterProxyModelQML::rowCount(parent);
151+ auto count = QIdentityProxyModel::rowCount(parent);
128 if (m_showManual && !parent.isValid())
153 if (m_showManual && !parent.isValid())
@@ -133,36 +158,38 @@ int UsersModel::rowCount(const QModelIndex &parent) const
133 return count;
158 return count;
136−QModelIndex UsersModel::index(int row, int column, const QModelIndex &parent) const
161+QModelIndex MangleModel::index(int row, int column, const QModelIndex &parent) const
138 if ((row == manualRow() || row == guestRow()) && !parent.isValid()) {
163 if ((row == manualRow() || row == guestRow()) && !parent.isValid()) {
139 return createIndex(row, column);
164 return createIndex(row, column);
141− return UnitySortFilterProxyModelQML::index(row, column, parent);
166+ return QIdentityProxyModel::index(row, column, parent);
145−QVariant UsersModel::data(const QModelIndex &index, int role) const
170+// **** Now we continue with actual UsersModel class ****
172+UsersModel::UsersModel(QObject* parent)
173+ : UnitySortFilterProxyModelQML(parent)
147− if (index.row() == manualRow() && index.column() == 0) {
175+ setModel(new MangleModel(this));
148− switch (role) {
176+ setSortCaseSensitivity(Qt::CaseInsensitive);
149− case QLightDM::UsersModel::NameRole: return QStringLiteral("*other");
177+ setSortLocaleAware(true);
150− case QLightDM::UsersModel::RealNameRole: return gettext("Login");
178+ setSortRole(QLightDM::UsersModel::RealNameRole);
151− case QLightDM::UsersModel::LoggedInRole: return false;
179+ sort(0);
152− case QLightDM::UsersModel::SessionRole: return Greeter::instance()->defaultSessionHint();
153− default: return QVariant();
155− } else if (index.row() == guestRow() && index.column() == 0) {
181+
156− switch (role) {
182+bool UsersModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
157− case QLightDM::UsersModel::NameRole: return QStringLiteral("*guest");
184+ auto leftName = source_left.data(QLightDM::UsersModel::NameRole);
158− case QLightDM::UsersModel::RealNameRole: return gettext("Guest Session");
185+ auto rightName = source_right.data(QLightDM::UsersModel::NameRole);
159− case QLightDM::UsersModel::LoggedInRole: return false;
186+
160− case QLightDM::UsersModel::SessionRole: return Greeter::instance()->defaultSessionHint();
187+ if (leftName == QStringLiteral("*guest"))
161− default: return QVariant();
188+ return false;
162− }
189+ if (rightName == QStringLiteral("*guest"))
165− return QSortFilterProxyModel::data(index, role);
192+ return UnitySortFilterProxyModelQML::lessThan(source_left, source_right);
168#include "UsersModel.moc"
195#include "UsersModel.moc"