diff --git a/src/app/main.cpp b/src/app/main.cpp index 883fd8f..682d151 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -1,325 +1,325 @@ /* 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 "itinerary_version.h" #include "logging.h" #include "applicationcontroller.h" #include "brightnessmanager.h" #include "countrymodel.h" #include "documentmanager.h" #include "documentsmodel.h" #include "favoritelocationmodel.h" #include "livedatamanager.h" #include "localizer.h" #include "locationinformation.h" #include "lockmanager.h" #include "navigationcontroller.h" #include "pkpassmanager.h" #include "timelinemodel.h" #include "pkpassimageprovider.h" #include "publictransport.h" #include "reservationmanager.h" #include "settings.h" #include "statisticsmodel.h" #include "statisticstimerangemodel.h" #include "tickettokenmodel.h" #include "timelinedelegatecontroller.h" #include "transfermanager.h" #include "tripgroupinfoprovider.h" #include "tripgroupmanager.h" #include "tripgroupproxymodel.h" #include "util.h" #include "weatherforecastmodel.h" #include #include #include #include #include #include #ifndef Q_OS_ANDROID #include #endif #include #include #include #include #include #ifdef Q_OS_ANDROID #include #include #include #include #else #include #endif #include #include #include #include #include #include void registerKPkPassTypes() { qmlRegisterUncreatableType("org.kde.pkpass", 1, 0, "Barcode", {}); qmlRegisterUncreatableType("org.kde.pkpass", 1, 0, "Field", {}); qmlRegisterUncreatableType("org.kde.pkpass", 1, 0, "Pass", {}); qmlRegisterUncreatableType("org.kde.pkpass", 1, 0, "BoardingPass", {}); } void registerKItineraryTypes() { qRegisterMetaType(); qmlRegisterUncreatableType("org.kde.kitinerary", 1, 0, "Ticket", {}); qmlRegisterUncreatableMetaObject(KItinerary::KnowledgeDb::staticMetaObject, "org.kde.kitinerary", 1, 0, "KnowledgeDb", {}); } void registerApplicationTypes() { qRegisterMetaType(); qRegisterMetaType(); qRegisterMetaType(); qRegisterMetaType(); qmlRegisterUncreatableType("org.kde.itinerary", 1, 0, "LocationInformation", {}); qmlRegisterUncreatableType("org.kde.itinerary", 1, 0, "StatisticsItem", {}); qmlRegisterUncreatableType("org.kde.itinerary", 1, 0, "TimelineElement", {}); qmlRegisterUncreatableType("org.kde.itinerary", 1, 0, "TimelineModel", {}); qmlRegisterUncreatableType("org.kde.itinerary", 1, 0, "Transfer", {}); qmlRegisterType("org.kde.itinerary", 1, 0, "CountryModel"); qmlRegisterType("org.kde.itinerary", 1, 0, "DocumentsModel"); qmlRegisterType("org.kde.itinerary", 1, 0, "SortFilterProxyModel"); // TODO use this from kitemmodels? qmlRegisterType("org.kde.itinerary", 1, 0, "StatisticsModel"); qmlRegisterType("org.kde.itinerary", 1, 0, "StatisticsTimeRangeModel"); qmlRegisterType("org.kde.itinerary", 1, 0, "TicketTokenModel"); qmlRegisterType("org.kde.itinerary", 1, 0, "TimelineDelegateController"); qmlRegisterType("org.kde.itinerary", 1, 0, "WeatherForecastModel"); } // for registering QML singletons only static DocumentManager *s_documentManager = nullptr; static FavoriteLocationModel *s_favoriteLocationModel = nullptr; static PkPassManager *s_pkPassManager = nullptr; static Settings *s_settings = nullptr; static TransferManager *s_tranferManager = nullptr; static TripGroupManager *s_tripGroupManager = nullptr; static LiveDataManager *s_liveDataMnager = nullptr; static WeatherForecastManager *s_weatherForecastManager = nullptr; #define REGISTER_SINGLETON_INSTANCE(Class, Instance) \ qmlRegisterSingletonType("org.kde.itinerary", 1, 0, #Class, [](QQmlEngine *engine, QJSEngine*) -> QObject* { \ engine->setObjectOwnership(Instance, QQmlEngine::CppOwnership); \ return Instance; \ }); void registerApplicationSingletons() { REGISTER_SINGLETON_INSTANCE(ApplicationController, ApplicationController::instance()) REGISTER_SINGLETON_INSTANCE(DocumentManager, s_documentManager) REGISTER_SINGLETON_INSTANCE(FavoriteLocationModel, s_favoriteLocationModel) REGISTER_SINGLETON_INSTANCE(PkPassManager, s_pkPassManager) REGISTER_SINGLETON_INSTANCE(Settings, s_settings) REGISTER_SINGLETON_INSTANCE(TransferManager, s_tranferManager) REGISTER_SINGLETON_INSTANCE(TripGroupManager, s_tripGroupManager) REGISTER_SINGLETON_INSTANCE(LiveDataManager, s_liveDataMnager) REGISTER_SINGLETON_INSTANCE(WeatherForecastManager, s_weatherForecastManager) qmlRegisterSingletonType("org.kde.itinerary", 1, 0, "Localizer", [](QQmlEngine*, QJSEngine *engine) -> QJSValue { return engine->toScriptValue(Localizer()); }); qmlRegisterSingletonType("org.kde.itinerary", 1, 0, "NavigationController", [](QQmlEngine*, QJSEngine *engine) -> QJSValue { return engine->toScriptValue(NavigationController()); }); qmlRegisterSingletonType("org.kde.itinerary", 1, 0, "PublicTransport", [](QQmlEngine*, QJSEngine *engine) -> QJSValue { return engine->toScriptValue(PublicTransport()); }); qmlRegisterSingletonType("org.kde.itinerary", 1, 0, "Util", [](QQmlEngine*, QJSEngine*) -> QObject*{ return new Util; }); } #undef REGISTER_SINGLETON_INSTANCE void handleViewIntent(ApplicationController *appController) { #ifdef Q_OS_ANDROID // handle opened files using namespace KAndroidExtras; appController->importFromUrl(Activity::getIntent().getData()); #else Q_UNUSED(appController); #endif } void handlePositionalArguments(ApplicationController *appController, const QStringList &args) { for (const auto &file : args) { const auto localUrl = QUrl::fromLocalFile(file); if (QFile::exists(localUrl.toLocalFile())) appController->importFromUrl(localUrl); else appController->importFromUrl(QUrl::fromUserInput(file)); } } #ifdef Q_OS_ANDROID Q_DECL_EXPORT #endif int main(int argc, char **argv) { QCoreApplication::setApplicationName(QStringLiteral("itinerary")); QCoreApplication::setOrganizationName(QStringLiteral("KDE")); QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org")); QCoreApplication::setApplicationVersion(QStringLiteral(ITINERARY_VERSION_STRING)); - QGuiApplication::setApplicationDisplayName(i18n("KDE Itinerary")); QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); #ifdef Q_OS_ANDROID QGuiApplication app(argc, argv); QQuickStyle::setStyle(QStringLiteral("Material")); #else QApplication app(argc, argv); // for native file dialogs #endif + QGuiApplication::setApplicationDisplayName(i18n("KDE Itinerary")); QGuiApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("itinerary"))); QCommandLineParser parser; parser.addHelpOption(); parser.addVersionOption(); parser.addPositionalArgument(QStringLiteral("file"), i18n("PkPass or JSON-LD file to import.")); parser.process(app); #ifndef Q_OS_ANDROID KDBusService service(KDBusService::Unique); #endif Settings settings; s_settings = &settings; PkPassManager passMgr; s_pkPassManager = &passMgr; ReservationManager resMgr; resMgr.setPkPassManager(&passMgr); DocumentManager docMgr; s_documentManager = &docMgr; FavoriteLocationModel favLocModel; s_favoriteLocationModel = &favLocModel; TripGroupManager tripGroupMgr; tripGroupMgr.setReservationManager(&resMgr); s_tripGroupManager = &tripGroupMgr; BrightnessManager brightnessManager; LockManager lockManager; LiveDataManager liveDataMgr; liveDataMgr.setPkPassManager(&passMgr); liveDataMgr.setReservationManager(&resMgr); liveDataMgr.setPollingEnabled(settings.queryLiveData()); QObject::connect(&settings, &Settings::queryLiveDataChanged, &liveDataMgr, &LiveDataManager::setPollingEnabled); s_liveDataMnager = &liveDataMgr; WeatherForecastManager weatherForecastMgr; weatherForecastMgr.setAllowNetworkAccess(settings.weatherForecastEnabled()); QObject::connect(&settings, &Settings::weatherForecastEnabledChanged, &weatherForecastMgr, &WeatherForecastManager::setAllowNetworkAccess); s_weatherForecastManager = &weatherForecastMgr; TransferManager transferManager; transferManager.setReservationManager(&resMgr); transferManager.setTripGroupManager(&tripGroupMgr); transferManager.setFavoriteLocationModel(&favLocModel); transferManager.setPublicTransportManager(liveDataMgr.publicTransportManager()); transferManager.setAutoAddTransfers(settings.autoAddTransfers()); transferManager.setAutoFillTransfers(settings.autoFillTransfers()); QObject::connect(&settings, &Settings::autoAddTransfersChanged, &transferManager, &TransferManager::setAutoAddTransfers); QObject::connect(&settings, &Settings::autoFillTransfersChanged, &transferManager, &TransferManager::setAutoFillTransfers); s_tranferManager = &transferManager; TimelineModel timelineModel; timelineModel.setHomeCountryIsoCode(settings.homeCountryIsoCode()); timelineModel.setReservationManager(&resMgr); timelineModel.setWeatherForecastManager(&weatherForecastMgr); timelineModel.setTransferManager(&transferManager); timelineModel.setTripGroupManager(&tripGroupMgr); QObject::connect(&settings, &Settings::homeCountryIsoCodeChanged, &timelineModel, &TimelineModel::setHomeCountryIsoCode); TripGroupProxyModel tripGroupProxy; tripGroupProxy.setSourceModel(&timelineModel); TripGroupInfoProvider tripGroupInfoProvider; tripGroupInfoProvider.setReservationManager(&resMgr); tripGroupInfoProvider.setWeatherForecastManager(&weatherForecastMgr); ApplicationController appController; appController.setReservationManager(&resMgr); appController.setPkPassManager(&passMgr); appController.setDocumentManager(&docMgr); appController.setFavoriteLocationModel(&favLocModel); #ifndef Q_OS_ANDROID QObject::connect(&service, &KDBusService::activateRequested, [&parser, &appController](const QStringList &args, const QString &workingDir) { qCDebug(Log) << "remote activation" << args << workingDir; if (!args.isEmpty()) { QDir::setCurrent(workingDir); parser.parse(args); handlePositionalArguments(&appController, parser.positionalArguments()); } if (!QGuiApplication::allWindows().isEmpty()) { QGuiApplication::allWindows().at(0)->requestActivate(); } }); #endif registerKPkPassTypes(); registerKItineraryTypes(); registerApplicationTypes(); registerApplicationSingletons(); QQmlApplicationEngine engine; engine.addImageProvider(QStringLiteral("org.kde.pkpass"), new PkPassImageProvider(&passMgr)); auto l10nContext = new KLocalizedContext(&engine); l10nContext->setTranslationDomain(QStringLiteral(TRANSLATION_DOMAIN)); engine.rootContext()->setContextObject(l10nContext); // TODO get rid of those, e.g. by using singletons engine.rootContext()->setContextProperty(QStringLiteral("_reservationManager"), &resMgr); engine.rootContext()->setContextProperty(QStringLiteral("_timelineModel"), &tripGroupProxy); engine.rootContext()->setContextProperty(QStringLiteral("_brightnessManager"), &brightnessManager); engine.rootContext()->setContextProperty(QStringLiteral("_lockManager"), &lockManager); engine.rootContext()->setContextProperty(QStringLiteral("_tripGroupInfoProvider"), QVariant::fromValue(tripGroupInfoProvider)); engine.load(QStringLiteral("qrc:/main.qml")); handlePositionalArguments(&appController, parser.positionalArguments()); handleViewIntent(&appController); return app.exec(); }