| 1 | /* |
| 2 | This file is part of KNewStuff2. |
| 3 | SPDX-FileCopyrightText: 2019 Dan Leinir Turthra Jensen <admin@leinir.dk> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "knsrcmodel.h" |
| 9 | #include <KLocalizedQmlContext> |
| 10 | |
| 11 | #include <KLocalizedString> |
| 12 | |
| 13 | #include <QApplication> |
| 14 | #include <QCommandLineOption> |
| 15 | #include <QCommandLineParser> |
| 16 | #include <QQmlApplicationEngine> |
| 17 | #include <QQmlContext> |
| 18 | |
| 19 | int main(int argc, char **argv) |
| 20 | { |
| 21 | QApplication app(argc, argv); |
| 22 | QCoreApplication::setApplicationName(QStringLiteral("knewstuff-dialog" )); |
| 23 | QCoreApplication::setApplicationVersion(QStringLiteral("1.0" )); |
| 24 | QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org" )); |
| 25 | KLocalizedString::setApplicationDomain("knewstuff-dialog" ); |
| 26 | |
| 27 | QCommandLineParser *parser = new QCommandLineParser; |
| 28 | parser->addHelpOption(); |
| 29 | parser->addPositionalArgument(QStringLiteral("knsrcfile" ), |
| 30 | i18n("The KNSRC file you want to show. If none is passed, you will be presented with a dialog which lets you switch between " |
| 31 | "all the config files installed into the systemwide knsrc file location" )); |
| 32 | parser->addOption( |
| 33 | commandLineOption: QCommandLineOption(QStringLiteral("url" ), |
| 34 | i18n("A kns url to show information from. The format for a kns url is kns://knsrcfile/providerid/entryid\n'knsrcfile'\nis the name " |
| 35 | "of a knsrc file as might be passed directly through this tool's knsrcfile argument\n'providerid'\nis the hostname of the " |
| 36 | "provider the entry should exist on\n'entryid'\nis the unique ID of an entry found in the provider specified by the knsrc " |
| 37 | "file.\n An example of such a url is kns://sddmtheme.knsrc/api.kde-look.org/2059021" ), |
| 38 | QStringLiteral("knsurl" ))); |
| 39 | parser->process(app); |
| 40 | |
| 41 | QQmlApplicationEngine *appengine = new QQmlApplicationEngine(); |
| 42 | qmlRegisterType<KNSRCModel>(uri: "org.kde.newstuff.tools.dialog" , versionMajor: 1, versionMinor: 0, qmlName: "KNSRCModel" ); |
| 43 | auto *context = new KLocalizedQmlContext(appengine); |
| 44 | context->setTranslationDomain(QStringLiteral("knewstuff6" )); |
| 45 | appengine->rootContext()->setContextObject(context); |
| 46 | |
| 47 | if (parser->optionNames().contains(QStringLiteral("url" ))) { |
| 48 | const QUrl url(parser->value(QStringLiteral("url" ))); |
| 49 | Q_ASSERT(url.isValid()); |
| 50 | Q_ASSERT(url.scheme() == QLatin1String("kns" )); |
| 51 | |
| 52 | const QString knsrcfile{url.host()}; |
| 53 | |
| 54 | const QStringList pathParts = url.path().split(sep: QLatin1Char('/'), behavior: Qt::SkipEmptyParts); |
| 55 | const QString providerId = pathParts.at(i: 0); |
| 56 | const QString entryId = pathParts.at(i: 1); |
| 57 | if (pathParts.size() != 2) { |
| 58 | qWarning() << "wrong format in the url path" << url << pathParts; |
| 59 | } |
| 60 | Q_ASSERT(!providerId.isEmpty() && !entryId.isEmpty()); |
| 61 | |
| 62 | appengine->rootContext()->setContextProperty(QStringLiteral("knsrcfile" ), knsrcfile); |
| 63 | appengine->rootContext()->setContextProperty(QStringLiteral("knsProviderId" ), providerId); |
| 64 | appengine->rootContext()->setContextProperty(QStringLiteral("knsEntryId" ), entryId); |
| 65 | appengine->load(QStringLiteral("qrc:/qml/dialog.qml" )); |
| 66 | } else if (!parser->positionalArguments().isEmpty()) { |
| 67 | appengine->rootContext()->setContextProperty(QStringLiteral("knsrcfile" ), parser->positionalArguments().first()); |
| 68 | appengine->load(QStringLiteral("qrc:/qml/dialog.qml" )); |
| 69 | } else { |
| 70 | appengine->load(QStringLiteral("qrc:/qml/main.qml" )); |
| 71 | } |
| 72 | |
| 73 | return app.exec(); |
| 74 | } |
| 75 | |