on 2d4064c help sign in
unity8/plugins/LightDM/UsersModel.cpp
119 lines 3.9 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 *
16 * Author: Michael Terry <michael.terry@canonical.com>
17 */
19#include "Greeter.h"
20#include "UsersModel.h"
21#include <QLightDM/UsersModel>
22#include <QtCore/QSortFilterProxyModel>
24#include <libintl.h>
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
33 Q_OBJECT
35public:
36 explicit MangleModel(QObject* parent=0);
38 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
39};
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;
57 }
59 return variantData;
62// **** Now we continue with actual UsersModel class ****
64UsersModel::UsersModel(QObject* parent)
65 : UnitySortFilterProxyModelQML(parent)
67 setModel(new MangleModel(this));
68 setSortCaseSensitivity(Qt::CaseInsensitive);
69 setSortLocaleAware(true);
70 setSortRole(QLightDM::UsersModel::RealNameRole);
71 sort(0);
74int UsersModel::guestRow() const
76 return Greeter::instance()->hasGuestAccount() ?
77 UnitySortFilterProxyModelQML::rowCount() : -1;
80int UsersModel::rowCount(const QModelIndex &parent) const
82 auto count = UnitySortFilterProxyModelQML::rowCount(parent);
84 if (Greeter::instance()->hasGuestAccount() && !parent.isValid())
85 count++;
87 return count;
90QModelIndex UsersModel::index(int row, int column, const QModelIndex &parent) const
92 if (row == guestRow() && !parent.isValid()) {
93 return createIndex(row, column);
94 } else {
95 return UnitySortFilterProxyModelQML::index(row, column, parent);
96 }
99QVariant UsersModel::data(const QModelIndex &index, int role) const
101 if (index.row() == guestRow() && index.column() == 0) {
102 switch (role) {
103 case QLightDM::UsersModel::NameRole: return QStringLiteral("*guest");
104 case QLightDM::UsersModel::RealNameRole: return gettext("Guest Session");
105 default: return QVariant();
106 }
107 }
109 return QSortFilterProxyModel::data(index, role);
112QObject *UsersModel::mock()
114 // get through MangleModel down to QLightDM::UsersModel
115 MangleModel *mangleModel = static_cast<MangleModel*>(sourceModel());
116 return mangleModel->sourceModel()->property("mock").value<QObject*>();
119#include "UsersModel.moc"