File indexing completed on 2024-05-12 17:07:03

0001 /*
0002     SPDX-FileCopyrightText: 2019 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include <KAboutData>
0008 #include <KCrash>
0009 #include <KDBusService>
0010 #include <KLocalizedString>
0011 #include <KSharedConfig>
0012 #include <KWindowConfig>
0013 #include <QAbstractListModel>
0014 #include <QApplication>
0015 #include <QCommandLineParser>
0016 #include <QDBusConnection>
0017 #include <QDBusConnectionInterface>
0018 #include <QDBusMessage>
0019 #include <QDebug>
0020 #include <QIcon>
0021 #include <QLocale>
0022 #include <QPainter>
0023 #include <QQmlApplicationEngine>
0024 #include <QQmlContext>
0025 #include <QQuickImageProvider>
0026 #include <QQuickWindow>
0027 #include <QSessionManager>
0028 #include <QVector>
0029 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0030 #include <QX11Info>
0031 #else
0032 #include <QtGui/private/qtx11extras_p.h>
0033 #endif
0034 
0035 #include "config-workspace.h"
0036 #include <kstartupinfo.h>
0037 
0038 class EngineWatcher : public QObject
0039 {
0040 public:
0041     EngineWatcher(QQmlApplicationEngine *engine)
0042         : QObject(engine)
0043     {
0044         connect(engine, &QQmlApplicationEngine::objectCreated, this, &EngineWatcher::integrateObject);
0045     }
0046 
0047     void integrateObject(QObject *object)
0048     {
0049         QWindow *window = qobject_cast<QWindow *>(object);
0050 
0051         auto conf = KSharedConfig::openConfig();
0052         KWindowConfig::restoreWindowSize(window, conf->group("Window"));
0053 
0054         object->installEventFilter(this);
0055     }
0056 
0057     bool eventFilter(QObject *object, QEvent *event) override
0058     {
0059         if (event->type() == QEvent::Close) {
0060             QWindow *window = qobject_cast<QWindow *>(object);
0061 
0062             auto conf = KSharedConfig::openConfig();
0063             auto group = conf->group("Window");
0064             KWindowConfig::saveWindowSize(window, group);
0065             group.sync();
0066         }
0067         return false;
0068     }
0069 };
0070 
0071 int main(int argc, char **argv)
0072 {
0073 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0074     QGuiApplication::setFallbackSessionManagementEnabled(false);
0075 #endif
0076     QApplication app(argc, argv);
0077     app.setAttribute(Qt::AA_UseHighDpiPixmaps, true);
0078     app.setWindowIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-emoticons")));
0079     KCrash::initialize();
0080 
0081     KLocalizedString::setApplicationDomain("org.kde.plasma.emojier");
0082 
0083     KAboutData about(QStringLiteral("plasma.emojier"),
0084                      i18n("Emoji Selector"),
0085                      QStringLiteral(WORKSPACE_VERSION_STRING),
0086                      i18n("Emoji Selector"),
0087                      KAboutLicense::GPL,
0088                      i18n("(C) 2019 Aleix Pol i Gonzalez"));
0089     about.addAuthor(QStringLiteral("Aleix Pol i Gonzalez"), QString(), QStringLiteral("aleixpol@kde.org"));
0090     about.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"), i18nc("EMAIL OF TRANSLATORS", "Your emails"));
0091     //     about.setProductName("");
0092     about.setProgramLogo(app.windowIcon());
0093     KAboutData::setApplicationData(about);
0094 
0095     auto disableSessionManagement = [](QSessionManager &sm) {
0096         sm.setRestartHint(QSessionManager::RestartNever);
0097     };
0098     QObject::connect(&app, &QGuiApplication::commitDataRequest, disableSessionManagement);
0099     QObject::connect(&app, &QGuiApplication::saveStateRequest, disableSessionManagement);
0100 
0101     KDBusService::StartupOptions startup = {};
0102     {
0103         QCommandLineParser parser;
0104 
0105         QCommandLineOption replaceOption({QStringLiteral("replace")}, i18n("Replace an existing instance"));
0106         parser.addOption(replaceOption);
0107         about.setupCommandLine(&parser);
0108         parser.process(app);
0109         about.processCommandLine(&parser);
0110 
0111         if (parser.isSet(replaceOption)) {
0112             startup |= KDBusService::Replace;
0113         }
0114     }
0115 
0116     KDBusService *service = new KDBusService(KDBusService::Unique | startup, &app);
0117 
0118     qmlRegisterAnonymousType<QAbstractItemModel>("emojier", 1);
0119 
0120     QQmlApplicationEngine engine;
0121     new EngineWatcher(&engine);
0122 
0123     engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
0124     engine.load(QUrl(QStringLiteral("qrc:/ui/emojier.qml")));
0125 
0126     QObject::connect(service, &KDBusService::activateRequested, &engine, [&engine](const QStringList & /*arguments*/, const QString & /*workingDirectory*/) {
0127         for (QObject *object : engine.rootObjects()) {
0128             auto w = qobject_cast<QQuickWindow *>(object);
0129             if (!w)
0130                 continue;
0131 
0132             if (w && QX11Info::isPlatformX11())
0133                 KStartupInfo::setNewStartupId(w, QX11Info::nextStartupId());
0134 
0135             w->setVisible(true);
0136             w->raise();
0137         }
0138     });
0139 
0140     return app.exec();
0141 }