main.cpp 3.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
    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"

#include "pkpassmanager.h"
#include "timelinemodel.h"
#include "pkpassimageprovider.h"

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

#include <QQmlApplicationEngine>
#include <QQmlContext>

30 31 32 33 34
#ifdef Q_OS_ANDROID
#include <QtAndroid>
#include <QAndroidJniObject>
#endif

35 36 37 38
#include <QCommandLineParser>
#include <QGuiApplication>
#include <QIcon>

Volker Krause's avatar
Volker Krause committed
39 40 41
#ifdef Q_OS_ANDROID
Q_DECL_EXPORT
#endif
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
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;
    TimelineModel timelineModel;
    timelineModel.setPkPassManager(&passMgr);

    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);
    engine.rootContext()->setContextProperty(QStringLiteral("_timelineModel"), &timelineModel);
    engine.load(QStringLiteral(":/main.qml"));

    for (const auto &file : parser.positionalArguments())
        passMgr.importPass(QUrl::fromLocalFile(file));

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#ifdef Q_OS_ANDROID
    // handle opened files
    const auto activity = QtAndroid::androidActivity();
    if (activity.isValid()) {
        const auto intent = activity.callObjectMethod("getIntent", "()Landroid/content/Intent;");
        if (intent.isValid()) {
            const auto data = intent.callObjectMethod("getDataString", "()Ljava/lang/String;");
            if (data.isValid()) {
                // TODO handle content:// urls
                passMgr.importPass(QUrl(data.toString()));
            }
        }
    }
#endif

92 93
    return app.exec();
}