on xenial help sign in
Implement sessionsModel
commit4fd9ab3ae0573e2d1413d99b77da753ec3bd8341 authorJosh Arenson <joshua.arenson@canonical.com> dateTue 17 Nov 2015, 19:52 (11 years ago) parent7e12dc0
22 files changed+764−33
plugins/LightDM/CMakeLists.txt+1 plugins/LightDM/Greeter.cpp+7−2 plugins/LightDM/Greeter.h+3−2 plugins/LightDM/IntegratedLightDM/liblightdm/CMakeLists.txt+2 plugins/LightDM/IntegratedLightDM/liblightdm/SessionsModel.cpp+87 plugins/LightDM/IntegratedLightDM/liblightdm/SessionsModel.h+72 plugins/LightDM/IntegratedLightDM/liblightdm/SessionsModelPrivate.cpp+41 plugins/LightDM/IntegratedLightDM/liblightdm/SessionsModelPrivate.h+54 plugins/LightDM/SessionsModel.cpp+83 plugins/LightDM/SessionsModel.h+60 plugins/LightDM/plugin.cpp+17−7 tests/mocks/LightDM/IntegratedLightDM/CMakeLists.txt+2 tests/mocks/LightDM/IntegratedLightDM/MockSessionsModel.h+33 tests/mocks/LightDM/IntegratedLightDM/QLightDM/SessionsModel+17 tests/mocks/LightDM/IntegratedLightDM/liblightdm/CMakeLists.txt+2 tests/mocks/LightDM/IntegratedLightDM/liblightdm/Greeter.cpp+1−1 tests/mocks/LightDM/IntegratedLightDM/liblightdm/SessionsModel.cpp+82 tests/mocks/LightDM/IntegratedLightDM/liblightdm/SessionsModel.h+66 tests/mocks/LightDM/IntegratedLightDM/liblightdm/SessionsModelPrivate.cpp+44 tests/mocks/LightDM/IntegratedLightDM/liblightdm/SessionsModelPrivate.h+58 tests/mocks/LightDM/IntegratedLightDM/liblightdm/UsersModelPrivate.cpp+21−21 tests/mocks/LightDM/IntegratedLightDM/plugin.cpp+11
plugins/LightDM/CMakeLists.txt+1
@@ -20,6 +20,7 @@ set(QMLPLUGIN_SRC
2020 DBusGreeterList.cpp
2121 Greeter.cpp
2222 plugin.cpp
23+ SessionsModel.cpp
2324 UsersModel.cpp
2425 )
2526
plugins/LightDM/Greeter.cpp+7−2
@@ -1,5 +1,5 @@
11/*
2− * Copyright (C) 2013 Canonical, Ltd.
2+ * Copyright (C) 2013, 2015 Canonical, Ltd.
33 *
44 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
@@ -13,7 +13,6 @@
1313 * You should have received a copy of the GNU General Public License
1414 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1515 *
16− * Author: Michael Terry <michael.terry@canonical.com>
1716 */
1817
1918#include "Greeter.h"
@@ -72,6 +71,12 @@ QString Greeter::authenticationUser() const
7271 return d->m_greeter->authenticationUser();
7372}
7473
74+QString Greeter::defaultSessionHint() const
75+{
76+ Q_D(const Greeter);
77+ return d->m_greeter->defaultSessionHint();
78+}
79+
7580bool Greeter::promptless() const
7681{
7782 Q_D(const Greeter);
plugins/LightDM/Greeter.h+3−2
@@ -1,5 +1,5 @@
11/*
2− * Copyright (C) 2012,2013 Canonical, Ltd.
2+ * Copyright (C) 2012,2013,2015 Canonical, Ltd.
33 *
44 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
@@ -13,7 +13,6 @@
1313 * You should have received a copy of the GNU General Public License
1414 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1515 *
16− * Authors: Michael Terry <michael.terry@canonical.com>
1716 */
1817
1918/* This class is a really tiny filter around QLightDM::Greeter. There are some
@@ -37,6 +36,7 @@ class Greeter : public QObject
3736 Q_PROPERTY(bool active READ isActive WRITE setIsActive NOTIFY isActiveChanged)
3837 Q_PROPERTY(bool authenticated READ isAuthenticated NOTIFY isAuthenticatedChanged)
3938 Q_PROPERTY(QString authenticationUser READ authenticationUser NOTIFY authenticationUserChanged)
39+ Q_PROPERTY(QString defaultSession READ defaultSessionHint CONSTANT)
4040 Q_PROPERTY(bool promptless READ promptless NOTIFY promptlessChanged)
4141
4242public:
@@ -45,6 +45,7 @@ public:
4545 bool isActive() const;
4646 bool isAuthenticated() const;
4747 QString authenticationUser() const;
48+ QString defaultSessionHint() const;
4849 bool promptless() const;
4950
5051public Q_SLOTS:
plugins/LightDM/IntegratedLightDM/liblightdm/CMakeLists.txt+2
@@ -2,8 +2,10 @@ pkg_check_modules(LIBUSERMETRICSOUTPUT REQUIRED libusermetricsoutput-1)
22
33set(LibLightDM_SOURCES
44 Greeter.cpp
5+ SessionsModel.cpp
56 UsersModel.cpp
67 GreeterPrivate.cpp
8+ SessionsModelPrivate.cpp
79 UsersModelPrivate.cpp
810 ${CMAKE_SOURCE_DIR}/plugins/Utils/qvariantlistmodel.cpp
911 )
plugins/LightDM/IntegratedLightDM/liblightdm/SessionsModel.cppnew file+87
@@ -0,0 +1,87 @@
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+#include <QtGui/QIcon>
34+
35+namespace QLightDM
36+{
37+
38+SessionsModel::SessionsModel(QObject* parent) :
39+ QAbstractListModel(parent),
40+ d_ptr(new SessionsModelPrivate(this))
41+{
42+ Q_D(SessionsModel);
43+ m_roleNames = QAbstractListModel::roleNames();
44+ m_roleNames[KeyRole] = "key";
45+ m_roleNames[TypeRole] = "type";
46+}
47+
48+SessionsModel::~SessionsModel()
49+{
50+ delete d_ptr;
51+}
52+
53+QVariant SessionsModel::data(const QModelIndex& index, int role) const
54+{
55+ Q_D(const SessionsModel);
56+
57+ if(!index.isValid()) {
58+ return QVariant();
59+ }
60+
61+ int row = index.row();
62+
63+ switch (role) {
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
plugins/LightDM/IntegratedLightDM/liblightdm/SessionsModel.hnew file+72
@@ -0,0 +1,72 @@
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+#ifndef UNITY_MOCK_SESSIONSMODEL_H
19+#define UNITY_MOCK_SESSIONSMODEL_H
20+
21+#include <QtCore/QAbstractListModel>
22+#include <QtCore/QString>
23+
24+/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
25+ * CHANGES MADE HERE MUST BE REFLECTED ON THE MOCK LIB
26+ * COUNTERPART IN tests/mocks/LightDM/IntegratedLightDM/liblightdm
27+ * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
28+
29+// This is taken from liblightdm and modified to confirm to our syle conventions
30+namespace QLightDM
31+{
32+class SessionsModelPrivate;
33+
34+class Q_DECL_EXPORT SessionsModel : public QAbstractListModel
35+ {
36+ Q_OBJECT
37+
38+ Q_ENUMS(SessionModelRoles SessionType)
39+
40+ public:
41+
42+ enum SessionModelRoles {
43+ //name is exposed as Qt::DisplayRole
44+ //comment is exposed as Qt::TooltipRole
45+ KeyRole = Qt::UserRole,
46+ IdRole = KeyRole, /** Deprecated */
47+ TypeRole
48+ };
49+
50+ enum SessionType {
51+ LocalSessions,
52+ RemoteSessions
53+ };
54+
55+ explicit SessionsModel(QObject* parent=0); /** Deprecated. Loads local sessions*/
56+ explicit SessionsModel(SessionsModel::SessionType, QObject* parent=0);
57+ virtual ~SessionsModel();
58+
59+ QHash<int, QByteArray> roleNames() const override;
60+ int rowCount(const QModelIndex& parent) const override;
61+ QVariant data(const QModelIndex& index, int role) const override;
62+
63+ protected:
64+ SessionsModelPrivate* const d_ptr;
65+
66+ private:
67+ QHash<int, QByteArray> m_roleNames;
68+ Q_DECLARE_PRIVATE(SessionsModel)
69+ };
70+}
71+
72+#endif // UNITY_MOCK_SESSIONSMODEL_H
plugins/LightDM/IntegratedLightDM/liblightdm/SessionsModelPrivate.cppnew file+41
@@ -0,0 +1,41 @@
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+#include "SessionsModelPrivate.h"
19+
20+#include <QDir>
21+#include <QSettings>
22+#include <QStringList>
23+
24+namespace QLightDM
25+{
26+
27+SessionsModelPrivate::SessionsModelPrivate(SessionsModel* parent)
28+ : q_ptr(parent)
29+{
30+ QSettings settings(QDir::homePath() + "/.unity8-greeter-demo", QSettings::NativeFormat);
31+ const QStringList sessions = settings.value(QStringLiteral("sessions"), QStringList() << qgetenv("DESKTOP_SESSION")).toStringList();
32+
33+ sessionItems.reserve(sessions.count());
34+ Q_FOREACH(const QString& session, sessions)
35+ {
36+ sessionItems.append({"", "", session, ""});
37+ }
38+
39+}
40+
41+} // namespace QLightDM
plugins/LightDM/IntegratedLightDM/liblightdm/SessionsModelPrivate.hnew file+54
@@ -0,0 +1,54 @@
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+#ifndef UNITY_MOCK_SESSIONSMODEL_PRIVATE_H
19+#define UNITY_MOCK_SESSIONSMODEL_PRIVATE_H
20+
21+#include <QtCore/QList>
22+#include <QtCore/QString>
23+
24+namespace QLightDM
25+{
26+class SessionsModel;
27+
28+class SessionItem
29+{
30+public:
31+ QString key; // unused
32+ QString type; // unused
33+ QString name;
34+ QString comment; // unused
35+};
36+
37+class SessionsModelPrivate
38+{
39+public:
40+ explicit SessionsModelPrivate(SessionsModel* parent=0);
41+ virtual ~SessionsModelPrivate() = default;
42+
43+ QList<SessionItem> sessionItems;
44+
45+protected:
46+ SessionsModel* const q_ptr;
47+
48+private:
49+ Q_DECLARE_PUBLIC(SessionsModel)
50+};
51+
52+} // namespace QLightDM
53+
54+#endif // UNITY_MOCK_SESSIONSMODEL_PRIVATE_H
plugins/LightDM/SessionsModel.cppnew file+83
@@ -0,0 +1,83 @@
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+#include "SessionsModel.h"
19+#include <QtCore/QFile>
20+#include <QtCore/QSortFilterProxyModel>
21+QHash<int, QByteArray> SessionsModel::roleNames() const
22+{
23+ return m_roleNames;
24+}
25+
26+int SessionsModel::rowCount(const QModelIndex& parent) const
27+{
28+ return m_model->rowCount(parent);
29+}
30+
31+QModelIndex SessionsModel::sessionIndex(QString sessionName) const
32+{
33+ for (int i = 0; i < rowCount(QModelIndex()); i ++) {
34+ QModelIndex index = m_model->index(i, Qt::DisplayRole);
35+ QString aSession(this->data(index, Qt::DisplayRole).toString());
36+ if (!QString::compare(aSession, sessionName, Qt::CaseInsensitive)) {
37+ return index;
38+ }
39+ }
40+ return QModelIndex();
41+}
42+
43+QUrl SessionsModel::iconUrl(QString sessionName) const
44+{
45+ Q_FOREACH(const QUrl& searchDirectory, m_iconSearchDirectories)
46+ {
47+ // This is an established icon naming convention
48+ QString iconUrl = searchDirectory.toString(QUrl::StripTrailingSlash) +
49+ "/" + sessionName.toLower() + "_badge.png";
50+ QFile iconFile(iconUrl);
51+ if (iconFile.exists()) {
52+ return QUrl(iconUrl);
53+ }
54+ }
55+
56+ // FIXME make this smarter
57+ return QUrl("./graphics/session_icons/unknown_badge.png");
58+}
59+
60+QVariant SessionsModel::data(const QModelIndex& index, int role) const
61+{
62+ switch (role) {
63+ case SessionsModel::IconRole:
64+ return QVariant(iconUrl(m_model->data(index, Qt::DisplayRole).toString()));
65+ default:
66+ return m_model->data(index, role);
67+ }
68+}
69+
70+SessionsModel::SessionsModel(QObject* parent)
71+ : UnitySortFilterProxyModelQML(parent)
72+{
73+ // Add a custom IconRole that isn't in either of the lightdm implementations
74+ m_model = new QLightDM::SessionsModel(this);
75+ m_roleNames = m_model->roleNames();
76+ m_roleNames[IconRole] = "icon_url";
77+
78+ setModel(m_model);
79+ setSortCaseSensitivity(Qt::CaseInsensitive);
80+ setSortLocaleAware(true);
81+ setSortRole(Qt::DisplayRole);
82+ sort(0);
83+}
plugins/LightDM/SessionsModel.hnew file+60
@@ -0,0 +1,60 @@
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+#ifndef UNITY_SESSIONSMODEL_H
19+#define UNITY_SESSIONSMODEL_H
20+
21+#include <unitysortfilterproxymodelqml.h>
22+#include <QLightDM/SessionsModel>
23+#include <QtCore/QHash>
24+#include <QtCore/QObject>
25+#include <QtCore/QUrl>
26+
27+class SessionsModel : public UnitySortFilterProxyModelQML
28+{
29+ Q_OBJECT
30+
31+ Q_ENUMS(SessionModelRoles)
32+
33+public:
34+ enum SessionModelRoles {
35+ /* This is tricky / ugly. Since we are ultimately chaining 3 enums together,
36+ * the _first_ value of this enum MUST be the _last_ value of
37+ * QLightDM::SessionsModel::SessionModelRoles and consquently, this must
38+ * also match the last value in the corresponding enum of the integrated lib
39+ */
40+ TypeRole = QLightDM::SessionsModel::SessionModelRoles::TypeRole,
41+ IconRole
42+ };
43+
44+ explicit SessionsModel(QObject* parent=0);
45+
46+ QHash<int, QByteArray> roleNames() const override;
47+ int rowCount(const QModelIndex& parent) const override;
48+ Q_INVOKABLE QModelIndex sessionIndex(QString sessionName) const;
49+ QVariant data(const QModelIndex& index, int role) const override;
50+ Q_INVOKABLE QUrl iconUrl(QString sessionName) const;
51+
52+private:
53+ QLightDM::SessionsModel* m_model;
54+ QHash<int, QByteArray> m_roleNames;
55+
56+ // FIXME make this the correct paths
57+ const QList<QUrl> m_iconSearchDirectories{QUrl("/usr/share/unity8/Greeter/graphics/session_icons")};
58+};
59+
60+#endif // UNITY_SESSIONSMODEL_H
plugins/LightDM/plugin.cpp+17−7
@@ -19,9 +19,11 @@
1919#include "DBusGreeter.h"
2020#include "DBusGreeterList.h"
2121#include "Greeter.h"
22+#include "SessionsModel.h"
2223#include "UsersModel.h"
2324#include <libusermetricsoutput/ColorTheme.h>
2425#include <libusermetricsoutput/UserMetrics.h>
26+#include <QLightDM/SessionsModel>
2527#include <QLightDM/UsersModel>
2628
2729#include <QAbstractItemModel>
@@ -40,6 +42,13 @@ static QObject *greeter_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
4042 return greeter;
4143}
4244
45+static QObject *sessions_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
46+{
47+ Q_UNUSED(engine)
48+ Q_UNUSED(scriptEngine)
49+ return new SessionsModel();
50+}
51+
4352static QObject *users_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
4453{
4554 Q_UNUSED(engine)
@@ -61,21 +70,22 @@ void PLUGIN_CLASSNAME::registerTypes(const char *uri)
6170
6271#if defined INTEGRATED_LIGHTDM
6372 Q_ASSERT(uri == QLatin1String("LightDM.IntegratedLightDM"));
64−#elif defined FULL_LIGHTDM
65− Q_ASSERT(uri == QLatin1String("LightDM.FullLightDM"));
66−#endif
67− qRegisterMetaType<QLightDM::Greeter::MessageType>("QLightDM::Greeter::MessageType");
68− qRegisterMetaType<QLightDM::Greeter::PromptType>("QLightDM::Greeter::PromptType");
69−
70−#if defined INTEGRATED_LIGHTDM
7173 qmlRegisterSingletonType<Greeter>(uri, 0, 1, "Greeter", greeter_provider);
7274#elif defined FULL_LIGHTDM
75+ Q_ASSERT(uri == QLatin1String("LightDM.FullLightDM"));
7376 qmlRegisterSingletonType<QLightDM::Greeter>(uri, 0, 1, "Greeter", greeter_provider);
7477#else
7578 #error No library defined in LightDM plugin
7679#endif
7780
81+ qRegisterMetaType<QLightDM::Greeter::MessageType>("QLightDM::Greeter::MessageType");
82+ qRegisterMetaType<QLightDM::Greeter::PromptType>("QLightDM::Greeter::PromptType");
83+
84+ qmlRegisterSingletonType<SessionsModel>(uri, 0, 1, "Sessions", sessions_provider);
85+ qmlRegisterUncreatableType<SessionsModel>(uri, 0, 1, "SessionRoles", QStringLiteral("Type is not instantiable"));
86+
7887 qmlRegisterSingletonType<UsersModel>(uri, 0, 1, "Users", users_provider);
7988 qmlRegisterUncreatableType<QLightDM::UsersModel>(uri, 0, 1, "UserRoles", QStringLiteral("Type is not instantiable"));
89+
8090 qmlRegisterSingletonType<UserMetricsOutput::UserMetrics>(uri, 0, 1, "Infographic", infographic_provider);
8191}
tests/mocks/LightDM/IntegratedLightDM/CMakeLists.txt+2
@@ -19,9 +19,11 @@ set(QMLPLUGIN_SRC
1919 ${CMAKE_SOURCE_DIR}/plugins/LightDM/DBusGreeter.cpp
2020 ${CMAKE_SOURCE_DIR}/plugins/LightDM/DBusGreeterList.cpp
2121 ${CMAKE_SOURCE_DIR}/plugins/LightDM/Greeter.cpp
22+ ${CMAKE_SOURCE_DIR}/plugins/LightDM/SessionsModel.cpp
2223 ${CMAKE_SOURCE_DIR}/plugins/LightDM/UsersModel.cpp
2324 ${CMAKE_SOURCE_DIR}/plugins/Utils/unitysortfilterproxymodelqml.cpp
2425 MockGreeter.cpp
26+ MockSessionsModel.h
2527 MockUsersModel.cpp
2628 plugin.cpp
2729 )
tests/mocks/LightDM/IntegratedLightDM/MockSessionsModel.hnew file+33
@@ -0,0 +1,33 @@
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+#ifndef MOCK_UNITY_SESSIONSMODEL_H
19+#define MOCK_UNITY_SESSIONSMODEL_H
20+
21+#include <SessionsModel.h>
22+#include <QLightDM/SessionsModel>
23+
24+// A wrapper class to preserve naming conventions, thus
25+// enhancing code readability
26+class MockSessionsModel : public SessionsModel
27+{
28+ Q_OBJECT
29+
30+ using SessionsModel::SessionsModel;
31+};
32+
33+#endif // MOCK_UNITY_SESSIONSMODEL_H
tests/mocks/LightDM/IntegratedLightDM/QLightDM/SessionsModelnew file+17
@@ -0,0 +1,17 @@
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+#include "../liblightdm/SessionsModel.h"
tests/mocks/LightDM/IntegratedLightDM/liblightdm/CMakeLists.txt+2
@@ -1,7 +1,9 @@
11set(LibLightDM_SOURCES
22 Greeter.cpp
3+ SessionsModel.cpp
34 UsersModel.cpp
45 GreeterPrivate.cpp
6+ SessionsModelPrivate.cpp
57 UsersModelPrivate.cpp
68 ${CMAKE_SOURCE_DIR}/plugins/Utils/qvariantlistmodel.cpp
79 )
tests/mocks/LightDM/IntegratedLightDM/liblightdm/Greeter.cpp+1−1
@@ -51,7 +51,7 @@ QString Greeter::getHint(const QString &name) const
5151
5252QString Greeter::defaultSessionHint() const
5353{
54− return "ubuntu";
54+ return QStringLiteral("ubuntu");
5555}
5656
5757bool Greeter::hideUsersHint() const
tests/mocks/LightDM/IntegratedLightDM/liblightdm/SessionsModel.cppnew file+82
@@ -0,0 +1,82 @@
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+// 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)
23+
24+#include "SessionsModel.h"
25+#include "SessionsModelPrivate.h"
26+#include <QtCore/QDir>
27+#include <QtCore/QString>
28+#include <QtGui/QIcon>
29+
30+namespace QLightDM
31+{
32+
33+SessionsModel::SessionsModel(QObject* parent) :
34+ QAbstractListModel(parent),
35+ d_ptr(new SessionsModelPrivate(this))
36+{
37+ Q_D(SessionsModel);
38+ m_roleNames = QAbstractListModel::roleNames();
39+ m_roleNames[KeyRole] = "key";
40+ m_roleNames[TypeRole] = "type";
41+}
42+
43+SessionsModel::~SessionsModel()
44+{
45+ delete d_ptr;
46+}
47+
48+QVariant SessionsModel::data(const QModelIndex& index, int role) const
49+{
50+ Q_D(const SessionsModel);
51+
52+ if(!index.isValid()) {
53+ return QVariant();
54+ }
55+
56+ int row = index.row();
57+
58+ switch (role) {
59+ case Qt::DisplayRole:
60+ return d->sessionItems[row].name;
61+ default:
62+ return QVariant();
63+ }
64+}
65+
66+QHash<int, QByteArray> SessionsModel::roleNames() const
67+{
68+ return m_roleNames;
69+}
70+
71+int SessionsModel::rowCount(const QModelIndex& parent) const
72+{
73+ Q_D(const SessionsModel);
74+
75+ if (parent.isValid()) {
76+ return 0;
77+ } else { // parent is root
78+ return d->sessionItems.size();
79+ }
80+}
81+
82+} // namespace QLightDM
tests/mocks/LightDM/IntegratedLightDM/liblightdm/SessionsModel.hnew file+66
@@ -0,0 +1,66 @@
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+#ifndef UNITY_MOCK_SESSIONSMODEL_H
19+#define UNITY_MOCK_SESSIONSMODEL_H
20+
21+#include <QtCore/QAbstractListModel>
22+#include <QtCore/QString>
23+
24+namespace QLightDM
25+{
26+class SessionsModelPrivate;
27+
28+class Q_DECL_EXPORT SessionsModel : public QAbstractListModel
29+ {
30+ Q_OBJECT
31+
32+ Q_ENUMS(SessionModelRoles SessionType)
33+
34+ public:
35+
36+ enum SessionModelRoles {
37+ //name is exposed as Qt::DisplayRole
38+ //comment is exposed as Qt::TooltipRole
39+ KeyRole = Qt::UserRole,
40+ IdRole = KeyRole, /** Deprecated */
41+ TypeRole
42+ };
43+
44+ enum SessionType {
45+ LocalSessions,
46+ RemoteSessions
47+ };
48+
49+ explicit SessionsModel(QObject* parent=0); /** Deprecated. Loads local sessions*/
50+ explicit SessionsModel(SessionsModel::SessionType, QObject* parent=0);
51+ virtual ~SessionsModel();
52+
53+ QHash<int, QByteArray> roleNames() const override;
54+ int rowCount(const QModelIndex& parent) const override;
55+ QVariant data(const QModelIndex& index, int role) const override;
56+
57+ protected:
58+ SessionsModelPrivate* const d_ptr;
59+
60+ private:
61+ QHash<int, QByteArray> m_roleNames;
62+ Q_DECLARE_PRIVATE(SessionsModel)
63+ };
64+}
65+
66+#endif // UNITY_MOCK_SESSIONSMODEL_H
tests/mocks/LightDM/IntegratedLightDM/liblightdm/SessionsModelPrivate.cppnew file+44
@@ -0,0 +1,44 @@
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+#include "SessionsModel.h"
18+#include "SessionsModelPrivate.h"
19+
20+namespace QLightDM
21+{
22+
23+SessionsModelPrivate::SessionsModelPrivate(SessionsModel* parent)
24+ : q_ptr(parent)
25+{
26+ resetEntries();
27+}
28+
29+void SessionsModelPrivate::resetEntries()
30+{
31+ Q_Q(SessionsModel);
32+
33+ q->beginResetModel();
34+ sessionItems =
35+ {
36+ {"", "", "Ubuntu", ""},
37+ {"", "", "GNOME", ""},
38+ {"", "", "Plasma", ""},
39+ {"", "", "Unknown?", ""}
40+ };
41+ q->endResetModel();
42+}
43+
44+} // namespace QLightDM
tests/mocks/LightDM/IntegratedLightDM/liblightdm/SessionsModelPrivate.hnew file+58
@@ -0,0 +1,58 @@
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+#ifndef UNITY_MOCK_SESSIONSMODEL_PRIVATE_H
19+#define UNITY_MOCK_SESSIONSMODEL_PRIVATE_H
20+
21+#include <QtCore/QList>
22+#include <QtCore/QString>
23+
24+namespace QLightDM
25+{
26+class SessionsModel;
27+
28+class SessionItem
29+{
30+public:
31+ QString key; // unused
32+ QString type; // unused
33+ QString name;
34+ QString comment; // unused
35+};
36+
37+class SessionsModelPrivate
38+{
39+public:
40+ explicit SessionsModelPrivate(SessionsModel* parent=0);
41+ virtual ~SessionsModelPrivate() = default;
42+
43+ QList<SessionItem> sessionItems;
44+
45+ void resetEntries();
46+protected:
47+ SessionsModel* const q_ptr;
48+
49+private:
50+ void resetEntries_multipleSessions();
51+ void resetEntries_noSessions();
52+ void resetEntries_singleSession();
53+ Q_DECLARE_PUBLIC(SessionsModel)
54+};
55+
56+} // namespace QLightDM
57+
58+#endif // UNITY_MOCK_SESSIONSMODEL_PRIVATE_H
tests/mocks/LightDM/IntegratedLightDM/liblightdm/UsersModelPrivate.cpp+21−21
@@ -54,7 +54,7 @@ void UsersModelPrivate::resetEntries_single()
5454{
5555 entries =
5656 {
57− { "single", "Single User", 0, 0, false, false, 0, 0 },
57+ { "single", "Single User", 0, 0, false, false, "Ubuntu", 0 },
5858 };
5959}
6060
@@ -62,7 +62,7 @@ void UsersModelPrivate::resetEntries_singlePassphrase()
6262{
6363 entries =
6464 {
65− { "single", "Single User", 0, 0, false, false, 0, 0 },
65+ { "single", "Single User", 0, 0, false, false, "Ubuntu", 0 },
6666 };
6767}
6868
@@ -78,26 +78,26 @@ void UsersModelPrivate::resetEntries_full()
7878{
7979 entries =
8080 {
81− { "has-password", "Has Password", 0, 0, false, false, 0, 0 },
82− { "has-pin", "Has PIN", 0, 0, false, false, 0, 0 },
83− { "different-prompt", "Different Prompt", 0, 0, false, false, 0, 0 },
84− { "no-password", "No Password", 0, 0, false, false, 0, 0 },
85− { "auth-error", "Auth Error", 0, 0, false, false, 0, 0 },
86− { "two-factor", "Two Factor", 0, 0, false, false, 0, 0 },
87− { "info-prompt", "Info Prompt", 0, 0, false, false, 0, 0 },
88− { "html-info-prompt", "HTML Info Prompt", 0, 0, false, false, 0, 0 },
89− { "long-info-prompt", "Long Info Prompt", 0, 0, false, false, 0, 0 },
90− { "wide-info-prompt", "Wide Info Prompt", 0, 0, false, false, 0, 0 },
91− { "multi-info-prompt", "Multi Info Prompt", 0, 0, false, false, 0, 0 },
92− { "long-name", "Long name (far far too long to fit)", 0, 0, false, false, 0, 0 },
93− { "color-background", "Color Background", "#dd4814", 0, false, false, 0, 0 },
81+ { "has-password", "Has Password", 0, 0, false, false, "Ubuntu", 0 },
82+ { "has-pin", "Has PIN", 0, 0, false, false, "Ubuntu", 0 },
83+ { "different-prompt", "Different Prompt", 0, 0, false, false, "Ubuntu", 0 },
84+ { "no-password", "No Password", 0, 0, false, false, "Ubuntu", 0 },
85+ { "auth-error", "Auth Error", 0, 0, false, false, "Ubuntu", 0 },
86+ { "two-factor", "Two Factor", 0, 0, false, false, "Ubuntu", 0 },
87+ { "info-prompt", "Info Prompt", 0, 0, false, false, "Ubuntu", 0 },
88+ { "html-info-prompt", "HTML Info Prompt", 0, 0, false, false, "Ubuntu", 0 },
89+ { "long-info-prompt", "Long Info Prompt", 0, 0, false, false, "Ubuntu", 0 },
90+ { "wide-info-prompt", "Wide Info Prompt", 0, 0, false, false, "Ubuntu", 0 },
91+ { "multi-info-prompt", "Multi Info Prompt", 0, 0, false, false, "Ububntu", 0 },
92+ { "long-name", "Long name (far far too long to fit)", 0, 0, false, false, "Ubuntu", 0 },
93+ { "color-background", "Color Background", "#dd4814", 0, false, false, "Ubuntu", 0 },
9494 // white and black are a bit redundant, but useful for manually testing if UI is still readable
95− { "white-background", "White Background", "#ffffff", 0, false, false, 0, 0 },
96− { "black-background", "Black Background", "#000000", 0, false, false, 0, 0 },
97− { "no-background", "No Background", "", 0, false, false, 0, 0 },
98− { "unicode", "가나다라마", 0, 0, false, false, 0, 0 },
99− { "no-response", "No Response", 0, 0, false, false, 0, 0 },
100− { "empty-name", "", 0, 0, false, false, 0, 0 },
95+ { "white-background", "White Background", "#ffffff", 0, false, false, "Ubuntu", 0 },
96+ { "black-background", "Black Background", "#000000", 0, false, false, "Ubuntu", 0 },
97+ { "no-background", "No Background", "", 0, false, false, "Ubuntu", 0 },
98+ { "unicode", "가나다라마", 0, 0, false, false, "Ubuntu", 0 },
99+ { "no-response", "No Response", 0, 0, false, false, "Ubuntu", 0 },
100+ { "empty-name", "", 0, 0, false, false, "Ubuntu", 0 },
101101 };
102102}
103103
tests/mocks/LightDM/IntegratedLightDM/plugin.cpp+11
@@ -18,7 +18,9 @@
1818#include <DBusGreeter.h>
1919#include <DBusGreeterList.h>
2020#include "MockGreeter.h"
21+#include "MockSessionsModel.h"
2122#include "MockUsersModel.h"
23+#include <QLightDM/SessionsModel>
2224#include "ColorTheme.h"
2325#include "UserMetrics.h"
2426#include <QLightDM/UsersModel>
@@ -39,6 +41,13 @@ static QObject *greeter_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
3941 return greeter;
4042}
4143
44+static QObject *sessions_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
45+{
46+ Q_UNUSED(engine)
47+ Q_UNUSED(scriptEngine)
48+ return new MockSessionsModel;
49+}
50+
4251static QObject *users_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
4352{
4453 Q_UNUSED(engine)
@@ -62,6 +71,8 @@ void LightDMPlugin::registerTypes(const char *uri)
6271 qRegisterMetaType<QLightDM::Greeter::MessageType>("QLightDM::Greeter::MessageType");
6372 qRegisterMetaType<QLightDM::Greeter::PromptType>("QLightDM::Greeter::PromptType");
6473 qmlRegisterSingletonType<MockGreeter>(uri, 0, 1, "Greeter", greeter_provider);
74+ qmlRegisterSingletonType<MockSessionsModel>(uri, 0, 1, "Sessions", sessions_provider);
75+ qmlRegisterUncreatableType<SessionsModel>(uri, 0, 1, "SessionRoles", "Type is not instantiable");
6576 qmlRegisterSingletonType<MockUsersModel>(uri, 0, 1, "Users", users_provider);
6677 qmlRegisterUncreatableType<QLightDM::UsersModel>(uri, 0, 1, "UserRoles", "Type is not instantiable");
6778 qmlRegisterSingletonType<UserMetricsOutput::UserMetrics>(uri, 0, 1, "Infographic", infographic_provider);