~mzanetti/unity8/update-wallpaper

« back to all changes in this revision

Viewing changes to plugins/LightDM/UsersModel.cpp

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
 
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 data = QSortFilterProxyModel::data(index, role);
 
47
 
 
48
    // If user's real name is empty, switch to unix name
 
49
    if (role == QLightDM::UsersModel::RealNameRole && data.toString().isEmpty()) {
 
50
        data = QSortFilterProxyModel::data(index, QLightDM::UsersModel::NameRole);
 
51
    } else if (role == QLightDM::UsersModel::BackgroundPathRole && data.toString().startsWith('#')) {
 
52
        data = "data:image/svg+xml,<svg><rect width='100%' height='100%' fill='" + data.toString() + "'/></svg>";
 
53
    }
 
54
 
 
55
    return data;
 
56
}
 
57
 
 
58
// **** Now we continue with actual UsersModel class ****
 
59
 
 
60
UsersModel::UsersModel(QObject* parent)
 
61
  : QSortFilterProxyModelQML(parent)
 
62
{
 
63
    setModel(new MangleModel(this));
 
64
    setSortCaseSensitivity(Qt::CaseInsensitive);
 
65
    setSortLocaleAware(true);
 
66
    setSortRole(QLightDM::UsersModel::RealNameRole);
 
67
    sort(0);
 
68
}
 
69
 
 
70
#include "UsersModel.moc"