main.cpp 4.24 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 22 23

#include "pkpassmanager.h"
#include "timelinemodel.h"
#include "pkpassimageprovider.h"
Volker Krause's avatar
Volker Krause committed
24
#include "reservationmanager.h"
25 26 27 28 29 30 31

#include <KPkPass/Field>
#include <KPkPass/Barcode>

#include <QQmlApplicationEngine>
#include <QQmlContext>

32 33 34 35 36
#ifdef Q_OS_ANDROID
#include <QtAndroid>
#include <QAndroidJniObject>
#endif

37
#include <QCommandLineParser>
Volker Krause's avatar
Volker Krause committed
38
#include <QDebug>
39 40 41
#include <QGuiApplication>
#include <QIcon>

Volker Krause's avatar
Volker Krause committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
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>());
61
        passMgr->importPassFromTempFile(tmpFile.toString());
Volker Krause's avatar
Volker Krause committed
62 63 64 65 66 67 68 69 70 71 72 73 74
    } 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
75 76 77
#ifdef Q_OS_ANDROID
Q_DECL_EXPORT
#endif
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
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
98
    ReservationManager resMgr;
99
    resMgr.setPkPassManager(&passMgr);
100 101
    TimelineModel timelineModel;
    timelineModel.setPkPassManager(&passMgr);
Volker Krause's avatar
Volker Krause committed
102
    timelineModel.setReservationManager(&resMgr);
103 104 105 106 107 108 109

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

    QQmlApplicationEngine engine;
    engine.addImageProvider(QStringLiteral("org.kde.pkpass"), new PkPassImageProvider(&passMgr));
    engine.rootContext()->setContextProperty(QStringLiteral("_pkpassManager"), &passMgr);
Volker Krause's avatar
Volker Krause committed
110
    engine.rootContext()->setContextProperty(QStringLiteral("_reservationManager"), &resMgr);
111 112 113 114 115
    engine.rootContext()->setContextProperty(QStringLiteral("_timelineModel"), &timelineModel);
    engine.load(QStringLiteral(":/main.qml"));

    for (const auto &file : parser.positionalArguments())
        passMgr.importPass(QUrl::fromLocalFile(file));
Volker Krause's avatar
Volker Krause committed
116
    handleViewIntent(&passMgr);
117

118 119
    return app.exec();
}