2 * Copyright (C) 2013,2015-2016 Canonical, Ltd.
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.
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.
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/>.
18#include "UsersModel.h"
19#include <QIdentityProxyModel>
20#include <QLightDM/UsersModel>
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
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;
45MangleModel::MangleModel(QObject* parent)
46 : QIdentityProxyModel(parent)
48 setSourceModel(new QLightDM::UsersModel(this));
51QVariant MangleModel::data(const QModelIndex &index, int role) const
55 if (index.row() == guestRow() && index.column() == 0) {
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;
67 variantData = QIdentityProxyModel::data(index, role);
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;
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())
97QModelIndex MangleModel::index(int row, int column, const QModelIndex &parent) const
99 if (row == guestRow() && !parent.isValid()) {
100 return createIndex(row, column);
102 return QIdentityProxyModel::index(row, column, parent);
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);
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"))
125 if (rightName == QStringLiteral("*guest"))
128 return UnitySortFilterProxyModelQML::lessThan(source_left, source_right);
131#include "UsersModel.moc"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list