File indexing completed on 2024-04-21 04:41:38

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <gbfs/gbfsjob.h>
0008 
0009 #include <QCommandLineParser>
0010 #include <QCoreApplication>
0011 #include <QNetworkAccessManager>
0012 #include <QUrl>
0013 
0014 #include <iostream>
0015 
0016 using namespace KPublicTransport;
0017 
0018 int main(int argc, char** argv)
0019 {
0020     QCoreApplication app(argc, argv);
0021     QCommandLineParser parser;
0022     parser.addHelpOption();
0023     QCommandLineOption listOpt(QStringLiteral("list"), QStringLiteral("list all known services"));
0024     parser.addOption(listOpt);
0025     QCommandLineOption discoverOpt(QStringLiteral("discover"), QStringLiteral("perform discovery on the given GBFS feed"), QStringLiteral("url"));
0026     parser.addOption(discoverOpt);
0027     parser.process(app);
0028 
0029     const auto& services = GBFSServiceRepository::services();
0030     if (parser.isSet(listOpt)) {
0031         for (const auto &s : services) {
0032             std::cout << qPrintable(s.systemId) << " (" << qPrintable(s.discoveryUrl.toString()) << ") - ["
0033                 << s.boundingBox.topLeft().x() << "," << s.boundingBox.topLeft().y() << "|"
0034                 << s.boundingBox.bottomRight().x() << "," << s.boundingBox.bottomRight().y() << "]" << std::endl;
0035         }
0036         return 0;
0037     }
0038 
0039     QNetworkAccessManager nam;
0040     nam.setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
0041 
0042     GBFSJob job(&nam);
0043     int exitCode = -1;
0044     QObject::connect(&job, &GBFSJob::finished, &app, [&]() {
0045         if (job.error() != GBFSJob::NoError) {
0046             qWarning() << job.error() << job.errorMessage();
0047             app.exit(exitCode = 1);
0048         } else {
0049             app.exit(exitCode = 0);
0050         }
0051     });
0052 
0053     GBFSService service;
0054     service.discoveryUrl = QUrl(parser.value(discoverOpt));
0055 
0056     for (const auto &s : services) {
0057         if (s.discoveryUrl == service.discoveryUrl) {
0058             service = s;
0059             break;
0060         }
0061     }
0062     job.discoverAndUpdate(service);
0063 
0064     return exitCode < 0 ? app.exec() : exitCode;
0065 }