File indexing completed on 2024-05-12 17:08:26

0001 /*
0002  *   SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
0003  *
0004  *   SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "lnflistmodel.h"
0008 #include "lnflogic.h"
0009 #include <QApplication>
0010 
0011 #include <QQuickItem>
0012 #include <klocalizedstring.h>
0013 #include <qcommandlineoption.h>
0014 #include <qcommandlineparser.h>
0015 
0016 #include <KAboutData>
0017 #include <QQmlContext>
0018 #include <QQmlEngine>
0019 #include <QQmlExpression>
0020 #include <QQmlProperty>
0021 #include <QQuickWindow>
0022 #include <kdeclarative/qmlobject.h>
0023 #include <kpackage/package.h>
0024 #include <kpackage/packageloader.h>
0025 
0026 int main(int argc, char **argv)
0027 {
0028     QCommandLineParser parser;
0029     QApplication app(argc, argv);
0030 
0031     app.setApplicationVersion(PROJECT_VERSION);
0032     parser.addVersionOption();
0033     parser.addHelpOption();
0034     parser.setApplicationDescription(i18n("Plasma Look And Feel Explorer"));
0035 
0036     QCommandLineOption themeOption(QCommandLineOption(QStringList() << "l"
0037                                                                     << "lookandfeel",
0038                                                       i18n("Look And Feel to open"),
0039                                                       "lookandfeel"));
0040 
0041     parser.addOption(themeOption);
0042 
0043     parser.process(app);
0044 
0045     const QString packagePath("org.kde.plasma.lookandfeelexplorer");
0046 
0047     // usually we have an ApplicationWindow here, so we do not need to create a window by ourselves
0048     KDeclarative::QmlObject obj;
0049     obj.setTranslationDomain(packagePath);
0050     obj.setInitializationDelayed(true);
0051     obj.engine()->rootContext()->setContextProperty("commandlineTheme", parser.value(themeOption));
0052     obj.engine()->rootContext()->setContextProperty("commandlineArguments", parser.positionalArguments());
0053     KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("KPackage/GenericQML"));
0054     package.setPath(packagePath);
0055     obj.setSource(QUrl::fromLocalFile(package.filePath("mainscript")));
0056     const KPluginMetaData data = package.metadata();
0057 
0058     qmlRegisterAnonymousType<LnfLogic>("org.kde.plasma.sdk", 1);
0059     qmlRegisterAnonymousType<LnfListModel>("org.kde.plasma.sdk", 1);
0060 
0061     LnfLogic *lnfLogic = new LnfLogic(&obj);
0062     obj.engine()->rootContext()->setContextProperty("lnfLogic", QVariant::fromValue(lnfLogic));
0063 
0064     obj.completeInitialization();
0065 
0066     if (!data.isValid()) {
0067         return -1;
0068     }
0069 
0070     // About data
0071     KAboutData aboutData(data.pluginId(), data.name(), data.version(), data.description(), KAboutLicense::byKeyword(data.license()).key());
0072 
0073     for (auto author : data.authors()) {
0074         aboutData.addAuthor(author.name(), author.task(), author.emailAddress(), author.webAddress(), author.ocsUsername());
0075     }
0076 
0077     // The root is not a window?
0078     // have to use a normal QQuickWindow since the root item is already created
0079     QWindow *window = qobject_cast<QWindow *>(obj.rootObject());
0080     if (window) {
0081         window->setTitle(data.name());
0082         window->setIcon(QIcon::fromTheme(data.iconName()));
0083     } else {
0084         qWarning() << "Error loading the ApplicationWindow";
0085     }
0086 
0087     return app.exec();
0088 }