Commit 803fce6c authored by Volker Krause's avatar Volker Krause

Feed pkpass data into the reservation manager

parent 94db3365
......@@ -16,6 +16,7 @@
*/
#include <reservationmanager.h>
#include <pkpassmanager.h>
#include <QtTest/qtest.h>
#include <QSignalSpy>
......@@ -32,6 +33,12 @@ private:
}
}
void clearPasses(PkPassManager *mgr)
{
for (const auto id : mgr->passes())
mgr->removePass(id);
}
private slots:
void initTestCase()
{
......@@ -78,6 +85,32 @@ private slots:
QVERIFY(mgr.reservations().isEmpty());
QVERIFY(mgr.reservation(resId).isNull());
}
void testPkPassChanges()
{
PkPassManager passMgr;
clearPasses(&passMgr);
ReservationManager mgr;
mgr.setPkPassManager(&passMgr);
clearReservations(&mgr);
QSignalSpy addSpy(&mgr, &ReservationManager::reservationAdded);
QVERIFY(addSpy.isValid());
QSignalSpy updateSpy(&mgr, &ReservationManager::reservationUpdated);
QVERIFY(updateSpy.isValid());
QVERIFY(mgr.reservations().isEmpty());
const auto passId = QStringLiteral("pass.booking.kde.org/MTIzNA==");
passMgr.importPass(QUrl::fromLocalFile(QLatin1String(SOURCE_DIR "/data/boardingpass-v1.pkpass")));
QCOMPARE(addSpy.size(), 1);
QVERIFY(updateSpy.isEmpty());
passMgr.importPass(QUrl::fromLocalFile(QLatin1String(SOURCE_DIR "/data/boardingpass-v2.pkpass")));
QCOMPARE(addSpy.size(), 1);
QCOMPARE(updateSpy.size(), 1);
}
};
QTEST_GUILESS_MAIN(ReservationManagerTest)
......
......@@ -96,6 +96,7 @@ int main(int argc, char **argv)
PkPassManager passMgr;
ReservationManager resMgr;
resMgr.setPkPassManager(&passMgr);
TimelineModel timelineModel;
timelineModel.setPkPassManager(&passMgr);
timelineModel.setReservationManager(&resMgr);
......
......@@ -16,8 +16,11 @@
*/
#include "reservationmanager.h"
#include "pkpassmanager.h"
#include "logging.h"
#include <KItinerary/ExtractorEngine>
#include <KItinerary/ExtractorPostprocessor>
#include <KItinerary/Flight>
#include <KItinerary/JsonLdDocument>
#include <KItinerary/MergeUtil>
......@@ -42,6 +45,14 @@ ReservationManager::ReservationManager(QObject* parent)
ReservationManager::~ReservationManager() = default;
void ReservationManager::setPkPassManager(PkPassManager* mgr)
{
m_passMgr = mgr;
connect(mgr, &PkPassManager::passAdded, this, &ReservationManager::passAdded);
connect(mgr, &PkPassManager::passUpdated, this, &ReservationManager::passUpdated);
connect(mgr, &PkPassManager::passRemoved, this, &ReservationManager::passRemoved);
}
QVector<QString> ReservationManager::reservations() const
{
const auto basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/reservations");
......@@ -91,9 +102,6 @@ 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.toLocalFile());
if (!f.open(QFile::ReadOnly)) {
qCWarning(Log) << "Unable to open file:" << f.errorString();
......@@ -107,6 +115,14 @@ void ReservationManager::importReservation(const QUrl& filename)
}
const auto resData = JsonLdDocument::fromJson(doc.array());
importReservations(resData);
}
void ReservationManager::importReservations(const QVector<QVariant> &resData)
{
const auto basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/reservations/");
QDir::root().mkpath(basePath);
for (auto res : resData) {
QString resId;
bool oldResFound = false;
......@@ -150,3 +166,31 @@ void ReservationManager::removeReservation(const QString& id)
m_reservations.remove(id);
emit reservationRemoved(id);
}
void ReservationManager::passAdded(const QString& passId)
{
const auto pass = m_passMgr->pass(passId);
const auto extractors = m_extractorRepo.extractorsForPass(pass);
for (const auto &extractor : extractors) {
ExtractorEngine engine;
engine.setExtractor(extractor);
engine.setPass(pass);
const auto data = engine.extract();
const auto res = JsonLdDocument::fromJson(data);
ExtractorPostprocessor postproc;
postproc.process(res);
importReservations(postproc.result());
}
}
void ReservationManager::passUpdated(const QString& passId)
{
passAdded(passId);
}
void ReservationManager::passRemoved(const QString& passId)
{
Q_UNUSED(passId);
// TODO
}
......@@ -18,9 +18,13 @@
#ifndef RESERVATIONMANAGER_H
#define RESERVATIONMANAGER_H
#include <KItinerary/ExtractorRepository>
#include <QHash>
#include <QObject>
class PkPassManager;
class QUrl;
/** Manages JSON-LD reservation data. */
......@@ -31,6 +35,8 @@ public:
ReservationManager(QObject *parent = nullptr);
~ReservationManager();
void setPkPassManager(PkPassManager *mgr);
QVector<QString> reservations() const;
QVariant reservation(const QString &id) const;
......@@ -43,7 +49,15 @@ signals:
void reservationRemoved(const QString &id);
private:
void importReservations(const QVector<QVariant> &resData);
void passAdded(const QString &passId);
void passUpdated(const QString &passId);
void passRemoved(const QString &passId);
mutable QHash<QString, QVariant> m_reservations;
PkPassManager *m_passMgr = nullptr;
KItinerary::ExtractorRepository m_extractorRepo;
};
#endif // RESERVATIONMANAGER_H
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