on xenial help sign in
Move extra items inside MangleModel, which is now a simple identity proxy, to avoid confusing the sort proxy model and thus avoid crashes
f01ede6Michael Terry
plugins/LightDM/UsersModel.cpp+54−37
@@ -12,23 +12,22 @@
12 *
12 *
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/>.
15− *
16− * Author: Michael Terry <michael.terry@canonical.com>
17 */
15 */
18
16
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>
23
21
24#include <libintl.h>
22#include <libintl.h>
25
23
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
32{
31{
33 Q_OBJECT
32 Q_OBJECT
34
33
@@ -36,21 +35,41 @@ public:
36 explicit MangleModel(QObject* parent=0);
35 explicit MangleModel(QObject* parent=0);
37
36
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;
40+
41+private:
42+ int guestRow() const;
39};
43};
40
44
41MangleModel::MangleModel(QObject* parent)
45MangleModel::MangleModel(QObject* parent)
42− : QSortFilterProxyModel(parent)
46+ : QIdentityProxyModel(parent)
43{
47{
44 setSourceModel(new QLightDM::UsersModel(this));
48 setSourceModel(new QLightDM::UsersModel(this));
45}
49}
46
50
47QVariant MangleModel::data(const QModelIndex &index, int role) const
51QVariant MangleModel::data(const QModelIndex &index, int role) const
48{
52{
49− QVariant variantData = QSortFilterProxyModel::data(index, role);
53+ QVariant variantData;
54+
55+ if (index.row() == guestRow() && index.column() == 0) {
56+ switch (role) {
57+ case QLightDM::UsersModel::NameRole:
58+ variantData = QStringLiteral("*guest"); break;
59+ case QLightDM::UsersModel::RealNameRole:
60+ variantData = gettext("Guest Session"); break;
61+ case QLightDM::UsersModel::LoggedInRole:
62+ variantData = false; break;
63+ case QLightDM::UsersModel::SessionRole:
64+ variantData = Greeter::instance()->defaultSessionHint(); break;
65+ }
66+ } else {
67+ variantData = QIdentityProxyModel::data(index, role);
68+ }
50
69
51 // If user's real name is empty, switch to unix name
70 // If user's real name is empty, switch to unix name
52 if (role == QLightDM::UsersModel::RealNameRole && variantData.toString().isEmpty()) {
71 if (role == QLightDM::UsersModel::RealNameRole && variantData.toString().isEmpty()) {
53− variantData = QSortFilterProxyModel::data(index, QLightDM::UsersModel::NameRole);
72+ variantData = QIdentityProxyModel::data(index, QLightDM::UsersModel::NameRole);
54 } else if (role == QLightDM::UsersModel::BackgroundPathRole && variantData.toString().startsWith('#')) {
73 } 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>";
74 const QString stringData = "data:image/svg+xml,<svg><rect width='100%' height='100%' fill='" + variantData.toString() + "'/></svg>";
56 variantData = stringData;
75 variantData = stringData;
@@ -59,27 +78,15 @@ QVariant MangleModel::data(const QModelIndex &index, int role) const
59 return variantData;
78 return variantData;
60}
79}
61
80
62−// **** Now we continue with actual UsersModel class ****
63−
64−UsersModel::UsersModel(QObject* parent)
65− : UnitySortFilterProxyModelQML(parent)
66−{
67− setModel(new MangleModel(this));
68− setSortCaseSensitivity(Qt::CaseInsensitive);
69− setSortLocaleAware(true);
70− setSortRole(QLightDM::UsersModel::RealNameRole);
71− sort(0);
72−}
73−
74−int UsersModel::guestRow() const
81+int MangleModel::guestRow() const
75{
82{
76 return Greeter::instance()->hasGuestAccount() ?
83 return Greeter::instance()->hasGuestAccount() ?
77− UnitySortFilterProxyModelQML::rowCount() : -1;
84+ QIdentityProxyModel::rowCount() : -1;
78}
85}
79
86
80−int UsersModel::rowCount(const QModelIndex &parent) const
87+int MangleModel::rowCount(const QModelIndex &parent) const
81{
88{
82− auto count = UnitySortFilterProxyModelQML::rowCount(parent);
89+ auto count = QIdentityProxyModel::rowCount(parent);
83
90
84 if (Greeter::instance()->hasGuestAccount() && !parent.isValid())
91 if (Greeter::instance()->hasGuestAccount() && !parent.isValid())
85 count++;
92 count++;
@@ -87,28 +94,38 @@ int UsersModel::rowCount(const QModelIndex &parent) const
87 return count;
94 return count;
88}
95}
89
96
90−QModelIndex UsersModel::index(int row, int column, const QModelIndex &parent) const
97+QModelIndex MangleModel::index(int row, int column, const QModelIndex &parent) const
91{
98{
92 if (row == guestRow() && !parent.isValid()) {
99 if (row == guestRow() && !parent.isValid()) {
93 return createIndex(row, column);
100 return createIndex(row, column);
94 } else {
101 } else {
95− return UnitySortFilterProxyModelQML::index(row, column, parent);
102+ return QIdentityProxyModel::index(row, column, parent);
96 }
103 }
97}
104}
98
105
99−QVariant UsersModel::data(const QModelIndex &index, int role) const
106+// **** Now we continue with actual UsersModel class ****
107+
108+UsersModel::UsersModel(QObject* parent)
109+ : UnitySortFilterProxyModelQML(parent)
100{
110{
101− if (index.row() == guestRow() && index.column() == 0) {
111+ setModel(new MangleModel(this));
102− switch (role) {
112+ setSortCaseSensitivity(Qt::CaseInsensitive);
103− case QLightDM::UsersModel::NameRole: return QStringLiteral("*guest");
113+ setSortLocaleAware(true);
104− case QLightDM::UsersModel::RealNameRole: return gettext("Guest Session");
114+ setSortRole(QLightDM::UsersModel::RealNameRole);
105− case QLightDM::UsersModel::LoggedInRole: return false;
115+ sort(0);
106− case QLightDM::UsersModel::SessionRole: return Greeter::instance()->defaultSessionHint();
116+}
107− default: return QVariant();
117+
108− }
118+bool UsersModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
109− }
119+{
120+ auto leftName = source_left.data(QLightDM::UsersModel::NameRole);
121+ auto rightName = source_right.data(QLightDM::UsersModel::NameRole);
122+
123+ if (leftName == QStringLiteral("*guest"))
124+ return false;
125+ if (rightName == QStringLiteral("*guest"))
126+ return true;
110
127
111− return QSortFilterProxyModel::data(index, role);
128+ return UnitySortFilterProxyModelQML::lessThan(source_left, source_right);
112}
129}
113
130
114#include "UsersModel.moc"
131#include "UsersModel.moc"
plugins/LightDM/UsersModel.h+2−6
@@ -30,10 +30,6 @@ class UsersModel : public UnitySortFilterProxyModelQML
30public:
30public:
31 explicit UsersModel(QObject* parent=0);
31 explicit UsersModel(QObject* parent=0);
32
32
33− int rowCount(const QModelIndex &parent = QModelIndex()) const override;
33+protected:
34− QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
34+ bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
35− QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
36−
37−private:
38− int guestRow() const;
39};
35};
tests/mocks/AccountsService/AccountsService.cpp+2−2
@@ -121,8 +121,8 @@ QString AccountsService::backgroundFile() const
121
121
122 // Check if our mock user has a background set in liblightdm
122 // Check if our mock user has a background set in liblightdm
123 for (int i = 0; i < m_usersModel->count(); i++) {
123 for (int i = 0; i < m_usersModel->count(); i++) {
124− if (m_usersModel->data(m_usersModel->index(i, 0), QLightDM::UsersModel::NameRole) == m_user) {
124+ if (m_usersModel->data(i, QLightDM::UsersModel::NameRole) == m_user) {
125− return m_usersModel->data(m_usersModel->index(i, 0), QLightDM::UsersModel::BackgroundPathRole).toString();
125+ return m_usersModel->data(i, QLightDM::UsersModel::BackgroundPathRole).toString();
126 }
126 }
127 }
127 }
128
128