on 1663e5c help sign in
unity8/tests/mocks/LightDM/UsersModel.cpp
117 lines 3.4 kB history raw
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 */
19// LightDM currently is Qt4 compatible, and so doesn't define setRoleNames.
20// To use the same method of setting role name that it does, we
21// set our compatibility to Qt4 here too.
22#define QT_DISABLE_DEPRECATED_BEFORE QT_VERSION_CHECK(4, 0, 0)
24#include "UsersModel.h"
25#include "UsersModelPrivate.h"
26#include <QtCore/QDir>
27#include <QtCore/QString>
28#include <QtGui/QIcon>
30namespace QLightDM
33UsersModel::UsersModel(QObject *parent) :
34 QAbstractListModel(parent),
35 d_ptr(new UsersModelPrivate(this))
37 Q_D(UsersModel);
39 // Extend roleNames (we want to keep the "display" role)
40 QHash<int, QByteArray> roles = roleNames();
41 roles[NameRole] = "name";
42 roles[RealNameRole] = "realName";
43 roles[LoggedInRole] = "loggedIn";
44 roles[BackgroundRole] = "background";
45 roles[BackgroundPathRole] = "backgroundPath";
46 roles[SessionRole] = "session";
47 roles[HasMessagesRole] = "hasMessages";
48 roles[ImagePathRole] = "imagePath";
49 setRoleNames(roles);
51 // Now modify our mock user backgrounds
52 QDir bgdir = QDir("/usr/share/demo-assets/shell/backgrounds/");
53 QStringList backgrounds = bgdir.entryList(QDir::Files | QDir::NoDotAndDotDot);
55 for (int i = 0, j = 0; i < d->entries.size(); i++) {
56 Entry &entry = d->entries[i];
57 if (entry.background.isNull() && !backgrounds.isEmpty()) {
58 entry.background = bgdir.filePath(backgrounds[j++]);
59 if (j >= backgrounds.length()) {
60 j = 0;
61 }
62 }
63 }
66UsersModel::~UsersModel()
68 delete d_ptr;
71int UsersModel::rowCount(const QModelIndex &parent) const
73 Q_D(const UsersModel);
75 if (parent.isValid()) {
76 return 0;
77 } else { // parent is root
78 return d->entries.size();
79 }
82QVariant UsersModel::data(const QModelIndex &index, int role) const
84 Q_D(const UsersModel);
86 if (!index.isValid()) {
87 return QVariant();
88 }
90 int row = index.row();
91 switch (role) {
92 case Qt::DisplayRole:
93 return d->entries[row].real_name;
94 case Qt::DecorationRole:
95 return QIcon();
96 case UsersModel::NameRole:
97 return d->entries[row].username;
98 case UsersModel::RealNameRole:
99 return d->entries[row].real_name;
100 case UsersModel::SessionRole:
101 return d->entries[row].session;
102 case UsersModel::LoggedInRole:
103 return d->entries[row].is_active;
104 case UsersModel::BackgroundRole:
105 return QPixmap(d->entries[row].background);
106 case UsersModel::BackgroundPathRole:
107 return d->entries[row].background;
108 case UsersModel::HasMessagesRole:
109 return d->entries[row].has_messages;
110 case UsersModel::ImagePathRole:
111 return "";
112 default:
113 return QVariant();
114 }