on 09dac0d help sign in
unity8/plugins/LightDM/UsersModel.cpp
131 lines 4.4 kB history raw
1/*
2 * Copyright (C) 2013,2015-2016 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
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/>.
15 */
17#include "Greeter.h"
18#include "UsersModel.h"
19#include <QIdentityProxyModel>
20#include <QLightDM/UsersModel>
22#include <libintl.h>
24// First, we define an internal class that wraps LightDM's UsersModel. This
25// class will modify some of the data coming from LightDM. For example, we
26// modify any empty Real Names into just normal Names. We also add optional
27// rows, depending on configuration.
28// (We can't modify the data directly in UsersModel below because it won't sort
29// using the modified data.)
30class MangleModel : public QIdentityProxyModel
32 Q_OBJECT
34public:
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;
41private:
42 int guestRow() const;
43};
45MangleModel::MangleModel(QObject* parent)
46 : QIdentityProxyModel(parent)
48 setSourceModel(new QLightDM::UsersModel(this));
51QVariant MangleModel::data(const QModelIndex &index, int role) const
53 QVariant variantData;
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 }
70 // If user's real name is empty, switch to unix name
71 if (role == QLightDM::UsersModel::RealNameRole && variantData.toString().isEmpty()) {
72 variantData = QIdentityProxyModel::data(index, QLightDM::UsersModel::NameRole);
73 } else if (role == QLightDM::UsersModel::BackgroundPathRole && variantData.toString().startsWith('#')) {
74 const QString stringData = "data:image/svg+xml,<svg><rect width='100%' height='100%' fill='" + variantData.toString() + "'/></svg>";
75 variantData = stringData;
76 }
78 return variantData;
81int MangleModel::guestRow() const
83 return Greeter::instance()->hasGuestAccount() ?
84 QIdentityProxyModel::rowCount() : -1;
87int MangleModel::rowCount(const QModelIndex &parent) const
89 auto count = QIdentityProxyModel::rowCount(parent);
91 if (Greeter::instance()->hasGuestAccount() && !parent.isValid())
92 count++;
94 return count;
97QModelIndex MangleModel::index(int row, int column, const QModelIndex &parent) const
99 if (row == guestRow() && !parent.isValid()) {
100 return createIndex(row, column);
101 } else {
102 return QIdentityProxyModel::index(row, column, parent);
103 }
106// **** Now we continue with actual UsersModel class ****
108UsersModel::UsersModel(QObject* parent)
109 : UnitySortFilterProxyModelQML(parent)
111 setModel(new MangleModel(this));
112 setSortCaseSensitivity(Qt::CaseInsensitive);
113 setSortLocaleAware(true);
114 setSortRole(QLightDM::UsersModel::RealNameRole);
115 sort(0);
118bool UsersModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
120 auto leftName = source_left.data(QLightDM::UsersModel::NameRole);
121 auto rightName = source_right.data(QLightDM::UsersModel::NameRole);
123 if (leftName == QStringLiteral("*guest"))
124 return false;
125 if (rightName == QStringLiteral("*guest"))
126 return true;
128 return UnitySortFilterProxyModelQML::lessThan(source_left, source_right);
131#include "UsersModel.moc"