Unity 8
UsersModel.cpp
1 /*
2  * Copyright (C) 2013-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  */
18 
19 
20 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21  * CHANGES MADE HERE MUST BE REFLECTED ON THE MOCK LIB
22  * COUNTERPART IN tests/mocks/Lightdm/liblightdm
23  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
24 
25 // LightDM currently is Qt4 compatible, and so doesn't define setRoleNames.
26 // To use the same method of setting role name that it does, we
27 // set our compatibility to Qt4 here too.
28 #define QT_DISABLE_DEPRECATED_BEFORE QT_VERSION_CHECK(4, 0, 0)
29 
30 #include "UsersModel.h"
31 #include "UsersModelPrivate.h"
32 #include <QtCore/QDir>
33 #include <QtCore/QString>
34 #include <QtGui/QIcon>
35 
36 namespace QLightDM
37 {
38 
39 UsersModel::UsersModel(QObject *parent) :
40  QAbstractListModel(parent),
41  d_ptr(new UsersModelPrivate(this))
42 {
43  Q_D(UsersModel);
44 
45  // Extend roleNames (we want to keep the "display" role)
46  QHash<int, QByteArray> roles = roleNames();
47  roles[NameRole] = "name";
48  roles[RealNameRole] = "realName";
49  roles[LoggedInRole] = "loggedIn";
50  roles[BackgroundRole] = "background";
51  roles[BackgroundPathRole] = "backgroundPath";
52  roles[SessionRole] = "session";
53  roles[HasMessagesRole] = "hasMessages";
54  roles[ImagePathRole] = "imagePath";
55  roles[UidRole] = "uid";
56  setRoleNames(roles);
57 
58  connect(d_ptr, &UsersModelPrivate::dataChanged, this, [this](int i) {
59  QModelIndex index = createIndex(i, 0);
60  Q_EMIT dataChanged(index, index);
61  });
62 }
63 
64 int UsersModel::rowCount(const QModelIndex &parent) const
65 {
66  Q_D(const UsersModel);
67 
68  if (parent.isValid()) {
69  return 0;
70  } else { // parent is root
71  return d->entries.size();
72  }
73 }
74 
75 QVariant UsersModel::data(const QModelIndex &index, int role) const
76 {
77  Q_D(const UsersModel);
78 
79  if (!index.isValid()) {
80  return QVariant();
81  }
82 
83  int row = index.row();
84  switch (role) {
85  case Qt::DisplayRole:
86  return d->entries[row].real_name;
87  case Qt::DecorationRole:
88  return QIcon();
89  case UsersModel::NameRole:
90  return d->entries[row].username;
91  case UsersModel::RealNameRole:
92  return d->entries[row].real_name;
93  case UsersModel::SessionRole:
94  return d->entries[row].session;
95  case UsersModel::LoggedInRole:
96  return d->entries[row].is_active;
97  case UsersModel::BackgroundRole:
98  return QPixmap(d->entries[row].background);
99  case UsersModel::BackgroundPathRole:
100  return d->entries[row].background;
101  case UsersModel::HasMessagesRole:
102  return d->entries[row].has_messages;
103  case UsersModel::ImagePathRole:
104  return "";
105  case UsersModel::UidRole:
106  return d->entries[row].uid;
107  default:
108  return QVariant();
109  }
110 }
111 
112 }