File indexing completed on 2024-05-05 16:16:11

0001 /*
0002     This file is part of KNewStuff2.
0003     SPDX-FileCopyrightText: 2019 Dan Leinir Turthra Jensen <admin@leinir.dk>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "knsrcmodel.h"
0009 
0010 #include "engine.h"
0011 
0012 #include <KLocalizedString>
0013 
0014 #include <QApplication>
0015 #include <QCommandLineOption>
0016 #include <QCommandLineParser>
0017 #include <QQmlApplicationEngine>
0018 #include <QQmlContext>
0019 
0020 int main(int argc, char **argv)
0021 {
0022     QApplication app(argc, argv);
0023     QCoreApplication::setApplicationName(QStringLiteral("knewstuff-dialog"));
0024     QCoreApplication::setApplicationVersion(QStringLiteral("1.0"));
0025     QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
0026     KLocalizedString::setApplicationDomain("knewstuff-dialog");
0027 
0028     QCommandLineParser *parser = new QCommandLineParser;
0029     parser->addHelpOption();
0030     parser->addPositionalArgument(QStringLiteral("knsrcfile"),
0031                                   i18n("The KNSRC file you want to show. If none is passed, you will be presented with a dialog which lets you switch between "
0032                                        "all the config files installed into the systemwide knsrc file location"));
0033     parser->addOption(
0034         QCommandLineOption(QStringLiteral("url"),
0035                            i18n("A kns url to show information from. The format for a kns url is kns://knsrcfile/providerid/entryid\n'knsrcfile'\nis the name "
0036                                 "of a knsrc file as might be passed directly through this tool's knsrcfile argument\n'providerid'\nis the hostname of the "
0037                                 "provider the entry should exist on\n'entryid'\nis the unique ID of an entry found in the provider specified by the knsrc "
0038                                 "file.\n An example of such a url is kns://peruse.knsrc/api.kde-look.org/1316714"),
0039                            QStringLiteral("knsurl")));
0040     parser->process(app);
0041 
0042     QQmlApplicationEngine *appengine = new QQmlApplicationEngine();
0043     qmlRegisterType<KNSRCModel>("org.kde.newstuff.tools.dialog", 1, 0, "KNSRCModel");
0044     auto *context = new KLocalizedContext(appengine);
0045     context->setTranslationDomain(QStringLiteral("knewstuff5"));
0046     appengine->rootContext()->setContextObject(context);
0047 
0048     if (parser->optionNames().contains(QStringLiteral("url"))) {
0049         const QUrl url(parser->value(QStringLiteral("url")));
0050         Q_ASSERT(url.isValid());
0051         Q_ASSERT(url.scheme() == QLatin1String("kns"));
0052 
0053         const QString knsrcfile{url.host()};
0054         const auto pathParts = url.path().split(QLatin1Char('/'), Qt::SkipEmptyParts);
0055         if (pathParts.size() != 2) {
0056             qWarning() << "wrong format in the url path" << url << pathParts;
0057             return 1;
0058         }
0059         const auto providerId = pathParts.at(0);
0060         const auto entryId = pathParts.at(1);
0061         appengine->rootContext()->setContextProperty(QStringLiteral("knsrcfile"), knsrcfile);
0062         appengine->rootContext()->setContextProperty(QStringLiteral("knsProviderId"), providerId);
0063         appengine->rootContext()->setContextProperty(QStringLiteral("knsEntryId"), entryId);
0064         appengine->load(QStringLiteral("qrc:/qml/dialog.qml"));
0065     } else if (!parser->positionalArguments().isEmpty()) {
0066         appengine->rootContext()->setContextProperty(QStringLiteral("knsrcfile"), parser->positionalArguments().first());
0067         appengine->load(QStringLiteral("qrc:/qml/dialog.qml"));
0068     } else {
0069         appengine->load(QStringLiteral("qrc:/qml/main.qml"));
0070     }
0071 
0072     return app.exec();
0073 }