File indexing completed on 2025-01-05 05:18:57

0001 // SPDX-FileCopyrightText: 2023 Loren Burkholder <computersemiexpert@outlook.com>
0002 //
0003 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 
0005 #include <KAboutData>
0006 #include <KCrash>
0007 #include <KLocalizedContext>
0008 #include <KLocalizedString>
0009 #include <QApplication>
0010 #include <QQmlApplicationEngine>
0011 #include <QQmlContext>
0012 #include <QQuickStyle>
0013 #include <QUrl>
0014 
0015 #include "ChatModel.h"
0016 #include "controller.h"
0017 #include "kllmcoresettings.h"
0018 #include "alpaka-version.h"
0019 #include "windowcontroller.h"
0020 
0021 // signal handler for SIGINT & SIGTERM
0022 #ifdef Q_OS_UNIX
0023 #include <KSignalHandler>
0024 #include <signal.h>
0025 #include <unistd.h>
0026 #endif
0027 
0028 static QWindow *windowFromEngine(QQmlApplicationEngine *engine)
0029 {
0030     const auto rootObjects = engine->rootObjects();
0031     auto *window = qobject_cast<QQuickWindow *>(rootObjects.first());
0032     Q_ASSERT(window);
0033     return window;
0034 }
0035 
0036 int main(int argc, char *argv[])
0037 {
0038     QApplication app(argc, argv);
0039 
0040     KLocalizedString::setApplicationDomain(QByteArrayLiteral("alpaka"));
0041 
0042     QCoreApplication::setOrganizationName(QStringLiteral("KDE"));
0043 
0044     KCrash::initialize();
0045     KAboutData about(QStringLiteral("alpaka"),
0046                      i18n("Alpaka"),
0047                      QStringLiteral(ALPAKA_VERSION_STRING),
0048                      i18n("Ollama client"),
0049                      KAboutLicense::GPL_V3,
0050                      i18n("© 2023 Loren Burkholder"));
0051     about.addAuthor(i18n("Loren Burkholder"), i18n("Maintainer"), QStringLiteral("computersemiexpert@outlook.com"));
0052     about.addCredit(i18n("Laurent Montel"), i18n("Developer"), QStringLiteral("montel@kde.org"));
0053     about.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"), i18nc("EMAIL OF TRANSLATORS", "Your emails"));
0054     about.setOrganizationDomain("kde.org");
0055 
0056     KAboutData::setApplicationData(about);
0057 
0058     if (qEnvironmentVariableIsEmpty("QT_QUICK_CONTROLS_STYLE"))
0059         QQuickStyle::setStyle(QStringLiteral("org.kde.desktop"));
0060 
0061     auto settings = KLLMCoreSettings::self();
0062     auto &controller = Controller::instance();
0063     qmlRegisterSingletonInstance("org.kde.alpaka", 1, 0, "AlpakaSettings", settings);
0064     qmlRegisterSingletonInstance("org.kde.alpaka", 1, 0, "Controller", &controller);
0065     qmlRegisterType<ChatModel>("org.kde.alpaka", 0, 1, "ChatModel");
0066 
0067     Controller::instance();
0068     QQmlApplicationEngine engine;
0069     QObject::connect(
0070         &engine,
0071         &QQmlApplicationEngine::objectCreationFailed,
0072         &app,
0073         [] {
0074             QCoreApplication::exit(-1);
0075         },
0076         Qt::QueuedConnection);
0077     engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
0078     engine.load(QUrl(QStringLiteral("qrc:/org/kde/alpaka/qml/main.qml")));
0079 
0080     app.connect(&app, &QApplication::aboutToQuit, KLLMCoreSettings::self(), &KLLMCoreSettings::save);
0081 
0082     QWindow *window = windowFromEngine(&engine);
0083 
0084     WindowController::instance().setWindow(window);
0085     WindowController::instance().restoreGeometry();
0086 #ifdef Q_OS_UNIX
0087     /**
0088      * Set up signal handler for SIGINT and SIGTERM
0089      */
0090     KSignalHandler::self()->watchSignal(SIGINT);
0091     KSignalHandler::self()->watchSignal(SIGTERM);
0092     QObject::connect(KSignalHandler::self(), &KSignalHandler::signalReceived, &app, [](int signal) {
0093         if (signal == SIGINT || signal == SIGTERM) {
0094             printf("Shutting down...\n");
0095         }
0096     });
0097 #endif
0098 
0099     return app.exec();
0100 }