Unity 8
UsersModel.cpp
1 /*
2  * Copyright (C) 2013 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  */
18 
19 #include "UsersModel.h"
20 #include <QLightDM/UsersModel>
21 #include <QtCore/QSortFilterProxyModel>
22 
23 // First, we define an internal class that wraps LightDM's UsersModel. This
24 // class will modify some of the data coming from LightDM. For example, we
25 // modify any empty Real Names into just normal Names.
26 // (We can't modify the data directly in UsersModel below because it won't sort
27 // using the modified data.)
28 class MangleModel : public QSortFilterProxyModel
29 {
30  Q_OBJECT
31 
32 public:
33  explicit MangleModel(QObject* parent=0);
34 
35  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
36 };
37 
38 MangleModel::MangleModel(QObject* parent)
39  : QSortFilterProxyModel(parent)
40 {
41  setSourceModel(new QLightDM::UsersModel(this));
42 }
43 
44 QVariant MangleModel::data(const QModelIndex &index, int role) const
45 {
46  QVariant variantData = QSortFilterProxyModel::data(index, role);
47 
48  // If user's real name is empty, switch to unix name
49  if (role == QLightDM::UsersModel::RealNameRole && variantData.toString().isEmpty()) {
50  variantData = QSortFilterProxyModel::data(index, QLightDM::UsersModel::NameRole);
51  } else if (role == QLightDM::UsersModel::BackgroundPathRole && variantData.toString().startsWith('#')) {
52  const QString stringData = "data:image/svg+xml,<svg><rect width='100%' height='100%' fill='" + variantData.toString() + "'/></svg>";
53  variantData = stringData;
54  }
55 
56  return variantData;
57 }
58 
59 // **** Now we continue with actual UsersModel class ****
60 
61 UsersModel::UsersModel(QObject* parent)
62  : UnitySortFilterProxyModelQML(parent)
63 {
64  setModel(new MangleModel(this));
65  setSortCaseSensitivity(Qt::CaseInsensitive);
66  setSortLocaleAware(true);
67  setSortRole(QLightDM::UsersModel::RealNameRole);
68  sort(0);
69 }
70 
71 #include "UsersModel.moc"