File indexing completed on 2024-06-09 09:10:17

0001 // SPDX-FileCopyrightText: 2019-2020 Jonah Brüchert <jbb@kaidan.im>
0002 // SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org>
0003 // SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 #include <QApplication>
0006 #include <QCommandLineParser>
0007 #include <QIcon>
0008 #include <QQmlApplicationEngine>
0009 #include <QUrl>
0010 #include <QtQml>
0011 
0012 #include <KAboutData>
0013 #include <KLocalizedContext>
0014 #include <KLocalizedString>
0015 
0016 #include "fontlistmodel.h"
0017 #include "savedcommandsmodel.h"
0018 #include "shellcommand.h"
0019 #include "terminalsettings.h"
0020 #include "terminaltabmodel.h"
0021 #include "util.h"
0022 #include "version.h"
0023 
0024 #include <KonsoleQML.h>
0025 
0026 constexpr auto URI = "org.kde.qmlkonsole";
0027 
0028 Q_DECL_EXPORT int main(int argc, char *argv[])
0029 {
0030     QApplication app(argc, argv);
0031 
0032     KLocalizedString::setApplicationDomain("qmlkonsole");
0033     KAboutData aboutData(QStringLiteral("qmlkonsole"),
0034                          QStringLiteral("QMLKonsole"),
0035                          QStringLiteral(QMLKONSOLE_VERSION_STRING),
0036                          QStringLiteral("Mobile terminal application"),
0037                          KAboutLicense::GPL,
0038                          i18n("© 2020-2022 KDE Community"));
0039     aboutData.setBugAddress("https://bugs.kde.org/enter_bug.cgi?product=QMLKonsole");
0040     aboutData.addAuthor(i18n("Jonah Brüchert"), QString(), QStringLiteral("jbb@kaidan.im"));
0041     aboutData.addAuthor(i18n("Devin Lin"), QString(), QStringLiteral("devin@kde.org"));
0042     KAboutData::setApplicationData(aboutData);
0043 
0044     qmlRegisterSingletonInstance<TerminalSettings>(URI, 1, 0, "TerminalSettings", TerminalSettings::self());
0045     qmlRegisterSingletonType<ShellCommand>(URI, 1, 0, "ShellCommand", [](QQmlEngine *, QJSEngine *) {
0046         return new ShellCommand();
0047     });
0048 
0049     KonsoleQML::registerTypes("org.kde.konsoleqml");
0050 
0051     QObject::connect(TerminalSettings::self(), &TerminalSettings::configChanged, QApplication::instance(), [] {
0052         TerminalSettings::self()->save();
0053         qDebug() << "saving config";
0054     });
0055 
0056     QQmlApplicationEngine engine;
0057     engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
0058 
0059     Util util;
0060     {
0061         QCommandLineParser parser;
0062         parser.addOption(QCommandLineOption(QStringLiteral("e"), i18n("Command to execute"), QStringLiteral("command")));
0063         parser.addOption(QCommandLineOption(QStringLiteral("workdir"), i18n("Set the initial working directory to 'dir'"), QStringLiteral("dir")));
0064         parser.addVersionOption();
0065         parser.addHelpOption();
0066         parser.process(app);
0067 
0068         QString initialCommand = parser.isSet(QStringLiteral("e")) ? parser.value(QStringLiteral("e")) : "";
0069         QString initialWorkDir = parser.isSet(QStringLiteral("workdir")) ? parser.value(QStringLiteral("workdir")) : QDir::currentPath();
0070         util.setInitialCommand(std::move(initialCommand));
0071         util.setInitialWorkDir(std::move(initialWorkDir));
0072     }
0073 
0074     qmlRegisterSingletonType<SavedCommandsModel>(URI, 1, 0, "SavedCommandsModel", [](QQmlEngine *, QJSEngine *) -> QObject * {
0075         const auto self = SavedCommandsModel::self();
0076         QQmlEngine::setObjectOwnership(self, QQmlEngine::CppOwnership);
0077         return self;
0078     });
0079     qmlRegisterSingletonType<TerminalTabModel>(URI, 1, 0, "TerminalTabModel", [](QQmlEngine *, QJSEngine *) -> QObject * {
0080         const auto self = TerminalTabModel::self();
0081         QQmlEngine::setObjectOwnership(self, QQmlEngine::CppOwnership);
0082         return self;
0083     });
0084     qmlRegisterSingletonType<FontListSearchModel>(URI, 1, 0, "FontListSearchModel", [](QQmlEngine *, QJSEngine *) -> QObject * {
0085         const auto self = FontListSearchModel::self();
0086         QQmlEngine::setObjectOwnership(self, QQmlEngine::CppOwnership);
0087         return self;
0088     });
0089     qmlRegisterSingletonInstance(URI, 1, 0, "Util", &util);
0090     qmlRegisterSingletonType(URI, 1, 0, "About", [](QQmlEngine *, QJSEngine *engine) {
0091         return engine->toScriptValue(KAboutData::applicationData());
0092     });
0093 
0094     engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
0095 
0096     if (engine.rootObjects().isEmpty()) {
0097         return -1;
0098     }
0099 
0100     // required for X11
0101     app.setWindowIcon(QIcon::fromTheme(QStringLiteral("org.kde.qmlkonsole")));
0102 
0103     return app.exec();
0104 }