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 
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  setRoleNames(roles);
56 
57  // Now modify our mock user backgrounds
58  QDir bgdir = QDir(QStringLiteral("/usr/share/demo-assets/shell/backgrounds/"));
59  QStringList backgrounds = bgdir.entryList(QDir::Files | QDir::NoDotAndDotDot);
60 
61  for (int i = 0, j = 0; i < d->entries.size(); i++) {
62  Entry &entry = d->entries[i];
63  if (entry.background.isNull() && !backgrounds.isEmpty()) {
64  entry.background = bgdir.filePath(backgrounds[j++]);
65  if (j >= backgrounds.length()) {
66  j = 0;
67  }
68  }
69  }
70 }
71 
72 UsersModel::~UsersModel()
73 {
74  delete d_ptr;
75 }
76 
77 int UsersModel::rowCount(const QModelIndex &parent) const
78 {
79  Q_D(const UsersModel);
80 
81  if (parent.isValid()) {
82  return 0;
83  } else { // parent is root
84  return d->entries.size();
85  }
86 }
87 
88 QVariant UsersModel::data(const QModelIndex &index, int role) const
89 {
90  Q_D(const UsersModel);
91 
92  if (!index.isValid()) {
93  return QVariant();
94  }
95 
96  int row = index.row();
97  switch (role) {
98  case Qt::DisplayRole:
99  return d->entries[row].real_name;
100  case Qt::DecorationRole:
101  return QIcon();
102  case UsersModel::NameRole:
103  return d->entries[row].username;
104  case UsersModel::RealNameRole:
105  return d->entries[row].real_name;
106  case UsersModel::SessionRole:
107  return d->entries[row].session;
108  case UsersModel::LoggedInRole:
109  return d->entries[row].is_active;
110  case UsersModel::BackgroundRole:
111  return QPixmap(d->entries[row].background);
112  case UsersModel::BackgroundPathRole:
113  return d->entries[row].background;
114  case UsersModel::HasMessagesRole:
115  return d->entries[row].has_messages;
116  case UsersModel::ImagePathRole:
117  return "";
118  default:
119  return QVariant();
120  }
121 }
122 
123 }