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/>.
16 * Author: Michael Terry <michael.terry@canonical.com>
20#include "UsersModel.h"
21#include <QLightDM/UsersModel>
22#include <QtCore/QSortFilterProxyModel>
26// 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
28// modify any empty Real Names into just normal Names.
29// (We can't modify the data directly in UsersModel below because it won't sort
30// using the modified data.)
31class MangleModel : public QSortFilterProxyModel
36 explicit MangleModel(QObject* parent=0);
38 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
41MangleModel::MangleModel(QObject* parent)
42 : QSortFilterProxyModel(parent)
44 setSourceModel(new QLightDM::UsersModel(this));
47QVariant MangleModel::data(const QModelIndex &index, int role) const
49 QVariant variantData = QSortFilterProxyModel::data(index, role);
51 // If user's real name is empty, switch to unix name
52 if (role == QLightDM::UsersModel::RealNameRole && variantData.toString().isEmpty()) {
53 variantData = QSortFilterProxyModel::data(index, QLightDM::UsersModel::NameRole);
54 } 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>";
56 variantData = stringData;
62// **** Now we continue with actual UsersModel class ****
64UsersModel::UsersModel(QObject* parent)
65 : UnitySortFilterProxyModelQML(parent)
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);
81void UsersModel::updateShowManual()
83 // Show manual login if we are asked to OR if no other entry exists
84 bool showManual = Greeter::instance()->showManualLoginHint() ||
85 (QSortFilterProxyModel::rowCount() == 0 &&
86 !Greeter::instance()->hasGuestAccount());
88 if (m_showManual != showManual) {
89 int row = QSortFilterProxyModel::rowCount();
91 beginInsertRows(QModelIndex(), row, row);
93 beginRemoveRows(QModelIndex(), row, row);
95 m_showManual = showManual;
104int UsersModel::manualRow() const
109 return QSortFilterProxyModel::rowCount();
112int UsersModel::guestRow() const
114 if (!Greeter::instance()->hasGuestAccount())
117 int row = QSortFilterProxyModel::rowCount();
124int UsersModel::rowCount(const QModelIndex &parent) const
126 auto count = UnitySortFilterProxyModelQML::rowCount(parent);
128 if (m_showManual && !parent.isValid())
130 if (Greeter::instance()->hasGuestAccount() && !parent.isValid())
136QModelIndex UsersModel::index(int row, int column, const QModelIndex &parent) const
138 if ((row == manualRow() || row == guestRow()) && !parent.isValid()) {
139 return createIndex(row, column);
141 return UnitySortFilterProxyModelQML::index(row, column, parent);
145QVariant UsersModel::data(const QModelIndex &index, int role) const
147 if (index.row() == manualRow() && index.column() == 0) {
149 case QLightDM::UsersModel::NameRole: return QStringLiteral("*other");
150 case QLightDM::UsersModel::RealNameRole: return gettext("Login");
151 case QLightDM::UsersModel::LoggedInRole: return false;
152 case QLightDM::UsersModel::SessionRole: return Greeter::instance()->defaultSessionHint();
153 default: return QVariant();
155 } else if (index.row() == guestRow() && index.column() == 0) {
157 case QLightDM::UsersModel::NameRole: return QStringLiteral("*guest");
158 case QLightDM::UsersModel::RealNameRole: return gettext("Guest Session");
159 case QLightDM::UsersModel::LoggedInRole: return false;
160 case QLightDM::UsersModel::SessionRole: return Greeter::instance()->defaultSessionHint();
161 default: return QVariant();
165 return QSortFilterProxyModel::data(index, role);
168#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