main.cpp 5.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
    Copyright (C) 2018 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 <http://www.gnu.org/licenses/>.
*/

#include "itinerary_version.h"
Volker Krause's avatar
Volker Krause committed
19
#include "logging.h"
20

21
#include "applicationcontroller.h"
22
#include "countryinformation.h"
23
#include "localizer.h"
24 25 26
#include "pkpassmanager.h"
#include "timelinemodel.h"
#include "pkpassimageprovider.h"
Volker Krause's avatar
Volker Krause committed
27
#include "reservationmanager.h"
28

29
#include <KItinerary/CountryDb>
30 31
#include <KItinerary/Ticket>

32 33 34 35 36 37
#include <KPkPass/Field>
#include <KPkPass/Barcode>

#include <QQmlApplicationEngine>
#include <QQmlContext>

38 39 40 41 42
#ifdef Q_OS_ANDROID
#include <QtAndroid>
#include <QAndroidJniObject>
#endif

43
#include <QCommandLineParser>
Volker Krause's avatar
Volker Krause committed
44
#include <QDebug>
45 46 47
#include <QGuiApplication>
#include <QIcon>

Volker Krause's avatar
Volker Krause committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
void handleViewIntent(PkPassManager *passMgr)
{
#ifdef Q_OS_ANDROID
    // handle opened files
    const auto activity = QtAndroid::androidActivity();
    if (!activity.isValid())
        return;

    const auto intent = activity.callObjectMethod("getIntent", "()Landroid/content/Intent;");
    if (!intent.isValid())
        return;

    const auto uri = intent.callObjectMethod("getData", "()Landroid/net/Uri;");
    if (!uri.isValid())
        return;

    const auto scheme = uri.callObjectMethod("getScheme", "()Ljava/lang/String;");
    if (scheme.toString() == QLatin1String("content")) {
        const auto tmpFile = activity.callObjectMethod("receiveContent", "(Landroid/net/Uri;)Ljava/lang/String;", uri.object<jobject>());
67
        passMgr->importPassFromTempFile(tmpFile.toString());
Volker Krause's avatar
Volker Krause committed
68 69 70 71 72 73 74 75 76 77 78 79 80
    } else if (scheme.toString() == QLatin1String("file")) {
        const auto uriStr = uri.callObjectMethod("toString", "()Ljava/lang/String;");
        passMgr->importPass(QUrl(uriStr.toString()));
    } else {
        const auto uriStr = uri.callObjectMethod("toString", "()Ljava/lang/String;");
        qCWarning(Log) << "Unknown intent URI:" << uriStr.toString();
    }
#else
    Q_UNUSED(passMgr);
#endif
}


Volker Krause's avatar
Volker Krause committed
81 82 83
#ifdef Q_OS_ANDROID
Q_DECL_EXPORT
#endif
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
int main(int argc, char **argv)
{
    QCoreApplication::setApplicationName(QStringLiteral("kde-itinerary"));
    QCoreApplication::setOrganizationName(QStringLiteral("KDE"));
    QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
    QCoreApplication::setApplicationVersion(QStringLiteral(ITINERARY_VERSION_STRING));

    QGuiApplication::setApplicationDisplayName(QStringLiteral("KDE Itinerary")); // TODO i18n
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
    QGuiApplication app(argc, argv);
    QGuiApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("map-globe")));

    QCommandLineParser parser;
    parser.addHelpOption();
    parser.addVersionOption();
    parser.addPositionalArgument("pass", QStringLiteral("PkPass file to import."));
    parser.process(app);

    PkPassManager passMgr;
Volker Krause's avatar
Volker Krause committed
104
    ReservationManager resMgr;
105
    resMgr.setPkPassManager(&passMgr);
106
    TimelineModel timelineModel;
Volker Krause's avatar
Volker Krause committed
107
    timelineModel.setReservationManager(&resMgr);
108

109 110
    ApplicationController appController;

111 112 113
    qmlRegisterUncreatableType<KPkPass::Barcode>("org.kde.pkpass", 1, 0, "Barcode", {});
    qmlRegisterUncreatableType<KPkPass::Field>("org.kde.pkpass", 1, 0, "Field", {});

114
    qRegisterMetaType<KItinerary::KnowledgeDb::DrivingSide>();
115
    qmlRegisterUncreatableType<KItinerary::Ticket>("org.kde.kitinerary", 1, 0, "Ticket", {});
116
    qmlRegisterUncreatableMetaObject(KItinerary::KnowledgeDb::staticMetaObject, "org.kde.kitinerary", 1, 0, "KnowledgeDb", {});
117

118
    qmlRegisterUncreatableType<CountryInformation>("org.kde.itinerary", 1, 0, "CountryInformation", {});
119
    qmlRegisterUncreatableType<TimelineModel>("org.kde.itinerary", 1, 0, "TimelineModel", {});
120 121 122
    qmlRegisterSingletonType<Localizer>("org.kde.itinerary", 1, 0, "Localizer", [](QQmlEngine*, QJSEngine*) -> QObject*{
        return new Localizer;
    });
123

124 125 126
    QQmlApplicationEngine engine;
    engine.addImageProvider(QStringLiteral("org.kde.pkpass"), new PkPassImageProvider(&passMgr));
    engine.rootContext()->setContextProperty(QStringLiteral("_pkpassManager"), &passMgr);
Volker Krause's avatar
Volker Krause committed
127
    engine.rootContext()->setContextProperty(QStringLiteral("_reservationManager"), &resMgr);
128
    engine.rootContext()->setContextProperty(QStringLiteral("_timelineModel"), &timelineModel);
129
    engine.rootContext()->setContextProperty(QStringLiteral("_appController"), &appController);
130 131
    engine.load(QStringLiteral(":/main.qml"));

132 133 134 135 136 137
    for (const auto &file : parser.positionalArguments()) {
        if (file.endsWith(QLatin1String(".pkpass")))
            passMgr.importPass(QUrl::fromLocalFile(file));
        else
            resMgr.importReservation(QUrl::fromLocalFile(file));
    }
Volker Krause's avatar
Volker Krause committed
138
    handleViewIntent(&passMgr);
139

140 141
    return app.exec();
}