summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--wayland/democompositor/apps/appentry.cpp2
-rw-r--r--wayland/democompositor/apps/appentry.h12
-rw-r--r--wayland/democompositor/apps/applistmodel.cpp136
-rw-r--r--wayland/democompositor/apps/applistmodel.h88
-rw-r--r--wayland/democompositor/apps/appparser.cpp6
-rw-r--r--wayland/democompositor/apps/appparser.h2
-rw-r--r--wayland/democompositor/apps/apps.pri2
-rw-r--r--wayland/democompositor/tests/applist/applist.pro17
-rw-r--r--wayland/democompositor/tests/applist/applist.qrc6
-rw-r--r--wayland/democompositor/tests/applist/tst_applistmodel.cpp108
-rw-r--r--wayland/democompositor/tests/apps/apps.pro3
-rw-r--r--wayland/democompositor/tests/apps/tst_appparser.cpp4
12 files changed, 378 insertions, 8 deletions
diff --git a/wayland/democompositor/apps/appentry.cpp b/wayland/democompositor/apps/appentry.cpp
index 6cd813a..aaae167 100644
--- a/wayland/democompositor/apps/appentry.cpp
+++ b/wayland/democompositor/apps/appentry.cpp
@@ -53,5 +53,5 @@
AppEntry AppEntry::empty()
{
QString empty;
- return AppEntry{empty, empty, empty, empty};
+ return AppEntry{empty, empty, empty, empty, empty};
}
diff --git a/wayland/democompositor/apps/appentry.h b/wayland/democompositor/apps/appentry.h
index 6de410c..cb7a727 100644
--- a/wayland/democompositor/apps/appentry.h
+++ b/wayland/democompositor/apps/appentry.h
@@ -50,17 +50,27 @@
#pragma once
+#include <QtCore/QObject>
#include <QtCore/QString>
/**
* A const representatation of an application entry. Members include
* executable name, path, icon and other information in the future.
*/
-struct AppEntry {
+class AppEntry {
+ Q_GADGET
+ Q_PROPERTY(QString iconName MEMBER iconName CONSTANT)
+ Q_PROPERTY(QString appName MEMBER appName CONSTANT)
+ Q_PROPERTY(QString executableName MEMBER executableName CONSTANT)
+ Q_PROPERTY(QString executablePath MEMBER executablePath CONSTANT)
+public:
const QString iconName;
const QString appName;
const QString executableName;
const QString executablePath;
+ const QString sourceFileName;
static AppEntry empty();
};
+
+Q_DECLARE_METATYPE(AppEntry)
diff --git a/wayland/democompositor/apps/applistmodel.cpp b/wayland/democompositor/apps/applistmodel.cpp
new file mode 100644
index 0000000..542f8a7
--- /dev/null
+++ b/wayland/democompositor/apps/applistmodel.cpp
@@ -0,0 +1,136 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of Qt for Device Creation.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "applistmodel.h"
+#include "appparser.h"
+#include "applog.h"
+
+static QHash<int, QByteArray> modelRoles()
+{
+ QHash<int, QByteArray> roles;
+ roles[AppListModel::App] = "appEntry";
+ roles[AppListModel::IconName] = "iconName";
+ roles[AppListModel::ApplicationName] = "applicationName";
+ roles[AppListModel::ExeuctableName] = "executableName";
+ roles[AppListModel::ExecutablePath] = "executablePath";
+ roles[AppListModel::SourceFileName] = "sourceFileName";
+ return roles;
+}
+
+QHash<int, QByteArray> AppListModel::m_roles = modelRoles();
+
+AppListModel::~AppListModel()
+{
+ qDeleteAll(m_rows);
+}
+
+int AppListModel::rowCount(const QModelIndex& index) const
+{
+ if (index.isValid())
+ return 0;
+ return m_rows.count();
+}
+
+QVariant AppListModel::data(const QModelIndex& index, int role) const
+{
+ if (!index.isValid() || index.parent().isValid())
+ return QVariant();
+
+ auto entry = m_rows[index.row()];
+
+ switch (role) {
+ case App:
+ return QVariant::fromValue(*entry);
+ case IconName:
+ return entry->iconName;
+ case ApplicationName:
+ return entry->appName;
+ case ExeuctableName:
+ return entry->executableName;
+ case ExecutablePath:
+ return entry->executablePath;
+ case SourceFileName:
+ return entry->sourceFileName;
+ default:
+ qCWarning(apps) << "Unhandled role" << role;
+ return QVariant();
+ }
+}
+
+QHash<int, QByteArray> AppListModel::roleNames() const
+{
+ return m_roles;
+}
+
+void AppListModel::addFile(const QString& fileName)
+{
+ beginResetModel();
+ doAddFile(fileName);
+ endResetModel();
+}
+
+void AppListModel::doAddFile(const QString& fileName)
+{
+ bool ok;
+ auto newEntry = AppParser::parseFile(fileName, &ok);
+ if (!ok)
+ return;
+
+ for (int i = 0; i < m_rows.count(); ++i) {
+ auto oldEntry = m_rows[i];
+ if (oldEntry->sourceFileName == fileName) {
+ m_rows[i] = new AppEntry(newEntry);
+ delete oldEntry;
+ return;
+ }
+ }
+
+ m_rows.push_back(new AppEntry(newEntry));
+}
diff --git a/wayland/democompositor/apps/applistmodel.h b/wayland/democompositor/apps/applistmodel.h
new file mode 100644
index 0000000..f8fc3e0
--- /dev/null
+++ b/wayland/democompositor/apps/applistmodel.h
@@ -0,0 +1,88 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of Qt for Device Creation.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#pragma once
+
+#include <QtCore/QAbstractListModel>
+#include <QtCore/QVector>
+
+class AppEntry;
+
+/**
+ * A model that holds all available applications and
+ * exports them to a QML scene
+ */
+class AppListModel : public QAbstractListModel {
+ Q_OBJECT
+public:
+
+ ~AppListModel();
+
+ enum Roles {
+ App = Qt::UserRole,
+ IconName,
+ ApplicationName,
+ ExeuctableName,
+ ExecutablePath,
+ SourceFileName,
+ };
+
+ int rowCount(const QModelIndex& parent) const Q_DECL_OVERRIDE;
+ QVariant data(const QModelIndex& index, int role) const Q_DECL_OVERRIDE;
+ QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;
+
+ void addFile(const QString& fileName);
+
+private:
+ void doAddFile(const QString& fileName);
+
+ QVector<AppEntry*> m_rows;
+ static QHash<int, QByteArray> m_roles;
+};
diff --git a/wayland/democompositor/apps/appparser.cpp b/wayland/democompositor/apps/appparser.cpp
index 18d2d22..2c373cc 100644
--- a/wayland/democompositor/apps/appparser.cpp
+++ b/wayland/democompositor/apps/appparser.cpp
@@ -94,7 +94,7 @@ static int readInt(const QJsonObject& object, const QString& key, bool *ok)
return doReadInt(object.value(key), ok);
}
-AppEntry AppParser::parseData(const QByteArray& content, bool *ok)
+AppEntry AppParser::parseData(const QByteArray& content, const QString& fileName, bool *ok)
{
*ok = true;
QJsonParseError error;
@@ -132,7 +132,7 @@ AppEntry AppParser::parseData(const QByteArray& content, bool *ok)
if (!*ok)
return AppEntry::empty();
- return AppEntry{iconName, appName, executableName, executablePath};
+ return AppEntry{iconName, appName, executableName, executablePath, fileName};
}
AppEntry AppParser::parseFile(const QString& fileName, bool *ok)
@@ -146,7 +146,7 @@ AppEntry AppParser::parseFile(const QString& fileName, bool *ok)
return AppEntry::empty();
}
- auto entry = parseData(file.readAll(), ok);
+ auto entry = parseData(file.readAll(), fileName, ok);
file.close();
if (!*ok) {
qCWarning(apps) << "Failed to parse" << fileName;
diff --git a/wayland/democompositor/apps/appparser.h b/wayland/democompositor/apps/appparser.h
index da52c5a..a6711b4 100644
--- a/wayland/democompositor/apps/appparser.h
+++ b/wayland/democompositor/apps/appparser.h
@@ -57,6 +57,6 @@
*/
class AppParser {
public:
- static AppEntry parseData(const QByteArray& content, bool *ok);
+ static AppEntry parseData(const QByteArray& content, const QString& fileName, bool *ok);
static AppEntry parseFile(const QString& fileName, bool *ok);
};
diff --git a/wayland/democompositor/apps/apps.pri b/wayland/democompositor/apps/apps.pri
index dfcee61..9b19060 100644
--- a/wayland/democompositor/apps/apps.pri
+++ b/wayland/democompositor/apps/apps.pri
@@ -2,8 +2,10 @@
HEADERS += \
apps/appentry.h \
apps/applog.h \
+ apps/applistmodel.h \
apps/appparser.h
SOURCES += \
apps/appentry.cpp \
+ apps/applistmodel.cpp \
apps/appparser.cpp
diff --git a/wayland/democompositor/tests/applist/applist.pro b/wayland/democompositor/tests/applist/applist.pro
new file mode 100644
index 0000000..e253b66
--- /dev/null
+++ b/wayland/democompositor/tests/applist/applist.pro
@@ -0,0 +1,17 @@
+TARGET = tst_applistmodel
+QT = testlib
+CONFIG += testcase
+
+INCLUDEPATH += ../../apps
+
+HEADERS += \
+ ../../apps/appentry.h \
+ ../../apps/applistmodel.h
+
+SOURCES += \
+ ../../apps/appentry.cpp \
+ ../../apps/appparser.cpp \
+ ../../apps/applistmodel.cpp
+
+SOURCES += tst_applistmodel.cpp
+RESOURCES += applist.qrc
diff --git a/wayland/democompositor/tests/applist/applist.qrc b/wayland/democompositor/tests/applist/applist.qrc
new file mode 100644
index 0000000..04db642
--- /dev/null
+++ b/wayland/democompositor/tests/applist/applist.qrc
@@ -0,0 +1,6 @@
+<!DOCTYPE RCC><RCC version="1.0">
+<qresource prefix="/">
+ <file alias="app1.json">../../apps/files/clock.app</file>
+ <file alias="app2.json">../../apps/files/maps.app</file>
+</qresource>
+</RCC>
diff --git a/wayland/democompositor/tests/applist/tst_applistmodel.cpp b/wayland/democompositor/tests/applist/tst_applistmodel.cpp
new file mode 100644
index 0000000..921defc
--- /dev/null
+++ b/wayland/democompositor/tests/applist/tst_applistmodel.cpp
@@ -0,0 +1,108 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of Qt for Device Creation.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "applistmodel.h"
+#include "appentry.h"
+
+#include <QtCore/QVariant>
+
+#include <QtTest>
+
+class tst_AppListModel : public QObject {
+ Q_OBJECT
+
+private Q_SLOTS:
+ void testNoDuplicates();
+ void testRoles();
+};
+
+void tst_AppListModel::testNoDuplicates()
+{
+ AppListModel model;
+ QCOMPARE(model.rowCount(QModelIndex()), 0);
+ model.addFile(":/app1.json");
+ QCOMPARE(model.rowCount(QModelIndex()), 1);
+ model.addFile(":/app2.json");
+ QCOMPARE(model.rowCount(QModelIndex()), 2);
+ model.addFile(":/app1.json");
+ QCOMPARE(model.rowCount(QModelIndex()), 2);
+ model.addFile(":/app2.json");
+}
+
+void tst_AppListModel::testRoles()
+{
+ AppListModel model;
+ model.addFile(":/app1.json");
+
+ auto idx = model.index(1, 0);
+ QVERIFY(!idx.isValid());
+ idx = model.index(0, 0);
+ QVERIFY(idx.isValid());
+
+ /* Check we get a full AppEntry back */
+ auto var = idx.data(AppListModel::App);
+ QVERIFY(var.canConvert<AppEntry>());
+ auto app = var.value<AppEntry>();
+ QCOMPARE(app.sourceFileName, QStringLiteral(":/app1.json"));
+
+ var = idx.data(AppListModel::IconName);
+ QCOMPARE(var.toString(), QStringLiteral("qrc:/images/Icon_Clocks.png"));
+ var = idx.data(AppListModel::ApplicationName);
+ QCOMPARE(var.toString(), QStringLiteral("Clocks"));
+ var = idx.data(AppListModel::ExeuctableName);
+ QCOMPARE(var.toString(), QStringLiteral("clocks"));
+ var = idx.data(AppListModel::ExecutablePath);
+ QCOMPARE(var.toString(), QStringLiteral("./"));
+ var = idx.data(AppListModel::SourceFileName);
+ QCOMPARE(var.toString(), QStringLiteral(":/app1.json"));
+}
+
+QTEST_MAIN(tst_AppListModel)
+#include "tst_applistmodel.moc"
diff --git a/wayland/democompositor/tests/apps/apps.pro b/wayland/democompositor/tests/apps/apps.pro
index 710e077..21a6cfe 100644
--- a/wayland/democompositor/tests/apps/apps.pro
+++ b/wayland/democompositor/tests/apps/apps.pro
@@ -4,6 +4,9 @@ CONFIG += testcase
INCLUDEPATH += ../../apps
+HEADERS += \
+ ../../apps/appentry.h
+
SOURCES += \
../../apps/appentry.cpp \
../../apps/appparser.cpp
diff --git a/wayland/democompositor/tests/apps/tst_appparser.cpp b/wayland/democompositor/tests/apps/tst_appparser.cpp
index 48b6b4d..6d63fd9 100644
--- a/wayland/democompositor/tests/apps/tst_appparser.cpp
+++ b/wayland/democompositor/tests/apps/tst_appparser.cpp
@@ -91,7 +91,7 @@ void tst_AppParser::testValidVersion1()
QFETCH(QString, path);
bool ok = false;
- auto entry = AppParser::parseData(input, &ok);
+ auto entry = AppParser::parseData(input, QStringLiteral("dummy"), &ok);
QVERIFY(ok);
}
@@ -109,7 +109,7 @@ void tst_AppParser::testInvalid()
QFETCH(QByteArray, input);
bool ok = true;
- AppParser::parseData(input, &ok);
+ AppParser::parseData(input, QStringLiteral("dummy"), &ok);
QVERIFY(!ok);
}