Lomiri
SessionsModel.cpp
1 /*
2  * Copyright (C) 2015 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  */
17 
18 
19 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
20  * CHANGES MADE HERE MUST BE REFLECTED ON THE MOCK LIB
21  * COUNTERPART IN tests/mocks/LightDM/IntegratedLightDM/liblightdm
22  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
23 
24 // LightDM currently is Qt4 compatible, and so doesn't define setRoleNames.
25 // To use the same method of setting role name that it does, we
26 // set our compatibility to Qt4 here too.
27 #define QT_DISABLE_DEPRECATED_BEFORE QT_VERSION_CHECK(4, 0, 0)
28 
29 #include "SessionsModel.h"
30 #include "SessionsModelPrivate.h"
31 #include <QtCore/QDir>
32 #include <QtCore/QString>
33 
34 namespace QLightDM
35 {
36 
37 SessionsModel::SessionsModel(QObject* parent) :
38  QAbstractListModel(parent),
39  d_ptr(new SessionsModelPrivate(this))
40 {
41  m_roleNames = QAbstractListModel::roleNames();
42  m_roleNames[KeyRole] = "key";
43  m_roleNames[TypeRole] = "type";
44 }
45 
46 SessionsModel::~SessionsModel()
47 {
48  delete d_ptr;
49 }
50 
51 QVariant SessionsModel::data(const QModelIndex& index, int role) const
52 {
53  Q_D(const SessionsModel);
54 
55  if(!index.isValid()) {
56  return QVariant();
57  }
58 
59  int row = index.row();
60 
61  switch (role) {
62  case QLightDM::SessionsModel::KeyRole:
63  return d->sessionItems[row].key;
64  case Qt::DisplayRole:
65  return d->sessionItems[row].name;
66  default:
67  return QVariant();
68  }
69 }
70 
71 QHash<int, QByteArray> SessionsModel::roleNames() const
72 {
73  return m_roleNames;
74 }
75 
76 int SessionsModel::rowCount(const QModelIndex& parent) const
77 {
78  Q_D(const SessionsModel);
79 
80  if (parent.isValid()) {
81  return 0;
82  } else { // parent is root
83  return d->sessionItems.size();
84  }
85 }
86 
87 } // namespace QLightDM