Commit 94db3365 authored by Volker Krause's avatar Volker Krause

Hook up reservation manager

parent 07304ed5
......@@ -51,7 +51,7 @@ private slots:
QVERIFY(rmSpy.isValid());
QVERIFY(mgr.reservations().isEmpty());
mgr.importReservation(QLatin1String(SOURCE_DIR "/data/4U8465-v1.json"));
mgr.importReservation(QUrl::fromLocalFile(QLatin1String(SOURCE_DIR "/data/4U8465-v1.json")));
auto res = mgr.reservations();
QCOMPARE(res.size(), 1);
......@@ -63,7 +63,7 @@ private slots:
QVERIFY(updateSpy.isEmpty());
QVERIFY(!mgr.reservation(resId).isNull());
mgr.importReservation(QLatin1String(SOURCE_DIR "/data/4U8465-v2.json"));
mgr.importReservation(QUrl::fromLocalFile(QLatin1String(SOURCE_DIR "/data/4U8465-v2.json")));
QCOMPARE(addSpy.size(), 1);
QCOMPARE(updateSpy.size(), 1);
QCOMPARE(mgr.reservations().size(), 1);
......
......@@ -21,6 +21,7 @@
#include "pkpassmanager.h"
#include "timelinemodel.h"
#include "pkpassimageprovider.h"
#include "reservationmanager.h"
#include <KPkPass/Field>
#include <KPkPass/Barcode>
......@@ -94,8 +95,10 @@ int main(int argc, char **argv)
parser.process(app);
PkPassManager passMgr;
ReservationManager resMgr;
TimelineModel timelineModel;
timelineModel.setPkPassManager(&passMgr);
timelineModel.setReservationManager(&resMgr);
qmlRegisterUncreatableType<KPkPass::Barcode>("org.kde.pkpass", 1, 0, "Barcode", {});
qmlRegisterUncreatableType<KPkPass::Field>("org.kde.pkpass", 1, 0, "Field", {});
......@@ -103,6 +106,7 @@ int main(int argc, char **argv)
QQmlApplicationEngine engine;
engine.addImageProvider(QStringLiteral("org.kde.pkpass"), new PkPassImageProvider(&passMgr));
engine.rootContext()->setContextProperty(QStringLiteral("_pkpassManager"), &passMgr);
engine.rootContext()->setContextProperty(QStringLiteral("_reservationManager"), &resMgr);
engine.rootContext()->setContextProperty(QStringLiteral("_timelineModel"), &timelineModel);
engine.load(QStringLiteral(":/main.qml"));
......
......@@ -30,12 +30,16 @@ Kirigami.ApplicationWindow {
height: 720
FileDialog {
property bool loadPass: false
id: fileDialog
title: qsTr("Please choose a file")
folder: shortcuts.home
onAccepted: {
console.log(fileDialog.fileUrls);
_pkpassManager.importPass(fileDialog.fileUrl);
if (loadPass)
_pkpassManager.importPass(fileDialog.fileUrl);
else
_reservationManager.importReservation(fileDialog.fileUrl);
}
}
......@@ -43,10 +47,19 @@ Kirigami.ApplicationWindow {
title: qsTr("KDE Itinerary")
titleIcon: "map-symbolic"
actions: [
Kirigami.Action {
text: qsTr("Import Reservation...")
iconName: "document-open"
onTriggered: {
fileDialog.loadPass = false;
fileDialog.visible = true;
}
},
Kirigami.Action {
text: qsTr("Import Pass...")
iconName: "document-open"
onTriggered: {
fileDialog.loadPass = true;
fileDialog.visible = true;
}
},
......
......@@ -29,6 +29,7 @@
#include <QJsonArray>
#include <QJsonDocument>
#include <QStandardPaths>
#include <QUrl>
#include <QUuid>
#include <QVector>
......@@ -85,12 +86,15 @@ QVariant ReservationManager::reservation(const QString& id) const
return resData.at(0);
}
void ReservationManager::importReservation(const QString& filename)
void ReservationManager::importReservation(const QUrl& filename)
{
if (!filename.isLocalFile())
return;
const auto basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/reservations/");
QDir::root().mkpath(basePath);
QFile f(filename);
QFile f(filename.toLocalFile());
if (!f.open(QFile::ReadOnly)) {
qCWarning(Log) << "Unable to open file:" << f.errorString();
return;
......
......@@ -21,6 +21,8 @@
#include <QHash>
#include <QObject>
class QUrl;
/** Manages JSON-LD reservation data. */
class ReservationManager : public QObject
{
......@@ -32,7 +34,7 @@ public:
QVector<QString> reservations() const;
QVariant reservation(const QString &id) const;
void importReservation(const QString &filename);
Q_INVOKABLE void importReservation(const QUrl &filename);
void removeReservation(const QString &id);
signals:
......
......@@ -34,10 +34,10 @@ TimelineModel::~TimelineModel() = default;
void TimelineModel::setPkPassManager(PkPassManager* mgr)
{
beginResetModel();
m_mgr = mgr;
m_passMgr = mgr;
m_passes = mgr->passes();
std::sort(m_passes.begin(), m_passes.end(), [this](const QString &lhs, const QString &rhs) {
return PkPassManager::relevantDate(m_mgr->pass(lhs)) > PkPassManager::relevantDate(m_mgr->pass(rhs));
return PkPassManager::relevantDate(m_passMgr->pass(lhs)) > PkPassManager::relevantDate(m_passMgr->pass(rhs));
});
connect(mgr, &PkPassManager::passAdded, this, &TimelineModel::passAdded);
connect(mgr, &PkPassManager::passUpdated, this, &TimelineModel::passUpdated);
......@@ -45,25 +45,31 @@ void TimelineModel::setPkPassManager(PkPassManager* mgr)
endResetModel();
}
void TimelineModel::setReservationManager(ReservationManager* mgr)
{
// TODO move the above logic here
m_resMgr = mgr;
}
int TimelineModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid() || !m_mgr)
if (parent.isValid() || !m_passMgr)
return 0;
return m_passes.size();
}
QVariant TimelineModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid() || !m_mgr)
if (!index.isValid() || !m_passMgr)
return {};
switch (role) {
case PassRole:
return QVariant::fromValue(m_mgr->pass(m_passes.at(index.row())));
return QVariant::fromValue(m_passMgr->pass(m_passes.at(index.row())));
case PassIdRole:
return m_passes.at(index.row());
case SectionHeader:
return QLocale().toString(PkPassManager::relevantDate(m_mgr->pass(m_passes.at(index.row()))).date(), QLocale::ShortFormat);
return QLocale().toString(PkPassManager::relevantDate(m_passMgr->pass(m_passes.at(index.row()))).date(), QLocale::ShortFormat);
}
return {};
}
......@@ -80,7 +86,7 @@ QHash<int, QByteArray> TimelineModel::roleNames() const
void TimelineModel::passAdded(const QString &passId)
{
auto it = std::lower_bound(m_passes.begin(), m_passes.end(), passId, [this](const QString &lhs, const QString &rhs) {
return PkPassManager::relevantDate(m_mgr->pass(lhs)) > PkPassManager::relevantDate(m_mgr->pass(rhs));
return PkPassManager::relevantDate(m_passMgr->pass(lhs)) > PkPassManager::relevantDate(m_passMgr->pass(rhs));
});
auto index = std::distance(m_passes.begin(), it);
beginInsertRows({}, index, index);
......@@ -91,7 +97,7 @@ void TimelineModel::passAdded(const QString &passId)
void TimelineModel::passUpdated(const QString &passId)
{
auto it = std::lower_bound(m_passes.begin(), m_passes.end(), passId, [this](const QString &lhs, const QString &rhs) {
return PkPassManager::relevantDate(m_mgr->pass(lhs)) > PkPassManager::relevantDate(m_mgr->pass(rhs));
return PkPassManager::relevantDate(m_passMgr->pass(lhs)) > PkPassManager::relevantDate(m_passMgr->pass(rhs));
});
auto row = std::distance(m_passes.begin(), it);
emit dataChanged(index(row, 0), index(row, 0));
......@@ -100,7 +106,7 @@ void TimelineModel::passUpdated(const QString &passId)
void TimelineModel::passRemoved(const QString &passId)
{
auto it = std::lower_bound(m_passes.begin(), m_passes.end(), passId, [this](const QString &lhs, const QString &rhs) {
return PkPassManager::relevantDate(m_mgr->pass(lhs)) > PkPassManager::relevantDate(m_mgr->pass(rhs));
return PkPassManager::relevantDate(m_passMgr->pass(lhs)) > PkPassManager::relevantDate(m_passMgr->pass(rhs));
});
auto index = std::distance(m_passes.begin(), it);
beginRemoveRows({}, index, index);
......
......@@ -21,6 +21,7 @@
#include <QAbstractListModel>
class PkPassManager;
class ReservationManager;
class TimelineModel : public QAbstractListModel
{
......@@ -35,6 +36,7 @@ public:
~TimelineModel();
void setPkPassManager(PkPassManager *mgr);
void setReservationManager(ReservationManager *mgr);
QVariant data(const QModelIndex& index, int role) const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
......@@ -45,7 +47,8 @@ private:
void passUpdated(const QString &passId);
void passRemoved(const QString &passId);
PkPassManager *m_mgr = nullptr;
PkPassManager *m_passMgr = nullptr;
ReservationManager *m_resMgr = nullptr;
QVector<QString> m_passes;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment