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

0001 /*
0002  *  SPDX-FileCopyrightText: 2021 Felipe Kinoshita <kinofhek@gmail.com>
0003  *  SPDX-FileCopyrightText: 2022 Nate Graham <nate@kde.org>
0004  *
0005  *  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006  */
0007 
0008 #include <QApplication>
0009 #include <QCommandLineOption>
0010 #include <QCommandLineParser>
0011 #include <QQmlApplicationEngine>
0012 #include <QQuickWindow>
0013 #include <QSurfaceFormat>
0014 #include <QUrl>
0015 #include <QtQml>
0016 
0017 #include "config.h"
0018 #include "controller.h"
0019 #include "plasma-welcome-version.h"
0020 
0021 #include "module.h"
0022 #include <KAboutData>
0023 #include <KDBusService>
0024 #include <KLocalizedContext>
0025 #include <KLocalizedString>
0026 #include <KWindowSystem>
0027 
0028 int main(int argc, char *argv[])
0029 {
0030     auto format = QSurfaceFormat::defaultFormat();
0031     format.setOption(QSurfaceFormat::ResetNotification);
0032     QSurfaceFormat::setDefaultFormat(format);
0033 
0034 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0035     QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
0036     QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
0037 #endif
0038 
0039     QApplication app(argc, argv);
0040     QCoreApplication::setOrganizationName(QStringLiteral("KDE"));
0041     QCoreApplication::setApplicationName(QStringLiteral("plasma-welcome"));
0042     KLocalizedString::setApplicationDomain("plasma-welcome");
0043 
0044     const QString description = i18nc("@info:usagetip", "A welcome app for KDE Plasma");
0045     KAboutData aboutData(
0046         // The program name used internally.
0047         QStringLiteral("plasma-welcome"),
0048         // A displayable program name string.
0049         i18nc("@title", "Welcome to KDE Plasma"),
0050         // The program version string.
0051         QStringLiteral(PLASMA_WELCOME_VERSION_STRING),
0052         // Short description of what the app does.
0053         description,
0054         // The license this code is released under.
0055         KAboutLicense::GPL,
0056         // Copyright Statement.
0057         i18nc("@info copyright string", "(c) 2021"));
0058     aboutData.addAuthor(i18nc("@info:credit", "Felipe Kinoshita"),
0059                         i18nc("@info:credit", "Author"),
0060                         QStringLiteral("kinofhek@gmail.com"),
0061                         QStringLiteral("https://fhek.gitlab.io.com"));
0062     aboutData.addAuthor(i18nc("@info:credit", "Nate Graham"),
0063                         i18nc("@info:credit", "Author"),
0064                         QStringLiteral("nate@kde.org"),
0065                         QStringLiteral("https://pointieststick.com"));
0066     KAboutData::setApplicationData(aboutData);
0067 
0068     QQmlApplicationEngine engine;
0069 
0070     qmlRegisterSingletonInstance("org.kde.welcome", 1, 0, "Config", Config::self());
0071     Controller controller;
0072     qmlRegisterSingletonInstance("org.kde.welcome", 1, 0, "Controller", &controller);
0073     qmlRegisterType<Module>("org.kde.welcome", 1, 0, "Module");
0074 
0075     // Parse CLI args
0076     QCommandLineParser parser;
0077     parser.setApplicationDescription(description);
0078     aboutData.setupCommandLine(&parser);
0079     parser.addOption(QCommandLineOption(QStringLiteral("after-upgrade-to"),
0080                                         i18n("Version number of the Plasma release to display release notes for, e.g. 5.25"),
0081                                         QStringLiteral("version number")));
0082     parser.process(app);
0083     aboutData.processCommandLine(&parser);
0084     if (parser.isSet(QStringLiteral("after-upgrade-to"))) {
0085         const QString versionNumber = parser.value(QStringLiteral("after-upgrade-to"));
0086         controller.setPlasmaUpgradeVersion(versionNumber);
0087     }
0088 
0089     engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
0090     engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
0091 
0092     if (engine.rootObjects().isEmpty()) {
0093         return -1;
0094     }
0095 
0096     // Make it single-instance so it raises e.g. when middle-clicking on taskmanager
0097     // or accidentally launching it while it's already open
0098     KDBusService service(KDBusService::Unique);
0099     QObject::connect(&service, &KDBusService::activateRequested, &engine, [&engine](const QStringList & /*arguments*/, const QString & /*workingDirectory*/) {
0100         const auto rootObjects = engine.rootObjects();
0101         for (auto obj : rootObjects) {
0102             auto view = qobject_cast<QQuickWindow *>(obj);
0103             if (view) {
0104                 KWindowSystem::updateStartupId(view);
0105                 KWindowSystem::activateWindow(view);
0106                 return;
0107             }
0108         }
0109     });
0110 
0111     QObject::connect(&app, &QApplication::aboutToQuit, [=]() {
0112         Config::self()->setShouldShow(false); // only relevant for Plasma 5.27 version
0113         Config::self()->setLastSeenVersion(QStringLiteral(PLASMA_WELCOME_VERSION_STRING));
0114         Config::self()->save();
0115     });
0116 
0117     return app.exec();
0118 }