Commit 62fb41b1 authored by Volker Krause's avatar Volker Krause
Browse files

Refactor stopover to reservation matching

Less duplicated code, and less code that is hardcoded to train
reservations.
parent e34c7db8
......@@ -22,6 +22,7 @@ set(itinerary_srcs
pkpassmanager.cpp
pkpassimageprovider.cpp
publictransport.cpp
reservationhelper.cpp
reservationmanager.cpp
statisticsmodel.cpp
statisticstimerangemodel.cpp
......
......@@ -20,6 +20,7 @@
#include "livedatamanager.h"
#include "logging.h"
#include "pkpassmanager.h"
#include "reservationhelper.h"
#include "reservationmanager.h"
#include "publictransport.h"
......@@ -30,10 +31,10 @@
#include <KItinerary/SortUtil>
#include <KItinerary/TrainTrip>
#include <KPublicTransport/DepartureReply>
#include <KPublicTransport/DepartureRequest>
#include <KPublicTransport/Location>
#include <KPublicTransport/Manager>
#include <KPublicTransport/StopoverReply>
#include <KPublicTransport/StopoverRequest>
#ifdef HAVE_NOTIFICATIONS
#include <KNotifications/KNotification>
......@@ -113,12 +114,12 @@ void LiveDataManager::setPollingEnabled(bool pollingEnabled)
}
}
KPublicTransport::Departure LiveDataManager::arrival(const QString &resId)
KPublicTransport::Stopover LiveDataManager::arrival(const QString &resId)
{
return m_arrivals.value(resId).change;
}
KPublicTransport::Departure LiveDataManager::departure(const QString &resId)
KPublicTransport::Stopover LiveDataManager::departure(const QString &resId)
{
return m_departures.value(resId).change;
}
......@@ -136,46 +137,31 @@ static bool isSameLine(const KPublicTransport::Line &lhs, const QString &trainNa
return KPublicTransport::Line::isSame(lhs, rhs);
}
static bool isDepartureForReservation(const QVariant &res, const KPublicTransport::Departure &dep)
static bool isDepartureForReservation(const QVariant &res, const KPublicTransport::Stopover &dep)
{
if (JsonLd::isA<TrainReservation>(res)) {
const auto trip = res.value<TrainReservation>().reservationFor().value<TrainTrip>();
return dep.scheduledDepartureTime() == trip.departureTime() && isSameLine(dep.route().line(), trip.trainName(), trip.trainNumber());
}
if (JsonLd::isA<BusReservation>(res)) {
const auto trip = res.value<BusReservation>().reservationFor().value<BusTrip>();
return dep.scheduledDepartureTime() == trip.departureTime() && isSameLine(dep.route().line(), trip.busName(), trip.busNumber());
}
return false;
const auto lineData = ReservationHelper::lineNameAndNumber(res);
return PublicTransport::isSameMode(res, dep.route().line().mode())
&& SortUtil::startDateTime(res) == dep.scheduledDepartureTime()
&& isSameLine(dep.route().line(), lineData.first, lineData.second);
}
static bool isArrivalForReservation(const QVariant &res, const KPublicTransport::Departure &dep)
static bool isArrivalForReservation(const QVariant &res, const KPublicTransport::Stopover &arr)
{
if (JsonLd::isA<TrainReservation>(res)) {
const auto trip = res.value<TrainReservation>().reservationFor().value<TrainTrip>();
return dep.scheduledArrivalTime() == trip.arrivalTime() && isSameLine(dep.route().line(), trip.trainName(), trip.trainNumber());
}
if (JsonLd::isA<BusReservation>(res)) {
const auto trip = res.value<BusReservation>().reservationFor().value<BusTrip>();
return dep.scheduledArrivalTime() == trip.arrivalTime() && isSameLine(dep.route().line(), trip.busName(), trip.busNumber());
}
return false;
const auto lineData = ReservationHelper::lineNameAndNumber(res);
return PublicTransport::isSameMode(res, arr.route().line().mode())
&& SortUtil::endDateTime(res) == arr.scheduledArrivalTime()
&& isSameLine(arr.route().line(), lineData.first, lineData.second);
}
void LiveDataManager::checkTrainTrip(const QVariant &res, const QString& resId)
void LiveDataManager::checkReservation(const QVariant &res, const QString& resId)
{
Q_ASSERT(JsonLd::isA<TrainReservation>(res));
const auto trip = res.value<TrainReservation>().reservationFor().value<TrainTrip>();
qCDebug(Log) << trip.trainName() << trip.trainNumber() << trip.departureTime();
using namespace KPublicTransport;
if (!hasDeparted(resId, res)) {
DepartureRequest req(PublicTransport::locationFromPlace(trip.departureStation(), res));
req.setDateTime(trip.departureTime());
auto reply = m_ptMgr->queryDeparture(req);
StopoverRequest req(PublicTransport::locationFromPlace(LocationUtil::departureLocation(res), res));
req.setMode(StopoverRequest::QueryDeparture);
req.setDateTime(SortUtil::startDateTime(res));
auto reply = m_ptMgr->queryStopover(req);
connect(reply, &Reply::finished, this, [this, res, resId, reply]() {
reply->deleteLater();
if (reply->error() != Reply::NoError) {
......@@ -196,10 +182,10 @@ void LiveDataManager::checkTrainTrip(const QVariant &res, const QString& resId)
}
if (!hasArrived(resId, res)) {
DepartureRequest req(PublicTransport::locationFromPlace(trip.arrivalStation(), res));
req.setMode(DepartureRequest::QueryArrival);
req.setDateTime(trip.arrivalTime());
auto reply = m_ptMgr->queryDeparture(req);
StopoverRequest req(PublicTransport::locationFromPlace(LocationUtil::arrivalLocation(res), res));
req.setMode(StopoverRequest::QueryArrival);
req.setDateTime(SortUtil::endDateTime(res));
auto reply = m_ptMgr->queryStopover(req);
connect(reply, &Reply::finished, this, [this, res, resId, reply]() {
reply->deleteLater();
if (reply->error() != Reply::NoError) {
......@@ -508,7 +494,7 @@ void LiveDataManager::pollForUpdates(bool force)
}
if (JsonLd::isA<TrainReservation>(res)) {
checkTrainTrip(res, batchId);
checkReservation(res, batchId);
}
// check for pkpass updates, for each element in this batch
......
......@@ -54,8 +54,8 @@ public:
void setPollingEnabled(bool pollingEnabled);
KPublicTransport::Departure arrival(const QString &resId);
KPublicTransport::Departure departure(const QString &resId);
KPublicTransport::Stopover arrival(const QString &resId);
KPublicTransport::Stopover departure(const QString &resId);
public Q_SLOTS:
/** Checks all applicable elements for updates. */
......@@ -73,7 +73,7 @@ private:
void batchRenamed(const QString &oldBatchId, const QString &newBatchId);
void batchRemoved(const QString &resId);
void checkTrainTrip(const QVariant &res, const QString &resId);
void checkReservation(const QVariant &res, const QString &resId);
void updateArrivalData(const KPublicTransport::Departure &arr, const QString &resId);
void updateDepartureData(const KPublicTransport::Departure &dep, const QString &resId);
......
......@@ -234,14 +234,16 @@ QVariant PublicTransport::applyJourneySection(const QVariant &res, const KPublic
return res;
}
bool PublicTransport::isSameMode(const QVariant &res, const KPublicTransport::JourneySection &section)
bool PublicTransport::isSameMode(const QVariant &res, KPublicTransport::Line::Mode mode)
{
using namespace KPublicTransport;
if (KItinerary::JsonLd::isA<KItinerary::TrainReservation>(res)) {
return isTrainMode(section.route().line().mode());
return isTrainMode(mode);
} else if (KItinerary::JsonLd::isA<KItinerary::BusReservation>(res)) {
return isBusMode(section.route().line().mode());
return isBusMode(mode);
} else if (KItinerary::JsonLd::isA<KItinerary::FlightReservation>(res)) {
return mode == Line::Air;
} else if (res.isValid()) {
qCWarning(Log) << "unexpected reservation type!?" << res;
}
......@@ -249,6 +251,11 @@ bool PublicTransport::isSameMode(const QVariant &res, const KPublicTransport::Jo
return false;
}
bool PublicTransport::isSameMode(const QVariant &res, const KPublicTransport::JourneySection &section)
{
return isSameMode(res, section.route().line().mode());
}
KPublicTransport::StopoverRequest PublicTransport::stopoverRequestForPlace(const QVariant &place, const QDateTime &dt) const
{
KPublicTransport::StopoverRequest req;
......
......@@ -25,6 +25,7 @@
#include <KItinerary/Place>
#include <KPublicTransport/Location>
#include <KPublicTransport/Line>
namespace KPublicTransport {
class JourneySection;
......@@ -72,6 +73,7 @@ public:
static QVariant applyJourneySection(const QVariant &res, const KPublicTransport::JourneySection &section);
/** Checks if the given reservation and journey section have a compatible mode of transportation. */
static bool isSameMode(const QVariant &res, KPublicTransport::Line::Mode mode);
static bool isSameMode(const QVariant &res, const KPublicTransport::JourneySection &section);
/** Provide an icon source for usage in Kirigami.Icon that represents the line mode.
......
/*
Copyright (C) 2020 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "reservationhelper.h"
#include <KItinerary/BusTrip>
#include <KItinerary/Flight>
#include <KItinerary/Reservation>
#include <KItinerary/TrainTrip>
using namespace KItinerary;
std::pair<QString, QString> ReservationHelper::lineNameAndNumber(const QVariant &res)
{
if (JsonLd::isA<TrainReservation>(res)) {
const auto trip = res.value<TrainReservation>().reservationFor().value<TrainTrip>();
return std::make_pair(trip.trainName(), trip.trainNumber());
}
if (JsonLd::isA<BusReservation>(res)) {
const auto trip = res.value<BusReservation>().reservationFor().value<BusTrip>();
return std::make_pair(trip.busName(), trip.busNumber());
}
if (JsonLd::isA<FlightReservation>(res)) {
const auto flight = res.value<FlightReservation>().reservationFor().value<Flight>();
return std::make_pair(flight.airline().iataCode(), flight.flightNumber());
}
return {};
}
/*
Copyright (C) 2020 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef RESERVATIONHELPER_H
#define RESERVATIONHELPER_H
#include <utility>
class QString;
class QVariant;
/** Helper methods for dealing with KItinerary reservation elements. */
namespace ReservationHelper
{
std::pair<QString, QString> lineNameAndNumber(const QVariant &res);
}
#endif // RESERVATIONHELPER_H
Supports Markdown
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