diff --git a/src/publictransport/departurereply.cpp b/src/publictransport/departurereply.cpp index 1d59fa4..6f65755 100644 --- a/src/publictransport/departurereply.cpp +++ b/src/publictransport/departurereply.cpp @@ -1,71 +1,80 @@ /* Copyright (C) 2018 Volker Krause 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 . */ #include "departurereply.h" #include "reply_p.h" #include "departurerequest.h" #include "logging.h" #include "backends/navitiaclient.h" #include "backends/navitiaparser.h" #include #include #include using namespace KPublicTransport; namespace KPublicTransport { class DepartureReplyPrivate : public ReplyPrivate { public: + DepartureRequest request; std::vector departures; }; } DepartureReply::DepartureReply(const DepartureRequest &req, QNetworkAccessManager *nam) : Reply(new DepartureReplyPrivate) { + Q_D(DepartureReply); + d->request = req; auto reply = NavitiaClient::queryDeparture(req, nam); connect(reply, &QNetworkReply::finished, [reply, this] { Q_D(DepartureReply); switch (reply->error()) { case QNetworkReply::NoError: d->departures = NavitiaParser::parseDepartures(reply->readAll()); break; case QNetworkReply::ContentNotFoundError: d->error = NotFoundError; d->errorMsg = NavitiaParser::parseErrorMessage(reply->readAll()); break; default: d->error = NetworkError; d->errorMsg = reply->errorString(); qCDebug(Log) << reply->error() << reply->errorString(); } emit finished(); deleteLater(); }); } DepartureReply::~DepartureReply() = default; +DepartureRequest DepartureReply::request() const +{ + Q_D(const DepartureReply); + return d->request; +} + std::vector DepartureReply::departures() const { Q_D(const DepartureReply); return d->departures; // TODO this copies } diff --git a/src/publictransport/departurereply.h b/src/publictransport/departurereply.h index 824f9e6..e5cb457 100644 --- a/src/publictransport/departurereply.h +++ b/src/publictransport/departurereply.h @@ -1,51 +1,54 @@ /* Copyright (C) 2018 Volker Krause 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 . */ #ifndef KPUBLICTRANSPORT_DEPARTUREREPLY_H #define KPUBLICTRANSPORT_DEPARTUREREPLY_H #include "reply.h" #include class QNetworkAccessManager; namespace KPublicTransport { class Departure; class DepartureRequest; class DepartureReplyPrivate; /** Departure query reply. */ class DepartureReply : public Reply { Q_OBJECT public: ~DepartureReply(); + /** The request this is the reply for. */ + DepartureRequest request() const; + /** Returns the found departure information. */ std::vector departures() const; private: friend class Manager; explicit DepartureReply(const DepartureRequest &req, QNetworkAccessManager *nam); Q_DECLARE_PRIVATE(DepartureReply) }; } #endif // KPUBLICTRANSPORT_DEPARTUREREPLY_H diff --git a/src/publictransport/journeyreply.cpp b/src/publictransport/journeyreply.cpp index 9df9cae..37c55b1 100644 --- a/src/publictransport/journeyreply.cpp +++ b/src/publictransport/journeyreply.cpp @@ -1,106 +1,115 @@ /* Copyright (C) 2018 Volker Krause 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 . */ #include "journeyreply.h" #include "reply_p.h" #include "journeyrequest.h" #include "logging.h" #include "backends/navitiaclient.h" #include "backends/navitiaparser.h" #include #include #include #include #include using namespace KPublicTransport; namespace KPublicTransport { class JourneyReplyPrivate : public ReplyPrivate { public: void postProcessJourneys(); + JourneyRequest request; std::vector journeys; }; } void JourneyReplyPrivate::postProcessJourneys() { // try to fill gaps in timezone data for (auto &journey : journeys) { auto sections = journey.takeSections(); for (auto §ion : sections) { if (section.mode() == JourneySection::Walking) { if (!section.from().timeZone().isValid() && section.to().timeZone().isValid()) { auto from = section.from(); from.setTimeZone(section.to().timeZone()); section.setFrom(from); auto dt = section.departureTime(); dt.setTimeZone(from.timeZone()); section.setDepartureTime(dt); } if (section.from().timeZone().isValid() && !section.to().timeZone().isValid()) { auto to = section.to(); to.setTimeZone(section.from().timeZone()); section.setTo(to); auto dt = section.arrivalTime(); dt.setTimeZone(to.timeZone()); section.setArrivalTime(dt); } } } journey.setSections(std::move(sections)); } } JourneyReply::JourneyReply(const JourneyRequest &req, QNetworkAccessManager *nam) : Reply(new JourneyReplyPrivate) { + Q_D(JourneyReply); + d->request = req; auto reply = NavitiaClient::findJourney(req, nam); connect(reply, &QNetworkReply::finished, [reply, this] { Q_D(JourneyReply); switch (reply->error()) { case QNetworkReply::NoError: d->journeys = NavitiaParser::parseJourneys(reply->readAll()); d->postProcessJourneys(); break; case QNetworkReply::ContentNotFoundError: d->error = NotFoundError; d->errorMsg = NavitiaParser::parseErrorMessage(reply->readAll()); break; default: d->error = NetworkError; d->errorMsg = reply->errorString(); qCDebug(Log) << reply->error() << reply->errorString(); } emit finished(); deleteLater(); }); } JourneyReply::~JourneyReply() = default; +JourneyRequest JourneyReply::request() const +{ + Q_D(const JourneyReply); + return d->request; +} + std::vector JourneyReply::journeys() const { Q_D(const JourneyReply); // TODO avoid the copy here return d->journeys; } diff --git a/src/publictransport/journeyreply.h b/src/publictransport/journeyreply.h index 40c4f6e39..3d64b6a 100644 --- a/src/publictransport/journeyreply.h +++ b/src/publictransport/journeyreply.h @@ -1,51 +1,54 @@ /* Copyright (C) 2018 Volker Krause 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 . */ #ifndef KPUBLICTRANSPORT_JOURNEYREPLY_H #define KPUBLICTRANSPORT_JOURNEYREPLY_H #include "reply.h" #include class QNetworkAccessManager; namespace KPublicTransport { class Journey; class JourneyReplyPrivate; class JourneyRequest; /** Journey query response. */ class JourneyReply : public Reply { Q_OBJECT public: ~JourneyReply(); + /** The request this is the reply for. */ + JourneyRequest request() const; + /** Returns the found journeys. */ std::vector journeys() const; private: friend class Manager; explicit JourneyReply(const JourneyRequest &req, QNetworkAccessManager *nam); Q_DECLARE_PRIVATE(JourneyReply) }; } #endif // KPUBLICTRANSPORT_JOURNEYREPLY_H