File indexing completed on 2024-05-19 07:45:41

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 <KLocalizedString>
0011 
0012 #include <QApplication>
0013 #include <QCommandLineOption>
0014 #include <QCommandLineParser>
0015 #include <QQmlApplicationEngine>
0016 #include <QQmlContext>
0017 
0018 int main(int argc, char **argv)
0019 {
0020     QApplication app(argc, argv);
0021     QCoreApplication::setApplicationName(QStringLiteral("knewstuff-dialog"));
0022     QCoreApplication::setApplicationVersion(QStringLiteral("1.0"));
0023     QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
0024     KLocalizedString::setApplicationDomain("knewstuff-dialog");
0025 
0026     QCommandLineParser *parser = new QCommandLineParser;
0027     parser->addHelpOption();
0028     parser->addPositionalArgument(QStringLiteral("knsrcfile"),
0029                                   i18n("The KNSRC file you want to show. If none is passed, you will be presented with a dialog which lets you switch between "
0030                                        "all the config files installed into the systemwide knsrc file location"));
0031     parser->addOption(
0032         QCommandLineOption(QStringLiteral("url"),
0033                            i18n("A kns url to show information from. The format for a kns url is kns://knsrcfile/providerid/entryid\n'knsrcfile'\nis the name "
0034                                 "of a knsrc file as might be passed directly through this tool's knsrcfile argument\n'providerid'\nis the hostname of the "
0035                                 "provider the entry should exist on\n'entryid'\nis the unique ID of an entry found in the provider specified by the knsrc "
0036                                 "file.\n An example of such a url is kns://sddmtheme.knsrc/api.kde-look.org/2059021"),
0037                            QStringLiteral("knsurl")));
0038     parser->process(app);
0039 
0040     QQmlApplicationEngine *appengine = new QQmlApplicationEngine();
0041     qmlRegisterType<KNSRCModel>("org.kde.newstuff.tools.dialog", 1, 0, "KNSRCModel");
0042     auto *context = new KLocalizedContext(appengine);
0043     context->setTranslationDomain(QStringLiteral("knewstuff6"));
0044     appengine->rootContext()->setContextObject(context);
0045 
0046     if (parser->optionNames().contains(QStringLiteral("url"))) {
0047         const QUrl url(parser->value(QStringLiteral("url")));
0048         Q_ASSERT(url.isValid());
0049         Q_ASSERT(url.scheme() == QLatin1String("kns"));
0050 
0051         const QString knsrcfile{url.host()};
0052 
0053         const QStringList pathParts = url.path().split(QLatin1Char('/'), Qt::SkipEmptyParts);
0054         const QString providerId = pathParts.at(0);
0055         const QString entryId = pathParts.at(1);
0056         if (pathParts.size() != 2) {
0057             qWarning() << "wrong format in the url path" << url << pathParts;
0058         }
0059         Q_ASSERT(!providerId.isEmpty() && !entryId.isEmpty());
0060 
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 }