diff --git a/src/publictransport/CMakeLists.txt b/src/publictransport/CMakeLists.txt index c61eb4f..c0337fa 100644 --- a/src/publictransport/CMakeLists.txt +++ b/src/publictransport/CMakeLists.txt @@ -1,57 +1,59 @@ set(kpublictransport_srcs departurereply.cpp departurerequest.cpp journeyreply.cpp journeyrequest.cpp manager.cpp + reply.cpp backends/navitiaclient.cpp backends/navitiaparser.cpp datatypes/departure.cpp datatypes/journey.cpp datatypes/line.cpp datatypes/location.cpp ) ecm_qt_declare_logging_category(kpublictransport_srcs HEADER logging.h IDENTIFIER KPublicTransport::Log CATEGORY_NAME org.kde.kpublictransport) add_library(KPublicTransport STATIC ${kpublictransport_srcs}) target_include_directories(KPublicTransport PUBLIC "$") target_link_libraries(KPublicTransport PUBLIC Qt5::Gui PRIVATE Qt5::Network ) ecm_generate_headers(KPublicTransport_FORWARDING_HEADERS HEADER_NAMES DepartureReply DepartureRequest JourneyReply JourneyRequest Manager + Reply PREFIX KPublicTransport REQUIRED_HEADERS KPublicTransport_HEADERS ) # # ### for testing only ecm_generate_headers(KPublicTransport_Backends_FORWARDING_HEADERS HEADER_NAMES NavitiaClient NavitiaParser PREFIX KPublicTransport REQUIRED_HEADERS KPublicTransport_Backends_HEADERS RELATIVE backends ) ecm_generate_headers(KPublicTransport_Datatypes_FORWARDING_HEADERS HEADER_NAMES Datatypes Departure Journey Line Location PREFIX KPublicTransport REQUIRED_HEADERS KPublicTransport_Datatypes_HEADERS RELATIVE datatypes ) diff --git a/src/publictransport/departurereply.cpp b/src/publictransport/departurereply.cpp index 80829ce..1d59fa4 100644 --- a/src/publictransport/departurereply.cpp +++ b/src/publictransport/departurereply.cpp @@ -1,80 +1,71 @@ /* 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 { +class DepartureReplyPrivate : public ReplyPrivate { public: std::vector departures; - QString errorMsg; - DepartureReply::Error error = DepartureReply::NoError; }; } DepartureReply::DepartureReply(const DepartureRequest &req, QNetworkAccessManager *nam) - : d(new DepartureReplyPrivate) + : Reply(new DepartureReplyPrivate) { 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; std::vector DepartureReply::departures() const { + Q_D(const DepartureReply); return d->departures; // TODO this copies } - -DepartureReply::Error DepartureReply::error() const -{ - return d->error; -} - -QString DepartureReply::errorString() const -{ - return d->errorMsg; -} diff --git a/src/publictransport/departurereply.h b/src/publictransport/departurereply.h index 66a0db7..824f9e6 100644 --- a/src/publictransport/departurereply.h +++ b/src/publictransport/departurereply.h @@ -1,68 +1,51 @@ /* 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 +#include "reply.h" -#include #include class QNetworkAccessManager; namespace KPublicTransport { class Departure; class DepartureRequest; class DepartureReplyPrivate; /** Departure query reply. */ -class DepartureReply : public QObject +class DepartureReply : public Reply { Q_OBJECT public: ~DepartureReply(); /** Returns the found departure information. */ std::vector departures() const; - /** Error types. */ - enum Error { - NoError, ///< Nothing went wrong. - NetworkError, ///< Error during network operations. - NotFoundError, ///< The requested journey could not be found. - UnknownError ///< Anything else. - }; - /** Error code. */ - Error error() const; - /** Textual error message. */ - QString errorString() const; - -Q_SIGNALS: - /** Emitted whenever the journey search has been completed. */ - void finished(); - private: friend class Manager; explicit DepartureReply(const DepartureRequest &req, QNetworkAccessManager *nam); - std::unique_ptr d; + Q_DECLARE_PRIVATE(DepartureReply) }; } #endif // KPUBLICTRANSPORT_DEPARTUREREPLY_H diff --git a/src/publictransport/journeyreply.cpp b/src/publictransport/journeyreply.cpp index 25f7872..9df9cae 100644 --- a/src/publictransport/journeyreply.cpp +++ b/src/publictransport/journeyreply.cpp @@ -1,115 +1,106 @@ /* 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 { +class JourneyReplyPrivate : public ReplyPrivate { public: void postProcessJourneys(); std::vector journeys; - QString errorMsg; - JourneyReply::Error error = JourneyReply::NoError; }; } 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) - : d(new JourneyReplyPrivate) + : Reply(new JourneyReplyPrivate) { 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; std::vector JourneyReply::journeys() const { + Q_D(const JourneyReply); // TODO avoid the copy here return d->journeys; } - -JourneyReply::Error JourneyReply::error() const -{ - return d->error; -} - -QString JourneyReply::errorString() const -{ - return d->errorMsg; -} diff --git a/src/publictransport/journeyreply.h b/src/publictransport/journeyreply.h index 17582df..40c4f6e39 100644 --- a/src/publictransport/journeyreply.h +++ b/src/publictransport/journeyreply.h @@ -1,68 +1,51 @@ /* 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 +#include "reply.h" -#include #include class QNetworkAccessManager; namespace KPublicTransport { class Journey; class JourneyReplyPrivate; class JourneyRequest; /** Journey query response. */ -class JourneyReply : public QObject +class JourneyReply : public Reply { Q_OBJECT public: ~JourneyReply(); /** Returns the found journeys. */ std::vector journeys() const; - /** Error types. */ - enum Error { - NoError, ///< Nothing went wrong. - NetworkError, ///< Error during network operations. - NotFoundError, ///< The requested journey could not be found. - UnknownError ///< Anything else. - }; - /** Error code. */ - Error error() const; - /** Textual error message. */ - QString errorString() const; - -Q_SIGNALS: - /** Emitted whenever the journey search has been completed. */ - void finished(); - private: friend class Manager; explicit JourneyReply(const JourneyRequest &req, QNetworkAccessManager *nam); - std::unique_ptr d; + Q_DECLARE_PRIVATE(JourneyReply) }; } #endif // KPUBLICTRANSPORT_JOURNEYREPLY_H diff --git a/src/publictransport/reply.cpp b/src/publictransport/reply.cpp new file mode 100644 index 0000000..cdf1bfd --- /dev/null +++ b/src/publictransport/reply.cpp @@ -0,0 +1,38 @@ +/* + 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 "reply.h" +#include "reply_p.h" + +using namespace KPublicTransport; + +Reply::Reply(ReplyPrivate *dd) + : d_ptr(dd) +{ +} + +Reply::~Reply() = default; + +Reply::Error Reply::error() const +{ + return d_ptr->error; +} + +QString Reply::errorString() const +{ + return d_ptr->errorMsg; +} diff --git a/src/publictransport/journeyreply.h b/src/publictransport/reply.h similarity index 68% copy from src/publictransport/journeyreply.h copy to src/publictransport/reply.h index 17582df..d767ef7 100644 --- a/src/publictransport/journeyreply.h +++ b/src/publictransport/reply.h @@ -1,68 +1,62 @@ /* 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 +#ifndef KPUBLICTRANSPORT_REPLY_H +#define KPUBLICTRANSPORT_REPLY_H #include #include -#include -class QNetworkAccessManager; +template static inline typename std::unique_ptr::pointer qGetPtrHelper(const std::unique_ptr &p) { return p.get(); } namespace KPublicTransport { -class Journey; -class JourneyReplyPrivate; -class JourneyRequest; +class ReplyPrivate; -/** Journey query response. */ -class JourneyReply : public QObject +/** Query response base class. */ +class Reply : public QObject { Q_OBJECT public: - ~JourneyReply(); - - /** Returns the found journeys. */ - std::vector journeys() const; + ~Reply(); /** Error types. */ enum Error { NoError, ///< Nothing went wrong. NetworkError, ///< Error during network operations. - NotFoundError, ///< The requested journey could not be found. + NotFoundError, ///< The requested journey/departure/place could not be found. UnknownError ///< Anything else. }; + /** Error code. */ Error error() const; /** Textual error message. */ QString errorString() const; Q_SIGNALS: /** Emitted whenever the journey search has been completed. */ void finished(); -private: - friend class Manager; - explicit JourneyReply(const JourneyRequest &req, QNetworkAccessManager *nam); - std::unique_ptr d; +protected: + explicit Reply(ReplyPrivate *dd); + std::unique_ptr d_ptr; }; } #endif // KPUBLICTRANSPORT_JOURNEYREPLY_H diff --git a/src/publictransport/reply_p.h b/src/publictransport/reply_p.h new file mode 100644 index 0000000..73cc1d5 --- /dev/null +++ b/src/publictransport/reply_p.h @@ -0,0 +1,39 @@ +/* + 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_REPLY_P_H +#define KPUBLICTRANSPORT_REPLY_P_H + +#include "reply.h" + +#include + +namespace KPublicTransport { + +class ReplyPrivate +{ +public: + virtual ~ReplyPrivate() = default; + + QString errorMsg; + Reply::Error error = Reply::NoError; +}; + +} + +#endif // KPUBLICTRANSPORT_REPLY_P_H +