File indexing completed on 2024-06-09 05:29:32

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 <KWindowSystem>
0014 #include <QAbstractListModel>
0015 #include <QApplication>
0016 #include <QCommandLineParser>
0017 #include <QDBusConnection>
0018 #include <QDBusConnectionInterface>
0019 #include <QDBusMessage>
0020 #include <QDebug>
0021 #include <QIcon>
0022 #include <QList>
0023 #include <QLocale>
0024 #include <QPainter>
0025 #include <QQmlApplicationEngine>
0026 #include <QQmlContext>
0027 #include <QQuickImageProvider>
0028 #include <QQuickWindow>
0029 #include <QSessionManager>
0030 
0031 #include "config-workspace.h"
0032 
0033 class EngineWatcher : public QObject
0034 {
0035 public:
0036     EngineWatcher(QQmlApplicationEngine *engine)
0037         : QObject(engine)
0038     {
0039         connect(engine, &QQmlApplicationEngine::objectCreated, this, &EngineWatcher::integrateObject);
0040     }
0041 
0042     void integrateObject(QObject *object)
0043     {
0044         QWindow *window = qobject_cast<QWindow *>(object);
0045 
0046         auto conf = KSharedConfig::openConfig();
0047         KWindowConfig::restoreWindowSize(window, conf->group(QStringLiteral("Window")));
0048 
0049         object->installEventFilter(this);
0050     }
0051 
0052     bool eventFilter(QObject *object, QEvent *event) override
0053     {
0054         if (event->type() == QEvent::Close) {
0055             QWindow *window = qobject_cast<QWindow *>(object);
0056 
0057             auto conf = KSharedConfig::openConfig();
0058             auto group = conf->group(QStringLiteral("Window"));
0059             KWindowConfig::saveWindowSize(window, group);
0060             group.sync();
0061         }
0062         return false;
0063     }
0064 };
0065 
0066 int main(int argc, char **argv)
0067 {
0068     QApplication app(argc, argv);
0069     app.setWindowIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-emoticons")));
0070     KCrash::initialize();
0071 
0072     KLocalizedString::setApplicationDomain(QByteArrayLiteral("org.kde.plasma.emojier"));
0073 
0074     KAboutData about(QStringLiteral("plasma.emojier"),
0075                      i18n("Emoji Selector"),
0076                      QStringLiteral(WORKSPACE_VERSION_STRING),
0077                      i18n("Emoji Selector"),
0078                      KAboutLicense::GPL,
0079                      i18n("(C) 2019 Aleix Pol i Gonzalez"));
0080     about.addAuthor(QStringLiteral("Aleix Pol i Gonzalez"), QString(), QStringLiteral("aleixpol@kde.org"));
0081     about.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"), i18nc("EMAIL OF TRANSLATORS", "Your emails"));
0082     //     about.setProductName("");
0083     about.setProgramLogo(app.windowIcon());
0084     KAboutData::setApplicationData(about);
0085 
0086     auto disableSessionManagement = [](QSessionManager &sm) {
0087         sm.setRestartHint(QSessionManager::RestartNever);
0088     };
0089     QObject::connect(&app, &QGuiApplication::commitDataRequest, disableSessionManagement);
0090     QObject::connect(&app, &QGuiApplication::saveStateRequest, disableSessionManagement);
0091 
0092     KDBusService::StartupOptions startup = {};
0093     {
0094         QCommandLineParser parser;
0095 
0096         QCommandLineOption replaceOption({QStringLiteral("replace")}, i18n("Replace an existing instance"));
0097         parser.addOption(replaceOption);
0098         about.setupCommandLine(&parser);
0099         parser.process(app);
0100         about.processCommandLine(&parser);
0101 
0102         if (parser.isSet(replaceOption)) {
0103             startup |= KDBusService::Replace;
0104         }
0105     }
0106 
0107     KDBusService *service = new KDBusService(KDBusService::Unique | startup, &app);
0108 
0109     QQmlApplicationEngine engine;
0110     new EngineWatcher(&engine);
0111 
0112     engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
0113     engine.loadFromModule("org.kde.plasma.emoji.app", "Emojier");
0114 
0115     QObject::connect(service, &KDBusService::activateRequested, &engine, [&engine](const QStringList & /*arguments*/, const QString & /*workingDirectory*/) {
0116         for (QObject *object : engine.rootObjects()) {
0117             auto w = qobject_cast<QQuickWindow *>(object);
0118             if (!w)
0119                 continue;
0120 
0121             KWindowSystem::updateStartupId(w);
0122             KWindowSystem::activateWindow(w);
0123 
0124             w->setVisible(true);
0125             w->raise();
0126         }
0127     });
0128 
0129     return app.exec();
0130 }